211 lines
10 KiB
Rust
211 lines
10 KiB
Rust
//! Hands off (Custos docs/12_hands_off.md): a machine-key vault that Custos opens itself, the rules for auto-connect,
|
|
//! the unattended setup run and unattended signing, and *Take over the vault*.
|
|
|
|
use std::collections::BTreeSet;
|
|
|
|
use custos_vault::security::{
|
|
Action, Item, Level, ProvisionAsk, Store, Verb, auto_connect_allowed, is_setup_run, setup_on_connect,
|
|
unattended_provision, unattended_sign,
|
|
};
|
|
use custos_vault::{Error, FactorKind, KdfParams, SecretString, Unlock, Vault};
|
|
|
|
const FAST: KdfParams = KdfParams { m_cost_kib: 64, t_cost: 1, p_cost: 1 };
|
|
const NOW: i64 = 1_793_750_000_000;
|
|
const PC: [u8; 32] = [7; 32];
|
|
|
|
fn none() -> BTreeSet<Item> {
|
|
BTreeSet::new()
|
|
}
|
|
fn ticked(items: &[Item]) -> BTreeSet<Item> {
|
|
items.iter().copied().collect()
|
|
}
|
|
|
|
// ---------------------------------------------------------------- take over
|
|
|
|
#[test]
|
|
fn a_machine_key_vault_is_taken_over_with_a_master_password() {
|
|
let mut v = Vault::create_machine_store(&PC, "MARK-PC").unwrap();
|
|
assert!(v.opens_with_machine_key());
|
|
// The hands-off vault holds the release set (with the approval key) from its first start.
|
|
v.signing_create(custos_vault::signing::Role::Release, &[], &[], NOW).unwrap();
|
|
|
|
let id = v.take_over(&SecretString::from("a long password"), FAST, NOW).expect("taken over");
|
|
assert!(!v.opens_with_machine_key());
|
|
assert!(v.factors().iter().any(|f| f.id == id && f.kind == FactorKind::Password));
|
|
let bytes = v.to_bytes().unwrap();
|
|
|
|
// This PC alone no longer opens it; the password and this PC do, and the keys are still there.
|
|
assert!(Vault::open_machine_store(&bytes, &PC).is_err(), "the PC-only access is gone");
|
|
let pw = SecretString::from("a long password");
|
|
let w = Vault::open_with(&bytes, Unlock::Password(&pw), Unlock::Material(FactorKind::Device, &PC))
|
|
.expect("two factors");
|
|
assert!(w.same_key_as(&v));
|
|
assert_eq!(w.signing_keys().len(), v.signing_keys().len());
|
|
assert!(!w.signing_keys().is_empty());
|
|
|
|
// Unattended work is turned off, and it is audited.
|
|
let view = w.security_view(NOW);
|
|
assert!(view.ticked.contains(&Item::NoAutoConnect));
|
|
assert!(view.ticked.contains(&Item::NoUnattended));
|
|
assert!(view.ticked.contains(&Item::CardSigning));
|
|
assert_eq!(view.level, Level::Hardened);
|
|
assert!(view.audit.iter().any(|e| e.action == Action::TakenOver));
|
|
}
|
|
|
|
#[test]
|
|
fn only_a_machine_key_vault_is_taken_over_and_only_once() {
|
|
let mut user = Vault::create(&SecretString::from("pw"), FAST).unwrap();
|
|
assert!(!user.opens_with_machine_key());
|
|
assert!(user.take_over(&SecretString::from("a long password"), FAST, NOW).is_err());
|
|
|
|
let mut v = Vault::create_machine_store(&PC, "MARK-PC").unwrap();
|
|
v.take_over(&SecretString::from("a long password"), FAST, NOW).unwrap();
|
|
assert!(v.take_over(&SecretString::from("another long one"), FAST, NOW).is_err(), "once");
|
|
}
|
|
|
|
#[test]
|
|
fn after_the_take_over_a_phone_can_be_added_as_for_any_vault() {
|
|
let mut v = Vault::create_machine_store(&PC, "MARK-PC").unwrap();
|
|
assert!(v.add_factor(Unlock::Material(FactorKind::Approval, &[3; 32]), "phone").is_err(), "not before");
|
|
v.take_over(&SecretString::from("a long password"), FAST, NOW).unwrap();
|
|
v.add_factor(Unlock::Material(FactorKind::Approval, &[3; 32]), "phone").expect("after");
|
|
let bytes = v.to_bytes().unwrap();
|
|
assert!(
|
|
Vault::open_with(
|
|
&bytes,
|
|
Unlock::Material(FactorKind::Device, &PC),
|
|
Unlock::Material(FactorKind::Approval, &[3; 32])
|
|
)
|
|
.is_ok()
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn notes_carry_their_detail_and_refuse_other_actions() {
|
|
let mut v = Vault::create_machine_store(&PC, "MARK-PC").unwrap();
|
|
v.security_note(Action::AutoConnected, "Connected to 192.168.100.217 as MARK-PC", NOW).unwrap();
|
|
v.security_note(Action::UnattendedStored, "setup-1: arca/authelia-mark, arca/totp-mark", NOW).unwrap();
|
|
v.security_note(Action::SetupItemRemoved, "setup-1: arca/totp-mark", NOW).unwrap();
|
|
v.security_note(Action::UnattendedSigned, "domi-release minisign run r42 Arca_0.3.0_x64-setup.exe", NOW).unwrap();
|
|
assert!(v.security_note(Action::Ticked, "x", NOW).is_err());
|
|
assert!(v.security_note(Action::AutoConnected, "line\nbreak", NOW).is_err(), "no control characters");
|
|
assert!(v.security_note(Action::AutoConnected, &"x".repeat(501), NOW).is_err(), "at most 500 characters");
|
|
|
|
let bytes = v.to_bytes().unwrap();
|
|
let w = Vault::open_machine_store(&bytes, &PC).unwrap();
|
|
let audit = w.security_view(NOW).audit;
|
|
assert_eq!(audit.len(), 4);
|
|
assert_eq!(audit[0].detail.as_deref(), Some("Connected to 192.168.100.217 as MARK-PC"));
|
|
assert_eq!(audit[3].action, Action::UnattendedSigned);
|
|
}
|
|
|
|
// ---------------------------------------------------------------- auto-connect and the Setup window
|
|
|
|
#[test]
|
|
fn auto_connect_runs_in_setup_and_standard_and_stops_when_hardened() {
|
|
assert!(auto_connect_allowed(Level::Setup, &none()));
|
|
assert!(auto_connect_allowed(Level::Standard, &none()));
|
|
assert!(!auto_connect_allowed(Level::Hardened, &ticked(&[Item::HelloApprovals])));
|
|
assert!(!auto_connect_allowed(Level::Setup, &ticked(&[Item::NoAutoConnect])), "the item holds in Setup too");
|
|
assert!(!auto_connect_allowed(Level::Setup, &ticked(&[Item::NoSsh])), "no SSH implies no auto-connect");
|
|
}
|
|
|
|
#[test]
|
|
fn a_new_connect_opens_setup_unless_unattended_is_off() {
|
|
assert!(setup_on_connect(&none()));
|
|
assert!(!setup_on_connect(&ticked(&[Item::NoUnattended])));
|
|
assert!(!setup_on_connect(&ticked(&[Item::NoSsh])));
|
|
}
|
|
|
|
#[test]
|
|
fn setup_runs_are_named_setup_dash_something_short() {
|
|
assert!(is_setup_run("setup-1"));
|
|
assert!(is_setup_run("setup-2026-11-05.first_run"));
|
|
assert!(is_setup_run(&format!("setup-{}", "a".repeat(58))));
|
|
assert!(!is_setup_run(&format!("setup-{}", "a".repeat(59))));
|
|
assert!(!is_setup_run("setup-"));
|
|
assert!(!is_setup_run("setup-a b"));
|
|
assert!(!is_setup_run("setup-a/b"));
|
|
assert!(!is_setup_run("Setup-1"));
|
|
assert!(!is_setup_run("r42"));
|
|
}
|
|
|
|
// ---------------------------------------------------------------- the unattended setup run
|
|
|
|
fn ask(item: &str) -> ProvisionAsk<'_> {
|
|
ProvisionAsk { verb: Verb::Ask, generate: true, exists: false, store: Store::Vault, item, totp: false }
|
|
}
|
|
fn offer(item: &str) -> ProvisionAsk<'_> {
|
|
ProvisionAsk { verb: Verb::Offer, generate: false, exists: false, store: Store::Vault, item, totp: false }
|
|
}
|
|
|
|
#[test]
|
|
fn a_setup_run_in_setup_takes_new_items_in_the_allowed_namespaces() {
|
|
for item in ["arca/authelia-mark", "custos/x", "fabrica/hub-token", "aula/admin", "gitea/read-token"] {
|
|
assert!(unattended_provision(Level::Setup, &none(), "setup-1", &ask(item)), "{item}");
|
|
assert!(unattended_provision(Level::Setup, &none(), "setup-1", &offer(item)), "{item}");
|
|
}
|
|
let totp = ProvisionAsk { totp: true, ..offer("arca/totp-mark") };
|
|
assert!(unattended_provision(Level::Setup, &none(), "setup-1", &totp));
|
|
let claude = ProvisionAsk { store: Store::Claude, ..offer("fabrica-hub-token") };
|
|
assert!(unattended_provision(Level::Setup, &none(), "setup-1", &claude), "the Claude store's names");
|
|
}
|
|
|
|
#[test]
|
|
fn everything_else_waits_for_a_card() {
|
|
let s = |l, t: &BTreeSet<Item>, run, r: &ProvisionAsk| unattended_provision(l, t, run, r);
|
|
let n = none();
|
|
// Existing items, asks without --generate, replacing offers.
|
|
assert!(!s(Level::Setup, &n, "setup-1", &ProvisionAsk { exists: true, ..ask("arca/x") }), "an existing item");
|
|
assert!(!s(Level::Setup, &n, "setup-1", &ProvisionAsk { generate: false, ..ask("arca/x") }), "no --generate");
|
|
assert!(!s(Level::Setup, &n, "setup-1", &ProvisionAsk { exists: true, ..offer("arca/x") }), "a replacing offer");
|
|
// Namespaces.
|
|
for item in ["mark/bank", "domi/root", "arcax/y", "arca", "../arca/x", "ARCA/x"] {
|
|
assert!(!s(Level::Setup, &n, "setup-1", &ask(item)), "{item}");
|
|
}
|
|
// Display offers, and asks from the Claude store.
|
|
assert!(!s(Level::Setup, &n, "setup-1", &ProvisionAsk { store: Store::Display, ..offer("arca/totp-mark") }));
|
|
assert!(!s(Level::Setup, &n, "setup-1", &ProvisionAsk { store: Store::Claude, ..ask("fabrica-hub-token") }));
|
|
// The run and the level.
|
|
assert!(!s(Level::Setup, &n, "r42", &ask("arca/x")), "not a setup run");
|
|
assert!(!s(Level::Standard, &n, "setup-1", &ask("arca/x")), "after the window");
|
|
assert!(!s(Level::Hardened, &ticked(&[Item::NarrowScope]), "setup-1", &ask("arca/x")));
|
|
// Items that turn it off.
|
|
assert!(!s(Level::Setup, &ticked(&[Item::NoUnattended]), "setup-1", &ask("arca/x")));
|
|
let totp = ProvisionAsk { totp: true, ..offer("arca/totp-mark") };
|
|
assert!(!s(Level::Setup, &ticked(&[Item::TotpOnPhone]), "setup-1", &totp), "the TOTP lives on the phone");
|
|
}
|
|
|
|
// ---------------------------------------------------------------- unattended signing
|
|
|
|
#[test]
|
|
fn a_machine_key_vault_signs_run_requests_of_the_release_kinds_without_a_card() {
|
|
for kind in ["minisign", "manifest", "authenticode", "approval"] {
|
|
assert!(unattended_sign(Level::Standard, &none(), true, kind, true), "{kind}");
|
|
assert!(unattended_sign(Level::Setup, &none(), true, kind, true), "{kind} in Setup");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn signing_waits_for_a_card_otherwise() {
|
|
assert!(!unattended_sign(Level::Standard, &none(), false, "minisign", true), "taken over");
|
|
assert!(!unattended_sign(Level::Standard, &none(), true, "minisign", false), "no run");
|
|
assert!(!unattended_sign(Level::Standard, &none(), true, "ssh", true), "another kind");
|
|
assert!(!unattended_sign(Level::Hardened, &ticked(&[Item::CardSigning]), true, "minisign", true));
|
|
assert!(!unattended_sign(Level::Hardened, &ticked(&[Item::HelloApprovals]), true, "minisign", true), "hardened");
|
|
}
|
|
|
|
#[test]
|
|
fn the_new_items_round_trip_and_can_be_undone() {
|
|
let mut v = Vault::create(&SecretString::from("pw"), FAST).unwrap();
|
|
for item in [Item::NoAutoConnect, Item::NoUnattended, Item::CardSigning] {
|
|
assert!(v.security_tick(item, NOW));
|
|
}
|
|
let bytes = v.to_bytes().unwrap();
|
|
let mut w = Vault::open(&bytes, &SecretString::from("pw")).unwrap();
|
|
assert_eq!(w.security_view(NOW).ticked, ticked(&[Item::NoAutoConnect, Item::NoUnattended, Item::CardSigning]));
|
|
assert!(w.security_untick(Item::CardSigning, custos_vault::security::Auth::Hello, NOW));
|
|
assert!(!w.security_view(NOW).ticked.contains(&Item::CardSigning));
|
|
let _ = Error::Corrupted;
|
|
}
|