Sub topic 7
How two peers agree on keys
This chapter assumes you know the basics of confidentiality, integrity and authentication from the previous chapter. It also assumes you are comfortable with IP addressing and the Linux command line.
The problem
Encrypting tunnel traffic needs a shared secret key. The two peers sit on opposite sides of a network they do not trust, so they cannot just send the key across it. Key exchange solves this. The peers end up with the same session key, and an eavesdropper who records every packet still cannot compute it.
Agreeing on a key is not enough, though. Each peer must also know who is at the other end. Otherwise an attacker in the middle can agree on one key with you and another with the server, then relay traffic between you. A key exchange therefore has two jobs: derive a shared secret and authenticate the peers.
Diffie-Hellman in one picture
Diffie-Hellman (DH) is a method for deriving a shared secret over an open channel. Each side generates a private value and computes a public value from it. They swap the public values. Each side then combines its own private value with the other's public value. Both get the same result. An observer sees only the public values, and recovering the secret from them is computationally infeasible.
Modern protocols use the elliptic-curve version. X25519 is the common choice. IPsec, TLS and WireGuard all use it or an equivalent.
Authentication: who is the other end?
Plain DH has no identity. Protocols add one of these:
| Method | How a peer proves identity | Used by |
|---|---|---|
| Pre-shared key (PSK) | Both sides hold the same secret in advance | IPsec, simple setups |
| Certificate | A trusted certificate authority (CA) signed the peer's public key | TLS VPNs, IPsec |
| Raw public key | Each side is configured with the other's public key | WireGuard, SSH host keys |
A certificate is a document that binds a name to a public key. A CA signs it. A peer that trusts the CA can check the signature and then trust the binding. This scales to many users, because you do not configure every pair of peers by hand.
A raw public key needs no CA. You copy the peer's key through a channel you trust, such as a ticket or a secure shell session. This is simple and has very little to go wrong, but you must manage distribution yourself.
Forward secrecy
Suppose an attacker records all your encrypted traffic today. Next year they steal a long-term private key. Can they decrypt the old recordings?
If the session key was derived from fresh, temporary DH values that were then deleted, they cannot. This property is forward secrecy. The long-term key only proves identity. It does not protect past sessions. Without forward secrecy, for example when a session key is simply encrypted under a long-term key, one stolen key exposes all recorded history.
Good VPNs rekey regularly. WireGuard, for example, replaces its session keys about every two minutes during active use.
Generating a key pair
WireGuard uses raw public keys. This command makes a private key and derives the public key from it:
umask 077
wg genkey | tee privatekey | wg pubkey > publickeyumask 077 makes the new files readable only by you. Share publickey with the other peer. Never share privatekey. The public key is safe to publish because the private key cannot be recovered from it.
Common errors
- Copying the wrong key. A peer that holds a wrong public key will reject the handshake, or worse, accept a key that belongs to an attacker. Compare fingerprints over a second channel.
- Weak PSKs. A short or guessable PSK can be attacked offline if an attacker captures a handshake. Use long random values.
- Skipping certificate checks. Turning off validation to "make it work" removes the authentication half of the exchange.
- Large clock errors. Certificates have validity dates, so a badly wrong clock makes valid certificates look expired.
Exercise
An attacker records a month of your VPN traffic, then steals the server's long-term private key. Why can they still not read the recorded traffic if the VPN has forward secrecy?
Show answer
With forward secrecy, each session key came from temporary Diffie-Hellman values that were deleted after use. The long-term key only authenticated the peers. It cannot recreate those session keys, so the recorded traffic stays unreadable.
Exercise
Why is Diffie-Hellman alone not enough to build a safe tunnel?
Show answer
It derives a shared secret but does not prove who holds the other half. An attacker in the middle can run one exchange with each side. The protocol must also authenticate the peers with a PSK, certificates or known public keys.