use indicatrix_net::tls;
use rcgen::{
BasicConstraints, CertificateParams, DnType, ExtendedKeyUsagePurpose, IsCa, Issuer, KeyPair,
KeyUsagePurpose,
};
use std::{
fmt,
net::IpAddr,
path::{Path, PathBuf},
};
use time::{Duration, OffsetDateTime};
pub const CA_CERT_FILE: &str = "ca.pem";
pub const CA_KEY_FILE: &str = "ca.key";
pub const SERVER_CERT_FILE: &str = "server.pem";
pub const SERVER_KEY_FILE: &str = "server.key";
pub const CLIENT_CERT_FILE: &str = "client.pem";
pub const CLIENT_KEY_FILE: &str = "client.key";
pub const ALLOWLIST_FILE: &str = "allowlist.txt";
const CA_LIFETIME_DAYS: i64 = 365 * 10;
const LEAF_LIFETIME_DAYS: i64 = 365 * 5;
const NOT_BEFORE_SLACK_DAYS: i64 = 1;
#[derive(Debug)]
pub enum PkiError {
Io {
verb: &'static str,
path: PathBuf,
source: std::io::Error,
},
CaAlreadyExists { dir: PathBuf },
CaFileUnreadable {
path: PathBuf,
dir: PathBuf,
source: std::io::Error,
},
NoServerSan,
EmptyClientName,
Rcgen {
context: &'static str,
source: rcgen::Error,
},
RcgenAtPath { path: PathBuf, source: rcgen::Error },
Message(String),
Certificate {
path: PathBuf,
source: tls::TlsError,
},
AllowlistUpdate {
out_dir: PathBuf,
path: PathBuf,
fingerprint_hex: String,
name: String,
source: tls::TlsError,
},
}
impl fmt::Display for PkiError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Io { verb, path, source } => {
write!(f, "could not {verb} {}: {source}", path.display())
}
Self::CaAlreadyExists { dir } => write!(
f,
"{} already contains a CA ({CA_CERT_FILE} and/or {CA_KEY_FILE}) -- refusing to overwrite it, since \
that would invalidate every certificate already issued from it. Remove those files yourself first \
if you really mean to start over.",
dir.display()
),
Self::CaFileUnreadable { path, dir, source } => write!(
f,
"could not read {}: {source} (run `indicatrix-worker cert init --dir {}` first)",
path.display(),
dir.display()
),
Self::NoServerSan => write!(
f,
"issue-server requires at least one --host or --ip: rustls ignores Common Name entirely, so a \
certificate with no Subject Alternative Name can never be validated by a mutual-TLS client -- see \
--help"
),
Self::EmptyClientName => write!(f, "--name must not be empty"),
Self::Rcgen { context, source } => write!(f, "{context}: {source}"),
Self::RcgenAtPath { path, source } => write!(f, "{}: {source}", path.display()),
Self::Message(msg) => f.write_str(msg),
Self::Certificate { path, source } => write!(f, "{}: {source}", path.display()),
Self::AllowlistUpdate {
out_dir,
path,
fingerprint_hex,
name,
source,
} => write!(
f,
"issued the certificate at {} but could not update {}: {source} -- add this line to it by hand: \
{fingerprint_hex} # {name}",
out_dir.display(),
path.display()
),
}
}
}
impl std::error::Error for PkiError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Io { source, .. } | Self::CaFileUnreadable { source, .. } => Some(source),
Self::Rcgen { source, .. } | Self::RcgenAtPath { source, .. } => Some(source),
Self::Certificate { source, .. } | Self::AllowlistUpdate { source, .. } => Some(source),
Self::CaAlreadyExists { .. }
| Self::NoServerSan
| Self::EmptyClientName
| Self::Message(_) => None,
}
}
}
impl From<String> for PkiError {
fn from(msg: String) -> Self {
Self::Message(msg)
}
}
fn not_before_with_skew_slack() -> OffsetDateTime {
OffsetDateTime::now_utc() - Duration::days(NOT_BEFORE_SLACK_DAYS)
}
fn not_after(lifetime_days: i64) -> OffsetDateTime {
OffsetDateTime::now_utc() + Duration::days(lifetime_days)
}
pub fn init(dir: &Path) -> Result<(), PkiError> {
std::fs::create_dir_all(dir).map_err(|source| PkiError::Io {
verb: "create",
path: dir.to_path_buf(),
source,
})?;
let ca_cert_path = dir.join(CA_CERT_FILE);
let ca_key_path = dir.join(CA_KEY_FILE);
if ca_cert_path.exists() || ca_key_path.exists() {
return Err(PkiError::CaAlreadyExists {
dir: dir.to_path_buf(),
});
}
let key_pair = KeyPair::generate().map_err(|source| PkiError::Rcgen {
context: "CA key generation failed",
source,
})?;
let mut params =
CertificateParams::new(Vec::<String>::new()).map_err(|source| PkiError::Rcgen {
context: "unexpected error building an empty SAN list",
source,
})?;
params.is_ca = IsCa::Ca(BasicConstraints::Unconstrained);
params.key_usages = vec![
KeyUsagePurpose::KeyCertSign,
KeyUsagePurpose::CrlSign,
KeyUsagePurpose::DigitalSignature,
];
params
.distinguished_name
.push(DnType::CommonName, "indicatrix-worker private CA");
params.not_before = not_before_with_skew_slack();
params.not_after = not_after(CA_LIFETIME_DAYS);
let cert = params
.self_signed(&key_pair)
.map_err(|source| PkiError::Rcgen {
context: "CA self-signing failed",
source,
})?;
std::fs::write(&ca_cert_path, cert.pem()).map_err(|source| PkiError::Io {
verb: "write",
path: ca_cert_path.clone(),
source,
})?;
tls::write_private_key_pem(&ca_key_path, &key_pair.serialize_pem())?;
tracing::info!(
"indicatrix-worker cert init: wrote {} and {} -- CA expires {} (UTC)",
ca_cert_path.display(),
ca_key_path.display(),
params.not_after.date()
);
Ok(())
}
pub(crate) fn load_ca(dir: &Path) -> Result<Issuer<'static, KeyPair>, PkiError> {
let ca_cert_path = dir.join(CA_CERT_FILE);
let ca_key_path = dir.join(CA_KEY_FILE);
let ca_pem =
std::fs::read_to_string(&ca_cert_path).map_err(|source| PkiError::CaFileUnreadable {
path: ca_cert_path.clone(),
dir: dir.to_path_buf(),
source,
})?;
let ca_key_pem =
std::fs::read_to_string(&ca_key_path).map_err(|source| PkiError::CaFileUnreadable {
path: ca_key_path.clone(),
dir: dir.to_path_buf(),
source,
})?;
let ca_key = KeyPair::from_pem(&ca_key_pem).map_err(|source| PkiError::RcgenAtPath {
path: ca_key_path.clone(),
source,
})?;
let issuer =
Issuer::from_ca_cert_pem(&ca_pem, ca_key).map_err(|source| PkiError::RcgenAtPath {
path: ca_cert_path.clone(),
source,
})?;
Ok(issuer)
}
pub fn issue_server(dir: &Path, hosts: &[String], ips: &[IpAddr]) -> Result<(), PkiError> {
if hosts.is_empty() && ips.is_empty() {
return Err(PkiError::NoServerSan);
}
let ca_issuer = load_ca(dir)?;
let mut sans: Vec<String> = hosts.to_vec();
sans.extend(ips.iter().map(IpAddr::to_string));
let mut params = CertificateParams::new(sans).map_err(|source| PkiError::Rcgen {
context: "invalid --host/--ip value",
source,
})?;
let common_name = hosts.first().cloned().unwrap_or_else(|| ips[0].to_string());
params
.distinguished_name
.push(DnType::CommonName, common_name);
params.not_before = not_before_with_skew_slack();
params.not_after = not_after(LEAF_LIFETIME_DAYS);
params.use_authority_key_identifier_extension = true;
params.key_usages = vec![
KeyUsagePurpose::DigitalSignature,
KeyUsagePurpose::KeyEncipherment,
];
params.extended_key_usages = vec![ExtendedKeyUsagePurpose::ServerAuth];
let key_pair = KeyPair::generate().map_err(|source| PkiError::Rcgen {
context: "server key generation failed",
source,
})?;
let cert = params
.signed_by(&key_pair, &ca_issuer)
.map_err(|source| PkiError::Rcgen {
context: "failed to sign the server certificate",
source,
})?;
let cert_path = dir.join(SERVER_CERT_FILE);
let key_path = dir.join(SERVER_KEY_FILE);
std::fs::write(&cert_path, cert.pem()).map_err(|source| PkiError::Io {
verb: "write",
path: cert_path.clone(),
source,
})?;
tls::write_private_key_pem(&key_path, &key_pair.serialize_pem())?;
tracing::info!(
"indicatrix-worker cert issue-server: wrote {} and {} (SANs: {}{}{}) -- expires {} (UTC)",
cert_path.display(),
key_path.display(),
hosts.join(", "),
if hosts.is_empty() || ips.is_empty() {
""
} else {
", "
},
ips.iter()
.map(IpAddr::to_string)
.collect::<Vec<_>>()
.join(", "),
params.not_after.date()
);
Ok(())
}
pub fn issue_client(dir: &Path, name: &str, out: &Path) -> Result<(), PkiError> {
let ca_issuer = load_ca(dir)?;
let (cert, key_pair, not_after) = sign_client_leaf(&ca_issuer, name)?;
std::fs::create_dir_all(out).map_err(|source| PkiError::Io {
verb: "create",
path: out.to_path_buf(),
source,
})?;
let ca_source_path = dir.join(CA_CERT_FILE);
let ca_pem = std::fs::read_to_string(&ca_source_path).map_err(|source| PkiError::Io {
verb: "read",
path: ca_source_path.clone(),
source,
})?;
let out_ca_path = out.join(CA_CERT_FILE);
let out_cert_path = out.join(CLIENT_CERT_FILE);
let out_key_path = out.join(CLIENT_KEY_FILE);
std::fs::write(&out_ca_path, &ca_pem).map_err(|source| PkiError::Io {
verb: "write",
path: out_ca_path.clone(),
source,
})?;
std::fs::write(&out_cert_path, cert.pem()).map_err(|source| PkiError::Io {
verb: "write",
path: out_cert_path.clone(),
source,
})?;
tls::write_private_key_pem(&out_key_path, &key_pair.serialize_pem())?;
let fingerprint = tls::fingerprint(cert.der());
let fingerprint_hex = tls::fingerprint_to_hex(&fingerprint);
let allowlist_path = dir.join(ALLOWLIST_FILE);
tls::append_to_allowlist(&allowlist_path, &fingerprint, name).map_err(|source| {
PkiError::AllowlistUpdate {
out_dir: out.to_path_buf(),
path: allowlist_path.clone(),
fingerprint_hex: fingerprint_hex.clone(),
name: name.to_string(),
source,
}
})?;
tracing::info!(
"indicatrix-worker cert issue-client: wrote bundle to {} ({CA_CERT_FILE}, {CLIENT_CERT_FILE}, {CLIENT_KEY_FILE}) -- \
fingerprint {fingerprint_hex} added to {} -- expires {} (UTC)",
out.display(),
allowlist_path.display(),
not_after.date()
);
Ok(())
}
fn sign_client_leaf(
ca_issuer: &Issuer<'static, KeyPair>,
name: &str,
) -> Result<(rcgen::Certificate, KeyPair, OffsetDateTime), PkiError> {
if name.trim().is_empty() {
return Err(PkiError::EmptyClientName);
}
let mut params =
CertificateParams::new(Vec::<String>::new()).map_err(|source| PkiError::Rcgen {
context: "unexpected error building an empty SAN list",
source,
})?;
params.distinguished_name.push(DnType::CommonName, name);
params.not_before = not_before_with_skew_slack();
params.not_after = not_after(LEAF_LIFETIME_DAYS);
params.use_authority_key_identifier_extension = true;
params.key_usages = vec![KeyUsagePurpose::DigitalSignature];
params.extended_key_usages = vec![ExtendedKeyUsagePurpose::ClientAuth];
let key_pair = KeyPair::generate().map_err(|source| PkiError::Rcgen {
context: "client key generation failed",
source,
})?;
let cert = params
.signed_by(&key_pair, ca_issuer)
.map_err(|source| PkiError::Rcgen {
context: "failed to sign the client certificate",
source,
})?;
Ok((cert, key_pair, params.not_after))
}
pub struct InMemoryClientBundle {
pub ca_pem: String,
pub ca_fingerprint: tls::Fingerprint,
pub client_cert_pem: String,
pub client_key_pem: String,
pub client_fingerprint: tls::Fingerprint,
}
pub fn issue_client_in_memory(dir: &Path, name: &str) -> Result<InMemoryClientBundle, PkiError> {
let ca_issuer = load_ca(dir)?;
let (cert, key_pair, _not_after) = sign_client_leaf(&ca_issuer, name)?;
let ca_cert_path = dir.join(CA_CERT_FILE);
let ca_pem = std::fs::read_to_string(&ca_cert_path).map_err(|source| PkiError::Io {
verb: "read",
path: ca_cert_path.clone(),
source,
})?;
let ca_der = tls::load_certs(&ca_cert_path).map_err(|source| PkiError::Certificate {
path: ca_cert_path.clone(),
source,
})?;
let ca_fingerprint = tls::fingerprint(&ca_der[0]);
let client_fingerprint = tls::fingerprint(cert.der());
Ok(InMemoryClientBundle {
ca_pem,
ca_fingerprint,
client_cert_pem: cert.pem(),
client_key_pem: key_pair.serialize_pem(),
client_fingerprint,
})
}
#[must_use]
pub fn default_allowlist_path(ca_path: &Path) -> PathBuf {
ca_path
.parent()
.filter(|p| !p.as_os_str().is_empty())
.unwrap_or_else(|| Path::new("."))
.join(ALLOWLIST_FILE)
}
#[cfg(test)]
mod tests {
use super::*;
fn temp_dir(label: &str) -> PathBuf {
let dir = std::env::temp_dir().join(format!(
"indicatrix-worker-pki-test-{label}-{}-{}",
std::process::id(),
fastrand_seed()
));
std::fs::create_dir_all(&dir).unwrap();
dir
}
fn fastrand_seed() -> u128 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
}
#[test]
fn init_writes_a_ca_and_refuses_to_overwrite_it() {
let dir = temp_dir("init");
init(&dir).unwrap();
assert!(dir.join(CA_CERT_FILE).exists());
assert!(dir.join(CA_KEY_FILE).exists());
let err = init(&dir).unwrap_err().to_string();
assert!(err.contains("already contains a CA"), "{err}");
std::fs::remove_dir_all(&dir).ok();
}
#[test]
fn issue_server_requires_at_least_one_san() {
let dir = temp_dir("issue-server-no-san");
init(&dir).unwrap();
let err = issue_server(&dir, &[], &[]).unwrap_err().to_string();
assert!(err.contains("--host or --ip"), "{err}");
std::fs::remove_dir_all(&dir).ok();
}
#[test]
fn issue_server_writes_a_cert_with_the_requested_sans() {
let dir = temp_dir("issue-server");
init(&dir).unwrap();
issue_server(
&dir,
&["worker.lan".to_string()],
&["10.0.0.5".parse().unwrap()],
)
.unwrap();
let cert_pem = std::fs::read_to_string(dir.join(SERVER_CERT_FILE)).unwrap();
assert!(cert_pem.contains("BEGIN CERTIFICATE"));
assert!(dir.join(SERVER_KEY_FILE).exists());
std::fs::remove_dir_all(&dir).ok();
}
#[test]
fn issue_client_writes_a_bundle_and_updates_the_allowlist() {
let dir = temp_dir("issue-client-dir");
let out = temp_dir("issue-client-out");
init(&dir).unwrap();
issue_client(&dir, "laptop", &out).unwrap();
assert!(out.join(CA_CERT_FILE).exists());
assert!(out.join(CLIENT_CERT_FILE).exists());
assert!(out.join(CLIENT_KEY_FILE).exists());
let allowlist_text = std::fs::read_to_string(dir.join(ALLOWLIST_FILE)).unwrap();
assert!(allowlist_text.contains("# laptop"), "{allowlist_text}");
let client_der = tls::load_certs(&out.join(CLIENT_CERT_FILE)).unwrap();
let fp = tls::fingerprint(&client_der[0]);
let allowlist = tls::Allowlist::load(&dir.join(ALLOWLIST_FILE)).unwrap();
assert!(allowlist.contains(&fp));
std::fs::remove_dir_all(&dir).ok();
std::fs::remove_dir_all(&out).ok();
}
#[test]
fn issue_client_rejects_an_empty_name() {
let dir = temp_dir("issue-client-empty-name");
init(&dir).unwrap();
let err = issue_client(&dir, " ", &dir.join("bundle"))
.unwrap_err()
.to_string();
assert!(err.contains("--name"), "{err}");
std::fs::remove_dir_all(&dir).ok();
}
#[test]
fn issue_server_and_issue_client_fail_clearly_without_an_existing_ca() {
let dir = temp_dir("no-ca");
let err = issue_server(&dir, &["worker.lan".to_string()], &[])
.unwrap_err()
.to_string();
assert!(err.contains("cert init"), "{err}");
std::fs::remove_dir_all(&dir).ok();
}
#[test]
fn default_allowlist_path_sits_beside_the_ca_file() {
let ca = Path::new("C:/pki/ca.pem");
assert_eq!(
default_allowlist_path(ca),
PathBuf::from("C:/pki").join(ALLOWLIST_FILE)
);
}
}