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