agentmux_srv/server/reactive/lan_signer.rs
1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The key a LAN peer's agent signs with, as this machine trusts it: asked of the
5//! peers, pinned only when a trusted install vouches for it, and moved only the same
6//! way (`lan_continuity`). Shared by the LAN jekt check (`verify_lan_signature`) and
7//! the remote-operation check (`remote_ops::receive`), so the two can't judge a
8//! sender's key differently.
9
10use base64::{engine::general_purpose::STANDARD as BASE64, Engine as _};
11
12use super::lan_continuity;
13use super::AppState;
14use crate::backend::lan::discovery::LanPubkeyLookup;
15
16/// What became of looking up `claimed`'s signing key.
17pub(crate) enum SignerKey {
18 /// The key the peers show and this machine's pin accepts.
19 Pinned(Vec<u8>),
20 /// No peer has a key on file for that name: nothing to check against.
21 Unknown,
22 /// First contact: the name has no pin and no trusted install vouched for the key it shows. Nothing is
23 /// pinned and the message is not proven; the person accepts the key once on the pin screen.
24 Unproven,
25 /// A key can't be had or can't be trusted (renamed, rate-limited, pin
26 /// unreadable, or a different key nobody vouched for): counts as a failure.
27 Refused,
28}
29
30/// Ask the LAN peers for `claimed`'s key and check it against this machine's pin.
31pub(crate) async fn resolve(state: &AppState, claimed: &str) -> SignerKey {
32 let observed_key = match state.lan_discovery.find_agent_lan_pubkey_asking_again(claimed, &state.http_client).await {
33 LanPubkeyLookup::Found(key) => key,
34 // Genuinely unknown sender: nothing to check against, not a red flag on its own.
35 LanPubkeyLookup::NotFound => return SignerKey::Unknown,
36 LanPubkeyLookup::Renamed(current) => {
37 tracing::warn!(agent_id = %claimed, current = %current, "LAN jekt signed as a name its agent was renamed from, which never signs: counted as failed");
38 return SignerKey::Refused;
39 }
40 // The lookup was skipped (rate-limited), not genuinely absent: a signature WAS
41 // presented, so "we didn't check" must not collapse into "nothing to check", or an
42 // attacker could exhaust the limiter to slip a forged identity through unverified.
43 LanPubkeyLookup::RateLimited => {
44 tracing::warn!(agent_id = %claimed, "LAN pubkey lookup was rate-limited while verifying a signature: treating as failed rather than passing it through");
45 return SignerKey::Refused;
46 }
47 };
48 // mDNS discovery is unauthenticated and a lookup answer is not signed, so an answer alone proves nothing.
49 // A name is pinned only when a trusted install vouches for its key (a certificate); a LATER, DIFFERENT
50 // key is a red flag that moves the pin only by the same proof, never a silent update.
51 let observed_key_b64 = BASE64.encode(&observed_key);
52 let Ok(pin) = state.mstore.lan_peer_pubkey_pin_since(claimed) else {
53 tracing::warn!(agent_id = %claimed, "LAN pubkey pin store unreadable: signature counted as failed");
54 return SignerKey::Refused; // fail closed
55 };
56 match pin {
57 Some((pinned_key_b64, _)) => {
58 if pinned_key_b64 != observed_key_b64 && !lan_continuity::on_mismatch(state, claimed, &observed_key).await {
59 return SignerKey::Refused;
60 }
61 }
62 None if !lan_continuity::on_first_contact(state, claimed, &observed_key).await => return SignerKey::Unproven,
63 None => {}
64 }
65 SignerKey::Pinned(observed_key)
66}