@@ -1245,16 +1245,13 @@ static int GetASN_Integer(const byte* input, word32 idx, int length,
12451245 */
12461246int GetASN_BitString(const byte* input, word32 idx, int length)
12471247{
1248- #if (!defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)) || \
1249- (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
12501248 /* Check contents consist of one or more octets. */
12511249 if (length == 0) {
12521250 #ifdef WOLFSSL_DEBUG_ASN_TEMPLATE
12531251 WOLFSSL_MSG("Zero length BIT STRING not allowed");
12541252 #endif
12551253 return ASN_PARSE_E;
12561254 }
1257- #endif
12581255 /* Ensure unused bits value is valid range. */
12591256 if (input[idx] > 7) {
12601257 #ifdef WOLFSSL_DEBUG_ASN_TEMPLATE
@@ -20631,6 +20628,25 @@ static int DecodeCertInternal(DecodedCert* cert, int verify, int* criticalExt,
2063120628 }
2063220629 }
2063320630
20631+ #if !defined(WOLFSSL_NO_ASN_STRICT) && !defined(WOLFSSL_PYTHON) && \
20632+ !defined(WOLFSSL_ASN_ALLOW_0_SERIAL)
20633+ /* Check for serial number of 0. RFC 5280 section 4.1.2.2 requires
20634+ * positive serial numbers. However, allow zero for self-signed CA
20635+ * certificates (root CAs) being loaded as trust anchors since they
20636+ * are explicitly trusted and some legacy root CAs in real-world
20637+ * trust stores have serial number 0. */
20638+ if ((ret == 0) && (cert->serialSz == 1) && (cert->serial[0] == 0)) {
20639+ if (!(cert->isCA && cert->selfSigned)
20640+ #ifdef WOLFSSL_CERT_REQ
20641+ && !cert->isCSR
20642+ #endif
20643+ ) {
20644+ WOLFSSL_MSG("Error serial number of 0 for non-root certificate");
20645+ ret = ASN_PARSE_E;
20646+ }
20647+ }
20648+ #endif
20649+
2063420650 if ((ret == 0) && (!done) && (badDate != 0)) {
2063520651 /* Parsed whole certificate fine but return any date errors. */
2063620652 ret = badDate;
0 commit comments