@@ -10769,15 +10769,19 @@ static int wc_PKCS7_DecryptKtri(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1076910769 if (GetLength(pkiMsg, idx, &length, pkiMsgSz) < 0)
1077010770 return ASN_PARSE_E;
1077110771
10772- if ((word32)keyIdSize > pkiMsgSz - (*idx))
10772+ /* Validate SKID container and keyIdSize against buffer */
10773+ if ((word32)length > pkiMsgSz - (*idx))
1077310774 return BUFFER_E;
1077410775
10776+ if (length < keyIdSize)
10777+ return ASN_PARSE_E;
10778+
1077510779 /* if we found correct recipient, SKID will match */
1077610780 if (XMEMCMP(pkiMsg + (*idx), pkcs7->issuerSubjKeyId,
1077710781 (word32)keyIdSize) == 0) {
1077810782 *recipFound = 1;
1077910783 }
10780- (*idx) += (word32)keyIdSize ;
10784+ (*idx) += (word32)length ;
1078110785 }
1078210786
1078310787 if (GetAlgoId(pkiMsg, idx, &encOID, oidKeyType, pkiMsgSz) < 0)
@@ -11054,6 +11058,14 @@ static int wc_PKCS7_KariGetOriginatorIdentifierOrKey(WC_PKCS7_KARI* kari,
1105411058 if (GetLength(pkiMsg, idx, &length, pkiMsgSz) < 0)
1105511059 return ASN_PARSE_E;
1105611060
11061+ /* BIT STRING must have at least unused-bits byte + 1 byte of content */
11062+ if (length < 2)
11063+ return ASN_PARSE_E;
11064+
11065+ /* Validate BIT STRING content is within input buffer */
11066+ if (*idx > pkiMsgSz || (word32)length > pkiMsgSz - *idx)
11067+ return ASN_PARSE_E;
11068+
1105711069 if (GetASNTag(pkiMsg, idx, &tag, pkiMsgSz) < 0)
1105811070 return ASN_EXPECT_0_E;
1105911071
@@ -11533,9 +11545,22 @@ static int wc_PKCS7_DecryptOri(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1153311545 XMEMCPY(oriOID, pkiMsg + *idx, (word32)oriOIDSz);
1153411546 *idx += (word32)oriOIDSz;
1153511547
11548+ /* Validate OID did not consume more than the SEQUENCE declared */
11549+ if ((*idx - tmpIdx) > (word32)seqSz) {
11550+ WOLFSSL_MSG("ORI oriType OID exceeds SEQUENCE boundary");
11551+ return ASN_PARSE_E;
11552+ }
11553+
1153611554 /* get oriValue, increment idx */
1153711555 oriValue = pkiMsg + *idx;
1153811556 oriValueSz = (word32)seqSz - (*idx - tmpIdx);
11557+
11558+ /* Validate oriValue region is within input buffer */
11559+ if (*idx > pkiMsgSz || oriValueSz > pkiMsgSz - *idx) {
11560+ WOLFSSL_MSG("ORI oriValue exceeds input buffer");
11561+ return ASN_PARSE_E;
11562+ }
11563+
1153911564 *idx += oriValueSz;
1154011565
1154111566 /* pass oriOID and oriValue to user callback, expect back
@@ -11713,6 +11738,12 @@ static int wc_PKCS7_DecryptPwri(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1171311738 return ASN_PARSE_E;
1171411739 }
1171511740
11741+ /* Validate IV is within input buffer */
11742+ if (*idx > pkiMsgSz || (word32)length > pkiMsgSz - *idx) {
11743+ XFREE(salt, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
11744+ return ASN_PARSE_E;
11745+ }
11746+
1171611747 XMEMCPY(tmpIv, pkiMsg + (*idx), (word32)length);
1171711748 *idx += (word32)length;
1171811749
@@ -11732,6 +11763,12 @@ static int wc_PKCS7_DecryptPwri(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1173211763 return ASN_PARSE_E;
1173311764 }
1173411765
11766+ /* Validate EncryptedKey is within input buffer */
11767+ if (*idx > pkiMsgSz || (word32)length > pkiMsgSz - *idx) {
11768+ XFREE(salt, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
11769+ return ASN_PARSE_E;
11770+ }
11771+
1173511772 /* allocate temporary space for decrypted key */
1173611773 cekSz = (word32)length;
1173711774 cek = (byte*)XMALLOC(cekSz, pkcs7->heap, DYNAMIC_TYPE_TMP_BUFFER);
@@ -11818,7 +11855,7 @@ static int wc_PKCS7_DecryptKekri(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1181811855 byte* keyId = NULL;
1181911856 const byte* datePtr = NULL;
1182011857 byte dateFormat, tag;
11821- word32 keyIdSz, kekIdSz, keyWrapOID, localIdx;
11858+ word32 keyIdSz, kekIdSz, kekIdEnd, keyWrapOID, localIdx;
1182211859
1182311860 int ret = 0;
1182411861 byte* pkiMsg = in;
@@ -11844,6 +11881,11 @@ static int wc_PKCS7_DecryptKekri(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1184411881 return ASN_PARSE_E;
1184511882
1184611883 kekIdSz = (word32)length;
11884+ kekIdEnd = *idx + kekIdSz;
11885+
11886+ /* Validate KEKIdentifier boundary is within input buffer */
11887+ if (kekIdEnd < *idx || kekIdEnd > pkiMsgSz)
11888+ return ASN_PARSE_E;
1184711889
1184811890 if (GetASNTag(pkiMsg, idx, &tag, pkiMsgSz) < 0)
1184911891 return ASN_PARSE_E;
@@ -11854,17 +11896,21 @@ static int wc_PKCS7_DecryptKekri(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1185411896 if (GetLength(pkiMsg, idx, &length, pkiMsgSz) < 0)
1185511897 return ASN_PARSE_E;
1185611898
11899+ /* Validate keyIdentifier is within input buffer */
11900+ if (*idx > pkiMsgSz || (word32)length > pkiMsgSz - *idx)
11901+ return ASN_PARSE_E;
11902+
1185711903 /* save keyIdentifier and length */
1185811904 keyId = pkiMsg + *idx;
1185911905 keyIdSz = (word32)length;
1186011906 *idx += keyIdSz;
1186111907
1186211908 /* may have OPTIONAL GeneralizedTime */
1186311909 localIdx = *idx;
11864- if ((*idx < kekIdSz ) && GetASNTag(pkiMsg, &localIdx, &tag,
11910+ if ((*idx < kekIdEnd ) && GetASNTag(pkiMsg, &localIdx, &tag,
1186511911 pkiMsgSz) == 0 && tag == ASN_GENERALIZED_TIME) {
11866- if (wc_GetDateInfo(pkiMsg + *idx, (int)pkiMsgSz, &datePtr ,
11867- &dateFormat, &dateLen) != 0) {
11912+ if (wc_GetDateInfo(pkiMsg + *idx, (int)( pkiMsgSz - *idx) ,
11913+ &datePtr, & dateFormat, &dateLen) != 0) {
1186811914 return ASN_PARSE_E;
1186911915 }
1187011916 *idx += (word32)(dateLen + 1);
@@ -11876,7 +11922,7 @@ static int wc_PKCS7_DecryptKekri(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1187611922
1187711923 /* may have OPTIONAL OtherKeyAttribute */
1187811924 localIdx = *idx;
11879- if ((*idx < kekIdSz ) && GetASNTag(pkiMsg, &localIdx, &tag,
11925+ if ((*idx < kekIdEnd ) && GetASNTag(pkiMsg, &localIdx, &tag,
1188011926 pkiMsgSz) == 0 && tag == (ASN_SEQUENCE |
1188111927 ASN_CONSTRUCTED)) {
1188211928 if (GetSequence(pkiMsg, idx, &length, pkiMsgSz) < 0)
@@ -11905,6 +11951,10 @@ static int wc_PKCS7_DecryptKekri(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1190511951 if (GetLength(pkiMsg, idx, &length, pkiMsgSz) < 0)
1190611952 return ASN_PARSE_E;
1190711953
11954+ /* Validate EncryptedKey is within input buffer */
11955+ if (*idx > pkiMsgSz || (word32)length > pkiMsgSz - *idx)
11956+ return ASN_PARSE_E;
11957+
1190811958 #ifndef NO_AES
1190911959 direction = AES_DECRYPTION;
1191011960 #else
0 commit comments