tokio-rustls/src/lib.rs

245 lines
6.6 KiB
Rust
Raw Normal View History

//! Asynchronous TLS/SSL streams for Tokio using [Rustls](https://github.com/ctz/rustls).
2017-02-21 03:52:43 +00:00
extern crate rustls;
extern crate webpki;
2017-02-21 03:52:43 +00:00
2018-03-21 13:44:36 +00:00
#[cfg(feature = "tokio")] mod tokio_impl;
#[cfg(feature = "futures")] mod futures_impl;
2018-03-21 05:08:47 +00:00
2017-02-21 03:52:43 +00:00
use std::io;
use std::sync::Arc;
2017-03-30 06:49:24 +00:00
use rustls::{
Session, ClientSession, ServerSession,
ClientConfig, ServerConfig
};
2017-02-21 03:52:43 +00:00
2017-02-22 05:03:21 +00:00
/// Extension trait for the `Arc<ClientConfig>` type in the `rustls` crate.
2018-03-21 13:44:36 +00:00
pub trait ClientConfigExt: sealed::Sealed {
fn connect_async<S>(&self, domain: webpki::DNSNameRef, stream: S)
2017-02-21 03:52:43 +00:00
-> ConnectAsync<S>
2018-03-20 12:17:44 +00:00
where S: io::Read + io::Write;
2017-02-21 03:52:43 +00:00
}
2017-02-22 05:03:21 +00:00
/// Extension trait for the `Arc<ServerConfig>` type in the `rustls` crate.
2018-03-21 13:44:36 +00:00
pub trait ServerConfigExt: sealed::Sealed {
2017-02-21 03:52:43 +00:00
fn accept_async<S>(&self, stream: S)
-> AcceptAsync<S>
2018-03-20 12:17:44 +00:00
where S: io::Read + io::Write;
2017-02-21 03:52:43 +00:00
}
2017-02-22 05:03:21 +00:00
/// Future returned from `ClientConfigExt::connect_async` which will resolve
/// once the connection handshake has finished.
2017-02-21 03:52:43 +00:00
pub struct ConnectAsync<S>(MidHandshake<S, ClientSession>);
2017-02-22 05:03:21 +00:00
/// Future returned from `ServerConfigExt::accept_async` which will resolve
/// once the accept handshake has finished.
2017-02-21 03:52:43 +00:00
pub struct AcceptAsync<S>(MidHandshake<S, ServerSession>);
2018-03-21 13:44:36 +00:00
impl sealed::Sealed for Arc<ClientConfig> {}
2017-02-21 03:52:43 +00:00
2017-02-22 05:03:21 +00:00
impl ClientConfigExt for Arc<ClientConfig> {
fn connect_async<S>(&self, domain: webpki::DNSNameRef, stream: S)
2017-02-21 03:52:43 +00:00
-> ConnectAsync<S>
2018-03-20 12:17:44 +00:00
where S: io::Read + io::Write
2017-02-21 03:52:43 +00:00
{
2017-05-26 10:22:03 +00:00
connect_async_with_session(stream, ClientSession::new(self, domain))
2017-02-21 03:52:43 +00:00
}
}
2017-05-26 10:22:03 +00:00
#[inline]
2017-05-26 08:38:02 +00:00
pub fn connect_async_with_session<S>(stream: S, session: ClientSession)
-> ConnectAsync<S>
2018-03-20 12:17:44 +00:00
where S: io::Read + io::Write
2017-05-26 08:38:02 +00:00
{
2018-03-21 13:44:36 +00:00
ConnectAsync(MidHandshake { inner: Some(TlsStream::new(stream, session)) })
2017-05-26 08:38:02 +00:00
}
2018-03-21 13:44:36 +00:00
impl sealed::Sealed for Arc<ServerConfig> {}
2017-02-22 05:03:21 +00:00
impl ServerConfigExt for Arc<ServerConfig> {
2017-02-21 03:52:43 +00:00
fn accept_async<S>(&self, stream: S)
-> AcceptAsync<S>
2018-03-20 12:17:44 +00:00
where S: io::Read + io::Write
2017-02-21 03:52:43 +00:00
{
2017-05-26 10:22:03 +00:00
accept_async_with_session(stream, ServerSession::new(self))
2017-02-21 03:52:43 +00:00
}
}
2017-05-26 10:22:03 +00:00
#[inline]
2017-05-26 08:38:02 +00:00
pub fn accept_async_with_session<S>(stream: S, session: ServerSession)
-> AcceptAsync<S>
2018-03-20 12:17:44 +00:00
where S: io::Read + io::Write
2017-05-26 08:38:02 +00:00
{
2018-03-21 13:44:36 +00:00
AcceptAsync(MidHandshake { inner: Some(TlsStream::new(stream, session)) })
2017-05-26 08:38:02 +00:00
}
2017-02-21 03:52:43 +00:00
struct MidHandshake<S, C> {
inner: Option<TlsStream<S, C>>
}
2017-02-22 05:03:21 +00:00
/// A wrapper around an underlying raw stream which implements the TLS or SSL
/// protocol.
#[derive(Debug)]
2017-02-21 03:52:43 +00:00
pub struct TlsStream<S, C> {
is_shutdown: bool,
2017-02-21 03:52:43 +00:00
eof: bool,
io: S,
session: C
}
2017-02-22 05:03:21 +00:00
impl<S, C> TlsStream<S, C> {
pub fn get_ref(&self) -> (&S, &C) {
(&self.io, &self.session)
}
pub fn get_mut(&mut self) -> (&mut S, &mut C) {
(&mut self.io, &mut self.session)
}
}
2018-03-21 13:44:36 +00:00
macro_rules! try_wouldblock {
( continue $r:expr ) => {
match $r {
Ok(true) => continue,
Ok(false) => false,
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => true,
Err(e) => return Err(e)
}
};
( ignore $r:expr ) => {
match $r {
Ok(_) => (),
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => (),
Err(e) => return Err(e)
}
};
}
2017-02-21 03:52:43 +00:00
impl<S, C> TlsStream<S, C>
2018-03-20 12:17:44 +00:00
where S: io::Read + io::Write, C: Session
2017-02-21 03:52:43 +00:00
{
#[inline]
2018-03-21 13:44:36 +00:00
fn new(io: S, session: C) -> TlsStream<S, C> {
2017-02-21 03:52:43 +00:00
TlsStream {
is_shutdown: false,
2017-02-21 03:52:43 +00:00
eof: false,
io: io,
session: session
}
}
2018-03-21 13:44:36 +00:00
fn do_read(&mut self) -> io::Result<bool> {
if !self.eof && self.session.wants_read() {
if self.session.read_tls(&mut self.io)? == 0 {
self.eof = true;
}
if let Err(err) = self.session.process_new_packets() {
// flush queued messages before returning an Err in
// order to send alerts instead of abruptly closing
// the socket
if self.session.wants_write() {
// ignore result to avoid masking original error
let _ = self.session.write_tls(&mut self.io);
}
return Err(io::Error::new(io::ErrorKind::InvalidData, err));
}
Ok(true)
} else {
Ok(false)
}
}
fn do_write(&mut self) -> io::Result<bool> {
if self.session.wants_write() {
self.session.write_tls(&mut self.io)?;
Ok(true)
} else {
Ok(false)
}
}
#[inline]
2017-02-21 03:52:43 +00:00
pub fn do_io(&mut self) -> io::Result<()> {
loop {
2018-03-21 13:44:36 +00:00
let write_would_block = try_wouldblock!(continue self.do_write());
let read_would_block = try_wouldblock!(continue self.do_read());
2017-02-21 03:52:43 +00:00
2018-03-21 13:44:36 +00:00
if write_would_block || read_would_block {
2017-02-21 03:52:43 +00:00
return Err(io::Error::from(io::ErrorKind::WouldBlock));
} else {
return Ok(());
}
}
}
}
impl<S, C> io::Read for TlsStream<S, C>
2018-03-20 12:17:44 +00:00
where S: io::Read + io::Write, C: Session
2017-02-21 03:52:43 +00:00
{
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
2018-03-21 13:44:36 +00:00
try_wouldblock!(ignore self.do_io());
loop {
match self.session.read(buf) {
2018-03-21 13:44:36 +00:00
Ok(0) if !self.eof => while self.do_read()? {},
Ok(n) => return Ok(n),
Err(e) => if e.kind() == io::ErrorKind::ConnectionAborted {
2018-03-21 13:44:36 +00:00
try_wouldblock!(ignore self.do_read());
return if self.eof { Ok(0) } else { Err(e) }
} else {
return Err(e)
}
}
2017-02-21 03:52:43 +00:00
}
}
}
impl<S, C> io::Write for TlsStream<S, C>
2018-03-20 12:17:44 +00:00
where S: io::Read + io::Write, C: Session
2017-02-21 03:52:43 +00:00
{
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
2018-03-21 13:44:36 +00:00
try_wouldblock!(ignore self.do_io());
2017-05-26 09:59:51 +00:00
2018-03-21 13:44:36 +00:00
let mut wlen = self.session.write(buf)?;
2018-03-21 13:44:36 +00:00
loop {
match self.do_write() {
Ok(true) => continue,
Ok(false) if wlen == 0 => (),
Ok(false) => break,
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock =>
if wlen == 0 {
2017-05-26 10:22:03 +00:00
// Both rustls buffer and IO buffer are blocking.
return Err(io::Error::from(io::ErrorKind::WouldBlock));
} else {
2018-03-21 13:44:36 +00:00
continue
2017-05-26 10:22:03 +00:00
},
2018-03-21 13:44:36 +00:00
Err(e) => return Err(e)
}
2018-03-21 13:44:36 +00:00
assert_eq!(wlen, 0);
wlen = self.session.write(buf)?;
}
2018-03-21 13:44:36 +00:00
Ok(wlen)
2017-02-21 03:52:43 +00:00
}
fn flush(&mut self) -> io::Result<()> {
self.session.flush()?;
2018-03-21 13:44:36 +00:00
while self.do_write()? {};
2017-04-14 04:43:03 +00:00
self.io.flush()
2017-02-21 03:52:43 +00:00
}
}
2018-03-21 13:44:36 +00:00
mod sealed {
pub trait Sealed {}
}