Coverage Report

Created: 2025-11-03 06:51

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hostap/src/ap/wpa_auth_ft.c
Line
Count
Source
1
/*
2
 * hostapd - IEEE 802.11r - Fast BSS Transition
3
 * Copyright (c) 2004-2018, 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/eloop.h"
13
#include "utils/list.h"
14
#include "common/ieee802_11_defs.h"
15
#include "common/ieee802_11_common.h"
16
#include "common/ocv.h"
17
#include "common/wpa_ctrl.h"
18
#include "drivers/driver.h"
19
#include "crypto/aes.h"
20
#include "crypto/aes_siv.h"
21
#include "crypto/aes_wrap.h"
22
#include "crypto/sha384.h"
23
#include "crypto/sha512.h"
24
#include "crypto/random.h"
25
#include "ap_config.h"
26
#include "ieee802_11.h"
27
#include "wmm.h"
28
#include "wpa_auth.h"
29
#include "wpa_auth_i.h"
30
#include "pmksa_cache_auth.h"
31
32
33
#ifdef CONFIG_IEEE80211R_AP
34
35
const unsigned int ftRRBseqTimeout = 10;
36
const unsigned int ftRRBmaxQueueLen = 100;
37
38
/* TODO: make these configurable */
39
static const int dot11RSNAConfigPMKLifetime = 43200;
40
41
42
static int wpa_ft_send_rrb_auth_resp(struct wpa_state_machine *sm,
43
             const u8 *current_ap, const u8 *sta_addr,
44
             u16 status, const u8 *resp_ies,
45
             size_t resp_ies_len);
46
static void ft_finish_pull(struct wpa_state_machine *sm);
47
static void wpa_ft_expire_pull(void *eloop_ctx, void *timeout_ctx);
48
static void wpa_ft_rrb_seq_timeout(void *eloop_ctx, void *timeout_ctx);
49
50
struct tlv_list {
51
  u16 type;
52
  size_t len;
53
  const u8 *data;
54
};
55
56
57
/**
58
 * wpa_ft_rrb_decrypt - Decrypt FT RRB message
59
 * @key: AES-SIV key for AEAD
60
 * @key_len: Length of key in octets
61
 * @enc: Pointer to encrypted TLVs
62
 * @enc_len: Length of encrypted TLVs in octets
63
 * @auth: Pointer to authenticated TLVs
64
 * @auth_len: Length of authenticated TLVs in octets
65
 * @src_addr: MAC address of the frame sender
66
 * @type: Vendor-specific subtype of the RRB frame (FT_PACKET_*)
67
 * @plain: Pointer to return the pointer to the allocated plaintext buffer;
68
 *  needs to be freed by the caller if not NULL;
69
 *  will only be returned on success
70
 * @plain_len: Pointer to return the length of the allocated plaintext buffer
71
 *  in octets
72
 * Returns: 0 on success, -1 on error
73
 */
74
static int wpa_ft_rrb_decrypt(const u8 *key, const size_t key_len,
75
            const u8 *enc, size_t enc_len,
76
            const u8 *auth, const size_t auth_len,
77
            const u8 *src_addr, u8 type,
78
            u8 **plain, size_t *plain_size)
79
0
{
80
0
  const u8 *ad[3] = { src_addr, auth, &type };
81
0
  size_t ad_len[3] = { ETH_ALEN, auth_len, sizeof(type) };
82
83
0
  wpa_printf(MSG_DEBUG, "FT(RRB): src_addr=" MACSTR " type=%u",
84
0
       MAC2STR(src_addr), type);
85
0
  wpa_hexdump_key(MSG_DEBUG, "FT(RRB): decrypt using key", key, key_len);
86
0
  wpa_hexdump(MSG_DEBUG, "FT(RRB): encrypted TLVs", enc, enc_len);
87
0
  wpa_hexdump(MSG_DEBUG, "FT(RRB): authenticated TLVs", auth, auth_len);
88
89
0
  if (!key) { /* skip decryption */
90
0
    *plain = os_memdup(enc, enc_len);
91
0
    if (enc_len > 0 && !*plain)
92
0
      goto err;
93
94
0
    *plain_size = enc_len;
95
96
0
    return 0;
97
0
  }
98
99
0
  *plain = NULL;
100
101
  /* SIV overhead */
102
0
  if (enc_len < AES_BLOCK_SIZE)
103
0
    goto err;
104
105
0
  *plain = os_zalloc(enc_len - AES_BLOCK_SIZE);
106
0
  if (!*plain)
107
0
    goto err;
108
109
0
  if (aes_siv_decrypt(key, key_len, enc, enc_len, 3, ad, ad_len,
110
0
          *plain) < 0) {
111
0
    if (enc_len < AES_BLOCK_SIZE + 2)
112
0
      goto err;
113
114
    /* Try to work around Ethernet devices that add extra
115
     * two octet padding even if the frame is longer than
116
     * the minimum Ethernet frame. */
117
0
    enc_len -= 2;
118
0
    if (aes_siv_decrypt(key, key_len, enc, enc_len, 3, ad, ad_len,
119
0
            *plain) < 0)
120
0
      goto err;
121
0
  }
122
123
0
  *plain_size = enc_len - AES_BLOCK_SIZE;
124
0
  wpa_hexdump_key(MSG_DEBUG, "FT(RRB): decrypted TLVs",
125
0
      *plain, *plain_size);
126
0
  return 0;
127
0
err:
128
0
  os_free(*plain);
129
0
  *plain = NULL;
130
0
  *plain_size = 0;
131
132
0
  wpa_printf(MSG_ERROR, "FT(RRB): Failed to decrypt");
133
134
0
  return -1;
135
0
}
136
137
138
/* get first tlv record in packet matching type
139
 * @data (decrypted) packet
140
 * @return 0 on success else -1
141
 */
142
static int wpa_ft_rrb_get_tlv(const u8 *plain, size_t plain_len,
143
            u16 type, size_t *tlv_len, const u8 **tlv_data)
144
0
{
145
0
  const struct ft_rrb_tlv *f;
146
0
  size_t left;
147
0
  le16 type16;
148
0
  size_t len;
149
150
0
  left = plain_len;
151
0
  type16 = host_to_le16(type);
152
153
0
  while (left >= sizeof(*f)) {
154
0
    f = (const struct ft_rrb_tlv *) plain;
155
156
0
    left -= sizeof(*f);
157
0
    plain += sizeof(*f);
158
0
    len = le_to_host16(f->len);
159
160
0
    if (left < len) {
161
0
      wpa_printf(MSG_DEBUG, "FT: RRB message truncated");
162
0
      break;
163
0
    }
164
165
0
    if (f->type == type16) {
166
0
      *tlv_len = len;
167
0
      *tlv_data = plain;
168
0
      return 0;
169
0
    }
170
171
0
    left -= len;
172
0
    plain += len;
173
0
  }
174
175
0
  return -1;
176
0
}
177
178
179
static void wpa_ft_rrb_dump(const u8 *plain, const size_t plain_len)
180
0
{
181
0
  const struct ft_rrb_tlv *f;
182
0
  size_t left;
183
0
  size_t len;
184
185
0
  left = plain_len;
186
187
0
  wpa_printf(MSG_DEBUG, "FT: RRB dump message");
188
0
  while (left >= sizeof(*f)) {
189
0
    f = (const struct ft_rrb_tlv *) plain;
190
191
0
    left -= sizeof(*f);
192
0
    plain += sizeof(*f);
193
0
    len = le_to_host16(f->len);
194
195
0
    wpa_printf(MSG_DEBUG, "FT: RRB TLV type = %d, len = %zu",
196
0
         le_to_host16(f->type), len);
197
198
0
    if (left < len) {
199
0
      wpa_printf(MSG_DEBUG,
200
0
           "FT: RRB message truncated: left %zu bytes, need %zu",
201
0
           left, len);
202
0
      break;
203
0
    }
204
205
0
    wpa_hexdump(MSG_DEBUG, "FT: RRB TLV data", plain, len);
206
207
0
    left -= len;
208
0
    plain += len;
209
0
  }
210
211
0
  if (left > 0)
212
0
    wpa_hexdump(MSG_DEBUG, "FT: RRB TLV padding", plain, left);
213
214
0
  wpa_printf(MSG_DEBUG, "FT: RRB dump message end");
215
0
}
216
217
218
static int cmp_int(const void *a, const void *b)
219
0
{
220
0
  int x, y;
221
222
0
  x = *((int *) a);
223
0
  y = *((int *) b);
224
0
  return x - y;
225
0
}
226
227
228
static int wpa_ft_rrb_get_tlv_vlan(const u8 *plain, const size_t plain_len,
229
           struct vlan_description *vlan)
230
0
{
231
0
  struct ft_rrb_tlv *f;
232
0
  size_t left;
233
0
  size_t len;
234
0
  int taggedidx;
235
0
  int vlan_id;
236
0
  int type;
237
238
0
  left = plain_len;
239
0
  taggedidx = 0;
240
0
  os_memset(vlan, 0, sizeof(*vlan));
241
242
0
  while (left >= sizeof(*f)) {
243
0
    f = (struct ft_rrb_tlv *) plain;
244
245
0
    left -= sizeof(*f);
246
0
    plain += sizeof(*f);
247
248
0
    len = le_to_host16(f->len);
249
0
    type = le_to_host16(f->type);
250
251
0
    if (left < len) {
252
0
      wpa_printf(MSG_DEBUG, "FT: RRB message truncated");
253
0
      return -1;
254
0
    }
255
256
0
    if (type != FT_RRB_VLAN_UNTAGGED && type != FT_RRB_VLAN_TAGGED)
257
0
      goto skip;
258
259
0
    if (type == FT_RRB_VLAN_UNTAGGED && len != sizeof(le16)) {
260
0
      wpa_printf(MSG_DEBUG,
261
0
           "FT: RRB VLAN_UNTAGGED invalid length");
262
0
      return -1;
263
0
    }
264
265
0
    if (type == FT_RRB_VLAN_TAGGED && len % sizeof(le16) != 0) {
266
0
      wpa_printf(MSG_DEBUG,
267
0
           "FT: RRB VLAN_TAGGED invalid length");
268
0
      return -1;
269
0
    }
270
271
0
    while (len >= sizeof(le16)) {
272
0
      vlan_id = WPA_GET_LE16(plain);
273
0
      plain += sizeof(le16);
274
0
      left -= sizeof(le16);
275
0
      len -= sizeof(le16);
276
277
0
      if (vlan_id <= 0 || vlan_id > MAX_VLAN_ID) {
278
0
        wpa_printf(MSG_DEBUG,
279
0
             "FT: RRB VLAN ID invalid %d",
280
0
             vlan_id);
281
0
        continue;
282
0
      }
283
284
0
      if (type == FT_RRB_VLAN_UNTAGGED)
285
0
        vlan->untagged = vlan_id;
286
287
0
      if (type == FT_RRB_VLAN_TAGGED &&
288
0
          taggedidx < MAX_NUM_TAGGED_VLAN) {
289
0
        vlan->tagged[taggedidx] = vlan_id;
290
0
        taggedidx++;
291
0
      } else if (type == FT_RRB_VLAN_TAGGED) {
292
0
        wpa_printf(MSG_DEBUG, "FT: RRB too many VLANs");
293
0
      }
294
0
    }
295
296
0
  skip:
297
0
    left -= len;
298
0
    plain += len;
299
0
  }
300
301
0
  if (taggedidx)
302
0
    qsort(vlan->tagged, taggedidx, sizeof(int), cmp_int);
303
304
0
  vlan->notempty = vlan->untagged || vlan->tagged[0];
305
306
0
  return 0;
307
0
}
308
309
310
static size_t wpa_ft_tlv_len(const struct tlv_list *tlvs)
311
0
{
312
0
  size_t tlv_len = 0;
313
0
  int i;
314
315
0
  if (!tlvs)
316
0
    return 0;
317
318
0
  for (i = 0; tlvs[i].type != FT_RRB_LAST_EMPTY; i++) {
319
0
    tlv_len += sizeof(struct ft_rrb_tlv);
320
0
    tlv_len += tlvs[i].len;
321
0
  }
322
323
0
  return tlv_len;
324
0
}
325
326
327
static size_t wpa_ft_tlv_lin(const struct tlv_list *tlvs, u8 *start,
328
           u8 *endpos)
329
0
{
330
0
  int i;
331
0
  size_t tlv_len;
332
0
  struct ft_rrb_tlv *hdr;
333
0
  u8 *pos;
334
335
0
  if (!tlvs)
336
0
    return 0;
337
338
0
  tlv_len = 0;
339
0
  pos = start;
340
0
  for (i = 0; tlvs[i].type != FT_RRB_LAST_EMPTY; i++) {
341
0
    if (tlv_len + sizeof(*hdr) > (size_t) (endpos - start))
342
0
      return tlv_len;
343
0
    tlv_len += sizeof(*hdr);
344
0
    hdr = (struct ft_rrb_tlv *) pos;
345
0
    hdr->type = host_to_le16(tlvs[i].type);
346
0
    hdr->len = host_to_le16(tlvs[i].len);
347
0
    pos = start + tlv_len;
348
349
0
    if (tlv_len + tlvs[i].len > (size_t) (endpos - start))
350
0
      return tlv_len;
351
0
    if (tlvs[i].len == 0)
352
0
      continue;
353
0
    tlv_len += tlvs[i].len;
354
0
    os_memcpy(pos, tlvs[i].data, tlvs[i].len);
355
0
    pos = start + tlv_len;
356
0
  }
357
358
0
  return tlv_len;
359
0
}
360
361
362
static size_t wpa_ft_vlan_len(const struct vlan_description *vlan)
363
0
{
364
0
  size_t tlv_len = 0;
365
0
  int i;
366
367
0
  if (!vlan || !vlan->notempty)
368
0
    return 0;
369
370
0
  if (vlan->untagged) {
371
0
    tlv_len += sizeof(struct ft_rrb_tlv);
372
0
    tlv_len += sizeof(le16);
373
0
  }
374
0
  if (vlan->tagged[0])
375
0
    tlv_len += sizeof(struct ft_rrb_tlv);
376
0
  for (i = 0; i < MAX_NUM_TAGGED_VLAN && vlan->tagged[i]; i++)
377
0
    tlv_len += sizeof(le16);
378
379
0
  return tlv_len;
380
0
}
381
382
383
static size_t wpa_ft_vlan_lin(const struct vlan_description *vlan,
384
            u8 *start, u8 *endpos)
385
0
{
386
0
  size_t tlv_len;
387
0
  int i, len;
388
0
  struct ft_rrb_tlv *hdr;
389
0
  u8 *pos = start;
390
391
0
  if (!vlan || !vlan->notempty)
392
0
    return 0;
393
394
0
  tlv_len = 0;
395
0
  if (vlan->untagged) {
396
0
    tlv_len += sizeof(*hdr);
397
0
    if (start + tlv_len > endpos)
398
0
      return tlv_len;
399
0
    hdr = (struct ft_rrb_tlv *) pos;
400
0
    hdr->type = host_to_le16(FT_RRB_VLAN_UNTAGGED);
401
0
    hdr->len = host_to_le16(sizeof(le16));
402
0
    pos = start + tlv_len;
403
404
0
    tlv_len += sizeof(le16);
405
0
    if (start + tlv_len > endpos)
406
0
      return tlv_len;
407
0
    WPA_PUT_LE16(pos, vlan->untagged);
408
0
    pos = start + tlv_len;
409
0
  }
410
411
0
  if (!vlan->tagged[0])
412
0
    return tlv_len;
413
414
0
  tlv_len += sizeof(*hdr);
415
0
  if (start + tlv_len > endpos)
416
0
    return tlv_len;
417
0
  hdr = (struct ft_rrb_tlv *) pos;
418
0
  hdr->type = host_to_le16(FT_RRB_VLAN_TAGGED);
419
0
  len = 0; /* len is computed below */
420
0
  pos = start + tlv_len;
421
422
0
  for (i = 0; i < MAX_NUM_TAGGED_VLAN && vlan->tagged[i]; i++) {
423
0
    tlv_len += sizeof(le16);
424
0
    if (start + tlv_len > endpos)
425
0
      break;
426
0
    len += sizeof(le16);
427
0
    WPA_PUT_LE16(pos, vlan->tagged[i]);
428
0
    pos = start + tlv_len;
429
0
  }
430
431
0
  hdr->len = host_to_le16(len);
432
433
0
  return tlv_len;
434
0
}
435
436
437
static int wpa_ft_rrb_lin(const struct tlv_list *tlvs1,
438
        const struct tlv_list *tlvs2,
439
        const struct vlan_description *vlan,
440
        u8 **plain, size_t *plain_len)
441
0
{
442
0
  u8 *pos, *endpos;
443
0
  size_t tlv_len;
444
445
0
  tlv_len = wpa_ft_tlv_len(tlvs1);
446
0
  tlv_len += wpa_ft_tlv_len(tlvs2);
447
0
  tlv_len += wpa_ft_vlan_len(vlan);
448
449
0
  *plain_len = tlv_len;
450
0
  *plain = os_zalloc(tlv_len);
451
0
  if (!*plain) {
452
0
    wpa_printf(MSG_ERROR, "FT: Failed to allocate plaintext");
453
0
    goto err;
454
0
  }
455
456
0
  pos = *plain;
457
0
  endpos = *plain + tlv_len;
458
0
  pos += wpa_ft_tlv_lin(tlvs1, pos, endpos);
459
0
  pos += wpa_ft_tlv_lin(tlvs2, pos, endpos);
460
0
  pos += wpa_ft_vlan_lin(vlan, pos, endpos);
461
462
  /* validity check */
463
0
  if (pos != endpos) {
464
0
    wpa_printf(MSG_ERROR, "FT: Length error building RRB");
465
0
    goto err;
466
0
  }
467
468
0
  return 0;
469
470
0
err:
471
0
  os_free(*plain);
472
0
  *plain = NULL;
473
0
  *plain_len = 0;
474
0
  return -1;
475
0
}
476
477
478
static int wpa_ft_rrb_encrypt(const u8 *key, const size_t key_len,
479
            const u8 *plain, const size_t plain_len,
480
            const u8 *auth, const size_t auth_len,
481
            const u8 *src_addr, u8 type, u8 *enc)
482
0
{
483
0
  const u8 *ad[3] = { src_addr, auth, &type };
484
0
  size_t ad_len[3] = { ETH_ALEN, auth_len, sizeof(type) };
485
486
0
  wpa_printf(MSG_DEBUG, "FT(RRB): src_addr=" MACSTR " type=%u",
487
0
       MAC2STR(src_addr), type);
488
0
  wpa_hexdump_key(MSG_DEBUG, "FT(RRB): plaintext message",
489
0
      plain, plain_len);
490
0
  wpa_hexdump_key(MSG_DEBUG, "FT(RRB): encrypt using key", key, key_len);
491
0
  wpa_hexdump(MSG_DEBUG, "FT(RRB): authenticated TLVs", auth, auth_len);
492
493
0
  if (!key) {
494
    /* encryption not needed, return plaintext as packet */
495
0
    os_memcpy(enc, plain, plain_len);
496
0
  } else if (aes_siv_encrypt(key, key_len, plain, plain_len,
497
0
           3, ad, ad_len, enc) < 0) {
498
0
    wpa_printf(MSG_ERROR, "FT: Failed to encrypt RRB-OUI message");
499
0
    return -1;
500
0
  }
501
0
  wpa_hexdump(MSG_DEBUG, "FT(RRB): encrypted TLVs",
502
0
        enc, plain_len + AES_BLOCK_SIZE);
503
504
0
  return 0;
505
0
}
506
507
508
/**
509
 * wpa_ft_rrb_build - Build and encrypt an FT RRB message
510
 * @key: AES-SIV key for AEAD
511
 * @key_len: Length of key in octets
512
 * @tlvs_enc0: First set of to-be-encrypted TLVs
513
 * @tlvs_enc1: Second set of to-be-encrypted TLVs
514
 * @tlvs_auth: Set of to-be-authenticated TLVs
515
 * @src_addr: MAC address of the frame sender
516
 * @type: Vendor-specific subtype of the RRB frame (FT_PACKET_*)
517
 * @packet Pointer to return the pointer to the allocated packet buffer;
518
 *         needs to be freed by the caller if not null;
519
 *         will only be returned on success
520
 * @packet_len: Pointer to return the length of the allocated buffer in octets
521
 * Returns: 0 on success, -1 on error
522
 */
523
static int wpa_ft_rrb_build(const u8 *key, const size_t key_len,
524
          const struct tlv_list *tlvs_enc0,
525
          const struct tlv_list *tlvs_enc1,
526
          const struct tlv_list *tlvs_auth,
527
          const struct vlan_description *vlan,
528
          const u8 *src_addr, u8 type,
529
          u8 **packet, size_t *packet_len)
