sozu-lib 2.0.2

sozu library to build hot reconfigurable HTTP reverse proxies
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
//! Crypto provider selection.
//!
//! - `crypto-ring` (default): pure Rust via [ring](https://github.com/briansmith/ring).
//! - `crypto-aws-lc-rs`: post-quantum-capable [aws-lc-rs](https://github.com/aws/aws-lc-rs).
//!   Requires `cmake`.
//! - `crypto-openssl`: system OpenSSL via [rustls-openssl](https://github.com/rustls/rustls-openssl).
//!   Requires `cmake` + OpenSSL headers.
//! - `fips`: implies `crypto-aws-lc-rs` plus `rustls/fips` (FIPS 140-3 build).
//!
//! At least one provider feature must be enabled. When several are enabled
//! together (e.g. `cargo build --all-features` in CI, or `--features fips`
//! on top of the default `crypto-ring`), a deterministic precedence chain
//! selects one: `fips > ring > aws-lc-rs > openssl`. `fips` always wins, so
//! a binary built with `--features fips` runs aws-lc-rs in FIPS mode even
//! when `crypto-ring` is also enabled. Downstream packaging (Dockerfile,
//! RPM, PKGBUILD) selects exactly one provider explicitly.

use std::sync::LazyLock;

use rustls::crypto::CryptoProvider;

static DEFAULT_PROVIDER: LazyLock<CryptoProvider> = LazyLock::new(default_provider);

#[cfg(not(any(
    feature = "crypto-ring",
    feature = "crypto-aws-lc-rs",
    feature = "crypto-openssl"
)))]
compile_error!(
    "No crypto provider selected. Enable one of: `crypto-ring`, `crypto-aws-lc-rs`, or `crypto-openssl`."
);

// `fips` wins. It implies `crypto-aws-lc-rs` plus `rustls/fips`, so the
// resulting binary uses aws-lc-rs in FIPS mode regardless of which other
// provider features are enabled by `default` or `--all-features`. This
// lets `cargo build --features fips` produce a genuine FIPS binary even
// while the default `crypto-ring` is still active, instead of silently
// linking ring and emitting a non-FIPS provider in a `+fips`-tagged
// build. Below `fips`, the precedence chain falls back to `ring >
// aws-lc-rs > openssl`, matching the binary default. Downstream
// packaging surfaces (`Dockerfile`, `os-build/...`) still select exactly
// one feature in production builds.

#[cfg(feature = "fips")]
pub use rustls::crypto::aws_lc_rs::{
    cipher_suite::{
        TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
        TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
        TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
        TLS13_AES_128_GCM_SHA256, TLS13_AES_256_GCM_SHA384, TLS13_CHACHA20_POLY1305_SHA256,
    },
    default_provider,
    sign::any_supported_type,
};

#[cfg(all(feature = "crypto-ring", not(feature = "fips")))]
pub use rustls::crypto::ring::{
    cipher_suite::{
        TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
        TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
        TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
        TLS13_AES_128_GCM_SHA256, TLS13_AES_256_GCM_SHA384, TLS13_CHACHA20_POLY1305_SHA256,
    },
    default_provider,
    sign::any_supported_type,
};

#[cfg(all(
    feature = "crypto-aws-lc-rs",
    not(feature = "fips"),
    not(feature = "crypto-ring")
))]
pub use rustls::crypto::aws_lc_rs::{
    cipher_suite::{
        TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
        TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
        TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
        TLS13_AES_128_GCM_SHA256, TLS13_AES_256_GCM_SHA384, TLS13_CHACHA20_POLY1305_SHA256,
    },
    default_provider,
    sign::any_supported_type,
};

#[cfg(all(
    feature = "crypto-openssl",
    not(feature = "fips"),
    not(feature = "crypto-ring"),
    not(feature = "crypto-aws-lc-rs")
))]
pub use rustls_openssl::{
    cipher_suite::{
        TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
        TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
        TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
        TLS13_AES_128_GCM_SHA256, TLS13_AES_256_GCM_SHA384, TLS13_CHACHA20_POLY1305_SHA256,
    },
    default_provider,
};

