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