94 lines
3.4 KiB
Rust
94 lines
3.4 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/.
|
|
|
|
//! Helpers for tests that need certificates the Domi CA itself would never make.
|
|
|
|
use rcgen::{
|
|
BasicConstraints, CertificateParams, ExtendedKeyUsagePurpose, IsCa, Issuer, KeyUsagePurpose,
|
|
};
|
|
|
|
use crate::ca::dn;
|
|
use crate::inspect::{armor, csr_der, first_pem};
|
|
use crate::*;
|
|
|
|
fn build(e: rcgen::Error) -> PkiError {
|
|
PkiError::Build(e.to_string())
|
|
}
|
|
|
|
fn time(unix: i64) -> Result<time::OffsetDateTime, PkiError> {
|
|
time::OffsetDateTime::from_unix_timestamp(unix).map_err(|e| PkiError::Build(e.to_string()))
|
|
}
|
|
|
|
/// A server certificate for `name`, signed with `key` as if the first certificate of
|
|
/// `issuer_pem` were a CA (for "a leaf cannot sign").
|
|
pub fn sign_as(issuer_pem: &str, key: KeyPair, name: &str, now: i64) -> Result<String, PkiError> {
|
|
let leaf_key = generate_key()?;
|
|
let mut p = CertificateParams::new(vec![name.to_owned()]).map_err(build)?;
|
|
p.distinguished_name = dn(name);
|
|
p.extended_key_usages = vec![ExtendedKeyUsagePurpose::ServerAuth];
|
|
p.not_before = time(now - 300)?;
|
|
p.not_after = time(now + 30 * DAY)?;
|
|
let issuer = Issuer::from_ca_cert_pem(&first_pem(issuer_pem)?, key.0).map_err(build)?;
|
|
Ok(p.signed_by(&leaf_key.0, &issuer).map_err(build)?.pem())
|
|
}
|
|
|
|
/// A CA below the intermediate (which pathLen 0 forbids).
|
|
pub fn sub_ca(int: &Intermediate, key: &KeyPair, now: i64) -> Result<String, PkiError> {
|
|
let mut p = CertificateParams::default();
|
|
p.distinguished_name = dn("Sub CA");
|
|
p.is_ca = IsCa::Ca(BasicConstraints::Unconstrained);
|
|
p.key_usages = vec![KeyUsagePurpose::KeyCertSign];
|
|
p.not_before = time(now - 300)?;
|
|
p.not_after = time(now + 30 * DAY)?;
|
|
let issuer = Issuer::from_ca_cert_pem(int.cert_pem(), &int.key().0).map_err(build)?;
|
|
Ok(p.signed_by(&key.0, &issuer).map_err(build)?.pem())
|
|
}
|
|
|
|
/// A CSR that asks for far too much: a CA, no path limit, a web name, 50 years.
|
|
pub fn greedy_csr(key: &KeyPair, cn: &str) -> Result<String, PkiError> {
|
|
let mut p = CertificateParams::new(vec!["www.example.com".to_owned()]).map_err(build)?;
|
|
p.distinguished_name = dn(cn);
|
|
p.is_ca = IsCa::Ca(BasicConstraints::Unconstrained);
|
|
p.not_after = time(4_000_000_000)?;
|
|
p.serialize_request(&key.0)
|
|
.map_err(build)?
|
|
.pem()
|
|
.map_err(build)
|
|
}
|
|
|
|
/// Flip one byte inside the CSR's signed part.
|
|
pub fn tamper_csr(csr_pem: &str) -> String {
|
|
let Ok(mut der) = csr_der(csr_pem) else {
|
|
return csr_pem.to_owned();
|
|
};
|
|
if let Some(b) = der.get_mut(40) {
|
|
*b ^= 0x01;
|
|
}
|
|
armor("CERTIFICATE REQUEST", &der)
|
|
}
|
|
|
|
/// An agent certificate made by any intermediate (a compromised Ianitor), chain PEM.
|
|
pub fn agent_under_any<S: Signer>(
|
|
int: &Intermediate<S>,
|
|
csr_pem: &str,
|
|
team: &str,
|
|
agent: &str,
|
|
now: i64,
|
|
) -> Result<String, PkiError> {
|
|
Ok(int
|
|
.issue_agent_unchecked(csr_pem, team, agent, now)?
|
|
.chain_pem)
|
|
}
|
|
|
|
/// A CSR for an Ed25519 key (agents must use P-256).
|
|
pub fn ed25519_csr(cn: &str) -> Result<String, PkiError> {
|
|
let key = rcgen::KeyPair::generate_for(&rcgen::PKCS_ED25519).map_err(build)?;
|
|
let mut p = CertificateParams::default();
|
|
p.distinguished_name = dn(cn);
|
|
p.serialize_request(&key)
|
|
.map_err(build)?
|
|
.pem()
|
|
.map_err(build)
|
|
}
|