DEVOPS, SRE AND CLOUD · PART 2 · NETWORKING FOR DEVOPS · SECURITY · 38 OF 187

TLS and Certificates: Handshake, PKI, mTLS

TLS hands-on: what it guarantees, the TLS 1.3 handshake, certificates and chains of trust built with our own lab CA, SAN names, the exact errors when trust or the name is wrong (curl exit 60), TLS 1.3 vs 1.2 ciphers seen with openssl s_client, a strict Python 3.13 check that rejected our CA, expiry, Let's Encrypt and ACME, cert-manager, and mutual TLS.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
1 / 12

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Privacy, integrity, identity
  • 1-RTT handshake
  • CA chain
  • SAN must match
  • exit 60
  • s_client
  • automate renewal

Notes

Three guarantees

TLS gives three guarantees. Confidentiality: nobody on the network path can read the traffic. Integrity: nobody can change it without detection. Authentication: you are really talking to the server you meant, proved by its certificate. Encryption without authentication is useless, because an attacker in the middle could simply present their own key. And TLS protects data in transit only, not a compromised server.

TLS provides
  • confidentiality: nobody on the path can read it
  • integrity: nobody can change it unnoticed
  • authentication: you are talking to the real server (its certificate proves it)
  • it does NOT protect the endpoints themselves

The handshake

The TLS 1.3 handshake takes one round trip. The client says hello with supported versions and ciphers, a key share, the server name it wants, called SNI, and ALPN protocols. The server replies with its choices, its own key share, its certificate and a signature proving it holds the private key. Both sides derive the same session keys from the key shares, and the client checks the certificate. Then encrypted data flows.

TLS 1.3, simplified
  • ClientHello: versions, ciphers, key share, SNI name, ALPN
  • ServerHello: chosen cipher, its key share + certificate, signature
  • both derive the same session keys (ECDHE)
  • client verifies the cert
  • 1 round trip, then data

Chain of trust

Why should the client believe a certificate? Because a certificate authority it already trusts signed it. We built this for real: a lab CA with its own key and certificate, then a key and signing request for shoprung dot test, signed by the CA. The certificate names shoprung dot test in its subject alternative names and is valid for one month. A client that trusts our CA now trusts this certificate. Browsers and operating systems ship with a list of trusted public CAs.

openssl (our lab)
ca.key + ca.crt
  "ShopRung Lab CA" (EC P-256)
shop.key + CSR → signed by CA
shop.crt  CN=shoprung.test
  SAN DNS:shoprung.test
  Dec 2 2026 → Jan 1 2027

client trusts CA → trusts
  every cert the CA signed

It works

With our CA passed to curl, the connection worked: TLS 1.3 with an AES two fifty six cipher, subject shoprung dot test, issuer our lab CA, and the host name matched the certificate's alternative name. curl also printed the expiry date, the first thing to check in any certificate incident. ShopRung answered two hundred through nginx over HTTP two.

curl -v --cacert ca.crt
SSL connection using TLSv1.3
  / TLS_AES_256_GCM_SHA384
subject: CN=shoprung.test
issuer: CN=ShopRung Lab CA
subjectAltName: host
  "shoprung.test" matched
expire date: Jan 1 2027
HTTP/2 200

Break it twice

Now break it. Without our CA, curl failed with unable to get local issuer certificate: it could not build a chain to anything it trusts. In production this usually means the server forgot to send the intermediate certificate. Calling the server by IP address failed differently: no alternative certificate subject name matches the target host name. Both exit with code sixty. The wrong fix is minus k, which turns off verification and with it the whole point of TLS.

curl exit 60 (ours)
without --cacert:
  SSL certificate problem:
  unable to get local issuer
  certificate
https://127.0.0.1:8443:
  no alternative certificate
  subject name matches
  target host name
never fix with -k in prod

Read it with openssl

