diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/federation/keys.rs | 147 | ||||
| -rw-r--r-- | src/federation/mod.rs | 1 |
2 files changed, 148 insertions, 0 deletions
diff --git a/src/federation/keys.rs b/src/federation/keys.rs new file mode 100644 index 0000000..7df0bd9 --- /dev/null +++ b/src/federation/keys.rs @@ -0,0 +1,147 @@ +//! Keys, node IDs and signatures, each spelled one way (`docs/federation.md`, Events): +//! +//! * a key: `ssh-ed25519`, one space, and the key's base64, padded and canonical, with no +//! comment, its 32 bytes an Ed25519 public key; +//! * a node ID: as continuum spells it, `continuum:` and the 64 lower-case hex digits of +//! the node's Ed25519 public key; +//! * a signature: SSHSIG of version 1 over the event's bytes without `sig`, for the +//! namespace `continuum-federation`, hashed with `sha512`, its reserved field empty, its +//! key the event's, in base64, padded and canonical, unarmored; checked strictly. +//! +//! So nobody but its signer makes another event of what a party signed. + +use base64::{Engine, engine::general_purpose::STANDARD as BASE64}; +use ed25519_dalek::{SigningKey, VerifyingKey}; + +use crate::access::sshsig::{self, PublicKey}; +use crate::identity; + +/// What a party's signature is for. +pub(crate) const NAMESPACE: &str = "continuum-federation"; + +/// The key `text` spells, where it spells one as the federation does. +pub(crate) fn key(text: &str) -> Option<PublicKey> { + let key = PublicKey::parse(text).ok()?; + (key.line() == text && VerifyingKey::from_bytes(&key.0).is_ok()).then_some(key) +} + +/// Whether `text` is a node ID spelled as continuum spells one. +pub(crate) fn node_id(text: &str) -> bool { + identity::public_of(text).is_some_and(|key| VerifyingKey::from_bytes(&key).is_ok()) +} + +/// Whether `sig` is `key`'s signature of `message`, spelled as the federation spells one. +pub(crate) fn verifies(key: &PublicKey, sig: &str, message: &[u8]) -> bool { + let Ok(blob) = BASE64.decode(sig) else { + return false; + }; + if BASE64.encode(&blob) != sig || blob.len() < 64 { + return false; + } + // The blob reads back to itself: what signing with this key writes around its + // signature, the last 64 bytes. + if blob != written(key, &blob[blob.len() - 64..]) { + return false; + } + sshsig::verify_blob(&blob, NAMESPACE, message).is_ok_and(|signer| signer == *key) +} + +/// `message` signed with `key`, as the federation spells a signature: what +/// `ssh-keygen -Y sign -n continuum-federation` makes, unarmored. +pub(crate) fn sign(key: &SigningKey, message: &[u8]) -> String { + let blob = sshsig::dearmor(&sshsig::sign(key, NAMESPACE, message)) + .expect("a signature made here dearmors"); + BASE64.encode(blob) +} + +/// The SSHSIG blob of a signature by `key`, of version 1, for the federation's namespace, +/// its reserved field empty, hashed with `sha512`: as `ssh-keygen` writes it. +fn written(key: &PublicKey, signature: &[u8]) -> Vec<u8> { + let mut inner = Vec::new(); + string(&mut inner, b"ssh-ed25519"); + string(&mut inner, signature); + let mut blob = b"SSHSIG".to_vec(); + blob.extend_from_slice(&1u32.to_be_bytes()); + string(&mut blob, &key.blob()); + string(&mut blob, NAMESPACE.as_bytes()); + string(&mut blob, b""); + string(&mut blob, b"sha512"); + string(&mut blob, &inner); + blob +} + +fn string(out: &mut Vec<u8>, bytes: &[u8]) { + out.extend_from_slice(&(bytes.len() as u32).to_be_bytes()); + out.extend_from_slice(bytes); +} + +#[cfg(test)] +mod tests { + use super::*; + + fn signer(seed: u8) -> SigningKey { + SigningKey::from_bytes(&[seed; 32]) + } + + fn public(key: &SigningKey) -> PublicKey { + PublicKey(key.verifying_key().to_bytes()) + } + + #[test] + fn a_key_is_spelled_one_way() { + let line = public(&signer(1)).line(); + assert!(key(&line).is_some()); + assert!(key(&format!("{line} comment")).is_none()); + assert!(key(&line.replacen(' ', " ", 1)).is_none()); + assert!(key(&format!(" {line}")).is_none()); + let unpadded = line.trim_end_matches('='); + if unpadded != line { + assert!(key(unpadded).is_none()); + } + } + + #[test] + fn a_node_id_is_spelled_one_way() { + let id = identity::node_id_of(&signer(2).verifying_key().to_bytes()); + assert!(node_id(&id)); + assert!(!node_id(&id.to_uppercase())); + assert!(!node_id(&id.replace("continuum:", "continuum:0"))); + assert!(!node_id(&id[..id.len() - 1])); + } + + #[test] + fn a_signature_is_its_signers_and_reads_back_to_itself() { + let key = signer(3); + let sig = sign(&key, b"an event"); + assert!(verifies(&public(&key), &sig, b"an event")); + assert!(!verifies(&public(&key), &sig, b"another event")); + assert!(!verifies(&public(&signer(4)), &sig, b"an event")); + // Armored, or base64 spelled otherwise: none. + let armored = sshsig::sign(&key, NAMESPACE, b"an event"); + assert!(!verifies(&public(&key), &armored, b"an event")); + assert!(!verifies(&public(&key), &format!("{sig}\n"), b"an event")); + // For another namespace: none. + let elsewhere = BASE64.encode( + sshsig::dearmor(&sshsig::sign(&key, "another", b"an event")).expect("dearmors"), + ); + assert!(!verifies(&public(&key), &elsewhere, b"an event")); + } + + #[test] + fn a_signature_with_a_reserved_field_is_none() { + let key = signer(5); + let blob = BASE64.decode(sign(&key, b"an event")).expect("base64"); + // The reserved field, empty, follows the namespace: give it a byte. The signature + // no longer matches, but the blob is refused before that is asked. + let at = 6 + 4 + 4 + public(&key).blob().len() + 4 + NAMESPACE.len(); + let mut changed = blob[..at].to_vec(); + changed.extend_from_slice(&1u32.to_be_bytes()); + changed.push(b'x'); + changed.extend_from_slice(&blob[at + 4..]); + assert!(!verifies( + &public(&key), + &BASE64.encode(changed), + b"an event" + )); + } +} diff --git a/src/federation/mod.rs b/src/federation/mod.rs index 6485c8a..9c49e66 100644 --- a/src/federation/mod.rs +++ b/src/federation/mod.rs @@ -6,4 +6,5 @@ #![allow(dead_code)] pub(crate) mod jcs; +pub(crate) mod keys; pub(crate) mod store; |
