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_nvram_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/BpTpmNvram.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_result(tpm_manager::NVRAM_RESULT_IPC_ERROR);
36 }
37
38 } // namespace
39
40 namespace tpm_manager {
41
42 using android::tpm_manager::ITpmNvram;
43
TpmNvramBinderProxy(ITpmNvram * binder)44 TpmNvramBinderProxy::TpmNvramBinderProxy(ITpmNvram* binder) : binder_(binder) {}
45
Initialize()46 bool TpmNvramBinderProxy::Initialize() {
47 if (binder_) {
48 return true;
49 }
50 android::sp<android::IBinder> service_binder =
51 android::BinderWrapper::GetOrCreateInstance()->GetService(
52 kTpmNvramBinderName);
53 if (!service_binder.get()) {
54 LOG(ERROR) << "TpmNvramBinderProxy: Service does not exist.";
55 return false;
56 }
57 default_binder_ = new android::tpm_manager::BpTpmNvram(service_binder);
58 binder_ = default_binder_.get();
59 return true;
60 }
61
DefineSpace(const DefineSpaceRequest & request,const DefineSpaceCallback & callback)62 void TpmNvramBinderProxy::DefineSpace(const DefineSpaceRequest& request,
63 const DefineSpaceCallback& callback) {
64 auto method = base::Bind(&ITpmNvram::DefineSpace, base::Unretained(binder_));
65 auto get_error = base::Bind(&CreateErrorResponse<DefineSpaceReply>);
66 BinderProxyHelper<DefineSpaceRequest, DefineSpaceReply> helper(
67 method, callback, get_error);
68 helper.SendRequest(request);
69 }
70
DestroySpace(const DestroySpaceRequest & request,const DestroySpaceCallback & callback)71 void TpmNvramBinderProxy::DestroySpace(const DestroySpaceRequest& request,
72 const DestroySpaceCallback& callback) {
73 auto method = base::Bind(&ITpmNvram::DestroySpace, base::Unretained(binder_));
74 auto get_error = base::Bind(&CreateErrorResponse<DestroySpaceReply>);
75 BinderProxyHelper<DestroySpaceRequest, DestroySpaceReply> helper(
76 method, callback, get_error);
77 helper.SendRequest(request);
78 }
79
WriteSpace(const WriteSpaceRequest & request,const WriteSpaceCallback & callback)80 void TpmNvramBinderProxy::WriteSpace(const WriteSpaceRequest& request,
81 const WriteSpaceCallback& callback) {
82 auto method = base::Bind(&ITpmNvram::WriteSpace, base::Unretained(binder_));
83 auto get_error = base::Bind(&CreateErrorResponse<WriteSpaceReply>);
84 BinderProxyHelper<WriteSpaceRequest, WriteSpaceReply> helper(method, callback,
85 get_error);
86 helper.SendRequest(request);
87 }
88
ReadSpace(const ReadSpaceRequest & request,const ReadSpaceCallback & callback)89 void TpmNvramBinderProxy::ReadSpace(const ReadSpaceRequest& request,
90 const ReadSpaceCallback& callback) {
91 auto method = base::Bind(&ITpmNvram::ReadSpace, base::Unretained(binder_));
92 auto get_error = base::Bind(&CreateErrorResponse<ReadSpaceReply>);
93 BinderProxyHelper<ReadSpaceRequest, ReadSpaceReply> helper(method, callback,
94 get_error);
95 helper.SendRequest(request);
96 }
97
LockSpace(const LockSpaceRequest & request,const LockSpaceCallback & callback)98 void TpmNvramBinderProxy::LockSpace(const LockSpaceRequest& request,
99 const LockSpaceCallback& callback) {
100 auto method = base::Bind(&ITpmNvram::LockSpace, base::Unretained(binder_));
101 auto get_error = base::Bind(&CreateErrorResponse<LockSpaceReply>);
102 BinderProxyHelper<LockSpaceRequest, LockSpaceReply> helper(method, callback,
103 get_error);
104 helper.SendRequest(request);
105 }
106
ListSpaces(const ListSpacesRequest & request,const ListSpacesCallback & callback)107 void TpmNvramBinderProxy::ListSpaces(const ListSpacesRequest& request,
108 const ListSpacesCallback& callback) {
109 auto method = base::Bind(&ITpmNvram::ListSpaces, base::Unretained(binder_));
110 auto get_error = base::Bind(&CreateErrorResponse<ListSpacesReply>);
111 BinderProxyHelper<ListSpacesRequest, ListSpacesReply> helper(method, callback,
112 get_error);
113 helper.SendRequest(request);
114 }
115
GetSpaceInfo(const GetSpaceInfoRequest & request,const GetSpaceInfoCallback & callback)116 void TpmNvramBinderProxy::GetSpaceInfo(const GetSpaceInfoRequest& request,
117 const GetSpaceInfoCallback& callback) {
118 auto method = base::Bind(&ITpmNvram::GetSpaceInfo, base::Unretained(binder_));
119 auto get_error = base::Bind(&CreateErrorResponse<GetSpaceInfoReply>);
120 BinderProxyHelper<GetSpaceInfoRequest, GetSpaceInfoReply> helper(
121 method, callback, get_error);
122 helper.SendRequest(request);
123 }
124
125 } // namespace tpm_manager
126