Skip to content

Commit 580cbe2

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

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

tests/api.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34877,6 +34877,65 @@ static int test_pkcs7_decode_encrypted_outputsz(void)
3487734877
return EXPECT_RESULT();
3487834878
}
3487934879

34880+
/* Dummy ORI callback for PKCS#7 ORI overflow test */
34881+
#if defined(HAVE_PKCS7) && !defined(WOLFSSL_NO_MALLOC)
34882+
static int test_dummy_ori_cb(wc_PKCS7* pkcs7, byte* oriType, word32 oriTypeSz,
34883+
byte* oriValue, word32 oriValueSz,
34884+
byte* decryptedKey, word32* decryptedKeySz,
34885+
void* ctx)
34886+
{
34887+
(void)pkcs7; (void)oriType; (void)oriTypeSz;
34888+
(void)oriValue; (void)oriValueSz;
34889+
(void)decryptedKey; (void)decryptedKeySz; (void)ctx;
34890+
return -1;
34891+
}
34892+
#endif
34893+
34894+
/* Test: PKCS#7 ORI must reject OID larger than MAX_OID_SZ (32) */
34895+
static int test_pkcs7_ori_oversized_oid(void)
34896+
{
34897+
EXPECT_DECLS;
34898+
#if defined(HAVE_PKCS7) && !defined(WOLFSSL_NO_MALLOC)
34899+
wc_PKCS7* p7 = NULL;
34900+
byte out[256];
34901+
34902+
/* EnvelopedData with [4] IMPLICIT ORI containing an 80-byte OID */
34903+
static const byte poc[] = {
34904+
0x30, 0x6b,
34905+
0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x03,
34906+
0xa0, 0x5e,
34907+
0x30, 0x5c,
34908+
0x02, 0x01, 0x00,
34909+
0x31, 0x57,
34910+
0xa4, 0x55,
34911+
0x06, 0x50,
34912+
0x2a,
34913+
0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
34914+
0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
34915+
0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
34916+
0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
34917+
0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
34918+
0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
34919+
0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
34920+
0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
34921+
0x04, 0x01, 0x00
34922+
};
34923+
34924+
p7 = wc_PKCS7_New(NULL, INVALID_DEVID);
34925+
ExpectNotNull(p7);
34926+
if (p7 != NULL) {
34927+
wc_PKCS7_SetOriDecryptCb(p7, test_dummy_ori_cb);
34928+
34929+
/* Must return error (ASN_PARSE_E), not overflow the stack */
34930+
ExpectIntLT(wc_PKCS7_DecodeEnvelopedData(p7, (byte*)poc, sizeof(poc),
34931+
out, sizeof(out)), 0);
34932+
34933+
wc_PKCS7_Free(p7);
34934+
}
34935+
#endif
34936+
return EXPECT_RESULT();
34937+
}
34938+
3488034939
TEST_CASE testCases[] = {
3488134940
TEST_DECL(test_fileAccess),
3488234941

@@ -35694,6 +35753,7 @@ TEST_CASE testCases[] = {
3569435753
TEST_DECL(test_DhAgree_rejects_p_minus_1),
3569535754
TEST_DECL(test_ed448_rejects_identity_key),
3569635755
TEST_DECL(test_pkcs7_decode_encrypted_outputsz),
35756+
TEST_DECL(test_pkcs7_ori_oversized_oid),
3569735757

3569835758
#if defined(WOLFSSL_SNIFFER) && defined(WOLFSSL_SNIFFER_CHAIN_INPUT)
3569935759
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)