Switches to newtype wrapper for Oid

This commit is contained in:
Bastian Köcher 2018-03-08 12:24:37 +01:00
parent 724dd6f830
commit b0ea53184d
1 changed files with 27 additions and 22 deletions

View File

@ -70,14 +70,26 @@ pub enum Public {}
/// A tag type indicating that a key has private components. /// A tag type indicating that a key has private components.
pub enum Private {} pub enum Private {}
/// The OIDs that identify the type of a key. /// The Oids that identify the type of a key.
#[derive(PartialEq, Debug)] #[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum OID { pub struct Oid(c_int);
RSA,
HMAC, impl Oid {
DSA, /// Creates a `Oid` from an integer representation.
DH, pub fn from_raw(value: c_int) -> Oid {
EC, Oid(value)
}
/// Returns the integer representation of `Oid`.
pub fn as_raw(&self) -> c_int {
self.0
}
pub const RSA: Oid = Oid(ffi::EVP_PKEY_RSA);
pub const HMAC: Oid = Oid(ffi::EVP_PKEY_HMAC);
pub const DSA: Oid = Oid(ffi::EVP_PKEY_DSA);
pub const DH: Oid = Oid(ffi::EVP_PKEY_DH);
pub const EC: Oid = Oid(ffi::EVP_PKEY_EC);
} }
/// A trait indicating that a key has parameters. /// A trait indicating that a key has parameters.
@ -166,22 +178,15 @@ impl<T> PKeyRef<T> {
} }
} }
/// Returns `Some(OID)` as the type of this key or `None`, if the OID is supported by this /// Returns `Some(Oid)` as the type of this key or `None`, if the Oid is supported by this
/// library. /// library.
/// ///
/// This corresponds to [`EVP_PKEY_id`]. /// This corresponds to [`EVP_PKEY_id`].
/// ///
/// [`EVP_PKEY_id`]: https://www.openssl.org/docs/man1.1.0/crypto/EVP_PKEY_id.html /// [`EVP_PKEY_id`]: https://www.openssl.org/docs/man1.1.0/crypto/EVP_PKEY_id.html
pub fn get_id(&self) -> Option<OID> { pub fn oid(&self) -> Oid {
unsafe { unsafe {
match ffi::EVP_PKEY_id(self.as_ptr()) { Oid::from_raw(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,
}
} }
} }
} }
@ -560,7 +565,7 @@ mod tests {
let rsa = Rsa::generate(2048).unwrap(); let rsa = Rsa::generate(2048).unwrap();
let pkey = PKey::from_rsa(rsa).unwrap(); let pkey = PKey::from_rsa(rsa).unwrap();
pkey.rsa().unwrap(); pkey.rsa().unwrap();
assert_eq!(pkey.get_id().unwrap(), OID::RSA); assert_eq!(pkey.oid(), Oid::RSA);
assert!(pkey.dsa().is_err()); assert!(pkey.dsa().is_err());
} }
@ -569,7 +574,7 @@ mod tests {
let dsa = Dsa::generate(2048).unwrap(); let dsa = Dsa::generate(2048).unwrap();
let pkey = PKey::from_dsa(dsa).unwrap(); let pkey = PKey::from_dsa(dsa).unwrap();
pkey.dsa().unwrap(); pkey.dsa().unwrap();
assert_eq!(pkey.get_id().unwrap(), OID::DSA); assert_eq!(pkey.oid(), Oid::DSA);
assert!(pkey.rsa().is_err()); assert!(pkey.rsa().is_err());
} }
@ -579,7 +584,7 @@ mod tests {
let dh = Dh::params_from_pem(dh).unwrap(); let dh = Dh::params_from_pem(dh).unwrap();
let pkey = PKey::from_dh(dh).unwrap(); let pkey = PKey::from_dh(dh).unwrap();
pkey.dh().unwrap(); pkey.dh().unwrap();
assert_eq!(pkey.get_id().unwrap(), OID::DH); assert_eq!(pkey.oid(), Oid::DH);
assert!(pkey.rsa().is_err()); assert!(pkey.rsa().is_err());
} }
@ -588,7 +593,7 @@ mod tests {
let ec_key = EcKey::from_curve_name(Nid::X9_62_PRIME256V1).unwrap(); let ec_key = EcKey::from_curve_name(Nid::X9_62_PRIME256V1).unwrap();
let pkey = PKey::from_ec_key(ec_key).unwrap(); let pkey = PKey::from_ec_key(ec_key).unwrap();
pkey.ec_key().unwrap(); pkey.ec_key().unwrap();
assert_eq!(pkey.get_id().unwrap(), OID::EC); assert_eq!(pkey.oid(), Oid::EC);
assert!(pkey.rsa().is_err()); assert!(pkey.rsa().is_err());
} }
} }