dmrtd 0.3.2

eMRTD / ICAO 9303 reader core in pure Rust — BAC, PACE (ECDH-GM P-256), MRZ, LDS parsing, secure messaging. Transport-agnostic (NFC plugs in via a Transceiver trait).
Documentation
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
//! ASN.1 Object Identifiers for DMRTD.
//!
//! Defines:
//! - Custom PACE OIDs.
//! - [`Oie`] (Object Identifier Element) — the dotted-string form, a
//!   human-readable name, and the raw OID arc values.
//! - [`OiePaceProtocol`] — an [`Oie`] augmented with the cipher algorithm,
//!   key length, token agreement algorithm, and mapping type decoded from the
//!   readable name.
//! - [`Asn1ObjectIdentifierType`] — a registry of known OIDs, accessed via
//!   [`Asn1ObjectIdentifierType::instance`].
//!
//! [`KeyLength`] and [`CipherAlgorithm`] live in [`crate::crypto::aes`]
//! and are re-exported from here.

use once_cell::sync::Lazy;
use thiserror::Error;

pub use crate::crypto::aes::{CipherAlgorithm, KeyLength};

// ---------------------------------------------------------------------------
// Errors
// ---------------------------------------------------------------------------

/// Error raised by [`OiePaceProtocol`] construction or [`Oie`] validation.
#[derive(Debug, Error, PartialEq, Eq)]
#[error("OIEexception: {0}")]
pub struct OieException(pub String);

/// Error raised by [`Asn1ObjectIdentifierType`] lookups.
#[derive(Debug, Error, PartialEq, Eq)]
#[error("ASN1ObjectIdentifierObjectException: {0}")]
pub struct Asn1OidObjectException(pub String);

// ---------------------------------------------------------------------------
// Enums
// ---------------------------------------------------------------------------

/// Token agreement algorithm for PACE.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TokenAgreementAlgo {
    /// Diffie-Hellman.
    Dh,
    /// Elliptic-Curve Diffie-Hellman.
    Ecdh,
}

/// PACE mapping type.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MappingType {
    /// Generic Mapping.
    Gm,
    /// Integrated Mapping.
    Im,
    /// Chip Authentication Mapping (not currently supported).
    Cam,
}

// ---------------------------------------------------------------------------
// Oie — Object Identifier Element
// ---------------------------------------------------------------------------

/// Object Identifier Element — the dotted-string form, a human-readable
/// name, and the raw arc values.
#[derive(Debug, Clone)]
pub struct Oie {
    pub identifier_string: String,
    pub readable_name: String,
    pub identifier: Vec<u32>,
}

impl Oie {
    /// Creates a new [`Oie`].
    pub fn new(
        identifier_string: impl Into<String>,
        readable_name: impl Into<String>,
        identifier: Vec<u32>,
    ) -> Self {
        Self {
            identifier_string: identifier_string.into(),
            readable_name: readable_name.into(),
            identifier,
        }
    }

    /// Returns `true` if this [`Oie`]'s `identifier` arc-sequence equals
    /// `other`.
    pub fn compare_only_identifier(&self, other: &[u32]) -> bool {
        self.identifier == other
    }
}

impl std::fmt::Display for Oie {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "OIE: {}, {}, {:?}",
            self.identifier_string, self.readable_name, self.identifier
        )
    }
}

/// Equality follows the `==`: compares only the arc-sequence.
impl PartialEq for Oie {
    fn eq(&self, other: &Self) -> bool {
        self.identifier == other.identifier
    }
}

impl Eq for Oie {}

impl std::hash::Hash for Oie {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.identifier.hash(state);
    }
}

// ---------------------------------------------------------------------------
// OiePaceProtocol
// ---------------------------------------------------------------------------

/// PACE protocol OIE — an [`Oie`] with decoded cipher/key/agreement/mapping
/// parameters.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OiePaceProtocol {
    pub oie: Oie,
    pub cipher_algorithm: CipherAlgorithm,
    pub key_length: KeyLength,
    pub token_agreement_algorithm: TokenAgreementAlgo,
    pub mapping_type: MappingType,
}

