Fix a possible memory leak in dh_cms_encrypt
authorBernd Edlinger <bernd.edlinger@hotmail.de>
Sun, 10 Dec 2023 14:07:08 +0000 (15:07 +0100)
committerTomas Mraz <tomas@openssl.org>
Fri, 22 Dec 2023 12:45:37 +0000 (13:45 +0100)
Add a missing check of the return code of X509_ALGOR_set0,
otherwise the ASN1_STRING object wrap_str may be leaked.

Reviewed-by: Tom Cosgrove <tom.cosgrove@arm.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/22998)

crypto/cms/cms_dh.c

index 9509796317b36f929b6de43e37583dc3a9a00a62..2f54ed2673a9881e33830af334465403019da888 100644 (file)
@@ -316,10 +316,10 @@ static int dh_cms_encrypt(CMS_RecipientInfo *ri)
         goto err;
     ASN1_STRING_set0(wrap_str, penc, penclen);
     penc = NULL;
-    X509_ALGOR_set0(talg, OBJ_nid2obj(NID_id_smime_alg_ESDH),
-                    V_ASN1_SEQUENCE, wrap_str);
-
-    rv = 1;
+    rv = X509_ALGOR_set0(talg, OBJ_nid2obj(NID_id_smime_alg_ESDH),
+                         V_ASN1_SEQUENCE, wrap_str);
+    if (!rv)
+        ASN1_STRING_free(wrap_str);
 
  err:
     OPENSSL_free(penc);