Skip to content

Commit 067702d

Browse files
committed
Adds the 'Saved' interface type.
- Adds a native version of the Saved interface type. Signed-off-by: Jesper Brynolf <jesper.brynolf@gmail.com>
1 parent b514969 commit 067702d

File tree

4 files changed

+176
-24
lines changed

4 files changed

+176
-24
lines changed

tss-esapi/src/constants/tss.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,10 @@ pub const TSS2_RESMGR_TPM_RC_LAYER: TSS2_RC = 0x000C0000; /* base is a TPM2_RC_*
644644

645645
pub const TSS2_RC_SUCCESS: TSS2_RC = 0x00000000;
646646

647+
pub const TPMI_DH_SAVED_TRANSIENT: TPMI_DH_SAVED = 0x80000000; /* an ordinary transient object */
648+
pub const TPMI_DH_SAVED_SEQUENCE: TPMI_DH_SAVED = 0x80000001; /* a sequence object */
649+
pub const TPMI_DH_SAVED_TRANSIENT_CLEAR: TPMI_DH_SAVED = 0x80000002; /* a transient object with the stClear attribute SET */
650+
647651
pub use crate::tss2_esys::TSS2_BASE_RC_ABI_MISMATCH; /* Passed in ABI version doesn't match called module's ABI version */
648652
pub use crate::tss2_esys::TSS2_BASE_RC_BAD_CONTEXT; /* A context structure is bad */
649653
pub use crate::tss2_esys::TSS2_BASE_RC_BAD_REFERENCE; /* A pointer is NULL that isn't allowed to be NULL. */

tss-esapi/src/handles/tpm.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,10 @@ pub mod saved_session {
326326
pub mod transient {
327327
//! Module for transient TPM handles
328328
use super::*;
329-
use crate::constants::tss::{TPM2_HT_TRANSIENT, TPM2_TRANSIENT_FIRST, TPM2_TRANSIENT_LAST};
329+
use crate::constants::tss::{
330+
TPM2_HT_TRANSIENT, TPM2_TRANSIENT_FIRST, TPM2_TRANSIENT_LAST, TPMI_DH_SAVED_SEQUENCE,
331+
TPMI_DH_SAVED_TRANSIENT, TPMI_DH_SAVED_TRANSIENT_CLEAR,
332+
};
330333

331334
create_tpm_handle_type!(
332335
TransientTpmHandle,
@@ -335,6 +338,13 @@ pub mod transient {
335338
TPM2_TRANSIENT_FIRST,
336339
TPM2_TRANSIENT_LAST
337340
);
341+
add_constant_handle!(TransientTpmHandle, SavedTransient, TPMI_DH_SAVED_TRANSIENT);
342+
add_constant_handle!(TransientTpmHandle, SavedSequence, TPMI_DH_SAVED_SEQUENCE);
343+
add_constant_handle!(
344+
TransientTpmHandle,
345+
SavedTransientClear,
346+
TPMI_DH_SAVED_TRANSIENT_CLEAR
347+
);
338348
}
339349

340350
pub mod persistent {

tss-esapi/src/interface_types/data_handles.rs

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
HmacSessionTpmHandle, NvIndexTpmHandle, PcrTpmHandle, PersistentTpmHandle,
55
PolicySessionTpmHandle, TpmHandle, TransientTpmHandle,
66
},
7-
tss2_esys::TPMI_DH_CONTEXT,
7+
tss2_esys::{TPMI_DH_CONTEXT, TPMI_DH_SAVED},
88
Error, Result, WrapperErrorKind,
99
};
1010
use std::convert::TryFrom;
@@ -27,7 +27,6 @@ pub enum Parent {
2727
Endorsement,
2828
}
2929

30-
///
3130
/// Enum representing the Persistent DH interface type
3231
/// (TPMI_DH_PERSISTENT)
3332
///
@@ -72,6 +71,10 @@ pub enum Pcr {
7271
///
7372
/// # Details
7473
/// This corresponds to the TPMI_DH_CONTEXT interface type.
74+
/// This corresponds to the TPMI_DH_CONTEXT interface type. This only
75+
/// exist for compatibility purposes the specification is not entirely
76+
/// clear on whether this should still be used or be completely replaced by
77+
/// [Saved].
7578
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
7679
pub enum ContextDataHandle {
7780
Hmac(HmacSessionTpmHandle),
@@ -109,3 +112,53 @@ impl TryFrom<TPMI_DH_CONTEXT> for ContextDataHandle {
109112
})
110113
}
111114
}
115+
116+
/// Enum representing the 'Saved' data handles interface type.
117+
///
118+
/// # Details
119+
/// This corresponds to the TPMI_DH_SAVED interface type.
120+
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
121+
pub enum Saved {
122+
Hmac(HmacSessionTpmHandle),
123+
Policy(PolicySessionTpmHandle),
124+
Transient,
125+
Sequence,
126+
TransientClear,
127+
}
128+
129+
impl From<HmacSessionTpmHandle> for Saved {
130+
fn from(hmac_session_tpm_handle: HmacSessionTpmHandle) -> Self {
131+
Saved::Hmac(hmac_session_tpm_handle)
132+
}
133+
}
134+
135+
impl From<PolicySessionTpmHandle> for Saved {
136+
fn from(policy_session_tpm_handle: PolicySessionTpmHandle) -> Self {
137+
Saved::Policy(policy_session_tpm_handle)
138+
}
139+
}
140+
141+
impl TryFrom<TransientTpmHandle> for Saved {
142+
type Error = Error;
143+
fn try_from(transient_tpm_handle: TransientTpmHandle) -> Result<Self> {
144+
match transient_tpm_handle {
145+
TransientTpmHandle::SavedTransient => Ok(Saved::Transient),
146+
TransientTpmHandle::SavedSequence => Ok(Saved::Sequence),
147+
TransientTpmHandle::SavedTransientClear => Ok(Saved::TransientClear),
148+
_ => Err(Error::local_error(WrapperErrorKind::InvalidParam)),
149+
}
150+
}
151+
}
152+
153+
impl TryFrom<TPMI_DH_SAVED> for Saved {
154+
type Error = Error;
155+
156+
fn try_from(ffi: TPMI_DH_SAVED) -> Result<Self> {
157+
TpmHandle::try_from(ffi).and_then(|tpm_handle| match tpm_handle {
158+
TpmHandle::HmacSession(handle) => Ok(Self::Hmac(handle)),
159+
TpmHandle::PolicySession(handle) => Ok(Self::Policy(handle)),
160+
TpmHandle::Transient(handle) => Saved::try_from(handle),
161+
_ => Err(Error::local_error(WrapperErrorKind::InvalidParam)),
162+
})
163+
}
164+
}

