tsp-ltv 0.1.0

Shared timestamping (RFC 3161) and long-term validation (OCSP, CRL, trust stores) infrastructure for AdES signatures
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
//! Shared DER/ASN.1 parsing and encoding utilities.
//!
//! These helpers work directly with raw DER byte slices, providing
//! low-level TLV (Tag-Length-Value) operations used across multiple
//! modules (CRL, OCSP, TSP, X.509 extension parsing).
//!
//! All functions work on `&[u8]` slices and return owned `Vec<u8>`
//! or borrowed sub-slices as appropriate. Error messages include
//! context about what was being parsed.

/// Parse a DER TLV and return `(tag, value_bytes)`.
///
/// Ignores any trailing data after the first complete TLV.
pub fn parse_tlv(data: &[u8]) -> Result<(u8, Vec<u8>), String> {
    let (tag, body, _rest) = parse_tlv_with_rest(data)?;
    Ok((tag, body.to_vec()))
}

/// Parse a DER TLV and return `(tag, value_slice, remaining_bytes)`.
pub fn parse_tlv_with_rest(data: &[u8]) -> Result<(u8, &[u8], &[u8]), String> {
    if data.is_empty() {
        return Err("empty input".into());
    }

    let tag = data[0];
    let (len, header_len) = parse_der_length(&data[1..])?;
    let total_header = 1 + header_len;

    if total_header + len > data.len() {
        return Err(format!(
            "TLV length exceeds data: header={total_header}, len={len}, available={}",
            data.len()
        ));
    }

    let value = &data[total_header..total_header + len];
    let rest = &data[total_header + len..];
    Ok((tag, value, rest))
}

/// Parse DER definite-form length.
///
/// Returns `(length_value, number_of_bytes_consumed)`.
pub fn parse_der_length(data: &[u8]) -> Result<(usize, usize), String> {
    if data.is_empty() {
        return Err("empty length".into());
    }

    let first = data[0];
    if first < 0x80 {
        Ok((first as usize, 1))
    } else if first == 0x80 {
        Err("indefinite length not supported".into())
    } else {
        let num_bytes = (first & 0x7F) as usize;
        if num_bytes > 4 {
            return Err(format!("length too large: {num_bytes} bytes"));
        }
        if 1 + num_bytes > data.len() {
            return Err("insufficient data for length".into());
        }
        let mut len: usize = 0;
        for i in 0..num_bytes {
            len = (len << 8) | (data[1 + i] as usize);
        }
        Ok((len, 1 + num_bytes))
    }
}

/// Encode a DER TLV from tag and value bytes.
pub fn encode_tlv(tag: u8, value: &[u8]) -> Vec<u8> {
    let mut out = Vec::with_capacity(1 + 5 + value.len());
    out.push(tag);
    encode_der_length(&mut out, value.len());
    out.extend_from_slice(value);
    out
}

/// Encode a DER SEQUENCE wrapping the concatenation of `parts`.
pub fn encode_sequence_from_parts(parts: &[&[u8]]) -> Vec<u8> {
    let total_len: usize = parts.iter().map(|p| p.len()).sum();
    let mut body = Vec::with_capacity(total_len);
    for part in parts {
        body.extend_from_slice(part);
    }
    encode_sequence_raw(&body)
}

/// Encode a DER SEQUENCE from a pre-assembled body.
pub fn encode_sequence_raw(body: &[u8]) -> Vec<u8> {
    let mut out = Vec::with_capacity(1 + 5 + body.len());
    out.push(0x30); // SEQUENCE tag
    encode_der_length(&mut out, body.len());
    out.extend_from_slice(body);
    out
}

/// Encode DER definite-form length into `out`.
pub fn encode_der_length(out: &mut Vec<u8>, len: usize) {
    if len < 0x80 {
        out.push(len as u8);
    } else if len <= 0xFF {
        out.push(0x81);
        out.push(len as u8);
    } else if len <= 0xFFFF {
        out.push(0x82);
        out.push((len >> 8) as u8);
        out.push(len as u8);
    } else if len <= 0xFF_FFFF {
        out.push(0x83);
        out.push((len >> 16) as u8);
        out.push((len >> 8) as u8);
        out.push(len as u8);
    } else {
        out.push(0x84);
        out.push((len >> 24) as u8);
        out.push((len >> 16) as u8);
        out.push((len >> 8) as u8);
        out.push(len as u8);
    }
}

