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