530
0
{
531
0
  u8 *plain = NULL, *auth = NULL, *pos, *tmp;
532
0
  size_t plain_len = 0, auth_len = 0;
533
0
  int ret = -1;
534
0
  size_t pad_len = 0;
535
536
0
  *packet = NULL;
537
0
  if (wpa_ft_rrb_lin(tlvs_enc0, tlvs_enc1, vlan, &plain, &plain_len) < 0)
538
0
    goto out;
539
540
0
  if (wpa_ft_rrb_lin(tlvs_auth, NULL, NULL, &auth, &auth_len) < 0)
541
0
    goto out;
542
543
0
  *packet_len = sizeof(u16) + auth_len + plain_len;
544
0
  if (key)
545
0
    *packet_len += AES_BLOCK_SIZE;
546
0
#define RRB_MIN_MSG_LEN 64
547
0
  if (*packet_len < RRB_MIN_MSG_LEN) {
548
0
    pad_len = RRB_MIN_MSG_LEN - *packet_len;
549
0
    if (pad_len < sizeof(struct ft_rrb_tlv))
550
0
      pad_len = sizeof(struct ft_rrb_tlv);
551
0
    wpa_printf(MSG_DEBUG,
552
0
         "FT: Pad message to minimum Ethernet frame length (%d --> %d)",
553
0
         (int) *packet_len, (int) (*packet_len + pad_len));
554
0
    *packet_len += pad_len;
555
0
    tmp = os_realloc(auth, auth_len + pad_len);
556
0
    if (!tmp)
557
0
      goto out;
558
0
    auth = tmp;
559
0
    pos = auth + auth_len;
560
0
    WPA_PUT_LE16(pos, FT_RRB_LAST_EMPTY);
561
0
    pos += 2;
562
0
    WPA_PUT_LE16(pos, pad_len - sizeof(struct ft_rrb_tlv));
563
0
    pos += 2;
564
0
    os_memset(pos, 0, pad_len - sizeof(struct ft_rrb_tlv));
565
0
    auth_len += pad_len;
566
567
0
  }
568
0
  *packet = os_zalloc(*packet_len);
569
0
  if (!*packet)
570
0
    goto out;
571
572
0
  pos = *packet;
573
0
  WPA_PUT_LE16(pos, auth_len);
574
0
  pos += 2;
575
0
  os_memcpy(pos, auth, auth_len);
576
0
  pos += auth_len;
577
0
  if (wpa_ft_rrb_encrypt(key, key_len, plain, plain_len, auth,
578
0
             auth_len, src_addr, type, pos) < 0)
579
0
    goto out;
580
0
  wpa_hexdump(MSG_MSGDUMP, "FT: RRB frame payload", *packet, *packet_len);
581
582
0
  ret = 0;
583
584
0
out:
585
0
  bin_clear_free(plain, plain_len);
586
0
  os_free(auth);
587
588
0
  if (ret) {
589
0
    wpa_printf(MSG_ERROR, "FT: Failed to build RRB-OUI message");
590
0
    os_free(*packet);
591
0
    *packet = NULL;
592
0
    *packet_len = 0;
593
0
  }
594
595
0
  return ret;
596
0
}
597
598
599
0
#define RRB_GET_SRC(srcfield, type, field, txt, checklength) do { \
600
0
  if (wpa_ft_rrb_get_tlv(srcfield, srcfield##_len, type, \
601
0
        &f_##field##_len, &f_##field) < 0 || \
602
0
      (checklength > 0 && ((size_t) checklength) != f_##field##_len)) { \
603
0
    wpa_printf(MSG_INFO, "FT: Missing required " #field \
604
0
         " in %s from " MACSTR, txt, MAC2STR(src_addr)); \
605
0
    wpa_ft_rrb_dump(srcfield, srcfield##_len); \
606
0
    goto out; \
607
0
  } \
608
0
} while (0)
609
610
#define RRB_GET(type, field, txt, checklength) \
611
0
  RRB_GET_SRC(plain, type, field, txt, checklength)
612
#define RRB_GET_AUTH(type, field, txt, checklength) \
613
0
  RRB_GET_SRC(auth, type, field, txt, checklength)
614
615
0
#define RRB_GET_OPTIONAL_SRC(srcfield, type, field, txt, checklength) do { \
616
0
  if (wpa_ft_rrb_get_tlv(srcfield, srcfield##_len, type, \
617
0
        &f_##field##_len, &f_##field) < 0 || \
618
0
      (checklength > 0 && ((size_t) checklength) != f_##field##_len)) { \
619
0
    wpa_printf(MSG_DEBUG, "FT: Missing optional " #field \
620
0
         " in %s from " MACSTR, txt, MAC2STR(src_addr)); \
621
0
    f_##field##_len = 0; \
622
0
    f_##field = NULL; \
623
0
  } \
624
0
} while (0)
625
626
#define RRB_GET_OPTIONAL(type, field, txt, checklength) \
627
0
  RRB_GET_OPTIONAL_SRC(plain, type, field, txt, checklength)
628
#define RRB_GET_OPTIONAL_AUTH(type, field, txt, checklength) \
629
  RRB_GET_OPTIONAL_SRC(auth, type, field, txt, checklength)
630
631
static int wpa_ft_rrb_send(struct wpa_authenticator *wpa_auth, const u8 *dst,
632
         const u8 *data, size_t data_len)
633
0
{
634
0
  if (wpa_auth->cb->send_ether == NULL)
635
0
    return -1;
636
0
  wpa_printf(MSG_DEBUG, "FT: RRB send to " MACSTR, MAC2STR(dst));
637
0
  return wpa_auth->cb->send_ether(wpa_auth->cb_ctx, dst, ETH_P_RRB,
638
0
          data, data_len);
639
0
}
640
641
642
static int wpa_ft_rrb_oui_send(struct wpa_authenticator *wpa_auth,
643
             const u8 *dst, u8 oui_suffix,
644
             const u8 *data, size_t data_len)
645
0
{
646
0
  if (!wpa_auth->cb->send_oui)
647
0
    return -1;
648
0
  wpa_printf(MSG_DEBUG, "FT: RRB-OUI type %u send to " MACSTR " (len=%u)",
649
0
       oui_suffix, MAC2STR(dst), (unsigned int) data_len);
650
0
  return wpa_auth->cb->send_oui(wpa_auth->cb_ctx, dst, oui_suffix, data,
651
0
              data_len);
652
0
}
653
654
655
static int wpa_ft_action_send(struct wpa_authenticator *wpa_auth,
656
            const u8 *dst, const u8 *data, size_t data_len)
657
0
{
658
0
  if (wpa_auth->cb->send_ft_action == NULL)
659
0
    return -1;
660
0
  return wpa_auth->cb->send_ft_action(wpa_auth->cb_ctx, dst,
661
0
              data, data_len);
662
0
}
663
664
665
static const u8 * wpa_ft_get_psk(struct wpa_authenticator *wpa_auth,
666
         const u8 *addr, const u8 *p2p_dev_addr,
667
         const u8 *prev_psk)
668
0
{
669
0
  if (wpa_auth->cb->get_psk == NULL)
670
0
    return NULL;
671
0
  return wpa_auth->cb->get_psk(wpa_auth->cb_ctx, addr, p2p_dev_addr,
672
0
             prev_psk, NULL, NULL);
673
0
}
674
675
676
static struct wpa_state_machine *
677
wpa_ft_add_sta(struct wpa_authenticator *wpa_auth, const u8 *sta_addr)
678
0
{
679
0
  if (wpa_auth->cb->add_sta == NULL)
680
0
    return NULL;
681
0
  return wpa_auth->cb->add_sta(wpa_auth->cb_ctx, sta_addr);
682
0
}
683
684
685
static int wpa_ft_set_vlan(struct wpa_authenticator *wpa_auth,
686
         const u8 *sta_addr, struct vlan_description *vlan)
687
0
{
688
0
  if (!wpa_auth->cb->set_vlan)
689
0
    return -1;
690
0
  return wpa_auth->cb->set_vlan(wpa_auth->cb_ctx, sta_addr, vlan);
691
0
}
692
693
694
static int wpa_ft_get_vlan(struct wpa_authenticator *wpa_auth,
695
         const u8 *sta_addr, struct vlan_description *vlan)
696
0
{
697
0
  if (!wpa_auth->cb->get_vlan)
698
0
    return -1;
699
0
  return wpa_auth->cb->get_vlan(wpa_auth->cb_ctx, sta_addr, vlan);
700
0
}
701
702
703
static int
704
wpa_ft_set_identity(struct wpa_authenticator *wpa_auth, const u8 *sta_addr,
705
        const u8 *identity, size_t identity_len)
706
0
{
707
0
  if (!wpa_auth->cb->set_identity)
708
0
    return -1;
709
0
  return wpa_auth->cb->set_identity(wpa_auth->cb_ctx, sta_addr, identity,
710
0
            identity_len);
711
0
}
712
713
714
static size_t
715
wpa_ft_get_identity(struct wpa_authenticator *wpa_auth, const u8 *sta_addr,
716
        const u8 **buf)
717
0
{
718
0
  *buf = NULL;
719
0
  if (!wpa_auth->cb->get_identity)
720
0
    return 0;
721
0
  return wpa_auth->cb->get_identity(wpa_auth->cb_ctx, sta_addr, buf);
722
0
}
723
724
725
static int
726
wpa_ft_set_radius_cui(struct wpa_authenticator *wpa_auth, const u8 *sta_addr,
727
          const u8 *radius_cui, size_t radius_cui_len)
728
0
{
729
0
  if (!wpa_auth->cb->set_radius_cui)
730
0
    return -1;
731
0
  return wpa_auth->cb->set_radius_cui(wpa_auth->cb_ctx, sta_addr,
732
0
              radius_cui, radius_cui_len);
733
0
}
734
735
736
static size_t
737
wpa_ft_get_radius_cui(struct wpa_authenticator *wpa_auth, const u8 *sta_addr,
738
          const u8 **buf)
739
0
{
740
0
  *buf = NULL;
741
0
  if (!wpa_auth->cb->get_radius_cui)
742
0
    return 0;
743
0
  return wpa_auth->cb->get_radius_cui(wpa_auth->cb_ctx, sta_addr, buf);
744
0
}
745
746
747
static void
748
wpa_ft_set_session_timeout(struct wpa_authenticator *wpa_auth,
749
          const u8 *sta_addr, int session_timeout)
750
0
{
751
0
  if (!wpa_auth->cb->set_session_timeout)
752
0
    return;
753
0
  wpa_auth->cb->set_session_timeout(wpa_auth->cb_ctx, sta_addr,
754
0
            session_timeout);
755
0
}
756
757
758
static int
759
wpa_ft_get_session_timeout(struct wpa_authenticator *wpa_auth,
760
          const u8 *sta_addr)
761
0
{
762
0
  if (!wpa_auth->cb->get_session_timeout)
763
0
    return 0;
764
0
  return wpa_auth->cb->get_session_timeout(wpa_auth->cb_ctx, sta_addr);
765
0
}
766
767
768
static int wpa_ft_add_tspec(struct wpa_authenticator *wpa_auth,
769
          const u8 *sta_addr,
770
          u8 *tspec_ie, size_t tspec_ielen)
771
0
{
772
0
  if (wpa_auth->cb->add_tspec == NULL) {
773
0
    wpa_printf(MSG_DEBUG, "FT: add_tspec is not initialized");
774
0
    return -1;
775
0
  }
776
0
  return wpa_auth->cb->add_tspec(wpa_auth->cb_ctx, sta_addr, tspec_ie,
777
0
               tspec_ielen);
778
0
}
779
780
781
#ifdef CONFIG_OCV
782
static int wpa_channel_info(struct wpa_authenticator *wpa_auth,
783
             struct wpa_channel_info *ci)
784
{
785
  if (!wpa_auth->cb->channel_info)
786
    return -1;
787
  return wpa_auth->cb->channel_info(wpa_auth->cb_ctx, ci);
788
}
789
#endif /* CONFIG_OCV */
790
791
792
int wpa_write_mdie(struct wpa_auth_config *conf, u8 *buf, size_t len)
793
0
{
794
0
  u8 *pos = buf;
795
0
  u8 capab;
796
0
  if (len < 2 + sizeof(struct rsn_mdie))
797
0
    return -1;
798
799
0
  *pos++ = WLAN_EID_MOBILITY_DOMAIN;
800
0
  *pos++ = MOBILITY_DOMAIN_ID_LEN + 1;
801
0
  os_memcpy(pos, conf->mobility_domain, MOBILITY_DOMAIN_ID_LEN);
802
0
  pos += MOBILITY_DOMAIN_ID_LEN;
803
0
  capab = 0;
804
0
  if (conf->ft_over_ds)
805
0
    capab |= RSN_FT_CAPAB_FT_OVER_DS;
806
0
  *pos++ = capab;
807
808
0
  return pos - buf;
809
0
}
810
811
812
int wpa_write_ftie(struct wpa_auth_config *conf, int key_mgmt, size_t key_len,
813
       const u8 *r0kh_id, size_t r0kh_id_len,
814
       const u8 *anonce, const u8 *snonce,
815
       u8 *buf, size_t len, const u8 *subelem,
816
       size_t subelem_len, int rsnxe_used)
817
0
{
818
0
  u8 *pos = buf, *ielen;
819
0
  size_t hdrlen;
820
0
  u16 mic_control = rsnxe_used ? FTE_MIC_CTRL_RSNXE_USED : 0;
821
822
0
  if (key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY &&
823
0
      key_len == SHA256_MAC_LEN)
824
0
    hdrlen = sizeof(struct rsn_ftie);
825
0
  else if (key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY &&
826
0
     key_len == SHA384_MAC_LEN)
827
0
    hdrlen = sizeof(struct rsn_ftie_sha384);
828
0
  else if (key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY &&
829
0
     key_len == SHA512_MAC_LEN)
830
0
    hdrlen = sizeof(struct rsn_ftie_sha512);
831
0
  else if (wpa_key_mgmt_sha384(key_mgmt))
832
0
    hdrlen = sizeof(struct rsn_ftie_sha384);
833
0
  else
834
0
    hdrlen = sizeof(struct rsn_ftie);
835
836
0
  if (len < 2 + hdrlen + 2 + FT_R1KH_ID_LEN + 2 + r0kh_id_len +
837
0
      subelem_len)
838
0
    return -1;
839
840
0
  *pos++ = WLAN_EID_FAST_BSS_TRANSITION;
841
0
  ielen = pos++;
842
843
0
  if (key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY &&
844
0
      key_len == SHA512_MAC_LEN) {
845
0
    struct rsn_ftie_sha512 *hdr = (struct rsn_ftie_sha512 *) pos;
846
847
0
    os_memset(hdr, 0, sizeof(*hdr));
848
0
    pos += sizeof(*hdr);
849
0
    mic_control |= FTE_MIC_LEN_32 << FTE_MIC_CTRL_MIC_LEN_SHIFT;
850
0
    WPA_PUT_LE16(hdr->mic_control, mic_control);
851
0
    if (anonce)
852
0
      os_memcpy(hdr->anonce, anonce, WPA_NONCE_LEN);
853
0
    if (snonce)
854
0
      os_memcpy(hdr->snonce, snonce, WPA_NONCE_LEN);
855
0
  } else if ((key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY &&
856
0
        key_len == SHA384_MAC_LEN) ||
857
0
       wpa_key_mgmt_sha384(key_mgmt)) {
858
0
    struct rsn_ftie_sha384 *hdr = (struct rsn_ftie_sha384 *) pos;
859
860
0
    os_memset(hdr, 0, sizeof(*hdr));
861
0
    pos += sizeof(*hdr);
862
0
    mic_control |= FTE_MIC_LEN_24 << FTE_MIC_CTRL_MIC_LEN_SHIFT;
863
0
    WPA_PUT_LE16(hdr->mic_control, mic_control);
864
0
    if (anonce)
865
0
      os_memcpy(hdr->anonce, anonce, WPA_NONCE_LEN);
866
0
    if (snonce)
867
0
      os_memcpy(hdr->snonce, snonce, WPA_NONCE_LEN);
868
0
  } else {
869
0
    struct rsn_ftie *hdr = (struct rsn_ftie *) pos;
870
871
0
    os_memset(hdr, 0, sizeof(*hdr));
872
0
    pos += sizeof(*hdr);
873
0
    mic_control |= FTE_MIC_LEN_16 << FTE_MIC_CTRL_MIC_LEN_SHIFT;
874
0
    WPA_PUT_LE16(hdr->mic_control, mic_control);
875
0
    if (anonce)
876
0
      os_memcpy(hdr->anonce, anonce, WPA_NONCE_LEN);
877
0
    if (snonce)
878
0
      os_memcpy(hdr->snonce, snonce, WPA_NONCE_LEN);
879
0
  }
880
881
  /* Optional Parameters */
882
0
  *pos++ = FTIE_SUBELEM_R1KH_ID;
883
0
  *pos++ = FT_R1KH_ID_LEN;
884
0
  os_memcpy(pos, conf->r1_key_holder, FT_R1KH_ID_LEN);
885
0
  pos += FT_R1KH_ID_LEN;
886
887
0
  if (r0kh_id) {
888
0
    *pos++ = FTIE_SUBELEM_R0KH_ID;
889
0
    *pos++ = r0kh_id_len;
890
0
    os_memcpy(pos, r0kh_id, r0kh_id_len);
891
0
    pos += r0kh_id_len;
892
0
  }
893
894
0
  if (subelem) {
895
0
    os_memcpy(pos, subelem, subelem_len);
896
0
    pos += subelem_len;
897
0
  }
898
899
0
  *ielen = pos - buf - 2;
900
901
0
  return pos - buf;
902
0
}
903
904
905
/* A packet to be handled after seq response */
906
struct ft_remote_item {
907
  struct dl_list list;
908
909
  u8 nonce[FT_RRB_NONCE_LEN];
910
  struct os_reltime nonce_ts;
911
912
  u8 src_addr[ETH_ALEN];
913
  u8 *enc;
914
  size_t enc_len;
915
  u8 *auth;
916
  size_t auth_len;
917
  int (*cb)(struct wpa_authenticator *wpa_auth,
918
      const u8 *src_addr,
919
      const u8 *enc, size_t enc_len,
920
      const u8 *auth, size_t auth_len,
921
      int no_defer);
922
};
923
924
925
static void wpa_ft_rrb_seq_free(struct ft_remote_item *item)
926
0
{
927
0
  eloop_cancel_timeout(wpa_ft_rrb_seq_timeout, ELOOP_ALL_CTX, item);
928
0
  dl_list_del(&item->list);
929
0
  bin_clear_free(item->enc, item->enc_len);
930
0
  os_free(item->auth);
931
0
  os_free(item);
932
0
}
933
934
935
static void wpa_ft_rrb_seq_flush(struct wpa_authenticator *wpa_auth,
936
         struct ft_remote_seq *rkh_seq, int cb)
937
0
{
938
0
  struct ft_remote_item *item, *n;
939
940
0
  dl_list_for_each_safe(item, n, &rkh_seq->rx.queue,
941
0
            struct ft_remote_item, list) {
942
0
    if (cb && item->cb)
943
0
      item->cb(wpa_auth, item->src_addr, item->enc,
944
0
         item->enc_len, item->auth, item->auth_len, 1);
945
0
    wpa_ft_rrb_seq_free(item);
946
0
  }
947
0
}
948
949
950
static void wpa_ft_rrb_seq_timeout(void *eloop_ctx, void *timeout_ctx)
951
0
{
952
0
  struct ft_remote_item *item = timeout_ctx;
953
954
0
  wpa_ft_rrb_seq_free(item);
955
0
}
956
957
958
static int
959
wpa_ft_rrb_seq_req(struct wpa_authenticator *wpa_auth,
960
       struct ft_remote_seq *rkh_seq, const u8 *src_addr,
961
       const u8 *f_r0kh_id, size_t f_r0kh_id_len,
962
       const u8 *f_r1kh_id, const u8 *key, size_t key_len,
963
       const u8 *enc, size_t enc_len,
964
       const u8 *auth, size_t auth_len,
965
       int (*cb)(struct wpa_authenticator *wpa_auth,
966
           const u8 *src_addr,
967
           const u8 *enc, size_t enc_len,
968
           const u8 *auth, size_t auth_len,
969
           int no_defer))
970
0
{
971
0
  struct ft_remote_item *item = NULL;
972
0
  u8 *packet = NULL;
973
0
  size_t packet_len;
974
0
  struct tlv_list seq_req_auth[] = {
975
0
    { .type = FT_RRB_NONCE, .len = FT_RRB_NONCE_LEN,
976
0
      .data = NULL /* to be filled: item->nonce */ },
977
0
    { .type = FT_RRB_R0KH_ID, .len = f_r0kh_id_len,
978
0
      .data = f_r0kh_id },
979
0
    { .type = FT_RRB_R1KH_ID, .len = FT_R1KH_ID_LEN,
980
0
      .data = f_r1kh_id },
981
0
    { .type = FT_RRB_LAST_EMPTY, .len = 0, .data = NULL },
982
0
  };
983
984
0
  if (dl_list_len(&rkh_seq->rx.queue) >= ftRRBmaxQueueLen) {
985
0
    wpa_printf(MSG_DEBUG, "FT: Sequence number queue too long");
986
0
    goto err;
987
0
  }
988
989
0
  wpa_printf(MSG_DEBUG, "FT: Send sequence number request from " MACSTR
990
0
       " to " MACSTR,
991
0
       MAC2STR(wpa_auth->addr), MAC2STR(src_addr));
992
0
  item = os_zalloc(sizeof(*item));
993
0
  if (!item)
994
0
    goto err;
995
996
0
  os_memcpy(item->src_addr, src_addr, ETH_ALEN);
997
0
  item->cb = cb;
998
999
0
  if (random_get_bytes(item->nonce, FT_RRB_NONCE_LEN) < 0) {
1000
0
    wpa_printf(MSG_DEBUG, "FT: Seq num nonce: out of random bytes");
1001
0
    goto err;
1002
0
  }
1003
1004
0
  if (os_get_reltime(&item->nonce_ts) < 0)
1005
0
    goto err;
1006
1007
0
  if (enc && enc_len > 0) {
1008
0
    item->enc = os_memdup(enc, enc_len);
1009
0
    item->enc_len = enc_len;
1010
0
    if (!item->enc)
1011
0
      goto err;
1012
0
  }
1013
1014
0
  if (auth && auth_len > 0) {
1015
0
    item->auth = os_memdup(auth, auth_len);
1016
0
    item->auth_len = auth_len;
1017
0
    if (!item->auth)
1018
0
      goto err;
1019
0
  }
1020
1021
0
  eloop_register_timeout(ftRRBseqTimeout, 0, wpa_ft_rrb_seq_timeout,
1022
0
             wpa_auth, item);
1023
1024
0
  seq_req_auth[0].data = item->nonce;
1025
1026
0
  if (wpa_ft_rrb_build(key, key_len, NULL, NULL, seq_req_auth, NULL,
1027
0
           wpa_auth->addr, FT_PACKET_R0KH_R1KH_SEQ_REQ,
1028
0
           &packet, &packet_len) < 0) {
1029
0
    item = NULL; /* some other seq resp might still accept this */
1030
0
    goto err;
1031
0
  }
1032
1033
0
  dl_list_add(&rkh_seq->rx.queue, &item->list);
1034
1035
0
  wpa_ft_rrb_oui_send(wpa_auth, src_addr, FT_PACKET_R0KH_R1KH_SEQ_REQ,
1036
0
          packet, packet_len);
1037
1038
0
  os_free(packet);
1039
1040
0
  return 0;
1041
0
err:
1042
0
  wpa_printf(MSG_DEBUG, "FT: Failed to send sequence number request");
1043
0
  if (item) {
1044
0
    os_free(item->auth);
1045
0
    bin_clear_free(item->enc, item->enc_len);
1046
0
    os_free(item);
1047
0
  }
1048
1049
0
  return -1;
1050
0
}
1051
1052
1053
0
#define FT_RRB_SEQ_OK    0
1054
0
#define FT_RRB_SEQ_DROP  1
1055
0
#define FT_RRB_SEQ_DEFER 2
1056
1057
static int
1058
wpa_ft_rrb_seq_chk(struct ft_remote_seq *rkh_seq, const u8 *src_addr,
1059
       const u8 *enc, size_t enc_len,
1060
       const u8 *auth, size_t auth_len,
1061
       const char *msgtype, int no_defer)
1062
0
{
1063
0
  const u8 *f_seq;
1064
0
  size_t f_seq_len;
1065
0
  const struct ft_rrb_seq *msg_both;
1066
0
  u32 msg_seq, msg_off, rkh_off;
1067
0
  struct os_reltime now;
1068
0
  unsigned int i;
1069
1070
0
  RRB_GET_AUTH(FT_RRB_SEQ, seq, msgtype, sizeof(*msg_both));
1071
0
  wpa_hexdump(MSG_DEBUG, "FT: sequence number", f_seq, f_seq_len);
1072
0
  msg_both = (const struct ft_rrb_seq *) f_seq;
1073
1074
0
  if (rkh_seq->rx.num_last == 0) {
1075
    /* first packet from remote */
1076
0
    goto defer;
1077
0
  }
1078
1079
0
  if (le_to_host32(msg_both->dom) != rkh_seq->rx.dom) {
1080
    /* remote might have rebooted */
1081
0
    goto defer;
1082
0
  }
1083
1084
0
  if (os_get_reltime(&now) == 0) {
1085
0
    u32 msg_ts_now_remote, msg_ts_off;
1086
0
    struct os_reltime now_remote;
1087
1088
0
    os_reltime_sub(&now, &rkh_seq->rx.time_offset, &now_remote);
1089
0
    msg_ts_now_remote = now_remote.sec;
1090
0
    msg_ts_off = le_to_host32(msg_both->ts) -
1091
0
      (msg_ts_now_remote - ftRRBseqTimeout);
1092
0
    if (msg_ts_off > 2 * ftRRBseqTimeout)
1093
0
      goto defer;
1094
0
  }
1095
1096
0
  msg_seq = le_to_host32(msg_both->seq);
1097
0
  rkh_off = rkh_seq->rx.last[rkh_seq->rx.offsetidx];
1098
0
  msg_off = msg_seq - rkh_off;
1099
0
  if (msg_off > 0xC0000000)
1100
0
    goto out; /* too old message, drop it */
1101
1102
0
  if (msg_off <= 0x40000000) {
1103
0
    for (i = 0; i < rkh_seq->rx.num_last; i++) {
1104
0
      if (rkh_seq->rx.last[i] == msg_seq)
1105
0
        goto out; /* duplicate message, drop it */
1106
0
    }
1107
1108
0
    return FT_RRB_SEQ_OK;
1109
0
  }
1110
1111
0
defer:
1112
0
  if (no_defer)
1113
0
    goto out;
1114
1115
0
  wpa_printf(MSG_DEBUG, "FT: Possibly invalid sequence number in %s from "
1116
0
       MACSTR, msgtype, MAC2STR(src_addr));
1117
1118
0
  return FT_RRB_SEQ_DEFER;
1119
0
out:
1120
0
  wpa_printf(MSG_DEBUG, "FT: Invalid sequence number in %s from " MACSTR,
1121
0
       msgtype, MAC2STR(src_addr));
1122
1123
0
  return FT_RRB_SEQ_DROP;
1124
0
}
1125
1126
1127
static void
1128
wpa_ft_rrb_seq_accept(struct wpa_authenticator *wpa_auth,
1129
          struct ft_remote_seq *rkh_seq, const u8 *src_addr,
1130
          const u8 *auth, size_t auth_len,
1131
          const char *msgtype)
1132
0
{
1133
0
  const u8 *f_seq;
1134
0
  size_t f_seq_len;
1135
0
  const struct ft_rrb_seq *msg_both;
1136
0
  u32 msg_seq, msg_off, min_off, rkh_off;
1137
0
  int minidx = 0;
1138
0
  unsigned int i;
1139
1140
0
  RRB_GET_AUTH(FT_RRB_SEQ, seq, msgtype, sizeof(*msg_both));
1141
0
  msg_both = (const struct ft_rrb_seq *) f_seq;
1142
1143
0
  msg_seq = le_to_host32(msg_both->seq);
1144
1145
0
  if (rkh_seq->rx.num_last < FT_REMOTE_SEQ_BACKLOG) {
1146
0
    rkh_seq->rx.last[rkh_seq->rx.num_last] = msg_seq;
1147
0
    rkh_seq->rx.num_last++;
1148
0
    return;
1149
0
  }
1150
1151
0
  rkh_off = rkh_seq->rx.last[rkh_seq->rx.offsetidx];
1152
0
  for (i = 0; i < rkh_seq->rx.num_last; i++) {
1153
0
    msg_off = rkh_seq->rx.last[i] - rkh_off;
1154
0
    min_off = rkh_seq->rx.last[minidx] - rkh_off;
1155
0
    if (msg_off < min_off && i != rkh_seq->rx.offsetidx)
1156
0
      minidx = i;
1157
0
  }
1158
0
  rkh_seq->rx.last[rkh_seq->rx.offsetidx] = msg_seq;
1159
0
  rkh_seq->rx.offsetidx = minidx;
1160
1161
0
  return;
1162
0
out:
1163
  /* RRB_GET_AUTH should never fail here as
1164
   * wpa_ft_rrb_seq_chk() verified FT_RRB_SEQ presence. */
1165
0
  wpa_printf(MSG_ERROR, "FT: %s() failed", __func__);
1166
0
}
1167
1168
1169
static int wpa_ft_new_seq(struct ft_remote_seq *rkh_seq,
1170
        struct ft_rrb_seq *f_seq)
1171
0
{
1172
0
  struct os_reltime now;
1173
1174
0
  if (os_get_reltime(&now) < 0)
1175
0
    return -1;
1176
1177
0
  if (!rkh_seq->tx.dom) {
1178
0
    if (random_get_bytes((u8 *) &rkh_seq->tx.seq,
1179
0
             sizeof(rkh_seq->tx.seq))) {
1180
0
      wpa_printf(MSG_ERROR,
1181
0
           "FT: Failed to get random data for sequence number initialization");
1182
0
      rkh_seq->tx.seq = now.usec;
1183
0
    }
1184
0
    if (random_get_bytes((u8 *) &rkh_seq->tx.dom,
1185
0
             sizeof(rkh_seq->tx.dom))) {
1186
0
      wpa_printf(MSG_ERROR,
1187
0
           "FT: Failed to get random data for sequence number initialization");
1188
0
      rkh_seq->tx.dom = now.usec;
1189
0
    }
1190
0
    rkh_seq->tx.dom |= 1;
1191
0
  }
1192
1193
0
  f_seq->dom = host_to_le32(rkh_seq->tx.dom);
1194
0
  f_seq->seq = host_to_le32(rkh_seq->tx.seq);
1195
0
  f_seq->ts = host_to_le32(now.sec);
1196
1197
0
  rkh_seq->tx.seq++;
1198
1199
0
  return 0;
1200
0
}
1201
1202
1203
struct wpa_ft_pmk_r0_sa {
1204
  struct dl_list list;
1205
  u8 pmk_r0[PMK_LEN_MAX];
1206
  size_t pmk_r0_len;
1207
  u8 pmk_r0_name[WPA_PMK_NAME_LEN];
1208
  u8 spa[ETH_ALEN];
1209
  int pairwise; /* Pairwise cipher suite, WPA_CIPHER_* */
1210
  struct vlan_description *vlan;
1211
  os_time_t expiration; /* 0 for no expiration */
1212
  u8 *identity;
1213
  size_t identity_len;
1214
  u8 *radius_cui;
1215
  size_t radius_cui_len;
1216
  os_time_t session_timeout; /* 0 for no expiration */
1217
  /* TODO: radius_class, EAP type */
1218
  int pmk_r1_pushed;
1219
};
1220
1221
struct wpa_ft_pmk_r1_sa {
1222
  struct dl_list list;
1223
  u8 pmk_r1[PMK_LEN_MAX];
1224
  size_t pmk_r1_len;
1225
  u8 pmk_r1_name[WPA_PMK_NAME_LEN];
1226
  u8 spa[ETH_ALEN];
1227
  int pairwise; /* Pairwise cipher suite, WPA_CIPHER_* */
1228
  struct vlan_description *vlan;
1229
  u8 *identity;
1230
  size_t identity_len;
1231
  u8 *radius_cui;
1232
  size_t radius_cui_len;
1233
  os_time_t session_timeout; /* 0 for no expiration */
1234
  /* TODO: radius_class, EAP type */
1235
};
1236
1237
struct wpa_ft_pmk_cache {
1238
  struct dl_list pmk_r0; /* struct wpa_ft_pmk_r0_sa */
1239
  struct dl_list pmk_r1; /* struct wpa_ft_pmk_r1_sa */
1240
};
1241
1242
1243
static void wpa_ft_expire_pmk_r0(void *eloop_ctx, void *timeout_ctx);
1244
static void wpa_ft_expire_pmk_r1(void *eloop_ctx, void *timeout_ctx);
1245
1246
1247
static void wpa_ft_free_pmk_r0(struct wpa_ft_pmk_r0_sa *r0)
1248
0
{
1249
0
  if (!r0)
1250
0
    return;
1251
1252
0
  dl_list_del(&r0->list);
1253
0
  eloop_cancel_timeout(wpa_ft_expire_pmk_r0, r0, NULL);
1254
1255
0
  os_memset(r0->pmk_r0, 0, PMK_LEN_MAX);
1256
0
  os_free(r0->vlan);
1257
0
  os_free(r0->identity);
1258
0
  os_free(r0->radius_cui);
1259
0
  os_free(r0);
1260
0
}
1261
1262
1263
static void wpa_ft_expire_pmk_r0(void *eloop_ctx, void *timeout_ctx)
1264
0
{
1265
0
  struct wpa_ft_pmk_r0_sa *r0 = eloop_ctx;
1266
0
  struct os_reltime now;
1267
0
  int expires_in;
1268
0
  int session_timeout;
1269
1270
0
  os_get_reltime(&now);
1271
1272
0
  if (!r0)
1273
0
    return;
1274
1275
0
  expires_in = r0->expiration - now.sec;
1276
0
  session_timeout = r0->session_timeout - now.sec;
1277
  /* conditions to remove from cache:
1278
   * a) r0->expiration is set and hit
1279
   * -or-
1280
   * b) r0->session_timeout is set and hit
1281
   */
1282
0
  if ((!r0->expiration || expires_in > 0) &&
1283
0
      (!r0->session_timeout || session_timeout > 0)) {
1284
0
    wpa_printf(MSG_ERROR,
1285
0
         "FT: %s() called for non-expired entry %p",
1286
0
         __func__, r0);
1287
0
    eloop_cancel_timeout(wpa_ft_expire_pmk_r0, r0, NULL);
1288
0
    if (r0->expiration && expires_in > 0)
1289
0
      eloop_register_timeout(expires_in + 1, 0,
1290
0
                 wpa_ft_expire_pmk_r0, r0, NULL);
1291
0
    if (r0->session_timeout && session_timeout > 0)
1292
0
      eloop_register_timeout(session_timeout + 1, 0,
1293
0
                 wpa_ft_expire_pmk_r0, r0, NULL);
1294
0
    return;
1295
0
  }
1296
1297
0
  wpa_ft_free_pmk_r0(r0);
1298
0
}
1299
1300
1301
static void wpa_ft_free_pmk_r1(struct wpa_ft_pmk_r1_sa *r1)
1302
0
{
1303
0
  if (!r1)
1304
0
    return;
1305
1306
0
  dl_list_del(&r1->list);
1307
0
  eloop_cancel_timeout(wpa_ft_expire_pmk_r1, r1, NULL);
1308
1309
0
  os_memset(r1->pmk_r1, 0, PMK_LEN_MAX);
1310
0
  os_free(r1->vlan);
1311
0
  os_free(r1->identity);
1312
0
  os_free(r1->radius_cui);
1313
0
  os_free(r1);
1314
0
}
1315
1316
1317
static void wpa_ft_expire_pmk_r1(void *eloop_ctx, void *timeout_ctx)
1318
0
{
1319
0
  struct wpa_ft_pmk_r1_sa *r1 = eloop_ctx;
1320
1321
0
  wpa_ft_free_pmk_r1(r1);
1322
0
}
1323
1324
1325
struct wpa_ft_pmk_cache * wpa_ft_pmk_cache_init(void)
1326
1.10k
{
1327
1.10k
  struct wpa_ft_pmk_cache *cache;
1328
1329
1.10k
  cache = os_zalloc(sizeof(*cache));
1330
1.10k
  if (cache) {
1331
1.10k
    dl_list_init(&cache->pmk_r0);
1332
1.10k
    dl_list_init(&cache->pmk_r1);
1333
1.10k
  }
1334
1335
1.10k
  return cache;
1336
1.10k
}
1337
1338
1339
void wpa_ft_pmk_cache_deinit(struct wpa_ft_pmk_cache *cache)
1340
1.10k
{
1341
1.10k
  struct wpa_ft_pmk_r0_sa *r0, *r0prev;
1342
1.10k
  struct wpa_ft_pmk_r1_sa *r1, *r1prev;
1343
1344
1.10k
  dl_list_for_each_safe(r0, r0prev, &cache->pmk_r0,
1345
1.10k
            struct wpa_ft_pmk_r0_sa, list)
1346
0
    wpa_ft_free_pmk_r0(r0);
1347
1348
1.10k
  dl_list_for_each_safe(r1, r1prev, &cache->pmk_r1,
1349
1.10k
            struct wpa_ft_pmk_r1_sa, list)
1350
0
    wpa_ft_free_pmk_r1(r1);
1351
1352
1.10k
  os_free(cache);
1353
1.10k
}
1354
1355
1356
static int wpa_ft_store_pmk_r0(struct wpa_authenticator *wpa_auth,
1357
             const u8 *spa, const u8 *pmk_r0,
1358
             size_t pmk_r0_len,
1359
             const u8 *pmk_r0_name, int pairwise,
1360
             const struct vlan_description *vlan,
1361
             int expires_in, int session_timeout,
1362
             const u8 *identity, size_t identity_len,
1363
             const u8 *radius_cui, size_t radius_cui_len)
1364
0
{
1365
0
  struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
1366
0
  struct wpa_ft_pmk_r0_sa *r0;
1367
0
  struct os_reltime now;
1368
1369
  /* TODO: add limit on number of entries in cache */
1370
0
  os_get_reltime(&now);
1371
1372
0
  r0 = os_zalloc(sizeof(*r0));
1373
0
  if (r0 == NULL)
1374
0
    return -1;
1375
1376
0
  os_memcpy(r0->pmk_r0, pmk_r0, pmk_r0_len);
1377
0
  r0->pmk_r0_len = pmk_r0_len;
1378
0
  os_memcpy(r0->pmk_r0_name, pmk_r0_name, WPA_PMK_NAME_LEN);
1379
0
  os_memcpy(r0->spa, spa, ETH_ALEN);
1380
0
  r0->pairwise = pairwise;
1381
0
  if (expires_in > 0)
1382
0
    r0->expiration = now.sec + expires_in;
1383
0
  if (vlan && vlan->notempty) {
1384
0
    r0->vlan = os_zalloc(sizeof(*vlan));
1385
0
    if (!r0->vlan) {
1386
0
      bin_clear_free(r0, sizeof(*r0));
1387
0
      return -1;
1388
0
    }
1389
0
    *r0->vlan = *vlan;
1390
0
  }
1391
0
  if (identity) {
1392
0
    r0->identity = os_malloc(identity_len);
1393
0
    if (r0->identity) {
1394
0
      os_memcpy(r0->identity, identity, identity_len);
1395
0
      r0->identity_len = identity_len;
1396
0
    }
1397
0
  }
1398
0
  if (radius_cui) {
1399
0
    r0->radius_cui = os_malloc(radius_cui_len);
1400
0
    if (r0->radius_cui) {
1401
0
      os_memcpy(r0->radius_cui, radius_cui, radius_cui_len);
1402
0
      r0->radius_cui_len = radius_cui_len;
1403
0
    }
1404
0
  }
1405
0
  if (session_timeout > 0)
1406
0
    r0->session_timeout = now.sec + session_timeout;
1407
1408
0
  dl_list_add(&cache->pmk_r0, &r0->list);
1409
0
  if (expires_in > 0)
1410
0
    eloop_register_timeout(expires_in + 1, 0, wpa_ft_expire_pmk_r0,
1411
0
               r0, NULL);
1412
0
  if (session_timeout > 0)
1413
0
    eloop_register_timeout(session_timeout + 1, 0,
1414
0
               wpa_ft_expire_pmk_r0, r0, NULL);
1415
1416
0
  return 0;
1417
0
}
1418
1419
1420
static int wpa_ft_fetch_pmk_r0(struct wpa_authenticator *wpa_auth,
1421
             const u8 *spa, const u8 *pmk_r0_name,
1422
             const struct wpa_ft_pmk_r0_sa **r0_out)
1423
0
{
1424
0
  struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
1425
0
  struct wpa_ft_pmk_r0_sa *r0;
1426
0
  struct os_reltime now;
1427
1428
0
  os_get_reltime(&now);
1429
0
  dl_list_for_each(r0, &cache->pmk_r0, struct wpa_ft_pmk_r0_sa, list) {
1430
0
    if (ether_addr_equal(r0->spa, spa) &&
1431
0
        os_memcmp_const(r0->pmk_r0_name, pmk_r0_name,
1432
0
            WPA_PMK_NAME_LEN) == 0) {
1433
0
      *r0_out = r0;
1434
0
      return 0;
1435
0
    }
1436
0
  }
1437
1438
0
  *r0_out = NULL;
1439
0
  return -1;
1440
0
}
1441
1442
1443
static int wpa_ft_store_pmk_r1(struct wpa_authenticator *wpa_auth,
1444
             const u8 *spa, const u8 *pmk_r1,
1445
             size_t pmk_r1_len,
1446
             const u8 *pmk_r1_name, int pairwise,
1447
             const struct vlan_description *vlan,
1448
             int expires_in, int session_timeout,
1449
             const u8 *identity, size_t identity_len,
1450
             const u8 *radius_cui, size_t radius_cui_len)
1451
0
{
1452
0
  struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
1453
0
  int max_expires_in = wpa_auth->conf.r1_max_key_lifetime;
1454
0
  struct wpa_ft_pmk_r1_sa *r1;
1455
0
  struct os_reltime now;
1456
1457
  /* TODO: limit on number of entries in cache */
1458
0
  os_get_reltime(&now);
1459
1460
0
  if (max_expires_in && (max_expires_in < expires_in || expires_in == 0))
1461
0
    expires_in = max_expires_in;
1462
1463
0
  r1 = os_zalloc(sizeof(*r1));
1464
0
  if (r1 == NULL)
1465
0
    return -1;
1466
1467
0
  os_memcpy(r1->pmk_r1, pmk_r1, pmk_r1_len);
1468
0
  r1->pmk_r1_len = pmk_r1_len;
1469
0
  os_memcpy(r1->pmk_r1_name, pmk_r1_name, WPA_PMK_NAME_LEN);
1470
0
  os_memcpy(r1->spa, spa, ETH_ALEN);
1471
0
  r1->pairwise = pairwise;
1472
0
  if (vlan && vlan->notempty) {
1473
0
    r1->vlan = os_zalloc(sizeof(*vlan));
1474
0
    if (!r1->vlan) {
1475
0
      bin_clear_free(r1, sizeof(*r1));
1476
0
      return -1;
1477
0
    }
1478
0
    *r1->vlan = *vlan;
1479
0
  }
1480
0
  if (identity) {
1481
0
    r1->identity = os_malloc(identity_len);
1482
0
    if (r1->identity) {
1483
0
      os_memcpy(r1->identity, identity, identity_len);
1484
0
      r1->identity_len = identity_len;
1485
0
    }
1486
0
  }
1487
0
  if (radius_cui) {
1488
0
    r1->radius_cui = os_malloc(radius_cui_len);
1489
0
    if (r1->radius_cui) {
1490
0
      os_memcpy(r1->radius_cui, radius_cui, radius_cui_len);
1491
0
      r1->radius_cui_len = radius_cui_len;
1492
0
    }
1493
0
  }
1494
0
  if (session_timeout > 0)
1495
0
    r1->session_timeout = now.sec + session_timeout;
1496
1497
0
  dl_list_add(&cache->pmk_r1, &r1->list);
1498
1499
0
  if (expires_in > 0)
1500
0
    eloop_register_timeout(expires_in + 1, 0, wpa_ft_expire_pmk_r1,
1501
0
               r1, NULL);
1502
0
  if (session_timeout > 0)
1503
0
    eloop_register_timeout(session_timeout + 1, 0,
1504
0
               wpa_ft_expire_pmk_r1, r1, NULL);
1505
1506
0
  return 0;
1507
0
}
1508
1509
1510
int wpa_ft_fetch_pmk_r1(struct wpa_authenticator *wpa_auth,
1511
      const u8 *spa, const u8 *pmk_r1_name,
1512
      u8 *pmk_r1, size_t *pmk_r1_len, int *pairwise,
1513
      struct vlan_description *vlan,
1514
      const u8 **identity, size_t *identity_len,
1515
      const u8 **radius_cui, size_t *radius_cui_len,
1516
      int *session_timeout)
1517
0
{
1518
0
  struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
1519
0
  struct wpa_ft_pmk_r1_sa *r1;
1520
0
  struct os_reltime now;
1521
1522
0
  os_get_reltime(&now);
1523
1524
0
  dl_list_for_each(r1, &cache->pmk_r1, struct wpa_ft_pmk_r1_sa, list) {
1525
0
    if (ether_addr_equal(r1->spa, spa) &&
1526
0
        os_memcmp_const(r1->pmk_r1_name, pmk_r1_name,
1527
0
            WPA_PMK_NAME_LEN) == 0) {
1528
0
      os_memcpy(pmk_r1, r1->pmk_r1, r1->pmk_r1_len);
1529
0
      *pmk_r1_len = r1->pmk_r1_len;
1530
0
      if (pairwise)
1531
0
        *pairwise = r1->pairwise;
1532
0
      if (vlan && r1->vlan)
1533
0
        *vlan = *r1->vlan;
1534
0
      if (vlan && !r1->vlan)
1535
0
        os_memset(vlan, 0, sizeof(*vlan));
1536
0
      if (identity && identity_len) {
1537
0
        *identity = r1->identity;
1538
0
        *identity_len = r1->identity_len;
1539
0
      }
1540
0
      if (radius_cui && radius_cui_len) {
1541
0
        *radius_cui = r1->radius_cui;
1542
0
        *radius_cui_len = r1->radius_cui_len;
1543
0
      }
1544
0
      if (session_timeout && r1->session_timeout > now.sec)
1545
0
        *session_timeout = r1->session_timeout -
1546
0
          now.sec;
1547
0
      else if (session_timeout && r1->session_timeout)
1548
0
        *session_timeout = 1;
1549
0
      else if (session_timeout)
1550
0
        *session_timeout = 0;
1551
0
      return 0;
1552
0
    }
1553
0
  }
1554
1555
0
  return -1;
1556
0
}
1557
1558
1559
static int wpa_ft_rrb_init_r0kh_seq(struct ft_remote_r0kh *r0kh)
1560
0
{
1561
0
  if (r0kh->seq)
1562
0
    return 0;
1563
1564
0
  r0kh->seq = os_zalloc(sizeof(*r0kh->seq));
1565
0
  if (!r0kh->seq) {
1566
0
    wpa_printf(MSG_DEBUG, "FT: Failed to allocate r0kh->seq");
1567
0
    return -1;
1568
0
  }
1569
1570
0
  dl_list_init(&r0kh->seq->rx.queue);
1571
1572
0
  return 0;
1573
0
}
1574
1575
1576
static void wpa_ft_rrb_lookup_r0kh(struct wpa_authenticator *wpa_auth,
1577
           const u8 *f_r0kh_id, size_t f_r0kh_id_len,
1578
           struct ft_remote_r0kh **r0kh_out,
1579
           struct ft_remote_r0kh **r0kh_wildcard)
1580
0
{
1581
0
  struct ft_remote_r0kh *r0kh;
1582
1583
0
  *r0kh_wildcard = NULL;
1584
0
  *r0kh_out = NULL;
1585
1586
0
  if (wpa_auth->conf.r0kh_list)
1587
0
    r0kh = *wpa_auth->conf.r0kh_list;
1588
0
  else
1589
0
    r0kh = NULL;
1590
0
  for (; r0kh; r0kh = r0kh->next) {
1591
0
    if (r0kh->id_len == 1 && r0kh->id[0] == '*')
1592
0
      *r0kh_wildcard = r0kh;
1593
0
    if (f_r0kh_id && r0kh->id_len == f_r0kh_id_len &&
1594
0
        os_memcmp_const(f_r0kh_id, r0kh->id, f_r0kh_id_len) == 0)
1595
0
      *r0kh_out = r0kh;
1596
0
  }
1597
1598
0
  if (!*r0kh_out && !*r0kh_wildcard)
1599
0
    wpa_printf(MSG_DEBUG, "FT: No matching R0KH found");
1600
1601
0
  if (*r0kh_out && wpa_ft_rrb_init_r0kh_seq(*r0kh_out) < 0)
1602
0
    *r0kh_out = NULL;
1603
0
}
1604
1605
1606
static int wpa_ft_rrb_init_r1kh_seq(struct ft_remote_r1kh *r1kh)
1607
0
{
1608
0
  if (r1kh->seq)
1609
0
    return 0;
1610
1611
0
  r1kh->seq = os_zalloc(sizeof(*r1kh->seq));
1612
0
  if (!r1kh->seq) {
1613
0
    wpa_printf(MSG_DEBUG, "FT: Failed to allocate r1kh->seq");
1614
0
    return -1;
1615
0
  }
1616
1617
0
  dl_list_init(&r1kh->seq->rx.queue);
1618
1619
0
  return 0;
1620
0
}
1621
1622
1623
static void wpa_ft_rrb_lookup_r1kh(struct wpa_authenticator *wpa_auth,
1624
           const u8 *f_r1kh_id,
1625
           struct ft_remote_r1kh **r1kh_out,
1626
           struct ft_remote_r1kh **r1kh_wildcard)
1627
0
{
1628
0
  struct ft_remote_r1kh *r1kh;
1629
1630
0
  *r1kh_wildcard = NULL;
1631
0
  *r1kh_out = NULL;
1632
1633
0
  if (wpa_auth->conf.r1kh_list)
1634
0
    r1kh = *wpa_auth->conf.r1kh_list;
1635
0
  else
1636
0
    r1kh = NULL;
1637
0
  for (; r1kh; r1kh = r1kh->next) {
1638
0
    if (is_zero_ether_addr(r1kh->addr) &&
1639
0
        is_zero_ether_addr(r1kh->id))
1640
0
      *r1kh_wildcard = r1kh;
1641
0
    if (f_r1kh_id &&
1642
0
        os_memcmp_const(r1kh->id, f_r1kh_id, FT_R1KH_ID_LEN) == 0)
1643
0
      *r1kh_out = r1kh;
1644
0
  }
1645
1646
0
  if (!*r1kh_out && !*r1kh_wildcard)
1647
0
    wpa_printf(MSG_DEBUG, "FT: No matching R1KH found");
1648
1649
0
  if (*r1kh_out && wpa_ft_rrb_init_r1kh_seq(*r1kh_out) < 0)
1650
0
    *r1kh_out = NULL;
1651
0
}
1652
1653
1654
static int wpa_ft_rrb_check_r0kh(struct wpa_authenticator *wpa_auth,
1655
         const u8 *f_r0kh_id, size_t f_r0kh_id_len)
1656
0
{
1657
0
  if (f_r0kh_id_len != wpa_auth->conf.r0_key_holder_len ||
1658
0
      os_memcmp_const(f_r0kh_id, wpa_auth->conf.r0_key_holder,
1659
0
          f_r0kh_id_len) != 0)
1660
0
    return -1;
1661
1662
0
  return 0;
1663
0
}
1664
1665
1666
static int wpa_ft_rrb_check_r1kh(struct wpa_authenticator *wpa_auth,
1667
         const u8 *f_r1kh_id)
1668
0
{
1669
0
  if (os_memcmp_const(f_r1kh_id, wpa_auth->conf.r1_key_holder,
1670
0
          FT_R1KH_ID_LEN) != 0)
1671
0
    return -1;
1672
1673
0
  return 0;
1674
0
}
1675
1676
1677
static void wpa_ft_rrb_del_r0kh(void *eloop_ctx, void *timeout_ctx)
1678
0
{
1679
0
  struct wpa_authenticator *wpa_auth = eloop_ctx;
1680
0
  struct ft_remote_r0kh *r0kh, *prev = NULL;
1681
1682
0
  if (!wpa_auth->conf.r0kh_list)
1683
0
    return;
1684
1685
0
  for (r0kh = *wpa_auth->conf.r0kh_list; r0kh; r0kh = r0kh->next) {
1686
0
    if (r0kh == timeout_ctx)
1687
0
      break;
1688
0
    prev = r0kh;
1689
0
  }
1690
0
  if (!r0kh)
1691
0
    return;
1692
0
  if (prev)
1693
0
    prev->next = r0kh->next;
1694
0
  else
1695
0
    *wpa_auth->conf.r0kh_list = r0kh->next;
1696
0
  if (r0kh->seq)
1697
0
    wpa_ft_rrb_seq_flush(wpa_auth, r0kh->seq, 0);
1698
0
  os_free(r0kh->seq);
1699
0
  os_free(r0kh);
1700
0
}
1701
1702
1703
static void wpa_ft_rrb_r0kh_replenish(struct wpa_authenticator *wpa_auth,
1704
              struct ft_remote_r0kh *r0kh, int timeout)
1705
0
{
1706
0
  if (timeout > 0)
1707
0
    eloop_replenish_timeout(timeout, 0, wpa_ft_rrb_del_r0kh,
1708
0
          wpa_auth, r0kh);
1709
0
}
1710
1711
1712
static void wpa_ft_rrb_r0kh_timeout(struct wpa_authenticator *wpa_auth,
1713
            struct ft_remote_r0kh *r0kh, int timeout)
1714
0
{
1715
0
  eloop_cancel_timeout(wpa_ft_rrb_del_r0kh, wpa_auth, r0kh);
1716
1717
0
  if (timeout > 0)
1718
0
    eloop_register_timeout(timeout, 0, wpa_ft_rrb_del_r0kh,
1719
0
               wpa_auth, r0kh);
1720
0
}
1721
1722
1723
static struct ft_remote_r0kh *
1724
wpa_ft_rrb_add_r0kh(struct wpa_authenticator *wpa_auth,
1725
        struct ft_remote_r0kh *r0kh_wildcard,
1726
        const u8 *src_addr, const u8 *r0kh_id, size_t id_len,
1727
        int timeout)
1728
0
{
1729
0
  struct ft_remote_r0kh *r0kh;
1730
1731
0
  if (!wpa_auth->conf.r0kh_list)
1732
0
    return NULL;
1733
1734
0
  r0kh = os_zalloc(sizeof(*r0kh));
1735
0
  if (!r0kh)
1736
0
    return NULL;
1737
1738
0
  if (src_addr)
1739
0
    os_memcpy(r0kh->addr, src_addr, sizeof(r0kh->addr));
1740
1741
0
  if (id_len > FT_R0KH_ID_MAX_LEN)
1742
0
    id_len = FT_R0KH_ID_MAX_LEN;
1743
0
  os_memcpy(r0kh->id, r0kh_id, id_len);
1744
0
  r0kh->id_len = id_len;
1745
1746
0
  os_memcpy(r0kh->key, r0kh_wildcard->key, sizeof(r0kh->key));
1747
1748
0
  r0kh->next = *wpa_auth->conf.r0kh_list;
1749
0
  *wpa_auth->conf.r0kh_list = r0kh;
1750
1751
0
  if (timeout > 0)
1752
0
    eloop_register_timeout(timeout, 0, wpa_ft_rrb_del_r0kh,
1753
0
               wpa_auth, r0kh);
1754
1755
0
  if (wpa_ft_rrb_init_r0kh_seq(r0kh) < 0)
1756
0
    return NULL;
1757
1758
0
  return r0kh;
1759
0
}
1760
1761
1762
static void wpa_ft_rrb_del_r1kh(void *eloop_ctx, void *timeout_ctx)
1763
0
{
1764
0
  struct wpa_authenticator *wpa_auth = eloop_ctx;
1765
0
  struct ft_remote_r1kh *r1kh, *prev = NULL;
1766
1767
0
  if (!wpa_auth->conf.r1kh_list)
1768
0
    return;
1769
1770
0
  for (r1kh = *wpa_auth->conf.r1kh_list; r1kh; r1kh = r1kh->next) {
1771
0
    if (r1kh == timeout_ctx)
1772
0
      break;
1773
0
    prev = r1kh;
1774
0
  }
1775
0
  if (!r1kh)
1776
0
    return;
1777
0
  if (prev)
1778
0
    prev->next = r1kh->next;
1779
0
  else
1780
0
    *wpa_auth->conf.r1kh_list = r1kh->next;
1781
0
  if (r1kh->seq)
1782
0
    wpa_ft_rrb_seq_flush(wpa_auth, r1kh->seq, 0);
1783
0
  os_free(r1kh->seq);
1784
0
  os_free(r1kh);
1785
0
}
1786
1787
1788
static void wpa_ft_rrb_r1kh_replenish(struct wpa_authenticator *wpa_auth,
1789
              struct ft_remote_r1kh *r1kh, int timeout)
1790
0
{
1791
0
  if (timeout > 0)
1792
0
    eloop_replenish_timeout(timeout, 0, wpa_ft_rrb_del_r1kh,
1793
0
          wpa_auth, r1kh);
1794
0
}
1795
1796
1797
static struct ft_remote_r1kh *
1798
wpa_ft_rrb_add_r1kh(struct wpa_authenticator *wpa_auth,
1799
        struct ft_remote_r1kh *r1kh_wildcard,
1800
        const u8 *src_addr, const u8 *r1kh_id, int timeout)
1801
0
{
1802
0
  struct ft_remote_r1kh *r1kh;
1803
1804
0
  if (!wpa_auth->conf.r1kh_list)
1805
0
    return NULL;
1806
1807
0
  r1kh = os_zalloc(sizeof(*r1kh));
1808
0
  if (!r1kh)
1809
0
    return NULL;
1810
1811
0
  os_memcpy(r1kh->addr, src_addr, sizeof(r1kh->addr));
1812
0
  os_memcpy(r1kh->id, r1kh_id, sizeof(r1kh->id));
1813
0
  os_memcpy(r1kh->key, r1kh_wildcard->key, sizeof(r1kh->key));
1814
0
  r1kh->next = *wpa_auth->conf.r1kh_list;
1815
0
  *wpa_auth->conf.r1kh_list = r1kh;
1816
1817
0
  if (timeout > 0)
1818
0
    eloop_register_timeout(timeout, 0, wpa_ft_rrb_del_r1kh,
1819
0
               wpa_auth, r1kh);
1820
1821
0
  if (wpa_ft_rrb_init_r1kh_seq(r1kh) < 0)
1822
0
    return NULL;
1823
1824
0
  return r1kh;
1825
0
}
1826
1827
1828
void wpa_ft_sta_deinit(struct wpa_state_machine *sm)
1829
1.10k
{
1830
1.10k
  eloop_cancel_timeout(wpa_ft_expire_pull, sm, NULL);
1831
1.10k
}
1832
1833
1834
static void wpa_ft_deinit_seq(struct wpa_authenticator *wpa_auth)
1835
1.10k
{
1836
1.10k
  struct ft_remote_r0kh *r0kh;
1837
1.10k
  struct ft_remote_r1kh *r1kh;
1838
1839
1.10k
  eloop_cancel_timeout(wpa_ft_rrb_seq_timeout, wpa_auth, ELOOP_ALL_CTX);
1840
1841
1.10k
  if (wpa_auth->conf.r0kh_list)
1842
0
    r0kh = *wpa_auth->conf.r0kh_list;
1843
1.10k
  else
1844
1.10k
    r0kh = NULL;
1845
1.10k
  for (; r0kh; r0kh = r0kh->next) {
1846
0
    if (!r0kh->seq)
1847
0
      continue;
1848
0
    wpa_ft_rrb_seq_flush(wpa_auth, r0kh->seq, 0);
1849
0
    os_free(r0kh->seq);
1850
0
    r0kh->seq = NULL;
1851
0
  }
1852
1853
1.10k
  if (wpa_auth->conf.r1kh_list)
1854
0
    r1kh = *wpa_auth->conf.r1kh_list;
1855
1.10k
  else
1856
1.10k
    r1kh = NULL;
1857
1.10k
  for (; r1kh; r1kh = r1kh->next) {
1858
0
    if (!r1kh->seq)
1859
0
      continue;
1860
0
    wpa_ft_rrb_seq_flush(wpa_auth, r1kh->seq, 0);
1861
0
    os_free(r1kh->seq);
1862
0
    r1kh->seq = NULL;
1863
0
  }
1864
1.10k
}
1865
1866
1867
static void wpa_ft_deinit_rkh_tmp(struct wpa_authenticator *wpa_auth)
1868
1.10k
{
1869
1.10k
  struct ft_remote_r0kh *r0kh, *r0kh_next, *r0kh_prev = NULL;
1870
1.10k
  struct ft_remote_r1kh *r1kh, *r1kh_next, *r1kh_prev = NULL;
1871
1872
1.10k
  if (wpa_auth->conf.r0kh_list)
1873
0
    r0kh = *wpa_auth->conf.r0kh_list;
1874
1.10k
  else
1875
1.10k
    r0kh = NULL;
1876
1.10k
  while (r0kh) {
1877
0
    r0kh_next = r0kh->next;
1878
0
    if (eloop_cancel_timeout(wpa_ft_rrb_del_r0kh, wpa_auth,
1879
0
           r0kh) > 0) {
1880
0
      if (r0kh_prev)
1881
0
        r0kh_prev->next = r0kh_next;
1882
0
      else
1883
0
        *wpa_auth->conf.r0kh_list = r0kh_next;
1884
0
      os_free(r0kh);
1885
0
    } else {
1886
0
      r0kh_prev = r0kh;
1887
0
    }
1888
0
    r0kh = r0kh_next;
1889
0
  }
1890
1891
1.10k
  if (wpa_auth->conf.r1kh_list)
1892
0
    r1kh = *wpa_auth->conf.r1kh_list;
1893
1.10k
  else
1894
1.10k
    r1kh = NULL;
1895
1.10k
  while (r1kh) {
1896
0
    r1kh_next = r1kh->next;
1897
0
    if (eloop_cancel_timeout(wpa_ft_rrb_del_r1kh, wpa_auth,
1898
0
           r1kh) > 0) {
1899
0
      if (r1kh_prev)
1900
0
        r1kh_prev->next = r1kh_next;
1901
0
      else
1902
0
        *wpa_auth->conf.r1kh_list = r1kh_next;
1903
0
      os_free(r1kh);
1904
0
    } else {
1905
0
      r1kh_prev = r1kh;
1906
0
    }
1907
0
    r1kh = r1kh_next;
1908
0
  }
1909
1.10k
}
1910
1911
1912
void wpa_ft_deinit(struct wpa_authenticator *wpa_auth)
1913
1.10k
{
1914
1.10k
  wpa_ft_deinit_seq(wpa_auth);
1915
1.10k
  wpa_ft_deinit_rkh_tmp(wpa_auth);
1916
1.10k
}
1917
1918
1919
static void wpa_ft_block_r0kh(struct wpa_authenticator *wpa_auth,
1920
            const u8 *f_r0kh_id, size_t f_r0kh_id_len)
1921
0
{
1922
0
  struct ft_remote_r0kh *r0kh, *r0kh_wildcard;
1923
1924
0
  if (!wpa_auth->conf.rkh_neg_timeout)
1925
0
    return;
1926
1927
0
  wpa_ft_rrb_lookup_r0kh(wpa_auth, f_r0kh_id, f_r0kh_id_len,
1928
0
             &r0kh, &r0kh_wildcard);
1929
1930
0
  if (!r0kh_wildcard) {
1931
    /* r0kh removed after neg_timeout and might need re-adding */
1932
0
    return;
1933
0
  }
1934
1935
0
  wpa_hexdump(MSG_DEBUG, "FT: Temporarily block R0KH-ID",
1936
0
        f_r0kh_id, f_r0kh_id_len);
1937
1938
0
  if (r0kh) {
1939
0
    wpa_ft_rrb_r0kh_timeout(wpa_auth, r0kh,
1940
0
          wpa_auth->conf.rkh_neg_timeout);
1941
0
    os_memset(r0kh->addr, 0, ETH_ALEN);
1942
0
  } else
1943
0
    wpa_ft_rrb_add_r0kh(wpa_auth, r0kh_wildcard, NULL, f_r0kh_id,
1944
0
            f_r0kh_id_len,
1945
0
            wpa_auth->conf.rkh_neg_timeout);
1946
0
}
1947
1948
1949
static void wpa_ft_expire_pull(void *eloop_ctx, void *timeout_ctx)
1950
0
{
1951
0
  struct wpa_state_machine *sm = eloop_ctx;
1952
1953
0
  wpa_printf(MSG_DEBUG, "FT: Timeout pending pull request for " MACSTR,
1954
0
       MAC2STR(sm->addr));
1955
0
  if (sm->ft_pending_pull_left_retries <= 0)
1956
0
    wpa_ft_block_r0kh(sm->wpa_auth, sm->r0kh_id, sm->r0kh_id_len);
1957
1958
  /* cancel multiple timeouts */
1959
0
  eloop_cancel_timeout(wpa_ft_expire_pull, sm, NULL);
1960
0
  ft_finish_pull(sm);
1961
0
}
1962
1963
1964
static int wpa_ft_pull_pmk_r1(struct wpa_state_machine *sm,
1965
            const u8 *ies, size_t ies_len,
1966
            const u8 *pmk_r0_name)
1967
0
{
1968
0
  struct ft_remote_r0kh *r0kh, *r0kh_wildcard;
1969
0
  u8 *packet = NULL;
1970
0
  const u8 *key, *f_r1kh_id = sm->wpa_auth->conf.r1_key_holder;
1971
0
  size_t packet_len, key_len;
1972
0
  struct ft_rrb_seq f_seq;
1973
0
  int tsecs, tusecs, first;
1974
0
  struct wpabuf *ft_pending_req_ies;
1975
0
  int r0kh_timeout;
1976
0
  struct tlv_list req_enc[] = {
1977
0
    { .type = FT_RRB_PMK_R0_NAME, .len = WPA_PMK_NAME_LEN,
1978
0
      .data = pmk_r0_name },
1979
0
    { .type = FT_RRB_S1KH_ID, .len = ETH_ALEN,
1980
0
      .data = sm->addr },
1981
0
    { .type = FT_RRB_LAST_EMPTY, .len = 0, .data = NULL },
1982
0
  };
1983
0
  struct tlv_list req_auth[] = {
1984
0
    { .type = FT_RRB_NONCE, .len = FT_RRB_NONCE_LEN,
1985
0
      .data = sm->ft_pending_pull_nonce },
1986
0
    { .type = FT_RRB_SEQ, .len = sizeof(f_seq),
1987
0
      .data = (u8 *) &f_seq },
1988
0
    { .type = FT_RRB_R0KH_ID, .len = sm->r0kh_id_len,
1989
0
      .data = sm->r0kh_id },
1990
0
    { .type = FT_RRB_R1KH_ID, .len = FT_R1KH_ID_LEN,
1991
0
      .data = f_r1kh_id },
1992
0
    { .type = FT_RRB_LAST_EMPTY, .len = 0, .data = NULL },
1993
0
  };
1994
1995
0
  if (sm->ft_pending_pull_left_retries <= 0)
1996
0
    return -1;
1997
0
  first = sm->ft_pending_pull_left_retries ==
1998
0
    sm->wpa_auth->conf.rkh_pull_retries;
1999
0
  sm->ft_pending_pull_left_retries--;
2000
2001
0
  wpa_ft_rrb_lookup_r0kh(sm->wpa_auth, sm->r0kh_id, sm->r0kh_id_len,
2002
0
             &r0kh, &r0kh_wildcard);
2003
2004
  /* Keep r0kh sufficiently long in the list for seq num check */
2005
0
  r0kh_timeout = sm->wpa_auth->conf.rkh_pull_timeout / 1000 +
2006
0
    1 + ftRRBseqTimeout;
2007
0
  if (r0kh) {
2008
0
    wpa_ft_rrb_r0kh_replenish(sm->wpa_auth, r0kh, r0kh_timeout);
2009
0
  } else if (r0kh_wildcard) {
2010
0
    wpa_printf(MSG_DEBUG, "FT: Using wildcard R0KH-ID");
2011
    /* r0kh->addr: updated by SEQ_RESP and wpa_ft_expire_pull */
2012
0
    r0kh = wpa_ft_rrb_add_r0kh(sm->wpa_auth, r0kh_wildcard,
2013
0
             r0kh_wildcard->addr,
2014
0
             sm->r0kh_id, sm->r0kh_id_len,
2015
0
             r0kh_timeout);
2016
0
  }
2017
0
  if (r0kh == NULL) {
2018
0
    wpa_hexdump(MSG_DEBUG, "FT: Did not find R0KH-ID",
2019
0
          sm->r0kh_id, sm->r0kh_id_len);
2020
0
    return -1;
2021
0
  }
2022
0
  if (is_zero_ether_addr(r0kh->addr)) {
2023
0
    wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID is temporarily blocked",
2024
0
          sm->r0kh_id, sm->r0kh_id_len);
2025
0
    return -1;
2026
0
  }
2027
0
  if (ether_addr_equal(r0kh->addr, sm->wpa_auth->addr)) {
2028
0
    wpa_printf(MSG_DEBUG,
2029
0
         "FT: R0KH-ID points to self - no matching key available");
2030
0
    return -1;
2031
0
  }
2032
2033
0
  key = r0kh->key;
2034
0
  key_len = sizeof(r0kh->key);
2035
2036
0
  if (r0kh->seq->rx.num_last == 0) {
2037
    /* A sequence request will be sent out anyway when pull
2038
     * response is received. Send it out now to avoid one RTT. */
2039
0
    wpa_ft_rrb_seq_req(sm->wpa_auth, r0kh->seq, r0kh->addr,
2040
0
           r0kh->id, r0kh->id_len, f_r1kh_id, key,
2041
0
           key_len, NULL, 0, NULL, 0, NULL);
2042
0
  }
2043
2044
0
  wpa_printf(MSG_DEBUG, "FT: Send PMK-R1 pull request from " MACSTR
2045
0
       " to remote R0KH address " MACSTR,
2046
0
       MAC2STR(sm->wpa_auth->addr), MAC2STR(r0kh->addr));
2047
2048
0
  if (first &&
2049
0
      random_get_bytes(sm->ft_pending_pull_nonce, FT_RRB_NONCE_LEN) < 0) {
2050
0
    wpa_printf(MSG_DEBUG, "FT: Failed to get random data for "
2051
0
         "nonce");
2052
0
    return -1;
2053
0
  }
2054
2055
0
  if (wpa_ft_new_seq(r0kh->seq, &f_seq) < 0) {
2056
0
    wpa_printf(MSG_DEBUG, "FT: Failed to get seq num");
2057
0
    return -1;
2058
0
  }
2059
2060
0
  if (wpa_ft_rrb_build(key, key_len, req_enc, NULL, req_auth, NULL,
2061
0
           sm->wpa_auth->addr, FT_PACKET_R0KH_R1KH_PULL,
2062
0
           &packet, &packet_len) < 0)
2063
0
    return -1;
2064
2065
0
  ft_pending_req_ies = wpabuf_alloc_copy(ies, ies_len);
2066
0
  wpabuf_free(sm->ft_pending_req_ies);
2067
0
  sm->ft_pending_req_ies = ft_pending_req_ies;
2068
0
  if (!sm->ft_pending_req_ies) {
2069
0
    os_free(packet);
2070
0
    return -1;
2071
0
  }
2072
2073
0
  tsecs = sm->wpa_auth->conf.rkh_pull_timeout / 1000;
2074
0
  tusecs = (sm->wpa_auth->conf.rkh_pull_timeout % 1000) * 1000;
2075
0
  eloop_register_timeout(tsecs, tusecs, wpa_ft_expire_pull, sm, NULL);
2076
2077
0
  wpa_ft_rrb_oui_send(sm->wpa_auth, r0kh->addr, FT_PACKET_R0KH_R1KH_PULL,
2078
0
          packet, packet_len);
2079
2080
0
  os_free(packet);
2081
2082
0
  return 0;
2083
0
}
2084
2085
2086
int wpa_ft_store_pmk_fils(struct wpa_state_machine *sm,
2087
        const u8 *pmk_r0, const u8 *pmk_r0_name)
2088
0
{
2089
0
  int expires_in = sm->wpa_auth->conf.r0_key_lifetime;
2090
0
  struct vlan_description vlan;
2091
0
  const u8 *identity, *radius_cui;
2092
0
  size_t identity_len, radius_cui_len;
2093
0
  int session_timeout;
2094
0
  size_t pmk_r0_len = wpa_key_mgmt_sha384(sm->wpa_key_mgmt) ?
2095
0
    SHA384_MAC_LEN : PMK_LEN;
2096
2097
0
  if (wpa_ft_get_vlan(sm->wpa_auth, sm->addr, &vlan) < 0) {
2098
0
    wpa_printf(MSG_DEBUG, "FT: vlan not available for STA " MACSTR,
2099
0
         MAC2STR(sm->addr));
2100
0
    return -1;
2101
0
  }
2102
2103
0
  identity_len = wpa_ft_get_identity(sm->wpa_auth, sm->addr, &identity);
2104
0
  radius_cui_len = wpa_ft_get_radius_cui(sm->wpa_auth, sm->addr,
2105
0
                 &radius_cui);
2106
0
  session_timeout = wpa_ft_get_session_timeout(sm->wpa_auth, sm->addr);
2107
2108
0
  return wpa_ft_store_pmk_r0(sm->wpa_auth, sm->addr, pmk_r0, pmk_r0_len,
2109
0
           pmk_r0_name, sm->pairwise, &vlan, expires_in,
2110
0
           session_timeout, identity, identity_len,
2111
0
           radius_cui, radius_cui_len);
2112
0
}
2113
2114
2115
int wpa_auth_derive_ptk_ft(struct wpa_state_machine *sm, struct wpa_ptk *ptk,
2116
         u8 *pmk_r0, u8 *pmk_r1, u8 *pmk_r0_name,
2117
         size_t *key_len, size_t kdk_len)
2118
0
{
2119
0
  size_t pmk_r0_len, pmk_r1_len;
2120
0
  u8 ptk_name[WPA_PMK_NAME_LEN];
2121
0
  const u8 *mdid = sm->wpa_auth->conf.mobility_domain;
2122
0
  const u8 *r0kh = sm->wpa_auth->conf.r0_key_holder;
2123
0
  size_t r0kh_len = sm->wpa_auth->conf.r0_key_holder_len;
2124
0
  const u8 *r1kh = sm->wpa_auth->conf.r1_key_holder;
2125
0
  const u8 *ssid = sm->wpa_auth->conf.ssid;
2126
0
  size_t ssid_len = sm->wpa_auth->conf.ssid_len;
2127
0
  const u8 *mpmk;
2128
0
  size_t mpmk_len;
2129
2130
0
  if (sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY &&
2131
0
      (sm->xxkey_len == SHA256_MAC_LEN ||
2132
0
       sm->xxkey_len == SHA384_MAC_LEN ||
2133
0
       sm->xxkey_len == SHA512_MAC_LEN))
2134
0
    pmk_r0_len = sm->xxkey_len;
2135
0
  else if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt))
2136
0
    pmk_r0_len = SHA384_MAC_LEN;
2137
0
  else
2138
0
    pmk_r0_len = PMK_LEN;
2139
0
  *key_len = pmk_r1_len = pmk_r0_len;
2140
2141
0
  if (sm->xxkey_len > 0) {
2142
0
    mpmk = sm->xxkey;
2143
0
    mpmk_len = sm->xxkey_len;
2144
0
  } else if (sm->pmksa) {
2145
0
    mpmk = sm->pmksa->pmk;
2146
0
    mpmk_len = sm->pmksa->pmk_len;
2147
0
  } else {
2148
0
    wpa_printf(MSG_DEBUG, "FT: XXKey not available for key "
2149
0
         "derivation");
2150
0
    return -1;
2151
0
  }
2152
2153
0
  if (wpa_derive_pmk_r0(mpmk, mpmk_len, ssid, ssid_len, mdid,
2154
0
            r0kh, r0kh_len, sm->addr,
2155
0
            pmk_r0, pmk_r0_name,
2156
0
            sm->wpa_key_mgmt) < 0 ||
2157
0
      wpa_derive_pmk_r1(pmk_r0, pmk_r0_len, pmk_r0_name, r1kh, sm->addr,
2158
0
            pmk_r1, sm->pmk_r1_name) < 0)
2159
0
    return -1;
2160
2161
0
  return wpa_pmk_r1_to_ptk(pmk_r1, pmk_r1_len, sm->SNonce, sm->ANonce,
2162
0
         sm->addr, sm->wpa_auth->addr, sm->pmk_r1_name,
2163
0
         ptk, ptk_name, sm->wpa_key_mgmt, sm->pairwise,
2164
0
         kdk_len);
2165
0
}
2166
2167
2168
void wpa_auth_ft_store_keys(struct wpa_state_machine *sm, const u8 *pmk_r0,
2169
          const u8 *pmk_r1, const u8 *pmk_r0_name,
2170
          size_t key_len)
