diff options
| author | Steven Fackler <[email protected]> | 2016-11-13 15:02:38 +0000 |
|---|---|---|
| committer | Steven Fackler <[email protected]> | 2016-11-13 15:02:38 +0000 |
| commit | 08e0c4ca9061c3dc0c951db0c08909689b04a310 (patch) | |
| tree | 117d95bb3cc28cf88cdedb97c985bbe9db436415 /openssl/src/macros.rs | |
| parent | No need to use a raw string anymore (diff) | |
| download | rust-openssl-08e0c4ca9061c3dc0c951db0c08909689b04a310.tar.xz rust-openssl-08e0c4ca9061c3dc0c951db0c08909689b04a310.zip | |
Some serialization support for EcKey
Diffstat (limited to 'openssl/src/macros.rs')
| -rw-r--r-- | openssl/src/macros.rs | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/openssl/src/macros.rs b/openssl/src/macros.rs new file mode 100644 index 00000000..0db3401c --- /dev/null +++ b/openssl/src/macros.rs @@ -0,0 +1,77 @@ + +macro_rules! type_ { + ($n:ident, $r:ident, $c:path, $d:path) => { + pub struct $n(*mut $c); + + impl ::types::OpenSslType for $n { + type CType = $c; + type Ref = $r; + + unsafe fn from_ptr(ptr: *mut $c) -> $n { + $n(ptr) + } + } + + impl Drop for $n { + fn drop(&mut self) { + unsafe { $d(self.0) } + } + } + + impl ::std::ops::Deref for $n { + type Target = $r; + + fn deref(&self) -> &$r { + unsafe { ::types::OpenSslTypeRef::from_ptr(self.0) } + } + } + + impl ::std::ops::DerefMut for $n { + fn deref_mut(&mut self) -> &mut $r { + unsafe { ::types::OpenSslTypeRef::from_ptr_mut(self.0) } + } + } + + pub struct $r(::util::Opaque); + + impl ::types::OpenSslTypeRef for $r { + type CType = $c; + } + } +} + +macro_rules! private_key_from_pem { + ($t:ident, $f:path) => { + /// Deserializes a PEM-formatted private key. + pub fn private_key_from_pem(pem: &[u8]) -> Result<$t, ::error::ErrorStack> { + unsafe { + ::init(); + let bio = try!(::bio::MemBioSlice::new(pem)); + cvt_p($f(bio.as_ptr(), ::std::ptr::null_mut(), None, ::std::ptr::null_mut())) + .map($t) + } + } + + /// Deserializes a PEM-formatted private key, using a callback to retrieve a password if the + /// key is encrypted. + /// + /// The callback should copy the password into the provided buffer and return the number of + /// bytes written. + pub fn private_key_from_pem_callback<F>(pem: &[u8], + callback: F) + -> Result<$t, ::error::ErrorStack> + where F: FnOnce(&mut [u8]) -> usize + { + unsafe { + ffi::init(); + let mut cb = ::util::CallbackState::new(callback); + let bio = try!(::bio::MemBioSlice::new(pem)); + cvt_p($f(bio.as_ptr(), + ptr::null_mut(), + Some(::util::invoke_passwd_cb::<F>), + &mut cb as *mut _ as *mut ::libc::c_void)) + .map($t) + } + } + } +} |