pkcs12/
bag_type.rs

1//! BagType-related types
2
3use der::asn1::ObjectIdentifier;
4use der::{ErrorKind, FixedTag, Tag};
5
6/// Indicates the type of content.
7#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
8pub enum BagType {
9    /// Plain data content type
10    Key,
11
12    /// Signed-data content type
13    Pkcs8,
14
15    /// Enveloped-data content type
16    Cert,
17
18    /// Signed-and-enveloped-data content type
19    Crl,
20
21    /// Digested-data content type
22    Secret,
23
24    /// Encrypted-data content type
25    SafeContents,
26}
27
28impl FixedTag for BagType {
29    const TAG: Tag = Tag::ObjectIdentifier;
30}
31
32impl From<BagType> for ObjectIdentifier {
33    fn from(content_type: BagType) -> ObjectIdentifier {
34        match content_type {
35            BagType::Key => crate::PKCS_12_KEY_BAG_OID,
36            BagType::Pkcs8 => crate::PKCS_12_PKCS8_KEY_BAG_OID,
37            BagType::Cert => crate::PKCS_12_CERT_BAG_OID,
38            BagType::Crl => crate::PKCS_12_CRL_BAG_OID,
39            BagType::Secret => crate::PKCS_12_SECRET_BAG_OID,
40            BagType::SafeContents => crate::PKCS_12_SAFE_CONTENTS_BAG_OID,
41        }
42    }
43}
44
45impl TryFrom<ObjectIdentifier> for BagType {
46    type Error = der::Error;
47
48    fn try_from(oid: ObjectIdentifier) -> der::Result<Self> {
49        match oid {
50            crate::PKCS_12_KEY_BAG_OID => Ok(Self::Key),
51            crate::PKCS_12_PKCS8_KEY_BAG_OID => Ok(Self::Pkcs8),
52            crate::PKCS_12_CERT_BAG_OID => Ok(Self::Cert),
53            crate::PKCS_12_CRL_BAG_OID => Ok(Self::Crl),
54            crate::PKCS_12_SECRET_BAG_OID => Ok(Self::Secret),
55            crate::PKCS_12_SAFE_CONTENTS_BAG_OID => Ok(Self::SafeContents),
56            _ => Err(ErrorKind::OidUnknown { oid }.into()),
57        }
58    }
59}