• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_stub.h"
17 
18 #include "string_ex.h"
19 
20 #include "security_guard_define.h"
21 #include "security_guard_log.h"
22 
23 namespace OHOS::Security::SecurityGuard {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)24 int32_t DataCollectManagerStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
25     MessageParcel &reply, MessageOption &option)
26 {
27     SGLOGD("%{public}s", __func__);
28     do {
29         if (IDataCollectManager::GetDescriptor() != data.ReadInterfaceToken()) {
30             SGLOGE("Descriptor error");
31             break;
32         }
33 
34         switch (code) {
35             case CMD_DATA_COLLECT: {
36                 return HandleDataCollectCmd(data, reply);
37             }
38             case CMD_DATA_REQUEST: {
39                 return HandleDataRequestCmd(data, reply);
40             }
41             case CMD_DATA_SUBSCRIBE: {
42                 return HandleDataSubscribeCmd(data, reply);
43             }
44             case CMD_DATA_UNSUBSCRIBE: {
45                 return HandleDataUnsubscribeCmd(data, reply);
46             }
47             case CMD_SECURITY_EVENT_QUERY: {
48                 return HandleSecurityEventQueryCmd(data, reply);
49             }
50             case CMD_SECURITY_COLLECTOR_START: {
51                 return HandleStartCmd(data, reply);
52             }
53             case CMD_SECURITY_COLLECTOR_STOP: {
54                 return HandleStopCmd(data, reply);
55             }
56             case CMD_SECURITY_CONFIG_UPDATE: {
57                 return HandleConfigUpdateCmd(data, reply);
58             }
59             case CMD_SECURITY_EVENT_CONFIG_QUERY: {
60                 return HandleSecurityEventConfigQueryCmd(data, reply);
61             }
62             case CMD_SECURITY_EVENT_MUTE: {
63                 return HandleEventMuteCmd(data, reply);
64             }
65             case CMD_SECURITY_EVENT_UNMUTE: {
66                 return HandleEventUnMuteCmd(data, reply);
67             }
68             default: {
69                 break;
70             }
71         }
72     } while (false);
73     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
74 }
75 
HandleDataCollectCmd(MessageParcel & data,MessageParcel & reply)76 int32_t DataCollectManagerStub::HandleDataCollectCmd(MessageParcel &data, MessageParcel &reply)
77 {
78     SGLOGD("%{public}s", __func__);
79     uint32_t expected = sizeof(int64_t);
80     uint32_t actual = data.GetReadableBytes();
81     if (actual <= expected) {
82         SGLOGE("actual length error, value=%{public}u", actual);
83         return BAD_PARAM;
84     }
85 
86     int64_t eventId = data.ReadInt64();
87     std::string version = data.ReadString();
88     std::string time = data.ReadString();
89     std::string content = data.ReadString();
90     return RequestDataSubmit(eventId, version, time, content);
91 }
92 
HandleDataRequestCmd(MessageParcel & data,MessageParcel & reply)93 int32_t DataCollectManagerStub::HandleDataRequestCmd(MessageParcel &data, MessageParcel &reply)
94 {
95     SGLOGD("%{public}s", __func__);
96     const uint32_t expected = 4;
97     uint32_t actual = data.GetReadableBytes();
98     if (actual <= expected) {
99         SGLOGE("actual length error, value=%{public}u", actual);
100         return BAD_PARAM;
101     }
102 
103     std::string devId = {};
104     std::string eventList = data.ReadString();
105     auto object = data.ReadRemoteObject();
106     if (object == nullptr) {
107         SGLOGE("object is nullptr");
108         return BAD_PARAM;
109     }
110     return RequestRiskData(devId, eventList, object);
111 }
112 
HandleDataSubscribeCmd(MessageParcel & data,MessageParcel & reply)113 int32_t DataCollectManagerStub::HandleDataSubscribeCmd(MessageParcel &data, MessageParcel &reply)
114 {
115     SGLOGI("%{public}s", __func__);
116     uint32_t expected = sizeof(uint64_t);
117     uint32_t actual = data.GetReadableBytes();
118     if (actual <= expected) {
119         SGLOGE("actual length error, value=%{public}u", actual);
120         return BAD_PARAM;
121     }
122 
123     std::unique_ptr<SecurityCollector::SecurityCollectorSubscribeInfo> info(
124         data.ReadParcelable<SecurityCollector::SecurityCollectorSubscribeInfo>());
125     if (!info) {
126         SGLOGE("failed to read parcelable for subscribeInfo");
127         return BAD_PARAM;
128     }
129 
130     auto callback = data.ReadRemoteObject();
131     if (callback == nullptr) {
132         SGLOGE("callback is nullptr");
133         return BAD_PARAM;
134     }
135     int32_t ret = Subscribe(*info, callback);
136     reply.WriteInt32(ret);
137     return ret;
138 }
139 
HandleDataUnsubscribeCmd(MessageParcel & data,MessageParcel & reply)140 int32_t DataCollectManagerStub::HandleDataUnsubscribeCmd(MessageParcel &data, MessageParcel &reply)
141 {
142     SGLOGI("%{public}s", __func__);
143     uint32_t expected = sizeof(uint64_t);
144     uint32_t actual = data.GetReadableBytes();
145     if (actual <= expected) {
146         SGLOGE("actual length error, value=%{public}u", actual);
147         return BAD_PARAM;
148     }
149     std::unique_ptr<SecurityCollector::SecurityCollectorSubscribeInfo> info(
150         data.ReadParcelable<SecurityCollector::SecurityCollectorSubscribeInfo>());
151     if (!info) {
152         SGLOGE("failed to read parcelable for unsubscribeInfo");
153         return BAD_PARAM;
154     }
155     auto callback = data.ReadRemoteObject();
156     if (callback == nullptr) {
157         SGLOGE("callback is nullptr");
158         return BAD_PARAM;
159     }
160 
161     int32_t ret = Unsubscribe(*info, callback);
162     reply.WriteInt32(ret);
163     return ret;
164 }
165 
HandleSecurityEventQueryCmd(MessageParcel & data,MessageParcel & reply)166 int32_t DataCollectManagerStub::HandleSecurityEventQueryCmd(MessageParcel &data, MessageParcel &reply)
167 {
168     SGLOGI("%{public}s", __func__);
169     uint32_t expected = sizeof(uint32_t);
170     uint32_t actual = data.GetReadableBytes();
171     if (actual <= expected) {
172         SGLOGE("actual length error, value=%{public}u", actual);
173         return BAD_PARAM;
174     }
175     std::string eventGroup = data.ReadString();
176     uint32_t size = 0;
177     if (!data.ReadUint32(size)) {
178         SGLOGE("failed to get the event size");
179         return BAD_PARAM;
180     }
181 
182     if (size > MAX_QUERY_EVENT_SIZE) {
183         SGLOGE("the event size error");
184         return BAD_PARAM;
185     }
186     std::vector<SecurityCollector::SecurityEventRuler> rulers;
187     for (uint32_t index = 0; index < size; index++) {
188         std::shared_ptr<SecurityCollector::SecurityEventRuler> event(
189             data.ReadParcelable<SecurityCollector::SecurityEventRuler>());
190         if (event == nullptr) {
191             SGLOGE("failed read security event");
192             return BAD_PARAM;
193         }
194         rulers.emplace_back(*event);
195     }
196 
197     auto callback = data.ReadRemoteObject();
198     if (callback == nullptr) {
199         SGLOGE("callback is nullptr");
200         return BAD_PARAM;
201     }
202 
203     int32_t ret = QuerySecurityEvent(rulers, callback, eventGroup);
204     reply.WriteInt32(ret);
205     return ret;
206 }
207 
HandleStartCmd(MessageParcel & data,MessageParcel & reply)208 int32_t DataCollectManagerStub::HandleStartCmd(MessageParcel &data, MessageParcel &reply)
209 {
210     SGLOGI("in HandleStartCmd");
211     uint32_t expected = sizeof(uint64_t);
212     uint32_t actual = data.GetReadableBytes();
213     if (actual <= expected) {
214         SGLOGE("actual length error, value=%{public}u", actual);
215         return BAD_PARAM;
216     }
217 
218     std::unique_ptr<SecurityCollector::SecurityCollectorSubscribeInfo> info(
219         data.ReadParcelable<SecurityCollector::SecurityCollectorSubscribeInfo>());
220     if (!info) {
221         SGLOGE("failed to read parcelable for start collector");
222         return BAD_PARAM;
223     }
224 
225     auto callback = data.ReadRemoteObject();
226     if (callback == nullptr) {
227         SGLOGE("callback is nullptr");
228         return BAD_PARAM;
229     }
230     int32_t ret = CollectorStart(*info, callback);
231     reply.WriteInt32(ret);
232     return ret;
233 }
234 
HandleStopCmd(MessageParcel & data,MessageParcel & reply)235 int32_t DataCollectManagerStub::HandleStopCmd(MessageParcel &data, MessageParcel &reply)
236 {
237     SGLOGI("%{public}s", __func__);
238     uint32_t expected = sizeof(uint64_t);
239     uint32_t actual = data.GetReadableBytes();
240     if (actual <= expected) {
241         SGLOGE("actual length error, value=%{public}u", actual);
242         return BAD_PARAM;
243     }
244 
245     std::unique_ptr<SecurityCollector::SecurityCollectorSubscribeInfo> info(
246         data.ReadParcelable<SecurityCollector::SecurityCollectorSubscribeInfo>());
247     if (!info) {
248         SGLOGE("failed to read parcelable for stop collector");
249         return BAD_PARAM;
250     }
251 
252     auto callback = data.ReadRemoteObject();
253     if (callback == nullptr) {
254         SGLOGE("callback is nullptr");
255         return BAD_PARAM;
256     }
257     int32_t ret = CollectorStop(*info, callback);
258     reply.WriteInt32(ret);
259     return ret;
260 }
261 
HandleConfigUpdateCmd(MessageParcel & data,MessageParcel & reply)262 int32_t DataCollectManagerStub::HandleConfigUpdateCmd(MessageParcel &data, MessageParcel &reply)
263 {
264     SGLOGI("%{public}s", __func__);
265     uint32_t expected = sizeof(uint64_t);
266     uint32_t actual = data.GetReadableBytes();
267     if (actual <= expected) {
268         SGLOGE("actual length error, value=%{public}u", actual);
269         return BAD_PARAM;
270     }
271 
272     std::string fileName = data.ReadString();
273     if (fileName.empty()) {
274         SGLOGE("failed to read fileName for config update");
275         return BAD_PARAM;
276     }
277     int32_t fd = data.ReadFileDescriptor();
278     if (fd < 0) {
279         SGLOGE("failed to read file fd for config update");
280         return BAD_PARAM;
281     }
282     SecurityGuard::SecurityConfigUpdateInfo info (fd, fileName);
283     int32_t ret = ConfigUpdate(info);
284     reply.WriteInt32(ret);
285     return ret;
286 }
287 
HandleSecurityEventConfigQueryCmd(MessageParcel & data,MessageParcel & reply)288 int32_t DataCollectManagerStub::HandleSecurityEventConfigQueryCmd(MessageParcel &data, MessageParcel &reply)
289 {
290     SGLOGD("%{public}s", __func__);
291     std::string result;
292     int32_t ret = QuerySecurityEventConfig(result);
293     reply.WriteString(result);
294 
295     return ret;
296 }
297 
HandleEventMuteCmd(MessageParcel & data,MessageParcel & reply)298 int32_t DataCollectManagerStub::HandleEventMuteCmd(MessageParcel &data, MessageParcel &reply)
299 {
300     SGLOGI("%{public}s", __func__);
301     uint32_t expected = sizeof(uint64_t);
302     uint32_t actual = data.GetReadableBytes();
303     if (actual <= expected) {
304         SGLOGE("actual length error, value=%{public}u", actual);
305         return BAD_PARAM;
306     }
307 
308     std::unique_ptr<SecurityGuard::SecurityEventFilter> info(
309         data.ReadParcelable<SecurityGuard::SecurityEventFilter>());
310     if (!info) {
311         SGLOGE("failed to read parcelable for mute Info");
312         return BAD_PARAM;
313     }
314 
315     std::string sdkFlag = data.ReadString();
316     if (sdkFlag.empty()) {
317         SGLOGE("failed to read sdkFlag for mute Info");
318         return BAD_PARAM;
319     }
320     auto callback = data.ReadRemoteObject();
321     if (callback == nullptr) {
322         SGLOGE("callback is nullptr");
323         return BAD_PARAM;
324     }
325     int32_t ret = Mute(*info, callback, sdkFlag);
326     reply.WriteInt32(ret);
327     return ret;
328 }
329 
HandleEventUnMuteCmd(MessageParcel & data,MessageParcel & reply)330 int32_t DataCollectManagerStub::HandleEventUnMuteCmd(MessageParcel &data, MessageParcel &reply)
331 {
332     SGLOGI("%{public}s", __func__);
333     uint32_t expected = sizeof(uint64_t);
334     uint32_t actual = data.GetReadableBytes();
335     if (actual <= expected) {
336         SGLOGE("actual length error, value=%{public}u", actual);
337         return BAD_PARAM;
338     }
339 
340     std::unique_ptr<SecurityGuard::SecurityEventFilter> info(
341         data.ReadParcelable<SecurityGuard::SecurityEventFilter>());
342     if (!info) {
343         SGLOGE("failed to read parcelable for mute Info");
344         return BAD_PARAM;
345     }
346     std::string sdkFlag = data.ReadString();
347     if (sdkFlag.empty()) {
348         SGLOGE("failed to read sdkFlag for mute Info");
349         return BAD_PARAM;
350     }
351     auto callback = data.ReadRemoteObject();
352     if (callback == nullptr) {
353         SGLOGE("callback is nullptr");
354         return BAD_PARAM;
355     }
356     int32_t ret = Unmute(*info, callback, sdkFlag);
357     reply.WriteInt32(ret);
358     return ret;
359 }
360 }