[core] Tests first: the Domi CA chains to the root, a leaf cannot sign, expired and wrong-name certs are rejected (red)

This commit is contained in:
2026-11-07 22:40:56 +01:00
parent 7cb5715580
commit cde0c3ea3b
5 changed files with 405 additions and 0 deletions
+1
View File
@@ -10,6 +10,7 @@
pub mod compile;
pub mod edge_config;
pub mod pki;
mod error;
mod model;
pub mod routes;
+75
View File
@@ -0,0 +1,75 @@
// 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/.
//! The Domi CA. Interface only; the implementation follows the tests (red commit).
pub const ROOT_DAYS: u32 = 3653;
pub const INTERMEDIATE_DAYS: u32 = 365;
pub const SERVER_DAYS: u32 = 90;
pub const SERVER_RENEW_DAYS: u32 = 30;
pub const DEVICE_DAYS: u32 = 365;
pub const ROOT_CN: &str = "Domi PKI Root";
pub const INTERMEDIATE_CN: &str = "Ianitor Domi Intermediate";
pub const KEY_KDF_ITERATIONS: u32 = 0;
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum PkiError {
#[error("not built yet")]
NotBuilt,
#[error("bad certificate")]
BadCertificate(String),
#[error("bad csr")]
BadCsr,
#[error("csr subject")]
CsrSubject,
#[error("bad key")]
BadKey,
#[error("not a ca")]
NotCa,
#[error("key mismatch")]
KeyMismatch,
#[error("issuer expired")]
IssuerExpired,
#[error("wrong password")]
WrongPassword,
}
#[derive(Debug)]
pub struct KeyPair;
impl KeyPair {
pub fn serialize_pem(&self) -> String { String::new() }
pub fn from_pem(_: &str) -> Result<KeyPair, PkiError> { Err(PkiError::NotBuilt) }
pub fn public_key_der(&self) -> Vec<u8> { Vec::new() }
}
pub fn generate_key() -> Result<KeyPair, PkiError> { Err(PkiError::NotBuilt) }
pub fn create_root(_: i64) -> Result<(String, KeyPair), PkiError> { Err(PkiError::NotBuilt) }
pub fn intermediate_csr(_: &KeyPair, _: &str) -> Result<String, PkiError> { Err(PkiError::NotBuilt) }
pub fn sign_intermediate(_: &str, _: &KeyPair, _: &str, _: i64) -> Result<String, PkiError> { Err(PkiError::NotBuilt) }
#[derive(Debug)]
pub struct Intermediate;
impl Intermediate {
pub fn new(_: &str, _: KeyPair) -> Result<Intermediate, PkiError> { Err(PkiError::NotBuilt) }
pub fn cert_pem(&self) -> &str { "" }
pub fn not_after(&self) -> i64 { 0 }
}
#[derive(Debug)]
pub struct Issued { pub cert_pem: String, pub chain_pem: String, pub key_pem: String }
pub fn issue_server(_: &Intermediate, _: &str, _: i64) -> Result<Issued, PkiError> { Err(PkiError::NotBuilt) }
pub fn issue_device(_: &Intermediate, _: &str, _: i64) -> Result<Issued, PkiError> { Err(PkiError::NotBuilt) }
#[derive(Debug)]
pub struct CertInfo { pub subject_cn: String, pub not_after: i64, pub is_ca: bool, pub path_len: Option<u32>, pub dns_names: Vec<String> }
pub fn inspect(_: &str) -> Result<CertInfo, PkiError> { Err(PkiError::NotBuilt) }
pub fn public_key_der(_: &str) -> Result<Vec<u8>, PkiError> { Err(PkiError::NotBuilt) }
pub fn issued_by(_: &str, _: &str) -> Result<bool, PkiError> { Err(PkiError::NotBuilt) }
pub fn match_code(_: &[u8]) -> String { String::new() }
pub fn encrypt_key_with(_: &KeyPair, _: &[u8], _: u32) -> Result<String, PkiError> { Err(PkiError::NotBuilt) }
pub fn decrypt_key(_: &str, _: &[u8]) -> Result<KeyPair, PkiError> { Err(PkiError::NotBuilt) }
#[doc(hidden)]
pub mod testing {
use super::*;
pub fn sign_as(_: &str, _: KeyPair, _: &str, _: i64) -> Result<String, PkiError> { Err(PkiError::NotBuilt) }
pub fn sub_ca(_: &Intermediate, _: &KeyPair, _: i64) -> Result<String, PkiError> { Err(PkiError::NotBuilt) }
pub fn greedy_csr(_: &KeyPair, _: &str) -> Result<String, PkiError> { Err(PkiError::NotBuilt) }
pub fn tamper_csr(s: &str) -> String { s.to_owned() }
}