@@ -30,10 +30,11 @@ use crate::error::Error;
3030use crate :: spki_for_anchor;
3131use crate :: { public_values_eq, subject_name} ;
3232
33+ /// Build a [`VerifiedPath`] for an end-entity certificate from the given trust anchors.
3334// Use `'a` for lifetimes that we don't care about, `'p` for lifetimes that become a part of
3435// the `VerifiedPath`.
35- pub ( crate ) struct PathBuilder < ' a , ' p > {
36- pub ( crate ) eku : & ' a dyn ExtendedKeyUsageValidator ,
36+ pub struct PathBuilder < ' a , ' p > {
37+ pub ( crate ) eku : & ' p dyn ExtendedKeyUsageValidator ,
3738 pub ( crate ) supported_sig_algs : & ' a [ & ' a dyn SignatureVerificationAlgorithm ] ,
3839 pub ( crate ) trust_anchors : & ' p [ TrustAnchor < ' p > ] ,
3940 pub ( crate ) intermediate_certs : & ' p [ CertificateDer < ' p > ] ,
@@ -43,7 +44,68 @@ pub(crate) struct PathBuilder<'a, 'p> {
4344}
4445
4546impl < ' a , ' p : ' a > PathBuilder < ' a , ' p > {
46- pub ( crate ) fn build (
47+ /// Build a new [`PathBuilder`] with the given parameters.
48+ ///
49+ /// * `eku` is the intended usage of the certificate, indicating what kind
50+ /// of usage we're verifying the certificate for. The default [`ExtendedKeyUsageValidator`]
51+ /// implementation is [`ExtendedKeyUsage`](crate::ExtendedKeyUsage).
52+ /// * `supported_sig_algs` is the list of signature algorithms that are
53+ /// trusted for use in certificate signatures; the end-entity certificate's
54+ /// public key is not validated against this list.
55+ /// * `trust_anchors` is the list of root CAs to trust in the built path.
56+ pub fn new (
57+ eku : & ' p dyn ExtendedKeyUsageValidator ,
58+ supported_sig_algs : & ' a [ & ' a dyn SignatureVerificationAlgorithm ] ,
59+ trust_anchors : & ' p [ TrustAnchor < ' p > ] ,
60+ ) -> Self {
61+ Self {
62+ eku,
63+ supported_sig_algs,
64+ trust_anchors,
65+ intermediate_certs : & [ ] ,
66+ revocation : None ,
67+ verify_path : None ,
68+ }
69+ }
70+
71+ /// Set the sequence of intermediate certificates to use for path building.
72+ ///
73+ /// These should be sent by the peer. Defaults to empty.
74+ pub fn with_intermediate_certs ( mut self , intermediate_certs : & ' p [ CertificateDer < ' p > ] ) -> Self {
75+ self . intermediate_certs = intermediate_certs;
76+ self
77+ }
78+
79+ /// Set the revocation options to use for path building.
80+ ///
81+ /// By default, revocation checking is disabled.
82+ pub fn with_revocation ( mut self , revocation : RevocationOptions < ' a > ) -> Self {
83+ self . revocation = Some ( revocation) ;
84+ self
85+ }
86+
87+ /// Set a path verification function to use for path building.
88+ ///
89+ /// `verify()` will only be called for potentially verified paths, that is, paths that
90+ /// have been verified up to the trust anchor. As such, `verify()` cannot be used to
91+ /// verify a path that doesn't satisfy the constraints listed above; it can only be used to
92+ /// reject a path that does satisfy the aforementioned constraints. If `verify()` returns
93+ /// an error, path building will continue in order to try other options.
94+ ///
95+ /// By default, no additional path verification is done.
96+ pub fn with_path_verification (
97+ mut self ,
98+ verify : & ' a dyn Fn ( & VerifiedPath < ' _ > ) -> Result < ( ) , Error > ,
99+ ) -> Self {
100+ self . verify_path = Some ( verify) ;
101+ self
102+ }
103+
104+ /// Build a [`VerifiedPath`] for `end_entity` at the given `time`.
105+ ///
106+ /// If successful, yields a `VerifiedPath` type that can be used to inspect a verified chain
107+ /// of certificates that leads from the `end_entity` to one of the `self.trust_anchors`.
108+ pub fn build (
47109 & self ,
48110 end_entity : & ' p EndEntityCert < ' p > ,
49111 time : UnixTime ,
@@ -173,9 +235,7 @@ impl<'a, 'p: 'a> PathBuilder<'a, 'p> {
173235 }
174236}
175237
176- /// Path from end-entity certificate to trust anchor that's been verified.
177- ///
178- /// See [`EndEntityCert::verify_for_usage()`] for more details on what verification entails.
238+ /// Path from end-entity certificate to trust anchor that's been verified by a [`PathBuilder`].
179239pub struct VerifiedPath < ' p > {
180240 end_entity : & ' p EndEntityCert < ' p > ,
181241 intermediates : Intermediates < ' p > ,
@@ -1361,18 +1421,23 @@ mod tests {
13611421 ) -> Result < VerifiedPath < ' a > , ControlFlow < Error , Error > > {
13621422 let time = UnixTime :: since_unix_epoch ( Duration :: from_secs ( 0x1fed_f00d ) ) ;
13631423 let mut path = PartialPath :: new ( ee_cert) ;
1364- let opts = PathBuilder {
1365- eku : & ExtendedKeyUsage :: SERVER_AUTH ,
1366- supported_sig_algs : rustls_aws_lc_rs:: ALL_VERIFICATION_ALGS ,
1424+
1425+ let builder = PathBuilder :: new (
1426+ & ExtendedKeyUsage :: SERVER_AUTH ,
1427+ rustls_aws_lc_rs:: ALL_VERIFICATION_ALGS ,
13671428 trust_anchors,
1368- intermediate_certs,
1369- revocation : None ,
1370- verify_path,
1429+ )
1430+ . with_intermediate_certs ( intermediate_certs) ;
1431+ let builder = match verify_path {
1432+ Some ( verify) => builder. with_path_verification ( verify) ,
1433+ None => builder,
13711434 } ;
13721435
1373- match opts . build_chain_inner ( & mut path, time, 0 , & mut budget. unwrap_or_default ( ) ) {
1436+ match builder . build_chain_inner ( & mut path, time, 0 , & mut budget. unwrap_or_default ( ) ) {
13741437 Ok ( anchor) => Ok ( VerifiedPath :: new ( ee_cert, anchor, path) ) ,
13751438 Err ( err) => Err ( err) ,
13761439 }
13771440 }
1441+
1442+ //const EKU_SERVER_AUTH: ExtendedKeyUsage = ExtendedKeyUsage::server_auth();
13781443}
0 commit comments