159 lines
8.3 KiB
Rust
159 lines
8.3 KiB
Rust
//! Two factors in the browser: opening a version-2 vault with a sealed answer or a passkey's PRF output,
|
|
//! and a phone's pairing and approvals (docs/FACTORS.md, docs/SEAL.md).
|
|
|
|
use custos_seal::Recipient;
|
|
use custos_vault::{FactorKind, Item, ItemType, KdfParams, SecretString, Unlock, Vault};
|
|
use data_encoding::BASE64;
|
|
use serde_json::{Value, json};
|
|
|
|
const FAST: KdfParams = KdfParams { m_cost_kib: 64, t_cost: 1, p_cost: 1 };
|
|
const PASSWORD: &str = "Test4-Vault7-Only2";
|
|
const SECRET: &str = "s3cret-Ž!";
|
|
|
|
fn call(v: Value) -> Value {
|
|
custos_wasm::handle(v.to_string().as_bytes())
|
|
}
|
|
|
|
/// 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 login = Item::new(ItemType::WebLogin, "GitHub", 0);
|
|
login.set("password", SECRET, 0);
|
|
v.add(login);
|
|
v
|
|
}
|
|
|
|
fn b64(v: &Vault) -> String {
|
|
BASE64.encode(&v.to_bytes().unwrap())
|
|
}
|
|
|
|
#[test]
|
|
fn factors_from_the_header() {
|
|
let v = vault(&[3; 32]);
|
|
let r = call(json!({ "op": "factors", "vault": b64(&v) }));
|
|
assert_eq!(r["version"], 2, "{r}");
|
|
let kinds: Vec<&str> = r["factors"].as_array().unwrap().iter().map(|f| f["kind"].as_str().unwrap()).collect();
|
|
assert_eq!(kinds, ["password", "totp"]);
|
|
let totp = v.factors().into_iter().find(|f| f.kind == FactorKind::Totp).unwrap();
|
|
assert_eq!(r["factors"][1]["id"], totp.id.as_str());
|
|
}
|
|
|
|
#[test]
|
|
fn open_with_password_and_a_sealed_totp_release() {
|
|
let material = [3u8; 32];
|
|
let v = vault(&material);
|
|
let file = b64(&v);
|
|
let totp = v.factors().into_iter().find(|f| f.kind == FactorKind::Totp).unwrap().id;
|
|
let context = format!("totp:{totp}");
|
|
|
|
let me = call(json!({ "op": "recipient" }));
|
|
let public = BASE64.decode(me["pub"].as_str().unwrap().as_bytes()).unwrap();
|
|
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 password = json!({ "kind": "password", "password": PASSWORD });
|
|
|
|
// Only the right context opens it.
|
|
let wrong = call(json!({ "op": "open_with", "vault": file, "factors": [password, sealed_in("totp:other")] }));
|
|
assert_eq!(wrong["ok"], false);
|
|
// The key is used once, even when the answer was refused.
|
|
let again = call(json!({ "op": "open_with", "vault": file, "factors": [password, sealed_in(&context)] }));
|
|
assert_eq!(again["error"], "That request expired. Ask again.");
|
|
|
|
let me = call(json!({ "op": "recipient" }));
|
|
let public = BASE64.decode(me["pub"].as_str().unwrap().as_bytes()).unwrap();
|
|
let sealed = BASE64.encode(&custos_seal::seal(&public, context.as_bytes(), &material).unwrap());
|
|
let ok = call(json!({ "op": "open_with", "vault": file, "factors": [
|
|
{ "kind": "sealed", "factor": "totp", "pub": me["pub"], "sealed": sealed, "context": context },
|
|
password,
|
|
]}));
|
|
assert_eq!(ok["ok"], true, "{ok}");
|
|
assert_eq!(ok["items"].as_array().unwrap().len(), 1);
|
|
assert!(!ok.to_string().contains(SECRET));
|
|
call(json!({ "op": "lock" }));
|
|
|
|
// One factor, or the same one twice, never opens it.
|
|
for factors in [json!([password]), json!([password, password])] {
|
|
let r = call(json!({ "op": "open_with", "vault": file, "factors": factors }));
|
|
assert_eq!(r["ok"], false, "{r}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_phone_pairs_then_opens_and_approves() {
|
|
let mut v = vault(&[3; 32]);
|
|
let prf = [9u8; 32];
|
|
let prf64 = BASE64.encode(&prf);
|
|
|
|
// 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 }));
|
|
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");
|
|
assert!(pc.open(&sealed, b"pair:other").is_err());
|
|
let plain: Value = serde_json::from_slice(&pc.open(&sealed, context.as_bytes()).unwrap()).unwrap();
|
|
assert_eq!((plain["label"].as_str(), plain["passkey"].as_str()), (Some("Phone"), Some(prf64.as_str())));
|
|
let approval = BASE64.decode(plain["approval"].as_str().unwrap().as_bytes()).unwrap();
|
|
assert_eq!(approval.len(), 32);
|
|
let wrapped = r["wrapped"].as_str().unwrap();
|
|
assert!(!r.to_string().contains(plain["approval"].as_str().unwrap()), "the material leaves only sealed or wrapped");
|
|
|
|
// 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 file = b64(&v);
|
|
|
|
// The phone opens it with its passkey and the master password.
|
|
let r = call(json!({ "op": "open_with", "vault": file, "factors": [
|
|
{ "kind": "material", "factor": "passkey", "material": prf64 },
|
|
{ "kind": "password", "password": PASSWORD },
|
|
]}));
|
|
assert_eq!(r["ok"], true, "{r}");
|
|
call(json!({ "op": "lock" }));
|
|
// Its two factors never open it together.
|
|
let me = call(json!({ "op": "recipient" }));
|
|
let public = BASE64.decode(me["pub"].as_str().unwrap().as_bytes()).unwrap();
|
|
let sealed = BASE64.encode(&custos_seal::seal(&public, b"approval:x:y", &approval).unwrap());
|
|
let r = call(json!({ "op": "open_with", "vault": file, "factors": [
|
|
{ "kind": "material", "factor": "passkey", "material": prf64 },
|
|
{ "kind": "sealed", "factor": "approval", "pub": me["pub"], "sealed": sealed, "context": "approval:x:y" },
|
|
]}));
|
|
assert_eq!(r["error"], "these two factors are kept on the same device; use one of them with another factor");
|
|
|
|
// The PC asks the phone; the phone approves with its passkey.
|
|
let requester = Recipient::generate();
|
|
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 }));
|
|
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());
|
|
}
|
|
|
|
#[test]
|
|
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 }));
|
|
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());
|
|
assert!(pc.open(&sealed, b"domi-pair:other").is_err(), "the box is for this pairing only");
|
|
let plain: Value = serde_json::from_slice(&pc.open(&sealed, b"domi-pair:abc_-1").unwrap()).unwrap();
|
|
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);
|
|
}
|