From ac66571a87cf36e769bb8308568808e3a0c2ce31 Mon Sep 17 00:00:00 2001 From: tv Date: Wed, 18 Nov 2026 11:17:06 +0000 Subject: continuum-party: a key the ssh-agent holds, given itself `--key` gives a public key itself, an `authorized_keys` line, as well as a file of them: of a file of several, the first signs, with a warning naming it, and another is given itself to sign with it. An agent that doesn't hold the key given signs nothing, and the client stops before it records or puts what it would have signed, saying which key it asked for. --- src/config/mod.rs | 4 +- src/federation/keys.rs | 3 +- src/federation/party.rs | 123 +++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 109 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/config/mod.rs b/src/config/mod.rs index 2ff0a45..f74f431 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -750,9 +750,9 @@ pub(crate) const PARTY_OPTIONS: &[OptionSpec] = &[ option( "key", Kind::Text, - "PATH", + "KEY", None, - "The party's key it signs with: an OpenSSH private key file, unencrypted, of an Ed25519 key; or a public key file (ssh-ed25519 or ecdsa-sha2-nistp256), signed with through the ssh-agent at SSH_AUTH_SOCK", + "The party's key it signs with: an OpenSSH private key file, unencrypted, of an Ed25519 key; or a public key (ssh-ed25519 or ecdsa-sha2-nistp256), signed with through the ssh-agent at SSH_AUTH_SOCK: itself, as an authorized_keys line, or a file of such lines, whose first signs", ), option( "record", diff --git a/src/federation/keys.rs b/src/federation/keys.rs index ed47b9b..22940ad 100644 --- a/src/federation/keys.rs +++ b/src/federation/keys.rs @@ -199,7 +199,8 @@ pub(crate) fn sign(key: &SigningKey, message: &[u8]) -> String { /// signature: what `ssh-keygen -Y sign -n continuum-federation` makes through the agent, /// unarmored, an ECDSA signature's `s` made low. pub(crate) fn sign_through(socket: &Path, key: &Key, message: &[u8]) -> Result { - let answer = super::agent::sign(socket, &key.blob(), &signed(message))?; + let answer = super::agent::sign(socket, &key.blob(), &signed(message)) + .map_err(|why| format!("{}: {why}", key.line()))?; from_agent(key, &answer, message) } diff --git a/src/federation/party.rs b/src/federation/party.rs index 4ba51ed..5d18eb8 100644 --- a/src/federation/party.rs +++ b/src/federation/party.rs @@ -1,10 +1,10 @@ //! `continuum-party`: a party's client of a federation (`docs/federation.md`). It signs its //! party's events with one of its keys (`--key`: an OpenSSH private key file, or a public -//! key the ssh-agent at `SSH_AUTH_SOCK` holds), puts them through a node's API (`--node`, -//! its user and password in the URL, or `--token`), and keeps its party's record -//! (`--record`, `client::Record`), by which it votes, written before a vote is sent. Its -//! options may be set in configuration files as the node's are (`--config`, -//! `config::PARTY_OPTIONS`): the same format, imports and all. +//! key the ssh-agent at `SSH_AUTH_SOCK` holds, given itself or in a file), puts them +//! through a node's API (`--node`, its user and password in the URL, or `--token`), and +//! keeps its party's record (`--record`, `client::Record`), by which it votes, written +//! before a vote is sent. Its options may be set in configuration files as the node's are +//! (`--config`, `config::PARTY_OPTIONS`): the same format, imports and all. //! //! * `init PARTY`: begins a record of `PARTY`, on the version current now; with `--root //! FILE`, from the file the store's swarm starts from, for a party that never voted in @@ -343,23 +343,70 @@ fn strings(matches: &ArgMatches, name: &str) -> Vec { .collect() } -/// The key `--key` names: a private key file's, or a public key's, held by the ssh-agent. +/// What `--key` gives: a private key, from an OpenSSH private key file; or a public key, +/// given itself (an `authorized_keys` line), or the first of a file of such lines, with +/// how many more the file holds. +enum Given { + Private(SigningKey), + Public(Key, usize), +} + +/// What `given`, `--key`'s value, gives (`Given`). +fn given_key(given: &str) -> Result { + let itself = match Key::parse(given) { + Ok(key) => return Ok(Given::Public(key, 0)), + Err(why) => why, + }; + let text = match std::fs::read_to_string(given) { + Ok(text) => text, + Err(error) if error.kind() == std::io::ErrorKind::NotFound => { + bail!("{given}: no file, nor a key ({itself})") + } + Err(error) => return Err(error).with_context(|| format!("Reading {given}")), + }; + if text.trim_start().starts_with("-----BEGIN") { + let key = client::signing_key(&text).map_err(|why| anyhow!("{given}: {why}"))?; + return Ok(Given::Private(key)); + } + let mut lines = text + .lines() + .map(str::trim) + .filter(|line| !line.is_empty() && !line.starts_with('#')); + let first = lines + .next() + .with_context(|| format!("{given} holds no key"))?; + let key = Key::parse(first).map_err(|why| anyhow!("{given}: {why}"))?; + Ok(Given::Public(key, lines.count())) +} + +/// The key `--key` gives (`given_key`): a private key, signing here; or a public key, +/// signing through the ssh-agent at `SSH_AUTH_SOCK`, which must hold it. Of a file of +/// several public keys, the first, with a warning: another is given as `--key` itself. fn signing_key(options: &Options) -> Result { - let file = options + let given = options .key .as_ref() .context("--key names the party's key")?; - let text = std::fs::read_to_string(file).with_context(|| format!("Reading {file}"))?; - if let Ok(key) = Key::parse(&text) { - let socket = std::env::var_os("SSH_AUTH_SOCK").with_context(|| { - format!( - "{file} is a public key, signed with through an ssh-agent: SSH_AUTH_SOCK names none" - ) - })?; - return Ok(Signing::Agent(key, PathBuf::from(socket))); - } - let key = client::signing_key(&text).map_err(|why| anyhow!("{file}: {why}"))?; - Ok(Signing::File(key)) + let key = match given_key(given)? { + Given::Private(key) => return Ok(Signing::File(key)), + Given::Public(key, 0) => key, + Given::Public(key, more) => { + eprintln!( + "Warning: {given} holds {} keys: signing with the first, {}; to sign with \ + another, give that key itself as --key", + more + 1, + key.line() + ); + key + } + }; + let socket = std::env::var_os("SSH_AUTH_SOCK").with_context(|| { + format!( + "{} signs through an ssh-agent: SSH_AUTH_SOCK names none", + key.line() + ) + })?; + Ok(Signing::Agent(key, PathBuf::from(socket))) } /// What signs: a key at hand, or a key the ssh-agent at a socket holds. @@ -699,6 +746,46 @@ mod tests { super::command().debug_assert(); } + /// `--key` gives a public key itself, or the first of a file's with how many more it + /// holds, or a private key file's key; a file of neither, or none, gives nothing. + #[test] + fn the_key_given() { + use super::{Given, given_key}; + use crate::federation::vectors::ECDSA_KEY; + let ed25519 = crate::federation::harness::key_line(1); + let dir = crate::files::scratch_dir("party-key"); + let file = |name: &str, text: &str| { + let path = dir.join(name); + std::fs::write(&path, text).unwrap(); + path.to_str().unwrap().to_owned() + }; + let public = |given: &str| match given_key(given).unwrap() { + Given::Public(key, more) => (key.line(), more), + Given::Private(_) => panic!("{given}: a private key"), + }; + assert_eq!(public(ECDSA_KEY), (ECDSA_KEY.to_owned(), 0)); + assert_eq!( + public(&format!("{ed25519} tv@laptop")), + (ed25519.clone(), 0) + ); + let one = file("phone.pub", &format!("{ECDSA_KEY} tv@phone\n")); + assert_eq!(public(&one), (ECDSA_KEY.to_owned(), 0)); + let two = file( + "agent.pub", + &format!("{ed25519} tv@laptop\n\n{ECDSA_KEY} tv@phone\n"), + ); + assert_eq!(public(&two), (ed25519, 1)); + assert!(given_key(&file("rsa.pub", "ssh-rsa AAAAB3NzaC1yc2E tv\n")).is_err()); + assert!(given_key(&file("empty.pub", "\n")).is_err()); + assert!(given_key(dir.join("missing").to_str().unwrap()).is_err()); + let private = file( + "tv.key", + "-----BEGIN OPENSSH PRIVATE KEY-----\n-----END OPENSSH PRIVATE KEY-----\n", + ); + let why = given_key(&private).err().expect("no key").to_string(); + assert!(why.contains("OpenSSH private key"), "{why}"); + } + /// Its options come from configuration files as the node's do, arguments winning. #[test] fn its_options_come_from_configuration_files_and_arguments() { -- cgit v1.3.1