Fix d2i_PrivateKey_ex() to work as documented
authorRichard Levitte <levitte@openssl.org>
Mon, 11 May 2020 07:14:11 +0000 (09:14 +0200)
committerRichard Levitte <levitte@openssl.org>
Tue, 12 May 2020 04:48:47 +0000 (06:48 +0200)
d2i_PrivateKey(), and thereby d2i_PrivateKey_ex(), is documented to
return keys of the type given as first argument |type|, unconditionally.
Most specifically, the manual says this:

> An error occurs if the decoded key does not match type.

However, when faced of a PKCS#8 wrapped key, |type| was ignored, which
may lead to unexpected results.

Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/11787)

crypto/asn1/d2i_pr.c

index c7346f542407a09e86c14b006e3e0e75a7834c5f..3ddc56d4082058c909148066a4cb5ab57984f646 100644 (file)
@@ -58,6 +58,8 @@ EVP_PKEY *d2i_PrivateKey_ex(int type, EVP_PKEY **a, const unsigned char **pp,
                 goto err;
             EVP_PKEY_free(ret);
             ret = tmp;
+            if (EVP_PKEY_type(type) != EVP_PKEY_base_id(ret))
+                goto err;
         } else {
             ASN1err(0, ERR_R_ASN1_LIB);
             goto err;