1 /*
2 * Copyright (c) 2021-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 "param_comm.h"
17
18 #include <stdlib.h>
19 #include <string.h>
20
21 #include "init_param.h"
22 #ifdef LITEOS_SUPPORT
23 #include "hal_sys_param.h"
24 #endif
25 #include "parameter.h"
26 #include "sysparam_errno.h"
27 #ifdef USE_MBEDTLS
28 #include "mbedtls/sha256.h"
29 #endif
30
31 #include "securec.h"
32 #include "beget_ext.h"
33
GetSystemError(int err)34 INIT_LOCAL_API int GetSystemError(int err)
35 {
36 switch (err) {
37 case 0:
38 return 0;
39 case PARAM_CODE_INVALID_PARAM:
40 case PARAM_CODE_INVALID_NAME:
41 case PARAM_CODE_READ_ONLY:
42 return EC_INVALID;
43 case PARAM_CODE_INVALID_VALUE:
44 return SYSPARAM_INVALID_VALUE;
45 case PARAM_CODE_NOT_FOUND:
46 case PARAM_CODE_NODE_EXIST:
47 return SYSPARAM_NOT_FOUND;
48 case DAC_RESULT_FORBIDED:
49 return SYSPARAM_PERMISSION_DENIED;
50 case PARAM_CODE_REACHED_MAX:
51 case PARAM_CODE_FAIL_CONNECT:
52 case PARAM_CODE_INVALID_SOCKET:
53 case PARAM_CODE_NOT_SUPPORT:
54 return SYSPARAM_SYSTEM_ERROR;
55 case PARAM_CODE_TIMEOUT:
56 return SYSPARAM_WAIT_TIMEOUT;
57 default:
58 return SYSPARAM_SYSTEM_ERROR;
59 }
60 }
61
IsValidParamValue(const char * value,uint32_t len)62 INIT_LOCAL_API int IsValidParamValue(const char *value, uint32_t len)
63 {
64 if ((value == NULL) || (strlen(value) + 1 > len)) {
65 return 0;
66 }
67 return 1;
68 }
69
GetParameter_(const char * key,const char * def,char * value,uint32_t len)70 INIT_LOCAL_API int GetParameter_(const char *key, const char *def, char *value, uint32_t len)
71 {
72 if ((key == NULL) || (value == NULL) || (len > (uint32_t)PARAM_BUFFER_MAX)) {
73 return EC_INVALID;
74 }
75 uint32_t size = len;
76 int ret = SystemGetParameter(key, NULL, &size);
77 if (ret != 0) {
78 if (def == NULL) {
79 return GetSystemError(ret);
80 }
81 if (strlen(def) > len) {
82 return EC_INVALID;
83 }
84 ret = strcpy_s(value, len, def);
85 return (ret == 0) ? 0 : EC_FAILURE;
86 } else if (size > len) {
87 return EC_INVALID;
88 }
89
90 size = len;
91 ret = SystemGetParameter(key, value, &size);
92 return GetSystemError(ret);
93 }
94
GetProperty(const char * key,const char ** paramHolder)95 INIT_LOCAL_API const char *GetProperty(const char *key, const char **paramHolder)
96 {
97 BEGET_CHECK(paramHolder != NULL, return NULL);
98 if (*paramHolder != NULL) {
99 return *paramHolder;
100 }
101 uint32_t len = 0;
102 int ret = SystemGetParameter(key, NULL, &len);
103 if (ret == 0 && len > 0) {
104 char *res = (char *)malloc(len + 1);
105 BEGET_CHECK(res != NULL, return NULL);
106 ret = SystemGetParameter(key, res, &len);
107 if (ret != 0) {
108 free(res);
109 return NULL;
110 }
111 *paramHolder = res;
112 }
113 return *paramHolder;
114 }
115
GetProductModel_(void)116 INIT_LOCAL_API const char *GetProductModel_(void)
117 {
118 static const char *productModel = NULL;
119 return GetProperty("const.product.model", &productModel);
120 }
121
GetManufacture_(void)122 INIT_LOCAL_API const char *GetManufacture_(void)
123 {
124 static const char *productManufacture = NULL;
125 return GetProperty("const.product.manufacturer", &productManufacture);
126 }
127
128 #ifdef USE_MBEDTLS
GetSha256Value(const char * input,char * udid,int udidSize)129 static int GetSha256Value(const char *input, char *udid, int udidSize)
130 {
131 if (input == NULL) {
132 return EC_FAILURE;
133 }
134 char buf[DEV_BUF_LENGTH] = { 0 };
135 unsigned char hash[HASH_LENGTH] = { 0 };
136
137 mbedtls_sha256_context context;
138 mbedtls_sha256_init(&context);
139 mbedtls_sha256_starts(&context, 0);
140 mbedtls_sha256_update(&context, (const unsigned char *)input, strlen(input));
141 mbedtls_sha256_finish(&context, hash);
142
143 for (size_t i = 0; i < HASH_LENGTH; i++) {
144 unsigned char value = hash[i];
145 memset_s(buf, DEV_BUF_LENGTH, 0, DEV_BUF_LENGTH);
146 int len = sprintf_s(buf, sizeof(buf), "%02X", value);
147 if (len > 0 && strcat_s(udid, udidSize, buf) != 0) {
148 return EC_FAILURE;
149 }
150 }
151 return EC_SUCCESS;
152 }
153 #else
GetSha256Value(const char * input,char * udid,int udidSize)154 static int GetSha256Value(const char *input, char *udid, int udidSize)
155 {
156 (void)input;
157 (void)udid;
158 (void)udidSize;
159 return EC_FAILURE;
160 }
161 #endif
162
GetSerial_(void)163 INIT_LOCAL_API const char *GetSerial_(void)
164 {
165 #ifdef LITEOS_SUPPORT
166 return HalGetSerial();
167 #else
168 static char *ohosSerial = NULL;
169 if (ohosSerial == NULL) {
170 BEGET_CHECK((ohosSerial = (char *)calloc(1, PARAM_VALUE_LEN_MAX)) != NULL, return NULL);
171 }
172 uint32_t len = PARAM_VALUE_LEN_MAX;
173 int ret = SystemGetParameter("ohos.boot.sn", ohosSerial, &len);
174 BEGET_CHECK(ret == 0, return NULL);
175 return ohosSerial;
176 #endif
177 }
178
GetDevUdid_(char * udid,int size)179 INIT_LOCAL_API int GetDevUdid_(char *udid, int size)
180 {
181 if (size < UDID_LEN || udid == NULL) {
182 return EC_FAILURE;
183 }
184
185 uint32_t len = (uint32_t)size;
186 int ret = SystemGetParameter("const.product.udid", udid, &len);
187 BEGET_CHECK(ret != 0, return ret);
188
189 const char *manufacture = GetManufacture_();
190 const char *model = GetProductModel_();
191 const char *sn = GetSerial_();
192 if (manufacture == NULL || model == NULL || sn == NULL) {
193 return -1;
194 }
195 int tmpSize = strlen(manufacture) + strlen(model) + strlen(sn) + 1;
196 if (tmpSize <= 1 || tmpSize > DEV_BUF_MAX_LENGTH) {
197 return -1;
198 }
199 char *tmp = NULL;
200 BEGET_CHECK((tmp = (char *)malloc(tmpSize)) != NULL, return -1);
201
202 (void)memset_s(tmp, tmpSize, 0, tmpSize);
203 if ((strcat_s(tmp, tmpSize, manufacture) != 0) || (strcat_s(tmp, tmpSize, model) != 0) ||
204 (strcat_s(tmp, tmpSize, sn) != 0)) {
205 free(tmp);
206 return -1;
207 }
208
209 ret = GetSha256Value(tmp, udid, size);
210 free(tmp);
211 return ret;
212 }
213
GetFullName_(void)214 INIT_LOCAL_API const char *GetFullName_(void)
215 {
216 static const char *fillname = NULL;
217 return GetProperty("const.ohos.fullname", &fillname);
218 }
219