Skip to content

Commit 8b32726

Browse files
committed
Merge #62: Upgrade rustls and webpki version
68d2f23 Upgrade rustls and webpki version (Thomas Eizinger) Pull request description: ACKs for top commit: afilini: ACK 68d2f23 Tree-SHA512: 3578954d6629470742fec326525fb34377380c429ff51d5f15e128045161fc6fba4852a22089c2fa9e6bfa7c35ffbaf06eadb0f457fbcb414d15962b9283cd64
2 parents 98e1bf2 + 68d2f23 commit 8b32726

File tree

3 files changed

+52
-28
lines changed

3 files changed

+52
-28
lines changed

Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ serde = { version = "^1.0", features = ["derive"] }
2323
serde_json = { version = "^1.0" }
2424

2525
# Optional dependencies
26-
socks = { version = "^0.3", optional = true }
27-
openssl = { version = "^0.10", optional = true }
28-
rustls = { version = "0.16.0", optional = true, features = ["dangerous_configuration"] }
29-
webpki = { version = "0.21.0", optional = true }
30-
webpki-roots = { version = "^0.19", optional = true }
26+
socks = { version = "0.3", optional = true }
27+
openssl = { version = "0.10", optional = true }
28+
rustls = { version = "0.20", optional = true, features = ["dangerous_configuration"] }
29+
webpki = { version = "0.22", optional = true }
30+
webpki-roots = { version = "0.22", optional = true }
3131

3232
[features]
3333
default = ["proxy", "use-rustls"]

src/raw_client.rs

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//! This module contains the definition of the raw client that wraps the transport method
44
55
use std::collections::{BTreeMap, BTreeSet, HashMap, VecDeque};
6+
use std::convert::TryFrom;
67
use std::io::{BufRead, BufReader, Read, Write};
78
use std::mem::drop;
89
use std::net::{TcpStream, ToSocketAddrs};
@@ -24,7 +25,9 @@ use openssl::ssl::{SslConnector, SslMethod, SslStream, SslVerifyMode};
2425
any(feature = "default", feature = "use-rustls"),
2526
not(feature = "use-openssl")
2627
))]
27-
use rustls::{ClientConfig, ClientSession, StreamOwned};
28+
use rustls::{
29+
ClientConfig, ClientConnection, OwnedTrustAnchor, RootCertStore, ServerName, StreamOwned,
30+
};
2831

2932
#[cfg(any(feature = "default", feature = "proxy"))]
3033
use socks::{Socks5Stream, TargetAddr, ToTargetAddr};
@@ -277,19 +280,23 @@ impl RawClient<ElectrumSslStream> {
277280
))]
278281
mod danger {
279282
use rustls;
280-
use webpki;
283+
use rustls::client::ServerCertVerified;
284+
use rustls::{Certificate, Error, ServerName};
285+
use std::time::SystemTime;
281286

282287
pub struct NoCertificateVerification {}
283288

284-
impl rustls::ServerCertVerifier for NoCertificateVerification {
289+
impl rustls::client::ServerCertVerifier for NoCertificateVerification {
285290
fn verify_server_cert(
286291
&self,
287-
_roots: &rustls::RootCertStore,
288-
_presented_certs: &[rustls::Certificate],
289-
_dns_name: webpki::DNSNameRef<'_>,
290-
_ocsp: &[u8],
291-
) -> Result<rustls::ServerCertVerified, rustls::TLSError> {
292-
Ok(rustls::ServerCertVerified::assertion())
292+
_end_entity: &Certificate,
293+
_intermediates: &[Certificate],
294+
_server_name: &ServerName,
295+
_scts: &mut dyn Iterator<Item = &[u8]>,
296+
_ocsp_response: &[u8],
297+
_now: SystemTime,
298+
) -> Result<ServerCertVerified, Error> {
299+
Ok(ServerCertVerified::assertion())
293300
}
294301
}
295302
}
@@ -299,7 +306,7 @@ mod danger {
299306
not(feature = "use-openssl")
300307
))]
301308
/// Transport type used to establish a Rustls TLS encrypted/authenticated connection with the server
302-
pub type ElectrumSslStream = StreamOwned<ClientSession, TcpStream>;
309+
pub type ElectrumSslStream = StreamOwned<ClientConnection, TcpStream>;
303310
#[cfg(all(
304311
any(feature = "default", feature = "use-rustls"),
305312
not(feature = "use-openssl")
@@ -341,26 +348,37 @@ impl RawClient<ElectrumSslStream> {
341348
validate_domain: bool,
342349
tcp_stream: TcpStream,
343350
) -> Result<Self, Error> {
344-
let mut config = ClientConfig::new();
345-
if validate_domain {
351+
let builder = ClientConfig::builder().with_safe_defaults();
352+
353+
let config = if validate_domain {
346354
socket_addr.domain().ok_or(Error::MissingDomain)?;
347355

356+
let mut store = RootCertStore::empty();
357+
store.add_server_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.0.into_iter().map(|t| {
358+
OwnedTrustAnchor::from_subject_spki_name_constraints(
359+
t.subject,
360+
t.spki,
361+
t.name_constraints,
362+
)
363+
}));
364+
348365
// TODO: cert pinning
349-
config
350-
.root_store
351-
.add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);
366+
builder.with_root_certificates(store).with_no_client_auth()
352367
} else {
353-
config
354-
.dangerous()
355-
.set_certificate_verifier(std::sync::Arc::new(danger::NoCertificateVerification {}))
356-
}
368+
builder
369+
.with_custom_certificate_verifier(std::sync::Arc::new(
370+
danger::NoCertificateVerification {},
371+
))
372+
.with_no_client_auth()
373+
};
357374

358375
let domain = socket_addr.domain().unwrap_or("NONE").to_string();
359-
let session = ClientSession::new(
360-
&std::sync::Arc::new(config),
361-
webpki::DNSNameRef::try_from_ascii_str(&domain)
376+
let session = ClientConnection::new(
377+
std::sync::Arc::new(config),
378+
ServerName::try_from(domain.as_str())
362379
.map_err(|_| Error::InvalidDNSNameError(domain.clone()))?,
363-
);
380+
)
381+
.map_err(Error::CouldNotCreateConnection)?;
364382
let stream = StreamOwned::new(session, tcp_stream);
365383

366384
Ok(stream.into())

src/types.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,10 @@ pub enum Error {
303303
/// Broken IPC communication channel: the other thread probably has exited
304304
Mpsc,
305305

306+
#[cfg(feature = "use-rustls")]
307+
/// Could not create a rustls client connection
308+
CouldNotCreateConnection(rustls::Error),
309+
306310
#[cfg(feature = "use-openssl")]
307311
/// Invalid OpenSSL method used
308312
InvalidSslMethod(openssl::error::ErrorStack),
@@ -323,6 +327,8 @@ impl Display for Error {
323327
Error::SslHandshakeError(e) => Display::fmt(e, f),
324328
#[cfg(feature = "use-openssl")]
325329
Error::InvalidSslMethod(e) => Display::fmt(e, f),
330+
#[cfg(feature = "use-rustls")]
331+
Error::CouldNotCreateConnection(e) => Display::fmt(e, f),
326332

327333
Error::Message(e) => f.write_str(e),
328334
Error::InvalidDNSNameError(domain) => write!(f, "Invalid domain name {} not matching SSL certificate", domain),

0 commit comments

Comments
 (0)