Debian Codegraph @

crt/s2n/bindings/rust/extended/s2n-tls/src/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::error::{Error, Fallible};
5 use s2n_tls_sys::*;
6 use std::fmt;
7
8 // ClientHello is an opaque wrapper struct around `s2n_client_hello`. Note that
9 // the size of this type is not known, and as such it can only be used through
10 // references and pointers.
11 //
12 // This implementation is motivated by the different memory management required
13 // for different s2n_client_hello pointers. `s2n_client_hello_parse_message`
14 // returns a `*mut s2n_client_hello` which owns its own memory. This neatly fits
15 // the "smart pointer" pattern and can be represented as a `Box<T>`.
16 //
17 // `s2n_connection_get_client_hello` returns a `*mut s2n_client_hello` which
18 // references memory owned by the connection, and therefore must not outlive
19 // the connection struct. This is best represented as a reference tied to the
20 // lifetime of the `Connection` struct.
21
22 pub struct ClientHello(s2n_client_hello);
23
24 impl ClientHello {
25 /// Corresponds to [`s2n_client_hello_parse_message`].
26 ///
27 /// The bytes must include the message header.
28 pub fn parse_client_hello(hello: &[u8]) -> Result<Box<Self>, crate::error::Error> {
29 crate::init::init();
30 let handle = unsafe {
31 s2n_client_hello_parse_message(hello.as_ptr(), hello.len() as u32).into_result()?
32 };
33 let client_hello = handle.as_ptr() as *mut ClientHello;
34 // safety: s2n_client_hello_parse_message returns a pointer that "owns"
35 // its memory. This memory must be cleaned up by the application. The
36 // Box<Self> will call Self::Drop when it goes out of scope so memory
37 // will be automatically managed.
38 unsafe { Ok(Box::from_raw(client_hello)) }
39 }
40
41 // this accepts a mut ref instead of a pointer, so that lifetimes are nicely
42 // calculated for us. As is always the case, the reference must not be null.
43 // this is marked "pub(crate)" to expose it to the connection module but
44 // prevent it from being used externally.
45 pub(crate) fn from_ptr(hello: &s2n_client_hello) -> &Self {
46 // SAFETY: casting *s2n_client_hello <-> *ClientHello: For repr(Rust),
47 // repr(packed(N)), repr(align(N)), and repr(C) structs: if all fields of a
48 // struct have size 0, then the struct has size 0.
49 // https://rust-lang.github.io/unsafe-code-guidelines/layout/structs-and-tuples.html#zero-sized-structs
50 unsafe { &*(hello as *const s2n_client_hello as *const ClientHello) }
51 }
52
53 // SAFETY: casting *const s2n_client_hello -> *mut s2n_client_hello: This is
54 // safe as long as the data is not actually mutated. As authors of s2n-tls,
55 // we know that the get_hash and get_fingerprint methods do not mutate the
56 // data, and use mut pointers as a matter of convention because it makes
57 // working with s2n_stuffers and s2n_blobs easier.
58 pub(crate) fn deref_mut_ptr(&self) -> *mut s2n_client_hello {
59 &self.0 as *const s2n_client_hello as *mut s2n_client_hello
60 }
61
62 /// Corresponds to [`s2n_client_hello_get_session_id`], but also
63 /// calls [`s2n_client_hello_get_session_id_length`].
64 pub fn session_id(&self) -> Result<Vec<u8>, Error> {
65 let mut session_id_length = 0;
66 unsafe {
67 s2n_client_hello_get_session_id_length(self.deref_mut_ptr(), &mut session_id_length)
68 .into_result()?;
69 }
70
71 let mut session_id = vec![0; session_id_length as usize];
72 let mut out_length = 0;
73 unsafe {
74 s2n_client_hello_get_session_id(
75 self.deref_mut_ptr(),
76 session_id.as_mut_ptr(),
77 &mut out_length,
78 session_id_length,
79 )
80 .into_result()?;
81 }
82 Ok(session_id)
83 }
84
85 /// Corresponds to [`s2n_client_hello_get_server_name`], but also
86 /// calls [`s2n_client_hello_get_server_name_length`].
87 pub fn server_name(&self) -> Result<Vec<u8>, Error> {
88 let mut server_name_length = 0;
89 unsafe {
90 s2n_client_hello_get_server_name_length(self.deref_mut_ptr(), &mut server_name_length)
91 .into_result()?;
92 }
93
94 let mut server_name = vec![0; server_name_length as usize];
95 let mut out_length = 0;
96 unsafe {
97 s2n_client_hello_get_server_name(
98 self.deref_mut_ptr(),
99 server_name.as_mut_ptr(),
100 server_name_length,
101 &mut out_length,
102 )
103 .into_result()?;
104 }
105 Ok(server_name)
106 }
107
108 /// Corresponds to [`s2n_client_hello_get_random`].
109 ///
110 /// Returns the 32-byte client random from the ClientHello message. The
111 /// random bytes in the message returned by [`Self::raw_message`] are
112 /// zeroed out, so this accessor is the only way to read them.
113 pub fn random(&self) -> Result<[u8; 32], Error> {
114 let mut out = [0u8; 32];
115 unsafe {
116 s2n_client_hello_get_random(self.deref_mut_ptr(), out.as_mut_ptr(), out.len() as u32)
117 .into_result()?;
118 }
119 Ok(out)
120 }
121
122 /// Corresponds to [`s2n_client_hello_get_raw_message`], but also
123 /// calls [`s2n_client_hello_get_raw_message_length`].
124 ///
125 /// The returned bytes do not include the message header.
126 pub fn raw_message(&self) -> Result<Vec<u8>, Error> {
127 let message_length =
128 unsafe { s2n_client_hello_get_raw_message_length(self.deref_mut_ptr()).into_result()? };
129
130 let mut raw_message = vec![0; message_length];
131 unsafe {
132 s2n_client_hello_get_raw_message(
133 self.deref_mut_ptr(),
134 raw_message.as_mut_ptr(),
135 message_length as u32,
136 )
137 .into_result()?
138 };
139 Ok(raw_message)
140 }
141 }
142
143 impl Drop for ClientHello {
144 /// Corresponds to [`s2n_client_hello_free`].
145 fn drop(&mut self) {
146 let mut client_hello: *mut s2n_client_hello = &mut self.0;
147 // ignore failures. There isn't anything to be done to handle them, but
148 // allowing the program to continue is preferable to crashing.
149 let _ = unsafe {
150 s2n_tls_sys::s2n_client_hello_free(std::ptr::addr_of_mut!(client_hello)).into_result()
151 };
152 }
153 }
154
155 impl fmt::Debug for ClientHello {
156 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
157 let session_id = self.session_id().map_err(|_| fmt::Error)?;
158 let session_id = hex::encode(session_id);
159 let message_head = self.raw_message().map_err(|_| fmt::Error)?;
160 f.debug_struct("ClientHello")
161 .field("session_id", &session_id)
162 .field("message_len", &(message_head.len()))
163 .finish_non_exhaustive()
164 }
165 }
166
167 // Leftover from when fingerprinting was implemented in this module
168 #[cfg(feature = "unstable-fingerprint")]
169 pub use crate::fingerprint::FingerprintType;
170
171 #[cfg(test)]
172 mod tests {
173 use crate::client_hello::ClientHello;
174
175 #[test]
176 fn invalid_client_bytes() {
177 let raw_client_hello_bytes =
178 "random_value_that_is_unlikely_to_be_valid_client_hello".as_bytes();
179 let result = ClientHello::parse_client_hello(raw_client_hello_bytes);
180 assert!(result.is_err());
181 }
182
183 #[test]
184 fn server_name() {
185 let raw_client_hello = vec![
186 0x01, 0x00, 0x00, 0xEC, 0x03, 0x03, 0x90, 0xe8, 0xcc, 0xee, 0xe5, 0x70, 0xa2, 0xa1,
187 0x2f, 0x6b, 0x69, 0xd2, 0x66, 0x96, 0x0f, 0xcf, 0x20, 0xd5, 0x32, 0x6e, 0xc4, 0xb2,
188 0x8c, 0xc7, 0xbd, 0x0a, 0x06, 0xc2, 0xa5, 0x14, 0xfc, 0x34, 0x20, 0xaf, 0x72, 0xbf,
189 0x39, 0x99, 0xfb, 0x20, 0x70, 0xc3, 0x10, 0x83, 0x0c, 0xee, 0xfb, 0xfa, 0x72, 0xcc,
190 0x5d, 0xa8, 0x99, 0xb4, 0xc5, 0x53, 0xd6, 0x3d, 0xa0, 0x53, 0x7a, 0x5c, 0xbc, 0xf5,
191 0x0b, 0x00, 0x1e, 0xc0, 0x2b, 0xc0, 0x2f, 0xcc, 0xa9, 0xcc, 0xa8, 0xc0, 0x2c, 0xc0,
192 0x30, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0x00, 0x33, 0x00, 0x39, 0x00,
193 0x2f, 0x00, 0x35, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x85, 0x00, 0x00, 0x00, 0x23, 0x00,
194 0x21, 0x00, 0x00, 0x1e, 0x69, 0x6e, 0x63, 0x6f, 0x6d, 0x69, 0x6e, 0x67, 0x2e, 0x74,
195 0x65, 0x6c, 0x65, 0x6d, 0x65, 0x74, 0x72, 0x79, 0x2e, 0x6d, 0x6f, 0x7a, 0x69, 0x6c,
196 0x6c, 0x61, 0x2e, 0x6f, 0x72, 0x67, 0x00, 0x17, 0x00, 0x00, 0xff, 0x01, 0x00, 0x01,
197 0x00, 0x00, 0x0a, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x1d, 0x00, 0x17, 0x00, 0x18, 0x00,
198 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00, 0x10, 0x00,
199 0x0e, 0x00, 0x0c, 0x02, 0x68, 0x32, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e,
200 0x31, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x18,
201 0x00, 0x16, 0x04, 0x03, 0x05, 0x03, 0x06, 0x03, 0x08, 0x04, 0x08, 0x05, 0x08, 0x06,
202 0x04, 0x01, 0x05, 0x01, 0x06, 0x01, 0x02, 0x03, 0x02, 0x01, 0x00, 0x1c, 0x00, 0x02,
203 0x40, 0x00,
204 ];
205 let client_hello = ClientHello::parse_client_hello(raw_client_hello.as_slice()).unwrap();
206 let server_name = client_hello.server_name().unwrap();
207 assert_eq!("incoming.telemetry.mozilla.org".as_bytes(), server_name);
208 }
209 }