diff options
| author | Steven Fackler <[email protected]> | 2016-11-13 17:42:45 +0000 |
|---|---|---|
| committer | Steven Fackler <[email protected]> | 2016-11-13 17:42:45 +0000 |
| commit | df9666c334f29d4d0887919c2b35c45092960d3a (patch) | |
| tree | 6854bd46f774eab430a2e21e850a61c1900e67d9 /openssl/src | |
| parent | Macroise from_der (diff) | |
| download | rust-openssl-df9666c334f29d4d0887919c2b35c45092960d3a.tar.xz rust-openssl-df9666c334f29d4d0887919c2b35c45092960d3a.zip | |
Macroise to_pem
Diffstat (limited to 'openssl/src')
| -rw-r--r-- | openssl/src/dh.rs | 12 | ||||
| -rw-r--r-- | openssl/src/macros.rs | 27 | ||||
| -rw-r--r-- | openssl/src/x509/mod.rs | 30 |
3 files changed, 36 insertions, 33 deletions
diff --git a/openssl/src/dh.rs b/openssl/src/dh.rs index 604d4f5a..37663ac0 100644 --- a/openssl/src/dh.rs +++ b/openssl/src/dh.rs @@ -4,22 +4,14 @@ use std::mem; use std::ptr; use {cvt, cvt_p, init}; -use bio::{MemBio, MemBioSlice}; +use bio::MemBioSlice; use bn::BigNum; use types::OpenSslTypeRef; type_!(Dh, DhRef, ffi::DH, ffi::DH_free); impl DhRef { - /// Encodes the parameters to PEM. - pub fn to_pem(&self) -> Result<Vec<u8>, ErrorStack> { - let mem_bio = try!(MemBio::new()); - unsafe { - try!(cvt(ffi::PEM_write_bio_DHparams(mem_bio.as_ptr(), self.as_ptr()))); - } - Ok(mem_bio.get_buf().to_owned()) - } - + to_pem!(ffi::PEM_write_bio_DHparams); to_der!(ffi::i2d_DHparams); } diff --git a/openssl/src/macros.rs b/openssl/src/macros.rs index 39944124..a57f36eb 100644 --- a/openssl/src/macros.rs +++ b/openssl/src/macros.rs @@ -136,6 +136,33 @@ macro_rules! private_key_to_pem { } } +macro_rules! to_pem_inner { + (#[$m:meta] $n:ident, $f:path) => { + #[$m] + pub fn $n(&self) -> Result<Vec<u8>, ::error::ErrorStack> { + unsafe { + let bio = try!(::bio::MemBio::new()); + try!(cvt($f(bio.as_ptr(), self.as_ptr()))); + Ok(bio.get_buf().to_owned()) + } + } + } +} + +macro_rules! public_key_to_pem { + ($f:path) => { + to_pem_inner!(/// Serializes a public key to PEM. + public_key_to_pem, $f); + } +} + +macro_rules! to_pem { + ($f:path) => { + to_pem_inner!(/// Serializes this value to PEM. + to_pem, $f); + } +} + macro_rules! to_der_inner { (#[$m:meta] $n:ident, $f:path) => { #[$m] diff --git a/openssl/src/x509/mod.rs b/openssl/src/x509/mod.rs index 0a5a6c4d..8a739ec6 100644 --- a/openssl/src/x509/mod.rs +++ b/openssl/src/x509/mod.rs @@ -12,7 +12,7 @@ use std::str; use {cvt, cvt_p}; use asn1::{Asn1StringRef, Asn1Time, Asn1TimeRef}; -use bio::{MemBio, MemBioSlice}; +use bio::MemBioSlice; use hash::MessageDigest; use pkey::{PKey, PKeyRef}; use rand::rand_bytes; @@ -415,15 +415,7 @@ impl X509Ref { } } - /// Writes certificate as PEM - pub fn to_pem(&self) -> Result<Vec<u8>, ErrorStack> { - let mem_bio = try!(MemBio::new()); - unsafe { - try!(cvt(ffi::PEM_write_bio_X509(mem_bio.as_ptr(), self.as_ptr()))); - } - Ok(mem_bio.get_buf().to_owned()) - } - + to_pem!(ffi::PEM_write_bio_X509); to_der!(ffi::i2d_X509); } @@ -549,19 +541,6 @@ impl X509NameEntryRef { type_!(X509Req, X509ReqRef, ffi::X509_REQ, ffi::X509_REQ_free); -impl X509ReqRef { - /// Writes CSR as PEM - pub fn to_pem(&self) -> Result<Vec<u8>, ErrorStack> { - let mem_bio = try!(MemBio::new()); - if unsafe { ffi::PEM_write_bio_X509_REQ(mem_bio.as_ptr(), self.as_ptr()) } != 1 { - return Err(ErrorStack::get()); - } - Ok(mem_bio.get_buf().to_owned()) - } - - to_der!(ffi::i2d_X509_REQ); -} - impl X509Req { /// Reads CSR from PEM pub fn from_pem(buf: &[u8]) -> Result<X509Req, ErrorStack> { @@ -578,6 +557,11 @@ impl X509Req { from_der!(X509Req, ffi::d2i_X509_REQ); } +impl X509ReqRef { + to_pem!(ffi::PEM_write_bio_X509_REQ); + to_der!(ffi::i2d_X509_REQ); +} + /// A collection of X.509 extensions. /// /// Upholds the invariant that a certificate MUST NOT include more than one |