2171
0
{
2172
0
  int psk_local = sm->wpa_auth->conf.ft_psk_generate_local;
2173
0
  int expires_in = sm->wpa_auth->conf.r0_key_lifetime;
2174
0
  struct vlan_description vlan;
2175
0
  const u8 *identity, *radius_cui;
2176
0
  size_t identity_len, radius_cui_len;
2177
0
  int session_timeout;
2178
2179
0
  if (psk_local && wpa_key_mgmt_ft_psk(sm->wpa_key_mgmt))
2180
0
    return;
2181
2182
0
  if (wpa_ft_get_vlan(sm->wpa_auth, sm->addr, &vlan) < 0) {
2183
0
    wpa_printf(MSG_DEBUG, "FT: vlan not available for STA " MACSTR,
2184
0
         MAC2STR(sm->addr));
2185
0
    return;
2186
0
  }
2187
2188
0
  identity_len = wpa_ft_get_identity(sm->wpa_auth, sm->addr, &identity);
2189
0
  radius_cui_len = wpa_ft_get_radius_cui(sm->wpa_auth, sm->addr,
2190
0
                 &radius_cui);
2191
0
  session_timeout = wpa_ft_get_session_timeout(sm->wpa_auth, sm->addr);
2192
2193
2194
0
  wpa_ft_store_pmk_r0(sm->wpa_auth, sm->addr, pmk_r0, key_len,
2195
0
          pmk_r0_name,
2196
0
          sm->pairwise, &vlan, expires_in,
2197
0
          session_timeout, identity, identity_len,
2198
0
          radius_cui, radius_cui_len);
2199
0
  wpa_ft_store_pmk_r1(sm->wpa_auth, sm->addr, pmk_r1, key_len,
2200
0
          sm->pmk_r1_name, sm->pairwise, &vlan,
2201
0
          expires_in, session_timeout, identity,
2202
0
          identity_len, radius_cui, radius_cui_len);
2203
0
}
2204
2205
2206
static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
2207
              const u8 *addr, int idx, u8 *seq)
