Skip to main content

oid_usage/
oid_usage.rs

1//! Example: Working with ASN.1 Object Identifiers (OIDs)
2//!
3//! This example demonstrates how to create, encode, decode, and use
4//! Object Identifiers which are commonly used in X.509 certificates
5//! and other cryptographic protocols.
6//!
7//! Run with: cargo run --example oid_usage
8
9use std::str::FromStr;
10use synta::{FromDer, ObjectIdentifier, ToDer};
11
12fn main() {
13    println!("=== ASN.1 Object Identifier (OID) Example ===\n");
14
15    // Common OIDs used in cryptography and X.509
16    common_oids();
17
18    // Encoding and decoding
19    encode_decode_example();
20
21    // Working with OID components
22    oid_components();
23}
24
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}
68
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}