1 macro_rules! private_key_from_pem {
2     ($(#[$m:meta])* $n:ident, $(#[$m2:meta])* $n2:ident, $(#[$m3:meta])* $n3:ident, $t:ty, $f:path) => {
3         from_pem!($(#[$m])* $n, $t, $f);
4 
5         $(#[$m2])*
6         pub fn $n2(pem: &[u8], passphrase: &[u8]) -> Result<$t, crate::error::ErrorStack> {
7             unsafe {
8                 ffi::init();
9                 let bio = crate::bio::MemBioSlice::new(pem)?;
10                 let passphrase = ::std::ffi::CString::new(passphrase).unwrap();
11                 cvt_p($f(bio.as_ptr(),
12                          ptr::null_mut(),
13                          None,
14                          passphrase.as_ptr() as *const _ as *mut _))
15                     .map(|p| ::foreign_types::ForeignType::from_ptr(p))
16             }
17         }
18 
19         $(#[$m3])*
20         pub fn $n3<F>(pem: &[u8], callback: F) -> Result<$t, crate::error::ErrorStack>
21             where F: FnOnce(&mut [u8]) -> Result<usize, crate::error::ErrorStack>
22         {
23             unsafe {
24                 ffi::init();
25                 let mut cb = crate::util::CallbackState::new(callback);
26                 let bio = crate::bio::MemBioSlice::new(pem)?;
27                 cvt_p($f(bio.as_ptr(),
28                          ptr::null_mut(),
29                          Some(crate::util::invoke_passwd_cb::<F>),
30                          &mut cb as *mut _ as *mut _))
31                     .map(|p| ::foreign_types::ForeignType::from_ptr(p))
32             }
33         }
34     }
35 }
36 
37 macro_rules! private_key_to_pem {
38     ($(#[$m:meta])* $n:ident, $(#[$m2:meta])* $n2:ident, $f:path) => {
39         $(#[$m])*
40         pub fn $n(&self) -> Result<Vec<u8>, crate::error::ErrorStack> {
41             unsafe {
42                 let bio = crate::bio::MemBio::new()?;
43                 cvt($f(bio.as_ptr(),
44                         self.as_ptr(),
45                         ptr::null(),
46                         ptr::null_mut(),
47                         -1,
48                         None,
49                         ptr::null_mut()))?;
50                 Ok(bio.get_buf().to_owned())
51             }
52         }
53 
54         $(#[$m2])*
55         pub fn $n2(
56             &self,
57             cipher: crate::symm::Cipher,
58             passphrase: &[u8]
59         ) -> Result<Vec<u8>, crate::error::ErrorStack> {
60             unsafe {
61                 let bio = crate::bio::MemBio::new()?;
62                 assert!(passphrase.len() <= ::libc::c_int::max_value() as usize);
63                 cvt($f(bio.as_ptr(),
64                         self.as_ptr(),
65                         cipher.as_ptr(),
66                         passphrase.as_ptr() as *const _ as *mut _,
67                         passphrase.len() as ::libc::c_int,
68                         None,
69                         ptr::null_mut()))?;
70                 Ok(bio.get_buf().to_owned())
71             }
72         }
73     }
74 }
75 
76 macro_rules! to_pem {
77     ($(#[$m:meta])* $n:ident, $f:path) => {
78         $(#[$m])*
79         pub fn $n(&self) -> Result<Vec<u8>, crate::error::ErrorStack> {
80             unsafe {
81                 let bio = crate::bio::MemBio::new()?;
82                 cvt($f(bio.as_ptr(), self.as_ptr()))?;
83                 Ok(bio.get_buf().to_owned())
84             }
85         }
86     }
87 }
88 
89 macro_rules! to_der {
90     ($(#[$m:meta])* $n:ident, $f:path) => {
91         $(#[$m])*
92         pub fn $n(&self) -> Result<Vec<u8>, crate::error::ErrorStack> {
93             unsafe {
94                 let len = crate::cvt($f(::foreign_types::ForeignTypeRef::as_ptr(self),
95                                         ptr::null_mut()))?;
96                 let mut buf = vec![0; len as usize];
97                 crate::cvt($f(::foreign_types::ForeignTypeRef::as_ptr(self),
98                               &mut buf.as_mut_ptr()))?;
99                 Ok(buf)
100             }
101         }
102     };
103 }
104 
105 macro_rules! from_der {
106     ($(#[$m:meta])* $n:ident, $t:ty, $f:path) => {
107         $(#[$m])*
108         pub fn $n(der: &[u8]) -> Result<$t, crate::error::ErrorStack> {
109             unsafe {
110                 ffi::init();
111                 let len = ::std::cmp::min(der.len(), ::libc::c_long::max_value() as usize) as ::libc::c_long;
112                 crate::cvt_p($f(::std::ptr::null_mut(), &mut der.as_ptr(), len))
113                     .map(|p| ::foreign_types::ForeignType::from_ptr(p))
114             }
115         }
116     }
117 }
118 
119 macro_rules! from_pem {
120     ($(#[$m:meta])* $n:ident, $t:ty, $f:path) => {
121         $(#[$m])*
122         pub fn $n(pem: &[u8]) -> Result<$t, crate::error::ErrorStack> {
123             unsafe {
124                 crate::init();
125                 let bio = crate::bio::MemBioSlice::new(pem)?;
126                 cvt_p($f(bio.as_ptr(), ::std::ptr::null_mut(), None, ::std::ptr::null_mut()))
127                     .map(|p| ::foreign_types::ForeignType::from_ptr(p))
128             }
129         }
130     }
131 }
132 
133 macro_rules! foreign_type_and_impl_send_sync {
134     (
135         $(#[$impl_attr:meta])*
136         type CType = $ctype:ty;
137         fn drop = $drop:expr;
138         $(fn clone = $clone:expr;)*
139 
140         $(#[$owned_attr:meta])*
141         pub struct $owned:ident;
142         $(#[$borrowed_attr:meta])*
143         pub struct $borrowed:ident;
144     )
145         => {
146             ::foreign_types::foreign_type! {
147                 $(#[$impl_attr])*
148                 type CType = $ctype;
149                 fn drop = $drop;
150                 $(fn clone = $clone;)*
151                 $(#[$owned_attr])*
152                 pub struct $owned;
153                 $(#[$borrowed_attr])*
154                 pub struct $borrowed;
155             }
156 
157             unsafe impl Send for $owned{}
158             unsafe impl Send for $borrowed{}
159             unsafe impl Sync for $owned{}
160             unsafe impl Sync for $borrowed{}
161         };
162 }
163 
164 macro_rules! generic_foreign_type_and_impl_send_sync {
165     (
166         $(#[$impl_attr:meta])*
167         type CType = $ctype:ty;
168         fn drop = $drop:expr;
169         $(fn clone = $clone:expr;)*
170 
171         $(#[$owned_attr:meta])*
172         pub struct $owned:ident<T>;
173         $(#[$borrowed_attr:meta])*
174         pub struct $borrowed:ident<T>;
175     ) => {
176         $(#[$owned_attr])*
177         pub struct $owned<T>(*mut $ctype, ::std::marker::PhantomData<T>);
178 
179         $(#[$impl_attr])*
180         impl<T> ::foreign_types::ForeignType for $owned<T> {
181             type CType = $ctype;
182             type Ref = $borrowed<T>;
183 
184             #[inline]
185             unsafe fn from_ptr(ptr: *mut $ctype) -> $owned<T> {
186                 $owned(ptr, ::std::marker::PhantomData)
187             }
188 
189             #[inline]
190             fn as_ptr(&self) -> *mut $ctype {
191                 self.0
192             }
193         }
194 
195         impl<T> Drop for $owned<T> {
196             #[inline]
197             fn drop(&mut self) {
198                 unsafe { $drop(self.0) }
199             }
200         }
201 
202         $(
203             impl<T> Clone for $owned<T> {
204                 #[inline]
205                 fn clone(&self) -> $owned<T> {
206                     unsafe {
207                         let handle: *mut $ctype = $clone(self.0);
208                         ::foreign_types::ForeignType::from_ptr(handle)
209                     }
210                 }
211             }
212 
213             impl<T> ::std::borrow::ToOwned for $borrowed<T> {
214                 type Owned = $owned<T>;
215                 #[inline]
216                 fn to_owned(&self) -> $owned<T> {
217                     unsafe {
218                         let handle: *mut $ctype =
219                             $clone(::foreign_types::ForeignTypeRef::as_ptr(self));
220                         $crate::ForeignType::from_ptr(handle)
221                     }
222                 }
223             }
224         )*
225 
226         impl<T> ::std::ops::Deref for $owned<T> {
227             type Target = $borrowed<T>;
228 
229             #[inline]
230             fn deref(&self) -> &$borrowed<T> {
231                 unsafe { ::foreign_types::ForeignTypeRef::from_ptr(self.0) }
232             }
233         }
234 
235         impl<T> ::std::ops::DerefMut for $owned<T> {
236             #[inline]
237             fn deref_mut(&mut self) -> &mut $borrowed<T> {
238                 unsafe { ::foreign_types::ForeignTypeRef::from_ptr_mut(self.0) }
239             }
240         }
241 
242         impl<T> ::std::borrow::Borrow<$borrowed<T>> for $owned<T> {
243             #[inline]
244             fn borrow(&self) -> &$borrowed<T> {
245                 &**self
246             }
247         }
248 
249         impl<T> ::std::convert::AsRef<$borrowed<T>> for $owned<T> {
250             #[inline]
251             fn as_ref(&self) -> &$borrowed<T> {
252                 &**self
253             }
254         }
255 
256         $(#[$borrowed_attr])*
257         pub struct $borrowed<T>(::foreign_types::Opaque, ::std::marker::PhantomData<T>);
258 
259         $(#[$impl_attr])*
260         impl<T> ::foreign_types::ForeignTypeRef for $borrowed<T> {
261             type CType = $ctype;
262         }
263 
264         unsafe impl<T> Send for $owned<T>{}
265         unsafe impl<T> Send for $borrowed<T>{}
266         unsafe impl<T> Sync for $owned<T>{}
267         unsafe impl<T> Sync for $borrowed<T>{}
268     };
269 }
270