rustls/builder.rs
1use alloc::format;
2use core::fmt;
3use core::marker::PhantomData;
4
5use crate::client::EchMode;
6use crate::crypto::CryptoProvider;
7use crate::sync::Arc;
8use crate::time_provider::TimeProvider;
9#[cfg(doc)]
10use crate::{ClientConfig, ServerConfig};
11
12/// A [builder] for [`ServerConfig`] or [`ClientConfig`] values.
13///
14/// To get one of these, call [`ServerConfig::builder()`] or [`ClientConfig::builder()`].
15///
16/// To build a config, you must make at least three decisions (in order):
17///
18/// - What crypto provider do you want to use?
19/// - How should this client or server verify certificates provided by its peer?
20/// - What certificates should this client or server present to its peer?
21///
22/// For settings besides these, see the fields of [`ServerConfig`] and [`ClientConfig`].
23///
24/// The rustls project recommends the crypto provider based on aws-lc-rs for production use.
25/// This can be selected by passing in `rustls_aws_lc_rs::DEFAULT_PROVIDER`,
26/// which includes safe defaults for cipher suites and protocol versions.
27///
28/// After choosing the `CryptoProvider`, you must choose (a) how to verify certificates and (b) what certificates
29/// (if any) to send to the peer. The methods to do this are specific to whether you're building a ClientConfig
30/// or a ServerConfig, as tracked by the [`ConfigSide`] type parameter on the various impls of ConfigBuilder.
31///
32/// A `Result<ClientConfig, Error>` or `Result<ServerConfig, Error>` is the outcome of the builder process.
33/// The error is used to report consistency problems with the configuration. For example, it's an error
34/// to have a `CryptoProvider` that has no cipher suites.
35///
36/// # ClientConfig certificate configuration
37///
38/// For a client, _certificate verification_ must be configured either by calling one of:
39/// - [`ConfigBuilder::with_root_certificates`] or
40/// - [`ConfigBuilder::dangerous()`] and [`DangerousClientConfigBuilder::with_custom_certificate_verifier`]
41///
42/// Next, _certificate sending_ (also known as "client authentication", "mutual TLS", or "mTLS") must be configured
43/// or disabled using one of:
44/// - [`ConfigBuilder::with_no_client_auth`] - to not send client authentication (most common)
45/// - [`ConfigBuilder::with_client_auth_cert`] - to always send a specific certificate
46/// - [`ConfigBuilder::with_server_credential_resolver`] - to send a certificate chosen dynamically
47///
48/// For example:
49///
50/// ```ignore
51/// # use std::sync::Arc;
52/// # use rustls::ClientConfig;
53/// # use rustls::crypto::TEST_PROVIDER as DEFAULT_PROVIDER;
54/// # let root_certs = rustls::RootCertStore::empty();
55/// ClientConfig::builder(Arc::new(DEFAULT_PROVIDER))
56/// .with_root_certificates(root_certs)
57/// .with_no_client_auth()
58/// .unwrap();
59/// ```
60///
61/// # ServerConfig certificate configuration
62///
63/// For a server, _certificate verification_ must be configured by calling one of:
64/// - [`ConfigBuilder::with_no_client_auth`] - to not require client authentication (most common)
65/// - [`ConfigBuilder::with_client_cert_verifier`] - to use a custom verifier
66///
67/// Next, _certificate sending_ must be configured by calling one of:
68/// - [`ConfigBuilder::with_single_cert`] - to send a specific certificate
69/// - [`ConfigBuilder::with_single_cert_with_ocsp`] - to send a specific certificate, plus stapled OCSP
70/// - [`ConfigBuilder::with_server_credential_resolver`] - to send a certificate chosen dynamically
71///
72/// For example:
73///
74/// ```ignore
75/// # use std::sync::Arc;
76/// # use rustls::crypto::Identity;
77/// # use rustls::crypto::TEST_PROVIDER as DEFAULT_PROVIDER;
78/// # use rustls::ServerConfig;
79/// # let certs = vec![];
80/// # let private_key = pki_types::PrivateKeyDer::from(
81/// # pki_types::PrivatePkcs8KeyDer::from(vec![])
82/// # );
83/// ServerConfig::builder(Arc::new(DEFAULT_PROVIDER))
84/// .with_no_client_auth()
85/// .with_single_cert(Arc::new(Identity::from_cert_chain(certs).unwrap()), private_key)
86/// .expect("bad certificate/key/provider");
87/// ```
88///
89/// # Types
90///
91/// ConfigBuilder uses the [typestate] pattern to ensure at compile time that each required
92/// configuration item is provided exactly once. This is tracked in the `State` type parameter,
93/// which can have these values:
94///
95/// - [`WantsVerifier`]
96/// - [`WantsClientCert`]
97/// - [`WantsServerCert`]
98///
99/// The other type parameter is `Side`, which is either `ServerConfig` or `ClientConfig`
100/// depending on whether the ConfigBuilder was built with [`ServerConfig::builder()`] or
101/// [`ClientConfig::builder()`].
102///
103/// You won't need to write out either of these type parameters explicitly. If you write a
104/// correct chain of configuration calls they will be used automatically. If you write an
105/// incorrect chain of configuration calls you will get an error message from the compiler
106/// mentioning some of these types.
107///
108/// Additionally, ServerConfig and ClientConfig carry a private field containing a
109/// [`CryptoProvider`], from [`ClientConfig::builder()`] or
110/// [`ServerConfig::builder()`]. This determines which cryptographic backend
111/// is used.
112///
113/// [builder]: https://rust-unofficial.github.io/patterns/patterns/creational/builder.html
114/// [typestate]: http://cliffle.com/blog/rust-typestate/
115/// [`ServerConfig`]: crate::ServerConfig
116/// [`ServerConfig::builder`]: crate::ServerConfig::builder
117/// [`ClientConfig`]: crate::ClientConfig
118/// [`ClientConfig::builder()`]: crate::ClientConfig::builder()
119/// [`ServerConfig::builder()`]: crate::ServerConfig::builder()
120/// [`ClientConfig::builder()`]: crate::ClientConfig::builder()
121/// [`ServerConfig::builder()`]: crate::ServerConfig::builder()
122/// [`ConfigBuilder<ClientConfig, WantsVerifier>`]: struct.ConfigBuilder.html#impl-3
123/// [`ConfigBuilder<ServerConfig, WantsVerifier>`]: struct.ConfigBuilder.html#impl-6
124/// [`WantsClientCert`]: crate::client::WantsClientCert
125/// [`WantsServerCert`]: crate::server::WantsServerCert
126/// [`CryptoProvider::get_default`]: crate::crypto::CryptoProvider::get_default
127/// [`DangerousClientConfigBuilder::with_custom_certificate_verifier`]: crate::client::danger::DangerousClientConfigBuilder::with_custom_certificate_verifier
128#[derive(Clone)]
129pub struct ConfigBuilder<Side: ConfigSide, State> {
130 pub(crate) state: State,
131 pub(crate) provider: Arc<CryptoProvider>,
132 pub(crate) time_provider: Arc<dyn TimeProvider>,
133 pub(crate) side: PhantomData<Side>,
134}
135
136impl<Side: ConfigSide, State> ConfigBuilder<Side, State> {
137 /// Return the crypto provider used to construct this builder.
138 pub fn crypto_provider(&self) -> &Arc<CryptoProvider> {
139 &self.provider
140 }
141}
142
143impl<Side: ConfigSide, State: fmt::Debug> fmt::Debug for ConfigBuilder<Side, State> {
144 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
145 let side_name = core::any::type_name::<Side>();
146 let (ty, _) = side_name
147 .split_once('<')
148 .unwrap_or((side_name, ""));
149 let (_, name) = ty.rsplit_once("::").unwrap_or(("", ty));
150
151 f.debug_struct(&format!("ConfigBuilder<{name}, _>",))
152 .field("state", &self.state)
153 .finish_non_exhaustive()
154 }
155}
156
157/// Config builder state where the caller must supply a verifier.
158///
159/// For more information, see the [`ConfigBuilder`] documentation.
160#[derive(Clone, Debug)]
161pub struct WantsVerifier {
162 pub(crate) client_ech_mode: Option<EchMode>,
163}
164
165/// Helper trait to abstract [`ConfigBuilder`] over building a [`ClientConfig`] or [`ServerConfig`].
166///
167/// [`ClientConfig`]: crate::ClientConfig
168/// [`ServerConfig`]: crate::ServerConfig
169pub trait ConfigSide: crate::sealed::Sealed {}
170
171impl ConfigSide for crate::ClientConfig {}
172impl ConfigSide for crate::ServerConfig {}
173
174impl crate::sealed::Sealed for crate::ClientConfig {}
175impl crate::sealed::Sealed for crate::ServerConfig {}