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
//! ISO/IEC 7816-4 Command APDU.
//!
//! # Serialisation cases
//!
//! | Case | Data | Le  | Extended? | Wire format                          |
//! |------|------|-----|-----------|--------------------------------------|
//! | 1    | No   | No  | —         | `[CLA INS P1 P2]`                    |
//! | 2s   | No   | Yes | No        | `[CLA INS P1 P2 Le]`                 |
//! | 2e   | No   | Yes | Yes       | `[CLA INS P1 P2 00 LeHi LeLo]`      |
//! | 3s   | Yes  | No  | No        | `[CLA INS P1 P2 Lc data]`           |
//! | 3e   | Yes  | No  | Yes       | `[CLA INS P1 P2 00 00 LcHi LcLo data]` |
//! | 4s   | Yes  | Yes | No        | `[CLA INS P1 P2 Lc data Le]`        |
//! | 4e   | Yes  | Yes | Yes       | `[CLA INS P1 P2 00 00 LcHi LcLo data LeHi LeLo]` |
//!
//! **Extended form** is used when `data.len() > 255` OR `ne > 256`.
//!
//! Special Le values: in short form `ne == 256` → `0x00`. In extended form
//! `ne == 256` → `0x0100` (the literal value); only `ne == 65536` → `0x0000`.

use std::fmt;
use thiserror::Error;

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

/// Errors that can occur when constructing a [`CommandApdu`].
#[derive(Debug, Error, PartialEq, Eq)]
pub enum CommandApduError {
    /// The data field exceeds the ISO 7816-4 maximum of 65 535 bytes.
    #[error("Command APDU data length {0} exceeds maximum of 65535 bytes")]
    DataTooLong(usize),

    /// The `ne` (expected response length) value exceeds 65 536.
    #[error("Command APDU ne value {0} exceeds maximum of 65536")]
    InvalidNe(u32),
}

// ---------------------------------------------------------------------------
// CommandApdu
// ---------------------------------------------------------------------------

/// An ISO/IEC 7816-4 Command APDU.
///
/// # Fields
/// - `cla`, `ins`, `p1`, `p2` — the four mandatory header bytes.
/// - `data` — optional command data field (max 65 535 bytes).
/// - `ne` — expected response length (0 = none; max 65 536). In short form
///   `ne == 256` is the `0x00` "any length" sentinel; in extended form only
///   `ne == 65 536` is the `0x0000` sentinel, while `ne == 256` is the literal
///   `0x0100`.
#[derive(Debug, Clone)]
pub struct CommandApdu {
    pub cla: u8,
    pub ins: u8,
    pub p1: u8,
    pub p2: u8,
    /// Optional command data (max 65 535 bytes).
    pub data: Option<Vec<u8>>,
    /// Expected response length: 0 = not included; max = 65 536.
    pub ne: u32,
}

impl CommandApdu {
    /// Creates a new [`CommandApdu`], validating `data` length and `ne` range.
    ///
    /// # Errors
    /// - [`CommandApduError::DataTooLong`] — `data.len() > 65535`.
    /// - [`CommandApduError::InvalidNe`] — `ne > 65536`.
    pub fn new(
        cla: u8,
        ins: u8,
        p1: u8,
        p2: u8,
        data: Option<Vec<u8>>,
        ne: u32,
    ) -> Result<Self, CommandApduError> {
        if let Some(ref d) = data {
            if d.len() > 0xFFFF {
                return Err(CommandApduError::DataTooLong(d.len()));
            }
        }
        if ne > 65536 {
            return Err(CommandApduError::InvalidNe(ne));
        }
        Ok(Self {
            cla,
            ins,
            p1,
            p2,
            data,
            ne,
        })
    }

    /// Returns the four-byte command header `[CLA, INS, P1, P2]`.
    pub fn raw_header(&self) -> [u8; 4] {
        [self.cla, self.ins, self.p1, self.p2]
    }

