Search code examples
opensslprivate-key

Generate EVP_PKEY from Private Key Bytes In PEM Format


I have a private key bytes stored in PEM format, in a variable of Type LPSTR. i.e

LPSTR pPrivateKeyInPem; 

Now I need to generate an EVP_PKEY using pPrivateKeyInPem, so that it can be loaded into an SSL_CTX Object using the SSL_CTX_use_PrivateKey() API of Openssl.

How can I do this?


Solution

  • I'll omit conversion from LPSTR to char*, which is covered here: Convert lptstr to char*

    For the OpenSSL part

    BIO *mem;
    mem = BIO_new_mem_buf(pkey, -1); //pkey is of type char*
    
    key = PEM_read_bio_PrivateKey(mem, NULL, NULL, 0);