Catalog My learning Blog
Application Security for Developers: Secure Coding Course · Phase 3 · Lesson 18 · 4 min read

TLS Handshake and Certificate Validation Explained

On this page
  1. Why encrypted isn't the same as secure
  2. The TLS 1.3 handshake and forward secrecy
  3. What a client checks in certificate validation
  4. Code that silently disables TLS verification
  5. Hardening TLS: versions, HSTS, mTLS and pinning
  6. Check yourself

Why encrypted isn't the same as secure

A connection can be encrypted and still go straight to an attacker. Encryption hides traffic from eavesdroppers; certificate validation is what proves you're talking to the real server. Most real-world TLS failures in applications aren't broken cryptography; they're validation that was disabled.

The TLS 1.3 handshake and forward secrecy

  1. ClientHello: supported versions and ciphers, and the client's ephemeral key share.
  2. ServerHello: the chosen cipher and the server's key share. Both derive the same session keys; everything after this is encrypted.
  3. The server sends its certificate chain and a signature over the handshake with its private key, proving it holds the key for that certificate.
  4. The client validates the certificate and the signature, then both sides exchange Finished messages.

One round trip. Because the key shares are ephemeral, stealing the server's private key later can't decrypt recorded sessions: forward secrecy. (The networking course shows a live handshake with openssl s_client.)

What a client checks in certificate validation

Think of a passport. A border officer trusts it not because of what it says about you, but because a government they recognize issued it. A certificate works the same way: the server's certificate is vouched for by an intermediate certificate authority (CA), which is vouched for by a root CA that your operating system or browser already trusts.

Code
1. Chain of trust   The certificate is signed by an intermediate, which is signed by a root CA
                    in the client's trust store.
2. Validity dates   Today is between notBefore and notAfter.
3. Hostname         The requested name matches a Subject Alternative Name (wildcards cover one level).
4. Key usage        The certificate is allowed for server authentication.
5. Revocation       Not revoked (OCSP stapling or CRLs; browsers also use their own revocation lists).
6. Proof of key     The server signed the handshake with the matching private key.

Fail any of them and a correct client refuses to connect. Skip check 3, and any valid certificate for any domain, which anyone can get for free, impersonates you.

The handshake agrees on keys, then the client walks the certificate chain to a trusted root and checks dates, hostname and the server's proof of key ownership before trusting the connection.
The handshake agrees on keys, then the client walks the certificate chain to a trusted root and checks dates, hostname and the server's proof of key ownership before trusting the connection.

Code that silently disables TLS verification

These appear in real codebases, usually added "temporarily" to get past a certificate error:

Code
requests.get(url, verify=False)                       # Python
Code
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';       // Node: disables checks for the whole process
new https.Agent({ rejectUnauthorized: false });
Code
handler.ServerCertificateCustomValidationCallback = (m, c, ch, e) => true;   // .NET
Shell
curl -k https://api.internal.example.com/            # or --insecure

Each one accepts any certificate, so anyone on the network path can intercept and modify traffic. Search for them in code review and with SAST rules.

Fix the real problem instead:

  • Internal CA? Add your CA certificate to the client's trust store (REQUESTS_CA_BUNDLE, NODE_EXTRA_CA_CERTS, the system store).
  • Missing intermediate on the server? Serve the full chain.
  • Hostname mismatch? Issue a certificate with the right names.
  • Expired? Automate renewal with ACME.

Hardening TLS: versions, HSTS, mTLS and pinning

  • TLS 1.2 and 1.3 only; disable SSL 3, TLS 1.0 and 1.1.
  • HSTS so browsers never fall back to HTTP (Day 25).
  • Mutual TLS (mTLS) between services, where both sides present certificates, often managed by a service mesh.
  • Certificate pinning only with great care: it can lock out your own clients when you rotate certificates. Mobile apps that pin must ship backup pins.
  • Monitor Certificate Transparency logs for certificates issued for your domains that you didn't request.

Check yourself

Any certificate from a trusted CA, including one an attacker obtains for their own domain, would be accepted for your server.

Previously recorded sessions; because session keys came from ephemeral key exchange, a later theft of the server's private key can't decrypt them.

Add the internal CA to the client's trusted certificates, or fix the server's certificate chain or hostname. Disabling verification makes the connection open to interception and tends to spread into production code.

Browsers ship with a list of HSTS domains, so even the first visit uses HTTPS. Removal is slow, so only preload once every subdomain supports HTTPS.

Both sides present certificates, so the server also authenticates the client cryptographically. It's common for service-to-service traffic in zero trust networks and service meshes, where every call should prove which service is making it.