Remove syspopup
[platform/core/connectivity/net-config.git] / src / wifi-eap.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <tapi_common.h>
21 #include <TapiUtility.h>
22 #include <ITapiSim.h>
23
24 #include "log.h"
25 #include "util.h"
26 #include "netdbus.h"
27 #include "neterror.h"
28
29 #define SIM_RAND_DATA_LEN 16
30 #define SIM_AUTH_MAX_RESP_DATA_LEN 128
31 #define SIM_AUTH_SRES_LEN 4
32 #define SIM_AUTH_KC_LEN 8
33
34 #define AKA_RAND_DATA_LEN 16
35 #define AKA_AUTN_DATA_LEN 16
36 #define AKA_AUTH_RES_MAX_LEN 16
37 #define AKA_AUTH_RES_MIN_LEN 4
38 #define AKA_AUTH_CK_LEN 16
39 #define AKA_AUTH_IK_LEN 16
40
41 struct wifii_authentication_data {
42         int auth_result;
43         int resp_length;
44         int authentication_key_length;
45         int cipher_length;
46         int integrity_length;
47         char *resp_data;
48         char *authentication_key;
49         char *cipher_data;
50         char *integrity_data;
51 };
52
53 TapiHandle *tapi_handle = NULL;
54 static struct wifii_authentication_data *wifi_authdata;
55
56 static void *__netconfig_wifi_free_wifi_authdata(struct wifii_authentication_data *data)
57 {
58         if (data) {
59                 if (data->resp_data)
60                         g_free(data->resp_data);
61                 if (data->authentication_key)
62                         g_free(data->authentication_key);
63                 if (data->cipher_data)
64                         g_free(data->cipher_data);
65                 if (data->integrity_data)
66                         g_free(data->integrity_data);
67
68                 g_free(data);
69                 data = NULL;
70         }
71
72         return NULL;
73 }
74
75 static void __netconfig_tapi_init()
76 {
77         tapi_handle = tel_init(NULL);
78 }
79
80 static void __netconfig_tapi_deinit()
81 {
82         tel_deinit(tapi_handle);
83         tapi_handle = NULL;
84
85         wifi_authdata = __netconfig_wifi_free_wifi_authdata(wifi_authdata);
86 }
87
88 static gboolean __netconfig_wifi_get_sim_imsi(DBusGMethodInvocation *context)
89 {
90         DBG(" ");
91
92         int ret;
93         GError *error = NULL;
94         TelSimImsiInfo_t imsi_info;
95         char *imsi;
96
97         if (tapi_handle == NULL)
98                 __netconfig_tapi_init();
99
100         ret = tel_get_sim_imsi(tapi_handle, &imsi_info);
101         if (ret != TAPI_API_SUCCESS) {
102                 ERR("Failed tel_get_sim_imsi() : [%d]", ret);
103                 netconfig_error_fail_get_imsi(&error);
104                 dbus_g_method_return_error(context, error);
105                 return FALSE;
106         }
107
108         imsi = g_strdup_printf("%s%s%s", imsi_info.szMcc, imsi_info.szMnc, imsi_info.szMsin);
109
110         dbus_g_method_return(context, imsi);
111         g_free(imsi);
112
113         return TRUE;
114 }
115
116 void __netconfig_response_sim_authentication(TapiHandle *handle, int result, void *data, void *user_data)
117 {
118         DBG(" ");
119
120         if (wifi_authdata != NULL)
121                 wifi_authdata = __netconfig_wifi_free_wifi_authdata(wifi_authdata);
122
123         wifi_authdata = g_try_new0(struct wifii_authentication_data, 1);
124
125         TelSimAuthenticationResponse_t *auth_resp = (TelSimAuthenticationResponse_t *) data;
126         if (auth_resp == NULL) {
127                 ERR("the auth response is NULL");
128                 wifi_authdata->auth_result = -1;
129                 return;
130         } else {
131                 wifi_authdata->auth_result = auth_resp->auth_result;
132         }
133
134         if (auth_resp->auth_result == TAPI_SIM_AUTH_NO_ERROR) {
135                 wifi_authdata->resp_length = auth_resp->resp_length;
136                 wifi_authdata->authentication_key_length = auth_resp->authentication_key_length;
137
138                 if (wifi_authdata->resp_data != NULL)
139                         g_free(wifi_authdata->resp_data);
140                 wifi_authdata->resp_data = g_strdup(auth_resp->resp_data);
141
142                 if (wifi_authdata->authentication_key != NULL)
143                         g_free(wifi_authdata->authentication_key);
144                 wifi_authdata->authentication_key = g_strdup(auth_resp->authentication_key);
145         } else {
146                 ERR("the result error for sim auth : [%d]", auth_resp->auth_result);
147                 wifi_authdata->resp_length = 0;
148                 wifi_authdata->authentication_key_length = 0;
149         }
150 }
151
152 void __netconfig_response_aka_authentication(TapiHandle *handle, int result, void *data, void *user_data)
153 {
154         DBG(" ");
155
156         if (wifi_authdata != NULL)
157                 wifi_authdata = __netconfig_wifi_free_wifi_authdata(wifi_authdata);
158
159         wifi_authdata = g_try_new0(struct wifii_authentication_data, 1);
160
161         TelSimAuthenticationResponse_t *auth_resp = (TelSimAuthenticationResponse_t *) data;
162         if (auth_resp == NULL) {
163                 ERR("the auth response is NULL");
164                 wifi_authdata->auth_result = -1;
165                 return;
166         } else {
167                 wifi_authdata->auth_result = auth_resp->auth_result;
168         }
169
170         if (auth_resp->auth_result == TAPI_SIM_AUTH_NO_ERROR) {
171                 wifi_authdata->resp_length = auth_resp->resp_length;
172                 wifi_authdata->cipher_length = auth_resp->cipher_length;
173                 wifi_authdata->integrity_length = auth_resp->integrity_length;
174
175                 if (wifi_authdata->resp_data != NULL)
176                         g_free(wifi_authdata->resp_data);
177                 wifi_authdata->resp_data = g_strdup(auth_resp->resp_data);
178
179                 if (wifi_authdata->cipher_data != NULL)
180                         g_free(wifi_authdata->cipher_data);
181                 wifi_authdata->cipher_data = g_strdup(auth_resp->cipher_data);
182
183                 if (wifi_authdata->integrity_data != NULL)
184                         g_free(wifi_authdata->integrity_data);
185                 wifi_authdata->integrity_data = g_strdup(auth_resp->integrity_data);
186         } else {
187                 ERR("the result error for aka auth : [%d]", auth_resp->auth_result);
188                 if (auth_resp->auth_result == TAPI_SIM_AUTH_SQN_FAILURE ||
189                                         auth_resp->auth_result == TAPI_SIM_AUTH_SYNCH_FAILURE) {
190                         wifi_authdata->resp_length = auth_resp->resp_length;
191
192                         if (wifi_authdata->resp_data != NULL)
193                                 g_free(wifi_authdata->resp_data);
194                         wifi_authdata->resp_data = g_strdup(auth_resp->resp_data);
195                 }
196         }
197 }
198
199 static gboolean __netconfig_wifi_req_sim_auth(GArray *rand_data, GError **error)
200 {
201         DBG(" ");
202
203         int i;
204         int ret;
205         TelSimAuthenticationData_t auth_data;
206
207         if (rand_data->len != SIM_RAND_DATA_LEN) {
208                 ERR("wrong rand data len : [%d]", rand_data->len);
209                 netconfig_error_fail_req_sim_auth_wrong_param(error);
210                 return FALSE;
211         }
212
213         if ((ret = g_array_get_element_size(rand_data)) != 1) {
214                 ERR("wrong rand data size : [%d]", ret);
215                 netconfig_error_fail_req_sim_auth_wrong_param(error);
216                 return FALSE;
217         }
218
219         memset(&auth_data, 0, sizeof(auth_data));
220         auth_data.auth_type = TAPI_SIM_AUTH_TYPE_GSM;
221         auth_data.rand_length = SIM_RAND_DATA_LEN;
222         for (i=0; i<rand_data->len; i++)
223                 auth_data.rand_data[i] = g_array_index(rand_data, guint8, i);
224
225         if (tapi_handle == NULL)
226                 __netconfig_tapi_init();
227
228         ret = tel_req_sim_authentication(tapi_handle, &auth_data, __netconfig_response_sim_authentication, NULL);
229         if (ret != TAPI_API_SUCCESS) {
230                 ERR("Failed tel_req_sim_authentication() : [%d]", ret);
231                 netconfig_error_fail_req_sim_auth(error);
232                 return FALSE;
233         }
234
235         return TRUE;
236 }
237
238 static gboolean __netconfig_wifi_req_aka_auth(GArray *rand_data, GArray *autn_data, GError **error)
239 {
240         DBG(" ");
241
242         int i;
243         int ret;
244         TelSimAuthenticationData_t auth_data;
245
246         if (rand_data->len != AKA_RAND_DATA_LEN) {
247                 ERR("wrong rand data len : [%d]", rand_data->len);
248                 netconfig_error_fail_req_sim_auth_wrong_param(error);
249                 return FALSE;
250         }
251
252         if (autn_data->len != AKA_AUTN_DATA_LEN) {
253                 ERR("wrong autn data len : [%d]", autn_data->len);
254                 netconfig_error_fail_req_sim_auth_wrong_param(error);
255                 return FALSE;
256         }
257
258         if ((ret = g_array_get_element_size(rand_data)) != 1) {
259                 ERR("wrong rand data size : [%d]", ret);
260                 netconfig_error_fail_req_sim_auth_wrong_param(error);
261                 return FALSE;
262         }
263
264         if ((ret = g_array_get_element_size(autn_data)) != 1) {
265                 ERR("wrong autn data size : [%d]", ret);
266                 netconfig_error_fail_req_sim_auth_wrong_param(error);
267                 return FALSE;
268         }
269
270         memset(&auth_data, 0, sizeof(auth_data));
271         auth_data.auth_type = TAPI_SIM_AUTH_TYPE_3G;
272         auth_data.rand_length = AKA_RAND_DATA_LEN;
273         auth_data.autn_length = AKA_AUTN_DATA_LEN;
274         for (i=0; i<rand_data->len; i++)
275                 auth_data.rand_data[i] = g_array_index(rand_data, guint8, i);
276         for (i=0; i<autn_data->len; i++)
277                 auth_data.autn_data[i] = g_array_index(autn_data, guint8, i);
278
279         if (tapi_handle == NULL)
280                 __netconfig_tapi_init();
281
282         ret = tel_req_sim_authentication(tapi_handle, &auth_data, __netconfig_response_aka_authentication, NULL);
283         if (ret != TAPI_API_SUCCESS) {
284                 ERR("Failed tel_req_sim_authentication() : [%d]", ret);
285                 netconfig_error_fail_req_sim_auth(error);
286                 return FALSE;
287         }
288
289         return TRUE;
290 }
291
292 static gboolean __netconfig_wifi_get_sim_authdata(DBusGMethodInvocation *context)
293 {
294         DBG(" ");
295
296         GArray *array = NULL;
297         GError *error = NULL;
298
299         if (wifi_authdata == NULL) {
300                 DBG("the status error : no response yet");
301                 netconfig_error_fail_get_sim_auth_delay(&error);
302                 dbus_g_method_return_error(context, error);
303                 return FALSE;
304         }
305
306         if (wifi_authdata->auth_result == TAPI_SIM_AUTH_NO_ERROR) {
307                 if (wifi_authdata->resp_length == SIM_AUTH_SRES_LEN &&
308                                 wifi_authdata->authentication_key_length == SIM_AUTH_KC_LEN) {
309                         array = g_array_sized_new(FALSE, FALSE, sizeof(guchar), SIM_AUTH_SRES_LEN+SIM_AUTH_KC_LEN);
310                         g_array_append_vals(array, wifi_authdata->resp_data, SIM_AUTH_SRES_LEN);
311                         g_array_append_vals(array, wifi_authdata->authentication_key, SIM_AUTH_KC_LEN);
312                         dbus_g_method_return(context, array);
313                         g_array_free (array, TRUE);
314                 } else {
315                         ERR("auth data length is wrong, SRES = [%d], Kc = [%d]",
316                                         wifi_authdata->resp_length, wifi_authdata->authentication_key_length);
317
318                         netconfig_error_fail_get_sim_auth_wrong_data(&error);
319                         dbus_g_method_return_error(context, error);
320                         __netconfig_tapi_deinit();
321                         return FALSE;
322                 }
323         } else {
324                 ERR("failed auth result = [%d]", wifi_authdata->auth_result);
325                 netconfig_error_fail_get_sim_auth_wrong_data(&error);
326                 dbus_g_method_return_error(context, error);
327                 __netconfig_tapi_deinit();
328                 return FALSE;
329         }
330
331         __netconfig_tapi_deinit();
332         return TRUE;
333 }
334
335 static gboolean __netconfig_wifi_get_aka_authdata(DBusGMethodInvocation *context)
336 {
337         DBG(" ");
338
339         GArray *array = NULL;
340         GError *error = NULL;
341         guchar res_len;
342
343         if (wifi_authdata == NULL) {
344                 DBG("the status error : no response yet");
345                 netconfig_error_fail_get_sim_auth_delay(&error);
346                 dbus_g_method_return_error(context, error);
347                 return FALSE;
348         }
349
350         switch (wifi_authdata->auth_result) {
351         case TAPI_SIM_AUTH_NO_ERROR:
352                  break;
353
354         case TAPI_SIM_AUTH_SQN_FAILURE:
355         case TAPI_SIM_AUTH_SYNCH_FAILURE:
356                 array = g_array_sized_new(FALSE, FALSE, sizeof(guchar), wifi_authdata->resp_length+1);
357                 res_len = (guchar)((wifi_authdata->resp_length-1) & 0xff);
358
359                 g_array_append_vals(array, &res_len, 1);
360                 g_array_append_vals(array, wifi_authdata->resp_data, wifi_authdata->resp_length);
361
362                 dbus_g_method_return(context, array);
363                 g_array_free (array, TRUE);
364
365                 g_free(wifi_authdata->resp_data);
366                 g_free(wifi_authdata);
367                 wifi_authdata = NULL;
368
369                 return TRUE;
370
371         default:
372                 netconfig_error_fail_get_sim_auth_wrong_data(&error);
373                 dbus_g_method_return_error(context, error);
374                 __netconfig_tapi_deinit();
375                 return FALSE;
376         }
377
378         if ((wifi_authdata->resp_length >= AKA_AUTH_RES_MIN_LEN ||
379                         wifi_authdata->resp_length <= AKA_AUTH_RES_MAX_LEN) &&
380                         wifi_authdata->cipher_length == AKA_AUTH_CK_LEN &&
381                         wifi_authdata->integrity_length == AKA_AUTH_IK_LEN) {
382                 array = g_array_sized_new(FALSE, FALSE, sizeof(guchar), wifi_authdata->resp_length+AKA_AUTH_CK_LEN+AKA_AUTH_IK_LEN+1);
383
384                 res_len = (guchar)((wifi_authdata->resp_length-1) & 0xff);
385                 g_array_append_vals(array, &res_len, 1);
386                 g_array_append_vals(array, wifi_authdata->resp_data, wifi_authdata->resp_length);
387                 g_array_append_vals(array, wifi_authdata->cipher_data, AKA_AUTH_CK_LEN);
388                 g_array_append_vals(array, wifi_authdata->integrity_data, AKA_AUTH_IK_LEN);
389
390                 dbus_g_method_return(context, array);
391                 g_array_free (array, TRUE);
392         } else {
393                 ERR("auth data length is wrong, res = [%d], Kc = [%d], Ki = [%d]",
394                                 wifi_authdata->resp_length, wifi_authdata->cipher_length, wifi_authdata->integrity_length);
395
396                 netconfig_error_fail_get_sim_auth_wrong_data(&error);
397                 dbus_g_method_return_error(context, error);
398                 __netconfig_tapi_deinit();
399                 return FALSE;
400         }
401
402         __netconfig_tapi_deinit();
403         return TRUE;
404 }
405
406 gboolean netconfig_iface_wifi_get_sim_imsi(NetconfigWifi *wifi, DBusGMethodInvocation *context)
407 {
408         gboolean ret = TRUE;
409
410         DBG("Get sim Imsi");
411
412         g_return_val_if_fail(wifi != NULL, FALSE);
413
414         ret = __netconfig_wifi_get_sim_imsi(context);
415
416         return ret;
417 }
418
419 gboolean netconfig_iface_wifi_req_sim_auth(NetconfigWifi *wifi, GArray *rand_data, gboolean *result, GError **error)
420 {
421         gboolean ret = TRUE;
422
423         DBG("Req sim Authentication");
424
425         g_return_val_if_fail(wifi != NULL, FALSE);
426
427         ret = __netconfig_wifi_req_sim_auth(rand_data, error);
428
429         *result = ret;
430         return ret;
431 }
432
433 gboolean netconfig_iface_wifi_req_aka_auth(NetconfigWifi *wifi, GArray *rand_data, GArray *autn_data, gboolean *result, GError **error)
434 {
435         gboolean ret = TRUE;
436
437         DBG("Req aka Authentication");
438
439         g_return_val_if_fail(wifi != NULL, FALSE);
440
441         ret = __netconfig_wifi_req_aka_auth(rand_data, autn_data, error);
442
443         *result = ret;
444         return ret;
445 }
446
447 gboolean netconfig_iface_wifi_get_sim_auth(NetconfigWifi *wifi, DBusGMethodInvocation *context)
448 {
449         gboolean ret = TRUE;
450
451         DBG("Get sim Authdata");
452
453         g_return_val_if_fail(wifi != NULL, FALSE);
454
455         ret = __netconfig_wifi_get_sim_authdata(context);
456
457         return ret;
458 }
459
460 gboolean netconfig_iface_wifi_get_aka_auth(NetconfigWifi *wifi, DBusGMethodInvocation *context)
461 {
462         gboolean ret = TRUE;
463
464         DBG("Get aka Authdata");
465
466         g_return_val_if_fail(wifi != NULL, FALSE);
467
468         ret = __netconfig_wifi_get_aka_authdata(context);
469
470         return ret;
471 }