2208
0
{
2209
0
  if (wpa_auth->cb->get_seqnum == NULL)
2210
0
    return -1;
2211
0
  return wpa_auth->cb->get_seqnum(wpa_auth->cb_ctx, addr, idx, seq);
2212
0
}
2213
2214
2215
static u8 * wpa_ft_gtk_subelem(struct wpa_state_machine *sm, size_t *len,
2216
             bool reassoc, int vlan_id)
2217
0
{
2218
0
  u8 *subelem;
2219
0
  struct wpa_auth_config *conf = &sm->wpa_auth->conf;
2220
0
  struct wpa_group *gsm = sm->group;
2221
0
  size_t subelem_len, pad_len;
2222
0
  const u8 *key;
2223
0
  size_t key_len;
2224
0
  u8 keybuf[WPA_GTK_MAX_LEN];
2225
0
  const u8 *kek;
2226
0
  size_t kek_len;
2227
2228
#ifdef CONFIG_IEEE80211BE
2229
  if (reassoc && vlan_id)
2230
    gsm = wpa_select_vlan_wpa_group(gsm, vlan_id);
2231
#endif /* CONFIG_IEEE80211BE */
2232
2233
0
  if (wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
2234
0
    kek = sm->PTK.kek2;
2235
0
    kek_len = sm->PTK.kek2_len;
2236
0
  } else {
2237
0
    kek = sm->PTK.kek;
2238
0
    kek_len = sm->PTK.kek_len;
2239
0
  }
2240
2241
0
  key_len = gsm->GTK_len;
2242
0
  if (key_len > sizeof(keybuf))
2243
0
    return NULL;
2244
2245
  /*
2246
   * Pad key for AES Key Wrap if it is not multiple of 8 bytes or is less
2247
   * than 16 bytes.
2248
   */
2249
0
  pad_len = key_len % 8;
2250
0
  if (pad_len)
2251
0
    pad_len = 8 - pad_len;
2252
0
  if (key_len + pad_len < 16)
2253
0
    pad_len += 8;
2254
0
  if (pad_len && key_len < sizeof(keybuf)) {
2255
0
    os_memcpy(keybuf, gsm->GTK[gsm->GN - 1], key_len);
2256
0
    if (conf->disable_gtk) {
2257
      /*
2258
       * Provide unique random GTK to each STA to prevent use
2259
       * of GTK in the BSS.
2260
       */
2261
0
      if (random_get_bytes(keybuf, key_len) < 0)
2262
0
        return NULL;
2263
0
    }
2264
0
    os_memset(keybuf + key_len, 0, pad_len);
2265
0
    keybuf[key_len] = 0xdd;
2266
0
    key_len += pad_len;
2267
0
    key = keybuf;
2268
0
  } else if (conf->disable_gtk) {
2269
    /*
2270
     * Provide unique random GTK to each STA to prevent use of GTK
2271
     * in the BSS.
2272
     */
2273
0
    if (random_get_bytes(keybuf, key_len) < 0)
2274
0
      return NULL;
2275
0
    key = keybuf;
2276
0
  } else {
2277
0
    key = gsm->GTK[gsm->GN - 1];
2278
0
  }
2279
2280
  /*
2281
   * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
2282
   * Key[5..32].
2283
   */
2284
0
  subelem_len = 13 + key_len + 8;
2285
0
  subelem = os_zalloc(subelem_len);
2286
0
  if (subelem == NULL)
2287
0
    return NULL;
2288
2289
0
  subelem[0] = FTIE_SUBELEM_GTK;
2290
0
  subelem[1] = 11 + key_len + 8;
2291
  /* Key ID in B0-B1 of Key Info */
2292
0
  WPA_PUT_LE16(&subelem[2], gsm->GN & 0x03);
2293
0
  subelem[4] = gsm->GTK_len;
2294
0
  wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, subelem + 5);
2295
0
  if (aes_wrap(kek, kek_len, key_len / 8, key, subelem + 13)) {
2296
0
    wpa_printf(MSG_DEBUG,
2297
0
         "FT: GTK subelem encryption failed: kek_len=%d",
2298
0
         (int) kek_len);
2299
0
    forced_memzero(keybuf, sizeof(keybuf));
2300
0
    os_free(subelem);
2301
0
    return NULL;
2302
0
  }
2303
2304
0
  forced_memzero(keybuf, sizeof(keybuf));
2305
0
  *len = subelem_len;
2306
0
  return subelem;
2307
0
}
2308
2309
2310
static u8 * wpa_ft_igtk_subelem(struct wpa_state_machine *sm, size_t *len)
2311
0
{
2312
0
  u8 *subelem, *pos;
2313
0
  struct wpa_auth_config *conf = &sm->wpa_auth->conf;
2314
0
  struct wpa_group *gsm = sm->group;
2315
0
  size_t subelem_len;
2316
0
  const u8 *kek, *igtk;
2317
0
  size_t kek_len;
2318
0
  size_t igtk_len;
2319
0
  u8 stub_igtk[WPA_IGTK_MAX_LEN];
2320
2321
0
  if (wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
2322
0
    kek = sm->PTK.kek2;
2323
0
    kek_len = sm->PTK.kek2_len;
2324
0
  } else {
2325
0
    kek = sm->PTK.kek;
2326
0
    kek_len = sm->PTK.kek_len;
2327
0
  }
2328
2329
0
  igtk_len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
2330
2331
  /* Sub-elem ID[1] | Length[1] | KeyID[2] | IPN[6] | Key Length[1] |
2332
   * Key[16+8] */
2333
0
  subelem_len = 1 + 1 + 2 + 6 + 1 + igtk_len + 8;
2334
0
  subelem = os_zalloc(subelem_len);
2335
0
  if (subelem == NULL)
2336
0
    return NULL;
2337
2338
0
  pos = subelem;
2339
0
  *pos++ = FTIE_SUBELEM_IGTK;
2340
0
  *pos++ = subelem_len - 2;
2341
0
  WPA_PUT_LE16(pos, gsm->GN_igtk);
2342
0
  pos += 2;
2343
0
  wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos);
2344
0
  pos += 6;
2345
0
  *pos++ = igtk_len;
2346
0
  igtk = gsm->IGTK[gsm->GN_igtk - 4];
2347
0
  if (conf->disable_gtk) {
2348
    /*
2349
     * Provide unique random IGTK to each STA to prevent use of
2350
     * IGTK in the BSS.
2351
     */
2352
0
    if (random_get_bytes(stub_igtk, igtk_len / 8) < 0) {
2353
0
      os_free(subelem);
2354
0
      return NULL;
2355
0
    }
2356
0
    igtk = stub_igtk;
2357
0
  }
2358
0
  if (aes_wrap(kek, kek_len, igtk_len / 8, igtk, pos)) {
2359
0
    wpa_printf(MSG_DEBUG,
2360
0
         "FT: IGTK subelem encryption failed: kek_len=%d",
2361
0
         (int) kek_len);
2362
0
    os_free(subelem);
2363
0
    return NULL;
2364
0
  }
2365
2366
0
  *len = subelem_len;
2367
0
  return subelem;
2368
0
}
2369
2370
2371
static u8 * wpa_ft_bigtk_subelem(struct wpa_state_machine *sm, size_t *len)
2372
0
{
2373
0
  u8 *subelem, *pos;
2374
0
  struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2375
0
  struct wpa_group *gsm = wpa_auth->group;
2376
0
  size_t subelem_len;
2377
0
  const u8 *kek, *bigtk;
2378
0
  size_t kek_len;
2379
0
  size_t bigtk_len;
2380
2381
0
  if (wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
2382
0
    kek = sm->PTK.kek2;
2383
0
    kek_len = sm->PTK.kek2_len;
2384
0
  } else {
2385
0
    kek = sm->PTK.kek;
2386
0
    kek_len = sm->PTK.kek_len;
2387
0
  }
2388
2389
0
  bigtk_len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
2390
2391
  /* Sub-elem ID[1] | Length[1] | KeyID[2] | BIPN[6] | Key Length[1] |
2392
   * Key[16+8] */
2393
0
  subelem_len = 1 + 1 + 2 + 6 + 1 + bigtk_len + 8;
2394
0
  subelem = os_zalloc(subelem_len);
2395
0
  if (subelem == NULL)
2396
0
    return NULL;
2397
2398
0
  pos = subelem;
2399
0
  *pos++ = FTIE_SUBELEM_BIGTK;
2400
0
  *pos++ = subelem_len - 2;
2401
0
  WPA_PUT_LE16(pos, gsm->GN_bigtk);
2402
0
  pos += 2;
2403
0
  wpa_auth_get_seqnum(wpa_auth, NULL, gsm->GN_bigtk, pos);
2404
0
  pos += 6;
2405
0
  *pos++ = bigtk_len;
2406
0
  bigtk = gsm->BIGTK[gsm->GN_bigtk - 6];
2407
0
  if (aes_wrap(kek, kek_len, bigtk_len / 8, bigtk, pos)) {
2408
0
    wpa_printf(MSG_DEBUG,
2409
0
         "FT: BIGTK subelem encryption failed: kek_len=%d",
2410
0
         (int) kek_len);
2411
0
    os_free(subelem);
2412
0
    return NULL;
2413
0
  }
2414
2415
0
  *len = subelem_len;
2416
0
  return subelem;
2417
0
}
2418
2419
2420
static u8 * wpa_ft_process_rdie(struct wpa_state_machine *sm,
2421
        u8 *pos, u8 *end, u8 id, u8 descr_count,
2422
        const u8 *ies, size_t ies_len)
2423
0
{
2424
0
  struct ieee802_11_elems parse;
2425
0
  struct rsn_rdie *rdie;
2426
2427
0
  wpa_printf(MSG_DEBUG, "FT: Resource Request: id=%d descr_count=%d",
2428
0
       id, descr_count);
2429
0
  wpa_hexdump(MSG_MSGDUMP, "FT: Resource descriptor IE(s)",
2430
0
        ies, ies_len);
2431
2432
0
  if (end - pos < (int) sizeof(*rdie)) {
2433
0
    wpa_printf(MSG_ERROR, "FT: Not enough room for response RDIE");
2434
0
    return pos;
2435
0
  }
2436
2437
0
  *pos++ = WLAN_EID_RIC_DATA;
2438
0
  *pos++ = sizeof(*rdie);
2439
0
  rdie = (struct rsn_rdie *) pos;
2440
0
  rdie->id = id;
2441
0
  rdie->descr_count = 0;
2442
0
  rdie->status_code = host_to_le16(WLAN_STATUS_SUCCESS);
2443
0
  pos += sizeof(*rdie);
2444
2445
0
  if (ieee802_11_parse_elems((u8 *) ies, ies_len, &parse, 1) ==
2446
0
      ParseFailed) {
2447
0
    wpa_printf(MSG_DEBUG, "FT: Failed to parse request IEs");
2448
0
    rdie->status_code =
2449
0
      host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE);
2450
0
    return pos;
2451
0
  }
2452
2453
0
  if (parse.wmm_tspec) {
2454
0
    struct wmm_tspec_element *tspec;
2455
2456
0
    if (parse.wmm_tspec_len + 2 < (int) sizeof(*tspec)) {
2457
0
      wpa_printf(MSG_DEBUG, "FT: Too short WMM TSPEC IE "
2458
0
           "(%d)", (int) parse.wmm_tspec_len);
2459
0
      rdie->status_code =
2460
0
        host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE);
2461
0
      return pos;
2462
0
    }
2463
0
    if (end - pos < (int) sizeof(*tspec)) {
2464
0
      wpa_printf(MSG_ERROR, "FT: Not enough room for "
2465
0
           "response TSPEC");
2466
0
      rdie->status_code =
2467
0
        host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE);
2468
0
      return pos;
2469
0
    }
2470
0
    tspec = (struct wmm_tspec_element *) pos;
2471
0
    os_memcpy(tspec, parse.wmm_tspec - 2, sizeof(*tspec));
2472
0
  }
2473
2474
0
#ifdef NEED_AP_MLME
2475
0
  if (parse.wmm_tspec && sm->wpa_auth->conf.ap_mlme) {
2476
0
    int res;
2477
2478
0
    res = wmm_process_tspec((struct wmm_tspec_element *) pos);
2479
0
    wpa_printf(MSG_DEBUG, "FT: ADDTS processing result: %d", res);
2480
0
    if (res == WMM_ADDTS_STATUS_INVALID_PARAMETERS)
2481
0
      rdie->status_code =
2482
0
        host_to_le16(WLAN_STATUS_INVALID_PARAMETERS);
2483
0
    else if (res == WMM_ADDTS_STATUS_REFUSED)
2484
0
      rdie->status_code =
2485
0
        host_to_le16(WLAN_STATUS_REQUEST_DECLINED);
2486
0
    else {
2487
      /* TSPEC accepted; include updated TSPEC in response */
2488
0
      rdie->descr_count = 1;
2489
0
      pos += sizeof(struct wmm_tspec_element);
2490
0
    }
2491
0
    return pos;
2492
0
  }
2493
0
#endif /* NEED_AP_MLME */
2494
2495
0
  if (parse.wmm_tspec && !sm->wpa_auth->conf.ap_mlme) {
2496
0
    int res;
2497
2498
0
    res = wpa_ft_add_tspec(sm->wpa_auth, sm->addr, pos,
2499
0
               sizeof(struct wmm_tspec_element));
2500
0
    if (res >= 0) {
2501
0
      if (res)
2502
0
        rdie->status_code = host_to_le16(res);
2503
0
      else {
2504
        /* TSPEC accepted; include updated TSPEC in
2505
         * response */
2506
0
        rdie->descr_count = 1;
2507
0
        pos += sizeof(struct wmm_tspec_element);
2508
0
      }
2509
0
      return pos;
2510
0
    }
2511
0
  }
2512
2513
0
  wpa_printf(MSG_DEBUG, "FT: No supported resource requested");
2514
0
  rdie->status_code = host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE);
2515
0
  return pos;
2516
0
}
2517
2518
2519
static u8 * wpa_ft_process_ric(struct wpa_state_machine *sm, u8 *pos, u8 *end,
2520
             const u8 *ric, size_t ric_len)
2521
0
{
2522
0
  const u8 *rpos, *start;
2523
0
  const struct rsn_rdie *rdie;
2524
2525
0
  wpa_hexdump(MSG_MSGDUMP, "FT: RIC Request", ric, ric_len);
2526
2527
0
  rpos = ric;
2528
0
  while (rpos + sizeof(*rdie) < ric + ric_len) {
2529
0
    if (rpos[0] != WLAN_EID_RIC_DATA || rpos[1] < sizeof(*rdie) ||
2530
0
        rpos + 2 + rpos[1] > ric + ric_len)
2531
0
      break;
2532
0
    rdie = (const struct rsn_rdie *) (rpos + 2);
2533
0
    rpos += 2 + rpos[1];
2534
0
    start = rpos;
2535
2536
0
    while (rpos + 2 <= ric + ric_len &&
2537
0
           rpos + 2 + rpos[1] <= ric + ric_len) {
2538
0
      if (rpos[0] == WLAN_EID_RIC_DATA)
2539
0
        break;
2540
0
      rpos += 2 + rpos[1];
2541
0
    }
2542
0
    pos = wpa_ft_process_rdie(sm, pos, end, rdie->id,
2543
0
            rdie->descr_count,
2544
0
            start, rpos - start);
2545
0
  }
2546
2547
0
  return pos;
2548
0
}
2549
2550
2551
u8 * wpa_sm_write_assoc_resp_ies(struct wpa_state_machine *sm, u8 *pos,
2552
         size_t max_len, int auth_alg,
2553
         const u8 *req_ies, size_t req_ies_len,
2554
         int omit_rsnxe, bool reassoc, int vlan_id)
2555
0
{
2556
0
  u8 *end, *mdie, *ftie, *rsnie = NULL, *r0kh_id, *subelem = NULL;
2557
0
  u8 *fte_mic, *elem_count;
2558
0
  size_t mdie_len, ftie_len, rsnie_len = 0, r0kh_id_len, subelem_len = 0;
2559
0
  u8 rsnxe_buf[10], *rsnxe = rsnxe_buf;
2560
0
  size_t rsnxe_len;
2561
0
  int rsnxe_used;
2562
0
  int res;
2563
0
  struct wpa_auth_config *conf;
2564
0
  struct wpa_ft_ies parse;
2565
0
  u8 *ric_start;
2566
0
  u8 *anonce, *snonce;
2567
0
  const u8 *kck;
2568
0
  size_t kck_len;
2569
0
  size_t key_len;
2570
2571
0
  if (sm == NULL)
2572
0
    return pos;
2573
2574
0
  conf = &sm->wpa_auth->conf;
2575
2576
0
  if (!wpa_key_mgmt_ft(sm->wpa_key_mgmt))
2577
0
    return pos;
2578
2579
0
  end = pos + max_len;
2580
2581
#ifdef CONFIG_TESTING_OPTIONS
2582
  if (auth_alg == WLAN_AUTH_FT &&
2583
      sm->wpa_auth->conf.rsne_override_ft_set) {
2584
    wpa_printf(MSG_DEBUG,
2585
         "TESTING: RSNE FT override for MIC calculation");
2586
    rsnie = sm->wpa_auth->conf.rsne_override_ft;
2587
    rsnie_len = sm->wpa_auth->conf.rsne_override_ft_len;
2588
    if (end - pos < (long int) rsnie_len)
2589
      return pos;
2590
    os_memcpy(pos, rsnie, rsnie_len);
2591
    rsnie = pos;
2592
    pos += rsnie_len;
2593
    if (rsnie_len > PMKID_LEN && sm->pmk_r1_name_valid) {
2594
      int idx;
2595
2596
      /* Replace all 0xff PMKID with the valid PMKR1Name */
2597
      for (idx = 0; idx < PMKID_LEN; idx++) {
2598
        if (rsnie[rsnie_len - 1 - idx] != 0xff)
2599
          break;
2600
      }
2601
      if (idx == PMKID_LEN)
2602
        os_memcpy(&rsnie[rsnie_len - PMKID_LEN],
2603
            sm->pmk_r1_name, WPA_PMK_NAME_LEN);
2604
    }
2605
  } else
2606
#endif /* CONFIG_TESTING_OPTIONS */
2607
0
  if (auth_alg == WLAN_AUTH_FT ||
2608
0
      ((auth_alg == WLAN_AUTH_FILS_SK ||
2609
0
        auth_alg == WLAN_AUTH_FILS_SK_PFS ||
2610
0
        auth_alg == WLAN_AUTH_FILS_PK) &&
2611
0
       (sm->wpa_key_mgmt & (WPA_KEY_MGMT_FT_FILS_SHA256 |
2612
0
          WPA_KEY_MGMT_FT_FILS_SHA384)))) {
2613
0
    if (!sm->pmk_r1_name_valid) {
2614
0
      wpa_printf(MSG_ERROR,
2615
0
           "FT: PMKR1Name is not valid for Assoc Resp RSNE");
2616
0
      return NULL;
2617
0
    }
2618
0
    wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name for Assoc Resp RSNE",
2619
0
          sm->pmk_r1_name, WPA_PMK_NAME_LEN);
2620
    /*
2621
     * RSN (only present if this is a Reassociation Response and
2622
     * part of a fast BSS transition; or if this is a
2623
     * (Re)Association Response frame during an FT initial mobility
2624
     * domain association using FILS)
2625
     */
2626
0
    res = wpa_write_rsn_ie(conf, pos, end - pos, sm->pmk_r1_name);
2627
0
    if (res < 0)
2628
0
      return NULL;
2629
0
    rsnie = pos;
2630
0
    rsnie_len = res;
2631
0
    pos += res;
2632
0
  }
