rasn 0.29.0

A safe no_std ASN.1 codec framework.
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
//! The character forms of the time types for every codec that carries time
//! values as text: GeneralizedTime and UTCTime as X.680 (§46, §47) defines
//! them, DATE as `YYYYMMDD` (X.690 §8.26.2), and the canonical forms that
//! X.690 requires of CER and DER (§11.7, §11.8).

use alloc::vec::Vec;

use chrono::{DateTime, Datelike, FixedOffset, NaiveDate, NaiveTime, Timelike, Utc};

const NANOSECONDS_PER_SECOND: u64 = 1_000_000_000;

/// Parses a GeneralizedTime in any form X.680 §46.3 allows: the date and
/// hour, optional minutes and seconds, an optional fraction of the smallest
/// unit given after `.` or `,`, and either `Z`, a `±HHMM` offset or nothing,
/// which is read as UTC.
pub(crate) fn parse_generalized_time(text: &[u8]) -> Option<DateTime<FixedOffset>> {
    parse_generalized_time_form(text, false)
}

/// Parses a GeneralizedTime in the canonical form of X.690 §11.7: the time
/// to the second, a fraction of a second only after `.`, and `Z`.
pub(crate) fn parse_canonical_generalized_time(text: &[u8]) -> Option<DateTime<FixedOffset>> {
    parse_generalized_time_form(text, true)
}

fn parse_generalized_time_form(text: &[u8], canonical: bool) -> Option<DateTime<FixedOffset>> {
    let mut text = Characters(text);
    let date = text.date()?;
    let hour = text.number(2)?;
    let minute = text.optional_number(2);
    let second = minute.and_then(|_| text.optional_number(2));
    if canonical && second.is_none() {
        return None;
    }
    // The fraction is of the smallest unit given.
    let unit_seconds = match (minute, second) {
        (_, Some(_)) => 1,
        (Some(_), None) => 60,
        (None, None) => 3600,
    };
    let fraction = match text.separator(canonical) {
        Some(()) => text.fraction()? * unit_seconds,
        None => 0,
    };
    let zone = match text.zone(canonical)? {
        Some(offset) => offset,
        None if canonical => return None,
        None => FixedOffset::east_opt(0)?,
    };
    if !text.is_empty() {
        return None;
    }
    let time = time_of_day(hour, minute.unwrap_or(0), second.unwrap_or(0), fraction)?;
    date.and_time(time).and_local_timezone(zone).single()
}

/// Parses a UTCTime in any form X.680 §47.3 allows: a two-digit year, the
/// time to the minute or the second, and either `Z` or a `±HHMM` offset.
pub(crate) fn parse_utc_time(text: &[u8]) -> Option<DateTime<Utc>> {
    parse_utc_time_form(text, false)
}

/// Parses a UTCTime in the canonical form of X.690 §11.8: the time to the
/// second and `Z`.
pub(crate) fn parse_canonical_utc_time(text: &[u8]) -> Option<DateTime<Utc>> {
    parse_utc_time_form(text, true)
}

fn parse_utc_time_form(text: &[u8], canonical: bool) -> Option<DateTime<Utc>> {
    let mut text = Characters(text);
    // The century is not given; years from 70 are in the twentieth.
    let year = match text.number(2)? {
        year @ 70.. => 1900 + year,
        year => 2000 + year,
    };
    let month = text.number(2)?;
    let day = text.number(2)?;
    let hour = text.number(2)?;
    let minute = text.number(2)?;
    let second = text.optional_number(2);
    if canonical && second.is_none() {
        return None;
    }
    let zone = text.zone(canonical)??;
    if !text.is_empty() {
        return None;
    }
    let date = NaiveDate::from_ymd_opt(year as i32, month, day)?;
    let time = time_of_day(hour, minute, second.unwrap_or(0), 0)?;
    Some(
        date.and_time(time)
            .and_local_timezone(zone)
            .single()?
            .to_utc(),
    )
}

/// Parses a DATE, `YYYYMMDD`.
pub(crate) fn parse_date(text: &[u8]) -> Option<NaiveDate> {
    let mut text = Characters(text);
    let date = text.date()?;
    text.is_empty().then_some(date)
}

