Debian Codegraph @

crt/s2n/bindings/rust/extended/s2n-tls/src/callbacks/session_ticket.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 std::time::Duration;
5
6 use s2n_tls_sys::*;
7
8 use crate::{
9 connection::Connection,
10 error::{Error, Fallible},
11 };
12
13 /// A trait to retrieve session tickets from the connection
14 pub trait SessionTicketCallback: 'static + Send + Sync {
15 fn on_session_ticket(&self, connection: &mut Connection, session_ticket: &SessionTicket);
16 }
17
18 pub struct SessionTicket(s2n_session_ticket);
19
20 impl SessionTicket {
21 pub(crate) fn from_ptr(ticket: &s2n_session_ticket) -> &Self {
22 unsafe { &*(ticket as *const s2n_session_ticket as *const SessionTicket) }
23 }
24
25 // SAFETY: casting *const s2n_session_ticket -> *mut s2n_session_ticket: This is
26 // safe as long as the data is not actually mutated. As authors of s2n-tls,
27 // we know that the get_lifetime and get_data methods do not mutate the
28 // data, and use mut pointers as a matter of convention because it makes
29 // working with s2n_stuffers and s2n_blobs easier.
30 pub(crate) fn deref_mut_ptr(&self) -> *mut s2n_session_ticket {
31 &self.0 as *const s2n_session_ticket as *mut s2n_session_ticket
32 }
33
34 /// Corresponds to [`s2n_session_ticket_get_lifetime`].
35 pub fn lifetime(&self) -> Result<Duration, Error> {
36 let mut lifetime = 0;
37 unsafe {
38 s2n_session_ticket_get_lifetime(self.deref_mut_ptr(), &mut lifetime).into_result()?
39 };
40 Ok(Duration::new(lifetime.into(), 0))
41 }
42
43 /// Corresponds to [`s2n_session_ticket_get_data_len`].
44 #[allow(clippy::len_without_is_empty)]
45 pub fn len(&self) -> Result<usize, Error> {
46 let mut data_len = 0;
47 unsafe {
48 s2n_session_ticket_get_data_len(self.deref_mut_ptr(), &mut data_len).into_result()?
49 };
50 Ok(data_len)
51 }
52
53 /// Corresponds to [`s2n_session_ticket_get_data`].
54 pub fn data(&self, output: &mut [u8]) -> Result<(), Error> {
55 unsafe {
56 s2n_session_ticket_get_data(self.deref_mut_ptr(), output.len(), output.as_mut_ptr())
57 .into_result()?
58 };
59 Ok(())
60 }
61 }