impl OiePaceProtocol {
    /// Construct from identifier string, readable name, and arc values.
    ///
    /// The identifier string and readable name are upper-cased; the readable
    /// name is then used to derive the cipher/key-length/token-agreement/
    /// mapping parameters.
    ///
    /// # Errors
    /// Returns [`OieException`] if the readable name does not match a known
    /// PACE protocol, or if it designates an unsupported CAM mapping.
    pub fn new(
        identifier_string: impl Into<String>,
        readable_name: impl Into<String>,
        identifier: Vec<u32>,
    ) -> Result<Self, OieException> {
        let identifier_string = identifier_string.into().to_uppercase();
        let readable_name = readable_name.into().to_uppercase();
        let params = Self::params_from_name(&readable_name, &identifier_string)?;

        // Consistency guard: the OID string must be a registered PACE OID, and
        // the supplied arc sequence and readable name must agree with the
        // canonical registry entry. Rejecting registry misses (rather than
        // bypassing the guard) stops a caller pairing a valid protocol name with
        // an arbitrary / unregistered OID string or arc.
        let registered = Asn1ObjectIdentifierType::instance()
            .get_oid_by_identifier_string(&identifier_string)
            .map_err(|_| {
                OieException(format!(
                    "OIEPaceProtocol; unknown PACE OID {identifier_string} (not in registry)"
                ))
            })?;
        if registered.readable_name.to_uppercase() != readable_name
            || registered.identifier != identifier
        {
            return Err(OieException(format!(
                "OIEPaceProtocol; inconsistent PACE protocol for OID {identifier_string}: \
                 readable name {readable_name} / arc {identifier:?} does not match the \
                 registered entry ({})",
                registered.readable_name
            )));
        }

        Ok(Self {
            oie: Oie {
                identifier_string,
                readable_name,
                identifier,
            },
            cipher_algorithm: params.0,
            key_length: params.1,
            token_agreement_algorithm: params.2,
            mapping_type: params.3,
        })
    }

    /// Construct from an existing [`Oie`].
    pub fn from_oie(oie: Oie) -> Result<Self, OieException> {
        Self::new(oie.identifier_string, oie.readable_name, oie.identifier)
    }

    fn params_from_name(
        name: &str,
        id_str: &str,
    ) -> Result<(CipherAlgorithm, KeyLength, TokenAgreementAlgo, MappingType), OieException> {
        use CipherAlgorithm::{Aes, DeSede};
        use KeyLength::{S128, S192, S256};
        use MappingType::{Gm, Im};
        use TokenAgreementAlgo::{Dh, Ecdh};

        match name {
            "ID-PACE-DH-GM-3DES-CBC-CBC" => Ok((DeSede, S128, Dh, Gm)),
            "ID-PACE-DH-GM-AES-CBC-CMAC-128" => Ok((Aes, S128, Dh, Gm)),
            "ID-PACE-DH-GM-AES-CBC-CMAC-192" => Ok((Aes, S192, Dh, Gm)),
            "ID-PACE-DH-GM-AES-CBC-CMAC-256" => Ok((Aes, S256, Dh, Gm)),
            "ID-PACE-DH-IM-3DES-CBC-CBC" => Ok((DeSede, S128, Dh, Im)),
            "ID-PACE-DH-IM-AES-CBC-CMAC-128" => Ok((Aes, S128, Dh, Im)),
            "ID-PACE-DH-IM-AES-CBC-CMAC-192" => Ok((Aes, S192, Dh, Im)),
            "ID-PACE-DH-IM-AES-CBC-CMAC-256" => Ok((Aes, S256, Dh, Im)),
            "ID-PACE-ECDH-GM-3DES-CBC-CBC" => Ok((DeSede, S128, Ecdh, Gm)),
            "ID-PACE-ECDH-GM-AES-CBC-CMAC-128" => Ok((Aes, S128, Ecdh, Gm)),
            "ID-PACE-ECDH-GM-AES-CBC-CMAC-192" => Ok((Aes, S192, Ecdh, Gm)),
            "ID-PACE-ECDH-GM-AES-CBC-CMAC-256" => Ok((Aes, S256, Ecdh, Gm)),
            "ID-PACE-ECDH-IM-3DES-CBC-CBC" => Ok((DeSede, S128, Ecdh, Im)),
            "ID-PACE-ECDH-IM-AES-CBC-CMAC-128" => Ok((Aes, S128, Ecdh, Im)),
            "ID-PACE-ECDH-IM-AES-CBC-CMAC-192" => Ok((Aes, S192, Ecdh, Im)),
            "ID-PACE-ECDH-IM-AES-CBC-CMAC-256" => Ok((Aes, S256, Ecdh, Im)),
            "ID-PACE-ECDH-CAM-AES-CBC-CMAC-128"
            | "ID-PACE-ECDH-CAM-AES-CBC-CMAC-192"
            | "ID-PACE-ECDH-CAM-AES-CBC-CMAC-256" => Err(OieException(format!(
                "OIEPaceProtocol; Mapping type CAM  not supported: {id_str}"
            ))),
            _ => Err(OieException(format!(
                "OIEPaceProtocol; Unknown identifierString: {id_str}"
            ))),
        }
    }
}

