Hi!
I'm having some problems to sign my data with encrypted RSA keys. I'm doing the following sequence:
// Create the RSA key pair and write into PEM files
RSA *pair = RSA_generate_key(1024, 3, NULL, NULL);
EVP_PKEY *pkey = EVP_PKEY_new();
EVP_PKEY_assign_RSA(pkey, pair);
FILE *fp = fopen("rsaprivatekey.pem", "w");
const EVP_CIPHER *c = EVP_des_ede3_cbc(); // *****encrypt with 3-des
string password = "abc123";
int klen = password.length();
unsigned char *kstr = new unsigned char[klen];
memcpy(kstr, password.c_str(), klen);
PEM_write_PrivateKey(fp,pkey, c, kstr, klen, NULL, NULL); // write the private key into a file
fclose(fp);
// **** Writes the public key into a file
fp = fopen("rsapublickey.pem", "w");
PEM_write_RSAPublicKey(fp,pair);
fclose(fp);
// Read the PEM files
string password = "abc123";
FILE *fp = fopen("rsaprivatekey.pem", "r");
int klen = password.length();
unsigned char *kstr = new unsigned char[klen];
memcpy(kstr, password.c_str(), klen);
EVP_PKEY *pkey = PEM_read_PrivateKey(fp, NULL, NULL, kstr);
fclose(fp);
// Try to sign the data
EVP_MD_CTX ctx;
EVP_MD_CTX_init(&ctx);
const EVP_MD *digest_type = EVP_sha1(); // *** I must use SHA1 as digest algorithm
EVP_SignInit_ex(&ctx, digest_type, NULL);
EVP_SignUpdate(&ctx, data, data_len); //data has some content
unsigned char sig[EVP_PKEY_size(pkey)];
unsigned int siglen = 0;
// Sign the data with RSA-SHA1
EVP_SignFinal(&ctx, sig, &siglen, pkey) // ********** HERE I HAVE AN ERROR:
ERROR: (error:0606B06E:lib(6):func(107):reason(110)) = > This means function SignFinal, reason EVP_R_WRONG_PUBLIC_KEY_TYPE.
When I try with encrypted DSA keys it works, and with unencrypted RSA keys it works too.... What is the problem with my encrypted RSA keys above??
Thanks for the help!