modify shellchat net tls test to use trusted CA certificates

This commit is contained in:
nworbnhoj 2023-06-26 16:00:21 +10:00
parent 6ef1e8d67c
commit ab8d40189a

View file

@ -536,58 +536,55 @@ impl<'a> ShellCmdApi<'a> for NetCmd {
Some("test") => { Some("test") => {
log::set_max_level(log::LevelFilter::Info); log::set_max_level(log::LevelFilter::Info);
log::info!("starting TLS run"); log::info!("starting TLS run");
let mut root_store = rustls::RootCertStore::empty();
log::info!("create root store");
root_store.add_server_trust_anchors(
webpki_roots::TLS_SERVER_ROOTS
.0
.iter()
.map(|ta| {
rustls::OwnedTrustAnchor::from_subject_spki_name_constraints(
ta.subject,
ta.spki,
ta.name_constraints,
)
})
);
log::info!("build TLS client config"); log::info!("build TLS client config");
let trusted = Trusted::new().unwrap();
let config = rustls::ClientConfig::builder() let config = rustls::ClientConfig::builder()
.with_safe_defaults() .with_safe_defaults()
.with_root_certificates(root_store) .with_root_certificates(trusted.into())
.with_no_client_auth(); .with_no_client_auth();
let target = match tokens.next() {
log::info!("point TLS to bunniefoo.com"); Some(target) => target,
let server_name = "bunniefoo.com".try_into().unwrap(); None => "bunnyfoo.com",
};
log::info!("point TLS to {}", target);
let mut conn = let mut conn =
rustls::ClientConnection::new(Arc::new(config), server_name) rustls::ClientConnection::new(Arc::new(config), target.try_into().unwrap())
.unwrap(); .unwrap();
log::info!("connect TCPstream to bunniefoo.com"); log::info!("connect TCPstream to {}", target);
let mut sock = TcpStream::connect("bunniefoo.com:443").unwrap(); let url = format!("{}:443", target);
let mut sock = TcpStream::connect(url).unwrap();
let mut tls = rustls::Stream::new(&mut conn, &mut sock); let mut tls = rustls::Stream::new(&mut conn, &mut sock);
log::info!("create http headers and write to server"); log::info!("create http headers and write to server");
tls.write_all( let msg = format!("GET / HTTP/1.1\r\nHost: {}\r\nConnection: close\r\nAccept-Encoding: identity\r\n\r\n", target);
concat!( match tls.write_all(msg.as_bytes()) {
"GET / HTTP/1.1\r\n", Ok(()) => {
"Host: bunniefoo.com\r\n", log::info!("GET on tls accepted");
"Connection: close\r\n", write!(ret, "GET on tls accepted\n").ok();
"Accept-Encoding: identity\r\n", }
"\r\n" Err(e) => {
) log::warn!("failed to GET on tls connection: {e}");
.as_bytes(), write!(ret, "{e}\n").ok();
) }
.unwrap(); };
log::info!("readout cipher suite");
let ciphersuite = tls.conn.negotiated_cipher_suite().unwrap();
log::info!("Current ciphersuite: {:?}", ciphersuite.suite());
let mut plaintext = Vec::new(); let mut plaintext = Vec::new();
log::info!("read TLS response"); log::info!("read TLS response");
tls.read_to_end(&mut plaintext).unwrap(); match tls.read_to_end(&mut plaintext) {
log::info!("len: {}", plaintext.len()); Ok(n) => {
log::info!("received {} bytes in response on tls", n);
write!(ret, "received {} bytes in response on tls\n", n).ok();
}
Err(e) => {
log::warn!("failed to read tls response: {e}");
write!(ret, "{e}\n").ok();
}
};
log::info!( log::info!(
"{}", "{}",
std::str::from_utf8(&plaintext).unwrap_or("utf-error") std::str::from_utf8(&plaintext).unwrap_or("utf-error")
); );
log::set_max_level(log::LevelFilter::Info); log::set_max_level(log::LevelFilter::Info);
} }
// list trusted Certificate Authority certificates // list trusted Certificate Authority certificates