sequoia-openpgp 2.2.0

OpenPGP data types and associated machinery
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
use std::fmt;
use std::ops::{Deref, Index, IndexMut};

use crate::{
    Error,
    Result,
    types::Timestamp,
    types::Duration,
};

// A `const fn` function can only use a subset of Rust's
// functionality.  The subset is growing, but we restrict ourselves to
// only use `const fn` functionality that is available in Debian
// stable, which, as of 2024, is rustc version 1.63.0.  This
// requires a bit of creativity.
#[derive(Debug, Clone)]
pub(super) enum VecOrSlice<'a, T> {
    Vec(Vec<T>),
    Slice(&'a [T]),
}

// Make a `VecOrSlice` act like a `Vec`.
impl<'a, T> VecOrSlice<'a, T> {
    // Returns an empty `VecOrSlice`.
    const fn empty() -> Self {
        VecOrSlice::Vec(Vec::new())
    }

    // Like `Vec::get`.
    fn get(&self, i: usize) -> Option<&T> {
        match self {
            VecOrSlice::Vec(v) => v.get(i),
            VecOrSlice::Slice(s) => s.get(i),
        }
    }

    // Like `Vec::len`.
    fn len(&self) -> usize {
        match self {
            VecOrSlice::Vec(v) => v.len(),
            VecOrSlice::Slice(s) => s.len(),
        }
    }

    // Like `Vec::resize`.
    fn resize(&mut self, size: usize, value: T)
        where T: Clone
    {
        let v = self.as_mut();
        v.resize(size, value);
    }

    pub(super) fn as_mut(&mut self) -> &mut Vec<T>
        where T: Clone
    {
        let v: Vec<T> = match self {
            VecOrSlice::Vec(ref mut v) => std::mem::take(v),
            VecOrSlice::Slice(s) => s.to_vec(),
        };

        *self = VecOrSlice::Vec(v);
        if let VecOrSlice::Vec(ref mut v) = self {
            v
        } else {
            unreachable!()
        }
    }
}

impl<'a, T> Deref for VecOrSlice<'a, T> {
    type Target = [T];

    fn deref(&self) -> &Self::Target {
        match self {
            VecOrSlice::Vec(ref v) => &v[..],
            VecOrSlice::Slice(s) => s,
        }
    }
}

impl<'a, T> Index<usize> for VecOrSlice<'a, T> {
    type Output = T;

    fn index(&self, i: usize) -> &T {
        match self {
            VecOrSlice::Vec(v) => &v[i],
            VecOrSlice::Slice(s) => &s[i],
        }
    }
}

impl<'a, T> IndexMut<usize> for VecOrSlice<'a, T>
    where T: Clone
{
    fn index_mut(&mut self, i: usize) -> &mut T {
        if let VecOrSlice::Slice(s) = self {
            *self = VecOrSlice::Vec(s.to_vec());
        };

        match self {
            VecOrSlice::Vec(v) => &mut v[i],
            VecOrSlice::Slice(_) => unreachable!(),
        }
    }
}

/// A given algorithm may be considered: completely broken, safe, or
/// too weak to be used after a certain time.
#[derive(Debug, Clone)]
pub(super) struct CutoffList<A> {
    // Indexed by `A as u8`.
    //
    // A value of `None` means that no vulnerabilities are known.
    //
    // Note: we use `u64` and not `SystemTime`, because there is no
    // way to construct a `SystemTime` in a `const fn`.
    pub(super) cutoffs: VecOrSlice<'static, Option<Timestamp>>,

    pub(super) _a: std::marker::PhantomData<A>,
}

pub(super) const REJECT : Option<Timestamp> = Some(Timestamp::UNIX_EPOCH);
pub(super) const ACCEPT : Option<Timestamp> = None;

pub(super) const DEFAULT_POLICY : Option<Timestamp> = REJECT;

impl<A> Default for CutoffList<A> {
    fn default() -> Self {
        Self::reject_all()
    }
}

impl<A> CutoffList<A> {
    // Rejects all algorithms.
    pub(super) const fn reject_all() -> Self {
        Self {
            cutoffs: VecOrSlice::empty(),
            _a: std::marker::PhantomData,
        }
    }
}

