TLS, HTTPS & certificates
The HTTP chapter said HTTPS is "HTTP inside TLS". This chapter opens that box: what TLS protects, what happens in the handshake, how certificates prove who a server is, how to get free ones from Let's Encrypt, and how to debug the certificate errors you will definitely run into.
What TLS gives you
- Confidentiality: traffic is encrypted, so Wi-Fi owners and ISPs see only scrambled bytes. They can still see which server you connect to, just not what you send.
- Integrity: every record carries a cryptographic check; if a byte is changed in transit, the connection fails.
- Authentication: the server proves it really is
demee.ccwith a certificate a trusted authority signed. Without this, you could be talking, very privately, to an attacker.
"SSL" is the old name. SSL and TLS 1.0/1.1 are deprecated and disabled in modern browsers; allow only TLS 1.2 and 1.3. "SSL certificate" and "TLS certificate" mean the same thing.
The handshake (TLS 1.3)
- ClientHello: the client lists the versions and ciphers it supports, sends its half of a key exchange (a key share), and names the site it wants in the SNI field. SNI is how one IP address can serve many HTTPS sites with different certificates.
- ServerHello: the server picks a cipher and sends its own key share. Both sides combine the shares (an ECDHE exchange, commonly X25519) into the same secret, which never crossed the wire. Everything after this is encrypted.
- Certificate + CertificateVerify: the server sends its certificate chain and a signature over the handshake made with the certificate's private key, proving it owns that certificate.
- Finished: the client checks the certificate (trusted chain, right hostname, not expired), both sides confirm the handshake wasn't tampered with, and the HTTP request goes out encrypted with fast symmetric keys (AES-GCM or ChaCha20-Poly1305).
TLS 1.3 needs one round trip for this, TLS 1.2 needed two. Because session keys come from throwaway key shares, stealing the server's private key later doesn't decrypt recorded traffic: forward secrecy.
Certificates, chains and CAs
A certificate (X.509, usually a .pem text file) binds a public key to names. What matters in it:
- Subject Alternative Name (SAN): the hostnames it's valid for. Browsers check this list, not the old Common Name. A wildcard
*.demee.cccoversstudy.demee.ccbut notdemee.ccora.b.demee.cc. - Issuer, validity (
notBefore/notAfter), the public key, and the issuer's signature over all of it.
Trust works as a chain: your leaf certificate is signed by an intermediate, which is signed by a root. Roots come pre-installed in the OS and browser (the trust store; on Ubuntu, the ca-certificates package). The server must send the leaf and the intermediates. Forgetting the intermediate is a classic mistake: browsers often cope, but curl, scripts and phones fail.
A Certificate Authority (CA) checks that you control a domain before signing. Industry rules keep cutting lifetimes: since 15 March 2026 a public certificate can be valid for at most 200 days, dropping to 100 days in 2027 and 47 days in 2029. Renewal has to be automated.
Let's Encrypt, ACME and certbot
Let's Encrypt is a free, non-profit CA. Its certificates last 90 days by default (it plans 45 days by 2028) and everything runs over the ACME protocol. To prove control of a domain, the client passes a challenge:
- HTTP-01: serve a token at
http://yourdomain/.well-known/acme-challenge/<token>. Needs port 80 open and DNS already pointing at the server. - DNS-01: create a TXT record at
_acme-challenge.yourdomain. Works for private servers, and is the only way to get a wildcard.
$ sudo apt install certbot python3-certbot-nginx
$ sudo certbot --nginx -d example.com -d www.example.com
# passes HTTP-01, gets the cert, edits the Nginx config, reloads Nginx
$ sudo certbot certificates # names and expiry dates
$ sudo certbot renew --dry-run # test renewal against the staging server
$ systemctl list-timers | grep certbot # the timer that renews automatically
Files land in /etc/letsencrypt/live/example.com/: fullchain.pem (leaf plus intermediates: what the server should send) and privkey.pem (never share or commit it). Configured by hand, Nginx looks like this:
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$host$request_uri; # send all HTTP to HTTPS
}
server {
listen 443 ssl;
server_name example.com www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
location / {
proxy_pass http://127.0.0.1:3000; # the app, plain HTTP on localhost
}
}
Caddy, Traefik and Kubernetes' cert-manager do the whole ACME dance automatically.
renew --dry-run) and monitor expiry yourself.Checking a certificate
$ echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null \
| openssl x509 -noout -subject -issuer -dates -ext subjectAltName
subject=CN = example.com
issuer=C = US, O = Let's Encrypt, CN = YR1
notBefore=Sep 1 08:14:02 2026 GMT
notAfter=Nov 30 08:14:01 2026 GMT
X509v3 Subject Alternative Name:
DNS:example.com, DNS:www.example.com
-servernamesets SNI; without it a multi-site server may hand you its default certificate. Without the pipe,s_clientprints the whole chain and ends withVerify return code: 0 (ok); any other code names the problem.openssl x509 -noout -checkend 2592000fails if the certificate expires within 30 days: handy in cron or CI.
$ curl -v https://example.com -o /dev/null
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384 / X25519 / RSASSA-PSS
* Server certificate:
* subject: CN=example.com
* expire date: Nov 30 08:14:01 2026 GMT
* subjectAltName: host "example.com" matched cert's "example.com"
* issuer: C=US; O=Let's Encrypt; CN=YR1
* SSL certificate verify ok.
(Wording varies slightly between versions, and the issuer depends on which intermediate signed the certificate.) In production, let monitoring watch expiry: Prometheus's blackbox exporter exposes probe_ssl_earliest_cert_expiry.
Common errors and fixes
| What you see | Cause | Fix |
|---|---|---|
certificate has expiredChrome: NET::ERR_CERT_DATE_INVALID | Renewal silently failed (rarely: the client's clock is wrong). | sudo certbot renew and reload. Find out why: port 80 blocked, DNS moved, or no reload after renewal (--deploy-hook "systemctl reload nginx"). |
no alternative certificate subject name matches target host nameChrome: NET::ERR_CERT_COMMON_NAME_INVALID | Valid certificate, wrong name: e.g. it covers example.com but not www, or the wrong server block answered. | Reissue with every name (-d example.com -d www.example.com); check server_name. |
self-signed certificateChrome: NET::ERR_CERT_AUTHORITY_INVALID | Signed by itself or by a private CA the client doesn't trust. | Use a public CA on public sites. Internally, trust your CA: curl --cacert ca.crt or update-ca-certificates. |
unable to get local issuer certificate | No chain to a trusted root: intermediate not sent, or an outdated CA bundle. | Use fullchain.pem, not cert.pem; update ca-certificates. |
wrong version number | TLS spoken to a plain-HTTP port. | Use http:// there, or connect to the TLS port (443). |
curl -k (--insecure) skips certificate checks. Fine for a quick test, never in scripts or pipelines: it throws away authentication, so anyone could impersonate the server.