12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
+ use std:: sync:: OnceLock ;
15
16
use std:: { net:: SocketAddr , sync:: Arc } ;
16
17
17
18
use crate :: net:: {
@@ -22,6 +23,7 @@ use futures::{
22
23
stream:: { unfold, BoxStream , FuturesUnordered } ,
23
24
Future , FutureExt , Stream , StreamExt , TryStreamExt ,
24
25
} ;
26
+ use rustls:: crypto:: CryptoProvider ;
25
27
use rustls:: pki_types:: PrivateKeyDer ;
26
28
use rustls:: KeyLogFile ;
27
29
use rustls_pemfile:: Item ;
@@ -34,6 +36,22 @@ use crate::tls::{
34
36
maybe:: MaybeTlsStream ,
35
37
} ;
36
38
39
+ static PROVIDER : OnceLock < Arc < CryptoProvider > > = OnceLock :: new ( ) ;
40
+
41
+ fn provider ( ) -> Arc < CryptoProvider > {
42
+ PROVIDER
43
+ . get_or_init ( || {
44
+ let provider = rustls:: crypto:: aws_lc_rs:: default_provider ( ) ;
45
+ // This will fail if the provider has been initialised elsewhere unexpectedly.
46
+ provider
47
+ . clone ( )
48
+ . install_default ( )
49
+ . expect ( "Crypto Provider has already been initialised elsewhere." ) ;
50
+ Arc :: new ( provider)
51
+ } )
52
+ . clone ( )
53
+ }
54
+
37
55
/// [`ServerConnections`] implementation that only supports secure connections.
38
56
#[ derive( Clone ) ]
39
57
pub struct RustlsServerNetworking {
@@ -92,11 +110,13 @@ impl TryFrom<ServerConfig> for RustlsServerNetworking {
92
110
_ => return Err ( TlsError :: InvalidPrivateKey ) ,
93
111
}
94
112
}
95
- CertFormat :: Der => PrivateKeyDer :: try_from ( body)
96
- . map_err ( |e| TlsError :: BadCertificate ( rustls:: Error :: General ( e. to_string ( ) ) ) ) ?,
113
+ CertFormat :: Der => {
114
+ PrivateKeyDer :: try_from ( body) . map_err ( |_| TlsError :: InvalidPrivateKey ) ?
115
+ }
97
116
} ;
98
117
99
- let mut config = rustls:: ServerConfig :: builder ( )
118
+ let mut config = rustls:: ServerConfig :: builder_with_provider ( provider ( ) )
119
+ . with_safe_default_protocol_versions ( ) ?
100
120
. with_no_client_auth ( )
101
121
. with_single_cert ( chain, server_key)
102
122
. expect ( "Invalid certs or private key." ) ;
0 commit comments