|
1 | | -#[cfg(any(feature = "rustls-native-certs", feature = "webpki-roots"))] |
| 1 | +#[cfg(any(feature = "rustls-platform-verifier", feature = "webpki-roots"))] |
2 | 2 | use std::sync::Arc; |
3 | 3 |
|
4 | 4 | use compio_io::{AsyncRead, AsyncWrite}; |
5 | 5 | use compio_net::TcpStream; |
6 | 6 | use compio_tls::TlsConnector; |
7 | | -#[cfg(any(feature = "rustls-native-certs", feature = "webpki-roots"))] |
| 7 | +#[cfg(any(feature = "rustls-platform-verifier", feature = "webpki-roots"))] |
8 | 8 | use rustls::{ClientConfig, RootCertStore}; |
9 | 9 | use tungstenite::{ |
10 | 10 | Error, |
@@ -37,92 +37,93 @@ where |
37 | 37 | let connector = if let Some(connector) = connector { |
38 | 38 | connector |
39 | 39 | } else { |
40 | | - // Only create root_store when we actually have certificate features enabled |
41 | | - #[cfg(any(feature = "rustls-native-certs", feature = "webpki-roots"))] |
42 | | - let root_store = { |
43 | | - let mut store = RootCertStore::empty(); |
44 | | - |
45 | | - #[cfg(feature = "rustls-native-certs")] |
46 | | - { |
47 | | - let cert_result = rustls_native_certs::load_native_certs(); |
48 | | - |
49 | | - // Log any errors that occurred |
50 | | - for err in &cert_result.errors { |
51 | | - log::warn!("Error loading native certificate: {err}"); |
52 | | - } |
| 40 | + // Create TLS connector with platform verifier when feature is enabled |
| 41 | + #[cfg(feature = "rustls-platform-verifier")] |
| 42 | + { |
| 43 | + use rustls_platform_verifier::BuilderVerifierExt; |
53 | 44 |
|
54 | | - if !cert_result.certs.is_empty() { |
55 | | - let (added, ignored) = |
56 | | - store.add_parsable_certificates(cert_result.certs); |
| 45 | + // Use platform's native certificate verification |
| 46 | + // This provides better security and enterprise integration |
| 47 | + let config_result = ClientConfig::builder().with_platform_verifier(); |
| 48 | + |
| 49 | + match config_result { |
| 50 | + Ok(config_builder) => { |
57 | 51 | log::debug!( |
58 | | - "Added {added} native root certificates (ignored {ignored})" |
| 52 | + "Using rustls-platform-verifier for certificate validation" |
59 | 53 | ); |
| 54 | + TlsConnector::from(Arc::new(config_builder.with_no_client_auth())) |
| 55 | + } |
| 56 | + Err(e) => { |
| 57 | + log::warn!("Error creating platform verifier: {e}"); |
60 | 58 |
|
61 | 59 | // Only fail if webpki-roots is NOT enabled as fallback |
62 | 60 | #[cfg(not(feature = "webpki-roots"))] |
63 | | - if added == 0 { |
| 61 | + { |
64 | 62 | return Err(Error::Io(std::io::Error::new( |
65 | | - std::io::ErrorKind::NotFound, |
66 | | - "No valid native root certificates found", |
| 63 | + std::io::ErrorKind::Other, |
| 64 | + format!("Failed to create platform verifier: {}", e), |
67 | 65 | ))); |
68 | 66 | } |
69 | | - } else { |
70 | | - log::warn!("No native root certificates found"); |
71 | 67 |
|
72 | | - // Only fail if webpki-roots is NOT enabled as fallback |
73 | | - #[cfg(not(feature = "webpki-roots"))] |
74 | | - return Err(Error::Io(std::io::Error::new( |
75 | | - std::io::ErrorKind::NotFound, |
76 | | - "No native root certificates found", |
77 | | - ))); |
| 68 | + // Fall through to webpki-roots if available |
| 69 | + #[cfg(feature = "webpki-roots")] |
| 70 | + { |
| 71 | + use log::debug; |
| 72 | + |
| 73 | + let mut root_store = RootCertStore::empty(); |
| 74 | + let webpki_certs = webpki_roots::TLS_SERVER_ROOTS.to_vec(); |
| 75 | + root_store.extend(webpki_certs); |
| 76 | + debug!( |
| 77 | + "Falling back to {} webpki root certificates", |
| 78 | + webpki_roots::TLS_SERVER_ROOTS.len() |
| 79 | + ); |
| 80 | + |
| 81 | + TlsConnector::from(Arc::new( |
| 82 | + ClientConfig::builder() |
| 83 | + .with_root_certificates(root_store) |
| 84 | + .with_no_client_auth(), |
| 85 | + )) |
| 86 | + } |
78 | 87 | } |
79 | 88 | } |
| 89 | + } |
80 | 90 |
|
81 | | - // Load webpki-roots whenever the feature is enabled |
82 | | - // This serves as a fallback when native-certs is also enabled |
83 | | - #[cfg(feature = "webpki-roots")] |
84 | | - { |
85 | | - use log::debug; |
86 | | - |
87 | | - let webpki_certs = webpki_roots::TLS_SERVER_ROOTS.to_vec(); |
88 | | - store.extend(webpki_certs); |
89 | | - debug!( |
90 | | - "Added {} webpki root certificates", |
91 | | - webpki_roots::TLS_SERVER_ROOTS.len() |
92 | | - ); |
93 | | - } |
94 | | - |
95 | | - store |
96 | | - }; |
97 | | - |
98 | | - // Check if we have neither feature enabled |
99 | | - #[cfg(not(any(feature = "rustls-native-certs", feature = "webpki-roots")))] |
| 91 | + // Use webpki-roots when platform-verifier is not available |
| 92 | + // This serves as a fallback or standalone certificate source |
| 93 | + #[cfg(all( |
| 94 | + feature = "webpki-roots", |
| 95 | + not(feature = "rustls-platform-verifier") |
| 96 | + ))] |
100 | 97 | { |
101 | | - return Err(Error::Io(std::io::Error::new( |
102 | | - std::io::ErrorKind::NotFound, |
103 | | - "No root certificate features enabled. Enable either \ |
104 | | - 'rustls-native-certs' or 'webpki-roots'", |
105 | | - ))); |
106 | | - } |
| 98 | + use log::debug; |
107 | 99 |
|
108 | | - // Check if root_store is empty (only when features are enabled) |
109 | | - #[cfg(any(feature = "rustls-native-certs", feature = "webpki-roots"))] |
110 | | - if root_store.is_empty() { |
111 | | - return Err(Error::Io(std::io::Error::new( |
112 | | - std::io::ErrorKind::NotFound, |
113 | | - "No root certificates available", |
114 | | - ))); |
115 | | - } |
| 100 | + let mut root_store = RootCertStore::empty(); |
| 101 | + let webpki_certs = webpki_roots::TLS_SERVER_ROOTS.to_vec(); |
| 102 | + root_store.extend(webpki_certs); |
| 103 | + debug!( |
| 104 | + "Using {} webpki root certificates", |
| 105 | + webpki_roots::TLS_SERVER_ROOTS.len() |
| 106 | + ); |
116 | 107 |
|
117 | | - // Create the TLS connector (only when features are enabled) |
118 | | - #[cfg(any(feature = "rustls-native-certs", feature = "webpki-roots"))] |
119 | | - { |
120 | 108 | TlsConnector::from(Arc::new( |
121 | 109 | ClientConfig::builder() |
122 | 110 | .with_root_certificates(root_store) |
123 | 111 | .with_no_client_auth(), |
124 | 112 | )) |
125 | 113 | } |
| 114 | + |
| 115 | + // Check if we have neither feature enabled |
| 116 | + #[cfg(not(any( |
| 117 | + feature = "rustls-platform-verifier", |
| 118 | + feature = "webpki-roots" |
| 119 | + )))] |
| 120 | + { |
| 121 | + return Err(Error::Io(std::io::Error::new( |
| 122 | + std::io::ErrorKind::NotFound, |
| 123 | + "No root certificate features enabled. Enable either \ |
| 124 | + 'rustls-platform-verifier' or 'webpki-roots'", |
| 125 | + ))); |
| 126 | + } |
126 | 127 | }; |
127 | 128 |
|
128 | 129 | connector |
|
0 commit comments