Skip to main content

rustls/client/
config.rs

1use alloc::vec::Vec;
2use core::any::Any;
3use core::fmt;
4use core::hash::{Hash, Hasher};
5use core::marker::PhantomData;
6
7#[cfg(feature = "webpki")]
8use pki_types::PrivateKeyDer;
9use pki_types::{FipsStatus, ServerName, UnixTime};
10
11use super::ech::EchMode;
12use super::handy::{ClientSessionMemoryCache, FailResolveClientCert, NoClientSessionStorage};
13use super::{Tls12Session, Tls13Session};
14use crate::builder::{ConfigBuilder, WantsVerifier};
15use crate::client::connection::ClientConnectionBuilder;
16#[cfg(doc)]
17use crate::crypto;
18use crate::crypto::kx::NamedGroup;
19use crate::crypto::{CipherSuite, CryptoProvider, SelectedCredential, SignatureScheme, hash};
20#[cfg(feature = "webpki")]
21use crate::crypto::{Credentials, Identity, SingleCredential};
22use crate::enums::{ApplicationProtocol, CertificateType, ProtocolVersion};
23use crate::error::{ApiMisuse, Error};
24use crate::key_log::NoKeyLog;
25use crate::suites::SupportedCipherSuite;
26use crate::sync::Arc;
27use crate::time_provider::{DefaultTimeProvider, TimeProvider};
28#[cfg(feature = "webpki")]
29use crate::webpki::{self, WebPkiServerVerifier};
30use crate::{DistinguishedName, DynHasher, KeyLog, compress, verify};
31
32/// Common configuration for (typically) all connections made by a program.
33///
34/// Making one of these is cheap, though one of the inputs may be expensive: gathering trust roots
35/// from the operating system to add to the [`RootCertStore`] passed to `with_root_certificates()`
36/// (the rustls-native-certs crate is often used for this) may take on the order of a few hundred
37/// milliseconds.
38///
39/// These must be created via the [`ClientConfig::builder()`] or [`ClientConfig::builder()`]
40/// function.
41///
42/// Note that using [`ConfigBuilder<ClientConfig, WantsVersions>::with_ech()`] will produce a common
43/// configuration specific to the provided [`crate::client::EchConfig`] that may not be appropriate
44/// for all connections made by the program. In this case the configuration should only be shared
45/// by connections intended for domains that offer the provided [`crate::client::EchConfig`] in
46/// their DNS zone.
47///
48/// # Defaults
49///
50/// * [`ClientConfig::max_fragment_size`]: the default is `None` (meaning 16kB).
51/// * [`ClientConfig::resumption`]: supports resumption with up to 256 server names, using session
52///   ids or tickets, with a max of eight tickets per server.
53/// * [`ClientConfig::alpn_protocols`]: the default is empty -- no ALPN protocol is negotiated.
54/// * [`ClientConfig::key_log`]: key material is not logged.
55/// * [`ClientConfig::cert_decompressors`]: depends on the crate features, see [`compress::default_cert_decompressors()`].
56/// * [`ClientConfig::cert_compressors`]: depends on the crate features, see [`compress::default_cert_compressors()`].
57/// * [`ClientConfig::cert_compression_cache`]: caches the most recently used 4 compressions
58///
59/// [`RootCertStore`]: crate::RootCertStore
60#[derive(Clone, Debug)]
61pub struct ClientConfig {
62    /// Which ALPN protocols we include in our client hello.
63    /// If empty, no ALPN extension is sent.
64    pub alpn_protocols: Vec<ApplicationProtocol<'static>>,
65
66    /// Whether to check the selected ALPN was offered.
67    ///
68    /// The default is true.
69    pub check_selected_alpn: bool,
70
71    /// How and when the client can resume a previous session.
72    ///
73    /// # Sharing `resumption` between `ClientConfig`s
74    /// In a program using many `ClientConfig`s it may improve resumption rates
75    /// (which has a significant impact on connection performance) if those
76    /// configs share a single `Resumption`.
77    ///
78    /// However, resumption is only allowed between two `ClientConfig`s if their
79    /// `client_auth_cert_resolver` (ie, potential client authentication credentials)
80    /// and `verifier` (ie, server certificate verification settings):
81    ///
82    /// - are the same type (determined by hashing their `TypeId`), and
83    /// - input the same data into [`ServerVerifier::hash_config()`] and
84    ///   [`ClientCredentialResolver::hash_config()`].
85    ///
86    /// To illustrate, imagine two `ClientConfig`s `A` and `B`.  `A` fully validates
87    /// the server certificate, `B` does not.  If `A` and `B` shared a resumption store,
88    /// it would be possible for a session originated by `B` to be inserted into the
89    /// store, and then resumed by `A`.  This would give a false impression to the user
90    /// of `A` that the server certificate is fully validated.
91    ///
92    /// [`ServerVerifier::hash_config()`]: verify::ServerVerifier::hash_config()
93    pub resumption: Resumption,
94
95    /// The maximum size of plaintext input to be emitted in a single TLS record.
96    /// A value of None is equivalent to the [TLS maximum] of 16 kB.
97    ///
98    /// rustls enforces an arbitrary minimum of 32 bytes for this field.
99    /// Out of range values are reported as errors when initializing a connection.
100    ///
101    /// Setting this value to a little less than the TCP MSS may improve latency
102    /// for stream-y workloads.
103    ///
104    /// [TLS maximum]: https://datatracker.ietf.org/doc/html/rfc8446#section-5.1
105    pub max_fragment_size: Option<usize>,
106
107    /// Whether to send the Server Name Indication (SNI) extension
108    /// during the client handshake.
109    ///
110    /// The default is true.
111    pub enable_sni: bool,
112
113    /// How to output key material for debugging.  The default
114    /// does nothing.
115    pub key_log: Arc<dyn KeyLog>,
116
117    /// Allows traffic secrets to be extracted after the handshake,
118    /// e.g. for kTLS setup.
119    pub enable_secret_extraction: bool,
120
121    /// Whether to send data on the first flight ("early data") in
122    /// TLS 1.3 handshakes.
123    ///
124    /// The default is false.
125    pub enable_early_data: bool,
126
127    /// If set to `true`, requires the server to support the extended
128    /// master secret extraction method defined in [RFC 7627].
129    ///
130    /// The default is `true` if the configured [`CryptoProvider`] is FIPS-compliant,
131    /// false otherwise.
132    ///
133    /// It must be set to `true` to meet FIPS requirement mentioned in section
134    /// **D.Q Transition of the TLS 1.2 KDF to Support the Extended Master
135    /// Secret** from [FIPS 140-3 IG.pdf].
136    ///
137    /// [RFC 7627]: https://datatracker.ietf.org/doc/html/rfc7627
138    /// [FIPS 140-3 IG.pdf]: https://csrc.nist.gov/csrc/media/Projects/cryptographic-module-validation-program/documents/fips%20140-3/FIPS%20140-3%20IG.pdf
139    pub require_ems: bool,
140
141    /// Items that affect the fundamental security properties of a connection.
142    pub(super) domain: SecurityDomain,
143
144    /// How to decompress the server's certificate chain.
145    ///
146    /// If this is non-empty, the [RFC8779] certificate compression
147    /// extension is offered, and any compressed certificates are
148    /// transparently decompressed during the handshake.
149    ///
150    /// This only applies to TLS1.3 connections.  It is ignored for
151    /// TLS1.2 connections.
152    ///
153    /// [RFC8779]: https://datatracker.ietf.org/doc/rfc8879/
154    pub cert_decompressors: Vec<&'static dyn compress::CertDecompressor>,
155
156    /// How to compress the client's certificate chain.
157    ///
158    /// If a server supports this extension, and advertises support
159    /// for one of the compression algorithms included here, the
160    /// client certificate will be compressed according to [RFC8779].
161    ///
162    /// This only applies to TLS1.3 connections.  It is ignored for
163    /// TLS1.2 connections.
164    ///
165    /// [RFC8779]: https://datatracker.ietf.org/doc/rfc8879/
166    pub cert_compressors: Vec<&'static dyn compress::CertCompressor>,
167
168    /// Caching for compressed certificates.
169    ///
170    /// This is optional: [`compress::CompressionCache::Disabled`] gives
171    /// a cache that does no caching.
172    pub cert_compression_cache: Arc<compress::CompressionCache>,
173
174    /// How to offer Encrypted Client Hello (ECH). The default is to not offer ECH.
175    pub(super) ech_mode: Option<EchMode>,
176}
177
178impl ClientConfig {
179    /// Create a builder for a client configuration with a specific [`CryptoProvider`].
180    ///
181    /// This will use the provider's configured ciphersuites.
182    ///
183    /// For more information, see the [`ConfigBuilder`] documentation.
184    pub fn builder(provider: Arc<CryptoProvider>) -> ConfigBuilder<Self, WantsVerifier> {
185        Self::builder_with_details(provider, Arc::new(DefaultTimeProvider))
186    }
187
188    /// Create a builder for a client configuration with no default implementation details.
189    ///
190    /// This API must be used by `no_std` users.
191    ///
192    /// You must provide a specific [`TimeProvider`].
193    ///
194    /// You must provide a specific [`CryptoProvider`].
195    ///
196    /// For more information, see the [`ConfigBuilder`] documentation.
197    pub fn builder_with_details(
198        provider: Arc<CryptoProvider>,
199        time_provider: Arc<dyn TimeProvider>,
200    ) -> ConfigBuilder<Self, WantsVerifier> {
201        ConfigBuilder {
202            state: WantsVerifier {
203                client_ech_mode: None,
204            },
205            provider,
206            time_provider,
207            side: PhantomData,
208        }
209    }
210
211    /// Create a new client connection builder for the given server name.
212    ///
213    /// The `ClientConfig` controls how the client behaves;
214    /// `name` is the name of server we want to talk to.
215    pub fn connect(self: &Arc<Self>, server_name: ServerName<'static>) -> ClientConnectionBuilder {
216        ClientConnectionBuilder {
217            config: self.clone(),
218            name: server_name,
219            alpn_protocols: None,
220        }
221    }
222
223    /// Access configuration options whose use is dangerous and requires
224    /// extra care.
225    pub fn dangerous(&mut self) -> danger::DangerousClientConfig<'_> {
226        danger::DangerousClientConfig { cfg: self }
227    }
228
229    /// Return the FIPS validation status for connections made with this configuration.
230    ///
231    /// This is different from [`CryptoProvider::fips()`]: [`CryptoProvider::fips()`]
232    /// is concerned only with cryptography, whereas this _also_ covers TLS-level
233    /// configuration that NIST recommends, as well as ECH HPKE suites if applicable.
234    pub fn fips(&self) -> FipsStatus {
235        if !self.require_ems {
236            return FipsStatus::Unvalidated;
237        }
238
239        let status = self.domain.provider.fips();
240        match &self.ech_mode {
241            Some(ech) => Ord::min(status, ech.fips()),
242            None => status,
243        }
244    }
245
246    /// Return the crypto provider used to construct this client configuration.
247    pub fn provider(&self) -> &Arc<CryptoProvider> {
248        &self.domain.provider
249    }
250
251    /// Return the resolver for this client configuration.
252    ///
253    /// This is the object that determines which credentials to use for client
254    /// authentication.
255    pub fn resolver(&self) -> &Arc<dyn ClientCredentialResolver> {
256        &self.domain.client_auth_cert_resolver
257    }
258
259    /// Return the resolver for this client configuration.
260    ///
261    /// This is the object that determines which credentials to use for client
262    /// authentication.
263    pub fn verifier(&self) -> &Arc<dyn verify::ServerVerifier> {
264        &self.domain.verifier
265    }
266
267    pub(crate) fn supports_version(&self, v: ProtocolVersion) -> bool {
268        self.domain.provider.supports_version(v)
269    }
270
271    pub(super) fn find_cipher_suite(&self, suite: CipherSuite) -> Option<SupportedCipherSuite> {
272        self.domain
273            .provider
274            .iter_cipher_suites()
275            .find(|&scs| scs.suite() == suite)
276    }
277
278    pub(super) fn current_time(&self) -> Result<UnixTime, Error> {
279        self.domain
280            .time_provider
281            .current_time()
282            .ok_or(Error::FailedToGetCurrentTime)
283    }
284
285    /// A hash which partitions this config's use of the [`Self::resumption`] store.
286    pub(super) fn config_hash(&self) -> [u8; 32] {
287        self.domain.config_hash
288    }
289}
290
291struct HashAdapter<'a>(&'a mut dyn hash::Context);
292
293impl Hasher for HashAdapter<'_> {
294    fn finish(&self) -> u64 {
295        // SAFETY: this is private to `SecurityDomain::new`, which guarantees `hash::Output`
296        // is at least 32 bytes.
297        u64::from_be_bytes(
298            self.0.fork_finish().as_ref()[..8]
299                .try_into()
300                .unwrap(),
301        )
302    }
303
304    fn write(&mut self, bytes: &[u8]) {
305        self.0.update(bytes)
306    }
307}
308
309/// Client session data store for possible future resumption.
310///
311/// All data in this interface should be treated as **highly sensitive**, containing enough key
312/// material to break all security of the corresponding session.
313///
314/// `set_`, `insert_`, `remove_` and `take_` operations are mutating; this isn't
315/// expressed in the type system to allow implementations freedom in
316/// how to achieve interior mutability.  `Mutex` is a common choice.
317pub trait ClientSessionStore: fmt::Debug + Send + Sync {
318    /// Remember what `NamedGroup` the given server chose.
319    fn set_kx_hint(&self, key: ClientSessionKey<'static>, group: NamedGroup);
320
321    /// Value most recently passed to `set_kx_hint` for the given `key`.
322    ///
323    /// If `None` is returned, the caller chooses the first configured group, and an extra round
324    /// trip might happen if that choice is unsatisfactory to the server.
325    fn kx_hint(&self, key: &ClientSessionKey<'_>) -> Option<NamedGroup>;
326
327    /// Remember a TLS1.2 session, allowing resumption of this connection in the future.
328    ///
329    /// At most one of these per session key can be remembered at a time.
330    fn set_tls12_session(&self, key: ClientSessionKey<'static>, value: Tls12Session);
331
332    /// Get the most recently saved TLS1.2 session for `key` provided to `set_tls12_session`.
333    fn tls12_session(&self, key: &ClientSessionKey<'_>) -> Option<Tls12Session>;
334
335    /// Remove and forget any saved TLS1.2 session for `key`.
336    fn remove_tls12_session(&self, key: &ClientSessionKey<'static>);
337
338    /// Remember a TLS1.3 ticket, allowing resumption of this connection in the future.
339    ///
340    /// This can be called multiple times for a given session, allowing multiple independent tickets
341    /// to be valid at once.  The number of times this is called is controlled by the server, so
342    /// implementations of this trait should apply a reasonable bound of how many items are stored
343    /// simultaneously.
344    fn insert_tls13_ticket(&self, key: ClientSessionKey<'static>, value: Tls13Session);
345
346    /// Return a TLS1.3 ticket previously provided to `insert_tls13_ticket()`.
347    ///
348    /// Implementations of this trait must return each value provided to `insert_tls13_ticket()` _at most once_.
349    fn take_tls13_ticket(&self, key: &ClientSessionKey<'static>) -> Option<Tls13Session>;
350}
351
352/// Identifies a security context and server in the [`ClientSessionStore`] interface.
353#[derive(Clone, Debug, Eq, Hash, PartialEq)]
354#[non_exhaustive]
355pub struct ClientSessionKey<'a> {
356    /// A hash to partition the client storage between different security domains.
357    pub config_hash: [u8; 32],
358
359    /// Transport-level identity of the server.
360    pub server_name: ServerName<'a>,
361}
362
363impl ClientSessionKey<'_> {
364    /// Copy the value to own its contents.
365    pub fn to_owned(&self) -> ClientSessionKey<'static> {
366        let Self {
367            config_hash,
368            server_name,
369        } = self;
370        ClientSessionKey {
371            config_hash: *config_hash,
372            server_name: server_name.to_owned(),
373        }
374    }
375}
376
377/// A trait for the ability to choose a certificate chain and
378/// private key for the purposes of client authentication.
379pub trait ClientCredentialResolver: fmt::Debug + Send + Sync {
380    /// Resolve a client certificate chain/private key to use as the client's identity.
381    ///
382    /// The `SelectedCredential` returned from this method contains an identity and a
383    /// one-time-use [`Signer`] wrapping the private key. This is usually obtained via a
384    /// [`Credentials`], on which an implementation can call [`Credentials::signer()`].
385    /// An implementation can either store long-lived [`Credentials`] values, or instantiate
386    /// them as needed using one of its constructors.
387    ///
388    /// Return `None` to continue the handshake without any client
389    /// authentication.  The server may reject the handshake later
390    /// if it requires authentication.
391    ///
392    /// [RFC 5280 A.1]: https://www.rfc-editor.org/rfc/rfc5280#appendix-A.1
393    ///
394    /// [`Credentials`]: crate::crypto::Credentials
395    /// [`Credentials::signer()`]: crate::crypto::Credentials::signer
396    /// [`Signer`]: crate::crypto::Signer
397    fn resolve(&self, request: &CredentialRequest<'_>) -> Option<SelectedCredential>;
398
399    /// Returns which [`CertificateType`]s this resolver supports.
400    ///
401    /// Should return the empty slice if the resolver does not have any credentials to send.
402    /// Implementations should return the same value every time.
403    ///
404    /// See [RFC 7250](https://tools.ietf.org/html/rfc7250) for more information.
405    fn supported_certificate_types(&self) -> &'static [CertificateType];
406
407    /// Instance configuration should be input to `h`.
408    fn hash_config(&self, h: &mut dyn Hasher);
409}
410
411/// Context from the server to inform client credential selection.
412pub struct CredentialRequest<'a> {
413    pub(super) negotiated_type: CertificateType,
414    pub(super) root_hint_subjects: &'a [DistinguishedName],
415    pub(super) signature_schemes: &'a [SignatureScheme],
416}
417
418impl CredentialRequest<'_> {
419    /// List of certificate authority subject distinguished names provided by the server.
420    ///
421    /// If the list is empty, the client should send whatever certificate it has. The hints
422    /// are expected to be DER-encoded X.500 distinguished names, per [RFC 5280 A.1]. Note that
423    /// the encoding comes from the server and has not been validated by rustls.
424    ///
425    /// See [`DistinguishedName`] for more information on decoding with external crates like
426    /// `x509-parser`.
427    ///
428    /// [`DistinguishedName`]: crate::DistinguishedName
429    pub fn root_hint_subjects(&self) -> &[DistinguishedName] {
430        self.root_hint_subjects
431    }
432
433    /// Get the compatible signature schemes.
434    pub fn signature_schemes(&self) -> &[SignatureScheme] {
435        self.signature_schemes
436    }
437
438    /// The negotiated certificate type.
439    ///
440    /// If the server does not support [RFC 7250], this will be `CertificateType::X509`.
441    ///
442    /// [RFC 7250]: https://tools.ietf.org/html/rfc7250
443    pub fn negotiated_type(&self) -> CertificateType {
444        self.negotiated_type
445    }
446}
447
448/// Items that affect the fundamental security properties of a connection.
449///
450/// This is its own type because `config_hash` depends on the other fields:
451/// fields therefore should not be mutated, but an entire object created
452/// through [`Self::new`] for any edits.
453#[derive(Clone, Debug)]
454pub(super) struct SecurityDomain {
455    /// Provides the current system time
456    time_provider: Arc<dyn TimeProvider>,
457
458    /// Source of randomness and other crypto.
459    provider: Arc<CryptoProvider>,
460
461    /// How to verify the server certificate chain.
462    verifier: Arc<dyn verify::ServerVerifier>,
463
464    /// How to decide what client auth certificate/keys to use.
465    client_auth_cert_resolver: Arc<dyn ClientCredentialResolver>,
466
467    config_hash: [u8; 32],
468}
469
470impl SecurityDomain {
471    pub(crate) fn new(
472        provider: Arc<CryptoProvider>,
473        client_auth_cert_resolver: Arc<dyn ClientCredentialResolver + 'static>,
474        verifier: Arc<dyn verify::ServerVerifier + 'static>,
475        time_provider: Arc<dyn TimeProvider + 'static>,
476    ) -> Self {
477        // Use a hash function that outputs at least 32 bytes.
478        let hash = provider
479            .iter_cipher_suites()
480            .map(|cs| cs.hash_provider())
481            .find(|h| h.output_len() >= 32)
482            .expect("no suitable cipher suite available (with |H| >= 32)"); // this is -- in practice -- all cipher suites
483
484        let mut h = hash.start();
485        let mut adapter = HashAdapter(h.as_mut());
486
487        // Include TypeId of impl, so two different types with different non-configured
488        // behavior do not collide even if their `hash_config()`s are the same.
489        client_auth_cert_resolver
490            .type_id()
491            .hash(&mut DynHasher(&mut adapter));
492        client_auth_cert_resolver.hash_config(&mut adapter);
493
494        verifier
495            .type_id()
496            .hash(&mut DynHasher(&mut adapter));
497        verifier.hash_config(&mut adapter);
498
499        time_provider
500            .type_id()
501            .hash(&mut DynHasher(&mut adapter));
502
503        let config_hash = h.finish().as_ref()[..32]
504            .try_into()
505            .unwrap();
506
507        Self {
508            time_provider,
509            provider,
510            verifier,
511            client_auth_cert_resolver,
512            config_hash,
513        }
514    }
515
516    fn with_verifier(&self, verifier: Arc<dyn verify::ServerVerifier + 'static>) -> Self {
517        let Self {
518            time_provider,
519            provider,
520            verifier: _,
521            client_auth_cert_resolver,
522            config_hash: _,
523        } = self;
524        Self::new(
525            provider.clone(),
526            client_auth_cert_resolver.clone(),
527            verifier,
528            time_provider.clone(),
529        )
530    }
531}
532
533/// Configuration for how/when a client is allowed to resume a previous session.
534#[derive(Clone, Debug)]
535pub struct Resumption {
536    /// How we store session data or tickets. The default is to use an in-memory
537    /// [super::handy::ClientSessionMemoryCache].
538    pub(super) store: Arc<dyn ClientSessionStore>,
539
540    /// What mechanism is used for resuming a TLS 1.2 session.
541    pub(super) tls12_resumption: Tls12Resumption,
542}
543
544impl Resumption {
545    /// Create a new `Resumption` that stores data for the given number of sessions in memory.
546    ///
547    /// This is the default `Resumption` choice, and enables resuming a TLS 1.2 session with
548    /// a session id or RFC 5077 ticket.
549    pub fn in_memory_sessions(num: usize) -> Self {
550        Self {
551            store: Arc::new(ClientSessionMemoryCache::new(num)),
552            tls12_resumption: Tls12Resumption::SessionIdOrTickets,
553        }
554    }
555
556    /// Use a custom [`ClientSessionStore`] implementation to store sessions.
557    ///
558    /// By default, enables resuming a TLS 1.2 session with a session id or RFC 5077 ticket.
559    pub fn store(store: Arc<dyn ClientSessionStore>) -> Self {
560        Self {
561            store,
562            tls12_resumption: Tls12Resumption::SessionIdOrTickets,
563        }
564    }
565
566    /// Disable all use of session resumption.
567    pub fn disabled() -> Self {
568        Self {
569            store: Arc::new(NoClientSessionStorage),
570            tls12_resumption: Tls12Resumption::Disabled,
571        }
572    }
573
574    /// Configure whether TLS 1.2 sessions may be resumed, and by what mechanism.
575    ///
576    /// This is meaningless if you've disabled resumption entirely, which is the case in `no-std`
577    /// contexts.
578    pub fn tls12_resumption(mut self, tls12: Tls12Resumption) -> Self {
579        self.tls12_resumption = tls12;
580        self
581    }
582}
583
584impl Default for Resumption {
585    /// Create an in-memory session store resumption with up to 256 server names, allowing
586    /// a TLS 1.2 session to resume with a session id or RFC 5077 ticket.
587    fn default() -> Self {
588        Self::in_memory_sessions(256)
589    }
590}
591
592/// What mechanisms to support for resuming a TLS 1.2 session.
593#[non_exhaustive]
594#[derive(Clone, Copy, Debug, PartialEq)]
595pub enum Tls12Resumption {
596    /// Disable 1.2 resumption.
597    Disabled,
598    /// Support 1.2 resumption using session ids only.
599    SessionIdOnly,
600    /// Support 1.2 resumption using session ids or RFC 5077 tickets.
601    ///
602    /// See[^1] for why you might like to disable RFC 5077 by instead choosing the `SessionIdOnly`
603    /// option. Note that TLS 1.3 tickets do not have those issues.
604    ///
605    /// [^1]: <https://words.filippo.io/we-need-to-talk-about-session-tickets/>
606    SessionIdOrTickets,
607}
608
609impl ConfigBuilder<ClientConfig, WantsVerifier> {
610    /// Choose how to verify server certificates.
611    ///
612    /// Using this function does not configure revocation.  If you wish to
613    /// configure revocation, instead use:
614    ///
615    /// ```diff
616    /// - .with_root_certificates(root_store)
617    /// + .with_webpki_verifier(
618    /// +   WebPkiServerVerifier::builder(root_store, crypto_provider)
619    /// +   .with_crls(...)
620    /// +   .build()?
621    /// + )
622    /// ```
623    #[cfg(feature = "webpki")]
624    pub fn with_root_certificates(
625        self,
626        root_store: impl Into<Arc<webpki::RootCertStore>>,
627    ) -> ConfigBuilder<ClientConfig, WantsClientCert> {
628        let algorithms = self
629            .provider
630            .signature_verification_algorithms;
631        self.with_webpki_verifier(
632            WebPkiServerVerifier::new_without_revocation(root_store, algorithms).into(),
633        )
634    }
635
636    /// Choose how to verify server certificates using a webpki verifier.
637    ///
638    /// See [`webpki::WebPkiServerVerifier::builder`] and
639    /// [`webpki::WebPkiServerVerifier::builder`] for more information.
640    #[cfg(feature = "webpki")]
641    pub fn with_webpki_verifier(
642        self,
643        verifier: Arc<WebPkiServerVerifier>,
644    ) -> ConfigBuilder<ClientConfig, WantsClientCert> {
645        ConfigBuilder {
646            state: WantsClientCert {
647                verifier,
648                client_ech_mode: self.state.client_ech_mode,
649            },
650            provider: self.provider,
651            time_provider: self.time_provider,
652            side: PhantomData,
653        }
654    }
655
656    /// Enable Encrypted Client Hello (ECH) in the given mode.
657    ///
658    /// This requires TLS 1.3 as the only supported protocol version to meet the requirement
659    /// to support ECH.  At the end, the config building process will return an error if either
660    /// TLS1.3 _is not_ supported by the provider, or TLS1.2 _is_ supported.
661    ///
662    /// The `ClientConfig` that will be produced by this builder will be specific to the provided
663    /// [`crate::client::EchConfig`] and may not be appropriate for all connections made by the program.
664    /// In this case the configuration should only be shared by connections intended for domains
665    /// that offer the provided [`crate::client::EchConfig`] in their DNS zone.
666    pub fn with_ech(mut self, mode: EchMode) -> Self {
667        self.state.client_ech_mode = Some(mode);
668        self
669    }
670
671    /// Access configuration options whose use is dangerous and requires
672    /// extra care.
673    pub fn dangerous(self) -> danger::DangerousClientConfigBuilder {
674        danger::DangerousClientConfigBuilder { cfg: self }
675    }
676}
677
678/// A config builder state where the caller needs to supply whether and how to provide a client
679/// certificate.
680///
681/// For more information, see the [`ConfigBuilder`] documentation.
682#[derive(Clone)]
683pub struct WantsClientCert {
684    verifier: Arc<dyn verify::ServerVerifier>,
685    client_ech_mode: Option<EchMode>,
686}
687
688impl ConfigBuilder<ClientConfig, WantsClientCert> {
689    /// Sets a single certificate chain and matching private key for use
690    /// in client authentication.
691    ///
692    /// `cert_chain` is a vector of DER-encoded certificates.
693    /// `key_der` is a DER-encoded private key as PKCS#1, PKCS#8, or SEC1. The
694    /// `aws-lc-rs` and `ring` [`CryptoProvider`]s support
695    /// all three encodings, but other `CryptoProviders` may not.
696    ///
697    /// This function fails if `key_der` is invalid.
698    #[cfg(feature = "webpki")]
699    pub fn with_client_auth_cert(
700        self,
701        identity: Arc<Identity<'static>>,
702        key_der: PrivateKeyDer<'static>,
703    ) -> Result<ClientConfig, Error> {
704        let credentials = Credentials::from_der(identity, key_der, &self.provider)?;
705        self.with_client_credential_resolver(Arc::new(SingleCredential::from(credentials)))
706    }
707
708    /// Do not support client auth.
709    pub fn with_no_client_auth(self) -> Result<ClientConfig, Error> {
710        self.with_client_credential_resolver(Arc::new(FailResolveClientCert {}))
711    }
712
713    /// Sets a custom [`ClientCredentialResolver`].
714    pub fn with_client_credential_resolver(
715        self,
716        client_auth_cert_resolver: Arc<dyn ClientCredentialResolver>,
717    ) -> Result<ClientConfig, Error> {
718        self.provider.consistency_check()?;
719
720        if self.state.client_ech_mode.is_some() {
721            match (
722                self.provider
723                    .tls12_cipher_suites
724                    .is_empty(),
725                self.provider
726                    .tls13_cipher_suites
727                    .is_empty(),
728            ) {
729                (_, true) => return Err(ApiMisuse::EchRequiresTls13Support.into()),
730                (false, _) => return Err(ApiMisuse::EchForbidsTls12Support.into()),
731                (true, false) => {}
732            };
733        }
734
735        let require_ems = !matches!(self.provider.fips(), FipsStatus::Unvalidated);
736        Ok(ClientConfig {
737            alpn_protocols: Vec::new(),
738            check_selected_alpn: true,
739            resumption: Resumption::default(),
740            max_fragment_size: None,
741            enable_sni: true,
742            key_log: Arc::new(NoKeyLog {}),
743            enable_secret_extraction: false,
744            enable_early_data: false,
745            require_ems,
746            domain: SecurityDomain::new(
747                self.provider,
748                client_auth_cert_resolver,
749                self.state.verifier,
750                self.time_provider,
751            ),
752            cert_decompressors: compress::default_cert_decompressors().to_vec(),
753            cert_compressors: compress::default_cert_compressors().to_vec(),
754            cert_compression_cache: Arc::new(compress::CompressionCache::default()),
755            ech_mode: self.state.client_ech_mode,
756        })
757    }
758}
759
760/// Container for unsafe APIs
761pub(super) mod danger {
762    use core::marker::PhantomData;
763
764    use crate::client::WantsClientCert;
765    use crate::client::config::ClientConfig;
766    use crate::sync::Arc;
767    use crate::verify::ServerVerifier;
768    use crate::{ConfigBuilder, WantsVerifier};
769
770    /// Accessor for dangerous configuration options.
771    #[derive(Debug)]
772    pub struct DangerousClientConfig<'a> {
773        /// The underlying ClientConfig
774        pub(super) cfg: &'a mut ClientConfig,
775    }
776
777    impl DangerousClientConfig<'_> {
778        /// Overrides the default `ServerVerifier` with something else.
779        pub fn set_certificate_verifier(&mut self, verifier: Arc<dyn ServerVerifier>) {
780            self.cfg.domain = self.cfg.domain.with_verifier(verifier);
781        }
782    }
783
784    /// Accessor for dangerous configuration options.
785    #[derive(Debug)]
786    pub struct DangerousClientConfigBuilder {
787        /// The underlying ClientConfigBuilder
788        pub(super) cfg: ConfigBuilder<ClientConfig, WantsVerifier>,
789    }
790
791    impl DangerousClientConfigBuilder {
792        /// Set a custom certificate verifier.
793        pub fn with_custom_certificate_verifier(
794            self,
795            verifier: Arc<dyn ServerVerifier>,
796        ) -> ConfigBuilder<ClientConfig, WantsClientCert> {
797            ConfigBuilder {
798                state: WantsClientCert {
799                    verifier,
800                    client_ech_mode: self.cfg.state.client_ech_mode,
801                },
802                provider: self.cfg.provider,
803                time_provider: self.cfg.time_provider,
804                side: PhantomData,
805            }
806        }
807    }
808}