• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 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_test_modify_old_key.h"
17 
18 #include <dirent.h>
19 #include <errno.h>
20 #include <unistd.h>
21 #include <sys/types.h>
22 #include <stdio.h>
23 
24 #include "hks_client_service.h"
25 #include "hks_type_inner.h"
26 #include "hks_storage.h"
27 #include "hks_param.h"
28 #include "hks_log.h"
29 #include "hks_storage_manager.h"
30 #include "hks_core_service_key_attest.h"
31 #include "hks_core_service_key_chipset_platform_derive.h"
32 #include "hks_core_service_key_generate.h"
33 #include "hks_core_service_key_operate_one_stage.h"
34 #include "hks_core_service_key_operate_three_stage.h"
35 #include "hks_core_service_key_other.h"
36 
37 #define KEY_MAX_SIZE 4096
38 #define DIR_TYPE 4
39 #define DEFAULT_PATH_LEN 1024
40 
HksTestGenerateOldKey(const struct HksBlob * keyAlias,const struct HksParamSet * paramSet,const struct HksProcessInfo * processInfo)41 int32_t HksTestGenerateOldKey(const struct HksBlob *keyAlias, const struct HksParamSet *paramSet,
42     const struct HksProcessInfo *processInfo)
43 {
44     HKS_LOG_I("enter HksTestGenerateOldKey");
45 
46     struct HksParamSet *newParamSet = NULL;
47     (void)HksInitParamSet(&newParamSet);
48 
49     (void)HksAddParams(newParamSet, paramSet->params, paramSet->paramsCnt);
50 
51     struct HksParam tmpParam;
52     tmpParam.tag = HKS_TAG_PROCESS_NAME;
53     tmpParam.blob = processInfo->processName;
54 
55     (void)HksAddParams(newParamSet, &tmpParam, 1);
56 
57     (void)HksBuildParamSet(&newParamSet);
58 
59     uint8_t keyData[KEY_MAX_SIZE] = { 0 };
60     struct HksBlob keyBlob = { .size = KEY_MAX_SIZE, .data = keyData };
61 
62     (void)HksCoreGenerateKey(keyAlias, newParamSet, NULL, &keyBlob);
63 
64     (void)HksManageStoreKeyBlob(processInfo, newParamSet, keyAlias, &keyBlob, HKS_STORAGE_TYPE_KEY);
65 
66     HksFreeParamSet(&newParamSet);
67     return HKS_SUCCESS;
68 }
69 
HksTestDeleteOldKey(const struct HksBlob * keyAlias,const struct HksProcessInfo * processInfo)70 int32_t HksTestDeleteOldKey(const struct HksBlob *keyAlias, const struct HksProcessInfo *processInfo)
71 {
72     return HksServiceDeleteKey(processInfo, keyAlias, NULL);
73 }
74 
HksTestOldKeyExist(const struct HksBlob * keyAlias)75 int32_t HksTestOldKeyExist(const struct HksBlob *keyAlias)
76 {
77     const char *userId = "0";
78     const char *processName = "hks_client";
79     struct HksProcessInfo processInfo = {
80         { strlen(userId), (uint8_t *)userId },
81         { strlen(processName), (uint8_t *)processName },
82         0,
83         0,
84         0
85     };
86     return HksServiceKeyExist(&processInfo, keyAlias, NULL);
87 }
88 
HksTestInitialize(void)89 int32_t HksTestInitialize(void)
90 {
91     int32_t ret = HksCoreModuleInit();
92     if (ret != HKS_SUCCESS) {
93         return ret;
94     }
95     ret = HksServiceInitialize();
96     return ret;
97 }
98 
ChangeDirAndFiles(const char * path,uint32_t uid)99 void ChangeDirAndFiles(const char *path, uint32_t uid)
100 {
101     DIR *dir;
102     struct dirent *ptr;
103     dir = opendir(path);
104     if (dir == NULL) {
105         return;
106     }
107     int ret = EOK;
108     while ((ptr = readdir(dir)) != NULL) {
109         if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) {
110             continue;
111         }
112         char curPath[DEFAULT_PATH_LEN] = { 0 };
113 
114         ret = strcpy_s(curPath, DEFAULT_PATH_LEN, path);
115         if (ret != EOK) {
116             break;
117         }
118         ret = strcat_s(curPath, DEFAULT_PATH_LEN, "/");
119         if (ret != EOK) {
120             break;
121         }
122         ret = strcat_s(curPath, DEFAULT_PATH_LEN, ptr->d_name);
123         if (ret != EOK) {
124             break;
125         }
126 
127         ret = chown(curPath, uid, uid);
128         if (ret != EOK) {
129             break;
130         }
131         if (ptr->d_type == DIR_TYPE) {
132             ChangeDirAndFiles(curPath, uid);
133         }
134     }
135     if (ret != EOK) {
136         printf("chmod dir and file failed! errno = 0x%x \n", errno);
137     }
138     (void)closedir(dir);
139 }
140 
HksChangeOldKeyOwner(const char * path,uint32_t uid)141 void HksChangeOldKeyOwner(const char *path, uint32_t uid)
142 {
143     ChangeDirAndFiles(path, uid);
144 }