Base Code merged to SPIN 2.4
[platform/core/connectivity/net-config.git] / src / wifi-eap.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2000 - 2012 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 "log.h"
21 #include "util.h"
22 #include "netdbus.h"
23 #include "neterror.h"
24 #include "wifi-tel-intf.h"
25 #include "network-state.h"
26 #include "wifi-eap.h"
27
28 #define SIM_RAND_DATA_LEN 16
29 #define SIM_AUTH_MAX_RESP_DATA_LEN 128
30 #define SIM_AUTH_SRES_LEN 4
31 #define SIM_AUTH_KC_LEN 8
32
33 #define AKA_RAND_DATA_LEN 16
34 #define AKA_AUTN_DATA_LEN 16
35 #define AKA_AUTH_RES_MAX_LEN 16
36 #define AKA_AUTH_RES_MIN_LEN 4
37 #define AKA_AUTH_CK_LEN 16
38 #define AKA_AUTH_IK_LEN 16
39
40 struct wifii_authentication_data {
41         int auth_result;
42         int resp_length;
43         int authentication_key_length;
44         int cipher_length;
45         int integrity_length;
46         char *resp_data;
47         char *authentication_key;
48         char *cipher_data;
49         char *integrity_data;
50 };
51
52 static struct wifii_authentication_data *wifi_authdata;
53
54 static void *__netconfig_wifi_free_wifi_authdata(
55                 struct wifii_authentication_data *data)
56 {
57         if (data != NULL) {
58                 if (data->resp_data)
59                         g_free(data->resp_data);
60                 if (data->authentication_key)
61                         g_free(data->authentication_key);
62                 if (data->cipher_data)
63                         g_free(data->cipher_data);
64                 if (data->integrity_data)
65                         g_free(data->integrity_data);
66
67                 g_free(data);
68                 data = NULL;
69         }
70
71         return NULL;
72 }
73
74 static void __netconfig_wifi_clean_authentication(void)
75 {
76         netconfig_tel_deinit();
77
78         wifi_authdata = __netconfig_wifi_free_wifi_authdata(wifi_authdata);
79 }
80
81 static gboolean __netconfig_wifi_get_sim_imsi(Wifi *wifi,
82                 GDBusMethodInvocation *context)
83 {
84         int ret;
85         TapiHandle *handle;
86         TelSimImsiInfo_t imsi_info;
87         char *imsi;
88
89         handle = (TapiHandle *)netconfig_tel_init();
90         if (handle == NULL) {
91                 ERR("tapi_init failed");
92                 netconfig_error_fail_get_imsi(context);
93                 return FALSE;
94         }
95
96         ERR("before tel_get_sim_imsi");
97         ret = tel_get_sim_imsi(handle, &imsi_info);
98         ERR("after tel_get_sim_imsi");
99         if (ret != TAPI_API_SUCCESS) {
100                 ERR("Failed tel_get_sim_imsi() : [%d]", ret);
101                 netconfig_error_fail_get_imsi(context);
102                 return FALSE;
103         }
104
105         imsi = g_strdup_printf("%s%s%s", imsi_info.szMcc,
106                         imsi_info.szMnc, imsi_info.szMsin);
107
108         wifi_complete_get_sim_imsi(wifi, context, imsi);
109         g_free(imsi);
110
111         return TRUE;
112 }
113
114 void __netconfig_response_sim_authentication(TapiHandle *handle,
115                 int result, void *data, void *user_data)
116 {
117         if (wifi_authdata != NULL)
118                 wifi_authdata = __netconfig_wifi_free_wifi_authdata(wifi_authdata);
119
120         wifi_authdata = g_try_new0(struct wifii_authentication_data, 1);
121
122         TelSimAuthenticationResponse_t *auth_resp =
123                                 (TelSimAuthenticationResponse_t *) data;
124         if (auth_resp == NULL) {
125                 ERR("the auth response is NULL");
126
127                 wifi_authdata->auth_result = -1;
128                 return;
129         } else
130                 wifi_authdata->auth_result = auth_resp->auth_result;
131
132         if (auth_resp->auth_result == TAPI_SIM_AUTH_NO_ERROR) {
133                 wifi_authdata->resp_length = auth_resp->resp_length;
134                 wifi_authdata->authentication_key_length =
135                                         auth_resp->authentication_key_length;
136
137                 if (wifi_authdata->resp_data != NULL)
138                         g_free(wifi_authdata->resp_data);
139
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
145                 wifi_authdata->authentication_key =
146                                                         g_strdup(auth_resp->authentication_key);
147         } else {
148                 ERR("the result error for sim auth : [%d]", auth_resp->auth_result);
149
150                 wifi_authdata->resp_length = 0;
151                 wifi_authdata->authentication_key_length = 0;
152         }
153 }
154
155 void __netconfig_response_aka_authentication(TapiHandle *handle,
156                 int result, void *data, void *user_data)
157 {
158         if (wifi_authdata != NULL)
159                 wifi_authdata = __netconfig_wifi_free_wifi_authdata(wifi_authdata);
160
161         wifi_authdata = g_try_new0(struct wifii_authentication_data, 1);
162
163         TelSimAuthenticationResponse_t *auth_resp =
164                                         (TelSimAuthenticationResponse_t *) data;
165         if (auth_resp == NULL) {
166                 ERR("the auth response is NULL");
167
168                 wifi_authdata->auth_result = -1;
169                 return;
170         } else
171                 wifi_authdata->auth_result = auth_resp->auth_result;
172
173         if (auth_resp->auth_result == TAPI_SIM_AUTH_NO_ERROR) {
174                 wifi_authdata->resp_length = auth_resp->resp_length;
175                 wifi_authdata->cipher_length = auth_resp->cipher_length;
176                 wifi_authdata->integrity_length = auth_resp->integrity_length;
177
178                 if (wifi_authdata->resp_data != NULL)
179                         g_free(wifi_authdata->resp_data);
180
181                 wifi_authdata->resp_data = g_strdup(auth_resp->resp_data);
182
183                 if (wifi_authdata->cipher_data != NULL)
184                         g_free(wifi_authdata->cipher_data);
185
186                 wifi_authdata->cipher_data = g_strdup(auth_resp->cipher_data);
187
188                 if (wifi_authdata->integrity_data != NULL)
189                         g_free(wifi_authdata->integrity_data);
190
191                 wifi_authdata->integrity_data = g_strdup(auth_resp->integrity_data);
192         } else {
193                 ERR("the result error for aka auth : [%d]", auth_resp->auth_result);
194
195                 if (auth_resp->auth_result == TAPI_SIM_AUTH_SQN_FAILURE ||
196                                         auth_resp->auth_result == TAPI_SIM_AUTH_SYNCH_FAILURE) {
197                         wifi_authdata->resp_length = auth_resp->resp_length;
198
199                         if (wifi_authdata->resp_data != NULL)
200                                 g_free(wifi_authdata->resp_data);
201
202                         wifi_authdata->resp_data = g_strdup(auth_resp->resp_data);
203                 }
204         }
205 }
206
207 static gboolean __netconfig_wifi_req_sim_auth(GArray *rand_data,
208                 GDBusMethodInvocation *context)
209 {
210         int i;
211         int ret;
212         TapiHandle *handle;
213         TelSimAuthenticationData_t auth_data;
214
215         if (rand_data == NULL)
216                 return FALSE;
217
218         if (rand_data->len != SIM_RAND_DATA_LEN) {
219                 ERR("wrong rand data len : [%d]", rand_data->len);
220
221                 netconfig_error_fail_req_sim_auth_wrong_param(context);
222                 return FALSE;
223         }
224
225         if ((ret = g_array_get_element_size(rand_data)) != 1) {
226                 ERR("wrong rand data size : [%d]", ret);
227
228                 netconfig_error_fail_req_sim_auth_wrong_param(context);
229                 return FALSE;
230         }
231
232         memset(&auth_data, 0, sizeof(auth_data));
233
234         auth_data.auth_type = TAPI_SIM_AUTH_TYPE_GSM;
235         auth_data.rand_length = SIM_RAND_DATA_LEN;
236
237         for (i=0; i<rand_data->len; i++)
238                 auth_data.rand_data[i] = g_array_index(rand_data, guint8, i);
239
240         handle = (TapiHandle *)netconfig_tel_init();
241         if (handle == NULL) {
242                 netconfig_error_fail_req_sim_auth(context);
243                 return FALSE;
244         }
245
246         ret = tel_req_sim_authentication(handle,
247                         &auth_data, __netconfig_response_sim_authentication, NULL);
248         if (ret != TAPI_API_SUCCESS) {
249                 ERR("Failed tel_req_sim_authentication() : [%d]", ret);
250
251                 netconfig_error_fail_req_sim_auth(context);
252                 return FALSE;
253         }
254
255         return TRUE;
256 }
257
258 static gboolean __netconfig_wifi_req_aka_auth(
259                 GArray *rand_data, GArray *autn_data, GDBusMethodInvocation *context)
260 {
261         int i;
262         int ret;
263         TapiHandle *handle;
264         TelSimAuthenticationData_t auth_data;
265
266         if (rand_data == NULL || autn_data == NULL)
267                 return FALSE;
268
269         if (rand_data->len != AKA_RAND_DATA_LEN) {
270                 ERR("wrong rand data len : [%d]", rand_data->len);
271
272                 netconfig_error_fail_req_sim_auth_wrong_param(context);
273                 return FALSE;
274         }
275
276         if (autn_data->len != AKA_AUTN_DATA_LEN) {
277                 ERR("wrong autn data len : [%d]", autn_data->len);
278
279                 netconfig_error_fail_req_sim_auth_wrong_param(context);
280                 return FALSE;
281         }
282
283         if ((ret = g_array_get_element_size(rand_data)) != 1) {
284                 ERR("wrong rand data size : [%d]", ret);
285
286                 netconfig_error_fail_req_sim_auth_wrong_param(context);
287                 return FALSE;
288         }
289
290         if ((ret = g_array_get_element_size(autn_data)) != 1) {
291                 ERR("wrong autn data size : [%d]", ret);
292
293                 netconfig_error_fail_req_sim_auth_wrong_param(context);
294                 return FALSE;
295         }
296
297         memset(&auth_data, 0, sizeof(auth_data));
298
299         auth_data.auth_type = TAPI_SIM_AUTH_TYPE_3G;
300         auth_data.rand_length = AKA_RAND_DATA_LEN;
301         auth_data.autn_length = AKA_AUTN_DATA_LEN;
302
303         for (i=0; i<rand_data->len; i++)
304                 auth_data.rand_data[i] = g_array_index(rand_data, guint8, i);
305
306         for (i=0; i<autn_data->len; i++)
307                 auth_data.autn_data[i] = g_array_index(autn_data, guint8, i);
308
309         handle = (TapiHandle *)netconfig_tel_init();
310         if (handle == NULL) {
311                 netconfig_error_fail_req_sim_auth(context);
312                 return FALSE;
313         }
314
315         ret = tel_req_sim_authentication(handle, &auth_data,
316                         __netconfig_response_aka_authentication, NULL);
317
318         if (ret != TAPI_API_SUCCESS) {
319                 ERR("Failed tel_req_sim_authentication() : [%d]", ret);
320
321                 netconfig_error_fail_req_sim_auth(context);
322                 return FALSE;
323         }
324         return TRUE;
325 }
326
327 static gboolean __netconfig_wifi_get_sim_authdata(Wifi *wifi,
328                 GDBusMethodInvocation *context)
329 {
330         GArray *array = NULL;
331
332         if (wifi_authdata == NULL) {
333                 DBG("the status error : no response yet");
334                 netconfig_error_fail_get_sim_auth_delay(context);
335                 return FALSE;
336         }
337
338         if (wifi_authdata->auth_result == TAPI_SIM_AUTH_NO_ERROR) {
339                 if (wifi_authdata->resp_length == SIM_AUTH_SRES_LEN &&
340                                 wifi_authdata->authentication_key_length == SIM_AUTH_KC_LEN) {
341                         array = g_array_sized_new(FALSE, FALSE, sizeof(guchar),
342                                         SIM_AUTH_SRES_LEN+SIM_AUTH_KC_LEN);
343                         g_array_append_vals(array, wifi_authdata->resp_data,
344                                         SIM_AUTH_SRES_LEN);
345                         g_array_append_vals(array, wifi_authdata->authentication_key,
346                                         SIM_AUTH_KC_LEN);
347                 } else {
348                         ERR("auth data length is wrong, SRES = [%d], Kc = [%d]",
349                                         wifi_authdata->resp_length,
350                                         wifi_authdata->authentication_key_length);
351                         netconfig_error_fail_get_sim_auth_wrong_data(context);
352                         __netconfig_wifi_clean_authentication();
353                         return FALSE;
354                 }
355         } else {
356                 ERR("failed auth result = [%d]", wifi_authdata->auth_result);
357                 netconfig_error_fail_get_sim_auth_wrong_data(context);
358                 __netconfig_wifi_clean_authentication();
359                 return FALSE;
360         }
361
362         wifi_complete_get_sim_auth(wifi, context, array->data);
363         g_array_free (array, TRUE);
364         __netconfig_wifi_clean_authentication();
365         return TRUE;
366 }
367
368 static gboolean __netconfig_wifi_get_aka_authdata(Wifi *wifi, GDBusMethodInvocation *context)
369 {
370         GArray *array = NULL;
371         guchar res_len;
372
373         if (wifi_authdata == NULL) {
374                 DBG("the status error : no response yet");
375                 netconfig_error_fail_get_sim_auth_delay(context);
376                 return FALSE;
377         }
378
379         switch (wifi_authdata->auth_result) {
380         case TAPI_SIM_AUTH_NO_ERROR:
381                  break;
382
383         case TAPI_SIM_AUTH_SQN_FAILURE:
384         case TAPI_SIM_AUTH_SYNCH_FAILURE:
385                 array = g_array_sized_new(FALSE, FALSE, sizeof(guchar),
386                                                                         wifi_authdata->resp_length+1);
387                 res_len = (guchar)((wifi_authdata->resp_length-1) & 0xff);
388
389                 g_array_append_vals(array, &res_len, 1);
390                 g_array_append_vals(array, wifi_authdata->resp_data,
391                                                                 wifi_authdata->resp_length);
392
393                 wifi_complete_get_aka_auth(wifi, context, array->data);
394                 g_array_free (array, TRUE);
395
396                 __netconfig_wifi_clean_authentication();
397
398                 return TRUE;
399
400         default:
401                 netconfig_error_fail_get_sim_auth_wrong_data(context);
402                 __netconfig_wifi_clean_authentication();
403                 return FALSE;
404         }
405
406         if ((wifi_authdata->resp_length >= AKA_AUTH_RES_MIN_LEN ||
407                         wifi_authdata->resp_length <= AKA_AUTH_RES_MAX_LEN) &&
408                         wifi_authdata->cipher_length == AKA_AUTH_CK_LEN &&
409                         wifi_authdata->integrity_length == AKA_AUTH_IK_LEN) {
410                 array = g_array_sized_new(FALSE, FALSE, sizeof(guchar),
411                                 wifi_authdata->resp_length+AKA_AUTH_CK_LEN+AKA_AUTH_IK_LEN+1);
412
413                 res_len = (guchar)((wifi_authdata->resp_length-1) & 0xff);
414                 g_array_append_vals(array, &res_len, 1);
415                 g_array_append_vals(array, wifi_authdata->resp_data,
416                                                                 wifi_authdata->resp_length);
417                 g_array_append_vals(array, wifi_authdata->cipher_data,
418                                                                 AKA_AUTH_CK_LEN);
419                 g_array_append_vals(array, wifi_authdata->integrity_data,
420                                                                 AKA_AUTH_IK_LEN);
421         } else {
422                 ERR("auth data length is wrong, res = [%d], Kc = [%d], Ki = [%d]",
423                                 wifi_authdata->resp_length, wifi_authdata->cipher_length,
424                                 wifi_authdata->integrity_length);
425
426                 netconfig_error_fail_get_sim_auth_wrong_data(context);
427                 __netconfig_wifi_clean_authentication();
428                 return FALSE;
429         }
430
431         wifi_complete_get_aka_auth(wifi, context, array->data);
432         g_array_free (array, TRUE);
433         __netconfig_wifi_clean_authentication();
434
435         return TRUE;
436 }
437
438 gboolean handle_get_sim_imsi(Wifi *wifi, GDBusMethodInvocation *context)
439 {
440         gboolean ret = TRUE;
441
442         DBG("Get IMSI");
443
444         g_return_val_if_fail(wifi != NULL, FALSE);
445
446         ret = __netconfig_wifi_get_sim_imsi(wifi, context);
447
448         return ret;
449 }
450
451 gboolean handle_req_sim_auth(Wifi *wifi, GDBusMethodInvocation *context,
452                 const gchar *rand_data)
453 {
454         gboolean result = TRUE;
455
456         DBG("Request SIM Authentication");
457
458         g_return_val_if_fail(wifi != NULL, FALSE);
459
460         result = __netconfig_wifi_req_sim_auth((GArray *)rand_data, context);
461
462         if (result)
463                 wifi_complete_req_sim_auth(wifi, context, result);
464
465         return result;
466 }
467
468 gboolean handle_req_aka_auth(Wifi *wifi, GDBusMethodInvocation *context,
469                 const gchar *rand_data, const gchar *autn_data)
470 {
471         gboolean result = TRUE;
472         GArray *rand_data_garray;
473         GArray *autn_data_garray;
474
475         rand_data_garray = g_array_sized_new(FALSE, FALSE, sizeof(guchar),
476                         strlen(rand_data));
477         memcpy(rand_data_garray->data, rand_data, rand_data_garray->len);
478
479         autn_data_garray = g_array_sized_new(FALSE, FALSE, sizeof(guchar),
480                         strlen(autn_data));
481         memcpy(autn_data_garray->data, rand_data, autn_data_garray->len);
482
483         DBG("Request AKA Authentication");
484
485         g_return_val_if_fail(wifi != NULL, FALSE);
486
487         result = __netconfig_wifi_req_aka_auth(rand_data_garray, autn_data_garray, context);
488         if (result) {
489                 wifi_complete_req_aka_auth(wifi, context, result);
490         } else {
491                 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_FAILED_REQ_AKA_AUTH, "FailReqAkaAuth");
492         }
493
494         g_array_free(rand_data_garray, FALSE);
495         g_array_free(autn_data_garray, FALSE);
496
497         return result;
498 }
499
500 gboolean handle_get_sim_auth(Wifi *wifi, GDBusMethodInvocation *context)
501 {
502         gboolean ret = TRUE;
503
504         DBG("Get SIM Authdata");
505
506         g_return_val_if_fail(wifi != NULL, FALSE);
507
508         ret = __netconfig_wifi_get_sim_authdata(wifi, context);
509         return ret;
510 }
511
512 gboolean handle_get_aka_auth(Wifi *wifi, GDBusMethodInvocation *context)
513 {
514         gboolean ret = TRUE;
515
516         DBG("Get AKA Authdata");
517
518         g_return_val_if_fail(wifi != NULL, FALSE);
519
520         ret = __netconfig_wifi_get_aka_authdata(wifi, context);
521
522         return ret;
523 }