Skip to content

Commit cd16b39

Browse files
committed
x509-cert: remove From<RdnSequence> for Name
1 parent 5629c4f commit cd16b39

File tree

2 files changed

+72
-16
lines changed

2 files changed

+72
-16
lines changed

x509-cert/src/builder/profile/cabf/tls.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::{
2222
},
2323
AsExtension, Extension,
2424
},
25-
name::{Name, RdnSequence, RelativeDistinguishedName},
25+
name::{Name, RelativeDistinguishedName},
2626
};
2727
use spki::SubjectPublicKeyInfoRef;
2828

@@ -160,8 +160,7 @@ impl CertificateType {
160160
.filter(|rdn| !rdn.0.is_empty())
161161
.collect();
162162

163-
let subject: RdnSequence = rdns.into();
164-
let subject: Name = subject.into();
163+
let subject: Name = Name(rdns.into());
165164

166165
Ok(Self::DomainValidated(DomainValidated { subject, names }))
167166
}

x509-cert/src/name.rs

+70-13
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ use const_oid::{
66
db::{rfc3280, rfc4519},
77
ObjectIdentifier,
88
};
9-
use core::{fmt, str::FromStr};
9+
use core::{cmp::Ordering, fmt, str::FromStr};
1010
use der::{
1111
asn1::{Any, Ia5StringRef, PrintableStringRef, SetOfVec},
12-
Encode,
12+
DecodeValue, Encode, EncodeValue, FixedTag, Header, Length, Reader, Tag, ValueOrd, Writer,
1313
};
1414

1515
/// X.501 Name as defined in [RFC 5280 Section 4.1.2.4]. X.501 Name is used to represent distinguished names.
@@ -42,17 +42,74 @@ use der::{
4242
/// [RFC 5280 Section 4.1.2.4]: https://datatracker.ietf.org/doc/html/rfc5280#section-4.1.2.4
4343
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
4444
#[derive(Clone, Debug, Default, PartialEq, Eq)]
45-
pub struct Name(RdnSequence);
46-
47-
// This will implement `From<RdnSequence>` which is provided as an escape hatch to build names
48-
// from `bmpString`, `TeletexString`, or `UniversalString`:
49-
// ```
50-
// When CAs have previously issued certificates with issuer fields with
51-
// attributes encoded using TeletexString, BMPString, or
52-
// UniversalString, then the CA MAY continue to use these encodings of
53-
// the DirectoryString to preserve backward compatibility.
54-
// ```
55-
impl_newtype!(Name, RdnSequence);
45+
pub struct Name(pub(crate) RdnSequence);
46+
47+
impl Name {
48+
/// Build a name from an [`RdnSequence`].
49+
///
50+
///
51+
/// This is provided as an escape hatch (see [RFC 5280 Section 4.1.2.4]) to build
52+
/// names from `bmpString`, `TeletexString`, or `UniversalString`:
53+
/// ```text
54+
/// When CAs have previously issued certificates with issuer fields with
55+
/// attributes encoded using TeletexString, BMPString, or
56+
/// UniversalString, then the CA MAY continue to use these encodings of
57+
/// the DirectoryString to preserve backward compatibility.
58+
/// ```
59+
///
60+
/// # Safety
61+
///
62+
/// As the name implies, this is a dangerous helper. You are responsible for ensuring the
63+
/// [`RdnSequence`] complies with the RFC.
64+
///
65+
/// [RFC 5280 Section 4.1.2.4]: https://datatracker.ietf.org/doc/html/rfc5280#section-4.1.2.4
66+
#[cfg(feature = "hazmat")]
67+
pub fn hazmat_from_rdn_sequence(value: RdnSequence) -> Self {
68+
Self(value)
69+
}
70+
}
71+
72+
impl From<Name> for RdnSequence {
73+
#[inline]
74+
fn from(value: Name) -> Self {
75+
value.0
76+
}
77+
}
78+
79+
impl AsRef<RdnSequence> for Name {
80+
#[inline]
81+
fn as_ref(&self) -> &RdnSequence {
82+
&self.0
83+
}
84+
}
85+
86+
impl FixedTag for Name {
87+
const TAG: Tag = <RdnSequence as FixedTag>::TAG;
88+
}
89+
90+
impl<'a> DecodeValue<'a> for Name {
91+
type Error = der::Error;
92+
93+
fn decode_value<R: Reader<'a>>(decoder: &mut R, header: Header) -> der::Result<Self> {
94+
Ok(Self(RdnSequence::decode_value(decoder, header)?))
95+
}
96+
}
97+
98+
impl EncodeValue for Name {
99+
fn encode_value(&self, encoder: &mut impl Writer) -> der::Result<()> {
100+
self.0.encode_value(encoder)
101+
}
102+
103+
fn value_len(&self) -> der::Result<Length> {
104+
self.0.value_len()
105+
}
106+
}
107+
108+
impl ValueOrd for Name {
109+
fn value_cmp(&self, other: &Self) -> der::Result<Ordering> {
110+
self.0.value_cmp(&other.0)
111+
}
112+
}
56113

57114
impl Name {
58115
/// Is this [`Name`] empty?

0 commit comments

Comments
 (0)