d2i_X509(): Make deallocation behavior consistent with d2i_X509_AUX()
authorDr. David von Oheimb <David.von.Oheimb@siemens.com>
Wed, 30 Dec 2020 08:46:38 +0000 (09:46 +0100)
committerDr. David von Oheimb <dev@ddvo.net>
Wed, 13 Jan 2021 10:19:17 +0000 (11:19 +0100)
Partly fixes #13754

Reviewed-by: Tomas Mraz <tmraz@fedoraproject.org>
(Merged from https://github.com/openssl/openssl/pull/13755)

crypto/x509/x_x509.c

index b09fa2754ad9109f8a73c858c3855a077edb60e7..287b6c2a1ec2884ab9252217f06ed6a128c02b16 100644 (file)
@@ -125,12 +125,16 @@ IMPLEMENT_ASN1_DUP_FUNCTION(X509)
 X509 *d2i_X509(X509 **a, const unsigned char **in, long len)
 {
     X509 *cert = NULL;
+    int free_on_error = a != NULL && *a == NULL;
 
     cert = (X509 *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, (X509_it()));
     /* Only cache the extensions if the cert object was passed in */
     if (cert != NULL && a != NULL) {
-        if (!x509v3_cache_extensions(cert))
+        if (!x509v3_cache_extensions(cert)) {
+            if (free_on_error)
+                X509_free(cert);
             cert = NULL;
+        }
     }
     return cert;
 }