Skip to content

Commit 6f6b6e7

Browse files
Add additional checks for encryptedContentSz exceeding pkiMsgSz.
1 parent ee8b654 commit 6f6b6e7

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

wolfcrypt/src/pkcs7.c

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14356,9 +14356,17 @@ int wc_PKCS7_DecodeAuthEnvelopedData(wc_PKCS7* pkcs7, byte* in,
1435614356
}
1435714357

1435814358
if (ret == 0) {
14359-
XMEMCPY(encryptedContent, &pkiMsg[idx],
14359+
word32 tmpSum;
14360+
if (!WC_SAFE_SUM_WORD32(idx, (word32)encryptedContentSz,
14361+
tmpSum) ||
14362+
tmpSum > pkiMsgSz) {
14363+
ret = BUFFER_E;
14364+
break;
14365+
} else {
14366+
XMEMCPY(encryptedContent, &pkiMsg[idx],
1436014367
(word32)encryptedContentSz);
14361-
idx += (word32)encryptedContentSz;
14368+
idx += (word32)encryptedContentSz;
14369+
}
1436214370
}
1436314371
#ifndef NO_PKCS7_STREAM
1436414372
pkcs7->stream->bufferPt = encryptedContent;
@@ -15274,16 +15282,22 @@ int wc_PKCS7_DecodeEncryptedData(wc_PKCS7* pkcs7, byte* in, word32 inSz,
1527415282
}
1527515283

1527615284
if (ret == 0) {
15277-
XMEMCPY(encryptedContent, &pkiMsg[idx],
15278-
(unsigned int)encryptedContentSz);
15279-
idx += (word32)encryptedContentSz;
15280-
15281-
/* decrypt encryptedContent */
15282-
ret = wc_PKCS7_DecryptContent(pkcs7, encOID,
15283-
pkcs7->encryptionKey, pkcs7->encryptionKeySz,
15284-
tmpIv, expBlockSz, NULL, 0, NULL, 0,
15285-
encryptedContent, encryptedContentSz,
15286-
encryptedContent, pkcs7->devId, pkcs7->heap);
15285+
word32 tmpSum;
15286+
if (!WC_SAFE_SUM_WORD32(idx, (word32)encryptedContentSz, tmpSum) ||
15287+
tmpSum > pkiMsgSz) {
15288+
ret = BUFFER_E;
15289+
} else {
15290+
XMEMCPY(encryptedContent, &pkiMsg[idx],
15291+
(unsigned int)encryptedContentSz);
15292+
idx += (word32)encryptedContentSz;
15293+
15294+
/* decrypt encryptedContent */
15295+
ret = wc_PKCS7_DecryptContent(pkcs7, encOID,
15296+
pkcs7->encryptionKey, pkcs7->encryptionKeySz,
15297+
tmpIv, expBlockSz, NULL, 0, NULL, 0,
15298+
encryptedContent, encryptedContentSz,
15299+
encryptedContent, pkcs7->devId, pkcs7->heap);
15300+
}
1528715301
if (ret != 0) {
1528815302
XFREE(encryptedContent, pkcs7->heap, DYNAMIC_TYPE_PKCS7);
1528915303
}

0 commit comments

Comments
 (0)