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