agentmux_common/jekt_sign/reagent.rs
1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3//
4//! WAN-tier service senders' pinned Ed25519 keys, and the two checks a receiver makes on a
5//! `reagent_sig`: that it verifies against the pinned key, and that it was made by the trusted
6//! production key (the only one that authorises a verified sender).
7//!
8//! The keys are pinned here rather than fetched: every AgentMux instance verifies these senders, so
9//! the private half stays in the one signing service and only public keys ship. A development build
10//! also holds the development key, so local traffic and messages from before the rotation still
11//! verify; a release build does not (task #49102) and logs why when it refuses a signature.
12
13use super::{signed_material, BASE64};
14use base64::Engine as _;
15use ed25519_dalek::{Signature, Verifier, VerifyingKey};
16
17/// The `key_id` of the development signing key [`reagent_public_key`] holds in development and
18/// test builds only. A release build holds no key under this id: [`verify_reagent_jekt`] refuses a
19/// signature under it, and says why in the log, so a dropped WAN jekt reads as a deliberate
20/// refusal rather than as an unknown key.
21const DEV_SIGNING_KEY_ID: &str = "reagent-v1-dev";
22
23/// Pinned Ed25519 public keys for AgentMux's own WAN-tier service senders.
24/// Keyed by `key_id` (carried alongside the signature in the wire format)
25/// so a future key rotation can add a new entry without invalidating
26/// verification of messages already in flight when signed under the old
27/// key. The matching private key is never present in this repo — it is
28/// the signing key held in AgentMux's private secret store, used only by
29/// the signing service.
30///
31/// `reagent-v1-dev` was a placeholder minted during initial implementation
32/// (its private half was generated in a local shell for wiring/testing
33/// purposes and was treated as already-exposed from the moment it was
34/// generated) — kept registered here, but the signing service no longer
35/// signs under it, so no genuine production traffic uses it. Left in place only
36/// so a message that happened to be signed under it before the rotation still
37/// verifies in a development or test build, matching this map's whole "add,
38/// don't replace" design. A release build does not compile the key in at all
39/// (the arm below), so it refuses such a signature (task #49102).
40///
41/// `reagent-v1` (2026-08-14) is the real production key: generated so that
42/// the private key was never printed, logged, or otherwise appeared in any
43/// transcript — the private half is the signing key held in AgentMux's
44/// private secret store, consistent with the exposure lesson
45/// `reagent-v1-dev` was kept around specifically to document.
46fn reagent_public_key(key_id: &str) -> Option<[u8; 32]> {
47 match key_id {
48 // Development and test builds only: traffic signed before the rotation to the production
49 // key still verifies locally, and no released build accepts a signature under it (task #49102).
50 #[cfg(debug_assertions)]
51 DEV_SIGNING_KEY_ID => Some([
52 104, 98, 241, 120, 99, 50, 230, 185, 228, 117, 241, 110, 130, 85, 252, 75, 141, 152,
53 224, 92, 125, 154, 123, 40, 96, 149, 214, 172, 94, 120, 116, 200,
54 ]),
55 "reagent-v1" => Some([
56 185, 72, 99, 0, 119, 36, 15, 50, 117, 0, 134, 93, 66, 39, 65, 21, 64, 229, 3, 157,
57 238, 188, 79, 40, 74, 42, 99, 108, 28, 101, 243, 205,
58 ]),
59 _ => None,
60 }
61}
62
63/// The one `key_id` whose matching private key is believed secret today.
64/// `reagent-v1-dev` is registered in `reagent_public_key` for development
65/// and test builds only (so `verify_reagent_jekt` checks its signatures
66/// there), but its private half is documented above as exposed since it was
67/// generated, so a signature under it proves nothing about the sender.
68/// Receivers record a reagent signature as verified only through
69/// [`verify_trusted_reagent_jekt`], which requires this key; any other key
70/// yields `reagent_verified = Some(false)` — an active verification failure,
71/// forced sensitive.
72pub fn is_reagent_trusted_signing_key(key_id: &str) -> bool {
73 key_id == "reagent-v1"
74}
75
76/// Verify a WAN-tier jekt's claimed `reagent_sig` against the pinned public
77/// key for `key_id`. Returns `false` (never panics) for an unknown
78/// `key_id`, malformed base64, a malformed/wrong-length signature, or a
79/// signature that simply doesn't verify — callers should treat all of
80/// these identically: "not verified," rendering `SIG=invalid` rather than
81/// `SIG=verified` in the marker (see `wrap_jekt_message`'s doc comment).
82///
83/// This function alone does NOT make a verified sender: a message signed
84/// under the production key verifies here, and receivers must still use
85/// [`verify_trusted_reagent_jekt`]. A development build also verifies a
86/// message signed under the development key; a release build does not hold
87/// that key, refuses its signature and logs why (task #49102).
88pub fn verify_reagent_jekt(
89 key_id: &str,
90 msgid: &str,
91 source_agent: &str,
92 target_agent: &str,
93 ts_secs: i64,
94 message: &str,
95 sig_b64: &str,
96) -> bool {
97 let Some(pubkey_bytes) = reagent_public_key(key_id) else {
98 // A release build does not hold the development key: refusing it is deliberate, and the
99 // reason goes to the log so a jekt dropped here is not mistaken for an unknown key.
100 #[cfg(not(debug_assertions))]
101 if key_id == DEV_SIGNING_KEY_ID {
102 tracing::warn!(
103 key_id,
104 "refusing a WAN jekt signed with the development signing key: a release build does not accept it"
105 );
106 }
107 return false;
108 };
109 let Ok(verifying_key) = VerifyingKey::from_bytes(&pubkey_bytes) else { return false };
110 let Ok(sig_bytes) = BASE64.decode(sig_b64) else { return false };
111 let Ok(sig_arr) = <[u8; 64]>::try_from(sig_bytes.as_slice()) else { return false };
112 let signature = Signature::from_bytes(&sig_arr);
113 let material = signed_material(msgid, source_agent, target_agent, ts_secs, message);
114 verifying_key.verify(material.as_bytes(), &signature).is_ok()
115}
116
117/// The verification result a receiver records as `reagent_verified`: the
118/// signature checks out AND its key is the trusted production key. A
119/// signature under any other registered key is not a verified sender.
120pub fn verify_trusted_reagent_jekt(
121 key_id: &str,
122 msgid: &str,
123 source_agent: &str,
124 target_agent: &str,
125 ts_secs: i64,
126 message: &str,
127 sig_b64: &str,
128) -> bool {
129 is_reagent_trusted_signing_key(key_id)
130 && verify_reagent_jekt(key_id, msgid, source_agent, target_agent, ts_secs, message, sig_b64)
131}
132
133#[cfg(test)]
134mod tests {
135 use super::*;
136
137 // ---- verify_reagent_jekt (Ed25519) ----
138 //
139 // Fixture signature produced offline against the "reagent-v1-dev" pinned
140 // public key's matching private key, over signed_material("msg-1",
141 // "github-consumer", "agentx", 1000, "hello") — i.e. the exact material
142 // `signed_material()` above constructs for those arguments. Exercises the
143 // real pinned key path end to end rather than a separate test-only key.
144 const FIXTURE_SIG_B64: &str =
145 "QehidZjJa2jYLPIPYSsVxUlm86W5Fdbr9PV3P4HJyZwJ68/HZR9EaAL0MpcVtTuZJW2+MMGebc0RH9HITNJGCw==";
146
147 /// The development build's half of the pair (task #49102): local development and test builds still
148 /// verify traffic signed with the development key, so nothing local breaks. The release build's
149 /// counterpart, `a_development_key_signature_is_refused_by_a_release_build`, is compiled in
150 /// there instead.
151 #[cfg(debug_assertions)]
152 #[test]
153 fn a_development_key_signature_verifies_in_a_development_build() {
154 assert!(verify_reagent_jekt(
155 "reagent-v1-dev",
156 "msg-1",
157 "github-consumer",
158 "agentx",
159 1_000,
160 "hello",
161 FIXTURE_SIG_B64,
162 ));
163 }
164
165 /// The release-build half of the pair above (task #49102): a development signature is refused, and
166 /// the release build's reason is in the log line the compiled refusal carries (asserted by
167 /// `the_development_signing_key_is_compiled_only_into_development_builds`).
168 #[cfg(not(debug_assertions))]
169 #[test]
170 fn a_development_key_signature_is_refused_by_a_release_build() {
171 assert!(
172 !verify_reagent_jekt("reagent-v1-dev", "msg-1", "github-consumer", "agentx", 1_000, "hello", FIXTURE_SIG_B64),
173 "a release build must not verify a development key's signature"
174 );
175 assert!(!verify_trusted_reagent_jekt(
176 "reagent-v1-dev",
177 "msg-1",
178 "github-consumer",
179 "agentx",
180 1_000,
181 "hello",
182 FIXTURE_SIG_B64,
183 ));
184 assert!(!is_reagent_trusted_signing_key("reagent-v1-dev"), "and it never authorises a verified sender");
185 }
186
187 // Same fixture material, signed under the real production key
188 // (`reagent-v1`, provisioned 2026-08-14 — see this key's own doc comment
189 // on `reagent_public_key` for how the private half was generated and
190 // provisioned without ever appearing in a transcript). Proves the
191 // production key is genuinely wired up and verifiable end to end, not
192 // just registered as an unused map entry.
193 const PROD_FIXTURE_SIG_B64: &str =
194 "FCFjcvAzla329a39u8fFxOvRWaH1R2fUn8RsGtP9RaIbLbaS3aXgAQ7YB4ssWV5TDvAeGrwSkHfoeGi11iCPBg==";
195
196 #[test]
197 fn a_correctly_signed_reagent_message_verifies_under_the_production_key() {
198 assert!(verify_reagent_jekt(
199 "reagent-v1",
200 "msg-1",
201 "github-consumer",
202 "agentx",
203 1_000,
204 "hello",
205 PROD_FIXTURE_SIG_B64,
206 ));
207 }
208
209 #[test]
210 fn the_dev_and_prod_reagent_keys_are_not_interchangeable() {
211 // The prod fixture must NOT verify against the dev key id, and
212 // vice versa -- proves these are genuinely two distinct keys, not
213 // the same key registered twice under different names.
214 assert!(!verify_reagent_jekt(
215 "reagent-v1-dev",
216 "msg-1",
217 "github-consumer",
218 "agentx",
219 1_000,
220 "hello",
221 PROD_FIXTURE_SIG_B64,
222 ));
223 assert!(!verify_reagent_jekt(
224 "reagent-v1",
225 "msg-1",
226 "github-consumer",
227 "agentx",
228 1_000,
229 "hello",
230 FIXTURE_SIG_B64,
231 ));
232 }
233
234 // ---- is_reagent_trusted_signing_key ----
235 //
236 // The dev key verifies signatures fine (it's a real registered key) but
237 // must never be treated as authorization to relax TIER=sensitive — its
238 // private half is documented as exposed since generation. Only the
239 // production key is "trusted" in that stronger sense.
240
241 #[test]
242 fn the_production_key_is_trusted_for_tier_relaxation() {
243 assert!(is_reagent_trusted_signing_key("reagent-v1"));
244 }
245
246 #[cfg(debug_assertions)]
247 #[test]
248 fn the_exposed_dev_key_is_not_trusted_for_tier_relaxation_even_though_it_verifies() {
249 assert!(!is_reagent_trusted_signing_key("reagent-v1-dev"));
250 // Sanity check: it's not untrusted because it's unregistered — in a development or test
251 // build it genuinely verifies signatures, it's just not the trusted one.
252 assert!(verify_reagent_jekt(
253 "reagent-v1-dev",
254 "msg-1",
255 "github-consumer",
256 "agentx",
257 1_000,
258 "hello",
259 FIXTURE_SIG_B64,
260 ));
261 }
262
263 #[test]
264 fn an_unregistered_key_id_is_not_trusted_for_tier_relaxation() {
265 assert!(!is_reagent_trusted_signing_key("reagent-v2-does-not-exist"));
266 }
267
268 /// The development signing key exists only in a development or test build (task #49102): the arm
269 /// that registers it is compiled under `debug_assertions` alone, the release build's refusal
270 /// carries its reason, and no build configuration or workflow turns debug assertions on for a
271 /// release. So the key cannot be switched back on by a setting or an environment variable.
272 #[test]
273 fn the_development_signing_key_is_compiled_only_into_development_builds() {
274 let source = include_str!("reagent.rs");
275 let lines: Vec<&str> = source.lines().collect();
276 let arm = lines
277 .iter()
278 .position(|l| l.trim_start().starts_with("DEV_SIGNING_KEY_ID => Some("))
279 .expect("the development key's registration arm");
280 assert_eq!(
281 lines[arm - 1].trim(),
282 "#[cfg(debug_assertions)]",
283 "the development key is compiled in only where debug assertions are on"
284 );
285 assert!(
286 source.contains("a release build does not accept it"),
287 "the release build's refusal names its reason in the log"
288 );
289 let workspace = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("..").join("..");
290 let mut texts = vec![std::fs::read_to_string(workspace.join("Cargo.toml")).unwrap_or_default()];
291 texts.extend(["config.toml", "config"].iter().filter_map(|f| std::fs::read_to_string(workspace.join(".cargo").join(f)).ok()));
292 if let Ok(dir) = std::fs::read_dir(workspace.join(".github").join("workflows")) {
293 texts.extend(dir.flatten().filter_map(|f| std::fs::read_to_string(f.path()).ok()));
294 }
295 for text in texts {
296 for line in text.lines().filter(|l| !l.trim_start().starts_with('#')) {
297 let squeezed: String = line.split_whitespace().collect();
298 assert!(
299 !squeezed.contains("debug-assertions=true") && !squeezed.contains("Cdebug-assertions"),
300 "a build turns debug assertions on, which would compile the key into a release: {line}"
301 );
302 }
303 }
304 }
305
306 // ---- verify_trusted_reagent_jekt ----
307
308 #[test]
309 fn a_production_key_signature_is_a_trusted_verification() {
310 assert!(verify_trusted_reagent_jekt(
311 "reagent-v1",
312 "msg-1",
313 "github-consumer",
314 "agentx",
315 1_000,
316 "hello",
317 PROD_FIXTURE_SIG_B64,
318 ));
319 }
320
321 #[test]
322 fn a_valid_dev_key_signature_is_not_a_trusted_verification() {
323 assert!(!verify_trusted_reagent_jekt(
324 "reagent-v1-dev",
325 "msg-1",
326 "github-consumer",
327 "agentx",
328 1_000,
329 "hello",
330 FIXTURE_SIG_B64,
331 ));
332 }
333
334 #[test]
335 fn a_tampered_production_key_message_is_not_a_trusted_verification() {
336 assert!(!verify_trusted_reagent_jekt(
337 "reagent-v1",
338 "msg-1",
339 "github-consumer",
340 "agentx",
341 1_000,
342 "hello, tampered",
343 PROD_FIXTURE_SIG_B64,
344 ));
345 }
346
347 #[test]
348 fn reagent_unknown_key_id_fails_verification() {
349 assert!(!verify_reagent_jekt(
350 "reagent-v2-does-not-exist",
351 "msg-1",
352 "github-consumer",
353 "agentx",
354 1_000,
355 "hello",
356 FIXTURE_SIG_B64,
357 ));
358 }
359
360 #[test]
361 fn reagent_tampered_message_fails_verification() {
362 assert!(!verify_reagent_jekt(
363 "reagent-v1-dev",
364 "msg-1",
365 "github-consumer",
366 "agentx",
367 1_000,
368 "goodbye",
369 FIXTURE_SIG_B64,
370 ));
371 }
372
373 #[test]
374 fn reagent_wrong_target_fails_verification() {
375 // Same reattribution/replay resistance as host-tier HMAC — the
376 // signature is bound to target_agent, so it can't be replayed
377 // claiming delivery to a different agent.
378 assert!(!verify_reagent_jekt(
379 "reagent-v1-dev",
380 "msg-1",
381 "github-consumer",
382 "agenty",
383 1_000,
384 "hello",
385 FIXTURE_SIG_B64,
386 ));
387 }
388
389 #[test]
390 fn reagent_wrong_source_fails_verification() {
391 assert!(!verify_reagent_jekt(
392 "reagent-v1-dev",
393 "msg-1",
394 "someone-else",
395 "agentx",
396 1_000,
397 "hello",
398 FIXTURE_SIG_B64,
399 ));
400 }
401
402 #[test]
403 fn reagent_wrong_timestamp_fails_verification() {
404 assert!(!verify_reagent_jekt(
405 "reagent-v1-dev",
406 "msg-1",
407 "github-consumer",
408 "agentx",
409 1_001,
410 "hello",
411 FIXTURE_SIG_B64,
412 ));
413 }
414
415 #[test]
416 fn reagent_wrong_msgid_fails_verification() {
417 assert!(!verify_reagent_jekt(
418 "reagent-v1-dev",
419 "msg-2",
420 "github-consumer",
421 "agentx",
422 1_000,
423 "hello",
424 FIXTURE_SIG_B64,
425 ));
426 }
427
428 #[test]
429 fn reagent_malformed_base64_signature_fails_verification_not_panics() {
430 assert!(!verify_reagent_jekt(
431 "reagent-v1-dev",
432 "msg-1",
433 "github-consumer",
434 "agentx",
435 1_000,
436 "hello",
437 "not-valid-base64!!",
438 ));
439 }
440
441 #[test]
442 fn reagent_wrong_length_signature_fails_verification_not_panics() {
443 assert!(!verify_reagent_jekt(
444 "reagent-v1-dev",
445 "msg-1",
446 "github-consumer",
447 "agentx",
448 1_000,
449 "hello",
450 "dG9vc2hvcnQ=", // "tooshort" base64 — decodes fine, wrong length
451 ));
452 }
453
454 #[test]
455 fn reagent_empty_signature_fails_verification() {
456 assert!(!verify_reagent_jekt(
457 "reagent-v1-dev",
458 "msg-1",
459 "github-consumer",
460 "agentx",
461 1_000,
462 "hello",
463 "",
464 ));
465 }
466}