Skip to content

Commit 0587801

Browse files
ctzdjc
authored andcommitted
Take rustls 0.23
- track new alert-sending API for Acceptor.
1 parent 096b161 commit 0587801

File tree

4 files changed

+37
-8
lines changed

4 files changed

+37
-8
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ exclude = ["/.github", "/examples", "/scripts"]
1414

1515
[dependencies]
1616
tokio = "1.0"
17-
rustls = { version = "0.22", default-features = false }
17+
rustls = { version = "0.23", default-features = false, features = ["std"] }
1818
pki-types = { package = "rustls-pki-types", version = "1" }
1919

2020
[features]

src/common/handshake.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ use std::pin::Pin;
44
use std::task::{Context, Poll};
55
use std::{io, mem};
66

7+
use rustls::server::AcceptedAlert;
78
use rustls::{ConnectionCommon, SideData};
89
use tokio::io::{AsyncRead, AsyncWrite};
910

10-
use crate::common::{Stream, TlsState};
11+
use crate::common::{Stream, SyncWriteAdapter, TlsState};
1112

1213
pub(crate) trait IoSession {
1314
type Io;
@@ -21,7 +22,15 @@ pub(crate) trait IoSession {
2122
pub(crate) enum MidHandshake<IS: IoSession> {
2223
Handshaking(IS),
2324
End,
24-
Error { io: IS::Io, error: io::Error },
25+
SendAlert {
26+
io: IS::Io,
27+
alert: AcceptedAlert,
28+
error: io::Error,
29+
},
30+
Error {
31+
io: IS::Io,
32+
error: io::Error,
33+
},
2534
}
2635

2736
impl<IS, SD> Future for MidHandshake<IS>
@@ -38,6 +47,15 @@ where
3847

3948
let mut stream = match mem::replace(this, MidHandshake::End) {
4049
MidHandshake::Handshaking(stream) => stream,
50+
MidHandshake::SendAlert {
51+
mut io,
52+
mut alert,
53+
error,
54+
} => {
55+
let mut writer = SyncWriteAdapter { io: &mut io, cx };
56+
let _ = alert.write(&mut writer); // best effort
57+
return Poll::Ready(Err((error, io)));
58+
}
4159
// Starting the handshake returned an error; fail the future immediately.
4260
MidHandshake::Error { io, error } => return Poll::Ready(Err((error, io))),
4361
_ => panic!("unexpected polling after handshake"),

src/lib.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,10 @@ where
288288
return Poll::Ready(Ok(StartHandshake { accepted, io }));
289289
}
290290
Ok(None) => continue,
291-
Err(err) => {
292-
return Poll::Ready(Err(io::Error::new(io::ErrorKind::InvalidInput, err)))
291+
Err((err, mut alert)) => {
292+
let mut writer = common::SyncWriteAdapter { io, cx };
293+
let _ = alert.write(&mut writer); // best effort
294+
return Poll::Ready(Err(io::Error::new(io::ErrorKind::InvalidInput, err)));
293295
}
294296
}
295297
}
@@ -319,9 +321,10 @@ where
319321
{
320322
let mut conn = match self.accepted.into_connection(config) {
321323
Ok(conn) => conn,
322-
Err(error) => {
323-
return Accept(MidHandshake::Error {
324+
Err((error, alert)) => {
325+
return Accept(MidHandshake::SendAlert {
324326
io: self.io,
327+
alert,
325328
// TODO(eliza): should this really return an `io::Error`?
326329
// Probably not...
327330
error: io::Error::new(io::ErrorKind::Other, error),
@@ -361,6 +364,7 @@ impl<IO> Connect<IO> {
361364
pub fn get_ref(&self) -> Option<&IO> {
362365
match &self.0 {
363366
MidHandshake::Handshaking(sess) => Some(sess.get_ref().0),
367+
MidHandshake::SendAlert { io, .. } => Some(io),
364368
MidHandshake::Error { io, .. } => Some(io),
365369
MidHandshake::End => None,
366370
}
@@ -369,6 +373,7 @@ impl<IO> Connect<IO> {
369373
pub fn get_mut(&mut self) -> Option<&mut IO> {
370374
match &mut self.0 {
371375
MidHandshake::Handshaking(sess) => Some(sess.get_mut().0),
376+
MidHandshake::SendAlert { io, .. } => Some(io),
372377
MidHandshake::Error { io, .. } => Some(io),
373378
MidHandshake::End => None,
374379
}
@@ -384,6 +389,7 @@ impl<IO> Accept<IO> {
384389
pub fn get_ref(&self) -> Option<&IO> {
385390
match &self.0 {
386391
MidHandshake::Handshaking(sess) => Some(sess.get_ref().0),
392+
MidHandshake::SendAlert { io, .. } => Some(io),
387393
MidHandshake::Error { io, .. } => Some(io),
388394
MidHandshake::End => None,
389395
}
@@ -392,6 +398,7 @@ impl<IO> Accept<IO> {
392398
pub fn get_mut(&mut self) -> Option<&mut IO> {
393399
match &mut self.0 {
394400
MidHandshake::Handshaking(sess) => Some(sess.get_mut().0),
401+
MidHandshake::SendAlert { io, .. } => Some(io),
395402
MidHandshake::Error { io, .. } => Some(io),
396403
MidHandshake::End => None,
397404
}

tests/test.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,16 @@ async fn lazy_config_acceptor_take_io() -> Result<(), rustls::Error> {
223223
}
224224

225225
let server_msg = b"message from server";
226+
let fatal_alert_decode_error = b"\x15\x03\x03\x00\x02\x02\x32";
226227

227228
let some_io = acceptor.take_io();
228229
assert!(some_io.is_some(), "Expected Some(io)");
229230
some_io.unwrap().write_all(server_msg).await.unwrap();
230231

231-
assert_eq!(rx.await.unwrap(), server_msg);
232+
assert_eq!(
233+
rx.await.unwrap(),
234+
[&fatal_alert_decode_error[..], &server_msg[..]].concat()
235+
);
232236

233237
assert!(
234238
acceptor.take_io().is_none(),

0 commit comments

Comments
 (0)