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_import_key.h"
17
18 #include <cstdint>
19 #include <string>
20 #include "hks_event_info.h"
21 #include "hks_param.h"
22 #include "hks_template.h"
23 #include "hks_type.h"
24 #include "hks_type_enum.h"
25 #include "hks_type_inner.h"
26 #include "hks_report_common.h"
27
28
PreConstructImportKeyReportParamSet(const struct HksBlob * keyAlias,const struct HksParamSet * paramSetIn,uint64_t startTime,const struct HksBlob * keyIn,struct HksParamSet ** paramSetOut)29 int32_t PreConstructImportKeyReportParamSet(const struct HksBlob *keyAlias, const struct HksParamSet *paramSetIn,
30 uint64_t startTime, const struct HksBlob *keyIn, struct HksParamSet **paramSetOut)
31 {
32 HKS_IF_TRUE_LOGI_RETURN(keyAlias == nullptr || paramSetIn == nullptr, HKS_ERROR_NULL_POINTER,
33 "PreConstructImportKeyReportParamSet params is null")
34 int32_t ret = HksInitParamSet(paramSetOut);
35 HKS_IF_NOT_SUCC_LOGI_RETURN(ret, ret, "PreConstructImportKeyReportParamSet 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 = AddKeyHash(*paramSetOut, keyIn);
42 HKS_IF_NOT_SUCC_LOGI_BREAK(ret, "pre add common info to params failed!")
43
44 struct HksParam params[] = {
45 {
46 .tag = HKS_TAG_PARAM1_UINT32,
47 .uint32Param = HKS_EVENT_IMPORT_KEY
48 },
49 {
50 .tag = HKS_TAG_PARAM0_UINT32,
51 .uint32Param = HKS_EVENT_IMPORT_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("PreConstructImportKeyReportParamSet failed");
59 HksFreeParamSet(paramSetOut);
60 }
61 return ret;
62 }
63
HksParamSetToEventInfoForImport(const struct HksParamSet * paramSetIn,struct HksEventInfo * eventInfo)64 int32_t HksParamSetToEventInfoForImport(const struct HksParamSet *paramSetIn, struct HksEventInfo *eventInfo)
65 {
66 HKS_IF_TRUE_LOGI_RETURN(paramSetIn == nullptr || eventInfo == nullptr, HKS_ERROR_NULL_POINTER,
67 "HksParamSetToEventInfoForImport 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->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_IMPORT_KEY_TYPE, ¶mToEventInfo) == HKS_SUCCESS) {
78 eventInfo->importInfo.keyType = paramToEventInfo->uint32Param;
79 }
80
81 if (HksGetParam(paramSetIn, HKS_TAG_UNWRAP_ALGORITHM_SUITE, ¶mToEventInfo) == HKS_SUCCESS) {
82 eventInfo->importInfo.algSuit = paramToEventInfo->uint32Param;
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
HksEventInfoIsNeedReportForImport(const struct HksEventInfo * eventInfo)92 bool HksEventInfoIsNeedReportForImport(const struct HksEventInfo *eventInfo)
93 {
94 return ((eventInfo != nullptr) && (eventInfo->common.result.code != HKS_SUCCESS));
95 }
96
HksEventInfoIsEqualForImport(const struct HksEventInfo * eventInfo1,const struct HksEventInfo * eventInfo2)97 bool HksEventInfoIsEqualForImport(const struct HksEventInfo *eventInfo1, const struct HksEventInfo *eventInfo2)
98 {
99 return CheckEventCommonAndKey(eventInfo1, eventInfo2);
100 }
101
HksEventInfoAddForImport(struct HksEventInfo * dstEventInfo,const struct HksEventInfo * srcEventInfo)102 void HksEventInfoAddForImport(struct HksEventInfo *dstEventInfo, const struct HksEventInfo *srcEventInfo)
103 {
104 if (HksEventInfoIsEqualForImport(dstEventInfo, srcEventInfo)) {
105 dstEventInfo->common.count++;
106 }
107 }
108
HksEventInfoToMapForImport(const struct HksEventInfo * eventInfo,std::unordered_map<std::string,std::string> & reportData)109 int32_t HksEventInfoToMapForImport(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, "HksEventInfoToMapForImport evenInfo is null")
113 auto ret = reportData.insert_or_assign("import_key_type", std::to_string(eventInfo->importInfo.keyType));
114 HKS_IF_NOT_TRUE_LOGI(ret.second, "reportData insert import_key_type failed!");
115
116 ret = reportData.insert_or_assign("unwrap_algorithm_suit", std::to_string(eventInfo->importInfo.algSuit));
117 HKS_IF_NOT_TRUE_LOGI(ret.second, "reportData insert unwrap_algorithm_suit failed!");
118
119 ret = EventInfoToMapKeyInfo(&(eventInfo->importInfo.keyInfo), reportData);
120 HKS_IF_NOT_TRUE_LOGI(ret.second, "reportData EventInfoToMapKeyInfo failed!");
121
122 ret = EventInfoToMapKeyAccessInfo(&(eventInfo->importInfo.keyAccessInfo), reportData);
123 HKS_IF_NOT_TRUE_LOGI_RETURN(ret.second, HKS_ERROR_BUFFER_TOO_SMALL,
124 "HksEventInfoToMapForImport failed! reportData insert failed!")
125 return HKS_SUCCESS;
126 }