Skip to content

Commit 2ac6d1f

Browse files
committed
fmt
1 parent 2e9c49c commit 2ac6d1f

3 files changed

Lines changed: 72 additions & 102 deletions

File tree

src/tls/x509/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod identity;
2+
mod parser;
23
mod store;
34

45
use boring2::x509::X509;
Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,34 @@
1-
use std::sync::Arc;
1+
use boring2::x509::store::{X509Store, X509StoreBuilder};
22

3-
use boring2::x509::store::X509StoreBuilder;
3+
use super::{Certificate, CertificateInput};
4+
use crate::{Error, Result};
45

5-
use super::{CertStore, Certificate, CertificateInput};
6-
use crate::Error;
7-
8-
pub fn parse_certs_with_iter<'c, I>(
6+
pub fn parse_certs<'c, I>(
97
certs: I,
108
parser: fn(&'c [u8]) -> crate::Result<Certificate>,
11-
) -> crate::Result<CertStore>
9+
) -> Result<X509Store>
1210
where
1311
I: IntoIterator,
1412
I::Item: Into<CertificateInput<'c>>,
1513
{
1614
let mut store = X509StoreBuilder::new().map_err(Error::tls)?;
1715
let certs = filter_map_certs(certs, parser);
18-
process_certs_with_builder(certs.into_iter(), &mut store)?;
19-
Ok(CertStore(Arc::new(store.build())))
16+
process_certs(certs.into_iter(), &mut store)?;
17+
Ok(store.build())
2018
}
2119

22-
pub fn parse_certs_with_stack<C, F>(certs: C, x509: F) -> crate::Result<CertStore>
20+
pub fn parse_certs_with_stack<C, F>(certs: C, parse: F) -> Result<X509Store>
2321
where
2422
C: AsRef<[u8]>,
25-
F: Fn(C) -> crate::Result<Vec<Certificate>>,
23+
F: Fn(C) -> Result<Vec<Certificate>>,
2624
{
2725
let mut store = X509StoreBuilder::new().map_err(Error::tls)?;
28-
let certs = x509(certs)?;
29-
process_certs_with_builder(certs.into_iter(), &mut store)?;
30-
Ok(CertStore(Arc::new(store.build())))
26+
let certs = parse(certs)?;
27+
process_certs(certs.into_iter(), &mut store)?;
28+
Ok(store.build())
3129
}
3230

