Coverage Report

Created: 2025-04-24 06:18

/src/hostap/src/ap/wpa_auth_ie.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * hostapd - WPA/RSN IE and KDE definitions
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 "common/ieee802_11_defs.h"
13
#include "drivers/driver.h"
14
#include "eapol_auth/eapol_auth_sm.h"
15
#include "ap_config.h"
16
#include "ieee802_11.h"
17
#include "wpa_auth.h"
18
#include "pmksa_cache_auth.h"
19
#include "wpa_auth_ie.h"
20
#include "wpa_auth_i.h"
21
22
23
#ifdef CONFIG_RSN_TESTING
24
int rsn_testing = 0;
25
#endif /* CONFIG_RSN_TESTING */
26
27
28
static int wpa_write_wpa_ie(struct wpa_auth_config *conf, u8 *buf, size_t len)
29
0
{
30
0
  struct wpa_ie_hdr *hdr;
31
0
  int num_suites;
32
0
  u8 *pos, *count;
33
0
  u32 suite;
34
35
0
  hdr = (struct wpa_ie_hdr *) buf;
36
0
  hdr->elem_id = WLAN_EID_VENDOR_SPECIFIC;
37
0
  RSN_SELECTOR_PUT(hdr->oui, WPA_OUI_TYPE);
38
0
  WPA_PUT_LE16(hdr->version, WPA_VERSION);
39
0
  pos = (u8 *) (hdr + 1);
40
41
0
  suite = wpa_cipher_to_suite(WPA_PROTO_WPA, conf->wpa_group);
42
0
  if (suite == 0) {
43
0
    wpa_printf(MSG_DEBUG, "Invalid group cipher (%d).",
44
0
         conf->wpa_group);
45
0
    return -1;
46
0
  }
47
0
  RSN_SELECTOR_PUT(pos, suite);
48
0
  pos += WPA_SELECTOR_LEN;
49
50
0
  count = pos;
51
0
  pos += 2;
52
53
0
  num_suites = wpa_cipher_put_suites(pos, conf->wpa_pairwise);
54
0
  if (num_suites == 0) {
55
0
    wpa_printf(MSG_DEBUG, "Invalid pairwise cipher (%d).",
56
0
         conf->wpa_pairwise);
57
0
    return -1;
58
0
  }
59
0
  pos += num_suites * WPA_SELECTOR_LEN;
60
0
  WPA_PUT_LE16(count, num_suites);
61
62
0
  num_suites = 0;
63
0
  count = pos;
64
0
  pos += 2;
65
66
0
  if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
67
0
    RSN_SELECTOR_PUT(pos, WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
68
0
    pos += WPA_SELECTOR_LEN;
69
0
    num_suites++;
70
0
  }
71
0
  if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
72
0
    RSN_SELECTOR_PUT(pos, WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
73
0
    pos += WPA_SELECTOR_LEN;
74
0
    num_suites++;
75
0
  }
76
77
0
  if (num_suites == 0) {
78
0
    wpa_printf(MSG_DEBUG, "Invalid key management type (%d).",
79
0
         conf->wpa_key_mgmt);
80
0
    return -1;
81
0
  }
82
0
  WPA_PUT_LE16(count, num_suites);
83
84
  /* WPA Capabilities; use defaults, so no need to include it */
85
86
0
  hdr->len = (pos - buf) - 2;
87
88
0
  return pos - buf;
89
0
}
90
91
92
static u16 wpa_own_rsn_capab(struct wpa_auth_config *conf,
93
           enum mfp_options mfp)
94
0
{
95
0
  u16 capab = 0;
96
97
0
  if (conf->rsn_preauth)
98
0
    capab |= WPA_CAPABILITY_PREAUTH;
99
0
  if (conf->wmm_enabled) {
100
    /* 4 PTKSA replay counters when using WMM */
101
0
    capab |= (RSN_NUM_REPLAY_COUNTERS_16 << 2);
102
0
  }
103
0
  if (mfp != NO_MGMT_FRAME_PROTECTION) {
104
0
    capab |= WPA_CAPABILITY_MFPC;
105
0
    if (mfp == MGMT_FRAME_PROTECTION_REQUIRED)
106
0
      capab |= WPA_CAPABILITY_MFPR;
107
0
  }
108
#ifdef CONFIG_OCV
109
  if (conf->ocv)
110
    capab |= WPA_CAPABILITY_OCVC;
111
#endif /* CONFIG_OCV */
112
#ifdef CONFIG_RSN_TESTING
113
  if (rsn_testing)
114
    capab |= BIT(8) | BIT(15);
115
#endif /* CONFIG_RSN_TESTING */
116
0
  if (conf->extended_key_id)
117
0
    capab |= WPA_CAPABILITY_EXT_KEY_ID_FOR_UNICAST;
118
119
0
  return capab;
120
0
}
121
122
123
static u8 * rsne_write_data(u8 *buf, size_t len, u8 *pos, int group,
124
          int pairwise, int key_mgmt, u16 rsn_capab,
125
          const u8 *pmkid, enum mfp_options mfp,
126
          int group_mgmt_cipher)
127
0
{
128
0
  int num_suites, res;
129
0
  u8 *count;
130
0
  u32 suite;
131
132
0
  suite = wpa_cipher_to_suite(WPA_PROTO_RSN, group);
133
0
  if (suite == 0) {
134
0
    wpa_printf(MSG_DEBUG, "Invalid group cipher (%d).", group);
135
0
    return NULL;
136
0
  }
137
0
  RSN_SELECTOR_PUT(pos, suite);
138
0
  pos += RSN_SELECTOR_LEN;
139
140
0
  num_suites = 0;
141
0
  count = pos;
142
0
  pos += 2;
143
144
#ifdef CONFIG_RSN_TESTING
145
  if (rsn_testing) {
146
    RSN_SELECTOR_PUT(pos, RSN_SELECTOR(0x12, 0x34, 0x56, 1));
147
    pos += RSN_SELECTOR_LEN;
148
    num_suites++;
149
  }
150
#endif /* CONFIG_RSN_TESTING */
151
152
0
  res = rsn_cipher_put_suites(pos, pairwise);
153
0
  num_suites += res;
154
0
  pos += res * RSN_SELECTOR_LEN;
155
156
#ifdef CONFIG_RSN_TESTING
157
  if (rsn_testing) {
158
    RSN_SELECTOR_PUT(pos, RSN_SELECTOR(0x12, 0x34, 0x56, 2));
159
    pos += RSN_SELECTOR_LEN;
160
    num_suites++;
161
  }
162
#endif /* CONFIG_RSN_TESTING */
163
164
0
  if (num_suites == 0) {
165
0
    wpa_printf(MSG_DEBUG, "Invalid pairwise cipher (%d).",
166
0
         pairwise);
167
0
    return NULL;
168
0
  }
169
0
  WPA_PUT_LE16(count, num_suites);
170
171
0
  num_suites = 0;
172
0
  count = pos;
173
0
  pos += 2;
174
175
#ifdef CONFIG_RSN_TESTING
176
  if (rsn_testing) {
177
    RSN_SELECTOR_PUT(pos, RSN_SELECTOR(0x12, 0x34, 0x56, 1));
178
    pos += RSN_SELECTOR_LEN;
179
    num_suites++;
180
  }
181
#endif /* CONFIG_RSN_TESTING */
182
183
0
  if (key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
184
0
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_UNSPEC_802_1X);
185
0
    pos += RSN_SELECTOR_LEN;
186
0
    num_suites++;
187
0
  }
188
0
  if (key_mgmt & WPA_KEY_MGMT_PSK) {
189
0
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X);
190
0
    pos += RSN_SELECTOR_LEN;
191
0
    num_suites++;
192
0
  }
193
0
#ifdef CONFIG_IEEE80211R_AP
194
0
  if (key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
195
0
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_802_1X);
196
0
    pos += RSN_SELECTOR_LEN;
197
0
    num_suites++;
198
0
  }
199
#ifdef CONFIG_SHA384
200
  if (key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X_SHA384) {
201
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_802_1X_SHA384);
202
    pos += RSN_SELECTOR_LEN;
203
    num_suites++;
204
  }
205
#endif /* CONFIG_SHA384 */
206
0
  if (key_mgmt & WPA_KEY_MGMT_FT_PSK) {
207
0
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_PSK);
208
0
    pos += RSN_SELECTOR_LEN;
209
0
    num_suites++;
210
0
  }
211
0
#endif /* CONFIG_IEEE80211R_AP */
212
#ifdef CONFIG_SHA384
213
  if (key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA384) {
214
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_802_1X_SHA384);
215
    pos += RSN_SELECTOR_LEN;
216
    num_suites++;
217
  }
218
#endif /* CONFIG_SHA384 */
219
0
  if (key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
220
0
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_802_1X_SHA256);
221
0
    pos += RSN_SELECTOR_LEN;
222
0
    num_suites++;
223
0
  }
224
0
  if (key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
225
0
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_PSK_SHA256);
226
0
    pos += RSN_SELECTOR_LEN;
227
0
    num_suites++;
228
0
  }
