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