2021-09-28 17:01:37 +00:00
|
|
|
use std::convert::TryFrom;
|
2019-02-18 12:01:37 +00:00
|
|
|
use std::io;
|
|
|
|
use std::net::ToSocketAddrs;
|
2020-02-27 23:32:52 +00:00
|
|
|
use std::sync::Arc;
|
2020-12-23 21:42:03 +00:00
|
|
|
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
2019-08-10 16:00:49 +00:00
|
|
|
use tokio::net::TcpStream;
|
2021-09-28 17:01:37 +00:00
|
|
|
use tokio_rustls::{
|
|
|
|
client::TlsStream,
|
|
|
|
rustls::{self, ClientConfig, OwnedTrustAnchor},
|
|
|
|
TlsConnector,
|
|
|
|
};
|
2019-02-18 12:01:37 +00:00
|
|
|
|
2020-02-27 23:32:52 +00:00
|
|
|
async fn get(
|
|
|
|
config: Arc<ClientConfig>,
|
|
|
|
domain: &str,
|
|
|
|
port: u16,
|
|
|
|
) -> io::Result<(TlsStream<TcpStream>, String)> {
|
2019-10-01 08:27:42 +00:00
|
|
|
let connector = TlsConnector::from(config);
|
2019-02-18 12:01:37 +00:00
|
|
|
let input = format!("GET / HTTP/1.0\r\nHost: {}\r\n\r\n", domain);
|
|
|
|
|
2020-02-27 23:32:52 +00:00
|
|
|
let addr = (domain, port).to_socket_addrs()?.next().unwrap();
|
2021-09-28 17:01:37 +00:00
|
|
|
let domain = rustls::ServerName::try_from(domain).unwrap();
|
2019-05-18 16:48:56 +00:00
|
|
|
let mut buf = Vec::new();
|
|
|
|
|
|
|
|
let stream = TcpStream::connect(&addr).await?;
|
|
|
|
let mut stream = connector.connect(domain, stream).await?;
|
|
|
|
stream.write_all(input.as_bytes()).await?;
|
2019-10-01 02:53:11 +00:00
|
|
|
stream.flush().await?;
|
2019-05-18 16:48:56 +00:00
|
|
|
stream.read_to_end(&mut buf).await?;
|
2019-02-18 12:01:37 +00:00
|
|
|
|
2019-05-18 16:48:56 +00:00
|
|
|
Ok((stream, String::from_utf8(buf).unwrap()))
|
2019-02-18 12:01:37 +00:00
|
|
|
}
|
|
|
|
|
2019-08-10 16:00:49 +00:00
|
|
|
#[tokio::test]
|
2019-10-01 08:27:42 +00:00
|
|
|
async fn test_tls12() -> io::Result<()> {
|
2021-09-28 17:01:37 +00:00
|
|
|
let mut root_store = rustls::RootCertStore::empty();
|
|
|
|
root_store.add_server_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.0.iter().map(|ta| {
|
|
|
|
OwnedTrustAnchor::from_subject_spki_name_constraints(
|
|
|
|
ta.subject,
|
|
|
|
ta.spki,
|
|
|
|
ta.name_constraints,
|
|
|
|
)
|
|
|
|
}));
|
|
|
|
let config = rustls::ClientConfig::builder()
|
|
|
|
.with_safe_default_cipher_suites()
|
|
|
|
.with_safe_default_kx_groups()
|
|
|
|
.with_protocol_versions(&[&rustls::version::TLS12])
|
|
|
|
.unwrap()
|
|
|
|
.with_root_certificates(root_store)
|
|
|
|
.with_no_client_auth();
|
|
|
|
|
2019-02-18 12:01:37 +00:00
|
|
|
let config = Arc::new(config);
|
2019-10-01 08:27:42 +00:00
|
|
|
let domain = "tls-v1-2.badssl.com";
|
2019-02-18 12:01:37 +00:00
|
|
|
|
2019-10-01 08:27:42 +00:00
|
|
|
let (_, output) = get(config.clone(), domain, 1012).await?;
|
2021-09-28 17:01:37 +00:00
|
|
|
assert!(
|
|
|
|
output.contains("<title>tls-v1-2.badssl.com</title>"),
|
|
|
|
"failed badssl test, output: {}",
|
|
|
|
output
|
|
|
|
);
|
2019-02-18 12:01:37 +00:00
|
|
|
|
2019-10-01 08:27:42 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-10-31 16:04:06 +00:00
|
|
|
#[ignore]
|
2019-10-01 08:27:42 +00:00
|
|
|
#[should_panic]
|
|
|
|
#[test]
|
|
|
|
fn test_tls13() {
|
|
|
|
unimplemented!("todo https://github.com/chromium/badssl.com/pull/373");
|
|
|
|
}
|
2019-02-18 12:01:37 +00:00
|
|
|
|
2019-10-01 08:27:42 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_modern() -> io::Result<()> {
|
2021-09-28 17:01:37 +00:00
|
|
|
let mut root_store = rustls::RootCertStore::empty();
|
|
|
|
root_store.add_server_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.0.iter().map(|ta| {
|
|
|
|
OwnedTrustAnchor::from_subject_spki_name_constraints(
|
|
|
|
ta.subject,
|
|
|
|
ta.spki,
|
|
|
|
ta.name_constraints,
|
|
|
|
)
|
|
|
|
}));
|
|
|
|
let config = rustls::ClientConfig::builder()
|
|
|
|
.with_safe_defaults()
|
|
|
|
.with_root_certificates(root_store)
|
|
|
|
.with_no_client_auth();
|
2019-10-01 08:27:42 +00:00
|
|
|
let config = Arc::new(config);
|
|
|
|
let domain = "mozilla-modern.badssl.com";
|
|
|
|
|
|
|
|
let (_, output) = get(config.clone(), domain, 443).await?;
|
2021-09-28 17:01:37 +00:00
|
|
|
assert!(
|
|
|
|
output.contains("<title>mozilla-modern.badssl.com</title>"),
|
|
|
|
"failed badssl test, output: {}",
|
|
|
|
output
|
|
|
|
);
|
2019-08-10 16:00:49 +00:00
|
|
|
|
|
|
|
Ok(())
|
2019-02-18 12:01:37 +00:00
|
|
|
}
|