Skip to content

Commit e44b389

Browse files
committed
Makes use of PcrSlotCollection.
Changes PcrSelection to use PcrSlotCollection internally. Changes PcrSelect to use PcrSlotCollection internally. Changes TaggedPcrSelect to use PcrSlotCollection internally. Signed-off-by: Jesper Brynolf <[email protected]>
1 parent 57f059b commit e44b389

22 files changed

+355
-342
lines changed

tss-esapi/src/abstraction/pcr.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ pub use data::PcrData;
5353
/// PcrSlot::Slot20,
5454
/// PcrSlot::Slot21,
5555
/// ])
56-
/// .build();
56+
/// .build()
57+
/// .expect("Failed to build PcrSelectionList");
5758
/// let _pcr_data = tss_esapi::abstraction::pcr::read_all(&mut context, pcr_selection_list)
5859
/// .expect("pcr::read_all failed");
5960
/// ```

tss-esapi/src/context/tpm_commands/integrity_collection_pcr.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ impl Context {
146146
/// // that is going to be read.
147147
/// let pcr_selection_list = PcrSelectionListBuilder::new()
148148
/// .with_selection(HashingAlgorithm::Sha256, &[PcrSlot::Slot0, PcrSlot::Slot1])
149-
/// .build();
149+
/// .build()
150+
/// .expect("Failed to build PcrSelectionList");
150151
///
151152
/// let (update_counter, read_pcr_list, digest_list) = context.pcr_read(pcr_selection_list)
152153
/// .expect("Call to pcr_read failed");

tss-esapi/src/structures/lists/pcr_selection.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::convert::TryFrom;
1010

1111
/// A struct representing a pcr selection list. This
1212
/// corresponds to the TSS TPML_PCR_SELECTION.
13-
#[derive(Debug, Clone, PartialEq, Eq)]
13+
#[derive(Debug, Clone, PartialEq, Eq, Default)]
1414
pub struct PcrSelectionList {
1515
items: Vec<PcrSelection>,
1616
}
@@ -56,7 +56,7 @@ impl PcrSelectionList {
5656
///
5757
/// This returns an empty list if None is passed
5858
pub fn list_from_option(pcr_list: Option<PcrSelectionList>) -> PcrSelectionList {
59-
pcr_list.unwrap_or_else(|| PcrSelectionListBuilder::new().build())
59+
pcr_list.unwrap_or_default()
6060
}
6161

6262
/// Private methods for removing pcr selections that are empty.
@@ -89,7 +89,7 @@ impl PcrSelectionList {
8989
}
9090

9191
impl From<PcrSelectionList> for TPML_PCR_SELECTION {
92-
fn from(pcr_selections: PcrSelectionList) -> TPML_PCR_SELECTION {
92+
fn from(pcr_selections: PcrSelectionList) -> Self {
9393
let mut tss_pcr_selection_list: TPML_PCR_SELECTION = Default::default();
9494
for pcr_selection in pcr_selections.items {
9595
tss_pcr_selection_list.pcrSelections[tss_pcr_selection_list.count as usize] =
@@ -178,17 +178,20 @@ impl PcrSelectionListBuilder {
178178
/// provided.
179179
///
180180
/// If no size of select have been provided then it will
181-
/// be defaulted to 3. This may not be the correct size for
181+
/// be defaulted to to the most suitable with regard to TPM2_PCR_SELECT_MAX.
182+
/// This may not be the correct size for
182183
/// the current platform. The correct values can be obtained
183184
/// by querying the tpm for its capabilities.
184-
pub fn build(self) -> PcrSelectionList {
185+
pub fn build(self) -> Result<PcrSelectionList> {
185186
let size_of_select = self.size_of_select.unwrap_or_default();
186-
PcrSelectionList {
187-
items: self
188-
.items
189-
.iter()
190-
.map(|(k, v)| PcrSelection::new(*k, size_of_select, v.as_slice()))
191-
.collect(),
192-
}
187+
self.items
188+
.iter()
189+
.try_fold(Vec::<PcrSelection>::new(), |mut acc, (&k, v)| {
190+
PcrSelection::create(k, size_of_select, v.as_slice()).map(|pcr_select| {
191+
acc.push(pcr_select);
192+
acc
193+
})
194+
})
195+
.map(|items| PcrSelectionList { items })
193196
}
194197
}

tss-esapi/src/structures/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ pub mod command_code_attributes_list {
131131
pub use super::lists::command_code_attributes::*;
132132
}
133133

134-
pub use pcr::slot_collection::PcrSlotCollection;
134+
pub(crate) use pcr::slot_collection::PcrSlotCollection;
135135
/////////////////////////////////////////////////////////
136136
/// The parameters section
137137
/////////////////////////////////////////////////////////
Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
// Copyright 2020 Contributors to the Parsec project.
22
// SPDX-License-Identifier: Apache-2.0
33
use crate::{
4-
structures::{PcrSelectSize, PcrSlot},
5-
tss2_esys::{TPM2_PCR_SELECT_MAX, TPMS_PCR_SELECT},
6-
Error, Result, WrapperErrorKind,
4+
structures::{PcrSelectSize, PcrSlot, PcrSlotCollection},
5+
tss2_esys::TPMS_PCR_SELECT,
6+
Error, Result,
77
};
88

9-
use enumflags2::BitFlags;
10-
use log::error;
11-
129
use std::convert::TryFrom;
1310
/// This module contains necessary representations
1411
/// of the items belonging to the TPMS_PCR_SELECT
@@ -21,17 +18,15 @@ use std::convert::TryFrom;
2118
/// not adhering to a platform-specific specification.
2219
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
2320
pub struct PcrSelect {
24-
size_of_select: PcrSelectSize,
25-
selected_pcrs: BitFlags<PcrSlot>,
21+
pcr_slot_collection: PcrSlotCollection,
2622
}
2723

2824
impl PcrSelect {
2925
/// Creates a new PcrSelect
30-
pub fn new(size_of_select: PcrSelectSize, pcr_slots: &[PcrSlot]) -> Self {
31-
PcrSelect {
32-
size_of_select,
33-
selected_pcrs: pcr_slots.iter().copied().collect(),
34-
}
26+
pub fn create(pcr_select_size: PcrSelectSize, pcr_slots: &[PcrSlot]) -> Result<Self> {
27+
PcrSlotCollection::create(pcr_select_size, pcr_slots).map(|pcr_slot_collection| PcrSelect {
28+
pcr_slot_collection,
29+
})
3530
}
3631

3732
/// Returns the size of the select.
@@ -41,46 +36,32 @@ impl PcrSelect {
4136
/// octets that are needed to hold the bit field
4237
/// that indicate what slots that are selected.
4338
pub fn size_of_select(&self) -> PcrSelectSize {
44-
self.size_of_select
39+
self.pcr_slot_collection.size_of_select()
4540
}
4641

4742
/// Returns the selected PCRs in the select.
4843
pub fn selected_pcrs(&self) -> Vec<PcrSlot> {
49-
self.selected_pcrs.iter().collect()
44+
self.pcr_slot_collection.collection()
5045
}
5146
}
5247

5348
impl TryFrom<TPMS_PCR_SELECT> for PcrSelect {
5449
type Error = Error;
5550
fn try_from(tss_pcr_select: TPMS_PCR_SELECT) -> Result<Self> {
56-
// Parse the sizeofSelect into a SelectSize.
57-
let size_of_select = PcrSelectSize::try_from(tss_pcr_select.sizeofSelect)?;
58-
59-
// Select only the octets indicated by sizeofSelect
60-
let mut selected_octets = [0u8; TPM2_PCR_SELECT_MAX as usize];
61-
let number_of_selected_octets: usize = size_of_select.as_usize();
62-
selected_octets[..number_of_selected_octets]
63-
.copy_from_slice(&tss_pcr_select.pcrSelect[..number_of_selected_octets]);
64-
65-
// Parse selected pcrs into BitFlags
66-
let selected_pcrs = BitFlags::<PcrSlot>::try_from(u32::from_le_bytes(selected_octets))
67-
.map_err(|e| {
68-
error!("Error parsing pcrSelect to a BitFlags<PcrSlot>: {}.", e);
69-
Error::local_error(WrapperErrorKind::UnsupportedParam)
70-
})?;
71-
72-
Ok(PcrSelect {
73-
size_of_select,
74-
selected_pcrs,
75-
})
51+
PcrSlotCollection::try_from((tss_pcr_select.sizeofSelect, tss_pcr_select.pcrSelect)).map(
52+
|pcr_slot_collection| PcrSelect {
53+
pcr_slot_collection,
54+
},
55+
)
7656
}
7757
}
7858

7959
impl From<PcrSelect> for TPMS_PCR_SELECT {
8060
fn from(pcr_select: PcrSelect) -> Self {
61+
let (size_of_select, pcr_select) = pcr_select.pcr_slot_collection.into();
8162
TPMS_PCR_SELECT {
82-
sizeofSelect: pcr_select.size_of_select.as_u8(),
83-
pcrSelect: pcr_select.selected_pcrs.bits().to_le_bytes(),
63+
sizeofSelect: size_of_select,
64+
pcrSelect: pcr_select,
8465
}
8566
}
8667
}

0 commit comments

Comments
 (0)