use std::sync::Arc;
#[cfg(any(
feature = "crypto-aws-lc-rs",
feature = "crypto-ring",
feature = "crypto-openssl",
feature = "crypto-fips",
))]
use std::sync::Once;
use rustls::crypto::CryptoProvider;
#[cfg(not(any(
feature = "crypto-aws-lc-rs",
feature = "crypto-ring",
feature = "crypto-openssl",
feature = "crypto-fips",
)))]
compile_error!(
"magnetar: enable at least one of crypto-{aws-lc-rs,ring,openssl,fips} \
on the magnetar / magnetar-runtime-moonpool crate"
);
#[cfg(feature = "crypto-aws-lc-rs")]
pub fn install_default_provider() {
static ONCE: Once = Once::new();
ONCE.call_once(|| {
let _ = rustls::crypto::aws_lc_rs::default_provider().install_default();
});
}
#[cfg(all(not(feature = "crypto-aws-lc-rs"), feature = "crypto-fips"))]
pub fn install_default_provider() {
static ONCE: Once = Once::new();
ONCE.call_once(|| {
let _ = rustls::crypto::default_fips_provider().install_default();
});
}
#[cfg(all(
not(any(feature = "crypto-aws-lc-rs", feature = "crypto-fips")),
feature = "crypto-openssl"
))]
pub fn install_default_provider() {
static ONCE: Once = Once::new();
ONCE.call_once(|| {
let _ = rustls_openssl::default_provider().install_default();
});
}
#[cfg(all(
not(any(
feature = "crypto-aws-lc-rs",
feature = "crypto-fips",
feature = "crypto-openssl"
)),
feature = "crypto-ring"
))]
pub fn install_default_provider() {
static ONCE: Once = Once::new();
ONCE.call_once(|| {
let _ = rustls::crypto::ring::default_provider().install_default();
});
}
#[must_use]
pub fn active_provider() -> Arc<CryptoProvider> {
install_default_provider();
CryptoProvider::get_default()
.cloned()
.expect("install_default_provider() must populate the global rustls CryptoProvider")
}
#[cfg(test)]
mod tests {
use super::{active_provider, install_default_provider};
#[test]
fn install_default_provider_is_idempotent() {
install_default_provider();
install_default_provider();
install_default_provider();
}
#[test]
fn active_provider_returns_a_valid_provider() {
let provider = active_provider();
assert!(
!provider.cipher_suites.is_empty(),
"active rustls provider must expose at least one cipher suite"
);
}
}