Есть два ключа:
1) openssl genrsa -out file_priv.pem 1024
2) openssl rsa -pubout -in file_priv.pem -out file_pub.pem
Программа из примеров openssl написана для сертификатов. Текст:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
#include <stdio.h>
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <openssl/objects.h>
#include <openssl/x509.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/ssl.h>
int main ()
{
int err;
int sig_len;
unsigned char sig_buf [ 4096 ];
static char certfile[] = "cert.pem";
static char keyfile[] = "key.pem";
static char data[] = "str";
EVP_MD_CTX md_ctx;
EVP_PKEY * pkey;
FILE * fp,*fp2;
X509 * x509;
/* Just load the crypto library error strings,
* SSL_load_error_strings() loads the crypto AND the SSL ones */
/* SSL_load_error_strings();*/
ERR_load_crypto_strings();
/* Read private key */
fp2= fopen (sign2, "r+");
fp = fopen (keyfile, "r");
if (fp == NULL) exit ( 1 );
pkey = PEM_read_PrivateKey(fp, NULL, NULL, NULL);
fclose (fp);
if (pkey == NULL) {
ERR_print_errors_fp (stderr);
exit ( 1 );
}
/* Do the signature */
EVP_SignInit (&md_ctx, EVP_sha1());
EVP_SignUpdate (&md_ctx, data, strlen(data));
sig_len = sizeof(sig_buf);
err = EVP_SignFinal (&md_ctx, sig_buf, &sig_len, pkey);
if (err != 1 ) {
ERR_print_errors_fp(stderr);
exit ( 1 );
}
EVP_PKEY_free (pkey);
/* Read public key */
fp = fopen (certfile, "r");
if (fp == NULL) exit ( 1 );
x509 = PEM_read_X509(fp, NULL, NULL, NULL);
//x509 = PEM_read_RSAPublicKey(fp, NULL, NULL, NULL);
fclose (fp);
if (x509 == NULL) {
ERR_print_errors_fp (stderr);
exit ( 1 );
}
/* Get public key - eay */
pkey=X509_get_pubkey(x509);
if (pkey == NULL) {
ERR_print_errors_fp (stderr);
exit ( 1 );
}
/* Verify the signature */
EVP_VerifyInit (&md_ctx, EVP_sha1());
EVP_VerifyUpdate (&md_ctx, data, strlen((char*)data));
err = EVP_VerifyFinal (&md_ctx, sig_buf, sig_len, pkey);
EVP_PKEY_free (pkey);
if (err != 1 ) {
ERR_print_errors_fp (stderr);
exit ( 1 );
}
printf ("Signature Verified Ok.\n");
return( 0 );
}
Как нужно ее модифицировать,чтобы она работала с вышеописанными ключами.
Какие функции использовать вместо
x509 = PEM_read_X509(fp, NULL, NULL, NULL)
?
При изменении выдает ошибку
15316 :error:0906D06C:PEM routines:PEM_read_bio:no start line:pem_lib.c: 642 :Expecting: RSA PUBLIC KEY