An EVP_PKEY can be saved directly to disk in a several formats. PEM_write_PrivateKey is used to save EVP_PKEY in a PEM format:
FILE *f;
f = fopen("key.pem", "wb");
PEM_write_PrivateKey(
f, /* use the FILE* that was opened */
pkey, /* EVP_PKEY structure */
EVP_des_ede3_cbc(), /* default cipher for encrypting the key on disk */
"replace_me", /* passphrase required for decrypting the key on disk */
10, /* length of the passphrase string */
NULL, /* callback for requesting a password */
NULL /* data to pass to the callback */
);
To save a private key to a BIO, use PEM_write_bio_PrivateKey:
BIO *bio;
bio = BIO_new(BIO_s_mem());
PEM_write_bio_PrivateKey(
bio, /* BIO to write the private key to */
pkey, /* EVP_PKEY structure */
EVP_des_ede3_cbc(), /* default cipher for encrypting the key on disk */
"replace_me", /* passphrase required for decrypting the key on disk */
10, /* length of the passphrase string */
NULL, /* callback for requesting a password */
NULL /* data to pass to the callback */
);