Skip to main content

agentmux_srv_core/muxbus/
wan_key_revocation.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! A signed notice that one agent's WAN key was retired.
5//!
6//! An install replaces an agent's key when the old one should no longer be
7//! trusted. The install signs a
8//! [`WanKeyRevocation`] naming the key (instance, agent, channel,
9//! fingerprint) with its instance key, the same key that certified the agent
10//! key in the first place, so only that install can retire it and an agent
11//! never can. The directory serves the notice, and a receiver that holds it
12//! refuses any message signed by that fingerprint.
13//!
14//! The byte format must match the cloud's (`wan-keys.ts` there); the fixed
15//! vector in the tests below is the contract between the two.
16
17use base64::{engine::general_purpose::STANDARD as BASE64, Engine as _};
18
19use agentmux_common::jekt_sign::{decode_public_key_b64, ed25519_sign_b64, ed25519_verify_b64, wan_instance_id, FIELD_SEP};
20
21/// Domain separator for a key revocation. Distinct from the certificate's and
22/// the instance revocation's, so none of their signatures can stand in for one.
23const WAN_KEY_REVOKE_DOMAIN: &str = "amx-wan-agent-key-revoke-v1";
24
25fn wan_key_revoke_material(instance_id: &str, agent_id: &str, channel: &str, key_fp: &str, revoked_at: i64) -> String {
26    format!(
27        "{WAN_KEY_REVOKE_DOMAIN}{FIELD_SEP}{instance_id}{FIELD_SEP}{agent_id}{FIELD_SEP}{channel}{FIELD_SEP}{key_fp}{FIELD_SEP}{revoked_at}"
28    )
29}
30
31/// One agent key retired by the instance that minted it. Every field is
32/// untrusted until [`WanKeyRevocation::verify`] passes.
33#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
34pub struct WanKeyRevocation {
35    pub instance_id: String,
36    pub instance_pubkey: String,
37    pub agent_id: String,
38    pub channel: String,
39    pub key_fp: String,
40    pub revoked_at: i64,
41    pub sig: String,
42}
43
44impl WanKeyRevocation {
45    /// Retire the key with fingerprint `key_fp`, signing with the instance
46    /// key. `None` for a malformed instance private key; never panics.
47    pub fn sign(instance_private_key: &[u8], agent_id: &str, channel: &str, key_fp: &str, revoked_at: i64) -> Option<Self> {
48        let seed: [u8; 32] = instance_private_key.try_into().ok()?;
49        let instance_public_key = ed25519_dalek::SigningKey::from_bytes(&seed).verifying_key().to_bytes();
50        let instance_id = wan_instance_id(&instance_public_key);
51        let agent_id = agent_id.to_ascii_lowercase();
52        let material = wan_key_revoke_material(&instance_id, &agent_id, channel, key_fp, revoked_at);
53        Some(Self {
54            instance_id,
55            instance_pubkey: BASE64.encode(instance_public_key),
56            agent_id,
57            channel: channel.to_string(),
58            key_fp: key_fp.to_string(),
59            revoked_at,
60            sig: ed25519_sign_b64(&seed, &material)?,
61        })
62    }
63
64    /// The instance id is the hash of the instance key, the agent id is
65    /// lowercase, and the instance key signed this key's retirement.
66    pub fn verify(&self) -> bool {
67        let Some(instance_public_key) = decode_public_key_b64(&self.instance_pubkey) else { return false };
68        wan_instance_id(&instance_public_key) == self.instance_id
69            && self.agent_id == self.agent_id.to_ascii_lowercase()
70            && ed25519_verify_b64(
71                &instance_public_key,
72                &wan_key_revoke_material(&self.instance_id, &self.agent_id, &self.channel, &self.key_fp, self.revoked_at),
73                &self.sig,
74            )
75    }
76
77    /// Whether this names exactly the key a message or record carries
78    /// (the agent compared case-insensitively, as every W3-S identifier is).
79    pub fn names(&self, instance_id: &str, agent_id: &str, channel: &str, key_fp: &str) -> bool {
80        self.instance_id == instance_id.to_ascii_lowercase()
81            && self.agent_id == agent_id.to_ascii_lowercase()
82            && self.channel == channel
83            && self.key_fp == key_fp
84    }
85}
86
87#[cfg(test)]
88mod tests {
89    use super::*;
90
91    // Cross-language vector, the same as `V_*` in the cloud's wan-keys.test.ts:
92    // instance seed [1; 32], and the fingerprint of agent seed [2; 32].
93    const V_INSTANCE_PUBKEY: &str = "iojj3XQJ8ZX9UtstPLpdcspnCb8dlBIb83SIAbQPb1w=";
94    const V_INSTANCE_ID: &str = "gr2q7gf5lh6pzfdnurnkvputhm";
95    const V_KEY_FP: &str = "ajgD1fBZkCocba-8m6RykhL3yqwIY0zDrnaydSnwOCc";
96    const V_KEY_REVOKE_SIG: &str =
97        "8OewRgBH2AN0tNh6jVIkB5qX+T0BWkUCezup79Vyaff8vw/VXo9oLdryeNs3a5N/YwKumN89MGpyQXbPuiv2BA==";
98
99    fn vector() -> WanKeyRevocation {
100        WanKeyRevocation {
101            instance_id: V_INSTANCE_ID.into(),
102            instance_pubkey: V_INSTANCE_PUBKEY.into(),
103            agent_id: "camper".into(),
104            channel: "stable".into(),
105            key_fp: V_KEY_FP.into(),
106            revoked_at: 1_790_000_600,
107            sig: V_KEY_REVOKE_SIG.into(),
108        }
109    }
110
111    #[test]
112    fn signing_gives_the_signature_the_cloud_checks() {
113        let signed = WanKeyRevocation::sign(&[1; 32], "Camper", "stable", V_KEY_FP, 1_790_000_600).unwrap();
114        assert_eq!(signed, vector());
115        assert!(signed.verify());
116    }
117
118    #[test]
119    fn the_cloud_signed_vector_verifies() {
120        assert!(vector().verify());
121    }
122
123    #[test]
124    fn anything_changed_after_signing_fails() {
125        let changes: [(&str, fn(&mut WanKeyRevocation)); 8] = [
126            ("agent", |r| r.agent_id = "lark".into()),
127            ("uppercased agent", |r| r.agent_id = "Camper".into()),
128            ("channel", |r| r.channel = "beta".into()),
129            ("fingerprint", |r| r.key_fp = "x".repeat(43)),
130            ("time", |r| r.revoked_at += 1),
131            ("signature", |r| r.sig = V_KEY_REVOKE_SIG.replace('8', "9")),
132            ("instance key", |r| r.instance_pubkey = BASE64.encode([9u8; 32])),
133            ("instance id", |r| r.instance_id = "a".repeat(26)),
134        ];
135        for (what, change) in changes {
136            let mut r = vector();
137            change(&mut r);
138            assert!(!r.verify(), "a changed {what} must not verify");
139        }
140    }
141
142    #[test]
143    fn another_instances_key_cannot_sign_for_this_one() {
144        let mut forged = WanKeyRevocation::sign(&[9; 32], "camper", "stable", V_KEY_FP, 1_790_000_600).unwrap();
145        forged.instance_id = V_INSTANCE_ID.into();
146        assert!(!forged.verify(), "the id is not the hash of the signing key");
147        forged.instance_pubkey = V_INSTANCE_PUBKEY.into();
148        assert!(!forged.verify(), "the key given is not the one that signed");
149    }
150
151    #[test]
152    fn a_certificate_or_an_instance_revocation_does_not_stand_in() {
153        let cert_sig = "ighbn1y/kyisIolSHwNyRtBqC2u95zQEBtGc3wxwksBUBHrDoUvXmz8yEXY20TFzOBC6IDEsG51WzTNJCrgNAA==";
154        assert!(!WanKeyRevocation { sig: cert_sig.into(), ..vector() }.verify());
155    }
156
157    #[test]
158    fn a_malformed_private_key_signs_nothing() {
159        assert!(WanKeyRevocation::sign(&[1; 31], "camper", "stable", V_KEY_FP, 1).is_none());
160    }
161
162    #[test]
163    fn it_names_exactly_one_key() {
164        let r = vector();
165        assert!(r.names(V_INSTANCE_ID, "Camper", "stable", V_KEY_FP));
166        assert!(!r.names(V_INSTANCE_ID, "camper", "beta", V_KEY_FP));
167        assert!(!r.names(V_INSTANCE_ID, "lark", "stable", V_KEY_FP));
168        assert!(!r.names("a".repeat(26).as_str(), "camper", "stable", V_KEY_FP));
169        assert!(!r.names(V_INSTANCE_ID, "camper", "stable", &"x".repeat(43)));
170    }
171}