Skip to content

Commit

Permalink
chore: replace rustls-pemfile with rustls-pki-types
Browse files Browse the repository at this point in the history
  • Loading branch information
tottoto committed Feb 7, 2025
1 parent 3707436 commit cd9f3e9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ http3 = ["rustls-tls-manual-roots", "dep:h3", "dep:h3-quinn", "dep:quinn", "dep:
# Don't rely on these whatsoever. They may disappear at any time.

# Enables common types used for TLS. Useless on its own.
__tls = ["dep:rustls-pemfile", "tokio/io-util"]
__tls = ["dep:rustls-pki-types", "tokio/io-util"]

# Enables common rustls code.
# Equivalent to rustls-tls-manual-roots but shorter :)
__rustls = ["dep:hyper-rustls", "dep:tokio-rustls", "dep:rustls", "__tls", "dep:rustls-pemfile", "dep:rustls-pki-types"]
__rustls = ["dep:hyper-rustls", "dep:tokio-rustls", "dep:rustls", "__tls"]
__rustls-ring = ["hyper-rustls?/ring", "tokio-rustls?/ring", "rustls?/ring", "quinn?/ring"]

[dependencies]
Expand Down Expand Up @@ -131,7 +131,6 @@ pin-project-lite = "0.2.11"
ipnet = "2.3"

# Optional deps...
rustls-pemfile = { version = "2", optional = true }

## default-tls
hyper-tls = { version = "0.6", optional = true }
Expand All @@ -141,7 +140,7 @@ tokio-native-tls = { version = "0.3.0", optional = true }
# rustls-tls
hyper-rustls = { version = "0.27.0", default-features = false, optional = true, features = ["http1", "tls12"] }
rustls = { version = "0.23.4", optional = true, default-features = false, features = ["std", "tls12"] }
rustls-pki-types = { version = "1.1.0", features = ["alloc"] ,optional = true }
rustls-pki-types = { version = "1.1.0", features = ["std"] ,optional = true }
tokio-rustls = { version = "0.26", optional = true, default-features = false, features = ["tls12"] }
webpki-roots = { version = "0.26.0", optional = true }
rustls-native-certs = { version = "0.8.0", optional = true }
Expand Down
36 changes: 19 additions & 17 deletions src/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ use rustls::{
server::ParsedCertificate, DigitallySignedStruct, Error as TLSError, RootCertStore,
SignatureScheme,
};
use rustls_pki_types::pem::PemObject;
#[cfg(feature = "__rustls")]
use rustls_pki_types::{ServerName, UnixTime};
use std::{
Expand Down Expand Up @@ -228,7 +229,7 @@ impl Certificate {
}

fn read_pem_certs(reader: &mut impl BufRead) -> crate::Result<Vec<Vec<u8>>> {
rustls_pemfile::certs(reader)
rustls_pki_types::CertificateDer::pem_reader_iter(reader)
.map(|result| match result {
Ok(cert) => Ok(cert.as_ref().to_vec()),
Err(_) => Err(crate::error::builder("invalid certificate encoding")),
Expand Down Expand Up @@ -339,28 +340,31 @@ impl Identity {
/// This requires the `rustls-tls(-...)` Cargo feature enabled.
#[cfg(feature = "__rustls")]
pub fn from_pem(buf: &[u8]) -> crate::Result<Identity> {
use rustls_pemfile::Item;
use rustls_pki_types::pem::SectionKind;
use std::io::Cursor;

let (key, certs) = {
let mut pem = Cursor::new(buf);
let mut sk = Vec::<rustls_pki_types::PrivateKeyDer>::new();
let mut certs = Vec::<rustls_pki_types::CertificateDer>::new();

for result in rustls_pemfile::read_all(&mut pem) {
match result {
Ok(Item::X509Certificate(cert)) => certs.push(cert),
Ok(Item::Pkcs1Key(key)) => sk.push(key.into()),
Ok(Item::Pkcs8Key(key)) => sk.push(key.into()),
Ok(Item::Sec1Key(key)) => sk.push(key.into()),
Ok(_) => {
return Err(crate::error::builder(TLSError::General(String::from(
"No valid certificate was found",
))))
while let Some((kind, pem)) =
rustls_pki_types::pem::from_buf(&mut pem).map_err(|_| {
crate::error::builder(TLSError::General(String::from(
"Invalid identity PEM file",
)))
})?
{
match kind {
SectionKind::Certificate => certs.push(pem.into()),
SectionKind::PrivateKey
| SectionKind::RsaPrivateKey
| SectionKind::EcPrivateKey => {
sk.push(rustls_pki_types::PrivateKeyDer::from_pem(kind, pem).unwrap())
}
Err(_) => {
_ => {
return Err(crate::error::builder(TLSError::General(String::from(
"Invalid identity PEM file",
"No valid certificate was found",
))))
}
}
Expand Down Expand Up @@ -469,9 +473,7 @@ impl CertificateRevocationList {
/// This requires the `rustls-tls(-...)` Cargo feature enabled.
#[cfg(feature = "__rustls")]
pub fn from_pem_bundle(pem_bundle: &[u8]) -> crate::Result<Vec<CertificateRevocationList>> {
let mut reader = BufReader::new(pem_bundle);

rustls_pemfile::crls(&mut reader)
rustls_pki_types::CertificateRevocationListDer::pem_slice_iter(pem_bundle)
.map(|result| match result {
Ok(crl) => Ok(CertificateRevocationList { inner: crl }),
Err(_) => Err(crate::error::builder("invalid crl encoding")),
Expand Down

0 comments on commit cd9f3e9

Please sign in to comment.