705 lines
27 KiB
Rust
705 lines
27 KiB
Rust
//! Actual protected local policy/keyring/health/baseline verification. No key
|
|
//! returned in a job/manifest is promoted to trust and no floor is guessed.
|
|
use crate::{clock::NativeClock, original::Original};
|
|
use anyhow::{Context, Result, ensure};
|
|
use ed25519_dalek::{Signature, VerifyingKey};
|
|
use rmm_agent_core::{auth::Binding, reconcile::SyncClock};
|
|
use rmm_domain::{
|
|
agent_update::{Platform, UpdateKeyringFile},
|
|
agent_update_provisioning::{
|
|
ActivationEvidence, BASELINE_SIGNATURE_DOMAIN, HealthKeyPin, InstalledReleaseRecord,
|
|
LocalUpdatePolicy,
|
|
},
|
|
jobs::hash,
|
|
};
|
|
use rmm_updater::{
|
|
ActiveRelease, Platform as ReleasePlatform, SignedRelease, TrustedContext, TrustedUpdateKey,
|
|
UpdateKeyring, verify_release,
|
|
};
|
|
use rmm_updater_linux::files::{Directory, HeldFile};
|
|
use std::{ffi::OsStr, path::Path};
|
|
|
|
struct Protected {
|
|
dir: Directory,
|
|
file: HeldFile,
|
|
name: String,
|
|
max: u64,
|
|
}
|
|
impl Protected {
|
|
fn read(path: &Path, max: u64) -> Result<(Self, Vec<u8>)> {
|
|
let dir = Directory::open(path.parent().context("UPDATE_TRUST_PARENT")?, 0, false)?;
|
|
let name = path
|
|
.file_name()
|
|
.context("UPDATE_TRUST_NAME")?
|
|
.to_str()
|
|
.context("UPDATE_TRUST_NAME")?
|
|
.to_owned();
|
|
let mut file = dir.inspect(OsStr::new(&name), 0, max)?;
|
|
let raw = file.bytes(max)?;
|
|
Ok((
|
|
Self {
|
|
dir,
|
|
file,
|
|
name,
|
|
max,
|
|
},
|
|
raw,
|
|
))
|
|
}
|
|
fn verify(&self) -> Result<()> {
|
|
self.dir
|
|
.file(OsStr::new(&self.name), self.file.proof(), self.max)?;
|
|
Ok(())
|
|
}
|
|
}
|
|
pub struct TrustState {
|
|
updater_binding: rmm_domain::agent_update::UpdaterBinding,
|
|
predecessor: rmm_domain::agent_update::Predecessor,
|
|
protected: Vec<Protected>,
|
|
policy: LocalUpdatePolicy,
|
|
installed: InstalledReleaseRecord,
|
|
installed_raw: Vec<u8>,
|
|
keyring: UpdateKeyring,
|
|
keyring_file: UpdateKeyringFile,
|
|
baseline_signer_revoked: bool,
|
|
health_public_key: [u8; 32],
|
|
}
|
|
impl TrustState {
|
|
pub fn load(original: &Original, binding: &Binding, clock: &NativeClock) -> Result<Self> {
|
|
let result = Self::load_retained(original, binding)?;
|
|
result.recheck(original, clock)?;
|
|
Ok(result)
|
|
}
|
|
/// EXACT ORIGINAL evidence only. The separate historical keyring below
|
|
/// never supplies current admission, Health, Finalize or effect authority.
|
|
pub fn load_retained(original: &Original, binding: &Binding) -> Result<Self> {
|
|
Self::load_inputs(
|
|
Some(original.update()),
|
|
binding,
|
|
original.delegation().prepared_at,
|
|
)
|
|
}
|
|
/// Explicit PRE-consumption verification. No Original/start/consumption is
|
|
/// fabricated to reuse a retained-evidence API. Caller still needs fresh
|
|
/// independent HelperSync admission and atomic durable handoff afterwards.
|
|
pub fn load_prepared(
|
|
update: &rmm_domain::agent_update::ResolvedAgentUpdate,
|
|
binding: &Binding,
|
|
clock: &NativeClock,
|
|
) -> Result<Self> {
|
|
ensure!(
|
|
update.validate() && update.execution_identity() == Some("root"),
|
|
"UPDATE_PREPARED_ACTION"
|
|
);
|
|
let state = Self::load_inputs(Some(update), binding, clock.now()?)?;
|
|
state.recheck_prepared(update, clock)?;
|
|
Ok(state)
|
|
}
|
|
pub fn load_observed(binding: &Binding, clock: &NativeClock) -> Result<Self> {
|
|
let state = Self::load_inputs(None, binding, clock.now()?)?;
|
|
state.recheck_observed()?;
|
|
Ok(state)
|
|
}
|
|
pub fn recheck_observed(&self) -> Result<()> {
|
|
self.recheck_protected()?;
|
|
ensure!(
|
|
!self.baseline_signer_revoked,
|
|
"UPDATE_BASELINE_SIGNER_REVOKED"
|
|
);
|
|
verify_installed_release(
|
|
&self.keyring,
|
|
&self.installed,
|
|
self.updater_binding.keyring_generation,
|
|
)
|
|
}
|
|
pub fn updater_binding(&self) -> &rmm_domain::agent_update::UpdaterBinding {
|
|
&self.updater_binding
|
|
}
|
|
pub fn predecessor(&self) -> &rmm_domain::agent_update::Predecessor {
|
|
&self.predecessor
|
|
}
|
|
pub fn activation_floor(&self) -> u64 {
|
|
self.installed.activation_floor
|
|
}
|
|
pub fn verify_candidate(
|
|
&self,
|
|
envelope: &rmm_domain::agent_update::ReleaseEnvelope,
|
|
clock: &NativeClock,
|
|
) -> Result<()> {
|
|
self.recheck_observed()?;
|
|
let active = self
|
|
.installed
|
|
.release_envelope
|
|
.decode()
|
|
.map_err(anyhow::Error::msg)?;
|
|
verify(
|
|
&self.keyring,
|
|
envelope,
|
|
&TrustedContext {
|
|
platform: ReleasePlatform::LinuxX64,
|
|
now_utc_seconds: u64::try_from(clock.now()?.timestamp())?,
|
|
keyring_generation: self.updater_binding.keyring_generation,
|
|
protocol_major: active.manifest.protocol_major,
|
|
state_schema: active.manifest.state_schema,
|
|
updater_version: 1,
|
|
minimum_release_sequence: self.installed.activation_floor,
|
|
active_release: Some(ActiveRelease {
|
|
sequence: active.manifest.release_sequence,
|
|
manifest_sha256: self.installed.manifest_sha256.clone(),
|
|
}),
|
|
},
|
|
)
|
|
}
|
|
fn load_inputs(
|
|
update: Option<&rmm_domain::agent_update::ResolvedAgentUpdate>,
|
|
binding: &Binding,
|
|
prepared_at: chrono::DateTime<chrono::Utc>,
|
|
) -> Result<Self> {
|
|
let mut protected = Vec::new();
|
|
let (file, raw) = Protected::read(Path::new("/etc/rmm-agent-updater/policy.json"), 65536)?;
|
|
let (policy, policy_hash) =
|
|
LocalUpdatePolicy::from_bytes(&raw).map_err(anyhow::Error::msg)?;
|
|
ensure!(
|
|
policy.tenant_id == binding.tenant_id
|
|
&& policy.asset_id == binding.asset_id
|
|
&& policy.agent_id == binding.agent_id
|
|
&& policy.enrollment_generation == binding.enrollment_generation
|
|
&& policy.assignment_version == binding.assignment_version,
|
|
"UPDATE_PROTECTED_POLICY_CHANGED"
|
|
);
|
|
protected.push(file);
|
|
let (file, raw) = Protected::read(Path::new(&policy.registry_path), 65536)?;
|
|
ensure!(
|
|
hash(&raw) == policy.reader_registry_sha256,
|
|
"UPDATE_PROTECTED_READER_REGISTRY_CHANGED"
|
|
);
|
|
protected.push(file);
|
|
let (file, raw) = Protected::read(Path::new(&policy.keyring_path), 16384)?;
|
|
let keys: UpdateKeyringFile = serde_json::from_slice(&raw)?;
|
|
ensure!(keys.validate(), "UPDATE_PROTECTED_KEYRING_CHANGED");
|
|
let keyring_sha256 = hash(&raw);
|
|
protected.push(file);
|
|
let job_key = hex::decode(&binding.public_key_hex)?;
|
|
let mut trusted = Vec::new();
|
|
let mut historical_keys = Vec::new();
|
|
let mut baseline_key = None;
|
|
for entry in &keys.keys {
|
|
ensure!(entry.platform == Platform::LinuxX64, "UPDATE_KEY_PLATFORM");
|
|
let public: [u8; 32] = hex::decode(&entry.public_key_hex)?
|
|
.try_into()
|
|
.map_err(|_| anyhow::anyhow!("UPDATE_KEY_SIZE"))?;
|
|
ensure!(
|
|
public.as_slice() != job_key,
|
|
"UPDATE_JOB_RELEASE_KEYS_NOT_SEPARATE"
|
|
);
|
|
if hash(&public) == policy.initial_baseline.key_id {
|
|
baseline_key = Some((entry, public));
|
|
}
|
|
trusted.push(TrustedUpdateKey::new(
|
|
public,
|
|
ReleasePlatform::LinuxX64,
|
|
entry.first_sequence,
|
|
entry.last_sequence,
|
|
entry.revoked,
|
|
)?);
|
|
historical_keys.push(TrustedUpdateKey::new(
|
|
public,
|
|
ReleasePlatform::LinuxX64,
|
|
entry.first_sequence,
|
|
entry.last_sequence,
|
|
false,
|
|
)?);
|
|
}
|
|
let keyring = UpdateKeyring::new(keys.generation, trusted)?;
|
|
let historical = UpdateKeyring::new(keys.generation, historical_keys)?;
|
|
let baseline = policy
|
|
.initial_baseline
|
|
.decode()
|
|
.map_err(anyhow::Error::msg)?;
|
|
let (baseline_entry, public) = baseline_key.context("UPDATE_BASELINE_KEY_NOT_TRUSTED")?;
|
|
ensure!(
|
|
baseline
|
|
.record
|
|
.release_envelope
|
|
.decode()
|
|
.map_err(anyhow::Error::msg)?
|
|
.manifest
|
|
.release_sequence
|
|
>= baseline_entry.first_sequence
|
|
&& baseline
|
|
.record
|
|
.release_envelope
|
|
.decode()
|
|
.map_err(anyhow::Error::msg)?
|
|
.manifest
|
|
.release_sequence
|
|
<= baseline_entry.last_sequence,
|
|
"UPDATE_BASELINE_KEY_REVOKED_OR_RANGE"
|
|
);
|
|
let public = VerifyingKey::from_bytes(&public)?;
|
|
ensure!(!public.is_weak(), "UPDATE_BASELINE_WEAK_KEY");
|
|
let mut message = BASELINE_SIGNATURE_DOMAIN.to_vec();
|
|
message.extend_from_slice(&baseline.payload_bytes);
|
|
public.verify_strict(&message, &Signature::from_bytes(&baseline.signature))?;
|
|
let (file, raw) = Protected::read(Path::new(&policy.health_pin_path), 4096)?;
|
|
let health: HealthKeyPin = serde_json::from_slice(&raw)?;
|
|
let health_public_key = health.public_key().map_err(anyhow::Error::msg)?;
|
|
ensure!(
|
|
!VerifyingKey::from_bytes(&health_public_key)?.is_weak()
|
|
&& health_public_key.as_slice() != job_key
|
|
&& keys
|
|
.keys
|
|
.iter()
|
|
.all(|key| key.public_key_hex != hex::encode(health_public_key)),
|
|
"UPDATE_HEALTH_KEY_NOT_SEPARATE_OR_CHANGED"
|
|
);
|
|
let health_key_pin_sha256 = hash(&raw);
|
|
protected.push(file);
|
|
let (file, raw) = Protected::read(Path::new(&policy.installed_record_path), 32768)?;
|
|
let installed = InstalledReleaseRecord::from_bytes(&raw).map_err(anyhow::Error::msg)?;
|
|
ensure!(
|
|
!installed.rolled_back
|
|
&& installed.activation_floor >= policy.initial_activation_floor
|
|
&& installed.activated_at <= u64::try_from(prepared_at.timestamp())?,
|
|
"UPDATE_INSTALLED_FLOOR_OR_TIME"
|
|
);
|
|
if matches!(
|
|
installed.activation_evidence,
|
|
ActivationEvidence::Bootstrap { .. }
|
|
) {
|
|
ensure!(
|
|
raw == baseline.payload_bytes
|
|
&& hash(&raw) == policy.initial_record_sha256
|
|
&& installed.activation_floor == policy.initial_activation_floor,
|
|
"UPDATE_INSTALLED_BASELINE_CHANGED"
|
|
);
|
|
}
|
|
let active = installed
|
|
.release_envelope
|
|
.decode()
|
|
.map_err(anyhow::Error::msg)?;
|
|
let predecessor = rmm_domain::agent_update::Predecessor {
|
|
release_id: active.manifest.release_id,
|
|
release_sequence: active.manifest.release_sequence,
|
|
manifest_sha256: installed.manifest_sha256.clone(),
|
|
installed_record_sha256: hash(&raw),
|
|
agent_sha256: active.manifest.artifacts[0].sha256.clone(),
|
|
helper_sha256: active.manifest.artifacts[1].sha256.clone(),
|
|
};
|
|
let updater_binding = rmm_domain::agent_update::UpdaterBinding {
|
|
binary_sha256: policy.updater_binary_sha256.clone(),
|
|
policy_sha256: policy_hash,
|
|
keyring_generation: keys.generation,
|
|
keyring_sha256,
|
|
health_public_key_hex: hex::encode(health_public_key),
|
|
health_key_pin_sha256,
|
|
};
|
|
if let Some(update) = update {
|
|
ensure!(
|
|
update.predecessor == predecessor && update.updater == updater_binding,
|
|
"UPDATE_PROTECTED_INPUT_BINDING"
|
|
);
|
|
}
|
|
verify_installed_release(&historical, &installed, keys.generation)?;
|
|
if let Some(update) = update {
|
|
verify(
|
|
&historical,
|
|
&update.release_envelope,
|
|
&TrustedContext {
|
|
platform: ReleasePlatform::LinuxX64,
|
|
now_utc_seconds: u64::try_from(prepared_at.timestamp())?,
|
|
keyring_generation: keys.generation,
|
|
protocol_major: active.manifest.protocol_major,
|
|
state_schema: active.manifest.state_schema,
|
|
updater_version: 1,
|
|
minimum_release_sequence: installed.activation_floor,
|
|
active_release: Some(ActiveRelease {
|
|
sequence: active.manifest.release_sequence,
|
|
manifest_sha256: installed.manifest_sha256.clone(),
|
|
}),
|
|
},
|
|
)?;
|
|
}
|
|
protected.push(file);
|
|
let baseline_signer_revoked = baseline_entry.revoked;
|
|
Ok(Self {
|
|
updater_binding,
|
|
predecessor,
|
|
protected,
|
|
policy,
|
|
installed,
|
|
installed_raw: raw,
|
|
keyring,
|
|
keyring_file: keys,
|
|
baseline_signer_revoked,
|
|
health_public_key,
|
|
})
|
|
}
|
|
pub fn recheck(&self, original: &Original, clock: &NativeClock) -> Result<()> {
|
|
self.recheck_original_binding(original)?;
|
|
self.recheck_protected()?;
|
|
self.recheck_crypto(original, clock)
|
|
}
|
|
fn recheck_crypto(&self, original: &Original, clock: &NativeClock) -> Result<()> {
|
|
self.recheck_crypto_inputs(original.update(), clock)
|
|
}
|
|
pub fn recheck_prepared(
|
|
&self,
|
|
update: &rmm_domain::agent_update::ResolvedAgentUpdate,
|
|
clock: &NativeClock,
|
|
) -> Result<()> {
|
|
ensure!(
|
|
update.updater == self.updater_binding && update.predecessor == self.predecessor,
|
|
"UPDATE_PREPARED_CONTEXT_CHANGED"
|
|
);
|
|
self.recheck_protected()?;
|
|
self.recheck_crypto_inputs(update, clock)
|
|
}
|
|
fn recheck_crypto_inputs(
|
|
&self,
|
|
update: &rmm_domain::agent_update::ResolvedAgentUpdate,
|
|
clock: &NativeClock,
|
|
) -> Result<()> {
|
|
ensure!(
|
|
!self.baseline_signer_revoked,
|
|
"UPDATE_BASELINE_SIGNER_REVOKED"
|
|
);
|
|
verify_installed_release(
|
|
&self.keyring,
|
|
&self.installed,
|
|
update.updater.keyring_generation,
|
|
)?;
|
|
let active = self
|
|
.installed
|
|
.release_envelope
|
|
.decode()
|
|
.map_err(anyhow::Error::msg)?;
|
|
verify(
|
|
&self.keyring,
|
|
&update.release_envelope,
|
|
&TrustedContext {
|
|
platform: ReleasePlatform::LinuxX64,
|
|
now_utc_seconds: u64::try_from(clock.now()?.timestamp())?,
|
|
keyring_generation: update.updater.keyring_generation,
|
|
protocol_major: active.manifest.protocol_major,
|
|
state_schema: active.manifest.state_schema,
|
|
updater_version: 1,
|
|
minimum_release_sequence: self.installed.activation_floor,
|
|
active_release: Some(ActiveRelease {
|
|
sequence: active.manifest.release_sequence,
|
|
manifest_sha256: self.installed.manifest_sha256.clone(),
|
|
}),
|
|
},
|
|
)
|
|
}
|
|
pub fn installed_raw(&self) -> &[u8] {
|
|
&self.installed_raw
|
|
}
|
|
/// Exact typed active-record namespace only. Every other protected name
|
|
/// remains mandatory; neither caller booleans nor path wildcards can skip it.
|
|
pub fn recheck_record_namespace(
|
|
&self,
|
|
original: &Original,
|
|
clock: &NativeClock,
|
|
namespace: &crate::record::RecordNamespaceProof<'_>,
|
|
) -> Result<()> {
|
|
self.recheck_original_binding(original)?;
|
|
self.recheck_protected_record_namespace(original, namespace)?;
|
|
self.recheck_crypto(original, clock)
|
|
}
|
|
pub fn recheck_protected_record_namespace(
|
|
&self,
|
|
original: &Original,
|
|
namespace: &crate::record::RecordNamespaceProof<'_>,
|
|
) -> Result<()> {
|
|
self.recheck_original_binding(original)?;
|
|
namespace.verify()?;
|
|
ensure!(
|
|
namespace.grant_id() == original.permit().grant_id
|
|
&& namespace.owner_nonce() == original.handoff().owner_nonce
|
|
&& namespace.old_sha256() == original.handoff().predecessor_record_sha256
|
|
&& namespace.old_sha256() == hash(&self.installed_raw),
|
|
"UPDATE_RECORD_NAMESPACE_ORIGINAL_BINDING"
|
|
);
|
|
let mut matched = 0;
|
|
for protected in &self.protected {
|
|
if protected.name == "installed-record.json"
|
|
&& protected.dir.identity() == namespace.directory_identity()
|
|
{
|
|
ensure!(
|
|
protected.file.proof() == namespace.old_file(),
|
|
"UPDATE_RECORD_ORIGINAL_HELD_IDENTITY_CHANGED"
|
|
);
|
|
matched += 1;
|
|
} else {
|
|
protected.verify()?;
|
|
}
|
|
}
|
|
ensure!(matched == 1, "UPDATE_RECORD_EXACT_PROTECTED_NAME_REQUIRED");
|
|
namespace.verify()
|
|
}
|
|
pub fn recheck_protected(&self) -> Result<()> {
|
|
for file in &self.protected {
|
|
file.verify()?;
|
|
}
|
|
Ok(())
|
|
}
|
|
pub fn recheck_original_binding(&self, original: &Original) -> Result<()> {
|
|
ensure!(
|
|
original.update().updater == self.updater_binding
|
|
&& original.update().predecessor == self.predecessor,
|
|
"UPDATE_TRUST_ORIGINAL_CONTEXT_CHANGED"
|
|
);
|
|
Ok(())
|
|
}
|
|
/// Full admission/Stop claims call this under THEIR actual guard. The
|
|
/// updater repeats it after all readers stop and before the first swap.
|
|
/// Never gate Unknown-only adjudication on this admission-oriented check.
|
|
pub fn qualify_history(
|
|
&self,
|
|
db: &rusqlite::Connection,
|
|
guard: &impl rmm_agent_core::helper_reconcile::ExecutorLedgerGuard,
|
|
binding: &Binding,
|
|
) -> Result<()> {
|
|
self.recheck_protected()?;
|
|
let baseline = self
|
|
.policy
|
|
.initial_baseline
|
|
.decode()
|
|
.map_err(anyhow::Error::msg)?;
|
|
let baseline_hash = hash(&baseline.payload_bytes);
|
|
let mut records = Vec::new();
|
|
let mut held = Vec::new();
|
|
let mut seen = std::collections::BTreeSet::new();
|
|
let mut next = self.installed.previous_record_sha256.clone();
|
|
let mut total = self.installed_raw.len() + baseline.payload_bytes.len();
|
|
while let Some(digest) = next {
|
|
ensure!(
|
|
rmm_domain::agent_update::digest(&digest)
|
|
&& seen.insert(digest.clone())
|
|
&& seen.len() < 64,
|
|
"UPDATE_RECORD_ARCHIVE_CHAIN"
|
|
);
|
|
if digest == baseline_hash {
|
|
break;
|
|
}
|
|
let (file, raw) = Protected::read(
|
|
&Path::new("/var/lib/rmm-agent-updater/records").join(format!("{digest}.json")),
|
|
32768,
|
|
)?;
|
|
ensure!(hash(&raw) == digest, "UPDATE_RECORD_ARCHIVE_HASH_CHANGED");
|
|
total = total
|
|
.checked_add(raw.len())
|
|
.context("UPDATE_RECORD_ARCHIVE_SIZE")?;
|
|
ensure!(total <= 2 * 1024 * 1024, "UPDATE_RECORD_ARCHIVE_SIZE");
|
|
let record = InstalledReleaseRecord::from_bytes(&raw).map_err(anyhow::Error::msg)?;
|
|
next = record.previous_record_sha256.clone();
|
|
records.push(raw);
|
|
held.push(file);
|
|
}
|
|
let record = crate::history::verify_history(
|
|
db,
|
|
guard,
|
|
binding,
|
|
&self.keyring_file,
|
|
&self.policy.initial_baseline,
|
|
&records,
|
|
&self.installed_raw,
|
|
)?;
|
|
ensure!(
|
|
record.bytes().map_err(anyhow::Error::msg)? == self.installed_raw,
|
|
"UPDATE_HISTORY_ACTIVE_CHANGED"
|
|
);
|
|
for file in &held {
|
|
file.verify()?;
|
|
}
|
|
self.recheck_protected()
|
|
}
|
|
pub fn policy(&self) -> &LocalUpdatePolicy {
|
|
&self.policy
|
|
}
|
|
pub fn health_public_key(&self) -> [u8; 32] {
|
|
self.health_public_key
|
|
}
|
|
}
|
|
fn verify(
|
|
keyring: &UpdateKeyring,
|
|
envelope: &rmm_domain::agent_update::ReleaseEnvelope,
|
|
context: &TrustedContext,
|
|
) -> Result<()> {
|
|
let decoded = envelope.decode().map_err(anyhow::Error::msg)?;
|
|
verify_release(
|
|
SignedRelease {
|
|
schema_version: envelope.schema_version,
|
|
key_id: &envelope.key_id,
|
|
payload_bytes: &decoded.payload_bytes,
|
|
signature: &decoded.signature,
|
|
},
|
|
keyring,
|
|
context,
|
|
)?;
|
|
Ok(())
|
|
}
|
|
|
|
fn verify_installed_release(
|
|
keyring: &UpdateKeyring,
|
|
installed: &InstalledReleaseRecord,
|
|
generation: u64,
|
|
) -> Result<()> {
|
|
let active = installed
|
|
.release_envelope
|
|
.decode()
|
|
.map_err(anyhow::Error::msg)?;
|
|
verify(
|
|
keyring,
|
|
&installed.release_envelope,
|
|
&TrustedContext {
|
|
platform: ReleasePlatform::LinuxX64,
|
|
now_utc_seconds: installed.activated_at,
|
|
keyring_generation: generation,
|
|
protocol_major: active.manifest.protocol_major,
|
|
state_schema: active.manifest.state_schema,
|
|
updater_version: 1,
|
|
minimum_release_sequence: installed.activation_floor,
|
|
active_release: Some(ActiveRelease {
|
|
sequence: active.manifest.release_sequence,
|
|
manifest_sha256: installed.manifest_sha256.clone(),
|
|
}),
|
|
},
|
|
)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD};
|
|
use ed25519_dalek::{Signer, SigningKey};
|
|
use rmm_domain::agent_update::{
|
|
ArtifactDescriptor, ArtifactRole, ReleaseEnvelope, ReleaseManifest,
|
|
};
|
|
use rmm_domain::agent_update_provisioning::SignedBaselineRecord;
|
|
use uuid::Uuid;
|
|
|
|
fn release(sequence: u64, published: u64, expires: u64) -> ReleaseEnvelope {
|
|
let signer = SigningKey::from_bytes(&[83; 32]); // public TEST fixture only
|
|
let manifest = ReleaseManifest {
|
|
schema_version: 1,
|
|
release_id: Uuid::from_u128(u128::from(sequence)),
|
|
release_sequence: sequence,
|
|
version: format!("test-{sequence}"),
|
|
platform: Platform::LinuxX64,
|
|
published_at: published,
|
|
expires_at: expires,
|
|
protocol_major: 1,
|
|
state_schema: 1,
|
|
minimum_updater_version: 1,
|
|
artifacts: [
|
|
ArtifactDescriptor {
|
|
role: ArtifactRole::Agent,
|
|
artifact_id: Uuid::from_u128(101),
|
|
sha256: hash(b"test agent"),
|
|
bytes: 10,
|
|
},
|
|
ArtifactDescriptor {
|
|
role: ArtifactRole::Helper,
|
|
artifact_id: Uuid::from_u128(102),
|
|
sha256: hash(b"test helper"),
|
|
bytes: 11,
|
|
},
|
|
],
|
|
};
|
|
let raw = serde_json::to_vec(&manifest).unwrap();
|
|
let mut signed = rmm_updater::RELEASE_SIGNATURE_DOMAIN.to_vec();
|
|
signed.extend_from_slice(&raw);
|
|
ReleaseEnvelope {
|
|
schema_version: 1,
|
|
key_id: hash(signer.verifying_key().as_bytes()),
|
|
payload_bytes: URL_SAFE_NO_PAD.encode(raw),
|
|
signature: URL_SAFE_NO_PAD.encode(signer.sign(&signed).to_bytes()),
|
|
}
|
|
}
|
|
#[test]
|
|
fn separately_signed_baseline_at_floor_is_historical_only_and_candidate_stays_strict() {
|
|
let signer = SigningKey::from_bytes(&[83; 32]);
|
|
let keyring = UpdateKeyring::new(
|
|
7,
|
|
vec![
|
|
TrustedUpdateKey::new(
|
|
signer.verifying_key().to_bytes(),
|
|
ReleasePlatform::LinuxX64,
|
|
1,
|
|
20,
|
|
false,
|
|
)
|
|
.unwrap(),
|
|
],
|
|
)
|
|
.unwrap();
|
|
let envelope = release(7, 90, 110);
|
|
let record = InstalledReleaseRecord {
|
|
schema_version: 1,
|
|
profile: "decoder_compatible_handoff_v1".into(),
|
|
platform: Platform::LinuxX64,
|
|
manifest_sha256: hash(&envelope.decode().unwrap().payload_bytes),
|
|
release_envelope: envelope,
|
|
activation_floor: 7,
|
|
activated_at: 95,
|
|
previous_record_sha256: None,
|
|
activation_evidence: ActivationEvidence::Bootstrap {
|
|
qualification_receipt_sha256: hash(b"local synthetic qualification receipt"),
|
|
},
|
|
rolled_back: false,
|
|
};
|
|
let raw = record.bytes().unwrap();
|
|
let mut message = BASELINE_SIGNATURE_DOMAIN.to_vec();
|
|
message.extend_from_slice(&raw);
|
|
let baseline = SignedBaselineRecord {
|
|
schema_version: 1,
|
|
key_id: hash(signer.verifying_key().as_bytes()),
|
|
payload_bytes: URL_SAFE_NO_PAD.encode(&raw),
|
|
signature: URL_SAFE_NO_PAD.encode(signer.sign(&message).to_bytes()),
|
|
};
|
|
let decoded = baseline.decode().unwrap();
|
|
signer
|
|
.verifying_key()
|
|
.verify_strict(&message, &Signature::from_bytes(&decoded.signature))
|
|
.unwrap();
|
|
assert_eq!(decoded.payload_bytes, raw);
|
|
// Exercise the ACTUAL production context builder, where None at the
|
|
// confirmed floor previously rejected every legitimate initial record.
|
|
verify_installed_release(&keyring, &decoded.record, 7).unwrap();
|
|
let candidate_context = TrustedContext {
|
|
platform: ReleasePlatform::LinuxX64,
|
|
now_utc_seconds: 200,
|
|
keyring_generation: 7,
|
|
protocol_major: 1,
|
|
state_schema: 1,
|
|
updater_version: 1,
|
|
minimum_release_sequence: 7,
|
|
active_release: Some(ActiveRelease {
|
|
sequence: 7,
|
|
manifest_sha256: record.manifest_sha256.clone(),
|
|
}),
|
|
};
|
|
verify(&keyring, &release(8, 190, 210), &candidate_context).unwrap();
|
|
assert!(verify(&keyring, &release(7, 190, 210), &candidate_context).is_err());
|
|
assert!(verify(&keyring, &record.release_envelope, &candidate_context).is_err());
|
|
assert!(verify(&keyring, &release(6, 190, 210), &candidate_context).is_err());
|
|
let revoked = UpdateKeyring::new(
|
|
7,
|
|
vec![
|
|
TrustedUpdateKey::new(
|
|
signer.verifying_key().to_bytes(),
|
|
ReleasePlatform::LinuxX64,
|
|
1,
|
|
20,
|
|
true,
|
|
)
|
|
.unwrap(),
|
|
],
|
|
)
|
|
.unwrap();
|
|
assert!(verify_installed_release(&revoked, &record, 7).is_err());
|
|
}
|
|
}
|