• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/client/tpm_ownership_dbus_proxy.h"
18 
19 #include <brillo/bind_lambda.h>
20 #include <brillo/dbus/dbus_method_invoker.h>
21 
22 #include "tpm_manager/common/tpm_manager_constants.h"
23 #include "tpm_manager/common/tpm_ownership_dbus_interface.h"
24 
25 namespace {
26 
27 // Use a two minute timeout because TPM operations can take a long time.
28 const int kDBusTimeoutMS = 2 * 60 * 1000;
29 
30 }  // namespace
31 
32 namespace tpm_manager {
33 
~TpmOwnershipDBusProxy()34 TpmOwnershipDBusProxy::~TpmOwnershipDBusProxy() {
35   if (bus_) {
36     bus_->ShutdownAndBlock();
37   }
38 }
39 
Initialize()40 bool TpmOwnershipDBusProxy::Initialize() {
41   dbus::Bus::Options options;
42   options.bus_type = dbus::Bus::SYSTEM;
43   bus_ = new dbus::Bus(options);
44   object_proxy_ = bus_->GetObjectProxy(
45       tpm_manager::kTpmManagerServiceName,
46       dbus::ObjectPath(tpm_manager::kTpmManagerServicePath));
47   return (object_proxy_ != nullptr);
48 }
49 
GetTpmStatus(const GetTpmStatusRequest & request,const GetTpmStatusCallback & callback)50 void TpmOwnershipDBusProxy::GetTpmStatus(const GetTpmStatusRequest& request,
51                                          const GetTpmStatusCallback& callback) {
52   CallMethod<GetTpmStatusReply>(tpm_manager::kGetTpmStatus, request, callback);
53 }
54 
TakeOwnership(const TakeOwnershipRequest & request,const TakeOwnershipCallback & callback)55 void TpmOwnershipDBusProxy::TakeOwnership(
56     const TakeOwnershipRequest& request,
57     const TakeOwnershipCallback& callback) {
58   CallMethod<TakeOwnershipReply>(tpm_manager::kTakeOwnership, request,
59                                  callback);
60 }
61 
RemoveOwnerDependency(const RemoveOwnerDependencyRequest & request,const RemoveOwnerDependencyCallback & callback)62 void TpmOwnershipDBusProxy::RemoveOwnerDependency(
63     const RemoveOwnerDependencyRequest& request,
64     const RemoveOwnerDependencyCallback& callback) {
65   CallMethod<RemoveOwnerDependencyReply>(tpm_manager::kRemoveOwnerDependency,
66                                          request, callback);
67 }
68 
69 template <typename ReplyProtobufType,
70           typename RequestProtobufType,
71           typename CallbackType>
CallMethod(const std::string & method_name,const RequestProtobufType & request,const CallbackType & callback)72 void TpmOwnershipDBusProxy::CallMethod(const std::string& method_name,
73                                        const RequestProtobufType& request,
74                                        const CallbackType& callback) {
75   auto on_error = [callback](brillo::Error* error) {
76     ReplyProtobufType reply;
77     reply.set_status(STATUS_NOT_AVAILABLE);
78     callback.Run(reply);
79   };
80   brillo::dbus_utils::CallMethodWithTimeout(
81       kDBusTimeoutMS, object_proxy_, tpm_manager::kTpmOwnershipInterface,
82       method_name, callback, base::Bind(on_error), request);
83 }
84 
85 }  // namespace tpm_manager
86