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: refactor Header::peek #1480

Merged
merged 1 commit into from
Aug 18, 2024
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
6 changes: 3 additions & 3 deletions der/src/document.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! ASN.1 DER-encoded documents stored on the heap.

use crate::{Decode, Encode, Error, FixedTag, Length, Reader, SliceReader, Tag, Writer};
use crate::{Decode, Encode, Error, FixedTag, Header, Length, Reader, SliceReader, Tag, Writer};
use alloc::vec::Vec;
use core::fmt::{self, Debug};

Expand Down Expand Up @@ -148,7 +148,7 @@ impl<'a> Decode<'a> for Document {
type Error = Error;

fn decode<R: Reader<'a>>(reader: &mut R) -> Result<Document, Error> {
let header = reader.peek_header()?;
let header = Header::peek(reader)?;
let length = (header.encoded_len()? + header.length)?;
let bytes = reader.read_slice(length)?;

Expand Down Expand Up @@ -323,7 +323,7 @@ impl ZeroizeOnDrop for SecretDocument {}
/// Attempt to decode a ASN.1 `SEQUENCE` from the given decoder, returning the
/// entire sequence including the header.
fn decode_sequence<'a>(decoder: &mut SliceReader<'a>) -> Result<&'a [u8], Error> {
let header = decoder.peek_header()?;
let header = Header::peek(decoder)?;
header.tag.assert_eq(Tag::Sequence)?;

let len = (header.encoded_len()? + header.length)?;
Expand Down
40 changes: 40 additions & 0 deletions der/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@ impl Header {
let length = length.try_into().map_err(|_| ErrorKind::Overflow)?;
Ok(Self { tag, length })
}

/// Peek forward in the reader, attempting to decode a [`Header`] at the current position.
///
/// Does not modify the reader's state.
pub fn peek<'a>(reader: &impl Reader<'a>) -> Result<Self> {
let mut buf = [0u8; Self::MAX_SIZE];

for i in 2..Self::MAX_SIZE {
let slice = &mut buf[0..i];
if reader.peek_into(slice).is_ok() {
if let Ok(header) = Self::from_der(slice) {
return Ok(header);
}
}
}

Self::from_der(&buf)
}
}

impl<'a> Decode<'a> for Header {
Expand Down Expand Up @@ -63,3 +81,25 @@ impl DerOrd for Header {
}
}
}

#[cfg(test)]
mod tests {
use super::Header;
use crate::{Length, Reader, SliceReader, Tag};
use hex_literal::hex;

#[test]
#[allow(clippy::unwrap_used)]
fn peek() {
// INTEGER: 42
const EXAMPLE_MSG: &[u8] = &hex!("02012A00");

let reader = SliceReader::new(EXAMPLE_MSG).unwrap();
assert_eq!(reader.position(), Length::ZERO);

let header = Header::peek(&reader).unwrap();
assert_eq!(header.tag, Tag::Integer);
assert_eq!(header.length, Length::ONE);
assert_eq!(reader.position(), Length::ZERO); // Position unchanged
}
}
23 changes: 11 additions & 12 deletions der/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,18 @@ pub trait Reader<'r>: Sized {
/// the data at the current position in the decoder.
///
/// Does not modify the decoder's state.
#[deprecated(since = "0.8.0-rc.1", note = "use `Header::peek` instead")]
fn peek_header(&self) -> Result<Header, Error> {
let mut buf = [0u8; Header::MAX_SIZE];

for i in 2..Header::MAX_SIZE {
let slice = &mut buf[0..i];
if self.peek_into(slice).is_ok() {
if let Ok(header) = Header::from_der(slice) {
return Ok(header);
}
}
}
Header::peek(self)
}

Header::from_der(&buf)
/// Peek at the next byte in the reader.
#[deprecated(since = "0.8.0-rc.1", note = "use `Tag::peek` instead")]
fn peek_tag(&self) -> Result<Tag, Error> {
match self.peek_byte() {
Some(byte) => byte.try_into(),
None => Err(Error::incomplete(self.input_len())),
}
}

/// Read a single byte.
Expand Down Expand Up @@ -172,7 +171,7 @@ pub trait Reader<'r>: Sized {

/// Obtain a slice of bytes contain a complete TLV production suitable for parsing later.
fn tlv_bytes(&mut self) -> Result<&'r [u8], Error> {
let header = self.peek_header()?;
let header = Header::peek(self)?;
let header_len = header.encoded_len()?;
self.read_slice((header_len + header.length)?)
}
Expand Down
13 changes: 1 addition & 12 deletions der/src/reader/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ impl<'a> Reader<'a> for SliceReader<'a> {
#[allow(clippy::unwrap_used, clippy::panic)]
mod tests {
use super::SliceReader;
use crate::{Decode, ErrorKind, Length, Reader, Tag};
use crate::{Decode, ErrorKind, Length, Reader};
use hex_literal::hex;

// INTEGER: 42
Expand Down Expand Up @@ -226,15 +226,4 @@ mod tests {
err.kind()
);
}

#[test]
fn peek_header() {
let reader = SliceReader::new(EXAMPLE_MSG).unwrap();
assert_eq!(reader.position(), Length::ZERO);

let header = reader.peek_header().unwrap();
assert_eq!(header.tag, Tag::Integer);
assert_eq!(header.length, Length::ONE);
assert_eq!(reader.position(), Length::ZERO); // Position unchanged
}
}
Loading