@@ -26872,32 +26872,37 @@ static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
2687226872#endif
2687326873
2687426874#if defined(HAVE_PKCS7) && defined(ASN_BER_TO_DER)
26875- static byte encodeSignedDataStreamOut[FOURK_BUF*3] = {0};
26876- static int encodeSignedDataStreamIdx = 0;
26877- static word32 encodeSignedDataStreamOutIdx = 0;
26875+ typedef struct encodeSignedDataStream {
26876+ byte out[FOURK_BUF*3];
26877+ int idx;
26878+ word32 outIdx;
26879+ } encodeSignedDataStream;
2687826880
2687926881
2688026882/* content is 8k of partially created bundle */
26881- static int GetContentCB(PKCS7* pkcs7, byte** content)
26883+ static int GetContentCB(PKCS7* pkcs7, byte** content, void* ctx )
2688226884{
2688326885 int ret = 0;
26886+ encodeSignedDataStream* strm = (encodeSignedDataStream*)ctx;
2688426887
26885- if (encodeSignedDataStreamOutIdx < pkcs7->contentSz) {
26886- ret = (pkcs7->contentSz > encodeSignedDataStreamOutIdx + FOURK_BUF)?
26887- FOURK_BUF : pkcs7->contentSz - encodeSignedDataStreamOutIdx ;
26888- *content = encodeSignedDataStreamOut + encodeSignedDataStreamOutIdx ;
26889- encodeSignedDataStreamOutIdx += ret;
26888+ if (strm->outIdx < pkcs7->contentSz) {
26889+ ret = (pkcs7->contentSz > strm->outIdx + FOURK_BUF)?
26890+ FOURK_BUF : pkcs7->contentSz - strm->outIdx ;
26891+ *content = strm->out + strm->outIdx ;
26892+ strm->outIdx += ret;
2689026893 }
2689126894
2689226895 (void)pkcs7;
2689326896 return ret;
2689426897}
2689526898
26896- static int StreamOutputCB(PKCS7* pkcs7, const byte* output, word32 outputSz)
26899+ static int StreamOutputCB(PKCS7* pkcs7, const byte* output, word32 outputSz,
26900+ void* ctx)
2689726901{
26898- XMEMCPY(encodeSignedDataStreamOut + encodeSignedDataStreamIdx, output,
26899- outputSz);
26900- encodeSignedDataStreamIdx += outputSz;
26902+ encodeSignedDataStream* strm = (encodeSignedDataStream*)ctx;
26903+
26904+ XMEMCPY(strm->out + strm->idx, output, outputSz);
26905+ strm->idx += outputSz;
2690126906 (void)pkcs7;
2690226907 return 0;
2690326908}
@@ -27031,6 +27036,7 @@ static int test_wc_PKCS7_EncodeSignedData(void)
2703127036 /* reinitialize and test setting stream mode */
2703227037 {
2703327038 int signedSz;
27039+ encodeSignedDataStream strm;
2703427040
2703527041 ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
2703627042 ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
@@ -27051,8 +27057,9 @@ static int test_wc_PKCS7_EncodeSignedData(void)
2705127057 pkcs7->rng = &rng;
2705227058 }
2705327059 ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 0);
27054- ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, NULL, NULL), 0);
27055- ExpectIntEQ(wc_PKCS7_SetStreamMode(NULL, 1, NULL, NULL), BAD_FUNC_ARG);
27060+ ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, NULL, NULL, NULL), 0);
27061+ ExpectIntEQ(wc_PKCS7_SetStreamMode(NULL, 1, NULL, NULL, NULL),
27062+ BAD_FUNC_ARG);
2705627063 ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 1);
2705727064
2705827065 ExpectIntGT(signedSz = wc_PKCS7_EncodeSignedData(pkcs7, output,
@@ -27085,8 +27092,9 @@ static int test_wc_PKCS7_EncodeSignedData(void)
2708527092 #endif
2708627093 pkcs7->rng = &rng;
2708727094 }
27095+ XMEMSET(&strm, 0, sizeof(strm));
2708827096 ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, GetContentCB,
27089- StreamOutputCB), 0);
27097+ StreamOutputCB, (void*)&strm ), 0);
2709027098
2709127099 ExpectIntGT(signedSz = wc_PKCS7_EncodeSignedData(pkcs7, NULL, 0), 0);
2709227100 wc_PKCS7_Free(pkcs7);
@@ -27096,8 +27104,7 @@ static int test_wc_PKCS7_EncodeSignedData(void)
2709627104 ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
2709727105
2709827106 /* use exact signed buffer size since BER encoded */
27099- ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, encodeSignedDataStreamOut,
27100- signedSz), 0);
27107+ ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, strm.out, signedSz), 0);
2710127108 }
2710227109#endif
2710327110
@@ -28335,6 +28342,8 @@ static int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
2833528342 testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
2833628343 for (i = 0; i < testSz; i++) {
2833728344 #ifdef ASN_BER_TO_DER
28345+ encodeSignedDataStream strm;
28346+
2833828347 /* test setting stream mode, the first one using IO callbacks */
2833928348 ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
2834028349 (word32)(testVectors + i)->certSz), 0);
@@ -28355,12 +28364,13 @@ static int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
2835528364 }
2835628365
2835728366 if (i == 0) {
28367+ XMEMSET(&strm, 0, sizeof(strm));
2835828368 ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, GetContentCB,
28359- StreamOutputCB), 0);
28369+ StreamOutputCB, (void*)&strm ), 0);
2836028370 encodedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL, 0);
2836128371 }
2836228372 else {
28363- ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, NULL, NULL), 0);
28373+ ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, NULL, NULL, NULL ), 0);
2836428374 encodedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
2836528375 (word32)sizeof(output));
2836628376 }
@@ -28396,7 +28406,7 @@ static int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
2839628406 if (encodedSz > 0) {
2839728407 if (i == 0) {
2839828408 decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7,
28399- encodeSignedDataStreamOut , (word32)encodedSz, decoded,
28409+ strm.out , (word32)encodedSz, decoded,
2840028410 (word32)sizeof(decoded));
2840128411 }
2840228412 else {
0 commit comments