Hello,

I wanted to sign a message using EVP interface, and got seg-fault on access
to the key passed to EVP_SignFinal.

. Do you have an idea why?

The code:

FILE *read_file = fopen ("mycert.pem", "r");
RSA *rsa_key = PEM_read_RSAPrivateKey(read_file, NULL, NULL, NULL);

unsigned char *start_buf = (unsigned char*)malloc(sizeof(unsigned char) *
1024);
int *buf_length = (int *)malloc(sizeof(int));

unsigned char *out_buf = (unsigned char*)malloc(sizeof(unsigned char) *
1024);
int *out_buf_length = (int *)malloc(sizeof(int));

strcpy(start_buf, "signed content");
EVP_MD_CTX signature_digest_ctx;

EVP_MD_CTX_init(&signature_digest_ctx);

int res= EVP_SignInit_ex(&signature_digest_ctx, EVP_sha1(), NULL);
res = EVP_SignUpdate(&signature_digest_ctx, start_buf, buf_length);
EVP_PKEY *pkey = EVP_PKEY_new();
EVP_PKEY_assign_RSA(pkey, rsa_key);

*res = EVP_SignFinal(&signature_digest_ctx, out_buf, out_buf_length, pkey);
// seg-fault because pkey is freed twice, I don't understand why.*

Thanks,
John

Reply via email to