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 28 MockTpm::MockTpm() : Tpm(nullptr) { 29 ON_CALL(*this, PCR_AllocateSync(_, _, _, _, _, _, _, _)) 30 .WillByDefault(DoAll(SetArgPointee<3>(YES), 31 Return(TPM_RC_SUCCESS))); 32 } 33 34 MockTpm::~MockTpm() {} 35 36 void MockTpm::StartAuthSession( 37 const TPMI_DH_OBJECT& tpm_key, 38 const std::string& tpm_key_name, 39 const TPMI_DH_ENTITY& bind, 40 const std::string& bind_name, 41 const TPM2B_NONCE& nonce_caller, 42 const TPM2B_ENCRYPTED_SECRET& encrypted_salt, 43 const TPM_SE& session_type, 44 const TPMT_SYM_DEF& symmetric, 45 const TPMI_ALG_HASH& auth_hash, 46 AuthorizationDelegate* authorization_delegate, 47 const StartAuthSessionResponse& callback) { 48 StartAuthSessionShort(tpm_key, bind, nonce_caller, encrypted_salt, 49 session_type, symmetric, auth_hash, 50 authorization_delegate, callback); 51 } 52 53 TPM_RC MockTpm::StartAuthSessionSync( 54 const TPMI_DH_OBJECT& tpm_key, 55 const std::string& tpm_key_name, 56 const TPMI_DH_ENTITY& bind, 57 const std::string& bind_name, 58 const TPM2B_NONCE& nonce_caller, 59 const TPM2B_ENCRYPTED_SECRET& encrypted_salt, 60 const TPM_SE& session_type, 61 const TPMT_SYM_DEF& symmetric, 62 const TPMI_ALG_HASH& auth_hash, 63 TPMI_SH_AUTH_SESSION* session_handle, 64 TPM2B_NONCE* nonce_tpm, 65 AuthorizationDelegate* authorization_delegate) { 66 return StartAuthSessionSyncShort(tpm_key, bind, nonce_caller, 67 encrypted_salt, session_type, symmetric, 68 auth_hash, session_handle, nonce_tpm, 69 authorization_delegate); 70 } 71 TPM_RC MockTpm::CreateSync( 72 const TPMI_DH_OBJECT& parent_handle, 73 const std::string& parent_handle_name, 74 const TPM2B_SENSITIVE_CREATE& in_sensitive, 75 const TPM2B_PUBLIC& in_public, 76 const TPM2B_DATA& outside_info, 77 const TPML_PCR_SELECTION& creation_pcr, 78 TPM2B_PRIVATE* out_private, 79 TPM2B_PUBLIC* out_public, 80 TPM2B_CREATION_DATA* creation_data, 81 TPM2B_DIGEST* creation_hash, 82 TPMT_TK_CREATION* creation_ticket, 83 AuthorizationDelegate* authorization_delegate) { 84 return CreateSyncShort(parent_handle, in_sensitive, in_public, creation_pcr, 85 out_private, out_public, creation_data, 86 creation_hash, creation_ticket, 87 authorization_delegate); 88 } 89 TPM_RC MockTpm::CertifyCreationSync( 90 const TPMI_DH_OBJECT& sign_handle, 91 const std::string& sign_handle_name, 92 const TPMI_DH_OBJECT& object_handle, 93 const std::string& object_handle_name, 94 const TPM2B_DATA& qualifying_data, 95 const TPM2B_DIGEST& creation_hash, 96 const TPMT_SIG_SCHEME& in_scheme, 97 const TPMT_TK_CREATION& creation_ticket, 98 TPM2B_ATTEST* certify_info, 99 TPMT_SIGNATURE* signature, 100 AuthorizationDelegate* authorization_delegate) { 101 return CertifyCreationSyncShort(sign_handle, object_handle, 102 qualifying_data, creation_hash, in_scheme, 103 creation_ticket, certify_info, signature, 104 authorization_delegate); 105 } 106 TPM_RC MockTpm::GetSessionAuditDigestSync( 107 const TPMI_RH_ENDORSEMENT& privacy_admin_handle, 108 const std::string& privacy_admin_handle_name, 109 const TPMI_DH_OBJECT& sign_handle, 110 const std::string& sign_handle_name, 111 const TPMI_SH_HMAC& session_handle, 112 const std::string& session_handle_name, 113 const TPM2B_DATA& qualifying_data, 114 const TPMT_SIG_SCHEME& in_scheme, 115 TPM2B_ATTEST* audit_info, 116 TPMT_SIGNATURE* signature, 117 AuthorizationDelegate* authorization_delegate) { 118 return GetSessionAuditDigestSyncShort(privacy_admin_handle, sign_handle, 119 session_handle, qualifying_data, 120 in_scheme, audit_info, signature, 121 authorization_delegate); 122 } 123 TPM_RC MockTpm::CommitSync( 124 const TPMI_DH_OBJECT& sign_handle, 125 const std::string& sign_handle_name, 126 const UINT32& param_size, 127 const TPM2B_ECC_POINT& p1, 128 const TPM2B_SENSITIVE_DATA& s2, 129 const TPM2B_ECC_PARAMETER& y2, 130 UINT32* param_size_out, 131 TPM2B_ECC_POINT* k, 132 TPM2B_ECC_POINT* l, 133 TPM2B_ECC_POINT* e, 134 UINT16* counter, 135 AuthorizationDelegate* authorization_delegate) { 136 return CommitSyncShort(sign_handle, param_size, p1, y2, param_size_out, k, 137 l, e, counter, authorization_delegate); 138 } 139 void MockTpm::PolicySigned( 140 const TPMI_DH_OBJECT& auth_object, 141 const std::string& auth_object_name, 142 const TPMI_SH_POLICY& policy_session, 143 const std::string& policy_session_name, 144 const TPM2B_NONCE& nonce_tpm, 145 const TPM2B_DIGEST& cp_hash_a, 146 const TPM2B_NONCE& policy_ref, 147 const INT32& expiration, 148 const TPMT_SIGNATURE& auth, 149 AuthorizationDelegate* authorization_delegate, 150 const PolicySignedResponse& callback) { 151 PolicySignedShort(auth_object, policy_session, nonce_tpm, cp_hash_a, 152 policy_ref, expiration, auth, authorization_delegate, 153 callback); 154 } 155 TPM_RC MockTpm::PolicySignedSync( 156 const TPMI_DH_OBJECT& auth_object, 157 const std::string& auth_object_name, 158 const TPMI_SH_POLICY& policy_session, 159 const std::string& policy_session_name, 160 const TPM2B_NONCE& nonce_tpm, 161 const TPM2B_DIGEST& cp_hash_a, 162 const TPM2B_NONCE& policy_ref, 163 const INT32& expiration, 164 const TPMT_SIGNATURE& auth, 165 TPM2B_TIMEOUT* timeout, 166 TPMT_TK_AUTH* policy_ticket, 167 AuthorizationDelegate* authorization_delegate) { 168 return PolicySignedSyncShort(auth_object, policy_session, nonce_tpm, 169 cp_hash_a, policy_ref, expiration, auth, timeout, 170 policy_ticket, authorization_delegate); 171 } 172 TPM_RC MockTpm::PolicySecretSync( 173 const TPMI_DH_ENTITY& auth_handle, 174 const std::string& auth_handle_name, 175 const TPMI_SH_POLICY& policy_session, 176 const std::string& policy_session_name, 177 const TPM2B_NONCE& nonce_tpm, 178 const TPM2B_DIGEST& cp_hash_a, 179 const TPM2B_NONCE& policy_ref, 180 const INT32& expiration, 181 TPM2B_TIMEOUT* timeout, 182 TPMT_TK_AUTH* policy_ticket, 183 AuthorizationDelegate* authorization_delegate) { 184 return PolicySecretSyncShort(auth_handle, policy_session, nonce_tpm, 185 cp_hash_a, policy_ref, expiration, timeout, 186 policy_ticket, authorization_delegate); 187 } 188 void MockTpm::PolicyNV(const TPMI_RH_NV_AUTH& auth_handle, 189 const std::string& auth_handle_name, 190 const TPMI_RH_NV_INDEX& nv_index, 191 const std::string& nv_index_name, 192 const TPMI_SH_POLICY& policy_session, 193 const std::string& policy_session_name, 194 const TPM2B_OPERAND& operand_b, 195 const UINT16& offset, 196 const TPM_EO& operation, 197 AuthorizationDelegate* authorization_delegate, 198 const PolicyNVResponse& callback) { 199 PolicyNVShort(auth_handle, nv_index, policy_session, operand_b, offset, 200 operation, authorization_delegate, callback); 201 } 202 TPM_RC MockTpm::CreatePrimarySync( 203 const TPMI_RH_HIERARCHY& primary_handle, 204 const std::string& primary_handle_name, 205 const TPM2B_SENSITIVE_CREATE& in_sensitive, 206 const TPM2B_PUBLIC& in_public, 207 const TPM2B_DATA& outside_info, 208 const TPML_PCR_SELECTION& creation_pcr, 209 TPM_HANDLE* object_handle, 210 TPM2B_PUBLIC* out_public, 211 TPM2B_CREATION_DATA* creation_data, 212 TPM2B_DIGEST* creation_hash, 213 TPMT_TK_CREATION* creation_ticket, 214 TPM2B_NAME* name, 215 AuthorizationDelegate* authorization_delegate) { 216 return CreatePrimarySyncShort(primary_handle, in_public, creation_pcr, 217 object_handle, out_public, creation_data, 218 creation_hash, creation_ticket, name, 219 authorization_delegate); 220 } 221 void MockTpm::NV_Certify(const TPMI_DH_OBJECT& sign_handle, 222 const std::string& sign_handle_name, 223 const TPMI_RH_NV_AUTH& auth_handle, 224 const std::string& auth_handle_name, 225 const TPMI_RH_NV_INDEX& nv_index, 226 const std::string& nv_index_name, 227 const TPM2B_DATA& qualifying_data, 228 const TPMT_SIG_SCHEME& in_scheme, 229 const UINT16& size, 230 const UINT16& offset, 231 AuthorizationDelegate* authorization_delegate, 232 const NV_CertifyResponse& callback) { 233 NV_CertifyShort(sign_handle, auth_handle, nv_index, qualifying_data, 234 in_scheme, size, offset, authorization_delegate, callback); 235 } 236 TPM_RC MockTpm::NV_CertifySync( 237 const TPMI_DH_OBJECT& sign_handle, 238 const std::string& sign_handle_name, 239 const TPMI_RH_NV_AUTH& auth_handle, 240 const std::string& auth_handle_name, 241 const TPMI_RH_NV_INDEX& nv_index, 242 const std::string& nv_index_name, 243 const TPM2B_DATA& qualifying_data, 244 const TPMT_SIG_SCHEME& in_scheme, 245 const UINT16& size, 246 const UINT16& offset, 247 TPM2B_ATTEST* certify_info, 248 TPMT_SIGNATURE* signature, 249 AuthorizationDelegate* authorization_delegate) { 250 return NV_CertifySyncShort(sign_handle, auth_handle, nv_index, 251 qualifying_data, in_scheme, size, offset, 252 certify_info, signature, authorization_delegate); 253 } 254 255 } // namespace trunks 256