I am trying to sign some data using a DSA certificate. I am saving the certificate in memory (it was generated with the openssl gendsa command).
My function looks like this, and my problem is with res = EVP_SignFinal
. Here the function returns 0, and sets the signature_len on 0.
bool my_dsa_sign(const Certificate& certificate, const char* messageData, size_t messageLength, Signature &outSignature) {
bool resultOk = false;
BIO* bio = BIO_new_mem_buf((void*) certificate.getPEMData(), certificate.getSize());
if(NULL != bio) {
EVP_PKEY *pkey = NULL;
if(NULL != (pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL))) {
unsigned int signature_len;
EVP_MD_CTX ctx;
int res = EVP_SignInit(&ctx, EVP_sha512());
if(1 == res) {
res = EVP_SignUpdate(&ctx, messageData, messageLength);
if(1 == res) {
unsigned char* s = (unsigned char*)malloc(EVP_PKEY_size(pkey));
// problem here
res = EVP_SignFinal(&ctx, s, &signature_len, pkey);
if(1 == res) {
resultOk = true;
signature.setData(s, signature_len);
}
}
}
EVP_PKEY_free(pkey);
EVP_MD_CTX_cleanup(&ctx);
}
BIO_free(bio);
}
return resultOk;
}
any idea what the problem might be ? I have checked with the debugger, the value of the certificate is correct, in the form:
-----BEGIN DSA PRIVATE KEY-----
MIID....
.......
-----END DSA PRIVATE KEY-----
Also the length is correct, matches with that of the file.