[vault] Signing: the signing body member (release and test key sets, scope, sign-only API, append-only audit log); custos-sign draws from SysRng; the wasm build links getrandom 0.2 to the browser CSPRNG
This commit is contained in:
@@ -23,3 +23,5 @@ qrcode = { version = "0.14", default-features = false, features = ["svg"] }
|
||||
|
||||
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
||||
getrandom = "0.4"
|
||||
# rand_core 0.6 (custos-sign, through custos-vault) uses getrandom 0.2: the same browser CSPRNG (lib.rs, `fill_v02`).
|
||||
getrandom02 = { package = "getrandom", version = "0.2", features = ["custom"] }
|
||||
|
||||
@@ -48,11 +48,21 @@ const PHONE_APPROVAL: &[u8] = b"custos phone approval v1";
|
||||
#[derive(Deserialize)]
|
||||
#[serde(tag = "kind", rename_all = "snake_case", rename_all_fields = "camelCase")]
|
||||
enum FactorIn {
|
||||
Password { password: SecretString },
|
||||
Password {
|
||||
password: SecretString,
|
||||
},
|
||||
/// A factor service's or a device's answer, sealed to a key from `recipient` (`pub`) for `context`.
|
||||
Sealed { factor: FactorKind, r#pub: String, sealed: String, context: String },
|
||||
Sealed {
|
||||
factor: FactorKind,
|
||||
r#pub: String,
|
||||
sealed: String,
|
||||
context: String,
|
||||
},
|
||||
/// Material the page holds itself: a passkey's PRF output (base64).
|
||||
Material { factor: FactorKind, material: SecretString },
|
||||
Material {
|
||||
factor: FactorKind,
|
||||
material: SecretString,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@@ -426,8 +436,12 @@ pub fn handle(request: &[u8]) -> Value {
|
||||
}
|
||||
Request::DomiPair { r#pub, id, label, device } => {
|
||||
let device_ok = data_encoding::BASE64URL_NOPAD.decode(device.as_bytes()).is_ok_and(|d| d.len() == 32);
|
||||
let id_ok = !id.is_empty() && id.len() <= 64 && id.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'_');
|
||||
let (Some(public), true, true) = (b64(&r#pub), device_ok, id_ok) else { return err("A malformed request.") };
|
||||
let id_ok = !id.is_empty()
|
||||
&& id.len() <= 64
|
||||
&& id.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'_');
|
||||
let (Some(public), true, true) = (b64(&r#pub), device_ok, id_ok) else {
|
||||
return err("A malformed request.");
|
||||
};
|
||||
let label: String = label.trim().chars().filter(|c| !c.is_control()).take(40).collect();
|
||||
let plain = json!({ "label": label, "device": device }).to_string();
|
||||
match custos_seal::seal(&public, format!("domi-pair:{id}").as_bytes(), plain.as_bytes()) {
|
||||
@@ -473,6 +487,14 @@ mod abi {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The same for getrandom 0.2, which rand_core 0.6 (custos-sign's RSA and X.509 crates) is built on. The
|
||||
/// browser never makes signing keys, but the crates must link.
|
||||
fn fill_v02(dest: &mut [u8]) -> Result<(), getrandom02::Error> {
|
||||
unsafe { random_fill(dest.as_mut_ptr(), dest.len()) };
|
||||
Ok(())
|
||||
}
|
||||
getrandom02::register_custom_getrandom!(fill_v02);
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn alloc(len: usize) -> *mut u8 {
|
||||
if len == 0 {
|
||||
|
||||
@@ -17,8 +17,14 @@ fn call(v: Value) -> Value {
|
||||
/// A version-2 vault: the master password and a TOTP factor with `totp` as its material.
|
||||
fn vault(totp: &[u8]) -> Vault {
|
||||
let pw = SecretString::from(PASSWORD);
|
||||
let mut v = Vault::create_with(Unlock::Password(&pw), "Master password", Unlock::Material(FactorKind::Totp, totp), "TOTP", FAST)
|
||||
.unwrap();
|
||||
let mut v = Vault::create_with(
|
||||
Unlock::Password(&pw),
|
||||
"Master password",
|
||||
Unlock::Material(FactorKind::Totp, totp),
|
||||
"TOTP",
|
||||
FAST,
|
||||
)
|
||||
.unwrap();
|
||||
let mut login = Item::new(ItemType::WebLogin, "GitHub", 0);
|
||||
login.set("password", SECRET, 0);
|
||||
v.add(login);
|
||||
@@ -53,7 +59,8 @@ fn open_with_password_and_a_sealed_totp_release() {
|
||||
assert_eq!(me["match"], custos_seal::match_code(&public));
|
||||
// What the factor service sends back after a valid code.
|
||||
let sealed = BASE64.encode(&custos_seal::seal(&public, context.as_bytes(), &material).unwrap());
|
||||
let sealed_in = |ctx: &str| json!({ "kind": "sealed", "factor": "totp", "pub": me["pub"], "sealed": sealed, "context": ctx });
|
||||
let sealed_in =
|
||||
|ctx: &str| json!({ "kind": "sealed", "factor": "totp", "pub": me["pub"], "sealed": sealed, "context": ctx });
|
||||
let password = json!({ "kind": "password", "password": PASSWORD });
|
||||
|
||||
// Only the right context opens it.
|
||||
@@ -91,7 +98,9 @@ fn a_phone_pairs_then_opens_and_approves() {
|
||||
// The PC waits with a one-time key; the phone seals its pairing to it.
|
||||
let pc = Recipient::generate();
|
||||
let context = "pair:abc";
|
||||
let r = call(json!({ "op": "phone_enrol", "pub": BASE64.encode(&pc.public()), "context": context, "label": "Phone", "prf": prf64 }));
|
||||
let r = call(
|
||||
json!({ "op": "phone_enrol", "pub": BASE64.encode(&pc.public()), "context": context, "label": "Phone", "prf": prf64 }),
|
||||
);
|
||||
assert_eq!(r["ok"], true, "{r}");
|
||||
let sealed = BASE64.decode(r["sealed"].as_str().unwrap().as_bytes()).unwrap();
|
||||
assert_eq!(r["code"], custos_seal::sender_code(&sealed).unwrap(), "the PC checks the code the phone shows");
|
||||
@@ -105,7 +114,8 @@ fn a_phone_pairs_then_opens_and_approves() {
|
||||
|
||||
// The PC adds both factors.
|
||||
let passkey_id = v.add_factor(Unlock::Material(FactorKind::Passkey, &prf), "Phone passkey").unwrap();
|
||||
let approval_id = v.add_factor_apart(Unlock::Material(FactorKind::Approval, &approval), "Phone", &[&passkey_id]).unwrap();
|
||||
let approval_id =
|
||||
v.add_factor_apart(Unlock::Material(FactorKind::Approval, &approval), "Phone", &[&passkey_id]).unwrap();
|
||||
let file = b64(&v);
|
||||
|
||||
// The phone opens it with its passkey and the master password.
|
||||
@@ -130,13 +140,18 @@ fn a_phone_pairs_then_opens_and_approves() {
|
||||
let req_pub = BASE64.encode(&requester.public());
|
||||
assert_eq!(call(json!({ "op": "match_code", "pub": req_pub }))["match"], requester.match_code());
|
||||
let ctx = format!("approval:{approval_id}:r1");
|
||||
let wrong_key = call(json!({ "op": "phone_approve", "wrapped": wrapped, "prf": BASE64.encode(&[8u8; 32]), "pub": req_pub, "context": ctx }));
|
||||
let wrong_key = call(
|
||||
json!({ "op": "phone_approve", "wrapped": wrapped, "prf": BASE64.encode(&[8u8; 32]), "pub": req_pub, "context": ctx }),
|
||||
);
|
||||
assert_eq!(wrong_key["ok"], false);
|
||||
let r = call(json!({ "op": "phone_approve", "wrapped": wrapped, "prf": prf64, "pub": req_pub, "context": ctx }));
|
||||
let sealed = BASE64.decode(r["sealed"].as_str().unwrap().as_bytes()).unwrap();
|
||||
let m = requester.open(&sealed, ctx.as_bytes()).unwrap();
|
||||
let pw = SecretString::from(PASSWORD);
|
||||
assert!(Vault::open_with(&v.to_bytes().unwrap(), Unlock::Password(&pw), Unlock::Material(FactorKind::Approval, &m)).is_ok());
|
||||
assert!(
|
||||
Vault::open_with(&v.to_bytes().unwrap(), Unlock::Password(&pw), Unlock::Material(FactorKind::Approval, &m))
|
||||
.is_ok()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -144,7 +159,9 @@ fn a_phone_pairs_as_a_domi_device() {
|
||||
// The PC waits with a one-time key; the phone seals its device public key and label to it.
|
||||
let pc = Recipient::generate();
|
||||
let device = data_encoding::BASE64URL_NOPAD.encode(&[5u8; 32]);
|
||||
let r = call(json!({ "op": "domi_pair", "pub": BASE64.encode(&pc.public()), "id": "abc_-1", "label": " Pixel\n", "device": device }));
|
||||
let r = call(
|
||||
json!({ "op": "domi_pair", "pub": BASE64.encode(&pc.public()), "id": "abc_-1", "label": " Pixel\n", "device": device }),
|
||||
);
|
||||
assert_eq!(r["ok"], true, "{r}");
|
||||
let sealed = BASE64.decode(r["sealed"].as_str().unwrap().as_bytes()).unwrap();
|
||||
assert_eq!(r["code"], custos_seal::sender_code(&sealed).unwrap());
|
||||
@@ -153,6 +170,16 @@ fn a_phone_pairs_as_a_domi_device() {
|
||||
assert_eq!(plain, json!({ "label": "Pixel", "device": device }));
|
||||
// Malformed: a device key of the wrong length, an id that would change the context.
|
||||
let short = data_encoding::BASE64URL_NOPAD.encode(&[5u8; 31]);
|
||||
assert_eq!(call(json!({ "op": "domi_pair", "pub": BASE64.encode(&pc.public()), "id": "abc", "label": "x", "device": short }))["ok"], false);
|
||||
assert_eq!(call(json!({ "op": "domi_pair", "pub": BASE64.encode(&pc.public()), "id": "a:b", "label": "x", "device": device }))["ok"], false);
|
||||
assert_eq!(
|
||||
call(
|
||||
json!({ "op": "domi_pair", "pub": BASE64.encode(&pc.public()), "id": "abc", "label": "x", "device": short })
|
||||
)["ok"],
|
||||
false
|
||||
);
|
||||
assert_eq!(
|
||||
call(
|
||||
json!({ "op": "domi_pair", "pub": BASE64.encode(&pc.public()), "id": "a:b", "label": "x", "device": device })
|
||||
)["ok"],
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user