Skip to content

Commit 4e423fd

Browse files
committed
More PKCS#7 bounds checks
1 parent 46f3ebb commit 4e423fd

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
@@ -7046,6 +7046,9 @@ static int PKCS7_VerifySignedData(wc_PKCS7* pkcs7, const byte* hashBuf,
70467046

70477047
idx += (word32)length;
70487048
}
7049+
else if (ret == 0) {
7050+
ret = ASN_PARSE_E;
7051+
}
70497052

70507053
pkcs7->content = content;
70517054
pkcs7->contentSz = (word32)contentSz;
@@ -9626,7 +9629,7 @@ static int wc_PKCS7_PwriKek_KeyUnWrap(wc_PKCS7* pkcs7, const byte* kek,
96269629
cekLen = outTmp[0];
96279630

96289631
/* verify length */
9629-
fail |= ctMaskGT(cekLen, (int)inSz);
9632+
fail |= ctMaskGT(cekLen, (int)inSz - 4);
96309633
/* verify check bytes */
96319634
fail |= ctMaskNotEq((int)(outTmp[1] ^ outTmp[4]), 0xFF);
96329635
fail |= ctMaskNotEq((int)(outTmp[2] ^ outTmp[5]), 0xFF);
@@ -11933,7 +11936,9 @@ static int wc_PKCS7_DecryptKekri(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1193311936
&datePtr, &dateFormat, &dateLen) != 0) {
1193411937
return ASN_PARSE_E;
1193511938
}
11936-
*idx += (word32)(dateLen + 1);
11939+
/* datePtr points to the start of the date value
11940+
* within pkiMsg; advance past the full TLV. */
11941+
*idx = (word32)(datePtr - pkiMsg) + (word32)dateLen;
1193711942
}
1193811943

1193911944
if (*idx > pkiMsgSz) {
@@ -13102,6 +13107,14 @@ int wc_PKCS7_DecodeEnvelopedData(wc_PKCS7* pkcs7, byte* in,
1310213107
ret = ASN_PARSE_E;
1310313108
}
1310413109

13110+
#ifdef NO_PKCS7_STREAM
13111+
if (ret == 0 && encryptedContentTotalSz > (int)(pkiMsgSz - idx)) {
13112+
/* In non-streaming mode, ensure the content fits in the buffer.
13113+
* Streaming mode handles this via AddDataToStream. */
13114+
ret = BUFFER_E;
13115+
}
13116+
#endif
13117+
1310513118
if (ret != 0)
1310613119
break;
1310713120

@@ -15355,6 +15368,12 @@ int wc_PKCS7_DecodeEncryptedData(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1535515368
pkiMsgSz, NO_USER_CHECK) <= 0)
1535615369
ret = ASN_PARSE_E;
1535715370

15371+
#ifdef NO_PKCS7_STREAM
15372+
if (ret == 0 && encryptedContentSz > (int)(pkiMsgSz - idx)) {
15373+
ret = BUFFER_E;
15374+
}
15375+
#endif
15376+
1535815377
if (ret < 0)
1535915378
break;
1536015379
#ifndef NO_PKCS7_STREAM
@@ -15392,7 +15411,8 @@ int wc_PKCS7_DecodeEncryptedData(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1539215411
version = (int)pkcs7->stream->vers;
1539315412
tmpIv = pkcs7->stream->tmpIv;
1539415413
#endif
15395-
if (encryptedContentSz <= 0) {
15414+
if (encryptedContentSz <= 0 ||
15415+
encryptedContentSz > (int)(pkiMsgSz - idx)) {
1539615416
ret = BUFFER_E;
1539715417
break;
1539815418
}

0 commit comments

Comments
 (0)