@@ -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+
0 commit comments