@@ -34,8 +34,10 @@ const SUPPORTED_OPCODES: [Opcode; 7] = [
34
34
Opcode :: ListOpcodes ,
35
35
] ;
36
36
37
- const ROOT_KEY_SIZE : usize = 2048 ;
37
+ const ROOT_KEY_SIZE : u16 = 2048 ;
38
38
const ROOT_KEY_AUTH_SIZE : usize = 32 ;
39
+ const AUTH_STRING_PREFIX : & str = "str:" ;
40
+ const AUTH_HEX_PREFIX : & str = "hex:" ;
39
41
40
42
/// Provider for Trusted Platform Modules
41
43
///
@@ -49,7 +51,7 @@ pub struct TpmProvider {
49
51
// The Mutex is needed both because interior mutability is needed to the ESAPI Context
50
52
// structure that is shared between threads and because two threads are not allowed the same
51
53
// ESAPI context simultaneously.
52
- esapi_context : Mutex < tss_esapi:: TransientObjectContext > ,
54
+ esapi_context : Mutex < tss_esapi:: TransientKeyContext > ,
53
55
// The Key Info Manager stores the key context and its associated authValue (a PasswordContext
54
56
// structure).
55
57
#[ derivative( Debug = "ignore" ) ]
@@ -60,7 +62,7 @@ impl TpmProvider {
60
62
// Creates and initialise a new instance of TpmProvider.
61
63
fn new (
62
64
key_info_store : Arc < RwLock < dyn ManageKeyInfo + Send + Sync > > ,
63
- esapi_context : tss_esapi:: TransientObjectContext ,
65
+ esapi_context : tss_esapi:: TransientKeyContext ,
64
66
) -> Option < TpmProvider > {
65
67
Some ( TpmProvider {
66
68
esapi_context : Mutex :: new ( esapi_context) ,
@@ -192,35 +194,64 @@ impl TpmProviderBuilder {
192
194
self
193
195
}
194
196
197
+ fn get_hierarchy_auth ( & mut self ) -> std:: io:: Result < Vec < u8 > > {
198
+ match self . owner_hierarchy_auth . take ( ) {
199
+ None => Err ( std:: io:: Error :: new (
200
+ ErrorKind :: InvalidData ,
201
+ "missing owner hierarchy auth" ,
202
+ ) ) ,
203
+ Some ( mut auth) if auth. starts_with ( AUTH_STRING_PREFIX ) => {
204
+ Ok ( auth. split_off ( AUTH_STRING_PREFIX . len ( ) ) . into ( ) )
205
+ }
206
+ Some ( mut auth) if auth. starts_with ( AUTH_HEX_PREFIX ) => Ok ( hex:: decode (
207
+ auth. split_off ( AUTH_STRING_PREFIX . len ( ) ) ,
208
+ )
209
+ . or_else ( |_| {
210
+ Err ( std:: io:: Error :: new (
211
+ ErrorKind :: InvalidData ,
212
+ "invalid hex owner hierarchy auth" ,
213
+ ) )
214
+ } ) ?) ,
215
+ Some ( auth) => Ok ( auth. into ( ) ) ,
216
+ }
217
+ }
218
+
195
219
/// Create an instance of TpmProvider
196
220
///
197
221
/// # Safety
198
222
///
199
223
/// Undefined behaviour might appear if two instances of TransientObjectContext are created
200
224
/// using a same TCTI that does not handle multiple applications concurrently.
201
- pub unsafe fn build ( self ) -> std:: io:: Result < TpmProvider > {
225
+ pub unsafe fn build ( mut self ) -> std:: io:: Result < TpmProvider > {
226
+ let hierarchy_auth = self . get_hierarchy_auth ( ) ?;
202
227
TpmProvider :: new (
203
228
self . key_info_store . ok_or_else ( || {
204
229
std:: io:: Error :: new ( ErrorKind :: InvalidData , "missing key info store" )
205
230
} ) ?,
206
- tss_esapi:: TransientObjectContext :: new (
207
- self . tcti
208
- . ok_or_else ( || std:: io:: Error :: new ( ErrorKind :: InvalidData , "missing TCTI" ) ) ?,
209
- ROOT_KEY_SIZE ,
210
- ROOT_KEY_AUTH_SIZE ,
211
- self . owner_hierarchy_auth
212
- . ok_or_else ( || {
213
- std:: io:: Error :: new ( ErrorKind :: InvalidData , "missing owner hierarchy auth" )
214
- } ) ?
215
- . as_bytes ( ) ,
216
- )
217
- . or_else ( |e| {
218
- error ! ( "Error creating TSS Transient Object Context ({})." , e) ;
219
- Err ( std:: io:: Error :: new (
220
- ErrorKind :: InvalidData ,
221
- "failed initializing TSS context" ,
222
- ) )
223
- } ) ?,
231
+ tss_esapi:: abstraction:: transient:: TransientKeyContextBuilder :: new ( )
232
+ . with_tcti (
233
+ self . tcti . ok_or_else ( || {
234
+ std:: io:: Error :: new ( ErrorKind :: InvalidData , "missing TCTI" )
235
+ } ) ?,
236
+ )
237
+ . with_root_key_size ( ROOT_KEY_SIZE )
238
+ . with_root_key_auth_size ( ROOT_KEY_AUTH_SIZE )
239
+ . with_hierarchy_auth ( hierarchy_auth)
240
+ . with_hierarchy ( tss_esapi:: utils:: Hierarchy :: Owner )
241
+ . with_session_hash_alg (
242
+ tss_esapi:: utils:: algorithm_specifiers:: HashingAlgorithm :: Sha256 . into ( ) ,
243
+ )
244
+ . with_default_context_cipher (
245
+ tss_esapi:: utils:: algorithm_specifiers:: Cipher :: aes_256_cfb ( ) ,
246
+ )
247
+ . build ( )
248
+ . or_else ( |e| {
249
+ error ! ( "Error creating TSS Transient Object Context ({})." , e) ;
250
+ Err ( std:: io:: Error :: new (
251
+ ErrorKind :: InvalidData ,
252
+ "failed initializing TSS context" ,
253
+ ) )
254
+ } ) ?,
224
255
)
225
256
. ok_or_else ( || {
226
257
std:: io:: Error :: new ( ErrorKind :: InvalidData , "failed initializing TPM provider" )
0 commit comments