Skip to content

Commit 71ee9ab

Browse files
committed
Resolves PR comments and failing crypto provider initialisation
1 parent 5b91c9a commit 71ee9ab

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

runtime/swimos_remote/src/tls/net/server.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
use std::sync::OnceLock;
1516
use std::{net::SocketAddr, sync::Arc};
1617

1718
use crate::net::{
@@ -22,6 +23,7 @@ use futures::{
2223
stream::{unfold, BoxStream, FuturesUnordered},
2324
Future, FutureExt, Stream, StreamExt, TryStreamExt,
2425
};
26+
use rustls::crypto::CryptoProvider;
2527
use rustls::pki_types::PrivateKeyDer;
2628
use rustls::KeyLogFile;
2729
use rustls_pemfile::Item;
@@ -34,6 +36,22 @@ use crate::tls::{
3436
maybe::MaybeTlsStream,
3537
};
3638

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+
3755
/// [`ServerConnections`] implementation that only supports secure connections.
3856
#[derive(Clone)]
3957
pub struct RustlsServerNetworking {
@@ -92,11 +110,13 @@ impl TryFrom<ServerConfig> for RustlsServerNetworking {
92110
_ => return Err(TlsError::InvalidPrivateKey),
93111
}
94112
}
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+
}
97116
};
98117

99-
let mut config = rustls::ServerConfig::builder()
118+
let mut config = rustls::ServerConfig::builder_with_provider(provider())
119+
.with_safe_default_protocol_versions()?
100120
.with_no_client_auth()
101121
.with_single_cert(chain, server_key)
102122
.expect("Invalid certs or private key.");

0 commit comments

Comments
 (0)