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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright 2024 New Vector Ltd.
// Copyright 2022-2024 The Matrix.org Foundation C.I.C.
//
// SPDX-License-Identifier: AGPL-3.0-only
// Please see LICENSE in the repository root for full details.

use std::{
    pin::Pin,
    sync::Arc,
    task::{Context, Poll},
};

use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use tokio_rustls::{
    rustls::{
        pki_types::CertificateDer, ProtocolVersion, ServerConfig, ServerConnection,
        SupportedCipherSuite,
    },
    TlsAcceptor,
};

#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct TlsStreamInfo {
    pub protocol_version: ProtocolVersion,
    pub negotiated_cipher_suite: SupportedCipherSuite,
    pub sni_hostname: Option<String>,
    pub alpn_protocol: Option<Vec<u8>>,
    pub peer_certificates: Option<Vec<CertificateDer<'static>>>,
}

impl TlsStreamInfo {
    #[must_use]
    pub fn is_alpn_h2(&self) -> bool {
        matches!(self.alpn_protocol.as_deref(), Some(b"h2"))
    }
}

pin_project_lite::pin_project! {
    #[project = MaybeTlsStreamProj]
    pub enum MaybeTlsStream<T> {
        Secure {
            #[pin]
            stream: tokio_rustls::server::TlsStream<T>
        },
        Insecure {
            #[pin]
            stream: T,
        },
    }
}

impl<T> MaybeTlsStream<T> {
    /// Get a reference to the underlying IO stream
    ///
    /// Returns [`None`] if the stream closed before the TLS handshake finished.
    /// It is guaranteed to return [`Some`] value after the handshake finished,
    /// or if it is a non-TLS connection.
    pub fn get_ref(&self) -> &T {
        match self {
            Self::Secure { stream } => stream.get_ref().0,
            Self::Insecure { stream } => stream,
        }
    }

    /// Get a ref to the [`ServerConnection`] of the establish TLS stream.
    ///
    /// Returns [`None`] for non-TLS connections.
    pub fn get_tls_connection(&self) -> Option<&ServerConnection> {
        match self {
            Self::Secure { stream } => Some(stream.get_ref().1),
            Self::Insecure { .. } => None,
        }
    }

    /// Gather informations about the TLS connection. Returns `None` if the
    /// stream is not a TLS stream.
    ///
    /// # Panics
    ///
    /// Panics if the TLS handshake is not done yet, which should never happen
    pub fn tls_info(&self) -> Option<TlsStreamInfo> {
        let conn = self.get_tls_connection()?;

        // SAFETY: we're getting the protocol version and cipher suite *after* the
        // handshake, so this should never lead to a panic
        let protocol_version = conn
            .protocol_version()
            .expect("TLS handshake is not done yet");
        let negotiated_cipher_suite = conn
            .negotiated_cipher_suite()
            .expect("TLS handshake is not done yet");

        let sni_hostname = conn.server_name().map(ToOwned::to_owned);
        let alpn_protocol = conn.alpn_protocol().map(ToOwned::to_owned);
        let peer_certificates = conn.peer_certificates().map(|certs| {
            certs
                .iter()
                .cloned()
                .map(CertificateDer::into_owned)
                .collect()
        });
        Some(TlsStreamInfo {
            protocol_version,
            negotiated_cipher_suite,
            sni_hostname,
            alpn_protocol,
            peer_certificates,
        })
    }
}

impl<T> AsyncRead for MaybeTlsStream<T>
where
    T: AsyncRead + AsyncWrite + Unpin,
{
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut Context,
        buf: &mut ReadBuf,
    ) -> Poll<std::io::Result<()>> {
        match self.project() {
            MaybeTlsStreamProj::Secure { stream } => stream.poll_read(cx, buf),
            MaybeTlsStreamProj::Insecure { stream } => stream.poll_read(cx, buf),
        }
    }
}

impl<T> AsyncWrite for MaybeTlsStream<T>
where
    T: AsyncRead + AsyncWrite + Unpin,
{
    fn poll_write(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &[u8],
    ) -> Poll<std::io::Result<usize>> {
        match self.project() {
            MaybeTlsStreamProj::Secure { stream } => stream.poll_write(cx, buf),
            MaybeTlsStreamProj::Insecure { stream } => stream.poll_write(cx, buf),
        }
    }

    fn poll_write_vectored(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        bufs: &[std::io::IoSlice<'_>],
    ) -> Poll<Result<usize, std::io::Error>> {
        match self.project() {
            MaybeTlsStreamProj::Secure { stream } => stream.poll_write_vectored(cx, bufs),
            MaybeTlsStreamProj::Insecure { stream } => stream.poll_write_vectored(cx, bufs),
        }
    }

    fn is_write_vectored(&self) -> bool {
        match self {
            Self::Secure { stream } => stream.is_write_vectored(),
            Self::Insecure { stream } => stream.is_write_vectored(),
        }
    }

    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
        match self.project() {
            MaybeTlsStreamProj::Secure { stream } => stream.poll_flush(cx),
            MaybeTlsStreamProj::Insecure { stream } => stream.poll_flush(cx),
        }
    }

    fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
        match self.project() {
            MaybeTlsStreamProj::Secure { stream } => stream.poll_shutdown(cx),
            MaybeTlsStreamProj::Insecure { stream } => stream.poll_shutdown(cx),
        }
    }
}

#[derive(Clone)]
pub struct MaybeTlsAcceptor {
    tls_config: Option<Arc<ServerConfig>>,
}

impl MaybeTlsAcceptor {
    #[must_use]
    pub fn new(tls_config: Option<Arc<ServerConfig>>) -> Self {
        Self { tls_config }
    }

    #[must_use]
    pub fn new_secure(tls_config: Arc<ServerConfig>) -> Self {
        Self {
            tls_config: Some(tls_config),
        }
    }

    #[must_use]
    pub fn new_insecure() -> Self {
        Self { tls_config: None }
    }

    #[must_use]
    pub const fn is_secure(&self) -> bool {
        self.tls_config.is_some()
    }

    /// Accept a connection and do the TLS handshake
    ///
    /// # Errors
    ///
    /// Returns an error if the TLS handshake failed
    pub async fn accept<T>(&self, stream: T) -> Result<MaybeTlsStream<T>, std::io::Error>
    where
        T: AsyncRead + AsyncWrite + Unpin,
    {
        match &self.tls_config {
            Some(config) => {
                let acceptor = TlsAcceptor::from(config.clone());
                let stream = acceptor.accept(stream).await?;
                Ok(MaybeTlsStream::Secure { stream })
            }
            None => Ok(MaybeTlsStream::Insecure { stream }),
        }
    }
}