1 //
2 // Copyright (C) 2015 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 "tpm_manager/server/dbus_service.h"
18
19 #include <memory>
20 #include <string>
21
22 #include <brillo/bind_lambda.h>
23 #include <dbus/bus.h>
24 #include <dbus/object_path.h>
25
26 #include "tpm_manager/common/tpm_manager_constants.h"
27 #include "tpm_manager/common/tpm_nvram_dbus_interface.h"
28 #include "tpm_manager/common/tpm_ownership_dbus_interface.h"
29
30 namespace tpm_manager {
31
DBusService(const scoped_refptr<dbus::Bus> & bus,TpmNvramInterface * nvram_service,TpmOwnershipInterface * ownership_service)32 DBusService::DBusService(const scoped_refptr<dbus::Bus>& bus,
33 TpmNvramInterface* nvram_service,
34 TpmOwnershipInterface* ownership_service)
35 : dbus_object_(nullptr, bus, dbus::ObjectPath(kTpmManagerServicePath)),
36 nvram_service_(nvram_service),
37 ownership_service_(ownership_service) {}
38
Register(const CompletionAction & callback)39 void DBusService::Register(const CompletionAction& callback) {
40 brillo::dbus_utils::DBusInterface* ownership_dbus_interface =
41 dbus_object_.AddOrGetInterface(kTpmOwnershipInterface);
42
43 ownership_dbus_interface->AddMethodHandler(
44 kGetTpmStatus,
45 base::Unretained(this),
46 &DBusService::HandleOwnershipDBusMethod<
47 GetTpmStatusRequest,
48 GetTpmStatusReply,
49 &TpmOwnershipInterface::GetTpmStatus>);
50
51 ownership_dbus_interface->AddMethodHandler(
52 kTakeOwnership,
53 base::Unretained(this),
54 &DBusService::HandleOwnershipDBusMethod<
55 TakeOwnershipRequest,
56 TakeOwnershipReply,
57 &TpmOwnershipInterface::TakeOwnership>);
58
59 ownership_dbus_interface->AddMethodHandler(
60 kRemoveOwnerDependency,
61 base::Unretained(this),
62 &DBusService::HandleOwnershipDBusMethod<
63 RemoveOwnerDependencyRequest,
64 RemoveOwnerDependencyReply,
65 &TpmOwnershipInterface::RemoveOwnerDependency>);
66
67 brillo::dbus_utils::DBusInterface* nvram_dbus_interface =
68 dbus_object_.AddOrGetInterface(kTpmNvramInterface);
69
70 nvram_dbus_interface->AddMethodHandler(
71 kDefineNvram,
72 base::Unretained(this),
73 &DBusService::HandleNvramDBusMethod<
74 DefineNvramRequest,
75 DefineNvramReply,
76 &TpmNvramInterface::DefineNvram>);
77
78 nvram_dbus_interface->AddMethodHandler(
79 kDestroyNvram,
80 base::Unretained(this),
81 &DBusService::HandleNvramDBusMethod<
82 DestroyNvramRequest,
83 DestroyNvramReply,
84 &TpmNvramInterface::DestroyNvram>);
85
86 nvram_dbus_interface->AddMethodHandler(
87 kWriteNvram,
88 base::Unretained(this),
89 &DBusService::HandleNvramDBusMethod<
90 WriteNvramRequest,
91 WriteNvramReply,
92 &TpmNvramInterface::WriteNvram>);
93
94 nvram_dbus_interface->AddMethodHandler(
95 kReadNvram,
96 base::Unretained(this),
97 &DBusService::HandleNvramDBusMethod<
98 ReadNvramRequest,
99 ReadNvramReply,
100 &TpmNvramInterface::ReadNvram>);
101
102 nvram_dbus_interface->AddMethodHandler(
103 kIsNvramDefined,
104 base::Unretained(this),
105 &DBusService::HandleNvramDBusMethod<
106 IsNvramDefinedRequest,
107 IsNvramDefinedReply,
108 &TpmNvramInterface::IsNvramDefined>);
109
110 nvram_dbus_interface->AddMethodHandler(
111 kIsNvramLocked,
112 base::Unretained(this),
113 &DBusService::HandleNvramDBusMethod<
114 IsNvramLockedRequest,
115 IsNvramLockedReply,
116 &TpmNvramInterface::IsNvramLocked>);
117
118 nvram_dbus_interface->AddMethodHandler(
119 kGetNvramSize,
120 base::Unretained(this),
121 &DBusService::HandleNvramDBusMethod<
122 GetNvramSizeRequest,
123 GetNvramSizeReply,
124 &TpmNvramInterface::GetNvramSize>);
125
126 dbus_object_.RegisterAsync(callback);
127 }
128
129 template<typename RequestProtobufType,
130 typename ReplyProtobufType,
131 DBusService::HandlerFunction<RequestProtobufType,
132 ReplyProtobufType,
133 TpmNvramInterface> func>
HandleNvramDBusMethod(std::unique_ptr<DBusMethodResponse<const ReplyProtobufType &>> response,const RequestProtobufType & request)134 void DBusService::HandleNvramDBusMethod(
135 std::unique_ptr<DBusMethodResponse<const ReplyProtobufType&>> response,
136 const RequestProtobufType& request) {
137 // Convert |response| to a shared_ptr so |nvram_service_| can safely copy the
138 // callback.
139 using SharedResponsePointer = std::shared_ptr<
140 DBusMethodResponse<const ReplyProtobufType&>>;
141 // A callback that sends off the reply protobuf.
142 auto callback = [](const SharedResponsePointer& response,
143 const ReplyProtobufType& reply) {
144 response->Return(reply);
145 };
146 (nvram_service_->*func)(
147 request,
148 base::Bind(callback, SharedResponsePointer(std::move(response))));
149 }
150
151 template<typename RequestProtobufType,
152 typename ReplyProtobufType,
153 DBusService::HandlerFunction<RequestProtobufType,
154 ReplyProtobufType,
155 TpmOwnershipInterface> func>
HandleOwnershipDBusMethod(std::unique_ptr<DBusMethodResponse<const ReplyProtobufType &>> response,const RequestProtobufType & request)156 void DBusService::HandleOwnershipDBusMethod(
157 std::unique_ptr<DBusMethodResponse<const ReplyProtobufType&>> response,
158 const RequestProtobufType& request) {
159 // Convert |response| to a shared_ptr so |ownership_service_| can safely
160 // copy the callback.
161 using SharedResponsePointer = std::shared_ptr<
162 DBusMethodResponse<const ReplyProtobufType&>>;
163 // A callback that sends off the reply protobuf.
164 auto callback = [](const SharedResponsePointer& response,
165 const ReplyProtobufType& reply) {
166 response->Return(reply);
167 };
168 (ownership_service_->*func)(
169 request,
170 base::Bind(callback, SharedResponsePointer(std::move(response))));
171 }
172
173 } // namespace tpm_manager
174