Skip to content

Commit afa9934

Browse files
committed
More PKCS#7 bounds checks
1 parent 3f6a181 commit afa9934

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

wolfcrypt/src/pkcs7.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7034,6 +7034,9 @@ static int PKCS7_VerifySignedData(wc_PKCS7* pkcs7, const byte* hashBuf,
70347034

70357035
idx += (word32)length;
70367036
}
7037+
else if (ret == 0) {
7038+
ret = ASN_PARSE_E;
7039+
}
70377040

70387041
pkcs7->content = content;
70397042
pkcs7->contentSz = (word32)contentSz;
@@ -9614,7 +9617,7 @@ static int wc_PKCS7_PwriKek_KeyUnWrap(wc_PKCS7* pkcs7, const byte* kek,
96149617
cekLen = outTmp[0];
96159618

96169619
/* verify length */
9617-
fail |= ctMaskGT(cekLen, (int)inSz);
9620+
fail |= ctMaskGT(cekLen, (int)inSz - 4);
96189621
/* verify check bytes */
96199622
fail |= ctMaskNotEq((int)(outTmp[1] ^ outTmp[4]), 0xFF);
96209623
fail |= ctMaskNotEq((int)(outTmp[2] ^ outTmp[5]), 0xFF);
@@ -13090,6 +13093,14 @@ int wc_PKCS7_DecodeEnvelopedData(wc_PKCS7* pkcs7, byte* in,
1309013093
ret = ASN_PARSE_E;
1309113094
}
1309213095

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

@@ -15343,6 +15354,12 @@ int wc_PKCS7_DecodeEncryptedData(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1534315354
pkiMsgSz, NO_USER_CHECK) <= 0)
1534415355
ret = ASN_PARSE_E;
1534515356

15357+
#ifdef NO_PKCS7_STREAM
15358+
if (ret == 0 && encryptedContentSz > (int)(pkiMsgSz - idx)) {
15359+
ret = BUFFER_E;
15360+
}
15361+
#endif
15362+
1534615363
if (ret < 0)
1534715364
break;
1534815365
#ifndef NO_PKCS7_STREAM
@@ -15380,7 +15397,8 @@ int wc_PKCS7_DecodeEncryptedData(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1538015397
version = (int)pkcs7->stream->vers;
1538115398
tmpIv = pkcs7->stream->tmpIv;
1538215399
#endif
15383-
if (encryptedContentSz <= 0) {
15400+
if (encryptedContentSz <= 0 ||
15401+
encryptedContentSz > (int)(pkiMsgSz - idx)) {
1538415402
ret = BUFFER_E;
1538515403
break;
1538615404
}

0 commit comments

Comments
 (0)