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