Add NULL check in i2d_PrivateKey()
authorRichard Levitte <levitte@openssl.org>
Mon, 9 May 2016 19:52:11 +0000 (21:52 +0200)
committerRichard Levitte <levitte@openssl.org>
Mon, 9 May 2016 20:30:25 +0000 (22:30 +0200)
Originally submitted by Kurt Cancemi <kurt@x64architecture.com>

Closes RT#4533

Reviewed-by: Matt Caswell <matt@openssl.org>
(cherry picked from commit 59a56c4cf02bbf1efeda6c2a5893d5079db78ff3)

crypto/asn1/i2d_pr.c

index 4d338ac55aed4e9b9b3a87600558d685abdac81d..12966ec536e1a6c98b70309f6d5bc48043c32f76 100644 (file)
@@ -69,10 +69,13 @@ int i2d_PrivateKey(EVP_PKEY *a, unsigned char **pp)
     }
     if (a->ameth && a->ameth->priv_encode) {
         PKCS8_PRIV_KEY_INFO *p8 = EVP_PKEY2PKCS8(a);
-        int ret = i2d_PKCS8_PRIV_KEY_INFO(p8, pp);
-        PKCS8_PRIV_KEY_INFO_free(p8);
+        int ret = 0;
+        if (p8 != NULL) {
+            ret = i2d_PKCS8_PRIV_KEY_INFO(p8, pp);
+            PKCS8_PRIV_KEY_INFO_free(p8);
+        }
         return ret;
     }
     ASN1err(ASN1_F_I2D_PRIVATEKEY, ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
-    return (-1);
+    return -1;
 }