2633
2634
  /* Mobility Domain Information */
2635
0
  res = wpa_write_mdie(conf, pos, end - pos);
2636
0
  if (res < 0)
2637
0
    return NULL;
2638
0
  mdie = pos;
2639
0
  mdie_len = res;
2640
0
  pos += res;
2641
2642
  /* Fast BSS Transition Information */
2643
0
  if (auth_alg == WLAN_AUTH_FT) {
2644
0
    subelem = wpa_ft_gtk_subelem(sm, &subelem_len, reassoc,
2645
0
               vlan_id);
2646
0
    if (!subelem) {
2647
0
      wpa_printf(MSG_DEBUG,
2648
0
           "FT: Failed to add GTK subelement");
2649
0
      return NULL;
2650
0
    }
2651
0
    r0kh_id = sm->r0kh_id;
2652
0
    r0kh_id_len = sm->r0kh_id_len;
2653
0
    anonce = sm->ANonce;
2654
0
    snonce = sm->SNonce;
2655
0
    if (sm->mgmt_frame_prot) {
2656
0
      u8 *igtk;
2657
0
      size_t igtk_len;
2658
0
      u8 *nbuf;
2659
0
      igtk = wpa_ft_igtk_subelem(sm, &igtk_len);
2660
0
      if (igtk == NULL) {
2661
0
        wpa_printf(MSG_DEBUG,
2662
0
             "FT: Failed to add IGTK subelement");
2663
0
        os_free(subelem);
2664
0
        return NULL;
2665
0
      }
2666
0
      nbuf = os_realloc(subelem, subelem_len + igtk_len);
2667
0
      if (nbuf == NULL) {
2668
0
        os_free(subelem);
2669
0
        os_free(igtk);
2670
0
        return NULL;
2671
0
      }
2672
0
      subelem = nbuf;
2673
0
      os_memcpy(subelem + subelem_len, igtk, igtk_len);
2674
0
      subelem_len += igtk_len;
2675
0
      os_free(igtk);
2676
0
    }
2677
0
    if (sm->mgmt_frame_prot && conf->beacon_prot) {
2678
0
      u8 *bigtk;
2679
0
      size_t bigtk_len;
2680
0
      u8 *nbuf;
2681
2682
0
      bigtk = wpa_ft_bigtk_subelem(sm, &bigtk_len);
2683
0
      if (!bigtk) {
2684
0
        wpa_printf(MSG_DEBUG,
2685
0
             "FT: Failed to add BIGTK subelement");
2686
0
        os_free(subelem);
2687
0
        return NULL;
2688
0
      }
2689
0
      nbuf = os_realloc(subelem, subelem_len + bigtk_len);
2690
0
      if (!nbuf) {
2691
0
        os_free(subelem);
2692
0
        os_free(bigtk);
2693
0
        return NULL;
2694
0
      }
2695
0
      subelem = nbuf;
2696
0
      os_memcpy(subelem + subelem_len, bigtk, bigtk_len);
2697
0
      subelem_len += bigtk_len;
2698
0
      os_free(bigtk);
2699
0
    }
2700
#ifdef CONFIG_OCV
2701
    if (wpa_auth_uses_ocv(sm)) {
2702
      struct wpa_channel_info ci;
2703
      u8 *nbuf, *ocipos;
2704
2705
      if (wpa_channel_info(sm->wpa_auth, &ci) != 0) {
2706
        wpa_printf(MSG_WARNING,
2707
             "Failed to get channel info for OCI element");
2708
        os_free(subelem);
2709
        return NULL;
2710
      }
2711
#ifdef CONFIG_TESTING_OPTIONS
2712
      if (conf->oci_freq_override_ft_assoc) {
2713
        wpa_printf(MSG_INFO,
2714
             "TEST: Override OCI frequency %d -> %u MHz",
2715
             ci.frequency,
2716
             conf->oci_freq_override_ft_assoc);
2717
        ci.frequency = conf->oci_freq_override_ft_assoc;
2718
      }
2719
#endif /* CONFIG_TESTING_OPTIONS */
2720
2721
      subelem_len += 2 + OCV_OCI_LEN;
2722
      nbuf = os_realloc(subelem, subelem_len);
2723
      if (!nbuf) {
2724
        os_free(subelem);
2725
        return NULL;
2726
      }
2727
      subelem = nbuf;
2728
2729
      ocipos = subelem + subelem_len - 2 - OCV_OCI_LEN;
2730
      *ocipos++ = FTIE_SUBELEM_OCI;
2731
      *ocipos++ = OCV_OCI_LEN;
2732
      if (ocv_insert_oci(&ci, &ocipos) < 0) {
2733
        os_free(subelem);
2734
        return NULL;
2735
      }
2736
    }
2737
#endif /* CONFIG_OCV */
2738
0
  } else {
2739
0
    r0kh_id = conf->r0_key_holder;
2740
0
    r0kh_id_len = conf->r0_key_holder_len;
2741
0
    anonce = NULL;
2742
0
    snonce = NULL;
2743
0
  }
2744
0
  rsnxe_used = (auth_alg == WLAN_AUTH_FT) &&
2745
0
    (conf->sae_pwe == SAE_PWE_HASH_TO_ELEMENT ||
2746
0
     conf->sae_pwe == SAE_PWE_BOTH);
2747
#ifdef CONFIG_TESTING_OPTIONS
2748
  if (sm->wpa_auth->conf.ft_rsnxe_used) {
2749
    rsnxe_used = sm->wpa_auth->conf.ft_rsnxe_used == 1;
2750
    wpa_printf(MSG_DEBUG, "TESTING: FT: Force RSNXE Used %d",
2751
         rsnxe_used);
2752
  }
2753
#endif /* CONFIG_TESTING_OPTIONS */
2754
0
  key_len = sm->xxkey_len;
2755
0
  if (!key_len)
2756
0
    key_len = sm->pmk_r1_len;
2757
0
  if (!key_len && sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY &&
2758
0
      sm->wpa_auth->cb->get_psk) {
2759
0
    size_t psk_len;
2760
2761
0
    if (sm->wpa_auth->cb->get_psk(sm->wpa_auth->cb_ctx,
2762
0
                sm->addr, sm->p2p_dev_addr,
2763
0
                NULL, &psk_len, NULL))
2764
0
      key_len = psk_len;
2765
0
  }
2766
0
  res = wpa_write_ftie(conf, sm->wpa_key_mgmt, key_len,
2767
0
           r0kh_id, r0kh_id_len,
2768
0
           anonce, snonce, pos, end - pos,
2769
0
           subelem, subelem_len, rsnxe_used);
2770
0
  os_free(subelem);
2771
0
  if (res < 0)
2772
0
    return NULL;
2773
0
  ftie = pos;
2774
0
  ftie_len = res;
2775
0
  pos += res;
2776
2777
0
  if (sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY &&
2778
0
      key_len == SHA512_MAC_LEN) {
2779
0
    struct rsn_ftie_sha512 *_ftie =
2780
0
      (struct rsn_ftie_sha512 *) (ftie + 2);
2781
2782
0
    fte_mic = _ftie->mic;
2783
0
    elem_count = &_ftie->mic_control[1];
2784
0
  } else if ((sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY &&
2785
0
        key_len == SHA384_MAC_LEN) ||
2786
0
       wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) {
2787
0
    struct rsn_ftie_sha384 *_ftie =
2788
0
      (struct rsn_ftie_sha384 *) (ftie + 2);
2789
2790
0
    fte_mic = _ftie->mic;
2791
0
    elem_count = &_ftie->mic_control[1];
2792
0
  } else {
2793
0
    struct rsn_ftie *_ftie = (struct rsn_ftie *) (ftie + 2);
2794
2795
0
    fte_mic = _ftie->mic;
2796
0
    elem_count = &_ftie->mic_control[1];
2797
0
  }
2798
0
  if (auth_alg == WLAN_AUTH_FT)
2799
0
    *elem_count = 3; /* Information element count */
2800
2801
0
  ric_start = pos;
2802
0
  if (wpa_ft_parse_ies(req_ies, req_ies_len, &parse,
2803
0
           sm->wpa_key_mgmt, false) == 0 && parse.ric) {
2804
0
    pos = wpa_ft_process_ric(sm, pos, end, parse.ric,
2805
0
           parse.ric_len);
2806
0
    if (auth_alg == WLAN_AUTH_FT)
2807
0
      *elem_count +=
2808
0
        ieee802_11_ie_count(ric_start,
2809
0
                pos - ric_start);
2810
0
  }
2811
0
  if (ric_start == pos)
2812
0
    ric_start = NULL;
2813
2814
0
  if (omit_rsnxe) {
2815
0
    rsnxe_len = 0;
2816
0
  } else {
2817
0
    res = wpa_write_rsnxe(&sm->wpa_auth->conf, rsnxe,
2818
0
              sizeof(rsnxe_buf));
2819
0
    if (res < 0) {
2820
0
      pos = NULL;
2821
0
      goto fail;
2822
0
    }
2823
0
    rsnxe_len = res;
2824
0
  }
2825
#ifdef CONFIG_TESTING_OPTIONS
2826
  if (auth_alg == WLAN_AUTH_FT &&
2827
      sm->wpa_auth->conf.rsnxe_override_ft_set) {
2828
    wpa_printf(MSG_DEBUG,
2829
         "TESTING: RSNXE FT override for MIC calculation");
2830
    rsnxe = sm->wpa_auth->conf.rsnxe_override_ft;
2831
    rsnxe_len = sm->wpa_auth->conf.rsnxe_override_ft_len;
2832
  }
2833
#endif /* CONFIG_TESTING_OPTIONS */
2834
0
  if (auth_alg == WLAN_AUTH_FT && rsnxe_len)
2835
0
    *elem_count += 1;
2836
2837
0
  if (wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
2838
0
    kck = sm->PTK.kck2;
2839
0
    kck_len = sm->PTK.kck2_len;
2840
0
  } else {
2841
0
    kck = sm->PTK.kck;
2842
0
    kck_len = sm->PTK.kck_len;
2843
0
  }
2844
0
  if (auth_alg == WLAN_AUTH_FT &&
2845
0
      wpa_ft_mic(sm->wpa_key_mgmt, kck, kck_len,
2846
0
           sm->addr, sm->wpa_auth->addr, 6,
2847
0
           mdie, mdie_len, ftie, ftie_len,
2848
0
           rsnie, rsnie_len,
2849
0
           ric_start, ric_start ? pos - ric_start : 0,
2850
0
           rsnxe_len ? rsnxe : NULL, rsnxe_len,
2851
0
           NULL,
2852
0
           fte_mic) < 0) {
2853
0
    wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
2854
0
    pos = NULL;
2855
0
    goto fail;
2856
0
  }
2857
2858
0
  os_free(sm->assoc_resp_ftie);
2859
0
  sm->assoc_resp_ftie = os_malloc(ftie_len);
2860
0
  if (!sm->assoc_resp_ftie) {
2861
0
    pos = NULL;
2862
0
    goto fail;
2863
0
  }
2864
0
  os_memcpy(sm->assoc_resp_ftie, ftie, ftie_len);
2865
2866
0
fail:
2867
0
  wpa_ft_parse_ies_free(&parse);
2868
0
  return pos;
2869
0
}
2870
2871
2872
static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
2873
           int vlan_id,
2874
           enum wpa_alg alg, const u8 *addr, int idx,
2875
           u8 *key, size_t key_len,
2876
           enum key_flag key_flag)
2877
0
{
2878
0
  if (wpa_auth->cb->set_key == NULL)
2879
0
    return -1;
2880
0
  return wpa_auth->cb->set_key(wpa_auth->cb_ctx, vlan_id, alg, addr, idx,
2881
0
             key, key_len, key_flag);
2882
0
}
2883
2884
2885
#ifdef CONFIG_PASN
2886
static inline int wpa_auth_set_ltf_keyseed(struct wpa_authenticator *wpa_auth,
2887
             const u8 *peer_addr,
2888
             const u8 *ltf_keyseed,
2889
             size_t ltf_keyseed_len)
2890
{
2891
  if (!wpa_auth->cb->set_ltf_keyseed)
2892
    return -1;
2893
  return wpa_auth->cb->set_ltf_keyseed(wpa_auth->cb_ctx, peer_addr,
2894
               ltf_keyseed, ltf_keyseed_len);
2895
}
2896
#endif /* CONFIG_PASN */
2897
2898
2899
static inline int wpa_auth_add_sta_ft(struct wpa_authenticator *wpa_auth,
2900
              const u8 *addr)
2901
0
{
2902
0
  if (!wpa_auth->cb->add_sta_ft)
2903
0
    return -1;
2904
0
  return wpa_auth->cb->add_sta_ft(wpa_auth->cb_ctx, addr);
2905
0
}
2906
2907
2908
void wpa_ft_install_ptk(struct wpa_state_machine *sm, int retry)
2909
0
{
2910
0
  enum wpa_alg alg;
2911
0
  int klen;
2912
2913
  /* MLME-SETKEYS.request(PTK) */
2914
0
  alg = wpa_cipher_to_alg(sm->pairwise);
2915
0
  klen = wpa_cipher_key_len(sm->pairwise);
2916
0
  if (!wpa_cipher_valid_pairwise(sm->pairwise)) {
2917
0
    wpa_printf(MSG_DEBUG, "FT: Unknown pairwise alg 0x%x - skip "
2918
0
         "PTK configuration", sm->pairwise);
2919
0
    return;
2920
0
  }
2921
2922
0
  if (sm->tk_already_set) {
2923
    /* Must avoid TK reconfiguration to prevent clearing of TX/RX
2924
     * PN in the driver */
2925
0
    wpa_printf(MSG_DEBUG,
2926
0
         "FT: Do not re-install same PTK to the driver");
2927
0
    return;
2928
0
  }
2929
2930
0
  if (!retry)
2931
0
    wpa_auth_add_sta_ft(sm->wpa_auth, sm->addr);
2932
2933
  /* FIX: add STA entry to kernel/driver here? The set_key will fail
2934
   * most likely without this.. At the moment, STA entry is added only
2935
   * after association has been completed. This function will be called
2936
   * again after association to get the PTK configured, but that could be
2937
   * optimized by adding the STA entry earlier.
2938
   */
2939
0
  if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, sm->keyidx_active,
2940
0
           sm->PTK.tk, klen, KEY_FLAG_PAIRWISE_RX_TX))
2941
0
    return;
2942
2943
#ifdef CONFIG_PASN
2944
  if (sm->wpa_auth->conf.secure_ltf &&
2945
      ieee802_11_rsnx_capab(sm->rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF) &&
2946
      wpa_auth_set_ltf_keyseed(sm->wpa_auth, sm->addr,
2947
             sm->PTK.ltf_keyseed,
2948
             sm->PTK.ltf_keyseed_len)) {
2949
    wpa_printf(MSG_ERROR,
2950
         "FT: Failed to set LTF keyseed to driver");
2951
    return;
2952
  }
2953
#endif /* CONFIG_PASN */
2954
2955
  /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
2956
0
  sm->pairwise_set = true;
2957
0
  sm->tk_already_set = true;
2958
2959
0
  wpa_auth_store_ptksa(sm->wpa_auth, sm->addr, sm->pairwise,
2960
0
           dot11RSNAConfigPMKLifetime, &sm->PTK);
2961
0
}
2962
2963
2964
/* Derive PMK-R1 from PSK, check all available PSK */
2965
static int wpa_ft_psk_pmk_r1(struct wpa_state_machine *sm,
2966
           const u8 *req_pmk_r1_name,
2967
           u8 *out_pmk_r1, int *out_pairwise,
2968
           struct vlan_description *out_vlan,
2969
           const u8 **out_identity, size_t *out_identity_len,
2970
           const u8 **out_radius_cui,
2971
           size_t *out_radius_cui_len,
2972
           int *out_session_timeout)
2973
0
{
2974
0
  const u8 *pmk = NULL;
2975
0
  u8 pmk_r0[PMK_LEN], pmk_r0_name[WPA_PMK_NAME_LEN];
2976
0
  u8 pmk_r1[PMK_LEN], pmk_r1_name[WPA_PMK_NAME_LEN];
2977
0
  struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2978
0
  const u8 *mdid = wpa_auth->conf.mobility_domain;
2979
0
  const u8 *r0kh = sm->r0kh_id;
2980
0
  size_t r0kh_len = sm->r0kh_id_len;
2981
0
  const u8 *r1kh = wpa_auth->conf.r1_key_holder;
2982
0
  const u8 *ssid = wpa_auth->conf.ssid;
2983
0
  size_t ssid_len = wpa_auth->conf.ssid_len;
2984
0
  int pairwise;
2985
2986
0
  pairwise = sm->pairwise;
2987
2988
0
  for (;;) {
2989
0
    pmk = wpa_ft_get_psk(wpa_auth, sm->addr, sm->p2p_dev_addr,
2990
0
             pmk);
2991
0
    if (pmk == NULL)
2992
0
      break;
2993
2994
0
    if (wpa_derive_pmk_r0(pmk, PMK_LEN, ssid, ssid_len, mdid, r0kh,
2995
0
              r0kh_len, sm->addr,
2996
0
              pmk_r0, pmk_r0_name,
2997
0
              WPA_KEY_MGMT_FT_PSK) < 0 ||
2998
0
        wpa_derive_pmk_r1(pmk_r0, PMK_LEN, pmk_r0_name, r1kh,
2999
0
              sm->addr, pmk_r1, pmk_r1_name) < 0 ||
3000
0
        os_memcmp_const(pmk_r1_name, req_pmk_r1_name,
3001
0
            WPA_PMK_NAME_LEN) != 0)
3002
0
      continue;
3003
3004
    /* We found a PSK that matches the requested pmk_r1_name */
3005
0
    wpa_printf(MSG_DEBUG,
3006
0
         "FT: Found PSK to generate PMK-R1 locally");
3007
0
    os_memcpy(out_pmk_r1, pmk_r1, PMK_LEN);
3008
0
    if (out_pairwise)
3009
0
      *out_pairwise = pairwise;
3010
0
    os_memcpy(sm->PMK, pmk, PMK_LEN);
3011
0
    sm->pmk_len = PMK_LEN;
3012
0
    if (out_vlan &&
3013
0
        wpa_ft_get_vlan(sm->wpa_auth, sm->addr, out_vlan) < 0) {
3014
0
      wpa_printf(MSG_DEBUG, "FT: vlan not available for STA "
3015
0
           MACSTR, MAC2STR(sm->addr));
3016
0
      return -1;
3017
0
    }
3018
3019
0
    if (out_identity && out_identity_len) {
3020
0
      *out_identity_len = wpa_ft_get_identity(
3021
0
        sm->wpa_auth, sm->addr, out_identity);
3022
0
    }
3023
3024
0
    if (out_radius_cui && out_radius_cui_len) {
3025
0
      *out_radius_cui_len = wpa_ft_get_radius_cui(
3026
0
        sm->wpa_auth, sm->addr, out_radius_cui);
3027
0
    }
3028
3029
0
    if (out_session_timeout) {
3030
0
      *out_session_timeout = wpa_ft_get_session_timeout(
3031
0
        sm->wpa_auth, sm->addr);
3032
0
    }
3033
3034
0
    return 0;
3035
0
  }
3036
3037
0
  wpa_printf(MSG_DEBUG,
3038
0
       "FT: Did not find PSK to generate PMK-R1 locally");
3039
0
  return -1;
3040
0
}
3041
3042
3043
/* Detect the configuration the station asked for.
3044
 * Required to detect FT-PSK and pairwise cipher.
3045
 */
3046
static int wpa_ft_set_key_mgmt(struct wpa_state_machine *sm,
3047
             struct wpa_ft_ies *parse)
3048
0
{
3049
0
  int key_mgmt, ciphers;
3050
3051
0
  if (sm->wpa_key_mgmt)
3052
0
    return 0;
3053
3054
0
  key_mgmt = parse->key_mgmt & sm->wpa_auth->conf.wpa_key_mgmt;
3055
0
  if (!key_mgmt) {
3056
0
    wpa_printf(MSG_DEBUG, "FT: Invalid key mgmt (0x%x) from "
3057
0
         MACSTR, parse->key_mgmt, MAC2STR(sm->addr));
3058
0
    return -1;
3059
0
  }
3060
0
  if (key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
3061
0
    sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
3062
#ifdef CONFIG_SHA384
3063
  else if (key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X_SHA384)
3064
    sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X_SHA384;
3065
#endif /* CONFIG_SHA384 */
3066
0
  else if (key_mgmt & WPA_KEY_MGMT_FT_PSK)
3067
0
    sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_PSK;
3068
#ifdef CONFIG_FILS
3069
  else if (key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256)
3070
    sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_FILS_SHA256;
3071
  else if (key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384)
3072
    sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_FILS_SHA384;
3073
#endif /* CONFIG_FILS */
3074
0
  ciphers = parse->pairwise_cipher & sm->wpa_auth->conf.rsn_pairwise;
3075
0
  if (!ciphers) {
3076
0
    wpa_printf(MSG_DEBUG, "FT: Invalid pairwise cipher (0x%x) from "
3077
0
         MACSTR,
3078
0
         parse->pairwise_cipher, MAC2STR(sm->addr));
3079
0
    return -1;
3080
0
  }
3081
0
  sm->pairwise = wpa_pick_pairwise_cipher(ciphers, 0);
3082
3083
0
  return 0;
3084
0
}
3085
3086
3087
static int wpa_ft_local_derive_pmk_r1(struct wpa_authenticator *wpa_auth,
3088
              struct wpa_state_machine *sm,
3089
              const u8 *r0kh_id, size_t r0kh_id_len,
3090
              const u8 *req_pmk_r0_name,
3091
              u8 *out_pmk_r1_name,
3092
              u8 *out_pmk_r1, int *out_pairwise,
3093
              struct vlan_description *vlan,
3094
              const u8 **identity, size_t *identity_len,
3095
              const u8 **radius_cui,
3096
              size_t *radius_cui_len,
3097
              int *out_session_timeout,
3098
              size_t *pmk_r1_len)
3099
0
{
3100
0
  struct wpa_auth_config *conf = &wpa_auth->conf;
3101
0
  const struct wpa_ft_pmk_r0_sa *r0;
3102
0
  int expires_in = 0;
3103
0
  int session_timeout = 0;
3104
0
  struct os_reltime now;
3105
3106
0
  if (conf->r0_key_holder_len != r0kh_id_len ||
3107
0
      os_memcmp(conf->r0_key_holder, r0kh_id, conf->r0_key_holder_len) !=
3108
0
      0)
3109
0
    return -1; /* not our R0KH-ID */
3110
3111
0
  wpa_printf(MSG_DEBUG, "FT: STA R0KH-ID matching local configuration");
3112
0
  if (wpa_ft_fetch_pmk_r0(sm->wpa_auth, sm->addr, req_pmk_r0_name, &r0) <
3113
0
      0)
3114
0
    return -1; /* no matching PMKR0Name in local cache */
3115
3116
0
  wpa_printf(MSG_DEBUG, "FT: Requested PMKR0Name found in local cache");
3117
3118
0
  if (wpa_derive_pmk_r1(r0->pmk_r0, r0->pmk_r0_len, r0->pmk_r0_name,
3119
0
            conf->r1_key_holder,
3120
0
            sm->addr, out_pmk_r1, out_pmk_r1_name) < 0)
3121
0
    return -1;
3122
3123
0
  os_get_reltime(&now);
3124
0
  if (r0->expiration)
3125
0
    expires_in = r0->expiration - now.sec;
3126
3127
0
  if (r0->session_timeout)
3128
0
    session_timeout = r0->session_timeout - now.sec;
3129
3130
0
  wpa_ft_store_pmk_r1(wpa_auth, sm->addr, out_pmk_r1, r0->pmk_r0_len,
3131
0
          out_pmk_r1_name,
3132
0
          sm->pairwise, r0->vlan, expires_in, session_timeout,
3133
0
          r0->identity, r0->identity_len,
3134
0
          r0->radius_cui, r0->radius_cui_len);
3135
3136
0
  *out_pairwise = sm->pairwise;
3137
0
  if (vlan) {
3138
0
    if (r0->vlan)
3139
0
      *vlan = *r0->vlan;
3140
0
    else
3141
0
      os_memset(vlan, 0, sizeof(*vlan));
3142
0
  }
3143
3144
0
  if (identity && identity_len) {
3145
0
    *identity = r0->identity;
3146
0
    *identity_len = r0->identity_len;
3147
0
  }
3148
3149
0
  if (radius_cui && radius_cui_len) {
3150
0
    *radius_cui = r0->radius_cui;
3151
0
    *radius_cui_len = r0->radius_cui_len;
3152
0
  }
3153
3154
0
  *out_session_timeout = session_timeout;
3155
3156
0
  *pmk_r1_len = r0->pmk_r0_len;
3157
3158
0
  return 0;
3159
0
}
3160
3161
3162
static int wpa_ft_process_auth_req(struct wpa_state_machine *sm,
3163
           const u8 *ies, size_t ies_len,
3164
           u8 **resp_ies, size_t *resp_ies_len)
3165
0
{
3166
0
  struct rsn_mdie *mdie;
3167
0
  u8 pmk_r1[PMK_LEN_MAX], pmk_r1_name[WPA_PMK_NAME_LEN];
3168
0
  u8 ptk_name[WPA_PMK_NAME_LEN];
3169
0
  struct wpa_auth_config *conf;
3170
0
  struct wpa_ft_ies parse;
3171
0
  size_t buflen;
3172
0
  int ret;
3173
0
  u8 *pos, *end;
3174
0
  int pairwise, session_timeout = 0;
3175
0
  struct vlan_description vlan;
3176
0
  const u8 *identity, *radius_cui;
3177
0
  size_t identity_len = 0, radius_cui_len = 0;
3178
0
  size_t pmk_r1_len, kdk_len, len;
3179
0
  int retval = WLAN_STATUS_UNSPECIFIED_FAILURE;
3180
3181
0
  *resp_ies = NULL;
3182
0
  *resp_ies_len = 0;
3183
3184
0
  sm->pmk_r1_name_valid = 0;
3185
0
  conf = &sm->wpa_auth->conf;
3186
3187
0
  wpa_hexdump(MSG_DEBUG, "FT: Received authentication frame IEs",
3188
0
        ies, ies_len);
3189
3190
0
  if (wpa_ft_parse_ies(ies, ies_len, &parse, 0, false)) {
3191
0
    wpa_printf(MSG_DEBUG, "FT: Failed to parse FT IEs");
3192
0
    return WLAN_STATUS_UNSPECIFIED_FAILURE;
3193
0
  }
3194
3195
0
  mdie = (struct rsn_mdie *) parse.mdie;
3196
0
  if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
3197
0
      os_memcmp(mdie->mobility_domain,
3198
0
          sm->wpa_auth->conf.mobility_domain,
3199
0
          MOBILITY_DOMAIN_ID_LEN) != 0) {
3200
0
    wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
3201
0
    retval = WLAN_STATUS_INVALID_MDIE;
3202
0
    goto out;
3203
0
  }
3204
3205
0
  if (!parse.ftie || parse.ftie_len < sizeof(struct rsn_ftie)) {
3206
0
    wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
3207
0
    retval = WLAN_STATUS_INVALID_FTIE;
3208
0
    goto out;
3209
0
  }
3210
3211
0
  if (parse.r0kh_id == NULL) {
3212
0
    wpa_printf(MSG_DEBUG, "FT: Invalid FTIE - no R0KH-ID");
3213
0
    retval = WLAN_STATUS_INVALID_FTIE;
3214
0
    goto out;
3215
0
  }
3216
3217
0
  wpa_hexdump(MSG_DEBUG, "FT: STA R0KH-ID",
3218
0
        parse.r0kh_id, parse.r0kh_id_len);
3219
0
  os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len);
3220
0
  sm->r0kh_id_len = parse.r0kh_id_len;
3221
3222
0
  if (parse.rsn_pmkid == NULL) {
3223
0
    wpa_printf(MSG_DEBUG, "FT: No PMKID in RSNIE");
3224
0
    retval = WLAN_STATUS_INVALID_PMKID;
3225
0
    goto out;
3226
0
  }
3227
3228
0
  if (wpa_ft_set_key_mgmt(sm, &parse) < 0)
3229
0
    goto out;
3230
3231
0
  wpa_hexdump(MSG_DEBUG, "FT: Requested PMKR0Name",
3232
0
        parse.rsn_pmkid, WPA_PMK_NAME_LEN);
3233
3234
0
  if (conf->ft_psk_generate_local &&
3235
0
      wpa_key_mgmt_ft_psk(sm->wpa_key_mgmt)) {
3236
0
    if (wpa_derive_pmk_r1_name(parse.rsn_pmkid,
3237
0
             sm->wpa_auth->conf.r1_key_holder,
3238
0
             sm->addr, pmk_r1_name, PMK_LEN) < 0)
3239
0
      goto out;
3240
0
    if (wpa_ft_psk_pmk_r1(sm, pmk_r1_name, pmk_r1, &pairwise,
3241
0
              &vlan, &identity, &identity_len,
3242
0
              &radius_cui, &radius_cui_len,
3243
0
              &session_timeout) < 0) {
3244
0
      retval = WLAN_STATUS_INVALID_PMKID;
3245
0
      goto out;
3246
0
    }
3247
0
    pmk_r1_len = PMK_LEN;
3248
0
    wpa_printf(MSG_DEBUG,
3249
0
         "FT: Generated PMK-R1 for FT-PSK locally");
3250
0
    goto pmk_r1_derived;
3251
0
  }
3252
3253
  /* Need to test all possible hash algorithms for FT-SAE-EXT-KEY since
3254
   * the key length is not yet known. For other AKMs, only the length
3255
   * identified by the AKM is used. */
