From a05ab47d10a640a5ce0cac3aeaecd2a06cd870ae Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 13 Aug 2022 21:23:35 +0800 Subject: [PATCH] add some test code to exercise TLS to break the linking --- Cargo.lock | 9 +++-- services/shellchat/Cargo.toml | 1 + services/shellchat/src/cmds/net_cmd.rs | 48 +++++++++++++++++++++++++- 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 856994a0f..e01d674ac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2821,10 +2821,14 @@ dependencies = [ "cc", "libc", "once_cell", + "rkyv", "spin 0.5.2", "untrusted", "web-sys", "winapi", + "xous", + "xous-ipc 0.9.7 (registry+https://github.com/rust-lang/crates.io-index)", + "xous-names", ] [[package]] @@ -3256,6 +3260,7 @@ dependencies = [ "tts-frontend", "usb-device-xous", "utralib", + "webpki-roots", "x25519-dalek", "xous", "xous-ipc 0.9.7", @@ -4276,9 +4281,9 @@ dependencies = [ [[package]] name = "webpki-roots" -version = "0.22.3" +version = "0.22.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44d8de8415c823c8abd270ad483c6feeac771fad964890779f9a8cb24fbbc1bf" +checksum = "f1c760f0d366a6c24a02ed7816e23e691f5d92291f94d15e836006fd11b04daf" dependencies = [ "webpki 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/services/shellchat/Cargo.toml b/services/shellchat/Cargo.toml index 50e040408..50408f1a4 100644 --- a/services/shellchat/Cargo.toml +++ b/services/shellchat/Cargo.toml @@ -75,6 +75,7 @@ tracing-subscriber = {version = "0.3.11", optional = true} # for rustls testing #rustls = "0.20.6" rustls = {path="../../../rustls/rustls"} +webpki-roots = "0.22.4" # for the curve25519 tests # hardware acceleration adaptations are inserted into a fork of the main branch. diff --git a/services/shellchat/src/cmds/net_cmd.rs b/services/shellchat/src/cmds/net_cmd.rs index bc089534f..de02d814f 100644 --- a/services/shellchat/src/cmds/net_cmd.rs +++ b/services/shellchat/src/cmds/net_cmd.rs @@ -18,6 +18,7 @@ use dns::Dns; // necessary to work around https://github.com/rust-lang/rust/issu use std::str::FromStr; #[cfg(feature="ditherpunk")] use gam::DecodePng; +use std::convert::TryInto; pub struct NetCmd { callback_id: Option, @@ -385,7 +386,52 @@ impl<'a> ShellCmdApi<'a> for NetCmd { } } "tls" => { - write!(ret, "Work in progress. Please check back later!").unwrap(); + let mut root_store = rustls::RootCertStore::empty(); + 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, + ) + }) + ); + let config = rustls::ClientConfig::builder() + .with_safe_defaults() + .with_root_certificates(root_store) + .with_no_client_auth(); + + let server_name = "bunniefoo.com".try_into().unwrap(); + let mut conn = rustls::ClientConnection::new(Arc::new(config), server_name).unwrap(); + + let mut sock = TcpStream::connect("google.com:443").unwrap(); + let mut tls = rustls::Stream::new(&mut conn, &mut sock); + tls.write_all( + concat!( + "GET / HTTP/1.1\r\n", + "Host: bunniefoo.com\r\n", + "Connection: close\r\n", + "Accept-Encoding: identity\r\n", + "\r\n" + ) + .as_bytes(), + ) + .unwrap(); + let ciphersuite = tls + .conn + .negotiated_cipher_suite() + .unwrap(); + log::info!( + "Current ciphersuite: {:?}", + ciphersuite.suite() + ); + let mut plaintext = Vec::new(); + tls.read_to_end(&mut plaintext).unwrap(); + log::info!("len: {}", plaintext.len()); + log::info!("{}", std::str::from_utf8(&plaintext).unwrap_or("utf-error")); } #[cfg(any(target_os = "none", target_os = "xous"))] "ping" => {