e79d4b
From 57afc290cd3ce2e9752a0ce5cba41ecc78fdc1bd Mon Sep 17 00:00:00 2001
e79d4b
From: rpm-build <rpm-build>
e79d4b
Date: Sun, 31 Jul 2022 10:39:53 +0900
e79d4b
Subject: [PATCH] gnutls-3.7.6-pkcs7-verify.patch
e79d4b
e79d4b
---
e79d4b
 lib/x509/pkcs7.c                 |   3 +-
e79d4b
 tests/Makefile.am                |   2 +-
e79d4b
 tests/pkcs7-verify-double-free.c | 215 +++++++++++++++++++++++++++++++
e79d4b
 3 files changed, 218 insertions(+), 2 deletions(-)
e79d4b
 create mode 100644 tests/pkcs7-verify-double-free.c
e79d4b
e79d4b
diff --git a/lib/x509/pkcs7.c b/lib/x509/pkcs7.c
e79d4b
index 1f35fab..d5be7f4 100644
e79d4b
--- a/lib/x509/pkcs7.c
e79d4b
+++ b/lib/x509/pkcs7.c
e79d4b
@@ -1318,7 +1318,8 @@ gnutls_x509_crt_t find_signer(gnutls_pkcs7_t pkcs7, gnutls_x509_trust_list_t tl,
e79d4b
 				issuer = find_verified_issuer_of(pkcs7, issuer, purpose, vflags);
e79d4b
 
e79d4b
 				if (issuer != NULL && gnutls_x509_crt_check_issuer(issuer, issuer)) {
e79d4b
-					if (prev) gnutls_x509_crt_deinit(prev);
e79d4b
+					if (prev && prev != signer)
e79d4b
+						gnutls_x509_crt_deinit(prev);
e79d4b
 					prev = issuer;
e79d4b
 					break;
e79d4b
 				}
e79d4b
diff --git a/tests/Makefile.am b/tests/Makefile.am
e79d4b
index 26e39fb..7a7a4af 100644
e79d4b
--- a/tests/Makefile.am
e79d4b
+++ b/tests/Makefile.am
e79d4b
@@ -233,7 +233,7 @@ ctests += mini-record-2 simple gnutls_hmac_fast set_pkcs12_cred cert certuniquei
e79d4b
 	 tls13-without-timeout-func buffer status-request-revoked \
e79d4b
 	 set_x509_ocsp_multi_cli kdf-api keylog-func handshake-write \
e79d4b
 	 x509cert-dntypes id-on-xmppAddr tls13-compat-mode ciphersuite-name \
e79d4b
-	 x509-upnconstraint
e79d4b
+	 x509-upnconstraint pkcs7-verify-double-free
e79d4b
 
e79d4b
 ctests += tls-channel-binding
e79d4b
 
e79d4b
diff --git a/tests/pkcs7-verify-double-free.c b/tests/pkcs7-verify-double-free.c
e79d4b
new file mode 100644
e79d4b
index 0000000..fadf307
e79d4b
--- /dev/null
e79d4b
+++ b/tests/pkcs7-verify-double-free.c
e79d4b
@@ -0,0 +1,215 @@
e79d4b
+/*
e79d4b
+ * Copyright (C) 2022 Red Hat, Inc.
e79d4b
+ *
e79d4b
+ * Author: Zoltan Fridrich
e79d4b
+ *
e79d4b
+ * This file is part of GnuTLS.
e79d4b
+ *
e79d4b
+ * GnuTLS is free software: you can redistribute it and/or modify it
e79d4b
+ * under the terms of the GNU General Public License as published by
e79d4b
+ * the Free Software Foundation, either version 3 of the License, or
e79d4b
+ * (at your option) any later version.
e79d4b
+ *
e79d4b
+ * GnuTLS is distributed in the hope that it will be useful, but
e79d4b
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
e79d4b
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
e79d4b
+ * General Public License for more details.
e79d4b
+ *
e79d4b
+ * You should have received a copy of the GNU General Public License
e79d4b
+ * along with GnuTLS. If not, see <https://www.gnu.org/licenses/>.
e79d4b
+ */
e79d4b
+
e79d4b
+#ifdef HAVE_CONFIG_H
e79d4b
+#include <config.h>
e79d4b
+#endif
e79d4b
+
e79d4b
+#include <stdio.h>
e79d4b
+#include <gnutls/pkcs7.h>
e79d4b
+#include <gnutls/x509.h>
e79d4b
+
e79d4b
+#include "utils.h"
e79d4b
+
e79d4b
+static char rca_pem[] =
e79d4b
+	"-----BEGIN CERTIFICATE-----\n"
e79d4b
+	"MIIDCjCCAfKgAwIBAgIBATANBgkqhkiG9w0BAQsFADAVMRMwEQYDVQQKDApFeGFt\n"
e79d4b
+	"cGxlIENBMCAXDTE3MDcyMTE0NDMzNloYDzIyMjIwNzIxMTQ0MzM2WjAV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) - 1 };
e79d4b
+const gnutls_datum_t ca_datum = { (void *)ca_pem, sizeof(ca_pem) - 1 };
e79d4b
+const gnutls_datum_t ee_datum = { (void *)ee_pem, sizeof(ee_pem) - 1 };
e79d4b
+const gnutls_datum_t msg_datum = { (void *)msg_pem, sizeof(msg_pem) - 1 };
e79d4b
+
e79d4b
+static void tls_log_func(int level, const char *str)
e79d4b
+{
e79d4b
+	fprintf(stderr, "%s |<%d>| %s", "err", level, str);
e79d4b
+}
e79d4b
+
e79d4b
+#define CHECK(X)\
e79d4b
+{\
e79d4b
+	r = X;\
e79d4b
+	if (r < 0)\
e79d4b
+		fail("error in %d: %s\n", __LINE__, gnutls_strerror(r));\
e79d4b
+}\
e79d4b
+
e79d4b
+void doit(void)
e79d4b
+{
e79d4b
+	int r;
e79d4b
+	gnutls_x509_crt_t rca_cert = NULL;
e79d4b
+	gnutls_x509_crt_t ca_cert = NULL;
e79d4b
+	gnutls_x509_crt_t ee_cert = NULL;
e79d4b
+	gnutls_x509_trust_list_t tlist = NULL;
e79d4b
+	gnutls_pkcs7_t pkcs7 = NULL;
e79d4b
+	gnutls_datum_t data = { (unsigned char *)"xxx", 3 };
e79d4b
+
e79d4b
+	if (debug) {
e79d4b
+		gnutls_global_set_log_function(tls_log_func);
e79d4b
+		gnutls_global_set_log_level(4711);
e79d4b
+	}
e79d4b
+
e79d4b
+	// Import certificates
e79d4b
+	CHECK(gnutls_x509_crt_init(&rca_cert));
e79d4b
+	CHECK(gnutls_x509_crt_import(rca_cert, &rca_datum, GNUTLS_X509_FMT_PEM));
e79d4b
+	CHECK(gnutls_x509_crt_init(&ca_cert));
e79d4b
+	CHECK(gnutls_x509_crt_import(ca_cert, &ca_datum, GNUTLS_X509_FMT_PEM));
e79d4b
+	CHECK(gnutls_x509_crt_init(&ee_cert));
e79d4b
+	CHECK(gnutls_x509_crt_import(ee_cert, &ee_datum, GNUTLS_X509_FMT_PEM));
e79d4b
+
e79d4b
+	// Setup trust store
e79d4b
+	CHECK(gnutls_x509_trust_list_init(&tlist, 0));
e79d4b
+	CHECK(gnutls_x509_trust_list_add_named_crt(tlist, rca_cert, "rca", 3, 0));
e79d4b
+	CHECK(gnutls_x509_trust_list_add_named_crt(tlist, ca_cert, "ca", 2, 0));
e79d4b
+	CHECK(gnutls_x509_trust_list_add_named_crt(tlist, ee_cert, "ee", 2, 0));
e79d4b
+
e79d4b
+	// Setup pkcs7 structure
e79d4b
+	CHECK(gnutls_pkcs7_init(&pkcs7));
e79d4b
+	CHECK(gnutls_pkcs7_import(pkcs7, &msg_datum, GNUTLS_X509_FMT_PEM));
e79d4b
+
e79d4b
+	// Signature verification
e79d4b
+	gnutls_pkcs7_verify(pkcs7, tlist, NULL, 0, 0, &data, 0);
e79d4b
+
e79d4b
+	gnutls_x509_crt_deinit(rca_cert);
e79d4b
+	gnutls_x509_crt_deinit(ca_cert);
e79d4b
+	gnutls_x509_crt_deinit(ee_cert);
e79d4b
+	gnutls_x509_trust_list_deinit(tlist, 0);
e79d4b
+	gnutls_pkcs7_deinit(pkcs7);
e79d4b
+}
e79d4b
-- 
e79d4b
2.37.1
e79d4b