impl<A> CutoffList<A>
    where u8: From<A>,
          A: fmt::Display,
          A: std::clone::Clone
{
    // Sets a cutoff time.
    pub(super) fn set(&mut self, a: A, cutoff: Option<Timestamp>) {
        let i : u8 = a.into();
        let i : usize = i.into();

        if i >= self.cutoffs.len() {
            // We reject by default.
            self.cutoffs.resize(i + 1, DEFAULT_POLICY);
        }
        self.cutoffs[i] = cutoff;
    }

    // Returns the cutoff time for algorithm `a`.
    #[inline]
    pub(super) fn cutoff(&self, a: A) -> Option<Timestamp> {
        let i : u8 = a.into();
        *self.cutoffs.get(i as usize).unwrap_or(&DEFAULT_POLICY)
    }

    // Checks whether the `a` is safe to use at time `time`.
    //
    // `tolerance` is added to the cutoff time.
    #[inline]
    pub(super) fn check(&self, a: A, time: Timestamp,
                        tolerance: Option<Duration>)
        -> Result<()>
    {
        if let Some(cutoff) = self.cutoff(a.clone()) {
            let cutoff = cutoff
                .checked_add(tolerance.unwrap_or_else(|| Duration::seconds(0)))
                .unwrap_or(Timestamp::MAX);
            if time >= cutoff {
                Err(Error::PolicyViolation(
                    a.to_string(), Some(cutoff.into())).into())
            } else {
                Ok(())
            }
        } else {
            // None => always secure.
            Ok(())
        }
    }
}

macro_rules! a_cutoff_list {
    ($name:ident, $algo:ty, $values_count:expr, $values:expr) => {
        // It would be nicer to just have a `CutoffList` and store the
        // default as a `VecOrSlice::Slice`.  Unfortunately, we can't
        // create a slice in a `const fn`, so that doesn't work.
        //
        // To work around that issue, we store the array in the
        // wrapper type, and remember if we are using it or a custom
        // version.
        #[derive(Debug, Clone)]
        enum $name {
            Default(),
            Custom(CutoffList<$algo>),
        }

        #[allow(unused)]
        impl $name {
            const DEFAULTS : [ Option<Timestamp>; $values_count ] = $values;

            // Turn the `Foo::Default` into a `Foo::Custom`, if
            // necessary.
            fn force(&mut self) -> &mut CutoffList<$algo> {
                use crate::policy::cutofflist::VecOrSlice;

                if let $name::Default() = self {
                    *self = $name::Custom(CutoffList {
                        cutoffs: VecOrSlice::Vec(Self::DEFAULTS.to_vec()),
                        _a: std::marker::PhantomData,
                    });
                }

                match self {
                    $name::Custom(ref mut l) => l,
                    _ => unreachable!(),
                }
            }

            fn set(&mut self, a: $algo, cutoff: Option<Timestamp>) {
                self.force().set(a, cutoff)
            }

            // Reset the cutoff list to its defaults.
            fn defaults(&mut self) {
                *self = Self::Default();
            }

            fn reject_all(&mut self) {
                *self = Self::Custom(CutoffList::reject_all());
            }

            fn cutoff(&self, a: $algo) -> Option<Timestamp> {
                use crate::policy::cutofflist::DEFAULT_POLICY;

                match self {
                    $name::Default() => {
                        let i : u8 = a.into();
                        let i : usize = i.into();

                        if i >= Self::DEFAULTS.len() {
                            DEFAULT_POLICY
                        } else {
                            Self::DEFAULTS[i]
                        }
                    }
                    $name::Custom(ref l) => l.cutoff(a),
                }
            }

            fn check(&self, a: $algo, time: Timestamp, d: Option<types::Duration>)
                -> Result<()>
            {
                use crate::policy::cutofflist::VecOrSlice;

                match self {
                    $name::Default() => {
                        // Convert the default to a `CutoffList` on
                        // the fly to avoid duplicating
                        // `CutoffList::check`.
                        CutoffList {
                            cutoffs: VecOrSlice::Slice(&Self::DEFAULTS[..]),
                            _a: std::marker::PhantomData,
                        }.check(a, time, d)
                    }

                    $name::Custom(ref l) => l.check(a, time, d),
                }
            }
        }
    }
}

