[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,6 +23,7 @@ pub mod identity;
|
||||
mod item;
|
||||
mod save;
|
||||
mod secret;
|
||||
pub mod signing;
|
||||
|
||||
pub use item::{Attachment, Field, HISTORY_LIMIT, HistoryEntry, Item, ItemType, new_id, now_ms};
|
||||
pub use save::{CrashPoint, save_atomic, save_atomic_crashing};
|
||||
@@ -70,6 +71,10 @@ pub enum Error {
|
||||
NotACert,
|
||||
#[error("those words are not the ones in the recovery phrase")]
|
||||
WrongWords,
|
||||
#[error("this vault already has {0} signing keys")]
|
||||
SigningExists(&'static str),
|
||||
#[error("signing keys: {0}")]
|
||||
Signing(String),
|
||||
#[error("{0}")]
|
||||
Io(#[from] std::io::Error),
|
||||
}
|
||||
@@ -261,6 +266,8 @@ struct Body {
|
||||
factors: Vec<FactorJson>,
|
||||
#[serde(default)]
|
||||
identity: Option<identity::RootJson>,
|
||||
#[serde(default)]
|
||||
signing: Option<signing::StoreJson>,
|
||||
}
|
||||
|
||||
/// What `to_bytes` serializes: borrows the items instead of cloning every secret.
|
||||
@@ -272,6 +279,8 @@ struct BodyRef<'a> {
|
||||
factors: Vec<FactorJson>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
identity: Option<identity::RootJson>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
signing: Option<signing::StoreJson>,
|
||||
}
|
||||
|
||||
/// An unlocked vault: the vault key and the decrypted items, both wiped on drop.
|
||||
@@ -286,6 +295,8 @@ pub struct Vault {
|
||||
kdf_new: KdfParams,
|
||||
/// The Domi identity's root key (docs/IDENTITY.md), never handed out.
|
||||
identity: Option<identity::Root>,
|
||||
/// Signing keys and their audit log (docs/SIGNING.md); no key is ever handed out.
|
||||
signing: signing::Store,
|
||||
}
|
||||
|
||||
impl fmt::Debug for Vault {
|
||||
@@ -385,6 +396,7 @@ impl Vault {
|
||||
factors: Vec::new(),
|
||||
kdf_new: kdf,
|
||||
identity: None,
|
||||
signing: signing::Store::default(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -401,6 +413,7 @@ impl Vault {
|
||||
factors: Vec::new(),
|
||||
kdf_new: kdf,
|
||||
identity: None,
|
||||
signing: signing::Store::default(),
|
||||
};
|
||||
v.add_factor(a, label_a)?;
|
||||
v.add_factor(b, label_b)?;
|
||||
@@ -433,6 +446,7 @@ impl Vault {
|
||||
factors: vec![rec],
|
||||
kdf_new: KdfParams::DEFAULT,
|
||||
identity: None,
|
||||
signing: signing::Store::default(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -554,7 +568,20 @@ impl Vault {
|
||||
Some(j) => Some(identity::Root::from_json(j).ok_or(Error::Corrupted)?),
|
||||
None => None,
|
||||
};
|
||||
Ok(Vault { key, version: header.version, slots: header.slots, items: body.items, factors, kdf_new, identity })
|
||||
let signing = match body.signing {
|
||||
Some(j) => signing::Store::from_json(j).ok_or(Error::Corrupted)?,
|
||||
None => signing::Store::default(),
|
||||
};
|
||||
Ok(Vault {
|
||||
key,
|
||||
version: header.version,
|
||||
slots: header.slots,
|
||||
items: body.items,
|
||||
factors,
|
||||
kdf_new,
|
||||
identity,
|
||||
signing,
|
||||
})
|
||||
}
|
||||
|
||||
/// Wraps the vault key for device unlock with key material only this device can produce (e.g. a
|
||||
@@ -588,6 +615,7 @@ impl Vault {
|
||||
items: &self.items,
|
||||
factors: self.factors.iter().map(FactorRec::json).collect(),
|
||||
identity: self.identity.as_ref().map(identity::Root::json),
|
||||
signing: self.signing.json(),
|
||||
};
|
||||
serde_json::to_writer(&mut *json, &body).map_err(|e| Error::Io(std::io::Error::other(e.to_string())))?;
|
||||
let sealed = crypto::seal(&self.key, &body_nonce, &json, &out);
|
||||
|
||||
@@ -353,7 +353,13 @@ fn clean_list(list: &[String]) -> Result<Vec<String>, Error> {
|
||||
impl Vault {
|
||||
/// Creates the release set (`domi-release`, `domi-root`, `domi-code-signing`) or the test set (`domi-test`,
|
||||
/// `domi-test-root`, `domi-test-code-signing`). The caller saves. Slow: two RSA-3072 keys.
|
||||
pub fn signing_create(&mut self, role: Role, pipelines: &[String], repos: &[String], now: i64) -> Result<Vec<String>, Error> {
|
||||
pub fn signing_create(
|
||||
&mut self,
|
||||
role: Role,
|
||||
pipelines: &[String],
|
||||
repos: &[String],
|
||||
now: i64,
|
||||
) -> Result<Vec<String>, Error> {
|
||||
if self.signing.keys.iter().any(|k| k.role == role) {
|
||||
return Err(Error::SigningExists(match role {
|
||||
Role::Release => "release",
|
||||
@@ -363,8 +369,22 @@ impl Vault {
|
||||
let pipelines = clean_list(pipelines)?;
|
||||
let repos = clean_list(repos)?;
|
||||
let (ed_id, root_id, leaf_id, ed_name, root_name, leaf_name) = match role {
|
||||
Role::Release => ("domi-release", "domi-root", "domi-code-signing", "Domi release", "Domi Household Root CA", "Domi Code Signing"),
|
||||
Role::Test => ("domi-test", "domi-test-root", "domi-test-code-signing", "Domi test", "Domi Test Root (never trusted)", "Domi Test Code Signing"),
|
||||
Role::Release => (
|
||||
"domi-release",
|
||||
"domi-root",
|
||||
"domi-code-signing",
|
||||
"Domi release",
|
||||
"Domi Household Root CA",
|
||||
"Domi Code Signing",
|
||||
),
|
||||
Role::Test => (
|
||||
"domi-test",
|
||||
"domi-test-root",
|
||||
"domi-test-code-signing",
|
||||
"Domi test",
|
||||
"Domi Test Root (never trusted)",
|
||||
"Domi Test Code Signing",
|
||||
),
|
||||
};
|
||||
let ed = custos_sign::new_ed25519();
|
||||
let root = custos_sign::new_root(root_name, 10).map_err(|e| Error::Signing(e.to_string()))?;
|
||||
@@ -387,8 +407,17 @@ impl Vault {
|
||||
let mut ed_key = key(ed_id, KeyKind::Ed25519, ed_name, Secret::Ed25519(ed.secret), None, None, true);
|
||||
ed_key.public = Some(ed.public);
|
||||
ed_key.key_id = Some(ed.key_id);
|
||||
let root_key = key(root_id, KeyKind::RootCa, root_name, Secret::Rsa(root.secret), Some(root.cert_der), None, false);
|
||||
let leaf_key = key(leaf_id, KeyKind::CodeSigning, leaf_name, Secret::Rsa(leaf.secret), Some(leaf.cert_der), Some(root_id), true);
|
||||
let root_key =
|
||||
key(root_id, KeyKind::RootCa, root_name, Secret::Rsa(root.secret), Some(root.cert_der), None, false);
|
||||
let leaf_key = key(
|
||||
leaf_id,
|
||||
KeyKind::CodeSigning,
|
||||
leaf_name,
|
||||
Secret::Rsa(leaf.secret),
|
||||
Some(leaf.cert_der),
|
||||
Some(root_id),
|
||||
true,
|
||||
);
|
||||
self.signing.keys.extend([ed_key, root_key, leaf_key]);
|
||||
Ok(vec![ed_id.into(), root_id.into(), leaf_id.into()])
|
||||
}
|
||||
@@ -421,7 +450,12 @@ impl Vault {
|
||||
/// Changes who may ask for a key. The caller saves.
|
||||
pub fn signing_set_scope(&mut self, id: &str, pipelines: &[String], repos: &[String]) -> Result<(), Error> {
|
||||
let (pipelines, repos) = (clean_list(pipelines)?, clean_list(repos)?);
|
||||
let k = self.signing.keys.iter_mut().find(|k| k.id == id).ok_or_else(|| Error::Signing(format!("no key `{id}`")))?;
|
||||
let k = self
|
||||
.signing
|
||||
.keys
|
||||
.iter_mut()
|
||||
.find(|k| k.id == id)
|
||||
.ok_or_else(|| Error::Signing(format!("no key `{id}`")))?;
|
||||
if k.kind == KeyKind::RootCa {
|
||||
return Err(Error::Signing("a root CA signs no requests, so it has no scope".into()));
|
||||
}
|
||||
@@ -442,7 +476,8 @@ impl Vault {
|
||||
let k = self.signing.get(&req.key).ok_or_else(|| Refusal::NoKey(req.key.clone()))?;
|
||||
let fits = matches!(
|
||||
(k.kind, req.kind),
|
||||
(KeyKind::Ed25519, SignKind::Minisign | SignKind::Manifest) | (KeyKind::CodeSigning, SignKind::Authenticode)
|
||||
(KeyKind::Ed25519, SignKind::Minisign | SignKind::Manifest)
|
||||
| (KeyKind::CodeSigning, SignKind::Authenticode)
|
||||
);
|
||||
if k.kind == KeyKind::RootCa {
|
||||
return Err(Refusal::Invalid(format!("`{}` is a root CA: it signs only the certificate it issued", k.id)));
|
||||
@@ -499,10 +534,14 @@ impl Vault {
|
||||
let k = self.signing.get(&req.key).expect("checked");
|
||||
let seconds = (now.max(0) / 1000) as u64;
|
||||
let bytes = match (&k.secret, req.kind) {
|
||||
(Secret::Ed25519(s), SignKind::Minisign) => {
|
||||
custos_sign::minisign(s, &k.key_id.expect("ed25519 keys have an id"), &req.digest, &req.context.file, seconds)
|
||||
.map(String::into_bytes)
|
||||
}
|
||||
(Secret::Ed25519(s), SignKind::Minisign) => custos_sign::minisign(
|
||||
s,
|
||||
&k.key_id.expect("ed25519 keys have an id"),
|
||||
&req.digest,
|
||||
&req.context.file,
|
||||
seconds,
|
||||
)
|
||||
.map(String::into_bytes),
|
||||
(Secret::Ed25519(s), SignKind::Manifest) => custos_sign::manifest(
|
||||
s,
|
||||
&k.key_id.expect("ed25519 keys have an id"),
|
||||
@@ -536,7 +575,14 @@ impl Vault {
|
||||
self.signing_record(req, outcome, reason, None, now);
|
||||
}
|
||||
|
||||
fn signing_record(&mut self, req: &Request, outcome: Outcome, reason: Option<String>, signature: Option<String>, now: i64) {
|
||||
fn signing_record(
|
||||
&mut self,
|
||||
req: &Request,
|
||||
outcome: Outcome,
|
||||
reason: Option<String>,
|
||||
signature: Option<String>,
|
||||
now: i64,
|
||||
) {
|
||||
let mut context = req.context.clone();
|
||||
if req.kind == SignKind::Manifest {
|
||||
context.file = "SHA256SUMS".into();
|
||||
@@ -558,3 +604,115 @@ impl Vault {
|
||||
&self.signing.audit
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
//! Mandatory test 2 for the core: no API path returns a private key. It lives here because only this module can
|
||||
//! see the secrets to look for.
|
||||
use super::*;
|
||||
use crate::{KdfParams, SecretString};
|
||||
|
||||
fn s(v: &[&str]) -> Vec<String> {
|
||||
v.iter().map(|x| x.to_string()).collect()
|
||||
}
|
||||
|
||||
fn req(key: &str, kind: SignKind, digest: usize) -> Request {
|
||||
Request {
|
||||
key: key.into(),
|
||||
kind,
|
||||
digest: vec![5; digest],
|
||||
manifest: None,
|
||||
context: Context {
|
||||
repo: "Arca".into(),
|
||||
pipeline: "release".into(),
|
||||
trigger: "tag".into(),
|
||||
file: "a.exe".into(),
|
||||
..Default::default()
|
||||
},
|
||||
via: Via::Local,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_api_path_returns_a_private_key() {
|
||||
let mut v =
|
||||
Vault::create(&SecretString::from("pw"), KdfParams { m_cost_kib: 64, t_cost: 1, p_cost: 1 }).unwrap();
|
||||
v.signing_create(Role::Release, &s(&["*"]), &s(&["*"]), 1).unwrap();
|
||||
v.signing_create(Role::Test, &s(&["*"]), &s(&["*"]), 1).unwrap();
|
||||
let secrets: Vec<Vec<u8>> = v
|
||||
.signing
|
||||
.keys
|
||||
.iter()
|
||||
.map(|k| match &k.secret {
|
||||
Secret::Ed25519(s) => s.expose().to_vec(),
|
||||
Secret::Rsa(s) => s.expose().to_vec(),
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(secrets.len(), 6);
|
||||
|
||||
// Every public way out: views (serialised and printed), certificates, minisign public keys, checks,
|
||||
// signatures of every kind, refusals, the audit log, errors, and the Debug of the vault's own store.
|
||||
let mut out = String::new();
|
||||
let views = v.signing_keys();
|
||||
out += &serde_json::to_string(&views).unwrap();
|
||||
out += &format!("{views:?}{:?}", v.signing);
|
||||
for k in &views {
|
||||
if let Ok(c) = v.signing_export_cert(&k.id) {
|
||||
out += &data_encoding::HEXLOWER.encode(&c);
|
||||
}
|
||||
}
|
||||
let mut reqs =
|
||||
vec![req("domi-release", SignKind::Minisign, 64), req("domi-code-signing", SignKind::Authenticode, 32)];
|
||||
reqs.push(req("domi-root", SignKind::Authenticode, 32));
|
||||
reqs.push(req("domi-test", SignKind::Minisign, 64));
|
||||
for r in &reqs {
|
||||
out += &format!("{:?}", v.signing_check(r));
|
||||
for approval in [Approval::Mark, Approval::Auto] {
|
||||
match v.signing_sign(r, approval, 2) {
|
||||
Ok(signed) => out += &format!("{signed:?}{}", data_encoding::HEXLOWER.encode(&signed.bytes)),
|
||||
Err(e) => out += &format!("{e:?}{e}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
out += &serde_json::to_string(v.signing_audit()).unwrap();
|
||||
out += &format!("{:?}", v.signing_audit());
|
||||
out += &format!("{:?}", v.signing_create(Role::Release, &[], &[], 3));
|
||||
|
||||
for secret in &secrets {
|
||||
for enc in [
|
||||
data_encoding::HEXLOWER.encode(secret),
|
||||
data_encoding::HEXUPPER.encode(secret),
|
||||
data_encoding::BASE64.encode(secret),
|
||||
data_encoding::BASE64URL_NOPAD.encode(secret),
|
||||
] {
|
||||
assert!(!out.contains(&enc), "a secret appears in an output");
|
||||
}
|
||||
}
|
||||
// Partial copies too: every 16-byte window of the numbers that are actually private. (A PKCS#8 RSA key
|
||||
// also holds the public modulus and DER headers, which the certificate rightly shows.)
|
||||
for private in private_parts(&secrets) {
|
||||
for w in private.windows(16).step_by(8) {
|
||||
assert!(!out.contains(&data_encoding::HEXLOWER.encode(w)), "part of a secret appears in an output");
|
||||
assert!(!out.contains(&data_encoding::BASE64.encode(w)[..20]), "part of a secret appears in an output");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Ed25519 seeds as they are; for RSA the private exponent and the primes.
|
||||
fn private_parts(secrets: &[Vec<u8>]) -> Vec<Vec<u8>> {
|
||||
use rsa::pkcs8::DecodePrivateKey;
|
||||
use rsa::traits::PrivateKeyParts;
|
||||
let mut out = Vec::new();
|
||||
for s in secrets {
|
||||
if s.len() == 32 {
|
||||
out.push(s.clone());
|
||||
} else {
|
||||
let k = rsa::RsaPrivateKey::from_pkcs8_der(s).unwrap();
|
||||
out.push(k.d().to_bytes_be());
|
||||
out.extend(k.primes().iter().map(|p| p.to_bytes_be()));
|
||||
}
|
||||
}
|
||||
assert!(out.len() >= 2 + 4 * 3, "seeds, exponents and primes");
|
||||
out
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user