Skip to content

Commit f409cc1

Browse files
committed
Update rustls to version 0.23
Updates the code to compile with newer versions of `rustls` and `rustls_pemfile`. Replaces `lazy_static` with `std::sync::LazyLock`, because `lazy_static` could not handle `async move` construct for unknown reason, and it's recommended to replace it anyways. Fixes some warnings generated by `cargo clippy`, mostly lifetime elisions. I didn't fix the warnings regarding the large size difference between variants of enums, because I wasn't sure about the correct fix. The `Poll:Ok` would probably still return the large data instead of the Box anyways.
1 parent 245641a commit f409cc1

File tree

6 files changed

+88
-94
lines changed

6 files changed

+88
-94
lines changed

Diff for: Cargo.toml

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "async-tls"
3-
version = "0.13.0"
3+
version = "0.14.0"
44
authors = [
55
"The async-rs developers",
66
"Florian Gilcher <[email protected]>",
@@ -23,11 +23,11 @@ appveyor = { repository = "async-std/async-tls" }
2323
[dependencies]
2424
futures-io = "0.3.5"
2525
futures-core = "0.3.5"
26-
rustls = "0.21"
27-
rustls-pemfile = "1.0"
26+
rustls = "0.23"
27+
rustls-pemfile = "2.2"
2828
# webpki = { version = "0.22.0", optional = true }
29-
rustls-webpki = { version = "0.101.4", optional = true }
30-
webpki-roots = { version = "0.22.3", optional = true }
29+
rustls-webpki = { version = "0.102", optional = true }
30+
webpki-roots = { version = "0.26", optional = true }
3131

3232
[features]
3333
default = ["client", "server"]
@@ -36,7 +36,6 @@ early-data = []
3636
server = []
3737

3838
[dev-dependencies]
39-
lazy_static = "1"
4039
futures-executor = "0.3.5"
4140
futures-util = { version = "0.3.5", features = ["io"] }
4241
async-std = { version = "1.11", features = ["unstable"] }

Diff for: src/common/tls_state.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,10 @@ impl TlsState {
2424
}
2525

2626
pub(crate) fn writeable(&self) -> bool {
27-
match *self {
28-
TlsState::WriteShutdown | TlsState::FullyShutdown => false,
29-
_ => true,
30-
}
27+
!matches!(*self, TlsState::WriteShutdown | TlsState::FullyShutdown)
3128
}
3229

3330
pub(crate) fn readable(self) -> bool {
34-
match self {
35-
TlsState::ReadShutdown | TlsState::FullyShutdown => false,
36-
_ => true,
37-
}
31+
!matches!(self, TlsState::ReadShutdown | TlsState::FullyShutdown)
3832
}
3933
}

Diff for: src/connector.rs

+8-13
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use crate::common::tls_state::TlsState;
33
use crate::client;
44

