2369a30b34a169aafb762294a0d25471c24ab570
[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 /**
24  * wps_init - Initialize WPS Registration protocol data
25  * @cfg: WPS configuration
26  * Returns: Pointer to allocated data or %NULL on failure
27  *
28  * This function is used to initialize WPS data for a registration protocol
29  * instance (i.e., each run of registration protocol as a Registrar of
30  * Enrollee. The caller is responsible for freeing this data after the
31  * registration run has been completed by calling wps_deinit().
32  */
33 struct wps_data * wps_init(const struct wps_config *cfg)
34 {
35         struct wps_data *data = os_zalloc(sizeof(*data));
36         if (data == NULL)
37                 return NULL;
38         data->authenticator = cfg->authenticator;
39         data->wps = cfg->wps;
40         data->registrar = cfg->registrar;
41         if (cfg->registrar) {
42                 os_memcpy(data->uuid_r, cfg->wps->uuid, WPS_UUID_LEN);
43         } else {
44                 os_memcpy(data->mac_addr_e, cfg->wps->dev.mac_addr, ETH_ALEN);
45                 os_memcpy(data->uuid_e, cfg->wps->uuid, WPS_UUID_LEN);
46         }
47         if (cfg->pin) {
48                 data->dev_pw_id = DEV_PW_DEFAULT;
49                 data->dev_password = os_malloc(cfg->pin_len);
50                 if (data->dev_password == NULL) {
51                         os_free(data);
52                         return NULL;
53                 }
54                 os_memcpy(data->dev_password, cfg->pin, cfg->pin_len);
55                 data->dev_password_len = cfg->pin_len;
56         }
57
58         data->pbc = cfg->pbc;
59         if (cfg->pbc) {
60                 /* Use special PIN '00000000' for PBC */
61                 data->dev_pw_id = DEV_PW_PUSHBUTTON;
62                 os_free(data->dev_password);
63                 data->dev_password = os_malloc(8);
64                 if (data->dev_password == NULL) {
65                         os_free(data);
66                         return NULL;
67                 }
68                 os_memset(data->dev_password, '0', 8);
69                 data->dev_password_len = 8;
70         }
71
72         data->state = data->registrar ? RECV_M1 : SEND_M1;
73
74         if (cfg->assoc_wps_ie) {
75                 struct wps_parse_attr attr;
76                 wpa_hexdump_buf(MSG_DEBUG, "WPS: WPS IE from (Re)AssocReq",
77                                 cfg->assoc_wps_ie);
78                 if (wps_parse_msg(cfg->assoc_wps_ie, &attr) < 0) {
79                         wpa_printf(MSG_DEBUG, "WPS: Failed to parse WPS IE "
80                                    "from (Re)AssocReq");
81                 } else if (attr.request_type == NULL) {
82                         wpa_printf(MSG_DEBUG, "WPS: No Request Type attribute "
83                                    "in (Re)AssocReq WPS IE");
84                 } else {
85                         wpa_printf(MSG_DEBUG, "WPS: Request Type (from WPS IE "
86                                    "in (Re)AssocReq WPS IE): %d",
87                                    *attr.request_type);
88                         data->request_type = *attr.request_type;
89                 }
90         }
91
92         return data;
93 }
94
95
96 /**
97  * wps_deinit - Deinitialize WPS Registration protocol data
98  * @data: WPS Registration protocol data from wps_init()
99  */
100 void wps_deinit(struct wps_data *data)
101 {
102         if (data->wps_pin_revealed) {
103                 wpa_printf(MSG_DEBUG, "WPS: Full PIN information revealed and "
104                            "negotiation failed");
105                 if (data->registrar)
106                         wps_registrar_invalidate_pin(data->registrar,
107                                                      data->uuid_e);
108         } else if (data->registrar)
109                 wps_registrar_unlock_pin(data->registrar, data->uuid_e);
110
111         wpabuf_free(data->dh_privkey);
112         wpabuf_free(data->dh_pubkey_e);
113         wpabuf_free(data->dh_pubkey_r);
114         wpabuf_free(data->last_msg);
115         os_free(data->dev_password);
116         os_free(data->new_psk);
117         wps_device_data_free(&data->peer_dev);
118         os_free(data);
119 }
120
121
122 /**
123  * wps_process_msg - Process a WPS message
124  * @wps: WPS Registration protocol data from wps_init()
125  * @op_code: Message OP Code
126  * @msg: Message data
127  * Returns: Processing result
128  *
129  * This function is used to process WPS messages with OP Codes WSC_ACK,
130  * WSC_NACK, WSC_MSG, and WSC_Done. The caller (e.g., EAP server/peer) is
131  * responsible for reassembling the messages before calling this function.
132  * Response to this message is built by calling wps_get_msg().
133  */
134 enum wps_process_res wps_process_msg(struct wps_data *wps,
135                                      enum wsc_op_code op_code,
136                                      const struct wpabuf *msg)
137 {
138         if (wps->registrar)
139                 return wps_registrar_process_msg(wps, op_code, msg);
140         else
141                 return wps_enrollee_process_msg(wps, op_code, msg);
142 }
143
144
145 /**
146  * wps_get_msg - Build a WPS message
147  * @wps: WPS Registration protocol data from wps_init()
148  * @op_code: Buffer for returning message OP Code
149  * Returns: The generated WPS message or %NULL on failure
150  *
151  * This function is used to build a response to a message processed by calling
152  * wps_process_msg(). The caller is responsible for freeing the buffer.
153  */
154 struct wpabuf * wps_get_msg(struct wps_data *wps, enum wsc_op_code *op_code)
155 {
156         if (wps->registrar)
157                 return wps_registrar_get_msg(wps, op_code);
158         else
159                 return wps_enrollee_get_msg(wps, op_code);
160 }
161
162
163 /**
164  * wps_is_selected_pbc_registrar - Check whether WPS IE indicates active PBC
165  * @msg: WPS IE contents from Beacon or Probe Response frame
166  * Returns: 1 if PBC Registrar is active, 0 if not
167  */
168 int wps_is_selected_pbc_registrar(const struct wpabuf *msg)
169 {
170         struct wps_parse_attr attr;
171
172         /*
173          * In theory, this could also verify that attr.sel_reg_config_methods
174          * includes WPS_CONFIG_PUSHBUTTON, but some deployed AP implementations
175          * do not set Selected Registrar Config Methods attribute properly, so
176          * it is safer to just use Device Password ID here.
177          */
178
179         if (wps_parse_msg(msg, &attr) < 0 ||
180             !attr.selected_registrar || *attr.selected_registrar == 0 ||
181             !attr.dev_password_id ||
182             WPA_GET_BE16(attr.dev_password_id) != DEV_PW_PUSHBUTTON)
183                 return 0;
184
185         return 1;
186 }
187
188
189 /**
190  * wps_is_selected_pbc_registrar - Check whether WPS IE indicates active PIN
191  * @msg: WPS IE contents from Beacon or Probe Response frame
192  * Returns: 1 if PIN Registrar is active, 0 if not
193  */
194 int wps_is_selected_pin_registrar(const struct wpabuf *msg)
195 {
196         struct wps_parse_attr attr;
197
198         /*
199          * In theory, this could also verify that attr.sel_reg_config_methods
200          * includes WPS_CONFIG_LABEL, WPS_CONFIG_DISPLAY, or WPS_CONFIG_KEYPAD,
201          * but some deployed AP implementations do not set Selected Registrar
202          * Config Methods attribute properly, so it is safer to just use
203          * Device Password ID here.
204          */
205
206         if (wps_parse_msg(msg, &attr) < 0 ||
207             !attr.selected_registrar || *attr.selected_registrar == 0 ||
208             !attr.dev_password_id ||
209             WPA_GET_BE16(attr.dev_password_id) == DEV_PW_PUSHBUTTON)
210                 return 0;
211
212         return 1;
213 }
214
215
216 /**
217  * wps_get_uuid_e - Get UUID-E from WPS IE
218  * @msg: WPS IE contents from Beacon or Probe Response frame
219  * Returns: Pointer to UUID-E or %NULL if not included
220  *
221  * The returned pointer is to the msg contents and it remains valid only as
222  * long as the msg buffer is valid.
223  */
224 const u8 * wps_get_uuid_e(const struct wpabuf *msg)
225 {
226         struct wps_parse_attr attr;
227
228         if (wps_parse_msg(msg, &attr) < 0)
229                 return NULL;
230         return attr.uuid_e;
231 }
232
233
234 /**
235  * wps_build_assoc_req_ie - Build WPS IE for (Re)Association Request
236  * @req_type: Value for Request Type attribute
237  * Returns: WPS IE or %NULL on failure
238  *
239  * The caller is responsible for freeing the buffer.
240  */
241 struct wpabuf * wps_build_assoc_req_ie(enum wps_request_type req_type)
242 {
243         struct wpabuf *ie;
244         u8 *len;
245
246         wpa_printf(MSG_DEBUG, "WPS: Building WPS IE for (Re)Association "
247                    "Request");
248         ie = wpabuf_alloc(100);
249         if (ie == NULL)
250                 return NULL;
251
252         wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
253         len = wpabuf_put(ie, 1);
254         wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
255
256         if (wps_build_version(ie) ||
257             wps_build_req_type(ie, req_type)) {
258                 wpabuf_free(ie);
259                 return NULL;
260         }
261
262         *len = wpabuf_len(ie) - 2;
263
264         return ie;
265 }
266
267
268 /**
269  * wps_build_probe_req_ie - Build WPS IE for Probe Request
270  * @pbc: Whether searching for PBC mode APs
271  * @dev: Device attributes
272  * @uuid: Own UUID
273  * @req_type: Value for Request Type attribute
274  * Returns: WPS IE or %NULL on failure
275  *
276  * The caller is responsible for freeing the buffer.
277  */
278 struct wpabuf * wps_build_probe_req_ie(int pbc, struct wps_device_data *dev,
279                                        const u8 *uuid,
280                                        enum wps_request_type req_type)
281 {
282         struct wpabuf *ie;
283         u8 *len;
284         u16 methods;
285
286         wpa_printf(MSG_DEBUG, "WPS: Building WPS IE for Probe Request");
287
288         ie = wpabuf_alloc(200);
289         if (ie == NULL)
290                 return NULL;
291
292         wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
293         len = wpabuf_put(ie, 1);
294         wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
295
296         if (pbc)
297                 methods = WPS_CONFIG_PUSHBUTTON;
298         else
299                 methods = WPS_CONFIG_LABEL | WPS_CONFIG_DISPLAY |
300                         WPS_CONFIG_KEYPAD;
301
302         if (wps_build_version(ie) ||
303             wps_build_req_type(ie, req_type) ||
304             wps_build_config_methods(ie, methods) ||
305             wps_build_uuid_e(ie, uuid) ||
306             wps_build_primary_dev_type(dev, ie) ||
307             wps_build_rf_bands(dev, ie) ||
308             wps_build_assoc_state(NULL, ie) ||
309             wps_build_config_error(ie, WPS_CFG_NO_ERROR) ||
310             wps_build_dev_password_id(ie, pbc ? DEV_PW_PUSHBUTTON :
311                                       DEV_PW_DEFAULT)) {
312                 wpabuf_free(ie);
313                 return NULL;
314         }
315
316         *len = wpabuf_len(ie) - 2;
317
318         return ie;
319 }