Skip to content

Commit 01d84e7

Browse files
committed
Resolves PR comments
1 parent e1a22f4 commit 01d84e7

File tree

3 files changed

+25
-21
lines changed

3 files changed

+25
-21
lines changed

runtime/swimos_remote/src/tls/config/mod.rs

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

15+
use rustls::crypto::CryptoProvider;
16+
use std::sync::Arc;
17+
1518
/// Supported certificate formats for TLS connections.
1619
pub enum CertFormat {
1720
Pem,
@@ -60,9 +63,11 @@ impl PrivateKey {
6063
Self::new(CertFormat::Pem, body)
6164
}
6265
}
63-
/// Combined TLS configuration (both server and client)/
66+
/// Combined TLS configuration (both server and client).
6467
pub struct TlsConfig {
68+
/// Configuration parameters for a TLS client.
6569
pub client: ClientConfig,
70+
/// Configuration parameters for a TLS server.
6671
pub server: ServerConfig,
6772
}
6873

@@ -74,17 +79,26 @@ impl TlsConfig {
7479

7580
/// Configuration parameters for a TLS server.
7681
pub struct ServerConfig {
82+
/// A chain of TLS certificates (starting with the server certificate and ending with the CA).
7783
pub chain: CertChain,
84+
/// An unvalidated private key for a server.
7885
pub key: PrivateKey,
86+
/// Whether to enable a [`KeyLog`] implementation that opens a file whose name is given by the
87+
/// `SSLKEYLOGFILE` environment variable, and writes keys into it. While this may be enabled,
88+
/// if `SSLKEYLOGFILE` is not set, it will do nothing.
7989
pub enable_log_file: bool,
90+
/// Process-wide [`CryptoProvider`] that must already have been installed as the default
91+
/// provider.
92+
pub provider: Arc<CryptoProvider>,
8093
}
8194

8295
impl ServerConfig {
83-
pub fn new(chain: CertChain, key: PrivateKey) -> Self {
96+
pub fn new(chain: CertChain, key: PrivateKey, provider: Arc<CryptoProvider>) -> Self {
8497
ServerConfig {
8598
chain,
8699
key,
87100
enable_log_file: false,
101+
provider,
88102
}
89103
}
90104
}

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

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

15-
use std::sync::OnceLock;
1615
use std::{net::SocketAddr, sync::Arc};
1716

1817
use crate::net::{
@@ -23,7 +22,6 @@ use futures::{
2322
stream::{unfold, BoxStream, FuturesUnordered},
2423
Future, FutureExt, Stream, StreamExt, TryStreamExt,
2524
};
26-
use rustls::crypto::CryptoProvider;
2725
use rustls::pki_types::PrivateKeyDer;
2826
use rustls::KeyLogFile;
2927
use rustls_pemfile::Item;
@@ -36,22 +34,6 @@ use crate::tls::{
3634
maybe::MaybeTlsStream,
3735
};
3836

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::ring::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-
5537
/// [`ServerConnections`] implementation that only supports secure connections.
5638
#[derive(Clone)]
5739
pub struct RustlsServerNetworking {
@@ -92,6 +74,7 @@ impl TryFrom<ServerConfig> for RustlsServerNetworking {
9274
chain: CertChain(certs),
9375
key,
9476
enable_log_file,
77+
provider,
9578
} = config;
9679

9780
let mut chain = vec![];
@@ -115,7 +98,7 @@ impl TryFrom<ServerConfig> for RustlsServerNetworking {
11598
}
11699
};
117100

118-
let mut config = rustls::ServerConfig::builder_with_provider(provider())
101+
let mut config = rustls::ServerConfig::builder_with_provider(provider)
119102
.with_safe_default_protocol_versions()?
120103
.with_no_client_auth()
121104
.with_single_cert(chain, server_key)

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,18 @@ fn make_server_config() -> ServerConfig {
4646
CertificateFile::der(ca_cert),
4747
]);
4848

49+
let provider = rustls::crypto::aws_lc_rs::default_provider();
50+
provider
51+
.clone()
52+
.install_default()
53+
.expect("Crypto Provider has already been initialised elsewhere.");
54+
4955
let key = PrivateKey::der(server_key);
5056
ServerConfig {
5157
chain,
5258
key,
5359
enable_log_file: false,
60+
provider: Arc::new(provider),
5461
}
5562
}
5663

0 commit comments

Comments
 (0)