Skip to main content

core_api/host/
auth.rs

1//! Host public-key types and the cloud signing-key directory.
2use serde::{Deserialize, Serialize};
3use std::collections::{BTreeMap, BTreeSet};
4
5pub const KEY_REFRESH_INTERVAL_SECONDS: u64 = 24 * 60 * 60;
6pub const CLAIM_TOKEN_REQUIRED: &str = "HOST_CLAIM_TOKEN_REQUIRED";
7
8/// Public P-256 point; x/y are exactly 32 bytes, base64url without padding.
9#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
10#[serde(rename_all = "camelCase", deny_unknown_fields)]
11pub struct PublicKey {
12    pub x: String,
13    pub y: String,
14}
15
16/// Verification-only permits a signing-key overlap; disabled keys accept no proofs.
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
18#[serde(rename_all = "camelCase")]
19pub enum SigningKeyStatus {
20    Active,
21    VerifyOnly,
22    Disabled,
23}
24
25#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
26#[serde(rename_all = "camelCase", deny_unknown_fields)]
27pub struct SigningKey {
28    pub kid: String,
29    pub status: SigningKeyStatus,
30    /// RS256 JWK modulus and exponent, base64url without padding.
31    pub n: String,
32    pub e: String,
33}
34
35/// A complete authoritative snapshot from the configured HTTPS cloud.
36#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
37#[serde(rename_all = "camelCase", deny_unknown_fields)]
38pub struct SigningKeySet {
39    pub version: u64,
40    pub keys: Vec<SigningKey>,
41}
42
43impl SigningKeySet {
44    pub fn validate_update(&self, previous: Option<&Self>) -> Result<(), &'static str> {
45        use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine};
46        if self.version == 0 || self.version > 9_007_199_254_740_991 {
47            return Err("invalid key set version");
48        }
49        if self.keys.is_empty() || self.keys.len() > 128 {
50            return Err("invalid key set size");
51        }
52        let mut kids = BTreeSet::new();
53        let mut active = 0;
54        for key in &self.keys {
55            if key.kid.is_empty() || key.kid.len() > 128 || !kids.insert(&key.kid) {
56                return Err("invalid or duplicate signing key ID");
57            }
58            let n = URL_SAFE_NO_PAD
59                .decode(&key.n)
60                .map_err(|_| "invalid RSA modulus")?;
61            let e = URL_SAFE_NO_PAD
62                .decode(&key.e)
63                .map_err(|_| "invalid RSA exponent")?;
64            if !(256..=512).contains(&n.len())
65                || n.first().is_some_and(|v| *v < 128)
66                || e != [1, 0, 1]
67            {
68                return Err("unsupported RSA public key");
69            }
70            active += usize::from(key.status == SigningKeyStatus::Active);
71        }
72        if active != 1 {
73            return Err("exactly one active signing key is required");
74        }
75        if let Some(old) = previous {
76            if self.version < old.version {
77                return Err("key set rollback");
78            }
79            let index: BTreeMap<_, _> = old.keys.iter().map(|k| (&k.kid, k)).collect();
80            for key in &self.keys {
81                if let Some(prior) = index.get(&key.kid) {
82                    if key.n != prior.n || key.e != prior.e {
83                        return Err("signing key ID was reused");
84                    }
85                    if prior.status == SigningKeyStatus::Disabled
86                        && key.status != SigningKeyStatus::Disabled
87                    {
88                        return Err("disabled signing key was reactivated");
89                    }
90                }
91            }
92            let next: BTreeMap<_, _> = self.keys.iter().map(|k| (&k.kid, k)).collect();
93            if self.version == old.version && next != index {
94                return Err("conflicting key set version");
95            }
96            // Keep tombstones: an offline Host must learn emergency disablement too.
97            if old
98                .keys
99                .iter()
100                .any(|k| k.status == SigningKeyStatus::Disabled && !next.contains_key(&k.kid))
101            {
102                return Err("disabled signing key tombstone was removed");
103            }
104        }
105        Ok(())
106    }
107
108    pub fn permits_token(&self, kid: &str) -> bool {
109        self.keys
110            .iter()
111            .any(|key| key.kid == kid && key.status != SigningKeyStatus::Disabled)
112    }
113}