3256
0
  for (len = SHA256_MAC_LEN; len <= SHA512_MAC_LEN; len += 16) {
3257
0
    if (parse.key_mgmt != WPA_KEY_MGMT_FT_SAE_EXT_KEY &&
3258
0
        ((wpa_key_mgmt_sha384(parse.key_mgmt) &&
3259
0
          len != SHA384_MAC_LEN) ||
3260
0
         (!wpa_key_mgmt_sha384(parse.key_mgmt) &&
3261
0
          len != SHA256_MAC_LEN)))
3262
0
      continue;
3263
0
    if (wpa_derive_pmk_r1_name(parse.rsn_pmkid,
3264
0
             sm->wpa_auth->conf.r1_key_holder,
3265
0
             sm->addr, pmk_r1_name, len) < 0)
3266
0
      continue;
3267
3268
0
    if (wpa_ft_fetch_pmk_r1(sm->wpa_auth, sm->addr, pmk_r1_name,
3269
0
          pmk_r1, &pmk_r1_len, &pairwise, &vlan,
3270
0
          &identity, &identity_len, &radius_cui,
3271
0
          &radius_cui_len,
3272
0
          &session_timeout) == 0) {
3273
0
      wpa_printf(MSG_DEBUG,
3274
0
           "FT: Found PMKR1Name (using SHA%zu) from local cache",
3275
0
           pmk_r1_len * 8);
3276
0
      goto pmk_r1_derived;
3277
0
    }
3278
0
  }
3279
3280
0
  wpa_printf(MSG_DEBUG,
3281
0
       "FT: No PMK-R1 available in local cache for the requested PMKR1Name");
3282
0
  if (wpa_ft_local_derive_pmk_r1(sm->wpa_auth, sm,
3283
0
               parse.r0kh_id, parse.r0kh_id_len,
3284
0
               parse.rsn_pmkid,
3285
0
               pmk_r1_name, pmk_r1, &pairwise,
3286
0
               &vlan, &identity, &identity_len,
3287
0
               &radius_cui, &radius_cui_len,
3288
0
               &session_timeout, &pmk_r1_len) == 0) {
3289
0
    wpa_printf(MSG_DEBUG,
3290
0
         "FT: Generated PMK-R1 based on local PMK-R0");
3291
0
    goto pmk_r1_derived;
3292
0
  }
3293
3294
0
  if (wpa_ft_pull_pmk_r1(sm, ies, ies_len, parse.rsn_pmkid) < 0) {
3295
0
    wpa_printf(MSG_DEBUG,
3296
0
         "FT: Did not have matching PMK-R1 and either unknown or blocked R0KH-ID or NAK from R0KH");
3297
0
    retval = WLAN_STATUS_INVALID_PMKID;
3298
0
    goto out;
3299
0
  }
3300
3301
0
  retval = -1; /* Status pending */
3302
0
  goto out;
3303
3304
0
pmk_r1_derived:
3305
0
  wpa_hexdump_key(MSG_DEBUG, "FT: Selected PMK-R1", pmk_r1, pmk_r1_len);
3306
0
  sm->pmk_r1_name_valid = 1;
3307
0
  os_memcpy(sm->pmk_r1_name, pmk_r1_name, WPA_PMK_NAME_LEN);
3308
0
  os_memcpy(sm->pmk_r1, pmk_r1, pmk_r1_len);
3309
0
  sm->pmk_r1_len = pmk_r1_len;
3310
3311
0
  if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
3312
0
    wpa_printf(MSG_DEBUG, "FT: Failed to get random data for "
3313
0
         "ANonce");
3314
0
    goto out;
3315
0
  }
3316
3317
  /* Now that we know the correct PMK-R1 length and as such, the length
3318
   * of the MIC field, fetch the SNonce. */
3319
0
  if (pmk_r1_len == SHA512_MAC_LEN) {
3320
0
    const struct rsn_ftie_sha512 *ftie;
3321
3322
0
    ftie = (const struct rsn_ftie_sha512 *) parse.ftie;
3323
0
    if (!ftie || parse.ftie_len < sizeof(*ftie)) {
3324
0
      wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
3325
0
      retval = WLAN_STATUS_INVALID_FTIE;
3326
0
      goto out;
3327
0
    }
3328
3329
0
    os_memcpy(sm->SNonce, ftie->snonce, WPA_NONCE_LEN);
3330
0
  } else if (pmk_r1_len == SHA384_MAC_LEN) {
3331
0
    const struct rsn_ftie_sha384 *ftie;
3332
3333
0
    ftie = (const struct rsn_ftie_sha384 *) parse.ftie;
3334
0
    if (!ftie || parse.ftie_len < sizeof(*ftie)) {
3335
0
      wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
3336
0
      retval = WLAN_STATUS_INVALID_FTIE;
3337
0
      goto out;
3338
0
    }
3339
3340
0
    os_memcpy(sm->SNonce, ftie->snonce, WPA_NONCE_LEN);
3341
0
  } else {
3342
0
    const struct rsn_ftie *ftie;
3343
3344
0
    ftie = (const struct rsn_ftie *) parse.ftie;
3345
0
    if (!ftie || parse.ftie_len < sizeof(*ftie)) {
3346
0
      wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
3347
0
      retval = WLAN_STATUS_INVALID_FTIE;
3348
0
      goto out;
3349
0
    }
3350
3351
0
    os_memcpy(sm->SNonce, ftie->snonce, WPA_NONCE_LEN);
3352
0
  }
3353
3354
0
  wpa_hexdump(MSG_DEBUG, "FT: Received SNonce",
3355
0
        sm->SNonce, WPA_NONCE_LEN);
3356
0
  wpa_hexdump(MSG_DEBUG, "FT: Generated ANonce",
3357
0
        sm->ANonce, WPA_NONCE_LEN);
3358
3359
0
  if (sm->wpa_auth->conf.force_kdk_derivation ||
3360
0
      (sm->wpa_auth->conf.secure_ltf &&
3361
0
       ieee802_11_rsnx_capab(sm->rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF)))
3362
0
    kdk_len = WPA_KDK_MAX_LEN;
3363
0
  else
3364
0
    kdk_len = 0;
3365
3366
0
  if (wpa_pmk_r1_to_ptk(pmk_r1, pmk_r1_len, sm->SNonce, sm->ANonce,
3367
0
            sm->addr, sm->wpa_auth->addr, pmk_r1_name,
3368
0
            &sm->PTK, ptk_name, parse.key_mgmt,
3369
0
            pairwise, kdk_len) < 0)
3370
0
    goto out;
3371
3372
#ifdef CONFIG_PASN
3373
  if (sm->wpa_auth->conf.secure_ltf &&
3374
      ieee802_11_rsnx_capab(sm->rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF) &&
3375
      wpa_ltf_keyseed(&sm->PTK, parse.key_mgmt, pairwise)) {
3376
    wpa_printf(MSG_DEBUG, "FT: Failed to derive LTF keyseed");
3377
    goto out;
3378
  }
3379
#endif /* CONFIG_PASN */
3380
3381
0
  sm->pairwise = pairwise;
3382
0
  sm->PTK_valid = true;
3383
0
  sm->tk_already_set = false;
3384
0
  wpa_ft_install_ptk(sm, 0);
3385
3386
0
  if (wpa_ft_set_vlan(sm->wpa_auth, sm->addr, &vlan) < 0) {
3387
0
    wpa_printf(MSG_DEBUG, "FT: Failed to configure VLAN");
3388
0
    goto out;
3389
0
  }
3390
0
  if (wpa_ft_set_identity(sm->wpa_auth, sm->addr,
3391
0
        identity, identity_len) < 0 ||
3392
0
      wpa_ft_set_radius_cui(sm->wpa_auth, sm->addr,
3393
0
          radius_cui, radius_cui_len) < 0) {
3394
0
    wpa_printf(MSG_DEBUG, "FT: Failed to configure identity/CUI");
3395
0
    goto out;
3396
0
  }
3397
0
  wpa_ft_set_session_timeout(sm->wpa_auth, sm->addr, session_timeout);
3398
3399
0
  buflen = 2 + sizeof(struct rsn_mdie) + 2 + sizeof(struct rsn_ftie) +
3400
0
    2 + FT_R1KH_ID_LEN + 200;
3401
0
  *resp_ies = os_zalloc(buflen);
3402
0
  if (*resp_ies == NULL)
3403
0
    goto fail;
3404
3405
0
  pos = *resp_ies;
3406
0
  end = *resp_ies + buflen;
3407
3408
0
  ret = wpa_write_rsn_ie(conf, pos, end - pos, parse.rsn_pmkid);
3409
0
  if (ret < 0)
3410
0
    goto fail;
3411
0
  pos += ret;
3412
3413
0
  ret = wpa_write_mdie(conf, pos, end - pos);
3414
0
  if (ret < 0)
3415
0
    goto fail;
3416
0
  pos += ret;
3417
3418
0
  ret = wpa_write_ftie(conf, parse.key_mgmt, pmk_r1_len,
3419
0
           parse.r0kh_id, parse.r0kh_id_len,
3420
0
           sm->ANonce, sm->SNonce, pos, end - pos, NULL, 0,
3421
0
           0);
3422
0
  if (ret < 0)
3423
0
    goto fail;
3424
0
  pos += ret;
3425
3426
0
  *resp_ies_len = pos - *resp_ies;
3427
3428
0
  retval = WLAN_STATUS_SUCCESS;
3429
0
  goto out;
3430
0
fail:
3431
0
  os_free(*resp_ies);
3432
0
  *resp_ies = NULL;
3433
0
out:
3434
0
  wpa_ft_parse_ies_free(&parse);
3435
0
  return retval;
3436
0
}
3437
3438
3439
void wpa_ft_process_auth(struct wpa_state_machine *sm,
3440
       u16 auth_transaction, const u8 *ies, size_t ies_len,
3441
       void (*cb)(void *ctx, const u8 *dst,
3442
            u16 auth_transaction, u16 status,
3443
            const u8 *ies, size_t ies_len),
3444
       void *ctx)
3445
0
{
3446
0
  u16 status;
3447
0
  u8 *resp_ies;
3448
0
  size_t resp_ies_len;
3449
0
  int res;
3450
3451
0
  if (sm == NULL) {
3452
0
    wpa_printf(MSG_DEBUG, "FT: Received authentication frame, but "
3453
0
         "WPA SM not available");
3454
0
    return;
3455
0
  }
3456
3457
0
  wpa_printf(MSG_DEBUG, "FT: Received authentication frame: STA=" MACSTR
3458
0
       " BSSID=" MACSTR " transaction=%d",
3459
0
       MAC2STR(sm->addr), MAC2STR(sm->wpa_auth->addr),
3460
0
       auth_transaction);
3461
0
  sm->ft_pending_cb = cb;
3462
0
  sm->ft_pending_cb_ctx = ctx;
3463
0
  sm->ft_pending_auth_transaction = auth_transaction;
3464
0
  sm->ft_pending_pull_left_retries = sm->wpa_auth->conf.rkh_pull_retries;
3465
0
  res = wpa_ft_process_auth_req(sm, ies, ies_len, &resp_ies,
3466
0
              &resp_ies_len);
3467
0
  if (res < 0) {
3468
0
    wpa_printf(MSG_DEBUG, "FT: Callback postponed until response is available");
3469
0
    return;
3470
0
  }
3471
0
  status = res;
3472
3473
0
  wpa_printf(MSG_DEBUG, "FT: FT authentication response: dst=" MACSTR
3474
0
       " auth_transaction=%d status=%u (%s)",
3475
0
       MAC2STR(sm->addr), auth_transaction + 1, status,
3476
0
       status2str(status));
3477
0
  wpa_hexdump(MSG_DEBUG, "FT: Response IEs", resp_ies, resp_ies_len);
3478
0
  cb(ctx, sm->addr, auth_transaction + 1, status, resp_ies, resp_ies_len);
3479
0
  os_free(resp_ies);
3480
0
}
3481
3482
3483
int wpa_ft_validate_reassoc(struct wpa_state_machine *sm, const u8 *ies,
3484
          size_t ies_len)
3485
0
{
3486
0
  struct wpa_ft_ies parse;
3487
0
  struct rsn_mdie *mdie;
3488
0
  u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
3489
0
  size_t mic_len;
3490
0
  unsigned int count;
3491
0
  const u8 *kck;
3492
0
  size_t kck_len;
3493
0
  struct wpa_auth_config *conf;
3494
0
  int retval = WLAN_STATUS_UNSPECIFIED_FAILURE;
3495
3496
0
  if (sm == NULL)
3497
0
    return WLAN_STATUS_UNSPECIFIED_FAILURE;
3498
3499
0
  conf = &sm->wpa_auth->conf;
3500
3501
0
  wpa_hexdump(MSG_DEBUG, "FT: Reassoc Req IEs", ies, ies_len);
3502
3503
0
  if (wpa_ft_parse_ies(ies, ies_len, &parse, sm->wpa_key_mgmt,
3504
0
           false) < 0) {
3505
0
    wpa_printf(MSG_DEBUG, "FT: Failed to parse FT IEs");
3506
0
    return WLAN_STATUS_UNSPECIFIED_FAILURE;
3507
0
  }
3508
3509
0
  if (parse.rsn == NULL) {
3510
0
    wpa_printf(MSG_DEBUG, "FT: No RSNIE in Reassoc Req");
3511
0
    goto out;
3512
0
  }
3513
3514
0
  if (parse.rsn_pmkid == NULL) {
3515
0
    wpa_printf(MSG_DEBUG, "FT: No PMKID in RSNIE");
3516
0
    retval = WLAN_STATUS_INVALID_PMKID;
3517
0
    goto out;
3518
0
  }
3519
3520
0
  if (os_memcmp_const(parse.rsn_pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN)
3521
0
      != 0) {
3522
0
    wpa_printf(MSG_DEBUG, "FT: PMKID in Reassoc Req did not match "
3523
0
         "with the PMKR1Name derived from auth request");
3524
0
    retval = WLAN_STATUS_INVALID_PMKID;
3525
0
    goto out;
3526
0
  }
3527
3528
0
  mdie = (struct rsn_mdie *) parse.mdie;
3529
0
  if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
3530
0
      os_memcmp(mdie->mobility_domain, conf->mobility_domain,
3531
0
          MOBILITY_DOMAIN_ID_LEN) != 0) {
3532
0
    wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
3533
0
    retval = WLAN_STATUS_INVALID_MDIE;
3534
0
    goto out;
3535
0
  }
3536
3537
0
  if (sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY &&
3538
0
      sm->pmk_r1_len == SHA512_MAC_LEN)
3539
0
    mic_len = 32;
3540
0
  else if ((sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY &&
3541
0
      sm->pmk_r1_len == SHA384_MAC_LEN) ||
3542
0
     wpa_key_mgmt_sha384(sm->wpa_key_mgmt))
3543
0
    mic_len = 24;
3544
0
  else
3545
0
    mic_len = 16;
3546
3547
0
  if (!parse.ftie || !parse.fte_anonce || !parse.fte_snonce ||
3548
0
      parse.fte_mic_len != mic_len) {
3549
0
    wpa_printf(MSG_DEBUG,
3550
0
         "FT: Invalid FTE (fte_mic_len=%zu mic_len=%zu)",
3551
0
         parse.fte_mic_len, mic_len);
3552
0
    retval = WLAN_STATUS_INVALID_FTIE;
3553
0
    goto out;
3554
0
  }
3555
3556
0
  if (os_memcmp(parse.fte_snonce, sm->SNonce, WPA_NONCE_LEN) != 0) {
3557
0
    wpa_printf(MSG_DEBUG, "FT: SNonce mismatch in FTIE");
3558
0
    wpa_hexdump(MSG_DEBUG, "FT: Received SNonce",
3559
0
          parse.fte_snonce, WPA_NONCE_LEN);
3560
0
    wpa_hexdump(MSG_DEBUG, "FT: Expected SNonce",
3561
0
          sm->SNonce, WPA_NONCE_LEN);
3562
0
    retval = WLAN_STATUS_INVALID_FTIE;
3563
0
    goto out;
3564
0
  }
3565
3566
0
  if (os_memcmp(parse.fte_anonce, sm->ANonce, WPA_NONCE_LEN) != 0) {
3567
0
    wpa_printf(MSG_DEBUG, "FT: ANonce mismatch in FTIE");
3568
0
    wpa_hexdump(MSG_DEBUG, "FT: Received ANonce",
3569
0
          parse.fte_anonce, WPA_NONCE_LEN);
3570
0
    wpa_hexdump(MSG_DEBUG, "FT: Expected ANonce",
3571
0
          sm->ANonce, WPA_NONCE_LEN);
3572
0
    retval = WLAN_STATUS_INVALID_FTIE;
3573
0
    goto out;
3574
0
  }
3575
3576
0
  if (parse.r0kh_id == NULL) {
3577
0
    wpa_printf(MSG_DEBUG, "FT: No R0KH-ID subelem in FTIE");
3578
0
    retval = WLAN_STATUS_INVALID_FTIE;
3579
0
    goto out;
3580
0
  }
3581
3582
0
  if (parse.r0kh_id_len != sm->r0kh_id_len ||
3583
0
      os_memcmp_const(parse.r0kh_id, sm->r0kh_id, parse.r0kh_id_len) != 0)
3584
0
  {
3585
0
    wpa_printf(MSG_DEBUG, "FT: R0KH-ID in FTIE did not match with "
3586
0
         "the current R0KH-ID");
3587
0
    wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID in FTIE",
3588
0
          parse.r0kh_id, parse.r0kh_id_len);
3589
0
    wpa_hexdump(MSG_DEBUG, "FT: The current R0KH-ID",
3590
0
          sm->r0kh_id, sm->r0kh_id_len);
3591
0
    retval = WLAN_STATUS_INVALID_FTIE;
3592
0
    goto out;
3593
0
  }
3594
3595
0
  if (parse.r1kh_id == NULL) {
3596
0
    wpa_printf(MSG_DEBUG, "FT: No R1KH-ID subelem in FTIE");
3597
0
    retval = WLAN_STATUS_INVALID_FTIE;
3598
0
    goto out;
3599
0
  }
3600
3601
0
  if (os_memcmp_const(parse.r1kh_id, conf->r1_key_holder,
3602
0
          FT_R1KH_ID_LEN) != 0) {
3603
0
    wpa_printf(MSG_DEBUG, "FT: Unknown R1KH-ID used in "
3604
0
         "ReassocReq");
3605
0
    wpa_hexdump(MSG_DEBUG, "FT: R1KH-ID in FTIE",
3606
0
          parse.r1kh_id, FT_R1KH_ID_LEN);
3607
0
    wpa_hexdump(MSG_DEBUG, "FT: Expected R1KH-ID",
3608
0
          conf->r1_key_holder, FT_R1KH_ID_LEN);
3609
0
    retval = WLAN_STATUS_INVALID_FTIE;
3610
0
    goto out;
3611
0
  }
3612
3613
0
  if (parse.rsn_pmkid == NULL ||
3614
0
      os_memcmp_const(parse.rsn_pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN))
3615
0
  {
3616
0
    wpa_printf(MSG_DEBUG, "FT: No matching PMKR1Name (PMKID) in "
3617
0
         "RSNIE (pmkid=%d)", !!parse.rsn_pmkid);
3618
0
    retval = WLAN_STATUS_INVALID_PMKID;
3619
0
    goto out;
3620
0
  }
3621
3622
0
  count = 3;
3623
0
  if (parse.ric)
3624
0
    count += ieee802_11_ie_count(parse.ric, parse.ric_len);
3625
0
  if (parse.rsnxe)
3626
0
    count++;
3627
0
  if (parse.fte_elem_count != count) {
3628
0
    wpa_printf(MSG_DEBUG, "FT: Unexpected IE count in MIC "
3629
0
         "Control: received %u expected %u",
3630
0
         parse.fte_elem_count, count);
3631
0
    goto out;
3632
0
  }
3633
3634
0
  if (wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
3635
0
    kck = sm->PTK.kck2;
3636
0
    kck_len = sm->PTK.kck2_len;
3637
0
  } else {
3638
0
    kck = sm->PTK.kck;
3639
0
    kck_len = sm->PTK.kck_len;
3640
0
  }
3641
0
  if (wpa_ft_mic(sm->wpa_key_mgmt, kck, kck_len,
3642
0
           sm->addr, sm->wpa_auth->addr, 5,
3643
0
           parse.mdie - 2, parse.mdie_len + 2,
3644
0
           parse.ftie - 2, parse.ftie_len + 2,
3645
0
           parse.rsn - 2, parse.rsn_len + 2,
3646
0
           parse.ric, parse.ric_len,
3647
0
           parse.rsnxe ? parse.rsnxe - 2 : NULL,
3648
0
           parse.rsnxe ? parse.rsnxe_len + 2 : 0,
3649
0
           NULL,
3650
0
           mic) < 0) {
3651
0
    wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
3652
0
    goto out;
3653
0
  }
3654
3655
0
  if (os_memcmp_const(mic, parse.fte_mic, mic_len) != 0) {
3656
0
    wpa_printf(MSG_DEBUG, "FT: Invalid MIC in FTIE");
3657
0
    wpa_printf(MSG_DEBUG, "FT: addr=" MACSTR " auth_addr=" MACSTR,
3658
0
         MAC2STR(sm->addr), MAC2STR(sm->wpa_auth->addr));
3659
0
    wpa_hexdump(MSG_MSGDUMP, "FT: Received MIC",
3660
0
          parse.fte_mic, mic_len);
3661
0
    wpa_hexdump(MSG_MSGDUMP, "FT: Calculated MIC", mic, mic_len);
3662
0
    wpa_hexdump(MSG_MSGDUMP, "FT: MDIE",
3663
0
          parse.mdie - 2, parse.mdie_len + 2);
3664
0
    wpa_hexdump(MSG_MSGDUMP, "FT: FTIE",
3665
0
          parse.ftie - 2, parse.ftie_len + 2);
3666
0
    wpa_hexdump(MSG_MSGDUMP, "FT: RSN",
3667
0
          parse.rsn - 2, parse.rsn_len + 2);
3668
0
    wpa_hexdump(MSG_MSGDUMP, "FT: RSNXE",
3669
0
          parse.rsnxe ? parse.rsnxe - 2 : NULL,
3670
0
          parse.rsnxe ? parse.rsnxe_len + 2 : 0);
3671
0
    retval = WLAN_STATUS_INVALID_FTIE;
3672
0
    goto out;
3673
0
  }
3674
3675
0
  if (parse.fte_rsnxe_used &&
3676
0
      (conf->sae_pwe == SAE_PWE_HASH_TO_ELEMENT ||
3677
0
       conf->sae_pwe == SAE_PWE_BOTH) &&
3678
0
      !parse.rsnxe) {
3679
0
    wpa_printf(MSG_INFO,
3680
0
         "FT: FTE indicated that STA uses RSNXE, but RSNXE was not included");
3681
0
    retval = -1; /* discard request */
3682
0
    goto out;
3683
0
  }
3684
3685
#ifdef CONFIG_OCV
3686
  if (wpa_auth_uses_ocv(sm)) {
3687
    struct wpa_channel_info ci;
3688
    int tx_chanwidth;
3689
    int tx_seg1_idx;
3690
    enum oci_verify_result res;
3691
3692
    if (wpa_channel_info(sm->wpa_auth, &ci) != 0) {
3693
      wpa_printf(MSG_WARNING,
3694
           "Failed to get channel info to validate received OCI in (Re)Assoc Request");
3695
      goto out;
3696
    }
3697
3698
    if (get_sta_tx_parameters(sm,
3699
            channel_width_to_int(ci.chanwidth),
3700
            ci.seg1_idx, &tx_chanwidth,
3701
            &tx_seg1_idx) < 0)
3702
      goto out;
3703
3704
    res = ocv_verify_tx_params(parse.oci, parse.oci_len, &ci,
3705
             tx_chanwidth, tx_seg1_idx);
3706
    if (wpa_auth_uses_ocv(sm) == 2 && res == OCI_NOT_FOUND) {
3707
      /* Work around misbehaving STAs */
3708
      wpa_printf(MSG_INFO,
3709
           "Disable OCV with a STA that does not send OCI");
3710
      wpa_auth_set_ocv(sm, 0);
3711
    } else if (res != OCI_SUCCESS) {
3712
      wpa_printf(MSG_WARNING, "OCV failed: %s", ocv_errorstr);
3713
      if (sm->wpa_auth->conf.msg_ctx)
3714
        wpa_msg(sm->wpa_auth->conf.msg_ctx, MSG_INFO,
3715
          OCV_FAILURE "addr=" MACSTR
3716
          " frame=ft-reassoc-req error=%s",
3717
          MAC2STR(sm->addr), ocv_errorstr);
3718
      retval = WLAN_STATUS_INVALID_FTIE;
3719
      goto out;
3720
    }
3721
  }
3722
#endif /* CONFIG_OCV */
3723
3724
0
  retval = WLAN_STATUS_SUCCESS;
3725
0
out:
3726
0
  wpa_ft_parse_ies_free(&parse);
3727
0
  return retval;
3728
0
}
3729
3730
3731
int wpa_ft_action_rx(struct wpa_state_machine *sm, const u8 *data, size_t len)
3732
0
{
3733
0
  const u8 *sta_addr, *target_ap;
3734
0
  const u8 *ies;
3735
0
  size_t ies_len;
3736
0
  u8 action;
3737
0
  struct ft_rrb_frame *frame;
3738
3739
0
  if (sm == NULL)
3740
0
    return -1;
3741
3742
  /*
3743
   * data: Category[1] Action[1] STA_Address[6] Target_AP_Address[6]
3744
   * FT Request action frame body[variable]
3745
   */
3746
3747
0
  if (len < 14) {
3748
0
    wpa_printf(MSG_DEBUG, "FT: Too short FT Action frame "
3749
0
         "(len=%lu)", (unsigned long) len);
3750
0
    return -1;
3751
0
  }
3752
3753
0
  action = data[1];
3754
0
  sta_addr = data + 2;
3755
0
  target_ap = data + 8;
3756
0
  ies = data + 14;
3757
0
  ies_len = len - 14;
3758
3759
0
  wpa_printf(MSG_DEBUG, "FT: Received FT Action frame (STA=" MACSTR
3760
0
       " Target AP=" MACSTR " Action=%d)",
3761
0
       MAC2STR(sta_addr), MAC2STR(target_ap), action);
3762
3763
0
  if (!ether_addr_equal(sta_addr, sm->addr)) {
3764
0
    wpa_printf(MSG_DEBUG, "FT: Mismatch in FT Action STA address: "
3765
0
         "STA=" MACSTR " STA-Address=" MACSTR,
3766
0
         MAC2STR(sm->addr), MAC2STR(sta_addr));
3767
0
    return -1;
3768
0
  }
3769
3770
  /*
3771
   * Do some validity checking on the target AP address (not own and not
3772
   * broadcast. This could be extended to filter based on a list of known
3773
   * APs in the MD (if such a list were configured).
3774
   */
3775
0
  if ((target_ap[0] & 0x01) ||
3776
0
      ether_addr_equal(target_ap, sm->wpa_auth->addr)) {
3777
0
    wpa_printf(MSG_DEBUG, "FT: Invalid Target AP in FT Action "
3778
0
         "frame");
3779
0
    return -1;
3780
0
  }
3781
3782
0
  wpa_hexdump(MSG_MSGDUMP, "FT: Action frame body", ies, ies_len);
3783
3784
0
  if (!sm->wpa_auth->conf.ft_over_ds) {
3785
0
    wpa_printf(MSG_DEBUG, "FT: Over-DS option disabled - reject");
3786
0
    return -1;
3787
0
  }
3788
3789
  /* RRB - Forward action frame to the target AP */
3790
0
  frame = os_malloc(sizeof(*frame) + len);
3791
0
  if (frame == NULL)
3792
0
    return -1;
3793
0
  frame->frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
3794
0
  frame->packet_type = FT_PACKET_REQUEST;
3795
0
  frame->action_length = host_to_le16(len);
3796
0
  os_memcpy(frame->ap_address, sm->wpa_auth->addr, ETH_ALEN);
3797
0
  os_memcpy(frame + 1, data, len);
3798
3799
0
  wpa_ft_rrb_send(sm->wpa_auth, target_ap, (u8 *) frame,
3800
0
      sizeof(*frame) + len);
3801
0
  os_free(frame);
3802
3803
0
  return 0;
3804
0
}
3805
3806
3807
static void wpa_ft_rrb_rx_request_cb(void *ctx, const u8 *dst,
3808
             u16 auth_transaction, u16 resp,
3809
             const u8 *ies, size_t ies_len)
3810
0
{
3811
0
  struct wpa_state_machine *sm = ctx;
3812
0
  wpa_printf(MSG_DEBUG, "FT: Over-the-DS RX request cb for " MACSTR,
3813
0
       MAC2STR(sm->addr));
3814
0
  wpa_ft_send_rrb_auth_resp(sm, sm->ft_pending_current_ap, sm->addr,
3815
0
          WLAN_STATUS_SUCCESS, ies, ies_len);
3816
0
}
3817
3818
3819
static int wpa_ft_rrb_rx_request(struct wpa_authenticator *wpa_auth,
3820
         const u8 *current_ap, const u8 *sta_addr,
3821
         const u8 *body, size_t len)
3822
0
{
3823
0
  struct wpa_state_machine *sm;
3824
0
  u16 status;
3825
0
  u8 *resp_ies;
3826
0
  size_t resp_ies_len;
3827
0
  int res;
3828
3829
0
  sm = wpa_ft_add_sta(wpa_auth, sta_addr);
3830
0
  if (sm == NULL) {
3831
0
    wpa_printf(MSG_DEBUG, "FT: Failed to add new STA based on "
3832
0
         "RRB Request");
3833
0
    return -1;
3834
0
  }
3835
3836
0
  wpa_hexdump(MSG_MSGDUMP, "FT: RRB Request Frame body", body, len);
3837
3838
0
  sm->ft_pending_cb = wpa_ft_rrb_rx_request_cb;
3839
0
  sm->ft_pending_cb_ctx = sm;
3840
0
  os_memcpy(sm->ft_pending_current_ap, current_ap, ETH_ALEN);
3841
0
  sm->ft_pending_pull_left_retries = sm->wpa_auth->conf.rkh_pull_retries;
3842
0
  res = wpa_ft_process_auth_req(sm, body, len, &resp_ies,
3843
0
              &resp_ies_len);
3844
0
  if (res < 0) {
3845
0
    wpa_printf(MSG_DEBUG, "FT: No immediate response available - wait for pull response");
3846
0
    return 0;
3847
0
  }
3848
0
  status = res;
3849
3850
0
  res = wpa_ft_send_rrb_auth_resp(sm, current_ap, sta_addr, status,
3851
0
          resp_ies, resp_ies_len);
3852
0
  os_free(resp_ies);
3853
0
  return res;
3854
0
}
3855
3856
3857
static int wpa_ft_send_rrb_auth_resp(struct wpa_state_machine *sm,
3858
             const u8 *current_ap, const u8 *sta_addr,
3859
             u16 status, const u8 *resp_ies,
3860
             size_t resp_ies_len)
