Skip to content

Commit 9eac8cb

Browse files
add a test case
1 parent 2044d6b commit 9eac8cb

1 file changed

Lines changed: 87 additions & 7 deletions

File tree

tests/api.c

Lines changed: 87 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26871,6 +26871,38 @@ static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
2687126871
}
2687226872
#endif
2687326873

26874+
#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;
26878+
26879+
26880+
/* content is 8k of partially created bundle */
26881+
static int GetContentCB(PKCS7* pkcs7, byte** content)
26882+
{
26883+
int ret = 0;
26884+
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;
26890+
}
26891+
26892+
(void)pkcs7;
26893+
return ret;
26894+
}
26895+
26896+
static int StreamOutputCB(PKCS7* pkcs7, const byte* output, word32 outputSz)
26897+
{
26898+
XMEMCPY(encodeSignedDataStreamOut + encodeSignedDataStreamIdx, output,
26899+
outputSz);
26900+
encodeSignedDataStreamIdx += outputSz;
26901+
(void)pkcs7;
26902+
return 0;
26903+
}
26904+
#endif
26905+
2687426906

2687526907
/*
2687626908
* Testing wc_PKCS7_EncodeSignedData()
@@ -27033,6 +27065,39 @@ static int test_wc_PKCS7_EncodeSignedData(void)
2703327065

2703427066
/* use exact signed buffer size since BER encoded */
2703527067
ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, signedSz), 0);
27068+
wc_PKCS7_Free(pkcs7);
27069+
27070+
/* now try with using callbacks for IO */
27071+
ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
27072+
ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
27073+
27074+
ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
27075+
27076+
if (pkcs7 != NULL) {
27077+
pkcs7->contentSz = FOURK_BUF*2;
27078+
pkcs7->privateKey = key;
27079+
pkcs7->privateKeySz = (word32)sizeof(key);
27080+
pkcs7->encryptOID = RSAk;
27081+
#ifdef NO_SHA
27082+
pkcs7->hashOID = SHA256h;
27083+
#else
27084+
pkcs7->hashOID = SHAh;
27085+
#endif
27086+
pkcs7->rng = &rng;
27087+
}
27088+
ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, GetContentCB,
27089+
StreamOutputCB), 0);
27090+
27091+
ExpectIntGT(signedSz = wc_PKCS7_EncodeSignedData(pkcs7, NULL, 0), 0);
27092+
wc_PKCS7_Free(pkcs7);
27093+
pkcs7 = NULL;
27094+
27095+
ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
27096+
ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
27097+
27098+
/* use exact signed buffer size since BER encoded */
27099+
ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, encodeSignedDataStreamOut,
27100+
signedSz), 0);
2703627101
}
2703727102
#endif
2703827103

@@ -28270,15 +28335,16 @@ static int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
2827028335
testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
2827128336
for (i = 0; i < testSz; i++) {
2827228337
#ifdef ASN_BER_TO_DER
28273-
/* test setting stream mode */
28338+
/* test setting stream mode, the first one using IO callbacks */
2827428339
ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
2827528340
(word32)(testVectors + i)->certSz), 0);
2827628341
if (pkcs7 != NULL) {
2827728342
#ifdef ECC_TIMING_RESISTANT
2827828343
pkcs7->rng = &rng;
2827928344
#endif
2828028345

28281-
pkcs7->content = (byte*)(testVectors + i)->content;
28346+
if (i != 0)
28347+
pkcs7->content = (byte*)(testVectors + i)->content;
2828228348
pkcs7->contentSz = (testVectors + i)->contentSz;
2828328349
pkcs7->contentOID = (testVectors + i)->contentOID;
2828428350
pkcs7->encryptOID = (testVectors + i)->encryptOID;
@@ -28287,10 +28353,17 @@ static int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
2828728353
pkcs7->privateKey = (testVectors + i)->privateKey;
2828828354
pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
2828928355
}
28290-
ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, NULL, NULL), 0);
2829128356

28292-
encodedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
28293-
(word32)sizeof(output));
28357+
if (i == 0) {
28358+
ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, GetContentCB,
28359+
StreamOutputCB), 0);
28360+
encodedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL, 0);
28361+
}
28362+
else {
28363+
ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, NULL, NULL), 0);
28364+
encodedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
28365+
(word32)sizeof(output));
28366+
}
2829428367

2829528368
switch ((testVectors + i)->encryptOID) {
2829628369
#ifndef NO_DES3
@@ -28321,8 +28394,15 @@ static int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
2832128394
}
2832228395

2832328396
if (encodedSz > 0) {
28324-
decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
28325-
(word32)encodedSz, decoded, (word32)sizeof(decoded));
28397+
if (i == 0) {
28398+
decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7,
28399+
encodeSignedDataStreamOut, (word32)encodedSz, decoded,
28400+
(word32)sizeof(decoded));
28401+
}
28402+
else {
28403+
decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
28404+
(word32)encodedSz, decoded, (word32)sizeof(decoded));
28405+
}
2832628406
ExpectIntGE(decodedSz, 0);
2832728407
/* Verify the size of each buffer. */
2832828408
ExpectIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);

0 commit comments

Comments
 (0)