| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
use crate::parsing::ClientHelloSupportedParameters; |
| 8 |
use s2n_tls_metrics_schema::static_lists::{Cipher, Group, Signature, Version}; |
| 9 |
|
| 10 |
pub(crate) trait TlsProfile { |
| 11 |
const ALLOWED_VERSIONS: &[Version]; |
| 12 |
const ALLOWED_CIPHERS: &[Cipher]; |
| 13 |
const ALLOWED_GROUPS: &[Group]; |
| 14 |
const ALLOWED_SIGNATURES: &[Signature]; |
| 15 |
|
| 16 |
|
| 17 |
fn supported(client_hello: &ClientHelloSupportedParameters) -> bool { |
| 18 |
let supported_version = client_hello |
| 19 |
.supported_versions() |
| 20 |
.iter() |
| 21 |
.any(|client_version| Self::ALLOWED_VERSIONS.contains(client_version)); |
| 22 |
|
| 23 |
let supported_cipher = client_hello |
| 24 |
.supported_ciphers() |
| 25 |
.iter() |
| 26 |
.any(|client_cipher| Self::ALLOWED_CIPHERS.contains(client_cipher)); |
| 27 |
|
| 28 |
let supported_signature = client_hello |
| 29 |
.supported_signatures() |
| 30 |
.map(|client_signatures| { |
| 31 |
client_signatures |
| 32 |
.iter() |
| 33 |
.any(|client_signature| Self::ALLOWED_SIGNATURES.contains(client_signature)) |
| 34 |
}) |
| 35 |
.unwrap_or(false); |
| 36 |
|
| 37 |
let supported_group = client_hello |
| 38 |
.supported_groups() |
| 39 |
.map(|client_groups| { |
| 40 |
client_groups |
| 41 |
.iter() |
| 42 |
.any(|client_group| Self::ALLOWED_GROUPS.contains(client_group)) |
| 43 |
}) |
| 44 |
.unwrap_or(false); |
| 45 |
|
| 46 |
supported_version && supported_cipher && supported_group && supported_signature |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
pub(crate) struct General20251201; |
| 51 |
impl TlsProfile for General20251201 { |
| 52 |
const ALLOWED_VERSIONS: &[Version] = &[Version::TLS_1_2, Version::TLS_1_3]; |
| 53 |
|
| 54 |
const ALLOWED_CIPHERS: &[Cipher] = &[ |
| 55 |
Cipher::TLS_AES_128_GCM_SHA256, |
| 56 |
Cipher::TLS_AES_256_GCM_SHA384, |
| 57 |
Cipher::TLS_CHACHA20_POLY1305_SHA256, |
| 58 |
Cipher::TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 59 |
Cipher::TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 60 |
Cipher::TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, |
| 61 |
Cipher::TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, |
| 62 |
|
| 63 |
|
| 64 |
]; |
| 65 |
|
| 66 |
const ALLOWED_GROUPS: &[Group] = &[ |
| 67 |
Group::x25519, |
| 68 |
Group::secp256r1, |
| 69 |
Group::secp384r1, |
| 70 |
Group::secp521r1, |
| 71 |
Group::X25519MLKEM768, |
| 72 |
Group::SecP256r1MLKEM768, |
| 73 |
Group::SecP384r1MLKEM1024, |
| 74 |
]; |
| 75 |
|
| 76 |
const ALLOWED_SIGNATURES: &[Signature] = &[ |
| 77 |
Signature::rsa_pss_pss_sha256, |
| 78 |
Signature::rsa_pss_pss_sha384, |
| 79 |
Signature::rsa_pss_pss_sha512, |
| 80 |
Signature::rsa_pss_rsae_sha256, |
| 81 |
Signature::rsa_pss_rsae_sha384, |
| 82 |
Signature::rsa_pss_rsae_sha512, |
| 83 |
Signature::ecdsa_secp256r1_sha256, |
| 84 |
Signature::ecdsa_secp384r1_sha384, |
| 85 |
Signature::ecdsa_secp521r1_sha512, |
| 86 |
]; |
| 87 |
} |
| 88 |
|
| 89 |
pub(crate) struct Fips20251201; |
| 90 |
impl TlsProfile for Fips20251201 { |
| 91 |
const ALLOWED_VERSIONS: &[Version] = &[Version::TLS_1_2, Version::TLS_1_3]; |
| 92 |
|
| 93 |
const ALLOWED_CIPHERS: &[Cipher] = &[ |
| 94 |
Cipher::TLS_AES_128_GCM_SHA256, |
| 95 |
Cipher::TLS_AES_256_GCM_SHA384, |
| 96 |
Cipher::TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 97 |
Cipher::TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 98 |
Cipher::TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, |
| 99 |
Cipher::TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, |
| 100 |
]; |
| 101 |
|
| 102 |
const ALLOWED_GROUPS: &[Group] = &[ |
| 103 |
Group::secp256r1, |
| 104 |
Group::secp384r1, |
| 105 |
Group::secp521r1, |
| 106 |
Group::X25519MLKEM768, |
| 107 |
Group::SecP256r1MLKEM768, |
| 108 |
Group::SecP384r1MLKEM1024, |
| 109 |
]; |
| 110 |
|
| 111 |
const ALLOWED_SIGNATURES: &[Signature] = &[ |
| 112 |
Signature::rsa_pss_pss_sha256, |
| 113 |
Signature::rsa_pss_pss_sha384, |
| 114 |
Signature::rsa_pss_pss_sha512, |
| 115 |
Signature::rsa_pss_rsae_sha256, |
| 116 |
Signature::rsa_pss_rsae_sha384, |
| 117 |
Signature::rsa_pss_rsae_sha512, |
| 118 |
Signature::ecdsa_secp256r1_sha256, |
| 119 |
Signature::ecdsa_secp384r1_sha384, |
| 120 |
Signature::ecdsa_secp521r1_sha512, |
| 121 |
]; |
| 122 |
} |
| 123 |
|
| 124 |
pub(crate) struct Cnsa1; |
| 125 |
impl TlsProfile for Cnsa1 { |
| 126 |
const ALLOWED_VERSIONS: &[Version] = &[Version::TLS_1_2, Version::TLS_1_3]; |
| 127 |
|
| 128 |
const ALLOWED_CIPHERS: &[Cipher] = &[ |
| 129 |
Cipher::TLS_AES_256_GCM_SHA384, |
| 130 |
Cipher::TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 131 |
]; |
| 132 |
|
| 133 |
const ALLOWED_GROUPS: &[Group] = &[Group::secp384r1]; |
| 134 |
|
| 135 |
const ALLOWED_SIGNATURES: &[Signature] = &[Signature::ecdsa_secp384r1_sha384]; |
| 136 |
} |
| 137 |
|
| 138 |
pub(crate) struct Cnsa2; |
| 139 |
impl TlsProfile for Cnsa2 { |
| 140 |
const ALLOWED_VERSIONS: &[Version] = &[Version::TLS_1_3]; |
| 141 |
|
| 142 |
const ALLOWED_CIPHERS: &[Cipher] = &[Cipher::TLS_AES_256_GCM_SHA384]; |
| 143 |
|
| 144 |
const ALLOWED_GROUPS: &[Group] = &[Group::MLKEM1024]; |
| 145 |
|
| 146 |
const ALLOWED_SIGNATURES: &[Signature] = &[Signature::mldsa87]; |
| 147 |
} |
| 148 |
|
| 149 |
#[cfg(test)] |
| 150 |
mod tests { |
| 151 |
use super::*; |
| 152 |
use s2n_tls::{ |
| 153 |
config::Builder, |
| 154 |
security::Policy, |
| 155 |
testing::{CertKeyPair, InsecureAcceptAllCertificatesHandler, TestPair}, |
| 156 |
}; |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
fn handshake_with_policy( |
| 162 |
policy_name: &str, |
| 163 |
cert: &CertKeyPair, |
| 164 |
) -> s2n_tls::connection::Connection { |
| 165 |
let policy = Policy::from_version(policy_name).unwrap(); |
| 166 |
|
| 167 |
|
| 168 |
let client_config = { |
| 169 |
let mut b = Builder::new(); |
| 170 |
b.set_security_policy(&policy).unwrap(); |
| 171 |
b.with_system_certs(false).unwrap(); |
| 172 |
b.trust_pem(cert.ca_cert()).unwrap(); |
| 173 |
b.set_verify_host_callback(InsecureAcceptAllCertificatesHandler {}) |
| 174 |
.unwrap(); |
| 175 |
b.build().unwrap() |
| 176 |
}; |
| 177 |
|
| 178 |
|
| 179 |
let server_config = { |
| 180 |
let mut b = Builder::new(); |
| 181 |
b.set_security_policy(&Policy::from_version("test_all").unwrap()) |
| 182 |
.unwrap(); |
| 183 |
b.with_system_certs(false).unwrap(); |
| 184 |
b.load_pem(cert.cert(), cert.key()).unwrap(); |
| 185 |
b.trust_pem(cert.ca_cert()).unwrap(); |
| 186 |
b.set_verify_host_callback(InsecureAcceptAllCertificatesHandler {}) |
| 187 |
.unwrap(); |
| 188 |
b.build().unwrap() |
| 189 |
}; |
| 190 |
|
| 191 |
let mut pair = TestPair::from_configs(&client_config, &server_config); |
| 192 |
pair.handshake().unwrap(); |
| 193 |
pair.server |
| 194 |
} |
| 195 |
|
| 196 |
fn default_cert() -> CertKeyPair { |
| 197 |
CertKeyPair::default() |
| 198 |
} |
| 199 |
|
| 200 |
fn ecdsa_p384_cert() -> CertKeyPair { |
| 201 |
CertKeyPair::from_path( |
| 202 |
"permutations/ec_ecdsa_p384_sha384/", |
| 203 |
"server-chain", |
| 204 |
"server-key", |
| 205 |
"ca-cert", |
| 206 |
) |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
fn mldsa87_configs(policy_name: &str) -> (s2n_tls::config::Config, s2n_tls::config::Config) { |
| 212 |
let pems = concat!(env!("CARGO_MANIFEST_DIR"), "/../../../../tests/pems/mldsa/"); |
| 213 |
let cert = std::fs::read(format!("{pems}ML-DSA-87.crt")).unwrap(); |
| 214 |
let key = std::fs::read(format!("{pems}ML-DSA-87-seed.priv")).unwrap(); |
| 215 |
|
| 216 |
let client_config = { |
| 217 |
let mut b = Builder::new(); |
| 218 |
b.set_security_policy(&Policy::from_version(policy_name).unwrap()) |
| 219 |
.unwrap(); |
| 220 |
b.with_system_certs(false).unwrap(); |
| 221 |
b.trust_pem(&cert).unwrap(); |
| 222 |
b.set_verify_host_callback(InsecureAcceptAllCertificatesHandler {}) |
| 223 |
.unwrap(); |
| 224 |
b.build().unwrap() |
| 225 |
}; |
| 226 |
let server_config = { |
| 227 |
let mut b = Builder::new(); |
| 228 |
b.set_security_policy(&Policy::from_version("test_all").unwrap()) |
| 229 |
.unwrap(); |
| 230 |
b.with_system_certs(false).unwrap(); |
| 231 |
b.load_pem(&cert, &key).unwrap(); |
| 232 |
b.trust_pem(&cert).unwrap(); |
| 233 |
b.set_verify_host_callback(InsecureAcceptAllCertificatesHandler {}) |
| 234 |
.unwrap(); |
| 235 |
b.build().unwrap() |
| 236 |
}; |
| 237 |
(client_config, server_config) |
| 238 |
} |
| 239 |
|
| 240 |
#[test] |
| 241 |
fn default_compatible_with_general() { |
| 242 |
let server = handshake_with_policy("default", &default_cert()); |
| 243 |
let ch = server.client_hello().unwrap(); |
| 244 |
assert!(General20251201::supported( |
| 245 |
&ClientHelloSupportedParameters::new(ch).unwrap() |
| 246 |
)); |
| 247 |
} |
| 248 |
|
| 249 |
#[test] |
| 250 |
fn default_fips_compatible_with_fips() { |
| 251 |
let server = handshake_with_policy("default_fips", &default_cert()); |
| 252 |
let ch = server.client_hello().unwrap(); |
| 253 |
assert!(Fips20251201::supported( |
| 254 |
&ClientHelloSupportedParameters::new(ch).unwrap() |
| 255 |
)); |
| 256 |
} |
| 257 |
|
| 258 |
#[test] |
| 259 |
fn cnsa_1_compatible_with_cnsa1() { |
| 260 |
let server = handshake_with_policy("cnsa_1", &ecdsa_p384_cert()); |
| 261 |
let ch = server.client_hello().unwrap(); |
| 262 |
assert!(Cnsa1::supported( |
| 263 |
&ClientHelloSupportedParameters::new(ch).unwrap() |
| 264 |
)); |
| 265 |
} |
| 266 |
|
| 267 |
#[test] |
| 268 |
fn cnsa_2_compatible_with_cnsa2() { |
| 269 |
let (client_config, server_config) = mldsa87_configs("cnsa_2"); |
| 270 |
let mut pair = TestPair::from_configs(&client_config, &server_config); |
| 271 |
pair.handshake().unwrap(); |
| 272 |
let ch = pair.server.client_hello().unwrap(); |
| 273 |
let supported_parameters = ClientHelloSupportedParameters::new(ch).unwrap(); |
| 274 |
|
| 275 |
assert!(Cnsa2::supported(&supported_parameters)); |
| 276 |
|
| 277 |
assert!(!Cnsa1::supported(&supported_parameters)); |
| 278 |
|
| 279 |
assert!(!Fips20251201::supported(&supported_parameters)); |
| 280 |
|
| 281 |
assert!(!General20251201::supported(&supported_parameters)); |
| 282 |
} |
| 283 |
|
| 284 |
#[test] |
| 285 |
fn cnsa_1_2_interop_compatible_with_cnsa1_and_cnsa2() { |
| 286 |
|
| 287 |
let cert = ecdsa_p384_cert(); |
| 288 |
let server = handshake_with_policy("cnsa_1_2_interop", &cert); |
| 289 |
let ch = server.client_hello().unwrap(); |
| 290 |
let supported_parameters = ClientHelloSupportedParameters::new(ch).unwrap(); |
| 291 |
assert!(Cnsa1::supported(&supported_parameters)); |
| 292 |
assert!(Cnsa2::supported(&supported_parameters)); |
| 293 |
} |
| 294 |
} |