| 1 | //! Peer-authentication predicates, reified sans-IO. The signed-request checks in |
| 2 | //! `peer_auth::verify` — a well-formed identity, a timestamp within the clock |
| 3 | //! window, and replay-cache retention — are pure predicates over scalars, and |
| 4 | //! each is a SECURITY fence. A widened identity charset lets a `target` smuggle |
| 5 | //! a path segment past the WrongTarget check; a one-sided clock window replays |
| 6 | //! an old signature forever; retention below the clock window forgets a nonce |
| 7 | //! while a signature bearing it is still acceptable, reopening the replay hole |
| 8 | //! the cache exists to close. Signing/MAC stays in production; the gates move. |
| 9 | |
| 10 | pub type Ms = u64; |
| 11 | |
| 12 | /// Is a peer identity (`source`/`target`) well-formed? Non-empty, at most 128 |
| 13 | /// bytes, and only ASCII alphanumerics plus `_ - .`. The charset is the fence — |
| 14 | /// admit `/` and a `target` could carry a path segment. |
| 15 | pub fn valid_identity(id: &str) -> bool { |
| 16 | !id.is_empty() |
| 17 | && id.len() <= 128 |
| 18 | && id |
| 19 | .bytes() |
| 20 | .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'_' | b'-' | b'.')) |
| 21 | } |
| 22 | |
| 23 | /// Is a request timestamp within the accepted clock window of `now`? The window |
| 24 | /// is TWO-sided (`abs_diff`): a check that accepted arbitrarily old timestamps |
| 25 | /// would let a captured signature replay forever. |
| 26 | pub fn within_clock_window(now: Ms, timestamp: Ms, window: Ms) -> bool { |
| 27 | now.abs_diff(timestamp) <= window |
| 28 | } |
| 29 | |
| 30 | /// May a replay-cache nonce be evicted — has its retention window elapsed? |
| 31 | /// Retention MUST be at least the clock window, or a nonce is forgotten while a |
| 32 | /// signature bearing it is still within the clock window and would be accepted. |
| 33 | pub fn replay_entry_expired(now: Ms, seen_ms: Ms, retention: Ms) -> bool { |
| 34 | now.saturating_sub(seen_ms) > retention |
| 35 | } |