I am facing one silly issue in storing my DH Keys in PEM Format. Here is working code snippet to generate Keys in Diffie Hellman format, I can store the DH Params in PEM Format. However I don't find any function in openSSL which can store the Keys also in PEM Format.
DH *privkey = DH_new();
/* Generate the parameters to be used */
DH_generate_parameters_ex(privkey, 2048, DH_GENERATOR_2, NULL)
/* Generate the public and private key pair */
DH_generate_key(privkey)
/* Store DH Params in PEM Format */
FILE* fptr = fopen("dhp.pem", "w");
PEM_write_DHparams(fptr, privkey);
/* Get PublicKey of Peer To generate Shared Secret ----*/
BIGNUM *pubkeyPeer = NULL;
BN_dec2bn(&pubkeyPeer, BN_bn2dec(GetPubKeyPeer());
/* Generate Shared Secret by getting Public Key of Peer */
unsigned char *secret;
int secret_size;
if (NULL == (secret= (unsigned char *)OPENSSL_malloc(sizeof(unsigned char) * (DH_size(privkey))))) {
printf("Can Not Allocate Memory for Shared Secret ");
}
if (0 > (secret_size = DH_compute_key(secret, pubkeyPeer, privkey))) {
printf("Shared Secret Generation Failure ");
}
/* DUMP Shared Secret ---*/
BIO_dump_fp(stdout, (const char *)secret, secret_size);
This all is fine, But I need to share my Public Key with Peer in PEM Format. Is there some function to convert my DH Public Key to PEM Format?