diff options
Diffstat (limited to 'openssl/src/ssl/callbacks.rs')
| -rw-r--r-- | openssl/src/ssl/callbacks.rs | 46 |
1 files changed, 43 insertions, 3 deletions
diff --git a/openssl/src/ssl/callbacks.rs b/openssl/src/ssl/callbacks.rs index b23c60e7..f45146c6 100644 --- a/openssl/src/ssl/callbacks.rs +++ b/openssl/src/ssl/callbacks.rs @@ -52,7 +52,7 @@ where } #[cfg(not(osslconf = "OPENSSL_NO_PSK"))] -pub extern "C" fn raw_psk<F>( +pub extern "C" fn raw_client_psk<F>( ssl: *mut ffi::SSL, hint: *const c_char, identity: *mut c_char, @@ -74,7 +74,7 @@ where .ssl_context() .ex_data(callback_idx) .expect("BUG: psk callback missing") as *const F; - let hint = if hint != ptr::null() { + let hint = if !hint.is_null() { Some(CStr::from_ptr(hint).to_bytes()) } else { None @@ -84,7 +84,47 @@ where let psk_sl = slice::from_raw_parts_mut(psk as *mut u8, max_psk_len as usize); match (*callback)(ssl, hint, identity_sl, psk_sl) { Ok(psk_len) => psk_len as u32, - _ => 0, + Err(e) => { + e.put(); + 0 + } + } + } +} + +#[cfg(not(osslconf = "OPENSSL_NO_PSK"))] +pub extern "C" fn raw_server_psk<F>( + ssl: *mut ffi::SSL, + identity: *const c_char, + psk: *mut c_uchar, + max_psk_len: c_uint, +) -> c_uint +where + F: Fn(&mut SslRef, Option<&[u8]>, &mut [u8]) -> Result<usize, ErrorStack> + + 'static + + Sync + + Send, +{ + unsafe { + let ssl = SslRef::from_ptr_mut(ssl); + let callback_idx = SslContext::cached_ex_index::<F>(); + + let callback = ssl.ssl_context() + .ex_data(callback_idx) + .expect("BUG: psk callback missing") as *const F; + let identity = if identity != ptr::null() { + Some(CStr::from_ptr(identity).to_bytes()) + } else { + None + }; + // Give the callback mutable slices into which it can write the psk. + let psk_sl = slice::from_raw_parts_mut(psk as *mut u8, max_psk_len as usize); + match (*callback)(ssl, identity, psk_sl) { + Ok(psk_len) => psk_len as u32, + Err(e) => { + e.put(); + 0 + } } } } |