@@ -2430,6 +2430,72 @@ static int wc_PKCS7_SignedDataBuildSignature(PKCS7* pkcs7,
24302430 #define BER_OCTET_LENGTH 4096
24312431#endif
24322432
2433+ /**
2434+ * This helper function encodes a chunk of content stream and writes it out.
2435+ *
2436+ * @param pkcs7 Pointer to a PKCS7 structure.
2437+ * @param cipherType The type of cipher to use for encryption.
2438+ * @param aes Optional pointer to an Aes structure for AES encryption.
2439+ * @param encContentOut Buffer to hold the encrypted content.
2440+ * @param contentData Buffer holding the content to be encrypted.
2441+ * @param contentDataSz Size of the content to be encrypted.
2442+ * @param out Buffer to hold the output data.
2443+ * @param outIdx Pointer to an index into the output buffer.
2444+ * @param esd Pointer to an ESD structure for digest calculation.
2445+ * @return Returns 0 on success, and a negative value on failure.
2446+ */
2447+ static int wc_PKCS7_EncodeContentStreamHelper (PKCS7 * pkcs7 , int cipherType ,
2448+ Aes * aes , byte * encContentOut , byte * contentData , int contentDataSz ,
2449+ byte * out , word32 * outIdx , ESD * esd )
2450+ {
2451+ int ret = BAD_FUNC_ARG ;
2452+ byte encContentOutOct [MAX_OCTET_STR_SZ ];
2453+ word32 encContentOutOctSz = 0 ;
2454+
2455+ switch (cipherType ) {
2456+ case WC_CIPHER_NONE :
2457+ XMEMCPY (encContentOut , contentData , contentDataSz );
2458+ if (esd && esd -> contentDigestSet != 1 ) {
2459+ ret = wc_HashUpdate (& esd -> hash , esd -> hashType ,
2460+ contentData , contentDataSz );
2461+ }
2462+ break ;
2463+
2464+ #ifndef NO_AES
2465+ case WC_CIPHER_AES_CBC :
2466+ ret = wc_AesCbcEncrypt (aes , encContentOut ,
2467+ contentData , contentDataSz );
2468+ break ;
2469+ #endif
2470+
2471+ #ifdef WOLFSSL_AESGCM_STREAM
2472+ case WC_CIPHER_AES_GCM :
2473+ ret = wc_AesGcmEncryptUpdate (aes , encContentOut ,
2474+ contentData , contentDataSz , NULL , 0 );
2475+ break ;
2476+ #endif
2477+ }
2478+
2479+ #ifdef WOLFSSL_ASYNC_CRYPT
2480+ /* async encrypt not available here, so block till done */
2481+ if (ret == WC_PENDING_E && cipherType != WC_CIPHER_NONE ) {
2482+ ret = wc_AsyncWait (ret , & aes -> asyncDev , WC_ASYNC_FLAG_NONE );
2483+ }
2484+ #endif
2485+
2486+ if (ret == 0 ) {
2487+ encContentOutOctSz = SetOctetString (contentDataSz , encContentOutOct );
2488+ wc_PKCS7_WriteOut (pkcs7 , (out )? out + * outIdx : NULL ,
2489+ encContentOutOct , encContentOutOctSz );
2490+ * outIdx += encContentOutOctSz ;
2491+ wc_PKCS7_WriteOut (pkcs7 , (out )? out + * outIdx : NULL ,
2492+ encContentOut , contentDataSz );
2493+ * outIdx += contentDataSz ;
2494+ }
2495+
2496+ return ret ;
2497+ }
2498+
24332499
24342500/* Used for encoding the content, potentially one octet chunck at a time if
24352501 * in streaming mode with IO callbacks set.
@@ -2457,8 +2523,6 @@ static int wc_PKCS7_EncodeContentStream(PKCS7* pkcs7, ESD* esd, void* aes,
24572523 if (pkcs7 -> encodeStream ) {
24582524 int sz ;
24592525 word32 totalSz = 0 ;
2460- byte encContentOutOct [MAX_OCTET_STR_SZ ];
2461- word32 encContentOutOctSz = 0 ;
24622526 byte * buf ;
24632527 byte * encContentOut ;
24642528 byte * contentData ;
@@ -2487,9 +2551,6 @@ static int wc_PKCS7_EncodeContentStream(PKCS7* pkcs7, ESD* esd, void* aes,
24872551 return MEMORY_E ;
24882552 }
24892553
2490- encContentOutOctSz = SetOctetString (BER_OCTET_LENGTH ,
2491- encContentOutOct );
2492-
24932554 /* keep pulling from content until empty */
24942555 do {
24952556 int contentDataRead = 0 ;
@@ -2498,6 +2559,14 @@ static int wc_PKCS7_EncodeContentStream(PKCS7* pkcs7, ESD* esd, void* aes,
24982559 if (pkcs7 -> getContentCb ) {
24992560 contentDataRead = pkcs7 -> getContentCb (pkcs7 ,
25002561 & buf , pkcs7 -> streamCtx );
2562+
2563+ if (buf == NULL ) {
2564+ WOLFSSL_MSG ("Get content callback returned null "
2565+ "buffer pointer" );
2566+ XFREE (encContentOut , heap , DYNAMIC_TYPE_PKCS7 );
2567+ XFREE (contentData , heap , DYNAMIC_TYPE_PKCS7 );
2568+ return BAD_FUNC_ARG ;
2569+ }
25012570 }
25022571 else
25032572 #endif
@@ -2515,14 +2584,6 @@ static int wc_PKCS7_EncodeContentStream(PKCS7* pkcs7, ESD* esd, void* aes,
25152584 /* no more data returned from callback */
25162585 break ;
25172586 }
2518-
2519- if (buf == NULL ) {
2520- WOLFSSL_MSG ("Get content callback returned null "
2521- "buffer pointer" );
2522- XFREE (encContentOut , heap , DYNAMIC_TYPE_PKCS7 );
2523- XFREE (contentData , heap , DYNAMIC_TYPE_PKCS7 );
2524- return BAD_FUNC_ARG ;
2525- }
25262587 totalSz += (word32 )contentDataRead ;
25272588
25282589 /* check and handle octet boundary */
@@ -2532,50 +2593,9 @@ static int wc_PKCS7_EncodeContentStream(PKCS7* pkcs7, ESD* esd, void* aes,
25322593 contentDataRead -= sz ;
25332594
25342595 XMEMCPY (contentData + idx , buf , sz );
2535-
2536- /* encrypt and flush out data */
2537- switch (cipherType ) {
2538- case WC_CIPHER_NONE :
2539- XMEMCPY (encContentOut , contentData , BER_OCTET_LENGTH );
2540- if (esd && esd -> contentDigestSet != 1 ) {
2541- ret = wc_HashUpdate (& esd -> hash , esd -> hashType ,
2542- contentData , BER_OCTET_LENGTH );
2543- }
2544- break ;
2545-
2546- #ifndef NO_AES
2547- case WC_CIPHER_AES_CBC :
2548- ret = wc_AesCbcEncrypt (aes , encContentOut ,
2549- contentData , BER_OCTET_LENGTH );
2550- break ;
2551- #endif
2552-
2553- #ifdef WOLFSSL_AESGCM_STREAM
2554- case WC_CIPHER_AES_GCM :
2555- ret = wc_AesGcmEncryptUpdate (aes , encContentOut ,
2556- contentData , BER_OCTET_LENGTH , NULL , 0 );
2557- break ;
2558- #endif
2559- }
2560-
2561- #ifdef WOLFSSL_ASYNC_CRYPT
2562- /* async encrypt not available here, so block till done */
2563- if (cipherType != WC_CIPHER_NONE ) {
2564- ret = wc_AsyncWait (ret , & aes -> asyncDev , WC_ASYNC_FLAG_NONE );
2565- }
2566- #endif
2567- if (pkcs7 -> encodeStream ) {
2568- wc_PKCS7_WriteOut (pkcs7 ,
2569- (out )? out + outIdx : NULL ,
2570- encContentOutOct ,
2571- encContentOutOctSz );
2572- outIdx += encContentOutOctSz ;
2573- }
2574- wc_PKCS7_WriteOut (pkcs7 ,
2575- (out )? out + outIdx : NULL ,
2576- encContentOut ,
2577- BER_OCTET_LENGTH );
2578- outIdx += BER_OCTET_LENGTH ;
2596+ ret = wc_PKCS7_EncodeContentStreamHelper (pkcs7 , cipherType ,
2597+ aes , encContentOut , contentData , BER_OCTET_LENGTH , out ,
2598+ & outIdx , esd );
25792599
25802600 /* copy over any remaining data */
25812601 XMEMCPY (contentData , buf + sz , contentDataRead );
@@ -2605,45 +2625,9 @@ static int wc_PKCS7_EncodeContentStream(PKCS7* pkcs7, ESD* esd, void* aes,
26052625 idx += padSz ;
26062626 }
26072627
2608-
26092628 /* encrypt and flush out remainder of content data */
2610- switch (cipherType ) {
2611- case WC_CIPHER_NONE :
2612- XMEMCPY (encContentOut , contentData , idx );
2613- if (esd && esd -> contentDigestSet != 1 ) {
2614- ret = wc_HashUpdate (& esd -> hash , esd -> hashType , contentData ,
2615- idx );
2616- }
2617- break ;
2618-
2619- #ifndef NO_AES
2620- case WC_CIPHER_AES_CBC :
2621- ret = wc_AesCbcEncrypt (aes , encContentOut , contentData , idx );
2622- break ;
2623- #endif
2624-
2625- #ifdef WOLFSSL_AESGCM_STREAM
2626- case WC_CIPHER_AES_GCM :
2627- ret = wc_AesGcmEncryptUpdate (aes , encContentOut ,
2628- contentData , idx , NULL , 0 );
2629- break ;
2630- #endif
2631- }
2632- #ifdef WOLFSSL_ASYNC_CRYPT
2633- /* async encrypt not available here, so block till done */
2634- if (cipherType != WC_CIPHER_NONE ) {
2635- ret = wc_AsyncWait (ret , & aes -> asyncDev , WC_ASYNC_FLAG_NONE );
2636- }
2637- #endif
2638- if (pkcs7 -> encodeStream ) {
2639- encContentOutOctSz = SetOctetString (idx ,
2640- encContentOutOct );
2641- wc_PKCS7_WriteOut (pkcs7 , (out )? out + outIdx : NULL ,
2642- encContentOutOct , encContentOutOctSz );
2643- outIdx += encContentOutOctSz ;
2644- }
2645- wc_PKCS7_WriteOut (pkcs7 , (out )? out + outIdx : NULL ,
2646- encContentOut , idx );
2629+ ret = wc_PKCS7_EncodeContentStreamHelper (pkcs7 , cipherType , aes ,
2630+ encContentOut , contentData , idx , out , & outIdx , esd );
26472631
26482632 if (cipherType == WC_CIPHER_NONE && esd && esd -> contentDigestSet != 1 ) {
26492633 ret = wc_HashFinal (& esd -> hash , esd -> hashType ,
@@ -7574,7 +7558,7 @@ int wc_PKCS7_WriteOut(PKCS7* pkcs7, byte* output, const byte* input,
75747558 XMEMCPY (output , input , inputSz );
75757559 }
75767560 else {
7577- WOLFSSL_MSG ("No provided way to output bundle" );
7561+ WOLFSSL_MSG ("No way provided to output bundle" );
75787562 ret = BUFFER_E ;
75797563 }
75807564
0 commit comments