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 #include "cert_manager_file.h"
17 
18 #include "securec.h"
19 
20 #include "cert_manager_file_operator.h"
21 #include "cert_manager_mem.h"
22 #include "cert_manager_storage.h"
23 #include "cert_manager_updateflag.h"
24 #include "cm_cert_property_rdb.h"
25 #include "cm_log.h"
26 #include "cm_type.h"
27 
CertManagerFileSize(const char * path,const char * fileName)28 inline uint32_t CertManagerFileSize(const char *path, const char *fileName)
29 {
30     return CmFileSize(path, fileName);
31 }
32 
CertManagerFileRead(const char * path,const char * fileName,uint32_t offset,uint8_t * buf,uint32_t len)33 inline uint32_t CertManagerFileRead(const char *path, const char *fileName, uint32_t offset, uint8_t *buf, uint32_t len)
34 {
35     return CmFileRead(path, fileName, offset, buf, len);
36 }
37 
CertManagerFileWrite(const char * path,const char * fileName,uint32_t offset,const uint8_t * buf,uint32_t len)38 inline int32_t CertManagerFileWrite(const char *path, const char *fileName,
39     uint32_t offset, const uint8_t *buf, uint32_t len)
40 {
41     return CmFileWrite(path, fileName, offset, buf, len);
42 }
43 
CertManagerFileRemove(const char * path,const char * fileName)44 inline int32_t CertManagerFileRemove(const char *path, const char *fileName)
45 {
46     return CmFileRemove(path, fileName);
47 }
48 
GetNumberOfFiles(const char * path)49 static int32_t GetNumberOfFiles(const char *path)
50 {
51     void *dir = CmOpenDir(path);
52     if (dir == NULL) {
53         CM_LOG_W("can't open directory");
54         return -1;
55     }
56 
57     int32_t count = 0;
58     struct CmFileDirentInfo dire = {{0}};
59     while (CmGetDirFile(dir, &dire) == CMR_OK) {
60         count++;
61     }
62     (void)CmCloseDir(dir);
63     return count;
64 }
65 
FreeFileNames(struct CmMutableBlob * fNames,uint32_t fileCount)66 void FreeFileNames(struct CmMutableBlob *fNames, uint32_t fileCount)
67 {
68     if (fNames == NULL) {
69         return ;
70     }
71 
72     for (uint32_t i = 0; i < fileCount; i++) {
73         fNames[i].size = 0;
74         CM_FREE_PTR(fNames[i].data);
75     }
76     CMFree(fNames);
77 }
78 
MallocFileNames(struct CmMutableBlob ** fNames,const char * path,uint32_t * fileCount)79 static int32_t MallocFileNames(struct CmMutableBlob **fNames, const char *path, uint32_t *fileCount)
80 {
81     int32_t fileNums = GetNumberOfFiles(path);
82     if (fileNums == 0) {
83         return CM_SUCCESS;
84     }
85 
86     if (fileNums < 0) {
87         CM_LOG_E("Failed to obtain number of files from: path");
88         return CM_FAILURE;
89     }
90 
91     if (fileNums > MAX_COUNT_CERTIFICATE) {
92         CM_LOG_E("cert count %d beyond MAX", fileNums);
93         return CMR_ERROR_INVALID_ARGUMENT;
94     }
95 
96     uint32_t bufSize = sizeof(struct CmMutableBlob) * (uint32_t)fileNums;
97     struct CmMutableBlob *temp = (struct CmMutableBlob *)CMMalloc(bufSize);
98     if (temp == NULL) {
99         CM_LOG_E("Failed to allocate memory for file names");
100         return CMR_ERROR_MALLOC_FAIL;
101     }
102     (void)memset_s(temp, bufSize, 0, bufSize);
103 
104     *fNames = temp;
105     *fileCount = (uint32_t)fileNums;
106 
107     return CM_SUCCESS;
108 }
109 
GetFileNames(const char * path,struct CmMutableBlob * fNames,uint32_t fileCount)110 static int32_t GetFileNames(const char *path, struct CmMutableBlob *fNames, uint32_t fileCount)
111 {
112     void *d = CmOpenDir(path);
113     if (d == NULL) {
114         CM_LOG_E("Failed to open directory");
115         return CM_FAILURE;
116     }
117 
118     int32_t ret = CM_SUCCESS;
119     uint32_t i = 0;
120     struct CmFileDirentInfo dire = {0};
121     while (CmGetDirFile(d, &dire) == CMR_OK) {
122         /* get fileCount files first, verify in follow-up process, no need return err code */
123         if (i >= fileCount) {
124             CM_LOG_D("only get %u certfiles", fileCount);
125             break;
126         }
127 
128         uint32_t nameSize = strlen(dire.fileName) + 1; /* include '\0' at end */
129         fNames[i].data = (uint8_t *)CMMalloc(nameSize); /* uniformly free memory by caller */
130         if (fNames[i].data == NULL) {
131             CM_LOG_E("malloc file name data failed");
132             ret = CMR_ERROR_MALLOC_FAIL;
133             break;
134         }
135 
136         fNames[i].size = nameSize;
137         (void)memset_s(fNames[i].data, nameSize, 0, nameSize);
138         if (sprintf_s((char *)fNames[i].data, nameSize, "%s", dire.fileName) < 0) {
139             CM_LOG_E("copy file name failed");
140             ret = CM_FAILURE;
141             break;
142         }
143 
144         i++;
145     }
146 
147     (void) CmCloseDir(d);
148     if (i != fileCount) {
149         CM_LOG_E("get certfiles no enough");
150         ret = CM_FAILURE;
151     }
152 
153     return ret;
154 }
155 
CertManagerGetFilenames(struct CmMutableBlob * fileNames,const char * path)156 int32_t CertManagerGetFilenames(struct CmMutableBlob *fileNames, const char *path)
157 {
158     if ((fileNames == NULL) || (path == NULL)) {
159         CM_LOG_E("invalid parameters");
160         return CMR_ERROR_INVALID_ARGUMENT;
161     }
162 
163     uint32_t fileCount = 0;
164     struct CmMutableBlob *fNames = NULL;
165     int32_t ret = MallocFileNames(&fNames, path, &fileCount);
166     if (ret != CM_SUCCESS) {
167         CM_LOG_E("Failed to malloc memory for files name");
168         return ret;
169     }
170 
171     ret = GetFileNames(path, fNames, fileCount);
172     if (ret != CM_SUCCESS) {
173         CM_LOG_E("get file name failed");
174         FreeFileNames(fNames, fileCount);
175         return ret;
176     }
177 
178     fileNames->data = (uint8_t *)fNames;
179     fileNames->size = fileCount;
180     return ret;
181 }
182 
GetNumberOfDirs(const char * userIdPath)183 int32_t GetNumberOfDirs(const char *userIdPath)
184 {
185     void *dir = CmOpenDir(userIdPath);
186     if (dir == NULL) {
187         CM_LOG_W("can't open directory");
188         return CM_FAILURE;
189     }
190 
191     int32_t fileCount = 0;
192     struct CmFileDirentInfo dire = {{0}};
193     while (CmGetSubDir(dir, &dire) == CMR_OK) {
194         fileCount++;
195     }
196     (void)CmCloseDir(dir);
197     return fileCount;
198 }
199 
GetCertCount(const char * path)200 int32_t GetCertCount(const char *path)
201 {
202     return GetNumberOfFiles(path);
203 }