diff options
| author | slopulus <slop@lassul.us> | 2026-11-17 20:01:42 +0100 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-17 21:09:38 +0100 |
| commit | bcda4d67483239691b045c5b978047f017f3b9e3 (patch) | |
| tree | f6a36de02d707969c1c78c03ce589cef171fbad3 /src/federation/agent.rs | |
| parent | a1e398621eec9472ad67f1eafeba9ff23b012c2f (diff) | |
continuum-party: signing through an ssh-agent
A key held in hardware is never a file: only an ssh-agent signs with it. Given a public
key as `--key` (a `.pub` file, `ssh-ed25519` or `ecdsa-sha2-nistp256`), the client asks
the agent at `SSH_AUTH_SOCK` to sign what SSHSIG signs (a sign request with the key's
blob, no flags) and writes the blob around the agent's signature as `ssh-keygen -Y sign`
does. An ECDSA signature the agent gives with a high `s` is made low, the other of its
two spellings, which the federation takes. What it makes is checked before it is used.
A private key file signs as it did.
A test starts `ssh-agent`, adds an ECDSA and an Ed25519 key to it and deletes their
files, signs through it, and has both continuum and `ssh-keygen -Y verify` check each
signature, one of an agent's high-S answers among them. Without OpenSSH it asks nothing.
Diffstat (limited to 'src/federation/agent.rs')
| -rw-r--r-- | src/federation/agent.rs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/federation/agent.rs b/src/federation/agent.rs new file mode 100644 index 0000000..a41cf1a --- /dev/null +++ b/src/federation/agent.rs @@ -0,0 +1,45 @@ +//! What a party's client asks of an ssh-agent (draft-ietf-sshm-ssh-agent, as OpenSSH's +//! `ssh-agent` speaks it): a signature by a key it holds, so the key itself needn't be a +//! file, or anywhere but the agent. + +use std::io::{Read, Write}; +use std::os::unix::net::UnixStream; +use std::path::Path; + +use crate::access::sshsig::{Reader, string}; + +const SIGN_REQUEST: u8 = 13; +const SIGN_RESPONSE: u8 = 14; +/// The longest answer read: a signature is far shorter. +const LONGEST: usize = 64 * 1024; + +/// The signature of `data` by the key whose SSH encoding is `key`, from the agent at +/// `socket`: its type and its bytes, each a string, as SSH encodes a signature. +pub(crate) fn sign(socket: &Path, key: &[u8], data: &[u8]) -> Result<Vec<u8>, String> { + let failed = |error: std::io::Error| format!("the ssh-agent at {}: {error}", socket.display()); + let mut request = vec![SIGN_REQUEST]; + string(&mut request, key); + string(&mut request, data); + request.extend_from_slice(&0u32.to_be_bytes()); + let mut message = Vec::new(); + string(&mut message, &request); + let mut stream = UnixStream::connect(socket).map_err(failed)?; + stream.write_all(&message).map_err(failed)?; + let mut length = [0; 4]; + stream.read_exact(&mut length).map_err(failed)?; + let length = u32::from_be_bytes(length) as usize; + if length > LONGEST { + return Err("the ssh-agent's answer is longer than a signature".to_owned()); + } + let mut answer = vec![0; length]; + stream.read_exact(&mut answer).map_err(failed)?; + let Some((&SIGN_RESPONSE, rest)) = answer.split_first() else { + return Err( + "the ssh-agent signs nothing: it holds no such key, or was told not to".to_owned(), + ); + }; + let mut reader = Reader(rest); + let signature = reader.string()?; + reader.end()?; + Ok(signature.to_vec()) +} |
