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