    /// Serialises the command APDU according to ISO/IEC 7816-4.
    ///
    /// See the module-level documentation for the encoding rules.
    pub fn to_bytes(&self) -> Vec<u8> {
        let data_len = self.data.as_ref().map_or(0, |d| d.len());
        // Non-empty data present in the Lc/data fields?
        let has_data = self.data.as_ref().is_some_and(|d| !d.is_empty());

        // Extended form is required when data > 255 bytes OR ne > 256.
        let extended = data_len > 255 || self.ne > 256;

        let mut out = Vec::new();
        out.extend_from_slice(&self.raw_header());

        if has_data {
            // ---- Lc field ----
            if extended {
                // Extended Lc: 0x00 followed by two-byte big-endian length.
                out.push(0x00);
                out.extend_from_slice(&(data_len as u16).to_be_bytes());
            } else {
                // Short Lc: single byte.
                out.push(data_len as u8);
            }

            // ---- Data field ----
            out.extend_from_slice(self.data.as_ref().unwrap());

            // ---- Le field (if ne > 0) ----
            if self.ne > 0 {
                if extended {
                    // Extended Le: two-byte big-endian. Only 65536 maps to the
                    // 0x0000 "any length" sentinel; 256 is the literal 0x0100.
                    let le_val = if self.ne == 65536 {
                        0u16
                    } else {
                        self.ne as u16
                    };
                    out.extend_from_slice(&le_val.to_be_bytes());
                } else {
                    // Short Le: single byte; 256 → 0x00.
                    let le_val = if self.ne == 256 { 0u8 } else { self.ne as u8 };
                    out.push(le_val);
                }
            }
        } else {
            // No data (Case 1 or Case 2).
            if self.ne > 0 {
                if extended {
                    // Extended Case 2: prefix 0x00 (addByte) then two-byte Le.
                    // Only 65536 maps to 0x0000; 256 is the literal 0x0100.
                    out.push(0x00);
                    let le_val = if self.ne == 65536 {
                        0u16
                    } else {
                        self.ne as u16
                    };
                    out.extend_from_slice(&le_val.to_be_bytes());
                } else {
                    // Short Case 2: single Le byte; 256 → 0x00.
                    let le_val = if self.ne == 256 { 0u8 } else { self.ne as u8 };
                    out.push(le_val);
                }
            }
            // Case 1: ne == 0, no data → only the 4-byte header.
        }

        out
    }
}

