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