1 /*
2 * Copyright (c) 2025-2025 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 <cstdint>
17 #include <string>
18 #include <sys/stat.h>
19 #include <ctime>
20 #include <unordered_map>
21
22 #include "hks_event_info.h"
23 #include "hks_log.h"
24 #include "hks_mem.h"
25 #include "hks_param.h"
26 #include "hks_template.h"
27 #include "hks_type.h"
28 #include "hks_type_enum.h"
29 #include "hks_type_inner.h"
30 #include "hks_ha_plugin.h"
31 #include "securec.h"
32
ThreeStageBuildCommonInfo(const struct HksParamSet * paramSet,struct HksEventInfo * eventInfo)33 static int32_t ThreeStageBuildCommonInfo(const struct HksParamSet *paramSet, struct HksEventInfo *eventInfo)
34 {
35 struct HksParam *param = nullptr;
36 if (HksGetParam(paramSet, HKS_TAG_PARAM3_BUFFER, ¶m) == HKS_SUCCESS) {
37 HKS_IF_TRUE_LOGI_RETURN(param->blob.size < sizeof(HksEventInfo), HKS_ERROR_BUFFER_TOO_SMALL,
38 "blob size is less than eventInfo")
39 *eventInfo = *reinterpret_cast<HksEventInfo *>(param->blob.data);
40 } else {
41 return HKS_FAILURE;
42 }
43
44 if (HksGetParam(paramSet, HKS_TAG_PARAM0_BUFFER, ¶m) == HKS_SUCCESS) {
45 eventInfo->common.function = static_cast<char *>(HksMalloc(param->blob.size));
46 HKS_IF_NULL_LOGI_RETURN(eventInfo->common.function, HKS_ERROR_MALLOC_FAIL, "malloc funcname fail")
47 (void)memcpy_s(eventInfo->common.function, param->blob.size, param->blob.data, param->blob.size);
48 }
49
50 if (HksGetParam(paramSet, HKS_TAG_PARAM1_BUFFER, ¶m) == HKS_SUCCESS) {
51 HKS_IF_TRUE_LOGI_RETURN(param->blob.size < sizeof(struct timespec), HKS_ERROR_BUFFER_TOO_SMALL,
52 "blob size is less than timespec")
53 (void)memcpy_s(&eventInfo->common.time, param->blob.size, param->blob.data, param->blob.size);
54 }
55
56 if (HksGetParam(paramSet, HKS_TAG_PARAM2_BUFFER, ¶m) == HKS_SUCCESS) {
57 eventInfo->common.callerInfo.name = static_cast<char *>(HksMalloc(param->blob.size));
58 HKS_IF_NULL_LOGI_RETURN(eventInfo->common.callerInfo.name, HKS_ERROR_MALLOC_FAIL, "malloc processname fail")
59 (void)memcpy_s(eventInfo->common.callerInfo.name, param->blob.size, param->blob.data, param->blob.size);
60 }
61
62 if (HksGetParam(paramSet, HKS_TAG_PARAM0_NULL, ¶m) == HKS_SUCCESS) {
63 eventInfo->common.result.errMsg = static_cast<char *>(HksMalloc(param->blob.size));
64 HKS_IF_NULL_LOGI_RETURN(eventInfo->common.result.errMsg, HKS_ERROR_MALLOC_FAIL, "malloc error msg fail")
65 (void)memcpy_s((char *)eventInfo->common.result.errMsg, param->blob.size, param->blob.data, param->blob.size);
66 }
67
68 if (HksGetParam(paramSet, HKS_TAG_TRACE_ID, ¶m) == HKS_SUCCESS) {
69 eventInfo->common.traceId = param->uint64Param;
70 }
71
72 eventInfo->common.count = 1;
73 return HKS_SUCCESS;
74 }
75
HksFreeEventInfo(HksEventInfo ** eventInfo)76 void HksFreeEventInfo(HksEventInfo **eventInfo)
77 {
78 HKS_IF_TRUE_LOGI_RETURN_VOID(eventInfo == nullptr || (*eventInfo) == nullptr, "eventInfo is nullptr");
79 HKS_FREE((*eventInfo)->common.function);
80 HKS_FREE((*eventInfo)->common.callerInfo.name);
81 HKS_FREE((*eventInfo)->common.result.errMsg);
82 if ((*eventInfo)->common.eventId == HKS_EVENT_DATA_SIZE_STATISTICS) {
83 HKS_FREE((*eventInfo)->dataSizeInfo.component);
84 HKS_FREE((*eventInfo)->dataSizeInfo.partition);
85 HKS_FREE((*eventInfo)->dataSizeInfo.foldPath);
86 HKS_FREE((*eventInfo)->dataSizeInfo.foldSize);
87 }
88 }
89
BuildCommonInfo(const struct HksParamSet * paramSet,struct HksEventInfo * eventInfo)90 int32_t BuildCommonInfo(const struct HksParamSet *paramSet, struct HksEventInfo *eventInfo)
91 {
92 HKS_IF_TRUE_LOGI_RETURN(paramSet == nullptr || eventInfo == nullptr, HKS_ERROR_NULL_POINTER,
93 "paramset or eventInfo is null")
94 int32_t ret = ThreeStageBuildCommonInfo(paramSet, eventInfo);
95 if (ret != HKS_SUCCESS) {
96 HksFreeEventInfo(&eventInfo);
97 }
98 return ret;
99 }
100
101 // add count, dataLen, totalCost
AddEventInfoCommon(HksEventInfo * info1,const HksEventInfo * info2)102 void AddEventInfoCommon(HksEventInfo *info1, const HksEventInfo *info2)
103 {
104 HKS_IF_TRUE_LOGI_RETURN_VOID(info1 == nullptr || info2 == nullptr, "eventInfo is null")
105 info1->common.count++;
106 if (!IsAdditionOverflow(info1->common.statInfo.dataLen, info2->common.statInfo.dataLen)) {
107 info1->common.statInfo.dataLen += info2->common.statInfo.dataLen;
108 }
109
110 if (!IsAdditionOverflow(info1->common.statInfo.totalCost, info2->common.statInfo.totalCost)) {
111 info1->common.statInfo.totalCost += info2->common.statInfo.totalCost;
112 }
113 }
114
KeyInfoToMap(const HksEventKeyInfo * keyInfo,std::unordered_map<std::string,std::string> & map)115 void KeyInfoToMap(const HksEventKeyInfo *keyInfo, std::unordered_map<std::string, std::string>& map)
116 {
117 std::unordered_map<std::string, std::string> infoMap = {
118 { "alias_hash", std::to_string(keyInfo->aliasHash) },
119 { "storage_level", std::to_string(keyInfo->storageLevel) },
120 { "specific_os_account_id", std::to_string(keyInfo->specificUserId) },
121 { "algorithm", std::to_string(keyInfo->alg) },
122 { "purpose", std::to_string(keyInfo->purpose) },
123 { "key_size", std::to_string(keyInfo->keySize) },
124 { "key_flag", std::to_string(keyInfo->keyFlag) },
125 { "key_hash", std::to_string(keyInfo->keyHash) },
126 { "batch_operation", std::to_string(keyInfo->isBatch) },
127 { "batch_purpose", std::to_string(keyInfo->batchPur) },
128 { "batch_timeout", std::to_string(keyInfo->batchTimeOut) },
129 };
130
131 for (auto info : infoMap) {
132 map.insert_or_assign(info.first, info.second);
133 }
134 }
135
KeyAccessInfoToMap(const HksEventKeyAccessInfo * accessInfo,std::unordered_map<std::string,std::string> & map)136 void KeyAccessInfoToMap(const HksEventKeyAccessInfo *accessInfo, std::unordered_map<std::string, std::string>& map)
137 {
138 std::unordered_map<std::string, std::string> infoMap = {
139 { "auth_type", std::to_string(accessInfo->authType) },
140 { "access_type", std::to_string(accessInfo->accessType) },
141 { "challenge_type", std::to_string(accessInfo->challengeType) },
142 { "challenge_pos", std::to_string(accessInfo->challengePos) },
143 { "auth_timeout", std::to_string(accessInfo->authTimeOut) },
144 { "auth_purpose", std::to_string(accessInfo->authPurpose) },
145 { "front_os_account_id", std::to_string(accessInfo->frontUserId) },
146 { "auth_mode", std::to_string(accessInfo->authMode) },
147 { "need_pwd_set", std::to_string(accessInfo->needPwdSet) },
148 };
149
150 for (auto info : infoMap) {
151 map.insert_or_assign(info.first, info.second);
152 }
153 }
154
CryptoInfoToMap(const HksEventCryptoInfo * cryptoInfo,std::unordered_map<std::string,std::string> & map)155 void CryptoInfoToMap(const HksEventCryptoInfo *cryptoInfo, std::unordered_map<std::string, std::string>& map)
156 {
157 std::unordered_map<std::string, std::string> infoMap = {
158 { "block_mode", std::to_string(cryptoInfo->blockMode) },
159 { "padding", std::to_string(cryptoInfo->padding) },
160 { "digest", std::to_string(cryptoInfo->digest) },
161 { "mgf_digest", std::to_string(cryptoInfo->mgfDigest) },
162 { "handle_id", std::to_string(cryptoInfo->handleId) },
163 };
164
165 for (auto info : infoMap) {
166 map.insert_or_assign(info.first, info.second);
167 }
168 }
169
AgreeDeriveInfoToMap(const HksEventAgreeDeriveInfo * info,std::unordered_map<std::string,std::string> & map)170 void AgreeDeriveInfoToMap(const HksEventAgreeDeriveInfo *info, std::unordered_map<std::string, std::string>& map)
171 {
172 std::unordered_map<std::string, std::string> infoMap = {
173 { "iter_count", std::to_string(info->iterCnt) },
174 { "storage_flag", std::to_string(info->storageFlag) },
175 { "derive_key_size", std::to_string(info->keySize) },
176 { "agree_pubkey_type", std::to_string(info->pubKeyType) },
177 { "handle_id", std::to_string(info->handleId) },
178 };
179
180 for (auto info : infoMap) {
181 map.insert_or_assign(info.first, info.second);
182 }
183 }
184
AttestInfoToMap(const HksEventAttestInfo * attestInfo,std::unordered_map<std::string,std::string> & map)185 void AttestInfoToMap(const HksEventAttestInfo *attestInfo, std::unordered_map<std::string, std::string>& map)
186 {
187 std::unordered_map<std::string, std::string> infoMap = {
188 { "is_annonymous_attest", std::to_string(attestInfo->isAnonymous) },
189 { "attest_cert_type", std::to_string(attestInfo->baseCertType) },
190 };
191
192 for (auto info : infoMap) {
193 map.insert_or_assign(info.first, info.second);
194 }
195 }
196