Skip to content

Commit c3f2456

Browse files
authored
Merge pull request #8520 from JacobBarthelmeh/pkcs7_verify_stream
PKCS7 verify and decode indefinite length support
2 parents 27ed748 + 8dd6144 commit c3f2456

File tree

7 files changed

+968
-449
lines changed

7 files changed

+968
-449
lines changed

certs/include.am

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ EXTRA_DIST += \
5353
certs/wolfssl-website-ca.pem \
5454
certs/test-degenerate.p7b \
5555
certs/test-stream-sign.p7b \
56+
certs/test-stream-dec.p7b \
5657
certs/test-ber-exp02-05-2022.p7b \
5758
certs/test-servercert.p12 \
5859
certs/test-servercert-rc2.p12 \

certs/renewcerts.sh

+5
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,11 @@ run_renewcerts(){
858858
openssl smime -sign -in ./ca-cert.pem -out test-stream-sign.p7b -signer ./ca-cert.pem -nodetach -nocerts -binary -outform DER -stream -inkey ./ca-key.pem
859859
check_result $? ""
860860

861+
echo "Creating test-stream-dec.p7b..."
862+
echo ""
863+
openssl cms -encrypt -in ca-cert.pem -recip client-cert.pem -out test-stream-dec.p7b -outform DER -stream
864+
check_result $? ""
865+
861866
echo "End of section"
862867
echo "---------------------------------------------------------------------"
863868

certs/test-stream-dec.p7b

5.95 KB
Binary file not shown.

tests/api.c

+90
Original file line numberDiff line numberDiff line change
@@ -39219,6 +39219,95 @@ static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
3921939219
#endif /* HAVE_PKCS7 && !NO_AES && HAVE_AES_CBC && !NO_AES_256 */
3922039220

3922139221

39222+
#if defined(HAVE_PKCS7) && defined(ASN_BER_TO_DER)
39223+
#define MAX_TEST_DECODE_SIZE 6000
39224+
static int test_wc_PKCS7_DecodeEnvelopedData_stream_decrypt_cb(wc_PKCS7* pkcs7,
39225+
const byte* output, word32 outputSz, void* ctx) {
39226+
WOLFSSL_BUFFER_INFO* out = (WOLFSSL_BUFFER_INFO*)ctx;
39227+
39228+
if (out == NULL) {
39229+
return -1;
39230+
}
39231+
39232+
if (outputSz + out->length > MAX_TEST_DECODE_SIZE) {
39233+
printf("Example buffer size needs increased");
39234+
}
39235+
39236+
/* printf("Decoded in %d bytes\n", outputSz);
39237+
* for (word32 z = 0; z < outputSz; z++) printf("%02X", output[z]);
39238+
* printf("\n");
39239+
*/
39240+
39241+
XMEMCPY(out->buffer + out->length, output, outputSz);
39242+
out->length += outputSz;
39243+
39244+
(void)pkcs7;
39245+
return 0;
39246+
}
39247+
#endif /* HAVE_PKCS7 && ASN_BER_TO_DER */
39248+
39249+
/*
39250+
* Testing wc_PKCS7_DecodeEnvelopedData with streaming
39251+
*/
39252+
static int test_wc_PKCS7_DecodeEnvelopedData_stream(void)
39253+
{
39254+
#if defined(HAVE_PKCS7) && defined(ASN_BER_TO_DER)
39255+
EXPECT_DECLS;
39256+
PKCS7* pkcs7 = NULL;
39257+
int ret = 0;
39258+
XFILE f = XBADFILE;
39259+
const char* testStream = "./certs/test-stream-dec.p7b";
39260+
byte testStreamBuffer[100];
39261+
size_t testStreamBufferSz = 0;
39262+
byte decodedData[MAX_TEST_DECODE_SIZE]; /* large enough to hold result of decode, which is ca-cert.pem */
39263+
WOLFSSL_BUFFER_INFO out;
39264+
39265+
out.length = 0;
39266+
out.buffer = decodedData;
39267+
39268+
ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
39269+
ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)client_cert_der_2048,
39270+
sizeof_client_cert_der_2048), 0);
39271+
39272+
ExpectIntEQ(wc_PKCS7_SetKey(pkcs7, (byte*)client_key_der_2048,
39273+
sizeof_client_key_der_2048), 0);
39274+
ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, NULL,
39275+
test_wc_PKCS7_DecodeEnvelopedData_stream_decrypt_cb, (void*)&out), 0);
39276+
39277+
ExpectTrue((f = XFOPEN(testStream, "rb")) != XBADFILE);
39278+
if (EXPECT_SUCCESS()) {
39279+
do {
39280+
testStreamBufferSz = XFREAD(testStreamBuffer, 1,
39281+
sizeof(testStreamBuffer), f);
39282+
if (testStreamBufferSz == 0) {
39283+
break;
39284+
}
39285+
39286+
ret = wc_PKCS7_DecodeEnvelopedData(pkcs7, testStreamBuffer,
39287+
(word32)testStreamBufferSz, NULL, 0);
39288+
if (testStreamBufferSz < sizeof(testStreamBuffer)) {
39289+
break;
39290+
}
39291+
} while (ret == WC_NO_ERR_TRACE(WC_PKCS7_WANT_READ_E));
39292+
#ifdef NO_DES3
39293+
ExpectIntEQ(ret, ALGO_ID_E);
39294+
#else
39295+
ExpectIntGT(ret, 0);
39296+
#endif
39297+
}
39298+
39299+
if (f != XBADFILE) {
39300+
XFCLOSE(f);
39301+
f = XBADFILE;
39302+
}
39303+
39304+
wc_PKCS7_Free(pkcs7);
39305+
return EXPECT_RESULT();
39306+
#else
39307+
return TEST_SKIPPED;
39308+
#endif
39309+
} /* END test_wc_PKCS7_DecodeEnvelopedData_stream() */
39310+
3922239311
/*
3922339312
* Testing wc_PKCS7_EncodeEnvelopedData()
3922439313
*/
@@ -89473,6 +89562,7 @@ TEST_CASE testCases[] = {
8947389562
TEST_DECL(test_wc_PKCS7_EncodeSignedData_ex),
8947489563
TEST_DECL(test_wc_PKCS7_VerifySignedData_RSA),
8947589564
TEST_DECL(test_wc_PKCS7_VerifySignedData_ECC),
89565+
TEST_DECL(test_wc_PKCS7_DecodeEnvelopedData_stream),
8947689566
TEST_DECL(test_wc_PKCS7_EncodeDecodeEnvelopedData),
8947789567
TEST_DECL(test_wc_PKCS7_EncodeEncryptedData),
8947889568
TEST_DECL(test_wc_PKCS7_Degenerate),

0 commit comments

Comments
 (0)