From 36fabdadfd76c99b812ffcd38afb119e16a5162b Mon Sep 17 00:00:00 2001 From: quininer kel Date: Fri, 21 Jul 2017 17:57:57 +0800 Subject: [PATCH] [Added] danger feature --- Cargo.toml | 3 +++ src/lib.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 7e438ba..9c31b71 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,9 @@ readme = "README.md" description = "Asynchronous TLS/SSL streams for Tokio using Rustls." categories = ["asynchronous", "cryptography", "network-programming"] +[features] +danger = [ "rustls/dangerous_configuration" ] + [dependencies] futures = "0.1" tokio-io = "0.1" diff --git a/src/lib.rs b/src/lib.rs index d4d1aa5..1c1aa11 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,6 +22,11 @@ pub trait ClientConfigExt { fn connect_async(&self, domain: &str, stream: S) -> ConnectAsync where S: AsyncRead + AsyncWrite; + + #[cfg(feature = "danger")] + fn danger_connect_async_without_providing_domain_for_certificate_verification_and_server_name_indication(&self, stream: S) + -> ConnectAsync + where S: AsyncRead + AsyncWrite; } /// Extension trait for the `Arc` type in the `rustls` crate. @@ -48,6 +53,30 @@ impl ClientConfigExt for Arc { { connect_async_with_session(stream, ClientSession::new(self, domain)) } + + #[cfg(feature = "danger")] + fn danger_connect_async_without_providing_domain_for_certificate_verification_and_server_name_indication(&self, stream: S) + -> ConnectAsync + where S: AsyncRead + AsyncWrite + { + use rustls::{ ServerCertVerifier, RootCertStore, Certificate, TLSError }; + + struct NoCertVerifier; + impl ServerCertVerifier for NoCertVerifier { + fn verify_server_cert(&self, _: &RootCertStore, _: &[Certificate], _: &str) + -> Result<(), TLSError> + { + Ok(()) + } + } + + let mut client_config = ClientConfig::new(); + client_config.clone_from(self); + client_config.dangerous() + .set_certificate_verifier(Box::new(NoCertVerifier)); + + Arc::new(client_config).connect_async("", stream) + } } #[inline]