# TLS and HTTPS: certificates without the mystery

Read a real certificate chain, see what the TLS handshake proves, recognise the four certificate errors that cause real outages, and how Let's Encrypt and mTLS fit in.

Course: [Networking for DevOps](https://devops.rajeev.pro/courses/networking), lesson 6. About 30 minutes, beginner. Source: https://devops.rajeev.pro/courses/networking/06-tls-and-https

## What you will learn

- Explain what TLS protects and what a certificate proves
- Read a certificate chain and its dates and names with openssl
- Recognise expired, wrong-host, self-signed and broken-chain errors and fix each
- Explain how Let's Encrypt issues certificates and what mTLS adds

> **Lab status.** **Verified lab:** `openssl s_client` and `curl` against github.com and the badssl.com test sites. The output shown is real. <br />**Code walkthrough:** issuing a certificate with Let's Encrypt and setting up mTLS (they need a public domain or a service mesh). <br />**Cloud lab:** none needed.

More outages come from expired and mismatched certificates than from attacks. Once you can read a certificate, these
problems take minutes, not hours.

## What TLS protects

TLS sits between TCP and HTTP and gives you three things: **encryption** (nobody on the path can read it),
**integrity** (nobody can change it unnoticed) and **authentication** (you're really talking to github.com). The
third is what certificates are for, and it's the part that breaks.

## Leaf, intermediate, root

> **Figure.** Four boxes. Bottom, the leaf sent by the server: CN github.com, SAN www.github.com, valid 30 October 2026 to 27 January 2027, signed by Sectigo Public Server Authentication CA DV E36. Above it the intermediate, signed by Sectigo Root E46. Above that the root, cross-signed by USERTrust ECC Certification Authority. Top, the trust store shipped with the OS.

```text
$ echo | openssl s_client -connect github.com:443 -servername github.com
 0 s:CN=github.com
   i:C=GB, O=Sectigo Limited, CN=Sectigo Public Server Authentication CA DV E36
 1 s:C=GB, O=Sectigo Limited, CN=Sectigo Public Server Authentication CA DV E36
   i:C=GB, O=Sectigo Limited, CN=Sectigo Public Server Authentication Root E46
 ...
Protocol: TLSv1.3
Verify return code: 0 (ok)
$ echo | openssl s_client -connect github.com:443 -servername github.com 2>/dev/null | openssl x509 -noout -subject -issuer -dates -ext subjectAltName
subject=CN=github.com
issuer=C=GB, O=Sectigo Limited, CN=Sectigo Public Server Authentication CA DV E36
notBefore=Oct 30 00:00:00 2026 GMT
notAfter=Jan 27 23:59:59 2027 GMT
X509v3 Subject Alternative Name:
    DNS:github.com, DNS:www.github.com
```

(Trimmed.) `s:` is the subject, `i:` who signed it. The leaf is valid for under three months, and only for the names
in its **SAN** list. Your machine trusts it because each signature leads up to a root in its trust store.

## The handshake

> **Figure.** Sequence after TCP. Client Hello with ciphers, key share, SNI github.com and ALPN h2. Server Hello with TLS_AES_128_GCM_SHA256 and its key share, then the encrypted certificate chain, signature and Finished. The client checks chain, dates and hostname and sends Finished. Then encrypted GET / HTTP/2 and 200 OK.

```text
$ curl -sv https://github.com -o /dev/null 2>&1 | grep -E 'SSL connection|ALPN|expire date'
* ALPN: curl offers h2,http/1.1
* SSL connection using TLSv1.3 / TLS_AES_128_GCM_SHA256 / x25519 / id-ecPublicKey
* ALPN: server accepted h2
*   expire date: Jan 27 23:59:59 2027 GMT
```

TLS 1.3 needs one round trip after TCP's handshake. The client sends the site name (**SNI**) in the clear so the
server can pick the right certificate; that's how one IP serves certificates for thousands of names. ALPN agrees on
HTTP/2 at the same time.

## Four errors you will meet

> **Figure.** Four cards with real curl errors. Expired: certificate has expired; renew and automate renewal. Wrong host: no alternative certificate subject name matches; the name isn't in the SAN list. Self-signed: no trusted CA vouches for it; use a real CA or add your own CA to the trust store. Broken chain: self-signed certificate in chain; an untrusted root or a missing intermediate, so serve the full chain.

```text
$ curl -sS https://expired.badssl.com -o /dev/null
curl: (60) SSL certificate OpenSSL verify result: certificate has expired (10)
$ curl -sS https://wrong.host.badssl.com -o /dev/null
curl: (60) SSL: no alternative certificate subject name matches target hostname 'wrong.host.badssl.com'
$ curl -sS https://self-signed.badssl.com -o /dev/null
curl: (60) SSL certificate OpenSSL verify result: self-signed certificate (18)
$ curl -sS https://untrusted-root.badssl.com -o /dev/null
curl: (60) SSL certificate OpenSSL verify result: self-signed certificate in certificate chain (19)
```

Read the reason, not just "SSL error". The most common in real life is **expired**, so monitor expiry: Prometheus'
blackbox exporter exposes `probe_ssl_earliest_cert_expiry`, and an alert at 14 days left catches failed renewals.

## Certificates from Let's Encrypt

> **Figure.** Left: the ACME HTTP-01 flow in five steps. certbot asks for a certificate for shop.example.com, Let's Encrypt replies with a token, certbot serves it at /.well-known/acme-challenge/token on port 80, Let's Encrypt fetches it over the internet, and on a match issues a 90-day certificate, renewed at about 60 days. Right: mutual TLS between an orders service and a payments service, both with certificates signed by an internal CA.

**Let's Encrypt** issues free certificates to anyone who can prove control of a name, using the **ACME** protocol.
Certificates last 90 days on purpose, so renewal has to be automated: `certbot` on a server, **cert-manager** in
Kubernetes, or let **AWS Certificate Manager** issue and renew certificates for your load balancers. The DNS-01
challenge (a TXT record instead of a file) works for wildcard names and servers that aren't public.

## Mutual TLS

Normal HTTPS proves only the server's identity. With **mutual TLS (mTLS)** the client presents a certificate too,
so the server knows exactly which service is calling. It's how service meshes such as Istio and Linkerd secure
traffic between Pods, with certificates issued and rotated automatically by an internal CA.

## Mission: Audit certificates you depend on

For three HTTPS sites your work depends on, record the issuer, expiry date, SAN names and TLS version. Then work out the days left for each and decide which one would need attention first.

- You need: openssl and curl
- You'll have: ~/certs.txt

```sh
$ for h in github.com example.com www.cloudflare.com; do
    echo "== $h"
    echo | openssl s_client -connect $h:443 -servername $h 2>/dev/null \
      | openssl x509 -noout -issuer -enddate -ext subjectAltName
    end=$(echo | openssl s_client -connect $h:443 -servername $h 2>/dev/null | openssl x509 -noout -enddate | cut -d= -f2)
    echo "days left: $(( ($(date -d "$end" +%s) - $(date +%s)) / 86400 ))"
  done > ~/certs.txt
```

Anything under 30 days on a certificate that should auto-renew is worth checking today.

## Try it

- Run `openssl s_client` against github.com and read the chain.
- Print the certificate's subject, issuer, dates and SAN list with `openssl x509`.
- Call the four badssl.com test sites with curl and read each error.
- Check how many days a certificate you own has left.
- Do the mini mission and keep `~/certs.txt`.

## Quiz

1. curl says "no alternative certificate subject name matches target hostname". What is wrong?
   - The certificate expired
   - **The name you connected to is not in the certificate's SAN list** (answer)
   - The server is down
   - TLS 1.3 isn't supported
   Why: The certificate is valid but for other names. Add the name to the certificate or connect with the right name.

2. Browsers trust your certificate but some clients fail with an untrusted chain. The most likely cause?
   - **The server sends only the leaf certificate, not the intermediate** (answer)
   - The private key is wrong
   - DNS is broken
   - The certificate is too new
   Why: Browsers often cache intermediates; curl, Java and other clients don't. Serve the full chain.

3. What does mutual TLS add over normal HTTPS?
   - Faster connections
   - **The client also presents a certificate, so the server knows which client is calling** (answer)
   - It removes the need for a CA
   - It encrypts DNS
   Why: Normal TLS proves only the server's identity. mTLS proves both, which is why it's used between internal services.

4. Why is curl -k a bad fix for a certificate error in production?
   - It's slower
   - **It turns off verification, so you could be talking to anyone** (answer)
   - It only works on Linux
   - It disables HTTP/2
   Why: Encryption without verification protects you from no one, because an attacker can present their own certificate.

## Resources

- [How HTTPS works (comic)](https://howhttps.works/) (howhttps.works)
- [badssl.com test sites](https://badssl.com/) (badssl.com)
- [Let's Encrypt, how it works](https://letsencrypt.org/how-it-works/) (Let's Encrypt)
- [SSL Labs server test](https://www.ssllabs.com/ssltest/) (Qualys)
