Skip to content

Commit b514969

Browse files
committed
Adds the ContextDataHandle interface type.
Signed-off-by: Jesper Brynolf <jesper.brynolf@gmail.com>
1 parent 5a0f772 commit b514969

File tree

3 files changed

+134
-8
lines changed

3 files changed

+134
-8
lines changed

tss-esapi/src/interface_types/data_handles.rs

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
// Copyright 2020 Contributors to the Parsec project.
2-
// SPDX-License-Identifier: Apache-2.0
3-
4-
use crate::handles::{NvIndexTpmHandle, PcrTpmHandle, PersistentTpmHandle, TransientTpmHandle};
5-
6-
/// Can be created with either a persistent
7-
/// or transient TPM handle.
1+
/// This module contains native representations of the TPMI_DH types.
2+
use crate::{
3+
handles::{
4+
HmacSessionTpmHandle, NvIndexTpmHandle, PcrTpmHandle, PersistentTpmHandle,
5+
PolicySessionTpmHandle, TpmHandle, TransientTpmHandle,
6+
},
7+
tss2_esys::TPMI_DH_CONTEXT,
8+
Error, Result, WrapperErrorKind,
9+
};
10+
use std::convert::TryFrom;
11+
/// Enum representing the 'Object' data handles interface type.
12+
///
13+
/// # Details
14+
/// This corresponds to the TPMI_DH_OBJECT interface type.
815
#[derive(Debug, Copy, Clone)]
916
pub enum Object {
1017
Transient(TransientTpmHandle),
@@ -53,10 +60,52 @@ pub enum Entity {
5360
Platform,
5461
Endorsement,
5562
Lockout,
56-
// TODO: Handle Auth
63+
// TODO: Handle Auth, that is vendor specific.
5764
}
5865

5966
#[derive(Debug, Copy, Clone)]
6067
pub enum Pcr {
6168
Pcr(PcrTpmHandle),
6269
}
70+
71+
/// Enum representing the 'Context' data handles interface type.
72+
///
73+
/// # Details
74+
/// This corresponds to the TPMI_DH_CONTEXT interface type.
75+
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
76+
pub enum ContextDataHandle {
77+
Hmac(HmacSessionTpmHandle),
78+
Policy(PolicySessionTpmHandle),
79+
Transient(TransientTpmHandle),
80+
}
81+
82+
impl From<HmacSessionTpmHandle> for ContextDataHandle {
83+
fn from(hmac_session_tpm_handle: HmacSessionTpmHandle) -> Self {
84+
ContextDataHandle::Hmac(hmac_session_tpm_handle)
85+
}
86+
}
87+
88+
impl From<PolicySessionTpmHandle> for ContextDataHandle {
89+
fn from(policy_session_tpm_handle: PolicySessionTpmHandle) -> Self {
90+
ContextDataHandle::Policy(policy_session_tpm_handle)
91+
}
92+
}
93+
94+
impl From<TransientTpmHandle> for ContextDataHandle {
95+
fn from(transient_tpm_handle: TransientTpmHandle) -> Self {
96+
ContextDataHandle::Transient(transient_tpm_handle)
97+
}
98+
}
99+
100+
impl TryFrom<TPMI_DH_CONTEXT> for ContextDataHandle {
101+
type Error = Error;
102+
103+
fn try_from(ffi: TPMI_DH_CONTEXT) -> Result<Self> {
104+
TpmHandle::try_from(ffi).and_then(|tpm_handle| match tpm_handle {
105+
TpmHandle::HmacSession(handle) => Ok(Self::Hmac(handle)),
106+
TpmHandle::PolicySession(handle) => Ok(Self::Policy(handle)),
107+
TpmHandle::Transient(handle) => Ok(Self::Transient(handle)),
108+
_ => Err(Error::local_error(WrapperErrorKind::InvalidParam)),
109+
})
110+
}
111+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright 2023 Contributors to the Parsec project.
2+
// SPDX-License-Identifier: Apache-2.0
3+
use std::convert::TryFrom;
4+
use tss_esapi::{
5+
constants::tss::{
6+
TPM2_HMAC_SESSION_LAST, TPM2_PERMANENT_LAST, TPM2_POLICY_SESSION_LAST, TPM2_TRANSIENT_LAST,
7+
},
8+
handles::{HmacSessionTpmHandle, PolicySessionTpmHandle, TransientTpmHandle},
9+
interface_types::data_handles::ContextDataHandle,
10+
Error, WrapperErrorKind,
11+
};
12+
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(|_| {
16+
panic!(
17+
"Converting {} into ContextDataHandle should not cause an error.",
18+
std::stringify!($tss)
19+
);
20+
});
21+
let expected_handle = $handle_type::try_from($tss).unwrap_or_else(|_| {
22+
panic!(
23+
"Converting {} into {} should not cause an error.",
24+
std::stringify!($tss),
25+
std::stringify!($handle_type)
26+
);
27+
});
28+
if let ContextDataHandle::$enum_item(actual_handle) = context_data_handle {
29+
assert_eq!(
30+
expected_handle,
31+
actual_handle,
32+
"{} was converted into the expected handle.",
33+
std::stringify!($tss)
34+
);
35+
} else {
36+
panic!(
37+
"{} should convert into a {}",
38+
std::stringify!($tss),
39+
std::stringify!(ContextDataHandle::$enum_item)
40+
);
41+
}
42+
assert_eq!(
43+
context_data_handle,
44+
ContextDataHandle::from(expected_handle)
45+
);
46+
};
47+
}
48+
49+
#[test]
50+
fn test_context_data_handle_valid_conversions() {
51+
context_data_handle_valid_conversions!(
52+
ContextDataHandle::Hmac,
53+
HmacSessionTpmHandle,
54+
TPM2_HMAC_SESSION_LAST
55+
);
56+
context_data_handle_valid_conversions!(
57+
ContextDataHandle::Policy,
58+
PolicySessionTpmHandle,
59+
TPM2_POLICY_SESSION_LAST
60+
);
61+
context_data_handle_valid_conversions!(
62+
ContextDataHandle::Transient,
63+
TransientTpmHandle,
64+
TPM2_TRANSIENT_LAST
65+
);
66+
}
67+
68+
#[test]
69+
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+
}
76+
}

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

Lines changed: 1 addition & 0 deletions
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
mod algorithms_tests;
4+
mod data_handles_tests;
45
mod key_bits_tests;
56
mod reserved_handles_tests;
67
mod structure_tags_tests;

0 commit comments

Comments
 (0)