Skip to content

Commit e0a19a7

Browse files
committed
Fix GetSafeContent to check length
1 parent 46f4b3b commit e0a19a7

3 files changed

Lines changed: 44 additions & 2 deletions

File tree

tests/api/test_pkcs12.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,37 @@ 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 11-byte OID (1.2.840.113549.1.7.1). Without the
249+
* bounds check, (word32)curSz - (localIdx - curIdx) = 5 - 11 underflows
250+
* to ~4GB. */
251+
static const byte crafted[] = {
252+
0x30, 0x23, /* outer SEQ */
253+
0x02, 0x01, 0x03, /* version 3 */
254+
0x30, 0x1E, /* AuthSafe wrapper SEQ */
255+
0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
256+
0x01, 0x07, 0x01, /* OID pkcs7-data */
257+
0xA0, 0x11, /* [0] CONSTRUCTED ctx */
258+
0x04, 0x0F, /* OCTET STRING */
259+
0x30, 0x0D, /* SEQ of ContentInfo arr */
260+
0x30, 0x05, /* ContentInfo SEQ, length=5 LIE */
261+
0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
262+
0x01, 0x07, 0x01 /* OID: 11 bytes actual */
263+
};
264+
265+
ExpectNotNull(pkcs12 = wc_PKCS12_new());
266+
ExpectIntLT(wc_d2i_PKCS12(crafted, (word32)sizeof(crafted), pkcs12), 0);
267+
wc_PKCS12_free(pkcs12);
268+
#endif
269+
return EXPECT_RESULT();
270+
}
271+

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)