HTTPS, TLS and Certificates Explained: How a Secure Connection Works

By Furi Sherpa Lama · October 28, 2026 · 19 min read

Every website you deploy needs HTTPS. Without it, browsers show "Not secure" next to your domain, and anything your users type can be read by anyone on the network path. Getting a certificate takes a few commands. Understanding what it actually does takes a bit longer, and that understanding is what helps you when a certificate breaks at 2 a.m.

This post covers why HTTP is not secure, how encryption works, what certificates and certificate authorities are, how the chain of trust works, and what happens during a TLS handshake. Then it gets practical: inspecting any site's certificate with openssl and curl, getting a free certificate from Let's Encrypt (or letting Caddy handle it all automatically), making a self-signed certificate for testing, and fixing the most common certificate errors.

All you need to follow along is a terminal with curl and openssl, which Linux and macOS already have. For the Let's Encrypt section you also need a Linux server with a domain pointing at it.

This post started as my notes on HTTPS, SSL, TLS & Certificate Authority Explained by Laiture, which is a great visual introduction. I've updated a few points to match how TLS works today, and added the commands I use when working with certificates.

Why HTTP Is Not Secure

When you log in to a website, your email and password leave your machine and travel to a server. On the way they pass through your Wi-Fi, your router, your ISP, several networks on the internet, and the server's own network.

Every one of those hops can see the traffic. Wi-Fi is radio, so anyone nearby with a cheap receiver can capture it. Anyone who controls a router on the path can read it, change it, or redirect it.

With plain HTTP, the data is sent as readable text. Whoever captures it can read your password as easily as the server can. That's why browsers mark HTTP sites as "Not secure".

HTTPS fixes this. You can see the difference on any site that uses it:

curl -sI http://furi.info.np
HTTP/1.0 308 Permanent Redirect
Location: https://furi.info.np/

The server refuses to talk over plain HTTP and redirects you to HTTPS. Every site should do this.

HTTPS, SSL and TLS

These three names get mixed up all the time:

  • HTTPS is HTTP sent over a secure connection. It uses port 443 instead of HTTP's port 80.
  • TLS (Transport Layer Security) is the protocol that makes the connection secure.
  • SSL (Secure Sockets Layer) is the old name for TLS. Every version of SSL is broken and switched off, but people still say "SSL certificate". It means the same thing as a TLS certificate.

Today you'll only see TLS 1.2 and TLS 1.3. Versions 1.0 and 1.1 are deprecated, and browsers refuse them.

TLS gives you three things:

  1. Encryption. Nobody on the path can read the data.
  2. Integrity. Nobody can change the data without it being detected.
  3. Authentication. You're talking to the real server, not someone pretending to be it.

To understand how it does that, you need two kinds of encryption.

Symmetric Encryption

Symmetric encryption uses one key to both encrypt and decrypt. A key is a random string of bits. You feed your data and the key into an algorithm such as AES-256, and you get scrambled data that's useless to anyone without the key. Feed the scrambled data and the same key back in, and you get the original.

Symmetric encryption is very fast, so it's what TLS uses for the actual data you send.

The problem is getting the key to the server. If your laptop creates a key and sends it across the network, anyone watching captures the key along with the encrypted data, and decrypts everything. You need a way to agree on a key without anyone else learning it.

Asymmetric Encryption

Asymmetric encryption uses two keys that belong together: a public key and a private key.

  • Data encrypted with the public key can only be decrypted with the private key.
  • The private key can also sign data. Anyone with the public key can check that signature, but nobody can create one without the private key.

The public key can be shared with the whole world. The private key never leaves its owner. If you read my SSH post, this is the same idea as your SSH key pair.

Asymmetric encryption is much slower than symmetric encryption, so TLS uses it only at the start of a connection, to set up a shared symmetric key.

Agreeing on a shared key

There have been two ways to do this.

The old way: RSA key transport. The server sends its public key. Your browser creates a symmetric key, encrypts it with the server's public key, and sends it back. Only the server's private key can decrypt it. Many explanations, including the video, describe this method. It worked in TLS 1.2 and earlier, but it has a weakness: if the server's private key ever leaks, an attacker who recorded your old traffic can decrypt all of it.

