Skip to content

Commit 1f0f78d

Browse files
committed
feat: add support for aws-lc-rs alongside ring
1 parent aae46b7 commit 1f0f78d

10 files changed

Lines changed: 60 additions & 7 deletions

File tree

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ categories = ["network-programming"]
3737
rtc = { version = "0.20.0-alpha.1", path = "rtc" }
3838
signal = { version = "0.20.0-alpha.1", path = "examples/examples/signal", package = "rtc-signal" }
3939
datachannel = { version = "0.20.0-alpha.1", path = "rtc-datachannel", package = "rtc-datachannel" }
40-
dtls = { version = "0.20.0-alpha.1", path = "rtc-dtls", package = "rtc-dtls" }
40+
dtls = { version = "0.20.0-alpha.1", path = "rtc-dtls", package = "rtc-dtls", default-features = false }
4141
ice = { version = "0.20.0-alpha.1", path = "rtc-ice", package = "rtc-ice" }
4242
interceptor = { version = "0.20.0-alpha.1", path = "rtc-interceptor", package = "rtc-interceptor" }
4343
interceptor-derive = { version = "0.20.0-alpha.1", path = "rtc-interceptor-derive", package = "rtc-interceptor-derive" }
@@ -59,6 +59,7 @@ byteorder = "1.5.0"
5959
log = "0.4.29"
6060
rcgen = { version = "0.14.6", features = ["pem", "x509-parser"] }
6161
ring = "0.17.14"
62+
aws-lc-rs = { version = "1.17.0", default-features = false, features = ["aws-lc-sys"] }
6263
rand = "0.9.2"
6364
serde = { version = "1.0.228", features = ["derive"] }
6465
thiserror = "2.0.17"

rtc-dtls/Cargo.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ x25519-dalek = { version = "2.0.1", features = ["static_secrets"] }
3232
x509-parser = "0.16.0"
3333
der-parser = "9.0.0"
3434
rcgen.workspace = true
35-
ring.workspace = true
36-
rustls = { version = "0.23.27", default-features = false, features = ["std", "ring"] }
35+
ring = { workspace = true, optional = true }
36+
aws-lc-rs = { workspace = true, optional = true }
37+
rustls = { version = "0.23.27", default-features = false, features = ["std"] }
3738
rkyv = "0.8"
3839
bytecheck = "0.8"
3940
subtle = "2.5.0"
@@ -52,7 +53,10 @@ ctrlc.workspace = true
5253
futures = "0.3.30"
5354

5455
[features]
56+
default = ["ring"]
5557
pem = ["dep:pem"]
58+
ring = ["dep:ring", "rustls/ring"]
59+
aws-lc-rs = ["dep:aws-lc-rs", "rustls/aws-lc-rs"]
5660

5761
#[[example]]
5862
#name = "dtls_chat_server"

