variant_ssl/
macros.rs

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) => {
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 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! digest {
135    ($(#[$m:meta])* $n:ident, $f:path) => {
136        $(#[$m])*
137        pub fn $n(&self, hash_type: crate::hash::MessageDigest)
138            -> Result<crate::hash::DigestBytes, crate::error::ErrorStack> {
139            unsafe {
140                let mut digest = DigestBytes {
141                    buf: [0; ffi::EVP_MAX_MD_SIZE as usize],
142                    len: ffi::EVP_MAX_MD_SIZE as usize,
143                };
144                let mut len = ffi::EVP_MAX_MD_SIZE as c_uint;
145                crate::cvt($f(
146                    ::foreign_types::ForeignTypeRef::as_ptr(self),
147                    hash_type.as_ptr(),
148                    digest.buf.as_mut_ptr() as *mut _,
149                    &mut len,
150                ))?;
151                digest.len = len as usize;
152
153                Ok(digest)
154            }
155        }
156    };
157}
158
159macro_rules! foreign_type_and_impl_send_sync {
160    (
161        $(#[$impl_attr:meta])*
162        type CType = $ctype:ty;
163        fn drop = $drop:expr;
164        $(fn clone = $clone:expr;)*
165
166        $(#[$owned_attr:meta])*
167        pub struct $owned:ident;
168        $(#[$borrowed_attr:meta])*
169        pub struct $borrowed:ident;
170    )
171        => {
172            ::foreign_types::foreign_type! {
173                $(#[$impl_attr])*
174                type CType = $ctype;
175                fn drop = $drop;
176                $(fn clone = $clone;)*
177                $(#[$owned_attr])*
178                pub struct $owned;
179                $(#[$borrowed_attr])*
180                pub struct $borrowed;
181            }
182
183            unsafe impl Send for $owned{}
184            unsafe impl Send for $borrowed{}
185            unsafe impl Sync for $owned{}
186            unsafe impl Sync for $borrowed{}
187        };
188}
189
190macro_rules! generic_foreign_type_and_impl_send_sync {
191    (
192        $(#[$impl_attr:meta])*
193        type CType = $ctype:ty;
194        fn drop = $drop:expr;
195        $(fn clone = $clone:expr;)*
196
197        $(#[$owned_attr:meta])*
198        pub struct $owned:ident<T>;
199        $(#[$borrowed_attr:meta])*
200        pub struct $borrowed:ident<T>;
201    ) => {
202        $(#[$owned_attr])*
203        pub struct $owned<T>(*mut $ctype, ::std::marker::PhantomData<T>);
204
205        $(#[$impl_attr])*
206        impl<T> ::foreign_types::ForeignType for $owned<T> {
207            type CType = $ctype;
208            type Ref = $borrowed<T>;
209
210            #[inline]
211            unsafe fn from_ptr(ptr: *mut $ctype) -> $owned<T> {
212                $owned(ptr, ::std::marker::PhantomData)
213            }
214
215            #[inline]
216            fn as_ptr(&self) -> *mut $ctype {
217                self.0
218            }
219        }
220
221        impl<T> Drop for $owned<T> {
222            #[inline]
223            fn drop(&mut self) {
224                unsafe { $drop(self.0) }
225            }
226        }
227
228        $(
229            impl<T> Clone for $owned<T> {
230                #[inline]
231                fn clone(&self) -> $owned<T> {
232                    unsafe {
233                        let handle: *mut $ctype = $clone(self.0);
234                        ::foreign_types::ForeignType::from_ptr(handle)
235                    }
236                }
237            }
238
239            impl<T> ::std::borrow::ToOwned for $borrowed<T> {
240                type Owned = $owned<T>;
241                #[inline]
242                fn to_owned(&self) -> $owned<T> {
243                    unsafe {
244                        let handle: *mut $ctype =
245                            $clone(::foreign_types::ForeignTypeRef::as_ptr(self));
246                        $crate::ForeignType::from_ptr(handle)
247                    }
248                }
249            }
250        )*
251
252        impl<T> ::std::ops::Deref for $owned<T> {
253            type Target = $borrowed<T>;
254
255            #[inline]
256            fn deref(&self) -> &$borrowed<T> {
257                unsafe { ::foreign_types::ForeignTypeRef::from_ptr(self.0) }
258            }
259        }
260
261        impl<T> ::std::ops::DerefMut for $owned<T> {
262            #[inline]
263            fn deref_mut(&mut self) -> &mut $borrowed<T> {
264                unsafe { ::foreign_types::ForeignTypeRef::from_ptr_mut(self.0) }
265            }
266        }
267
268        impl<T> ::std::borrow::Borrow<$borrowed<T>> for $owned<T> {
269            #[inline]
270            fn borrow(&self) -> &$borrowed<T> {
271                &**self
272            }
273        }
274
275        impl<T> ::std::convert::AsRef<$borrowed<T>> for $owned<T> {
276            #[inline]
277            fn as_ref(&self) -> &$borrowed<T> {
278                &**self
279            }
280        }
281
282        $(#[$borrowed_attr])*
283        pub struct $borrowed<T>(::foreign_types::Opaque, ::std::marker::PhantomData<T>);
284
285        $(#[$impl_attr])*
286        impl<T> ::foreign_types::ForeignTypeRef for $borrowed<T> {
287            type CType = $ctype;
288        }
289
290        unsafe impl<T> Send for $owned<T>{}
291        unsafe impl<T> Send for $borrowed<T>{}
292        unsafe impl<T> Sync for $owned<T>{}
293        unsafe impl<T> Sync for $borrowed<T>{}
294    };
295}