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
17 #include <stdio.h>
18
19 #include <android-base/endian.h>
20 #include <gflags/gflags.h>
21 #include <android-base/logging.h>
22
23 #include "host/libs/config/cuttlefish_config.h"
24 #include "host/libs/config/logging.h"
25 #include "common/libs/fs/shared_buf.h"
26 #include "common/libs/fs/shared_fd.h"
27
28 DEFINE_string(device, "", "The device file for the host TPM.");
29 DEFINE_int32(server_fd, -1, "A server file descriptor to accept guest tpm "
30 "connections.");
31
32 namespace {
33
HandleClient(cuttlefish::SharedFD client,cuttlefish::SharedFD device)34 void HandleClient(cuttlefish::SharedFD client, cuttlefish::SharedFD device) {
35 while (true) {
36 // TPM2 simulator command protocol.
37 std::vector<char> command_bytes(4, 0);
38 CHECK(cuttlefish::ReadExact(client, &command_bytes) == 4) << "Could not receive TPM_SEND_COMMAND";
39 std::uint32_t command_received =
40 betoh32(*reinterpret_cast<std::uint32_t*>(command_bytes.data()));
41 CHECK(command_received == 8)
42 << "Command received was not TPM_SEND_COMMAND, instead got " << command_received;
43 std::vector<char> locality {0};
44 CHECK(cuttlefish::ReadExact(client, &locality) == 1) << "Could not receive locality";
45 std::vector<char> length_bytes(4, 0);
46 CHECK(cuttlefish::ReadExact(client, &length_bytes) == 4) << "Could not receive command length";
47 std::vector<char> command(betoh32(*reinterpret_cast<std::uint32_t*>(length_bytes.data())), 0);
48 CHECK(cuttlefish::ReadExact(client, &command) == command.size()) << "Could not read TPM message";
49
50 CHECK(device->Write(command.data(), command.size()) == command.size())
51 << "Could not write TPM command to host device: " << device->StrError();
52
53 std::string tpm_response;
54 CHECK(cuttlefish::ReadAll(device, &tpm_response) >= 0)
55 << "host TPM gave an IO error: " << device->StrError();
56
57 *reinterpret_cast<std::uint32_t*>(length_bytes.data()) = htobe32(tpm_response.size());
58 CHECK(cuttlefish::WriteAll(client, length_bytes) == 4)
59 << "Could not send response length: " << client->StrError();
60 CHECK(cuttlefish::WriteAll(client, tpm_response) == tpm_response.size())
61 << "Could not send response message: " << client->StrError();
62 std::vector<char> parity = {0, 0, 0, 0};
63 CHECK(cuttlefish::WriteAll(client, parity) == 4)
64 << "Could not send parity bytes: " << client->StrError();
65 }
66 }
67
68 } // namespace
69
main(int argc,char ** argv)70 int main(int argc, char** argv) {
71 cuttlefish::DefaultSubprocessLogging(argv);
72 google::ParseCommandLineFlags(&argc, &argv, true);
73
74 CHECK(!FLAGS_device.empty()) << "A device must be set.";
75 CHECK(FLAGS_server_fd > -1) << "A server fd must be given.";
76
77 auto server = cuttlefish::SharedFD::Dup(FLAGS_server_fd);
78 close(FLAGS_server_fd);
79 CHECK(server->IsOpen()) << "Could not dup vsock server fd: " << server->StrError();
80
81 auto device = cuttlefish::SharedFD::Open(FLAGS_device.c_str(), O_RDWR);
82 CHECK(device->IsOpen()) << "Could not open " << FLAGS_device << ": " << device->StrError();
83
84 while (true) {
85 auto client = cuttlefish::SharedFD::Accept(*server);
86 CHECK(client->IsOpen()) << "Could not accept TPM client: " << client->StrError();
87 HandleClient(client, device);
88 }
89 }
90