Commit 7db00b97 authored by Manuel Schölling's avatar Manuel Schölling
Browse files

Add X509::public_key()

parent 8027fff7
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -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);

+11 −1
Original line number Diff line number Diff line
@@ -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<R>(reader: &mut R) -> Result<PKey, SslError> where R: Read {
        let mut mem_bio = try!(MemBio::new());
+8 −1
Original line number Diff line number Diff line
@@ -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<Vec<u8>> {
        let evp = hash_type.evp_md();
+3 −0
Original line number Diff line number Diff line
@@ -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]