summaryrefslogtreecommitdiff
path: root/openssl/src/pkey.rs
diff options
context:
space:
mode:
authorBastian Köcher <git@kchr.de>2018-03-08 12:24:37 +0100
committerBastian Köcher <git@kchr.de>2018-03-08 12:24:37 +0100
commitb0ea53184d980357222ca0619985f6318f4974da (patch)
treec3429776a76a98cb8c18118e057e989c987716df /openssl/src/pkey.rs
parent724dd6f830d64de6fc1e6f55b8b0a9f630d928f5 (diff)
downloadrust-openssl-b0ea53184d980357222ca0619985f6318f4974da.zip
Switches to newtype wrapper for Oid
Diffstat (limited to 'openssl/src/pkey.rs')
-rw-r--r--openssl/src/pkey.rs49
1 files changed, 27 insertions, 22 deletions
diff --git a/openssl/src/pkey.rs b/openssl/src/pkey.rs
index 252f0e22..76ecef7c 100644
--- a/openssl/src/pkey.rs
+++ b/openssl/src/pkey.rs
@@ -70,14 +70,26 @@ 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,
+/// The Oids that identify the type of a key.
+#[derive(Debug, Copy, Clone, PartialEq, Eq)]
+pub struct Oid(c_int);
+
+impl Oid {
+ /// Creates a `Oid` from an integer representation.
+ pub fn from_raw(value: c_int) -> Oid {
+ 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.
@@ -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.
///
/// 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> {
+ pub fn oid(&self) -> 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,
- }
+ Oid::from_raw(ffi::EVP_PKEY_id(self.as_ptr()))
}
}
}
@@ -560,7 +565,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_eq!(pkey.oid(), Oid::RSA);
assert!(pkey.dsa().is_err());
}
@@ -569,7 +574,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_eq!(pkey.oid(), Oid::DSA);
assert!(pkey.rsa().is_err());
}
@@ -579,7 +584,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_eq!(pkey.oid(), Oid::DH);
assert!(pkey.rsa().is_err());
}
@@ -588,7 +593,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_eq!(pkey.oid(), Oid::EC);
assert!(pkey.rsa().is_err());
}
}