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