11use crate :: error:: { Error , Result } ;
22use log:: debug;
3- use rustls:: pki_types:: CertificateDer ;
3+ use rustls:: pki_types:: { CertificateDer , PrivateKeyDer , PrivatePkcs8KeyDer } ;
44use rustls:: RootCertStore ;
55use std:: sync:: Arc ;
66
77#[ derive( Clone , Debug ) ]
88pub ( crate ) struct MaterialSnapshot {
9+ pub generation : u64 ,
910 pub certified_key : Arc < rustls:: sign:: CertifiedKey > ,
1011 pub roots : Arc < RootCertStore > ,
1112}
1213
13- pub ( crate ) fn roots_from_bundle_der ( bundle_authorities : & [ Vec < u8 > ] ) -> Result < Arc < RootCertStore > > {
14+ /// Build a `RootCertStore` from DER-encoded certificate authorities.
15+ ///
16+ /// ## Errors
17+ ///
18+ /// Returns [`Error::Internal`] if no certificates are accepted into the store.
19+ pub ( crate ) fn roots_from_certs ( certs : & [ CertificateDer < ' static > ] ) -> Result < Arc < RootCertStore > > {
1420 let mut store = RootCertStore :: empty ( ) ;
1521
16- let ders: Vec < CertificateDer < ' static > > = bundle_authorities
17- . iter ( )
18- . cloned ( )
19- . map ( CertificateDer :: from)
20- . collect ( ) ;
22+ let added = store. add_parsable_certificates ( certs. iter ( ) . cloned ( ) ) ;
2123
22- let added = store. add_parsable_certificates ( ders) ;
23-
24- debug ! ( "loaded root cert(s): {:?}" , added) ;
24+ debug ! ( "loaded root cert(s): {added:?}" ) ;
2525
2626 if store. is_empty ( ) {
2727 return Err ( Error :: Internal (
@@ -32,18 +32,17 @@ pub(crate) fn roots_from_bundle_der(bundle_authorities: &[Vec<u8>]) -> Result<Ar
3232 Ok ( Arc :: new ( store) )
3333}
3434
35- pub ( crate ) fn certified_key_from_der (
36- cert_chain_der : & [ Vec < u8 > ] ,
35+ /// Build a rustls `CertifiedKey` from a cert chain and a PKCS#8 private key.
36+ ///
37+ /// ## Errors
38+ ///
39+ /// Returns [`Error::CertifiedKey`] if the crypto provider is not installed
40+ /// or the key can't be loaded.
41+ pub ( crate ) fn certified_key_from_chain_and_key (
42+ cert_chain : Vec < CertificateDer < ' static > > ,
3743 private_key_pkcs8_der : & [ u8 ] ,
3844) -> Result < Arc < rustls:: sign:: CertifiedKey > > {
39- let certs: Vec < rustls:: pki_types:: CertificateDer < ' static > > = cert_chain_der
40- . iter ( )
41- . map ( |c| rustls:: pki_types:: CertificateDer :: from ( c. clone ( ) ) )
42- . collect ( ) ;
43-
44- let key_der = rustls:: pki_types:: PrivateKeyDer :: Pkcs8 (
45- rustls:: pki_types:: PrivatePkcs8KeyDer :: from ( private_key_pkcs8_der. to_vec ( ) ) ,
46- ) ;
45+ let key_der = PrivateKeyDer :: Pkcs8 ( PrivatePkcs8KeyDer :: from ( private_key_pkcs8_der. to_vec ( ) ) ) ;
4746
4847 let provider = rustls:: crypto:: CryptoProvider :: get_default ( )
4948 . ok_or_else ( || Error :: CertifiedKey ( "rustls crypto provider is not installed" . into ( ) ) ) ?;
@@ -54,7 +53,29 @@ pub(crate) fn certified_key_from_der(
5453 . map_err ( |e| Error :: CertifiedKey ( format ! ( "{e:?}" ) ) ) ?;
5554
5655 Ok ( Arc :: new ( rustls:: sign:: CertifiedKey :: new (
57- certs ,
56+ cert_chain ,
5857 signing_key,
5958 ) ) )
6059}
60+
61+ /// Helper: build an owned cert chain from an iterator of DER bytes.
62+ ///
63+ /// This prevents higher layers from passing around `Vec<Vec<u8>>`.
64+ pub ( crate ) fn cert_chain_from_der_bytes < ' a , I > ( ders : I ) -> Vec < CertificateDer < ' static > >
65+ where
66+ I : IntoIterator < Item = & ' a [ u8 ] > ,
67+ {
68+ ders. into_iter ( )
69+ . map ( |b| CertificateDer :: from ( b. to_vec ( ) ) )
70+ . collect ( )
71+ }
72+
73+ /// Helper: build owned root certs from an iterator of DER bytes.
74+ pub ( crate ) fn certs_from_der_bytes < ' a , I > ( ders : I ) -> Vec < CertificateDer < ' static > >
75+ where
76+ I : IntoIterator < Item = & ' a [ u8 ] > ,
77+ {
78+ ders. into_iter ( )
79+ . map ( |b| CertificateDer :: from ( b. to_vec ( ) ) )
80+ . collect ( )
81+ }
0 commit comments