// This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. #![allow(dead_code, clippy::unwrap_used, clippy::expect_used, clippy::panic)] use std::io::Cursor; use domi_pki::{self as pki, Intermediate}; pub const NOW: i64 = 1_800_000_000; pub const DAY: i64 = 86_400; /// A Domi root (local, as in tests) with Ianitor's and the agents' intermediates. pub struct Ca { pub root_pem: String, pub root_key: pki::KeyPair, pub agents: Intermediate, pub ianitor: Intermediate, } impl Ca { pub fn root_der(&self) -> Vec { pki::chain_der(&self.root_pem).unwrap().remove(0) } /// A fresh agent certificate chain (DER, leaf first) for `team`/`agent`, issued at `at`. pub fn agent(&self, team: &str, agent: &str, at: i64) -> (Vec>, String) { let key = pki::generate_key().unwrap(); let csr = pki::agent_csr(&key, "legatus").unwrap(); let a = pki::issue_agent(&self.agents, &csr, team, agent, at).unwrap(); (pki::chain_der(&a.chain_pem).unwrap(), a.serial_hex) } } fn intermediate(root_pem: &str, root_key: &pki::KeyPair, cn: &str) -> Intermediate { let key = pki::generate_key().unwrap(); let csr = pki::intermediate_csr(&key, cn).unwrap(); let pem = pki::sign_intermediate(root_pem, root_key, &csr, cn, NOW).unwrap(); Intermediate::new(&pem, key).unwrap() } pub fn ca() -> Ca { let (root_pem, root_key) = pki::create_root_local(NOW).unwrap(); let agents = intermediate(&root_pem, &root_key, pki::AGENTS_INTERMEDIATE_CN); let ianitor = intermediate(&root_pem, &root_key, pki::IANITOR_INTERMEDIATE_CN); Ca { root_pem, root_key, agents, ianitor, } } /// A team release key, as Custos keeps it. pub struct ReleaseKey(pub minisign::KeyPair); impl ReleaseKey { pub fn new() -> ReleaseKey { ReleaseKey(minisign::KeyPair::generate_unencrypted_keypair().unwrap()) } /// The base64 key line Legatus pins. pub fn public(&self) -> String { self.0.pk.to_base64() } /// A minisign signature (prehashed, as Custos signs) of `bytes`. pub fn sign(&self, bytes: &[u8]) -> String { minisign::sign( Some(&self.0.pk), &self.0.sk, Cursor::new(bytes), Some("release"), None, ) .unwrap() .into_string() } } pub fn sha256_hex(b: &[u8]) -> String { ring::digest::digest(&ring::digest::SHA256, b) .as_ref() .iter() .map(|x| format!("{x:02x}")) .collect() } /// A manifest for one artifact, as Fabrica's release object would write it. pub fn manifest(team: &str, app: &str, version: &str, name: &str, artifact: &[u8]) -> String { format!( r#"{{"v":1,"team":"{team}","app":"{app}","version":"{version}","created_at":{NOW},"artifacts":[{{"name":"{name}","sha256":"{}","size":{}}}]}}"#, sha256_hex(artifact), artifact.len() ) }