The modern way: key exchange. Each side generates a temporary key pair just for this connection and sends the other its public half. Each side then combines its own private half with the other side's public half, and the math gives both sides the same secret. The secret itself never crosses the network, so there's nothing to capture. The temporary keys are thrown away after the connection, so a leaked server key can't decrypt past traffic. This is called forward secrecy.

TLS 1.3 only allows the modern way. The common algorithm is X25519. Newer browsers and servers combine it with ML-KEM, a post-quantum algorithm, so traffic recorded today stays safe even against future quantum computers. You'll see this as X25519MLKEM768 in curl output later.

The man-in-the-middle problem

Key exchange keeps the key secret from someone who only watches. But what about someone who sits in the middle and answers for the server?

An attacker on your network intercepts your connection and does the key exchange with you, using their own keys. They do a second key exchange with the real server. Now they decrypt everything you send, read it, re-encrypt it, and pass it on. Neither you nor the server notices.

Encryption alone can't stop this. Your browser needs a way to prove that the public key it received really belongs to the site it asked for. That proof is a certificate.

Certificates and Certificate Authorities

A certificate is a small file that says "this public key belongs to this domain", vouched for by someone your browser trusts. That someone is a certificate authority (CA), such as Let's Encrypt, DigiCert or Sectigo.

What's inside a certificate

You can read any site's certificate with one command:

echo | openssl s_client -connect furi.info.np:443 -servername furi.info.np 2>/dev/null \
  | openssl x509 -noout -subject -issuer -dates -ext subjectAltName
subject=CN=*.furi.info.np
issuer=C=US, O=Let's Encrypt, CN=YR1
notBefore=Sep 24 12:31:21 2026 GMT
notAfter=Dec 23 12:31:20 2026 GMT
X509v3 Subject Alternative Name:
    DNS:*.furi.info.np, DNS:furi.info.np

The important parts:

  • Subject and Subject Alternative Name (SAN): the domains the certificate is valid for. Browsers only look at the SAN list. This one covers furi.info.np and any single subdomain, like www.furi.info.np.
  • Issuer: the CA that signed it. Here it's Let's Encrypt's YR1.
  • Validity: notBefore and notAfter. Outside those dates, the certificate is rejected.
  • Public key: the site's public key.
  • Signature: the CA's signature over everything above.

How a CA signs a certificate

The CA takes all the certificate's contents, runs them through a hash function (such as SHA-256) to get a short fingerprint, and signs that fingerprint with its private key.

Your browser checks it in reverse. It hashes the same contents itself, then uses the CA's public key to check the signature. If one byte of the certificate was changed, such as the domain or the public key, the hashes won't match and the browser rejects it.

You'll often hear this described as "the CA encrypts the server's public key with its private key". That's a useful picture, but it's not quite what happens: the CA signs a hash of the whole certificate, and checking a signature isn't the same as decrypting.

How a site gets a certificate

This happens once, long before any visitor shows up, not during each connection:

  1. The server generates its own key pair. The private key never leaves the server.
  2. It creates a certificate signing request (CSR) containing the public key and the domain names.
  3. The CA checks that the requester really controls the domain. Let's Encrypt does this automatically with the ACME protocol: it asks you to serve a token at http://your-domain/.well-known/acme-challenge/ (HTTP-01) or to create a DNS TXT record (DNS-01).
  4. The CA signs the certificate and sends it back.

You can create a key and CSR yourself to see what one looks like:

openssl req -new -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -noenc \
  -keyout example.key -out example.csr -subj "/CN=example.com"

In practice, tools like Certbot do all of this for you.

Proving the server owns the certificate

A certificate is public. Anyone can download yours, as you just did with openssl. So what stops an attacker from presenting your certificate as their own?

During the handshake, the server signs the handshake data with its private key. Your browser checks that signature with the public key from the certificate. An attacker has the certificate but not the private key, so they can't produce that signature. In curl -v output this step shows up as CERT verify.

That's why protecting the private key matters so much. Whoever has it can pretend to be your site.

Where browsers get CA keys

Your browser doesn't fetch CA keys during the connection. Your operating system and browser ship with a trust store: a list of root CA certificates they trust, around 150 of them. Every CA in that list had to pass strict audits to get in.

On Debian and Ubuntu the system trust store lives at /etc/ssl/certs/ca-certificates.crt. You'll see curl load it in the next sections.

