1- #[ cfg( feature = "rustls" ) ]
1+ #[ cfg( any ( feature = "rustls" , feature = "native-tls" ) ) ]
22use std:: sync:: Arc ;
3- #[ cfg( all( feature = "native-tls" , not( feature = "rustls" ) , feature = "tokio-native-tls" ) ) ]
4- use std:: sync:: { Arc , Mutex } ;
53
64#[ cfg( all( feature = "native-tls" , not( feature = "rustls" ) ) ) ]
75use native_tls:: { Certificate , TlsConnector , TlsConnectorBuilder } ;
86#[ cfg( feature = "rustls" ) ]
97use rustls:: RootCertStore ;
8+ #[ cfg( all( feature = "native-tls" , not( feature = "rustls" ) , feature = "tokio-native-tls" ) ) ]
9+ use tokio_native_tls:: TlsConnector as AsyncTlsConnector ;
1010#[ cfg( feature = "rustls-webpki" ) ]
1111use webpki_roots:: TLS_SERVER_ROOTS ;
1212
1313use crate :: Error ;
1414
15- #[ derive( Clone ) ]
1615#[ cfg( feature = "rustls" ) ]
17- pub ( crate ) struct Certificates {
18- pub ( crate ) inner : Arc < RootCertStore > ,
16+ pub ( crate ) struct CertificatesBuilder {
17+ pub ( crate ) inner : RootCertStore ,
1918 pub ( crate ) disable_default : bool ,
2019}
2120
22- #[ derive( Clone ) ]
2321#[ cfg( all( feature = "native-tls" , not( feature = "rustls" ) , feature = "tokio-native-tls" ) ) ]
24- pub ( crate ) struct Certificates {
25- pub ( crate ) inner : CertificatesInner ,
22+ pub ( crate ) struct CertificatesBuilder {
23+ pub ( crate ) inner : TlsConnectorBuilder ,
2624}
2725
28- #[ derive( Clone ) ]
29- #[ cfg( all( feature = "native-tls" , not( feature = "rustls" ) , feature = "tokio-native-tls" ) ) ]
30- pub ( crate ) enum CertificatesInner {
31- Builder ( Arc < Mutex < TlsConnectorBuilder > > ) ,
32- Built ( TlsConnector ) ,
33- }
34-
35- impl Certificates {
26+ impl CertificatesBuilder {
3627 #[ cfg( feature = "rustls" ) ]
3728 pub ( crate ) fn new ( cert_der : Option < Vec < u8 > > ) -> Result < Self , Error > {
38- let certificates = Self { inner : Arc :: new ( RootCertStore :: empty ( ) ) , disable_default : false } ;
29+ let mut certificates = Self { inner : RootCertStore :: empty ( ) , disable_default : false } ;
3930
4031 if let Some ( cert_der) = cert_der {
41- certificates. append_certificate ( cert_der)
42- } else {
43- Ok ( certificates)
32+ certificates. append_certificate ( cert_der) ?;
4433 }
34+
35+ Ok ( certificates)
4536 }
4637
4738 #[ cfg( all( feature = "native-tls" , not( feature = "rustls" ) , feature = "tokio-native-tls" ) ) ]
4839 pub ( crate ) fn new ( cert_der : Option < Vec < u8 > > ) -> Result < Self , Error > {
4940 let builder = TlsConnector :: builder ( ) ;
50- let inner = CertificatesInner :: Builder ( Arc :: new ( Mutex :: new ( builder) ) ) ;
51- let certificates = Self { inner : inner } ;
41+ let mut certificates = Self { inner : builder } ;
5242
5343 if let Some ( cert_der) = cert_der {
54- certificates. append_certificate ( cert_der)
55- } else {
56- Ok ( certificates)
44+ certificates. append_certificate ( cert_der) ?;
5745 }
46+
47+ Ok ( certificates)
5848 }
5949
6050 #[ cfg( feature = "rustls" ) ]
61- pub ( crate ) fn append_certificate ( mut self , cert_der : Vec < u8 > ) -> Result < Self , Error > {
62- let certificates = Arc :: make_mut ( & mut self . inner ) ;
63- certificates. add ( & rustls:: Certificate ( cert_der) ) . map_err ( Error :: RustlsAppendCert ) ?;
51+ pub ( crate ) fn append_certificate ( & mut self , cert_der : Vec < u8 > ) -> Result < & mut Self , Error > {
52+ self . inner . add ( & rustls:: Certificate ( cert_der) ) . map_err ( Error :: RustlsAppendCert ) ?;
6453
6554 Ok ( self )
6655 }
6756
6857 #[ cfg( all( feature = "native-tls" , not( feature = "rustls" ) , feature = "tokio-native-tls" ) ) ]
69- pub ( crate ) fn append_certificate ( mut self , cert_der : Vec < u8 > ) -> Result < Self , Error > {
70- let new_inner = match self . inner {
71- CertificatesInner :: Builder ( builder_mutex) => {
72- let certificate = Certificate :: from_der ( & cert_der) ?;
73-
74- {
75- let mut builder_guard = builder_mutex. lock ( ) . unwrap ( ) ;
76- builder_guard. add_root_certificate ( certificate) ;
77- }
58+ pub ( crate ) fn append_certificate ( & mut self , cert_der : Vec < u8 > ) -> Result < & mut Self , Error > {
59+ let certificate = Certificate :: from_der ( & cert_der) ?;
60+ self . inner . add_root_certificate ( certificate) ;
7861
79- CertificatesInner :: Builder ( builder_mutex)
80- }
81- CertificatesInner :: Built ( _) => return Err ( Error :: NativeTlsAppendCert ) ,
82- } ;
83-
84- self . inner = new_inner;
8562 Ok ( self )
8663 }
8764
8865 #[ cfg( all( feature = "native-tls" , not( feature = "rustls" ) , feature = "tokio-native-tls" ) ) ]
89- pub ( crate ) fn build ( mut self ) -> Result < Self , Error > {
90- let new_inner = match self . inner {
91- CertificatesInner :: Builder ( builder_mutex) => {
92- let mut builder_guard = builder_mutex. lock ( ) . unwrap ( ) ;
93- let connector = builder_guard. build ( ) ?;
94-
95- CertificatesInner :: Built ( connector)
96- }
97- CertificatesInner :: Built ( _) => return Ok ( self ) ,
98- } ;
66+ pub ( crate ) fn build ( self ) -> Result < Certificates , Error > {
67+ let connector = self . inner . build ( ) ?;
68+ let async_connector = AsyncTlsConnector :: from ( connector) ;
9969
100- self . inner = new_inner;
101- Ok ( self )
70+ Ok ( Certificates ( Arc :: new ( async_connector) ) )
10271 }
10372
10473 #[ cfg( feature = "rustls" ) ]
105- pub ( crate ) fn with_root_certificates ( mut self ) -> Self {
106- let root_certificates = Arc :: make_mut ( & mut self . inner ) ;
74+ pub ( crate ) fn build ( mut self ) -> Result < Certificates , Error > {
75+ if !self . disable_default {
76+ self . with_root_certificates ( ) ;
77+ }
78+
79+ Ok ( Certificates ( Arc :: new ( self . inner ) ) )
80+ }
10781
82+ #[ cfg( feature = "rustls" ) ]
83+ fn with_root_certificates ( & mut self ) -> & mut Self {
10884 // Try to load native certs
10985 #[ cfg( feature = "https-rustls-probe" ) ]
11086 if let Ok ( os_roots) = rustls_native_certs:: load_native_certs ( ) {
11187 for root_cert in os_roots {
11288 // Ignore erroneous OS certificates, there's nothing
11389 // to do differently in that situation anyways.
114- let _ = root_certificates . add ( & rustls:: Certificate ( root_cert. 0 ) ) ;
90+ let _ = self . inner . add ( & rustls:: Certificate ( root_cert. 0 ) ) ;
11591 }
11692 }
11793
11894 #[ cfg( feature = "rustls-webpki" ) ]
11995 {
12096 #[ allow( deprecated) ]
12197 // Need to use add_server_trust_anchors to compile with rustls 0.21.1
122- root_certificates . add_server_trust_anchors ( TLS_SERVER_ROOTS . iter ( ) . map ( |ta| {
98+ self . inner . add_server_trust_anchors ( TLS_SERVER_ROOTS . iter ( ) . map ( |ta| {
12399 rustls:: OwnedTrustAnchor :: from_subject_spki_name_constraints (
124100 ta. subject ,
125101 ta. spki ,
@@ -131,19 +107,22 @@ impl Certificates {
131107 }
132108
133109 #[ cfg( feature = "rustls" ) ]
134- pub ( crate ) fn disable_default ( mut self ) -> Result < Self , Error > {
110+ pub ( crate ) fn disable_default ( & mut self ) -> Result < & mut Self , Error > {
135111 self . disable_default = true ;
136112 Ok ( self )
137113 }
138114
139115 #[ cfg( all( feature = "native-tls" , not( feature = "rustls" ) , feature = "tokio-native-tls" ) ) ]
140- pub ( crate ) fn disable_default ( self ) -> Result < Self , Error > {
141- match self . inner {
142- CertificatesInner :: Builder ( ref builder_mutex) => {
143- builder_mutex. lock ( ) . unwrap ( ) . disable_built_in_roots ( true ) ;
144- Ok ( self )
145- }
146- CertificatesInner :: Built ( _) => return Err ( Error :: InvalidTlsConfig ) ,
147- }
116+ pub ( crate ) fn disable_default ( & mut self ) -> Result < & mut Self , Error > {
117+ self . inner . disable_built_in_roots ( true ) ;
118+ Ok ( self )
148119 }
149120}
121+
122+ #[ derive( Clone ) ]
123+ #[ cfg( feature = "rustls" ) ]
124+ pub ( crate ) struct Certificates ( pub ( crate ) Arc < RootCertStore > ) ;
125+
126+ #[ derive( Clone ) ]
127+ #[ cfg( all( feature = "native-tls" , not( feature = "rustls" ) , feature = "tokio-native-tls" ) ) ]
128+ pub ( crate ) struct Certificates ( pub ( crate ) Arc < AsyncTlsConnector > ) ;
0 commit comments