TLS & certificates
Explain the TLS 1.3 handshake and verify a server's certificate chain.
By Invicly Tech, , in Computer Networking. About 12 minutes to read.
In short
TLS encrypts and authenticates connections, turning HTTP into HTTPS. During the handshake the client and server agree on keys with ephemeral Diffie-Hellman and the server proves its identity with a certificate signed by a trusted certificate authority. TLS 1.3 completes this in a single round trip.
Explained simply
When you see the padlock in your browser, TLS (Transport Layer Security) is at work. It does three jobs for a connection: it keeps the data secret (encryption), it detects any change on the way (integrity), and it proves you are really talking to the site you asked for (authentication).
Encryption needs a shared secret key, but the two sides have never met. TLS solves this with a clever math trick called Diffie-Hellman: each side sends a public value, and both can compute the same secret, while someone watching the wire cannot. A fresh secret is made for every connection.
Proving identity is the job of certificates. A certificate is a small file that says: this public key belongs to example.com. It is signed by a certificate authority (CA), a company your device already trusts. If the signature checks out and the name matches, the browser trusts the connection.
Most TLS problems in real life are not about math. They are about certificates: an expired one, a name that does not match, or a missing middle certificate in the chain. Knowing how to inspect a certificate from the command line will solve most of these in minutes.
Press Play on a diagram to walk through it one step at a time.
- Client to Server: ClientHello: versions, ciphers, key share, SNI, ALPN1ClientHello: versions, ciphers, key share, SNI, ALPN
- Server to Client: ServerHello: chosen cipher and key share2ServerHello: chosen cipher and key share
- Server to Client: Encrypted: certificate, CertificateVerify, Finished3Encrypted: certificate, CertificateVerify, Finished
- Client to Server: Client verifies chain and hostname, sends Finished4Client verifies chain and hostname, sends Finished
- Client to Server: Encrypted application data5Encrypted application data
- 1Receive chain
Leaf plus intermediates from the server
- 2Build path to a root
Root must be in the local trust store
- 3Verify signatures
Each certificate signed by the next
- 4Check dates and usage
Not expired, allowed for TLS servers
- 5Match hostname
Name must be in the SAN list
- 6Check CertificateVerify
Proves the server holds the private key
In depth
Inside the TLS 1.3 handshake
The client starts with ClientHello. It lists the TLS versions and cipher suites it supports, includes a random value, and already sends a key share: its public Diffie-Hellman value for a group it guesses the server likes, typically X25519. Modern browsers and servers also offer the hybrid post-quantum group X25519MLKEM768. The extensions carry the hostname (SNI) and the application protocols it wants (ALPN).
The server answers with ServerHello, choosing a cipher suite and sending its own key share. From that moment both sides can compute the handshake keys, so everything after ServerHello is encrypted: EncryptedExtensions (including the chosen ALPN), Certificate, CertificateVerify (a signature over the handshake, proving the server owns the private key) and Finished (a check that nobody tampered with any handshake message).
If the client guessed a group the server does not accept, the server sends HelloRetryRequest and the handshake costs one extra round trip.
- TLS 1.3 cipher suites: TLS_AES_128_GCM_SHA256, TLS_AES_256_GCM_SHA384, TLS_CHACHA20_POLY1305_SHA256
- RSA key exchange and static keys were removed: every TLS 1.3 handshake has forward secrecy
- The certificate is encrypted in 1.3, unlike in 1.2
How a chain of trust is checked
Root CA keys are precious, so roots rarely sign server certificates directly. A root signs an intermediate CA certificate, and the intermediate signs your server (leaf) certificate. Your device ships with a trust store of a few hundred roots. The server must send its leaf plus the intermediates; it should not send the root, which the client already has.
The client checks, for every link: the signature is valid with the issuer's public key, the dates are current, and the issuer is allowed to act as a CA. Then it checks the leaf matches the hostname. Browsers use only the Subject Alternative Name (SAN) list for this and ignore the old Common Name field. A wildcard like *.example.com covers api.example.com but not example.com itself or a.b.example.com.
The most common failure is a server that sends only the leaf. Browsers sometimes recover by fetching the intermediate themselves, but curl, Java and many apps fail with unable to get local issuer certificate.
- Intermediate CA
- Signed by the root
- Sent by the server
- Signs leaf certificates
- Leaf certificate
- Signed by the intermediate
- SAN: example.com, www.example.com
- Holds the server's public key
SNI, ALPN and Encrypted Client Hello
One IP address often hosts hundreds of sites, and each needs its own certificate. The server must pick the certificate before encryption starts, but the HTTP Host header only arrives later. SNI (Server Name Indication) solves this by putting the hostname in the ClientHello. Load balancers and CDNs also use SNI to route connections. Without SNI, a server returns its default certificate, which is why openssl s_client needs -servername.
ALPN, also in ClientHello, lists protocols like h2 and http/1.1. The server picks one, so HTTP/2 is agreed without an extra round trip.
Because SNI is sent in clear text, anyone on the path can see which site you visit. Encrypted Client Hello (ECH) hides the real hostname inside an encrypted inner ClientHello, using a public key published in the site's HTTPS DNS record. Major browsers and some CDNs support it, but most servers do not use it yet.
Session resumption and 0-RTT
A full handshake costs one round trip plus some CPU work for signatures. After a handshake, a TLS 1.3 server can send a session ticket. On the next connection the client presents the ticket as a pre-shared key (PSK), skipping the certificate exchange.
With 0-RTT (early data), the client can even send its first request together with the ClientHello, using keys derived from the ticket. This saves a full round trip, which users on distant or mobile networks feel. The catch: early data can be replayed. An attacker who records the first packet can resend it, and the server may process the request twice. So servers should only accept 0-RTT for safe, idempotent requests like GET, and many turn it off entirely. Early data also lacks full forward secrecy, because it depends on the ticket key.
- 1 round trip
- Certificate checked
- Full forward secrecy
- Always safe
- 1 round trip
- No certificate sent
- Less CPU
- Ticket from last visit
- 0 round trips for first request
- Can be replayed
- Only idempotent requests
- Often disabled
Certificates in real operations
Certificate lifetimes keep getting shorter. Public certificates issued from 15 March 2026 may be valid for at most 200 days, and the CA/Browser Forum schedule cuts this to 100 days in 2027 and 47 days in 2029. Let's Encrypt certificates already last 90 days. Manual renewal does not scale; automate it with the ACME protocol using tools like certbot, acme.sh, or a built-in client in Caddy, Traefik or cert-manager in Kubernetes.
Monitor expiry from outside, because an expired certificate takes down the site for everyone at once. Keep private keys readable only by the service (mode 600), and never put them in Git.
TLS can also authenticate the client. In mutual TLS (mTLS) the server asks for a client certificate and checks it against its own CA. Service meshes and internal APIs use mTLS so that every service proves its identity to every other.
- Allow TLS 1.2 and 1.3 only; 1.0 and 1.1 are deprecated
- fullchain.pem = leaf + intermediates; that is what servers should send
- openssl x509 -checkend 1209600 fails if a cert expires within 14 days
Learn it in 4 short steps
About 45m each. Track them with a timer in your plan.
- 1Trace the TLS 1.3 handshake~45m
- 2Explain certificates and chains of trust~45m
- 3Explain SNI and ALPN~45m
- 4Inspect a chain with openssl s_client~45m
Key points
- TLS 1.3 needs one round trip; resumed sessions can send 0-RTT data.
- Certificates chain from the server certificate through intermediates to a root CA.
- SNI tells the server which hostname the client wants before encryption starts.
- Forward secrecy means a stolen server key cannot decrypt past sessions.
Key terms
Tap a card to flip it. 0/5 known
Examples
openssl s_client -connect example.com:443 -servername example.com </dev/null \
| openssl x509 -noout -subject -issuer -dates
openssl s_client -connect example.com:443 -tls1_3 -brief </dev/null
curl -vI https://example.com 2>&1 | grep -E 'SSL|TLS|subject|issuer'Diagnosing unable to get local issuer certificate
Browsers open the site, but a Python script and curl fail. Step 1: show the chain the server sends with -showcerts and count the certificates. Only one (depth 0) means the intermediate is missing. Step 2: check the verify result line. Step 3: fix the server to use the full chain file (on nginx, ssl_certificate should point to fullchain.pem, not cert.pem), reload, and test again.
openssl s_client -connect shop.example.com:443 \
-servername shop.example.com -showcerts </dev/null 2>/dev/null \
| grep -E '^ *[0-9] s:|Verify return code'
# 0 s:CN=shop.example.com
# Verify return code: 21 (unable to verify the first certificate)
# After switching nginx to fullchain.pem:
# 0 s:CN=shop.example.com
# 1 s:C=US, O=Let's Encrypt, CN=R11
# Verify return code: 0 (ok)Checking names and expiry of a certificate
A user sees a name mismatch warning on api.example.com. Step 1: pull the certificate and print its SAN list and dates. Step 2: if api.example.com is not in the list (and no wildcard covers it), the certificate needs reissuing with that name. Step 3: use -checkend in monitoring to warn two weeks before expiry.
echo | openssl s_client -connect api.example.com:443 \
-servername api.example.com 2>/dev/null \
| openssl x509 -noout -subject -dates -ext subjectAltName
# notAfter=Jan 30 12:00:00 2027 GMT
# X509v3 Subject Alternative Name:
# DNS:example.com, DNS:www.example.com (api is missing)
echo | openssl s_client -connect api.example.com:443 -servername api.example.com 2>/dev/null \
| openssl x509 -noout -checkend 1209600 || echo 'expires within 14 days'Quick reference
- openssl s_client -connect host:443 -servername host shows the handshake and chain
- -showcerts prints every certificate the server sent; -brief prints version and cipher only
- openssl x509 -noout -ext subjectAltName lists the names a cert is valid for
- Verify return code 0 (ok) is good; 20 or 21 usually means a missing intermediate
- TLS 1.3: 1 round trip; 0-RTT only for idempotent requests
- Wildcard *.example.com does not cover example.com or a.b.example.com
- Serve fullchain.pem (leaf + intermediates), never the root alone or the leaf alone
- Automate renewal with ACME; public certs issued from March 2026 last at most 200 days
Common mistakes
- Serving the leaf certificate without its intermediates.
- Letting certificates expire; automate renewal with ACME.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Find the TLS version, cipher suite, issuer and expiry date of a website you use daily.
- Exercise 2medium
Show that a server refuses TLS 1.1 but accepts TLS 1.2 and 1.3.
- Exercise 3hard
Create your own small CA, issue a server certificate for localhost with a SAN, and make curl trust it.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Is SSL the same as TLS?
- TLS is the successor to SSL, and all SSL versions are broken and disabled. People still say SSL certificate out of habit, but the protocol in use is TLS 1.2 or 1.3.
- Does HTTPS hide which websites I visit?
- It hides the page path, content and headers. The hostname is still visible in DNS lookups and in the SNI field, unless both encrypted DNS and Encrypted Client Hello are used.
- Why do certificates expire so quickly now?
- Short lifetimes limit the damage from a stolen key or a wrongly issued certificate. With ACME automation, renewal costs nothing, so the industry is moving toward 47-day certificates by 2029.
- What is the difference between a self-signed certificate and a CA-signed one?
- Both encrypt the same way. A self-signed certificate is not vouched for by any CA your device trusts, so clients warn unless you install it or your own CA manually.