Skip to content

Commit

Permalink
feat(bench): impl into for base config type (#5056)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmayclin authored Jan 24, 2025
1 parent eb93bc0 commit eab019f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 20 deletions.
14 changes: 8 additions & 6 deletions bindings/rust/standard/bench/src/harness/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

mod io;
pub use io::{LocalDataBuffer, ViewIO};
pub use io::{LocalDataBuffer, TestPairIO, ViewIO};

use io::TestPairIO;
use std::{error::Error, fmt::Debug, fs::read_to_string, rc::Rc};
use strum::EnumIter;

Expand Down Expand Up @@ -157,7 +155,11 @@ pub trait TlsConnection: Sized {
fn name() -> String;

/// Make connection from existing config and buffer
fn new_from_config(config: &Self::Config, io: ViewIO) -> Result<Self, Box<dyn Error>>;
fn new_from_config(
mode: Mode,
config: &Self::Config,
io: &TestPairIO,
) -> Result<Self, Box<dyn Error>>;

/// Run one handshake step: receive msgs from other connection, process, and send new msgs
fn handshake(&mut self) -> Result<(), Box<dyn Error>>;
Expand Down Expand Up @@ -255,8 +257,8 @@ where
server_tx_stream: Rc::pin(Default::default()),
client_tx_stream: Rc::pin(Default::default()),
};
let client = C::new_from_config(client_config, io.client_view()).unwrap();
let server = S::new_from_config(server_config, io.server_view()).unwrap();
let client = C::new_from_config(Mode::Client, client_config, &io).unwrap();
let server = S::new_from_config(Mode::Server, server_config, &io).unwrap();
Self { client, server, io }
}

Expand Down
24 changes: 21 additions & 3 deletions bindings/rust/standard/bench/src/openssl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
use crate::{
get_cert_path,
harness::{
CipherSuite, CryptoConfig, HandshakeType, KXGroup, Mode, TlsBenchConfig, TlsConnection,
ViewIO,
self, CipherSuite, CryptoConfig, HandshakeType, KXGroup, Mode, TlsBenchConfig,
TlsConnection, ViewIO,
},
PemType::*,
};
Expand Down Expand Up @@ -42,6 +42,15 @@ pub struct OpenSslConfig {
session_ticket_storage: SessionTicketStorage,
}

impl From<SslContext> for OpenSslConfig {
fn from(value: SslContext) -> Self {
OpenSslConfig {
config: value,
session_ticket_storage: Default::default(),
}
}
}