/// A data structure may have multiple versions.  For instance, there
/// are multiple versions of packets.  Each version of a given packet
/// may have different security properties.
#[derive(Debug, Clone)]
pub(super) struct VersionedCutoffList<A> where A: 'static {
    // Indexed by `A as u8`.
    //
    // A value of `None` means that no vulnerabilities are known.
    //
    // Note: we use `u64` and not `SystemTime`, because there is no
    // way to construct a `SystemTime` in a `const fn`.
    pub(super) unversioned_cutoffs: VecOrSlice<'static, Option<Timestamp>>,

    // The content is: (algo, version, policy).
    pub(super) versioned_cutoffs:
        VecOrSlice<'static, (A, u8, Option<Timestamp>)>,

    pub(super) _a: std::marker::PhantomData<A>,
}

impl<A> Default for VersionedCutoffList<A> {
    fn default() -> Self {
        Self::reject_all()
    }
}

impl<A> VersionedCutoffList<A> {
    // Rejects all algorithms.
    pub(super) const fn reject_all() -> Self {
        Self {
            unversioned_cutoffs: VecOrSlice::empty(),
            versioned_cutoffs: VecOrSlice::empty(),
            _a: std::marker::PhantomData,
        }
    }
}

impl<A> VersionedCutoffList<A>
    where u8: From<A>,
          A: fmt::Display,
          A: std::clone::Clone,
          A: Eq,
          A: Ord,
{
    // versioned_cutoffs must be sorted and deduplicated.  Make sure
    // it is so.
    pub(super) fn assert_sorted(&self) {
        if cfg!(debug_assertions) || cfg!(test) {
            for window in self.versioned_cutoffs.windows(2) {
                let a = &window[0];
                let b = &window[1];

                // Sorted, no duplicates.
                assert!((&a.0, a.1) < (&b.0, b.1));
            }
        }
    }

    // Sets a cutoff time for version `version` of algorithm `algo`.
    pub(super) fn set_versioned(&mut self,
                                algo: A, version: u8,
                                cutoff: Option<Timestamp>)
    {
        self.assert_sorted();
        let cutofflist = self.versioned_cutoffs.as_mut();
        match cutofflist.binary_search_by(|(a, v, _)| {
            algo.cmp(a).then(version.cmp(v)).reverse()
        }) {
            Ok(i) => {
                // Replace.
                cutofflist[i] = (algo, version, cutoff);
            }
            Err(i) => {
                // Insert.
                cutofflist.insert(i, (algo, version, cutoff));
            }
        };
        self.assert_sorted();
    }

    // Sets a cutoff time for algorithm `algo`.
    pub(super) fn set_unversioned(&mut self, algo: A,
                                  cutoff: Option<Timestamp>)
    {
        let i: u8 = algo.into();
        let i: usize = i.into();

        if i >= self.unversioned_cutoffs.len() {
            // We reject by default.
            self.unversioned_cutoffs.resize(i + 1, DEFAULT_POLICY);
        }
        self.unversioned_cutoffs[i] = cutoff;
    }

    // Returns the cutoff time for version `version` of algorithm `algo`.
    #[inline]
    pub(super) fn cutoff(&self, algo: A, version: u8) -> Option<Timestamp> {
        self.assert_sorted();
        match self.versioned_cutoffs.binary_search_by(|(a, v, _)| {
            algo.cmp(a).then(version.cmp(v)).reverse()
        }) {
            Ok(i) => {
                self.versioned_cutoffs[i].2
            }
            Err(_loc) => {
                // Fallback to the unversioned cutoff list.
                *self.unversioned_cutoffs.get(u8::from(algo) as usize)
                    .unwrap_or(&DEFAULT_POLICY)
            }
        }
    }

    // Checks whether version `version` of the algorithm `algo` is safe
    // to use at time `time`.
    //
    // `tolerance` is added to the cutoff time.
    #[inline]
    pub(super) fn check(&self, algo: A, version: u8, time: Timestamp,
                        tolerance: Option<Duration>)
        -> Result<()>
    {
        if let Some(cutoff) = self.cutoff(algo.clone(), version) {
            let cutoff = cutoff
                .checked_add(tolerance.unwrap_or_else(|| Duration::seconds(0)))
                .unwrap_or(Timestamp::MAX);
            if time >= cutoff {
                Err(Error::PolicyViolation(
                    format!("{} v{}", algo, version),
                    Some(cutoff.into())).into())
            } else {
                Ok(())
            }
        } else {
            // None => always secure.
            Ok(())
        }
    }
}

