Macro-implement private_key_to_pem
This commit is contained in:
parent
08e0c4ca90
commit
2a8923c050
|
|
@ -14,19 +14,7 @@ use util::{CallbackState, invoke_passwd_cb_old};
|
||||||
type_!(Dsa, DsaRef, ffi::DSA, ffi::DSA_free);
|
type_!(Dsa, DsaRef, ffi::DSA, ffi::DSA_free);
|
||||||
|
|
||||||
impl DsaRef {
|
impl DsaRef {
|
||||||
/// Encodes a DSA private key as unencrypted PEM formatted data.
|
private_key_to_pem!(ffi::PEM_write_bio_DSAPrivateKey);
|
||||||
pub fn private_key_to_pem(&self) -> Result<Vec<u8>, ErrorStack> {
|
|
||||||
assert!(self.has_private_key());
|
|
||||||
let mem_bio = try!(MemBio::new());
|
|
||||||
|
|
||||||
unsafe {
|
|
||||||
try!(cvt(ffi::PEM_write_bio_DSAPrivateKey(mem_bio.as_ptr(), self.as_ptr(),
|
|
||||||
ptr::null(), ptr::null_mut(), 0,
|
|
||||||
None, ptr::null_mut())))
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(mem_bio.get_buf().to_owned())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Encodes a DSA public key as PEM formatted data.
|
/// Encodes a DSA public key as PEM formatted data.
|
||||||
pub fn public_key_to_pem(&self) -> Result<Vec<u8>, ErrorStack> {
|
pub fn public_key_to_pem(&self) -> Result<Vec<u8>, ErrorStack> {
|
||||||
|
|
|
||||||
|
|
@ -75,3 +75,22 @@ macro_rules! private_key_from_pem {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! private_key_to_pem {
|
||||||
|
($f:path) => {
|
||||||
|
/// Serializes the private key to PEM.
|
||||||
|
pub fn private_key_to_pem(&self) -> Result<Vec<u8>, ::error::ErrorStack> {
|
||||||
|
unsafe {
|
||||||
|
let bio = try!(MemBio::new());
|
||||||
|
try!(cvt($f(bio.as_ptr(),
|
||||||
|
self.as_ptr(),
|
||||||
|
ptr::null(),
|
||||||
|
ptr::null_mut(),
|
||||||
|
-1,
|
||||||
|
None,
|
||||||
|
ptr::null_mut())));
|
||||||
|
Ok(bio.get_buf().to_owned())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -48,22 +48,7 @@ impl PKeyRef {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Encodes the private key in the PEM format.
|
private_key_to_pem!(ffi::PEM_write_bio_PrivateKey);
|
||||||
// FIXME: also add password and encryption
|
|
||||||
pub fn private_key_to_pem(&self) -> Result<Vec<u8>, ErrorStack> {
|
|
||||||
let mem_bio = try!(MemBio::new());
|
|
||||||
unsafe {
|
|
||||||
try!(cvt(ffi::PEM_write_bio_PrivateKey(mem_bio.as_ptr(),
|
|
||||||
self.as_ptr(),
|
|
||||||
ptr::null(),
|
|
||||||
ptr::null_mut(),
|
|
||||||
-1,
|
|
||||||
None,
|
|
||||||
ptr::null_mut())));
|
|
||||||
|
|
||||||
}
|
|
||||||
Ok(mem_bio.get_buf().to_owned())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Encodes the public key in the PEM format.
|
/// Encodes the public key in the PEM format.
|
||||||
pub fn public_key_to_pem(&self) -> Result<Vec<u8>, ErrorStack> {
|
pub fn public_key_to_pem(&self) -> Result<Vec<u8>, ErrorStack> {
|
||||||
|
|
|
||||||
|
|
@ -23,21 +23,7 @@ pub const PKCS1_OAEP_PADDING: Padding = Padding(ffi::RSA_PKCS1_OAEP_PADDING);
|
||||||
type_!(Rsa, RsaRef, ffi::RSA, ffi::RSA_free);
|
type_!(Rsa, RsaRef, ffi::RSA, ffi::RSA_free);
|
||||||
|
|
||||||
impl RsaRef {
|
impl RsaRef {
|
||||||
/// Writes an RSA private key as unencrypted PEM formatted data
|
private_key_to_pem!(ffi::PEM_write_bio_RSAPrivateKey);
|
||||||
pub fn private_key_to_pem(&self) -> Result<Vec<u8>, ErrorStack> {
|
|
||||||
let mem_bio = try!(MemBio::new());
|
|
||||||
|
|
||||||
unsafe {
|
|
||||||
try!(cvt(ffi::PEM_write_bio_RSAPrivateKey(mem_bio.as_ptr(),
|
|
||||||
self.as_ptr(),
|
|
||||||
ptr::null(),
|
|
||||||
ptr::null_mut(),
|
|
||||||
0,
|
|
||||||
None,
|
|
||||||
ptr::null_mut())));
|
|
||||||
}
|
|
||||||
Ok(mem_bio.get_buf().to_owned())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Writes an RSA public key as PEM formatted data
|
/// Writes an RSA public key as PEM formatted data
|
||||||
pub fn public_key_to_pem(&self) -> Result<Vec<u8>, ErrorStack> {
|
pub fn public_key_to_pem(&self) -> Result<Vec<u8>, ErrorStack> {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue