Adds `PKeyRef::get_id` to get the OID of a key

This commit is contained in:
Bastian Köcher 2018-03-07 18:42:13 +01:00
parent f645165ee2
commit 9a8a1c752b
2 changed files with 34 additions and 0 deletions

View File

@ -2023,6 +2023,7 @@ extern "C" {
pub fn EVP_PKEY_get1_DH(k: *mut EVP_PKEY) -> *mut DH;
pub fn EVP_PKEY_get1_EC_KEY(k: *mut EVP_PKEY) -> *mut EC_KEY;
pub fn EVP_PKEY_cmp(a: *const EVP_PKEY, b: *const EVP_PKEY) -> c_int;
pub fn EVP_PKEY_id(pkey: *const EVP_PKEY) -> c_int;
pub fn EVP_PKEY_new_mac_key(
type_: c_int,
e: *mut ENGINE,

View File

@ -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());
}
}