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