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;
|
2021-12-16 13:32:46 +00:00
|
|
|
use std::io::{BufReader, Cursor, ErrorKind};
|
2020-02-27 23:32:52 +00:00
|
|
|
use std::net::SocketAddr;
|
|
|
|
use std::sync::mpsc::channel;
|
|
|
|
use std::sync::Arc;
|
2021-12-16 13:32:46 +00:00
|
|
|
use std::time::Duration;
|
2020-02-27 23:32:52 +00:00
|
|
|
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};
|
2023-06-06 06:15:07 +00:00
|
|
|
use tokio::sync::oneshot;
|
2021-12-16 13:32:46 +00:00
|
|
|
use tokio::{runtime, time};
|
2021-10-30 06:10:58 +00:00
|
|
|
use tokio_rustls::{LazyConfigAcceptor, 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();
|
2022-07-28 10:51:38 +00:00
|
|
|
(addr, "foobar.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]) {
|
2022-11-04 21:11:05 +00:00
|
|
|
&TEST_SERVER
|
2018-08-16 12:44:37 +00:00
|
|
|
}
|
|
|
|
|
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 mut root_store = rustls::RootCertStore::empty();
|
2022-10-11 14:12:04 +00:00
|
|
|
root_store.add_server_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,
|
|
|
|
)
|
|
|
|
}));
|
|
|
|
|
2021-09-28 17:01:37 +00:00
|
|
|
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 mut root_store = rustls::RootCertStore::empty();
|
2022-10-11 14:12:04 +00:00
|
|
|
root_store.add_server_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,
|
|
|
|
)
|
|
|
|
}));
|
|
|
|
|
2021-09-28 17:01:37 +00:00
|
|
|
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
|
|
|
}
|
2021-10-30 06:10:58 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_lazy_config_acceptor() -> io::Result<()> {
|
|
|
|
let (sconfig, cconfig) = utils::make_configs();
|
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
|
|
|
let (cstream, sstream) = tokio::io::duplex(1200);
|
2022-07-28 10:51:38 +00:00
|
|
|
let domain = rustls::ServerName::try_from("foobar.com").unwrap();
|
2021-10-30 06:10:58 +00:00
|
|
|
tokio::spawn(async move {
|
|
|
|
let connector = crate::TlsConnector::from(cconfig);
|
|
|
|
let mut client = connector.connect(domain, cstream).await.unwrap();
|
|
|
|
client.write_all(b"hello, world!").await.unwrap();
|
|
|
|
|
|
|
|
let mut buf = Vec::new();
|
|
|
|
client.read_to_end(&mut buf).await.unwrap();
|
|
|
|
});
|
|
|
|
|
2022-10-23 05:43:45 +00:00
|
|
|
let acceptor = LazyConfigAcceptor::new(rustls::server::Acceptor::default(), sstream);
|
2021-10-30 06:10:58 +00:00
|
|
|
let start = acceptor.await.unwrap();
|
|
|
|
let ch = start.client_hello();
|
|
|
|
|
2022-07-28 10:51:38 +00:00
|
|
|
assert_eq!(ch.server_name(), Some("foobar.com"));
|
2021-10-30 06:10:58 +00:00
|
|
|
assert_eq!(
|
|
|
|
ch.alpn()
|
|
|
|
.map(|protos| protos.collect::<Vec<_>>())
|
2022-10-11 14:12:04 +00:00
|
|
|
.unwrap_or_default(),
|
2021-10-30 06:10:58 +00:00
|
|
|
Vec::<&[u8]>::new()
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut stream = start.into_stream(sconfig).await.unwrap();
|
|
|
|
let mut buf = [0; 13];
|
|
|
|
stream.read_exact(&mut buf).await.unwrap();
|
|
|
|
assert_eq!(&buf[..], b"hello, world!");
|
|
|
|
|
|
|
|
stream.write_all(b"bye").await.unwrap();
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-12-16 13:32:46 +00:00
|
|
|
// This test is a follow-up from https://github.com/tokio-rs/tls/issues/85
|
|
|
|
#[tokio::test]
|
|
|
|
async fn lazy_config_acceptor_eof() {
|
|
|
|
let buf = Cursor::new(Vec::new());
|
2022-10-23 05:43:45 +00:00
|
|
|
let acceptor = LazyConfigAcceptor::new(rustls::server::Acceptor::default(), buf);
|
2021-12-16 13:32:46 +00:00
|
|
|
|
|
|
|
let accept_result = match time::timeout(Duration::from_secs(3), acceptor).await {
|
|
|
|
Ok(res) => res,
|
|
|
|
Err(_elapsed) => panic!("timeout"),
|
|
|
|
};
|
|
|
|
|
|
|
|
match accept_result {
|
|
|
|
Ok(_) => panic!("accepted a connection from zero bytes of data"),
|
|
|
|
Err(e) if e.kind() == ErrorKind::UnexpectedEof => {}
|
|
|
|
Err(e) => panic!("unexpected error: {:?}", e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-06 06:15:07 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn lazy_config_acceptor_take_io() -> Result<(), rustls::Error> {
|
|
|
|
let (mut cstream, sstream) = tokio::io::duplex(1200);
|
|
|
|
|
|
|
|
let (tx, rx) = oneshot::channel();
|
|
|
|
|
|
|
|
tokio::spawn(async move {
|
|
|
|
cstream.write_all(b"hello, world!").await.unwrap();
|
|
|
|
|
|
|
|
let mut buf = Vec::new();
|
|
|
|
cstream.read_to_end(&mut buf).await.unwrap();
|
|
|
|
tx.send(buf).unwrap();
|
|
|
|
});
|
|
|
|
|
|
|
|
let acceptor = LazyConfigAcceptor::new(rustls::server::Acceptor::default(), sstream);
|
|
|
|
futures_util::pin_mut!(acceptor);
|
|
|
|
if (acceptor.as_mut().await).is_ok() {
|
|
|
|
panic!("Expected Err(err)");
|
|
|
|
}
|
|
|
|
|
|
|
|
let server_msg = b"message from server";
|
|
|
|
|
|
|
|
let some_io = acceptor.take_io();
|
|
|
|
assert!(some_io.is_some(), "Expected Some(io)");
|
|
|
|
some_io.unwrap().write_all(server_msg).await.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(rx.await.unwrap(), server_msg);
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
acceptor.take_io().is_none(),
|
|
|
|
"Should not be able to take twice"
|
|
|
|
);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-10-30 06:10:58 +00:00
|
|
|
// Include `utils` module
|
|
|
|
include!("utils.rs");
|