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_rename_key.h"
17
18 #include <cstdint>
19 #include <string>
20 #include "hks_event_info.h"
21 #include "hks_log.h"
22 #include "hks_param.h"
23 #include "hks_template.h"
24 #include "hks_type.h"
25 #include "hks_type_enum.h"
26 #include "hks_type_inner.h"
27 #include "hks_report_common.h"
28
PreConstructRenameReportParamSet(const struct HksBlob * keyAlias,const struct HksBlob * dstKeyAlias,const struct HksParamSet * paramSetIn,uint64_t startTime,struct HksParamSet ** paramSetOut)29 int32_t PreConstructRenameReportParamSet(const struct HksBlob *keyAlias, const struct HksBlob *dstKeyAlias,
30 const struct HksParamSet *paramSetIn, uint64_t startTime, struct HksParamSet **paramSetOut)
31 {
32 HKS_IF_TRUE_LOGI_RETURN(keyAlias == nullptr || paramSetIn == nullptr, HKS_ERROR_NULL_POINTER,
33 "PreConstructRenameReportParamSet params is null")
34 int32_t ret = HksInitParamSet(paramSetOut);
35 HKS_IF_NOT_SUCC_LOGI_RETURN(ret, ret, "PreConstructRenameReportParamSet InitParamSet failed")
36
37 do {
38 ret = PreAddCommonInfo(*paramSetOut, keyAlias, paramSetIn, startTime);
39 HKS_IF_NOT_SUCC_LOGI_BREAK(ret, "pre add common info to params failed!")
40
41 ret = AddKeyAliasHash(*paramSetOut, dstKeyAlias, HKS_TAG_PARAM6_UINT32);
42 HKS_IF_NOT_SUCC_LOGI_BREAK(ret, "pre add key dest alias hash to params failed!")
43
44 struct HksParam params[] = {
45 {
46 .tag = HKS_TAG_PARAM1_UINT32,
47 .uint32Param = HKS_EVENT_RENAME_KEY
48 },
49 {
50 .tag = HKS_TAG_PARAM0_UINT32,
51 .uint32Param = HKS_EVENT_RENAME_KEY
52 },
53 };
54 ret = HksAddParams(*paramSetOut, params, HKS_ARRAY_SIZE(params));
55 HKS_IF_NOT_SUCC_LOGI_BREAK(ret, "add in params failed!")
56 } while (0);
57 if (ret != HKS_SUCCESS) {
58 HKS_LOG_I("PreConstructRenameReportParamSet failed");
59 HksFreeParamSet(paramSetOut);
60 }
61 return ret;
62 }
63
HksParamSetToEventInfoForRename(const struct HksParamSet * paramSetIn,struct HksEventInfo * eventInfo)64 int32_t HksParamSetToEventInfoForRename(const struct HksParamSet *paramSetIn, struct HksEventInfo *eventInfo)
65 {
66 HKS_IF_TRUE_LOGI_RETURN(paramSetIn == nullptr || eventInfo == nullptr, HKS_ERROR_NULL_POINTER,
67 "HksParamSetToEventInfoForRename params is null")
68 int32_t ret = HKS_SUCCESS;
69 do {
70 ret = GetCommonEventInfo(paramSetIn, eventInfo);
71 HKS_IF_NOT_SUCC_LOGI_BREAK(ret, "report GetCommonEventInfo failed! ret = %" LOG_PUBLIC "d", ret);
72
73 ret = GetEventKeyInfo(paramSetIn, &(eventInfo->renameInfo.keyInfo));
74 HKS_IF_NOT_SUCC_LOGI_BREAK(ret, "report GetEventKeyInfo failed! ret = %" LOG_PUBLIC "d", ret);
75
76 struct HksParam *paramToEventInfo = nullptr;
77 if (HksGetParam(paramSetIn, HKS_TAG_PARAM6_UINT32, ¶mToEventInfo) == HKS_SUCCESS) {
78 eventInfo->renameInfo.dstAliasHash = paramToEventInfo->uint32Param;
79 }
80
81 if (HksGetParam(paramSetIn, HKS_TAG_IS_COPY_NEW_KEY, ¶mToEventInfo) == HKS_SUCCESS) {
82 eventInfo->renameInfo.isCopy = static_cast<uint32_t>(paramToEventInfo->boolParam);
83 }
84 } while (0);
85 if (ret != HKS_SUCCESS) {
86 HKS_LOG_I("report ParamSetToEventInfo failed! ret = %" LOG_PUBLIC "d", ret);
87 FreeEventInfoSpecificPtr(eventInfo);
88 }
89 return ret;
90 }
91
HksEventInfoIsNeedReportForRename(const struct HksEventInfo * eventInfo)92 bool HksEventInfoIsNeedReportForRename(const struct HksEventInfo *eventInfo)
93 {
94 return ((eventInfo != nullptr) && (eventInfo->common.result.code != HKS_SUCCESS));
95 }
96
HksEventInfoIsEqualForRename(const struct HksEventInfo * eventInfo1,const struct HksEventInfo * eventInfo2)97 bool HksEventInfoIsEqualForRename(const struct HksEventInfo *eventInfo1, const struct HksEventInfo *eventInfo2)
98 {
99 return CheckEventCommon(eventInfo1, eventInfo2);
100 }
101
HksEventInfoAddForRename(struct HksEventInfo * dstEventInfo,const struct HksEventInfo * srcEventInfo)102 void HksEventInfoAddForRename(struct HksEventInfo *dstEventInfo, const struct HksEventInfo *srcEventInfo)
103 {
104 if (HksEventInfoIsEqualForRename(dstEventInfo, srcEventInfo)) {
105 dstEventInfo->common.count++;
106 }
107 }
108
HksEventInfoToMapForRename(const struct HksEventInfo * eventInfo,std::unordered_map<std::string,std::string> & reportData)109 int32_t HksEventInfoToMapForRename(const struct HksEventInfo *eventInfo,
110 std::unordered_map<std::string, std::string> &reportData)
111 {
112 HKS_IF_NULL_LOGI_RETURN(eventInfo, HKS_ERROR_NULL_POINTER, "HksEventInfoToMapForRename evenInfo is null")
113 auto ret = EventInfoToMapKeyInfo(&eventInfo->renameInfo.keyInfo, reportData);
114 HKS_IF_NOT_TRUE_LOGI(ret.second, "reportData EventInfoToMapKeyInfo failed!");
115
116 ret = reportData.insert_or_assign("dst_alias_hash", std::to_string(eventInfo->renameInfo.dstAliasHash));
117 HKS_IF_NOT_TRUE_LOGI(ret.second, "reportData insert agree_pubkey_is_alias failed!");
118
119 ret = reportData.insert_or_assign("copy_key", std::to_string(eventInfo->renameInfo.isCopy));
120 HKS_IF_NOT_TRUE_LOGI(ret.second, "reportData insert agree_pubkey_is_alias failed!");
121
122 HKS_IF_NOT_TRUE_LOGI_RETURN(ret.second, HKS_ERROR_BUFFER_TOO_SMALL,
123 "HksEventInfoToMapForImport failed! reportData insert failed!")
124 return HKS_SUCCESS;
125 }