Skip to main content

TrustAnchor

Struct TrustAnchor 

Source
#[non_exhaustive]
pub struct TrustAnchor { pub subject: Name, pub subject_public_key_info: SubjectPublicKeyInfoOwned, pub name_constraints: Option<NameConstraints>, }
Expand description

A trust anchor used to terminate path validation.

A trust anchor is typically either a self-signed root CA certificate or a raw (name, SPKI) pair extracted from a platform trust store. The trust anchor itself is not signature-verified — it is trusted by definition (RFC 5280 §6.1.1(c)).

Validity period: RFC 5280 §6.1.1(c) explicitly excludes the trust anchor’s notBefore/notAfter from path validation. An expired root CA certificate used as a trust anchor will still anchor valid paths — this is intentional behavior, not a bug. Callers are responsible for ensuring their trust store contains the anchors they intend to trust.

PartialEq is byte-level, not semantic: The derived PartialEq compares fields verbatim. Two anchors representing the same CA may compare unequal if their DER encodings differ — for example, one AlgorithmIdentifier with explicit NULL parameters and another with absent parameters are both valid for RSA (RFC 3279 §2.3.1) but will not be equal under ==. Do not use == to deduplicate a trust store; use names_match and compare algorithm.oid plus subject_public_key bytes directly. Path validation already handles this internally, so it is not affected by this encoding difference.

§Stability

TrustAnchor is #[non_exhaustive]: new fields may be added in minor versions. Construct via TrustAnchor::new, TrustAnchor::from_cert, or TrustAnchor::from/try_from. Do not use struct literal syntax.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§subject: Name

The subject distinguished name of the trust anchor.

§subject_public_key_info: SubjectPublicKeyInfoOwned

The subject public key info of the trust anchor.

Must be a valid SPKI for the chosen signature algorithm. An empty or malformed SPKI will cause signature verification to fail with Error::NoTrustedPath (no anchor matched), not a panic.

§name_constraints: Option<NameConstraints>

NameConstraints from the trust anchor certificate, if present.

When set, chain_walk seeds the initial permitted_subtrees and excluded_subtrees state from this value before walking the chain. Populated automatically by from_cert; None for programmatically constructed anchors unless explicitly set.

Implementations§

Source§

impl TrustAnchor

Source

pub const fn new( subject: Name, subject_public_key_info: SubjectPublicKeyInfoOwned, ) -> Self

Create a trust anchor from raw subject name and SPKI.

Source

pub fn from_cert(cert: Certificate) -> Self

Extract subject name and SPKI from a certificate to create a trust anchor.

This is the typical constructor when your trust store contains full self-signed root CA certificates.

Prefer TrustAnchor::from (i.e. TrustAnchor::from(&cert)) when you need to keep cert alive after building the anchor.

§Warning: malformed NameConstraints are silently dropped

If the anchor certificate contains a malformed or unparseable NameConstraints extension, from_cert silently sets name_constraints = None and continues. The resulting anchor will not enforce any name-constraint restrictions from that extension, which may widen the trust scope beyond what the certificate intended.

For strict RFC 5280 §4.2 compliance — where a critical extension that cannot be parsed MUST cause rejection — use TrustAnchor::try_from instead. That path returns Err(DerError) so the caller can reject the malformed anchor rather than silently operating without name constraints.

Trait Implementations§

Source§

impl Clone for TrustAnchor

Source§

fn clone(&self) -> TrustAnchor

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 TrustAnchor

Source§

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

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

impl<'de> Deserialize<'de> for TrustAnchor

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Eq for TrustAnchor

Source§

impl From<&CertificateInner> for TrustAnchor

Source§

fn from(cert: &Certificate) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for TrustAnchor

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for TrustAnchor

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for TrustAnchor

Source§

impl TryFrom<CertificateInner> for TrustAnchor

Fail-closed construction from an owned certificate.

Returns Err(DerError) if the certificate contains a NameConstraints extension with malformed DER. Use this when building a trust store that must reject certificates with unparseable critical extensions per RFC 5280 §4.2.

The error type is the opaque DerError newtype rather than der::Error so that a future major-version bump in the der crate does not cascade into a semver break here.

§Why only TryFrom<Certificate> and not TryFrom<&Certificate>

TryFrom<&Certificate> would conflict with the blanket impl impl<T, U: Into<T>> TryFrom<U> provided by Rust core, because From<&Certificate> is already implemented (and From implies Into). Use TrustAnchor::try_from(cert.clone()) if you need to keep cert.

Source§

type Error = DerError

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

fn try_from(cert: Certificate) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

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<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<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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> Same for T

Source§

type Output = T

Should always be Self
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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V