2018-08-16 12:44:37 +00:00
|
|
|
#[macro_use] extern crate lazy_static;
|
2017-08-13 10:19:17 +00:00
|
|
|
extern crate rustls;
|
2018-02-28 06:36:37 +00:00
|
|
|
extern crate tokio;
|
2017-08-13 10:19:17 +00:00
|
|
|
extern crate tokio_rustls;
|
2017-09-03 22:58:55 +00:00
|
|
|
extern crate webpki;
|
2017-08-13 10:19:17 +00:00
|
|
|
|
|
|
|
use std::{ io, thread };
|
|
|
|
use std::io::{ BufReader, Cursor };
|
|
|
|
use std::sync::Arc;
|
|
|
|
use std::sync::mpsc::channel;
|
2018-08-16 12:44:37 +00:00
|
|
|
use std::net::SocketAddr;
|
2018-02-28 06:36:37 +00:00
|
|
|
use tokio::net::{ TcpListener, TcpStream };
|
2018-08-16 12:44:37 +00:00
|
|
|
use rustls::{ ServerConfig, ClientConfig };
|
2017-08-13 10:19:17 +00:00
|
|
|
use rustls::internal::pemfile::{ certs, rsa_private_keys };
|
|
|
|
use tokio_rustls::{ ClientConfigExt, ServerConfigExt };
|
|
|
|
|
|
|
|
const CERT: &str = include_str!("end.cert");
|
|
|
|
const CHAIN: &str = include_str!("end.chain");
|
|
|
|
const RSA: &str = include_str!("end.rsa");
|
|
|
|
|
2018-08-16 12:44:37 +00:00
|
|
|
lazy_static!{
|
|
|
|
static ref TEST_SERVER: (SocketAddr, &'static str, &'static str) = {
|
|
|
|
use tokio::prelude::*;
|
|
|
|
use tokio::io as aio;
|
2017-08-13 10:19:17 +00:00
|
|
|
|
2018-08-16 12:44:37 +00:00
|
|
|
let cert = certs(&mut BufReader::new(Cursor::new(CERT))).unwrap();
|
|
|
|
let mut keys = rsa_private_keys(&mut BufReader::new(Cursor::new(RSA))).unwrap();
|
2018-03-22 11:47:27 +00:00
|
|
|
|
2018-08-16 12:44:37 +00:00
|
|
|
let mut config = ServerConfig::new(rustls::NoClientAuth::new());
|
|
|
|
config.set_single_cert(cert, keys.pop().unwrap())
|
|
|
|
.expect("invalid key or certificate");
|
|
|
|
let config = Arc::new(config);
|
2017-08-13 10:19:17 +00:00
|
|
|
|
2018-08-16 12:44:37 +00:00
|
|
|
let (send, recv) = channel();
|
2017-08-13 10:19:17 +00:00
|
|
|
|
2018-08-16 12:44:37 +00:00
|
|
|
thread::spawn(move || {
|
|
|
|
let addr = SocketAddr::from(([127, 0, 0, 1], 0));
|
|
|
|
let listener = TcpListener::bind(&addr).unwrap();
|
2017-08-13 10:19:17 +00:00
|
|
|
|
2018-08-16 12:44:37 +00:00
|
|
|
send.send(listener.local_addr().unwrap()).unwrap();
|
2017-08-13 10:19:17 +00:00
|
|
|
|
2018-08-16 12:44:37 +00:00
|
|
|
let done = listener.incoming()
|
|
|
|
.for_each(move |stream| {
|
|
|
|
let done = config.accept_async(stream)
|
|
|
|
.and_then(|stream| {
|
|
|
|
let (reader, writer) = stream.split();
|
|
|
|
aio::copy(reader, writer)
|
|
|
|
})
|
|
|
|
.then(|_| Ok(()));
|
2017-08-13 10:19:17 +00:00
|
|
|
|
2018-08-16 12:44:37 +00:00
|
|
|
tokio::spawn(done);
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.map_err(|err| panic!("{:?}", err));
|
2018-02-28 06:36:37 +00:00
|
|
|
|
2018-08-16 12:44:37 +00:00
|
|
|
tokio::run(done);
|
|
|
|
});
|
2017-08-13 10:19:17 +00:00
|
|
|
|
2018-08-16 12:44:37 +00:00
|
|
|
let addr = recv.recv().unwrap();
|
|
|
|
(addr, "localhost", CHAIN)
|
|
|
|
};
|
2017-08-13 10:19:17 +00:00
|
|
|
}
|
|
|
|
|
2018-08-16 12:44:37 +00:00
|
|
|
|
|
|
|
fn start_server() -> &'static (SocketAddr, &'static str, &'static str) {
|
|
|
|
&*TEST_SERVER
|
|
|
|
}
|
|
|
|
|
|
|
|
fn start_client(addr: &SocketAddr, domain: &str, chain: &str) -> io::Result<()> {
|
2018-03-22 11:47:27 +00:00
|
|
|
use tokio::prelude::*;
|
|
|
|
use tokio::io as aio;
|
|
|
|
|
2018-08-16 12:44:37 +00:00
|
|
|
const FILE: &'static [u8] = include_bytes!("../README.md");
|
|
|
|
|
2017-09-03 22:58:55 +00:00
|
|
|
let domain = webpki::DNSNameRef::try_from_ascii_str(domain).unwrap();
|
2017-08-13 10:19:17 +00:00
|
|
|
let mut config = ClientConfig::new();
|
2018-08-16 12:44:37 +00:00
|
|
|
let mut chain = BufReader::new(Cursor::new(chain));
|
|
|
|
config.root_store.add_pem_file(&mut chain).unwrap();
|
2017-08-13 10:19:17 +00:00
|
|
|
let config = Arc::new(config);
|
|
|
|
|
2018-02-28 06:36:37 +00:00
|
|
|
let done = TcpStream::connect(addr)
|
Remove `danger` feature & the API it controls.
The singular purpose of this crate should be to integrate Tokio and
Rustls. Therefore, any feature that isn't about making Rustls work
nicely with Tokio should be assumed a priori to be out of scope.
In particular, it is out of scope for tokio-rustls to provide APIs to
control SNI behavior. Instead, the application should configure
Rustls's SNI behavior using Rustls's configuration APIs, and pass the
configuration to tokio-rustls. Similarly, it is out of scope for
tokio-rustls to provide APIs to control the certificate validation
behavior. Instead, the application should configure certificate
validation using Rustls's APIs. Perhaps there should be a crate that
makes it convenient to do "dangerous" certificate validation, but IMO
that shouldn't be tokio-rustls, but a different one.
FWIW, the `danger` API was inherited from tokio-tls, and I'm working on
making an analogous change there.
2017-08-29 04:40:16 +00:00
|
|
|
.and_then(|stream| config.connect_async(domain, stream))
|
2018-08-16 12:44:37 +00:00
|
|
|
.and_then(|stream| aio::write_all(stream, FILE))
|
|
|
|
.and_then(|(stream, _)| aio::read_exact(stream, vec![0; FILE.len()]))
|
2018-03-31 07:16:31 +00:00
|
|
|
.and_then(|(stream, buf)| {
|
2018-08-16 12:44:37 +00:00
|
|
|
assert_eq!(buf, FILE);
|
2018-03-31 07:16:31 +00:00
|
|
|
aio::shutdown(stream)
|
|
|
|
})
|
|
|
|
.map(drop);
|
2017-08-13 10:19:17 +00:00
|
|
|
|
2018-02-28 06:36:37 +00:00
|
|
|
done.wait()
|
2017-08-13 10:19:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
2018-03-22 11:47:27 +00:00
|
|
|
fn pass() {
|
2018-08-16 12:44:37 +00:00
|
|
|
let (addr, domain, chain) = start_server();
|
2017-08-13 10:19:17 +00:00
|
|
|
|
2018-08-16 12:44:37 +00:00
|
|
|
start_client(addr, domain, chain).unwrap();
|
2017-08-13 10:19:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fail() {
|
2018-08-16 12:44:37 +00:00
|
|
|
let (addr, domain, chain) = start_server();
|
2017-08-13 10:19:17 +00:00
|
|
|
|
2018-08-16 12:44:37 +00:00
|
|
|
assert_ne!(domain, &"google.com");
|
|
|
|
assert!(start_client(addr, "google.com", chain).is_err());
|
2017-08-13 10:19:17 +00:00
|
|
|
}
|