| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
use std::panic::UnwindSafe; |
| 5 |
|
| 6 |
|
| 7 |
#[derive(Debug, PartialEq, Eq)] |
| 8 |
enum Libcrypto { |
| 9 |
Awslc, |
| 10 |
AwslcFips, |
| 11 |
OpenSsl102, |
| 12 |
OpenSsl111, |
| 13 |
OpenSsl30, |
| 14 |
} |
| 15 |
|
| 16 |
impl Libcrypto { |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
fn from_env() -> Self { |
| 22 |
let libcrypto = match std::env::var("S2N_LIBCRYPTO") { |
| 23 |
Ok(libcrypto) => libcrypto, |
| 24 |
Err(_) => { |
| 25 |
println!("S2N_LIBCRYPTO not set, assuming awslc"); |
| 26 |
"awslc".to_string() |
| 27 |
} |
| 28 |
}; |
| 29 |
|
| 30 |
match libcrypto.as_str() { |
| 31 |
"awslc" => Libcrypto::Awslc, |
| 32 |
"awslc-fips" | "awslc-fips-2022" | "awslc-fips-2024" => Libcrypto::AwslcFips, |
| 33 |
"openssl-1.0.2" => Libcrypto::OpenSsl102, |
| 34 |
"openssl-1.1.1" => Libcrypto::OpenSsl111, |
| 35 |
"openssl-3.0" => Libcrypto::OpenSsl30, |
| 36 |
_ => panic!("unexpected libcrypto: {libcrypto}"), |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 44 |
pub enum Capability { |
| 45 |
|
| 46 |
Tls13, |
| 47 |
MLKem, |
| 48 |
MLDsa, |
| 49 |
Chachapoly, |
| 50 |
} |
| 51 |
|
| 52 |
impl Capability { |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
fn supported(&self) -> bool { |
| 57 |
let libcrypto = Libcrypto::from_env(); |
| 58 |
match self { |
| 59 |
|
| 60 |
Capability::Tls13 => libcrypto != Libcrypto::OpenSsl102, |
| 61 |
Capability::Chachapoly => libcrypto != Libcrypto::OpenSsl102, |
| 62 |
|
| 63 |
Capability::MLKem => matches!(libcrypto, Libcrypto::Awslc | Libcrypto::AwslcFips), |
| 64 |
Capability::MLDsa => matches!(libcrypto, Libcrypto::Awslc), |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
pub fn required_capability(required_capabilities: &[Capability], test: impl FnOnce() + UnwindSafe) { |
| 74 |
let result = std::panic::catch_unwind(test); |
| 75 |
if required_capabilities.iter().all(|c| c.supported()) { |
| 76 |
result.unwrap(); |
| 77 |
} else { |
| 78 |
println!("expecting test failure"); |
| 79 |
let panic = result.unwrap_err(); |
| 80 |
println!("panic was {panic:?}"); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
pub fn required_capability_with_inner_result( |
| 89 |
required_capabilities: &[Capability], |
| 90 |
test: fn() -> Result<(), Box<dyn std::error::Error>>, |
| 91 |
) { |
| 92 |
let result = std::panic::catch_unwind(test); |
| 93 |
if required_capabilities.iter().all(|c| c.supported()) { |
| 94 |
result.unwrap().unwrap(); |
| 95 |
} else { |
| 96 |
println!("expecting test failure"); |
| 97 |
match result { |
| 98 |
Ok(Ok(())) => { |
| 99 |
panic!( |
| 100 |
"The test should have failed, but instead succeeded. \ |
| 101 |
Required capabilities are misconfigured" |
| 102 |
); |
| 103 |
} |
| 104 |
Ok(Err(e)) => { |
| 105 |
println!("Test failed as expected with explicit error: {e:?}"); |
| 106 |
} |
| 107 |
Err(panic) => { |
| 108 |
println!("Test failed as expected with panic: {panic:?}"); |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
} |