229
#ifdef CONFIG_SAE
230
  if (key_mgmt & WPA_KEY_MGMT_SAE) {
231
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_SAE);
232
    pos += RSN_SELECTOR_LEN;
233
    num_suites++;
234
  }
235
  if (key_mgmt & WPA_KEY_MGMT_SAE_EXT_KEY) {
236
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_SAE_EXT_KEY);
237
    pos += RSN_SELECTOR_LEN;
238
    num_suites++;
239
  }
240
  if (key_mgmt & WPA_KEY_MGMT_FT_SAE) {
241
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_SAE);
242
    pos += RSN_SELECTOR_LEN;
243
    num_suites++;
244
  }
245
  if (key_mgmt & WPA_KEY_MGMT_FT_SAE_EXT_KEY) {
246
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_SAE_EXT_KEY);
247
    pos += RSN_SELECTOR_LEN;
248
    num_suites++;
249
  }
250
#endif /* CONFIG_SAE */
251
0
  if (key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
252
0
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_802_1X_SUITE_B);
253
0
    pos += RSN_SELECTOR_LEN;
254
0
    num_suites++;
255
0
  }
256
0
  if (key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
257
0
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192);
258
0
    pos += RSN_SELECTOR_LEN;
259
0
    num_suites++;
260
0
  }
261
#ifdef CONFIG_FILS
262
  if (key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
263
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FILS_SHA256);
264
    pos += RSN_SELECTOR_LEN;
265
    num_suites++;
266
  }
267
  if (key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
268
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FILS_SHA384);
269
    pos += RSN_SELECTOR_LEN;
270
    num_suites++;
271
  }
272
#ifdef CONFIG_IEEE80211R_AP
273
  if (key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
274
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA256);
275
    pos += RSN_SELECTOR_LEN;
276
    num_suites++;
277
  }
278
  if (key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
279
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA384);
280
    pos += RSN_SELECTOR_LEN;
281
    num_suites++;
282
  }
283
#endif /* CONFIG_IEEE80211R_AP */
284
#endif /* CONFIG_FILS */
285
#ifdef CONFIG_OWE
286
  if (key_mgmt & WPA_KEY_MGMT_OWE) {
287
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_OWE);
288
    pos += RSN_SELECTOR_LEN;
289
    num_suites++;
290
  }
291
#endif /* CONFIG_OWE */
292
#ifdef CONFIG_DPP
293
  if (key_mgmt & WPA_KEY_MGMT_DPP) {
294
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_DPP);
295
    pos += RSN_SELECTOR_LEN;
296
    num_suites++;
297
  }
298
#endif /* CONFIG_DPP */
299
#ifdef CONFIG_PASN
300
  if (key_mgmt & WPA_KEY_MGMT_PASN) {
301
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_PASN);
302
    pos += RSN_SELECTOR_LEN;
303
    num_suites++;
304
  }
305
#endif /* CONFIG_PASN */
306
307
#ifdef CONFIG_RSN_TESTING
308
  if (rsn_testing) {
309
    RSN_SELECTOR_PUT(pos, RSN_SELECTOR(0x12, 0x34, 0x56, 2));
310
    pos += RSN_SELECTOR_LEN;
311
    num_suites++;
312
  }
313
#endif /* CONFIG_RSN_TESTING */
314
315
0
  if (num_suites == 0) {
316
0
    wpa_printf(MSG_DEBUG, "Invalid key management type (%d).",
317
0
         key_mgmt);
318
0
    return NULL;
319
0
  }
320
0
  WPA_PUT_LE16(count, num_suites);
321
322
  /* RSN Capabilities */
323
0
  WPA_PUT_LE16(pos, rsn_capab);
324
0
  pos += 2;
325
326
0
  if (pmkid) {
327
0
    if (2 + PMKID_LEN > buf + len - pos)
328
0
      return NULL;
329
    /* PMKID Count */
330
0
    WPA_PUT_LE16(pos, 1);
331
0
    pos += 2;
332
0
    os_memcpy(pos, pmkid, PMKID_LEN);
333
0
    pos += PMKID_LEN;
334
0
  }
335
336
337
0
  if (mfp != NO_MGMT_FRAME_PROTECTION &&
338
0
      group_mgmt_cipher != WPA_CIPHER_AES_128_CMAC) {
339
0
    if (2 + 4 > buf + len - pos)
340
0
      return NULL;
341
0
    if (!pmkid) {
342
      /* PMKID Count */
343
0
      WPA_PUT_LE16(pos, 0);
344
0
      pos += 2;
345
0
    }
346
347
    /* Management Group Cipher Suite */
348
0
    switch (group_mgmt_cipher) {
349
0
    case WPA_CIPHER_AES_128_CMAC:
350
0
      RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
351
0
      break;
352
0
    case WPA_CIPHER_BIP_GMAC_128:
353
0
      RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_BIP_GMAC_128);
354
0
      break;
355
0
    case WPA_CIPHER_BIP_GMAC_256:
356
0
      RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_BIP_GMAC_256);
357
0
      break;
358
0
    case WPA_CIPHER_BIP_CMAC_256:
359
0
      RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_BIP_CMAC_256);
360
0
      break;
361
0
    default:
362
0
      wpa_printf(MSG_DEBUG,
363
0
           "Invalid group management cipher (0x%x)",
364
0
           group_mgmt_cipher);
365
0
      return NULL;
366
0
    }
367
0
    pos += RSN_SELECTOR_LEN;
368
0
  }
369
370
#ifdef CONFIG_RSN_TESTING
371
  if (rsn_testing) {
372
    /*
373
     * Fill in any defined fields and add extra data to the end of
374
     * the element.
375
     */
376
    int pmkid_count_set = pmkid != NULL;
377
    if (mfp != NO_MGMT_FRAME_PROTECTION)
378
      pmkid_count_set = 1;
379
    /* PMKID Count */
380
    WPA_PUT_LE16(pos, 0);
381
    pos += 2;
382
    if (mfp == NO_MGMT_FRAME_PROTECTION) {
383
      /* Management Group Cipher Suite */
384
      RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
385
      pos += RSN_SELECTOR_LEN;
386
    }
387
388
    os_memset(pos, 0x12, 17);
389
    pos += 17;
390
  }
391
#endif /* CONFIG_RSN_TESTING */
392
0
  return pos;
393
0
}
394
395
396
int wpa_write_rsn_ie(struct wpa_auth_config *conf, u8 *buf, size_t len,
397
         const u8 *pmkid)
398
0
{
399
0
  struct rsn_ie_hdr *hdr;
400
0
  u8 *pos;
401
402
0
  hdr = (struct rsn_ie_hdr *) buf;
403
0
  hdr->elem_id = WLAN_EID_RSN;
404
0
  WPA_PUT_LE16(hdr->version, RSN_VERSION);
405
0
  pos = (u8 *) (hdr + 1);
406
407
0
  pos = rsne_write_data(buf, len, pos, conf->wpa_group,
408
0
            conf->rsn_pairwise, conf->wpa_key_mgmt,
409
0
            wpa_own_rsn_capab(conf, conf->ieee80211w), pmkid,
410
0
            conf->ieee80211w, conf->group_mgmt_cipher);
411
0
  if (!pos)
412
0
    return -1;
413
414
0
  hdr->len = (pos - buf) - 2;
415
416
0
  return pos - buf;
417
0
}
418
419
420
static int wpa_write_rsne_override(struct wpa_auth_config *conf, u8 *buf,
421
           size_t len)
422
0
{
423
0
  u8 *pos, *len_pos;
424
425
0
  pos = buf;
426
0
  *pos++ = WLAN_EID_VENDOR_SPECIFIC;
427
0
  len_pos = pos++;
428
429
0
  WPA_PUT_BE32(pos, RSNE_OVERRIDE_IE_VENDOR_TYPE);
430
0
  pos += 4;
431
432
0
  WPA_PUT_LE16(pos, RSN_VERSION);
433
0
  pos += 2;
434
435
0
  pos = rsne_write_data(buf, len, pos, conf->wpa_group,
436
0
            conf->rsn_override_pairwise,
437
0
            conf->rsn_override_key_mgmt,
438
0
            wpa_own_rsn_capab(conf, conf->rsn_override_mfp),
439
0
            NULL, conf->rsn_override_mfp,
440
0
            conf->group_mgmt_cipher);
441
0
  if (!pos)
442
0
    return -1;
443
444
0
  *len_pos = (pos - buf) - 2;
445
446
0
  return pos - buf;
447
0
}
448
449
450
static int wpa_write_rsne_override_2(struct wpa_auth_config *conf, u8 *buf,
451
             size_t len)
