How HTTPS and TLS Really Work: Handshake, Certificates, and Encryption
Why HTTPS Matters (and Why You Should Care)
Every time you visit a website, you're trusting that the data you send and receive is private and unaltered. But without HTTPS, that trust is misplaced: anyone on the network path—coffee shop Wi‑Fi, your ISP, or a malicious actor—can read or modify your traffic. HTTPS solves this by layering HTTP over TLS (Transport Layer Security), providing encryption, data integrity, and authentication.
In this article, we'll break down how HTTPS and TLS really work, from the handshake to certificates, so you can understand the mechanics and avoid common pitfalls.
The TLS Handshake: Setting Up a Secure Connection
Before any HTTP data is exchanged, the client and server perform a TLS handshake to agree on encryption algorithms, verify identities, and establish shared keys. The exact steps vary by TLS version, but the core ideas are similar.
TLS 1.2 Handshake (Simplified)
- Client Hello: The client sends supported cipher suites, TLS version, and a random value.
- Server Hello: The server picks a cipher suite, sends its own random value, and provides its certificate.
- Certificate Verification: The client checks the certificate's validity, chain of trust, and hostname match.
- Key Exchange: Using algorithms like ECDHE, both parties derive a shared secret (pre‑master secret).
- Finished: Both sides send a MAC over the handshake to confirm integrity; encrypted communication begins.
TLS 1.3 Handshake (Faster and Safer)
TLS 1.3 reduces round trips and removes legacy algorithms. The handshake typically completes in one round trip (1‑RTT), and it supports 0‑RTT for resumed sessions. Key exchange is always ephemeral (forward secrecy), and the certificate is encrypted in the handshake.
Certificates: The Trust Anchors
A certificate binds a public key to an identity (like a domain name). It's issued by a Certificate Authority (CA) after verifying control of the domain. Browsers trust a set of root CAs, and certificates chain up to those roots.
When a client connects, it verifies:
- The certificate is signed by a trusted CA (directly or via intermediates).
- The certificate is within its validity period.
- The hostname matches the certificate's Subject Alternative Name (SAN).
- The certificate hasn't been revoked (via CRL or OCSP).
If any check fails, the browser shows a warning. Modern tools like Let's Encrypt make it easy to obtain free, automated certificates.
Encryption and Integrity
TLS uses symmetric encryption (like AES‑GCM or ChaCha20‑Poly1305) for bulk data because it's fast. The handshake establishes shared keys for each direction. Integrity is ensured by AEAD (Authenticated Encryption with Associated Data), which combines encryption and authentication.
Forward secrecy is achieved through ephemeral key exchanges (ECDHE), meaning that even if the server's private key is compromised later, past sessions remain secure.
Common Pitfalls and How to Avoid Them
- Expired certificates: Automate renewal (e.g., with Certbot) and monitor expiry.
- Mixed content: Ensure all resources (images, scripts) are loaded over HTTPS to avoid warnings and security gaps.
- Weak cipher suites: Disable outdated protocols (SSLv3, TLS 1.0/1.1) and weak ciphers; prefer TLS 1.3.
- Incomplete certificate chains: Include intermediate certificates; test with SSL Labs.
- HSTS not enabled: Use HTTP Strict Transport Security to force HTTPS.
Debugging TLS Issues with OpenSSL
When something goes wrong, OpenSSL is your friend. Here's how to inspect a certificate chain:
openssl s_client -connect example.com:443 -servername example.com -showcerts
This prints the certificate chain and handshake details. Look for verification errors and expiry dates.
FAQ
What is the difference between HTTPS and TLS?
HTTPS is HTTP over TLS. TLS is the cryptographic protocol that provides encryption, integrity, and authentication; HTTPS is the application of TLS to HTTP traffic.
Do I need a certificate for internal services?
Yes, even for internal services, using TLS with certificates prevents eavesdropping and man‑in‑the‑middle attacks. You can use an internal CA or self‑signed certificates (with proper distribution).
How does TLS 1.3 improve security?
TLS 1.3 removes obsolete features (like RSA key exchange and CBC ciphers), enforces forward secrecy, and encrypts more of the handshake, reducing attack surface and improving privacy.
Ready to inspect your own TLS setup? Use our Nginx log analyzer to spot TLS errors and performance issues in your server logs.