Skip to content
Tutorials Center
My learning

Topics

Lesson 64 of 79 ยท Network Security

TLS and HTTPS

How TLS and HTTPS work: the TLS 1.3 handshake, certificates and chains, certificate authorities, SNI, ALPN, HSTS, forward secrecy and common misconfigurations.

When the address bar shows a padlock, a protocol called TLS (Transport Layer Security) is protecting the connection. HTTPS is simply HTTP carried inside a TLS session, normally on TCP port 443. TLS gives you three guarantees: confidentiality (eavesdroppers cannot read the traffic), integrity (nobody can change it unnoticed) and authentication (you are talking to the real server, not an impostor). It is the practical answer to the sniffing, tampering and man-in-the-middle threats from network security fundamentals.

TLS is built entirely from the tools in cryptography for networking: a key exchange, signatures and certificates, key derivation and authenticated encryption. This lesson walks through the TLS 1.3 handshake step by step, explains how certificates and certificate authorities create trust, and covers the extensions you will meet in practice (SNI, ALPN, HSTS) and the mistakes that most often break real deployments. The runnable examples rebuild the key pieces with Node's crypto module. They are teaching models, not a TLS implementation, and real captures at the end are read-only.

Where TLS sits

TLS runs between the application and TCP. The application writes bytes as usual; TLS cuts them into records, encrypts and authenticates each record, and hands them to TCP. The same protocol protects many applications: HTTPS, secure email (SMTP/IMAP over TLS), MQTT over TLS, and database connections. (HTTP/3 uses the TLS 1.3 handshake inside QUIC instead of on top of TCP.)

Versions you should know: SSL 2.0 and 3.0 and TLS 1.0 and 1.1 are obsolete and should be disabled. TLS 1.2 (2008) is still widely used. TLS 1.3 (2018) is the current version: a faster handshake and a much smaller set of safe options.

The TLS 1.3 handshake

After the TCP handshake, the client and server exchange messages that agree on parameters, perform a key exchange, authenticate the server and derive keys. In TLS 1.3 this takes one round trip:

sequenceDiagram
    participant C as Client
    participant S as Server
    C->>S: ClientHello (versions, cipher suites, key_share, SNI, ALPN)
    S->>C: ServerHello (chosen suite, key_share)
    Note over C,S: both derive handshake keys. Everything below is encrypted
    S->>C: EncryptedExtensions (ALPN result)
    S->>C: Certificate (chain)
    S->>C: CertificateVerify (signature over transcript)
    S->>C: Finished (MAC over transcript)
    C->>S: Finished
    Note over C,S: application keys derived. Encrypted HTTP starts
    C->>S: Application data (GET / ...)

The client sends its key share in the very first message, so the keys exist after one round trip.

  1. ClientHello. The client lists the TLS versions and cipher suites it supports, a random value, and, crucially, one or more key shares: its public value for an ephemeral key exchange (usually X25519). It also includes the server name (SNI) and the application protocols it would like (ALPN).
  2. ServerHello. The server picks a cipher suite and replies with its own key share. Now both sides compute the same shared secret with ECDH, and from it derive the handshake keys with a key derivation function (HKDF). Everything after this point is encrypted.
  3. Certificate, CertificateVerify, Finished. The server proves its identity. It sends its certificate chain, then a CertificateVerify message, which is a signature over the whole handshake transcript made with the private key that matches the certificate's public key. This ties the key exchange to the identity: a man in the middle cannot produce that signature. The server then sends Finished, a MAC over the transcript.
  4. Client Finished. The client verifies the certificate chain and the signature, sends its own Finished, and both sides derive the application traffic keys. From now on each record is protected with an AEAD cipher such as AES-GCM or ChaCha20-Poly1305.

Cipher suites in TLS 1.3 are short and all strong: TLS_AES_128_GCM_SHA256, TLS_AES_256_GCM_SHA384 and TLS_CHACHA20_POLY1305_SHA256. The key exchange and signature algorithms are negotiated separately. Gone are the weak options that TLS 1.2 still allowed (RSA key transport, CBC modes, RC4, compression), and so are renegotiation and static Diffie-Hellman.

Forward secrecy is built in. Because each connection uses fresh ephemeral key shares that are deleted after the handshake, stealing the server's long-term private key later does not let an attacker decrypt traffic they recorded earlier. The long-term key only signs, never encrypts session keys. (In TLS 1.2 with RSA key exchange the opposite was true, which is one reason it was removed.)

Resumption and 0-RTT. A client that has connected before can receive a session ticket and resume with a cheaper handshake. TLS 1.3 also allows 0-RTT data: the client sends application data in its very first flight using a key from the earlier session. This is fastest, but 0-RTT data can be replayed by an attacker, so servers should accept it only for idempotent requests such as GET, never for something like a payment.

