1 // 2 // Copyright (C) 2020 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 #include "host/commands/secure_env/device_tpm.h" 17 18 #include <android-base/logging.h> 19 #include <tss2/tss2_rc.h> 20 #include <tss2/tss2_tcti.h> 21 #include <tss2/tss2_tcti_device.h> 22 23 namespace cuttlefish { 24 FinalizeTcti(TSS2_TCTI_CONTEXT * tcti_context)25static void FinalizeTcti(TSS2_TCTI_CONTEXT* tcti_context) { 26 if (tcti_context == nullptr) { 27 return; 28 } 29 auto finalize_fn = TSS2_TCTI_FINALIZE(tcti_context); 30 if (finalize_fn != nullptr) { 31 finalize_fn(tcti_context); 32 } 33 delete[] (char*) tcti_context; 34 } 35 DeviceTpm(const std::string & path)36DeviceTpm::DeviceTpm(const std::string& path) : tpm_(nullptr, &FinalizeTcti) { 37 size_t size = 0; 38 auto rc = Tss2_Tcti_Device_Init(nullptr, &size, path.c_str()); 39 if (rc != TSS2_RC_SUCCESS) { 40 LOG(ERROR) << "Could not get Device TCTI size: " << Tss2_RC_Decode(rc) 41 << "(" << rc << ")"; 42 return; 43 } 44 tpm_.reset(reinterpret_cast<TSS2_TCTI_CONTEXT*>(new char[size])); 45 rc = Tss2_Tcti_Device_Init(tpm_.get(), &size, path.c_str()); 46 if (rc != TSS2_RC_SUCCESS) { 47 LOG(ERROR) << "Could not create Device TCTI: " << Tss2_RC_Decode(rc) 48 << "(" << rc << ")"; 49 delete[] (char*) tpm_.release(); 50 return; 51 } 52 } 53 TctiContext()54TSS2_TCTI_CONTEXT* DeviceTpm::TctiContext() { 55 return tpm_.get(); 56 } 57 58 } // namespace cuttlefish 59