account for edge case with pkcs7 streaming

pull/8634/head
JacobBarthelmeh 2025-04-03 14:39:38 -06:00
parent 3ff4e5e303
commit 8b0650d0fb
1 changed files with 22 additions and 11 deletions

View File

@ -2642,7 +2642,14 @@ static int wc_PKCS7_EncodeContentStream(wc_PKCS7* pkcs7, ESD* esd, void* aes,
/* check and handle octet boundary */ /* check and handle octet boundary */
sz = contentDataRead; sz = contentDataRead;
if ((int)idx + sz > BER_OCTET_LENGTH) { if ((int)idx + sz > BER_OCTET_LENGTH) {
sz = BER_OCTET_LENGTH - (int)idx; int amtWritten = 0;
/* loop over current buffer until it is empty */
while (idx + sz > BER_OCTET_LENGTH) {
sz = BER_OCTET_LENGTH;
if (idx > 0) { /* account for previously stored data */
sz = BER_OCTET_LENGTH - idx;
}
contentDataRead -= sz; contentDataRead -= sz;
XMEMCPY(contentData + idx, buf, (word32)sz); XMEMCPY(contentData + idx, buf, (word32)sz);
@ -2654,9 +2661,13 @@ static int wc_PKCS7_EncodeContentStream(wc_PKCS7* pkcs7, ESD* esd, void* aes,
XFREE(contentData, heap, DYNAMIC_TYPE_PKCS7); XFREE(contentData, heap, DYNAMIC_TYPE_PKCS7);
return ret; return ret;
} }
idx = 0; /* cleared out previously stored data */
amtWritten += sz;
sz = contentDataRead;
}
/* copy over any remaining data */ /* copy over any remaining data */
XMEMCPY(contentData, buf + sz, (word32)contentDataRead); XMEMCPY(contentData, buf + amtWritten, (word32)contentDataRead);
idx = (word32)contentDataRead; idx = (word32)contentDataRead;
} }
else { else {