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