Articles / Security

  • tls
  • x509
  • certificates
  • openssl
  • go
  • debugging

Why Is a TLS Certificate Rejected Before Its Expiry Date?

You check the certificate, it has weeks left, and the client still refuses it. "Not expired" is only one test, on only one certificate. The client has to build a whole path to a root it trusts, and every link has to pass at the time the client thinks it is, for the job you are using it for.

The alert may say "expired" when it means something else

TLS has an alert called certificate_expired, and RFC 8446 defines it as a certificate that "has expired or is not currently valid". Go's error also covers both cases: certificate has expired or is not yet valid.

So read the rest of the message and note which side produced it. A server log showing an alert from a client tells you far less than the client's own verification error.

These are the usual suspects when the leaf's end date looks fine:

  • The client's clock is wrong, or the certificate's start date is still in the future.
  • An intermediate or root in the path has expired.
  • The certificate is not allowed to be used this way.
  • The name does not match.
  • The client has a stricter policy than the dates, or the certificate has been revoked.

Check the start date and the client's clock

A certificate has a notBefore as well as a notAfter, and a client with a slow clock sees a new certificate as coming from the future. Boards without a battery-backed clock, restored virtual machines and devices that cannot reach NTP are good candidates.

It often shows up straight after a renewal. The old certificate worked on the slow machine because its start date was long past; the new one started recently. CAs commonly backdate notBefore a little to absorb small errors, but that will not rescue a clock that is days out.

Compare these on the failing machine itself:

date -u
openssl x509 -in leaf.pem -noout -startdate -enddate

Quick detour: this one is properly circular. If a device gets its time from a source authenticated with TLS (or NTS, which uses TLS for its key exchange), it may need the right time to check the certificate that would give it the right time. Something has to break the loop: a hardware clock, a saved last-known time, or a deliberately chosen bootstrap source.

An intermediate can expire before the leaf does

Nothing in the leaf's dates tells you about the certificates above it, and every certificate in the accepted path needs to be valid. Public CAs plan around this. Private CAs, built one afternoon three years ago, often do not.

I made a test chain with a 90-day leaf and a 1-day intermediate, then asked OpenSSL to verify it ten days ahead:

openssl verify -CAfile ca.pem -untrusted int.pem \
  -attime "$(date -d '+10 days' +%s)" leaf.pem
CN=TestInt
error 10 at 1 depth lookup: certificate has expired
error leaf.pem: verification failed

Look at the depth. Depth 0 is the leaf; here the problem is at depth 1, the intermediate. The -attime option takes a Unix timestamp, so you can also find out about next month's outage today.

Hang on, what if there are two possible paths? This happened at scale when DST Root CA X3 expired in September 2021. Some older clients, notably those on OpenSSL 1.0.2, could follow the path to the expired root and fail, although another route to a trusted root existed. The server sent the same bytes, and the answer depended on the client.

In date does not mean allowed for this job

Certificates carry restrictions, and a perfectly fresh one fails if it is used outside them. RFC 5280 path validation and the application's own checks cover things such as:

  • Extended key usage: a server certificate that only lists clientAuth is no good for a server.
  • CA constraints: an issuer must be marked as a CA and permitted to sign certificates.
  • Name constraints: a private CA limited to one domain cannot vouch for another.
  • Subject alternative names: the hostname or IP address you dialled must be listed. Go ignores a name that appears only in the Common Name.

I tried the first of these with a clientAuth-only leaf. curl, using OpenSSL, reported unsuitable certificate purpose (26). Go said x509: certificate specifies an incompatible key usage. Neither error has anything to do with dates.

Clients add rules of their own

Two clients can disagree about the same chain because each brings its own trust store and policy. For example, Apple platforms limit how long a publicly trusted TLS server certificate may be valid: for those issued from 1 September 2020 the ceiling was set at 398 days. So a certificate can be well within its dates and still be rejected for having too many of them.

