#![doc(html_root_url = "https://docs.rs/tonic-openssl/0.2.0")]
#![warn(
missing_debug_implementations,
missing_docs,
rust_2018_idioms,
unreachable_pub
)]
use async_stream::try_stream;
use futures::{Stream, TryStream, TryStreamExt};
use openssl::ssl::{Ssl, SslAcceptor};
use std::{
fmt::Debug,
pin::Pin,
sync::Arc,
task::{Context, Poll},
};
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use tonic::transport::{server::Connected, Certificate};
pub type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
pub const ALPN_H2_WIRE: &[u8] = b"\x02h2";
pub fn incoming<S>(
incoming: S,
acceptor: SslAcceptor,
) -> impl Stream<Item = Result<SslStream<S::Ok>, Error>>
where
S: TryStream + Unpin,
S::Ok: AsyncRead + AsyncWrite + Send + Sync + Debug + Unpin + 'static,
S::Error: Into<crate::Error>,
{
let mut incoming = incoming;
try_stream! {
while let Some(stream) = incoming.try_next().await? {
let ssl = Ssl::new(acceptor.context())?;
let mut tls = tokio_openssl::SslStream::new(ssl, stream)?;
Pin::new(&mut tls).accept().await?;
let ssl = SslStream {
inner: tls
};
yield ssl;
}
}
}
#[derive(Debug)]
pub struct SslStream<S> {
inner: tokio_openssl::SslStream<S>,
}
impl<S: Connected> Connected for SslStream<S> {
type ConnectInfo = SslConnectInfo<S::ConnectInfo>;
fn connect_info(&self) -> Self::ConnectInfo {
let inner = self.inner.get_ref().connect_info();
let ssl = self.inner.ssl();
let certs = ssl
.verified_chain()
.map(|certs| {
certs
.iter()
.filter_map(|c| c.to_pem().ok())
.map(Certificate::from_pem)
.collect()
})
.map(Arc::new);
SslConnectInfo { inner, certs }
}
}
impl<S> AsyncRead for SslStream<S>
where
S: AsyncRead + AsyncWrite + Unpin,
{
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<std::io::Result<()>> {
Pin::new(&mut self.inner).poll_read(cx, buf)
}
}
impl<S> AsyncWrite for SslStream<S>
where
S: AsyncRead + AsyncWrite + Unpin,
{
fn poll_write(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<std::io::Result<usize>> {
Pin::new(&mut self.inner).poll_write(cx, buf)
}
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
Pin::new(&mut self.inner).poll_flush(cx)
}
fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
Pin::new(&mut self.inner).poll_shutdown(cx)
}
}
#[derive(Debug, Clone)]
pub struct SslConnectInfo<T> {
inner: T,
certs: Option<Arc<Vec<Certificate>>>,
}
impl<T> SslConnectInfo<T> {
pub fn get_ref(&self) -> &T {
&self.inner
}
pub fn get_mut(&mut self) -> &mut T {
&mut self.inner
}
pub fn peer_certs(&self) -> Option<Arc<Vec<Certificate>>> {
self.certs.clone()
}
}