Skip to content

Commit 5a2c5c8

Browse files
authored
Merge branch 'master' into master
2 parents 03796be + 3ff2eaf commit 5a2c5c8

File tree

3 files changed

+44
-43
lines changed

3 files changed

+44
-43
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ tower-service = "0.3"
4040
tokio-tungstenite = { version = "0.15", optional = true }
4141
percent-encoding = "2.1"
4242
pin-project = "1.0"
43-
tokio-rustls = { version = "0.22", optional = true }
43+
tokio-rustls = { version = "0.23", optional = true }
44+
rustls-pemfile = "0.2"
4445

4546
[dev-dependencies]
4647
pretty_env_logger = "0.4"

src/reject.rs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
//! #[derive(Debug)]
2929
//! struct InvalidParameter;
3030
//!
31-
//! impl reject::Reject for InvalidParameter {};
31+
//! impl reject::Reject for InvalidParameter {}
3232
//!
3333
//! // Custom rejection handler that maps rejections into responses.
3434
//! async fn handle_rejection(err: Rejection) -> Result<impl Reply, std::convert::Infallible> {
@@ -42,23 +42,18 @@
4242
//! }
4343
//! }
4444
//!
45-
//! #[tokio::main]
46-
//! async fn main() {
4745
//!
48-
//! // Filter on `/:id`, but reject with InvalidParameter if the `id` is `0`.
49-
//! // Recover from this rejection using a custom rejection handler.
50-
//! let route = warp::path::param()
51-
//! .and_then(|id: u32| async move {
52-
//! if id == 0 {
53-
//! Err(warp::reject::custom(InvalidParameter))
54-
//! } else {
55-
//! Ok("id is valid")
56-
//! }
57-
//! })
58-
//! .recover(handle_rejection);
59-
//!
60-
//! warp::serve(route).run(([127, 0, 0, 1], 3030)).await;
61-
//! }
46+
//! // Filter on `/:id`, but reject with InvalidParameter if the `id` is `0`.
47+
//! // Recover from this rejection using a custom rejection handler.
48+
//! let route = warp::path::param()
49+
//! .and_then(|id: u32| async move {
50+
//! if id == 0 {
51+
//! Err(warp::reject::custom(InvalidParameter))
52+
//! } else {
53+
//! Ok("id is valid")
54+
//! }
55+
//! })
56+
//! .recover(handle_rejection);
6257
//! ```
6358
6459
use std::any::Any;

src/tls.rs

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ use hyper::server::conn::{AddrIncoming, AddrStream};
1515

1616
use crate::transport::Transport;
1717
use tokio_rustls::rustls::{
18-
AllowAnyAnonymousOrAuthenticatedClient, AllowAnyAuthenticatedClient, NoClientAuth,
19-
RootCertStore, ServerConfig, TLSError,
18+
server::{AllowAnyAnonymousOrAuthenticatedClient, AllowAnyAuthenticatedClient, NoClientAuth},
19+
Certificate, Error as TlsError, PrivateKey, RootCertStore, ServerConfig,
2020
};
2121

2222
/// Represents errors that can occur building the TlsConfig
@@ -32,7 +32,7 @@ pub(crate) enum TlsConfigError {
3232
/// An error from an empty key
3333
EmptyKey,
3434
/// An error from an invalid key
35-
InvalidKey(TLSError),
35+
InvalidKey(TlsError),
3636
}
3737

3838
impl fmt::Display for TlsConfigError {
@@ -169,8 +169,11 @@ impl TlsConfigBuilder {
169169

170170
pub(crate) fn build(mut self) -> Result<ServerConfig, TlsConfigError> {
171171
let mut cert_rdr = BufReader::new(self.cert);
172-
let cert = tokio_rustls::rustls::internal::pemfile::certs(&mut cert_rdr)
173-
.map_err(|()| TlsConfigError::CertParseError)?;
172+
let cert = rustls_pemfile::certs(&mut cert_rdr)
173+
.map_err(|_e| TlsConfigError::CertParseError)?
174+
.into_iter()
175+
.map(Certificate)
176+
.collect();
174177

175178
let key = {
176179
// convert it to Vec<u8> to allow reading it again if key is RSA
@@ -183,21 +186,17 @@ impl TlsConfigBuilder {
183186
return Err(TlsConfigError::EmptyKey);
184187
}
185188

186-
let mut pkcs8 = tokio_rustls::rustls::internal::pemfile::pkcs8_private_keys(
187-
&mut key_vec.as_slice(),
188-
)
189-
.map_err(|()| TlsConfigError::Pkcs8ParseError)?;
189+
let mut pkcs8 = rustls_pemfile::pkcs8_private_keys(&mut key_vec.as_slice())
190+
.map_err(|_e| TlsConfigError::Pkcs8ParseError)?;
190191

191192
if !pkcs8.is_empty() {
192-
pkcs8.remove(0)
193+
PrivateKey(pkcs8.remove(0))
193194
} else {
194-
let mut rsa = tokio_rustls::rustls::internal::pemfile::rsa_private_keys(
195-
&mut key_vec.as_slice(),
196-
)
197-
.map_err(|()| TlsConfigError::RsaParseError)?;
195+
let mut rsa = rustls_pemfile::rsa_private_keys(&mut key_vec.as_slice())
196+
.map_err(|_e| TlsConfigError::RsaParseError)?;
198197

199198
if !rsa.is_empty() {
200-
rsa.remove(0)
199+
PrivateKey(rsa.remove(0))
201200
} else {
202201
return Err(TlsConfigError::EmptyKey);
203202
}
@@ -207,13 +206,18 @@ impl TlsConfigBuilder {
207206
fn read_trust_anchor(
208207
trust_anchor: Box<dyn Read + Send + Sync>,
209208
) -> Result<RootCertStore, TlsConfigError> {
210-
let mut reader = BufReader::new(trust_anchor);
209+
let trust_anchors = {
210+
let mut reader = BufReader::new(trust_anchor);
211+
rustls_pemfile::certs(&mut reader).map_err(TlsConfigError::Io)?
212+
};
213+
211214
let mut store = RootCertStore::empty();
212-
if let Ok((0, _)) | Err(()) = store.add_pem_file(&mut reader) {
213-
Err(TlsConfigError::CertParseError)
214-
} else {
215-
Ok(store)
215+
let (added, _skipped) = store.add_parsable_certificates(&trust_anchors);
216+
if added == 0 {
217+
return Err(TlsConfigError::CertParseError);
216218
}
219+
220+
Ok(store)
217221
}
218222

219223
let client_auth = match self.client_auth {
@@ -226,11 +230,12 @@ impl TlsConfigBuilder {
226230
}
227231
};
228232

229-
let mut config = ServerConfig::new(client_auth);
230-
config
231-
.set_single_cert_with_ocsp_and_sct(cert, key, self.ocsp_resp, Vec::new())
232-
.map_err(|err| TlsConfigError::InvalidKey(err))?;
233-
config.set_protocols(&["h2".into(), "http/1.1".into()]);
233+
let mut config = ServerConfig::builder()
234+
.with_safe_defaults()
235+
.with_client_cert_verifier(client_auth.into())
236+
.with_single_cert_with_ocsp_and_sct(cert, key, self.ocsp_resp, Vec::new())
237+
.map_err(TlsConfigError::InvalidKey)?;
238+
config.alpn_protocols = vec!["h2".into(), "http/1.1".into()];
234239
Ok(config)
235240
}
236241
}

0 commit comments

Comments
 (0)