impl TlsBenchConfig for OpenSslConfig {
fn make_config(
mode: Mode,
Expand Down Expand Up @@ -147,7 +156,11 @@ impl TlsConnection for OpenSslConnection {
)
}

fn new_from_config(config: &Self::Config, io: ViewIO) -> Result<Self, Box<dyn Error>> {
fn new_from_config(
mode: harness::Mode,
config: &Self::Config,
io: &harness::TestPairIO,
) -> Result<Self, Box<dyn Error>> {
// check if there is a session ticket available
// a session ticket will only be available if the Config was created
// with session resumption enabled
Expand All @@ -166,6 +179,11 @@ impl TlsConnection for OpenSslConnection {
unsafe { connection.set_session(ticket)? };
}

let io = match mode {
Mode::Client => io.client_view(),
Mode::Server => io.server_view(),
};

let connection = SslStream::new(connection, io)?;
Ok(Self { connection })
}
Expand Down
27 changes: 24 additions & 3 deletions bindings/rust/standard/bench/src/rustls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

use crate::{
harness::{
read_to_bytes, CipherSuite, CryptoConfig, HandshakeType, KXGroup, Mode, TlsBenchConfig,
TlsConnection, ViewIO,
self, read_to_bytes, CipherSuite, CryptoConfig, HandshakeType, KXGroup, Mode,
TlsBenchConfig, TlsConnection, ViewIO,
},
PemType::{self, *},
SigType,
Expand Down Expand Up @@ -90,6 +90,18 @@ pub enum RustlsConfig {
Server(Arc<ServerConfig>),
}

impl From<ClientConfig> for RustlsConfig {
fn from(value: ClientConfig) -> Self {
RustlsConfig::Client(value.into())
}
}

impl From<ServerConfig> for RustlsConfig {
fn from(value: ServerConfig) -> Self {
RustlsConfig::Server(value.into())
}
}

impl TlsBenchConfig for RustlsConfig {
fn make_config(
mode: Mode,
Expand Down Expand Up @@ -170,7 +182,11 @@ impl TlsConnection for RustlsConnection {
"rustls".to_string()
}

fn new_from_config(config: &Self::Config, io: ViewIO) -> Result<Self, Box<dyn Error>> {
fn new_from_config(
mode: harness::Mode,
config: &Self::Config,
io: &harness::TestPairIO,
) -> Result<Self, Box<dyn Error>> {
let connection = match config {
RustlsConfig::Client(config) => Connection::Client(ClientConnection::new(
config.clone(),
Expand All @@ -181,6 +197,11 @@ impl TlsConnection for RustlsConnection {
}
};

let io = match mode {
Mode::Client => io.client_view(),
Mode::Server => io.server_view(),
};

Ok(Self { io, connection })
}

Expand Down
32 changes: 24 additions & 8 deletions bindings/rust/standard/bench/src/s2n_tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

use crate::{
harness::{
read_to_bytes, CipherSuite, CryptoConfig, HandshakeType, KXGroup, LocalDataBuffer, Mode,
TlsConnection, ViewIO,
self, read_to_bytes, CipherSuite, CryptoConfig, HandshakeType, KXGroup, LocalDataBuffer,
Mode, TlsConnection,
},
PemType::*,
};
Expand Down Expand Up @@ -57,11 +57,19 @@ const KEY_VALUE: [u8; 16] = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3];

/// s2n-tls has mode-independent configs, so this struct wraps the config with the mode
pub struct S2NConfig {
mode: Mode,
config: s2n_tls::config::Config,
ticket_storage: SessionTicketStorage,
}

impl From<s2n_tls::config::Config> for S2NConfig {
fn from(value: s2n_tls::config::Config) -> Self {
S2NConfig {
config: value,
ticket_storage: Default::default(),
}
}
}

impl crate::harness::TlsBenchConfig for S2NConfig {
fn make_config(
mode: Mode,
Expand All @@ -80,7 +88,7 @@ impl crate::harness::TlsBenchConfig for S2NConfig {
let mut builder = Builder::new();
builder
.set_security_policy(&Policy::from_version(security_policy)?)?
.wipe_trust_store()?
.with_system_certs(false)?
.set_client_auth_type(match handshake_type {
HandshakeType::MutualAuth => ClientAuthType::Required,
_ => ClientAuthType::None, // ServerAuth or resumption handshake
Expand Down Expand Up @@ -144,7 +152,6 @@ impl crate::harness::TlsBenchConfig for S2NConfig {
}

Ok(S2NConfig {
mode,
config: builder.build()?,
ticket_storage: session_ticket_storage,
})
Expand Down Expand Up @@ -205,15 +212,24 @@ impl TlsConnection for S2NConnection {
"s2n-tls".to_string()
}

fn new_from_config(config: &Self::Config, io: ViewIO) -> Result<Self, Box<dyn Error>> {
let mode = match config.mode {
fn new_from_config(
mode: harness::Mode,
config: &Self::Config,
io: &harness::TestPairIO,
) -> Result<Self, Box<dyn Error>> {
let s2n_mode = match mode {
Mode::Client => s2n_tls::enums::Mode::Client,
Mode::Server => s2n_tls::enums::Mode::Server,
};

let io = match mode {
Mode::Client => io.client_view(),
Mode::Server => io.server_view(),
};

let io = Box::pin(io);

let mut connection = Connection::new(mode);
let mut connection = Connection::new(s2n_mode);
connection
.set_blinding(Blinding::SelfService)?
.set_config(config.config.clone())?
Expand Down

0 comments on commit eab019f

Please sign in to comment.