83 lines
2.3 KiB
Rust
83 lines
2.3 KiB
Rust
// 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::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()
|
|
}
|