Day 8: TLS
Goal: You read a certificate and its chain with
openssl, build your own small CA with an intermediate certificate, give nginx TLS and mTLS, explain SNI using a failure and identify the three most common TLS errors – wrong name, expired, incomplete chain – by their message · Duration: approx. 100 min · Prerequisite: Day 7 (unitweb-nginxis running)
Briefing
- TLS errors are notorious because every application translates them differently: “x509: certificate signed by unknown authority” in Go, “PKIX path building failed” in Java, “SSL: CERTIFICATE_VERIFY_FAILED” in Python, “unable to get local issuer certificate” in curl. Behind them are almost always the same three causes.
- The tool that makes all causes visible is
openssl.openssl s_clientis a TLS client without HTTP: it shows which certificate the server sends, with which chain, and why validation fails.openssl x509reads a single certificate. - Today you build a company PKI in miniature: a root CA, an issuing
intermediate CA, certificates for
shop.firma.labandapi.firma.laband a client certificate. That way you can introduce every fault deliberately.
Drill 1: Reading a certificate
# on netz
# Establish a TLS connection and summarize the essentials
# s_client openssl's TLS client
# -connect example.com:443 address and port
# -servername example.com name sent along in the handshake (SNI, Drill 3)
# -brief only a summary instead of all details
# </dev/null no input: openssl closes the connection right away
sudo ip netns exec client openssl s_client -connect example.com:443 -servername example.com -brief </dev/nullExpected (protocol and cipher may differ):
CONNECTION ESTABLISHED
Protocol version: TLSv1.3
Ciphersuite: TLS_AES_256_GCM_SHA384
Peer certificate: CN = example.com
Hash used: SHA256
Signature type: ECDSA
Verification: OK
Server Temp Key: X25519, 253 bits
DONE
Verification: OK means: the name matches, the
certificate is valid, and the chain leads to a CA that the system
trusts. What the certificate contains is shown by
openssl x509:
# on netz
# Pass the server certificate from the connection to openssl x509
# 2>/dev/null discard s_client's status messages
# x509 -noout read the certificate, but don't print it as text
# -subject -issuer for whom, from whom
# -dates valid from, valid until
# -ext subjectAltName the names it is valid for
sudo ip netns exec client openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null \
| openssl x509 -noout -subject -issuer -dates -ext subjectAltNameExpected (dates and issuing CA change):
subject=CN = example.com
issuer=C = US, O = SSL Corporation, CN = Cloudflare TLS Issuing ECC CA 3
notBefore=Nov 24 23:48:34 2026 GMT
notAfter=Feb 22 23:53:08 2027 GMT
X509v3 Subject Alternative Name:
DNS:example.com, DNS:*.example.com
For the name, only the Subject Alternative Name
(SAN) counts. The CN in the subject is a leftover; a
certificate with only a CN is rejected by all current clients. And the
chain the server sends along:
# on netz
# -showcerts print all certificates the server sends
# grep 's:\|i:' only subject (s:) and issuer (i:) of each certificate
sudo ip netns exec client openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null 2>/dev/null \
| grep -E '^ *[0-9] s:|^ *i:'Expected:
0 s:CN = example.com
i:C = US, O = SSL Corporation, CN = Cloudflare TLS Issuing ECC CA 3
1 s:C = US, O = SSL Corporation, CN = Cloudflare TLS Issuing ECC CA 3
i:C = US, O = SSL Corporation, CN = SSL.com TLS Transit ECC CA R2
2 s:C = US, O = SSL Corporation, CN = SSL.com TLS Transit ECC CA R2
i:C = US, O = SSL Corporation, CN = SSL.com TLS ECC Root CA 2022
3 s:C = US, O = SSL Corporation, CN = SSL.com TLS ECC Root CA 2022
i:C = GB, ST = Greater Manchester, L = Salford, O = Comodo CA Limited, CN = AAA Certificate Services
Read the chain from top to bottom: each i: (issuer) is
the s: of the next line. The server must send all
intermediate certificates; the client knows the root
itself, from its trust store. If a link is missing, validation fails –
today’s challenge.
Save your progress? Sign up for free
Drill 2: Your own small CA
Companies run their own CAs for internal services. The structure is always the same: a root CA that stays offline signs an intermediate CA, and the intermediate CA issues the server certificates. You build that now with elliptic curve keys (P-256), today’s standard.
# on netz
mkdir -p ~/ndbg/pki && cd ~/ndbg/pki
# Root CA: key and self-signed certificate in one step
# req -x509 a self-signed certificate right away instead of a request
# -newkey ec -pkeyopt ec_paramgen_curve:P-256 generate a new EC key on the P-256 curve
# -nodes don't encrypt the key with a password (lab)
# -keyout root.key -out root.crt key and certificate file
# -days 3650 valid for 10 years
# -subj "/CN=Firma Root CA" name, without prompting
# -addext …CA:TRUE may sign other certificates
# -addext keyUsage=…keyCertSign key only for signing certificates and revocation lists
openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:P-256 -nodes \
-keyout root.key -out root.crt -days 3650 -subj "/CN=Firma Root CA" \
-addext "basicConstraints=critical,CA:TRUE" -addext "keyUsage=critical,keyCertSign,cRLSign"The intermediate CA is created in two steps: a request (CSR) with its own key, then the root’s signature. The properties of a CA come from a small file:
# on netz
# Request of the intermediate CA
# req (without -x509) certificate signing request (CSR) instead of a certificate
# -out inter.csr request file
openssl req -newkey ec -pkeyopt ec_paramgen_curve:P-256 -nodes \
-keyout inter.key -out inter.csr -subj "/CN=Firma Issuing CA"
# Extensions of the intermediate CA: a CA, but no further CAs below it (pathlen:0)
printf 'basicConstraints=critical,CA:TRUE,pathlen:0\nkeyUsage=critical,keyCertSign,cRLSign\n' > inter.ext
# Root signs
# x509 -req -in inter.csr turn a request into a certificate
# -CA root.crt -CAkey root.key sign with the root's certificate and key
# -CAcreateserial create the serial number file root.srl
# -days 1825 5 years
# -extfile inter.ext take the extensions from the file
openssl x509 -req -in inter.csr -CA root.crt -CAkey root.key -CAcreateserial \
-days 1825 -out inter.crt -extfile inter.extExpected:
Certificate request self-signature ok
subject=CN = Firma Issuing CA
Now the server certificate for shop.firma.lab. The name
belongs in the SAN, and serverAuth allows its use as a
server:
# on netz
openssl req -newkey ec -pkeyopt ec_paramgen_curve:P-256 -nodes \
-keyout shop.key -out shop.csr -subj "/CN=shop.firma.lab"
# subjectAltName=DNS:… the name clients check against
# extendedKeyUsage=serverAuth usable only as a server certificate
printf 'subjectAltName=DNS:shop.firma.lab\nextendedKeyUsage=serverAuth\n' > shop.ext
# The intermediate CA signs, 90 days – as long as is common with public CAs
openssl x509 -req -in shop.csr -CA inter.crt -CAkey inter.key -CAcreateserial \
-days 90 -out shop.crt -extfile shop.ext
# For the server: own certificate plus intermediate CA in one file (“fullchain”)
cat shop.crt inter.crt > shop-full.crtThe same for api.firma.lab, and a client certificate for
Drill 4 – there with clientAuth:
# on netz
openssl req -newkey ec -pkeyopt ec_paramgen_curve:P-256 -nodes -keyout api.key -out api.csr -subj "/CN=api.firma.lab"
printf 'subjectAltName=DNS:api.firma.lab\nextendedKeyUsage=serverAuth\n' > api.ext
openssl x509 -req -in api.csr -CA inter.crt -CAkey inter.key -CAcreateserial -days 90 -out api.crt -extfile api.ext
cat api.crt inter.crt > api-full.crt
# Client certificate for a monitoring system
# extendedKeyUsage=clientAuth usable only to authenticate a client
openssl req -newkey ec -pkeyopt ec_paramgen_curve:P-256 -nodes -keyout client.key -out client.csr -subj "/CN=monitoring"
printf 'extendedKeyUsage=clientAuth\n' > client.ext
openssl x509 -req -in client.csr -CA inter.crt -CAkey inter.key -CAcreateserial -days 90 -out client.crt -extfile client.ext
# Root and intermediate CA together: nginx checks client certificates against this
cat root.crt inter.crt > ca-chain.crtThe test without a network – openssl verify checks a
chain made of files:
# on netz
# verify -CAfile root.crt trust this root
# -untrusted inter.crt intermediate certificate for building the chain (not as a trust anchor)
openssl verify -CAfile root.crt -untrusted inter.crt shop.crt
# without the intermediate CA
openssl verify -CAfile root.crt shop.crtExpected:
shop.crt: OK
CN = shop.firma.lab
error 20 at 0 depth lookup: unable to get local issuer certificate
error shop.crt: verification failed
Remember the second case: without the intermediate certificate, validation finds no path to the root. This exact message is what a client gets when the server doesn’t send the chain along.
Save your progress? Sign up for free
Drill 3: TLS for nginx and the SNI issue
Add two TLS servers on port 8443 to ~/ndbg/nginx.conf,
before the last closing brace }:
server {
listen 10.2.0.10:8443 ssl default_server;
server_name shop.firma.lab;
ssl_certificate /home/ubuntu/ndbg/pki/shop-full.crt;
ssl_certificate_key /home/ubuntu/ndbg/pki/shop.key;
location / { return 200 "shop über TLS\n"; }
}
server {
listen 10.2.0.10:8443 ssl;
server_name api.firma.lab;
ssl_certificate /home/ubuntu/ndbg/pki/api-full.crt;
ssl_certificate_key /home/ubuntu/ndbg/pki/api.key;
ssl_client_certificate /home/ubuntu/ndbg/pki/ca-chain.crt;
ssl_verify_client on;
location / { return 200 "api über mTLS, Client: $ssl_client_s_dn\n"; }
}
| Line | Meaning |
|---|---|
listen … ssl |
speak TLS on this port |
ssl_certificate |
certificate with intermediate CA (fullchain) |
ssl_certificate_key |
the matching private key |
ssl_client_certificate,
ssl_verify_client on |
only allow clients with a certificate from this CA (Drill 4) |
$ssl_client_s_dn |
subject of the client certificate – this way the application knows who is asking |
# on netz
# Check the configuration before nginx restarts
# -t test only
sudo ip netns exec web nginx -t -c /home/ubuntu/ndbg/nginx.conf
sudo systemctl restart web-nginx
# Without specifying a CA: curl doesn't know the company root
sudo ip netns exec client curl -sS --resolve shop.firma.lab:8443:10.2.0.10 https://shop.firma.lab:8443/
# With the company root as trust anchor
# --cacert root.crt trust this CA (instead of the system trust store)
sudo ip netns exec client curl -sS --cacert ~/ndbg/pki/root.crt --resolve shop.firma.lab:8443:10.2.0.10 https://shop.firma.lab:8443/Expected (curl’s explanation lines after the error message are omitted):
nginx: the configuration file /home/ubuntu/ndbg/nginx.conf syntax is ok
nginx: configuration file /home/ubuntu/ndbg/nginx.conf test is successful
curl: (60) SSL certificate problem: unable to get local issuer certificate
shop über TLS
Within the company you distribute root.crt into the
trust stores (/usr/local/share/ca-certificates/ and
update-ca-certificates on Ubuntu, ConfigMap or Secret in
Kubernetes). Then no client needs --cacert.
Now SNI: two certificates sit on the same address and the same port. How does nginx know which one to send? The client names the desired name in the first packet of the handshake – even before there is HTTP and a Host header:
# on netz
# without a server name
sudo ip netns exec client openssl s_client -connect 10.2.0.10:8443 -CAfile ~/ndbg/pki/root.crt </dev/null 2>/dev/null \
| grep -E '^subject=|Verify return code'
# with server name api.firma.lab
sudo ip netns exec client openssl s_client -connect 10.2.0.10:8443 -servername api.firma.lab -CAfile ~/ndbg/pki/root.crt </dev/null 2>/dev/null \
| grep -E '^subject=|Verify return code'Expected:
subject=CN = shop.firma.lab
Verify return code: 0 (ok)
subject=CN = api.firma.lab
Verify return code: 0 (ok)
Without SNI, nginx delivers the certificate of the
default_server. This leads to the failure that was
announced on Day 7 – -H Host is not enough for HTTPS:
# on netz
# Host header api, but URL with IP: curl sends no SNI, gets the shop certificate
sudo ip netns exec client curl -sS --cacert ~/ndbg/pki/root.crt -H 'Host: api.firma.lab' https://10.2.0.10:8443/Expected:
curl: (60) SSL: no alternative certificate subject name matches target host name '10.2.0.10'
curl checks the certificate against the name from the
URL, and SNI is missing entirely. With
--resolve api.firma.lab:8443:10.2.0.10, URL, SNI and Host
header all match at once.
Save your progress? Sign up for free
Drill 4: mTLS – the client shows its ID
# on netz
R='--resolve api.firma.lab:8443:10.2.0.10'
# without a client certificate
sudo ip netns exec client curl -sS --cacert ~/ndbg/pki/root.crt $R https://api.firma.lab:8443/
# with client certificate and key
# --cert client.crt certificate curl presents in the handshake
# --key client.key the matching private key
sudo ip netns exec client curl -sS --cacert ~/ndbg/pki/root.crt --cert ~/ndbg/pki/client.crt --key ~/ndbg/pki/client.key $R https://api.firma.lab:8443/Expected (the HTML of the first response is shortened):
<title>400 No required SSL certificate was sent</title>
api über mTLS, Client: CN=monitoring
A tricky detail: without a certificate, it’s not the handshake that
fails; nginx answers with HTTP 400 – curl exits with exit code 0. Other
servers abort the handshake (alert certificate required).
Which CAs the server accepts for client certificates is revealed by
openssl s_client … -servername api.firma.lab in the line
Acceptable client certificate CA names – useful when a
client sends the wrong certificate.
| Message (curl) | Cause | Proof with openssl |
|---|---|---|
unable to get local issuer certificate |
chain incomplete or CA unknown | s_client -showcerts: how many certificates arrive?
-CAfile correct? |
no alternative certificate subject name matches |
name doesn’t match the SAN, often missing SNI | x509 -ext subjectAltName,
s_client -servername |
certificate has expired |
notAfter exceeded |
x509 -dates, x509 -checkend 0 |
HTTP 400 No required SSL certificate /
alert certificate required |
mTLS: client certificate missing or from the wrong CA | Acceptable client certificate CA names |
Save your progress? Sign up for free
Exercise
Introduce the fault: a certificate that expires the moment it is issued, plus chain:
# on netz
cd ~/ndbg/pki
# -days 0 valid until the time of issue
openssl x509 -req -in shop.csr -CA inter.crt -CAkey inter.key -CAcreateserial -days 0 -out shop-alt.crt -extfile shop.ext
cat shop-alt.crt inter.crt > shop-alt-full.crt
# sed -i 's#alt#neu#' datei replace in the file
sed -i 's#pki/shop-full.crt#pki/shop-alt-full.crt#' ~/ndbg/nginx.conf
sudo systemctl restart web-nginxThe shop reports a TLS error. Find the message with curl, prove the cause with openssl on the running server and on the file, and fix it.
Solution
# on netz
sudo ip netns exec client curl -sS --cacert ~/ndbg/pki/root.crt --resolve shop.firma.lab:8443:10.2.0.10 https://shop.firma.lab:8443/
# On the running server: verification result and validity
sudo ip netns exec client openssl s_client -connect 10.2.0.10:8443 -servername shop.firma.lab -CAfile ~/ndbg/pki/root.crt </dev/null 2>/dev/null \
| grep 'Verify return code'
sudo ip netns exec client openssl s_client -connect 10.2.0.10:8443 -servername shop.firma.lab </dev/null 2>/dev/null \
| openssl x509 -noout -dates
# On the file: does it expire within the next 0 seconds?
# -checkend 0 exit code 1 if the certificate is no longer valid in 0 seconds
openssl x509 -in ~/ndbg/pki/shop-alt.crt -noout -checkend 0Expected (the times are those of your issuance):
curl: (60) SSL certificate problem: certificate has expired
Verify return code: 10 (certificate has expired)
notBefore=Nov 26 16:01:59 2026 GMT
notAfter=Nov 26 16:01:59 2026 GMT
Certificate will expire
notAfter is in the past. Fix: put the valid certificate
back, and the proof:
# on netz
sed -i 's#pki/shop-alt-full.crt#pki/shop-full.crt#' ~/ndbg/nginx.conf
sudo systemctl restart web-nginx
sudo ip netns exec client curl -sS --cacert ~/ndbg/pki/root.crt --resolve shop.firma.lab:8443:10.2.0.10 https://shop.firma.lab:8443/Expected:
shop über TLS
-checkend 2592000 (30 days in seconds) is the simplest
monitoring for certificate files that aren’t renewed automatically –
exit code 1 means: take action.
Save your progress? Sign up for free
Challenge
Introduce the fault:
# on netz
sed -i 's#pki/shop-full.crt#pki/shop.crt#' ~/ndbg/nginx.conf
sudo systemctl restart web-nginxThe ticket: “Since the certificate swap, the shop works perfectly in
the browser, but our Go service reports
x509: certificate signed by unknown authority and curl
unable to get local issuer certificate. The root CA is the
same for all of them.” Prove what the server is doing wrong, and why the
browser still doesn’t complain.
Solution
# on netz
sudo ip netns exec client curl -sS --cacert ~/ndbg/pki/root.crt --resolve shop.firma.lab:8443:10.2.0.10 https://shop.firma.lab:8443/
# Which certificates does the server send?
sudo ip netns exec client openssl s_client -connect 10.2.0.10:8443 -servername shop.firma.lab -CAfile ~/ndbg/pki/root.crt -showcerts </dev/null 2>/dev/null \
| grep -E '^ *[0-9] s:|Verify return code'Expected:
curl: (60) SSL certificate problem: unable to get local issuer certificate
0 s:CN = shop.firma.lab
Verify return code: 21 (unable to verify the first certificate)
The server sends only certificate 0; the intermediate certificate is
missing. With the root alone, no client finds the path (Drill 2,
openssl verify without -untrusted). Browsers
hide the error: they remember intermediate certificates from earlier
visits or fetch them via the address in the certificate (AIA). curl, Go,
Java and Python don’t do that. That is why “it works in the browser” is
no proof for TLS. Fix:
# on netz
sed -i 's#pki/shop.crt#pki/shop-full.crt#' ~/ndbg/nginx.conf
sudo systemctl restart web-nginx
sudo ip netns exec client openssl s_client -connect 10.2.0.10:8443 -servername shop.firma.lab -CAfile ~/ndbg/pki/root.crt -showcerts </dev/null 2>/dev/null \
| grep -E '^ *[0-9] s:|Verify return code'Expected:
0 s:CN = shop.firma.lab
1 s:CN = Firma Issuing CA
Verify return code: 0 (ok)
Save your progress? Sign up for free
Checkpoint
# on netz
# nginx uses the complete chains
grep -c 'full.crt' ~/ndbg/nginx.conf
# shop over TLS, api only with client certificate
sudo ip netns exec client curl -sS --cacert ~/ndbg/pki/root.crt --resolve shop.firma.lab:8443:10.2.0.10 https://shop.firma.lab:8443/
sudo ip netns exec client curl -sS --cacert ~/ndbg/pki/root.crt --cert ~/ndbg/pki/client.crt --key ~/ndbg/pki/client.key \
--resolve api.firma.lab:8443:10.2.0.10 https://api.firma.lab:8443/
# Server certificates still valid for at least 30 days
openssl x509 -in ~/ndbg/pki/shop.crt -noout -checkend 2592000
openssl x509 -in ~/ndbg/pki/api.crt -noout -checkend 2592000Expected:
2
shop über TLS
api über mTLS, Client: CN=monitoring
Certificate will not expire
Certificate will not expire
Clean up
Nothing. The PKI in ~/ndbg/pki and nginx with TLS stay –
on Day 10 you capture TLS handshakes, on Day 14 you need certificates in
the cluster. The key files are lab material; in real environments the
root key is kept offline, and server keys come from a secrets
manager.
Interview note
“curl reports unable to get local issuer certificate, in
the browser it works. What’s going on?” Almost always the server doesn’t
send the intermediate certificates along; browsers compensate for that,
libraries don’t. Proof:
openssl s_client -showcerts -servername … shows only
certificate 0. Bonus points: for the name only the SAN counts, without
SNI the server sends its default certificate (hence
--resolve instead of -H Host), and with mTLS a
missing client certificate can show up as HTTP 400 instead of a
handshake error.
Enterprise note
Expired certificates are among the most common self-inflicted outages
– even at large providers. Three countermeasures: renew automatically
(ACME, cert-manager in Kubernetes), monitor the lifetimes of all
certificates (blackbox_exporter with
probe_ssl_earliest_cert_expiry, or -checkend
for files) and distribute internal root CAs centrally instead of per
application. Public certificate lifetimes shrink step by step to 47 days
by 2029 – anyone still renewing by hand today will soon be doing it very
often.