1use crate::cert::{lenient_certificate_serial_number, Cert, EndEntityOrCa};
2use crate::{der, Error};
3
4#[derive(Debug)]
14pub struct TrustAnchor<'a> {
15 pub subject: &'a [u8],
17
18 pub spki: &'a [u8],
20
21 pub name_constraints: Option<&'a [u8]>,
24}
25
26#[deprecated(
28 since = "0.101.2",
29 note = "The per-usage trust anchor representations and verification functions are deprecated in \
30 favor of the general-purpose `TrustAnchor` type and `EndEntity::verify_for_usage` function. \
31 The new `verify_for_usage` function expresses trust anchor and end entity purpose with the \
32 key usage argument."
33)]
34#[derive(Debug)]
35pub struct TlsServerTrustAnchors<'a>(pub &'a [TrustAnchor<'a>]);
36
37#[deprecated(
39 since = "0.101.2",
40 note = "The per-usage trust anchor representations and verification functions are deprecated in \
41 favor of the general-purpose `TrustAnchor` type and `EndEntity::verify_for_usage` function. \
42 The new `verify_for_usage` function expresses trust anchor and end entity purpose with the \
43 key usage argument."
44)]
45#[derive(Debug)]
46pub struct TlsClientTrustAnchors<'a>(pub &'a [TrustAnchor<'a>]);
47
48impl<'a> TrustAnchor<'a> {
49 pub fn try_from_cert_der(cert_der: &'a [u8]) -> Result<Self, Error> {
54 let cert_der = untrusted::Input::from(cert_der);
55
56 match Cert::from_der(cert_der, EndEntityOrCa::EndEntity) {
66 Ok(cert) => Ok(Self::from(cert)),
67 Err(Error::UnsupportedCertVersion) => {
68 Self::from_v1_der(cert_der).or(Err(Error::BadDer))
69 }
70 Err(err) => Err(err),
71 }
72 }
73
74 fn from_v1_der(cert_der: untrusted::Input<'a>) -> Result<Self, Error> {
76 cert_der.read_all(Error::BadDer, |cert_der| {
78 der::nested(cert_der, der::Tag::Sequence, Error::BadDer, |cert_der| {
79 let anchor = der::nested(cert_der, der::Tag::Sequence, Error::BadDer, |tbs| {
80 lenient_certificate_serial_number(tbs)?;
82
83 skip(tbs, der::Tag::Sequence)?; skip(tbs, der::Tag::Sequence)?; skip(tbs, der::Tag::Sequence)?; let subject = der::expect_tag_and_get_value(tbs, der::Tag::Sequence)?;
87 let spki = der::expect_tag_and_get_value(tbs, der::Tag::Sequence)?;
88
89 Ok(TrustAnchor {
90 subject: subject.as_slice_less_safe(),
91 spki: spki.as_slice_less_safe(),
92 name_constraints: None,
93 })
94 });
95
96 skip(cert_der, der::Tag::Sequence)?;
98 skip(cert_der, der::Tag::BitString)?;
99
100 anchor
101 })
102 })
103 }
104}
105
106impl<'a> From<Cert<'a>> for TrustAnchor<'a> {
107 fn from(cert: Cert<'a>) -> Self {
108 Self {
109 subject: cert.subject.as_slice_less_safe(),
110 spki: cert.spki.value().as_slice_less_safe(),
111 name_constraints: cert.name_constraints.map(|nc| nc.as_slice_less_safe()),
112 }
113 }
114}
115
116fn skip(input: &mut untrusted::Reader, tag: der::Tag) -> Result<(), Error> {
117 der::expect_tag_and_get_value(input, tag).map(|_| ())
118}