33-
pub fn process_certs_with_builder<I>(iter: I, store: &mut X509StoreBuilder) -> crate::Result<()>
31+
pub fn process_certs<I>(iter: I, store: &mut X509StoreBuilder) -> Result<()>
3432
where
3533
I: Iterator<Item = Certificate>,
3634
{
@@ -54,7 +52,7 @@ where
5452

5553
pub fn filter_map_certs<'c, I>(
5654
certs: I,
57-
parser: fn(&'c [u8]) -> crate::Result<Certificate>,
55+
parser: fn(&'c [u8]) -> Result<Certificate>,
5856
) -> impl Iterator<Item = Certificate>
5957
where
6058
I: IntoIterator,
Lines changed: 57 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
mod parser;
2-
3-
use std::{fmt::Debug, path::Path, sync::Arc};
1+
use std::sync::Arc;
42

53
use boring2::{
64
ssl::SslConnectorBuilder,
75
x509::store::{X509Store, X509StoreBuilder},
86
};
9-
use parser::{
10-
filter_map_certs, parse_certs_with_iter, parse_certs_with_stack, process_certs_with_builder,
11-
};
127

13-
use super::{Certificate, CertificateInput};
14-
use crate::Error;
8+
use super::{
9+
Certificate, CertificateInput,
10+
parser::{filter_map_certs, parse_certs, parse_certs_with_stack, process_certs},
11+
};
12+
use crate::{Error, Result};
1513

1614
/// A builder for constructing a `CertStore`.
1715
///
1816
/// This builder provides methods to add certificates to the store from various formats,
1917
/// and to set default paths for the certificate store. Once all desired certificates
2018
/// have been added, the `build` method can be used to create the `CertStore`.
2119
pub struct CertStoreBuilder {
22-
builder: crate::Result<X509StoreBuilder>,
20+
builder: Result<X509StoreBuilder>,
2321
}
2422

23+
// ====== impl CertStoreBuilder ======
24+
2525
impl CertStoreBuilder {
2626
/// Adds a DER-encoded certificate to the certificate store.
2727
#[inline]
@@ -68,7 +68,7 @@ impl CertStoreBuilder {
6868
{
6969
if let Ok(ref mut builder) = self.builder {
7070
let result = Certificate::stack_from_pem(certs.as_ref())
71-
.and_then(|certs| process_certs_with_builder(certs.into_iter(), builder));
71+
.and_then(|certs| process_certs(certs.into_iter(), builder));
7272

7373
if let Err(err) = result {
7474
self.builder = Err(err);
@@ -77,23 +77,6 @@ impl CertStoreBuilder {
7777
self
7878
}
7979

80-
/// Adds PEM-encoded certificates from a file to the certificate store.
81-
///
82-
/// This method reads the file at the specified path, expecting it to contain a PEM-encoded
83-
/// certificate stack, and then adds the certificates to the store.
84-
pub fn add_file_pem_certs<P>(mut self, path: P) -> Self
85-
where
86-
P: AsRef<Path>,
87-
{
88-
match std::fs::read(path) {
89-
Ok(data) => return self.add_stack_pem_certs(data),
90-
Err(err) => {
91-
self.builder = Err(Error::builder(err));
92-
}
93-
}
94-
self
95-
}
96-
9780
/// Load certificates from their default locations.
9881
///
9982
/// These locations are read from the `SSL_CERT_FILE` and `SSL_CERT_DIR`
@@ -112,15 +95,20 @@ impl CertStoreBuilder {
11295
///
11396
/// This method finalizes the builder and constructs the `CertStore`
11497
/// containing all the added certificates.
115-
pub fn build(self) -> crate::Result<CertStore> {
116-
let builder = self.builder?;
117-
Ok(CertStore(Arc::new(builder.build())))
98+
#[inline]
99+
pub fn build(self) -> Result<CertStore> {
100+
self.builder
101+
.map(X509StoreBuilder::build)
102+
.map(Arc::new)
103+
.map(CertStore)
118104
}
105+
}
119106

107+
impl CertStoreBuilder {
120108
fn parse_cert<'c, C, P>(mut self, cert: C, parser: P) -> Self
121109
where
122110
C: Into<CertificateInput<'c>>,
123-
P: Fn(&'c [u8]) -> crate::Result<Certificate>,
111+
P: Fn(&'c [u8]) -> Result<Certificate>,
124112
{
125113
if let Ok(ref mut builder) = self.builder {
126114
let input = cert.into();
@@ -135,18 +123,14 @@ impl CertStoreBuilder {
135123
self
136124
}
137125

138-
fn parse_certs<'c, I>(
139-
mut self,
140-
certs: I,
141-
parser: fn(&'c [u8]) -> crate::Result<Certificate>,
142-
) -> Self
126+
fn parse_certs<'c, I>(mut self, certs: I, parser: fn(&'c [u8]) -> Result<Certificate>) -> Self
143127
where
144128
I: IntoIterator,
145129
I::Item: Into<CertificateInput<'c>>,
146130
{
147131
if let Ok(ref mut builder) = self.builder {
148132
let certs = filter_map_certs(certs, parser);
149-
if let Err(err) = process_certs_with_builder(certs, builder) {
133+
if let Err(err) = process_certs(certs, builder) {
150134
self.builder = Err(err);
151135
}
152136
}
@@ -158,37 +142,8 @@ impl CertStoreBuilder {
158142
#[derive(Clone)]
159143
pub struct CertStore(Arc<X509Store>);
160144

161-
impl Default for CertStore {
162-
fn default() -> Self {
163-
#[cfg(feature = "webpki-roots")]
164-
pub(super) static LOAD_CERTS: std::sync::LazyLock<CertStore> =
165-
std::sync::LazyLock::new(|| {
166-
CertStore::builder()
167-
.add_der_certs(webpki_root_certs::TLS_SERVER_ROOT_CERTS)
168-
.build()
169-
.expect("failed to load default cert store")
170-
});
171-
172-
#[cfg(not(feature = "webpki-roots"))]
173-
{
174-
CertStore::builder()
175-
.set_default_paths()
176-
.build()
177-
.expect("failed to load default cert store")
178-
}
145+
// ====== impl CertStore ======
179146

180-
#[cfg(feature = "webpki-roots")]
181-
LOAD_CERTS.clone()
182-
}
183-
}
184-
185-
impl Debug for CertStore {
186-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
187-
f.debug_struct("CertStore").finish()
188-
}
189-
}
190-
191-
/// ====== impl CertStore ======
192147
impl CertStore {
193148
/// Creates a new `CertStoreBuilder`.
194149
#[inline]
@@ -200,45 +155,37 @@ impl CertStore {
200155

201156
/// Creates a new `CertStore` from a collection of DER-encoded certificates.
202157
#[inline]
203-
pub fn from_der_certs<'c, C>(certs: C) -> crate::Result<CertStore>
158+
pub fn from_der_certs<'c, C>(certs: C) -> Result<CertStore>
204159
where
205160
C: IntoIterator,
206161
C::Item: Into<CertificateInput<'c>>,
207162
{
208-
parse_certs_with_iter(certs, Certificate::from_der)
163+
parse_certs(certs, Certificate::from_der)
164+
.map(Arc::new)
165+
.map(CertStore)
209166
}
210167

211168
/// Creates a new `CertStore` from a collection of PEM-encoded certificates.
212169
#[inline]
213-
pub fn from_pem_certs<'c, C>(certs: C) -> crate::Result<CertStore>
170+
pub fn from_pem_certs<'c, C>(certs: C) -> Result<CertStore>
214171
where
215172
C: IntoIterator,
216173
C::Item: Into<CertificateInput<'c>>,
217174
{
218-
parse_certs_with_iter(certs, Certificate::from_pem)
175+
parse_certs(certs, Certificate::from_pem)
176+
.map(Arc::new)
177+
.map(CertStore)
219178
}
220179

221180
/// Creates a new `CertStore` from a PEM-encoded certificate stack.
222181
#[inline]
223-
pub fn from_pem_stack<C>(certs: C) -> crate::Result<CertStore>
182+
pub fn from_pem_stack<C>(certs: C) -> Result<CertStore>
224183
where
225184
C: AsRef<[u8]>,
226185
{
227186
parse_certs_with_stack(certs, Certificate::stack_from_pem)
228-
}
229-
230-
/// Creates a new `CertStore` from a PEM-encoded certificate file.
231-
///
232-
/// This method reads the file at the specified path, expecting it to contain a PEM-encoded
233-
/// certificate stack, and then constructs a `CertStore` from it.
234-
#[inline]
235-
pub fn from_pem_file<P>(path: P) -> crate::Result<CertStore>
236-
where
237-
P: AsRef<Path>,
238-
{
239-
std::fs::read(path)
240-
.map_err(Error::builder)
241-
.and_then(Self::from_pem_stack)
187+
.map(Arc::new)
188+
.map(CertStore)
242189
}
243190
}
244191

@@ -248,3 +195,27 @@ impl CertStore {
248195
tls.set_cert_store_ref(&self.0);
249196
}
250197
}
198+
199+
impl Default for CertStore {
200+
fn default() -> Self {
201+
#[cfg(feature = "webpki-roots")]
202+
pub(super) static LOAD_CERTS: std::sync::LazyLock<CertStore> =
203+
std::sync::LazyLock::new(|| {
204+
CertStore::builder()
205+
.add_der_certs(webpki_root_certs::TLS_SERVER_ROOT_CERTS)
206+
.build()
207+
.expect("failed to load default cert store")
208+
});
209+
210+
#[cfg(not(feature = "webpki-roots"))]
211+
{
212+
CertStore::builder()
213+
.set_default_paths()
214+
.build()
215+
.expect("failed to load default cert store")
216+
}
217+
218+
#[cfg(feature = "webpki-roots")]
219+
LOAD_CERTS.clone()
220+
}
221+
}

0 commit comments

Comments
 (0)