Skip to content

Commit

Permalink
x509-cert: make name an owned type
Browse files Browse the repository at this point in the history
  • Loading branch information
baloo committed Nov 27, 2022
1 parent 4811a2d commit 3dd5e79
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 69 deletions.
2 changes: 1 addition & 1 deletion x509-cert/src/anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub struct TrustAnchorInfo<'a> {
#[derive(Clone, Debug, Eq, PartialEq, Sequence)]
#[allow(missing_docs)]
pub struct CertPathControls<'a> {
pub ta_name: Name<'a>,
pub ta_name: Name,

#[asn1(context_specific = "0", tag_mode = "IMPLICIT", optional = "true")]
pub certificate: Option<Certificate<'a>>,
Expand Down
28 changes: 14 additions & 14 deletions x509-cert/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use const_oid::db::rfc4519::{COUNTRY_NAME, DOMAIN_COMPONENT};
use core::fmt::{self, Write};

use const_oid::db::DB;
use der::asn1::{AnyRef, ObjectIdentifier, SetOfVec};
use der::asn1::{Any, ObjectIdentifier, SetOfVec};
use der::{Decode, Encode, Error, ErrorKind, Sequence, Tag, Tagged, ValueOrd};

/// X.501 `AttributeType` as defined in [RFC 5280 Appendix A.1].
Expand All @@ -24,7 +24,7 @@ pub type AttributeType = ObjectIdentifier;
/// ```
///
/// [RFC 5280 Appendix A.1]: https://datatracker.ietf.org/doc/html/rfc5280#appendix-A.1
pub type AttributeValue<'a> = AnyRef<'a>;
pub type AttributeValue = Any;

/// X.501 `Attribute` as defined in [RFC 5280 Appendix A.1].
///
Expand All @@ -50,15 +50,15 @@ pub type AttributeValue<'a> = AnyRef<'a>;
/// [RFC 5280 Appendix A.1]: https://datatracker.ietf.org/doc/html/rfc5280#appendix-A.1
#[derive(Clone, Debug, PartialEq, Eq, Sequence, ValueOrd)]
#[allow(missing_docs)]
pub struct Attribute<'a> {
pub struct Attribute {
pub oid: AttributeType,
pub values: SetOfVec<AttributeValue<'a>>,
pub values: SetOfVec<AttributeValue>,
}

impl<'a> TryFrom<&'a [u8]> for Attribute<'a> {
impl TryFrom<&[u8]> for Attribute {
type Error = Error;

fn try_from(bytes: &'a [u8]) -> Result<Self, Self::Error> {
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
Self::from_der(bytes)
}
}
Expand All @@ -70,7 +70,7 @@ impl<'a> TryFrom<&'a [u8]> for Attribute<'a> {
/// ```
///
/// [RFC 2986 Section 4]: https://datatracker.ietf.org/doc/html/rfc2986#section-4
pub type Attributes<'a> = SetOfVec<Attribute<'a>>;
pub type Attributes = SetOfVec<Attribute>;

/// X.501 `AttributeTypeAndValue` as defined in [RFC 5280 Appendix A.1].
///
Expand All @@ -82,11 +82,11 @@ pub type Attributes<'a> = SetOfVec<Attribute<'a>>;
/// ```
///
/// [RFC 5280 Appendix A.1]: https://datatracker.ietf.org/doc/html/rfc5280#appendix-A.1
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Sequence, ValueOrd)]
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Sequence, ValueOrd)]
#[allow(missing_docs)]
pub struct AttributeTypeAndValue<'a> {
pub struct AttributeTypeAndValue {
pub oid: AttributeType,
pub value: AnyRef<'a>,
pub value: AttributeValue,
}

#[derive(Copy, Clone)]
Expand Down Expand Up @@ -141,7 +141,7 @@ impl Parser {
}
}

impl AttributeTypeAndValue<'_> {
impl AttributeTypeAndValue {
/// Parses the hex value in the `OID=#HEX` format.
fn encode_hex(oid: ObjectIdentifier, val: &str) -> Result<Vec<u8>, Error> {
// Ensure an even number of hex bytes.
Expand Down Expand Up @@ -169,7 +169,7 @@ impl AttributeTypeAndValue<'_> {
}

// Serialize.
let value = AnyRef::from_der(&bytes)?;
let value = Any::from_der(&bytes)?;
let atv = AttributeTypeAndValue { oid, value };
atv.to_vec()
}
Expand All @@ -189,7 +189,7 @@ impl AttributeTypeAndValue<'_> {
};

