Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

der: add from_der_partial on Decode trait #1725

Merged
merged 4 commits into from
Mar 16, 2025
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
15 changes: 14 additions & 1 deletion der/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use core::marker::PhantomData;
use crate::{PemReader, pem::PemLabel};

#[cfg(doc)]
use crate::{Length, Tag};
use crate::{ErrorKind, Length, Tag};

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

/// Parse `Self` from the provided DER-encoded byte slice.
///
/// Returns [`ErrorKind::TrailingData`] if message is incomplete.
fn from_der(bytes: &'a [u8]) -> Result<Self, Self::Error> {
let mut reader = SliceReader::new(bytes)?;
let result = Self::decode(&mut reader)?;
Ok(reader.finish(result)?)
}

/// Parse `Self` from the provided DER-encoded byte slice.
///
/// Returns remaining byte slice, without checking for incomplete message.
fn from_der_partial(bytes: &'a [u8]) -> Result<(Self, &'a [u8]), Self::Error> {
let mut reader = SliceReader::new(bytes)?;
let result = Self::decode(&mut reader)?;

let remaining = reader.remaining()?;
Ok((result, remaining))
}
}

impl<'a, T> Decode<'a> for T
Expand Down
2 changes: 1 addition & 1 deletion der/src/reader/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl<'a> SliceReader<'a> {

/// Obtain the remaining bytes in this slice reader from the current cursor
/// position.
fn remaining(&self) -> Result<&'a [u8], Error> {
pub(crate) fn remaining(&self) -> Result<&'a [u8], Error> {
if self.is_failed() {
Err(ErrorKind::Failed.at(self.position))
} else {
Expand Down
29 changes: 29 additions & 0 deletions der/tests/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,35 @@ mod sequence {
pub subject_public_key: &'a [u8],
}

#[test]
fn decode_spki() {
let spki_bytes = hex!(
// first SPKI
"30 1A
30 0D
06 09
2A 86 48 86 F7 0D 01 01 01
05 00
03 09
00 A0 A1 A2 A3 A4 A5 A6 A7"
// second SPKI
"30 1A
30 0D
06 09
2A 86 48 86 F7 0D 01 01 01
05 00
03 09
00 B0 B1 B2 B3 B4 B5 B6 B7");

// decode first
let (spki, remaining) = SubjectPublicKeyInfo::from_der_partial(&spki_bytes).unwrap();
assert_eq!(spki.subject_public_key, hex!("A0 A1 A2 A3 A4 A5 A6 A7"));

// decode second
let (spki, _) = SubjectPublicKeyInfo::from_der_partial(remaining).unwrap();
assert_eq!(spki.subject_public_key, hex!("B0 B1 B2 B3 B4 B5 B6 B7"));
}

/// PKCS#8v2 `OneAsymmetricKey`
#[derive(Sequence)]
pub struct OneAsymmetricKey<'a> {
Expand Down