tokio-rustls/src/lib.rs

234 lines
6.6 KiB
Rust
Raw Normal View History

2017-02-22 05:03:21 +00:00
//! Async TLS streams
//!
//! [tokio-tls](https://github.com/tokio-rs/tokio-tls) fork, use [rustls](https://github.com/ctz/rustls).
2017-02-22 05:30:01 +00:00
#[cfg_attr(feature = "tokio-proto", macro_use)]
2017-02-21 03:52:43 +00:00
extern crate futures;
extern crate tokio_core;
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 };
use tokio_core::io::Io;
use rustls::{ Session, ClientSession, ServerSession };
2017-02-22 05:30:01 +00:00
use rustls::{ 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>
where S: Io;
}
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>
where S: Io;
}
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>
where S: Io
{
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>
where S: Io
{
AcceptAsync(MidHandshake {
inner: Some(TlsStream::new(stream, ServerSession::new(self)))
})
}
}
impl<S: Io> Future for ConnectAsync<S> {
type Item = TlsStream<S, ClientSession>;
type Error = io::Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
self.0.poll()
}
}
impl<S: Io> Future for AcceptAsync<S> {
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>
where S: Io, C: Session
{
type Item = TlsStream<S, C>;
type Error = io::Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
loop {
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() {
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
}
}
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>
where S: Io, C: Session
{
#[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 {
let read_would_block = match (!self.eof && self.session.wants_read(), self.io.poll_read()) {
(true, Async::Ready(())) => {
match self.session.read_tls(&mut self.io) {
Ok(0) => self.eof = true,
Ok(_) => self.session.process_new_packets()
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?,
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => (),
Err(e) => return Err(e)
};
continue
},
(true, Async::NotReady) => true,
(false, _) => false,
};
let write_would_block = match (self.session.wants_write(), self.io.poll_write()) {
(true, Async::Ready(())) => match self.session.write_tls(&mut self.io) {
Ok(_) => continue,
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => continue,
Err(e) => return Err(e)
},
(true, Async::NotReady) => true,
(false, _) => false
};
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>
where S: Io, C: Session
{
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
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>
where S: Io, C: Session
{
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let output = self.session.write(buf)?;
2017-02-21 03:52:43 +00:00
while self.session.wants_write() && self.io.poll_write().is_ready() {
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
}
Ok(output)
2017-02-21 03:52:43 +00:00
}
fn flush(&mut self) -> io::Result<()> {
self.session.flush()?;
while self.session.wants_write() {
2017-02-21 03:52:43 +00:00
self.session.write_tls(&mut self.io)?;
}
Ok(())
}
}
impl<S, C> Io for TlsStream<S, C> where S: Io, C: Session {
// TODO impl poll_{read, write}
}