diff options
author | Bastian Köcher <git@kchr.de> | 2018-03-07 18:42:13 +0100 |
---|---|---|
committer | Bastian Köcher <git@kchr.de> | 2018-03-07 18:42:13 +0100 |
commit | 9a8a1c752b05c0421a032f7f835e8f95ce15df47 (patch) | |
tree | a7e9541caffff630f4cf920d6431d5cb55927fc2 /openssl/src/pkey.rs | |
parent | f645165ee2b41f9c15d9a7d8f3eec56000b7e445 (diff) | |
download | rust-openssl-9a8a1c752b05c0421a032f7f835e8f95ce15df47.zip |
Adds `PKeyRef::get_id` to get the OID of a key
Diffstat (limited to 'openssl/src/pkey.rs')
-rw-r--r-- | openssl/src/pkey.rs | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/openssl/src/pkey.rs b/openssl/src/pkey.rs index 8f7c4e4e..252f0e22 100644 --- a/openssl/src/pkey.rs +++ b/openssl/src/pkey.rs @@ -70,6 +70,16 @@ pub enum Public {} /// A tag type indicating that a key has private components. pub enum Private {} +/// The OIDs that identify the type of a key. +#[derive(PartialEq, Debug)] +pub enum OID { + RSA, + HMAC, + DSA, + DH, + EC, +} + /// A trait indicating that a key has parameters. pub unsafe trait HasParams {} @@ -155,6 +165,25 @@ impl<T> PKeyRef<T> { Ok(EcKey::from_ptr(ec_key)) } } + + /// Returns `Some(OID)` as the type of this key or `None`, if the OID is supported by this + /// library. + /// + /// This corresponds to [`EVP_PKEY_id`]. + /// + /// [`EVP_PKEY_id`]: https://www.openssl.org/docs/man1.1.0/crypto/EVP_PKEY_id.html + pub fn get_id(&self) -> Option<OID> { + unsafe { + match ffi::EVP_PKEY_id(self.as_ptr()) { + ffi::EVP_PKEY_RSA => Some(OID::RSA), + ffi::EVP_PKEY_HMAC => Some(OID::HMAC), + ffi::EVP_PKEY_DSA => Some(OID::DSA), + ffi::EVP_PKEY_DH => Some(OID::DH), + ffi::EVP_PKEY_EC => Some(OID::EC), + _ => None, + } + } + } } impl<T> PKeyRef<T> @@ -531,6 +560,7 @@ mod tests { let rsa = Rsa::generate(2048).unwrap(); let pkey = PKey::from_rsa(rsa).unwrap(); pkey.rsa().unwrap(); + assert_eq!(pkey.get_id().unwrap(), OID::RSA); assert!(pkey.dsa().is_err()); } @@ -539,6 +569,7 @@ mod tests { let dsa = Dsa::generate(2048).unwrap(); let pkey = PKey::from_dsa(dsa).unwrap(); pkey.dsa().unwrap(); + assert_eq!(pkey.get_id().unwrap(), OID::DSA); assert!(pkey.rsa().is_err()); } @@ -548,6 +579,7 @@ mod tests { let dh = Dh::params_from_pem(dh).unwrap(); let pkey = PKey::from_dh(dh).unwrap(); pkey.dh().unwrap(); + assert_eq!(pkey.get_id().unwrap(), OID::DH); assert!(pkey.rsa().is_err()); } @@ -556,6 +588,7 @@ mod tests { let ec_key = EcKey::from_curve_name(Nid::X9_62_PRIME256V1).unwrap(); let pkey = PKey::from_ec_key(ec_key).unwrap(); pkey.ec_key().unwrap(); + assert_eq!(pkey.get_id().unwrap(), OID::EC); assert!(pkey.rsa().is_err()); } } |