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