Key exchange, key derivation and encrypted records

The first model below follows steps 2 and 4 in code: an X25519 exchange, HKDF to derive a key per direction, and AES-256-GCM to protect a record, including what happens when someone flips a bit.

Node.js Key exchange, key derivation and an encrypted record

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Certificates and trust

The key exchange gives you an encrypted channel, but with whom? The answer is the server's certificate. A certificate is a signed statement: "this public key belongs to the name www.example.com." It follows the X.509 format and contains:

  • the subject and its Subject Alternative Names (SAN), the list of host names the certificate is valid for (the old Common Name field is ignored by browsers);
  • the subject's public key;
  • the validity period (not before / not after);
  • the issuer (the certificate authority) and its signature over all of the above;
  • extensions such as key usage and whether the certificate is itself allowed to act as a CA.

Certificate authorities and chains

A certificate authority (CA) is an organization everybody agrees to trust to check identities before signing certificates. Your operating system and browser ship with a trust store of a hundred or so root CA certificates. Roots are kept offline and rarely sign server certificates directly. Instead they sign intermediate CAs, which issue the leaf certificates that servers present. The server sends the leaf and intermediates; the client builds a path to a root it already trusts:

flowchart TB
    R["Root CA<br/>(in your trust store, self-signed)"] -->|signs| I["Intermediate CA"]
    I -->|signs| L["Leaf certificate<br/>www.example.com"]

The client trusts the leaf because each signature checks out up to a root it already holds.

To validate, the client checks every link in the chain:

  1. Each certificate's signature verifies with its issuer's public key.
  2. The chain ends at a trusted root.
  3. All certificates are within their validity dates.
  4. Intermediate certificates are allowed to be CAs.
  5. The host name the user asked for appears in the leaf's SAN.
  6. The certificate has not been revoked (checked through CRLs, OCSP or short lifetimes).

