Skip to main content
This is unreleased documentation for the main (development) branch of crypto-glue.

Name

Struct Name 

Source
pub struct Name(/* private fields */);
Expand description

X.501 Name as defined in RFC 5280 Section 4.1.2.4. X.501 Name is used to represent distinguished names.

Name ::= CHOICE { rdnSequence  RDNSequence }

To build name, the syntax described in RFC 4514 Section 3 is expected.

The following attribute names are recognized:

     String  X.500 AttributeType
     ------  --------------------------------------------
     CN      commonName (2.5.4.3)
     L       localityName (2.5.4.7)
     ST      stateOrProvinceName (2.5.4.8)
     O       organizationName (2.5.4.10)
     OU      organizationalUnitName (2.5.4.11)
     C       countryName (2.5.4.6)
     STREET  streetAddress (2.5.4.9)
     DC      domainComponent (0.9.2342.19200300.100.1.25)
     UID     userId (0.9.2342.19200300.100.1.1)

§Example

use std::str::FromStr;
use x509_cert::name::Name;

// Multiple syntaxes are supported by `from_str`:
let subject = Name::from_str("CN=example.com").unwrap();
let subject = Name::from_str("C=US; ST=California; L=Los Angeles; O=InternetCorporationforAssignedNamesandNumbers; CN=www.example.org").unwrap();
let subject = Name::from_str("C=US,ST=California,L=Los Angeles,O=InternetCorporationforAssignedNamesandNumbers,CN=www.example.org").unwrap();
let subject = Name::from_str("C=US/ST=California/L=Los Angeles/O=InternetCorporationforAssignedNamesandNumbers/CN=www.example.org").unwrap();
let subject = Name::from_str("UID=jsmith,DC=example,DC=net").unwrap();
let subject = Name::from_str("OU=Sales+CN=J.  Smith,DC=example,DC=net").unwrap();
let subject = Name::from_str(r#"CN=James \"Jim\" Smith\, III,DC=example,DC=net"#).unwrap();
let subject = Name::from_str(r#"CN=Before\0dAfter,DC=example,DC=net"#).unwrap();
let subject = Name::from_str("1.3.6.1.4.1.1466.0=#04024869").unwrap();

Implementations§

Source§

impl Name

Source

pub fn hazmat_from_rdn_sequence(value: RdnSequence) -> Self

Build a name from an RdnSequence.

This is provided as an escape hatch (see RFC 5280 Section 4.1.2.4) to build names from bmpString, TeletexString, or UniversalString:

When CAs have previously issued certificates with issuer fields with
attributes encoded using TeletexString, BMPString, or
UniversalString, then the CA MAY continue to use these encodings of
the DirectoryString to preserve backward compatibility.
§Safety

As the name implies, this is a dangerous helper. You are responsible for ensuring the RdnSequence complies with the RFC.

Source§

impl Name

Source

pub fn is_empty(&self) -> bool

Is this Name empty?

Source

pub fn len(&self) -> usize

Returns the number of RelativeDistinguishedName elements in this Name.

Source

pub fn iter(&self) -> impl Iterator<Item = &AttributeTypeAndValue> + '_

Returns an iterator over the inner AttributeTypeAndValues.

This iterator does not expose which attributes are grouped together as RelativeDistinguishedNames. If you need this, use Self::iter_rdn.

Source

pub fn iter_rdn(&self) -> impl Iterator<Item = &RelativeDistinguishedName> + '_

Returns an iterator over the inner RelativeDistinguishedNames.

Source§

impl Name

Source

pub fn by_oid<'a, T>(&'a self, oid: ObjectIdentifier) -> Result<Option<T>>
where T: TryFrom<&'a Any, Error = Error> + Debug,

Returns the element found in the name identified by oid

This will return Ok(None) if no such element is present.

If more than one attribute is present with the specified OID, only the first attribute is returned. Later elements should be fetched using Name::iter.

§Errors

This will return der::Error if the content is not serialized as expected

Source

pub fn common_name(&self) -> Result<Option<DirectoryString>>

Returns the Common Name (CN) found in the name.

This will return Ok(None) if no CN is found.

If more than one value is present, only the first is returned. Later elements should be fetched using Name::iter.

§Errors

This will return der::Error if the content is not serialized as a string.

Source

pub fn country(&self) -> Result<Option<PrintableStringRef<'_>>>

Returns the Country (C) found in the name.

This will return Ok(None) if no Country is found.

If more than one value is present, only the first is returned. Later elements should be fetched using Name::iter.

§Errors

This will return der::Error if the content is not serialized as a printableString.

Source

pub fn state_or_province(&self) -> Result<Option<DirectoryString>>

Returns the State or Province (ST) found in the name.

This will return Ok(None) if no State or Province is found.

If more than one value is present, only the first is returned. Later elements should be fetched using Name::iter.

§Errors

This will return der::Error if the content is not serialized as a string.

Source

pub fn locality(&self) -> Result<Option<DirectoryString>>

Returns the Locality (L) found in the name.

This will return Ok(None) if no Locality is found.

If more than one value is present, only the first is returned. Later elements should be fetched using Name::iter.

§Errors

This will return der::Error if the content is not serialized as a string.

Source

pub fn organization(&self) -> Result<Option<DirectoryString>>

Returns the Organization (O) found in the name.

This will return Ok(None) if no Organization is found.

If more than one value is present, only the first is returned. Later elements should be fetched using Name::iter.

§Errors

This will return der::Error if the content is not serialized as a string.

Source

pub fn organization_unit(&self) -> Result<Option<DirectoryString>>

Returns the Organization Unit (OU) found in the name.

This will return Ok(None) if no Organization Unit is found.

If more than one value is present, only the first is returned. Later elements should be fetched using Name::iter.

§Errors

This will return der::Error if the content is not serialized as a string.

Source

pub fn email_address(&self) -> Result<Option<Ia5StringRef<'_>>>

Returns the Email Address (emailAddress) found in the name.

This will return Ok(None) if no email address is found.

If more than one value is present, only the first is returned. Later elements should be fetched using Name::iter.

§Errors

This will return der::Error if the content is not serialized as an ia5String.

Trait Implementations§

Source§

impl AsRef<RdnSequence> for Name

Source§

fn as_ref(&self) -> &RdnSequence

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for Name

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Name

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> DecodeValue<'a> for Name

Source§

type Error = Error

Type returned in the event of a decoding error.
Source§

fn decode_value<R: Reader<'a>>(decoder: &mut R, header: Header) -> Result<Self>

Attempt to decode this value using the provided Reader. Read more
Source§

impl Default for Name

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for Name

Serializes the name according to the rules in RFC 4514.

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl EncodeValue for Name

Source§

fn encode_value(&self, encoder: &mut impl Writer) -> Result<()>

Encode value (sans [Tag]+Length header) as ASN.1 DER using the provided Writer. Read more
Source§

fn value_len(&self) -> Result<Length>

Compute the length of this value (sans [Tag]+Length header) when encoded as ASN.1 DER. Read more
Source§

fn header(&self) -> Result<Header, Error>
where Self: Tagged,

Get the Header used to encode this value. Read more
Source§

impl Eq for Name

Source§

impl FixedTag for Name

Source§

const TAG: Tag = <RdnSequence as FixedTag>::TAG

ASN.1 tag
Source§

impl From<Name> for RdnSequence

Source§

fn from(value: Name) -> Self

Converts to this type from the input type.
Source§

impl FromStr for Name

Parse a Name string.

Follows the rules in RFC 4514.

Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for Name

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Name

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Name

Source§

impl ValueOrd for Name

Source§

fn value_cmp(&self, other: &Self) -> Result<Ordering>

Return an Ordering between value portion of TLV-encoded self and other when serialized as ASN.1 DER. Read more

Auto Trait Implementations§

§

impl Freeze for Name

§

impl RefUnwindSafe for Name

§

impl Send for Name

§

impl Sync for Name

§

impl Unpin for Name

§

impl UnsafeUnpin for Name

§

impl UnwindSafe for Name

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<'a, T> Choice<'a> for T
where T: Decode<'a> + FixedTag,

Source§

fn can_decode(tag: Tag) -> bool

Is the provided Tag decodable as a variant of this CHOICE?
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<'a, T> Decode<'a> for T
where T: DecodeValue<'a> + FixedTag + 'a,

Source§

type Error = <T as DecodeValue<'a>>::Error

Type returned in the event of a decoding error.
Source§

fn decode<R>(reader: &mut R) -> Result<T, <T as DecodeValue<'a>>::Error>
where R: Reader<'a>,

Attempt to decode this TLV message using the provided decoder. Read more
Source§

fn from_der(bytes: &'a [u8]) -> Result<Self, Self::Error>

Parse Self from the provided DER-encoded byte slice. Read more
Source§

fn from_der_partial(bytes: &'a [u8]) -> Result<(Self, &'a [u8]), Self::Error>

Parse Self from the provided DER-encoded byte slice. Read more
Source§

impl<T> DecodeOwned for T
where T: for<'a> Decode<'a>,

Source§

impl<T> DerOrd for T

Source§

fn der_cmp(&self, other: &T) -> Result<Ordering, Error>

Return an Ordering between self and other when serialized as ASN.1 DER. Read more
Source§

impl<T> Encode for T
where T: EncodeValue + Tagged + ?Sized,

Source§

fn encoded_len(&self) -> Result<Length, Error>

Compute the length of this TLV object in bytes when encoded as ASN.1 DER. Read more
Source§

fn encode(&self, writer: &mut impl Writer) -> Result<(), Error>

Encode this TLV object as ASN.1 DER using the provided Writer. Read more
Source§

fn encode_to_slice<'a>(&self, buf: &'a mut [u8]) -> Result<&'a [u8], Error>

Encode this TLV object to the provided byte slice, returning a sub-slice containing the encoded message. Read more
Source§

fn encode_to_vec(&self, buf: &mut Vec<u8>) -> Result<Length, Error>

Encode this TLV object as ASN.1 DER, appending it to the provided byte vector. Read more
Source§

fn to_der(&self) -> Result<Vec<u8>, Error>

Encode this TLV object as ASN.1 DER, returning a byte vector. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IsConstructed for T
where T: FixedTag + ?Sized,

Source§

const CONSTRUCTED: bool

ASN.1 constructed bit
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> Tagged for T
where T: FixedTag + ?Sized,

Source§

fn tag(&self) -> Tag

Get the ASN.1 tag that this type is encoded with.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.