WPS 2.0: Add virtual/physical display and pushbutton config methods
[mech_eap.git] / src / wps / wps.c
1 /*
2  * Wi-Fi Protected Setup
3  * Copyright (c) 2007-2009, 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 "crypto/dh_group5.h"
19 #include "common/ieee802_11_defs.h"
20 #include "wps_i.h"
21 #include "wps_dev_attr.h"
22
23
24 /**
25  * wps_init - Initialize WPS Registration protocol data
26  * @cfg: WPS configuration
27  * Returns: Pointer to allocated data or %NULL on failure
28  *
29  * This function is used to initialize WPS data for a registration protocol
30  * instance (i.e., each run of registration protocol as a Registrar of
31  * Enrollee. The caller is responsible for freeing this data after the
32  * registration run has been completed by calling wps_deinit().
33  */
34 struct wps_data * wps_init(const struct wps_config *cfg)
35 {
36         struct wps_data *data = os_zalloc(sizeof(*data));
37         if (data == NULL)
38                 return NULL;
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 = data->wps->oob_dev_pw_id == 0 ?
49                         DEV_PW_DEFAULT : data->wps->oob_dev_pw_id;
50                 data->dev_password = os_malloc(cfg->pin_len);
51                 if (data->dev_password == NULL) {
52                         os_free(data);
53                         return NULL;
54                 }
55                 os_memcpy(data->dev_password, cfg->pin, cfg->pin_len);
56                 data->dev_password_len = cfg->pin_len;
57         }
58
59         data->pbc = cfg->pbc;
60         if (cfg->pbc) {
61                 /* Use special PIN '00000000' for PBC */
62                 data->dev_pw_id = DEV_PW_PUSHBUTTON;
63                 os_free(data->dev_password);
64                 data->dev_password = os_malloc(8);
65                 if (data->dev_password == NULL) {
66                         os_free(data);
67                         return NULL;
68                 }
69                 os_memset(data->dev_password, '0', 8);
70                 data->dev_password_len = 8;
71         }
72
73         data->state = data->registrar ? RECV_M1 : SEND_M1;
74
75         if (cfg->assoc_wps_ie) {
76                 struct wps_parse_attr attr;
77                 wpa_hexdump_buf(MSG_DEBUG, "WPS: WPS IE from (Re)AssocReq",
78                                 cfg->assoc_wps_ie);
79                 if (wps_parse_msg(cfg->assoc_wps_ie, &attr) < 0) {
80                         wpa_printf(MSG_DEBUG, "WPS: Failed to parse WPS IE "
81                                    "from (Re)AssocReq");
82                 } else if (attr.request_type == NULL) {
83                         wpa_printf(MSG_DEBUG, "WPS: No Request Type attribute "
84                                    "in (Re)AssocReq WPS IE");
85                 } else {
86                         wpa_printf(MSG_DEBUG, "WPS: Request Type (from WPS IE "
87                                    "in (Re)AssocReq WPS IE): %d",
88                                    *attr.request_type);
89                         data->request_type = *attr.request_type;
90                 }
91         }
92
93         if (cfg->new_ap_settings) {
94                 data->new_ap_settings =
95                         os_malloc(sizeof(*data->new_ap_settings));
96                 if (data->new_ap_settings == NULL) {
97                         os_free(data);
98                         return NULL;
99                 }
100                 os_memcpy(data->new_ap_settings, cfg->new_ap_settings,
101                           sizeof(*data->new_ap_settings));
102         }
103
104         if (cfg->peer_addr)
105                 os_memcpy(data->peer_dev.mac_addr, cfg->peer_addr, ETH_ALEN);
106
107         data->use_psk_key = cfg->use_psk_key;
108
109         return data;
110 }
111
112
113 /**
114  * wps_deinit - Deinitialize WPS Registration protocol data
115  * @data: WPS Registration protocol data from wps_init()
116  */
117 void wps_deinit(struct wps_data *data)
118 {
119         if (data->wps_pin_revealed) {
120                 wpa_printf(MSG_DEBUG, "WPS: Full PIN information revealed and "
121                            "negotiation failed");
122                 if (data->registrar)
123                         wps_registrar_invalidate_pin(data->wps->registrar,
124                                                      data->uuid_e);
125         } else if (data->registrar)
126                 wps_registrar_unlock_pin(data->wps->registrar, data->uuid_e);
127
128         wpabuf_free(data->dh_privkey);
129         wpabuf_free(data->dh_pubkey_e);
130         wpabuf_free(data->dh_pubkey_r);
131         wpabuf_free(data->last_msg);
132         os_free(data->dev_password);
133         os_free(data->new_psk);
134         wps_device_data_free(&data->peer_dev);
135         os_free(data->new_ap_settings);
136         dh5_free(data->dh_ctx);
137         os_free(data);
138 }
139
140
141 /**
142  * wps_process_msg - Process a WPS message
143  * @wps: WPS Registration protocol data from wps_init()
144  * @op_code: Message OP Code
145  * @msg: Message data
146  * Returns: Processing result
147  *
148  * This function is used to process WPS messages with OP Codes WSC_ACK,
149  * WSC_NACK, WSC_MSG, and WSC_Done. The caller (e.g., EAP server/peer) is
150  * responsible for reassembling the messages before calling this function.
151  * Response to this message is built by calling wps_get_msg().
152  */
153 enum wps_process_res wps_process_msg(struct wps_data *wps,
154                                      enum wsc_op_code op_code,
155                                      const struct wpabuf *msg)
156 {
157         if (wps->registrar)
158                 return wps_registrar_process_msg(wps, op_code, msg);
159         else
160                 return wps_enrollee_process_msg(wps, op_code, msg);
161 }
162
163
164 /**
165  * wps_get_msg - Build a WPS message
166  * @wps: WPS Registration protocol data from wps_init()
167  * @op_code: Buffer for returning message OP Code
168  * Returns: The generated WPS message or %NULL on failure
169  *
170  * This function is used to build a response to a message processed by calling
171  * wps_process_msg(). The caller is responsible for freeing the buffer.
172  */
173 struct wpabuf * wps_get_msg(struct wps_data *wps, enum wsc_op_code *op_code)
174 {
175         if (wps->registrar)
176                 return wps_registrar_get_msg(wps, op_code);
177         else
178                 return wps_enrollee_get_msg(wps, op_code);
179 }
180
181
182 /**
183  * wps_is_selected_pbc_registrar - Check whether WPS IE indicates active PBC
184  * @msg: WPS IE contents from Beacon or Probe Response frame
185  * Returns: 1 if PBC Registrar is active, 0 if not
186  */
187 int wps_is_selected_pbc_registrar(const struct wpabuf *msg)
188 {
189         struct wps_parse_attr attr;
190
191         /*
192          * In theory, this could also verify that attr.sel_reg_config_methods
193          * includes WPS_CONFIG_PUSHBUTTON, but some deployed AP implementations
194          * do not set Selected Registrar Config Methods attribute properly, so
195          * it is safer to just use Device Password ID here.
196          */
197
198         if (wps_parse_msg(msg, &attr) < 0 ||
199             !attr.selected_registrar || *attr.selected_registrar == 0 ||
200             !attr.dev_password_id ||
201             WPA_GET_BE16(attr.dev_password_id) != DEV_PW_PUSHBUTTON)
202                 return 0;
203
204         return 1;
205 }
206
207
208 static int is_selected_pin_registrar(struct wps_parse_attr *attr)
209 {
210         /*
211          * In theory, this could also verify that attr.sel_reg_config_methods
212          * includes WPS_CONFIG_LABEL, WPS_CONFIG_DISPLAY, or WPS_CONFIG_KEYPAD,
213          * but some deployed AP implementations do not set Selected Registrar
214          * Config Methods attribute properly, so it is safer to just use
215          * Device Password ID here.
216          */
217
218         if (!attr->selected_registrar || *attr->selected_registrar == 0)
219                 return 0;
220
221         if (attr->dev_password_id != NULL &&
222             WPA_GET_BE16(attr->dev_password_id) == DEV_PW_PUSHBUTTON)
223                 return 0;
224
225         return 1;
226 }
227
228
229 /**
230  * wps_is_selected_pin_registrar - Check whether WPS IE indicates active PIN
231  * @msg: WPS IE contents from Beacon or Probe Response frame
232  * Returns: 1 if PIN Registrar is active, 0 if not
233  */
234 int wps_is_selected_pin_registrar(const struct wpabuf *msg)
235 {
236         struct wps_parse_attr attr;
237
238         if (wps_parse_msg(msg, &attr) < 0)
239                 return 0;
240
241         return is_selected_pin_registrar(&attr);
242 }
243
244
245 /**
246  * wps_is_addr_authorized - Check whether WPS IE authorizes MAC address
247  * @msg: WPS IE contents from Beacon or Probe Response frame
248  * @addr: MAC address to search for
249  * @ver1_compat: Whether to use version 1 compatibility mode
250  * Returns: 1 if address is authorized, 0 if not
251  */
252 int wps_is_addr_authorized(const struct wpabuf *msg, const u8 *addr,
253                            int ver1_compat)
254 {
255         struct wps_parse_attr attr;
256         unsigned int i;
257         const u8 *pos;
258
259         if (wps_parse_msg(msg, &attr) < 0)
260                 return 0;
261
262         if (!attr.version2 && ver1_compat) {
263                 /*
264                  * Version 1.0 AP - AuthorizedMACs not used, so revert back to
265                  * old mechanism of using SelectedRegistrar.
266                  */
267                 return is_selected_pin_registrar(&attr);
268         }
269
270         if (!attr.authorized_macs)
271                 return 0;
272
273         pos = attr.authorized_macs;
274         for (i = 0; i < attr.authorized_macs_len / ETH_ALEN; i++) {
275                 if (os_memcmp(pos, addr, ETH_ALEN) == 0)
276                         return 1;
277                 pos += ETH_ALEN;
278         }
279
280         return 0;
281 }
282
283
284 /**
285  * wps_ap_priority_compar - Prioritize WPS IE from two APs
286  * @wps_a: WPS IE contents from Beacon or Probe Response frame
287  * @wps_b: WPS IE contents from Beacon or Probe Response frame
288  * Returns: 1 if wps_b is considered more likely selection for WPS
289  * provisioning, -1 if wps_a is considered more like, or 0 if no preference
290  */
291 int wps_ap_priority_compar(const struct wpabuf *wps_a,
292                            const struct wpabuf *wps_b)
293 {
294         struct wps_parse_attr attr_a, attr_b;
295         int sel_a, sel_b;
296
297         if (wps_a == NULL || wps_parse_msg(wps_a, &attr_a) < 0)
298                 return 1;
299         if (wps_b == NULL || wps_parse_msg(wps_b, &attr_b) < 0)
300                 return -1;
301
302         sel_a = attr_a.selected_registrar && *attr_a.selected_registrar != 0;
303         sel_b = attr_b.selected_registrar && *attr_b.selected_registrar != 0;
304
305         if (sel_a && !sel_b)
306                 return -1;
307         if (!sel_a && sel_b)
308                 return 1;
309
310         return 0;
311 }
312
313
314 /**
315  * wps_get_uuid_e - Get UUID-E from WPS IE
316  * @msg: WPS IE contents from Beacon or Probe Response frame
317  * Returns: Pointer to UUID-E or %NULL if not included
318  *
319  * The returned pointer is to the msg contents and it remains valid only as
320  * long as the msg buffer is valid.
321  */
322 const u8 * wps_get_uuid_e(const struct wpabuf *msg)
323 {
324         struct wps_parse_attr attr;
325
326         if (wps_parse_msg(msg, &attr) < 0)
327                 return NULL;
328         return attr.uuid_e;
329 }
330
331
332 /**
333  * wps_build_assoc_req_ie - Build WPS IE for (Re)Association Request
334  * @req_type: Value for Request Type attribute
335  * Returns: WPS IE or %NULL on failure
336  *
337  * The caller is responsible for freeing the buffer.
338  */
339 struct wpabuf * wps_build_assoc_req_ie(enum wps_request_type req_type)
340 {
341         struct wpabuf *ie;
342         u8 *len;
343
344         wpa_printf(MSG_DEBUG, "WPS: Building WPS IE for (Re)Association "
345                    "Request");
346         ie = wpabuf_alloc(100);
347         if (ie == NULL)
348                 return NULL;
349
350         wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
351         len = wpabuf_put(ie, 1);
352         wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
353
354         if (wps_build_version(ie) ||
355             wps_build_req_type(ie, req_type) ||
356             wps_build_version2(ie)) {
357                 wpabuf_free(ie);
358                 return NULL;
359         }
360
361         *len = wpabuf_len(ie) - 2;
362
363         return ie;
364 }
365
366
367 /**
368  * wps_build_assoc_resp_ie - Build WPS IE for (Re)Association Response
369  * Returns: WPS IE or %NULL on failure
370  *
371  * The caller is responsible for freeing the buffer.
372  */
373 struct wpabuf * wps_build_assoc_resp_ie(void)
374 {
375         struct wpabuf *ie;
376         u8 *len;
377
378         wpa_printf(MSG_DEBUG, "WPS: Building WPS IE for (Re)Association "
379                    "Response");
380         ie = wpabuf_alloc(100);
381         if (ie == NULL)
382                 return NULL;
383
384         wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
385         len = wpabuf_put(ie, 1);
386         wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
387
388         if (wps_build_version(ie) ||
389             wps_build_resp_type(ie, WPS_RESP_AP) ||
390             wps_build_version2(ie)) {
391                 wpabuf_free(ie);
392                 return NULL;
393         }
394
395         *len = wpabuf_len(ie) - 2;
396
397         return ie;
398 }
399
400
401 /**
402  * wps_build_probe_req_ie - Build WPS IE for Probe Request
403  * @pbc: Whether searching for PBC mode APs
404  * @dev: Device attributes
405  * @uuid: Own UUID
406  * @req_type: Value for Request Type attribute
407  * Returns: WPS IE or %NULL on failure
408  *
409  * The caller is responsible for freeing the buffer.
410  */
411 struct wpabuf * wps_build_probe_req_ie(int pbc, struct wps_device_data *dev,
412                                        const u8 *uuid,
413                                        enum wps_request_type req_type)
414 {
415         struct wpabuf *ie;
416         u8 *len;
417         u16 methods;
418
419         wpa_printf(MSG_DEBUG, "WPS: Building WPS IE for Probe Request");
420
421         ie = wpabuf_alloc(200);
422         if (ie == NULL)
423                 return NULL;
424
425         wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
426         len = wpabuf_put(ie, 1);
427         wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
428
429         if (pbc) {
430                 methods = WPS_CONFIG_PUSHBUTTON;
431                 /*
432                  * TODO: At least in theory, should figure out whether this
433                  * Probe Request was triggered with physical or virtual
434                  * pushbutton.
435                  */
436                 methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
437         } else {
438                 /*
439                  * TODO: At least in theory, should figure out whether this
440                  * Probe Request was triggered using physical or virtual
441                  * display.
442                  */
443                 methods = WPS_CONFIG_LABEL | WPS_CONFIG_DISPLAY |
444                         WPS_CONFIG_VIRT_DISPLAY |
445                         WPS_CONFIG_KEYPAD;
446 #ifdef CONFIG_WPS_UFD
447                 methods |= WPS_CONFIG_USBA;
448 #endif /* CONFIG_WPS_UFD */
449 #ifdef CONFIG_WPS_NFC
450                 methods |= WPS_CONFIG_NFC_INTERFACE;
451 #endif /* CONFIG_WPS_NFC */
452         }
453
454         if (wps_build_version(ie) ||
455             wps_build_req_type(ie, req_type) ||
456             wps_build_config_methods(ie, methods) ||
457             wps_build_uuid_e(ie, uuid) ||
458             wps_build_primary_dev_type(dev, ie) ||
459             wps_build_rf_bands(dev, ie) ||
460             wps_build_assoc_state(NULL, ie) ||
461             wps_build_config_error(ie, WPS_CFG_NO_ERROR) ||
462             wps_build_dev_password_id(ie, pbc ? DEV_PW_PUSHBUTTON :
463                                       DEV_PW_DEFAULT) ||
464             wps_build_version2(ie) ||
465             wps_build_manufacturer(dev, ie) ||
466             wps_build_model_name(dev, ie) ||
467             wps_build_model_number(dev, ie) ||
468             wps_build_dev_name(dev, ie) ||
469             (req_type == WPS_REQ_ENROLLEE &&
470              wps_build_req_to_enroll(ie))) {
471                 wpabuf_free(ie);
472                 return NULL;
473         }
474
475         *len = wpabuf_len(ie) - 2;
476
477         return ie;
478 }
479
480
481 void wps_free_pending_msgs(struct upnp_pending_message *msgs)
482 {
483         struct upnp_pending_message *p, *prev;
484         p = msgs;
485         while (p) {
486                 prev = p;
487                 p = p->next;
488                 wpabuf_free(prev->msg);
489                 os_free(prev);
490         }
491 }
492
493
494 int wps_attr_text(struct wpabuf *data, char *buf, char *end)
495 {
496         struct wps_parse_attr attr;
497         char *pos = buf;
498         int ret;
499
500         if (wps_parse_msg(data, &attr) < 0)
501                 return -1;
502
503         if (attr.wps_state) {
504                 if (*attr.wps_state == WPS_STATE_NOT_CONFIGURED)
505                         ret = os_snprintf(pos, end - pos,
506                                           "wps_state=unconfigured\n");
507                 else if (*attr.wps_state == WPS_STATE_CONFIGURED)
508                         ret = os_snprintf(pos, end - pos,
509                                           "wps_state=configured\n");
510                 else
511                         ret = 0;
512                 if (ret < 0 || ret >= end - pos)
513                         return pos - buf;
514                 pos += ret;
515         }
516
517         if (attr.ap_setup_locked && *attr.ap_setup_locked) {
518                 ret = os_snprintf(pos, end - pos,
519                                   "wps_ap_setup_locked=1\n");
520                 if (ret < 0 || ret >= end - pos)
521                         return pos - buf;
522                 pos += ret;
523         }
524
525         if (attr.selected_registrar && *attr.selected_registrar) {
526                 ret = os_snprintf(pos, end - pos,
527                                   "wps_selected_registrar=1\n");
528                 if (ret < 0 || ret >= end - pos)
529                         return pos - buf;
530                 pos += ret;
531         }
532
533         if (attr.dev_password_id) {
534                 ret = os_snprintf(pos, end - pos,
535                                   "wps_device_password_id=%u\n",
536                                   WPA_GET_BE16(attr.dev_password_id));
537                 if (ret < 0 || ret >= end - pos)
538                         return pos - buf;
539                 pos += ret;
540         }
541
542         if (attr.sel_reg_config_methods) {
543                 ret = os_snprintf(pos, end - pos,
544                                   "wps_selected_registrar_config_methods="
545                                   "0x%04x\n",
546                                   WPA_GET_BE16(attr.sel_reg_config_methods));
547                 if (ret < 0 || ret >= end - pos)
548                         return pos - buf;
549                 pos += ret;
550         }
551
552         if (attr.primary_dev_type) {
553                 char devtype[WPS_DEV_TYPE_BUFSIZE];
554                 ret = os_snprintf(pos, end - pos,
555                                   "wps_primary_device_type=%s\n",
556                                   wps_dev_type_bin2str(attr.primary_dev_type,
557                                                        devtype,
558                                                        sizeof(devtype)));
559                 if (ret < 0 || ret >= end - pos)
560                         return pos - buf;
561                 pos += ret;
562         }
563
564         if (attr.dev_name) {
565                 char *str = os_malloc(attr.dev_name_len + 1);
566                 size_t i;
567                 if (str == NULL)
568                         return pos - buf;
569                 for (i = 0; i < attr.dev_name_len; i++) {
570                         if (attr.dev_name[i] < 32)
571                                 str[i] = '_';
572                         else
573                                 str[i] = attr.dev_name[i];
574                 }
575                 str[i] = '\0';
576                 ret = os_snprintf(pos, end - pos, "wps_device_name=%s\n", str);
577                 os_free(str);
578                 if (ret < 0 || ret >= end - pos)
579                         return pos - buf;
580                 pos += ret;
581         }
582
583         if (attr.config_methods) {
584                 ret = os_snprintf(pos, end - pos,
585                                   "wps_config_methods=0x%04x\n",
586                                   WPA_GET_BE16(attr.config_methods));
587                 if (ret < 0 || ret >= end - pos)
588                         return pos - buf;
589                 pos += ret;
590         }
591
592         return pos - buf;
593 }