1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use std::sync::Arc;
use rustls::StreamOwned;
use webpki::DNSNameRef;
use tls_api::async_as_sync::AsyncIoAsSyncIo;
use tls_api::spi_connector_common;
use tls_api::AsyncSocket;
use tls_api::AsyncSocketBox;
use tls_api::BoxFuture;
use tls_api::ImplInfo;
use crate::handshake::HandshakeFuture;
use crate::RustlsStream;
use std::future::Future;
pub struct TlsConnectorBuilder {
pub config: rustls::ClientConfig,
pub verify_hostname: bool,
}
pub struct TlsConnector {
pub config: Arc<rustls::ClientConfig>,
}
impl tls_api::TlsConnectorBuilder for TlsConnectorBuilder {
type Connector = TlsConnector;
type Underlying = rustls::ClientConfig;
fn underlying_mut(&mut self) -> &mut rustls::ClientConfig {
&mut self.config
}
fn set_alpn_protocols(&mut self, protocols: &[&[u8]]) -> anyhow::Result<()> {
self.config.alpn_protocols = protocols.into_iter().map(|p: &&[u8]| p.to_vec()).collect();
Ok(())
}
fn set_verify_hostname(&mut self, verify: bool) -> anyhow::Result<()> {
if !verify {
struct NoCertificateVerifier;
impl rustls::ServerCertVerifier for NoCertificateVerifier {
fn verify_server_cert(
&self,
_roots: &rustls::RootCertStore,
_presented_certs: &[rustls::Certificate],
_dns_name: webpki::DNSNameRef,
_ocsp_response: &[u8],
) -> Result<rustls::ServerCertVerified, rustls::TLSError> {
Ok(rustls::ServerCertVerified::assertion())
}
}
self.config
.dangerous()
.set_certificate_verifier(Arc::new(NoCertificateVerifier));
self.verify_hostname = false;
} else {
if !self.verify_hostname {
return Err(crate::Error::VerifyHostnameTrue.into());
}
}
Ok(())
}
fn add_root_certificate(&mut self, cert: &[u8]) -> anyhow::Result<()> {
let cert = rustls::Certificate(cert.to_vec());
self.config
.root_store
.add(&cert)
.map_err(anyhow::Error::new)?;
Ok(())
}
fn build(mut self) -> anyhow::Result<TlsConnector> {
if self.config.root_store.is_empty() {
self.config
.root_store
.add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);
}
Ok(TlsConnector {
config: Arc::new(self.config),
})
}
}
impl TlsConnector {
pub fn connect_impl<'a, S>(
&'a self,
domain: &'a str,
stream: S,
) -> impl Future<Output = anyhow::Result<crate::TlsStream<S>>> + 'a
where
S: AsyncSocket,
{
let dns_name =
match DNSNameRef::try_from_ascii_str(domain).map_err(|e| anyhow::Error::new(e)) {
Ok(dns_name) => dns_name,
Err(e) => return BoxFuture::new(async { Err(e) }),
};
let tls_stream: crate::TlsStream<S> =
crate::TlsStream::new(RustlsStream::Client(StreamOwned {
sess: rustls::ClientSession::new(&self.config, dns_name),
sock: AsyncIoAsSyncIo::new(stream),
}));
BoxFuture::new(HandshakeFuture::MidHandshake(tls_stream))
}
}
impl tls_api::TlsConnector for TlsConnector {
type Builder = TlsConnectorBuilder;
type Underlying = Arc<rustls::ClientConfig>;
type TlsStream = crate::TlsStream<AsyncSocketBox>;
fn underlying_mut(&mut self) -> &mut Self::Underlying {
&mut self.config
}
const IMPLEMENTED: bool = true;
const SUPPORTS_ALPN: bool = true;
fn info() -> ImplInfo {
crate::info()
}
fn builder() -> anyhow::Result<TlsConnectorBuilder> {
Ok(TlsConnectorBuilder {
config: rustls::ClientConfig::new(),
verify_hostname: true,
})
}
spi_connector_common!();
}