@@ -23753,18 +23753,7 @@ static int DecodeCertInternal(DecodedCert* cert, int verify, int* criticalExt,
2375323753 cert->version = version;
2375423754 cert->serialSz = (int)serialSz;
2375523755
23756- #if !defined(WOLFSSL_NO_ASN_STRICT) && !defined(WOLFSSL_PYTHON) && \
23757- !defined(WOLFSSL_ASN_ALLOW_0_SERIAL)
23758- /* RFC 5280 section 4.1.2.2 states that non-conforming CAs may issue
23759- * a negative or zero serial number and should be handled gracefully.
23760- * Since it is a non-conforming CA that issues a serial of 0 then we
23761- * treat it as an error here. */
23762- if (cert->serialSz == 1 && cert->serial[0] == 0) {
23763- WOLFSSL_MSG("Error serial number of 0, use WOLFSSL_NO_ASN_STRICT "
23764- "if wanted");
23765- ret = ASN_PARSE_E;
23766- }
23767- #endif
23756+ /* Check for serial size of zero */
2376823757 if (cert->serialSz == 0) {
2376923758 WOLFSSL_MSG("Error serial size is zero. Should be at least one "
2377023759 "even with no serial number.");
@@ -23980,6 +23969,20 @@ static int DecodeCertInternal(DecodedCert* cert, int verify, int* criticalExt,
2398023969 }
2398123970 }
2398223971
23972+ #if !defined(WOLFSSL_NO_ASN_STRICT) && !defined(WOLFSSL_PYTHON) && \
23973+ !defined(WOLFSSL_ASN_ALLOW_0_SERIAL)
23974+ /* Check for serial number of 0. RFC 5280 section 4.1.2.2 requires
23975+ * positive serial numbers. However, allow zero for self-signed CA
23976+ * certificates (root CAs) since they are explicitly trusted and some
23977+ * legacy root CAs in real-world trust stores have serial number 0. */
23978+ if ((ret == 0) && (cert->serialSz == 1) && (cert->serial[0] == 0)) {
23979+ if (!(cert->isCA && cert->selfSigned)) {
23980+ WOLFSSL_MSG("Error serial number of 0 for non-root certificate");
23981+ ret = ASN_PARSE_E;
23982+ }
23983+ }
23984+ #endif
23985+
2398323986 if ((ret == 0) && (!done) && (badDate != 0)) {
2398423987 /* Parsed whole certificate fine but return any date errors. */
2398523988 ret = badDate;
0 commit comments