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