Skip to content

Commit 1de4020

Browse files
committed
Respect outputSz in PKCS7 decode methods
Reported by: Nicholas Carlini <npc@anthropic.com>
1 parent 2237297 commit 1de4020

2 files changed

Lines changed: 93 additions & 0 deletions

File tree

tests/api.c

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

34797+
/* Test: wc_PKCS7_DecodeEncryptedData must respect outputSz.
34798+
* 588-byte EncryptedData blob: 496 bytes of 'A' encrypted with
34799+
* AES-256-CBC under all-zero key/IV. Decrypted content is 496 bytes,
34800+
* but we pass a 128-byte output buffer. Must return BUFFER_E. */
34801+
static int test_pkcs7_decode_encrypted_outputsz(void)
34802+
{
34803+
EXPECT_DECLS;
34804+
#if defined(HAVE_PKCS7) && !defined(NO_AES) && defined(HAVE_AES_CBC) && \
34805+
!defined(WOLFSSL_NO_MALLOC) && defined(WOLFSSL_AES_256)
34806+
wc_PKCS7* p7 = NULL;
34807+
byte key[32];
34808+
byte out[128]; /* smaller than 496-byte decrypted content */
34809+
34810+
static const byte encBlob[] = {
34811+
0x30,0x82,0x02,0x48,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,
34812+
0x01,0x07,0x06,0xa0,0x82,0x02,0x39,0x30,0x82,0x02,0x35,0x02,
34813+
0x01,0x00,0x30,0x82,0x02,0x2e,0x06,0x09,0x2a,0x86,0x48,0x86,
34814+
0xf7,0x0d,0x01,0x07,0x01,0x30,0x1d,0x06,0x09,0x60,0x86,0x48,
34815+
0x01,0x65,0x03,0x04,0x01,0x2a,0x04,0x10,0x00,0x00,0x00,0x00,
34816+
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
34817+
0x80,0x82,0x02,0x00,0x7e,0x0e,0x75,0x77,0xef,0x9c,0x30,0xa6,
34818+
0xbf,0x0b,0x25,0xe0,0x62,0x1e,0x82,0x7e,0x76,0xf7,0x57,0xef,
34819+
0x12,0x71,0xc5,0x74,0x0e,0x60,0xc3,0x3c,0x8e,0xc3,0xce,0x70,
34820+
0xbd,0x3c,0xb4,0x60,0x76,0xb1,0xea,0xb5,0x4b,0xc1,0xfe,0x5b,
34821+
0x88,0xfa,0x43,0x46,0x03,0x28,0x9d,0xab,0x3d,0xb5,0x88,0x50,
34822+
0x07,0xfc,0x3c,0xc9,0x49,0x4d,0x68,0x97,0xb1,0x59,0xb6,0x35,
34823+
0xa1,0x68,0x80,0xb0,0x8f,0xb2,0x21,0x9b,0xfc,0xd8,0xf0,0x67,
34824+
0xbc,0x4b,0x9b,0x28,0x1c,0x3b,0xa0,0x40,0x76,0x8f,0x1f,0x9f,
34825+
0x87,0xe6,0xe4,0xa5,0xdc,0x9a,0x9a,0x84,0x86,0xff,0x43,0x8a,
34826+
0x57,0x8d,0x97,0xd2,0x8f,0x67,0x90,0xd8,0x4f,0x0f,0x0d,0xac,
34827+
0x56,0x6d,0x51,0x33,0xa3,0x37,0x9e,0xe9,0xbf,0x07,0xab,0x93,
34828+
0xbc,0xbe,0xc1,0x64,0x07,0x30,0xf0,0xff,0x89,0x0a,0x36,0x1c,
34829+
0xc8,0xe9,0xae,0x24,0x1f,0x95,0x5c,0xa1,0x8a,0x3e,0x15,0x86,
34830+
0x49,0x70,0x55,0xc2,0xa5,0x90,0xb9,0xda,0x2b,0x97,0x6f,0x5f,
34831+
0x60,0x61,0xc9,0xcf,0x4d,0x3a,0xb1,0xe4,0x9f,0x37,0x0f,0xf3,
34832+
0xba,0x85,0x04,0x52,0x68,0x00,0x47,0x3d,0x83,0xc1,0x3f,0xc6,
34833+
0x70,0x97,0xc1,0x0c,0xc0,0x0b,0xa2,0xcb,0x8c,0x88,0xb5,0x01,
34834+
0x29,0x7f,0x7e,0xd2,0x46,0x24,0x82,0x92,0x28,0xdd,0x49,0x53,
34835+
0x0f,0x76,0xad,0x4b,0x81,0x85,0x3a,0x9f,0xda,0x0d,0x69,0xe2,
34836+
0x57,0xb9,0xe9,0xfa,0x53,0xed,0x20,0x6f,0x62,0x43,0x1d,0x21,
34837+
0xa9,0x53,0x3d,0xd5,0xb9,0x1a,0x4b,0x3e,0xb7,0x05,0x87,0xb6,
34838+
0xe3,0xfe,0x03,0x09,0xe1,0x74,0x34,0x42,0x84,0xb1,0x06,0xf9,
34839+
0xfe,0x64,0xc1,0xd2,0xce,0x3d,0x29,0xf4,0x94,0xb8,0xfc,0xbe,
34840+
0xb1,0x90,0xd6,0x38,0x61,0x4e,0x43,0x36,0x4e,0x27,0x87,0xd3,
34841+
0x24,0xdc,0x34,0xf0,0x28,0x2d,0xc8,0xff,0x89,0x9f,0xeb,0xee,
34842+
0x0d,0x45,0xfb,0xc5,0x53,0xd3,0xdf,0xcb,0xf8,0xeb,0x7e,0xcb,
34843+
0x2a,0xd7,0xa2,0xd6,0x6a,0xf1,0xb8,0x24,0xa1,0x05,0x16,0x56,
34844+
0x2e,0x03,0xfe,0x21,0x19,0x36,0x8a,0xeb,0x50,0x8d,0x42,0x31,
34845+
0x1d,0xb3,0x0a,0x13,0x1d,0x16,0x09,0x0b,0x40,0x4e,0x90,0x78,
34846+
0xbf,0x41,0x5f,0x4d,0xd9,0xce,0x91,0xaa,0xd0,0x38,0x5c,0xfb,
34847+
0xc4,0x9f,0x55,0x4e,0x04,0x9d,0xd0,0xca,0x74,0x3c,0x6c,0x01,
34848+
0x1b,0x84,0x76,0xfa,0xba,0x5d,0x2d,0x35,0x7e,0xe3,0x37,0x8b,
34849+
0x72,0x0a,0xbb,0xa4,0x4d,0x56,0x2b,0x3b,0x9f,0x5a,0xa0,0xe3,
34850+
0x60,0x26,0x65,0x09,0xed,0xfc,0x10,0x0c,0xa1,0x4f,0x12,0x76,
34851+
0xe2,0x4c,0xbd,0x1b,0x67,0xb4,0xb4,0x54,0x5c,0x38,0x47,0x8b,
34852+
0x7e,0x35,0x8e,0x4d,0x4e,0xef,0x91,0x3f,0xff,0x16,0x0a,0x42,
34853+
0x0b,0xe5,0x28,0x26,0x24,0x9f,0x6f,0xb4,0x63,0x8a,0xa1,0x52,
34854+
0x22,0x3a,0xdb,0xd9,0x8e,0x76,0x6e,0x6a,0xa9,0xa1,0xec,0xf2,
34855+
0x9c,0x58,0xf8,0x6b,0xdd,0xf2,0xf8,0x2d,0xcb,0x8c,0x96,0xda,
34856+
0xb4,0x9c,0x44,0xdc,0xab,0x43,0x2c,0x22,0xf3,0xfe,0x1a,0xb9,
34857+
0x3b,0x82,0x30,0xa2,0xc7,0xef,0x52,0x08,0x7b,0x4f,0x3f,0x7a,
34858+
0x7e,0x28,0xd7,0x67,0xb9,0xb1,0x69,0x9b,0x96,0x3e,0xc5,0xe4,
34859+
0x45,0xb0,0x23,0x75,0x00,0xda,0xee,0xdb,0x6d,0xa9,0xe7,0x98
34860+
};
34861+
34862+
XMEMSET(key, 0, sizeof(key));
34863+
34864+
p7 = wc_PKCS7_New(NULL, INVALID_DEVID);
34865+
ExpectNotNull(p7);
34866+
if (p7 != NULL) {
34867+
p7->encryptionKey = key;
34868+
p7->encryptionKeySz = sizeof(key);
34869+
34870+
/* 496-byte content into 128-byte buffer must return BUFFER_E */
34871+
ExpectIntLT(wc_PKCS7_DecodeEncryptedData(p7, (byte*)encBlob,
34872+
(word32)sizeof(encBlob), out, sizeof(out)), 0);
34873+
34874+
wc_PKCS7_Free(p7);
34875+
}
34876+
#endif
34877+
return EXPECT_RESULT();
34878+
}
34879+
3479734880
TEST_CASE testCases[] = {
3479834881
TEST_DECL(test_fileAccess),
3479934882

@@ -35610,6 +35693,7 @@ TEST_CASE testCases[] = {
3561035693
TEST_DECL(test_wc_DhSetNamedKey),
3561135694
TEST_DECL(test_DhAgree_rejects_p_minus_1),
3561235695
TEST_DECL(test_ed448_rejects_identity_key),
35696+
TEST_DECL(test_pkcs7_decode_encrypted_outputsz),
3561335697

3561435698
#if defined(WOLFSSL_SNIFFER) && defined(WOLFSSL_SNIFFER_CHAIN_INPUT)
3561535699
TEST_DECL(test_sniffer_chain_input_overflow),

wolfcrypt/src/pkcs7.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14606,6 +14606,10 @@ int wc_PKCS7_DecodeAuthEnvelopedData(wc_PKCS7* pkcs7, byte* in,
1460614606
}
1460714607

1460814608
/* copy plaintext to output */
14609+
if ((word32)encryptedContentSz > outputSz) {
14610+
ret = BUFFER_E;
14611+
break;
14612+
}
1460914613
XMEMCPY(output, encryptedContent, (word32)encryptedContentSz);
1461014614

1461114615
/* free memory, zero out keys */
@@ -15290,6 +15294,11 @@ int wc_PKCS7_DecodeEncryptedData(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1529015294
}
1529115295

1529215296
/* copy plaintext to output */
15297+
if ((word32)(encryptedContentSz - padLen) > outputSz) {
15298+
XFREE(encryptedContent, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
15299+
ret = BUFFER_E;
15300+
break;
15301+
}
1529315302
XMEMCPY(output, encryptedContent,
1529415303
(unsigned int)(encryptedContentSz - padLen));
1529515304

0 commit comments

Comments
 (0)