From 7db00b97ba4a5e513e2a8bd555bd2b2c36bc0afa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Sch=C3=B6lling?= Date: Wed, 15 Apr 2015 22:54:03 +0200 Subject: Add X509::public_key() --- openssl/src/crypto/pkey.rs | 12 +++++++++++- openssl/src/x509/mod.rs | 9 ++++++++- openssl/src/x509/tests.rs | 3 +++ 3 files changed, 22 insertions(+), 2 deletions(-) (limited to 'openssl/src') diff --git a/openssl/src/crypto/pkey.rs b/openssl/src/crypto/pkey.rs index 8454f252..33433b0c 100644 --- a/openssl/src/crypto/pkey.rs +++ b/openssl/src/crypto/pkey.rs @@ -11,7 +11,7 @@ use ffi; use ssl::error::{SslError, StreamError}; #[derive(Copy, Clone)] -enum Parts { +pub enum Parts { Neither, Public, 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 pub fn private_key_from_pem(reader: &mut R) -> Result where R: Read { let mut mem_bio = try!(MemBio::new()); diff --git a/openssl/src/x509/mod.rs b/openssl/src/x509/mod.rs index 50731e48..c0e730f7 100644 --- a/openssl/src/x509/mod.rs +++ b/openssl/src/x509/mod.rs @@ -11,7 +11,7 @@ use asn1::{Asn1Time}; use bio::{MemBio}; use crypto::hash; use crypto::hash::Type as HashType; -use crypto::pkey::{PKey}; +use crypto::pkey::{PKey,Parts}; use crypto::rand::rand_bytes; use ffi; use ssl::error::{SslError, StreamError}; @@ -402,6 +402,13 @@ impl<'ctx> X509<'ctx> { 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 pub fn fingerprint(&self, hash_type: hash::Type) -> Option> { let evp = hash_type.evp_md(); diff --git a/openssl/src/x509/tests.rs b/openssl/src/x509/tests.rs index 5ea0c1dc..1788b556 100644 --- a/openssl/src/x509/tests.rs +++ b/openssl/src/x509/tests.rs @@ -2,6 +2,7 @@ use serialize::hex::FromHex; use std::io; use std::path::Path; use std::fs::File; +use std::str; use crypto::hash::Type::{SHA256}; use x509::{X509, X509Generator}; @@ -28,6 +29,8 @@ fn test_cert_gen() { // FIXME: check data in result to be correct, needs implementation // of X509 getters + + assert_eq!(pkey.save_pub(), cert.public_key().save_pub()); } #[test] -- cgit v1.2.3