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