Skip to content

Commit

Permalink
Add APIs to expose client and server cipher lists
Browse files Browse the repository at this point in the history
The client sent ciphers in the ClientHello are unparsed and thus require
the user to convert u16s into SslCipher instances. It could be worth
doing this parsing in the library itself to make things consistent and
always return a StackRef<SslCipher>.
  • Loading branch information
rushilmehra authored and ghedo committed Jun 26, 2024
1 parent 1879e9c commit 936d81b
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions boring/src/ssl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2270,11 +2270,29 @@ impl ClientHello<'_> {
pub fn random(&self) -> &[u8] {
unsafe { slice::from_raw_parts(self.0.random, self.0.random_len) }
}

/// Returns the raw list of ciphers supported by the client in its Client Hello record.
pub fn ciphers(&self) -> &[u8] {
unsafe { slice::from_raw_parts(self.0.cipher_suites, self.0.cipher_suites_len) }
}
}

/// Information about a cipher.
pub struct SslCipher(*mut ffi::SSL_CIPHER);

impl SslCipher {
pub fn from_value(value: u16) -> Option<Self> {
unsafe {
let ptr = ffi::SSL_get_cipher_by_value(value);
if ptr.is_null() {
None
} else {
Some(Self::from_ptr(ptr as *mut ffi::SSL_CIPHER))
}
}
}
}

impl Stackable for SslCipher {
type StackType = ffi::stack_st_SSL_CIPHER;
}
Expand Down Expand Up @@ -2958,6 +2976,18 @@ impl SslRef {
}
}

/// Returns the stack of available SslCiphers for `SSL`, sorted by preference.
///
/// This corresponds to [`SSL_get_ciphers`].
///
/// [`SSL_get_ciphers`]: https://www.openssl.org/docs/man1.0.2/man3/SSL_get_ciphers.html
pub fn ciphers(&self) -> &StackRef<SslCipher> {
unsafe {
let cipher_list = ffi::SSL_get_ciphers(self.as_ptr());
StackRef::from_ptr(cipher_list)
}
}

/// Returns the current cipher if the session is active.
///
/// This corresponds to [`SSL_get_current_cipher`].
Expand Down

0 comments on commit 936d81b

Please sign in to comment.