I am implementing an encryption / decryption scheme in my application using OpenSSL's high-level EVP_*()
functions, so I can easily switch the actual algorithms used without having to change the API calls.
I can create a key pair with relative ease:
// dumbed down, no error checking for brevity
EVP_PKEY * pkey;
// can change EVP_PKEY_RSA to something else here
EVP_PKEY_CTX * context = EVP_PKEY_CTX_new_id( EVP_PKEY_RSA, NULL );
EVP_PKEY_keygen_init( ctx );
// could set parameters here
EVP_PKEY_keygen( context, &pkey );
// ...
EVP_PKEY_CTX_free( context );
pkey
now holds a key pair, i.e. both secret and public key. That's fine for the secret side of things, but obviously I would like to extract only the public key component for use on the public side of things.
I was able to find RSA-specific functions, but nothing using the high-level EVP_*()
API.
Help?
PKEY
? Or do you want to know how to save a public key? Or something else? – InkermanEVP_PKEY
data structure that contains only the public key component. – FishboltEVP_*
gear (it can be done, but it takes some work). Usually you write out a DER or PEM encoded key (public or private). Have you looked at the functions inpem(3)
gear. With the*_bio
functions, you can write to file or memory. Take a look at, for example,<openssl src>/apps/rsautl.c
(for the RSA case). – InkermanEVP_PKEY
. During the trip, you have to get the key in its native format (likeRSA*
orDSA*
) and convert it from private to public. Then you send it back to anEVP_PKEY
. But like I said, if all you need is an DER or PEM encoded key to distribute, use the stuff inpem(3)
. – Inkerman