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.

Time O(1)Space O(1)Code C#
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

In the .NET library

In ASP.NET Core, UseHttpsRedirection and UseHsts enforce HTTPS; certificates are usually terminated at a reverse proxy or load balancer.