33//! This module contains the definition of the raw client that wraps the transport method
44
55use std:: collections:: { BTreeMap , BTreeSet , HashMap , VecDeque } ;
6+ use std:: convert:: TryFrom ;
67use std:: io:: { BufRead , BufReader , Read , Write } ;
78use std:: mem:: drop;
89use 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" ) ) ]
3033use socks:: { Socks5Stream , TargetAddr , ToTargetAddr } ;
@@ -277,19 +280,23 @@ impl RawClient<ElectrumSslStream> {
277280) ) ]
278281mod 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 ( ) )
0 commit comments