rustls_platform_verifier/verification/
mod.rs1use rustls::crypto::CryptoProvider;
2use std::sync::Arc;
3
4#[cfg(all(
5 any(unix, target_arch = "wasm32"),
6 not(target_os = "android"),
7 not(target_vendor = "apple"),
8))]
9mod others;
10
11#[cfg(all(
12 any(unix, target_arch = "wasm32"),
13 not(target_os = "android"),
14 not(target_vendor = "apple"),
15))]
16pub use others::Verifier;
17
18#[cfg(target_vendor = "apple")]
19mod apple;
20
21#[cfg(target_vendor = "apple")]
22pub use apple::Verifier;
23
24#[cfg(target_os = "android")]
25pub(crate) mod android;
26
27#[cfg(target_os = "android")]
28pub use android::Verifier;
29
30#[cfg(windows)]
31mod windows;
32
33#[cfg(windows)]
34pub use windows::Verifier;
35
36#[cfg_attr(windows, allow(dead_code))] #[derive(Debug, PartialEq)]
41pub(crate) struct EkuError;
42
43impl std::fmt::Display for EkuError {
44 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45 f.write_str("certificate had invalid extensions")
46 }
47}
48
49impl std::error::Error for EkuError {}
50
51fn log_server_cert(_end_entity: &rustls::pki_types::CertificateDer<'_>) {
54 #[cfg(feature = "cert-logging")]
55 {
56 use base64::Engine;
57 log::debug!(
58 "verifying certificate: {}",
59 base64::engine::general_purpose::STANDARD.encode(_end_entity.as_ref())
60 );
61 }
62}
63
64#[cfg(any(windows, target_vendor = "apple"))]
67fn invalid_certificate(reason: impl Into<String>) -> rustls::Error {
68 rustls::Error::InvalidCertificate(rustls::CertificateError::Other(rustls::OtherError(
69 Arc::from(Box::from(reason.into())),
70 )))
71}
72
73#[cfg(target_os = "windows")]
81const ALLOWED_EKUS: &[windows_sys::core::PCSTR] =
84 &[windows_sys::Win32::Security::Cryptography::szOID_PKIX_KP_SERVER_AUTH];
85#[cfg(target_os = "android")]
86pub const ALLOWED_EKUS: &[&str] = &["1.3.6.1.5.5.7.3.1"];
87
88impl Verifier {
89 pub fn with_provider(mut self, crypto_provider: Arc<CryptoProvider>) -> Self {
94 self.set_provider(crypto_provider);
95 self
96 }
97
98 pub fn set_provider(&mut self, crypto_provider: Arc<CryptoProvider>) {
103 self.crypto_provider = crypto_provider.into();
104 }
105
106 fn get_provider(&self) -> &Arc<CryptoProvider> {
107 self.crypto_provider.get_or_init(|| {
108 CryptoProvider::get_default()
109 .expect("rustls default CryptoProvider not set")
110 .clone()
111 })
112 }
113}