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
9pub 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 match Cert::for_trust_anchor(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#[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
54fn extract_trust_anchor_from_v1_cert_der(
56 cert_der: untrusted::Input<'_>,
57) -> Result<TrustAnchor<'_>, Error> {
58 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 lenient_certificate_serial_number(tbs)?;
72
73 skip(tbs, der::Tag::Sequence)?; skip(tbs, der::Tag::Sequence)?; skip(tbs, der::Tag::Sequence)?; 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 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}
112
113#[cfg(test)]
114mod tests {
115 use rcgen::{CertificateParams, CustomExtension, KeyPair};
116
117 use super::*;
118
119 #[test]
122 fn anchor_ignores_critical_extension_with_unknown_oid() {
123 let der = cert_with_critical_extension(&[1, 2, 3, 4]);
124 anchor_from_trusted_cert(&der)
125 .expect("critical extension with unknown OID should be ignored for trust anchors");
126 }
127
128 #[test]
132 fn anchor_ignores_critical_extension_with_unknown_id_ce_oid() {
133 let der = cert_with_critical_extension(&[2, 5, 29, 99]);
134 anchor_from_trusted_cert(&der).expect(
135 "critical id-ce extension with unknown OID should be ignored for trust anchors",
136 );
137 }
138
139 fn cert_with_critical_extension(oid: &[u64]) -> CertificateDer<'static> {
140 let mut params = CertificateParams::new(vec!["example.com".into()]).unwrap();
141 let mut ext = CustomExtension::from_oid_content(oid, vec![1, 2]);
142 ext.set_criticality(true);
143 params.custom_extensions.push(ext);
144
145 let key = KeyPair::generate_for(&rcgen::PKCS_ECDSA_P256_SHA256).unwrap();
146 let cert = params.self_signed(&key).unwrap();
147 CertificateDer::from(cert.der().to_vec())
148 }
149}