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
use crate::interface_types::algorithm::HashingAlgorithm;
use crate::structures::{PcrSelectSize, PcrSelection, PcrSlot};
use crate::tss2_esys::TPML_PCR_SELECTION;
use crate::{Error, Result, WrapperErrorKind};
use log::error;
use std::collections::HashMap;
use std::convert::TryFrom;
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct PcrSelectionList {
items: Vec<PcrSelection>,
}
impl PcrSelectionList {
pub const MAX_SIZE: usize = 16;
pub fn len(&self) -> usize {
self.items.len()
}
pub fn is_empty(&self) -> bool {
self.items.is_empty()
}
pub fn get_selections(&self) -> &[PcrSelection] {
&self.items
}
pub fn subtract(&mut self, other: &Self) -> Result<()> {
if self == other {
self.items.clear();
return Ok(());
}
if self.is_empty() {
error!("Cannot remove items that does not exist");
return Err(Error::local_error(WrapperErrorKind::InvalidParam));
}
for other_pcr_selection in other.get_selections() {
self.remove_selection(other_pcr_selection)?;
}
self.remove_empty_selections();
Ok(())
}
pub fn list_from_option(pcr_list: Option<PcrSelectionList>) -> PcrSelectionList {
pcr_list.unwrap_or_default()
}
fn remove_empty_selections(&mut self) {
self.items.retain(|v| !v.is_empty());
}
fn remove_selection(&mut self, pcr_selection: &PcrSelection) -> Result<()> {
pcr_selection.selected().iter().try_for_each(|&pcr_slot| {
self.items
.iter_mut()
.find(|existing_pcr_selection| {
existing_pcr_selection.hashing_algorithm() == pcr_selection.hashing_algorithm()
&& existing_pcr_selection.is_selected(pcr_slot)
})
.ok_or_else(|| {
error!("Cannot remove items from a selection that does not exists");
Error::local_error(WrapperErrorKind::InvalidParam)
})
.and_then(|existing_pcr_selection| existing_pcr_selection.deselect_exact(pcr_slot))
})
}
pub fn builder() -> PcrSelectionListBuilder {
PcrSelectionListBuilder::new()
}
}
impl From<PcrSelectionList> for TPML_PCR_SELECTION {
fn from(pcr_selections: PcrSelectionList) -> Self {
let mut tss_pcr_selection_list: TPML_PCR_SELECTION = Default::default();
for pcr_selection in pcr_selections.items {
tss_pcr_selection_list.pcrSelections[tss_pcr_selection_list.count as usize] =
pcr_selection.into();
tss_pcr_selection_list.count += 1;
}
tss_pcr_selection_list
}
}
impl TryFrom<TPML_PCR_SELECTION> for PcrSelectionList {
type Error = Error;
fn try_from(tpml_pcr_selection: TPML_PCR_SELECTION) -> Result<PcrSelectionList> {
let size = tpml_pcr_selection.count as usize;
if size > PcrSelectionList::MAX_SIZE {
error!(
"Invalid size value in TPML_PCR_SELECTION (> {})",
PcrSelectionList::MAX_SIZE
);
return Err(Error::local_error(WrapperErrorKind::InvalidParam));
}
let mut items = Vec::<PcrSelection>::with_capacity(size);
for tpms_pcr_selection in tpml_pcr_selection.pcrSelections[..size].iter() {
let parsed_pcr_selection = PcrSelection::try_from(*tpms_pcr_selection)?;
items.push(parsed_pcr_selection);
}
Ok(PcrSelectionList { items })
}
}
#[derive(Debug, Default)]
pub struct PcrSelectionListBuilder {
size_of_select: Option<PcrSelectSize>,
items: HashMap<HashingAlgorithm, Vec<PcrSlot>>,
}
impl PcrSelectionListBuilder {
pub fn new() -> Self {
PcrSelectionListBuilder {
size_of_select: None,
items: Default::default(),
}
}
pub fn with_size_of_select(mut self, size_of_select: PcrSelectSize) -> Self {
self.size_of_select = Some(size_of_select);
self
}
pub fn with_selection(
mut self,
hash_algorithm: HashingAlgorithm,
pcr_slots: &[PcrSlot],
) -> Self {
match self.items.get_mut(&hash_algorithm) {
Some(previously_selected_pcr_slots) => {
previously_selected_pcr_slots.extend_from_slice(pcr_slots);
}
None => {
let _ = self.items.insert(hash_algorithm, pcr_slots.to_vec());
}
}
self
}
pub fn build(self) -> Result<PcrSelectionList> {
let size_of_select = self.size_of_select.unwrap_or_default();
self.items
.iter()
.try_fold(Vec::<PcrSelection>::new(), |mut acc, (&k, v)| {
PcrSelection::create(k, size_of_select, v.as_slice()).map(|pcr_select| {
acc.push(pcr_select);
acc
})
})
.map(|items| PcrSelectionList { items })
}
}