452
0
{
453
0
  u8 *pos, *len_pos;
454
455
0
  pos = buf;
456
0
  *pos++ = WLAN_EID_VENDOR_SPECIFIC;
457
0
  len_pos = pos++;
458
459
0
  WPA_PUT_BE32(pos, RSNE_OVERRIDE_2_IE_VENDOR_TYPE);
460
0
  pos += 4;
461
462
0
  WPA_PUT_LE16(pos, RSN_VERSION);
463
0
  pos += 2;
464
465
0
  pos = rsne_write_data(buf, len, pos, conf->wpa_group,
466
0
            conf->rsn_override_pairwise_2,
467
0
            conf->rsn_override_key_mgmt_2,
468
0
            wpa_own_rsn_capab(conf, conf->rsn_override_mfp_2),
469
0
            NULL, conf->rsn_override_mfp_2,
470
0
            conf->group_mgmt_cipher);
471
0
  if (!pos)
472
0
    return -1;
473
474
0
  *len_pos = (pos - buf) - 2;
475
476
0
  return pos - buf;
477
0
}
478
479
480
static u32 rsnxe_capab(struct wpa_auth_config *conf, int key_mgmt)
481
0
{
482
0
  u32 capab = 0;
483
484
0
  if (wpa_key_mgmt_sae(key_mgmt) &&
485
0
      (conf->sae_pwe == SAE_PWE_HASH_TO_ELEMENT ||
486
0
       conf->sae_pwe == SAE_PWE_BOTH || conf->sae_pk ||
487
0
       wpa_key_mgmt_sae_ext_key(key_mgmt))) {
488
0
    capab |= BIT(WLAN_RSNX_CAPAB_SAE_H2E);
489
#ifdef CONFIG_SAE_PK
490
    if (conf->sae_pk)
491
      capab |= BIT(WLAN_RSNX_CAPAB_SAE_PK);
492
#endif /* CONFIG_SAE_PK */
493
0
  }
494
495
0
  if (conf->secure_ltf)
496
0
    capab |= BIT(WLAN_RSNX_CAPAB_SECURE_LTF);
497
0
  if (conf->secure_rtt)
498
0
    capab |= BIT(WLAN_RSNX_CAPAB_SECURE_RTT);
499
0
  if (conf->prot_range_neg)
500
0
    capab |= BIT(WLAN_RSNX_CAPAB_URNM_MFPR);
501
0
  if (conf->ssid_protection)
502
0
    capab |= BIT(WLAN_RSNX_CAPAB_SSID_PROTECTION);
503
0
  if (conf->spp_amsdu)
504
0
    capab |= BIT(WLAN_RSNX_CAPAB_SPP_A_MSDU);
505
506
0
  return capab;
507
0
}
508
509
510
int wpa_write_rsnxe(struct wpa_auth_config *conf, u8 *buf, size_t len)
511
0
{
512
0
  u8 *pos = buf;
513
0
  u32 capab = 0, tmp;
514
0
  size_t flen;
515
516
0
  capab = rsnxe_capab(conf, conf->wpa_key_mgmt);
517
518
0
  if (!capab)
519
0
    return 0; /* no supported extended RSN capabilities */
520
0
  tmp = capab;
521
0
  flen = 0;
522
0
  while (tmp) {
523
0
    flen++;
524
0
    tmp >>= 8;
525
0
  }
526
0
  if (len < 2 + flen)
527
0
    return -1;
528
0
  capab |= flen - 1; /* bit 0-3 = Field length (n - 1) */
529
530
0
  *pos++ = WLAN_EID_RSNX;
531
0
  *pos++ = flen;
532
0
  while (capab) {
533
0
    *pos++ = capab & 0xff;
534
0
    capab >>= 8;
535
0
  }
536
537
0
  return pos - buf;
538
0
}
539
540
541
static int wpa_write_rsnxe_override(struct wpa_auth_config *conf, u8 *buf,
542
            size_t len)
