Skip to content

Commit 1caa3d5

Browse files
authored
Merge pull request #294 from Superhepper/locality
Removes TPMA_LOCALITY from context methods.
2 parents 62fb9b7 + 4655346 commit 1caa3d5

File tree

7 files changed

+357
-8
lines changed

7 files changed

+357
-8
lines changed

tss-esapi/src/attributes/locality.rs

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// Copyright 2021 Contributors to the Parsec project.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use crate::{tss2_esys::TPMA_LOCALITY, Error, Result, WrapperErrorKind};
5+
use bitfield::bitfield;
6+
use log::error;
7+
8+
bitfield! {
9+
/// Bitfield representing the locality attributes.
10+
#[derive(Copy, Clone, Eq, PartialEq)]
11+
pub struct LocalityAttributes(TPMA_LOCALITY);
12+
impl Debug;
13+
14+
_, set_locality_zero: 0;
15+
pub locality_zero, _: 0;
16+
_, set_locality_one: 1;
17+
pub locality_one, _: 1;
18+
_, set_locality_two: 2;
19+
pub locality_two, _: 2;
20+
_, set_locality_three: 3;
21+
pub locality_three, _: 3;
22+
_, set_locality_four: 4;
23+
pub locality_four, _: 4;
24+
_, set_extended: 7, 5;
25+
extended, _: 7, 5;
26+
}
27+
28+
impl LocalityAttributes {
29+
pub const LOCALITY_ZERO: LocalityAttributes = LocalityAttributes(1);
30+
pub const LOCALITY_ONE: LocalityAttributes = LocalityAttributes(2);
31+
pub const LOCALITY_TWO: LocalityAttributes = LocalityAttributes(4);
32+
pub const LOCALITY_THREE: LocalityAttributes = LocalityAttributes(8);
33+
pub const LOCALITY_FOUR: LocalityAttributes = LocalityAttributes(16);
34+
/// Returns true if the attributes are extended
35+
pub fn is_extended(&self) -> bool {
36+
self.extended() != 0u8
37+
}
38+
39+
/// Returns the LocalityAttributes as a number.
40+
///
41+
/// # Error
42+
/// If the attributes are not extended en InvalidParams error
43+
/// is returned.
44+
pub fn as_extended(&self) -> Result<u8> {
45+
if self.is_extended() {
46+
Ok(self.0)
47+
} else {
48+
error!("Cannot retrieve LocalityAttributes as extended when the attributes are not indicated to be extended");
49+
Err(Error::local_error(WrapperErrorKind::InvalidParam))
50+
}
51+
}
52+
53+
/// Returns the builder used to construct LocalAttributes.
54+
pub fn builder() -> LocalityAttributesBuilder {
55+
LocalityAttributesBuilder::new()
56+
}
57+
}
58+
59+
impl From<TPMA_LOCALITY> for LocalityAttributes {
60+
fn from(tpma_locality: TPMA_LOCALITY) -> Self {
61+
LocalityAttributes(tpma_locality)
62+
}
63+
}
64+
65+
impl From<LocalityAttributes> for TPMA_LOCALITY {
66+
fn from(locality_attributes: LocalityAttributes) -> Self {
67+
locality_attributes.0
68+
}
69+
}
70+
71+
#[derive(Debug, Clone)]
72+
pub struct LocalityAttributesBuilder {
73+
localities: Vec<u8>,
74+
}
75+
76+
impl LocalityAttributesBuilder {
77+
/// Creates a new builder.
78+
pub const fn new() -> Self {
79+
LocalityAttributesBuilder {
80+
localities: Vec::new(),
81+
}
82+
}
83+
/// Adds a locality to the builder
84+
pub fn with_locality(mut self, locality: u8) -> Self {
85+
self.localities.push(locality);
86+
self
87+
}
88+
89+
/// Adds a slice of localities to the builder
90+
pub fn with_localities(mut self, localities: &[u8]) -> Self {
91+
self.localities.extend_from_slice(localities);
92+
self
93+
}
94+
95+
/// Builds the attributes
96+
pub fn build(self) -> Result<LocalityAttributes> {
97+
let mut locality_attributes = LocalityAttributes(0);
98+
for locality in self.localities {
99+
if locality_attributes.is_extended() {
100+
error!("Locality attribute {new} and locality attribute {prev} cannot be combined because locality attribute {prev} is extended", new=locality, prev=locality_attributes.0);
101+
return Err(Error::local_error(WrapperErrorKind::InvalidParam));
102+
}
103+
match locality {
104+
0 => locality_attributes.set_locality_zero(true),
105+
1 => locality_attributes.set_locality_one(true),
106+
2 => locality_attributes.set_locality_two(true),
107+
3 => locality_attributes.set_locality_three(true),
108+
4 => locality_attributes.set_locality_four(true),
109+
5..=31 => {
110+
error!(
111+
"Locality attribute {new} is invalid and cannot be combined with other locality attributes",
112+
new=locality
113+
);
114+
return Err(Error::local_error(WrapperErrorKind::InvalidParam));
115+
}
116+
32.. => {
117+
if locality_attributes.0 != 0 {
118+
error!("Locality attribute {new} is extended and cannot be combined with locality attribute(s) {old}", new=locality, old=locality_attributes.0);
119+
return Err(Error::local_error(WrapperErrorKind::InvalidParam));
120+
}
121+
locality_attributes.0 = locality;
122+
}
123+
}
124+
}
125+
Ok(locality_attributes)
126+
}
127+
}

