| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
use crate::{ |
| 5 |
harness::{self, read_to_bytes, Mode, TlsConfigBuilder, TlsConnection, TlsInfo, ViewIO}, |
| 6 |
PemType, |
| 7 |
}; |
| 8 |
use s2n_tls::{ |
| 9 |
callbacks::{SessionTicketCallback, VerifyHostNameCallback}, |
| 10 |
connection::Connection, |
| 11 |
enums::{Blinding, Version}, |
| 12 |
security::Policy, |
| 13 |
}; |
| 14 |
use std::{ |
| 15 |
borrow::BorrowMut, |
| 16 |
error::Error, |
| 17 |
ffi::c_void, |
| 18 |
io::ErrorKind, |
| 19 |
os::raw::c_int, |
| 20 |
pin::Pin, |
| 21 |
rc::Rc, |
| 22 |
sync::{Arc, Mutex}, |
| 23 |
task::Poll, |
| 24 |
}; |
| 25 |
|
| 26 |
pub const LOCALHOST_VERIFY_CALLBACK: HostNameHandler = HostNameHandler { |
| 27 |
expected_server_name: "localhost", |
| 28 |
}; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
pub struct HostNameHandler { |
| 33 |
expected_server_name: &'static str, |
| 34 |
} |
| 35 |
impl HostNameHandler { |
| 36 |
pub fn new(expected_server_name: &'static str) -> Self { |
| 37 |
Self { |
| 38 |
expected_server_name, |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
impl VerifyHostNameCallback for HostNameHandler { |
| 44 |
fn verify_host_name(&self, hostname: &str) -> bool { |
| 45 |
self.expected_server_name == hostname |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
pub unsafe extern "C" fn generic_send_cb<T: std::io::Write>( |
| 55 |
context: *mut c_void, |
| 56 |
data: *const u8, |
| 57 |
len: u32, |
| 58 |
) -> c_int { |
| 59 |
let context: &mut T = &mut *(context as *mut T); |
| 60 |
let data = core::slice::from_raw_parts(data, len as _); |
| 61 |
let bytes_written = context.write(data).unwrap(); |
| 62 |
bytes_written as c_int |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
pub unsafe extern "C" fn generic_recv_cb<T: std::io::Read>( |
| 71 |
context: *mut c_void, |
| 72 |
data: *mut u8, |
| 73 |
len: u32, |
| 74 |
) -> c_int { |
| 75 |
let context: &mut T = &mut *(context as *mut T); |
| 76 |
let data = core::slice::from_raw_parts_mut(data, len as _); |
| 77 |
match context.read(data) { |
| 78 |
Ok(len) => len as c_int, |
| 79 |
Err(err) if err.kind() == ErrorKind::WouldBlock => { |
| 80 |
errno::set_errno(errno::Errno(libc::EWOULDBLOCK)); |
| 81 |
-1 |
| 82 |
} |
| 83 |
Err(unrecognized) => panic!("unexpected error: {unrecognized}"), |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
#[derive(Clone, Debug, Default)] |
| 88 |
pub struct SessionTicketStorage(Arc<Mutex<Option<Vec<u8>>>>); |
| 89 |
|
| 90 |
impl SessionTicketStorage { |
| 91 |
|
| 92 |
pub fn get_ticket(self) -> Vec<u8> { |
| 93 |
self.0.lock().unwrap().borrow_mut().take().unwrap() |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
impl SessionTicketCallback for SessionTicketStorage { |
| 98 |
fn on_session_ticket( |
| 99 |
&self, |
| 100 |
_connection: &mut s2n_tls::connection::Connection, |
| 101 |
session_ticket: &s2n_tls::callbacks::SessionTicket, |
| 102 |
) { |
| 103 |
let mut ticket = vec![0; session_ticket.len().unwrap()]; |
| 104 |
session_ticket.data(&mut ticket).unwrap(); |
| 105 |
let _ = self.0.lock().unwrap().insert(ticket); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
pub const KEY_NAME: &str = "InsecureTestKey"; |
| 110 |
pub const KEY_VALUE: [u8; 16] = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3]; |
| 111 |
|
| 112 |
pub struct S2NConfig { |
| 113 |
pub config: s2n_tls::config::Config, |
| 114 |
pub ticket_storage: SessionTicketStorage, |
| 115 |
} |
| 116 |
|
| 117 |
impl From<s2n_tls::config::Config> for S2NConfig { |
| 118 |
fn from(value: s2n_tls::config::Config) -> Self { |
| 119 |
S2NConfig { |
| 120 |
config: value, |
| 121 |
ticket_storage: Default::default(), |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
#[allow(dead_code)] |
| 130 |
pub struct S2NConnection { |
| 131 |
io: Pin<Box<ViewIO>>, |
| 132 |
connection: Connection, |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
handshake_done: bool, |
| 138 |
} |
| 139 |
|
| 140 |
impl S2NConnection { |
| 141 |
pub fn connection(&self) -> &Connection { |
| 142 |
&self.connection |
| 143 |
} |
| 144 |
|
| 145 |
pub fn connection_mut(&mut self) -> &mut Connection { |
| 146 |
&mut self.connection |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
impl TlsConnection for S2NConnection { |
| 151 |
type Config = S2NConfig; |
| 152 |
|
| 153 |
fn new_from_config( |
| 154 |
mode: harness::Mode, |
| 155 |
config: &Self::Config, |
| 156 |
io: &Rc<harness::TestPairIO>, |
| 157 |
) -> Result<Self, Box<dyn Error>> { |
| 158 |
let s2n_mode = match mode { |
| 159 |
Mode::Client => s2n_tls::enums::Mode::Client, |
| 160 |
Mode::Server => s2n_tls::enums::Mode::Server, |
| 161 |
}; |
| 162 |
|
| 163 |
let io = match mode { |
| 164 |
Mode::Client => io.client_view(), |
| 165 |
Mode::Server => io.server_view(), |
| 166 |
}; |
| 167 |
|
| 168 |
let mut io = Box::pin(io); |
| 169 |
|
| 170 |
let mut connection = Connection::new(s2n_mode); |
| 171 |
connection |
| 172 |
.set_blinding(Blinding::SelfService)? |
| 173 |
.set_config(config.config.clone())? |
| 174 |
.set_send_callback(Some(generic_send_cb::<ViewIO>))? |
| 175 |
.set_receive_callback(Some(generic_recv_cb::<ViewIO>))?; |
| 176 |
unsafe { |
| 177 |
connection |
| 178 |
.set_send_context(&mut *io as *mut ViewIO as *mut c_void)? |
| 179 |
.set_receive_context(&mut *io as *mut ViewIO as *mut c_void)?; |
| 180 |
} |
| 181 |
|
| 182 |
if let Some(ticket) = config.ticket_storage.0.lock().unwrap().borrow_mut().take() { |
| 183 |
connection.set_session_ticket(&ticket)?; |
| 184 |
} |
| 185 |
|
| 186 |
Ok(Self { |
| 187 |
io, |
| 188 |
connection, |
| 189 |
handshake_done: false, |
| 190 |
}) |
| 191 |
} |
| 192 |
|
| 193 |
fn handshake(&mut self) -> Result<(), Box<dyn Error>> { |
| 194 |
if let Poll::Ready(res) = self.connection.poll_negotiate() { |
| 195 |
res?; |
| 196 |
self.handshake_done = true; |
| 197 |
} |
| 198 |
Ok(()) |
| 199 |
} |
| 200 |
|
| 201 |
fn handshake_completed(&self) -> bool { |
| 202 |
self.handshake_done |
| 203 |
} |
| 204 |
|
| 205 |
fn send(&mut self, data: &[u8]) { |
| 206 |
let mut write_offset = 0; |
| 207 |
while write_offset < data.len() { |
| 208 |
match self.connection.poll_send(&data[write_offset..]) { |
| 209 |
Poll::Ready(bytes_written) => write_offset += bytes_written.unwrap(), |
| 210 |
Poll::Pending => panic!("unexpected `Pending` poll"), |
| 211 |
} |
| 212 |
assert!(self.connection.poll_flush().is_ready()); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
fn recv(&mut self, data: &mut [u8]) -> std::io::Result<()> { |
| 217 |
let data_len = data.len(); |
| 218 |
let mut read_offset = 0; |
| 219 |
while read_offset < data_len { |
| 220 |
match self.connection.poll_recv(data) { |
| 221 |
Poll::Ready(bytes_read) => read_offset += bytes_read?, |
| 222 |
Poll::Pending => { |
| 223 |
return Err(std::io::Error::new( |
| 224 |
ErrorKind::WouldBlock, |
| 225 |
"poll_recv returned pending", |
| 226 |
)) |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
Ok(()) |
| 231 |
} |
| 232 |
|
| 233 |
fn shutdown_send(&mut self) { |
| 234 |
assert!(matches!( |
| 235 |
self.connection.poll_shutdown_send(), |
| 236 |
Poll::Ready(_) |
| 237 |
)); |
| 238 |
} |
| 239 |
|
| 240 |
fn shutdown_finish(&mut self) -> bool { |
| 241 |
matches!(self.connection.poll_shutdown(), Poll::Ready(_)) |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
impl TlsInfo for S2NConnection { |
| 246 |
fn name() -> String { |
| 247 |
"s2n-tls".to_string() |
| 248 |
} |
| 249 |
|
| 250 |
fn get_negotiated_cipher_suite(&self) -> String { |
| 251 |
self.connection.cipher_suite().unwrap().to_string() |
| 252 |
} |
| 253 |
|
| 254 |
fn negotiated_tls13(&self) -> bool { |
| 255 |
self.connection.actual_protocol_version().unwrap() == Version::TLS13 |
| 256 |
} |
| 257 |
|
| 258 |
fn resumed_connection(&self) -> bool { |
| 259 |
let handshake_type = self.connection.handshake_type().unwrap(); |
| 260 |
assert!(handshake_type.contains("NEGOTIATED")); |
| 261 |
!handshake_type.contains("FULL_HANDSHAKE") |
| 262 |
} |
| 263 |
|
| 264 |
fn mutual_auth(&self) -> bool { |
| 265 |
self.connection.client_cert_used() |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
impl TlsConfigBuilder for s2n_tls::config::Builder { |
| 270 |
type Config = S2NConfig; |
| 271 |
|
| 272 |
fn new_test_config(_mode: Mode) -> Self { |
| 273 |
let mut builder = s2n_tls::config::Builder::new(); |
| 274 |
builder.with_system_certs(false).unwrap(); |
| 275 |
builder |
| 276 |
.set_security_policy(&Policy::from_version("test_all").unwrap()) |
| 277 |
.unwrap(); |
| 278 |
builder |
| 279 |
} |
| 280 |
|
| 281 |
fn set_chain(&mut self, sig_type: crate::SigType) { |
| 282 |
self.load_pem( |
| 283 |
read_to_bytes(PemType::ClientCertChain, sig_type).as_slice(), |
| 284 |
read_to_bytes(PemType::ClientKey, sig_type).as_slice(), |
| 285 |
) |
| 286 |
.unwrap(); |
| 287 |
} |
| 288 |
|
| 289 |
fn set_trust(&mut self, sig_type: crate::SigType) { |
| 290 |
self.trust_pem(read_to_bytes(PemType::CACert, sig_type).as_slice()) |
| 291 |
.unwrap(); |
| 292 |
self.set_verify_host_callback(HostNameHandler { |
| 293 |
expected_server_name: "localhost", |
| 294 |
}) |
| 295 |
.unwrap(); |
| 296 |
} |
| 297 |
|
| 298 |
fn build(self) -> Self::Config { |
| 299 |
S2NConfig { |
| 300 |
config: self.build().unwrap(), |
| 301 |
ticket_storage: SessionTicketStorage::default(), |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
#[cfg(test)] |
| 307 |
mod tests { |
| 308 |
use super::*; |
| 309 |
use crate::test_utilities; |
| 310 |
use s2n_tls::config; |
| 311 |
|
| 312 |
#[test] |
| 313 |
fn handshake() { |
| 314 |
test_utilities::handshake::<S2NConnection, config::Builder>(); |
| 315 |
} |
| 316 |
|
| 317 |
#[test] |
| 318 |
fn transfer() { |
| 319 |
test_utilities::transfer::<S2NConnection, config::Builder>(); |
| 320 |
} |
| 321 |
} |