Skip to content

Commit 8d0dc7a

Browse files
fix asn original build, vs warning, and add test cases
1 parent bf23357 commit 8d0dc7a

3 files changed

Lines changed: 109 additions & 31 deletions

File tree

tests/api.c

Lines changed: 90 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26916,13 +26916,56 @@ static int test_wc_PKCS7_EncodeSignedData(void)
2691626916
}
2691726917

2691826918
ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
26919-
2692026919
wc_PKCS7_Free(pkcs7);
2692126920
pkcs7 = NULL;
26921+
2692226922
ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
2692326923
ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
2692426924
ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
2692526925

26926+
#ifdef ASN_BER_TO_DER
26927+
wc_PKCS7_Free(pkcs7);
26928+
26929+
/* reinitialize and test setting stream mode */
26930+
{
26931+
int signedSz;
26932+
26933+
ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
26934+
ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
26935+
26936+
ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
26937+
26938+
if (pkcs7 != NULL) {
26939+
pkcs7->content = data;
26940+
pkcs7->contentSz = (word32)sizeof(data);
26941+
pkcs7->privateKey = key;
26942+
pkcs7->privateKeySz = (word32)sizeof(key);
26943+
pkcs7->encryptOID = RSAk;
26944+
#ifdef NO_SHA
26945+
pkcs7->hashOID = SHA256h;
26946+
#else
26947+
pkcs7->hashOID = SHAh;
26948+
#endif
26949+
pkcs7->rng = &rng;
26950+
}
26951+
ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 0);
26952+
ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1), 0);
26953+
ExpectIntEQ(wc_PKCS7_SetStreamMode(NULL, 1), BAD_FUNC_ARG);
26954+
ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 1);
26955+
26956+
ExpectIntGT(signedSz = wc_PKCS7_EncodeSignedData(pkcs7, output,
26957+
outputSz), 0);
26958+
wc_PKCS7_Free(pkcs7);
26959+
pkcs7 = NULL;
26960+
26961+
ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
26962+
ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
26963+
26964+
/* use exact signed buffer size since BER encoded */
26965+
ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, signedSz), 0);
26966+
}
26967+
#endif
26968+
2692626969
/* Pass in bad args. */
2692726970
ExpectIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz),
2692826971
BAD_FUNC_ARG);
@@ -27953,6 +27996,9 @@ static int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
2795327996
EXPECT_DECLS;
2795427997
#if defined(HAVE_PKCS7)
2795527998
PKCS7* pkcs7 = NULL;
27999+
#ifdef ASN_BER_TO_DER
28000+
int encodedSz;
28001+
#endif
2795628002
#ifdef ECC_TIMING_RESISTANT
2795728003
WC_RNG rng;
2795828004
#endif
@@ -28153,6 +28199,39 @@ static int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
2815328199

2815428200
testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
2815528201
for (i = 0; i < testSz; i++) {
28202+
#ifdef ASN_BER_TO_DER
28203+
/* test setting stream mode */
28204+
ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
28205+
(word32)(testVectors + i)->certSz), 0);
28206+
if (pkcs7 != NULL) {
28207+
#ifdef ECC_TIMING_RESISTANT
28208+
pkcs7->rng = &rng;
28209+
#endif
28210+
28211+
pkcs7->content = (byte*)(testVectors + i)->content;
28212+
pkcs7->contentSz = (testVectors + i)->contentSz;
28213+
pkcs7->contentOID = (testVectors + i)->contentOID;
28214+
pkcs7->encryptOID = (testVectors + i)->encryptOID;
28215+
pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
28216+
pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
28217+
pkcs7->privateKey = (testVectors + i)->privateKey;
28218+
pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
28219+
}
28220+
ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1), 0);
28221+
28222+
ExpectIntGE(encodedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
28223+
(word32)sizeof(output)), 0);
28224+
28225+
decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
28226+
(word32)encodedSz, decoded, (word32)sizeof(decoded));
28227+
ExpectIntGE(decodedSz, 0);
28228+
/* Verify the size of each buffer. */
28229+
ExpectIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
28230+
wc_PKCS7_Free(pkcs7);
28231+
pkcs7 = NULL;
28232+
ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
28233+
#endif
28234+
2815628235
ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
2815728236
(word32)(testVectors + i)->certSz), 0);
2815828237
if (pkcs7 != NULL) {
@@ -28170,6 +28249,11 @@ static int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
2817028249
pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
2817128250
}
2817228251

28252+
#ifdef ASN_BER_TO_DER
28253+
/* test without setting stream mode */
28254+
ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 0);
28255+
#endif
28256+
2817328257
ExpectIntGE(wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
2817428258
(word32)sizeof(output)), 0);
2817528259

