Skip to content

Commit fe20db8

Browse files
committed
feat(tls): do not verify TLS certificates for hostnames starting with _
1 parent cff0192 commit fe20db8

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/net/tls.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ use crate::net::session::SessionStream;
1010
use tokio_rustls::rustls;
1111
use tokio_rustls::rustls::client::ClientSessionStore;
1212

13+
mod danger;
14+
use danger::NoCertificateVerification;
15+
1316
pub async fn wrap_tls<'a>(
1417
strict_tls: bool,
1518
hostname: &str,
@@ -124,6 +127,12 @@ pub async fn wrap_rustls<'a>(
124127
config.resumption = resumption;
125128
config.enable_sni = use_sni;
126129

130+
if hostname.starts_with("_") {
131+
config
132+
.dangerous()
133+
.set_certificate_verifier(Arc::new(NoCertificateVerification::new()));
134+
}
135+
127136
let tls = tokio_rustls::TlsConnector::from(Arc::new(config));
128137
let name = tokio_rustls::rustls::pki_types::ServerName::try_from(hostname)?.to_owned();
129138
let tls_stream = tls.connect(name, stream).await?;

src/net/tls/danger.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//! Dangerous TLS implementation of accepting invalid certificates for Rustls.
2+
3+
use rustls::pki_types::{CertificateDer, ServerName, UnixTime};
4+
use tokio_rustls::rustls;
5+
6+
#[derive(Debug)]
7+
pub(super) struct NoCertificateVerification();
8+
9+
impl NoCertificateVerification {
10+
pub(super) fn new() -> Self {
11+
Self()
12+
}
13+
}
14+
15+
impl rustls::client::danger::ServerCertVerifier for NoCertificateVerification {
16+
fn verify_server_cert(
17+
&self,
18+
_end_entity: &CertificateDer<'_>,
19+
_intermediates: &[CertificateDer<'_>],
20+
_server_name: &ServerName<'_>,
21+
_ocsp_response: &[u8],
22+
_now: UnixTime,
23+
) -> Result<rustls::client::danger::ServerCertVerified, rustls::Error> {
24+
Ok(rustls::client::danger::ServerCertVerified::assertion())
25+
}
26+
27+
fn verify_tls12_signature(
28+
&self,
29+
message: &[u8],
30+
cert: &CertificateDer<'_>,
31+
dss: &rustls::DigitallySignedStruct,
32+
) -> Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
33+
let provider = rustls::crypto::ring::default_provider();
34+
let supported_schemes = &provider.signature_verification_algorithms;
35+
rustls::crypto::verify_tls12_signature(message, cert, dss, supported_schemes)
36+
}
37+
38+
fn verify_tls13_signature(
39+
&self,
40+
message: &[u8],
41+
cert: &CertificateDer<'_>,
42+
dss: &rustls::DigitallySignedStruct,
43+
) -> Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
44+
let provider = rustls::crypto::ring::default_provider();
45+
let supported_schemes = &provider.signature_verification_algorithms;
46+
rustls::crypto::verify_tls13_signature(message, cert, dss, supported_schemes)
47+
}
48+
49+
fn supported_verify_schemes(&self) -> Vec<rustls::SignatureScheme> {
50+
let provider = rustls::crypto::ring::default_provider();
51+
provider
52+
.signature_verification_algorithms
53+
.supported_schemes()
54+
}
55+
}

0 commit comments

Comments
 (0)