Skip to content

Commit

Permalink
s2n-tls: expose selected application protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
zh-jq-b committed Jun 13, 2024
1 parent d611eea commit cbe0841
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
8 changes: 8 additions & 0 deletions bindings/rust/s2n-tls/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,14 @@ impl Connection {
})
}

pub fn selected_application_protocol(&self) -> Option<&[u8]> {
let protocol = unsafe { s2n_get_application_protocol(self.connection.as_ptr()) };
if protocol.is_null() {
return None;
}
Some(unsafe { CStr::from_ptr(protocol).to_bytes() })
}

/// Provides access to the TLS-Exporter functionality.
///
/// See https://datatracker.ietf.org/doc/html/rfc5705 and https://www.rfc-editor.org/rfc/rfc8446.
Expand Down
21 changes: 21 additions & 0 deletions bindings/rust/s2n-tls/src/testing/s2n_tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1030,4 +1030,25 @@ mod tests {
.unwrap();
assert_eq!(context.invoked_count, 1);
}

#[test]
fn no_application_protocol() -> Result<(), Error> {
let config = config_builder(&security::DEFAULT)?.build()?;
let mut pair = tls_pair(config);
assert!(poll_tls_pair_result(&mut pair).is_ok());
assert!(pair.server.0.connection.selected_application_protocol().is_none());
Ok(())
}

#[test]
fn application_protocol() -> Result<(), Error> {
let config = config_builder(&security::DEFAULT)?.build()?;
let mut pair = tls_pair(config);
pair.server.0.connection.set_application_protocol_preference(["http/1.1", "h2"]).unwrap();
pair.client.0.connection.append_application_protocol_preference(b"h2").unwrap();
assert!(poll_tls_pair_result(&mut pair).is_ok());
let protocol = pair.server.0.connection.selected_application_protocol().unwrap();
assert_eq!(protocol, b"h2");
Ok(())
}
}

0 comments on commit cbe0841

Please sign in to comment.