Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pkcs12 tests for wolfcrypt #7502

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions wolfcrypt/src/pkcs12.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,21 @@ WC_PKCS12* wc_PKCS12_new(void)
return pkcs12;
}

WC_PKCS12* wc_PKCS12_new_ex(void* heap, int devId)
{
WC_PKCS12* pkcs12 = (WC_PKCS12*)XMALLOC(sizeof(WC_PKCS12),
heap, DYNAMIC_TYPE_PKCS);
if (pkcs12 == NULL) {
WOLFSSL_MSG("Memory issue when creating WC_PKCS12 struct");
return NULL;
}

XMEMSET(pkcs12, 0, sizeof(WC_PKCS12));
pkcs12->heap = heap;
(void)devId;

ColtonWilley marked this conversation as resolved.
Show resolved Hide resolved
return pkcs12;
}

static void freeSafe(AuthenticatedSafe* safe, void* heap)
{
Expand Down Expand Up @@ -1102,6 +1117,12 @@ void wc_FreeCertList(WC_DerCertList* list, void* heap)
(void)heap;
}

/* Publicly exposed wrapper function for wc_FreeCertList */
void wc_PKCS12_FreeCertList(WC_DerCertList* list, void* heap)
{
wc_FreeCertList(list, heap);
}

static WARN_UNUSED_RESULT int freeDecCertList(WC_DerCertList** list,
byte** pkey, word32* pkeySz, byte** cert, word32* certSz, void* heap)
{
Expand Down
229 changes: 227 additions & 2 deletions wolfcrypt/test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,9 @@ const byte const_byte_array[] = "A+Gd\0\0\0";
#include <wolfssl/wolfcrypt/pwdbased.h>
#include <wolfssl/wolfcrypt/ripemd.h>
#include <wolfssl/wolfcrypt/error-crypt.h>
#ifdef HAVE_PKCS12
#include <wolfssl/wolfcrypt/pkcs12.h>
#endif
#ifdef HAVE_ECC
#include <wolfssl/wolfcrypt/ecc.h>
#endif
Expand Down Expand Up @@ -595,7 +598,13 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_evpSig_test(void);
#endif

WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pbkdf1_test(void);
#if defined(HAVE_PKCS12)
WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs12_kdf_test(void);
#if !defined(NO_AES) && !defined(NO_ASN) && !defined(NO_PWDBASED) && \
!defined(NO_RSA) && !(defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_STATIC_MEMORY))
WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs12_test(void);
#endif
#endif
#if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pbkdf2_test(void);
#endif
Expand Down Expand Up @@ -1672,6 +1681,17 @@ options: [-s max_relative_stack_bytes] [-m max_relative_heap_memory_bytes]\n\
PRIVATE_KEY_LOCK();
#endif

#if defined(HAVE_PKCS12) && !defined(NO_AES) && !defined(NO_ASN) && \
!defined(NO_PWDBASED) && !defined(NO_RSA) && \
!(defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_STATIC_MEMORY))
PRIVATE_KEY_UNLOCK();
if ( (ret = pkcs12_test()) != 0)
TEST_FAIL("PKCS12 test failed!\n", ret);
else
TEST_PASS("PKCS12 test passed!\n");
PRIVATE_KEY_LOCK();
#endif

#if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
if ( (ret = openssl_test()) != 0)
TEST_FAIL("OPENSSL test failed!\n", ret);
Expand Down Expand Up @@ -24703,7 +24723,212 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t scrypt_test(void)
#endif

#ifdef HAVE_PKCS12

