diff options
Diffstat (limited to 'src/federation/keys.rs')
| -rw-r--r-- | src/federation/keys.rs | 136 |
1 files changed, 106 insertions, 30 deletions
diff --git a/src/federation/keys.rs b/src/federation/keys.rs index aff7792..cf0f366 100644 --- a/src/federation/keys.rs +++ b/src/federation/keys.rs @@ -135,6 +135,12 @@ pub(crate) fn node_of(key: &Key) -> Option<String> { /// Whether `sig` is `key`'s signature of `message`, spelled as the federation spells one. pub(crate) fn verifies(key: &Key, sig: &str, message: &[u8]) -> bool { + verifies_in(NAMESPACE, key, sig, message) +} + +/// Whether `sig` is `key`'s signature of `message` for `namespace`, spelled as the +/// federation spells one but for its namespace: a party's login (`access::challenge`). +pub(crate) fn verifies_in(namespace: &str, key: &Key, sig: &str, message: &[u8]) -> bool { let Ok(blob) = BASE64.decode(sig) else { return false; }; @@ -143,23 +149,23 @@ pub(crate) fn verifies(key: &Key, sig: &str, message: &[u8]) -> bool { } // The blob reads back to itself: what signing with this key writes around its // signature, all that follows what it writes around none. - let Some(signature) = blob.get(written(key, &[]).len()..) else { + let Some(signature) = blob.get(written(namespace, key, &[]).len()..) else { return false; }; - if blob != written(key, signature) { + if blob != written(namespace, key, signature) { return false; } match key { Key::Ed25519(public) => { - sshsig::verify_blob(&blob, NAMESPACE, message).is_ok_and(|signer| signer == *public) + sshsig::verify_blob(&blob, namespace, message).is_ok_and(|signer| signer == *public) } - Key::P256(point) => p256_verifies(point, signature, message), + Key::P256(point) => p256_verifies(namespace, point, signature, message), } } /// Whether `signature`, `r` and `s` as SSH's mpints, is the P-256 key `point`'s of -/// `message`, spelled one way: each mpint its shortest, `s` low. -fn p256_verifies(point: &[u8; 65], signature: &[u8], message: &[u8]) -> bool { +/// `message` for `namespace`, spelled one way: each mpint its shortest, `s` low. +fn p256_verifies(namespace: &str, point: &[u8; 65], signature: &[u8], message: &[u8]) -> bool { let Some(parsed) = ecdsa_signature(signature) else { return false; }; @@ -167,7 +173,7 @@ fn p256_verifies(point: &[u8; 65], signature: &[u8], message: &[u8]) -> bool { return false; } p256::ecdsa::VerifyingKey::from_sec1_bytes(point) - .is_ok_and(|key| key.verify(&signed(message), &parsed).is_ok()) + .is_ok_and(|key| key.verify(&signed(namespace, message), &parsed).is_ok()) } /// An ECDSA signature from `r` and `s`, SSH's mpints, where each is an integer from 1 to @@ -207,10 +213,35 @@ fn mpints(signature: &p256::ecdsa::Signature) -> Vec<u8> { out } +/// Of `keys`, as the federation spells them, the one whose signature of `message` for +/// `namespace` `signature` is: spelled as the federation spells one but for its namespace, +/// or armored, as `ssh-keygen -Y sign` writes it. None where it is none of theirs. +pub(crate) fn signer<'a>( + keys: &'a [String], + namespace: &str, + signature: &str, + message: &[u8], +) -> Option<&'a str> { + let signature = signature.trim(); + let sig = if signature.starts_with("-----BEGIN") { + BASE64.encode(sshsig::dearmor(signature).ok()?) + } else { + signature.to_owned() + }; + keys.iter() + .find(|line| key(line).is_some_and(|key| verifies_in(namespace, &key, &sig, message))) + .map(String::as_str) +} + /// `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)) + sign_in(NAMESPACE, key, message) +} + +/// As `sign`, for `namespace`. +pub(crate) fn sign_in(namespace: &str, key: &SigningKey, message: &[u8]) -> String { + let blob = sshsig::dearmor(&sshsig::sign(key, namespace, message)) .expect("a signature made here dearmors"); BASE64.encode(blob) } @@ -219,14 +250,24 @@ 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<String, String> { - let answer = super::agent::sign(socket, &key.blob(), &signed(message)) + sign_through_in(NAMESPACE, socket, key, message) +} + +/// As `sign_through`, for `namespace`. +pub(crate) fn sign_through_in( + namespace: &str, + socket: &Path, + key: &Key, + message: &[u8], +) -> Result<String, String> { + let answer = super::agent::sign(socket, &key.blob(), &signed(namespace, message)) .map_err(|why| format!("{}: {why}", key.line()))?; - from_agent(key, &answer, message) + from_agent(namespace, key, &answer, message) } -/// The federation's signature of `message` by `key` from `answer`, the signature the agent -/// made of what SSHSIG signs, as SSH encodes one. -fn from_agent(key: &Key, answer: &[u8], message: &[u8]) -> Result<String, String> { +/// The signature of `message` for `namespace` by `key` from `answer`, the signature the +/// agent made of what SSHSIG signs, as SSH encodes one. +fn from_agent(namespace: &str, key: &Key, answer: &[u8], message: &[u8]) -> Result<String, String> { let mut reader = Reader(answer); if reader.string()? != key.kind().as_bytes() { return Err(format!("the agent's signature isn't an {} one", key.kind())); @@ -241,34 +282,35 @@ fn from_agent(key: &Key, answer: &[u8], message: &[u8]) -> Result<String, String .normalize_s(), ), }; - let sig = BASE64.encode(written(key, &signature)); - if !verifies(key, &sig, message) { + let sig = BASE64.encode(written(namespace, key, &signature)); + if !verifies_in(namespace, key, &sig, message) { return Err("the agent's signature doesn't check".to_owned()); } Ok(sig) } -/// What a signature of `message` is made over: SSHSIG's signed data, hashed with `sha512`. -fn signed(message: &[u8]) -> Vec<u8> { +/// What a signature of `message` for `namespace` is made over: SSHSIG's signed data, hashed +/// with `sha512`. +fn signed(namespace: &str, message: &[u8]) -> Vec<u8> { sshsig::signed_data( - NAMESPACE.as_bytes(), + namespace.as_bytes(), b"", b"sha512", &Sha512::digest(message), ) } -/// 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 around -/// `signature`, the key type's own. -fn written(key: &Key, signature: &[u8]) -> Vec<u8> { +/// The SSHSIG blob of a signature by `key`, of version 1, for `namespace`, its reserved +/// field empty, hashed with `sha512`: as `ssh-keygen` writes it around `signature`, the +/// key type's own. +fn written(namespace: &str, key: &Key, signature: &[u8]) -> Vec<u8> { let mut inner = Vec::new(); string(&mut inner, key.kind().as_bytes()); 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, namespace.as_bytes()); string(&mut blob, b""); string(&mut blob, b"sha512"); string(&mut blob, &inner); @@ -281,7 +323,7 @@ fn written(key: &Key, signature: &[u8]) -> Vec<u8> { #[cfg(test)] pub(crate) fn respelled(key: &Key, sig: &str) -> [String; 2] { let blob = BASE64.decode(sig).expect("base64"); - let rs = &blob[written(key, &[]).len()..]; + let rs = &blob[written(NAMESPACE, key, &[]).len()..]; let (r, s) = ecdsa_signature(rs).expect("r and s").split_scalars(); let high = p256::ecdsa::Signature::from_scalars(r, -s).expect("a signature"); let mut reader = Reader(rs); @@ -289,7 +331,7 @@ pub(crate) fn respelled(key: &Key, sig: &str) -> [String; 2] { let mut longer = Vec::new(); string(&mut longer, &[&[0], r].concat()); string(&mut longer, s); - [mpints(&high), longer].map(|numbers| BASE64.encode(written(key, &numbers))) + [mpints(&high), longer].map(|numbers| BASE64.encode(written(NAMESPACE, key, &numbers))) } #[cfg(test)] @@ -368,6 +410,36 @@ mod tests { assert!(!verifies(&public(&key), &elsewhere, b"an event")); } + /// Of a party's keys, the one that signed a login, for the login's namespace: spelled + /// as the federation spells a signature, or armored as `ssh-keygen` writes it; none + /// for another namespace, an event's included, or by another key. + #[test] + fn the_key_a_login_is_signed_with() { + let (tv, laptop) = (signer(6), signer(7)); + let keys = [public(&tv).line(), public(&laptop).line()]; + let login = "continuum-login"; + let sig = sign_in(login, &laptop, b"cc1.challenge"); + assert_eq!( + super::signer(&keys, login, &sig, b"cc1.challenge"), + Some(keys[1].as_str()) + ); + let armored = sshsig::sign(&laptop, login, b"cc1.challenge"); + assert_eq!( + super::signer(&keys, login, &armored, b"cc1.challenge"), + Some(keys[1].as_str()) + ); + assert_eq!(super::signer(&keys, login, &sig, b"cc1.other"), None); + let event = sign(&laptop, b"cc1.challenge"); + assert_eq!(super::signer(&keys, login, &event, b"cc1.challenge"), None); + let stranger = sign_in(login, &signer(8), b"cc1.challenge"); + assert_eq!( + super::signer(&keys, login, &stranger, b"cc1.challenge"), + None + ); + assert!(verifies_in(login, &public(&laptop), &sig, b"cc1.challenge")); + assert!(!verifies(&public(&laptop), &sig, b"cc1.challenge")); + } + #[test] fn a_signature_with_a_reserved_field_is_none() { let key = signer(5); @@ -399,7 +471,7 @@ mod tests { /// The `r` and `s` of `sig`, a signature by `key` as the federation spells one. fn rs<'a>(key: &Key, sig: &'a [u8]) -> &'a [u8] { - &sig[written(key, &[]).len()..] + &sig[written(NAMESPACE, key, &[]).len()..] } #[test] @@ -410,7 +482,7 @@ mod tests { let blob = BASE64.decode(&sig).expect("base64"); let ours = ecdsa_signature(rs(&key, &blob)).expect("r and s"); let refused = |rs: &[u8]| { - let other = BASE64.encode(written(&key, rs)); + let other = BASE64.encode(written(NAMESPACE, &key, rs)); assert!(!verifies(&key, &other, &message)); assert!(Event::read(ECDSA_WRITE.replace(&sig, &other).as_bytes()).is_none()); }; @@ -421,7 +493,11 @@ mod tests { unreachable!("an ECDSA key") }; let verifying = p256::ecdsa::VerifyingKey::from_sec1_bytes(&point).expect("a point"); - assert!(verifying.verify(&signed(&message), &twin).is_ok()); + assert!( + verifying + .verify(&signed(NAMESPACE, &message), &twin) + .is_ok() + ); assert_ne!(twin, ours); assert_eq!(twin.normalize_s(), ours); refused(&mpints(&twin)); @@ -532,7 +608,7 @@ mod tests { message.as_bytes(), ); if let Key::P256(_) = key { - let data = signed(message.as_bytes()); + let data = signed(NAMESPACE, message.as_bytes()); let high = (0..64) .map(|_| { super::super::agent::sign(&socket, &key.blob(), &data).expect("signed") @@ -545,7 +621,7 @@ mod tests { }) .expect("a high `s` among 64 signatures"); checked( - &from_agent(&key, &high, message.as_bytes()).expect("made low"), + &from_agent(NAMESPACE, &key, &high, message.as_bytes()).expect("made low"), message.as_bytes(), ); } |