3861
0
{
3862
0
  struct wpa_authenticator *wpa_auth = sm->wpa_auth;
3863
0
  size_t rlen;
3864
0
  struct ft_rrb_frame *frame;
3865
0
  u8 *pos;
3866
3867
0
  wpa_printf(MSG_DEBUG, "FT: RRB authentication response: STA=" MACSTR
3868
0
       " CurrentAP=" MACSTR " status=%u (%s)",
3869
0
       MAC2STR(sm->addr), MAC2STR(current_ap), status,
3870
0
       status2str(status));
3871
0
  wpa_hexdump(MSG_DEBUG, "FT: Response IEs", resp_ies, resp_ies_len);
3872
3873
  /* RRB - Forward action frame response to the Current AP */
3874
3875
  /*
3876
   * data: Category[1] Action[1] STA_Address[6] Target_AP_Address[6]
3877
   * Status_Code[2] FT Request action frame body[variable]
3878
   */
3879
0
  rlen = 2 + 2 * ETH_ALEN + 2 + resp_ies_len;
3880
3881
0
  frame = os_malloc(sizeof(*frame) + rlen);
3882
0
  if (frame == NULL)
3883
0
    return -1;
3884
0
  frame->frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
3885
0
  frame->packet_type = FT_PACKET_RESPONSE;
3886
0
  frame->action_length = host_to_le16(rlen);
3887
0
  os_memcpy(frame->ap_address, wpa_auth->addr, ETH_ALEN);
3888
0
  pos = (u8 *) (frame + 1);
3889
0
  *pos++ = WLAN_ACTION_FT;
3890
0
  *pos++ = 2; /* Action: Response */
3891
0
  os_memcpy(pos, sta_addr, ETH_ALEN);
3892
0
  pos += ETH_ALEN;
3893
0
  os_memcpy(pos, wpa_auth->addr, ETH_ALEN);
3894
0
  pos += ETH_ALEN;
3895
0
  WPA_PUT_LE16(pos, status);
3896
0
  pos += 2;
3897
0
  if (resp_ies)
3898
0
    os_memcpy(pos, resp_ies, resp_ies_len);
3899
3900
0
  wpa_ft_rrb_send(wpa_auth, current_ap, (u8 *) frame,
3901
0
      sizeof(*frame) + rlen);
3902
0
  os_free(frame);
3903
3904
0
  return 0;
3905
0
}
3906
3907
3908
static int wpa_ft_rrb_build_r0(const u8 *key, const size_t key_len,
3909
             const struct tlv_list *tlvs,
3910
             const struct wpa_ft_pmk_r0_sa *pmk_r0,
3911
             const u8 *r1kh_id, const u8 *s1kh_id,
3912
             const struct tlv_list *tlv_auth,
3913
             const u8 *src_addr, u8 type,
3914
             u8 **packet, size_t *packet_len)
3915
0
{
3916
0
  u8 pmk_r1[PMK_LEN_MAX];
3917
0
  size_t pmk_r1_len = pmk_r0->pmk_r0_len;
3918
0
  u8 pmk_r1_name[WPA_PMK_NAME_LEN];
3919
0
  u8 f_pairwise[sizeof(le16)];
3920
0
  u8 f_expires_in[sizeof(le16)];
3921
0
  u8 f_session_timeout[sizeof(le32)];
3922
0
  int expires_in;
3923
0
  int session_timeout;
3924
0
  struct os_reltime now;
3925
0
  int ret;
3926
0
  struct tlv_list sess_tlv[] = {
3927
0
    { .type = FT_RRB_PMK_R1, .len = pmk_r1_len,
3928
0
      .data = pmk_r1 },
3929
0
    { .type = FT_RRB_PMK_R1_NAME, .len = sizeof(pmk_r1_name),
3930
0
      .data = pmk_r1_name },
3931
0
    { .type = FT_RRB_PAIRWISE, .len = sizeof(f_pairwise),
3932
0
      .data = f_pairwise },
3933
0
    { .type = FT_RRB_EXPIRES_IN, .len = sizeof(f_expires_in),
3934
0
      .data = f_expires_in },
3935
0
    { .type = FT_RRB_IDENTITY, .len = pmk_r0->identity_len,
3936
0
      .data = pmk_r0->identity },
3937
0
    { .type = FT_RRB_RADIUS_CUI, .len = pmk_r0->radius_cui_len,
3938
0
      .data = pmk_r0->radius_cui },
3939
0
    { .type = FT_RRB_SESSION_TIMEOUT,
3940
0
      .len = sizeof(f_session_timeout),
3941
0
      .data = f_session_timeout },
3942
0
    { .type = FT_RRB_LAST_EMPTY, .len = 0, .data = NULL },
3943
0
  };
3944
3945
0
  wpa_printf(MSG_DEBUG, "FT: Derive PMK-R1 for peer AP");
3946
0
  if (wpa_derive_pmk_r1(pmk_r0->pmk_r0, pmk_r0->pmk_r0_len,
3947
0
            pmk_r0->pmk_r0_name, r1kh_id,
3948
0
            s1kh_id, pmk_r1, pmk_r1_name) < 0)
3949
0
    return -1;
3950
0
  WPA_PUT_LE16(f_pairwise, pmk_r0->pairwise);
3951
3952
0
  os_get_reltime(&now);
3953
0
  if (pmk_r0->expiration > now.sec)
3954
0
    expires_in = pmk_r0->expiration - now.sec;
3955
0
  else if (pmk_r0->expiration)
3956
0
    expires_in = 1;
3957
0
  else
3958
0
    expires_in = 0;
3959
0
  WPA_PUT_LE16(f_expires_in, expires_in);
3960
3961
0
  if (pmk_r0->session_timeout > now.sec)
3962
0
    session_timeout = pmk_r0->session_timeout - now.sec;
3963
0
  else if (pmk_r0->session_timeout)
3964
0
    session_timeout = 1;
3965
0
  else
3966
0
    session_timeout = 0;
3967
0
  WPA_PUT_LE32(f_session_timeout, session_timeout);
3968
3969
0
  ret = wpa_ft_rrb_build(key, key_len, tlvs, sess_tlv, tlv_auth,
3970
0
             pmk_r0->vlan, src_addr, type,
3971
0
             packet, packet_len);
3972
3973
0
  forced_memzero(pmk_r1, sizeof(pmk_r1));
3974
3975
0
  return ret;
3976
0
}
3977
3978
3979
static int wpa_ft_rrb_rx_pull(struct wpa_authenticator *wpa_auth,
3980
            const u8 *src_addr,
3981
            const u8 *enc, size_t enc_len,
3982
            const u8 *auth, size_t auth_len,
3983
            int no_defer)
3984
0
{
3985
0
  const char *msgtype = "pull request";
3986
0
  u8 *plain = NULL, *packet = NULL;
3987
0
  size_t plain_len = 0, packet_len = 0;
3988
0
  struct ft_remote_r1kh *r1kh, *r1kh_wildcard;
3989
0
  const u8 *key;
3990
0
  size_t key_len;
3991
0
  int seq_ret;
3992
0
  const u8 *f_nonce, *f_r0kh_id, *f_r1kh_id, *f_s1kh_id, *f_pmk_r0_name;
3993
0
  size_t f_nonce_len, f_r0kh_id_len, f_r1kh_id_len, f_s1kh_id_len;
3994
0
  size_t f_pmk_r0_name_len;
3995
0
  const struct wpa_ft_pmk_r0_sa *r0;
3996
0
  int ret;
3997
0
  struct tlv_list resp[2];
3998
0
  struct tlv_list resp_auth[5];
3999
0
  struct ft_rrb_seq f_seq;
4000
4001
0
  wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 pull");
4002
4003
0
  RRB_GET_AUTH(FT_RRB_R0KH_ID, r0kh_id, msgtype, -1);
4004
0
  wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID", f_r0kh_id, f_r0kh_id_len);
4005
4006
0
  if (wpa_ft_rrb_check_r0kh(wpa_auth, f_r0kh_id, f_r0kh_id_len)) {
4007
0
    wpa_printf(MSG_DEBUG, "FT: R0KH-ID mismatch");
4008
0
    goto out;
4009
0
  }
4010
4011
0
  RRB_GET_AUTH(FT_RRB_R1KH_ID, r1kh_id, msgtype, FT_R1KH_ID_LEN);
4012
0
  wpa_printf(MSG_DEBUG, "FT: R1KH-ID=" MACSTR, MAC2STR(f_r1kh_id));
4013
4014
0
  wpa_ft_rrb_lookup_r1kh(wpa_auth, f_r1kh_id, &r1kh, &r1kh_wildcard);
4015
0
  if (r1kh) {
4016
0
    key = r1kh->key;
4017
0
    key_len = sizeof(r1kh->key);
4018
0
  } else if (r1kh_wildcard) {
4019
0
    wpa_printf(MSG_DEBUG, "FT: Using wildcard R1KH-ID");
4020
0
    key = r1kh_wildcard->key;
4021
0
    key_len = sizeof(r1kh_wildcard->key);
4022
0
  } else {
4023
0
    goto out;
4024
0
  }
4025
4026
0
  RRB_GET_AUTH(FT_RRB_NONCE, nonce, "pull request", FT_RRB_NONCE_LEN);
4027
0
  wpa_hexdump(MSG_DEBUG, "FT: nonce", f_nonce, f_nonce_len);
4028
4029
0
  seq_ret = FT_RRB_SEQ_DROP;
4030
0
  if (r1kh)
4031
0
    seq_ret = wpa_ft_rrb_seq_chk(r1kh->seq, src_addr, enc, enc_len,
4032
0
               auth, auth_len, msgtype, no_defer);
4033
0
  if (!no_defer && r1kh_wildcard &&
4034
0
      (!r1kh || !ether_addr_equal(r1kh->addr, src_addr))) {
4035
    /* wildcard: r1kh-id unknown or changed addr -> do a seq req */
4036
0
    seq_ret = FT_RRB_SEQ_DEFER;
4037
0
  }
4038
4039
0
  if (seq_ret == FT_RRB_SEQ_DROP)
4040
0
    goto out;
4041
4042
0
  if (wpa_ft_rrb_decrypt(key, key_len, enc, enc_len, auth, auth_len,
4043
0
             src_addr, FT_PACKET_R0KH_R1KH_PULL,
4044
0
             &plain, &plain_len) < 0)
4045
0
    goto out;
4046
4047
0
  if (!r1kh)
4048
0
    r1kh = wpa_ft_rrb_add_r1kh(wpa_auth, r1kh_wildcard, src_addr,
4049
0
             f_r1kh_id,
4050
0
             wpa_auth->conf.rkh_pos_timeout);
4051
0
  if (!r1kh)
4052
0
    goto out;
4053
4054
0
  if (seq_ret == FT_RRB_SEQ_DEFER) {
4055
0
    wpa_ft_rrb_seq_req(wpa_auth, r1kh->seq, src_addr, f_r0kh_id,
4056
0
           f_r0kh_id_len, f_r1kh_id, key, key_len,
4057
0
           enc, enc_len, auth, auth_len,
4058
0
           &wpa_ft_rrb_rx_pull);
4059
0
    goto out;
4060
0
  }
4061
4062
0
  wpa_ft_rrb_seq_accept(wpa_auth, r1kh->seq, src_addr, auth, auth_len,
4063
0
            msgtype);
4064
0
  wpa_ft_rrb_r1kh_replenish(wpa_auth, r1kh,
4065
0
          wpa_auth->conf.rkh_pos_timeout);
4066
4067
0
  RRB_GET(FT_RRB_PMK_R0_NAME, pmk_r0_name, msgtype, WPA_PMK_NAME_LEN);
4068
0
  wpa_hexdump(MSG_DEBUG, "FT: PMKR0Name", f_pmk_r0_name,
4069
0
        f_pmk_r0_name_len);
4070
4071
0
  RRB_GET(FT_RRB_S1KH_ID, s1kh_id, msgtype, ETH_ALEN);
4072
0
  wpa_printf(MSG_DEBUG, "FT: S1KH-ID=" MACSTR, MAC2STR(f_s1kh_id));
4073
4074
0
  if (wpa_ft_new_seq(r1kh->seq, &f_seq) < 0) {
4075
0
    wpa_printf(MSG_DEBUG, "FT: Failed to get seq num");
4076
0
    goto out;
4077
0
  }
4078
4079
0
  wpa_printf(MSG_DEBUG, "FT: Send PMK-R1 pull response from " MACSTR
4080
0
       " to " MACSTR,
4081
0
       MAC2STR(wpa_auth->addr), MAC2STR(src_addr));
4082
4083
0
  resp[0].type = FT_RRB_S1KH_ID;
4084
0
  resp[0].len = f_s1kh_id_len;
4085
0
  resp[0].data = f_s1kh_id;
4086
0
  resp[1].type = FT_RRB_LAST_EMPTY;
4087
0
  resp[1].len = 0;
4088
0
  resp[1].data = NULL;
4089
4090
0
  resp_auth[0].type = FT_RRB_NONCE;
4091
0
  resp_auth[0].len = f_nonce_len;
4092
0
  resp_auth[0].data = f_nonce;
4093
0
  resp_auth[1].type = FT_RRB_SEQ;
4094
0
  resp_auth[1].len = sizeof(f_seq);
4095
0
  resp_auth[1].data = (u8 *) &f_seq;
4096
0
  resp_auth[2].type = FT_RRB_R0KH_ID;
4097
0
  resp_auth[2].len = f_r0kh_id_len;
4098
0
  resp_auth[2].data = f_r0kh_id;
4099
0
  resp_auth[3].type = FT_RRB_R1KH_ID;
4100
0
  resp_auth[3].len = f_r1kh_id_len;
4101
0
  resp_auth[3].data = f_r1kh_id;
4102
0
  resp_auth[4].type = FT_RRB_LAST_EMPTY;
4103
0
  resp_auth[4].len = 0;
4104
0
  resp_auth[4].data = NULL;
4105
4106
0
  if (wpa_ft_fetch_pmk_r0(wpa_auth, f_s1kh_id, f_pmk_r0_name, &r0) < 0) {
4107
0
    wpa_printf(MSG_DEBUG, "FT: No matching PMK-R0-Name found");
4108
0
    ret = wpa_ft_rrb_build(key, key_len, resp, NULL, resp_auth,
4109
0
               NULL, wpa_auth->addr,
4110
0
               FT_PACKET_R0KH_R1KH_RESP,
4111
0
               &packet, &packet_len);
4112
0
  } else {
4113
0
    ret = wpa_ft_rrb_build_r0(key, key_len, resp, r0, f_r1kh_id,
4114
0
            f_s1kh_id, resp_auth, wpa_auth->addr,
4115
0
            FT_PACKET_R0KH_R1KH_RESP,
4116
0
            &packet, &packet_len);
4117
0
  }
4118
4119
0
  if (!ret)
4120
0
    wpa_ft_rrb_oui_send(wpa_auth, src_addr,
4121
0
            FT_PACKET_R0KH_R1KH_RESP, packet,
4122
0
            packet_len);
4123
4124
0
out:
4125
0
  os_free(plain);
4126
0
  os_free(packet);
4127
4128
0
  return 0;
4129
0
}
4130
4131
4132
/* @returns  0 on success
4133
 *          -1 on error
4134
 *          -2 if FR_RRB_PAIRWISE is missing
4135
 */
4136
static int wpa_ft_rrb_rx_r1(struct wpa_authenticator *wpa_auth,
4137
          const u8 *src_addr, u8 type,
4138
          const u8 *enc, size_t enc_len,
4139
          const u8 *auth, size_t auth_len,
4140
          const char *msgtype, u8 *s1kh_id_out,
4141
          int (*cb)(struct wpa_authenticator *wpa_auth,
4142
              const u8 *src_addr,
4143
              const u8 *enc, size_t enc_len,
4144
              const u8 *auth, size_t auth_len,
4145
              int no_defer))
4146
0
{
4147
0
  u8 *plain = NULL;
4148
0
  size_t plain_len = 0;
4149
0
  struct ft_remote_r0kh *r0kh, *r0kh_wildcard;
4150
0
  const u8 *key;
4151
0
  size_t key_len;
4152
0
  int seq_ret;
4153
0
  const u8 *f_r1kh_id, *f_s1kh_id, *f_r0kh_id;
4154
0
  const u8 *f_pmk_r1_name, *f_pairwise, *f_pmk_r1;
4155
0
  const u8 *f_expires_in;
4156
0
  size_t f_r1kh_id_len, f_s1kh_id_len, f_r0kh_id_len;
4157
0
  const u8 *f_identity, *f_radius_cui;
4158
0
  const u8 *f_session_timeout;
4159
0
  size_t f_pmk_r1_name_len, f_pairwise_len, f_pmk_r1_len;
4160
0
  size_t f_expires_in_len;
4161
0
  size_t f_identity_len, f_radius_cui_len;
4162
0
  size_t f_session_timeout_len;
4163
0
  int pairwise;
4164
0
  int ret = -1;
4165
0
  int expires_in;
4166
0
  int session_timeout;
4167
0
  struct vlan_description vlan;
4168
0
  size_t pmk_r1_len;
4169
4170
0
  RRB_GET_AUTH(FT_RRB_R0KH_ID, r0kh_id, msgtype, -1);
4171
0
  wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID", f_r0kh_id, f_r0kh_id_len);
4172
4173
0
  RRB_GET_AUTH(FT_RRB_R1KH_ID, r1kh_id, msgtype, FT_R1KH_ID_LEN);
4174
0
  wpa_printf(MSG_DEBUG, "FT: R1KH-ID=" MACSTR, MAC2STR(f_r1kh_id));
4175
4176
0
  if (wpa_ft_rrb_check_r1kh(wpa_auth, f_r1kh_id)) {
4177
0
    wpa_printf(MSG_DEBUG, "FT: R1KH-ID mismatch");
4178
0
    goto out;
4179
0
  }
4180
4181
0
  wpa_ft_rrb_lookup_r0kh(wpa_auth, f_r0kh_id, f_r0kh_id_len, &r0kh,
4182
0
             &r0kh_wildcard);
4183
0
  if (r0kh) {
4184
0
    key = r0kh->key;
4185
0
    key_len = sizeof(r0kh->key);
4186
0
  } else if (r0kh_wildcard) {
4187
0
    wpa_printf(MSG_DEBUG, "FT: Using wildcard R0KH-ID");
4188
0
    key = r0kh_wildcard->key;
4189
0
    key_len = sizeof(r0kh_wildcard->key);
4190
0
  } else {
4191
0
    goto out;
4192
0
  }
4193
4194
0
  seq_ret = FT_RRB_SEQ_DROP;
4195
0
  if (r0kh) {
4196
0
    seq_ret = wpa_ft_rrb_seq_chk(r0kh->seq, src_addr, enc, enc_len,
4197
0
               auth, auth_len, msgtype,
4198
0
               cb ? 0 : 1);
4199
0
  }
4200
0
  if (cb && r0kh_wildcard &&
4201
0
      (!r0kh || !ether_addr_equal(r0kh->addr, src_addr))) {
4202
    /* wildcard: r0kh-id unknown or changed addr -> do a seq req */
4203
0
    seq_ret = FT_RRB_SEQ_DEFER;
4204
0
  }
4205
4206
0
  if (seq_ret == FT_RRB_SEQ_DROP)
4207
0
    goto out;
4208
4209
0
  if (wpa_ft_rrb_decrypt(key, key_len, enc, enc_len, auth, auth_len,
4210
0
             src_addr, type, &plain, &plain_len) < 0)
4211
0
    goto out;
4212
4213
0
  if (!r0kh)
4214
0
    r0kh = wpa_ft_rrb_add_r0kh(wpa_auth, r0kh_wildcard, src_addr,
4215
0
             f_r0kh_id, f_r0kh_id_len,
4216
0
             wpa_auth->conf.rkh_pos_timeout);
4217
0
  if (!r0kh)
4218
0
    goto out;
4219
4220
0
  if (seq_ret == FT_RRB_SEQ_DEFER) {
4221
0
    wpa_ft_rrb_seq_req(wpa_auth, r0kh->seq, src_addr, f_r0kh_id,
4222
0
           f_r0kh_id_len, f_r1kh_id, key, key_len,
4223
0
           enc, enc_len, auth, auth_len, cb);
4224
0
    goto out;
4225
0
  }
4226
4227
0
  wpa_ft_rrb_seq_accept(wpa_auth, r0kh->seq, src_addr, auth, auth_len,
4228
0
            msgtype);
4229
0
  wpa_ft_rrb_r0kh_replenish(wpa_auth, r0kh,
4230
0
          wpa_auth->conf.rkh_pos_timeout);
4231
4232
0
  RRB_GET(FT_RRB_S1KH_ID, s1kh_id, msgtype, ETH_ALEN);
4233
0
  wpa_printf(MSG_DEBUG, "FT: S1KH-ID=" MACSTR, MAC2STR(f_s1kh_id));
4234
4235
0
  if (s1kh_id_out)
4236
0
    os_memcpy(s1kh_id_out, f_s1kh_id, ETH_ALEN);
4237
4238
0
  ret = -2;
4239
0
  RRB_GET(FT_RRB_PAIRWISE, pairwise, msgtype, sizeof(le16));
4240
0
  wpa_hexdump(MSG_DEBUG, "FT: pairwise", f_pairwise, f_pairwise_len);
4241
4242
0
  ret = -1;
4243
0
  RRB_GET(FT_RRB_PMK_R1_NAME, pmk_r1_name, msgtype, WPA_PMK_NAME_LEN);
4244
0
  wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name",
4245
0
        f_pmk_r1_name, WPA_PMK_NAME_LEN);
4246
4247
0
  pmk_r1_len = PMK_LEN;
4248
0
  if (wpa_ft_rrb_get_tlv(plain, plain_len, FT_RRB_PMK_R1, &f_pmk_r1_len,
4249
0
             &f_pmk_r1) == 0 &&
4250
0
      (f_pmk_r1_len == PMK_LEN || f_pmk_r1_len == SHA384_MAC_LEN ||
4251
0
       f_pmk_r1_len == SHA512_MAC_LEN))
4252
0
    pmk_r1_len = f_pmk_r1_len;
4253
0
  RRB_GET(FT_RRB_PMK_R1, pmk_r1, msgtype, pmk_r1_len);
4254
0
  wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", f_pmk_r1, pmk_r1_len);
4255
4256
0
  pairwise = WPA_GET_LE16(f_pairwise);
4257
4258
0
  RRB_GET_OPTIONAL(FT_RRB_EXPIRES_IN, expires_in, msgtype,
4259
0
       sizeof(le16));
4260
0
  if (f_expires_in)
4261
0
    expires_in = WPA_GET_LE16(f_expires_in);
4262
0
  else
4263
0
    expires_in = 0;
4264
4265
0
  wpa_printf(MSG_DEBUG, "FT: PMK-R1 %s - expires_in=%d", msgtype,
4266
0
       expires_in);
4267
4268
0
  if (wpa_ft_rrb_get_tlv_vlan(plain, plain_len, &vlan) < 0) {
4269
0
    wpa_printf(MSG_DEBUG, "FT: Cannot parse vlan");
4270
0
    wpa_ft_rrb_dump(plain, plain_len);
4271
0
    goto out;
4272
0
  }
4273
4274
0
  wpa_printf(MSG_DEBUG, "FT: vlan %d%s",
4275
0
       le_to_host16(vlan.untagged), vlan.tagged[0] ? "+" : "");
4276
4277
0
  RRB_GET_OPTIONAL(FT_RRB_IDENTITY, identity, msgtype, -1);
4278
0
  if (f_identity)
4279
0
    wpa_hexdump_ascii(MSG_DEBUG, "FT: Identity", f_identity,
4280
0
          f_identity_len);
4281
4282
0
  RRB_GET_OPTIONAL(FT_RRB_RADIUS_CUI, radius_cui, msgtype, -1);
4283
0
  if (f_radius_cui)
4284
0
    wpa_hexdump_ascii(MSG_DEBUG, "FT: CUI", f_radius_cui,
4285
0
          f_radius_cui_len);
4286
4287
0
  RRB_GET_OPTIONAL(FT_RRB_SESSION_TIMEOUT, session_timeout, msgtype,
4288
0
       sizeof(le32));
4289
0
  if (f_session_timeout)
4290
0
    session_timeout = WPA_GET_LE32(f_session_timeout);
4291
0
  else
4292
0
    session_timeout = 0;
4293
0
  wpa_printf(MSG_DEBUG, "FT: session_timeout %d", session_timeout);
4294
4295
0
  if (wpa_ft_store_pmk_r1(wpa_auth, f_s1kh_id, f_pmk_r1, pmk_r1_len,
4296
0
        f_pmk_r1_name,
4297
0
        pairwise, &vlan, expires_in, session_timeout,
4298
0
        f_identity, f_identity_len, f_radius_cui,
4299
0
        f_radius_cui_len) < 0)
4300
0
    goto out;
4301
4302
0
  ret = 0;
4303
0
out:
4304
0
  bin_clear_free(plain, plain_len);
4305
4306
0
  return ret;
4307
4308
0
}
4309
4310
4311
static void ft_finish_pull(struct wpa_state_machine *sm)
4312
0
{
4313
0
  int res;
4314
0
  u8 *resp_ies;
4315
0
  size_t resp_ies_len;
4316
0
  u16 status;
4317
4318
0
  if (!sm->ft_pending_cb || !sm->ft_pending_req_ies)
4319
0
    return;
4320
4321
0
  res = wpa_ft_process_auth_req(sm, wpabuf_head(sm->ft_pending_req_ies),
4322
0
              wpabuf_len(sm->ft_pending_req_ies),
4323
0
              &resp_ies, &resp_ies_len);
4324
0
  if (res < 0) {
4325
    /* this loop is broken by ft_pending_pull_left_retries */
4326
0
    wpa_printf(MSG_DEBUG,
4327
0
         "FT: Callback postponed until response is available");
4328
0
    return;
4329
0
  }
4330
0
  wpabuf_free(sm->ft_pending_req_ies);
4331
0
  sm->ft_pending_req_ies = NULL;
4332
0
  status = res;
4333
0
  wpa_printf(MSG_DEBUG, "FT: Postponed auth callback result for " MACSTR
4334
0
       " - status %u", MAC2STR(sm->addr), status);
4335
4336
0
  sm->ft_pending_cb(sm->ft_pending_cb_ctx, sm->addr,
4337
0
        sm->ft_pending_auth_transaction + 1, status,
4338
0
        resp_ies, resp_ies_len);
4339
0
  os_free(resp_ies);
4340
0
}
4341
4342
4343
struct ft_get_sta_ctx {
4344
  const u8 *nonce;
4345
  const u8 *s1kh_id;
4346
  struct wpa_state_machine *sm;
4347
};
4348
4349
4350
static int ft_get_sta_cb(struct wpa_state_machine *sm, void *ctx)
4351
0
{
4352
0
  struct ft_get_sta_ctx *info = ctx;
4353
4354
0
  if ((info->s1kh_id &&
4355
0
       !ether_addr_equal(info->s1kh_id, sm->addr)) ||
4356
0
      os_memcmp(info->nonce, sm->ft_pending_pull_nonce,
4357
0
          FT_RRB_NONCE_LEN) != 0 ||
4358
0
      sm->ft_pending_cb == NULL || sm->ft_pending_req_ies == NULL)
4359
0
    return 0;
4360
4361
0
  info->sm = sm;
4362
4363
0
  return 1;
4364
0
}
4365
4366
4367
static int wpa_ft_rrb_rx_resp(struct wpa_authenticator *wpa_auth,
4368
            const u8 *src_addr,
4369
            const u8 *enc, size_t enc_len,
4370
            const u8 *auth, size_t auth_len,
4371
            int no_defer)
4372
0
{
4373
0
  const char *msgtype = "pull response";
4374
0
  int nak, ret = -1;
4375
0
  struct ft_get_sta_ctx ctx;
4376
0
  u8 s1kh_id[ETH_ALEN];
4377
0
  const u8 *f_nonce;
4378
0
  size_t f_nonce_len;
4379
4380
0
  wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 pull response");
4381
4382
0
  RRB_GET_AUTH(FT_RRB_NONCE, nonce, msgtype, FT_RRB_NONCE_LEN);
4383
0
  wpa_hexdump(MSG_DEBUG, "FT: nonce", f_nonce, f_nonce_len);
4384
4385
0
  os_memset(&ctx, 0, sizeof(ctx));
4386
0
  ctx.nonce = f_nonce;
4387
0
  if (!wpa_auth_for_each_sta(wpa_auth, ft_get_sta_cb, &ctx)) {
4388
    /* nonce not found */
4389
0
    wpa_printf(MSG_DEBUG, "FT: Invalid nonce");
4390
0
    return -1;
4391
0
  }
4392
4393
0
  ret = wpa_ft_rrb_rx_r1(wpa_auth, src_addr, FT_PACKET_R0KH_R1KH_RESP,
4394
0
             enc, enc_len, auth, auth_len, msgtype, s1kh_id,
4395
0
             no_defer ? NULL : &wpa_ft_rrb_rx_resp);
4396
0
  if (ret == -2) {
4397
0
    ret = 0;
4398
0
    nak = 1;
4399
0
  } else {
4400
0
    nak = 0;
4401
0
  }
4402
0
  if (ret < 0)
4403
0
    return -1;
4404
4405
0
  ctx.s1kh_id = s1kh_id;
4406
0
  if (wpa_auth_for_each_sta(wpa_auth, ft_get_sta_cb, &ctx)) {
4407
0
    wpa_printf(MSG_DEBUG,
4408
0
         "FT: Response to a pending pull request for " MACSTR,
4409
0
         MAC2STR(ctx.sm->addr));
4410
0
    eloop_cancel_timeout(wpa_ft_expire_pull, ctx.sm, NULL);
4411
0
    if (nak)
4412
0
      ctx.sm->ft_pending_pull_left_retries = 0;
4413
0
    ft_finish_pull(ctx.sm);
4414
0
  }
4415
4416
0
out:
4417
0
  return ret;
4418
0
}
4419
4420
4421
static int wpa_ft_rrb_rx_push(struct wpa_authenticator *wpa_auth,
4422
            const u8 *src_addr,
4423
            const u8 *enc, size_t enc_len,
4424
            const u8 *auth, size_t auth_len, int no_defer)
