From 512690fce5643cc104da05227ced58e0250799da Mon Sep 17 00:00:00 2001 From: Jun Kurihara Date: Fri, 14 Oct 2022 22:45:13 +0900 Subject: [PATCH] changed how to support multiple domains and support client authentication directly by rustls using split server_config --- README.md | 31 ++++++- TODO.md | 4 +- example-certs/client_pass=foobar.p12 | Bin 1648 -> 0 bytes example-certs/client_pass=foobar.pfx | Bin 0 -> 1510 bytes src/backend/mod.rs | 121 ++++++++++++++++----------- src/error.rs | 1 + src/handler/handler_main.rs | 43 +++++----- src/proxy/proxy_client_cert.rs | 1 + src/proxy/proxy_h3.rs | 100 ++++++++++------------ src/proxy/proxy_main.rs | 6 +- src/proxy/proxy_tls.rs | 95 ++++++++++----------- 11 files changed, 218 insertions(+), 184 deletions(-) delete mode 100644 example-certs/client_pass=foobar.p12 create mode 100644 example-certs/client_pass=foobar.pfx diff --git a/README.md b/README.md index a986892..09991d5 100644 --- a/README.md +++ b/README.md @@ -226,6 +226,33 @@ Other than them, all you need is to mount your `config.toml` as `/etc/rpxy.toml` [`./bench`](./bench/) directory could be a very simple example of configuration of `rpxy`. This can also be an example of an example of docker use case. +## Experimental Features and Caveats + +### HTTP/3 + +`rpxy` can serves HTTP/3 requests thanks to `quinn` and `hyperium/h3`. To enable this experimental feature, add an entry `experimental.h3` in your `config.toml` like follows. Any values in the entry like `alt_svc_max_age` are optional. + +```toml +[experimental.h3] +alt_svc_max_age = 3600 +request_max_body_size = 65536 +max_concurrent_connections = 10000 +max_concurrent_bidistream = 100 +max_concurrent_unistream = 100 +``` + +### Client Authentication via Client Certificates + +Client authentication is enabled when `apps."app_name".tls.client_ca_cert_path` is set for the domain specified by `"app_name"` like + +```toml +[apps.localhost] +server_name = 'localhost' # Domain name +tls = { https_redirection = true, tls_cert_path = './server.crt', tls_cert_key_path = './server.key', client_ca_cert_path = './client_cert.ca.crt' } +``` + + However, currently we have a limitation on HTTP/3 support for applications that enables client authentication. If an application is set with client authentication, HTTP/3 doesn't work for the application. + ## TIPS ### Using Private Key Issued by Let's Encrypt @@ -284,12 +311,14 @@ First, you need to prepare a CA certificate used to verify client certificate. I % openssl x509 -req -days 365 -sha256 -in client.csr -CA client.ca.crt -CAkey client.ca.key -CAcreateserial -out client.crt -extfile client.ext ``` - Now you have a client key `client.key` and certificate `client.crt` (version 3). `p12` file can be retrieved as + Now you have a client key `client.key` and certificate `client.crt` (version 3). `pfx` (`p12`) file can be retrieved as ```bash % openssl pkcs12 -export -inkey client.key -in client.crt -certfile client.ca.crt -out client.pfx ``` + Note that on MacOS, a `pfx` generated by `OpenSSL 3.0.6` cannot be imported to MacOS KeyChain Access. We generated the sample `pfx` using `LibreSSL 2.8.3` instead `OpenSSL`. + All of sample certificate files are found in `./example-certs/` directory. ### (Work Around) Deployment on Ubuntu 22.04LTS using docker behind `ufw` diff --git a/TODO.md b/TODO.md index 2a10f18..dcbd9ee 100644 --- a/TODO.md +++ b/TODO.md @@ -9,5 +9,7 @@ - Documentation - Client certificate - support intermediate certificate. Currently, only supports client certificates directly signed by root CA. - - split rustls::server::ServerConfig for SNIs + - Currently, we took the following approach (caveats) + - For Http2 and 1.1, prepare `rustls::ServerConfig` for each domain name and hence client CA cert is set for each one. + - For Http3, use aggregated `rustls::ServerConfig` for multiple domain names except for ones requiring client-auth. So, if a domain name is set with client authentication, http3 doesn't work for the domain. - etc. diff --git a/example-certs/client_pass=foobar.p12 b/example-certs/client_pass=foobar.p12 deleted file mode 100644 index 37a3a4ea33f0a17602f3595da00cbe586b60c7f8..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1648 zcmai!dpOgJAIJCIzWZ)+TUgqYgqT~_SAH%dzh5YuvaDF5lU$2&$|IpLEV(x2*F`iA z&O}9WXkotP8470(fKPsr04c4wl)kpxbE)_o-0QUZF(5mo$*z|{0 zfptJ^?g{0Y=ZdSJkVq^7R)Mg8eM2gt5%5+>$v=$>grGpc48&P0X%r{7gw^I48e(JL zb_$c(;2!t9-DS%cHQegS24nRr?y)rF(=)JS`Rv2mO4rRQIXglJ{`;u5+A58X*j`e3 z=A-N2dHv)NR0lJE;DB0P3Zwn4F?+USs3f2UXOfk5ALU}Pme|=|K*`W@_Bm^IUA=W^ zV1g8W*);5PxYYG!%wgmEb>wDKX}YJ=b@NrUzR&)VW|u&uMTEz-%OPC}dieHV(`$Pq zFATRe9e}+|QfIpli{y48q5)}@;SE<#c^t0I_?gCmoxJ6TFEUHDeV>ViV8&Riq%m8( zW~5!|qUM6%+G;$}n7@*Di%YLG0xXfi--z+9YbWE5L7nlZYBron^7N9i&@H^7ifHR+ z0!4*#uAM9vhjDBsFF`kruMt*s(1F5_>hisewgx!~SvupXoS zC9Cq=sM*%{2FrxRagDlg!BkHP&c-X7YPXg0AORX(bh8aO@;GjhG^Z(klYP)puW#Gr zoR;-rqtG%z6mPRqs$Sl{$V}gj0Fv|SeOrjL-ES{sEy%C#E2~fAFufxHeT_=DNfZW8 zR&S>GrQF)AEe?{17achtBC#L6@@T6JdxPP_bbemD2+F;8LRF&JY0|HJIz!Do`i~%= zC=LN}_K%bGrdxuAMU2tuCbOxi#GyN~JfW@qiT`{EjGWiPDpY5P+w|DnLpAMb%)MVG z{Dsa1L-ko(aF>!oH<&(BTkPW(xaH!ze{pWP$ZS&f{&Y(*$Q3CzS>f3-H3DUn!>eiC zBU)k)nor|F;9PuU)l39{@W>^ly)d-GJHdnv9?tH$@r z)ow=2d-nFKED}#)lsN8sWqFGoZ=+iBCsMjz1Bo>eW*0E;|9YQW+XD6W*u8$w{>DJ` z%glM*R0Sbsqm%$;=0z@8rJ**|YRmDz>5EbRD(Z}eC)lEcUx88XAfDxuv7gRIWnIu* zfSNJo=B0%;tclOAtwb9xY9u5|2O2&n!T9$ga}Y&mKzTxSx-IfX0Lb(xgxP@8cOLx- zIA9mZ21x6%+4>P7<^Qu!WdndLPML#Kq<@Dq4W}(WG@2~uZ*jKs%3A7kpJJ!%1`}Nd z;U7I<%2iFqjuRp>!=27+Uc9iW>q)SEIj1`zunT4W>f~K%yd2Y~eCkX8^ugz2WeMN3 zq62dSoaaBgWxfy{9Y<*j_lzV@oezt#FFsO2tS zTqme4FWpAH7qt+%zt#?`TlW7N%c-H#ZWfWWew_D<*PSkL{H5+NPYp>22enR!87A>; zF!DQrA{2x>5Dmc*A z@tVLW9$}^5r*rEXxcO^Gl|RMQ>!%{!Y1ZJF<9n}eo!gZeS2n@L)bzEN?o-?Ei2+k1d%y7a#a5CB*VP?wFZ1GX}Ie13{+Ub+hij$G*PQ-bDCs` zA7o&J8OZ?9FoFRB1_>&LNQU{A9Sm;zL%veF1NuNs0EeZ*RH~Dp z@ntl)GLxyQPz7Rt=&23=!}ZJ55#q?pT(t9@EodiRfBh8&aIw@a#v=V{MBUl*h z2{_t?)ep}UzfQ<$xmNCfgnOKp;@T52B`_lf2`Yw2hW8Bt2^BFG1QgJ&Nf4$mNXGYJ zzN{G)aBS-FV*@ZTFd;Ar1_dh)0|FWa00b0ulXQ;o!!b<%#Rj0UXF|YK8hwid2>qe9 M!o5b{?*ak{07rbV;s5{u literal 0 HcmV?d00001 diff --git a/src/backend/mod.rs b/src/backend/mod.rs index bc701e0..c6a2842 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -6,7 +6,7 @@ use crate::{ utils::{BytesName, PathNameBytesExp, ServerNameBytesExp}, }; use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet}; -use rustls::OwnedTrustAnchor; +use rustls::{OwnedTrustAnchor, RootCertStore}; use std::{ fs::File, io::{self, BufReader, Cursor, Read}, @@ -146,6 +146,7 @@ impl Backend { }) .collect(); + // TODO: SKID is not used currently let subject_key_identifiers: HashSet<_> = certs .iter() .filter_map(|v| { @@ -176,82 +177,104 @@ pub struct Backends { pub default_server_name_bytes: Option, // for plaintext http } -pub type SniKeyIdsMap = HashMap>>; +pub type SniServerCryptoMap = HashMap>; pub struct ServerCrypto { - pub inner: Arc, - pub server_name_client_ca_keyids_map: Arc, + // For Quic/HTTP3, only servers with no client authentication + pub inner_global_no_client_auth: Arc, + // For TLS over TCP/HTTP2 and 1.1, map of SNI to server_crypto for all given servers + pub inner_local_map: Arc, } impl Backends { - pub async fn generate_server_crypto_with_cert_resolver(&self) -> Result { - let mut resolver = ResolvesServerCertUsingSni::new(); - let mut client_ca_roots = rustls::RootCertStore::empty(); - let mut client_ca_key_ids: SniKeyIdsMap = HashMap::default(); + pub async fn generate_server_crypto(&self) -> Result { + let mut resolver_global = ResolvesServerCertUsingSni::new(); + let mut server_crypto_local_map: SniServerCryptoMap = HashMap::default(); - // let mut cnt = 0; for (server_name_bytes_exp, backend) in self.apps.iter() { if backend.tls_cert_key_path.is_some() && backend.tls_cert_path.is_some() { match backend.read_certs_and_key() { Ok(certified_key) => { - if let Err(e) = resolver.add(backend.server_name.as_str(), certified_key) { + let mut resolver_local = ResolvesServerCertUsingSni::new(); + let mut client_ca_roots_local = RootCertStore::empty(); + + // add server certificate and key + if let Err(e) = resolver_local.add(backend.server_name.as_str(), certified_key.to_owned()) { error!( "{}: Failed to read some certificates and keys {}", backend.server_name.as_str(), e ) - } else { - // debug!("Add certificate for server_name: {}", backend.server_name.as_str()); - // cnt += 1; } + + if backend.client_ca_cert_path.is_none() { + // aggregated server config for no client auth server for http3 + if let Err(e) = resolver_global.add(backend.server_name.as_str(), certified_key) { + error!( + "{}: Failed to read some certificates and keys {}", + backend.server_name.as_str(), + e + ) + } + } else { + // add client certificate if specified + match backend.read_client_ca_certs() { + Ok((owned_trust_anchors, _subject_key_ids)) => { + client_ca_roots_local.add_server_trust_anchors(owned_trust_anchors.into_iter()); + } + Err(e) => { + warn!( + "Failed to add client CA certificate for {}: {}", + backend.server_name.as_str(), + e + ); + } + } + } + + let mut server_config_local = if client_ca_roots_local.is_empty() { + // with no client auth, enable http1.1 -- 3 + let mut sc = ServerConfig::builder() + .with_safe_defaults() + .with_no_client_auth() + .with_cert_resolver(Arc::new(resolver_local)); + #[cfg(feature = "http3")] + { + sc.alpn_protocols = vec![b"h3".to_vec(), b"hq-29".to_vec()]; // TODO: remove hq-29 later? + } + sc + } else { + // with client auth, enable only http1.1 and 2 + // let client_certs_verifier = rustls::server::AllowAnyAnonymousOrAuthenticatedClient::new(client_ca_roots); + let client_certs_verifier = rustls::server::AllowAnyAuthenticatedClient::new(client_ca_roots_local); + ServerConfig::builder() + .with_safe_defaults() + .with_client_cert_verifier(client_certs_verifier) + .with_cert_resolver(Arc::new(resolver_local)) + }; + server_config_local.alpn_protocols.push(b"h2".to_vec()); + server_config_local.alpn_protocols.push(b"http/1.1".to_vec()); + + server_crypto_local_map.insert(server_name_bytes_exp.to_owned(), Arc::new(server_config_local)); } Err(e) => { warn!("Failed to add certificate for {}: {}", backend.server_name.as_str(), e); } } - // add client certificate if specified - if backend.client_ca_cert_path.is_some() { - match backend.read_client_ca_certs() { - Ok((owned_trust_anchors, subject_key_ids)) => { - // TODO: ここでSubject Key ID (CA Key ID)を記録しておく。認証後にpeer certificateのauthority key idとの一貫性をチェック。 - // v3 x509前提で特定のkey id extが入ってなければ使えない前提 - client_ca_roots.add_server_trust_anchors(owned_trust_anchors.into_iter()); - client_ca_key_ids.insert(server_name_bytes_exp.to_owned(), subject_key_ids); - } - Err(e) => { - warn!( - "Failed to add client ca certificate for {}: {}", - backend.server_name.as_str(), - e - ); - } - } - } } } // debug!("Load certificate chain for {} server_name's", cnt); ////////////// - let mut server_config = if client_ca_key_ids.is_empty() { - ServerConfig::builder() - .with_safe_defaults() - .with_no_client_auth() - .with_cert_resolver(Arc::new(resolver)) - } else { - // TODO: Client Certs - // No ClientCert or WithClientCert - // let client_certs_verifier = rustls::server::AllowAnyAuthenticatedClient::new(client_ca_roots); - let client_certs_verifier = rustls::server::AllowAnyAnonymousOrAuthenticatedClient::new(client_ca_roots); - ServerConfig::builder() - .with_safe_defaults() - .with_client_cert_verifier(client_certs_verifier) - .with_cert_resolver(Arc::new(resolver)) - }; + let mut server_crypto_global = ServerConfig::builder() + .with_safe_defaults() + .with_no_client_auth() + .with_cert_resolver(Arc::new(resolver_global)); ////////////////////////////// #[cfg(feature = "http3")] { - server_config.alpn_protocols = vec![ + server_crypto_global.alpn_protocols = vec![ b"h3".to_vec(), b"hq-29".to_vec(), // TODO: remove later? b"h2".to_vec(), @@ -264,8 +287,8 @@ impl Backends { } Ok(ServerCrypto { - inner: Arc::new(server_config), - server_name_client_ca_keyids_map: Arc::new(client_ca_key_ids), + inner_global_no_client_auth: Arc::new(server_crypto_global), + inner_local_map: Arc::new(server_crypto_local_map), }) } } diff --git a/src/error.rs b/src/error.rs index 7a39c9e..aa679f8 100644 --- a/src/error.rs +++ b/src/error.rs @@ -44,6 +44,7 @@ pub enum RpxyError { Other(#[from] anyhow::Error), } +#[allow(dead_code)] #[derive(Debug, Error, Clone)] pub enum ClientCertsError { #[error("TLS Client Certificate is Required for Given SNI: {0}")] diff --git a/src/handler/handler_main.rs b/src/handler/handler_main.rs index 251f898..fbc5161 100644 --- a/src/handler/handler_main.rs +++ b/src/handler/handler_main.rs @@ -1,6 +1,12 @@ // Highly motivated by https://github.com/felipenoris/hyper-reverse-proxy use super::{utils_headers::*, utils_request::*, utils_synth_response::*}; -use crate::{backend::UpstreamGroup, error::*, globals::Globals, log::*, utils::ServerNameBytesExp}; +use crate::{ + backend::{Backend, UpstreamGroup}, + error::*, + globals::Globals, + log::*, + utils::ServerNameBytesExp, +}; use hyper::{ client::connect::Connect, header::{self, HeaderValue}, @@ -35,26 +41,11 @@ where listen_addr: SocketAddr, tls_enabled: bool, tls_server_name: Option, - tls_client_auth_result: Option>, ) -> Result> { //////// let mut log_data = MessageLog::from(&req); log_data.client_addr(&client_addr); ////// - // First check client auth result if exist - if let Some(res) = tls_client_auth_result { - match res { - Err(ClientCertsError::ClientCertRequired(_)) => { - // Client cert is required for the TLS server name - return self.return_with_error_log(StatusCode::FORBIDDEN, &mut log_data); - } - Err(ClientCertsError::InconsistentClientCert(_)) => { - // Client cert provided was inconsistent to the TLS server name - return self.return_with_error_log(StatusCode::BAD_REQUEST, &mut log_data); - } - _ => (), - } - } // Here we start to handle with server_name let server_name = if let Ok(v) = req.parse_host() { @@ -133,7 +124,7 @@ where if res_backend.status() != StatusCode::SWITCHING_PROTOCOLS { // Generate response to client - if self.generate_response_forwarded(&mut res_backend).is_ok() { + if self.generate_response_forwarded(&mut res_backend, backend).is_ok() { log_data.status_code(&res_backend.status()).output(); return Ok(res_backend); } else { @@ -191,7 +182,11 @@ where //////////////////////////////////////////////////// // Functions to generate messages - fn generate_response_forwarded(&self, response: &mut Response) -> Result<()> { + fn generate_response_forwarded( + &self, + response: &mut Response, + chosen_backend: &Backend, + ) -> Result<()> { let headers = response.headers_mut(); remove_connection_header(headers); remove_hop_header(headers); @@ -199,7 +194,8 @@ where #[cfg(feature = "http3")] { - if self.globals.http3 { + // TODO: Workaround for avoid h3 for client authentication + if self.globals.http3 && chosen_backend.client_ca_cert_path.is_none() { if let Some(port) = self.globals.https_port { add_header_entry_overwrite_if_exist( headers, @@ -210,6 +206,15 @@ where ), )?; } + } else { + // remove alt-svc to disallow requests via http3 + headers.remove(header::ALT_SVC.as_str()); + } + } + #[cfg(not(feature = "http3"))] + { + if let Some(port) = self.globals.https_port { + headers.remove(header::ALT_SVC.as_str()); } } diff --git a/src/proxy/proxy_client_cert.rs b/src/proxy/proxy_client_cert.rs index aa212c1..adac4b7 100644 --- a/src/proxy/proxy_client_cert.rs +++ b/src/proxy/proxy_client_cert.rs @@ -4,6 +4,7 @@ use rustls::Certificate; use x509_parser::extensions::ParsedExtension; use x509_parser::prelude::*; +#[allow(dead_code)] // TODO: consider move this function to the layer of handle_request (L7) to return 403 pub(super) fn check_client_authentication( client_certs: Option<&[Certificate]>, diff --git a/src/proxy/proxy_h3.rs b/src/proxy/proxy_h3.rs index d0aaf5d..d5a6c88 100644 --- a/src/proxy/proxy_h3.rs +++ b/src/proxy/proxy_h3.rs @@ -1,9 +1,9 @@ -use super::{proxy_client_cert::check_client_authentication, Proxy}; -use crate::{backend::SniKeyIdsMap, error::*, log::*, utils::ServerNameBytesExp}; +use super::Proxy; +use crate::{error::*, log::*, utils::ServerNameBytesExp}; use bytes::{Buf, Bytes}; use h3::{quic::BidiStream, server::RequestStream}; use hyper::{client::connect::Connect, Body, Request, Response}; -use std::{net::SocketAddr, sync::Arc}; +use std::net::SocketAddr; use tokio::time::{timeout, Duration}; impl Proxy @@ -14,28 +14,11 @@ where self, conn: quinn::Connecting, tls_server_name: ServerNameBytesExp, - sni_cc_map: Arc, ) -> Result<()> { let client_addr = conn.remote_address(); match conn.await { Ok(new_conn) => { - // Check client certificates - let cc = { - // https://docs.rs/quinn/latest/quinn/struct.Connection.html - let client_certs_setting_for_sni = sni_cc_map.get(&tls_server_name); - let client_certs = match new_conn.connection.peer_identity() { - Some(peer_identity) => peer_identity - .downcast::>() - .ok() - .map(|p| p.into_iter().collect::>()), - None => None, - }; - (client_certs, client_certs_setting_for_sni) - }; - // TODO: pass this value to the layer of handle_request (L7) to return 403 - let tls_client_auth_result = check_client_authentication(cc.0.as_ref().map(AsRef::as_ref), cc.1); - let mut h3_conn = h3::server::Connection::<_, bytes::Bytes>::new(h3_quinn::Connection::new(new_conn)).await?; info!( "QUIC/HTTP3 connection established from {:?} {:?}", @@ -43,43 +26,46 @@ where ); // TODO: Is here enough to fetch server_name from NewConnection? // to avoid deep nested call from listener_service_h3 - while let Some((req, stream)) = match h3_conn.accept().await { - Ok(opt_req) => opt_req, - Err(e) => { - warn!("HTTP/3 failed to accept incoming connection: {}", e); - return Ok(h3_conn.shutdown(0).await?); - } - } { - // We consider the connection count separately from the stream count. - // Max clients for h1/h2 = max 'stream' for h3. - let request_count = self.globals.request_count.clone(); - if request_count.increment() > self.globals.max_clients { - request_count.decrement(); - return Ok(h3_conn.shutdown(0).await?); - } - debug!("Request incoming: current # {}", request_count.current()); - - let self_inner = self.clone(); - let tls_server_name_inner = tls_server_name.clone(); - let tls_client_auth_result_inner = tls_client_auth_result.clone(); - self.globals.runtime_handle.spawn(async move { - if let Err(e) = timeout( - self_inner.globals.proxy_timeout + Duration::from_secs(1), // timeout per stream are considered as same as one in http2 - self_inner.stream_serve_h3( - req, - stream, - client_addr, - tls_server_name_inner, - tls_client_auth_result_inner, - ), - ) - .await - { - error!("HTTP/3 failed to process stream: {}", e); + loop { + // this routine follows hyperium/h3 examples https://github.com/hyperium/h3/blob/master/examples/server.rs + match h3_conn.accept().await { + Ok(None) => { + break; } - request_count.decrement(); - debug!("Request processed: current # {}", request_count.current()); - }); + Err(e) => { + warn!("HTTP/3 error on accept incoming connection: {}", e); + match e.get_error_level() { + h3::error::ErrorLevel::ConnectionError => break, + h3::error::ErrorLevel::StreamError => continue, + } + } + Ok(Some((req, stream))) => { + // We consider the connection count separately from the stream count. + // Max clients for h1/h2 = max 'stream' for h3. + let request_count = self.globals.request_count.clone(); + if request_count.increment() > self.globals.max_clients { + request_count.decrement(); + h3_conn.shutdown(0).await?; + break; + } + debug!("Request incoming: current # {}", request_count.current()); + + let self_inner = self.clone(); + let tls_server_name_inner = tls_server_name.clone(); + self.globals.runtime_handle.spawn(async move { + if let Err(e) = timeout( + self_inner.globals.proxy_timeout + Duration::from_secs(1), // timeout per stream are considered as same as one in http2 + self_inner.stream_serve_h3(req, stream, client_addr, tls_server_name_inner), + ) + .await + { + error!("HTTP/3 failed to process stream: {}", e); + } + request_count.decrement(); + debug!("Request processed: current # {}", request_count.current()); + }); + } + } } } Err(err) => { @@ -97,7 +83,6 @@ where stream: RequestStream, client_addr: SocketAddr, tls_server_name: ServerNameBytesExp, - tls_client_auth_result: std::result::Result<(), ClientCertsError>, ) -> Result<()> where S: BidiStream + Send + 'static, @@ -149,7 +134,6 @@ where self.listening_on, self.tls_enabled, Some(tls_server_name), - Some(tls_client_auth_result), ) .await?; diff --git a/src/proxy/proxy_main.rs b/src/proxy/proxy_main.rs index 8501902..964ad70 100644 --- a/src/proxy/proxy_main.rs +++ b/src/proxy/proxy_main.rs @@ -51,7 +51,6 @@ where server: Http, peer_addr: SocketAddr, tls_server_name: Option, - tls_client_auth_result: Option>, ) where I: AsyncRead + AsyncWrite + Send + Unpin + 'static, { @@ -75,7 +74,6 @@ where self.listening_on, self.tls_enabled, tls_server_name.clone(), - tls_client_auth_result.clone(), ) }), ) @@ -94,9 +92,7 @@ where let tcp_listener = TcpListener::bind(&self.listening_on).await?; info!("Start TCP proxy serving with HTTP request for configured host names"); while let Ok((stream, _client_addr)) = tcp_listener.accept().await { - self - .clone() - .client_serve(stream, server.clone(), _client_addr, None, None); + self.clone().client_serve(stream, server.clone(), _client_addr, None); } Ok(()) as Result<()> }; diff --git a/src/proxy/proxy_tls.rs b/src/proxy/proxy_tls.rs index b90c8a3..dcd7a58 100644 --- a/src/proxy/proxy_tls.rs +++ b/src/proxy/proxy_tls.rs @@ -1,9 +1,6 @@ -use super::{ - proxy_client_cert::check_client_authentication, - proxy_main::{LocalExecutor, Proxy}, -}; +use super::proxy_main::{LocalExecutor, Proxy}; use crate::{ - backend::{ServerCrypto, SniKeyIdsMap}, + backend::{ServerCrypto, SniServerCryptoMap}, constants::*, error::*, log::*, @@ -17,7 +14,6 @@ use tokio::{ sync::watch, time::{sleep, timeout, Duration}, }; -use tokio_rustls::TlsAcceptor; #[cfg(feature = "http3")] use futures::StreamExt; @@ -31,7 +27,7 @@ where async fn cert_service(&self, server_crypto_tx: watch::Sender>>) { info!("Start cert watch service"); loop { - if let Ok(server_crypto) = self.globals.backends.generate_server_crypto_with_cert_resolver().await { + if let Ok(server_crypto) = self.globals.backends.generate_server_crypto().await { if let Err(_e) = server_crypto_tx.send(Some(Arc::new(server_crypto))) { error!("Failed to populate server crypto"); break; @@ -52,61 +48,61 @@ where let tcp_listener = TcpListener::bind(&self.listening_on).await?; info!("Start TCP proxy serving with HTTPS request for configured host names"); - // let mut server_crypto: Option> = None; - let mut tls_acceptor: Option = None; - let mut sni_client_ca_keyid_map: Option> = None; + let mut server_crypto_map: Option> = None; loop { tokio::select! { tcp_cnx = tcp_listener.accept() => { - if tls_acceptor.is_none() || tcp_cnx.is_err() || sni_client_ca_keyid_map.is_none() { + if tcp_cnx.is_err() || server_crypto_map.is_none() { continue; } let (raw_stream, client_addr) = tcp_cnx.unwrap(); - let acceptor = tls_acceptor.clone().unwrap(); - let sni_cc_map = sni_client_ca_keyid_map.clone().unwrap(); + let sc_map_inner = server_crypto_map.clone(); let server_clone = server.clone(); let self_inner = self.clone(); // spawns async handshake to avoid blocking thread by sequential handshake. let handshake_fut = async move { - // timeout is introduced to avoid get stuck here. - let accepted = match timeout(Duration::from_secs(TLS_HANDSHAKE_TIMEOUT_SEC), acceptor.accept(raw_stream)).await { - Ok(a) => a, - Err(e) => { - return Err(RpxyError::Proxy(format!("Timeout to handshake TLS: {}", e))); - } - }; - let stream = match accepted { + let acceptor = tokio_rustls::LazyConfigAcceptor::new(rustls::server::Acceptor::new().unwrap(), raw_stream).await; + if let Err(e) = acceptor { + return Err(RpxyError::Proxy(format!("Failed to handshake TLS: {}", e))); + } + let start = acceptor.unwrap(); + let client_hello = start.client_hello(); + let server_name = client_hello.server_name(); + debug!("HTTP/2 or 1.1: SNI in ClientHello: {:?}", server_name); + let server_name = server_name.map_or_else(|| None, |v| Some(v.to_server_name_vec())); + if server_name.is_none(){ + return Err(RpxyError::Proxy("No SNI is given".to_string())); + } + let server_crypto = sc_map_inner.as_ref().unwrap().get(server_name.as_ref().unwrap()); + if server_crypto.is_none() { + return Err(RpxyError::Proxy(format!("No TLS serving app for {:?}", "xx"))); + } + let stream = match start.into_stream(server_crypto.unwrap().clone()).await { Ok(s) => s, Err(e) => { return Err(RpxyError::Proxy(format!("Failed to handshake TLS: {}", e))); } }; - // Retrieve SNI - let (_, conn) = stream.get_ref(); - let server_name = conn.sni_hostname(); - debug!("HTTP/2 or 1.1: SNI in ClientHello: {:?}", server_name); - let server_name = server_name.map_or_else(|| None, |v| Some(v.to_server_name_vec())); - if server_name.is_none(){ - Err(RpxyError::Proxy("No SNI is given".to_string())) - } else { - ////////////////////////////// - // Check client certificate - let client_certs = conn.peer_certificates(); - let client_ca_keyids_set_for_sni = sni_cc_map.get(&server_name.clone().unwrap()); - // TODO: pass this value to the layer of handle_request (L7) to return 403 - let client_certs_auth_result = check_client_authentication(client_certs, client_ca_keyids_set_for_sni); - ////////////////////////////// - // this immediately spawns another future to actually handle stream. so it is okay to introduce timeout for handshake. - // TODO: don't want to pass copied value... - self_inner.client_serve(stream, server_clone, client_addr, server_name, Some(client_certs_auth_result)); - Ok(()) - } + self_inner.client_serve(stream, server_clone, client_addr, server_name); + Ok(()) }; + self.globals.runtime_handle.spawn( async move { - if let Err(e) = handshake_fut.await { - error!("{}", e); - } + // timeout is introduced to avoid get stuck here. + match timeout( + Duration::from_secs(TLS_HANDSHAKE_TIMEOUT_SEC), + handshake_fut + ).await { + Ok(a) => { + if let Err(e) = a { + error!("{}", e); + } + }, + Err(e) => { + error!("Timeout to handshake TLS: {}", e); + } + }; }); } _ = server_crypto_rx.changed() => { @@ -114,8 +110,7 @@ where break; } let server_crypto = server_crypto_rx.borrow().clone().unwrap(); - tls_acceptor = Some(TlsAcceptor::from(server_crypto.inner.clone())); - sni_client_ca_keyid_map = Some(server_crypto.server_name_client_ca_keyids_map.clone()); + server_crypto_map = Some(server_crypto.inner_local_map.clone()); } else => break } @@ -143,11 +138,10 @@ where let (endpoint, mut incoming) = Endpoint::server(server_config_h3, self.listening_on)?; let mut server_crypto: Option> = None; - let mut sni_client_ca_keyid_map: Option> = None; loop { tokio::select! { new_conn = incoming.next() => { - if server_crypto.is_none() || new_conn.is_none() || sni_client_ca_keyid_map.is_none() { + if server_crypto.is_none() || new_conn.is_none() { continue; } let mut conn = new_conn.unwrap(); @@ -173,7 +167,7 @@ where ); // TODO: server_nameをここで出してどんどん深く投げていくのは効率が悪い。connecting -> connectionsの後でいいのでは? // TODO: 通常のTLSと同じenumか何かにまとめたい - let fut = self.clone().connection_serve_h3(conn, new_server_name, sni_client_ca_keyid_map.clone().unwrap()); + let fut = self.clone().connection_serve_h3(conn, new_server_name); self.globals.runtime_handle.spawn(async move { // Timeout is based on underlying quic if let Err(e) = fut.await { @@ -187,8 +181,7 @@ where } server_crypto = server_crypto_rx.borrow().clone(); if server_crypto.is_some(){ - endpoint.set_server_config(Some(QuicServerConfig::with_crypto(server_crypto.clone().unwrap().inner.clone()))); - sni_client_ca_keyid_map = Some(server_crypto.clone().unwrap().server_name_client_ca_keyids_map.clone()); + endpoint.set_server_config(Some(QuicServerConfig::with_crypto(server_crypto.clone().unwrap().inner_global_no_client_auth.clone()))); } } else => break