impl std::fmt::Display for OiePaceProtocol {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "OIEPaceProtocol: {}, {}, {:?}, CipherAlgorithm: {:?}, KeyLength: {:?}, TokenAgreementAlgo: {:?}, MappingType: {:?}",
            self.oie.identifier_string,
            self.oie.readable_name,
            self.oie.identifier,
            self.cipher_algorithm,
            self.key_length,
            self.token_agreement_algorithm,
            self.mapping_type,
        )
    }
}

// ---------------------------------------------------------------------------
// Custom OIDs
// ---------------------------------------------------------------------------

/// Custom PACE OIDs defined by this module.
pub static CUSTOM_OIDS: Lazy<Vec<Oie>> = Lazy::new(|| {
    vec![
        Oie::new(
            "0.4.0.127.0.7.2.2.4.1.1",
            "id-PACE-DH-GM-3DES-CBC-CBC",
            vec![0, 4, 0, 127, 0, 7, 2, 2, 4, 1, 1],
        ),
        Oie::new(
            "0.4.0.127.0.7.2.2.4.1.2",
            "id-PACE-DH-GM-AES-CBC-CMAC-128",
            vec![0, 4, 0, 127, 0, 7, 2, 2, 4, 1, 2],
        ),
        Oie::new(
            "0.4.0.127.0.7.2.2.4.1.3",
            "id-PACE-DH-GM-AES-CBC-CMAC-192",
            vec![0, 4, 0, 127, 0, 7, 2, 2, 4, 1, 3],
        ),
        Oie::new(
            "0.4.0.127.0.7.2.2.4.1.4",
            "id-PACE-DH-GM-AES-CBC-CMAC-256",
            vec![0, 4, 0, 127, 0, 7, 2, 2, 4, 1, 4],
        ),
        Oie::new(
            "0.4.0.127.0.7.2.2.4.3.1",
            "id-PACE-DH-IM-3DES-CBC-CBC",
            vec![0, 4, 0, 127, 0, 7, 2, 2, 4, 3, 1],
        ),
        Oie::new(
            "0.4.0.127.0.7.2.2.4.3.2",
            "id-PACE-DH-IM-AES-CBC-CMAC-128",
            vec![0, 4, 0, 127, 0, 7, 2, 2, 4, 3, 2],
        ),
        Oie::new(
            "0.4.0.127.0.7.2.2.4.3.3",
            "id-PACE-DH-IM-AES-CBC-CMAC-192",
            vec![0, 4, 0, 127, 0, 7, 2, 2, 4, 3, 3],
        ),
        Oie::new(
            "0.4.0.127.0.7.2.2.4.3.4",
            "id-PACE-DH-IM-AES-CBC-CMAC-256",
            vec![0, 4, 0, 127, 0, 7, 2, 2, 4, 3, 4],
        ),
        Oie::new(
            "0.4.0.127.0.7.2.2.4.2.1",
            "id-PACE-ECDH-GM-3DES-CBC-CBC",
            vec![0, 4, 0, 127, 0, 7, 2, 2, 4, 2, 1],
        ),
        Oie::new(
            "0.4.0.127.0.7.2.2.4.2.2",
            "id-PACE-ECDH-GM-AES-CBC-CMAC-128",
            vec![0, 4, 0, 127, 0, 7, 2, 2, 4, 2, 2],
        ),
        Oie::new(
            "0.4.0.127.0.7.2.2.4.2.3",
            "id-PACE-ECDH-GM-AES-CBC-CMAC-192",
            vec![0, 4, 0, 127, 0, 7, 2, 2, 4, 2, 3],
        ),
        Oie::new(
            "0.4.0.127.0.7.2.2.4.2.4",
            "id-PACE-ECDH-GM-AES-CBC-CMAC-256",
            vec![0, 4, 0, 127, 0, 7, 2, 2, 4, 2, 4],
        ),
        Oie::new(
            "0.4.0.127.0.7.2.2.4.4.1",
            "id-PACE-ECDH-IM-3DES-CBC-CBC",
            vec![0, 4, 0, 127, 0, 7, 2, 2, 4, 4, 1],
        ),
        Oie::new(
            "0.4.0.127.0.7.2.2.4.4.2",
            "id-PACE-ECDH-IM-AES-CBC-CMAC-128",
            vec![0, 4, 0, 127, 0, 7, 2, 2, 4, 4, 2],
        ),
        Oie::new(
            "0.4.0.127.0.7.2.2.4.4.3",
            "id-PACE-ECDH-IM-AES-CBC-CMAC-192",
            vec![0, 4, 0, 127, 0, 7, 2, 2, 4, 4, 3],
        ),
        Oie::new(
            "0.4.0.127.0.7.2.2.4.4.4",
            "id-PACE-ECDH-IM-AES-CBC-CMAC-256",
            vec![0, 4, 0, 127, 0, 7, 2, 2, 4, 4, 4],
        ),
    ]
});

