@@ -10758,15 +10758,19 @@ static int wc_PKCS7_DecryptKtri(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1075810758 if (GetLength(pkiMsg, idx, &length, pkiMsgSz) < 0)
1075910759 return ASN_PARSE_E;
1076010760
10761- if ((word32)keyIdSize > pkiMsgSz - (*idx))
10761+ /* Validate SKID container and keyIdSize against buffer */
10762+ if ((word32)length > pkiMsgSz - (*idx))
1076210763 return BUFFER_E;
1076310764
10765+ if (length < keyIdSize)
10766+ return ASN_PARSE_E;
10767+
1076410768 /* if we found correct recipient, SKID will match */
1076510769 if (XMEMCMP(pkiMsg + (*idx), pkcs7->issuerSubjKeyId,
1076610770 (word32)keyIdSize) == 0) {
1076710771 *recipFound = 1;
1076810772 }
10769- (*idx) += (word32)keyIdSize ;
10773+ (*idx) += (word32)length ;
1077010774 }
1077110775
1077210776 if (GetAlgoId(pkiMsg, idx, &encOID, oidKeyType, pkiMsgSz) < 0)
@@ -11043,6 +11047,14 @@ static int wc_PKCS7_KariGetOriginatorIdentifierOrKey(WC_PKCS7_KARI* kari,
1104311047 if (GetLength(pkiMsg, idx, &length, pkiMsgSz) < 0)
1104411048 return ASN_PARSE_E;
1104511049
11050+ /* BIT STRING must have at least unused-bits byte + 1 byte of content */
11051+ if (length < 2)
11052+ return ASN_PARSE_E;
11053+
11054+ /* Validate BIT STRING content is within input buffer */
11055+ if (*idx > pkiMsgSz || (word32)length > pkiMsgSz - *idx)
11056+ return ASN_PARSE_E;
11057+
1104611058 if (GetASNTag(pkiMsg, idx, &tag, pkiMsgSz) < 0)
1104711059 return ASN_EXPECT_0_E;
1104811060
@@ -11522,9 +11534,22 @@ static int wc_PKCS7_DecryptOri(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1152211534 XMEMCPY(oriOID, pkiMsg + *idx, (word32)oriOIDSz);
1152311535 *idx += (word32)oriOIDSz;
1152411536
11537+ /* Validate OID did not consume more than the SEQUENCE declared */
11538+ if ((*idx - tmpIdx) > (word32)seqSz) {
11539+ WOLFSSL_MSG("ORI oriType OID exceeds SEQUENCE boundary");
11540+ return ASN_PARSE_E;
11541+ }
11542+
1152511543 /* get oriValue, increment idx */
1152611544 oriValue = pkiMsg + *idx;
1152711545 oriValueSz = (word32)seqSz - (*idx - tmpIdx);
11546+
11547+ /* Validate oriValue region is within input buffer */
11548+ if (*idx > pkiMsgSz || oriValueSz > pkiMsgSz - *idx) {
11549+ WOLFSSL_MSG("ORI oriValue exceeds input buffer");
11550+ return ASN_PARSE_E;
11551+ }
11552+
1152811553 *idx += oriValueSz;
1152911554
1153011555 /* pass oriOID and oriValue to user callback, expect back
@@ -11702,6 +11727,12 @@ static int wc_PKCS7_DecryptPwri(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1170211727 return ASN_PARSE_E;
1170311728 }
1170411729
11730+ /* Validate IV is within input buffer */
11731+ if (*idx > pkiMsgSz || (word32)length > pkiMsgSz - *idx) {
11732+ XFREE(salt, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
11733+ return ASN_PARSE_E;
11734+ }
11735+
1170511736 XMEMCPY(tmpIv, pkiMsg + (*idx), (word32)length);
1170611737 *idx += (word32)length;
1170711738
@@ -11721,6 +11752,12 @@ static int wc_PKCS7_DecryptPwri(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1172111752 return ASN_PARSE_E;
1172211753 }
1172311754
11755+ /* Validate EncryptedKey is within input buffer */
11756+ if (*idx > pkiMsgSz || (word32)length > pkiMsgSz - *idx) {
11757+ XFREE(salt, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
11758+ return ASN_PARSE_E;
11759+ }
11760+
1172411761 /* allocate temporary space for decrypted key */
1172511762 cekSz = (word32)length;
1172611763 cek = (byte*)XMALLOC(cekSz, pkcs7->heap, DYNAMIC_TYPE_TMP_BUFFER);
@@ -11807,7 +11844,7 @@ static int wc_PKCS7_DecryptKekri(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1180711844 byte* keyId = NULL;
1180811845 const byte* datePtr = NULL;
1180911846 byte dateFormat, tag;
11810- word32 keyIdSz, kekIdSz, keyWrapOID, localIdx;
11847+ word32 keyIdSz, kekIdSz, kekIdEnd, keyWrapOID, localIdx;
1181111848
1181211849 int ret = 0;
1181311850 byte* pkiMsg = in;
@@ -11833,6 +11870,11 @@ static int wc_PKCS7_DecryptKekri(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1183311870 return ASN_PARSE_E;
1183411871
1183511872 kekIdSz = (word32)length;
11873+ kekIdEnd = *idx + kekIdSz;
11874+
11875+ /* Validate KEKIdentifier boundary is within input buffer */
11876+ if (kekIdEnd < *idx || kekIdEnd > pkiMsgSz)
11877+ return ASN_PARSE_E;
1183611878
1183711879 if (GetASNTag(pkiMsg, idx, &tag, pkiMsgSz) < 0)
1183811880 return ASN_PARSE_E;
@@ -11843,17 +11885,21 @@ static int wc_PKCS7_DecryptKekri(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1184311885 if (GetLength(pkiMsg, idx, &length, pkiMsgSz) < 0)
1184411886 return ASN_PARSE_E;
1184511887
11888+ /* Validate keyIdentifier is within input buffer */
11889+ if (*idx > pkiMsgSz || (word32)length > pkiMsgSz - *idx)
11890+ return ASN_PARSE_E;
11891+
1184611892 /* save keyIdentifier and length */
1184711893 keyId = pkiMsg + *idx;
1184811894 keyIdSz = (word32)length;
1184911895 *idx += keyIdSz;
1185011896
1185111897 /* may have OPTIONAL GeneralizedTime */
1185211898 localIdx = *idx;
11853- if ((*idx < kekIdSz ) && GetASNTag(pkiMsg, &localIdx, &tag,
11899+ if ((*idx < kekIdEnd ) && GetASNTag(pkiMsg, &localIdx, &tag,
1185411900 pkiMsgSz) == 0 && tag == ASN_GENERALIZED_TIME) {
11855- if (wc_GetDateInfo(pkiMsg + *idx, (int)pkiMsgSz, &datePtr ,
11856- &dateFormat, &dateLen) != 0) {
11901+ if (wc_GetDateInfo(pkiMsg + *idx, (int)( pkiMsgSz - *idx) ,
11902+ &datePtr, & dateFormat, &dateLen) != 0) {
1185711903 return ASN_PARSE_E;
1185811904 }
1185911905 *idx += (word32)(dateLen + 1);
@@ -11865,7 +11911,7 @@ static int wc_PKCS7_DecryptKekri(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1186511911
1186611912 /* may have OPTIONAL OtherKeyAttribute */
1186711913 localIdx = *idx;
11868- if ((*idx < kekIdSz ) && GetASNTag(pkiMsg, &localIdx, &tag,
11914+ if ((*idx < kekIdEnd ) && GetASNTag(pkiMsg, &localIdx, &tag,
1186911915 pkiMsgSz) == 0 && tag == (ASN_SEQUENCE |
1187011916 ASN_CONSTRUCTED)) {
1187111917 if (GetSequence(pkiMsg, idx, &length, pkiMsgSz) < 0)
@@ -11894,6 +11940,10 @@ static int wc_PKCS7_DecryptKekri(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1189411940 if (GetLength(pkiMsg, idx, &length, pkiMsgSz) < 0)
1189511941 return ASN_PARSE_E;
1189611942
11943+ /* Validate EncryptedKey is within input buffer */
11944+ if (*idx > pkiMsgSz || (word32)length > pkiMsgSz - *idx)
11945+ return ASN_PARSE_E;
11946+
1189711947 #ifndef NO_AES
1189811948 direction = AES_DECRYPTION;
1189911949 #else
0 commit comments