HTTPS: the TLS 1.3 handshake
How a browser and server agree on secret keys over a public network, and how the certificate stops impostors.
Watch it animatedStep through it, with the running line highlighted. Free with an account.The idea
Both sides contribute a key share. From the two shares each side computes the same secret without ever sending it. The certificate, signed by a trusted certificate authority, proves the server owns the domain, so a man in the middle cannot pretend to be it.
Code
// What the client checks, in order (simplified)
void Handshake()
{
SendClientHello(supportedCiphers, keyShare);
var (serverShare, cert) = ReceiveServerHello();
if (!cert.IsSignedByTrustedCA() || cert.Host != host)
throw new AuthenticationException();
var keys = DeriveKeys(myPrivate, serverShare);
SendEncrypted("GET / HTTP/1.1");
}Tips
- TLS gives confidentiality (encrypted), integrity (tamper-proof) and server authentication.
- HTTPS does not make a site safe from XSS or SQL injection; it protects data in transit only.
- HSTS tells browsers to always use HTTPS for a site, closing the downgrade gap.
In the .NET library
In ASP.NET Core, UseHttpsRedirection and UseHsts enforce HTTPS; certificates are usually terminated at a reverse proxy or load balancer.