Part AWhere TLS fits
HTTPS = HTTP inside TLS
TLS (Transport Layer Security) wraps a connection so that traffic is private and tamper-evident. HTTPS is not a different protocol: it is plain HTTP (chapter 24) running inside a TLS-encrypted connection. Everything about HTTP still applies; TLS just protects it in transit. (SSL is TLS's old name; all SSL versions are broken and switched off.)
How HTTP, TLS and TCP fit together
TLS sits between HTTP and TCP. HTTP hands its bytes to TLS; TLS encrypts them into records and hands those to TCP; TCP delivers them reliably (chapters 16-20):
one HTTPS packet on the wire
| Layer | Job here | Visible to someone on the path? |
|---|---|---|
| HTTP | GET /orders/1042, the token, the JSON | No: encrypted |
| TLS | Encrypts, checks integrity, proves identity | Only the record headers, plus the handshake's first messages |
| TCP | Ports, sequence numbers, retransmission | Yes: port 443, sizes, timing |
| IP | Addresses, routing | Yes: 34.87.120.15 |
Three guarantees
| # | Guarantee | Means | Provided by |
|---|---|---|---|
| 1 | Confidentiality | Eavesdroppers see only scrambled data | Symmetric encryption (AES or ChaCha20) |
| 2 | Integrity | Any change on the way is detected | An authentication tag on every record (AEAD) |
| 3 | Authentication | You are really talking to api.kade.lk | The server's certificate and a signature made with its private key |
Part BThe cryptography
Symmetric encryption: fast, one shared key
In symmetric encryption, the same key locks and unlocks. Modern ciphers like AES (sped up by special CPU instructions) and ChaCha20 (fast on phones without them) encrypt gigabytes per second per CPU core. TLS 1.3 uses them in AEAD mode ("authenticated encryption"): each record is encrypted and gets a tag that proves it was not changed, in one step.
The catch: both sides need the same key, and they have never met. Sending the key over the network would let anyone listening read everything after it.
Asymmetric cryptography: key pairs
In asymmetric (public-key) cryptography, each party has a key pair: a private key kept secret and a public key shared with everyone. What one key does, only the other can undo. It is used in two ways:
Signatures
Sign with the private key; anyone checks with the public key. Proves "the owner of this key made this, and it was not changed".
TLS use: kade-api proves it owns its certificate.
Key exchange
Two sides each combine their own private value with the other's public value and arrive at the same secret, without ever sending it.
TLS use: laptop and kade-api agree on the symmetric key.
Asymmetric operations rely on hard maths (large-number and elliptic-curve problems), which makes them thousands of times slower than symmetric encryption. Fine for a few operations per connection; far too slow for every byte of a video.
Key exchange: agreeing on a secret in public
The idea behind TLS's key exchange is Diffie-Hellman. Both sides agree on public numbers, each picks a private number, and they swap only the results. Anyone listening sees the swapped values but cannot work out the shared secret, because undoing the maths is extremely hard at real sizes. Try it with tiny numbers:
| Laptop | On the network (anyone can see) | kade-api |
|---|---|---|
| secret a = 6 | p = 23, g = 5 | secret b = 15 |
| A = ga mod p = 8 | A = 8 → ← B = 19 | B = gb mod p = 19 |
| Ba mod p = 2 | To get the secret, a listener would have to find a from A (or b from B). Easy for p = 23; practically impossible for real key sizes. | Ab mod p = 2 |
Both sides computed 2 without ever sending it. This shared secret becomes the seed for the symmetric keys.
Real TLS uses the elliptic-curve version (ECDHE, usually the curve X25519) with 256-bit numbers. The "E" means ephemeral: fresh private numbers for every connection, thrown away afterwards (section 9).
Why TLS uses both
| # | Phase | Crypto | Used for | Why this kind |
|---|---|---|---|---|
| 1 | Handshake | Asymmetric: ECDHE key exchange | Agree on a shared secret over an open network | Solves "how do we share a key we never met to swap" |
| 2 | Handshake | Asymmetric: signature with the certificate's key | Prove the server is api.kade.lk | Only the real private key can make it |
| 3 | Handshake | Hash-based key derivation (HKDF) | Turn the shared secret into separate keys for each direction | One secret, many keys, all unguessable |
| 4 | Every record after | Symmetric AEAD (AES-GCM or ChaCha20-Poly1305) | Encrypt and protect all HTTP data | Fast enough for every byte |
The TLS 1.3 handshake
After the TCP handshake, the laptop and kade-api run the TLS handshake:
| ClientHello | Supported TLS versions and cipher suites; a key share (the laptop's public ECDHE value); the server name SNI = api.kade.lk (section 12); ALPN = "h2, http/1.1" (chapter 24); a random number. |
| ServerHello | The chosen suite (e.g. TLS_AES_128_GCM_SHA256) and kade-api's key share. Both sides can now compute the shared secret: everything after this is encrypted. |
| Certificate | kade-api's certificate chain (Part C). |
| CertificateVerify | A signature over the whole handshake so far, made with the certificate's private key. This is the proof: a stolen copy of the certificate is useless without the private key. |
| Finished | Each side sends a check value over the whole handshake, so any tampering with the hellos (like forcing a weaker cipher) is detected. |
Forward secrecy
Because the key-exchange numbers are ephemeral, each connection's keys exist only in memory for that connection. If an attacker records today's traffic and steals kade-api's private key next year, they still cannot decrypt the recording: the private key was only used to sign, never to protect the session keys. This is forward secrecy. TLS 1.3 always has it. Old TLS 1.2 setups with "RSA key exchange" did not: there, one stolen key opened every recorded session.
The full timeline: TCP + TLS + HTTP
A brand-new HTTPS request from home to kade-api (41 ms round trip), counted in round trips:
| # | Setup | Round trips before the response arrives | From home |
|---|---|---|---|
| 1 | TCP + TLS 1.2 + HTTP | 1 + 2 + 1 = 4 | ~164 ms |
| 2 | TCP + TLS 1.3 + HTTP | 1 + 1 + 1 = 3 | ~123 ms |
| 3 | QUIC (HTTP/3), new | 1 + 1 = 2 | ~82 ms |
| 4 | QUIC or TLS 1.3 resumed with 0-RTT | 1 | ~41 ms |
| 5 | Reused keep-alive connection | 1 | ~41 ms |
Part CCertificates
What a certificate is
A certificate is a signed statement: "this public key belongs to api.kade.lk", signed by someone the browser already trusts. The format is called X.509. kade-api's certificate (as issued for the load balancer later):
| Subject | CN = api.kade.lk |
| Subject Alternative Names (SAN) | api.kade.lk, kade.lk, www.kade.lk the names browsers actually check |
| Issuer | an intermediate CA of Google Trust Services allowed by the CAA record (chapter 23) |
| Validity | not before 2026-09-01 · not after 2026-11-30 90 days |
| Public key | ECDSA P-256 (the matching private key never leaves the server) |
| Key usage | digital signature · TLS server authentication |
| Serial number | unique per issuer; used for revocation |
| Signature | the issuer's signature over all of the above |
The chain of trust, and SNI
Browsers and operating systems ship with a trust store of about 150 root CA certificates. Roots are kept offline and rarely used; they sign intermediate CAs, which sign server certificates. The server sends its own certificate plus the intermediates; the client builds the chain up to a root it already trusts:
Wide by design - scroll it sideways.
One IP often serves many sites (a load balancer or Cloudflare). Before any certificate is sent, the server needs to know which one to pick, so the ClientHello includes the name: SNI (Server Name Indication). SNI is sent in clear text, which is how the café can see the site name even over HTTPS. A newer extension, Encrypted ClientHello (ECH), hides it where both sides support it.
Try it: will the browser trust this?
Pick a situation. The checks run in the order a browser does them:
- Build a chain from the certificate up to a root in the trust store
- Each certificate's signature verifies with its issuer's public key
- Today's date (on the client) is inside every certificate's validity dates
- The name typed (api.kade.lk) is in the leaf's SAN list
- Not revoked; allowed for TLS server use
- CertificateVerify: the server's signature over this handshake verifies with the leaf's public key
Trusted. The connection continues.
Every check passes. The padlock appears and the request goes ahead.
Getting a certificate
A CA only signs for a name after you prove you control it. With ACME, the automated protocol used by Let's Encrypt, Google Trust Services and others, the whole process takes seconds:
| 1 | Create a key pair on the server. Build a CSR (certificate signing request): the names plus the public key, signed with the private key. |
| 2 | The CA gives you a challenge to prove control of each name. |
| 3 | You answer it (next table). The CA checks the CAA record too (chapter 23). |
| 4 | The CA signs and returns the certificate and its chain, and logs it publicly (section 16). |
| # | Challenge | How you prove control | Good for |
|---|---|---|---|
| 1 | HTTP-01 | Serve a given token at http://api.kade.lk/.well-known/acme-challenge/… | Simple setups with port 80 open |
| 2 | DNS-01 | Create a TXT record at _acme-challenge.api.kade.lk (chapter 23) | Wildcards (*.kade.lk) and servers not reachable from the internet |
| 3 | TLS-ALPN-01 | Answer a special TLS handshake on port 443 | When only 443 is open |
| DV (domain validated) | Proves control of the name only. What ACME issues, and what almost everyone uses. |
| OV / EV | The CA also checks the organisation. Browsers no longer show it differently, so it adds little for websites. |
| Wildcard | *.kade.lk covers api, www, admin … but one level only, and not kade.lk itself. |
Lifetimes, renewal and revocation
- Short lifetimes. ACME certificates last 90 days, and the industry has agreed to cut the maximum for all public certificates in steps, down to 47 days by 2029. Short lives limit the damage from a leaked key. Renewal must be automated: an expired certificate is one of the most common causes of outages.
- Revocation. If a private key leaks, the certificate should be revoked before it expires. Clients learn about revocations through lists (CRLs) or an online check (OCSP), which the server can attach to its handshake ("OCSP stapling"). In practice browsers check unevenly, which is another reason for short lifetimes.
Certificate Transparency
Every publicly trusted certificate must be written to public, append-only Certificate Transparency (CT) logs, and browsers refuse certificates that are not logged. So Kadé can search the logs (for example on crt.sh) for every certificate ever issued for kade.lk, and spot one it did not request. Attackers use the same logs to discover new subdomains, so do not rely on a secret-sounding name like admin-7f3.kade.lk to hide anything.
Common certificate errors
| # | Browser says | Cause | Fix |
|---|---|---|---|
| 1 | NET::ERR_CERT_DATE_INVALID | Expired, or the client's clock is wrong | Automate renewal; check the device clock |
| 2 | NET::ERR_CERT_COMMON_NAME_INVALID | The name is not in the SAN list | Add the name (e.g. www) to the certificate |
| 3 | NET::ERR_CERT_AUTHORITY_INVALID | Self-signed, or a CA the client does not trust | Use a public CA; for internal services, install your own root on your own machines |
| 4 | Works in Chrome, fails in curl or an Android app | The server does not send the intermediate (missing chain) | Serve the full chain file, not just the leaf |
| 5 | Every site shows the company's name as issuer | A corporate proxy decrypts traffic with its own root installed on work laptops (TLS inspection) | Expected on managed devices; apps with pinned certificates break there |
TLS in the Kadé setup
| # | Where TLS ends | Certificate | Notes |
|---|---|---|---|
| 1 | Today: on kade-api itself | From Let's Encrypt, renewed by a cron job on the VM | The private key lives on the VM; Node does the TLS work |
| 2 | Later: at the Google load balancer | Google-managed, auto-renewed | "TLS termination" at the edge. The load balancer → kade-api hop runs inside Google's network, as HTTP or re-encrypted HTTPS. |
| 3 | If Cloudflare sits in front | Cloudflare's edge certificate + an origin certificate | Use "Full (strict)": Cloudflare also checks the origin's certificate. "Flexible" sends the second hop unencrypted. |
| 4 | kade-api → PayGate | PayGate's certificate, plus optionally Kadé's own client certificate | Mutual TLS (mTLS): both sides show certificates. Common for server-to-server APIs. |
Strict-Transport-Security: max-age=31536000 (HSTS) tells browsers to use HTTPS for kade.lk for a year, even if someone types http://. It closes the gap where the very first plain-HTTP request could be intercepted before the redirect.Summary
| # | Idea | In one line | New terms |
|---|---|---|---|
| 1-2 | HTTPS | HTTP inside TLS, on top of TCP; IPs, ports and SNI stay visible | TLS, HTTPS, TLS record |
| 3 | Guarantees | Confidentiality, integrity, authentication | AEAD |
| 4-5 | Crypto kinds | Symmetric: fast, one shared key. Asymmetric: key pairs, slow; signatures and key exchange. | public/private key, signature |
| 6-7 | Hybrid | ECDHE agrees on a secret; the certificate key signs; AES/ChaCha20 protects the data | Diffie-Hellman, ECDHE, HKDF |
| 8 | Handshake | ClientHello, ServerHello, Certificate, CertificateVerify, Finished: 1 RTT | cipher suite, key share, ALPN |
| 9 | Forward secrecy | Ephemeral keys: a stolen private key cannot open old recordings | forward secrecy |
| 10 | Timeline | TCP + TLS 1.3 + HTTP = 3 RTT; QUIC 2; 0-RTT or reuse 1 | 0-RTT |
| 11-12 | Certificates | X.509: SAN names, issuer, dates, key; chain to a trusted root; SNI picks the certificate | X.509, SAN, trust store, intermediate, SNI |
| 14-16 | Lifecycle | ACME challenges, DV/wildcard, short lifetimes, revocation, CT logs | ACME, CSR, OCSP, CT |
| 17-18 | Practice | Common errors; where TLS terminates; mTLS; HSTS | TLS termination, mTLS, HSTS |
Try it yourself
A full TLS handshake
- What to look for: The certificate chain (depth 0, 1, 2),
Protocol: TLSv1.3, the cipher. - From section: 8. The TLS 1.3 handshake
openssl s_client -connect example.com:443 -servername example.comThe certificate's main fields
- What to look for: Subject, issuer, notBefore/notAfter, the SAN list.
- From section: 11. What a certificate is
openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | openssl x509 -noout -subject -issuer -dates -ext subjectAltNameWhere the time goes
- What to look for: Each step adds roughly one round trip.
- From section: 10. The full timeline: TCP + TLS + HTTP
curl -o /dev/null -s -w 'tcp %{time_connect} tls %{time_appconnect} first byte %{time_starttransfer}\n' https://example.comTLS version, ALPN result, certificate
- What to look for:
ALPN: server accepted h2. - From section: 8. The TLS 1.3 handshake
curl -v --tlsv1.3 https://example.com 2>&1 | grep -i -E 'SSL connection|ALPN|subject|issuer'Every certificate issued for it
- Steps: Search your domain at
crt.sh - What to look for: Issuers and dates you recognise, and nothing you do not.
- From section: 16. Certificate Transparency
crt.shCertificates on GCP load balancers
- What to look for: Type MANAGED, status ACTIVE, expiry date.
- From section: 18. TLS in the Kadé setup
gcloud compute ssl-certificates list