Commit 7a1e1550c0 for openssl.org
commit 7a1e1550c0b778eae83602eb72ab55997d6cc8af
Author: Jakub Zelenka <jakub.zelenka@openssl.foundation>
Date: Tue Nov 24 15:13:57 2026 +0100
pkcs7: free propq when freed by ASN.1 template
The property query copy held by a PKCS7 object was released only
by the PKCS7 and PKCS12 free functions. It leaked whenever the object
was freed through its ASN.1 template instead, which happens when decoding
into an existing object fails, or when the object is a member of another
structure, such as a timestamp response.
Free it from the ASN.1 callback, as CMS does, so that every free path
releases it.
Resolves: https://github.com/openssl/openssl/issues/33511
Fixes: fe2a7341b504 "PKCS12 - Add additional libctx and propq support."
Fixes: 90a1f2d76f53 "Add libctx support to PKCS7."
Assisted-by: Claude:claude-fable-5-2
Reviewed-by: Richard Levitte <levitte@openssl.foundation>
Reviewed-by: Nikola Pajkovsky <nikolap@openssl.org>
Reviewed-by: Eugene Syromiatnikov <esyr@openssl.org>
Merge-date: Wed Dec 2 06:20:17 2026
Merged-from: https://github.com/openssl/openssl/pull/33962
diff --git a/crypto/pkcs12/p12_asn.c b/crypto/pkcs12/p12_asn.c
index 79fbd33606..910b034bf5 100644
--- a/crypto/pkcs12/p12_asn.c
+++ b/crypto/pkcs12/p12_asn.c
@@ -31,10 +31,6 @@ PKCS12 *PKCS12_new(void)
void PKCS12_free(PKCS12 *p12)
{
- if (p12 != NULL && p12->authsafes != NULL) {
- OPENSSL_free(p12->authsafes->ctx.propq);
- p12->authsafes->ctx.propq = NULL;
- }
ASN1_item_free((ASN1_VALUE *)p12, ASN1_ITEM_rptr(PKCS12));
}
diff --git a/crypto/pkcs7/pk7_asn1.c b/crypto/pkcs7/pk7_asn1.c
index b3a1a9d39e..d565278c53 100644
--- a/crypto/pkcs7/pk7_asn1.c
+++ b/crypto/pkcs7/pk7_asn1.c
@@ -59,6 +59,10 @@ static int pk7_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
*content = ossl_pkcs7_get0_stream_content(*pp7);
} break;
+
+ case ASN1_OP_FREE_POST:
+ OPENSSL_free((*pp7)->ctx.propq);
+ break;
}
return 1;
}
@@ -117,10 +121,7 @@ PKCS7 *PKCS7_new_ex(OSSL_LIB_CTX *libctx, const char *propq)
void PKCS7_free(PKCS7 *p7)
{
- if (p7 != NULL) {
- OPENSSL_free(p7->ctx.propq);
- ASN1_item_free((ASN1_VALUE *)p7, ASN1_ITEM_rptr(PKCS7));
- }
+ ASN1_item_free((ASN1_VALUE *)p7, ASN1_ITEM_rptr(PKCS7));
}
IMPLEMENT_ASN1_NDEF_FUNCTION(PKCS7)
diff --git a/test/pkcs7_test.c b/test/pkcs7_test.c
index 120cffbe8a..55f727f6ed 100644
--- a/test/pkcs7_test.c
+++ b/test/pkcs7_test.c
@@ -603,6 +603,37 @@ end:
return ret;
}
+/* The property query copy must be freed when the ASN.1 template frees p7 */
+static int pkcs7_propq_template_free_test(void)
+{
+ /* SEQUENCE { INTEGER 5 } is not a ContentInfo */
+ static const unsigned char bad[] = { 0x30, 0x03, 0x02, 0x01, 0x05 };
+ const unsigned char *in = bad;
+ PKCS7 *p7 = NULL, *inner = NULL;
+ int ret = 0;
+
+ /* Failed decoding into an existing object frees it */
+ if (!TEST_ptr(p7 = PKCS7_new_ex(NULL, "provider=default"))
+ || !TEST_ptr_null(d2i_PKCS7(&p7, &in, sizeof(bad)))
+ || !TEST_ptr_null(p7))
+ goto end;
+
+ /* The inner content is freed as a member of the outer structure */
+ if (!TEST_ptr(p7 = PKCS7_new_ex(NULL, "provider=default"))
+ || !TEST_ptr(inner = PKCS7_new_ex(NULL, "provider=default"))
+ || !TEST_true(PKCS7_set_type(p7, NID_pkcs7_signed))
+ || !TEST_true(PKCS7_set_type(inner, NID_pkcs7_data))
+ || !TEST_true(PKCS7_set_content(p7, inner)))
+ goto end;
+ inner = NULL;
+
+ ret = 1;
+end:
+ PKCS7_free(inner);
+ PKCS7_free(p7);
+ return ret;
+}
+
int setup_tests(void)
{
const char *certin, *privkeyin;
@@ -637,6 +668,7 @@ int setup_tests(void)
ADD_TEST(test_pkcs7_smimecap);
ADD_TEST(pkcs7_stream_non_data_test);
ADD_TEST(pkcs7_null_contents_test);
+ ADD_TEST(pkcs7_propq_template_free_test);
return 1;
}