use std::sync::Arc;
use pki_types::CertificateRevocationListDer;
use webpki::BorrowedCertRevocationList;
use super::verify::{AnonymousClientPolicy, WebPkiClientVerifier};
use crate::verify::ClientCertVerifier;
use crate::{CertRevocationListError, RootCertStore};
#[derive(Debug, Clone)]
pub struct ClientCertVerifierBuilder {
roots: Arc<RootCertStore>,
crls: Vec<CertificateRevocationListDer<'static>>,
anon_policy: AnonymousClientPolicy,
}
impl ClientCertVerifierBuilder {
pub(crate) fn new(roots: Arc<RootCertStore>) -> Self {
Self {
roots,
crls: Vec::new(),
anon_policy: AnonymousClientPolicy::Deny,
}
}
pub fn with_crls(
mut self,
crls: impl IntoIterator<Item = CertificateRevocationListDer<'static>>,
) -> Self {
self.crls.extend(crls);
self
}
pub fn allow_unauthenticated(mut self) -> Self {
self.anon_policy = AnonymousClientPolicy::Allow;
self
}
pub fn build(self) -> Result<Arc<dyn ClientCertVerifier>, ClientCertVerifierBuilderError> {
if self.roots.is_empty() {
return Err(ClientCertVerifierBuilderError::NoRootAnchors);
}
Ok(Arc::new(WebPkiClientVerifier::new(
self.roots,
self.crls
.into_iter()
.map(|der_crl| {
BorrowedCertRevocationList::from_der(der_crl.as_ref())
.and_then(|crl| crl.to_owned())
.map_err(CertRevocationListError::from)
})
.collect::<Result<Vec<_>, CertRevocationListError>>()?,
self.anon_policy,
)))
}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum ClientCertVerifierBuilderError {
NoRootAnchors,
InvalidCrl(CertRevocationListError),
}
impl From<CertRevocationListError> for ClientCertVerifierBuilderError {
fn from(value: CertRevocationListError) -> Self {
Self::InvalidCrl(value)
}
}
#[cfg(test)]
mod tests {
use crate::server::ClientCertVerifierBuilderError;
use crate::webpki::verify::WebPkiClientVerifier;
use crate::RootCertStore;
use pki_types::{CertificateDer, CertificateRevocationListDer};
use std::sync::Arc;
fn load_crls(crls_der: &[&[u8]]) -> Vec<CertificateRevocationListDer<'static>> {
crls_der
.iter()
.map(|pem_bytes| {
rustls_pemfile::crls(&mut &pem_bytes[..])
.next()
.unwrap()
.unwrap()
})
.collect()
}
fn test_crls() -> Vec<CertificateRevocationListDer<'static>> {
load_crls(&[
include_bytes!("../../../test-ca/ecdsa/client.revoked.crl.pem").as_slice(),
include_bytes!("../../../test-ca/rsa/client.revoked.crl.pem").as_slice(),
])
}
fn load_roots(roots_der: &[&[u8]]) -> Arc<RootCertStore> {
let mut roots = RootCertStore::empty();
roots_der.iter().for_each(|der| {
roots
.add(CertificateDer::from(der.to_vec()))
.unwrap()
});
roots.into()
}
fn test_roots() -> Arc<RootCertStore> {
load_roots(&[
include_bytes!("../../../test-ca/ecdsa/ca.der").as_slice(),
include_bytes!("../../../test-ca/rsa/ca.der").as_slice(),
])
}
#[test]
fn test_noauth() {
WebPkiClientVerifier::no_client_auth();
}
#[test]
fn test_required_auth() {
let builder = WebPkiClientVerifier::builder(test_roots());
println!("{:?}", builder);
builder.build().unwrap();
}
#[test]
fn test_optional_auth() {
let builder = WebPkiClientVerifier::builder(test_roots()).allow_unauthenticated();
println!("{:?}", builder);
builder.build().unwrap();
}
#[test]
fn test_without_crls_required_auth() {
let builder = WebPkiClientVerifier::builder(test_roots());
println!("{:?}", builder);
builder.build().unwrap();
}
#[test]
fn test_without_crls_opptional_auth() {
let builder = WebPkiClientVerifier::builder(test_roots()).allow_unauthenticated();
println!("{:?}", builder);
builder.build().unwrap();
}
#[test]
fn test_with_invalid_crls() {
let result = WebPkiClientVerifier::builder(test_roots())
.with_crls(vec![CertificateRevocationListDer::from(vec![0xFF])])
.build();
assert!(matches!(
result,
Err(ClientCertVerifierBuilderError::InvalidCrl(_))
));
}
#[test]
fn test_with_crls_multiple_calls() {
let initial_crls = test_crls();
let extra_crls =
load_crls(&[
include_bytes!("../../../test-ca/eddsa/client.revoked.crl.pem").as_slice(),
]);
let builder = WebPkiClientVerifier::builder(test_roots())
.with_crls(initial_crls.clone())
.with_crls(extra_crls.clone());
assert_eq!(builder.crls.len(), initial_crls.len() + extra_crls.len());
println!("{:?}", builder);
builder.build().unwrap();
}
#[test]
fn test_with_crls_required_auth_implicit() {
let builder = WebPkiClientVerifier::builder(test_roots()).with_crls(test_crls());
println!("{:?}", builder);
builder.build().unwrap();
}
#[test]
fn test_with_crls_optional_auth() {
let builder = WebPkiClientVerifier::builder(test_roots())
.with_crls(test_crls())
.allow_unauthenticated();
println!("{:?}", builder);
builder.build().unwrap();
}
#[test]
fn test_builder_no_roots() {
let result = WebPkiClientVerifier::builder(RootCertStore::empty().into()).build();
assert!(matches!(
result,
Err(ClientCertVerifierBuilderError::NoRootAnchors)
));
}
}