WPS: Add UFD support (USBA out-of-band mechanism)
[wpasupplicant] / 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->wps = cfg->wps;
39         data->registrar = cfg->registrar;
40         if (cfg->registrar) {
41                 os_memcpy(data->uuid_r, cfg->wps->uuid, WPS_UUID_LEN);
42         } else {
43                 os_memcpy(data->mac_addr_e, cfg->wps->dev.mac_addr, ETH_ALEN);
44                 os_memcpy(data->uuid_e, cfg->wps->uuid, WPS_UUID_LEN);
45         }
46         if (cfg->pin) {
47                 data->dev_pw_id = data->wps->oob_dev_pw_id == 0 ?
48                         DEV_PW_DEFAULT : data->wps->oob_dev_pw_id;
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->wps->registrar,
107                                                      data->uuid_e);
108         } else if (data->registrar)
109                 wps_registrar_unlock_pin(data->wps->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_pin_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                 return 0;
208
209         if (!attr.selected_registrar || *attr.selected_registrar == 0)
210                 return 0;
211
212         if (attr.dev_password_id != NULL &&
213             WPA_GET_BE16(attr.dev_password_id) == DEV_PW_PUSHBUTTON)
214                 return 0;
215
216         return 1;
217 }
218
219
220 /**
221  * wps_get_uuid_e - Get UUID-E from WPS IE
222  * @msg: WPS IE contents from Beacon or Probe Response frame
223  * Returns: Pointer to UUID-E or %NULL if not included
224  *
225  * The returned pointer is to the msg contents and it remains valid only as
226  * long as the msg buffer is valid.
227  */
228 const u8 * wps_get_uuid_e(const struct wpabuf *msg)
229 {
230         struct wps_parse_attr attr;
231
232         if (wps_parse_msg(msg, &attr) < 0)
233                 return NULL;
234         return attr.uuid_e;
235 }
236
237
238 /**
239  * wps_build_assoc_req_ie - Build WPS IE for (Re)Association Request
240  * @req_type: Value for Request Type attribute
241  * Returns: WPS IE or %NULL on failure
242  *
243  * The caller is responsible for freeing the buffer.
244  */
245 struct wpabuf * wps_build_assoc_req_ie(enum wps_request_type req_type)
246 {
247         struct wpabuf *ie;
248         u8 *len;
249
250         wpa_printf(MSG_DEBUG, "WPS: Building WPS IE for (Re)Association "
251                    "Request");
252         ie = wpabuf_alloc(100);
253         if (ie == NULL)
254                 return NULL;
255
256         wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
257         len = wpabuf_put(ie, 1);
258         wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
259
260         if (wps_build_version(ie) ||
261             wps_build_req_type(ie, req_type)) {
262                 wpabuf_free(ie);
263                 return NULL;
264         }
265
266         *len = wpabuf_len(ie) - 2;
267
268         return ie;
269 }
270
271
272 /**
273  * wps_build_probe_req_ie - Build WPS IE for Probe Request
274  * @pbc: Whether searching for PBC mode APs
275  * @dev: Device attributes
276  * @uuid: Own UUID
277  * @req_type: Value for Request Type attribute
278  * Returns: WPS IE or %NULL on failure
279  *
280  * The caller is responsible for freeing the buffer.
281  */
282 struct wpabuf * wps_build_probe_req_ie(int pbc, struct wps_device_data *dev,
283                                        const u8 *uuid,
284                                        enum wps_request_type req_type)
285 {
286         struct wpabuf *ie;
287         u8 *len;
288         u16 methods;
289
290         wpa_printf(MSG_DEBUG, "WPS: Building WPS IE for Probe Request");
291
292         ie = wpabuf_alloc(200);
293         if (ie == NULL)
294                 return NULL;
295
296         wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
297         len = wpabuf_put(ie, 1);
298         wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
299
300         if (pbc)
301                 methods = WPS_CONFIG_PUSHBUTTON;
302         else
303                 methods = WPS_CONFIG_LABEL | WPS_CONFIG_DISPLAY |
304                         WPS_CONFIG_KEYPAD | WPS_CONFIG_USBA;
305
306         if (wps_build_version(ie) ||
307             wps_build_req_type(ie, req_type) ||
308             wps_build_config_methods(ie, methods) ||
309             wps_build_uuid_e(ie, uuid) ||
310             wps_build_primary_dev_type(dev, ie) ||
311             wps_build_rf_bands(dev, ie) ||
312             wps_build_assoc_state(NULL, ie) ||
313             wps_build_config_error(ie, WPS_CFG_NO_ERROR) ||
314             wps_build_dev_password_id(ie, pbc ? DEV_PW_PUSHBUTTON :
315                                       DEV_PW_DEFAULT)) {
316                 wpabuf_free(ie);
317                 return NULL;
318         }
319
320         *len = wpabuf_len(ie) - 2;
321
322         return ie;
323 }
324
325
326 void wps_free_pending_msgs(struct upnp_pending_message *msgs)
327 {
328         struct upnp_pending_message *p, *prev;
329         p = msgs;
330         while (p) {
331                 prev = p;
332                 p = p->next;
333                 wpabuf_free(prev->msg);
334                 os_free(prev);
335         }
336 }