|
2 | 2 |
|
3 | 3 | use crate::attr::AttributeTypeAndValue;
|
4 | 4 | use alloc::vec::Vec;
|
| 5 | +use const_oid::{ |
| 6 | + db::{rfc3280, rfc4519}, |
| 7 | + ObjectIdentifier, |
| 8 | +}; |
5 | 9 | use core::{fmt, str::FromStr};
|
6 |
| -use der::{asn1::SetOfVec, Encode}; |
| 10 | +use der::{ |
| 11 | + asn1::{Any, PrintableStringRef, SetOfVec}, |
| 12 | + Encode, |
| 13 | +}; |
7 | 14 |
|
8 | 15 | /// X.501 Name as defined in [RFC 5280 Section 4.1.2.4]. X.501 Name is used to represent distinguished names.
|
9 | 16 | ///
|
10 | 17 | /// ```text
|
11 | 18 | /// Name ::= CHOICE { rdnSequence RDNSequence }
|
12 | 19 | /// ```
|
13 | 20 | ///
|
| 21 | +/// # Example |
| 22 | +/// |
| 23 | +/// ``` |
| 24 | +/// use std::str::FromStr; |
| 25 | +/// use x509_cert::name::Name; |
| 26 | +/// |
| 27 | +/// let subject = Name::from_str("CN=example.com").expect("correctly formatted subject"); |
| 28 | +/// ``` |
| 29 | +/// |
14 | 30 | /// [RFC 5280 Section 4.1.2.4]: https://datatracker.ietf.org/doc/html/rfc5280#section-4.1.2.4
|
15 |
| -pub type Name = RdnSequence; |
| 31 | +#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] |
| 32 | +#[derive(Clone, Debug, Default, PartialEq, Eq)] |
| 33 | +pub struct Name(RdnSequence); |
| 34 | + |
| 35 | +impl_newtype!(Name, RdnSequence); |
| 36 | + |
| 37 | +impl Name { |
| 38 | + /// Is this [`Name`] empty? |
| 39 | + #[inline] |
| 40 | + pub fn is_empty(&self) -> bool { |
| 41 | + self.0.is_empty() |
| 42 | + } |
| 43 | + |
| 44 | + /// Returns the number of [`RelativeDistinguishedName`] elements in this [`Name`]. |
| 45 | + pub fn len(&self) -> usize { |
| 46 | + self.0 .0.len() |
| 47 | + } |
| 48 | + |
| 49 | + /// Returns an iterator over the inner [`AttributeTypeAndValue`]s. |
| 50 | + /// |
| 51 | + /// This iterator does not expose which attributes are grouped together as |
| 52 | + /// [`RelativeDistinguishedName`]s. If you need this, use [`Self::iter_rdn`]. |
| 53 | + #[inline] |
| 54 | + pub fn iter(&self) -> impl Iterator<Item = &'_ AttributeTypeAndValue> + '_ { |
| 55 | + self.0 .0.iter().flat_map(move |rdn| rdn.0.as_slice()) |
| 56 | + } |
| 57 | + |
| 58 | + /// Returns an iterator over the inner [`RelativeDistinguishedName`]s. |
| 59 | + #[inline] |
| 60 | + pub fn iter_rdn(&self) -> impl Iterator<Item = &'_ RelativeDistinguishedName> + '_ { |
| 61 | + self.0 .0.iter() |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +impl Name { |
| 66 | + /// Returns the element found in the name identified by `oid` |
| 67 | + /// |
| 68 | + /// This will return `Ok(None)` if no such element is present. |
| 69 | + /// |
| 70 | + /// # Errors |
| 71 | + /// |
| 72 | + /// This will return [`der::Error`] if the content is not serialized as expected. |
| 73 | + pub fn by_oid<'a, T>(&'a self, oid: ObjectIdentifier) -> der::Result<Option<T>> |
| 74 | + where |
| 75 | + T: TryFrom<&'a Any, Error = der::Error>, |
| 76 | + { |
| 77 | + self.iter() |
| 78 | + .filter(|atav| atav.oid == oid) |
| 79 | + .map(|atav| T::try_from(&atav.value)) |
| 80 | + .next() |
| 81 | + .transpose() |
| 82 | + } |
| 83 | + |
| 84 | + /// Returns the Common Name (CN) found in the name. |
| 85 | + /// |
| 86 | + /// This will return `Ok(None)` if no CN is found. |
| 87 | + /// |
| 88 | + /// # Errors |
| 89 | + /// |
| 90 | + /// This will return [`der::Error`] if the content is not serialized as a printableString. |
| 91 | + pub fn common_name(&self) -> der::Result<Option<PrintableStringRef<'_>>> { |
| 92 | + self.by_oid::<PrintableStringRef<'_>>(rfc4519::COMMON_NAME) |
| 93 | + } |
| 94 | + |
| 95 | + /// Returns the Country (C) found in the name. |
| 96 | + /// |
| 97 | + /// This will return `Ok(None)` if no Country is found. |
| 98 | + /// |
| 99 | + /// # Errors |
| 100 | + /// |
| 101 | + /// This will return [`der::Error`] if the content is not serialized as a printableString. |
| 102 | + pub fn country(&self) -> der::Result<Option<PrintableStringRef<'_>>> { |
| 103 | + self.by_oid::<PrintableStringRef<'_>>(rfc4519::COUNTRY) |
| 104 | + } |
| 105 | + |
| 106 | + /// Returns the State or Province (ST) found in the name. |
| 107 | + /// |
| 108 | + /// This will return `Ok(None)` if no State or Province is found. |
| 109 | + /// |
| 110 | + /// # Errors |
| 111 | + /// |
| 112 | + /// This will return [`der::Error`] if the content is not serialized as a printableString. |
| 113 | + pub fn state_or_province(&self) -> der::Result<Option<PrintableStringRef<'_>>> { |
| 114 | + self.by_oid::<PrintableStringRef<'_>>(rfc4519::ST) |
| 115 | + } |
| 116 | + |
| 117 | + /// Returns the Locality (L) found in the name. |
| 118 | + /// |
| 119 | + /// This will return `Ok(None)` if no Locality is found. |
| 120 | + /// |
| 121 | + /// # Errors |
| 122 | + /// |
| 123 | + /// This will return [`der::Error`] if the content is not serialized as a printableString. |
| 124 | + pub fn locality(&self) -> der::Result<Option<PrintableStringRef<'_>>> { |
| 125 | + self.by_oid::<PrintableStringRef<'_>>(rfc4519::LOCALITY) |
| 126 | + } |
| 127 | + |
| 128 | + /// Returns the Organization (O) found in the name. |
| 129 | + /// |
| 130 | + /// This will return `Ok(None)` if no Organization is found. |
| 131 | + /// |
| 132 | + /// # Errors |
| 133 | + /// |
| 134 | + /// This will return [`der::Error`] if the content is not serialized as a printableString. |
| 135 | + pub fn organization(&self) -> der::Result<Option<PrintableStringRef<'_>>> { |
| 136 | + self.by_oid::<PrintableStringRef<'_>>(rfc4519::ORGANIZATION) |
| 137 | + } |
| 138 | + |
| 139 | + /// Returns the Organization Unit (OU) found in the name. |
| 140 | + /// |
| 141 | + /// This will return `Ok(None)` if no Organization Unit is found. |
| 142 | + /// |
| 143 | + /// # Errors |
| 144 | + /// |
| 145 | + /// This will return [`der::Error`] if the content is not serialized as a printableString. |
| 146 | + pub fn organization_unit(&self) -> der::Result<Option<PrintableStringRef<'_>>> { |
| 147 | + self.by_oid::<PrintableStringRef<'_>>(rfc4519::ORGANIZATIONAL_UNIT) |
| 148 | + } |
| 149 | + |
| 150 | + /// Returns the Email Address (emailAddress) found in the name. |
| 151 | + /// |
| 152 | + /// This will return `Ok(None)` if no email address is found. |
| 153 | + /// |
| 154 | + /// # Errors |
| 155 | + /// |
| 156 | + /// This will return [`der::Error`] if the content is not serialized as a printableString. |
| 157 | + pub fn email_address(&self) -> der::Result<Option<PrintableStringRef<'_>>> { |
| 158 | + self.by_oid::<PrintableStringRef<'_>>(rfc3280::EMAIL_ADDRESS) |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +/// Parse a [`Name`] string. |
| 163 | +/// |
| 164 | +/// Follows the rules in [RFC 4514]. |
| 165 | +/// |
| 166 | +/// [RFC 4514]: https://datatracker.ietf.org/doc/html/rfc4514 |
| 167 | +impl FromStr for Name { |
| 168 | + type Err = der::Error; |
| 169 | + |
| 170 | + fn from_str(s: &str) -> der::Result<Self> { |
| 171 | + Ok(Self(RdnSequence::from_str(s)?)) |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +/// Serializes the name according to the rules in [RFC 4514]. |
| 176 | +/// |
| 177 | +/// [RFC 4514]: https://datatracker.ietf.org/doc/html/rfc4514 |
| 178 | +impl fmt::Display for Name { |
| 179 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 180 | + self.0.fmt(f) |
| 181 | + } |
| 182 | +} |
16 | 183 |
|
17 | 184 | /// X.501 RDNSequence as defined in [RFC 5280 Section 4.1.2.4].
|
18 | 185 | ///
|
|
0 commit comments