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 "in_process_tpm.h"
17
18 #include <endian.h>
19 #include <stddef.h>
20
21 #include <tss2/tss2_rc.h>
22
23 #include "host/commands/secure_env/tpm_commands.h"
24
25 extern "C" {
26 typedef int SOCKET;
27 #include "TpmBuildSwitches.h"
28 #include "TpmTcpProtocol.h"
29 #include "Simulator_fp.h"
30 #include "Manufacture_fp.h"
31 #define delete delete_
32 #include "Platform_fp.h"
33 #undef delete
34 #undef DEBUG
35 }
36
37 #include <android-base/logging.h>
38
39 struct __attribute__((__packed__)) tpm_message_header {
40 uint16_t tag;
41 uint32_t length;
42 uint32_t ordinal;
43 };
44
TpmFromContext(TSS2_TCTI_CONTEXT * context)45 InProcessTpm* InProcessTpm::TpmFromContext(TSS2_TCTI_CONTEXT* context) {
46 auto offset = offsetof(InProcessTpm, tcti_context_);
47 char* context_char = reinterpret_cast<char*>(context);
48 return reinterpret_cast<InProcessTpm*>(context_char - offset);
49 }
50
Transmit(TSS2_TCTI_CONTEXT * context,size_t size,uint8_t const * command)51 static TSS2_RC Transmit(
52 TSS2_TCTI_CONTEXT *context, size_t size, uint8_t const *command) {
53 return InProcessTpm::TpmFromContext(context)->Transmit(size, command);
54 }
55
Receive(TSS2_TCTI_CONTEXT * context,size_t * size,uint8_t * response,int32_t timeout)56 static TSS2_RC Receive(
57 TSS2_TCTI_CONTEXT *context,
58 size_t* size,
59 uint8_t* response,
60 int32_t timeout) {
61 return
62 InProcessTpm::TpmFromContext(context)->Receive(size, response, timeout);
63 }
64
InProcessTpm()65 InProcessTpm::InProcessTpm() {
66 tcti_context_.v1.magic = 0xFAD;
67 tcti_context_.v1.version = 1;
68 tcti_context_.v1.transmit = ::Transmit;
69 tcti_context_.v1.receive = ::Receive;
70 _plat__NVEnable(NULL);
71 if (_plat__NVNeedsManufacture()) {
72 // Can't use android logging here due to a macro conflict with TPM internals
73 LOG(DEBUG) << "Manufacturing TPM state";
74 if (TPM_Manufacture(1)) {
75 LOG(FATAL) << "Failed to manufacture TPM state";
76 }
77 }
78 _rpc__Signal_PowerOn(false);
79 _rpc__Signal_NvOn();
80 }
81
~InProcessTpm()82 InProcessTpm::~InProcessTpm() {
83 _rpc__Signal_NvOff();
84 _rpc__Signal_PowerOff();
85 }
86
TctiContext()87 TSS2_TCTI_CONTEXT* InProcessTpm::TctiContext() {
88 return reinterpret_cast<TSS2_TCTI_CONTEXT*>(&tcti_context_);
89 }
90
Transmit(size_t size,uint8_t const * command)91 TSS2_RC InProcessTpm::Transmit(size_t size, uint8_t const* command) {
92 std::lock_guard lock(queue_mutex_);
93 command_queue_.emplace_back(command, command + size);
94 return TSS2_RC_SUCCESS;
95 }
96
Receive(size_t * size,uint8_t * response,int32_t)97 TSS2_RC InProcessTpm::Receive(size_t* size, uint8_t* response, int32_t) {
98 // TODO(schuffelen): Use the timeout argument
99 std::vector<uint8_t> request;
100 {
101 std::lock_guard lock(queue_mutex_);
102 if (command_queue_.empty()) {
103 return TSS2_TCTI_RC_GENERAL_FAILURE;
104 }
105 request = std::move(command_queue_.front());
106 command_queue_.pop_front();
107 }
108 auto header = reinterpret_cast<tpm_message_header*>(request.data());
109 LOG(VERBOSE) << "Sending TPM command "
110 << TpmCommandName(be32toh(header->ordinal));
111 _IN_BUFFER input = {
112 .BufferSize = request.size(),
113 .Buffer = request.data(),
114 };
115 _OUT_BUFFER output = {
116 .BufferSize = (uint32_t) *size,
117 .Buffer = response,
118 };
119 _rpc__Send_Command(3, input, &output);
120 *size = output.BufferSize;
121 header = reinterpret_cast<tpm_message_header*>(response);
122 auto rc = be32toh(header->ordinal);
123 LOG(VERBOSE) << "Received TPM response " << Tss2_RC_Decode(rc)
124 << " (" << rc << ")";
125 return TSS2_RC_SUCCESS;
126 }
127