Skip to content

Commit 409b5fc

Browse files
authored
Merge pull request #10172 from embhorn/zd21568
Fix pkcs12 parse issue
2 parents 14ebd3d + 863db50 commit 409b5fc

3 files changed

Lines changed: 219 additions & 16 deletions

File tree

tests/api/test_pkcs12.c

Lines changed: 193 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <wolfssl/wolfcrypt/pkcs12.h>
3232
#include <wolfssl/wolfcrypt/pwdbased.h>
3333
#include <wolfssl/wolfcrypt/types.h>
34+
#include <wolfssl/wolfcrypt/aes.h>
3435
#include <tests/api/api.h>
3536
#include <tests/api/test_pkcs12.h>
3637

@@ -272,6 +273,198 @@ int test_wc_d2i_PKCS12_oid_underflow(void)
272273
return EXPECT_RESULT();
273274
}
274275

276+
/* Test that validates the fix for heap OOB read vulnerability where
277+
* ASN.1 parsing after DecryptContent() would use stale ContentInfo bounds.
278+
* This is a basic test that verifies the fix compiles and basic PKCS#12
279+
* functionality still works after adding contentSz bounds checking. */
280+
int test_wc_PKCS12_encrypted_content_bounds(void)
281+
{
282+
EXPECT_DECLS;
283+
#if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12) && \
284+
!defined(NO_RSA) && !defined(NO_AES) && !defined(NO_SHA) && \
285+
!defined(NO_SHA256) && defined(USE_CERT_BUFFERS_2048)
286+
287+
/* This test validates that the fix for heap OOB read is in place.
288+
* The fix ensures ASN.1 parsing uses contentSz (actual decrypted size)
289+
* instead of ci->dataSz (original ContentInfo size) as bounds.
290+
*
291+
* We test this by exercising the PKCS#12 parsing path with encrypted
292+
* content to ensure the fix doesn't break normal operation. */
293+
294+
byte* inKey = (byte*) server_key_der_2048;
295+
const word32 inKeySz = sizeof_server_key_der_2048;
296+
byte* inCert = (byte*) server_cert_der_2048;
297+
const word32 inCertSz = sizeof_server_cert_der_2048;
298+
WC_DerCertList inCa = {
299+
(byte*)ca_cert_der_2048, sizeof_ca_cert_der_2048, NULL
300+
};
301+
char pkcs12Passwd[] = "test_bounds_fix";
302+
303+
WC_PKCS12* pkcs12Export = NULL;
304+
WC_PKCS12* pkcs12Import = NULL;
305+
byte* pkcs12Der = NULL;
306+
byte* outKey = NULL;
307+
byte* outCert = NULL;
308+
WC_DerCertList* outCaList = NULL;
309+
int exportRet = 0;
310+
word32 pkcs12DerSz = 0;
311+
word32 outKeySz = 0;
312+
word32 outCertSz = 0;
313+
314+
/* Create a PKCS#12 with encrypted content */
315+
ExpectNotNull(pkcs12Export = wc_PKCS12_create(pkcs12Passwd,
316+
sizeof(pkcs12Passwd) - 1, NULL, inKey, inKeySz, inCert, inCertSz,
317+
&inCa, -1, -1, 2048, 2048, 0, NULL));
318+
319+
/* Serialize to DER - use int intermediate to avoid word32 truncation
320+
* of negative error codes from wc_i2d_PKCS12(). */
321+
ExpectIntGE((exportRet = wc_i2d_PKCS12(pkcs12Export, &pkcs12Der, NULL)), 0);
322+
pkcs12DerSz = (word32)exportRet;
323+
324+
/* Parse it back - this exercises the fixed bounds checking code path */
325+
ExpectNotNull(pkcs12Import = wc_PKCS12_new_ex(NULL));
326+
ExpectIntGE(wc_d2i_PKCS12(pkcs12Der, pkcs12DerSz, pkcs12Import), 0);
327+
328+
/* This parse operation now uses contentSz instead of ci->dataSz for bounds,
329+
* preventing the heap OOB read that existed before the fix */
330+
ExpectIntEQ(wc_PKCS12_parse(pkcs12Import, pkcs12Passwd, &outKey, &outKeySz,
331+
&outCert, &outCertSz, &outCaList), 0);
332+
333+
/* Verify the parsing worked correctly */
334+
ExpectIntEQ(outKeySz, inKeySz);
335+
ExpectIntEQ(outCertSz, inCertSz);
336+
ExpectNotNull(outCaList);
337+
ExpectIntEQ(outCaList->bufferSz, inCa.bufferSz);
338+
ExpectIntEQ(XMEMCMP(outKey, inKey, inKeySz), 0);
339+
ExpectIntEQ(XMEMCMP(outCert, inCert, inCertSz), 0);
340+
ExpectIntEQ(XMEMCMP(outCaList->buffer, inCa.buffer, inCa.bufferSz), 0);
341+
342+
/* Clean up */
343+
XFREE(outKey, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
344+
XFREE(outCert, NULL, DYNAMIC_TYPE_PKCS);
345+
wc_FreeCertList(outCaList, NULL);
346+
wc_PKCS12_free(pkcs12Import);
347+
XFREE(pkcs12Der, NULL, DYNAMIC_TYPE_PKCS);
348+
wc_PKCS12_free(pkcs12Export);
349+
350+
#endif
351+
352+
/* Part 2: True regression test - craft a malformed PKCS#12 whose decrypted
353+
* SafeBags SEQUENCE claims a length that exceeds the decrypted content
354+
* bounds (contentSz) but fits within the stale ContentInfo bounds
355+
* (ci->dataSz). Before the fix, the parser used ci->dataSz, allowing a
356+
* heap OOB read; with the fix it uses contentSz and rejects the blob. */
357+
#if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12) && \
358+
defined(WOLFSSL_AES_256) && defined(HAVE_AES_CBC) && \
359+
defined(HAVE_AES_DECRYPT) && !defined(NO_SHA256) && !defined(NO_HMAC) && \
360+
defined(WOLFSSL_ASN_TEMPLATE) && !defined(HAVE_FIPS)
361+
{
362+
static const char regPassword[] = "test";
363+
static const byte regSalt[8] = {1, 2, 3, 4, 5, 6, 7, 8};
364+
static const byte regIv[16] = {0};
365+
366+
/* Malformed SafeBags plaintext (one AES block = 16 bytes).
367+
* The outer SEQUENCE claims length 100 - this exceeds the decrypted
368+
* content size (16) but fits inside the stale ci->dataSz (127) that
369+
* the unfixed code used as the parsing bound. */
370+
static const byte regPlaintext[16] = {
371+
0x30, 0x64, /* SEQUENCE, length 100 */
372+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
373+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00
374+
};
375+
376+
/* Complete PKCS#12 DER (170 bytes).
377+
* Structure: PFX { version 3, authSafe { DATA { AuthenticatedSafe {
378+
* EncryptedData { PBES2(AES-256-CBC, HMAC-SHA256, PBKDF2)
379+
* <ciphertext placeholder at offset 154> } } } } }
380+
* No MacData - macIter=0 skips MAC verification. */
381+
byte regDer[170] = {
382+
0x30, 0x81, 0xA7, /* PFX SEQ (167) */
383+
0x02, 0x01, 0x03, /* version 3 */
384+
0x30, 0x81, 0xA1, /* authSafe ContentInfo (161) */
385+
0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
386+
0xF7, 0x0D, 0x01, 0x07, 0x01, /* OID data */
387+
0xA0, 0x81, 0x93, /* [0] CONS. (147) */
388+
0x04, 0x81, 0x90, /* OCTET STRING (144) */
389+
0x30, 0x81, 0x8D, /* AuthenticatedSafe SEQ (141) */
390+
0x30, 0x81, 0x8A, /* ContentInfo SEQ (138) */
391+
0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
392+
0xF7, 0x0D, 0x01, 0x07, 0x06, /* OID encryptedData */
393+
0xA0, 0x7D, /* [0] CONS. (125) */
394+
0x30, 0x7B, /* EncryptedData SEQ (123) */
395+
0x02, 0x01, 0x00, /* version 0 */
396+
0x30, 0x76, /* EncryptedContentInfo SEQ (118) */
397+
0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
398+
0xF7, 0x0D, 0x01, 0x07, 0x01, /* OID data */
399+
/* --- EncryptContent payload (107 bytes) --- */
400+
0x30, 0x57, /* AlgorithmIdentifier SEQ (87) */
401+
0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
402+
0xF7, 0x0D, 0x01, 0x05, 0x0D, /* OID pbes2 */
403+
0x30, 0x4A, /* PBES2-params SEQ (74) */
404+
0x30, 0x29, /* keyDerivFunc SEQ (41) */
405+
0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
406+
0xF7, 0x0D, 0x01, 0x05, 0x0C, /* OID pbkdf2 */
407+
0x30, 0x1C, /* PBKDF2-params SEQ (28) */
408+
0x04, 0x08,
409+
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, /* salt */
410+
0x02, 0x02, 0x08, 0x00, /* iterations 2048 */
411+
0x30, 0x0C, /* PRF SEQ (12) */
412+
0x06, 0x08, 0x2A, 0x86, 0x48, 0x86,
413+
0xF7, 0x0D, 0x02, 0x09, /* OID hmac-sha256 */
414+
0x05, 0x00, /* NULL */
415+
0x30, 0x1D, /* encryptionScheme SEQ (29) */
416+
0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
417+
0x65, 0x03, 0x04, 0x01, 0x2A, /* OID aes256-cbc */
418+
0x04, 0x10, /* IV OCT (16) */
419+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
420+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
421+
0x80, 0x10, /* [0] IMPLICIT CT (16) */
422+
/* 16 bytes ciphertext - filled at runtime */
423+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
424+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
425+
};
426+
427+
byte regKey[32];
428+
byte regCiphertext[16];
429+
Aes regAes;
430+
WC_PKCS12* regP12 = NULL;
431+
byte* regPkey = NULL;
432+
byte* regCert = NULL;
433+
word32 regPkeySz = 0;
434+
word32 regCertSz = 0;
435+
436+
/* Derive AES-256 key with the same PBKDF2 that DecryptContent uses */
437+
ExpectIntEQ(wc_PBKDF2(regKey, (const byte*)regPassword,
438+
(int)XSTRLEN(regPassword), regSalt, (int)sizeof(regSalt),
439+
2048, 32, WC_SHA256), 0);
440+
441+
/* Encrypt the malformed plaintext */
442+
ExpectIntEQ(wc_AesInit(&regAes, NULL, INVALID_DEVID), 0);
443+
ExpectIntEQ(wc_AesSetKey(&regAes, regKey, 32, regIv,
444+
AES_ENCRYPTION), 0);
445+
ExpectIntEQ(wc_AesCbcEncrypt(&regAes, regCiphertext, regPlaintext,
446+
sizeof(regPlaintext)), 0);
447+
wc_AesFree(&regAes);
448+
449+
/* Patch ciphertext into the DER template at offset 154 */
450+
XMEMCPY(regDer + 154, regCiphertext, sizeof(regCiphertext));
451+
452+
/* Parse the crafted PKCS#12 - d2i should succeed (outer structure
453+
* is valid), but wc_PKCS12_parse must fail because GetSequence
454+
* rejects SEQUENCE length 100 against contentSz 16. */
455+
ExpectNotNull(regP12 = wc_PKCS12_new_ex(NULL));
456+
ExpectIntGE(wc_d2i_PKCS12(regDer, (word32)sizeof(regDer), regP12), 0);
457+
ExpectIntLT(wc_PKCS12_parse(regP12, regPassword, &regPkey, &regPkeySz,
458+
&regCert, &regCertSz, NULL), 0);
459+
460+
XFREE(regPkey, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
461+
XFREE(regCert, NULL, DYNAMIC_TYPE_PKCS);
462+
wc_PKCS12_free(regP12);
463+
}
464+
#endif
465+
return EXPECT_RESULT();
466+
}
467+
275468
int test_wc_PKCS12_PBKDF(void)
276469
{
277470
EXPECT_DECLS;
@@ -675,4 +868,3 @@ int test_wc_PKCS12_PBKDF_ex_sha512_256(void)
675868
#endif
676869
return EXPECT_RESULT();
677870
}
678-

tests/api/test_pkcs12.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ int test_wc_i2d_PKCS12(void);
2828
int test_wc_PKCS12_create(void);
2929
int test_wc_d2i_PKCS12_bad_mac_salt(void);
3030
int test_wc_d2i_PKCS12_oid_underflow(void);
31+
int test_wc_PKCS12_encrypted_content_bounds(void);
3132
int test_wc_PKCS12_PBKDF(void);
3233
int test_wc_PKCS12_PBKDF_ex(void);
3334
int test_wc_PKCS12_PBKDF_ex_sha1(void);
@@ -42,6 +43,7 @@ int test_wc_PKCS12_PBKDF_ex_sha512_256(void);
4243
TEST_DECL_GROUP("pkcs12", test_wc_PKCS12_create), \
4344
TEST_DECL_GROUP("pkcs12", test_wc_d2i_PKCS12_bad_mac_salt), \
4445
TEST_DECL_GROUP("pkcs12", test_wc_d2i_PKCS12_oid_underflow), \
46+
TEST_DECL_GROUP("pkcs12", test_wc_PKCS12_encrypted_content_bounds), \
4547
TEST_DECL_GROUP("pkcs12", test_wc_PKCS12_PBKDF), \
4648
TEST_DECL_GROUP("pkcs12", test_wc_PKCS12_PBKDF_ex), \
4749
TEST_DECL_GROUP("pkcs12", test_wc_PKCS12_PBKDF_ex_sha1), \

wolfcrypt/src/pkcs12.c

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,7 @@ int wc_PKCS12_parse_ex(WC_PKCS12* pkcs12, const char* psw,
13351335
byte* buf = NULL;
13361336
word32 i, oid;
13371337
word32 algId;
1338+
word32 contentSz = 0;
13381339
int ret, pswSz;
13391340
#ifdef ASN_BER_TO_DER
13401341
int curIdx;
@@ -1450,6 +1451,11 @@ int wc_PKCS12_parse_ex(WC_PKCS12* pkcs12, const char* psw,
14501451
goto exit_pk12par;
14511452
}
14521453

1454+
/* DecryptContent strips the PBE ASN.1 wrapper and returns the
1455+
* actual decrypted payload size, which is smaller than the
1456+
* allocated buf. Track the real bounds so subsequent ASN.1
1457+
* parsing does not read past the decrypted content. */
1458+
contentSz = (word32)ret;
14531459
data = buf;
14541460
idx = 0;
14551461

@@ -1486,36 +1492,39 @@ int wc_PKCS12_parse_ex(WC_PKCS12* pkcs12, const char* psw,
14861492
goto exit_pk12par;
14871493
}
14881494

1495+
/* DATA branch: data still points into ci->data, so the
1496+
* ContentInfo size is the correct parsing bound. */
1497+
contentSz = ci->dataSz;
14891498
}
14901499

14911500
/* parse through bags in ContentInfo */
1492-
if ((ret = GetSequence(data, &idx, &totalSz, ci->dataSz)) < 0) {
1501+
if ((ret = GetSequence(data, &idx, &totalSz, contentSz)) < 0) {
14931502
goto exit_pk12par;
14941503
}
14951504
totalSz += (int)idx;
14961505

14971506
while ((int)idx < totalSz) {
14981507
int bagSz;
1499-
if ((ret = GetSequence(data, &idx, &bagSz, ci->dataSz)) < 0) {
1508+
if ((ret = GetSequence(data, &idx, &bagSz, contentSz)) < 0) {
15001509
goto exit_pk12par;
15011510
}
15021511
bagSz += (int)idx;
15031512

15041513
if ((ret = GetObjectId(data, &idx, &oid, oidIgnoreType,
1505-
ci->dataSz)) < 0) {
1514+
contentSz)) < 0) {
15061515
goto exit_pk12par;
15071516
}
15081517

15091518
switch (oid) {
15101519
case WC_PKCS12_KeyBag: /* 667 */
15111520
WOLFSSL_MSG("PKCS12 Key Bag found");
1512-
if (GetASNTag(data, &idx, &tag, ci->dataSz) < 0) {
1521+
if (GetASNTag(data, &idx, &tag, contentSz) < 0) {
15131522
ERROR_OUT(ASN_PARSE_E, exit_pk12par);
15141523
}
15151524
if (tag != (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC)) {
15161525
ERROR_OUT(ASN_PARSE_E, exit_pk12par);
15171526
}
1518-
if ((ret = GetLength(data, &idx, &size, ci->dataSz)) <= 0) {
1527+
if ((ret = GetLength(data, &idx, &size, contentSz)) <= 0) {
15191528
if (ret == 0)
15201529
ret = ASN_PARSE_E;
15211530
goto exit_pk12par;
@@ -1553,14 +1562,14 @@ int wc_PKCS12_parse_ex(WC_PKCS12* pkcs12, const char* psw,
15531562
byte* k;
15541563

15551564
WOLFSSL_MSG("PKCS12 Shrouded Key Bag found");
1556-
if (GetASNTag(data, &idx, &tag, ci->dataSz) < 0) {
1565+
if (GetASNTag(data, &idx, &tag, contentSz) < 0) {
15571566
ERROR_OUT(ASN_PARSE_E, exit_pk12par);
15581567
}
15591568
if (tag != (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC)) {
15601569
ERROR_OUT(ASN_PARSE_E, exit_pk12par);
15611570
}
15621571
if ((ret = GetLength(data, &idx, &size,
1563-
ci->dataSz)) < 0) {
1572+
contentSz)) < 0) {
15641573
goto exit_pk12par;
15651574
}
15661575

@@ -1626,51 +1635,51 @@ int wc_PKCS12_parse_ex(WC_PKCS12* pkcs12, const char* psw,
16261635
{
16271636
WC_DerCertList* node;
16281637
WOLFSSL_MSG("PKCS12 Cert Bag found");
1629-
if (GetASNTag(data, &idx, &tag, ci->dataSz) < 0) {
1638+
if (GetASNTag(data, &idx, &tag, contentSz) < 0) {
16301639
ERROR_OUT(ASN_PARSE_E, exit_pk12par);
16311640
}
16321641
if (tag != (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC)) {
16331642
ERROR_OUT(ASN_PARSE_E, exit_pk12par);
16341643
}
1635-
if ((ret = GetLength(data, &idx, &size, ci->dataSz)) < 0) {
1644+
if ((ret = GetLength(data, &idx, &size, contentSz)) < 0) {
16361645
goto exit_pk12par;
16371646
}
16381647

16391648
/* get cert bag type */
1640-
if ((ret = GetSequence(data, &idx, &size, ci->dataSz)) <0) {
1649+
if ((ret = GetSequence(data, &idx, &size, contentSz)) <0) {
16411650
goto exit_pk12par;
16421651
}
16431652

16441653
if ((ret = GetObjectId(data, &idx, &oid, oidIgnoreType,
1645-
ci->dataSz)) < 0) {
1654+
contentSz)) < 0) {
16461655
goto exit_pk12par;
16471656
}
16481657

16491658
switch (oid) {
16501659
case WC_PKCS12_CertBag_Type1: /* 675 */
16511660
/* type 1 */
16521661
WOLFSSL_MSG("PKCS12 cert bag type 1");
1653-
if (GetASNTag(data, &idx, &tag, ci->dataSz) < 0) {
1662+
if (GetASNTag(data, &idx, &tag, contentSz) < 0) {
16541663
ERROR_OUT(ASN_PARSE_E, exit_pk12par);
16551664
}
16561665
if (tag != (ASN_CONSTRUCTED |
16571666
ASN_CONTEXT_SPECIFIC)) {
16581667
ERROR_OUT(ASN_PARSE_E, exit_pk12par);
16591668
}
1660-
if ((ret = GetLength(data, &idx, &size, ci->dataSz))
1669+
if ((ret = GetLength(data, &idx, &size, contentSz))
16611670
<= 0) {
16621671
if (ret == 0)
16631672
ret = ASN_PARSE_E;
16641673
goto exit_pk12par;
16651674
}
1666-
if (GetASNTag(data, &idx, &tag, ci->dataSz) < 0) {
1675+
if (GetASNTag(data, &idx, &tag, contentSz) < 0) {
16671676
ERROR_OUT(ASN_PARSE_E, exit_pk12par);
16681677
}
16691678
if (tag != ASN_OCTET_STRING) {
16701679
ERROR_OUT(ASN_PARSE_E, exit_pk12par);
16711680

16721681
}
1673-
if ((ret = GetLength(data, &idx, &size, ci->dataSz))
1682+
if ((ret = GetLength(data, &idx, &size, contentSz))
16741683
< 0) {
16751684
goto exit_pk12par;
16761685
}

0 commit comments

Comments
 (0)