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 <stdlib.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 #include "limits.h"
22 #include "securec.h"
23 #include "device_attest_oem_file.h"
24
OEMGenFilePath(const char * dirPath,const char * fileName)25 char* OEMGenFilePath(const char* dirPath, const char* fileName)
26 {
27 if (dirPath == NULL || fileName == NULL) {
28 return NULL;
29 }
30
31 uint32_t filePathLen = strlen(dirPath) + 1 + strlen(fileName) + 1;
32 if (filePathLen > PATH_MAX) {
33 return NULL;
34 }
35 char* filePath = (char *)malloc(filePathLen);
36 if (filePath == NULL) {
37 return NULL;
38 }
39 (void)memset_s(filePath, filePathLen, 0, filePathLen);
40 if (sprintf_s(filePath, filePathLen, "%s%s%s", dirPath, "/", fileName) < 0) {
41 free(filePath);
42 return NULL;
43 }
44 return filePath;
45 }
46
OEMGetFileSize(const char * path,const char * fileName,uint32_t * result)47 int32_t OEMGetFileSize(const char* path, const char* fileName, uint32_t* result)
48 {
49 if (path == NULL || fileName == NULL || result == NULL) {
50 return DEVICE_ATTEST_OEM_ERR;
51 }
52
53 char* filePath = OEMGenFilePath(path, fileName);
54 if (filePath == NULL) {
55 return DEVICE_ATTEST_OEM_ERR;
56 }
57
58 char* formatPath = realpath(filePath, NULL);
59 if (formatPath == NULL) {
60 return DEVICE_ATTEST_OEM_ERR;
61 }
62
63 FILE* fp = fopen(formatPath, "r");
64 if (fp == NULL) {
65 free(formatPath);
66 return DEVICE_ATTEST_OEM_ERR;
67 }
68 if (fseek(fp, 0, SEEK_END) < 0) {
69 free(formatPath);
70 (void)fclose(fp);
71 return DEVICE_ATTEST_OEM_ERR;
72 }
73 *result = ftell(fp);
74 free(formatPath);
75 (void)fclose(fp);
76 return DEVICE_ATTEST_OEM_OK;
77 }
78
OEMWriteFile(const char * path,const char * fileName,const char * data,uint32_t dataLen)79 int32_t OEMWriteFile(const char* path, const char* fileName, const char* data, uint32_t dataLen)
80 {
81 if (path == NULL || fileName == NULL || data == NULL || dataLen == 0) {
82 return DEVICE_ATTEST_OEM_ERR;
83 }
84
85 char* filePath = OEMGenFilePath(path, fileName);
86 if (filePath == NULL) {
87 return DEVICE_ATTEST_OEM_ERR;
88 }
89
90 char* formatPath = realpath(filePath, NULL);
91 free(filePath);
92 if (formatPath == NULL) {
93 return DEVICE_ATTEST_OEM_ERR;
94 }
95
96 FILE* fp = fopen(formatPath, "wb+");
97 if (fp == NULL) {
98 free(formatPath);
99 return DEVICE_ATTEST_OEM_ERR;
100 }
101 int32_t ret = DEVICE_ATTEST_OEM_OK;
102 do {
103 if (fwrite(data, dataLen, 1, fp) != 1) {
104 ret = DEVICE_ATTEST_OEM_ERR;
105 break;
106 }
107 if (fflush(fp) != DEVICE_ATTEST_OEM_OK) {
108 ret = DEVICE_ATTEST_OEM_ERR;
109 break;
110 }
111 int fd = fileno(fp);
112 if (fsync(fd) != DEVICE_ATTEST_OEM_OK) {
113 ret = DEVICE_ATTEST_OEM_ERR;
114 break;
115 }
116 } while (0);
117 free(formatPath);
118 (void)fclose(fp);
119 return ret;
120 }
121
OEMReadFile(const char * path,const char * fileName,char * buffer,uint32_t bufferLen)122 int32_t OEMReadFile(const char* path, const char* fileName, char* buffer, uint32_t bufferLen)
123 {
124 if (path == NULL || fileName == NULL || buffer == NULL || bufferLen == 0) {
125 return DEVICE_ATTEST_OEM_ERR;
126 }
127
128 uint32_t fileSize = 0;
129 if (OEMGetFileSize(path, fileName, &fileSize) != 0 || fileSize > bufferLen) {
130 return DEVICE_ATTEST_OEM_ERR;
131 }
132
133 char* filePath = OEMGenFilePath(path, fileName);
134 if (filePath == NULL) {
135 return DEVICE_ATTEST_OEM_ERR;
136 }
137
138 char* formatPath = realpath(filePath, NULL);
139 free(filePath);
140 if (formatPath == NULL) {
141 return DEVICE_ATTEST_OEM_ERR;
142 }
143
144 FILE* fp = fopen(formatPath, "rb");
145 if (fp == NULL) {
146 free(formatPath);
147 return DEVICE_ATTEST_OEM_ERR;
148 }
149 if (fread(buffer, fileSize, 1, fp) != 1) {
150 free(formatPath);
151 (void)fclose(fp);
152 return DEVICE_ATTEST_OEM_ERR;
153 }
154 free(formatPath);
155 (void)fclose(fp);
156 return DEVICE_ATTEST_OEM_OK;
157 }
158
OEMCreateFile(const char * path,const char * fileName)159 int32_t OEMCreateFile(const char* path, const char* fileName)
160 {
161 if (path == NULL || fileName == NULL) {
162 return DEVICE_ATTEST_OEM_ERR;
163 }
164
165 char* formatPath = realpath(path, NULL);
166 if (formatPath == NULL) {
167 return DEVICE_ATTEST_OEM_ERR;
168 }
169 uint32_t realPathLen = strlen(formatPath) + 1 + strlen(fileName) + 1;
170 if (realPathLen > PATH_MAX) {
171 return DEVICE_ATTEST_OEM_ERR;
172 }
173 char* realPath = (char *)malloc(realPathLen);
174 if (realPath == NULL) {
175 free(formatPath);
176 return DEVICE_ATTEST_OEM_ERR;
177 }
178 (void)memset_s(realPath, realPathLen, 0, realPathLen);
179 if (sprintf_s(realPath, realPathLen, "%s%s%s", formatPath, "/", fileName) < 0) {
180 free(formatPath);
181 free(realPath);
182 return DEVICE_ATTEST_OEM_ERR;
183 }
184 free(formatPath);
185
186 FILE* fp = fopen(realPath, "w");
187 if (fp == NULL) {
188 free(realPath);
189 return DEVICE_ATTEST_OEM_ERR;
190 }
191 free(realPath);
192 int32_t ret = DEVICE_ATTEST_OEM_OK;
193 do {
194 if (fflush(fp) != DEVICE_ATTEST_OEM_OK) {
195 ret = DEVICE_ATTEST_OEM_ERR;
196 break;
197 }
198 int fd = fileno(fp);
199 if (fsync(fd) != DEVICE_ATTEST_OEM_OK) {
200 ret = DEVICE_ATTEST_OEM_ERR;
201 break;
202 }
203 } while (0);
204 (void)fclose(fp);
205 return ret;
206 }