1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "sec_comp_proxy.h"
16
17 #include "sec_comp_click_event_parcel.h"
18 #include "sec_comp_err.h"
19 #include "sec_comp_log.h"
20
21 namespace OHOS {
22 namespace Security {
23 namespace SecurityComponent {
24 namespace {
25 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompProxy"};
26 }
27
SecCompProxy(const sptr<IRemoteObject> & impl)28 SecCompProxy::SecCompProxy(const sptr<IRemoteObject>& impl) : IRemoteProxy<ISecCompService>(impl)
29 {}
30
~SecCompProxy()31 SecCompProxy::~SecCompProxy()
32 {}
33
RegisterSecurityComponent(SecCompType type,const std::string & componentInfo,int32_t & scId)34 int32_t SecCompProxy::RegisterSecurityComponent(SecCompType type,
35 const std::string& componentInfo, int32_t& scId)
36 {
37 MessageParcel data;
38 if (!data.WriteInterfaceToken(SecCompProxy::GetDescriptor())) {
39 SC_LOG_ERROR(LABEL, "Register write descriptor fail");
40 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
41 }
42
43 if (!data.WriteUint32(type)) {
44 SC_LOG_ERROR(LABEL, "Register write Uint32 fail");
45 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
46 }
47 if (!data.WriteString(componentInfo)) {
48 SC_LOG_ERROR(LABEL, "Register write string fail");
49 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
50 }
51
52 MessageParcel reply;
53 MessageOption option(MessageOption::TF_SYNC);
54 sptr<IRemoteObject> remote = Remote();
55 if (remote == nullptr) {
56 SC_LOG_ERROR(LABEL, "Register remote service is null");
57 return SC_SERVICE_ERROR_IPC_REQUEST_FAIL;
58 }
59 int32_t requestResult = remote->SendRequest(
60 static_cast<uint32_t>(SecurityComponentServiceInterfaceCode::REGISTER_SECURITY_COMPONENT),
61 data, reply, option);
62 if (requestResult != SC_OK) {
63 SC_LOG_ERROR(LABEL, "Register request fail, result: %{public}d", requestResult);
64 return requestResult;
65 }
66 int32_t res;
67 if (!reply.ReadInt32(res)) {
68 SC_LOG_ERROR(LABEL, "Register read result int32 fail");
69 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
70 }
71
72 if (!reply.ReadInt32(scId)) {
73 SC_LOG_ERROR(LABEL, "Register read scId int32 fail");
74 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
75 }
76 return res;
77 }
78
UpdateSecurityComponent(int32_t scId,const std::string & componentInfo)79 int32_t SecCompProxy::UpdateSecurityComponent(int32_t scId, const std::string& componentInfo)
80 {
81 MessageParcel data;
82 if (!data.WriteInterfaceToken(SecCompProxy::GetDescriptor())) {
83 SC_LOG_ERROR(LABEL, "Update write descriptor fail");
84 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
85 }
86
87 if (!data.WriteInt32(scId)) {
88 SC_LOG_ERROR(LABEL, "Update write Int32 fail");
89 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
90 }
91 if (!data.WriteString(componentInfo)) {
92 SC_LOG_ERROR(LABEL, "Update write string fail");
93 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
94 }
95
96 MessageParcel reply;
97 MessageOption option(MessageOption::TF_SYNC);
98 sptr<IRemoteObject> remote = Remote();
99 if (remote == nullptr) {
100 SC_LOG_ERROR(LABEL, "Update remote update service is null");
101 return SC_SERVICE_ERROR_IPC_REQUEST_FAIL;
102 }
103 int32_t requestResult = remote->SendRequest(
104 static_cast<uint32_t>(SecurityComponentServiceInterfaceCode::UPDATE_SECURITY_COMPONENT), data, reply, option);
105 if (requestResult != SC_OK) {
106 SC_LOG_ERROR(LABEL, "Update request fail, result: %{public}d", requestResult);
107 return requestResult;
108 }
109 int32_t res;
110 if (!reply.ReadInt32(res)) {
111 SC_LOG_ERROR(LABEL, "Update read result int32 fail");
112 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
113 }
114 return res;
115 }
116
UnregisterSecurityComponent(int32_t scId)117 int32_t SecCompProxy::UnregisterSecurityComponent(int32_t scId)
118 {
119 MessageParcel data;
120 if (!data.WriteInterfaceToken(SecCompProxy::GetDescriptor())) {
121 SC_LOG_ERROR(LABEL, "Unregister write descriptor fail");
122 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
123 }
124
125 if (!data.WriteInt32(scId)) {
126 SC_LOG_ERROR(LABEL, "Unregister write Int32 fail");
127 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
128 }
129
130 MessageParcel reply;
131 MessageOption option(MessageOption::TF_SYNC);
132 sptr<IRemoteObject> remote = Remote();
133 if (remote == nullptr) {
134 SC_LOG_ERROR(LABEL, "Unregister remote service is null");
135 return SC_SERVICE_ERROR_IPC_REQUEST_FAIL;
136 }
137 int32_t requestResult = remote->SendRequest(
138 static_cast<uint32_t>(SecurityComponentServiceInterfaceCode::UNREGISTER_SECURITY_COMPONENT),
139 data, reply, option);
140 if (requestResult != SC_OK) {
141 SC_LOG_ERROR(LABEL, "Unregister request fail, result: %{public}d", requestResult);
142 return requestResult;
143 }
144 int32_t res;
145 if (!reply.ReadInt32(res)) {
146 SC_LOG_ERROR(LABEL, "Unregister read int32 fail");
147 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
148 }
149 return res;
150 }
151
ReportSecurityComponentClickEvent(int32_t scId,const std::string & componentInfo,const SecCompClickEvent & touchInfo,sptr<IRemoteObject> callerToken)152 int32_t SecCompProxy::ReportSecurityComponentClickEvent(int32_t scId,
153 const std::string& componentInfo, const SecCompClickEvent& touchInfo, sptr<IRemoteObject> callerToken)
154 {
155 MessageParcel data;
156 if (!data.WriteInterfaceToken(SecCompProxy::GetDescriptor())) {
157 SC_LOG_ERROR(LABEL, "Report write descriptor fail");
158 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
159 }
160
161 if (!data.WriteInt32(scId)) {
162 SC_LOG_ERROR(LABEL, "Report write Uint32 fail");
163 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
164 }
165
166 if (!data.WriteString(componentInfo)) {
167 SC_LOG_ERROR(LABEL, "Report write string fail");
168 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
169 }
170
171 sptr<SecCompClickEventParcel> parcel = new (std::nothrow) SecCompClickEventParcel();
172 if (parcel == nullptr) {
173 SC_LOG_ERROR(LABEL, "Report new click event parcel failed");
174 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
175 }
176 parcel->touchInfoParams_ = touchInfo;
177 if (!data.WriteParcelable(parcel)) {
178 SC_LOG_ERROR(LABEL, "Report write touchInfo fail");
179 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
180 }
181
182 if ((callerToken != nullptr) && !data.WriteRemoteObject(callerToken)) {
183 SC_LOG_ERROR(LABEL, "Report write caller token fail");
184 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
185 }
186
187 MessageParcel reply;
188 MessageOption option(MessageOption::TF_SYNC);
189 sptr<IRemoteObject> remote = Remote();
190 if (remote == nullptr) {
191 SC_LOG_ERROR(LABEL, "Report remote service is null");
192 return SC_SERVICE_ERROR_IPC_REQUEST_FAIL;
193 }
194 int32_t requestResult = remote->SendRequest(
195 static_cast<uint32_t>(SecurityComponentServiceInterfaceCode::REPORT_SECURITY_COMPONENT_CLICK_EVENT),
196 data, reply, option);
197 if (requestResult != SC_OK) {
198 SC_LOG_ERROR(LABEL, "Report request fail, result: %{public}d", requestResult);
199 return requestResult;
200 }
201 int32_t res;
202 if (!reply.ReadInt32(res)) {
203 SC_LOG_ERROR(LABEL, "Report read int32 fail");
204 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
205 }
206 return res;
207 }
208
ReduceAfterVerifySavePermission(AccessToken::AccessTokenID tokenId)209 bool SecCompProxy::ReduceAfterVerifySavePermission(AccessToken::AccessTokenID tokenId)
210 {
211 MessageParcel data;
212 if (!data.WriteInterfaceToken(SecCompProxy::GetDescriptor())) {
213 SC_LOG_ERROR(LABEL, "Verify write descriptor fail");
214 return false;
215 }
216
217 if (!data.WriteUint32(tokenId)) {
218 SC_LOG_ERROR(LABEL, "Verify write Uint32 fail");
219 return false;
220 }
221
222 MessageParcel reply;
223 MessageOption option(MessageOption::TF_SYNC);
224 sptr<IRemoteObject> remote = Remote();
225 if (remote == nullptr) {
226 SC_LOG_ERROR(LABEL, "Verify remote service is null");
227 return false;
228 }
229 int32_t requestResult = remote->SendRequest(
230 static_cast<uint32_t>(SecurityComponentServiceInterfaceCode::VERIFY_TEMP_SAVE_PERMISSION),
231 data, reply, option);
232 if (requestResult != SC_OK) {
233 SC_LOG_ERROR(LABEL, "Verify request fail, result: %{public}d", requestResult);
234 return false;
235 }
236 bool res;
237 if (!reply.ReadBool(res)) {
238 SC_LOG_ERROR(LABEL, "Verify read bool fail");
239 return false;
240 }
241 return res;
242 }
243
GetEnhanceRemoteObject()244 sptr<IRemoteObject> SecCompProxy::GetEnhanceRemoteObject()
245 {
246 MessageParcel data;
247 if (!data.WriteInterfaceToken(SecCompProxy::GetDescriptor())) {
248 SC_LOG_ERROR(LABEL, "Get enhance write descriptor fail");
249 return nullptr;
250 }
251
252 MessageParcel reply;
253 MessageOption option(MessageOption::TF_SYNC);
254 sptr<IRemoteObject> remote = Remote();
255 if (remote == nullptr) {
256 SC_LOG_ERROR(LABEL, "Get enhance remote service is null");
257 return nullptr;
258 }
259 int32_t requestResult = remote->SendRequest(
260 static_cast<uint32_t>(SecurityComponentServiceInterfaceCode::GET_SECURITY_COMPONENT_ENHANCE_OBJECT),
261 data, reply, option);
262 if (requestResult != SC_OK) {
263 SC_LOG_ERROR(LABEL, "Get enhance request fail, result: %{public}d", requestResult);
264 return nullptr;
265 }
266 sptr<IRemoteObject> callback = reply.ReadRemoteObject();
267 if (callback == nullptr) {
268 SC_LOG_ERROR(LABEL, "Get enhance read remote object fail");
269 }
270 return callback;
271 }
272 } // namespace SecurityComponent
273 } // namespace Security
274 } // namespace OHOS
275