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