Skip to main content

at_cryptoauth/cert/
name.rs

1//! Name-related definitions as defined in X.501 (and updated by RFC 5280).
2
3use crate::impl_newtype;
4
5use super::attr::AttributeTypeAndValue;
6use der::asn1::{SequenceOf, SetOf};
7
8const MAX_RDN_SEQ: usize = 1;
9const MAX_RDN_ATTRS: usize = 1;
10
11/// X.501 Name as defined in [RFC 5280 Section 4.1.2.4]. X.501 Name is used to represent distinguished names.
12///
13/// ```text
14/// Name ::= CHOICE { rdnSequence  RDNSequence }
15/// ```
16///
17/// [RFC 5280 Section 4.1.2.4]: https://datatracker.ietf.org/doc/html/rfc5280#section-4.1.2.4
18pub type Name<'a> = RdnSequence<'a>;
19
20/// X.501 RDNSequence as defined in [RFC 5280 Section 4.1.2.4].
21///
22/// ```text
23/// RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
24/// ```
25///
26/// [RFC 5280 Section 4.1.2.4]: https://datatracker.ietf.org/doc/html/rfc5280#section-4.1.2.4
27#[derive(Clone, Debug, Default, PartialEq, Eq)]
28pub struct RdnSequence<'a>(pub SequenceOf<RelativeDistinguishedName<'a>, MAX_RDN_SEQ>);
29
30impl RdnSequence<'_> {
31    /// Is this [`RdnSequence`] empty?
32    pub fn is_empty(&self) -> bool {
33        self.0.is_empty()
34    }
35}
36
37impl_newtype!(
38    RdnSequence<'a>,
39    SequenceOf<RelativeDistinguishedName<'a>, MAX_RDN_SEQ>
40);
41
42/// X.501 DistinguishedName as defined in [RFC 5280 Section 4.1.2.4].
43///
44/// ```text
45/// DistinguishedName ::=   RDNSequence
46/// ```
47///
48/// [RFC 5280 Section 4.1.2.4]: https://datatracker.ietf.org/doc/html/rfc5280#section-4.1.2.4
49pub type DistinguishedName<'a> = RdnSequence<'a>;
50
51/// RelativeDistinguishedName as defined in [RFC 5280 Section 4.1.2.4].
52///
53/// ```text
54/// RelativeDistinguishedName ::= SET SIZE (1..MAX) OF AttributeTypeAndValue
55/// ```
56///
57/// Note that we follow the more common definition above. This technically
58/// differs from the definition in X.501, which is:
59///
60/// ```text
61/// RelativeDistinguishedName ::= SET SIZE (1..MAX) OF AttributeTypeAndDistinguishedValue
62///
63/// AttributeTypeAndDistinguishedValue ::= SEQUENCE {
64///     type ATTRIBUTE.&id ({SupportedAttributes}),
65///     value ATTRIBUTE.&Type({SupportedAttributes}{@type}),
66///     primaryDistinguished BOOLEAN DEFAULT TRUE,
67///     valuesWithContext SET SIZE (1..MAX) OF SEQUENCE {
68///         distingAttrValue [0] ATTRIBUTE.&Type ({SupportedAttributes}{@type}) OPTIONAL,
69///         contextList SET SIZE (1..MAX) OF Context
70///     } OPTIONAL
71/// }
72/// ```
73///
74/// [RFC 5280 Section 4.1.2.4]: https://datatracker.ietf.org/doc/html/rfc5280#section-4.1.2.4
75#[derive(Clone, Debug, Default, PartialEq, Eq)]
76pub struct RelativeDistinguishedName<'a>(pub SetOf<AttributeTypeAndValue<'a>, MAX_RDN_ATTRS>);
77
78impl_newtype!(
79    RelativeDistinguishedName<'a>,
80    SetOf<AttributeTypeAndValue<'a>, MAX_RDN_ATTRS>
81);