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 "risk_analysis_model.h"
17 
18 #include <nlohmann/json.hpp>
19 
20 #include "risk_analysis_define.h"
21 #include "security_guard_log.h"
22 
23 namespace OHOS::Security::SecurityGuard {
SetEventInfo(int64_t eventId,std::string status,std::string & eventInfo)24 void RiskAnalysisModel::SetEventInfo(int64_t eventId, std::string status, std::string &eventInfo)
25 {
26     eventInfo += "[" + std::to_string(eventId) + ":" + status + "]";
27 }
28 
RiskAnalysis(std::vector<SecEvent> & eventData,std::string & eventInfo)29 ErrorCode RiskAnalysisModel::RiskAnalysis(std::vector<SecEvent> &eventData, std::string &eventInfo)
30 {
31     for (const SecEvent &data : eventData) {
32         nlohmann::json jsonObj = nlohmann::json::parse(data.content, nullptr, false);
33         if (jsonObj.is_discarded()) {
34             SGLOGE("json err");
35             return JSON_ERR;
36         }
37 
38         auto content = jsonObj.get<EventContentSt>();
39         if (content.cred != CREDIBLE) {
40             SetEventInfo(data.eventId, "INCREDIBLE", eventInfo);
41             SGLOGE("not cred");
42             continue;
43         }
44 
45         if (content.status != SAFE) {
46             SetEventInfo(data.eventId, "RISK", eventInfo);
47             SGLOGE("status error");
48             return FAILED;
49         }
50         SetEventInfo(data.eventId, "SAFE", eventInfo);
51     }
52 
53     SGLOGI("no risk");
54     return SUCCESS;
55 }
56 }
57