@@ -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
0 commit comments