Skip to content

Make TagNumber support tags beyond 30. #1651

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

Merged
merged 1 commit into from
Feb 25, 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
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
7 changes: 2 additions & 5 deletions der/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,10 @@ pub trait Reader<'r>: Sized {
Header::peek(self)
}

/// Peek at the next byte in the reader.
/// Peek at the next tag 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