//! WHICH SERVER ANSWERED — the one fact a relay name cannot carry. //! //! A household is one relay name, and the relay hands that name to whichever //! Mac is active: the name stays the same when the machine behind it changes. //! So a device that keeps a verdict about itself (`403 device_not_approved`, //! `device_revoked`) has to keep WHO gave it, or a stale Mac that held the name //! for sixty seconds speaks for the household until somebody re-pairs — which //! is what happened on 2026-09-28 (`cotton-client/src/transport/refusal.rs`). //! //! Every answer a server makes carries [`SERVER_ID_HEADER`], and its value is //! [`server_id_of`] the serving machine's own device id. **A tag, not the id**: //! the header is on `GET /health` too, which says nothing about who is hosted //! there, so what an unauthenticated caller learns is "the same machine //! answered twice" and nothing it could address a route with. The TLS //! certificate cannot do this job — it is sealed into the vault and placed on //! every Mac of the household, so every Mac presents the same one. use sha2::{Digest, Sha256}; /// The header every answer carries: the answering machine's tag. pub const SERVER_ID_HEADER: &str = "x-cotton-server"; /// Domain separation, so the tag is never a hash some other part of the tree /// also computes over a device id. const SERVER_ID_CONTEXT: &str = "cotton-server-id-v1"; /// The tag for a serving machine: SHA-256 over the context and the machine's /// device id, the first 16 bytes as lower-case hex. /// /// Stable for a machine's life (its device key does not change) and different /// for every machine, which are the only two things a reader compares. pub fn server_id_of(device_id: &str) -> String { let mut hasher = Sha256::new(); hasher.update(SERVER_ID_CONTEXT.as_bytes()); hasher.update(b"\n"); hasher.update(device_id.as_bytes()); hex::encode(&hasher.finalize()[..16]) } #[cfg(test)] mod tests { use super::*; #[test] fn a_machine_has_one_tag_and_two_machines_have_two() { assert_eq!(server_id_of("mac-a-device"), server_id_of("mac-a-device")); assert_ne!(server_id_of("mac-a-device"), server_id_of("mac-b-device")); } #[test] fn the_tag_is_not_the_device_id_it_was_made_from() { let tag = server_id_of("mac-a-device"); assert_eq!(tag.len(), 32); assert!(!tag.contains("mac-a")); assert!(tag.bytes().all(|byte| byte.is_ascii_hexdigit())); } }