| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#![allow(clippy::missing_safety_doc)] |
| 5 |
|
| 6 |
#[cfg(feature = "unstable-cert_authorities")] |
| 7 |
use crate::cert_authorities::CertRequestState; |
| 8 |
#[cfg(feature = "unstable-renegotiate")] |
| 9 |
use crate::renegotiate::RenegotiateState; |
| 10 |
use crate::{ |
| 11 |
callbacks::*, |
| 12 |
cert_chain::{CertificateChain, CertificateChainHandle}, |
| 13 |
config::Config, |
| 14 |
enums::*, |
| 15 |
error::{Error, Fallible, Pollable}, |
| 16 |
psk::Psk, |
| 17 |
security, |
| 18 |
utilities::cstr_to_str, |
| 19 |
}; |
| 20 |
|
| 21 |
use core::{ |
| 22 |
convert::TryInto, |
| 23 |
fmt, |
| 24 |
mem::{self, ManuallyDrop, MaybeUninit}, |
| 25 |
pin::Pin, |
| 26 |
ptr::NonNull, |
| 27 |
task::{Poll, Waker}, |
| 28 |
time::Duration, |
| 29 |
}; |
| 30 |
use libc::c_void; |
| 31 |
use s2n_tls_sys::*; |
| 32 |
use std::{ |
| 33 |
any::{Any, TypeId}, |
| 34 |
collections::HashMap, |
| 35 |
ffi::CStr, |
| 36 |
}; |
| 37 |
|
| 38 |
mod builder; |
| 39 |
pub use builder::*; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
macro_rules! const_str { |
| 49 |
($c_chars:expr) => { |
| 50 |
CStr::from_ptr($c_chars) |
| 51 |
.to_str() |
| 52 |
.map_err(|_| Error::INVALID_INPUT) |
| 53 |
}; |
| 54 |
} |
| 55 |
|
| 56 |
#[non_exhaustive] |
| 57 |
#[derive(Debug, PartialEq)] |
| 58 |
|
| 59 |
|
| 60 |
pub struct KeyUpdateCount { |
| 61 |
pub send_key_updates: u8, |
| 62 |
pub recv_key_updates: u8, |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
pub struct Connection { |
| 67 |
connection: NonNull<s2n_connection>, |
| 68 |
} |
| 69 |
|
| 70 |
impl fmt::Debug for Connection { |
| 71 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 72 |
let mut debug = f.debug_struct("Connection"); |
| 73 |
if let Ok(handshake) = self.handshake_type() { |
| 74 |
debug.field("handshake_type", &handshake); |
| 75 |
} |
| 76 |
if let Ok(cipher) = self.cipher_suite() { |
| 77 |
debug.field("cipher_suite", &cipher); |
| 78 |
} |
| 79 |
if let Ok(version) = self.actual_protocol_version() { |
| 80 |
debug.field("actual_protocol_version", &version); |
| 81 |
} |
| 82 |
if let Some(group_name) = self.selected_key_exchange_group() { |
| 83 |
debug.field("selected_key_exchange_group", &group_name); |
| 84 |
} |
| 85 |
debug.finish_non_exhaustive() |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
unsafe impl Send for Connection {} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
unsafe impl Sync for Connection {} |
| 108 |
|
| 109 |
impl Connection { |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
pub fn new(mode: Mode) -> Self { |
| 122 |
crate::init::init(); |
| 123 |
|
| 124 |
let connection = unsafe { s2n_connection_new(mode.into()).into_result() }.unwrap(); |
| 125 |
|
| 126 |
unsafe { |
| 127 |
debug_assert! { |
| 128 |
s2n_connection_get_config(connection.as_ptr(), &mut core::ptr::null_mut()) |
| 129 |
.into_result() |
| 130 |
.is_err() |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
let mut connection = Self { connection }; |
| 135 |
connection.init_context(); |
| 136 |
connection |
| 137 |
} |
| 138 |
|
| 139 |
fn init_context(&mut self) { |
| 140 |
let context = Box::new(Context::new()); |
| 141 |
let context = Box::into_raw(context) as *mut c_void; |
| 142 |
|
| 143 |
unsafe { |
| 144 |
|
| 145 |
debug_assert!(s2n_connection_get_ctx(self.connection.as_ptr()) |
| 146 |
.into_result() |
| 147 |
.is_err()); |
| 148 |
|
| 149 |
s2n_connection_set_ctx(self.connection.as_ptr(), context) |
| 150 |
.into_result() |
| 151 |
.unwrap(); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
pub fn new_client() -> Self { |
| 156 |
Self::new(Mode::Client) |
| 157 |
} |
| 158 |
|
| 159 |
pub fn new_server() -> Self { |
| 160 |
Self::new(Mode::Server) |
| 161 |
} |
| 162 |
|
| 163 |
pub(crate) fn as_ptr(&mut self) -> *mut s2n_connection { |
| 164 |
self.connection.as_ptr() |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
#[cfg(s2n_tls_external_build)] |
| 175 |
pub fn unstable_as_ptr(&mut self) -> *mut s2n_connection { |
| 176 |
self.as_ptr() |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
pub(crate) unsafe fn from_raw(connection: NonNull<s2n_connection>) -> Self { |
| 183 |
Self { connection } |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
pub fn mode(&self) -> Mode { |
| 190 |
let mode = unsafe { s2n_connection_get_mode(self.connection.as_ptr()) }; |
| 191 |
mode.try_into().unwrap() |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
pub fn set_blinding(&mut self, blinding: Blinding) -> Result<&mut Self, Error> { |
| 200 |
unsafe { |
| 201 |
s2n_connection_set_blinding(self.connection.as_ptr(), blinding.into()).into_result() |
| 202 |
}?; |
| 203 |
Ok(self) |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
pub fn remaining_blinding_delay(&self) -> Result<Duration, Error> { |
| 215 |
let nanos = unsafe { s2n_connection_get_delay(self.connection.as_ptr()).into_result() }?; |
| 216 |
Ok(Duration::from_nanos(nanos)) |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
pub fn set_client_auth_type( |
| 227 |
&mut self, |
| 228 |
client_auth_type: ClientAuthType, |
| 229 |
) -> Result<&mut Self, Error> { |
| 230 |
unsafe { |
| 231 |
s2n_connection_set_client_auth_type(self.connection.as_ptr(), client_auth_type.into()) |
| 232 |
.into_result() |
| 233 |
}?; |
| 234 |
Ok(self) |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
unsafe fn drop_config(&mut self) -> Result<(), Error> { |
| 244 |
let mut prev_config = core::ptr::null_mut(); |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
if s2n_connection_get_config(self.connection.as_ptr(), &mut prev_config) |
| 249 |
.into_result() |
| 250 |
.is_ok() |
| 251 |
{ |
| 252 |
let prev_config = NonNull::new(prev_config).expect( |
| 253 |
"config should exist since the call to s2n_connection_get_config was successful", |
| 254 |
); |
| 255 |
drop(Config::from_raw(prev_config)); |
| 256 |
} |
| 257 |
|
| 258 |
Ok(()) |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
pub fn set_config(&mut self, mut config: Config) -> Result<&mut Self, Error> { |
| 265 |
unsafe { |
| 266 |
|
| 267 |
self.drop_config()?; |
| 268 |
|
| 269 |
s2n_connection_set_config(self.connection.as_ptr(), config.as_mut_ptr()) |
| 270 |
.into_result()?; |
| 271 |
|
| 272 |
debug_assert! { |
| 273 |
s2n_connection_get_config(self.connection.as_ptr(), &mut core::ptr::null_mut()).into_result().is_ok(), |
| 274 |
"s2n_connection_set_config was successful" |
| 275 |
}; |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
mem::forget(config); |
| 280 |
} |
| 281 |
|
| 282 |
Ok(self) |
| 283 |
} |
| 284 |
|
| 285 |
pub(crate) fn config(&self) -> Option<Config> { |
| 286 |
let mut raw = core::ptr::null_mut(); |
| 287 |
let config = unsafe { |
| 288 |
s2n_connection_get_config(self.connection.as_ptr(), &mut raw) |
| 289 |
.into_result() |
| 290 |
.ok()?; |
| 291 |
let raw = NonNull::new(raw)?; |
| 292 |
Config::from_raw(raw) |
| 293 |
}; |
| 294 |
|
| 295 |
|
| 296 |
let _ = ManuallyDrop::new(config.clone()); |
| 297 |
Some(config) |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
pub fn set_security_policy(&mut self, policy: &security::Policy) -> Result<&mut Self, Error> { |
| 302 |
unsafe { |
| 303 |
s2n_connection_set_cipher_preferences( |
| 304 |
self.connection.as_ptr(), |
| 305 |
policy.as_cstr().as_ptr(), |
| 306 |
) |
| 307 |
.into_result() |
| 308 |
}?; |
| 309 |
Ok(self) |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
pub fn set_dynamic_record_threshold( |
| 320 |
&mut self, |
| 321 |
resize_threshold: u32, |
| 322 |
timeout_threshold: u16, |
| 323 |
) -> Result<&mut Self, Error> { |
| 324 |
unsafe { |
| 325 |
s2n_connection_set_dynamic_record_threshold( |
| 326 |
self.connection.as_ptr(), |
| 327 |
resize_threshold, |
| 328 |
timeout_threshold, |
| 329 |
) |
| 330 |
.into_result() |
| 331 |
}?; |
| 332 |
Ok(self) |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
pub fn request_key_update(&mut self, peer_request: PeerKeyUpdate) -> Result<&mut Self, Error> { |
| 349 |
unsafe { |
| 350 |
s2n_connection_request_key_update(self.connection.as_ptr(), peer_request.into()) |
| 351 |
.into_result() |
| 352 |
}?; |
| 353 |
Ok(self) |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
#[cfg(feature = "unstable-ktls")] |
| 362 |
pub fn key_update_counts(&self) -> Result<KeyUpdateCount, Error> { |
| 363 |
let mut send_key_updates = 0; |
| 364 |
let mut recv_key_updates = 0; |
| 365 |
unsafe { |
| 366 |
s2n_connection_get_key_update_counts( |
| 367 |
self.connection.as_ptr(), |
| 368 |
&mut send_key_updates, |
| 369 |
&mut recv_key_updates, |
| 370 |
) |
| 371 |
.into_result()?; |
| 372 |
} |
| 373 |
Ok(KeyUpdateCount { |
| 374 |
send_key_updates, |
| 375 |
recv_key_updates, |
| 376 |
}) |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
pub fn set_application_protocol_preference<P: IntoIterator<Item = I>, I: AsRef<[u8]>>( |
| 389 |
&mut self, |
| 390 |
protocols: P, |
| 391 |
) -> Result<&mut Self, Error> { |
| 392 |
|
| 393 |
unsafe { |
| 394 |
s2n_connection_set_protocol_preferences(self.connection.as_ptr(), core::ptr::null(), 0) |
| 395 |
.into_result() |
| 396 |
}?; |
| 397 |
|
| 398 |
for protocol in protocols { |
| 399 |
self.append_application_protocol_preference(protocol.as_ref())?; |
| 400 |
} |
| 401 |
|
| 402 |
Ok(self) |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
pub fn append_application_protocol_preference( |
| 407 |
&mut self, |
| 408 |
protocol: &[u8], |
| 409 |
) -> Result<&mut Self, Error> { |
| 410 |
unsafe { |
| 411 |
s2n_connection_append_protocol_preference( |
| 412 |
self.connection.as_ptr(), |
| 413 |
protocol.as_ptr(), |
| 414 |
protocol |
| 415 |
.len() |
| 416 |
.try_into() |
| 417 |
.map_err(|_| Error::INVALID_INPUT)?, |
| 418 |
) |
| 419 |
.into_result() |
| 420 |
}?; |
| 421 |
Ok(self) |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
pub fn set_receive_callback(&mut self, callback: s2n_recv_fn) -> Result<&mut Self, Error> { |
| 428 |
unsafe { s2n_connection_set_recv_cb(self.connection.as_ptr(), callback).into_result() }?; |
| 429 |
Ok(self) |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
pub unsafe fn set_receive_context(&mut self, context: *mut c_void) -> Result<&mut Self, Error> { |
| 438 |
s2n_connection_set_recv_ctx(self.connection.as_ptr(), context).into_result()?; |
| 439 |
Ok(self) |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
pub fn set_send_callback(&mut self, callback: s2n_send_fn) -> Result<&mut Self, Error> { |
| 446 |
unsafe { s2n_connection_set_send_cb(self.connection.as_ptr(), callback).into_result() }?; |
| 447 |
Ok(self) |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
pub unsafe fn set_send_context(&mut self, context: *mut c_void) -> Result<&mut Self, Error> { |
| 456 |
s2n_connection_set_send_ctx(self.connection.as_ptr(), context).into_result()?; |
| 457 |
Ok(self) |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
pub fn set_verify_host_callback<T: 'static + VerifyHostNameCallback>( |
| 468 |
&mut self, |
| 469 |
handler: T, |
| 470 |
) -> Result<&mut Self, Error> { |
| 471 |
unsafe extern "C" fn verify_host_cb_fn( |
| 472 |
host_name: *const ::libc::c_char, |
| 473 |
host_name_len: usize, |
| 474 |
context: *mut ::libc::c_void, |
| 475 |
) -> u8 { |
| 476 |
let context = &mut *(context as *mut Context); |
| 477 |
let handler = context.verify_host_callback.as_mut().unwrap(); |
| 478 |
verify_host(host_name, host_name_len, handler) |
| 479 |
} |
| 480 |
|
| 481 |
self.context_mut().verify_host_callback = Some(Box::new(handler)); |
| 482 |
unsafe { |
| 483 |
s2n_connection_set_verify_host_callback( |
| 484 |
self.connection.as_ptr(), |
| 485 |
Some(verify_host_cb_fn), |
| 486 |
self.context_mut() as *mut Context as *mut c_void, |
| 487 |
) |
| 488 |
.into_result() |
| 489 |
}?; |
| 490 |
Ok(self) |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
pub fn prefer_low_latency(&mut self) -> Result<&mut Self, Error> { |
| 498 |
unsafe { s2n_connection_prefer_low_latency(self.connection.as_ptr()).into_result() }?; |
| 499 |
Ok(self) |
| 500 |
} |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
pub fn prefer_throughput(&mut self) -> Result<&mut Self, Error> { |
| 506 |
unsafe { s2n_connection_prefer_throughput(self.connection.as_ptr()).into_result() }?; |
| 507 |
Ok(self) |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
pub fn set_receive_buffering(&mut self, enabled: bool) -> Result<&mut Self, Error> { |
| 514 |
unsafe { |
| 515 |
s2n_connection_set_recv_buffering(self.connection.as_ptr(), enabled).into_result() |
| 516 |
}?; |
| 517 |
Ok(self) |
| 518 |
} |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
pub fn release_buffers(&mut self) -> Result<&mut Self, Error> { |
| 527 |
unsafe { s2n_connection_release_buffers(self.connection.as_ptr()).into_result() }?; |
| 528 |
Ok(self) |
| 529 |
} |
| 530 |
|
| 531 |
|
| 532 |
pub fn use_corked_io(&mut self) -> Result<&mut Self, Error> { |
| 533 |
unsafe { s2n_connection_use_corked_io(self.connection.as_ptr()).into_result() }?; |
| 534 |
Ok(self) |
| 535 |
} |
| 536 |
|
| 537 |
pub(crate) fn wipe_method<F, T>(&mut self, wipe: F) -> Result<(), Error> |
| 538 |
where |
| 539 |
F: FnOnce(&mut Self) -> Result<T, Error>, |
| 540 |
{ |
| 541 |
|
| 542 |
|
| 543 |
unsafe { self.drop_context()? }; |
| 544 |
|
| 545 |
let result = wipe(self); |
| 546 |
|
| 547 |
|
| 548 |
self.init_context(); |
| 549 |
result?; |
| 550 |
|
| 551 |
Ok(()) |
| 552 |
} |
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
pub fn wipe(&mut self) -> Result<&mut Self, Error> { |
| 563 |
self.wipe_method(|conn| unsafe { s2n_connection_wipe(conn.as_ptr()).into_result() })?; |
| 564 |
Ok(self) |
| 565 |
} |
| 566 |
|
| 567 |
fn trigger_initializer(&mut self) { |
| 568 |
if !core::mem::replace(&mut self.context_mut().connection_initialized, true) { |
| 569 |
if let Some(config) = self.config() { |
| 570 |
if let Some(callback) = config.context().connection_initializer.as_ref() { |
| 571 |
let future = callback.initialize_connection(self); |
| 572 |
AsyncCallback::trigger(future, self); |
| 573 |
} |
| 574 |
} |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
fn poll_async_task(&mut self) -> Option<Poll<Result<(), Error>>> { |
| 582 |
self.take_async_callback().map(|mut callback| { |
| 583 |
let waker = self.waker().ok_or(Error::MISSING_WAKER)?.clone(); |
| 584 |
let mut ctx = core::task::Context::from_waker(&waker); |
| 585 |
match Pin::new(&mut callback).poll(self, &mut ctx) { |
| 586 |
Poll::Ready(result) => Poll::Ready(result), |
| 587 |
Poll::Pending => { |
| 588 |
|
| 589 |
self.set_async_callback(callback); |
| 590 |
Poll::Pending |
| 591 |
} |
| 592 |
} |
| 593 |
}) |
| 594 |
} |
| 595 |
|
| 596 |
pub(crate) fn poll_negotiate_method<F, T>( |
| 597 |
&mut self, |
| 598 |
mut negotiate: F, |
| 599 |
) -> Poll<Result<(), Error>> |
| 600 |
where |
| 601 |
F: FnMut(&mut Connection) -> Poll<Result<T, Error>>, |
| 602 |
{ |
| 603 |
self.trigger_initializer(); |
| 604 |
|
| 605 |
loop { |
| 606 |
|
| 607 |
match self.poll_async_task().unwrap_or(Poll::Ready(Ok(()))) { |
| 608 |
Poll::Ready(Err(err)) => return Poll::Ready(Err(err)), |
| 609 |
Poll::Pending => return Poll::Pending, |
| 610 |
Poll::Ready(Ok(_)) => {} |
| 611 |
}; |
| 612 |
|
| 613 |
match negotiate(self) { |
| 614 |
Poll::Ready(res) => return Poll::Ready(res.map(|_| ())), |
| 615 |
Poll::Pending => { |
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
if self.context_mut().async_callback.is_some() { |
| 622 |
|
| 623 |
continue; |
| 624 |
} |
| 625 |
|
| 626 |
|
| 627 |
return Poll::Pending; |
| 628 |
} |
| 629 |
} |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
pub fn poll_negotiate(&mut self) -> Poll<Result<&mut Self, Error>> { |
| 645 |
let mut blocked = s2n_blocked_status::NOT_BLOCKED; |
| 646 |
self.poll_negotiate_method(|conn| unsafe { |
| 647 |
s2n_negotiate(conn.as_ptr(), &mut blocked).into_poll() |
| 648 |
}) |
| 649 |
.map_ok(|_| self) |
| 650 |
} |
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
#[cfg(not(feature = "unstable-renegotiate"))] |
| 659 |
pub fn poll_send(&mut self, buf: &[u8]) -> Poll<Result<usize, Error>> { |
| 660 |
let mut blocked = s2n_blocked_status::NOT_BLOCKED; |
| 661 |
let buf_len: isize = buf.len().try_into().map_err(|_| Error::INVALID_INPUT)?; |
| 662 |
let buf_ptr = buf.as_ptr() as *const ::libc::c_void; |
| 663 |
unsafe { s2n_send(self.connection.as_ptr(), buf_ptr, buf_len, &mut blocked).into_poll() } |
| 664 |
} |
| 665 |
|
| 666 |
#[cfg(not(feature = "unstable-renegotiate"))] |
| 667 |
pub(crate) fn poll_recv_raw( |
| 668 |
&mut self, |
| 669 |
buf_ptr: *mut ::libc::c_void, |
| 670 |
buf_len: isize, |
| 671 |
) -> Poll<Result<usize, Error>> { |
| 672 |
let mut blocked = s2n_blocked_status::NOT_BLOCKED; |
| 673 |
unsafe { s2n_recv(self.connection.as_ptr(), buf_ptr, buf_len, &mut blocked).into_poll() } |
| 674 |
} |
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
pub fn poll_recv(&mut self, buf: &mut [u8]) -> Poll<Result<usize, Error>> { |
| 684 |
let buf_len: isize = buf.len().try_into().map_err(|_| Error::INVALID_INPUT)?; |
| 685 |
let buf_ptr = buf.as_ptr() as *mut ::libc::c_void; |
| 686 |
self.poll_recv_raw(buf_ptr, buf_len) |
| 687 |
} |
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
pub fn poll_recv_uninitialized( |
| 703 |
&mut self, |
| 704 |
buf: &mut [MaybeUninit<u8>], |
| 705 |
) -> Poll<Result<usize, Error>> { |
| 706 |
let buf_len: isize = buf.len().try_into().map_err(|_| Error::INVALID_INPUT)?; |
| 707 |
let buf_ptr = buf.as_ptr() as *mut ::libc::c_void; |
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
self.poll_recv_raw(buf_ptr, buf_len) |
| 715 |
} |
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
pub fn poll_flush(&mut self) -> Poll<Result<&mut Self, Error>> { |
| 726 |
let mut blocked = s2n_blocked_status::NOT_BLOCKED; |
| 727 |
unsafe { |
| 728 |
s2n_flush(self.connection.as_ptr(), &mut blocked) |
| 729 |
.into_poll() |
| 730 |
.map_ok(|_| self) |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
pub fn peek_len(&self) -> usize { |
| 738 |
unsafe { s2n_peek(self.connection.as_ptr()) as usize } |
| 739 |
} |
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
pub fn peek_buffered_len(&self) -> usize { |
| 752 |
unsafe { s2n_peek_buffered(self.connection.as_ptr()) as usize } |
| 753 |
} |
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
pub fn poll_shutdown(&mut self) -> Poll<Result<&mut Self, Error>> { |
| 763 |
if !self.remaining_blinding_delay()?.is_zero() { |
| 764 |
return Poll::Pending; |
| 765 |
} |
| 766 |
let mut blocked = s2n_blocked_status::NOT_BLOCKED; |
| 767 |
unsafe { |
| 768 |
s2n_shutdown(self.connection.as_ptr(), &mut blocked) |
| 769 |
.into_poll() |
| 770 |
.map_ok(|_| self) |
| 771 |
} |
| 772 |
} |
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
pub fn poll_shutdown_send(&mut self) -> Poll<Result<&mut Self, Error>> { |
| 781 |
if !self.remaining_blinding_delay()?.is_zero() { |
| 782 |
return Poll::Pending; |
| 783 |
} |
| 784 |
let mut blocked = s2n_blocked_status::NOT_BLOCKED; |
| 785 |
unsafe { |
| 786 |
s2n_shutdown_send(self.connection.as_ptr(), &mut blocked) |
| 787 |
.into_poll() |
| 788 |
.map_ok(|_| self) |
| 789 |
} |
| 790 |
} |
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
pub fn alert(&self) -> Option<u8> { |
| 796 |
let alert = |
| 797 |
unsafe { s2n_connection_get_alert(self.connection.as_ptr()).into_result() }.ok()?; |
| 798 |
Some(alert as u8) |
| 799 |
} |
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
pub fn set_server_name(&mut self, server_name: &str) -> Result<&mut Self, Error> { |
| 805 |
let server_name = std::ffi::CString::new(server_name).map_err(|_| Error::INVALID_INPUT)?; |
| 806 |
unsafe { |
| 807 |
s2n_set_server_name(self.connection.as_ptr(), server_name.as_ptr()).into_result() |
| 808 |
}?; |
| 809 |
Ok(self) |
| 810 |
} |
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
pub fn server_name(&self) -> Option<&str> { |
| 816 |
unsafe { |
| 817 |
let server_name = s2n_get_server_name(self.connection.as_ptr()); |
| 818 |
match server_name.into_result() { |
| 819 |
Ok(server_name) => CStr::from_ptr(server_name).to_str().ok(), |
| 820 |
Err(_) => None, |
| 821 |
} |
| 822 |
} |
| 823 |
} |
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
pub fn set_session_ticket(&mut self, session: &[u8]) -> Result<&mut Self, Error> { |
| 829 |
unsafe { |
| 830 |
s2n_connection_set_session(self.connection.as_ptr(), session.as_ptr(), session.len()) |
| 831 |
.into_result() |
| 832 |
}?; |
| 833 |
Ok(self) |
| 834 |
} |
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
pub fn session_ticket_length(&self) -> Result<usize, Error> { |
| 840 |
let len = |
| 841 |
unsafe { s2n_connection_get_session_length(self.connection.as_ptr()).into_result()? }; |
| 842 |
Ok(len.try_into().unwrap()) |
| 843 |
} |
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
pub fn session_ticket(&self, output: &mut [u8]) -> Result<usize, Error> { |
| 857 |
if output.len() < self.session_ticket_length()? { |
| 858 |
return Err(Error::INVALID_INPUT); |
| 859 |
} |
| 860 |
let written = unsafe { |
| 861 |
s2n_connection_get_session(self.connection.as_ptr(), output.as_mut_ptr(), output.len()) |
| 862 |
.into_result()? |
| 863 |
}; |
| 864 |
Ok(written.try_into().unwrap()) |
| 865 |
} |
| 866 |
|
| 867 |
|
| 868 |
pub fn set_waker(&mut self, waker: Option<&Waker>) -> Result<&mut Self, Error> { |
| 869 |
let ctx = self.context_mut(); |
| 870 |
|
| 871 |
if let Some(waker) = waker { |
| 872 |
if let Some(prev_waker) = ctx.waker.as_mut() { |
| 873 |
|
| 874 |
if !prev_waker.will_wake(waker) { |
| 875 |
prev_waker.clone_from(waker); |
| 876 |
} |
| 877 |
} else { |
| 878 |
ctx.waker = Some(waker.clone()); |
| 879 |
} |
| 880 |
} else { |
| 881 |
ctx.waker = None; |
| 882 |
} |
| 883 |
Ok(self) |
| 884 |
} |
| 885 |
|
| 886 |
|
| 887 |
pub fn waker(&self) -> Option<&Waker> { |
| 888 |
let ctx = self.context(); |
| 889 |
ctx.waker.as_ref() |
| 890 |
} |
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
fn take_async_callback(&mut self) -> Option<AsyncCallback> { |
| 898 |
let ctx = self.context_mut(); |
| 899 |
ctx.async_callback.take() |
| 900 |
} |
| 901 |
|
| 902 |
|
| 903 |
pub(crate) fn set_async_callback(&mut self, callback: AsyncCallback) { |
| 904 |
let ctx = self.context_mut(); |
| 905 |
debug_assert!(ctx.async_callback.is_none()); |
| 906 |
ctx.async_callback = Some(callback); |
| 907 |
} |
| 908 |
|
| 909 |
|
| 910 |
fn context_mut(&mut self) -> &mut Context { |
| 911 |
unsafe { |
| 912 |
let ctx = s2n_connection_get_ctx(self.connection.as_ptr()) |
| 913 |
.into_result() |
| 914 |
.unwrap(); |
| 915 |
&mut *(ctx.as_ptr() as *mut Context) |
| 916 |
} |
| 917 |
} |
| 918 |
|
| 919 |
|
| 920 |
fn context(&self) -> &Context { |
| 921 |
unsafe { |
| 922 |
let ctx = s2n_connection_get_ctx(self.connection.as_ptr()) |
| 923 |
.into_result() |
| 924 |
.unwrap(); |
| 925 |
&*(ctx.as_ptr() as *mut Context) |
| 926 |
} |
| 927 |
} |
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
unsafe fn drop_context(&mut self) -> Result<(), Error> { |
| 935 |
let ctx = s2n_connection_get_ctx(self.connection.as_ptr()).into_result(); |
| 936 |
if let Ok(ctx) = ctx { |
| 937 |
drop(Box::from_raw(ctx.as_ptr() as *mut Context)); |
| 938 |
} |
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
s2n_connection_set_ctx(self.connection.as_ptr(), core::ptr::null_mut()).into_result()?; |
| 943 |
Ok(()) |
| 944 |
} |
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
pub fn server_name_extension_used(&mut self) { |
| 950 |
|
| 951 |
|
| 952 |
unsafe { |
| 953 |
s2n_connection_server_name_extension_used(self.connection.as_ptr()) |
| 954 |
.into_result() |
| 955 |
.unwrap(); |
| 956 |
} |
| 957 |
} |
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
pub fn client_cert_used(&self) -> bool { |
| 965 |
unsafe { s2n_connection_client_cert_used(self.connection.as_ptr()) == 1 } |
| 966 |
} |
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
pub fn client_cert_chain_bytes(&self) -> Result<Option<&[u8]>, Error> { |
| 972 |
if !self.client_cert_used() { |
| 973 |
return Ok(None); |
| 974 |
} |
| 975 |
|
| 976 |
let mut chain = std::ptr::null_mut(); |
| 977 |
let mut len = 0; |
| 978 |
unsafe { |
| 979 |
s2n_connection_get_client_cert_chain(self.connection.as_ptr(), &mut chain, &mut len) |
| 980 |
.into_result()?; |
| 981 |
} |
| 982 |
|
| 983 |
if chain.is_null() || len == 0 { |
| 984 |
return Ok(None); |
| 985 |
} |
| 986 |
|
| 987 |
unsafe { Ok(Some(std::slice::from_raw_parts(chain, len as usize))) } |
| 988 |
} |
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
pub fn client_hello(&self) -> Result<&crate::client_hello::ClientHello, Error> { |
| 1022 |
let mut handle = |
| 1023 |
unsafe { s2n_connection_get_client_hello(self.connection.as_ptr()).into_result()? }; |
| 1024 |
Ok(crate::client_hello::ClientHello::from_ptr(unsafe { |
| 1025 |
handle.as_mut() |
| 1026 |
})) |
| 1027 |
} |
| 1028 |
|
| 1029 |
|
| 1030 |
pub(crate) fn mark_client_hello_cb_done(&mut self) -> Result<(), Error> { |
| 1031 |
unsafe { |
| 1032 |
s2n_client_hello_cb_done(self.connection.as_ptr()).into_result()?; |
| 1033 |
} |
| 1034 |
Ok(()) |
| 1035 |
} |
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
pub fn actual_protocol_version(&self) -> Result<Version, Error> { |
| 1041 |
let version = unsafe { |
| 1042 |
s2n_connection_get_actual_protocol_version(self.connection.as_ptr()).into_result()? |
| 1043 |
}; |
| 1044 |
version.try_into() |
| 1045 |
} |
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
pub fn client_hello_is_sslv2(&self) -> Result<bool, Error> { |
| 1057 |
let version = unsafe { |
| 1058 |
s2n_connection_get_client_hello_version(self.connection.as_ptr()).into_result()? |
| 1059 |
}; |
| 1060 |
let version: Version = version.try_into()?; |
| 1061 |
Ok(version == Version::SSLV2) |
| 1062 |
} |
| 1063 |
|
| 1064 |
|
| 1065 |
pub fn handshake_type(&self) -> Result<&str, Error> { |
| 1066 |
let handshake = unsafe { |
| 1067 |
s2n_connection_get_handshake_type_name(self.connection.as_ptr()).into_result()? |
| 1068 |
}; |
| 1069 |
unsafe { |
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
const_str!(handshake) |
| 1075 |
} |
| 1076 |
} |
| 1077 |
|
| 1078 |
|
| 1079 |
pub fn cipher_suite(&self) -> Result<&str, Error> { |
| 1080 |
let cipher = unsafe { s2n_connection_get_cipher(self.connection.as_ptr()).into_result()? }; |
| 1081 |
unsafe { |
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
const_str!(cipher) |
| 1087 |
} |
| 1088 |
} |
| 1089 |
|
| 1090 |
|
| 1091 |
#[deprecated = "PQ TLS 1.2 KEM Names are no longer supported. Use kem_group_name() to retrieve PQ TLS 1.3 Group name."] |
| 1092 |
pub fn kem_name(&self) -> Option<&str> { |
| 1093 |
let name_bytes = { |
| 1094 |
let name = unsafe { s2n_connection_get_kem_name(self.connection.as_ptr()) }; |
| 1095 |
if name.is_null() { |
| 1096 |
return None; |
| 1097 |
} |
| 1098 |
name |
| 1099 |
}; |
| 1100 |
|
| 1101 |
let name_str = unsafe { |
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
const_str!(name_bytes) |
| 1107 |
}; |
| 1108 |
|
| 1109 |
match name_str { |
| 1110 |
Ok("NONE") => None, |
| 1111 |
Ok(name) => Some(name), |
| 1112 |
Err(_) => { |
| 1113 |
|
| 1114 |
|
| 1115 |
None |
| 1116 |
} |
| 1117 |
} |
| 1118 |
} |
| 1119 |
|
| 1120 |
|
| 1121 |
pub fn kem_group_name(&self) -> Option<&str> { |
| 1122 |
let name_bytes = { |
| 1123 |
let name = unsafe { s2n_connection_get_kem_group_name(self.connection.as_ptr()) }; |
| 1124 |
if name.is_null() { |
| 1125 |
return None; |
| 1126 |
} |
| 1127 |
name |
| 1128 |
}; |
| 1129 |
|
| 1130 |
let name_str = unsafe { |
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
const_str!(name_bytes) |
| 1136 |
}; |
| 1137 |
|
| 1138 |
match name_str { |
| 1139 |
Ok("NONE") => None, |
| 1140 |
Ok(name) => Some(name), |
| 1141 |
Err(_) => { |
| 1142 |
|
| 1143 |
|
| 1144 |
None |
| 1145 |
} |
| 1146 |
} |
| 1147 |
} |
| 1148 |
|
| 1149 |
|
| 1150 |
#[deprecated = "Use selected_key_exchange_group instead"] |
| 1151 |
pub fn selected_curve(&self) -> Result<&str, Error> { |
| 1152 |
let curve = unsafe { s2n_connection_get_curve(self.connection.as_ptr()).into_result()? }; |
| 1153 |
unsafe { |
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
const_str!(curve) |
| 1159 |
} |
| 1160 |
} |
| 1161 |
|
| 1162 |
|
| 1163 |
pub fn selected_key_exchange_group(&self) -> Option<&str> { |
| 1164 |
let mut group_name = core::ptr::null(); |
| 1165 |
unsafe { |
| 1166 |
s2n_connection_get_key_exchange_group(self.connection.as_ptr(), &mut group_name) |
| 1167 |
.into_result() |
| 1168 |
.ok() |
| 1169 |
}?; |
| 1170 |
|
| 1171 |
unsafe { |
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
const_str!(group_name).ok() |
| 1178 |
} |
| 1179 |
} |
| 1180 |
|
| 1181 |
|
| 1182 |
pub fn selected_signature_algorithm(&self) -> Result<SignatureAlgorithm, Error> { |
| 1183 |
let mut sig_alg = s2n_tls_signature_algorithm::ANONYMOUS; |
| 1184 |
unsafe { |
| 1185 |
s2n_connection_get_selected_signature_algorithm(self.connection.as_ptr(), &mut sig_alg) |
| 1186 |
.into_result()?; |
| 1187 |
} |
| 1188 |
sig_alg.try_into() |
| 1189 |
} |
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
pub fn signature_scheme(&self) -> Option<&'static str> { |
| 1200 |
let mut sig_alg: *const std::ffi::c_char = std::ptr::null(); |
| 1201 |
unsafe { |
| 1202 |
s2n_connection_get_signature_scheme(self.connection.as_ptr(), &mut sig_alg) |
| 1203 |
.into_result() |
| 1204 |
.ok()?; |
| 1205 |
} |
| 1206 |
let result = unsafe { |
| 1207 |
|
| 1208 |
|
| 1209 |
cstr_to_str(sig_alg) |
| 1210 |
}; |
| 1211 |
if result == "none" { |
| 1212 |
None |
| 1213 |
} else { |
| 1214 |
Some(result) |
| 1215 |
} |
| 1216 |
} |
| 1217 |
|
| 1218 |
|
| 1219 |
pub fn selected_hash_algorithm(&self) -> Result<HashAlgorithm, Error> { |
| 1220 |
let mut hash_alg = s2n_tls_hash_algorithm::NONE; |
| 1221 |
unsafe { |
| 1222 |
s2n_connection_get_selected_digest_algorithm(self.connection.as_ptr(), &mut hash_alg) |
| 1223 |
.into_result()?; |
| 1224 |
} |
| 1225 |
hash_alg.try_into() |
| 1226 |
} |
| 1227 |
|
| 1228 |
|
| 1229 |
pub fn certificate_match(&self) -> Result<CertSNIMatch, Error> { |
| 1230 |
let mut cert_match = s2n_cert_sni_match::SNI_NO_MATCH; |
| 1231 |
unsafe { |
| 1232 |
s2n_connection_get_certificate_match(self.connection.as_ptr(), &mut cert_match) |
| 1233 |
.into_result()?; |
| 1234 |
} |
| 1235 |
cert_match.try_into() |
| 1236 |
} |
| 1237 |
|
| 1238 |
|
| 1239 |
pub fn selected_client_signature_algorithm(&self) -> Result<Option<SignatureAlgorithm>, Error> { |
| 1240 |
let mut sig_alg = s2n_tls_signature_algorithm::ANONYMOUS; |
| 1241 |
unsafe { |
| 1242 |
s2n_connection_get_selected_client_cert_signature_algorithm( |
| 1243 |
self.connection.as_ptr(), |
| 1244 |
&mut sig_alg, |
| 1245 |
) |
| 1246 |
.into_result()?; |
| 1247 |
} |
| 1248 |
Ok(match sig_alg { |
| 1249 |
s2n_tls_signature_algorithm::ANONYMOUS => None, |
| 1250 |
sig_alg => Some(sig_alg.try_into()?), |
| 1251 |
}) |
| 1252 |
} |
| 1253 |
|
| 1254 |
|
| 1255 |
pub fn selected_client_hash_algorithm(&self) -> Result<Option<HashAlgorithm>, Error> { |
| 1256 |
let mut hash_alg = s2n_tls_hash_algorithm::NONE; |
| 1257 |
unsafe { |
| 1258 |
s2n_connection_get_selected_client_cert_digest_algorithm( |
| 1259 |
self.connection.as_ptr(), |
| 1260 |
&mut hash_alg, |
| 1261 |
) |
| 1262 |
.into_result()?; |
| 1263 |
} |
| 1264 |
Ok(match hash_alg { |
| 1265 |
s2n_tls_hash_algorithm::NONE => None, |
| 1266 |
hash_alg => Some(hash_alg.try_into()?), |
| 1267 |
}) |
| 1268 |
} |
| 1269 |
|
| 1270 |
|
| 1271 |
pub fn application_protocol(&self) -> Option<&[u8]> { |
| 1272 |
let protocol = unsafe { s2n_get_application_protocol(self.connection.as_ptr()) }; |
| 1273 |
if protocol.is_null() { |
| 1274 |
return None; |
| 1275 |
} |
| 1276 |
Some(unsafe { CStr::from_ptr(protocol).to_bytes() }) |
| 1277 |
} |
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
pub fn tls_exporter( |
| 1287 |
&self, |
| 1288 |
label: &[u8], |
| 1289 |
context: &[u8], |
| 1290 |
output: &mut [u8], |
| 1291 |
) -> Result<(), Error> { |
| 1292 |
unsafe { |
| 1293 |
s2n_connection_tls_exporter( |
| 1294 |
self.connection.as_ptr(), |
| 1295 |
label.as_ptr(), |
| 1296 |
label.len().try_into().map_err(|_| Error::INVALID_INPUT)?, |
| 1297 |
context.as_ptr(), |
| 1298 |
context.len().try_into().map_err(|_| Error::INVALID_INPUT)?, |
| 1299 |
output.as_mut_ptr(), |
| 1300 |
output.len().try_into().map_err(|_| Error::INVALID_INPUT)?, |
| 1301 |
) |
| 1302 |
.into_result() |
| 1303 |
.map(|_| ()) |
| 1304 |
} |
| 1305 |
} |
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
pub fn peer_cert_chain(&self) -> Result<CertificateChain<'static>, Error> { |
| 1313 |
unsafe { |
| 1314 |
let chain_handle = CertificateChainHandle::allocate()?; |
| 1315 |
s2n_connection_get_peer_cert_chain( |
| 1316 |
self.connection.as_ptr(), |
| 1317 |
chain_handle.cert.as_ptr(), |
| 1318 |
) |
| 1319 |
.into_result() |
| 1320 |
.map(|_| ())?; |
| 1321 |
Ok(CertificateChain::from_allocated(chain_handle)) |
| 1322 |
} |
| 1323 |
} |
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
pub fn selected_cert(&self) -> Option<CertificateChain<'_>> { |
| 1335 |
unsafe { |
| 1336 |
|
| 1337 |
|
| 1338 |
#[allow(clippy::manual_map)] |
| 1339 |
if let Some(ptr) = |
| 1340 |
NonNull::new(s2n_connection_get_selected_cert(self.connection.as_ptr())) |
| 1341 |
{ |
| 1342 |
Some(CertificateChain::from_ptr_reference(ptr)) |
| 1343 |
} else { |
| 1344 |
None |
| 1345 |
} |
| 1346 |
} |
| 1347 |
} |
| 1348 |
|
| 1349 |
|
| 1350 |
pub fn master_secret(&self) -> Result<Vec<u8>, Error> { |
| 1351 |
|
| 1352 |
let mut secret = vec![0; 48]; |
| 1353 |
unsafe { |
| 1354 |
s2n_connection_get_master_secret( |
| 1355 |
self.connection.as_ptr(), |
| 1356 |
secret.as_mut_ptr(), |
| 1357 |
secret.len(), |
| 1358 |
) |
| 1359 |
.into_result()?; |
| 1360 |
} |
| 1361 |
Ok(secret) |
| 1362 |
} |
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
pub fn serialization_length(&self) -> Result<usize, Error> { |
| 1368 |
unsafe { |
| 1369 |
let mut length = 0; |
| 1370 |
s2n_connection_serialization_length(self.connection.as_ptr(), &mut length) |
| 1371 |
.into_result()?; |
| 1372 |
Ok(length.try_into().unwrap()) |
| 1373 |
} |
| 1374 |
} |
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
pub fn serialize(&self, output: &mut [u8]) -> Result<(), Error> { |
| 1380 |
unsafe { |
| 1381 |
s2n_connection_serialize( |
| 1382 |
self.connection.as_ptr(), |
| 1383 |
output.as_mut_ptr(), |
| 1384 |
output.len().try_into().map_err(|_| Error::INVALID_INPUT)?, |
| 1385 |
) |
| 1386 |
.into_result()?; |
| 1387 |
Ok(()) |
| 1388 |
} |
| 1389 |
} |
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
pub fn deserialize(&mut self, input: &[u8]) -> Result<(), Error> { |
| 1396 |
let size = input.len(); |
| 1397 |
|
| 1398 |
|
| 1399 |
let input = input.as_ptr() as *mut u8; |
| 1400 |
unsafe { |
| 1401 |
s2n_connection_deserialize( |
| 1402 |
self.as_ptr(), |
| 1403 |
input, |
| 1404 |
size.try_into().map_err(|_| Error::INVALID_INPUT)?, |
| 1405 |
) |
| 1406 |
.into_result()?; |
| 1407 |
Ok(()) |
| 1408 |
} |
| 1409 |
} |
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
pub fn resumed(&self) -> bool { |
| 1415 |
unsafe { s2n_connection_is_session_resumed(self.connection.as_ptr()) == 1 } |
| 1416 |
} |
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
|
| 1423 |
pub fn append_psk(&mut self, psk: &Psk) -> Result<(), Error> { |
| 1424 |
unsafe { |
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
s2n_connection_append_psk(self.as_ptr(), psk.ptr.as_ptr()).into_result()? |
| 1429 |
}; |
| 1430 |
Ok(()) |
| 1431 |
} |
| 1432 |
|
| 1433 |
|
| 1434 |
pub fn negotiated_psk_identity_length(&self) -> Result<usize, Error> { |
| 1435 |
let mut length = 0; |
| 1436 |
unsafe { |
| 1437 |
s2n_connection_get_negotiated_psk_identity_length(self.connection.as_ptr(), &mut length) |
| 1438 |
.into_result()? |
| 1439 |
}; |
| 1440 |
Ok(length as usize) |
| 1441 |
} |
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
pub fn negotiated_psk_identity(&self, destination: &mut [u8]) -> Result<(), Error> { |
| 1448 |
unsafe { |
| 1449 |
s2n_connection_get_negotiated_psk_identity( |
| 1450 |
self.connection.as_ptr(), |
| 1451 |
destination.as_mut_ptr(), |
| 1452 |
destination.len().min(u16::MAX as usize) as u16, |
| 1453 |
) |
| 1454 |
.into_result()?; |
| 1455 |
} |
| 1456 |
Ok(()) |
| 1457 |
} |
| 1458 |
|
| 1459 |
|
| 1460 |
|
| 1461 |
|
| 1462 |
|
| 1463 |
|
| 1464 |
|
| 1465 |
pub fn set_application_context<T: Send + Sync + 'static>(&mut self, app_context: T) { |
| 1466 |
let context_type_id = TypeId::of::<T>(); |
| 1467 |
self.context_mut() |
| 1468 |
.app_context |
| 1469 |
.insert(context_type_id, Box::new(app_context)); |
| 1470 |
} |
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
pub fn remove_application_context<T: Send + Sync + 'static>( |
| 1477 |
&mut self, |
| 1478 |
) -> Option<Box<dyn Any + Send + Sync>> { |
| 1479 |
let context_type_id = TypeId::of::<T>(); |
| 1480 |
self.context_mut().app_context.remove(&context_type_id) |
| 1481 |
} |
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
|
| 1490 |
|
| 1491 |
pub fn application_context<T: Send + Sync + 'static>(&self) -> Option<&T> { |
| 1492 |
let context_type_id = TypeId::of::<T>(); |
| 1493 |
|
| 1494 |
|
| 1495 |
|
| 1496 |
self.context() |
| 1497 |
.app_context |
| 1498 |
.get(&context_type_id) |
| 1499 |
.and_then(|app_context| app_context.downcast_ref::<T>()) |
| 1500 |
} |
| 1501 |
|
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
|
| 1506 |
|
| 1507 |
|
| 1508 |
|
| 1509 |
|
| 1510 |
pub fn application_context_mut<T: Send + Sync + 'static>(&mut self) -> Option<&mut T> { |
| 1511 |
let context_type_id = TypeId::of::<T>(); |
| 1512 |
self.context_mut() |
| 1513 |
.app_context |
| 1514 |
.get_mut(&context_type_id) |
| 1515 |
.and_then(|app_context| app_context.downcast_mut::<T>()) |
| 1516 |
} |
| 1517 |
|
| 1518 |
#[cfg(feature = "unstable-cert_authorities")] |
| 1519 |
pub(crate) fn cert_request_state(&mut self) -> &mut CertRequestState { |
| 1520 |
&mut self.context_mut().cert_request_state |
| 1521 |
} |
| 1522 |
|
| 1523 |
#[cfg(feature = "unstable-renegotiate")] |
| 1524 |
pub(crate) fn renegotiate_state_mut(&mut self) -> &mut RenegotiateState { |
| 1525 |
&mut self.context_mut().renegotiate_state |
| 1526 |
} |
| 1527 |
|
| 1528 |
#[cfg(feature = "unstable-renegotiate")] |
| 1529 |
pub(crate) fn renegotiate_state(&self) -> &RenegotiateState { |
| 1530 |
&self.context().renegotiate_state |
| 1531 |
} |
| 1532 |
} |
| 1533 |
|
| 1534 |
struct Context { |
| 1535 |
waker: Option<Waker>, |
| 1536 |
async_callback: Option<AsyncCallback>, |
| 1537 |
verify_host_callback: Option<Box<dyn VerifyHostNameCallback>>, |
| 1538 |
connection_initialized: bool, |
| 1539 |
app_context: HashMap<TypeId, Box<dyn Any + Send + Sync>>, |
| 1540 |
#[cfg(feature = "unstable-renegotiate")] |
| 1541 |
pub(crate) renegotiate_state: RenegotiateState, |
| 1542 |
#[cfg(feature = "unstable-cert_authorities")] |
| 1543 |
pub(crate) cert_request_state: CertRequestState, |
| 1544 |
} |
| 1545 |
|
| 1546 |
impl Context { |
| 1547 |
fn new() -> Self { |
| 1548 |
Context { |
| 1549 |
waker: None, |
| 1550 |
async_callback: None, |
| 1551 |
verify_host_callback: None, |
| 1552 |
connection_initialized: false, |
| 1553 |
app_context: HashMap::new(), |
| 1554 |
#[cfg(feature = "unstable-renegotiate")] |
| 1555 |
renegotiate_state: RenegotiateState::default(), |
| 1556 |
#[cfg(feature = "unstable-cert_authorities")] |
| 1557 |
cert_request_state: CertRequestState::default(), |
| 1558 |
} |
| 1559 |
} |
| 1560 |
} |
| 1561 |
|
| 1562 |
#[cfg(feature = "quic")] |
| 1563 |
impl Connection { |
| 1564 |
|
| 1565 |
pub fn enable_quic(&mut self) -> Result<&mut Self, Error> { |
| 1566 |
unsafe { s2n_connection_enable_quic(self.connection.as_ptr()).into_result() }?; |
| 1567 |
Ok(self) |
| 1568 |
} |
| 1569 |
|
| 1570 |
|
| 1571 |
pub fn set_quic_transport_parameters(&mut self, buffer: &[u8]) -> Result<&mut Self, Error> { |
| 1572 |
unsafe { |
| 1573 |
s2n_connection_set_quic_transport_parameters( |
| 1574 |
self.connection.as_ptr(), |
| 1575 |
buffer.as_ptr(), |
| 1576 |
buffer.len().try_into().map_err(|_| Error::INVALID_INPUT)?, |
| 1577 |
) |
| 1578 |
.into_result() |
| 1579 |
}?; |
| 1580 |
Ok(self) |
| 1581 |
} |
| 1582 |
|
| 1583 |
|
| 1584 |
pub fn quic_transport_parameters(&mut self) -> Result<&[u8], Error> { |
| 1585 |
let mut ptr = core::ptr::null(); |
| 1586 |
let mut len = 0; |
| 1587 |
unsafe { |
| 1588 |
s2n_connection_get_quic_transport_parameters( |
| 1589 |
self.connection.as_ptr(), |
| 1590 |
&mut ptr, |
| 1591 |
&mut len, |
| 1592 |
) |
| 1593 |
.into_result() |
| 1594 |
}?; |
| 1595 |
let buffer = unsafe { core::slice::from_raw_parts(ptr, len as _) }; |
| 1596 |
Ok(buffer) |
| 1597 |
} |
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
|
| 1602 |
|
| 1603 |
|
| 1604 |
pub unsafe fn set_secret_callback( |
| 1605 |
&mut self, |
| 1606 |
callback: s2n_secret_cb, |
| 1607 |
context: *mut c_void, |
| 1608 |
) -> Result<&mut Self, Error> { |
| 1609 |
s2n_connection_set_secret_callback(self.connection.as_ptr(), callback, context) |
| 1610 |
.into_result()?; |
| 1611 |
Ok(self) |
| 1612 |
} |
| 1613 |
|
| 1614 |
|
| 1615 |
pub fn quic_process_post_handshake_message(&mut self) -> Result<&mut Self, Error> { |
| 1616 |
let mut blocked = s2n_blocked_status::NOT_BLOCKED; |
| 1617 |
unsafe { |
| 1618 |
s2n_recv_quic_post_handshake_message(self.connection.as_ptr(), &mut blocked) |
| 1619 |
.into_result() |
| 1620 |
}?; |
| 1621 |
Ok(self) |
| 1622 |
} |
| 1623 |
|
| 1624 |
|
| 1625 |
|
| 1626 |
|
| 1627 |
pub fn are_session_tickets_enabled(&self) -> bool { |
| 1628 |
unsafe { s2n_connection_are_session_tickets_enabled(self.connection.as_ptr()) } |
| 1629 |
} |
| 1630 |
} |
| 1631 |
|
| 1632 |
impl AsRef<Connection> for Connection { |
| 1633 |
fn as_ref(&self) -> &Connection { |
| 1634 |
self |
| 1635 |
} |
| 1636 |
} |
| 1637 |
|
| 1638 |
impl AsMut<Connection> for Connection { |
| 1639 |
fn as_mut(&mut self) -> &mut Connection { |
| 1640 |
self |
| 1641 |
} |
| 1642 |
} |
| 1643 |
|
| 1644 |
impl Drop for Connection { |
| 1645 |
|
| 1646 |
fn drop(&mut self) { |
| 1647 |
|
| 1648 |
unsafe { |
| 1649 |
|
| 1650 |
let _ = self.drop_context(); |
| 1651 |
|
| 1652 |
|
| 1653 |
let _ = self.drop_config(); |
| 1654 |
|
| 1655 |
|
| 1656 |
let _ = s2n_connection_free(self.connection.as_ptr()).into_result(); |
| 1657 |
} |
| 1658 |
} |
| 1659 |
} |
| 1660 |
|
| 1661 |
#[cfg(test)] |
| 1662 |
mod tests { |
| 1663 |
use futures_test::task::noop_waker; |
| 1664 |
|
| 1665 |
use super::*; |
| 1666 |
use crate::{ |
| 1667 |
security::Policy, |
| 1668 |
testing::{build_config, config_builder, LIFOSessionResumption, SniTestCerts, TestPair}, |
| 1669 |
}; |
| 1670 |
use std::{ |
| 1671 |
net::{IpAddr, Ipv4Addr, SocketAddr}, |
| 1672 |
time::SystemTime, |
| 1673 |
}; |
| 1674 |
|
| 1675 |
|
| 1676 |
#[test] |
| 1677 |
fn context_send_test() { |
| 1678 |
fn assert_send<T: 'static + Send>() {} |
| 1679 |
assert_send::<Context>(); |
| 1680 |
} |
| 1681 |
|
| 1682 |
|
| 1683 |
#[test] |
| 1684 |
fn context_sync_test() { |
| 1685 |
fn assert_sync<T: 'static + Sync>() {} |
| 1686 |
assert_sync::<Context>(); |
| 1687 |
} |
| 1688 |
|
| 1689 |
|
| 1690 |
#[test] |
| 1691 |
fn test_app_context_set_and_retrieve() { |
| 1692 |
let mut connection = Connection::new_server(); |
| 1693 |
|
| 1694 |
let test_value: u32 = 1142; |
| 1695 |
|
| 1696 |
|
| 1697 |
assert!(connection.application_context::<u32>().is_none()); |
| 1698 |
|
| 1699 |
connection.set_application_context(test_value); |
| 1700 |
|
| 1701 |
|
| 1702 |
assert_eq!(*connection.application_context::<u32>().unwrap(), 1142); |
| 1703 |
} |
| 1704 |
|
| 1705 |
|
| 1706 |
#[test] |
| 1707 |
fn test_app_context_modify() { |
| 1708 |
let test_value: u64 = 0; |
| 1709 |
|
| 1710 |
let mut connection = Connection::new_server(); |
| 1711 |
connection.set_application_context(test_value); |
| 1712 |
|
| 1713 |
let context_value = connection.application_context_mut::<u64>().unwrap(); |
| 1714 |
*context_value += 1; |
| 1715 |
|
| 1716 |
assert_eq!(*connection.application_context::<u64>().unwrap(), 1); |
| 1717 |
} |
| 1718 |
|
| 1719 |
|
| 1720 |
#[test] |
| 1721 |
fn test_app_context_override() { |
| 1722 |
let mut connection = Connection::new_server(); |
| 1723 |
|
| 1724 |
let test_value: u16 = 1142; |
| 1725 |
connection.set_application_context(test_value); |
| 1726 |
|
| 1727 |
assert_eq!(*connection.application_context::<u16>().unwrap(), 1142); |
| 1728 |
|
| 1729 |
|
| 1730 |
let test_value: u16 = 10; |
| 1731 |
connection.set_application_context(test_value); |
| 1732 |
|
| 1733 |
assert_eq!(*connection.application_context::<u16>().unwrap(), 10); |
| 1734 |
|
| 1735 |
|
| 1736 |
let test_value: i16 = -20; |
| 1737 |
connection.set_application_context(test_value); |
| 1738 |
|
| 1739 |
assert_eq!(*connection.application_context::<i16>().unwrap(), -20); |
| 1740 |
} |
| 1741 |
|
| 1742 |
|
| 1743 |
#[test] |
| 1744 |
fn test_multiple_app_contexts() { |
| 1745 |
let mut connection = Connection::new_server(); |
| 1746 |
|
| 1747 |
let first_test_value: u16 = 1142; |
| 1748 |
connection.set_application_context(first_test_value); |
| 1749 |
|
| 1750 |
assert_eq!(*connection.application_context::<u16>().unwrap(), 1142); |
| 1751 |
|
| 1752 |
|
| 1753 |
let second_test_value: SocketAddr = |
| 1754 |
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080); |
| 1755 |
connection.set_application_context(second_test_value); |
| 1756 |
|
| 1757 |
assert_eq!( |
| 1758 |
*connection.application_context::<SocketAddr>().unwrap(), |
| 1759 |
second_test_value |
| 1760 |
); |
| 1761 |
|
| 1762 |
|
| 1763 |
assert_eq!( |
| 1764 |
second_test_value, |
| 1765 |
*connection |
| 1766 |
.remove_application_context::<SocketAddr>() |
| 1767 |
.unwrap() |
| 1768 |
.downcast::<SocketAddr>() |
| 1769 |
.unwrap() |
| 1770 |
); |
| 1771 |
|
| 1772 |
assert!(connection.application_context::<SocketAddr>().is_none()); |
| 1773 |
} |
| 1774 |
|
| 1775 |
|
| 1776 |
#[test] |
| 1777 |
fn test_app_context_invalid_type() { |
| 1778 |
let mut connection = Connection::new_server(); |
| 1779 |
|
| 1780 |
let test_value: u32 = 0; |
| 1781 |
connection.set_application_context(test_value); |
| 1782 |
|
| 1783 |
|
| 1784 |
assert!(connection.application_context::<i16>().is_none()); |
| 1785 |
|
| 1786 |
|
| 1787 |
assert!(connection.application_context::<u32>().is_some()); |
| 1788 |
} |
| 1789 |
|
| 1790 |
|
| 1791 |
|
| 1792 |
#[test] |
| 1793 |
fn test_certificate_match_variants() -> Result<(), Box<dyn std::error::Error>> { |
| 1794 |
let scenarios = vec![ |
| 1795 |
(None, CertSNIMatch::NoSNI), |
| 1796 |
(Some("nonmatching_sni"), CertSNIMatch::NoMatch), |
| 1797 |
(Some("127.0.0.1"), CertSNIMatch::ExactMatch), |
| 1798 |
]; |
| 1799 |
|
| 1800 |
for (sni_opt, expected) in scenarios { |
| 1801 |
let config = build_config(&security::DEFAULT_TLS13)?; |
| 1802 |
let mut pair = TestPair::from_config(&config); |
| 1803 |
|
| 1804 |
if let Some(sni) = sni_opt { |
| 1805 |
pair.client.set_server_name(sni)?; |
| 1806 |
} |
| 1807 |
|
| 1808 |
pair.handshake()?; |
| 1809 |
let cert_match = pair.server.certificate_match()?; |
| 1810 |
|
| 1811 |
assert_eq!(cert_match, expected,); |
| 1812 |
} |
| 1813 |
|
| 1814 |
Ok(()) |
| 1815 |
} |
| 1816 |
|
| 1817 |
|
| 1818 |
#[test] |
| 1819 |
fn test_certificate_match_returns_wildcard_match() -> Result<(), Box<dyn std::error::Error>> { |
| 1820 |
let wildcard_cert = SniTestCerts::WildcardInsectRsa.get(); |
| 1821 |
|
| 1822 |
let mut builder = crate::config::Builder::new(); |
| 1823 |
builder.load_pem(wildcard_cert.cert(), wildcard_cert.key())?; |
| 1824 |
let server_config = builder.build()?; |
| 1825 |
|
| 1826 |
let mut client_builder = crate::config::Builder::new(); |
| 1827 |
client_builder.trust_pem(wildcard_cert.cert())?; |
| 1828 |
let client_config = client_builder.build()?; |
| 1829 |
|
| 1830 |
let mut pair = TestPair::from_configs(&client_config, &server_config); |
| 1831 |
|
| 1832 |
pair.client.set_server_name("anything.insect.hexapod")?; |
| 1833 |
pair.handshake()?; |
| 1834 |
|
| 1835 |
let cert_match = pair.server.certificate_match()?; |
| 1836 |
assert_eq!(cert_match, CertSNIMatch::WildcardMatch); |
| 1837 |
|
| 1838 |
Ok(()) |
| 1839 |
} |
| 1840 |
|
| 1841 |
|
| 1842 |
#[cfg(s2n_tls_external_build)] |
| 1843 |
#[test] |
| 1844 |
fn test_unstable_as_ptr() -> Result<(), Error> { |
| 1845 |
let mut connection = Connection::new_client(); |
| 1846 |
|
| 1847 |
let test_server_name = "test-server-name"; |
| 1848 |
connection.set_server_name(test_server_name)?; |
| 1849 |
|
| 1850 |
let server_name = unsafe { |
| 1851 |
let server_name = s2n_get_server_name(connection.unstable_as_ptr()); |
| 1852 |
CStr::from_ptr(server_name).to_str().unwrap() |
| 1853 |
}; |
| 1854 |
|
| 1855 |
assert_eq!(server_name, test_server_name); |
| 1856 |
Ok(()) |
| 1857 |
} |
| 1858 |
|
| 1859 |
#[test] |
| 1860 |
fn signature_scheme_before_handshake() { |
| 1861 |
let connection = Connection::new_server(); |
| 1862 |
assert_eq!(connection.signature_scheme(), None); |
| 1863 |
} |
| 1864 |
|
| 1865 |
#[test] |
| 1866 |
fn signature_scheme_after_handshake() -> Result<(), Box<dyn std::error::Error>> { |
| 1867 |
let server_config = { |
| 1868 |
|
| 1869 |
let policy = Policy::from_version("20240503")?; |
| 1870 |
let mut builder = config_builder(&policy).unwrap(); |
| 1871 |
builder.add_session_ticket_key( |
| 1872 |
b"a key name", |
| 1873 |
b"good enough bytes for test", |
| 1874 |
SystemTime::UNIX_EPOCH, |
| 1875 |
)?; |
| 1876 |
builder.build()? |
| 1877 |
}; |
| 1878 |
|
| 1879 |
let client_config = { |
| 1880 |
let session_tickets = LIFOSessionResumption::default(); |
| 1881 |
let mut builder = config_builder(&security::DEFAULT).unwrap(); |
| 1882 |
builder.enable_session_tickets(true)?; |
| 1883 |
builder.set_session_ticket_callback(session_tickets.clone())?; |
| 1884 |
builder.set_connection_initializer(session_tickets.clone())?; |
| 1885 |
builder.build()? |
| 1886 |
}; |
| 1887 |
|
| 1888 |
|
| 1889 |
{ |
| 1890 |
let mut test_pair = TestPair::from_configs(&client_config, &server_config); |
| 1891 |
test_pair.client.set_waker(Some(&noop_waker()))?; |
| 1892 |
test_pair.handshake().unwrap(); |
| 1893 |
|
| 1894 |
assert!(test_pair.client.poll_recv(&mut [0]).is_pending()); |
| 1895 |
|
| 1896 |
for conn in [&test_pair.client, &test_pair.server] { |
| 1897 |
assert_eq!(conn.signature_scheme(), Some("rsa_pss_rsae_sha256")) |
| 1898 |
} |
| 1899 |
} |
| 1900 |
|
| 1901 |
|
| 1902 |
{ |
| 1903 |
let mut test_pair = TestPair::from_configs(&client_config, &server_config); |
| 1904 |
test_pair.client.set_waker(Some(&noop_waker()))?; |
| 1905 |
test_pair.handshake().unwrap(); |
| 1906 |
assert!(test_pair.client.resumed()); |
| 1907 |
assert_eq!(test_pair.client.signature_scheme(), None); |
| 1908 |
assert_eq!(test_pair.server.signature_scheme(), None); |
| 1909 |
} |
| 1910 |
Ok(()) |
| 1911 |
} |
| 1912 |
} |