1 #include "core_auth.h"
2 
core_auth_tls_psk(aiot_sysdep_portfile_t * sysdep,char ** psk_id,char psk[65],char * product_key,char * device_name,char * device_secret,char * module_name)3 int32_t core_auth_tls_psk(aiot_sysdep_portfile_t *sysdep, char **psk_id, char psk[65], char *product_key, char *device_name, char *device_secret, char *module_name)
4 {
5     int32_t res = STATE_SUCCESS;
6     char *tmp_psk_id = NULL, *auth_type = "devicename", *sign_method = "hmacsha256";
7     char *psk_id_src[] = { auth_type, sign_method, product_key, device_name, CORE_AUTH_TIMESTAMP};
8     char *psk_plain_text = NULL, *psk_plain_text_src[] = { product_key, device_name, CORE_AUTH_TIMESTAMP};
9     uint8_t psk_hex[32] = {0};
10 
11     if (NULL == device_secret) {
12         return STATE_USER_INPUT_MISSING_DEVICE_SECRET;
13     }
14 
15     res = core_sprintf(sysdep, &tmp_psk_id, "%s|%s|%s&%s|%s", psk_id_src, sizeof(psk_id_src)/sizeof(char *), module_name);
16     if (res < STATE_SUCCESS) {
17         return res;
18     }
19 
20     res = core_sprintf(sysdep, &psk_plain_text, "id%s&%stimestamp%s", psk_plain_text_src, sizeof(psk_plain_text_src)/sizeof(char *), module_name);
21     if (res < STATE_SUCCESS) {
22         sysdep->core_sysdep_free(tmp_psk_id);
23         return res;
24     }
25 
26     core_hmac_sha256((const uint8_t *)psk_plain_text, (uint32_t)strlen(psk_plain_text), (const uint8_t *)device_secret, (uint32_t)strlen(device_secret), psk_hex);
27     core_hex2str(psk_hex, 32, psk, 0);
28 
29     *psk_id = tmp_psk_id;
30     sysdep->core_sysdep_free(psk_plain_text);
31 
32     return res;
33 }
34 
core_auth_mqtt_username(aiot_sysdep_portfile_t * sysdep,char ** dest,char * product_key,char * device_name,char * module_name)35 int32_t core_auth_mqtt_username(aiot_sysdep_portfile_t *sysdep, char **dest, char *product_key, char *device_name, char *module_name)
36 {
37     char *src[] = { device_name, product_key };
38 
39     return core_sprintf(sysdep, dest, "%s&%s", src, sizeof(src)/sizeof(char *), module_name);
40 }
41 
core_auth_mqtt_password(aiot_sysdep_portfile_t * sysdep,char ** dest,char * product_key,char * device_name,char * device_secret,uint8_t assigned_clientid,char * module_name)42 int32_t core_auth_mqtt_password(aiot_sysdep_portfile_t *sysdep, char **dest, char *product_key, char *device_name, char *device_secret, uint8_t assigned_clientid, char *module_name)
43 {
44     int32_t res = 0;
45     char *plain_text = NULL;
46     uint8_t sign[32] = {0};
47 
48     if(1 == assigned_clientid) {
49         char *src[] = { device_name, product_key, CORE_AUTH_TIMESTAMP };
50         res = core_sprintf(sysdep, &plain_text, "clientIddeviceName%sproductKey%stimestamp%s", src, sizeof(src)/sizeof(char *), module_name);
51     } else {
52         char *src[] = { product_key, device_name, device_name, product_key, CORE_AUTH_TIMESTAMP };
53         res = core_sprintf(sysdep, &plain_text, "clientId%s.%sdeviceName%sproductKey%stimestamp%s", src, sizeof(src)/sizeof(char *), module_name);
54     }
55 
56     if (res < STATE_SUCCESS) {
57         return res;
58     }
59 
60     *dest = sysdep->core_sysdep_malloc(65, module_name);
61     if (*dest == NULL) {
62         sysdep->core_sysdep_free(plain_text);
63         return STATE_SYS_DEPEND_MALLOC_FAILED;
64     }
65     memset(*dest, 0, 65);
66 
67     core_hmac_sha256((const uint8_t *)plain_text, (uint32_t)strlen(plain_text), (const uint8_t *)device_secret, (uint32_t)strlen(device_secret),sign);
68     core_hex2str(sign, 32, *dest, 0);
69 
70     sysdep->core_sysdep_free(plain_text);
71 
72     return 0;
73 }
74 
core_auth_mqtt_clientid(aiot_sysdep_portfile_t * sysdep,char ** dest,char * product_key,char * device_name,char * secure_mode,char * extend_clientid,uint8_t assigned_clientid,char * module_name)75 int32_t core_auth_mqtt_clientid(aiot_sysdep_portfile_t *sysdep, char **dest, char *product_key, char *device_name, char *secure_mode, char *extend_clientid, uint8_t assigned_clientid, char *module_name)
76 {
77     if(1 == assigned_clientid) {
78         char *src[] = { CORE_AUTH_TIMESTAMP, CORE_AUTH_SDK_VERSION, secure_mode, extend_clientid};
79         return core_sprintf(sysdep, dest, "|timestamp=%s,_ss=1,_v=%s,securemode=%s,signmethod=hmacsha256,ext=3,%s|", src, sizeof(src)/sizeof(char *), module_name);    /* ext bitmap: bit0-rrpc, bit1-ext_notify */
80     } else {
81        char *src[] = { product_key, device_name, CORE_AUTH_TIMESTAMP, CORE_AUTH_SDK_VERSION, secure_mode, extend_clientid};
82        return core_sprintf(sysdep, dest, "%s.%s|timestamp=%s,_ss=1,_v=%s,securemode=%s,signmethod=hmacsha256,ext=3,%s|", src, sizeof(src)/sizeof(char *), module_name);    /* ext bitmap: bit0-rrpc, bit1-ext_notify */
83     }
84 }
85 
86 
87 
core_auth_http_body(aiot_sysdep_portfile_t * sysdep,char ** dest,char * product_key,char * device_name,char * device_secret,char * module_name)88 int32_t core_auth_http_body(aiot_sysdep_portfile_t *sysdep, char **dest, char *product_key, char *device_name, char *device_secret, char *module_name)
89 {
90     int32_t res = 0;
91     char *sign_ele[] = { product_key, device_name, device_name, product_key, NULL };
92     char *plain_text = NULL;
93     uint8_t sign_hex[32] = {0};
94     char sign_str[65] = {0};
95 
96     res = core_sprintf(sysdep, &plain_text, "clientId%s.%sdeviceName%sproductKey%s", sign_ele, 4, module_name);
97     if (res < STATE_SUCCESS) {
98         return res;
99     }
100 
101     core_hmac_sha256((const uint8_t *)plain_text, (uint32_t)strlen(plain_text), (const uint8_t *)device_secret, (uint32_t)strlen(device_secret), sign_hex);
102     core_hex2str(sign_hex, 32, sign_str, 0);
103 
104     sysdep->core_sysdep_free(plain_text);
105     sign_ele[4] = sign_str;
106     res = core_sprintf(sysdep,
107                        dest,
108                        "{\"clientId\":\"%s.%s\",\"signmethod\":\"hmacsha256\",\"deviceName\":\"%s\",\"productKey\":\"%s\",\"sign\":\"%s\"}",
109                        sign_ele,
110                        sizeof(sign_ele)/sizeof(char *),
111                        module_name);
112 
113     return res;
114 }
115 
116