/// Encode a non-negative integer as DER INTEGER.
pub fn encode_integer_u64(val: u64) -> Vec<u8> {
    let be_bytes = val.to_be_bytes();
    let start = be_bytes.iter().position(|&b| b != 0).unwrap_or(7);
    let significant = &be_bytes[start..];

    let needs_padding = significant.is_empty() || (significant[0] & 0x80) != 0;
    let mut value_bytes = Vec::with_capacity(significant.len() + 1);
    if needs_padding {
        value_bytes.push(0x00);
    }
    value_bytes.extend_from_slice(significant);

    encode_tlv(0x02, &value_bytes)
}

/// Decode a DER INTEGER body (no tag/length) to `u64`.
///
/// Handles leading zero padding. Truncates values > 64 bits.
pub fn decode_integer_u64(bytes: &[u8]) -> u64 {
    let mut val: u64 = 0;
    for &b in bytes {
        val = (val << 8) | (b as u64);
    }
    val
}

/// Encode a DER BOOLEAN value.
pub fn encode_boolean(val: bool) -> Vec<u8> {
    encode_tlv(0x01, &[if val { 0xFF } else { 0x00 }])
}

/// Extract the raw body bytes of an INTEGER from a DER-encoded integer.
///
/// Strips the leading zero byte used for positive sign encoding.
/// Useful for comparing serial numbers.
pub fn parse_integer_body(body: &[u8]) -> Vec<u8> {
    // Skip leading 0x00 padding used to indicate positive sign
    if body.len() > 1 && body[0] == 0x00 {
        body[1..].to_vec()
    } else {
        body.to_vec()
    }
}

/// Find the first child element with a specific tag inside a SEQUENCE body.
///
/// Searches the immediate children (not recursive) of the given body bytes.
/// Returns the value bytes of the first match, or `None`.
pub fn find_tagged_value<'a>(body: &'a [u8], target_tag: u8) -> Option<&'a [u8]> {
    let mut pos = body;
    while !pos.is_empty() {
        match parse_tlv_with_rest(pos) {
            Ok((tag, value, rest)) => {
                if tag == target_tag {
                    return Some(value);
                }
                pos = rest;
            }
            Err(_) => break,
        }
    }
    None
}

/// Iterate over child TLVs in a body, calling `f` for each `(tag, value, rest)`.
///
/// Stops at the first parse error or when body is exhausted.
pub fn for_each_tlv<F>(body: &[u8], mut f: F)
where
    F: FnMut(u8, &[u8]),
{
    let mut pos = body;
    while !pos.is_empty() {
        match parse_tlv_with_rest(pos) {
            Ok((tag, value, rest)) => {
                f(tag, value);
                pos = rest;
            }
            Err(_) => break,
        }
    }
}

/// Compare two DER INTEGER bodies for equality, ignoring leading zero padding.
pub fn integer_bodies_equal(a: &[u8], b: &[u8]) -> bool {
    parse_integer_body(a) == parse_integer_body(b)
}

/// Parse a DER-encoded GeneralizedTime body (no tag/length) to a chrono DateTime.
///
/// Format: `YYYYMMDDHHMMSSZ` or `YYYYMMDDHHMMSS.fracZ`
pub fn parse_generalized_time(body: &[u8]) -> Result<chrono::DateTime<chrono::Utc>, String> {
    let s =
        std::str::from_utf8(body).map_err(|e| format!("GeneralizedTime: invalid UTF-8: {e}"))?;

    // Strip trailing 'Z'
    let s = s.strip_suffix('Z').unwrap_or(s);

    // Strip fractional seconds if present
    let s = if let Some(dot_pos) = s.find('.') {
        &s[..dot_pos]
    } else {
        s
    };

    if s.len() < 14 {
        return Err(format!("GeneralizedTime too short: {s}"));
    }

    let year: i32 = s[0..4].parse().map_err(|e| format!("year: {e}"))?;
    let month: u32 = s[4..6].parse().map_err(|e| format!("month: {e}"))?;
    let day: u32 = s[6..8].parse().map_err(|e| format!("day: {e}"))?;
    let hour: u32 = s[8..10].parse().map_err(|e| format!("hour: {e}"))?;
    let min: u32 = s[10..12].parse().map_err(|e| format!("minute: {e}"))?;
    let sec: u32 = s[12..14].parse().map_err(|e| format!("second: {e}"))?;

    use chrono::{NaiveDate, NaiveDateTime, NaiveTime, TimeZone};
    let date = NaiveDate::from_ymd_opt(year, month, day)
        .ok_or_else(|| format!("invalid date: {year}-{month}-{day}"))?;
    let time = NaiveTime::from_hms_opt(hour, min, sec)
        .ok_or_else(|| format!("invalid time: {hour}:{min}:{sec}"))?;
    let dt = NaiveDateTime::new(date, time);
    Ok(chrono::Utc.from_utc_datetime(&dt))
}

