tls_api_native_tls/
acceptor.rs

1use crate::handshake::HandshakeFuture;
2
3use std::future::Future;
4use tls_api::async_as_sync::AsyncIoAsSyncIo;
5use tls_api::spi_acceptor_common;
6use tls_api::AsyncSocket;
7use tls_api::AsyncSocketBox;
8use tls_api::ImplInfo;
9
10pub struct TlsAcceptorBuilder(pub native_tls::TlsAcceptorBuilder);
11pub struct TlsAcceptor(pub native_tls::TlsAcceptor);
12
13// TlsAcceptor and TlsAcceptorBuilder
14
15impl tls_api::TlsAcceptorBuilder for TlsAcceptorBuilder {
16    type Acceptor = TlsAcceptor;
17
18    type Underlying = native_tls::TlsAcceptorBuilder;
19
20    fn set_alpn_protocols(&mut self, _protocols: &[&[u8]]) -> anyhow::Result<()> {
21        Err(crate::Error::AlpnNotSupported.into())
22    }
23
24    fn underlying_mut(&mut self) -> &mut native_tls::TlsAcceptorBuilder {
25        &mut self.0
26    }
27
28    fn build(self) -> anyhow::Result<TlsAcceptor> {
29        self.0.build().map(TlsAcceptor).map_err(anyhow::Error::new)
30    }
31}
32
33impl TlsAcceptor {
34    fn accept_impl<S>(
35        &self,
36        stream: S,
37    ) -> impl Future<Output = anyhow::Result<crate::TlsStream<S>>> + '_
38    where
39        S: AsyncSocket,
40    {
41        HandshakeFuture::Initial(move |s| self.0.accept(s), AsyncIoAsSyncIo::new(stream))
42    }
43}
44
45impl tls_api::TlsAcceptor for TlsAcceptor {
46    type Builder = TlsAcceptorBuilder;
47
48    type Underlying = native_tls::TlsAcceptor;
49    type TlsStream = crate::TlsStream<AsyncSocketBox>;
50
51    fn underlying_mut(&mut self) -> &mut Self::Underlying {
52        &mut self.0
53    }
54
55    const IMPLEMENTED: bool = true;
56    /// Server side of `native-tls` does not support ALPN,
57    /// because `security-framework` does not support it.
58    const SUPPORTS_ALPN: bool = false;
59    const SUPPORTS_DER_KEYS: bool = false;
60    const SUPPORTS_PKCS12_KEYS: bool = true;
61
62    fn info() -> ImplInfo {
63        crate::info()
64    }
65
66    fn builder_from_pkcs12(pkcs12: &[u8], passphrase: &str) -> anyhow::Result<Self::Builder> {
67        Ok(TlsAcceptorBuilder(native_tls::TlsAcceptor::builder(
68            native_tls::Identity::from_pkcs12(pkcs12, passphrase).map_err(anyhow::Error::new)?,
69        )))
70    }
71
72    spi_acceptor_common!(crate::TlsStream<S>);
73}