// Serialize.
let value = AnyRef::new(tag, parser.as_bytes())?;
let value = Any::new(tag, parser.as_bytes())?;
let atv = AttributeTypeAndValue { oid, value };
atv.to_vec()
}
Expand Down Expand Up @@ -221,7 +221,7 @@ impl AttributeTypeAndValue<'_> {
/// Serializes the structure according to the rules in [RFC 4514].
///
/// [RFC 4514]: https://datatracker.ietf.org/doc/html/rfc4514
impl fmt::Display for AttributeTypeAndValue<'_> {
impl fmt::Display for AttributeTypeAndValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let val = match self.value.tag() {
Tag::PrintableString => self.value.printable_string().ok().map(|s| s.as_str()),
Expand Down
4 changes: 2 additions & 2 deletions x509-cert/src/certificate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ pub struct TbsCertificate<'a> {

pub serial_number: UIntRef<'a>,
pub signature: AlgorithmIdentifier<'a>,
pub issuer: Name<'a>,
pub issuer: Name,
pub validity: Validity,
pub subject: Name<'a>,
pub subject: Name,
pub subject_public_key_info: SubjectPublicKeyInfo<'a>,

#[asn1(context_specific = "1", tag_mode = "IMPLICIT", optional = "true")]
Expand Down
2 changes: 1 addition & 1 deletion x509-cert/src/crl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub struct RevokedCert<'a> {
pub struct TbsCertList<'a> {
pub version: Version,
pub signature: AlgorithmIdentifier<'a>,
pub issuer: Name<'a>,
pub issuer: Name,
pub this_update: Time,
pub next_update: Option<Time>,
pub revoked_certificates: Option<Vec<RevokedCert<'a>>>,
Expand Down
9 changes: 3 additions & 6 deletions x509-cert/src/ext/pkix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,13 @@ impl_newtype!(IssuerAltName<'a>, name::GeneralNames<'a>);
///
/// [RFC 5280 Section 4.2.1.8]: https://datatracker.ietf.org/doc/html/rfc5280#section-4.2.1.8
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct SubjectDirectoryAttributes<'a>(pub Vec<AttributeTypeAndValue<'a>>);
pub struct SubjectDirectoryAttributes(pub Vec<AttributeTypeAndValue>);

