Sub topic 1
How TLS and certificates protect HTTPS
Prerequisites
This chapter assumes you can use a Linux shell with sudo, and that you know the basics of HTTP and DNS (what a request, a response, and an A record are). You do not need a server for this chapter. The commands below only connect to a public site, so you can run them from any machine that has openssl installed.
What HTTPS adds to HTTP
HTTP sends requests and responses as plain text. Anyone on the network path, such as a Wi-Fi operator, an internet provider, or a compromised router, can read or change that traffic. HTTPS is HTTP carried inside a TLS connection. TLS (Transport Layer Security) is a protocol that wraps a TCP connection and gives you three properties:
- Confidentiality. Data is encrypted, so an observer sees only ciphertext.
- Integrity. Each record carries an authentication tag, so any change in transit is detected and the connection is dropped.
- Authentication. The client can check that it is talking to the real owner of the domain name, not an impostor.
The first two properties come from cryptography that both sides can compute. The third is harder, and it is the reason certificates exist. Encryption alone is not enough: if you encrypt traffic to an attacker who is pretending to be your bank, the attacker still reads everything. The client needs a way to tie a name such as example.com to a specific key.
TLS is the successor to SSL. All SSL versions and TLS 1.0 and 1.1 are deprecated. Today you should serve TLS 1.2 and TLS 1.3. People still say "SSL certificate", but the certificate format is the same in both worlds.
Two kinds of cryptography
TLS combines two families of algorithms.
| Kind | Examples | Strength | Weakness | Used for |
|---|---|---|---|---|
| Asymmetric (public key) | RSA, ECDSA, X25519 | Two parties can agree on secrets without sharing one beforehand. A signature can be checked by anyone. | Slow | Authentication and key exchange |
| Symmetric | AES-GCM, ChaCha20-Poly1305 | Fast, suitable for bulk data | Both sides need the same secret key | Encrypting the traffic |
An asymmetric key pair has a private key, which stays on your server, and a public key, which you can give to anyone. A signature made with the private key can be verified with the public key. This is how a server proves it holds the key without revealing it.
TLS uses the slow asymmetric operations only during the handshake. They authenticate the server and produce a shared secret. After that, both sides use fast symmetric encryption with keys derived from that secret.
The TLS handshake
The handshake happens after the TCP connection opens and before any HTTP data is sent. In TLS 1.3 it takes one round trip.
The steps that matter for certificates are these:
- ClientHello. The client lists the TLS versions and cipher suites it supports. It also sends its half of a key exchange and the hostname it wants, in a field called SNI (Server Name Indication). SNI lets one IP address host many sites, because the server can pick the right certificate before it has seen any HTTP headers.
- ServerHello and certificate. The server picks a cipher suite, sends its half of the key exchange, and sends its certificate chain. From here on, TLS 1.3 encrypts the rest of the handshake.
- CertificateVerify. The server signs a hash of the handshake so far with its private key. This proves it holds the key that matches the public key in the certificate. Without this step, anyone could replay a copy of a public certificate.
- Client checks. The client validates the certificate (see below). If any check fails, the browser shows a warning or
curlexits with an error. - Finished. Both sides confirm that they derived the same keys, and encrypted application data begins.
Key exchange in modern TLS uses ephemeral Diffie-Hellman. Each connection generates fresh temporary keys, so recording traffic today and stealing the server's private key later does not let an attacker decrypt it. This property is called forward secrecy. It is mandatory in TLS 1.3.
What a certificate contains
A certificate is a signed document that binds a public key to one or more names. Web certificates follow the X.509 format. The fields you will see most often are:
| Field | Meaning |
|---|---|
| Subject | The entity the certificate is for. For Let's Encrypt this is only a common name (CN). |
| Subject Alternative Name (SAN) | The list of DNS names the certificate is valid for. Browsers check this field, not the CN. |
| Issuer | The certificate authority (CA) that signed it. |
| Not Before / Not After | The validity period. |
| Public key | The server's public key and its algorithm. |
| Key Usage and Extended Key Usage | What the key may be used for, such as TLS server authentication. |
| Signature | The issuer's signature over all the fields above. |
The signature is what makes the document trustworthy. A client does not trust the certificate because it says "I am example.com". It trusts it because a CA it already trusts signed it. The next chapter covers how that trust is organized into a chain. For now, remember that the client validates four things:
- The signature chains up to a CA the client trusts.
- The current time falls between Not Before and Not After.
- The hostname the user asked for appears in the SAN list. A wildcard such as
*.example.commatches exactly one label, so it coverswww.example.combut notexample.comora.b.example.com. - The certificate has not been revoked, and its key usage allows server authentication.
Inspecting a real certificate
You can watch all of this with openssl. The first command opens a TLS connection and prints the chain the server sent. The second command decodes the first certificate.
echo | openssl s_client -connect example.com:443 -servername example.com -showcerts 2>/dev/null \
| openssl x509 -noout -subject -issuer -dates -ext subjectAltNameThe -servername option sets SNI. The echo | closes the connection immediately after the handshake. The -ext subjectAltName option needs OpenSSL 1.1.1 or later. On older versions, use -text and look for the "Subject Alternative Name" line.
To see the negotiated protocol and cipher suite, run:
echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null \
| grep -E 'Protocol|Cipher|Verification'You should see a line such as Protocol : TLSv1.3 and a verification result. A good result means OpenSSL built a path to a trusted root.
Common failure modes
Knowing what the client checks tells you what the error messages mean.
| Symptom | Likely cause |
|---|---|
| Name mismatch (`NET::ERR_CERT_COMMON_NAME_INVALID`, `no alternative certificate subject name matches`) | The requested hostname is not in the SAN list, or the server returned a default certificate because SNI did not match any `server_name`. |
| Expired certificate | Not After has passed. Usually a failed renewal. |
| Unknown issuer or incomplete chain | The server sent only the leaf certificate and left out the intermediate CA. Some clients cannot fetch it. |
| Handshake failure or "no protocols available" | The client and server share no TLS version or cipher suite, for example an old client against a server limited to TLS 1.2 and 1.3. |
| Certificate not yet valid | Not Before is in the future, or the client's clock is wrong. |
Clock errors are easy to overlook. A server or client whose time is off by days will reject valid certificates, so keep time synchronized with chrony or systemd-timesyncd.
Why the private key matters
The public certificate is not secret. Your server hands it to every visitor. The private key is the only secret, and the whole scheme depends on it staying private. Anyone who obtains it can impersonate your site to any client that trusts the certificate, until the certificate expires or is revoked. Later chapters cover where Certbot stores the key and how to restrict access to it.
Where Let's Encrypt fits in
Historically, a certificate cost money and required manual paperwork, so many sites stayed on plain HTTP. Let's Encrypt is a free CA that issues certificates automatically. Its certificates are short-lived (90 days), which limits the damage from a leaked key and pushes you to automate renewal. The remaining chapters build that automation step by step: how the CA proves you own a domain, how to get a certificate with Certbot, how to configure nginx, and how to renew without human involvement.
Exercise
A browser reports that the certificate for www.example.com is invalid, but openssl s_client shows a certificate that is signed by a trusted CA and has not expired. Name two other things from this chapter that you should check in the certificate, and the openssl option that makes a multi-site server return the right certificate.
Show answer
Check the Subject Alternative Name list to confirm that www.example.com is in it (a certificate for only example.com does not match, and a wildcard *.example.com does not match the bare domain). Also check that the server sent the full chain including the intermediate CA, and that the client's clock is correct. To get the correct certificate from a server that hosts several sites, pass -servername www.example.com so that openssl sends SNI in the ClientHello.
Exercise
Why does TLS use asymmetric cryptography only for the handshake and then switch to symmetric encryption for the data?
Show answer
Asymmetric operations such as signatures and key exchange are slow, but they let two parties authenticate each other and agree on a secret without having shared one before. Symmetric ciphers such as AES-GCM are much faster for bulk data, so TLS uses the asymmetric step once to produce a shared secret, then derives symmetric keys from it for the rest of the connection.