543
0
{
544
0
  u8 *pos = buf;
545
0
  u32 capab, tmp;
546
0
  size_t flen;
547
548
0
  capab = rsnxe_capab(conf, conf->rsn_override_key_mgmt |
549
0
          conf->rsn_override_key_mgmt_2);
550
551
0
  if (!capab)
552
0
    return 0; /* no supported extended RSN capabilities */
553
0
  tmp = capab;
554
0
  flen = 0;
555
0
  while (tmp) {
556
0
    flen++;
557
0
    tmp >>= 8;
558
0
  }
559
0
  if (len < 2 + flen)
560
0
    return -1;
561
0
  capab |= flen - 1; /* bit 0-3 = Field length (n - 1) */
562
563
0
  *pos++ = WLAN_EID_VENDOR_SPECIFIC;
564
0
  *pos++ = 4 + flen;
565
0
  WPA_PUT_BE32(pos, RSNXE_OVERRIDE_IE_VENDOR_TYPE);
566
0
  pos += 4;
567
568
0
  while (capab) {
569
0
    *pos++ = capab & 0xff;
570
0
    capab >>= 8;
571
0
  }
572
573
0
  return pos - buf;
574
0
}
575
576
577
int wpa_auth_gen_wpa_ie(struct wpa_authenticator *wpa_auth)
578
0
{
579
0
  u8 *pos, buf[1500];
580
0
  int res;
581
582
#ifdef CONFIG_TESTING_OPTIONS
583
  if (wpa_auth->conf.own_ie_override_len) {
584
    wpa_hexdump(MSG_DEBUG, "WPA: Forced own IE(s) for testing",
585
          wpa_auth->conf.own_ie_override,
586
          wpa_auth->conf.own_ie_override_len);
587
    os_free(wpa_auth->wpa_ie);
588
    wpa_auth->wpa_ie =
589
      os_malloc(wpa_auth->conf.own_ie_override_len);
590
    if (wpa_auth->wpa_ie == NULL)
591
      return -1;
592
    os_memcpy(wpa_auth->wpa_ie, wpa_auth->conf.own_ie_override,
593
        wpa_auth->conf.own_ie_override_len);
594
    wpa_auth->wpa_ie_len = wpa_auth->conf.own_ie_override_len;
595
    return 0;
596
  }
597
#endif /* CONFIG_TESTING_OPTIONS */
598
599
0
  pos = buf;
600
601
0
  if (wpa_auth->conf.wpa & WPA_PROTO_RSN) {
602
#ifdef CONFIG_TESTING_OPTIONS
603
    if (wpa_auth->conf.rsne_override_set) {
604
      wpa_hexdump(MSG_DEBUG,
605
            "RSN: Forced own RSNE for testing",
606
            wpa_auth->conf.rsne_override,
607
            wpa_auth->conf.rsne_override_len);
608
      if (sizeof(buf) - (pos - buf) <
609
          wpa_auth->conf.rsne_override_len)
610
        return -1;
611
      os_memcpy(pos, wpa_auth->conf.rsne_override,
612
          wpa_auth->conf.rsne_override_len);
613
      pos += wpa_auth->conf.rsne_override_len;
614
      goto rsnxe;
615
    }
616
#endif /* CONFIG_TESTING_OPTIONS */
617
0
    res = wpa_write_rsn_ie(&wpa_auth->conf,
618
0
               pos, buf + sizeof(buf) - pos, NULL);
619
0
    if (res < 0)
620
0
      return res;
621
0
    pos += res;
622
#ifdef CONFIG_TESTING_OPTIONS
623
  rsnxe:
624
    if (wpa_auth->conf.rsnxe_override_set) {
625
      wpa_hexdump(MSG_DEBUG,
626
            "RSN: Forced own RSNXE for testing",
627
            wpa_auth->conf.rsnxe_override,
628
            wpa_auth->conf.rsnxe_override_len);
629
      if (sizeof(buf) - (pos - buf) <
630
          wpa_auth->conf.rsnxe_override_len)
631
        return -1;
632
      os_memcpy(pos, wpa_auth->conf.rsnxe_override,
633
          wpa_auth->conf.rsnxe_override_len);
634
      pos += wpa_auth->conf.rsnxe_override_len;
635
      goto fte;
636
    }
637
#endif /* CONFIG_TESTING_OPTIONS */
638
0
    if (wpa_auth->conf.rsn_override_omit_rsnxe)
639
0
      res = 0;
640
0
    else
641
0
      res = wpa_write_rsnxe(&wpa_auth->conf, pos,
642
0
                buf + sizeof(buf) - pos);
643
0
    if (res < 0)
644
0
      return res;
645
0
    pos += res;
646
0
  }
647
#ifdef CONFIG_TESTING_OPTIONS
648
fte:
649
#endif /* CONFIG_TESTING_OPTIONS */
650
0
#ifdef CONFIG_IEEE80211R_AP
651
0
  if (wpa_key_mgmt_ft(wpa_auth->conf.wpa_key_mgmt)) {
652
0
    res = wpa_write_mdie(&wpa_auth->conf, pos,
653
0
             buf + sizeof(buf) - pos);
654
0
    if (res < 0)
655
0
      return res;
656
0
    pos += res;
657
0
  }
658
0
#endif /* CONFIG_IEEE80211R_AP */
659
0
  if (wpa_auth->conf.wpa & WPA_PROTO_WPA) {
660
0
    res = wpa_write_wpa_ie(&wpa_auth->conf,
661
0
               pos, buf + sizeof(buf) - pos);
662
0
    if (res < 0)
663
0
      return res;
664
0
    pos += res;
665
0
  }
666
0
  if ((wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
667
0
      wpa_auth->conf.rsn_override_key_mgmt) {
668
#ifdef CONFIG_TESTING_OPTIONS
669
    if (wpa_auth->conf.rsnoe_override_set) {
670
      wpa_hexdump(MSG_DEBUG,
671
            "RSN: Forced own RSNOE for testing",
672
            wpa_auth->conf.rsnoe_override,
673
            wpa_auth->conf.rsnoe_override_len);
674
      if (sizeof(buf) - (pos - buf) <
675
          wpa_auth->conf.rsnoe_override_len)
676
        return -1;
677
      os_memcpy(pos, wpa_auth->conf.rsnoe_override,
678
          wpa_auth->conf.rsnoe_override_len);
679
      pos += wpa_auth->conf.rsnoe_override_len;
680
      goto rsno2e;
681
    }
682
#endif /* CONFIG_TESTING_OPTIONS */
683
0
    res = wpa_write_rsne_override(&wpa_auth->conf,
684
0
                pos, buf + sizeof(buf) - pos);
685
0
    if (res < 0)
686
0
      return res;
687
0
    pos += res;
688
0
  }
689
#ifdef CONFIG_TESTING_OPTIONS
690
rsno2e:
691
#endif /* CONFIG_TESTING_OPTIONS */
692
0
  if ((wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
693
0
      wpa_auth->conf.rsn_override_key_mgmt_2) {
694
#ifdef CONFIG_TESTING_OPTIONS
695
    if (wpa_auth->conf.rsno2e_override_set) {
696
      wpa_hexdump(MSG_DEBUG,
697
            "RSN: Forced own RSNO2E for testing",
698
            wpa_auth->conf.rsno2e_override,
699
            wpa_auth->conf.rsno2e_override_len);
700
      if (sizeof(buf) - (pos - buf) <
701
          wpa_auth->conf.rsno2e_override_len)
702
        return -1;
703
      os_memcpy(pos, wpa_auth->conf.rsno2e_override,
704
          wpa_auth->conf.rsno2e_override_len);
705
      pos += wpa_auth->conf.rsno2e_override_len;
706
      goto rsnxoe;
707
    }
708
#endif /* CONFIG_TESTING_OPTIONS */
709
0
    res = wpa_write_rsne_override_2(&wpa_auth->conf, pos,
710
0
            buf + sizeof(buf) - pos);
711
0
    if (res < 0)
712
0
      return res;
713
0
    pos += res;
714
0
  }
715
#ifdef CONFIG_TESTING_OPTIONS
716
rsnxoe:
717
#endif /* CONFIG_TESTING_OPTIONS */
718
0
  if ((wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
719
0
      (wpa_auth->conf.rsn_override_key_mgmt ||
720
0
       wpa_auth->conf.rsn_override_key_mgmt_2)) {
721
#ifdef CONFIG_TESTING_OPTIONS
722
    if (wpa_auth->conf.rsnxoe_override_set) {
723
      wpa_hexdump(MSG_DEBUG,
724
            "RSN: Forced own RSNXOE for testing",
725
            wpa_auth->conf.rsnxoe_override,
726
            wpa_auth->conf.rsnxoe_override_len);
727
      if (sizeof(buf) - (pos - buf) <
728
          wpa_auth->conf.rsnxoe_override_len)
729
        return -1;
730
      os_memcpy(pos, wpa_auth->conf.rsnxoe_override,
731
          wpa_auth->conf.rsnxoe_override_len);
732
      pos += wpa_auth->conf.rsnxoe_override_len;
733
      goto done;
734
    }
735
#endif /* CONFIG_TESTING_OPTIONS */
736
0
    res = wpa_write_rsnxe_override(&wpa_auth->conf, pos,
737
0
                 buf + sizeof(buf) - pos);
738
0
    if (res < 0)
739
0
      return res;
740
0
    pos += res;
741
0
  }
742
#ifdef CONFIG_TESTING_OPTIONS
743
done:
744
#endif /* CONFIG_TESTING_OPTIONS */
745
746
0
  wpa_hexdump(MSG_DEBUG, "RSN: Own IEs", buf, pos - buf);
747
0
  os_free(wpa_auth->wpa_ie);
748
0
  wpa_auth->wpa_ie = os_malloc(pos - buf);
749
0
  if (wpa_auth->wpa_ie == NULL)
750
0
    return -1;
751
0
  os_memcpy(wpa_auth->wpa_ie, buf, pos - buf);
752
0
  wpa_auth->wpa_ie_len = pos - buf;
753
754
0
  return 0;
755
0
}
756
757
758
u8 * wpa_add_kde(u8 *pos, u32 kde, const u8 *data, size_t data_len,
759
     const u8 *data2, size_t data2_len)
760
0
{
761
0
  *pos++ = WLAN_EID_VENDOR_SPECIFIC;
762
0
  *pos++ = RSN_SELECTOR_LEN + data_len + data2_len;
763
0
  RSN_SELECTOR_PUT(pos, kde);
764
0
  pos += RSN_SELECTOR_LEN;
765
0
  os_memcpy(pos, data, data_len);
766
0
  pos += data_len;
767
0
  if (data2) {
768
0
    os_memcpy(pos, data2, data2_len);
769
0
    pos += data2_len;
770
0
  }
771
0
  return pos;
772
0
}
773
774
775
struct wpa_auth_okc_iter_data {
776
  struct rsn_pmksa_cache_entry *pmksa;
777
  const u8 *aa;
778
  const u8 *spa;
779
  const u8 *pmkid;
780
};
781
782
783
static int wpa_auth_okc_iter(struct wpa_authenticator *a, void *ctx)
784
0
{
785
0
  struct wpa_auth_okc_iter_data *data = ctx;
786
0
  data->pmksa = pmksa_cache_get_okc(a->pmksa, data->aa, data->spa,
787
0
            data->pmkid);
788
0
  if (data->pmksa)
789
0
    return 1;
790
0
  return 0;
791
0
}
792
793
794
#ifdef CONFIG_IEEE80211BE
795
796
struct wpa_auth_link_iter_data {
797
  struct wpa_authenticator *wpa_auth;
798
  struct rsn_pmksa_cache_entry *pmksa;
799
  const u8 *spa;
800
  const u8 *pmkid;
801
};
802
803
static int wpa_auth_pmksa_iter(struct wpa_authenticator *a, void *ctx)
804
{
805
  struct wpa_auth_link_iter_data *data = ctx;
806
807
  if (a == data->wpa_auth ||
808
      !ether_addr_equal(a->mld_addr, data->wpa_auth->mld_addr))
809
    return 0;
810
811
  data->pmksa = pmksa_cache_auth_get(a->pmksa, data->spa, data->pmkid);
812
  if (data->pmksa)
813
    return 1;
814
  return 0;
815
}
816
817
#endif /* CONFIG_IEEE80211BE */
818
819
820
enum wpa_validate_result
821
wpa_validate_wpa_ie(struct wpa_authenticator *wpa_auth,
822
        struct wpa_state_machine *sm, int freq,
823
        const u8 *wpa_ie, size_t wpa_ie_len,
824
        const u8 *rsnxe, size_t rsnxe_len,
825
        const u8 *mdie, size_t mdie_len,
826
        const u8 *owe_dh, size_t owe_dh_len,
827
        struct wpa_state_machine *assoc_sm, bool is_ml)
828
0
{
829
0
  struct wpa_auth_config *conf = &wpa_auth->conf;
830
0
  struct wpa_ie_data data;
831
0
  int ciphers, key_mgmt, res, version;
832
0
  u32 selector;
833
0
  size_t i;
834
0
  const u8 *pmkid = NULL;
835
0
  bool ap_pmf_enabled;
836
837
0
  if (wpa_auth == NULL || sm == NULL)
838
0
    return WPA_NOT_ENABLED;
839
840
0
  if (wpa_ie == NULL || wpa_ie_len < 1)
841
0
    return WPA_INVALID_IE;
842
843
0
  if (wpa_ie[0] == WLAN_EID_RSN)
844
0
    version = WPA_PROTO_RSN;
845
0
  else
846
0
    version = WPA_PROTO_WPA;
847
848
0
  if (!(wpa_auth->conf.wpa & version)) {
849
0
    wpa_printf(MSG_DEBUG, "Invalid WPA proto (%d) from " MACSTR,
850
0
         version, MAC2STR(sm->addr));
851
0
    return WPA_INVALID_PROTO;
852
0
  }
853
854
0
  if (version == WPA_PROTO_RSN) {
855
0
    res = wpa_parse_wpa_ie_rsn(wpa_ie, wpa_ie_len, &data);
856
0
    if (!data.has_pairwise)
857
0
      data.pairwise_cipher = wpa_default_rsn_cipher(freq);
858
0
    if (!data.has_group)
859
0
      data.group_cipher = wpa_default_rsn_cipher(freq);
860
861
0
    if (wpa_key_mgmt_ft(data.key_mgmt) && !mdie &&
862
0
        !wpa_key_mgmt_only_ft(data.key_mgmt)) {
863
      /* Workaround for some HP and Epson printers that seem
864
       * to incorrectly copy the FT-PSK + WPA-PSK AKMs from AP
865
       * advertised RSNE to Association Request frame. */
866
0
      wpa_printf(MSG_DEBUG,
867
0
           "RSN: FT set in RSNE AKM but MDE is missing from "
868
0
           MACSTR
869
0
           " - ignore FT AKM(s) because there's also a non-FT AKM",
870
0
           MAC2STR(sm->addr));
871
0
      data.key_mgmt &= ~WPA_KEY_MGMT_FT;
872
0
    }
873
874
0
    selector = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
875
0
    if (0) {
876
0
    }
877
0
    else if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
878
0
      selector = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
879
0
    else if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B)
880
0
      selector = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
881
#ifdef CONFIG_FILS
882
#ifdef CONFIG_IEEE80211R_AP
883
    else if (data.key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384)
884
      selector = RSN_AUTH_KEY_MGMT_FT_FILS_SHA384;
885
    else if (data.key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256)
886
      selector = RSN_AUTH_KEY_MGMT_FT_FILS_SHA256;
887
#endif /* CONFIG_IEEE80211R_AP */
888
    else if (data.key_mgmt & WPA_KEY_MGMT_FILS_SHA384)
889
      selector = RSN_AUTH_KEY_MGMT_FILS_SHA384;
890
    else if (data.key_mgmt & WPA_KEY_MGMT_FILS_SHA256)
891
      selector = RSN_AUTH_KEY_MGMT_FILS_SHA256;
892
#endif /* CONFIG_FILS */
893
0
#ifdef CONFIG_IEEE80211R_AP
894
#ifdef CONFIG_SHA384
895
    else if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X_SHA384)
896
      selector = RSN_AUTH_KEY_MGMT_FT_802_1X_SHA384;
897
#endif /* CONFIG_SHA384 */
898
0
    else if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
899
0
      selector = RSN_AUTH_KEY_MGMT_FT_802_1X;
900
0
    else if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK)
901
0
      selector = RSN_AUTH_KEY_MGMT_FT_PSK;
902
0
#endif /* CONFIG_IEEE80211R_AP */
903
0
    else if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
904
0
      selector = RSN_AUTH_KEY_MGMT_802_1X_SHA256;
905
0
    else if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
906
0
      selector = RSN_AUTH_KEY_MGMT_PSK_SHA256;
907
#ifdef CONFIG_SAE
908
    else if (data.key_mgmt & WPA_KEY_MGMT_SAE)
909
      selector = RSN_AUTH_KEY_MGMT_SAE;
910
    else if (data.key_mgmt & WPA_KEY_MGMT_SAE_EXT_KEY)
911
      selector = RSN_AUTH_KEY_MGMT_SAE_EXT_KEY;
912
    else if (data.key_mgmt & WPA_KEY_MGMT_FT_SAE)
913
      selector = RSN_AUTH_KEY_MGMT_FT_SAE;
914
    else if (data.key_mgmt & WPA_KEY_MGMT_FT_SAE_EXT_KEY)
915
      selector = RSN_AUTH_KEY_MGMT_FT_SAE_EXT_KEY;
916
#endif /* CONFIG_SAE */
917
0
    else if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X)
918
0
      selector = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
919
0
    else if (data.key_mgmt & WPA_KEY_MGMT_PSK)
920
0
      selector = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
921
#ifdef CONFIG_OWE
922
    else if (data.key_mgmt & WPA_KEY_MGMT_OWE)
923
      selector = RSN_AUTH_KEY_MGMT_OWE;
924
#endif /* CONFIG_OWE */
925
#ifdef CONFIG_DPP
926
    else if (data.key_mgmt & WPA_KEY_MGMT_DPP)
927
      selector = RSN_AUTH_KEY_MGMT_DPP;
928
#endif /* CONFIG_DPP */
929
#ifdef CONFIG_SHA384
930
    else if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA384)