tss-esapi/tests/integration_tests/interface_types_tests/data_handles_tests.rs

Lines changed: 106 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@ use std::convert::TryFrom;
44
use tss_esapi::{
55
constants::tss::{
66
TPM2_HMAC_SESSION_LAST, TPM2_PERMANENT_LAST, TPM2_POLICY_SESSION_LAST, TPM2_TRANSIENT_LAST,
7+
TPMI_DH_SAVED_SEQUENCE, TPMI_DH_SAVED_TRANSIENT, TPMI_DH_SAVED_TRANSIENT_CLEAR,
78
},
89
handles::{HmacSessionTpmHandle, PolicySessionTpmHandle, TransientTpmHandle},
9-
interface_types::data_handles::ContextDataHandle,
10+
interface_types::data_handles::{ContextDataHandle, Saved},
1011
Error, WrapperErrorKind,
1112
};
1213

13-
macro_rules! context_data_handle_valid_conversions {
14-
(ContextDataHandle::$enum_item:ident, $handle_type:ident, $tss:ident) => {
15-
let context_data_handle = ContextDataHandle::try_from($tss).unwrap_or_else(|_| {
14+
macro_rules! test_valid_conversions_for_range_enum_items {
15+
($enum_type:ident::$enum_item:ident, $handle_type:ident, $tss:ident) => {
16+
let actual_enum_item = $enum_type::try_from($tss).unwrap_or_else(|_| {
1617
panic!(
17-
"Converting {} into ContextDataHandle should not cause an error.",
18-
std::stringify!($tss)
18+
"Converting {} into {} should not cause an error.",
19+
std::stringify!($tss),
20+
std::any::type_name::<$enum_type>(),
1921
);
2022
});
2123
let expected_handle = $handle_type::try_from($tss).unwrap_or_else(|_| {
@@ -25,40 +27,89 @@ macro_rules! context_data_handle_valid_conversions {
2527
std::stringify!($handle_type)
2628
);
2729
});
28-
if let ContextDataHandle::$enum_item(actual_handle) = context_data_handle {
30+
if let $enum_type::$enum_item(actual_handle) = actual_enum_item {
2931
assert_eq!(
3032
expected_handle,
3133
actual_handle,
32-
"{} was converted into the expected handle.",
34+
"{} was not converted into the expected handle.",
3335
std::stringify!($tss)
3436
);
3537
} else {
3638
panic!(
37-
"{} should convert into a {}",
39+
"{} should convert into a {}.",
3840
std::stringify!($tss),
39-
std::stringify!(ContextDataHandle::$enum_item)
41+
std::stringify!($enum_type::$enum_item)
4042
);
4143
}
4244
assert_eq!(
43-
context_data_handle,
44-
ContextDataHandle::from(expected_handle)
45+
actual_enum_item,
46+
$enum_type::try_from(expected_handle).unwrap_or_else(|_| {
47+
panic!(
48+
"Should be possible to convert {:?} into {}.",
49+
expected_handle,
50+
std::any::type_name::<$enum_type>()
51+
)
52+
})
4553
);
4654
};
4755
}
4856

57+
macro_rules! test_valid_conversions_constant_handle_value {
58+
($enum_type:ident::$enum_item:ident, $handle_type:ident::$constant_item:ident, $tss:ident) => {
59+
let actual_enum_item = $enum_type::try_from($tss).unwrap_or_else(|_| {
60+
panic!(
61+
"Converting {} into {} should not cause an error.",
62+
std::stringify!($tss),
63+
std::any::type_name::<$enum_type>(),
64+
);
65+
});
66+
let expected_handle = $handle_type::$constant_item;
67+
assert_eq!(
68+
actual_enum_item,
69+
$enum_type::try_from(expected_handle).unwrap_or_else(|_| {
70+
panic!(
71+
"Should be possible to convert {:?} into {}.",
72+
expected_handle,
73+
std::any::type_name::<$enum_type>()
74+
)
75+
})
76+
);
77+
};
78+
}
79+
80+
macro_rules! test_invalid_conversions {
81+
($enum_type:ident, $invalid_value:ident, WrapperErrorKind::$error_kind:ident) => {
82+
let result = $enum_type::try_from($invalid_value);
83+
if let Err(error) = result {
84+
assert_eq!(
85+
Error::WrapperError(WrapperErrorKind::$error_kind),
86+
error,
87+
"Converting an invalid value {} did not produce the expected error: {}.",
88+
std::stringify!($invalid_value),
89+
std::stringify!(Error::WrapperError(WrapperErrorKind::$error_kind)),
90+
);
91+
} else {
92+
panic!(
93+
"Converting an invalid value {} did not produce an error.",
94+
std::stringify!($invalid_value)
95+
);
96+
}
97+
};
98+
}
99+
49100
#[test]
50101
fn test_context_data_handle_valid_conversions() {
51-
context_data_handle_valid_conversions!(
102+
test_valid_conversions_for_range_enum_items!(
52103
ContextDataHandle::Hmac,
53104
HmacSessionTpmHandle,
54105
TPM2_HMAC_SESSION_LAST
55106
);
56-
context_data_handle_valid_conversions!(
107+
test_valid_conversions_for_range_enum_items!(
57108
ContextDataHandle::Policy,
58109
PolicySessionTpmHandle,
59110
TPM2_POLICY_SESSION_LAST
60111
);
61-
context_data_handle_valid_conversions!(
112+
test_valid_conversions_for_range_enum_items!(
62113
ContextDataHandle::Transient,
63114
TransientTpmHandle,
64115
TPM2_TRANSIENT_LAST
@@ -67,10 +118,44 @@ fn test_context_data_handle_valid_conversions() {
67118

68119
#[test]
69120
fn test_context_data_handle_invalid_conversion() {
70-
let result = ContextDataHandle::try_from(TPM2_PERMANENT_LAST);
71-
if let Err(error) = result {
72-
assert_eq!(Error::WrapperError(WrapperErrorKind::InvalidParam), error);
73-
} else {
74-
panic!("Converting an invalid value `TPM2_PERMANENT_LAST` into a ContextDataHandle should produce an error.");
75-
}
121+
test_invalid_conversions!(
122+
ContextDataHandle,
123+
TPM2_PERMANENT_LAST,
124+
WrapperErrorKind::InvalidParam
125+
);
126+
}
127+
128+
#[test]
129+
fn test_saved_valid_conversions() {
130+
test_valid_conversions_for_range_enum_items!(
131+
Saved::Hmac,
132+
HmacSessionTpmHandle,
133+
TPM2_HMAC_SESSION_LAST
134+
);
135+
test_valid_conversions_for_range_enum_items!(
136+
Saved::Policy,
137+
PolicySessionTpmHandle,
138+
TPM2_POLICY_SESSION_LAST
139+
);
140+
test_valid_conversions_constant_handle_value!(
141+
Saved::Transient,
142+
TransientTpmHandle::SavedTransient,
143+
TPMI_DH_SAVED_TRANSIENT
144+
);
145+
test_valid_conversions_constant_handle_value!(
146+
Saved::Sequence,
147+
TransientTpmHandle::SavedSequence,
148+
TPMI_DH_SAVED_SEQUENCE
149+
);
150+
test_valid_conversions_constant_handle_value!(
151+
Saved::TransientClear,
152+
TransientTpmHandle::SavedTransientClear,
153+
TPMI_DH_SAVED_TRANSIENT_CLEAR
154+
);
155+
}
156+
157+
#[test]
158+
fn test_saved_invalid_conversions() {
159+
test_invalid_conversions!(Saved, TPM2_PERMANENT_LAST, WrapperErrorKind::InvalidParam);
160+
test_invalid_conversions!(Saved, TPM2_TRANSIENT_LAST, WrapperErrorKind::InvalidParam);
76161
}

0 commit comments

Comments
 (0)