From 83de1117ff26a9d4a5655f8d91b8ffcc83c6deb0 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Thu, 13 Jun 2019 23:32:45 -0700 Subject: [PATCH] add tls mux client The latency is not great, and there are more authentication and authorization features that need to be added, but this shows that we can connect and talk to a remote muxer. --- src/config.rs | 30 ++++++++++++++++++++++++ src/main.rs | 5 ++++ src/server/client.rs | 52 +++++++++++++++++++++++++++++++++++++++++- src/server/listener.rs | 1 + 4 files changed, 87 insertions(+), 1 deletion(-) diff --git a/src/config.rs b/src/config.rs index 364d699c7..5537fc8ff 100644 --- a/src/config.rs +++ b/src/config.rs @@ -97,6 +97,31 @@ pub struct Config { /// the path to an x509 PEM encoded CA chain file pub mux_server_pem_ca: Option, + /// When using the mux client domain, identifies the host:port + /// pair of the remote server. + pub mux_server_remote_address: Option, + + /// When using the mux client domain: + /// the path to an x509 PEM encoded private key file + pub mux_client_pem_private_key: Option, + + /// When using the mux client domain: + /// the path to an x509 PEM encoded certificate file + pub mux_client_pem_cert: Option, + + /// When using the mux client domain: + /// the path to an x509 PEM encoded CA chain file + pub mux_client_pem_ca: Option, + + /// When using the mux client domain, explicitly control whether + /// the client checks that the certificate presented by the + /// server matches the hostname portion of mux_server_remote_address. + /// The default is true. + /// This option is made available for troubleshooting purposes and + /// should not be used outside of a controlled environment as it + /// weakens the security of the TLS channel. + pub mux_client_accept_invalid_hostnames: Option, + #[serde(default)] pub keys: Vec, } @@ -354,6 +379,11 @@ impl Default for Config { mux_server_pem_private_key: None, mux_server_pem_cert: None, mux_server_pem_ca: None, + mux_server_remote_address: None, + mux_client_pem_private_key: None, + mux_client_pem_cert: None, + mux_client_pem_ca: None, + mux_client_accept_invalid_hostnames: None, keys: vec![], } } diff --git a/src/main.rs b/src/main.rs index a00c9bc49..5680defaa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -96,6 +96,8 @@ struct StartCommand { /// default domain for new tabs and windows #[structopt(long = "mux-client-as-default-domain")] mux_client_as_default_domain: bool, + #[structopt(long = "mux-tls-client-as-default-domain")] + mux_tls_client_as_default_domain: bool, /// Instead of executing your shell, run PROG. /// For example: `wezterm start -- bash -l` will spawn bash @@ -146,6 +148,9 @@ fn run_terminal_gui(config: Arc, opts: &StartCommand) -> Result< let domain: Arc = if opts.mux_client_as_default_domain { let client = Client::new_unix_domain(&config)?; Arc::new(ClientDomain::new(client)) + } else if opts.mux_tls_client_as_default_domain { + let client = Client::new_tls(&config)?; + Arc::new(ClientDomain::new(client)) } else { Arc::new(LocalDomain::new(&config)?) }; diff --git a/src/server/client.rs b/src/server/client.rs index db0f7bf07..df29e59f7 100644 --- a/src/server/client.rs +++ b/src/server/client.rs @@ -1,14 +1,19 @@ #![allow(dead_code)] use crate::config::Config; use crate::server::codec::*; +use crate::server::listener::IdentitySource; use crate::server::UnixStream; -use failure::{bail, ensure, err_msg, Error, Fallible}; +use failure::{bail, ensure, err_msg, format_err, Error, Fallible}; use log::info; +use native_tls::TlsConnector; +use std::convert::TryInto; +use std::net::TcpStream; use std::path::Path; use std::sync::Arc; pub trait ReadAndWrite: std::io::Read + std::io::Write {} impl ReadAndWrite for UnixStream {} +impl ReadAndWrite for native_tls::TlsStream {} pub struct Client { stream: Box, @@ -53,6 +58,51 @@ impl Client { Ok(Self { stream, serial: 0 }) } + pub fn new_tls(config: &Arc) -> Fallible { + let remote_address = config + .mux_server_remote_address + .as_ref() + .ok_or_else(|| err_msg("missing mux_server_remote_address config value"))?; + + let remote_host_name = remote_address.split(':').next().ok_or_else(|| { + format_err!( + "expected mux_server_remote_address to have the form 'host:port', but have {}", + remote_address + ) + })?; + + let identity = IdentitySource::PemFiles { + key: config + .mux_client_pem_private_key + .as_ref() + .ok_or_else(|| err_msg("missing mux_client_pem_private_key config value"))? + .into(), + cert: config.mux_client_pem_cert.clone(), + chain: config.mux_client_pem_ca.clone(), + }; + + let connector = TlsConnector::builder() + .identity(identity.try_into()?) + .danger_accept_invalid_hostnames( + config.mux_client_accept_invalid_hostnames.unwrap_or(false), + ) + .build()?; + + let stream = TcpStream::connect(remote_address) + .map_err(|e| format_err!("connecting to {}: {}", remote_address, e))?; + + let stream = Box::new(connector.connect(remote_host_name, stream).map_err(|e| { + format_err!( + "TlsConnector for {} with host name {}: {} ({:?})", + remote_address, + remote_host_name, + e, + e + ) + })?); + Ok(Self { stream, serial: 0 }) + } + pub fn send_pdu(&mut self, pdu: Pdu) -> Result { let serial = self.serial; self.serial += 1; diff --git a/src/server/listener.rs b/src/server/listener.rs index b8737c978..577e8be10 100644 --- a/src/server/listener.rs +++ b/src/server/listener.rs @@ -48,6 +48,7 @@ impl LocalListener { } } +#[derive(Debug)] pub enum IdentitySource { Pkcs12File { path: PathBuf,