/// Load a private key into a signing key.
///
/// For `ring` and `aws-lc-rs`, this delegates to `sign::any_supported_type`.
/// For `rustls-openssl`, this delegates to `KeyProvider::load_private_key`.
#[cfg(all(
    feature = "crypto-openssl",
    not(feature = "fips"),
    not(feature = "crypto-ring"),
    not(feature = "crypto-aws-lc-rs")
))]
pub fn any_supported_type(
    der: &rustls::pki_types::PrivateKeyDer<'_>,
) -> Result<std::sync::Arc<dyn rustls::sign::SigningKey>, rustls::Error> {
    use rustls::crypto::KeyProvider;
    rustls_openssl::KeyProvider.load_private_key(der.clone_key())
}

/// Look up a key exchange group by its string name.
///
/// Accepts the standard TLS named group identifiers used in sozu configuration:
/// - `"x25519"` / `"X25519"` — Curve25519 ECDHE
/// - `"secp256r1"` / `"P-256"` — NIST P-256 ECDHE
/// - `"secp384r1"` / `"P-384"` — NIST P-384 ECDHE
/// - `"X25519MLKEM768"` — Post-quantum hybrid (aws-lc-rs and openssl only)
///
/// Returns `None` if the group name is unknown or not supported by the compiled provider.
pub fn kx_group_by_name(name: &str) -> Option<&'static dyn rustls::crypto::SupportedKxGroup> {
    let provider = &*DEFAULT_PROVIDER;
    let named_group = match name {
        "x25519" | "X25519" => rustls::NamedGroup::X25519,
        "secp256r1" | "P-256" => rustls::NamedGroup::secp256r1,
        "secp384r1" | "P-384" => rustls::NamedGroup::secp384r1,
        "X25519MLKEM768" => rustls::NamedGroup::X25519MLKEM768,
        _ => return None,
    };
    provider
        .kx_groups
        .iter()
        .find(|g| g.name() == named_group)
        .copied()
}

/// Look up a cipher suite by its string name, filtered through the active
/// crypto provider's supported set.
///
/// Accepts the rustls cipher suite names used in sozu configuration.
/// Returns `None` if the name is unknown OR if the suite is not present
/// in `default_provider().cipher_suites` for the active provider build.
///
/// The filter step is what keeps a FIPS build (aws-lc-rs with the
/// upstream `fips` feature) from silently advertising non-FIPS suites
/// such as ChaCha20-Poly1305 when `DEFAULT_CIPHER_LIST` includes them:
/// rustls 0.23 only reports `ServerConfig::fips() == true` if every
/// configured cipher and KX group is FIPS-approved (see
/// `rustls/src/crypto/mod.rs` and `aws_lc_rs/mod.rs` ChaCha gating
/// under `feature = "fips"`).
pub fn cipher_suite_by_name(name: &str) -> Option<rustls::SupportedCipherSuite> {
    let candidate = match name {
        "TLS13_AES_256_GCM_SHA384" => Some(TLS13_AES_256_GCM_SHA384),
        "TLS13_AES_128_GCM_SHA256" => Some(TLS13_AES_128_GCM_SHA256),
        "TLS13_CHACHA20_POLY1305_SHA256" => Some(TLS13_CHACHA20_POLY1305_SHA256),
        "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384" => Some(TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384),
        "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256" => Some(TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256),
        "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256" => {
            Some(TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256)
        }
        "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384" => Some(TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384),
        "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256" => Some(TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256),
        "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256" => {
            Some(TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256)
        }
        _ => None,
    }?;

    // Only return the suite if the active provider actually advertises
    // it. Compare by `CipherSuite` enum (newtype around the IANA value)
    // since `SupportedCipherSuite` does not implement `Eq`.
    let wanted = candidate.suite();
    DEFAULT_PROVIDER
        .cipher_suites
        .iter()
        .find(|s| s.suite() == wanted)
        .copied()
}

#[cfg(test)]
mod tests {
    use super::*;
    use rustls::NamedGroup;

