Skip to content

Commit 63b8d6f

Browse files
committed
Update to rustls 0.22 alpha
1 parent a70ea6c commit 63b8d6f

File tree

8 files changed

+72
-117
lines changed

8 files changed

+72
-117
lines changed

Cargo.toml

Lines changed: 4 additions & 4 deletions
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.21.6", default-features = false }
17+
rustls = { version = "=0.22.0-alpha.2", default-features = false }
1818

1919
[features]
2020
default = ["logging", "tls12"]
@@ -29,6 +29,6 @@ argh = "0.1"
2929
tokio = { version = "1.0", features = ["full"] }
3030
futures-util = "0.3.1"
3131
lazy_static = "1"
32-
webpki-roots = "0.25"
33-
rustls-pemfile = "1"
34-
webpki = { package = "rustls-webpki", version = "0.101.2", features = ["alloc", "std"] }
32+
webpki-roots = "=0.26.0-alpha.1"
33+
rustls-pemfile = "=2.0.0-alpha.1"
34+
webpki = { package = "rustls-webpki", version = "=0.102.0-alpha.2", features = ["alloc", "std"] }

examples/client.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use std::sync::Arc;
88
use argh::FromArgs;
99
use tokio::io::{copy, split, stdin as tokio_stdin, stdout as tokio_stdout, AsyncWriteExt};
1010
use tokio::net::TcpStream;
11-
use tokio_rustls::rustls::{self, OwnedTrustAnchor};
1211
use tokio_rustls::TlsConnector;
1312

1413
/// Tokio Rustls client example
@@ -45,24 +44,11 @@ async fn main() -> io::Result<()> {
4544
let mut root_cert_store = rustls::RootCertStore::empty();
4645
if let Some(cafile) = &options.cafile {
4746
let mut pem = BufReader::new(File::open(cafile)?);
48-
let certs = rustls_pemfile::certs(&mut pem)?;
49-
let trust_anchors = certs.iter().map(|cert| {
50-
let ta = webpki::TrustAnchor::try_from_cert_der(&cert[..]).unwrap();
51-
OwnedTrustAnchor::from_subject_spki_name_constraints(
52-
ta.subject,
53-
ta.spki,
54-
ta.name_constraints,
55-
)
56-
});
57-
root_cert_store.add_trust_anchors(trust_anchors);
47+
for cert in rustls_pemfile::certs(&mut pem) {
48+
root_cert_store.add(cert?).unwrap();
49+
}
5850
} else {
59-
root_cert_store.add_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.iter().map(|ta| {
60-
OwnedTrustAnchor::from_subject_spki_name_constraints(
61-
ta.subject,
62-
ta.spki,
63-
ta.name_constraints,
64-
)
65-
}));
51+
root_cert_store.add_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
6652
}
6753

6854
let config = rustls::ClientConfig::builder()

examples/server.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use argh::FromArgs;
88
use rustls_pemfile::{certs, rsa_private_keys};
99
use tokio::io::{copy, sink, split, AsyncWriteExt};
1010
use tokio::net::TcpListener;
11-
use tokio_rustls::rustls::{self, Certificate, PrivateKey};
1211
use tokio_rustls::TlsAcceptor;
12+
use webpki::types::{CertificateDer, PrivateKeyDer};
1313

1414
/// Tokio Rustls server example
1515
#[derive(FromArgs)]
@@ -31,16 +31,15 @@ struct Options {
3131
echo_mode: bool,
3232
}
3333

34-
fn load_certs(path: &Path) -> io::Result<Vec<Certificate>> {
35-
certs(&mut BufReader::new(File::open(path)?))
36-
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "invalid cert"))
37-
.map(|mut certs| certs.drain(..).map(Certificate).collect())
34+
fn load_certs(path: &Path) -> io::Result<Vec<CertificateDer<'static>>> {
35+
certs(&mut BufReader::new(File::open(path)?)).collect()
3836
}
3937

40-
fn load_keys(path: &Path) -> io::Result<Vec<PrivateKey>> {
38+
fn load_keys(path: &Path) -> io::Result<PrivateKeyDer<'static>> {
4139
rsa_private_keys(&mut BufReader::new(File::open(path)?))
42-
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "invalid key"))
43-
.map(|mut keys| keys.drain(..).map(PrivateKey).collect())
40+
.next()
41+
.unwrap()
42+
.map(Into::into)
4443
}
4544