macro_rules! a_versioned_cutoff_list {
    ($name:ident, $algo:ty,
     // A slice indexed by the algorithm.
     $unversioned_values_count: expr, $unversioned_values: expr,
     // A slice of the form: [ (algo, version, cutoff), ... ]
     //
     // Note: the values must be sorted and (algo, version) must be
     // unique!
     $versioned_values_count:expr, $versioned_values:expr) => {
        // It would be nicer to just have a `CutoffList` and store the
        // default as a `VecOrSlice::Slice`.  Unfortunately, we can't
        // create a slice in a `const fn`, so that doesn't work.
        //
        // To work around that issue, we store the array in the
        // wrapper type, and remember if we are using it or a custom
        // version.
        #[derive(Debug, Clone)]
        enum $name {
            Default(),
            Custom(VersionedCutoffList<$algo>),
        }

        impl std::ops::Deref for $name {
            type Target = VersionedCutoffList<$algo>;

            fn deref(&self) -> &Self::Target {
                match self {
                    $name::Default() => &Self::DEFAULT,
                    $name::Custom(l) => l,
                }
            }
        }

        #[allow(unused)]
        impl $name {
            const VERSIONED_DEFAULTS:
                [ ($algo, u8, Option<Timestamp>); $versioned_values_count ]
                = $versioned_values;
            const UNVERSIONED_DEFAULTS:
                [ Option<Timestamp>; $unversioned_values_count ]
                = $unversioned_values;

            const DEFAULT: VersionedCutoffList<$algo> = VersionedCutoffList {
                versioned_cutoffs:
                    crate::policy::cutofflist::VecOrSlice::Slice(
                        &Self::VERSIONED_DEFAULTS),
                unversioned_cutoffs:
                    crate::policy::cutofflist::VecOrSlice::Slice(
                        &Self::UNVERSIONED_DEFAULTS),
                _a: std::marker::PhantomData,
            };

            // Turn the `Foo::Default` into a `Foo::Custom`, if
            // necessary, to allow modification.
            fn force(&mut self) -> &mut VersionedCutoffList<$algo> {
                use crate::policy::cutofflist::VecOrSlice;

                if let $name::Default() = self {
                    *self = Self::Custom($name::DEFAULT);
                }

                match self {
                    $name::Custom(ref mut l) => l,
                    _ => unreachable!(),
                }
            }

            // Set the cutoff for the specified version of the
            // specified algorithm.
            fn set_versioned(&mut self, algo: $algo, version: u8,
                             cutoff: Option<Timestamp>)
            {
                self.force().set_versioned(algo, version, cutoff)
            }

            // Sets the cutoff for the specified algorithm independent
            // of its version.
            fn set_unversioned(&mut self, algo: $algo,
                               cutoff: Option<Timestamp>)
            {
                // Clear any versioned cutoffs.
                let l = self.force();
                l.versioned_cutoffs.as_mut().retain(|(a, _v, _c)| {
                    &algo != a
                });

                l.set_unversioned(algo, cutoff)
            }

            // Resets the cutoff list to its defaults.
            fn defaults(&mut self) {
                *self = Self::Default();
            }

            // Causes the cutoff list to reject everything.
            fn reject_all(&mut self) {
                *self = Self::Custom(VersionedCutoffList::reject_all());
            }

            // Returns the cutoff for the specified version of the
            // specified algorithm.
            //
            // This first considers the versioned cutoff list.  If
            // there is no entry in the versioned list, it fallsback
            // to the unversioned cutoff list.  If there is also no
            // entry there, then it falls back to the default.
            fn cutoff(&self, algo: $algo, version: u8) -> Option<Timestamp> {
                let cutofflist = if let $name::Custom(ref l) = self {
                    l
                } else {
                    &Self::DEFAULT
                };

                cutofflist.cutoff(algo, version)
            }

            fn check(&self, algo: $algo, version: u8,
                     time: Timestamp, d: Option<types::Duration>)
                -> Result<()>
            {
                let cutofflist = if let $name::Custom(ref l) = self {
                    l
                } else {
                    &Self::DEFAULT
                };

                cutofflist.check(algo, version, time, d)
            }
        }

        // Make sure VERSIONED_DEFAULTS is sorted and the keys are
        // unique.
        #[test]
        #[allow(non_snake_case)]
        fn $name() {
            $name::DEFAULT.assert_sorted();
        }
    }
}