impl fmt::Display for CommandApdu {
    /// Formats as `"C-APDU(CLA:XX INS:XX P1:XX P2:XX Le:N Lc:M Data:...)"`.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let lc = self.data.as_ref().map_or(0, |d| d.len());
        let data_str = match &self.data {
            Some(d) if !d.is_empty() => hex::encode(d),
            _ => "None".to_string(),
        };
        write!(
            f,
            "C-APDU(CLA:{:02X} INS:{:02X} P1:{:02X} P2:{:02X} Le:{} Lc:{} Data:{})",
            self.cla, self.ins, self.p1, self.p2, self.ne, lc, data_str
        )
    }
}

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

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

    fn apdu(cla: u8, ins: u8, p1: u8, p2: u8, data: Option<&[u8]>, ne: u32) -> Vec<u8> {
        CommandApdu::new(cla, ins, p1, p2, data.map(|d| d.to_vec()), ne)
            .unwrap()
            .to_bytes()
    }

    // -----------------------------------------------------------------------
    // Case 1 — no data, no Le
    // -----------------------------------------------------------------------

    #[test]
    fn case1_no_data_no_le() {
        assert_eq!(
            apdu(0x00, 0x10, 0x20, 0x30, None, 0),
            hex::decode("00102030").unwrap()
        );
    }

    // -----------------------------------------------------------------------
    // Case 2s — no data, short Le
    // -----------------------------------------------------------------------

    #[test]
    fn case2s_ne_0xa0() {
        assert_eq!(
            apdu(0x00, 0x10, 0x20, 0x30, None, 0xA0),
            hex::decode("00102030A0").unwrap()
        );
    }

    #[test]
    fn case2s_ne_0x80() {
        assert_eq!(
            apdu(0x00, 0x10, 0x20, 0x30, None, 0x80),
            hex::decode("0010203080").unwrap()
        );
    }

    #[test]
    fn case2s_ne_256_encoded_as_0x00() {
        // ne = 256 → short form 0x00 (means "accept any length")
        assert_eq!(
            apdu(0x00, 0x10, 0x20, 0x30, None, 256),
            hex::decode("0010203000").unwrap()
        );
    }

    // -----------------------------------------------------------------------
    // Case 2e — no data, extended Le
    // -----------------------------------------------------------------------

    #[test]
    fn case2e_ne_0xaabb() {
        assert_eq!(
            apdu(0x00, 0x10, 0x20, 0x30, None, 0xAABB),
            hex::decode("0010203000AABB").unwrap()
        );
    }

    #[test]
    fn case2e_ne_0x180() {
        assert_eq!(
            apdu(0x00, 0x10, 0x20, 0x30, None, 0x180),
            hex::decode("00102030000180").unwrap()
        );
    }

    #[test]
    fn case2e_ne_65536_encoded_as_0x0000() {
        // ne = 65536 → extended 0x0000
        assert_eq!(
            apdu(0x00, 0x10, 0x20, 0x30, None, 65536),
            hex::decode("00102030000000").unwrap()
        );
    }

    // -----------------------------------------------------------------------
    // Case 3s — data, no Le, short
    // -----------------------------------------------------------------------

    #[test]
    fn case3s_data_no_le() {
        assert_eq!(
            apdu(
                0x00,
                0x10,
                0x20,
                0x30,
                Some(&hex::decode("0102030405060708").unwrap()),
                0
            ),
            hex::decode("00102030080102030405060708").unwrap()
        );
    }

    #[test]
    fn case3s_4_byte_data_no_le() {
        assert_eq!(
            apdu(
                0x00,
                0x10,
                0x20,
                0x30,
                Some(&hex::decode("41424344").unwrap()),
                0
            ),
            hex::decode("001020300441424344").unwrap()
        );
    }

    // -----------------------------------------------------------------------
    // Case 4s — data + short Le
    // -----------------------------------------------------------------------

    #[test]
    fn case4s_data_ne_0xa0() {
        assert_eq!(
            apdu(
                0x00,
                0x10,
                0x20,
                0x30,
                Some(&hex::decode("0102030405060708").unwrap()),
                0xA0
            ),
            hex::decode("00102030080102030405060708A0").unwrap()
        );
    }

    #[test]
    fn case4s_data_ne_256_encoded_as_0x00() {
        // ne = 256, data fits in short form (8 bytes)
        assert_eq!(
            apdu(
                0x00,
                0x10,
                0x20,
                0x30,
                Some(&hex::decode("0102030405060708").unwrap()),
                256
            ),
            hex::decode("0010203008010203040506070800").unwrap()
        );
    }

    #[test]
    fn case4s_4_byte_data_ne_0x80() {
        assert_eq!(
            apdu(
                0x00,
                0x10,
                0x20,
                0x30,
                Some(&hex::decode("41424344").unwrap()),
                0x80
            ),
            hex::decode("00102030044142434480").unwrap()
        );
    }

    // -----------------------------------------------------------------------
    // Case 4e — data + extended Le  (ne > 256 forces extended)
    // -----------------------------------------------------------------------

    #[test]
    fn case4e_data_ne_0x0180() {
        assert_eq!(
            apdu(
                0x00,
                0x10,
                0x20,
                0x30,
                Some(&hex::decode("0102030405060708").unwrap()),
                0x0180
            ),
            hex::decode("0010203000000801020304050607080180").unwrap()
        );
    }

    #[test]
    fn case4e_data_ne_65536_encoded_as_0x0000() {
        assert_eq!(
            apdu(
                0x00,
                0x10,
                0x20,
                0x30,
                Some(&hex::decode("0102030405060708").unwrap()),
                65536
            ),
            hex::decode("0010203000000801020304050607080000").unwrap()
        );
    }

    #[test]
    fn case4e_4_byte_data_ne_0x180() {
        // ne > 256 forces extended even with short data
        assert_eq!(
            apdu(
                0x00,
                0x10,
                0x20,
                0x30,
                Some(&hex::decode("41424344").unwrap()),
                0x180
            ),
            hex::decode("00102030000004414243440180").unwrap()
        );
    }

    // -----------------------------------------------------------------------
    // Case 3e / 4e — data > 255 bytes forces extended Lc
    // -----------------------------------------------------------------------

    #[test]
    fn case3e_256_byte_data_no_le() {
        let data = vec![0xAAu8; 256];
        let mut expected = hex::decode("00102030000100").unwrap(); // header + extended Lc (256)
        expected.extend_from_slice(&data);
        assert_eq!(apdu(0x00, 0x10, 0x20, 0x30, Some(&data), 0), expected);
    }

    #[test]
    fn case4e_256_byte_data_ne_0xa0() {
        let data = vec![0xAAu8; 256];
        let mut expected = hex::decode("00102030000100").unwrap();
        expected.extend_from_slice(&data);
        expected.extend_from_slice(&hex::decode("00A0").unwrap()); // extended Le
        assert_eq!(apdu(0x00, 0x10, 0x20, 0x30, Some(&data), 0xA0), expected);
    }

    #[test]
    fn case4e_256_byte_data_ne_256() {
        let data = vec![0xAAu8; 256];
        let mut expected = hex::decode("00102030000100").unwrap();
        expected.extend_from_slice(&data);
        // In extended form 256 is the literal 0x0100 (0x0000 would mean 65536).
        expected.extend_from_slice(&[0x01, 0x00]);
        assert_eq!(apdu(0x00, 0x10, 0x20, 0x30, Some(&data), 256), expected);
    }

    #[test]
    fn case4e_256_byte_data_ne_0x0180() {
        let data = vec![0xAAu8; 256];
        let mut expected = hex::decode("00102030000100").unwrap();
        expected.extend_from_slice(&data);
        expected.extend_from_slice(&[0x01, 0x80]);
        assert_eq!(apdu(0x00, 0x10, 0x20, 0x30, Some(&data), 0x0180), expected);
    }

    #[test]
    fn case4e_256_byte_data_ne_65536() {
        let data = vec![0xAAu8; 256];
        let mut expected = hex::decode("00102030000100").unwrap();
        expected.extend_from_slice(&data);
        expected.extend_from_slice(&[0x00, 0x00]); // 65536 → 0x0000
        assert_eq!(apdu(0x00, 0x10, 0x20, 0x30, Some(&data), 65536), expected);
    }

    // -----------------------------------------------------------------------
    // None vs empty slice (both treated as no-data)
    // -----------------------------------------------------------------------

    #[test]
    fn none_and_empty_data_equivalent() {
        // Both None and Some([]) should produce Case 1 output
        let v1 = CommandApdu::new(0x00, 0x10, 0x20, 0x30, None, 0)
            .unwrap()
            .to_bytes();
        let v2 = CommandApdu::new(0x00, 0x10, 0x20, 0x30, Some(vec![]), 0)
            .unwrap()
            .to_bytes();
        assert_eq!(v1, v2);
        assert_eq!(v1, hex::decode("00102030").unwrap());
    }

    // -----------------------------------------------------------------------
    // Validation
    // -----------------------------------------------------------------------

    #[test]
    fn ne_too_large_errors() {
        assert_eq!(
            CommandApdu::new(0x00, 0x00, 0x00, 0x00, None, 65537).unwrap_err(),
            CommandApduError::InvalidNe(65537)
        );
    }

    #[test]
    fn data_too_long_errors() {
        let big_data = vec![0u8; 0x10000]; // 65536 bytes
        assert!(matches!(
            CommandApdu::new(0x00, 0x00, 0x00, 0x00, Some(big_data), 0).unwrap_err(),
            CommandApduError::DataTooLong(_)
        ));
    }

    // -----------------------------------------------------------------------
    // raw_header
    // -----------------------------------------------------------------------

    #[test]
    fn raw_header() {
        let apdu = CommandApdu::new(0x00, 0x10, 0x20, 0x30, None, 0).unwrap();
        assert_eq!(apdu.raw_header(), [0x00, 0x10, 0x20, 0x30]);
    }

    // -----------------------------------------------------------------------
    // Display
    // -----------------------------------------------------------------------

    #[test]
    fn display_no_data() {
        let apdu = CommandApdu::new(0x00, 0xA4, 0x04, 0x00, None, 0).unwrap();
        let s = apdu.to_string();
        assert!(s.starts_with("C-APDU("));
        assert!(s.contains("CLA:00"));
        assert!(s.contains("INS:A4"));
    }
}