The Chain of Trust

Root CAs don't sign website certificates directly. Their private keys are far too valuable: if one leaked, every certificate it ever signed would be worthless. So root keys are kept offline in secure hardware, and they're used only to sign a few intermediate certificates. The intermediates sign the website certificates.

That gives you a chain:

Diagram of the chain of trust for furi.info.np: the server certificate is signed by the Let's Encrypt YR1 intermediate, which is signed by Root YR, which is cross-signed by ISRG Root X1, the root already in the browser's trust store

Your browser checks every link, starting from the site:

  1. The server certificate (*.furi.info.np) is signed by the intermediate YR1. The browser checks that signature with YR1's public key.
  2. YR1 is signed by Root YR, a newer Let's Encrypt root. The browser checks that signature with Root YR's public key.
  3. Root YR is new, so many devices don't have it in their trust store yet. The server sends a copy of it cross-signed by the older ISRG Root X1, so those devices can still follow the chain.
  4. ISRG Root X1 is already in the trust store, so the chain ends there. A root certificate is self-signed: its subject and issuer are the same.

If any signature fails, any certificate has expired, or the chain doesn't end at a trusted root, the connection is refused.

You can see the whole chain a server sends:

openssl s_client -connect furi.info.np:443 -servername furi.info.np </dev/null

Terminal output of openssl s_client for furi.info.np showing three certificates in the chain, TLS 1.3, and verify return code 0 (ok)

The depth lines at the top show openssl verifying the chain from the trusted root (depth 3) down to the site (depth 0). Under Certificate chain, each certificate shows its subject (s:) and issuer (i:), and the issuer of each one is the subject of the next. Certificate 2 is the cross-signed Root YR. The server doesn't send ISRG Root X1 itself, because your machine already has it. Verify return code: 0 (ok) means the whole chain checked out.

The server must send its own certificate and the intermediates. The browser only has the roots. In Let's Encrypt terms, that's why you configure your web server with fullchain.pem, not cert.pem. Forgetting this is one of the most common certificate mistakes.

See it in your browser

Click the icon to the left of the address bar, then Connection is secure, then Certificate is valid. You'll see who the certificate was issued to, who issued it, and when it expires. The Details tab shows the full chain, from the site's certificate up to the root.

The TLS 1.3 Handshake, Step by Step

Now all the pieces fit together. This is what happens in the first few milliseconds of every HTTPS connection:

Diagram of the TLS 1.3 handshake between a client and a server: Client Hello with key share, Server Hello with key share, then the encrypted certificate, certificate verify and finished messages, then the client's finished message and encrypted HTTP data

  1. Client Hello. Your browser says which TLS versions and ciphers it supports, which domain it wants (called SNI, so one server can host many sites), and sends its half of the key exchange.
  2. Server Hello. The server picks the version and cipher and sends its half of the key exchange. Both sides now calculate the same shared secret. Everything after this point is encrypted.
  3. Certificate. The server sends its certificate and the intermediates.
  4. Certificate Verify. The server signs the handshake with its private key, proving it owns the certificate.
  5. Finished. The server sends a checksum of the whole handshake, so any tampering is detected.
  6. The browser checks the chain up to a trusted root, that the domain is in the SAN list, that today's date is within the validity period, and the Certificate Verify signature. Then it sends its own Finished.
  7. Application data. HTTP requests and responses now flow, encrypted with the shared symmetric key.

TLS 1.3 does all of this in a single round trip. You can watch it happen:

curl -svI https://furi.info.np 2>&1 | grep '^\*'
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* SSL Trust Anchors:
*   CAfile: /etc/ssl/certs/ca-certificates.crt
* 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 handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_128_GCM_SHA256 / X25519MLKEM768 / RSASSA-PSS
* Server certificate:
*   subject: CN=*.furi.info.np
*   expire date: Dec 23 12:31:20 2026 GMT

Every step from the list is there. The connection line tells you the whole story: TLS 1.3, AES-128-GCM for the data (symmetric), X25519 with ML-KEM for the key exchange, and an RSA signature to prove the server's identity.

Inspecting Certificates

These are the commands I use most when working with certificates.

Check when a certificate expires

echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null \
  | openssl x509 -noout -dates

