Debian Codegraph @

crt/s2n/bindings/rust/standard/integration/src/utilities/capability_check.rs

raw symbols SCIP
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 use std::panic::UnwindSafe;
5
6 /// The libcrypto that s2n-tls is linked against.
7 #[derive(Debug, PartialEq, Eq)]
8 enum Libcrypto {
9 Awslc,
10 AwslcFips,
11 OpenSsl102,
12 OpenSsl111,
13 OpenSsl30,
14 }
15
16 impl Libcrypto {
17 /// Retrieve the libcrypto from the `S2N_LIBCRYPTO` env variable if available,
18 /// otherwise return "awslc".
19 ///
20 /// S2N_LIBCRYPTO is set in CI as well as the Nix devshell.
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 /// A `Capability` represents a functionality of s2n-tls that may or may not be
42 /// available depending on the linked libcrypto.
43 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
44 pub enum Capability {
45 /// Support for TLS 1.3
46 Tls13,
47 MLKem,
48 MLDsa,
49 Chachapoly,
50 }
51
52 impl Capability {
53 /// Returns whether a capability is supported.
54 ///
55 /// Internally, this just maps from the libcrypto to its supported capabilities.
56 fn supported(&self) -> bool {
57 let libcrypto = Libcrypto::from_env();
58 match self {
59 // OpenSSL 1.0.2 doesn't support RSA-PSS, so TLS 1.3 isn't enabled
60 Capability::Tls13 => libcrypto != Libcrypto::OpenSsl102,
61 Capability::Chachapoly => libcrypto != Libcrypto::OpenSsl102,
62 // AWS-LC supports both ML-KEM + ML-DSA but AWSLCFIPS only supports ML-KEM
63 Capability::MLKem => matches!(libcrypto, Libcrypto::Awslc | Libcrypto::AwslcFips),
64 Capability::MLDsa => matches!(libcrypto, Libcrypto::Awslc),
65 }
66 }
67 }
68
69 /// Declare the required capabilities for a test to run.
70 ///
71 /// If all the required capabilities are present then the test must pass. Otherwise
72 /// we expect the test to panic/fail.
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 /// Declare the required capabilities for a test to run.
85 ///
86 /// This function is identical to [`required_capability`], but allows the test function
87 /// to return a result.
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 }