rustls/webpki/client_verifier.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
use alloc::sync::Arc;
use alloc::vec::Vec;
use pki_types::{CertificateDer, CertificateRevocationListDer, UnixTime};
use webpki::{CertRevocationList, RevocationCheckDepth, UnknownStatusPolicy};
use super::{pki_error, VerifierBuilderError};
use crate::crypto::{CryptoProvider, WebPkiSupportedAlgorithms};
use crate::verify::{
ClientCertVerified, ClientCertVerifier, DigitallySignedStruct, HandshakeSignatureValid,
NoClientAuth,
};
use crate::webpki::parse_crls;
use crate::webpki::verify::{verify_tls12_signature, verify_tls13_signature, ParsedCertificate};
use crate::{DistinguishedName, Error, RootCertStore, SignatureScheme};
#[cfg(doc)]
use crate::crypto;
#[cfg(doc)]
use crate::server::ServerConfig;
#[cfg(doc)]
use crate::ConfigBuilder;
/// A builder for configuring a `webpki` client certificate verifier.
///
/// For more information, see the [`WebPkiClientVerifier`] documentation.
#[derive(Debug, Clone)]
pub struct ClientCertVerifierBuilder {
roots: Arc<RootCertStore>,
root_hint_subjects: Vec<DistinguishedName>,
crls: Vec<CertificateRevocationListDer<'static>>,
revocation_check_depth: RevocationCheckDepth,
unknown_revocation_policy: UnknownStatusPolicy,
anon_policy: AnonymousClientPolicy,
supported_algs: WebPkiSupportedAlgorithms,
}
impl ClientCertVerifierBuilder {
pub(crate) fn new(
roots: Arc<RootCertStore>,
supported_algs: WebPkiSupportedAlgorithms,
) -> Self {
Self {
root_hint_subjects: roots.subjects(),
roots,
crls: Vec::new(),
anon_policy: AnonymousClientPolicy::Deny,
revocation_check_depth: RevocationCheckDepth::Chain,
unknown_revocation_policy: UnknownStatusPolicy::Deny,
supported_algs,
}
}
/// Clear the list of trust anchor hint subjects.
///
/// By default, the client cert verifier will use the subjects provided by the root cert
/// store configured for client authentication. Calling this function will remove these
/// hint subjects, indicating the client should make a free choice of which certificate
/// to send.
///
/// See [`ClientCertVerifier::root_hint_subjects`] for more information on
/// circumstances where you may want to clear the default hint subjects.
pub fn clear_root_hint_subjects(mut self) -> Self {
self.root_hint_subjects = Vec::default();
self
}
/// Add additional [`DistinguishedName`]s to the list of trust anchor hint subjects.
///
/// By default, the client cert verifier will use the subjects provided by the root cert
/// store configured for client authentication. Calling this function will add to these
/// existing hint subjects. Calling this function with empty `subjects` will have no
/// effect.
///
/// See [`ClientCertVerifier::root_hint_subjects`] for more information on
/// circumstances where you may want to override the default hint subjects.
pub fn add_root_hint_subjects(
mut self,
subjects: impl IntoIterator<Item = DistinguishedName>,
) -> Self {
self.root_hint_subjects.extend(subjects);
self
}
/// Verify the revocation state of presented client certificates against the provided
/// certificate revocation lists (CRLs). Calling `with_crls` multiple times appends the
/// given CRLs to the existing collection.
///
/// By default all certificates in the verified chain built from the presented client
/// certificate to a trust anchor will have their revocation status checked. Calling
/// [`only_check_end_entity_revocation`][Self::only_check_end_entity_revocation] will
/// change this behavior to only check the end entity client certificate.
///
/// By default if a certificate's revocation status can not be determined using the
/// configured CRLs, it will be treated as an error. Calling
/// [`allow_unknown_revocation_status`][Self::allow_unknown_revocation_status] will change
/// this behavior to allow unknown revocation status.
pub fn with_crls(
mut self,
crls: impl IntoIterator<Item = CertificateRevocationListDer<'static>>,
) -> Self {
self.crls.extend(crls);
self
}
/// Only check the end entity certificate revocation status when using CRLs.
///
/// If CRLs are provided using [`with_crls`][Self::with_crls] only check the end entity
/// certificate's revocation status. Overrides the default behavior of checking revocation
/// status for each certificate in the verified chain built to a trust anchor
/// (excluding the trust anchor itself).
///
/// If no CRLs are provided then this setting has no effect. Neither the end entity certificate
/// or any intermediates will have revocation status checked.
pub fn only_check_end_entity_revocation(mut self) -> Self {
self.revocation_check_depth = RevocationCheckDepth::EndEntity;
self
}
/// Allow unauthenticated clients to connect.
///
/// Clients that offer a client certificate issued by a trusted root, and clients that offer no
/// client certificate will be allowed to connect.
pub fn allow_unauthenticated(mut self) -> Self {
self.anon_policy = AnonymousClientPolicy::Allow;
self
}
/// Allow unknown certificate revocation status when using CRLs.
///
/// If CRLs are provided with [`with_crls`][Self::with_crls] and it isn't possible to
/// determine the revocation status of a certificate, do not treat it as an error condition.
/// Overrides the default behavior where unknown revocation status is considered an error.
///
/// If no CRLs are provided then this setting has no effect as revocation status checks
/// are not performed.
pub fn allow_unknown_revocation_status(mut self) -> Self {
self.unknown_revocation_policy = UnknownStatusPolicy::Allow;
self
}
/// Build a client certificate verifier. The built verifier will be used for the server to offer
/// client certificate authentication, to control how offered client certificates are validated,
/// and to determine what to do with anonymous clients that do not respond to the client
/// certificate authentication offer with a client certificate.
///
/// If `with_signature_verification_algorithms` was not called on the builder, a default set of
/// signature verification algorithms is used, controlled by the selected [`CryptoProvider`].
///
/// Once built, the provided `Arc<dyn ClientCertVerifier>` can be used with a Rustls
/// [`ServerConfig`] to configure client certificate validation using
/// [`with_client_cert_verifier`][ConfigBuilder<ClientConfig, WantsVerifier>::with_client_cert_verifier].
///
/// # Errors
/// This function will return a `ClientCertVerifierBuilderError` if:
/// 1. No trust anchors have been provided.
/// 2. DER encoded CRLs have been provided that can not be parsed successfully.
pub fn build(self) -> Result<Arc<dyn ClientCertVerifier>, VerifierBuilderError> {
if self.roots.is_empty() {
return Err(VerifierBuilderError::NoRootAnchors);
}
Ok(Arc::new(WebPkiClientVerifier::new(
self.roots,
self.root_hint_subjects,
parse_crls(self.crls)?,
self.revocation_check_depth,
self.unknown_revocation_policy,
self.anon_policy,
self.supported_algs,
)))
}
}
/// A client certificate verifier that uses the `webpki` crate[^1] to perform client certificate
/// validation. It must be created via the [WebPkiClientVerifier::builder()] function.
///
/// Once built, the provided `Arc<dyn ClientCertVerifier>` can be used with a Rustls [`ServerConfig`]
/// to configure client certificate validation using [`with_client_cert_verifier`][ConfigBuilder<ClientConfig, WantsVerifier>::with_client_cert_verifier].
///
/// Example:
///
/// To require all clients present a client certificate issued by a trusted CA:
/// ```no_run
/// # #[cfg(feature = "ring")] {
/// # use rustls::RootCertStore;
/// # use rustls::server::WebPkiClientVerifier;
/// # let roots = RootCertStore::empty();
/// let client_verifier = WebPkiClientVerifier::builder(roots.into())
/// .build()
/// .unwrap();
/// # }
/// ```
///
/// Or, to allow clients presenting a client certificate authenticated by a trusted CA, or
/// anonymous clients that present no client certificate:
/// ```no_run
/// # #[cfg(feature = "ring")] {
/// # use rustls::RootCertStore;
/// # use rustls::server::WebPkiClientVerifier;
/// # let roots = RootCertStore::empty();
/// let client_verifier = WebPkiClientVerifier::builder(roots.into())
/// .allow_unauthenticated()
/// .build()
/// .unwrap();
/// # }
/// ```
///
/// If you wish to disable advertising client authentication:
/// ```no_run
/// # use rustls::RootCertStore;
/// # use rustls::server::WebPkiClientVerifier;
/// # let roots = RootCertStore::empty();
/// let client_verifier = WebPkiClientVerifier::no_client_auth();
/// ```
///
/// You can also configure the client verifier to check for certificate revocation with
/// client certificate revocation lists (CRLs):
/// ```no_run
/// # #[cfg(feature = "ring")] {
/// # use rustls::RootCertStore;
/// # use rustls::server::{WebPkiClientVerifier};
/// # let roots = RootCertStore::empty();
/// # let crls = Vec::new();
/// let client_verifier = WebPkiClientVerifier::builder(roots.into())
/// .with_crls(crls)
/// .build()
/// .unwrap();
/// # }
/// ```
///
/// [^1]: <https://github.com/rustls/webpki>
#[derive(Debug)]
pub struct WebPkiClientVerifier {
roots: Arc<RootCertStore>,
root_hint_subjects: Vec<DistinguishedName>,
crls: Vec<CertRevocationList<'static>>,
revocation_check_depth: RevocationCheckDepth,
unknown_revocation_policy: UnknownStatusPolicy,
anonymous_policy: AnonymousClientPolicy,
supported_algs: WebPkiSupportedAlgorithms,
}
impl WebPkiClientVerifier {
/// Create a builder for the `webpki` client certificate verifier configuration using
/// the default [`CryptoProvider`].
///
/// Client certificate authentication will be offered by the server, and client certificates
/// will be verified using the trust anchors found in the provided `roots`. If you
/// wish to disable client authentication use [WebPkiClientVerifier::no_client_auth()] instead.
///
/// The cryptography used comes from the default [`CryptoProvider`]: [`crypto::ring::default_provider`].
/// Use [`Self::builder_with_provider`] if you wish to customize this.
///
/// For more information, see the [`ClientCertVerifierBuilder`] documentation.
#[cfg(feature = "ring")]
pub fn builder(roots: Arc<RootCertStore>) -> ClientCertVerifierBuilder {
Self::builder_with_provider(roots, crate::crypto::ring::default_provider().into())
}
/// Create a builder for the `webpki` client certificate verifier configuration using
/// a specified [`CryptoProvider`].
///
/// Client certificate authentication will be offered by the server, and client certificates
/// will be verified using the trust anchors found in the provided `roots`. If you
/// wish to disable client authentication use [WebPkiClientVerifier::no_client_auth()] instead.
///
/// The cryptography used comes from the specified [`CryptoProvider`].
///
/// For more information, see the [`ClientCertVerifierBuilder`] documentation.
pub fn builder_with_provider(
roots: Arc<RootCertStore>,
provider: Arc<CryptoProvider>,
) -> ClientCertVerifierBuilder {
ClientCertVerifierBuilder::new(roots, provider.signature_verification_algorithms)
}
/// Create a new `WebPkiClientVerifier` that disables client authentication. The server will
/// not offer client authentication and anonymous clients will be accepted.
///
/// This is in contrast to using `WebPkiClientVerifier::builder().allow_unauthenticated().build()`,
/// which will produce a verifier that will offer client authentication, but not require it.
pub fn no_client_auth() -> Arc<dyn ClientCertVerifier> {
Arc::new(NoClientAuth {})
}
/// Construct a new `WebpkiClientVerifier`.
///
/// * `roots` is a list of trust anchors to use for certificate validation.
/// * `root_hint_subjects` is a list of distinguished names to use for hinting acceptable
/// certificate authority subjects to a client.
/// * `crls` is a `Vec` of owned certificate revocation lists (CRLs) to use for
/// client certificate validation.
/// * `revocation_check_depth` controls which certificates have their revocation status checked
/// when `crls` are provided.
/// * `unknown_revocation_policy` controls how certificates with an unknown revocation status
/// are handled when `crls` are provided.
/// * `anonymous_policy` controls whether client authentication is required, or if anonymous
/// clients can connect.
/// * `supported_algs` specifies which signature verification algorithms should be used.
pub(crate) fn new(
roots: Arc<RootCertStore>,
root_hint_subjects: Vec<DistinguishedName>,
crls: Vec<CertRevocationList<'static>>,
revocation_check_depth: RevocationCheckDepth,
unknown_revocation_policy: UnknownStatusPolicy,
anonymous_policy: AnonymousClientPolicy,
supported_algs: WebPkiSupportedAlgorithms,
) -> Self {
Self {
roots,
root_hint_subjects,
crls,
revocation_check_depth,
unknown_revocation_policy,
anonymous_policy,
supported_algs,
}
}
}
impl ClientCertVerifier for WebPkiClientVerifier {
fn offer_client_auth(&self) -> bool {
true
}
fn client_auth_mandatory(&self) -> bool {
match self.anonymous_policy {
AnonymousClientPolicy::Allow => false,
AnonymousClientPolicy::Deny => true,
}
}
fn root_hint_subjects(&self) -> &[DistinguishedName] {
&self.root_hint_subjects
}
fn verify_client_cert(
&self,
end_entity: &CertificateDer<'_>,
intermediates: &[CertificateDer<'_>],
now: UnixTime,
) -> Result<ClientCertVerified, Error> {
let cert = ParsedCertificate::try_from(end_entity)?;
let crl_refs = self.crls.iter().collect::<Vec<_>>();
let revocation = if self.crls.is_empty() {
None
} else {
Some(
webpki::RevocationOptionsBuilder::new(&crl_refs)
// Note: safe to unwrap here - new is only fallible if no CRLs are provided
// and we verify this above.
.unwrap()
.with_depth(self.revocation_check_depth)
.with_status_policy(self.unknown_revocation_policy)
.build(),
)
};
cert.0
.verify_for_usage(
self.supported_algs.all,
&self.roots.roots,
intermediates,
now,
webpki::KeyUsage::client_auth(),
revocation,
None,
)
.map_err(pki_error)
.map(|_| ClientCertVerified::assertion())
}
fn verify_tls12_signature(
&self,
message: &[u8],
cert: &CertificateDer<'_>,
dss: &DigitallySignedStruct,
) -> Result<HandshakeSignatureValid, Error> {
verify_tls12_signature(message, cert, dss, &self.supported_algs)
}
fn verify_tls13_signature(
&self,
message: &[u8],
cert: &CertificateDer<'_>,
dss: &DigitallySignedStruct,
) -> Result<HandshakeSignatureValid, Error> {
verify_tls13_signature(message, cert, dss, &self.supported_algs)
}
fn supported_verify_schemes(&self) -> Vec<SignatureScheme> {
self.supported_algs.supported_schemes()
}
}
/// Controls how the [WebPkiClientVerifier] handles anonymous clients.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum AnonymousClientPolicy {
/// Clients that do not present a client certificate are allowed.
Allow,
/// Clients that do not present a client certificate are denied.
Deny,
}
#[cfg(all(test, feature = "ring"))]
mod tests {
use super::WebPkiClientVerifier;
use crate::server::VerifierBuilderError;
use crate::RootCertStore;
use pki_types::{CertificateDer, CertificateRevocationListDer};
use std::prelude::v1::*;
use std::sync::Arc;
use std::{format, println, vec};
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-p256/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-p256/ca.der").as_slice(),
include_bytes!("../../../test-ca/rsa/ca.der").as_slice(),
])
}
#[test]
fn test_client_verifier_no_auth() {
// We should be able to build a verifier that turns off client authentication.
WebPkiClientVerifier::no_client_auth();
}
#[test]
fn test_client_verifier_required_auth() {
// We should be able to build a verifier that requires client authentication, and does
// no revocation checking.
let builder = WebPkiClientVerifier::builder(test_roots());
// The builder should be Debug.
println!("{:?}", builder);
builder.build().unwrap();
}
#[test]
fn test_client_verifier_optional_auth() {
// We should be able to build a verifier that allows client authentication, and anonymous
// access, and does no revocation checking.
let builder = WebPkiClientVerifier::builder(test_roots()).allow_unauthenticated();
// The builder should be Debug.
println!("{:?}", builder);
builder.build().unwrap();
}
#[test]
fn test_client_verifier_without_crls_required_auth() {
// We should be able to build a verifier that requires client authentication, and does
// no revocation checking, that hasn't been configured to determine how to handle
// unauthenticated clients yet.
let builder = WebPkiClientVerifier::builder(test_roots());
// The builder should be Debug.
println!("{:?}", builder);
builder.build().unwrap();
}
#[test]
fn test_client_verifier_without_crls_opptional_auth() {
// We should be able to build a verifier that allows client authentication,
// and anonymous access, that does no revocation checking.
let builder = WebPkiClientVerifier::builder(test_roots()).allow_unauthenticated();
// The builder should be Debug.
println!("{:?}", builder);
builder.build().unwrap();
}
#[test]
fn test_with_invalid_crls() {
// Trying to build a client verifier with invalid CRLs should error at build time.
let result = WebPkiClientVerifier::builder(test_roots())
.with_crls(vec![CertificateRevocationListDer::from(vec![0xFF])])
.build();
assert!(matches!(result, Err(VerifierBuilderError::InvalidCrl(_))));
}
#[test]
fn test_with_crls_multiple_calls() {
// We should be able to call `with_crls` on a client verifier multiple times.
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());
// There should be the expected number of crls.
assert_eq!(builder.crls.len(), initial_crls.len() + extra_crls.len());
// The builder should be Debug.
println!("{:?}", builder);
builder.build().unwrap();
}
#[test]
fn test_client_verifier_with_crls_required_auth_implicit() {
// We should be able to build a verifier that requires client authentication, and that does
// revocation checking with CRLs, and that does not allow any anonymous access.
let builder = WebPkiClientVerifier::builder(test_roots()).with_crls(test_crls());
// The builder should be Debug.
println!("{:?}", builder);
builder.build().unwrap();
}
#[test]
fn test_client_verifier_with_crls_optional_auth() {
// We should be able to build a verifier that supports client authentication, that does
// revocation checking with CRLs, and that allows anonymous access.
let builder = WebPkiClientVerifier::builder(test_roots())
.with_crls(test_crls())
.allow_unauthenticated();
// The builder should be Debug.
println!("{:?}", builder);
builder.build().unwrap();
}
#[test]
fn test_client_verifier_ee_only() {
// We should be able to build a client verifier that only checks EE revocation status.
let builder = WebPkiClientVerifier::builder(test_roots())
.with_crls(test_crls())
.only_check_end_entity_revocation();
// The builder should be Debug.
println!("{:?}", builder);
builder.build().unwrap();
}
#[test]
fn test_client_verifier_allow_unknown() {
// We should be able to build a client verifier that allows unknown revocation status
let builder = WebPkiClientVerifier::builder(test_roots())
.with_crls(test_crls())
.allow_unknown_revocation_status();
// The builder should be Debug.
println!("{:?}", builder);
builder.build().unwrap();
}
#[test]
fn test_builder_no_roots() {
// Trying to create a client verifier builder with no trust anchors should fail at build time
let result = WebPkiClientVerifier::builder(RootCertStore::empty().into()).build();
assert!(matches!(result, Err(VerifierBuilderError::NoRootAnchors)));
}
#[test]
fn smoke() {
let all = vec![
VerifierBuilderError::NoRootAnchors,
VerifierBuilderError::InvalidCrl(crate::CertRevocationListError::ParseError),
];
for err in all {
let _ = format!("{:?}", err);
let _ = format!("{}", err);
}
}
}