931
      selector = RSN_AUTH_KEY_MGMT_802_1X_SHA384;
932
#endif /* CONFIG_SHA384 */
933
0
    wpa_auth->dot11RSNAAuthenticationSuiteSelected = selector;
934
935
0
    selector = wpa_cipher_to_suite(WPA_PROTO_RSN,
936
0
                 data.pairwise_cipher);
937
0
    if (!selector)
938
0
      selector = RSN_CIPHER_SUITE_CCMP;
939
0
    wpa_auth->dot11RSNAPairwiseCipherSelected = selector;
940
941
0
    selector = wpa_cipher_to_suite(WPA_PROTO_RSN,
942
0
                 data.group_cipher);
943
0
    if (!selector)
944
0
      selector = RSN_CIPHER_SUITE_CCMP;
945
0
    wpa_auth->dot11RSNAGroupCipherSelected = selector;
946
0
  } else {
947
0
    res = wpa_parse_wpa_ie_wpa(wpa_ie, wpa_ie_len, &data);
948
949
0
    selector = WPA_AUTH_KEY_MGMT_UNSPEC_802_1X;
950
0
    if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X)
951
0
      selector = WPA_AUTH_KEY_MGMT_UNSPEC_802_1X;
952
0
    else if (data.key_mgmt & WPA_KEY_MGMT_PSK)
953
0
      selector = WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X;
954
0
    wpa_auth->dot11RSNAAuthenticationSuiteSelected = selector;
955
956
0
    selector = wpa_cipher_to_suite(WPA_PROTO_WPA,
957
0
                 data.pairwise_cipher);
958
0
    if (!selector)
959
0
      selector = RSN_CIPHER_SUITE_TKIP;
960
0
    wpa_auth->dot11RSNAPairwiseCipherSelected = selector;
961
962
0
    selector = wpa_cipher_to_suite(WPA_PROTO_WPA,
963
0
                 data.group_cipher);
964
0
    if (!selector)
965
0
      selector = WPA_CIPHER_SUITE_TKIP;
966
0
    wpa_auth->dot11RSNAGroupCipherSelected = selector;
967
0
  }
968
0
  if (res) {
969
0
    wpa_printf(MSG_DEBUG, "Failed to parse WPA/RSN IE from "
970
0
         MACSTR " (res=%d)", MAC2STR(sm->addr), res);
971
0
    wpa_hexdump(MSG_DEBUG, "WPA/RSN IE", wpa_ie, wpa_ie_len);
972
0
    return WPA_INVALID_IE;
973
0
  }
974
975
0
  if (data.group_cipher != wpa_auth->conf.wpa_group) {
976
0
    wpa_printf(MSG_DEBUG, "Invalid WPA group cipher (0x%x) from "
977
0
         MACSTR, data.group_cipher, MAC2STR(sm->addr));
978
0
    return WPA_INVALID_GROUP;
979
0
  }
980
981
0
  if (sm->rsn_override_2)
982
0
    key_mgmt = data.key_mgmt &
983
0
      wpa_auth->conf.rsn_override_key_mgmt_2;
984
0
  else if (sm->rsn_override)
985
0
    key_mgmt = data.key_mgmt & wpa_auth->conf.rsn_override_key_mgmt;
986
0
  else
987
0
    key_mgmt = data.key_mgmt & wpa_auth->conf.wpa_key_mgmt;
988
0
  if (!key_mgmt) {
989
0
    wpa_printf(MSG_DEBUG, "Invalid WPA key mgmt (0x%x) from "
990
0
         MACSTR, data.key_mgmt, MAC2STR(sm->addr));
991
0
    return WPA_INVALID_AKMP;
992
0
  }
993
0
  if (0) {
994
0
  }
