• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 use crate::error::NidError;
22 use der_parser::oid::Oid;
23 use lazy_static::lazy_static;
24 use oid_registry::*;
25 use std::collections::HashMap;
26 
27 lazy_static! {
28     static ref OID_REGISTRY: OidRegistry<'static> = {
29         let reg = OidRegistry::default().with_all_crypto().with_x509();
30         // OIDs not in the default registry can be added here
31         reg
32     };
33     static ref ABBREV_MAP: HashMap<Oid<'static>, &'static str> = {
34         let mut m = HashMap::new();
35         m.insert(OID_X509_COMMON_NAME, "CN");
36         m.insert(OID_X509_COUNTRY_NAME, "C");
37         m.insert(OID_X509_LOCALITY_NAME, "L");
38         m.insert(OID_X509_STATE_OR_PROVINCE_NAME, "ST");
39         m.insert(OID_X509_ORGANIZATION_NAME, "O");
40         m.insert(OID_X509_ORGANIZATIONAL_UNIT, "OU");
41         m.insert(OID_DOMAIN_COMPONENT, "DC");
42         m.insert(OID_PKCS9_EMAIL_ADDRESS, "Email");
43         m
44     };
45 }
46 
47 /// Return the abbreviation (for ex. CN for Common Name), or if not found, the OID short name
oid2abbrev<'a>(oid: &'a Oid, registry: &'a OidRegistry) -> Result<&'a str, NidError>48 pub fn oid2abbrev<'a>(oid: &'a Oid, registry: &'a OidRegistry) -> Result<&'a str, NidError> {
49     if let Some(abbrev) = ABBREV_MAP.get(oid) {
50         return Ok(abbrev);
51     }
52     registry.get(oid).map(|entry| entry.sn()).ok_or(NidError)
53 }
54 
55 /// Returns the short name corresponding to the OID
oid2sn<'a>(oid: &'a Oid, registry: &'a OidRegistry) -> Result<&'a str, NidError>56 pub fn oid2sn<'a>(oid: &'a Oid, registry: &'a OidRegistry) -> Result<&'a str, NidError> {
57     registry.get(oid).map(|o| o.sn()).ok_or(NidError)
58 }
59 
60 /// Returns the description corresponding to the OID
oid2description<'a>(oid: &'a Oid, registry: &'a OidRegistry) -> Result<&'a str, NidError>61 pub fn oid2description<'a>(oid: &'a Oid, registry: &'a OidRegistry) -> Result<&'a str, NidError> {
62     registry.get(oid).map(|o| o.description()).ok_or(NidError)
63 }
64 
65 /// Return a reference to the default registry of known OIDs
oid_registry() -> &'static OidRegistry<'static>66 pub fn oid_registry() -> &'static OidRegistry<'static> {
67     &OID_REGISTRY
68 }
69 
70 #[cfg(test)]
71 mod tests {
72     use super::*;
73     use der_parser::oid;
74 
75     // This test is meant to check syntax of pattern matching with OID objects
76     #[test]
test_oid_match()77     fn test_oid_match() {
78         let oid = oid!(1.2.840 .113549 .1 .1 .5);
79         if oid == OID_PKCS1_SHA1WITHRSA {
80             // ok
81         }
82         // matching is not possible with Cow constants in pattern,
83         // see https://rust-lang.github.io/rfcs/1445-restrict-constants-in-patterns.html
84         //
85         // match oid {
86         //     OID_PKCS1_SHA1WITHRSA => (),
87         //     _ => (),
88         // }
89     }
90 }
91