Loading openssl-sys/src/lib.rs +1 −0 Original line number Diff line number Diff line Loading @@ -573,6 +573,7 @@ extern "C" { 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_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); Loading openssl/src/crypto/pkey.rs +11 −1 Original line number Diff line number Diff line Loading @@ -11,7 +11,7 @@ use ffi; use ssl::error::{SslError, StreamError}; #[derive(Copy, Clone)] enum Parts { pub enum Parts { Neither, Public, Both Loading Loading @@ -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<R>(reader: &mut R) -> Result<PKey, SslError> where R: Read { let mut mem_bio = try!(MemBio::new()); Loading openssl/src/x509/mod.rs +8 −1 Original line number Diff line number Diff line Loading @@ -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}; Loading Loading @@ -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<Vec<u8>> { let evp = hash_type.evp_md(); Loading openssl/src/x509/tests.rs +3 −0 Original line number Diff line number Diff line Loading @@ -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}; Loading @@ -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] Loading Loading
openssl-sys/src/lib.rs +1 −0 Original line number Diff line number Diff line Loading @@ -573,6 +573,7 @@ extern "C" { 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_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); Loading
openssl/src/crypto/pkey.rs +11 −1 Original line number Diff line number Diff line Loading @@ -11,7 +11,7 @@ use ffi; use ssl::error::{SslError, StreamError}; #[derive(Copy, Clone)] enum Parts { pub enum Parts { Neither, Public, Both Loading Loading @@ -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<R>(reader: &mut R) -> Result<PKey, SslError> where R: Read { let mut mem_bio = try!(MemBio::new()); Loading
openssl/src/x509/mod.rs +8 −1 Original line number Diff line number Diff line Loading @@ -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}; Loading Loading @@ -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<Vec<u8>> { let evp = hash_type.evp_md(); Loading
openssl/src/x509/tests.rs +3 −0 Original line number Diff line number Diff line Loading @@ -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}; Loading @@ -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] Loading