Skip to content

Commit 559d88e

Browse files
committed
Fix stack buffer overflow in wc_PKCS7_DecryptOri
Reported by: Nicholas Carlini <npc@anthropic.com>
1 parent 08f9fa0 commit 559d88e

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
@@ -34832,6 +34832,65 @@ static int test_pkcs7_decode_encrypted_outputsz(void)
3483234832
return EXPECT_RESULT();
3483334833
}
3483434834

34835+
/* Dummy ORI callback for PKCS#7 ORI overflow test */
34836+
#if defined(HAVE_PKCS7) && !defined(WOLFSSL_NO_MALLOC)
34837+
static int test_dummy_ori_cb(wc_PKCS7* pkcs7, byte* oriType, word32 oriTypeSz,
34838+
byte* oriValue, word32 oriValueSz,
34839+
byte* decryptedKey, word32* decryptedKeySz,
34840+
void* ctx)
34841+
{
34842+
(void)pkcs7; (void)oriType; (void)oriTypeSz;
34843+
(void)oriValue; (void)oriValueSz;
34844+
(void)decryptedKey; (void)decryptedKeySz; (void)ctx;
34845+
return -1;
34846+
}
34847+
#endif
34848+
34849+
/* Test: PKCS#7 ORI must reject OID larger than MAX_OID_SZ (32) */
34850+
static int test_pkcs7_ori_oversized_oid(void)
34851+
{
34852+
EXPECT_DECLS;
34853+
#if defined(HAVE_PKCS7) && !defined(WOLFSSL_NO_MALLOC)
34854+
wc_PKCS7* p7 = NULL;
34855+
byte out[256];
34856+
34857+
/* EnvelopedData with [4] IMPLICIT ORI containing an 80-byte OID */
34858+
static const byte poc[] = {
34859+
0x30, 0x6b,
34860+
0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x03,
34861+
0xa0, 0x5e,
34862+
0x30, 0x5c,
34863+
0x02, 0x01, 0x00,
34864+
0x31, 0x57,
34865+
0xa4, 0x55,
34866+
0x06, 0x50,
34867+
0x2a,
34868+
0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
34869+
0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
34870+
0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
34871+
0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
34872+
0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
34873+
0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
34874+
0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
34875+
0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
34876+
0x04, 0x01, 0x00
34877+
};
34878+
34879+
p7 = wc_PKCS7_New(NULL, INVALID_DEVID);
34880+
ExpectNotNull(p7);
34881+
if (p7 != NULL) {
34882+
wc_PKCS7_SetOriDecryptCb(p7, test_dummy_ori_cb);
34883+
34884+
/* Must return error (ASN_PARSE_E), not overflow the stack */
34885+
ExpectIntLT(wc_PKCS7_DecodeEnvelopedData(p7, (byte*)poc, sizeof(poc),
34886+
out, sizeof(out)), 0);
34887+
34888+
wc_PKCS7_Free(p7);
34889+
}
34890+
#endif
34891+
return EXPECT_RESULT();
34892+
}
34893+
3483534894
TEST_CASE testCases[] = {
3483634895
TEST_DECL(test_fileAccess),
3483734896

@@ -35648,6 +35707,7 @@ TEST_CASE testCases[] = {
3564835707
TEST_DECL(test_DhAgree_rejects_p_minus_1),
3564935708
TEST_DECL(test_ed448_rejects_identity_key),
3565035709
TEST_DECL(test_pkcs7_decode_encrypted_outputsz),
35710+
TEST_DECL(test_pkcs7_ori_oversized_oid),
3565135711

3565235712
#if defined(WOLFSSL_SNIFFER) && defined(WOLFSSL_SNIFFER_CHAIN_INPUT)
3565335713
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
@@ -11507,6 +11507,11 @@ static int wc_PKCS7_DecryptOri(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1150711507
if (GetASNObjectId(pkiMsg, idx, &oriOIDSz, pkiMsgSz) != 0)
1150811508
return ASN_PARSE_E;
1150911509

11510+
if (oriOIDSz <= 0 || (word32)oriOIDSz > MAX_OID_SZ) {
11511+
WOLFSSL_MSG("ORI oriType OID too large");
11512+
return ASN_PARSE_E;
11513+
}
11514+
1151011515
XMEMCPY(oriOID, pkiMsg + *idx, (word32)oriOIDSz);
1151111516
*idx += (word32)oriOIDSz;
1151211517

0 commit comments

Comments
 (0)