• 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 <securec.h>
22 #include <sys/types.h>
23 #include <stdio.h>
24 
25 #include "hks_client_service.h"
26 #include "hks_type_inner.h"
27 #include "hks_storage.h"
28 #include "hks_param.h"
29 #include "hks_log.h"
30 #include "hks_storage_manager.h"
31 #include "hks_core_service_key_attest.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         0
86     };
87     return HksServiceKeyExist(&processInfo, keyAlias, NULL);
88 }
89 
HksTestInitialize(void)90 int32_t HksTestInitialize(void)
91 {
92     int32_t ret = HksCoreModuleInit();
93     if (ret != HKS_SUCCESS) {
94         return ret;
95     }
96     ret = HksServiceInitialize();
97     return ret;
98 }
99 
ChangeDirAndFiles(const char * path,uint32_t uid)100 void ChangeDirAndFiles(const char *path, uint32_t uid)
101 {
102     DIR *dir;
103     struct dirent *ptr;
104     dir = opendir(path);
105     if (dir == NULL) {
106         return;
107     }
108     int ret = EOK;
109     while ((ptr = readdir(dir)) != NULL) {
110         if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) {
111             continue;
112         }
113         char curPath[DEFAULT_PATH_LEN] = { 0 };
114 
115         ret = strcpy_s(curPath, DEFAULT_PATH_LEN, path);
116         if (ret != EOK) {
117             break;
118         }
119         ret = strcat_s(curPath, DEFAULT_PATH_LEN, "/");
120         if (ret != EOK) {
121             break;
122         }
123         ret = strcat_s(curPath, DEFAULT_PATH_LEN, ptr->d_name);
124         if (ret != EOK) {
125             break;
126         }
127 
128         ret = chown(curPath, uid, uid);
129         if (ret != EOK) {
130             break;
131         }
132         if (ptr->d_type == DIR_TYPE) {
133             ChangeDirAndFiles(curPath, uid);
134         }
135     }
136     if (ret != EOK) {
137         printf("chmod dir and file failed! errno = 0x%x \n", errno);
138     }
139     (void)closedir(dir);
140 }
141 
HksChangeOldKeyOwner(const char * path,uint32_t uid)142 void HksChangeOldKeyOwner(const char *path, uint32_t uid)
143 {
144     ChangeDirAndFiles(path, uid);
145 }