cryptography - How do I decrypt a private key file and sign some text using openssl calls in C? -
i have 2 separate programs (spliced below). first generates key pair , saves files (works fine). second opens private key, decrypting pass phrase , need sign string of text. code below fails on pem_read_privatekey() (last) call (can't see why). can point me @ doing wrong , openssl calls should make use private key sign text?
int main (int argc, char *argv[]) { char *priv_pem = "priv.pem"; char *pub_pem = "pub.pem"; char *pass = "password"; file *fp; int bits = 4096; unsigned long exp = rsa_f4; rsa *rsa; evp_pkey *pkey; // generate key rsa=rsa_generate_key(bits,exp,null,null); if (rsa_check_key(rsa)!=1) exit(1,"error whilst checking key",""); pkey = evp_pkey_new(); evp_pkey_assign_rsa(pkey, rsa); // write encrypted private key if (!(fp = fopen(priv_pem, "w"))) exit(2,"error opening pem file",priv_pem); if (!pem_write_privatekey(fp,pkey,evp_aes_256_cbc(),null,0,null,pass)) exit(3,"error writing pem file",priv_pem); fclose(fp); // write public key if (!(fp = fopen(pub_pem, "w"))) exit(4,"error opening pem file",pub_pem); if (!pem_write_pubkey(fp, pkey)) exit(5,"error writing pem file",pub_pem); fclose(fp); // ------- end of key generation program ------- // ------- start of text signing program ------- // read in encrypted private key if (!(fp = fopen(priv_pem, "r"))) exit(6,"error reading encrypted private key file",priv_pem); if (!pem_read_privatekey(fp,&pkey,null,pass)) exit(7,"error decrypting private key file",priv_pem); fclose(fp); // sign text using private key.... // free rsa_free(rsa); return 0; }
have initialised pkey
null
before pass &pkey
pem_read_privatekey()
? if not, attempt re-use evp_pkey
structure pkey
points - , if pkey
uninitialised, looking @ random spot in memory.
you can use err_print_errors_fp(stderr);
dump openssl error stack stderr
when error occurs - helpful in finding problem.
Comments
Post a Comment