995
0
  else if (key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
996
0
    sm->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X_SUITE_B_192;
997
0
  else if (key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B)
998
0
    sm->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X_SUITE_B;
999
#ifdef CONFIG_FILS
1000
#ifdef CONFIG_IEEE80211R_AP
1001
  else if (key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384)
1002
    sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_FILS_SHA384;
1003
  else if (data.key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256)
1004
    sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_FILS_SHA256;
1005
#endif /* CONFIG_IEEE80211R_AP */
1006
  else if (key_mgmt & WPA_KEY_MGMT_FILS_SHA384)
1007
    sm->wpa_key_mgmt = WPA_KEY_MGMT_FILS_SHA384;
1008
  else if (key_mgmt & WPA_KEY_MGMT_FILS_SHA256)
1009
    sm->wpa_key_mgmt = WPA_KEY_MGMT_FILS_SHA256;
1010
#endif /* CONFIG_FILS */
1011
0
#ifdef CONFIG_IEEE80211R_AP
1012
#ifdef CONFIG_SHA384
1013
  else if (key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X_SHA384)
1014
    sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X_SHA384;
1015
#endif /* CONFIG_SHA384 */
1016
0
  else if (key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
1017
0
    sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
1018
0
  else if (key_mgmt & WPA_KEY_MGMT_FT_PSK)
1019
0
    sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_PSK;
1020
0
#endif /* CONFIG_IEEE80211R_AP */
1021
#ifdef CONFIG_SHA384
1022
  else if (key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA384)
1023
    sm->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA384;
1024
#endif /* CONFIG_SHA384 */
1025
0
  else if (key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
1026
0
    sm->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
1027
0
  else if (key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
1028
0
    sm->wpa_key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
1029
#ifdef CONFIG_SAE
1030
  else if (key_mgmt & WPA_KEY_MGMT_SAE)
1031
    sm->wpa_key_mgmt = WPA_KEY_MGMT_SAE;
1032
  else if (key_mgmt & WPA_KEY_MGMT_SAE_EXT_KEY)
1033
    sm->wpa_key_mgmt = WPA_KEY_MGMT_SAE_EXT_KEY;
1034
  else if (key_mgmt & WPA_KEY_MGMT_FT_SAE)
1035
    sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_SAE;
1036
  else if (key_mgmt & WPA_KEY_MGMT_FT_SAE_EXT_KEY)
1037
    sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_SAE_EXT_KEY;
1038
#endif /* CONFIG_SAE */
1039
0
  else if (key_mgmt & WPA_KEY_MGMT_IEEE8021X)
1040
0
    sm->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X;
1041
#ifdef CONFIG_OWE
1042
  else if (key_mgmt & WPA_KEY_MGMT_OWE)
1043
    sm->wpa_key_mgmt = WPA_KEY_MGMT_OWE;
1044
#endif /* CONFIG_OWE */
1045
#ifdef CONFIG_DPP
1046
  else if (key_mgmt & WPA_KEY_MGMT_DPP)
1047
    sm->wpa_key_mgmt = WPA_KEY_MGMT_DPP;
1048
#endif /* CONFIG_DPP */
1049
0
  else
1050
0
    sm->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
1051
1052
0
  if (version == WPA_PROTO_RSN && sm->rsn_override_2)
1053
0
    ciphers = data.pairwise_cipher &
1054
0
      wpa_auth->conf.rsn_override_pairwise_2;
1055
0
  else if (version == WPA_PROTO_RSN && sm->rsn_override)
1056
0
    ciphers = data.pairwise_cipher &
1057
0
      wpa_auth->conf.rsn_override_pairwise;
1058
0
  else if (version == WPA_PROTO_RSN)
1059
0
    ciphers = data.pairwise_cipher & wpa_auth->conf.rsn_pairwise;
1060
0
  else
1061
0
    ciphers = data.pairwise_cipher & wpa_auth->conf.wpa_pairwise;
1062
0
  if (!ciphers) {
1063
0
    wpa_printf(MSG_DEBUG, "Invalid %s pairwise cipher (0x%x) "
1064
0
         "from " MACSTR,
1065
0
         version == WPA_PROTO_RSN ? "RSN" : "WPA",
1066
0
         data.pairwise_cipher, MAC2STR(sm->addr));
1067
0
    return WPA_INVALID_PAIRWISE;
1068
0
  }
1069
1070
0
  if (wpa_auth->conf.ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
1071
0
    if (!(data.capabilities & WPA_CAPABILITY_MFPC)) {
1072
0
      wpa_printf(MSG_DEBUG, "Management frame protection "
1073
0
           "required, but client did not enable it");
1074
0
      return WPA_MGMT_FRAME_PROTECTION_VIOLATION;
1075
0
    }
1076
1077
0
    if (data.mgmt_group_cipher != wpa_auth->conf.group_mgmt_cipher)
1078
0
    {
1079
0
      wpa_printf(MSG_DEBUG, "Unsupported management group "
1080
0
           "cipher %d", data.mgmt_group_cipher);
1081
0
      return WPA_INVALID_MGMT_GROUP_CIPHER;
1082
0
    }
1083
0
  }
1084
1085
#ifdef CONFIG_SAE
1086
  if (wpa_auth->conf.ieee80211w == MGMT_FRAME_PROTECTION_OPTIONAL &&
1087
      wpa_auth->conf.sae_require_mfp &&
1088
      wpa_key_mgmt_sae(sm->wpa_key_mgmt) &&
1089
      !(data.capabilities & WPA_CAPABILITY_MFPC)) {
1090
    wpa_printf(MSG_DEBUG,
1091
         "Management frame protection required with SAE, but client did not enable it");
1092
    return WPA_MGMT_FRAME_PROTECTION_VIOLATION;
1093
  }
1094
#endif /* CONFIG_SAE */
1095
1096
#ifdef CONFIG_OCV
1097
  if (wpa_auth->conf.ocv && (data.capabilities & WPA_CAPABILITY_OCVC) &&
1098
      !(data.capabilities & WPA_CAPABILITY_MFPC)) {
1099
    /* Some legacy MFP incapable STAs wrongly copy OCVC bit from
1100
     * AP RSN capabilities. To improve interoperability with such
1101
     * legacy STAs allow connection without enabling OCV when the
1102
     * workaround mode (ocv=2) is enabled.
1103
     */
1104
    if (wpa_auth->conf.ocv == 2) {
1105
      wpa_printf(MSG_DEBUG,
1106
           "Allow connecting MFP incapable and OCV capable STA without enabling OCV");
1107
      wpa_auth_set_ocv(sm, 0);
1108
    } else {
1109
      wpa_printf(MSG_DEBUG,
1110
           "Management frame protection required with OCV, but client did not enable it");
1111
      return WPA_MGMT_FRAME_PROTECTION_VIOLATION;
1112
    }
1113
  } else {
1114
    wpa_auth_set_ocv(sm, (data.capabilities & WPA_CAPABILITY_OCVC) ?
1115
         wpa_auth->conf.ocv : 0);
1116
  }
1117
#endif /* CONFIG_OCV */
1118
0
  if (sm->rsn_override_2)
1119
0
    ap_pmf_enabled = conf->rsn_override_mfp_2 !=
1120
0
      NO_MGMT_FRAME_PROTECTION;
1121
0
  else if (sm->rsn_override)
1122
0
    ap_pmf_enabled = conf->rsn_override_mfp !=
1123
0
      NO_MGMT_FRAME_PROTECTION;
1124
0
  else
1125
0
    ap_pmf_enabled = conf->ieee80211w != NO_MGMT_FRAME_PROTECTION;
1126
1127
0
  if (!ap_pmf_enabled ||
1128
0
      !(data.capabilities & WPA_CAPABILITY_MFPC))
1129
0
    sm->mgmt_frame_prot = 0;
1130
0
  else
1131
0
    sm->mgmt_frame_prot = 1;
1132
0
  sm->mfpr = !!(data.capabilities & WPA_CAPABILITY_MFPR);
1133
1134
0
  if (sm->mgmt_frame_prot && (ciphers & WPA_CIPHER_TKIP)) {
1135
0
        wpa_printf(MSG_DEBUG,
1136
0
             "Management frame protection cannot use TKIP");
1137
0
        return WPA_MGMT_FRAME_PROTECTION_VIOLATION;
1138
0
  }
1139
1140
0
  if (wpa_auth->conf.spp_amsdu &&
1141
0
      ieee802_11_rsnx_capab(rsnxe, WLAN_RSNX_CAPAB_SPP_A_MSDU) &&
1142
0
      (ciphers & (WPA_CIPHER_CCMP_256 | WPA_CIPHER_CCMP |
1143
0
      WPA_CIPHER_GCMP_256 | WPA_CIPHER_GCMP)))
1144
0
    sm->spp_amsdu = 1;
1145
0
  else
1146
0
    sm->spp_amsdu = 0;
1147
1148
0
#ifdef CONFIG_IEEE80211R_AP
1149
0
  if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
1150
0
    if (mdie == NULL || mdie_len < MOBILITY_DOMAIN_ID_LEN + 1) {
1151
0
      wpa_printf(MSG_DEBUG, "RSN: Trying to use FT, but "
1152
0
           "MDIE not included");
1153
0
      return WPA_INVALID_MDIE;
1154
0
    }
1155
0
    if (os_memcmp(mdie, wpa_auth->conf.mobility_domain,
1156
0
            MOBILITY_DOMAIN_ID_LEN) != 0) {
1157
0
      wpa_hexdump(MSG_DEBUG, "RSN: Attempted to use unknown "
1158
0
            "MDIE", mdie, MOBILITY_DOMAIN_ID_LEN);
1159
0
      return WPA_INVALID_MDIE;
1160
0
    }
1161
0
  } else if (mdie != NULL) {
1162
0
    wpa_printf(MSG_DEBUG,
1163
0
         "RSN: Trying to use non-FT AKM suite, but MDIE included");
1164
0
    return WPA_INVALID_AKMP;
1165
0
  }
1166
0
#endif /* CONFIG_IEEE80211R_AP */
1167
1168
#ifdef CONFIG_OWE
1169
  if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE && !owe_dh) {
1170
    wpa_printf(MSG_DEBUG,
1171
         "OWE: No Diffie-Hellman Parameter element");
1172
    return WPA_INVALID_AKMP;
1173
  }
1174
#endif /* CONFIG_OWE */
1175
1176
#ifdef CONFIG_DPP2
1177
  if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP &&
1178
      ((conf->dpp_pfs == 1 && !owe_dh) ||
1179
       (conf->dpp_pfs == 2 && owe_dh))) {
1180
    wpa_printf(MSG_DEBUG, "DPP: PFS %s",
1181
         conf->dpp_pfs == 1 ? "required" : "not allowed");
1182
    return WPA_DENIED_OTHER_REASON;
1183
  }
1184
#endif /* CONFIG_DPP2 */
1185
1186
0
  sm->pairwise = wpa_pick_pairwise_cipher(ciphers, 0);
1187
0
  if (sm->pairwise < 0)
1188
0
    return WPA_INVALID_PAIRWISE;
1189
1190
  /* TODO: clear WPA/WPA2 state if STA changes from one to another */
1191
0
  if (wpa_ie[0] == WLAN_EID_RSN)
1192
0
    sm->wpa = WPA_VERSION_WPA2;
1193
0
  else
1194
0
    sm->wpa = WPA_VERSION_WPA;
1195
1196
0
  if (assoc_sm) {
1197
    /* For ML association link STA cannot choose a different
1198
     * AKM or pairwise cipher from association STA */
1199
0
    if (sm->wpa_key_mgmt != assoc_sm->wpa_key_mgmt)
1200
0
      return WPA_INVALID_AKMP;
1201
0
    if (sm->pairwise != assoc_sm->pairwise)
1202
0
      return WPA_INVALID_PAIRWISE;
1203
0
  }
1204
1205
#if defined(CONFIG_IEEE80211R_AP) && defined(CONFIG_FILS)
1206
  if ((sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA256 ||
1207
       sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA384) &&
1208
      (sm->auth_alg == WLAN_AUTH_FILS_SK ||
1209
       sm->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
1210
       sm->auth_alg == WLAN_AUTH_FILS_PK) &&
1211
      (data.num_pmkid != 1 || !data.pmkid || !sm->pmk_r1_name_valid ||
1212
       os_memcmp_const(data.pmkid, sm->pmk_r1_name,
1213
           WPA_PMK_NAME_LEN) != 0)) {
1214
    wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1215
         "No PMKR1Name match for FILS+FT");
1216
    return WPA_INVALID_PMKID;
1217
  }
