aws_smithy_http_client/client/
tls.rs1use crate::cfg::{cfg_rustls, cfg_s2n_tls};
6use crate::HttpClientError;
7
8#[derive(Debug, PartialEq, Clone)]
10#[non_exhaustive]
11pub enum Provider {
12 #[cfg(feature = "__rustls")]
13 Rustls(rustls_provider::CryptoMode),
15 #[cfg(feature = "s2n-tls")]
17 S2nTls,
18}
19
20#[cfg(not(all(aws_sdk_unstable, feature = "__rustls")))]
21impl Eq for Provider {}
22
23#[derive(Debug, Clone)]
25pub struct TlsContext {
26 #[allow(unused)]
27 trust_store: TrustStore,
28}
29
30impl TlsContext {
31 pub fn builder() -> TlsContextBuilder {
33 TlsContextBuilder::new()
34 }
35}
36
37impl Default for TlsContext {
38 fn default() -> Self {
39 TlsContext::builder().build().expect("valid default config")
40 }
41}
42
43#[derive(Debug)]
45pub struct TlsContextBuilder {
46 trust_store: TrustStore,
47}
48
49impl TlsContextBuilder {
50 fn new() -> Self {
51 TlsContextBuilder {
52 trust_store: TrustStore::default(),
53 }
54 }
55
56 pub fn with_trust_store(mut self, trust_store: TrustStore) -> Self {
58 self.trust_store = trust_store;
59 self
60 }
61
62 pub fn build(self) -> Result<TlsContext, HttpClientError> {
64 Ok(TlsContext {
65 trust_store: self.trust_store,
66 })
67 }
68}
69
70#[allow(unused)]
72#[derive(Debug, Clone)]
73struct CertificatePEM(Vec<u8>);
74
75impl From<&[u8]> for CertificatePEM {
76 fn from(value: &[u8]) -> Self {
77 CertificatePEM(value.to_vec())
78 }
79}
80
81#[derive(Debug, Clone)]
86pub struct TrustStore {
87 enable_native_roots: bool,
88 custom_certs: Vec<CertificatePEM>,
89}
90
91impl TrustStore {
92 pub fn empty() -> Self {
94 Self {
95 enable_native_roots: false,
96 custom_certs: Vec::new(),
97 }
98 }
99
100 pub fn with_native_roots(mut self, enable_native_roots: bool) -> Self {
104 self.enable_native_roots = enable_native_roots;
105 self
106 }
107
108 pub fn with_pem_certificate(mut self, pem_bytes: impl Into<Vec<u8>>) -> Self {
114 self.custom_certs.push(CertificatePEM(pem_bytes.into()));
117 self
118 }
119
120 pub fn add_pem_certificate(&mut self, pem_bytes: impl Into<Vec<u8>>) -> &mut Self {
126 self.custom_certs.push(CertificatePEM(pem_bytes.into()));
127 self
128 }
129}
130
131impl Default for TrustStore {
132 fn default() -> Self {
133 Self {
134 enable_native_roots: true,
135 custom_certs: Vec::new(),
136 }
137 }
138}
139
140cfg_rustls! {
141 pub mod rustls_provider;
143}
144
145cfg_s2n_tls! {
146 pub(crate) mod s2n_tls_provider;
148}