| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
use std::sync::LazyLock; |
| 12 |
|
| 13 |
use brass_aphid_wire_decryption::decryption::key_manager::KeyManager; |
| 14 |
use brass_aphid_wire_messages::iana; |
| 15 |
use openssl::ssl::SslContextBuilder; |
| 16 |
use s2n_tls::security::Policy; |
| 17 |
use tls_harness::{ |
| 18 |
cohort::{OpenSslConnection, S2NConnection}, |
| 19 |
harness::TlsConfigBuilderPair, |
| 20 |
TlsConnPair, |
| 21 |
}; |
| 22 |
|
| 23 |
use crate::capability_check::{required_capability, Capability}; |
| 24 |
|
| 25 |
struct Trial { |
| 26 |
client_group_configuration: String, |
| 27 |
server_policy: &'static Policy, |
| 28 |
} |
| 29 |
|
| 30 |
impl Trial { |
| 31 |
|
| 32 |
|
| 33 |
fn new(client_groups: String, server_policy: &'static Policy) -> Self { |
| 34 |
Self { |
| 35 |
client_group_configuration: client_groups, |
| 36 |
server_policy, |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
#[derive(Debug)] |
| 42 |
struct Outcome { |
| 43 |
|
| 44 |
client_key_shares: Vec<iana::Group>, |
| 45 |
server_selected_group: iana::Group, |
| 46 |
hello_retry_request: bool, |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
static PQ_ENABLED_POLICY: LazyLock<s2n_tls::security::Policy> = |
| 52 |
LazyLock::new(|| Policy::from_version("20251014").unwrap()); |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
static STRONGLY_PREFERRED_GROUPS: LazyLock<s2n_tls::security::Policy> = |
| 58 |
LazyLock::new(|| Policy::from_version("20251117").unwrap()); |
| 59 |
|
| 60 |
impl Trial { |
| 61 |
fn handshake(&self) -> Outcome { |
| 62 |
let key_manager = KeyManager::new(); |
| 63 |
let mut pair: TlsConnPair<OpenSslConnection, S2NConnection> = { |
| 64 |
let mut configs = |
| 65 |
TlsConfigBuilderPair::<SslContextBuilder, s2n_tls::config::Builder>::default(); |
| 66 |
configs |
| 67 |
.server |
| 68 |
.set_security_policy(self.server_policy) |
| 69 |
.unwrap(); |
| 70 |
key_manager.enable_s2n_logging(&mut configs.server); |
| 71 |
configs |
| 72 |
.client |
| 73 |
.set_groups_list(&self.client_group_configuration) |
| 74 |
.unwrap(); |
| 75 |
configs.connection_pair() |
| 76 |
}; |
| 77 |
pair.io.enable_recording(); |
| 78 |
pair.io.enable_decryption(key_manager.clone()); |
| 79 |
|
| 80 |
pair.handshake().unwrap(); |
| 81 |
pair.shutdown().unwrap(); |
| 82 |
|
| 83 |
let decrypted_stream = pair.io.decrypter.borrow(); |
| 84 |
let transcript = decrypted_stream.as_ref().unwrap().transcript(); |
| 85 |
|
| 86 |
let client_hello = transcript.client_hellos().first().unwrap().clone(); |
| 87 |
let key_shares = client_hello.key_share().unwrap(); |
| 88 |
|
| 89 |
let server_hello = transcript.server_hello(); |
| 90 |
Outcome { |
| 91 |
client_key_shares: key_shares, |
| 92 |
server_selected_group: server_hello.selected_group().unwrap().unwrap(), |
| 93 |
hello_retry_request: transcript.hello_retry_request().is_some(), |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
#[test] |
| 103 |
fn classical_group_selection() { |
| 104 |
required_capability(&[Capability::Tls13], || { |
| 105 |
let trial = Trial::new( |
| 106 |
"secp256r1:secp384r1:secp521r1".to_owned(), |
| 107 |
&PQ_ENABLED_POLICY, |
| 108 |
); |
| 109 |
let outcome = trial.handshake(); |
| 110 |
assert_eq!(outcome.client_key_shares, vec![iana::constants::secp256r1]); |
| 111 |
assert_eq!(outcome.server_selected_group, iana::constants::secp256r1); |
| 112 |
assert!(!outcome.hello_retry_request); |
| 113 |
|
| 114 |
let trial = Trial::new( |
| 115 |
"secp384r1:secp521r1:secp256r1".to_owned(), |
| 116 |
&PQ_ENABLED_POLICY, |
| 117 |
); |
| 118 |
let outcome = trial.handshake(); |
| 119 |
assert_eq!(outcome.client_key_shares, vec![iana::constants::secp384r1]); |
| 120 |
assert_eq!(outcome.server_selected_group, iana::constants::secp384r1); |
| 121 |
assert!(!outcome.hello_retry_request); |
| 122 |
}); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
#[test] |
| 130 |
fn pq_group_selection() { |
| 131 |
required_capability(&[Capability::Tls13, Capability::MLKem], || { |
| 132 |
let trial = Trial::new("secp384r1:X25519MLKEM768".to_owned(), &PQ_ENABLED_POLICY); |
| 133 |
let outcome = trial.handshake(); |
| 134 |
assert_eq!(outcome.client_key_shares, vec![iana::constants::secp384r1]); |
| 135 |
assert_eq!( |
| 136 |
outcome.server_selected_group, |
| 137 |
iana::constants::X25519MLKEM768 |
| 138 |
); |
| 139 |
assert!(outcome.hello_retry_request); |
| 140 |
}); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
#[test] |
| 150 |
fn strongly_preferred_groups() { |
| 151 |
required_capability(&[Capability::Tls13], || { |
| 152 |
|
| 153 |
let trial = Trial::new("secp384r1:secp256r1".to_owned(), &STRONGLY_PREFERRED_GROUPS); |
| 154 |
let outcome = trial.handshake(); |
| 155 |
assert_eq!(outcome.client_key_shares, vec![iana::constants::secp384r1]); |
| 156 |
assert_eq!(outcome.server_selected_group, iana::constants::secp384r1); |
| 157 |
assert!(!outcome.hello_retry_request); |
| 158 |
|
| 159 |
|
| 160 |
let trial = Trial::new("secp256r1:secp384r1".to_owned(), &STRONGLY_PREFERRED_GROUPS); |
| 161 |
let outcome = trial.handshake(); |
| 162 |
assert_eq!(outcome.client_key_shares, vec![iana::constants::secp256r1]); |
| 163 |
assert_eq!(outcome.server_selected_group, iana::constants::secp384r1); |
| 164 |
assert!(outcome.hello_retry_request); |
| 165 |
|
| 166 |
|
| 167 |
let trial = Trial::new("secp256r1:x448".to_owned(), &STRONGLY_PREFERRED_GROUPS); |
| 168 |
let outcome = trial.handshake(); |
| 169 |
assert_eq!(outcome.client_key_shares, vec![iana::constants::secp256r1]); |
| 170 |
assert_eq!(outcome.server_selected_group, iana::constants::secp256r1); |
| 171 |
assert!(!outcome.hello_retry_request); |
| 172 |
|
| 173 |
|
| 174 |
let trial = Trial::new("x448:secp256r1".to_owned(), &STRONGLY_PREFERRED_GROUPS); |
| 175 |
let outcome = trial.handshake(); |
| 176 |
assert_eq!(outcome.client_key_shares, vec![iana::constants::x448]); |
| 177 |
assert_eq!(outcome.server_selected_group, iana::constants::secp256r1); |
| 178 |
assert!(outcome.hello_retry_request); |
| 179 |
}); |
| 180 |
} |