/* Password is "cryptography" */
const byte aes256cbc_p12[] = {
0x30, 0x82, 0x03, 0xb0, 0x02, 0x01, 0x03, 0x30, 0x82, 0x03, 0x76, 0x06,
0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x82,
0x03, 0x67, 0x04, 0x82, 0x03, 0x63, 0x30, 0x82, 0x03, 0x5f, 0x30, 0x82,
0x02, 0x24, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07,
0x06, 0xa0, 0x82, 0x02, 0x15, 0x30, 0x82, 0x02, 0x11, 0x02, 0x01, 0x00,
0x30, 0x82, 0x02, 0x0a, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
0x01, 0x07, 0x01, 0x30, 0x49, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
0x0d, 0x01, 0x05, 0x0d, 0x30, 0x3c, 0x30, 0x1b, 0x06, 0x09, 0x2a, 0x86,
0x48, 0x86, 0xf7, 0x0d, 0x01, 0x05, 0x0c, 0x30, 0x0e, 0x04, 0x08, 0x28,
0xbc, 0xac, 0xe7, 0xfa, 0x8c, 0x28, 0xbd, 0x02, 0x02, 0x08, 0x00, 0x30,
0x1d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x01, 0x2a,
0x04, 0x10, 0x27, 0xbd, 0x21, 0xa3, 0xbb, 0x35, 0xcd, 0xd4, 0x11, 0xa2,
0x86, 0xa9, 0x68, 0xdc, 0xd9, 0xb2, 0x80, 0x82, 0x01, 0xb0, 0xaa, 0x7a,
0xb2, 0xb9, 0xb1, 0x9a, 0x06, 0x63, 0x68, 0xa3, 0x7b, 0x71, 0x7b, 0x59,
0x79, 0x27, 0xb1, 0xce, 0xde, 0x9a, 0xed, 0xa9, 0x50, 0xe7, 0x04, 0xd5,
0xc1, 0xec, 0x28, 0x29, 0x64, 0x1e, 0xab, 0xf3, 0xd7, 0x59, 0xdd, 0xf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xf5, 0x5e,
0x05, 0x24, 0x1f, 0xb4, 0x77, 0x95, 0x9e, 0xe2, 0x75, 0x3f, 0xce, 0xef,
0xc6, 0xa9, 0x06, 0x08, 0xec, 0xa0, 0x76, 0x41, 0x2a, 0x59, 0x30, 0x82,
0x01, 0x33, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07,
0x01, 0xa0, 0x82, 0x01, 0x24, 0x04, 0x82, 0x01, 0x20, 0x30, 0x82, 0x01,
0x1c, 0x30, 0x82, 0x01, 0x18, 0x06, 0x0b, 0x2a, 0x86, 0x48, 0x86, 0xf7,
0x0d, 0x01, 0x0c, 0x0a, 0x01, 0x02, 0xa0, 0x81, 0xe1, 0x30, 0x81, 0xde,
0x30, 0x49, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x05,
0x0d, 0x30, 0x3c, 0x30, 0x1b, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
0x0d, 0x01, 0x05, 0x0c, 0x30, 0x0e, 0x04, 0x08, 0x76, 0x53, 0x9a, 0xcb,
0x67, 0x1e, 0x15, 0xbd, 0x02, 0x02, 0x08, 0x00, 0x30, 0x1d, 0x06, 0x09,
0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x01, 0x2a, 0x04, 0x10, 0xdb,
0x6e, 0x66, 0x0c, 0xa7, 0xc9, 0x82, 0x22, 0xd8, 0xb3, 0xe3, 0x19, 0x8c,
0xe5, 0x74, 0xa0, 0x04, 0x81, 0x90, 0x38, 0xf9, 0x40, 0x5f, 0x3a, 0x5e,
0xf1, 0xc8, 0x2c, 0x0c, 0x2e, 0x04, 0x83, 0x0b, 0x26, 0xd0, 0x79, 0xeb,
0xad, 0xc1, 0xf4, 0x0b, 0x7c, 0x26, 0x32, 0x9b, 0xa2, 0x55, 0x4c, 0xb8,
0xee, 0xed, 0x47, 0xb7, 0xe8, 0x7a, 0x94, 0x86, 0x73, 0x68, 0xe6, 0x54,
0x82, 0x3d, 0x68, 0x22, 0x1f, 0x7f, 0x85, 0x9d, 0x7c, 0xa6, 0x26, 0x44,
0x03, 0xb8, 0xfa, 0xa9, 0x80, 0xf4, 0xcf, 0x99, 0xd7, 0x24, 0x49, 0x14,
0x5d, 0x04, 0xa1, 0xee, 0xf8, 0x50, 0xc9, 0x50, 0x69, 0x8f, 0x13, 0xc4,
0x41, 0xb9, 0x87, 0xd9, 0x7c, 0xe1, 0xef, 0xaa, 0x71, 0x49, 0x6a, 0x9e,
0xb2, 0xec, 0x06, 0x47, 0xba, 0x6f, 0x50, 0x5a, 0xb1, 0xa7, 0x48, 0xfb,
0x00, 0x23, 0x59, 0x72, 0x55, 0x3f, 0x5b, 0x24, 0x6f, 0x9d, 0xc8, 0x26,
0xd6, 0x4c, 0x12, 0x52, 0x71, 0x51, 0xd5, 0x24, 0x8a, 0x48, 0xfc, 0xa0,
0x2e, 0x1a, 0x6c, 0x21, 0xc8, 0xd1, 0xeb, 0x7e, 0x54, 0xb5, 0x90, 0xbf,
0xf3, 0xee, 0xd2, 0x84, 0x53, 0x10, 0x31, 0x25, 0x30, 0x23, 0x06, 0x09,
0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x15, 0x31, 0x16, 0x04,
0x14, 0x25, 0x34, 0xf6, 0x3c, 0x8f, 0x94, 0x8c, 0xe5, 0x48, 0x27, 0xf6,
0x70, 0xd9, 0x24, 0xd5, 0xfc, 0x81, 0xfa, 0xa1, 0x2c, 0x30, 0x31, 0x30,
0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00,
0x04, 0x14, 0x5e, 0x04, 0xa6, 0x13, 0xcd, 0x10, 0xee, 0x49, 0x46, 0x41,
0xb1, 0xc3, 0x7a, 0x46, 0xa1, 0x53, 0x36, 0x14, 0x2e, 0xeb, 0x04, 0x08,
ColtonWilley marked this conversation as resolved.
Show resolved Hide resolved
0x5d, 0x7f, 0x7d, 0x0b, 0xea, 0xf2, 0x9d, 0xdf, 0x02, 0x02, 0x08, 0x00
};
const int aes256cbc_p12_len = (int)sizeof(aes256cbc_p12);

