Coverage Report

Created: 2025-10-28 06:59

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