Debian Codegraph @

crt/s2n/bindings/rust/standard/benchmarks/src/setup.rs

raw symbols SCIP
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 use crate::{CipherSuite, CryptoConfig, HandshakeType, KXGroup, TlsBenchConfig};
5
6 use std::{error::Error, sync::Arc};
7 use tls_harness::{
8 cohort::{self, S2NConfig},
9 get_cert_path,
10 harness::{read_to_bytes, Mode},
11 PemType::*,
12 };
13
14 mod openssl_bench_setup {
15 use super::*;
16 use openssl::ssl::{
17 SslContext, SslFiletype, SslMethod, SslOptions, SslSessionCacheMode, SslVerifyMode,
18 SslVersion,
19 };
20 impl TlsBenchConfig for cohort::OpenSslConfig {
21 fn make_config(
22 mode: Mode,
23 crypto_config: super::CryptoConfig,
24 handshake_type: super::HandshakeType,
25 ) -> Result<Self, Box<dyn Error>> {
26 let cipher_suite = match crypto_config.cipher_suite {
27 CipherSuite::TLS_AES_128_GCM_SHA256 => "TLS_AES_128_GCM_SHA256",
28 super::CipherSuite::TLS_AES_256_GCM_SHA384 => "TLS_AES_256_GCM_SHA384",
29 };
30
31 let ec_key = match crypto_config.kx_group {
32 KXGroup::Secp256R1 => "P-256",
33 KXGroup::X25519 => "X25519",
34 KXGroup::X25519MLKEM768 => "X25519MLKEM768",
35 };
36
37 let ssl_method = match mode {
38 Mode::Client => SslMethod::tls_client(),
39 Mode::Server => SslMethod::tls_server(),
40 };
41
42 let session_ticket_storage = cohort::openssl::SessionTicketStorage::default();
43
44 let mut builder = SslContext::builder(ssl_method)?;
45 builder.set_min_proto_version(Some(SslVersion::TLS1_3))?;
46 builder.set_ciphersuites(cipher_suite)?;
47 builder.set_groups_list(ec_key)?;
48
49 match mode {
50 Mode::Client => {
51 builder.set_ca_file(get_cert_path(CACert, crypto_config.sig_type))?;
52 builder.set_verify(SslVerifyMode::FAIL_IF_NO_PEER_CERT | SslVerifyMode::PEER);
53
54 match handshake_type {
55 HandshakeType::MutualAuth => {
56 builder.set_certificate_chain_file(get_cert_path(
57 ClientCertChain,
58 crypto_config.sig_type,
59 ))?;
60 builder.set_private_key_file(
61 get_cert_path(ClientKey, crypto_config.sig_type),
62 SslFiletype::PEM,
63 )?;
64 }
65 HandshakeType::Resumption => {
66 builder.set_session_cache_mode(SslSessionCacheMode::CLIENT);
67 // do not attempt to define the callback outside of an
68 // expression directly passed into the function, because
69 // the compiler's type inference doesn't work for this
70 // scenario
71 // https://github.com/rust-lang/rust/issues/70263
72 builder.set_new_session_callback({
73 let sts = session_ticket_storage.clone();
74 move |_, ticket| {
75 let _ = sts.stored_ticket.lock().unwrap().insert(ticket);
76 }
77 });
78 }
79 HandshakeType::ServerAuth => {}
80 }
81 }
82 Mode::Server => {
83 builder.set_certificate_chain_file(get_cert_path(
84 ServerCertChain,
85 crypto_config.sig_type,
86 ))?;
87 builder.set_private_key_file(
88 get_cert_path(ServerKey, crypto_config.sig_type),
89 SslFiletype::PEM,
90 )?;
91
92 if handshake_type == HandshakeType::MutualAuth {
93 builder.set_ca_file(get_cert_path(CACert, crypto_config.sig_type))?;
94 builder
95 .set_verify(SslVerifyMode::FAIL_IF_NO_PEER_CERT | SslVerifyMode::PEER);
96 }
97 if handshake_type == HandshakeType::Resumption {
98 builder.set_session_cache_mode(SslSessionCacheMode::CLIENT);
99 } else {
100 builder.set_options(SslOptions::NO_TICKET);
101 builder.set_session_cache_mode(SslSessionCacheMode::OFF);
102 // OpenSSL Bug: https://github.com/openssl/openssl/issues/8077
103 // even with the above configuration, we must explicitly specify
104 // 0 tickets
105 builder.set_num_tickets(0)?;
106 }
107 }
108 }
109 Ok(Self {
110 config: builder.build(),
111 session_ticket_storage,
112 })
113 }
114 }
115 }
116
117 mod rustls_bench_setup {
118 use super::*;
119 use rustls::{
120 crypto::{
121 aws_lc_rs::{self, kx_group},
122 CryptoProvider,
123 },
124 server::WebPkiClientVerifier,
125 version, ClientConfig, ServerConfig,
126 };
127 use tls_harness::cohort::rustls::NoOpTicketer;
128
129 impl TlsBenchConfig for cohort::RustlsConfig {
130 fn make_config(
131 mode: Mode,
132 crypto_config: CryptoConfig,
133 handshake_type: HandshakeType,
134 ) -> Result<Self, Box<dyn Error>> {
135 let cipher_suite = match crypto_config.cipher_suite {
136 CipherSuite::TLS_AES_128_GCM_SHA256 => {
137 aws_lc_rs::cipher_suite::TLS13_AES_128_GCM_SHA256
138 }
139 CipherSuite::TLS_AES_256_GCM_SHA384 => {
140 aws_lc_rs::cipher_suite::TLS13_AES_256_GCM_SHA384
141 }
142 };
143
144 let kx_group = match crypto_config.kx_group {
145 KXGroup::Secp256R1 => &kx_group::SECP256R1,
146 KXGroup::X25519 => &kx_group::X25519,
147 KXGroup::X25519MLKEM768 => &kx_group::X25519MLKEM768,
148 };
149
150 let crypto_provider = Arc::new(CryptoProvider {
151 cipher_suites: vec![cipher_suite],
152 kx_groups: vec![*kx_group],
153 ..aws_lc_rs::default_provider()
154 });
155
156 match mode {
157 Mode::Client => {
158 let builder = ClientConfig::builder_with_provider(crypto_provider)
159 .with_protocol_versions(&[&version::TLS13])?
160 .with_root_certificates(Self::get_root_cert_store(crypto_config.sig_type));
161
162 let config = match handshake_type {
163 HandshakeType::ServerAuth | HandshakeType::Resumption => {
164 builder.with_no_client_auth()
165 }
166 HandshakeType::MutualAuth => builder.with_client_auth_cert(
167 Self::get_cert_chain(ClientCertChain, crypto_config.sig_type),
168 Self::get_key(ClientKey, crypto_config.sig_type),
169 )?,
170 };
171
172 if handshake_type != HandshakeType::Resumption {
173 rustls::client::Resumption::disabled();
174 }
175
176 Ok(cohort::RustlsConfig::Client(Arc::new(config)))
177 }
178 Mode::Server => {
179 let builder = ServerConfig::builder_with_provider(crypto_provider)
180 .with_protocol_versions(&[&version::TLS13])?;
181
182 let builder = match handshake_type {
183 HandshakeType::ServerAuth | HandshakeType::Resumption => {
184 builder.with_no_client_auth()
185 }
186 HandshakeType::MutualAuth => {
187 let client_cert_verifier = WebPkiClientVerifier::builder(
188 Self::get_root_cert_store(crypto_config.sig_type).into(),
189 )
190 .build()
191 .unwrap();
192 builder.with_client_cert_verifier(client_cert_verifier)
193 }
194 };
195
196 let mut config = builder.with_single_cert(
197 Self::get_cert_chain(ServerCertChain, crypto_config.sig_type),
198 Self::get_key(ServerKey, crypto_config.sig_type),
199 )?;
200
201 if handshake_type != HandshakeType::Resumption {
202 config.session_storage =
203 Arc::new(rustls::server::NoServerSessionStorage {});
204 config.ticketer = Arc::new(NoOpTicketer {});
205 }
206
207 Ok(cohort::RustlsConfig::Server(Arc::new(config)))
208 }
209 }
210 }
211 }
212 }
213
214 mod s2n_tls_bench_setup {
215 use std::time::SystemTime;
216
217 use super::*;
218
219 use s2n_tls::{enums::ClientAuthType, security::Policy};
220 impl TlsBenchConfig for cohort::S2NConfig {
221 fn make_config(
222 mode: Mode,
223 crypto_config: CryptoConfig,
224 handshake_type: HandshakeType,
225 ) -> Result<Self, Box<dyn Error>> {
226 // these security policies negotiate the given cipher suite and key
227 // exchange group as their top choice
228 let security_policy = match (crypto_config.cipher_suite, crypto_config.kx_group) {
229 (CipherSuite::TLS_AES_128_GCM_SHA256, KXGroup::Secp256R1) => "20230317",
230 (CipherSuite::TLS_AES_256_GCM_SHA384, KXGroup::Secp256R1) => "20190802",
231 (CipherSuite::TLS_AES_128_GCM_SHA256, KXGroup::X25519) => "20240417",
232 (CipherSuite::TLS_AES_256_GCM_SHA384, KXGroup::X25519) => "20190801",
233 (CipherSuite::TLS_AES_128_GCM_SHA256, KXGroup::X25519MLKEM768) => "20251014",
234 (CipherSuite::TLS_AES_256_GCM_SHA384, KXGroup::X25519MLKEM768) => {
235 "PQ-TLS-1-2-2024-10-09"
236 }
237 };
238
239 let mut builder = s2n_tls::config::Builder::new();
240 builder
241 .set_security_policy(&Policy::from_version(security_policy)?)?
242 .with_system_certs(false)?
243 .set_client_auth_type(match handshake_type {
244 HandshakeType::MutualAuth => ClientAuthType::Required,
245 _ => ClientAuthType::None, // ServerAuth or resumption handshake
246 })?;
247
248 if handshake_type == HandshakeType::Resumption {
249 builder.enable_session_tickets(true)?;
250 }
251
252 let session_ticket_storage = cohort::s2n_tls::SessionTicketStorage::default();
253
254 match mode {
255 Mode::Client => {
256 builder
257 .trust_pem(read_to_bytes(CACert, crypto_config.sig_type).as_slice())?
258 .set_verify_host_callback(cohort::s2n_tls::LOCALHOST_VERIFY_CALLBACK)?;
259
260 match handshake_type {
261 HandshakeType::MutualAuth => {
262 builder.load_pem(
263 read_to_bytes(ClientCertChain, crypto_config.sig_type).as_slice(),
264 read_to_bytes(ClientKey, crypto_config.sig_type).as_slice(),
265 )?;
266 }
267 HandshakeType::Resumption => {
268 builder.set_session_ticket_callback(session_ticket_storage.clone())?;
269 }
270 // no special configuration
271 HandshakeType::ServerAuth => {}
272 }
273 }
274 Mode::Server => {
275 builder.load_pem(
276 read_to_bytes(ServerCertChain, crypto_config.sig_type).as_slice(),
277 read_to_bytes(ServerKey, crypto_config.sig_type).as_slice(),
278 )?;
279
280 match handshake_type {
281 HandshakeType::MutualAuth => {
282 builder
283 .trust_pem(
284 read_to_bytes(CACert, crypto_config.sig_type).as_slice(),
285 )?
286 .set_verify_host_callback(
287 cohort::s2n_tls::LOCALHOST_VERIFY_CALLBACK,
288 )?;
289 }
290 HandshakeType::Resumption => {
291 builder.add_session_ticket_key(
292 cohort::s2n_tls::KEY_NAME.as_bytes(),
293 cohort::s2n_tls::KEY_VALUE.as_slice(),
294 // use a time that we are sure is in the past to
295 // make the key immediately available
296 SystemTime::UNIX_EPOCH,
297 )?;
298 }
299 // no special configuration for normal handshake
300 HandshakeType::ServerAuth => {}
301 };
302 }
303 }
304
305 Ok(S2NConfig {
306 config: builder.build()?,
307 ticket_storage: session_ticket_storage,
308 })
309 }
310 }
311 }