webpki/trust_anchor.rs
1#[cfg(feature = "alloc")]
2use pki_types::SubjectPublicKeyInfoDer;
3use pki_types::{CertificateDer, TrustAnchor};
4
5use crate::cert::{Cert, lenient_certificate_serial_number};
6use crate::der;
7use crate::error::{DerTypeId, Error};
8
9/// Interprets the given pre-validated DER-encoded certificate as a `TrustAnchor`.
10///
11/// This function extracts the components of a trust anchor (see [RFC 5280 6.1.1]) from
12/// an X.509 certificate obtained from a source trusted to have appropriately validated
13/// the subject name, public key, and name constraints in the certificate, for example your
14/// operating system's trust store.
15///
16/// No additional checks on the content of the certificate, including whether it is self-signed,
17/// or has a basic constraints extension indicating the `cA` boolean is true, will be performed.
18/// [RFC 5280 6.2] notes:
19/// > Implementations that use self-signed certificates to specify trust
20/// > anchor information are free to process or ignore such information.
21///
22/// This function is intended for users constructing `TrustAnchor`'s from existing trust stores
23/// that express trust anchors as X.509 certificates. It should **not** be used to treat an
24/// end-entity certificate as a `TrustAnchor` in an effort to validate the same end-entity
25/// certificate during path building. Webpki has no support for self-signed certificates.
26///
27/// [RFC 5280 6.1.1]: <https://datatracker.ietf.org/doc/html/rfc5280#section-6.1.1>
28/// [RFC 5280 6.2]: <https://www.rfc-editor.org/rfc/rfc5280#section-6.2>
29pub fn anchor_from_trusted_cert<'a>(
30 cert: &'a CertificateDer<'a>,
31) -> Result<TrustAnchor<'a>, Error> {
32 let cert_der = untrusted::Input::from(cert.as_ref());
33
34 // v1 certificates will result in `Error::BadDer` because `parse_cert` will
35 // expect a version field that isn't there. In that case, try to parse the
36 // certificate using a special parser for v1 certificates. Notably, that
37 // parser doesn't allow extensions, so there's no need to worry about
38 // embedded name constraints in a v1 certificate.
39 match Cert::from_der(cert_der) {
40 Ok(cert) => Ok(TrustAnchor::from(cert)),
41 Err(Error::UnsupportedCertVersion) => {
42 extract_trust_anchor_from_v1_cert_der(cert_der).or(Err(Error::BadDer))
43 }
44 Err(err) => Err(err),
45 }
46}
47
48/// Reconstitutes the given trust anchor's SubjectPublicKeyInfo.
49#[cfg(feature = "alloc")]
50pub fn spki_for_anchor(anchor: &TrustAnchor<'_>) -> SubjectPublicKeyInfoDer<'static> {
51 der::asn1_wrap(der::Tag::Sequence, &anchor.subject_public_key_info).into()
52}
53
54/// Parses a v1 certificate directly into a TrustAnchor.
55fn extract_trust_anchor_from_v1_cert_der(
56 cert_der: untrusted::Input<'_>,
57) -> Result<TrustAnchor<'_>, Error> {
58 // X.509 Certificate: https://tools.ietf.org/html/rfc5280#section-4.1.
59 cert_der.read_all(Error::BadDer, |cert_der| {
60 der::nested(
61 cert_der,
62 der::Tag::Sequence,
63 Error::TrailingData(DerTypeId::TrustAnchorV1),
64 |cert_der| {
65 let anchor = der::nested(
66 cert_der,
67 der::Tag::Sequence,
68 Error::TrailingData(DerTypeId::TrustAnchorV1TbsCertificate),
69 |tbs| {
70 // The version number field does not appear in v1 certificates.
71 lenient_certificate_serial_number(tbs)?;
72
73 skip(tbs, der::Tag::Sequence)?; // signature.
74 skip(tbs, der::Tag::Sequence)?; // issuer.
75 skip(tbs, der::Tag::Sequence)?; // validity.
76 let subject = der::expect_tag(tbs, der::Tag::Sequence)?;
77 let spki = der::expect_tag(tbs, der::Tag::Sequence)?;
78
79 Ok(TrustAnchor {
80 subject: subject.as_slice_less_safe().into(),
81 subject_public_key_info: spki.as_slice_less_safe().into(),
82 name_constraints: None,
83 })
84 },
85 );
86
87 // read and discard signatureAlgorithm + signature
88 skip(cert_der, der::Tag::Sequence)?;
89 skip(cert_der, der::Tag::BitString)?;
90
91 anchor
92 },
93 )
94 })
95}
96
97impl<'a> From<Cert<'a>> for TrustAnchor<'a> {
98 fn from(cert: Cert<'a>) -> Self {
99 Self {
100 subject: cert.subject.as_slice_less_safe().into(),
101 subject_public_key_info: cert.spki.as_slice_less_safe().into(),
102 name_constraints: cert
103 .name_constraints
104 .map(|nc| nc.as_slice_less_safe().into()),
105 }
106 }
107}
108
109fn skip(input: &mut untrusted::Reader<'_>, tag: der::Tag) -> Result<(), Error> {
110 der::expect_tag(input, tag).map(|_| ())
111}