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_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
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) => {
107 $(#[$m])*
108 pub fn $n(der: &[u8]) -> Result<$t, crate::error::ErrorStack> {
109 use std::convert::TryInto;
110 unsafe {
111 ffi::init();
112 let len = ::std::cmp::min(der.len(), ::libc::c_long::max_value() as usize) as ::libc::c_long;
113 crate::cvt_p($f(::std::ptr::null_mut(), &mut der.as_ptr(), len.try_into().unwrap()))
114 .map(|p| ::foreign_types::ForeignType::from_ptr(p))
115 }
116 }
117 }
118}
119
120macro_rules! from_pem {
121 ($(#[$m:meta])* $n:ident, $t:ty, $f:path) => {
122 $(#[$m])*
123 pub fn $n(pem: &[u8]) -> Result<$t, crate::error::ErrorStack> {
124 unsafe {
125 crate::init();
126 let bio = crate::bio::MemBioSlice::new(pem)?;
127 cvt_p($f(bio.as_ptr(), ::std::ptr::null_mut(), None, ::std::ptr::null_mut()))
128 .map(|p| ::foreign_types::ForeignType::from_ptr(p))
129 }
130 }
131 }
132}
133
134macro_rules! foreign_type_and_impl_send_sync {
135 (
136 $(#[$impl_attr:meta])*
137 type CType = $ctype:ty;
138 fn drop = $drop:expr;
139 $(fn clone = $clone:expr;)*
140
141 $(#[$owned_attr:meta])*
142 pub struct $owned:ident;
143 $(#[$borrowed_attr:meta])*
144 pub struct $borrowed:ident;
145 )
146 => {
147 ::foreign_types::foreign_type! {
148 $(#[$impl_attr])*
149 type CType = $ctype;
150 fn drop = $drop;
151 $(fn clone = $clone;)*
152 $(#[$owned_attr])*
153 pub struct $owned;
154 $(#[$borrowed_attr])*
155 pub struct $borrowed;
156 }
157
158 unsafe impl Send for $owned{}
159 unsafe impl Send for $borrowed{}
160 unsafe impl Sync for $owned{}
161 unsafe impl Sync for $borrowed{}
162 };
163}
164
165macro_rules! generic_foreign_type_and_impl_send_sync {
166 (
167 $(#[$impl_attr:meta])*
168 type CType = $ctype:ty;
169 fn drop = $drop:expr;
170 $(fn clone = $clone:expr;)*
171
172 $(#[$owned_attr:meta])*
173 pub struct $owned:ident<T>;
174 $(#[$borrowed_attr:meta])*
175 pub struct $borrowed:ident<T>;
176 ) => {
177 $(#[$owned_attr])*
178 pub struct $owned<T>(*mut $ctype, ::std::marker::PhantomData<T>);
179
180 $(#[$impl_attr])*
181 impl<T> ::foreign_types::ForeignType for $owned<T> {
182 type CType = $ctype;
183 type Ref = $borrowed<T>;
184
185 #[inline]
186 unsafe fn from_ptr(ptr: *mut $ctype) -> $owned<T> {
187 $owned(ptr, ::std::marker::PhantomData)
188 }
189
190 #[inline]
191 fn as_ptr(&self) -> *mut $ctype {
192 self.0
193 }
194 }
195
196 impl<T> Drop for $owned<T> {
197 #[inline]
198 fn drop(&mut self) {
199 unsafe { $drop(self.0) }
200 }
201 }
202
203 $(
204 impl<T> Clone for $owned<T> {
205 #[inline]
206 fn clone(&self) -> $owned<T> {
207 unsafe {
208 let handle: *mut $ctype = $clone(self.0);
209 ::foreign_types::ForeignType::from_ptr(handle)
210 }
211 }
212 }
213
214 impl<T> ::std::borrow::ToOwned for $borrowed<T> {
215 type Owned = $owned<T>;
216 #[inline]
217 fn to_owned(&self) -> $owned<T> {
218 unsafe {
219 let handle: *mut $ctype =
220 $clone(::foreign_types::ForeignTypeRef::as_ptr(self));
221 $crate::ForeignType::from_ptr(handle)
222 }
223 }
224 }
225 )*
226
227 impl<T> ::std::ops::Deref for $owned<T> {
228 type Target = $borrowed<T>;
229
230 #[inline]
231 fn deref(&self) -> &$borrowed<T> {
232 unsafe { ::foreign_types::ForeignTypeRef::from_ptr(self.0) }
233 }
234 }
235
236 impl<T> ::std::ops::DerefMut for $owned<T> {
237 #[inline]
238 fn deref_mut(&mut self) -> &mut $borrowed<T> {
239 unsafe { ::foreign_types::ForeignTypeRef::from_ptr_mut(self.0) }
240 }
241 }
242
243 impl<T> ::std::borrow::Borrow<$borrowed<T>> for $owned<T> {
244 #[inline]
245 fn borrow(&self) -> &$borrowed<T> {
246 &**self
247 }
248 }
249
250 impl<T> ::std::convert::AsRef<$borrowed<T>> for $owned<T> {
251 #[inline]
252 fn as_ref(&self) -> &$borrowed<T> {
253 &**self
254 }
255 }
256
257 $(#[$borrowed_attr])*
258 pub struct $borrowed<T>(::foreign_types::Opaque, ::std::marker::PhantomData<T>);
259
260 $(#[$impl_attr])*
261 impl<T> ::foreign_types::ForeignTypeRef for $borrowed<T> {
262 type CType = $ctype;
263 }
264
265 unsafe impl<T> Send for $owned<T>{}
266 unsafe impl<T> Send for $borrowed<T>{}
267 unsafe impl<T> Sync for $owned<T>{}
268 unsafe impl<T> Sync for $borrowed<T>{}
269 };
270}
271