1 /*
2  * Copyright (c) 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_type.h"
17 #include "hks_type_inner.h"
18 #ifndef _CUT_AUTHENTICATE_
19 
20 #ifdef HKS_CONFIG_FILE
21 #include HKS_CONFIG_FILE
22 #else
23 #include "hks_config.h"
24 #endif
25 
26 #include "hks_storage_utils.h"
27 
28 #include <stdbool.h>
29 #include <stddef.h>
30 #include <stdint.h>
31 #include <string.h>
32 
33 #include "hks_file_operator.h"
34 #include "hks_log.h"
35 #include "hks_mem.h"
36 #include "hks_template.h"
37 #include "hks_param.h"
38 #include "securec.h"
39 
40 #define HKS_ENCODE_OFFSET_LEN         6
41 #define HKS_ENCODE_KEY_SALT_VALUE     0x3f
42 #define HKS_ROOT_USER_UPPERBOUND      100
43 
44 #ifdef HKS_SUPPORT_POSIX
ConstructInvalidCharacter(const char input,char * output)45 static void ConstructInvalidCharacter(const char input, char *output)
46 {
47     switch (input) {
48         case ':':
49             *output = '#';
50             return;
51         case '<':
52             *output = '$';
53             return;
54         case '>':
55             *output = '%';
56             return;
57         case '?':
58             *output = '&';
59             return;
60         case '\\':
61             *output = '(';
62             return;
63         case '|':
64             *output = ')';
65             return;
66         default:
67             *output = input;
68             return;
69     }
70 }
71 #endif
72 
ResumeInvalidCharacter(const char input,char * output)73 static void ResumeInvalidCharacter(const char input, char *output)
74 {
75     switch (input) {
76         case '#':
77             *output = ':';
78             return;
79         case '$':
80             *output = '<';
81             return;
82         case '%':
83             *output = '>';
84             return;
85         case '&':
86             *output = '?';
87             return;
88         case '(':
89             *output = '\\';
90             return;
91         case ')':
92             *output = '|';
93             return;
94         default:
95             *output = input;
96             return;
97     }
98 }
99 
ConstructPlainName(const struct HksBlob * blob,char * targetName,uint32_t nameLen)100 int32_t ConstructPlainName(const struct HksBlob *blob, char *targetName, uint32_t nameLen)
101 {
102     if (blob->size == sizeof(int)) {
103         int32_t offset = sprintf_s(targetName, nameLen, "%d", *(int *)blob->data);
104         if (offset <= 0) {
105             HKS_LOG_E("get plain name failed");
106             return HKS_ERROR_INSUFFICIENT_MEMORY;
107         }
108 
109         return HKS_SUCCESS;
110     }
111 
112     uint32_t count = 0;
113     if (nameLen <= 1) {
114         return HKS_ERROR_INVALID_ARGUMENT;
115     }
116     for (uint32_t i = 0; i < blob->size; ++i) {
117         if (count >= (nameLen - 1)) { /* nameLen can be guaranteed to be greater than 1 */
118             return HKS_ERROR_INSUFFICIENT_DATA;
119         }
120         targetName[count++] = blob->data[i];
121 
122 #ifdef HKS_SUPPORT_POSIX
123         ConstructInvalidCharacter(targetName[count - 1], &targetName[count - 1]);
124 #endif
125     }
126 
127     return HKS_SUCCESS;
128 }
129 
130 /* Encode invisible content to visible */
ConstructName(const struct HksBlob * blob,char * targetName,uint32_t nameLen)131 int32_t ConstructName(const struct HksBlob *blob, char *targetName, uint32_t nameLen)
132 {
133     uint32_t count = 0;
134     if (nameLen <= 1) {
135         return HKS_ERROR_INVALID_ARGUMENT;
136     }
137 
138     for (uint32_t i = 0; i < blob->size; ++i) {
139         if (count >= (nameLen - 1)) { /* nameLen can be guaranteed to be greater than 1 */
140             return HKS_ERROR_INSUFFICIENT_DATA;
141         }
142 
143         if ((blob->data[i] < '0') || (blob->data[i] > '~')) {
144             targetName[count++] = '+' + (blob->data[i] >> HKS_ENCODE_OFFSET_LEN);
145             targetName[count++] = '0' + (blob->data[i] & HKS_ENCODE_KEY_SALT_VALUE);
146         } else {
147             targetName[count++] = blob->data[i];
148         }
149 
150 #ifdef HKS_SUPPORT_POSIX
151         ConstructInvalidCharacter(targetName[count - 1], &targetName[count - 1]);
152 #endif
153     }
154 
155     return HKS_SUCCESS;
156 }
157 
158 /* Decode encoded content to original content */
ConstructBlob(const char * src,struct HksBlob * blob)159 int32_t ConstructBlob(const char *src, struct HksBlob *blob)
160 {
161     uint32_t size = strlen(src);
162     uint8_t *outputBlob = (uint8_t *)HksMalloc(size);
163     HKS_IF_NULL_LOGE_RETURN(outputBlob, HKS_ERROR_MALLOC_FAIL, "malloc failed")
164 
165     uint32_t count = 0;
166     int32_t ret = HKS_SUCCESS;
167     for (uint32_t i = 0; i < size; ++i) {
168         if ((src[i] >= '+') && (src[i] <= '.')) {
169             uint8_t c = (uint8_t)(src[i] - '+') << HKS_ENCODE_OFFSET_LEN;
170             i++;
171             if (i >= size) {
172                 ret = HKS_ERROR_INVALID_KEY_FILE; /* keyfile name invalid */
173                 break;
174             }
175             char tmp;
176             ResumeInvalidCharacter(src[i], &tmp);
177             c += tmp - '0';
178             outputBlob[count++] = c;
179         } else {
180             char tmp;
181             ResumeInvalidCharacter(src[i], &tmp);
182             outputBlob[count++] = tmp;
183         }
184     }
185 
186     if (ret != HKS_SUCCESS) {
187         HKS_FREE(outputBlob);
188         return ret;
189     }
190 
191     if (blob->size < count) {
192         HKS_FREE(outputBlob);
193         return HKS_ERROR_BUFFER_TOO_SMALL;
194     }
195 
196     (void)memcpy_s(blob->data, blob->size, outputBlob, count);
197 
198     blob->size = count;
199     HKS_FREE(outputBlob);
200     return ret;
201 }
202 
GetPath(const char * path,const char * name,char * targetPath,uint32_t pathLen,uint32_t bakFlag)203 int32_t GetPath(const char *path, const char *name, char *targetPath, uint32_t pathLen, uint32_t bakFlag)
204 {
205     if (strncpy_s(targetPath, pathLen, path, strlen(path)) != EOK) {
206         HKS_LOG_E("strncpy path failed");
207         return HKS_ERROR_BAD_STATE;
208     }
209 
210     if (strlen(targetPath) <= 0) {
211         return HKS_ERROR_INTERNAL_ERROR;
212     }
213 
214     if (targetPath[strlen(targetPath) - 1] != '/') {
215         if (strncat_s(targetPath, pathLen, "/", strlen("/")) != EOK) {
216             HKS_LOG_E("strncat slash failed");
217             return HKS_ERROR_INTERNAL_ERROR;
218         }
219     }
220 
221     if (strncat_s(targetPath, pathLen, name, strlen(name)) != EOK) {
222         HKS_LOG_E("strncat Name failed");
223         return HKS_ERROR_BAD_STATE;
224     }
225 
226     if (bakFlag == HKS_STORAGE_BAK_FLAG_TRUE) {
227         if (strncat_s(targetPath, pathLen, ".bak", strlen(".bak")) != EOK) {
228             HKS_LOG_E("strncat bak failed");
229             return HKS_ERROR_BAD_STATE;
230         }
231     }
232 
233     return HKS_SUCCESS;
234 }
235 
DataInit(char ** data,uint32_t size)236 static int32_t DataInit(char **data, uint32_t size)
237 {
238     *data = (char *)HksMalloc(size);
239     HKS_IF_NULL_RETURN(*data, HKS_ERROR_MALLOC_FAIL)
240 
241     (void)memset_s(*data, size, 0, size);
242     return HKS_SUCCESS;
243 }
244 
FileInfoInit(struct HksStoreFileInfo * fileInfo)245 static int32_t FileInfoInit(struct HksStoreFileInfo *fileInfo)
246 {
247     fileInfo->mainPath.size = HKS_MAX_FILE_NAME_LEN;
248     /* if one param init fail, others free by caller function */
249     int32_t ret = DataInit(&fileInfo->mainPath.path, fileInfo->mainPath.size);
250     ret += DataInit(&fileInfo->mainPath.fileName, fileInfo->mainPath.size);
251 
252 #ifdef SUPPORT_STORAGE_BACKUP
253     fileInfo->bakPath.size = HKS_MAX_FILE_NAME_LEN;
254     ret += DataInit(&fileInfo->bakPath.path, fileInfo->bakPath.size);
255     ret += DataInit(&fileInfo->bakPath.fileName, fileInfo->bakPath.size);
256 #endif
257 
258     return ret;
259 }
260 
FileInfoFree(struct HksStoreFileInfo * fileInfo)261 void FileInfoFree(struct HksStoreFileInfo *fileInfo)
262 {
263     HKS_FREE(fileInfo->mainPath.path);
264     HKS_FREE(fileInfo->mainPath.fileName);
265     fileInfo->mainPath.size = 0;
266 
267 #ifdef SUPPORT_STORAGE_BACKUP
268     HKS_FREE(fileInfo->bakPath.path);
269     HKS_FREE(fileInfo->bakPath.fileName);
270     fileInfo->bakPath.size = 0;
271 #endif
272 }
273 
274 /*
275  * keyAlias: xxxxxxxxxxxxxxxxxxx********************xxxxxxxxxxxxxxxxxx
276  *                              |<- anonymous len ->||<- suffix len ->|
277  *           |<----------------- keyAlias len ----------------------->|
278  */
RecordKeyOperation(uint32_t operation,const char * path,const char * keyAlias)279 int32_t RecordKeyOperation(uint32_t operation, const char *path, const char *keyAlias)
280 {
281     (void)path;
282     uint32_t bufSize = strlen(keyAlias) + 1;
283     char *outKeyAlias = (char *)HksMalloc(bufSize);
284     HKS_IF_NULL_RETURN(outKeyAlias, HKS_ERROR_MALLOC_FAIL)
285 
286     (void)memset_s(outKeyAlias, bufSize, 0, bufSize);
287 
288     uint32_t keyAliasLen = strlen(keyAlias);
289     uint32_t anoyLen = (keyAliasLen + 1) / 2;
290     uint32_t suffixLen = anoyLen / 2;
291     outKeyAlias[0] = keyAlias[0]; // keyAliasLen > 0;
292     for (uint32_t i = 1; i < keyAliasLen; ++i) {
293         if ((keyAliasLen < (i + 1 + anoyLen + suffixLen)) &&
294             ((i + 1 + suffixLen) <= keyAliasLen)) {
295             outKeyAlias[i] = '*';
296         } else {
297             outKeyAlias[i] = keyAlias[i];
298         }
299     }
300     outKeyAlias[keyAliasLen] = '\0';
301 
302     int32_t ret = HKS_SUCCESS;
303     switch (operation) {
304         case KEY_OPERATION_SAVE:
305             HKS_LOG_I("generate key, storage path: %" LOG_PUBLIC "s, key alias: %" LOG_PUBLIC "s",
306                 path, outKeyAlias);
307             break;
308         case KEY_OPERATION_GET:
309             HKS_LOG_I("use key, storage path: %" LOG_PUBLIC "s, key alias: %" LOG_PUBLIC "s", path, outKeyAlias);
310             break;
311         case KEY_OPERATION_DELETE:
312             HKS_LOG_I("delete key, storage path: %" LOG_PUBLIC "s, key alias: %" LOG_PUBLIC "s",
313                 path, outKeyAlias);
314             break;
315         default:
316             ret = HKS_ERROR_INVALID_ARGUMENT;
317     }
318 
319     HKS_FREE(outKeyAlias);
320     return ret;
321 }
322 
FileNameListFree(struct HksFileEntry ** fileNameList,uint32_t keyCount)323 void FileNameListFree(struct HksFileEntry **fileNameList, uint32_t keyCount)
324 {
325     if (fileNameList != NULL && *fileNameList != NULL) {
326         for (uint32_t i = 0; i < keyCount; ++i) {
327             HKS_FREE((*fileNameList)[i].fileName);
328         }
329         HKS_FREE(*fileNameList);
330     }
331 }
332 
FileNameListInit(struct HksFileEntry ** fileNameList,uint32_t keyCount)333 int32_t FileNameListInit(struct HksFileEntry **fileNameList, uint32_t keyCount)
334 {
335     if (((keyCount > UINT32_MAX / (sizeof(struct HksFileEntry)))) || (keyCount == 0)) {
336         HKS_LOG_E("keyCount too big or is zero.");
337         return HKS_ERROR_BUFFER_TOO_SMALL;
338     }
339 
340     uint32_t totalSize = keyCount * sizeof(struct HksFileEntry);
341     *fileNameList = (struct HksFileEntry *)HksMalloc(totalSize);
342     HKS_IF_NULL_LOGE_RETURN(*fileNameList, HKS_ERROR_MALLOC_FAIL, "malloc file name list failed.")
343 
344     for (uint32_t i = 0; i < keyCount; ++i) {
345         (*fileNameList)[i].fileNameLen = HKS_MAX_FILE_NAME_LEN;
346         (*fileNameList)[i].fileName = (char *)HksMalloc(HKS_MAX_FILE_NAME_LEN);
347         if ((*fileNameList)[i].fileName == NULL) {
348             HKS_LOG_E("malloc failed.");
349             FileNameListFree(fileNameList, keyCount);
350             return HKS_ERROR_MALLOC_FAIL;
351         }
352     }
353 
354     return HKS_SUCCESS;
355 }
356 
357 #ifdef L2_STANDARD
CheckIfBothTagExist(const struct HksParam * storageLevel,const struct HksParam * specificUserId)358 static int32_t CheckIfBothTagExist(const struct HksParam *storageLevel,
359     const struct HksParam *specificUserId)
360 {
361     if (storageLevel->uint32Param == HKS_AUTH_STORAGE_LEVEL_DE) {
362         if (specificUserId->int32Param < 0) {
363             HKS_LOG_E("invalid specificUserId, specificUserId is %" LOG_PUBLIC "d.", specificUserId->int32Param);
364             return HKS_ERROR_INVALID_ARGUMENT;
365         }
366     } else {
367         if (specificUserId->int32Param < HKS_ROOT_USER_UPPERBOUND) {
368             HKS_LOG_E("invalid specificUserId when tag storage level is CE or ECE, specificUserId is %" LOG_PUBLIC "d",
369                 specificUserId->int32Param);
370             return HKS_ERROR_INVALID_ARGUMENT;
371         }
372     }
373     return HKS_SUCCESS;
374 }
375 
CheckIfOnlyStorageLevelTagExist(const struct HksProcessInfo * processInfo,const struct HksParam * storageLevel)376 static int32_t CheckIfOnlyStorageLevelTagExist(const struct HksProcessInfo *processInfo,
377     const struct HksParam *storageLevel)
378 {
379     if (storageLevel->uint32Param != HKS_AUTH_STORAGE_LEVEL_DE &&
380 #ifdef HUKS_ENABLE_SKIP_UPGRADE_KEY_STORAGE_SECURE_LEVEL
381         storageLevel->uint32Param != HKS_AUTH_STORAGE_LEVEL_OLD_DE_TMP &&
382 #endif
383         processInfo->userIdInt < HKS_ROOT_USER_UPPERBOUND) {
384         HKS_LOG_E("invalid userId when tag storage level is CE or ECE, userId is %" LOG_PUBLIC "d",
385             processInfo->userIdInt);
386         return HKS_ERROR_INVALID_ARGUMENT;
387     }
388 
389     return HKS_SUCCESS;
390 }
391 #endif
392 
CheckSpecificUserIdAndStorageLevel(const struct HksProcessInfo * processInfo,const struct HksParamSet * paramSet)393 int32_t CheckSpecificUserIdAndStorageLevel(const struct HksProcessInfo *processInfo,
394     const struct HksParamSet *paramSet)
395 {
396 #ifdef L2_STANDARD
397     if (paramSet == NULL) {
398         return HKS_ERROR_INVALID_ARGUMENT;
399     }
400     int32_t ret = HKS_SUCCESS;
401     struct HksParam *storageLevel = NULL;
402     struct HksParam *specificUserId = NULL;
403 
404     bool storageLevelExist = HksGetParam(paramSet, HKS_TAG_AUTH_STORAGE_LEVEL, &storageLevel) == HKS_SUCCESS;
405     bool specificUserIdExist = HksGetParam(paramSet, HKS_TAG_SPECIFIC_USER_ID, &specificUserId) == HKS_SUCCESS;
406     if (storageLevelExist && specificUserIdExist) {
407         ret = CheckIfBothTagExist(storageLevel, specificUserId);
408     } else if (storageLevelExist && !specificUserIdExist) {
409         ret = CheckIfOnlyStorageLevelTagExist(processInfo, storageLevel);
410     }
411     HKS_IF_NOT_SUCC_RETURN(ret, ret)
412 #endif
413     (void)processInfo;
414     (void)paramSet;
415     return HKS_SUCCESS;
416 }
417 
HksMakeFullDir(const char * path)418 int32_t HksMakeFullDir(const char *path)
419 {
420     uint32_t pathLen = strlen(path);
421     char *curPath = (char *)HksMalloc(pathLen + 1);
422     if (curPath == NULL) {
423         HKS_LOG_E("cur path malloc failed.");
424         return HKS_ERROR_MALLOC_FAIL;
425     }
426     int32_t ret = HKS_SUCCESS;
427     do {
428         for (uint32_t i = 1; i < pathLen; ++i) {
429             if (path[i] == '/') {
430                 (void)memcpy_s(curPath, pathLen, path, i);
431                 curPath[i] = '\0';
432                 if (HksIsDirExist(curPath) == HKS_SUCCESS) {
433                     continue;
434                 }
435                 ret = HksMakeDir(curPath);
436                 if (ret != HKS_SUCCESS && ret != HKS_ERROR_ALREADY_EXISTS) {
437                     HKS_LOG_E("mkdir %" LOG_PUBLIC "s failed.", curPath);
438                     break;
439                 }
440                 ret = HKS_SUCCESS;
441             }
442         }
443         if (ret != HKS_SUCCESS) {
444             break;
445         }
446         ret = HksMakeDir(path);
447         if (ret == HKS_ERROR_ALREADY_EXISTS) {
448             ret = HKS_SUCCESS;
449         }
450     } while (false);
451 
452     HKS_FREE(curPath);
453     return ret;
454 }
455 
456 // remove duplicated '/'
StandardizePath(char * path)457 static void StandardizePath(char *path)
458 {
459     // the size must be more than 0
460     uint32_t size = strlen(path);
461     uint32_t index = 1;
462     for (uint32_t i = 1; i < size; ++i) {
463         if (path[i] != '/' || path[i - 1] != '/') {
464             path[index] = path[i];
465             ++index;
466         }
467     }
468     // the index will be less than or equal to the size
469     path[index] = '\0';
470 }
471 
472 #ifdef L2_STANDARD
473 // Only ce and ece will access this check function.
CheckUserPathExist(enum HksPathType pathType,const char * userIdPath)474 static int32_t CheckUserPathExist(enum HksPathType pathType, const char *userIdPath)
475 {
476     char userPath[HKS_MAX_DIRENT_FILE_LEN] = { 0 };
477     int32_t offset = sprintf_s(userPath, HKS_MAX_DIRENT_FILE_LEN, "%s/%s",
478         pathType == CE_PATH ? HKS_CE_ROOT_PATH : HKS_ECE_ROOT_PATH, userIdPath);
479     if (offset <= 0) {
480         HKS_LOG_E("get user path failed, path type is %" LOG_PUBLIC "d.", pathType);
481         return HKS_ERROR_INSUFFICIENT_MEMORY;
482     }
483 
484     return HksIsDirExist(userPath) == HKS_SUCCESS ? HKS_SUCCESS : HKS_ERROR_NO_PERMISSION;
485 }
486 #endif
487 
ConstructPath(const struct HksStoreMaterial * material,const char * deDataPath,const char * ceOrEceDataPath,struct HksStoreInfo * fileInfoPath)488 static int32_t ConstructPath(const struct HksStoreMaterial *material, const char *deDataPath,
489     const char *ceOrEceDataPath, struct HksStoreInfo *fileInfoPath)
490 {
491     (void)ceOrEceDataPath;
492     int32_t ret = HKS_SUCCESS;
493     int32_t offset = 0;
494     switch (material->pathType) {
495         case DE_PATH:
496             offset = sprintf_s(fileInfoPath->path, HKS_MAX_DIRENT_FILE_LEN, "%s/%s/%s/%s",
497                 deDataPath, material->userIdPath, material->uidPath, material->storageTypePath);
498             break;
499 #ifdef L2_STANDARD
500         case CE_PATH:
501             ret = CheckUserPathExist(CE_PATH, material->userIdPath);
502             HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "check user path exist failed.")
503             offset = sprintf_s(fileInfoPath->path, HKS_MAX_DIRENT_FILE_LEN, "%s/%s/%s/%s/%s", HKS_CE_ROOT_PATH,
504                 material->userIdPath, ceOrEceDataPath, material->uidPath, material->storageTypePath);
505             break;
506         case ECE_PATH:
507             ret = CheckUserPathExist(ECE_PATH, material->userIdPath);
508             HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "check user path exist failed.")
509             offset = sprintf_s(fileInfoPath->path, HKS_MAX_DIRENT_FILE_LEN, "%s/%s/%s/%s/%s", HKS_ECE_ROOT_PATH,
510                 material->userIdPath, ceOrEceDataPath, material->uidPath, material->storageTypePath);
511             break;
512 #ifdef HUKS_ENABLE_SKIP_UPGRADE_KEY_STORAGE_SECURE_LEVEL
513         case TMP_PATH:
514             offset = sprintf_s(fileInfoPath->path, HKS_MAX_DIRENT_FILE_LEN, "%s/%s/%s/%s",
515                 HKS_KEY_STORE_TMP_PATH, material->userIdPath, material->uidPath, material->storageTypePath);
516             break;
517 #endif
518 #ifdef HKS_USE_RKC_IN_STANDARD
519         case RKC_IN_STANDARD_PATH:
520             // there is no user id path in rkc of standard
521             offset = sprintf_s(fileInfoPath->path, HKS_MAX_DIRENT_FILE_LEN, "%s/%s/%s",
522                 HKS_KEY_RKC_PATH, material->uidPath, material->storageTypePath);
523             break;
524 #endif
525 #endif
526 #ifdef HKS_ENABLE_LITE_HAP
527         case LITE_HAP_PATH:
528             offset = sprintf_s(fileInfoPath->path, HKS_MAX_DIRENT_FILE_LEN, "%s/%s/%s/%s",
529                 HKS_KEY_STORE_LITE_HAP, material->userIdPath,  material->uidPath, material->storageTypePath);
530             break;
531 #endif
532     }
533     if (offset <= 0) {
534         HKS_LOG_E("get path failed, path type is %" LOG_PUBLIC "d.", material->pathType);
535         return HKS_ERROR_INSUFFICIENT_MEMORY;
536     }
537     return ret;
538 }
539 
GetPathInfo(const struct HksStoreMaterial * material,const char * deDataPath,const char * ceOrEceDataPath,struct HksStoreInfo * fileInfoPath)540 static int32_t GetPathInfo(const struct HksStoreMaterial *material, const char *deDataPath,
541     const char *ceOrEceDataPath, struct HksStoreInfo *fileInfoPath)
542 {
543     int32_t ret = HKS_SUCCESS;
544 #ifdef L2_STANDARD
545     bool isUserPath = material->pathType == CE_PATH || material->pathType == ECE_PATH;
546 #else
547     bool isUserPath = false;
548 #endif
549 
550     ret = ConstructPath(material, deDataPath, ceOrEceDataPath, fileInfoPath);
551     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "construct main path failed")
552 
553     StandardizePath(fileInfoPath->path);
554     ret = HksMakeFullDir(fileInfoPath->path);
555     if (isUserPath) {
556         HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_NO_PERMISSION, "make full dir failed.")
557     } else {
558         HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "make full dir failed.")
559     }
560     if (material->keyAliasPath != NULL) {
561         if (strstr(material->keyAliasPath, "../") != NULL) {
562             HKS_LOG_E("invalid filePath, ../ is included in file path");
563             return HKS_ERROR_INVALID_ARGUMENT;
564         }
565         if (memcpy_s(fileInfoPath->fileName, HKS_MAX_FILE_NAME_LEN,
566             material->keyAliasPath, strlen(material->keyAliasPath)) != EOK) {
567             ret = HKS_ERROR_INSUFFICIENT_MEMORY;
568         }
569     }
570     return ret;
571 }
572 
HksGetFileInfo(const struct HksStoreMaterial * material,struct HksStoreFileInfo * fileInfo)573 int32_t HksGetFileInfo(const struct HksStoreMaterial *material, struct HksStoreFileInfo *fileInfo)
574 {
575     int32_t ret = FileInfoInit(fileInfo);
576     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "hks file info init failed, ret = %" LOG_PUBLIC "d.", ret)
577 
578 #ifdef L2_STANDARD
579     ret = GetPathInfo(material, HKS_KEY_STORE_PATH, HKS_STORE_SERVICE_PATH, &fileInfo->mainPath);
580     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "hks get main path info failed, ret = %" LOG_PUBLIC "d.", ret)
581 #else
582     ret = GetPathInfo(material, HKS_KEY_STORE_PATH, NULL, &fileInfo->mainPath);
583     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "hks get main path info failed, ret = %" LOG_PUBLIC "d.", ret)
584 #endif
585 
586 #ifdef SUPPORT_STORAGE_BACKUP
587     ret = GetPathInfo(material, HKS_KEY_STORE_BAK_PATH, HKS_STORE_SERVICE_BAK_PATH, &fileInfo->bakPath);
588     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "hks get bak path info failed, ret = %" LOG_PUBLIC "d.", ret)
589 #endif
590     return HKS_SUCCESS;
591 }
592 #endif /* _CUT_AUTHENTICATE_ */
593