rama_net/tls/server/
config.rs1use crate::{
2 address::Host,
3 tls::{ApplicationProtocol, DataEncoding, KeyLogIntent, ProtocolVersion, client::ClientHello},
4};
5use rama_core::error::OpaqueError;
6use serde::{Deserialize, Serialize};
7use std::{num::NonZeroU64, pin::Pin, sync::Arc};
8
9#[derive(Debug, Clone)]
10pub struct ServerConfig {
12 pub server_auth: ServerAuth,
14
15 pub expose_server_cert: bool,
20
21 pub protocol_versions: Option<Vec<ProtocolVersion>>,
23
24 pub application_layer_protocol_negotiation: Option<Vec<ApplicationProtocol>>,
26
27 pub client_verify_mode: ClientVerifyMode,
29
30 pub key_logger: KeyLogIntent,
32
33 pub store_client_certificate_chain: bool,
35}
36
37impl ServerConfig {
38 pub fn new(auth: ServerAuth) -> Self {
40 Self {
41 server_auth: auth,
42 expose_server_cert: false,
43 protocol_versions: None,
44 application_layer_protocol_negotiation: None,
45 client_verify_mode: ClientVerifyMode::default(),
46 key_logger: KeyLogIntent::default(),
47 store_client_certificate_chain: false,
48 }
49 }
50}
51
52#[derive(Debug, Clone)]
53pub enum ServerAuth {
55 SelfSigned(SelfSignedData),
57 Single(ServerAuthData),
59 CertIssuer(ServerCertIssuerData),
61}
62
63impl Default for ServerAuth {
64 fn default() -> Self {
65 ServerAuth::SelfSigned(SelfSignedData::default())
66 }
67}
68
69#[derive(Debug, Clone, Default)]
70pub struct ServerCertIssuerData {
71 pub kind: ServerCertIssuerKind,
73 pub cache_kind: CacheKind,
75}
76
77#[derive(Debug, Clone)]
78pub enum CacheKind {
80 MemCache { max_size: NonZeroU64 },
81 Disabled,
82}
83
84impl Default for CacheKind {
85 fn default() -> Self {
86 Self::MemCache {
87 max_size: NonZeroU64::new(8096).unwrap(),
88 }
89 }
90}
91
92#[derive(Debug, Clone)]
93pub enum ServerCertIssuerKind {
96 SelfSigned(SelfSignedData),
98 Single(ServerAuthData),
100 Dynamic(DynamicIssuer),
102}
103
104impl Default for ServerCertIssuerKind {
105 fn default() -> Self {
106 ServerCertIssuerKind::SelfSigned(SelfSignedData::default())
107 }
108}
109
110impl<T> From<T> for ServerCertIssuerKind
111where
112 T: DynamicCertIssuer,
113{
114 fn from(issuer: T) -> Self {
115 Self::Dynamic(DynamicIssuer::new(issuer))
116 }
117}
118
119#[derive(Debug, Clone, Serialize, Deserialize, Default)]
120pub struct SelfSignedData {
122 pub organisation_name: Option<String>,
124 pub common_name: Option<Host>,
128 pub subject_alternative_names: Option<Vec<String>>,
131}
132
133#[derive(Debug, Clone)]
134pub struct ServerAuthData {
136 pub private_key: DataEncoding,
138 pub cert_chain: DataEncoding,
140
141 pub ocsp: Option<Vec<u8>>,
143}
144
145#[derive(Clone)]
146pub struct DynamicIssuer {
148 issuer: Arc<dyn DynDynamicCertIssuer + Send + Sync>,
150}
151
152impl DynamicIssuer {
153 pub fn new<T: DynamicCertIssuer>(issuer: T) -> Self {
154 Self {
155 issuer: Arc::new(issuer),
156 }
157 }
158
159 pub async fn issue_cert(
160 &self,
161 client_hello: ClientHello,
162 server_name: Option<Host>,
163 ) -> Result<ServerAuthData, OpaqueError> {
164 self.issuer.issue_cert(client_hello, server_name).await
165 }
166}
167
168impl std::fmt::Debug for DynamicIssuer {
169 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
170 f.debug_struct("DynamicIssuer").finish()
171 }
172}
173
174pub trait DynamicCertIssuer: Send + Sync + 'static {
177 fn issue_cert(
178 &self,
179 client_hello: ClientHello,
180 server_name: Option<Host>,
181 ) -> impl Future<Output = Result<ServerAuthData, OpaqueError>> + Send + Sync + '_;
182}
183
184trait DynDynamicCertIssuer {
187 fn issue_cert(
188 &self,
189 client_hello: ClientHello,
190 server_name: Option<Host>,
191 ) -> Pin<Box<dyn Future<Output = Result<ServerAuthData, OpaqueError>> + Send + Sync + '_>>;
192}
193
194impl<T> DynDynamicCertIssuer for T
195where
196 T: DynamicCertIssuer,
197{
198 fn issue_cert(
199 &self,
200 client_hello: ClientHello,
201 server_name: Option<Host>,
202 ) -> Pin<Box<dyn Future<Output = Result<ServerAuthData, OpaqueError>> + Send + Sync + '_>> {
203 Box::pin(self.issue_cert(client_hello, server_name))
204 }
205}
206
207#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
208pub enum ClientVerifyMode {
210 #[default]
211 Auto,
214 Disable,
216 ClientAuth(DataEncoding),
218}