Web servers and networking · Intermediate · 22 min
How TLS certificates work: chains, SANs and expiry, with openssl
By Padding · Updated 18 Nov 2026
Ubuntu 26.04.1 LTS · OpenSSL 3.5.5 · Nginx 1.30.8
You will build a small certificate authority with OpenSSL 3.5, read and verify the chain it issues, serve it from Nginx, and have a script that warns you before a certificate expires.
On this page
Every HTTPS connection starts with the server handing over a certificate: a signed statement that says "this public key belongs to these names, until this date". The browser accepts it only if it can follow a chain of signatures from that certificate up to a root it already trusts, the names match the address it asked for, and the dates are current. Most TLS errors you will meet are one of those three checks failing.
Reading about chains only goes so far. In this tutorial you will make your own: a root CA, an intermediate CA and a server certificate for shop.test. You will read each one with openssl x509, prove the chain with openssl verify, serve it from Nginx on port 8443, inspect the handshake with openssl s_client, and finish with a small expiry check you can run from cron or a monitoring tool.
The CA you build here is for learning. Nothing outside your server trusts it, and you should never install its root on other people's devices.
Step 1 · Install OpenSSL and write the certificate profiles
Install OpenSSL and curl. Both are often present already; installing them again does no harm.
sudo apt-get update
sudo apt-get install -y openssl curl ca-certificates
openssl versionThe version line starts with OpenSSL 3.5.
Make a throwaway folder for the demo. Every command from here on runs inside it.
mkdir -p ~/tls-demo
cd ~/tls-demoA certificate is more than a name and a key. Extensions say what it is allowed to do. Write them down once, in a small file with one section per kind of certificate:
cat > ca.cnf <<'EOF'
[root_ca]
basicConstraints = critical, CA:TRUE
keyUsage = critical, keyCertSign, cRLSign
subjectKeyIdentifier = hash
[intermediate_ca]
basicConstraints = critical, CA:TRUE, pathlen:0
keyUsage = critical, keyCertSign, cRLSign
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always
[leaf]
basicConstraints = critical, CA:FALSE
keyUsage = critical, digitalSignature
extendedKeyUsage = serverAuth
subjectAltName = DNS:shop.test, DNS:www.shop.test, IP:127.0.0.1
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always
EOFWhat the lines mean:
basicConstraintsmarks a certificate as a CA or not.pathlen:0lets the intermediate sign server certificates but not further CAs.keyUsagelimits the key: CAs sign certificates (keyCertSign) and revocation lists (cRLSign); the server key only makes signatures during the handshake (digitalSignature).extendedKeyUsage = serverAuthsays the certificate is for TLS servers.subjectAltName(SAN) lists the names the certificate is valid for. Clients match the address they connected to against this list.- The two key identifiers link each certificate to the one that signed it. You will see that link in a later step.
Check it worked
grep -c '^\[' ca.cnfThis prints 3, one for each section.
Step 2 · Create the root CA
The root is the top of the chain. It signs itself, which is why only certificates already held by the client can act as roots.
Create its private key. Wrapping the command in ( umask 077; ... ) makes the file readable only by you from the moment it exists.
cd ~/tls-demo
( umask 077; openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out root.key )Make a signing request for the root's name, then sign it with its own key for ten years:
openssl req -new -key root.key -subj "/O=Demo Labs/CN=Demo Root CA" -out root.csr
openssl x509 -req -in root.csr -key root.key -sha256 -days 3650 \
-extfile ca.cnf -extensions root_ca -out root.crtReal roots live on offline hardware and sign almost nothing. Here the key sits in a folder, which is fine for a demo and nothing else.
Check it worked
openssl x509 -in root.crt -noout -subject -issuer
ls -l root.keyThe subject and issuer lines are identical: O=Demo Labs, CN=Demo Root CA. That is what self-signed means. The key's permissions read -rw-------.
Step 3 · Create an intermediate CA
Public CAs do not sign server certificates with their root key. They sign an intermediate with it, then use the intermediate for everyday work. If an intermediate key leaks, the CA revokes that one intermediate and the root stays trusted.
cd ~/tls-demo
( umask 077; openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out intermediate.key )
openssl req -new -key intermediate.key -subj "/O=Demo Labs/CN=Demo Intermediate CA" -out intermediate.csr
openssl x509 -req -in intermediate.csr -CA root.crt -CAkey root.key -sha256 -days 1825 \
-extfile ca.cnf -extensions intermediate_ca -out intermediate.crtThis time -CA and -CAkey name the root, so the root signs the request.
Check it worked
openssl verify -CAfile root.crt intermediate.crtThe output is intermediate.crt: OK. -CAfile adds root.crt as a trusted root for this one command. OpenSSL still checks the system store in /etc/ssl/certs as well, but nothing there knows your demo root, so the pass comes from root.crt.
Step 4 · Issue a server certificate with SANs
Now the certificate a web server presents. It gets its own key and a 45-day life, the length Let's Encrypt is moving to.
cd ~/tls-demo
( umask 077; openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out shop.key )
openssl req -new -key shop.key -subj "/CN=shop.test" -out shop.csr
openssl x509 -req -in shop.csr -CA intermediate.crt -CAkey intermediate.key -sha256 -days 45 \
-extfile ca.cnf -extensions leaf -out shop.crtWeb servers send the server certificate and the intermediate together. Build that file now, server certificate first:
cat shop.crt intermediate.crt > shop.fullchain.pemCheck it worked
grep -c 'BEGIN CERTIFICATE' shop.fullchain.pem
openssl x509 -in shop.crt -noout -ext subjectAltNameThe first command prints 2. The second lists DNS:shop.test, DNS:www.shop.test, IP Address:127.0.0.1.
Step 5 · Read a certificate with openssl x509
Print the whole server certificate:
cd ~/tls-demo
openssl x509 -in shop.crt -noout -textThe output is long. Read it top to bottom with these questions in mind.
Who issued it?
Issuer: O=Demo Labs, CN=Demo Intermediate CA. Names can be copied, so clients do not trust the name alone. Lower down, X509v3 Authority Key Identifier holds a short hash of the issuer's public key, and the issuer's own Subject Key Identifier must match it. Compare the two:
openssl x509 -in shop.crt -noout -ext authorityKeyIdentifier
openssl x509 -in intermediate.crt -noout -ext subjectKeyIdentifierThe hex strings are the same. The final proof is the signature at the bottom of the certificate, which only the intermediate's private key could have made. openssl verify checks it in the next step.
Which names is it for?
Subject: CN=shop.test looks like the answer, but it is not the one that counts. Browsers match only against X509v3 Subject Alternative Name. Chrome stopped reading the common name (CN) for this in 2017, and the current standard for checking names, RFC 9525, drops it too. Treat the CN as a label and put every hostname in the SAN.
OpenSSL's own name check is more forgiving, and that can hide a broken certificate. Issue one with a CN and no SAN:
printf '[cn_only]\nbasicConstraints = critical, CA:FALSE\nkeyUsage = critical, digitalSignature\nextendedKeyUsage = serverAuth\n' > cn-only.cnf
openssl x509 -req -in shop.csr -CA intermediate.crt -CAkey intermediate.key -sha256 -days 45 \
-extfile cn-only.cnf -extensions cn_only -out cn-only.crt
openssl verify -CAfile root.crt -untrusted intermediate.crt -verify_hostname shop.test cn-only.crtOpenSSL prints cn-only.crt: OK because it falls back to the CN when no DNS names are in the SAN. A browser rejects the same certificate with a name error. When you test a certificate, read the SAN yourself rather than trusting a pass from OpenSSL.
What may it do?
X509v3 Basic Constraints: critical, CA:FALSE means it cannot sign other certificates. Key Usage: Digital Signature and Extended Key Usage: TLS Web Server Authentication limit it to a TLS server. A certificate marked only for client authentication, or for email, fails as a server certificate even with the right names.
When is it valid?
Not Before and Not After are in UTC. A device whose clock is wrong, often a phone that lost power and reset its date, sees a valid certificate as "not yet valid" or "expired". When one user reports a certificate error that nobody else sees, ask them to check the date on their device.
Check it worked
openssl x509 -in shop.crt -noout -issuer -datesYou see the intermediate as issuer and notBefore/notAfter dates 45 days apart.
Step 6 · Verify the chain with openssl verify
Ask OpenSSL to check the server certificate against the root alone:
cd ~/tls-demo
openssl verify -CAfile root.crt shop.crt || echo "verification failed, as expected"It fails with error 20 at 0 depth lookup: unable to get local issuer certificate. Depth 0 is the server certificate. OpenSSL knows the root, but nothing connects shop.test to it, because the intermediate is missing.
Give it the intermediate with -untrusted. Certificates passed this way help build the chain but are never trusted on their own, which is how a client treats the intermediates a server sends.
openssl verify -show_chain -CAfile root.crt -untrusted intermediate.crt shop.crtThe output is shop.crt: OK followed by the chain, something like:
Chain:
depth=0: CN=shop.test (untrusted)
depth=1: O=Demo Labs, CN=Demo Intermediate CA (untrusted)
depth=2: O=Demo Labs, CN=Demo Root CAAdd the hostname check a browser makes. First a name in the SAN, then one that is not:
openssl verify -CAfile root.crt -untrusted intermediate.crt -verify_hostname www.shop.test shop.crt
openssl verify -CAfile root.crt -untrusted intermediate.crt -verify_hostname pay.shop.test shop.crt \
|| echo "verification failed, as expected"The first passes. The second fails with error 62 at 0 depth lookup: hostname mismatch.
Check it worked
You have now seen the two most common errors on purpose: error 20 for a missing intermediate and error 62 for a name that is not in the SAN. Run the passing check once more and confirm it ends in OK:
openssl verify -CAfile root.crt -untrusted shop.fullchain.pem shop.crtPassing the whole fullchain file as -untrusted works too, since the server certificate in it does no harm.
Step 7 · Serve the chain from Nginx and inspect it with s_client
Copy the certificate files where Nginx reads them. The key stays readable by root only.
cd ~/tls-demo
sudo install -d -m 755 /etc/nginx/tls
sudo install -m 644 shop.fullchain.pem /etc/nginx/tls/shop.test.fullchain.pem
sudo install -m 600 shop.key /etc/nginx/tls/shop.test.keyAdd a server block on port 8443. ssl_certificate takes the fullchain file: the server certificate first, then the intermediate.
sudo tee /etc/nginx/conf.d/shop-test.conf > /dev/null <<'EOF'
server {
listen 8443 ssl;
server_name shop.test www.shop.test;
ssl_certificate /etc/nginx/tls/shop.test.fullchain.pem;
ssl_certificate_key /etc/nginx/tls/shop.test.key;
location / {
default_type text/plain;
return 200 "shop.test over TLS\n";
}
}
EOFTest the configuration and reload:
sudo nginx -t && sudo systemctl reload nginxNow connect the way a client does. -servername sends the name in the handshake (SNI), which is how Nginx picks a certificate when one address serves many sites. -showcerts prints every certificate the server sends. Saving the output to a file makes it easier to read.
openssl s_client -connect 127.0.0.1:8443 -servername shop.test -showcerts \
-CAfile root.crt -verify_hostname shop.test < /dev/null > handshake.txt 2>&1
grep -E '^ *[0-9]+ s:|^ +i:' handshake.txt
grep -c 'BEGIN CERTIFICATE' handshake.txt
grep 'Verify return code' handshake.txtThe first grep shows the chain as the server sent it, something like:
0 s:CN=shop.test
i:O=Demo Labs, CN=Demo Intermediate CA
1 s:O=Demo Labs, CN=Demo Intermediate CA
i:O=Demo Labs, CN=Demo Root CARead each i: (issuer) line and find it as the s: (subject) of the next certificate. The last issuer, the root, is not in the list: the server never sends it, and the client must already hold it. The count is 2 and the last line is Verify return code: 0 (ok).
Check it worked
Make an ordinary HTTPS request. --resolve points shop.test at this server without touching DNS, and --cacert tells curl to trust your demo root:
curl -sS --cacert root.crt --resolve shop.test:8443:127.0.0.1 https://shop.test:8443/It prints shop.test over TLS. Without --cacert, curl refuses the connection, because the system trust store does not hold your demo root.
Step 8 · Check expiry before it bites
An expired certificate takes a site down as surely as a crashed server. openssl x509 -checkend <seconds> answers one question: will this certificate have expired that many seconds from now? It exits with 0 for no and 1 for yes, which is what scripts and monitoring tools need.
Try it on the file. 21 days is 1,814,400 seconds; 60 days is 5,184,000:
cd ~/tls-demo
openssl x509 -in shop.crt -noout -enddate -checkend 1814400
openssl x509 -in shop.crt -noout -checkend 5184000 || echo "exit status 1: expires within 60 days"The first prints the notAfter date and Certificate will not expire. The second prints Certificate will expire.
Files on disk can differ from what a server actually sends, for example after a renewal that nobody reloaded. Check the live certificate instead. Save this script:
cat > check-expiry.sh <<'EOF'
#!/usr/bin/env bash
# Usage: check-expiry.sh <hostname> [address:port] [days]
set -euo pipefail
name="$1"
addr="${2:-$name:443}"
days="${3:-21}"
cert=$(openssl s_client -connect "$addr" -servername "$name" < /dev/null 2> /dev/null | openssl x509) || {
echo "ERROR $name: could not read a certificate from $addr"
exit 2
}
end=$(printf '%s\n' "$cert" | openssl x509 -noout -enddate | cut -d= -f2)
if printf '%s\n' "$cert" | openssl x509 -noout -checkend $((days * 86400)) > /dev/null; then
echo "OK $name expires $end"
else
echo "WARN $name expires within $days days ($end)"
exit 1
fi
EOF
chmod +x check-expiry.shIt exits 0 when all is well, 1 when the certificate expires inside the window and 2 when it cannot connect, so cron, a CI job or a monitoring agent can act on the result. Run it against your Nginx with a 21-day window, then a 60-day one:
./check-expiry.sh shop.test 127.0.0.1:8443 21
./check-expiry.sh shop.test 127.0.0.1:8443 60 || echo "exit status $?"For a public site the defaults are enough. Replace example.com with your own domain:
./check-expiry.sh example.comWhere Let's Encrypt fits
A Let's Encrypt certificate has the same shape as the one you built. Its chain ends at ISRG Root X1 (RSA) or ISRG Root X2 (ECDSA), which ship in browser and operating system trust stores, and it passes through an intermediate that Let's Encrypt rotates from time to time. So never hard-code an intermediate; serve the fullchain.pem that Certbot writes. Let's Encrypt has also created a new generation of roots (Root YE and YR) that were still being added to trust stores in November 2026.
Lifetimes are getting shorter. As of November 2026, default certificates last 90 days. Let's Encrypt plans to cut that to 64 days from 10 February 2027 and 45 days from 16 February 2028, following the industry-wide limit of 47 days due in 2029. An opt-in profile already issues 45-day certificates. With lifetimes that short, automatic renewal is the only sensible setup, and an expiry check like the one above is how you find out when renewal has silently stopped.
Old devices matter here too. Android 7.0 and earlier do not trust ISRG Root X1 and lost access to Let's Encrypt sites when a cross-signature expired in September 2024. If your users include people on older, low-cost Android phones, which is common in many African markets, test your site on one before you assume HTTPS works for everyone.
Check it worked
The 21-day run printed a line starting OK shop.test expires. The 60-day run printed WARN shop.test expires within 60 days and then exit status 1.
Step 9 · Clean up
Remove the demo site from Nginx and reload:
sudo rm /etc/nginx/conf.d/shop-test.conf
sudo rm -r /etc/nginx/tlssudo nginx -t && sudo systemctl reload nginxThen delete the demo folder, which holds the CA keys:
cd ~
rm -r ~/tls-demoCheck it worked
ls ~/tls-demo 2>/dev/null || echo "tls-demo removed"
curl -sk https://127.0.0.1:8443/ || echo "nothing on 8443"You see tls-demo removed and nothing on 8443.
Next steps
You can now read any certificate, follow its chain and spot the usual faults.
- Put a real, publicly trusted certificate on your site: /tutorials/free-https-with-lets-encrypt-and-nginx
- Diagnose broken chains and name mismatches on live servers: /tutorials/fix-tls-certificate-chain-and-hostname-errors
- See where the TLS handshake sits in the life of a request: /tutorials/how-a-request-reaches-your-server
Related tutorials
- Write Nginx location blocks that match what you expectYou will build a practice Nginx 1.30 server where every location block labels its responses, predict which block answers each of twelve URLs, check your answers with curl, and fix the alias, regex-order and proxy_pass mistakes that catch most people out.
- Write a server firewall with nftablesYou will write one /etc/nftables.conf with a default-drop input chain, rate-limited SSH, named address sets, counters and logging, and load it over SSH behind a timer that undoes it if you lose your connection.
- Watch a TCP connection open and close with tcpdumpYou will capture a curl request to a local web server with tcpdump on Ubuntu 26.04, read its handshake, data and teardown line by line, see the reset a closed port sends, and save, filter and rotate captures as pcap files.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials