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_generate_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
PreConstructGenKeyReportParamSet(const struct HksBlob * keyAlias,const struct HksParamSet * paramSetIn,struct InfoPair infoPair,const struct HksBlob * keyIn,struct HksParamSet ** paramSetOut)29 int32_t PreConstructGenKeyReportParamSet(const struct HksBlob *keyAlias, const struct HksParamSet *paramSetIn,
30 struct InfoPair infoPair, const struct HksBlob *keyIn, struct HksParamSet **paramSetOut)
31 {
32 HKS_IF_TRUE_LOGI_RETURN(keyAlias == nullptr || paramSetIn == nullptr, HKS_ERROR_NULL_POINTER,
33 "PreConstructDeleteKeyReportParamSet params is null")
34 int32_t ret = HksInitParamSet(paramSetOut);
35 HKS_IF_NOT_SUCC_LOGI_RETURN(ret, ret, "ConstructGenKeyReportParamSet InitParamSet failed")
36
37 do {
38 ret = PreAddCommonInfo(*paramSetOut, keyAlias, paramSetIn, infoPair.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 key hash to params failed!")
43
44 struct HksParam params[] = {
45 {
46 .tag = HKS_TAG_PARAM1_UINT32,
47 .uint32Param = HKS_EVENT_GENERATE_KEY
48 },
49 {
50 .tag = HKS_TAG_PARAM0_UINT32,
51 .uint32Param = HKS_EVENT_GENERATE_KEY
52 },
53 {
54 .tag = HKS_TAG_TRACE_ID,
55 .uint64Param = infoPair.traceId
56 }
57 };
58 ret = HksAddParams(*paramSetOut, params, HKS_ARRAY_SIZE(params));
59 HKS_IF_NOT_SUCC_LOGI_BREAK(ret, "add in params failed!")
60 } while (0);
61 if (ret != HKS_SUCCESS) {
62 HKS_LOG_I("PreConstructGenKeyReportParamSet failed");
63 HksFreeParamSet(paramSetOut);
64 }
65 return ret;
66 }
67
HksParamSetToEventInfoForKeyGen(const struct HksParamSet * paramSetIn,struct HksEventInfo * eventInfo)68 int32_t HksParamSetToEventInfoForKeyGen(const struct HksParamSet *paramSetIn, struct HksEventInfo *eventInfo)
69 {
70 HKS_IF_TRUE_LOGI_RETURN(paramSetIn == nullptr || eventInfo == nullptr, HKS_ERROR_NULL_POINTER,
71 "HksParamSetToEventInfoForKeyGen params is null")
72 int32_t ret = HKS_SUCCESS;
73 do {
74 ret = GetCommonEventInfo(paramSetIn, eventInfo);
75 HKS_IF_NOT_SUCC_LOGI_BREAK(ret, "report GetCommonEventInfo failed! ret = %" LOG_PUBLIC "d", ret);
76
77 ret = GetEventKeyInfo(paramSetIn, &(eventInfo->keyInfo));
78 HKS_IF_NOT_SUCC_LOGI_BREAK(ret, "report GetEventKeyInfo failed! ret = %" LOG_PUBLIC "d", ret);
79
80 ret = GetEventKeyAccessInfo(paramSetIn, &(eventInfo->keyAccessInfo));
81 HKS_IF_NOT_SUCC_LOGI_BREAK(ret, "report GetEventKeyAccessInfo failed! ret = %" LOG_PUBLIC "d", ret);
82
83 struct HksParam *paramToEventInfo = nullptr;
84 if (HksGetParam(paramSetIn, HKS_TAG_AGREE_ALG, ¶mToEventInfo) == HKS_SUCCESS) {
85 eventInfo->generateInfo.agreeAlg = paramToEventInfo->uint32Param;
86 }
87
88 if (HksGetParam(paramSetIn, HKS_TAG_AGREE_PUBLIC_KEY_IS_KEY_ALIAS, ¶mToEventInfo) == HKS_SUCCESS) {
89 eventInfo->generateInfo.pubKeyIsAlias = static_cast<uint32_t>(paramToEventInfo->boolParam);
90 }
91 } while (0);
92 if (ret != HKS_SUCCESS) {
93 HKS_LOG_I("report ParamSetToEventInfo failed! ret = %" LOG_PUBLIC "d", ret);
94 FreeEventInfoSpecificPtr(eventInfo);
95 }
96 return ret;
97 }
98
HksEventInfoIsNeedReportForKeyGen(const struct HksEventInfo * eventInfo)99 bool HksEventInfoIsNeedReportForKeyGen(const struct HksEventInfo *eventInfo)
100 {
101 return ((eventInfo != nullptr) && (eventInfo->common.result.code != HKS_SUCCESS));
102 }
103
HksEventInfoIsEqualForKeyGen(const struct HksEventInfo * eventInfo1,const struct HksEventInfo * eventInfo2)104 bool HksEventInfoIsEqualForKeyGen(const struct HksEventInfo *eventInfo1, const struct HksEventInfo *eventInfo2)
105 {
106 return CheckEventCommonAndKey(eventInfo1, eventInfo2);
107 }
108
HksEventInfoAddForKeyGen(struct HksEventInfo * dstEventInfo,const struct HksEventInfo * srcEventInfo)109 void HksEventInfoAddForKeyGen(struct HksEventInfo *dstEventInfo, const struct HksEventInfo *srcEventInfo)
110 {
111 if (HksEventInfoIsEqualForKeyGen(dstEventInfo, srcEventInfo)) {
112 dstEventInfo->common.count++;
113 }
114 }
115
HksEventInfoToMapForKeyGen(const struct HksEventInfo * eventInfo,std::unordered_map<std::string,std::string> & reportData)116 int32_t HksEventInfoToMapForKeyGen(const struct HksEventInfo *eventInfo,
117 std::unordered_map<std::string, std::string> &reportData)
118 {
119 HKS_IF_NULL_LOGI_RETURN(eventInfo, HKS_ERROR_NULL_POINTER, "HksEventInfoToMapForKeyGen evenInfo is null")
120
121 auto ret = reportData.insert_or_assign("agree_alg", std::to_string(eventInfo->generateInfo.agreeAlg));
122 HKS_IF_NOT_TRUE_LOGI(ret.second, "reportData insert agree_alg failed!");
123
124 ret = reportData.insert_or_assign("agree_pubkey_is_alias", std::to_string(eventInfo->generateInfo.pubKeyIsAlias));
125 HKS_IF_NOT_TRUE_LOGI(ret.second, "reportData insert agree_pubkey_is_alias failed!");
126
127 ret = EventInfoToMapKeyInfo(&(eventInfo->generateInfo.keyInfo), reportData);
128 HKS_IF_NOT_TRUE_LOGI(ret.second, "reportData EventInfoToMapKeyInfo failed!");
129
130 ret = EventInfoToMapKeyAccessInfo(&(eventInfo->generateInfo.keyAccessInfo), reportData);
131 HKS_IF_NOT_TRUE_LOGI_RETURN(ret.second, HKS_ERROR_BUFFER_TOO_SMALL,
132 "HksEventInfoToMapForKeyGen failed! reportData insert failed!")
133 return HKS_SUCCESS;
134 }