impl<'a> AssociatedOid for SubjectDirectoryAttributes<'a> {
impl AssociatedOid for SubjectDirectoryAttributes {
const OID: ObjectIdentifier = ID_CE_SUBJECT_DIRECTORY_ATTRIBUTES;
}

impl_newtype!(
SubjectDirectoryAttributes<'a>,
Vec<AttributeTypeAndValue<'a>>
);
impl_newtype!(SubjectDirectoryAttributes, Vec<AttributeTypeAndValue>);

/// InhibitAnyPolicy as defined in [RFC 5280 Section 4.2.1.14].
///
Expand Down
2 changes: 1 addition & 1 deletion x509-cert/src/ext/pkix/name/dp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ pub enum DistributionPointName<'a> {
FullName(GeneralNames<'a>),

#[asn1(context_specific = "1", tag_mode = "IMPLICIT", constructed = "true")]
NameRelativeToCRLIssuer(RelativeDistinguishedName<'a>),
NameRelativeToCRLIssuer(RelativeDistinguishedName),
}
2 changes: 1 addition & 1 deletion x509-cert/src/ext/pkix/name/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub enum GeneralName<'a> {
DnsName(Ia5StringRef<'a>),

#[asn1(context_specific = "4", tag_mode = "EXPLICIT", constructed = "true")]
DirectoryName(Name<'a>),
DirectoryName(Name),

#[asn1(context_specific = "5", tag_mode = "IMPLICIT", constructed = "true")]
EdiPartyName(EdiPartyName<'a>),
Expand Down
23 changes: 10 additions & 13 deletions x509-cert/src/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use der::{asn1::SetOfVec, Decode, Encode};
/// ```
///
/// [RFC 5280 Section 4.1.2.4]: https://datatracker.ietf.org/doc/html/rfc5280#section-4.1.2.4
pub type Name<'a> = RdnSequence<'a>;
pub type Name = RdnSequence;

/// X.501 RDNSequence as defined in [RFC 5280 Section 4.1.2.4].
///
Expand All @@ -22,9 +22,9 @@ pub type Name<'a> = RdnSequence<'a>;
///
/// [RFC 5280 Section 4.1.2.4]: https://datatracker.ietf.org/doc/html/rfc5280#section-4.1.2.4
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct RdnSequence<'a>(pub Vec<RelativeDistinguishedName<'a>>);
pub struct RdnSequence(pub Vec<RelativeDistinguishedName>);

impl RdnSequence<'_> {
impl RdnSequence {
/// Converts an RDNSequence string into an encoded RDNSequence
///
/// This function follows the rules in [RFC 4514].
Expand All @@ -47,7 +47,7 @@ impl RdnSequence<'_> {
/// Serializes the structure according to the rules in [RFC 4514].
///
/// [RFC 4514]: https://datatracker.ietf.org/doc/html/rfc4514
impl fmt::Display for RdnSequence<'_> {
impl fmt::Display for RdnSequence {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (i, atv) in self.0.iter().enumerate() {
match i {
Expand All @@ -60,7 +60,7 @@ impl fmt::Display for RdnSequence<'_> {
}
}

impl_newtype!(RdnSequence<'a>, Vec<RelativeDistinguishedName<'a>>);
impl_newtype!(RdnSequence, Vec<RelativeDistinguishedName>);

/// Find the indices of all non-escaped separators.
fn find(s: &str, b: u8) -> impl '_ + Iterator<Item = usize> {
Expand Down Expand Up @@ -98,7 +98,7 @@ fn split(s: &str, b: u8) -> impl '_ + Iterator<Item = &'_ str> {
/// ```
///
/// [RFC 5280 Section 4.1.2.4]: https://datatracker.ietf.org/doc/html/rfc5280#section-4.1.2.4
pub type DistinguishedName<'a> = RdnSequence<'a>;
pub type DistinguishedName = RdnSequence;

/// RelativeDistinguishedName as defined in [RFC 5280 Section 4.1.2.4].
///
Expand All @@ -125,9 +125,9 @@ pub type DistinguishedName<'a> = RdnSequence<'a>;
///
/// [RFC 5280 Section 4.1.2.4]: https://datatracker.ietf.org/doc/html/rfc5280#section-4.1.2.4
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct RelativeDistinguishedName<'a>(pub SetOfVec<AttributeTypeAndValue<'a>>);
pub struct RelativeDistinguishedName(pub SetOfVec<AttributeTypeAndValue>);

impl RelativeDistinguishedName<'_> {
impl RelativeDistinguishedName {
/// Converts an RelativeDistinguishedName string into an encoded RelativeDistinguishedName
///
/// This function follows the rules in [RFC 4514].
Expand All @@ -150,7 +150,7 @@ impl RelativeDistinguishedName<'_> {
/// Serializes the structure according to the rules in [RFC 4514].
///
/// [RFC 4514]: https://datatracker.ietf.org/doc/html/rfc4514
impl fmt::Display for RelativeDistinguishedName<'_> {
impl fmt::Display for RelativeDistinguishedName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (i, atv) in self.0.iter().enumerate() {
match i {
Expand All @@ -163,7 +163,4 @@ impl fmt::Display for RelativeDistinguishedName<'_> {
}
}

impl_newtype!(
RelativeDistinguishedName<'a>,
SetOfVec<AttributeTypeAndValue<'a>>
);
impl_newtype!(RelativeDistinguishedName, SetOfVec<AttributeTypeAndValue>);
4 changes: 2 additions & 2 deletions x509-cert/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ pub struct CertReqInfo<'a> {
pub version: Version,

/// Subject name.
pub subject: Name<'a>,
pub subject: Name,

/// Subject public key info.
pub public_key: SubjectPublicKeyInfo<'a>,

/// Request attributes.
#[asn1(context_specific = "0", tag_mode = "IMPLICIT")]
pub attributes: Attributes<'a>,
pub attributes: Attributes,
}

impl<'a> TryFrom<&'a [u8]> for CertReqInfo<'a> {
Expand Down
Loading

0 comments on commit 3dd5e79

Please sign in to comment.