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