| 1 |
use crate::common::tls_state::TlsState; |
| 2 |
|
| 3 |
use crate::client; |
| 4 |
|
| 5 |
use futures_io::{AsyncRead, AsyncWrite}; |
| •6 |
use rustls::pki_types::ServerName; |
| 7 |
use rustls::{ClientConfig, ClientConnection, RootCertStore}; |
| 8 |
use std::convert::TryFrom; |
| 9 |
use std::future::Future; |
| 10 |
use std::io; |
| 11 |
use std::pin::Pin; |
| 12 |
use std::sync::Arc; |
| 13 |
use std::task::{Context, Poll}; |
| 14 |
|
| 15 |
/// The TLS connecting part. The acceptor drives |
| 16 |
/// the client side of the TLS handshake process. It works |
| 17 |
/// on any asynchronous stream. |
| 18 |
/// |
| 19 |
/// It provides a simple interface (`connect`), returning a future |
| 20 |
/// that will resolve when the handshake process completed. On |
| 21 |
/// success, it will hand you an async `TlsStream`. |
| 22 |
/// |
| 23 |
/// To create a `TlsConnector` with a non-default configuation, create |
| 24 |
/// a `rusttls::ClientConfig` and call `.into()` on it. |
| 25 |
/// |
| 26 |
/// ## Example |
| 27 |
/// |
| 28 |
/// ```rust |
| 29 |
/// use async_tls::TlsConnector; |
| 30 |
/// |
| 31 |
/// async_std::task::block_on(async { |
| 32 |
/// let connector = TlsConnector::default(); |
| 33 |
/// let tcp_stream = async_std::net::TcpStream::connect("example.com").await?; |
| 34 |
/// let encrypted_stream = connector.connect("example.com", tcp_stream).await?; |
| 35 |
/// |
| 36 |
/// Ok(()) as async_std::io::Result<()> |
| 37 |
/// }); |
| 38 |
/// ``` |
| 39 |
#[derive(Clone)] |
| 40 |
pub struct TlsConnector { |
| 41 |
inner: Arc<ClientConfig>, |
| 42 |
#[cfg(feature = "early-data")] |
| 43 |
early_data: bool, |
| 44 |
} |
| 45 |
|
| 46 |
impl From<Arc<ClientConfig>> for TlsConnector { |
| 47 |
fn from(inner: Arc<ClientConfig>) -> TlsConnector { |
| 48 |
TlsConnector { |
| 49 |
inner, |
| 50 |
#[cfg(feature = "early-data")] |
| 51 |
early_data: false, |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
impl From<ClientConfig> for TlsConnector { |
| 57 |
fn from(inner: ClientConfig) -> TlsConnector { |
| 58 |
TlsConnector { |
| 59 |
inner: Arc::new(inner), |
| 60 |
#[cfg(feature = "early-data")] |
| 61 |
early_data: false, |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
impl Default for TlsConnector { |
| 67 |
fn default() -> Self { |
| 68 |
let mut root_certs = RootCertStore::empty(); |
| •69 |
let result = rustls_native_certs::load_native_certs().expect("Failed to load system root certificates"); |
| 70 |
root_certs.add_parsable_certificates(result); |
| 71 |
let config = ClientConfig::builder() |
| 72 |
.with_root_certificates(root_certs) |
| 73 |
.with_no_client_auth(); |
| 74 |
Arc::new(config).into() |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
impl TlsConnector { |
| 79 |
/// Create a new TlsConnector with default configuration. |
| 80 |
/// |
| 81 |
/// This is the same as calling `TlsConnector::default()`. |
| 82 |
pub fn new() -> Self { |
| 83 |
Default::default() |
| 84 |
} |
| 85 |
|
| 86 |
/// Enable 0-RTT. |
| 87 |
/// |
| 88 |
/// You must also set `enable_early_data` to `true` in `ClientConfig`. |
| 89 |
#[cfg(feature = "early-data")] |
| 90 |
pub fn early_data(mut self, flag: bool) -> TlsConnector { |
| 91 |
self.early_data = flag; |
| 92 |
self |
| 93 |
} |
| 94 |
|
| 95 |
/// Connect to a server. `stream` can be any type implementing `AsyncRead` and `AsyncWrite`, |
| 96 |
/// such as TcpStreams or Unix domain sockets. |
| 97 |
/// |
| 98 |
/// The function will return a `Connect` Future, representing the connecting part of a Tls |
| 99 |
/// handshake. It will resolve when the handshake is over. |
| 100 |
#[inline] |
| 101 |
pub fn connect<'a, IO>(&self, domain: impl AsRef<str>, stream: IO) -> Connect<IO> |
| 102 |
where |
| 103 |
IO: AsyncRead + AsyncWrite + Unpin, |
| 104 |
{ |
| 105 |
self.connect_with(domain, stream, |_| ()) |
| 106 |
} |
| 107 |
|
| 108 |
// NOTE: Currently private, exposing ClientConnection exposes rusttls |
| 109 |
// Early data should be exposed differently |
| 110 |
fn connect_with<'a, IO, F>(&self, domain: impl AsRef<str>, stream: IO, f: F) -> Connect<IO> |
| 111 |
where |
| 112 |
IO: AsyncRead + AsyncWrite + Unpin, |
| 113 |
F: FnOnce(&mut ClientConnection), |
| 114 |
{ |
| 115 |
let domain = match ServerName::try_from(domain.as_ref()) { |
| •116 |
Ok(domain) => domain.to_owned(), |
| 117 |
Err(_) => { |
| 118 |
return Connect(ConnectInner::Error(Some(io::Error::new( |
| 119 |
io::ErrorKind::InvalidInput, |
| 120 |
"invalid domain", |
| 121 |
)))) |
| 122 |
} |
| 123 |
}; |
| 124 |
|
| 125 |
let mut session = match ClientConnection::new(self.inner.clone(), domain) { |
| 126 |
Ok(session) => session, |
| 127 |
Err(_) => { |
| 128 |
return Connect(ConnectInner::Error(Some(io::Error::new( |
| 129 |
io::ErrorKind::Other, |
| 130 |
"invalid connection", |
| 131 |
)))) |
| 132 |
} |
| 133 |
}; |
| 134 |
|
| 135 |
f(&mut session); |
| 136 |
|
| 137 |
#[cfg(not(feature = "early-data"))] |
| 138 |
{ |
| 139 |
Connect(ConnectInner::Handshake(client::MidHandshake::Handshaking( |
| 140 |
client::TlsStream { |
| 141 |
session, |
| 142 |
io: stream, |
| 143 |
state: TlsState::Stream, |
| 144 |
}, |
| 145 |
))) |
| 146 |
} |
| 147 |
|
| 148 |
#[cfg(feature = "early-data")] |
| 149 |
{ |
| 150 |
Connect(ConnectInner::Handshake(if self.early_data { |
| 151 |
client::MidHandshake::EarlyData(client::TlsStream { |
| 152 |
session, |
| 153 |
io: stream, |
| 154 |
state: TlsState::EarlyData, |
| 155 |
early_data: (0, Vec::new()), |
| 156 |
}) |
| 157 |
} else { |
| 158 |
client::MidHandshake::Handshaking(client::TlsStream { |
| 159 |
session, |
| 160 |
io: stream, |
| 161 |
state: TlsState::Stream, |
| 162 |
early_data: (0, Vec::new()), |
| 163 |
}) |
| 164 |
})) |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
/// Future returned from `TlsConnector::connect` which will resolve |
| 170 |
/// once the connection handshake has finished. |
| 171 |
pub struct Connect<IO>(ConnectInner<IO>); |
| 172 |
|
| 173 |
enum ConnectInner<IO> { |
| 174 |
Error(Option<io::Error>), |
| 175 |
Handshake(client::MidHandshake<IO>), |
| 176 |
} |
| 177 |
|
| 178 |
impl<IO: AsyncRead + AsyncWrite + Unpin> Future for Connect<IO> { |
| 179 |
type Output = io::Result<client::TlsStream<IO>>; |
| 180 |
|
| 181 |
#[inline] |
| 182 |
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
| 183 |
match self.0 { |
| 184 |
ConnectInner::Error(ref mut err) => { |
| 185 |
Poll::Ready(Err(err.take().expect("Polled twice after being Ready"))) |
| 186 |
} |
| 187 |
ConnectInner::Handshake(ref mut handshake) => Pin::new(handshake).poll(cx), |
| 188 |
} |
| 189 |
} |
| 190 |
} |