Skip to content

Commit

Permalink
Guard against empty strings given to select_next_proto (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
nox authored Aug 2, 2024
1 parent 5e304d9 commit 8ece782
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions boring/src/ssl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,10 @@ impl CompliancePolicy {
/// [`SslContextBuilder::set_alpn_protos`]: struct.SslContextBuilder.html#method.set_alpn_protos
/// [`SSL_select_next_proto`]: https://www.openssl.org/docs/man1.1.0/ssl/SSL_CTX_set_alpn_protos.html
pub fn select_next_proto<'a>(server: &[u8], client: &'a [u8]) -> Option<&'a [u8]> {
if server.is_empty() || client.is_empty() {
return None;
}

unsafe {
let mut out = ptr::null_mut();
let mut outlen = 0;
Expand All @@ -826,6 +830,7 @@ pub fn select_next_proto<'a>(server: &[u8], client: &'a [u8]) -> Option<&'a [u8]
client.as_ptr(),
client.len() as c_uint,
);

if r == ffi::OPENSSL_NPN_NEGOTIATED {
Some(slice::from_raw_parts(out as *const u8, outlen as usize))
} else {
Expand Down
7 changes: 7 additions & 0 deletions boring/src/ssl/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,13 @@ fn test_alpn_server_select_none() {
assert_eq!(None, s.ssl().selected_alpn_protocol());
}

#[test]
fn test_empty_alpn() {
assert_eq!(ssl::select_next_proto(b"", b""), None);
assert_eq!(ssl::select_next_proto(b"", b"\x08http/1.1"), None);
assert_eq!(ssl::select_next_proto(b"\x08http/1.1", b""), None);
}

#[test]
fn test_alpn_server_unilateral() {
let server = Server::builder().build();
Expand Down

0 comments on commit 8ece782

Please sign in to comment.