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 "cm_security_guard_report.h"
17 
18 #include "cm_log.h"
19 #include "cm_mem.h"
20 
21 #include "ipc_skeleton.h"
22 
23 #ifdef SUPPORT_SECURITY_GUARD
24 
25 #include "event_info.h"
26 #include "sg_collect_client.h"
27 
28 #define CM_INFO_JSON_MAX_LEN 512
29 #define SG_JSON_MAX_LEN 1024
30 #define CERT_EVENTID 1011015014
31 #define CERT_VERSION "1.0"
32 
33 using namespace OHOS::Security::SecurityGuard;
34 
CmGetCallingUid(void)35 uint32_t CmGetCallingUid(void)
36 {
37     return OHOS::IPCSkeleton::GetCallingUid();
38 }
39 
InfoToJson(const struct CmReportSGInfo * info,char * json,int32_t jsonLen)40 void InfoToJson(const struct CmReportSGInfo *info, char *json, int32_t jsonLen)
41 {
42     int32_t ret = snprintf_s(json, jsonLen, jsonLen - 1, "{\\\"action\\\":\\\"%s\\\", \\\"uid\\\":%u, "
43         "\\\"result\\\":%d, \\\"name\\\":\\\"%s\\\", \\\"isSetGrantUid\\\":%d, \\\"grantUid\\\":%u,"
44         "\\\"isSetStatus\\\":%d, \\\"status\\\":%d}", info->action, info->uid, info->result, info->name,
45         info->isSetGrantUid ? 1 : 0, info->grantUid, info->isSetStatus ? 1 : 0, info->status ? 1 : 0);
46     if (ret < 0) {
47         CM_LOG_E("info to json error");
48     }
49 }
50 
CmFillSGRecord(char * objectInfoJson,char * recordJson,int32_t recordJsonLen)51 void CmFillSGRecord(char *objectInfoJson, char *recordJson, int32_t recordJsonLen)
52 {
53     struct SGEventContent content;
54     (void)memset_s(&content, sizeof(content), 0, sizeof(content));
55     char constant[] = "";
56     content.type = 0;
57     content.subType = 0;
58     content.caller = constant;
59     content.objectInfo = objectInfoJson;
60     content.bootTime = constant;
61     content.wallTime = constant;
62     content.outcome = constant;
63     content.sourceInfo = constant;
64     content.targetInfo = constant;
65     content.extra = constant;
66     int32_t ret = snprintf_s(recordJson, recordJsonLen, recordJsonLen - 1, "{\"type\":%d, \"subType\":%d,"
67         "\"caller\":\"%s\", \"objectInfo\":\"%s\", \"bootTime\":\"%s\", \"wallTime\":\"%s\", \"outcome\":\"%s\", "
68         "\"sourceInfo\":\"%s\", \"targetInfo\":\"%s\", \"extra\":\"%s\"}", content.type, content.subType,
69         content.caller, content.objectInfo, content.bootTime, content.wallTime, content.outcome, content.sourceInfo,
70         content.targetInfo, content.extra);
71     if (ret < 0) {
72         CM_LOG_E("fill SG record error");
73     }
74 }
75 
CmReportSGRecord(const struct CmReportSGInfo * info)76 void CmReportSGRecord(const struct CmReportSGInfo *info)
77 {
78     char *objectJson = static_cast<char *>(CmMalloc(CM_INFO_JSON_MAX_LEN));
79     if (objectJson == NULL) {
80         CM_LOG_E("objectJson malloc error");
81         return;
82     }
83     (void)memset_s(objectJson, CM_INFO_JSON_MAX_LEN, 0, CM_INFO_JSON_MAX_LEN);
84     InfoToJson(info, objectJson, CM_INFO_JSON_MAX_LEN);
85 
86     char *recordJson = static_cast<char *>(CmMalloc(SG_JSON_MAX_LEN));
87     if (recordJson == NULL) {
88         CM_FREE_PTR(objectJson);
89         CM_LOG_E("recordJson malloc error");
90         return;
91     }
92     (void)memset_s(recordJson, SG_JSON_MAX_LEN, 0, SG_JSON_MAX_LEN);
93     CmFillSGRecord(objectJson, recordJson, SG_JSON_MAX_LEN);
94     CM_FREE_PTR(objectJson);
95     std::shared_ptr<EventInfo> eventInfo = std::make_shared<EventInfo>(CERT_EVENTID, CERT_VERSION, recordJson);
96     int32_t ret = NativeDataCollectKit::ReportSecurityInfo(eventInfo);
97     if (ret != 0) {
98         CM_LOG_E("report security info error");
99     }
100     CM_FREE_PTR(recordJson);
101     return;
102 }
103 #endif