Skip to content

Commit cc20edb

Browse files
authored
test(bench): add api for mutual auth handshake (#5437)
1 parent c15341f commit cc20edb

File tree

6 files changed

+50
-14
lines changed

6 files changed

+50
-14
lines changed

bindings/rust/extended/s2n-tls/src/connection.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,7 @@ impl Connection {
937937

938938
/// Check if client auth was used for a connection.
939939
///
940-
/// This is only relevant if [`ClientAuthType::Optional] was used.
940+
/// This is especially useful when the server has [`ClientAuthType::Optional`] configured.
941941
///
942942
/// Corresponds to [s2n_connection_client_cert_used].
943943
pub fn client_cert_used(&self) -> bool {

bindings/rust/standard/bench/src/harness/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ pub enum Mode {
7575
Server,
7676
}
7777

78+
/// While ServerAuth and Resumption are not mutually exclusive, they are treated
79+
/// as such for the purpose of benchmarking.
7880
#[derive(Clone, Copy, Default, EnumIter, Eq, PartialEq)]
7981
pub enum HandshakeType {
8082
#[default]
@@ -194,9 +196,11 @@ pub trait TlsInfo: Sized {
194196

195197
fn negotiated_tls13(&self) -> bool;
196198

197-
/// Describes whether a connection was resumed. This method is only valid on
198-
/// server connections because of rustls API limitations.
199+
/// Describes whether a connection was resumed.
199200
fn resumed_connection(&self) -> bool;
201+
202+
/// For the rustls & openssl implementations, this only works for servers.
203+
fn mutual_auth(&self) -> bool;
200204
}
201205

202206
/// A TlsConnPair owns the client and server tls connections along with the IO buffers.

bindings/rust/standard/bench/src/openssl.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,11 @@ impl TlsInfo for OpenSslConnection {
275275
fn resumed_connection(&self) -> bool {
276276
self.connection.ssl().session_reused()
277277
}
278+
279+
fn mutual_auth(&self) -> bool {
280+
assert!(self.connection.ssl().is_server());
281+
self.connection.ssl().verified_chain().is_some()
282+
}
278283
}
279284

280285
#[cfg(test)]

bindings/rust/standard/bench/src/rustls.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustls::{
2121
pki_types::{CertificateDer, PrivateKeyDer, ServerName},
2222
server::{ProducesTickets, WebPkiClientVerifier},
2323
version::TLS13,
24-
ClientConfig, ClientConnection, Connection,
24+
ClientConfig, ClientConnection, CommonState, Connection, HandshakeKind,
2525
ProtocolVersion::TLSv1_3,
2626
RootCertStore, ServerConfig, ServerConnection,
2727
};
@@ -63,6 +63,13 @@ impl RustlsConnection {
6363
},
6464
}
6565
}
66+
67+
fn connection_common(&self) -> &CommonState {
68+
match &self.connection {
69+
Connection::Client(client_connection) => client_connection,
70+
Connection::Server(server_connection) => server_connection,
71+
}
72+
}
6673
}
6774

6875
#[derive(Debug)]
@@ -310,11 +317,15 @@ impl TlsInfo for RustlsConnection {
310317
}
311318

312319
fn resumed_connection(&self) -> bool {
313-
if let rustls::Connection::Server(s) = &self.connection {
314-
s.received_resumption_data().is_some()
315-
} else {
316-
panic!("rustls connection resumption status must be check on the server side");
317-
}
320+
self.connection_common().handshake_kind().unwrap() == HandshakeKind::Resumed
321+
}
322+
323+
fn mutual_auth(&self) -> bool {
324+
assert!(matches!(self.connection, Connection::Server(_)));
325+
//> For servers, this is the certificate chain or the raw public key of
326+
//> the client, if client authentication was completed.
327+
//> https://docs.rs/rustls/latest/rustls/struct.CommonState.html#method.peer_certificates
328+
self.connection_common().peer_certificates().is_some()
318329
}
319330
}
320331

bindings/rust/standard/bench/src/s2n_tls.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -325,11 +325,13 @@ impl TlsInfo for S2NConnection {
325325
}
326326

327327
fn resumed_connection(&self) -> bool {
328-
!self
329-
.connection
330-
.handshake_type()
331-
.unwrap()
332-
.contains("FULL_HANDSHAKE")
328+
let handshake_type = self.connection.handshake_type().unwrap();
329+
assert!(handshake_type.contains("NEGOTIATED"));
330+
!handshake_type.contains("FULL_HANDSHAKE")
331+
}
332+
333+
fn mutual_auth(&self) -> bool {
334+
self.connection.client_cert_used()
333335
}
334336
}
335337

bindings/rust/standard/bench/src/test_utilities.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,20 @@ where
6666

6767
assert!(conn_pair.negotiated_tls13());
6868
assert_eq!(cipher_suite, conn_pair.get_negotiated_cipher_suite());
69+
match handshake_type {
70+
HandshakeType::ServerAuth => {
71+
assert!(!conn_pair.server.mutual_auth());
72+
assert!(!conn_pair.server.resumed_connection());
73+
}
74+
HandshakeType::MutualAuth => {
75+
assert!(conn_pair.server.mutual_auth());
76+
assert!(!conn_pair.server.resumed_connection());
77+
}
78+
HandshakeType::Resumption => {
79+
assert!(!conn_pair.server.mutual_auth());
80+
assert!(conn_pair.server.resumed_connection());
81+
}
82+
}
6983

7084
// read in "application data" handshake messages.
7185
// "NewSessionTicket" in the case of resumption

0 commit comments

Comments
 (0)