@@ -28178,6 +28262,7 @@ static int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
2817828262
ExpectIntGE(decodedSz, 0);
2817928263
/* Verify the size of each buffer. */
2818028264
ExpectIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
28265+
2818128266
/* Don't free the last time through the loop. */
2818228267
if (i < testSz - 1) {
2818328268
wc_PKCS7_Free(pkcs7);
@@ -28871,7 +28956,6 @@ static int test_wc_PKCS7_signed_enveloped(void)
2887128956
#ifdef HAVE_AES_CBC
2887228957
PKCS7* inner = NULL;
2887328958
#endif
28874-
void* pt = NULL;
2887528959
WC_RNG rng;
2887628960
unsigned char key[FOURK_BUF/2];
2887728961
unsigned char cert[FOURK_BUF/2];
@@ -28958,17 +29042,13 @@ static int test_wc_PKCS7_signed_enveloped(void)
2895829042
pkcs7->rng = &rng;
2895929043
}
2896029044

28961-
/* Set no certs in bundle for this test. Hang on to the pointer though to
28962-
* free it later. */
29045+
/* Set no certs in bundle for this test. */
2896329046
if (pkcs7 != NULL) {
28964-
pt = (void*)pkcs7->certList;
28965-
pkcs7->certList = NULL; /* no certs in bundle */
29047+
ExpectIntEQ(wc_PKCS7_SetNoCerts(pkcs7, 1), 0);
29048+
ExpectIntEQ(wc_PKCS7_SetNoCerts(NULL, 1), BAD_FUNC_ARG);
29049+
ExpectIntEQ(wc_PKCS7_GetNoCerts(pkcs7), 1);
2896629050
}
2896729051
ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
28968-
if (pkcs7 != NULL) {
28969-
/* restore pointer for PKCS7 free call */
28970-
pkcs7->certList = (Pkcs7Cert*)pt;
28971-
}
2897229052
wc_PKCS7_Free(pkcs7);
2897329053
pkcs7 = NULL;
2897429054

wolfcrypt/src/asn.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15482,7 +15482,7 @@ word32 SetSet(word32 len, byte* output)
1548215482
*/
1548315483
word32 SetImplicit(byte tag, byte number, word32 len, byte* output, byte isIndef)
1548415484
{
15485-
int useIndef = 0;
15485+
byte useIndef = 0;
1548615486

1548715487
if ((tag == ASN_OCTET_STRING) && isIndef) {
1548815488
tag = ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | number;
@@ -36453,7 +36453,7 @@ int EncodeOcspRequest(OcspRequest* req, byte* output, word32 size)
3645336453
*/
3645436454
extSz = EncodeOcspRequestExtensions(req, extArray + 2,
3645536455
OCSP_NONCE_EXT_SZ);
36456-
extSz += SetExplicit(2, extSz, extArray);
36456+
extSz += SetExplicit(2, extSz, extArray, 0);
3645736457
}
3645836458

3645936459
totalSz = algoSz + issuerSz + issuerKeySz + snSz;

wolfcrypt/src/pkcs7.c

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2630,27 +2630,25 @@ static int PKCS7_EncodeSigned(PKCS7* pkcs7, ESD* esd,
26302630
totalSz -= pkcs7->contentSz;
26312631
}
26322632

2633-
{
2634-
esd->innerSeqSz = SetSequenceEx(totalSz + total2Sz, esd->innerSeq,
2635-
pkcs7->encodeStream);
2636-
totalSz += esd->innerSeqSz;
2637-
if (pkcs7->encodeStream) {
2638-
totalSz += ASN_INDEF_END_SZ;
2639-
}
2633+
esd->innerSeqSz = SetSequenceEx(totalSz + total2Sz, esd->innerSeq,
2634+
pkcs7->encodeStream);
2635+
totalSz += esd->innerSeqSz;
2636+
if (pkcs7->encodeStream) {
2637+
totalSz += ASN_INDEF_END_SZ;
2638+
}
26402639

2641-
esd->outerContentSz = SetExplicit(0, totalSz + total2Sz,
2642-
esd->outerContent, pkcs7->encodeStream);
2643-
totalSz += esd->outerContentSz + signedDataOidSz;
2644-
if (pkcs7->encodeStream) {
2645-
totalSz += ASN_INDEF_END_SZ;
2646-
}
2640+
esd->outerContentSz = SetExplicit(0, totalSz + total2Sz,
2641+
esd->outerContent, pkcs7->encodeStream);
2642+
totalSz += esd->outerContentSz + signedDataOidSz;
2643+
if (pkcs7->encodeStream) {
2644+
totalSz += ASN_INDEF_END_SZ;
2645+
}
26472646

2648-
esd->outerSeqSz = SetSequenceEx(totalSz + total2Sz, esd->outerSeq,
2649-
pkcs7->encodeStream);
2650-
totalSz += esd->outerSeqSz;
2651-
if (pkcs7->encodeStream) {
2652-
totalSz += ASN_INDEF_END_SZ;
2653-
}
2647+
esd->outerSeqSz = SetSequenceEx(totalSz + total2Sz, esd->outerSeq,
2648+
pkcs7->encodeStream);
2649+
totalSz += esd->outerSeqSz;
2650+
if (pkcs7->encodeStream) {
2651+
totalSz += ASN_INDEF_END_SZ;
26542652
}
26552653

26562654
/* if using header/footer, we are not returning the content */

0 commit comments

Comments
 (0)