Skip to content

Commit

Permalink
Update tokio-rustls to v0.23 (#927)
Browse files Browse the repository at this point in the history
This change bumps the tokio-rustls version to pick up the new version of
rustls.

This helps to avoid duplicate dependencies when using other libraries
that depend on rustls.
  • Loading branch information
olix0r authored Dec 29, 2021
1 parent 969d10a commit 3ff2eaf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ tower-service = "0.3"
tokio-tungstenite = { version = "0.15", optional = true }
percent-encoding = "2.1"
pin-project = "1.0"
tokio-rustls = { version = "0.22", optional = true }
tokio-rustls = { version = "0.23", optional = true }
rustls-pemfile = "0.2"

[dev-dependencies]
pretty_env_logger = "0.4"
Expand Down
55 changes: 30 additions & 25 deletions src/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use hyper::server::conn::{AddrIncoming, AddrStream};

use crate::transport::Transport;
use tokio_rustls::rustls::{
AllowAnyAnonymousOrAuthenticatedClient, AllowAnyAuthenticatedClient, NoClientAuth,
RootCertStore, ServerConfig, TLSError,
server::{AllowAnyAnonymousOrAuthenticatedClient, AllowAnyAuthenticatedClient, NoClientAuth},
Certificate, Error as TlsError, PrivateKey, RootCertStore, ServerConfig,
};

/// Represents errors that can occur building the TlsConfig
Expand All @@ -32,7 +32,7 @@ pub(crate) enum TlsConfigError {
/// An error from an empty key
EmptyKey,
/// An error from an invalid key
InvalidKey(TLSError),
InvalidKey(TlsError),
}

impl fmt::Display for TlsConfigError {
Expand Down Expand Up @@ -169,8 +169,11 @@ impl TlsConfigBuilder {

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

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

let mut pkcs8 = tokio_rustls::rustls::internal::pemfile::pkcs8_private_keys(
&mut key_vec.as_slice(),
)
.map_err(|()| TlsConfigError::Pkcs8ParseError)?;
let mut pkcs8 = rustls_pemfile::pkcs8_private_keys(&mut key_vec.as_slice())
.map_err(|_e| TlsConfigError::Pkcs8ParseError)?;

if !pkcs8.is_empty() {
pkcs8.remove(0)
PrivateKey(pkcs8.remove(0))
} else {
let mut rsa = tokio_rustls::rustls::internal::pemfile::rsa_private_keys(
&mut key_vec.as_slice(),
)
.map_err(|()| TlsConfigError::RsaParseError)?;
let mut rsa = rustls_pemfile::rsa_private_keys(&mut key_vec.as_slice())
.map_err(|_e| TlsConfigError::RsaParseError)?;

if !rsa.is_empty() {
rsa.remove(0)
PrivateKey(rsa.remove(0))
} else {
return Err(TlsConfigError::EmptyKey);
}
Expand All @@ -207,13 +206,18 @@ impl TlsConfigBuilder {
fn read_trust_anchor(
trust_anchor: Box<dyn Read + Send + Sync>,
) -> Result<RootCertStore, TlsConfigError> {
let mut reader = BufReader::new(trust_anchor);
let trust_anchors = {
let mut reader = BufReader::new(trust_anchor);
rustls_pemfile::certs(&mut reader).map_err(TlsConfigError::Io)?
};

let mut store = RootCertStore::empty();
if let Ok((0, _)) | Err(()) = store.add_pem_file(&mut reader) {
Err(TlsConfigError::CertParseError)
} else {
Ok(store)
let (added, _skipped) = store.add_parsable_certificates(&trust_anchors);
if added == 0 {
return Err(TlsConfigError::CertParseError);
}

Ok(store)
}

let client_auth = match self.client_auth {
Expand All @@ -226,11 +230,12 @@ impl TlsConfigBuilder {
}
};

let mut config = ServerConfig::new(client_auth);
config
.set_single_cert_with_ocsp_and_sct(cert, key, self.ocsp_resp, Vec::new())
.map_err(|err| TlsConfigError::InvalidKey(err))?;
config.set_protocols(&["h2".into(), "http/1.1".into()]);
let mut config = ServerConfig::builder()
.with_safe_defaults()
.with_client_cert_verifier(client_auth.into())
.with_single_cert_with_ocsp_and_sct(cert, key, self.ocsp_resp, Vec::new())
.map_err(TlsConfigError::InvalidKey)?;
config.alpn_protocols = vec!["h2".into(), "http/1.1".into()];
Ok(config)
}
}
Expand Down

0 comments on commit 3ff2eaf

Please sign in to comment.