Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions quiche/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7614,6 +7614,25 @@ impl<F: BufFactory> Connection<F> {
self.handshake.peer_cert()
}

/// Fills the provided buffer with TLS exporter keying material.
///
/// The exporter is available after the TLS handshake completes. The label
/// identifies the protocol usage, while the optional context can separate
/// exporter instances within that usage. Returns [`InvalidState`] until
/// the connection is established.
///
/// [`InvalidState`]: enum.Error.html#variant.InvalidState
#[inline]
pub fn export_keying_material(
&self, out: &mut [u8], label: &[u8], context: Option<&[u8]>,
) -> Result<()> {
if !self.is_established() {
return Err(Error::InvalidState);
}

self.handshake.export_keying_material(out, label, context)
}

/// Returns the peer's certificate chain (if any) as a vector of DER-encoded
/// buffers.
///
Expand Down
72 changes: 72 additions & 0 deletions quiche/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,69 @@ fn handshake(#[values("cubic", "bbr2_gcongestion")] cc_algorithm_name: &str) {
assert_eq!(pipe.server.server_name(), Some("quic.tech"));
}

#[test]
fn export_keying_material() {
let mut pipe = test_utils::Pipe::new("cubic").unwrap();
let mut before_handshake = [0; 32];
assert_eq!(
pipe.client.export_keying_material(
&mut before_handshake,
b"EXPORTER-quiche-test",
None,
),
Err(Error::InvalidState),
);
assert_eq!(pipe.handshake(), Ok(()));

let mut client = [0; 32];
let mut server = [0; 32];
pipe.client
.export_keying_material(&mut client, b"EXPORTER-quiche-test", None)
.unwrap();
pipe.server
.export_keying_material(&mut server, b"EXPORTER-quiche-test", None)
.unwrap();
assert_eq!(client, server);

let mut other_label = [0; 32];
pipe.client
.export_keying_material(
&mut other_label,
b"EXPORTER-quiche-other-test",
None,
)
.unwrap();
assert_ne!(client, other_label);

let mut with_context = [0; 32];
pipe.client
.export_keying_material(
&mut with_context,
b"EXPORTER-quiche-test",
Some(b"context"),
)
.unwrap();
assert_ne!(client, with_context);

let mut client_with_empty_context = [0; 32];
let mut server_with_empty_context = [0; 32];
pipe.client
.export_keying_material(
&mut client_with_empty_context,
b"EXPORTER-quiche-test",
Some(b""),
)
.unwrap();
pipe.server
.export_keying_material(
&mut server_with_empty_context,
b"EXPORTER-quiche-test",
Some(b""),
)
.unwrap();
assert_eq!(client_with_empty_context, server_with_empty_context);
}

#[rstest]
fn handshake_done(
#[values("cubic", "bbr2_gcongestion")] cc_algorithm_name: &str,
Expand Down Expand Up @@ -494,6 +557,15 @@ fn handshake_confirmation(

assert!(!pipe.server.is_established());
assert!(!pipe.server.handshake_confirmed);
let mut server_exporter = [0; 32];
assert_eq!(
pipe.server.export_keying_material(
&mut server_exporter,
b"EXPORTER-quiche-test",
None,
),
Err(Error::InvalidState),
);

test_utils::process_flight(&mut pipe.client, flight).unwrap();

Expand Down
28 changes: 28 additions & 0 deletions quiche/src/tls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,28 @@ impl Handshake {
get_cipher_from_ptr(cipher.ok()?).ok()
}

pub fn export_keying_material(
&self, out: &mut [u8], label: &[u8], context: Option<&[u8]>,
) -> Result<()> {
let (context_ptr, context_len, use_context) = match context {
Some(context) => (context.as_ptr(), context.len(), 1),
None => (ptr::null(), 0, 0),
};

map_result(unsafe {
SSL_export_keying_material(
self.as_ptr(),
out.as_mut_ptr(),
out.len(),
label.as_ptr().cast(),
label.len(),
context_ptr,
context_len,
use_context,
)
})
}

#[cfg(test)]
pub fn set_options(&mut self, opts: u32) {
unsafe {
Expand Down Expand Up @@ -1168,6 +1190,12 @@ extern "C" {

fn SSL_get_current_cipher(ssl: *const SSL) -> *const SSL_CIPHER;

fn SSL_export_keying_material(
ssl: *const SSL, out: *mut u8, out_len: usize, label: *const c_char,
label_len: usize, context: *const u8, context_len: usize,
use_context: c_int,
) -> c_int;

fn SSL_set_session(ssl: *mut SSL, session: *mut SSL_SESSION) -> c_int;

fn SSL_get_SSL_CTX(ssl: *const SSL) -> *mut SSL_CTX;
Expand Down