2 * Network Configuration Module
4 * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <tapi_common.h>
21 #include <TapiUtility.h>
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
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
41 struct wifii_authentication_data {
44 int authentication_key_length;
48 char *authentication_key;
53 TapiHandle *tapi_handle = NULL;
54 static struct wifii_authentication_data *wifi_authdata;
56 static void *__netconfig_wifi_free_wifi_authdata(struct wifii_authentication_data *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);
75 static void __netconfig_tapi_init()
77 tapi_handle = tel_init(NULL);
80 static void __netconfig_tapi_deinit()
82 tel_deinit(tapi_handle);
85 wifi_authdata = __netconfig_wifi_free_wifi_authdata(wifi_authdata);
88 static gboolean __netconfig_wifi_get_sim_imsi(DBusGMethodInvocation *context)
94 TelSimImsiInfo_t imsi_info;
97 if (tapi_handle == NULL)
98 __netconfig_tapi_init();
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);
108 imsi = g_strdup_printf("%s%s%s", imsi_info.szMcc, imsi_info.szMnc, imsi_info.szMsin);
110 dbus_g_method_return(context, imsi);
116 void __netconfig_response_sim_authentication(TapiHandle *handle, int result, void *data, void *user_data)
120 if (wifi_authdata != NULL)
121 wifi_authdata = __netconfig_wifi_free_wifi_authdata(wifi_authdata);
123 wifi_authdata = g_try_new0(struct wifii_authentication_data, 1);
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;
131 wifi_authdata->auth_result = auth_resp->auth_result;
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;
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);
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);
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;
152 void __netconfig_response_aka_authentication(TapiHandle *handle, int result, void *data, void *user_data)
156 if (wifi_authdata != NULL)
157 wifi_authdata = __netconfig_wifi_free_wifi_authdata(wifi_authdata);
159 wifi_authdata = g_try_new0(struct wifii_authentication_data, 1);
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;
167 wifi_authdata->auth_result = auth_resp->auth_result;
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;
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);
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);
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);
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;
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);
199 static gboolean __netconfig_wifi_req_sim_auth(GArray *rand_data, GError **error)
205 TelSimAuthenticationData_t auth_data;
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);
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);
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);
225 if (tapi_handle == NULL)
226 __netconfig_tapi_init();
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);
238 static gboolean __netconfig_wifi_req_aka_auth(GArray *rand_data, GArray *autn_data, GError **error)
244 TelSimAuthenticationData_t auth_data;
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);
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);
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);
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);
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);
279 if (tapi_handle == NULL)
280 __netconfig_tapi_init();
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);
292 static gboolean __netconfig_wifi_get_sim_authdata(DBusGMethodInvocation *context)
296 GArray *array = NULL;
297 GError *error = NULL;
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);
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);
315 ERR("auth data length is wrong, SRES = [%d], Kc = [%d]",
316 wifi_authdata->resp_length, wifi_authdata->authentication_key_length);
318 netconfig_error_fail_get_sim_auth_wrong_data(&error);
319 dbus_g_method_return_error(context, error);
320 __netconfig_tapi_deinit();
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();
331 __netconfig_tapi_deinit();
335 static gboolean __netconfig_wifi_get_aka_authdata(DBusGMethodInvocation *context)
339 GArray *array = NULL;
340 GError *error = NULL;
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);
350 switch (wifi_authdata->auth_result) {
351 case TAPI_SIM_AUTH_NO_ERROR:
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);
359 g_array_append_vals(array, &res_len, 1);
360 g_array_append_vals(array, wifi_authdata->resp_data, wifi_authdata->resp_length);
362 dbus_g_method_return(context, array);
363 g_array_free (array, TRUE);
365 g_free(wifi_authdata->resp_data);
366 g_free(wifi_authdata);
367 wifi_authdata = NULL;
372 netconfig_error_fail_get_sim_auth_wrong_data(&error);
373 dbus_g_method_return_error(context, error);
374 __netconfig_tapi_deinit();
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);
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);
390 dbus_g_method_return(context, array);
391 g_array_free (array, TRUE);
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);
396 netconfig_error_fail_get_sim_auth_wrong_data(&error);
397 dbus_g_method_return_error(context, error);
398 __netconfig_tapi_deinit();
402 __netconfig_tapi_deinit();
406 gboolean netconfig_iface_wifi_get_sim_imsi(NetconfigWifi *wifi, DBusGMethodInvocation *context)
412 g_return_val_if_fail(wifi != NULL, FALSE);
414 ret = __netconfig_wifi_get_sim_imsi(context);
419 gboolean netconfig_iface_wifi_req_sim_auth(NetconfigWifi *wifi, GArray *rand_data, gboolean *result, GError **error)
423 DBG("Req sim Authentication");
425 g_return_val_if_fail(wifi != NULL, FALSE);
427 ret = __netconfig_wifi_req_sim_auth(rand_data, error);
433 gboolean netconfig_iface_wifi_req_aka_auth(NetconfigWifi *wifi, GArray *rand_data, GArray *autn_data, gboolean *result, GError **error)
437 DBG("Req aka Authentication");
439 g_return_val_if_fail(wifi != NULL, FALSE);
441 ret = __netconfig_wifi_req_aka_auth(rand_data, autn_data, error);
447 gboolean netconfig_iface_wifi_get_sim_auth(NetconfigWifi *wifi, DBusGMethodInvocation *context)
451 DBG("Get sim Authdata");
453 g_return_val_if_fail(wifi != NULL, FALSE);
455 ret = __netconfig_wifi_get_sim_authdata(context);
460 gboolean netconfig_iface_wifi_get_aka_auth(NetconfigWifi *wifi, DBusGMethodInvocation *context)
464 DBG("Get aka Authdata");
466 g_return_val_if_fail(wifi != NULL, FALSE);
468 ret = __netconfig_wifi_get_aka_authdata(context);