/// Parse a DER-encoded UTCTime body (no tag/length) to a chrono DateTime.
///
/// Format: `YYMMDDHHMMSSZ`
pub fn parse_utc_time(body: &[u8]) -> Result<chrono::DateTime<chrono::Utc>, String> {
    let s = std::str::from_utf8(body).map_err(|e| format!("UTCTime: invalid UTF-8: {e}"))?;

    let s = s.strip_suffix('Z').unwrap_or(s);

    if s.len() < 12 {
        return Err(format!("UTCTime too short: {s}"));
    }

    let yy: i32 = s[0..2].parse().map_err(|e| format!("year: {e}"))?;
    // RFC 5280 §4.1.2.5.1: values 0-49 → 2000-2049, 50-99 → 1950-1999
    let year = if yy >= 50 { 1900 + yy } else { 2000 + yy };
    let month: u32 = s[2..4].parse().map_err(|e| format!("month: {e}"))?;
    let day: u32 = s[4..6].parse().map_err(|e| format!("day: {e}"))?;
    let hour: u32 = s[6..8].parse().map_err(|e| format!("hour: {e}"))?;
    let min: u32 = s[8..10].parse().map_err(|e| format!("minute: {e}"))?;
    let sec: u32 = s[10..12].parse().map_err(|e| format!("second: {e}"))?;

    use chrono::{NaiveDate, NaiveDateTime, NaiveTime, TimeZone};
    let date = NaiveDate::from_ymd_opt(year, month, day)
        .ok_or_else(|| format!("invalid date: {year}-{month}-{day}"))?;
    let time = NaiveTime::from_hms_opt(hour, min, sec)
        .ok_or_else(|| format!("invalid time: {hour}:{min}:{sec}"))?;
    let dt = NaiveDateTime::new(date, time);
    Ok(chrono::Utc.from_utc_datetime(&dt))
}

