Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions der/src/reader/pem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,18 @@ impl<'i> Reader<'static> for PemReader<'i> {
{
let resumption = self.position.split_nested(len)?;
let ret = f(self);
let finished = self.is_finished();
let decoded = self.position.current();
let remaining = self.remaining_len();

self.position.resume_nested(resumption);

if ret.is_ok() && !finished {
return Err(self
.error(ErrorKind::TrailingData { decoded, remaining })
.into());
}

ret
}

Expand All @@ -90,3 +101,36 @@ impl fmt::Debug for PemReader<'_> {
.finish_non_exhaustive()
}
}

#[cfg(test)]
#[allow(clippy::unwrap_used, reason = "tests")]
mod tests {
use super::PemReader;
use crate::{
Error, ErrorKind, Length, Reader,
pem::{self, LineEnding},
};
use hex_literal::hex;

#[test]
fn nested_trailing_data() {
let pem = pem::encode_string("TEST", LineEnding::LF, &hex!("0102")).unwrap();
let mut reader = PemReader::new(pem.as_bytes()).unwrap();

let err: Error = reader
.read_nested(2u8.into(), |reader| {
reader.read_byte()?;
Ok(())
})
.expect_err("read_nested should return Err when the callback did not consume the complete contents of the nested value");

assert_eq!(Length::ONE, err.position().unwrap());
assert_eq!(
ErrorKind::TrailingData {
decoded: 1u8.into(),
remaining: 1u8.into(),
},
err.kind()
);
}
}
35 changes: 35 additions & 0 deletions der/tests/pem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use der::{
asn1::{BitString, ObjectIdentifier},
pem::{LineEnding, PemLabel},
};
use hex_literal::hex;

/// Example SPKI document encoded as DER.
const SPKI_DER: &[u8] = include_bytes!("examples/spki.der");
Expand Down Expand Up @@ -80,6 +81,40 @@ fn from_pem_trailing_data() {
}
}

#[test]
fn from_pem_nested_trailing_data() {
// AlgorithmIdentifier with trailing data after its parameters:
// SEQUENCE { OBJECT IDENTIFIER 1.3.101.112, NULL, BIT STRING aa..aa }
let algorithm = [&hex!("302a 06032b6570 0500 032100")[..], &[0xaa; 32]].concat();

// subjectPublicKey: BIT STRING bb..bb
let subject_public_key = [&hex!("032100")[..], &[0xbb; 32]].concat();

// SubjectPublicKeyInfo without and with a subjectPublicKey after the AlgorithmIdentifier.
// Ignoring the trailing data would decode the BIT STRING inside the AlgorithmIdentifier as
// the subjectPublicKey in both cases. Without one, the AlgorithmIdentifier ends where the
// SubjectPublicKeyInfo does, so the encoding would be accepted. With one, the real
// subjectPublicKey would be reported as trailing data, at the wrong position.
let spkis = [
[&hex!("302c")[..], &algorithm].concat(),
[&hex!("304f")[..], &algorithm, &subject_public_key].concat(),
];

for der in spkis {
let pem = der::pem::encode_string(SpkiOwned::PEM_LABEL, LineEnding::LF, &der).unwrap();

let err = SpkiOwned::from_der(&der).unwrap_err();
assert_eq!(
err.kind(),
ErrorKind::TrailingData {
decoded: 11u8.into(),
remaining: 35u8.into(),
}
);
assert_eq!(SpkiOwned::from_pem(&pem).unwrap_err(), err);
}
}

#[test]
fn to_pem() {
let spki = SpkiBorrowed::from_der(SPKI_DER).unwrap();
Expand Down
Loading