• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "security_collector_manager_stub.h"
17 
18 #include "security_collector_define.h"
19 #include "security_collector_log.h"
20 #include "security_collector_event_filter.h"
21 namespace OHOS::Security::SecurityCollector {
22 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)23 int32_t SecurityCollectorManagerStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
24     MessageParcel &reply, MessageOption &option)
25 {
26     LOGD("%{public}s", __func__);
27     do {
28         if (ISecurityCollectorManager::GetDescriptor() != data.ReadInterfaceToken()) {
29             break;
30         }
31 
32         switch (code) {
33             case CMD_COLLECTOR_SUBCRIBE: {
34                 return HandleSubscribeCmd(data, reply);
35             }
36             case CMD_COLLECTOR_UNSUBCRIBE: {
37                 return HandleUnsubscribeCmd(data, reply);
38             }
39             case CMD_COLLECTOR_START: {
40                 return HandleStartCmd(data, reply);
41             }
42             case CMD_COLLECTOR_STOP: {
43                 return HandleStopCmd(data, reply);
44             }
45             case CMD_SECURITY_EVENT_QUERY: {
46                 return HandleSecurityEventQueryCmd(data, reply);
47             }
48             case CMD_SECURITY_EVENT_MUTE: {
49                 return HandleMute(data, reply);
50             }
51             case CMD_SECURITY_EVENT_UNMUTE: {
52                 return HandleUnmute(data, reply);
53             }
54             default: {
55                 break;
56             }
57         }
58     } while (false);
59     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
60 }
61 
HandleSubscribeCmd(MessageParcel & data,MessageParcel & reply)62 int32_t SecurityCollectorManagerStub::HandleSubscribeCmd(MessageParcel &data, MessageParcel &reply)
63 {
64     LOGI("%{public}s", __func__);
65     uint32_t expected = sizeof(uint64_t);
66     uint32_t actual = data.GetReadableBytes();
67     if (actual <= expected) {
68         LOGE("actual length error, value=%{public}u", actual);
69         return BAD_PARAM;
70     }
71 
72     std::unique_ptr<SecurityCollectorSubscribeInfo> info(data.ReadParcelable<SecurityCollectorSubscribeInfo>());
73     if (!info) {
74         LOGE("failed to read parcelable for subscribeInfo");
75         return BAD_PARAM;
76     }
77 
78     auto callback = data.ReadRemoteObject();
79     if (callback == nullptr) {
80         LOGE("callback is nullptr");
81         return BAD_PARAM;
82     }
83     int32_t ret = Subscribe(*info, callback);
84     reply.WriteInt32(ret);
85     return ret;
86 }
87 
HandleUnsubscribeCmd(MessageParcel & data,MessageParcel & reply)88 int32_t SecurityCollectorManagerStub::HandleUnsubscribeCmd(MessageParcel &data, MessageParcel &reply)
89 {
90     LOGI("%{public}s", __func__);
91     uint32_t expected = sizeof(uint64_t);
92     uint32_t actual = data.GetReadableBytes();
93     if (actual <= expected) {
94         LOGE("actual length error, value=%{public}u", actual);
95         return BAD_PARAM;
96     }
97 
98     auto callback = data.ReadRemoteObject();
99     if (callback == nullptr) {
100         LOGE("callback is nullptr");
101         return BAD_PARAM;
102     }
103 
104     int32_t ret = Unsubscribe(callback);
105     reply.WriteInt32(ret);
106     return ret;
107 }
108 
HandleStartCmd(MessageParcel & data,MessageParcel & reply)109 int32_t SecurityCollectorManagerStub::HandleStartCmd(MessageParcel &data, MessageParcel &reply)
110 {
111     LOGI("in HandleStartCmd");
112     uint32_t expected = sizeof(uint64_t);
113     uint32_t actual = data.GetReadableBytes();
114     if (actual <= expected) {
115         LOGE("actual length error, value=%{public}u", actual);
116         return BAD_PARAM;
117     }
118 
119     std::unique_ptr<SecurityCollectorSubscribeInfo> info(data.ReadParcelable<SecurityCollectorSubscribeInfo>());
120     if (!info) {
121         LOGE("failed to read parcelable for subscribeInfo");
122         return BAD_PARAM;
123     }
124 
125     auto callback = data.ReadRemoteObject();
126     if (callback == nullptr) {
127         LOGE("callback is nullptr");
128         return BAD_PARAM;
129     }
130     int32_t ret = CollectorStart(*info, callback);
131     reply.WriteInt32(ret);
132     return ret;
133 }
134 
HandleStopCmd(MessageParcel & data,MessageParcel & reply)135 int32_t SecurityCollectorManagerStub::HandleStopCmd(MessageParcel &data, MessageParcel &reply)
136 {
137     LOGI("%{public}s", __func__);
138     uint32_t expected = sizeof(uint64_t);
139     uint32_t actual = data.GetReadableBytes();
140     if (actual <= expected) {
141         LOGE("actual length error, value=%{public}u", actual);
142         return BAD_PARAM;
143     }
144 
145     std::unique_ptr<SecurityCollectorSubscribeInfo> info(data.ReadParcelable<SecurityCollectorSubscribeInfo>());
146     if (!info) {
147         LOGE("failed to read parcelable for subscribeInfo");
148         return BAD_PARAM;
149     }
150 
151     auto callback = data.ReadRemoteObject();
152     if (callback == nullptr) {
153         LOGE("callback is nullptr");
154         return BAD_PARAM;
155     }
156     int32_t ret = CollectorStop(*info, callback);
157     reply.WriteInt32(ret);
158     return ret;
159 }
160 
HandleSecurityEventQueryCmd(MessageParcel & data,MessageParcel & reply)161 int32_t SecurityCollectorManagerStub::HandleSecurityEventQueryCmd(MessageParcel &data, MessageParcel &reply)
162 {
163     LOGI("%{public}s", __func__);
164     uint32_t expected = sizeof(uint32_t);
165     uint32_t actual = data.GetReadableBytes();
166     if (actual <= expected) {
167         LOGE("actual length error, value=%{public}u", actual);
168         return BAD_PARAM;
169     }
170 
171     uint32_t size = 0;
172     if (!data.ReadUint32(size)) {
173         LOGE("failed to get the event size");
174         return BAD_PARAM;
175     }
176 
177     if (size > MAX_QUERY_EVENT_SIZE) {
178         LOGE("the ruler size error");
179         return BAD_PARAM;
180     }
181     std::vector<SecurityCollector::SecurityEventRuler> rulers;
182     for (uint32_t index = 0; index < size; index++) {
183         std::shared_ptr<SecurityCollector::SecurityEventRuler> ruler(
184             data.ReadParcelable<SecurityCollector::SecurityEventRuler>());
185         if (ruler == nullptr) {
186             LOGE("failed read security event");
187             return BAD_PARAM;
188         }
189         rulers.emplace_back(*ruler);
190     }
191 
192     std::vector<SecurityCollector::SecurityEvent> events;
193     int32_t ret = QuerySecurityEvent(rulers, events);
194     if (ret != SUCCESS) {
195         LOGE("QuerySecurityEvent failed, ret=%{public}d", ret);
196         return ret;
197     }
198     if (!reply.WriteUint32(static_cast<uint32_t>(events.size()))) {
199         LOGE("failed to WriteInt32 for parcelable vector size");
200         return WRITE_ERR;
201     }
202 
203     for (const auto &event : events) {
204         if (!reply.WriteParcelable(&event)) {
205             LOGE("failed to WriteParcelable for parcelable");
206             return WRITE_ERR;
207         }
208     }
209     return SUCCESS;
210 }
211 
HandleMute(MessageParcel & data,MessageParcel & reply)212 int32_t SecurityCollectorManagerStub::HandleMute(MessageParcel &data, MessageParcel &reply)
213 {
214     LOGI("%{public}s", __func__);
215     uint32_t expected = sizeof(uint64_t);
216     uint32_t actual = data.GetReadableBytes();
217     if (actual <= expected) {
218         LOGE("actual length error, value=%{public}u", actual);
219         return BAD_PARAM;
220     }
221 
222     std::unique_ptr<SecurityCollectorEventFilter> info(
223         data.ReadParcelable<SecurityCollectorEventFilter>());
224     if (!info) {
225         LOGE("failed to read parcelable for mute Info");
226         return BAD_PARAM;
227     }
228     std::string flag {};
229     if (!data.ReadString(flag)) {
230         LOGE("failed to get SubscribeMute flag");
231     }
232     int32_t ret = Mute(*info, flag);
233     reply.WriteInt32(ret);
234     return ret;
235 }
236 
HandleUnmute(MessageParcel & data,MessageParcel & reply)237 int32_t SecurityCollectorManagerStub::HandleUnmute(MessageParcel &data, MessageParcel &reply)
238 {
239     LOGI("%{public}s", __func__);
240     uint32_t expected = sizeof(uint64_t);
241     uint32_t actual = data.GetReadableBytes();
242     if (actual <= expected) {
243         LOGE("actual length error, value=%{public}u", actual);
244         return BAD_PARAM;
245     }
246 
247     std::unique_ptr<SecurityCollectorEventFilter> info(
248         data.ReadParcelable<SecurityCollectorEventFilter>());
249     if (!info) {
250         LOGE("failed to read parcelable for mute Info");
251         return BAD_PARAM;
252     }
253 
254     std::string flag {};
255     if (!data.ReadString(flag)) {
256         LOGE("failed to get SubscribeUnMute flag");
257     }
258     int32_t ret = Unmute(*info, flag);
259     reply.WriteInt32(ret);
260     return ret;
261 }
262 }