Check When a TLS Certificate Expires With OpenSSL

Check a certificate’s expiry date and learn to tell a seven-day warning from an unreadable file in a local OpenSSL lab.

Check When a TLS Certificate Expires With OpenSSL

You have a certificate file and need to know whether to renew it this week. Start by checking that it’s the certificate you meant to inspect. Then ask OpenSSL for two things: its exact expiration date and whether that date falls inside your warning window.

This lab gives you three files to compare: a certificate good for one day, another good for thirty days, and a text file that isn’t a certificate at all. Nothing connects to a server or changes your trust store. Before you run it, predict which file should trigger a seven-day warning and which shouldn’t reach the date check at all.

1. Run the lab on Linux

Use a Bash terminal and check openssl version. The commands below were tested on Linux with OpenSSL 3.5.9. Apple’s preinstalled command and other versions weren’t tested. Paste the whole block as one unit.

The block creates a private temporary folder and two self-signed practice certificates. Each gets a fresh RSA key just for the exercise. The -noenc option writes those temporary keys without passphrase encryption, so don’t reuse this generation command for a real certificate or move these keys out of the lab. The inspection commands don’t open the private keys. The block exits its own temporary shell and deletes only its generated folder, even if an early step fails.

(
  umask 077
  lab=$(mktemp -d "/tmp/cert-expiry.XXXXXXXX") || exit 1
  trap 'rm -rf -- "$lab"' EXIT
  cd "$lab" || exit 1

  make_cert() {
    openssl req -x509 -newkey rsa:2048 -noenc \
      -subj "/CN=$1.invalid" -days "$2" \
      -keyout "$1.key" -out "$1.pem"
  }

  inspect_cert() {
    local cert=$1 rc=0
    printf '\nChecking %s\n' "$cert"
    if ! openssl x509 -inform PEM -in "$cert" -noout -subject -enddate; then
      printf 'Could not read a PEM certificate; expiry check skipped.\n' >&2
      return 2
    fi

    openssl x509 -inform PEM -in "$cert" -noout -checkend 604800 \
      2>check-error.txt || rc=$?
    if [[ -s check-error.txt ]]; then
      cat check-error.txt >&2
      printf 'OpenSSL reported an error; expiry result not used.\n' >&2
      return 3
    fi

    case "$rc" in
      0) printf 'Outside the seven-day warning window.\n' ;;
      1) printf 'Expires within seven days or has already expired.\n' ;;
      *) printf 'Expiry check failed with exit %s.\n' "$rc" >&2
         return 3 ;;
    esac
  }

  make_cert soon 1 2>generation.log || { cat generation.log >&2; exit 1; }
  make_cert later 30 2>generation.log || { cat generation.log >&2; exit 1; }
  printf '%s\n' 'This is not a certificate.' > invalid.pem || exit 1

  inspect_cert soon.pem || exit 1
  inspect_cert later.pem || exit 1
  invalid_rc=0
  inspect_cert invalid.pem || invalid_rc=$?
  if [[ $invalid_rc != 2 ]]; then
    printf 'Unexpected result for invalid fixture: exit %s\n' "$invalid_rc" >&2
    exit 1
  fi
)

2. Read the date before reading the decision

The first OpenSSL call opens a PEM certificate and prints its subject and expiration timestamp. In the lab, subject distinguishes soon.invalid from later.invalid. notAfter is the expiration date and time. The timestamp is shown in GMT, so don’t read it as local time.

Expect three outcomes:

  • soon.pem: expires about one day after generation. The threshold command returns 1 in this lab and the wrapper prints Expires within seven days or has already expired.
  • later.pem: expires about 30 days after generation. The threshold command returns 0 and the wrapper prints Outside the seven-day warning window.
  • invalid.pem: cannot be read as a certificate. The wrapper prints Could not read a PEM certificate; expiry check skipped. This is expected. It doesn’t mean an expiry date was found.

The second OpenSSL call uses -checkend 604800, where 604,800 seconds is seven days. It compares expiration with the clock on the computer running the command. If a result looks off, check the subject and your computer’s date and time. OpenSSL documents a nonzero exit when the certificate is near expiry; invocation errors can also return a nonzero status. This lab reads the file first and then treats diagnostics from the threshold command or unexpected exit codes as errors rather than renewal warnings. Its 0 and 1 outcomes were checked on the generated fixtures.

3. Use the date for the right decision

To inspect a file of your own, make sure it contains the single public PEM certificate you want, then run this read-only command with its path. You don’t need the private key, and you don’t need to share any file:

openssl x509 -inform PEM -in ./certificate.pem -noout -subject -enddate

If it can’t read the file, confirm the path and format and ask the certificate exporter for the intended PEM certificate. A chain file can contain several certificates; reading one does not tell you when each one expires.

Once that first command prints the expected subject and date, run the seven-day check on the same file:

openssl x509 -inform PEM -in ./certificate.pem -noout -checkend 604800

On the tested version, look for Certificate will expire or Certificate will not expire. If OpenSSL reports an error instead, fix the input before making a renewal decision. If the file is near expiry, plan the renewal and check that the renewed certificate actually gets deployed.

A distant notAfter date only answers the expiry question for that file. It doesn’t establish that the certificate is already valid, chains to a trusted issuer, matches a hostname, hasn’t been revoked, or is the certificate a live server presents. Both practice certificates are self-signed; the one with thirty days left still passes the expiry check.

Sources