Commit b0ea5318 authored by Bastian Köcher's avatar Bastian Köcher
Browse files

Switches to newtype wrapper for Oid

parent 724dd6f8
Loading
Loading
Loading
Loading
+27 −22
Original line number Diff line number Diff line
@@ -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());
    }
}