• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
FinalizeTcti(TSS2_TCTI_CONTEXT * tcti_context)23 static void FinalizeTcti(TSS2_TCTI_CONTEXT* tcti_context) {
24   if (tcti_context == nullptr) {
25     return;
26   }
27   auto finalize_fn = TSS2_TCTI_FINALIZE(tcti_context);
28   if (finalize_fn != nullptr) {
29     finalize_fn(tcti_context);
30   }
31   delete[] (char*) tcti_context;
32 }
33 
DeviceTpm(const std::string & path)34 DeviceTpm::DeviceTpm(const std::string& path) : tpm_(nullptr, &FinalizeTcti) {
35   size_t size = 0;
36   auto rc = Tss2_Tcti_Device_Init(nullptr, &size, path.c_str());
37   if (rc != TSS2_RC_SUCCESS) {
38     LOG(ERROR) << "Could not get Device TCTI size: " << Tss2_RC_Decode(rc)
39                << "(" << rc << ")";
40     return;
41   }
42   tpm_.reset(reinterpret_cast<TSS2_TCTI_CONTEXT*>(new char[size]));
43   rc = Tss2_Tcti_Device_Init(tpm_.get(), &size, path.c_str());
44   if (rc != TSS2_RC_SUCCESS) {
45     LOG(ERROR) << "Could not create Device TCTI: " << Tss2_RC_Decode(rc)
46                << "(" << rc << ")";
47     delete[] (char*) tpm_.release();
48     return;
49   }
50 }
51 
TctiContext()52 TSS2_TCTI_CONTEXT* DeviceTpm::TctiContext() {
53   return tpm_.get();
54 }
55