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