11//! rustls-native-certs allows rustls to use the platform's native certificate
22//! store when operating as a TLS client.
33//!
4- //! It consists of a single function [load_native_certs](fn.load_native_certs.html) which returns a
5- //! `rustls::RootCertStore` pre-filled from the native certificate store.
6-
7- /// Like `Result<T,E>`, but allows for functions that can return partially complete
8- /// work alongside an error.
9- pub type PartialResult < T , E > = Result < T , ( Option < T > , E ) > ;
4+ //! It provides the following functions:
5+ //! * A higher level function [load_native_certs](fn.build_native_certs.html)
6+ //! which returns a `rustls::RootCertStore` pre-filled from the native
7+ //! certificate store. It is only available if the `rustls` feature is
8+ //! enabled.
9+ //! * A lower level function [build_native_certs](fn.build_native_certs.html)
10+ //! that lets callers pass their own certificate parsing logic. It is
11+ //! available to all users.
1012
1113#[ cfg( all( unix, not( target_os = "macos" ) ) ) ]
1214mod unix;
@@ -23,50 +25,20 @@ mod macos;
2325#[ cfg( target_os = "macos" ) ]
2426use macos as platform;
2527
26- use rustls:: RootCertStore ;
27- use std:: io:: { Error , ErrorKind } ;
28+ #[ cfg( feature = "rustls" ) ]
29+ mod rustls;
30+
31+ use std:: io:: Error ;
2832use std:: io:: BufRead ;
2933
34+ #[ cfg( feature = "rustls" ) ]
35+ pub use crate :: rustls:: { load_native_certs, PartialResult } ;
36+
3037pub trait RootStoreBuilder {
3138 fn load_der ( & mut self , der : Vec < u8 > ) -> Result < ( ) , Error > ;
3239 fn load_pem_file ( & mut self , rd : & mut dyn BufRead ) -> Result < ( ) , Error > ;
3340}
3441
35- /// Loads root certificates found in the platform's native certificate
36- /// store.
37- ///
38- /// On success, this returns a `rustls::RootCertStore` loaded with a
39- /// snapshop of the root certificates found on this platform. This
40- /// function fails in a platform-specific way, expressed in a `std::io::Error`.
41- ///
42- /// This function can be expensive: on some platforms it involves loading
43- /// and parsing a ~300KB disk file. It's therefore prudent to call
44- /// this sparingly.
45- pub fn load_native_certs ( ) -> PartialResult < RootCertStore , Error > {
46- struct RootCertStoreLoader {
47- store : RootCertStore ,
48- } ;
49- impl RootStoreBuilder for RootCertStoreLoader {
50- fn load_der ( & mut self , der : Vec < u8 > ) -> Result < ( ) , Error > {
51- self . store . add ( & rustls:: Certificate ( der) )
52- . map_err ( |err| Error :: new ( ErrorKind :: InvalidData , err) )
53- }
54- fn load_pem_file ( & mut self , rd : & mut dyn BufRead ) -> Result < ( ) , Error > {
55- self . store . add_pem_file ( rd)
56- . map ( |_| ( ) )
57- . map_err ( |( ) | Error :: new ( ErrorKind :: InvalidData , format ! ( "could not load PEM file" ) ) )
58- }
59- }
60- let mut loader = RootCertStoreLoader {
61- store : RootCertStore :: empty ( ) ,
62- } ;
63- match build_native_certs ( & mut loader) {
64- Err ( err) if loader. store . is_empty ( ) => Err ( ( None , err) ) ,
65- Err ( err) => Err ( ( Some ( loader. store ) , err) ) ,
66- Ok ( ( ) ) => Ok ( loader. store ) ,
67- }
68- }
69-
7042/// Loads root certificates found in the platform's native certificate
7143/// store, executing callbacks on the provided builder.
7244///
0 commit comments