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
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use crate::{Component, Entry, ImageSbat, PushError, RevocationSbat};
use ascii::AsciiStr;
use rust_alloc::vec::Vec;

/// Image SBAT metadata.
///
/// This contains SBAT entries parsed from the `.sbat` section of a UEFI
/// PE executable.
///
/// See the [crate] documentation for a usage example.
#[derive(Debug, Default, Eq, PartialEq)]
pub struct ImageSbatVec<'a>(Vec<Entry<'a>>);

impl<'a> ImageSbatVec<'a> {
    /// Create a new `ImageSbatVec`.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add an SBAT entry.
    pub fn push(&mut self, entry: Entry<'a>) {
        self.0.push(entry);
    }
}

impl<'a> ImageSbat<'a> for ImageSbatVec<'a> {
    fn entries(&self) -> &[Entry<'a>] {
        &self.0
    }

    fn try_push(&mut self, entry: Entry<'a>) -> Result<(), PushError> {
        self.push(entry);
        Ok(())
    }
}

/// SBAT revocation data.
///
/// This contains SBAT revocation data parsed from a UEFI variable such
/// as `SbatLevel`.
///
/// See the [crate] documentation for a usage example.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct RevocationSbatVec<'a> {
    date: Option<&'a AsciiStr>,
    components: Vec<Component<'a>>,
}

impl<'a> RevocationSbatVec<'a> {
    /// Create an empty `RevocationSbatVec`.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a revoked component.
    fn push(&mut self, component: Component<'a>) {
        self.components.push(component);
    }
}

impl<'a> RevocationSbat<'a> for RevocationSbatVec<'a> {
    fn date(&self) -> Option<&AsciiStr> {
        self.date
    }

    fn set_date(&mut self, date: Option<&'a AsciiStr>) {
        self.date = date;
    }

    fn revoked_components(&self) -> &[Component<'a>] {
        &self.components
    }

    fn try_push(&mut self, component: Component<'a>) -> Result<(), PushError> {
        self.push(component);
        Ok(())
    }
}