2019-11-26 22:37:00 +00:00
|
|
|
use futures_util::future::TryFutureExt;
|
2019-05-19 16:28:27 +00:00
|
|
|
use lazy_static::lazy_static;
|
2021-09-28 17:01:37 +00:00
|
|
|
use rustls::{ClientConfig, OwnedTrustAnchor};
|
|
|
|
use rustls_pemfile::{certs, rsa_private_keys};
|
|
|
|
use std::convert::TryFrom;
|
2020-02-27 23:32:52 +00:00
|
|
|
use std::io::{BufReader, Cursor};
|
|
|
|
use std::net::SocketAddr;
|
|
|
|
use std::sync::mpsc::channel;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use std::{io, thread};
|
2020-12-23 21:42:03 +00:00
|
|
|
use tokio::io::{copy, split, AsyncReadExt, AsyncWriteExt};
|
2020-02-27 23:32:52 +00:00
|
|
|
use tokio::net::{TcpListener, TcpStream};
|
2019-11-26 22:37:00 +00:00
|
|
|
use tokio::runtime;
|
2020-02-27 23:32:52 +00:00
|
|
|
use tokio_rustls::{TlsAcceptor, TlsConnector};
|
2017-08-13 10:19:17 +00:00
|
|
|
|
|
|
|
const CERT: &str = include_str!("end.cert");
|
2021-09-28 17:01:37 +00:00
|
|
|
const CHAIN: &[u8] = include_bytes!("end.chain");
|
2017-08-13 10:19:17 +00:00
|
|
|
const RSA: &str = include_str!("end.rsa");
|
|
|
|
|
2020-02-27 23:32:52 +00:00
|
|
|
lazy_static! {
|
2021-09-28 17:01:37 +00:00
|
|
|
static ref TEST_SERVER: (SocketAddr, &'static str, &'static [u8]) = {
|
|
|
|
let cert = certs(&mut BufReader::new(Cursor::new(CERT)))
|
|
|
|
.unwrap()
|
|
|
|
.drain(..)
|
|
|
|
.map(rustls::Certificate)
|
|
|
|
.collect();
|
2018-08-16 12:44:37 +00:00
|
|
|
let mut keys = rsa_private_keys(&mut BufReader::new(Cursor::new(RSA))).unwrap();
|
2021-09-28 17:01:37 +00:00
|
|
|
let mut keys = keys.drain(..).map(rustls::PrivateKey);
|
2018-03-22 11:47:27 +00:00
|
|
|
|
2021-09-28 17:01:37 +00:00
|
|
|
let config = rustls::ServerConfig::builder()
|
|
|
|
.with_safe_defaults()
|
|
|
|
.with_no_client_auth()
|
|
|
|
.with_single_cert(cert, keys.next().unwrap())
|
|
|
|
.unwrap();
|
2019-05-21 16:54:10 +00:00
|
|
|
let acceptor = TlsAcceptor::from(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 || {
|
2020-10-16 10:26:32 +00:00
|
|
|
let runtime = runtime::Builder::new_current_thread()
|
2019-11-26 22:37:00 +00:00
|
|
|
.enable_io()
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
2020-10-16 10:26:32 +00:00
|
|
|
let runtime = Arc::new(runtime);
|
|
|
|
let runtime2 = runtime.clone();
|
2019-08-10 15:43:19 +00:00
|
|
|
|
|
|
|
let done = async move {
|
2019-05-19 16:28:27 +00:00
|
|
|
let addr = SocketAddr::from(([127, 0, 0, 1], 0));
|
2020-10-16 10:26:32 +00:00
|
|
|
let listener = TcpListener::bind(&addr).await?;
|
2019-05-19 16:28:27 +00:00
|
|
|
|
|
|
|
send.send(listener.local_addr()?).unwrap();
|
|
|
|
|
2019-11-26 22:37:00 +00:00
|
|
|
loop {
|
|
|
|
let (stream, _) = listener.accept().await?;
|
|
|
|
|
2019-05-21 16:54:10 +00:00
|
|
|
let acceptor = acceptor.clone();
|
2019-08-10 15:43:19 +00:00
|
|
|
let fut = async move {
|
2019-11-26 22:37:00 +00:00
|
|
|
let stream = acceptor.accept(stream).await?;
|
2019-08-10 15:43:19 +00:00
|
|
|
|
2019-10-01 15:00:49 +00:00
|
|
|
let (mut reader, mut writer) = split(stream);
|
2019-11-26 22:37:00 +00:00
|
|
|
copy(&mut reader, &mut writer).await?;
|
2019-08-10 15:43:19 +00:00
|
|
|
|
|
|
|
Ok(()) as io::Result<()>
|
2020-02-27 23:32:52 +00:00
|
|
|
}
|
|
|
|
.unwrap_or_else(|err| eprintln!("server: {:?}", err));
|
2019-08-10 15:43:19 +00:00
|
|
|
|
2020-10-16 10:26:32 +00:00
|
|
|
runtime2.spawn(fut);
|
2019-05-19 16:28:27 +00:00
|
|
|
}
|
2020-02-27 23:32:52 +00:00
|
|
|
}
|
|
|
|
.unwrap_or_else(|err: io::Error| eprintln!("server: {:?}", err));
|
2019-05-19 16:28:27 +00:00
|
|
|
|
2019-10-01 02:53:11 +00:00
|
|
|
runtime.block_on(done);
|
2018-08-16 12:44:37 +00:00
|
|
|
});
|
2017-08-13 10:19:17 +00:00
|
|
|
|
2018-08-16 12:44:37 +00:00
|
|
|
let addr = recv.recv().unwrap();
|
2019-10-01 15:00:49 +00:00
|
|
|
(addr, "testserver.com", CHAIN)
|
2018-08-16 12:44:37 +00:00
|
|
|
};
|
2017-08-13 10:19:17 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 17:01:37 +00:00
|
|
|
fn start_server() -> &'static (SocketAddr, &'static str, &'static [u8]) {
|
2018-08-16 12:44:37 +00:00
|
|
|
&*TEST_SERVER
|
|
|
|
}
|
|
|
|
|
2019-05-19 16:28:27 +00:00
|
|
|
async fn start_client(addr: SocketAddr, domain: &str, config: Arc<ClientConfig>) -> io::Result<()> {
|
2021-06-29 22:45:21 +00:00
|
|
|
const FILE: &[u8] = include_bytes!("../README.md");
|
2018-08-16 12:44:37 +00:00
|
|
|
|
2021-09-28 17:01:37 +00:00
|
|
|
let domain = rustls::ServerName::try_from(domain).unwrap();
|
2019-02-18 12:01:37 +00:00
|
|
|
let config = TlsConnector::from(config);
|
2019-05-19 16:28:27 +00:00
|
|
|
let mut buf = vec![0; FILE.len()];
|
|
|
|
|
|
|
|
let stream = TcpStream::connect(&addr).await?;
|
|
|
|
let mut stream = config.connect(domain, stream).await?;
|
|
|
|
stream.write_all(FILE).await?;
|
2019-10-01 02:53:11 +00:00
|
|
|
stream.flush().await?;
|
2019-05-19 16:28:27 +00:00
|
|
|
stream.read_exact(&mut buf).await?;
|
|
|
|
|
|
|
|
assert_eq!(buf, FILE);
|
2017-08-13 10:19:17 +00:00
|
|
|
|
2019-05-19 16:28:27 +00:00
|
|
|
Ok(())
|
2017-08-13 10:19:17 +00:00
|
|
|
}
|
|
|
|
|
2019-08-10 15:43:19 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn pass() -> io::Result<()> {
|
2018-08-16 12:44:37 +00:00
|
|
|
let (addr, domain, chain) = start_server();
|
2017-08-13 10:19:17 +00:00
|
|
|
|
2019-09-01 05:29:07 +00:00
|
|
|
// TODO: not sure how to resolve this right now but since
|
|
|
|
// TcpStream::bind now returns a future it creates a race
|
|
|
|
// condition until its ready sometimes.
|
|
|
|
use std::time::*;
|
2020-10-16 10:26:32 +00:00
|
|
|
tokio::time::sleep(Duration::from_secs(1)).await;
|
2019-09-01 05:29:07 +00:00
|
|
|
|
2021-09-28 17:01:37 +00:00
|
|
|
let chain = certs(&mut std::io::Cursor::new(*chain)).unwrap();
|
|
|
|
let trust_anchors = chain
|
|
|
|
.iter()
|
|
|
|
.map(|cert| {
|
|
|
|
let ta = webpki::TrustAnchor::try_from_cert_der(&cert[..]).unwrap();
|
|
|
|
OwnedTrustAnchor::from_subject_spki_name_constraints(
|
|
|
|
ta.subject,
|
|
|
|
ta.spki,
|
|
|
|
ta.name_constraints,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
let mut root_store = rustls::RootCertStore::empty();
|
|
|
|
root_store.add_server_trust_anchors(trust_anchors.into_iter());
|
|
|
|
let config = rustls::ClientConfig::builder()
|
|
|
|
.with_safe_defaults()
|
|
|
|
.with_root_certificates(root_store)
|
|
|
|
.with_no_client_auth();
|
2019-02-18 12:01:37 +00:00
|
|
|
let config = Arc::new(config);
|
|
|
|
|
2021-09-28 17:01:37 +00:00
|
|
|
start_client(*addr, domain, config).await?;
|
2019-08-10 15:43:19 +00:00
|
|
|
|
|
|
|
Ok(())
|
2018-03-22 11:47:27 +00:00
|
|
|
}
|
|
|
|
|
2019-08-10 15:43:19 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn fail() -> io::Result<()> {
|
2018-08-16 12:44:37 +00:00
|
|
|
let (addr, domain, chain) = start_server();
|
2017-08-13 10:19:17 +00:00
|
|
|
|
2021-09-28 17:01:37 +00:00
|
|
|
let chain = certs(&mut std::io::Cursor::new(*chain)).unwrap();
|
|
|
|
let trust_anchors = chain
|
|
|
|
.iter()
|
|
|
|
.map(|cert| {
|
|
|
|
let ta = webpki::TrustAnchor::try_from_cert_der(&cert[..]).unwrap();
|
|
|
|
OwnedTrustAnchor::from_subject_spki_name_constraints(
|
|
|
|
ta.subject,
|
|
|
|
ta.spki,
|
|
|
|
ta.name_constraints,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
let mut root_store = rustls::RootCertStore::empty();
|
|
|
|
root_store.add_server_trust_anchors(trust_anchors.into_iter());
|
|
|
|
let config = rustls::ClientConfig::builder()
|
|
|
|
.with_safe_defaults()
|
|
|
|
.with_root_certificates(root_store)
|
|
|
|
.with_no_client_auth();
|
2019-02-18 12:01:37 +00:00
|
|
|
let config = Arc::new(config);
|
|
|
|
|
2018-08-16 12:44:37 +00:00
|
|
|
assert_ne!(domain, &"google.com");
|
2021-06-29 22:45:21 +00:00
|
|
|
let ret = start_client(*addr, "google.com", config).await;
|
2019-08-10 15:43:19 +00:00
|
|
|
assert!(ret.is_err());
|
|
|
|
|
|
|
|
Ok(())
|
2017-08-13 10:19:17 +00:00
|
|
|
}
|