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