summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDarren Krahn <dkrahn@google.com>2015-12-17 23:54:31 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2015-12-17 23:54:31 +0000
commitbd0dc3c259a6da35a17d5fa6962b25b6a52a1a3d (patch)
treef06f46011644232134f40e76f0304ca87d4c659b
parent01f9f5ee5a279a763b32c5fe10780ae2f4579f7f (diff)
parentbf8d6235ca6a0f2afa2e10b2484aa93023c56398 (diff)
downloadtpm-bd0dc3c259a6da35a17d5fa6962b25b6a52a1a3d.tar.gz
Merge "trunks: Convert the simulator handle to in-process."
-rw-r--r--trunks/tpm_simulator_handle.cc110
-rw-r--r--trunks/tpm_simulator_handle.h12
2 files changed, 39 insertions, 83 deletions
diff --git a/trunks/tpm_simulator_handle.cc b/trunks/tpm_simulator_handle.cc
index 5e26651..4402a95 100644
--- a/trunks/tpm_simulator_handle.cc
+++ b/trunks/tpm_simulator_handle.cc
@@ -16,61 +16,42 @@
#include "trunks/tpm_simulator_handle.h"
-#include <fcntl.h>
#include <unistd.h>
+#ifdef USE_SIMULATOR
+extern "C" {
+#include <tpm2/TpmBuildSwitches.h>
+#include <tpm2/_TPM_Init_fp.h>
+#include <tpm2/ExecCommand_fp.h>
+#include <tpm2/Manufacture_fp.h>
+#include <tpm2/Platform.h>
+} // extern "C"
+#endif // USE_SIMULATOR
+
#include <base/callback.h>
#include <base/logging.h>
-#include <base/posix/eintr_wrapper.h>
-
-namespace {
-
-const char kTpmSimRequestFile[] = "/dev/tpm-req";
-const char kTpmSimResponseFile[] = "/dev/tpm-resp";
-const uint32_t kTpmBufferSize = 4096;
-const int kInvalidFileDescriptor = -1;
+#include <base/stl_util.h>
-} // namespace
+#include "trunks/error_codes.h"
namespace trunks {
-TpmSimulatorHandle::TpmSimulatorHandle() :
- req_fd_(kInvalidFileDescriptor), resp_fd_(kInvalidFileDescriptor) {}
+TpmSimulatorHandle::TpmSimulatorHandle() {}
-TpmSimulatorHandle::~TpmSimulatorHandle() {
- int result = IGNORE_EINTR(close(req_fd_));
- if (result == -1) {
- PLOG(ERROR) << "TPM: couldn't close " << kTpmSimRequestFile;
- } else {
- LOG(INFO) << "TPM: " << kTpmSimRequestFile << " closed successfully";
- }
- result = IGNORE_EINTR(close(resp_fd_));
- if (result == -1) {
- PLOG(ERROR) << "TPM: couldn't close " << kTpmSimResponseFile;
- } else {
- LOG(INFO) << "TPM: " << kTpmSimResponseFile << " closed successfully";
- }
-}
+TpmSimulatorHandle::~TpmSimulatorHandle() {}
bool TpmSimulatorHandle::Init() {
- if (req_fd_ == kInvalidFileDescriptor) {
- req_fd_ = HANDLE_EINTR(open("/dev/tpm-req", O_RDWR));
- if (req_fd_ == kInvalidFileDescriptor) {
- PLOG(ERROR) << "TPM: Error opening file descriptor at "
- << kTpmSimRequestFile;
- return false;
- }
- LOG(INFO) << "TPM: " << kTpmSimRequestFile << " opened successfully";
- }
- if (resp_fd_ == kInvalidFileDescriptor) {
- resp_fd_ = HANDLE_EINTR(open("/dev/tpm-resp", O_RDWR));
- if (resp_fd_ == kInvalidFileDescriptor) {
- PLOG(ERROR) << "TPM: Error opening file descriptor at "
- << kTpmSimResponseFile;
- return false;
- }
- LOG(INFO) << "TPM: " << kTpmSimResponseFile << " opened successfully";
- }
+#ifdef USE_SIMULATOR
+ // Initialize TPM.
+ CHECK_EQ(chdir("/data/misc/trunksd"), 0);
+ _plat__Signal_PowerOn();
+ _TPM_Init();
+ _plat__SetNvAvail();
+ CHECK_EQ(TPM_Manufacture(TRUE), 0);
+ LOG(INFO) << "Simulator initialized.";
+#else
+ LOG(FATAL) << "Simulator not configured.";
+#endif
return true;
}
@@ -80,36 +61,17 @@ void TpmSimulatorHandle::SendCommand(const std::string& command,
}
std::string TpmSimulatorHandle::SendCommandAndWait(const std::string& command) {
- std::string response;
- TPM_RC result = SendCommandInternal(command, &response);
- if (result != TPM_RC_SUCCESS) {
- response = CreateErrorResponse(result);
- }
- return response;
-}
-
-TPM_RC TpmSimulatorHandle::SendCommandInternal(const std::string& command,
- std::string* response) {
- CHECK_NE(req_fd_, kInvalidFileDescriptor);
- CHECK_NE(resp_fd_, kInvalidFileDescriptor);
- int result = HANDLE_EINTR(write(req_fd_, command.data(), command.length()));
- if (result < 0) {
- PLOG(ERROR) << "TPM: Error writing to TPM simulator request handle.";
- return TRUNKS_RC_WRITE_ERROR;
- }
- if (static_cast<size_t>(result) != command.length()) {
- LOG(ERROR) << "TPM: Error writing to TPM simulator request handle: "
- << result << " vs " << command.length();
- return TRUNKS_RC_WRITE_ERROR;
- }
- char response_buf[kTpmBufferSize];
- result = HANDLE_EINTR(read(resp_fd_, response_buf, kTpmBufferSize));
- if (result < 0) {
- PLOG(ERROR) << "TPM: Error reading from TPM simulator response handle.";
- return TRUNKS_RC_READ_ERROR;
- }
- response->assign(response_buf, static_cast<size_t>(result));
- return TPM_RC_SUCCESS;
+#ifdef USE_SIMULATOR
+ unsigned int response_size;
+ unsigned char* response;
+ std::string mutable_command(command);
+ ExecuteCommand(command.size(), reinterpret_cast<unsigned char*>(
+ string_as_array(&mutable_command)),
+ &response_size, &response);
+ return std::string(reinterpret_cast<char*>(response), response_size);
+#else
+ return CreateErrorResponse(TCTI_RC_GENERAL_FAILURE);
+#endif
}
} // namespace trunks
diff --git a/trunks/tpm_simulator_handle.h b/trunks/tpm_simulator_handle.h
index 1b4d230..24c65ca 100644
--- a/trunks/tpm_simulator_handle.h
+++ b/trunks/tpm_simulator_handle.h
@@ -20,13 +20,13 @@
#include "trunks/command_transceiver.h"
#include <string>
+#include <vector>
#include "trunks/error_codes.h"
namespace trunks {
-// Sends command requests to a software TPM via a handle to /dev/tpm-req.
-// Receives command responses via a handle to /dev/tpm-resp. All commands are
+// Sends command requests to an in-process software TPM. All commands are
// sent synchronously. The SendCommand method is supported but does not return
// until a response is received and the callback has been called. Command and
// response data are opaque to this class; it performs no validation.
@@ -50,13 +50,7 @@ class TpmSimulatorHandle : public CommandTransceiver {
std::string SendCommandAndWait(const std::string& command) override;
private:
- // Writes a |command| to /dev/tpm-req and reads the |response| from
- // /dev/tpm-resp. Returns TPM_RC_SUCCESS on success.
- TPM_RC SendCommandInternal(const std::string& command, std::string* response);
-
- int req_fd_; // A file descriptor for /dev/tpm-req.
- int resp_fd_; // A file descriptor for /dev/tpm-resp.
-
+ std::vector<unsigned char> command_buffer;
DISALLOW_COPY_AND_ASSIGN(TpmSimulatorHandle);
};