Skip to content

Commit 26fc041

Browse files
committed
Cleans up CapabilityData.
Cleans up CapabilityData enum by removing all conversions functions and instead implment TryFrom and From using the conversions of the underlaying list types. Makes use of the max_cap_size function in all the list types that contain capability data to reduce the number items they depend on from the bindgen types and constants. Signed-off-by: Jesper Brynolf <[email protected]>
1 parent 880053e commit 26fc041

File tree

7 files changed

+124
-105
lines changed

7 files changed

+124
-105
lines changed
Lines changed: 102 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
// Copyright 2020 Contributors to the Parsec project.
22
// SPDX-License-Identifier: Apache-2.0
33
use crate::{
4-
constants::tss::*,
4+
constants::CapabilityType,
55
structures::{
6-
AlgorithmPropertyList, CommandCodeList, EccCurveList, HandleList, PcrSelectionList,
7-
TaggedPcrPropertyList, TaggedTpmPropertyList,
6+
AlgorithmPropertyList, CommandCodeAttributesList, CommandCodeList, EccCurveList,
7+
HandleList, PcrSelectionList, TaggedPcrPropertyList, TaggedTpmPropertyList,
88
},
9-
tss2_esys::*,
9+
tss2_esys::{TPM2_CAP, TPM2_MAX_CAP_BUFFER, TPMS_CAPABILITY_DATA, TPMU_CAPABILITIES},
1010
Error, Result, WrapperErrorKind,
1111
};
12+
use log::error;
1213
use std::convert::{TryFrom, TryInto};
1314
use std::mem::size_of;
1415

