Skip to content

Commit 2ec2602

Browse files
committed
Refactor for rustls bump
Adapt the code for the new shape and arrangement of rustls types and traits.
1 parent 3d0e091 commit 2ec2602

File tree

3 files changed

+56
-21
lines changed

3 files changed

+56
-21
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ lazy_static = "1.4.0"
2525
thiserror = "1.0.38"
2626
native-tls = { version = "0.2.11", optional = true }
2727
tokio-native-tls = { version = "0.3.0", optional = true }
28-
rustls = { version = "0.22.0", optional = true }
28+
rustls = { version = "0.22.2", optional = true }
2929
tokio-rustls = { version = "0.25.0", optional = true }
3030
rustls-native-certs = { version = "0.7.0", optional = true }
3131
x509-parser = { version = "0.15.0", optional = true }

src/conn.rs

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use percent_encoding::percent_decode;
3232
#[cfg(all(feature = "gssapi", feature = "tls-rustls"))]
3333
use ring::digest::{self, digest, Algorithm};
3434
#[cfg(feature = "tls-rustls")]
35-
use rustls::{Certificate, ClientConfig, RootCertStore, ServerName};
35+
use rustls::{pki_types::CertificateDer, pki_types::ServerName, ClientConfig, RootCertStore};
3636
use tokio::io::{self, AsyncRead, AsyncWrite, AsyncWriteExt, ReadBuf};
3737
use tokio::net::TcpStream;
3838
#[cfg(unix)]
@@ -61,20 +61,54 @@ enum ConnType {
6161
}
6262

6363
#[cfg(feature = "tls-rustls")]
64+
#[derive(Debug)]
6465
struct NoCertVerification;
6566

6667
#[cfg(feature = "tls-rustls")]
67-
impl rustls::client::ServerCertVerifier for NoCertVerification {
68+
impl rustls::client::danger::ServerCertVerifier for NoCertVerification {
6869
fn verify_server_cert(
6970
&self,
70-
_: &Certificate,
71-
_: &[Certificate],
71+
_: &CertificateDer,
72+
_: &[CertificateDer],
7273
_: &ServerName,
73-
_: &mut dyn Iterator<Item = &[u8]>,
7474
_: &[u8],
75-
_: std::time::SystemTime,
76-
) -> std::result::Result<rustls::client::ServerCertVerified, rustls::Error> {
77-
Ok(rustls::client::ServerCertVerified::assertion())
75+
_: rustls::pki_types::UnixTime,
76+
) -> std::result::Result<rustls::client::danger::ServerCertVerified, rustls::Error> {
77+
Ok(rustls::client::danger::ServerCertVerified::assertion())
78+
}
79+
80+
fn verify_tls12_signature(
81+
&self,
82+
_: &[u8],
83+
_: &CertificateDer,
84+
_: &rustls::DigitallySignedStruct,
85+
) -> std::result::Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
86+
Ok(rustls::client::danger::HandshakeSignatureValid::assertion())
87+
}
88+
89+
fn verify_tls13_signature(
90+
&self,
91+
_: &[u8],
92+
_: &CertificateDer,
93+
_: &rustls::DigitallySignedStruct,
94+
) -> std::result::Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
95+
Ok(rustls::client::danger::HandshakeSignatureValid::assertion())
96+
}
97+
98+
fn supported_verify_schemes(&self) -> Vec<rustls::SignatureScheme> {
99+
vec![
100+
rustls::SignatureScheme::RSA_PKCS1_SHA1,
101+
rustls::SignatureScheme::ECDSA_SHA1_Legacy,
102+
rustls::SignatureScheme::RSA_PKCS1_SHA256,
103+
rustls::SignatureScheme::ECDSA_NISTP256_SHA256,
104+
rustls::SignatureScheme::RSA_PKCS1_SHA384,
105+
rustls::SignatureScheme::ECDSA_NISTP384_SHA384,
106+
rustls::SignatureScheme::RSA_PKCS1_SHA512,
107+
rustls::SignatureScheme::ECDSA_NISTP521_SHA512,
108+
rustls::SignatureScheme::RSA_PSS_SHA256,
109+
rustls::SignatureScheme::RSA_PSS_SHA384,
110+
rustls::SignatureScheme::RSA_PSS_SHA512,
111+
]
78112
}
79113
}
80114

@@ -83,7 +117,7 @@ lazy_static! {
83117
static ref CACERTS: RootCertStore = {
84118
let mut store = RootCertStore::empty();
85119
for cert in rustls_native_certs::load_native_certs().unwrap_or_else(|_| vec![]) {
86-
if let Ok(_) = store.add(&Certificate(cert.0)) {}
120+
if let Ok(_) = store.add(cert) {}
87121
}
88122
store
89123
};
@@ -477,17 +511,19 @@ impl LdapConnAsync {
477511
};
478512
TokioTlsConnector::from(config)
479513
.connect(
480-
ServerName::try_from(hostname).or_else(|e| {
481-
if no_tls_verify {
482-
if let Ok(_addr) = IpAddr::from_str(hostname) {
483-
ServerName::try_from("_irrelevant")
514+
ServerName::try_from(hostname)
515+
.map(|sn| sn.to_owned())
516+
.or_else(|e| {
517+
if no_tls_verify {
518+
if let Ok(_addr) = IpAddr::from_str(hostname) {
519+
ServerName::try_from("_irrelevant")
520+
} else {
521+
Err(e)
522+
}
484523
} else {
485524
Err(e)
486525
}
487-
} else {
488-
Err(e)
489-
}
490-
})?,
526+
})?,
491527
stream,
492528
)
493529
.await
@@ -497,7 +533,6 @@ impl LdapConnAsync {
497533
#[cfg(feature = "tls-rustls")]
498534
fn create_config(settings: &LdapConnSettings) -> Arc<ClientConfig> {
499535
let mut config = ClientConfig::builder()
500-
.with_safe_defaults()
501536
.with_root_certificates(CACERTS.clone())
502537
.with_no_client_auth();
503538
if settings.no_tls_verify {
@@ -635,7 +670,7 @@ impl LdapConnAsync {
635670
if certs.is_empty() {
636671
Ok(None)
637672
} else {
638-
Ok(Some(certs[0].clone().0))
673+
Ok(Some(certs[0].to_vec()))
639674
}
640675
}
641676
}

src/result.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ pub enum LdapError {
124124
#[error("rustls DNS error: {source}")]
125125
DNSName {
126126
#[from]
127-
source: rustls::client::InvalidDnsNameError,
127+
source: rustls::pki_types::InvalidDnsNameError,
128128
},
129129

130130
/// LDAP operation result with an error return code.

0 commit comments

Comments
 (0)