// ---------------------------------------------------------------------------
// Registry
// ---------------------------------------------------------------------------

/// Registry of known object identifiers. Currently holds [`CUSTOM_OIDS`];
/// use [`Asn1ObjectIdentifierType::instance`] to access the shared instance.
pub struct Asn1ObjectIdentifierType {
    oids: Vec<Oie>,
}

impl Asn1ObjectIdentifierType {
    /// Returns the shared registry instance.
    pub fn instance() -> &'static Self {
        static INSTANCE: Lazy<Asn1ObjectIdentifierType> = Lazy::new(|| Asn1ObjectIdentifierType {
            oids: CUSTOM_OIDS.clone(),
        });
        &INSTANCE
    }

    /// Returns all registered OIDs.
    pub fn oids(&self) -> &[Oie] {
        &self.oids
    }

    /// Returns `true` if an OID with the given dotted-string form is
    /// registered.
    pub fn has_oid_with_identifier_string(&self, identifier_string: &str) -> bool {
        self.oids
            .iter()
            .any(|o| o.identifier_string == identifier_string)
    }

    /// Returns the OID with the given dotted-string form.
    ///
    /// # Errors
    /// Returns [`Asn1OidObjectException`] if no matching OID is registered.
    pub fn get_oid_by_identifier_string(
        &self,
        identifier_string: &str,
    ) -> Result<&Oie, Asn1OidObjectException> {
        self.oids
            .iter()
            .find(|o| o.identifier_string == identifier_string)
            .ok_or_else(|| {
                Asn1OidObjectException(format!(
                    "Object identifier with identifier string {identifier_string} does not exist."
                ))
            })
    }

    /// Returns `true` if an OID with the given arc-sequence is registered.
    pub fn has_oid_with_identifier(&self, identifier: &[u32]) -> bool {
        self.oids.iter().any(|o| o.identifier == identifier)
    }

    /// Returns the OID with the given arc-sequence.
    ///
    /// # Errors
    /// Returns [`Asn1OidObjectException`] if no matching OID is registered.
    pub fn get_oid_by_identifier(
        &self,
        identifier: &[u32],
    ) -> Result<&Oie, Asn1OidObjectException> {
        self.oids
            .iter()
            .find(|o| o.identifier == identifier)
            .ok_or_else(|| {
                Asn1OidObjectException(format!(
                    "Object identifier with identifier {identifier:?} does not exist."
                ))
            })
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    // -----------------------------------------------------------------------
    // Oie
    // -----------------------------------------------------------------------

    #[test]
    fn oie_equality_uses_identifier_only() {
        let a = Oie::new(
            "0.4.0.127.0.7.2.2.4.1.2",
            "name-a",
            vec![0, 4, 0, 127, 0, 7, 2, 2, 4, 1, 2],
        );
        let b = Oie::new(
            "DIFFERENT.STRING",
            "name-b",
            vec![0, 4, 0, 127, 0, 7, 2, 2, 4, 1, 2],
        );
        assert_eq!(a, b);
    }

    #[test]
    fn oie_compare_only_identifier() {
        let a = Oie::new("x", "y", vec![1, 2, 3]);
        assert!(a.compare_only_identifier(&[1, 2, 3]));
        assert!(!a.compare_only_identifier(&[1, 2, 4]));
    }

    // -----------------------------------------------------------------------
    // OiePaceProtocol
    // -----------------------------------------------------------------------

    #[test]
    fn pace_protocol_dh_gm_aes_128() {
        let p = OiePaceProtocol::new(
            "0.4.0.127.0.7.2.2.4.1.2",
            "id-PACE-DH-GM-AES-CBC-CMAC-128",
            vec![0, 4, 0, 127, 0, 7, 2, 2, 4, 1, 2],
        )
        .unwrap();
        assert_eq!(p.cipher_algorithm, CipherAlgorithm::Aes);
        assert_eq!(p.key_length, KeyLength::S128);
        assert_eq!(p.token_agreement_algorithm, TokenAgreementAlgo::Dh);
        assert_eq!(p.mapping_type, MappingType::Gm);
        // Uppercased on construction.
        assert_eq!(p.oie.readable_name, "ID-PACE-DH-GM-AES-CBC-CMAC-128");
    }

    #[test]
    fn pace_protocol_ecdh_im_aes_256() {
        let p = OiePaceProtocol::new(
            "0.4.0.127.0.7.2.2.4.4.4",
            "id-PACE-ECDH-IM-AES-CBC-CMAC-256",
            vec![0, 4, 0, 127, 0, 7, 2, 2, 4, 4, 4],
        )
        .unwrap();
        assert_eq!(p.cipher_algorithm, CipherAlgorithm::Aes);
        assert_eq!(p.key_length, KeyLength::S256);
        assert_eq!(p.token_agreement_algorithm, TokenAgreementAlgo::Ecdh);
        assert_eq!(p.mapping_type, MappingType::Im);
    }

    #[test]
    fn pace_protocol_dh_gm_3des() {
        let p = OiePaceProtocol::new(
            "0.4.0.127.0.7.2.2.4.1.1",
            "id-PACE-DH-GM-3DES-CBC-CBC",
            vec![0, 4, 0, 127, 0, 7, 2, 2, 4, 1, 1],
        )
        .unwrap();
        assert_eq!(p.cipher_algorithm, CipherAlgorithm::DeSede);
        assert_eq!(p.key_length, KeyLength::S128);
        assert_eq!(p.token_agreement_algorithm, TokenAgreementAlgo::Dh);
        assert_eq!(p.mapping_type, MappingType::Gm);
    }

    #[test]
    fn pace_protocol_cam_is_unsupported() {
        let err = OiePaceProtocol::new(
            "0.4.0.127.0.7.2.2.4.6.2",
            "id-PACE-ECDH-CAM-AES-CBC-CMAC-128",
            vec![0, 4, 0, 127, 0, 7, 2, 2, 4, 6, 2],
        )
        .unwrap_err();
        assert!(err.0.contains("Mapping type CAM"));
    }

    #[test]
    fn pace_protocol_unknown_name() {
        let err = OiePaceProtocol::new("1.2.3", "not-a-real-pace-oid", vec![1, 2, 3]).unwrap_err();
        assert!(err.0.contains("Unknown identifierString"));
    }

    #[test]
    fn pace_protocol_rejects_name_oid_mismatch() {
        // OID string is the DH-GM-AES-128 entry, but the readable name claims
        // ECDH-GM-AES-128. The name decodes to valid params, yet it disagrees
        // with the registered OID, so construction must fail.
        let err = OiePaceProtocol::new(
            "0.4.0.127.0.7.2.2.4.1.2",
            "id-PACE-ECDH-GM-AES-CBC-CMAC-128",
            vec![0, 4, 0, 127, 0, 7, 2, 2, 4, 1, 2],
        )
        .unwrap_err();
        assert!(err.0.contains("inconsistent PACE protocol"));
    }

    #[test]
    fn pace_protocol_rejects_unregistered_oid() {
        // A valid readable name paired with an OID string that is not in the
        // registry must be rejected (the consistency guard is not bypassed).
        let err = OiePaceProtocol::new(
            "1.2.3.4.5",
            "id-PACE-ECDH-GM-AES-CBC-CMAC-128",
            vec![1, 2, 3, 4, 5],
        )
        .unwrap_err();
        assert!(err.0.contains("unknown PACE OID"));
    }

    #[test]
    fn registry_dh_gm_aes_192_resolves_to_protocol() {
        // The DH-GM-AES-192 registry entry must use the hyphenated readable
        // name so `from_oie` can decode its parameters (previously it used
        // underscores and was rejected as "Unknown identifierString").
        let reg = Asn1ObjectIdentifierType::instance();
        let oie = reg
            .get_oid_by_identifier_string("0.4.0.127.0.7.2.2.4.1.3")
            .unwrap()
            .clone();
        let p = OiePaceProtocol::from_oie(oie).unwrap();
        assert_eq!(p.cipher_algorithm, CipherAlgorithm::Aes);
        assert_eq!(p.key_length, KeyLength::S192);
        assert_eq!(p.token_agreement_algorithm, TokenAgreementAlgo::Dh);
        assert_eq!(p.mapping_type, MappingType::Gm);
    }

    #[test]
    fn pace_protocol_from_oie() {
        let oie = Oie::new(
            "0.4.0.127.0.7.2.2.4.2.4",
            "id-PACE-ECDH-GM-AES-CBC-CMAC-256",
            vec![0, 4, 0, 127, 0, 7, 2, 2, 4, 2, 4],
        );
        let p = OiePaceProtocol::from_oie(oie).unwrap();
        assert_eq!(p.cipher_algorithm, CipherAlgorithm::Aes);
        assert_eq!(p.key_length, KeyLength::S256);
        assert_eq!(p.token_agreement_algorithm, TokenAgreementAlgo::Ecdh);
        assert_eq!(p.mapping_type, MappingType::Gm);
    }

    // -----------------------------------------------------------------------
    // Asn1ObjectIdentifierType
    // -----------------------------------------------------------------------

    #[test]
    fn registry_contains_all_custom_pace_oids() {
        let reg = Asn1ObjectIdentifierType::instance();
        assert_eq!(reg.oids().len(), 16);
    }

    #[test]
    fn registry_lookup_by_identifier_string() {
        let reg = Asn1ObjectIdentifierType::instance();
        let o = reg
            .get_oid_by_identifier_string("0.4.0.127.0.7.2.2.4.2.2")
            .unwrap();
        assert_eq!(o.readable_name, "id-PACE-ECDH-GM-AES-CBC-CMAC-128");
    }

    #[test]
    fn registry_has_oid_with_identifier_string() {
        let reg = Asn1ObjectIdentifierType::instance();
        assert!(reg.has_oid_with_identifier_string("0.4.0.127.0.7.2.2.4.1.1"));
        assert!(!reg.has_oid_with_identifier_string("9.9.9.9"));
    }

    #[test]
    fn registry_lookup_by_identifier() {
        let reg = Asn1ObjectIdentifierType::instance();
        let o = reg
            .get_oid_by_identifier(&[0, 4, 0, 127, 0, 7, 2, 2, 4, 4, 4])
            .unwrap();
        assert_eq!(o.readable_name, "id-PACE-ECDH-IM-AES-CBC-CMAC-256");
    }

    #[test]
    fn registry_has_oid_with_identifier() {
        let reg = Asn1ObjectIdentifierType::instance();
        assert!(reg.has_oid_with_identifier(&[0, 4, 0, 127, 0, 7, 2, 2, 4, 3, 4]));
        assert!(!reg.has_oid_with_identifier(&[1, 2, 3]));
    }

    #[test]
    fn registry_missing_identifier_string_returns_error() {
        let reg = Asn1ObjectIdentifierType::instance();
        let err = reg.get_oid_by_identifier_string("nope").unwrap_err();
        assert!(err.0.contains("does not exist"));
    }

    #[test]
    fn registry_missing_identifier_returns_error() {
        let reg = Asn1ObjectIdentifierType::instance();
        let err = reg.get_oid_by_identifier(&[9, 9, 9]).unwrap_err();
        assert!(err.0.contains("does not exist"));
    }
}