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_os = "macos"),
8 not(target_os = "ios"),
9 not(target_os = "tvos")
10))]
11mod others;
12
13#[cfg(all(
14 any(unix, target_arch = "wasm32"),
15 not(target_os = "android"),
16 not(target_os = "macos"),
17 not(target_os = "ios"),
18 not(target_os = "tvos")
19))]
20pub use others::Verifier;
21
22#[cfg(any(target_os = "macos", target_os = "ios", target_os = "tvos"))]
23mod apple;
24
25#[cfg(any(target_os = "macos", target_os = "ios", target_os = "tvos"))]
26pub use apple::Verifier;
27
28#[cfg(target_os = "android")]
29pub(crate) mod android;
30
31#[cfg(target_os = "android")]
32pub use android::Verifier;
33
34#[cfg(windows)]
35mod windows;
36
37#[cfg(windows)]
38pub use windows::Verifier;
39
40#[cfg_attr(windows, allow(dead_code))] #[derive(Debug, PartialEq)]
45pub(crate) struct EkuError;
46
47impl std::fmt::Display for EkuError {
48 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49 f.write_str("certificate had invalid extensions")
50 }
51}
52
53impl std::error::Error for EkuError {}
54
55fn log_server_cert(_end_entity: &rustls::pki_types::CertificateDer<'_>) {
58 #[cfg(feature = "cert-logging")]
59 {
60 use base64::Engine;
61 log::debug!(
62 "verifying certificate: {}",
63 base64::engine::general_purpose::STANDARD.encode(_end_entity.as_ref())
64 );
65 }
66}
67
68#[cfg(any(windows, target_os = "macos", target_os = "ios", target_os = "tvos"))]
71fn invalid_certificate(reason: impl Into<String>) -> rustls::Error {
72 rustls::Error::InvalidCertificate(rustls::CertificateError::Other(rustls::OtherError(
73 Arc::from(Box::from(reason.into())),
74 )))
75}
76
77#[cfg(target_os = "windows")]
85const ALLOWED_EKUS: &[*mut u8] = &["1.3.6.1.5.5.7.3.1\0".as_ptr() as *mut u8];
90#[cfg(target_os = "android")]
91pub const ALLOWED_EKUS: &[&str] = &["1.3.6.1.5.5.7.3.1"];
92
93impl Verifier {
94 pub fn with_provider(mut self, crypto_provider: Arc<CryptoProvider>) -> Self {
99 self.set_provider(crypto_provider);
100 self
101 }
102
103 pub fn set_provider(&mut self, crypto_provider: Arc<CryptoProvider>) {
108 self.crypto_provider = crypto_provider.into();
109 }
110
111 fn get_provider(&self) -> &Arc<CryptoProvider> {
112 self.crypto_provider.get_or_init(|| {
113 CryptoProvider::get_default()
114 .expect("rustls default CryptoProvider not set")
115 .clone()
116 })
117 }
118}