1
- use crate :: error:: { Error , Result } ;
2
- use rustls_pemfile:: { certs, rsa_private_keys} ;
1
+ use crate :: error:: Result ;
3
2
use std:: {
4
3
fs:: File ,
5
4
io:: BufReader ,
@@ -33,30 +32,28 @@ pub(crate) fn retrieve_root_cert_store_for_client(cafile: &Option<PathBuf>) -> R
33
32
}
34
33
}
35
34
if !done {
36
- root_cert_store. add_trust_anchors (
37
- webpki_roots:: TLS_SERVER_ROOTS
38
- . iter ( )
39
- . map ( |ta| OwnedTrustAnchor :: from_subject_spki_name_constraints ( ta. subject , ta. spki , ta. name_constraints ) ) ,
40
- ) ;
35
+ root_cert_store. add_trust_anchors ( webpki_roots:: TLS_SERVER_ROOTS . iter ( ) . map ( |ta| {
36
+ let name_constraints = ta. name_constraints . clone ( ) . map ( |nc| nc. as_ref ( ) . to_vec ( ) ) ;
37
+ OwnedTrustAnchor :: from_subject_spki_name_constraints ( ta. subject . as_ref ( ) , ta. subject_public_key_info . as_ref ( ) , name_constraints)
38
+ } ) ) ;
41
39
}
42
40
Ok ( root_cert_store)
43
41
}
44
42
45
- mod danger {
46
- pub struct NoCertificateVerification { }
43
+ # [ derive ( Debug ) ]
44
+ pub struct NoCertificateVerification { }
47
45
48
- impl rustls:: client:: ServerCertVerifier for NoCertificateVerification {
49
- fn verify_server_cert (
50
- & self ,
51
- _end_entity : & rustls:: Certificate ,
52
- _intermediates : & [ rustls:: Certificate ] ,
53
- _server_name : & rustls:: ServerName ,
54
- _scts : & mut dyn Iterator < Item = & [ u8 ] > ,
55
- _ocsp : & [ u8 ] ,
56
- _now : std:: time:: SystemTime ,
57
- ) -> Result < rustls:: client:: ServerCertVerified , rustls:: Error > {
58
- Ok ( rustls:: client:: ServerCertVerified :: assertion ( ) )
59
- }
46
+ impl rustls:: client:: ServerCertVerifier for NoCertificateVerification {
47
+ fn verify_server_cert (
48
+ & self ,
49
+ _end_entity : & rustls:: Certificate ,
50
+ _intermediates : & [ rustls:: Certificate ] ,
51
+ _server_name : & rustls:: ServerName ,
52
+ _scts : & mut dyn Iterator < Item = & [ u8 ] > ,
53
+ _ocsp : & [ u8 ] ,
54
+ _now : std:: time:: SystemTime ,
55
+ ) -> Result < rustls:: client:: ServerCertVerified , rustls:: Error > {
56
+ Ok ( rustls:: client:: ServerCertVerified :: assertion ( ) )
60
57
}
61
58
}
62
59
@@ -69,9 +66,7 @@ pub(crate) async fn create_tls_client_stream(
69
66
. with_safe_defaults ( )
70
67
. with_root_certificates ( root_cert_store)
71
68
. with_no_client_auth ( ) ;
72
- config
73
- . dangerous ( )
74
- . set_certificate_verifier ( Arc :: new ( danger:: NoCertificateVerification { } ) ) ;
69
+ config. dangerous ( ) . set_certificate_verifier ( Arc :: new ( NoCertificateVerification { } ) ) ;
75
70
let connector = TlsConnector :: from ( std:: sync:: Arc :: new ( config) ) ;
76
71
77
72
let stream = crate :: tcp_stream:: create ( addr) . await ?;
@@ -84,13 +79,11 @@ pub(crate) async fn create_tls_client_stream(
84
79
}
85
80
86
81
pub ( crate ) fn server_load_certs ( path : & Path ) -> Result < Vec < Certificate > > {
87
- certs ( & mut BufReader :: new ( File :: open ( path) ?) )
88
- . map_err ( |e| Error :: from ( format ! ( "Certificate error: {e}" ) ) )
89
- . map ( |mut certs| certs. drain ( ..) . map ( Certificate ) . collect ( ) )
82
+ let certs = rustls_pemfile:: certs ( & mut BufReader :: new ( File :: open ( path) ?) ) ?;
83
+ Ok ( certs. into_iter ( ) . map ( Certificate ) . collect ( ) )
90
84
}
91
85
92
86
pub ( crate ) fn server_load_keys ( path : & Path ) -> Result < Vec < PrivateKey > > {
93
- rsa_private_keys ( & mut BufReader :: new ( File :: open ( path) ?) )
94
- . map_err ( |e| Error :: from ( format ! ( "PrivateKey error: {e}" ) ) )
95
- . map ( |mut keys| keys. drain ( ..) . map ( PrivateKey ) . collect ( ) )
87
+ let keys = rustls_pemfile:: rsa_private_keys ( & mut BufReader :: new ( File :: open ( path) ?) ) ?;
88
+ Ok ( keys. into_iter ( ) . map ( PrivateKey ) . collect ( ) )
96
89
}
0 commit comments