WPS: Set Request Type properly into WPS IE in ProbeReq/AssocReq
[mech_eap.git] / src / wps / wps.c
1 /*
2  * Wi-Fi Protected Setup
3  * Copyright (c) 2007-2008, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16
17 #include "common.h"
18 #include "wps_i.h"
19 #include "wps_dev_attr.h"
20 #include "ieee802_11_defs.h"
21
22
23 struct wps_data * wps_init(const struct wps_config *cfg)
24 {
25         struct wps_data *data = os_zalloc(sizeof(*data));
26         if (data == NULL)
27                 return NULL;
28         data->authenticator = cfg->authenticator;
29         data->wps = cfg->wps;
30         data->registrar = cfg->registrar;
31         if (cfg->enrollee_mac_addr)
32                 os_memcpy(data->mac_addr_e, cfg->enrollee_mac_addr, ETH_ALEN);
33         if (cfg->uuid) {
34                 os_memcpy(cfg->registrar ? data->uuid_r : data->uuid_e,
35                           cfg->uuid, WPS_UUID_LEN);
36         }
37         if (cfg->pin) {
38                 data->dev_pw_id = DEV_PW_DEFAULT;
39                 data->dev_password = os_malloc(cfg->pin_len);
40                 if (data->dev_password == NULL) {
41                         os_free(data);
42                         return NULL;
43                 }
44                 os_memcpy(data->dev_password, cfg->pin, cfg->pin_len);
45                 data->dev_password_len = cfg->pin_len;
46         }
47
48         data->pbc = cfg->pbc;
49         if (cfg->pbc) {
50                 /* Use special PIN '00000000' for PBC */
51                 data->dev_pw_id = DEV_PW_PUSHBUTTON;
52                 os_free(data->dev_password);
53                 data->dev_password = os_malloc(8);
54                 if (data->dev_password == NULL) {
55                         os_free(data);
56                         return NULL;
57                 }
58                 os_memset(data->dev_password, '0', 8);
59                 data->dev_password_len = 8;
60         }
61
62         data->state = data->registrar ? RECV_M1 : SEND_M1;
63
64         if (cfg->assoc_wps_ie) {
65                 struct wps_parse_attr attr;
66                 wpa_hexdump_buf(MSG_DEBUG, "WPS: WPS IE from (Re)AssocReq",
67                                 cfg->assoc_wps_ie);
68                 if (wps_parse_msg(cfg->assoc_wps_ie, &attr) < 0) {
69                         wpa_printf(MSG_DEBUG, "WPS: Failed to parse WPS IE "
70                                    "from (Re)AssocReq");
71                 } else if (attr.request_type == NULL) {
72                         wpa_printf(MSG_DEBUG, "WPS: No Request Type attribute "
73                                    "in (Re)AssocReq WPS IE");
74                 } else {
75                         wpa_printf(MSG_DEBUG, "WPS: Request Type (from WPS IE "
76                                    "in (Re)AssocReq WPS IE): %d",
77                                    *attr.request_type);
78                         data->request_type = *attr.request_type;
79                 }
80         }
81
82         return data;
83 }
84
85
86 void wps_deinit(struct wps_data *data)
87 {
88         if (data->wps_pin_revealed) {
89                 wpa_printf(MSG_DEBUG, "WPS: Full PIN information revealed and "
90                            "negotiation failed");
91                 if (data->registrar)
92                         wps_registrar_invalidate_pin(data->registrar,
93                                                      data->uuid_e);
94         } else if (data->registrar)
95                 wps_registrar_unlock_pin(data->registrar, data->uuid_e);
96
97         wpabuf_free(data->dh_privkey);
98         wpabuf_free(data->dh_pubkey_e);
99         wpabuf_free(data->dh_pubkey_r);
100         wpabuf_free(data->last_msg);
101         os_free(data->dev_password);
102         os_free(data->new_psk);
103         wps_device_data_free(&data->peer_dev);
104         os_free(data);
105 }
106
107
108 enum wps_process_res wps_process_msg(struct wps_data *wps, u8 op_code,
109                                      const struct wpabuf *msg)
110 {
111         if (wps->registrar)
112                 return wps_registrar_process_msg(wps, op_code, msg);
113         else
114                 return wps_enrollee_process_msg(wps, op_code, msg);
115 }
116
117
118 struct wpabuf * wps_get_msg(struct wps_data *wps, u8 *op_code)
119 {
120         if (wps->registrar)
121                 return wps_registrar_get_msg(wps, op_code);
122         else
123                 return wps_enrollee_get_msg(wps, op_code);
124 }
125
126
127 int wps_is_selected_pbc_registrar(const u8 *buf, size_t len)
128 {
129         struct wps_parse_attr attr;
130         struct wpabuf msg;
131
132         wpabuf_set(&msg, buf, len);
133         if (wps_parse_msg(&msg, &attr) < 0 ||
134             !attr.selected_registrar || *attr.selected_registrar == 0 ||
135             !attr.sel_reg_config_methods ||
136             !(WPA_GET_BE16(attr.sel_reg_config_methods) &
137               WPS_CONFIG_PUSHBUTTON) ||
138             !attr.dev_password_id ||
139             WPA_GET_BE16(attr.dev_password_id) != DEV_PW_PUSHBUTTON)
140                 return 0;
141
142         return 1;
143 }
144
145
146 int wps_is_selected_pin_registrar(const u8 *buf, size_t len)
147 {
148         struct wps_parse_attr attr;
149         struct wpabuf msg;
150
151         wpabuf_set(&msg, buf, len);
152         if (wps_parse_msg(&msg, &attr) < 0 ||
153             !attr.selected_registrar || *attr.selected_registrar == 0 ||
154             !attr.sel_reg_config_methods ||
155             !(WPA_GET_BE16(attr.sel_reg_config_methods) &
156               (WPS_CONFIG_LABEL | WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD)) ||
157             !attr.dev_password_id ||
158             WPA_GET_BE16(attr.dev_password_id) == DEV_PW_PUSHBUTTON)
159                 return 0;
160
161         return 1;
162 }
163
164
165 const u8 * wps_get_uuid_e(const u8 *buf, size_t len)
166 {
167         struct wps_parse_attr attr;
168         struct wpabuf msg;
169
170         wpabuf_set(&msg, buf, len);
171         if (wps_parse_msg(&msg, &attr) < 0)
172                 return NULL;
173         return attr.uuid_e;
174 }
175
176
177 struct wpabuf * wps_build_assoc_req_ie(u8 req_type)
178 {
179         struct wpabuf *ie;
180         u8 *len;
181
182         wpa_printf(MSG_DEBUG, "WPS: Building WPS IE for (Re)Association "
183                    "Request");
184         ie = wpabuf_alloc(100);
185         if (ie == NULL)
186                 return NULL;
187
188         wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
189         len = wpabuf_put(ie, 1);
190         wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
191
192         if (wps_build_version(ie) ||
193             wps_build_req_type(ie, req_type)) {
194                 wpabuf_free(ie);
195                 return NULL;
196         }
197
198         *len = wpabuf_len(ie) - 2;
199
200         return ie;
201 }
202
203
204 struct wpabuf * wps_build_probe_req_ie(int pbc, struct wps_device_data *dev,
205                                        const u8 *uuid, u8 req_type)
206 {
207         struct wpabuf *ie;
208         u8 *len;
209         u16 methods;
210
211         wpa_printf(MSG_DEBUG, "WPS: Building WPS IE for Probe Request");
212
213         ie = wpabuf_alloc(200);
214         if (ie == NULL)
215                 return NULL;
216
217         wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
218         len = wpabuf_put(ie, 1);
219         wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
220
221         if (pbc)
222                 methods = WPS_CONFIG_PUSHBUTTON;
223         else
224                 methods = WPS_CONFIG_LABEL | WPS_CONFIG_DISPLAY |
225                         WPS_CONFIG_KEYPAD;
226
227         if (wps_build_version(ie) ||
228             wps_build_req_type(ie, req_type) ||
229             wps_build_config_methods(ie, methods) ||
230             wps_build_uuid_e(ie, uuid) ||
231             wps_build_primary_dev_type(dev, ie) ||
232             wps_build_rf_bands(dev, ie) ||
233             wps_build_assoc_state(NULL, ie) ||
234             wps_build_config_error(ie, WPS_CFG_NO_ERROR) ||
235             wps_build_dev_password_id(ie, pbc ? DEV_PW_PUSHBUTTON :
236                                       DEV_PW_DEFAULT)) {
237                 wpabuf_free(ie);
238                 return NULL;
239         }
240
241         *len = wpabuf_len(ie) - 2;
242
243         return ie;
244 }