2017-03-30 03:58:30 +00:00
|
|
|
//! Asynchronous TLS/SSL streams for Tokio using [Rustls](https://github.com/ctz/rustls).
|
|
|
|
|
2017-02-22 05:03:21 +00:00
|
|
|
|
2017-03-16 02:15:06 +00:00
|
|
|
#[cfg_attr(feature = "tokio-proto", macro_use)] extern crate futures;
|
2017-03-30 07:39:11 +00:00
|
|
|
#[macro_use] extern crate tokio_io;
|
2017-02-21 03:52:43 +00:00
|
|
|
extern crate rustls;
|
|
|
|
|
2017-02-22 05:30:01 +00:00
|
|
|
pub mod proto;
|
|
|
|
|
2017-02-21 03:52:43 +00:00
|
|
|
use std::io;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use futures::{ Future, Poll, Async };
|
2017-03-16 02:15:06 +00:00
|
|
|
use tokio_io::{ AsyncRead, AsyncWrite };
|
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.
|
|
|
|
pub trait ClientConfigExt {
|
2017-02-21 03:52:43 +00:00
|
|
|
fn connect_async<S>(&self, domain: &str, stream: S)
|
|
|
|
-> ConnectAsync<S>
|
2017-03-16 02:15:06 +00:00
|
|
|
where S: AsyncRead + AsyncWrite;
|
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.
|
|
|
|
pub trait ServerConfigExt {
|
2017-02-21 03:52:43 +00:00
|
|
|
fn accept_async<S>(&self, stream: S)
|
|
|
|
-> AcceptAsync<S>
|
2017-03-16 02:15:06 +00:00
|
|
|
where S: AsyncRead + AsyncWrite;
|
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>);
|
|
|
|
|
|
|
|
|
2017-02-22 05:03:21 +00:00
|
|
|
impl ClientConfigExt for Arc<ClientConfig> {
|
2017-02-21 03:52:43 +00:00
|
|
|
fn connect_async<S>(&self, domain: &str, stream: S)
|
|
|
|
-> ConnectAsync<S>
|
2017-03-16 02:15:06 +00:00
|
|
|
where S: AsyncRead + AsyncWrite
|
2017-02-21 03:52:43 +00:00
|
|
|
{
|
|
|
|
ConnectAsync(MidHandshake {
|
|
|
|
inner: Some(TlsStream::new(stream, ClientSession::new(self, domain)))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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>
|
2017-03-16 02:15:06 +00:00
|
|
|
where S: AsyncRead + AsyncWrite
|
2017-02-21 03:52:43 +00:00
|
|
|
{
|
|
|
|
AcceptAsync(MidHandshake {
|
|
|
|
inner: Some(TlsStream::new(stream, ServerSession::new(self)))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-16 02:15:06 +00:00
|
|
|
impl<S: AsyncRead + AsyncWrite> Future for ConnectAsync<S> {
|
2017-02-21 03:52:43 +00:00
|
|
|
type Item = TlsStream<S, ClientSession>;
|
|
|
|
type Error = io::Error;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
self.0.poll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-16 02:15:06 +00:00
|
|
|
impl<S: AsyncRead + AsyncWrite> Future for AcceptAsync<S> {
|
2017-02-21 03:52:43 +00:00
|
|
|
type Item = TlsStream<S, ServerSession>;
|
|
|
|
type Error = io::Error;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
self.0.poll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct MidHandshake<S, C> {
|
|
|
|
inner: Option<TlsStream<S, C>>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S, C> Future for MidHandshake<S, C>
|
2017-03-16 02:15:06 +00:00
|
|
|
where S: AsyncRead + AsyncWrite, C: Session
|
2017-02-21 03:52:43 +00:00
|
|
|
{
|
|
|
|
type Item = TlsStream<S, C>;
|
|
|
|
type Error = io::Error;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
loop {
|
2017-03-01 02:34:24 +00:00
|
|
|
let stream = self.inner.as_mut().unwrap();
|
2017-02-21 03:52:43 +00:00
|
|
|
if !stream.session.is_handshaking() { break };
|
|
|
|
|
|
|
|
match stream.do_io() {
|
2017-02-27 12:59:35 +00:00
|
|
|
Ok(()) => match (stream.eof, stream.session.is_handshaking()) {
|
|
|
|
(true, true) => return Err(io::Error::from(io::ErrorKind::UnexpectedEof)),
|
|
|
|
(false, true) => continue,
|
|
|
|
(..) => break
|
2017-02-24 11:28:19 +00:00
|
|
|
},
|
|
|
|
Err(e) => match (e.kind(), stream.session.is_handshaking()) {
|
|
|
|
(io::ErrorKind::WouldBlock, true) => return Ok(Async::NotReady),
|
|
|
|
(io::ErrorKind::WouldBlock, false) => break,
|
|
|
|
(..) => return Err(e)
|
|
|
|
}
|
2017-02-21 03:52:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-01 02:34:24 +00:00
|
|
|
Ok(Async::Ready(self.inner.take().unwrap()))
|
2017-02-21 03:52:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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> {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-21 03:52:43 +00:00
|
|
|
impl<S, C> TlsStream<S, C>
|
2017-03-16 02:15:06 +00:00
|
|
|
where S: AsyncRead + AsyncWrite, C: Session
|
2017-02-21 03:52:43 +00:00
|
|
|
{
|
|
|
|
#[inline]
|
|
|
|
pub fn new(io: S, session: C) -> TlsStream<S, C> {
|
|
|
|
TlsStream {
|
|
|
|
eof: false,
|
|
|
|
io: io,
|
|
|
|
session: session
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn do_io(&mut self) -> io::Result<()> {
|
|
|
|
loop {
|
2017-03-16 02:15:06 +00:00
|
|
|
let read_would_block = if !self.eof && self.session.wants_read() {
|
|
|
|
match self.session.read_tls(&mut self.io) {
|
|
|
|
Ok(0) => {
|
|
|
|
self.eof = true;
|
|
|
|
continue
|
|
|
|
},
|
|
|
|
Ok(_) => {
|
|
|
|
self.session.process_new_packets()
|
|
|
|
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
|
|
|
|
continue
|
|
|
|
},
|
|
|
|
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => true,
|
|
|
|
Err(e) => return Err(e)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
false
|
2017-02-21 03:52:43 +00:00
|
|
|
};
|
|
|
|
|
2017-03-16 02:15:06 +00:00
|
|
|
let write_would_block = if self.session.wants_write() {
|
|
|
|
match self.session.write_tls(&mut self.io) {
|
2017-02-21 03:52:43 +00:00
|
|
|
Ok(_) => continue,
|
2017-03-16 02:15:06 +00:00
|
|
|
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => true,
|
2017-02-21 03:52:43 +00:00
|
|
|
Err(e) => return Err(e)
|
2017-03-16 02:15:06 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
false
|
2017-02-21 03:52:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if read_would_block || write_would_block {
|
|
|
|
return Err(io::Error::from(io::ErrorKind::WouldBlock));
|
|
|
|
} else {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S, C> io::Read for TlsStream<S, C>
|
2017-03-16 02:15:06 +00:00
|
|
|
where S: AsyncRead + AsyncWrite, C: Session
|
2017-02-21 03:52:43 +00:00
|
|
|
{
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
2017-02-27 12:59:35 +00:00
|
|
|
loop {
|
|
|
|
match self.session.read(buf) {
|
|
|
|
Ok(0) if !self.eof => self.do_io()?,
|
|
|
|
Ok(n) => return Ok(n),
|
|
|
|
Err(e) => if e.kind() == io::ErrorKind::ConnectionAborted {
|
|
|
|
self.do_io()?;
|
|
|
|
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>
|
2017-03-16 02:15:06 +00:00
|
|
|
where S: AsyncRead + AsyncWrite, C: Session
|
2017-02-21 03:52:43 +00:00
|
|
|
{
|
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
2017-02-27 12:59:35 +00:00
|
|
|
let output = self.session.write(buf)?;
|
|
|
|
|
2017-03-16 02:15:06 +00:00
|
|
|
while self.session.wants_write() {
|
2017-02-27 12:59:35 +00:00
|
|
|
match self.session.write_tls(&mut self.io) {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => break,
|
|
|
|
Err(e) => return Err(e)
|
|
|
|
}
|
2017-02-21 03:52:43 +00:00
|
|
|
}
|
2017-02-27 12:59:35 +00:00
|
|
|
|
|
|
|
Ok(output)
|
2017-02-21 03:52:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
self.session.flush()?;
|
2017-02-28 00:53:52 +00:00
|
|
|
while self.session.wants_write() {
|
2017-02-21 03:52:43 +00:00
|
|
|
self.session.write_tls(&mut self.io)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-16 02:15:06 +00:00
|
|
|
impl<S, C> AsyncRead for TlsStream<S, C>
|
|
|
|
where
|
|
|
|
S: AsyncRead + AsyncWrite,
|
|
|
|
C: Session
|
|
|
|
{}
|
|
|
|
|
|
|
|
impl<S, C> AsyncWrite for TlsStream<S, C>
|
|
|
|
where
|
|
|
|
S: AsyncRead + AsyncWrite,
|
|
|
|
C: Session
|
|
|
|
{
|
|
|
|
fn shutdown(&mut self) -> Poll<(), io::Error> {
|
|
|
|
self.session.send_close_notify();
|
2017-03-30 07:39:11 +00:00
|
|
|
try_nb!(io::Write::flush(self));
|
2017-03-16 02:15:06 +00:00
|
|
|
self.io.shutdown()
|
|
|
|
}
|
2017-03-01 02:34:24 +00:00
|
|
|
}
|