diff --git a/Cargo.toml b/Cargo.toml index 20c1af2..ec23e39 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,14 @@ publish.workspace = true name = "idunn" path = "src/main.rs" +[[bin]] +name = "idunn-provision" +path = "src/bin/idunn-provision.rs" + +[lib] +name = "idunn_daemon" +path = "src/lib.rs" + [dependencies] anyhow = "1" cultmesh-rs = { path = "../../vendor/cultmesh-rs" } diff --git a/src/bin/idunn-provision.rs b/src/bin/idunn-provision.rs new file mode 100644 index 0000000..cab36d3 --- /dev/null +++ b/src/bin/idunn-provision.rs @@ -0,0 +1,6 @@ +fn main() { + if let Err(error) = idunn_daemon::provisioning::run(std::env::args().skip(1)) { + eprintln!("idunn-provision: {error:#}"); + std::process::exit(1); + } +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..58309eb --- /dev/null +++ b/src/lib.rs @@ -0,0 +1 @@ +pub mod provisioning; diff --git a/src/main.rs b/src/main.rs index 2959a3f..e1b9d8b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,9 +8,12 @@ use cultnet_rs::{ CultNetMessage, CultNetRawPayloadEncoding, CultNetRudpPacketType, CultNetRudpSession, CultNetRudpSessionOptions, CultNetRudpSocketTransportConnection, CultNetRudpSocketTransportOptions, CultNetWireContract, - IdunnAuthenticatedProviderHealthProjectionPurpose, IdunnServiceIdentity, ServiceIdentitySigner, + GameCultProviderHealthIdentity, IdunnAuthenticatedProviderHealthProjectionPurpose, + IdunnServiceIdentity, IdunnSignedDaemonHealthPurpose, ServiceIdentitySignature, + ServiceIdentitySigner, decode_cultnet_message_from_slice, decode_rudp_packet, encode_cultnet_message_to_vec, - encode_rudp_packet, open_service_identity_at, + derive_service_identity_id, encode_rudp_packet, open_service_identity_at, + verify_service_identity_signature_with_public_key, }; use ed25519_dalek::{Signature, Verifier, VerifyingKey}; use odin_core::{ @@ -3075,15 +3078,9 @@ fn authenticate_generic_signed_health( "signed daemon health carries release authority not declared by its trust binding" )); } - let key_bytes: [u8; 32] = binding - .signer_public_key - .as_slice() - .try_into() - .map_err(|_| anyhow!("daemon health trust key length is invalid"))?; - let expected_identity = format!( - "{:x}", - Sha256::digest([HOST_IDENTITY_DOMAIN, binding.signer_public_key.as_slice()].concat()) - ); + let expected_identity = derive_service_identity_id::( + &binding.signer_public_key, + )?; if binding.signer_identity_id != expected_identity || statement.signer_identity_id != binding.signer_identity_id { @@ -3097,12 +3094,18 @@ fn authenticate_generic_signed_health( let mut unsigned = statement.clone(); unsigned.signature.clear(); let unsigned_payload = rmp_serde::to_vec(&unsigned)?; - VerifyingKey::from_bytes(&key_bytes)? - .verify( - &host_signature_message(SIGNED_DAEMON_HEALTH_TYPE, &unsigned_payload), - &Signature::from_bytes(&signature_bytes), - ) - .context("signed daemon health signature is invalid")?; + verify_service_identity_signature_with_public_key::< + GameCultProviderHealthIdentity, + IdunnSignedDaemonHealthPurpose, + >( + &binding.signer_public_key, + &unsigned_payload, + &ServiceIdentitySignature { + identity_id: binding.signer_identity_id.clone(), + signature: signature_bytes.to_vec(), + }, + ) + .context("signed daemon health signature is invalid")?; let admitted_at_unix_millis = parse_timestamp_millis(admitted_at) .ok_or_else(|| anyhow!("signed daemon health admission time is invalid"))?; if statement.observed_at_unix_millis > admitted_at_unix_millis @@ -8201,12 +8204,12 @@ mod tests { let root = tempfile::tempdir().unwrap(); let trust_path = root.path().join("health-trust.cc"); let store_path = root.path().join("idunn.cc"); - let key = SigningKey::from_bytes(&[31; 32]); - let public_key = key.verifying_key().to_bytes().to_vec(); - let identity_id = format!( - "{:x}", - Sha256::digest([HOST_IDENTITY_DOMAIN, public_key.as_slice()].concat()) - ); + let signer = cultnet_rs::enroll_service_identity_at::( + &root.path().join("provider-health-identity.cc"), + ) + .unwrap(); + let public_key = signer.entry().public_key.clone(); + let identity_id = signer.entry().identity_id.clone(); let binding = IdunnDaemonHealthTrustBindingRecord { schema_version: odin_core::IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA.into(), binding_id: "root/test-daemon/health".into(), @@ -8249,10 +8252,9 @@ mod tests { private_state_exposed: false, }; let unsigned = rmp_serde::to_vec(&statement).unwrap(); - statement.signature = key - .sign(&host_signature_message(SIGNED_DAEMON_HEALTH_TYPE, &unsigned)) - .to_bytes() - .to_vec(); + statement.signature = signer + .sign::(&unsigned) + .signature; let message = CultNetMessage::DocumentPutRaw { message_id: format!("signed-health-{sequence}"), document: CultNetRawDocumentRecord { @@ -8695,7 +8697,7 @@ mod tests { #[test] fn generic_signed_health_requires_canonical_bytes_and_preserves_millisecond_clock() { - let (mut message, options, _root) = generic_signed_health_fixture(1); + let (mut message, options, root) = generic_signed_health_fixture(1); let mut statement: IdunnSignedDaemonHealthRecord = match &message { CultNetMessage::DocumentPutRaw { document, .. } => { rmp_serde::from_slice(&document.payload).unwrap() @@ -8707,14 +8709,13 @@ mod tests { .unwrap() .timestamp_millis() as u64; statement.signature.clear(); - let key = SigningKey::from_bytes(&[31; 32]); - statement.signature = key - .sign(&host_signature_message( - SIGNED_DAEMON_HEALTH_TYPE, - &rmp_serde::to_vec(&statement).unwrap(), - )) - .to_bytes() - .to_vec(); + let signer = cultnet_rs::open_service_identity_at::( + &root.path().join("provider-health-identity.cc"), + ) + .unwrap(); + statement.signature = signer + .sign::(&rmp_serde::to_vec(&statement).unwrap()) + .signature; let CultNetMessage::DocumentPutRaw { document, .. } = &mut message else { unreachable!() }; diff --git a/src/provisioning.rs b/src/provisioning.rs new file mode 100644 index 0000000..80c4d4b --- /dev/null +++ b/src/provisioning.rs @@ -0,0 +1,640 @@ +use anyhow::{Context, Result, anyhow, bail}; +use cultcache_rs::{ + CultCacheEnvelope, CultCacheExpectedEnvelope, DatabaseEntry, SingleFileMessagePackBackingStore, +}; +use cultnet_rs::{ + GameCultProviderHealthIdentity, IdunnServiceIdentity, ServiceIdentityProfile, + ServiceIdentityTrustAnchor, derive_service_identity_id, enroll_service_identity_at, + export_service_identity_trust_anchor, open_service_identity_at, +}; +use odin_core::{ + GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA, GameCultServiceTrustAnchorRecord, + IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA, + IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SIGNING_PURPOSE, + IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA, IdunnDaemonHealthTrustBindingRecord, +}; +use std::collections::BTreeMap; +use std::path::{Path, PathBuf}; + +trait RootDistributionDocument: DatabaseEntry { + fn root_key(&self) -> &str; +} + +impl RootDistributionDocument for IdunnDaemonHealthTrustBindingRecord { + fn root_key(&self) -> &str { + &self.binding_id + } +} + +impl RootDistributionDocument for GameCultServiceTrustAnchorRecord { + fn root_key(&self) -> &str { + &self.trust_anchor_id + } +} + +pub fn run(args: impl IntoIterator) -> Result<()> { + let mut args = args.into_iter(); + let command = args.next().ok_or_else(|| anyhow!(usage()))?; + let options = parse_options(args)?; + match command.as_str() { + "enroll-idunn-identity" => { + require_only(&options, &["private-store"])?; + enroll_service_identity_at::(&path(&options, "private-store")?)?; + } + "export-idunn-public-anchor" => { + require_only(&options, &["private-store", "public-anchor"])?; + let private = path(&options, "private-store")?; + let public = path(&options, "public-anchor")?; + reject_alias(&private, &public)?; + refuse_existing(&public, "public anchor")?; + let signer = open_service_identity_at::(&private)?; + export_service_identity_trust_anchor(&signer, &public)?; + } + "create-daemon-health-trust-binding" => create_health_binding(&options)?, + "validate-daemon-health-trust-binding" => { + require_only(&options, &["input"])?; + read_typed::( + &path(&options, "input")?, + IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA, + )? + .validate()?; + } + "create-provider-projection-trust-anchor" => create_projection_anchor(&options)?, + "validate-provider-projection-trust-anchor" => validate_projection_anchor(&options)?, + _ => bail!("unknown command {command:?}\n{}", usage()), + } + Ok(()) +} + +fn create_health_binding(options: &BTreeMap) -> Result<()> { + require_only( + options, + &[ + "output", + "binding-id", + "daemon", + "health-contract", + "source-runtime", + "signer-public-key-hex", + "bound-at-unix-millis", + "release-binding-required", + ], + )?; + let public_key = decode_public_key(required(options, "signer-public-key-hex")?)?; + let record = IdunnDaemonHealthTrustBindingRecord { + schema_version: IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA.into(), + binding_id: required(options, "binding-id")?.into(), + daemon_id: required(options, "daemon")?.into(), + health_contract: required(options, "health-contract")?.into(), + source_runtime_id: required(options, "source-runtime")?.into(), + signer_identity_id: derive_service_identity_id::( + &public_key, + )?, + signer_public_key: public_key, + binding_authority: "root".into(), + bound_at_unix_millis: parse_u64(options, "bound-at-unix-millis")?, + release_binding_required: parse_bool(options, "release-binding-required")?, + private_state_exposed: false, + }; + record.validate()?; + write_new_typed( + &path(options, "output")?, + &record.binding_id, + &record, + IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA, + record.bound_at_unix_millis, + ) +} + +fn create_projection_anchor(options: &BTreeMap) -> Result<()> { + require_only( + options, + &[ + "output", + "trust-anchor-id", + "runtime-id", + "idunn-public-anchor", + "bound-at-unix-millis", + "expires-at-unix-millis", + ], + )?; + let output = path(options, "output")?; + let low_path = path(options, "idunn-public-anchor")?; + reject_alias(&output, &low_path)?; + let low = read_low_level_idunn_anchor(&low_path)?; + let bound = parse_u64(options, "bound-at-unix-millis")?; + let record = GameCultServiceTrustAnchorRecord { + schema_version: GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA.into(), + trust_anchor_id: required(options, "trust-anchor-id")?.into(), + service_id: "idunn".into(), + runtime_id: required(options, "runtime-id")?.into(), + signer_identity_id: derive_service_identity_id::(&low.public_key)?, + signer_public_key: low.public_key, + signature_algorithm: "ed25519".into(), + signing_purpose: IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SIGNING_PURPOSE.into(), + signed_schema: IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SCHEMA.into(), + binding_authority: "root".into(), + bound_at_unix_millis: bound, + expires_at_unix_millis: options + .get("expires-at-unix-millis") + .map(|value| value.parse().context("expires-at-unix-millis must be u64")) + .transpose()?, + private_state_exposed: false, + }; + record.validate()?; + write_new_typed( + &output, + &record.trust_anchor_id, + &record, + GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA, + bound, + ) +} + +fn validate_projection_anchor(options: &BTreeMap) -> Result<()> { + require_only(options, &["input", "idunn-public-anchor"])?; + let input = path(options, "input")?; + let low_path = path(options, "idunn-public-anchor")?; + reject_alias(&input, &low_path)?; + let record = read_typed::( + &input, + GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA, + )?; + record.validate()?; + let low = read_low_level_idunn_anchor(&low_path)?; + if record.signer_public_key != low.public_key || record.signer_identity_id != low.identity_id { + bail!("projection trust anchor does not match the supplied Idunn public anchor"); + } + Ok(()) +} + +fn write_new_typed( + path: &Path, + key: &str, + value: &T, + schema: &str, + millis: u64, +) -> Result<()> { + refuse_existing(path, "root distribution document")?; + if let Some(parent) = path.parent() { + std::fs::create_dir_all(parent)?; + } + let stored_at = chrono::DateTime::from_timestamp_millis( + i64::try_from(millis).context("document timestamp is out of range")?, + ) + .ok_or_else(|| anyhow!("document timestamp is out of range"))? + .to_rfc3339(); + let envelope = CultCacheEnvelope { + key: key.into(), + r#type: T::TYPE.into(), + payload: rmp_serde::to_vec(value)?, + stored_at, + schema_id: Some(schema.into()), + }; + let created = SingleFileMessagePackBackingStore::new(path).compare_exchange( + &[CultCacheExpectedEnvelope { + r#type: T::TYPE.into(), + key: key.into(), + current: None, + }], + &[envelope], + )?; + if !created { + bail!("root distribution document lost atomic create race; refusing replacement"); + } + Ok(()) +} + +fn read_typed(path: &Path, schema: &str) -> Result { + let entries = SingleFileMessagePackBackingStore::new(path).pull_all_read_only_snapshot()?; + let [entry] = entries.as_slice() else { + bail!("root distribution file must contain exactly one document"); + }; + if entry.r#type != T::TYPE || entry.schema_id.as_deref() != Some(schema) { + bail!("root distribution document has the wrong type or schema"); + } + let value: T = rmp_serde::from_slice(&entry.payload).context("decoding typed root document")?; + if rmp_serde::to_vec(&value)? != entry.payload { + bail!("root distribution document is not canonical positional MessagePack"); + } + if entry.key != value.root_key() { + bail!("root distribution envelope key does not match its typed document"); + } + Ok(value) +} + +fn read_low_level_idunn_anchor(path: &Path) -> Result { + let entries = SingleFileMessagePackBackingStore::new(path).pull_all_read_only_snapshot()?; + let [entry] = entries.as_slice() else { + bail!("Idunn public anchor store must contain exactly one document"); + }; + if entry.r#type != IdunnServiceIdentity::TRUST_ANCHOR_TYPE + || entry.key != IdunnServiceIdentity::TRUST_ANCHOR_KEY + || entry.schema_id.as_deref() != Some(IdunnServiceIdentity::TRUST_ANCHOR_SCHEMA) + { + bail!("public anchor does not belong to the Idunn service identity profile"); + } + let anchor: ServiceIdentityTrustAnchor = rmp_serde::from_slice(&entry.payload)?; + if rmp_serde::to_vec(&anchor)? != entry.payload + || anchor.schema_version != IdunnServiceIdentity::TRUST_ANCHOR_SCHEMA + || anchor.identity_id + != derive_service_identity_id::(&anchor.public_key)? + { + bail!("Idunn public anchor is malformed or names the wrong key"); + } + Ok(anchor) +} + +fn parse_options(args: impl Iterator) -> Result> { + let mut args = args.peekable(); + let mut out = BTreeMap::new(); + while let Some(name) = args.next() { + let name = name + .strip_prefix("--") + .ok_or_else(|| anyhow!("expected --option, got {name:?}"))?; + if name.contains("seed") || name.contains("private-key") { + bail!("private seed/key input is forbidden; enroll a protected identity instead"); + } + let value = args + .next() + .ok_or_else(|| anyhow!("missing value for --{name}"))?; + if out.insert(name.into(), value).is_some() { + bail!("duplicate option --{name}"); + } + } + Ok(out) +} + +fn require_only(options: &BTreeMap, names: &[&str]) -> Result<()> { + for name in names { + required(options, name)?; + } + if let Some(name) = options.keys().find(|name| !names.contains(&name.as_str())) { + bail!("unsupported option --{name}"); + } + Ok(()) +} + +fn required<'a>(options: &'a BTreeMap, name: &str) -> Result<&'a str> { + options + .get(name) + .map(String::as_str) + .ok_or_else(|| anyhow!("missing --{name}")) +} + +fn path(options: &BTreeMap, name: &str) -> Result { + let value = required(options, name)?; + if value.trim().is_empty() { + bail!("--{name} must not be empty"); + } + Ok(PathBuf::from(value)) +} + +fn parse_u64(options: &BTreeMap, name: &str) -> Result { + required(options, name)? + .parse() + .with_context(|| format!("--{name} must be u64")) +} + +fn parse_bool(options: &BTreeMap, name: &str) -> Result { + match required(options, name)? { + "true" => Ok(true), + "false" => Ok(false), + _ => bail!("--{name} must be true or false"), + } +} + +fn decode_public_key(value: &str) -> Result> { + if value.len() != 64 + || !value + .bytes() + .all(|byte| byte.is_ascii_hexdigit() && !byte.is_ascii_uppercase()) + { + bail!("signer public key must be exactly 64 lowercase hexadecimal characters"); + } + (0..32) + .map(|index| u8::from_str_radix(&value[index * 2..index * 2 + 2], 16).map_err(Into::into)) + .collect() +} + +fn refuse_existing(path: &Path, label: &str) -> Result<()> { + if path.exists() { + bail!( + "{label} {} already exists; replacement is forbidden", + path.display() + ); + } + Ok(()) +} + +fn reject_alias(first: &Path, second: &Path) -> Result<()> { + if normalized(first)? == normalized(second)? { + bail!("private/public or input/output paths must not alias"); + } + Ok(()) +} + +fn normalized(path: &Path) -> Result { + let absolute = if path.is_absolute() { + path.to_path_buf() + } else { + std::env::current_dir()?.join(path) + }; + let parent = absolute + .parent() + .ok_or_else(|| anyhow!("path has no parent"))?; + let parent = parent + .canonicalize() + .unwrap_or_else(|_| parent.to_path_buf()); + Ok(parent.join( + absolute + .file_name() + .ok_or_else(|| anyhow!("path has no filename"))?, + )) +} + +fn usage() -> &'static str { + "Usage: idunn-provision enroll-idunn-identity --private-store \n idunn-provision export-idunn-public-anchor --private-store --public-anchor \n idunn-provision create-daemon-health-trust-binding --output --binding-id --daemon --health-contract --source-runtime --signer-public-key-hex --bound-at-unix-millis --release-binding-required \n idunn-provision validate-daemon-health-trust-binding --input \n idunn-provision create-provider-projection-trust-anchor --output --trust-anchor-id --runtime-id --idunn-public-anchor --bound-at-unix-millis --expires-at-unix-millis \n idunn-provision validate-provider-projection-trust-anchor --input --idunn-public-anchor " +} + +#[cfg(test)] +mod tests { + use super::*; + use cultcache_rs::CacheBackingStore; + use tempfile::TempDir; + + fn invoke(args: &[&str]) -> Result<()> { + run(args.iter().map(|value| value.to_string())) + } + + fn replace_typed( + path: &Path, + key: &str, + schema: &str, + value: &T, + ) -> Result<()> { + std::fs::remove_file(path)?; + SingleFileMessagePackBackingStore::new(path).push(&CultCacheEnvelope { + key: key.into(), + r#type: T::TYPE.into(), + payload: rmp_serde::to_vec(value)?, + stored_at: "2026-07-19T20:00:00Z".into(), + schema_id: Some(schema.into()), + }) + } + + #[test] + fn identity_enrollment_and_export_are_immutable_and_paths_cannot_alias() -> Result<()> { + let temp = TempDir::new()?; + let private = temp.path().join("identity.cc"); + let public = temp.path().join("identity-public.cc"); + invoke(&[ + "enroll-idunn-identity", + "--private-store", + private.to_str().unwrap(), + ])?; + assert!( + invoke(&[ + "enroll-idunn-identity", + "--private-store", + private.to_str().unwrap() + ]) + .is_err() + ); + assert!( + invoke(&[ + "export-idunn-public-anchor", + "--private-store", + private.to_str().unwrap(), + "--public-anchor", + private.to_str().unwrap(), + ]) + .is_err() + ); + invoke(&[ + "export-idunn-public-anchor", + "--private-store", + private.to_str().unwrap(), + "--public-anchor", + public.to_str().unwrap(), + ])?; + assert!( + invoke(&[ + "export-idunn-public-anchor", + "--private-store", + private.to_str().unwrap(), + "--public-anchor", + public.to_str().unwrap(), + ]) + .is_err() + ); + assert!( + invoke(&[ + "enroll-idunn-identity", + "--private-store", + temp.path().join("new.cc").to_str().unwrap(), + "--private-seed", + "00", + ]) + .is_err() + ); + Ok(()) + } + + #[test] + fn health_binding_derives_identity_and_rejects_overwrite_and_corruption() -> Result<()> { + let temp = TempDir::new()?; + let output = temp.path().join("provider-binding.cc"); + let key = "07".repeat(32); + let args = [ + "create-daemon-health-trust-binding", + "--output", + output.to_str().unwrap(), + "--binding-id", + "root/epiphany/health", + "--daemon", + "yggdrasil-epiphany", + "--health-contract", + "epiphany.cultnet-rudp-runtime-health", + "--source-runtime", + "epiphany-daemon-supervisor", + "--signer-public-key-hex", + &key, + "--bound-at-unix-millis", + "1784483100000", + "--release-binding-required", + "true", + ]; + invoke(&args)?; + assert!(invoke(&args).is_err()); + let mut record = read_typed::( + &output, + IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA, + )?; + assert_eq!( + record.signer_identity_id, + derive_service_identity_id::( + &record.signer_public_key + )? + ); + record.signer_identity_id = "caller-chosen".into(); + replace_typed( + &output, + &record.binding_id, + IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA, + &record, + )?; + assert!( + invoke(&[ + "validate-daemon-health-trust-binding", + "--input", + output.to_str().unwrap() + ]) + .is_err() + ); + + record.signer_identity_id = derive_service_identity_id::( + &record.signer_public_key, + )?; + record.schema_version = "wrong.schema.v9".into(); + replace_typed( + &output, + &record.binding_id, + IDUNN_DAEMON_HEALTH_TRUST_BINDING_SCHEMA, + &record, + )?; + assert!( + invoke(&[ + "validate-daemon-health-trust-binding", + "--input", + output.to_str().unwrap() + ]) + .is_err() + ); + Ok(()) + } + + #[test] + fn projection_anchor_is_exact_profile_and_matches_low_level_anchor() -> Result<()> { + let temp = TempDir::new()?; + let private = temp.path().join("identity.cc"); + let public = temp.path().join("public.cc"); + let root = temp.path().join("root-anchor.cc"); + invoke(&[ + "enroll-idunn-identity", + "--private-store", + private.to_str().unwrap(), + ])?; + invoke(&[ + "export-idunn-public-anchor", + "--private-store", + private.to_str().unwrap(), + "--public-anchor", + public.to_str().unwrap(), + ])?; + let create = [ + "create-provider-projection-trust-anchor", + "--output", + root.to_str().unwrap(), + "--trust-anchor-id", + "root/idunn/provider-health", + "--runtime-id", + "idunn-yggdrasil", + "--idunn-public-anchor", + public.to_str().unwrap(), + "--bound-at-unix-millis", + "1784483100000", + "--expires-at-unix-millis", + "1815000000000", + ]; + invoke(&create)?; + invoke(&[ + "validate-provider-projection-trust-anchor", + "--input", + root.to_str().unwrap(), + "--idunn-public-anchor", + public.to_str().unwrap(), + ])?; + assert!(invoke(&create).is_err()); + assert!( + invoke(&[ + "validate-provider-projection-trust-anchor", + "--input", + public.to_str().unwrap(), + "--idunn-public-anchor", + public.to_str().unwrap(), + ]) + .is_err() + ); + + let mut record = read_typed::( + &root, + GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA, + )?; + record.signing_purpose = "wrong-purpose".into(); + replace_typed( + &root, + &record.trust_anchor_id, + GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA, + &record, + )?; + assert!( + invoke(&[ + "validate-provider-projection-trust-anchor", + "--input", + root.to_str().unwrap(), + "--idunn-public-anchor", + public.to_str().unwrap(), + ]) + .is_err() + ); + + record.signing_purpose = + IDUNN_AUTHENTICATED_PROVIDER_HEALTH_PROJECTION_SIGNING_PURPOSE.into(); + record.signer_public_key = vec![9; 32]; + replace_typed( + &root, + &record.trust_anchor_id, + GAMECULT_SERVICE_TRUST_ANCHOR_SCHEMA, + &record, + )?; + assert!( + invoke(&[ + "validate-provider-projection-trust-anchor", + "--input", + root.to_str().unwrap(), + "--idunn-public-anchor", + public.to_str().unwrap(), + ]) + .is_err() + ); + Ok(()) + } + + #[test] + fn release_binding_policy_is_explicit_and_boolean() { + let temp = TempDir::new().unwrap(); + let output = temp.path().join("binding.cc"); + let base = [ + "create-daemon-health-trust-binding", + "--output", + output.to_str().unwrap(), + "--binding-id", + "root/provider/health", + "--daemon", + "provider", + "--health-contract", + "provider.health", + "--source-runtime", + "provider-runtime", + "--signer-public-key-hex", + "0707070707070707070707070707070707070707070707070707070707070707", + "--bound-at-unix-millis", + "1784483100000", + ]; + assert!(invoke(&base).is_err()); + let mut partial = base.to_vec(); + partial.extend(["--release-binding-required", "sometimes"]); + assert!(invoke(&partial).is_err()); + } +}