Skip to lesson

HTTPS and TLS in plain words

A customer pays for an order over public Wi-Fi. On plain HTTP, everyone on that network could read the card details or change the amount. What stops them?

What TLS protects

TLS (Transport Layer Security) is the encryption layer between TCP and HTTP. HTTPS is simply HTTP sent inside a TLS connection: the text is the same, but protected on the way.

The first example sends a normal HTTP request by hand, but through an encrypted TLS connection.

Example 1 · The same HTTP text, inside TLS

Terminal
printf 'GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n' | openssl s_client -quiet -connect example.com:443 -servername example.com
Sample output (Git Bash or macOS Terminal; trimmed)
Connecting to 2606:4700:10::ac42:93f3
depth=3 C=US, O=SSL Corporation, CN=SSL.com TLS ECC Root CA 2022
verify return:1
depth=2 C=US, O=SSL Corporation, CN=SSL.com TLS Transit ECC CA R2
verify return:1
depth=1 C=US, O=SSL Corporation, CN=Cloudflare TLS Issuing ECC CA 3
verify return:1
depth=0 CN=example.com
verify return:1
HTTP/1.1 200 OK
Date: Fri, 16 Oct 2026 03:00:19 GMT
Content-Type: text/html; charset=utf-8
...

What this code does

  • This is the hand-typed request from the TCP lesson, word for word. The only change: openssl s_client opens a TLS connection on port 443 instead of a plain one on port 80. HTTP inside TLS is HTTPS.
  • Before the response, openssl printed the certificate checks (verify return:1 means passed). On the Wi-Fi, only encrypted bytes travelled; the readable request and response exist only at the two ends.
  • -servername example.com sends the host name in the client hello, so the server knows which site's certificate to show.

Plain HTTP crosses public Wi-Fi, the router and your internet provider as readable text, passwords and card details included. TLS (its old name was SSL) gives three protections:

  • Privacy: the bytes are encrypted, so people in between see only noise.
  • Integrity: any change made on the way is detected, so nobody can quietly add ads or alter an amount.
  • Authentication: the server proves it really is example.com, so a fake Wi-Fi hotspot cannot pretend to be your bank.
TLS does not hide everything: the server's name and address are still visible to the network, and a site that proves its name can still be a scam site. HTTPS means "private and really this site", not "safe site".

The TLS handshake in plain words

A shared secret is a key that client and server both compute from the key shares they exchange, without ever sending the secret itself. Later bytes are encrypted with keys made from it.

The example shows the TLS handshake in curl's verbose output, between the connection and the first HTTP line.

Example 2 · Watch the TLS handshake in curl

