pub struct ObjectIdentifier { /* private fields */ }Expand description
ASN.1 OBJECT IDENTIFIER
Uses SmallVec to avoid allocations for most OIDs (< 10 components)
Implementations§
Source§impl ObjectIdentifier
impl ObjectIdentifier
Sourcepub fn new(components: &[u32]) -> Result<Self>
pub fn new(components: &[u32]) -> Result<Self>
Create a new ObjectIdentifier from a slice of component integers.
§Validation
componentsmust have at least 2 elements.components[0]must be 0, 1, or 2.- If
components[0]is 0 or 1, thencomponents[1]must be < 40.
§Errors
Returns Error::InvalidOid if any
validation rule is violated.
§Example
use synta::ObjectIdentifier;
// rsadsi (1.2.840.113549)
let oid = ObjectIdentifier::new(&[1, 2, 840, 113549]).unwrap();
assert_eq!(oid.to_string(), "1.2.840.113549");Examples found in repository?
25fn common_oids() {
26 println!("Common OIDs in Cryptography:");
27 println!("-----------------------------");
28
29 let oids = vec![
30 ("RSA Encryption", vec![1, 2, 840, 113549, 1, 1, 1]),
31 ("SHA-256", vec![2, 16, 840, 1, 101, 3, 4, 2, 1]),
32 ("ECDSA with SHA-256", vec![1, 2, 840, 10045, 4, 3, 2]),
33 ("Common Name (CN)", vec![2, 5, 4, 3]),
34 ("Country (C)", vec![2, 5, 4, 6]),
35 ("Organization (O)", vec![2, 5, 4, 10]),
36 ];
37
38 for (name, components) in oids {
39 let oid = ObjectIdentifier::new(&components).unwrap();
40 println!(" {}: {}", name, oid);
41 }
42 println!();
43}
44
45fn encode_decode_example() {
46 println!("Encoding and Decoding Example:");
47 println!("------------------------------");
48
49 // Create OID for RSA encryption: 1.2.840.113549.1.1.1
50 let components = vec![1, 2, 840, 113549, 1, 1, 1];
51 let oid = ObjectIdentifier::new(&components).unwrap();
52
53 println!("Created OID: {}", oid);
54
55 // Encode to DER
56 let encoded = oid.to_der().unwrap();
57
58 println!("Encoded: {} bytes", encoded.len());
59 println!("Bytes: {:02X?}", encoded);
60
61 // Decode
62 let decoded = ObjectIdentifier::from_der(&encoded).unwrap();
63
64 println!("Decoded: {}", decoded);
65 println!("Match: {}", oid == decoded);
66 println!();
67}Sourcepub fn components(&self) -> &[u32]
pub fn components(&self) -> &[u32]
Return the OID as a slice of component integers.
The slice begins with the two top-level arc values (e.g. [1, 2, …]).
Matching against a known OID is most efficient by comparing the slice
directly rather than converting to a dotted-decimal string:
use synta::ObjectIdentifier;
let oid = ObjectIdentifier::new(&[1, 2, 840, 113549]).unwrap();
assert!(matches!(oid.components(), [1, 2, 840, 113549]));Examples found in repository?
69fn oid_components() {
70 println!("Working with OID Components:");
71 println!("----------------------------");
72
73 // Parse OID from string notation
74 let oid_str = "2.5.29.19"; // Basic Constraints extension
75 let oid = ObjectIdentifier::from_str(oid_str).unwrap();
76
77 println!("OID: {}", oid);
78 println!("Components: {:?}", oid.components());
79 println!("Number of components: {}", oid.components().len());
80
81 // Access individual components
82 let components = oid.components();
83 for (i, component) in components.iter().enumerate() {
84 println!(" Component {}: {}", i, component);
85 }
86 println!();
87
88 // Common X.509 extension OIDs
89 println!("Common X.509 Extension OIDs:");
90 let extensions = vec![
91 ("Subject Alternative Name", "2.5.29.17"),
92 ("Basic Constraints", "2.5.29.19"),
93 ("Key Usage", "2.5.29.15"),
94 ("Extended Key Usage", "2.5.29.37"),
95 ("CRL Distribution Points", "2.5.29.31"),
96 ("Authority Key Identifier", "2.5.29.35"),
97 ];
98
99 for (name, oid_str) in extensions {
100 let oid = ObjectIdentifier::from_str(oid_str).unwrap();
101 println!(" {}: {}", name, oid);
102 }
103}Sourcepub fn from_content_bytes(data: &[u8]) -> Result<Self>
pub fn from_content_bytes(data: &[u8]) -> Result<Self>
Parse an OID from its DER/BER content bytes — the raw value bytes
inside an OID TLV, with the tag (0x06) and length already stripped.
This is the low-level counterpart to Decode
for situations where the tag and length have already been consumed, for
example after stripping an implicit context tag from a GeneralName
registeredID [8] IMPLICIT OID alternative.
§Errors
Returns Error::InvalidOid if data is
empty, contains a truncated base-128 sequence, or causes a u32
overflow in any component.
§Example
use synta::ObjectIdentifier;
// commonName 2.5.4.3 — DER content bytes (tag+length stripped)
let oid = ObjectIdentifier::from_content_bytes(&[0x55, 0x04, 0x03]).unwrap();
assert_eq!(oid.to_string(), "2.5.4.3");Sourcepub fn to_content_bytes(&self) -> Vec<u8> ⓘ
pub fn to_content_bytes(&self) -> Vec<u8> ⓘ
Encode this OID as DER content bytes — the raw base-128 arc bytes
without the 0x06 tag byte or the length field.
This is the symmetric counterpart to from_content_bytes and is
useful for embedding OIDs in non-DER formats such as CBOR (RFC 9090
tag 111) without going through the full DER TLV encoder.
§Example
use synta::ObjectIdentifier;
// commonName 2.5.4.3
let oid = ObjectIdentifier::new(&[2, 5, 4, 3]).unwrap();
assert_eq!(oid.to_content_bytes(), &[0x55, 0x04, 0x03]);
// Round-trip
assert_eq!(ObjectIdentifier::from_content_bytes(&oid.to_content_bytes()).unwrap(), oid);Trait Implementations§
Source§impl Clone for ObjectIdentifier
impl Clone for ObjectIdentifier
Source§fn clone(&self) -> ObjectIdentifier
fn clone(&self) -> ObjectIdentifier
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for ObjectIdentifier
impl Debug for ObjectIdentifier
Source§impl Decode<'_> for ObjectIdentifier
impl Decode<'_> for ObjectIdentifier
Source§impl Display for ObjectIdentifier
impl Display for ObjectIdentifier
Source§impl Encode for ObjectIdentifier
impl Encode for ObjectIdentifier
impl Eq for ObjectIdentifier
Source§impl FromStr for ObjectIdentifier
Available on crate feature std only.
impl FromStr for ObjectIdentifier
std only.Source§impl PartialEq for ObjectIdentifier
impl PartialEq for ObjectIdentifier
impl StructuralPartialEq for ObjectIdentifier
Auto Trait Implementations§
impl Freeze for ObjectIdentifier
impl RefUnwindSafe for ObjectIdentifier
impl Send for ObjectIdentifier
impl Sync for ObjectIdentifier
impl Unpin for ObjectIdentifier
impl UnsafeUnpin for ObjectIdentifier
impl UnwindSafe for ObjectIdentifier
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> TagForOptional for Twhere
T: Tagged,
impl<T> TagForOptional for Twhere
T: Tagged,
Source§fn optional_tag() -> Option<Tag>
fn optional_tag() -> Option<Tag>
None if
the type accepts multiple tags (CHOICE) or any tag (ANY / Element<'a>).