Skip to content

Commit e1d708a

Browse files
authored
Merge pull request #507 from Superhepper/native-tpms-context
Native tpms context
2 parents 5a0f772 + 52f3563 commit e1d708a

File tree

16 files changed

+504
-95
lines changed

16 files changed

+504
-95
lines changed

tss-esapi/src/abstraction/transient/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ use crate::{
2020
structures::{
2121
Auth, CreateKeyResult, Data, Digest, EccPoint, EccScheme, Name, Public, PublicBuilder,
2222
PublicEccParametersBuilder, PublicKeyRsa, PublicRsaParametersBuilder, RsaExponent,
23-
RsaScheme, Signature, SignatureScheme, SymmetricDefinitionObject, VerifiedTicket,
23+
RsaScheme, SavedTpmContext, Signature, SignatureScheme, SymmetricDefinitionObject,
24+
VerifiedTicket,
2425
},
2526
tcti_ldr::TctiNameConf,
26-
utils::{create_restricted_decryption_rsa_public, PublicKey, TpmsContext},
27+
utils::{create_restricted_decryption_rsa_public, PublicKey},
2728
Context, Error, Result, ReturnCode, WrapperErrorKind as ErrorKind,
2829
};
2930

@@ -341,7 +342,7 @@ impl TransientKeyContext {
341342
/// just a public key.
342343
pub fn migrate_key_from_ctx(
343344
&mut self,
344-
context: TpmsContext,
345+
context: SavedTpmContext,
345346
auth: Option<Auth>,
346347
) -> Result<KeyMaterial> {
347348
self.set_session_attrs()?;

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/context/tpm_commands/context_management.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ use crate::{
44
context::handle_manager::HandleDropAction,
55
handles::{handle_conversion::TryIntoNotNone, AuthHandle, ObjectHandle, PersistentTpmHandle},
66
interface_types::{data_handles::Persistent, reserved_handles::Provision},
7+
structures::SavedTpmContext,
78
tss2_esys::{Esys_ContextLoad, Esys_ContextSave, Esys_EvictControl, Esys_FlushContext},
8-
utils::TpmsContext,
99
Context, Result, ReturnCode,
1010
};
1111
use log::error;
12-
use std::convert::{TryFrom, TryInto};
12+
use std::convert::TryFrom;
1313
use std::ptr::null_mut;
1414

1515
impl Context {
@@ -18,32 +18,27 @@ impl Context {
1818
/// # Errors
1919
/// * if conversion from `TPMS_CONTEXT` to `TpmsContext` fails, a `WrongParamSize` error will
2020
/// be returned
21-
pub fn context_save(&mut self, handle: ObjectHandle) -> Result<TpmsContext> {
21+
pub fn context_save(&mut self, handle: ObjectHandle) -> Result<SavedTpmContext> {
2222
let mut context_ptr = null_mut();
2323
ReturnCode::ensure_success(
2424
unsafe { Esys_ContextSave(self.mut_context(), handle.into(), &mut context_ptr) },
2525
|ret| {
2626
error!("Error in saving context: {:#010X}", ret);
2727
},
2828
)?;
29-
TpmsContext::try_from(Context::ffi_data_to_owned(context_ptr))
29+
SavedTpmContext::try_from(Context::ffi_data_to_owned(context_ptr))
3030
}
3131

3232
/// Load a previously saved context into the TPM and return the object handle.
3333
///
3434
/// # Errors
3535
/// * if conversion from `TpmsContext` to the native `TPMS_CONTEXT` fails, a `WrongParamSize`
3636
/// error will be returned
37-
pub fn context_load(&mut self, context: TpmsContext) -> Result<ObjectHandle> {
37+
pub fn context_load(&mut self, context: SavedTpmContext) -> Result<ObjectHandle> {
3838
let mut esys_loaded_handle = ObjectHandle::None.into();
39+
let tpm_context = context.into();
3940
ReturnCode::ensure_success(
40-
unsafe {
41-
Esys_ContextLoad(
42-
self.mut_context(),
43-
&context.try_into()?,
44-
&mut esys_loaded_handle,
45-
)
46-
},
41+
unsafe { Esys_ContextLoad(self.mut_context(), &tpm_context, &mut esys_loaded_handle) },
4742
|ret| {
4843
error!("Error in loading context: {:#010X}", ret);
4944
},

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: 127 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
// Copyright 2020 Contributors to the Parsec project.
22
// SPDX-License-Identifier: Apache-2.0
3+
/// This module contains native representations of the TPMI_DH types.
4+
use crate::{
5+
handles::{
6+
HmacSessionTpmHandle, NvIndexTpmHandle, PcrTpmHandle, PersistentTpmHandle,
7+
PolicySessionTpmHandle, TpmHandle, TransientTpmHandle,
8+
},
9+
tss2_esys::{TPMI_DH_CONTEXT, TPMI_DH_SAVED},
10+
Error, Result, WrapperErrorKind,
11+
};
12+
use std::convert::TryFrom;
313

4-
use crate::handles::{NvIndexTpmHandle, PcrTpmHandle, PersistentTpmHandle, TransientTpmHandle};
5-
6-
/// Can be created with either a persistent
7-
/// or transient TPM handle.
14+
/// Enum representing the 'Object' data handles interface type.
15+
///
16+
/// # Details
17+
/// This corresponds to the TPMI_DH_OBJECT interface type.
818
#[derive(Debug, Copy, Clone)]
919
pub enum Object {
1020
Transient(TransientTpmHandle),
@@ -20,7 +30,6 @@ pub enum Parent {
2030
Endorsement,
2131
}
2232

23-
///
2433
/// Enum representing the Persistent DH interface type
2534
/// (TPMI_DH_PERSISTENT)
2635
///
@@ -53,10 +62,122 @@ pub enum Entity {
5362
Platform,
5463
Endorsement,
5564
Lockout,
56-
// TODO: Handle Auth
65+
// TODO: Handle Auth, that is vendor specific.
5766
}
5867

5968
#[derive(Debug, Copy, Clone)]
6069
pub enum Pcr {
6170
Pcr(PcrTpmHandle),
6271
}
72+
73+
/// Enum representing the 'Context' data handles interface type.
74+
///
75+
/// # Details
76+
/// This corresponds to the `TPMI_DH_CONTEXT` interface type. This only
77+
/// exist for compatibility purposes the specification is not entirely
78+
/// clear on whether this should still be used or be completely replaced by
79+
/// [Saved].
80+
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
81+
pub enum ContextDataHandle {
82+
Hmac(HmacSessionTpmHandle),
83+
Policy(PolicySessionTpmHandle),
84+
Transient(TransientTpmHandle),
85+
}
86+
87+
impl From<HmacSessionTpmHandle> for ContextDataHandle {
88+
fn from(hmac_session_tpm_handle: HmacSessionTpmHandle) -> Self {
89+
ContextDataHandle::Hmac(hmac_session_tpm_handle)
90+
}
91+
}
92+
93+
impl From<PolicySessionTpmHandle> for ContextDataHandle {
94+
fn from(policy_session_tpm_handle: PolicySessionTpmHandle) -> Self {
95+
ContextDataHandle::Policy(policy_session_tpm_handle)
96+
}
97+
}
98+
99+
impl From<TransientTpmHandle> for ContextDataHandle {
100+
fn from(transient_tpm_handle: TransientTpmHandle) -> Self {
101+
ContextDataHandle::Transient(transient_tpm_handle)
102+
}
103+
}
104+
105+
impl TryFrom<TPMI_DH_CONTEXT> for ContextDataHandle {
106+
type Error = Error;
107+
108+
fn try_from(ffi: TPMI_DH_CONTEXT) -> Result<Self> {
109+
TpmHandle::try_from(ffi).and_then(|tpm_handle| match tpm_handle {
110+
TpmHandle::HmacSession(handle) => Ok(Self::Hmac(handle)),
111+
TpmHandle::PolicySession(handle) => Ok(Self::Policy(handle)),
112+
TpmHandle::Transient(handle) => Ok(Self::Transient(handle)),
113+
_ => Err(Error::local_error(WrapperErrorKind::InvalidParam)),
114+
})
115+
}
116+
}
117+
118+
/// Enum representing the 'Saved' data handles interface type.
119+
///
120+
/// # Details
121+
/// This corresponds to the `TPMI_DH_SAVED` interface type.
122+
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
123+
pub enum Saved {
124+
/// A HMAC session context.
125+
Hmac(HmacSessionTpmHandle),
126+
/// A policy session context.
127+
Policy(PolicySessionTpmHandle),
128+
/// An ordinary transient object.
129+
Transient,
130+
/// A sequence object.
131+
Sequence,
132+
/// A transient object with stClear attribute SET.
133+
TransientClear,
134+
}
135+
136+
impl From<HmacSessionTpmHandle> for Saved {
137+
fn from(hmac_session_tpm_handle: HmacSessionTpmHandle) -> Self {
138+
Saved::Hmac(hmac_session_tpm_handle)
139+
}
140+
}
141+
142+
impl From<PolicySessionTpmHandle> for Saved {
143+
fn from(policy_session_tpm_handle: PolicySessionTpmHandle) -> Self {
144+
Saved::Policy(policy_session_tpm_handle)
145+
}
146+
}
147+
148+
impl TryFrom<TransientTpmHandle> for Saved {
149+
type Error = Error;
150+
fn try_from(transient_tpm_handle: TransientTpmHandle) -> Result<Self> {
151+
match transient_tpm_handle {
152+
TransientTpmHandle::SavedTransient => Ok(Saved::Transient),
153+
TransientTpmHandle::SavedSequence => Ok(Saved::Sequence),
154+
TransientTpmHandle::SavedTransientClear => Ok(Saved::TransientClear),
155+
_ => Err(Error::local_error(WrapperErrorKind::InvalidParam)),
156+
}
157+
}
158+
}
159+
160+
impl TryFrom<TPMI_DH_SAVED> for Saved {
161+
type Error = Error;
162+
163+
fn try_from(ffi: TPMI_DH_SAVED) -> Result<Self> {
164+
TpmHandle::try_from(ffi).and_then(|tpm_handle| match tpm_handle {
165+
TpmHandle::HmacSession(handle) => Ok(Self::Hmac(handle)),
166+
TpmHandle::PolicySession(handle) => Ok(Self::Policy(handle)),
167+
TpmHandle::Transient(handle) => Saved::try_from(handle),
168+
_ => Err(Error::local_error(WrapperErrorKind::InvalidParam)),
169+
})
170+
}
171+
}
172+
173+
impl From<Saved> for TPMI_DH_SAVED {
174+
fn from(native: Saved) -> TPMI_DH_SAVED {
175+
match native {
176+
Saved::Hmac(handle) => handle.into(),
177+
Saved::Policy(handle) => handle.into(),
178+
Saved::Transient => TransientTpmHandle::SavedTransient.into(),
179+
Saved::Sequence => TransientTpmHandle::SavedSequence.into(),
180+
Saved::TransientClear => TransientTpmHandle::SavedTransientClear.into(),
181+
}
182+
}
183+
}

tss-esapi/src/structures/buffers.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,3 +390,12 @@ pub mod symmetric_key {
390390
pub mod timeout {
391391
buffer_type!(Timeout, 8, TPM2B_TIMEOUT);
392392
}
393+
394+
pub mod tpm_context_data {
395+
use crate::tss2_esys::TPMS_CONTEXT_DATA;
396+
buffer_type!(
397+
TpmContextData,
398+
std::mem::size_of::<TPMS_CONTEXT_DATA>(),
399+
TPM2B_CONTEXT_DATA
400+
);
401+
}

tss-esapi/src/structures/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub use self::buffers::{
4141
private_key_rsa::PrivateKeyRsa, private_vendor_specific::PrivateVendorSpecific,
4242
public::PublicBuffer, public_key_rsa::PublicKeyRsa, sensitive::SensitiveBuffer,
4343
sensitive_create::SensitiveCreateBuffer, sensitive_data::SensitiveData,
44-
symmetric_key::SymmetricKey, timeout::Timeout,
44+
symmetric_key::SymmetricKey, timeout::Timeout, tpm_context_data::TpmContextData,
4545
};
4646
/////////////////////////////////////////////////////////
4747
/// The creation section
@@ -212,3 +212,8 @@ pub use nv::storage::{NvPublic, NvPublicBuilder};
212212
/////////////////////////////////////////////////////////
213213
mod algorithm;
214214
pub use algorithm::symmetric::sensitive_create::SensitiveCreate;
215+
/////////////////////////////////////////////////////////
216+
/// TPM context structures
217+
/////////////////////////////////////////////////////////
218+
mod tpm_context;
219+
pub use tpm_context::SavedTpmContext;

tss-esapi/src/structures/tagged/public.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ impl TryFrom<TPMT_PUBLIC> for Public {
495495
impl_mu_standard!(Public, TPMT_PUBLIC);
496496

497497
impl Serialize for Public {
498-
/// Serialise the [Public] data into it's bytes representation of the TCG
498+
/// Serialize the [Public] data into it's bytes representation of the TCG
499499
/// TPMT_PUBLIC structure.
500500
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
501501
where

0 commit comments

Comments
 (0)