Files
domi-pki/tests/common/mod.rs
T
mnovak acfae4b6e8 [repo] Licence: CPAL-1.0 file headers instead of the MPL-2.0 notice
Each source file's three-line MPL-2.0 notice becomes an SPDX CPAL-1.0 header with the copyright
line; the Exhibit A notice itself is in LICENSE (CPAL 3.5). Shell scripts' SPDX ids follow.
Comments only: no code changes.
2026-11-23 00:19:31 +01:00

82 lines
2.2 KiB
Rust

// SPDX-License-Identifier: CPAL-1.0
// Copyright (c) 2026 Marek Novák. Exhibit A notice: LICENSE at the repository root.
#![allow(dead_code, clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use std::cell::Cell;
use domi_pki::{self as pki, Intermediate, KeyPair, Signer, SignerError};
pub const NOW: i64 = 1_800_000_000;
pub const DAY: i64 = 86_400;
/// A root key that only signs through the `Signer` trait (as Custos does), and counts how often.
pub struct CustosLike {
key: KeyPair,
pub signed: Cell<u32>,
}
impl CustosLike {
pub fn new() -> CustosLike {
CustosLike {
key: pki::generate_key().unwrap(),
signed: Cell::new(0),
}
}
}
impl Signer for CustosLike {
fn spki_der(&self) -> Vec<u8> {
self.key.spki_der()
}
fn sign(&self, msg: &[u8]) -> Result<Vec<u8>, SignerError> {
self.signed.set(self.signed.get() + 1);
self.key.sign(msg)
}
}
/// A full CA: the root in a "Custos", one Ianitor and one agents intermediate.
pub struct Ca {
pub root: CustosLike,
pub root_pem: String,
pub ianitor: Intermediate,
pub agents: Intermediate,
}
impl Ca {
pub fn root_der(&self) -> Vec<u8> {
pki::chain_der(&self.root_pem).unwrap().remove(0)
}
}
pub fn intermediate(root: &dyn Signer, root_pem: &str, 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, &csr, cn, NOW).unwrap();
Intermediate::new(&pem, key).unwrap()
}
pub fn ca() -> Ca {
let root = CustosLike::new();
let root_pem = pki::create_root(&root, NOW).unwrap();
let ianitor = intermediate(&root, &root_pem, pki::IANITOR_INTERMEDIATE_CN);
let agents = intermediate(&root, &root_pem, pki::AGENTS_INTERMEDIATE_CN);
Ca {
root,
root_pem,
ianitor,
agents,
}
}
/// An agent's own key and CSR, as Legatus makes them.
pub fn agent_csr(agent: &str) -> (KeyPair, String) {
let key = pki::generate_key().unwrap();
let csr = pki::agent_csr(&key, agent).unwrap();
(key, csr)
}
pub fn ders(pem: &str) -> Vec<Vec<u8>> {
pki::chain_der(pem).unwrap()
}