Skip to content

Commit 333db11

Browse files
committed
Defer panic on missing crypto provider until Rtc init
1 parent 7f72e2e commit 333db11

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Unreleased
22

3+
* Defer panic on missing crypto provider until Rtc init #792
4+
35
# 0.14.1
46

57
* Configure sctp to not give up INIT/data retransmissions #791

src/lib.rs

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,10 +1073,21 @@ impl Rtc {
10731073
}
10741074

10751075
pub(crate) fn new_from_config(config: RtcConfig) -> Result<Self, RtcError> {
1076+
let crypto_provider = config
1077+
.crypto_provider
1078+
.clone()
1079+
// If crypto_provider is not set in config, check process default
1080+
.or_else(|| CryptoProvider::get_default().cloned().map(Arc::new))
1081+
// Or fall back on feature flags
1082+
.or_else(|| Some(Arc::new(from_feature_flags())))
1083+
// from_feature_flags panics already, so we should never see
1084+
// this expect message.
1085+
.expect("a crash earlier if no crypto provider was set");
1086+
10761087
let session = Session::new(&config);
10771088

10781089
let local_creds = config.local_ice_credentials.unwrap_or_else(IceCreds::new);
1079-
let mut ice = IceAgent::new(local_creds, config.crypto_provider.sha1_hmac_provider);
1090+
let mut ice = IceAgent::new(local_creds, crypto_provider.sha1_hmac_provider);
10801091
if config.ice_lite {
10811092
ice.set_ice_lite(config.ice_lite);
10821093
}
@@ -1095,22 +1106,20 @@ impl Rtc {
10951106

10961107
let dtls_cert = config
10971108
.dtls_cert
1098-
.or_else(|| config.crypto_provider.dtls_provider.generate_certificate())
1109+
.or_else(|| crypto_provider.dtls_provider.generate_certificate())
10991110
.expect(
11001111
"No DTLS certificate provided and the crypto provider cannot generate one. \
11011112
Either provide a certificate via RtcConfig::set_dtls_cert or use a \
11021113
crypto provider that supports certificate generation.",
11031114
);
11041115

1105-
let crypto_provider = config.crypto_provider.clone();
1106-
11071116
Ok(Rtc {
11081117
alive: true,
11091118
ice,
11101119
dtls: Dtls::new(
11111120
&dtls_cert,
1112-
config.crypto_provider.dtls_provider,
1113-
config.crypto_provider.sha256_provider,
1121+
crypto_provider.dtls_provider,
1122+
crypto_provider.sha256_provider,
11141123
)
11151124
.expect("DTLS to init without problem"),
11161125
dtls_connected: false,
@@ -1893,7 +1902,7 @@ impl Rtc {
18931902
#[derive(Debug, Clone)]
18941903
pub struct RtcConfig {
18951904
local_ice_credentials: Option<IceCreds>,
1896-
crypto_provider: Arc<crate::crypto::CryptoProvider>,
1905+
crypto_provider: Option<Arc<crate::crypto::CryptoProvider>>,
18971906
dtls_cert: Option<config::DtlsCert>,
18981907
fingerprint_verification: bool,
18991908
ice_lite: bool,
@@ -1941,18 +1950,18 @@ impl RtcConfig {
19411950

19421951
/// Set the crypto provider.
19431952
///
1944-
/// This overrides what is set in [`crate::crypto::CryptoProvider::install_default()`].
1953+
/// This overrides what is set in [`crate::crypto::CryptoProvider::install_process_default()`].
19451954
pub fn set_crypto_provider(mut self, p: Arc<CryptoProvider>) -> Self {
1946-
self.crypto_provider = p;
1955+
self.crypto_provider = Some(p);
19471956
self
19481957
}
19491958

1950-
/// The configured crypto provider.
1959+
/// The configured crypto provider, if explicitly set.
19511960
///
1952-
/// Defaults to what's set in [`crate::crypto::CryptoProvider::get_default()`] followed
1953-
/// by a fallback to the default OpenSSL provider.
1954-
pub fn crypto_provider(&self) -> &Arc<CryptoProvider> {
1955-
&self.crypto_provider
1961+
/// Returns `None` if not explicitly set via [`Self::set_crypto_provider()`].
1962+
/// When `None`, the process default will be checked when building the [`Rtc`] instance.
1963+
pub fn crypto_provider(&self) -> Option<&Arc<CryptoProvider>> {
1964+
self.crypto_provider.as_ref()
19561965
}
19571966

19581967
/// Returns the configured DTLS certificate, if set.
@@ -2427,13 +2436,9 @@ impl BweConfig {
24272436

24282437
impl Default for RtcConfig {
24292438
fn default() -> Self {
2430-
let crypto_provider = CryptoProvider::get_default()
2431-
.cloned()
2432-
.unwrap_or_else(from_feature_flags);
2433-
24342439
Self {
24352440
local_ice_credentials: None,
2436-
crypto_provider: Arc::new(crypto_provider),
2441+
crypto_provider: None,
24372442
dtls_cert: None,
24382443
fingerprint_verification: true,
24392444
ice_lite: false,

0 commit comments

Comments
 (0)