@@ -15,8 +15,8 @@ use hyper::server::conn::{AddrIncoming, AddrStream};
15
15
16
16
use crate :: transport:: Transport ;
17
17
use tokio_rustls:: rustls:: {
18
- AllowAnyAnonymousOrAuthenticatedClient , AllowAnyAuthenticatedClient , NoClientAuth ,
19
- RootCertStore , ServerConfig , TLSError ,
18
+ server :: { AllowAnyAnonymousOrAuthenticatedClient , AllowAnyAuthenticatedClient , NoClientAuth } ,
19
+ Certificate , Error as TlsError , PrivateKey , RootCertStore , ServerConfig ,
20
20
} ;
21
21
22
22
/// Represents errors that can occur building the TlsConfig
@@ -32,7 +32,7 @@ pub(crate) enum TlsConfigError {
32
32
/// An error from an empty key
33
33
EmptyKey ,
34
34
/// An error from an invalid key
35
- InvalidKey ( TLSError ) ,
35
+ InvalidKey ( TlsError ) ,
36
36
}
37
37
38
38
impl fmt:: Display for TlsConfigError {
@@ -169,8 +169,11 @@ impl TlsConfigBuilder {
169
169
170
170
pub ( crate ) fn build ( mut self ) -> Result < ServerConfig , TlsConfigError > {
171
171
let mut cert_rdr = BufReader :: new ( self . cert ) ;
172
- let cert = tokio_rustls:: rustls:: internal:: pemfile:: certs ( & mut cert_rdr)
173
- . map_err ( |( ) | TlsConfigError :: CertParseError ) ?;
172
+ let cert = rustls_pemfile:: certs ( & mut cert_rdr)
173
+ . map_err ( |_e| TlsConfigError :: CertParseError ) ?
174
+ . into_iter ( )
175
+ . map ( Certificate )
176
+ . collect ( ) ;
174
177
175
178
let key = {
176
179
// convert it to Vec<u8> to allow reading it again if key is RSA
@@ -183,21 +186,17 @@ impl TlsConfigBuilder {
183
186
return Err ( TlsConfigError :: EmptyKey ) ;
184
187
}
185
188
186
- let mut pkcs8 = tokio_rustls:: rustls:: internal:: pemfile:: pkcs8_private_keys (
187
- & mut key_vec. as_slice ( ) ,
188
- )
189
- . map_err ( |( ) | TlsConfigError :: Pkcs8ParseError ) ?;
189
+ let mut pkcs8 = rustls_pemfile:: pkcs8_private_keys ( & mut key_vec. as_slice ( ) )
190
+ . map_err ( |_e| TlsConfigError :: Pkcs8ParseError ) ?;
190
191
191
192
if !pkcs8. is_empty ( ) {
192
- pkcs8. remove ( 0 )
193
+ PrivateKey ( pkcs8. remove ( 0 ) )
193
194
} else {
194
- let mut rsa = tokio_rustls:: rustls:: internal:: pemfile:: rsa_private_keys (
195
- & mut key_vec. as_slice ( ) ,
196
- )
197
- . map_err ( |( ) | TlsConfigError :: RsaParseError ) ?;
195
+ let mut rsa = rustls_pemfile:: rsa_private_keys ( & mut key_vec. as_slice ( ) )
196
+ . map_err ( |_e| TlsConfigError :: RsaParseError ) ?;
198
197
199
198
if !rsa. is_empty ( ) {
200
- rsa. remove ( 0 )
199
+ PrivateKey ( rsa. remove ( 0 ) )
201
200
} else {
202
201
return Err ( TlsConfigError :: EmptyKey ) ;
203
202
}
@@ -207,13 +206,18 @@ impl TlsConfigBuilder {
207
206
fn read_trust_anchor (
208
207
trust_anchor : Box < dyn Read + Send + Sync > ,
209
208
) -> Result < RootCertStore , TlsConfigError > {
210
- let mut reader = BufReader :: new ( trust_anchor) ;
209
+ let trust_anchors = {
210
+ let mut reader = BufReader :: new ( trust_anchor) ;
211
+ rustls_pemfile:: certs ( & mut reader) . map_err ( TlsConfigError :: Io ) ?
212
+ } ;
213
+
211
214
let mut store = RootCertStore :: empty ( ) ;
212
- if let Ok ( ( 0 , _) ) | Err ( ( ) ) = store. add_pem_file ( & mut reader) {
213
- Err ( TlsConfigError :: CertParseError )
214
- } else {
215
- Ok ( store)
215
+ let ( added, _skipped) = store. add_parsable_certificates ( & trust_anchors) ;
216
+ if added == 0 {
217
+ return Err ( TlsConfigError :: CertParseError ) ;
216
218
}
219
+
220
+ Ok ( store)
217
221
}
218
222
219
223
let client_auth = match self . client_auth {
@@ -226,11 +230,12 @@ impl TlsConfigBuilder {
226
230
}
227
231
} ;
228
232
229
- let mut config = ServerConfig :: new ( client_auth) ;
230
- config
231
- . set_single_cert_with_ocsp_and_sct ( cert, key, self . ocsp_resp , Vec :: new ( ) )
232
- . map_err ( |err| TlsConfigError :: InvalidKey ( err) ) ?;
233
- config. set_protocols ( & [ "h2" . into ( ) , "http/1.1" . into ( ) ] ) ;
233
+ let mut config = ServerConfig :: builder ( )
234
+ . with_safe_defaults ( )
235
+ . with_client_cert_verifier ( client_auth. into ( ) )
236
+ . with_single_cert_with_ocsp_and_sct ( cert, key, self . ocsp_resp , Vec :: new ( ) )
237
+ . map_err ( TlsConfigError :: InvalidKey ) ?;
238
+ config. alpn_protocols = vec ! [ "h2" . into( ) , "http/1.1" . into( ) ] ;
234
239
Ok ( config)
235
240
}
236
241
}
0 commit comments