/// The time of day at `hour`, `minute` and `second`, plus `nanoseconds`.
/// A `second` of 60 is a leap second, which `chrono` represents as second
/// 59 with a billion or more nanoseconds.
fn time_of_day(hour: u32, minute: u32, second: u32, nanoseconds: u64) -> Option<NaiveTime> {
    if minute >= 60 {
        return None;
    }
    let (second, leap) = match second {
        60 => (59, NANOSECONDS_PER_SECOND),
        second @ 0..60 => (second, 0),
        _ => return None,
    };
    let seconds = u64::from(hour) * 3600
        + u64::from(minute) * 60
        + u64::from(second)
        + nanoseconds / NANOSECONDS_PER_SECOND;
    let nanoseconds = nanoseconds % NANOSECONDS_PER_SECOND + leap;
    NaiveTime::from_num_seconds_from_midnight_opt(
        u32::try_from(seconds).ok()?,
        u32::try_from(nanoseconds).ok()?,
    )
}

/// The characters of a time value that have not been parsed.
struct Characters<'a>(&'a [u8]);

impl Characters<'_> {
    fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Parses a number of exactly `digits` decimal digits.
    fn number(&mut self, digits: usize) -> Option<u32> {
        let (number, rest) = self.0.split_at_checked(digits)?;
        let mut value = 0u32;
        for &digit in number {
            value = value * 10 + u32::from(digit.checked_sub(b'0').filter(|digit| *digit < 10)?);
        }
        self.0 = rest;
        Some(value)
    }

    /// Parses a number of `digits` decimal digits when the next character
    /// is a digit.
    fn optional_number(&mut self, digits: usize) -> Option<u32> {
        self.0
            .first()
            .is_some_and(u8::is_ascii_digit)
            .then(|| self.number(digits))
            .flatten()
    }

    /// Parses a date, `YYYYMMDD`.
    fn date(&mut self) -> Option<NaiveDate> {
        let year = self.number(4)?;
        let month = self.number(2)?;
        let day = self.number(2)?;
        NaiveDate::from_ymd_opt(year as i32, month, day)
    }

    /// Consumes the separator that starts a fraction: `.`, or `,` as well
    /// unless `canonical`.
    fn separator(&mut self, canonical: bool) -> Option<()> {
        let (&separator, rest) = self.0.split_first()?;
        let allowed = separator == b'.' || (separator == b',' && !canonical);
        allowed.then(|| self.0 = rest)
    }

    /// Parses the digits of a fraction, at least one, as billionths of the
    /// unit they qualify; digits beyond the ninth do not contribute.
    fn fraction(&mut self) -> Option<u64> {
        let digits = self
            .0
            .iter()
            .position(|character| !character.is_ascii_digit())
            .unwrap_or(self.0.len());
        if digits == 0 {
            return None;
        }
        let mut billionths = 0u64;
        for &digit in &self.0[..digits.min(9)] {
            billionths = billionths * 10 + u64::from(digit - b'0');
        }
        billionths *= 10u64.pow(9 - digits.min(9) as u32);
        self.0 = &self.0[digits..];
        Some(billionths)
    }

    /// Parses the time zone: `Z` for UTC, or a `±HHMM` offset unless
    /// `canonical`. Yields `None` when no zone is given, and fails on a
    /// malformed or disallowed one.
    fn zone(&mut self, canonical: bool) -> Option<Option<FixedOffset>> {
        let (&designator, rest) = match self.0.split_first() {
            Some(zone) => zone,
            None => return Some(None),
        };
        self.0 = rest;
        let sign = match designator {
            b'Z' => return Some(Some(FixedOffset::east_opt(0)?)),
            b'+' if !canonical => 1,
            b'-' if !canonical => -1,
            _ => return None,
        };
        let hours = self.number(2)?;
        let minutes = self.number(2)?;
        let seconds = i32::try_from(hours * 3600 + minutes * 60).ok()?;
        Some(Some(FixedOffset::east_opt(sign * seconds)?))
    }
}

