use std::collections::HashMap;
use std::sync::LazyLock;
static CIPHER_SUITE_MAP: LazyLock<HashMap<u16, &'static str>> = LazyLock::new(|| {
let mut map = HashMap::new();
map.insert(0x1301, "TLS_AES_128_GCM_SHA256");
map.insert(0x1302, "TLS_AES_256_GCM_SHA384");
map.insert(0x1303, "TLS_CHACHA20_POLY1305_SHA256");
map.insert(0x1304, "TLS_AES_128_CCM_SHA256");
map.insert(0x1305, "TLS_AES_128_CCM_8_SHA256");
map.insert(0xc02b, "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256");
map.insert(0xc02c, "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384");
map.insert(0xc02f, "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256");
map.insert(0xc030, "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384");
map.insert(0xc009, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA");
map.insert(0xc00a, "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA");
map.insert(0xc013, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA");
map.insert(0xc014, "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA");
map.insert(0xc023, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256");
map.insert(0xc024, "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384");
map.insert(0xc027, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256");
map.insert(0xc028, "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384");
map.insert(0xcca9, "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256");
map.insert(0xcca8, "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256");
map.insert(0xccaa, "TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256");
map.insert(0x009e, "TLS_DHE_RSA_WITH_AES_128_GCM_SHA256");
map.insert(0x009f, "TLS_DHE_RSA_WITH_AES_256_GCM_SHA384");
map.insert(0x0033, "TLS_DHE_RSA_WITH_AES_128_CBC_SHA");
map.insert(0x0039, "TLS_DHE_RSA_WITH_AES_256_CBC_SHA");
map.insert(0x0067, "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256");
map.insert(0x006b, "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256");
map.insert(0x009c, "TLS_RSA_WITH_AES_128_GCM_SHA256");
map.insert(0x009d, "TLS_RSA_WITH_AES_256_GCM_SHA384");
map.insert(0x002f, "TLS_RSA_WITH_AES_128_CBC_SHA");
map.insert(0x0035, "TLS_RSA_WITH_AES_256_CBC_SHA");
map.insert(0x003c, "TLS_RSA_WITH_AES_128_CBC_SHA256");
map.insert(0x003d, "TLS_RSA_WITH_AES_256_CBC_SHA256");
map.insert(0x000a, "TLS_RSA_WITH_3DES_EDE_CBC_SHA");
map.insert(0x0016, "TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA");
map.insert(0xc008, "TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA");
map.insert(0xc012, "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA");
map.insert(0x0004, "TLS_RSA_WITH_RC4_128_MD5");
map.insert(0x0005, "TLS_RSA_WITH_RC4_128_SHA");
map.insert(0xc007, "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA");
map.insert(0xc011, "TLS_ECDHE_RSA_WITH_RC4_128_SHA");
map.insert(0x008c, "TLS_PSK_WITH_AES_128_CBC_SHA");
map.insert(0x008d, "TLS_PSK_WITH_AES_256_CBC_SHA");
map.insert(0x00a8, "TLS_PSK_WITH_AES_128_GCM_SHA256");
map.insert(0x00a9, "TLS_PSK_WITH_AES_256_GCM_SHA384");
map.insert(0xc035, "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA");
map.insert(0xc036, "TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA");
map.insert(0xc037, "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256");
map.insert(0xc038, "TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA384");
map.insert(0xc03c, "TLS_RSA_WITH_ARIA_128_CBC_SHA256");
map.insert(0xc03d, "TLS_RSA_WITH_ARIA_256_CBC_SHA384");
map.insert(0xc060, "TLS_ECDHE_RSA_WITH_ARIA_128_GCM_SHA256");
map.insert(0xc061, "TLS_ECDHE_RSA_WITH_ARIA_256_GCM_SHA384");
map.insert(0xc072, "TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_CBC_SHA256");
map.insert(0xc073, "TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_CBC_SHA384");
map.insert(0xc076, "TLS_ECDHE_RSA_WITH_CAMELLIA_128_CBC_SHA256");
map.insert(0xc077, "TLS_ECDHE_RSA_WITH_CAMELLIA_256_CBC_SHA384");
map.insert(0x0001, "TLS_RSA_WITH_NULL_MD5");
map.insert(0x0002, "TLS_RSA_WITH_NULL_SHA");
map.insert(0x003b, "TLS_RSA_WITH_NULL_SHA256");
map
});
pub(super) fn get_cipher_suite_name(code: u16) -> Option<&'static str> {
CIPHER_SUITE_MAP.get(&code).copied()
}
pub fn format_cipher_suite(code: u16) -> String {
match get_cipher_suite_name(code) {
Some(name) => format!("{} (0x{:04X})", name, code),
None => format!("0x{:04X}", code),
}
}
pub fn is_secure_cipher_suite(code: u16) -> bool {
match code {
0x1301..=0x1305 => true,
0xc02b | 0xc02c | 0xc02f | 0xc030 => true, 0xcca8..=0xccaa => true, 0x009e | 0x009f => true, _ => false,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_tls13_cipher_suites() {
assert_eq!(
get_cipher_suite_name(0x1301),
Some("TLS_AES_128_GCM_SHA256")
);
assert_eq!(
get_cipher_suite_name(0x1302),
Some("TLS_AES_256_GCM_SHA384")
);
assert_eq!(
get_cipher_suite_name(0x1303),
Some("TLS_CHACHA20_POLY1305_SHA256")
);
}
#[test]
fn test_format_cipher_suite() {
assert_eq!(
format_cipher_suite(0x1301),
"TLS_AES_128_GCM_SHA256 (0x1301)"
);
assert_eq!(format_cipher_suite(0x9999), "0x9999");
}
#[test]
fn test_security_classification() {
assert!(is_secure_cipher_suite(0x1301)); assert!(is_secure_cipher_suite(0xc02f)); assert!(!is_secure_cipher_suite(0x0004)); assert!(!is_secure_cipher_suite(0x000a)); }
#[test]
fn test_unknown_cipher_suite() {
assert_eq!(get_cipher_suite_name(0xFFFF), None);
assert_eq!(format_cipher_suite(0xFFFF), "0xFFFF");
}
#[test]
fn test_aria_camellia_iana_names() {
assert_eq!(
get_cipher_suite_name(0xc03c),
Some("TLS_RSA_WITH_ARIA_128_CBC_SHA256")
);
assert_eq!(
get_cipher_suite_name(0xc03d),
Some("TLS_RSA_WITH_ARIA_256_CBC_SHA384")
);
assert_eq!(
get_cipher_suite_name(0xc060),
Some("TLS_ECDHE_RSA_WITH_ARIA_128_GCM_SHA256")
);
assert_eq!(
get_cipher_suite_name(0xc072),
Some("TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_CBC_SHA256")
);
assert_eq!(
get_cipher_suite_name(0xc073),
Some("TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_CBC_SHA384")
);
assert_eq!(
get_cipher_suite_name(0xc076),
Some("TLS_ECDHE_RSA_WITH_CAMELLIA_128_CBC_SHA256")
);
assert_eq!(
get_cipher_suite_name(0xc077),
Some("TLS_ECDHE_RSA_WITH_CAMELLIA_256_CBC_SHA384")
);
}
}