Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce tokio_boring::SslStreamBuilder #201

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions boring/src/ssl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3903,6 +3903,11 @@ impl<S> SslStreamBuilder<S> {
&self.inner.ssl
}

/// Returns a mutable reference to the `Ssl` object associated with this builder.
pub fn ssl_mut(&mut self) -> &mut SslRef {
&mut self.inner.ssl
}

/// Set the DTLS MTU size.
///
/// It will be ignored if the value is smaller than the minimum packet size
Expand Down
65 changes: 50 additions & 15 deletions tokio-boring/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#![warn(missing_docs)]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]

use boring::error::ErrorStack;
use boring::ssl::{
self, ConnectConfiguration, ErrorCode, MidHandshakeSslStream, ShutdownResult, SslAcceptor,
SslRef,
Expand Down Expand Up @@ -51,7 +50,11 @@ pub async fn connect<S>(
where
S: AsyncRead + AsyncWrite + Unpin,
{
handshake(|s| config.setup_connect(domain, s), stream).await
let mid_handshake = config
.setup_connect(domain, AsyncStreamBridge::new(stream))
.map_err(|err| HandshakeError(ssl::HandshakeError::SetupFailure(err)))?;

HandshakeFuture(Some(mid_handshake)).await
}

/// Asynchronously performs a server-side TLS handshake over the provided stream.
Expand All @@ -62,19 +65,8 @@ pub async fn accept<S>(acceptor: &SslAcceptor, stream: S) -> Result<SslStream<S>
where
S: AsyncRead + AsyncWrite + Unpin,
{
handshake(|s| acceptor.setup_accept(s), stream).await
}

async fn handshake<S>(
f: impl FnOnce(
AsyncStreamBridge<S>,
) -> Result<MidHandshakeSslStream<AsyncStreamBridge<S>>, ErrorStack>,
stream: S,
) -> Result<SslStream<S>, HandshakeError<S>>
where
S: AsyncRead + AsyncWrite + Unpin,
{
let mid_handshake = f(AsyncStreamBridge::new(stream))
let mid_handshake = acceptor
.setup_accept(AsyncStreamBridge::new(stream))
.map_err(|err| HandshakeError(ssl::HandshakeError::SetupFailure(err)))?;

HandshakeFuture(Some(mid_handshake)).await
Expand All @@ -88,6 +80,49 @@ fn cvt<T>(r: io::Result<T>) -> Poll<io::Result<T>> {
}
}

/// A partially constructed `SslStream`, useful for unusual handshakes.
pub struct SslStreamBuilder<S> {
inner: ssl::SslStreamBuilder<AsyncStreamBridge<S>>,
}

impl<S> SslStreamBuilder<S>
where
S: AsyncRead + AsyncWrite + Unpin,
{
/// Begins creating an `SslStream` atop `stream`.
pub fn new(ssl: ssl::Ssl, stream: S) -> Self {
Self {
inner: ssl::SslStreamBuilder::new(ssl, AsyncStreamBridge::new(stream)),
}
}

/// Initiates a client-side TLS handshake.
pub async fn accept(self) -> Result<SslStream<S>, HandshakeError<S>> {
let mid_handshake = self.inner.setup_accept();

HandshakeFuture(Some(mid_handshake)).await
}

/// Initiates a server-side TLS handshake.
pub async fn connect(self) -> Result<SslStream<S>, HandshakeError<S>> {
let mid_handshake = self.inner.setup_connect();

HandshakeFuture(Some(mid_handshake)).await
}
}

impl<S> SslStreamBuilder<S> {
/// Returns a shared reference to the `Ssl` object associated with this builder.
pub fn ssl(&self) -> &SslRef {
self.inner.ssl()
}

/// Returns a mutable reference to the `Ssl` object associated with this builder.
pub fn ssl_mut(&mut self) -> &mut SslRef {
self.inner.ssl_mut()
}
}

/// A wrapper around an underlying raw stream which implements the SSL
/// protocol.
///
Expand Down
Loading