Skip to content

Commit 0bd0573

Browse files
committed
provide with_platform_verifier_and_extra_roots extension function
1 parent 1099f16 commit 0bd0573

File tree

1 file changed

+57
-1
lines changed
  • rustls-platform-verifier/src

1 file changed

+57
-1
lines changed

rustls-platform-verifier/src/lib.rs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,26 @@ pub trait BuilderVerifierExt {
5858
fn with_platform_verifier(
5959
self,
6060
) -> Result<ConfigBuilder<ClientConfig, WantsClientCert>, rustls::Error>;
61+
62+
/// Configures the `ClientConfig` with the platform verifier
63+
/// and the extra root certificates to trust.
64+
///
65+
/// ```rust
66+
/// use rustls::ClientConfig;
67+
/// use rustls_platform_verifier::BuilderVerifierExt;
68+
///
69+
/// let roots = vec![/* ... */];
70+
///
71+
/// let config = ClientConfig::builder()
72+
/// .with_platform_verifier_and_extra_roots(roots)
73+
/// .unwrap()
74+
/// .with_no_client_auth();
75+
/// ```
76+
#[cfg(not(target_os = "android"))]
77+
fn with_platform_verifier_and_extra_roots(
78+
self,
79+
roots: impl IntoIterator<Item = rustls::pki_types::CertificateDer<'static>>,
80+
) -> Result<ConfigBuilder<ClientConfig, WantsClientCert>, rustls::Error>;
6181
}
6282

6383
impl BuilderVerifierExt for ConfigBuilder<ClientConfig, WantsVerifier> {
@@ -69,6 +89,17 @@ impl BuilderVerifierExt for ConfigBuilder<ClientConfig, WantsVerifier> {
6989
.dangerous()
7090
.with_custom_certificate_verifier(Arc::new(verifier)))
7191
}
92+
93+
#[cfg(not(target_os = "android"))]
94+
fn with_platform_verifier_and_extra_roots(
95+
self,
96+
roots: impl IntoIterator<Item = rustls::pki_types::CertificateDer<'static>>,
97+
) -> Result<ConfigBuilder<ClientConfig, WantsClientCert>, rustls::Error> {
98+
let verifier = Verifier::new_with_extra_roots(roots, self.crypto_provider().clone())?;
99+
Ok(self
100+
.dangerous()
101+
.with_custom_certificate_verifier(Arc::new(verifier)))
102+
}
72103
}
73104

74105
/// Extension trait to help build a [`ClientConfig`] with the platform verifier.
@@ -78,9 +109,25 @@ pub trait ConfigVerifierExt {
78109
/// ```rust
79110
/// use rustls::ClientConfig;
80111
/// use rustls_platform_verifier::ConfigVerifierExt;
81-
/// let config = ClientConfig::with_platform_verifier();
112+
/// let config = ClientConfig::with_platform_verifier().unwrap();
82113
/// ```
83114
fn with_platform_verifier() -> Result<ClientConfig, rustls::Error>;
115+
116+
/// Build a [`ClientConfig`] with the platform verifier, the default `CryptoProvider`,
117+
/// and the extra root certificates to trust.
118+
///
119+
/// ```rust
120+
/// use rustls::ClientConfig;
121+
/// use rustls_platform_verifier::ConfigVerifierExt;
122+
///
123+
/// let roots = vec![/* ... */];
124+
///
125+
/// let config = ClientConfig::with_platform_verifier_and_extra_roots(roots).unwrap();
126+
/// ```
127+
#[cfg(not(target_os = "android"))]
128+
fn with_platform_verifier_and_extra_roots(
129+
roots: impl IntoIterator<Item = pki_types::CertificateDer<'static>>,
130+
) -> Result<ClientConfig, rustls::Error>;
84131
}
85132

86133
impl ConfigVerifierExt for ClientConfig {
@@ -89,4 +136,13 @@ impl ConfigVerifierExt for ClientConfig {
89136
.with_platform_verifier()?
90137
.with_no_client_auth())
91138
}
139+
140+
#[cfg(not(target_os = "android"))]
141+
fn with_platform_verifier_and_extra_roots(
142+
roots: impl IntoIterator<Item = pki_types::CertificateDer<'static>>,
143+
) -> Result<ClientConfig, rustls::Error> {
144+
Ok(ClientConfig::builder()
145+
.with_platform_verifier_and_extra_roots(roots)?
146+
.with_no_client_auth())
147+
}
92148
}

0 commit comments

Comments
 (0)