Skip to content

Allow serial number 0 for root CA certificates#9567

Open
jackctj117 wants to merge 11 commits intowolfSSL:masterfrom
jackctj117:serial-0
Open

Allow serial number 0 for root CA certificates#9567
jackctj117 wants to merge 11 commits intowolfSSL:masterfrom
jackctj117:serial-0

Conversation

@jackctj117
Copy link
Contributor

Fixes #8615

This pull request updates the logic for validating X.509 certificate serial numbers in wolfcrypt/src/asn.c. The main change is to improve compliance with RFC 5280 while allowing for real-world exceptions involving root CAs. The previous strict check for zero serial numbers is now more nuanced, permitting serial number 0 for self-signed CA certificates but still rejecting it for other certificates.

Certificate serial number validation improvements:

  • Moved and updated the check for serial numbers of 0 to allow self-signed CA (root) certificates to have a serial number of 0, while still treating serial 0 as an error for all other certificates. This better aligns with RFC 5280 and real-world trust store practices. [1] [2]
  • Removed the previous check that always treated a serial number of 0 as an error, regardless of certificate type.

Testing

Tested with certificates generated using OpenSSL to verify all scenarios:

  • Root CA with serial 0 loads successfully (previously failed, now passes)
  • End-entity cert with serial 0 is correctly rejected
  • Normal end-entity cert signed by root CA with serial 0 verifies successfully
  • Self-signed non-CA cert with serial 0 is correctly rejected

@jackctj117 jackctj117 self-assigned this Dec 19, 2025
Copy link
Contributor

@kareem-wolfssl kareem-wolfssl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good.

Can you add some test cases with a CA and leaf cert with a serial of 0?

@jackctj117
Copy link
Contributor Author

@kareem-wolfssl it looks like the failures are looking for an expected failure due to a self-signed CA certificate with serial number 0.

@kareem-wolfssl
Copy link
Contributor

@kareem-wolfssl it looks like the failures are looking for an expected failure due to a self-signed CA certificate with serial number 0.

Yes, you will need to update the failing test test_MakeCertWith0Ser since we no longer expect a root CA with serial 0 to fail.

@dgarske
Copy link
Contributor

dgarske commented Dec 26, 2025

@jackctj117 looks like some valid unit test failures you will need to look into. All related to --enable-asn=original.

FAILURES:
   390: test_SerialNumber0_RootCA

@jackctj117
Copy link
Contributor Author

Jenkins retest this please. History lost.

1 similar comment
@jackctj117
Copy link
Contributor Author

Jenkins retest this please. History lost.

@jackctj117
Copy link
Contributor Author

Jenkins retest this please

1 similar comment
@jackctj117
Copy link
Contributor Author

Jenkins retest this please

@jackctj117
Copy link
Contributor Author

Jenkins retest this

@jackctj117
Copy link
Contributor Author

Jenkins retest this please

Comment on lines 678 to 679
ExpectIntNE(wolfSSL_CertManagerVerify(cm, eeSerial0File,
WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should test for the expected error code.

Comment on lines 701 to 702
ExpectIntNE(wolfSSL_CertManagerLoadCA(cm, selfSignedNonCASerial0File, NULL),
WOLFSSL_SUCCESS);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, check for the expected error code.

@kareem-wolfssl
Copy link
Contributor

kareem-wolfssl commented Jan 30, 2026

Jenkins retest this please.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@jackctj117 jackctj117 requested a review from Copilot February 5, 2026 20:20
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 20 out of 20 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 20 out of 20 changed files in this pull request and generated 2 comments.

Comments suppressed due to low confidence (1)

wolfcrypt/src/asn.c:23904

  • The log message for cert->serialSz == 0 is confusing: it suggests a certificate might legitimately have “no serial number”, but X.509 certificates always have a serial number and RFC 5280 requires it to be present. Consider rewording to clearly state that an empty/zero-length serial is invalid, and (if appropriate in this file) emit the verbose error macro consistently with other parse failures.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 20 out of 20 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 20 out of 20 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +25799 to +25818
#if !defined(WOLFSSL_NO_ASN_STRICT) && !defined(WOLFSSL_PYTHON) && \
!defined(WOLFSSL_ASN_ALLOW_0_SERIAL)
/* Check for serial number of 0. RFC 5280 section 4.1.2.2 requires
* positive serial numbers. However, allow zero for self-signed CA
* certificates (root CAs) being loaded as trust anchors since they
* are explicitly trusted and some legacy root CAs in real-world
* trust stores have serial number 0. */
if ((ret == 0) && (cert->serialSz == 1) && (cert->serial[0] == 0)) {
if (!(type == CA_TYPE && cert->isCA && cert->selfSigned)
#ifdef WOLFSSL_CERT_REQ
&& !cert->isCSR
#endif
) {
WOLFSSL_MSG("Error serial number of 0 for non-root certificate");
ret = ASN_PARSE_E;
}
}
if (ret < 0) {
return ret;
}
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Serial-0 enforcement is now only done in ParseCertRelative(). Other parsing entry points that call DecodeToKey()/wc_GetPubX509()/DecodeCertInternal() directly (e.g., certificate loading in src/ssl_load.c) will no longer reject a zero serial even in strict mode. This can reintroduce acceptance of non-root certs with serial 0 in parts of the API; consider adding an equivalent strict check in those code paths (or refactoring the check into a shared helper that supports the root-CA exception).

Copilot uses AI. Check for mistakes.
Comment on lines +795 to +805
#if !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
/* Test that root CA certificates with serial number 0 are accepted,
* while non-root certificates with serial 0 are rejected (issue #8615) */

#if !defined(WOLFSSL_NO_ASN_STRICT) && !defined(WOLFSSL_PYTHON) && \
!defined(WOLFSSL_ASN_ALLOW_0_SERIAL)
WOLFSSL_CERT_MANAGER* cm = NULL;
const char* rootSerial0File = "./certs/test-serial0/root_serial0.pem";
const char* rootNormalFile = "./certs/test-serial0/root.pem";
const char* eeSerial0File = "./certs/test-serial0/ee_serial0.pem";
const char* eeNormalFile = "./certs/test-serial0/ee_normal.pem";
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test loads/verifies PEM files via wolfSSL_CertManagerLoadCA/Verify, but it’s only guarded by NO_FILESYSTEM/NO_RSA. In builds without PEM-to-DER support (WOLFSSL_PEM_TO_DER), these APIs return NOT_COMPILED_IN and the test will fail. Please guard this test with WOLFSSL_PEM_TO_DER (or switch fixtures to DER and use WOLFSSL_FILETYPE_ASN1).

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: WolfSSL accepts a certificate whose serial number is zero

3 participants