@@ -1335,6 +1335,7 @@ int wc_PKCS12_parse_ex(WC_PKCS12* pkcs12, const char* psw,
13351335 byte * buf = NULL ;
13361336 word32 i , oid ;
13371337 word32 algId ;
1338+ word32 contentSz ;
13381339 int ret , pswSz ;
13391340#ifdef ASN_BER_TO_DER
13401341 int curIdx ;
@@ -1450,6 +1451,11 @@ int wc_PKCS12_parse_ex(WC_PKCS12* pkcs12, const char* psw,
14501451 goto exit_pk12par ;
14511452 }
14521453
1454+ /* DecryptContent strips the PBE ASN.1 wrapper and returns the
1455+ * actual decrypted payload size, which is smaller than the
1456+ * allocated buf. Track the real bounds so subsequent ASN.1
1457+ * parsing does not read past the decrypted content. */
1458+ contentSz = (word32 )ret ;
14531459 data = buf ;
14541460 idx = 0 ;
14551461
@@ -1486,36 +1492,39 @@ int wc_PKCS12_parse_ex(WC_PKCS12* pkcs12, const char* psw,
14861492 goto exit_pk12par ;
14871493 }
14881494
1495+ /* DATA branch: data still points into ci->data, so the
1496+ * ContentInfo size is the correct parsing bound. */
1497+ contentSz = ci -> dataSz ;
14891498 }
14901499
14911500 /* parse through bags in ContentInfo */
1492- if ((ret = GetSequence (data , & idx , & totalSz , ci -> dataSz )) < 0 ) {
1501+ if ((ret = GetSequence (data , & idx , & totalSz , contentSz )) < 0 ) {
14931502 goto exit_pk12par ;
14941503 }
14951504 totalSz += (int )idx ;
14961505
14971506 while ((int )idx < totalSz ) {
14981507 int bagSz ;
1499- if ((ret = GetSequence (data , & idx , & bagSz , ci -> dataSz )) < 0 ) {
1508+ if ((ret = GetSequence (data , & idx , & bagSz , contentSz )) < 0 ) {
15001509 goto exit_pk12par ;
15011510 }
15021511 bagSz += (int )idx ;
15031512
15041513 if ((ret = GetObjectId (data , & idx , & oid , oidIgnoreType ,
1505- ci -> dataSz )) < 0 ) {
1514+ contentSz )) < 0 ) {
15061515 goto exit_pk12par ;
15071516 }
15081517
15091518 switch (oid ) {
15101519 case WC_PKCS12_KeyBag : /* 667 */
15111520 WOLFSSL_MSG ("PKCS12 Key Bag found" );
1512- if (GetASNTag (data , & idx , & tag , ci -> dataSz ) < 0 ) {
1521+ if (GetASNTag (data , & idx , & tag , contentSz ) < 0 ) {
15131522 ERROR_OUT (ASN_PARSE_E , exit_pk12par );
15141523 }
15151524 if (tag != (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC )) {
15161525 ERROR_OUT (ASN_PARSE_E , exit_pk12par );
15171526 }
1518- if ((ret = GetLength (data , & idx , & size , ci -> dataSz )) <= 0 ) {
1527+ if ((ret = GetLength (data , & idx , & size , contentSz )) <= 0 ) {
15191528 if (ret == 0 )
15201529 ret = ASN_PARSE_E ;
15211530 goto exit_pk12par ;
@@ -1553,14 +1562,14 @@ int wc_PKCS12_parse_ex(WC_PKCS12* pkcs12, const char* psw,
15531562 byte * k ;
15541563
15551564 WOLFSSL_MSG ("PKCS12 Shrouded Key Bag found" );
1556- if (GetASNTag (data , & idx , & tag , ci -> dataSz ) < 0 ) {
1565+ if (GetASNTag (data , & idx , & tag , contentSz ) < 0 ) {
15571566 ERROR_OUT (ASN_PARSE_E , exit_pk12par );
15581567 }
15591568 if (tag != (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC )) {
15601569 ERROR_OUT (ASN_PARSE_E , exit_pk12par );
15611570 }
15621571 if ((ret = GetLength (data , & idx , & size ,
1563- ci -> dataSz )) < 0 ) {
1572+ contentSz )) < 0 ) {
15641573 goto exit_pk12par ;
15651574 }
15661575
@@ -1626,51 +1635,51 @@ int wc_PKCS12_parse_ex(WC_PKCS12* pkcs12, const char* psw,
16261635 {
16271636 WC_DerCertList * node ;
16281637 WOLFSSL_MSG ("PKCS12 Cert Bag found" );
1629- if (GetASNTag (data , & idx , & tag , ci -> dataSz ) < 0 ) {
1638+ if (GetASNTag (data , & idx , & tag , contentSz ) < 0 ) {
16301639 ERROR_OUT (ASN_PARSE_E , exit_pk12par );
16311640 }
16321641 if (tag != (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC )) {
16331642 ERROR_OUT (ASN_PARSE_E , exit_pk12par );
16341643 }
1635- if ((ret = GetLength (data , & idx , & size , ci -> dataSz )) < 0 ) {
1644+ if ((ret = GetLength (data , & idx , & size , contentSz )) < 0 ) {
16361645 goto exit_pk12par ;
16371646 }
16381647
16391648 /* get cert bag type */
1640- if ((ret = GetSequence (data , & idx , & size , ci -> dataSz )) < 0 ) {
1649+ if ((ret = GetSequence (data , & idx , & size , contentSz )) < 0 ) {
16411650 goto exit_pk12par ;
16421651 }
16431652
16441653 if ((ret = GetObjectId (data , & idx , & oid , oidIgnoreType ,
1645- ci -> dataSz )) < 0 ) {
1654+ contentSz )) < 0 ) {
16461655 goto exit_pk12par ;
16471656 }
16481657
16491658 switch (oid ) {
16501659 case WC_PKCS12_CertBag_Type1 : /* 675 */
16511660 /* type 1 */
16521661 WOLFSSL_MSG ("PKCS12 cert bag type 1" );
1653- if (GetASNTag (data , & idx , & tag , ci -> dataSz ) < 0 ) {
1662+ if (GetASNTag (data , & idx , & tag , contentSz ) < 0 ) {
16541663 ERROR_OUT (ASN_PARSE_E , exit_pk12par );
16551664 }
16561665 if (tag != (ASN_CONSTRUCTED |
16571666 ASN_CONTEXT_SPECIFIC )) {
16581667 ERROR_OUT (ASN_PARSE_E , exit_pk12par );
16591668 }
1660- if ((ret = GetLength (data , & idx , & size , ci -> dataSz ))
1669+ if ((ret = GetLength (data , & idx , & size , contentSz ))
16611670 <= 0 ) {
16621671 if (ret == 0 )
16631672 ret = ASN_PARSE_E ;
16641673 goto exit_pk12par ;
16651674 }
1666- if (GetASNTag (data , & idx , & tag , ci -> dataSz ) < 0 ) {
1675+ if (GetASNTag (data , & idx , & tag , contentSz ) < 0 ) {
16671676 ERROR_OUT (ASN_PARSE_E , exit_pk12par );
16681677 }
16691678 if (tag != ASN_OCTET_STRING ) {
16701679 ERROR_OUT (ASN_PARSE_E , exit_pk12par );
16711680
16721681 }
1673- if ((ret = GetLength (data , & idx , & size , ci -> dataSz ))
1682+ if ((ret = GetLength (data , & idx , & size , contentSz ))
16741683 < 0 ) {
16751684 goto exit_pk12par ;
16761685 }
0 commit comments