Debian Codegraph @

crt/s2n/bindings/rust/extended/s2n-tls/src/testing/client_hello.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::{
5 callbacks::{ClientHelloCallback, ConnectionFuture},
6 error, security,
7 };
8 use alloc::sync::Arc;
9 use core::{sync::atomic::Ordering, task::Poll};
10 use s2n_tls_sys::{s2n_client_hello_has_extension, s2n_connection_get_client_hello};
11 use std::{fmt, io, pin::Pin, sync::atomic::AtomicUsize};
12
13 // The Future returned by MockClientHelloHandler.
14 //
15 // An instance of this Future is stored on the connection and
16 // polled to make progress in the async client_hello_callback
17 pub struct MockClientHelloFuture {
18 require_pending_count: usize,
19 invoked: Arc<AtomicUsize>,
20 }
21
22 impl ConnectionFuture for MockClientHelloFuture {
23 fn poll(
24 self: Pin<&mut Self>,
25 connection: &mut crate::connection::Connection,
26 _ctx: &mut core::task::Context,
27 ) -> Poll<Result<(), error::Error>> {
28 if self.invoked.fetch_add(1, Ordering::SeqCst) < self.require_pending_count {
29 // confirm the callback can access the waker
30 connection.waker().unwrap().wake_by_ref();
31 return Poll::Pending;
32 }
33
34 // Test that the config can be changed
35 connection
36 .set_config(super::build_config(&security::DEFAULT_TLS13).unwrap())
37 .unwrap();
38
39 // Test that server_name_extension_used can be invoked
40 connection.server_name_extension_used();
41
42 Poll::Ready(Ok(()))
43 }
44 }
45
46 #[derive(Clone)]
47 pub struct MockClientHelloHandler {
48 require_pending_count: usize,
49 pub invoked: Arc<AtomicUsize>,
50 }
51
52 impl MockClientHelloHandler {
53 pub fn new(require_pending_count: usize) -> Self {
54 Self {
55 require_pending_count,
56 invoked: Arc::new(AtomicUsize::new(0)),
57 }
58 }
59 }
60
61 impl ClientHelloCallback for MockClientHelloHandler {
62 fn on_client_hello(
63 &self,
64 _connection: &mut crate::connection::Connection,
65 ) -> Result<Option<Pin<Box<dyn ConnectionFuture>>>, crate::error::Error> {
66 let fut = MockClientHelloFuture {
67 require_pending_count: self.require_pending_count,
68 invoked: self.invoked.clone(),
69 };
70
71 // returning `Some` indicates that the client_hello callback is
72 // not yet finished and that the supplied MockClientHelloFuture
73 // needs to be `poll`ed to make progress.
74 Ok(Some(Box::pin(fut)))
75 }
76 }
77
78 // A ClientHelloCallback which returns a Asynchronous task, which
79 // eventually returns an error.
80 #[derive(Default)]
81 pub struct FailingCHHandler;
82
83 impl ClientHelloCallback for FailingCHHandler {
84 fn on_client_hello(
85 &self,
86 _connection: &mut crate::connection::Connection,
87 ) -> Result<Option<Pin<Box<dyn ConnectionFuture>>>, error::Error> {
88 let io_error = io::Error::other(CustomError);
89 Err(crate::error::Error::application(Box::new(io_error)))
90 }
91 }
92
93 #[derive(Default)]
94 pub struct FailingAsyncCHHandler;
95 impl ClientHelloCallback for FailingAsyncCHHandler {
96 fn on_client_hello(
97 &self,
98 _connection: &mut crate::connection::Connection,
99 ) -> Result<Option<Pin<Box<dyn ConnectionFuture>>>, error::Error> {
100 let fut = FailingCHFuture::default();
101 Ok(Some(Box::pin(fut)))
102 }
103 }
104
105 // A ClientHelloCallback which returns a synchronous error.
106 #[derive(Default)]
107 struct FailingCHFuture {
108 pub invoked: Arc<AtomicUsize>,
109 }
110
111 impl ConnectionFuture for FailingCHFuture {
112 fn poll(
113 self: Pin<&mut Self>,
114 connection: &mut crate::connection::Connection,
115 _ctx: &mut core::task::Context,
116 ) -> Poll<Result<(), error::Error>> {
117 if self.invoked.fetch_add(1, Ordering::SeqCst) < 1 {
118 // confirm the callback can access the waker
119 connection.waker().unwrap().wake_by_ref();
120 return Poll::Pending;
121 }
122
123 let io_error = io::Error::other(CustomError);
124 let ret = Err(crate::error::Error::application(Box::new(io_error)));
125 Poll::Ready(ret)
126 }
127 }
128
129 impl Drop for FailingCHFuture {
130 // return pending once to simulate the async nature of the future and
131 // improve test coverage
132 fn drop(&mut self) {
133 assert!(self.invoked.load(Ordering::SeqCst) >= 1);
134 }
135 }
136 /// A client hello handler that asserts that the extension with the given
137 /// IANA code is either present or not present in the client hello
138 pub struct HasExtensionClientHelloHandler {
139 pub extension_iana: u16,
140 pub extension_expected: bool,
141 }
142
143 impl ClientHelloCallback for HasExtensionClientHelloHandler {
144 fn on_client_hello(
145 &self,
146 connection: &mut crate::connection::Connection,
147 ) -> Result<Option<Pin<Box<dyn ConnectionFuture>>>, error::Error> {
148 let mut exists = false;
149
150 unsafe {
151 let client_hello = s2n_connection_get_client_hello(connection.as_ptr());
152 s2n_client_hello_has_extension(client_hello, self.extension_iana, &mut exists as _);
153 }
154
155 if self.extension_expected {
156 assert!(
157 exists,
158 "Extension {} was not found in the client hello",
159 self.extension_iana
160 );
161 } else {
162 assert!(
163 !exists,
164 "Unexpected extension {} found in the client hello",
165 self.extension_iana
166 )
167 }
168
169 Ok(None)
170 }
171 }
172
173 #[derive(Debug)]
174 pub struct CustomError;
175
176 impl std::error::Error for CustomError {}
177 impl fmt::Display for CustomError {
178 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
179 write!(f, "custom error")
180 }
181 }