openssl s client is the TLS debugging tool. Against our nginx it negotiated TLS 1.3 and verified the chain with return code zero. Forcing TLS one point two gave an ECDHE ECDSA AES cipher instead, which shows nginx allowed both versions. openssl x509 reads a certificate file's dates and names. Many teams now disable everything below TLS one point two.

openssl s_client (ours)
default:
  TLSv1.3, TLS_AES_256_GCM_
  SHA384, Verify return 0
-tls1_2:
  ECDHE-ECDSA-AES256-GCM-
  SHA384
$ openssl x509 -noout
    -dates -ext subjectAltName
  < shop.crt

Strict clients

One real surprise. curl and openssl accepted our lab CA, but Python three point thirteen refused it: CA cert does not include key usage extension. Newer Python versions verify strictly by default. Different clients enforce different rules, so a certificate that works in your browser can still break one service. The fix is to create internal CAs properly, with key usage and basic constraints, or to use a real internal PKI.

Real surprise
ssl.SSLCertVerificationError:
  CA cert does not include
  key usage extension

curl and openssl: accepted
Python 3.13 strict mode:
  rejected
fix: make CAs with proper
  keyUsage + basicConstraints

Automate renewal

Certificates expire, so automate renewal. Let's Encrypt issues free certificates through the ACME protocol, with tools like certbot. Cloud load balancers use managed certificates, like AWS Certificate Manager. In Kubernetes, cert manager renews them. Still, monitor days until expiry for every endpoint. And for service to service security there is mutual TLS, where the client presents a certificate too; service meshes, in part ten, do this automatically.

In practice
  • Let's Encrypt: free 90-day certs via ACME (certbot)
  • cloud: ACM on load balancers
  • Kubernetes: cert-manager renews automatically
  • monitor days-to-expiry for every endpoint
  • mTLS: client shows a cert too (service mesh)

Common mistakes

Mistakes. Turning verification off in production code. Serving a certificate without its intermediate. No expiry monitoring. A certificate missing the name clients actually use, often www or an internal name. Private keys committed to Git. And old TLS versions left enabled.

Avoid these
  • curl -k / verify=False in production code
  • missing intermediate cert
  • no expiry monitoring
  • cert without the right SAN
  • private keys in Git
  • TLS 1.0/1.1 still enabled

Say it like this

In the interview: during the TLS handshake the server sends its certificate chain and proves it holds the private key with a signature. The client checks the chain up to a CA it trusts, that the host name is in the subject alternative names, and that the dates are valid. Both sides derive session keys with ephemeral Diffie Hellman. Operationally, I automate renewal and alert on days to expiry.

In the interview
  • "In the handshake the server
  • sends its certificate and signs
  • with its private key. The client
  • checks the chain up to a trusted
  • CA, the name in the SAN, and the dates. Then both derive session
  • keys with ECDHE. I automate
  • renewal and alert on expiry."

Interview questions

Q1What does TLS provide?

Confidentiality (encryption), integrity (tamper detection) and authentication of the server, and optionally the client, through certificates.

Q2How does a client verify a server certificate?

It builds a chain from the leaf through intermediates to a trusted root CA, checks signatures, validity dates and that the hostname matches a SAN entry.

Q3What causes "unable to get local issuer certificate"?

The client cannot build a chain to a trusted CA: usually the server omits the intermediate certificate, or the CA is private and not in the trust store.

Q4What is SNI?

Server Name Indication: the client sends the hostname in the ClientHello so a server hosting many sites can choose the right certificate.

Q5What is mutual TLS?

Both sides present certificates, so the server also authenticates the client; common between microservices and in service meshes.

Q6How do you prevent certificate expiry outages?

Automate issuance and renewal (ACME/Let's Encrypt, ACM, cert-manager) and monitor days-to-expiry for every endpoint with alerts well in advance.

Q7What is the difference between TLS 1.2 and 1.3?

TLS 1.3 needs one round trip (zero on resumption), removes old insecure ciphers and always uses forward-secret key exchange.