Public CAs also publish every certificate they issue in append-only Certificate Transparency logs, so a domain owner can notice a certificate that was issued for their name by mistake or by an attacker. Free automated CAs using the ACME protocol (such as Let's Encrypt) made HTTPS universal, and certificate lifetimes keep getting shorter (90 days or less) to limit the damage of a stolen key.

Node.js Validating a certificate chain (a toy model)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Each failure maps to a real browser error: a name mismatch, an expired certificate, an untrusted issuer and a tampered certificate are the four classic reasons for a certificate warning.

Putting it together: signing the handshake

Certificates alone only say "this key belongs to that name". The server must also prove it holds the matching private key right now, for this handshake. That is the job of CertificateVerify, which signs the transcript including both key shares. The model below shows a man in the middle failing: the attacker can swap the key share, but cannot produce a signature that the real certificate key verifies.

Node.js CertificateVerify and Finished: why a man in the middle fails

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

SNI, ALPN and the other extensions

TLS extensions in the ClientHello carry useful information:

  • SNI (Server Name Indication) tells the server which host name the client wants before a certificate is chosen. One IP address often hosts hundreds of sites, and the server needs the name to pick the right certificate. The catch: SNI is sent in plaintext in the ClientHello, so an on-path observer can see which site you visit even though the content is encrypted. Encrypted Client Hello (ECH) closes this gap, where supported.
  • ALPN (Application-Layer Protocol Negotiation) lets the client list protocols such as h2 and http/1.1, and the server picks one inside the handshake. That is how a browser learns that HTTP/2 can be used, without an extra round trip.
  • supported_versions, signature_algorithms and supported_groups negotiate the version, signature types and key-exchange groups.

HTTPS in practice: HSTS and redirects

A user who types example.com normally starts with plain http://. An attacker on the path can intercept that first request and keep the victim on HTTP (a "downgrade" or SSL-stripping attack). HSTS (HTTP Strict Transport Security) fixes this. A site sends a header on its HTTPS responses:

HTTP
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload

For the next max-age seconds (here one year) the browser will rewrite any http:// request for that domain to https:// itself and will refuse to continue past certificate errors. includeSubDomains extends this to subdomains. preload asks to be included in a list built into browsers, which protects even the very first visit. Roll HSTS out carefully: once it is set for a long period, a broken certificate cannot be clicked through.

Also remember mixed content: an HTTPS page that loads a script or image over plain HTTP undermines the protection, and browsers block or warn about it.

What TLS does not do

  • It protects data in transit between two endpoints, not on the server's disk or inside a compromised application.
  • It does not hide who you talk to: IP addresses, the SNI (without ECH), the amount and timing of traffic remain visible.
  • It does not make the server trustworthy. A phishing site can have a perfectly valid certificate for its own name. The padlock means "you are connected to the name in the address bar, privately", not "this site is honest".
  • A TLS-inspecting proxy (common in companies) works by installing its own root certificate on managed devices, so it can legitimately decrypt traffic. That is why you should never install a root certificate you do not understand.

Common misconfigurations

Problem Why it matters Fix
Expired certificate Users see a full-page warning, automated clients fail Automate renewal (ACME), monitor expiry dates
Missing intermediate certificate Works in some browsers, fails in others and in API clients Serve the full chain (leaf plus intermediates)
Name mismatch Certificate does not list the host name (or www variant) Include all names in the SAN list
TLS 1.0/1.1 or weak ciphers enabled Known attacks, compliance failures Allow TLS 1.2+ and modern AEAD suites only
No HSTS, HTTP not redirected Downgrade and SSL-stripping attacks Redirect to HTTPS, send HSTS
Disabling certificate checks in code (verify=False) Removes authentication; any attacker passes Fix the trust problem, never turn off validation
Private key stored insecurely or shared widely Anyone with the key can impersonate the server Restrict access, rotate keys, use a hardware module for high value keys
Mixed content Insecure sub-resources can be tampered with Load everything over HTTPS

Setting verify=False, rejectUnauthorized: false or curl -k "just to make it work" in code is one of the most common real-world security bugs. It keeps the encryption but throws away the authentication, so a man in the middle is back in business.

Inspecting a real connection (read-only)

Command-line tools show you the negotiated parameters. The output below is an abridged, illustrative example of what openssl s_client -connect example.com:443 -servername example.com prints; the exact text varies by tool version and server.

TEXT
CONNECTED(00000003)
depth=2 C = US, O = Example Trust, CN = Example Trust Root
depth=1 C = US, O = Example Trust, CN = Example Trust Issuing CA 1
depth=0 CN = example.com
verify return:1
---
Certificate chain
 0 s:CN = example.com
   i:C = US, O = Example Trust, CN = Example Trust Issuing CA 1
 1 s:C = US, O = Example Trust, CN = Example Trust Issuing CA 1
   i:C = US, O = Example Trust, CN = Example Trust Root
---
SSL-Session:
    Protocol  : TLSv1.3
    Cipher    : TLS_AES_256_GCM_SHA384
    ALPN protocol: h2
Verification: OK

Read it from the top: the chain depth (0 is the leaf), the name the leaf is for, the negotiated protocol and cipher suite, the ALPN result and the final verification verdict. In a packet capture the ClientHello and ServerHello are visible but everything after ServerHello is encrypted, which is why you cannot read HTTPS in Wireshark without keys. See packet capture with tcpdump and Wireshark for how to examine the handshake.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does the CertificateVerify message in TLS 1.3 prove?

    CertificateVerify is a signature over the handshake transcript made with the certificate's private key. It binds the key exchange to the server's identity, so a man in the middle cannot reuse it.
  2. 2. What is forward secrecy?

    Ephemeral key exchange makes each session key independent of the long-term key, which is only used to sign.
  3. 3. Why does the ClientHello include SNI?

    One IP address can host many sites. SNI names the site before the certificate is chosen. It travels in plaintext unless Encrypted Client Hello is used. ALPN is what negotiates HTTP/2.
  4. 4. A browser shows a certificate error saying the certificate is for www.example.com, but you visited example.org. Which check failed?

    The chain may be fully valid, but the certificate lists other names. The client must confirm that the requested host name appears in the SAN list.
  5. 5. What does the HSTS header tell the browser?

    HSTS makes the browser upgrade http:// requests to https:// itself for max-age seconds, which defeats SSL-stripping downgrade attacks.

Summary

  • HTTPS is HTTP inside TLS (port 443). TLS provides confidentiality, integrity and server authentication; TLS 1.3 is the current version, with a one-round-trip handshake and only strong options.
  • The handshake: ClientHello with key share, ServerHello with key share, derived keys, then Certificate, CertificateVerify and Finished. Ephemeral ECDH gives forward secrecy; 0-RTT data can be replayed.
  • Certificates bind names to public keys. Clients validate the whole chain up to a trusted root, check the dates, the host name (SAN) and revocation status. Certificate Transparency and ACME make issuance auditable and automatic.
  • SNI selects the certificate (visible in plaintext unless ECH is used), ALPN negotiates the application protocol, and HSTS forces HTTPS in the browser.
  • Most real problems are misconfigurations: expired or incomplete chains, name mismatches, old versions, and code that disables verification.

Next, learn how to control which packets may cross a network boundary at all: firewalls, VPNs and intrusion detection.