1 //
2 // Copyright (C) 2014 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #include "trunks/mock_tpm.h"
18 
19 #include "trunks/tpm_utility.h"
20 
21 using testing::_;
22 using testing::DoAll;
23 using testing::Return;
24 using testing::SetArgPointee;
25 
26 namespace trunks {
27 
MockTpm()28 MockTpm::MockTpm() : Tpm(nullptr) {
29   ON_CALL(*this, PCR_AllocateSync(_, _, _, _, _, _, _, _))
30       .WillByDefault(DoAll(SetArgPointee<3>(YES), Return(TPM_RC_SUCCESS)));
31 }
32 
~MockTpm()33 MockTpm::~MockTpm() {}
34 
StartAuthSession(const TPMI_DH_OBJECT & tpm_key,const std::string & tpm_key_name,const TPMI_DH_ENTITY & bind,const std::string & bind_name,const TPM2B_NONCE & nonce_caller,const TPM2B_ENCRYPTED_SECRET & encrypted_salt,const TPM_SE & session_type,const TPMT_SYM_DEF & symmetric,const TPMI_ALG_HASH & auth_hash,AuthorizationDelegate * authorization_delegate,const StartAuthSessionResponse & callback)35 void MockTpm::StartAuthSession(const TPMI_DH_OBJECT& tpm_key,
36                                const std::string& tpm_key_name,
37                                const TPMI_DH_ENTITY& bind,
38                                const std::string& bind_name,
39                                const TPM2B_NONCE& nonce_caller,
40                                const TPM2B_ENCRYPTED_SECRET& encrypted_salt,
41                                const TPM_SE& session_type,
42                                const TPMT_SYM_DEF& symmetric,
43                                const TPMI_ALG_HASH& auth_hash,
44                                AuthorizationDelegate* authorization_delegate,
45                                const StartAuthSessionResponse& callback) {
46   StartAuthSessionShort(tpm_key, bind, nonce_caller, encrypted_salt,
47                         session_type, symmetric, auth_hash,
48                         authorization_delegate, callback);
49 }
50 
StartAuthSessionSync(const TPMI_DH_OBJECT & tpm_key,const std::string & tpm_key_name,const TPMI_DH_ENTITY & bind,const std::string & bind_name,const TPM2B_NONCE & nonce_caller,const TPM2B_ENCRYPTED_SECRET & encrypted_salt,const TPM_SE & session_type,const TPMT_SYM_DEF & symmetric,const TPMI_ALG_HASH & auth_hash,TPMI_SH_AUTH_SESSION * session_handle,TPM2B_NONCE * nonce_tpm,AuthorizationDelegate * authorization_delegate)51 TPM_RC MockTpm::StartAuthSessionSync(
52     const TPMI_DH_OBJECT& tpm_key,
53     const std::string& tpm_key_name,
54     const TPMI_DH_ENTITY& bind,
55     const std::string& bind_name,
56     const TPM2B_NONCE& nonce_caller,
57     const TPM2B_ENCRYPTED_SECRET& encrypted_salt,
58     const TPM_SE& session_type,
59     const TPMT_SYM_DEF& symmetric,
60     const TPMI_ALG_HASH& auth_hash,
61     TPMI_SH_AUTH_SESSION* session_handle,
62     TPM2B_NONCE* nonce_tpm,
63     AuthorizationDelegate* authorization_delegate) {
64   return StartAuthSessionSyncShort(
65       tpm_key, bind, nonce_caller, encrypted_salt, session_type, symmetric,
66       auth_hash, session_handle, nonce_tpm, authorization_delegate);
67 }
CreateSync(const TPMI_DH_OBJECT & parent_handle,const std::string & parent_handle_name,const TPM2B_SENSITIVE_CREATE & in_sensitive,const TPM2B_PUBLIC & in_public,const TPM2B_DATA & outside_info,const TPML_PCR_SELECTION & creation_pcr,TPM2B_PRIVATE * out_private,TPM2B_PUBLIC * out_public,TPM2B_CREATION_DATA * creation_data,TPM2B_DIGEST * creation_hash,TPMT_TK_CREATION * creation_ticket,AuthorizationDelegate * authorization_delegate)68 TPM_RC MockTpm::CreateSync(const TPMI_DH_OBJECT& parent_handle,
69                            const std::string& parent_handle_name,
70                            const TPM2B_SENSITIVE_CREATE& in_sensitive,
71                            const TPM2B_PUBLIC& in_public,
72                            const TPM2B_DATA& outside_info,
73                            const TPML_PCR_SELECTION& creation_pcr,
74                            TPM2B_PRIVATE* out_private,
75                            TPM2B_PUBLIC* out_public,
76                            TPM2B_CREATION_DATA* creation_data,
77                            TPM2B_DIGEST* creation_hash,
78                            TPMT_TK_CREATION* creation_ticket,
79                            AuthorizationDelegate* authorization_delegate) {
80   return CreateSyncShort(parent_handle, in_sensitive, in_public, creation_pcr,
81                          out_private, out_public, creation_data, creation_hash,
82                          creation_ticket, authorization_delegate);
83 }
CertifyCreationSync(const TPMI_DH_OBJECT & sign_handle,const std::string & sign_handle_name,const TPMI_DH_OBJECT & object_handle,const std::string & object_handle_name,const TPM2B_DATA & qualifying_data,const TPM2B_DIGEST & creation_hash,const TPMT_SIG_SCHEME & in_scheme,const TPMT_TK_CREATION & creation_ticket,TPM2B_ATTEST * certify_info,TPMT_SIGNATURE * signature,AuthorizationDelegate * authorization_delegate)84 TPM_RC MockTpm::CertifyCreationSync(
85     const TPMI_DH_OBJECT& sign_handle,
86     const std::string& sign_handle_name,
87     const TPMI_DH_OBJECT& object_handle,
88     const std::string& object_handle_name,
89     const TPM2B_DATA& qualifying_data,
90     const TPM2B_DIGEST& creation_hash,
91     const TPMT_SIG_SCHEME& in_scheme,
92     const TPMT_TK_CREATION& creation_ticket,
93     TPM2B_ATTEST* certify_info,
94     TPMT_SIGNATURE* signature,
95     AuthorizationDelegate* authorization_delegate) {
96   return CertifyCreationSyncShort(
97       sign_handle, object_handle, qualifying_data, creation_hash, in_scheme,
98       creation_ticket, certify_info, signature, authorization_delegate);
99 }
GetSessionAuditDigestSync(const TPMI_RH_ENDORSEMENT & privacy_admin_handle,const std::string & privacy_admin_handle_name,const TPMI_DH_OBJECT & sign_handle,const std::string & sign_handle_name,const TPMI_SH_HMAC & session_handle,const std::string & session_handle_name,const TPM2B_DATA & qualifying_data,const TPMT_SIG_SCHEME & in_scheme,TPM2B_ATTEST * audit_info,TPMT_SIGNATURE * signature,AuthorizationDelegate * authorization_delegate)100 TPM_RC MockTpm::GetSessionAuditDigestSync(
101     const TPMI_RH_ENDORSEMENT& privacy_admin_handle,
102     const std::string& privacy_admin_handle_name,
103     const TPMI_DH_OBJECT& sign_handle,
104     const std::string& sign_handle_name,
105     const TPMI_SH_HMAC& session_handle,
106     const std::string& session_handle_name,
107     const TPM2B_DATA& qualifying_data,
108     const TPMT_SIG_SCHEME& in_scheme,
109     TPM2B_ATTEST* audit_info,
110     TPMT_SIGNATURE* signature,
111     AuthorizationDelegate* authorization_delegate) {
112   return GetSessionAuditDigestSyncShort(
113       privacy_admin_handle, sign_handle, session_handle, qualifying_data,
114       in_scheme, audit_info, signature, authorization_delegate);
115 }
CommitSync(const TPMI_DH_OBJECT & sign_handle,const std::string & sign_handle_name,const UINT32 & param_size,const TPM2B_ECC_POINT & p1,const TPM2B_SENSITIVE_DATA & s2,const TPM2B_ECC_PARAMETER & y2,UINT32 * param_size_out,TPM2B_ECC_POINT * k,TPM2B_ECC_POINT * l,TPM2B_ECC_POINT * e,UINT16 * counter,AuthorizationDelegate * authorization_delegate)116 TPM_RC MockTpm::CommitSync(const TPMI_DH_OBJECT& sign_handle,
117                            const std::string& sign_handle_name,
118                            const UINT32& param_size,
119                            const TPM2B_ECC_POINT& p1,
120                            const TPM2B_SENSITIVE_DATA& s2,
121                            const TPM2B_ECC_PARAMETER& y2,
122                            UINT32* param_size_out,
123                            TPM2B_ECC_POINT* k,
124                            TPM2B_ECC_POINT* l,
125                            TPM2B_ECC_POINT* e,
126                            UINT16* counter,
127                            AuthorizationDelegate* authorization_delegate) {
128   return CommitSyncShort(sign_handle, param_size, p1, y2, param_size_out, k, l,
129                          e, counter, authorization_delegate);
130 }
PolicySigned(const TPMI_DH_OBJECT & auth_object,const std::string & auth_object_name,const TPMI_SH_POLICY & policy_session,const std::string & policy_session_name,const TPM2B_NONCE & nonce_tpm,const TPM2B_DIGEST & cp_hash_a,const TPM2B_NONCE & policy_ref,const INT32 & expiration,const TPMT_SIGNATURE & auth,AuthorizationDelegate * authorization_delegate,const PolicySignedResponse & callback)131 void MockTpm::PolicySigned(const TPMI_DH_OBJECT& auth_object,
132                            const std::string& auth_object_name,
133                            const TPMI_SH_POLICY& policy_session,
134                            const std::string& policy_session_name,
135                            const TPM2B_NONCE& nonce_tpm,
136                            const TPM2B_DIGEST& cp_hash_a,
137                            const TPM2B_NONCE& policy_ref,
138                            const INT32& expiration,
139                            const TPMT_SIGNATURE& auth,
140                            AuthorizationDelegate* authorization_delegate,
141                            const PolicySignedResponse& callback) {
142   PolicySignedShort(auth_object, policy_session, nonce_tpm, cp_hash_a,
143                     policy_ref, expiration, auth, authorization_delegate,
144                     callback);
145 }
PolicySignedSync(const TPMI_DH_OBJECT & auth_object,const std::string & auth_object_name,const TPMI_SH_POLICY & policy_session,const std::string & policy_session_name,const TPM2B_NONCE & nonce_tpm,const TPM2B_DIGEST & cp_hash_a,const TPM2B_NONCE & policy_ref,const INT32 & expiration,const TPMT_SIGNATURE & auth,TPM2B_TIMEOUT * timeout,TPMT_TK_AUTH * policy_ticket,AuthorizationDelegate * authorization_delegate)146 TPM_RC MockTpm::PolicySignedSync(
147     const TPMI_DH_OBJECT& auth_object,
148     const std::string& auth_object_name,
149     const TPMI_SH_POLICY& policy_session,
150     const std::string& policy_session_name,
151     const TPM2B_NONCE& nonce_tpm,
152     const TPM2B_DIGEST& cp_hash_a,
153     const TPM2B_NONCE& policy_ref,
154     const INT32& expiration,
155     const TPMT_SIGNATURE& auth,
156     TPM2B_TIMEOUT* timeout,
157     TPMT_TK_AUTH* policy_ticket,
158     AuthorizationDelegate* authorization_delegate) {
159   return PolicySignedSyncShort(auth_object, policy_session, nonce_tpm,
160                                cp_hash_a, policy_ref, expiration, auth, timeout,
161                                policy_ticket, authorization_delegate);
162 }
PolicySecretSync(const TPMI_DH_ENTITY & auth_handle,const std::string & auth_handle_name,const TPMI_SH_POLICY & policy_session,const std::string & policy_session_name,const TPM2B_NONCE & nonce_tpm,const TPM2B_DIGEST & cp_hash_a,const TPM2B_NONCE & policy_ref,const INT32 & expiration,TPM2B_TIMEOUT * timeout,TPMT_TK_AUTH * policy_ticket,AuthorizationDelegate * authorization_delegate)163 TPM_RC MockTpm::PolicySecretSync(
164     const TPMI_DH_ENTITY& auth_handle,
165     const std::string& auth_handle_name,
166     const TPMI_SH_POLICY& policy_session,
167     const std::string& policy_session_name,
168     const TPM2B_NONCE& nonce_tpm,
169     const TPM2B_DIGEST& cp_hash_a,
170     const TPM2B_NONCE& policy_ref,
171     const INT32& expiration,
172     TPM2B_TIMEOUT* timeout,
173     TPMT_TK_AUTH* policy_ticket,
174     AuthorizationDelegate* authorization_delegate) {
175   return PolicySecretSyncShort(auth_handle, policy_session, nonce_tpm,
176                                cp_hash_a, policy_ref, expiration, timeout,
177                                policy_ticket, authorization_delegate);
178 }
PolicyNV(const TPMI_RH_NV_AUTH & auth_handle,const std::string & auth_handle_name,const TPMI_RH_NV_INDEX & nv_index,const std::string & nv_index_name,const TPMI_SH_POLICY & policy_session,const std::string & policy_session_name,const TPM2B_OPERAND & operand_b,const UINT16 & offset,const TPM_EO & operation,AuthorizationDelegate * authorization_delegate,const PolicyNVResponse & callback)179 void MockTpm::PolicyNV(const TPMI_RH_NV_AUTH& auth_handle,
180                        const std::string& auth_handle_name,
181                        const TPMI_RH_NV_INDEX& nv_index,
182                        const std::string& nv_index_name,
183                        const TPMI_SH_POLICY& policy_session,
184                        const std::string& policy_session_name,
185                        const TPM2B_OPERAND& operand_b,
186                        const UINT16& offset,
187                        const TPM_EO& operation,
188                        AuthorizationDelegate* authorization_delegate,
189                        const PolicyNVResponse& callback) {
190   PolicyNVShort(auth_handle, nv_index, policy_session, operand_b, offset,
191                 operation, authorization_delegate, callback);
192 }
CreatePrimarySync(const TPMI_RH_HIERARCHY & primary_handle,const std::string & primary_handle_name,const TPM2B_SENSITIVE_CREATE & in_sensitive,const TPM2B_PUBLIC & in_public,const TPM2B_DATA & outside_info,const TPML_PCR_SELECTION & creation_pcr,TPM_HANDLE * object_handle,TPM2B_PUBLIC * out_public,TPM2B_CREATION_DATA * creation_data,TPM2B_DIGEST * creation_hash,TPMT_TK_CREATION * creation_ticket,TPM2B_NAME * name,AuthorizationDelegate * authorization_delegate)193 TPM_RC MockTpm::CreatePrimarySync(
194     const TPMI_RH_HIERARCHY& primary_handle,
195     const std::string& primary_handle_name,
196     const TPM2B_SENSITIVE_CREATE& in_sensitive,
197     const TPM2B_PUBLIC& in_public,
198     const TPM2B_DATA& outside_info,
199     const TPML_PCR_SELECTION& creation_pcr,
200     TPM_HANDLE* object_handle,
201     TPM2B_PUBLIC* out_public,
202     TPM2B_CREATION_DATA* creation_data,
203     TPM2B_DIGEST* creation_hash,
204     TPMT_TK_CREATION* creation_ticket,
205     TPM2B_NAME* name,
206     AuthorizationDelegate* authorization_delegate) {
207   return CreatePrimarySyncShort(primary_handle, in_public, creation_pcr,
208                                 object_handle, out_public, creation_data,
209                                 creation_hash, creation_ticket, name,
210                                 authorization_delegate);
211 }
NV_Certify(const TPMI_DH_OBJECT & sign_handle,const std::string & sign_handle_name,const TPMI_RH_NV_AUTH & auth_handle,const std::string & auth_handle_name,const TPMI_RH_NV_INDEX & nv_index,const std::string & nv_index_name,const TPM2B_DATA & qualifying_data,const TPMT_SIG_SCHEME & in_scheme,const UINT16 & size,const UINT16 & offset,AuthorizationDelegate * authorization_delegate,const NV_CertifyResponse & callback)212 void MockTpm::NV_Certify(const TPMI_DH_OBJECT& sign_handle,
213                          const std::string& sign_handle_name,
214                          const TPMI_RH_NV_AUTH& auth_handle,
215                          const std::string& auth_handle_name,
216                          const TPMI_RH_NV_INDEX& nv_index,
217                          const std::string& nv_index_name,
218                          const TPM2B_DATA& qualifying_data,
219                          const TPMT_SIG_SCHEME& in_scheme,
220                          const UINT16& size,
221                          const UINT16& offset,
222                          AuthorizationDelegate* authorization_delegate,
223                          const NV_CertifyResponse& callback) {
224   NV_CertifyShort(sign_handle, auth_handle, nv_index, qualifying_data,
225                   in_scheme, size, offset, authorization_delegate, callback);
226 }
NV_CertifySync(const TPMI_DH_OBJECT & sign_handle,const std::string & sign_handle_name,const TPMI_RH_NV_AUTH & auth_handle,const std::string & auth_handle_name,const TPMI_RH_NV_INDEX & nv_index,const std::string & nv_index_name,const TPM2B_DATA & qualifying_data,const TPMT_SIG_SCHEME & in_scheme,const UINT16 & size,const UINT16 & offset,TPM2B_ATTEST * certify_info,TPMT_SIGNATURE * signature,AuthorizationDelegate * authorization_delegate)227 TPM_RC MockTpm::NV_CertifySync(const TPMI_DH_OBJECT& sign_handle,
228                                const std::string& sign_handle_name,
229                                const TPMI_RH_NV_AUTH& auth_handle,
230                                const std::string& auth_handle_name,
231                                const TPMI_RH_NV_INDEX& nv_index,
232                                const std::string& nv_index_name,
233                                const TPM2B_DATA& qualifying_data,
234                                const TPMT_SIG_SCHEME& in_scheme,
235                                const UINT16& size,
236                                const UINT16& offset,
237                                TPM2B_ATTEST* certify_info,
238                                TPMT_SIGNATURE* signature,
239                                AuthorizationDelegate* authorization_delegate) {
240   return NV_CertifySyncShort(sign_handle, auth_handle, nv_index,
241                              qualifying_data, in_scheme, size, offset,
242                              certify_info, signature, authorization_delegate);
243 }
244 
245 }  // namespace trunks
246