/// Parse a time value that could be either UTCTime (tag 0x17) or
/// GeneralizedTime (tag 0x18).
pub fn parse_x509_time(tag: u8, body: &[u8]) -> Result<chrono::DateTime<chrono::Utc>, String> {
    match tag {
        0x17 => parse_utc_time(body),
        0x18 => parse_generalized_time(body),
        _ => Err(format!(
            "expected UTCTime (0x17) or GeneralizedTime (0x18), got 0x{tag:02x}"
        )),
    }
}

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

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

    #[test]
    fn test_tlv_roundtrip() {
        let value = b"hello";
        let encoded = encode_tlv(0x04, value); // OCTET STRING
        let (tag, body) = parse_tlv(&encoded).unwrap();
        assert_eq!(tag, 0x04);
        assert_eq!(body, value);
    }

    #[test]
    fn test_sequence_encoding() {
        let int1 = encode_integer_u64(42);
        let int2 = encode_integer_u64(100);
        let seq = encode_sequence_from_parts(&[&int1, &int2]);
        let (tag, _body) = parse_tlv(&seq).unwrap();
        assert_eq!(tag, 0x30);
    }

    #[test]
    fn test_der_length_roundtrip() {
        for len in [0, 1, 127, 128, 255, 256, 65535, 65536] {
            let mut buf = Vec::new();
            encode_der_length(&mut buf, len);
            let (parsed_len, consumed) = parse_der_length(&buf).unwrap();
            assert_eq!(parsed_len, len, "length roundtrip failed for {len}");
            assert_eq!(consumed, buf.len());
        }
    }

    #[test]
    fn test_encode_decode_integer() {
        assert_eq!(encode_integer_u64(0), vec![0x02, 0x01, 0x00]);
        assert_eq!(encode_integer_u64(1), vec![0x02, 0x01, 0x01]);
        assert_eq!(encode_integer_u64(128), vec![0x02, 0x02, 0x00, 0x80]);

        // Decode back
        assert_eq!(decode_integer_u64(&[0x01]), 1);
        assert_eq!(decode_integer_u64(&[0x00, 0x80]), 128);
    }

    #[test]
    fn test_parse_integer_body_strips_padding() {
        assert_eq!(parse_integer_body(&[0x00, 0x80]), vec![0x80]);
        assert_eq!(parse_integer_body(&[0x42]), vec![0x42]);
        assert_eq!(parse_integer_body(&[0x00, 0x01, 0x02]), vec![0x01, 0x02]);
    }

    #[test]
    fn test_integer_bodies_equal() {
        assert!(integer_bodies_equal(&[0x00, 0x80], &[0x80]));
        assert!(integer_bodies_equal(&[0x42], &[0x42]));
        assert!(!integer_bodies_equal(&[0x42], &[0x43]));
    }

    #[test]
    fn test_find_tagged_value() {
        // Build: SEQUENCE { INTEGER 42, OCTET STRING "hi", BOOLEAN true }
        let int = encode_integer_u64(42);
        let oct = encode_tlv(0x04, b"hi");
        let boolean = encode_boolean(true);
        let mut body = Vec::new();
        body.extend_from_slice(&int);
        body.extend_from_slice(&oct);
        body.extend_from_slice(&boolean);

        // Find OCTET STRING (0x04)
        let found = find_tagged_value(&body, 0x04);
        assert_eq!(found, Some(b"hi".as_slice()));

        // Find BOOLEAN (0x01)
        let found = find_tagged_value(&body, 0x01);
        assert_eq!(found, Some(&[0xFF][..]));

        // Not found
        assert!(find_tagged_value(&body, 0x06).is_none());
    }

    #[test]
    fn test_parse_generalized_time() {
        let dt = parse_generalized_time(b"20260303120000Z").unwrap();
        assert_eq!(dt.to_rfc3339(), "2026-03-03T12:00:00+00:00");

        // With fractional seconds
        let dt = parse_generalized_time(b"20260303120000.5Z").unwrap();
        assert_eq!(dt.to_rfc3339(), "2026-03-03T12:00:00+00:00");
    }

    #[test]
    fn test_parse_utc_time() {
        // 26 → 2026
        let dt = parse_utc_time(b"260303120000Z").unwrap();
        assert_eq!(dt.to_rfc3339(), "2026-03-03T12:00:00+00:00");

        // 99 → 1999
        let dt = parse_utc_time(b"990101000000Z").unwrap();
        assert_eq!(dt.to_rfc3339(), "1999-01-01T00:00:00+00:00");
    }

    #[test]
    fn test_parse_x509_time() {
        let dt = parse_x509_time(0x17, b"260303120000Z").unwrap();
        assert_eq!(dt.to_rfc3339(), "2026-03-03T12:00:00+00:00");

        let dt = parse_x509_time(0x18, b"20260303120000Z").unwrap();
        assert_eq!(dt.to_rfc3339(), "2026-03-03T12:00:00+00:00");

        assert!(parse_x509_time(0x04, b"whatever").is_err());
    }

    #[test]
    fn test_for_each_tlv() {
        let int = encode_integer_u64(1);
        let oct = encode_tlv(0x04, b"test");
        let mut body = Vec::new();
        body.extend_from_slice(&int);
        body.extend_from_slice(&oct);

        let mut tags = Vec::new();
        for_each_tlv(&body, |tag, _value| {
            tags.push(tag);
        });
        assert_eq!(tags, vec![0x02, 0x04]);
    }

    #[test]
    fn test_empty_input_errors() {
        assert!(parse_tlv(&[]).is_err());
        assert!(parse_tlv_with_rest(&[]).is_err());
        assert!(parse_der_length(&[]).is_err());
    }
}