@@ -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