1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//! X.509 objects definitions: OID, short and long names, NID (internal ID)
//!
//! Most definitions taken from OpenSSL: /usr/include/openssl/objects.h
//! Note: values does not match openssl, for ex. NIDs
//!
//! Note: the objects registry is implemented as a static array with linear search. This is not the
//! most efficient method, but makes maintainance easier.

// use std::convert::From;
use der_parser::oid::Oid;

use error::NidError;

/// ASN.1 node identifier
///
/// This enumeration lists the node IDs used (and/or supported) in x.509 certificates.
/// It is not guaranteed to be exhaustive.
#[derive(Debug,PartialEq,Clone,Copy)]
#[repr(u8)]
pub enum Nid{
    Undef,
    Algorithm,
    RsaDsi,
    Pkcs,
    Md2,
    Md5,
    Rc4,
    RsaEncryption,
    RsaMd2,
    RsaMd5,
    PbdMd2Des,
    PbeMd5Des,
    X500,
    X509,
    CommonName,
    CountryName,
    LocalityName,
    StateOrProvinceName,
    OrganizationName,
    OrganizationalUnitName,
    Rsa,
    Pkcs7,
    Pkcs7Data,
    Pkcs7SignedData,
    Pkcs7EnvelopedData,
    Pkcs7SignedAndEnvelopedData,
    Pkcs7DigestData,
    Pkcs7EncryptedData,
    Pkcs3,
    DhKeyAgreement,
    DesEcb,
    DesCfb,
    DesCbc,
    DesEde,
    DesEde3,
    IdeaCbc,
    IdeaCfb,
    IdeaEcb,
    Rc2Cbc,
    Rc2Ecb,
    Rc2Cfb,
    Rc2Ofb,
    Sha,
    Sha1WithRsaEncryption,
    DesEdeCbc,
    DesEde3Cbc,
    DesOfb,
    IdeaOfb,
    Pkcs9,
    EmailAddress,
    UnstructuredName,
    ContentType,
    MessageDigest,
    SigningTime,
    Countersignature,
    ChallengePassword,
    UnstructuredAddress,
    ExtendedCertificateAttributes,

    RsaSha1,

    SubjectKeyIdentifier,
    KeyUsage,
    PrivateKeyUsagePeriod,
    SubjectAltName,

    BasicConstraints,

    CertificatePolicies,
    AuthorityKeyIdentifier,
}

// impl From<u32> for Nid {
//     fn from(u: u32) -> Nid { Nid(u) }
// }

// helper macros to be able to use the node OID of the parent, and only append the child values
macro_rules! rsadsi {
    ( )                =>    { &[ 1, 2, 840, 113549 ] };
    ( $( $x:expr ),* ) =>    { &[ 1, 2, 840, 113549, $( $x ),* ] }
}
macro_rules! pkcs1 {
    ( )                =>    { rsadsi!( 1, 1 ) };
    ( $( $x:expr ),* ) =>    { rsadsi!( 1, 1, $( $x ),* ) }
}
macro_rules! pkcs9 {
    ( )                =>    { rsadsi!( 1, 9 ) };
    ( $( $x:expr ),* ) =>    { rsadsi!( 1, 9, $( $x ),* ) }
}
macro_rules! algo {
    ( $( $x:expr ),* ) =>    { &[ 1, 3, 14, 3, 2, $( $x ),* ] }
}
macro_rules! x509 {
    ( $( $x:expr ),* ) =>    { &[ 2, 5, 4, $( $x ),* ] }
}
macro_rules! idce {
    ( $( $x:expr ),* ) =>    { &[ 2, 5, 29, $( $x ),* ] }
}

const OBJ_ALGO : &[u64]    = algo!();
const OBJ_RSADSI : &[u64]  = rsadsi!();
const OBJ_X500 : &[u64]    = &[2, 5];
const OBJ_X509 : &[u64]    = x509!();
const OBJ_CN : &[u64]      = x509!(3);
const OBJ_C : &[u64]       = x509!(6);
const OBJ_L : &[u64]       = x509!(7);
const OBJ_ST : &[u64]      = x509!(8);
const OBJ_O : &[u64]       = x509!(10);
const OBJ_OU : &[u64]      = x509!(11);

const OBJ_PKCS9 : &[u64]   = pkcs9!();
const OBJ_EMAIL : &[u64]   = pkcs9!(1);

// XXX ...

const OBJ_RSASHA1 : &[u64] = pkcs1!(5);

// other constants

// const OBJ_IDCE : &[u64]    = idce!();
const OBJ_SKI : &[u64]     = idce!(14);
const OBJ_KU : &[u64]      = idce!(15);
const OBJ_PKUP : &[u64]    = idce!(16);
const OBJ_SAN : &[u64]     = idce!(17);

const OBJ_BC : &[u64]      = idce!(19);

const OBJ_CPOL : &[u64]    = idce!(32);

const OBJ_AKI : &[u64]     = idce!(35);



struct OidEntry {
    sn: &'static str,
    ln: &'static str,
    nid: Nid,
    oid: &'static [u64],
}

