ifd-jcecard 0.2.0

PC/SC IFD Handler for jcecard virtual OpenPGP and PIV smart card
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
//! BER-TLV Parser
//!
//! Parses BER-TLV (Basic Encoding Rules - Tag Length Value) structures
//! as used in smart card applications. Follows the talktosc crate patterns.

use thiserror::Error;

/// Errors that can occur during TLV parsing
#[derive(Debug, Error, PartialEq, Eq)]
pub enum TLVError {
    #[error("Unexpected end of data while parsing tag")]
    UnexpectedEndTag,

    #[error("Unexpected end of data while parsing length")]
    UnexpectedEndLength,

    #[error("Unexpected end of data while parsing value")]
    UnexpectedEndValue,

    #[error("Invalid length encoding")]
    InvalidLength,

    #[error("Length too large: {0}")]
    LengthTooLarge(usize),

    #[error("Only two bytes for tags supported")]
    TagTooLong,
}

/// A TLV (Tag-Length-Value) structure
///
/// Following the talktosc pattern, this struct contains:
/// - `tag`: The tag value (stored as u32 to support 1-3 byte tags)
/// - `value`: The actual data bytes
/// - `subs`: Child TLVs for composite (constructed) data objects
///
/// # Example
/// ```ignore
/// let tlvs = read_list(&data, true);
/// if let Some(aid) = tlvs[0].get_aid() {
///     println!("AID: {:?}", aid);
/// }
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TLV {
    /// The tag (1-3 bytes encoded as u32)
    pub tag: u32,
    /// The raw value bytes
    pub value: Vec<u8>,
    /// Child TLVs if this is a constructed (composite) tag
    pub subs: Vec<TLV>,
}

impl TLV {
    /// Create a new primitive TLV
    pub fn new(tag: u32, value: Vec<u8>) -> Self {
        Self {
            tag,
            value,
            subs: Vec::new(),
        }
    }

    /// Create a new constructed TLV with children
    pub fn constructed(tag: u32, children: Vec<TLV>) -> Self {
        Self {
            tag,
            value: Vec::new(),
            subs: children,
        }
    }

    /// Get the tag value as u16 (like talktosc)
    pub fn get_t(&self) -> u16 {
        self.tag as u16
    }

    /// Get the length of the value
    pub fn get_l(&self) -> u16 {
        self.value.len() as u16
    }

    /// Get the value as a slice (like talktosc)
    pub fn get_v(&self) -> &[u8] {
        &self.value
    }

    /// Check if this TLV has nested children (composite DO)
    ///
    /// This matches the talktosc `if_recursive()` method.
    pub fn if_recursive(&self) -> bool {
        !self.subs.is_empty()
    }

    /// Check if this is a constructed (container) tag based on the tag bits
    pub fn is_constructed(&self) -> bool {
        let first_byte = self.first_tag_byte();
        (first_byte & 0x20) != 0
    }

    /// Get the first byte of the tag
    fn first_tag_byte(&self) -> u8 {
        if self.tag > 0xFFFF {
            ((self.tag >> 16) & 0xFF) as u8
        } else if self.tag > 0xFF {
            ((self.tag >> 8) & 0xFF) as u8
        } else {
            (self.tag & 0xFF) as u8
        }
    }

    /// Recursively search for a tag (depth-first)
    ///
    /// This matches the talktosc `find_tag()` method.
    pub fn find_tag(&self, tag: u16) -> Option<TLV> {
        if self.tag == tag as u32 {
            return Some(self.clone());
        }
        for tlv in &self.subs {
            if let Some(found) = tlv.find_tag(tag) {
                return Some(found);
            }
        }
        None
    }

    /// Find a tag and return a reference (non-cloning version)
    pub fn find(&self, tag: u32) -> Option<&TLV> {
        if self.tag == tag {
            return Some(self);
        }
        for child in &self.subs {
            if let Some(found) = child.find(tag) {
                return Some(found);
            }
        }
        None
    }

    /// Find a direct child by tag (non-recursive)
    pub fn find_child(&self, tag: u32) -> Option<&TLV> {
        self.subs.iter().find(|c| c.tag == tag)
    }

    // =========================================================================
    // OpenPGP-specific helper methods (following talktosc pattern)
    // =========================================================================

    /// Get the Application Identifier (AID) - tag 0x4F
    pub fn get_aid(&self) -> Option<Vec<u8>> {
        self.find_tag(0x4F).map(|t| t.value.clone())
    }

    /// Get the historical bytes - tag 0x5F52
    pub fn get_historical_bytes(&self) -> Option<Vec<u8>> {
        self.find_tag(0x5F52).map(|t| t.value.clone())
    }

    /// Get the 60 bytes of fingerprints (3 x 20 bytes) - tag 0xC5
    pub fn get_fingerprints(&self) -> Option<Vec<u8>> {
        self.find_tag(0xC5).map(|t| t.value.clone())
    }

