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