How HTTPS and TLS Really Work: A Practical Guide
You type https:// and the browser shows a padlock. But what actually happens behind that padlock? Many developers treat HTTPS as a black box—until something breaks, like a certificate error, a mixed-content warning, or a mysterious handshake failure. This article explains how HTTPS and TLS really work, from the initial handshake to the encrypted data exchange, and gives you practical steps to verify and debug TLS in your own projects.
What HTTPS Adds to HTTP
HTTP sends data in plain text. Anyone on the network path—your ISP, a coffee-shop Wi-Fi operator, or a malicious router—can read or modify it. HTTPS wraps HTTP inside TLS (Transport Layer Security), which provides three guarantees:
- Confidentiality: Data is encrypted so eavesdroppers cannot read it.
- Integrity: Data cannot be altered in transit without detection.
- Authentication: The server proves its identity via a certificate, so you know you are talking to the real site.
These guarantees come from a combination of asymmetric cryptography (for the handshake) and symmetric cryptography (for the actual data).
The TLS Handshake in Plain English
The handshake is the negotiation that happens before any HTTP data is sent. Modern TLS 1.3 simplifies it to one round trip, but the core ideas remain. Here is a simplified sequence:
- Client Hello: The client says hello, lists supported TLS versions, cipher suites, and a random value.
- Server Hello: The server picks a TLS version and cipher suite, sends its own random value, and presents its certificate chain.
- Certificate verification: The client checks the certificate against trusted root CAs, verifies the domain name, and checks expiration.
- Key exchange: Using the certificate's public key (or a Diffie-Hellman exchange), both sides derive a shared secret without ever sending it over the wire.
- Finished: Both sides send a MAC over the handshake transcript to confirm nothing was tampered with. Encrypted HTTP can now flow.
In TLS 1.3, the key exchange and server certificate are sent in the first flight, reducing latency. The client can also send early data (0-RTT) in some cases, though that has replay risks.
Certificates: The Trust Anchor
A certificate binds a public key to a domain name and is signed by a Certificate Authority (CA). Your browser and OS ship with a trust store of root CAs. When a server sends its certificate chain, the client builds a path from the server certificate up to a trusted root.
Common certificate problems include:
- Expired certificate: The
notAfterdate has passed. - Hostname mismatch: The certificate's Subject Alternative Name (SAN) does not include the domain you requested.
- Untrusted issuer: The chain leads to a CA not in the trust store, often because an intermediate certificate is missing.
- Revoked certificate: The CA has revoked it (checked via OCSP or CRL).
Let's Encrypt made free certificates mainstream, and automation via ACME protocol means you rarely need to manage renewals manually.
Encryption: Asymmetric for Setup, Symmetric for Speed
Asymmetric cryptography (RSA or ECDSA) is slow but solves key distribution. Symmetric cryptography (AES-GCM or ChaCha20-Poly1305) is fast but requires both sides to share a key. TLS uses asymmetric crypto only during the handshake to establish a shared session key, then switches to symmetric encryption for the data.
Forward secrecy is a key property: with ephemeral Diffie-Hellman (DHE or ECDHE), the session key is derived per connection and discarded. Even if the server's private key leaks later, past sessions cannot be decrypted.
Practical: Inspect a TLS Connection
You can examine the handshake and certificate with OpenSSL. This is invaluable when debugging.
openssl s_client -connect example.com:443 -servername example.com -showcerts
Look for the certificate chain, the negotiated protocol (e.g., TLSv1.3), and the cipher suite. To check expiration quickly:
echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | openssl x509 -noout -dates -subject
For a more user-friendly view, browser developer tools show security details under the Security tab. Command-line tools like curl -vI https://example.com also print TLS details.
Common TLS Misconfigurations and Fixes
| Problem | Symptom | Fix |
|---|---|---|
| Missing intermediate certificate | Works in some browsers, fails in others | Include the full chain in server config |
| Weak protocols enabled | Security scanners flag TLS 1.0/1.1 | Disable old TLS versions; require TLS 1.2+ |
| Mixed content | Padlock warns or breaks | Load all subresources over HTTPS |
| HSTS not enabled | Downgrade attacks possible | Add Strict-Transport-Security header |
Use tools like SSL Labs to audit your configuration. For Nginx, a modern config might look like:
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256';
ssl_prefer_server_ciphers off;
ssl_certificate /path/fullchain.pem;
ssl_certificate_key /path/privkey.pem;
Always include the full chain (server cert + intermediates) in fullchain.pem.
Debugging TLS Issues in Development
When you see UNABLE_TO_VERIFY_LEAF_SIGNATURE or certificate has expired, do not disable verification in production. Instead:
- Check the system time—clock skew can cause false expiration errors.
- Verify the certificate chain with
openssl verify -CAfile chain.pem cert.pem. - Use
openssl s_client -connect host:port -servername hostto see what the server actually sends. - For local development, use
mkcertto generate locally trusted certificates.
FAQ
Is TLS the same as SSL?
SSL is the predecessor to TLS. SSL 2.0 and 3.0 are deprecated and insecure. When people say SSL today, they usually mean TLS. Always use TLS 1.2 or 1.3.
Do I need a certificate for localhost?
Browsers treat localhost as a secure context even over HTTP, but some APIs (like service workers or geolocation) require HTTPS. For realistic testing, generate a locally trusted certificate with tools like mkcert.
How does HTTPS protect against man-in-the-middle attacks?
The certificate verification step ensures the client is talking to a server that holds a private key matching a certificate signed by a trusted CA for that domain. An attacker cannot forge that without compromising a CA or the server's private key. The handshake's Finished messages also detect tampering.
If you need to inspect or analyze server logs for TLS errors, the Nginx Log Analyzer can help you spot handshake failures and certificate issues quickly.