Skip to content

Commit 1f3eb7a

Browse files
authored
Restrict dangerous path to no hostname verification (#65)
Normal path should not suffer this dangerous.
1 parent d858d0b commit 1f3eb7a

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

src/tls.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,28 +40,23 @@ impl Default for TlsOptions {
4040
// * Allow disabling Hostname Verification: https://github.com/rustls/rustls/issues/578
4141
// * Dangerous verifiers API proposal: https://github.com/rustls/rustls/pull/1197
4242
#[derive(Debug)]
43-
struct TlsServerCertVerifier {
43+
struct NoHostnameVerificationServerCertVerifier {
4444
roots: RootCertStore,
4545
supported: WebPkiSupportedAlgorithms,
46-
hostname_verification: bool,
4746
}
4847

49-
impl TlsServerCertVerifier {
50-
fn new(roots: RootCertStore, hostname_verification: bool) -> Self {
51-
Self {
52-
roots,
53-
supported: CryptoProvider::get_default().unwrap().signature_verification_algorithms,
54-
hostname_verification,
55-
}
48+
impl NoHostnameVerificationServerCertVerifier {
49+
unsafe fn new(roots: RootCertStore) -> Self {
50+
Self { roots, supported: CryptoProvider::get_default().unwrap().signature_verification_algorithms }
5651
}
5752
}
5853

59-
impl ServerCertVerifier for TlsServerCertVerifier {
54+
impl ServerCertVerifier for NoHostnameVerificationServerCertVerifier {
6055
fn verify_server_cert(
6156
&self,
6257
end_entity: &CertificateDer<'_>,
6358
intermediates: &[CertificateDer<'_>],
64-
server_name: &ServerName<'_>,
59+
_server_name: &ServerName<'_>,
6560
_ocsp_response: &[u8],
6661
now: UnixTime,
6762
) -> Result<ServerCertVerified, TlsError> {
@@ -74,9 +69,6 @@ impl ServerCertVerifier for TlsServerCertVerifier {
7469
self.supported.all,
7570
)?;
7671

77-
if self.hostname_verification {
78-
rustls::client::verify_server_name(&cert, server_name)?;
79-
}
8072
Ok(ServerCertVerified::assertion())
8173
}
8274

@@ -168,10 +160,16 @@ impl TlsOptions {
168160
}
169161

170162
pub(crate) fn into_config(mut self) -> Result<ClientConfig> {
163+
let roots = self.take_roots();
171164
// This has to be called before server cert verifier to install default crypto provider.
172165
let builder = ClientConfig::builder();
173-
let verifier = TlsServerCertVerifier::new(self.take_roots(), self.hostname_verification);
174-
let builder = builder.dangerous().with_custom_certificate_verifier(Arc::new(verifier));
166+
let builder = match self.hostname_verification {
167+
true => builder.with_root_certificates(roots),
168+
false => unsafe {
169+
let verifier = NoHostnameVerificationServerCertVerifier::new(roots);
170+
builder.dangerous().with_custom_certificate_verifier(Arc::new(verifier))
171+
},
172+
};
175173
if let Some((client_cert, client_key)) = self.identity.take() {
176174
match builder.with_client_auth_cert(client_cert, client_key) {
177175
Ok(config) => Ok(config),

0 commit comments

Comments
 (0)