• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #ifdef HKS_CONFIG_FILE
17 #include HKS_CONFIG_FILE
18 #else
19 #include "hks_config.h"
20 #endif
21 
22 #include "hks_file_operator.h"
23 
24 #ifndef _STORAGE_LITE_
25 #include <dirent.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 
31 /* use product definitions temporarily */
32 #define DEFAULT_FILE_PERMISSION 0700
33 #else
34 
35 #include <utils_file.h>
36 
37 #endif /* _STORAGE_LITE_ */
38 
39 #include "securec.h"
40 
41 #include "hks_log.h"
42 #include "hks_mem.h"
43 #include "hks_template.h"
44 
45 #ifdef HUKS_LOG_MINI_EXT_ENABLED
46 #include "log.h"
47 #endif
48 
49 
50 #ifndef _CUT_AUTHENTICATE_
GetFileName(const char * path,const char * fileName,char * fullFileName,uint32_t fullFileNameLen)51 static int32_t GetFileName(const char *path, const char *fileName, char *fullFileName, uint32_t fullFileNameLen)
52 {
53     if (path != NULL) {
54         if (strncpy_s(fullFileName, fullFileNameLen, path, strlen(path)) != EOK) {
55             return HKS_ERROR_INTERNAL_ERROR;
56         }
57 
58         if (path[strlen(path) - 1] != '/') {
59             if (strncat_s(fullFileName, fullFileNameLen, "/", strlen("/")) != EOK) {
60                 return HKS_ERROR_INTERNAL_ERROR;
61             }
62         }
63 
64         if (strncat_s(fullFileName, fullFileNameLen, fileName, strlen(fileName)) != EOK) {
65             return HKS_ERROR_INTERNAL_ERROR;
66         }
67     } else {
68         if (strncpy_s(fullFileName, fullFileNameLen, fileName, strlen(fileName)) != EOK) {
69             return HKS_ERROR_INTERNAL_ERROR;
70         }
71     }
72 
73     return HKS_SUCCESS;
74 }
75 
GetFullFileName(const char * path,const char * fileName,char ** fullFileName)76 static int32_t GetFullFileName(const char *path, const char *fileName, char **fullFileName)
77 {
78     uint32_t nameLen = HKS_MAX_FILE_NAME_LEN;
79     char *tmpFileName = (char *)HksMalloc(nameLen);
80     HKS_IF_NULL_RETURN(tmpFileName, HKS_ERROR_MALLOC_FAIL)
81     (void)memset_s(tmpFileName, nameLen, 0, nameLen);
82 
83     int32_t ret = GetFileName(path, fileName, tmpFileName, nameLen);
84     if (ret != HKS_SUCCESS) {
85         HKS_LOG_E("get full fileName failed");
86         HKS_FREE_PTR(tmpFileName);
87         return ret;
88     }
89 
90     *fullFileName = tmpFileName;
91     return HKS_SUCCESS;
92 }
93 
94 #ifndef _STORAGE_LITE_
IsFileExist(const char * fileName)95 static int32_t IsFileExist(const char *fileName)
96 {
97     struct stat fileStat;
98     int32_t ret = stat(fileName, &fileStat);
99     if (ret == -1) {
100         if (errno == ENOENT) {
101             return HKS_ERROR_NOT_EXIST;
102         } else {
103             HKS_LOG_E("file stat failed, errno = 0x%" LOG_PUBLIC "x", errno);
104             return HKS_ERROR_OPEN_FILE_FAIL;
105         }
106     }
107 
108     return HKS_SUCCESS;
109 }
110 
FileRead(const char * fileName,uint32_t offset,uint8_t * buf,uint32_t len)111 static uint32_t FileRead(const char *fileName, uint32_t offset, uint8_t *buf, uint32_t len)
112 {
113     (void)offset;
114     int32_t fd = open(fileName, O_RDONLY);
115     if (fd < 0) {
116         HKS_LOG_E("failed to open file, errno = 0x%" LOG_PUBLIC "x", errno);
117 #ifdef HUKS_LOG_MINI_EXT_ENABLED
118         HILOG_ERROR(HILOG_MODULE_SCY, "failed to open file, errno = 0x%{public}X", errno);
119 #endif
120         return 0;
121     }
122 
123     int32_t size = read(fd, buf, len);
124     close(fd);
125     if (size < 0) {
126         HKS_LOG_E("failed to read file, errno = 0x%" LOG_PUBLIC "x", errno);
127 #ifdef HUKS_LOG_MINI_EXT_ENABLED
128         HILOG_ERROR(HILOG_MODULE_SCY, "failed to read file, errno = 0x%{public}X", errno);
129 #endif
130         return 0;
131     }
132 
133     return (uint32_t)size;
134 }
135 
FileSize(const char * fileName)136 static uint32_t FileSize(const char *fileName)
137 {
138     struct stat fileStat;
139     (void)memset_s(&fileStat, sizeof(fileStat), 0, sizeof(fileStat));
140     if (stat(fileName, &fileStat) != 0) {
141         HKS_LOG_E("file stat failed, errno = 0x%" LOG_PUBLIC "x", errno);
142         return 0;
143     }
144 
145     return (uint32_t)fileStat.st_size;
146 }
147 
FileWrite(const char * fileName,uint32_t offset,const uint8_t * buf,uint32_t len)148 static int32_t FileWrite(const char *fileName, uint32_t offset, const uint8_t *buf, uint32_t len)
149 {
150     (void)offset;
151     int32_t fd = open(fileName, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
152     if (fd < 0) {
153         HKS_LOG_E("open file failed, errno = 0x%" LOG_PUBLIC "x", errno);
154 #ifdef HUKS_LOG_MINI_EXT_ENABLED
155         HILOG_ERROR(HILOG_MODULE_SCY, "open file failed, errno = 0x%{public}X", errno);
156 #endif
157         return HKS_ERROR_OPEN_FILE_FAIL;
158     }
159 
160     int32_t size = write(fd, buf, len);
161     if (size < 0) {
162         HKS_LOG_E("write file size failed, errno = 0x%" LOG_PUBLIC "x", errno);
163 #ifdef HUKS_LOG_MINI_EXT_ENABLED
164         HILOG_ERROR(HILOG_MODULE_SCY, "write file size failed, errno = 0x%{public}X", errno);
165 #endif
166         close(fd);
167         return HKS_ERROR_WRITE_FILE_FAIL;
168     }
169 
170     fsync(fd);
171     close(fd);
172     return HKS_SUCCESS;
173 }
174 
FileRemove(const char * fileName)175 static int32_t FileRemove(const char *fileName)
176 {
177     struct stat fileStat;
178     int32_t ret = stat(fileName, &fileStat);
179     if (ret == -1) {
180         if (errno == ENOENT) {
181             return HKS_SUCCESS; /* if file not exist, return ok */
182         } else {
183             HKS_LOG_E("file stat failed, errno = 0x%" LOG_PUBLIC "x", errno);
184             return HKS_ERROR_OPEN_FILE_FAIL;
185         }
186     }
187 
188     if (S_ISDIR(fileStat.st_mode)) {
189         return HKS_ERROR_INVALID_ARGUMENT; /* FileRemove func only care about files. */
190     }
191 
192     if (unlink(fileName) != 0) {
193         HKS_LOG_E("remove file failed, errno = 0x%" LOG_PUBLIC "x", errno);
194         return HKS_ERROR_REMOVE_FILE_FAIL;
195     }
196 
197     return HKS_SUCCESS;
198 }
199 
HksIsDirExist(const char * path)200 int32_t HksIsDirExist(const char *path)
201 {
202     HKS_IF_NULL_RETURN(path, HKS_ERROR_NULL_POINTER)
203     return IsFileExist(path);
204 }
205 
HksMakeDir(const char * path)206 int32_t HksMakeDir(const char *path)
207 {
208     int result = mkdir(path, DEFAULT_FILE_PERMISSION);
209     if (result == 0) {
210         return HKS_SUCCESS;
211     } else {
212         switch (errno) {
213             case EEXIST:
214                 return HKS_ERROR_ALREADY_EXISTS;
215             default:
216                 HKS_LOG_E("mkdir failed, errno = 0x%" LOG_PUBLIC "x", errno);
217 #ifdef HUKS_LOG_MINI_EXT_ENABLED
218                 HILOG_ERROR(HILOG_MODULE_SCY, "mkdir failed, errno = 0x%{public}X", errno);
219 #endif
220                 return HKS_ERROR_MAKE_DIR_FAIL;
221         }
222     }
223 }
224 
HksOpenDir(const char * path)225 void *HksOpenDir(const char *path)
226 {
227     return (void *)opendir(path);
228 }
229 
HksCloseDir(void * dirp)230 int32_t HksCloseDir(void *dirp)
231 {
232     return closedir((DIR *)dirp);
233 }
234 
HksGetDirFile(void * dirp,struct HksFileDirentInfo * direntInfo)235 int32_t HksGetDirFile(void *dirp, struct HksFileDirentInfo *direntInfo)
236 {
237     DIR *dir = (DIR *)dirp;
238     struct dirent *dire = readdir(dir);
239 
240     while (dire != NULL) {
241         if (dire->d_type != DT_REG) { /* only care about files. */
242             dire = readdir(dir);
243             continue;
244         }
245 
246         uint32_t len = strlen(dire->d_name);
247         if (memcpy_s(direntInfo->fileName, sizeof(direntInfo->fileName) - 1, dire->d_name, len) != EOK) {
248             return HKS_ERROR_INSUFFICIENT_MEMORY;
249         }
250         direntInfo->fileName[len] = '\0';
251         return HKS_SUCCESS;
252     }
253 
254     return HKS_ERROR_NOT_EXIST;
255 }
256 
HksGetStoragePath(enum HksStoragePathType pathType,char * path,uint32_t * len)257 int32_t HksGetStoragePath(enum HksStoragePathType pathType, char *path, uint32_t *len)
258 {
259     if ((path == NULL) || (len == NULL) || (*len <= 1)) {
260         return HKS_ERROR_INVALID_ARGUMENT;
261     }
262     errno_t ret;
263     uint32_t pathLen;
264     if (pathType == HKS_STORAGE_MAIN_PATH) {
265         pathLen = strlen(HKS_KEY_STORE_PATH);
266         ret = memcpy_s(path, *len - 1, HKS_KEY_STORE_PATH, pathLen);
267     } else if (pathType == HKS_STORAGE_BACKUP_PATH) {
268         pathLen = strlen(HKS_KEY_STORE_BAK_PATH);
269         ret = memcpy_s(path, *len - 1, HKS_KEY_STORE_BAK_PATH, pathLen);
270 #ifdef HKS_ENABLE_LITE_HAP
271     } else if (pathType == HKS_STORAGE_LITE_HAP_PATH) {
272         pathLen = strlen(HKS_KEY_STORE_LITE_HAP);
273         ret = memcpy_s(path, *len - 1, HKS_KEY_STORE_LITE_HAP, pathLen);
274 #endif
275     } else {
276         return HKS_ERROR_INVALID_ARGUMENT;
277     }
278     if (ret != EOK) {
279         HKS_LOG_E("memcpy failed");
280         return HKS_ERROR_INSUFFICIENT_MEMORY;
281     }
282     path[pathLen] = '\0';
283     *len = pathLen + 1;
284     return HKS_SUCCESS;
285 }
286 
HksRemoveDir(const char * dirPath)287 int32_t HksRemoveDir(const char *dirPath)
288 {
289     struct stat fileStat;
290     int32_t ret = stat(dirPath, &fileStat);
291     if (ret != 0) {
292         HKS_LOG_E("file stat failed");
293         return HKS_FAILURE;
294     }
295 
296     if (!S_ISDIR(fileStat.st_mode)) {
297         HKS_LOG_E("path is not dir");
298         return HKS_FAILURE;
299     }
300 
301     DIR *dir = opendir(dirPath);
302     HKS_IF_NULL_LOGE_RETURN(dir, HKS_ERROR_OPEN_FILE_FAIL, "open dir failed")
303 
304     struct dirent *dire = readdir(dir);
305     while (dire != NULL) {
306         if (dire->d_type == DT_REG) { /* only care about files. */
307             ret = HksFileRemove(dirPath, dire->d_name);
308             /* Continue to delete remaining files */
309             HKS_IF_NOT_SUCC_LOGE(ret, "remove file failed when remove dir files, ret = %" LOG_PUBLIC "d.", ret)
310         }
311         dire = readdir(dir);
312     }
313 
314     closedir(dir);
315     return HKS_SUCCESS;
316 }
317 #else
IsFileExist(const char * fileName)318 static int32_t IsFileExist(const char *fileName)
319 {
320     unsigned int fileSize;
321     int32_t ret = UtilsFileStat(fileName, &fileSize);
322     if (ret < 0) {
323         return HKS_ERROR_NOT_EXIST;
324     }
325     return HKS_SUCCESS;
326 }
327 
FileRead(const char * fileName,uint32_t offset,uint8_t * buf,uint32_t len)328 static uint32_t FileRead(const char *fileName, uint32_t offset, uint8_t *buf, uint32_t len)
329 {
330     /* now offset is 0, but we maybe extend hi1131 file interfaces in the future */
331     if (offset != 0) {
332         return 0;
333     }
334 
335     unsigned int fileSize;
336     int32_t ret = UtilsFileStat(fileName, &fileSize);
337     if (ret < 0) {
338         HKS_LOG_E("stat file failed, errno = 0x%" LOG_PUBLIC "x", ret);
339         return 0;
340     }
341 
342     if (len > fileSize) {
343         HKS_LOG_E("read data over file size!\n, file size = %" LOG_PUBLIC "d\n, buf len = %" LOG_PUBLIC "d\n",
344             fileSize, len);
345         return 0;
346     }
347 
348     int fd = UtilsFileOpen(fileName, O_RDONLY_FS, 0);
349     if (fd < 0) {
350         HKS_LOG_E("failed to open file, errno = 0x%" LOG_PUBLIC "x", fd);
351         return 0;
352     }
353 
354     ret = UtilsFileRead(fd, (char *)buf, len);
355     UtilsFileClose(fd);
356     if (ret < 0) {
357         HKS_LOG_E("failed to read file, errno = 0x%" LOG_PUBLIC "x", ret);
358         return 0;
359     }
360 
361     return len;
362 }
363 
FileWrite(const char * fileName,uint32_t offset,const uint8_t * buf,uint32_t len)364 static int32_t FileWrite(const char *fileName, uint32_t offset, const uint8_t *buf, uint32_t len)
365 {
366     /* now offset is 0, but we may extend hi1131 file interfaces */
367     if (offset != 0) {
368         return HKS_ERROR_INVALID_ARGUMENT;
369     }
370 
371     int fd = UtilsFileOpen(fileName, O_CREAT_FS | O_TRUNC_FS | O_RDWR_FS, 0);
372     if (fd < 0) {
373         HKS_LOG_E("failed to open key file, errno = 0x%" LOG_PUBLIC "x\n", fd);
374         return HKS_ERROR_OPEN_FILE_FAIL;
375     }
376 
377     int32_t ret = UtilsFileWrite(fd, (const char*)buf, len);
378     (void)UtilsFileClose(fd);
379     if (ret < 0) {
380         HKS_LOG_E("failed to write key file, errno = 0x%" LOG_PUBLIC "x\n", ret);
381         return HKS_ERROR_WRITE_FILE_FAIL;
382     }
383 
384     return HKS_SUCCESS;
385 }
386 
FileSize(const char * fileName)387 static uint32_t FileSize(const char *fileName)
388 {
389     unsigned int fileSize;
390     int32_t ret = UtilsFileStat(fileName, &fileSize);
391     if (ret < 0) {
392         HKS_LOG_E("stat file failed, errno = 0x%" LOG_PUBLIC "x", ret);
393         return 0;
394     }
395     return fileSize;
396 }
397 
FileRemove(const char * fileName)398 static int32_t FileRemove(const char *fileName)
399 {
400     int32_t ret = UtilsFileDelete(fileName);
401     if (ret < 0) {
402         HKS_LOG_E("remove file failed");
403         return HKS_ERROR_REMOVE_FILE_FAIL;
404     }
405     return HKS_SUCCESS;
406 }
407 
408 #endif
409 
HksFileRead(const char * path,const char * fileName,uint32_t offset,uint8_t * buf,uint32_t len)410 uint32_t HksFileRead(const char *path, const char *fileName, uint32_t offset, uint8_t *buf, uint32_t len)
411 {
412     if ((fileName == NULL) || (buf == NULL) || (len == 0)) {
413         return 0;
414     }
415 
416     char *fullFileName = NULL;
417     int32_t ret = GetFullFileName(path, fileName, &fullFileName);
418     HKS_IF_NOT_SUCC_RETURN(ret, 0)
419 
420     uint32_t size = FileRead(fullFileName, offset, buf, len);
421     HKS_FREE_PTR(fullFileName);
422     return size;
423 }
424 
HksFileWrite(const char * path,const char * fileName,uint32_t offset,const uint8_t * buf,uint32_t len)425 int32_t HksFileWrite(const char *path, const char *fileName, uint32_t offset, const uint8_t *buf, uint32_t len)
426 {
427     if ((fileName == NULL) || (buf == NULL) || (len == 0)) {
428         return HKS_ERROR_INVALID_ARGUMENT;
429     }
430 
431     char *fullFileName = NULL;
432     int32_t ret = GetFullFileName(path, fileName, &fullFileName);
433     HKS_IF_NOT_SUCC_RETURN(ret, ret)
434 
435     ret = FileWrite(fullFileName, offset, buf, len);
436     HKS_FREE_PTR(fullFileName);
437     return ret;
438 }
439 
HksFileSize(const char * path,const char * fileName)440 uint32_t HksFileSize(const char *path, const char *fileName)
441 {
442     HKS_IF_NULL_RETURN(fileName, 0)
443 
444     char *fullFileName = NULL;
445     int32_t ret = GetFullFileName(path, fileName, &fullFileName);
446     HKS_IF_NOT_SUCC_RETURN(ret, 0)
447 
448     uint32_t size = FileSize(fullFileName);
449     HKS_FREE_PTR(fullFileName);
450     return size;
451 }
452 
HksIsFileExist(const char * path,const char * fileName)453 int32_t HksIsFileExist(const char *path, const char *fileName)
454 {
455     HKS_IF_NULL_RETURN(fileName, HKS_ERROR_NULL_POINTER)
456 
457     char *fullFileName = NULL;
458     int32_t ret = GetFullFileName(path, fileName, &fullFileName);
459     HKS_IF_NOT_SUCC_RETURN(ret, ret)
460 
461     ret = IsFileExist(fullFileName);
462     HKS_FREE_PTR(fullFileName);
463     return ret;
464 }
465 
HksFileRemove(const char * path,const char * fileName)466 int32_t HksFileRemove(const char *path, const char *fileName)
467 {
468     HKS_IF_NULL_RETURN(fileName, HKS_ERROR_INVALID_ARGUMENT)
469 
470     char *fullFileName = NULL;
471     int32_t ret = GetFullFileName(path, fileName, &fullFileName);
472     HKS_IF_NOT_SUCC_RETURN(ret, ret)
473 
474     ret = FileRemove(fullFileName);
475     HKS_FREE_PTR(fullFileName);
476     return ret;
477 }
478 
HksGetFileName(const char * path,const char * fileName,char * fullFileName,uint32_t fullFileNameLen)479 int32_t HksGetFileName(const char *path, const char *fileName, char *fullFileName, uint32_t fullFileNameLen)
480 {
481     return GetFileName(path, fileName, fullFileName, fullFileNameLen);
482 }
483 
484 #endif /* _CUT_AUTHENTICATE_ */
485