Always pass -servername. Without it, a server that hosts several sites may send you the wrong certificate.

Check a certificate file on disk

openssl x509 -in /etc/letsencrypt/live/example.com/cert.pem -noout -text

-text prints everything: the SAN list, the key type, the signature algorithm and more.

Alert before it expires

-checkend takes a number of seconds and tells you whether the certificate expires within that time. This checks for the next 14 days:

openssl x509 -in cert.pem -noout -checkend 1209600

It prints Certificate will expire or Certificate will not expire, and exits with 1 or 0, so it's easy to use in a script or a cron job.

Getting a Free Certificate With Let's Encrypt

Let's Encrypt is a free, automated CA, and it's what most sites use. Certbot is the official client. These steps are for Ubuntu with Nginx.

Before you start, make sure:

  • Your domain's DNS A record points at your server's public IP.
  • Port 80 is open, because Let's Encrypt checks your domain over HTTP. Port 443 should be open too.
  • Nginx has a server block with server_name example.com www.example.com;.
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

Install Certbot and get a certificate

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com

Certbot proves you control the domain, gets the certificate, edits your Nginx config to use it, and can add the HTTP to HTTPS redirect for you. Your files end up in /etc/letsencrypt/live/example.com/:

File What it is
privkey.pem Your private key. Never share it or commit it to Git.
cert.pem Your certificate alone
chain.pem The intermediate certificates
fullchain.pem Your certificate plus the intermediates. This is what Nginx should serve.

Certbot sets up Nginx like this:

ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

Renewal

Let's Encrypt certificates last 90 days, and the industry is moving everyone to short lifetimes. Since March 2026, no public certificate can be valid for more than 200 days. That drops to 100 days in 2027 and 47 days in 2029. Manual renewal won't scale. Automate it.

Certbot installs a timer that renews certificates automatically. Check that it exists, and test renewal without changing anything:

systemctl list-timers | grep certbot
sudo certbot renew --dry-run

If the dry run succeeds, renewal will work on its own.

The Automatic Way: Caddy

With Nginx, HTTPS is a separate job: Certbot gets the certificate, edits the config, and a timer renews it. Caddy is a web server that does all of that itself. You give it a domain name, and it gets a certificate from Let's Encrypt, serves the full chain, redirects HTTP to HTTPS, and renews the certificate before it expires. There's no Certbot and no renewal timer to check.

The requirements are the same as for Certbot: the domain's DNS must point at the server, and ports 80 and 443 must be open.

Install Caddy

On Ubuntu or Debian, from Caddy's official repository:

sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo chmod o+r /usr/share/keyrings/caddy-stable-archive-keyring.gpg
sudo chmod o+r /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy

The package starts Caddy as a systemd service called caddy. Stop Nginx first if it's running, since only one of them can use ports 80 and 443.

Configure it

Replace the contents of /etc/caddy/Caddyfile with your domain and what to serve. This sends traffic to an app running on port 3000:

example.com, www.example.com {
    reverse_proxy localhost:3000
}

That's the whole config. Reload Caddy and watch it get the certificate:

sudo systemctl reload caddy
sudo journalctl -u caddy -f

Within a few seconds the log shows Caddy completing the challenge and obtaining a certificate for each name. If Let's Encrypt fails, Caddy tries ZeroSSL, another free CA. Check the result the same way as any other site:

curl -svI https://example.com 2>&1 | grep -E 'SSL connection|subject|expire'

Nginx or Caddy?

Nginx + Certbot Caddy
Getting a certificate Run certbot --nginx Automatic
Renewal Certbot's timer Built in
HTTP to HTTPS redirect Certbot can add it Automatic
Full chain You point Nginx at fullchain.pem Automatic
Config Longer, very flexible A few lines
Good for Existing Nginx setups, complex configs. It's also what most job ads ask for. New servers, side projects, home labs

Even with Caddy, everything earlier in this post still matters. When Caddy can't get a certificate, it's for the reasons you now understand: DNS pointing at the wrong server, port 80 blocked so the challenge fails, or a clock that's wrong. The log tells you which, and the troubleshooting section below applies to Caddy too.

A Self-Signed Certificate for Testing

For local development or an internal test server, you can make a certificate that you sign yourself. Browsers won't trust it, because no CA in their trust store signed it, but it's fine for testing.

openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -noenc \
  -keyout key.pem -out cert.pem -days 30 \
  -subj "/CN=localhost" -addext "subjectAltName=DNS:localhost"
  • -x509 makes a certificate instead of a signing request.
  • -noenc leaves the private key without a passphrase, so a server can load it without asking. (On OpenSSL older than 3.0, use -nodes.)
  • -addext "subjectAltName=..." adds the SAN. Without it, browsers and curl reject the certificate even if the CN matches.

Start a test TLS server with it:

openssl s_server -cert cert.pem -key key.pem -accept 8443 -www

In another terminal, curl rejects it because it's self-signed:

curl https://localhost:8443
curl: (60) SSL certificate OpenSSL verify result: self-signed certificate (18)

Tell curl to trust that one certificate, and it works:

curl --cacert cert.pem https://localhost:8443

That's the safe way to test. You might see curl -k (or --insecure) recommended online. It turns off certificate checking completely, which removes the protection against man-in-the-middle attacks. Never use it in scripts or production.

Troubleshooting Certificate Errors

These are the errors you'll actually see. The messages are from curl. Browsers show the same problems with codes like NET::ERR_CERT_DATE_INVALID.

Error Usually means Fix
certificate has expired The certificate's notAfter date has passed Renew it, then fix whatever stopped automatic renewal
no alternative certificate subject name matches target hostname The domain isn't in the certificate's SAN list Reissue the certificate with every name you serve, such as both example.com and www.example.com
unable to get local issuer certificate The server isn't sending its intermediate certificates Serve fullchain.pem, not cert.pem
self-signed certificate The certificate wasn't signed by a trusted CA Use a real CA, or trust it explicitly with --cacert for testing
self-signed certificate in certificate chain The chain ends at a root your machine doesn't trust Often a corporate proxy, or an old trust store. Update ca-certificates.
certificate is not yet valid The certificate's start date is in the future Usually the client's clock is wrong. Check it with timedatectl.

When something's wrong, start with:

openssl s_client -connect example.com:443 -servername example.com </dev/null

Look at the certificate chain, the dates, and the Verify return code at the bottom. It names the exact problem.

To practise, badssl.com has test sites for every one of these errors, such as expired.badssl.com and wrong.host.badssl.com. Point curl at them and see what each failure looks like.

HTTPS and TLS Cheat Sheet

Task Command
Watch a TLS handshake curl -svI https://example.com
Show the certificate chain openssl s_client -connect example.com:443 -servername example.com </dev/null
Show expiry dates ... | openssl x509 -noout -dates
Show the domains covered ... | openssl x509 -noout -ext subjectAltName
Read a certificate file openssl x509 -in cert.pem -noout -text
Expires within 14 days? openssl x509 -in cert.pem -noout -checkend 1209600
Create a key and CSR openssl req -new -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -noenc -keyout example.key -out example.csr -subj "/CN=example.com"
Self-signed certificate openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -noenc -keyout key.pem -out cert.pem -days 30 -subj "/CN=localhost" -addext "subjectAltName=DNS:localhost"
Test TLS server openssl s_server -cert cert.pem -key key.pem -accept 8443 -www
Trust one certificate curl --cacert cert.pem https://localhost:8443
Get a Let's Encrypt certificate sudo certbot --nginx -d example.com -d www.example.com
Test renewal sudo certbot renew --dry-run
List certificates Certbot manages sudo certbot certificates
Reload Caddy after editing the Caddyfile sudo systemctl reload caddy
Watch Caddy get certificates sudo journalctl -u caddy -f

(... means the echo | openssl s_client ... 2>/dev/null part from the row above it.)

Wrapping Up

The main points:

  1. Symmetric encryption protects your data, and a key exchange sets up the key without ever sending it.
  2. A certificate proves that a public key belongs to a domain, and a CA's signature proves the certificate is real.
  3. Browsers trust a small set of root CAs, and every certificate must chain back to one of them. Always serve the full chain.
  4. Certificate lifetimes are getting shorter every year. Automate renewal and alert on expiry, or use a server like Caddy that handles certificates for you.
  5. When a certificate breaks, openssl s_client usually tells you exactly why.

The best way to learn this is to point openssl s_client at sites you use every day, follow their chains, and break a test server on purpose to see each error.