diff options
author | Utkarsh Sanghi <usanghi@google.com> | 2015-10-20 09:31:56 -0700 |
---|---|---|
committer | Utkarsh Sanghi <usanghi@google.com> | 2015-10-26 21:57:43 +0000 |
commit | c9429017d51d78d516ac40531ce83fe35c48524c (patch) | |
tree | 3b891853186d1705791655c84bae74773ee7378f | |
parent | 39074f04aec7466722271d1484ff75f48a77f93c (diff) | |
download | tpm_manager-c9429017d51d78d516ac40531ce83fe35c48524c.tar.gz |
tpm_manager: Break up proxy interface
This CL breaks up the dbus_proxy interface to inherit from
TpmOwnershipInterface and TpmNvramInterface.
Bug: 24659038
TEST=compile and run on DUT.
Change-Id: I74c9a7fa3c1c0cbe76e60f08bbb4160d41a40744
22 files changed, 691 insertions, 395 deletions
diff --git a/client/main.cc b/client/main.cc index 771249c..a752c5c 100644 --- a/client/main.cc +++ b/client/main.cc @@ -28,7 +28,8 @@ #include <brillo/daemons/daemon.h> #include <brillo/syslog_logging.h> -#include "tpm_manager/client/dbus_proxy.h" +#include "tpm_manager/client/tpm_nvram_dbus_proxy.h" +#include "tpm_manager/client/tpm_ownership_dbus_proxy.h" #include "tpm_manager/common/print_tpm_ownership_interface_proto.h" #include "tpm_manager/common/print_tpm_nvram_interface_proto.h" #include "tpm_manager/common/tpm_ownership_interface.pb.h" @@ -93,11 +94,18 @@ class ClientLoop : public ClientLoopBase { LOG(ERROR) << "Error initializing tpm_manager_client."; return exit_code; } - tpm_manager_.reset(new tpm_manager::DBusProxy()); - if (!tpm_manager_->Initialize()) { - LOG(ERROR) << "Error initializing dbus proxy to tpm_managerd."; + TpmNvramDBusProxy* nvram_proxy = new TpmNvramDBusProxy(); + if (!nvram_proxy->Initialize()) { + LOG(ERROR) << "Error initializing proxy to nvram interface."; return EX_UNAVAILABLE; } + TpmOwnershipDBusProxy* ownership_proxy = new TpmOwnershipDBusProxy(); + if (!ownership_proxy->Initialize()) { + LOG(ERROR) << "Error initializing proxy to ownership interface."; + return EX_UNAVAILABLE; + } + tpm_nvram_.reset(nvram_proxy); + tpm_ownership_.reset(ownership_proxy); exit_code = ScheduleCommand(); if (exit_code == EX_USAGE) { printf("%s", kUsage); @@ -106,7 +114,8 @@ class ClientLoop : public ClientLoopBase { } void OnShutdown(int* exit_code) override { - tpm_manager_.reset(); + tpm_nvram_.reset(); + tpm_ownership_.reset(); ClientLoopBase::OnShutdown(exit_code); } @@ -208,7 +217,7 @@ class ClientLoop : public ClientLoopBase { void HandleGetTpmStatus() { GetTpmStatusRequest request; - tpm_manager_->GetTpmStatus( + tpm_ownership_->GetTpmStatus( request, base::Bind(&ClientLoop::PrintReplyAndQuit<GetTpmStatusReply>, weak_factory_.GetWeakPtr())); @@ -216,7 +225,7 @@ class ClientLoop : public ClientLoopBase { void HandleTakeOwnership() { TakeOwnershipRequest request; - tpm_manager_->TakeOwnership( + tpm_ownership_->TakeOwnership( request, base::Bind(&ClientLoop::PrintReplyAndQuit<TakeOwnershipReply>, weak_factory_.GetWeakPtr())); @@ -226,7 +235,7 @@ class ClientLoop : public ClientLoopBase { DefineNvramRequest request; request.set_index(index); request.set_length(length); - tpm_manager_->DefineNvram( + tpm_nvram_->DefineNvram( request, base::Bind(&ClientLoop::PrintReplyAndQuit<DefineNvramReply>, weak_factory_.GetWeakPtr())); @@ -235,7 +244,7 @@ class ClientLoop : public ClientLoopBase { void HandleDestroyNvram(uint32_t index) { DestroyNvramRequest request; request.set_index(index); - tpm_manager_->DestroyNvram( + tpm_nvram_->DestroyNvram( request, base::Bind(&ClientLoop::PrintReplyAndQuit<DestroyNvramReply>, weak_factory_.GetWeakPtr())); @@ -245,7 +254,7 @@ class ClientLoop : public ClientLoopBase { WriteNvramRequest request; request.set_index(index); request.set_data(data); - tpm_manager_->WriteNvram( + tpm_nvram_->WriteNvram( request, base::Bind(&ClientLoop::PrintReplyAndQuit<WriteNvramReply>, weak_factory_.GetWeakPtr())); @@ -254,7 +263,7 @@ class ClientLoop : public ClientLoopBase { void HandleReadNvram(uint32_t index) { ReadNvramRequest request; request.set_index(index); - tpm_manager_->ReadNvram( + tpm_nvram_->ReadNvram( request, base::Bind(&ClientLoop::PrintReplyAndQuit<ReadNvramReply>, weak_factory_.GetWeakPtr())); @@ -263,7 +272,7 @@ class ClientLoop : public ClientLoopBase { void HandleIsNvramDefined(uint32_t index) { IsNvramDefinedRequest request; request.set_index(index); - tpm_manager_->IsNvramDefined( + tpm_nvram_->IsNvramDefined( request, base::Bind(&ClientLoop::PrintReplyAndQuit<IsNvramDefinedReply>, weak_factory_.GetWeakPtr())); @@ -272,7 +281,7 @@ class ClientLoop : public ClientLoopBase { void HandleIsNvramLocked(uint32_t index) { IsNvramLockedRequest request; request.set_index(index); - tpm_manager_->IsNvramLocked( + tpm_nvram_->IsNvramLocked( request, base::Bind(&ClientLoop::PrintReplyAndQuit<IsNvramLockedReply>, weak_factory_.GetWeakPtr())); @@ -281,14 +290,15 @@ class ClientLoop : public ClientLoopBase { void HandleGetNvramSize(uint32_t index) { GetNvramSizeRequest request; request.set_index(index); - tpm_manager_->GetNvramSize( + tpm_nvram_->GetNvramSize( request, base::Bind(&ClientLoop::PrintReplyAndQuit<GetNvramSizeReply>, weak_factory_.GetWeakPtr())); } // Pointer to a DBus proxy to tpm_managerd. - std::unique_ptr<tpm_manager::TpmManagerInterface> tpm_manager_; + std::unique_ptr<tpm_manager::TpmNvramInterface> tpm_nvram_; + std::unique_ptr<tpm_manager::TpmOwnershipInterface> tpm_ownership_; // Declared last so that weak pointers will be destroyed first. base::WeakPtrFactory<ClientLoop> weak_factory_{this}; diff --git a/client/dbus_proxy.cc b/client/tpm_nvram_dbus_proxy.cc index 18c1190..fc0706e 100644 --- a/client/dbus_proxy.cc +++ b/client/tpm_nvram_dbus_proxy.cc @@ -14,12 +14,13 @@ // limitations under the License. // -#include "tpm_manager/client/dbus_proxy.h" +#include "tpm_manager/client/tpm_nvram_dbus_proxy.h" #include <brillo/bind_lambda.h> #include <brillo/dbus/dbus_method_invoker.h> -#include "tpm_manager/common/dbus_interface.h" +#include "tpm_manager/common/tpm_manager_constants.h" +#include "tpm_manager/common/tpm_nvram_dbus_interface.h" namespace { @@ -30,15 +31,13 @@ const int kDBusTimeoutMS = 2 * 60 * 1000; namespace tpm_manager { -DBusProxy::DBusProxy() {} - -DBusProxy::~DBusProxy() { +TpmNvramDBusProxy::~TpmNvramDBusProxy() { if (bus_) { bus_->ShutdownAndBlock(); } } -bool DBusProxy::Initialize() { +bool TpmNvramDBusProxy::Initialize() { dbus::Bus::Options options; options.bus_type = dbus::Bus::SYSTEM; bus_ = new dbus::Bus(options); @@ -48,60 +47,49 @@ bool DBusProxy::Initialize() { return (object_proxy_ != nullptr); } -void DBusProxy::GetTpmStatus(const GetTpmStatusRequest& request, - const GetTpmStatusCallback& callback) { - CallMethod<GetTpmStatusReply>(tpm_manager::kGetTpmStatus, request, callback); -} - -void DBusProxy::TakeOwnership(const TakeOwnershipRequest& request, - const TakeOwnershipCallback& callback) { - CallMethod<TakeOwnershipReply>( - tpm_manager::kTakeOwnership, request, callback); -} - -void DBusProxy::DefineNvram(const DefineNvramRequest& request, - const DefineNvramCallback& callback) { +void TpmNvramDBusProxy::DefineNvram(const DefineNvramRequest& request, + const DefineNvramCallback& callback) { CallMethod<DefineNvramReply>(tpm_manager::kDefineNvram, request, callback); } -void DBusProxy::DestroyNvram(const DestroyNvramRequest& request, - const DestroyNvramCallback& callback) { +void TpmNvramDBusProxy::DestroyNvram(const DestroyNvramRequest& request, + const DestroyNvramCallback& callback) { CallMethod<DestroyNvramReply>(tpm_manager::kDestroyNvram, request, callback); } -void DBusProxy::WriteNvram(const WriteNvramRequest& request, - const WriteNvramCallback& callback) { +void TpmNvramDBusProxy::WriteNvram(const WriteNvramRequest& request, + const WriteNvramCallback& callback) { CallMethod<WriteNvramReply>(tpm_manager::kWriteNvram, request, callback); } -void DBusProxy::ReadNvram(const ReadNvramRequest& request, - const ReadNvramCallback& callback) { +void TpmNvramDBusProxy::ReadNvram(const ReadNvramRequest& request, + const ReadNvramCallback& callback) { CallMethod<ReadNvramReply>(tpm_manager::kReadNvram, request, callback); } -void DBusProxy::IsNvramDefined(const IsNvramDefinedRequest& request, - const IsNvramDefinedCallback& callback) { +void TpmNvramDBusProxy::IsNvramDefined(const IsNvramDefinedRequest& request, + const IsNvramDefinedCallback& callback) { CallMethod<IsNvramDefinedReply>( tpm_manager::kIsNvramDefined, request, callback); } -void DBusProxy::IsNvramLocked(const IsNvramLockedRequest& request, - const IsNvramLockedCallback& callback) { +void TpmNvramDBusProxy::IsNvramLocked(const IsNvramLockedRequest& request, + const IsNvramLockedCallback& callback) { CallMethod<IsNvramLockedReply>( tpm_manager::kIsNvramLocked, request, callback); } -void DBusProxy::GetNvramSize(const GetNvramSizeRequest& request, - const GetNvramSizeCallback& callback) { +void TpmNvramDBusProxy::GetNvramSize(const GetNvramSizeRequest& request, + const GetNvramSizeCallback& callback) { CallMethod<GetNvramSizeReply>(tpm_manager::kGetNvramSize, request, callback); } template<typename ReplyProtobufType, typename RequestProtobufType, typename CallbackType> -void DBusProxy::CallMethod(const std::string& method_name, - const RequestProtobufType& request, - const CallbackType& callback) { +void TpmNvramDBusProxy::CallMethod(const std::string& method_name, + const RequestProtobufType& request, + const CallbackType& callback) { auto on_error = [callback](brillo::Error* error) { ReplyProtobufType reply; reply.set_status(STATUS_NOT_AVAILABLE); @@ -110,7 +98,7 @@ void DBusProxy::CallMethod(const std::string& method_name, brillo::dbus_utils::CallMethodWithTimeout( kDBusTimeoutMS, object_proxy_, - tpm_manager::kTpmManagerInterface, + tpm_manager::kTpmNvramInterface, method_name, callback, base::Bind(on_error), diff --git a/client/dbus_proxy.h b/client/tpm_nvram_dbus_proxy.h index d52c68c..4522311 100644 --- a/client/dbus_proxy.h +++ b/client/tpm_nvram_dbus_proxy.h @@ -14,13 +14,14 @@ // limitations under the License. // -#ifndef TPM_MANAGER_CLIENT_DBUS_PROXY_H_ -#define TPM_MANAGER_CLIENT_DBUS_PROXY_H_ +#ifndef TPM_MANAGER_CLIENT_TPM_NVRAM_DBUS_PROXY_H_ +#define TPM_MANAGER_CLIENT_TPM_NVRAM_DBUS_PROXY_H_ -#include "tpm_manager/common/tpm_manager_interface.h" +#include "tpm_manager/common/tpm_nvram_interface.h" #include <string> +#include <base/macros.h> #include <base/memory/ref_counted.h> #include <dbus/bus.h> #include <dbus/object_proxy.h> @@ -29,22 +30,21 @@ namespace tpm_manager { -// An implementation of TpmManagerInterface that forwards requests to +// An implementation of TpmNvramInterface that forwards requests to // tpm_managerd over D-Bus. // Usage: -// std::unique_ptr<TpmManagerInterface> tpm_manager = new DBusProxy(); -// tpm_manager->GetTpmStatus(...); -class TPM_MANAGER_EXPORT DBusProxy : public TpmManagerInterface { +// std::unique_ptr<TpmNvramInterface> tpm_manager = new TpmNvramDBusProxy(); +// tpm_manager->DefineNvram(...); +class TPM_MANAGER_EXPORT TpmNvramDBusProxy : public TpmNvramInterface { public: - DBusProxy(); - virtual ~DBusProxy(); + TpmNvramDBusProxy() = default; + virtual ~TpmNvramDBusProxy(); - // TpmManagerInterface methods. - bool Initialize() override; - void GetTpmStatus(const GetTpmStatusRequest& request, - const GetTpmStatusCallback& callback) override; - void TakeOwnership(const TakeOwnershipRequest& request, - const TakeOwnershipCallback& callback) override; + // Performs initialization tasks. This method must be called before calling + // any other method in this class. Returns true on success. + bool Initialize(); + + // TpmNvramInterface methods. void DefineNvram(const DefineNvramRequest& request, const DefineNvramCallback& callback) override; void DestroyNvram(const DestroyNvramRequest& request, @@ -75,9 +75,9 @@ class TPM_MANAGER_EXPORT DBusProxy : public TpmManagerInterface { scoped_refptr<dbus::Bus> bus_; dbus::ObjectProxy* object_proxy_; - DISALLOW_COPY_AND_ASSIGN(DBusProxy); + DISALLOW_COPY_AND_ASSIGN(TpmNvramDBusProxy); }; } // namespace tpm_manager -#endif // TPM_MANAGER_CLIENT_DBUS_PROXY_H_ +#endif // TPM_MANAGER_CLIENT_TPM_NVRAM_DBUS_PROXY_H_ diff --git a/client/dbus_proxy_test.cc b/client/tpm_nvram_dbus_proxy_test.cc index b299b41..b03237e 100644 --- a/client/dbus_proxy_test.cc +++ b/client/tpm_nvram_dbus_proxy_test.cc @@ -21,7 +21,7 @@ #include <gmock/gmock.h> #include <gtest/gtest.h> -#include "tpm_manager/client/dbus_proxy.h" +#include "tpm_manager/client/tpm_nvram_dbus_proxy.h" using testing::_; using testing::Invoke; @@ -30,9 +30,9 @@ using testing::WithArgs; namespace tpm_manager { -class DBusProxyTest : public testing::Test { +class TpmNvramDBusProxyTest : public testing::Test { public: - ~DBusProxyTest() override = default; + ~TpmNvramDBusProxyTest() override = default; void SetUp() override { mock_object_proxy_ = new StrictMock<dbus::MockObjectProxy>( nullptr, "", dbus::ObjectPath("")); @@ -41,84 +41,10 @@ class DBusProxyTest : public testing::Test { protected: scoped_refptr<StrictMock<dbus::MockObjectProxy>> mock_object_proxy_; - DBusProxy proxy_; + TpmNvramDBusProxy proxy_; }; -TEST_F(DBusProxyTest, GetTpmStatus) { - auto fake_dbus_call = []( - dbus::MethodCall* method_call, - const dbus::MockObjectProxy::ResponseCallback& response_callback) { - // Verify request protobuf. - dbus::MessageReader reader(method_call); - GetTpmStatusRequest request; - EXPECT_TRUE(reader.PopArrayOfBytesAsProto(&request)); - // Create reply protobuf. - auto response = dbus::Response::CreateEmpty(); - dbus::MessageWriter writer(response.get()); - GetTpmStatusReply reply; - reply.set_status(STATUS_SUCCESS); - reply.set_enabled(true); - reply.set_owned(true); - reply.mutable_local_data()->set_owned_by_this_install(true); - reply.set_dictionary_attack_counter(3); - reply.set_dictionary_attack_threshold(4); - reply.set_dictionary_attack_lockout_in_effect(true); - reply.set_dictionary_attack_lockout_seconds_remaining(5); - writer.AppendProtoAsArrayOfBytes(reply); - response_callback.Run(response.release()); - }; - EXPECT_CALL(*mock_object_proxy_, CallMethodWithErrorCallback(_, _, _, _)) - .WillOnce(WithArgs<0, 2>(Invoke(fake_dbus_call))); - - // Set expectations on the outputs. - int callback_count = 0; - auto callback = [&callback_count](const GetTpmStatusReply& reply) { - callback_count++; - EXPECT_EQ(STATUS_SUCCESS, reply.status()); - EXPECT_TRUE(reply.enabled()); - EXPECT_TRUE(reply.owned()); - EXPECT_TRUE(reply.local_data().owned_by_this_install()); - EXPECT_EQ(3, reply.dictionary_attack_counter()); - EXPECT_EQ(4, reply.dictionary_attack_threshold()); - EXPECT_TRUE(reply.dictionary_attack_lockout_in_effect()); - EXPECT_EQ(5, reply.dictionary_attack_lockout_seconds_remaining()); - }; - GetTpmStatusRequest request; - proxy_.GetTpmStatus(request, base::Bind(callback)); - EXPECT_EQ(1, callback_count); -} - -TEST_F(DBusProxyTest, TakeOwnership) { - auto fake_dbus_call = []( - dbus::MethodCall* method_call, - const dbus::MockObjectProxy::ResponseCallback& response_callback) { - // Verify request protobuf. - dbus::MessageReader reader(method_call); - TakeOwnershipRequest request; - EXPECT_TRUE(reader.PopArrayOfBytesAsProto(&request)); - // Create reply protobuf. - auto response = dbus::Response::CreateEmpty(); - dbus::MessageWriter writer(response.get()); - TakeOwnershipReply reply; - reply.set_status(STATUS_SUCCESS); - writer.AppendProtoAsArrayOfBytes(reply); - response_callback.Run(response.release()); - }; - EXPECT_CALL(*mock_object_proxy_, CallMethodWithErrorCallback(_, _, _, _)) - .WillOnce(WithArgs<0, 2>(Invoke(fake_dbus_call))); - - // Set expectations on the outputs. - int callback_count = 0; - auto callback = [&callback_count](const TakeOwnershipReply& reply) { - callback_count++; - EXPECT_EQ(STATUS_SUCCESS, reply.status()); - }; - TakeOwnershipRequest request; - proxy_.TakeOwnership(request, base::Bind(callback)); - EXPECT_EQ(1, callback_count); -} - -TEST_F(DBusProxyTest, DefineNvram) { +TEST_F(TpmNvramDBusProxyTest, DefineNvram) { uint32_t nvram_index = 5; size_t nvram_length = 32; auto fake_dbus_call = [nvram_index, nvram_length]( @@ -155,7 +81,7 @@ TEST_F(DBusProxyTest, DefineNvram) { EXPECT_EQ(1, callback_count); } -TEST_F(DBusProxyTest, DestroyNvram) { +TEST_F(TpmNvramDBusProxyTest, DestroyNvram) { uint32_t nvram_index = 5; auto fake_dbus_call = [nvram_index]( dbus::MethodCall* method_call, @@ -187,7 +113,7 @@ TEST_F(DBusProxyTest, DestroyNvram) { proxy_.DestroyNvram(request, base::Bind(callback)); EXPECT_EQ(1, callback_count); } -TEST_F(DBusProxyTest, WriteNvram) { +TEST_F(TpmNvramDBusProxyTest, WriteNvram) { uint32_t nvram_index = 5; std::string nvram_data("nvram_data"); auto fake_dbus_call = [nvram_index, nvram_data]( @@ -224,7 +150,7 @@ TEST_F(DBusProxyTest, WriteNvram) { EXPECT_EQ(1, callback_count); } -TEST_F(DBusProxyTest, ReadNvram) { +TEST_F(TpmNvramDBusProxyTest, ReadNvram) { uint32_t nvram_index = 5; std::string nvram_data("nvram_data"); auto fake_dbus_call = [nvram_index, nvram_data]( @@ -261,7 +187,7 @@ TEST_F(DBusProxyTest, ReadNvram) { EXPECT_EQ(1, callback_count); } -TEST_F(DBusProxyTest, IsNvramDefined) { +TEST_F(TpmNvramDBusProxyTest, IsNvramDefined) { uint32_t nvram_index = 5; bool nvram_defined = true; auto fake_dbus_call = [nvram_index, nvram_defined]( @@ -299,7 +225,7 @@ TEST_F(DBusProxyTest, IsNvramDefined) { EXPECT_EQ(1, callback_count); } -TEST_F(DBusProxyTest, IsNvramLocked) { +TEST_F(TpmNvramDBusProxyTest, IsNvramLocked) { uint32_t nvram_index = 5; bool nvram_locked = true; auto fake_dbus_call = [nvram_index, nvram_locked]( @@ -337,7 +263,7 @@ TEST_F(DBusProxyTest, IsNvramLocked) { EXPECT_EQ(1, callback_count); } -TEST_F(DBusProxyTest, GetNvramSize) { +TEST_F(TpmNvramDBusProxyTest, GetNvramSize) { uint32_t nvram_index = 5; size_t nvram_size = 32; auto fake_dbus_call = [nvram_index, nvram_size]( diff --git a/client/tpm_ownership_dbus_proxy.cc b/client/tpm_ownership_dbus_proxy.cc new file mode 100644 index 0000000..3282ca4 --- /dev/null +++ b/client/tpm_ownership_dbus_proxy.cc @@ -0,0 +1,84 @@ +// +// Copyright (C) 2015 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include "tpm_manager/client/tpm_ownership_dbus_proxy.h" + +#include <brillo/bind_lambda.h> +#include <brillo/dbus/dbus_method_invoker.h> + +#include "tpm_manager/common/tpm_manager_constants.h" +#include "tpm_manager/common/tpm_ownership_dbus_interface.h" + +namespace { + +// Use a two minute timeout because TPM operations can take a long time. +const int kDBusTimeoutMS = 2 * 60 * 1000; + +} // namespace + +namespace tpm_manager { + +TpmOwnershipDBusProxy::~TpmOwnershipDBusProxy() { + if (bus_) { + bus_->ShutdownAndBlock(); + } +} + +bool TpmOwnershipDBusProxy::Initialize() { + dbus::Bus::Options options; + options.bus_type = dbus::Bus::SYSTEM; + bus_ = new dbus::Bus(options); + object_proxy_ = bus_->GetObjectProxy( + tpm_manager::kTpmManagerServiceName, + dbus::ObjectPath(tpm_manager::kTpmManagerServicePath)); + return (object_proxy_ != nullptr); +} + +void TpmOwnershipDBusProxy::GetTpmStatus( + const GetTpmStatusRequest& request, + const GetTpmStatusCallback& callback) { + CallMethod<GetTpmStatusReply>(tpm_manager::kGetTpmStatus, request, callback); +} + +void TpmOwnershipDBusProxy::TakeOwnership( + const TakeOwnershipRequest& request, + const TakeOwnershipCallback& callback) { + CallMethod<TakeOwnershipReply>( + tpm_manager::kTakeOwnership, request, callback); +} + +template<typename ReplyProtobufType, + typename RequestProtobufType, + typename CallbackType> +void TpmOwnershipDBusProxy::CallMethod(const std::string& method_name, + const RequestProtobufType& request, + const CallbackType& callback) { + auto on_error = [callback](brillo::Error* error) { + ReplyProtobufType reply; + reply.set_status(STATUS_NOT_AVAILABLE); + callback.Run(reply); + }; + brillo::dbus_utils::CallMethodWithTimeout( + kDBusTimeoutMS, + object_proxy_, + tpm_manager::kTpmOwnershipInterface, + method_name, + callback, + base::Bind(on_error), + request); +} + +} // namespace tpm_manager diff --git a/client/tpm_ownership_dbus_proxy.h b/client/tpm_ownership_dbus_proxy.h new file mode 100644 index 0000000..34c1a49 --- /dev/null +++ b/client/tpm_ownership_dbus_proxy.h @@ -0,0 +1,73 @@ +// +// Copyright (C) 2015 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#ifndef TPM_MANAGER_CLIENT_TPM_OWNERSHIP_DBUS_PROXY_H_ +#define TPM_MANAGER_CLIENT_TPM_OWNERSHIP_DBUS_PROXY_H_ + +#include "tpm_manager/common/tpm_ownership_interface.h" + +#include <string> + +#include <base/macros.h> +#include <base/memory/ref_counted.h> +#include <dbus/bus.h> +#include <dbus/object_proxy.h> + +#include "tpm_manager/common/export.h" + +namespace tpm_manager { + +// An implementation of TpmOwnershipInterface that forwards requests to +// tpm_managerd over D-Bus. +// Usage: +// std::unique_ptr<TpmOwnershipInterface> tpm_ = new TpmOwnershipDBusProxy(); +// tpm_->GetTpmStatus(...); +class TPM_MANAGER_EXPORT TpmOwnershipDBusProxy : public TpmOwnershipInterface { + public: + TpmOwnershipDBusProxy() = default; + virtual ~TpmOwnershipDBusProxy(); + + // Performs initialization tasks. This method must be called before calling + // any other method in this class. Returns true on success. + bool Initialize(); + + // TpmOwnershipInterface methods. + void GetTpmStatus(const GetTpmStatusRequest& request, + const GetTpmStatusCallback& callback) override; + void TakeOwnership(const TakeOwnershipRequest& request, + const TakeOwnershipCallback& callback) override; + + void set_object_proxy(dbus::ObjectProxy* object_proxy) { + object_proxy_ = object_proxy; + } + + private: + // Template method to call a given |method_name| remotely via dbus. + template<typename ReplyProtobufType, + typename RequestProtobufType, + typename CallbackType> + void CallMethod(const std::string& method_name, + const RequestProtobufType& request, + const CallbackType& callback); + + scoped_refptr<dbus::Bus> bus_; + dbus::ObjectProxy* object_proxy_; + DISALLOW_COPY_AND_ASSIGN(TpmOwnershipDBusProxy); +}; + +} // namespace tpm_manager + +#endif // TPM_MANAGER_CLIENT_TPM_OWNERSHIP_DBUS_PROXY_H_ diff --git a/client/tpm_ownership_dbus_proxy_test.cc b/client/tpm_ownership_dbus_proxy_test.cc new file mode 100644 index 0000000..a22afe6 --- /dev/null +++ b/client/tpm_ownership_dbus_proxy_test.cc @@ -0,0 +1,121 @@ +// +// Copyright (C) 2015 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include <string> + +#include <brillo/bind_lambda.h> +#include <dbus/mock_object_proxy.h> +#include <gmock/gmock.h> +#include <gtest/gtest.h> + +#include "tpm_manager/client/tpm_ownership_dbus_proxy.h" + +using testing::_; +using testing::Invoke; +using testing::StrictMock; +using testing::WithArgs; + +namespace tpm_manager { + +class TpmOwnershipDBusProxyTest : public testing::Test { + public: + ~TpmOwnershipDBusProxyTest() override = default; + void SetUp() override { + mock_object_proxy_ = new StrictMock<dbus::MockObjectProxy>( + nullptr, "", dbus::ObjectPath("")); + proxy_.set_object_proxy(mock_object_proxy_.get()); + } + + protected: + scoped_refptr<StrictMock<dbus::MockObjectProxy>> mock_object_proxy_; + TpmOwnershipDBusProxy proxy_; +}; + +TEST_F(TpmOwnershipDBusProxyTest, GetTpmStatus) { + auto fake_dbus_call = []( + dbus::MethodCall* method_call, + const dbus::MockObjectProxy::ResponseCallback& response_callback) { + // Verify request protobuf. + dbus::MessageReader reader(method_call); + GetTpmStatusRequest request; + EXPECT_TRUE(reader.PopArrayOfBytesAsProto(&request)); + // Create reply protobuf. + auto response = dbus::Response::CreateEmpty(); + dbus::MessageWriter writer(response.get()); + GetTpmStatusReply reply; + reply.set_status(STATUS_SUCCESS); + reply.set_enabled(true); + reply.set_owned(true); + reply.mutable_local_data()->set_owned_by_this_install(true); + reply.set_dictionary_attack_counter(3); + reply.set_dictionary_attack_threshold(4); + reply.set_dictionary_attack_lockout_in_effect(true); + reply.set_dictionary_attack_lockout_seconds_remaining(5); + writer.AppendProtoAsArrayOfBytes(reply); + response_callback.Run(response.release()); + }; + EXPECT_CALL(*mock_object_proxy_, CallMethodWithErrorCallback(_, _, _, _)) + .WillOnce(WithArgs<0, 2>(Invoke(fake_dbus_call))); + + // Set expectations on the outputs. + int callback_count = 0; + auto callback = [&callback_count](const GetTpmStatusReply& reply) { + callback_count++; + EXPECT_EQ(STATUS_SUCCESS, reply.status()); + EXPECT_TRUE(reply.enabled()); + EXPECT_TRUE(reply.owned()); + EXPECT_TRUE(reply.local_data().owned_by_this_install()); + EXPECT_EQ(3, reply.dictionary_attack_counter()); + EXPECT_EQ(4, reply.dictionary_attack_threshold()); + EXPECT_TRUE(reply.dictionary_attack_lockout_in_effect()); + EXPECT_EQ(5, reply.dictionary_attack_lockout_seconds_remaining()); + }; + GetTpmStatusRequest request; + proxy_.GetTpmStatus(request, base::Bind(callback)); + EXPECT_EQ(1, callback_count); +} + +TEST_F(TpmOwnershipDBusProxyTest, TakeOwnership) { + auto fake_dbus_call = []( + dbus::MethodCall* method_call, + const dbus::MockObjectProxy::ResponseCallback& response_callback) { + // Verify request protobuf. + dbus::MessageReader reader(method_call); + TakeOwnershipRequest request; + EXPECT_TRUE(reader.PopArrayOfBytesAsProto(&request)); + // Create reply protobuf. + auto response = dbus::Response::CreateEmpty(); + dbus::MessageWriter writer(response.get()); + TakeOwnershipReply reply; + reply.set_status(STATUS_SUCCESS); + writer.AppendProtoAsArrayOfBytes(reply); + response_callback.Run(response.release()); + }; + EXPECT_CALL(*mock_object_proxy_, CallMethodWithErrorCallback(_, _, _, _)) + .WillOnce(WithArgs<0, 2>(Invoke(fake_dbus_call))); + + // Set expectations on the outputs. + int callback_count = 0; + auto callback = [&callback_count](const TakeOwnershipReply& reply) { + callback_count++; + EXPECT_EQ(STATUS_SUCCESS, reply.status()); + }; + TakeOwnershipRequest request; + proxy_.TakeOwnership(request, base::Bind(callback)); + EXPECT_EQ(1, callback_count); +} + +} // namespace tpm_manager diff --git a/common/mock_tpm_manager_interface.h b/common/mock_tpm_manager_interface.h deleted file mode 100644 index 8246754..0000000 --- a/common/mock_tpm_manager_interface.h +++ /dev/null @@ -1,63 +0,0 @@ -// -// Copyright (C) 2015 The Android Open Source Project -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -#ifndef TPM_MANAGER_COMMON_MOCK_TPM_MANAGER_INTERFACE_H_ -#define TPM_MANAGER_COMMON_MOCK_TPM_MANAGER_INTERFACE_H_ - -#include "tpm_manager/common/tpm_manager_interface.h" - -#include <gmock/gmock.h> - -namespace tpm_manager { - -class MockTpmManagerInterface : public TpmManagerInterface { - public: - MockTpmManagerInterface(); - ~MockTpmManagerInterface() override; - - MOCK_METHOD0(Initialize, bool()); - MOCK_METHOD2(GetTpmStatus, - void(const GetTpmStatusRequest& request, - const GetTpmStatusCallback& callback)); - MOCK_METHOD2(TakeOwnership, - void(const TakeOwnershipRequest& request, - const TakeOwnershipCallback& callback)); - MOCK_METHOD2(DefineNvram, - void(const DefineNvramRequest& request, - const DefineNvramCallback& callback)); - MOCK_METHOD2(DestroyNvram, - void(const DestroyNvramRequest& request, - const DestroyNvramCallback& callback)); - MOCK_METHOD2(WriteNvram, - void(const WriteNvramRequest& request, - const WriteNvramCallback& callback)); - MOCK_METHOD2(ReadNvram, - void(const ReadNvramRequest& request, - const ReadNvramCallback& callback)); - MOCK_METHOD2(IsNvramDefined, - void(const IsNvramDefinedRequest& request, - const IsNvramDefinedCallback& callback)); - MOCK_METHOD2(IsNvramLocked, - void(const IsNvramLockedRequest& request, - const IsNvramLockedCallback& callback)); - MOCK_METHOD2(GetNvramSize, - void(const GetNvramSizeRequest& request, - const GetNvramSizeCallback& callback)); -}; - -} // namespace tpm_manager - -#endif // TPM_MANAGER_COMMON_MOCK_TPM_MANAGER_INTERFACE_H_ diff --git a/common/mock_tpm_manager_interface.cc b/common/mock_tpm_nvram_interface.cc index a585597..d0c12c1 100644 --- a/common/mock_tpm_manager_interface.cc +++ b/common/mock_tpm_nvram_interface.cc @@ -14,11 +14,11 @@ // limitations under the License. // -#include "tpm_manager/common/mock_tpm_manager_interface.h" +#include "tpm_manager/common/mock_tpm_nvram_interface.h" namespace tpm_manager { -MockTpmManagerInterface::MockTpmManagerInterface() {} -MockTpmManagerInterface::~MockTpmManagerInterface() {} +MockTpmNvramInterface::MockTpmNvramInterface() {} +MockTpmNvramInterface::~MockTpmNvramInterface() {} } // namespace tpm_manager diff --git a/common/mock_tpm_nvram_interface.h b/common/mock_tpm_nvram_interface.h new file mode 100644 index 0000000..c7360e7 --- /dev/null +++ b/common/mock_tpm_nvram_interface.h @@ -0,0 +1,49 @@ +// +// Copyright (C) 2015 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#ifndef TPM_MANAGER_COMMON_MOCK_TPM_NVRAM_INTERFACE_H_ +#define TPM_MANAGER_COMMON_MOCK_TPM_NVRAM_INTERFACE_H_ + +#include <gmock/gmock.h> + +#include "tpm_manager/common/tpm_nvram_interface.h" + +namespace tpm_manager { + +class MockTpmNvramInterface : public TpmNvramInterface { + public: + MockTpmNvramInterface(); + ~MockTpmNvramInterface() override; + + MOCK_METHOD2(DefineNvram, void(const DefineNvramRequest& request, + const DefineNvramCallback& callback)); + MOCK_METHOD2(DestroyNvram, void(const DestroyNvramRequest& request, + const DestroyNvramCallback& callback)); + MOCK_METHOD2(WriteNvram, void(const WriteNvramRequest& request, + const WriteNvramCallback& callback)); + MOCK_METHOD2(ReadNvram, void(const ReadNvramRequest& request, + const ReadNvramCallback& callback)); + MOCK_METHOD2(IsNvramDefined, void(const IsNvramDefinedRequest& request, + const IsNvramDefinedCallback& callback)); + MOCK_METHOD2(IsNvramLocked, void(const IsNvramLockedRequest& request, + const IsNvramLockedCallback& callback)); + MOCK_METHOD2(GetNvramSize, void(const GetNvramSizeRequest& request, + const GetNvramSizeCallback& callback)); +}; + +} // namespace tpm_manager + +#endif // TPM_MANAGER_COMMON_MOCK_TPM_NVRAM_INTERFACE_H_ diff --git a/common/mock_tpm_ownership_interface.cc b/common/mock_tpm_ownership_interface.cc new file mode 100644 index 0000000..ebc3bd6 --- /dev/null +++ b/common/mock_tpm_ownership_interface.cc @@ -0,0 +1,24 @@ +// +// Copyright (C) 2015 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include "tpm_manager/common/mock_tpm_ownership_interface.h" + +namespace tpm_manager { + +MockTpmOwnershipInterface::MockTpmOwnershipInterface() {} +MockTpmOwnershipInterface::~MockTpmOwnershipInterface() {} + +} // namespace tpm_manager diff --git a/common/mock_tpm_ownership_interface.h b/common/mock_tpm_ownership_interface.h new file mode 100644 index 0000000..5ae7ac6 --- /dev/null +++ b/common/mock_tpm_ownership_interface.h @@ -0,0 +1,39 @@ +// +// Copyright (C) 2015 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#ifndef TPM_MANAGER_COMMON_MOCK_TPM_OWNERSHIP_INTERFACE_H_ +#define TPM_MANAGER_COMMON_MOCK_TPM_OWNERSHIP_INTERFACE_H_ + +#include <gmock/gmock.h> + +#include "tpm_manager/common/tpm_ownership_interface.h" + +namespace tpm_manager { + +class MockTpmOwnershipInterface : public TpmOwnershipInterface { + public: + MockTpmOwnershipInterface(); + ~MockTpmOwnershipInterface() override; + + MOCK_METHOD2(GetTpmStatus, void(const GetTpmStatusRequest& request, + const GetTpmStatusCallback& callback)); + MOCK_METHOD2(TakeOwnership, void(const TakeOwnershipRequest& request, + const TakeOwnershipCallback& callback)); +}; + +} // namespace tpm_manager + +#endif // TPM_MANAGER_COMMON_MOCK_TPM_OWNERSHIP_INTERFACE_H_ diff --git a/common/tpm_manager_constants.h b/common/tpm_manager_constants.h new file mode 100644 index 0000000..eabf378 --- /dev/null +++ b/common/tpm_manager_constants.h @@ -0,0 +1,27 @@ +// +// Copyright (C) 2015 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#ifndef TPM_MANAGER_COMMON_TPM_MANAGER_CONSTANTS_H_ +#define TPM_MANAGER_COMMON_TPM_MANAGER_CONSTANTS_H_ + +namespace tpm_manager { + +constexpr char kTpmManagerServiceName[] = "org.chromium.TpmManager"; +constexpr char kTpmManagerServicePath[] = "/org/chromium/TpmManager"; + +} // namespace tpm_manager + +#endif // TPM_MANAGER_COMMON_TPM_MANAGER_CONSTANTS_H_ diff --git a/common/tpm_manager_interface.h b/common/tpm_manager_interface.h deleted file mode 100644 index 531bdb9..0000000 --- a/common/tpm_manager_interface.h +++ /dev/null @@ -1,66 +0,0 @@ -// -// Copyright (C) 2015 The Android Open Source Project -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -#ifndef TPM_MANAGER_COMMON_TPM_MANAGER_INTERFACE_H_ -#define TPM_MANAGER_COMMON_TPM_MANAGER_INTERFACE_H_ - -#include <base/callback.h> - -#include "tpm_manager/common/export.h" -#include "tpm_manager/common/tpm_nvram_interface.h" -#include "tpm_manager/common/tpm_ownership_interface.h" - -namespace tpm_manager { - -// This is the main TpmManager interface that is implemented by the proxies -// and services. -// TODO(usanghi): Move this class into server/ since the client side will -// implement each interface seperately. -class TPM_MANAGER_EXPORT TpmManagerInterface : public TpmNvramInterface, - public TpmOwnershipInterface { - public: - virtual ~TpmManagerInterface() = default; - - // Performs initialization tasks. This method must be called before calling - // any other method on this interface. - virtual bool Initialize() = 0; - - // TpmOwnershipInterface methods. - virtual void GetTpmStatus(const GetTpmStatusRequest& request, - const GetTpmStatusCallback& callback) = 0; - virtual void TakeOwnership(const TakeOwnershipRequest& request, - const TakeOwnershipCallback& callback) = 0; - - // TpmNvramInterface methods. - virtual void DefineNvram(const DefineNvramRequest& request, - const DefineNvramCallback& callback) = 0; - virtual void DestroyNvram(const DestroyNvramRequest& request, - const DestroyNvramCallback& callback) = 0; - virtual void WriteNvram(const WriteNvramRequest& request, - const WriteNvramCallback& callback) = 0; - virtual void ReadNvram(const ReadNvramRequest& request, - const ReadNvramCallback& callback) = 0; - virtual void IsNvramDefined(const IsNvramDefinedRequest& request, - const IsNvramDefinedCallback& callback) = 0; - virtual void IsNvramLocked(const IsNvramLockedRequest& request, - const IsNvramLockedCallback& callback) = 0; - virtual void GetNvramSize(const GetNvramSizeRequest& request, - const GetNvramSizeCallback& callback) = 0; -}; - -} // namespace tpm_manager - -#endif // TPM_MANAGER_COMMON_TPM_MANAGER_INTERFACE_H_ diff --git a/common/dbus_interface.h b/common/tpm_nvram_dbus_interface.h index d8ebc24..cd70595 100644 --- a/common/dbus_interface.h +++ b/common/tpm_nvram_dbus_interface.h @@ -14,18 +14,14 @@ // limitations under the License. // -#ifndef TPM_MANAGER_COMMON_DBUS_INTERFACE_H_ -#define TPM_MANAGER_COMMON_DBUS_INTERFACE_H_ +#ifndef TPM_MANAGER_COMMON_TPM_NVRAM_DBUS_INTERFACE_H_ +#define TPM_MANAGER_COMMON_TPM_NVRAM_DBUS_INTERFACE_H_ namespace tpm_manager { -constexpr char kTpmManagerInterface[] = "org.chromium.TpmManager"; -constexpr char kTpmManagerServicePath[] = "/org/chromium/TpmManager"; -constexpr char kTpmManagerServiceName[] = "org.chromium.TpmManager"; +constexpr char kTpmNvramInterface[] = "org.chromium.TpmNvram"; -// Methods exported by tpm_manager. -constexpr char kGetTpmStatus[] = "GetTpmStatus"; -constexpr char kTakeOwnership[] = "TakeOwnership"; +// Methods exported by tpm_manager nvram D-Bus interface. constexpr char kDefineNvram[] = "DefineNvram"; constexpr char kDestroyNvram[] = "DestroyNvram"; constexpr char kWriteNvram[] = "WriteNvram"; @@ -36,4 +32,4 @@ constexpr char kGetNvramSize[] = "GetNvramSize"; } // namespace tpm_manager -#endif // TPM_MANAGER_COMMON_DBUS_INTERFACE_H_ +#endif // TPM_MANAGER_COMMON_TPM_NVRAM_DBUS_INTERFACE_H_ diff --git a/common/tpm_ownership_dbus_interface.h b/common/tpm_ownership_dbus_interface.h new file mode 100644 index 0000000..979b0ff --- /dev/null +++ b/common/tpm_ownership_dbus_interface.h @@ -0,0 +1,30 @@ +// +// Copyright (C) 2015 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#ifndef TPM_MANAGER_COMMON_TPM_OWNERSHIP_DBUS_INTERFACE_H_ +#define TPM_MANAGER_COMMON_TPM_OWNERSHIP_DBUS_INTERFACE_H_ + +namespace tpm_manager { + +constexpr char kTpmOwnershipInterface[] = "org.chromium.TpmOwnership"; + +// Methods exported by tpm_manager ownership D-Bus interface. +constexpr char kGetTpmStatus[] = "GetTpmStatus"; +constexpr char kTakeOwnership[] = "TakeOwnership"; + +} // namespace tpm_manager + +#endif // TPM_MANAGER_COMMON_TPM_OWNERSHIP_DBUS_INTERFACE_H_ diff --git a/server/dbus_service.cc b/server/dbus_service.cc index 09c569f..2859ca9 100644 --- a/server/dbus_service.cc +++ b/server/dbus_service.cc @@ -23,90 +23,97 @@ #include <dbus/bus.h> #include <dbus/object_path.h> -#include "tpm_manager/common/dbus_interface.h" +#include "tpm_manager/common/tpm_manager_constants.h" +#include "tpm_manager/common/tpm_nvram_dbus_interface.h" +#include "tpm_manager/common/tpm_ownership_dbus_interface.h" namespace tpm_manager { DBusService::DBusService(const scoped_refptr<dbus::Bus>& bus, - TpmManagerInterface* service) + TpmNvramInterface* nvram_service, + TpmOwnershipInterface* ownership_service) : dbus_object_(nullptr, bus, dbus::ObjectPath(kTpmManagerServicePath)), - service_(service) {} + nvram_service_(nvram_service), + ownership_service_(ownership_service) {} void DBusService::Register(const CompletionAction& callback) { - brillo::dbus_utils::DBusInterface* dbus_interface = - dbus_object_.AddOrGetInterface(kTpmManagerInterface); + brillo::dbus_utils::DBusInterface* ownership_dbus_interface = + dbus_object_.AddOrGetInterface(kTpmOwnershipInterface); - dbus_interface->AddMethodHandler( + ownership_dbus_interface->AddMethodHandler( kGetTpmStatus, base::Unretained(this), - &DBusService::HandleDBusMethod< + &DBusService::HandleOwnershipDBusMethod< GetTpmStatusRequest, GetTpmStatusReply, - &TpmManagerInterface::GetTpmStatus>); + &TpmOwnershipInterface::GetTpmStatus>); - dbus_interface->AddMethodHandler( + ownership_dbus_interface->AddMethodHandler( kTakeOwnership, base::Unretained(this), - &DBusService::HandleDBusMethod< + &DBusService::HandleOwnershipDBusMethod< TakeOwnershipRequest, TakeOwnershipReply, - &TpmManagerInterface::TakeOwnership>); + &TpmOwnershipInterface::TakeOwnership>); - dbus_interface->AddMethodHandler( + brillo::dbus_utils::DBusInterface* nvram_dbus_interface = + dbus_object_.AddOrGetInterface(kTpmNvramInterface); + + nvram_dbus_interface->AddMethodHandler( kDefineNvram, base::Unretained(this), - &DBusService::HandleDBusMethod< + &DBusService::HandleNvramDBusMethod< DefineNvramRequest, DefineNvramReply, - &TpmManagerInterface::DefineNvram>); + &TpmNvramInterface::DefineNvram>); - dbus_interface->AddMethodHandler( + nvram_dbus_interface->AddMethodHandler( kDestroyNvram, base::Unretained(this), - &DBusService::HandleDBusMethod< + &DBusService::HandleNvramDBusMethod< DestroyNvramRequest, DestroyNvramReply, - &TpmManagerInterface::DestroyNvram>); + &TpmNvramInterface::DestroyNvram>); - dbus_interface->AddMethodHandler( + nvram_dbus_interface->AddMethodHandler( kWriteNvram, base::Unretained(this), - &DBusService::HandleDBusMethod< + &DBusService::HandleNvramDBusMethod< WriteNvramRequest, WriteNvramReply, - &TpmManagerInterface::WriteNvram>); + &TpmNvramInterface::WriteNvram>); - dbus_interface->AddMethodHandler( + nvram_dbus_interface->AddMethodHandler( kReadNvram, base::Unretained(this), - &DBusService::HandleDBusMethod< + &DBusService::HandleNvramDBusMethod< ReadNvramRequest, ReadNvramReply, - &TpmManagerInterface::ReadNvram>); + &TpmNvramInterface::ReadNvram>); - dbus_interface->AddMethodHandler( + nvram_dbus_interface->AddMethodHandler( kIsNvramDefined, base::Unretained(this), - &DBusService::HandleDBusMethod< + &DBusService::HandleNvramDBusMethod< IsNvramDefinedRequest, IsNvramDefinedReply, - &TpmManagerInterface::IsNvramDefined>); + &TpmNvramInterface::IsNvramDefined>); - dbus_interface->AddMethodHandler( + nvram_dbus_interface->AddMethodHandler( kIsNvramLocked, base::Unretained(this), - &DBusService::HandleDBusMethod< + &DBusService::HandleNvramDBusMethod< IsNvramLockedRequest, IsNvramLockedReply, - &TpmManagerInterface::IsNvramLocked>); + &TpmNvramInterface::IsNvramLocked>); - dbus_interface->AddMethodHandler( + nvram_dbus_interface->AddMethodHandler( kGetNvramSize, base::Unretained(this), - &DBusService::HandleDBusMethod< + &DBusService::HandleNvramDBusMethod< GetNvramSizeRequest, GetNvramSizeReply, - &TpmManagerInterface::GetNvramSize>); + &TpmNvramInterface::GetNvramSize>); dbus_object_.RegisterAsync(callback); } @@ -114,11 +121,12 @@ void DBusService::Register(const CompletionAction& callback) { template<typename RequestProtobufType, typename ReplyProtobufType, DBusService::HandlerFunction<RequestProtobufType, - ReplyProtobufType> func> -void DBusService::HandleDBusMethod( + ReplyProtobufType, + TpmNvramInterface> func> +void DBusService::HandleNvramDBusMethod( std::unique_ptr<DBusMethodResponse<const ReplyProtobufType&>> response, const RequestProtobufType& request) { - // Convert |response| to a shared_ptr so |service_| can safely copy the + // Convert |response| to a shared_ptr so |nvram_service_| can safely copy the // callback. using SharedResponsePointer = std::shared_ptr< DBusMethodResponse<const ReplyProtobufType&>>; @@ -127,7 +135,29 @@ void DBusService::HandleDBusMethod( const ReplyProtobufType& reply) { response->Return(reply); }; - (service_->*func)( + (nvram_service_->*func)( + request, + base::Bind(callback, SharedResponsePointer(std::move(response)))); +} + +template<typename RequestProtobufType, + typename ReplyProtobufType, + DBusService::HandlerFunction<RequestProtobufType, + ReplyProtobufType, + TpmOwnershipInterface> func> +void DBusService::HandleOwnershipDBusMethod( + std::unique_ptr<DBusMethodResponse<const ReplyProtobufType&>> response, + const RequestProtobufType& request) { + // Convert |response| to a shared_ptr so |ownership_service_| can safely + // copy the callback. + using SharedResponsePointer = std::shared_ptr< + DBusMethodResponse<const ReplyProtobufType&>>; + // A callback that sends off the reply protobuf. + auto callback = [](const SharedResponsePointer& response, + const ReplyProtobufType& reply) { + response->Return(reply); + }; + (ownership_service_->*func)( request, base::Bind(callback, SharedResponsePointer(std::move(response)))); } diff --git a/server/dbus_service.h b/server/dbus_service.h index e5502c6..4746310 100644 --- a/server/dbus_service.h +++ b/server/dbus_service.h @@ -23,7 +23,8 @@ #include <brillo/dbus/dbus_object.h> #include <dbus/bus.h> -#include "tpm_manager/common/tpm_manager_interface.h" +#include "tpm_manager/common/tpm_nvram_interface.h" +#include "tpm_manager/common/tpm_ownership_interface.h" namespace tpm_manager { @@ -34,10 +35,12 @@ using CompletionAction = // Handles D-Bus communtion with the TpmManager daemon. class DBusService { public: - // Does not take ownership of |service|. |service| must remain valid for the + // Does not take ownership of |nvram_service| or |ownership_service|. The + // services proviced must be initialized, and must remain valid for the // lifetime of this instance. DBusService(const scoped_refptr<dbus::Bus>& bus, - TpmManagerInterface* service); + TpmNvramInterface* nvram_service, + TpmOwnershipInterface* ownership_service); virtual ~DBusService() = default; // Connects to D-Bus system bus and exports TpmManager methods. @@ -47,22 +50,34 @@ class DBusService { friend class DBusServiceTest; template<typename RequestProtobufType, - typename ReplyProtobufType> - using HandlerFunction = void(TpmManagerInterface::*)( + typename ReplyProtobufType, + typename TpmInterface> + using HandlerFunction = void(TpmInterface::*)( const RequestProtobufType&, const base::Callback<void(const ReplyProtobufType&)>&); - // Template to handle D-Bus calls. + // Templates to handle D-Bus calls. + template<typename RequestProtobufType, + typename ReplyProtobufType, + DBusService::HandlerFunction<RequestProtobufType, + ReplyProtobufType, + TpmNvramInterface> func> + void HandleNvramDBusMethod( + std::unique_ptr<DBusMethodResponse<const ReplyProtobufType&>> response, + const RequestProtobufType& request); + template<typename RequestProtobufType, typename ReplyProtobufType, DBusService::HandlerFunction<RequestProtobufType, - ReplyProtobufType> func> - void HandleDBusMethod( + ReplyProtobufType, + TpmOwnershipInterface> func> + void HandleOwnershipDBusMethod( std::unique_ptr<DBusMethodResponse<const ReplyProtobufType&>> response, const RequestProtobufType& request); brillo::dbus_utils::DBusObject dbus_object_; - TpmManagerInterface* service_; + TpmNvramInterface* nvram_service_; + TpmOwnershipInterface* ownership_service_; DISALLOW_COPY_AND_ASSIGN(DBusService); }; diff --git a/server/dbus_service_test.cc b/server/dbus_service_test.cc index 2e9230a..25c7bbe 100644 --- a/server/dbus_service_test.cc +++ b/server/dbus_service_test.cc @@ -23,8 +23,11 @@ #include <gmock/gmock.h> #include <gtest/gtest.h> -#include "tpm_manager/common/dbus_interface.h" -#include "tpm_manager/common/mock_tpm_manager_interface.h" +#include "tpm_manager/common/mock_tpm_nvram_interface.h" +#include "tpm_manager/common/mock_tpm_ownership_interface.h" +#include "tpm_manager/common/tpm_manager_constants.h" +#include "tpm_manager/common/tpm_nvram_dbus_interface.h" +#include "tpm_manager/common/tpm_ownership_dbus_interface.h" #include "tpm_manager/server/dbus_service.h" using testing::_; @@ -47,7 +50,9 @@ class DBusServiceTest : public testing::Test { mock_bus_.get(), path); ON_CALL(*mock_bus_, GetExportedObject(path)) .WillByDefault(Return(mock_exported_object_.get())); - dbus_service_.reset(new DBusService(mock_bus_, &mock_service_)); + dbus_service_.reset(new DBusService(mock_bus_, + &mock_nvram_service_, + &mock_ownership_service_)); dbus_service_->Register(brillo::dbus_utils::AsyncEventSequencer:: GetDefaultCompletionAction()); } @@ -55,39 +60,38 @@ class DBusServiceTest : public testing::Test { template<typename RequestProtobufType, typename ReplyProtobufType> void ExecuteMethod(const std::string& method_name, const RequestProtobufType& request, - ReplyProtobufType* reply) { - std::unique_ptr<dbus::MethodCall> call = CreateMethodCall(method_name); + ReplyProtobufType* reply, + const std::string& interface) { + std::unique_ptr<dbus::MethodCall> call = CreateMethodCall(method_name, + interface); dbus::MessageWriter writer(call.get()); writer.AppendProtoAsArrayOfBytes(request); - auto response = CallMethod(call.get()); + auto response = brillo::dbus_utils::testing::CallMethod( + dbus_service_->dbus_object_, method_call); dbus::MessageReader reader(response.get()); EXPECT_TRUE(reader.PopArrayOfBytesAsProto(reply)); } protected: - std::unique_ptr<dbus::Response> CallMethod(dbus::MethodCall* method_call) { - return brillo::dbus_utils::testing::CallMethod( - dbus_service_->dbus_object_, method_call); - } - std::unique_ptr<dbus::MethodCall> CreateMethodCall( - const std::string& method_name) { + const std::string& method_name, const std::string& interface) { std::unique_ptr<dbus::MethodCall> call(new dbus::MethodCall( - kTpmManagerInterface, method_name)); + interface, method_name)); call->SetSerial(1); return call; } scoped_refptr<dbus::MockBus> mock_bus_; scoped_refptr<dbus::MockExportedObject> mock_exported_object_; - StrictMock<MockTpmManagerInterface> mock_service_; + StrictMock<MockTpmNvramInterface> mock_nvram_service_; + StrictMock<MockTpmOwnershipInterface> mock_ownership_service_; std::unique_ptr<DBusService> dbus_service_; }; TEST_F(DBusServiceTest, CopyableCallback) { - EXPECT_CALL(mock_service_, GetTpmStatus(_, _)) - .WillOnce(WithArgs<1>( - Invoke([](const TpmManagerInterface::GetTpmStatusCallback& callback) { + EXPECT_CALL(mock_ownership_service_, GetTpmStatus(_, _)) + .WillOnce(WithArgs<1>(Invoke([]( + const TpmOwnershipInterface::GetTpmStatusCallback& callback) { // Copy the callback, then call the original. GetTpmStatusReply reply; base::Closure copy = base::Bind(callback, reply); @@ -95,15 +99,15 @@ TEST_F(DBusServiceTest, CopyableCallback) { }))); GetTpmStatusRequest request; GetTpmStatusReply reply; - ExecuteMethod(kGetTpmStatus, request, &reply); + ExecuteMethod(kGetTpmStatus, request, &reply, kTpmOwnershipInterface); } TEST_F(DBusServiceTest, GetTpmStatus) { GetTpmStatusRequest request; - EXPECT_CALL(mock_service_, GetTpmStatus(_, _)) + EXPECT_CALL(mock_ownership_service_, GetTpmStatus(_, _)) .WillOnce(Invoke([]( const GetTpmStatusRequest& request, - const TpmManagerInterface::GetTpmStatusCallback& callback) { + const TpmOwnershipInterface::GetTpmStatusCallback& callback) { GetTpmStatusReply reply; reply.set_status(STATUS_SUCCESS); reply.set_enabled(true); @@ -116,7 +120,7 @@ TEST_F(DBusServiceTest, GetTpmStatus) { callback.Run(reply); })); GetTpmStatusReply reply; - ExecuteMethod(kGetTpmStatus, request, &reply); + ExecuteMethod(kGetTpmStatus, request, &reply, kTpmOwnershipInterface); EXPECT_EQ(STATUS_SUCCESS, reply.status()); EXPECT_TRUE(reply.enabled()); EXPECT_TRUE(reply.owned()); @@ -128,17 +132,17 @@ TEST_F(DBusServiceTest, GetTpmStatus) { } TEST_F(DBusServiceTest, TakeOwnership) { - EXPECT_CALL(mock_service_, TakeOwnership(_, _)) + EXPECT_CALL(mock_ownership_service_, TakeOwnership(_, _)) .WillOnce(Invoke([]( const TakeOwnershipRequest& request, - const TpmManagerInterface::TakeOwnershipCallback& callback) { + const TpmOwnershipInterface::TakeOwnershipCallback& callback) { TakeOwnershipReply reply; reply.set_status(STATUS_SUCCESS); callback.Run(reply); })); TakeOwnershipRequest request; TakeOwnershipReply reply; - ExecuteMethod(kTakeOwnership, request, &reply); + ExecuteMethod(kTakeOwnership, request, &reply, kTpmOwnershipInterface); EXPECT_EQ(STATUS_SUCCESS, reply.status()); } @@ -148,10 +152,10 @@ TEST_F(DBusServiceTest, DefineNvram) { DefineNvramRequest request; request.set_index(nvram_index); request.set_length(nvram_length); - EXPECT_CALL(mock_service_, DefineNvram(_, _)) + EXPECT_CALL(mock_nvram_service_, DefineNvram(_, _)) .WillOnce(Invoke([nvram_index, nvram_length]( const DefineNvramRequest& request, - const TpmManagerInterface::DefineNvramCallback& callback) { + const TpmNvramInterface::DefineNvramCallback& callback) { EXPECT_TRUE(request.has_index()); EXPECT_EQ(nvram_index, request.index()); EXPECT_TRUE(request.has_length()); @@ -161,7 +165,7 @@ TEST_F(DBusServiceTest, DefineNvram) { callback.Run(reply); })); DefineNvramReply reply; - ExecuteMethod(kDefineNvram, request, &reply); + ExecuteMethod(kDefineNvram, request, &reply, kTpmNvramInterface); EXPECT_EQ(STATUS_SUCCESS, reply.status()); } @@ -169,10 +173,10 @@ TEST_F(DBusServiceTest, DestroyNvram) { uint32_t nvram_index = 5; DestroyNvramRequest request; request.set_index(nvram_index); - EXPECT_CALL(mock_service_, DestroyNvram(_, _)) + EXPECT_CALL(mock_nvram_service_, DestroyNvram(_, _)) .WillOnce(Invoke([nvram_index]( const DestroyNvramRequest& request, - const TpmManagerInterface::DestroyNvramCallback& callback) { + const TpmNvramInterface::DestroyNvramCallback& callback) { EXPECT_TRUE(request.has_index()); EXPECT_EQ(nvram_index, request.index()); DestroyNvramReply reply; @@ -180,7 +184,7 @@ TEST_F(DBusServiceTest, DestroyNvram) { callback.Run(reply); })); DestroyNvramReply reply; - ExecuteMethod(kDestroyNvram, request, &reply); + ExecuteMethod(kDestroyNvram, request, &reply, kTpmNvramInterface); EXPECT_EQ(STATUS_SUCCESS, reply.status()); } @@ -190,10 +194,10 @@ TEST_F(DBusServiceTest, WriteNvram) { WriteNvramRequest request; request.set_index(nvram_index); request.set_data(nvram_data); - EXPECT_CALL(mock_service_, WriteNvram(_, _)) + EXPECT_CALL(mock_nvram_service_, WriteNvram(_, _)) .WillOnce(Invoke([nvram_index, nvram_data]( const WriteNvramRequest& request, - const TpmManagerInterface::WriteNvramCallback& callback) { + const TpmNvramInterface::WriteNvramCallback& callback) { EXPECT_TRUE(request.has_index()); EXPECT_EQ(nvram_index, request.index()); EXPECT_TRUE(request.has_data()); @@ -203,7 +207,7 @@ TEST_F(DBusServiceTest, WriteNvram) { callback.Run(reply); })); WriteNvramReply reply; - ExecuteMethod(kWriteNvram, request, &reply); + ExecuteMethod(kWriteNvram, request, &reply, kTpmNvramInterface); EXPECT_EQ(STATUS_SUCCESS, reply.status()); } @@ -212,10 +216,10 @@ TEST_F(DBusServiceTest, ReadNvram) { std::string nvram_data("nvram_data"); ReadNvramRequest request; request.set_index(nvram_index); - EXPECT_CALL(mock_service_, ReadNvram(_, _)) + EXPECT_CALL(mock_nvram_service_, ReadNvram(_, _)) .WillOnce(Invoke([nvram_index, nvram_data]( const ReadNvramRequest& request, - const TpmManagerInterface::ReadNvramCallback& callback) { + const TpmNvramInterface::ReadNvramCallback& callback) { EXPECT_TRUE(request.has_index()); EXPECT_EQ(nvram_index, request.index()); ReadNvramReply reply; @@ -224,7 +228,7 @@ TEST_F(DBusServiceTest, ReadNvram) { callback.Run(reply); })); ReadNvramReply reply; - ExecuteMethod(kReadNvram, request, &reply); + ExecuteMethod(kReadNvram, request, &reply, kTpmNvramInterface); EXPECT_EQ(STATUS_SUCCESS, reply.status()); EXPECT_TRUE(reply.has_data()); EXPECT_EQ(nvram_data, reply.data()); @@ -235,10 +239,10 @@ TEST_F(DBusServiceTest, IsNvramDefined) { bool nvram_defined = true; IsNvramDefinedRequest request; request.set_index(nvram_index); - EXPECT_CALL(mock_service_, IsNvramDefined(_, _)) + EXPECT_CALL(mock_nvram_service_, IsNvramDefined(_, _)) .WillOnce(Invoke([nvram_index, nvram_defined]( const IsNvramDefinedRequest& request, - const TpmManagerInterface::IsNvramDefinedCallback& callback) { + const TpmNvramInterface::IsNvramDefinedCallback& callback) { EXPECT_TRUE(request.has_index()); EXPECT_EQ(nvram_index, request.index()); IsNvramDefinedReply reply; @@ -247,7 +251,7 @@ TEST_F(DBusServiceTest, IsNvramDefined) { callback.Run(reply); })); IsNvramDefinedReply reply; - ExecuteMethod(kIsNvramDefined, request, &reply); + ExecuteMethod(kIsNvramDefined, request, &reply, kTpmNvramInterface); EXPECT_EQ(STATUS_SUCCESS, reply.status()); EXPECT_TRUE(reply.has_is_defined()); EXPECT_EQ(nvram_defined, reply.is_defined()); @@ -258,10 +262,10 @@ TEST_F(DBusServiceTest, IsNvramLocked) { bool nvram_locked = true; IsNvramLockedRequest request; request.set_index(nvram_index); - EXPECT_CALL(mock_service_, IsNvramLocked(_, _)) + EXPECT_CALL(mock_nvram_service_, IsNvramLocked(_, _)) .WillOnce(Invoke([nvram_index, nvram_locked]( const IsNvramLockedRequest& request, - const TpmManagerInterface::IsNvramLockedCallback& callback) { + const TpmNvramInterface::IsNvramLockedCallback& callback) { EXPECT_TRUE(request.has_index()); EXPECT_EQ(nvram_index, request.index()); IsNvramLockedReply reply; @@ -270,7 +274,7 @@ TEST_F(DBusServiceTest, IsNvramLocked) { callback.Run(reply); })); IsNvramLockedReply reply; - ExecuteMethod(kIsNvramLocked, request, &reply); + ExecuteMethod(kIsNvramLocked, request, &reply, kTpmNvramInterface); EXPECT_EQ(STATUS_SUCCESS, reply.status()); EXPECT_TRUE(reply.has_is_locked()); EXPECT_EQ(nvram_locked, reply.is_locked()); @@ -281,10 +285,10 @@ TEST_F(DBusServiceTest, GetNvramSize) { size_t nvram_size = 32; GetNvramSizeRequest request; request.set_index(nvram_index); - EXPECT_CALL(mock_service_, GetNvramSize(_, _)) + EXPECT_CALL(mock_nvram_service_, GetNvramSize(_, _)) .WillOnce(Invoke([nvram_index, nvram_size]( const GetNvramSizeRequest& request, - const TpmManagerInterface::GetNvramSizeCallback& callback) { + const TpmNvramInterface::GetNvramSizeCallback& callback) { EXPECT_TRUE(request.has_index()); EXPECT_EQ(nvram_index, request.index()); GetNvramSizeReply reply; @@ -293,7 +297,7 @@ TEST_F(DBusServiceTest, GetNvramSize) { callback.Run(reply); })); GetNvramSizeReply reply; - ExecuteMethod(kGetNvramSize, request, &reply); + ExecuteMethod(kGetNvramSize, request, &reply, kTpmNvramInterface); EXPECT_EQ(STATUS_SUCCESS, reply.status()); EXPECT_TRUE(reply.has_size()); EXPECT_EQ(nvram_size, reply.size()); diff --git a/server/main.cc b/server/main.cc index 307334f..8a4e74c 100644 --- a/server/main.cc +++ b/server/main.cc @@ -24,7 +24,7 @@ #include <brillo/syslog_logging.h> #include <brillo/userdb_utils.h> -#include "tpm_manager/common/dbus_interface.h" +#include "tpm_manager/common/tpm_manager_constants.h" #include "tpm_manager/server/dbus_service.h" #include "tpm_manager/server/local_data_store_impl.h" #include "tpm_manager/server/tpm_manager_service.h" @@ -85,7 +85,7 @@ class TpmManagerDaemon : public brillo::DBusServiceDaemon { void RegisterDBusObjectsAsync(AsyncEventSequencer* sequencer) override { dbus_service_.reset(new tpm_manager::DBusService( - bus_, tpm_manager_service_.get())); + bus_, tpm_manager_service_.get(), tpm_manager_service_.get())); dbus_service_->Register(sequencer->GetHandler("Register() failed.", true)); } @@ -94,7 +94,7 @@ class TpmManagerDaemon : public brillo::DBusServiceDaemon { std::unique_ptr<tpm_manager::TpmStatus> tpm_status_; std::unique_ptr<tpm_manager::TpmInitializer> tpm_initializer_; std::unique_ptr<tpm_manager::TpmNvram> tpm_nvram_; - std::unique_ptr<tpm_manager::TpmManagerInterface> tpm_manager_service_; + std::unique_ptr<tpm_manager::TpmManagerService> tpm_manager_service_; std::unique_ptr<tpm_manager::DBusService> dbus_service_; DISALLOW_COPY_AND_ASSIGN(TpmManagerDaemon); diff --git a/server/tpm_manager_service.h b/server/tpm_manager_service.h index 6fc7afa..64196d4 100644 --- a/server/tpm_manager_service.h +++ b/server/tpm_manager_service.h @@ -17,8 +17,6 @@ #ifndef TPM_MANAGER_SERVER_TPM_MANAGER_SERVICE_H_ #define TPM_MANAGER_SERVER_TPM_MANAGER_SERVICE_H_ -#include "tpm_manager/common/tpm_manager_interface.h" - #include <memory> #include <base/callback.h> @@ -27,6 +25,8 @@ #include <base/threading/thread.h> #include <brillo/bind_lambda.h> +#include "tpm_manager/common/tpm_nvram_interface.h" +#include "tpm_manager/common/tpm_ownership_interface.h" #include "tpm_manager/server/local_data_store.h" #include "tpm_manager/server/tpm_initializer.h" #include "tpm_manager/server/tpm_nvram.h" @@ -37,7 +37,7 @@ namespace tpm_manager { // This class implements the core tpm_manager service. All Tpm access is // asynchronous, except for the initial setup in Initialize(). // Usage: -// std::unique_ptr<TpmManagerInterface> tpm_manager = new TpmManagerService(); +// std::unique_ptr<TpmManagerService> tpm_manager = new TpmManagerService(); // CHECK(tpm_manager->Initialize()); // tpm_manager->GetTpmStatus(...); // @@ -52,7 +52,8 @@ namespace tpm_manager { // safe because the thread is owned by this class (so it is guaranteed not to // process a task after destruction). Weak pointers are used to post replies // back to the main thread. -class TpmManagerService : public TpmManagerInterface { +class TpmManagerService : public TpmNvramInterface, + public TpmOwnershipInterface { public: // If |wait_for_ownership| is set, TPM initialization will be postponed until // an explicit TakeOwnership request is received. Does not take ownership of @@ -64,12 +65,17 @@ class TpmManagerService : public TpmManagerInterface { TpmNvram* tpm_nvram); ~TpmManagerService() override = default; - // TpmManagerInterface methods. - bool Initialize() override; + // Performs initialization tasks. This method must be called before calling + // any other method in this class. Returns true on success. + bool Initialize(); + + // TpmOwnershipInterface methods. void GetTpmStatus(const GetTpmStatusRequest& request, const GetTpmStatusCallback& callback) override; void TakeOwnership(const TakeOwnershipRequest& request, const TakeOwnershipCallback& callback) override; + + // TpmNvramInterface methods. void DefineNvram(const DefineNvramRequest& request, const DefineNvramCallback& callback) override; void DestroyNvram(const DestroyNvramRequest& request, diff --git a/tpm_manager.gyp b/tpm_manager.gyp index 8922ac9..8fd48cc 100644 --- a/tpm_manager.gyp +++ b/tpm_manager.gyp @@ -58,7 +58,8 @@ 'target_name': 'libtpm_manager', 'type': 'shared_library', 'sources': [ - 'client/dbus_proxy.cc', + 'client/tpm_nvram_dbus_proxy.cc', + 'client/tpm_ownership_dbus_proxy.cc', ], 'dependencies': [ 'proto_library', @@ -149,8 +150,10 @@ ], }, 'sources': [ - 'client/dbus_proxy_test.cc', - 'common/mock_tpm_manager_interface.cc', + 'client/tpm_nvram_dbus_proxy_test.cc', + 'client/tpm_ownership_dbus_proxy_test.cc', + 'common/mock_tpm_nvram_interface.cc', + 'common/mock_tpm_ownership_interface.cc', 'server/dbus_service_test.cc', 'server/mock_local_data_store.cc', 'server/mock_openssl_crypto_util.cc', |