/// Appends `value` as a GeneralizedTime in the canonical form: UTC, the time
/// to the second, a fraction of a second only when it is not zero and
/// without trailing zeros, and `Z`.
pub(crate) fn write_generalized_time(out: &mut Vec<u8>, value: &DateTime<FixedOffset>) {
    let value = value.naive_utc();
    write_date(out, &value.date());
    let (second, nanoseconds) = second_and_nanoseconds(value.time());
    write_time(out, value.time(), second);
    if nanoseconds > 0 {
        out.push(b'.');
        let mut fraction = nanoseconds;
        let mut digits = 9;
        while fraction % 10 == 0 {
            fraction /= 10;
            digits -= 1;
        }
        write_decimal(out, fraction, digits);
    }
    out.push(b'Z');
}

/// Appends `value` as a UTCTime in the canonical form: UTC, the year in two
/// digits, the time to the second and `Z`.
pub(crate) fn write_utc_time(out: &mut Vec<u8>, value: &DateTime<Utc>) {
    let value = value.naive_utc();
    write_pair(out, value.year().rem_euclid(100) as u32);
    write_pair(out, value.month());
    write_pair(out, value.day());
    let (second, _) = second_and_nanoseconds(value.time());
    write_time(out, value.time(), second);
    out.push(b'Z');
}

/// Appends `value` as a DATE: the year in four digits, then the month and
/// the day in two digits each.
pub(crate) fn write_date(out: &mut Vec<u8>, value: &NaiveDate) {
    let year = value.year();
    if let Ok(year) = u32::try_from(year)
        && year <= 9999
    {
        write_pair(out, year / 100);
        write_pair(out, year % 100);
    } else {
        // A year outside the four-digit range is not representable; it is
        // written with a sign and as many digits as it needs.
        out.push(if year < 0 { b'-' } else { b'+' });
        write_decimal(out, year.unsigned_abs(), 4);
    }
    write_pair(out, value.month());
    write_pair(out, value.day());
}

/// Appends the hour, minute and `second` of `value` in two digits each.
fn write_time(out: &mut Vec<u8>, value: NaiveTime, second: u32) {
    write_pair(out, value.hour());
    write_pair(out, value.minute());
    write_pair(out, second);
}

/// Appends `value`, which is below 100, in two digits.
#[inline]
fn write_pair(out: &mut Vec<u8>, value: u32) {
    debug_assert!(value < 100);
    out.extend_from_slice(&[b'0' + (value / 10) as u8, b'0' + (value % 10) as u8]);
}

/// The second and the nanoseconds within it. `chrono` represents a leap
/// second as second 59 with a billion or more nanoseconds; it is written
/// as second 60.
fn second_and_nanoseconds(value: NaiveTime) -> (u32, u32) {
    const NANOSECONDS_PER_SECOND: u32 = 1_000_000_000;
    let nanoseconds = value.nanosecond();
    if nanoseconds >= NANOSECONDS_PER_SECOND {
        (value.second() + 1, nanoseconds - NANOSECONDS_PER_SECOND)
    } else {
        (value.second(), nanoseconds)
    }
}

