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 <set>
17 #include "security_guard_sdk_adaptor.h"
18
19 #include "iservice_registry.h"
20
21 #include "data_collect_manager_callback_service.h"
22 #include "data_collect_manager_proxy.h"
23 #include "i_data_collect_manager.h"
24 #include "i_risk_analysis_manager.h"
25 #include "risk_analysis_manager_callback_service.h"
26 #include "risk_analysis_manager_proxy.h"
27 #include "security_collector_manager_callback_service.h"
28 #include "security_collector_manager_proxy.h"
29 #include "security_guard_log.h"
30 #include "security_guard_utils.h"
31 #include "collector_service_loader.h"
32 #include "security_event_query_callback_service.h"
33 #include "acquire_data_manager_callback_service.h"
34 #include "security_config_update_info.h"
35
36 namespace OHOS::Security::SecurityGuard {
37 namespace {
38 const std::set<int64_t> GRANTED_EVENT{1037000001, 1064001001, 1064001002};
39 static std::mutex g_mutex;
40 }
41 sptr<IRemoteObject> SecurityGuardSdkAdaptor::object_ = nullptr;
42 std::map<std::shared_ptr<SecurityCollector::ICollectorSubscriber>,
43 sptr<AcquireDataManagerCallbackService>> SecurityGuardSdkAdaptor::subscribers_ {};
44 std::mutex SecurityGuardSdkAdaptor::objMutex_;
RequestSecurityEventInfo(std::string & devId,std::string & eventList,RequestRiskDataCallback callback)45 int32_t SecurityGuardSdkAdaptor::RequestSecurityEventInfo(std::string &devId, std::string &eventList,
46 RequestRiskDataCallback callback)
47 {
48 auto proxy = LoadDataCollectManageService();
49 if (proxy == nullptr) {
50 SGLOGE("proxy is null");
51 return NULL_OBJECT;
52 }
53
54 auto obj = new (std::nothrow) DataCollectManagerCallbackService(callback);
55 if (obj == nullptr) {
56 SGLOGE("stub is null");
57 return NULL_OBJECT;
58 }
59 int32_t ret = proxy->RequestRiskData(devId, eventList, obj);
60 if (ret != SUCCESS) {
61 SGLOGE("RequestSecurityEventInfo error, ret=%{public}d", ret);
62 return ret;
63 }
64 return SUCCESS;
65 }
66
RequestSecurityModelResult(const std::string & devId,uint32_t modelId,const std::string & param,ResultCallback callback)67 int32_t SecurityGuardSdkAdaptor::RequestSecurityModelResult(const std::string &devId, uint32_t modelId,
68 const std::string ¶m, ResultCallback callback)
69 {
70 auto registry = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
71 if (registry == nullptr) {
72 SGLOGE("GetSystemAbilityManager error");
73 return NULL_OBJECT;
74 }
75
76 auto object = registry->GetSystemAbility(RISK_ANALYSIS_MANAGER_SA_ID);
77 auto proxy = iface_cast<RiskAnalysisManagerProxy>(object);
78 if (proxy == nullptr) {
79 SGLOGE("proxy is null");
80 return NULL_OBJECT;
81 }
82
83 sptr<RiskAnalysisManagerCallbackService> stub = new (std::nothrow) RiskAnalysisManagerCallbackService(callback);
84 if (stub == nullptr) {
85 SGLOGE("stub is null");
86 return NULL_OBJECT;
87 }
88 int32_t ret = proxy->RequestSecurityModelResult(devId, modelId, param, stub);
89 SGLOGI("RequestSecurityModelResult result, ret=%{public}d", ret);
90 return ret;
91 }
92
ReportSecurityInfo(const std::shared_ptr<EventInfo> & info)93 int32_t SecurityGuardSdkAdaptor::ReportSecurityInfo(const std::shared_ptr<EventInfo> &info)
94 {
95 if (info == nullptr) {
96 return BAD_PARAM;
97 }
98 auto registry = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
99 if (registry == nullptr) {
100 SGLOGE("GetSystemAbilityManager error");
101 return NULL_OBJECT;
102 }
103 auto object = registry->GetSystemAbility(DATA_COLLECT_MANAGER_SA_ID);
104 auto proxy = iface_cast<IDataCollectManager>(object);
105 if (proxy == nullptr) {
106 SGLOGE("proxy is null");
107 return NULL_OBJECT;
108 }
109 int64_t eventId = info->GetEventId();
110 std::string version = info->GetVersion();
111 std::string content = info->GetContent();
112 std::string date = SecurityGuardUtils::GetDate();
113 int32_t ret = proxy->RequestDataSubmit(eventId, version, date, content);
114 if (ret != SUCCESS) {
115 SGLOGE("RequestSecurityInfo error, ret=%{public}d", ret);
116 return ret;
117 }
118 return SUCCESS;
119 }
120
SetModelState(uint32_t modelId,bool enable)121 int32_t SecurityGuardSdkAdaptor::SetModelState(uint32_t modelId, bool enable)
122 {
123 auto registry = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
124 if (registry == nullptr) {
125 SGLOGE("GetSystemAbilityManager error");
126 return NULL_OBJECT;
127 }
128
129 auto object = registry->GetSystemAbility(RISK_ANALYSIS_MANAGER_SA_ID);
130 auto proxy = iface_cast<RiskAnalysisManagerProxy>(object);
131 if (proxy == nullptr) {
132 SGLOGE("proxy is null");
133 return NULL_OBJECT;
134 }
135
136 int32_t ret = proxy->SetModelState(modelId, enable);
137 SGLOGI("SetModelState result, ret=%{public}d", ret);
138 return ret;
139 }
140
NotifyCollector(const SecurityCollector::Event & event,int64_t duration)141 int32_t SecurityGuardSdkAdaptor::NotifyCollector(const SecurityCollector::Event &event, int64_t duration)
142 {
143 SGLOGI("On NotifyCollector...");
144 if (GRANTED_EVENT.find(event.eventId) == GRANTED_EVENT.end()) {
145 SGLOGE("NotifyCollector error event id %{public}" PRId64 ", can not Notify", event.eventId);
146 return BAD_PARAM;
147 }
148 auto object = SecurityCollector::CollectorServiceLoader::GetInstance().LoadCollectorService();
149 auto proxy = iface_cast<SecurityCollector::ISecurityCollectorManager>(object);
150 if (proxy == nullptr) {
151 SGLOGE("proxy is null");
152 return NULL_OBJECT;
153 }
154
155 SecurityCollector::SecurityCollectorSubscribeInfo subscriberInfo{event, duration, true};
156 sptr<SecurityCollector::SecurityCollectorManagerCallbackService> callback =
157 new (std::nothrow) SecurityCollector::SecurityCollectorManagerCallbackService(nullptr);
158 if (callback == nullptr) {
159 SGLOGE("callback is null");
160 return NULL_OBJECT;
161 }
162 int32_t ret = proxy->Subscribe(subscriberInfo, callback);
163 SGLOGI("NotifyCollector result, ret=%{public}d", ret);
164 return ret;
165 }
166
StartCollector(const SecurityCollector::Event & event,int64_t duration)167 int32_t SecurityGuardSdkAdaptor::StartCollector(const SecurityCollector::Event &event,
168 int64_t duration)
169 {
170 auto proxy = LoadDataCollectManageService();
171 if (proxy == nullptr) {
172 SGLOGE("proxy is null");
173 return NULL_OBJECT;
174 }
175
176 SecurityCollector::SecurityCollectorSubscribeInfo subscriberInfo{event, duration, true};
177 sptr<SecurityCollector::SecurityCollectorManagerCallbackService> callback =
178 new (std::nothrow) SecurityCollector::SecurityCollectorManagerCallbackService(nullptr);
179 if (callback == nullptr) {
180 SGLOGE("callback is null");
181 return NULL_OBJECT;
182 }
183
184 int32_t ret = proxy->CollectorStart(subscriberInfo, callback);
185 SGLOGI("StartCollector result, ret=%{public}d", ret);
186 return ret;
187 }
188
StopCollector(const SecurityCollector::Event & event)189 int32_t SecurityGuardSdkAdaptor::StopCollector(const SecurityCollector::Event &event)
190 {
191 SGLOGD("in SecurityGuardSdkAdaptor StopCollector ************");
192 auto proxy = LoadDataCollectManageService();
193 if (proxy == nullptr) {
194 SGLOGE("proxy is null");
195 return NULL_OBJECT;
196 }
197
198 SecurityCollector::SecurityCollectorSubscribeInfo subscriberInfo{event, -1, true};
199 sptr<SecurityCollector::SecurityCollectorManagerCallbackService> callback =
200 new (std::nothrow) SecurityCollector::SecurityCollectorManagerCallbackService(nullptr);
201 if (callback == nullptr) {
202 SGLOGE("callback is null");
203 return NULL_OBJECT;
204 }
205
206 int32_t ret = proxy->CollectorStop(subscriberInfo, callback);
207 SGLOGI("StopCollector result, ret=%{public}d", ret);
208 return ret;
209 }
210
QuerySecurityEvent(std::vector<SecurityCollector::SecurityEventRuler> rulers,std::shared_ptr<SecurityEventQueryCallback> callback)211 int32_t SecurityGuardSdkAdaptor::QuerySecurityEvent(std::vector<SecurityCollector::SecurityEventRuler> rulers,
212 std::shared_ptr<SecurityEventQueryCallback> callback)
213 {
214 auto proxy = LoadDataCollectManageService();
215 if (proxy == nullptr) {
216 SGLOGE("proxy is null");
217 return NULL_OBJECT;
218 }
219
220 auto obj = new (std::nothrow) SecurityEventQueryCallbackService(callback);
221 if (obj == nullptr) {
222 SGLOGE("obj is null");
223 return NULL_OBJECT;
224 }
225
226 int32_t ret = proxy->QuerySecurityEvent(rulers, obj);
227 if (ret != SUCCESS) {
228 SGLOGE("QuerySecurityEvent error, ret=%{public}d", ret);
229 return ret;
230 }
231 return SUCCESS;
232 }
233
Subscribe(const std::shared_ptr<SecurityCollector::ICollectorSubscriber> & subscriber)234 int32_t SecurityGuardSdkAdaptor::Subscribe(const std::shared_ptr<SecurityCollector::ICollectorSubscriber> &subscriber)
235 {
236 if (subscriber == nullptr) {
237 SGLOGE("subscriber is nullptr");
238 return NULL_OBJECT;
239 }
240 if (subscribers_.find(subscriber) != subscribers_.end()) {
241 SGLOGE("the callback has been registered.");
242 return BAD_PARAM;
243 }
244 auto proxy = LoadDataCollectManageService();
245 if (proxy == nullptr) {
246 SGLOGE("proxy is null");
247 return NULL_OBJECT;
248 }
249
250 auto obj = new (std::nothrow) AcquireDataManagerCallbackService(subscriber);
251 if (obj == nullptr) {
252 SGLOGE("obj is null");
253 return NULL_OBJECT;
254 }
255
256 int32_t ret = proxy->Subscribe(subscriber->GetSubscribeInfo(), obj);
257 if (ret != SUCCESS) {
258 SGLOGE("Subscribe error, ret=%{public}d", ret);
259 return ret;
260 }
261 {
262 std::lock_guard<std::mutex> lock(g_mutex);
263 subscribers_[subscriber] = obj;
264 }
265 return SUCCESS;
266 }
267
Unsubscribe(const std::shared_ptr<SecurityCollector::ICollectorSubscriber> & subscriber)268 int32_t SecurityGuardSdkAdaptor::Unsubscribe(const std::shared_ptr<SecurityCollector::ICollectorSubscriber> &subscriber)
269 {
270 if (subscriber == nullptr) {
271 SGLOGE("subscriber is nullptr");
272 return NULL_OBJECT;
273 }
274 auto iter = subscribers_.find(subscriber);
275 if (iter == subscribers_.end()) {
276 SGLOGE("the callback has not been registered.");
277 return BAD_PARAM;
278 }
279 auto proxy = LoadDataCollectManageService();
280 if (proxy == nullptr) {
281 SGLOGE("proxy is null");
282 return NULL_OBJECT;
283 }
284
285 int32_t ret = proxy->Unsubscribe(subscribers_[subscriber]);
286 if (ret != SUCCESS) {
287 SGLOGE("Unsubscribe error, ret=%{public}d", ret);
288 return ret;
289 }
290 {
291 std::lock_guard<std::mutex> lock(g_mutex);
292 subscribers_.erase(iter);
293 }
294 return SUCCESS;
295 }
296
LoadDataCollectManageService()297 sptr<IDataCollectManager> SecurityGuardSdkAdaptor::LoadDataCollectManageService()
298 {
299 std::lock_guard<std::mutex> lock(objMutex_);
300 if (object_ != nullptr) {
301 SGLOGI("object_ not null");
302 return iface_cast<IDataCollectManager>(object_);
303 }
304 auto registry = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
305 if (registry == nullptr) {
306 SGLOGE("GetSystemAbilityManager error");
307 return nullptr;
308 }
309 object_ = registry->GetSystemAbility(DATA_COLLECT_MANAGER_SA_ID);
310 return iface_cast<IDataCollectManager>(object_);
311 }
312
ConfigUpdate(const SecurityGuard::SecurityConfigUpdateInfo & updateInfo)313 int32_t SecurityGuardSdkAdaptor::ConfigUpdate(const SecurityGuard::SecurityConfigUpdateInfo &updateInfo)
314 {
315 auto proxy = LoadDataCollectManageService();
316 if (proxy == nullptr) {
317 SGLOGE("proxy is null");
318 return NULL_OBJECT;
319 }
320
321 int32_t ret = proxy->ConfigUpdate(updateInfo);
322 if (ret != SUCCESS) {
323 SGLOGE("ConfigUpdate error, ret=%{public}d", ret);
324 return ret;
325 }
326 return SUCCESS;
327 }
328 } // OHOS::Security::SecurityGuard