1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use std::sync::Arc;
use rustls::StreamOwned;
use tls_api::async_as_sync::AsyncIoAsSyncIo;
use tls_api::spi_acceptor_common;
use tls_api::AsyncSocket;
use tls_api::AsyncSocketBox;
use tls_api::BoxFuture;
use tls_api::ImplInfo;
use crate::handshake::HandshakeFuture;
use crate::RustlsStream;
use std::future::Future;
pub struct TlsAcceptorBuilder(pub rustls::ServerConfig);
pub struct TlsAcceptor(pub Arc<rustls::ServerConfig>);
impl tls_api::TlsAcceptorBuilder for TlsAcceptorBuilder {
type Acceptor = TlsAcceptor;
type Underlying = rustls::ServerConfig;
fn set_alpn_protocols(&mut self, protocols: &[&[u8]]) -> anyhow::Result<()> {
self.0.alpn_protocols = protocols.into_iter().map(|p| p.to_vec()).collect();
Ok(())
}
fn underlying_mut(&mut self) -> &mut rustls::ServerConfig {
&mut self.0
}
fn build(self) -> anyhow::Result<TlsAcceptor> {
Ok(TlsAcceptor(Arc::new(self.0)))
}
}
impl TlsAcceptor {
pub fn accept_impl<'a, S>(
&'a self,
stream: S,
) -> impl Future<Output = anyhow::Result<crate::TlsStream<S>>> + 'a
where
S: AsyncSocket,
{
let conn = rustls::ServerConnection::new(self.0.clone());
let conn = match conn.map_err(|e| anyhow::Error::new(e)) {
Ok(conn) => conn,
Err(e) => return BoxFuture::new(async { Err(e) }),
};
let tls_stream: crate::TlsStream<S> =
crate::TlsStream::new(RustlsStream::Server(StreamOwned {
sock: AsyncIoAsSyncIo::new(stream),
conn,
}));
BoxFuture::new(HandshakeFuture::MidHandshake(tls_stream))
}
}
impl tls_api::TlsAcceptor for TlsAcceptor {
type Builder = TlsAcceptorBuilder;
type Underlying = Arc<rustls::ServerConfig>;
type TlsStream = crate::TlsStream<AsyncSocketBox>;
fn underlying_mut(&mut self) -> &mut Self::Underlying {
&mut self.0
}
const IMPLEMENTED: bool = true;
const SUPPORTS_ALPN: bool = true;
const SUPPORTS_DER_KEYS: bool = true;
const SUPPORTS_PKCS12_KEYS: bool = false;
fn info() -> ImplInfo {
crate::info()
}
fn builder_from_der_key(cert: &[u8], key: &[u8]) -> anyhow::Result<TlsAcceptorBuilder> {
let cert = rustls::Certificate(cert.to_vec());
let config = rustls::ServerConfig::builder()
.with_safe_defaults()
.with_no_client_auth()
.with_single_cert(vec![cert], rustls::PrivateKey(key.to_vec()))
.map_err(anyhow::Error::new)?;
Ok(TlsAcceptorBuilder(config))
}
spi_acceptor_common!();
}