1macro_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
37macro_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 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
76macro_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
89macro_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
105macro_rules! from_der {
106 ($(#[$m:meta])* $n:ident, $t:ty, $f:path, $len_ty:ty) => {
107 $(#[$m])*
108 pub fn $n(der: &[u8]) -> Result<$t, crate::error::ErrorStack> {
109 unsafe {
110 crate::ffi::init();
111 let len = ::std::cmp::min(der.len(), <$len_ty>::MAX as usize) as $len_ty;
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
119macro_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
133macro_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 )
143 => {
144 foreign_type! {
145 $(#[$impl_attr])*
146 $(#[$owned_attr])*
147 pub unsafe type $owned: Send + Sync {
148 type CType = $ctype;
149 fn drop = $drop;
150 $(fn clone = $clone;)*
151 }
152 }
153 };
154}
155
156macro_rules! generic_foreign_type_and_impl_send_sync {
157 (
158 $(#[$impl_attr:meta])*
159 type CType = $ctype:ty;
160 fn drop = $drop:expr;
161 $(fn clone = $clone:expr;)*
162
163 $(#[$owned_attr:meta])*
164 pub struct $owned:ident<T>;
165 $(#[$borrowed_attr:meta])*
166 pub struct $borrowed:ident<T>;
167 ) => {
168 $(#[$owned_attr])*
169 pub struct $owned<T>(*mut $ctype, ::std::marker::PhantomData<T>);
170
171 $(#[$impl_attr])*
172 unsafe impl<T> ::foreign_types::ForeignType for $owned<T> {
173 type CType = $ctype;
174 type Ref = $borrowed<T>;
175
176 #[inline]
177 unsafe fn from_ptr(ptr: *mut $ctype) -> $owned<T> {
178 $owned(ptr, ::std::marker::PhantomData)
179 }
180
181 #[inline]
182 fn as_ptr(&self) -> *mut $ctype {
183 self.0
184 }
185 }
186
187 impl<T> Drop for $owned<T> {
188 #[inline]
189 fn drop(&mut self) {
190 unsafe { $drop(self.0) }
191 }
192 }
193
194 $(
195 impl<T> Clone for $owned<T> {
196 #[inline]
197 fn clone(&self) -> $owned<T> {
198 unsafe {
199 let handle: *mut $ctype = $clone(self.0);
200 ::foreign_types::ForeignType::from_ptr(handle)
201 }
202 }
203 }
204
205 impl<T> ::std::borrow::ToOwned for $borrowed<T> {
206 type Owned = $owned<T>;
207 #[inline]
208 fn to_owned(&self) -> $owned<T> {
209 unsafe {
210 let handle: *mut $ctype =
211 $clone(::foreign_types::ForeignTypeRef::as_ptr(self));
212 $crate::ForeignType::from_ptr(handle)
213 }
214 }
215 }
216 )*
217
218 impl<T> ::std::ops::Deref for $owned<T> {
219 type Target = $borrowed<T>;
220
221 #[inline]
222 fn deref(&self) -> &$borrowed<T> {
223 unsafe { ::foreign_types::ForeignTypeRef::from_ptr(self.0) }
224 }
225 }
226
227 impl<T> ::std::ops::DerefMut for $owned<T> {
228 #[inline]
229 fn deref_mut(&mut self) -> &mut $borrowed<T> {
230 unsafe { ::foreign_types::ForeignTypeRef::from_ptr_mut(self.0) }
231 }
232 }
233
234 impl<T> ::std::borrow::Borrow<$borrowed<T>> for $owned<T> {
235 #[inline]
236 fn borrow(&self) -> &$borrowed<T> {
237 &**self
238 }
239 }
240
241 impl<T> ::std::convert::AsRef<$borrowed<T>> for $owned<T> {
242 #[inline]
243 fn as_ref(&self) -> &$borrowed<T> {
244 &**self
245 }
246 }
247
248 $(#[$borrowed_attr])*
249 pub struct $borrowed<T>(::foreign_types::Opaque, ::std::marker::PhantomData<T>);
250
251 $(#[$impl_attr])*
252 unsafe impl<T> ::foreign_types::ForeignTypeRef for $borrowed<T> {
253 type CType = $ctype;
254 }
255
256 unsafe impl<T> Send for $owned<T>{}
257 unsafe impl<T> Send for $borrowed<T>{}
258 unsafe impl<T> Sync for $owned<T>{}
259 unsafe impl<T> Sync for $borrowed<T>{}
260 };
261}