Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

s2n-tls rust binding: expose selected application protocol #4599

Merged
merged 6 commits into from
Jun 18, 2024
Merged
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
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 @@ -947,6 +947,14 @@ impl Connection {
})
}

pub fn 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
27 changes: 27 additions & 0 deletions bindings/rust/s2n-tls/src/testing/s2n_tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -998,4 +998,31 @@ mod tests {

Ok(())
}

#[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.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"])?;
pair.client
.0
.connection
.append_application_protocol_preference(b"h2")?;
assert!(poll_tls_pair_result(&mut pair).is_ok());
let protocol = pair.server.0.connection.application_protocol().unwrap();
assert_eq!(protocol, b"h2");
Ok(())
}
}
Loading