Skip to content

Commit

Permalink
Follow previous commit to (a) conclusion:
Browse files Browse the repository at this point in the history
- impl PemObject for simple types that have one associated SectionType,
  via PemObjectFilter.
- impl PemObject directly for the multiplexed PrivateKeyDer type
- make iterators yield their source types (ie, `CertificateDer::iter_pem_file`
  yields `CertificateDer`s, rather than `(SectionType::Certificate, Vec<u8>)`
  tuples which avoids boilerplate in common usage (eg, "get me the certificates
  from this file").

  This removes the ability to use those iterators directly (to do one-pass
  parsing of multiple types), so impl `PemObject` for the tuple type too.
  There is a test that relies on this to verify that we don't mess up the
  order of PEM sections.
  • Loading branch information
ctz committed Sep 24, 2024
1 parent d86166c commit 02a6ded
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 137 deletions.
7 changes: 4 additions & 3 deletions fuzz/fuzz_targets/pem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ use std::io::Cursor;

use libfuzzer_sys::fuzz_target;

use rustls_pki_types::pem;
use rustls_pki_types::pem::PemObject;
use rustls_pki_types::{CertificateDer, PrivateKeyDer};

fuzz_target!(|data: &[u8]| {
for x in pem::iter_from_reader(&mut Cursor::new(data)) {
for x in CertificateDer::pem_reader_iter(&mut Cursor::new(data)) {
match x {
Ok(_item) => (),
Err(_err) => break,
}
}

for x in pem::iter_from_slice(data) {
for x in PrivateKeyDer::pem_slice_iter(data) {
match x {
Ok(_item) => (),
Err(_err) => break,
Expand Down
102 changes: 22 additions & 80 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ extern crate alloc;

#[cfg(feature = "alloc")]
use alloc::vec::Vec;
#[cfg(feature = "alloc")]
use pem::{PemObject, SectionType};
use core::fmt;
use core::ops::Deref;
use core::time::Duration;
#[cfg(feature = "alloc")]
use pem::{PemObject, PemObjectFilter, SectionType};
#[cfg(all(
feature = "std",
not(all(target_family = "wasm", target_os = "unknown"))
Expand Down Expand Up @@ -132,26 +132,17 @@ impl<'a> PrivateKeyDer<'a> {
}

#[cfg(feature = "alloc")]
impl PrivateKeyDer<'static> {
fn from_section((sec, value): (pem::SectionType, Vec<u8>)) -> Option<Self> {
match sec {
pem::SectionType::RsaPrivateKey => Some(Self::Pkcs1(value.into())),
pem::SectionType::EcPrivateKey => Some(Self::Sec1(value.into())),
pem::SectionType::PrivateKey => Some(Self::Pkcs8(value.into())),
impl PemObject for PrivateKeyDer<'static> {
fn from_pem(r#type: SectionType, value: Vec<u8>) -> Option<Self> {
match r#type {
SectionType::RsaPrivateKey => Some(Self::Pkcs1(value.into())),
SectionType::EcPrivateKey => Some(Self::Sec1(value.into())),
SectionType::PrivateKey => Some(Self::Pkcs8(value.into())),
_ => None,
}
}
}

#[cfg(feature = "alloc")]
impl PemObject for PrivateKeyDer<'_> {
const TYPES: &'static [SectionType] = &[
SectionType::RsaPrivateKey,
SectionType::EcPrivateKey,
SectionType::PrivateKey,
];
}

impl<'a> From<PrivatePkcs1KeyDer<'a>> for PrivateKeyDer<'a> {
fn from(key: PrivatePkcs1KeyDer<'a>) -> Self {
Self::Pkcs1(key)
Expand Down Expand Up @@ -289,15 +280,8 @@ impl PrivatePkcs1KeyDer<'_> {
}

#[cfg(feature = "alloc")]
impl PrivatePkcs1KeyDer<'static> {
fn from_section((sec, value): (pem::SectionType, Vec<u8>)) -> Option<Self> {
match sec {
pem::SectionType::RsaPrivateKey => Some(value.into()),
_ => None,
}
}

provide_pem_funcs!();
impl PemObjectFilter for PrivatePkcs1KeyDer<'static> {
const TYPES: &'static [SectionType] = &[SectionType::RsaPrivateKey];
}

impl<'a> From<&'a [u8]> for PrivatePkcs1KeyDer<'a> {
Expand Down Expand Up @@ -343,15 +327,8 @@ impl PrivateSec1KeyDer<'_> {
}

#[cfg(feature = "alloc")]
impl PrivateSec1KeyDer<'static> {
fn from_section((sec, value): (pem::SectionType, Vec<u8>)) -> Option<Self> {
match sec {
pem::SectionType::EcPrivateKey => Some(value.into()),
_ => None,
}
}

provide_pem_funcs!();
impl PemObjectFilter for PrivateSec1KeyDer<'static> {
const TYPES: &'static [SectionType] = &[SectionType::EcPrivateKey];
}

impl<'a> From<&'a [u8]> for PrivateSec1KeyDer<'a> {
Expand Down Expand Up @@ -397,15 +374,8 @@ impl PrivatePkcs8KeyDer<'_> {
}

#[cfg(feature = "alloc")]
impl PrivatePkcs8KeyDer<'static> {
fn from_section((sec, value): (pem::SectionType, Vec<u8>)) -> Option<Self> {
match sec {
pem::SectionType::PrivateKey => Some(value.into()),
_ => None,
}
}

provide_pem_funcs!();
impl PemObjectFilter for PrivatePkcs8KeyDer<'static> {
const TYPES: &'static [SectionType] = &[SectionType::PrivateKey];
}

impl<'a> From<&'a [u8]> for PrivatePkcs8KeyDer<'a> {
Expand Down Expand Up @@ -474,15 +444,8 @@ impl TrustAnchor<'_> {
pub struct CertificateRevocationListDer<'a>(Der<'a>);

