3
3
//! This module contains the definition of the raw client that wraps the transport method
4
4
5
5
use std:: collections:: { BTreeMap , BTreeSet , HashMap , VecDeque } ;
6
+ use std:: convert:: TryFrom ;
6
7
use std:: io:: { BufRead , BufReader , Read , Write } ;
7
8
use std:: mem:: drop;
8
9
use std:: net:: { TcpStream , ToSocketAddrs } ;
@@ -24,7 +25,9 @@ use openssl::ssl::{SslConnector, SslMethod, SslStream, SslVerifyMode};
24
25
any( feature = "default" , feature = "use-rustls" ) ,
25
26
not( feature = "use-openssl" )
26
27
) ) ]
27
- use rustls:: { ClientConfig , ClientSession , StreamOwned } ;
28
+ use rustls:: {
29
+ ClientConfig , ClientConnection , OwnedTrustAnchor , RootCertStore , ServerName , StreamOwned ,
30
+ } ;
28
31
29
32
#[ cfg( any( feature = "default" , feature = "proxy" ) ) ]
30
33
use socks:: { Socks5Stream , TargetAddr , ToTargetAddr } ;
@@ -277,19 +280,23 @@ impl RawClient<ElectrumSslStream> {
277
280
) ) ]
278
281
mod danger {
279
282
use rustls;
280
- use webpki;
283
+ use rustls:: client:: ServerCertVerified ;
284
+ use rustls:: { Certificate , Error , ServerName } ;
285
+ use std:: time:: SystemTime ;
281
286
282
287
pub struct NoCertificateVerification { }
283
288
284
- impl rustls:: ServerCertVerifier for NoCertificateVerification {
289
+ impl rustls:: client :: ServerCertVerifier for NoCertificateVerification {
285
290
fn verify_server_cert (
286
291
& 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 ( ) )
293
300
}
294
301
}
295
302
}
@@ -299,7 +306,7 @@ mod danger {
299
306
not( feature = "use-openssl" )
300
307
) ) ]
301
308
/// 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 > ;
303
310
#[ cfg( all(
304
311
any( feature = "default" , feature = "use-rustls" ) ,
305
312
not( feature = "use-openssl" )
@@ -341,26 +348,37 @@ impl RawClient<ElectrumSslStream> {
341
348
validate_domain : bool ,
342
349
tcp_stream : TcpStream ,
343
350
) -> 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 {
346
354
socket_addr. domain ( ) . ok_or ( Error :: MissingDomain ) ?;
347
355
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
+
348
365
// 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 ( )
352
367
} 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
+ } ;
357
374
358
375
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 ( ) )
362
379
. map_err ( |_| Error :: InvalidDNSNameError ( domain. clone ( ) ) ) ?,
363
- ) ;
380
+ )
381
+ . map_err ( Error :: CouldNotCreateConnection ) ?;
364
382
let stream = StreamOwned :: new ( session, tcp_stream) ;
365
383
366
384
Ok ( stream. into ( ) )
0 commit comments