#if !defined(NO_AES) && !defined(NO_ASN) && !defined(NO_PWDBASED) && \
!defined(NO_RSA) && !(defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_STATIC_MEMORY))
WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs12_test(void)
{
wc_test_ret_t ret;
WC_PKCS12 *pkcs12 = NULL;
byte *key = NULL;
word32 keyLen = 0;
ColtonWilley marked this conversation as resolved.
Show resolved Hide resolved
byte *cert = NULL;
word32 certLen = 0;
WC_DerCertList *certList = NULL;
const char *passwd = "cryptography";
/* RSA key encoded within aes256cbc_p12 */
WOLFSSL_SMALL_STACK_STATIC const byte expectedKey[] = {
0x30, 0x6B, 0x02, 0x01, 0x01, 0x04, 0x20, 0x03, 0xC6, 0x6A, 0xCF,
0x9B, 0xCB, 0x79, 0x1D, 0x1E, 0x3D, 0x95, 0x1E, 0x8C, 0x17, 0xDD,
0xC8, 0xC9, 0xA7, 0x9C, 0x8E, 0x14, 0xE6, 0xE0, 0x09, 0x02, 0x95,
0x93, 0xB0, 0xC9, 0xFF, 0x46, 0xE5, 0xA1, 0x44, 0x03, 0x42, 0x00,
0x04, 0x18, 0xFF, 0xCF, 0xBB, 0xF9, 0x39, 0xB8, 0xF5, 0xDD, 0xC3,
0xEE, 0xC0, 0x40, 0x8B, 0x06, 0x75, 0x06, 0xAB, 0x4F, 0xCD, 0xD8,
0x2C, 0x52, 0x24, 0x4E, 0x1F, 0xE0, 0x10, 0x46, 0x67, 0xB5, 0x5F,
0x15, 0xB9, 0x62, 0xBD, 0x3B, 0xCF, 0x0C, 0x6F, 0xBE, 0x1A, 0xF7,
ColtonWilley marked this conversation as resolved.
Show resolved Hide resolved
0xB4, 0xA1, 0x0F, 0xB4, 0xB9, 0xCB, 0x6E, 0x86, 0xB3, 0x50, 0xF9,
0x6C, 0x51, 0xBF, 0xC1, 0x82, 0xD7, 0xBE, 0xC5, 0xF9, 0x05,
};
word32 expectedKeyLen = (word32)sizeof(expectedKey);
/* Cert encoded within aes256cbc_p12 */
WOLFSSL_SMALL_STACK_STATIC const byte expectedCert[] = {
0x30, 0x82, 0x01, 0x51, 0x30, 0x81, 0xF7, 0xA0, 0x03, 0x02, 0x01,
0x02, 0x02, 0x02, 0x03, 0x09, 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86,
0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30, 0x27, 0x31, 0x0B, 0x30,
0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x63,
0x72, 0x79, 0x70, 0x74, 0x6F, 0x67, 0x72, 0x61, 0x70, 0x68, 0x79,
0x20, 0x43, 0x41, 0x30, 0x1E, 0x17, 0x0D, 0x31, 0x37, 0x30, 0x31,
0x30, 0x31, 0x31, 0x32, 0x30, 0x31, 0x30, 0x30, 0x5A, 0x17, 0x0D,
0x33, 0x38, 0x31, 0x32, 0x33, 0x31, 0x30, 0x38, 0x33, 0x30, 0x30,
0x30, 0x5A, 0x30, 0x27, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x18, 0x30, 0x16, 0x06,
0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x63, 0x72, 0x79, 0x70, 0x74,
0x6F, 0x67, 0x72, 0x61, 0x70, 0x68, 0x79, 0x20, 0x43, 0x41, 0x30,
0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02,
0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07,
0x03, 0x42, 0x00, 0x04, 0x18, 0xFF, 0xCF, 0xBB, 0xF9, 0x39, 0xB8,
0xF5, 0xDD, 0xC3, 0xEE, 0xC0, 0x40, 0x8B, 0x06, 0x75, 0x06, 0xAB,
0x4F, 0xCD, 0xD8, 0x2C, 0x52, 0x24, 0x4E, 0x1F, 0xE0, 0x10, 0x46,
0x67, 0xB5, 0x5F, 0x15, 0xB9, 0x62, 0xBD, 0x3B, 0xCF, 0x0C, 0x6F,
0xBE, 0x1A, 0xF7, 0xB4, 0xA1, 0x0F, 0xB4, 0xB9, 0xCB, 0x6E, 0x86,
0xB3, 0x50, 0xF9, 0x6C, 0x51, 0xBF, 0xC1, 0x82, 0xD7, 0xBE, 0xC5,
0xF9, 0x05, 0xA3, 0x13, 0x30, 0x11, 0x30, 0x0F, 0x06, 0x03, 0x55,
0x1D, 0x13, 0x01, 0x01, 0xFF, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01,
0xFF, 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04,
0x03, 0x02, 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xD1,
0x12, 0xEF, 0x8D, 0x97, 0x5A, 0x6E, 0xB8, 0xB6, 0x41, 0xA7, 0xCF,
0xC0, 0xE7, 0xA4, 0x6E, 0xAE, 0xDA, 0x51, 0xE4, 0x64, 0x54, 0x2B,
0xDE, 0x86, 0x95, 0xBC, 0xF7, 0x1E, 0x9A, 0xF9, 0x5B, 0x02, 0x21,
0x00, 0xD1, 0x61, 0x86, 0xCE, 0x66, 0x31, 0xE4, 0x2F, 0x54, 0xBD,
ColtonWilley marked this conversation as resolved.
Show resolved Hide resolved
0xF5, 0xC8, 0x2B, 0xB3, 0x44, 0xCE, 0x24, 0xF8, 0xA5, 0x0B, 0x72,
0x11, 0x21, 0x34, 0xB9, 0x15, 0x4A, 0x5F, 0x0E, 0x27, 0x32, 0xA9,
};
word32 expectedCertLen = (word32)sizeof(expectedCert);
WOLFSSL_ENTER("pkcs12_test");

pkcs12 = wc_PKCS12_new_ex(HEAP_HINT, devId);
if (pkcs12 == NULL)
return WC_TEST_RET_ENC_NC;

ret = wc_d2i_PKCS12((const byte *)aes256cbc_p12,
(word32)aes256cbc_p12_len, pkcs12);
if (ret < 0) {
ret = WC_TEST_RET_ENC_EC(ret);
goto exit;
}

/* Negative test cases */
ret = wc_PKCS12_parse(pkcs12, (const char *)"wrong", &key, &keyLen,
&cert, &certLen, &certList);
if (ret >= 0) {
ret = WC_TEST_RET_ENC_EC(ret);
goto exit;
}

ret = wc_PKCS12_parse(NULL, (const char *)passwd, &key, &keyLen,
&cert, &certLen, &certList);
if (ret >= 0) {
ret = WC_TEST_RET_ENC_EC(ret);
goto exit;
}

/* Positive test case for parsing */
ret = wc_PKCS12_parse(pkcs12, (const char *)passwd, &key, &keyLen,
&cert, &certLen, &certList);
if (ret < 0) {
ret = WC_TEST_RET_ENC_EC(ret);
goto exit;
}

/* Ensure we parsed the values as expected */
if (keyLen != expectedKeyLen)
return WC_TEST_RET_ENC_NC;

if (XMEMCMP(key, expectedKey, keyLen) != 0)
return WC_TEST_RET_ENC_NC;

if (certLen != expectedCertLen)
return WC_TEST_RET_ENC_NC;

if (XMEMCMP(cert, expectedCert, certLen) != 0)
return WC_TEST_RET_ENC_NC;

exit:
if (pkcs12 != NULL)
wc_PKCS12_free(pkcs12);
if (certList != NULL)
wc_PKCS12_FreeCertList(certList, HEAP_HINT);

return ret;
}
#endif

WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs12_kdf_test(void)
{
WOLFSSL_SMALL_STACK_STATIC const byte passwd[] = { 0x00, 0x73, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x67,
0x00, 0x00 };
Expand All @@ -24730,7 +24955,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs12_test(void)
int kLen = 24;
int iterations = 1;
wc_test_ret_t ret;
WOLFSSL_ENTER("pkcs12_test");
WOLFSSL_ENTER("pkcs12_kdf_test");

ret = wc_PKCS12_PBKDF(derived, passwd, sizeof(passwd), salt, 8,
iterations, kLen, WC_SHA256, id);
Expand Down Expand Up @@ -24835,7 +25060,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pwdbased_test(void)
return ret;
#endif
#ifdef HAVE_PKCS12
ret = pkcs12_test();
ret = pkcs12_kdf_test();
if (ret != 0)
return ret;
#endif
Expand Down
3 changes: 2 additions & 1 deletion wolfssl/wolfcrypt/pkcs12.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ enum {
};

WOLFSSL_API WC_PKCS12* wc_PKCS12_new(void);
WOLFSSL_API WC_PKCS12* wc_PKCS12_new_ex(void* heap, int devId);
WOLFSSL_API void wc_PKCS12_free(WC_PKCS12* pkcs12);
WOLFSSL_API int wc_d2i_PKCS12(const byte* der, word32 derSz, WC_PKCS12* pkcs12);
#ifndef NO_FILESYSTEM
Expand All @@ -62,11 +63,11 @@ WOLFSSL_API WC_PKCS12* wc_PKCS12_create(char* pass, word32 passSz,
char* name, byte* key, word32 keySz, byte* cert, word32 certSz,
WC_DerCertList* ca, int nidKey, int nidCert, int iter, int macIter,
int keyType, void* heap);
WOLFSSL_API void wc_PKCS12_FreeCertList(WC_DerCertList* list, void* heap);


WOLFSSL_LOCAL int wc_PKCS12_SetHeap(WC_PKCS12* pkcs12, void* heap);
WOLFSSL_LOCAL void* wc_PKCS12_GetHeap(WC_PKCS12* pkcs12);

WOLFSSL_LOCAL void wc_FreeCertList(WC_DerCertList* list, void* heap);

#ifdef __cplusplus
Expand Down
Loading