#[cfg(feature = "alloc")]
impl CertificateRevocationListDer<'static> {
fn from_section((sec, value): (pem::SectionType, Vec<u8>)) -> Option<Self> {
match sec {
pem::SectionType::Crl => Some(value.into()),
_ => None,
}
}

provide_pem_funcs!();
impl PemObjectFilter for CertificateRevocationListDer<'static> {
const TYPES: &'static [SectionType] = &[SectionType::Crl];
}

impl AsRef<[u8]> for CertificateRevocationListDer<'_> {
Expand Down Expand Up @@ -520,15 +483,8 @@ impl<'a> From<Vec<u8>> for CertificateRevocationListDer<'a> {
pub struct CertificateSigningRequestDer<'a>(Der<'a>);

#[cfg(feature = "alloc")]
impl CertificateSigningRequestDer<'static> {
fn from_section((sec, value): (pem::SectionType, Vec<u8>)) -> Option<Self> {
match sec {
pem::SectionType::Csr => Some(value.into()),
_ => None,
}
}

provide_pem_funcs!();
impl PemObjectFilter for CertificateSigningRequestDer<'static> {
const TYPES: &'static [SectionType] = &[SectionType::Csr];
}

impl AsRef<[u8]> for CertificateSigningRequestDer<'_> {
Expand Down Expand Up @@ -574,15 +530,8 @@ impl<'a> CertificateDer<'a> {
}

#[cfg(feature = "alloc")]
impl CertificateDer<'static> {
fn from_section((sec, value): (pem::SectionType, Vec<u8>)) -> Option<Self> {
match sec {
pem::SectionType::Certificate => Some(value.into()),
_ => None,
}
}

provide_pem_funcs!();
impl PemObjectFilter for CertificateDer<'static> {
const TYPES: &'static [SectionType] = &[SectionType::Certificate];
}

impl AsRef<[u8]> for CertificateDer<'_> {
Expand Down Expand Up @@ -629,15 +578,8 @@ pub type SubjectPublicKeyInfo<'a> = SubjectPublicKeyInfoDer<'a>;
pub struct SubjectPublicKeyInfoDer<'a>(Der<'a>);

#[cfg(feature = "alloc")]
impl SubjectPublicKeyInfoDer<'static> {
fn from_section((sec, value): (pem::SectionType, Vec<u8>)) -> Option<Self> {
match sec {
pem::SectionType::PublicKey => Some(value.into()),
_ => None,
}
}

provide_pem_funcs!();
impl PemObjectFilter for SubjectPublicKeyInfoDer<'static> {
const TYPES: &'static [SectionType] = &[SectionType::PublicKey];
}

impl AsRef<[u8]> for SubjectPublicKeyInfoDer<'_> {
Expand Down
Loading

0 comments on commit 02a6ded

Please sign in to comment.