Skip to main content

ObjectIdentifier

Struct ObjectIdentifier 

Source
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

Source

pub fn new(components: &[u32]) -> Result<Self>

Create a new ObjectIdentifier from a slice of component integers.

§Validation
  • components must have at least 2 elements.
  • components[0] must be 0, 1, or 2.
  • If components[0] is 0 or 1, then components[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?
examples/oid_usage.rs (line 39)
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}
Source

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?
examples/oid_usage.rs (line 78)
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}
Source

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");
Source

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

Source§

fn clone(&self) -> ObjectIdentifier

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ObjectIdentifier

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Decode<'_> for ObjectIdentifier

Source§

fn decode(decoder: &mut Decoder<'_>) -> Result<Self>

Decode one ASN.1 value from decoder. Read more
Source§

impl Display for ObjectIdentifier

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Encode for ObjectIdentifier

Source§

fn encode(&self, encoder: &mut Encoder) -> Result<()>

Encode this value as ASN.1 TLV bytes into encoder. Read more
Source§

fn encoded_len(&self) -> Result<usize>

Return the number of bytes that encode will write. Read more
Source§

impl Eq for ObjectIdentifier

Source§

impl FromStr for ObjectIdentifier

Available on crate feature std only.
Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl PartialEq for ObjectIdentifier

Source§

fn eq(&self, other: &ObjectIdentifier) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for ObjectIdentifier

Source§

impl Tagged for ObjectIdentifier

Source§

fn tag() -> Tag

Return the outermost ASN.1 tag for this type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromDer for T
where T: for<'a> Decode<'a>,

Source§

fn from_der(input: &[u8]) -> Result<T, Error>

Decode one value from DER-encoded input. Read more
Source§

fn from_ber(input: &[u8]) -> Result<T, Error>

Decode one value from BER-encoded input. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> TagForOptional for T
where T: Tagged,

Source§

fn optional_tag() -> Option<Tag>

Return the expected tag for optional-field peek-ahead, or None if the type accepts multiple tags (CHOICE) or any tag (ANY / Element<'a>).
Source§

fn accepts_tag_for_optional(tag: Tag) -> bool

Return true when tag (the peeked next tag in the stream) indicates that this type is present as an optional field. Read more
Source§

impl<T> ToDer for T
where T: Encode,

Source§

fn to_der(&self) -> Result<Vec<u8>>

Encode self to DER and return the bytes.
Source§

fn to_ber(&self) -> Result<Vec<u8>>

Encode self to BER and return the bytes.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.