Skip to content

Commit a82f668

Browse files
authored
der: add Decode::from_der_partial (#1725)
Closes #1278
1 parent 5f58a61 commit a82f668

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

der/src/decode.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use core::marker::PhantomData;
77
use crate::{PemReader, pem::PemLabel};
88

99
#[cfg(doc)]
10-
use crate::{Length, Tag};
10+
use crate::{ErrorKind, Length, Tag};
1111

1212
#[cfg(feature = "alloc")]
1313
use alloc::boxed::Box;
@@ -34,11 +34,24 @@ pub trait Decode<'a>: Sized + 'a {
3434
}
3535

3636
/// Parse `Self` from the provided DER-encoded byte slice.
37+
///
38+
/// Returns [`ErrorKind::TrailingData`] if message is incomplete.
3739
fn from_der(bytes: &'a [u8]) -> Result<Self, Self::Error> {
3840
let mut reader = SliceReader::new(bytes)?;
3941
let result = Self::decode(&mut reader)?;
4042
Ok(reader.finish(result)?)
4143
}
44+
45+
/// Parse `Self` from the provided DER-encoded byte slice.
46+
///
47+
/// Returns remaining byte slice, without checking for incomplete message.
48+
fn from_der_partial(bytes: &'a [u8]) -> Result<(Self, &'a [u8]), Self::Error> {
49+
let mut reader = SliceReader::new(bytes)?;
50+
let result = Self::decode(&mut reader)?;
51+
52+
let remaining = reader.remaining()?;
53+
Ok((result, remaining))
54+
}
4255
}
4356

4457
impl<'a, T> Decode<'a> for T

der/src/reader/slice.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl<'a> SliceReader<'a> {
5656

5757
/// Obtain the remaining bytes in this slice reader from the current cursor
5858
/// position.
59-
fn remaining(&self) -> Result<&'a [u8], Error> {
59+
pub(crate) fn remaining(&self) -> Result<&'a [u8], Error> {
6060
if self.is_failed() {
6161
Err(ErrorKind::Failed.at(self.position))
6262
} else {

der/tests/derive.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,35 @@ mod sequence {
362362
pub subject_public_key: &'a [u8],
363363
}
364364

365+
#[test]
366+
fn decode_spki() {
367+
let spki_bytes = hex!(
368+
// first SPKI
369+
"30 1A
370+
30 0D
371+
06 09
372+
2A 86 48 86 F7 0D 01 01 01
373+
05 00
374+
03 09
375+
00 A0 A1 A2 A3 A4 A5 A6 A7"
376+
// second SPKI
377+
"30 1A
378+
30 0D
379+
06 09
380+
2A 86 48 86 F7 0D 01 01 01
381+
05 00
382+
03 09
383+
00 B0 B1 B2 B3 B4 B5 B6 B7");
384+
385+
// decode first
386+
let (spki, remaining) = SubjectPublicKeyInfo::from_der_partial(&spki_bytes).unwrap();
387+
assert_eq!(spki.subject_public_key, hex!("A0 A1 A2 A3 A4 A5 A6 A7"));
388+
389+
// decode second
390+
let (spki, _) = SubjectPublicKeyInfo::from_der_partial(remaining).unwrap();
391+
assert_eq!(spki.subject_public_key, hex!("B0 B1 B2 B3 B4 B5 B6 B7"));
392+
}
393+
365394
/// PKCS#8v2 `OneAsymmetricKey`
366395
#[derive(Sequence)]
367396
pub struct OneAsymmetricKey<'a> {

0 commit comments

Comments
 (0)