    #[test]
    fn default_provider_has_tls13_cipher_suites() {
        let provider = default_provider();
        let names: Vec<_> = provider.cipher_suites.iter().map(|cs| cs.suite()).collect();
        assert!(
            names.contains(&rustls::CipherSuite::TLS13_AES_256_GCM_SHA384),
            "provider must support TLS13_AES_256_GCM_SHA384"
        );
        assert!(
            names.contains(&rustls::CipherSuite::TLS13_AES_128_GCM_SHA256),
            "provider must support TLS13_AES_128_GCM_SHA256"
        );
        // CHACHA20_POLY1305 is not FIPS-approved
        #[cfg(not(feature = "fips"))]
        assert!(
            names.contains(&rustls::CipherSuite::TLS13_CHACHA20_POLY1305_SHA256),
            "provider must support TLS13_CHACHA20_POLY1305_SHA256"
        );
    }

    #[test]
    fn default_provider_has_tls12_cipher_suites() {
        let provider = default_provider();
        let names: Vec<_> = provider.cipher_suites.iter().map(|cs| cs.suite()).collect();
        assert!(
            names.contains(&rustls::CipherSuite::TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384),
            "provider must support TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384"
        );
        assert!(
            names.contains(&rustls::CipherSuite::TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384),
            "provider must support TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384"
        );
    }

    #[test]
    fn default_provider_has_classical_kx_groups() {
        let provider = default_provider();
        let groups: Vec<_> = provider.kx_groups.iter().map(|g| g.name()).collect();
        // X25519 is not FIPS-approved (only NIST curves are)
        #[cfg(not(feature = "fips"))]
        assert!(
            groups.contains(&NamedGroup::X25519),
            "provider must support X25519 key exchange"
        );
        assert!(
            groups.contains(&NamedGroup::secp256r1),
            "provider must support secp256r1 key exchange"
        );
        assert!(
            groups.contains(&NamedGroup::secp384r1),
            "provider must support secp384r1 key exchange"
        );
    }

    #[cfg(all(feature = "crypto-aws-lc-rs", not(feature = "fips")))]
    #[test]
    fn aws_lc_rs_supports_post_quantum_kx() {
        let provider = default_provider();
        let groups: Vec<_> = provider.kx_groups.iter().map(|g| g.name()).collect();
        assert!(
            groups.contains(&NamedGroup::X25519MLKEM768),
            "aws-lc-rs provider must support X25519MLKEM768 post-quantum key exchange"
        );
    }

    #[cfg(all(feature = "crypto-aws-lc-rs", not(feature = "fips")))]
    #[test]
    fn aws_lc_rs_has_more_kx_groups_than_classical() {
        let provider = default_provider();
        // aws-lc-rs should have more kx groups than just the 3 classical ones
        // (X25519, secp256r1, secp384r1) because it also includes PQ groups
        assert!(
            provider.kx_groups.len() > 3,
            "aws-lc-rs should have more than 3 kx groups (has {}), including post-quantum",
            provider.kx_groups.len()
        );
    }

    #[cfg(feature = "fips")]
    #[test]
    fn fips_provider_has_nist_kx_groups() {
        let provider = default_provider();
        let groups: Vec<_> = provider.kx_groups.iter().map(|g| g.name()).collect();
        assert!(
            groups.contains(&NamedGroup::secp256r1),
            "FIPS provider must support secp256r1"
        );
        assert!(
            groups.contains(&NamedGroup::secp384r1),
            "FIPS provider must support secp384r1"
        );
    }

    #[cfg(feature = "fips")]
    #[test]
    fn fips_provider_has_aes_gcm_cipher_suites() {
        let provider = default_provider();
        let suites: Vec<_> = provider.cipher_suites.iter().map(|cs| cs.suite()).collect();
        assert!(
            suites.contains(&rustls::CipherSuite::TLS13_AES_256_GCM_SHA384),
            "FIPS provider must support TLS13_AES_256_GCM_SHA384"
        );
        assert!(
            suites.contains(&rustls::CipherSuite::TLS13_AES_128_GCM_SHA256),
            "FIPS provider must support TLS13_AES_128_GCM_SHA256"
        );
    }

    #[cfg(all(feature = "crypto-ring", not(feature = "fips")))]
    #[test]
    fn ring_has_no_mlkem() {
        let provider = default_provider();
        let groups: Vec<_> = provider.kx_groups.iter().map(|g| g.name()).collect();
        assert!(
            !groups.contains(&NamedGroup::X25519MLKEM768),
            "ring provider should not advertise X25519MLKEM768"
        );
    }

