In SSH's publickey authentication method, what does the client sign, and why can't that signature be replayed to log in on another connection?
answer
- RFC 4252 section 7
- an unsigned query comes first
- SSH_MSG_USERAUTH_PK_OK
- exchange hash of the first key exchange
basics
~20 sThe client signs the session identifier, the exchange hash of that connection's first key exchange, followed by the user name, service, method, algorithm and public key. Each connection has its own session identifier, so the signature verifies only where it was made.
solid answer
~50 sRFC 4252 section 7 runs `publickey` in up to two steps. The client may first send an unsigned query, an `SSH_MSG_USERAUTH_REQUEST` with the boolean FALSE, the algorithm name and the public key, and the server answers `SSH_MSG_USERAUTH_PK_OK` if that key would be acceptable or `SSH_MSG_USERAUTH_FAILURE` if not. Then the client sends the request with TRUE and a signature over the **session identifier** followed by the request fields: user name, service name, `"publickey"`, the algorithm and the key. The session identifier is the exchange hash H of the connection's first key exchange, computed over both sides' KEXINITs, the host key and fresh key-exchange values, so no two connections share it. The server checks that the key is authorised for the user, then that the signature verifies, so a captured signature fails on any other connection, including one a man in the middle opens to the real server.
code
pseudocode · 12 lines# client -> server: query, no signature
SSH_MSG_USERAUTH_REQUEST(user="deploy", service="ssh-connection",
method="publickey", has_sig=FALSE, alg="ssh-ed25519", key=PUB)
# server -> client
SSH_MSG_USERAUTH_PK_OK(alg="ssh-ed25519", key=PUB) # or ..._FAILURE
# client -> server: proof of possession
sig = Sign(PRIV, session_id || SSH_MSG_USERAUTH_REQUEST || "deploy"
|| "ssh-connection" || "publickey" || TRUE
|| "ssh-ed25519" || PUB)
SSH_MSG_USERAUTH_REQUEST(..., has_sig=TRUE, alg, PUB, signature=sig)
# server: PUB authorised for deploy? sig valid over THIS session_id?
# yes -> SSH_MSG_USERAUTH_SUCCESS (or FAILURE, partial success, if more is required)go deeper
Recall that a key login is a signature check: the server holds the public key, the client signs, and the private key never crosses the wire.
Walk through the messages: the unsigned query, SSH_MSG_USERAUTH_PK_OK, then the signed request whose data starts with the session identifier, and the two checks the server makes.
Use the mechanism to explain incidents: why a captured signature is worthless, why a man in the middle cannot relay a key login but can relay a password, and why re-keying leaves the login valid.
Connect the design to policy: binding authentication to the session identifier is what makes key logins safe against a compromised server, so the residual risk is the client host and who controls its keys.
## Where publickey sits in the protocol SSH user authentication (RFC 4252) starts once the transport layer (RFC 4253) has completed its first key exchange. The transport hands it a **session identifier**: the **exchange hash H** of that first key exchange. RFC 4253 section 7.2 says that once computed, the session identifier is not changed, even if keys are later re-exchanged. The `publickey` method is what puts this value to work: the session identifier is the thing the client's signature commits to. ## The two-step exchange 1. **Optional query.** The client sends `SSH_MSG_USERAUTH_REQUEST` with method `"publickey"`, the boolean **FALSE**, the public-key algorithm name and the public key blob, with no signature. 2. **Server's answer.** The server replies `SSH_MSG_USERAUTH_PK_OK` (message number 60), echoing the algorithm and blob, if that key would be acceptable for the user; otherwise `SSH_MSG_USERAUTH_FAILURE` with the methods that can continue. 3. **Proof.** The client sends the request again with **TRUE**, the algorithm, the public key and a **signature**. 4. **Verdict.** The server checks the key and the signature, then sends `SSH_MSG_USERAUTH_SUCCESS`, or `SSH_MSG_USERAUTH_FAILURE`, with *partial success* set when this step worked but the server's policy requires another method as well. The query is an **optimisation, not a security step**. A private key may sit behind a passphrase or a hardware touch, and signing costs computation, so asking first spares both for keys the server would refuse. RFC 4252 lets the client skip straight to step 3. A PK_OK reply proves nothing about the client either: a public key is public, so anyone may ask whether it is accepted. ## What the signature covers The signature is made with the private key over these fields, in this order: | Field | Why it is there | |---|---| | session identifier | binds the proof to this one connection | | the `SSH_MSG_USERAUTH_REQUEST` message number | marks the data as a user-authentication request | | user name | the account being logged in to | | service name, normally `ssh-connection` | the service to start afterwards | | `"publickey"` and TRUE | the method, and that a signature follows | | public-key algorithm name | e.g. `ssh-ed25519` or `rsa-sha2-512` | | public key | the key the server is asked to trust | Every field an attacker might want to swap, the account above all, is inside the signed data, so changing it invalidates the signature. The algorithm is **not** tied to the key-exchange negotiation. RFC 4252 says any public-key algorithm may be offered here and a server that does not support it simply rejects the request. OpenSSH's sshd restricts what it accepts with `PubkeyAcceptedAlgorithms`; an RSA key signs with `rsa-sha2-256` or `rsa-sha2-512` (RFC 8332). ## What the server checks RFC 4252 requires two checks, and both must pass: - **The key is a valid authenticator for this user.** In OpenSSH that means the key is listed for the account in `authorized_keys`, subject to any options on its line, or it is a user certificate signed by a trusted CA whose principals fit the account. - **The signature verifies** under that public key, over the fields above, with this connection's session identifier. Neither check needs a secret on the server. That is why RFC 4251 section 9.4.5 says public key authentication makes no assumptions about security on the server, while section 9.4.4 notes it does assume the client host is not compromised. ## Why replay and relay both fail The exchange hash H is computed over both sides' version strings, both `SSH_MSG_KEXINIT` messages, the server's host key, the fresh key-exchange values from each side and the shared secret. Two connections therefore end up with different session identifiers, and that has three consequences: - **Replay fails.** A signature recorded from a log, a compromised server or a debugging capture does not verify on a new connection, whose session identifier differs. - **Relay through a man in the middle fails.** If a client accepts an impostor's host key, the impostor runs two connections, client to impostor and impostor to the real server. Each has its own key exchange and its own host key inside the hash, so each has its own session identifier. The client's signature covers the first; the real server checks it against the second, and it fails. A password, by contrast, the impostor simply decrypts and reuses. - **Re-keying does not log anyone out.** Because the identifier never changes after the first key exchange, a re-key during a long session leaves the completed authentication valid. How the key exchange picks its algorithms and verifies the host key is the subject of the material on key exchange and host keys. Holding keys in an agent and letting other hosts request signatures is the subject of the material on the SSH agent.
- Can an SSH client skip the SSH_MSG_USERAUTH_PK_OK query and sign straight away?Yes. RFC 4252 says the client MAY send the signed request directly. The query only saves work: a key may need a passphrase or a hardware touch before it can sign, and asking first avoids that for keys the server will refuse. If the signed request is refused, the server answers SSH_MSG_USERAUTH_FAILURE either way.
- Must the signature algorithm in SSH user authentication match what key exchange negotiated?No. RFC 4252 says the list is not constrained by what key exchange negotiated: the client names the algorithm in the request, for example rsa-sha2-512 for an RSA key under RFC 8332, and the server rejects it if unsupported. OpenSSH's sshd decides which it accepts with PubkeyAcceptedAlgorithms.
- Does a long SSH session have to re-authenticate after the transport re-keys?No. RFC 4253 section 7.2 fixes the session identifier as the exchange hash of the first key exchange and says it is not changed when keys are re-exchanged, and user authentication happens once per connection. The signature made at login therefore stays tied to the connection for its whole life.
- SSH_MSG_USERAUTH_PK_OK means the user is already authenticated.
- The client signs only the user name, so a signature works on any connection.
- In SSH-2 the server encrypts a challenge with the public key for the client to decrypt.
- The session identifier changes at every re-key, so the login must be repeated.
- The user-auth signature algorithm must be the one KEXINIT negotiated.