@@ -17,11 +18,12 @@ use std::mem::size_of;
1718
///
1819
/// # Details
1920
/// This corresponds to `TPMS_CAPABILITY_DATA`
21+
#[non_exhaustive]
2022
#[derive(Debug, Clone)]
2123
pub enum CapabilityData {
2224
Algorithms(AlgorithmPropertyList),
2325
Handles(HandleList),
24-
Commands(Vec<TPMA_CC>),
26+
Commands(CommandCodeAttributesList),
2527
PpCommands(CommandCodeList),
2628
AuditCommands(CommandCodeList),
2729
AssignedPcr(PcrSelectionList),
@@ -33,85 +35,113 @@ pub enum CapabilityData {
3335
// ActData(),
3436
}
3537

36-
pub(crate) const fn max_cap_size<T>() -> u32 {
37-
((TPM2_MAX_CAP_BUFFER as usize - size_of::<TPM2_CAP>() - size_of::<u32>()) / size_of::<T>())
38-
as u32
38+
pub const fn max_cap_size<T>() -> usize {
39+
(TPM2_MAX_CAP_BUFFER as usize - size_of::<TPM2_CAP>() - size_of::<u32>()) / size_of::<T>()
3940
}
4041

41-
fn cd_from_alg_properties(props: TPML_ALG_PROPERTY) -> Result<CapabilityData> {
42-
Ok(CapabilityData::Algorithms(props.try_into()?))
43-
}
44-
45-
fn cd_from_handles(props: TPML_HANDLE) -> Result<CapabilityData> {
46-
Ok(CapabilityData::Handles(HandleList::try_from(props)?))
47-
}
48-
49-
fn cd_from_command(props: TPML_CCA) -> Result<CapabilityData> {
50-
if props.count > TPM2_MAX_CAP_CC {
51-
return Err(Error::WrapperError(WrapperErrorKind::InvalidParam));
52-
}
53-
54-
let mut data = Vec::new();
55-
data.reserve_exact(props.count as usize);
56-
57-
for i in 0..props.count {
58-
data.push(props.commandAttributes[i as usize]);
42+
impl From<CapabilityData> for TPMS_CAPABILITY_DATA {
43+
fn from(capability_data: CapabilityData) -> Self {
44+
match capability_data {
45+
CapabilityData::Algorithms(data) => TPMS_CAPABILITY_DATA {
46+
capability: CapabilityType::Algorithms.into(),
47+
data: TPMU_CAPABILITIES {
48+
algorithms: data.into(),
49+
},
50+
},
51+
CapabilityData::Handles(data) => TPMS_CAPABILITY_DATA {
52+
capability: CapabilityType::Handles.into(),
53+
data: TPMU_CAPABILITIES {
54+
handles: data.into(),
55+
},
56+
},
57+
CapabilityData::Commands(data) => TPMS_CAPABILITY_DATA {
58+
capability: CapabilityType::Command.into(),
59+
data: TPMU_CAPABILITIES {
60+
command: data.into(),
61+
},
62+
},
63+
CapabilityData::PpCommands(data) => TPMS_CAPABILITY_DATA {
64+
capability: CapabilityType::PpCommands.into(),
65+
data: TPMU_CAPABILITIES {
66+
ppCommands: data.into(),
67+
},
68+
},
69+
CapabilityData::AuditCommands(data) => TPMS_CAPABILITY_DATA {
70+
capability: CapabilityType::AuditCommands.into(),
71+
data: TPMU_CAPABILITIES {
72+
auditCommands: data.into(),
73+
},
74+
},
75+
CapabilityData::AssignedPcr(data) => TPMS_CAPABILITY_DATA {
76+
capability: CapabilityType::AssignedPcr.into(),
77+
data: TPMU_CAPABILITIES {
78+
assignedPCR: data.into(),
79+
},
80+
},
81+
CapabilityData::TpmProperties(data) => TPMS_CAPABILITY_DATA {
82+
capability: CapabilityType::TpmProperties.into(),
83+
data: TPMU_CAPABILITIES {
84+
tpmProperties: data.into(),
85+
},
86+
},
87+
CapabilityData::PcrProperties(data) => TPMS_CAPABILITY_DATA {
88+
capability: CapabilityType::PcrProperties.into(),
89+
data: TPMU_CAPABILITIES {
90+
pcrProperties: data.into(),
91+
},
92+
},
93+
CapabilityData::EccCurves(data) => TPMS_CAPABILITY_DATA {
94+
capability: CapabilityType::EccCurves.into(),
95+
data: TPMU_CAPABILITIES {
96+
eccCurves: data.into(),
97+
},
98+
},
99+
}
59100
}
60-
61-
Ok(CapabilityData::Commands(data))
62-
}
63-
64-
fn cd_from_pp_commands(props: TPML_CC) -> Result<CapabilityData> {
65-
Ok(CapabilityData::PpCommands(CommandCodeList::try_from(
66-
props,
67-
)?))
68-
}
69-
70-
fn cd_from_audit_commands(props: TPML_CC) -> Result<CapabilityData> {
71-
Ok(CapabilityData::AuditCommands(CommandCodeList::try_from(
72-
props,
73-
)?))
74-
}
75-
76-
fn cd_from_assigned_pcrs(props: TPML_PCR_SELECTION) -> Result<CapabilityData> {
77-
Ok(CapabilityData::AssignedPcr(props.try_into()?))
78-
}
79-
80-
fn cd_from_tpm_properties(props: TPML_TAGGED_TPM_PROPERTY) -> Result<CapabilityData> {
81-
Ok(CapabilityData::TpmProperties(props.try_into()?))
82-
}
83-
84-
fn cd_from_pcr_properties(props: TPML_TAGGED_PCR_PROPERTY) -> Result<CapabilityData> {
85-
Ok(CapabilityData::PcrProperties(props.try_into()?))
86-
}
87-
88-
fn cd_from_ecc_curves(props: TPML_ECC_CURVE) -> Result<CapabilityData> {
89-
Ok(CapabilityData::EccCurves(EccCurveList::try_from(props)?))
90101
}
91102

92103
impl TryFrom<TPMS_CAPABILITY_DATA> for CapabilityData {
93104
type Error = Error;
94105

95-
fn try_from(capab_data: TPMS_CAPABILITY_DATA) -> Result<Self> {
106+
fn try_from(tpms_capability_data: TPMS_CAPABILITY_DATA) -> Result<Self> {
96107
// SAFETY: This is a C union, and Rust wants us to make sure we're using the correct item.
97108
// These unsafe blocks are fine because we ensure the correct type is used.
98-
match capab_data.capability {
99-
TPM2_CAP_ALGS => cd_from_alg_properties(unsafe { capab_data.data.algorithms }),
100-
TPM2_CAP_HANDLES => cd_from_handles(unsafe { capab_data.data.handles }),
101-
TPM2_CAP_COMMANDS => cd_from_command(unsafe { capab_data.data.command }),
102-
TPM2_CAP_PP_COMMANDS => cd_from_pp_commands(unsafe { capab_data.data.ppCommands }),
103-
TPM2_CAP_AUDIT_COMMANDS => {
104-
cd_from_audit_commands(unsafe { capab_data.data.auditCommands })
105-
}
106-
TPM2_CAP_PCRS => cd_from_assigned_pcrs(unsafe { capab_data.data.assignedPCR }),
107-
TPM2_CAP_TPM_PROPERTIES => {
108-
cd_from_tpm_properties(unsafe { capab_data.data.tpmProperties })
109+
match CapabilityType::try_from(tpms_capability_data.capability)? {
110+
CapabilityType::Algorithms => Ok(CapabilityData::Algorithms(
111+
unsafe { tpms_capability_data.data.algorithms }.try_into()?,
112+
)),
113+
CapabilityType::Handles => Ok(CapabilityData::Handles(
114+
unsafe { tpms_capability_data.data.handles }.try_into()?,
115+
)),
116+
CapabilityType::Command => Ok(CapabilityData::Commands(
117+
unsafe { tpms_capability_data.data.command }.try_into()?,
118+
)),
119+
CapabilityType::PpCommands => Ok(CapabilityData::PpCommands(
120+
unsafe { tpms_capability_data.data.ppCommands }.try_into()?,
121+
)),
122+
CapabilityType::AuditCommands => Ok(CapabilityData::AuditCommands(
123+
unsafe { tpms_capability_data.data.auditCommands }.try_into()?,
124+
)),
125+
CapabilityType::AssignedPcr => Ok(CapabilityData::AssignedPcr(
126+
unsafe { tpms_capability_data.data.assignedPCR }.try_into()?,
127+
)),
128+
CapabilityType::TpmProperties => Ok(CapabilityData::TpmProperties(
129+
unsafe { tpms_capability_data.data.tpmProperties }.try_into()?,
130+
)),
131+
CapabilityType::PcrProperties => Ok(CapabilityData::PcrProperties(
132+
unsafe { tpms_capability_data.data.pcrProperties }.try_into()?,
133+
)),
134+
CapabilityType::EccCurves => Ok(CapabilityData::EccCurves(
135+
unsafe { tpms_capability_data.data.eccCurves }.try_into()?,
136+
)),
137+
CapabilityType::AuthPolicies => {
138+
error!("AuthPolicies capability type is currently not supported");
139+
Err(Error::WrapperError(WrapperErrorKind::UnsupportedParam))
109140
}
110-
TPM2_CAP_PCR_PROPERTIES => {
111-
cd_from_pcr_properties(unsafe { capab_data.data.pcrProperties })
141+
CapabilityType::Act => {
142+
error!("Act capability type is currently not supported");
143+
Err(Error::WrapperError(WrapperErrorKind::UnsupportedParam))
112144
}
113-
TPM2_CAP_ECC_CURVES => cd_from_ecc_curves(unsafe { capab_data.data.eccCurves }),
114-
_ => Err(Error::WrapperError(WrapperErrorKind::UnsupportedParam)),
115145
}
116146
}
117147
}

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use crate::{
55
constants::AlgorithmIdentifier,
66
structures::AlgorithmProperty,
7-
tss2_esys::{TPM2_CAP, TPM2_MAX_CAP_BUFFER, TPML_ALG_PROPERTY, TPMS_ALG_PROPERTY},
7+
tss2_esys::{TPML_ALG_PROPERTY, TPMS_ALG_PROPERTY},
88
Error, Result, WrapperErrorKind,
99
};
1010
use log::error;
@@ -29,10 +29,7 @@ impl AlgorithmPropertyList {
2929
/// Private function that calculates the maximum number
3030
/// elements allowed in internal storage.
3131
const fn calculate_max_size() -> usize {
32-
(TPM2_MAX_CAP_BUFFER as usize
33-
- std::mem::size_of::<TPM2_CAP>()
34-
- std::mem::size_of::<u32>())
35-
/ std::mem::size_of::<TPMS_ALG_PROPERTY>()
32+
crate::structures::capabilitydata::max_cap_size::<TPMS_ALG_PROPERTY>()
3633
}
3734
}
3835

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33
use crate::{
44
attributes::CommandCodeAttributes,
5-
tss2_esys::{TPM2_CAP, TPM2_MAX_CAP_BUFFER, TPMA_CC, TPML_CCA},
5+
tss2_esys::{TPMA_CC, TPML_CCA},
66
Error, Result, WrapperErrorKind,
77
};
88
use log::error;
@@ -31,13 +31,7 @@ impl CommandCodeAttributesList {
3131
/// Private function that calculates the maximum number
3232
/// elements allowed in internal storage.
3333
const fn calculate_max_size() -> usize {
34-
// According to the specification the size is vendor specific.
35-
// So if someone is using modified values in their TSS libraries
36-
// it is picked up here.
37-
(TPM2_MAX_CAP_BUFFER as usize
38-
- std::mem::size_of::<TPM2_CAP>()
39-
- std::mem::size_of::<u32>())
40-
/ std::mem::size_of::<TPMA_CC>()
34+
crate::structures::capabilitydata::max_cap_size::<TPMA_CC>()
4135
}
4236
}
4337

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ pub struct EccCurveList {
1717
}
1818

1919
impl EccCurveList {
20-
pub const MAX_SIZE: usize =
21-
crate::structures::capabilitydata::max_cap_size::<TPM2_ECC_CURVE>() as usize;
20+
pub const MAX_SIZE: usize = Self::calculate_max_size();
2221

2322
pub fn new() -> Self {
2423
EccCurveList {
@@ -43,6 +42,12 @@ impl EccCurveList {
4342
pub fn into_inner(self) -> Vec<EccCurveIdentifier> {
4443
self.ecc_curves
4544
}
45+
46+
/// Private function that calculates the maximum number
47+
/// elements allowed in internal storage.
48+
const fn calculate_max_size() -> usize {
49+
crate::structures::capabilitydata::max_cap_size::<TPM2_ECC_CURVE>()
50+
}
4651
}
4752

4853
impl TryFrom<TPML_ECC_CURVE> for EccCurveList {

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ pub struct HandleList {
1717
}
1818

1919
impl HandleList {
20-
pub const MAX_SIZE: usize =
21-
crate::structures::capabilitydata::max_cap_size::<TPM2_HANDLE>() as usize;
20+
pub const MAX_SIZE: usize = Self::calculate_max_size();
2221

2322
pub fn new() -> Self {
2423
HandleList {
@@ -43,6 +42,12 @@ impl HandleList {
4342
pub fn into_inner(self) -> Vec<TpmHandle> {
4443
self.handles
4544
}
45+
46+
/// Private function that calculates the maximum number
47+
/// elements allowed in internal storage.
48+
const fn calculate_max_size() -> usize {
49+
crate::structures::capabilitydata::max_cap_size::<TPM2_HANDLE>()
50+
}
4651
}
4752

4853
impl TryFrom<TPML_HANDLE> for HandleList {

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use crate::{
55
constants::PcrPropertyTag,
66
structures::{PcrSlot, TaggedPcrSelect},
7-
tss2_esys::{TPM2_CAP, TPM2_MAX_CAP_BUFFER, TPML_TAGGED_PCR_PROPERTY, TPMS_TAGGED_PCR_SELECT},
7+
tss2_esys::{TPML_TAGGED_PCR_PROPERTY, TPMS_TAGGED_PCR_SELECT},
88
Error, Result, WrapperErrorKind,
99
};
1010
use log::error;
@@ -44,13 +44,7 @@ impl TaggedPcrPropertyList {
4444
/// Private function that calculates the maximum number
4545
/// elements allowed in internal storage.
4646
const fn calculate_max_size() -> usize {
47-
// According to the specification the size is vendor specific.
48-
// So if someone is using modified values in their TSS libraries
49-
// it is picked up here.
50-
(TPM2_MAX_CAP_BUFFER as usize
51-
- std::mem::size_of::<TPM2_CAP>()
52-
- std::mem::size_of::<u32>())
53-
/ std::mem::size_of::<TPMS_TAGGED_PCR_SELECT>()
47+
crate::structures::capabilitydata::max_cap_size::<TPMS_TAGGED_PCR_SELECT>()
5448
}
5549
}
5650

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use crate::{
55
constants::PropertyTag,
66
structures::TaggedProperty,
7-
tss2_esys::{TPM2_CAP, TPM2_MAX_CAP_BUFFER, TPML_TAGGED_TPM_PROPERTY, TPMS_TAGGED_PROPERTY},
7+
tss2_esys::{TPML_TAGGED_TPM_PROPERTY, TPMS_TAGGED_PROPERTY},
88
Error, Result, WrapperErrorKind,
99
};
1010
use log::error;
@@ -32,13 +32,7 @@ impl TaggedTpmPropertyList {
3232
/// Private function that calculates the maximum number
3333
/// elements allowed in internal storage.
3434
const fn calculate_max_size() -> usize {
35-
// According to the specification the size is vendor specific.
36-
// So if someone is using modified values in their TSS libraries
37-
// it is picked up here.
38-
(TPM2_MAX_CAP_BUFFER as usize
39-
- std::mem::size_of::<TPM2_CAP>()
40-
- std::mem::size_of::<u32>())
41-
/ std::mem::size_of::<TPMS_TAGGED_PROPERTY>()
35+
crate::structures::capabilitydata::max_cap_size::<TPMS_TAGGED_PROPERTY>()
4236
}
4337
}
4438

0 commit comments

Comments
 (0)