Skip to content
Open
Changes from 1 commit
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
85 changes: 79 additions & 6 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ impl TlsAcceptor {
pub struct LazyConfigAcceptor<IO> {
acceptor: rustls::server::Acceptor,
io: Option<IO>,
alert: Option<(rustls::Error, AcceptedAlert)>,
alert: Option<AlertState>,
send_alert: bool,
}

impl<IO> LazyConfigAcceptor<IO>
Expand All @@ -83,9 +84,33 @@ where
acceptor,
io: Some(io),
alert: None,
send_alert: true,
}
}

/// Configure whether to send a TLS alert on failure.
pub fn send_alert(mut self, send: bool) -> Self {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@howardjohn what is the purpose of this? It doesn't make sense to me. If send_alert is true, we've sent the alert before this is reachable. If send_alert is false, I think you can only call this without taking out either the io or the alert, and there's no way you can actually check what the server received, I think?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is meant to be set before you poll, not after. We could also instead do new_without_alert() or equivilent?

(just a note, this was from your initial commit not my testing change)

self.send_alert = send;
self
}

/// Writes a stored alert, consuming the alert (if any) and IO.
pub async fn write_alert(&mut self) -> io::Result<()> {
let Some(alert) = self.take_alert() else {
return Ok(());
};

let Some(io) = self.take_io() else {
return Ok(());
};

WritingAlert {
io,
alert: Some(alert),
}
.await
}

/// Takes back the client connection. Will return `None` if called more than once or if the
/// connection has been accepted.
///
Expand Down Expand Up @@ -130,6 +155,14 @@ where
pub fn take_io(&mut self) -> Option<IO> {
self.io.take()
}

pub fn take_alert(&mut self) -> Option<AcceptedAlert> {
match self.alert.take() {
Some(AlertState::Sending(_, alert)) => Some(alert),
Some(AlertState::Saved(alert)) => Some(alert),
None => None,
}
}
}

impl<IO> Future for LazyConfigAcceptor<IO>
Expand All @@ -151,17 +184,17 @@ where
}
};

if let Some((err, mut alert)) = this.alert.take() {
if let Some(AlertState::Sending(err, mut alert)) = this.alert.take() {
match alert.write(&mut SyncWriteAdapter { io, cx }) {
Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
this.alert = Some((err, alert));
this.alert = Some(AlertState::Sending(err, alert));
return Poll::Pending;
}
Ok(0) | Err(_) => {
return Poll::Ready(Err(io::Error::new(io::ErrorKind::InvalidData, err)))
}
Ok(_) => {
this.alert = Some((err, alert));
this.alert = Some(AlertState::Sending(err, alert));
continue;
}
};
Expand All @@ -181,9 +214,49 @@ where
return Poll::Ready(Ok(StartHandshake { accepted, io }));
}
Ok(None) => {}
Err((err, alert)) => {
this.alert = Some((err, alert));
Err((err, alert)) => match this.send_alert {
true => this.alert = Some(AlertState::Sending(err, alert)),
false => {
this.alert = Some(AlertState::Saved(alert));
return Poll::Ready(Err(io::Error::new(io::ErrorKind::InvalidData, err)));
}
},
}
}
}
}

enum AlertState {
Sending(rustls::Error, AcceptedAlert),
Saved(AcceptedAlert),
}

struct WritingAlert<IO> {
io: IO,
alert: Option<AcceptedAlert>,
}

impl<IO> Future for WritingAlert<IO>
where
IO: AsyncRead + AsyncWrite + Unpin,
{
type Output = Result<(), io::Error>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
let io = &mut this.io;
loop {
let Some(mut alert) = this.alert.take() else {
return Poll::Ready(Ok(()));
};

match alert.write(&mut SyncWriteAdapter { io, cx }) {
Ok(0) => return Poll::Ready(Ok(())),
Ok(_) => continue,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty sure this is wrong. In case of a partial write, just calling alert.write() again is wrong, since it'll duplicate the parts of the alert that were sent previously.

(Presumably it would be exceedingly rare to get a partial write, but still...)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this works and is tested. The test uses a duplex stream with a capacity of 2 to ensure we cannot write it in one chunk. This is the same approach used in another test:

async fn lazy_config_acceptor_alert() {
    // Intentionally small so that we have to call alert.write several times

Looking at the alert.write() function, it does seem to consume the data it wrote such that multiple calls to the same alert.write() are valid IIUC

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, for some reason it worked on my branch but not the PR now. let me see what changed.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Ok(_) => continue,
Ok(_) => {
this.alert = Some(alert);
continue;
}

This fixes the issue and aligns with how LazyConfigAcceptor writes the alert

Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
this.alert = Some(alert);
return Poll::Pending;
}
Err(e) => return Poll::Ready(Err(io::Error::new(io::ErrorKind::InvalidData, e))),
}
}
}
Expand Down