1 //
2 // Copyright (C) 2016 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_binder_proxy.h"
18
19 #include <base/bind.h>
20 #include <base/callback.h>
21 #include <base/logging.h>
22 #include <binderwrapper/binder_wrapper.h>
23 #include <utils/Errors.h>
24
25 #include "android/tpm_manager/BnTpmManagerClient.h"
26 #include "android/tpm_manager/BpTpmOwnership.h"
27 #include "tpm_manager/client/binder_proxy_helper.h"
28 #include "tpm_manager/common/tpm_manager.pb.h"
29 #include "tpm_manager/common/tpm_manager_constants.h"
30
31 namespace {
32
33 template <typename ResponseProtobufType>
CreateErrorResponse(ResponseProtobufType * response)34 void CreateErrorResponse(ResponseProtobufType* response) {
35 response->set_status(tpm_manager::STATUS_DEVICE_ERROR);
36 }
37
38 } // namespace
39
40 namespace tpm_manager {
41
42 using android::tpm_manager::ITpmOwnership;
43
TpmOwnershipBinderProxy(ITpmOwnership * binder)44 TpmOwnershipBinderProxy::TpmOwnershipBinderProxy(ITpmOwnership* binder)
45 : binder_(binder) {}
46
Initialize()47 bool TpmOwnershipBinderProxy::Initialize() {
48 if (binder_) {
49 return true;
50 }
51 android::sp<android::IBinder> service_binder =
52 android::BinderWrapper::GetOrCreateInstance()->GetService(
53 kTpmOwnershipBinderName);
54 if (!service_binder.get()) {
55 LOG(ERROR) << "TpmOwnershipBinderProxy: Service does not exist.";
56 return false;
57 }
58 default_binder_ = new android::tpm_manager::BpTpmOwnership(service_binder);
59 binder_ = default_binder_.get();
60 return true;
61 }
62
GetTpmStatus(const GetTpmStatusRequest & request,const GetTpmStatusCallback & callback)63 void TpmOwnershipBinderProxy::GetTpmStatus(
64 const GetTpmStatusRequest& request,
65 const GetTpmStatusCallback& callback) {
66 auto method =
67 base::Bind(&ITpmOwnership::GetTpmStatus, base::Unretained(binder_));
68 auto get_error = base::Bind(&CreateErrorResponse<GetTpmStatusReply>);
69 BinderProxyHelper<GetTpmStatusRequest, GetTpmStatusReply> helper(
70 method, callback, get_error);
71 helper.SendRequest(request);
72 }
73
TakeOwnership(const TakeOwnershipRequest & request,const TakeOwnershipCallback & callback)74 void TpmOwnershipBinderProxy::TakeOwnership(
75 const TakeOwnershipRequest& request,
76 const TakeOwnershipCallback& callback) {
77 auto method =
78 base::Bind(&ITpmOwnership::TakeOwnership, base::Unretained(binder_));
79 auto get_error = base::Bind(&CreateErrorResponse<TakeOwnershipReply>);
80 BinderProxyHelper<TakeOwnershipRequest, TakeOwnershipReply> helper(
81 method, callback, get_error);
82 helper.SendRequest(request);
83 }
84
RemoveOwnerDependency(const RemoveOwnerDependencyRequest & request,const RemoveOwnerDependencyCallback & callback)85 void TpmOwnershipBinderProxy::RemoveOwnerDependency(
86 const RemoveOwnerDependencyRequest& request,
87 const RemoveOwnerDependencyCallback& callback) {
88 auto method = base::Bind(&ITpmOwnership::RemoveOwnerDependency,
89 base::Unretained(binder_));
90 auto get_error = base::Bind(&CreateErrorResponse<RemoveOwnerDependencyReply>);
91 BinderProxyHelper<RemoveOwnerDependencyRequest, RemoveOwnerDependencyReply>
92 helper(method, callback, get_error);
93 helper.SendRequest(request);
94 }
95
96 } // namespace tpm_manager
97