1 /*
2 * Copyright (c) 2023-2023 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 #define HUKS_DISABLE_LOG_AT_FILE_TO_REDUCE_ROM_SIZE
16
17 #include "hks_upgrade_key_accesser.h"
18
19 #ifdef HKS_CONFIG_FILE
20 #include HKS_CONFIG_FILE
21 #else
22 #include "hks_config.h"
23 #endif
24
25 #ifdef HKS_ENABLE_UPGRADE_KEY
26
27 #include "huks_access.h"
28 #include "hks_client_service_util.h"
29 #include "hks_log.h"
30 #include "hks_mem.h"
31 #include "hks_template.h"
32 #include "hks_type_inner.h"
33
34 #include "securec.h"
35
36 // add some mandatory params in service, the others mandatory params added in core
AddMandatoryParamsInService(const struct HksParamSet * srcParamSet,struct HksParamSet * targetParamSet)37 static int32_t AddMandatoryParamsInService(const struct HksParamSet *srcParamSet, struct HksParamSet *targetParamSet)
38 {
39 HKS_IF_NULL_RETURN(srcParamSet, HKS_ERROR_INVALID_ARGUMENT)
40 return HksAddParams(targetParamSet, srcParamSet->params, srcParamSet->paramsCnt);
41 }
42
HksDoUpgradeKeyAccess(const struct HksBlob * oldKey,const struct HksParamSet * srcParamSet,struct HksBlob * newKey)43 int32_t HksDoUpgradeKeyAccess(const struct HksBlob *oldKey, const struct HksParamSet *srcParamSet,
44 struct HksBlob *newKey)
45 {
46 int32_t ret = HKS_SUCCESS;
47 struct HksParamSet *paramSet = NULL;
48 do {
49 ret = HksInitParamSet(¶mSet);
50 HKS_IF_NOT_SUCC_LOGE_BREAK(ret, "init paramSet failed!")
51
52 ret = AddMandatoryParamsInService(srcParamSet, paramSet);
53 HKS_IF_NOT_SUCC_LOGE_BREAK(ret, "AddUpgradeParams failed!")
54
55 ret = HksBuildParamSet(¶mSet);
56 HKS_IF_NOT_SUCC_LOGE_BREAK(ret, "build paramSet failed!")
57 ret = HuksAccessUpgradeKey(oldKey, paramSet, newKey);
58 } while (0);
59 HksFreeParamSet(¶mSet);
60 return ret;
61 }
62 #endif /* HKS_ENABLE_UPGRADE_KEY */
63