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