| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
use crate::error::{Error, Fallible}; |
| 5 |
use s2n_tls_sys::*; |
| 6 |
use std::fmt; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
pub struct ClientHello(s2n_client_hello); |
| 23 |
|
| 24 |
impl ClientHello { |
| 25 |
|
| 26 |
|
| 27 |
|
| 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 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
unsafe { Ok(Box::from_raw(client_hello)) } |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
pub(crate) fn from_ptr(hello: &s2n_client_hello) -> &Self { |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
unsafe { &*(hello as *const s2n_client_hello as *const ClientHello) } |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 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 |
|
| 63 |
|
| 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 |
|
| 86 |
|
| 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 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 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 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 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 |
|
| 145 |
fn drop(&mut self) { |
| 146 |
let mut client_hello: *mut s2n_client_hello = &mut self.0; |
| 147 |
|
| 148 |
|
| 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 |
|
| 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 |
} |