Commit 4016edd4 authored by Kevin King's avatar Kevin King
Browse files

add EVP_PKEY_copy_parameters to FFI

copy EVP_PKEY params in PKey::clone

test that PKey::clone creates a copy
parent 3bcb977b
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -548,6 +548,7 @@ extern "C" {
    pub fn EVP_PKEY_new() -> *mut EVP_PKEY;
    pub fn EVP_PKEY_free(k: *mut EVP_PKEY);
    pub fn EVP_PKEY_assign(pkey: *mut EVP_PKEY, typ: c_int, key: *const c_void) -> c_int;
    pub fn EVP_PKEY_copy_parameters(to: *mut EVP_PKEY, from: *const EVP_PKEY) -> c_int;
    pub fn EVP_PKEY_get1_RSA(k: *mut EVP_PKEY) -> *mut RSA;
    pub fn EVP_PKEY_set1_RSA(k: *mut EVP_PKEY, r: *mut RSA) -> c_int;
    pub fn EVP_PKEY_cmp(a: *const EVP_PKEY, b: *const EVP_PKEY) -> c_int;
+0 −4
Original line number Diff line number Diff line
@@ -8,10 +8,6 @@ void rust_SSL_CTX_clone(SSL_CTX *ctx) {
    CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
}

void rust_EVP_PKEY_clone(EVP_PKEY *pkey) {
    CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY);
}

void rust_X509_clone(X509 *x509) {
    CRYPTO_add(&x509->references,1,CRYPTO_LOCK_X509);
}
+15 −7
Original line number Diff line number Diff line
@@ -53,10 +53,6 @@ fn openssl_hash_nid(hash: HashType) -> c_int {
    }
}

extern "C" {
    fn rust_EVP_PKEY_clone(pkey: *mut ffi::EVP_PKEY);
}

pub struct PKey {
    evp: *mut ffi::EVP_PKEY,
    parts: Parts,
@@ -614,10 +610,10 @@ impl Drop for PKey {
impl Clone for PKey {
    fn clone(&self) -> Self {
        unsafe {
            rust_EVP_PKEY_clone(self.evp);
            let new_evp = ffi::EVP_PKEY_new();
            assert!(ffi::EVP_PKEY_copy_parameters(new_evp, self.evp) == 0);
            PKey::from_handle(new_evp, self.parts)
        }

        PKey::from_handle(self.evp, self.parts)
    }
}

@@ -866,4 +862,16 @@ mod tests {
        pkey.load_pub(&[]);
        pkey.verify(&[], &[]);
    }

    #[test]
    fn test_pkey_clone_creates_copy() {
        let mut pkey = super::PKey::new();
        pkey.gen(512);
        let old_pkey_n = pkey.get_rsa().n().unwrap();

        let mut pkey2 = pkey.clone();
        pkey2.gen(512);

        assert!(old_pkey_n == pkey.get_rsa().n().unwrap());
    }
}