Skip to content

Commit 66f419b

Browse files
add user ctx to stream IO callbacks
1 parent fbf1b78 commit 66f419b

3 files changed

Lines changed: 40 additions & 27 deletions

File tree

tests/api.c

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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 {

wolfcrypt/src/pkcs7.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2497,7 +2497,7 @@ static int wc_PKCS7_EncodeContentStream(PKCS7* pkcs7, ESD* esd, void* aes,
24972497
#ifdef ASN_BER_TO_DER
24982498
if (pkcs7->getContentCb) {
24992499
contentDataRead = pkcs7->getContentCb(pkcs7,
2500-
&buf);
2500+
&buf, pkcs7->streamCtx);
25012501
}
25022502
else
25032503
#endif
@@ -7549,7 +7549,7 @@ int wc_PKCS7_WriteOut(PKCS7* pkcs7, byte* output, const byte* input,
75497549

75507550
#ifdef ASN_BER_TO_DER
75517551
if (pkcs7->streamOutCb) {
7552-
ret = pkcs7->streamOutCb(pkcs7, input, inputSz);
7552+
ret = pkcs7->streamOutCb(pkcs7, input, inputSz, pkcs7->streamCtx);
75537553
/* sanity check on user provided ret value */
75547554
if (ret < 0) {
75557555
WOLFSSL_MSG("Return value error from stream out callback");
@@ -13854,7 +13854,7 @@ int wc_PKCS7_SetDecodeEncryptedCtx(PKCS7* pkcs7, void* ctx)
1385413854
* returns 0 on success */
1385513855
int wc_PKCS7_SetStreamMode(PKCS7* pkcs7, byte flag,
1385613856
CallbackGetContent getContentCb,
13857-
CallbackStreamOut streamOutCb)
13857+
CallbackStreamOut streamOutCb, void* ctx)
1385813858
{
1385913859
if (pkcs7 == NULL) {
1386013860
return BAD_FUNC_ARG;
@@ -13863,11 +13863,13 @@ int wc_PKCS7_SetStreamMode(PKCS7* pkcs7, byte flag,
1386313863
pkcs7->encodeStream = flag;
1386413864
pkcs7->getContentCb = getContentCb;
1386513865
pkcs7->streamOutCb = streamOutCb;
13866+
pkcs7->streamCtx = ctx;
1386613867
return 0;
1386713868
#else
1386813869
(void)flag;
1386913870
(void)getContentCb;
1387013871
(void)streamOutCb;
13872+
(void)ctx;
1387113873
return NOT_COMPILED_IN;
1387213874
#endif
1387313875
}

wolfssl/wolfcrypt/pkcs7.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,9 @@ typedef int (*CallbackWrapCEK)(PKCS7* pkcs7, byte* cek, word32 cekSz,
225225
int keyWrapAlgo, int type, int dir);
226226

227227
/* Callbacks for supporting different stream cases */
228-
typedef int (*CallbackGetContent)(PKCS7* pkcs7, byte** content);
228+
typedef int (*CallbackGetContent)(PKCS7* pkcs7, byte** content, void* ctx);
229229
typedef int (*CallbackStreamOut)(PKCS7* pkcs7, const byte* output,
230-
word32 outputSz);
230+
word32 outputSz, void* ctx);
231231

232232
#if defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && !defined(NO_RSA)
233233
/* RSA sign raw digest callback, user builds DigestInfo */
@@ -254,6 +254,7 @@ struct PKCS7 {
254254
word32 derSz;
255255
CallbackGetContent getContentCb;
256256
CallbackStreamOut streamOutCb;
257+
void* streamCtx; /* passed to getcontentCb and streamOutCb */
257258
#endif
258259
byte encodeStream:1; /* use BER when encoding */
259260
byte noCerts:1; /* if certificates should be added into bundle
@@ -509,7 +510,7 @@ WOLFSSL_API int wc_PKCS7_SetDecodeEncryptedCtx(PKCS7* pkcs7, void* ctx);
509510
WOLFSSL_LOCAL int wc_PKCS7_WriteOut(PKCS7* pkcs7, byte* output,
510511
const byte* input, word32 inputSz);
511512
WOLFSSL_API int wc_PKCS7_SetStreamMode(PKCS7* pkcs7, byte flag,
512-
CallbackGetContent getContentCb, CallbackStreamOut streamOutCb);
513+
CallbackGetContent getContentCb, CallbackStreamOut streamOutCb, void* ctx);
513514
WOLFSSL_API int wc_PKCS7_GetStreamMode(PKCS7* pkcs7);
514515
WOLFSSL_API int wc_PKCS7_SetNoCerts(PKCS7* pkcs7, byte flag);
515516
WOLFSSL_API int wc_PKCS7_GetNoCerts(PKCS7* pkcs7);

0 commit comments

Comments
 (0)