How to load an RSA key from binary data to an RSA structure using the OpenSSL C Library?
c, c++, openssl, rsa
Solution
Use `d2i_RSAPrivateKey` to load directly from a buffer containing binary DER format:
const unsigned char *p = key;
RSA *r = d2i_RSAPrivateKey(NULL, &p, keylen);
Problem
Currently I have my private key saved in a file, private.key, and I use the following function to load it: RSA *r = PEM_read_RSAPrivateKey("private.key", NULL, NULL, NULL); This works perfectly but I'm not happy with the file-based format; I want to save my key in pure binary form (ie, no base64 or similar) in a `char*` variable and load/save the key from/to it. This way I have much more freedom: I'll be able to store the key directly into the application `const char key[] { 0x01, 0x02, ... };`, send it over a network socket, etc. Unfortunately though I haven't found a way to do that. The only way to save and load a key I know of reads/saves it to a file directly.