//! Local verification for the separately distributed stable bootstrap updater. use crate::{ ArtifactRole, Platform, SignedRelease, TrustedContext, TrustedUpdateKey, UpdateKeyring, VerifiedRelease, }; use anyhow::{Context, Result, ensure}; use rmm_domain::agent_update::{ReleaseEnvelope, UpdateKeyringFile}; use std::{ ffi::OsString, fs::File, io::Read, time::{SystemTime, UNIX_EPOCH}, }; fn bounded(path: &OsString, limit: u64) -> Result> { let mut bytes = Vec::new(); File::open(path)?.take(limit + 1).read_to_end(&mut bytes)?; ensure!(bytes.len() as u64 <= limit, "bootstrap metadata too large"); Ok(bytes) } /// Arguments are four local files in order: pinned keyring, envelope, agent, /// helper. The caller owns the protected staging directory and activation floor. pub fn verify_bootstrap_files(platform: Platform, args: &[OsString]) -> Result { let [keyring, envelope, agent, helper] = args else { anyhow::bail!("four local paths required: KEYRING ENVELOPE AGENT HELPER"); }; let config: UpdateKeyringFile = serde_json::from_slice(&bounded(keyring, 16384)?)?; ensure!(config.validate(), "invalid bootstrap keyring"); let keys = config .keys .into_iter() .map(|key| { let raw = hex::decode(key.public_key_hex)? .try_into() .ok() .context("Ed25519 public key length")?; TrustedUpdateKey::new( raw, key.platform.into(), key.first_sequence, key.last_sequence, key.revoked, ) }) .collect::>>()?; let ring = UpdateKeyring::new(config.generation, keys)?; let envelope: ReleaseEnvelope = serde_json::from_slice(&bounded(envelope, 32768)?)?; let decoded = envelope .decode() .map_err(|_| anyhow::anyhow!("invalid release envelope"))?; let context = TrustedContext { platform, now_utc_seconds: SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs(), keyring_generation: config.generation, protocol_major: 1, state_schema: 1, updater_version: if platform == Platform::WindowsGnuX64 { 2 } else { 1 }, minimum_release_sequence: 0, active_release: None, }; let release = crate::verify_release( SignedRelease { schema_version: envelope.schema_version, key_id: &envelope.key_id, payload_bytes: &decoded.payload_bytes, signature: &decoded.signature, }, &ring, &context, )?; release.verify_artifact(ArtifactRole::Agent, File::open(agent)?)?; release.verify_artifact(ArtifactRole::Helper, File::open(helper)?)?; Ok(release) }