Skip to content

Commit 5a4c044

Browse files
committed
Expose TrustAnchor -> SubjectPublicKeyInfoDer operation
1 parent a3bd956 commit 5a4c044

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ pub use {
8989
},
9090
};
9191

92+
#[cfg(feature = "alloc")]
93+
pub use trust_anchor::spki_for_anchor;
94+
9295
#[cfg(feature = "alloc")]
9396
pub use crl::{OwnedCertRevocationList, OwnedRevokedCert};
9497

src/trust_anchor.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use pki_types::{CertificateDer, TrustAnchor};
1+
use pki_types::{CertificateDer, SubjectPublicKeyInfoDer, TrustAnchor};
22

33
use crate::cert::{Cert, lenient_certificate_serial_number};
44
use crate::der;
@@ -43,6 +43,12 @@ pub fn anchor_from_trusted_cert<'a>(
4343
}
4444
}
4545

46+
/// Reconstitutes the given trust anchor's SubjectPublicKeyInfo.
47+
#[cfg(feature = "alloc")]
48+
pub fn spki_for_anchor(anchor: &TrustAnchor<'_>) -> SubjectPublicKeyInfoDer<'static> {
49+
der::asn1_wrap(der::Tag::Sequence, &anchor.subject_public_key_info).into()
50+
}
51+
4652
/// Parses a v1 certificate directly into a TrustAnchor.
4753
fn extract_trust_anchor_from_v1_cert_der(
4854
cert_der: untrusted::Input<'_>,

src/verify_cert.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ use crate::crl::RevocationOptions;
2626
use crate::der::{self, FromDer};
2727
use crate::end_entity::EndEntityCert;
2828
use crate::error::Error;
29+
#[cfg(feature = "alloc")]
30+
use crate::trust_anchor;
2931
use crate::{public_values_eq, subject_name};
3032

3133
// Use `'a` for lifetimes that we don't care about, `'p` for lifetimes that become a part of
@@ -220,10 +222,7 @@ impl<'p> VerifiedPath<'p> {
220222
pub fn issuer_spki(&self) -> SubjectPublicKeyInfoDer<'p> {
221223
match self.intermediate_certificates().next() {
222224
Some(issuer) => issuer.subject_public_key_info(),
223-
None => SubjectPublicKeyInfoDer::from(der::asn1_wrap(
224-
der::Tag::Sequence,
225-
self.anchor.subject_public_key_info.as_ref(),
226-
)),
225+
None => trust_anchor::spki_for_anchor(self.anchor),
227226
}
228227
}
229228
}

tests/integration.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,3 +509,13 @@ fn no_scts() {
509509
cert.sct_log_timestamps().collect::<Result<Vec<_>, _>>()
510510
);
511511
}
512+
513+
#[cfg(feature = "alloc")]
514+
#[test]
515+
fn anchor_spki() {
516+
let ca = CertificateDer::from(&include_bytes!("netflix/ca.der")[..]);
517+
let anchor = anchor_from_trusted_cert(&ca).unwrap();
518+
let spki = webpki::spki_for_anchor(&anchor);
519+
520+
assert_eq!(Some(&0x30), spki.first()); // starts with SEQUENCE
521+
}

0 commit comments

Comments
 (0)