parent
15490a43e3
commit
6b7279eb52
|
|
@ -1713,11 +1713,20 @@ extern {
|
||||||
name: *const c_char,
|
name: *const c_char,
|
||||||
namelen: size_t) -> c_int;
|
namelen: size_t) -> c_int;
|
||||||
|
|
||||||
|
pub fn d2i_DHparams(k: *mut *mut DH, pp: *mut *const c_uchar, length: c_long) -> *mut DH;
|
||||||
|
pub fn i2d_DHparams(dh: *const DH, pp: *mut *mut c_uchar) -> c_int;
|
||||||
|
|
||||||
|
pub fn d2i_DSAPublicKey(a: *mut *mut DSA, pp: *mut *const c_uchar, length: c_long) -> *mut DSA;
|
||||||
|
pub fn i2d_DSAPublicKey(a: *const DSA, pp: *mut *mut c_uchar) -> c_int;
|
||||||
|
pub fn d2i_DSAPrivateKey(a: *mut *mut DSA, pp: *mut *const c_uchar, length: c_long) -> *mut DSA;
|
||||||
|
pub fn i2d_DSAPrivateKey(a: *const DSA, pp: *mut *mut c_uchar) -> c_int;
|
||||||
|
|
||||||
pub fn d2i_X509(a: *mut *mut X509, pp: *mut *const c_uchar, length: c_long) -> *mut X509;
|
pub fn d2i_X509(a: *mut *mut X509, pp: *mut *const c_uchar, length: c_long) -> *mut X509;
|
||||||
pub fn i2d_X509_bio(b: *mut BIO, x: *mut X509) -> c_int;
|
pub fn i2d_X509_bio(b: *mut BIO, x: *mut X509) -> c_int;
|
||||||
pub fn i2d_X509_REQ_bio(b: *mut BIO, x: *mut X509_REQ) -> c_int;
|
pub fn i2d_X509_REQ_bio(b: *mut BIO, x: *mut X509_REQ) -> c_int;
|
||||||
|
|
||||||
pub fn i2d_PUBKEY_bio(b: *mut BIO, x: *mut EVP_PKEY) -> c_int;
|
pub fn i2d_PUBKEY_bio(b: *mut BIO, x: *mut EVP_PKEY) -> c_int;
|
||||||
|
pub fn i2d_PrivateKey_bio(b: *mut BIO, x: *mut EVP_PKEY) -> c_int;
|
||||||
|
|
||||||
pub fn i2d_RSA_PUBKEY(k: *mut RSA, buf: *mut *mut u8) -> c_int;
|
pub fn i2d_RSA_PUBKEY(k: *mut RSA, buf: *mut *mut u8) -> c_int;
|
||||||
pub fn d2i_RSA_PUBKEY(k: *mut *mut RSA, buf: *mut *const u8, len: c_long) -> *mut RSA;
|
pub fn d2i_RSA_PUBKEY(k: *mut *mut RSA, buf: *mut *const u8, len: c_long) -> *mut RSA;
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,12 @@
|
||||||
use ffi;
|
|
||||||
use error::ErrorStack;
|
use error::ErrorStack;
|
||||||
use bio::MemBioSlice;
|
use ffi;
|
||||||
use std::ptr;
|
use libc::c_long;
|
||||||
|
use std::cmp;
|
||||||
use std::mem;
|
use std::mem;
|
||||||
|
use std::ptr;
|
||||||
|
|
||||||
use {cvt, cvt_p};
|
use {cvt, cvt_p, init};
|
||||||
use bio::MemBio;
|
use bio::{MemBio, MemBioSlice};
|
||||||
use bn::BigNum;
|
use bn::BigNum;
|
||||||
use types::OpenSslTypeRef;
|
use types::OpenSslTypeRef;
|
||||||
|
|
||||||
|
|
@ -15,18 +16,27 @@ impl DhRef {
|
||||||
/// Encodes the parameters to PEM.
|
/// Encodes the parameters to PEM.
|
||||||
pub fn to_pem(&self) -> Result<Vec<u8>, ErrorStack> {
|
pub fn to_pem(&self) -> Result<Vec<u8>, ErrorStack> {
|
||||||
let mem_bio = try!(MemBio::new());
|
let mem_bio = try!(MemBio::new());
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
try!(cvt(ffi::PEM_write_bio_DHparams(mem_bio.as_ptr(), self.as_ptr())));
|
try!(cvt(ffi::PEM_write_bio_DHparams(mem_bio.as_ptr(), self.as_ptr())));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(mem_bio.get_buf().to_owned())
|
Ok(mem_bio.get_buf().to_owned())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Encodes the parameters to DER.
|
||||||
|
pub fn to_der(&self) -> Result<Vec<u8>, ErrorStack> {
|
||||||
|
unsafe {
|
||||||
|
let len = try!(cvt(ffi::i2d_DHparams(self.as_ptr(), ptr::null_mut())));
|
||||||
|
let mut buf = vec![0; len as usize];
|
||||||
|
try!(cvt(ffi::i2d_DHparams(self.as_ptr(), &mut buf.as_mut_ptr())));
|
||||||
|
Ok(buf)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Dh {
|
impl Dh {
|
||||||
pub fn from_params(p: BigNum, g: BigNum, q: BigNum) -> Result<Dh, ErrorStack> {
|
pub fn from_params(p: BigNum, g: BigNum, q: BigNum) -> Result<Dh, ErrorStack> {
|
||||||
unsafe {
|
unsafe {
|
||||||
|
init();
|
||||||
let dh = Dh(try!(cvt_p(ffi::DH_new())));
|
let dh = Dh(try!(cvt_p(ffi::DH_new())));
|
||||||
try!(cvt(compat::DH_set0_pqg(dh.0, p.as_ptr(), q.as_ptr(), g.as_ptr())));
|
try!(cvt(compat::DH_set0_pqg(dh.0, p.as_ptr(), q.as_ptr(), g.as_ptr())));
|
||||||
mem::forget((p, g, q));
|
mem::forget((p, g, q));
|
||||||
|
|
@ -34,9 +44,11 @@ impl Dh {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Reads Diffie-Hellman parameters from PEM.
|
||||||
pub fn from_pem(buf: &[u8]) -> Result<Dh, ErrorStack> {
|
pub fn from_pem(buf: &[u8]) -> Result<Dh, ErrorStack> {
|
||||||
let mem_bio = try!(MemBioSlice::new(buf));
|
|
||||||
unsafe {
|
unsafe {
|
||||||
|
init();
|
||||||
|
let mem_bio = try!(MemBioSlice::new(buf));
|
||||||
cvt_p(ffi::PEM_read_bio_DHparams(mem_bio.as_ptr(),
|
cvt_p(ffi::PEM_read_bio_DHparams(mem_bio.as_ptr(),
|
||||||
ptr::null_mut(),
|
ptr::null_mut(),
|
||||||
None,
|
None,
|
||||||
|
|
@ -45,6 +57,16 @@ impl Dh {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Reads Diffie-Hellman parameters from DER.
|
||||||
|
pub fn from_der(buf: &[u8]) -> Result<Dh, ErrorStack> {
|
||||||
|
unsafe {
|
||||||
|
init();
|
||||||
|
let len = cmp::min(buf.len(), c_long::max_value() as usize) as c_long;
|
||||||
|
let dh = try!(cvt_p(ffi::d2i_DHparams(ptr::null_mut(), &mut buf.as_ptr(), len)));
|
||||||
|
Ok(Dh(dh))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Requires the `v102` or `v110` features and OpenSSL 1.0.2 or OpenSSL 1.1.0.
|
/// Requires the `v102` or `v110` features and OpenSSL 1.0.2 or OpenSSL 1.1.0.
|
||||||
#[cfg(any(all(feature = "v102", ossl102), all(feature = "v110", ossl110)))]
|
#[cfg(any(all(feature = "v102", ossl102), all(feature = "v110", ossl110)))]
|
||||||
pub fn get_1024_160() -> Result<Dh, ErrorStack> {
|
pub fn get_1024_160() -> Result<Dh, ErrorStack> {
|
||||||
|
|
@ -139,7 +161,15 @@ mod tests {
|
||||||
fn test_dh_from_pem() {
|
fn test_dh_from_pem() {
|
||||||
let mut ctx = SslContext::builder(SslMethod::tls()).unwrap();
|
let mut ctx = SslContext::builder(SslMethod::tls()).unwrap();
|
||||||
let params = include_bytes!("../test/dhparams.pem");
|
let params = include_bytes!("../test/dhparams.pem");
|
||||||
let dh = Dh::from_pem(params).ok().expect("Failed to load PEM");
|
let dh = Dh::from_pem(params).unwrap();
|
||||||
ctx.set_tmp_dh(&dh).unwrap();
|
ctx.set_tmp_dh(&dh).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_dh_from_der() {
|
||||||
|
let params = include_bytes!("../test/dhparams.pem");
|
||||||
|
let dh = Dh::from_pem(params).unwrap();
|
||||||
|
let der = dh.to_der().unwrap();
|
||||||
|
Dh::from_der(&der).unwrap();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
use error::ErrorStack;
|
use error::ErrorStack;
|
||||||
use ffi;
|
use ffi;
|
||||||
use libc::{c_int, c_char, c_void};
|
use libc::{c_int, c_char, c_void, c_long};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
|
use std::cmp;
|
||||||
|
|
||||||
use bio::{MemBio, MemBioSlice};
|
use bio::{MemBio, MemBioSlice};
|
||||||
use bn::BigNumRef;
|
use bn::BigNumRef;
|
||||||
|
|
@ -13,7 +14,7 @@ use util::{CallbackState, invoke_passwd_cb};
|
||||||
type_!(Dsa, DsaRef, ffi::DSA, ffi::DSA_free);
|
type_!(Dsa, DsaRef, ffi::DSA, ffi::DSA_free);
|
||||||
|
|
||||||
impl DsaRef {
|
impl DsaRef {
|
||||||
/// Writes an DSA private key as unencrypted PEM formatted data
|
/// Encodes a DSA private key as unencrypted PEM formatted data.
|
||||||
pub fn private_key_to_pem(&self) -> Result<Vec<u8>, ErrorStack> {
|
pub fn private_key_to_pem(&self) -> Result<Vec<u8>, ErrorStack> {
|
||||||
assert!(self.has_private_key());
|
assert!(self.has_private_key());
|
||||||
let mem_bio = try!(MemBio::new());
|
let mem_bio = try!(MemBio::new());
|
||||||
|
|
@ -27,7 +28,7 @@ impl DsaRef {
|
||||||
Ok(mem_bio.get_buf().to_owned())
|
Ok(mem_bio.get_buf().to_owned())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Writes an 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> {
|
||||||
let mem_bio = try!(MemBio::new());
|
let mem_bio = try!(MemBio::new());
|
||||||
unsafe {
|
unsafe {
|
||||||
|
|
@ -36,6 +37,26 @@ impl DsaRef {
|
||||||
Ok(mem_bio.get_buf().to_owned())
|
Ok(mem_bio.get_buf().to_owned())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Encodes a DSA private key as unencrypted DER formatted data.
|
||||||
|
pub fn private_key_to_der(&self) -> Result<Vec<u8>, ErrorStack> {
|
||||||
|
unsafe {
|
||||||
|
let len = try!(cvt(ffi::i2d_DSAPrivateKey(self.as_ptr(), ptr::null_mut())));
|
||||||
|
let mut buf = vec![0; len as usize];
|
||||||
|
try!(cvt(ffi::i2d_DSAPrivateKey(self.as_ptr(), &mut buf.as_mut_ptr())));
|
||||||
|
Ok(buf)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Encodes a DSA public key as DER formatted data.
|
||||||
|
pub fn public_key_to_der(&self) -> Result<Vec<u8>, ErrorStack> {
|
||||||
|
unsafe {
|
||||||
|
let len = try!(cvt(ffi::i2d_DSAPublicKey(self.as_ptr(), ptr::null_mut())));
|
||||||
|
let mut buf = vec![0; len as usize];
|
||||||
|
try!(cvt(ffi::i2d_DSAPublicKey(self.as_ptr(), &mut buf.as_mut_ptr())));
|
||||||
|
Ok(buf)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn size(&self) -> Option<u32> {
|
pub fn size(&self) -> Option<u32> {
|
||||||
if self.q().is_some() {
|
if self.q().is_some() {
|
||||||
unsafe { Some(ffi::DSA_size(self.as_ptr()) as u32) }
|
unsafe { Some(ffi::DSA_size(self.as_ptr()) as u32) }
|
||||||
|
|
@ -139,7 +160,7 @@ impl Dsa {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reads an DSA public key from PEM formatted data.
|
/// Reads a DSA public key from PEM formatted data.
|
||||||
pub fn public_key_from_pem(buf: &[u8]) -> Result<Dsa, ErrorStack> {
|
pub fn public_key_from_pem(buf: &[u8]) -> Result<Dsa, ErrorStack> {
|
||||||
ffi::init();
|
ffi::init();
|
||||||
|
|
||||||
|
|
@ -152,6 +173,26 @@ impl Dsa {
|
||||||
Ok(Dsa(dsa))
|
Ok(Dsa(dsa))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Reads a DSA private key from DER formatted data.
|
||||||
|
pub fn private_key_from_der(buf: &[u8]) -> Result<Dsa, ErrorStack> {
|
||||||
|
unsafe {
|
||||||
|
ffi::init();
|
||||||
|
let len = cmp::min(buf.len(), c_long::max_value() as usize) as c_long;
|
||||||
|
let dsa = try!(cvt_p(ffi::d2i_DSAPrivateKey(ptr::null_mut(), &mut buf.as_ptr(), len)));
|
||||||
|
Ok(Dsa(dsa))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Reads a DSA public key from DER formatted data.
|
||||||
|
pub fn public_key_from_der(buf: &[u8]) -> Result<Dsa, ErrorStack> {
|
||||||
|
unsafe {
|
||||||
|
ffi::init();
|
||||||
|
let len = cmp::min(buf.len(), c_long::max_value() as usize) as c_long;
|
||||||
|
let dsa = try!(cvt_p(ffi::d2i_DSAPublicKey(ptr::null_mut(), &mut buf.as_ptr(), len)));
|
||||||
|
Ok(Dsa(dsa))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for Dsa {
|
impl fmt::Debug for Dsa {
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,7 @@ impl PKeyRef {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Stores private key as a PEM
|
/// Encodes the private key in the PEM format.
|
||||||
// FIXME: also add password and encryption
|
// FIXME: also add password and encryption
|
||||||
pub fn private_key_to_pem(&self) -> Result<Vec<u8>, ErrorStack> {
|
pub fn private_key_to_pem(&self) -> Result<Vec<u8>, ErrorStack> {
|
||||||
let mem_bio = try!(MemBio::new());
|
let mem_bio = try!(MemBio::new());
|
||||||
|
|
@ -65,7 +65,7 @@ impl PKeyRef {
|
||||||
Ok(mem_bio.get_buf().to_owned())
|
Ok(mem_bio.get_buf().to_owned())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Encode public key in 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> {
|
||||||
let mem_bio = try!(MemBio::new());
|
let mem_bio = try!(MemBio::new());
|
||||||
unsafe {
|
unsafe {
|
||||||
|
|
@ -74,7 +74,7 @@ impl PKeyRef {
|
||||||
Ok(mem_bio.get_buf().to_owned())
|
Ok(mem_bio.get_buf().to_owned())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Encode public key in DER format
|
/// Encodes the public key in the DER format.
|
||||||
pub fn public_key_to_der(&self) -> Result<Vec<u8>, ErrorStack> {
|
pub fn public_key_to_der(&self) -> Result<Vec<u8>, ErrorStack> {
|
||||||
let mem_bio = try!(MemBio::new());
|
let mem_bio = try!(MemBio::new());
|
||||||
unsafe {
|
unsafe {
|
||||||
|
|
@ -83,6 +83,15 @@ impl PKeyRef {
|
||||||
Ok(mem_bio.get_buf().to_owned())
|
Ok(mem_bio.get_buf().to_owned())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Encodes the private key in the DER format
|
||||||
|
pub fn private_key_to_der(&self) -> Result<Vec<u8>, ErrorStack> {
|
||||||
|
let mem_bio = try!(MemBio::new());
|
||||||
|
unsafe {
|
||||||
|
try!(cvt(ffi::i2d_PrivateKey_bio(mem_bio.as_ptr(), self.as_ptr())));
|
||||||
|
}
|
||||||
|
Ok(mem_bio.get_buf().to_owned())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn public_eq(&self, other: &PKeyRef) -> bool {
|
pub fn public_eq(&self, other: &PKeyRef) -> bool {
|
||||||
unsafe { ffi::EVP_PKEY_cmp(self.as_ptr(), other.as_ptr()) == 1 }
|
unsafe { ffi::EVP_PKEY_cmp(self.as_ptr(), other.as_ptr()) == 1 }
|
||||||
}
|
}
|
||||||
|
|
@ -148,7 +157,7 @@ impl PKey {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reads private key from PEM, takes ownership of handle
|
/// Reads a private key from PEM.
|
||||||
pub fn private_key_from_pem(buf: &[u8]) -> Result<PKey, ErrorStack> {
|
pub fn private_key_from_pem(buf: &[u8]) -> Result<PKey, ErrorStack> {
|
||||||
ffi::init();
|
ffi::init();
|
||||||
let mem_bio = try!(MemBioSlice::new(buf));
|
let mem_bio = try!(MemBioSlice::new(buf));
|
||||||
|
|
@ -181,7 +190,7 @@ impl PKey {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reads public key from PEM, takes ownership of handle
|
/// Reads a public key from PEM.
|
||||||
pub fn public_key_from_pem(buf: &[u8]) -> Result<PKey, ErrorStack> {
|
pub fn public_key_from_pem(buf: &[u8]) -> Result<PKey, ErrorStack> {
|
||||||
ffi::init();
|
ffi::init();
|
||||||
let mem_bio = try!(MemBioSlice::new(buf));
|
let mem_bio = try!(MemBioSlice::new(buf));
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
use ffi;
|
use ffi;
|
||||||
|
use std::cmp;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
use std::mem;
|
use std::mem;
|
||||||
use libc::{c_int, c_void, c_char};
|
use libc::{c_int, c_void, c_char, c_long};
|
||||||
|
|
||||||
use {cvt, cvt_p, cvt_n};
|
use {cvt, cvt_p, cvt_n};
|
||||||
use bn::{BigNum, BigNumRef};
|
use bn::{BigNum, BigNumRef};
|
||||||
|
|
@ -49,6 +50,26 @@ impl RsaRef {
|
||||||
Ok(mem_bio.get_buf().to_owned())
|
Ok(mem_bio.get_buf().to_owned())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Encodes an RSA private key as unencrypted DER formatted data.
|
||||||
|
pub fn private_key_to_der(&self) -> Result<Vec<u8>, ErrorStack> {
|
||||||
|
unsafe {
|
||||||
|
let len = try!(cvt(ffi::i2d_RSAPrivateKey(self.as_ptr(), ptr::null_mut())));
|
||||||
|
let mut buf = vec![0; len as usize];
|
||||||
|
try!(cvt(ffi::i2d_RSAPrivateKey(self.as_ptr(), &mut buf.as_mut_ptr())));
|
||||||
|
Ok(buf)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Encodes an RSA public key as DER formatted data.
|
||||||
|
pub fn public_key_to_der(&self) -> Result<Vec<u8>, ErrorStack> {
|
||||||
|
unsafe {
|
||||||
|
let len = try!(cvt(ffi::i2d_RSA_PUBKEY(self.as_ptr(), ptr::null_mut())));
|
||||||
|
let mut buf = vec![0; len as usize];
|
||||||
|
try!(cvt(ffi::i2d_RSA_PUBKEY(self.as_ptr(), &mut buf.as_mut_ptr())));
|
||||||
|
Ok(buf)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn size(&self) -> usize {
|
pub fn size(&self) -> usize {
|
||||||
unsafe {
|
unsafe {
|
||||||
assert!(self.n().is_some());
|
assert!(self.n().is_some());
|
||||||
|
|
@ -250,6 +271,7 @@ impl Rsa {
|
||||||
///
|
///
|
||||||
/// The public exponent will be 65537.
|
/// The public exponent will be 65537.
|
||||||
pub fn generate(bits: u32) -> Result<Rsa, ErrorStack> {
|
pub fn generate(bits: u32) -> Result<Rsa, ErrorStack> {
|
||||||
|
ffi::init();
|
||||||
unsafe {
|
unsafe {
|
||||||
let rsa = Rsa(try!(cvt_p(ffi::RSA_new())));
|
let rsa = Rsa(try!(cvt_p(ffi::RSA_new())));
|
||||||
let e = try!(BigNum::from_u32(ffi::RSA_F4 as u32));
|
let e = try!(BigNum::from_u32(ffi::RSA_F4 as u32));
|
||||||
|
|
@ -260,6 +282,7 @@ impl Rsa {
|
||||||
|
|
||||||
/// Reads an RSA private key from PEM formatted data.
|
/// Reads an RSA private key from PEM formatted data.
|
||||||
pub fn private_key_from_pem(buf: &[u8]) -> Result<Rsa, ErrorStack> {
|
pub fn private_key_from_pem(buf: &[u8]) -> Result<Rsa, ErrorStack> {
|
||||||
|
ffi::init();
|
||||||
let mem_bio = try!(MemBioSlice::new(buf));
|
let mem_bio = try!(MemBioSlice::new(buf));
|
||||||
unsafe {
|
unsafe {
|
||||||
let rsa = try!(cvt_p(ffi::PEM_read_bio_RSAPrivateKey(mem_bio.as_ptr(),
|
let rsa = try!(cvt_p(ffi::PEM_read_bio_RSAPrivateKey(mem_bio.as_ptr(),
|
||||||
|
|
@ -274,6 +297,7 @@ impl Rsa {
|
||||||
pub fn private_key_from_pem_cb<F>(buf: &[u8], pass_cb: F) -> Result<Rsa, ErrorStack>
|
pub fn private_key_from_pem_cb<F>(buf: &[u8], pass_cb: F) -> Result<Rsa, ErrorStack>
|
||||||
where F: FnOnce(&mut [c_char]) -> usize
|
where F: FnOnce(&mut [c_char]) -> usize
|
||||||
{
|
{
|
||||||
|
ffi::init();
|
||||||
let mut cb = CallbackState::new(pass_cb);
|
let mut cb = CallbackState::new(pass_cb);
|
||||||
let mem_bio = try!(MemBioSlice::new(buf));
|
let mem_bio = try!(MemBioSlice::new(buf));
|
||||||
|
|
||||||
|
|
@ -289,6 +313,7 @@ impl Rsa {
|
||||||
|
|
||||||
/// Reads an RSA public key from PEM formatted data.
|
/// Reads an RSA public key from PEM formatted data.
|
||||||
pub fn public_key_from_pem(buf: &[u8]) -> Result<Rsa, ErrorStack> {
|
pub fn public_key_from_pem(buf: &[u8]) -> Result<Rsa, ErrorStack> {
|
||||||
|
ffi::init();
|
||||||
let mem_bio = try!(MemBioSlice::new(buf));
|
let mem_bio = try!(MemBioSlice::new(buf));
|
||||||
unsafe {
|
unsafe {
|
||||||
let rsa = try!(cvt_p(ffi::PEM_read_bio_RSA_PUBKEY(mem_bio.as_ptr(),
|
let rsa = try!(cvt_p(ffi::PEM_read_bio_RSA_PUBKEY(mem_bio.as_ptr(),
|
||||||
|
|
@ -298,6 +323,26 @@ impl Rsa {
|
||||||
Ok(Rsa(rsa))
|
Ok(Rsa(rsa))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Reads an RSA private key from DER formatted data.
|
||||||
|
pub fn private_key_from_der(buf: &[u8]) -> Result<Rsa, ErrorStack> {
|
||||||
|
unsafe {
|
||||||
|
ffi::init();
|
||||||
|
let len = cmp::min(buf.len(), c_long::max_value() as usize) as c_long;
|
||||||
|
let dsa = try!(cvt_p(ffi::d2i_RSAPrivateKey(ptr::null_mut(), &mut buf.as_ptr(), len)));
|
||||||
|
Ok(Rsa(dsa))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Reads an RSA public key from DER formatted data.
|
||||||
|
pub fn public_key_from_der(buf: &[u8]) -> Result<Rsa, ErrorStack> {
|
||||||
|
unsafe {
|
||||||
|
ffi::init();
|
||||||
|
let len = cmp::min(buf.len(), c_long::max_value() as usize) as c_long;
|
||||||
|
let dsa = try!(cvt_p(ffi::d2i_RSA_PUBKEY(ptr::null_mut(), &mut buf.as_ptr(), len)));
|
||||||
|
Ok(Rsa(dsa))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for Rsa {
|
impl fmt::Debug for Rsa {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue