Code refactored to replace WPS with BSSID scan
[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 "network-state.h"
25 #include "wifi-eap.h"
26 #include "wifi-power.h"
27
28 static struct wifi_authentication_data *wifi_authdata;
29
30 gboolean handle_get_sim_imsi(Wifi *wifi, GDBusMethodInvocation *context)
31 {
32         gboolean ret = TRUE;
33
34         DBG("Get IMSI");
35
36         g_return_val_if_fail(wifi != NULL, FALSE);
37
38         ret = __netconfig_wifi_get_sim_imsi(wifi, context);
39
40         return ret;
41 }
42
43 gboolean handle_req_sim_auth(Wifi *wifi, GDBusMethodInvocation *context, GVariant *rand_data)
44 {
45         gboolean result = TRUE;
46         GArray *rand_data_garray;
47         GVariantIter *iter;
48         gint length;
49         guchar *out_auth_data;
50         guchar byte;
51         int i = 0;
52
53         DBG("Request SIM Authentication");
54
55         g_return_val_if_fail(wifi != NULL, FALSE);
56
57         g_variant_get(rand_data, "ay", &iter);
58         length = g_variant_iter_n_children(iter);
59         out_auth_data = g_new0(guchar, length);
60
61         while (g_variant_iter_loop(iter, "y", &byte)) {
62                 *(out_auth_data + i) = byte;
63                 i++;
64         }
65         g_variant_iter_free(iter);
66
67         rand_data_garray = g_array_sized_new(FALSE, FALSE, sizeof(guchar), length);
68         memcpy(rand_data_garray->data, out_auth_data, length);
69         g_free(out_auth_data);
70         rand_data_garray->len = length;
71
72         result = __netconfig_wifi_req_sim_auth(rand_data_garray, context, &wifi_authdata);
73         g_array_free(rand_data_garray, FALSE);
74
75         if (result)
76                 wifi_complete_req_sim_auth(wifi, context, result);
77
78         return result;
79 }
80
81 gboolean handle_req_aka_auth(Wifi *wifi, GDBusMethodInvocation *context, GVariant *rand_data, GVariant *autn_data)
82 {
83         netconfig_error_e ret = NETCONFIG_NO_ERROR;
84         gboolean result = FALSE;
85         GVariantIter *iter;
86         gint length;
87         guchar *out_auth_data;
88         guchar byte;
89         int i = 0;
90         GArray *rand_data_garray;
91         GArray *autn_data_garray;
92
93         DBG("Request AKA Authentication");
94
95         g_return_val_if_fail(wifi != NULL, FALSE);
96
97         g_variant_get(rand_data, "ay", &iter);
98         length = g_variant_iter_n_children(iter);
99         out_auth_data = g_new0(guchar, length);
100         while (g_variant_iter_loop(iter, "y", &byte)) {
101                 *(out_auth_data + i) = byte;
102                 i++;
103         }
104         g_variant_iter_free(iter);
105
106         rand_data_garray = g_array_sized_new(FALSE, FALSE, sizeof(guchar), length);
107         memcpy(rand_data_garray->data, out_auth_data, length);
108         g_free(out_auth_data);
109         rand_data_garray->len = length;
110
111         i = 0;
112         g_variant_get(autn_data, "ay", &iter);
113         length = g_variant_iter_n_children(iter);
114         out_auth_data = g_new0(guchar, length);
115         while (g_variant_iter_loop(iter, "y", &byte)) {
116                 *(out_auth_data + i) = byte;
117                 i++;
118         }
119         g_variant_iter_free(iter);
120
121         autn_data_garray = g_array_sized_new(FALSE, FALSE, sizeof(guchar), length);
122         memcpy(autn_data_garray->data, out_auth_data, length);
123         g_free(out_auth_data);
124         autn_data_garray->len = length;
125
126         ret = __netconfig_wifi_req_aka_auth(rand_data_garray, autn_data_garray, context, &wifi_authdata);
127         if (ret == NETCONFIG_NO_ERROR) {
128                 result = TRUE;
129                 wifi_complete_req_aka_auth(wifi, context, result);
130         } else if (ret == NETCONFIG_ERROR_FAILED_REQ_SIM_AUTH_WRONG_PARAM) {
131                 netconfig_error_dbus_method_return(context,
132                                 NETCONFIG_ERROR_FAILED_REQ_SIM_AUTH_WRONG_PARAM, "FailReqSimAuthWrongParam");
133         } else {
134                 netconfig_error_dbus_method_return(context,
135                                 NETCONFIG_ERROR_FAILED_REQ_SIM_AUTH, "FailReqSimAuth");
136         }
137
138         g_array_free(rand_data_garray, FALSE);
139         g_array_free(autn_data_garray, FALSE);
140
141         return result;
142 }
143
144 gboolean handle_get_sim_auth(Wifi *wifi, GDBusMethodInvocation *context)
145 {
146         gboolean ret = TRUE;
147
148         DBG("Get SIM Authdata");
149
150         g_return_val_if_fail(wifi != NULL, FALSE);
151
152         ret = __netconfig_wifi_get_sim_authdata(wifi, context, &wifi_authdata);
153         return ret;
154 }
155
156 gboolean handle_get_aka_auth(Wifi *wifi, GDBusMethodInvocation *context)
157 {
158         gboolean ret = TRUE;
159
160         DBG("Get AKA Authdata");
161
162         g_return_val_if_fail(wifi != NULL, FALSE);
163
164         ret = __netconfig_wifi_get_aka_authdata(wifi, context, &wifi_authdata);
165
166         return ret;
167 }
168
169 EXPORT_SYM void netconfig_complete_get_sim_imsi(void *wifi, GDBusMethodInvocation *context, char *imsi)
170 {
171         wifi_complete_get_sim_imsi((Wifi*)wifi, context, imsi);
172 }
173
174 EXPORT_SYM void netconfig_complete_get_aka_auth(void *wifi, GDBusMethodInvocation *context, GArray *array)
175 {
176         wifi_complete_get_aka_auth((Wifi*)wifi, context, array->data);
177 }
178
179 EXPORT_SYM void netconfig_complete_get_sim_auth(void *wifi, GDBusMethodInvocation *context, GArray *array)
180 {
181         wifi_complete_get_sim_auth((Wifi*)wifi, context, array->data);
182 }
183
184 EXPORT_SYM void netconfig_wifi_power_on()
185 {
186 #if defined TIZEN_WEARABLE
187         wifi_power_on_wearable(TRUE);
188 #else
189         wifi_power_on();
190 #endif
191 }