]> git.ipfire.org Git - thirdparty/hostap.git/blob - src/wps/wps_registrar.c
Merge driver ops set_wps_beacon_ie and set_wps_probe_resp_ie
[thirdparty/hostap.git] / src / wps / wps_registrar.c
1 /*
2 * Wi-Fi Protected Setup - Registrar
3 * Copyright (c) 2008-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 "utils/includes.h"
16
17 #include "utils/common.h"
18 #include "utils/base64.h"
19 #include "utils/eloop.h"
20 #include "utils/uuid.h"
21 #include "utils/list.h"
22 #include "crypto/crypto.h"
23 #include "crypto/sha256.h"
24 #include "common/ieee802_11_defs.h"
25 #include "wps_i.h"
26 #include "wps_dev_attr.h"
27 #include "wps_upnp.h"
28 #include "wps_upnp_i.h"
29
30 #define WPS_WORKAROUNDS
31
32 struct wps_uuid_pin {
33 struct dl_list list;
34 u8 uuid[WPS_UUID_LEN];
35 int wildcard_uuid;
36 u8 *pin;
37 size_t pin_len;
38 #define PIN_LOCKED BIT(0)
39 #define PIN_EXPIRES BIT(1)
40 int flags;
41 struct os_time expiration;
42 };
43
44
45 static void wps_free_pin(struct wps_uuid_pin *pin)
46 {
47 os_free(pin->pin);
48 os_free(pin);
49 }
50
51
52 static void wps_remove_pin(struct wps_uuid_pin *pin)
53 {
54 dl_list_del(&pin->list);
55 wps_free_pin(pin);
56 }
57
58
59 static void wps_free_pins(struct dl_list *pins)
60 {
61 struct wps_uuid_pin *pin, *prev;
62 dl_list_for_each_safe(pin, prev, pins, struct wps_uuid_pin, list)
63 wps_remove_pin(pin);
64 }
65
66
67 struct wps_pbc_session {
68 struct wps_pbc_session *next;
69 u8 addr[ETH_ALEN];
70 u8 uuid_e[WPS_UUID_LEN];
71 struct os_time timestamp;
72 };
73
74
75 static void wps_free_pbc_sessions(struct wps_pbc_session *pbc)
76 {
77 struct wps_pbc_session *prev;
78
79 while (pbc) {
80 prev = pbc;
81 pbc = pbc->next;
82 os_free(prev);
83 }
84 }
85
86
87 struct wps_registrar_device {
88 struct wps_registrar_device *next;
89 struct wps_device_data dev;
90 u8 uuid[WPS_UUID_LEN];
91 };
92
93
94 struct wps_registrar {
95 struct wps_context *wps;
96
97 int pbc;
98 int selected_registrar;
99
100 int (*new_psk_cb)(void *ctx, const u8 *mac_addr, const u8 *psk,
101 size_t psk_len);
102 int (*set_ie_cb)(void *ctx, struct wpabuf *beacon_ie,
103 struct wpabuf *probe_resp_ie);
104 void (*pin_needed_cb)(void *ctx, const u8 *uuid_e,
105 const struct wps_device_data *dev);
106 void (*reg_success_cb)(void *ctx, const u8 *mac_addr,
107 const u8 *uuid_e);
108 void (*set_sel_reg_cb)(void *ctx, int sel_reg, u16 dev_passwd_id,
109 u16 sel_reg_config_methods);
110 void *cb_ctx;
111
112 struct dl_list pins;
113 struct wps_pbc_session *pbc_sessions;
114
115 int skip_cred_build;
116 struct wpabuf *extra_cred;
117 int disable_auto_conf;
118 int sel_reg_union;
119 int sel_reg_dev_password_id_override;
120 int sel_reg_config_methods_override;
121 int static_wep_only;
122
123 struct wps_registrar_device *devices;
124
125 int force_pbc_overlap;
126 };
127
128
129 static int wps_set_ie(struct wps_registrar *reg);
130 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx);
131 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
132 void *timeout_ctx);
133
134
135 static void wps_free_devices(struct wps_registrar_device *dev)
136 {
137 struct wps_registrar_device *prev;
138
139 while (dev) {
140 prev = dev;
141 dev = dev->next;
142 wps_device_data_free(&prev->dev);
143 os_free(prev);
144 }
145 }
146
147
148 static struct wps_registrar_device * wps_device_get(struct wps_registrar *reg,
149 const u8 *addr)
150 {
151 struct wps_registrar_device *dev;
152
153 for (dev = reg->devices; dev; dev = dev->next) {
154 if (os_memcmp(dev->dev.mac_addr, addr, ETH_ALEN) == 0)
155 return dev;
156 }
157 return NULL;
158 }
159
160
161 static void wps_device_clone_data(struct wps_device_data *dst,
162 struct wps_device_data *src)
163 {
164 os_memcpy(dst->mac_addr, src->mac_addr, ETH_ALEN);
165 os_memcpy(dst->pri_dev_type, src->pri_dev_type, WPS_DEV_TYPE_LEN);
166
167 #define WPS_STRDUP(n) \
168 os_free(dst->n); \
169 dst->n = src->n ? os_strdup(src->n) : NULL
170
171 WPS_STRDUP(device_name);
172 WPS_STRDUP(manufacturer);
173 WPS_STRDUP(model_name);
174 WPS_STRDUP(model_number);
175 WPS_STRDUP(serial_number);
176 #undef WPS_STRDUP
177 }
178
179
180 int wps_device_store(struct wps_registrar *reg,
181 struct wps_device_data *dev, const u8 *uuid)
182 {
183 struct wps_registrar_device *d;
184
185 d = wps_device_get(reg, dev->mac_addr);
186 if (d == NULL) {
187 d = os_zalloc(sizeof(*d));
188 if (d == NULL)
189 return -1;
190 d->next = reg->devices;
191 reg->devices = d;
192 }
193
194 wps_device_clone_data(&d->dev, dev);
195 os_memcpy(d->uuid, uuid, WPS_UUID_LEN);
196
197 return 0;
198 }
199
200
201 static void wps_registrar_add_pbc_session(struct wps_registrar *reg,
202 const u8 *addr, const u8 *uuid_e)
203 {
204 struct wps_pbc_session *pbc, *prev = NULL;
205 struct os_time now;
206
207 os_get_time(&now);
208
209 pbc = reg->pbc_sessions;
210 while (pbc) {
211 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
212 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
213 if (prev)
214 prev->next = pbc->next;
215 else
216 reg->pbc_sessions = pbc->next;
217 break;
218 }
219 prev = pbc;
220 pbc = pbc->next;
221 }
222
223 if (!pbc) {
224 pbc = os_zalloc(sizeof(*pbc));
225 if (pbc == NULL)
226 return;
227 os_memcpy(pbc->addr, addr, ETH_ALEN);
228 if (uuid_e)
229 os_memcpy(pbc->uuid_e, uuid_e, WPS_UUID_LEN);
230 }
231
232 pbc->next = reg->pbc_sessions;
233 reg->pbc_sessions = pbc;
234 pbc->timestamp = now;
235
236 /* remove entries that have timed out */
237 prev = pbc;
238 pbc = pbc->next;
239
240 while (pbc) {
241 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
242 prev->next = NULL;
243 wps_free_pbc_sessions(pbc);
244 break;
245 }
246 prev = pbc;
247 pbc = pbc->next;
248 }
249 }
250
251
252 static void wps_registrar_remove_pbc_session(struct wps_registrar *reg,
253 const u8 *addr, const u8 *uuid_e)
254 {
255 struct wps_pbc_session *pbc, *prev = NULL;
256
257 pbc = reg->pbc_sessions;
258 while (pbc) {
259 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
260 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
261 if (prev)
262 prev->next = pbc->next;
263 else
264 reg->pbc_sessions = pbc->next;
265 os_free(pbc);
266 break;
267 }
268 prev = pbc;
269 pbc = pbc->next;
270 }
271 }
272
273
274 static int wps_registrar_pbc_overlap(struct wps_registrar *reg,
275 const u8 *addr, const u8 *uuid_e)
276 {
277 int count = 0;
278 struct wps_pbc_session *pbc;
279 struct os_time now;
280
281 os_get_time(&now);
282
283 for (pbc = reg->pbc_sessions; pbc; pbc = pbc->next) {
284 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME)
285 break;
286 if (addr == NULL || os_memcmp(addr, pbc->addr, ETH_ALEN) ||
287 uuid_e == NULL ||
288 os_memcmp(uuid_e, pbc->uuid_e, WPS_UUID_LEN))
289 count++;
290 }
291
292 if (addr || uuid_e)
293 count++;
294
295 return count > 1 ? 1 : 0;
296 }
297
298
299 static int wps_build_wps_state(struct wps_context *wps, struct wpabuf *msg)
300 {
301 wpa_printf(MSG_DEBUG, "WPS: * Wi-Fi Protected Setup State (%d)",
302 wps->wps_state);
303 wpabuf_put_be16(msg, ATTR_WPS_STATE);
304 wpabuf_put_be16(msg, 1);
305 wpabuf_put_u8(msg, wps->wps_state);
306 return 0;
307 }
308
309
310 #ifdef CONFIG_WPS_UPNP
311 static void wps_registrar_free_pending_m2(struct wps_context *wps)
312 {
313 struct upnp_pending_message *p, *p2, *prev = NULL;
314 p = wps->upnp_msgs;
315 while (p) {
316 if (p->type == WPS_M2 || p->type == WPS_M2D) {
317 if (prev == NULL)
318 wps->upnp_msgs = p->next;
319 else
320 prev->next = p->next;
321 wpa_printf(MSG_DEBUG, "WPS UPnP: Drop pending M2/M2D");
322 p2 = p;
323 p = p->next;
324 wpabuf_free(p2->msg);
325 os_free(p2);
326 continue;
327 }
328 prev = p;
329 p = p->next;
330 }
331 }
332 #endif /* CONFIG_WPS_UPNP */
333
334
335 static int wps_build_ap_setup_locked(struct wps_context *wps,
336 struct wpabuf *msg)
337 {
338 if (wps->ap_setup_locked) {
339 wpa_printf(MSG_DEBUG, "WPS: * AP Setup Locked");
340 wpabuf_put_be16(msg, ATTR_AP_SETUP_LOCKED);
341 wpabuf_put_be16(msg, 1);
342 wpabuf_put_u8(msg, 1);
343 }
344 return 0;
345 }
346
347
348 static int wps_build_selected_registrar(struct wps_registrar *reg,
349 struct wpabuf *msg)
350 {
351 if (!reg->sel_reg_union)
352 return 0;
353 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar");
354 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR);
355 wpabuf_put_be16(msg, 1);
356 wpabuf_put_u8(msg, 1);
357 return 0;
358 }
359
360
361 static int wps_build_sel_reg_dev_password_id(struct wps_registrar *reg,
362 struct wpabuf *msg)
363 {
364 u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
365 if (!reg->sel_reg_union)
366 return 0;
367 if (reg->sel_reg_dev_password_id_override >= 0)
368 id = reg->sel_reg_dev_password_id_override;
369 wpa_printf(MSG_DEBUG, "WPS: * Device Password ID (%d)", id);
370 wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
371 wpabuf_put_be16(msg, 2);
372 wpabuf_put_be16(msg, id);
373 return 0;
374 }
375
376
377 static int wps_build_sel_reg_config_methods(struct wps_registrar *reg,
378 struct wpabuf *msg)
379 {
380 u16 methods;
381 if (!reg->sel_reg_union)
382 return 0;
383 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
384 if (reg->pbc)
385 methods |= WPS_CONFIG_PUSHBUTTON;
386 if (reg->sel_reg_config_methods_override >= 0)
387 methods = reg->sel_reg_config_methods_override;
388 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar Config Methods (%x)",
389 methods);
390 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
391 wpabuf_put_be16(msg, 2);
392 wpabuf_put_be16(msg, methods);
393 return 0;
394 }
395
396
397 static int wps_build_probe_config_methods(struct wps_registrar *reg,
398 struct wpabuf *msg)
399 {
400 u16 methods;
401 methods = 0;
402 wpa_printf(MSG_DEBUG, "WPS: * Config Methods (%x)", methods);
403 wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
404 wpabuf_put_be16(msg, 2);
405 wpabuf_put_be16(msg, methods);
406 return 0;
407 }
408
409
410 static int wps_build_config_methods_r(struct wps_registrar *reg,
411 struct wpabuf *msg)
412 {
413 u16 methods;
414 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
415 if (reg->pbc)
416 methods |= WPS_CONFIG_PUSHBUTTON;
417 return wps_build_config_methods(msg, methods);
418 }
419
420
421 static int wps_build_resp_type(struct wps_registrar *reg, struct wpabuf *msg)
422 {
423 u8 resp = reg->wps->ap ? WPS_RESP_AP : WPS_RESP_REGISTRAR;
424 wpa_printf(MSG_DEBUG, "WPS: * Response Type (%d)", resp);
425 wpabuf_put_be16(msg, ATTR_RESPONSE_TYPE);
426 wpabuf_put_be16(msg, 1);
427 wpabuf_put_u8(msg, resp);
428 return 0;
429 }
430
431
432 /**
433 * wps_registrar_init - Initialize WPS Registrar data
434 * @wps: Pointer to longterm WPS context
435 * @cfg: Registrar configuration
436 * Returns: Pointer to allocated Registrar data or %NULL on failure
437 *
438 * This function is used to initialize WPS Registrar functionality. It can be
439 * used for a single Registrar run (e.g., when run in a supplicant) or multiple
440 * runs (e.g., when run as an internal Registrar in an AP). Caller is
441 * responsible for freeing the returned data with wps_registrar_deinit() when
442 * Registrar functionality is not needed anymore.
443 */
444 struct wps_registrar *
445 wps_registrar_init(struct wps_context *wps,
446 const struct wps_registrar_config *cfg)
447 {
448 struct wps_registrar *reg = os_zalloc(sizeof(*reg));
449 if (reg == NULL)
450 return NULL;
451
452 dl_list_init(&reg->pins);
453 reg->wps = wps;
454 reg->new_psk_cb = cfg->new_psk_cb;
455 reg->set_ie_cb = cfg->set_ie_cb;
456 reg->pin_needed_cb = cfg->pin_needed_cb;
457 reg->reg_success_cb = cfg->reg_success_cb;
458 reg->set_sel_reg_cb = cfg->set_sel_reg_cb;
459 reg->cb_ctx = cfg->cb_ctx;
460 reg->skip_cred_build = cfg->skip_cred_build;
461 if (cfg->extra_cred) {
462 reg->extra_cred = wpabuf_alloc_copy(cfg->extra_cred,
463 cfg->extra_cred_len);
464 if (reg->extra_cred == NULL) {
465 os_free(reg);
466 return NULL;
467 }
468 }
469 reg->disable_auto_conf = cfg->disable_auto_conf;
470 reg->sel_reg_dev_password_id_override = -1;
471 reg->sel_reg_config_methods_override = -1;
472 reg->static_wep_only = cfg->static_wep_only;
473
474 if (wps_set_ie(reg)) {
475 wps_registrar_deinit(reg);
476 return NULL;
477 }
478
479 return reg;
480 }
481
482
483 /**
484 * wps_registrar_deinit - Deinitialize WPS Registrar data
485 * @reg: Registrar data from wps_registrar_init()
486 */
487 void wps_registrar_deinit(struct wps_registrar *reg)
488 {
489 if (reg == NULL)
490 return;
491 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
492 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
493 wps_free_pins(&reg->pins);
494 wps_free_pbc_sessions(reg->pbc_sessions);
495 wpabuf_free(reg->extra_cred);
496 wps_free_devices(reg->devices);
497 os_free(reg);
498 }
499
500
501 /**
502 * wps_registrar_add_pin - Configure a new PIN for Registrar
503 * @reg: Registrar data from wps_registrar_init()
504 * @uuid: UUID-E or %NULL for wildcard (any UUID)
505 * @pin: PIN (Device Password)
506 * @pin_len: Length of pin in octets
507 * @timeout: Time (in seconds) when the PIN will be invalidated; 0 = no timeout
508 * Returns: 0 on success, -1 on failure
509 */
510 int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *uuid,
511 const u8 *pin, size_t pin_len, int timeout)
512 {
513 struct wps_uuid_pin *p;
514
515 p = os_zalloc(sizeof(*p));
516 if (p == NULL)
517 return -1;
518 if (uuid == NULL)
519 p->wildcard_uuid = 1;
520 else
521 os_memcpy(p->uuid, uuid, WPS_UUID_LEN);
522 p->pin = os_malloc(pin_len);
523 if (p->pin == NULL) {
524 os_free(p);
525 return -1;
526 }
527 os_memcpy(p->pin, pin, pin_len);
528 p->pin_len = pin_len;
529
530 if (timeout) {
531 p->flags |= PIN_EXPIRES;
532 os_get_time(&p->expiration);
533 p->expiration.sec += timeout;
534 }
535
536 dl_list_add(&reg->pins, &p->list);
537
538 wpa_printf(MSG_DEBUG, "WPS: A new PIN configured (timeout=%d)",
539 timeout);
540 wpa_hexdump(MSG_DEBUG, "WPS: UUID", uuid, WPS_UUID_LEN);
541 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: PIN", pin, pin_len);
542 reg->selected_registrar = 1;
543 reg->pbc = 0;
544 wps_registrar_selected_registrar_changed(reg);
545 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
546 eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
547 wps_registrar_set_selected_timeout,
548 reg, NULL);
549
550 return 0;
551 }
552
553
554 static void wps_registrar_expire_pins(struct wps_registrar *reg)
555 {
556 struct wps_uuid_pin *pin, *prev;
557 struct os_time now;
558
559 os_get_time(&now);
560 dl_list_for_each_safe(pin, prev, &reg->pins, struct wps_uuid_pin, list)
561 {
562 if ((pin->flags & PIN_EXPIRES) &&
563 os_time_before(&pin->expiration, &now)) {
564 wpa_hexdump(MSG_DEBUG, "WPS: Expired PIN for UUID",
565 pin->uuid, WPS_UUID_LEN);
566 wps_remove_pin(pin);
567 }
568 }
569 }
570
571
572 /**
573 * wps_registrar_invalidate_pin - Invalidate a PIN for a specific UUID-E
574 * @reg: Registrar data from wps_registrar_init()
575 * @uuid: UUID-E
576 * Returns: 0 on success, -1 on failure (e.g., PIN not found)
577 */
578 int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid)
579 {
580 struct wps_uuid_pin *pin, *prev;
581
582 dl_list_for_each_safe(pin, prev, &reg->pins, struct wps_uuid_pin, list)
583 {
584 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
585 wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
586 pin->uuid, WPS_UUID_LEN);
587 wps_remove_pin(pin);
588 return 0;
589 }
590 }
591
592 return -1;
593 }
594
595
596 static const u8 * wps_registrar_get_pin(struct wps_registrar *reg,
597 const u8 *uuid, size_t *pin_len)
598 {
599 struct wps_uuid_pin *pin, *found = NULL;
600
601 wps_registrar_expire_pins(reg);
602
603 dl_list_for_each(pin, &reg->pins, struct wps_uuid_pin, list) {
604 if (!pin->wildcard_uuid &&
605 os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
606 found = pin;
607 break;
608 }
609 }
610
611 if (!found) {
612 /* Check for wildcard UUIDs since none of the UUID-specific
613 * PINs matched */
614 dl_list_for_each(pin, &reg->pins, struct wps_uuid_pin, list) {
615 if (pin->wildcard_uuid == 1) {
616 wpa_printf(MSG_DEBUG, "WPS: Found a wildcard "
617 "PIN. Assigned it for this UUID-E");
618 pin->wildcard_uuid = 2;
619 os_memcpy(pin->uuid, uuid, WPS_UUID_LEN);
620 found = pin;
621 break;
622 }
623 }
624 }
625
626 if (!found)
627 return NULL;
628
629 /*
630 * Lock the PIN to avoid attacks based on concurrent re-use of the PIN
631 * that could otherwise avoid PIN invalidations.
632 */
633 if (found->flags & PIN_LOCKED) {
634 wpa_printf(MSG_DEBUG, "WPS: Selected PIN locked - do not "
635 "allow concurrent re-use");
636 return NULL;
637 }
638 *pin_len = found->pin_len;
639 found->flags |= PIN_LOCKED;
640 return found->pin;
641 }
642
643
644 /**
645 * wps_registrar_unlock_pin - Unlock a PIN for a specific UUID-E
646 * @reg: Registrar data from wps_registrar_init()
647 * @uuid: UUID-E
648 * Returns: 0 on success, -1 on failure
649 *
650 * PINs are locked to enforce only one concurrent use. This function unlocks a
651 * PIN to allow it to be used again. If the specified PIN was configured using
652 * a wildcard UUID, it will be removed instead of allowing multiple uses.
653 */
654 int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid)
655 {
656 struct wps_uuid_pin *pin;
657
658 dl_list_for_each(pin, &reg->pins, struct wps_uuid_pin, list) {
659 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
660 if (pin->wildcard_uuid == 2) {
661 wpa_printf(MSG_DEBUG, "WPS: Invalidating used "
662 "wildcard PIN");
663 return wps_registrar_invalidate_pin(reg, uuid);
664 }
665 pin->flags &= ~PIN_LOCKED;
666 return 0;
667 }
668 }
669
670 return -1;
671 }
672
673
674 static void wps_registrar_stop_pbc(struct wps_registrar *reg)
675 {
676 reg->selected_registrar = 0;
677 reg->pbc = 0;
678 wps_registrar_selected_registrar_changed(reg);
679 }
680
681
682 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx)
683 {
684 struct wps_registrar *reg = eloop_ctx;
685
686 wpa_printf(MSG_DEBUG, "WPS: PBC timed out - disable PBC mode");
687 wps_pbc_timeout_event(reg->wps);
688 wps_registrar_stop_pbc(reg);
689 }
690
691
692 /**
693 * wps_registrar_button_pushed - Notify Registrar that AP button was pushed
694 * @reg: Registrar data from wps_registrar_init()
695 * Returns: 0 on success, -1 on failure
696 *
697 * This function is called on an AP when a push button is pushed to activate
698 * PBC mode. The PBC mode will be stopped after walk time (2 minutes) timeout
699 * or when a PBC registration is completed.
700 */
701 int wps_registrar_button_pushed(struct wps_registrar *reg)
702 {
703 if (wps_registrar_pbc_overlap(reg, NULL, NULL)) {
704 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - do not start PBC "
705 "mode");
706 wps_pbc_overlap_event(reg->wps);
707 return -1;
708 }
709 wpa_printf(MSG_DEBUG, "WPS: Button pushed - PBC mode started");
710 reg->force_pbc_overlap = 0;
711 reg->selected_registrar = 1;
712 reg->pbc = 1;
713 wps_registrar_selected_registrar_changed(reg);
714
715 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
716 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wps_registrar_pbc_timeout,
717 reg, NULL);
718 return 0;
719 }
720
721
722 static void wps_registrar_pbc_completed(struct wps_registrar *reg)
723 {
724 wpa_printf(MSG_DEBUG, "WPS: PBC completed - stopping PBC mode");
725 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
726 wps_registrar_stop_pbc(reg);
727 }
728
729
730 static void wps_registrar_pin_completed(struct wps_registrar *reg)
731 {
732 wpa_printf(MSG_DEBUG, "WPS: PIN completed using internal Registrar");
733 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
734 reg->selected_registrar = 0;
735 wps_registrar_selected_registrar_changed(reg);
736 }
737
738
739 /**
740 * wps_registrar_probe_req_rx - Notify Registrar of Probe Request
741 * @reg: Registrar data from wps_registrar_init()
742 * @addr: MAC address of the Probe Request sender
743 * @wps_data: WPS IE contents
744 *
745 * This function is called on an AP when a Probe Request with WPS IE is
746 * received. This is used to track PBC mode use and to detect possible overlap
747 * situation with other WPS APs.
748 */
749 void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
750 const struct wpabuf *wps_data)
751 {
752 struct wps_parse_attr attr;
753 u16 methods;
754
755 wpa_hexdump_buf(MSG_MSGDUMP,
756 "WPS: Probe Request with WPS data received",
757 wps_data);
758
759 if (wps_parse_msg(wps_data, &attr) < 0)
760 return;
761 if (!wps_version_supported(attr.version)) {
762 wpa_printf(MSG_DEBUG, "WPS: Unsupported ProbeReq WPS IE "
763 "version 0x%x", attr.version ? *attr.version : 0);
764 return;
765 }
766
767 if (attr.config_methods == NULL) {
768 wpa_printf(MSG_DEBUG, "WPS: No Config Methods attribute in "
769 "Probe Request");
770 return;
771 }
772
773 methods = WPA_GET_BE16(attr.config_methods);
774 if (!(methods & WPS_CONFIG_PUSHBUTTON))
775 return; /* Not PBC */
776
777 wpa_printf(MSG_DEBUG, "WPS: Probe Request for PBC received from "
778 MACSTR, MAC2STR(addr));
779
780 wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
781 if (wps_registrar_pbc_overlap(reg, addr, attr.uuid_e)) {
782 wpa_printf(MSG_DEBUG, "WPS: PBC session overlap detected");
783 reg->force_pbc_overlap = 1;
784 wps_pbc_overlap_event(reg->wps);
785 }
786 }
787
788
789 static int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
790 const u8 *psk, size_t psk_len)
791 {
792 if (reg->new_psk_cb == NULL)
793 return 0;
794
795 return reg->new_psk_cb(reg->cb_ctx, mac_addr, psk, psk_len);
796 }
797
798
799 static void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
800 const struct wps_device_data *dev)
801 {
802 if (reg->pin_needed_cb == NULL)
803 return;
804
805 reg->pin_needed_cb(reg->cb_ctx, uuid_e, dev);
806 }
807
808
809 static void wps_cb_reg_success(struct wps_registrar *reg, const u8 *mac_addr,
810 const u8 *uuid_e)
811 {
812 if (reg->reg_success_cb == NULL)
813 return;
814
815 reg->reg_success_cb(reg->cb_ctx, mac_addr, uuid_e);
816 }
817
818
819 static int wps_cb_set_ie(struct wps_registrar *reg, struct wpabuf *beacon_ie,
820 struct wpabuf *probe_resp_ie)
821 {
822 if (reg->set_ie_cb == NULL)
823 return 0;
824
825 return reg->set_ie_cb(reg->cb_ctx, beacon_ie, probe_resp_ie);
826 }
827
828
829 static void wps_cb_set_sel_reg(struct wps_registrar *reg)
830 {
831 u16 methods = 0;
832 if (reg->set_sel_reg_cb == NULL)
833 return;
834
835 if (reg->selected_registrar) {
836 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
837 if (reg->pbc)
838 methods |= WPS_CONFIG_PUSHBUTTON;
839 }
840
841 reg->set_sel_reg_cb(reg->cb_ctx, reg->selected_registrar,
842 reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT,
843 methods);
844 }
845
846
847 /* Encapsulate WPS IE data with one (or more, if needed) IE headers */
848 static struct wpabuf * wps_ie_encapsulate(struct wpabuf *data)
849 {
850 struct wpabuf *ie;
851 const u8 *pos, *end;
852
853 ie = wpabuf_alloc(wpabuf_len(data) + 100);
854 if (ie == NULL) {
855 wpabuf_free(data);
856 return NULL;
857 }
858
859 pos = wpabuf_head(data);
860 end = pos + wpabuf_len(data);
861
862 while (end > pos) {
863 size_t frag_len = end - pos;
864 if (frag_len > 251)
865 frag_len = 251;
866 wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
867 wpabuf_put_u8(ie, 4 + frag_len);
868 wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
869 wpabuf_put_data(ie, pos, frag_len);
870 pos += frag_len;
871 }
872
873 wpabuf_free(data);
874
875 return ie;
876 }
877
878
879 static int wps_set_ie(struct wps_registrar *reg)
880 {
881 struct wpabuf *beacon;
882 struct wpabuf *probe;
883
884 wpa_printf(MSG_DEBUG, "WPS: Build Beacon and Probe Response IEs");
885
886 beacon = wpabuf_alloc(300);
887 if (beacon == NULL)
888 return -1;
889 probe = wpabuf_alloc(400);
890 if (probe == NULL) {
891 wpabuf_free(beacon);
892 return -1;
893 }
894
895 if (wps_build_version(beacon) ||
896 wps_build_wps_state(reg->wps, beacon) ||
897 wps_build_ap_setup_locked(reg->wps, beacon) ||
898 wps_build_selected_registrar(reg, beacon) ||
899 wps_build_sel_reg_dev_password_id(reg, beacon) ||
900 wps_build_sel_reg_config_methods(reg, beacon) ||
901 wps_build_version(probe) ||
902 wps_build_wps_state(reg->wps, probe) ||
903 wps_build_ap_setup_locked(reg->wps, probe) ||
904 wps_build_selected_registrar(reg, probe) ||
905 wps_build_sel_reg_dev_password_id(reg, probe) ||
906 wps_build_sel_reg_config_methods(reg, probe) ||
907 wps_build_resp_type(reg, probe) ||
908 wps_build_uuid_e(probe, reg->wps->uuid) ||
909 wps_build_device_attrs(&reg->wps->dev, probe) ||
910 wps_build_probe_config_methods(reg, probe) ||
911 wps_build_rf_bands(&reg->wps->dev, probe)) {
912 wpabuf_free(beacon);
913 wpabuf_free(probe);
914 return -1;
915 }
916
917 beacon = wps_ie_encapsulate(beacon);
918 probe = wps_ie_encapsulate(probe);
919
920 if (!beacon || !probe) {
921 wpabuf_free(beacon);
922 wpabuf_free(probe);
923 return -1;
924 }
925
926 if (reg->static_wep_only) {
927 /*
928 * Windows XP and Vista clients can get confused about
929 * EAP-Identity/Request when they probe the network with
930 * EAPOL-Start. In such a case, they may assume the network is
931 * using IEEE 802.1X and prompt user for a certificate while
932 * the correct (non-WPS) behavior would be to ask for the
933 * static WEP key. As a workaround, use Microsoft Provisioning
934 * IE to advertise that legacy 802.1X is not supported.
935 */
936 const u8 ms_wps[7] = {
937 WLAN_EID_VENDOR_SPECIFIC, 5,
938 /* Microsoft Provisioning IE (00:50:f2:5) */
939 0x00, 0x50, 0xf2, 5,
940 0x00 /* no legacy 802.1X or MS WPS */
941 };
942 wpa_printf(MSG_DEBUG, "WPS: Add Microsoft Provisioning IE "
943 "into Beacon/Probe Response frames");
944 wpabuf_put_data(beacon, ms_wps, sizeof(ms_wps));
945 wpabuf_put_data(probe, ms_wps, sizeof(ms_wps));
946 }
947
948 return wps_cb_set_ie(reg, beacon, probe);
949 }
950
951
952 static int wps_get_dev_password(struct wps_data *wps)
953 {
954 const u8 *pin;
955 size_t pin_len = 0;
956
957 os_free(wps->dev_password);
958 wps->dev_password = NULL;
959
960 if (wps->pbc) {
961 wpa_printf(MSG_DEBUG, "WPS: Use default PIN for PBC");
962 pin = (const u8 *) "00000000";
963 pin_len = 8;
964 } else {
965 pin = wps_registrar_get_pin(wps->wps->registrar, wps->uuid_e,
966 &pin_len);
967 }
968 if (pin == NULL) {
969 wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
970 "the Enrollee");
971 wps_cb_pin_needed(wps->wps->registrar, wps->uuid_e,
972 &wps->peer_dev);
973 return -1;
974 }
975
976 wps->dev_password = os_malloc(pin_len);
977 if (wps->dev_password == NULL)
978 return -1;
979 os_memcpy(wps->dev_password, pin, pin_len);
980 wps->dev_password_len = pin_len;
981
982 return 0;
983 }
984
985
986 static int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
987 {
988 wpa_printf(MSG_DEBUG, "WPS: * UUID-R");
989 wpabuf_put_be16(msg, ATTR_UUID_R);
990 wpabuf_put_be16(msg, WPS_UUID_LEN);
991 wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
992 return 0;
993 }
994
995
996 static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
997 {
998 u8 *hash;
999 const u8 *addr[4];
1000 size_t len[4];
1001
1002 if (os_get_random(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
1003 return -1;
1004 wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
1005 wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
1006 wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
1007
1008 if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
1009 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
1010 "R-Hash derivation");
1011 return -1;
1012 }
1013
1014 wpa_printf(MSG_DEBUG, "WPS: * R-Hash1");
1015 wpabuf_put_be16(msg, ATTR_R_HASH1);
1016 wpabuf_put_be16(msg, SHA256_MAC_LEN);
1017 hash = wpabuf_put(msg, SHA256_MAC_LEN);
1018 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
1019 addr[0] = wps->snonce;
1020 len[0] = WPS_SECRET_NONCE_LEN;
1021 addr[1] = wps->psk1;
1022 len[1] = WPS_PSK_LEN;
1023 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1024 len[2] = wpabuf_len(wps->dh_pubkey_e);
1025 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1026 len[3] = wpabuf_len(wps->dh_pubkey_r);
1027 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1028 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
1029
1030 wpa_printf(MSG_DEBUG, "WPS: * R-Hash2");
1031 wpabuf_put_be16(msg, ATTR_R_HASH2);
1032 wpabuf_put_be16(msg, SHA256_MAC_LEN);
1033 hash = wpabuf_put(msg, SHA256_MAC_LEN);
1034 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
1035 addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
1036 addr[1] = wps->psk2;
1037 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1038 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
1039
1040 return 0;
1041 }
1042
1043
1044 static int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
1045 {
1046 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce1");
1047 wpabuf_put_be16(msg, ATTR_R_SNONCE1);
1048 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1049 wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
1050 return 0;
1051 }
1052
1053
1054 static int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
1055 {
1056 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce2");
1057 wpabuf_put_be16(msg, ATTR_R_SNONCE2);
1058 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1059 wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
1060 WPS_SECRET_NONCE_LEN);
1061 return 0;
1062 }
1063
1064
1065 static int wps_build_cred_network_idx(struct wpabuf *msg,
1066 const struct wps_credential *cred)
1067 {
1068 wpa_printf(MSG_DEBUG, "WPS: * Network Index");
1069 wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
1070 wpabuf_put_be16(msg, 1);
1071 wpabuf_put_u8(msg, 1);
1072 return 0;
1073 }
1074
1075
1076 static int wps_build_cred_ssid(struct wpabuf *msg,
1077 const struct wps_credential *cred)
1078 {
1079 wpa_printf(MSG_DEBUG, "WPS: * SSID");
1080 wpabuf_put_be16(msg, ATTR_SSID);
1081 wpabuf_put_be16(msg, cred->ssid_len);
1082 wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
1083 return 0;
1084 }
1085
1086
1087 static int wps_build_cred_auth_type(struct wpabuf *msg,
1088 const struct wps_credential *cred)
1089 {
1090 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type (0x%x)",
1091 cred->auth_type);
1092 wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
1093 wpabuf_put_be16(msg, 2);
1094 wpabuf_put_be16(msg, cred->auth_type);
1095 return 0;
1096 }
1097
1098
1099 static int wps_build_cred_encr_type(struct wpabuf *msg,
1100 const struct wps_credential *cred)
1101 {
1102 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type (0x%x)",
1103 cred->encr_type);
1104 wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
1105 wpabuf_put_be16(msg, 2);
1106 wpabuf_put_be16(msg, cred->encr_type);
1107 return 0;
1108 }
1109
1110
1111 static int wps_build_cred_network_key(struct wpabuf *msg,
1112 const struct wps_credential *cred)
1113 {
1114 wpa_printf(MSG_DEBUG, "WPS: * Network Key (len=%d)",
1115 (int) cred->key_len);
1116 wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
1117 wpabuf_put_be16(msg, cred->key_len);
1118 wpabuf_put_data(msg, cred->key, cred->key_len);
1119 return 0;
1120 }
1121
1122
1123 static int wps_build_cred_mac_addr(struct wpabuf *msg,
1124 const struct wps_credential *cred)
1125 {
1126 wpa_printf(MSG_DEBUG, "WPS: * MAC Address (" MACSTR ")",
1127 MAC2STR(cred->mac_addr));
1128 wpabuf_put_be16(msg, ATTR_MAC_ADDR);
1129 wpabuf_put_be16(msg, ETH_ALEN);
1130 wpabuf_put_data(msg, cred->mac_addr, ETH_ALEN);
1131 return 0;
1132 }
1133
1134
1135 static int wps_build_credential(struct wpabuf *msg,
1136 const struct wps_credential *cred)
1137 {
1138 if (wps_build_cred_network_idx(msg, cred) ||
1139 wps_build_cred_ssid(msg, cred) ||
1140 wps_build_cred_auth_type(msg, cred) ||
1141 wps_build_cred_encr_type(msg, cred) ||
1142 wps_build_cred_network_key(msg, cred) ||
1143 wps_build_cred_mac_addr(msg, cred))
1144 return -1;
1145 return 0;
1146 }
1147
1148
1149 int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
1150 {
1151 struct wpabuf *cred;
1152
1153 if (wps->wps->registrar->skip_cred_build)
1154 goto skip_cred_build;
1155
1156 wpa_printf(MSG_DEBUG, "WPS: * Credential");
1157 if (wps->use_cred) {
1158 os_memcpy(&wps->cred, wps->use_cred, sizeof(wps->cred));
1159 goto use_provided;
1160 }
1161 os_memset(&wps->cred, 0, sizeof(wps->cred));
1162
1163 os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
1164 wps->cred.ssid_len = wps->wps->ssid_len;
1165
1166 /* Select the best authentication and encryption type */
1167 if (wps->auth_type & WPS_AUTH_WPA2PSK)
1168 wps->auth_type = WPS_AUTH_WPA2PSK;
1169 else if (wps->auth_type & WPS_AUTH_WPAPSK)
1170 wps->auth_type = WPS_AUTH_WPAPSK;
1171 else if (wps->auth_type & WPS_AUTH_OPEN)
1172 wps->auth_type = WPS_AUTH_OPEN;
1173 else if (wps->auth_type & WPS_AUTH_SHARED)
1174 wps->auth_type = WPS_AUTH_SHARED;
1175 else {
1176 wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
1177 wps->auth_type);
1178 return -1;
1179 }
1180 wps->cred.auth_type = wps->auth_type;
1181
1182 if (wps->auth_type == WPS_AUTH_WPA2PSK ||
1183 wps->auth_type == WPS_AUTH_WPAPSK) {
1184 if (wps->encr_type & WPS_ENCR_AES)
1185 wps->encr_type = WPS_ENCR_AES;
1186 else if (wps->encr_type & WPS_ENCR_TKIP)
1187 wps->encr_type = WPS_ENCR_TKIP;
1188 else {
1189 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1190 "type for WPA/WPA2");
1191 return -1;
1192 }
1193 } else {
1194 if (wps->encr_type & WPS_ENCR_WEP)
1195 wps->encr_type = WPS_ENCR_WEP;
1196 else if (wps->encr_type & WPS_ENCR_NONE)
1197 wps->encr_type = WPS_ENCR_NONE;
1198 else {
1199 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1200 "type for non-WPA/WPA2 mode");
1201 return -1;
1202 }
1203 }
1204 wps->cred.encr_type = wps->encr_type;
1205 /*
1206 * Set MAC address in the Credential to be the Enrollee's MAC address
1207 */
1208 os_memcpy(wps->cred.mac_addr, wps->mac_addr_e, ETH_ALEN);
1209
1210 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap &&
1211 !wps->wps->registrar->disable_auto_conf) {
1212 u8 r[16];
1213 /* Generate a random passphrase */
1214 if (os_get_random(r, sizeof(r)) < 0)
1215 return -1;
1216 os_free(wps->new_psk);
1217 wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
1218 if (wps->new_psk == NULL)
1219 return -1;
1220 wps->new_psk_len--; /* remove newline */
1221 while (wps->new_psk_len &&
1222 wps->new_psk[wps->new_psk_len - 1] == '=')
1223 wps->new_psk_len--;
1224 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
1225 wps->new_psk, wps->new_psk_len);
1226 os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
1227 wps->cred.key_len = wps->new_psk_len;
1228 } else if (wps->use_psk_key && wps->wps->psk_set) {
1229 char hex[65];
1230 wpa_printf(MSG_DEBUG, "WPS: Use PSK format for Network Key");
1231 wpa_snprintf_hex(hex, sizeof(hex), wps->wps->psk, 32);
1232 os_memcpy(wps->cred.key, hex, 32 * 2);
1233 wps->cred.key_len = 32 * 2;
1234 } else if (wps->wps->network_key) {
1235 os_memcpy(wps->cred.key, wps->wps->network_key,
1236 wps->wps->network_key_len);
1237 wps->cred.key_len = wps->wps->network_key_len;
1238 } else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
1239 char hex[65];
1240 /* Generate a random per-device PSK */
1241 os_free(wps->new_psk);
1242 wps->new_psk_len = 32;
1243 wps->new_psk = os_malloc(wps->new_psk_len);
1244 if (wps->new_psk == NULL)
1245 return -1;
1246 if (os_get_random(wps->new_psk, wps->new_psk_len) < 0) {
1247 os_free(wps->new_psk);
1248 wps->new_psk = NULL;
1249 return -1;
1250 }
1251 wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
1252 wps->new_psk, wps->new_psk_len);
1253 wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
1254 wps->new_psk_len);
1255 os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
1256 wps->cred.key_len = wps->new_psk_len * 2;
1257 }
1258
1259 use_provided:
1260 cred = wpabuf_alloc(200);
1261 if (cred == NULL)
1262 return -1;
1263
1264 if (wps_build_credential(cred, &wps->cred)) {
1265 wpabuf_free(cred);
1266 return -1;
1267 }
1268
1269 wpabuf_put_be16(msg, ATTR_CRED);
1270 wpabuf_put_be16(msg, wpabuf_len(cred));
1271 wpabuf_put_buf(msg, cred);
1272 wpabuf_free(cred);
1273
1274 skip_cred_build:
1275 if (wps->wps->registrar->extra_cred) {
1276 wpa_printf(MSG_DEBUG, "WPS: * Credential (pre-configured)");
1277 wpabuf_put_buf(msg, wps->wps->registrar->extra_cred);
1278 }
1279
1280 return 0;
1281 }
1282
1283
1284 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
1285 {
1286 wpa_printf(MSG_DEBUG, "WPS: * AP Settings");
1287
1288 if (wps_build_credential(msg, &wps->cred))
1289 return -1;
1290
1291 return 0;
1292 }
1293
1294
1295 static struct wpabuf * wps_build_m2(struct wps_data *wps)
1296 {
1297 struct wpabuf *msg;
1298
1299 if (os_get_random(wps->nonce_r, WPS_NONCE_LEN) < 0)
1300 return NULL;
1301 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
1302 wps->nonce_r, WPS_NONCE_LEN);
1303 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
1304
1305 wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
1306 msg = wpabuf_alloc(1000);
1307 if (msg == NULL)
1308 return NULL;
1309
1310 if (wps_build_version(msg) ||
1311 wps_build_msg_type(msg, WPS_M2) ||
1312 wps_build_enrollee_nonce(wps, msg) ||
1313 wps_build_registrar_nonce(wps, msg) ||
1314 wps_build_uuid_r(wps, msg) ||
1315 wps_build_public_key(wps, msg) ||
1316 wps_derive_keys(wps) ||
1317 wps_build_auth_type_flags(wps, msg) ||
1318 wps_build_encr_type_flags(wps, msg) ||
1319 wps_build_conn_type_flags(wps, msg) ||
1320 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1321 wps_build_device_attrs(&wps->wps->dev, msg) ||
1322 wps_build_rf_bands(&wps->wps->dev, msg) ||
1323 wps_build_assoc_state(wps, msg) ||
1324 wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
1325 wps_build_dev_password_id(msg, wps->dev_pw_id) ||
1326 wps_build_os_version(&wps->wps->dev, msg) ||
1327 wps_build_authenticator(wps, msg)) {
1328 wpabuf_free(msg);
1329 return NULL;
1330 }
1331
1332 wps->state = RECV_M3;
1333 return msg;
1334 }
1335
1336
1337 static struct wpabuf * wps_build_m2d(struct wps_data *wps)
1338 {
1339 struct wpabuf *msg;
1340 u16 err = wps->config_error;
1341
1342 wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
1343 msg = wpabuf_alloc(1000);
1344 if (msg == NULL)
1345 return NULL;
1346
1347 if (wps->wps->ap && wps->wps->ap_setup_locked &&
1348 err == WPS_CFG_NO_ERROR)
1349 err = WPS_CFG_SETUP_LOCKED;
1350
1351 if (wps_build_version(msg) ||
1352 wps_build_msg_type(msg, WPS_M2D) ||
1353 wps_build_enrollee_nonce(wps, msg) ||
1354 wps_build_registrar_nonce(wps, msg) ||
1355 wps_build_uuid_r(wps, msg) ||
1356 wps_build_auth_type_flags(wps, msg) ||
1357 wps_build_encr_type_flags(wps, msg) ||
1358 wps_build_conn_type_flags(wps, msg) ||
1359 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1360 wps_build_device_attrs(&wps->wps->dev, msg) ||
1361 wps_build_rf_bands(&wps->wps->dev, msg) ||
1362 wps_build_assoc_state(wps, msg) ||
1363 wps_build_config_error(msg, err) ||
1364 wps_build_os_version(&wps->wps->dev, msg)) {
1365 wpabuf_free(msg);
1366 return NULL;
1367 }
1368
1369 wps->state = RECV_M2D_ACK;
1370 return msg;
1371 }
1372
1373
1374 static struct wpabuf * wps_build_m4(struct wps_data *wps)
1375 {
1376 struct wpabuf *msg, *plain;
1377
1378 wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
1379
1380 wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
1381
1382 plain = wpabuf_alloc(200);
1383 if (plain == NULL)
1384 return NULL;
1385
1386 msg = wpabuf_alloc(1000);
1387 if (msg == NULL) {
1388 wpabuf_free(plain);
1389 return NULL;
1390 }
1391
1392 if (wps_build_version(msg) ||
1393 wps_build_msg_type(msg, WPS_M4) ||
1394 wps_build_enrollee_nonce(wps, msg) ||
1395 wps_build_r_hash(wps, msg) ||
1396 wps_build_r_snonce1(wps, plain) ||
1397 wps_build_key_wrap_auth(wps, plain) ||
1398 wps_build_encr_settings(wps, msg, plain) ||
1399 wps_build_authenticator(wps, msg)) {
1400 wpabuf_free(plain);
1401 wpabuf_free(msg);
1402 return NULL;
1403 }
1404 wpabuf_free(plain);
1405
1406 wps->state = RECV_M5;
1407 return msg;
1408 }
1409
1410
1411 static struct wpabuf * wps_build_m6(struct wps_data *wps)
1412 {
1413 struct wpabuf *msg, *plain;
1414
1415 wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
1416
1417 plain = wpabuf_alloc(200);
1418 if (plain == NULL)
1419 return NULL;
1420
1421 msg = wpabuf_alloc(1000);
1422 if (msg == NULL) {
1423 wpabuf_free(plain);
1424 return NULL;
1425 }
1426
1427 if (wps_build_version(msg) ||
1428 wps_build_msg_type(msg, WPS_M6) ||
1429 wps_build_enrollee_nonce(wps, msg) ||
1430 wps_build_r_snonce2(wps, plain) ||
1431 wps_build_key_wrap_auth(wps, plain) ||
1432 wps_build_encr_settings(wps, msg, plain) ||
1433 wps_build_authenticator(wps, msg)) {
1434 wpabuf_free(plain);
1435 wpabuf_free(msg);
1436 return NULL;
1437 }
1438 wpabuf_free(plain);
1439
1440 wps->wps_pin_revealed = 1;
1441 wps->state = RECV_M7;
1442 return msg;
1443 }
1444
1445
1446 static struct wpabuf * wps_build_m8(struct wps_data *wps)
1447 {
1448 struct wpabuf *msg, *plain;
1449
1450 wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
1451
1452 plain = wpabuf_alloc(500);
1453 if (plain == NULL)
1454 return NULL;
1455
1456 msg = wpabuf_alloc(1000);
1457 if (msg == NULL) {
1458 wpabuf_free(plain);
1459 return NULL;
1460 }
1461
1462 if (wps_build_version(msg) ||
1463 wps_build_msg_type(msg, WPS_M8) ||
1464 wps_build_enrollee_nonce(wps, msg) ||
1465 ((wps->wps->ap || wps->er) && wps_build_cred(wps, plain)) ||
1466 (!wps->wps->ap && !wps->er && wps_build_ap_settings(wps, plain)) ||
1467 wps_build_key_wrap_auth(wps, plain) ||
1468 wps_build_encr_settings(wps, msg, plain) ||
1469 wps_build_authenticator(wps, msg)) {
1470 wpabuf_free(plain);
1471 wpabuf_free(msg);
1472 return NULL;
1473 }
1474 wpabuf_free(plain);
1475
1476 wps->state = RECV_DONE;
1477 return msg;
1478 }
1479
1480
1481 static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
1482 {
1483 struct wpabuf *msg;
1484
1485 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
1486
1487 msg = wpabuf_alloc(1000);
1488 if (msg == NULL)
1489 return NULL;
1490
1491 if (wps_build_version(msg) ||
1492 wps_build_msg_type(msg, WPS_WSC_ACK) ||
1493 wps_build_enrollee_nonce(wps, msg) ||
1494 wps_build_registrar_nonce(wps, msg)) {
1495 wpabuf_free(msg);
1496 return NULL;
1497 }
1498
1499 return msg;
1500 }
1501
1502
1503 static struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
1504 {
1505 struct wpabuf *msg;
1506
1507 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
1508
1509 msg = wpabuf_alloc(1000);
1510 if (msg == NULL)
1511 return NULL;
1512
1513 if (wps_build_version(msg) ||
1514 wps_build_msg_type(msg, WPS_WSC_NACK) ||
1515 wps_build_enrollee_nonce(wps, msg) ||
1516 wps_build_registrar_nonce(wps, msg) ||
1517 wps_build_config_error(msg, wps->config_error)) {
1518 wpabuf_free(msg);
1519 return NULL;
1520 }
1521
1522 return msg;
1523 }
1524
1525
1526 struct wpabuf * wps_registrar_get_msg(struct wps_data *wps,
1527 enum wsc_op_code *op_code)
1528 {
1529 struct wpabuf *msg;
1530
1531 #ifdef CONFIG_WPS_UPNP
1532 if (wps->wps->wps_upnp) {
1533 struct upnp_pending_message *p, *prev = NULL;
1534 if (wps->ext_reg > 1)
1535 wps_registrar_free_pending_m2(wps->wps);
1536 p = wps->wps->upnp_msgs;
1537 /* TODO: check pending message MAC address */
1538 while (p && p->next) {
1539 prev = p;
1540 p = p->next;
1541 }
1542 if (p) {
1543 wpa_printf(MSG_DEBUG, "WPS: Use pending message from "
1544 "UPnP");
1545 if (prev)
1546 prev->next = NULL;
1547 else
1548 wps->wps->upnp_msgs = NULL;
1549 msg = p->msg;
1550 switch (p->type) {
1551 case WPS_WSC_ACK:
1552 *op_code = WSC_ACK;
1553 break;
1554 case WPS_WSC_NACK:
1555 *op_code = WSC_NACK;
1556 break;
1557 default:
1558 *op_code = WSC_MSG;
1559 break;
1560 }
1561 os_free(p);
1562 if (wps->ext_reg == 0)
1563 wps->ext_reg = 1;
1564 return msg;
1565 }
1566 }
1567 if (wps->ext_reg) {
1568 wpa_printf(MSG_DEBUG, "WPS: Using external Registrar, but no "
1569 "pending message available");
1570 return NULL;
1571 }
1572 #endif /* CONFIG_WPS_UPNP */
1573
1574 switch (wps->state) {
1575 case SEND_M2:
1576 if (wps_get_dev_password(wps) < 0)
1577 msg = wps_build_m2d(wps);
1578 else
1579 msg = wps_build_m2(wps);
1580 *op_code = WSC_MSG;
1581 break;
1582 case SEND_M2D:
1583 msg = wps_build_m2d(wps);
1584 *op_code = WSC_MSG;
1585 break;
1586 case SEND_M4:
1587 msg = wps_build_m4(wps);
1588 *op_code = WSC_MSG;
1589 break;
1590 case SEND_M6:
1591 msg = wps_build_m6(wps);
1592 *op_code = WSC_MSG;
1593 break;
1594 case SEND_M8:
1595 msg = wps_build_m8(wps);
1596 *op_code = WSC_MSG;
1597 break;
1598 case RECV_DONE:
1599 msg = wps_build_wsc_ack(wps);
1600 *op_code = WSC_ACK;
1601 break;
1602 case SEND_WSC_NACK:
1603 msg = wps_build_wsc_nack(wps);
1604 *op_code = WSC_NACK;
1605 break;
1606 default:
1607 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
1608 "a message", wps->state);
1609 msg = NULL;
1610 break;
1611 }
1612
1613 if (*op_code == WSC_MSG && msg) {
1614 /* Save a copy of the last message for Authenticator derivation
1615 */
1616 wpabuf_free(wps->last_msg);
1617 wps->last_msg = wpabuf_dup(msg);
1618 }
1619
1620 return msg;
1621 }
1622
1623
1624 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
1625 {
1626 if (e_nonce == NULL) {
1627 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
1628 return -1;
1629 }
1630
1631 os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
1632 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
1633 wps->nonce_e, WPS_NONCE_LEN);
1634
1635 return 0;
1636 }
1637
1638
1639 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
1640 {
1641 if (r_nonce == NULL) {
1642 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
1643 return -1;
1644 }
1645
1646 if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
1647 wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
1648 return -1;
1649 }
1650
1651 return 0;
1652 }
1653
1654
1655 static int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
1656 {
1657 if (uuid_e == NULL) {
1658 wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
1659 return -1;
1660 }
1661
1662 os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
1663 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
1664
1665 return 0;
1666 }
1667
1668
1669 static int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
1670 {
1671 if (pw_id == NULL) {
1672 wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
1673 return -1;
1674 }
1675
1676 wps->dev_pw_id = WPA_GET_BE16(pw_id);
1677 wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
1678
1679 return 0;
1680 }
1681
1682
1683 static int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
1684 {
1685 if (e_hash1 == NULL) {
1686 wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
1687 return -1;
1688 }
1689
1690 os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
1691 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
1692
1693 return 0;
1694 }
1695
1696
1697 static int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
1698 {
1699 if (e_hash2 == NULL) {
1700 wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
1701 return -1;
1702 }
1703
1704 os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
1705 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
1706
1707 return 0;
1708 }
1709
1710
1711 static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
1712 {
1713 u8 hash[SHA256_MAC_LEN];
1714 const u8 *addr[4];
1715 size_t len[4];
1716
1717 if (e_snonce1 == NULL) {
1718 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
1719 return -1;
1720 }
1721
1722 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
1723 WPS_SECRET_NONCE_LEN);
1724
1725 /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
1726 addr[0] = e_snonce1;
1727 len[0] = WPS_SECRET_NONCE_LEN;
1728 addr[1] = wps->psk1;
1729 len[1] = WPS_PSK_LEN;
1730 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1731 len[2] = wpabuf_len(wps->dh_pubkey_e);
1732 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1733 len[3] = wpabuf_len(wps->dh_pubkey_r);
1734 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1735
1736 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
1737 wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
1738 "not match with the pre-committed value");
1739 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1740 wps_pwd_auth_fail_event(wps->wps, 0, 1);
1741 return -1;
1742 }
1743
1744 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
1745 "half of the device password");
1746
1747 return 0;
1748 }
1749
1750
1751 static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
1752 {
1753 u8 hash[SHA256_MAC_LEN];
1754 const u8 *addr[4];
1755 size_t len[4];
1756
1757 if (e_snonce2 == NULL) {
1758 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
1759 return -1;
1760 }
1761
1762 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
1763 WPS_SECRET_NONCE_LEN);
1764
1765 /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
1766 addr[0] = e_snonce2;
1767 len[0] = WPS_SECRET_NONCE_LEN;
1768 addr[1] = wps->psk2;
1769 len[1] = WPS_PSK_LEN;
1770 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1771 len[2] = wpabuf_len(wps->dh_pubkey_e);
1772 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1773 len[3] = wpabuf_len(wps->dh_pubkey_r);
1774 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1775
1776 if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
1777 wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
1778 "not match with the pre-committed value");
1779 wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
1780 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1781 wps_pwd_auth_fail_event(wps->wps, 0, 2);
1782 return -1;
1783 }
1784
1785 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
1786 "half of the device password");
1787 wps->wps_pin_revealed = 0;
1788 wps_registrar_unlock_pin(wps->wps->registrar, wps->uuid_e);
1789
1790 return 0;
1791 }
1792
1793
1794 static int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
1795 {
1796 if (mac_addr == NULL) {
1797 wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
1798 return -1;
1799 }
1800
1801 wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
1802 MAC2STR(mac_addr));
1803 os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
1804 os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
1805
1806 return 0;
1807 }
1808
1809
1810 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
1811 size_t pk_len)
1812 {
1813 if (pk == NULL || pk_len == 0) {
1814 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
1815 return -1;
1816 }
1817
1818 #ifdef CONFIG_WPS_OOB
1819 if (wps->wps->oob_conf.pubkey_hash != NULL) {
1820 const u8 *addr[1];
1821 u8 hash[WPS_HASH_LEN];
1822
1823 addr[0] = pk;
1824 sha256_vector(1, addr, &pk_len, hash);
1825 if (os_memcmp(hash,
1826 wpabuf_head(wps->wps->oob_conf.pubkey_hash),
1827 WPS_OOB_PUBKEY_HASH_LEN) != 0) {
1828 wpa_printf(MSG_ERROR, "WPS: Public Key hash error");
1829 return -1;
1830 }
1831 }
1832 #endif /* CONFIG_WPS_OOB */
1833
1834 wpabuf_free(wps->dh_pubkey_e);
1835 wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
1836 if (wps->dh_pubkey_e == NULL)
1837 return -1;
1838
1839 return 0;
1840 }
1841
1842
1843 static int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
1844 {
1845 u16 auth_types;
1846
1847 if (auth == NULL) {
1848 wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
1849 "received");
1850 return -1;
1851 }
1852
1853 auth_types = WPA_GET_BE16(auth);
1854
1855 wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
1856 auth_types);
1857 wps->auth_type = wps->wps->auth_types & auth_types;
1858 if (wps->auth_type == 0) {
1859 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1860 "authentication types (own 0x%x Enrollee 0x%x)",
1861 wps->wps->auth_types, auth_types);
1862 #ifdef WPS_WORKAROUNDS
1863 /*
1864 * Some deployed implementations seem to advertise incorrect
1865 * information in this attribute. For example, Linksys WRT350N
1866 * seems to have a byteorder bug that breaks this negotiation.
1867 * In order to interoperate with existing implementations,
1868 * assume that the Enrollee supports everything we do.
1869 */
1870 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
1871 "does not advertise supported authentication types "
1872 "correctly");
1873 wps->auth_type = wps->wps->auth_types;
1874 #else /* WPS_WORKAROUNDS */
1875 return -1;
1876 #endif /* WPS_WORKAROUNDS */
1877 }
1878
1879 return 0;
1880 }
1881
1882
1883 static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
1884 {
1885 u16 encr_types;
1886
1887 if (encr == NULL) {
1888 wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
1889 "received");
1890 return -1;
1891 }
1892
1893 encr_types = WPA_GET_BE16(encr);
1894
1895 wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
1896 encr_types);
1897 wps->encr_type = wps->wps->encr_types & encr_types;
1898 if (wps->encr_type == 0) {
1899 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1900 "encryption types (own 0x%x Enrollee 0x%x)",
1901 wps->wps->encr_types, encr_types);
1902 #ifdef WPS_WORKAROUNDS
1903 /*
1904 * Some deployed implementations seem to advertise incorrect
1905 * information in this attribute. For example, Linksys WRT350N
1906 * seems to have a byteorder bug that breaks this negotiation.
1907 * In order to interoperate with existing implementations,
1908 * assume that the Enrollee supports everything we do.
1909 */
1910 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
1911 "does not advertise supported encryption types "
1912 "correctly");
1913 wps->encr_type = wps->wps->encr_types;
1914 #else /* WPS_WORKAROUNDS */
1915 return -1;
1916 #endif /* WPS_WORKAROUNDS */
1917 }
1918
1919 return 0;
1920 }
1921
1922
1923 static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
1924 {
1925 if (conn == NULL) {
1926 wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
1927 "received");
1928 return -1;
1929 }
1930
1931 wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
1932 *conn);
1933
1934 return 0;
1935 }
1936
1937
1938 static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
1939 {
1940 u16 m;
1941
1942 if (methods == NULL) {
1943 wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
1944 return -1;
1945 }
1946
1947 m = WPA_GET_BE16(methods);
1948
1949 wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x"
1950 "%s%s%s%s%s%s%s%s%s", m,
1951 m & WPS_CONFIG_USBA ? " [USBA]" : "",
1952 m & WPS_CONFIG_ETHERNET ? " [Ethernet]" : "",
1953 m & WPS_CONFIG_LABEL ? " [Label]" : "",
1954 m & WPS_CONFIG_DISPLAY ? " [Display]" : "",
1955 m & WPS_CONFIG_EXT_NFC_TOKEN ? " [Ext NFC Token]" : "",
1956 m & WPS_CONFIG_INT_NFC_TOKEN ? " [Int NFC Token]" : "",
1957 m & WPS_CONFIG_NFC_INTERFACE ? " [NFC]" : "",
1958 m & WPS_CONFIG_PUSHBUTTON ? " [PBC]" : "",
1959 m & WPS_CONFIG_KEYPAD ? " [Keypad]" : "");
1960
1961 if (!(m & WPS_CONFIG_DISPLAY) && !wps->use_psk_key) {
1962 /*
1963 * The Enrollee does not have a display so it is unlikely to be
1964 * able to show the passphrase to a user and as such, could
1965 * benefit from receiving PSK to reduce key derivation time.
1966 */
1967 wpa_printf(MSG_DEBUG, "WPS: Prefer PSK format key due to "
1968 "Enrollee not supporting display");
1969 wps->use_psk_key = 1;
1970 }
1971
1972 return 0;
1973 }
1974
1975
1976 static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
1977 {
1978 if (state == NULL) {
1979 wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
1980 "received");
1981 return -1;
1982 }
1983
1984 wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
1985 *state);
1986
1987 return 0;
1988 }
1989
1990
1991 static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
1992 {
1993 u16 a;
1994
1995 if (assoc == NULL) {
1996 wpa_printf(MSG_DEBUG, "WPS: No Association State received");
1997 return -1;
1998 }
1999
2000 a = WPA_GET_BE16(assoc);
2001 wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
2002
2003 return 0;
2004 }
2005
2006
2007 static int wps_process_config_error(struct wps_data *wps, const u8 *err)
2008 {
2009 u16 e;
2010
2011 if (err == NULL) {
2012 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
2013 return -1;
2014 }
2015
2016 e = WPA_GET_BE16(err);
2017 wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
2018
2019 return 0;
2020 }
2021
2022
2023 static enum wps_process_res wps_process_m1(struct wps_data *wps,
2024 struct wps_parse_attr *attr)
2025 {
2026 wpa_printf(MSG_DEBUG, "WPS: Received M1");
2027
2028 if (wps->state != RECV_M1) {
2029 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2030 "receiving M1", wps->state);
2031 return WPS_FAILURE;
2032 }
2033
2034 if (wps_process_uuid_e(wps, attr->uuid_e) ||
2035 wps_process_mac_addr(wps, attr->mac_addr) ||
2036 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
2037 wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
2038 wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
2039 wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
2040 wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
2041 wps_process_config_methods(wps, attr->config_methods) ||
2042 wps_process_wps_state(wps, attr->wps_state) ||
2043 wps_process_device_attrs(&wps->peer_dev, attr) ||
2044 wps_process_rf_bands(&wps->peer_dev, attr->rf_bands) ||
2045 wps_process_assoc_state(wps, attr->assoc_state) ||
2046 wps_process_dev_password_id(wps, attr->dev_password_id) ||
2047 wps_process_config_error(wps, attr->config_error) ||
2048 wps_process_os_version(&wps->peer_dev, attr->os_version))
2049 return WPS_FAILURE;
2050
2051 if (wps->dev_pw_id < 0x10 &&
2052 wps->dev_pw_id != DEV_PW_DEFAULT &&
2053 wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
2054 wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
2055 wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
2056 (wps->dev_pw_id != DEV_PW_PUSHBUTTON ||
2057 !wps->wps->registrar->pbc)) {
2058 wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
2059 wps->dev_pw_id);
2060 wps->state = SEND_M2D;
2061 return WPS_CONTINUE;
2062 }
2063
2064 #ifdef CONFIG_WPS_OOB
2065 if (wps->dev_pw_id >= 0x10 &&
2066 wps->dev_pw_id != wps->wps->oob_dev_pw_id) {
2067 wpa_printf(MSG_DEBUG, "WPS: OOB Device Password ID "
2068 "%d mismatch", wps->dev_pw_id);
2069 wps->state = SEND_M2D;
2070 return WPS_CONTINUE;
2071 }
2072 #endif /* CONFIG_WPS_OOB */
2073
2074 if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
2075 if (wps->wps->registrar->force_pbc_overlap ||
2076 wps_registrar_pbc_overlap(wps->wps->registrar,
2077 wps->mac_addr_e, wps->uuid_e)) {
2078 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
2079 "negotiation");
2080 wps->state = SEND_M2D;
2081 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2082 wps_pbc_overlap_event(wps->wps);
2083 wps->wps->registrar->force_pbc_overlap = 1;
2084 return WPS_CONTINUE;
2085 }
2086 wps_registrar_add_pbc_session(wps->wps->registrar,
2087 wps->mac_addr_e, wps->uuid_e);
2088 wps->pbc = 1;
2089 }
2090
2091 wps->state = SEND_M2;
2092 return WPS_CONTINUE;
2093 }
2094
2095
2096 static enum wps_process_res wps_process_m3(struct wps_data *wps,
2097 const struct wpabuf *msg,
2098 struct wps_parse_attr *attr)
2099 {
2100 wpa_printf(MSG_DEBUG, "WPS: Received M3");
2101
2102 if (wps->state != RECV_M3) {
2103 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2104 "receiving M3", wps->state);
2105 wps->state = SEND_WSC_NACK;
2106 return WPS_CONTINUE;
2107 }
2108
2109 if (wps->pbc && wps->wps->registrar->force_pbc_overlap) {
2110 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2111 "session overlap");
2112 wps->state = SEND_WSC_NACK;
2113 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2114 return WPS_CONTINUE;
2115 }
2116
2117 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2118 wps_process_authenticator(wps, attr->authenticator, msg) ||
2119 wps_process_e_hash1(wps, attr->e_hash1) ||
2120 wps_process_e_hash2(wps, attr->e_hash2)) {
2121 wps->state = SEND_WSC_NACK;
2122 return WPS_CONTINUE;
2123 }
2124
2125 wps->state = SEND_M4;
2126 return WPS_CONTINUE;
2127 }
2128
2129
2130 static enum wps_process_res wps_process_m5(struct wps_data *wps,
2131 const struct wpabuf *msg,
2132 struct wps_parse_attr *attr)
2133 {
2134 struct wpabuf *decrypted;
2135 struct wps_parse_attr eattr;
2136
2137 wpa_printf(MSG_DEBUG, "WPS: Received M5");
2138
2139 if (wps->state != RECV_M5) {
2140 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2141 "receiving M5", wps->state);
2142 wps->state = SEND_WSC_NACK;
2143 return WPS_CONTINUE;
2144 }
2145
2146 if (wps->pbc && wps->wps->registrar->force_pbc_overlap) {
2147 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2148 "session overlap");
2149 wps->state = SEND_WSC_NACK;
2150 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2151 return WPS_CONTINUE;
2152 }
2153
2154 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2155 wps_process_authenticator(wps, attr->authenticator, msg)) {
2156 wps->state = SEND_WSC_NACK;
2157 return WPS_CONTINUE;
2158 }
2159
2160 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2161 attr->encr_settings_len);
2162 if (decrypted == NULL) {
2163 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
2164 "Settings attribute");
2165 wps->state = SEND_WSC_NACK;
2166 return WPS_CONTINUE;
2167 }
2168
2169 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2170 "attribute");
2171 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2172 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2173 wps_process_e_snonce1(wps, eattr.e_snonce1)) {
2174 wpabuf_free(decrypted);
2175 wps->state = SEND_WSC_NACK;
2176 return WPS_CONTINUE;
2177 }
2178 wpabuf_free(decrypted);
2179
2180 wps->state = SEND_M6;
2181 return WPS_CONTINUE;
2182 }
2183
2184
2185 static void wps_sta_cred_cb(struct wps_data *wps)
2186 {
2187 /*
2188 * Update credential to only include a single authentication and
2189 * encryption type in case the AP configuration includes more than one
2190 * option.
2191 */
2192 if (wps->cred.auth_type & WPS_AUTH_WPA2PSK)
2193 wps->cred.auth_type = WPS_AUTH_WPA2PSK;
2194 else if (wps->cred.auth_type & WPS_AUTH_WPAPSK)
2195 wps->cred.auth_type = WPS_AUTH_WPAPSK;
2196 if (wps->cred.encr_type & WPS_ENCR_AES)
2197 wps->cred.encr_type = WPS_ENCR_AES;
2198 else if (wps->cred.encr_type & WPS_ENCR_TKIP)
2199 wps->cred.encr_type = WPS_ENCR_TKIP;
2200 wpa_printf(MSG_DEBUG, "WPS: Update local configuration based on the "
2201 "AP configuration");
2202 if (wps->wps->cred_cb)
2203 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
2204 }
2205
2206
2207 static void wps_cred_update(struct wps_credential *dst,
2208 struct wps_credential *src)
2209 {
2210 os_memcpy(dst->ssid, src->ssid, sizeof(dst->ssid));
2211 dst->ssid_len = src->ssid_len;
2212 dst->auth_type = src->auth_type;
2213 dst->encr_type = src->encr_type;
2214 dst->key_idx = src->key_idx;
2215 os_memcpy(dst->key, src->key, sizeof(dst->key));
2216 dst->key_len = src->key_len;
2217 }
2218
2219
2220 static int wps_process_ap_settings_r(struct wps_data *wps,
2221 struct wps_parse_attr *attr)
2222 {
2223 if (wps->wps->ap || wps->er)
2224 return 0;
2225
2226 /* AP Settings Attributes in M7 when Enrollee is an AP */
2227 if (wps_process_ap_settings(attr, &wps->cred) < 0)
2228 return -1;
2229
2230 wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
2231
2232 if (wps->new_ap_settings) {
2233 wpa_printf(MSG_INFO, "WPS: Update AP configuration based on "
2234 "new settings");
2235 wps_cred_update(&wps->cred, wps->new_ap_settings);
2236 return 0;
2237 } else {
2238 /*
2239 * Use the AP PIN only to receive the current AP settings, not
2240 * to reconfigure the AP.
2241 */
2242 if (wps->ap_settings_cb) {
2243 wps->ap_settings_cb(wps->ap_settings_cb_ctx,
2244 &wps->cred);
2245 return 1;
2246 }
2247 wps_sta_cred_cb(wps);
2248 return 1;
2249 }
2250 }
2251
2252
2253 static enum wps_process_res wps_process_m7(struct wps_data *wps,
2254 const struct wpabuf *msg,
2255 struct wps_parse_attr *attr)
2256 {
2257 struct wpabuf *decrypted;
2258 struct wps_parse_attr eattr;
2259
2260 wpa_printf(MSG_DEBUG, "WPS: Received M7");
2261
2262 if (wps->state != RECV_M7) {
2263 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2264 "receiving M7", wps->state);
2265 wps->state = SEND_WSC_NACK;
2266 return WPS_CONTINUE;
2267 }
2268
2269 if (wps->pbc && wps->wps->registrar->force_pbc_overlap) {
2270 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2271 "session overlap");
2272 wps->state = SEND_WSC_NACK;
2273 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2274 return WPS_CONTINUE;
2275 }
2276
2277 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2278 wps_process_authenticator(wps, attr->authenticator, msg)) {
2279 wps->state = SEND_WSC_NACK;
2280 return WPS_CONTINUE;
2281 }
2282
2283 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2284 attr->encr_settings_len);
2285 if (decrypted == NULL) {
2286 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypt Encrypted "
2287 "Settings attribute");
2288 wps->state = SEND_WSC_NACK;
2289 return WPS_CONTINUE;
2290 }
2291
2292 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2293 "attribute");
2294 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2295 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2296 wps_process_e_snonce2(wps, eattr.e_snonce2) ||
2297 wps_process_ap_settings_r(wps, &eattr)) {
2298 wpabuf_free(decrypted);
2299 wps->state = SEND_WSC_NACK;
2300 return WPS_CONTINUE;
2301 }
2302
2303 wpabuf_free(decrypted);
2304
2305 wps->state = SEND_M8;
2306 return WPS_CONTINUE;
2307 }
2308
2309
2310 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
2311 const struct wpabuf *msg)
2312 {
2313 struct wps_parse_attr attr;
2314 enum wps_process_res ret = WPS_CONTINUE;
2315
2316 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
2317
2318 if (wps_parse_msg(msg, &attr) < 0)
2319 return WPS_FAILURE;
2320
2321 if (!wps_version_supported(attr.version)) {
2322 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2323 attr.version ? *attr.version : 0);
2324 return WPS_FAILURE;
2325 }
2326
2327 if (attr.msg_type == NULL) {
2328 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2329 return WPS_FAILURE;
2330 }
2331
2332 if (*attr.msg_type != WPS_M1 &&
2333 (attr.registrar_nonce == NULL ||
2334 os_memcmp(wps->nonce_r, attr.registrar_nonce,
2335 WPS_NONCE_LEN != 0))) {
2336 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2337 return WPS_FAILURE;
2338 }
2339
2340 switch (*attr.msg_type) {
2341 case WPS_M1:
2342 #ifdef CONFIG_WPS_UPNP
2343 if (wps->wps->wps_upnp && attr.mac_addr) {
2344 /* Remove old pending messages when starting new run */
2345 wps_free_pending_msgs(wps->wps->upnp_msgs);
2346 wps->wps->upnp_msgs = NULL;
2347
2348 upnp_wps_device_send_wlan_event(
2349 wps->wps->wps_upnp, attr.mac_addr,
2350 UPNP_WPS_WLANEVENT_TYPE_EAP, msg);
2351 }
2352 #endif /* CONFIG_WPS_UPNP */
2353 ret = wps_process_m1(wps, &attr);
2354 break;
2355 case WPS_M3:
2356 ret = wps_process_m3(wps, msg, &attr);
2357 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2358 wps_fail_event(wps->wps, WPS_M3);
2359 break;
2360 case WPS_M5:
2361 ret = wps_process_m5(wps, msg, &attr);
2362 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2363 wps_fail_event(wps->wps, WPS_M5);
2364 break;
2365 case WPS_M7:
2366 ret = wps_process_m7(wps, msg, &attr);
2367 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2368 wps_fail_event(wps->wps, WPS_M7);
2369 break;
2370 default:
2371 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
2372 *attr.msg_type);
2373 return WPS_FAILURE;
2374 }
2375
2376 if (ret == WPS_CONTINUE) {
2377 /* Save a copy of the last message for Authenticator derivation
2378 */
2379 wpabuf_free(wps->last_msg);
2380 wps->last_msg = wpabuf_dup(msg);
2381 }
2382
2383 return ret;
2384 }
2385
2386
2387 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
2388 const struct wpabuf *msg)
2389 {
2390 struct wps_parse_attr attr;
2391
2392 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
2393
2394 if (wps_parse_msg(msg, &attr) < 0)
2395 return WPS_FAILURE;
2396
2397 if (!wps_version_supported(attr.version)) {
2398 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2399 attr.version ? *attr.version : 0);
2400 return WPS_FAILURE;
2401 }
2402
2403 if (attr.msg_type == NULL) {
2404 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2405 return WPS_FAILURE;
2406 }
2407
2408 if (*attr.msg_type != WPS_WSC_ACK) {
2409 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2410 *attr.msg_type);
2411 return WPS_FAILURE;
2412 }
2413
2414 #ifdef CONFIG_WPS_UPNP
2415 if (wps->wps->wps_upnp && wps->ext_reg && wps->state == RECV_M2D_ACK &&
2416 upnp_wps_subscribers(wps->wps->wps_upnp)) {
2417 if (wps->wps->upnp_msgs)
2418 return WPS_CONTINUE;
2419 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2420 "external Registrar");
2421 return WPS_PENDING;
2422 }
2423 #endif /* CONFIG_WPS_UPNP */
2424
2425 if (attr.registrar_nonce == NULL ||
2426 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2427 {
2428 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2429 return WPS_FAILURE;
2430 }
2431
2432 if (attr.enrollee_nonce == NULL ||
2433 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2434 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2435 return WPS_FAILURE;
2436 }
2437
2438 if (wps->state == RECV_M2D_ACK) {
2439 #ifdef CONFIG_WPS_UPNP
2440 if (wps->wps->wps_upnp &&
2441 upnp_wps_subscribers(wps->wps->wps_upnp)) {
2442 if (wps->wps->upnp_msgs)
2443 return WPS_CONTINUE;
2444 if (wps->ext_reg == 0)
2445 wps->ext_reg = 1;
2446 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2447 "external Registrar");
2448 return WPS_PENDING;
2449 }
2450 #endif /* CONFIG_WPS_UPNP */
2451
2452 wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
2453 "terminate negotiation");
2454 }
2455
2456 return WPS_FAILURE;
2457 }
2458
2459
2460 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
2461 const struct wpabuf *msg)
2462 {
2463 struct wps_parse_attr attr;
2464 int old_state;
2465
2466 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
2467
2468 old_state = wps->state;
2469 wps->state = SEND_WSC_NACK;
2470
2471 if (wps_parse_msg(msg, &attr) < 0)
2472 return WPS_FAILURE;
2473
2474 if (!wps_version_supported(attr.version)) {
2475 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2476 attr.version ? *attr.version : 0);
2477 return WPS_FAILURE;
2478 }
2479
2480 if (attr.msg_type == NULL) {
2481 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2482 return WPS_FAILURE;
2483 }
2484
2485 if (*attr.msg_type != WPS_WSC_NACK) {
2486 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2487 *attr.msg_type);
2488 return WPS_FAILURE;
2489 }
2490
2491 #ifdef CONFIG_WPS_UPNP
2492 if (wps->wps->wps_upnp && wps->ext_reg) {
2493 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
2494 "Registrar terminated by the Enrollee");
2495 return WPS_FAILURE;
2496 }
2497 #endif /* CONFIG_WPS_UPNP */
2498
2499 if (attr.registrar_nonce == NULL ||
2500 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2501 {
2502 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2503 return WPS_FAILURE;
2504 }
2505
2506 if (attr.enrollee_nonce == NULL ||
2507 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2508 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2509 return WPS_FAILURE;
2510 }
2511
2512 if (attr.config_error == NULL) {
2513 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
2514 "in WSC_NACK");
2515 return WPS_FAILURE;
2516 }
2517
2518 wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
2519 "Configuration Error %d", WPA_GET_BE16(attr.config_error));
2520
2521 switch (old_state) {
2522 case RECV_M3:
2523 wps_fail_event(wps->wps, WPS_M2);
2524 break;
2525 case RECV_M5:
2526 wps_fail_event(wps->wps, WPS_M4);
2527 break;
2528 case RECV_M7:
2529 wps_fail_event(wps->wps, WPS_M6);
2530 break;
2531 case RECV_DONE:
2532 wps_fail_event(wps->wps, WPS_M8);
2533 break;
2534 default:
2535 break;
2536 }
2537
2538 return WPS_FAILURE;
2539 }
2540
2541
2542 static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
2543 const struct wpabuf *msg)
2544 {
2545 struct wps_parse_attr attr;
2546
2547 wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
2548
2549 if (wps->state != RECV_DONE &&
2550 (!wps->wps->wps_upnp || !wps->ext_reg)) {
2551 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2552 "receiving WSC_Done", wps->state);
2553 return WPS_FAILURE;
2554 }
2555
2556 if (wps_parse_msg(msg, &attr) < 0)
2557 return WPS_FAILURE;
2558
2559 if (!wps_version_supported(attr.version)) {
2560 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2561 attr.version ? *attr.version : 0);
2562 return WPS_FAILURE;
2563 }
2564
2565 if (attr.msg_type == NULL) {
2566 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2567 return WPS_FAILURE;
2568 }
2569
2570 if (*attr.msg_type != WPS_WSC_DONE) {
2571 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2572 *attr.msg_type);
2573 return WPS_FAILURE;
2574 }
2575
2576 #ifdef CONFIG_WPS_UPNP
2577 if (wps->wps->wps_upnp && wps->ext_reg) {
2578 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
2579 "Registrar completed successfully");
2580 wps_device_store(wps->wps->registrar, &wps->peer_dev,
2581 wps->uuid_e);
2582 return WPS_DONE;
2583 }
2584 #endif /* CONFIG_WPS_UPNP */
2585
2586 if (attr.registrar_nonce == NULL ||
2587 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2588 {
2589 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2590 return WPS_FAILURE;
2591 }
2592
2593 if (attr.enrollee_nonce == NULL ||
2594 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2595 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2596 return WPS_FAILURE;
2597 }
2598
2599 wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
2600 wps_device_store(wps->wps->registrar, &wps->peer_dev,
2601 wps->uuid_e);
2602
2603 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
2604 wps->wps->ap && !wps->wps->registrar->disable_auto_conf) {
2605 struct wps_credential cred;
2606
2607 wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
2608 "on first Enrollee connection");
2609
2610 os_memset(&cred, 0, sizeof(cred));
2611 os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
2612 cred.ssid_len = wps->wps->ssid_len;
2613 cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
2614 cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
2615 os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
2616 cred.key_len = wps->new_psk_len;
2617
2618 wps->wps->wps_state = WPS_STATE_CONFIGURED;
2619 wpa_hexdump_ascii_key(MSG_DEBUG,
2620 "WPS: Generated random passphrase",
2621 wps->new_psk, wps->new_psk_len);
2622 if (wps->wps->cred_cb)
2623 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
2624
2625 os_free(wps->new_psk);
2626 wps->new_psk = NULL;
2627 }
2628
2629 if (!wps->wps->ap && !wps->er)
2630 wps_sta_cred_cb(wps);
2631
2632 if (wps->new_psk) {
2633 if (wps_cb_new_psk(wps->wps->registrar, wps->mac_addr_e,
2634 wps->new_psk, wps->new_psk_len)) {
2635 wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
2636 "new PSK");
2637 }
2638 os_free(wps->new_psk);
2639 wps->new_psk = NULL;
2640 }
2641
2642 wps_cb_reg_success(wps->wps->registrar, wps->mac_addr_e, wps->uuid_e);
2643
2644 if (wps->pbc) {
2645 wps_registrar_remove_pbc_session(wps->wps->registrar,
2646 wps->mac_addr_e, wps->uuid_e);
2647 wps_registrar_pbc_completed(wps->wps->registrar);
2648 } else {
2649 wps_registrar_pin_completed(wps->wps->registrar);
2650 }
2651
2652 wps_success_event(wps->wps);
2653
2654 return WPS_DONE;
2655 }
2656
2657
2658 enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
2659 enum wsc_op_code op_code,
2660 const struct wpabuf *msg)
2661 {
2662 enum wps_process_res ret;
2663
2664 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
2665 "op_code=%d)",
2666 (unsigned long) wpabuf_len(msg), op_code);
2667
2668 #ifdef CONFIG_WPS_UPNP
2669 if (wps->wps->wps_upnp && op_code == WSC_MSG && wps->ext_reg == 1) {
2670 struct wps_parse_attr attr;
2671 if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type &&
2672 *attr.msg_type == WPS_M3)
2673 wps->ext_reg = 2; /* past M2/M2D phase */
2674 }
2675 if (wps->ext_reg > 1)
2676 wps_registrar_free_pending_m2(wps->wps);
2677 if (wps->wps->wps_upnp && wps->ext_reg &&
2678 wps->wps->upnp_msgs == NULL &&
2679 (op_code == WSC_MSG || op_code == WSC_Done || op_code == WSC_NACK))
2680 {
2681 struct wps_parse_attr attr;
2682 int type;
2683 if (wps_parse_msg(msg, &attr) < 0 || attr.msg_type == NULL)
2684 type = -1;
2685 else
2686 type = *attr.msg_type;
2687 wpa_printf(MSG_DEBUG, "WPS: Sending received message (type %d)"
2688 " to external Registrar for processing", type);
2689 upnp_wps_device_send_wlan_event(wps->wps->wps_upnp,
2690 wps->mac_addr_e,
2691 UPNP_WPS_WLANEVENT_TYPE_EAP,
2692 msg);
2693 if (op_code == WSC_MSG)
2694 return WPS_PENDING;
2695 } else if (wps->wps->wps_upnp && wps->ext_reg && op_code == WSC_MSG) {
2696 wpa_printf(MSG_DEBUG, "WPS: Skip internal processing - using "
2697 "external Registrar");
2698 return WPS_CONTINUE;
2699 }
2700 #endif /* CONFIG_WPS_UPNP */
2701
2702 switch (op_code) {
2703 case WSC_MSG:
2704 return wps_process_wsc_msg(wps, msg);
2705 case WSC_ACK:
2706 return wps_process_wsc_ack(wps, msg);
2707 case WSC_NACK:
2708 return wps_process_wsc_nack(wps, msg);
2709 case WSC_Done:
2710 ret = wps_process_wsc_done(wps, msg);
2711 if (ret == WPS_FAILURE) {
2712 wps->state = SEND_WSC_NACK;
2713 wps_fail_event(wps->wps, WPS_WSC_DONE);
2714 }
2715 return ret;
2716 default:
2717 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
2718 return WPS_FAILURE;
2719 }
2720 }
2721
2722
2723 int wps_registrar_update_ie(struct wps_registrar *reg)
2724 {
2725 return wps_set_ie(reg);
2726 }
2727
2728
2729 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
2730 void *timeout_ctx)
2731 {
2732 struct wps_registrar *reg = eloop_ctx;
2733
2734 wpa_printf(MSG_DEBUG, "WPS: Selected Registrar timeout - "
2735 "unselect internal Registrar");
2736 reg->selected_registrar = 0;
2737 reg->pbc = 0;
2738 wps_registrar_selected_registrar_changed(reg);
2739 }
2740
2741
2742 #ifdef CONFIG_WPS_UPNP
2743 static void wps_registrar_sel_reg_add(struct wps_registrar *reg,
2744 struct subscription *s)
2745 {
2746 wpa_printf(MSG_DEBUG, "WPS: External Registrar selected (dev_pw_id=%d "
2747 "config_methods=0x%x)",
2748 s->dev_password_id, s->config_methods);
2749 reg->sel_reg_union = 1;
2750 if (reg->sel_reg_dev_password_id_override != DEV_PW_PUSHBUTTON)
2751 reg->sel_reg_dev_password_id_override = s->dev_password_id;
2752 if (reg->sel_reg_config_methods_override == -1)
2753 reg->sel_reg_config_methods_override = 0;
2754 reg->sel_reg_config_methods_override |= s->config_methods;
2755 }
2756 #endif /* CONFIG_WPS_UPNP */
2757
2758
2759 static void wps_registrar_sel_reg_union(struct wps_registrar *reg)
2760 {
2761 #ifdef CONFIG_WPS_UPNP
2762 struct subscription *s;
2763
2764 if (reg->wps->wps_upnp == NULL)
2765 return;
2766
2767 dl_list_for_each(s, &reg->wps->wps_upnp->subscriptions,
2768 struct subscription, list) {
2769 struct subscr_addr *sa;
2770 sa = dl_list_first(&s->addr_list, struct subscr_addr, list);
2771 if (sa) {
2772 wpa_printf(MSG_DEBUG, "WPS: External Registrar %s:%d",
2773 inet_ntoa(sa->saddr.sin_addr),
2774 ntohs(sa->saddr.sin_port));
2775 }
2776 if (s->selected_registrar)
2777 wps_registrar_sel_reg_add(reg, s);
2778 else
2779 wpa_printf(MSG_DEBUG, "WPS: External Registrar not "
2780 "selected");
2781 }
2782 #endif /* CONFIG_WPS_UPNP */
2783 }
2784
2785
2786 /**
2787 * wps_registrar_selected_registrar_changed - SetSelectedRegistrar change
2788 * @reg: Registrar data from wps_registrar_init()
2789 *
2790 * This function is called when selected registrar state changes, e.g., when an
2791 * AP receives a SetSelectedRegistrar UPnP message.
2792 */
2793 void wps_registrar_selected_registrar_changed(struct wps_registrar *reg)
2794 {
2795 wpa_printf(MSG_DEBUG, "WPS: Selected registrar information changed");
2796
2797 reg->sel_reg_union = reg->selected_registrar;
2798 reg->sel_reg_dev_password_id_override = -1;
2799 reg->sel_reg_config_methods_override = -1;
2800 if (reg->selected_registrar) {
2801 reg->sel_reg_config_methods_override =
2802 reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
2803 if (reg->pbc) {
2804 reg->sel_reg_dev_password_id_override =
2805 DEV_PW_PUSHBUTTON;
2806 reg->sel_reg_config_methods_override |=
2807 WPS_CONFIG_PUSHBUTTON;
2808 }
2809 wpa_printf(MSG_DEBUG, "WPS: Internal Registrar selected "
2810 "(pbc=%d)", reg->pbc);
2811 } else
2812 wpa_printf(MSG_DEBUG, "WPS: Internal Registrar not selected");
2813
2814 wps_registrar_sel_reg_union(reg);
2815
2816 wps_set_ie(reg);
2817 wps_cb_set_sel_reg(reg);
2818 }
2819
2820
2821 int wps_registrar_get_info(struct wps_registrar *reg, const u8 *addr,
2822 char *buf, size_t buflen)
2823 {
2824 struct wps_registrar_device *d;
2825 int len = 0, ret;
2826 char uuid[40];
2827 char devtype[WPS_DEV_TYPE_BUFSIZE];
2828
2829 d = wps_device_get(reg, addr);
2830 if (d == NULL)
2831 return 0;
2832 if (uuid_bin2str(d->uuid, uuid, sizeof(uuid)))
2833 return 0;
2834
2835 ret = os_snprintf(buf + len, buflen - len,
2836 "wpsUuid=%s\n"
2837 "wpsPrimaryDeviceType=%s\n"
2838 "wpsDeviceName=%s\n"
2839 "wpsManufacturer=%s\n"
2840 "wpsModelName=%s\n"
2841 "wpsModelNumber=%s\n"
2842 "wpsSerialNumber=%s\n",
2843 uuid,
2844 wps_dev_type_bin2str(d->dev.pri_dev_type, devtype,
2845 sizeof(devtype)),
2846 d->dev.device_name ? d->dev.device_name : "",
2847 d->dev.manufacturer ? d->dev.manufacturer : "",
2848 d->dev.model_name ? d->dev.model_name : "",
2849 d->dev.model_number ? d->dev.model_number : "",
2850 d->dev.serial_number ? d->dev.serial_number : "");
2851 if (ret < 0 || (size_t) ret >= buflen - len)
2852 return len;
2853 len += ret;
2854
2855 return len;
2856 }