4645
#[tokio::main]
@@ -53,13 +52,13 @@ async fn main() -> io::Result<()> {
5352
.next()
5453
.ok_or_else(|| io::Error::from(io::ErrorKind::AddrNotAvailable))?;
5554
let certs = load_certs(&options.cert)?;
56-
let mut keys = load_keys(&options.key)?;
55+
let key = load_keys(&options.key)?;
5756
let flag_echo = options.echo_mode;
5857

5958
let config = rustls::ServerConfig::builder()
6059
.with_safe_defaults()
6160
.with_no_client_auth()
62-
.with_single_cert(certs, keys.remove(0))
61+
.with_single_cert(certs, key)
6362
.map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?;
6463
let acceptor = TlsAcceptor::from(Arc::new(config));
6564

src/lib.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ use std::sync::Arc;
4747
use std::task::{Context, Poll};
4848

4949
pub use rustls;
50+
use rustls::crypto::ring::Ring;
5051
use rustls::{ClientConfig, ClientConnection, CommonState, ServerConfig, ServerConnection};
5152
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
5253

@@ -67,19 +68,19 @@ pub mod server;
6768
/// A wrapper around a `rustls::ClientConfig`, providing an async `connect` method.
6869
#[derive(Clone)]
6970
pub struct TlsConnector {
70-
inner: Arc<ClientConfig>,
71+
inner: Arc<ClientConfig<Ring>>,
7172
#[cfg(feature = "early-data")]
7273
early_data: bool,
7374
}
7475

7576
/// A wrapper around a `rustls::ServerConfig`, providing an async `accept` method.
7677
#[derive(Clone)]
7778
pub struct TlsAcceptor {
78-
inner: Arc<ServerConfig>,
79+
inner: Arc<ServerConfig<Ring>>,
7980
}
8081

81-
impl From<Arc<ClientConfig>> for TlsConnector {
82-
fn from(inner: Arc<ClientConfig>) -> TlsConnector {
82+
impl From<Arc<ClientConfig<Ring>>> for TlsConnector {
83+
fn from(inner: Arc<ClientConfig<Ring>>) -> TlsConnector {
8384
TlsConnector {
8485
inner,
8586
#[cfg(feature = "early-data")]
@@ -88,8 +89,8 @@ impl From<Arc<ClientConfig>> for TlsConnector {
8889
}
8990
}
9091

91-
impl From<Arc<ServerConfig>> for TlsAcceptor {
92-
fn from(inner: Arc<ServerConfig>) -> TlsAcceptor {
92+
impl From<Arc<ServerConfig<Ring>>> for TlsAcceptor {
93+
fn from(inner: Arc<ServerConfig<Ring>>) -> TlsAcceptor {
9394
TlsAcceptor { inner }
9495
}
9596
}
@@ -210,9 +211,10 @@ where
210211
/// # Example
211212
///
212213
/// ```no_run
214+
/// # use rustls::crypto::ring::Ring;
213215
/// # fn choose_server_config(
214216
/// # _: rustls::server::ClientHello,
215-
/// # ) -> std::sync::Arc<rustls::ServerConfig> {
217+
/// # ) -> std::sync::Arc<rustls::ServerConfig<Ring>> {
216218
/// # unimplemented!();
217219
/// # }
218220
/// # #[allow(unused_variables)]
@@ -304,11 +306,11 @@ where
304306
self.accepted.client_hello()
305307
}
306308

307-
pub fn into_stream(self, config: Arc<ServerConfig>) -> Accept<IO> {
309+
pub fn into_stream(self, config: Arc<ServerConfig<Ring>>) -> Accept<IO> {
308310
self.into_stream_with(config, |_| ())
309311
}
310312

311-
pub fn into_stream_with<F>(self, config: Arc<ServerConfig>, f: F) -> Accept<IO>
313+
pub fn into_stream_with<F>(self, config: Arc<ServerConfig<Ring>>, f: F) -> Accept<IO>
312314
where
313315
F: FnOnce(&mut ServerConnection),
314316
{

tests/badssl.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ use std::io;
22
use std::net::ToSocketAddrs;
33
use std::sync::Arc;
44

5+
use rustls::crypto::ring::Ring;
56
use tokio::io::{AsyncReadExt, AsyncWriteExt};
67
use tokio::net::TcpStream;
78
use tokio_rustls::{
89
client::TlsStream,
9-
rustls::{self, ClientConfig, OwnedTrustAnchor},
10+
rustls::{self, ClientConfig},
1011
TlsConnector,
1112
};
1213

1314
async fn get(
14-
config: Arc<ClientConfig>,
15+
config: Arc<ClientConfig<Ring>>,
1516
domain: &str,
1617
port: u16,
1718
) -> io::Result<(TlsStream<TcpStream>, String)> {
@@ -34,13 +35,7 @@ async fn get(
3435
#[tokio::test]
3536
async fn test_tls12() -> io::Result<()> {
3637
let mut root_store = rustls::RootCertStore::empty();
37-
root_store.add_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.iter().map(|ta| {
38-
OwnedTrustAnchor::from_subject_spki_name_constraints(
39-
ta.subject,
40-
ta.spki,
41-
ta.name_constraints,
42-
)
43-
}));
38+
root_store.add_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
4439
let config = rustls::ClientConfig::builder()
4540
.with_safe_default_cipher_suites()
4641
.with_safe_default_kx_groups()
@@ -72,13 +67,7 @@ fn test_tls13() {
7267
#[tokio::test]
7368
async fn test_modern() -> io::Result<()> {
7469
let mut root_store = rustls::RootCertStore::empty();
75-
root_store.add_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.iter().map(|ta| {
76-
OwnedTrustAnchor::from_subject_spki_name_constraints(
77-
ta.subject,
78-
ta.spki,
79-
ta.name_constraints,
80-
)
81-
}));
70+
root_store.add_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
8271
let config = rustls::ClientConfig::builder()
8372
.with_safe_defaults()
8473
.with_root_certificates(root_store)

tests/early-data.rs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,13 @@ use std::thread;
1010
use std::time::Duration;
1111

1212
use futures_util::{future, future::Future, ready};
13-
use rustls::RootCertStore;
13+
use rustls::crypto::ring::Ring;
14+
use rustls::{self, ClientConfig, RootCertStore};
1415
use tokio::io::{split, AsyncRead, AsyncWriteExt, ReadBuf};
1516
use tokio::net::TcpStream;
1617
use tokio::sync::oneshot;
1718
use tokio::time::sleep;
18-
use tokio_rustls::{
19-
client::TlsStream,
20-
rustls::{self, ClientConfig, OwnedTrustAnchor},
21-
TlsConnector,
22-
};
19+
use tokio_rustls::{client::TlsStream, TlsConnector};
2320

2421
struct Read1<T>(T);
2522

@@ -42,7 +39,7 @@ impl<T: AsyncRead + Unpin> Future for Read1<T> {
4239
}
4340

4441
async fn send(
45-
config: Arc<ClientConfig>,
42+
config: Arc<ClientConfig<Ring>>,
4643
addr: SocketAddr,
4744
data: &[u8],
4845
) -> io::Result<TlsStream<TcpStream>> {
@@ -132,17 +129,11 @@ async fn test_0rtt() -> io::Result<()> {
132129
wait_for_server(format!("127.0.0.1:{}", server_port).as_str()).await;
133130

134131
let mut chain = BufReader::new(Cursor::new(include_str!("end.chain")));
135-
let certs = rustls_pemfile::certs(&mut chain).unwrap();
136-
let trust_anchors = certs.iter().map(|cert| {
137-
let ta = webpki::TrustAnchor::try_from_cert_der(&cert[..]).unwrap();
138-
OwnedTrustAnchor::from_subject_spki_name_constraints(
139-
ta.subject,
140-
ta.spki,
141-
ta.name_constraints,
142-
)
143-
});
144132
let mut root_store = RootCertStore::empty();
145-
root_store.add_trust_anchors(trust_anchors);
133+
for cert in rustls_pemfile::certs(&mut chain) {
134+
root_store.add(cert.unwrap()).unwrap();
135+
}
136+
146137
let mut config = rustls::ClientConfig::builder()
147138
.with_safe_default_cipher_suites()
148139
.with_safe_default_kx_groups()

tests/test.rs

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use std::{io, thread};
77

88
use futures_util::future::TryFutureExt;
99
use lazy_static::lazy_static;
10-
use rustls::{ClientConfig, OwnedTrustAnchor};
10+
use rustls::crypto::ring::Ring;
11+
use rustls::ClientConfig;
1112
use rustls_pemfile::{certs, rsa_private_keys};
1213
use tokio::io::{copy, split, AsyncReadExt, AsyncWriteExt};
1314
use tokio::net::{TcpListener, TcpStream};
@@ -22,17 +23,17 @@ const RSA: &str = include_str!("end.rsa");
2223
lazy_static! {
2324
static ref TEST_SERVER: (SocketAddr, &'static str, &'static [u8]) = {
2425
let cert = certs(&mut BufReader::new(Cursor::new(CERT)))
25-
.unwrap()
26-
.drain(..)
27-
.map(rustls::Certificate)
26+
.map(|result| result.unwrap())
2827
.collect();
29-
let mut keys = rsa_private_keys(&mut BufReader::new(Cursor::new(RSA))).unwrap();
30-
let mut keys = keys.drain(..).map(rustls::PrivateKey);
28+
let key = rsa_private_keys(&mut BufReader::new(Cursor::new(RSA)))
29+
.next()
30+
.unwrap()
31+
.unwrap();
3132

3233
let config = rustls::ServerConfig::builder()
3334
.with_safe_defaults()
3435
.with_no_client_auth()
35-
.with_single_cert(cert, keys.next().unwrap())
36+
.with_single_cert(cert, key.into())
3637
.unwrap();
3738
let acceptor = TlsAcceptor::from(Arc::new(config));
3839

@@ -83,7 +84,11 @@ fn start_server() -> &'static (SocketAddr, &'static str, &'static [u8]) {
8384
&TEST_SERVER
8485
}
8586

86-
async fn start_client(addr: SocketAddr, domain: &str, config: Arc<ClientConfig>) -> io::Result<()> {
87+
async fn start_client(
88+
addr: SocketAddr,
89+
domain: &str,
90+
config: Arc<ClientConfig<Ring>>,
91+
) -> io::Result<()> {
8792
const FILE: &[u8] = include_bytes!("../README.md");
8893

8994
let domain = rustls::ServerName::try_from(domain).unwrap();
@@ -111,16 +116,10 @@ async fn pass() -> io::Result<()> {
111116
use std::time::*;
112117
tokio::time::sleep(Duration::from_secs(1)).await;
113118

114-
let chain = certs(&mut std::io::Cursor::new(*chain)).unwrap();
115119
let mut root_store = rustls::RootCertStore::empty();
116-
root_store.add_trust_anchors(chain.iter().map(|cert| {
117-
let ta = webpki::TrustAnchor::try_from_cert_der(&cert[..]).unwrap();
118-
OwnedTrustAnchor::from_subject_spki_name_constraints(
119-
ta.subject,
120-
ta.spki,
121-
ta.name_constraints,
122-
)
123-
}));
120+
for cert in certs(&mut std::io::Cursor::new(*chain)) {
121+
root_store.add(cert.unwrap()).unwrap();
122+
}
124123

125124
let config = rustls::ClientConfig::builder()
126125
.with_safe_defaults()
@@ -137,16 +136,10 @@ async fn pass() -> io::Result<()> {
137136
async fn fail() -> io::Result<()> {
138137
let (addr, domain, chain) = start_server();
139138

140-
let chain = certs(&mut std::io::Cursor::new(*chain)).unwrap();
141139
let mut root_store = rustls::RootCertStore::empty();
142-
root_store.add_trust_anchors(chain.iter().map(|cert| {
143-
let ta = webpki::TrustAnchor::try_from_cert_der(&cert[..]).unwrap();
144-
OwnedTrustAnchor::from_subject_spki_name_constraints(
145-
ta.subject,
146-
ta.spki,
147-
ta.name_constraints,
148-
)
149-
}));
140+
for cert in certs(&mut std::io::Cursor::new(*chain)) {
141+
root_store.add(cert.unwrap()).unwrap();
142+
}
150143

151144
let config = rustls::ClientConfig::builder()
152145
.with_safe_defaults()

0 commit comments

Comments
 (0)