trz_gateway_common/security_configuration/trusted_store/
pem.rs1use std::sync::Arc;
2
3use nameth::NamedEnumValues as _;
4use nameth::nameth;
5use openssl::error::ErrorStack;
6use openssl::x509::store::X509Store;
7use openssl::x509::store::X509StoreBuilder;
8
9use super::TrustedStoreConfig;
10use crate::security_configuration::common::parse_pem_certificates;
11
12#[derive(Clone, Debug, Default, PartialEq, Eq)]
14pub struct PemTrustedStore {
15 pub root_certificates_pem: String,
16}
17
18impl TrustedStoreConfig for PemTrustedStore {
19 type Error = PemTrustedStoreError;
20
21 fn root_certificates(&self) -> Result<Arc<X509Store>, Self::Error> {
22 let mut trusted_roots =
23 X509StoreBuilder::new().map_err(PemTrustedStoreError::X509StoreBuilder)?;
24 let root_certificates = parse_pem_certificates(&self.root_certificates_pem);
25 for root_certificate in root_certificates {
26 match root_certificate {
27 Ok(root_ca) => trusted_roots
28 .add_cert(root_ca)
29 .map_err(PemTrustedStoreError::AddCert)?,
30 Err(error) => tracing::trace!("Failed to parse Root CA: {error}"),
31 }
32 }
33 Ok(Arc::new(trusted_roots.build()))
34 }
35}
36
37#[nameth]
38#[derive(thiserror::Error, Debug)]
39pub enum PemTrustedStoreError {
40 #[error("[{n}] Failed to create X509StoreBuilder: {0}", n = self.name())]
41 X509StoreBuilder(ErrorStack),
42
43 #[error("[{n}] Failed to add a X509 certificate to the store: {0}", n = self.name())]
44 AddCert(ErrorStack),
45}