    #[cfg(all(
        feature = "crypto-openssl",
        not(feature = "fips"),
        not(feature = "crypto-ring"),
        not(feature = "crypto-aws-lc-rs")
    ))]
    #[test]
    fn openssl_pq_kx_depends_on_openssl_version() {
        let provider = default_provider();
        let groups: Vec<_> = provider.kx_groups.iter().map(|g| g.name()).collect();
        let has_pq = groups.contains(&NamedGroup::X25519MLKEM768);
        // X25519MLKEM768 is only available with OpenSSL 3.5+.
        // On older versions, the provider should still work with classical groups.
        if has_pq {
            println!("OpenSSL 3.5+ detected: X25519MLKEM768 is available");
        } else {
            println!("OpenSSL < 3.5: X25519MLKEM768 not available, classical groups only");
        }
        // In all cases, classical groups must be present
        assert!(groups.contains(&NamedGroup::X25519));
        assert!(groups.contains(&NamedGroup::secp256r1));
    }

    #[cfg(not(feature = "fips"))]
    #[test]
    fn kx_group_by_name_resolves_x25519() {
        let group = kx_group_by_name("x25519").expect("x25519 should be supported");
        assert_eq!(group.name(), NamedGroup::X25519);
    }

    #[cfg(not(feature = "fips"))]
    #[test]
    fn kx_group_by_name_resolves_x25519_uppercase() {
        let group = kx_group_by_name("X25519").expect("X25519 should be supported");
        assert_eq!(group.name(), NamedGroup::X25519);
    }

    #[cfg(feature = "fips")]
    #[test]
    fn kx_group_by_name_returns_none_for_x25519_in_fips() {
        assert!(
            kx_group_by_name("x25519").is_none(),
            "X25519 is not FIPS-approved"
        );
        assert!(
            kx_group_by_name("X25519").is_none(),
            "X25519 is not FIPS-approved"
        );
    }

    #[test]
    fn kx_group_by_name_resolves_p256() {
        let group = kx_group_by_name("P-256").expect("P-256 should be supported");
        assert_eq!(group.name(), NamedGroup::secp256r1);
    }

    #[test]
    fn kx_group_by_name_resolves_secp256r1() {
        let group = kx_group_by_name("secp256r1").expect("secp256r1 should be supported");
        assert_eq!(group.name(), NamedGroup::secp256r1);
    }

    #[test]
    fn kx_group_by_name_resolves_p384() {
        let group = kx_group_by_name("P-384").expect("P-384 should be supported");
        assert_eq!(group.name(), NamedGroup::secp384r1);
    }

    #[test]
    fn kx_group_by_name_returns_none_for_unknown() {
        assert!(kx_group_by_name("P-521").is_none());
        assert!(kx_group_by_name("unknown").is_none());
        assert!(kx_group_by_name("").is_none());
    }

    #[cfg(all(feature = "crypto-aws-lc-rs", not(feature = "fips")))]
    #[test]
    fn kx_group_by_name_resolves_x25519mlkem768() {
        let group = kx_group_by_name("X25519MLKEM768").expect("X25519MLKEM768 should be supported");
        assert_eq!(group.name(), NamedGroup::X25519MLKEM768);
    }

    #[cfg(all(feature = "crypto-ring", not(feature = "fips")))]
    #[test]
    fn kx_group_by_name_returns_none_for_mlkem_on_ring() {
        assert!(
            kx_group_by_name("X25519MLKEM768").is_none(),
            "ring does not support X25519MLKEM768"
        );
    }

    #[test]
    fn can_load_rsa_private_key() {
        use rustls::pki_types::PrivateKeyDer;
        use rustls::pki_types::pem::PemObject;

        let key_pem = include_str!("../assets/key.pem");
        let private_key =
            PrivateKeyDer::from_pem_slice(key_pem.as_bytes()).expect("failed to parse PEM key");
        any_supported_type(&private_key).expect("provider must be able to load RSA private key");
    }

    #[test]
    fn can_build_server_config_with_tls13() {
        use std::sync::Arc;

        let provider = default_provider();
        let config = rustls::ServerConfig::builder_with_provider(Arc::new(provider))
            .with_protocol_versions(&[&rustls::version::TLS13])
            .expect("failed to build TLS 1.3 config")
            .with_no_client_auth()
            .with_cert_resolver(Arc::new(crate::tls::MutexCertificateResolver::default()));
        assert!(
            !config.alpn_protocols.contains(&b"h2".to_vec()),
            "default config should not have ALPN set"
        );
    }

    #[test]
    fn can_build_server_config_with_tls12_and_tls13() {
        use std::sync::Arc;

        let provider = default_provider();
        rustls::ServerConfig::builder_with_provider(Arc::new(provider))
            .with_protocol_versions(&[&rustls::version::TLS12, &rustls::version::TLS13])
            .expect("failed to build TLS 1.2+1.3 config")
            .with_no_client_auth()
            .with_cert_resolver(Arc::new(crate::tls::MutexCertificateResolver::default()));
    }

    #[cfg(all(feature = "crypto-aws-lc-rs", not(feature = "fips")))]
    #[test]
    fn pq_kx_compatible_with_server_config() {
        use std::sync::Arc;

        let provider = default_provider();
        // Verify the PQ kx group is present
        let has_pq = provider
            .kx_groups
            .iter()
            .any(|g| g.name() == NamedGroup::X25519MLKEM768);
        assert!(has_pq, "X25519MLKEM768 must be in the provider");

        // Build a TLS 1.3 config (PQ kx is only for TLS 1.3)
        let config = rustls::ServerConfig::builder_with_provider(Arc::new(provider))
            .with_protocol_versions(&[&rustls::version::TLS13])
            .expect("TLS 1.3 config with PQ kx should build successfully")
            .with_no_client_auth()
            .with_cert_resolver(Arc::new(crate::tls::MutexCertificateResolver::default()));

        // The config should be valid
        assert!(
            !config.alpn_protocols.contains(&b"h2".to_vec()),
            "default config should not have ALPN set"
        );
    }

    #[test]
    fn default_cipher_list_names_resolve_to_valid_suites() {
        use sozu_command::config::DEFAULT_CIPHER_LIST;

        let all_suites = [
            ("TLS13_AES_256_GCM_SHA384", TLS13_AES_256_GCM_SHA384.suite()),
            ("TLS13_AES_128_GCM_SHA256", TLS13_AES_128_GCM_SHA256.suite()),
            (
                "TLS13_CHACHA20_POLY1305_SHA256",
                TLS13_CHACHA20_POLY1305_SHA256.suite(),
            ),
            (
                "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
                TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384.suite(),
            ),
            (
                "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
                TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256.suite(),
            ),
            (
                "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256",
                TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256.suite(),
            ),
            (
                "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
                TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384.suite(),
            ),
            (
                "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
                TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256.suite(),
            ),
            (
                "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256",
                TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256.suite(),
            ),
        ];

        // Verify every name in DEFAULT_CIPHER_LIST matches a known suite
        for name in DEFAULT_CIPHER_LIST {
            let found = all_suites.iter().any(|(n, _)| *n == name);
            assert!(
                found,
                "DEFAULT_CIPHER_LIST entry {name:?} does not match any known cipher suite"
            );
        }

        // Verify the count matches (no duplicates, no missing)
        assert_eq!(
            DEFAULT_CIPHER_LIST.len(),
            all_suites.len(),
            "DEFAULT_CIPHER_LIST length should match number of known suites"
        );
    }

    #[test]
    fn cipher_suite_by_name_resolves_tls13() {
        assert!(cipher_suite_by_name("TLS13_AES_256_GCM_SHA384").is_some());
        assert!(cipher_suite_by_name("TLS13_AES_128_GCM_SHA256").is_some());
        #[cfg(not(feature = "fips"))]
        assert!(cipher_suite_by_name("TLS13_CHACHA20_POLY1305_SHA256").is_some());
    }

    #[test]
    fn cipher_suite_by_name_resolves_tls12() {
        assert!(cipher_suite_by_name("TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384").is_some());
        assert!(cipher_suite_by_name("TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384").is_some());
    }

    #[test]
    fn cipher_suite_by_name_returns_none_for_unknown() {
        assert!(cipher_suite_by_name("UNKNOWN_CIPHER").is_none());
        assert!(cipher_suite_by_name("").is_none());
        // OpenSSL-style names should NOT match (this was the old bug)
        assert!(cipher_suite_by_name("TLS_AES_256_GCM_SHA384").is_none());
    }
}