Skip to content

Commit e960a00

Browse files
authored
Merge pull request #7625 from JacobBarthelmeh/x509
sanity check on non conforming serial number of 0
2 parents 1c51465 + 467b3cb commit e960a00

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

tests/api.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53763,6 +53763,74 @@ static int test_MakeCertWithPathLen(void)
5376353763
return EXPECT_RESULT();
5376453764
}
5376553765

53766+
static int test_MakeCertWith0Ser(void)
53767+
{
53768+
EXPECT_DECLS;
53769+
#if defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME) && \
53770+
defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC) && \
53771+
defined(WOLFSSL_ASN_TEMPLATE)
53772+
Cert cert;
53773+
DecodedCert decodedCert;
53774+
byte der[FOURK_BUF];
53775+
int derSize = 0;
53776+
WC_RNG rng;
53777+
ecc_key key;
53778+
int ret;
53779+
53780+
XMEMSET(&rng, 0, sizeof(WC_RNG));
53781+
XMEMSET(&key, 0, sizeof(ecc_key));
53782+
XMEMSET(&cert, 0, sizeof(Cert));
53783+
XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
53784+
53785+
ExpectIntEQ(wc_InitRng(&rng), 0);
53786+
ExpectIntEQ(wc_ecc_init(&key), 0);
53787+
ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
53788+
ExpectIntEQ(wc_InitCert(&cert), 0);
53789+
53790+
(void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
53791+
(void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
53792+
(void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
53793+
(void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
53794+
(void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
53795+
(void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
53796+
CTC_NAME_SIZE);
53797+
(void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
53798+
CTC_NAME_SIZE);
53799+
53800+
cert.selfSigned = 1;
53801+
cert.isCA = 1;
53802+
cert.sigType = CTC_SHA256wECDSA;
53803+
53804+
#ifdef WOLFSSL_CERT_EXT
53805+
cert.keyUsage |= KEYUSE_KEY_CERT_SIGN;
53806+
#endif
53807+
53808+
/* set serial number to 0 */
53809+
cert.serialSz = 1;
53810+
cert.serial[0] = 0;
53811+
53812+
ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
53813+
ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
53814+
FOURK_BUF, NULL, &key, &rng), 0);
53815+
53816+
wc_InitDecodedCert(&decodedCert, der, (word32)derSize, NULL);
53817+
53818+
#if !defined(WOLFSSL_NO_ASN_STRICT) && !defined(WOLFSSL_PYTHON)
53819+
ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL),
53820+
ASN_PARSE_E);
53821+
#else
53822+
ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
53823+
#endif
53824+
53825+
wc_FreeDecodedCert(&decodedCert);
53826+
ret = wc_ecc_free(&key);
53827+
ExpectIntEQ(ret, 0);
53828+
ret = wc_FreeRng(&rng);
53829+
ExpectIntEQ(ret, 0);
53830+
#endif
53831+
return EXPECT_RESULT();
53832+
}
53833+
5376653834
static int test_MakeCertWithCaFalse(void)
5376753835
{
5376853836
EXPECT_DECLS;
@@ -73183,6 +73251,7 @@ TEST_CASE testCases[] = {
7318373251
TEST_DECL(test_wc_ParseCert),
7318473252
TEST_DECL(test_wc_ParseCert_Error),
7318573253
TEST_DECL(test_MakeCertWithPathLen),
73254+
TEST_DECL(test_MakeCertWith0Ser),
7318673255
TEST_DECL(test_MakeCertWithCaFalse),
7318773256
TEST_DECL(test_wc_SetKeyUsage),
7318873257
TEST_DECL(test_wc_SetAuthKeyIdFromPublicKey_ex),

wolfcrypt/src/asn.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21875,6 +21875,19 @@ static int DecodeCertInternal(DecodedCert* cert, int verify, int* criticalExt,
2187521875
/* Set fields extracted from data. */
2187621876
cert->version = version;
2187721877
cert->serialSz = (int)serialSz;
21878+
21879+
#if !defined(WOLFSSL_NO_ASN_STRICT) && !defined(WOLFSSL_PYTHON)
21880+
/* RFC 5280 section 4.1.2.2 states that non-conforming CAs may issue
21881+
* a negative or zero serial number and should be handled gracefully.
21882+
* Since it is a non-conforming CA that issues a serial of 0 then we
21883+
* treat it as an error here. */
21884+
if (cert->serialSz == 1 && cert->serial[0] == 0) {
21885+
WOLFSSL_MSG("Error serial number of 0, use WOLFSSL_NO_ASN_STRICT "
21886+
"if wanted");
21887+
ret = ASN_PARSE_E;
21888+
}
21889+
#endif
21890+
2187821891
cert->signatureOID = dataASN[X509CERTASN_IDX_TBS_ALGOID_OID].data.oid.sum;
2187921892
cert->keyOID = dataASN[X509CERTASN_IDX_TBS_SPUBKEYINFO_ALGO_OID].data.oid.sum;
2188021893
cert->certBegin = dataASN[X509CERTASN_IDX_TBS_SEQ].offset;

0 commit comments

Comments
 (0)