// This code has been taken from the warp project and slightly modified: https://github.com/seanmonstar/warp/blob/master/src/tls.rs // It's needed to create a hyper TLS server. use std::fs::File; use std::future::Future; use std::io::{self, BufReader, Cursor, Read}; use std::path::PathBuf; use std::pin::Pin; use std::sync::Arc; use std::task::{ready, Context, Poll}; use hyper::server::accept::Accept; use hyper::server::conn::{AddrIncoming, AddrStream}; use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; use tokio_rustls::rustls::{Error as TLSError, ServerConfig}; /// Represents errors that can occur building the TlsConfig #[derive(Debug)] pub(crate) enum TlsConfigError { Io(io::Error), /// An Error parsing the Certificate CertParseError, /// An error from an empty key EmptyKey, /// An error from an invalid key InvalidKey(TLSError), } impl std::fmt::Display for TlsConfigError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { TlsConfigError::Io(err) => err.fmt(f), TlsConfigError::CertParseError => write!(f, "certificate parse error"), TlsConfigError::EmptyKey => write!(f, "key contains no private key"), TlsConfigError::InvalidKey(err) => write!(f, "key contains an invalid key, {err}"), } } } impl std::error::Error for TlsConfigError {} impl From for TlsConfigError { fn from(err: io::Error) -> Self { Self::Io(err) } } /// Builder to set the configuration for the Tls server. pub(crate) struct TlsConfigBuilder { cert: Box, key: Box, ocsp_resp: Vec, } impl std::fmt::Debug for TlsConfigBuilder { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { f.debug_struct("TlsConfigBuilder").finish() } } impl TlsConfigBuilder { /// Create a new TlsConfigBuilder pub(crate) fn new() -> TlsConfigBuilder { TlsConfigBuilder { key: Box::new(io::empty()), cert: Box::new(io::empty()), ocsp_resp: Vec::new(), } } /// sets the Tls key via bytes slice pub(crate) fn key(mut self, key: &[u8]) -> Self { self.key = Box::new(Cursor::new(Vec::from(key))); self } /// sets the Tls certificate via bytes slice pub(crate) fn cert(mut self, cert: &[u8]) -> Self { self.cert = Box::new(Cursor::new(Vec::from(cert))); self } pub(crate) fn build(self) -> Result { let mut cert_rdr = BufReader::new(self.cert); let cert = rustls_pemfile::certs(&mut cert_rdr) .collect::, _>>() .map_err(|_| TlsConfigError::CertParseError)?; let Some(key) = rustls_pemfile::private_key(&mut BufReader::new(self.key))? else { return Err(TlsConfigError::EmptyKey); }; let config = ServerConfig::builder() .with_no_client_auth() .with_single_cert_with_ocsp(cert, key, self.ocsp_resp) .map_err(TlsConfigError::InvalidKey)?; Ok(config) } } struct LazyFile { path: PathBuf, file: Option, } impl LazyFile { fn lazy_read(&mut self, buf: &mut [u8]) -> io::Result { if self.file.is_none() { self.file = Some(File::open(&self.path)?); } self.file.as_mut().unwrap().read(buf) } } impl Read for LazyFile { fn read(&mut self, buf: &mut [u8]) -> io::Result { self.lazy_read(buf).map_err(|err| { let kind = err.kind(); io::Error::new(kind, format!("error reading file ({:?}): {}", self.path.display(), err)) }) } } enum State { Handshaking(tokio_rustls::Accept), Streaming(tokio_rustls::server::TlsStream), } // tokio_rustls::server::TlsStream doesn't expose constructor methods, // so we have to TlsAcceptor::accept and handshake to have access to it // TlsStream implements AsyncRead/AsyncWrite handshaking tokio_rustls::Accept first pub(crate) struct TlsStream { state: State, } impl TlsStream { fn new(stream: AddrStream, config: Arc) -> TlsStream { let accept = tokio_rustls::TlsAcceptor::from(config).accept(stream); TlsStream { state: State::Handshaking(accept), } } } impl AsyncRead for TlsStream { fn poll_read(self: Pin<&mut Self>, cx: &mut Context, buf: &mut ReadBuf) -> Poll> { let pin = self.get_mut(); match pin.state { State::Handshaking(ref mut accept) => match ready!(Pin::new(accept).poll(cx)) { Ok(mut stream) => { let result = Pin::new(&mut stream).poll_read(cx, buf); pin.state = State::Streaming(stream); result } Err(err) => Poll::Ready(Err(err)), }, State::Streaming(ref mut stream) => Pin::new(stream).poll_read(cx, buf), } } } impl AsyncWrite for TlsStream { fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll> { let pin = self.get_mut(); match pin.state { State::Handshaking(ref mut accept) => match ready!(Pin::new(accept).poll(cx)) { Ok(mut stream) => { let result = Pin::new(&mut stream).poll_write(cx, buf); pin.state = State::Streaming(stream); result } Err(err) => Poll::Ready(Err(err)), }, State::Streaming(ref mut stream) => Pin::new(stream).poll_write(cx, buf), } } fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { match self.state { State::Handshaking(_) => Poll::Ready(Ok(())), State::Streaming(ref mut stream) => Pin::new(stream).poll_flush(cx), } } fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { match self.state { State::Handshaking(_) => Poll::Ready(Ok(())), State::Streaming(ref mut stream) => Pin::new(stream).poll_shutdown(cx), } } } pub(crate) struct TlsAcceptor { config: Arc, incoming: AddrIncoming, } impl TlsAcceptor { pub(crate) fn new(config: ServerConfig, incoming: AddrIncoming) -> TlsAcceptor { TlsAcceptor { config: Arc::new(config), incoming, } } } impl Accept for TlsAcceptor { type Conn = TlsStream; type Error = io::Error; fn poll_accept(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll>> { let pin = self.get_mut(); match ready!(Pin::new(&mut pin.incoming).poll_accept(cx)) { Some(Ok(sock)) => Poll::Ready(Some(Ok(TlsStream::new(sock, pin.config.clone())))), Some(Err(e)) => Poll::Ready(Some(Err(e))), None => Poll::Ready(None), } } }