Skip to main content

at_cryptoauth/cert/
attr.rs

1//! Attribute-related definitions as defined in X.501 (and updated by RFC 5280).
2
3use der::asn1::{AnyRef, ObjectIdentifier, SetOf};
4use der::{Decode, Error, Sequence, ValueOrd};
5
6const MAX_ATTR_VALUES: usize = 1;
7const MAX_ATTR: usize = 1;
8
9/// X.501 `AttributeType` as defined in [RFC 5280 Appendix A.1].
10///
11/// ```text
12/// AttributeType           ::= OBJECT IDENTIFIER
13/// ```
14///
15/// [RFC 5280 Appendix A.1]: https://datatracker.ietf.org/doc/html/rfc5280#appendix-A.1
16pub type AttributeType = ObjectIdentifier;
17
18/// X.501 `AttributeValue` as defined in [RFC 5280 Appendix A.1].
19///
20/// ```text
21/// AttributeValue          ::= ANY
22/// ```
23///
24/// [RFC 5280 Appendix A.1]: https://datatracker.ietf.org/doc/html/rfc5280#appendix-A.1
25pub type AttributeValue<'a> = AnyRef<'a>;
26
27/// X.501 `Attribute` as defined in [RFC 5280 Appendix A.1].
28///
29/// ```text
30/// Attribute               ::= SEQUENCE {
31///     type             AttributeType,
32///     values    SET OF AttributeValue -- at least one value is required
33/// }
34/// ```
35///
36/// Note that [RFC 2986 Section 4] defines a constrained version of this type:
37///
38/// ```text
39/// Attribute { ATTRIBUTE:IOSet } ::= SEQUENCE {
40///     type   ATTRIBUTE.&id({IOSet}),
41///     values SET SIZE(1..MAX) OF ATTRIBUTE.&Type({IOSet}{@type})
42/// }
43/// ```
44///
45/// The unconstrained version should be preferred.
46///
47/// [RFC 2986 Section 4]: https://datatracker.ietf.org/doc/html/rfc2986#section-4
48/// [RFC 5280 Appendix A.1]: https://datatracker.ietf.org/doc/html/rfc5280#appendix-A.1
49#[derive(Clone, Debug, PartialEq, Eq, Sequence, ValueOrd)]
50#[allow(missing_docs)]
51pub struct Attribute<'a> {
52    pub oid: AttributeType,
53    pub values: SetOf<AttributeValue<'a>, MAX_ATTR_VALUES>,
54}
55
56impl<'a> TryFrom<&'a [u8]> for Attribute<'a> {
57    type Error = Error;
58
59    fn try_from(bytes: &'a [u8]) -> Result<Self, Self::Error> {
60        Self::from_der(bytes)
61    }
62}
63
64/// X.501 `Attributes` as defined in [RFC 2986 Section 4].
65///
66/// ```text
67/// Attributes { ATTRIBUTE:IOSet } ::= SET OF Attribute{{ IOSet }}
68/// ```
69///
70/// [RFC 2986 Section 4]: https://datatracker.ietf.org/doc/html/rfc2986#section-4
71pub type Attributes<'a> = SetOf<Attribute<'a>, MAX_ATTR>;
72
73/// X.501 `AttributeTypeAndValue` as defined in [RFC 5280 Appendix A.1].
74///
75/// ```text
76/// AttributeTypeAndValue ::= SEQUENCE {
77///   type     AttributeType,
78///   value    AttributeValue
79/// }
80/// ```
81///
82/// [RFC 5280 Appendix A.1]: https://datatracker.ietf.org/doc/html/rfc5280#appendix-A.1
83#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Sequence, ValueOrd)]
84pub struct AttributeTypeAndValue<'a> {
85    pub oid: AttributeType,
86    pub value: AnyRef<'a>,
87}