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             default: {
63                 break;
64             }
65         }
66     } while (false);
67     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
68 }
69 
HandleDataCollectCmd(MessageParcel &data, MessageParcel &reply)70 int32_t DataCollectManagerStub::HandleDataCollectCmd(MessageParcel &data, MessageParcel &reply)
71 {
72     SGLOGD("%{public}s", __func__);
73     uint32_t expected = sizeof(int64_t);
74     uint32_t actual = data.GetReadableBytes();
75     if (actual <= expected) {
76         SGLOGE("actual length error, value=%{public}u", actual);
77         return BAD_PARAM;
78     }
79 
80     int64_t eventId = data.ReadInt64();
81     std::string version = data.ReadString();
82     std::string time = data.ReadString();
83     std::string content = data.ReadString();
84     return RequestDataSubmit(eventId, version, time, content);
85 }
86 
HandleDataRequestCmd(MessageParcel &data, MessageParcel &reply)87 int32_t DataCollectManagerStub::HandleDataRequestCmd(MessageParcel &data, MessageParcel &reply)
88 {
89     SGLOGD("%{public}s", __func__);
90     const uint32_t expected = 4;
91     uint32_t actual = data.GetReadableBytes();
92     if (actual <= expected) {
93         SGLOGE("actual length error, value=%{public}u", actual);
94         return BAD_PARAM;
95     }
96 
97     std::string devId = {};
98     std::string eventList = data.ReadString();
99     auto object = data.ReadRemoteObject();
100     if (object == nullptr) {
101         SGLOGE("object is nullptr");
102         return BAD_PARAM;
103     }
104     return RequestRiskData(devId, eventList, object);
105 }
106 
HandleDataSubscribeCmd(MessageParcel &data, MessageParcel &reply)107 int32_t DataCollectManagerStub::HandleDataSubscribeCmd(MessageParcel &data, MessageParcel &reply)
108 {
109     SGLOGI("%{public}s", __func__);
110     uint32_t expected = sizeof(uint64_t);
111     uint32_t actual = data.GetReadableBytes();
112     if (actual <= expected) {
113         SGLOGE("actual length error, value=%{public}u", actual);
114         return BAD_PARAM;
115     }
116 
117     std::unique_ptr<SecurityCollector::SecurityCollectorSubscribeInfo> info(
118         data.ReadParcelable<SecurityCollector::SecurityCollectorSubscribeInfo>());
119     if (!info) {
120         SGLOGE("failed to read parcelable for subscribeInfo");
121         return BAD_PARAM;
122     }
123 
124     auto callback = data.ReadRemoteObject();
125     if (callback == nullptr) {
126         SGLOGE("callback is nullptr");
127         return BAD_PARAM;
128     }
129     int32_t ret = Subscribe(*info, callback);
130     reply.WriteInt32(ret);
131     return ret;
132 }
133 
HandleDataUnsubscribeCmd(MessageParcel &data, MessageParcel &reply)134 int32_t DataCollectManagerStub::HandleDataUnsubscribeCmd(MessageParcel &data, MessageParcel &reply)
135 {
136     SGLOGI("%{public}s", __func__);
137     uint32_t expected = sizeof(uint64_t);
138     uint32_t actual = data.GetReadableBytes();
139     if (actual <= expected) {
140         SGLOGE("actual length error, value=%{public}u", actual);
141         return BAD_PARAM;
142     }
143     auto callback = data.ReadRemoteObject();
144     if (callback == nullptr) {
145         SGLOGE("callback is nullptr");
146         return BAD_PARAM;
147     }
148 
149     int32_t ret = Unsubscribe(callback);
150     reply.WriteInt32(ret);
151     return ret;
152 }
153 
HandleSecurityEventQueryCmd(MessageParcel &data, MessageParcel &reply)154 int32_t DataCollectManagerStub::HandleSecurityEventQueryCmd(MessageParcel &data, MessageParcel &reply)
155 {
156     SGLOGI("%{public}s", __func__);
157     uint32_t expected = sizeof(uint32_t);
158     uint32_t actual = data.GetReadableBytes();
159     if (actual <= expected) {
160         SGLOGE("actual length error, value=%{public}u", actual);
161         return BAD_PARAM;
162     }
163 
164     uint32_t size = 0;
165     if (!data.ReadUint32(size)) {
166         SGLOGE("failed to get the event size");
167         return BAD_PARAM;
168     }
169 
170     if (size > MAX_QUERY_EVENT_SIZE) {
171         SGLOGE("the event size error");
172         return BAD_PARAM;
173     }
174     std::vector<SecurityCollector::SecurityEventRuler> rulers;
175     for (uint32_t index = 0; index < size; index++) {
176         std::shared_ptr<SecurityCollector::SecurityEventRuler> event(
177             data.ReadParcelable<SecurityCollector::SecurityEventRuler>());
178         if (event == nullptr) {
179             SGLOGE("failed read security event");
180             return BAD_PARAM;
181         }
182         rulers.emplace_back(*event);
183     }
184 
185     auto callback = data.ReadRemoteObject();
186     if (callback == nullptr) {
187         SGLOGE("callback is nullptr");
188         return BAD_PARAM;
189     }
190 
191     int32_t ret = QuerySecurityEvent(rulers, callback);
192     reply.WriteInt32(ret);
193     return ret;
194 }
195 
HandleStartCmd(MessageParcel &data, MessageParcel &reply)196 int32_t DataCollectManagerStub::HandleStartCmd(MessageParcel &data, MessageParcel &reply)
197 {
198     SGLOGI("in HandleStartCmd");
199     uint32_t expected = sizeof(uint64_t);
200     uint32_t actual = data.GetReadableBytes();
201     if (actual <= expected) {
202         SGLOGE("actual length error, value=%{public}u", actual);
203         return BAD_PARAM;
204     }
205 
206     std::unique_ptr<SecurityCollector::SecurityCollectorSubscribeInfo> info(
207         data.ReadParcelable<SecurityCollector::SecurityCollectorSubscribeInfo>());
208     if (!info) {
209         SGLOGE("failed to read parcelable for start collector");
210         return BAD_PARAM;
211     }
212 
213     auto callback = data.ReadRemoteObject();
214     if (callback == nullptr) {
215         SGLOGE("callback is nullptr");
216         return BAD_PARAM;
217     }
218     int32_t ret = CollectorStart(*info, callback);
219     reply.WriteInt32(ret);
220     return ret;
221 }
222 
HandleStopCmd(MessageParcel &data, MessageParcel &reply)223 int32_t DataCollectManagerStub::HandleStopCmd(MessageParcel &data, MessageParcel &reply)
224 {
225     SGLOGI("%{public}s", __func__);
226     uint32_t expected = sizeof(uint64_t);
227     uint32_t actual = data.GetReadableBytes();
228     if (actual <= expected) {
229         SGLOGE("actual length error, value=%{public}u", actual);
230         return BAD_PARAM;
231     }
232 
233     std::unique_ptr<SecurityCollector::SecurityCollectorSubscribeInfo> info(
234         data.ReadParcelable<SecurityCollector::SecurityCollectorSubscribeInfo>());
235     if (!info) {
236         SGLOGE("failed to read parcelable for stop collector");
237         return BAD_PARAM;
238     }
239 
240     auto callback = data.ReadRemoteObject();
241     if (callback == nullptr) {
242         SGLOGE("callback is nullptr");
243         return BAD_PARAM;
244     }
245     int32_t ret = CollectorStop(*info, callback);
246     reply.WriteInt32(ret);
247     return ret;
248 }
249 
HandleConfigUpdateCmd(MessageParcel &data, MessageParcel &reply)250 int32_t DataCollectManagerStub::HandleConfigUpdateCmd(MessageParcel &data, MessageParcel &reply)
251 {
252     SGLOGI("%{public}s", __func__);
253     uint32_t expected = sizeof(uint64_t);
254     uint32_t actual = data.GetReadableBytes();
255     if (actual <= expected) {
256         SGLOGE("actual length error, value=%{public}u", actual);
257         return BAD_PARAM;
258     }
259 
260     std::string fileName = data.ReadString();
261     if (fileName.empty()) {
262         SGLOGE("failed to read fileName for config update");
263         return BAD_PARAM;
264     }
265     int32_t fd = data.ReadFileDescriptor();
266     if (fd < 0) {
267         SGLOGE("failed to read file fd for config update");
268         return BAD_PARAM;
269     }
270     SecurityGuard::SecurityConfigUpdateInfo info (fd, fileName);
271     int32_t ret = ConfigUpdate(info);
272     reply.WriteInt32(ret);
273     return ret;
274 }
275 
HandleSecurityEventConfigQueryCmd(MessageParcel &data, MessageParcel &reply)276 int32_t DataCollectManagerStub::HandleSecurityEventConfigQueryCmd(MessageParcel &data, MessageParcel &reply)
277 {
278     SGLOGD("%{public}s", __func__);
279     std::string result;
280     int32_t ret = QuerySecurityEventConfig(result);
281     reply.WriteString(result);
282 
283     return ret;
284 }
285 
286 }