From 09df4395b5071217b76dc7d3d2e630eb8c5a79c2 Mon Sep 17 00:00:00 2001
From: Matt Caswell <matt@openssl.org>
Date: Fri, 19 Jan 2024 11:28:58 +0000
Subject: [PATCH] Add NULL checks where ContentInfo data can be NULL

PKCS12 structures contain PKCS7 ContentInfo fields. These fields are
optional and can be NULL even if the "type" is a valid value. OpenSSL
was not properly accounting for this and a NULL dereference can occur
causing a crash.

CVE-2024-0727

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Hugo Landau <hlandau@openssl.org>
Reviewed-by: Neil Horman <nhorman@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/23362)

(cherry picked from commit d135eeab8a5dbf72b3da5240bab9ddb7678dbd2c)
---
 crypto/pkcs12/p12_add.c  | 18 ++++++++++++++++++
 crypto/pkcs12/p12_mutl.c |  5 +++++
 crypto/pkcs12/p12_npas.c |  5 +++--
 crypto/pkcs7/pk7_mime.c  |  7 +++++--
 4 files changed, 31 insertions(+), 4 deletions(-)

Index: openssl-0.9.8j/crypto/pkcs12/p12_add.c
===================================================================
--- openssl-0.9.8j.orig/crypto/pkcs12/p12_add.c
+++ openssl-0.9.8j/crypto/pkcs12/p12_add.c
@@ -153,6 +153,11 @@ STACK_OF(PKCS12_SAFEBAG) *PKCS12_unpack_
 		PKCS12err(PKCS12_F_PKCS12_UNPACK_P7DATA,PKCS12_R_CONTENT_TYPE_NOT_DATA);
 		return NULL;
 		}
+	if (p7->d.data == NULL)
+		{
+		PKCS12err(PKCS12_F_PKCS12_UNPACK_P7DATA, PKCS12_R_DECODE_ERROR);
+		return NULL;
+		}
 	return ASN1_item_unpack(p7->d.data, ASN1_ITEM_rptr(PKCS12_SAFEBAGS));
 }
 
@@ -193,6 +198,11 @@ PKCS7 *PKCS12_pack_p7encdata(int pbe_nid
 STACK_OF(PKCS12_SAFEBAG) *PKCS12_unpack_p7encdata(PKCS7 *p7, const char *pass, int passlen)
 {
 	if(!PKCS7_type_is_encrypted(p7)) return NULL;
+	if (p7->d.encrypted == NULL)
+		{
+		PKCS12err(PKCS12_F_PKCS12_UNPACK_P7ENCDATA, PKCS12_R_DECODE_ERROR);
+		return NULL;
+		}
 	return PKCS12_item_decrypt_d2i(p7->d.encrypted->enc_data->algorithm,
 			           ASN1_ITEM_rptr(PKCS12_SAFEBAGS),
 				   pass, passlen,
@@ -220,5 +230,11 @@ STACK_OF(PKCS7) *PKCS12_unpack_authsafes
 		PKCS12err(PKCS12_F_PKCS12_UNPACK_AUTHSAFES,PKCS12_R_CONTENT_TYPE_NOT_DATA);
 		return NULL;
 		}
+	if (p12->authsafes->d.data == NULL)
+		{
+		PKCS12err(PKCS12_F_PKCS12_UNPACK_AUTHSAFES,
+			PKCS12_R_DECODE_ERROR);
+		return NULL;
+		}
 	return ASN1_item_unpack(p12->authsafes->d.data, ASN1_ITEM_rptr(PKCS12_AUTHSAFES));
 }
Index: openssl-0.9.8j/crypto/pkcs12/p12_mutl.c
===================================================================
--- openssl-0.9.8j.orig/crypto/pkcs12/p12_mutl.c
+++ openssl-0.9.8j/crypto/pkcs12/p12_mutl.c
@@ -78,6 +78,12 @@ int PKCS12_gen_mac(PKCS12 *p12, const ch
 		return 0;
 		}
 
+	if (p12->authsafes->d.data == NULL)
+		{
+		PKCS12err(PKCS12_F_PKCS12_GEN_MAC, PKCS12_R_DECODE_ERROR);
+		return 0;
+		}
+
 	salt = p12->mac->salt->data;
 	saltlen = p12->mac->salt->length;
 	if (!p12->mac->iter) iter = 1;
Index: openssl-0.9.8j/crypto/pkcs12/pk12err.c
===================================================================
--- openssl-0.9.8j.orig/crypto/pkcs12/pk12err.c
+++ openssl-0.9.8j/crypto/pkcs12/pk12err.c
@@ -96,6 +96,7 @@ static ERR_STRING_DATA PKCS12_str_functs
 {ERR_FUNC(PKCS12_F_PKCS12_SET_MAC),	"PKCS12_set_mac"},
 {ERR_FUNC(PKCS12_F_PKCS12_UNPACK_AUTHSAFES),	"PKCS12_unpack_authsafes"},
 {ERR_FUNC(PKCS12_F_PKCS12_UNPACK_P7DATA),	"PKCS12_unpack_p7data"},
+{ERR_FUNC(PKCS12_F_PKCS12_UNPACK_P7ENCDATA),	"PKCS12_unpack_p7encdata"},
 {ERR_FUNC(PKCS12_F_PKCS12_VERIFY_MAC),	"PKCS12_verify_mac"},
 {ERR_FUNC(PKCS12_F_PKCS8_ADD_KEYUSAGE),	"PKCS8_add_keyusage"},
 {ERR_FUNC(PKCS12_F_PKCS8_ENCRYPT),	"PKCS8_encrypt"},
Index: openssl-0.9.8j/crypto/pkcs12/pkcs12.h
===================================================================
--- openssl-0.9.8j.orig/crypto/pkcs12/pkcs12.h
+++ openssl-0.9.8j/crypto/pkcs12/pkcs12.h
@@ -299,6 +299,7 @@ void ERR_load_PKCS12_strings(void);
 #define PKCS12_F_PKCS12_SET_MAC				 123
 #define PKCS12_F_PKCS12_UNPACK_AUTHSAFES		 130
 #define PKCS12_F_PKCS12_UNPACK_P7DATA			 131
+#define PKCS12_F_PKCS12_UNPACK_P7ENCDATA		 233
 #define PKCS12_F_PKCS12_VERIFY_MAC			 126
 #define PKCS12_F_PKCS8_ADD_KEYUSAGE			 124
 #define PKCS12_F_PKCS8_ENCRYPT				 125
Index: openssl-0.9.8j/crypto/pkcs12/p12_npas.c
===================================================================
--- openssl-0.9.8j.orig/crypto/pkcs12/p12_npas.c
+++ openssl-0.9.8j/crypto/pkcs12/p12_npas.c
@@ -120,8 +120,14 @@ static int newpass_p12(PKCS12 *p12, char
 			bags = PKCS12_unpack_p7data(p7);
 		} else if (bagnid == NID_pkcs7_encrypted) {
 			bags = PKCS12_unpack_p7encdata(p7, oldpass, -1);
-			alg_get(p7->d.encrypted->enc_data->algorithm,
-				&pbe_nid, &pbe_iter, &pbe_saltlen);
+			if (p7->d.encrypted == NULL
+				|| !alg_get(p7->d.encrypted->enc_data->algorithm,
+					&pbe_nid, &pbe_iter, &pbe_saltlen))
+				{
+				sk_PKCS12_SAFEBAG_pop_free(bags,
+						PKCS12_SAFEBAG_free);
+				bags = NULL;
+				}
 		} else continue;
 		if (!bags) {
 			sk_PKCS7_pop_free(asafes, PKCS7_free);
