1 /*
2 * Copyright (c) 2022 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
16 #include "data_collect_manager_proxy.h"
17 #include <cinttypes>
18 #include "security_guard_define.h"
19 #include "security_guard_log.h"
20
21 namespace OHOS::Security::SecurityGuard {
DataCollectManagerProxy(const sptr<IRemoteObject> & impl)22 DataCollectManagerProxy::DataCollectManagerProxy(const sptr<IRemoteObject> &impl)
23 : IRemoteProxy<IDataCollectManager>(impl)
24 {
25 }
26
RequestDataSubmit(int64_t eventId,std::string & version,std::string & time,std::string & content,bool isSync)27 int32_t DataCollectManagerProxy::RequestDataSubmit(int64_t eventId, std::string &version,
28 std::string &time, std::string &content, bool isSync)
29 {
30 SGLOGD("enter DataCollectManagerProxy RequestDataSubmit");
31 SGLOGD("eventId=%{public}" PRId64 ", version=%{public}s", eventId, version.c_str());
32 MessageParcel data;
33 MessageParcel reply;
34 if (!data.WriteInterfaceToken(GetDescriptor())) {
35 SGLOGE("WriteInterfaceToken error");
36 return WRITE_ERR;
37 }
38 data.WriteInt64(eventId);
39 data.WriteString(version);
40 data.WriteString(time);
41 data.WriteString(content);
42
43 MessageOption option = { isSync ? MessageOption::TF_SYNC : MessageOption::TF_ASYNC };
44 sptr<IRemoteObject> remote = Remote();
45 if (remote == nullptr) {
46 SGLOGE("Remote error");
47 return NULL_OBJECT;
48 }
49 int ret = remote->SendRequest(CMD_DATA_COLLECT, data, reply, option);
50 if (ret != ERR_NONE) {
51 SGLOGE("ret=%{public}d", ret);
52 return ret;
53 }
54 if (isSync) {
55 ret = reply.ReadInt32();
56 SGLOGD("reply=%{public}d", ret);
57 }
58 return ret;
59 }
60
RequestRiskData(std::string & devId,std::string & eventList,const sptr<IRemoteObject> & callback)61 int32_t DataCollectManagerProxy::RequestRiskData(std::string &devId, std::string &eventList,
62 const sptr<IRemoteObject> &callback)
63 {
64 SGLOGI("enter DataCollectManagerProxy RequestRiskData");
65 MessageParcel data;
66 MessageParcel reply;
67
68 if (!data.WriteInterfaceToken(GetDescriptor())) {
69 SGLOGE("WriteInterfaceToken error");
70 return WRITE_ERR;
71 }
72 data.WriteString(eventList);
73 data.WriteRemoteObject(callback);
74
75 MessageOption option = { MessageOption::TF_SYNC };
76 sptr<IRemoteObject> remote = Remote();
77 if (remote == nullptr) {
78 SGLOGE("Remote error");
79 return NULL_OBJECT;
80 }
81 int ret = remote->SendRequest(CMD_DATA_REQUEST, data, reply, option);
82 if (ret != ERR_NONE) {
83 SGLOGE("ret=%{public}d", ret);
84 return ret;
85 }
86 ret = reply.ReadInt32();
87 SGLOGI("reply=%{public}d", ret);
88 return ret;
89 }
90
Subscribe(const SecurityCollector::SecurityCollectorSubscribeInfo & subscribeInfo,const sptr<IRemoteObject> & callback)91 int32_t DataCollectManagerProxy::Subscribe(const SecurityCollector::SecurityCollectorSubscribeInfo &subscribeInfo,
92 const sptr<IRemoteObject> &callback)
93 {
94 SGLOGI("enter DataCollectManagerProxy Subscribe");
95 MessageParcel data;
96 MessageParcel reply;
97
98 if (!data.WriteInterfaceToken(GetDescriptor())) {
99 SGLOGE("WriteInterfaceToken error");
100 return WRITE_ERR;
101 }
102
103 if (!data.WriteParcelable(&subscribeInfo)) {
104 SGLOGE("failed to write parcelable for subscribeInfo");
105 return WRITE_ERR;
106 }
107
108 data.WriteRemoteObject(callback);
109
110 MessageOption option = { MessageOption::TF_SYNC };
111 sptr<IRemoteObject> remote = Remote();
112 if (remote == nullptr) {
113 SGLOGE("Remote error");
114 return NULL_OBJECT;
115 }
116 int ret = remote->SendRequest(CMD_DATA_SUBSCRIBE, data, reply, option);
117 if (ret != ERR_NONE) {
118 SGLOGE("ret=%{public}d", ret);
119 return ret;
120 }
121 ret = reply.ReadInt32();
122 SGLOGD("reply=%{public}d", ret);
123 return ret;
124 }
125
Unsubscribe(const SecurityCollector::SecurityCollectorSubscribeInfo & subscribeInfo,const sptr<IRemoteObject> & callback)126 int32_t DataCollectManagerProxy::Unsubscribe(const SecurityCollector::SecurityCollectorSubscribeInfo &subscribeInfo,
127 const sptr<IRemoteObject> &callback)
128 {
129 SGLOGI("enter DataCollectManagerProxy Unsubscribe");
130 MessageParcel data;
131 MessageParcel reply;
132
133 if (!data.WriteInterfaceToken(GetDescriptor())) {
134 SGLOGE("WriteInterfaceToken error");
135 return WRITE_ERR;
136 }
137
138 if (!data.WriteParcelable(&subscribeInfo)) {
139 SGLOGE("failed to write parcelable for subscribeInfo");
140 return WRITE_ERR;
141 }
142
143 data.WriteRemoteObject(callback);
144
145 MessageOption option = { MessageOption::TF_SYNC };
146 sptr<IRemoteObject> remote = Remote();
147 if (remote == nullptr) {
148 SGLOGE("Remote error");
149 return NULL_OBJECT;
150 }
151 int ret = remote->SendRequest(CMD_DATA_UNSUBSCRIBE, data, reply, option);
152 if (ret != ERR_NONE) {
153 SGLOGE("ret=%{public}d", ret);
154 return ret;
155 }
156 ret = reply.ReadInt32();
157 SGLOGD("reply=%{public}d", ret);
158 return ret;
159 }
160
QuerySecurityEvent(std::vector<SecurityCollector::SecurityEventRuler> rulers,const sptr<IRemoteObject> & callback,const std::string & eventGroup)161 int32_t DataCollectManagerProxy::QuerySecurityEvent(std::vector<SecurityCollector::SecurityEventRuler> rulers,
162 const sptr<IRemoteObject> &callback, const std::string &eventGroup)
163 {
164 SGLOGI("enter DataCollectManagerProxy QuerySecurityEvent");
165 MessageParcel data;
166 MessageParcel reply;
167
168 if (!data.WriteInterfaceToken(GetDescriptor())) {
169 SGLOGE("WriteInterfaceToken error");
170 return WRITE_ERR;
171 }
172
173 if (!data.WriteString(eventGroup)) {
174 SGLOGE("failed to write string for eventGroup");
175 return WRITE_ERR;
176 }
177
178 if (!data.WriteUint32(rulers.size())) {
179 SGLOGE("failed to WriteInt32 for parcelable vector size");
180 return WRITE_ERR;
181 }
182
183 for (const auto &ruler : rulers) {
184 if (!data.WriteParcelable(&ruler)) {
185 SGLOGE("failed to WriteParcelable for parcelable");
186 return WRITE_ERR;
187 }
188 }
189
190 data.WriteRemoteObject(callback);
191
192 MessageOption option = { MessageOption::TF_SYNC };
193 sptr<IRemoteObject> remote = Remote();
194 if (remote == nullptr) {
195 SGLOGE("Remote error");
196 return NULL_OBJECT;
197 }
198 int ret = remote->SendRequest(CMD_SECURITY_EVENT_QUERY, data, reply, option);
199 if (ret != ERR_NONE) {
200 SGLOGE("ret=%{public}d", ret);
201 return ret;
202 }
203 ret = reply.ReadInt32();
204 SGLOGD("reply=%{public}d", ret);
205 return ret;
206 }
207
CollectorStart(const SecurityCollector::SecurityCollectorSubscribeInfo & subscribeInfo,const sptr<IRemoteObject> & callback)208 int32_t DataCollectManagerProxy::CollectorStart(const SecurityCollector::SecurityCollectorSubscribeInfo &subscribeInfo,
209 const sptr<IRemoteObject> &callback)
210 {
211 SGLOGI("enter DataCollectManagerProxy CollectorStart");
212 MessageParcel data;
213 MessageParcel reply;
214
215 if (!data.WriteInterfaceToken(GetDescriptor())) {
216 SGLOGE("WriteInterfaceToken error");
217 return WRITE_ERR;
218 }
219
220 if (!data.WriteParcelable(&subscribeInfo)) {
221 SGLOGE("failed to write parcelable for subscribeInfo");
222 return WRITE_ERR;
223 }
224
225 data.WriteRemoteObject(callback);
226
227 MessageOption option = { MessageOption::TF_SYNC };
228 sptr<IRemoteObject> remote = Remote();
229 if (remote == nullptr) {
230 SGLOGE("Remote error");
231 return NULL_OBJECT;
232 }
233 int ret = remote->SendRequest(CMD_SECURITY_COLLECTOR_START, data, reply, option);
234 if (ret != ERR_NONE) {
235 SGLOGE("ret=%{public}d", ret);
236 return ret;
237 }
238 ret = reply.ReadInt32();
239 SGLOGD("reply=%{public}d", ret);
240 return ret;
241 }
242
CollectorStop(const SecurityCollector::SecurityCollectorSubscribeInfo & subscribeInfo,const sptr<IRemoteObject> & callback)243 int32_t DataCollectManagerProxy::CollectorStop(const SecurityCollector::SecurityCollectorSubscribeInfo &subscribeInfo,
244 const sptr<IRemoteObject> &callback)
245 {
246 SGLOGI("enter DataCollectManagerProxy CollectorStop");
247 MessageParcel data;
248 MessageParcel reply;
249
250 if (!data.WriteInterfaceToken(GetDescriptor())) {
251 SGLOGE("WriteInterfaceToken error");
252 return WRITE_ERR;
253 }
254
255 if (!data.WriteParcelable(&subscribeInfo)) {
256 SGLOGE("failed to write parcelable for subscribeInfo");
257 return WRITE_ERR;
258 }
259
260 data.WriteRemoteObject(callback);
261
262 MessageOption option = { MessageOption::TF_SYNC };
263 sptr<IRemoteObject> remote = Remote();
264 if (remote == nullptr) {
265 SGLOGE("Remote error");
266 return NULL_OBJECT;
267 }
268 int ret = remote->SendRequest(CMD_SECURITY_COLLECTOR_STOP, data, reply, option);
269 if (ret != ERR_NONE) {
270 SGLOGE("ret=%{public}d", ret);
271 return ret;
272 }
273 ret = reply.ReadInt32();
274 SGLOGD("reply=%{public}d", ret);
275 return ret;
276 }
277
ConfigUpdate(const SecurityGuard::SecurityConfigUpdateInfo & updateInfo)278 int32_t DataCollectManagerProxy::ConfigUpdate(const SecurityGuard::SecurityConfigUpdateInfo &updateInfo)
279 {
280 SGLOGI("enter DataCollectManagerProxy ConfigUpdate");
281 MessageParcel data;
282 MessageParcel reply;
283
284 if (!data.WriteInterfaceToken(GetDescriptor())) {
285 SGLOGE("WriteInterfaceToken error");
286 return WRITE_ERR;
287 }
288
289 if (!data.WriteString(updateInfo.GetFileName())) {
290 SGLOGE("failed to write string for config update");
291 return WRITE_ERR;
292 }
293 if (!data.WriteFileDescriptor(updateInfo.GetFd())) {
294 SGLOGE("failed to write file descriptor for config update");
295 return WRITE_ERR;
296 }
297 MessageOption option = { MessageOption::TF_SYNC };
298 sptr<IRemoteObject> remote = Remote();
299 if (remote == nullptr) {
300 SGLOGE("Remote error");
301 return NULL_OBJECT;
302 }
303 int ret = remote->SendRequest(CMD_SECURITY_CONFIG_UPDATE, data, reply, option);
304 if (ret != ERR_NONE) {
305 SGLOGE("ret=%{public}d", ret);
306 return ret;
307 }
308 ret = reply.ReadInt32();
309 SGLOGD("reply=%{public}d", ret);
310 return ret;
311 }
312
QuerySecurityEventConfig(std::string & result)313 int32_t DataCollectManagerProxy::QuerySecurityEventConfig(std::string &result)
314 {
315 SGLOGI("Start DataCollectManagerProxy QuerySecurityEventConfig");
316 MessageParcel data;
317 MessageParcel reply;
318
319 if (!data.WriteInterfaceToken(GetDescriptor())) {
320 SGLOGE("WriteInterfaceToken error");
321 return WRITE_ERR;
322 }
323
324 sptr<IRemoteObject> remote = Remote();
325 if (remote == nullptr) {
326 SGLOGE("Remote error");
327 return NULL_OBJECT;
328 }
329
330 MessageOption option = { MessageOption::TF_SYNC };
331 int ret = remote->SendRequest(CMD_SECURITY_EVENT_CONFIG_QUERY, data, reply, option);
332 if (ret != ERR_NONE) {
333 SGLOGE("ret=%{public}d", ret);
334 return ret;
335 }
336
337 if (!reply.ReadString(result)) {
338 SGLOGE("Failed to get the system integrity result");
339 return BAD_PARAM;
340 }
341 return SUCCESS;
342 }
343
Mute(const SecurityEventFilter & subscribeMute,const sptr<IRemoteObject> & callback,const std::string & sdkFlag)344 int32_t DataCollectManagerProxy::Mute(const SecurityEventFilter &subscribeMute,
345 const sptr<IRemoteObject> &callback, const std::string &sdkFlag)
346 {
347 SGLOGI("Start DataCollectManagerProxy Mute");
348 MessageParcel data;
349 MessageParcel reply;
350
351 if (!data.WriteInterfaceToken(GetDescriptor())) {
352 SGLOGE("WriteInterfaceToken error");
353 return WRITE_ERR;
354 }
355
356 if (!data.WriteParcelable(&subscribeMute)) {
357 SGLOGE("failed to write parcelable for subscribeMute");
358 return WRITE_ERR;
359 }
360
361 if (!data.WriteString(sdkFlag)) {
362 SGLOGE("failed to write sdkFlag for subscribeMute");
363 return WRITE_ERR;
364 }
365 data.WriteRemoteObject(callback);
366 MessageOption option = { MessageOption::TF_SYNC };
367 sptr<IRemoteObject> remote = Remote();
368 if (remote == nullptr) {
369 SGLOGE("Remote error");
370 return NULL_OBJECT;
371 }
372 int ret = remote->SendRequest(CMD_SECURITY_EVENT_MUTE, data, reply, option);
373 if (ret != ERR_NONE) {
374 SGLOGE("ret=%{public}d", ret);
375 return ret;
376 }
377 ret = reply.ReadInt32();
378 SGLOGD("reply=%{public}d", ret);
379 return ret;
380 }
Unmute(const SecurityEventFilter & subscribeMute,const sptr<IRemoteObject> & callback,const std::string & sdkFlag)381 int32_t DataCollectManagerProxy::Unmute(const SecurityEventFilter &subscribeMute,
382 const sptr<IRemoteObject> &callback, const std::string &sdkFlag)
383 {
384 SGLOGI("Start DataCollectManagerProxy Unmute");
385 MessageParcel data;
386 MessageParcel reply;
387
388 if (!data.WriteInterfaceToken(GetDescriptor())) {
389 SGLOGE("WriteInterfaceToken error");
390 return WRITE_ERR;
391 }
392
393 if (!data.WriteParcelable(&subscribeMute)) {
394 SGLOGE("failed to write parcelable for subscribeMute");
395 return WRITE_ERR;
396 }
397
398 if (!data.WriteString(sdkFlag)) {
399 SGLOGE("failed to write sdkFlag for Unmute");
400 return WRITE_ERR;
401 }
402 data.WriteRemoteObject(callback);
403 MessageOption option = { MessageOption::TF_SYNC };
404 sptr<IRemoteObject> remote = Remote();
405 if (remote == nullptr) {
406 SGLOGE("Remote error");
407 return NULL_OBJECT;
408 }
409 int ret = remote->SendRequest(CMD_SECURITY_EVENT_UNMUTE, data, reply, option);
410 if (ret != ERR_NONE) {
411 SGLOGE("ret=%{public}d", ret);
412 return ret;
413 }
414 ret = reply.ReadInt32();
415 SGLOGD("reply=%{public}d", ret);
416 return ret;
417 }
418 }