| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
use std::{error::Error, fmt::Debug}; |
| 12 |
|
| 13 |
mod setup; |
| 14 |
#[cfg(test)] |
| 15 |
mod test_utilities; |
| 16 |
|
| 17 |
use tls_harness::{Mode, SigType, TlsConnPair, TlsConnection}; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#[derive(Clone, Copy, Default, Eq, PartialEq, strum::EnumIter)] |
| 22 |
pub enum HandshakeType { |
| 23 |
#[default] |
| 24 |
ServerAuth, |
| 25 |
MutualAuth, |
| 26 |
Resumption, |
| 27 |
} |
| 28 |
|
| 29 |
impl Debug for HandshakeType { |
| 30 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 31 |
match self { |
| 32 |
HandshakeType::ServerAuth => write!(f, "server-auth"), |
| 33 |
HandshakeType::MutualAuth => write!(f, "mTLS"), |
| 34 |
HandshakeType::Resumption => write!(f, "resumption"), |
| 35 |
} |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
#[allow(non_camel_case_types)] |
| 42 |
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, strum::EnumIter)] |
| 43 |
pub enum CipherSuite { |
| 44 |
#[default] |
| 45 |
TLS_AES_128_GCM_SHA256, |
| 46 |
TLS_AES_256_GCM_SHA384, |
| 47 |
} |
| 48 |
|
| 49 |
#[derive(Clone, Copy, Default, strum::EnumIter)] |
| 50 |
pub enum KXGroup { |
| 51 |
Secp256R1, |
| 52 |
#[default] |
| 53 |
X25519, |
| 54 |
X25519MLKEM768, |
| 55 |
} |
| 56 |
|
| 57 |
impl Debug for KXGroup { |
| 58 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 59 |
match self { |
| 60 |
Self::Secp256R1 => write!(f, "secp256r1"), |
| 61 |
Self::X25519 => write!(f, "x25519"), |
| 62 |
Self::X25519MLKEM768 => write!(f, "X25519MLKEM768"), |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
#[derive(Clone, Copy, Debug, Default)] |
| 68 |
pub struct CryptoConfig { |
| 69 |
pub cipher_suite: CipherSuite, |
| 70 |
pub kx_group: KXGroup, |
| 71 |
pub sig_type: SigType, |
| 72 |
} |
| 73 |
|
| 74 |
impl CryptoConfig { |
| 75 |
pub fn new(cipher_suite: CipherSuite, kx_group: KXGroup, sig_type: SigType) -> Self { |
| 76 |
Self { |
| 77 |
cipher_suite, |
| 78 |
kx_group, |
| 79 |
sig_type, |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
pub trait TlsBenchConfig: Sized { |
| 87 |
fn make_config( |
| 88 |
mode: Mode, |
| 89 |
crypto_config: CryptoConfig, |
| 90 |
handshake_type: HandshakeType, |
| 91 |
) -> Result<Self, Box<dyn Error>>; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
pub fn new_bench_pair<C, S>( |
| 96 |
crypto_config: CryptoConfig, |
| 97 |
handshake_type: HandshakeType, |
| 98 |
) -> Result<TlsConnPair<C, S>, Box<dyn Error>> |
| 99 |
where |
| 100 |
C: TlsConnection, |
| 101 |
S: TlsConnection, |
| 102 |
C::Config: TlsBenchConfig, |
| 103 |
S::Config: TlsBenchConfig, |
| 104 |
{ |
| 105 |
|
| 106 |
if handshake_type == HandshakeType::Resumption { |
| 107 |
let server_config = S::Config::make_config(Mode::Server, crypto_config, handshake_type)?; |
| 108 |
let client_config = C::Config::make_config(Mode::Client, crypto_config, handshake_type)?; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
let mut pair = TlsConnPair::<C, S>::from_configs(&client_config, &server_config); |
| 113 |
pair.handshake()?; |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
pair.round_trip_transfer(&mut [0]).unwrap(); |
| 119 |
|
| 120 |
pair.shutdown().unwrap(); |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
return Ok(TlsConnPair::<C, S>::from_configs( |
| 128 |
&client_config, |
| 129 |
&server_config, |
| 130 |
)); |
| 131 |
} |
| 132 |
|
| 133 |
Ok(TlsConnPair::<C, S>::from_configs( |
| 134 |
&C::Config::make_config(Mode::Client, crypto_config, handshake_type).unwrap(), |
| 135 |
&S::Config::make_config(Mode::Server, crypto_config, handshake_type).unwrap(), |
| 136 |
)) |
| 137 |
} |
| 138 |
|
| 139 |
#[cfg(test)] |
| 140 |
mod tests { |
| 141 |
use super::*; |
| 142 |
use std::io::ErrorKind; |
| 143 |
use tls_harness::{ |
| 144 |
cohort::{OpenSslConnection, RustlsConnection, S2NConnection}, |
| 145 |
harness::TlsInfo, |
| 146 |
TlsConnection, |
| 147 |
}; |
| 148 |
|
| 149 |
#[test] |
| 150 |
fn rustls_handshakes() { |
| 151 |
test_utilities::all_handshakes::<RustlsConnection>(); |
| 152 |
} |
| 153 |
|
| 154 |
#[test] |
| 155 |
fn openssl_handshakes() { |
| 156 |
test_utilities::all_handshakes::<OpenSslConnection>(); |
| 157 |
} |
| 158 |
|
| 159 |
#[test] |
| 160 |
fn s2n_handshakes() { |
| 161 |
test_utilities::all_handshakes::<S2NConnection>(); |
| 162 |
} |
| 163 |
|
| 164 |
#[test] |
| 165 |
fn rustls_transfer() { |
| 166 |
test_utilities::transfer::<RustlsConnection>(); |
| 167 |
} |
| 168 |
|
| 169 |
#[test] |
| 170 |
fn openssl_transfer() { |
| 171 |
test_utilities::transfer::<OpenSslConnection>(); |
| 172 |
} |
| 173 |
|
| 174 |
#[test] |
| 175 |
fn s2n_transfer() { |
| 176 |
test_utilities::transfer::<S2NConnection>(); |
| 177 |
} |
| 178 |
|
| 179 |
fn session_resumption<C, S>() |
| 180 |
where |
| 181 |
S: TlsConnection + TlsInfo, |
| 182 |
C: TlsConnection + TlsInfo, |
| 183 |
C::Config: TlsBenchConfig, |
| 184 |
S::Config: TlsBenchConfig, |
| 185 |
{ |
| 186 |
println!("testing with client:{} server:{}", C::name(), S::name()); |
| 187 |
let mut conn_pair = |
| 188 |
new_bench_pair::<C, S>(CryptoConfig::default(), HandshakeType::Resumption).unwrap(); |
| 189 |
conn_pair.handshake().unwrap(); |
| 190 |
|
| 191 |
let err = conn_pair.client_mut().recv(&mut [0]).unwrap_err(); |
| 192 |
assert_eq!(err.kind(), ErrorKind::WouldBlock); |
| 193 |
|
| 194 |
assert!(conn_pair.server().resumed_connection()); |
| 195 |
conn_pair.shutdown().unwrap(); |
| 196 |
} |
| 197 |
|
| 198 |
#[test] |
| 199 |
fn session_resumption_interop() { |
| 200 |
env_logger::builder() |
| 201 |
.filter_level(log::LevelFilter::Debug) |
| 202 |
.is_test(true) |
| 203 |
.try_init() |
| 204 |
.unwrap(); |
| 205 |
session_resumption::<S2NConnection, S2NConnection>(); |
| 206 |
session_resumption::<S2NConnection, RustlsConnection>(); |
| 207 |
session_resumption::<S2NConnection, OpenSslConnection>(); |
| 208 |
|
| 209 |
session_resumption::<RustlsConnection, RustlsConnection>(); |
| 210 |
session_resumption::<RustlsConnection, S2NConnection>(); |
| 211 |
session_resumption::<RustlsConnection, OpenSslConnection>(); |
| 212 |
|
| 213 |
session_resumption::<OpenSslConnection, OpenSslConnection>(); |
| 214 |
session_resumption::<OpenSslConnection, S2NConnection>(); |
| 215 |
session_resumption::<OpenSslConnection, RustlsConnection>(); |
| 216 |
} |
| 217 |
} |