@@ -10,36 +10,23 @@ use crate::{
10
10
session_handles:: { AuthSession , PolicySession } ,
11
11
} ,
12
12
structures:: { EncryptedSecret , IDObject , SymmetricDefinition } ,
13
- tss2_esys:: { TPM2B_PUBLIC , TPMT_PUBLIC } ,
13
+ tss2_esys:: { Tss2_MU_TPM2B_PUBLIC_Marshal , TPM2B_PUBLIC } ,
14
14
utils:: PublicKey ,
15
- Result ,
15
+ Error , Result ,
16
16
} ;
17
- use std:: convert:: { TryFrom , TryInto } ;
17
+ use log:: error;
18
+ use std:: convert:: TryFrom ;
18
19
19
20
#[ derive( Debug ) ]
20
21
/// Wrapper for the parameters needed by MakeCredential
21
22
pub struct MakeCredParams {
22
23
/// TPM name of the object
23
- name : Vec < u8 > ,
24
+ pub name : Vec < u8 > ,
24
25
/// Encoding of the public parameters of the object whose name
25
26
/// will be included in the credential computations
26
- public : Vec < u8 > ,
27
+ pub public : Vec < u8 > ,
27
28
/// Public part of the key used to protect the credential
28
- attesting_key_pub : PublicKey ,
29
- }
30
-
31
- impl MakeCredParams {
32
- pub fn name ( & self ) -> & [ u8 ] {
33
- & self . name
34
- }
35
-
36
- pub fn public ( & self ) -> & [ u8 ] {
37
- & self . public
38
- }
39
-
40
- pub fn attesting_key_pub ( & self ) -> & PublicKey {
41
- & self . attesting_key_pub
42
- }
29
+ pub attesting_key_pub : PublicKey ,
43
30
}
44
31
45
32
impl TransientKeyContext {
@@ -68,29 +55,29 @@ impl TransientKeyContext {
68
55
self . context . flush_context ( object_handle. into ( ) ) ?;
69
56
70
57
let public = TPM2B_PUBLIC :: from ( object_public) ;
71
- let public = unsafe {
72
- std:: mem:: transmute :: < TPMT_PUBLIC , [ u8 ; std:: mem:: size_of :: < TPMT_PUBLIC > ( ) ] > (
73
- public. publicArea ,
58
+ let mut pub_buf = [ 0u8 ; std:: mem:: size_of :: < TPM2B_PUBLIC > ( ) ] ;
59
+ let mut offset = 0 ;
60
+ let result = unsafe {
61
+ Tss2_MU_TPM2B_PUBLIC_Marshal (
62
+ & public,
63
+ & mut pub_buf as * mut u8 ,
64
+ pub_buf. len ( ) as u64 ,
65
+ & mut offset,
74
66
)
75
67
} ;
76
- let attesting_key_pub = match key {
77
- None => {
78
- let key_handle =
79
- ek:: create_ek_object ( & mut self . context , AsymmetricAlgorithm :: Rsa , None ) ?;
80
- let ( attesting_key_pub, _, _) =
81
- self . context . read_public ( key_handle) . or_else ( |e| {
82
- self . context . flush_context ( key_handle. into ( ) ) ?;
83
- Err ( e)
84
- } ) ?;
85
- self . context . flush_context ( key_handle. into ( ) ) ?;
68
+ let result = Error :: from_tss_rc ( result) ;
69
+ if !result. is_success ( ) {
70
+ error ! ( "Error in marshalling TPM2B" ) ;
71
+ return Err ( result) ;
72
+ }
86
73
87
- attesting_key_pub . try_into ( ) ?
88
- }
74
+ let attesting_key_pub = match key {
75
+ None => get_ek_object_public ( & mut self . context ) ? ,
89
76
Some ( key) => key. material . public ,
90
77
} ;
91
78
Ok ( MakeCredParams {
92
79
name : object_name. value ( ) . to_vec ( ) ,
93
- public : public . to_vec ( ) ,
80
+ public : pub_buf . to_vec ( ) ,
94
81
attesting_key_pub,
95
82
} )
96
83
}
@@ -203,3 +190,14 @@ impl TransientKeyContext {
203
190
) )
204
191
}
205
192
}
193
+
194
+ fn get_ek_object_public ( context : & mut crate :: Context ) -> Result < PublicKey > {
195
+ let key_handle = ek:: create_ek_object ( context, AsymmetricAlgorithm :: Rsa , None ) ?;
196
+ let ( attesting_key_pub, _, _) = context. read_public ( key_handle) . or_else ( |e| {
197
+ context. flush_context ( key_handle. into ( ) ) ?;
198
+ Err ( e)
199
+ } ) ?;
200
+ context. flush_context ( key_handle. into ( ) ) ?;
201
+
202
+ PublicKey :: try_from ( attesting_key_pub)
203
+ }
0 commit comments