Skip to content

Commit

Permalink
s2n-tls rust binding: expose selected application protocol (#4599)
Browse files Browse the repository at this point in the history
Co-authored-by: Lindsay Stewart <slindsay@amazon.com>
  • Loading branch information
zh-jq-b and lrstewart committed Jun 18, 2024
1 parent 5ea31b1 commit 933f379
Show file tree
Hide file tree
Showing 2 changed files with 35 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 @@ -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(())
}
}

0 comments on commit 933f379

Please sign in to comment.