1218
#endif /* CONFIG_IEEE80211R_AP && CONFIG_FILS */
1219
1220
0
  sm->pmksa = NULL;
1221
0
  for (i = 0; i < data.num_pmkid; i++) {
1222
0
    struct rsn_pmksa_cache *pmksa = wpa_auth->pmksa;
1223
1224
0
    wpa_hexdump(MSG_DEBUG, "RSN IE: STA PMKID",
1225
0
          &data.pmkid[i * PMKID_LEN], PMKID_LEN);
1226
#ifdef CONFIG_IEEE80211BE
1227
    if (is_ml)
1228
      pmksa = wpa_auth->ml_pmksa;
1229
#endif /* CONFIG_IEEE80211BE */
1230
0
    sm->pmksa = pmksa_cache_auth_get(pmksa, sm->addr,
1231
0
             &data.pmkid[i * PMKID_LEN]);
1232
#ifdef CONFIG_IEEE80211BE
1233
    if (!sm->pmksa && !is_ml && wpa_auth->is_ml)
1234
      sm->pmksa = pmksa_cache_auth_get(
1235
        wpa_auth->ml_pmksa, sm->addr,
1236
        &data.pmkid[i * PMKID_LEN]);
1237
    if (!sm->pmksa && is_ml) {
1238
      struct wpa_auth_link_iter_data idata;
1239
1240
      idata.wpa_auth = wpa_auth;
1241
      idata.pmksa = NULL;
1242
      idata.spa = sm->addr;
1243
      idata.pmkid = &data.pmkid[i * PMKID_LEN];
1244
      wpa_auth_for_each_auth(wpa_auth,
1245
                 wpa_auth_pmksa_iter,
1246
                 &idata);
1247
      if (idata.pmksa)
1248
        sm->pmksa = idata.pmksa;
1249
    }
1250
#endif /* CONFIG_IEEE80211BE */
1251
0
    if (!sm->pmksa && !is_zero_ether_addr(sm->p2p_dev_addr))
1252
0
      sm->pmksa = pmksa_cache_auth_get(
1253
0
        wpa_auth->pmksa, sm->p2p_dev_addr,
1254
0
        &data.pmkid[i * PMKID_LEN]);
1255
0
    if (sm->pmksa) {
1256
0
      pmkid = sm->pmksa->pmkid;
1257
0
      break;
1258
0
    }
1259
0
  }
1260
0
  for (i = 0; sm->pmksa == NULL && wpa_auth->conf.okc &&
1261
0
         i < data.num_pmkid; i++) {
1262
0
    struct wpa_auth_okc_iter_data idata;
1263
0
    idata.pmksa = NULL;
1264
0
    idata.aa = wpa_auth->addr;
1265
0
    idata.spa = sm->addr;
1266
0
    idata.pmkid = &data.pmkid[i * PMKID_LEN];
1267
0
    wpa_auth_for_each_auth(wpa_auth, wpa_auth_okc_iter, &idata);
1268
0
    if (idata.pmksa) {
1269
0
      wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1270
0
           "OKC match for PMKID");
1271
0
      sm->pmksa = pmksa_cache_add_okc(wpa_auth->pmksa,
1272
0
              idata.pmksa,
1273
0
              wpa_auth->addr,
1274
0
              idata.pmkid);
1275
0
      pmkid = idata.pmkid;
1276
0
      break;
1277
0
    }
1278
0
  }
1279
0
  if (sm->pmksa && pmkid) {
1280
0
    struct vlan_description *vlan;
1281
1282
0
    vlan = sm->pmksa->vlan_desc;
1283
0
    wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1284
0
         "PMKID found from PMKSA cache eap_type=%d vlan=%d%s",
1285
0
         sm->pmksa->eap_type_authsrv,
1286
0
         vlan ? vlan->untagged : 0,
1287
0
         (vlan && vlan->tagged[0]) ? "+" : "");
1288
0
    os_memcpy(wpa_auth->dot11RSNAPMKIDUsed, pmkid, PMKID_LEN);
1289
0
  }
1290
1291
#ifdef CONFIG_SAE
1292
  if (sm->wpa_key_mgmt == WPA_KEY_MGMT_SAE ||
1293
      sm->wpa_key_mgmt == WPA_KEY_MGMT_SAE_EXT_KEY) {
1294
    u64 drv_flags = 0;
1295
    u64 drv_flags2 = 0;
1296
    bool ap_sae_offload = false;
1297
1298
    if (wpa_auth->cb->get_drv_flags &&
1299
        wpa_auth->cb->get_drv_flags(wpa_auth->cb_ctx, &drv_flags,
1300
            &drv_flags2) == 0)
1301
      ap_sae_offload =
1302
        !!(drv_flags2 &
1303
           WPA_DRIVER_FLAGS2_SAE_OFFLOAD_AP);
1304
1305
    /* Authenticator needs to have a PMKSA corresponding to a
1306
     * PMKID (if present) included by the STA in (Re)Association
1307
     * Request frame if PMKSA caching is attempted to be used. In
1308
     * case of SAE, this follows Open System authentication. IEEE
1309
     * Std 802.11 mandates the AP to reject (re)association trying
1310
     * to use PMKSA caching for SAE authentication. While the
1311
     * PMKID (if any) in the RSNE in (Re)Association Request frame
1312
     * following SAE authentication (i.e., in the case of no PMKSA
1313
     * caching) is not really supposed to include an unknown PMKID,
1314
     * the standard does not require the AP to reject association.
1315
     * The PMKSA that was just derived using SAE authentication
1316
     * can be used regardless of which PMKID(s) are indicated in the
1317
     * (Re)Association Request frame. */
1318
    if (!ap_sae_offload && data.num_pmkid && !sm->pmksa &&
1319
        sm->auth_alg == WLAN_AUTH_OPEN) {
1320
      wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1321
           "No PMKSA cache entry found for SAE");
1322
      return WPA_INVALID_PMKID;
1323
    }
1324
  }
1325
#endif /* CONFIG_SAE */
1326
1327
#ifdef CONFIG_DPP
1328
  if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP && !sm->pmksa) {
1329
    wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1330
         "No PMKSA cache entry found for DPP");
1331
    return WPA_INVALID_PMKID;
1332
  }
1333
#endif /* CONFIG_DPP */
1334
1335
0
  if (conf->extended_key_id && sm->wpa == WPA_VERSION_WPA2 &&
1336
0
      sm->pairwise != WPA_CIPHER_TKIP &&
1337
0
      (data.capabilities & WPA_CAPABILITY_EXT_KEY_ID_FOR_UNICAST)) {
1338
0
    sm->use_ext_key_id = true;
1339
0
    if (conf->extended_key_id == 2 &&
1340
0
        !wpa_key_mgmt_ft(sm->wpa_key_mgmt) &&
1341
0
        !wpa_key_mgmt_fils(sm->wpa_key_mgmt))
1342
0
      sm->keyidx_active = 1;
1343
0
    else
1344
0
      sm->keyidx_active = 0;
1345
0
    wpa_printf(MSG_DEBUG,
1346
0
         "RSN: Extended Key ID supported (start with %d)",
1347
0
         sm->keyidx_active);
1348
0
  } else {
1349
0
    sm->use_ext_key_id = false;
1350
0
  }
