@@ -33,8 +33,10 @@ use crate::{
3333 server:: { io_other, Server } ,
3434} ;
3535use 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+ } ;
3840use std:: time:: Duration ;
3941use std:: { fmt, io, net:: SocketAddr , path:: Path , sync:: Arc } ;
4042use tokio:: {
@@ -172,10 +174,8 @@ impl RustlsConfig {
172174 /// The certificate must be DER-encoded X.509.
173175 ///
174176 /// 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) ?;
179179 let inner = Arc :: new ( ArcSwap :: from_pointee ( server_config) ) ;
180180
181181 Ok ( Self { inner } )
@@ -218,10 +218,12 @@ impl RustlsConfig {
218218 /// The certificate must be DER-encoded X.509.
219219 ///
220220 /// 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) ?;
225227 let inner = Arc :: new ( server_config) ;
226228
227229 self . inner . store ( inner) ;
@@ -278,12 +280,10 @@ impl fmt::Debug for RustlsConfig {
278280 }
279281}
280282
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 ( ) ;
284285
285286 let mut config = ServerConfig :: builder ( )
286- . with_safe_defaults ( )
287287 . with_no_client_auth ( )
288288 . with_single_cert ( cert, key)
289289 . map_err ( io_other) ?;
@@ -294,24 +294,14 @@ fn config_from_der(cert: Vec<Vec<u8>>, key: Vec<u8>) -> io::Result<ServerConfig>
294294}
295295
296296fn 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" ) ) ?;
313303
314- config_from_der ( cert, key_vec . pop ( ) . unwrap ( ) )
304+ config_from_der ( cert, key )
315305}
316306
317307async fn config_from_pem_file (
@@ -329,21 +319,12 @@ async fn config_from_pem_chain_file(
329319 chain : impl AsRef < Path > ,
330320) -> io:: Result < ServerConfig > {
331321 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 < _ > , _ > > ( ) ?;
335323 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" ) ) ?;
344326
345327 ServerConfig :: builder ( )
346- . with_safe_defaults ( )
347328 . with_no_client_auth ( )
348329 . with_single_cert ( cert, key_cert)
349330 . map_err ( |_| io_other ( "invalid certificate" ) )
@@ -361,17 +342,10 @@ mod tests {
361342 use http_body_util:: BodyExt ;
362343 use hyper:: client:: conn:: http1:: { handshake, SendRequest } ;
363344 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 } ;
375349 use tokio:: time:: sleep;
376350 use tokio:: { net:: TcpStream , task:: JoinHandle , time:: timeout} ;
377351 use tokio_rustls:: TlsConnector ;
@@ -551,13 +525,15 @@ mod tests {
551525 ( handle, server_task, addr)
552526 }
553527
554- async fn get_first_cert ( addr : SocketAddr ) -> Certificate {
528+ async fn get_first_cert ( addr : SocketAddr ) -> CertificateDer < ' static > {
555529 let stream = TcpStream :: connect ( addr) . await . unwrap ( ) ;
556530 let tls_stream = tls_connector ( ) . connect ( dns_name ( ) , stream) . await . unwrap ( ) ;
557531
558532 let ( _io, client_connection) = tls_stream. into_inner ( ) ;
559533
560- client_connection. peer_certificates ( ) . unwrap ( ) [ 0 ] . clone ( )
534+ client_connection. peer_certificates ( ) . unwrap ( ) [ 0 ]
535+ . clone ( )
536+ . into_owned ( )
561537 }
562538
563539 async fn connect ( addr : SocketAddr ) -> ( SendRequest < Body > , JoinHandle < ( ) > ) {
@@ -585,24 +561,50 @@ mod tests {
585561 }
586562
587563 fn tls_connector ( ) -> TlsConnector {
564+ #[ derive( Debug ) ]
588565 struct NoVerify ;
589566
590567 impl ServerCertVerifier for NoVerify {
591568 fn verify_server_cert (
592569 & 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 < ' _ > ,
597573 _ocsp_response : & [ u8 ] ,
598- _now : SystemTime ,
574+ _now : rustls :: pki_types :: UnixTime ,
599575 ) -> Result < ServerCertVerified , rustls:: Error > {
600576 Ok ( ServerCertVerified :: assertion ( ) )
601577 }
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+ }
602604 }
603605
604606 let mut client_config = ClientConfig :: builder ( )
605- . with_safe_defaults ( )
607+ . dangerous ( )
606608 . with_custom_certificate_verifier ( Arc :: new ( NoVerify ) )
607609 . with_no_client_auth ( ) ;
608610
@@ -611,7 +613,7 @@ mod tests {
611613 TlsConnector :: from ( Arc :: new ( client_config) )
612614 }
613615
614- fn dns_name ( ) -> ServerName {
616+ fn dns_name ( ) -> ServerName < ' static > {
615617 ServerName :: try_from ( "localhost" ) . unwrap ( )
616618 }
617619}
0 commit comments