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 class bits consts for Application and Private tag support #1721

Merged
merged 1 commit into from
Mar 24, 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
42 changes: 38 additions & 4 deletions der/src/tag/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,30 @@

use core::fmt;

/// `UNIVERSAL`: built-in types whose meaning is the same in all
/// applications.
pub const CLASS_UNIVERSAL: u8 = 0b00000000;
/// `APPLICATION`: types whose meaning is specific to an application.
pub const CLASS_APPLICATION: u8 = 0b01000000;
/// `CONTEXT-SPECIFIC`: types whose meaning is specific to a given
/// structured type.
pub const CLASS_CONTEXT_SPECIFIC: u8 = 0b10000000;
/// `PRIVATE`: types whose meaning is specific to a given enterprise.
pub const CLASS_PRIVATE: u8 = 0b11000000;

/// Class of an ASN.1 tag.
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[repr(u8)]
pub enum Class {
/// `UNIVERSAL`: built-in types whose meaning is the same in all
/// applications.
Universal = 0b00000000,
Universal = CLASS_UNIVERSAL,

/// `APPLICATION`: types whose meaning is specific to an application,
///
/// Types in two different applications may have the same
/// application-specific tag and different meanings.
Application = 0b01000000,
Application = CLASS_APPLICATION,

/// `CONTEXT-SPECIFIC`: types whose meaning is specific to a given
/// structured type.
Expand All @@ -23,10 +34,10 @@ pub enum Class {
/// with the same underlying tag within the context of a given structured
/// type, and component types in two different structured types may have
/// the same tag and different meanings.
ContextSpecific = 0b10000000,
ContextSpecific = CLASS_CONTEXT_SPECIFIC,

/// `PRIVATE`: types whose meaning is specific to a given enterprise.
Private = 0b11000000,
Private = CLASS_PRIVATE,
}

impl fmt::Display for Class {
Expand All @@ -39,3 +50,26 @@ impl fmt::Display for Class {
})
}
}

impl Class {
/// Returns class as 2 most-significant bits (mask 0b11000000)
pub const fn bits(&self) -> u8 {
*self as u8
}

/// Returns class extracted from 2 most-significant bits (mask 0b11000000)
pub const fn from_bits(bits: u8) -> Self {
match (bits >> 6) & 0b11 {
0b00 => Class::Universal,
0b01 => Class::Application,
0b10 => Class::ContextSpecific,
0b11 => Class::Private,
_ => unreachable!(),
}
}
}
impl From<u8> for Class {
fn from(value: u8) -> Self {
Class::from_bits(value)
}
}
2 changes: 1 addition & 1 deletion der/tests/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ mod sequence {
pub owned_optional_implicit_bytes: Option<Vec<u8>>,

#[asn1(
type = "OCTET STRING",
type = "BIT STRING",
context_specific = "6",
optional = "true",
tag_mode = "EXPLICIT"
Expand Down