Generic custom extension add fn return type

This commit is contained in:
Benjamin Saunders 2018-03-10 22:30:54 -08:00
parent b0bc1c770e
commit e02dbde2f7
3 changed files with 30 additions and 30 deletions

View File

@ -2,8 +2,6 @@ use ffi;
use libc::{c_char, c_int, c_uchar, c_uint, c_void}; use libc::{c_char, c_int, c_uchar, c_uint, c_void};
#[cfg(all(feature = "v111", ossl111))] #[cfg(all(feature = "v111", ossl111))]
use libc::size_t; use libc::size_t;
#[cfg(all(feature = "v111", ossl111))]
use std::borrow::Cow;
use std::ffi::CStr; use std::ffi::CStr;
use std::ptr; use std::ptr;
use std::slice; use std::slice;
@ -426,17 +424,18 @@ where
} }
#[cfg(all(feature = "v111", ossl111))] #[cfg(all(feature = "v111", ossl111))]
pub struct CustomExtAddState(Option<Cow<'static, [u8]>>); pub struct CustomExtAddState<T>(Option<T>);
#[cfg(all(feature = "v111", ossl111))] #[cfg(all(feature = "v111", ossl111))]
pub extern "C" fn raw_custom_ext_add<F>(ssl: *mut ffi::SSL, _: c_uint, pub extern "C" fn raw_custom_ext_add<F, T>(ssl: *mut ffi::SSL, _: c_uint,
context: c_uint, context: c_uint,
out: *mut *const c_uchar, out: *mut *const c_uchar,
outlen: *mut size_t, x: *mut ffi::X509, outlen: *mut size_t, x: *mut ffi::X509,
chainidx: size_t, al: *mut c_int, chainidx: size_t, al: *mut c_int,
_: *mut c_void) _: *mut c_void)
-> c_int -> c_int
where F: Fn(&mut SslRef, ExtensionContext, Option<(usize, &X509Ref)>) -> Result<Option<Cow<'static, [u8]>>, SslAlert> + 'static where F: Fn(&mut SslRef, ExtensionContext, Option<(usize, &X509Ref)>) -> Result<Option<T>, SslAlert> + 'static,
T: AsRef<[u8]> + 'static,
{ {
unsafe { unsafe {
let ssl_ctx = ffi::SSL_get_SSL_CTX(ssl as *const _); let ssl_ctx = ffi::SSL_get_SSL_CTX(ssl as *const _);
@ -448,13 +447,13 @@ pub extern "C" fn raw_custom_ext_add<F>(ssl: *mut ffi::SSL, _: c_uint,
match (callback)(ssl, ectx, cert) { match (callback)(ssl, ectx, cert) {
Ok(None) => 0, Ok(None) => 0,
Ok(Some(buf)) => { Ok(Some(buf)) => {
*outlen = buf.len() as size_t; *outlen = buf.as_ref().len() as size_t;
*out = buf.as_ptr(); *out = buf.as_ref().as_ptr();
let idx = get_ssl_callback_idx::<CustomExtAddState>(); let idx = get_ssl_callback_idx::<CustomExtAddState<T>>();
let ptr = ffi::SSL_get_ex_data(ssl.as_ptr(), idx); let ptr = ffi::SSL_get_ex_data(ssl.as_ptr(), idx);
if ptr.is_null() { if ptr.is_null() {
let x = Box::into_raw(Box::<CustomExtAddState>::new(CustomExtAddState(Some(buf)))) as *mut c_void; let x = Box::into_raw(Box::<CustomExtAddState<T>>::new(CustomExtAddState(Some(buf)))) as *mut c_void;
ffi::SSL_set_ex_data(ssl.as_ptr(), idx, x); ffi::SSL_set_ex_data(ssl.as_ptr(), idx, x);
} else { } else {
*(ptr as *mut _) = CustomExtAddState(Some(buf)) *(ptr as *mut _) = CustomExtAddState(Some(buf))
@ -470,14 +469,15 @@ pub extern "C" fn raw_custom_ext_add<F>(ssl: *mut ffi::SSL, _: c_uint,
} }
#[cfg(all(feature = "v111", ossl111))] #[cfg(all(feature = "v111", ossl111))]
pub extern "C" fn raw_custom_ext_free(ssl: *mut ffi::SSL, _: c_uint, pub extern "C" fn raw_custom_ext_free<T>(ssl: *mut ffi::SSL, _: c_uint,
_: c_uint, _: c_uint,
_: *mut *const c_uchar, _: *mut *const c_uchar,
_: *mut c_void) _: *mut c_void)
where T: 'static
{ {
unsafe { unsafe {
let state = ffi::SSL_get_ex_data(ssl, get_ssl_callback_idx::<CustomExtAddState>()); let state = ffi::SSL_get_ex_data(ssl, get_ssl_callback_idx::<CustomExtAddState<T>>());
let state = &mut (*(state as *mut CustomExtAddState)).0; let state = &mut (*(state as *mut CustomExtAddState<T>)).0;
state.take(); state.take();
} }
} }

View File

@ -61,8 +61,6 @@ use ffi;
use foreign_types::{ForeignType, ForeignTypeRef, Opaque}; use foreign_types::{ForeignType, ForeignTypeRef, Opaque};
use libc::{c_char, c_int, c_long, c_uchar, c_uint, c_ulong, c_void}; use libc::{c_char, c_int, c_long, c_uchar, c_uint, c_ulong, c_void};
use std::any::TypeId; use std::any::TypeId;
#[cfg(all(feature = "v111", ossl111))]
use std::borrow::Cow;
use std::cmp; use std::cmp;
use std::collections::HashMap; use std::collections::HashMap;
use std::ffi::{CStr, CString}; use std::ffi::{CStr, CString};
@ -1511,10 +1509,12 @@ impl SslContextBuilder {
/// ///
/// [`SSL_CTX_add_custom_ext`]: https://www.openssl.org/docs/manmaster/man3/SSL_CTX_add_custom_ext.html /// [`SSL_CTX_add_custom_ext`]: https://www.openssl.org/docs/manmaster/man3/SSL_CTX_add_custom_ext.html
#[cfg(all(feature = "v111", ossl111))] #[cfg(all(feature = "v111", ossl111))]
pub fn add_custom_ext<AddFn, ParseFn>(&mut self, ext_type: u16, context: ExtensionContext, add_cb: AddFn, parse_cb: ParseFn) pub fn add_custom_ext<AddFn, ParseFn, T>(
-> Result<(), ErrorStack> &mut self, ext_type: u16, context: ExtensionContext, add_cb: AddFn, parse_cb: ParseFn
) -> Result<(), ErrorStack>
where AddFn: Fn(&mut SslRef, ExtensionContext, Option<(usize, &X509Ref)>) where AddFn: Fn(&mut SslRef, ExtensionContext, Option<(usize, &X509Ref)>)
-> Result<Option<Cow<'static, [u8]>>, SslAlert> + 'static + Sync + Send, -> Result<Option<T>, SslAlert> + 'static + Sync + Send,
T: AsRef<[u8]> + 'static,
ParseFn: Fn(&mut SslRef, ExtensionContext, &[u8], Option<(usize, &X509Ref)>) ParseFn: Fn(&mut SslRef, ExtensionContext, &[u8], Option<(usize, &X509Ref)>)
-> Result<(), SslAlert> + 'static + Sync + Send, -> Result<(), SslAlert> + 'static + Sync + Send,
{ {
@ -1534,8 +1534,8 @@ impl SslContextBuilder {
); );
ffi::SSL_CTX_add_custom_ext(self.as_ptr(), ext_type as c_uint, context.bits(), ffi::SSL_CTX_add_custom_ext(self.as_ptr(), ext_type as c_uint, context.bits(),
Some(raw_custom_ext_add::<AddFn>), Some(raw_custom_ext_add::<AddFn, T>),
Some(raw_custom_ext_free), Some(raw_custom_ext_free::<T>),
ptr::null_mut(), ptr::null_mut(),
Some(raw_custom_ext_parse::<ParseFn>), Some(raw_custom_ext_parse::<ParseFn>),
ptr::null_mut()) ptr::null_mut())

View File

@ -1370,7 +1370,7 @@ fn custom_extensions() {
.unwrap(); .unwrap();
ctx.add_custom_ext( ctx.add_custom_ext(
12345, ssl::ExtensionContext::CLIENT_HELLO, 12345, ssl::ExtensionContext::CLIENT_HELLO,
|_, _, _| unreachable!(), |_, _, _| -> Result<Option<&'static [u8]>, _> { unreachable!() },
|_, _, data, _| { FOUND_EXTENSION.store(data == b"hello", Ordering::SeqCst); Ok(()) } |_, _, data, _| { FOUND_EXTENSION.store(data == b"hello", Ordering::SeqCst); Ok(()) }
).unwrap(); ).unwrap();
let ssl = Ssl::new(&ctx.build()).unwrap(); let ssl = Ssl::new(&ctx.build()).unwrap();
@ -1381,7 +1381,7 @@ fn custom_extensions() {
let mut ctx = SslContext::builder(SslMethod::tls()).unwrap(); let mut ctx = SslContext::builder(SslMethod::tls()).unwrap();
ctx.add_custom_ext( ctx.add_custom_ext(
12345, ssl::ExtensionContext::CLIENT_HELLO, 12345, ssl::ExtensionContext::CLIENT_HELLO,
|_, _, _| Ok(Some(b"hello"[..].into())), |_, _, _| Ok(Some(b"hello")),
|_, _, _, _| unreachable!() |_, _, _, _| unreachable!()
).unwrap(); ).unwrap();
let ssl = Ssl::new(&ctx.build()).unwrap(); let ssl = Ssl::new(&ctx.build()).unwrap();