Skip to content

Commit d135eea

Browse files
committed
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 #23362)
1 parent 9601413 commit d135eea

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

crypto/pkcs12/p12_add.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ STACK_OF(PKCS12_SAFEBAG) *PKCS12_unpack_p7data(PKCS7 *p7)
7878
ERR_raise(ERR_LIB_PKCS12, PKCS12_R_CONTENT_TYPE_NOT_DATA);
7979
return NULL;
8080
}
81+
82+
if (p7->d.data == NULL) {
83+
ERR_raise(ERR_LIB_PKCS12, PKCS12_R_DECODE_ERROR);
84+
return NULL;
85+
}
86+
8187
return ASN1_item_unpack(p7->d.data, ASN1_ITEM_rptr(PKCS12_SAFEBAGS));
8288
}
8389

@@ -150,6 +156,12 @@ STACK_OF(PKCS12_SAFEBAG) *PKCS12_unpack_p7encdata(PKCS7 *p7, const char *pass,
150156
{
151157
if (!PKCS7_type_is_encrypted(p7))
152158
return NULL;
159+
160+
if (p7->d.encrypted == NULL) {
161+
ERR_raise(ERR_LIB_PKCS12, PKCS12_R_DECODE_ERROR);
162+
return NULL;
163+
}
164+
153165
return PKCS12_item_decrypt_d2i_ex(p7->d.encrypted->enc_data->algorithm,
154166
ASN1_ITEM_rptr(PKCS12_SAFEBAGS),
155167
pass, passlen,
@@ -188,6 +200,12 @@ STACK_OF(PKCS7) *PKCS12_unpack_authsafes(const PKCS12 *p12)
188200
ERR_raise(ERR_LIB_PKCS12, PKCS12_R_CONTENT_TYPE_NOT_DATA);
189201
return NULL;
190202
}
203+
204+
if (p12->authsafes->d.data == NULL) {
205+
ERR_raise(ERR_LIB_PKCS12, PKCS12_R_DECODE_ERROR);
206+
return NULL;
207+
}
208+
191209
p7s = ASN1_item_unpack(p12->authsafes->d.data,
192210
ASN1_ITEM_rptr(PKCS12_AUTHSAFES));
193211
if (p7s != NULL) {

crypto/pkcs12/p12_mutl.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ static int pkcs12_gen_mac(PKCS12 *p12, const char *pass, int passlen,
9898
return 0;
9999
}
100100

101+
if (p12->authsafes->d.data == NULL) {
102+
ERR_raise(ERR_LIB_PKCS12, PKCS12_R_DECODE_ERROR);
103+
return 0;
104+
}
105+
101106
salt = p12->mac->salt->data;
102107
saltlen = p12->mac->salt->length;
103108
if (p12->mac->iter == NULL)

crypto/pkcs12/p12_npas.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ static int newpass_p12(PKCS12 *p12, const char *oldpass, const char *newpass)
7777
bags = PKCS12_unpack_p7data(p7);
7878
} else if (bagnid == NID_pkcs7_encrypted) {
7979
bags = PKCS12_unpack_p7encdata(p7, oldpass, -1);
80-
if (!alg_get(p7->d.encrypted->enc_data->algorithm,
81-
&pbe_nid, &pbe_iter, &pbe_saltlen))
80+
if (p7->d.encrypted == NULL
81+
|| !alg_get(p7->d.encrypted->enc_data->algorithm,
82+
&pbe_nid, &pbe_iter, &pbe_saltlen))
8283
goto err;
8384
} else {
8485
continue;

crypto/pkcs7/pk7_mime.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,13 @@ int SMIME_write_PKCS7(BIO *bio, PKCS7 *p7, BIO *data, int flags)
3333
int ctype_nid = OBJ_obj2nid(p7->type);
3434
const PKCS7_CTX *ctx = ossl_pkcs7_get0_ctx(p7);
3535

36-
if (ctype_nid == NID_pkcs7_signed)
36+
if (ctype_nid == NID_pkcs7_signed) {
37+
if (p7->d.sign == NULL)
38+
return 0;
3739
mdalgs = p7->d.sign->md_algs;
38-
else
40+
} else {
3941
mdalgs = NULL;
42+
}
4043

4144
flags ^= SMIME_OLDMIME;
4245

0 commit comments

Comments
 (0)