|
1 | 1 | use crate::error::Result;
|
| 2 | +use rustls::{ |
| 3 | + pki_types::{CertificateDer, PrivateKeyDer, ServerName}, |
| 4 | + RootCertStore, |
| 5 | +}; |
2 | 6 | use std::{
|
3 | 7 | fs::File,
|
4 | 8 | io::BufReader,
|
5 | 9 | net::SocketAddr,
|
6 | 10 | path::{Path, PathBuf},
|
7 |
| - sync::Arc, |
8 | 11 | };
|
9 | 12 | use tokio::net::TcpStream;
|
10 |
| -use tokio_rustls::{ |
11 |
| - client::TlsStream, |
12 |
| - rustls::{self, Certificate, OwnedTrustAnchor, PrivateKey, RootCertStore}, |
13 |
| - TlsConnector, |
14 |
| -}; |
| 13 | +use tokio_rustls::{client::TlsStream, TlsConnector}; |
15 | 14 |
|
16 | 15 | pub(crate) fn retrieve_root_cert_store_for_client(cafile: &Option<PathBuf>) -> Result<RootCertStore> {
|
17 | 16 | let mut root_cert_store = rustls::RootCertStore::empty();
|
18 | 17 | let mut done = false;
|
19 | 18 | if let Some(cafile) = cafile {
|
20 | 19 | if cafile.exists() {
|
21 | 20 | let mut pem = BufReader::new(File::open(cafile)?);
|
22 |
| - let certs = rustls_pemfile::certs(&mut pem)?; |
23 |
| - let trust_anchors = certs.iter().map(|cert| { |
24 |
| - if let Ok(ta) = webpki::TrustAnchor::try_from_cert_der(&cert[..]) { |
25 |
| - OwnedTrustAnchor::from_subject_spki_name_constraints(ta.subject, ta.spki, ta.name_constraints) |
26 |
| - } else { |
27 |
| - OwnedTrustAnchor::from_subject_spki_name_constraints(vec![], vec![], Some(vec![])) |
28 |
| - } |
29 |
| - }); |
30 |
| - root_cert_store.add_trust_anchors(trust_anchors); |
| 21 | + for cert in rustls_pemfile::certs(&mut pem) { |
| 22 | + root_cert_store.add(cert?)?; |
| 23 | + } |
31 | 24 | done = true;
|
32 | 25 | }
|
33 | 26 | }
|
34 | 27 | if !done {
|
35 |
| - root_cert_store.add_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.iter().map(|ta| { |
36 |
| - let name_constraints = ta.name_constraints.clone().map(|nc| nc.as_ref().to_vec()); |
37 |
| - OwnedTrustAnchor::from_subject_spki_name_constraints(ta.subject.as_ref(), ta.subject_public_key_info.as_ref(), name_constraints) |
38 |
| - })); |
| 28 | + root_cert_store.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned()); |
39 | 29 | }
|
40 | 30 | Ok(root_cert_store)
|
41 | 31 | }
|
42 | 32 |
|
43 |
| -#[derive(Debug)] |
44 |
| -pub struct NoCertificateVerification {} |
45 |
| - |
46 |
| -impl rustls::client::ServerCertVerifier for NoCertificateVerification { |
47 |
| - fn verify_server_cert( |
48 |
| - &self, |
49 |
| - _end_entity: &rustls::Certificate, |
50 |
| - _intermediates: &[rustls::Certificate], |
51 |
| - _server_name: &rustls::ServerName, |
52 |
| - _scts: &mut dyn Iterator<Item = &[u8]>, |
53 |
| - _ocsp: &[u8], |
54 |
| - _now: std::time::SystemTime, |
55 |
| - ) -> Result<rustls::client::ServerCertVerified, rustls::Error> { |
56 |
| - Ok(rustls::client::ServerCertVerified::assertion()) |
57 |
| - } |
58 |
| -} |
59 |
| - |
60 | 33 | pub(crate) async fn create_tls_client_stream(
|
61 | 34 | root_cert_store: RootCertStore,
|
62 | 35 | addr: SocketAddr,
|
63 | 36 | domain: &str,
|
64 | 37 | ) -> Result<TlsStream<TcpStream>> {
|
65 |
| - let mut config = rustls::ClientConfig::builder() |
66 |
| - .with_safe_defaults() |
| 38 | + let config = rustls::ClientConfig::builder() |
67 | 39 | .with_root_certificates(root_cert_store)
|
68 | 40 | .with_no_client_auth();
|
69 |
| - config.dangerous().set_certificate_verifier(Arc::new(NoCertificateVerification {})); |
70 | 41 | let connector = TlsConnector::from(std::sync::Arc::new(config));
|
71 | 42 |
|
72 | 43 | let stream = crate::tcp_stream::create(addr).await?;
|
73 | 44 |
|
74 |
| - let domain = rustls::ServerName::try_from(domain)?; |
| 45 | + let domain = ServerName::try_from(domain)?.to_owned(); |
75 | 46 |
|
76 | 47 | let stream = connector.connect(domain, stream).await?;
|
77 | 48 |
|
78 | 49 | Ok(stream)
|
79 | 50 | }
|
80 | 51 |
|
81 |
| -pub(crate) fn server_load_certs(path: &Path) -> Result<Vec<Certificate>> { |
82 |
| - let certs = rustls_pemfile::certs(&mut BufReader::new(File::open(path)?))?; |
83 |
| - Ok(certs.into_iter().map(Certificate).collect()) |
| 52 | +pub(crate) fn server_load_certs(path: &Path) -> Result<Vec<CertificateDer<'static>>> { |
| 53 | + let mut res = vec![]; |
| 54 | + for cert in rustls_pemfile::certs(&mut BufReader::new(File::open(path)?)) { |
| 55 | + res.push(cert?); |
| 56 | + } |
| 57 | + Ok(res) |
84 | 58 | }
|
85 | 59 |
|
86 |
| -pub(crate) fn server_load_keys(path: &Path) -> Result<Vec<PrivateKey>> { |
87 |
| - let keys = rustls_pemfile::rsa_private_keys(&mut BufReader::new(File::open(path)?))?; |
88 |
| - Ok(keys.into_iter().map(PrivateKey).collect()) |
| 60 | +pub(crate) fn server_load_keys(path: &Path) -> Result<Vec<PrivateKeyDer<'static>>> { |
| 61 | + let mut res = vec![]; |
| 62 | + for key in rustls_pemfile::rsa_private_keys(&mut BufReader::new(File::open(path)?)) { |
| 63 | + res.push(PrivateKeyDer::from(key?)); |
| 64 | + } |
| 65 | + Ok(res) |
89 | 66 | }
|
0 commit comments