55
use futures_io::{AsyncRead, AsyncWrite};
6-
use rustls::{ClientConfig, ClientConnection, OwnedTrustAnchor, RootCertStore, ServerName};
6+
use rustls::pki_types::ServerName;
7+
use rustls::{ClientConfig, ClientConnection, RootCertStore};
78
use std::convert::TryFrom;
89
use std::future::Future;
910
use std::io;
@@ -64,16 +65,10 @@ impl From<ClientConfig> for TlsConnector {
6465

6566
impl Default for TlsConnector {
6667
fn default() -> Self {
67-
let mut root_certs = RootCertStore::empty();
68-
root_certs.add_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.0.iter().map(|ta| {
69-
OwnedTrustAnchor::from_subject_spki_name_constraints(
70-
ta.subject,
71-
ta.spki,
72-
ta.name_constraints,
73-
)
74-
}));
68+
let root_certs = RootCertStore {
69+
roots: webpki_roots::TLS_SERVER_ROOTS.to_vec(),
70+
};
7571
let config = ClientConfig::builder()
76-
.with_safe_defaults()
7772
.with_root_certificates(root_certs)
7873
.with_no_client_auth();
7974
Arc::new(config).into()
@@ -103,7 +98,7 @@ impl TlsConnector {
10398
/// The function will return a `Connect` Future, representing the connecting part of a Tls
10499
/// handshake. It will resolve when the handshake is over.
105100
#[inline]
106-
pub fn connect<'a, IO>(&self, domain: impl AsRef<str>, stream: IO) -> Connect<IO>
101+
pub fn connect<IO>(&self, domain: impl AsRef<str>, stream: IO) -> Connect<IO>
107102
where
108103
IO: AsyncRead + AsyncWrite + Unpin,
109104
{
@@ -112,12 +107,12 @@ impl TlsConnector {
112107

113108
// NOTE: Currently private, exposing ClientConnection exposes rusttls
114109
// Early data should be exposed differently
115-
fn connect_with<'a, IO, F>(&self, domain: impl AsRef<str>, stream: IO, f: F) -> Connect<IO>
110+
fn connect_with<IO, F>(&self, domain: impl AsRef<str>, stream: IO, f: F) -> Connect<IO>
116111
where
117112
IO: AsyncRead + AsyncWrite + Unpin,
118113
F: FnOnce(&mut ClientConnection),
119114
{
120-
let domain = match ServerName::try_from(domain.as_ref()) {
115+
let domain = match ServerName::try_from(domain.as_ref().to_owned()) {
121116
Ok(domain) => domain,
122117
Err(_) => {
123118
return Connect(ConnectInner::Error(Some(io::Error::new(

Diff for: src/rusttls/stream.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ impl<'a, IO: AsyncRead + AsyncWrite + Unpin> Stream<'a, IO> {
153153
cx: &'a mut Context<'b>,
154154
}
155155

156-
impl<'a, 'b, T: AsyncRead + Unpin> Read for Reader<'a, 'b, T> {
156+
impl<T: AsyncRead + Unpin> Read for Reader<'_, '_, T> {
157157
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
158158
match Pin::new(&mut self.io).poll_read(self.cx, buf) {
159159
Poll::Ready(result) => result,
@@ -253,7 +253,7 @@ impl<'a, IO: AsyncRead + AsyncWrite + Unpin> Stream<'a, IO> {
253253
}
254254
}
255255

256-
impl<'a, IO: AsyncRead + AsyncWrite + Unpin> WriteTls<IO> for Stream<'a, IO> {
256+
impl<IO: AsyncRead + AsyncWrite + Unpin> WriteTls<IO> for Stream<'_, IO> {
257257
fn write_tls(&mut self, cx: &mut Context) -> io::Result<usize> {
258258
// TODO writev
259259

@@ -262,7 +262,7 @@ impl<'a, IO: AsyncRead + AsyncWrite + Unpin> WriteTls<IO> for Stream<'a, IO> {
262262
cx: &'a mut Context<'b>,
263263
}
264264

265-
impl<'a, 'b, T: AsyncWrite + Unpin> Write for Writer<'a, 'b, T> {
265+
impl<T: AsyncWrite + Unpin> Write for Writer<'_, '_, T> {
266266
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
267267
match Pin::new(&mut self.io).poll_write(self.cx, buf) {
268268
Poll::Ready(result) => result,
@@ -283,7 +283,7 @@ impl<'a, IO: AsyncRead + AsyncWrite + Unpin> WriteTls<IO> for Stream<'a, IO> {
283283
}
284284
}
285285

286-
impl<'a, IO: AsyncRead + AsyncWrite + Unpin> AsyncRead for Stream<'a, IO> {
286+
impl<IO: AsyncRead + AsyncWrite + Unpin> AsyncRead for Stream<'_, IO> {
287287
fn poll_read(
288288
self: Pin<&mut Self>,
289289
cx: &mut Context,
@@ -312,7 +312,7 @@ impl<'a, IO: AsyncRead + AsyncWrite + Unpin> AsyncRead for Stream<'a, IO> {
312312
}
313313
}
314314

315-
impl<'a, IO: AsyncRead + AsyncWrite + Unpin> AsyncWrite for Stream<'a, IO> {
315+
impl<IO: AsyncRead + AsyncWrite + Unpin> AsyncWrite for Stream<'_, IO> {
316316
fn poll_write(self: Pin<&mut Self>, cx: &mut Context, buf: &[u8]) -> Poll<io::Result<usize>> {
317317
let this = self.get_mut();
318318

Diff for: src/rusttls/test_stream.rs

+16-12
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ use futures_io::{AsyncRead, AsyncWrite};
44
use futures_util::io::{AsyncReadExt, AsyncWriteExt};
55
use futures_util::task::{noop_waker_ref, Context};
66
use futures_util::{future, ready};
7+
use rustls::pki_types::{PrivateKeyDer, ServerName};
78
use rustls::{
8-
Certificate, ClientConfig, ClientConnection, ConnectionCommon, PrivateKey, RootCertStore,
9-
ServerConfig, ServerConnection, ServerName,
9+
ClientConfig, ClientConnection, ConnectionCommon, RootCertStore,
10+
ServerConfig, ServerConnection,
1011
};
1112
use rustls_pemfile::{certs, pkcs8_private_keys};
1213
use std::convert::TryFrom;
@@ -17,7 +18,7 @@ use std::task::Poll;
1718

1819
struct Good<'a, D>(&'a mut ConnectionCommon<D>);
1920

20-
impl<'a, D> AsyncRead for Good<'a, D> {
21+
impl<D> AsyncRead for Good<'_, D> {
2122
fn poll_read(
2223
mut self: Pin<&mut Self>,
2324
_cx: &mut Context<'_>,
@@ -27,7 +28,7 @@ impl<'a, D> AsyncRead for Good<'a, D> {
2728
}
2829
}
2930

30-
impl<'a, D> AsyncWrite for Good<'a, D> {
31+
impl<D> AsyncWrite for Good<'_, D> {
3132
fn poll_write(
3233
mut self: Pin<&mut Self>,
3334
_cx: &mut Context<'_>,
@@ -223,24 +224,27 @@ fn make_pair() -> (ServerConnection, ClientConnection) {
223224
const CHAIN: &str = include_str!("../../tests/end.chain");
224225
const RSA: &str = include_str!("../../tests/end.rsa");
225226

226-
let cert = certs(&mut BufReader::new(Cursor::new(CERT))).unwrap();
227-
let cert = cert.into_iter().map(Certificate).collect();
228-
let mut keys = pkcs8_private_keys(&mut BufReader::new(Cursor::new(RSA))).unwrap();
229-
let key = PrivateKey(keys.pop().unwrap());
227+
let cert = certs(&mut BufReader::new(Cursor::new(CERT)))
228+
.collect::<Result<Vec<_>,_>>()
229+
.unwrap();
230+
let mut keys = pkcs8_private_keys(&mut BufReader::new(Cursor::new(RSA)))
231+
.collect::<Result<Vec<_>,_>>()
232+
.unwrap();
233+
let key = PrivateKeyDer::Pkcs8(keys.pop().unwrap());
230234
let sconfig = ServerConfig::builder()
231-
.with_safe_defaults()
232235
.with_no_client_auth()
233236
.with_single_cert(cert, key)
234237
.unwrap();
235238
let server = ServerConnection::new(Arc::new(sconfig));
236239

237240
let domain = ServerName::try_from("localhost").unwrap();
238241
let mut root_store = RootCertStore::empty();
239-
let chain = certs(&mut BufReader::new(Cursor::new(CHAIN))).unwrap();
240-
let (added, ignored) = root_store.add_parsable_certificates(&chain);
242+
let chain = certs(&mut BufReader::new(Cursor::new(CHAIN)))
243+
.collect::<Result<Vec<_>,_>>()
244+
.unwrap();
245+
let (added, ignored) = root_store.add_parsable_certificates(chain);
241246
assert!(added >= 1 && ignored == 0);
242247
let cconfig = ClientConfig::builder()
243-
.with_safe_defaults()
244248
.with_root_certificates(root_store)
245249
.with_no_client_auth();
246250
let client = ClientConnection::new(Arc::new(cconfig), domain);

Diff for: tests/test.rs

+52-50
Original file line numberDiff line numberDiff line change
@@ -4,61 +4,65 @@ use async_std::net::{TcpListener, TcpStream};
44
use async_std::prelude::*;
55
use async_std::task;
66
use async_tls::{TlsAcceptor, TlsConnector};
7-
use lazy_static::lazy_static;
8-
use rustls::{Certificate, ClientConfig, PrivateKey, RootCertStore, ServerConfig};
7+
use rustls::pki_types::{CertificateDer, PrivateKeyDer};
8+
use rustls::{ClientConfig, RootCertStore, ServerConfig};
99
use rustls_pemfile::{certs, pkcs8_private_keys};
1010
use std::io::{BufReader, Cursor};
1111
use std::net::SocketAddr;
1212
use std::sync::Arc;
13+
use std::sync::LazyLock;
1314

1415
const CERT: &str = include_str!("end.cert");
1516
const CHAIN: &str = include_str!("end.chain");
1617
const RSA: &str = include_str!("end.rsa");
1718

18-
lazy_static! {
19-
static ref TEST_SERVER: (SocketAddr, &'static str, Vec<Vec<u8>>) = {
20-
let cert = certs(&mut BufReader::new(Cursor::new(CERT))).unwrap();
21-
let cert = cert.into_iter().map(Certificate).collect();
22-
let chain = certs(&mut BufReader::new(Cursor::new(CHAIN))).unwrap();
23-
let mut keys = pkcs8_private_keys(&mut BufReader::new(Cursor::new(RSA))).unwrap();
24-
let key = PrivateKey(keys.pop().unwrap());
25-
let sconfig = ServerConfig::builder()
26-
.with_safe_defaults()
27-
.with_no_client_auth()
28-
.with_single_cert(cert, key)
29-
.unwrap();
30-
let acceptor = TlsAcceptor::from(Arc::new(sconfig));
31-
32-
let (send, recv) = bounded(1);
33-
34-
task::spawn(async move {
35-
let addr = SocketAddr::from(([127, 0, 0, 1], 0));
36-
let listener = TcpListener::bind(&addr).await?;
37-
38-
send.send(listener.local_addr()?).await.unwrap();
39-
40-
let mut incoming = listener.incoming();
41-
while let Some(stream) = incoming.next().await {
42-
let acceptor = acceptor.clone();
43-
task::spawn(async move {
44-
use futures_util::io::AsyncReadExt;
45-
let stream = acceptor.accept(stream?).await?;
46-
let (mut reader, mut writer) = stream.split();
47-
io::copy(&mut reader, &mut writer).await?;
48-
Ok(()) as io::Result<()>
49-
});
50-
}
51-
52-
Ok(()) as io::Result<()>
53-
});
54-
55-
let addr = task::block_on(async move { recv.recv().await.unwrap() });
56-
(addr, "localhost", chain)
57-
};
58-
}
59-
60-
fn start_server() -> &'static (SocketAddr, &'static str, Vec<Vec<u8>>) {
61-
&*TEST_SERVER
19+
static TEST_SERVER: LazyLock<(SocketAddr, &'static str, Vec<CertificateDer<'_>>)> = LazyLock::new(|| {
20+
let cert = certs(&mut BufReader::new(Cursor::new(CERT)))
21+
.collect::<Result<Vec<_>,_>>()
22+
.unwrap();
23+
let chain = certs(&mut BufReader::new(Cursor::new(CHAIN)))
24+
.collect::<Result<Vec<_>,_>>()
25+
.unwrap();
26+
let mut keys = pkcs8_private_keys(&mut BufReader::new(Cursor::new(RSA)))
27+
.map(|res| res.map(PrivateKeyDer::Pkcs8))
28+
.collect::<Result<Vec<_>,_>>()
29+
.unwrap();
30+
let key = keys.pop().unwrap();
31+
let sconfig = ServerConfig::builder()
32+
.with_no_client_auth()
33+
.with_single_cert(cert, key)
34+
.unwrap();
35+
let acceptor = TlsAcceptor::from(Arc::new(sconfig));
36+
37+
let (send, recv) = bounded(1);
38+
39+
task::spawn(async move {
40+
let addr = SocketAddr::from(([127, 0, 0, 1], 0));
41+
let listener = TcpListener::bind(&addr).await?;
42+
43+
send.send(listener.local_addr()?).await.unwrap();
44+
45+
let mut incoming = listener.incoming();
46+
while let Some(stream) = incoming.next().await {
47+
let acceptor = acceptor.clone();
48+
task::spawn(async move {
49+
use futures_util::io::AsyncReadExt;
50+
let stream = acceptor.accept(stream?).await?;
51+
let (mut reader, mut writer) = stream.split();
52+
io::copy(&mut reader, &mut writer).await?;
53+
Ok(()) as io::Result<()>
54+
});
55+
}
56+
57+
Ok(()) as io::Result<()>
58+
});
59+
60+
let addr = task::block_on(async { recv.recv().await.unwrap() });
61+
(addr, "localhost", chain)
62+
});
63+
64+
fn start_server() -> &'static (SocketAddr, &'static str, Vec<CertificateDer<'static>>) {
65+
&TEST_SERVER
6266
}
6367

6468
async fn start_client(addr: SocketAddr, domain: &str, config: Arc<ClientConfig>) -> io::Result<()> {
@@ -82,10 +86,9 @@ async fn start_client(addr: SocketAddr, domain: &str, config: Arc<ClientConfig>)
8286
fn pass() {
8387
let (addr, domain, chain) = start_server();
8488
let mut root_store = RootCertStore::empty();
85-
let (added, ignored) = root_store.add_parsable_certificates(&chain);
89+
let (added, ignored) = root_store.add_parsable_certificates(chain.clone());
8690
assert!(added >= 1 && ignored == 0);
8791
let config = ClientConfig::builder()
88-
.with_safe_defaults()
8992
.with_root_certificates(root_store)
9093
.with_no_client_auth();
9194
task::block_on(start_client(*addr, domain, Arc::new(config))).unwrap();
@@ -95,10 +98,9 @@ fn pass() {
9598
fn fail() {
9699
let (addr, domain, chain) = start_server();
97100
let mut root_store = RootCertStore::empty();
98-
let (added, ignored) = root_store.add_parsable_certificates(&chain);
101+
let (added, ignored) = root_store.add_parsable_certificates(chain.clone());
99102
assert!(added >= 1 && ignored == 0);
100103
let config = ClientConfig::builder()
101-
.with_safe_defaults()
102104
.with_root_certificates(root_store)
103105
.with_no_client_auth();
104106
let config = Arc::new(config);

0 commit comments

Comments
 (0)