use crate::error::{Failure, Result};
pub const PROVIDER_NAME: &str = if cfg!(feature = "tls") {
"rustcrypto"
} else {
"none"
};
pub const PROVIDER_IS_AUDITED: bool = false;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ProtocolFloor {
#[default]
Twelve,
Thirteen,
}
impl ProtocolFloor {
#[must_use]
pub fn from_word(word: &str) -> Self {
match word {
"1.3" => Self::Thirteen,
_ => Self::Twelve,
}
}
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Twelve => "1.2",
Self::Thirteen => "1.3",
}
}
}
#[cfg(feature = "tls")]
pub fn install(floor: ProtocolFloor) -> Result<()> {
let mut provider = rustls_rustcrypto::provider();
if floor == ProtocolFloor::Thirteen {
provider
.cipher_suites
.retain(|suite| matches!(suite, rustls::SupportedCipherSuite::Tls13(_)));
if provider.cipher_suites.is_empty() {
return Err(no_suite_for_floor(floor));
}
}
let _ = provider.install_default();
Ok(())
}
#[cfg(not(feature = "tls"))]
pub fn install(_floor: ProtocolFloor) -> Result<()> {
Ok(())
}
#[must_use]
pub fn no_suite_for_floor(floor: ProtocolFloor) -> Failure {
Failure::config(
"tls.no_suite_for_floor",
format!(
"this build carries no cipher suite for protocol {}",
floor.as_str()
),
)
.with_detail("detail", floor.as_str())
.with_hint("lower tls.min_version, or install a build whose provider offers that version")
}
#[must_use]
pub fn no_provider() -> Failure {
Failure::config(
"tls.no_provider",
"no cryptography provider could be installed for the encrypted channel",
)
.with_hint("reinstall with the default features, which carry the encrypted channel")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_build_names_exactly_one_provider() {
assert!(matches!(PROVIDER_NAME, "ring" | "rustcrypto" | "none"));
}
#[test]
fn only_the_widely_deployed_provider_claims_an_audit() {
if PROVIDER_IS_AUDITED {
assert_eq!(PROVIDER_NAME, "ring");
} else {
assert_ne!(PROVIDER_NAME, "ring");
}
}
#[test]
fn installing_twice_is_not_a_failure() {
install(ProtocolFloor::default()).expect("first install succeeds");
install(ProtocolFloor::default()).expect("second install is a no-op, not a failure");
}
#[test]
fn the_floor_is_read_from_the_published_word() {
assert_eq!(ProtocolFloor::from_word("1.3"), ProtocolFloor::Thirteen);
assert_eq!(ProtocolFloor::from_word("1.2"), ProtocolFloor::Twelve);
assert_eq!(ProtocolFloor::default(), ProtocolFloor::Twelve);
assert_eq!(ProtocolFloor::Thirteen.as_str(), "1.3");
}
#[cfg(feature = "tls")]
#[test]
fn raising_the_floor_leaves_no_older_suite_to_negotiate() {
let full = rustls_rustcrypto::provider();
let older = full
.cipher_suites
.iter()
.filter(|suite| !matches!(suite, rustls::SupportedCipherSuite::Tls13(_)))
.count();
let newer = full.cipher_suites.len() - older;
assert!(
older > 0,
"the permissive floor must offer the older version"
);
assert!(newer > 0, "raising the floor must leave something to offer");
}
#[test]
fn the_refusal_carries_a_next_step() {
let failure = no_provider();
assert_eq!(failure.category(), crate::error::Category::Config);
assert!(failure.hint().is_some());
assert!(!failure.is_retryable());
}
}