1 //
2 // Copyright (C) 2014 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 "attestation/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 "attestation/common/dbus_interface.h"
27 
28 using brillo::dbus_utils::DBusMethodResponse;
29 
30 namespace attestation {
31 
DBusService(const scoped_refptr<dbus::Bus> & bus,AttestationInterface * service)32 DBusService::DBusService(const scoped_refptr<dbus::Bus>& bus,
33                          AttestationInterface* service)
34     : dbus_object_(nullptr, bus, dbus::ObjectPath(kAttestationServicePath)),
35       service_(service) {
36 }
37 
Register(const CompletionAction & callback)38 void DBusService::Register(const CompletionAction& callback) {
39   brillo::dbus_utils::DBusInterface* dbus_interface =
40       dbus_object_.AddOrGetInterface(kAttestationInterface);
41 
42   dbus_interface->AddMethodHandler(kCreateGoogleAttestedKey,
43                                    base::Unretained(this),
44                                    &DBusService::HandleCreateGoogleAttestedKey);
45   dbus_interface->AddMethodHandler(kGetKeyInfo,
46                                    base::Unretained(this),
47                                    &DBusService::HandleGetKeyInfo);
48   dbus_interface->AddMethodHandler(kGetEndorsementInfo,
49                                    base::Unretained(this),
50                                    &DBusService::HandleGetEndorsementInfo);
51   dbus_interface->AddMethodHandler(kGetAttestationKeyInfo,
52                                    base::Unretained(this),
53                                    &DBusService::HandleGetAttestationKeyInfo);
54   dbus_interface->AddMethodHandler(kActivateAttestationKey,
55                                    base::Unretained(this),
56                                    &DBusService::HandleActivateAttestationKey);
57   dbus_interface->AddMethodHandler(kCreateCertifiableKey,
58                                    base::Unretained(this),
59                                    &DBusService::HandleCreateCertifiableKey);
60   dbus_interface->AddMethodHandler(kDecrypt,
61                                    base::Unretained(this),
62                                    &DBusService::HandleDecrypt);
63   dbus_interface->AddMethodHandler(kSign,
64                                    base::Unretained(this),
65                                    &DBusService::HandleSign);
66   dbus_interface->AddMethodHandler(
67       kRegisterKeyWithChapsToken,
68       base::Unretained(this),
69       &DBusService::HandleRegisterKeyWithChapsToken);
70 
71   dbus_object_.RegisterAsync(callback);
72 }
73 
HandleCreateGoogleAttestedKey(std::unique_ptr<DBusMethodResponse<const CreateGoogleAttestedKeyReply &>> response,const CreateGoogleAttestedKeyRequest & request)74 void DBusService::HandleCreateGoogleAttestedKey(
75     std::unique_ptr<DBusMethodResponse<const CreateGoogleAttestedKeyReply&>>
76         response,
77     const CreateGoogleAttestedKeyRequest& request) {
78   VLOG(1) << __func__;
79   // Convert |response| to a shared_ptr so |service_| can safely copy the
80   // callback.
81   using SharedResponsePointer = std::shared_ptr<
82       DBusMethodResponse<const CreateGoogleAttestedKeyReply&>>;
83   // A callback that fills the reply protobuf and sends it.
84   auto callback = [](const SharedResponsePointer& response,
85                      const CreateGoogleAttestedKeyReply& reply) {
86     response->Return(reply);
87   };
88   service_->CreateGoogleAttestedKey(
89       request,
90       base::Bind(callback, SharedResponsePointer(std::move(response))));
91 }
92 
HandleGetKeyInfo(std::unique_ptr<DBusMethodResponse<const GetKeyInfoReply &>> response,const GetKeyInfoRequest & request)93 void DBusService::HandleGetKeyInfo(
94     std::unique_ptr<DBusMethodResponse<const GetKeyInfoReply&>> response,
95     const GetKeyInfoRequest& request) {
96   VLOG(1) << __func__;
97   // Convert |response| to a shared_ptr so |service_| can safely copy the
98   // callback.
99   using SharedResponsePointer = std::shared_ptr<
100       DBusMethodResponse<const GetKeyInfoReply&>>;
101   // A callback that fills the reply protobuf and sends it.
102   auto callback = [](const SharedResponsePointer& response,
103                      const GetKeyInfoReply& reply) {
104     response->Return(reply);
105   };
106   service_->GetKeyInfo(
107       request,
108       base::Bind(callback, SharedResponsePointer(std::move(response))));
109 }
110 
HandleGetEndorsementInfo(std::unique_ptr<DBusMethodResponse<const GetEndorsementInfoReply &>> response,const GetEndorsementInfoRequest & request)111 void DBusService::HandleGetEndorsementInfo(
112     std::unique_ptr<DBusMethodResponse<const GetEndorsementInfoReply&>>
113         response,
114     const GetEndorsementInfoRequest& request) {
115   VLOG(1) << __func__;
116   // Convert |response| to a shared_ptr so |service_| can safely copy the
117   // callback.
118   using SharedResponsePointer = std::shared_ptr<
119       DBusMethodResponse<const GetEndorsementInfoReply&>>;
120   // A callback that fills the reply protobuf and sends it.
121   auto callback = [](const SharedResponsePointer& response,
122                      const GetEndorsementInfoReply& reply) {
123     response->Return(reply);
124   };
125   service_->GetEndorsementInfo(
126       request,
127       base::Bind(callback, SharedResponsePointer(std::move(response))));
128 }
129 
HandleGetAttestationKeyInfo(std::unique_ptr<DBusMethodResponse<const GetAttestationKeyInfoReply &>> response,const GetAttestationKeyInfoRequest & request)130 void DBusService::HandleGetAttestationKeyInfo(
131     std::unique_ptr<DBusMethodResponse<const GetAttestationKeyInfoReply&>>
132         response,
133     const GetAttestationKeyInfoRequest& request) {
134   VLOG(1) << __func__;
135   // Convert |response| to a shared_ptr so |service_| can safely copy the
136   // callback.
137   using SharedResponsePointer = std::shared_ptr<
138       DBusMethodResponse<const GetAttestationKeyInfoReply&>>;
139   // A callback that fills the reply protobuf and sends it.
140   auto callback = [](const SharedResponsePointer& response,
141                      const GetAttestationKeyInfoReply& reply) {
142     response->Return(reply);
143   };
144   service_->GetAttestationKeyInfo(
145       request,
146       base::Bind(callback, SharedResponsePointer(std::move(response))));
147 }
148 
HandleActivateAttestationKey(std::unique_ptr<DBusMethodResponse<const ActivateAttestationKeyReply &>> response,const ActivateAttestationKeyRequest & request)149 void DBusService::HandleActivateAttestationKey(
150     std::unique_ptr<DBusMethodResponse<const ActivateAttestationKeyReply&>>
151         response,
152     const ActivateAttestationKeyRequest& request) {
153   VLOG(1) << __func__;
154   // Convert |response| to a shared_ptr so |service_| can safely copy the
155   // callback.
156   using SharedResponsePointer = std::shared_ptr<
157       DBusMethodResponse<const ActivateAttestationKeyReply&>>;
158   // A callback that fills the reply protobuf and sends it.
159   auto callback = [](const SharedResponsePointer& response,
160                      const ActivateAttestationKeyReply& reply) {
161     response->Return(reply);
162   };
163   service_->ActivateAttestationKey(
164       request,
165       base::Bind(callback, SharedResponsePointer(std::move(response))));
166 }
167 
HandleCreateCertifiableKey(std::unique_ptr<DBusMethodResponse<const CreateCertifiableKeyReply &>> response,const CreateCertifiableKeyRequest & request)168 void DBusService::HandleCreateCertifiableKey(
169     std::unique_ptr<DBusMethodResponse<const CreateCertifiableKeyReply&>>
170         response,
171     const CreateCertifiableKeyRequest& request) {
172   VLOG(1) << __func__;
173   // Convert |response| to a shared_ptr so |service_| can safely copy the
174   // callback.
175   using SharedResponsePointer = std::shared_ptr<
176       DBusMethodResponse<const CreateCertifiableKeyReply&>>;
177   // A callback that fills the reply protobuf and sends it.
178   auto callback = [](const SharedResponsePointer& response,
179                      const CreateCertifiableKeyReply& reply) {
180     response->Return(reply);
181   };
182   service_->CreateCertifiableKey(
183       request,
184       base::Bind(callback, SharedResponsePointer(std::move(response))));
185 }
186 
HandleDecrypt(std::unique_ptr<DBusMethodResponse<const DecryptReply &>> response,const DecryptRequest & request)187 void DBusService::HandleDecrypt(
188     std::unique_ptr<DBusMethodResponse<const DecryptReply&>> response,
189     const DecryptRequest& request) {
190   VLOG(1) << __func__;
191   // Convert |response| to a shared_ptr so |service_| can safely copy the
192   // callback.
193   using SharedResponsePointer = std::shared_ptr<
194       DBusMethodResponse<const DecryptReply&>>;
195   // A callback that fills the reply protobuf and sends it.
196   auto callback = [](const SharedResponsePointer& response,
197                      const DecryptReply& reply) {
198     response->Return(reply);
199   };
200   service_->Decrypt(
201       request,
202       base::Bind(callback, SharedResponsePointer(std::move(response))));
203 }
204 
HandleSign(std::unique_ptr<DBusMethodResponse<const SignReply &>> response,const SignRequest & request)205 void DBusService::HandleSign(
206     std::unique_ptr<DBusMethodResponse<const SignReply&>> response,
207     const SignRequest& request) {
208   VLOG(1) << __func__;
209   // Convert |response| to a shared_ptr so |service_| can safely copy the
210   // callback.
211   using SharedResponsePointer = std::shared_ptr<
212       DBusMethodResponse<const SignReply&>>;
213   // A callback that fills the reply protobuf and sends it.
214   auto callback = [](const SharedResponsePointer& response,
215                      const SignReply& reply) {
216     response->Return(reply);
217   };
218   service_->Sign(
219       request,
220       base::Bind(callback, SharedResponsePointer(std::move(response))));
221 }
222 
HandleRegisterKeyWithChapsToken(std::unique_ptr<DBusMethodResponse<const RegisterKeyWithChapsTokenReply &>> response,const RegisterKeyWithChapsTokenRequest & request)223 void DBusService::HandleRegisterKeyWithChapsToken(
224     std::unique_ptr<DBusMethodResponse<const RegisterKeyWithChapsTokenReply&>>
225         response,
226     const RegisterKeyWithChapsTokenRequest& request) {
227   VLOG(1) << __func__;
228   // Convert |response| to a shared_ptr so |service_| can safely copy the
229   // callback.
230   using SharedResponsePointer = std::shared_ptr<
231       DBusMethodResponse<const RegisterKeyWithChapsTokenReply&>>;
232   // A callback that fills the reply protobuf and sends it.
233   auto callback = [](const SharedResponsePointer& response,
234                      const RegisterKeyWithChapsTokenReply& reply) {
235     response->Return(reply);
236   };
237   service_->RegisterKeyWithChapsToken(
238       request,
239       base::Bind(callback, SharedResponsePointer(std::move(response))));
240 }
241 
242 }  // namespace attestation
243