use std::ffi::c_int;
use std::fmt;
use crate::rslice::rustls_str;
pub type rustls_keylog_log_callback = Option<
unsafe extern "C" fn(
label: rustls_str,
client_random: *const u8,
client_random_len: usize,
secret: *const u8,
secret_len: usize,
),
>;
pub type rustls_keylog_will_log_callback = Option<unsafe extern "C" fn(label: rustls_str) -> c_int>;
pub(crate) type KeylogLogCallback = unsafe extern "C" fn(
label: rustls_str,
client_random: *const u8,
client_random_len: usize,
secret: *const u8,
secret_len: usize,
);
pub(crate) struct CallbackKeyLog {
pub(crate) log_cb: KeylogLogCallback,
pub(crate) will_log_cb: rustls_keylog_will_log_callback,
}
impl rustls::KeyLog for CallbackKeyLog {
fn log(&self, label: &str, client_random: &[u8], secret: &[u8]) {
unsafe {
(self.log_cb)(
rustls_str::try_from(label).unwrap(),
client_random.as_ptr(),
client_random.len(),
secret.as_ptr(),
secret.len(),
);
}
}
fn will_log(&self, label: &str) -> bool {
match self.will_log_cb {
Some(cb) => {
let label = rustls_str::try_from(label).unwrap();
!matches!(unsafe { (cb)(label) }, 0)
}
None => true,
}
}
}
impl fmt::Debug for CallbackKeyLog {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("CallbackKeyLog").finish()
}
}
unsafe impl Send for CallbackKeyLog {}
unsafe impl Sync for CallbackKeyLog {}