Skip to content

Commit 82b6b9c

Browse files
authored
Merge pull request #10018 from embhorn/zd21389
Fix GetSafeContent to check length
2 parents 2c030dd + b4d2cd6 commit 82b6b9c

3 files changed

Lines changed: 46 additions & 2 deletions

File tree

tests/api/test_pkcs12.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,39 @@ int test_wc_d2i_PKCS12_bad_mac_salt(void)
235235
return EXPECT_RESULT();
236236
}
237237

238+
/* Test that a crafted PKCS12 with a ContentInfo SEQUENCE length smaller than
239+
* the contained OID is rejected, rather than causing an integer underflow
240+
* in ci->dataSz calculation. */
241+
int test_wc_d2i_PKCS12_oid_underflow(void)
242+
{
243+
EXPECT_DECLS;
244+
#if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12)
245+
WC_PKCS12* pkcs12 = NULL;
246+
247+
/* Crafted PKCS12 DER: the inner ContentInfo SEQUENCE declares length 5,
248+
* but contains a valid OID (1.2.840.113549.1.7.1) that is 11 bytes
249+
* on the wire (tag 06 + length 09 + 9 value bytes). Without the bounds
250+
* check, (word32)curSz - (localIdx - curIdx) = 5 - 11 underflows
251+
* to ~4GB. */
252+
static const byte crafted[] = {
253+
0x30, 0x23, /* outer SEQ */
254+
0x02, 0x01, 0x03, /* version 3 */
255+
0x30, 0x1E, /* AuthSafe wrapper SEQ */
256+
0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
257+
0x01, 0x07, 0x01, /* OID pkcs7-data */
258+
0xA0, 0x11, /* [0] CONSTRUCTED ctx */
259+
0x04, 0x0F, /* OCTET STRING */
260+
0x30, 0x0D, /* SEQ of ContentInfo arr */
261+
0x30, 0x05, /* ContentInfo SEQ, length=5 LIE */
262+
0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
263+
0x01, 0x07, 0x01 /* OID: 11 bytes actual */
264+
};
265+
266+
ExpectNotNull(pkcs12 = wc_PKCS12_new());
267+
ExpectIntEQ(wc_d2i_PKCS12(crafted, (word32)sizeof(crafted), pkcs12),
268+
ASN_PARSE_E);
269+
wc_PKCS12_free(pkcs12);
270+
#endif
271+
return EXPECT_RESULT();
272+
}
273+

tests/api/test_pkcs12.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@
2727
int test_wc_i2d_PKCS12(void);
2828
int test_wc_PKCS12_create(void);
2929
int test_wc_d2i_PKCS12_bad_mac_salt(void);
30+
int test_wc_d2i_PKCS12_oid_underflow(void);
3031

3132
#define TEST_PKCS12_DECLS \
3233
TEST_DECL_GROUP("pkcs12", test_wc_i2d_PKCS12), \
33-
TEST_DECL_GROUP("pkcs12", test_wc_PKCS12_create), \
34-
TEST_DECL_GROUP("pkcs12", test_wc_d2i_PKCS12_bad_mac_salt)
34+
TEST_DECL_GROUP("pkcs12", test_wc_PKCS12_create), \
35+
TEST_DECL_GROUP("pkcs12", test_wc_d2i_PKCS12_bad_mac_salt), \
36+
TEST_DECL_GROUP("pkcs12", test_wc_d2i_PKCS12_oid_underflow)
3537

3638
#endif /* WOLFCRYPT_TEST_PKCS12_H */

wolfcrypt/src/pkcs12.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,12 @@ static int GetSafeContent(WC_PKCS12* pkcs12, const byte* input,
334334
return ret;
335335
}
336336

337+
/* Check that OID did not consume more than the sequence length */
338+
if (localIdx > curIdx + (word32)curSz) {
339+
freeSafe(safe, pkcs12->heap);
340+
return ASN_PARSE_E;
341+
}
342+
337343
/* create new content info struct ... possible OID sanity check? */
338344
ci = (ContentInfo*)XMALLOC(sizeof(ContentInfo), pkcs12->heap,
339345
DYNAMIC_TYPE_PKCS);

0 commit comments

Comments
 (0)