192f3ab15Sopenharmony_cimacro_rules! private_key_from_pem { 292f3ab15Sopenharmony_ci ($(#[$m:meta])* $n:ident, $(#[$m2:meta])* $n2:ident, $(#[$m3:meta])* $n3:ident, $t:ty, $f:path) => { 392f3ab15Sopenharmony_ci from_pem!($(#[$m])* $n, $t, $f); 492f3ab15Sopenharmony_ci 592f3ab15Sopenharmony_ci $(#[$m2])* 692f3ab15Sopenharmony_ci pub fn $n2(pem: &[u8], passphrase: &[u8]) -> Result<$t, crate::error::ErrorStack> { 792f3ab15Sopenharmony_ci unsafe { 892f3ab15Sopenharmony_ci ffi::init(); 992f3ab15Sopenharmony_ci let bio = crate::bio::MemBioSlice::new(pem)?; 1092f3ab15Sopenharmony_ci let passphrase = ::std::ffi::CString::new(passphrase).unwrap(); 1192f3ab15Sopenharmony_ci cvt_p($f(bio.as_ptr(), 1292f3ab15Sopenharmony_ci ptr::null_mut(), 1392f3ab15Sopenharmony_ci None, 1492f3ab15Sopenharmony_ci passphrase.as_ptr() as *const _ as *mut _)) 1592f3ab15Sopenharmony_ci .map(|p| ::foreign_types::ForeignType::from_ptr(p)) 1692f3ab15Sopenharmony_ci } 1792f3ab15Sopenharmony_ci } 1892f3ab15Sopenharmony_ci 1992f3ab15Sopenharmony_ci $(#[$m3])* 2092f3ab15Sopenharmony_ci pub fn $n3<F>(pem: &[u8], callback: F) -> Result<$t, crate::error::ErrorStack> 2192f3ab15Sopenharmony_ci where F: FnOnce(&mut [u8]) -> Result<usize, crate::error::ErrorStack> 2292f3ab15Sopenharmony_ci { 2392f3ab15Sopenharmony_ci unsafe { 2492f3ab15Sopenharmony_ci ffi::init(); 2592f3ab15Sopenharmony_ci let mut cb = crate::util::CallbackState::new(callback); 2692f3ab15Sopenharmony_ci let bio = crate::bio::MemBioSlice::new(pem)?; 2792f3ab15Sopenharmony_ci cvt_p($f(bio.as_ptr(), 2892f3ab15Sopenharmony_ci ptr::null_mut(), 2992f3ab15Sopenharmony_ci Some(crate::util::invoke_passwd_cb::<F>), 3092f3ab15Sopenharmony_ci &mut cb as *mut _ as *mut _)) 3192f3ab15Sopenharmony_ci .map(|p| ::foreign_types::ForeignType::from_ptr(p)) 3292f3ab15Sopenharmony_ci } 3392f3ab15Sopenharmony_ci } 3492f3ab15Sopenharmony_ci } 3592f3ab15Sopenharmony_ci} 3692f3ab15Sopenharmony_ci 3792f3ab15Sopenharmony_cimacro_rules! private_key_to_pem { 3892f3ab15Sopenharmony_ci ($(#[$m:meta])* $n:ident, $(#[$m2:meta])* $n2:ident, $f:path) => { 3992f3ab15Sopenharmony_ci $(#[$m])* 4092f3ab15Sopenharmony_ci pub fn $n(&self) -> Result<Vec<u8>, crate::error::ErrorStack> { 4192f3ab15Sopenharmony_ci unsafe { 4292f3ab15Sopenharmony_ci let bio = crate::bio::MemBio::new()?; 4392f3ab15Sopenharmony_ci cvt($f(bio.as_ptr(), 4492f3ab15Sopenharmony_ci self.as_ptr(), 4592f3ab15Sopenharmony_ci ptr::null(), 4692f3ab15Sopenharmony_ci ptr::null_mut(), 4792f3ab15Sopenharmony_ci -1, 4892f3ab15Sopenharmony_ci None, 4992f3ab15Sopenharmony_ci ptr::null_mut()))?; 5092f3ab15Sopenharmony_ci Ok(bio.get_buf().to_owned()) 5192f3ab15Sopenharmony_ci } 5292f3ab15Sopenharmony_ci } 5392f3ab15Sopenharmony_ci 5492f3ab15Sopenharmony_ci $(#[$m2])* 5592f3ab15Sopenharmony_ci pub fn $n2( 5692f3ab15Sopenharmony_ci &self, 5792f3ab15Sopenharmony_ci cipher: crate::symm::Cipher, 5892f3ab15Sopenharmony_ci passphrase: &[u8] 5992f3ab15Sopenharmony_ci ) -> Result<Vec<u8>, crate::error::ErrorStack> { 6092f3ab15Sopenharmony_ci unsafe { 6192f3ab15Sopenharmony_ci let bio = crate::bio::MemBio::new()?; 6292f3ab15Sopenharmony_ci assert!(passphrase.len() <= ::libc::c_int::max_value() as usize); 6392f3ab15Sopenharmony_ci cvt($f(bio.as_ptr(), 6492f3ab15Sopenharmony_ci self.as_ptr(), 6592f3ab15Sopenharmony_ci cipher.as_ptr(), 6692f3ab15Sopenharmony_ci passphrase.as_ptr() as *const _ as *mut _, 6792f3ab15Sopenharmony_ci passphrase.len() as ::libc::c_int, 6892f3ab15Sopenharmony_ci None, 6992f3ab15Sopenharmony_ci ptr::null_mut()))?; 7092f3ab15Sopenharmony_ci Ok(bio.get_buf().to_owned()) 7192f3ab15Sopenharmony_ci } 7292f3ab15Sopenharmony_ci } 7392f3ab15Sopenharmony_ci } 7492f3ab15Sopenharmony_ci} 7592f3ab15Sopenharmony_ci 7692f3ab15Sopenharmony_cimacro_rules! to_pem { 7792f3ab15Sopenharmony_ci ($(#[$m:meta])* $n:ident, $f:path) => { 7892f3ab15Sopenharmony_ci $(#[$m])* 7992f3ab15Sopenharmony_ci pub fn $n(&self) -> Result<Vec<u8>, crate::error::ErrorStack> { 8092f3ab15Sopenharmony_ci unsafe { 8192f3ab15Sopenharmony_ci let bio = crate::bio::MemBio::new()?; 8292f3ab15Sopenharmony_ci cvt($f(bio.as_ptr(), self.as_ptr()))?; 8392f3ab15Sopenharmony_ci Ok(bio.get_buf().to_owned()) 8492f3ab15Sopenharmony_ci } 8592f3ab15Sopenharmony_ci } 8692f3ab15Sopenharmony_ci } 8792f3ab15Sopenharmony_ci} 8892f3ab15Sopenharmony_ci 8992f3ab15Sopenharmony_cimacro_rules! to_der { 9092f3ab15Sopenharmony_ci ($(#[$m:meta])* $n:ident, $f:path) => { 9192f3ab15Sopenharmony_ci $(#[$m])* 9292f3ab15Sopenharmony_ci pub fn $n(&self) -> Result<Vec<u8>, crate::error::ErrorStack> { 9392f3ab15Sopenharmony_ci unsafe { 9492f3ab15Sopenharmony_ci let len = crate::cvt($f(::foreign_types::ForeignTypeRef::as_ptr(self), 9592f3ab15Sopenharmony_ci ptr::null_mut()))?; 9692f3ab15Sopenharmony_ci let mut buf = vec![0; len as usize]; 9792f3ab15Sopenharmony_ci crate::cvt($f(::foreign_types::ForeignTypeRef::as_ptr(self), 9892f3ab15Sopenharmony_ci &mut buf.as_mut_ptr()))?; 9992f3ab15Sopenharmony_ci Ok(buf) 10092f3ab15Sopenharmony_ci } 10192f3ab15Sopenharmony_ci } 10292f3ab15Sopenharmony_ci }; 10392f3ab15Sopenharmony_ci} 10492f3ab15Sopenharmony_ci 10592f3ab15Sopenharmony_cimacro_rules! from_der { 10692f3ab15Sopenharmony_ci ($(#[$m:meta])* $n:ident, $t:ty, $f:path) => { 10792f3ab15Sopenharmony_ci $(#[$m])* 10892f3ab15Sopenharmony_ci pub fn $n(der: &[u8]) -> Result<$t, crate::error::ErrorStack> { 10992f3ab15Sopenharmony_ci use std::convert::TryInto; 11092f3ab15Sopenharmony_ci unsafe { 11192f3ab15Sopenharmony_ci ffi::init(); 11292f3ab15Sopenharmony_ci let len = ::std::cmp::min(der.len(), ::libc::c_long::max_value() as usize) as ::libc::c_long; 11392f3ab15Sopenharmony_ci crate::cvt_p($f(::std::ptr::null_mut(), &mut der.as_ptr(), len.try_into().unwrap())) 11492f3ab15Sopenharmony_ci .map(|p| ::foreign_types::ForeignType::from_ptr(p)) 11592f3ab15Sopenharmony_ci } 11692f3ab15Sopenharmony_ci } 11792f3ab15Sopenharmony_ci } 11892f3ab15Sopenharmony_ci} 11992f3ab15Sopenharmony_ci 12092f3ab15Sopenharmony_cimacro_rules! from_pem { 12192f3ab15Sopenharmony_ci ($(#[$m:meta])* $n:ident, $t:ty, $f:path) => { 12292f3ab15Sopenharmony_ci $(#[$m])* 12392f3ab15Sopenharmony_ci pub fn $n(pem: &[u8]) -> Result<$t, crate::error::ErrorStack> { 12492f3ab15Sopenharmony_ci unsafe { 12592f3ab15Sopenharmony_ci crate::init(); 12692f3ab15Sopenharmony_ci let bio = crate::bio::MemBioSlice::new(pem)?; 12792f3ab15Sopenharmony_ci cvt_p($f(bio.as_ptr(), ::std::ptr::null_mut(), None, ::std::ptr::null_mut())) 12892f3ab15Sopenharmony_ci .map(|p| ::foreign_types::ForeignType::from_ptr(p)) 12992f3ab15Sopenharmony_ci } 13092f3ab15Sopenharmony_ci } 13192f3ab15Sopenharmony_ci } 13292f3ab15Sopenharmony_ci} 13392f3ab15Sopenharmony_ci 13492f3ab15Sopenharmony_cimacro_rules! foreign_type_and_impl_send_sync { 13592f3ab15Sopenharmony_ci ( 13692f3ab15Sopenharmony_ci $(#[$impl_attr:meta])* 13792f3ab15Sopenharmony_ci type CType = $ctype:ty; 13892f3ab15Sopenharmony_ci fn drop = $drop:expr; 13992f3ab15Sopenharmony_ci $(fn clone = $clone:expr;)* 14092f3ab15Sopenharmony_ci 14192f3ab15Sopenharmony_ci $(#[$owned_attr:meta])* 14292f3ab15Sopenharmony_ci pub struct $owned:ident; 14392f3ab15Sopenharmony_ci $(#[$borrowed_attr:meta])* 14492f3ab15Sopenharmony_ci pub struct $borrowed:ident; 14592f3ab15Sopenharmony_ci ) 14692f3ab15Sopenharmony_ci => { 14792f3ab15Sopenharmony_ci ::foreign_types::foreign_type! { 14892f3ab15Sopenharmony_ci $(#[$impl_attr])* 14992f3ab15Sopenharmony_ci type CType = $ctype; 15092f3ab15Sopenharmony_ci fn drop = $drop; 15192f3ab15Sopenharmony_ci $(fn clone = $clone;)* 15292f3ab15Sopenharmony_ci $(#[$owned_attr])* 15392f3ab15Sopenharmony_ci pub struct $owned; 15492f3ab15Sopenharmony_ci $(#[$borrowed_attr])* 15592f3ab15Sopenharmony_ci pub struct $borrowed; 15692f3ab15Sopenharmony_ci } 15792f3ab15Sopenharmony_ci 15892f3ab15Sopenharmony_ci unsafe impl Send for $owned{} 15992f3ab15Sopenharmony_ci unsafe impl Send for $borrowed{} 16092f3ab15Sopenharmony_ci unsafe impl Sync for $owned{} 16192f3ab15Sopenharmony_ci unsafe impl Sync for $borrowed{} 16292f3ab15Sopenharmony_ci }; 16392f3ab15Sopenharmony_ci} 16492f3ab15Sopenharmony_ci 16592f3ab15Sopenharmony_cimacro_rules! generic_foreign_type_and_impl_send_sync { 16692f3ab15Sopenharmony_ci ( 16792f3ab15Sopenharmony_ci $(#[$impl_attr:meta])* 16892f3ab15Sopenharmony_ci type CType = $ctype:ty; 16992f3ab15Sopenharmony_ci fn drop = $drop:expr; 17092f3ab15Sopenharmony_ci $(fn clone = $clone:expr;)* 17192f3ab15Sopenharmony_ci 17292f3ab15Sopenharmony_ci $(#[$owned_attr:meta])* 17392f3ab15Sopenharmony_ci pub struct $owned:ident<T>; 17492f3ab15Sopenharmony_ci $(#[$borrowed_attr:meta])* 17592f3ab15Sopenharmony_ci pub struct $borrowed:ident<T>; 17692f3ab15Sopenharmony_ci ) => { 17792f3ab15Sopenharmony_ci $(#[$owned_attr])* 17892f3ab15Sopenharmony_ci pub struct $owned<T>(*mut $ctype, ::std::marker::PhantomData<T>); 17992f3ab15Sopenharmony_ci 18092f3ab15Sopenharmony_ci $(#[$impl_attr])* 18192f3ab15Sopenharmony_ci impl<T> ::foreign_types::ForeignType for $owned<T> { 18292f3ab15Sopenharmony_ci type CType = $ctype; 18392f3ab15Sopenharmony_ci type Ref = $borrowed<T>; 18492f3ab15Sopenharmony_ci 18592f3ab15Sopenharmony_ci #[inline] 18692f3ab15Sopenharmony_ci unsafe fn from_ptr(ptr: *mut $ctype) -> $owned<T> { 18792f3ab15Sopenharmony_ci $owned(ptr, ::std::marker::PhantomData) 18892f3ab15Sopenharmony_ci } 18992f3ab15Sopenharmony_ci 19092f3ab15Sopenharmony_ci #[inline] 19192f3ab15Sopenharmony_ci fn as_ptr(&self) -> *mut $ctype { 19292f3ab15Sopenharmony_ci self.0 19392f3ab15Sopenharmony_ci } 19492f3ab15Sopenharmony_ci } 19592f3ab15Sopenharmony_ci 19692f3ab15Sopenharmony_ci impl<T> Drop for $owned<T> { 19792f3ab15Sopenharmony_ci #[inline] 19892f3ab15Sopenharmony_ci fn drop(&mut self) { 19992f3ab15Sopenharmony_ci unsafe { $drop(self.0) } 20092f3ab15Sopenharmony_ci } 20192f3ab15Sopenharmony_ci } 20292f3ab15Sopenharmony_ci 20392f3ab15Sopenharmony_ci $( 20492f3ab15Sopenharmony_ci impl<T> Clone for $owned<T> { 20592f3ab15Sopenharmony_ci #[inline] 20692f3ab15Sopenharmony_ci fn clone(&self) -> $owned<T> { 20792f3ab15Sopenharmony_ci unsafe { 20892f3ab15Sopenharmony_ci let handle: *mut $ctype = $clone(self.0); 20992f3ab15Sopenharmony_ci ::foreign_types::ForeignType::from_ptr(handle) 21092f3ab15Sopenharmony_ci } 21192f3ab15Sopenharmony_ci } 21292f3ab15Sopenharmony_ci } 21392f3ab15Sopenharmony_ci 21492f3ab15Sopenharmony_ci impl<T> ::std::borrow::ToOwned for $borrowed<T> { 21592f3ab15Sopenharmony_ci type Owned = $owned<T>; 21692f3ab15Sopenharmony_ci #[inline] 21792f3ab15Sopenharmony_ci fn to_owned(&self) -> $owned<T> { 21892f3ab15Sopenharmony_ci unsafe { 21992f3ab15Sopenharmony_ci let handle: *mut $ctype = 22092f3ab15Sopenharmony_ci $clone(::foreign_types::ForeignTypeRef::as_ptr(self)); 22192f3ab15Sopenharmony_ci $crate::ForeignType::from_ptr(handle) 22292f3ab15Sopenharmony_ci } 22392f3ab15Sopenharmony_ci } 22492f3ab15Sopenharmony_ci } 22592f3ab15Sopenharmony_ci )* 22692f3ab15Sopenharmony_ci 22792f3ab15Sopenharmony_ci impl<T> ::std::ops::Deref for $owned<T> { 22892f3ab15Sopenharmony_ci type Target = $borrowed<T>; 22992f3ab15Sopenharmony_ci 23092f3ab15Sopenharmony_ci #[inline] 23192f3ab15Sopenharmony_ci fn deref(&self) -> &$borrowed<T> { 23292f3ab15Sopenharmony_ci unsafe { ::foreign_types::ForeignTypeRef::from_ptr(self.0) } 23392f3ab15Sopenharmony_ci } 23492f3ab15Sopenharmony_ci } 23592f3ab15Sopenharmony_ci 23692f3ab15Sopenharmony_ci impl<T> ::std::ops::DerefMut for $owned<T> { 23792f3ab15Sopenharmony_ci #[inline] 23892f3ab15Sopenharmony_ci fn deref_mut(&mut self) -> &mut $borrowed<T> { 23992f3ab15Sopenharmony_ci unsafe { ::foreign_types::ForeignTypeRef::from_ptr_mut(self.0) } 24092f3ab15Sopenharmony_ci } 24192f3ab15Sopenharmony_ci } 24292f3ab15Sopenharmony_ci 24392f3ab15Sopenharmony_ci impl<T> ::std::borrow::Borrow<$borrowed<T>> for $owned<T> { 24492f3ab15Sopenharmony_ci #[inline] 24592f3ab15Sopenharmony_ci fn borrow(&self) -> &$borrowed<T> { 24692f3ab15Sopenharmony_ci &**self 24792f3ab15Sopenharmony_ci } 24892f3ab15Sopenharmony_ci } 24992f3ab15Sopenharmony_ci 25092f3ab15Sopenharmony_ci impl<T> ::std::convert::AsRef<$borrowed<T>> for $owned<T> { 25192f3ab15Sopenharmony_ci #[inline] 25292f3ab15Sopenharmony_ci fn as_ref(&self) -> &$borrowed<T> { 25392f3ab15Sopenharmony_ci &**self 25492f3ab15Sopenharmony_ci } 25592f3ab15Sopenharmony_ci } 25692f3ab15Sopenharmony_ci 25792f3ab15Sopenharmony_ci $(#[$borrowed_attr])* 25892f3ab15Sopenharmony_ci pub struct $borrowed<T>(::foreign_types::Opaque, ::std::marker::PhantomData<T>); 25992f3ab15Sopenharmony_ci 26092f3ab15Sopenharmony_ci $(#[$impl_attr])* 26192f3ab15Sopenharmony_ci impl<T> ::foreign_types::ForeignTypeRef for $borrowed<T> { 26292f3ab15Sopenharmony_ci type CType = $ctype; 26392f3ab15Sopenharmony_ci } 26492f3ab15Sopenharmony_ci 26592f3ab15Sopenharmony_ci unsafe impl<T> Send for $owned<T>{} 26692f3ab15Sopenharmony_ci unsafe impl<T> Send for $borrowed<T>{} 26792f3ab15Sopenharmony_ci unsafe impl<T> Sync for $owned<T>{} 26892f3ab15Sopenharmony_ci unsafe impl<T> Sync for $borrowed<T>{} 26992f3ab15Sopenharmony_ci }; 27092f3ab15Sopenharmony_ci} 271