How do OpenSSH ssh-add -h destination constraints limit where a forwarded agent key can be used, and what can they not prevent?
answer
- hop by hop, from origin
- hosts named by host key
- the agent learns the path
- the server key inside the signature
- a permitted hop still borrows
basics
~20 sSince OpenSSH 8.9, ssh-add -h names, by host key, where a key may authenticate and through which hops; the agent checks each request's bound session path and refuses others. Root on a permitted hop can still use it.
solid answer
~50 s`ssh-add -h bastion.example.com -h "bastion.example.com>[email protected]"` lets a key log in from the laptop to the bastion, and through the bastion to `db1` as `deploy`, and nowhere else. `ssh-add` resolves each name to host keys from `known_hosts` at load time. Every agent connection then starts with a `[email protected]` message in which `ssh` passes the server's host key, the session identifier and the server's signature over it, so the agent knows the path. Logins then use `[email protected]` where the server offers it (required on forwarded hops), which puts the target's host key inside the signed data; the agent refuses anything unlisted. What it cannot stop: root on a permitted hop using the key toward permitted destinations, re-forwarding by tools that skip binding, and signing that is not user authentication. Every hop also needs a recent enough OpenSSH.
go deeper
Recall that since OpenSSH 8.9, ssh-add -h can limit which hosts a key in the agent may log in to, directly or through a forwarded hop.
Explain the src>dst syntax, that hosts are identified by host keys from known_hosts, and why the constraint list cannot be edited later.
Walk through session binding and host-bound signatures, then state plainly what a compromised permitted hop can still do and what every hop must run.
Decide when constraints justify keeping forwarding at all, given version requirements across fleets, versus eliminating forwarding and the delegation it implies.
## The problem constraints address An ordinary agent key is all-or-nothing: if the agent is reachable, local or forwarded, the key signs for any destination. **Destination constraints**, added in **OpenSSH 8.9**, let the user say where a key may be used, hop by hop, and have the agent enforce it cryptographically. They are the answer for the cases where forwarding cannot be avoided. ## Writing constraints Constraints are given with `-h` when the key is added and cannot be changed afterwards; to alter them, remove the key and add it again. | Form | Meaning | |---|---| | `[user@]host` | from the origin (the machine running the agent) to `host`, optionally only as `user` | | `src>[user@]dst` | through `src`, using the forwarded agent there, to `dst` | ```bash ssh-add -h bastion.example.com \ -h "bastion.example.com>[email protected]" \ ~/.ssh/id_ed25519 ``` This key can log in to the bastion as any user, and from the bastion to `db1` only as `deploy`. A request to reach another host, or to reach `db1` directly from somewhere else, is refused. On a hop where a key is not permitted it is not even listed, and attempts to remove or modify a constrained key are refused anywhere but on the origin. ## How the agent knows the path 1. **Hosts are host keys.** `ssh-add` looks each name up in the `known_hosts` files (others can be added with `-H`) and sends the host keys to the agent; wildcards and host certificates signed by a known CA are supported. Names typed later do not matter, keys do. 2. **Each agent connection is bound.** The first message `ssh` sends on a new agent connection is the `[email protected]` extension: the server's host key, the **session identifier** from the initial key exchange, the server's signature over it, and a flag saying whether this binding is for forwarding or for authentication. The agent verifies the signature and appends the hop to that connection's path. Each `ssh` along a forwarding chain adds its own hop before passing the channel on. 3. **The signature names its destination.** Where the server offers it, and always on forwarded connections, the login uses the `[email protected]` method, identical to the standard publickey request except that it also carries the server's host key, so the data the agent signs names the destination. Servers advertise support through the `SSH_MSG_EXT_INFO` mechanism of RFC 8308. 4. **The agent decides.** Before signing, it parses the request, extracts user, session identifier and host key, and compares the whole path with the constraints. Every hop must be permitted. ## What still works for an attacker Constraints narrow delegation; they do not remove it. - **Root on a permitted hop** can still use the forwarded socket there to authenticate to any destination permitted *through that hop*. In the example, root on the bastion can still log in to `db1` as `deploy`. - **Re-forwarding**: someone with access to a remote `SSH_AUTH_SOCK` can relay it onward with a tool that does not take part in binding; they still reach only permitted destinations. - **Path padding**: a malicious hop can replay binding messages to make the path look longer, but cannot forge the final destination, because the signature covers the target's host key. - **Other signatures**: constraints apply only to SSH user authentication. A constrained key cannot be used for other signing, such as signatures made with `ssh-keygen`. OpenSSH's own advice is to read a constraint as permitting use *through* a host, a delegation only slightly more trustworthy than that host. ## Requirements and failure modes - **Every participant needs support**: the agent, `ssh-add`, each `ssh` in the chain, and, for forwarded hops, each target `sshd` must speak the host-bound method. The first hop from the origin is exempt. - **It fails closed**: an agent that cannot honour a constraint refuses to add the key, and an unbound authentication request on a forwarded connection is refused. - **`known_hosts` hygiene matters**: a host whose current host key is missing at `ssh-add` time cannot be permitted. - The client directive `PubkeyAuthentication` accepts `unbound` and `host-bound` to control the host-bound extension. - **OpenSSH 10.5** fixed a locked agent refusing `[email protected]` requests, which had let operations meant to stay local, including use of destination-restricted keys, be performed remotely.
- Why must the forwarded hop's target sshd be recent enough for a constrained key to work?On a forwarded connection the agent only signs a login whose signed data includes the target's host key, which requires the `[email protected]` method, advertised by the server through RFC 8308 extension negotiation. An older server cannot offer it, so the agent refuses. The first hop from the origin is exempt because its binding comes from the local client.
- What did OpenSSH 10.5 fix in this area?A locked agent refused `[email protected]` requests, so connections could not be identified as forwarded while it was locked, and operations intended to be local only, such as using destination-restricted keys or adding PKCS#11 tokens, could be performed remotely. Since 10.5 binding requests are processed while the agent is locked.
- Can you widen a constrained key's destinations without reloading it?No. Keys and their constraints are immutable once added to the agent. Remove the key with `ssh-add -d` and add it again with the new `-h` list.
- Constraints match host names as typed, so DNS decides where a key works.
- Root on a permitted hop cannot use a constrained key at all.
- Destination constraints work whatever OpenSSH versions run along the path.
- A destination-constrained key still works for ssh-keygen signatures.
- A constrained key's destinations can be edited while it stays loaded.
Assumes OpenSSH 10.5; destination constraints arrived in 8.9, and 10.5 fixed locked agents refusing the session-bind requests the constraints depend on.