tonic/transport/server/
tls.rs1use crate::transport::{
2 service::TlsAcceptor,
3 tls::{Certificate, Identity},
4};
5use std::fmt;
6
7#[cfg(feature = "tls")]
9#[cfg_attr(docsrs, doc(cfg(feature = "tls")))]
10#[derive(Clone, Default)]
11pub struct ServerTlsConfig {
12 identity: Option<Identity>,
13 client_ca_root: Option<Certificate>,
14 rustls_raw: Option<tokio_rustls::rustls::ServerConfig>,
15}
16
17#[cfg(feature = "tls")]
18impl fmt::Debug for ServerTlsConfig {
19 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20 f.debug_struct("ServerTlsConfig").finish()
21 }
22}
23
24#[cfg(feature = "tls")]
25impl ServerTlsConfig {
26 pub fn new() -> Self {
28 ServerTlsConfig {
29 identity: None,
30 client_ca_root: None,
31 rustls_raw: None,
32 }
33 }
34
35 pub fn identity(self, identity: Identity) -> Self {
37 ServerTlsConfig {
38 identity: Some(identity),
39 ..self
40 }
41 }
42
43 pub fn client_ca_root(self, cert: Certificate) -> Self {
45 ServerTlsConfig {
46 client_ca_root: Some(cert),
47 ..self
48 }
49 }
50
51 pub fn rustls_server_config(
55 &mut self,
56 config: tokio_rustls::rustls::ServerConfig,
57 ) -> &mut Self {
58 self.rustls_raw = Some(config);
59 self
60 }
61
62 pub(crate) fn tls_acceptor(&self) -> Result<TlsAcceptor, crate::Error> {
63 match &self.rustls_raw {
64 None => TlsAcceptor::new_with_rustls_identity(
65 self.identity.clone().unwrap(),
66 self.client_ca_root.clone(),
67 ),
68 Some(config) => TlsAcceptor::new_with_rustls_raw(config.clone()),
69 }
70 }
71}