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_report.h"
17 #include "edm_log.h"
18 #include "nlohmann/json.hpp"
19 #ifdef SECURITY_GUARDE_ENABLE
20 #include "event_info.h"
21 #include "sg_collect_client.h"
22 #endif
23 
24 namespace OHOS {
25 namespace EDM {
26 
ReportSecurityInfo(const std::string & bundleName,const std::string & abilityName,const ReportInfo & reportInfo)27 void SecurityReport::ReportSecurityInfo(const std::string &bundleName, const std::string &abilityName,
28     const ReportInfo &reportInfo)
29 {
30 #ifdef SECURITY_GUARDE_ENABLE
31     const int64_t EVENT_ID = 1011015013; // 1011015013: report event id
32     using namespace Security::SecurityGuard;
33     nlohmann::json callPkgJson = nlohmann::json {
34         {"bundleName", bundleName},
35         {"abilityName", abilityName},
36     };
37     nlohmann::json jsonResult;
38     jsonResult["type"] = static_cast<int32_t>(reportInfo.operateType_);
39     jsonResult["subType"] = reportInfo.subType_; // reserved
40     jsonResult["caller"] = callPkgJson;
41     jsonResult["objectInfo"] = reportInfo.policyName_;
42     jsonResult["targettInfo"] = reportInfo.label_; // reserved
43     jsonResult["outcome"] = reportInfo.outcome_;
44     jsonResult["extra"] = reportInfo.extra_; // reserved
45     std::shared_ptr<EventInfo> eventInfo = std::make_shared<EventInfo>(EVENT_ID, "1.1", jsonResult.dump());
46     int32_t ret = OHOS::Security::SecurityGuard::NativeDataCollectKit::ReportSecurityInfo(eventInfo);
47     if (ret != ERR_OK) {
48         EDMLOGE("SecurityReport::ReportSecurityInfo ret: %{public}d", ret);
49     }
50 #endif
51 }
52 } // namespace EDM
53 } // namespace OHOS