Skip to content

Commit 937e611

Browse files
authored
der: add class bits consts for Application and Private tag support (#1721)
1 parent e0910cd commit 937e611

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

der/src/tag/class.rs

+38-4
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,30 @@
22
33
use core::fmt;
44

5+
/// `UNIVERSAL`: built-in types whose meaning is the same in all
6+
/// applications.
7+
pub const CLASS_UNIVERSAL: u8 = 0b00000000;
8+
/// `APPLICATION`: types whose meaning is specific to an application.
9+
pub const CLASS_APPLICATION: u8 = 0b01000000;
10+
/// `CONTEXT-SPECIFIC`: types whose meaning is specific to a given
11+
/// structured type.
12+
pub const CLASS_CONTEXT_SPECIFIC: u8 = 0b10000000;
13+
/// `PRIVATE`: types whose meaning is specific to a given enterprise.
14+
pub const CLASS_PRIVATE: u8 = 0b11000000;
15+
516
/// Class of an ASN.1 tag.
617
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
718
#[repr(u8)]
819
pub enum Class {
920
/// `UNIVERSAL`: built-in types whose meaning is the same in all
1021
/// applications.
11-
Universal = 0b00000000,
22+
Universal = CLASS_UNIVERSAL,
1223

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

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

2839
/// `PRIVATE`: types whose meaning is specific to a given enterprise.
29-
Private = 0b11000000,
40+
Private = CLASS_PRIVATE,
3041
}
3142

3243
impl fmt::Display for Class {
@@ -39,3 +50,26 @@ impl fmt::Display for Class {
3950
})
4051
}
4152
}
53+
54+
impl Class {
55+
/// Returns class as 2 most-significant bits (mask 0b11000000)
56+
pub const fn bits(&self) -> u8 {
57+
*self as u8
58+
}
59+
60+
/// Returns class extracted from 2 most-significant bits (mask 0b11000000)
61+
pub const fn from_bits(bits: u8) -> Self {
62+
match (bits >> 6) & 0b11 {
63+
0b00 => Class::Universal,
64+
0b01 => Class::Application,
65+
0b10 => Class::ContextSpecific,
66+
0b11 => Class::Private,
67+
_ => unreachable!(),
68+
}
69+
}
70+
}
71+
impl From<u8> for Class {
72+
fn from(value: u8) -> Self {
73+
Class::from_bits(value)
74+
}
75+
}

0 commit comments

Comments
 (0)