Skip to content

Commit 6d299ea

Browse files
authored
Merge pull request #8634 from JacobBarthelmeh/pkcs7_stream
account for edge case with pkcs7 streaming
2 parents 18ed67a + 04dce0e commit 6d299ea

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

wolfcrypt/src/pkcs7.c

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2635,21 +2635,32 @@ static int wc_PKCS7_EncodeContentStream(wc_PKCS7* pkcs7, ESD* esd, void* aes,
26352635
/* check and handle octet boundary */
26362636
sz = contentDataRead;
26372637
if ((int)idx + sz > BER_OCTET_LENGTH) {
2638-
sz = BER_OCTET_LENGTH - (int)idx;
2639-
contentDataRead -= sz;
2638+
int amtWritten = 0;
26402639

2641-
XMEMCPY(contentData + idx, buf, (word32)sz);
2642-
ret = wc_PKCS7_EncodeContentStreamHelper(pkcs7, cipherType,
2643-
aes, encContentOut, contentData, BER_OCTET_LENGTH, out,
2644-
&outIdx, esd);
2645-
if (ret != 0) {
2646-
XFREE(encContentOut, heap, DYNAMIC_TYPE_PKCS7);
2647-
XFREE(contentData, heap, DYNAMIC_TYPE_PKCS7);
2648-
return ret;
2640+
/* loop over current buffer until it is empty */
2641+
while (idx + (word32)sz > BER_OCTET_LENGTH) {
2642+
sz = BER_OCTET_LENGTH;
2643+
if (idx > 0) { /* account for previously stored data */
2644+
sz = BER_OCTET_LENGTH - (int)idx;
2645+
}
2646+
contentDataRead -= sz;
2647+
2648+
XMEMCPY(contentData + idx, buf, (word32)sz);
2649+
ret = wc_PKCS7_EncodeContentStreamHelper(pkcs7, cipherType,
2650+
aes, encContentOut, contentData, BER_OCTET_LENGTH, out,
2651+
&outIdx, esd);
2652+
if (ret != 0) {
2653+
XFREE(encContentOut, heap, DYNAMIC_TYPE_PKCS7);
2654+
XFREE(contentData, heap, DYNAMIC_TYPE_PKCS7);
2655+
return ret;
2656+
}
2657+
idx = 0; /* cleared out previously stored data */
2658+
amtWritten += sz;
2659+
sz = contentDataRead;
26492660
}
26502661

26512662
/* copy over any remaining data */
2652-
XMEMCPY(contentData, buf + sz, (word32)contentDataRead);
2663+
XMEMCPY(contentData, buf + amtWritten, (word32)contentDataRead);
26532664
idx = (word32)contentDataRead;
26542665
}
26552666
else {

0 commit comments

Comments
 (0)