Skip to content

Commit 775acfd

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 #23361) (cherry picked from commit 041962b)
1 parent 8c30857 commit 775acfd

4 files changed

Lines changed: 31 additions & 4 deletions

File tree

‎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_ex(p7->d.data, ASN1_ITEM_rptr(PKCS12_SAFEBAGS),
8288
ossl_pkcs7_ctx_get0_libctx(&p7->ctx),
8389
ossl_pkcs7_ctx_get0_propq(&p7->ctx));
@@ -152,6 +158,12 @@ STACK_OF(PKCS12_SAFEBAG) *PKCS12_unpack_p7encdata(PKCS7 *p7, const char *pass,
152158
{
153159
if (!PKCS7_type_is_encrypted(p7))
154160
return NULL;
161+
162+
if (p7->d.encrypted == NULL) {
163+
ERR_raise(ERR_LIB_PKCS12, PKCS12_R_DECODE_ERROR);
164+
return NULL;
165+
}
166+
155167
return PKCS12_item_decrypt_d2i_ex(p7->d.encrypted->enc_data->algorithm,
156168
ASN1_ITEM_rptr(PKCS12_SAFEBAGS),
157169
pass, passlen,
@@ -191,6 +203,12 @@ STACK_OF(PKCS7) *PKCS12_unpack_authsafes(const PKCS12 *p12)
191203
ERR_raise(ERR_LIB_PKCS12, PKCS12_R_CONTENT_TYPE_NOT_DATA);
192204
return NULL;
193205
}
206+
207+
if (p12->authsafes->d.data == NULL) {
208+
ERR_raise(ERR_LIB_PKCS12, PKCS12_R_DECODE_ERROR);
209+
return NULL;
210+
}
211+
194212
p7ctx = &p12->authsafes->ctx;
195213
p7s = ASN1_item_unpack_ex(p12->authsafes->d.data,
196214
ASN1_ITEM_rptr(PKCS12_AUTHSAFES),

‎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
@@ -80,8 +80,9 @@ static int newpass_p12(PKCS12 *p12, const char *oldpass, const char *newpass)
8080
bags = PKCS12_unpack_p7data(p7);
8181
} else if (bagnid == NID_pkcs7_encrypted) {
8282
bags = PKCS12_unpack_p7encdata(p7, oldpass, -1);
83-
if (!alg_get(p7->d.encrypted->enc_data->algorithm,
84-
&pbe_nid, &pbe_iter, &pbe_saltlen, &cipherid))
83+
if (p7->d.encrypted == NULL
84+
|| !alg_get(p7->d.encrypted->enc_data->algorithm,
85+
&pbe_nid, &pbe_iter, &pbe_saltlen, &cipherid))
8586
goto err;
8687
} else {
8788
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)