Avoid a null pointer deref on a malloc failure
authorMatt Caswell <matt@openssl.org>
Wed, 24 Feb 2021 15:04:41 +0000 (15:04 +0000)
committerMatt Caswell <matt@openssl.org>
Mon, 8 Mar 2021 15:02:07 +0000 (15:02 +0000)
Make sure we were sucessful in creating an EVP_PKEY

Reviewed-by: Richard Levitte <levitte@openssl.org>
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/14319)

crypto/evp/p_lib.c

index 63f3f4cbc7b1eb81974a3da1fd2c3777ca146275..ef38c5e3333a1fb69d0bde58a235c778369a2aec 100644 (file)
@@ -1824,10 +1824,15 @@ int evp_pkey_copy_downgraded(EVP_PKEY **dest, const EVP_PKEY *src)
             keytype = OBJ_nid2sn(type);
 
         /* Make sure we have a clean slate to copy into */
-        if (*dest == NULL)
+        if (*dest == NULL) {
             *dest = EVP_PKEY_new();
-        else
+            if (*dest == NULL) {
+                ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
+                return 0;
+            }
+        } else {
             evp_pkey_free_it(*dest);
+        }
 
         if (EVP_PKEY_set_type(*dest, type)) {
             /* If the key is typed but empty, we're done */