Search code examples
opensslcryptographyecdsa

Is there an OpenSSL function like SSL_CTX_use_*PrivateKey for ECDSA?


I'm a newcomer to crypto in general and OpenSSL in particular. I'm still in the learning phase, and am looking over the documentation (and even the source code). I see functions called: SSL_CTX_use_PrivateKey SSL_CTX_use_RSAPrivateKey

But I don't see one called something similar to SSL_CTX_use_ECDSAPrivateKey.

Is there such and function, and just can't find it? Or is ECDSA handled in another manner?


Solution

  • You should use SSL_CTX_use_PrivateKey for ECDSA. This function is a generic one for handling all key types. In fact in the forthcoming OpenSSL 3.0, SSL_CTX_use_RSAPrivateKey is deprecated and SSL_CTX_use_PrivateKey is the preferred function for all key types including RSA.

    In order to use SSL_CTX_use_PrivateKey you need to pass the the key as an EVP_PKEY object. If you have the key in the form of an EC_KEY object then you can create a new EVP_PKEY using EVP_PKEY_new() and then assign your EC_KEY to it using EVP_PKEY_set1_EC_KEY or EVP_PKEY_assign_EC_KEY. See:

    https://www.openssl.org/docs/man1.1.1/man3/EVP_PKEY_new.html https://www.openssl.org/docs/man1.1.1/man3/EVP_PKEY_set1_EC_KEY.html

    As an alternative you can use SSL_CTX_use_PrivateKey_file which will just load the key automatically from the file. Again you can use this function for any key type (including RSA and ECDSA).