4425
0
{
4426
0
  const char *msgtype = "push";
4427
4428
0
  wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 push");
4429
4430
0
  if (wpa_ft_rrb_rx_r1(wpa_auth, src_addr, FT_PACKET_R0KH_R1KH_PUSH,
4431
0
           enc, enc_len, auth, auth_len, msgtype, NULL,
4432
0
           no_defer ? NULL : wpa_ft_rrb_rx_push) < 0)
4433
0
    return -1;
4434
4435
0
  return 0;
4436
0
}
4437
4438
4439
static int wpa_ft_rrb_rx_seq(struct wpa_authenticator *wpa_auth,
4440
           const u8 *src_addr, int type,
4441
           const u8 *enc, size_t enc_len,
4442
           const u8 *auth, size_t auth_len,
4443
           struct ft_remote_seq **rkh_seq,
4444
           u8 **key, size_t *key_len,
4445
           struct ft_remote_r0kh **r0kh_out,
4446
           struct ft_remote_r1kh **r1kh_out,
4447
           struct ft_remote_r0kh **r0kh_wildcard_out,
4448
           struct ft_remote_r1kh **r1kh_wildcard_out)
4449
0
{
4450
0
  struct ft_remote_r0kh *r0kh = NULL;
4451
0
  struct ft_remote_r1kh *r1kh = NULL;
4452
0
  const u8 *f_r0kh_id, *f_r1kh_id;
4453
0
  size_t f_r0kh_id_len, f_r1kh_id_len;
4454
0
  int to_r0kh, to_r1kh;
4455
0
  u8 *plain = NULL;
4456
0
  size_t plain_len = 0;
4457
0
  struct ft_remote_r0kh *r0kh_wildcard;
4458
0
  struct ft_remote_r1kh *r1kh_wildcard;
4459
4460
0
  RRB_GET_AUTH(FT_RRB_R0KH_ID, r0kh_id, "seq", -1);
4461
0
  RRB_GET_AUTH(FT_RRB_R1KH_ID, r1kh_id, "seq", FT_R1KH_ID_LEN);
4462
4463
0
  to_r0kh = !wpa_ft_rrb_check_r0kh(wpa_auth, f_r0kh_id, f_r0kh_id_len);
4464
0
  to_r1kh = !wpa_ft_rrb_check_r1kh(wpa_auth, f_r1kh_id);
4465
4466
0
  if (to_r0kh && to_r1kh) {
4467
0
    wpa_printf(MSG_DEBUG, "FT: seq - local R0KH-ID and R1KH-ID");
4468
0
    goto out;
4469
0
  }
4470
4471
0
  if (!to_r0kh && !to_r1kh) {
4472
0
    wpa_printf(MSG_DEBUG, "FT: seq - remote R0KH-ID and R1KH-ID");
4473
0
    goto out;
4474
0
  }
4475
4476
0
  if (!to_r0kh) {
4477
0
    wpa_ft_rrb_lookup_r0kh(wpa_auth, f_r0kh_id, f_r0kh_id_len,
4478
0
               &r0kh, &r0kh_wildcard);
4479
0
    if (!r0kh_wildcard &&
4480
0
        (!r0kh || !ether_addr_equal(r0kh->addr, src_addr))) {
4481
0
      wpa_hexdump(MSG_DEBUG, "FT: Did not find R0KH-ID",
4482
0
            f_r0kh_id, f_r0kh_id_len);
4483
0
      goto out;
4484
0
    }
4485
0
    if (r0kh) {
4486
0
      *key = r0kh->key;
4487
0
      *key_len = sizeof(r0kh->key);
4488
0
    } else {
4489
0
      *key = r0kh_wildcard->key;
4490
0
      *key_len = sizeof(r0kh_wildcard->key);
4491
0
    }
4492
0
  }
4493
4494
0
  if (!to_r1kh) {
4495
0
    wpa_ft_rrb_lookup_r1kh(wpa_auth, f_r1kh_id, &r1kh,
4496
0
               &r1kh_wildcard);
4497
0
    if (!r1kh_wildcard &&
4498
0
        (!r1kh || !ether_addr_equal(r1kh->addr, src_addr))) {
4499
0
      wpa_hexdump(MSG_DEBUG, "FT: Did not find R1KH-ID",
4500
0
            f_r1kh_id, FT_R1KH_ID_LEN);
4501
0
      goto out;
4502
0
    }
4503
0
    if (r1kh) {
4504
0
      *key = r1kh->key;
4505
0
      *key_len = sizeof(r1kh->key);
4506
0
    } else {
4507
0
      *key = r1kh_wildcard->key;
4508
0
      *key_len = sizeof(r1kh_wildcard->key);
4509
0
    }
4510
0
  }
4511
4512
0
  if (wpa_ft_rrb_decrypt(*key, *key_len, enc, enc_len, auth, auth_len,
4513
0
             src_addr, type, &plain, &plain_len) < 0)
4514
0
    goto out;
4515
4516
0
  os_free(plain);
4517
4518
0
  if (!to_r0kh) {
4519
0
    if (!r0kh)
4520
0
      r0kh = wpa_ft_rrb_add_r0kh(wpa_auth, r0kh_wildcard,
4521
0
               src_addr, f_r0kh_id,
4522
0
               f_r0kh_id_len,
4523
0
               ftRRBseqTimeout);
4524
0
    if (!r0kh)
4525
0
      goto out;
4526
4527
0
    wpa_ft_rrb_r0kh_replenish(wpa_auth, r0kh, ftRRBseqTimeout);
4528
0
    *rkh_seq = r0kh->seq;
4529
0
    if (r0kh_out)
4530
0
      *r0kh_out = r0kh;
4531
0
    if (r0kh_wildcard_out)
4532
0
      *r0kh_wildcard_out = r0kh_wildcard;
4533
0
  }
4534
4535
0
  if (!to_r1kh) {
4536
0
    if (!r1kh)
4537
0
      r1kh = wpa_ft_rrb_add_r1kh(wpa_auth, r1kh_wildcard,
4538
0
               src_addr, f_r1kh_id,
4539
0
               ftRRBseqTimeout);
4540
0
    if (!r1kh)
4541
0
      goto out;
4542
4543
0
    wpa_ft_rrb_r1kh_replenish(wpa_auth, r1kh, ftRRBseqTimeout);
4544
0
    *rkh_seq = r1kh->seq;
4545
0
    if (r1kh_out)
4546
0
      *r1kh_out = r1kh;
4547
0
    if (r1kh_wildcard_out)
4548
0
      *r1kh_wildcard_out = r1kh_wildcard;
4549
0
  }
4550
4551
0
  return 0;
4552
0
out:
4553
0
  return -1;
4554
0
}
4555
4556
4557
static int wpa_ft_rrb_rx_seq_req(struct wpa_authenticator *wpa_auth,
4558
         const u8 *src_addr,
4559
         const u8 *enc, size_t enc_len,
4560
         const u8 *auth, size_t auth_len,
4561
         int no_defer)
4562
0
{
4563
0
  int ret = -1;
4564
0
  struct ft_rrb_seq f_seq;
4565
0
  const u8 *f_nonce, *f_r0kh_id, *f_r1kh_id;
4566
0
  size_t f_nonce_len, f_r0kh_id_len, f_r1kh_id_len;
4567
0
  struct ft_remote_seq *rkh_seq = NULL;
4568
0
  u8 *packet = NULL, *key = NULL;
4569
0
  size_t packet_len = 0, key_len = 0;
4570
0
  struct tlv_list seq_resp_auth[5];
4571
4572
0
  wpa_printf(MSG_DEBUG, "FT: Received sequence number request");
4573
4574
0
  if (wpa_ft_rrb_rx_seq(wpa_auth, src_addr, FT_PACKET_R0KH_R1KH_SEQ_REQ,
4575
0
            enc, enc_len, auth, auth_len, &rkh_seq, &key,
4576
0
            &key_len, NULL, NULL, NULL, NULL) < 0)
4577
0
    goto out;
4578
4579
0
  RRB_GET_AUTH(FT_RRB_NONCE, nonce, "seq request", FT_RRB_NONCE_LEN);
4580
0
  wpa_hexdump(MSG_DEBUG, "FT: seq request - nonce", f_nonce, f_nonce_len);
4581
4582
0
  RRB_GET_AUTH(FT_RRB_R0KH_ID, r0kh_id, "seq", -1);
4583
0
  RRB_GET_AUTH(FT_RRB_R1KH_ID, r1kh_id, "seq", FT_R1KH_ID_LEN);
4584
4585
0
  if (wpa_ft_new_seq(rkh_seq, &f_seq) < 0) {
4586
0
    wpa_printf(MSG_DEBUG, "FT: Failed to get seq num");
4587
0
    goto out;
4588
0
  }
4589
4590
0
  wpa_printf(MSG_DEBUG, "FT: Send sequence number response from " MACSTR
4591
0
       " to " MACSTR,
4592
0
       MAC2STR(wpa_auth->addr), MAC2STR(src_addr));
4593
4594
0
  seq_resp_auth[0].type = FT_RRB_NONCE;
4595
0
  seq_resp_auth[0].len = f_nonce_len;
4596
0
  seq_resp_auth[0].data = f_nonce;
4597
0
  seq_resp_auth[1].type = FT_RRB_SEQ;
4598
0
  seq_resp_auth[1].len = sizeof(f_seq);
4599
0
  seq_resp_auth[1].data = (u8 *) &f_seq;
4600
0
  seq_resp_auth[2].type = FT_RRB_R0KH_ID;
4601
0
  seq_resp_auth[2].len = f_r0kh_id_len;
4602
0
  seq_resp_auth[2].data = f_r0kh_id;
4603
0
  seq_resp_auth[3].type = FT_RRB_R1KH_ID;
4604
0
  seq_resp_auth[3].len = FT_R1KH_ID_LEN;
4605
0
  seq_resp_auth[3].data = f_r1kh_id;
4606
0
  seq_resp_auth[4].type = FT_RRB_LAST_EMPTY;
4607
0
  seq_resp_auth[4].len = 0;
4608
0
  seq_resp_auth[4].data = NULL;
4609
4610
0
  if (wpa_ft_rrb_build(key, key_len, NULL, NULL, seq_resp_auth, NULL,
4611
0
           wpa_auth->addr, FT_PACKET_R0KH_R1KH_SEQ_RESP,
4612
0
           &packet, &packet_len) < 0)
4613
0
    goto out;
4614
4615
0
  wpa_ft_rrb_oui_send(wpa_auth, src_addr,
4616
0
          FT_PACKET_R0KH_R1KH_SEQ_RESP, packet,
4617
0
          packet_len);
4618
4619
0
out:
4620
0
  os_free(packet);
4621
4622
0
  return ret;
4623
0
}
4624
4625
4626
static int wpa_ft_rrb_rx_seq_resp(struct wpa_authenticator *wpa_auth,
4627
          const u8 *src_addr,
4628
          const u8 *enc, size_t enc_len,
4629
          const u8 *auth, size_t auth_len,
4630
          int no_defer)
4631
0
{
4632
0
  u8 *key = NULL;
4633
0
  size_t key_len = 0;
4634
0
  struct ft_remote_r0kh *r0kh = NULL, *r0kh_wildcard = NULL;
4635
0
  struct ft_remote_r1kh *r1kh = NULL, *r1kh_wildcard = NULL;
4636
0
  const u8 *f_nonce, *f_seq;
4637
0
  size_t f_nonce_len, f_seq_len;
4638
0
  struct ft_remote_seq *rkh_seq = NULL;
4639
0
  struct ft_remote_item *item;
4640
0
  struct os_reltime now, now_remote;
4641
0
  int seq_ret, found;
4642
0
  const struct ft_rrb_seq *msg_both;
4643
0
  u32 msg_dom, msg_seq;
4644
4645
0
  wpa_printf(MSG_DEBUG, "FT: Received sequence number response");
4646
4647
0
  if (wpa_ft_rrb_rx_seq(wpa_auth, src_addr, FT_PACKET_R0KH_R1KH_SEQ_RESP,
4648
0
            enc, enc_len, auth, auth_len, &rkh_seq, &key,
4649
0
            &key_len, &r0kh, &r1kh, &r0kh_wildcard,
4650
0
            &r1kh_wildcard) < 0)
4651
0
    goto out;
4652
4653
0
  RRB_GET_AUTH(FT_RRB_NONCE, nonce, "seq response", FT_RRB_NONCE_LEN);
4654
0
  wpa_hexdump(MSG_DEBUG, "FT: seq response - nonce", f_nonce,
4655
0
        f_nonce_len);
4656
4657
0
  found = 0;
4658
0
  dl_list_for_each(item, &rkh_seq->rx.queue, struct ft_remote_item,
4659
0
       list) {
4660
0
    if (os_memcmp_const(f_nonce, item->nonce,
4661
0
            FT_RRB_NONCE_LEN) != 0 ||
4662
0
        os_get_reltime(&now) < 0 ||
4663
0
        os_reltime_expired(&now, &item->nonce_ts, ftRRBseqTimeout))
4664
0
      continue;
4665
4666
0
    found = 1;
4667
0
    break;
4668
0
  }
4669
0
  if (!found) {
4670
0
    wpa_printf(MSG_DEBUG, "FT: seq response - bad nonce");
4671
0
    goto out;
4672
0
  }
4673
4674
0
  if (r0kh) {
4675
0
    wpa_ft_rrb_r0kh_replenish(wpa_auth, r0kh,
4676
0
            wpa_auth->conf.rkh_pos_timeout);
4677
0
    if (r0kh_wildcard)
4678
0
      os_memcpy(r0kh->addr, src_addr, ETH_ALEN);
4679
0
  }
4680
4681
0
  if (r1kh) {
4682
0
    wpa_ft_rrb_r1kh_replenish(wpa_auth, r1kh,
4683
0
            wpa_auth->conf.rkh_pos_timeout);
4684
0
    if (r1kh_wildcard)
4685
0
      os_memcpy(r1kh->addr, src_addr, ETH_ALEN);
4686
0
  }
4687
4688
0
  seq_ret = wpa_ft_rrb_seq_chk(rkh_seq, src_addr, enc, enc_len, auth,
4689
0
             auth_len, "seq response", 1);
4690
0
  if (seq_ret == FT_RRB_SEQ_OK) {
4691
0
    wpa_printf(MSG_DEBUG, "FT: seq response - valid seq number");
4692
0
    wpa_ft_rrb_seq_accept(wpa_auth, rkh_seq, src_addr, auth,
4693
0
              auth_len, "seq response");
4694
0
  } else {
4695
0
    wpa_printf(MSG_DEBUG, "FT: seq response - reset seq number");
4696
4697
0
    RRB_GET_AUTH(FT_RRB_SEQ, seq, "seq response",
4698
0
           sizeof(*msg_both));
4699
0
    msg_both = (const struct ft_rrb_seq *) f_seq;
4700
4701
0
    msg_dom = le_to_host32(msg_both->dom);
4702
0
    msg_seq = le_to_host32(msg_both->seq);
4703
0
    now_remote.sec = le_to_host32(msg_both->ts);
4704
0
    now_remote.usec = 0;
4705
4706
0
    rkh_seq->rx.num_last = 2;
4707
0
    rkh_seq->rx.dom = msg_dom;
4708
0
    rkh_seq->rx.offsetidx = 0;
4709
    /* Accept some older, possibly cached packets as well */
4710
0
    rkh_seq->rx.last[0] = msg_seq - FT_REMOTE_SEQ_BACKLOG -
4711
0
      dl_list_len(&rkh_seq->rx.queue);
4712
0
    rkh_seq->rx.last[1] = msg_seq;
4713
4714
    /* local time - offset = remote time
4715
     * <=> local time - remote time = offset */
4716
0
    os_reltime_sub(&now, &now_remote, &rkh_seq->rx.time_offset);
4717
0
  }
4718
4719
0
  wpa_ft_rrb_seq_flush(wpa_auth, rkh_seq, 1);
4720
4721
0
  return 0;
4722
0
out:
4723
0
  return -1;
4724
0
}
4725
4726
4727
int wpa_ft_rrb_rx(struct wpa_authenticator *wpa_auth, const u8 *src_addr,
4728
      const u8 *data, size_t data_len)
4729
0
{
4730
0
  struct ft_rrb_frame *frame;
4731
0
  u16 alen;
4732
0
  const u8 *pos, *end, *start;
4733
0
  u8 action;
4734
0
  const u8 *sta_addr, *target_ap_addr;
4735
4736
0
  wpa_printf(MSG_DEBUG, "FT: RRB received frame from remote AP " MACSTR,
4737
0
       MAC2STR(src_addr));
4738
4739
0
  if (data_len < sizeof(*frame)) {
4740
0
    wpa_printf(MSG_DEBUG, "FT: Too short RRB frame (data_len=%lu)",
4741
0
         (unsigned long) data_len);
4742
0
    return -1;
4743
0
  }
4744
4745
0
  pos = data;
4746
0
  frame = (struct ft_rrb_frame *) pos;
4747
0
  pos += sizeof(*frame);
4748
4749
0
  alen = le_to_host16(frame->action_length);
4750
0
  wpa_printf(MSG_DEBUG, "FT: RRB frame - frame_type=%d packet_type=%d "
4751
0
       "action_length=%d ap_address=" MACSTR,
4752
0
       frame->frame_type, frame->packet_type, alen,
4753
0
       MAC2STR(frame->ap_address));
4754
4755
0
  if (frame->frame_type != RSN_REMOTE_FRAME_TYPE_FT_RRB) {
4756
    /* Discard frame per IEEE Std 802.11r-2008, 11A.10.3 */
4757
0
    wpa_printf(MSG_DEBUG, "FT: RRB discarded frame with "
4758
0
         "unrecognized type %d", frame->frame_type);
4759
0
    return -1;
4760
0
  }
4761
4762
0
  if (alen > data_len - sizeof(*frame)) {
4763
0
    wpa_printf(MSG_DEBUG, "FT: RRB frame too short for action "
4764
0
         "frame");
4765
0
    return -1;
4766
0
  }
4767
4768
0
  wpa_hexdump(MSG_MSGDUMP, "FT: RRB - FT Action frame", pos, alen);
4769
4770
0
  if (alen < 1 + 1 + 2 * ETH_ALEN) {
4771
0
    wpa_printf(MSG_DEBUG, "FT: Too short RRB frame (not enough "
4772
0
         "room for Action Frame body); alen=%lu",
4773
0
         (unsigned long) alen);
4774
0
    return -1;
4775
0
  }
4776
0
  start = pos;
4777
0
  end = pos + alen;
4778
4779
0
  if (*pos != WLAN_ACTION_FT) {
4780
0
    wpa_printf(MSG_DEBUG, "FT: Unexpected Action frame category "
4781
0
         "%d", *pos);
4782
0
    return -1;
4783
0
  }
4784
4785
0
  pos++;
4786
0
  action = *pos++;
4787
0
  sta_addr = pos;
4788
0
  pos += ETH_ALEN;
4789
0
  target_ap_addr = pos;
4790
0
  pos += ETH_ALEN;
4791
0
  wpa_printf(MSG_DEBUG, "FT: RRB Action Frame: action=%d sta_addr="
4792
0
       MACSTR " target_ap_addr=" MACSTR,
4793
0
       action, MAC2STR(sta_addr), MAC2STR(target_ap_addr));
4794
4795
0
  if (frame->packet_type == FT_PACKET_REQUEST) {
4796
0
    wpa_printf(MSG_DEBUG, "FT: FT Packet Type - Request");
4797
4798
0
    if (action != 1) {
4799
0
      wpa_printf(MSG_DEBUG, "FT: Unexpected Action %d in "
4800
0
           "RRB Request", action);
4801
0
      return -1;
4802
0
    }
4803
4804
0
    if (!ether_addr_equal(target_ap_addr, wpa_auth->addr)) {
4805
0
      wpa_printf(MSG_DEBUG, "FT: Target AP address in the "
4806
0
           "RRB Request does not match with own "
4807
0
           "address");
4808
0
      return -1;
4809
0
    }
4810
4811
0
    if (wpa_ft_rrb_rx_request(wpa_auth, frame->ap_address,
4812
0
            sta_addr, pos, end - pos) < 0)
4813
0
      return -1;
4814
0
  } else if (frame->packet_type == FT_PACKET_RESPONSE) {
4815
0
    u16 status_code;
4816
4817
0
    if (end - pos < 2) {
4818
0
      wpa_printf(MSG_DEBUG, "FT: Not enough room for status "
4819
0
           "code in RRB Response");
4820
0
      return -1;
4821
0
    }
4822
0
    status_code = WPA_GET_LE16(pos);
4823
4824
0
    wpa_printf(MSG_DEBUG, "FT: FT Packet Type - Response "
4825
0
         "(status_code=%d)", status_code);
4826
4827
0
    if (wpa_ft_action_send(wpa_auth, sta_addr, start, alen) < 0)
4828
0
      return -1;
4829
0
  } else {
4830
0
    wpa_printf(MSG_DEBUG, "FT: RRB discarded frame with unknown "
4831
0
         "packet_type %d", frame->packet_type);
4832
0
    return -1;
4833
0
  }
4834
4835
0
  return 0;
4836
0
}
4837
4838
4839
void wpa_ft_rrb_oui_rx(struct wpa_authenticator *wpa_auth, const u8 *src_addr,
4840
           const u8 *dst_addr, u8 oui_suffix, const u8 *data,
4841
           size_t data_len)
4842
0
{
4843
0
  const u8 *auth, *enc;
4844
0
  size_t alen, elen;
4845
0
  int no_defer = 0;
4846
4847
0
  wpa_printf(MSG_DEBUG, "FT: RRB-OUI(" MACSTR
4848
0
       ") received frame from remote AP "
4849
0
       MACSTR " oui_suffix=%u dst=" MACSTR,
4850
0
       MAC2STR(wpa_auth->addr), MAC2STR(src_addr), oui_suffix,
4851
0
       MAC2STR(dst_addr));
4852
0
  wpa_hexdump(MSG_MSGDUMP, "FT: RRB frame payload", data, data_len);
4853
4854
0
  if (is_multicast_ether_addr(src_addr)) {
4855
0
    wpa_printf(MSG_DEBUG,
4856
0
         "FT: RRB-OUI received frame from multicast address "
4857
0
         MACSTR, MAC2STR(src_addr));
4858
0
    return;
4859
0
  }
4860
4861
0
  if (is_multicast_ether_addr(dst_addr))
4862
0
    no_defer = 1;
4863
4864
0
  if (data_len < sizeof(u16)) {
4865
0
    wpa_printf(MSG_DEBUG, "FT: RRB-OUI frame too short");
4866
0
    return;
4867
0
  }
4868
4869
0
  alen = WPA_GET_LE16(data);
4870
0
  if (data_len < sizeof(u16) + alen) {
4871
0
    wpa_printf(MSG_DEBUG, "FT: RRB-OUI frame too short");
4872
0
    return;
4873
0
  }
4874
4875
0
  auth = data + sizeof(u16);
4876
0
  wpa_hexdump(MSG_MSGDUMP, "FT: Authenticated payload", auth, alen);
4877
0
  enc = data + sizeof(u16) + alen;
4878
0
  elen = data_len - sizeof(u16) - alen;
4879
0
  wpa_hexdump(MSG_MSGDUMP, "FT: Encrypted payload", enc, elen);
4880
4881
0
  switch (oui_suffix) {
4882
0
  case FT_PACKET_R0KH_R1KH_PULL:
4883
0
    wpa_ft_rrb_rx_pull(wpa_auth, src_addr, enc, elen, auth, alen,
4884
0
           no_defer);
4885
0
    break;
4886
0
  case FT_PACKET_R0KH_R1KH_RESP:
4887
0
    wpa_ft_rrb_rx_resp(wpa_auth, src_addr, enc, elen, auth, alen,
4888
0
           no_defer);
4889
0
    break;
4890
0
  case FT_PACKET_R0KH_R1KH_PUSH:
4891
0
    wpa_ft_rrb_rx_push(wpa_auth, src_addr, enc, elen, auth, alen,
4892
0
           no_defer);
4893
0
    break;
4894
0
  case FT_PACKET_R0KH_R1KH_SEQ_REQ:
4895
0
    wpa_ft_rrb_rx_seq_req(wpa_auth, src_addr, enc, elen, auth, alen,
4896
0
              no_defer);
4897
0
    break;
4898
0
  case FT_PACKET_R0KH_R1KH_SEQ_RESP:
4899
0
    wpa_ft_rrb_rx_seq_resp(wpa_auth, src_addr, enc, elen, auth,
4900
0
               alen, no_defer);
4901
0
    break;
4902
0
  }
4903
0
}
4904
4905
4906
static int wpa_ft_generate_pmk_r1(struct wpa_authenticator *wpa_auth,
4907
          struct wpa_ft_pmk_r0_sa *pmk_r0,
4908
          struct ft_remote_r1kh *r1kh,
4909
          const u8 *s1kh_id)
4910
0
{
4911
0
  u8 *packet;
4912
0
  size_t packet_len;
4913
0
  struct ft_rrb_seq f_seq;
4914
0
  struct tlv_list push[] = {
4915
0
    { .type = FT_RRB_S1KH_ID, .len = ETH_ALEN,
4916
0
      .data = s1kh_id },
4917
0
    { .type = FT_RRB_PMK_R0_NAME, .len = WPA_PMK_NAME_LEN,
4918
0
      .data = pmk_r0->pmk_r0_name },
4919
0
    { .type = FT_RRB_LAST_EMPTY, .len = 0, .data = NULL },
4920
0
  };
4921
0
  struct tlv_list push_auth[] = {
4922
0
    { .type = FT_RRB_SEQ, .len = sizeof(f_seq),
4923
0
      .data = (u8 *) &f_seq },
4924
0
    { .type = FT_RRB_R0KH_ID,
4925
0
      .len = wpa_auth->conf.r0_key_holder_len,
4926
0
      .data = wpa_auth->conf.r0_key_holder },
4927
0
    { .type = FT_RRB_R1KH_ID, .len = FT_R1KH_ID_LEN,
4928
0
      .data = r1kh->id },
4929
0
    { .type = FT_RRB_LAST_EMPTY, .len = 0, .data = NULL },
4930
0
  };
4931
4932
0
  if (wpa_ft_new_seq(r1kh->seq, &f_seq) < 0) {
4933
0
    wpa_printf(MSG_DEBUG, "FT: Failed to get seq num");
4934
0
    return -1;
4935
0
  }
4936
4937
0
  wpa_printf(MSG_DEBUG, "FT: Send PMK-R1 push from " MACSTR
4938
0
       " to remote R0KH address " MACSTR,
4939
0
       MAC2STR(wpa_auth->addr), MAC2STR(r1kh->addr));
4940
4941
0
  if (wpa_ft_rrb_build_r0(r1kh->key, sizeof(r1kh->key), push, pmk_r0,
4942
0
        r1kh->id, s1kh_id, push_auth, wpa_auth->addr,
4943
0
        FT_PACKET_R0KH_R1KH_PUSH,
4944
0
        &packet, &packet_len) < 0)
4945
0
    return -1;
4946
4947
0
  wpa_ft_rrb_oui_send(wpa_auth, r1kh->addr, FT_PACKET_R0KH_R1KH_PUSH,
4948
0
          packet, packet_len);
4949
4950
0
  os_free(packet);
4951
0
  return 0;
4952
0
}
4953
4954
4955
void wpa_ft_push_pmk_r1(struct wpa_authenticator *wpa_auth, const u8 *addr)
4956
292
{
4957
292
  struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
4958
292
  struct wpa_ft_pmk_r0_sa *r0, *r0found = NULL;
4959
292
  struct ft_remote_r1kh *r1kh;
4960
4961
292
  if (!wpa_auth->conf.pmk_r1_push)
4962
292
    return;
4963
0
  if (!wpa_auth->conf.r1kh_list)
4964
0
    return;
4965
4966
0
  dl_list_for_each(r0, &cache->pmk_r0, struct wpa_ft_pmk_r0_sa, list) {
4967
0
    if (ether_addr_equal(r0->spa, addr)) {
4968
0
      r0found = r0;
4969
0
      break;
4970
0
    }
4971
0
  }
4972
4973
0
  r0 = r0found;
4974
0
  if (r0 == NULL || r0->pmk_r1_pushed)
4975
0
    return;
4976
0
  r0->pmk_r1_pushed = 1;
4977
4978
0
  wpa_printf(MSG_DEBUG, "FT: Deriving and pushing PMK-R1 keys to R1KHs "
4979
0
       "for STA " MACSTR, MAC2STR(addr));
4980
4981
0
  for (r1kh = *wpa_auth->conf.r1kh_list; r1kh; r1kh = r1kh->next) {
4982
0
    if (is_zero_ether_addr(r1kh->addr) ||
4983
0
        is_zero_ether_addr(r1kh->id))
4984
0
      continue;
4985
0
    if (wpa_ft_rrb_init_r1kh_seq(r1kh) < 0)
4986
0
      continue;
4987
0
    wpa_ft_generate_pmk_r1(wpa_auth, r0, r1kh, addr);
4988
0
  }
4989
0
}
4990
4991
#endif /* CONFIG_IEEE80211R_AP */