Skip to content

Commit 3fa431b

Browse files
committed
der: pem reader rejects zero lengths reads
Since base64ct 1.7.0 ([RustCrypto#1387]), it now rejects zero lengths reads. This happens to trigger with an sha256WithRSAEncryption AlgorithmIdentifier where the parameters are null.
1 parent 3a74aaa commit 3fa431b

3 files changed

Lines changed: 23 additions & 15 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

der/src/reader/pem.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ impl<'i> Reader<'i> for PemReader<'i> {
8787
}
8888

8989
fn read_into<'o>(&mut self, buf: &'o mut [u8]) -> crate::Result<&'o [u8]> {
90+
if buf.is_empty() {
91+
return Ok(buf);
92+
}
93+
9094
let new_position = (self.position + buf.len())?;
9195
if new_position > self.input_len {
9296
return Err(ErrorKind::Incomplete {

der/tests/pem.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
#![cfg(all(feature = "derive", feature = "oid", feature = "pem"))]
44

55
use der::{
6-
Decode, DecodePem, EncodePem, Sequence,
76
asn1::{BitString, ObjectIdentifier},
87
pem::{LineEnding, PemLabel},
8+
Any, Decode, DecodePem, EncodePem, Sequence,
99
};
1010

1111
/// Example SPKI document encoded as DER.
@@ -15,14 +15,14 @@ const SPKI_DER: &[u8] = include_bytes!("examples/spki.der");
1515
const SPKI_PEM: &str = include_str!("examples/spki.pem");
1616

1717
/// X.509 `AlgorithmIdentifier`
18-
#[derive(Copy, Clone, Debug, Eq, PartialEq, Sequence)]
18+
#[derive(Clone, Debug, Eq, PartialEq, Sequence)]
1919
pub struct AlgorithmIdentifier {
2020
pub algorithm: ObjectIdentifier,
21-
// pub parameters: ... (not used in spki.pem)
21+
pub parameters: Option<Any>,
2222
}
2323

2424
/// X.509 `SubjectPublicKeyInfo` (SPKI) in borrowed form
25-
#[derive(Copy, Clone, Debug, Eq, PartialEq, Sequence)]
25+
#[derive(Clone, Debug, Eq, PartialEq, Sequence)]
2626
pub struct SpkiBorrowed<'a> {
2727
pub algorithm: AlgorithmIdentifier,
2828
#[asn1(type = "BIT STRING")]
@@ -65,3 +65,17 @@ fn to_pem() {
6565
let pem = spki.to_pem(LineEnding::LF).unwrap();
6666
assert_eq!(&pem, SPKI_PEM);
6767
}
68+
69+
#[test]
70+
fn read_zero_slices_from_pem() {
71+
let spki = SpkiOwned {
72+
algorithm: AlgorithmIdentifier {
73+
algorithm: ObjectIdentifier::new_unwrap("1.2.840.113549.1.1.11"),
74+
parameters: Some(Any::null()),
75+
},
76+
subject_public_key: BitString::new(0, []).unwrap(),
77+
};
78+
79+
let pem = spki.to_pem(LineEnding::LF).unwrap();
80+
SpkiOwned::from_pem(pem).unwrap();
81+
}

0 commit comments

Comments
 (0)