rtc-dtls/src/crypto/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ impl Clone for CryptoPrivateKey {
191191
EcdsaKeyPair::from_pkcs8(
192192
&ring::signature::ECDSA_P256_SHA256_ASN1_SIGNING,
193193
&self.serialized_der,
194+
#[cfg(feature = "ring")]
194195
&SystemRandom::new(),
195196
)
196197
.unwrap(),
@@ -232,6 +233,7 @@ impl CryptoPrivateKey {
232233
EcdsaKeyPair::from_pkcs8(
233234
&ring::signature::ECDSA_P256_SHA256_ASN1_SIGNING,
234235
&serialized_der,
236+
#[cfg(feature = "ring")]
235237
&SystemRandom::new(),
236238
)
237239
.map_err(|e| Error::Other(e.to_string()))?,
@@ -276,7 +278,10 @@ pub(crate) fn generate_key_signature(
276278
}
277279
CryptoPrivateKeyKind::Rsa256(kp) => {
278280
let system_random = SystemRandom::new();
281+
#[cfg(feature = "ring")]
279282
let mut signature = vec![0; kp.public().modulus_len()];
283+
#[cfg(feature = "aws-lc-rs")]
284+
let mut signature = vec![0; kp.public_modulus_len()];
280285
kp.sign(
281286
&ring::signature::RSA_PKCS1_SHA256,
282287
&system_random,
@@ -398,7 +403,10 @@ pub(crate) fn generate_certificate_verify(
398403
}
399404
CryptoPrivateKeyKind::Rsa256(kp) => {
400405
let system_random = SystemRandom::new();
406+
#[cfg(feature = "ring")]
401407
let mut signature = vec![0; kp.public().modulus_len()];
408+
#[cfg(feature = "aws-lc-rs")]
409+
let mut signature = vec![0; kp.public_modulus_len()];
402410
kp.sign(
403411
&ring::signature::RSA_PKCS1_SHA256,
404412
&system_random,

rtc-dtls/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ pub mod state;
2626
use cipher_suite::*;
2727
use extension::extension_use_srtp::SrtpProtectionProfile;
2828

29+
#[cfg(all(feature = "aws-lc-rs", feature = "ring"))]
30+
compile_error!("At most one of the features \"aws-lc-rs\" and \"ring\" can be enabled.");
31+
#[cfg(not(any(feature = "aws-lc-rs", feature = "ring")))]
32+
compile_error!("Exactly one of the features \"aws-lc-rs\" and \"ring\" must be enabled.");
33+
#[cfg(feature = "aws-lc-rs")]
34+
extern crate aws_lc_rs as ring;
35+
2936
pub(crate) fn find_matching_srtp_profile(
3037
a: &[SrtpProtectionProfile],
3138
b: &[SrtpProtectionProfile],

rtc-stun/Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ keywords.workspace = true
1212
categories.workspace = true
1313

1414
[features]
15-
default = []
15+
default = ["ring"]
1616
bench = []
17+
ring = ["dep:ring"]
18+
aws-lc-rs = ["dep:aws-lc-rs"]
1719

1820
[dependencies]
1921
shared = { workspace = true, default-features = false, features = [] }
@@ -26,7 +28,8 @@ rand.workspace = true
2628
base64 = "0.22.1"
2729
subtle = "2.5.0"
2830
crc = "3.0.1"
29-
ring.workspace = true
31+
ring = { workspace = true, optional = true }
32+
aws-lc-rs = { workspace = true, optional = true }
3033
md-5 = "0.10"
3134

3235
[dev-dependencies]

rtc-stun/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,10 @@ pub mod xoraddr;
2121
// IANA assigned ports for "stun" protocol.
2222
pub const DEFAULT_PORT: u16 = 3478;
2323
pub const DEFAULT_TLS_PORT: u16 = 5349;
24+
25+
#[cfg(all(feature = "aws-lc-rs", feature = "ring"))]
26+
compile_error!("At most one of the features \"aws-lc-rs\" and \"ring\" can be enabled.");
27+
#[cfg(not(any(feature = "aws-lc-rs", feature = "ring")))]
28+
compile_error!("Exactly one of the features \"aws-lc-rs\" and \"ring\" must be enabled.");
29+
#[cfg(feature = "aws-lc-rs")]
30+
extern crate aws_lc_rs as ring;

rtc/Cargo.toml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ log.workspace = true
3434
serde = "1"
3535
serde_json = { version = "1", features = [] }
3636
rcgen.workspace = true
37-
ring.workspace = true
37+
ring = { workspace = true, optional = true }
38+
aws-lc-rs = { workspace = true, optional = true }
3839
sha2 = "0.10"
39-
rustls = { version = "0.23.35", default-features = false, features = ["std", "ring"] }
40+
rustls = { version = "0.23.35", default-features = false, features = ["std"] }
4041
url = { version = "2", features = [] }
4142
hex = { version = "0.4", features = [] }
4243
pem = { version = "3", optional = true }
@@ -50,6 +51,10 @@ env_logger.workspace = true
5051
anyhow = "1"
5152

5253
[features]
54+
default = ["ring"]
5355
pem = ["dep:pem", "dtls/pem"]
5456
openssl = ["srtp/openssl"]
5557
vendored-openssl = ["srtp/vendored-openssl"]
58+
ring = ["dep:ring", "dtls/ring", "rustls/ring"]
59+
aws-lc-rs = ["dep:aws-lc-rs", "dtls/aws-lc-rs", "rustls/aws-lc-rs"]
60+
__testing = []

rtc/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,3 +663,10 @@ pub mod media_stream;
663663
pub mod peer_connection;
664664
pub mod rtp_transceiver;
665665
pub mod statistics;
666+
667+
#[cfg(all(feature = "aws-lc-rs", feature = "ring"))]
668+
compile_error!("At most one of the features \"aws-lc-rs\" and \"ring\" can be enabled.");
669+
#[cfg(not(any(feature = "aws-lc-rs", feature = "ring")))]
670+
compile_error!("Exactly one of the features \"aws-lc-rs\" and \"ring\" must be enabled.");
671+
#[cfg(feature = "aws-lc-rs")]
672+
extern crate aws_lc_rs as ring;

rtc/src/peer_connection/certificate/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ use std::time::{Duration, SystemTime};
188188

189189
use dtls::crypto::{CryptoPrivateKey, CryptoPrivateKeyKind};
190190
use rcgen::{CertificateParams, KeyPair};
191+
#[cfg(feature = "ring")]
191192
use ring::rand::SystemRandom;
192193
use ring::rsa;
193194
use ring::signature::{EcdsaKeyPair, Ed25519KeyPair};
@@ -358,6 +359,7 @@ impl RTCCertificate {
358359
EcdsaKeyPair::from_pkcs8(
359360
&ring::signature::ECDSA_P256_SHA256_ASN1_SIGNING,
360361
&serialized_der,
362+
#[cfg(feature = "ring")]
361363
&SystemRandom::new(),
362364
)
363365
.map_err(|e| Error::Other(e.to_string()))?,

rtc/src/peer_connection/transport/dtls/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,15 @@ impl RTCDtlsTransport {
119119
return Err(Error::ErrInvalidDTLSStart);
120120
}
121121

122+
// When testing with `aws-lc-rs`,
123+
// the `webrtc` interop tests uses `ring` for its crypto provider,
124+
// so we need to choose one of them to avoid conflicts.
125+
// We need a custom feature because
126+
// `#[cfg(test)]` is not propagated to this crate in integration tests.
127+
#[cfg(all(feature = "__testing", feature = "aws-lc-rs"))]
128+
let _ = rustls::crypto::aws_lc_rs::default_provider()
129+
.install_default();
130+
122131
self.dtls_role = self.derive_role(ice_role, remote_dtls_parameters.role);
123132

124133
let remote_fingerprints = remote_dtls_parameters.fingerprints;

0 commit comments

Comments
 (0)