Terminal
curl -sv https://example.com -o page.html
Sample output (curl on macOS or Linux, trimmed; Windows curl shows only a few schannel lines)
*   Trying 172.66.147.243:443...
...
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
* TLSv1.3 (IN), TLS handshake, Certificate (11):
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
* TLSv1.3 (IN), TLS handshake, Finished (20):
* TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.3 (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384 / x25519 / id-ecPublicKey
...
* Server certificate:
*  subject: CN=example.com
*  start date: Sep 26 22:49:11 2026 GMT
*  expire date: Dec 25 22:56:35 2026 GMT
*  subjectAltName: host "example.com" matched cert's "example.com"
*  issuer: C=US; O=SSL Corporation; CN=Cloudflare TLS Issuing ECC CA 3
*  SSL certificate verify ok.
...
> GET / HTTP/2

What this code does

  • Client hello and Server hello open the handshake, Certificate is the server sending its certificate, and CERT verify is the server proving it holds the private key. The two Finished lines mean both sides completed the handshake with the same keys.
  • The Server certificate block is the client's check: the name matched (subjectAltName ... matched), the dates are valid and the issuer is trusted, so SSL certificate verify ok. Only after that does the > GET line appear.
  • x25519 is the key-exchange method that produced the shared secret, and TLS_AES_256_GCM_SHA384 is the encryption used for the rest of the connection.

After the TCP handshake, client and server run a TLS handshake before any HTTP is sent. In plain words, a simplified picture of TLS 1.3, the current version:

  • Client hello: the client says which TLS versions and encryption methods it supports, the host name it wants (so one server address can host many sites) and its share of a key exchange.
  • Server hello: the server picks a version and method and sends its share of the key exchange.
  • Both sides combine the two key shares into the same shared secret.
  • The server sends its certificate (already encrypted), and the client checks it: right host name, not expired, issued by someone it trusts. If any check fails, it stops here with a certificate error; otherwise the encrypted HTTP request follows.
TLS 1.3 needs one extra round trip on top of the TCP handshake. That is the "tls done" time you measured with curl -w in the TCP lesson.

Certificates, certificate authorities and the padlock

A certificate is a small signed file that says this public key belongs to example.com, valid from one date to another. A certificate authority (CA) signs it after checking the domain.

This example reads a real site's certificate: who it was issued to, who issued it and its dates.

Example 3 · Read a site's certificate with openssl

Terminal
openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | openssl x509 -noout -subject -issuer -dates -ext subjectAltName
Sample output (dates change when the site renews its certificate)
Warning: Reading certificate from stdin since no -in or -new option is given
subject=CN=example.com
issuer=C=US, O=SSL Corporation, CN=Cloudflare TLS Issuing ECC CA 3
notBefore=Sep 26 22:49:11 2026 GMT
notAfter=Dec 25 22:56:35 2026 GMT
X509v3 Subject Alternative Name:
    DNS:example.com, DNS:*.example.com

What this code does

  • The first openssl fetches the certificate; the second prints its key fields. The warning line is harmless: newer openssl versions say where they read the certificate from.
  • subject and the Subject Alternative Name list are the names the certificate is valid for; *.example.com covers any single name in front, such as www.example.com. notBefore and notAfter are the validity dates, about 90 days apart here, which is common today.
  • issuer is the intermediate CA that signed it. Example 1 showed the full chain: depth=0 is the site, depth=1 and depth=2 are intermediates and depth=3 is the trusted root.

A public key is the half of a key pair that may be shared; only the server holds the matching private key, and the handshake proves it has it. Anyone can make a certificate, so who signed it matters. Your operating system and browser ship with a list of trusted root CAs. A site's certificate is usually signed by an intermediate CA, which is signed by a root, forming a chain; the client checks every link up to a root it trusts. Let's Encrypt gives such certificates for free, so there is no cost reason to skip HTTPS.

In the browser, click the icon left of the address (a padlock in most browsers; recent Chrome shows a small "tune" icon) and choose Connection is secure → Certificate is valid to see who the certificate was issued to, who issued it and its dates.

Certificate errors: expired, wrong host name, self-signed

This example opens three sites that each have a broken certificate and shows the curl error for each.

Example 4 · Three certificate errors

Terminal
curl https://expired.badssl.com/curl https://wrong.host.badssl.com/curl https://self-signed.badssl.com/
Sample output (Windows curl, trimmed)
curl: (35) schannel: next InitializeSecurityContext failed: SEC_E_CERT_EXPIRED (0x80090328) - The received certificate has expired.
curl: (60) schannel: SNI or certificate check failed: SEC_E_WRONG_PRINCIPAL (0x80090322) - The target principal name is incorrect.
More details here: https://curl.se/docs/sslcerts.html
...
curl: (60) schannel: SEC_E_UNTRUSTED_ROOT (0x80090325) - The certificate chain was issued by an authority that is not trusted.
More details here: https://curl.se/docs/sslcerts.html
...

What this code does

  • badssl.com is a public test site with one broken certificate per name. Each error names the failed check: the certificate expired, the name does not match ("principal" means the name), or the chain leads to no trusted root.
  • curl on macOS and Linux words them as certificate has expired, no alternative certificate subject name matches target hostname and self-signed certificate. Same checks, different text.
  • No HTTP request was sent in any of the three: the client stopped during the TLS handshake, before the > lines.

When the check fails, curl stops with an error and browsers show a full-page warning such as "Your connection is not private". The error tells you which check failed:

  • Expired (NET::ERR_CERT_DATE_INVALID): the "valid until" date has passed; the site forgot to renew. If every site shows this, your computer's clock is wrong.
  • Wrong host name (NET::ERR_CERT_COMMON_NAME_INVALID): the certificate is for another name, for example example.com when you opened shop.example.com, or you used an IP address instead of the name.
  • Self-signed or unknown issuer (NET::ERR_CERT_AUTHORITY_INVALID): the server signed its own certificate, or the chain does not lead to a trusted root. Common on test servers and on networks that intercept traffic.

Predict the output

Which check failed?

Read the curl error and name the failed certificate check.

Round 1 of 2

Which check failed?

$ curl https://expired.badssl.com/
curl: (60) SSL certificate problem: certificate has expired

Always HTTPS, and never turn the checks off

HSTS (HTTP Strict Transport Security) is a response header, Strict-Transport-Security, that tells a browser to use only HTTPS for that site from then on.

This example shows a site redirecting HTTP to HTTPS, then what -k really does to the certificate checks.

Example 5 · HTTPS by default, and what -k really does

Terminal
curl -sI http://github.comcurl -sI https://github.comcurl -k -s -o page.html -w "%{http_code}\n" https://self-signed.badssl.com/
Sample output (trimmed)
HTTP/1.1 301 Moved Permanently
Content-Length: 0
Location: https://github.com/

HTTP/1.1 200 OK
...
Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
...
200

What this code does

  • -I asks for the response headers only. Plain http://github.com gets no page, just a redirect (301) to the https:// address. The HTTPS response adds Strict-Transport-Security: "use HTTPS only for this site for the next year (31,536,000 seconds)".
  • With -k, the self-signed site "works" (200). Nothing was fixed: curl simply stopped checking who it was talking to. A fake server would have been accepted the same way, which is why -k belongs only in a quick experiment, never in scripts, apps or answers you share.

Every API should use HTTPS, even a small test one: API requests carry passwords, tokens and personal data, and test servers often become real ones. Big sites also redirect plain http:// to https://.

curl's -k (--insecure) option, and the same switch in any HTTP library, makes certificate errors disappear by skipping the checks. The connection is still encrypted, but you no longer know who is at the other end, so a fake server in the middle can read everything. Fix the cause instead: renew the expired certificate, use the host name the certificate was issued for, or, for a test server on your own laptop, make your computer trust its certificate (tools such as mkcert do this safely).

Seen -k or "disable SSL verification" in a copied answer online? Treat it as a warning sign, not a fix. It must never reach code that other people run.

Next step

Check your understanding

12 quick questions