Integrate signed agent catalog, tenant enrollment and HTTPS installers

This commit is contained in:
2026-11-17 15:56:18 +01:00
parent 689f326460
commit 7fd17272d5
70 changed files with 12982 additions and 327 deletions
+354
View File
@@ -0,0 +1,354 @@
//! Private Windows installed-record builder and fixed handle-based namespace.
//! Public metadata assembly is never permission to publish a record/floor.
use crate::{
helper_ipc::PinnedFile,
update_native::files::{self, Proof},
update_original::Original,
};
use anyhow::{Result, ensure};
use ed25519_dalek::{Signature, VerifyingKey};
use rmm_agent_core::update_handoff::{NativeEffect, NativeEffectClaim};
use rmm_domain::{
agent_update::ResolvedAgentUpdate,
agent_update_provisioning::{InstalledReleaseRecord, MAX_RECORD_BYTES},
jobs::hash,
};
use rmm_updater_handoff::{
Event, Evidence, HEALTH_DOMAIN, HealthReceipt, Journal, Phase, SignedHealth,
};
use std::{
fs::{File, OpenOptions},
io::Write,
os::windows::fs::OpenOptionsExt,
path::{Path, PathBuf},
};
use uuid::Uuid;
use windows_sys::Win32::Storage::FileSystem::*;
const ROOT: &str = r"C:\ProgramData\RustDiscoveryUpdater";
const CURRENT: &str = r"C:\ProgramData\RustDiscoveryUpdater\installed-record.json";
const RECORDS: &str = r"C:\ProgramData\RustDiscoveryUpdater\records";
pub(crate) struct BuiltRecord {
record: InstalledReleaseRecord,
raw: Vec<u8>,
prior_raw: Vec<u8>,
old_sha256: String,
sha256: String,
grant_id: Uuid,
owner_nonce: Uuid,
}
impl BuiltRecord {
pub(crate) fn build(
original: &Original,
prior_raw: &[u8],
journal: &Journal,
health_event_raw: &[u8],
) -> Result<Self> {
let header = original.handoff();
let handoff_sha256 = hash(&header.bytes()?);
ensure!(
journal.phase() == Phase::HealthVerified
&& journal.handoff_sha256() == handoff_sha256
&& !health_event_raw.is_empty()
&& health_event_raw.len() <= rmm_updater_handoff::MAX_EVENT_BYTES
&& hash(health_event_raw) == journal.head_sha256(),
"UPDATE_RECORD_HEALTH_HEAD"
);
let event: Event = serde_json::from_slice(health_event_raw)?;
ensure!(
serde_json::to_vec(&event)? == health_event_raw
&& event.handoff_sha256 == handoff_sha256
&& event.boot_id == header.boot_id,
"UPDATE_RECORD_HEALTH_EVENT"
);
let Evidence::HealthVerified { receipt: signed } = &event.evidence else {
anyhow::bail!("UPDATE_RECORD_HEALTH_REQUIRED")
};
ensure!(
!signed.payload.is_empty()
&& signed.payload.len() <= 4096
&& signed.signature.len() == 64,
"UPDATE_RECORD_HEALTH_LIMIT"
);
let receipt: HealthReceipt = serde_json::from_slice(&signed.payload)?;
ensure!(
serde_json::to_vec(&receipt)? == signed.payload,
"UPDATE_RECORD_HEALTH_NONCANONICAL"
);
// Journal possession is not independent key trust. Authenticate the
// exact receipt again with the ORIGINAL signed job's external key pin.
let raw: [u8; 32] = hex::decode(&original.update().updater.health_public_key_hex)?
.try_into()
.map_err(|_| anyhow::anyhow!("UPDATE_RECORD_HEALTH_KEY"))?;
let public = VerifyingKey::from_bytes(&raw)?;
ensure!(!public.is_weak(), "UPDATE_RECORD_HEALTH_KEY");
let mut message = HEALTH_DOMAIN.to_vec();
message.extend_from_slice(&signed.payload);
public.verify_strict(&message, &Signature::from_slice(&signed.signature)?)?;
ensure!(
receipt.schema_version == 1
&& receipt.handoff_sha256 == handoff_sha256
&& receipt.owner_nonce == header.owner_nonce
&& receipt.boot_id == header.boot_id
&& receipt.helper_journal_id == header.helper_journal_id
&& receipt.agent_journal_id == header.agent_journal_id
&& receipt.connection_generation > header.connection_generation
&& !receipt.ready
&& receipt.issued_at >= header.consumed_at
&& receipt.issued_at <= event.observed_at
&& event.observed_at < receipt.valid_until
&& receipt.valid_until <= header.finish_before,
"UPDATE_RECORD_HEALTH_BINDING"
);
Self::assemble(
original.update(),
prior_raw,
&handoff_sha256,
signed,
&receipt,
header.attempt.grant_id,
header.owner_nonce,
)
}
fn assemble(
update: &ResolvedAgentUpdate,
prior_raw: &[u8],
handoff_sha256: &str,
signed: &SignedHealth,
receipt: &HealthReceipt,
grant_id: Uuid,
owner_nonce: Uuid,
) -> Result<Self> {
ensure!(
update.validate() && !grant_id.is_nil() && !owner_nonce.is_nil(),
"UPDATE_RECORD_INPUT"
);
let record = rmm_domain::agent_update_provisioning::assemble_updated_record(
update,
prior_raw,
handoff_sha256,
signed,
receipt,
)
.map_err(anyhow::Error::msg)?;
let old_sha256 = hash(prior_raw);
let raw = record.bytes().map_err(anyhow::Error::msg)?;
let sha256 = hash(&raw);
ensure!(sha256 != old_sha256, "UPDATE_RECORD_SAME_BYTES");
Ok(Self {
record,
raw,
prior_raw: prior_raw.to_vec(),
old_sha256,
sha256,
grant_id,
owner_nonce,
})
}
pub(crate) fn record(&self) -> &InstalledReleaseRecord {
&self.record
}
pub(crate) fn sha256(&self) -> &str {
&self.sha256
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize)]
enum State {
Staged,
Archived,
Published,
}
pub(crate) struct Transition {
built: BuiltRecord,
root: std::cell::RefCell<PinnedFile>,
records: std::cell::RefCell<PinnedFile>,
old_proof: Proof,
new_proof: Proof,
new: File,
old: Option<File>,
stage: PathBuf,
archive: PathBuf,
state: State,
}
fn open(path: &Path, delete: bool) -> Result<File> {
let mut options = OpenOptions::new();
options
.read(true)
.share_mode(FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE)
.custom_flags(FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_WRITE_THROUGH);
if delete {
options
.access_mode(FILE_GENERIC_READ | FILE_GENERIC_WRITE | DELETE)
.share_mode(FILE_SHARE_READ);
}
Ok(options.open(path)?)
}
fn read(path: &Path) -> Result<Proof> {
files::proof(&mut open(path, false)?, path, MAX_RECORD_BYTES as u64)
}
impl Transition {
pub(crate) fn prepare(built: BuiltRecord) -> Result<Self> {
let root = PinnedFile::open(Path::new(ROOT), true, &[], &[], false)?;
let records = PinnedFile::open(Path::new(RECORDS), true, &[], &[], false)?;
let mut original = open(Path::new(CURRENT), false)?;
let old_proof = files::proof(&mut original, Path::new(CURRENT), MAX_RECORD_BYTES as u64)?;
ensure!(
old_proof.sha256 == built.old_sha256,
"UPDATE_RECORD_ORIGINAL_FILE_REQUIRED"
);
let stage = Path::new(ROOT).join(format!(".rmm-record-{}.stage", built.owner_nonce));
let archive = Path::new(RECORDS).join(format!("{}.json", built.old_sha256));
ensure!(
!archive.try_exists()?,
"UPDATE_RECORD_ARCHIVE_ALREADY_PRESENT"
);
let mut new = files::create_record_stage(&original, &stage)?;
new.write_all(&built.raw)?;
new.sync_all()?;
let new_proof = files::proof(&mut new, &stage, MAX_RECORD_BYTES as u64)?;
ensure!(
new_proof.sha256 == built.sha256
&& new_proof.security_sha256 == old_proof.security_sha256
&& new_proof.identity != old_proof.identity,
"UPDATE_RECORD_STAGE_BINDING"
);
let result = Self {
built,
root: std::cell::RefCell::new(root),
records: std::cell::RefCell::new(records),
old_proof,
new_proof,
new,
old: None,
stage,
archive,
state: State::Staged,
};
result.verify()?;
Ok(result)
}
pub(crate) fn verify(&self) -> Result<()> {
self.root.try_borrow_mut()?.check()?;
self.records.try_borrow_mut()?.check()?;
match self.state {
State::Staged => ensure!(
read(Path::new(CURRENT))? == self.old_proof
&& read(&self.stage)? == self.new_proof
&& !self.archive.try_exists()?,
"UPDATE_RECORD_STAGED_NAMESPACE_CHANGED"
),
State::Archived => ensure!(
read(&self.archive)? == self.old_proof
&& read(&self.stage)? == self.new_proof
&& !Path::new(CURRENT).try_exists()?,
"UPDATE_RECORD_ARCHIVED_NAMESPACE_CHANGED"
),
State::Published => ensure!(
read(&self.archive)? == self.old_proof
&& read(Path::new(CURRENT))? == self.new_proof
&& !self.stage.try_exists()?,
"UPDATE_RECORD_PUBLISHED_NAMESPACE_CHANGED"
),
}
Ok(())
}
pub(crate) fn verify_original(&self, raw: &[u8]) -> Result<()> {
ensure!(
self.built.prior_raw == raw && hash(raw) == self.old_proof.sha256,
"UPDATE_RECORD_PREDECESSOR_CHANGED"
);
self.verify()
}
pub(crate) fn effect(&self) -> Result<NativeEffect> {
let old_sha256 = self.built.old_sha256.clone();
let new_sha256 = self.built.sha256.clone();
Ok(match self.state {
State::Staged => NativeEffect::SaveInstalledRecord {
old_sha256,
new_sha256,
},
State::Archived => NativeEffect::PublishInstalledRecord {
old_sha256,
new_sha256,
},
State::Published => anyhow::bail!("UPDATE_RECORD_ALREADY_PUBLISHED"),
})
}
pub(crate) fn verify_effect(&self, effect: &NativeEffect, observed: bool) -> Result<()> {
self.verify()?;
let expected = match (self.state, observed) {
(State::Staged, false) | (State::Archived, true) => NativeEffect::SaveInstalledRecord {
old_sha256: self.built.old_sha256.clone(),
new_sha256: self.built.sha256.clone(),
},
(State::Archived, false) | (State::Published, true) => {
NativeEffect::PublishInstalledRecord {
old_sha256: self.built.old_sha256.clone(),
new_sha256: self.built.sha256.clone(),
}
}
_ => anyhow::bail!("UPDATE_RECORD_EFFECT_PHASE"),
};
ensure!(
*effect == expected,
"UPDATE_RECORD_EXACT_NATIVE_EFFECT_REQUIRED"
);
Ok(())
}
pub(crate) fn apply(&mut self, claim: &NativeEffectClaim) -> Result<()> {
self.verify_effect(claim.effect(), false)?;
ensure!(
claim.grant_id() == self.built.grant_id
&& claim.owner_nonce() == self.built.owner_nonce,
"UPDATE_RECORD_ORIGINAL_CLAIM_REQUIRED"
);
match self.state {
State::Staged => {
let mut old = open(Path::new(CURRENT), true)?;
ensure!(
files::proof(&mut old, Path::new(CURRENT), MAX_RECORD_BYTES as u64)?
== self.old_proof,
"UPDATE_RECORD_OLD_HANDLE_CHANGED"
);
files::rename_record(&old, &self.archive)?;
ensure!(
files::proof(&mut old, &self.archive, MAX_RECORD_BYTES as u64)?
== self.old_proof,
"UPDATE_RECORD_ARCHIVE_READBACK"
);
self.old = Some(old);
self.state = State::Archived;
}
State::Archived => {
files::rename_record(&self.new, Path::new(CURRENT))?;
ensure!(
files::proof(&mut self.new, Path::new(CURRENT), MAX_RECORD_BYTES as u64)?
== self.new_proof,
"UPDATE_RECORD_PUBLISH_READBACK"
);
self.state = State::Published;
}
State::Published => anyhow::bail!("UPDATE_RECORD_NO_RETRY"),
}
self.verify()
}
pub(crate) fn observation(&self) -> Result<String> {
self.verify()?;
Ok(hash(&serde_json::to_vec(&(
self.state,
&self.old_proof,
&self.new_proof,
))?))
}
pub(crate) fn built(&self) -> &BuiltRecord {
&self.built
}
pub(crate) fn published(&self) -> Result<()> {
ensure!(
self.state == State::Published,
"UPDATE_RECORD_PUBLICATION_REQUIRED"
);
self.verify()
}
}