@@ -16788,3 +16788,165 @@ int test_mldsa_pkcs8(void)
1678816788#endif
1678916789 return EXPECT_RESULT();
1679016790}
16791+
16792+ int test_mldsa_pkcs12(void)
16793+ {
16794+ EXPECT_DECLS;
16795+ #if !defined(NO_ASN) && defined(HAVE_PKCS12) && \
16796+ defined(HAVE_DILITHIUM) && !defined(NO_TLS) && \
16797+ !defined(NO_PWDBASED) && !defined(NO_HMAC) && \
16798+ !defined(NO_CERTS) && !defined(NO_DES3) && \
16799+ (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
16800+ defined(WOLFSSL_CERT_GEN)
16801+
16802+ WOLFSSL_CTX* ctx = NULL;
16803+ word32 i;
16804+ byte* inKey = NULL;
16805+ byte* inCert = NULL;
16806+ const word32 inKeyHeaderSz = 4;
16807+ const word32 inKeyMaxSz = inKeyHeaderSz + DILITHIUM_MAX_PRV_KEY_SIZE;
16808+ const word32 certConstSz = 412;
16809+ const word32 inCertMaxSz =
16810+ certConstSz + DILITHIUM_MAX_SIG_SIZE + DILITHIUM_MAX_PUB_KEY_SIZE;
16811+ const word32 pkcs8HeaderSz = 24;
16812+ WC_RNG rng;
16813+ dilithium_key mldsa_key;
16814+ char pkcs12Passwd[] = "mldsa";
16815+
16816+ struct {
16817+ int enc;
16818+ int wcId;
16819+ int oidSum;
16820+ int keySz;
16821+ int sigType;
16822+ int keyType;
16823+ } test_variant[] = {
16824+ {PBE_SHA1_DES3, WC_ML_DSA_44, ML_DSA_LEVEL2k,
16825+ ML_DSA_LEVEL2_PRV_KEY_SIZE, CTC_ML_DSA_LEVEL2, ML_DSA_LEVEL2_TYPE},
16826+ {PBE_SHA1_DES3, WC_ML_DSA_65, ML_DSA_LEVEL3k,
16827+ ML_DSA_LEVEL3_PRV_KEY_SIZE, CTC_ML_DSA_LEVEL3, ML_DSA_LEVEL3_TYPE},
16828+ {PBE_SHA1_DES3, WC_ML_DSA_87, ML_DSA_LEVEL5k,
16829+ ML_DSA_LEVEL5_PRV_KEY_SIZE, CTC_ML_DSA_LEVEL5, ML_DSA_LEVEL5_TYPE},
16830+ {-1, WC_ML_DSA_44, ML_DSA_LEVEL2k,
16831+ ML_DSA_LEVEL2_PRV_KEY_SIZE, CTC_ML_DSA_LEVEL2, ML_DSA_LEVEL2_TYPE},
16832+ {-1, WC_ML_DSA_65, ML_DSA_LEVEL3k,
16833+ ML_DSA_LEVEL3_PRV_KEY_SIZE, CTC_ML_DSA_LEVEL3, ML_DSA_LEVEL3_TYPE},
16834+ {-1, WC_ML_DSA_87, ML_DSA_LEVEL5k,
16835+ ML_DSA_LEVEL5_PRV_KEY_SIZE, CTC_ML_DSA_LEVEL5, ML_DSA_LEVEL5_TYPE},
16836+ };
16837+
16838+ ExpectNotNull(inKey = (byte*) XMALLOC(inKeyMaxSz, NULL,
16839+ DYNAMIC_TYPE_TMP_BUFFER));
16840+ ExpectNotNull(inCert = (byte*) XMALLOC(inCertMaxSz, NULL,
16841+ DYNAMIC_TYPE_TMP_BUFFER));
16842+
16843+ #ifndef NO_WOLFSSL_SERVER
16844+ ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
16845+ #else
16846+ ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
16847+ #endif /* NO_WOLFSSL_SERVER */
16848+
16849+ ExpectIntEQ(wc_InitRng(&rng), 0);
16850+ ExpectIntEQ(wc_dilithium_init(&mldsa_key), 0);
16851+
16852+ for (i = 0; i < sizeof(test_variant) / sizeof(test_variant[0]); ++i) {
16853+ WC_PKCS12* pkcs12Export = NULL;
16854+ WC_PKCS12* pkcs12Import = NULL;
16855+ byte* pkcs12Der = NULL;
16856+ byte* outKey = NULL;
16857+ byte* outCert = NULL;
16858+ word32 inKeySz = 0;
16859+ word32 inCertSz = 0;
16860+ word32 pkcs12DerSz = 0;
16861+ word32 outKeySz = 0;
16862+ word32 outCertSz = 0;
16863+ Cert cert;
16864+ word32 size;
16865+
16866+ if (EXPECT_FAIL())
16867+ break;
16868+
16869+ /* Create a key for wc_PKCS12_create() */
16870+ inKeySz = 0;
16871+ inKey[0] = 0x04; /* ASN.1 OCTET STRING */
16872+ inKey[1] = 0x82; /* 2 bytes length field */
16873+ inKey[2] = (test_variant[i].keySz >> 8) & 0xff; /* MSB of the length */
16874+ inKey[3] = test_variant[i].keySz & 0xff; /* LSB of the length */
16875+ inKeySz += inKeyHeaderSz;
16876+ ExpectIntEQ(wc_dilithium_set_level(&mldsa_key, test_variant[i].wcId),
16877+ 0);
16878+ ExpectIntEQ(wc_dilithium_make_key(&mldsa_key, &rng), 0);
16879+ size = inKeyMaxSz - inKeySz;
16880+ ExpectIntEQ(wc_dilithium_export_private(&mldsa_key, inKey + inKeySz,
16881+ &size), 0);
16882+ inKeySz += size;
16883+ size = inKeyMaxSz - inKeySz;
16884+ ExpectIntEQ(wc_dilithium_export_public(&mldsa_key, inKey + inKeySz,
16885+ &size), 0);
16886+ inKeySz += size;
16887+
16888+ /* Create a certificate for wc_PKCS12_create() */
16889+ ExpectIntEQ(wc_InitCert(&cert), 0);
16890+ XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
16891+ XSTRNCPY(cert.subject.state, "MT", CTC_NAME_SIZE);
16892+ XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
16893+ XSTRNCPY(cert.subject.org, "wolfSSL", CTC_NAME_SIZE);
16894+ XSTRNCPY(cert.subject.unit, "Engineering", CTC_NAME_SIZE);
16895+ XSTRNCPY(cert.subject.commonName, "www.wolfssl.com", CTC_NAME_SIZE);
16896+ XSTRNCPY(cert.subject.email, "root@wolfssl.com", CTC_NAME_SIZE);
16897+ XSTRNCPY((char*)cert.beforeDate, "\x18\x0f""20250101000000Z",
16898+ CTC_DATE_SIZE);
16899+ cert.beforeDateSz = 17;
16900+ XSTRNCPY((char*)cert.afterDate, "\x18\x0f""20493112115959Z",
16901+ CTC_DATE_SIZE);
16902+ cert.afterDateSz = 17;
16903+ cert.selfSigned = 1;
16904+ cert.sigType = test_variant[i].sigType;
16905+ cert.isCA = 0;
16906+ ExpectIntGE(inCertSz = wc_MakeCert_ex(&cert, inCert, inCertMaxSz,
16907+ test_variant[i].keyType, &mldsa_key, &rng), 0);
16908+ ExpectIntGE(inCertSz = wc_SignCert_ex(cert.bodySz, cert.sigType, inCert,
16909+ inCertMaxSz, test_variant[i].keyType, &mldsa_key, &rng), 0);
16910+
16911+ ExpectNotNull(pkcs12Export = wc_PKCS12_create(pkcs12Passwd,
16912+ sizeof(pkcs12Passwd) - 1,
16913+ (char*) "friendlyName" /* not used currently */,
16914+ (byte*) inKey, inKeySz, (byte*) inCert, inCertSz,
16915+ NULL, test_variant[i].enc, test_variant[i].enc, 100, 100,
16916+ 0 /* not used currently */, NULL));
16917+ pkcs12Der = NULL;
16918+ ExpectIntGE((pkcs12DerSz = wc_i2d_PKCS12(pkcs12Export, &pkcs12Der,
16919+ NULL)), 0);
16920+
16921+ ExpectNotNull(pkcs12Import = wc_PKCS12_new_ex(NULL));
16922+ ExpectIntGE(wc_d2i_PKCS12(pkcs12Der, pkcs12DerSz, pkcs12Import), 0);
16923+ ExpectIntEQ(wc_PKCS12_parse_ex(pkcs12Import, pkcs12Passwd, &outKey,
16924+ &outKeySz,
16925+ &outCert, &outCertSz, NULL, 1), 0);
16926+ ExpectIntGT(outKeySz, 0);
16927+ ExpectIntGT(outCertSz, 0);
16928+ ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, outKey, outKeySz,
16929+ WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
16930+ ExpectIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx, outCert, outCertSz,
16931+ WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
16932+
16933+ ExpectIntEQ(inKeySz, outKeySz - pkcs8HeaderSz);
16934+ ExpectIntEQ(XMEMCMP(inKey, outKey + pkcs8HeaderSz, inKeySz), 0);
16935+ ExpectIntEQ(inCertSz, outCertSz);
16936+ ExpectIntEQ(XMEMCMP(inCert, outCert, inCertSz), 0);
16937+
16938+ XFREE(outKey, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
16939+ XFREE(outCert, NULL, DYNAMIC_TYPE_PKCS);
16940+ wc_PKCS12_free(pkcs12Import);
16941+ XFREE(pkcs12Der, NULL, DYNAMIC_TYPE_PKCS);
16942+ wc_PKCS12_free(pkcs12Export);
16943+ }
16944+
16945+ wc_dilithium_free(&mldsa_key);
16946+ ExpectIntEQ(wc_FreeRng(&rng), 0);
16947+ wolfSSL_CTX_free(ctx);
16948+ XFREE(inCert, NULL, DYNAMIC_TYPE_TMP_BUFFER);
16949+ XFREE(inKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
16950+ #endif
16951+ return EXPECT_RESULT();
16952+ }
0 commit comments