1 /*
2  * Copyright (C) 2015-2018 Alibaba Group Holding Limited
3  */
4 #include "wifi_provision_internal.h"
5 
6 #if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
7 extern "C" {
8 #endif
9 
awss_complete_token(char passwd[HAL_MAX_PASSWD_LEN],uint8_t * bssid,uint8_t bssid_len,uint8_t * token_in,uint8_t token_len,uint8_t token_out[AWSS_TOKEN_LEN])10 int awss_complete_token(char passwd[HAL_MAX_PASSWD_LEN], uint8_t *bssid,
11                         uint8_t bssid_len, uint8_t *token_in, uint8_t token_len,
12                         uint8_t token_out[AWSS_TOKEN_LEN])
13 {
14     /*need to complete the token*/
15     int ret = 0;
16     if (token_len != 0 && token_len < 16 && token_in != NULL) {
17         int org_token_len = 0;
18         unsigned char buff[128] = { 0 };
19         unsigned char gen_token[32] = { 0 };
20         uint8_t pwd_len = strlen(passwd);
21 
22         if (bssid != NULL) {
23             memcpy(buff + org_token_len, bssid, bssid_len);
24             org_token_len += bssid_len;
25         }
26 
27         memcpy(buff + org_token_len, token_in, token_len);
28         org_token_len += token_len;
29 
30         if (pwd_len != 0 && 128 >= pwd_len + org_token_len) {
31             memcpy(buff + org_token_len, passwd, pwd_len);
32             org_token_len += pwd_len;
33         }
34 
35         utils_sha256(buff, org_token_len, gen_token);
36         memcpy(token_out, gen_token, AWSS_TOKEN_LEN);
37 
38     } else if (token_len == AWSS_TOKEN_LEN && token_in != NULL) {
39         memcpy(token_out, token_in, AWSS_TOKEN_LEN);
40     } else {
41         ret = -1;
42     }
43 
44     return ret;
45 }
awss_connect(char ssid[HAL_MAX_SSID_LEN],char passwd[HAL_MAX_PASSWD_LEN],uint8_t * bssid,uint8_t bssid_len,uint8_t * token,uint8_t token_len)46 int awss_connect(char ssid[HAL_MAX_SSID_LEN], char passwd[HAL_MAX_PASSWD_LEN],
47                  uint8_t *bssid, uint8_t bssid_len, uint8_t *token,
48                  uint8_t token_len)
49 {
50     unsigned char final_token[AWSS_TOKEN_LEN] = { 0 };
51     unsigned char final_bssid[6] = { 0 };
52 
53     uint8_t has_bssid = 1;
54     int ret;
55 
56     /*need to complete the token*/
57     ret = awss_complete_token(passwd, bssid, bssid_len, token, token_len,
58                               final_token);
59 
60     if (ret == 0) {
61         awss_set_token(final_token);
62     }
63 
64     /*need to complete the bssid */
65     if (bssid_len > 0 && bssid_len < 6 && bssid != NULL) {
66         if (zc_bssid != NULL) {
67             memcpy(final_bssid, zc_bssid, 6);
68         } else {
69             has_bssid = 0;
70         }
71     } else if (bssid_len == 6 && bssid != NULL) {
72         memcpy(final_bssid, bssid, 6);
73     } else {
74         has_bssid = 0;
75     }
76 
77     return HAL_Awss_Connect_Ap(WLAN_CONNECTION_TIMEOUT_MS, ssid, passwd, 0, 0,
78                                has_bssid ? final_bssid : NULL, 0);
79 }
80 
81 #if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
82 }
83 #endif
84