Skip to main content

hoike_core/
router.rs

1use std::collections::HashMap;
2use std::path::Path;
3use std::sync::{Arc, Mutex};
4
5use arc_swap::ArcSwap;
6use tracing::{info, warn};
7
8use crate::admission::admit_bundle;
9use crate::config::Config;
10use crate::error::{CoreError, Result};
11use crate::request::ParsedCertId;
12use crate::state::StateStore;
13use crate::storage::StateDirectoryLock;
14use ahu::Bundle;
15
16/// Routing key: (issuerNameHash, issuerKeyHash).
17/// Both SHA-1 and SHA-256 hash algorithms produce different CertIDs for the
18/// same CA, so both the SHA-1 and SHA-256 hashes from the bundle manifest
19/// are registered as separate scope keys pointing to the same bundle.
20type ScopeKey = (Vec<u8>, Vec<u8>);
21
22/// Metadata about a CA scope within a loaded bundle.
23#[derive(Debug, Clone)]
24pub struct ScopeEntry {
25    pub bundle_idx: usize,
26    pub ca_label: String,
27    pub nonce_policy: String,
28    pub completeness: String,
29    pub forward_to: Option<String>,
30    /// Fraction of the validity window advertised as HTTP `max-age` for this
31    /// scope's responses (from `CaConfig.max_age_fraction`).
32    pub max_age_fraction: f64,
33}
34
35/// The loaded working set: bundles indexed by CA scope for routing.
36pub struct ScopeMap {
37    entries: HashMap<ScopeKey, Vec<ScopeEntry>>,
38    bundles: Vec<Arc<Bundle>>,
39}
40
41/// Result of a successful lookup — the response bytes plus the window
42/// from the serving bundle for HTTP header generation.
43pub struct LookupResult {
44    pub response_bytes: Vec<u8>,
45    pub window: ahu::Window,
46    pub ca_label: String,
47    pub nonce_policy: String,
48    pub forward_to: Option<String>,
49    pub max_age_fraction: f64,
50}
51
52/// Per-bundle scope detail for diagnostic/admin views. Unlike `scope_info`,
53/// this carries each bundle's own window and entry count, so multi-CA
54/// deployments report accurate per-bundle data instead of the first bundle's.
55pub struct ScopeDetail {
56    pub ca_label: String,
57    pub producer_id: String,
58    pub epoch: u64,
59    pub completeness: String,
60    pub entry_count: u64,
61    pub window: ahu::Window,
62}
63
64/// The responder's loaded state: bundles indexed by CA scope,
65/// with persistent anti-rollback state.
66pub struct ResponderState {
67    scope_map: ArcSwap<ScopeMap>,
68    pub config: Config,
69    state_store: Mutex<StateStore>,
70    /// Retained writer lock (F04). Every clone of this responder — the
71    /// listener owner, the background signer loop, admin on-demand writers —
72    /// keeps the state-directory lock held; it is released only when the last
73    /// writer referencing this responder goes away. Retention is the field's
74    /// entire purpose.
75    #[allow(dead_code)]
76    writer_guard: Arc<StateDirectoryLock>,
77}
78
79impl ResponderState {
80    pub fn load(config: Config, guard: Arc<StateDirectoryLock>) -> Result<Self> {
81        // The guard must protect the directory this configuration names, so a
82        // mismatched guard can never authorize writes elsewhere.
83        let state_dir = std::fs::canonicalize(&config.storage.state_db).map_err(|e| {
84            CoreError::Config(format!(
85                "cannot resolve state directory {}: {e}",
86                config.storage.state_db.display()
87            ))
88        })?;
89        if guard.state_dir() != state_dir {
90            return Err(CoreError::Config(format!(
91                "writer lock guards state directory {} but the configuration uses {}",
92                guard.state_dir().display(),
93                state_dir.display()
94            )));
95        }
96        let state_db_path = config.storage.state_db.join("state.json");
97        let mut state_store = StateStore::open(&state_db_path)?;
98        let scope_map = match load_scope_map(&config, &mut state_store) {
99            Ok(map) => map,
100            Err(original) if !state_store.active_bundles().is_empty() => {
101                warn!(error = %original, "configured bundle load failed; recovering committed generation");
102                let mut recovery = config.clone();
103                if recovery.ca.is_empty() {
104                    let path = state_store
105                        .active_bundles()
106                        .get("default")
107                        .ok_or(original)?;
108                    let path = path.clone();
109                    let mut candidate = state_store.transaction();
110                    candidate.clear_active_bundles();
111                    let bundle = load_and_verify_bundle(&path, &config)?;
112                    candidate.check_rollback(&bundle)?;
113                    candidate.check_continuity(&bundle)?;
114                    candidate.advance_from_bundle(&bundle)?;
115                    candidate.snapshot("default", &bundle)?;
116                    let mut entries = HashMap::new();
117                    register_bundle_scopes(
118                        &bundle,
119                        0,
120                        "default",
121                        "ignore",
122                        "authoritative-complete",
123                        None,
124                        0.5,
125                        &mut entries,
126                    );
127                    state_store.commit(candidate)?;
128                    return Ok(Self {
129                        scope_map: ArcSwap::from_pointee(ScopeMap {
130                            entries,
131                            bundles: vec![Arc::new(bundle)],
132                        }),
133                        config,
134                        state_store: Mutex::new(state_store),
135                        writer_guard: guard,
136                    });
137                }
138                for ca in &mut recovery.ca {
139                    ca.bundle_file = Some(
140                        state_store
141                            .active_bundles()
142                            .get(&ca.label)
143                            .ok_or_else(|| {
144                                CoreError::Config(format!("no committed snapshot for {}", ca.label))
145                            })?
146                            .clone(),
147                    );
148                }
149                load_scope_map(&recovery, &mut state_store)?
150            }
151            Err(err) => return Err(err),
152        };
153        Ok(ResponderState {
154            scope_map: ArcSwap::from_pointee(scope_map),
155            config,
156            state_store: Mutex::new(state_store),
157            writer_guard: guard,
158        })
159    }
160
161    /// Resolve the CA scope a CertID routes to, without requiring an entry.
162    ///
163    /// Returns `(ca_label, is_authoritative_complete, forward_to)` for the
164    /// first scope matching the CertID's issuer hashes. Used by the serving
165    /// path to decide whether a serial with no stored entry is provably
166    /// never-issued (`unknown`) rather than out-of-scope (`unauthorized`),
167    /// and where to forward a nonce-bearing request for it.
168    pub fn scope_for_certid(
169        &self,
170        cert_id: &ParsedCertId,
171    ) -> Option<(String, bool, Option<String>)> {
172        let map = self.scope_map.load();
173        let key = (
174            cert_id.issuer_name_hash.clone(),
175            cert_id.issuer_key_hash.clone(),
176        );
177        let entries = map.entries.get(&key)?;
178        entries.first().map(|entry| {
179            let authoritative = entry.completeness == "authoritative-complete";
180            (
181                entry.ca_label.clone(),
182                authoritative,
183                entry.forward_to.clone(),
184            )
185        })
186    }
187
188    /// Look up a CertID across all loaded CA scopes.
189    ///
190    /// Routes by (issuerNameHash, issuerKeyHash) to find candidate bundles,
191    /// then searches each by entry_key. On collision (multiple bundles hold
192    /// an entry for the same serial), logs a warning and returns the first
193    /// by configuration order.
194    pub fn lookup(&self, cert_id: &ParsedCertId, preferred_algs: &[u16]) -> Option<LookupResult> {
195        let map = self.scope_map.load();
196        let key = (
197            cert_id.issuer_name_hash.clone(),
198            cert_id.issuer_key_hash.clone(),
199        );
200
201        let scope_entries = map.entries.get(&key)?;
202
203        let mut hits: Vec<(&ScopeEntry, Vec<u8>, ahu::Window)> = Vec::new();
204
205        for entry in scope_entries {
206            let bundle = &map.bundles[entry.bundle_idx];
207            let found = if preferred_algs.is_empty() {
208                bundle.lookup(&cert_id.entry_key)
209            } else {
210                bundle.lookup_preferred(&cert_id.entry_key, preferred_algs)
211            };
212            if let Some(response_bytes) = found {
213                hits.push((
214                    entry,
215                    response_bytes.to_vec(),
216                    bundle.manifest.window.clone(),
217                ));
218            }
219        }
220
221        let make_result =
222            |entry: &ScopeEntry, response_bytes: Vec<u8>, window: ahu::Window| LookupResult {
223                response_bytes,
224                window,
225                ca_label: entry.ca_label.clone(),
226                nonce_policy: entry.nonce_policy.clone(),
227                forward_to: entry.forward_to.clone(),
228                max_age_fraction: entry.max_age_fraction,
229            };
230
231        match hits.len() {
232            0 => None,
233            1 => {
234                let (entry, response_bytes, window) = hits.into_iter().next().unwrap();
235                Some(make_result(entry, response_bytes, window))
236            }
237            n => {
238                warn!(
239                    count = n,
240                    serial = hex::encode(&cert_id.serial_number),
241                    issuer_key_hash = hex::encode(&cert_id.issuer_key_hash),
242                    "multiple scopes hold entry for same serial — answering from first by config order"
243                );
244                let (entry, response_bytes, window) = hits.into_iter().next().unwrap();
245                Some(make_result(entry, response_bytes, window))
246            }
247        }
248    }
249
250    /// Get the first loaded bundle's window (for backward compatibility
251    /// with single-CA deployments). Prefer `LookupResult.window` when
252    /// a specific lookup succeeded.
253    pub fn default_window(&self) -> Option<ahu::Window> {
254        let map = self.scope_map.load();
255        map.bundles.first().map(|b| b.manifest.window.clone())
256    }
257
258    /// Total entry count across all loaded bundles.
259    pub fn total_entries(&self) -> u64 {
260        let map = self.scope_map.load();
261        map.bundles.iter().map(|b| b.manifest.entry_count).sum()
262    }
263
264    /// Number of loaded bundles.
265    pub fn bundle_count(&self) -> usize {
266        let map = self.scope_map.load();
267        map.bundles.len()
268    }
269
270    /// Scope count (number of distinct routing keys).
271    pub fn scope_count(&self) -> usize {
272        let map = self.scope_map.load();
273        map.entries.len()
274    }
275
276    /// Get info about all loaded scopes for diagnostics.
277    pub fn scope_info(&self) -> Vec<(String, u64, String)> {
278        let map = self.scope_map.load();
279        let mut info = Vec::new();
280        for entries in map.entries.values() {
281            for entry in entries {
282                let bundle = &map.bundles[entry.bundle_idx];
283                info.push((
284                    entry.ca_label.clone(),
285                    bundle
286                        .manifest
287                        .ca_scopes
288                        .first()
289                        .map(|s| s.epoch)
290                        .unwrap_or(0),
291                    entry.completeness.clone(),
292                ));
293            }
294        }
295        info
296    }
297
298    /// Per-bundle scope details (label, epoch, completeness, entry count, and
299    /// the bundle's own validity window). Prefer this over `scope_info` +
300    /// `default_window` when reporting per-bundle data for multi-CA setups.
301    pub fn bundle_scopes(&self) -> Vec<ScopeDetail> {
302        let map = self.scope_map.load();
303        let mut out = Vec::new();
304        for entries in map.entries.values() {
305            for entry in entries {
306                let bundle = &map.bundles[entry.bundle_idx];
307                out.push(ScopeDetail {
308                    ca_label: entry.ca_label.clone(),
309                    producer_id: bundle.manifest.producer_id.clone(),
310                    epoch: bundle
311                        .manifest
312                        .ca_scopes
313                        .first()
314                        .map(|s| s.epoch)
315                        .unwrap_or(0),
316                    completeness: entry.completeness.clone(),
317                    entry_count: bundle.manifest.entry_count,
318                    window: bundle.manifest.window.clone(),
319                });
320            }
321        }
322        out
323    }
324
325    /// Hot-reload all bundles from disk with anti-rollback checks.
326    pub fn reload(&self) -> Result<()> {
327        let mut store = self
328            .state_store
329            .lock()
330            .map_err(|e| CoreError::StateStore(format!("state store lock poisoned: {e}")))?;
331        let scope_map = load_scope_map(&self.config, &mut store)?;
332        let total: u64 = scope_map
333            .bundles
334            .iter()
335            .map(|b| b.manifest.entry_count)
336            .sum();
337        self.scope_map.store(Arc::new(scope_map));
338        info!(total_entries = total, "all bundles reloaded");
339        Ok(())
340    }
341}
342
343fn validate_nonce_config(config: &Config) -> Result<()> {
344    let mut labels = std::collections::HashSet::new();
345    for ca in &config.ca {
346        if !labels.insert(&ca.label) {
347            return Err(CoreError::Config(format!(
348                "duplicate CA label '{}'",
349                ca.label
350            )));
351        }
352        match ca.nonce_policy.as_str() {
353            "ignore" => {}
354            "live" => {
355                if config.server.mode == "edge" {
356                    return Err(CoreError::Config(format!(
357                        "CA '{}': nonce_policy \"live\" requires signer or combined mode \
358                         (edge nodes have no signing key)",
359                        ca.label
360                    )));
361                }
362                if ca.signing_key.is_none() {
363                    return Err(CoreError::Config(format!(
364                        "CA '{}': nonce_policy \"live\" requires a signing_key",
365                        ca.label
366                    )));
367                }
368            }
369            "forward" => {
370                if ca.forward_to.is_none() {
371                    return Err(CoreError::Config(format!(
372                        "CA '{}': nonce_policy \"forward\" requires a forward_to URL",
373                        ca.label
374                    )));
375                }
376            }
377            other => {
378                return Err(CoreError::Config(format!(
379                    "CA '{}': unknown nonce_policy \"{other}\" (expected: ignore, live, forward)",
380                    ca.label
381                )));
382            }
383        }
384    }
385    Ok(())
386}
387
388fn load_scope_map(config: &Config, state_store: &mut StateStore) -> Result<ScopeMap> {
389    // A full reload explicitly builds a replacement working set: the
390    // candidate starts from the committed snapshots of every configured
391    // label, not a merge with whatever was committed before.
392    let mut candidate = state_store.transaction();
393    candidate.clear_active_bundles();
394    let map = load_scope_map_candidate(config, &mut candidate)?;
395    state_store.commit(candidate)?;
396    Ok(map)
397}
398
399fn load_scope_map_candidate(config: &Config, state_store: &mut StateStore) -> Result<ScopeMap> {
400    let bundle_paths = configured_bundle_paths(config)?;
401
402    let mut bundles: Vec<Arc<Bundle>> = Vec::new();
403    let mut entries: HashMap<ScopeKey, Vec<ScopeEntry>> = HashMap::new();
404    let mut loaded_paths: HashMap<std::path::PathBuf, usize> = HashMap::new();
405
406    if config.ca.is_empty() {
407        let bundle = load_and_verify_bundle(&bundle_paths[0], config)?;
408        state_store.check_rollback(&bundle)?;
409        state_store.check_continuity(&bundle)?;
410        state_store.advance_from_bundle(&bundle)?;
411        let bundle_idx = 0;
412        register_bundle_scopes(
413            &bundle,
414            bundle_idx,
415            "default",
416            "ignore",
417            "authoritative-complete",
418            None,
419            0.5,
420            &mut entries,
421        );
422        bundles.push(Arc::new(bundle));
423    } else {
424        for (ca_config, bundle_path) in config.ca.iter().zip(bundle_paths) {
425            let canonical = bundle_path.canonicalize().unwrap_or(bundle_path.clone());
426
427            let bundle_idx = if let Some(&idx) = loaded_paths.get(&canonical) {
428                idx
429            } else {
430                let bundle = load_and_verify_bundle(&bundle_path, config)?;
431                state_store.check_rollback(&bundle)?;
432                state_store.check_continuity(&bundle)?;
433                state_store.advance_from_bundle(&bundle)?;
434                let idx = bundles.len();
435                bundles.push(Arc::new(bundle));
436                loaded_paths.insert(canonical, idx);
437                idx
438            };
439
440            register_bundle_scopes(
441                &bundles[bundle_idx],
442                bundle_idx,
443                &ca_config.label,
444                &ca_config.nonce_policy,
445                &ca_config.completeness,
446                ca_config.forward_to.as_deref(),
447                ca_config.max_age_fraction,
448                &mut entries,
449            );
450        }
451    }
452
453    // All inputs passed validation before writing any new immutable snapshots.
454    let mut labels = std::collections::HashSet::new();
455    for entries_for_scope in entries.values() {
456        for entry in entries_for_scope {
457            if labels.insert(entry.ca_label.clone()) {
458                state_store.snapshot(&entry.ca_label, &bundles[entry.bundle_idx])?;
459            }
460        }
461    }
462
463    let scope_count = entries.len();
464    let bundle_count = bundles.len();
465    info!(
466        bundles = bundle_count,
467        scopes = scope_count,
468        "scope map loaded"
469    );
470
471    Ok(ScopeMap { entries, bundles })
472}
473
474#[allow(clippy::too_many_arguments)]
475fn register_bundle_scopes(
476    bundle: &Bundle,
477    bundle_idx: usize,
478    ca_label: &str,
479    nonce_policy: &str,
480    completeness: &str,
481    forward_to: Option<&str>,
482    max_age_fraction: f64,
483    entries: &mut HashMap<ScopeKey, Vec<ScopeEntry>>,
484) {
485    for scope in &bundle.manifest.ca_scopes {
486        let key = (
487            scope.issuer_name_hash.clone(),
488            scope.issuer_key_hash.clone(),
489        );
490
491        let entry = ScopeEntry {
492            bundle_idx,
493            ca_label: ca_label.to_string(),
494            nonce_policy: nonce_policy.to_string(),
495            completeness: completeness.to_string(),
496            forward_to: forward_to.map(|s| s.to_string()),
497            max_age_fraction,
498        };
499
500        entries.entry(key).or_default().push(entry);
501
502        info!(
503            ca = ca_label,
504            epoch = scope.epoch,
505            issuer_key_hash =
506                hex::encode(&scope.issuer_key_hash[..8.min(scope.issuer_key_hash.len())]),
507            "registered CA scope"
508        );
509    }
510}
511
512/// Select configured bundle paths without opening or modifying rollback state.
513///
514/// Paths follow CA configuration order (and may repeat). With no explicit CAs,
515/// preserve legacy highest-epoch discovery. Both serving and read-only checks
516/// use the same selection and nonce-policy validation.
517pub fn configured_bundle_paths(config: &Config) -> Result<Vec<std::path::PathBuf>> {
518    validate_nonce_config(config)?;
519    if config.ca.is_empty() {
520        Ok(vec![find_newest_bundle(&config.storage.bundle_dir)?])
521    } else {
522        Ok(config
523            .ca
524            .iter()
525            .map(|ca| {
526                ca.bundle_file
527                    .clone()
528                    .unwrap_or_else(|| config.storage.bundle_dir.join(format!("{}.ahu", ca.label)))
529            })
530            .collect())
531    }
532}
533
534fn load_and_verify_bundle(path: &Path, config: &Config) -> Result<Bundle> {
535    info!(path = %path.display(), "loading bundle");
536    let bundle = Bundle::from_file(path)?;
537    let now = std::time::SystemTime::now()
538        .duration_since(std::time::UNIX_EPOCH)
539        .map_err(|e| CoreError::Config(e.to_string()))?
540        .as_secs();
541    let trust = admit_bundle(&bundle, &config.storage, now)?;
542
543    info!(
544        entry_count = bundle.manifest.entry_count,
545        producer = %bundle.manifest.producer_id,
546        scopes = bundle.manifest.ca_scopes.len(),
547        ?trust,
548        "bundle admitted"
549    );
550
551    Ok(bundle)
552}
553
554fn find_newest_bundle(dir: &Path) -> Result<std::path::PathBuf> {
555    let ahu_files: Vec<_> = std::fs::read_dir(dir)?
556        .filter_map(|e| e.ok())
557        .filter(|e| e.path().extension().is_some_and(|ext| ext == "ahu"))
558        .map(|e| e.path())
559        .collect();
560
561    if ahu_files.is_empty() {
562        return Err(CoreError::Config(format!(
563            "no .ahu files in {}",
564            dir.display()
565        )));
566    }
567
568    // Select the bundle with the highest max epoch across its CA scopes.
569    // Bundles that fail to parse are skipped with a warning.
570    let mut best: Option<(std::path::PathBuf, u64)> = None;
571    for path in &ahu_files {
572        let max_epoch = match ahu::Bundle::from_file(path) {
573            Ok(bundle) => bundle
574                .manifest
575                .ca_scopes
576                .iter()
577                .map(|s| s.epoch)
578                .max()
579                .unwrap_or(0),
580            Err(e) => {
581                warn!(
582                    path = %path.display(),
583                    error = %e,
584                    "failed to parse bundle for epoch selection, skipping"
585                );
586                continue;
587            }
588        };
589        if best.as_ref().is_none_or(|(_, e)| max_epoch > *e) {
590            best = Some((path.clone(), max_epoch));
591        }
592    }
593
594    best.map(|(p, _)| p)
595        .ok_or_else(|| CoreError::Config(format!("no valid .ahu files in {}", dir.display())))
596}