@@ -20047,61 +20047,127 @@ static int mockSignCb(const byte* in, word32 inLen, byte* out, word32* outLen,
2004720047static int test_wc_SignCert_cb(void)
2004820048{
2004920049 EXPECT_DECLS;
20050- #if defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC) && !defined(NO_ASN_TIME)
20051- Cert cert;
20052- byte der[FOURK_BUF];
20053- int derSize = 0;
20054- WC_RNG rng;
20055- ecc_key key;
20056- MockSignCtx signCtx;
20057- int ret;
20050+ #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
2005820051
20059- XMEMSET(&rng, 0, sizeof(WC_RNG));
20060- XMEMSET(&key, 0, sizeof(ecc_key));
20061- XMEMSET(&cert, 0, sizeof(Cert));
20062- XMEMSET(&signCtx, 0, sizeof(MockSignCtx));
20052+ #ifdef HAVE_ECC
20053+ /* Test with ECC key */
20054+ {
20055+ Cert cert;
20056+ byte der[FOURK_BUF];
20057+ int derSize = 0;
20058+ WC_RNG rng;
20059+ ecc_key key;
20060+ MockSignCtx signCtx;
20061+ int ret;
2006320062
20064- ExpectIntEQ(wc_InitRng( &rng) , 0);
20065- ExpectIntEQ(wc_ecc_init( &key) , 0);
20066- ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0 );
20067- ExpectIntEQ(wc_InitCert(&cert) , 0);
20063+ XMEMSET( &rng, 0, sizeof(WC_RNG) );
20064+ XMEMSET( &key, 0, sizeof(ecc_key) );
20065+ XMEMSET(&cert, 0, sizeof(Cert) );
20066+ XMEMSET(&signCtx , 0, sizeof(MockSignCtx) );
2006820067
20069- (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
20070- (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
20071- (void)XSTRNCPY(cert.subject.locality, "locality", CTC_NAME_SIZE);
20072- (void)XSTRNCPY(cert.subject.org, "org", CTC_NAME_SIZE);
20073- (void)XSTRNCPY(cert.subject.unit, "unit", CTC_NAME_SIZE);
20074- (void)XSTRNCPY(cert.subject.commonName, "www.example.com",
20075- CTC_NAME_SIZE);
20076- (void)XSTRNCPY(cert.subject.email, "test@example.com", CTC_NAME_SIZE);
20068+ ExpectIntEQ(wc_InitRng(&rng), 0);
20069+ ExpectIntEQ(wc_ecc_init(&key), 0);
20070+ ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
20071+ ExpectIntEQ(wc_InitCert(&cert), 0);
2007720072
20078- cert.selfSigned = 1;
20079- cert.isCA = 0;
20080- cert.sigType = CTC_SHA256wECDSA;
20073+ (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
20074+ (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
20075+ (void)XSTRNCPY(cert.subject.locality, "locality", CTC_NAME_SIZE);
20076+ (void)XSTRNCPY(cert.subject.org, "org", CTC_NAME_SIZE);
20077+ (void)XSTRNCPY(cert.subject.unit, "unit", CTC_NAME_SIZE);
20078+ (void)XSTRNCPY(cert.subject.commonName, "www.example.com",
20079+ CTC_NAME_SIZE);
20080+ (void)XSTRNCPY(cert.subject.email, "test@example.com", CTC_NAME_SIZE);
2008120081
20082- /* Make cert body */
20083- ExpectIntGT(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
20082+ cert.selfSigned = 1;
20083+ cert.isCA = 0;
20084+ cert.sigType = CTC_SHA256wECDSA;
2008420085
20085- /* Setup signing context with key and RNG */
20086- signCtx.key = &key;
20087- signCtx.rng = &rng;
20086+ /* Make cert body */
20087+ ExpectIntGT(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
2008820088
20089- /* Sign using callback API */
20090- ExpectIntGT(derSize = wc_SignCert_cb(cert.bodySz, cert.sigType, der,
20091- FOURK_BUF, ECC_TYPE, mockSignCb, &signCtx, & rng), 0) ;
20089+ /* Setup signing context with key and RNG */
20090+ signCtx.key = &key;
20091+ signCtx.rng = & rng;
2009220092
20093- /* Verify the certificate was created properly */
20094- ExpectIntGT(derSize, 0);
20093+ /* Sign using callback API */
20094+ ExpectIntGT(derSize = wc_SignCert_cb(cert.bodySz, cert.sigType, der,
20095+ FOURK_BUF, ECC_TYPE, mockSignCb, &signCtx, &rng), 0);
2009520096
20096- /* Test error cases */
20097- ExpectIntEQ(wc_SignCert_cb(cert.bodySz, cert.sigType, der,
20098- FOURK_BUF, ECC_TYPE, NULL, &signCtx, &rng), BAD_FUNC_ARG);
20097+ /* Verify the certificate was created properly */
20098+ ExpectIntGT(derSize, 0);
2009920099
20100- ret = wc_ecc_free(&key);
20101- ExpectIntEQ(ret, 0);
20102- ret = wc_FreeRng(&rng);
20103- ExpectIntEQ(ret, 0);
20104- #endif
20100+ /* Test error cases */
20101+ ExpectIntEQ(wc_SignCert_cb(cert.bodySz, cert.sigType, der,
20102+ FOURK_BUF, ECC_TYPE, NULL, &signCtx, &rng), BAD_FUNC_ARG);
20103+
20104+ ret = wc_ecc_free(&key);
20105+ ExpectIntEQ(ret, 0);
20106+ ret = wc_FreeRng(&rng);
20107+ ExpectIntEQ(ret, 0);
20108+ }
20109+ #endif /* HAVE_ECC */
20110+
20111+ #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
20112+ /* Test with RSA key */
20113+ {
20114+ Cert cert;
20115+ byte der[FOURK_BUF];
20116+ int derSize = 0;
20117+ WC_RNG rng;
20118+ RsaKey key;
20119+ MockSignCtx signCtx;
20120+ int ret;
20121+
20122+ XMEMSET(&rng, 0, sizeof(WC_RNG));
20123+ XMEMSET(&key, 0, sizeof(RsaKey));
20124+ XMEMSET(&cert, 0, sizeof(Cert));
20125+ XMEMSET(&signCtx, 0, sizeof(MockSignCtx));
20126+
20127+ ExpectIntEQ(wc_InitRng(&rng), 0);
20128+ ExpectIntEQ(wc_InitRsaKey(&key, NULL), 0);
20129+ ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
20130+ ExpectIntEQ(wc_InitCert(&cert), 0);
20131+
20132+ (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
20133+ (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
20134+ (void)XSTRNCPY(cert.subject.locality, "locality", CTC_NAME_SIZE);
20135+ (void)XSTRNCPY(cert.subject.org, "org", CTC_NAME_SIZE);
20136+ (void)XSTRNCPY(cert.subject.unit, "unit", CTC_NAME_SIZE);
20137+ (void)XSTRNCPY(cert.subject.commonName, "www.example.com",
20138+ CTC_NAME_SIZE);
20139+ (void)XSTRNCPY(cert.subject.email, "test@example.com", CTC_NAME_SIZE);
20140+
20141+ cert.selfSigned = 1;
20142+ cert.isCA = 0;
20143+ cert.sigType = CTC_SHA256wRSA;
20144+
20145+ /* Make cert body */
20146+ ExpectIntGT(wc_MakeCert(&cert, der, FOURK_BUF, &key, NULL, &rng), 0);
20147+
20148+ /* Setup signing context with key and RNG */
20149+ signCtx.key = &key;
20150+ signCtx.rng = &rng;
20151+
20152+ /* Sign using callback API with RSA */
20153+ ExpectIntGT(derSize = wc_SignCert_cb(cert.bodySz, cert.sigType, der,
20154+ FOURK_BUF, RSA_TYPE, mockSignCb, &signCtx, &rng), 0);
20155+
20156+ /* Verify the certificate was created properly */
20157+ ExpectIntGT(derSize, 0);
20158+
20159+ /* Test error case - NULL callback */
20160+ ExpectIntEQ(wc_SignCert_cb(cert.bodySz, cert.sigType, der,
20161+ FOURK_BUF, RSA_TYPE, NULL, &signCtx, &rng), BAD_FUNC_ARG);
20162+
20163+ ret = wc_FreeRsaKey(&key);
20164+ ExpectIntEQ(ret, 0);
20165+ ret = wc_FreeRng(&rng);
20166+ ExpectIntEQ(ret, 0);
20167+ }
20168+ #endif /* !NO_RSA && WOLFSSL_KEY_GEN */
20169+
20170+ #endif /* WOLFSSL_CERT_GEN && !NO_ASN_TIME */
2010520171 return EXPECT_RESULT();
2010620172}
2010720173#endif /* WOLFSSL_CERT_SIGN_CB */
0 commit comments