How can I get the public/private keys from an ECC-based X509Certificate2
's into CngKey
's for use with ECDsaCng
and ECDiffieHellmanCng
?
I'm currently using RSA 2048 bit key pairs to sign/encrypt stuff. I'm doing this by pulling the certificates from the X509Store
where they are securely stored with private keys marked as non-exportable. I would like to convert the current implementation to use ECDSA and ECDH so that I can use smaller key sizes for equivalent security.
I've successfully generated ECC certs using openssl:
openssl ecparam -out private.pem -name prime256v1 -genkey
openssl req -new -key private.pem -x509 -nodes -days 365 -out public.cer
openssl pkcs12 -export -in public.cer -inkey private.pem -out export.pfx
I've successfully installed the above generated certs in to the cert store. I can retrieve them by thumbprint, but the crypto providers for the private and public keys throw "Algorithm not supported" exceptions. Instead, I understand I'm supposed to use ECDsaCng
and ECDiffieHellmanCng
to sign/encrypt. But these deal in CngKey
's.
Bouncy Castle isn't an option because it requires the private keys to be exportable.
CLR Security will return me a CngKey
pair via GetCngPrivateKey
but it cannot be used with ECDsa because the key returned by CLRSecurity is an ECDH key. Furthermore CLR Security doesn't give me a way to get just the public key from an X509Certificate2
for signature verification (where I don't even have or need the private key of the signer).
Any ideas? I'm at my wits end... Any help would be much appreciated.