Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/conn/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,10 @@ impl Conn {
self.write_struct(&ssl_request).await?;
let conn = self;
let ssl_opts = conn.opts().ssl_opts().cloned().expect("unreachable");
let domain = conn.opts().ip_or_hostname().into();
let domain = ssl_opts
.tls_hostname_override()
.unwrap_or_else(|| conn.opts().ip_or_hostname())
.into();
conn.stream_mut()?.make_secure(domain, ssl_opts).await?;
Ok(())
} else {
Expand Down
3 changes: 1 addition & 2 deletions src/io/tls/rustls_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ impl ServerCertVerifier for DangerousVerifier {
) {
Ok(assertion) => Ok(assertion),
Err(ref e)
if e.to_string().contains("NotValidForName")
&& self.skip_domain_validation =>
if e.to_string().contains("NotValidForName") && self.skip_domain_validation =>
{
Ok(rustls::client::ServerCertVerified::assertion())
}
Expand Down
17 changes: 17 additions & 0 deletions src/opts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ pub struct SslOpts {
root_cert_path: Option<Cow<'static, Path>>,
skip_domain_validation: bool,
accept_invalid_certs: bool,
tls_hostname_override: Option<Cow<'static, str>>,
}

impl SslOpts {
Expand Down Expand Up @@ -178,6 +179,18 @@ impl SslOpts {
self
}

/// If set, will override the hostname used to verify the server's certificate.
///
/// This is useful when connecting to a server via a tunnel, where the server hostname
/// name is different from the hostname used to connect to the tunnel.
pub fn with_tls_hostname_override<T: Into<Cow<'static, str>>>(
mut self,
domain: Option<T>,
) -> Self {
self.tls_hostname_override = domain.map(Into::into);
self
}

#[cfg(any(feature = "native-tls", feature = "rustls-tls"))]
pub fn client_identity(&self) -> Option<&ClientIdentity> {
self.client_identity.as_ref()
Expand All @@ -194,6 +207,10 @@ impl SslOpts {
pub fn accept_invalid_certs(&self) -> bool {
self.accept_invalid_certs
}

pub fn tls_hostname_override(&self) -> Option<&str> {
self.tls_hostname_override.as_ref().map(AsRef::as_ref)
}
}

/// Connection pool options.
Expand Down