1 /*
2 * Copyright (C) 2015-2018 Alibaba Group Holding Limited
3 */
4
5 #include <stdio.h>
6 #include <string.h>
7 #include "aos/kernel.h"
8 #include "linkkit/wrappers/wrappers_defs.h"
9 #include "linkkit/infra/infra_aes.h"
10 #include "ulog/ulog.h"
11 #include "aos/kv.h"
12
13 #define DEVINFO_DN "hal_devinfo_dn"
14 #define DEVINFO_DS "hal_devinfo_ds"
15 #define DEVINFO_PK "hal_devinfo_pk"
16 #define DEVINFO_PS "hal_devinfo_ps"
17 #define LOG_TAG "HAL"
18 static const char *demo_iv = "f165u329c054k637";
19
20 #define PLATFORM_WAIT_INFINITE (~0)
21
22 char _product_key[IOTX_PRODUCT_KEY_LEN + 1] = { 0 };
23 char _product_secret[IOTX_PRODUCT_SECRET_LEN + 1] = { 0 };
24 char _device_name[IOTX_DEVICE_NAME_LEN + 1] = { 0 };
25 char _device_secret[IOTX_DEVICE_SECRET_LEN + 1] = { 0 };
26
HAL_SetProductKey(char * product_key)27 int HAL_SetProductKey(char *product_key)
28 {
29 int len;
30 if (product_key == NULL) {
31 return -1;
32 }
33
34 memset(_product_key, 0x0, IOTX_PRODUCT_KEY_LEN + 1);
35 len = strlen(product_key);
36 if (len > IOTX_PRODUCT_KEY_LEN) {
37 return -1;
38 }
39
40 strncpy(_product_key, product_key, len);
41 return len;
42 }
43
HAL_SetDeviceName(char * device_name)44 int HAL_SetDeviceName(char *device_name)
45 {
46 int len;
47 if (device_name == NULL) {
48 return -1;
49 }
50
51 memset(_device_name, 0x0, IOTX_DEVICE_NAME_LEN + 1);
52 len = strlen(device_name);
53 if (len > IOTX_DEVICE_NAME_LEN) {
54 return -1;
55 }
56
57 strncpy(_device_name, device_name, len);
58 return len;
59 }
60
HAL_SetProductSecret(char * product_secret)61 int HAL_SetProductSecret(char *product_secret)
62 {
63 int res;
64 int len;
65
66 if (product_secret == NULL) {
67 return -1;
68 }
69
70 memset(_product_secret, 0x0, IOTX_PRODUCT_SECRET_LEN + 1);
71 len = strlen(product_secret);
72 if (len > IOTX_PRODUCT_SECRET_LEN) {
73 return -1;
74 }
75
76 strncpy(_product_secret, product_secret, len);
77 return len;
78 }
79
HAL_SetDeviceSecret(char * device_secret)80 int HAL_SetDeviceSecret(char *device_secret)
81 {
82 int len;
83 if (device_secret == NULL) {
84 return -1;
85 }
86
87 memset(_device_secret, 0x0, IOTX_DEVICE_SECRET_LEN + 1);
88 len = strlen(device_secret);
89 if (len > IOTX_DEVICE_SECRET_LEN) {
90 return -1;
91 }
92
93 strncpy(_device_secret, device_secret, len);
94 return len;
95 }
96
HAL_GetProductKey(char product_key[IOTX_PRODUCT_KEY_LEN+1])97 int HAL_GetProductKey(char product_key[IOTX_PRODUCT_KEY_LEN + 1])
98 {
99 int len = strlen(_product_key);
100 memset(product_key, 0x0, IOTX_PRODUCT_KEY_LEN + 1);
101 if (len == 0) {
102 len = IOTX_PRODUCT_KEY_LEN;
103 aos_kv_get(DEVINFO_PK, _product_key, &len);
104 }
105
106 strncpy(product_key, _product_key, len);
107
108 return len;
109 }
110
HAL_GetProductSecret(char product_secret[IOTX_PRODUCT_SECRET_LEN+1])111 int HAL_GetProductSecret(char product_secret[IOTX_PRODUCT_SECRET_LEN + 1])
112 {
113 int len = strlen(_product_secret);
114 if (len == 0) {
115 #if 1
116 len = IOTX_PRODUCT_SECRET_LEN;
117 aos_kv_get(DEVINFO_PS, _product_secret, &len);
118 #else
119 len = strlen("kvFuluNMSGaLXIT6");
120 memcpy(_product_secret, "kvFuluNMSGaLXIT6", len + 1);
121 #endif
122 }
123
124 memset(product_secret, 0x0, IOTX_PRODUCT_SECRET_LEN + 1);
125 strncpy(product_secret, _product_secret, len);
126
127 return len;
128
129 return len;
130 }
131
HAL_GetDeviceName(char device_name[IOTX_DEVICE_NAME_LEN+1])132 int HAL_GetDeviceName(char device_name[IOTX_DEVICE_NAME_LEN + 1])
133 {
134 int len = strlen(_device_name);
135 memset(device_name, 0x0, IOTX_DEVICE_NAME_LEN + 1);
136 if (len == 0) {
137 len = IOTX_DEVICE_NAME_LEN;
138 aos_kv_get(DEVINFO_DN, _device_name, &len);
139 }
140 strncpy(device_name, _device_name, len);
141
142 return strlen(device_name);
143 }
144
HAL_GetDeviceSecret(char device_secret[IOTX_DEVICE_SECRET_LEN+1])145 int HAL_GetDeviceSecret(char device_secret[IOTX_DEVICE_SECRET_LEN + 1])
146 {
147 int len = strlen(_device_secret);
148 if (len == 0) {
149 #if 1
150 len = IOTX_DEVICE_SECRET_LEN;
151 aos_kv_get(DEVINFO_DS, _device_secret, &len);
152 #else
153 len = strlen("1e71e85af103c118eca9409cb4132705");
154 memcpy(_device_secret, "1e71e85af103c118eca9409cb4132705", len + 1);
155 #endif
156 }
157
158 memset(device_secret, 0x0, IOTX_DEVICE_SECRET_LEN + 1);
159 strncpy(device_secret, _device_secret, len);
160
161 return len;
162 }
163
HAL_SaveDeviceIdentity(char * pk,char * ps,char * dn,char * ds)164 int HAL_SaveDeviceIdentity(char *pk, char *ps, char *dn, char *ds)
165 {
166 int res, len;
167
168 if (HAL_SetProductKey(pk) > 0) {
169 len = strlen(pk);
170 aos_kv_set(DEVINFO_PK, pk, len, 1);
171 }
172
173 if (HAL_SetProductSecret(ps) > 0) {
174 do {
175 char dec_secret[IOTX_PRODUCT_SECRET_LEN + 1] = { 0 };
176 if (strlen(_product_key) == 0) {
177 HAL_GetProductKey(_product_key);
178 }
179 p_Aes128_t aes_e_h =
180 infra_aes128_init((unsigned char *)_product_key,
181 (unsigned char *)demo_iv, AES_ENCRYPTION);
182 if (aes_e_h == NULL) {
183 LOGE(LOG_TAG, "aes init failed");
184 break;
185 }
186 len = strlen(ps);
187 res = infra_aes128_cfb_encrypt(aes_e_h, _product_secret, len,
188 dec_secret);
189 infra_aes128_destroy(aes_e_h);
190 if (res < 0) {
191 LOGE(LOG_TAG, "encrypt ps failed");
192 break;
193 }
194 aos_kv_set(DEVINFO_PS, dec_secret, len, 1);
195 } while (0);
196 }
197
198 if (HAL_SetDeviceName(dn) > 0) {
199 len = strlen(dn);
200 aos_kv_set(DEVINFO_DN, dn, len, 1);
201 }
202
203 if (HAL_SetDeviceSecret(ds) > 0) {
204 do {
205 char dec_secret[IOTX_DEVICE_SECRET_LEN + 1] = { 0 };
206 if (strlen(_product_key) == 0) {
207 HAL_GetProductKey(_product_key);
208 }
209 p_Aes128_t aes_e_h =
210 infra_aes128_init((unsigned char *)_product_key,
211 (unsigned char *)demo_iv, AES_ENCRYPTION);
212 if (aes_e_h == NULL) {
213 LOGE(LOG_TAG, "aes init failed");
214 break;
215 }
216 len = strlen(ds);
217 res = infra_aes128_cfb_encrypt(aes_e_h, ds, len, dec_secret);
218 infra_aes128_destroy(aes_e_h);
219 if (res < 0) {
220 LOGE(LOG_TAG, "encrypt ds failed");
221 break;
222 }
223 aos_kv_set(DEVINFO_DS, dec_secret, len, 1);
224 } while (0);
225 }
226
227 return 0;
228 }
229
HAL_ClearDeviceIdentity()230 int HAL_ClearDeviceIdentity()
231 {
232 aos_kv_del(DEVINFO_PK);
233 aos_kv_del(DEVINFO_PS);
234 aos_kv_del(DEVINFO_DN);
235 aos_kv_del(DEVINFO_DS);
236 return 0;
237 }
238
HAL_GetFirmwareVersion(_OU_ char version[IOTX_FIRMWARE_VERSION_LEN])239 int HAL_GetFirmwareVersion(_OU_ char version[IOTX_FIRMWARE_VERSION_LEN])
240 {
241 memset(version, 0x0, IOTX_FIRMWARE_VERSION_LEN);
242
243 strncpy(version, (const char *)SYSINFO_APP_VERSION,
244 IOTX_FIRMWARE_VERSION_LEN - 1);
245 version[IOTX_FIRMWARE_VERSION_LEN - 1] = '\0';
246
247 return strlen(version);
248 }
249