Skip to content

Commit e1206bd

Browse files
committed
Fix stack buffer overflow in wc_PKCS7_DecryptOri
Reported by: Nicholas Carlini <npc@anthropic.com>
1 parent 2a327bd commit e1206bd

2 files changed

Lines changed: 65 additions & 0 deletions

File tree

tests/api.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34794,6 +34794,65 @@ static int test_pkcs7_decode_encrypted_outputsz(void)
3479434794
return EXPECT_RESULT();
3479534795
}
3479634796

34797+
/* Dummy ORI callback for PKCS#7 ORI overflow test */
34798+
#if defined(HAVE_PKCS7) && !defined(WOLFSSL_NO_MALLOC)
34799+
static int test_dummy_ori_cb(wc_PKCS7* pkcs7, byte* oriType, word32 oriTypeSz,
34800+
byte* oriValue, word32 oriValueSz,
34801+
byte* decryptedKey, word32* decryptedKeySz,
34802+
void* ctx)
34803+
{
34804+
(void)pkcs7; (void)oriType; (void)oriTypeSz;
34805+
(void)oriValue; (void)oriValueSz;
34806+
(void)decryptedKey; (void)decryptedKeySz; (void)ctx;
34807+
return -1;
34808+
}
34809+
#endif
34810+
34811+
/* Test: PKCS#7 ORI must reject OID larger than MAX_OID_SZ (32) */
34812+
static int test_pkcs7_ori_oversized_oid(void)
34813+
{
34814+
EXPECT_DECLS;
34815+
#if defined(HAVE_PKCS7) && !defined(WOLFSSL_NO_MALLOC)
34816+
wc_PKCS7* p7 = NULL;
34817+
byte out[256];
34818+
34819+
/* EnvelopedData with [4] IMPLICIT ORI containing an 80-byte OID */
34820+
static const byte poc[] = {
34821+
0x30, 0x6b,
34822+
0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x03,
34823+
0xa0, 0x5e,
34824+
0x30, 0x5c,
34825+
0x02, 0x01, 0x00,
34826+
0x31, 0x57,
34827+
0xa4, 0x55,
34828+
0x06, 0x50,
34829+
0x2a,
34830+
0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
34831+
0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
34832+
0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
34833+
0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
34834+
0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
34835+
0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
34836+
0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
34837+
0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
34838+
0x04, 0x01, 0x00
34839+
};
34840+
34841+
p7 = wc_PKCS7_New(NULL, INVALID_DEVID);
34842+
ExpectNotNull(p7);
34843+
if (p7 != NULL) {
34844+
wc_PKCS7_SetOriDecryptCb(p7, test_dummy_ori_cb);
34845+
34846+
/* Must return error (ASN_PARSE_E), not overflow the stack */
34847+
ExpectIntLT(wc_PKCS7_DecodeEnvelopedData(p7, (byte*)poc, sizeof(poc),
34848+
out, sizeof(out)), 0);
34849+
34850+
wc_PKCS7_Free(p7);
34851+
}
34852+
#endif
34853+
return EXPECT_RESULT();
34854+
}
34855+
3479734856
TEST_CASE testCases[] = {
3479834857
TEST_DECL(test_fileAccess),
3479934858

@@ -35609,6 +35668,7 @@ TEST_CASE testCases[] = {
3560935668
TEST_DECL(test_DhAgree_rejects_p_minus_1),
3561035669
TEST_DECL(test_ed448_rejects_identity_key),
3561135670
TEST_DECL(test_pkcs7_decode_encrypted_outputsz),
35671+
TEST_DECL(test_pkcs7_ori_oversized_oid),
3561235672

3561335673
#if defined(WOLFSSL_SNIFFER) && defined(WOLFSSL_SNIFFER_CHAIN_INPUT)
3561435674
TEST_DECL(test_sniffer_chain_input_overflow),

wolfcrypt/src/pkcs7.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11494,6 +11494,11 @@ static int wc_PKCS7_DecryptOri(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1149411494
if (GetASNObjectId(pkiMsg, idx, &oriOIDSz, pkiMsgSz) != 0)
1149511495
return ASN_PARSE_E;
1149611496

11497+
if (oriOIDSz <= 0 || (word32)oriOIDSz > MAX_OID_SZ) {
11498+
WOLFSSL_MSG("ORI oriType OID too large");
11499+
return ASN_PARSE_E;
11500+
}
11501+
1149711502
XMEMCPY(oriOID, pkiMsg + *idx, (word32)oriOIDSz);
1149811503
*idx += (word32)oriOIDSz;
1149911504

0 commit comments

Comments
 (0)