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
use core::fmt;
use std::{error::Error as StdError, sync::Arc};
use pki_types::CertificateRevocationListDer;
use webpki::BorrowedCertRevocationList;
use super::verify::{AnonymousClientPolicy, WebPkiClientVerifier, WebPkiSupportedAlgorithms};
use crate::verify::ClientCertVerifier;
use crate::{CertRevocationListError, RootCertStore};
/// 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>,
crls: Vec<CertificateRevocationListDer<'static>>,
anon_policy: AnonymousClientPolicy,
supported_algs: Option<WebPkiSupportedAlgorithms>,
}
impl ClientCertVerifierBuilder {
pub(crate) fn new(roots: Arc<RootCertStore>) -> Self {
Self {
roots,
crls: Vec::new(),
anon_policy: AnonymousClientPolicy::Deny,
supported_algs: None,
}
}
/// 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.
pub fn with_crls(
mut self,
crls: impl IntoIterator<Item = CertificateRevocationListDer<'static>>,
) -> Self {
self.crls.extend(crls);
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
}
/// Sets which signature verification algorithms are enabled.
///
/// If this is called multiple times, the last call wins.
pub fn with_signature_verification_algorithms(
mut self,
supported_algs: WebPkiSupportedAlgorithms,
) -> Self {
self.supported_algs = Some(supported_algs);
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 the `ring` crate feature is supplied, and `with_signature_verification_algorithms` was not
/// called on the builder, a default set of signature verification algorithms is used.
///
/// Once built, the provided `Arc<dyn ClientCertVerifier>` can be used with a Rustls
/// [crate::server::ServerConfig] to configure client certificate validation using
/// [`with_client_cert_verifier`][crate::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.
/// 3. No signature verification algorithms were set and the `ring` feature is not enabled.
#[cfg_attr(not(feature = "ring"), allow(unused_mut))]
pub fn build(mut self) -> Result<Arc<dyn ClientCertVerifier>, ClientCertVerifierBuilderError> {
if self.roots.is_empty() {
return Err(ClientCertVerifierBuilderError::NoRootAnchors);
}
#[cfg(feature = "ring")]
if self.supported_algs.is_none() {
self.supported_algs = Some(super::verify::SUPPORTED_SIG_ALGS);
}
let supported_algs = self
.supported_algs
.ok_or(ClientCertVerifierBuilderError::NoSupportedAlgorithms)?;
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,
supported_algs,
)))
}
}
/// One or more root trust anchors must be provided to create a [ClientCertVerifierBuilder].
/// If you wish to disable client authentication, then use [WebPkiClientVerifier::no_client_auth]
/// instead of constructing a builder.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum ClientCertVerifierBuilderError {
/// No root trust anchors were provided.
NoRootAnchors,
/// A provided CRL could not be parsed.
InvalidCrl(CertRevocationListError),
/// No supported signature verification algorithms were provided.
///
/// Call `with_signature_verification_algorithms` on the builder, or compile
/// with the `ring` feature.
NoSupportedAlgorithms,
}
impl From<CertRevocationListError> for ClientCertVerifierBuilderError {
fn from(value: CertRevocationListError) -> Self {
Self::InvalidCrl(value)
}
}
impl fmt::Display for ClientCertVerifierBuilderError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NoRootAnchors => write!(f, "no root trust anchors were provided"),
Self::InvalidCrl(e) => write!(f, "provided CRL could not be parsed: {:?}", e),
Self::NoSupportedAlgorithms => {
write!(f, "no signature verification algorithms were provided")
}
}
}
}
impl StdError for ClientCertVerifierBuilderError {}
#[cfg(all(test, feature = "ring"))]
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() {
// We should be able to build a verifier that turns off client authentication.
WebPkiClientVerifier::no_client_auth();
}
#[test]
fn test_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_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_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_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 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(ClientCertVerifierBuilderError::InvalidCrl(_))
));
}
#[test]
fn test_with_crls_multiple_calls() {
// We should be able to call `with_crls` 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_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_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_builder_no_roots() {
// Trying to create a builder with no trust anchors should fail at build time
let result = WebPkiClientVerifier::builder(RootCertStore::empty().into()).build();
assert!(matches!(
result,
Err(ClientCertVerifierBuilderError::NoRootAnchors)
));
}
#[test]
fn smoke() {
let all = vec![
ClientCertVerifierBuilderError::NoRootAnchors,
ClientCertVerifierBuilderError::InvalidCrl(crate::CertRevocationListError::ParseError),
ClientCertVerifierBuilderError::NoSupportedAlgorithms,
];
for err in all {
let _ = format!("{:?}", err);
let _ = format!("{}", err);
}
}
}