Why a Relayed TLS Login Fails: the Signature Covers the Handshake
You can fool a client into logging in to your server with its certificate, collect a perfectly valid signature, and still not be able to use it anywhere else. What the client signed was tied to that particular conversation.
Here TLS client authentication differs from a great many login schemes that look similar on a whiteboard.
A signed nonce is easy to relay
Take the naive design: the server sends a random number, the client signs it, the server checks the signature. It proves the client holds the key, and replaying yesterday's answer fails because today's number is different.
It does nothing to stop a live attacker in the middle:
- Mallory convinces Alice to log in to Mallory's server.
- Mallory opens a login to the real server and receives a nonce.
- Mallory hands that same nonce to Alice as their own challenge.
- Alice signs it, and Mallory forwards the signature.
- The real server lets Mallory in as Alice.
No cryptography was broken. The signature says "Alice's key saw this number", but says nothing about who Alice thought they were talking to, or over which connection.
TLS 1.3 signs a hash of everything so far
In TLS 1.3 the client proves possession of its key with a CertificateVerify message. RFC 8446 section 4.4.3 defines the signed input as these, joined together:
- 64 bytes of
0x20(spaces) - the string
TLS 1.3, client CertificateVerify - a single zero byte
- a hash of the handshake transcript up to that point
The interesting part is the transcript. It includes both sides' random values and ephemeral key shares, the server's certificate and signature, the server's Finished, the request for a client certificate and the client's certificate itself.
So Mallory has two connections, Alice to Mallory and Mallory to the real server, with two different transcripts. A signature made for one does not verify on the other.
Quick detour: why 64 spaces?
It looks like someone leaning on the space bar, but the reason is rather clever. In TLS 1.2, a server's ServerKeyExchange signature covers data beginning with the client random and server random: 64 bytes, half of them chosen by the other party.
The TLS 1.3 padding pushes the new context string past that region, so a signature from one protocol version is harder to pass off as the other. The context string also differs between client and server, so neither can be reflected back as its opposite.
Could Mallory make the transcripts match?
That is the obvious next move, and Mallory's choices are roughly these:
- Forward every byte unchanged. The transcripts match and the login succeeds, but the traffic keys come from an exchange between Alice and the real server. Mallory has become a very elaborate network cable.
- Swap in their own key share. Now Mallory can derive keys, but the transcripts differ.
- Copy Alice's
ClientHelloto the server. The server replies with its key share. Mallory lacks Alice's private ephemeral value, so cannot read the rest. Changing the reply to fix that changes Alice's transcript.
Mallory has to choose between matching transcripts and readable traffic. That holds because the same handshake produces the keys and the thing being signed.
Trying the replay in Go
Claims like this are cheap, so here is the attack. crypto/tls accepts any crypto.Signer as a private key, which lets us record what the victim's key produces and then play it back.
// signer lets us watch, or replace, what the TLS stack asks the key to do.
type signer struct {
pub crypto.PublicKey
sign func(digest []byte, opts crypto.SignerOpts) ([]byte, error)
}
func (s signer) Public() crypto.PublicKey { return s.pub }
func (s signer) Sign(_ io.Reader, d []byte, o crypto.SignerOpts) ([]byte, error) {
return s.sign(d, o)
}
The test uses two helpers not shown here. newCert makes a self-signed ECDSA P-256 certificate. handshake runs one TLS 1.3 connection over localhost, with the server set to RequireAndVerifyClientCert and trusting only the victim's certificate, and returns the server's handshake error.
The client in the helper sets InsecureSkipVerify. That is deliberately dreadful, to model a victim who will talk to any server; do not copy it into real code.
victim, evil, real := newCert(t), newCert(t), newCert(t)
key := victim.PrivateKey.(*ecdsa.PrivateKey)
// 1. The victim logs in to the evil server, which keeps the signature.
var captured []byte
recording := victim
recording.PrivateKey = signer{&key.PublicKey, func(d []byte, o crypto.SignerOpts) ([]byte, error) {
sig, err := key.Sign(rand.Reader, d, o)
captured = sig
return sig, err
}}
if err := handshake(t, evil, recording, victim.Leaf); err != nil {
t.Fatalf("victim to evil: %v", err)
}
// 2. The attacker presents the victim's certificate and that signature.
replaying := victim
replaying.PrivateKey = signer{&key.PublicKey, func([]byte, crypto.SignerOpts) ([]byte, error) {
return captured, nil
}}
err := handshake(t, real, replaying, victim.Leaf)
if err == nil {
t.Fatal("replayed signature was accepted")
}
t.Logf("real server said: %v", err)
With Go 1.27.4, the first handshake succeeds and the second is refused:
real server said: tls: invalid signature by the client certificate: ECDSA verification failure
Notice that the victim's carelessness about server certificates did not help the attacker replay anything. Client authentication resisted the relay even though server authentication had been switched off. The victim still sent their data to the wrong server, mind.
A key that signs anything undoes it
Look again at that Sign method: it receives a digest and has no idea what the digest means. If Mallory can get Alice's key to sign an arbitrary digest, they take the one from their own connection to the real server, have it signed, and walk in.
The protection comes from Alice's TLS stack building the input from its own transcript. Malware with access to a smartcard, an over-helpful remote signing service, or a key reused by some other protocol that signs raw challenges can all remove it.
A non-exportable key stops theft of the key, but on its own it cannot stop someone borrowing it.
TLS termination moves the guarantee
The signature is checked by whatever terminates TLS. If that is a reverse proxy, your application only gets the proxy's account of the proof, usually in a header. RFC 9440 standardises Client-Cert for this.
That can be perfectly sound, provided that:
- the proxy strips any copy of the header sent by the client
- the backend accepts connections only from the proxy
- the link between them is protected
A certificate in a header is no substitute for a signature, since certificates are public. Anyone who can reach the backend directly can type one in.
The same applies after login. If the server answers a certificate login with an ordinary cookie or bearer token, that token can be carried to another connection. RFC 8705 shows one remedy for OAuth: bind the access token to the client certificate.
Logins above TLS can borrow the same trick
Suppose you authenticate inside the connection, with your own signature or MAC scheme. TLS can give both ends a value derived from that session's secrets, called an exporter. Include it in what gets signed and a relayed answer stops matching.
For TLS 1.3, RFC 9266 defines the tls-exporter channel binding. In Go:
state := conn.ConnectionState() // conn is a *tls.Conn, after the handshake
binding, err := state.ExportKeyingMaterial("EXPORTER-Channel-Binding", nil, 32)
if err != nil {
return fmt.Errorf("channel binding: %w", err)
}
// Sign or MAC binding together with your challenge.
// The verifier derives its own copy; never accept one sent by the peer.
Older TLS needs more care, and there is a good bit of history behind that. The 2014 "triple handshake" attack arranged for two TLS connections to share a master secret, then used resumption and renegotiation to impersonate a client who used certificates. RFC 7627 fixed the root problem by tying the master secret to the handshake transcript.
The lesson repeats: bind secrets and proofs to the whole exchange, because an attacker may be able to arrange a few individual values. When reviewing a login protocol, ask what would differ in the signed bytes if there were two connections instead of one. If the answer is "nothing", Mallory would like a word.