| 1 |
use super::Stream; |
| 2 |
use futures_executor::block_on; |
| 3 |
use futures_io::{AsyncRead, AsyncWrite}; |
| 4 |
use futures_util::io::{AsyncReadExt, AsyncWriteExt}; |
| 5 |
use futures_util::task::{noop_waker_ref, Context}; |
| 6 |
use futures_util::{future, ready}; |
| •7 |
use rustls::pki_types::{PrivateKeyDer, ServerName}; |
| 8 |
use rustls::{ |
| •9 |
ClientConfig, ClientConnection, ConnectionCommon, RootCertStore, |
| 10 |
ServerConfig, ServerConnection, |
| 11 |
}; |
| 12 |
use rustls_pemfile::{certs, pkcs8_private_keys}; |
| 13 |
use std::convert::TryFrom; |
| 14 |
use std::io::{self, BufReader, Cursor, Read, Write}; |
| 15 |
use std::pin::Pin; |
| 16 |
use std::sync::Arc; |
| 17 |
use std::task::Poll; |
| 18 |
|
| 19 |
struct Good<'a, D>(&'a mut ConnectionCommon<D>); |
| 20 |
|
| 21 |
impl<'a, D> AsyncRead for Good<'a, D> { |
| 22 |
fn poll_read( |
| 23 |
mut self: Pin<&mut Self>, |
| 24 |
_cx: &mut Context<'_>, |
| 25 |
mut buf: &mut [u8], |
| 26 |
) -> Poll<io::Result<usize>> { |
| 27 |
Poll::Ready(self.0.write_tls(buf.by_ref())) |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
impl<'a, D> AsyncWrite for Good<'a, D> { |
| 32 |
fn poll_write( |
| 33 |
mut self: Pin<&mut Self>, |
| 34 |
_cx: &mut Context<'_>, |
| 35 |
mut buf: &[u8], |
| 36 |
) -> Poll<io::Result<usize>> { |
| 37 |
let len = self.0.read_tls(buf.by_ref())?; |
| 38 |
self.0 |
| 39 |
.process_new_packets() |
| 40 |
.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?; |
| 41 |
Poll::Ready(Ok(len)) |
| 42 |
} |
| 43 |
|
| 44 |
fn poll_flush(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> { |
| 45 |
self.0 |
| 46 |
.process_new_packets() |
| 47 |
.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?; |
| 48 |
Poll::Ready(Ok(())) |
| 49 |
} |
| 50 |
|
| 51 |
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { |
| 52 |
self.0.send_close_notify(); |
| 53 |
self.poll_flush(cx) |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
struct Bad(bool); |
| 58 |
|
| 59 |
impl AsyncRead for Bad { |
| 60 |
fn poll_read( |
| 61 |
self: Pin<&mut Self>, |
| 62 |
_cx: &mut Context<'_>, |
| 63 |
_: &mut [u8], |
| 64 |
) -> Poll<io::Result<usize>> { |
| 65 |
Poll::Ready(Ok(0)) |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
impl AsyncWrite for Bad { |
| 70 |
fn poll_write( |
| 71 |
self: Pin<&mut Self>, |
| 72 |
_cx: &mut Context<'_>, |
| 73 |
buf: &[u8], |
| 74 |
) -> Poll<io::Result<usize>> { |
| 75 |
if self.0 { |
| 76 |
Poll::Pending |
| 77 |
} else { |
| 78 |
Poll::Ready(Ok(buf.len())) |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> { |
| 83 |
Poll::Ready(Ok(())) |
| 84 |
} |
| 85 |
|
| 86 |
fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> { |
| 87 |
Poll::Ready(Ok(())) |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
#[test] |
| 92 |
fn stream_good() -> io::Result<()> { |
| 93 |
block_on(_stream_good()) |
| 94 |
} |
| 95 |
|
| 96 |
async fn _stream_good() -> io::Result<()> { |
| 97 |
const FILE: &[u8] = include_bytes!("../../README.md"); |
| 98 |
let (mut server, mut client) = make_pair(); |
| 99 |
future::poll_fn(|cx| do_handshake(&mut client, &mut server, cx)).await?; |
| 100 |
io::copy(&mut Cursor::new(FILE), &mut server.writer())?; |
| 101 |
server.send_close_notify(); |
| 102 |
|
| 103 |
{ |
| 104 |
let mut good = Good(&mut server); |
| 105 |
let mut stream = Stream::new(&mut good, &mut client); |
| 106 |
|
| 107 |
let mut buf = Vec::new(); |
| 108 |
stream.read_to_end(&mut buf).await?; |
| 109 |
assert_eq!(buf, FILE); |
| 110 |
stream.write_all(b"Hello World!").await?; |
| 111 |
stream.conn.send_close_notify(); |
| 112 |
stream.close().await?; |
| 113 |
} |
| 114 |
|
| 115 |
let mut buf = String::new(); |
| 116 |
server.reader().read_to_string(&mut buf)?; |
| 117 |
assert_eq!(buf, "Hello World!"); |
| 118 |
|
| 119 |
Ok(()) as io::Result<()> |
| 120 |
} |
| 121 |
|
| 122 |
#[test] |
| 123 |
fn stream_bad() -> io::Result<()> { |
| 124 |
let fut = async { |
| 125 |
let (mut server, mut client) = make_pair(); |
| 126 |
future::poll_fn(|cx| do_handshake(&mut client, &mut server, cx)).await?; |
| 127 |
client.set_buffer_limit(Some(1024)); |
| 128 |
|
| 129 |
let mut bad = Bad(true); |
| 130 |
let mut stream = Stream::new(&mut bad, &mut client); |
| 131 |
assert_eq!( |
| 132 |
future::poll_fn(|cx| stream.as_mut_pin().poll_write(cx, &[0x42; 8])).await?, |
| 133 |
8 |
| 134 |
); |
| 135 |
assert_eq!( |
| 136 |
future::poll_fn(|cx| stream.as_mut_pin().poll_write(cx, &[0x42; 8])).await?, |
| 137 |
8 |
| 138 |
); |
| 139 |
let r = future::poll_fn(|cx| stream.as_mut_pin().poll_write(cx, &[0x00; 1024])).await?; // fill buffer |
| 140 |
assert!(r < 1024); |
| 141 |
|
| 142 |
let mut cx = Context::from_waker(noop_waker_ref()); |
| 143 |
assert!(stream |
| 144 |
.as_mut_pin() |
| 145 |
.poll_write(&mut cx, &[0x01]) |
| 146 |
.is_pending()); |
| 147 |
|
| 148 |
Ok(()) as io::Result<()> |
| 149 |
}; |
| 150 |
|
| 151 |
block_on(fut) |
| 152 |
} |
| 153 |
|
| 154 |
#[test] |
| 155 |
fn stream_handshake() -> io::Result<()> { |
| 156 |
let fut = async { |
| 157 |
let (mut server, mut client) = make_pair(); |
| 158 |
|
| 159 |
{ |
| 160 |
let mut good = Good(&mut server); |
| 161 |
let mut stream = Stream::new(&mut good, &mut client); |
| 162 |
let (r, w) = future::poll_fn(|cx| stream.complete_io(cx)).await?; |
| 163 |
|
| 164 |
assert!(r > 0); |
| 165 |
assert!(w > 0); |
| 166 |
|
| 167 |
future::poll_fn(|cx| stream.complete_io(cx)).await?; // finish server handshake |
| 168 |
} |
| 169 |
|
| 170 |
assert!(!server.is_handshaking()); |
| 171 |
assert!(!client.is_handshaking()); |
| 172 |
|
| 173 |
Ok(()) as io::Result<()> |
| 174 |
}; |
| 175 |
|
| 176 |
block_on(fut) |
| 177 |
} |
| 178 |
|
| 179 |
#[test] |
| 180 |
fn stream_handshake_eof() -> io::Result<()> { |
| 181 |
let fut = async { |
| 182 |
let (_, mut client) = make_pair(); |
| 183 |
|
| 184 |
let mut bad = Bad(false); |
| 185 |
let mut stream = Stream::new(&mut bad, &mut client); |
| 186 |
|
| 187 |
let mut cx = Context::from_waker(noop_waker_ref()); |
| 188 |
let r = stream.complete_io(&mut cx); |
| 189 |
assert_eq!( |
| 190 |
r.map_err(|err| err.kind()), |
| 191 |
Poll::Ready(Err(io::ErrorKind::UnexpectedEof)) |
| 192 |
); |
| 193 |
|
| 194 |
Ok(()) as io::Result<()> |
| 195 |
}; |
| 196 |
|
| 197 |
block_on(fut) |
| 198 |
} |
| 199 |
|
| 200 |
#[test] |
| 201 |
fn stream_eof() -> io::Result<()> { |
| 202 |
let fut = async { |
| 203 |
let (mut server, mut client) = make_pair(); |
| 204 |
future::poll_fn(|cx| do_handshake(&mut client, &mut server, cx)).await?; |
| 205 |
|
| 206 |
let mut eof_stream = Bad(false); |
| 207 |
let mut stream = Stream::new(&mut eof_stream, &mut client); |
| 208 |
|
| 209 |
let mut buf = Vec::new(); |
| 210 |
let res = stream.read_to_end(&mut buf).await; |
| 211 |
assert_eq!( |
| 212 |
res.err().map(|e| e.kind()), |
| 213 |
Some(std::io::ErrorKind::UnexpectedEof) |
| 214 |
); |
| 215 |
|
| 216 |
Ok(()) as io::Result<()> |
| 217 |
}; |
| 218 |
|
| 219 |
block_on(fut) |
| 220 |
} |
| 221 |
|
| 222 |
fn make_pair() -> (ServerConnection, ClientConnection) { |
| 223 |
const CERT: &str = include_str!("../../tests/end.cert"); |
| 224 |
const CHAIN: &str = include_str!("../../tests/end.chain"); |
| 225 |
const RSA: &str = include_str!("../../tests/end.rsa"); |
| 226 |
|
| •227 |
let cert = certs(&mut BufReader::new(Cursor::new(CERT))).map(|c| c.unwrap()).collect::<Vec<_>>(); |
| 228 |
let mut keys = pkcs8_private_keys(&mut BufReader::new(Cursor::new(RSA))).map(|c| c.unwrap()).collect::<Vec<_>>(); |
| 229 |
let key = PrivateKeyDer::from(keys.pop().unwrap()); |
| 230 |
let sconfig = ServerConfig::builder() |
| 231 |
.with_no_client_auth() |
| 232 |
.with_single_cert(cert, key) |
| 233 |
.unwrap(); |
| 234 |
let server = ServerConnection::new(Arc::new(sconfig)); |
| 235 |
|
| 236 |
let domain = ServerName::try_from("localhost").unwrap(); |
| 237 |
let mut root_store = RootCertStore::empty(); |
| •238 |
let chain = certs(&mut BufReader::new(Cursor::new(CHAIN))).map(|c| c.unwrap()).collect::<Vec<_>>(); |
| 239 |
let (added, ignored) = root_store.add_parsable_certificates(chain); |
| 240 |
assert!(added >= 1 && ignored == 0); |
| 241 |
let cconfig = ClientConfig::builder() |
| 242 |
.with_root_certificates(root_store) |
| 243 |
.with_no_client_auth(); |
| 244 |
let client = ClientConnection::new(Arc::new(cconfig), domain); |
| 245 |
|
| 246 |
(server.unwrap(), client.unwrap()) |
| 247 |
} |
| 248 |
|
| 249 |
fn do_handshake( |
| 250 |
client: &mut ClientConnection, |
| 251 |
server: &mut ServerConnection, |
| 252 |
cx: &mut Context<'_>, |
| 253 |
) -> Poll<io::Result<()>> { |
| 254 |
let mut good = Good(server); |
| 255 |
let mut stream = Stream::new(&mut good, client); |
| 256 |
|
| 257 |
if stream.conn.is_handshaking() { |
| 258 |
ready!(stream.complete_io(cx))?; |
| 259 |
} |
| 260 |
|
| 261 |
if stream.conn.wants_write() { |
| 262 |
ready!(stream.complete_io(cx))?; |
| 263 |
} |
| 264 |
|
| 265 |
Poll::Ready(Ok(())) |
| 266 |
} |