tss-esapi/src/attributes/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ pub mod session;
1515
/// the specfication.
1616
pub mod nv_index;
1717

18+
pub mod locality;
19+
20+
pub use locality::{LocalityAttributes, LocalityAttributesBuilder};
1821
pub use nv_index::{NvIndexAttributes, NvIndexAttributesBuilder};
1922
pub use object::{ObjectAttributes, ObjectAttributesBuilder};
2023
pub use session::{SessionAttributes, SessionAttributesBuilder, SessionAttributesMask};

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright 2021 Contributors to the Parsec project.
22
// SPDX-License-Identifier: Apache-2.0
33
use crate::{
4+
attributes::LocalityAttributes,
45
handles::{AuthHandle, ObjectHandle, SessionHandle},
56
interface_types::session_handles::PolicySession,
67
structures::{
@@ -204,7 +205,7 @@ impl Context {
204205
pub fn policy_locality(
205206
&mut self,
206207
policy_session: PolicySession,
207-
locality: TPMA_LOCALITY,
208+
locality: LocalityAttributes,
208209
) -> Result<()> {
209210
let ret = unsafe {
210211
Esys_PolicyLocality(
@@ -213,7 +214,7 @@ impl Context {
213214
self.optional_session_1(),
214215
self.optional_session_2(),
215216
self.optional_session_3(),
216-
locality,
217+
locality.into(),
217218
)
218219
};
219220
let ret = Error::from_tss_rc(ret);

tss-esapi/src/structures/creation.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
use crate::{
5+
attributes::LocalityAttributes,
56
constants::AlgorithmIdentifier,
67
interface_types::algorithm::HashingAlgorithm,
78
structures::{Data, Digest, Name, PcrSelectionList},
8-
tss2_esys::{TPM2B_CREATION_DATA, TPMA_LOCALITY, TPMS_CREATION_DATA},
9+
tss2_esys::{TPM2B_CREATION_DATA, TPMS_CREATION_DATA},
910
Error, Result,
1011
};
1112
use std::convert::{TryFrom, TryInto};
@@ -14,7 +15,7 @@ use std::convert::{TryFrom, TryInto};
1415
pub struct CreationData {
1516
pcr_select: PcrSelectionList,
1617
pcr_digest: Digest,
17-
locality: TPMA_LOCALITY,
18+
locality: LocalityAttributes,
1819
parent_name_alg: Option<HashingAlgorithm>,
1920
parent_name: Name,
2021
parent_qualified_name: Name,
@@ -27,7 +28,7 @@ impl TryFrom<TPMS_CREATION_DATA> for CreationData {
2728
Ok(CreationData {
2829
pcr_select: tss_creation_data.pcrSelect.try_into()?,
2930
pcr_digest: tss_creation_data.pcrDigest.try_into()?,
30-
locality: tss_creation_data.locality,
31+
locality: tss_creation_data.locality.into(),
3132
parent_name_alg: match AlgorithmIdentifier::try_from(tss_creation_data.parentNameAlg)? {
3233
AlgorithmIdentifier::Null => None,
3334
alg => Some(HashingAlgorithm::try_from(alg)?),
@@ -51,7 +52,7 @@ impl From<CreationData> for TPMS_CREATION_DATA {
5152
TPMS_CREATION_DATA {
5253
pcrSelect: creation_data.pcr_select.into(),
5354
pcrDigest: creation_data.pcr_digest.into(),
54-
locality: creation_data.locality,
55+
locality: creation_data.locality.into(),
5556
parentNameAlg: match creation_data.parent_name_alg {
5657
None => AlgorithmIdentifier::Null.into(),
5758
Some(alg) => alg.into(),

0 commit comments

Comments
 (0)