@@ -33,8 +33,10 @@ use crate::{
33
33
server:: { io_other, Server } ,
34
34
} ;
35
35
use arc_swap:: ArcSwap ;
36
- use rustls:: { Certificate , PrivateKey , ServerConfig } ;
37
- use rustls_pemfile:: Item ;
36
+ use rustls:: {
37
+ pki_types:: { CertificateDer , PrivateKeyDer } ,
38
+ ServerConfig ,
39
+ } ;
38
40
use std:: time:: Duration ;
39
41
use std:: { fmt, io, net:: SocketAddr , path:: Path , sync:: Arc } ;
40
42
use tokio:: {
@@ -172,10 +174,8 @@ impl RustlsConfig {
172
174
/// The certificate must be DER-encoded X.509.
173
175
///
174
176
/// The private key must be DER-encoded ASN.1 in either PKCS#8 or PKCS#1 format.
175
- pub async fn from_der ( cert : Vec < Vec < u8 > > , key : Vec < u8 > ) -> io:: Result < Self > {
176
- let server_config = spawn_blocking ( || config_from_der ( cert, key) )
177
- . await
178
- . unwrap ( ) ?;
177
+ pub async fn from_der ( cert : Vec < Vec < u8 > > , key : PrivateKeyDer < ' static > ) -> io:: Result < Self > {
178
+ let server_config = config_from_der ( cert, key) ?;
179
179
let inner = Arc :: new ( ArcSwap :: from_pointee ( server_config) ) ;
180
180
181
181
Ok ( Self { inner } )
@@ -218,10 +218,12 @@ impl RustlsConfig {
218
218
/// The certificate must be DER-encoded X.509.
219
219
///
220
220
/// The private key must be DER-encoded ASN.1 in either PKCS#8 or PKCS#1 format.
221
- pub async fn reload_from_der ( & self , cert : Vec < Vec < u8 > > , key : Vec < u8 > ) -> io:: Result < ( ) > {
222
- let server_config = spawn_blocking ( || config_from_der ( cert, key) )
223
- . await
224
- . unwrap ( ) ?;
221
+ pub async fn reload_from_der (
222
+ & self ,
223
+ cert : Vec < Vec < u8 > > ,
224
+ key : PrivateKeyDer < ' static > ,
225
+ ) -> io:: Result < ( ) > {
226
+ let server_config = config_from_der ( cert, key) ?;
225
227
let inner = Arc :: new ( server_config) ;
226
228
227
229
self . inner . store ( inner) ;
@@ -278,12 +280,10 @@ impl fmt::Debug for RustlsConfig {
278
280
}
279
281
}
280
282
281
- fn config_from_der ( cert : Vec < Vec < u8 > > , key : Vec < u8 > ) -> io:: Result < ServerConfig > {
282
- let cert = cert. into_iter ( ) . map ( Certificate ) . collect ( ) ;
283
- let key = PrivateKey ( key) ;
283
+ fn config_from_der ( cert : Vec < Vec < u8 > > , key : PrivateKeyDer < ' static > ) -> io:: Result < ServerConfig > {
284
+ let cert = cert. into_iter ( ) . map ( CertificateDer :: from) . collect ( ) ;
284
285
285
286
let mut config = ServerConfig :: builder ( )
286
- . with_safe_defaults ( )
287
287
. with_no_client_auth ( )
288
288
. with_single_cert ( cert, key)
289
289
. map_err ( io_other) ?;
@@ -294,24 +294,14 @@ fn config_from_der(cert: Vec<Vec<u8>>, key: Vec<u8>) -> io::Result<ServerConfig>
294
294
}
295
295
296
296
fn config_from_pem ( cert : Vec < u8 > , key : Vec < u8 > ) -> io:: Result < ServerConfig > {
297
- use rustls_pemfile:: Item ;
298
-
299
- let cert = rustls_pemfile:: certs ( & mut cert. as_ref ( ) ) ?;
300
- // Check the entire PEM file for the key in case it is not first section
301
- let mut key_vec: Vec < Vec < u8 > > = rustls_pemfile:: read_all ( & mut key. as_ref ( ) ) ?
302
- . into_iter ( )
303
- . filter_map ( |i| match i {
304
- Item :: RSAKey ( key) | Item :: PKCS8Key ( key) | Item :: ECKey ( key) => Some ( key) ,
305
- _ => None ,
306
- } )
307
- . collect ( ) ;
308
-
309
- // Make sure file contains only one key
310
- if key_vec. len ( ) != 1 {
311
- return Err ( io_other ( "private key format not supported" ) ) ;
312
- }
297
+ let cert = rustls_pemfile:: certs ( & mut cert. as_ref ( ) )
298
+ . map ( |cert| cert. map ( |cert| cert. as_ref ( ) . to_vec ( ) ) )
299
+ . collect :: < Result < Vec < _ > , _ > > ( ) ?;
300
+ // Use the first private key found.
301
+ let key = rustls_pemfile:: private_key ( & mut key. as_ref ( ) ) ?
302
+ . ok_or ( io_other ( "private key format not found" ) ) ?;
313
303
314
- config_from_der ( cert, key_vec . pop ( ) . unwrap ( ) )
304
+ config_from_der ( cert, key )
315
305
}
316
306
317
307
async fn config_from_pem_file (
@@ -329,21 +319,12 @@ async fn config_from_pem_chain_file(
329
319
chain : impl AsRef < Path > ,
330
320
) -> io:: Result < ServerConfig > {
331
321
let cert = tokio:: fs:: read ( cert. as_ref ( ) ) . await ?;
332
- let cert = rustls_pemfile:: certs ( & mut cert. as_ref ( ) )
333
- . map ( |it| it. map ( |it| rustls:: Certificate ( it. to_vec ( ) ) ) )
334
- . collect :: < Result < Vec < _ > , _ > > ( ) ?;
322
+ let cert = rustls_pemfile:: certs ( & mut cert. as_ref ( ) ) . collect :: < Result < Vec < _ > , _ > > ( ) ?;
335
323
let key = tokio:: fs:: read ( chain. as_ref ( ) ) . await ?;
336
- let key_cert: rustls:: PrivateKey = match rustls_pemfile:: read_one ( & mut key. as_ref ( ) ) ?
337
- . ok_or_else ( || io_other ( "could not parse pem file" ) ) ?
338
- {
339
- Item :: Pkcs8Key ( key) => Ok ( rustls:: PrivateKey ( key. secret_pkcs8_der ( ) . to_vec ( ) . into ( ) ) ) ,
340
- x => Err ( io_other ( format ! (
341
- "invalid certificate format, received: {x:?}"
342
- ) ) ) ,
343
- } ?;
324
+ let key_cert = rustls_pemfile:: private_key ( & mut key. as_ref ( ) ) ?
325
+ . ok_or_else ( || io_other ( "could not parse pem file" ) ) ?;
344
326
345
327
ServerConfig :: builder ( )
346
- . with_safe_defaults ( )
347
328
. with_no_client_auth ( )
348
329
. with_single_cert ( cert, key_cert)
349
330
. map_err ( |_| io_other ( "invalid certificate" ) )
@@ -361,17 +342,10 @@ mod tests {
361
342
use http_body_util:: BodyExt ;
362
343
use hyper:: client:: conn:: http1:: { handshake, SendRequest } ;
363
344
use hyper_util:: rt:: TokioIo ;
364
- use rustls:: {
365
- client:: { ServerCertVerified , ServerCertVerifier } ,
366
- Certificate , ClientConfig , ServerName ,
367
- } ;
368
- use std:: {
369
- convert:: TryFrom ,
370
- io,
371
- net:: SocketAddr ,
372
- sync:: Arc ,
373
- time:: { Duration , SystemTime } ,
374
- } ;
345
+ use rustls:: client:: danger:: { HandshakeSignatureValid , ServerCertVerified , ServerCertVerifier } ;
346
+ use rustls:: pki_types:: { CertificateDer , ServerName } ;
347
+ use rustls:: { ClientConfig , SignatureScheme } ;
348
+ use std:: { io, net:: SocketAddr , sync:: Arc , time:: Duration } ;
375
349
use tokio:: time:: sleep;
376
350
use tokio:: { net:: TcpStream , task:: JoinHandle , time:: timeout} ;
377
351
use tokio_rustls:: TlsConnector ;
@@ -551,13 +525,15 @@ mod tests {
551
525
( handle, server_task, addr)
552
526
}
553
527
554
- async fn get_first_cert ( addr : SocketAddr ) -> Certificate {
528
+ async fn get_first_cert ( addr : SocketAddr ) -> CertificateDer < ' static > {
555
529
let stream = TcpStream :: connect ( addr) . await . unwrap ( ) ;
556
530
let tls_stream = tls_connector ( ) . connect ( dns_name ( ) , stream) . await . unwrap ( ) ;
557
531
558
532
let ( _io, client_connection) = tls_stream. into_inner ( ) ;
559
533
560
- client_connection. peer_certificates ( ) . unwrap ( ) [ 0 ] . clone ( )
534
+ client_connection. peer_certificates ( ) . unwrap ( ) [ 0 ]
535
+ . clone ( )
536
+ . into_owned ( )
561
537
}
562
538
563
539
async fn connect ( addr : SocketAddr ) -> ( SendRequest < Body > , JoinHandle < ( ) > ) {
@@ -585,24 +561,50 @@ mod tests {
585
561
}
586
562
587
563
fn tls_connector ( ) -> TlsConnector {
564
+ #[ derive( Debug ) ]
588
565
struct NoVerify ;
589
566
590
567
impl ServerCertVerifier for NoVerify {
591
568
fn verify_server_cert (
592
569
& self ,
593
- _end_entity : & Certificate ,
594
- _intermediates : & [ Certificate ] ,
595
- _server_name : & ServerName ,
596
- _scts : & mut dyn Iterator < Item = & [ u8 ] > ,
570
+ _end_entity : & CertificateDer < ' _ > ,
571
+ _intermediates : & [ CertificateDer < ' _ > ] ,
572
+ _server_name : & ServerName < ' _ > ,
597
573
_ocsp_response : & [ u8 ] ,
598
- _now : SystemTime ,
574
+ _now : rustls :: pki_types :: UnixTime ,
599
575
) -> Result < ServerCertVerified , rustls:: Error > {
600
576
Ok ( ServerCertVerified :: assertion ( ) )
601
577
}
578
+
579
+ fn verify_tls12_signature (
580
+ & self ,
581
+ _message : & [ u8 ] ,
582
+ _cert : & CertificateDer < ' _ > ,
583
+ _dss : & rustls:: DigitallySignedStruct ,
584
+ ) -> Result < HandshakeSignatureValid , rustls:: Error > {
585
+ Ok ( HandshakeSignatureValid :: assertion ( ) )
586
+ }
587
+
588
+ fn verify_tls13_signature (
589
+ & self ,
590
+ _message : & [ u8 ] ,
591
+ _cert : & CertificateDer < ' _ > ,
592
+ _dss : & rustls:: DigitallySignedStruct ,
593
+ ) -> Result < HandshakeSignatureValid , rustls:: Error > {
594
+ Ok ( HandshakeSignatureValid :: assertion ( ) )
595
+ }
596
+
597
+ fn supported_verify_schemes ( & self ) -> Vec < SignatureScheme > {
598
+ vec ! [
599
+ SignatureScheme :: RSA_PKCS1_SHA256 ,
600
+ SignatureScheme :: RSA_PSS_SHA256 ,
601
+ SignatureScheme :: ECDSA_NISTP256_SHA256 ,
602
+ ]
603
+ }
602
604
}
603
605
604
606
let mut client_config = ClientConfig :: builder ( )
605
- . with_safe_defaults ( )
607
+ . dangerous ( )
606
608
. with_custom_certificate_verifier ( Arc :: new ( NoVerify ) )
607
609
. with_no_client_auth ( ) ;
608
610
@@ -611,7 +613,7 @@ mod tests {
611
613
TlsConnector :: from ( Arc :: new ( client_config) )
612
614
}
613
615
614
- fn dns_name ( ) -> ServerName {
616
+ fn dns_name ( ) -> ServerName < ' static > {
615
617
ServerName :: try_from ( "localhost" ) . unwrap ( )
616
618
}
617
619
}
0 commit comments