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