Macroise to_pem

This commit is contained in:
Steven Fackler 2016-11-13 17:42:45 +00:00
parent 48c0009418
commit df9666c334
3 changed files with 36 additions and 33 deletions

View File

@ -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);
}

View File

@ -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]

View File

@ -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