Skip to content

Commit 4f21117

Browse files
committed
tests: Add PKCS#7 verification interoperability test
Intention of this test is to verify PKCS#7 / CMS signatures produced by third party tools. Usage: openssl cms -sign -in <(echo -n "openssl cms interop test message") \ -signer certs/client-cert.pem -inkey certs/client-key.pem \ -md sha256 -nodetach -outform DER -out /tmp/cms.der openssl smime -sign -in <(echo -n "openssl smime interop test message") \ -signer certs/client-cert.pem -inkey certs/client-key.pem \ -md sha256 -binary -nodetach -outform DER -out /tmp/smime.der ./configure --enable-pkcs7 --enable-certext --enable-examples \ --with-pkcs7-test-signed-data=/tmp/cms.der,/tmp/smime.der make -j$(nproc) tests/unit.test -test_wc_PKCS7_VerifySignedData_interop
1 parent b7e7e75 commit 4f21117

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

configure.ac

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10480,6 +10480,17 @@ if test "x$ENABLED_OLD_EXTDATA_FMT" = "xyes"; then
1048010480
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_OLD_EXTDATA_FMT"
1048110481
fi
1048210482
10483+
# Optional PKCS#7 SignedData interoperability test
10484+
AC_ARG_WITH([pkcs7-test-signed-data],
10485+
[AS_HELP_STRING([--with-pkcs7-test-signed-data=FILE[,FILE,...]],
10486+
[External test data for optional PKCS#7 interoperability test])],
10487+
[
10488+
AC_DEFINE([HAVE_PKCS7_TEST_SIGNED_DATA_FILES], [1],
10489+
[Define to enable PKCS#7 SignedData interoperability test])
10490+
AC_DEFINE_UNQUOTED([PKCS7_TEST_SIGNED_DATA_FILES], ["$withval"],
10491+
[Comma-separated list of DER-encoded CMS SignedData files for interop test])
10492+
])
10493+
1048310494
# determine if we have key validation mechanism
1048410495
if test "x$ENABLED_ECC" != "xno" || test "x$ENABLED_RSA" = "xyes"
1048510496
then

tests/api/test_pkcs7.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4856,3 +4856,70 @@ int test_wc_PKCS7_VerifySignedData_PKCS7ContentSeq(void)
48564856
return EXPECT_RESULT();
48574857
}
48584858

4859+
#if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_TEST_SIGNED_DATA_FILES) && \
4860+
!defined(NO_FILESYSTEM)
4861+
static int pkcs7_verify_one_signed_data_der_file(const char* path)
4862+
{
4863+
EXPECT_DECLS;
4864+
XFILE f = XBADFILE;
4865+
long signedDataSz;
4866+
byte* signedData = NULL;
4867+
PKCS7* pkcs7 = NULL;
4868+
4869+
ExpectTrue((f = XFOPEN(path, "rb")) != XBADFILE);
4870+
ExpectIntEQ(XFSEEK(f, 0, XSEEK_END), 0);
4871+
ExpectIntGT(signedDataSz = XFTELL(f), 0);
4872+
ExpectIntEQ(XFSEEK(f, 0, XSEEK_SET), 0);
4873+
4874+
ExpectNotNull(signedData = (byte*)XMALLOC((word32)signedDataSz, NULL,
4875+
DYNAMIC_TYPE_TMP_BUFFER));
4876+
ExpectIntEQ((long)XFREAD(signedData, 1, (word32)signedDataSz, f),
4877+
signedDataSz);
4878+
if (f != XBADFILE)
4879+
XFCLOSE(f);
4880+
4881+
ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID));
4882+
ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
4883+
4884+
/* test signature verification and message content */
4885+
ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7,
4886+
signedData,
4887+
(word32)signedDataSz), 0);
4888+
ExpectNotNull(pkcs7->contentDynamic);
4889+
4890+
XFREE(signedData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
4891+
wc_PKCS7_Free(pkcs7);
4892+
return EXPECT_RESULT();
4893+
}
4894+
4895+
/* Verify CMS SignedData DER files created with third party tools.
4896+
*
4897+
* This test verifies signatures created with external tools can be verified by
4898+
* WolfSSL. Standards allow some variance when creating CMS signatures. The
4899+
* test is intended to catch incompatibility from subtle differences in
4900+
* implementation.
4901+
*
4902+
* DER files are supplied at configure time:
4903+
* configure --with-pkcs7-test-signed-data=openssl_cms.der,openssl_smime.der
4904+
*/
4905+
int test_wc_PKCS7_VerifySignedData_interop(void)
4906+
{
4907+
EXPECT_DECLS;
4908+
char pathsTokenBuf[FOURK_BUF];
4909+
char* nextPath;
4910+
char* tokState = NULL;
4911+
4912+
ExpectTrue(XSTRLEN(PKCS7_TEST_SIGNED_DATA_FILES) < sizeof(pathsTokenBuf));
4913+
XSTRNCPY(pathsTokenBuf, PKCS7_TEST_SIGNED_DATA_FILES, sizeof(pathsTokenBuf));
4914+
4915+
nextPath = XSTRTOK(pathsTokenBuf, ",", &tokState);
4916+
while (nextPath != NULL) {
4917+
Expect(pkcs7_verify_one_signed_data_der_file(nextPath) == TEST_SUCCESS,
4918+
("%s signature verification succeeds", nextPath),
4919+
("%s signature verification failed", nextPath));
4920+
nextPath = XSTRTOK(NULL, ",", &tokState);
4921+
}
4922+
return EXPECT_RESULT();
4923+
}
4924+
#endif /* HAVE_PKCS7 && HAVE_PKCS7_TEST_SIGNED_DATA_FILES && !NO_FILESYSTEM */
4925+

tests/api/test_pkcs7.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ int test_wc_PKCS7_SetOriDecryptCtx(void);
5858
int test_wc_PKCS7_DecodeCompressedData(void);
5959
int test_wc_PKCS7_DecodeEnvelopedData_multiple_recipients(void);
6060
int test_wc_PKCS7_VerifySignedData_PKCS7ContentSeq(void);
61+
#if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_TEST_SIGNED_DATA_FILES) && \
62+
!defined(NO_FILESYSTEM)
63+
int test_wc_PKCS7_VerifySignedData_interop(void);
64+
#define TEST_PKCS7_INTEROP_VERIFY_SD_DECL \
65+
TEST_DECL_GROUP("pkcs7_sd", test_wc_PKCS7_VerifySignedData_interop)
66+
#else
67+
#define TEST_PKCS7_INTEROP_VERIFY_SD_DECL
68+
#endif
6169

6270

6371
#define TEST_PKCS7_DECLS \
@@ -92,7 +100,8 @@ int test_wc_PKCS7_VerifySignedData_PKCS7ContentSeq(void);
92100
TEST_DECL_GROUP("pkcs7_sd", test_wc_PKCS7_Degenerate), \
93101
TEST_DECL_GROUP("pkcs7_sd", test_wc_PKCS7_BER), \
94102
TEST_DECL_GROUP("pkcs7_sd", test_wc_PKCS7_NoDefaultSignedAttribs), \
95-
TEST_DECL_GROUP("pkcs7_sd", test_wc_PKCS7_VerifySignedData_PKCS7ContentSeq)
103+
TEST_DECL_GROUP("pkcs7_sd", test_wc_PKCS7_VerifySignedData_PKCS7ContentSeq), \
104+
TEST_PKCS7_INTEROP_VERIFY_SD_DECL
96105

97106
#define TEST_PKCS7_ENCRYPTED_DATA_DECLS \
98107
TEST_DECL_GROUP("pkcs7_ed", test_wc_PKCS7_DecodeEnvelopedData_stream), \

0 commit comments

Comments
 (0)