@@ -1630,14 +1630,15 @@ pub(crate) mod meta {
1630
1630
1631
1631
use anyhow:: Context ;
1632
1632
use anyhow:: Result ;
1633
- use rustls:: RootCertStore ;
1634
1633
use tokio:: net:: TcpListener ;
1635
1634
use tokio:: net:: TcpStream ;
1636
1635
use tokio_rustls:: TlsAcceptor ;
1637
1636
use tokio_rustls:: TlsConnector ;
1638
1637
use tokio_rustls:: client:: TlsStream ;
1639
- use tokio_rustls:: rustls:: Certificate ;
1640
- use tokio_rustls:: rustls:: PrivateKey ;
1638
+ use tokio_rustls:: rustls:: RootCertStore ;
1639
+ use tokio_rustls:: rustls:: pki_types:: CertificateDer ;
1640
+ use tokio_rustls:: rustls:: pki_types:: PrivateKeyDer ;
1641
+ use tokio_rustls:: rustls:: pki_types:: ServerName ;
1641
1642
1642
1643
use super :: * ;
1643
1644
use crate :: RemoteMessage ;
@@ -1665,24 +1666,17 @@ pub(crate) mod meta {
1665
1666
1666
1667
/// Returns the root cert store
1667
1668
fn root_cert_store ( ) -> Result < RootCertStore > {
1668
- let mut root_cert_store = rustls:: RootCertStore :: empty ( ) ;
1669
+ let mut root_cert_store = tokio_rustls :: rustls:: RootCertStore :: empty ( ) ;
1669
1670
let ca_cert_path =
1670
1671
std:: env:: var_os ( THRIFT_TLS_SRV_CA_PATH_ENV ) . unwrap_or ( DEFAULT_SRV_CA_PATH . into ( ) ) ;
1671
1672
let ca_certs = rustls_pemfile:: certs ( & mut BufReader :: new (
1672
1673
File :: open ( ca_cert_path) . context ( "Failed to open {ca_cert_path:?}" ) ?,
1673
1674
) ) ?;
1674
- let trust_anchors = ca_certs. iter ( ) . filter_map ( |cert| {
1675
- webpki:: TrustAnchor :: try_from_cert_der ( & cert[ ..] )
1676
- . map ( |ta| {
1677
- rustls:: OwnedTrustAnchor :: from_subject_spki_name_constraints (
1678
- ta. subject ,
1679
- ta. spki ,
1680
- ta. name_constraints ,
1681
- )
1682
- } )
1683
- . ok ( )
1684
- } ) ;
1685
- root_cert_store. add_trust_anchors ( trust_anchors) ;
1675
+ for cert in ca_certs {
1676
+ root_cert_store
1677
+ . add ( cert. into ( ) )
1678
+ . context ( "Failed to add certificate to root store" ) ?;
1679
+ }
1686
1680
Ok ( root_cert_store)
1687
1681
}
1688
1682
@@ -1693,7 +1687,7 @@ pub(crate) mod meta {
1693
1687
File :: open ( server_cert_path) . context ( "failed to open {server_cert_path}" ) ?,
1694
1688
) ) ?
1695
1689
. into_iter ( )
1696
- . map ( Certificate )
1690
+ . map ( CertificateDer :: from )
1697
1691
. collect ( ) ;
1698
1692
// certs are good here
1699
1693
let server_key_path = DEFAULT_SERVER_PEM_PATH ;
@@ -1712,22 +1706,28 @@ pub(crate) mod meta {
1712
1706
} ;
1713
1707
} ;
1714
1708
1715
- let config = rustls:: ServerConfig :: builder ( ) . with_safe_defaults ( ) ;
1709
+ let config = tokio_rustls :: rustls:: ServerConfig :: builder ( ) ;
1716
1710
1717
1711
let config = if enforce_client_tls {
1718
- let client_cert_verifier = Arc :: new ( rustls:: server:: AllowAnyAuthenticatedClient :: new (
1719
- root_cert_store ( ) ?,
1720
- ) ) ;
1712
+ let client_cert_verifier = tokio_rustls:: rustls:: server:: WebPkiClientVerifier :: builder (
1713
+ Arc :: new ( root_cert_store ( ) ?) ,
1714
+ )
1715
+ . build ( )
1716
+ . map_err ( |e| anyhow:: anyhow!( "Failed to build client verifier: {}" , e) ) ?;
1721
1717
config. with_client_cert_verifier ( client_cert_verifier)
1722
1718
} else {
1723
1719
config. with_no_client_auth ( )
1724
1720
}
1725
- . with_single_cert ( certs, PrivateKey ( key) ) ?;
1721
+ . with_single_cert (
1722
+ certs,
1723
+ PrivateKeyDer :: try_from ( key)
1724
+ . map_err ( |_| anyhow:: anyhow!( "Invalid private key format" ) ) ?,
1725
+ ) ?;
1726
1726
1727
1727
Ok ( TlsAcceptor :: from ( Arc :: new ( config) ) )
1728
1728
}
1729
1729
1730
- fn load_client_pem ( ) -> Result < Option < ( Vec < rustls :: Certificate > , rustls :: PrivateKey ) > > {
1730
+ fn load_client_pem ( ) -> Result < Option < ( Vec < CertificateDer < ' static > > , PrivateKeyDer < ' static > ) > > {
1731
1731
let Some ( cert_path) = std:: env:: var_os ( THRIFT_TLS_CL_CERT_PATH_ENV ) else {
1732
1732
return Ok ( None ) ;
1733
1733
} ;
@@ -1738,7 +1738,7 @@ pub(crate) mod meta {
1738
1738
File :: open ( cert_path) . context ( "failed to open {cert_path}" ) ?,
1739
1739
) ) ?
1740
1740
. into_iter ( )
1741
- . map ( rustls :: Certificate )
1741
+ . map ( CertificateDer :: from )
1742
1742
. collect ( ) ;
1743
1743
let mut key_reader =
1744
1744
BufReader :: new ( File :: open ( key_path) . context ( "failed to open {key_path}" ) ?) ;
@@ -1752,15 +1752,18 @@ pub(crate) mod meta {
1752
1752
} ;
1753
1753
} ;
1754
1754
// Certs are verified to be good here.
1755
- Ok ( Some ( ( certs, rustls:: PrivateKey ( key) ) ) )
1755
+ Ok ( Some ( (
1756
+ certs,
1757
+ PrivateKeyDer :: try_from ( key)
1758
+ . map_err ( |_| anyhow:: anyhow!( "Invalid private key format" ) ) ?,
1759
+ ) ) )
1756
1760
}
1757
1761
1758
1762
/// Creates a TLS connector by looking for necessary certs and keys in a Meta server environment.
1759
1763
fn tls_connector ( ) -> Result < TlsConnector > {
1760
1764
// TODO (T208180540): try to simplify the logic here.
1761
- let config = rustls:: ClientConfig :: builder ( )
1762
- . with_safe_defaults ( )
1763
- . with_root_certificates ( root_cert_store ( ) ?) ;
1765
+ let config = tokio_rustls:: rustls:: ClientConfig :: builder ( )
1766
+ . with_root_certificates ( Arc :: new ( root_cert_store ( ) ?) ) ;
1764
1767
let result = load_client_pem ( ) ?;
1765
1768
let config = if let Some ( ( certs, key) ) = result {
1766
1769
config
@@ -1772,9 +1775,9 @@ pub(crate) mod meta {
1772
1775
Ok ( TlsConnector :: from ( Arc :: new ( config) ) )
1773
1776
}
1774
1777
1775
- fn tls_connector_config ( peer_host_name : & str ) -> Result < ( TlsConnector , rustls :: ServerName ) > {
1778
+ fn tls_connector_config ( peer_host_name : & str ) -> Result < ( TlsConnector , ServerName < ' static > ) > {
1776
1779
let connector = tls_connector ( ) ?;
1777
- let server_name = rustls :: ServerName :: try_from ( peer_host_name) ?;
1780
+ let server_name = ServerName :: try_from ( peer_host_name. to_string ( ) ) ?;
1778
1781
Ok ( ( connector, server_name) )
1779
1782
}
1780
1783
0 commit comments