EVP: Downgrade EVP_PKEYs in EVP_PKEY2PKCS8()
authorRichard Levitte <levitte@openssl.org>
Sun, 2 Aug 2020 14:41:04 +0000 (16:41 +0200)
committerRichard Levitte <levitte@openssl.org>
Thu, 3 Sep 2020 15:48:32 +0000 (17:48 +0200)
EVP_PKEY2PKCS8() relies on the presence of an EVP_PKEY_ASN1_METHOD,
which requires "downgrading" the EVP_PKEY to have a legacy internal
key.

Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/12587)

crypto/evp/evp_pkey.c

index 92ae3e5fe8fc794f04ee68a8853ef1804b47624a..d435c86087ca9f4002ff1be39449cb893549c044 100644 (file)
@@ -77,6 +77,11 @@ PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(const EVP_PKEY *pkey)
         return NULL;
     }
 
+    /* Force a key downgrade if that's possible */
+    /* TODO(3.0) Is there a better way for provider-native keys? */
+    if (EVP_PKEY_get0(pkey) == NULL)
+        return NULL;
+
     if (pkey->ameth) {
         if (pkey->ameth->priv_encode) {
             if (!pkey->ameth->priv_encode(p8, pkey)) {