//! Exact connection-205 administrative closure. Neither a lease nor job authority. use crate::{ checkpoint_repair::{SignedDecision, inventory_sha256}, jobs::hash, windows_echo_probe::{AGENT, ASSET, TENANT, TEST_PRINCIPAL}, }; use chrono::{DateTime, Duration, Utc}; use serde::{Deserialize, Serialize}; use uuid::Uuid; pub const DOMAIN: &str = "rmm.dev.windows-sync-admin-closed-alignment.v1"; pub const FIRST: Uuid = Uuid::from_u128(0x1a4758ad0b39449e855116846851b09a); pub const SECOND: Uuid = Uuid::from_u128(0x2af359c55b5c4992b4451c00e271a288); pub const FIRST_REQUEST: Uuid = Uuid::from_u128(0x1f55d56f65b74e81bf8889c6765fe7a6); pub const SECOND_REQUEST: Uuid = Uuid::from_u128(0xed99c35576214846b8d03f534096eed4); pub const BOOT: Uuid = Uuid::from_u128(0xa9ad08955a968f2c8fb6cfdc2d5225ea); pub const ECHO_JOB: Uuid = Uuid::from_u128(0x645ad11b9ecd4efe8f928ad2fb6ed5cf); pub const ECHO_ATTEMPT: Uuid = Uuid::from_u128(0xe2ee25cd0e234c3caaa23048d232622a); pub const EXPIRED_JOB: Uuid = Uuid::from_u128(0x0a50aae9a7844535b62fef24c301d546); pub const EXPIRED_ATTEMPT: Uuid = Uuid::from_u128(0x78295f744565459a87b0641a8b95feec); pub const MAX_WITNESS_BYTES: usize = 768 * 1024; pub const FILE_ROLES: [&str; 6] = [ "telemetry", "consumption", "agent_lock", "helper_ledger", "helper_lock", "system_intents", ]; #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] #[serde(deny_unknown_fields)] pub struct NativeFile { pub role: String, /// Independently measured BY_HANDLE_FILE_INFORMATION volume/high/low tuple. pub file_id: Option<[u32; 3]>, pub sha256: Option, } #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] #[serde(deny_unknown_fields)] pub struct Counts { pub agent_jobs: u64, pub agent_consumed: u64, pub agent_starts: u64, pub agent_outcomes: u64, pub helper_consumed: u64, pub helper_starts: u64, pub helper_outcomes: u64, pub system_intents: u64, } #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] #[serde(deny_unknown_fields)] pub struct Witness { pub schema_version: u32, pub tenant_id: Uuid, pub asset_id: Uuid, pub agent_id: Uuid, pub enrollment_generation: u64, pub assignment_version: u64, pub boot_id: Uuid, pub connection_generation: u64, pub agent_journal_id: Uuid, pub helper_journal_id: Uuid, pub agent_checkpoint: u64, pub helper_checkpoint: u64, pub agent_last_decision: Uuid, pub helper_last_decision: Uuid, pub verification_key_id: String, pub verification_public_key_hex: String, pub binding_sha256: String, pub helper_binding_sha256: String, pub files: [NativeFile; 6], pub agent_inventory_sha256: String, pub consumption_inventory_sha256: String, pub helper_inventory_sha256: String, pub counts: Counts, pub original_requests: [Vec; 2], pub original_responses: [SignedDecision; 2], pub echo_offer: Vec, pub echo_start: Vec, pub echo_outcome: Vec, pub echo_result: Vec, pub echo_completion_receipt: Vec, pub expired_offer: Vec, } impl Witness { pub fn digest(&self) -> Option { inventory_sha256(&serde_json::to_value(self).ok()?).ok() } pub fn valid(&self) -> bool { let digest = crate::agent_update::digest; self.schema_version == 1 && (self.tenant_id, self.asset_id, self.agent_id) == (TENANT, ASSET, AGENT) && self.enrollment_generation == 1 && self.assignment_version == 1 && self.boot_id == BOOT && self.connection_generation == 205 && !self.agent_journal_id.is_nil() && !self.helper_journal_id.is_nil() && self.agent_journal_id != self.helper_journal_id && self.agent_checkpoint == 10137 && self.helper_checkpoint >= 148 && self.agent_last_decision == SECOND && self.helper_last_decision == FIRST && [ &self.verification_key_id, &self.verification_public_key_hex, &self.binding_sha256, &self.helper_binding_sha256, &self.agent_inventory_sha256, &self.consumption_inventory_sha256, &self.helper_inventory_sha256, ] .iter() .all(|h| digest(h)) && self.binding_sha256 == self.helper_binding_sha256 && hex::decode(&self.verification_public_key_hex) .is_ok_and(|k| hash(&k) == self.verification_key_id) && self.files.iter().zip(FILE_ROLES).all(|(f, role)| { f.role == role && match (&f.file_id, &f.sha256) { (Some(id), Some(h)) => *id != [0; 3] && digest(h), (None, None) => role == "system_intents", _ => false, } }) && self.counts == (Counts { agent_jobs: 2, agent_consumed: 1, agent_starts: 1, agent_outcomes: 1, helper_consumed: 0, helper_starts: 0, helper_outcomes: 0, system_intents: 0, }) && self .original_requests .iter() .all(|v| !v.is_empty() && v.len() <= 65536) && self.original_responses.iter().all(|v| { !v.decision_bytes.is_empty() && v.decision_bytes.len() <= 65536 && v.signature.len() == 64 }) && [ &self.echo_offer, &self.echo_start, &self.echo_outcome, &self.echo_result, &self.echo_completion_receipt, &self.expired_offer, ] .iter() .all(|v| !v.is_empty() && v.len() <= 131072) && serde_json::to_vec(self).is_ok_and(|v| v.len() <= MAX_WITNESS_BYTES) } } #[derive(Clone, Debug, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct Receipt { pub domain: String, pub schema_version: u32, pub receipt_id: Uuid, pub request_id: Uuid, pub witness: Witness, pub witness_sha256: String, pub key_id: String, pub issued_at: DateTime, pub valid_until: DateTime, pub confirmed_by: String, pub approval_origin: String, pub testsimulation: bool, pub ready: bool, pub helper_ready: bool, pub unlock: bool, } impl Receipt { pub fn valid(&self, now: DateTime) -> bool { self.domain == DOMAIN && self.schema_version == 1 && !self.receipt_id.is_nil() && !self.request_id.is_nil() && self.receipt_id != self.request_id && self.witness.valid() && self.witness.digest().as_deref() == Some(self.witness_sha256.as_str()) && self.key_id == self.witness.verification_key_id && self.confirmed_by == TEST_PRINCIPAL && self.approval_origin == "dev_test_browser_simulation" && self.testsimulation && !self.ready && !self.helper_ready && !self.unlock && self.issued_at <= now && now < self.valid_until && self.valid_until > self.issued_at && self.valid_until - self.issued_at <= Duration::minutes(5) } } #[cfg(test)] mod tests { use super::*; // Codec/profile fixture only: cryptographic original validation is independent. fn witness() -> Witness { Witness { schema_version: 1, tenant_id: TENANT, asset_id: ASSET, agent_id: AGENT, enrollment_generation: 1, assignment_version: 1, boot_id: BOOT, connection_generation: 205, agent_journal_id: Uuid::from_u128(1), helper_journal_id: Uuid::from_u128(2), agent_checkpoint: 10137, helper_checkpoint: 155, agent_last_decision: SECOND, helper_last_decision: FIRST, verification_key_id: hash(&[0; 32]), verification_public_key_hex: "00".repeat(32), binding_sha256: "ab".repeat(32), helper_binding_sha256: "ab".repeat(32), files: std::array::from_fn(|i| NativeFile { role: FILE_ROLES[i].into(), file_id: Some([1, i as u32 + 1, 1]), sha256: Some("cd".repeat(32)), }), agent_inventory_sha256: "ef".repeat(32), consumption_inventory_sha256: "12".repeat(32), helper_inventory_sha256: "34".repeat(32), counts: Counts { agent_jobs: 2, agent_consumed: 1, agent_starts: 1, agent_outcomes: 1, helper_consumed: 0, helper_starts: 0, helper_outcomes: 0, system_intents: 0, }, original_requests: [vec![1], vec![2]], original_responses: std::array::from_fn(|_| SignedDecision { decision_bytes: vec![1], signature: vec![0; 64], }), echo_offer: vec![1], echo_start: vec![1], echo_outcome: vec![1], echo_result: vec![1], echo_completion_receipt: vec![1], expired_offer: vec![1], } } #[test] fn closed_profile_never_accepts_new_consumption_stale_capture_or_other_incident() { let w = witness(); assert!(w.valid()); for mutation in 0..6 { let mut bad = w.clone(); match mutation { 0 => bad.connection_generation = 150, 1 => bad.helper_checkpoint = 147, 2 => bad.counts.helper_consumed = 1, 3 => bad.counts.agent_jobs = 3, 4 => bad.agent_last_decision = FIRST, _ => bad.files[0].file_id = None, }; assert!(!bad.valid()); } } #[test] fn receipt_is_exactly_bounded_and_cannot_be_readiness_or_real_human_approval() { let w = witness(); let now = Utc::now(); let r = Receipt { domain: DOMAIN.into(), schema_version: 1, receipt_id: Uuid::from_u128(3), request_id: Uuid::from_u128(4), witness_sha256: w.digest().unwrap(), key_id: w.verification_key_id.clone(), witness: w, issued_at: now, valid_until: now + Duration::minutes(5), confirmed_by: TEST_PRINCIPAL.into(), approval_origin: "dev_test_browser_simulation".into(), testsimulation: true, ready: false, helper_ready: false, unlock: false, }; assert!(r.valid(now)); assert!(!r.valid(r.valid_until)); assert!(!r.valid(now - Duration::nanoseconds(1))); for mutation in 0..7 { let mut bad = r.clone(); match mutation { 0 => bad.ready = true, 1 => bad.helper_ready = true, 2 => bad.unlock = true, 3 => bad.testsimulation = false, 4 => bad.valid_until += Duration::nanoseconds(1), 5 => bad.confirmed_by = "Administrator".into(), _ => bad.witness.helper_checkpoint += 1, }; assert!(!bad.valid(now)); } } }