1 /*
2  * Copyright (c) 2021-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 #ifndef HKS_STORAGE_UTILS_H
17 #define HKS_STORAGE_UTILS_H
18 
19 #include <stdint.h>
20 
21 #include "hks_type_inner.h"
22 #include "hks_file_operator.h"
23 
24 enum HksStorageType {
25     HKS_STORAGE_TYPE_KEY = 0,
26     HKS_STORAGE_TYPE_ROOT_KEY = 1,
27     HKS_STORAGE_TYPE_BAK_KEY = 2,
28 };
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 #define HKS_STORAGE_BAK_FLAG_TRUE     1
35 #define HKS_STORAGE_BAK_FLAG_FLASE    0
36 
37 struct HksStoreInfo {
38     char *path; /* file path include process/key(or certchain) */
39     char *fileName; /* file name that can be recognized by the file system */
40     uint32_t size;
41 };
42 
43 struct HksStoreFileInfo {
44     struct HksStoreInfo mainPath;
45 #ifdef SUPPORT_STORAGE_BACKUP
46     struct HksStoreInfo bakPath;
47 #endif
48 };
49 
50 enum HksPathType {
51     DE_PATH,
52 #ifdef L2_STANDARD
53     CE_PATH,
54     ECE_PATH,
55 
56     #ifdef HUKS_ENABLE_SKIP_UPGRADE_KEY_STORAGE_SECURE_LEVEL
57     TMP_PATH,
58     #endif
59 
60     #ifdef HKS_USE_RKC_IN_STANDARD
61     RKC_IN_STANDARD_PATH,
62     #endif
63 #endif
64 #ifdef HKS_ENABLE_LITE_HAP
65     LITE_HAP_PATH,
66 #endif
67 };
68 
69 struct HksStoreMaterial {
70     enum HksPathType pathType;
71     char *userIdPath;
72     char *uidPath;
73     /**
74      * Storage type, including key and root key materials
75      */
76     char *storageTypePath;
77 
78     char *keyAliasPath;
79 };
80 
81 struct HksFileEntry {
82     char *fileName;
83     uint32_t fileNameLen;
84 };
85 
86 enum KeyOperation {
87     KEY_OPERATION_SAVE = 0,
88     KEY_OPERATION_GET = 1,
89     KEY_OPERATION_DELETE = 2,
90 };
91 
92 int32_t ConstructPlainName(const struct HksBlob *blob, char *targetName, uint32_t nameLen);
93 
94 int32_t ConstructName(const struct HksBlob *blob, char *targetName, uint32_t nameLen);
95 
96 int32_t ConstructBlob(const char *src, struct HksBlob *blob);
97 
98 int32_t GetPath(const char *path, const char *name, char *targetPath, uint32_t pathLen, uint32_t bakFlag);
99 
100 int32_t HksFileInfoInit(struct HksStoreFileInfo *fileInfo);
101 
102 void FileInfoFree(struct HksStoreFileInfo *fileInfo);
103 
104 int32_t RecordKeyOperation(uint32_t operation, const char *path, const char *keyAlias);
105 
106 void FileNameListFree(struct HksFileEntry **fileNameList, uint32_t keyCount);
107 
108 int32_t FileNameListInit(struct HksFileEntry **fileNameList, uint32_t keyCount);
109 
110 int32_t HksMakeFullDir(const char *path);
111 
112 int32_t HksGetFileInfo(const struct HksStoreMaterial *material, struct HksStoreFileInfo *fileInfo);
113 
114 int32_t CheckSpecificUserIdAndStorageLevel(const struct HksProcessInfo *processInfo,
115     const struct HksParamSet *paramSet);
116 
117 #ifdef __cplusplus
118 }
119 #endif
120 
121 #endif /* HUKS_STORAGE_UTILS_H */