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 ) ]
919pub 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 ) ]
6069pub 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+ }
0 commit comments