Skip to content

Commit 8b0650d

Browse files
account for edge case with pkcs7 streaming
1 parent 3ff4e5e commit 8b0650d

1 file changed

Lines changed: 22 additions & 11 deletions

File tree

wolfcrypt/src/pkcs7.c

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2642,21 +2642,32 @@ static int wc_PKCS7_EncodeContentStream(wc_PKCS7* pkcs7, ESD* esd, void* aes,
26422642
/* check and handle octet boundary */
26432643
sz = contentDataRead;
26442644
if ((int)idx + sz > BER_OCTET_LENGTH) {
2645-
sz = BER_OCTET_LENGTH - (int)idx;
2646-
contentDataRead -= sz;
2645+
int amtWritten = 0;
26472646

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;
2647+
/* loop over current buffer until it is empty */
2648+
while (idx + sz > BER_OCTET_LENGTH) {
2649+
sz = BER_OCTET_LENGTH;
2650+
if (idx > 0) { /* account for previously stored data */
2651+
sz = BER_OCTET_LENGTH - idx;
2652+
}
2653+
contentDataRead -= sz;
2654+
2655+
XMEMCPY(contentData + idx, buf, (word32)sz);
2656+
ret = wc_PKCS7_EncodeContentStreamHelper(pkcs7, cipherType,
2657+
aes, encContentOut, contentData, BER_OCTET_LENGTH, out,
2658+
&outIdx, esd);
2659+
if (ret != 0) {
2660+
XFREE(encContentOut, heap, DYNAMIC_TYPE_PKCS7);
2661+
XFREE(contentData, heap, DYNAMIC_TYPE_PKCS7);
2662+
return ret;
2663+
}
2664+
idx = 0; /* cleared out previously stored data */
2665+
amtWritten += sz;
2666+
sz = contentDataRead;
26562667
}
26572668

26582669
/* copy over any remaining data */
2659-
XMEMCPY(contentData, buf + sz, (word32)contentDataRead);
2670+
XMEMCPY(contentData, buf + amtWritten, (word32)contentDataRead);
26602671
idx = (word32)contentDataRead;
26612672
}
26622673
else {

0 commit comments

Comments
 (0)