x509_parser/
objects.rs

1//! X.509 helper objects definitions and registry
2//!
3//! All OID objects and definitions are now stored in the [oid-registry](https://crates.io/crates/oid-registry) crate.
4//!
5//! This crate is re-exporting `oid-registry`, so to access the OID constants the
6//! `x509_parser::oid_oid_registry` namespace can be used (see example below).
7//!
8//! ## Example
9//!
10//! To get the short name for a given OID:
11//!
12//! ```rust
13//! use x509_parser::objects::*;
14//! use x509_parser::oid_registry::*;
15//!
16//! let oid = &OID_X509_COMMON_NAME;
17//! let sn = oid2sn(oid, oid_registry());
18//! assert_eq!(sn, Ok("commonName"));
19//! ```
20
21use crate::error::NidError;
22use asn1_rs::oid;
23use lazy_static::lazy_static;
24use oid_registry::*;
25use std::collections::HashMap;
26
27lazy_static! {
28    static ref OID_REGISTRY: OidRegistry<'static> = {
29        let mut reg = OidRegistry::default().with_all_crypto().with_x509();
30        // OIDs not in the default registry can be added here
31        let entry = OidEntry::new("id-mgf1", "Mask Generator Function 1 (MGF1)");
32        reg.insert(oid! {1.2.840.113549.1.1.8}, entry);
33        reg
34    };
35    static ref ABBREV_MAP: HashMap<Oid<'static>, &'static str> = {
36        let mut m = HashMap::new();
37        m.insert(OID_X509_COMMON_NAME, "CN");
38        m.insert(OID_X509_COUNTRY_NAME, "C");
39        m.insert(OID_X509_LOCALITY_NAME, "L");
40        m.insert(OID_X509_STATE_OR_PROVINCE_NAME, "ST");
41        m.insert(OID_X509_ORGANIZATION_NAME, "O");
42        m.insert(OID_X509_ORGANIZATIONAL_UNIT, "OU");
43        m.insert(OID_DOMAIN_COMPONENT, "DC");
44        m.insert(OID_PKCS9_EMAIL_ADDRESS, "Email");
45        m
46    };
47}
48
49/// Return the abbreviation (for ex. CN for Common Name), or if not found, the OID short name
50pub fn oid2abbrev<'a>(oid: &'a Oid, registry: &'a OidRegistry) -> Result<&'a str, NidError> {
51    if let Some(abbrev) = ABBREV_MAP.get(oid) {
52        return Ok(abbrev);
53    }
54    registry.get(oid).map(|entry| entry.sn()).ok_or(NidError)
55}
56
57/// Returns the short name corresponding to the OID
58pub fn oid2sn<'a>(oid: &'a Oid, registry: &'a OidRegistry) -> Result<&'a str, NidError> {
59    registry.get(oid).map(|o| o.sn()).ok_or(NidError)
60}
61
62/// Returns the description corresponding to the OID
63pub fn oid2description<'a>(oid: &'a Oid, registry: &'a OidRegistry) -> Result<&'a str, NidError> {
64    registry.get(oid).map(|o| o.description()).ok_or(NidError)
65}
66
67/// Return a reference to the default registry of known OIDs
68pub fn oid_registry() -> &'static OidRegistry<'static> {
69    &OID_REGISTRY
70}
71
72#[cfg(test)]
73mod tests {
74    use super::*;
75    use der_parser::oid;
76
77    // This test is meant to check syntax of pattern matching with OID objects
78    #[test]
79    fn test_oid_match() {
80        let oid = oid!(1.2.840 .113549 .1 .1 .5);
81        if oid == OID_PKCS1_SHA1WITHRSA {
82            // ok
83        }
84        // matching is not possible with Cow constants in pattern,
85        // see https://rust-lang.github.io/rfcs/1445-restrict-constants-in-patterns.html
86        //
87        // match oid {
88        //     OID_PKCS1_SHA1WITHRSA => (),
89        //     _ => (),
90        // }
91    }
92}