Skip to content

Commit

Permalink
Make TagNumber support tags beyond 30.
Browse files Browse the repository at this point in the history
  • Loading branch information
turbocool3r committed Feb 8, 2025
1 parent cbb1439 commit c1113a2
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 170 deletions.
4 changes: 1 addition & 3 deletions der/src/asn1/context_specific.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ impl<T> ContextSpecific<T> {
F: FnOnce(&mut R) -> Result<Self, E>,
E: From<Error>,
{
while let Some(octet) = reader.peek_byte() {
let tag = Tag::try_from(octet)?;

while let Some(tag) = Tag::peek_optional(reader)? {
if !tag.is_context_specific() || (tag.number() > tag_number) {
break;
} else if tag.number() == tag_number {
Expand Down
4 changes: 2 additions & 2 deletions der/src/asn1/optional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ where
type Error = T::Error;

fn decode<R: Reader<'a>>(reader: &mut R) -> Result<Option<T>, Self::Error> {
if let Some(byte) = reader.peek_byte() {
if T::can_decode(Tag::try_from(byte)?) {
if let Some(tag) = Tag::peek_optional(reader)? {
if T::can_decode(tag) {
return T::decode(reader).map(Some);
}
}
Expand Down
2 changes: 1 addition & 1 deletion der/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ where
{
/// Compute the length of this value in bytes when encoded as ASN.1 DER.
fn encoded_len(&self) -> Result<Length> {
self.value_len().and_then(|len| len.for_tlv())
self.value_len().and_then(|len| len.for_tlv(self.tag()))
}

/// Encode this value as ASN.1 DER using the provided [`Writer`].
Expand Down
2 changes: 1 addition & 1 deletion der/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct Header {

impl Header {
/// Maximum number of DER octets a header can be in this crate.
pub(crate) const MAX_SIZE: usize = 1 + Length::MAX_SIZE;
pub(crate) const MAX_SIZE: usize = Tag::MAX_SIZE + Length::MAX_SIZE;

/// Create a new [`Header`] from a [`Tag`] and a specified length.
///
Expand Down
6 changes: 3 additions & 3 deletions der/src/length.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Length calculations for encoded ASN.1 DER values
use crate::{Decode, DerOrd, Encode, Error, ErrorKind, Reader, Result, SliceWriter, Writer};
use crate::{Decode, DerOrd, Encode, Error, ErrorKind, Reader, Result, SliceWriter, Tag, Writer};
use core::{
cmp::Ordering,
fmt,
Expand Down Expand Up @@ -51,8 +51,8 @@ impl Length {

/// Get the length of DER Tag-Length-Value (TLV) encoded data if `self`
/// is the length of the inner "value" portion of the message.
pub fn for_tlv(self) -> Result<Self> {
Self::ONE + self.encoded_len()? + self
pub fn for_tlv(self, tag: Tag) -> Result<Self> {
tag.encoded_len()? + self.encoded_len()? + self
}

/// Perform saturating addition of two lengths.
Expand Down
5 changes: 1 addition & 4 deletions der/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,7 @@ pub trait Reader<'r>: Sized {
/// 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())),
}
Tag::peek(self)
}

/// Read a single byte.
Expand Down
Loading

0 comments on commit c1113a2

Please sign in to comment.