I have the content of a public key which I'm getting passed as an input to my system:
public_key = 'MDkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDIgAC/Bu9nyAtG1DQe7t7jszLb+dZ1GbXoR8G0rIXoak67NM='
I need to feed this into OpenSSL so it can take part in some ECDH decryption.
I can't seem to find a way to make OpenSSL accept the above input.
I've tried the following:
OpenSSL::PKey::RSA.new public_keyOpenSSL::PKey::RSA.new "-----BEGIN PUBLIC KEY-----\n#{public_key}\n-----END PUBLIC KEY-----\n"
Both of these return:
OpenSSL::PKey::RSAError: Neither PUB key nor PRIV key: nested asn1 error
I can do:
OpenSSL::PKey.read "-----BEGIN PUBLIC KEY-----\n#{public_key}\n-----END PUBLIC KEY-----\n"
This doesn't return an error, however I can't then see a way to set the group as secp128r2 without throwing an error.
Doing c = OpenSSL::PKey::EC.new("secp128r2") and trying to set the key after with c.public_key = OpenSSL::PKey.read("-----BEGIN PUBLIC KEY-----\n#{public_key}\n-----END PUBLIC KEY-----\n").public_key throws the error:
OpenSSL::PKey::ECError: EC_KEY_set_public_key: incompatible objects
.crtfile, and that you should either read the file verbatim or, better, tell the library where the file is located. I recommend against embedding the key into the source-code of the program where it would be unduly difficult to change. (Good security practice calls for public keys to have expiry dates and to be replaced periodically. Make this trivial to do.)