diff options
| author | Steven Fackler <[email protected]> | 2018-03-16 22:00:59 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2018-03-16 22:00:59 -0700 |
| commit | cf658e4c5c564cf4dc794a00a7bb20ceb0e425e3 (patch) | |
| tree | 21d9cf732de4edb4bf286c24478d030b441fda64 /openssl/src | |
| parent | Merge pull request #874 from rohit-lshift/priv-key-from-num (diff) | |
| parent | Expose additional cipher and digest accessors (diff) | |
| download | rust-openssl-cf658e4c5c564cf4dc794a00a7bb20ceb0e425e3.tar.xz rust-openssl-cf658e4c5c564cf4dc794a00a7bb20ceb0e425e3.zip | |
Merge pull request #875 from Ralith/hash-extras
Expose cipher digests and digest sizes
Diffstat (limited to 'openssl/src')
| -rw-r--r-- | openssl/src/hash.rs | 7 | ||||
| -rw-r--r-- | openssl/src/ssl/mod.rs | 26 | ||||
| -rw-r--r-- | openssl/src/symm.rs | 11 |
3 files changed, 44 insertions, 0 deletions
diff --git a/openssl/src/hash.rs b/openssl/src/hash.rs index c6d4c862..726ebe9c 100644 --- a/openssl/src/hash.rs +++ b/openssl/src/hash.rs @@ -16,6 +16,8 @@ use error::ErrorStack; pub struct MessageDigest(*const ffi::EVP_MD); impl MessageDigest { + pub unsafe fn from_ptr(x: *const ffi::EVP_MD) -> Self { MessageDigest(x) } + pub fn md5() -> MessageDigest { unsafe { MessageDigest(ffi::EVP_md5()) } } @@ -47,6 +49,11 @@ impl MessageDigest { pub fn as_ptr(&self) -> *const ffi::EVP_MD { self.0 } + + /// The size of the digest in bytes + pub fn size(&self) -> usize { + unsafe { ffi::EVP_MD_size(self.0) as usize } + } } unsafe impl Sync for MessageDigest {} diff --git a/openssl/src/ssl/mod.rs b/openssl/src/ssl/mod.rs index e2a0f156..f7f46a7f 100644 --- a/openssl/src/ssl/mod.rs +++ b/openssl/src/ssl/mod.rs @@ -96,6 +96,8 @@ use stack::{Stack, StackRef}; use ssl::bio::BioMethod; use ssl::error::InnerError; use ssl::callbacks::*; +use nid::Nid; +use hash::MessageDigest; pub use ssl::connector::{ConnectConfiguration, SslAcceptor, SslAcceptorBuilder, SslConnector, SslConnectorBuilder}; @@ -1814,6 +1816,30 @@ impl SslCipherRef { String::from_utf8(CStr::from_ptr(ptr as *const _).to_bytes().to_vec()).unwrap() } } + + /// Returns the handshake digest of the cipher. + /// + /// Available as of OpenSSL 1.1.1. This corresponds to [`SSL_CIPHER_get_handshake_digest`]. + /// + /// [`SSL_CIPHER_get_handshake_digest`]: https://www.openssl.org/docs/man1.1.1/man3/SSL_CIPHER_get_handshake_digest.html + #[cfg(all(feature = "v111", ossl111))] + pub fn handshake_digest(&self) -> Option<MessageDigest> { + unsafe { + let ptr = ffi::SSL_CIPHER_get_handshake_digest(self.as_ptr()); + if ptr.is_null() { None } else { Some(MessageDigest::from_ptr(ptr)) } + } + } + + /// Returns the NID corresponding to the cipher. + /// + /// Available as of OpenSSL 1.1.0. This corresponds to [`SSL_CIPHER_get_cipher_nid`] + /// + /// [`SSL_CIPHER_get_cipher_nid`]: https://www.openssl.org/docs/man1.1.0/ssl/SSL_CIPHER_get_cipher_nid.html + #[cfg(any(all(feature = "v110", ossl110), all(feature = "v111", ossl111)))] + pub fn cipher_nid(&self) -> Option<Nid> { + let n = unsafe { ffi::SSL_CIPHER_get_cipher_nid(self.as_ptr()) }; + if n == 0 { None } else { Some(Nid::from_raw(n)) } + } } foreign_type! { diff --git a/openssl/src/symm.rs b/openssl/src/symm.rs index 6b1d0134..a7068584 100644 --- a/openssl/src/symm.rs +++ b/openssl/src/symm.rs @@ -59,6 +59,7 @@ use ffi; use {cvt, cvt_p}; use error::ErrorStack; +use nid::Nid; #[derive(Copy, Clone)] pub enum Mode { @@ -75,6 +76,16 @@ pub enum Mode { pub struct Cipher(*const ffi::EVP_CIPHER); impl Cipher { + /// Looks up the cipher for a certain nid. + /// + /// This corresponds to [`EVP_get_cipherbynid`] + /// + /// [`EVP_get_cipherbynid`]: https://www.openssl.org/docs/man1.0.2/crypto/EVP_get_cipherbyname.html + pub fn from_nid(nid: Nid) -> Option<Cipher> { + let ptr = unsafe { ffi::EVP_get_cipherbyname(ffi::OBJ_nid2sn(nid.as_raw())) }; + if ptr.is_null() { None } else { Some(Cipher(ptr)) } + } + pub fn aes_128_ecb() -> Cipher { unsafe { Cipher(ffi::EVP_aes_128_ecb()) } } |