const OID_REGISTRY : &[OidEntry] = &[
    OidEntry{ sn:"UNDEF", ln:"undefined", nid:Nid::Undef, oid:&[0] },
    OidEntry{ sn:"Algorithm", ln:"algorithm", nid:Nid::Algorithm, oid:OBJ_ALGO },
    OidEntry{ sn:"rsadsi", ln:"rsadsi", nid:Nid::RsaDsi, oid:OBJ_RSADSI },
    OidEntry{ sn:"X500", ln:"X500", nid:Nid::X500, oid:OBJ_X500 },
    OidEntry{ sn:"X509", ln:"X509", nid:Nid::X509, oid:OBJ_X509 },
    OidEntry{ sn:"CN", ln:"commonName", nid:Nid::CommonName, oid:OBJ_CN },
    OidEntry{ sn:"C", ln:"countryName", nid:Nid::CountryName, oid:OBJ_C },
    OidEntry{ sn:"L", ln:"localityName", nid:Nid::LocalityName, oid:OBJ_L },
    OidEntry{ sn:"ST", ln:"stateOrProvinceName", nid:Nid::StateOrProvinceName, oid:OBJ_ST },
    OidEntry{ sn:"O", ln:"organizationName", nid:Nid::OrganizationName, oid:OBJ_O },
    OidEntry{ sn:"OU", ln:"organizationalUnitName", nid:Nid::OrganizationalUnitName, oid:OBJ_OU },

    OidEntry{ sn:"pkcs9", ln:"pkcs9", nid:Nid::Pkcs9, oid:OBJ_PKCS9 },
    OidEntry{ sn:"Email", ln:"emailAddress", nid:Nid::EmailAddress, oid:OBJ_EMAIL },

    OidEntry{ sn:"RSA-SHA1", ln:"sha1WithRSAEncryption", nid:Nid::RsaSha1, oid:OBJ_RSASHA1 },

    OidEntry{ sn:"subjectKeyIdentifier", ln:"X509v3 Subject Key Identifier", nid:Nid::SubjectKeyIdentifier, oid:OBJ_SKI },
    OidEntry{ sn:"keyUsage", ln:"X509v3 Key Usage", nid:Nid::KeyUsage, oid:OBJ_KU },
    OidEntry{ sn:"privateKeyUsagePeriod", ln:"X509v3 Private Key Usage Period", nid:Nid::PrivateKeyUsagePeriod, oid:OBJ_PKUP },
    OidEntry{ sn:"subjectAltName", ln:"X509v3 Subject Alternative Name", nid:Nid::SubjectAltName, oid:OBJ_SAN },

    OidEntry{ sn:"basicConstraints", ln:"X509v3 Basic Constraints", nid:Nid::BasicConstraints, oid:OBJ_BC },

    OidEntry{ sn:"certificatePolicies", ln:"X509v3 Certificate Policies", nid:Nid::CertificatePolicies, oid:OBJ_CPOL },
    OidEntry{ sn:"authorityKeyIdentifier", ln:"X509v3 Authority Key Identifier", nid:Nid::AuthorityKeyIdentifier, oid:OBJ_AKI },
];


/// Returns the short name corresponding to the Nid
pub fn nid2sn(nid: Nid) -> Result<&'static str,NidError> {
    // XXX pattern matching would be faster, but harder to maintain
    OID_REGISTRY
        .iter()
        .find(|ref o| o.nid == nid)
        .map(|ref o| o.sn)
        .ok_or(NidError)
}

/// Returns the long name corresponding to the Nid
pub fn nid2ln(nid: Nid) -> Result<&'static str,NidError> {
    // XXX pattern matching would be faster, but harder to maintain
    OID_REGISTRY
        .iter()
        .find(|ref o| o.nid == nid)
        .map(|ref o| o.ln)
        .ok_or(NidError)
}


pub fn nid2obj(nid: &Nid) -> Result<Oid,NidError> {
    OID_REGISTRY
        .iter()
        .find(|ref o| o.nid == *nid)
        .map(|ref o| Oid::from(o.oid))
        .ok_or(NidError)
    // XXX pattern matching would be faster, but harder to maintain
    // match nid {
    //     &Nid::RsaDsi => Ok(Oid::from(OBJ_RSADSI)),
    //     &Nid::RsaSha1 => Ok(Oid::from(OBJ_RSASHA1)),
    //     _ => Err(NidError),
    // }
}

pub fn oid2nid(obj: &Oid) -> Result<Nid,NidError> {
    // XXX could be faster by matching on known prefixes to filter subtree
    // true if obj starts with OBJ_RSADSI
    // let x = obj.iter().zip(OBJ_RSADSI).all(|(a,b)| a == b);

    // true if obj and OBJ_RSADSI are entirely equal
    // // or
    // if obj.iter().eq(OBJ_RSADSI) { return Ok(Nid::RsaDsi); }

    // if obj.iter().eq(OBJ_RSASHA1) { return Ok(Nid::RsaSha1); }

    // Err(NidError)
    OID_REGISTRY
        .iter()
        .find(|ref o| obj.iter().eq(o.oid.iter()))
        .map(|ref o| o.nid)
        .ok_or(NidError)
}

/// Returns the short name corresponding to the OID
pub fn oid2sn(obj: &Oid) -> Result<&'static str,NidError> {
    // XXX pattern matching would be faster, but harder to maintain
    OID_REGISTRY
        .iter()
        .find(|ref o| obj.iter().eq(o.oid.iter()))
        .map(|ref o| o.sn)
        .ok_or(NidError)
}


#[cfg(test)]
mod tests {
    use objects::*;
    use der_parser::oid::Oid;

#[test]
fn test_obj2nid() {
    let oid = Oid::from(&[1, 2, 840, 113549, 1, 1, 5]);
    assert_eq!(oid2nid(&oid), Ok(Nid::RsaSha1));

    let invalid_oid = Oid::from(&[5, 4, 3, 2, 1]);
    assert_eq!(oid2nid(&invalid_oid), Err(NidError));
}

#[test]
fn test_nid2sn() {
    assert_eq!(nid2sn(Nid::Undef), Ok("UNDEF"));
    assert_eq!(nid2sn(Nid::RsaSha1), Ok("RSA-SHA1"));
}

}