Debian Codegraph @

crt/s2n/bindings/rust/standard/integration/src/mtls/async_verify_and_offload.rs

raw symbols SCIP
1 // Copyright Amazon.com, Inc. or its affiliates.
2 // SPDX-License-Identifier: Apache-2.0
3
4 // mTLS async verify + async offload test
5 // This is a "stress test" of our mTLS async callback which configures:
6 // - rustls client (TLS 1.3)
7 // - s2n-tls server
8 // - async certificate validation callback
9 // - async offload operation (pkey verify)
10
11 use super::*;
12 use s2n_tls_sys::{
13 s2n_async_offload_op, s2n_async_offload_op_perform, s2n_async_offload_op_type,
14 s2n_config_set_async_offload_callback,
15 };
16 use std::ffi::c_void;
17
18 /// A wrapper around a raw pointer to `s2n_async_offload_op` that can be sent across threads.
19 ///
20 /// This is used in tests to simulate async offload operations where the operation
21 /// is deferred and performed on a different thread or after some async operation.
22 struct SendableAsyncOffloadOp(*mut s2n_async_offload_op);
23
24 // SAFETY: The pointer is owned by s2n-tls and remains valid for the duration of the
25 // pending async offload operation (until perform() is called, or freed).
26 // The test mimics the intended usage pattern where an application hands off the pointer
27 // to a worker thread that later performs the operation.
28 unsafe impl Send for SendableAsyncOffloadOp {}
29
30 // Async offload context for C FFI
31 struct AsyncOffloadCtx {
32 invoked: Arc<AtomicU64>,
33 sender: Sender<SendableAsyncOffloadOp>,
34 }
35
36 // C-style async offload callback
37 unsafe extern "C" fn test_async_offload_cb(
38 _conn: *mut s2n_connection,
39 op: *mut s2n_async_offload_op,
40 ctx: *mut c_void,
41 ) -> i32 {
42 let ctx = unsafe { Box::from_raw(ctx as *mut AsyncOffloadCtx) };
43
44 ctx.invoked.fetch_add(1, Ordering::SeqCst);
45 ctx.sender.send(SendableAsyncOffloadOp(op)).unwrap();
46
47 s2n_status_code::SUCCESS
48 }
49
50 /// Registers an async pkey verify offload callback and returns (invoked_counter, operation_receiver).
51 fn register_async_pkey_verify_offload(
52 s2n_cfg: &mut S2NConfig,
53 ) -> (Arc<AtomicU64>, Receiver<SendableAsyncOffloadOp>) {
54 let invoked = Arc::new(AtomicU64::new(0));
55 let (tx, rx) = std::sync::mpsc::channel();
56
57 let ctx = Box::new(AsyncOffloadCtx {
58 invoked: Arc::clone(&invoked),
59 sender: tx,
60 });
61 let ctx_ptr = Box::into_raw(ctx) as *mut c_void;
62
63 // SAFETY: s2n stores this context pointer and later returns it in the async
64 // callback. The callback will reclaim ownership using Box::from_raw to prevent leaks.
65 unsafe {
66 let raw = raw_config(s2n_cfg);
67 let allowed_types = s2n_async_offload_op_type::OFFLOAD_PKEY_VERIFY;
68
69 let result = s2n_config_set_async_offload_callback(
70 raw,
71 allowed_types,
72 Some(test_async_offload_cb),
73 ctx_ptr,
74 );
75 assert_eq!(
76 result,
77 s2n_status_code::SUCCESS,
78 "s2n_config_set_async_offload_callback failed"
79 );
80 }
81
82 (invoked, rx)
83 }
84
85 /// rustls client and s2n-tls server with async cert validation and async pkey
86 /// verify offload over TLS 1.3.
87 #[test]
88 fn s2n_server_tls13() {
89 crate::capability_check::required_capability(
90 &[crate::capability_check::Capability::Tls13],
91 || {
92 let client = rustls_mtls_client(SigType::Rsa2048, &rustls::version::TLS13);
93
94 let (server, cert_invoked, cert_rx, offload_invoked, offload_rx) = {
95 let builder = s2n_mtls_base_builder(SigType::Rsa2048);
96 let mut s2n_cfg = S2NConfig::from(builder.build().unwrap());
97
98 let (cert_invoked, cert_rx) = register_async_cert_callback(&mut s2n_cfg);
99 let (offload_invoked, offload_rx) =
100 register_async_pkey_verify_offload(&mut s2n_cfg);
101
102 (s2n_cfg, cert_invoked, cert_rx, offload_invoked, offload_rx)
103 };
104
105 let mut pair =
106 TlsConnPair::<RustlsConnection, S2NConnection>::from_configs(&client, &server);
107
108 // Drive early handshake messages (ClientHello, ServerHello, EncryptedExtensions)
109 pair.client.handshake().unwrap();
110 pair.server.handshake().unwrap();
111
112 // Progress until just before client Certificate is processed:
113 pair.client.handshake().unwrap();
114 assert_eq!(cert_invoked.load(Ordering::SeqCst), 0);
115 assert_eq!(offload_invoked.load(Ordering::SeqCst), 0);
116
117 // Server processes client Certificate -> async cert validation fires
118 pair.server.handshake().unwrap();
119 assert_eq!(cert_invoked.load(Ordering::SeqCst), 1);
120
121 let cert_ptr = cert_rx.recv().unwrap().0;
122 unsafe {
123 assert_eq!(s2n_cert_validation_accept(cert_ptr), 0);
124 }
125
126 // Continue server handshake: async offload for pkey verify fires
127 pair.server.handshake().unwrap();
128 assert_eq!(offload_invoked.load(Ordering::SeqCst), 1);
129
130 let SendableAsyncOffloadOp(offload_op_ptr) = offload_rx.recv().unwrap();
131 unsafe {
132 assert_eq!(s2n_async_offload_op_perform(offload_op_ptr), 0);
133 }
134
135 pair.handshake().unwrap();
136 pair.round_trip_assert(10).unwrap();
137 pair.shutdown().unwrap();
138 },
139 );
140 }