From 9b5c62b053fb84dc233c69ba1700bee9b8a4387f Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sat, 12 Nov 2016 11:00:15 +0000 Subject: [PATCH] Add PKey::bits --- openssl-sys/src/ossl10x.rs | 1 + openssl-sys/src/ossl110.rs | 1 + openssl/src/pkey.rs | 9 +++++++++ 3 files changed, 11 insertions(+) diff --git a/openssl-sys/src/ossl10x.rs b/openssl-sys/src/ossl10x.rs index 0cc75fca..baab16ac 100644 --- a/openssl-sys/src/ossl10x.rs +++ b/openssl-sys/src/ossl10x.rs @@ -600,6 +600,7 @@ extern { line: c_int) -> c_int; pub fn EVP_MD_CTX_create() -> *mut EVP_MD_CTX; pub fn EVP_MD_CTX_destroy(ctx: *mut EVP_MD_CTX); + pub fn EVP_PKEY_bits(key: *mut EVP_PKEY) -> c_int; pub fn sk_num(st: *const _STACK) -> c_int; pub fn sk_value(st: *const _STACK, n: c_int) -> *mut c_void; diff --git a/openssl-sys/src/ossl110.rs b/openssl-sys/src/ossl110.rs index e8d62d73..46df2d0e 100644 --- a/openssl-sys/src/ossl110.rs +++ b/openssl-sys/src/ossl110.rs @@ -147,6 +147,7 @@ extern { pub fn X509_STORE_CTX_get0_chain(ctx: *mut ::X509_STORE_CTX) -> *mut stack_st_X509; pub fn EVP_MD_CTX_new() -> *mut EVP_MD_CTX; pub fn EVP_MD_CTX_free(ctx: *mut EVP_MD_CTX); + pub fn EVP_PKEY_bits(key: *const EVP_PKEY) -> c_int; pub fn OpenSSL_version_num() -> c_ulong; pub fn OpenSSL_version(key: c_int) -> *const c_char; diff --git a/openssl/src/pkey.rs b/openssl/src/pkey.rs index b1c196bd..c9772d52 100644 --- a/openssl/src/pkey.rs +++ b/openssl/src/pkey.rs @@ -92,6 +92,15 @@ impl PKeyRef { Ok(mem_bio.get_buf().to_owned()) } + /// Returns the size of the key. + /// + /// This corresponds to the bit length of the modulus of an RSA key, and the bit length of the + /// group order for an elliptic curve key, for example. + pub fn bits(&self) -> usize { + unsafe { ffi::EVP_PKEY_bits(self.as_ptr()) as usize } + } + + /// Compares the public component of this key with another. pub fn public_eq(&self, other: &PKeyRef) -> bool { unsafe { ffi::EVP_PKEY_cmp(self.as_ptr(), other.as_ptr()) == 1 } }