    /// Get the cardholder name - tag 0x5B
    pub fn get_name(&self) -> Option<Vec<u8>> {
        self.find_tag(0x5B).map(|t| t.value.clone())
    }

    /// Get the signature algorithm attributes - tag 0xC1
    pub fn get_signature_algo_attributes(&self) -> Option<Vec<u8>> {
        self.find_tag(0xC1).map(|t| t.value.clone())
    }

    /// Get the encryption algorithm attributes - tag 0xC2
    pub fn get_encryption_algo_attributes(&self) -> Option<Vec<u8>> {
        self.find_tag(0xC2).map(|t| t.value.clone())
    }

    /// Get the authentication algorithm attributes - tag 0xC3
    pub fn get_authentication_algo_attributes(&self) -> Option<Vec<u8>> {
        self.find_tag(0xC3).map(|t| t.value.clone())
    }

    /// Get the PIN retry counts - tag 0xC4
    pub fn get_pin_tries(&self) -> Option<Vec<u8>> {
        self.find_tag(0xC4).map(|t| t.value.clone())
    }

    /// Get the key information - tag 0xDE
    pub fn get_key_information(&self) -> Option<Vec<u8>> {
        self.find_tag(0xDE).map(|t| t.value.clone())
    }

    /// Get the number of signatures - tag 0x93
    pub fn get_number_of_signatures(&self) -> Option<Vec<u8>> {
        self.find_tag(0x93).map(|t| t.value.clone())
    }
}

// For backwards compatibility, keep these as aliases
impl TLV {
    /// Alias for subs (backwards compatibility)
    pub fn children(&self) -> &Vec<TLV> {
        &self.subs
    }

    /// Alias for value (backwards compatibility)
    pub fn value(&self) -> &[u8] {
        &self.value
    }
}

/// Parse multiple TLVs from raw bytes
///
/// This is the talktosc-style entry point for parsing.
///
/// # Arguments
/// * `data` - Raw bytes to parse
/// * `recursive` - If true, parse nested TLVs in constructed tags
pub fn read_list(data: &[u8], recursive: bool) -> Vec<TLV> {
    let mut result = Vec::new();
    let mut remaining = data.to_vec();

    while !remaining.is_empty() {
        // Skip filler bytes (0x00, 0xFF)
        if remaining[0] == 0x00 || remaining[0] == 0xFF {
            remaining.remove(0);
            continue;
        }

        match read_single(remaining.clone(), recursive) {
            Ok((tlv, rest)) => {
                result.push(tlv);
                remaining = rest;
            }
            Err(_) => break,
        }
    }

    result
}

/// Parse a single TLV and return it with remaining bytes
///
/// This matches the talktosc `read_single()` function signature.
pub fn read_single(data: Vec<u8>, recursive: bool) -> Result<(TLV, Vec<u8>), String> {
    if data.is_empty() {
        return Err("Empty data".to_string());
    }

    let mut offset = 0;

    // Parse tag
    let (tag, tag_len) = parse_tag(&data[offset..])
        .map_err(|e| e.to_string())?;
    offset += tag_len;

    // Parse length
    if offset >= data.len() {
        return Err("Unexpected end of data while parsing length".to_string());
    }
    let (length, len_len) = parse_length(&data[offset..])
        .map_err(|e| e.to_string())?;
    offset += len_len;

    // Parse value
    if offset + length > data.len() {
        return Err("Unexpected end of data while parsing value".to_string());
    }
    let value = data[offset..offset + length].to_vec();
    offset += length;

    // Check if constructed and parse children
    let first_byte = if tag > 0xFFFF {
        ((tag >> 16) & 0xFF) as u8
    } else if tag > 0xFF {
        ((tag >> 8) & 0xFF) as u8
    } else {
        (tag & 0xFF) as u8
    };

    let subs = if recursive && (first_byte & 0x20) != 0 && !value.is_empty() {
        read_list(&value, true)
    } else {
        Vec::new()
    };

    let remaining = data[offset..].to_vec();
    Ok((TLV { tag, value, subs }, remaining))
}

/// Parse a BER tag (1-3 bytes)
fn parse_tag(data: &[u8]) -> Result<(u32, usize), TLVError> {
    if data.is_empty() {
        return Err(TLVError::UnexpectedEndTag);
    }

    let first = data[0];

    // Check if multi-byte tag (low 5 bits all set)
    if (first & 0x1F) != 0x1F {
        // Single byte tag
        return Ok((first as u32, 1));
    }

    // Multi-byte tag
    if data.len() < 2 {
        return Err(TLVError::UnexpectedEndTag);
    }

    let second = data[1];

    // Check if there's a third byte (bit 7 set means more bytes)
    if (second & 0x80) == 0 {
        // Two byte tag
        let tag = ((first as u32) << 8) | (second as u32);
        return Ok((tag, 2));
    }

    // Three byte tag
    if data.len() < 3 {
        return Err(TLVError::UnexpectedEndTag);
    }

    let third = data[2];
    let tag = ((first as u32) << 16) | ((second as u32) << 8) | (third as u32);
    Ok((tag, 3))
}

