Skip to content

Commit

Permalink
Add support for fetching SslCipher stack from ClientHelloResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
RoastVeg committed May 9, 2023
1 parent 5d2c405 commit 56d2e32
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
9 changes: 9 additions & 0 deletions openssl-sys/src/handwritten/ssl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,15 @@ extern "C" {
num: size_t,
readbytes: *mut size_t,
) -> c_int;
#[cfg(ossl111)]
pub fn SSL_bytes_to_cipher_list(
s: *mut SSL,
bytes: *const c_uchar,
len: size_t,
isv2format: c_int,
sk: *mut *mut stack_st_SSL_CIPHER,
scsvs: *mut *mut stack_st_SSL_CIPHER
) -> c_int;
}

extern "C" {
Expand Down
46 changes: 45 additions & 1 deletion openssl/src/ssl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ use crate::srtp::{SrtpProtectionProfile, SrtpProtectionProfileRef};
use crate::ssl::bio::BioMethod;
use crate::ssl::callbacks::*;
use crate::ssl::error::InnerError;
use crate::stack::{Stack, StackRef};
use crate::stack::{Stack, StackRef, Stackable};
use crate::util::{ForeignTypeExt, ForeignTypeRefExt};
use crate::x509::store::{X509Store, X509StoreBuilderRef, X509StoreRef};
#[cfg(any(ossl102, libressl261))]
Expand Down Expand Up @@ -1940,6 +1940,10 @@ impl ForeignType for SslCipher {
}
}

impl Stackable for SslCipher {
type StackType = ffi::stack_st_SSL_CIPHER;
}

impl Deref for SslCipher {
type Target = SslCipherRef;

Expand Down Expand Up @@ -2056,6 +2060,12 @@ impl SslCipherRef {
}
}

impl fmt::Debug for SslCipherRef {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "{}", self.name())
}
}

foreign_type_and_impl_send_sync! {
type CType = ffi::SSL_SESSION;
fn drop = ffi::SSL_SESSION_free;
Expand Down Expand Up @@ -3083,6 +3093,40 @@ impl SslRef {
}
}

/// Returns the stack of ciphers from the ciphers field of the client's hello message, and
/// the signalling cipher suite values.
///
/// This can only be used inside of the client hello callback. Otherwise, `None` is returned.
///
/// Requires OpenSSL 1.1.1 or newer.
#[corresponds(SSL_bytes_to_cipher_list)]
#[cfg(ossl111)]
pub fn client_hello_ciphers_stack(&self) -> Option<(Stack<SslCipher>, Stack<SslCipher>)> {
unsafe {
let mut ptr = ptr::null();
let len = ffi::SSL_client_hello_get0_ciphers(self.as_ptr(), &mut ptr);
if len == 0 {
None
} else {
let mut sk = ptr::null_mut();
let mut scsvs = ptr::null_mut();
let res = ffi::SSL_bytes_to_cipher_list(
self.as_ptr(),
ptr,
len,
ffi::SSL_client_hello_isv2(self.as_ptr()),
&mut sk,
&mut scsvs,
);
if res == 1 {
Some((Stack::from_ptr(sk), Stack::from_ptr(scsvs)))
} else {
None
}
}
}
}

/// Returns the compression methods field of the client's hello message.
///
/// This can only be used inside of the client hello callback. Otherwise, `None` is returned.
Expand Down
1 change: 1 addition & 0 deletions openssl/src/ssl/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1457,6 +1457,7 @@ fn client_hello() {
assert!(ssl.client_hello_random().is_some());
assert!(ssl.client_hello_session_id().is_some());
assert!(ssl.client_hello_ciphers().is_some());
assert!(ssl.client_hello_ciphers_stack().is_some());
assert!(ssl.client_hello_compression_methods().is_some());

CALLED_BACK.store(true, Ordering::SeqCst);
Expand Down

0 comments on commit 56d2e32

Please sign in to comment.