Other things a client may object to:

  • A root that is missing from its store, or has been distrusted.
  • A weak signature algorithm or a key below its minimum size.
  • Missing Certificate Transparency evidence, where the client requires it.
  • Revocation, if the client checks it. Behaviour varies a lot, and a plain openssl verify does not check CRLs unless asked.
  • Certificate or public key pins that no longer match after renewal.

Ask Go which path it accepts, and when

This small program prints what the server sent, then verifies it at a time you choose. It deliberately turns off handshake verification so that it can inspect a bad chain, then calls Verify itself. Do not copy InsecureSkipVerify into a real client.

package main

import (
	"crypto/tls"
	"crypto/x509"
	"flag"
	"fmt"
	"log"
	"net"
	"os"
	"time"
)

func main() {
	at := flag.String("at", "", "verify as of this RFC 3339 time (default: now)")
	caFile := flag.String("ca", "", "PEM roots to use instead of the system store")
	flag.Parse()
	if flag.NArg() != 1 {
		log.Fatal("usage: whycert [-at time] [-ca roots.pem] host:port")
	}
	host, _, err := net.SplitHostPort(flag.Arg(0))
	if err != nil {
		log.Fatal(err)
	}
	now := time.Now()
	if *at != "" {
		if now, err = time.Parse(time.RFC3339, *at); err != nil {
			log.Fatal(err)
		}
	}
	var roots *x509.CertPool // nil means the system roots
	if *caFile != "" {
		pem, err := os.ReadFile(*caFile)
		if err != nil {
			log.Fatal(err)
		}
		roots = x509.NewCertPool()
		if !roots.AppendCertsFromPEM(pem) {
			log.Fatal("no certificates in ", *caFile)
		}
	}

	// Verification is off only so we can look at a chain that fails it.
	// Nothing is sent over this connection. Never copy this into a client.
	dialer := &net.Dialer{Timeout: 10 * time.Second}
	conn, err := tls.DialWithDialer(dialer, "tcp", flag.Arg(0), &tls.Config{
		ServerName:         host,
		InsecureSkipVerify: true,
	})
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()

	certs := conn.ConnectionState().PeerCertificates
	intermediates := x509.NewCertPool()
	for i, c := range certs {
		fmt.Printf("%d %s\n  issuer %s\n  valid  %s to %s\n", i, c.Subject, c.Issuer,
			c.NotBefore.Format(time.RFC3339), c.NotAfter.Format(time.RFC3339))
		if i > 0 {
			intermediates.AddCert(c)
		}
	}

	chains, err := certs[0].Verify(x509.VerifyOptions{
		DNSName:       host,
		Roots:         roots,
		Intermediates: intermediates,
		CurrentTime:   now,
	})
	if err != nil {
		log.Fatalf("at %s: %v", now.UTC().Format(time.RFC3339), err)
	}
	for _, chain := range chains {
		fmt.Println("accepted path:")
		for _, c := range chain {
			fmt.Printf("  %s (until %s)\n", c.Subject, c.NotAfter.Format(time.DateOnly))
		}
	}
}

Against my test server, with -at set ten days ahead, the final line was:

at 2026-12-13T04:40:20Z: x509: certificate has expired or is not yet valid: current time 2026-12-13T04:40:20Z is after 2026-12-04T04:40:11Z

That second timestamp belongs to the intermediate; the leaf's expiry is in March 2027. Match the date in the error against the printed chain and you have your culprit.

Narrow it down in this order

  1. Get the exact error from the client that fails.
  2. Check that client's clock in UTC.
  3. Check both dates on every certificate in the path, including the root in the client's store.
  4. Check names, key usage and constraints.
  5. Verify with the same trust store, TLS library and version as the failing client.

The program above only reproduces Go's view, on the machine where you run it. It does not check revocation, and it says nothing about what an old phone or an embedded TLS stack will decide. A pass on your desk is only evidence about your desk.