/// Appends `value` in decimal, zero-padded to at least `width` digits.
fn write_decimal(out: &mut Vec<u8>, mut value: u32, width: usize) {
    let digits = value.checked_ilog10().map_or(1, |log| log as usize + 1);
    let start = out.len();
    out.resize(start + width.max(digits), b'0');
    for digit in out[start..].iter_mut().rev() {
        *digit = b'0' + (value % 10) as u8;
        value /= 10;
        if value == 0 {
            break;
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::string::String;
    use chrono::TimeZone;

    fn written(write: impl FnOnce(&mut Vec<u8>)) -> String {
        let mut out = Vec::new();
        write(&mut out);
        String::from_utf8(out).unwrap()
    }

    fn generalized_time(value: DateTime<FixedOffset>) -> String {
        written(|out| write_generalized_time(out, &value))
    }

    fn utc_time(value: DateTime<Utc>) -> String {
        written(|out| write_utc_time(out, &value))
    }

    fn date(year: i32, month: u32, day: u32) -> String {
        written(|out| write_date(out, &NaiveDate::from_ymd_opt(year, month, day).unwrap()))
    }

    /// Midnight on 2020-01-01 with the given nanoseconds, which may denote a
    /// leap second.
    fn with_nanoseconds(nanoseconds: u32) -> DateTime<FixedOffset> {
        Utc.with_ymd_and_hms(2020, 1, 1, 0, 0, 59)
            .unwrap()
            .with_nanosecond(nanoseconds)
            .unwrap()
            .fixed_offset()
    }

    #[test]
    fn generalized_time_is_written_in_utc() {
        let value = FixedOffset::east_opt(5 * 3600)
            .unwrap()
            .with_ymd_and_hms(2018, 6, 13, 3, 1, 58)
            .unwrap();
        assert_eq!(generalized_time(value), "20180612220158Z");
    }

    #[test]
    fn generalized_time_omits_a_zero_fraction() {
        assert_eq!(generalized_time(with_nanoseconds(0)), "20200101000059Z");
    }

    #[test]
    fn generalized_time_fraction_drops_trailing_zeros() {
        assert_eq!(
            generalized_time(with_nanoseconds(342_000_000)),
            "20200101000059.342Z"
        );
    }

    #[test]
    fn generalized_time_fraction_keeps_leading_zeros() {
        assert_eq!(
            generalized_time(with_nanoseconds(500_000)),
            "20200101000059.0005Z"
        );
    }

    #[test]
    fn generalized_time_fraction_can_need_all_nine_digits() {
        assert_eq!(
            generalized_time(with_nanoseconds(1)),
            "20200101000059.000000001Z"
        );
    }

    #[test]
    fn generalized_time_leap_second() {
        assert_eq!(
            generalized_time(with_nanoseconds(1_000_000_000)),
            "20200101000060Z"
        );
        assert_eq!(
            generalized_time(with_nanoseconds(1_500_000_000)),
            "20200101000060.5Z"
        );
    }

    #[test]
    fn utc_time_takes_the_last_two_digits_of_the_year() {
        assert_eq!(
            utc_time(Utc.with_ymd_and_hms(2018, 1, 22, 13, 29, 0).unwrap()),
            "180122132900Z"
        );
        assert_eq!(
            utc_time(Utc.with_ymd_and_hms(2000, 2, 29, 0, 0, 0).unwrap()),
            "000229000000Z"
        );
    }

    #[test]
    fn utc_time_year_before_the_common_era() {
        assert_eq!(
            utc_time(Utc.with_ymd_and_hms(-5, 3, 4, 5, 6, 7).unwrap()),
            "950304050607Z"
        );
    }

    #[test]
    fn utc_time_leap_second_has_no_fraction() {
        assert_eq!(
            utc_time(with_nanoseconds(1_500_000_000).to_utc()),
            "200101000060Z"
        );
    }

    #[test]
    fn date_fields_are_zero_padded() {
        assert_eq!(date(1, 2, 3), "00010203");
    }

    #[test]
    fn date_year_at_the_ends_of_the_four_digit_range() {
        assert_eq!(date(0, 12, 31), "00001231");
        assert_eq!(date(9999, 12, 31), "99991231");
    }

    #[test]
    fn date_year_outside_the_four_digit_range_is_signed() {
        assert_eq!(date(10000, 1, 1), "+100000101");
        assert_eq!(date(-1, 1, 1), "-00010101");
    }

    fn utc(year: i32, month: u32, day: u32, hour: u32, minute: u32, second: u32) -> DateTime<Utc> {
        Utc.with_ymd_and_hms(year, month, day, hour, minute, second)
            .unwrap()
    }

    #[test]
    fn parses_generalized_time_to_the_hour_minute_or_second() {
        let expected = utc(2018, 6, 13, 11, 0, 0).fixed_offset();
        assert_eq!(parse_generalized_time(b"2018061311"), Some(expected));
        assert_eq!(parse_generalized_time(b"201806131100"), Some(expected));
        assert_eq!(parse_generalized_time(b"20180613110000Z"), Some(expected));
    }

    #[test]
    fn parses_generalized_time_fraction_of_the_smallest_unit() {
        let half_past_twelve = utc(2018, 6, 13, 12, 30, 0).fixed_offset();
        assert_eq!(
            parse_generalized_time(b"2018061312.5"),
            Some(half_past_twelve)
        );
        assert_eq!(
            parse_generalized_time(b"201806131229,5"),
            Some(half_past_twelve - chrono::Duration::seconds(30))
        );
        assert_eq!(
            parse_generalized_time(b"20180613122959.1234567891Z"),
            Some(
                utc(2018, 6, 13, 12, 29, 59)
                    .with_nanosecond(123_456_789)
                    .unwrap()
                    .fixed_offset()
            )
        );
    }

    #[test]
    fn parses_generalized_time_offset() {
        let offset = FixedOffset::west_opt(5 * 3600).unwrap();
        let parsed = parse_generalized_time(b"20230122130000-0500").unwrap();
        assert_eq!(parsed.offset(), &offset);
        assert_eq!(parsed, utc(2023, 1, 22, 18, 0, 0));
    }

    #[test]
    fn parses_generalized_time_leap_second() {
        let leap_second = NaiveDate::from_ymd_opt(2016, 12, 31)
            .unwrap()
            .and_hms_nano_opt(23, 59, 59, 1_500_000_000)
            .unwrap()
            .and_utc()
            .fixed_offset();
        assert_eq!(
            parse_generalized_time(b"20161231235960.5Z"),
            Some(leap_second)
        );
    }

    #[test]
    fn rejects_malformed_generalized_time() {
        for malformed in [
            &b"20180613"[..],     // no hour
            b"2018061324",        // hour out of range
            b"201806131160",      // minute out of range
            b"20180613115961",    // second out of range
            b"20180613115959.",   // fraction without digits
            b"20180613115959+05", // incomplete offset
            b"20180613115959Z1",  // trailing characters
            b"2018O613115959Z",   // not a digit
        ] {
            assert_eq!(parse_generalized_time(malformed), None, "{malformed:?}");
        }
    }

    #[test]
    fn canonical_generalized_time_requires_seconds_a_dot_and_utc() {
        assert_eq!(
            parse_canonical_generalized_time(b"20180613110000.5Z"),
            Some(
                utc(2018, 6, 13, 11, 0, 0)
                    .with_nanosecond(500_000_000)
                    .unwrap()
                    .fixed_offset()
            )
        );
        for lenient_only in [
            &b"201806131100Z"[..],
            b"20180613110000,5Z",
            b"20180613110000",
            b"20180613110000+0000",
        ] {
            assert!(
                parse_generalized_time(lenient_only).is_some(),
                "{lenient_only:?}"
            );
            assert_eq!(
                parse_canonical_generalized_time(lenient_only),
                None,
                "{lenient_only:?}"
            );
        }
    }

    #[test]
    fn parses_utc_time_to_the_minute_or_second_in_utc() {
        let expected = utc(2018, 1, 22, 13, 29, 0);
        assert_eq!(parse_utc_time(b"1801221329Z"), Some(expected));
        assert_eq!(parse_utc_time(b"180122132900Z"), Some(expected));
        assert_eq!(parse_utc_time(b"180122082900-0500"), Some(expected));
    }

    #[test]
    fn utc_time_years_from_seventy_are_in_the_twentieth_century() {
        assert_eq!(
            parse_utc_time(b"700101000000Z"),
            Some(utc(1970, 1, 1, 0, 0, 0))
        );
        assert_eq!(
            parse_utc_time(b"690101000000Z"),
            Some(utc(2069, 1, 1, 0, 0, 0))
        );
    }

    #[test]
    fn utc_time_requires_a_zone() {
        assert_eq!(parse_utc_time(b"180122132900"), None);
    }

    #[test]
    fn canonical_utc_time_requires_seconds_and_utc() {
        assert_eq!(
            parse_canonical_utc_time(b"180122132900Z"),
            Some(utc(2018, 1, 22, 13, 29, 0))
        );
        assert_eq!(parse_canonical_utc_time(b"1801221329Z"), None);
        assert_eq!(parse_canonical_utc_time(b"180122082900-0500"), None);
    }

    #[test]
    fn parses_date() {
        assert_eq!(
            parse_date(b"20121221"),
            NaiveDate::from_ymd_opt(2012, 12, 21)
        );
        assert_eq!(parse_date(b"20120230"), None);
        assert_eq!(parse_date(b"2012122"), None);
        assert_eq!(parse_date(b"20121221Z"), None);
    }
}