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
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 default: {
49 break;
50 }
51 }
52 } while (false);
53 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
54 }
55
HandleSubscribeCmd(MessageParcel & data,MessageParcel & reply)56 int32_t SecurityCollectorManagerStub::HandleSubscribeCmd(MessageParcel &data, MessageParcel &reply)
57 {
58 LOGI("%{public}s", __func__);
59 uint32_t expected = sizeof(uint64_t);
60 uint32_t actual = data.GetReadableBytes();
61 if (expected >= actual) {
62 LOGE("actual length error, value=%{public}u", actual);
63 return BAD_PARAM;
64 }
65
66 std::unique_ptr<SecurityCollectorSubscribeInfo> info(data.ReadParcelable<SecurityCollectorSubscribeInfo>());
67 if (!info) {
68 LOGE("failed to read parcelable for subscribeInfo");
69 return BAD_PARAM;
70 }
71
72 auto callback = data.ReadRemoteObject();
73 if (callback == nullptr) {
74 LOGE("callback is nullptr");
75 return BAD_PARAM;
76 }
77 int32_t ret = Subscribe(*info, callback);
78 reply.WriteInt32(ret);
79 return ret;
80 }
81
HandleUnsubscribeCmd(MessageParcel & data,MessageParcel & reply)82 int32_t SecurityCollectorManagerStub::HandleUnsubscribeCmd(MessageParcel &data, MessageParcel &reply)
83 {
84 LOGI("%{public}s", __func__);
85 uint32_t expected = sizeof(uint64_t);
86 uint32_t actual = data.GetReadableBytes();
87 if (expected >= actual) {
88 LOGE("actual length error, value=%{public}u", actual);
89 return BAD_PARAM;
90 }
91
92 auto callback = data.ReadRemoteObject();
93 if (callback == nullptr) {
94 LOGE("callback is nullptr");
95 return BAD_PARAM;
96 }
97
98 int32_t ret = Unsubscribe(callback);
99 reply.WriteInt32(ret);
100 return ret;
101 }
102
HandleStartCmd(MessageParcel & data,MessageParcel & reply)103 int32_t SecurityCollectorManagerStub::HandleStartCmd(MessageParcel &data, MessageParcel &reply)
104 {
105 LOGI("in HandleStartCmd");
106 uint32_t expected = sizeof(uint64_t);
107 uint32_t actual = data.GetReadableBytes();
108 if (expected >= actual) {
109 LOGE("actual length error, value=%{public}u", actual);
110 return BAD_PARAM;
111 }
112
113 std::unique_ptr<SecurityCollectorSubscribeInfo> info(data.ReadParcelable<SecurityCollectorSubscribeInfo>());
114 if (!info) {
115 LOGE("failed to read parcelable for subscribeInfo");
116 return BAD_PARAM;
117 }
118
119 auto callback = data.ReadRemoteObject();
120 if (callback == nullptr) {
121 LOGE("callback is nullptr");
122 return BAD_PARAM;
123 }
124 int32_t ret = CollectorStart(*info, callback);
125 reply.WriteInt32(ret);
126 return ret;
127 }
128
HandleStopCmd(MessageParcel & data,MessageParcel & reply)129 int32_t SecurityCollectorManagerStub::HandleStopCmd(MessageParcel &data, MessageParcel &reply)
130 {
131 LOGI("%{public}s", __func__);
132 uint32_t expected = sizeof(uint64_t);
133 uint32_t actual = data.GetReadableBytes();
134 if (expected >= actual) {
135 LOGE("actual length error, value=%{public}u", actual);
136 return BAD_PARAM;
137 }
138
139 std::unique_ptr<SecurityCollectorSubscribeInfo> info(data.ReadParcelable<SecurityCollectorSubscribeInfo>());
140 if (!info) {
141 LOGE("failed to read parcelable for subscribeInfo");
142 return BAD_PARAM;
143 }
144
145 auto callback = data.ReadRemoteObject();
146 if (callback == nullptr) {
147 LOGE("callback is nullptr");
148 return BAD_PARAM;
149 }
150 int32_t ret = CollectorStop(*info, callback);
151 reply.WriteInt32(ret);
152 return ret;
153 }
154
HandleSecurityEventQueryCmd(MessageParcel & data,MessageParcel & reply)155 int32_t SecurityCollectorManagerStub::HandleSecurityEventQueryCmd(MessageParcel &data, MessageParcel &reply)
156 {
157 LOGI("%{public}s", __func__);
158 uint32_t expected = sizeof(uint32_t);
159 uint32_t actual = data.GetReadableBytes();
160 if (expected >= actual) {
161 LOGE("actual length error, value=%{public}u", actual);
162 return BAD_PARAM;
163 }
164
165 uint32_t size = 0;
166 if (!data.ReadUint32(size)) {
167 LOGE("failed to get the event size");
168 return BAD_PARAM;
169 }
170
171 if (size > MAX_QUERY_EVENT_SIZE) {
172 LOGE("the ruler size error");
173 return BAD_PARAM;
174 }
175 std::vector<SecurityCollector::SecurityEventRuler> rulers;
176 for (uint32_t index = 0; index < size; index++) {
177 std::shared_ptr<SecurityCollector::SecurityEventRuler> ruler(
178 data.ReadParcelable<SecurityCollector::SecurityEventRuler>());
179 if (ruler == nullptr) {
180 LOGE("failed read security event");
181 return BAD_PARAM;
182 }
183 rulers.emplace_back(*ruler);
184 }
185
186 std::vector<SecurityCollector::SecurityEvent> events;
187 int32_t ret = QuerySecurityEvent(rulers, events);
188 if (ret != SUCCESS) {
189 LOGE("QuerySecurityEvent failed, ret=%{public}d", ret);
190 return ret;
191 }
192 if (!reply.WriteUint32(events.size())) {
193 LOGE("failed to WriteInt32 for parcelable vector size");
194 return WRITE_ERR;
195 }
196
197 for (const auto &event : events) {
198 if (!reply.WriteParcelable(&event)) {
199 LOGE("failed to WriteParcelable for parcelable");
200 return WRITE_ERR;
201 }
202 }
203 return SUCCESS;
204 }
205 }