1351
1352
0
  if (sm->wpa_ie == NULL || sm->wpa_ie_len < wpa_ie_len) {
1353
0
    os_free(sm->wpa_ie);
1354
0
    sm->wpa_ie = os_malloc(wpa_ie_len);
1355
0
    if (sm->wpa_ie == NULL)
1356
0
      return WPA_ALLOC_FAIL;
1357
0
  }
1358
0
  os_memcpy(sm->wpa_ie, wpa_ie, wpa_ie_len);
1359
0
  sm->wpa_ie_len = wpa_ie_len;
1360
1361
0
  if (rsnxe && rsnxe_len) {
1362
0
    if (!sm->rsnxe || sm->rsnxe_len < rsnxe_len) {
1363
0
      os_free(sm->rsnxe);
1364
0
      sm->rsnxe = os_malloc(rsnxe_len);
1365
0
      if (!sm->rsnxe)
1366
0
        return WPA_ALLOC_FAIL;
1367
0
    }
1368
0
    os_memcpy(sm->rsnxe, rsnxe, rsnxe_len);
1369
0
    sm->rsnxe_len = rsnxe_len;
1370
0
  } else {
1371
0
    os_free(sm->rsnxe);
1372
0
    sm->rsnxe = NULL;
1373
0
    sm->rsnxe_len = 0;
1374
0
  }
1375
1376
0
  return WPA_IE_OK;
1377
0
}
1378
1379
1380
int wpa_auth_uses_mfp(struct wpa_state_machine *sm)
1381
0
{
1382
0
  return sm ? sm->mgmt_frame_prot : 0;
1383
0
}
1384
1385
1386
int wpa_auth_uses_spp_amsdu(struct wpa_state_machine *sm)
1387
0
{
1388
0
  return sm ? sm->spp_amsdu : 0;
1389
0
}
1390
1391
#ifdef CONFIG_OCV
1392
1393
void wpa_auth_set_ocv(struct wpa_state_machine *sm, int ocv)
1394
{
1395
  if (sm)
1396
    sm->ocv_enabled = ocv;
1397
}
1398
1399
1400
int wpa_auth_uses_ocv(struct wpa_state_machine *sm)
1401
{
1402
  return sm ? sm->ocv_enabled : 0;
1403
}
1404
1405
#endif /* CONFIG_OCV */
1406
1407
1408
#ifdef CONFIG_OWE
1409
u8 * wpa_auth_write_assoc_resp_owe(struct wpa_state_machine *sm,
1410
           u8 *pos, size_t max_len,
1411
           const u8 *req_ies, size_t req_ies_len)
1412
{
1413
  int res;
1414
  struct wpa_auth_config *conf;
1415
1416
  if (!sm)
1417
    return pos;
1418
  conf = &sm->wpa_auth->conf;
1419
1420
#ifdef CONFIG_TESTING_OPTIONS
1421
  if (conf->own_ie_override_len) {
1422
    if (max_len < conf->own_ie_override_len)
1423
      return NULL;
1424
    wpa_hexdump(MSG_DEBUG, "WPA: Forced own IE(s) for testing",
1425
          conf->own_ie_override, conf->own_ie_override_len);
1426
    os_memcpy(pos, conf->own_ie_override,
1427
        conf->own_ie_override_len);
1428
    return pos + conf->own_ie_override_len;
1429
  }
1430
#endif /* CONFIG_TESTING_OPTIONS */
1431
1432
  res = wpa_write_rsn_ie(conf, pos, max_len,
1433
             sm->pmksa ? sm->pmksa->pmkid : NULL);
1434
  if (res < 0)
1435
    return pos;
1436
  return pos + res;
1437
}
1438
#endif /* CONFIG_OWE */
1439
1440
1441
#ifdef CONFIG_FILS
1442
1443
u8 * wpa_auth_write_assoc_resp_fils(struct wpa_state_machine *sm,
1444
            u8 *pos, size_t max_len,
1445
            const u8 *req_ies, size_t req_ies_len)
1446
{
1447
  int res;
1448
1449
  if (!sm ||
1450
      sm->wpa_key_mgmt & (WPA_KEY_MGMT_FT_FILS_SHA256 |
1451
        WPA_KEY_MGMT_FT_FILS_SHA384))
1452
    return pos;
1453
1454
  res = wpa_write_rsn_ie(&sm->wpa_auth->conf, pos, max_len, NULL);
1455
  if (res < 0)
1456
    return pos;
1457
  return pos + res;
1458
}
1459
1460
1461
bool wpa_auth_write_fd_rsn_info(struct wpa_authenticator *wpa_auth,
1462
        u8 *fd_rsn_info)
1463
{
1464
  struct wpa_auth_config *conf;
1465
  u32 selectors = 0;
1466
  u8 *pos = fd_rsn_info;
1467
  int i, res;
1468
  u32 cipher, suite, selector, mask;
1469
  u8 tmp[10 * RSN_SELECTOR_LEN];
1470
1471
  if (!wpa_auth)
1472
    return false;
1473
  conf = &wpa_auth->conf;
1474
1475
  if (!(conf->wpa & WPA_PROTO_RSN))
1476
    return false;
1477
1478
  /* RSN Capability (B0..B15) */
1479
  WPA_PUT_LE16(pos, wpa_own_rsn_capab(conf, conf->ieee80211w));
1480
  pos += 2;
1481
1482
  /* Group Data Cipher Suite Selector (B16..B21) */
1483
  suite = wpa_cipher_to_suite(WPA_PROTO_RSN, conf->wpa_group);
1484
  if (suite == RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED)
1485
    cipher = 63; /* No cipher suite selected */
1486
  else if ((suite >> 8) == 0x000fac && ((suite & 0xff) <= 13))
1487
    cipher = suite & 0xff;
1488
  else
1489
    cipher = 62; /* vendor specific */
1490
  selectors |= cipher;
1491
1492
  /* Group Management Cipher Suite Selector (B22..B27) */
1493
  cipher = 63; /* Default to no cipher suite selected */
1494
  if (conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
1495
    switch (conf->group_mgmt_cipher) {
1496
    case WPA_CIPHER_AES_128_CMAC:
1497
      cipher = RSN_CIPHER_SUITE_AES_128_CMAC & 0xff;
1498
      break;
1499
    case WPA_CIPHER_BIP_GMAC_128:
1500
      cipher = RSN_CIPHER_SUITE_BIP_GMAC_128 & 0xff;
1501
      break;
1502
    case WPA_CIPHER_BIP_GMAC_256:
1503
      cipher = RSN_CIPHER_SUITE_BIP_GMAC_256 & 0xff;
1504
      break;
1505
    case WPA_CIPHER_BIP_CMAC_256:
1506
      cipher = RSN_CIPHER_SUITE_BIP_CMAC_256 & 0xff;
1507
      break;
1508
    }
1509
  }
1510
  selectors |= cipher << 6;
1511
1512
  /* Pairwise Cipher Suite Selector (B28..B33) */
1513
  cipher = 63; /* Default to no cipher suite selected */
1514
  res = rsn_cipher_put_suites(tmp, conf->rsn_pairwise);
1515
  if (res == 1 && tmp[0] == 0x00 && tmp[1] == 0x0f && tmp[2] == 0xac &&
1516
      tmp[3] <= 13)
1517
    cipher = tmp[3];
1518
  selectors |= cipher << 12;
1519
1520
  /* AKM Suite Selector (B34..B39) */
1521
  selector = 0; /* default to AKM from RSNE in Beacon/Probe Response */
1522
  mask = WPA_KEY_MGMT_FILS_SHA256 | WPA_KEY_MGMT_FILS_SHA384 |
1523
    WPA_KEY_MGMT_FT_FILS_SHA384;
1524
  if ((conf->wpa_key_mgmt & mask) && (conf->wpa_key_mgmt & ~mask) == 0) {
1525
    suite = conf->wpa_key_mgmt & mask;
1526
    if (suite == WPA_KEY_MGMT_FILS_SHA256)
1527
      selector = 1; /* 00-0f-ac:14 */
1528
    else if (suite == WPA_KEY_MGMT_FILS_SHA384)
1529
      selector = 2; /* 00-0f-ac:15 */
1530
    else if (suite == (WPA_KEY_MGMT_FILS_SHA256 |
1531
           WPA_KEY_MGMT_FILS_SHA384))
1532
      selector = 3; /* 00-0f-ac:14 or 00-0f-ac:15 */
1533
    else if (suite == WPA_KEY_MGMT_FT_FILS_SHA384)
1534
      selector = 4; /* 00-0f-ac:17 */
1535
  }
1536
  selectors |= selector << 18;
1537
1538
  for (i = 0; i < 3; i++) {
1539
    *pos++ = selectors & 0xff;
1540
    selectors >>= 8;
1541
  }
1542
1543
  return true;
1544
}
1545
1546
#endif /* CONFIG_FILS */