Merge pull request #200 from manuels/x509_pubkey

Add X509::public_key()
This commit is contained in:
Steven Fackler 2015-04-15 23:35:46 -04:00
commit 9ab4c93ab1
4 changed files with 23 additions and 2 deletions

View File

@ -573,6 +573,7 @@ extern "C" {
pub fn X509_set_version(x: *mut X509, version: c_ulong) -> c_int; pub fn X509_set_version(x: *mut X509, version: c_ulong) -> c_int;
pub fn X509_set_pubkey(x: *mut X509, pkey: *mut EVP_PKEY) -> c_int; pub fn X509_set_pubkey(x: *mut X509, pkey: *mut EVP_PKEY) -> c_int;
pub fn X509_sign(x: *mut X509, pkey: *mut EVP_PKEY, md: *const EVP_MD) -> c_int; pub fn X509_sign(x: *mut X509, pkey: *mut EVP_PKEY, md: *const EVP_MD) -> c_int;
pub fn X509_get_pubkey(x: *mut X509) -> *mut EVP_PKEY;
pub fn X509_EXTENSION_free(ext: *mut X509_EXTENSION); pub fn X509_EXTENSION_free(ext: *mut X509_EXTENSION);

View File

@ -11,7 +11,7 @@ use ffi;
use ssl::error::{SslError, StreamError}; use ssl::error::{SslError, StreamError};
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
enum Parts { pub enum Parts {
Neither, Neither,
Public, Public,
Both Both
@ -70,6 +70,16 @@ impl PKey {
} }
} }
pub fn from_handle(handle: *mut ffi::EVP_PKEY, parts: Parts) -> PKey {
ffi::init();
assert!(!handle.is_null());
PKey {
evp: handle,
parts: parts,
}
}
/// Reads private key from PEM, takes ownership of handle /// Reads private key from PEM, takes ownership of handle
pub fn private_key_from_pem<R>(reader: &mut R) -> Result<PKey, SslError> where R: Read { pub fn private_key_from_pem<R>(reader: &mut R) -> Result<PKey, SslError> where R: Read {
let mut mem_bio = try!(MemBio::new()); let mut mem_bio = try!(MemBio::new());

View File

@ -11,7 +11,7 @@ use asn1::{Asn1Time};
use bio::{MemBio}; use bio::{MemBio};
use crypto::hash; use crypto::hash;
use crypto::hash::Type as HashType; use crypto::hash::Type as HashType;
use crypto::pkey::{PKey}; use crypto::pkey::{PKey,Parts};
use crypto::rand::rand_bytes; use crypto::rand::rand_bytes;
use ffi; use ffi;
use ssl::error::{SslError, StreamError}; use ssl::error::{SslError, StreamError};
@ -402,6 +402,13 @@ impl<'ctx> X509<'ctx> {
X509Name { x509: self, name: name } X509Name { x509: self, name: name }
} }
pub fn public_key(&self) -> PKey {
let pkey = unsafe { ffi::X509_get_pubkey(self.handle) };
assert!(!pkey.is_null());
PKey::from_handle(pkey, Parts::Public)
}
/// Returns certificate fingerprint calculated using provided hash /// Returns certificate fingerprint calculated using provided hash
pub fn fingerprint(&self, hash_type: hash::Type) -> Option<Vec<u8>> { pub fn fingerprint(&self, hash_type: hash::Type) -> Option<Vec<u8>> {
let evp = hash_type.evp_md(); let evp = hash_type.evp_md();

View File

@ -2,6 +2,7 @@ use serialize::hex::FromHex;
use std::io; use std::io;
use std::path::Path; use std::path::Path;
use std::fs::File; use std::fs::File;
use std::str;
use crypto::hash::Type::{SHA256}; use crypto::hash::Type::{SHA256};
use x509::{X509, X509Generator}; use x509::{X509, X509Generator};
@ -28,6 +29,8 @@ fn test_cert_gen() {
// FIXME: check data in result to be correct, needs implementation // FIXME: check data in result to be correct, needs implementation
// of X509 getters // of X509 getters
assert_eq!(pkey.save_pub(), cert.public_key().save_pub());
} }
#[test] #[test]