Dev.to Security πŸ” Cybersecurity πŸ‘ 0 πŸ“– 4 min read

How to catch an expiring or broken SSL certificate before your visitors do

Many TLS certificates now live 90 days, and the maximum lifetime for all public certificates is dropping in steps (200 days since March 2026, 47 days by 2029). Auto-renewal handles most of it, until a DNS change, a moved

Many TLS certificates now live 90 days, and the maximum lifetime for all public certificates is dropping in steps (200 days since March 2026, 47 days by 2029). Auto-renewal handles most of it, until a DNS change, a moved server or a forgotten subdomain breaks the renewal and nobody notices until visitors see a browser warning.

Here is what to check, how to do it for free with tools you already have, and how to run the same checks on a long list of domains.

The four problems to look for

1. Expiry. The certificate is past its end date, or will be soon. You want a warning weeks ahead, not on the day.

2. Wrong name. The certificate is valid but does not cover the name the visitor typed. Typical after a migration: example.com is covered, www.example.com or shop.example.com is not.

3. Broken chain. The server does not send its intermediate certificate. Desktop browsers often repair this on their own, so the site "works for me", while API clients, payment callbacks and bots fail.

4. Self-signed or private CA. Fine on an internal tool, a problem on anything public.

Option A: openssl for one host

openssl is installed on most Linux and macOS machines and ships with Git for Windows:

openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null \
  | openssl x509 -noout -subject -issuer -enddate -ext subjectAltName

This prints who the certificate is for, who issued it, its end date and every name it covers. -servername matters: without it, a server hosting several sites may hand you the wrong certificate. To check the chain, look for Verify return code: 0 (ok) in the output of the first command alone.

Option B: a short Python script for a list

Python's standard library can do a strict check, the same way an API client would, with no extra package:

import socket, ssl
from datetime import datetime, timezone

def check(host, port=443, warn_days=30):
    ctx = ssl.create_default_context()  # verifies the chain and the hostname
    try:
        with socket.create_connection((host, port), timeout=10) as sock:
            with ctx.wrap_socket(sock, server_hostname=host) as tls:
                cert = tls.getpeercert()
    except ssl.SSLCertVerificationError as e:
        return f"BAD {host}: {e.verify_message}"
    except OSError as e:
        return f"ERR {host}: {e}"
    end = datetime.fromtimestamp(ssl.cert_time_to_seconds(cert["notAfter"]), timezone.utc)
    days = (end - datetime.now(timezone.utc)).days
    return f"{'OK ' if days > warn_days else 'BAD'} {host}: {days} days left"

for line in open("domains.txt", encoding="utf-8"):
    if line.strip():
        print(check(line.strip()))

Expired certificates, wrong names, self-signed certificates and missing intermediates all end up in the BAD branch with the reason. One limit: when verification fails, Python does not give you the certificate, so you see why it failed but not its dates or names. For that, fall back to the openssl command above. Run the script daily from cron or a scheduled task and you have a basic monitor.

Option C: a hosted checker for bulk jobs

Disclosure: this one is ours. We publish SSL Certificate Checker on Apify. You paste a list of domains, host:port pairs or full URLs, and it does one TLS handshake per host, up to 100 in parallel. Unlike the script above, it still reads and reports the certificate when it is not trusted.

from apify_client import ApifyClient  # pip install apify-client

client = ApifyClient("YOUR_APIFY_TOKEN")
run = client.actor("extas_corp/ssl-certificate-checker").call(
    run_input={"domains": ["example.com", "smtp.example.com:465"], "warnDays": 21})
for item in client.dataset(run["defaultDatasetId"]).iterate_items():
    if not item["ok"]:
        print(item["host"], item.get("daysLeft"), item["warnings"], item["error"])

A certificate served for the wrong host comes back like this (shortened):

{
  "host": "wrong.host.badssl.com",
  "ok": false,
  "trusted": false,
  "trustError": "Hostname mismatch, certificate is not valid for 'wrong.host.badssl.com'.",
  "subject": "*.badssl.com",
  "daysLeft": 45
}

Each row also has the issuer, validFrom / validTo, all alternative names, a wildcard flag, the TLS version and cipher. ok is true only when the certificate is trusted, not expired and not expiring within warnDays. DNS, connection and timeout errors are reported on their row and do not stop the run.

It is free during launch: you only pay Apify's own platform usage, which the free monthly credit covers for normal use. Limits: it is not a full TLS security scan (no cipher enumeration, no vulnerability tests), revocation is not checked, STARTTLS ports such as SMTP 587 are not supported, and a CDN may serve a different certificate in another region than the one the check runs from.

What to put on the list

  • Every hostname, not only the main domain: www, apex, api, mail, old marketing subdomains.
  • Mail and other TLS services on their own ports (465, 993).
  • The vendors you depend on: an expired certificate on a payment or webhook endpoint breaks your site too.

Run it daily, alert only on BAD, and the next expiry becomes a ticket instead of an outage.

πŸ“° Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.