• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "hks_report_common.h"
17 
18 #include <cerrno>
19 #include <shared_mutex>
20 #include <cstdint>
21 #include <string>
22 #include <sys/stat.h>
23 #include "hilog/log_c.h"
24 #include "hks_event_info.h"
25 #include "hks_log.h"
26 #include "hks_mem.h"
27 #include "hks_param.h"
28 #include "hks_template.h"
29 #include "hks_type.h"
30 #include "hks_type_enum.h"
31 #include "hks_type_inner.h"
32 #include "ipc_skeleton.h"
33 #include "securec.h"
34 #include "hks_util.h"
35 #include "accesstoken_kit.h"
36 #include "hap_token_info.h"
37 #include "ipc_skeleton.h"
38 /*
39 HKS_TAG_PARAM0_UINT32 -> eventId
40 HKS_TAG_PARAM0_BUFFER -> function
41 HKS_TAG_PARAM1_UINT32 -> operation
42 HKS_TAG_PARAM1_BUFFER -> time
43 HKS_TAG_PARAM2_BUFFER -> processName
44 HKS_TAG_PARAM2_UINT32 -> processUid
45 HKS_TAG_PARAM3_BUFFER -> result
46 HKS_TAG_PARAM3_UINT32 -> timeCost
47 HKS_TAG_PARAM0_NULL -> errorMsg
48 
49 HKS_TAG_PARAM4_UINT32 -> keyAliasHash
50 HKS_TAG_PARAM5_UINT32 -> keyHash
51 HKS_TAG_PARAM6_UINT32 -> renameDstKeyAliasHash
52 */
53 
DeConstructReportParamSet(struct HksParamSet ** paramSet)54 void DeConstructReportParamSet(struct HksParamSet **paramSet)
55 {
56     HksFreeParamSet(paramSet);
57 }
58 
AddKeyHash(struct HksParamSet * paramSetOut,const struct HksBlob * keyIn)59 int32_t AddKeyHash(struct HksParamSet *paramSetOut, const struct HksBlob *keyIn)
60 {
61     uint16_t keyHash = static_cast<uint16_t>(HksGetHash(keyIn));
62     struct HksParam keyParams[] = {
63         {
64             .tag = HKS_TAG_PARAM5_UINT32,
65             .uint32Param = (uint32_t)keyHash
66         }
67     };
68     int32_t ret = HksAddParams(paramSetOut, keyParams, HKS_ARRAY_SIZE(keyParams));
69     HKS_IF_NOT_SUCC_LOGI_RETURN(ret, ret, "AddKeyHash failed")
70     return ret;
71 }
72 
AddKeyAliasHash(struct HksParamSet * paramSetOut,const struct HksBlob * keyAlias,enum HksInnerTag paramTag)73 int32_t AddKeyAliasHash(struct HksParamSet *paramSetOut, const struct HksBlob *keyAlias, enum HksInnerTag paramTag)
74 {
75     uint8_t keyAliasHash = static_cast<uint8_t>(HksGetHash(keyAlias));
76     struct HksParam hashKeyAliasParam = {
77         .tag = paramTag,
78         .uint32Param = (uint32_t)keyAliasHash
79     };
80     int32_t ret = HksAddParams(paramSetOut, &hashKeyAliasParam, 1);
81     HKS_IF_NOT_SUCC_LOGI_RETURN(ret, ret, "AddKeyAliasHash failed")
82     return ret;
83 }
84 
AddErrorMessage(struct HksParamSet * paramSetOut)85 static int32_t AddErrorMessage(struct HksParamSet *paramSetOut)
86 {
87     const char *errMsg = HksGetThreadErrorMsg();
88     HKS_IF_NULL_LOGI_RETURN(errMsg, HKS_ERROR_NULL_POINTER, "error msg is null")
89     struct HksParam param = {
90         .tag = HKS_TAG_PARAM0_NULL,
91         .blob = { .size = strlen(errMsg) + 1, .data = (uint8_t*)errMsg },
92     };
93     int32_t ret = HksAddParams(paramSetOut, &param, 1);
94     HKS_IF_NOT_SUCC_LOGI_RETURN(ret, ret, "Add error msg failed")
95     return ret;
96 }
97 
AddTimeCost(struct HksParamSet * paramSetOut,uint64_t startTime)98 int32_t AddTimeCost(struct HksParamSet *paramSetOut, uint64_t startTime)
99 {
100     uint64_t endTime = 0;
101     (void)HksElapsedRealTime(&endTime);
102     uint32_t totalCost = 0;
103     if (endTime >= startTime) {
104         totalCost = static_cast<uint32_t>(endTime - startTime);
105     } else {
106         HKS_LOG_I("startTime is bigger than endTime. diff time: %" LOG_PUBLIC "d",
107             static_cast<uint32_t>(startTime - endTime));
108     }
109     struct HksParam params[] = {
110         {
111             .tag = HKS_TAG_PARAM3_UINT32,
112             .uint32Param = totalCost
113         }
114     };
115     int32_t ret = HksAddParams(paramSetOut, params, HKS_ARRAY_SIZE(params));
116     HKS_IF_NOT_SUCC_LOGI_RETURN(ret, ret, "Add error msg failed")
117     return ret;
118 }
119 
PreAddCommonInfo(struct HksParamSet * paramSetOut,const struct HksBlob * keyAlias,const struct HksParamSet * paramSetIn,uint64_t startTime)120 int32_t PreAddCommonInfo(struct HksParamSet *paramSetOut, const struct HksBlob *keyAlias,
121     const struct HksParamSet *paramSetIn, uint64_t startTime)
122 {
123     HKS_IF_NULL_LOGI_RETURN(paramSetIn, HKS_ERROR_NULL_POINTER, "paramSetIn is null ptr")
124 
125     int32_t ret = AddTimeCost(paramSetOut, startTime);
126     HKS_IF_NOT_SUCC_LOGI_RETURN(ret, ret, "PreAddCommonInfo add time cost to paramSetOut failed!")
127 
128     ret = AddKeyAliasHash(paramSetOut, keyAlias, HKS_TAG_PARAM4_UINT32);
129     HKS_IF_NOT_SUCC_LOGI_RETURN(ret, ret, "PreAddCommonInfo add kayAlias hash to paramSetOut failed!")
130 
131     ret = HksAddParams(paramSetOut, paramSetIn->params, paramSetIn->paramsCnt);
132     HKS_IF_NOT_SUCC_LOGI_RETURN(ret, ret, "PreAddCommonInfo add paramSetIn params to paramSetOut failed!")
133 
134     return ret;
135 }
136 
AddProcessInfo(struct HksParamSet * paramSetOut,const struct HksProcessInfo * processInfo)137 static int32_t AddProcessInfo(struct HksParamSet *paramSetOut, const struct HksProcessInfo *processInfo)
138 {
139     struct HksParam params[] = {
140         {
141             .tag = HKS_TAG_PARAM2_UINT32,
142             .uint32Param = processInfo->uidInt,
143         }
144     };
145     int32_t ret = HksAddParams(paramSetOut, params, HKS_ARRAY_SIZE(params));
146     HKS_IF_NOT_SUCC_LOGI(ret, "AddProcessInfo failed")
147     return ret;
148 }
149 
AddFuncName(struct HksParamSet * paramSetOut,const char * funcName)150 static int32_t AddFuncName(struct HksParamSet *paramSetOut, const char *funcName)
151 {
152     struct HksParam params[]  = {
153         {
154             .tag = HKS_TAG_PARAM0_BUFFER,
155             .blob = { .size = strlen(funcName) + 1, .data = (uint8_t*)funcName },
156         }
157     };
158     int32_t ret = HksAddParams(paramSetOut, params, HKS_ARRAY_SIZE(params));
159     HKS_IF_NOT_SUCC_LOGI(ret, "AddFuncName failed")
160     return ret;
161 }
162 
AddCommonInfo(const char * funcName,const struct HksProcessInfo * processInfo,struct HksParamSet * reportParamSet)163 static int32_t AddCommonInfo(const char *funcName, const struct HksProcessInfo *processInfo,
164     struct HksParamSet *reportParamSet)
165 {
166     int32_t ret = AddProcessInfo(reportParamSet, processInfo);
167     HKS_IF_NOT_SUCC_LOGI_RETURN(ret, ret, "add processInfo to params failed")
168 
169     ret = AddFuncName(reportParamSet, funcName);
170     HKS_IF_NOT_SUCC_LOGI_RETURN(ret, ret, "add function name to params failed")
171 
172     ret = AddErrorMessage(reportParamSet);
173     HKS_IF_NOT_SUCC_LOGI_RETURN(ret, ret, "add error message to params failed")
174     return ret;
175 }
176 
HksGetCallerType(void)177 static enum HksCallerType HksGetCallerType(void)
178 {
179     auto callingTokenId = OHOS::IPCSkeleton::GetCallingTokenID();
180     switch (OHOS::Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(callingTokenId)) {
181         case OHOS::Security::AccessToken::ATokenTypeEnum::TOKEN_HAP:
182             return HKS_HAP_TYPE;
183         case OHOS::Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE:
184         case OHOS::Security::AccessToken::ATokenTypeEnum::TOKEN_SHELL:
185             return HKS_SA_TYPE;
186         default:
187             HKS_LOG_I("Error token type, callerTokenId: %" LOG_PUBLIC "u", callingTokenId);
188             return HKS_UNIFIED_TYPE;
189     }
190 }
191 
ReportGetCallerName(std::string & callerName)192 int32_t ReportGetCallerName(std::string &callerName)
193 {
194     auto callingTokenId = OHOS::IPCSkeleton::GetCallingTokenID();
195     switch (HksGetCallerType()) {
196         case HKS_HAP_TYPE: {
197             OHOS::Security::AccessToken::HapTokenInfo hapTokenInfo;
198             int32_t accessTokenRet = OHOS::Security::AccessToken::AccessTokenKit::GetHapTokenInfo(callingTokenId,
199                 hapTokenInfo);
200             HKS_IF_TRUE_LOGI_RETURN(accessTokenRet != OHOS::Security::AccessToken::AccessTokenKitRet::RET_SUCCESS,
201                 HKS_ERROR_BAD_STATE, "GetHapTokenInfo failed, ret: %" LOG_PUBLIC "d", accessTokenRet)
202             callerName = hapTokenInfo.bundleName;
203             return HKS_SUCCESS;
204         }
205         case HKS_SA_TYPE: {
206             OHOS::Security::AccessToken::NativeTokenInfo saTokenInfo;
207             int32_t accessTokenRet = OHOS::Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(callingTokenId,
208                 saTokenInfo);
209             HKS_IF_TRUE_LOGI_RETURN(accessTokenRet != OHOS::Security::AccessToken::AccessTokenKitRet::RET_SUCCESS,
210                 HKS_ERROR_BAD_STATE, "GetNativeTokenInfo failed, ret: %" LOG_PUBLIC "d", accessTokenRet)
211             callerName = saTokenInfo.processName;
212             return HKS_SUCCESS;
213         }
214         default: {
215             HKS_LOG_I("Invalid caller Type!");
216             return HKS_ERROR_BAD_STATE;
217         }
218     }
219 }
220 
ConstructReportParamSet(const char * funcName,const struct HksProcessInfo * processInfo,int32_t errorCode,struct HksParamSet ** reportParamSet)221 int32_t ConstructReportParamSet(const char *funcName, const struct HksProcessInfo *processInfo,
222     int32_t errorCode, struct HksParamSet **reportParamSet)
223 {
224     HKS_IF_TRUE_LOGI_RETURN(funcName == nullptr || processInfo == nullptr || reportParamSet == nullptr ||
225         *reportParamSet == nullptr, HKS_ERROR_NULL_POINTER, "ConstructReportParamSet params is null")
226     int32_t ret = AddCommonInfo(funcName, processInfo, *reportParamSet);
227     HKS_IF_NOT_SUCC_LOGI_RETURN(ret, ret, "ConstructReportParamSet add common info failed!")
228 
229     do {
230         struct HksEventResultInfo resultInfo = {
231             .code = errorCode,
232             .module = 0,
233             .stage = 0,
234             .errMsg = nullptr
235         };
236         struct timespec time;
237         (void)timespec_get(&time, TIME_UTC);
238         std::string callerName = "Invalid caller name";
239         ret = ReportGetCallerName(callerName);
240         HKS_IF_NOT_SUCC_LOGI(ret, "ReportGetCallerName failed")
241         struct HksParam params[] = {
242             {
243                 .tag = HKS_TAG_PARAM1_BUFFER,
244                 .blob = { .size = sizeof(time), .data = (uint8_t *)&time },
245             },
246             {
247                 .tag = HKS_TAG_PARAM3_BUFFER,
248                 .blob = { .size = sizeof(resultInfo), .data = (uint8_t *)&resultInfo },
249             },
250             {
251                 .tag = HKS_TAG_PARAM2_BUFFER,
252                 .blob = { .size = callerName.size() + 1, .data = (uint8_t *)callerName.c_str() },
253             }
254         };
255         ret = HksAddParams(*reportParamSet, params, HKS_ARRAY_SIZE(params));
256         HKS_IF_NOT_SUCC_LOGI_BREAK(ret, "add params to reportParamSet failed")
257 
258         ret = HksBuildParamSet(reportParamSet);
259         HKS_IF_NOT_SUCC_LOGI_BREAK(ret, "Buil reportParamSet failed")
260     } while (0);
261     if (ret != HKS_SUCCESS) {
262         HKS_LOG_I("ConstructReportParamSet failed");
263         HksFreeParamSet(reportParamSet);
264     }
265     return ret;
266 }
267 
GetCommonEventInfo(const struct HksParamSet * paramSetIn,struct HksEventInfo * eventInfo)268 int32_t GetCommonEventInfo(const struct HksParamSet *paramSetIn, struct HksEventInfo *eventInfo)
269 {
270     HKS_IF_NULL_LOGI_RETURN(paramSetIn, HKS_ERROR_NULL_POINTER, "GetCommonEventInfo paramSetIn is null")
271     HKS_IF_NULL_LOGI_RETURN(eventInfo, HKS_ERROR_NULL_POINTER, "GetCommonEventInfo eventInfo is null")
272 
273     struct HksParam *paramToEventInfo = nullptr;
274     if (HksGetParam(paramSetIn, HKS_TAG_PARAM0_UINT32, &paramToEventInfo) == HKS_SUCCESS) {
275         eventInfo->common.eventId = paramToEventInfo->uint32Param;
276     }
277 
278     if (HksGetParam(paramSetIn, HKS_TAG_PARAM0_BUFFER, &paramToEventInfo) == HKS_SUCCESS) {
279         CopyParamBlobData(&eventInfo->common.function, paramToEventInfo);
280     }
281 
282     if (HksGetParam(paramSetIn, HKS_TAG_PARAM1_UINT32, &paramToEventInfo) == HKS_SUCCESS) {
283         eventInfo->common.operation = paramToEventInfo->uint32Param;
284     }
285 
286     eventInfo->common.count = 1;
287     if (HksGetParam(paramSetIn, HKS_TAG_PARAM1_BUFFER, &paramToEventInfo) == HKS_SUCCESS) {
288         if (paramToEventInfo->blob.size == sizeof(struct timespec)) {
289             eventInfo->common.time = *(struct timespec *)paramToEventInfo->blob.data;
290         }
291     }
292 
293     if (HksGetParam(paramSetIn, HKS_TAG_PARAM2_BUFFER, &paramToEventInfo) == HKS_SUCCESS) {
294         CopyParamBlobData(&eventInfo->common.callerInfo.name, paramToEventInfo);
295     }
296 
297     if (HksGetParam(paramSetIn, HKS_TAG_PARAM2_UINT32, &paramToEventInfo) == HKS_SUCCESS) {
298         eventInfo->common.callerInfo.uid = paramToEventInfo->uint32Param;
299     }
300 
301     if (HksGetParam(paramSetIn, HKS_TAG_PARAM3_BUFFER, &paramToEventInfo) == HKS_SUCCESS) {
302         if (paramToEventInfo->blob.size == sizeof(struct HksEventResultInfo)) {
303             eventInfo->common.result = *(struct HksEventResultInfo *)paramToEventInfo->blob.data;
304         }
305     }
306 
307     if (HksGetParam(paramSetIn, HKS_TAG_PARAM0_NULL, &paramToEventInfo) == HKS_SUCCESS) {
308         CopyParamBlobData(&eventInfo->common.result.errMsg, paramToEventInfo);
309     }
310 
311     if (HksGetParam(paramSetIn, HKS_TAG_PARAM3_UINT32, &paramToEventInfo) == HKS_SUCCESS) {
312         eventInfo->common.statInfo.totalCost = paramToEventInfo->uint32Param;
313     }
314 
315     if (HksGetParam(paramSetIn, HKS_TAG_TRACE_ID, &paramToEventInfo) == HKS_SUCCESS) {
316         eventInfo->common.traceId = paramToEventInfo->uint64Param;
317     }
318     return HKS_SUCCESS;
319 }
320 
FreeEventInfoSpecificPtr(struct HksEventInfo * eventInfo)321 void FreeEventInfoSpecificPtr(struct HksEventInfo *eventInfo)
322 {
323     HksFreeImpl(eventInfo->common.function);
324     HksFreeImpl(eventInfo->common.callerInfo.name);
325     HksFreeImpl(eventInfo->common.result.errMsg);
326 }
327 
CheckKeyInfo(const HksEventKeyInfo * keyInfo1,const HksEventKeyInfo * keyInfo2)328 static bool CheckKeyInfo(const HksEventKeyInfo *keyInfo1, const HksEventKeyInfo *keyInfo2)
329 {
330     return (keyInfo1->alg == keyInfo2->alg) && (keyInfo1->aliasHash == keyInfo2->aliasHash);
331 }
332 
333 // check eventId and caller name are equal
CheckEventCommon(const struct HksEventInfo * info1,const struct HksEventInfo * info2)334 bool CheckEventCommon(const struct HksEventInfo *info1, const struct HksEventInfo *info2)
335 {
336     HKS_IF_TRUE_RETURN(info1 == nullptr || info2 == nullptr, false)
337     HKS_IF_TRUE_RETURN(info1->common.eventId != info2->common.eventId || info1->common.callerInfo.name == nullptr ||
338         info2->common.callerInfo.name == nullptr, false)
339     return strcmp(info1->common.callerInfo.name, info2->common.callerInfo.name) == 0;
340 }
341 
CheckEventCommonAndKey(const struct HksEventInfo * info1,const struct HksEventInfo * info2)342 bool CheckEventCommonAndKey(const struct HksEventInfo *info1, const struct HksEventInfo *info2)
343 {
344     HKS_IF_NOT_TRUE_RETURN(CheckEventCommon(info1, info2), false)
345 
346     switch (info1->common.eventId) {
347         case HKS_EVENT_DERIVE:
348         case HKS_EVENT_AGREE:
349             return CheckKeyInfo(&info1->agreeDeriveInfo.keyInfo, &info2->agreeDeriveInfo.keyInfo);
350         case HKS_EVENT_GENERATE_KEY:
351             return CheckKeyInfo(&info1->generateInfo.keyInfo, &info2->generateInfo.keyInfo);
352         case HKS_EVENT_IMPORT_KEY:
353             return CheckKeyInfo(&info1->importInfo.keyInfo, &info2->importInfo.keyInfo);
354         default:
355             return false;
356     }
357 }
358 
GetEventKeyInfo(const struct HksParamSet * paramSetIn,struct HksEventKeyInfo * keyInfo)359 int32_t GetEventKeyInfo(const struct HksParamSet *paramSetIn, struct HksEventKeyInfo *keyInfo)
360 {
361     HKS_IF_NULL_LOGI_RETURN(paramSetIn, HKS_ERROR_NULL_POINTER, "GetEventKeyInfo paramSetIn is null")
362     HKS_IF_NULL_LOGI_RETURN(keyInfo, HKS_ERROR_NULL_POINTER, "GetEventKeyInfo eventInfo is null")
363     struct HksParam *paramToEventInfo = nullptr;
364     if (HksGetParam(paramSetIn, HKS_TAG_PARAM4_UINT32, &paramToEventInfo) == HKS_SUCCESS) {
365         keyInfo->aliasHash = paramToEventInfo->uint32Param;
366     }
367 
368     if (HksGetParam(paramSetIn, HKS_TAG_AUTH_STORAGE_LEVEL, &paramToEventInfo) == HKS_SUCCESS) {
369         keyInfo->storageLevel = paramToEventInfo->uint32Param;
370     }
371 
372     if (HksGetParam(paramSetIn, HKS_TAG_SPECIFIC_USER_ID, &paramToEventInfo) == HKS_SUCCESS) {
373         keyInfo->specificUserId = paramToEventInfo->int32Param;
374     }
375 
376     if (HksGetParam(paramSetIn, HKS_TAG_ALGORITHM, &paramToEventInfo) == HKS_SUCCESS) {
377         keyInfo->alg = paramToEventInfo->uint32Param;
378     }
379 
380     if (HksGetParam(paramSetIn, HKS_TAG_PURPOSE, &paramToEventInfo) == HKS_SUCCESS) {
381         keyInfo->purpose = paramToEventInfo->uint32Param;
382     }
383 
384     if (HksGetParam(paramSetIn, HKS_TAG_KEY_SIZE, &paramToEventInfo) == HKS_SUCCESS) {
385         keyInfo->keySize = paramToEventInfo->uint32Param;
386     }
387 
388     if (HksGetParam(paramSetIn, HKS_TAG_KEY_FLAG, &paramToEventInfo) == HKS_SUCCESS) {
389         keyInfo->keyFlag = paramToEventInfo->uint32Param;
390     }
391 
392     if (HksGetParam(paramSetIn, HKS_TAG_PARAM5_UINT32, &paramToEventInfo) == HKS_SUCCESS) {
393         keyInfo->keyHash = paramToEventInfo->uint32Param;
394     }
395 
396     if (HksGetParam(paramSetIn, HKS_TAG_IS_BATCH_OPERATION, &paramToEventInfo) == HKS_SUCCESS) {
397         keyInfo->isBatch = paramToEventInfo->boolParam;
398     }
399 
400     if (HksGetParam(paramSetIn, HKS_TAG_BATCH_PURPOSE, &paramToEventInfo) == HKS_SUCCESS) {
401         keyInfo->batchPur = paramToEventInfo->uint32Param;
402     }
403 
404     if (HksGetParam(paramSetIn, HKS_TAG_BATCH_OPERATION_TIMEOUT, &paramToEventInfo) == HKS_SUCCESS) {
405         keyInfo->batchTimeOut = paramToEventInfo->uint32Param;
406     }
407     return HKS_SUCCESS;
408 }
409 
GetEventKeyAccessInfo(const struct HksParamSet * paramSetIn,struct HksEventKeyAccessInfo * keyAccessInfo)410 int32_t GetEventKeyAccessInfo(const struct HksParamSet *paramSetIn, struct HksEventKeyAccessInfo *keyAccessInfo)
411 {
412     HKS_IF_NULL_LOGI_RETURN(paramSetIn, HKS_ERROR_NULL_POINTER, "GetEventKeyAccessInfo paramSetIn is null")
413     HKS_IF_NULL_LOGI_RETURN(keyAccessInfo, HKS_ERROR_NULL_POINTER, "GetEventKeyAccessInfo eventInfo is null")
414     struct HksParam *paramToEventInfo;
415     if (HksGetParam(paramSetIn, HKS_TAG_USER_AUTH_TYPE, &paramToEventInfo) == HKS_SUCCESS) {
416         keyAccessInfo->authType = paramToEventInfo->uint32Param;
417     }
418 
419     if (HksGetParam(paramSetIn, HKS_TAG_KEY_AUTH_ACCESS_TYPE, &paramToEventInfo) == HKS_SUCCESS) {
420         keyAccessInfo->accessType = paramToEventInfo->uint32Param;
421     }
422 
423     if (HksGetParam(paramSetIn, HKS_TAG_CHALLENGE_TYPE, &paramToEventInfo) == HKS_SUCCESS) {
424         keyAccessInfo->challengeType = paramToEventInfo->uint32Param;
425     }
426 
427     if (HksGetParam(paramSetIn, HKS_TAG_CHALLENGE_POS, &paramToEventInfo) == HKS_SUCCESS) {
428         keyAccessInfo->challengePos = paramToEventInfo->uint32Param;
429     }
430 
431     if (HksGetParam(paramSetIn, HKS_TAG_AUTH_TIMEOUT, &paramToEventInfo) == HKS_SUCCESS) {
432         keyAccessInfo->authTimeOut = paramToEventInfo->uint32Param;
433     }
434 
435     if (HksGetParam(paramSetIn, HKS_TAG_KEY_AUTH_PURPOSE, &paramToEventInfo) == HKS_SUCCESS) {
436         keyAccessInfo->authPurpose = paramToEventInfo->uint32Param;
437     }
438 
439     if (HksGetParam(paramSetIn, HKS_TAG_FRONT_USER_ID, &paramToEventInfo) == HKS_SUCCESS) {
440         keyAccessInfo->frontUserId = paramToEventInfo->uint32Param;
441     }
442 
443     if (HksGetParam(paramSetIn, HKS_TAG_USER_AUTH_MODE, &paramToEventInfo) == HKS_SUCCESS) {
444         keyAccessInfo->authMode = paramToEventInfo->uint32Param;
445     }
446 
447     if (HksGetParam(paramSetIn, HKS_TAG_IS_DEVICE_PASSWORD_SET, &paramToEventInfo) == HKS_SUCCESS) {
448         keyAccessInfo->needPwdSet = paramToEventInfo->boolParam;
449     }
450     return HKS_SUCCESS;
451 }
452 
EventInfoToMapKeyInfo(const struct HksEventKeyInfo * eventKeyInfo,std::unordered_map<std::string,std::string> & reportData)453 std::pair<std::unordered_map<std::string, std::string>::iterator, bool> EventInfoToMapKeyInfo(
454     const struct HksEventKeyInfo *eventKeyInfo, std::unordered_map<std::string, std::string> &reportData)
455 {
456     auto ret = reportData.insert_or_assign("alias_hash", std::to_string(eventKeyInfo->aliasHash));
457     HKS_IF_NOT_TRUE_LOGI(ret.second, "reportData insert alias hash failed!");
458 
459     ret = reportData.insert_or_assign("storage_level", std::to_string(eventKeyInfo->storageLevel));
460     HKS_IF_NOT_TRUE_LOGI(ret.second, "reportData insert storage level failed!");
461 
462     ret = reportData.insert_or_assign("specific_os_account_id", std::to_string(eventKeyInfo->specificUserId));
463     HKS_IF_NOT_TRUE_LOGI(ret.second, "reportData insert specific_os_account_id failed!");
464 
465     ret = reportData.insert_or_assign("algorithm", std::to_string(eventKeyInfo->alg));
466     HKS_IF_NOT_TRUE_LOGI(ret.second, "reportData insert algorithm failed!");
467 
468     ret = reportData.insert_or_assign("purpose", std::to_string(eventKeyInfo->purpose));
469     HKS_IF_NOT_TRUE_LOGI(ret.second, "reportData insert purpose failed!");
470 
471     ret = reportData.insert_or_assign("key_size", std::to_string(eventKeyInfo->keySize));
472     HKS_IF_NOT_TRUE_LOGI(ret.second, "reportData insert key_size failed!");
473 
474     ret = reportData.insert_or_assign("key_flag", std::to_string(eventKeyInfo->keyFlag));
475     HKS_IF_NOT_TRUE_LOGI(ret.second, "reportData insert keyFlag failed!");
476 
477     ret = reportData.insert_or_assign("key_hash", std::to_string(eventKeyInfo->keyHash));
478     HKS_IF_NOT_TRUE_LOGI(ret.second, "reportData insert key_hash failed!");
479 
480     ret = reportData.insert_or_assign("batch_operation", std::to_string(eventKeyInfo->isBatch));
481     HKS_IF_NOT_TRUE_LOGI(ret.second, "reportData insert batch_operation failed!");
482 
483     ret = reportData.insert_or_assign("batch_purpose", std::to_string(eventKeyInfo->batchPur));
484     HKS_IF_NOT_TRUE_LOGI(ret.second, "reportData insert batch_purpose failed!");
485 
486     ret = reportData.insert_or_assign("batch_timeout", std::to_string(eventKeyInfo->batchTimeOut));
487     HKS_IF_NOT_TRUE_LOGI(ret.second, "reportData insert batch_timeout failed!");
488     return ret;
489 }
490 
EventInfoToMapKeyAccessInfo(const struct HksEventKeyAccessInfo * eventKeyAccessInfo,std::unordered_map<std::string,std::string> & reportData)491 std::pair<std::unordered_map<std::string, std::string>::iterator, bool> EventInfoToMapKeyAccessInfo(
492     const struct HksEventKeyAccessInfo *eventKeyAccessInfo, std::unordered_map<std::string, std::string> &reportData)
493 {
494     auto ret = reportData.insert_or_assign("auth_type", std::to_string(eventKeyAccessInfo->authType));
495     HKS_IF_NOT_TRUE_LOGI(ret.second, "reportData insert auth_type failed!");
496 
497     ret = reportData.insert_or_assign("access_type", std::to_string(eventKeyAccessInfo->accessType));
498     HKS_IF_NOT_TRUE_LOGI(ret.second, "reportData insert access_type failed!");
499 
500     ret = reportData.insert_or_assign("challenge_type", std::to_string(eventKeyAccessInfo->challengeType));
501     HKS_IF_NOT_TRUE_LOGI(ret.second, "reportData insert challenge_type failed!");
502 
503     ret = reportData.insert_or_assign("challenge_pos", std::to_string(eventKeyAccessInfo->challengePos));
504     HKS_IF_NOT_TRUE_LOGI(ret.second, "reportData insert challenge_pos failed!");
505 
506     ret = reportData.insert_or_assign("auth_timeout", std::to_string(eventKeyAccessInfo->authTimeOut));
507     HKS_IF_NOT_TRUE_LOGI(ret.second, "reportData insert auth_timeout failed!");
508 
509     ret = reportData.insert_or_assign("auth_purpose", std::to_string(eventKeyAccessInfo->authPurpose));
510     HKS_IF_NOT_TRUE_LOGI(ret.second, "reportData insert auth_purpose failed!");
511 
512     ret = reportData.insert_or_assign("front_os_account_id", std::to_string(eventKeyAccessInfo->frontUserId));
513     HKS_IF_NOT_TRUE_LOGI(ret.second, "reportData insert front_os_account_id failed!");
514 
515     ret = reportData.insert_or_assign("auth_mode", std::to_string(eventKeyAccessInfo->authMode));
516     HKS_IF_NOT_TRUE_LOGI(ret.second, "reportData insert auth_mode failed!");
517 
518     ret = reportData.insert_or_assign("need_pwd_set", std::to_string(eventKeyAccessInfo->needPwdSet));
519     HKS_IF_NOT_TRUE_LOGI(ret.second, "reportData insert need_pwd_set failed!");
520     return ret;
521 }
522 
CopyParamBlobData(char ** dst,const struct HksParam * param)523 void CopyParamBlobData(char **dst, const struct HksParam *param)
524 {
525     HKS_IF_TRUE_RETURN_VOID(dst == nullptr || param == nullptr);
526     *dst = static_cast<char *>(HksMalloc(param->blob.size));
527     if (*dst != nullptr) {
528         (void)memset_s(*dst, param->blob.size, 0, param->blob.size);
529         (void)memcpy_s(*dst, param->blob.size, param->blob.data, param->blob.size);
530     }
531 }