Skip to content

Commit 40e7753

Browse files
committed
More PKCS#7 bounds checks
1 parent 09541f2 commit 40e7753

1 file changed

Lines changed: 23 additions & 3 deletions

File tree

wolfcrypt/src/pkcs7.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7035,6 +7035,9 @@ static int PKCS7_VerifySignedData(wc_PKCS7* pkcs7, const byte* hashBuf,
70357035

70367036
idx += (word32)length;
70377037
}
7038+
else if (ret == 0) {
7039+
ret = ASN_PARSE_E;
7040+
}
70387041

70397042
pkcs7->content = content;
70407043
pkcs7->contentSz = (word32)contentSz;
@@ -9615,7 +9618,7 @@ static int wc_PKCS7_PwriKek_KeyUnWrap(wc_PKCS7* pkcs7, const byte* kek,
96159618
cekLen = outTmp[0];
96169619

96179620
/* verify length */
9618-
fail |= ctMaskGT(cekLen, (int)inSz);
9621+
fail |= ctMaskGT(cekLen, (int)inSz - 4);
96199622
/* verify check bytes */
96209623
fail |= ctMaskNotEq((int)(outTmp[1] ^ outTmp[4]), 0xFF);
96219624
fail |= ctMaskNotEq((int)(outTmp[2] ^ outTmp[5]), 0xFF);
@@ -11922,7 +11925,9 @@ static int wc_PKCS7_DecryptKekri(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1192211925
&datePtr, &dateFormat, &dateLen) != 0) {
1192311926
return ASN_PARSE_E;
1192411927
}
11925-
*idx += (word32)(dateLen + 1);
11928+
/* datePtr points to the start of the date value
11929+
* within pkiMsg; advance past the full TLV. */
11930+
*idx = (word32)(datePtr - pkiMsg) + (word32)dateLen;
1192611931
}
1192711932

1192811933
if (*idx > pkiMsgSz) {
@@ -13091,6 +13096,14 @@ int wc_PKCS7_DecodeEnvelopedData(wc_PKCS7* pkcs7, byte* in,
1309113096
ret = ASN_PARSE_E;
1309213097
}
1309313098

13099+
#ifdef NO_PKCS7_STREAM
13100+
if (ret == 0 && encryptedContentTotalSz > (int)(pkiMsgSz - idx)) {
13101+
/* In non-streaming mode, ensure the content fits in the buffer.
13102+
* Streaming mode handles this via AddDataToStream. */
13103+
ret = BUFFER_E;
13104+
}
13105+
#endif
13106+
1309413107
if (ret != 0)
1309513108
break;
1309613109

@@ -15344,6 +15357,12 @@ int wc_PKCS7_DecodeEncryptedData(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1534415357
pkiMsgSz, NO_USER_CHECK) <= 0)
1534515358
ret = ASN_PARSE_E;
1534615359

15360+
#ifdef NO_PKCS7_STREAM
15361+
if (ret == 0 && encryptedContentSz > (int)(pkiMsgSz - idx)) {
15362+
ret = BUFFER_E;
15363+
}
15364+
#endif
15365+
1534715366
if (ret < 0)
1534815367
break;
1534915368
#ifndef NO_PKCS7_STREAM
@@ -15381,7 +15400,8 @@ int wc_PKCS7_DecodeEncryptedData(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1538115400
version = (int)pkcs7->stream->vers;
1538215401
tmpIv = pkcs7->stream->tmpIv;
1538315402
#endif
15384-
if (encryptedContentSz <= 0) {
15403+
if (encryptedContentSz <= 0 ||
15404+
encryptedContentSz > (int)(pkiMsgSz - idx)) {
1538515405
ret = BUFFER_E;
1538615406
break;
1538715407
}

0 commit comments

Comments
 (0)