/// Parse a BER length (1-5 bytes)
fn parse_length(data: &[u8]) -> Result<(usize, usize), TLVError> {
    if data.is_empty() {
        return Err(TLVError::UnexpectedEndLength);
    }

    let first = data[0];

    // Short form (0-127)
    if (first & 0x80) == 0 {
        return Ok((first as usize, 1));
    }

    // Long form
    let num_bytes = (first & 0x7F) as usize;

    if num_bytes == 0 {
        // Indefinite length - not supported
        return Err(TLVError::InvalidLength);
    }

    if num_bytes > 4 {
        return Err(TLVError::LengthTooLarge(num_bytes));
    }

    if data.len() < 1 + num_bytes {
        return Err(TLVError::UnexpectedEndLength);
    }

    let mut length: usize = 0;
    for i in 0..num_bytes {
        length = (length << 8) | (data[1 + i] as usize);
    }

    Ok((length, 1 + num_bytes))
}

// Keep the TLVParser for backwards compatibility
pub struct TLVParser;

impl TLVParser {
    /// Parse multiple TLVs from raw bytes
    pub fn parse(data: &[u8]) -> Result<Vec<TLV>, TLVError> {
        Ok(read_list(data, true))
    }

    /// Parse a single TLV
    pub fn parse_one(data: &[u8]) -> Result<(TLV, usize), TLVError> {
        match read_single(data.to_vec(), true) {
            Ok((tlv, remaining)) => {
                let consumed = data.len() - remaining.len();
                Ok((tlv, consumed))
            }
            Err(_) => Err(TLVError::InvalidLength), // Map string error to TLVError
        }
    }
}

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

    #[test]
    fn test_simple_tlv() {
        // 4F = tag, 08 = length (8 bytes), D276000124010304 = value (8 bytes)
        let data = hex::decode("4F08D276000124010304").unwrap();
        let tlvs = read_list(&data, true);
        assert_eq!(tlvs.len(), 1);
        assert_eq!(tlvs[0].tag, 0x4F);
        assert_eq!(tlvs[0].get_t(), 0x4F);
        assert_eq!(tlvs[0].value, hex::decode("D276000124010304").unwrap());
    }

    #[test]
    fn test_two_byte_tag() {
        // 5F50 = tag, 0B = length (11 bytes), "example.com" = 11 bytes
        let data = hex::decode("5F500B6578616D706C652E636F6D").unwrap();
        let tlvs = read_list(&data, true);
        assert_eq!(tlvs.len(), 1);
        assert_eq!(tlvs[0].tag, 0x5F50);
        assert_eq!(tlvs[0].value, b"example.com");
    }

    #[test]
    fn test_constructed_tlv() {
        let data = hex::decode("65085B06446F65203C3C").unwrap();
        let tlvs = read_list(&data, true);
        assert_eq!(tlvs.len(), 1);
        assert!(tlvs[0].is_constructed());
        assert!(tlvs[0].if_recursive());
        assert_eq!(tlvs[0].subs.len(), 1);
        assert_eq!(tlvs[0].subs[0].tag, 0x5B);
    }

    #[test]
    fn test_find_tag() {
        // 6E = tag, 0A = length (10 bytes), 4F08D276000124010304 = nested TLV (10 bytes)
        let data = hex::decode("6E0A4F08D276000124010304").unwrap();
        let tlvs = read_list(&data, true);
        let found = tlvs[0].find_tag(0x4F);
        assert!(found.is_some());
        assert_eq!(found.unwrap().tag, 0x4F);
    }

    #[test]
    fn test_get_aid() {
        // 6E = tag, 0A = length (10 bytes), 4F08D276000124010304 = nested TLV (10 bytes)
        let data = hex::decode("6E0A4F08D276000124010304").unwrap();
        let tlvs = read_list(&data, true);
        let aid = tlvs[0].get_aid();
        assert!(aid.is_some());
        assert_eq!(aid.unwrap(), hex::decode("D276000124010304").unwrap());
    }

    #[test]
    fn test_multiple_tlvs() {
        let data = hex::decode("4F0201025B03414243").unwrap();
        let tlvs = read_list(&data, true);
        assert_eq!(tlvs.len(), 2);
        assert_eq!(tlvs[0].tag, 0x4F);
        assert_eq!(tlvs[1].tag, 0x5B);
    }

    #[test]
    fn test_long_length() {
        let mut data = vec![0xC0, 0x81, 0x80];
        data.extend(vec![0x00; 128]);
        let tlvs = read_list(&data, true);
        assert_eq!(tlvs.len(), 1);
        assert_eq!(tlvs[0].value.len(), 128);
    }

    #[test]
    fn test_filler_bytes() {
        // Data with filler bytes at start
        let data = hex::decode("00FF4F020102").unwrap();
        let tlvs = read_list(&data, true);
        assert_eq!(tlvs.len(), 1);
        assert_eq!(tlvs[0].tag, 0x4F);
    }
}