1 use foreign_types::{ForeignType, ForeignTypeRef};
2 use libc::{c_char, c_int, c_void};
3 use std::any::Any;
4 use std::panic::{self, AssertUnwindSafe};
5 use std::slice;
6 
7 use crate::error::ErrorStack;
8 
9 /// Wraps a user-supplied callback and a slot for panics thrown inside the callback (while FFI
10 /// frames are on the stack).
11 ///
12 /// When dropped, checks if the callback has panicked, and resumes unwinding if so.
13 pub struct CallbackState<F> {
14     /// The user callback. Taken out of the `Option` when called.
15     cb: Option<F>,
16     /// If the callback panics, we place the panic object here, to be re-thrown once OpenSSL
17     /// returns.
18     panic: Option<Box<dyn Any + Send + 'static>>,
19 }
20 
21 impl<F> CallbackState<F> {
new(callback: F) -> Self22     pub fn new(callback: F) -> Self {
23         CallbackState {
24             cb: Some(callback),
25             panic: None,
26         }
27     }
28 }
29 
30 impl<F> Drop for CallbackState<F> {
drop(&mut self)31     fn drop(&mut self) {
32         if let Some(panic) = self.panic.take() {
33             panic::resume_unwind(panic);
34         }
35     }
36 }
37 
38 /// Password callback function, passed to private key loading functions.
39 ///
40 /// `cb_state` is expected to be a pointer to a `CallbackState`.
invoke_passwd_cb<F>( buf: *mut c_char, size: c_int, _rwflag: c_int, cb_state: *mut c_void, ) -> c_int where F: FnOnce(&mut [u8]) -> Result<usize, ErrorStack>,41 pub unsafe extern "C" fn invoke_passwd_cb<F>(
42     buf: *mut c_char,
43     size: c_int,
44     _rwflag: c_int,
45     cb_state: *mut c_void,
46 ) -> c_int
47 where
48     F: FnOnce(&mut [u8]) -> Result<usize, ErrorStack>,
49 {
50     let callback = &mut *(cb_state as *mut CallbackState<F>);
51 
52     let result = panic::catch_unwind(AssertUnwindSafe(|| {
53         let pass_slice = slice::from_raw_parts_mut(buf as *mut u8, size as usize);
54         callback.cb.take().unwrap()(pass_slice)
55     }));
56 
57     match result {
58         Ok(Ok(len)) => len as c_int,
59         Ok(Err(_)) => {
60             // FIXME restore error stack
61             0
62         }
63         Err(err) => {
64             callback.panic = Some(err);
65             0
66         }
67     }
68 }
69 
70 pub trait ForeignTypeExt: ForeignType {
from_ptr_opt(ptr: *mut Self::CType) -> Option<Self>71     unsafe fn from_ptr_opt(ptr: *mut Self::CType) -> Option<Self> {
72         if ptr.is_null() {
73             None
74         } else {
75             Some(Self::from_ptr(ptr))
76         }
77     }
78 }
79 impl<FT: ForeignType> ForeignTypeExt for FT {}
80 
81 pub trait ForeignTypeRefExt: ForeignTypeRef {
from_const_ptr<'a>(ptr: *const Self::CType) -> &'a Self82     unsafe fn from_const_ptr<'a>(ptr: *const Self::CType) -> &'a Self {
83         Self::from_ptr(ptr as *mut Self::CType)
84     }
85 
from_const_ptr_opt<'a>(ptr: *const Self::CType) -> Option<&'a Self>86     unsafe fn from_const_ptr_opt<'a>(ptr: *const Self::CType) -> Option<&'a Self> {
87         if ptr.is_null() {
88             None
89         } else {
90             Some(Self::from_const_ptr(ptr as *mut Self::CType))
91         }
92     }
93 }
94 impl<FT: ForeignTypeRef> ForeignTypeRefExt for FT {}
95