Skip to content

Commit 08d5fd9

Browse files
committed
Use extension traits to simplify config
1 parent 6927b45 commit 08d5fd9

File tree

4 files changed

+69
-33
lines changed

4 files changed

+69
-33
lines changed

Cargo.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,26 +91,21 @@ rustls-platform-verifier = "0.3"
9191
To get a rustls `ClientConfig` configured to use the platform verifier use:
9292

9393
```rust
94-
let config = rustls_platform_verifier::tls_config();
94+
use rustls::ClientConfig;
95+
use rustls_platform_verifier::ConfigVerifierExt;
96+
let config = ClientConfig::with_platform_verifier();
9597
```
9698

9799
This crate will use the [rustls process-default crypto provider](https://docs.rs/rustls/latest/rustls/crypto/struct.CryptoProvider.html#using-the-per-process-default-cryptoprovider). To construct a `ClientConfig` with a different `CryptoProvider`, use:
98100

99101
```rust
100-
let arc_crypto_provider = std::sync::Arc::new(rustls::crypto::ring::default_provider());
101-
let config = rustls_platform_verifier::tls_config_with_provider(arc_crypto_provider);
102-
```
103-
104-
If you want to adapt the configuration, you can build the `ClientConfig` like this:
105-
106-
```rust
107-
use std::sync::Arc;
108102
use rustls::ClientConfig;
109-
use rustls_platform_verifier::Verifier;
110-
111-
let mut config = ClientConfig::builder()
112-
.dangerous() // The `Verifier` we're using is actually safe
113-
.with_custom_certificate_verifier(Arc::new(Verifier::new()))
103+
use rustls_platform_verifier::BuilderVerifierExt;
104+
let arc_crypto_provider = std::sync::Arc::new(rustls::crypto::ring::default_provider());
105+
let config = ClientConfig::builder_with_provider(arc_crypto_provider)
106+
.with_safe_default_protocol_versions()
107+
.unwrap()
108+
.with_platform_verifier()
114109
.with_no_client_auth();
115110
```
116111

rustls-platform-verifier/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ cert-logging = ["base64"]
2929
docsrs = ["jni"]
3030

3131
[dependencies]
32-
rustls = { version = "0.23", default-features = false, features = ["std"] }
32+
rustls = { version = "0.23.16", default-features = false, features = ["std"] }
3333
log = { version = "0.4" }
3434
base64 = { version = "0.22", optional = true } # Only used when the `cert-logging` feature is enabled.
3535
jni = { version = "0.19", default-features = false, optional = true } # Only used during doc generation

rustls-platform-verifier/src/lib.rs

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![doc = include_str!("../README.md")]
33
#![warn(missing_docs)]
44

5-
use rustls::ClientConfig;
5+
use rustls::{client::WantsClientCert, ClientConfig, ConfigBuilder, WantsVerifier};
66
use std::sync::Arc;
77

88
mod verification;
@@ -36,9 +36,11 @@ pub use tests::ffi::*;
3636
/// ```ignore
3737
/// # use reqwest::ClientBuilder;
3838
/// #[tokio::main]
39+
/// use rustls_platform_verifier::ConfigVerifierExt;
40+
///
3941
/// async fn main() {
4042
/// let client = ClientBuilder::new()
41-
/// .use_preconfigured_tls(rustls_platform_verifier::tls_config())
43+
/// .use_preconfigured_tls(ClientConfig::with_platform_verifier())
4244
/// .build()
4345
/// .expect("nothing should fail");
4446
///
@@ -49,17 +51,14 @@ pub use tests::ffi::*;
4951
/// **Important:** You must ensure that your `reqwest` version is using the same Rustls
5052
/// version as this crate or it will panic when downcasting the `&dyn Any` verifier.
5153
///
52-
/// If you require more control over the rustls `ClientConfig`, you can
53-
/// instantiate a [Verifier] with [Verifier::default] and then use it
54-
/// with [`DangerousClientConfigBuilder::with_custom_certificate_verifier`][rustls::client::danger::DangerousClientConfigBuilder::with_custom_certificate_verifier].
54+
/// If you require more control over the rustls [`ClientConfig`], you can import the
55+
/// [`BuilderVerifierExt`] trait and call `.with_platform_verifier()` on the [`ConfigBuilder`].
5556
///
5657
/// Refer to the crate level documentation to see what platforms
5758
/// are currently supported.
59+
#[deprecated(since = "0.4.0", note = "use the `ConfigVerifierExt` instead")]
5860
pub fn tls_config() -> ClientConfig {
59-
ClientConfig::builder()
60-
.dangerous()
61-
.with_custom_certificate_verifier(Arc::new(Verifier::new()))
62-
.with_no_client_auth()
61+
ClientConfig::with_platform_verifier()
6362
}
6463

6564
/// Attempts to construct a `rustls` configuration that verifies TLS certificates in the best way
@@ -71,13 +70,13 @@ pub fn tls_config() -> ClientConfig {
7170
/// # Errors
7271
///
7372
/// Propagates any error returned by [`rustls::ConfigBuilder::with_safe_default_protocol_versions`].
73+
#[deprecated(since = "0.4.0", note = "use the `BuilderVerifierExt` instead")]
7474
pub fn tls_config_with_provider(
7575
provider: Arc<rustls::crypto::CryptoProvider>,
7676
) -> Result<ClientConfig, rustls::Error> {
77-
Ok(ClientConfig::builder_with_provider(provider.clone())
77+
Ok(ClientConfig::builder_with_provider(provider)
7878
.with_safe_default_protocol_versions()?
79-
.dangerous()
80-
.with_custom_certificate_verifier(Arc::new(Verifier::new().with_provider(provider)))
79+
.with_platform_verifier()
8180
.with_no_client_auth())
8281
}
8382

@@ -88,3 +87,45 @@ pub fn tls_config_with_provider(
8887
pub fn verifier_for_dbg(root: &[u8]) -> Arc<dyn rustls::client::danger::ServerCertVerifier> {
8988
Arc::new(Verifier::new_with_fake_root(root))
9089
}
90+
91+
/// Extension trait to help configure [`ClientConfig`]s with the platform verifier.
92+
pub trait BuilderVerifierExt {
93+
/// Configures the `ClientConfig` with the platform verifier.
94+
///
95+
/// ```rust
96+
/// use rustls::ClientConfig;
97+
/// use rustls_platform_verifier::BuilderVerifierExt;
98+
/// let config = ClientConfig::builder()
99+
/// .with_platform_verifier()
100+
/// .with_no_client_auth();
101+
/// ```
102+
fn with_platform_verifier(self) -> ConfigBuilder<ClientConfig, WantsClientCert>;
103+
}
104+
105+
impl BuilderVerifierExt for ConfigBuilder<ClientConfig, WantsVerifier> {
106+
fn with_platform_verifier(self) -> ConfigBuilder<ClientConfig, WantsClientCert> {
107+
let provider = self.crypto_provider().clone();
108+
self.dangerous()
109+
.with_custom_certificate_verifier(Arc::new(Verifier::new().with_provider(provider)))
110+
}
111+
}
112+
113+
/// Extension trait to help build a [`ClientConfig`] with the platform verifier.
114+
pub trait ConfigVerifierExt {
115+
/// Build a [`ClientConfig`] with the platform verifier and the default `CryptoProvider`.
116+
///
117+
/// ```rust
118+
/// use rustls::ClientConfig;
119+
/// use rustls_platform_verifier::ConfigVerifierExt;
120+
/// let config = ClientConfig::with_platform_verifier();
121+
/// ```
122+
fn with_platform_verifier() -> ClientConfig;
123+
}
124+
125+
impl ConfigVerifierExt for ClientConfig {
126+
fn with_platform_verifier() -> ClientConfig {
127+
ClientConfig::builder()
128+
.with_platform_verifier()
129+
.with_no_client_auth()
130+
}
131+
}

0 commit comments

Comments
 (0)