1 /*
2 * Copyright (c) 2020 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 <fcntl.h>
17 #include <securec.h>
18 #include <stdio.h>
19 #include <sys/types.h>
20 #include <unistd.h>
21 #include "hal_sys_param.h"
22 #ifdef USE_MBEDTLS
23 #include "mbedtls/sha256.h"
24 #endif
25 #include "ohos_errno.h"
26 #include "param_adaptor.h"
27 #include "parameter.h"
28
29 #define FILE_RO "ro."
30 #define OS_FULL_NAME_LEN 128
31 #define VERSION_ID_LEN 256
32 #define HASH_LENGTH 32
33 #define DEV_BUF_LENGTH 3
34 #define DEV_BUF_MAX_LENGTH 1024
35 #define DEV_UUID_LENGTH 65
36 #define OHOS_DISPLAY_VERSION_LEN 128
37 #define OHOS_PATCH_VERSION_LEN 64
38 #define OHOS_PATCH_VERSION_FILE "/patch/pversion"
39
40 static const char OHOS_OS_NAME[] = { "OpenHarmony" };
41 static const int OHOS_SDK_API_VERSION = 6;
42 static const char OHOS_SECURITY_PATCH_TAG[] = {"2021-09-01"};
43 static const char OHOS_RELEASE_TYPE[] = { "Beta" };
44
45 static const int MAJOR_VERSION = 1;
46 static const int SENIOR_VERSION = 0;
47 static const int FEATURE_VERSION = 1;
48 static const int BUILD_VERSION = 0;
49
50 static const char EMPTY_STR[] = { "" };
51
IsValidValue(const char * value,unsigned int len)52 static boolean IsValidValue(const char *value, unsigned int len)
53 {
54 if ((value == NULL) || !strlen(value) || (strlen(value) + 1 > len)) {
55 return FALSE;
56 }
57 return TRUE;
58 }
59
GetParameter(const char * key,const char * def,char * value,unsigned int len)60 int GetParameter(const char *key, const char *def, char *value, unsigned int len)
61 {
62 if ((key == NULL) || (value == NULL)) {
63 return EC_INVALID;
64 }
65 if (!CheckPermission()) {
66 return EC_FAILURE;
67 }
68 int ret = GetSysParam(key, value, len);
69 if (ret == EC_INVALID) {
70 return EC_INVALID;
71 }
72 if ((ret < 0) && IsValidValue(def, len)) {
73 if (strncpy_s(value, len, def, len - 1) != 0) {
74 return EC_FAILURE;
75 }
76 ret = (int)strlen(def);
77 }
78 return ret;
79 }
80
SetParameter(const char * key,const char * value)81 int SetParameter(const char *key, const char *value)
82 {
83 if ((key == NULL) || (value == NULL)) {
84 return EC_INVALID;
85 }
86 if (!CheckPermission()) {
87 return EC_FAILURE;
88 }
89 if (strncmp(key, FILE_RO, strlen(FILE_RO)) == 0) {
90 return EC_INVALID;
91 }
92
93 return SetSysParam(key, value);
94 }
95
GetDeviceType(void)96 const char *GetDeviceType(void)
97 {
98 return HalGetDeviceType();
99 }
100
GetManufacture(void)101 const char *GetManufacture(void)
102 {
103 return HalGetManufacture();
104 }
105
GetBrand(void)106 const char *GetBrand(void)
107 {
108 return HalGetBrand();
109 }
110
GetMarketName(void)111 const char *GetMarketName(void)
112 {
113 return HalGetMarketName();
114 }
115
GetProductSeries(void)116 const char *GetProductSeries(void)
117 {
118 return HalGetProductSeries();
119 }
120
GetProductModel(void)121 const char *GetProductModel(void)
122 {
123 return HalGetProductModel();
124 }
125
GetSoftwareModel(void)126 const char *GetSoftwareModel(void)
127 {
128 return HalGetSoftwareModel();
129 }
130
GetHardwareModel(void)131 const char *GetHardwareModel(void)
132 {
133 return HalGetHardwareModel();
134 }
135
GetHardwareProfile(void)136 const char *GetHardwareProfile(void)
137 {
138 return HalGetHardwareProfile();
139 }
140
GetSerial(void)141 const char *GetSerial(void)
142 {
143 return HalGetSerial();
144 }
145
GetBootloaderVersion(void)146 const char *GetBootloaderVersion(void)
147 {
148 return HalGetBootloaderVersion();
149 }
150
GetSecurityPatchTag(void)151 const char *GetSecurityPatchTag(void)
152 {
153 return OHOS_SECURITY_PATCH_TAG;
154 }
155
GetAbiList(void)156 const char *GetAbiList(void)
157 {
158 return HalGetAbiList();
159 }
160
BuildOSFullName(void)161 static const char *BuildOSFullName(void)
162 {
163 const char release[] = "Release";
164 char value[OS_FULL_NAME_LEN];
165 const char *releaseType = GetOsReleaseType();
166 int length;
167 if (strncmp(releaseType, release, sizeof(release) - 1) == 0) {
168 length = sprintf_s(value, OS_FULL_NAME_LEN, "%s-%d.%d.%d.%d",
169 OHOS_OS_NAME, MAJOR_VERSION, SENIOR_VERSION, FEATURE_VERSION, BUILD_VERSION);
170 } else {
171 length = sprintf_s(value, OS_FULL_NAME_LEN, "%s-%d.%d.%d.%d(%s)",
172 OHOS_OS_NAME, MAJOR_VERSION, SENIOR_VERSION, FEATURE_VERSION, BUILD_VERSION, releaseType);
173 }
174 if (length < 0) {
175 return EMPTY_STR;
176 }
177 const char *osFullName = strdup(value);
178 return osFullName;
179 }
180
GetOSFullName(void)181 const char *GetOSFullName(void)
182 {
183 static const char *osFullName = NULL;
184 if (osFullName != NULL) {
185 return osFullName;
186 }
187 osFullName = BuildOSFullName();
188 if (osFullName == NULL) {
189 return EMPTY_STR;
190 }
191 return osFullName;
192 }
193
BuildDisplayVersion(void)194 static const char *BuildDisplayVersion(void)
195 {
196 ssize_t len;
197 char patchValue[OHOS_PATCH_VERSION_LEN] = {0};
198 char displayValue[OHOS_DISPLAY_VERSION_LEN] = {0};
199 int fd = open(OHOS_PATCH_VERSION_FILE, O_RDONLY);
200 if (fd < 0) {
201 return NULL;
202 }
203 len = read(fd, patchValue, OHOS_PATCH_VERSION_LEN);
204 if (len < (ssize_t)strlen("version=")) {
205 close(fd);
206 return NULL;
207 }
208 close(fd);
209 if (patchValue[len - 1] == '\n') {
210 patchValue[len - 1] = '\0';
211 }
212 const char *versionValue = HalGetDisplayVersion();
213 const int versionLen = strlen(versionValue);
214 if (versionLen > 0) {
215 if (versionValue[versionLen - 1] != ')') {
216 len = sprintf_s(displayValue, OHOS_DISPLAY_VERSION_LEN, "%s(%s)", versionValue,
217 patchValue + strlen("version="));
218 } else {
219 char tempValue[versionLen];
220 (void)memset_s(tempValue, versionLen, 0, versionLen);
221 if (strncpy_s(tempValue, versionLen, versionValue, versionLen - 1) != 0) {
222 return NULL;
223 }
224 tempValue[versionLen - 1] = '\0';
225 len = sprintf_s(displayValue, OHOS_DISPLAY_VERSION_LEN, "%s%s)", tempValue,
226 patchValue + strlen("version="));
227 }
228 }
229 if (len < 0) {
230 return NULL;
231 }
232 return strdup(displayValue);
233 }
234
GetDisplayVersion(void)235 const char *GetDisplayVersion(void)
236 {
237 static const char *displayVersion = NULL;
238 if (displayVersion != NULL) {
239 return displayVersion;
240 }
241 displayVersion = BuildDisplayVersion();
242 if (displayVersion == NULL) {
243 return HalGetDisplayVersion();
244 }
245 return displayVersion;
246 }
247
GetSdkApiVersion(void)248 int GetSdkApiVersion(void)
249 {
250 return OHOS_SDK_API_VERSION;
251 }
252
GetFirstApiVersion(void)253 int GetFirstApiVersion(void)
254 {
255 return HalGetFirstApiVersion();
256 }
257
GetIncrementalVersion(void)258 const char *GetIncrementalVersion(void)
259 {
260 return HalGetIncrementalVersion();
261 }
262
BuildVersionId(void)263 static const char *BuildVersionId(void)
264 {
265 char value[VERSION_ID_LEN];
266 int len = sprintf_s(value, VERSION_ID_LEN, "%s/%s/%s/%s/%s/%s/%s/%d/%s/%s",
267 GetDeviceType(), GetManufacture(), GetBrand(), GetProductSeries(),
268 GetOSFullName(), GetProductModel(), GetSoftwareModel(),
269 OHOS_SDK_API_VERSION, GetIncrementalVersion(), GetBuildType());
270 if (len < 0) {
271 return EMPTY_STR;
272 }
273 const char *versionId = strdup(value);
274 return versionId;
275 }
276
GetVersionId(void)277 const char *GetVersionId(void)
278 {
279 static const char *versionId = NULL;
280 if (versionId != NULL) {
281 return versionId;
282 }
283 versionId = BuildVersionId();
284 if (versionId == NULL) {
285 return EMPTY_STR;
286 }
287 return versionId;
288 }
289
GetBuildType(void)290 const char *GetBuildType(void)
291 {
292 return HalGetBuildType();
293 }
294
GetBuildUser(void)295 const char *GetBuildUser(void)
296 {
297 return HalGetBuildUser();
298 }
299
GetBuildHost(void)300 const char *GetBuildHost(void)
301 {
302 return HalGetBuildHost();
303 }
304
GetBuildTime(void)305 const char *GetBuildTime(void)
306 {
307 return HalGetBuildTime();
308 }
309
GetBuildRootHash(void)310 const char *GetBuildRootHash(void)
311 {
312 return BUILD_ROOTHASH;
313 }
314
GetOsReleaseType(void)315 const char *GetOsReleaseType(void)
316 {
317 return OHOS_RELEASE_TYPE;
318 }
319 #ifdef USE_MBEDTLS
GetSha256Value(const unsigned char * input,char * udid,int udidSize)320 static int GetSha256Value(const unsigned char *input, char *udid, int udidSize)
321 {
322 if (input == NULL) {
323 return EC_FAILURE;
324 }
325 char buf[DEV_BUF_LENGTH] = { 0 };
326 unsigned char hash[HASH_LENGTH] = { 0 };
327
328 mbedtls_sha256_context context;
329 mbedtls_sha256_init(&context);
330 mbedtls_sha256_starts(&context, 0);
331 mbedtls_sha256_update(&context, input, strlen((const char *)input));
332 mbedtls_sha256_finish(&context, hash);
333
334 for (size_t i = 0; i < HASH_LENGTH; i++) {
335 unsigned char value = hash[i];
336 (void)memset_s(buf, DEV_BUF_LENGTH, 0, DEV_BUF_LENGTH);
337 sprintf_s(buf, sizeof(buf), "%02X", value);
338 if (strcat_s(udid, udidSize, buf) != 0) {
339 return EC_FAILURE;
340 }
341 }
342 return EC_SUCCESS;
343 }
344
GetDevUdid(char * udid,int size)345 int GetDevUdid(char *udid, int size)
346 {
347 if (size < DEV_UUID_LENGTH) {
348 return EC_FAILURE;
349 }
350
351 const char *manufacture = GetManufacture();
352 const char *model = GetProductModel();
353 const char *sn = GetSerial();
354 if (manufacture == NULL || model == NULL || sn == NULL) {
355 return EC_FAILURE;
356 }
357 int tmpSize = strlen(manufacture) + strlen(model) + strlen(sn) + 1;
358 if (tmpSize <= 0 || tmpSize > DEV_BUF_MAX_LENGTH) {
359 return EC_FAILURE;
360 }
361 char *tmp = malloc(tmpSize);
362 if (tmp == NULL) {
363 return EC_FAILURE;
364 }
365
366 (void)memset_s(tmp, tmpSize, 0, tmpSize);
367 if ((strcat_s(tmp, tmpSize, manufacture) != 0) ||
368 (strcat_s(tmp, tmpSize, model) != 0) ||
369 (strcat_s(tmp, tmpSize, sn) != 0)) {
370 free(tmp);
371 return EC_FAILURE;
372 }
373
374 int ret = GetSha256Value((const unsigned char *)tmp, udid, size);
375 free(tmp);
376 return ret;
377 }
378 #else
GetDevUdid(char * udid,int size)379 int GetDevUdid(char *udid, int size)
380 {
381 return 0;
382 }
383 #endif
384