Get key and certificate from * .pfx file using openssl and C

I have this code:

int importKey(){

FILE *fp=NULL;
RSA *pkey=NULL;
R_RSA_PRIVATE_KEY prk; //special structure

fp = fopen("sslcert/key.pem", "r");

fseek(fp, 0, SEEK_SET);
PEM_read_RSAPrivateKey(fp, &pkey, NULL, NULL);

if (!pkey)
{
    fseek(fp, 0, SEEK_SET);
    d2i_RSAPrivateKey_fp(fp, &pkey);
}

prk.bits=BN_num_bits(pkey->n);
return pkey; //check if pkey==0 or something else
}

This works fine when I transfer the file .pemto fp that I created from the command line using openssl -pkcs12 -in file.pfx -out key.pem. But I need to use this pfx file in fp = fopen()and somehow “extract” the private key inside the code and save it in this RSA * pkey, as well as extract the certificate from the same pfx file and save it in the X509 * px509 variable. Any help on this?

Meaning I actually need some openssl functions to execute some procedure as a command line command

+3
source share

All Articles