Coverage Report

Created: 2025-08-28 07:04

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