Coverage Report

Created: 2023-11-19 06:30

/src/hostap/src/common/wpa_common.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * WPA/RSN - Shared functions for supplicant and authenticator
3
 * Copyright (c) 2002-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 "includes.h"
10
11
#include "common.h"
12
#include "crypto/md5.h"
13
#include "crypto/sha1.h"
14
#include "crypto/sha256.h"
15
#include "crypto/sha384.h"
16
#include "crypto/sha512.h"
17
#include "crypto/aes_wrap.h"
18
#include "crypto/crypto.h"
19
#include "ieee802_11_defs.h"
20
#include "ieee802_11_common.h"
21
#include "defs.h"
22
#include "wpa_common.h"
23
24
25
static unsigned int wpa_kck_len(int akmp, size_t pmk_len)
26
10.8k
{
27
10.8k
  switch (akmp) {
28
0
  case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
29
0
  case WPA_KEY_MGMT_IEEE8021X_SHA384:
30
0
  case WPA_KEY_MGMT_FT_IEEE8021X_SHA384:
31
0
    return 24;
32
0
  case WPA_KEY_MGMT_FILS_SHA256:
33
0
  case WPA_KEY_MGMT_FT_FILS_SHA256:
34
0
  case WPA_KEY_MGMT_FILS_SHA384:
35
0
  case WPA_KEY_MGMT_FT_FILS_SHA384:
36
0
    return 0;
37
0
  case WPA_KEY_MGMT_DPP:
38
0
    return pmk_len / 2;
39
0
  case WPA_KEY_MGMT_OWE:
40
0
    return pmk_len / 2;
41
0
  case WPA_KEY_MGMT_SAE_EXT_KEY:
42
0
  case WPA_KEY_MGMT_FT_SAE_EXT_KEY:
43
0
    return pmk_len / 2;
44
10.8k
  default:
45
10.8k
    return 16;
46
10.8k
  }
47
10.8k
}
48
49
50
#ifdef CONFIG_IEEE80211R
51
static unsigned int wpa_kck2_len(int akmp)
52
0
{
53
0
  switch (akmp) {
54
0
  case WPA_KEY_MGMT_FT_FILS_SHA256:
55
0
    return 16;
56
0
  case WPA_KEY_MGMT_FT_FILS_SHA384:
57
0
    return 24;
58
0
  default:
59
0
    return 0;
60
0
  }
61
0
}
62
#endif /* CONFIG_IEEE80211R */
63
64
65
static unsigned int wpa_kek_len(int akmp, size_t pmk_len)
66
10.8k
{
67
10.8k
  switch (akmp) {
68
0
  case WPA_KEY_MGMT_FILS_SHA384:
69
0
  case WPA_KEY_MGMT_FT_FILS_SHA384:
70
0
    return 64;
71
0
  case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
72
0
  case WPA_KEY_MGMT_FILS_SHA256:
73
0
  case WPA_KEY_MGMT_FT_FILS_SHA256:
74
0
  case WPA_KEY_MGMT_FT_IEEE8021X_SHA384:
75
0
  case WPA_KEY_MGMT_IEEE8021X_SHA384:
76
0
    return 32;
77
0
  case WPA_KEY_MGMT_DPP:
78
0
    return pmk_len <= 32 ? 16 : 32;
79
0
  case WPA_KEY_MGMT_OWE:
80
0
    return pmk_len <= 32 ? 16 : 32;
81
0
  case WPA_KEY_MGMT_SAE_EXT_KEY:
82
0
  case WPA_KEY_MGMT_FT_SAE_EXT_KEY:
83
0
    return pmk_len <= 32 ? 16 : 32;
84
10.8k
  default:
85
10.8k
    return 16;
86
10.8k
  }
87
10.8k
}
88
89
90
#ifdef CONFIG_IEEE80211R
91
static unsigned int wpa_kek2_len(int akmp)
92
0
{
93
0
  switch (akmp) {
94
0
  case WPA_KEY_MGMT_FT_FILS_SHA256:
95
0
    return 16;
96
0
  case WPA_KEY_MGMT_FT_FILS_SHA384:
97
0
    return 32;
98
0
  default:
99
0
    return 0;
100
0
  }
101
0
}
102
#endif /* CONFIG_IEEE80211R */
103
104
105
unsigned int wpa_mic_len(int akmp, size_t pmk_len)
106
6.00M
{
107
6.00M
  switch (akmp) {
108
0
  case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
109
0
  case WPA_KEY_MGMT_FT_IEEE8021X_SHA384:
110
0
  case WPA_KEY_MGMT_IEEE8021X_SHA384:
111
0
    return 24;
112
0
  case WPA_KEY_MGMT_FILS_SHA256:
113
0
  case WPA_KEY_MGMT_FILS_SHA384:
114
0
  case WPA_KEY_MGMT_FT_FILS_SHA256:
115
0
  case WPA_KEY_MGMT_FT_FILS_SHA384:
116
0
    return 0;
117
0
  case WPA_KEY_MGMT_DPP:
118
0
    return pmk_len / 2;
119
0
  case WPA_KEY_MGMT_OWE:
120
0
    return pmk_len / 2;
121
0
  case WPA_KEY_MGMT_SAE_EXT_KEY:
122
0
  case WPA_KEY_MGMT_FT_SAE_EXT_KEY:
123
0
    return pmk_len / 2;
124
6.00M
  default:
125
6.00M
    return 16;
126
6.00M
  }
127
6.00M
}
128
129
130
/**
131
 * wpa_use_akm_defined - Is AKM-defined Key Descriptor Version used
132
 * @akmp: WPA_KEY_MGMT_* used in key derivation
133
 * Returns: 1 if AKM-defined Key Descriptor Version is used; 0 otherwise
134
 */
135
int wpa_use_akm_defined(int akmp)
136
72.7k
{
137
72.7k
  return akmp == WPA_KEY_MGMT_OSEN ||
138
72.7k
    akmp == WPA_KEY_MGMT_OWE ||
139
72.7k
    akmp == WPA_KEY_MGMT_DPP ||
140
72.7k
    akmp == WPA_KEY_MGMT_FT_IEEE8021X_SHA384 ||
141
72.7k
    akmp == WPA_KEY_MGMT_IEEE8021X_SHA384 ||
142
72.7k
    wpa_key_mgmt_sae(akmp) ||
143
72.7k
    wpa_key_mgmt_suite_b(akmp) ||
144
72.7k
    wpa_key_mgmt_fils(akmp);
145
72.7k
}
146
147
148
/**
149
 * wpa_use_cmac - Is CMAC integrity algorithm used for EAPOL-Key MIC
150
 * @akmp: WPA_KEY_MGMT_* used in key derivation
151
 * Returns: 1 if CMAC is used; 0 otherwise
152
 */
153
int wpa_use_cmac(int akmp)
154
51.6k
{
155
51.6k
  return akmp == WPA_KEY_MGMT_OSEN ||
156
51.6k
    akmp == WPA_KEY_MGMT_OWE ||
157
51.6k
    akmp == WPA_KEY_MGMT_DPP ||
158
51.6k
    wpa_key_mgmt_ft(akmp) ||
159
51.6k
    wpa_key_mgmt_sha256(akmp) ||
160
51.6k
    (wpa_key_mgmt_sae(akmp) &&
161
51.6k
     !wpa_key_mgmt_sae_ext_key(akmp)) ||
162
51.6k
    wpa_key_mgmt_suite_b(akmp);
163
51.6k
}
164
165
166
/**
167
 * wpa_use_aes_key_wrap - Is AES Keywrap algorithm used for EAPOL-Key Key Data
168
 * @akmp: WPA_KEY_MGMT_* used in key derivation
169
 * Returns: 1 if AES Keywrap is used; 0 otherwise
170
 *
171
 * Note: AKM 00-0F-AC:1 and 00-0F-AC:2 have special rules for selecting whether
172
 * to use AES Keywrap based on the negotiated pairwise cipher. This function
173
 * does not cover those special cases.
174
 */
175
int wpa_use_aes_key_wrap(int akmp)
176
0
{
177
0
  return akmp == WPA_KEY_MGMT_OSEN ||
178
0
    akmp == WPA_KEY_MGMT_OWE ||
179
0
    akmp == WPA_KEY_MGMT_DPP ||
180
0
    akmp == WPA_KEY_MGMT_IEEE8021X_SHA384 ||
181
0
    wpa_key_mgmt_ft(akmp) ||
182
0
    wpa_key_mgmt_sha256(akmp) ||
183
0
    wpa_key_mgmt_sae(akmp) ||
184
0
    wpa_key_mgmt_suite_b(akmp);
185
0
}
186
187
188
/**
189
 * wpa_eapol_key_mic - Calculate EAPOL-Key MIC
190
 * @key: EAPOL-Key Key Confirmation Key (KCK)
191
 * @key_len: KCK length in octets
192
 * @akmp: WPA_KEY_MGMT_* used in key derivation
193
 * @ver: Key descriptor version (WPA_KEY_INFO_TYPE_*)
194
 * @buf: Pointer to the beginning of the EAPOL header (version field)
195
 * @len: Length of the EAPOL frame (from EAPOL header to the end of the frame)
196
 * @mic: Pointer to the buffer to which the EAPOL-Key MIC is written
197
 * Returns: 0 on success, -1 on failure
198
 *
199
 * Calculate EAPOL-Key MIC for an EAPOL-Key packet. The EAPOL-Key MIC field has
200
 * to be cleared (all zeroes) when calling this function.
201
 *
202
 * Note: 'IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames' has an error in the
203
 * description of the Key MIC calculation. It includes packet data from the
204
 * beginning of the EAPOL-Key header, not EAPOL header. This incorrect change
205
 * happened during final editing of the standard and the correct behavior is
206
 * defined in the last draft (IEEE 802.11i/D10).
207
 */
208
int wpa_eapol_key_mic(const u8 *key, size_t key_len, int akmp, int ver,
209
          const u8 *buf, size_t len, u8 *mic)
210
{
211
  u8 hash[SHA512_MAC_LEN];
212
213
  if (key_len == 0) {
214
    wpa_printf(MSG_DEBUG,
215
         "WPA: KCK not set - cannot calculate MIC");
216
    return -1;
217
  }
218
219
  switch (ver) {
220
#ifndef CONFIG_FIPS
221
  case WPA_KEY_INFO_TYPE_HMAC_MD5_RC4:
222
    wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key MIC using HMAC-MD5");
223
    return hmac_md5(key, key_len, buf, len, mic);
224
#endif /* CONFIG_FIPS */
225
  case WPA_KEY_INFO_TYPE_HMAC_SHA1_AES:
226
    wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key MIC using HMAC-SHA1");
227
    if (hmac_sha1(key, key_len, buf, len, hash))
228
      return -1;
229
    os_memcpy(mic, hash, MD5_MAC_LEN);
230
    break;
231
  case WPA_KEY_INFO_TYPE_AES_128_CMAC:
232
    wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key MIC using AES-CMAC");
233
    return omac1_aes_128(key, buf, len, mic);
234
  case WPA_KEY_INFO_TYPE_AKM_DEFINED:
235
    switch (akmp) {
236
#ifdef CONFIG_SAE
237
    case WPA_KEY_MGMT_SAE:
238
    case WPA_KEY_MGMT_FT_SAE:
239
      wpa_printf(MSG_DEBUG,
240
           "WPA: EAPOL-Key MIC using AES-CMAC (AKM-defined - SAE)");
241
      return omac1_aes_128(key, buf, len, mic);
242
    case WPA_KEY_MGMT_SAE_EXT_KEY:
243
    case WPA_KEY_MGMT_FT_SAE_EXT_KEY:
244
      wpa_printf(MSG_DEBUG,
245
           "WPA: EAPOL-Key MIC using HMAC-SHA%u (AKM-defined - SAE-EXT-KEY)",
246
           (unsigned int) key_len * 8 * 2);
247
      if (key_len == 128 / 8) {
248
        if (hmac_sha256(key, key_len, buf, len, hash))
249
          return -1;
250
#ifdef CONFIG_SHA384
251
      } else if (key_len == 192 / 8) {
252
        if (hmac_sha384(key, key_len, buf, len, hash))
253
          return -1;
254
#endif /* CONFIG_SHA384 */
255
#ifdef CONFIG_SHA512
256
      } else if (key_len == 256 / 8) {
257
        if (hmac_sha512(key, key_len, buf, len, hash))
258
          return -1;
259
#endif /* CONFIG_SHA512 */
260
      } else {
261
        wpa_printf(MSG_INFO,
262
             "SAE: Unsupported KCK length: %u",
263
             (unsigned int) key_len);
264
        return -1;
265
      }
266
      os_memcpy(mic, hash, key_len);
267
      break;
268
#endif /* CONFIG_SAE */
269
#ifdef CONFIG_HS20
270
    case WPA_KEY_MGMT_OSEN:
271
      wpa_printf(MSG_DEBUG,
272
           "WPA: EAPOL-Key MIC using AES-CMAC (AKM-defined - OSEN)");
273
      return omac1_aes_128(key, buf, len, mic);
274
#endif /* CONFIG_HS20 */
275
#ifdef CONFIG_SUITEB
276
    case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
277
      wpa_printf(MSG_DEBUG,
278
           "WPA: EAPOL-Key MIC using HMAC-SHA256 (AKM-defined - Suite B)");
279
      if (hmac_sha256(key, key_len, buf, len, hash))
280
        return -1;
281
      os_memcpy(mic, hash, MD5_MAC_LEN);
282
      break;
283
#endif /* CONFIG_SUITEB */
284
#ifdef CONFIG_SUITEB192
285
    case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
286
      wpa_printf(MSG_DEBUG,
287
           "WPA: EAPOL-Key MIC using HMAC-SHA384 (AKM-defined - Suite B 192-bit)");
288
      if (hmac_sha384(key, key_len, buf, len, hash))
289
        return -1;
290
      os_memcpy(mic, hash, 24);
291
      break;
292
#endif /* CONFIG_SUITEB192 */
293
#ifdef CONFIG_OWE
294
    case WPA_KEY_MGMT_OWE:
295
      wpa_printf(MSG_DEBUG,
296
           "WPA: EAPOL-Key MIC using HMAC-SHA%u (AKM-defined - OWE)",
297
           (unsigned int) key_len * 8 * 2);
298
      if (key_len == 128 / 8) {
299
        if (hmac_sha256(key, key_len, buf, len, hash))
300
          return -1;
301
      } else if (key_len == 192 / 8) {
302
        if (hmac_sha384(key, key_len, buf, len, hash))
303
          return -1;
304
      } else if (key_len == 256 / 8) {
305
        if (hmac_sha512(key, key_len, buf, len, hash))
306
          return -1;
307
      } else {
308
        wpa_printf(MSG_INFO,
309
             "OWE: Unsupported KCK length: %u",
310
             (unsigned int) key_len);
311
        return -1;
312
      }
313
      os_memcpy(mic, hash, key_len);
314
      break;
315
#endif /* CONFIG_OWE */
316
#ifdef CONFIG_DPP
317
    case WPA_KEY_MGMT_DPP:
318
      wpa_printf(MSG_DEBUG,
319
           "WPA: EAPOL-Key MIC using HMAC-SHA%u (AKM-defined - DPP)",
320
           (unsigned int) key_len * 8 * 2);
321
      if (key_len == 128 / 8) {
322
        if (hmac_sha256(key, key_len, buf, len, hash))
323
          return -1;
324
      } else if (key_len == 192 / 8) {
325
        if (hmac_sha384(key, key_len, buf, len, hash))
326
          return -1;
327
      } else if (key_len == 256 / 8) {
328
        if (hmac_sha512(key, key_len, buf, len, hash))
329
          return -1;
330
      } else {
331
        wpa_printf(MSG_INFO,
332
             "DPP: Unsupported KCK length: %u",
333
             (unsigned int) key_len);
334
        return -1;
335
      }
336
      os_memcpy(mic, hash, key_len);
337
      break;
338
#endif /* CONFIG_DPP */
339
#ifdef CONFIG_SHA384
340
    case WPA_KEY_MGMT_IEEE8021X_SHA384:
341
#ifdef CONFIG_IEEE80211R
342
    case WPA_KEY_MGMT_FT_IEEE8021X_SHA384:
343
#endif /* CONFIG_IEEE80211R */
344
      wpa_printf(MSG_DEBUG,
345
           "WPA: EAPOL-Key MIC using HMAC-SHA384 (AKM-defined - 802.1X SHA384)");
346
      if (hmac_sha384(key, key_len, buf, len, hash))
347
        return -1;
348
      os_memcpy(mic, hash, 24);
349
      break;
350
#endif /* CONFIG_SHA384 */
351
    default:
352
      wpa_printf(MSG_DEBUG,
353
           "WPA: EAPOL-Key MIC algorithm not known (AKM-defined - akmp=0x%x)",
354
           akmp);
355
      return -1;
356
    }
357
    break;
358
  default:
359
    wpa_printf(MSG_DEBUG,
360
         "WPA: EAPOL-Key MIC algorithm not known (ver=%d)",
361
         ver);
362
    return -1;
363
  }
364
365
  return 0;
366
}
367
368
369
/**
370
 * wpa_pmk_to_ptk - Calculate PTK from PMK, addresses, and nonces
371
 * @pmk: Pairwise master key
372
 * @pmk_len: Length of PMK
373
 * @label: Label to use in derivation
374
 * @addr1: AA or SA
375
 * @addr2: SA or AA
376
 * @nonce1: ANonce or SNonce
377
 * @nonce2: SNonce or ANonce
378
 * @ptk: Buffer for pairwise transient key
379
 * @akmp: Negotiated AKM
380
 * @cipher: Negotiated pairwise cipher
381
 * @kdk_len: The length in octets that should be derived for KDK
382
 * Returns: 0 on success, -1 on failure
383
 *
384
 * IEEE Std 802.11i-2004 - 8.5.1.2 Pairwise key hierarchy
385
 * PTK = PRF-X(PMK, "Pairwise key expansion",
386
 *             Min(AA, SA) || Max(AA, SA) ||
387
 *             Min(ANonce, SNonce) || Max(ANonce, SNonce)
388
 *             [ || Z.x ])
389
 *
390
 * The optional Z.x component is used only with DPP and that part is not defined
391
 * in IEEE 802.11.
392
 */
393
int wpa_pmk_to_ptk(const u8 *pmk, size_t pmk_len, const char *label,
394
       const u8 *addr1, const u8 *addr2,
395
       const u8 *nonce1, const u8 *nonce2,
396
       struct wpa_ptk *ptk, int akmp, int cipher,
397
       const u8 *z, size_t z_len, size_t kdk_len)
398
{
399
#define MAX_Z_LEN 66 /* with NIST P-521 */
400
  u8 data[2 * ETH_ALEN + 2 * WPA_NONCE_LEN + MAX_Z_LEN];
401
  size_t data_len = 2 * ETH_ALEN + 2 * WPA_NONCE_LEN;
402
  u8 tmp[WPA_KCK_MAX_LEN + WPA_KEK_MAX_LEN + WPA_TK_MAX_LEN +
403
    WPA_KDK_MAX_LEN];
404
  size_t ptk_len;
405
#ifdef CONFIG_OWE
406
  int owe_ptk_workaround = 0;
407
408
  if (akmp == (WPA_KEY_MGMT_OWE | WPA_KEY_MGMT_PSK_SHA256)) {
409
    owe_ptk_workaround = 1;
410
    akmp = WPA_KEY_MGMT_OWE;
411
  }
412
#endif /* CONFIG_OWE */
413
414
  if (pmk_len == 0) {
415
    wpa_printf(MSG_ERROR, "WPA: No PMK set for PTK derivation");
416
    return -1;
417
  }
418
419
  if (z_len > MAX_Z_LEN)
420
    return -1;
421
422
  if (os_memcmp(addr1, addr2, ETH_ALEN) < 0) {
423
    os_memcpy(data, addr1, ETH_ALEN);
424
    os_memcpy(data + ETH_ALEN, addr2, ETH_ALEN);
425
  } else {
426
    os_memcpy(data, addr2, ETH_ALEN);
427
    os_memcpy(data + ETH_ALEN, addr1, ETH_ALEN);
428
  }
429
430
  if (os_memcmp(nonce1, nonce2, WPA_NONCE_LEN) < 0) {
431
    os_memcpy(data + 2 * ETH_ALEN, nonce1, WPA_NONCE_LEN);
432
    os_memcpy(data + 2 * ETH_ALEN + WPA_NONCE_LEN, nonce2,
433
        WPA_NONCE_LEN);
434
  } else {
435
    os_memcpy(data + 2 * ETH_ALEN, nonce2, WPA_NONCE_LEN);
436
    os_memcpy(data + 2 * ETH_ALEN + WPA_NONCE_LEN, nonce1,
437
        WPA_NONCE_LEN);
438
  }
439
440
  if (z && z_len) {
441
    os_memcpy(data + 2 * ETH_ALEN + 2 * WPA_NONCE_LEN, z, z_len);
442
    data_len += z_len;
443
  }
444
445
  if (kdk_len > WPA_KDK_MAX_LEN) {
446
    wpa_printf(MSG_ERROR,
447
         "WPA: KDK len=%zu exceeds max supported len",
448
         kdk_len);
449
    return -1;
450
  }
451
452
  ptk->kck_len = wpa_kck_len(akmp, pmk_len);
453
  ptk->kek_len = wpa_kek_len(akmp, pmk_len);
454
  ptk->tk_len = wpa_cipher_key_len(cipher);
455
  ptk->kdk_len = kdk_len;
456
  if (ptk->tk_len == 0) {
457
    wpa_printf(MSG_ERROR,
458
         "WPA: Unsupported cipher (0x%x) used in PTK derivation",
459
         cipher);
460
    return -1;
461
  }
462
  ptk_len = ptk->kck_len + ptk->kek_len + ptk->tk_len + ptk->kdk_len;
463
464
  if (wpa_key_mgmt_sha384(akmp)) {
465
#ifdef CONFIG_SHA384
466
    wpa_printf(MSG_DEBUG, "WPA: PTK derivation using PRF(SHA384)");
467
    if (sha384_prf(pmk, pmk_len, label, data, data_len,
468
             tmp, ptk_len) < 0)
469
      return -1;
470
#else /* CONFIG_SHA384 */
471
    return -1;
472
#endif /* CONFIG_SHA384 */
473
  } else if (wpa_key_mgmt_sha256(akmp)) {
474
    wpa_printf(MSG_DEBUG, "WPA: PTK derivation using PRF(SHA256)");
475
    if (sha256_prf(pmk, pmk_len, label, data, data_len,
476
             tmp, ptk_len) < 0)
477
      return -1;
478
#ifdef CONFIG_OWE
479
  } else if (akmp == WPA_KEY_MGMT_OWE && (pmk_len == 32 ||
480
            owe_ptk_workaround)) {
481
    wpa_printf(MSG_DEBUG, "WPA: PTK derivation using PRF(SHA256)");
482
    if (sha256_prf(pmk, pmk_len, label, data, data_len,
483
             tmp, ptk_len) < 0)
484
      return -1;
485
  } else if (akmp == WPA_KEY_MGMT_OWE && pmk_len == 48) {
486
    wpa_printf(MSG_DEBUG, "WPA: PTK derivation using PRF(SHA384)");
487
    if (sha384_prf(pmk, pmk_len, label, data, data_len,
488
             tmp, ptk_len) < 0)
489
      return -1;
490
  } else if (akmp == WPA_KEY_MGMT_OWE && pmk_len == 64) {
491
    wpa_printf(MSG_DEBUG, "WPA: PTK derivation using PRF(SHA512)");
492
    if (sha512_prf(pmk, pmk_len, label, data, data_len,
493
             tmp, ptk_len) < 0)
494
      return -1;
495
  } else if (akmp == WPA_KEY_MGMT_OWE) {
496
    wpa_printf(MSG_INFO, "OWE: Unknown PMK length %u",
497
         (unsigned int) pmk_len);
498
    return -1;
499
#endif /* CONFIG_OWE */
500
#ifdef CONFIG_DPP
501
  } else if (akmp == WPA_KEY_MGMT_DPP && pmk_len == 32) {
502
    wpa_printf(MSG_DEBUG, "WPA: PTK derivation using PRF(SHA256)");
503
    if (sha256_prf(pmk, pmk_len, label, data, data_len,
504
             tmp, ptk_len) < 0)
505
      return -1;
506
  } else if (akmp == WPA_KEY_MGMT_DPP && pmk_len == 48) {
507
    wpa_printf(MSG_DEBUG, "WPA: PTK derivation using PRF(SHA384)");
508
    if (sha384_prf(pmk, pmk_len, label, data, data_len,
509
             tmp, ptk_len) < 0)
510
      return -1;
511
  } else if (akmp == WPA_KEY_MGMT_DPP && pmk_len == 64) {
512
    wpa_printf(MSG_DEBUG, "WPA: PTK derivation using PRF(SHA512)");
513
    if (sha512_prf(pmk, pmk_len, label, data, data_len,
514
             tmp, ptk_len) < 0)
515
      return -1;
516
  } else if (akmp == WPA_KEY_MGMT_DPP) {
517
    wpa_printf(MSG_INFO, "DPP: Unknown PMK length %u",
518
         (unsigned int) pmk_len);
519
    return -1;
520
#endif /* CONFIG_DPP */
521
#ifdef CONFIG_SAE
522
  } else if (wpa_key_mgmt_sae_ext_key(akmp)) {
523
    if (pmk_len == 32) {
524
      wpa_printf(MSG_DEBUG,
525
           "SAE: PTK derivation using PRF(SHA256)");
526
      if (sha256_prf(pmk, pmk_len, label, data, data_len,
527
               tmp, ptk_len) < 0)
528
        return -1;
529
#ifdef CONFIG_SHA384
530
    } else if (pmk_len == 48) {
531
      wpa_printf(MSG_DEBUG,
532
           "SAE: PTK derivation using PRF(SHA384)");
533
      if (sha384_prf(pmk, pmk_len, label, data, data_len,
534
               tmp, ptk_len) < 0)
535
        return -1;
536
#endif /* CONFIG_SHA384 */
537
#ifdef CONFIG_SHA512
538
    } else if (pmk_len == 64) {
539
      wpa_printf(MSG_DEBUG,
540
           "SAE: PTK derivation using PRF(SHA512)");
541
      if (sha512_prf(pmk, pmk_len, label, data, data_len,
542
               tmp, ptk_len) < 0)
543
        return -1;
544
#endif /* CONFIG_SHA512 */
545
    } else {
546
      wpa_printf(MSG_INFO, "SAE: Unknown PMK length %u",
547
           (unsigned int) pmk_len);
548
      return -1;
549
    }
550
#endif /* CONFIG_SAE */
551
  } else {
552
    wpa_printf(MSG_DEBUG, "WPA: PTK derivation using PRF(SHA1)");
553
    if (sha1_prf(pmk, pmk_len, label, data, data_len, tmp,
554
           ptk_len) < 0)
555
      return -1;
556
  }
557
558
  wpa_printf(MSG_DEBUG, "WPA: PTK derivation - A1=" MACSTR " A2=" MACSTR,
559
       MAC2STR(addr1), MAC2STR(addr2));
560
  wpa_hexdump(MSG_DEBUG, "WPA: Nonce1", nonce1, WPA_NONCE_LEN);
561
  wpa_hexdump(MSG_DEBUG, "WPA: Nonce2", nonce2, WPA_NONCE_LEN);
562
  if (z && z_len)
563
    wpa_hexdump_key(MSG_DEBUG, "WPA: Z.x", z, z_len);
564
  wpa_hexdump_key(MSG_DEBUG, "WPA: PMK", pmk, pmk_len);
565
  wpa_hexdump_key(MSG_DEBUG, "WPA: PTK", tmp, ptk_len);
566
567
  os_memcpy(ptk->kck, tmp, ptk->kck_len);
568
  wpa_hexdump_key(MSG_DEBUG, "WPA: KCK", ptk->kck, ptk->kck_len);
569
570
  os_memcpy(ptk->kek, tmp + ptk->kck_len, ptk->kek_len);
571
  wpa_hexdump_key(MSG_DEBUG, "WPA: KEK", ptk->kek, ptk->kek_len);
572
573
  os_memcpy(ptk->tk, tmp + ptk->kck_len + ptk->kek_len, ptk->tk_len);
574
  wpa_hexdump_key(MSG_DEBUG, "WPA: TK", ptk->tk, ptk->tk_len);
575
576
  if (kdk_len) {
577
    os_memcpy(ptk->kdk, tmp + ptk->kck_len + ptk->kek_len +
578
        ptk->tk_len, ptk->kdk_len);
579
    wpa_hexdump_key(MSG_DEBUG, "WPA: KDK", ptk->kdk, ptk->kdk_len);
580
  }
581
582
  ptk->kek2_len = 0;
583
  ptk->kck2_len = 0;
584
585
  os_memset(tmp, 0, sizeof(tmp));
586
  os_memset(data, 0, data_len);
587
  return 0;
588
}
589
590
#ifdef CONFIG_FILS
591
592
int fils_rmsk_to_pmk(int akmp, const u8 *rmsk, size_t rmsk_len,
593
         const u8 *snonce, const u8 *anonce, const u8 *dh_ss,
594
         size_t dh_ss_len, u8 *pmk, size_t *pmk_len)
595
0
{
596
0
  u8 nonces[2 * FILS_NONCE_LEN];
597
0
  const u8 *addr[2];
598
0
  size_t len[2];
599
0
  size_t num_elem;
600
0
  int res;
601
602
  /* PMK = HMAC-Hash(SNonce || ANonce, rMSK [ || DHss ]) */
603
0
  wpa_printf(MSG_DEBUG, "FILS: rMSK to PMK derivation");
604
605
0
  if (wpa_key_mgmt_sha384(akmp))
606
0
    *pmk_len = SHA384_MAC_LEN;
607
0
  else if (wpa_key_mgmt_sha256(akmp))
608
0
    *pmk_len = SHA256_MAC_LEN;
609
0
  else
610
0
    return -1;
611
612
0
  wpa_hexdump_key(MSG_DEBUG, "FILS: rMSK", rmsk, rmsk_len);
613
0
  wpa_hexdump(MSG_DEBUG, "FILS: SNonce", snonce, FILS_NONCE_LEN);
614
0
  wpa_hexdump(MSG_DEBUG, "FILS: ANonce", anonce, FILS_NONCE_LEN);
615
0
  wpa_hexdump(MSG_DEBUG, "FILS: DHss", dh_ss, dh_ss_len);
616
617
0
  os_memcpy(nonces, snonce, FILS_NONCE_LEN);
618
0
  os_memcpy(&nonces[FILS_NONCE_LEN], anonce, FILS_NONCE_LEN);
619
0
  addr[0] = rmsk;
620
0
  len[0] = rmsk_len;
621
0
  num_elem = 1;
622
0
  if (dh_ss) {
623
0
    addr[1] = dh_ss;
624
0
    len[1] = dh_ss_len;
625
0
    num_elem++;
626
0
  }
627
0
  if (wpa_key_mgmt_sha384(akmp))
628
0
    res = hmac_sha384_vector(nonces, 2 * FILS_NONCE_LEN, num_elem,
629
0
           addr, len, pmk);
630
0
  else
631
0
    res = hmac_sha256_vector(nonces, 2 * FILS_NONCE_LEN, num_elem,
632
0
           addr, len, pmk);
633
0
  if (res == 0)
634
0
    wpa_hexdump_key(MSG_DEBUG, "FILS: PMK", pmk, *pmk_len);
635
0
  else
636
0
    *pmk_len = 0;
637
0
  return res;
638
0
}
639
640
641
int fils_pmkid_erp(int akmp, const u8 *reauth, size_t reauth_len,
642
       u8 *pmkid)
643
0
{
644
0
  const u8 *addr[1];
645
0
  size_t len[1];
646
0
  u8 hash[SHA384_MAC_LEN];
647
0
  int res;
648
649
  /* PMKID = Truncate-128(Hash(EAP-Initiate/Reauth)) */
650
0
  addr[0] = reauth;
651
0
  len[0] = reauth_len;
652
0
  if (wpa_key_mgmt_sha384(akmp))
653
0
    res = sha384_vector(1, addr, len, hash);
654
0
  else if (wpa_key_mgmt_sha256(akmp))
655
0
    res = sha256_vector(1, addr, len, hash);
656
0
  else
657
0
    return -1;
658
0
  if (res)
659
0
    return res;
660
0
  os_memcpy(pmkid, hash, PMKID_LEN);
661
0
  wpa_hexdump(MSG_DEBUG, "FILS: PMKID", pmkid, PMKID_LEN);
662
0
  return 0;
663
0
}
664
665
666
int fils_pmk_to_ptk(const u8 *pmk, size_t pmk_len, const u8 *spa, const u8 *aa,
667
        const u8 *snonce, const u8 *anonce, const u8 *dhss,
668
        size_t dhss_len, struct wpa_ptk *ptk,
669
        u8 *ick, size_t *ick_len, int akmp, int cipher,
670
        u8 *fils_ft, size_t *fils_ft_len, size_t kdk_len)
671
0
{
672
0
  u8 *data, *pos;
673
0
  size_t data_len;
674
0
  u8 tmp[FILS_ICK_MAX_LEN + WPA_KEK_MAX_LEN + WPA_TK_MAX_LEN +
675
0
         FILS_FT_MAX_LEN + WPA_KDK_MAX_LEN];
676
0
  size_t key_data_len;
677
0
  const char *label = "FILS PTK Derivation";
678
0
  int ret = -1;
679
0
  size_t offset;
680
681
  /*
682
   * FILS-Key-Data = PRF-X(PMK, "FILS PTK Derivation",
683
   *                       SPA || AA || SNonce || ANonce [ || DHss ])
684
   * ICK = L(FILS-Key-Data, 0, ICK_bits)
685
   * KEK = L(FILS-Key-Data, ICK_bits, KEK_bits)
686
   * TK = L(FILS-Key-Data, ICK_bits + KEK_bits, TK_bits)
687
   * If doing FT initial mobility domain association:
688
   * FILS-FT = L(FILS-Key-Data, ICK_bits + KEK_bits + TK_bits,
689
   *             FILS-FT_bits)
690
   * When a KDK is derived:
691
   * KDK = L(FILS-Key-Data, ICK_bits + KEK_bits + TK_bits + FILS-FT_bits,
692
   *     KDK_bits)
693
   */
694
0
  data_len = 2 * ETH_ALEN + 2 * FILS_NONCE_LEN + dhss_len;
695
0
  data = os_malloc(data_len);
696
0
  if (!data)
697
0
    goto err;
698
0
  pos = data;
699
0
  os_memcpy(pos, spa, ETH_ALEN);
700
0
  pos += ETH_ALEN;
701
0
  os_memcpy(pos, aa, ETH_ALEN);
702
0
  pos += ETH_ALEN;
703
0
  os_memcpy(pos, snonce, FILS_NONCE_LEN);
704
0
  pos += FILS_NONCE_LEN;
705
0
  os_memcpy(pos, anonce, FILS_NONCE_LEN);
706
0
  pos += FILS_NONCE_LEN;
707
0
  if (dhss)
708
0
    os_memcpy(pos, dhss, dhss_len);
709
710
0
  ptk->kck_len = 0;
711
0
  ptk->kek_len = wpa_kek_len(akmp, pmk_len);
712
0
  ptk->tk_len = wpa_cipher_key_len(cipher);
713
0
  if (wpa_key_mgmt_sha384(akmp))
714
0
    *ick_len = 48;
715
0
  else if (wpa_key_mgmt_sha256(akmp))
716
0
    *ick_len = 32;
717
0
  else
718
0
    goto err;
719
0
  key_data_len = *ick_len + ptk->kek_len + ptk->tk_len;
720
721
0
  if (kdk_len) {
722
0
    if (kdk_len > WPA_KDK_MAX_LEN) {
723
0
      wpa_printf(MSG_ERROR, "FILS: KDK len=%zu too big",
724
0
           kdk_len);
725
0
      goto err;
726
0
    }
727
728
0
    ptk->kdk_len = kdk_len;
729
0
    key_data_len += kdk_len;
730
0
  } else {
731
0
    ptk->kdk_len = 0;
732
0
  }
733
734
0
  if (fils_ft && fils_ft_len) {
735
0
    if (akmp == WPA_KEY_MGMT_FT_FILS_SHA256) {
736
0
      *fils_ft_len = 32;
737
0
    } else if (akmp == WPA_KEY_MGMT_FT_FILS_SHA384) {
738
0
      *fils_ft_len = 48;
739
0
    } else {
740
0
      *fils_ft_len = 0;
741
0
      fils_ft = NULL;
742
0
    }
743
0
    key_data_len += *fils_ft_len;
744
0
  }
745
746
0
  if (wpa_key_mgmt_sha384(akmp)) {
747
0
    wpa_printf(MSG_DEBUG, "FILS: PTK derivation using PRF(SHA384)");
748
0
    if (sha384_prf(pmk, pmk_len, label, data, data_len,
749
0
             tmp, key_data_len) < 0)
750
0
      goto err;
751
0
  } else {
752
0
    wpa_printf(MSG_DEBUG, "FILS: PTK derivation using PRF(SHA256)");
753
0
    if (sha256_prf(pmk, pmk_len, label, data, data_len,
754
0
             tmp, key_data_len) < 0)
755
0
      goto err;
756
0
  }
757
758
0
  wpa_printf(MSG_DEBUG, "FILS: PTK derivation - SPA=" MACSTR
759
0
       " AA=" MACSTR, MAC2STR(spa), MAC2STR(aa));
760
0
  wpa_hexdump(MSG_DEBUG, "FILS: SNonce", snonce, FILS_NONCE_LEN);
761
0
  wpa_hexdump(MSG_DEBUG, "FILS: ANonce", anonce, FILS_NONCE_LEN);
762
0
  if (dhss)
763
0
    wpa_hexdump_key(MSG_DEBUG, "FILS: DHss", dhss, dhss_len);
764
0
  wpa_hexdump_key(MSG_DEBUG, "FILS: PMK", pmk, pmk_len);
765
0
  wpa_hexdump_key(MSG_DEBUG, "FILS: FILS-Key-Data", tmp, key_data_len);
766
767
0
  os_memcpy(ick, tmp, *ick_len);
768
0
  offset = *ick_len;
769
0
  wpa_hexdump_key(MSG_DEBUG, "FILS: ICK", ick, *ick_len);
770
771
0
  os_memcpy(ptk->kek, tmp + offset, ptk->kek_len);
772
0
  wpa_hexdump_key(MSG_DEBUG, "FILS: KEK", ptk->kek, ptk->kek_len);
773
0
  offset += ptk->kek_len;
774
775
0
  os_memcpy(ptk->tk, tmp + offset, ptk->tk_len);
776
0
  wpa_hexdump_key(MSG_DEBUG, "FILS: TK", ptk->tk, ptk->tk_len);
777
0
  offset += ptk->tk_len;
778
779
0
  if (fils_ft && fils_ft_len) {
780
0
    os_memcpy(fils_ft, tmp + offset, *fils_ft_len);
781
0
    wpa_hexdump_key(MSG_DEBUG, "FILS: FILS-FT",
782
0
        fils_ft, *fils_ft_len);
783
0
    offset += *fils_ft_len;
784
0
  }
785
786
0
  if (ptk->kdk_len) {
787
0
    os_memcpy(ptk->kdk, tmp + offset, ptk->kdk_len);
788
0
    wpa_hexdump_key(MSG_DEBUG, "FILS: KDK", ptk->kdk, ptk->kdk_len);
789
0
  }
790
791
0
  ptk->kek2_len = 0;
792
0
  ptk->kck2_len = 0;
793
794
0
  os_memset(tmp, 0, sizeof(tmp));
795
0
  ret = 0;
796
0
err:
797
0
  bin_clear_free(data, data_len);
798
0
  return ret;
799
0
}
800
801
802
int fils_key_auth_sk(const u8 *ick, size_t ick_len, const u8 *snonce,
803
         const u8 *anonce, const u8 *sta_addr, const u8 *bssid,
804
         const u8 *g_sta, size_t g_sta_len,
805
         const u8 *g_ap, size_t g_ap_len,
806
         int akmp, u8 *key_auth_sta, u8 *key_auth_ap,
807
         size_t *key_auth_len)
808
0
{
809
0
  const u8 *addr[6];
810
0
  size_t len[6];
811
0
  size_t num_elem = 4;
812
0
  int res;
813
814
0
  wpa_printf(MSG_DEBUG, "FILS: Key-Auth derivation: STA-MAC=" MACSTR
815
0
       " AP-BSSID=" MACSTR, MAC2STR(sta_addr), MAC2STR(bssid));
816
0
  wpa_hexdump_key(MSG_DEBUG, "FILS: ICK", ick, ick_len);
817
0
  wpa_hexdump(MSG_DEBUG, "FILS: SNonce", snonce, FILS_NONCE_LEN);
818
0
  wpa_hexdump(MSG_DEBUG, "FILS: ANonce", anonce, FILS_NONCE_LEN);
819
0
  wpa_hexdump(MSG_DEBUG, "FILS: gSTA", g_sta, g_sta_len);
820
0
  wpa_hexdump(MSG_DEBUG, "FILS: gAP", g_ap, g_ap_len);
821
822
  /*
823
   * For (Re)Association Request frame (STA->AP):
824
   * Key-Auth = HMAC-Hash(ICK, SNonce || ANonce || STA-MAC || AP-BSSID
825
   *                      [ || gSTA || gAP ])
826
   */
827
0
  addr[0] = snonce;
828
0
  len[0] = FILS_NONCE_LEN;
829
0
  addr[1] = anonce;
830
0
  len[1] = FILS_NONCE_LEN;
831
0
  addr[2] = sta_addr;
832
0
  len[2] = ETH_ALEN;
833
0
  addr[3] = bssid;
834
0
  len[3] = ETH_ALEN;
835
0
  if (g_sta && g_sta_len && g_ap && g_ap_len) {
836
0
    addr[4] = g_sta;
837
0
    len[4] = g_sta_len;
838
0
    addr[5] = g_ap;
839
0
    len[5] = g_ap_len;
840
0
    num_elem = 6;
841
0
  }
842
843
0
  if (wpa_key_mgmt_sha384(akmp)) {
844
0
    *key_auth_len = 48;
845
0
    res = hmac_sha384_vector(ick, ick_len, num_elem, addr, len,
846
0
           key_auth_sta);
847
0
  } else if (wpa_key_mgmt_sha256(akmp)) {
848
0
    *key_auth_len = 32;
849
0
    res = hmac_sha256_vector(ick, ick_len, num_elem, addr, len,
850
0
           key_auth_sta);
851
0
  } else {
852
0
    return -1;
853
0
  }
854
0
  if (res < 0)
855
0
    return res;
856
857
  /*
858
   * For (Re)Association Response frame (AP->STA):
859
   * Key-Auth = HMAC-Hash(ICK, ANonce || SNonce || AP-BSSID || STA-MAC
860
   *                      [ || gAP || gSTA ])
861
   */
862
0
  addr[0] = anonce;
863
0
  addr[1] = snonce;
864
0
  addr[2] = bssid;
865
0
  addr[3] = sta_addr;
866
0
  if (g_sta && g_sta_len && g_ap && g_ap_len) {
867
0
    addr[4] = g_ap;
868
0
    len[4] = g_ap_len;
869
0
    addr[5] = g_sta;
870
0
    len[5] = g_sta_len;
871
0
  }
872
873
0
  if (wpa_key_mgmt_sha384(akmp))
874
0
    res = hmac_sha384_vector(ick, ick_len, num_elem, addr, len,
875
0
           key_auth_ap);
876
0
  else if (wpa_key_mgmt_sha256(akmp))
877
0
    res = hmac_sha256_vector(ick, ick_len, num_elem, addr, len,
878
0
           key_auth_ap);
879
0
  if (res < 0)
880
0
    return res;
881
882
0
  wpa_hexdump(MSG_DEBUG, "FILS: Key-Auth (STA)",
883
0
        key_auth_sta, *key_auth_len);
884
0
  wpa_hexdump(MSG_DEBUG, "FILS: Key-Auth (AP)",
885
0
        key_auth_ap, *key_auth_len);
886
887
0
  return 0;
888
0
}
889
890
#endif /* CONFIG_FILS */
891
892
893
#ifdef CONFIG_IEEE80211R
894
int wpa_ft_mic(int key_mgmt, const u8 *kck, size_t kck_len, const u8 *sta_addr,
895
         const u8 *ap_addr, u8 transaction_seqnum,
896
         const u8 *mdie, size_t mdie_len,
897
         const u8 *ftie, size_t ftie_len,
898
         const u8 *rsnie, size_t rsnie_len,
899
         const u8 *ric, size_t ric_len,
900
         const u8 *rsnxe, size_t rsnxe_len,
901
         const struct wpabuf *extra,
902
         u8 *mic)
903
0
{
904
0
  const u8 *addr[11];
905
0
  size_t len[11];
906
0
  size_t i, num_elem = 0;
907
0
  u8 zero_mic[32];
908
0
  size_t mic_len, fte_fixed_len;
909
0
  int res;
910
911
0
  if (kck_len == 16) {
912
0
    mic_len = 16;
913
0
#ifdef CONFIG_SHA384
914
0
  } else if (kck_len == 24) {
915
0
    mic_len = 24;
916
0
#endif /* CONFIG_SHA384 */
917
#ifdef CONFIG_SHA512
918
  } else if (kck_len == 32) {
919
    mic_len = 32;
920
#endif /* CONFIG_SHA512 */
921
0
  } else {
922
0
    wpa_printf(MSG_WARNING, "FT: Unsupported KCK length %u",
923
0
         (unsigned int) kck_len);
924
0
    return -1;
925
0
  }
926
927
0
  fte_fixed_len = sizeof(struct rsn_ftie) - 16 + mic_len;
928
929
0
  addr[num_elem] = sta_addr;
930
0
  len[num_elem] = ETH_ALEN;
931
0
  num_elem++;
932
933
0
  addr[num_elem] = ap_addr;
934
0
  len[num_elem] = ETH_ALEN;
935
0
  num_elem++;
936
937
0
  addr[num_elem] = &transaction_seqnum;
938
0
  len[num_elem] = 1;
939
0
  num_elem++;
940
941
0
  if (rsnie) {
942
0
    addr[num_elem] = rsnie;
943
0
    len[num_elem] = rsnie_len;
944
0
    num_elem++;
945
0
  }
946
0
  if (mdie) {
947
0
    addr[num_elem] = mdie;
948
0
    len[num_elem] = mdie_len;
949
0
    num_elem++;
950
0
  }
951
0
  if (ftie) {
952
0
    if (ftie_len < 2 + fte_fixed_len)
953
0
      return -1;
954
955
    /* IE hdr and mic_control */
956
0
    addr[num_elem] = ftie;
957
0
    len[num_elem] = 2 + 2;
958
0
    num_elem++;
959
960
    /* MIC field with all zeros */
961
0
    os_memset(zero_mic, 0, mic_len);
962
0
    addr[num_elem] = zero_mic;
963
0
    len[num_elem] = mic_len;
964
0
    num_elem++;
965
966
    /* Rest of FTIE */
967
0
    addr[num_elem] = ftie + 2 + 2 + mic_len;
968
0
    len[num_elem] = ftie_len - (2 + 2 + mic_len);
969
0
    num_elem++;
970
0
  }
971
0
  if (ric) {
972
0
    addr[num_elem] = ric;
973
0
    len[num_elem] = ric_len;
974
0
    num_elem++;
975
0
  }
976
977
0
  if (rsnxe) {
978
0
    addr[num_elem] = rsnxe;
979
0
    len[num_elem] = rsnxe_len;
980
0
    num_elem++;
981
0
  }
982
983
0
  if (extra) {
984
0
    addr[num_elem] = wpabuf_head(extra);
985
0
    len[num_elem] = wpabuf_len(extra);
986
0
    num_elem++;
987
0
  }
988
989
0
  for (i = 0; i < num_elem; i++)
990
0
    wpa_hexdump(MSG_MSGDUMP, "FT: MIC data", addr[i], len[i]);
991
0
  res = -1;
992
#ifdef CONFIG_SHA512
993
  if (kck_len == 32) {
994
    u8 hash[SHA512_MAC_LEN];
995
996
    if (hmac_sha512_vector(kck, kck_len, num_elem, addr, len, hash))
997
      return -1;
998
    os_memcpy(mic, hash, 32);
999
    res = 0;
1000
  }
1001
#endif /* CONFIG_SHA384 */
1002
0
#ifdef CONFIG_SHA384
1003
0
  if (kck_len == 24) {
1004
0
    u8 hash[SHA384_MAC_LEN];
1005
1006
0
    if (hmac_sha384_vector(kck, kck_len, num_elem, addr, len, hash))
1007
0
      return -1;
1008
0
    os_memcpy(mic, hash, 24);
1009
0
    res = 0;
1010
0
  }
1011
0
#endif /* CONFIG_SHA384 */
1012
0
  if (kck_len == 16 && key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY) {
1013
0
    u8 hash[SHA256_MAC_LEN];
1014
1015
0
    if (hmac_sha256_vector(kck, kck_len, num_elem, addr, len, hash))
1016
0
      return -1;
1017
0
    os_memcpy(mic, hash, 16);
1018
0
    res = 0;
1019
0
  }
1020
0
  if (kck_len == 16 && key_mgmt != WPA_KEY_MGMT_FT_SAE_EXT_KEY &&
1021
0
      omac1_aes_128_vector(kck, num_elem, addr, len, mic) == 0)
1022
0
    res = 0;
1023
1024
0
  return res;
1025
0
}
1026
1027
1028
static int wpa_ft_parse_ftie(const u8 *ie, size_t ie_len,
1029
           struct wpa_ft_ies *parse, const u8 *opt)
1030
0
{
1031
0
  const u8 *end, *pos;
1032
0
  u8 link_id;
1033
1034
0
  parse->ftie = ie;
1035
0
  parse->ftie_len = ie_len;
1036
1037
0
  pos = opt;
1038
0
  end = ie + ie_len;
1039
0
  wpa_hexdump(MSG_DEBUG, "FT: Parse FTE subelements", pos, end - pos);
1040
1041
0
  while (end - pos >= 2) {
1042
0
    u8 id, len;
1043
1044
0
    id = *pos++;
1045
0
    len = *pos++;
1046
0
    if (len > end - pos) {
1047
0
      wpa_printf(MSG_DEBUG, "FT: Truncated subelement");
1048
0
      return -1;
1049
0
    }
1050
1051
0
    switch (id) {
1052
0
    case FTIE_SUBELEM_R1KH_ID:
1053
0
      if (len != FT_R1KH_ID_LEN) {
1054
0
        wpa_printf(MSG_DEBUG,
1055
0
             "FT: Invalid R1KH-ID length in FTIE: %d",
1056
0
             len);
1057
0
        return -1;
1058
0
      }
1059
0
      parse->r1kh_id = pos;
1060
0
      wpa_hexdump(MSG_DEBUG, "FT: R1KH-ID",
1061
0
            parse->r1kh_id, FT_R1KH_ID_LEN);
1062
0
      break;
1063
0
    case FTIE_SUBELEM_GTK:
1064
0
      wpa_printf(MSG_DEBUG, "FT: GTK");
1065
0
      parse->gtk = pos;
1066
0
      parse->gtk_len = len;
1067
0
      break;
1068
0
    case FTIE_SUBELEM_R0KH_ID:
1069
0
      if (len < 1 || len > FT_R0KH_ID_MAX_LEN) {
1070
0
        wpa_printf(MSG_DEBUG,
1071
0
             "FT: Invalid R0KH-ID length in FTIE: %d",
1072
0
             len);
1073
0
        return -1;
1074
0
      }
1075
0
      parse->r0kh_id = pos;
1076
0
      parse->r0kh_id_len = len;
1077
0
      wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID",
1078
0
            parse->r0kh_id, parse->r0kh_id_len);
1079
0
      break;
1080
0
    case FTIE_SUBELEM_IGTK:
1081
0
      wpa_printf(MSG_DEBUG, "FT: IGTK");
1082
0
      parse->igtk = pos;
1083
0
      parse->igtk_len = len;
1084
0
      break;
1085
#ifdef CONFIG_OCV
1086
    case FTIE_SUBELEM_OCI:
1087
      parse->oci = pos;
1088
      parse->oci_len = len;
1089
      wpa_hexdump(MSG_DEBUG, "FT: OCI",
1090
            parse->oci, parse->oci_len);
1091
      break;
1092
#endif /* CONFIG_OCV */
1093
0
    case FTIE_SUBELEM_BIGTK:
1094
0
      wpa_printf(MSG_DEBUG, "FT: BIGTK");
1095
0
      parse->bigtk = pos;
1096
0
      parse->bigtk_len = len;
1097
0
      break;
1098
0
    case FTIE_SUBELEM_MLO_GTK:
1099
0
      if (len < 2 + 1 + 1 + 8) {
1100
0
        wpa_printf(MSG_DEBUG,
1101
0
             "FT: Too short MLO GTK in FTE");
1102
0
        return -1;
1103
0
      }
1104
0
      link_id = pos[2] & 0x0f;
1105
0
      wpa_printf(MSG_DEBUG, "FT: MLO GTK (Link ID %u)",
1106
0
           link_id);
1107
0
      if (link_id >= MAX_NUM_MLO_LINKS)
1108
0
        break;
1109
0
      parse->valid_mlo_gtks |= BIT(link_id);
1110
0
      parse->mlo_gtk[link_id] = pos;
1111
0
      parse->mlo_gtk_len[link_id] = len;
1112
0
      break;
1113
0
    case FTIE_SUBELEM_MLO_IGTK:
1114
0
      if (len < 2 + 6 + 1 + 1) {
1115
0
        wpa_printf(MSG_DEBUG,
1116
0
             "FT: Too short MLO IGTK in FTE");
1117
0
        return -1;
1118
0
      }
1119
0
      link_id = pos[2 + 6] & 0x0f;
1120
0
      wpa_printf(MSG_DEBUG, "FT: MLO IGTK (Link ID %u)",
1121
0
           link_id);
1122
0
      if (link_id >= MAX_NUM_MLO_LINKS)
1123
0
        break;
1124
0
      parse->valid_mlo_igtks |= BIT(link_id);
1125
0
      parse->mlo_igtk[link_id] = pos;
1126
0
      parse->mlo_igtk_len[link_id] = len;
1127
0
      break;
1128
0
    case FTIE_SUBELEM_MLO_BIGTK:
1129
0
      if (len < 2 + 6 + 1 + 1) {
1130
0
        wpa_printf(MSG_DEBUG,
1131
0
             "FT: Too short MLO BIGTK in FTE");
1132
0
        return -1;
1133
0
      }
1134
0
      link_id = pos[2 + 6] & 0x0f;
1135
0
      wpa_printf(MSG_DEBUG, "FT: MLO BIGTK (Link ID %u)",
1136
0
           link_id);
1137
0
      if (link_id >= MAX_NUM_MLO_LINKS)
1138
0
        break;
1139
0
      parse->valid_mlo_bigtks |= BIT(link_id);
1140
0
      parse->mlo_bigtk[link_id] = pos;
1141
0
      parse->mlo_bigtk_len[link_id] = len;
1142
0
      break;
1143
0
    default:
1144
0
      wpa_printf(MSG_DEBUG, "FT: Unknown subelem id %u", id);
1145
0
      break;
1146
0
    }
1147
1148
0
    pos += len;
1149
0
  }
1150
1151
0
  return 0;
1152
0
}
1153
1154
1155
static int wpa_ft_parse_fte(int key_mgmt, const u8 *ie, size_t len,
1156
          struct wpa_ft_ies *parse)
1157
0
{
1158
0
  size_t mic_len;
1159
0
  u8 mic_len_info;
1160
0
  const u8 *pos = ie;
1161
0
  const u8 *end = pos + len;
1162
1163
0
  wpa_hexdump(MSG_DEBUG, "FT: FTE-MIC Control", pos, 2);
1164
0
  parse->fte_rsnxe_used = pos[0] & FTE_MIC_CTRL_RSNXE_USED;
1165
0
  mic_len_info = (pos[0] & FTE_MIC_CTRL_MIC_LEN_MASK) >>
1166
0
    FTE_MIC_CTRL_MIC_LEN_SHIFT;
1167
0
  parse->fte_elem_count = pos[1];
1168
0
  pos += 2;
1169
1170
0
  if (key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY) {
1171
0
    switch (mic_len_info) {
1172
0
    case FTE_MIC_LEN_16:
1173
0
      mic_len = 16;
1174
0
      break;
1175
0
    case FTE_MIC_LEN_24:
1176
0
      mic_len = 24;
1177
0
      break;
1178
0
    case FTE_MIC_LEN_32:
1179
0
      mic_len = 32;
1180
0
      break;
1181
0
    default:
1182
0
      wpa_printf(MSG_DEBUG,
1183
0
           "FT: Unknown MIC Length subfield value %u",
1184
0
           mic_len_info);
1185
0
      return -1;
1186
0
    }
1187
0
  } else {
1188
0
    mic_len = wpa_key_mgmt_sha384(key_mgmt) ? 24 : 16;
1189
0
  }
1190
0
  if (mic_len > (size_t) (end - pos)) {
1191
0
    wpa_printf(MSG_DEBUG, "FT: No room for %zu octet MIC in FTE",
1192
0
         mic_len);
1193
0
    return -1;
1194
0
  }
1195
0
  wpa_hexdump(MSG_DEBUG, "FT: FTE-MIC", pos, mic_len);
1196
0
  parse->fte_mic = pos;
1197
0
  parse->fte_mic_len = mic_len;
1198
0
  pos += mic_len;
1199
1200
0
  if (2 * WPA_NONCE_LEN > end - pos)
1201
0
    return -1;
1202
0
  parse->fte_anonce = pos;
1203
0
  wpa_hexdump(MSG_DEBUG, "FT: FTE-ANonce",
1204
0
        parse->fte_anonce, WPA_NONCE_LEN);
1205
0
  pos += WPA_NONCE_LEN;
1206
0
  parse->fte_snonce = pos;
1207
0
  wpa_hexdump(MSG_DEBUG, "FT: FTE-SNonce",
1208
0
        parse->fte_snonce, WPA_NONCE_LEN);
1209
0
  pos += WPA_NONCE_LEN;
1210
1211
0
  return wpa_ft_parse_ftie(ie, len, parse, pos);
1212
0
}
1213
1214
1215
int wpa_ft_parse_ies(const u8 *ies, size_t ies_len, struct wpa_ft_ies *parse,
1216
         int key_mgmt, bool reassoc_resp)
1217
0
{
1218
0
  const u8 *end, *pos;
1219
0
  struct wpa_ie_data data;
1220
0
  int ret;
1221
0
  int prot_ie_count = 0;
1222
0
  const u8 *fte = NULL;
1223
0
  size_t fte_len = 0;
1224
0
  bool is_fte = false;
1225
0
  struct ieee802_11_elems elems;
1226
1227
0
  os_memset(parse, 0, sizeof(*parse));
1228
0
  if (ies == NULL)
1229
0
    return 0;
1230
1231
0
  if (ieee802_11_parse_elems(ies, ies_len, &elems, 0) == ParseFailed) {
1232
0
    wpa_printf(MSG_DEBUG, "FT: Failed to parse elements");
1233
0
    goto fail;
1234
0
  }
1235
1236
0
  pos = ies;
1237
0
  end = ies + ies_len;
1238
0
  while (end - pos >= 2) {
1239
0
    u8 id, len;
1240
1241
0
    id = *pos++;
1242
0
    len = *pos++;
1243
0
    if (len > end - pos)
1244
0
      break;
1245
1246
0
    if (id != WLAN_EID_FAST_BSS_TRANSITION &&
1247
0
        id != WLAN_EID_FRAGMENT)
1248
0
      is_fte = false;
1249
1250
0
    switch (id) {
1251
0
    case WLAN_EID_RSN:
1252
0
      wpa_hexdump(MSG_DEBUG, "FT: RSNE", pos, len);
1253
0
      parse->rsn = pos;
1254
0
      parse->rsn_len = len;
1255
0
      ret = wpa_parse_wpa_ie_rsn(parse->rsn - 2,
1256
0
               parse->rsn_len + 2,
1257
0
               &data);
1258
0
      if (ret < 0) {
1259
0
        wpa_printf(MSG_DEBUG, "FT: Failed to parse "
1260
0
             "RSN IE: %d", ret);
1261
0
        goto fail;
1262
0
      }
1263
0
      parse->rsn_capab = data.capabilities;
1264
0
      if (data.num_pmkid == 1 && data.pmkid)
1265
0
        parse->rsn_pmkid = data.pmkid;
1266
0
      parse->key_mgmt = data.key_mgmt;
1267
0
      parse->pairwise_cipher = data.pairwise_cipher;
1268
0
      if (!key_mgmt)
1269
0
        key_mgmt = parse->key_mgmt;
1270
0
      break;
1271
0
    case WLAN_EID_RSNX:
1272
0
      wpa_hexdump(MSG_DEBUG, "FT: RSNXE", pos, len);
1273
0
      if (len < 1)
1274
0
        break;
1275
0
      parse->rsnxe = pos;
1276
0
      parse->rsnxe_len = len;
1277
0
      break;
1278
0
    case WLAN_EID_MOBILITY_DOMAIN:
1279
0
      wpa_hexdump(MSG_DEBUG, "FT: MDE", pos, len);
1280
0
      if (len < sizeof(struct rsn_mdie))
1281
0
        goto fail;
1282
0
      parse->mdie = pos;
1283
0
      parse->mdie_len = len;
1284
0
      break;
1285
0
    case WLAN_EID_FAST_BSS_TRANSITION:
1286
0
      wpa_hexdump(MSG_DEBUG, "FT: FTE", pos, len);
1287
      /* The first two octets (MIC Control field) is in the
1288
       * same offset for all cases, but the second field (MIC)
1289
       * has variable length with three different values.
1290
       * In particular the FT-SAE-EXT-KEY is inconvinient to
1291
       * parse, so try to handle this in pieces instead of
1292
       * using the struct rsn_ftie* definitions. */
1293
1294
0
      if (len < 2)
1295
0
        goto fail;
1296
0
      prot_ie_count = pos[1]; /* Element Count field in
1297
             * MIC Control */
1298
0
      is_fte = true;
1299
0
      fte = pos;
1300
0
      fte_len = len;
1301
0
      break;
1302
0
    case WLAN_EID_FRAGMENT:
1303
0
      if (is_fte) {
1304
0
        wpa_hexdump(MSG_DEBUG, "FT: FTE fragment",
1305
0
              pos, len);
1306
0
        fte_len += 2 + len;
1307
0
      }
1308
0
      break;
1309
0
    case WLAN_EID_TIMEOUT_INTERVAL:
1310
0
      wpa_hexdump(MSG_DEBUG, "FT: Timeout Interval",
1311
0
            pos, len);
1312
0
      if (len != 5)
1313
0
        break;
1314
0
      parse->tie = pos;
1315
0
      parse->tie_len = len;
1316
0
      break;
1317
0
    case WLAN_EID_RIC_DATA:
1318
0
      if (parse->ric == NULL)
1319
0
        parse->ric = pos - 2;
1320
0
      break;
1321
0
    }
1322
1323
0
    pos += len;
1324
0
  }
1325
1326
0
  if (fte) {
1327
0
    int res;
1328
1329
0
    if (fte_len < 255) {
1330
0
      res = wpa_ft_parse_fte(key_mgmt, fte, fte_len, parse);
1331
0
    } else {
1332
0
      parse->fte_buf = ieee802_11_defrag_data(fte, fte_len,
1333
0
                false);
1334
0
      if (!parse->fte_buf)
1335
0
        goto fail;
1336
0
      res = wpa_ft_parse_fte(key_mgmt,
1337
0
                 wpabuf_head(parse->fte_buf),
1338
0
                 wpabuf_len(parse->fte_buf),
1339
0
                 parse);
1340
0
    }
1341
0
    if (res < 0)
1342
0
      goto fail;
1343
0
  }
1344
1345
0
  if (prot_ie_count == 0)
1346
0
    return 0; /* no MIC */
1347
1348
  /*
1349
   * Check that the protected IE count matches with IEs included in the
1350
   * frame.
1351
   */
1352
0
  if (reassoc_resp && elems.basic_mle) {
1353
0
    unsigned int link_id;
1354
1355
    /* TODO: This count should be done based on all _requested_,
1356
     * not _accepted_ links. */
1357
0
    for (link_id = 0; link_id < MAX_NUM_MLO_LINKS; link_id++) {
1358
0
      if (parse->mlo_gtk[link_id]) {
1359
0
        if (parse->rsn)
1360
0
          prot_ie_count--;
1361
0
        if (parse->rsnxe)
1362
0
          prot_ie_count--;
1363
0
      }
1364
0
    }
1365
0
  } else {
1366
0
    if (parse->rsn)
1367
0
      prot_ie_count--;
1368
0
    if (parse->rsnxe)
1369
0
      prot_ie_count--;
1370
0
  }
1371
0
  if (parse->mdie)
1372
0
    prot_ie_count--;
1373
0
  if (parse->ftie)
1374
0
    prot_ie_count--;
1375
0
  if (prot_ie_count < 0) {
1376
0
    wpa_printf(MSG_DEBUG, "FT: Some required IEs not included in "
1377
0
         "the protected IE count");
1378
0
    goto fail;
1379
0
  }
1380
1381
0
  if (prot_ie_count == 0 && parse->ric) {
1382
0
    wpa_printf(MSG_DEBUG, "FT: RIC IE(s) in the frame, but not "
1383
0
         "included in protected IE count");
1384
0
    goto fail;
1385
0
  }
1386
1387
  /* Determine the end of the RIC IE(s) */
1388
0
  if (parse->ric) {
1389
0
    pos = parse->ric;
1390
0
    while (end - pos >= 2 && 2 + pos[1] <= end - pos &&
1391
0
           prot_ie_count) {
1392
0
      prot_ie_count--;
1393
0
      pos += 2 + pos[1];
1394
0
    }
1395
0
    parse->ric_len = pos - parse->ric;
1396
0
  }
1397
0
  if (prot_ie_count) {
1398
0
    wpa_printf(MSG_DEBUG, "FT: %d protected IEs missing from "
1399
0
         "frame", (int) prot_ie_count);
1400
0
    goto fail;
1401
0
  }
1402
1403
0
  return 0;
1404
1405
0
fail:
1406
0
  wpa_ft_parse_ies_free(parse);
1407
0
  return -1;
1408
0
}
1409
1410
1411
void wpa_ft_parse_ies_free(struct wpa_ft_ies *parse)
1412
0
{
1413
0
  if (!parse)
1414
0
    return;
1415
0
  wpabuf_free(parse->fte_buf);
1416
0
  parse->fte_buf = NULL;
1417
0
}
1418
1419
#endif /* CONFIG_IEEE80211R */
1420
1421
1422
#ifdef CONFIG_PASN
1423
1424
/*
1425
 * pasn_use_sha384 - Should SHA384 be used or SHA256
1426
 *
1427
 * @akmp: Authentication and key management protocol
1428
 * @cipher: The cipher suite
1429
 *
1430
 * According to IEEE P802.11az/D2.7, 12.12.7, the hash algorithm to use is the
1431
 * hash algorithm defined for the Base AKM (see Table 9-151 (AKM suite
1432
 * selectors)). When there is no Base AKM, the hash algorithm is selected based
1433
 * on the pairwise cipher suite provided in the RSNE by the AP in the second
1434
 * PASN frame. SHA-256 is used as the hash algorithm, except for the ciphers
1435
 * 00-0F-AC:9 and 00-0F-AC:10 for which SHA-384 is used.
1436
 */
1437
bool pasn_use_sha384(int akmp, int cipher)
1438
8.82k
{
1439
8.82k
  return (akmp == WPA_KEY_MGMT_PASN && (cipher == WPA_CIPHER_CCMP_256 ||
1440
6.11k
                cipher == WPA_CIPHER_GCMP_256)) ||
1441
8.82k
    wpa_key_mgmt_sha384(akmp);
1442
8.82k
}
1443
1444
1445
/**
1446
 * pasn_pmk_to_ptk - Calculate PASN PTK from PMK, addresses, etc.
1447
 * @pmk: Pairwise master key
1448
 * @pmk_len: Length of PMK
1449
 * @spa: Suppplicant address
1450
 * @bssid: AP BSSID
1451
 * @dhss: Is the shared secret (DHss) derived from the PASN ephemeral key
1452
 *  exchange encoded as an octet string
1453
 * @dhss_len: The length of dhss in octets
1454
 * @ptk: Buffer for pairwise transient key
1455
 * @akmp: Negotiated AKM
1456
 * @cipher: Negotiated pairwise cipher
1457
 * @kdk_len: the length in octets that should be derived for HTLK. Can be zero.
1458
 * Returns: 0 on success, -1 on failure
1459
 */
1460
int pasn_pmk_to_ptk(const u8 *pmk, size_t pmk_len,
1461
        const u8 *spa, const u8 *bssid,
1462
        const u8 *dhss, size_t dhss_len,
1463
        struct wpa_ptk *ptk, int akmp, int cipher,
1464
        size_t kdk_len)
1465
182
{
1466
182
  u8 tmp[WPA_KCK_MAX_LEN + WPA_TK_MAX_LEN + WPA_KDK_MAX_LEN];
1467
182
  u8 *data;
1468
182
  size_t data_len, ptk_len;
1469
182
  int ret = -1;
1470
182
  const char *label = "PASN PTK Derivation";
1471
1472
182
  if (!pmk || !pmk_len) {
1473
0
    wpa_printf(MSG_ERROR, "PASN: No PMK set for PTK derivation");
1474
0
    return -1;
1475
0
  }
1476
1477
182
  if (!dhss || !dhss_len) {
1478
0
    wpa_printf(MSG_ERROR, "PASN: No DHss set for PTK derivation");
1479
0
    return -1;
1480
0
  }
1481
1482
  /*
1483
   * PASN-PTK = KDF(PMK, “PASN PTK Derivation”, SPA || BSSID || DHss)
1484
   *
1485
   * KCK = L(PASN-PTK, 0, 256)
1486
   * TK = L(PASN-PTK, 256, TK_bits)
1487
   * KDK = L(PASN-PTK, 256 + TK_bits, kdk_len * 8)
1488
   */
1489
182
  data_len = 2 * ETH_ALEN + dhss_len;
1490
182
  data = os_zalloc(data_len);
1491
182
  if (!data)
1492
0
    return -1;
1493
1494
182
  os_memcpy(data, spa, ETH_ALEN);
1495
182
  os_memcpy(data + ETH_ALEN, bssid, ETH_ALEN);
1496
182
  os_memcpy(data + 2 * ETH_ALEN, dhss, dhss_len);
1497
1498
182
  ptk->kck_len = WPA_PASN_KCK_LEN;
1499
182
  ptk->tk_len = wpa_cipher_key_len(cipher);
1500
182
  ptk->kdk_len = kdk_len;
1501
182
  ptk->kek_len = 0;
1502
182
  ptk->kek2_len = 0;
1503
182
  ptk->kck2_len = 0;
1504
1505
182
  if (ptk->tk_len == 0) {
1506
0
    wpa_printf(MSG_ERROR,
1507
0
         "PASN: Unsupported cipher (0x%x) used in PTK derivation",
1508
0
         cipher);
1509
0
    goto err;
1510
0
  }
1511
1512
182
  ptk_len = ptk->kck_len + ptk->tk_len + ptk->kdk_len;
1513
182
  if (ptk_len > sizeof(tmp))
1514
0
    goto err;
1515
1516
182
  if (pasn_use_sha384(akmp, cipher)) {
1517
0
    wpa_printf(MSG_DEBUG, "PASN: PTK derivation using SHA384");
1518
1519
0
    if (sha384_prf(pmk, pmk_len, label, data, data_len, tmp,
1520
0
             ptk_len) < 0)
1521
0
      goto err;
1522
182
  } else {
1523
182
    wpa_printf(MSG_DEBUG, "PASN: PTK derivation using SHA256");
1524
1525
182
    if (sha256_prf(pmk, pmk_len, label, data, data_len, tmp,
1526
182
             ptk_len) < 0)
1527
0
      goto err;
1528
182
  }
1529
1530
182
  wpa_printf(MSG_DEBUG,
1531
182
       "PASN: PTK derivation: SPA=" MACSTR " BSSID=" MACSTR,
1532
182
       MAC2STR(spa), MAC2STR(bssid));
1533
1534
182
  wpa_hexdump_key(MSG_DEBUG, "PASN: DHss", dhss, dhss_len);
1535
182
  wpa_hexdump_key(MSG_DEBUG, "PASN: PMK", pmk, pmk_len);
1536
182
  wpa_hexdump_key(MSG_DEBUG, "PASN: PASN-PTK", tmp, ptk_len);
1537
1538
182
  os_memcpy(ptk->kck, tmp, WPA_PASN_KCK_LEN);
1539
182
  wpa_hexdump_key(MSG_DEBUG, "PASN: KCK:", ptk->kck, WPA_PASN_KCK_LEN);
1540
1541
182
  os_memcpy(ptk->tk, tmp + WPA_PASN_KCK_LEN, ptk->tk_len);
1542
182
  wpa_hexdump_key(MSG_DEBUG, "PASN: TK:", ptk->tk, ptk->tk_len);
1543
1544
182
  if (kdk_len) {
1545
0
    os_memcpy(ptk->kdk, tmp + WPA_PASN_KCK_LEN + ptk->tk_len,
1546
0
        ptk->kdk_len);
1547
0
    wpa_hexdump_key(MSG_DEBUG, "PASN: KDK:",
1548
0
        ptk->kdk, ptk->kdk_len);
1549
0
  }
1550
1551
182
  forced_memzero(tmp, sizeof(tmp));
1552
182
  ret = 0;
1553
182
err:
1554
182
  bin_clear_free(data, data_len);
1555
182
  return ret;
1556
182
}
1557
1558
1559
/*
1560
 * pasn_mic_len - Returns the MIC length for PASN authentication
1561
 */
1562
u8 pasn_mic_len(int akmp, int cipher)
1563
5.25k
{
1564
5.25k
  if (pasn_use_sha384(akmp, cipher))
1565
0
    return 24;
1566
1567
5.25k
  return 16;
1568
5.25k
}
1569
1570
1571
/**
1572
 * wpa_ltf_keyseed - Compute LTF keyseed from KDK
1573
 * @ptk: Buffer that holds pairwise transient key
1574
 * @akmp: Negotiated AKM
1575
 * @cipher: Negotiated pairwise cipher
1576
 * Returns: 0 on success, -1 on failure
1577
 */
1578
int wpa_ltf_keyseed(struct wpa_ptk *ptk, int akmp, int cipher)
1579
0
{
1580
0
  u8 *buf;
1581
0
  size_t buf_len;
1582
0
  u8 hash[SHA384_MAC_LEN];
1583
0
  const u8 *kdk = ptk->kdk;
1584
0
  size_t kdk_len = ptk->kdk_len;
1585
0
  const char *label = "Secure LTF key seed";
1586
1587
0
  if (!kdk || !kdk_len) {
1588
0
    wpa_printf(MSG_ERROR, "WPA: No KDK for LTF keyseed generation");
1589
0
    return -1;
1590
0
  }
1591
1592
0
  buf = (u8 *)label;
1593
0
  buf_len = os_strlen(label);
1594
1595
0
  if (pasn_use_sha384(akmp, cipher)) {
1596
0
    wpa_printf(MSG_DEBUG,
1597
0
         "WPA: Secure LTF keyseed using HMAC-SHA384");
1598
1599
0
    if (hmac_sha384(kdk, kdk_len, buf, buf_len, hash)) {
1600
0
      wpa_printf(MSG_ERROR,
1601
0
           "WPA: HMAC-SHA384 compute failed");
1602
0
      return -1;
1603
0
    }
1604
0
    os_memcpy(ptk->ltf_keyseed, hash, SHA384_MAC_LEN);
1605
0
    ptk->ltf_keyseed_len = SHA384_MAC_LEN;
1606
0
    wpa_hexdump_key(MSG_DEBUG, "WPA: Secure LTF keyseed: ",
1607
0
        ptk->ltf_keyseed, ptk->ltf_keyseed_len);
1608
1609
0
  } else {
1610
0
    wpa_printf(MSG_DEBUG, "WPA: LTF keyseed using HMAC-SHA256");
1611
1612
0
    if (hmac_sha256(kdk, kdk_len, buf, buf_len, hash)) {
1613
0
      wpa_printf(MSG_ERROR,
1614
0
           "WPA: HMAC-SHA256 compute failed");
1615
0
      return -1;
1616
0
    }
1617
0
    os_memcpy(ptk->ltf_keyseed, hash, SHA256_MAC_LEN);
1618
0
    ptk->ltf_keyseed_len = SHA256_MAC_LEN;
1619
0
    wpa_hexdump_key(MSG_DEBUG, "WPA: Secure LTF keyseed: ",
1620
0
        ptk->ltf_keyseed, ptk->ltf_keyseed_len);
1621
0
  }
1622
1623
0
  return 0;
1624
0
}
1625
1626
1627
/**
1628
 * pasn_mic - Calculate PASN MIC
1629
 * @kck: The key confirmation key for the PASN PTKSA
1630
 * @akmp: Negotiated AKM
1631
 * @cipher: Negotiated pairwise cipher
1632
 * @addr1: For the 2nd PASN frame supplicant address; for the 3rd frame the
1633
 *  BSSID
1634
 * @addr2: For the 2nd PASN frame the BSSID; for the 3rd frame the supplicant
1635
 *  address
1636
 * @data: For calculating the MIC for the 2nd PASN frame, this should hold the
1637
 *  Beacon frame RSNE + RSNXE. For calculating the MIC for the 3rd PASN
1638
 *  frame, this should hold the hash of the body of the PASN 1st frame.
1639
 * @data_len: The length of data
1640
 * @frame: The body of the PASN frame including the MIC element with the octets
1641
 *  in the MIC field of the MIC element set to 0.
1642
 * @frame_len: The length of frame
1643
 * @mic: Buffer to hold the MIC on success. Should be big enough to handle the
1644
 *  maximal MIC length
1645
 * Returns: 0 on success, -1 on failure
1646
 */
1647
int pasn_mic(const u8 *kck, int akmp, int cipher,
1648
       const u8 *addr1, const u8 *addr2,
1649
       const u8 *data, size_t data_len,
1650
       const u8 *frame, size_t frame_len, u8 *mic)
1651
431
{
1652
431
  u8 *buf;
1653
431
  u8 hash[SHA384_MAC_LEN];
1654
431
  size_t buf_len = 2 * ETH_ALEN + data_len + frame_len;
1655
431
  int ret = -1;
1656
1657
431
  if (!kck) {
1658
0
    wpa_printf(MSG_ERROR, "PASN: No KCK for MIC calculation");
1659
0
    return -1;
1660
0
  }
1661
1662
431
  if (!data || !data_len) {
1663
0
    wpa_printf(MSG_ERROR, "PASN: invalid data for MIC calculation");
1664
0
    return -1;
1665
0
  }
1666
1667
431
  if (!frame || !frame_len) {
1668
0
    wpa_printf(MSG_ERROR, "PASN: invalid data for MIC calculation");
1669
0
    return -1;
1670
0
  }
1671
1672
431
  buf = os_zalloc(buf_len);
1673
431
  if (!buf)
1674
0
    return -1;
1675
1676
431
  os_memcpy(buf, addr1, ETH_ALEN);
1677
431
  os_memcpy(buf + ETH_ALEN, addr2, ETH_ALEN);
1678
1679
431
  wpa_hexdump_key(MSG_DEBUG, "PASN: MIC: data", data, data_len);
1680
431
  os_memcpy(buf + 2 * ETH_ALEN, data, data_len);
1681
1682
431
  wpa_hexdump_key(MSG_DEBUG, "PASN: MIC: frame", frame, frame_len);
1683
431
  os_memcpy(buf + 2 * ETH_ALEN + data_len, frame, frame_len);
1684
1685
431
  wpa_hexdump_key(MSG_DEBUG, "PASN: MIC: KCK", kck, WPA_PASN_KCK_LEN);
1686
431
  wpa_hexdump_key(MSG_DEBUG, "PASN: MIC: buf", buf, buf_len);
1687
1688
431
  if (pasn_use_sha384(akmp, cipher)) {
1689
0
    wpa_printf(MSG_DEBUG, "PASN: MIC using HMAC-SHA384");
1690
1691
0
    if (hmac_sha384(kck, WPA_PASN_KCK_LEN, buf, buf_len, hash))
1692
0
      goto err;
1693
1694
0
    os_memcpy(mic, hash, 24);
1695
0
    wpa_hexdump_key(MSG_DEBUG, "PASN: MIC: mic: ", mic, 24);
1696
431
  } else {
1697
431
    wpa_printf(MSG_DEBUG, "PASN: MIC using HMAC-SHA256");
1698
1699
431
    if (hmac_sha256(kck, WPA_PASN_KCK_LEN, buf, buf_len, hash))
1700
0
      goto err;
1701
1702
431
    os_memcpy(mic, hash, 16);
1703
431
    wpa_hexdump_key(MSG_DEBUG, "PASN: MIC: mic: ", mic, 16);
1704
431
  }
1705
1706
431
  ret = 0;
1707
431
err:
1708
431
  bin_clear_free(buf, buf_len);
1709
431
  return ret;
1710
431
}
1711
1712
1713
/**
1714
 * pasn_auth_frame_hash - Computes a hash of an Authentication frame body
1715
 * @akmp: Negotiated AKM
1716
 * @cipher: Negotiated pairwise cipher
1717
 * @data: Pointer to the Authentication frame body
1718
 * @len: Length of the Authentication frame body
1719
 * @hash: On return would hold the computed hash. Should be big enough to handle
1720
 *  SHA384.
1721
 * Returns: 0 on success, -1 on failure
1722
 */
1723
int pasn_auth_frame_hash(int akmp, int cipher, const u8 *data, size_t len,
1724
       u8 *hash)
1725
2.95k
{
1726
2.95k
  if (pasn_use_sha384(akmp, cipher)) {
1727
0
    wpa_printf(MSG_DEBUG, "PASN: Frame hash using SHA-384");
1728
0
    return sha384_vector(1, &data, &len, hash);
1729
2.95k
  } else {
1730
2.95k
    wpa_printf(MSG_DEBUG, "PASN: Frame hash using SHA-256");
1731
2.95k
    return sha256_vector(1, &data, &len, hash);
1732
2.95k
  }
1733
2.95k
}
1734
1735
#endif /* CONFIG_PASN */
1736
1737
1738
static int rsn_selector_to_bitfield(const u8 *s)
1739
10.0k
{
1740
10.0k
  if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_NONE)
1741
137
    return WPA_CIPHER_NONE;
1742
9.90k
  if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_TKIP)
1743
269
    return WPA_CIPHER_TKIP;
1744
9.63k
  if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_CCMP)
1745
4.56k
    return WPA_CIPHER_CCMP;
1746
5.07k
  if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_AES_128_CMAC)
1747
161
    return WPA_CIPHER_AES_128_CMAC;
1748
4.91k
  if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_GCMP)
1749
299
    return WPA_CIPHER_GCMP;
1750
4.61k
  if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_CCMP_256)
1751
254
    return WPA_CIPHER_CCMP_256;
1752
4.36k
  if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_GCMP_256)
1753
269
    return WPA_CIPHER_GCMP_256;
1754
4.09k
  if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_BIP_GMAC_128)
1755
194
    return WPA_CIPHER_BIP_GMAC_128;
1756
3.90k
  if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_BIP_GMAC_256)
1757
201
    return WPA_CIPHER_BIP_GMAC_256;
1758
3.69k
  if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_BIP_CMAC_256)
1759
172
    return WPA_CIPHER_BIP_CMAC_256;
1760
3.52k
  if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED)
1761
1.63k
    return WPA_CIPHER_GTK_NOT_USED;
1762
1.88k
  return 0;
1763
3.52k
}
1764
1765
1766
static int rsn_key_mgmt_to_bitfield(const u8 *s)
1767
6.24k
{
1768
6.24k
  if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_UNSPEC_802_1X)
1769
226
    return WPA_KEY_MGMT_IEEE8021X;
1770
6.02k
  if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X)
1771
283
    return WPA_KEY_MGMT_PSK;
1772
5.74k
#ifdef CONFIG_IEEE80211R
1773
5.74k
  if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_802_1X)
1774
215
    return WPA_KEY_MGMT_FT_IEEE8021X;
1775
5.52k
  if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_PSK)
1776
231
    return WPA_KEY_MGMT_FT_PSK;
1777
5.29k
#ifdef CONFIG_SHA384
1778
5.29k
  if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_802_1X_SHA384)
1779
236
    return WPA_KEY_MGMT_FT_IEEE8021X_SHA384;
1780
5.05k
#endif /* CONFIG_SHA384 */
1781
5.05k
#endif /* CONFIG_IEEE80211R */
1782
5.05k
#ifdef CONFIG_SHA384
1783
5.05k
  if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_802_1X_SHA384)
1784
78
    return WPA_KEY_MGMT_IEEE8021X_SHA384;
1785
4.98k
#endif /* CONFIG_SHA384 */
1786
4.98k
  if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_802_1X_SHA256)
1787
240
    return WPA_KEY_MGMT_IEEE8021X_SHA256;
1788
4.74k
  if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_PSK_SHA256)
1789
271
    return WPA_KEY_MGMT_PSK_SHA256;
1790
4.46k
#ifdef CONFIG_SAE
1791
4.46k
  if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_SAE)
1792
246
    return WPA_KEY_MGMT_SAE;
1793
4.22k
  if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_SAE_EXT_KEY)
1794
136
    return WPA_KEY_MGMT_SAE_EXT_KEY;
1795
4.08k
  if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_SAE)
1796
261
    return WPA_KEY_MGMT_FT_SAE;
1797
3.82k
  if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_SAE_EXT_KEY)
1798
127
    return WPA_KEY_MGMT_FT_SAE_EXT_KEY;
1799
3.69k
#endif /* CONFIG_SAE */
1800
3.69k
  if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_802_1X_SUITE_B)
1801
264
    return WPA_KEY_MGMT_IEEE8021X_SUITE_B;
1802
3.43k
  if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192)
1803
257
    return WPA_KEY_MGMT_IEEE8021X_SUITE_B_192;
1804
3.17k
  if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FILS_SHA256)
1805
261
    return WPA_KEY_MGMT_FILS_SHA256;
1806
2.91k
  if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FILS_SHA384)
1807
211
    return WPA_KEY_MGMT_FILS_SHA384;
1808
2.70k
  if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_FILS_SHA256)
1809
216
    return WPA_KEY_MGMT_FT_FILS_SHA256;
1810
2.49k
  if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_FILS_SHA384)
1811
174
    return WPA_KEY_MGMT_FT_FILS_SHA384;
1812
#ifdef CONFIG_OWE
1813
  if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_OWE)
1814
    return WPA_KEY_MGMT_OWE;
1815
#endif /* CONFIG_OWE */
1816
#ifdef CONFIG_DPP
1817
  if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_DPP)
1818
    return WPA_KEY_MGMT_DPP;
1819
#endif /* CONFIG_DPP */
1820
2.31k
  if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_OSEN)
1821
213
    return WPA_KEY_MGMT_OSEN;
1822
2.10k
#ifdef CONFIG_PASN
1823
2.10k
  if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_PASN)
1824
486
    return WPA_KEY_MGMT_PASN;
1825
1.61k
#endif /* CONFIG_PASN */
1826
1.61k
  return 0;
1827
2.10k
}
1828
1829
1830
int wpa_cipher_valid_group(int cipher)
1831
3.83k
{
1832
3.83k
  return wpa_cipher_valid_pairwise(cipher) ||
1833
3.83k
    cipher == WPA_CIPHER_GTK_NOT_USED;
1834
3.83k
}
1835
1836
1837
int wpa_cipher_valid_mgmt_group(int cipher)
1838
4.09k
{
1839
4.09k
  return cipher == WPA_CIPHER_GTK_NOT_USED ||
1840
4.09k
    cipher == WPA_CIPHER_AES_128_CMAC ||
1841
4.09k
    cipher == WPA_CIPHER_BIP_GMAC_128 ||
1842
4.09k
    cipher == WPA_CIPHER_BIP_GMAC_256 ||
1843
4.09k
    cipher == WPA_CIPHER_BIP_CMAC_256;
1844
4.09k
}
1845
1846
1847
/**
1848
 * wpa_parse_wpa_ie_rsn - Parse RSN IE
1849
 * @rsn_ie: Buffer containing RSN IE
1850
 * @rsn_ie_len: RSN IE buffer length (including IE number and length octets)
1851
 * @data: Pointer to structure that will be filled in with parsed data
1852
 * Returns: 0 on success, <0 on failure
1853
 */
1854
int wpa_parse_wpa_ie_rsn(const u8 *rsn_ie, size_t rsn_ie_len,
1855
       struct wpa_ie_data *data)
1856
4.01k
{
1857
4.01k
  const u8 *pos;
1858
4.01k
  int left;
1859
4.01k
  int i, count;
1860
1861
4.01k
  os_memset(data, 0, sizeof(*data));
1862
4.01k
  data->proto = WPA_PROTO_RSN;
1863
4.01k
  data->pairwise_cipher = WPA_CIPHER_CCMP;
1864
4.01k
  data->group_cipher = WPA_CIPHER_CCMP;
1865
4.01k
  data->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
1866
4.01k
  data->capabilities = 0;
1867
4.01k
  data->pmkid = NULL;
1868
4.01k
  data->num_pmkid = 0;
1869
4.01k
  data->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
1870
1871
4.01k
  if (rsn_ie_len == 0) {
1872
    /* No RSN IE - fail silently */
1873
0
    return -1;
1874
0
  }
1875
1876
4.01k
  if (rsn_ie_len < sizeof(struct rsn_ie_hdr)) {
1877
30
    wpa_printf(MSG_DEBUG, "%s: ie len too short %lu",
1878
30
         __func__, (unsigned long) rsn_ie_len);
1879
30
    return -1;
1880
30
  }
1881
1882
3.98k
  if (rsn_ie_len >= 6 && rsn_ie[1] >= 4 &&
1883
3.98k
      rsn_ie[1] == rsn_ie_len - 2 &&
1884
3.98k
      WPA_GET_BE32(&rsn_ie[2]) == OSEN_IE_VENDOR_TYPE) {
1885
2
    pos = rsn_ie + 6;
1886
2
    left = rsn_ie_len - 6;
1887
1888
2
    data->group_cipher = WPA_CIPHER_GTK_NOT_USED;
1889
2
    data->has_group = 1;
1890
2
    data->key_mgmt = WPA_KEY_MGMT_OSEN;
1891
2
    data->proto = WPA_PROTO_OSEN;
1892
3.98k
  } else {
1893
3.98k
    const struct rsn_ie_hdr *hdr;
1894
1895
3.98k
    hdr = (const struct rsn_ie_hdr *) rsn_ie;
1896
1897
3.98k
    if (hdr->elem_id != WLAN_EID_RSN ||
1898
3.98k
        hdr->len != rsn_ie_len - 2 ||
1899
3.98k
        WPA_GET_LE16(hdr->version) != RSN_VERSION) {
1900
140
      wpa_printf(MSG_DEBUG, "%s: malformed ie or unknown version",
1901
140
           __func__);
1902
140
      return -2;
1903
140
    }
1904
1905
3.84k
    pos = (const u8 *) (hdr + 1);
1906
3.84k
    left = rsn_ie_len - sizeof(*hdr);
1907
3.84k
  }
1908
1909
3.84k
  if (left >= RSN_SELECTOR_LEN) {
1910
3.83k
    data->group_cipher = rsn_selector_to_bitfield(pos);
1911
3.83k
    data->has_group = 1;
1912
3.83k
    if (!wpa_cipher_valid_group(data->group_cipher)) {
1913
147
      wpa_printf(MSG_DEBUG,
1914
147
           "%s: invalid group cipher 0x%x (%08x)",
1915
147
           __func__, data->group_cipher,
1916
147
           WPA_GET_BE32(pos));
1917
#ifdef CONFIG_NO_TKIP
1918
      if (RSN_SELECTOR_GET(pos) == RSN_CIPHER_SUITE_TKIP) {
1919
        wpa_printf(MSG_DEBUG,
1920
             "%s: TKIP as group cipher not supported in CONFIG_NO_TKIP=y build",
1921
             __func__);
1922
      }
1923
#endif /* CONFIG_NO_TKIP */
1924
147
      return -1;
1925
147
    }
1926
3.68k
    pos += RSN_SELECTOR_LEN;
1927
3.68k
    left -= RSN_SELECTOR_LEN;
1928
3.68k
  } else if (left > 0) {
1929
11
    wpa_printf(MSG_DEBUG, "%s: ie length mismatch, %u too much",
1930
11
         __func__, left);
1931
11
    return -3;
1932
11
  }
1933
1934
3.68k
  if (left >= 2) {
1935
3.67k
    data->pairwise_cipher = 0;
1936
3.67k
    count = WPA_GET_LE16(pos);
1937
3.67k
    pos += 2;
1938
3.67k
    left -= 2;
1939
3.67k
    if (count == 0 || count > left / RSN_SELECTOR_LEN) {
1940
40
      wpa_printf(MSG_DEBUG, "%s: ie count botch (pairwise), "
1941
40
           "count %u left %u", __func__, count, left);
1942
40
      return -4;
1943
40
    }
1944
3.63k
    if (count)
1945
3.63k
      data->has_pairwise = 1;
1946
9.38k
    for (i = 0; i < count; i++) {
1947
5.75k
      data->pairwise_cipher |= rsn_selector_to_bitfield(pos);
1948
5.75k
      pos += RSN_SELECTOR_LEN;
1949
5.75k
      left -= RSN_SELECTOR_LEN;
1950
5.75k
    }
1951
3.63k
    if (data->pairwise_cipher & WPA_CIPHER_AES_128_CMAC) {
1952
25
      wpa_printf(MSG_DEBUG, "%s: AES-128-CMAC used as "
1953
25
           "pairwise cipher", __func__);
1954
25
      return -1;
1955
25
    }
1956
3.63k
  } else if (left == 1) {
1957
2
    wpa_printf(MSG_DEBUG, "%s: ie too short (for key mgmt)",
1958
2
         __func__);
1959
2
    return -5;
1960
2
  }
1961
1962
3.62k
  if (left >= 2) {
1963
3.35k
    data->key_mgmt = 0;
1964
3.35k
    count = WPA_GET_LE16(pos);
1965
3.35k
    pos += 2;
1966
3.35k
    left -= 2;
1967
3.35k
    if (count == 0 || count > left / RSN_SELECTOR_LEN) {
1968
59
      wpa_printf(MSG_DEBUG, "%s: ie count botch (key mgmt), "
1969
59
           "count %u left %u", __func__, count, left);
1970
59
      return -6;
1971
59
    }
1972
11.5k
    for (i = 0; i < count; i++) {
1973
8.21k
      data->key_mgmt |= rsn_key_mgmt_to_bitfield(pos);
1974
8.21k
      pos += RSN_SELECTOR_LEN;
1975
8.21k
      left -= RSN_SELECTOR_LEN;
1976
8.21k
    }
1977
3.29k
  } else if (left == 1) {
1978
7
    wpa_printf(MSG_DEBUG, "%s: ie too short (for capabilities)",
1979
7
         __func__);
1980
7
    return -7;
1981
7
  }
1982
1983
3.55k
  if (left >= 2) {
1984
2.89k
    data->capabilities = WPA_GET_LE16(pos);
1985
2.89k
    pos += 2;
1986
2.89k
    left -= 2;
1987
2.89k
  }
1988
1989
3.55k
  if (left >= 2) {
1990
506
    u16 num_pmkid = WPA_GET_LE16(pos);
1991
506
    pos += 2;
1992
506
    left -= 2;
1993
506
    if (num_pmkid > (unsigned int) left / PMKID_LEN) {
1994
43
      wpa_printf(MSG_DEBUG, "%s: PMKID underflow "
1995
43
           "(num_pmkid=%u left=%d)",
1996
43
           __func__, num_pmkid, left);
1997
43
      data->num_pmkid = 0;
1998
43
      return -9;
1999
463
    } else {
2000
463
      data->num_pmkid = num_pmkid;
2001
463
      data->pmkid = pos;
2002
463
      pos += data->num_pmkid * PMKID_LEN;
2003
463
      left -= data->num_pmkid * PMKID_LEN;
2004
463
    }
2005
506
  }
2006
2007
3.51k
  if (left >= 4) {
2008
455
    data->mgmt_group_cipher = rsn_selector_to_bitfield(pos);
2009
455
    if (!wpa_cipher_valid_mgmt_group(data->mgmt_group_cipher)) {
2010
178
      wpa_printf(MSG_DEBUG,
2011
178
           "%s: Unsupported management group cipher 0x%x (%08x)",
2012
178
           __func__, data->mgmt_group_cipher,
2013
178
           WPA_GET_BE32(pos));
2014
178
      return -10;
2015
178
    }
2016
277
    pos += RSN_SELECTOR_LEN;
2017
277
    left -= RSN_SELECTOR_LEN;
2018
277
  }
2019
2020
3.33k
  if (left > 0) {
2021
93
    wpa_hexdump(MSG_DEBUG,
2022
93
          "wpa_parse_wpa_ie_rsn: ignore trailing bytes",
2023
93
          pos, left);
2024
93
  }
2025
2026
3.33k
  return 0;
2027
3.51k
}
2028
2029
2030
static int wpa_selector_to_bitfield(const u8 *s)
2031
0
{
2032
0
  if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_NONE)
2033
0
    return WPA_CIPHER_NONE;
2034
0
  if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_TKIP)
2035
0
    return WPA_CIPHER_TKIP;
2036
0
  if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_CCMP)
2037
0
    return WPA_CIPHER_CCMP;
2038
0
  return 0;
2039
0
}
2040
2041
2042
static int wpa_key_mgmt_to_bitfield(const u8 *s)
2043
0
{
2044
0
  if (RSN_SELECTOR_GET(s) == WPA_AUTH_KEY_MGMT_UNSPEC_802_1X)
2045
0
    return WPA_KEY_MGMT_IEEE8021X;
2046
0
  if (RSN_SELECTOR_GET(s) == WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X)
2047
0
    return WPA_KEY_MGMT_PSK;
2048
0
  if (RSN_SELECTOR_GET(s) == WPA_AUTH_KEY_MGMT_NONE)
2049
0
    return WPA_KEY_MGMT_WPA_NONE;
2050
0
  return 0;
2051
0
}
2052
2053
2054
int wpa_parse_wpa_ie_wpa(const u8 *wpa_ie, size_t wpa_ie_len,
2055
       struct wpa_ie_data *data)
2056
0
{
2057
0
  const struct wpa_ie_hdr *hdr;
2058
0
  const u8 *pos;
2059
0
  int left;
2060
0
  int i, count;
2061
2062
0
  os_memset(data, 0, sizeof(*data));
2063
0
  data->proto = WPA_PROTO_WPA;
2064
0
  data->pairwise_cipher = WPA_CIPHER_TKIP;
2065
0
  data->group_cipher = WPA_CIPHER_TKIP;
2066
0
  data->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
2067
0
  data->capabilities = 0;
2068
0
  data->pmkid = NULL;
2069
0
  data->num_pmkid = 0;
2070
0
  data->mgmt_group_cipher = 0;
2071
2072
0
  if (wpa_ie_len < sizeof(struct wpa_ie_hdr)) {
2073
0
    wpa_printf(MSG_DEBUG, "%s: ie len too short %lu",
2074
0
         __func__, (unsigned long) wpa_ie_len);
2075
0
    return -1;
2076
0
  }
2077
2078
0
  hdr = (const struct wpa_ie_hdr *) wpa_ie;
2079
2080
0
  if (hdr->elem_id != WLAN_EID_VENDOR_SPECIFIC ||
2081
0
      hdr->len != wpa_ie_len - 2 ||
2082
0
      RSN_SELECTOR_GET(hdr->oui) != WPA_OUI_TYPE ||
2083
0
      WPA_GET_LE16(hdr->version) != WPA_VERSION) {
2084
0
    wpa_printf(MSG_DEBUG, "%s: malformed ie or unknown version",
2085
0
         __func__);
2086
0
    return -2;
2087
0
  }
2088
2089
0
  pos = (const u8 *) (hdr + 1);
2090
0
  left = wpa_ie_len - sizeof(*hdr);
2091
2092
0
  if (left >= WPA_SELECTOR_LEN) {
2093
0
    data->group_cipher = wpa_selector_to_bitfield(pos);
2094
0
    pos += WPA_SELECTOR_LEN;
2095
0
    left -= WPA_SELECTOR_LEN;
2096
0
  } else if (left > 0) {
2097
0
    wpa_printf(MSG_DEBUG, "%s: ie length mismatch, %u too much",
2098
0
         __func__, left);
2099
0
    return -3;
2100
0
  }
2101
2102
0
  if (left >= 2) {
2103
0
    data->pairwise_cipher = 0;
2104
0
    count = WPA_GET_LE16(pos);
2105
0
    pos += 2;
2106
0
    left -= 2;
2107
0
    if (count == 0 || count > left / WPA_SELECTOR_LEN) {
2108
0
      wpa_printf(MSG_DEBUG, "%s: ie count botch (pairwise), "
2109
0
           "count %u left %u", __func__, count, left);
2110
0
      return -4;
2111
0
    }
2112
0
    for (i = 0; i < count; i++) {
2113
0
      data->pairwise_cipher |= wpa_selector_to_bitfield(pos);
2114
0
      pos += WPA_SELECTOR_LEN;
2115
0
      left -= WPA_SELECTOR_LEN;
2116
0
    }
2117
0
  } else if (left == 1) {
2118
0
    wpa_printf(MSG_DEBUG, "%s: ie too short (for key mgmt)",
2119
0
         __func__);
2120
0
    return -5;
2121
0
  }
2122
2123
0
  if (left >= 2) {
2124
0
    data->key_mgmt = 0;
2125
0
    count = WPA_GET_LE16(pos);
2126
0
    pos += 2;
2127
0
    left -= 2;
2128
0
    if (count == 0 || count > left / WPA_SELECTOR_LEN) {
2129
0
      wpa_printf(MSG_DEBUG, "%s: ie count botch (key mgmt), "
2130
0
           "count %u left %u", __func__, count, left);
2131
0
      return -6;
2132
0
    }
2133
0
    for (i = 0; i < count; i++) {
2134
0
      data->key_mgmt |= wpa_key_mgmt_to_bitfield(pos);
2135
0
      pos += WPA_SELECTOR_LEN;
2136
0
      left -= WPA_SELECTOR_LEN;
2137
0
    }
2138
0
  } else if (left == 1) {
2139
0
    wpa_printf(MSG_DEBUG, "%s: ie too short (for capabilities)",
2140
0
         __func__);
2141
0
    return -7;
2142
0
  }
2143
2144
0
  if (left >= 2) {
2145
0
    data->capabilities = WPA_GET_LE16(pos);
2146
0
    pos += 2;
2147
0
    left -= 2;
2148
0
  }
2149
2150
0
  if (left > 0) {
2151
0
    wpa_hexdump(MSG_DEBUG,
2152
0
          "wpa_parse_wpa_ie_wpa: ignore trailing bytes",
2153
0
          pos, left);
2154
0
  }
2155
2156
0
  return 0;
2157
0
}
2158
2159
2160
int wpa_default_rsn_cipher(int freq)
2161
0
{
2162
0
  if (freq > 56160)
2163
0
    return WPA_CIPHER_GCMP; /* DMG */
2164
2165
0
  return WPA_CIPHER_CCMP;
2166
0
}
2167
2168
2169
#ifdef CONFIG_IEEE80211R
2170
2171
/**
2172
 * wpa_derive_pmk_r0 - Derive PMK-R0 and PMKR0Name
2173
 *
2174
 * IEEE Std 802.11r-2008 - 8.5.1.5.3
2175
 */
2176
int wpa_derive_pmk_r0(const u8 *xxkey, size_t xxkey_len,
2177
          const u8 *ssid, size_t ssid_len,
2178
          const u8 *mdid, const u8 *r0kh_id, size_t r0kh_id_len,
2179
          const u8 *s0kh_id, u8 *pmk_r0, u8 *pmk_r0_name,
2180
          int key_mgmt)
2181
0
{
2182
0
  u8 buf[1 + SSID_MAX_LEN + MOBILITY_DOMAIN_ID_LEN + 1 +
2183
0
         FT_R0KH_ID_MAX_LEN + ETH_ALEN];
2184
0
  u8 *pos, r0_key_data[64 + 16], hash[64];
2185
0
  const u8 *addr[2];
2186
0
  size_t len[2];
2187
0
  size_t q, r0_key_data_len;
2188
0
  int res;
2189
2190
0
  if (key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY &&
2191
0
      (xxkey_len == SHA256_MAC_LEN || xxkey_len == SHA384_MAC_LEN ||
2192
0
       xxkey_len == SHA512_MAC_LEN))
2193
0
    q = xxkey_len;
2194
0
  else if (wpa_key_mgmt_sha384(key_mgmt))
2195
0
    q = SHA384_MAC_LEN;
2196
0
  else
2197
0
    q = SHA256_MAC_LEN;
2198
0
  r0_key_data_len = q + 16;
2199
2200
  /*
2201
   * R0-Key-Data = KDF-Hash-Length(XXKey, "FT-R0",
2202
   *                       SSIDlength || SSID || MDID || R0KHlength ||
2203
   *                       R0KH-ID || S0KH-ID)
2204
   * XXKey is either the second 256 bits of MSK or PSK; or the first
2205
   * 384 bits of MSK for FT-EAP-SHA384; or PMK from SAE.
2206
   * PMK-R0 = L(R0-Key-Data, 0, Q)
2207
   * PMK-R0Name-Salt = L(R0-Key-Data, Q, 128)
2208
   * Q = 384 for FT-EAP-SHA384; the length of the digest generated by H()
2209
   * for FT-SAE-EXT-KEY; or otherwise, 256
2210
   */
2211
0
  if (ssid_len > SSID_MAX_LEN || r0kh_id_len > FT_R0KH_ID_MAX_LEN)
2212
0
    return -1;
2213
0
  wpa_printf(MSG_DEBUG, "FT: Derive PMK-R0 using KDF-SHA%zu", q * 8);
2214
0
  wpa_hexdump_key(MSG_DEBUG, "FT: XXKey", xxkey, xxkey_len);
2215
0
  wpa_hexdump_ascii(MSG_DEBUG, "FT: SSID", ssid, ssid_len);
2216
0
  wpa_hexdump(MSG_DEBUG, "FT: MDID", mdid, MOBILITY_DOMAIN_ID_LEN);
2217
0
  wpa_hexdump_ascii(MSG_DEBUG, "FT: R0KH-ID", r0kh_id, r0kh_id_len);
2218
0
  wpa_printf(MSG_DEBUG, "FT: S0KH-ID: " MACSTR, MAC2STR(s0kh_id));
2219
0
  pos = buf;
2220
0
  *pos++ = ssid_len;
2221
0
  os_memcpy(pos, ssid, ssid_len);
2222
0
  pos += ssid_len;
2223
0
  os_memcpy(pos, mdid, MOBILITY_DOMAIN_ID_LEN);
2224
0
  pos += MOBILITY_DOMAIN_ID_LEN;
2225
0
  *pos++ = r0kh_id_len;
2226
0
  os_memcpy(pos, r0kh_id, r0kh_id_len);
2227
0
  pos += r0kh_id_len;
2228
0
  os_memcpy(pos, s0kh_id, ETH_ALEN);
2229
0
  pos += ETH_ALEN;
2230
2231
0
  res = -1;
2232
#ifdef CONFIG_SHA512
2233
  if (q == SHA512_MAC_LEN) {
2234
    if (xxkey_len != SHA512_MAC_LEN) {
2235
      wpa_printf(MSG_ERROR,
2236
           "FT: Unexpected XXKey length %d (expected %d)",
2237
           (int) xxkey_len, SHA512_MAC_LEN);
2238
      return -1;
2239
    }
2240
    res = sha512_prf(xxkey, xxkey_len, "FT-R0", buf, pos - buf,
2241
         r0_key_data, r0_key_data_len);
2242
  }
2243
#endif /* CONFIG_SHA512 */
2244
0
#ifdef CONFIG_SHA384
2245
0
  if (q == SHA384_MAC_LEN) {
2246
0
    if (xxkey_len != SHA384_MAC_LEN) {
2247
0
      wpa_printf(MSG_ERROR,
2248
0
           "FT: Unexpected XXKey length %d (expected %d)",
2249
0
           (int) xxkey_len, SHA384_MAC_LEN);
2250
0
      return -1;
2251
0
    }
2252
0
    res = sha384_prf(xxkey, xxkey_len, "FT-R0", buf, pos - buf,
2253
0
         r0_key_data, r0_key_data_len);
2254
0
  }
2255
0
#endif /* CONFIG_SHA384 */
2256
0
  if (q == SHA256_MAC_LEN) {
2257
0
    if (xxkey_len != PMK_LEN) {
2258
0
      wpa_printf(MSG_ERROR,
2259
0
           "FT: Unexpected XXKey length %d (expected %d)",
2260
0
           (int) xxkey_len, PMK_LEN);
2261
0
      return -1;
2262
0
    }
2263
0
    res = sha256_prf(xxkey, xxkey_len, "FT-R0", buf, pos - buf,
2264
0
         r0_key_data, r0_key_data_len);
2265
0
  }
2266
0
  if (res < 0)
2267
0
    return res;
2268
0
  os_memcpy(pmk_r0, r0_key_data, q);
2269
0
  wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R0", pmk_r0, q);
2270
0
  wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R0Name-Salt", &r0_key_data[q], 16);
2271
2272
  /*
2273
   * PMKR0Name = Truncate-128(Hash("FT-R0N" || PMK-R0Name-Salt)
2274
   */
2275
0
  addr[0] = (const u8 *) "FT-R0N";
2276
0
  len[0] = 6;
2277
0
  addr[1] = &r0_key_data[q];
2278
0
  len[1] = 16;
2279
2280
0
  res = -1;
2281
#ifdef CONFIG_SHA512
2282
  if (q == SHA512_MAC_LEN)
2283
    res = sha512_vector(2, addr, len, hash);
2284
#endif /* CONFIG_SHA512 */
2285
0
#ifdef CONFIG_SHA384
2286
0
  if (q == SHA384_MAC_LEN)
2287
0
    res = sha384_vector(2, addr, len, hash);
2288
0
#endif /* CONFIG_SHA384 */
2289
0
  if (q == SHA256_MAC_LEN)
2290
0
    res = sha256_vector(2, addr, len, hash);
2291
0
  if (res < 0) {
2292
0
    wpa_printf(MSG_DEBUG,
2293
0
         "FT: Failed to derive PMKR0Name (PMK-R0 len %zu)",
2294
0
         q);
2295
0
    return res;
2296
0
  }
2297
0
  os_memcpy(pmk_r0_name, hash, WPA_PMK_NAME_LEN);
2298
0
  wpa_hexdump(MSG_DEBUG, "FT: PMKR0Name", pmk_r0_name, WPA_PMK_NAME_LEN);
2299
0
  forced_memzero(r0_key_data, sizeof(r0_key_data));
2300
0
  return 0;
2301
0
}
2302
2303
2304
/**
2305
 * wpa_derive_pmk_r1_name - Derive PMKR1Name
2306
 *
2307
 * IEEE Std 802.11r-2008 - 8.5.1.5.4
2308
 */
2309
int wpa_derive_pmk_r1_name(const u8 *pmk_r0_name, const u8 *r1kh_id,
2310
         const u8 *s1kh_id, u8 *pmk_r1_name,
2311
         size_t pmk_r1_len)
2312
0
{
2313
0
  u8 hash[64];
2314
0
  const u8 *addr[4];
2315
0
  size_t len[4];
2316
0
  int res;
2317
0
  const char *title;
2318
2319
  /*
2320
   * PMKR1Name = Truncate-128(Hash("FT-R1N" || PMKR0Name ||
2321
   *                               R1KH-ID || S1KH-ID))
2322
   */
2323
0
  addr[0] = (const u8 *) "FT-R1N";
2324
0
  len[0] = 6;
2325
0
  addr[1] = pmk_r0_name;
2326
0
  len[1] = WPA_PMK_NAME_LEN;
2327
0
  addr[2] = r1kh_id;
2328
0
  len[2] = FT_R1KH_ID_LEN;
2329
0
  addr[3] = s1kh_id;
2330
0
  len[3] = ETH_ALEN;
2331
2332
0
  res = -1;
2333
#ifdef CONFIG_SHA512
2334
  if (pmk_r1_len == SHA512_MAC_LEN) {
2335
    title = "FT: PMKR1Name (using SHA512)";
2336
    res = sha512_vector(4, addr, len, hash);
2337
  }
2338
#endif /* CONFIG_SHA512 */
2339
0
#ifdef CONFIG_SHA384
2340
0
  if (pmk_r1_len == SHA384_MAC_LEN) {
2341
0
    title = "FT: PMKR1Name (using SHA384)";
2342
0
    res = sha384_vector(4, addr, len, hash);
2343
0
  }
2344
0
#endif /* CONFIG_SHA384 */
2345
0
  if (pmk_r1_len == SHA256_MAC_LEN) {
2346
0
    title = "FT: PMKR1Name (using SHA256)";
2347
0
    res = sha256_vector(4, addr, len, hash);
2348
0
  }
2349
0
  if (res < 0) {
2350
0
    wpa_printf(MSG_DEBUG,
2351
0
         "FT: Failed to derive PMKR1Name (PMK-R1 len %zu)",
2352
0
         pmk_r1_len);
2353
0
    return res;
2354
0
  }
2355
0
  os_memcpy(pmk_r1_name, hash, WPA_PMK_NAME_LEN);
2356
0
  wpa_hexdump(MSG_DEBUG, title, pmk_r1_name, WPA_PMK_NAME_LEN);
2357
0
  return 0;
2358
0
}
2359
2360
2361
/**
2362
 * wpa_derive_pmk_r1 - Derive PMK-R1 and PMKR1Name from PMK-R0
2363
 *
2364
 * IEEE Std 802.11r-2008 - 8.5.1.5.4
2365
 */
2366
int wpa_derive_pmk_r1(const u8 *pmk_r0, size_t pmk_r0_len,
2367
          const u8 *pmk_r0_name,
2368
          const u8 *r1kh_id, const u8 *s1kh_id,
2369
          u8 *pmk_r1, u8 *pmk_r1_name)
2370
0
{
2371
0
  u8 buf[FT_R1KH_ID_LEN + ETH_ALEN];
2372
0
  u8 *pos;
2373
0
  int res;
2374
2375
  /* PMK-R1 = KDF-Hash(PMK-R0, "FT-R1", R1KH-ID || S1KH-ID) */
2376
0
  wpa_printf(MSG_DEBUG, "FT: Derive PMK-R1 using KDF-SHA%zu",
2377
0
       pmk_r0_len * 8);
2378
0
  wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R0", pmk_r0, pmk_r0_len);
2379
0
  wpa_hexdump(MSG_DEBUG, "FT: R1KH-ID", r1kh_id, FT_R1KH_ID_LEN);
2380
0
  wpa_printf(MSG_DEBUG, "FT: S1KH-ID: " MACSTR, MAC2STR(s1kh_id));
2381
0
  pos = buf;
2382
0
  os_memcpy(pos, r1kh_id, FT_R1KH_ID_LEN);
2383
0
  pos += FT_R1KH_ID_LEN;
2384
0
  os_memcpy(pos, s1kh_id, ETH_ALEN);
2385
0
  pos += ETH_ALEN;
2386
2387
0
  res = -1;
2388
#ifdef CONFIG_SHA512
2389
  if (pmk_r0_len == SHA512_MAC_LEN)
2390
    res = sha512_prf(pmk_r0, pmk_r0_len, "FT-R1",
2391
         buf, pos - buf, pmk_r1, pmk_r0_len);
2392
#endif /* CONFIG_SHA512 */
2393
0
#ifdef CONFIG_SHA384
2394
0
  if (pmk_r0_len == SHA384_MAC_LEN)
2395
0
    res = sha384_prf(pmk_r0, pmk_r0_len, "FT-R1",
2396
0
         buf, pos - buf, pmk_r1, pmk_r0_len);
2397
0
#endif /* CONFIG_SHA384 */
2398
0
  if (pmk_r0_len == SHA256_MAC_LEN)
2399
0
    res = sha256_prf(pmk_r0, pmk_r0_len, "FT-R1",
2400
0
         buf, pos - buf, pmk_r1, pmk_r0_len);
2401
0
  if (res < 0) {
2402
0
    wpa_printf(MSG_ERROR, "FT: Failed to derive PMK-R1");
2403
0
    return res;
2404
0
  }
2405
0
  wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", pmk_r1, pmk_r0_len);
2406
2407
0
  return wpa_derive_pmk_r1_name(pmk_r0_name, r1kh_id, s1kh_id,
2408
0
              pmk_r1_name, pmk_r0_len);
2409
0
}
2410
2411
2412
/**
2413
 * wpa_pmk_r1_to_ptk - Derive PTK and PTKName from PMK-R1
2414
 *
2415
 * IEEE Std 802.11r-2008 - 8.5.1.5.5
2416
 */
2417
int wpa_pmk_r1_to_ptk(const u8 *pmk_r1, size_t pmk_r1_len,
2418
          const u8 *snonce, const u8 *anonce,
2419
          const u8 *sta_addr, const u8 *bssid,
2420
          const u8 *pmk_r1_name,
2421
          struct wpa_ptk *ptk, u8 *ptk_name, int akmp, int cipher,
2422
          size_t kdk_len)
2423
0
{
2424
0
  u8 buf[2 * WPA_NONCE_LEN + 2 * ETH_ALEN];
2425
0
  u8 *pos, hash[32];
2426
0
  const u8 *addr[6];
2427
0
  size_t len[6];
2428
0
  u8 tmp[2 * WPA_KCK_MAX_LEN + 2 * WPA_KEK_MAX_LEN + WPA_TK_MAX_LEN +
2429
0
         WPA_KDK_MAX_LEN];
2430
0
  size_t ptk_len, offset;
2431
0
  size_t key_len;
2432
0
  int res;
2433
2434
0
  if (kdk_len > WPA_KDK_MAX_LEN) {
2435
0
    wpa_printf(MSG_ERROR,
2436
0
         "FT: KDK len=%zu exceeds max supported len",
2437
0
         kdk_len);
2438
0
    return -1;
2439
0
  }
2440
2441
0
  if (akmp == WPA_KEY_MGMT_FT_SAE_EXT_KEY &&
2442
0
      (pmk_r1_len == SHA256_MAC_LEN || pmk_r1_len == SHA384_MAC_LEN ||
2443
0
       pmk_r1_len == SHA512_MAC_LEN))
2444
0
    key_len = pmk_r1_len;
2445
0
  else if (wpa_key_mgmt_sha384(akmp))
2446
0
    key_len = SHA384_MAC_LEN;
2447
0
  else
2448
0
    key_len = SHA256_MAC_LEN;
2449
2450
  /*
2451
   * PTK = KDF-PTKLen(PMK-R1, "FT-PTK", SNonce || ANonce ||
2452
   *                  BSSID || STA-ADDR)
2453
   */
2454
0
  wpa_printf(MSG_DEBUG, "FT: Derive PTK using KDF-SHA%zu", key_len * 8);
2455
0
  wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", pmk_r1, pmk_r1_len);
2456
0
  wpa_hexdump(MSG_DEBUG, "FT: SNonce", snonce, WPA_NONCE_LEN);
2457
0
  wpa_hexdump(MSG_DEBUG, "FT: ANonce", anonce, WPA_NONCE_LEN);
2458
0
  wpa_printf(MSG_DEBUG, "FT: BSSID=" MACSTR " STA-ADDR=" MACSTR,
2459
0
       MAC2STR(bssid), MAC2STR(sta_addr));
2460
0
  pos = buf;
2461
0
  os_memcpy(pos, snonce, WPA_NONCE_LEN);
2462
0
  pos += WPA_NONCE_LEN;
2463
0
  os_memcpy(pos, anonce, WPA_NONCE_LEN);
2464
0
  pos += WPA_NONCE_LEN;
2465
0
  os_memcpy(pos, bssid, ETH_ALEN);
2466
0
  pos += ETH_ALEN;
2467
0
  os_memcpy(pos, sta_addr, ETH_ALEN);
2468
0
  pos += ETH_ALEN;
2469
2470
0
  ptk->kck_len = wpa_kck_len(akmp, key_len);
2471
0
  ptk->kck2_len = wpa_kck2_len(akmp);
2472
0
  ptk->kek_len = wpa_kek_len(akmp, key_len);
2473
0
  ptk->kek2_len = wpa_kek2_len(akmp);
2474
0
  ptk->tk_len = wpa_cipher_key_len(cipher);
2475
0
  ptk->kdk_len = kdk_len;
2476
0
  ptk_len = ptk->kck_len + ptk->kek_len + ptk->tk_len +
2477
0
    ptk->kck2_len + ptk->kek2_len + ptk->kdk_len;
2478
2479
0
  res = -1;
2480
#ifdef CONFIG_SHA512
2481
  if (key_len == SHA512_MAC_LEN) {
2482
    if (pmk_r1_len != SHA512_MAC_LEN) {
2483
      wpa_printf(MSG_ERROR,
2484
           "FT: Unexpected PMK-R1 length %d (expected %d)",
2485
           (int) pmk_r1_len, SHA512_MAC_LEN);
2486
      return -1;
2487
    }
2488
    res = sha512_prf(pmk_r1, pmk_r1_len, "FT-PTK",
2489
         buf, pos - buf, tmp, ptk_len);
2490
  }
2491
#endif /* CONFIG_SHA512 */
2492
0
#ifdef CONFIG_SHA384
2493
0
  if (key_len == SHA384_MAC_LEN) {
2494
0
    if (pmk_r1_len != SHA384_MAC_LEN) {
2495
0
      wpa_printf(MSG_ERROR,
2496
0
           "FT: Unexpected PMK-R1 length %d (expected %d)",
2497
0
           (int) pmk_r1_len, SHA384_MAC_LEN);
2498
0
      return -1;
2499
0
    }
2500
0
    res = sha384_prf(pmk_r1, pmk_r1_len, "FT-PTK",
2501
0
         buf, pos - buf, tmp, ptk_len);
2502
0
  }
2503
0
#endif /* CONFIG_SHA384 */
2504
0
  if (key_len == SHA256_MAC_LEN) {
2505
0
    if (pmk_r1_len != PMK_LEN) {
2506
0
      wpa_printf(MSG_ERROR,
2507
0
           "FT: Unexpected PMK-R1 length %d (expected %d)",
2508
0
           (int) pmk_r1_len, PMK_LEN);
2509
0
      return -1;
2510
0
    }
2511
0
    res = sha256_prf(pmk_r1, pmk_r1_len, "FT-PTK",
2512
0
         buf, pos - buf, tmp, ptk_len);
2513
0
  }
2514
0
  if (res < 0)
2515
0
    return -1;
2516
0
  wpa_hexdump_key(MSG_DEBUG, "FT: PTK", tmp, ptk_len);
2517
2518
  /*
2519
   * PTKName = Truncate-128(SHA-256(PMKR1Name || "FT-PTKN" || SNonce ||
2520
   *                                ANonce || BSSID || STA-ADDR))
2521
   */
2522
0
  wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", pmk_r1_name, WPA_PMK_NAME_LEN);
2523
0
  addr[0] = pmk_r1_name;
2524
0
  len[0] = WPA_PMK_NAME_LEN;
2525
0
  addr[1] = (const u8 *) "FT-PTKN";
2526
0
  len[1] = 7;
2527
0
  addr[2] = snonce;
2528
0
  len[2] = WPA_NONCE_LEN;
2529
0
  addr[3] = anonce;
2530
0
  len[3] = WPA_NONCE_LEN;
2531
0
  addr[4] = bssid;
2532
0
  len[4] = ETH_ALEN;
2533
0
  addr[5] = sta_addr;
2534
0
  len[5] = ETH_ALEN;
2535
2536
0
  if (sha256_vector(6, addr, len, hash) < 0)
2537
0
    return -1;
2538
0
  os_memcpy(ptk_name, hash, WPA_PMK_NAME_LEN);
2539
2540
0
  os_memcpy(ptk->kck, tmp, ptk->kck_len);
2541
0
  offset = ptk->kck_len;
2542
0
  os_memcpy(ptk->kek, tmp + offset, ptk->kek_len);
2543
0
  offset += ptk->kek_len;
2544
0
  os_memcpy(ptk->tk, tmp + offset, ptk->tk_len);
2545
0
  offset += ptk->tk_len;
2546
0
  os_memcpy(ptk->kck2, tmp + offset, ptk->kck2_len);
2547
0
  offset += ptk->kck2_len;
2548
0
  os_memcpy(ptk->kek2, tmp + offset, ptk->kek2_len);
2549
0
  offset += ptk->kek2_len;
2550
0
  os_memcpy(ptk->kdk, tmp + offset, ptk->kdk_len);
2551
2552
0
  wpa_hexdump_key(MSG_DEBUG, "FT: KCK", ptk->kck, ptk->kck_len);
2553
0
  wpa_hexdump_key(MSG_DEBUG, "FT: KEK", ptk->kek, ptk->kek_len);
2554
0
  if (ptk->kck2_len)
2555
0
    wpa_hexdump_key(MSG_DEBUG, "FT: KCK2",
2556
0
        ptk->kck2, ptk->kck2_len);
2557
0
  if (ptk->kek2_len)
2558
0
    wpa_hexdump_key(MSG_DEBUG, "FT: KEK2",
2559
0
        ptk->kek2, ptk->kek2_len);
2560
0
  if (ptk->kdk_len)
2561
0
    wpa_hexdump_key(MSG_DEBUG, "FT: KDK", ptk->kdk, ptk->kdk_len);
2562
2563
0
  wpa_hexdump_key(MSG_DEBUG, "FT: TK", ptk->tk, ptk->tk_len);
2564
0
  wpa_hexdump(MSG_DEBUG, "FT: PTKName", ptk_name, WPA_PMK_NAME_LEN);
2565
2566
0
  forced_memzero(tmp, sizeof(tmp));
2567
2568
0
  return 0;
2569
0
}
2570
2571
#endif /* CONFIG_IEEE80211R */
2572
2573
2574
/**
2575
 * rsn_pmkid - Calculate PMK identifier
2576
 * @pmk: Pairwise master key
2577
 * @pmk_len: Length of pmk in bytes
2578
 * @aa: Authenticator address
2579
 * @spa: Supplicant address
2580
 * @pmkid: Buffer for PMKID
2581
 * @akmp: Negotiated key management protocol
2582
 *
2583
 * IEEE Std 802.11-2016 - 12.7.1.3 Pairwise key hierarchy
2584
 * AKM: 00-0F-AC:3, 00-0F-AC:5, 00-0F-AC:6, 00-0F-AC:14, 00-0F-AC:16
2585
 * PMKID = Truncate-128(HMAC-SHA-256(PMK, "PMK Name" || AA || SPA))
2586
 * AKM: 00-0F-AC:11
2587
 * See rsn_pmkid_suite_b()
2588
 * AKM: 00-0F-AC:12
2589
 * See rsn_pmkid_suite_b_192()
2590
 * AKM: 00-0F-AC:13, 00-0F-AC:15, 00-0F-AC:17
2591
 * PMKID = Truncate-128(HMAC-SHA-384(PMK, "PMK Name" || AA || SPA))
2592
 * Otherwise:
2593
 * PMKID = Truncate-128(HMAC-SHA-1(PMK, "PMK Name" || AA || SPA))
2594
 */
2595
void rsn_pmkid(const u8 *pmk, size_t pmk_len, const u8 *aa, const u8 *spa,
2596
         u8 *pmkid, int akmp)
2597
0
{
2598
0
  char *title = "PMK Name";
2599
0
  const u8 *addr[3];
2600
0
  const size_t len[3] = { 8, ETH_ALEN, ETH_ALEN };
2601
0
  unsigned char hash[SHA384_MAC_LEN];
2602
2603
0
  addr[0] = (u8 *) title;
2604
0
  addr[1] = aa;
2605
0
  addr[2] = spa;
2606
2607
0
  if (0) {
2608
0
#if defined(CONFIG_FILS) || defined(CONFIG_SHA384)
2609
0
  } else if (wpa_key_mgmt_sha384(akmp)) {
2610
0
    wpa_printf(MSG_DEBUG, "RSN: Derive PMKID using HMAC-SHA-384");
2611
0
    hmac_sha384_vector(pmk, pmk_len, 3, addr, len, hash);
2612
0
#endif /* CONFIG_FILS || CONFIG_SHA384 */
2613
0
  } else if (wpa_key_mgmt_sha256(akmp)) {
2614
0
    wpa_printf(MSG_DEBUG, "RSN: Derive PMKID using HMAC-SHA-256");
2615
0
    hmac_sha256_vector(pmk, pmk_len, 3, addr, len, hash);
2616
0
  } else {
2617
0
    wpa_printf(MSG_DEBUG, "RSN: Derive PMKID using HMAC-SHA-1");
2618
0
    hmac_sha1_vector(pmk, pmk_len, 3, addr, len, hash);
2619
0
  }
2620
0
  wpa_hexdump(MSG_DEBUG, "RSN: Derived PMKID", hash, PMKID_LEN);
2621
0
  os_memcpy(pmkid, hash, PMKID_LEN);
2622
0
}
2623
2624
2625
#ifdef CONFIG_SUITEB
2626
/**
2627
 * rsn_pmkid_suite_b - Calculate PMK identifier for Suite B AKM
2628
 * @kck: Key confirmation key
2629
 * @kck_len: Length of kck in bytes
2630
 * @aa: Authenticator address
2631
 * @spa: Supplicant address
2632
 * @pmkid: Buffer for PMKID
2633
 * Returns: 0 on success, -1 on failure
2634
 *
2635
 * IEEE Std 802.11ac-2013 - 11.6.1.3 Pairwise key hierarchy
2636
 * PMKID = Truncate(HMAC-SHA-256(KCK, "PMK Name" || AA || SPA))
2637
 */
2638
int rsn_pmkid_suite_b(const u8 *kck, size_t kck_len, const u8 *aa,
2639
          const u8 *spa, u8 *pmkid)
2640
{
2641
  char *title = "PMK Name";
2642
  const u8 *addr[3];
2643
  const size_t len[3] = { 8, ETH_ALEN, ETH_ALEN };
2644
  unsigned char hash[SHA256_MAC_LEN];
2645
2646
  addr[0] = (u8 *) title;
2647
  addr[1] = aa;
2648
  addr[2] = spa;
2649
2650
  if (hmac_sha256_vector(kck, kck_len, 3, addr, len, hash) < 0)
2651
    return -1;
2652
  os_memcpy(pmkid, hash, PMKID_LEN);
2653
  return 0;
2654
}
2655
#endif /* CONFIG_SUITEB */
2656
2657
2658
#ifdef CONFIG_SUITEB192
2659
/**
2660
 * rsn_pmkid_suite_b_192 - Calculate PMK identifier for Suite B AKM
2661
 * @kck: Key confirmation key
2662
 * @kck_len: Length of kck in bytes
2663
 * @aa: Authenticator address
2664
 * @spa: Supplicant address
2665
 * @pmkid: Buffer for PMKID
2666
 * Returns: 0 on success, -1 on failure
2667
 *
2668
 * IEEE Std 802.11ac-2013 - 11.6.1.3 Pairwise key hierarchy
2669
 * PMKID = Truncate(HMAC-SHA-384(KCK, "PMK Name" || AA || SPA))
2670
 */
2671
int rsn_pmkid_suite_b_192(const u8 *kck, size_t kck_len, const u8 *aa,
2672
        const u8 *spa, u8 *pmkid)
2673
{
2674
  char *title = "PMK Name";
2675
  const u8 *addr[3];
2676
  const size_t len[3] = { 8, ETH_ALEN, ETH_ALEN };
2677
  unsigned char hash[SHA384_MAC_LEN];
2678
2679
  addr[0] = (u8 *) title;
2680
  addr[1] = aa;
2681
  addr[2] = spa;
2682
2683
  if (hmac_sha384_vector(kck, kck_len, 3, addr, len, hash) < 0)
2684
    return -1;
2685
  os_memcpy(pmkid, hash, PMKID_LEN);
2686
  return 0;
2687
}
2688
#endif /* CONFIG_SUITEB192 */
2689
2690
2691
/**
2692
 * wpa_cipher_txt - Convert cipher suite to a text string
2693
 * @cipher: Cipher suite (WPA_CIPHER_* enum)
2694
 * Returns: Pointer to a text string of the cipher suite name
2695
 */
2696
const char * wpa_cipher_txt(int cipher)
2697
17
{
2698
17
  switch (cipher) {
2699
0
  case WPA_CIPHER_NONE:
2700
0
    return "NONE";
2701
#ifdef CONFIG_WEP
2702
  case WPA_CIPHER_WEP40:
2703
    return "WEP-40";
2704
  case WPA_CIPHER_WEP104:
2705
    return "WEP-104";
2706
#endif /* CONFIG_WEP */
2707
0
  case WPA_CIPHER_TKIP:
2708
0
    return "TKIP";
2709
17
  case WPA_CIPHER_CCMP:
2710
17
    return "CCMP";
2711
0
  case WPA_CIPHER_CCMP | WPA_CIPHER_TKIP:
2712
0
    return "CCMP+TKIP";
2713
0
  case WPA_CIPHER_GCMP:
2714
0
    return "GCMP";
2715
0
  case WPA_CIPHER_GCMP_256:
2716
0
    return "GCMP-256";
2717
0
  case WPA_CIPHER_CCMP_256:
2718
0
    return "CCMP-256";
2719
0
  case WPA_CIPHER_AES_128_CMAC:
2720
0
    return "BIP";
2721
0
  case WPA_CIPHER_BIP_GMAC_128:
2722
0
    return "BIP-GMAC-128";
2723
0
  case WPA_CIPHER_BIP_GMAC_256:
2724
0
    return "BIP-GMAC-256";
2725
0
  case WPA_CIPHER_BIP_CMAC_256:
2726
0
    return "BIP-CMAC-256";
2727
0
  case WPA_CIPHER_GTK_NOT_USED:
2728
0
    return "GTK_NOT_USED";
2729
0
  default:
2730
0
    return "UNKNOWN";
2731
17
  }
2732
17
}
2733
2734
2735
/**
2736
 * wpa_key_mgmt_txt - Convert key management suite to a text string
2737
 * @key_mgmt: Key management suite (WPA_KEY_MGMT_* enum)
2738
 * @proto: WPA/WPA2 version (WPA_PROTO_*)
2739
 * Returns: Pointer to a text string of the key management suite name
2740
 */
2741
const char * wpa_key_mgmt_txt(int key_mgmt, int proto)
2742
0
{
2743
0
  switch (key_mgmt) {
2744
0
  case WPA_KEY_MGMT_IEEE8021X:
2745
0
    if (proto == (WPA_PROTO_RSN | WPA_PROTO_WPA))
2746
0
      return "WPA2+WPA/IEEE 802.1X/EAP";
2747
0
    return proto == WPA_PROTO_RSN ?
2748
0
      "WPA2/IEEE 802.1X/EAP" : "WPA/IEEE 802.1X/EAP";
2749
0
  case WPA_KEY_MGMT_PSK:
2750
0
    if (proto == (WPA_PROTO_RSN | WPA_PROTO_WPA))
2751
0
      return "WPA2-PSK+WPA-PSK";
2752
0
    return proto == WPA_PROTO_RSN ?
2753
0
      "WPA2-PSK" : "WPA-PSK";
2754
0
  case WPA_KEY_MGMT_NONE:
2755
0
    return "NONE";
2756
0
  case WPA_KEY_MGMT_WPA_NONE:
2757
0
    return "WPA-NONE";
2758
0
  case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
2759
0
    return "IEEE 802.1X (no WPA)";
2760
0
#ifdef CONFIG_IEEE80211R
2761
0
  case WPA_KEY_MGMT_FT_IEEE8021X:
2762
0
    return "FT-EAP";
2763
0
  case WPA_KEY_MGMT_FT_IEEE8021X_SHA384:
2764
0
    return "FT-EAP-SHA384";
2765
0
  case WPA_KEY_MGMT_FT_PSK:
2766
0
    return "FT-PSK";
2767
0
#endif /* CONFIG_IEEE80211R */
2768
0
  case WPA_KEY_MGMT_IEEE8021X_SHA256:
2769
0
    return "WPA2-EAP-SHA256";
2770
0
  case WPA_KEY_MGMT_PSK_SHA256:
2771
0
    return "WPA2-PSK-SHA256";
2772
0
  case WPA_KEY_MGMT_WPS:
2773
0
    return "WPS";
2774
0
  case WPA_KEY_MGMT_SAE:
2775
0
    return "SAE";
2776
0
  case WPA_KEY_MGMT_SAE_EXT_KEY:
2777
0
    return "SAE-EXT-KEY";
2778
0
  case WPA_KEY_MGMT_FT_SAE:
2779
0
    return "FT-SAE";
2780
0
  case WPA_KEY_MGMT_FT_SAE_EXT_KEY:
2781
0
    return "FT-SAE-EXT-KEY";
2782
0
  case WPA_KEY_MGMT_OSEN:
2783
0
    return "OSEN";
2784
0
  case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
2785
0
    return "WPA2-EAP-SUITE-B";
2786
0
  case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
2787
0
    return "WPA2-EAP-SUITE-B-192";
2788
0
  case WPA_KEY_MGMT_FILS_SHA256:
2789
0
    return "FILS-SHA256";
2790
0
  case WPA_KEY_MGMT_FILS_SHA384:
2791
0
    return "FILS-SHA384";
2792
0
  case WPA_KEY_MGMT_FT_FILS_SHA256:
2793
0
    return "FT-FILS-SHA256";
2794
0
  case WPA_KEY_MGMT_FT_FILS_SHA384:
2795
0
    return "FT-FILS-SHA384";
2796
0
  case WPA_KEY_MGMT_OWE:
2797
0
    return "OWE";
2798
0
  case WPA_KEY_MGMT_DPP:
2799
0
    return "DPP";
2800
0
  case WPA_KEY_MGMT_PASN:
2801
0
    return "PASN";
2802
0
  case WPA_KEY_MGMT_IEEE8021X_SHA384:
2803
0
    return "WPA2-EAP-SHA384";
2804
0
  default:
2805
0
    return "UNKNOWN";
2806
0
  }
2807
0
}
2808
2809
2810
u32 wpa_akm_to_suite(int akm)
2811
0
{
2812
0
  if (akm & WPA_KEY_MGMT_FT_IEEE8021X_SHA384)
2813
0
    return RSN_AUTH_KEY_MGMT_FT_802_1X_SHA384;
2814
0
  if (akm & WPA_KEY_MGMT_FT_IEEE8021X)
2815
0
    return RSN_AUTH_KEY_MGMT_FT_802_1X;
2816
0
  if (akm & WPA_KEY_MGMT_FT_PSK)
2817
0
    return RSN_AUTH_KEY_MGMT_FT_PSK;
2818
0
  if (akm & WPA_KEY_MGMT_IEEE8021X_SHA384)
2819
0
    return RSN_AUTH_KEY_MGMT_802_1X_SHA384;
2820
0
  if (akm & WPA_KEY_MGMT_IEEE8021X_SHA256)
2821
0
    return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
2822
0
  if (akm & WPA_KEY_MGMT_IEEE8021X)
2823
0
    return RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
2824
0
  if (akm & WPA_KEY_MGMT_PSK_SHA256)
2825
0
    return RSN_AUTH_KEY_MGMT_PSK_SHA256;
2826
0
  if (akm & WPA_KEY_MGMT_PSK)
2827
0
    return RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
2828
0
  if (akm & WPA_KEY_MGMT_CCKM)
2829
0
    return RSN_AUTH_KEY_MGMT_CCKM;
2830
0
  if (akm & WPA_KEY_MGMT_OSEN)
2831
0
    return RSN_AUTH_KEY_MGMT_OSEN;
2832
0
  if (akm & WPA_KEY_MGMT_IEEE8021X_SUITE_B)
2833
0
    return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
2834
0
  if (akm & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
2835
0
    return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
2836
0
  if (akm & WPA_KEY_MGMT_FILS_SHA256)
2837
0
    return RSN_AUTH_KEY_MGMT_FILS_SHA256;
2838
0
  if (akm & WPA_KEY_MGMT_FILS_SHA384)
2839
0
    return RSN_AUTH_KEY_MGMT_FILS_SHA384;
2840
0
  if (akm & WPA_KEY_MGMT_FT_FILS_SHA256)
2841
0
    return RSN_AUTH_KEY_MGMT_FT_FILS_SHA256;
2842
0
  if (akm & WPA_KEY_MGMT_FT_FILS_SHA384)
2843
0
    return RSN_AUTH_KEY_MGMT_FT_FILS_SHA384;
2844
0
  if (akm & WPA_KEY_MGMT_SAE)
2845
0
    return RSN_AUTH_KEY_MGMT_SAE;
2846
0
  if (akm & WPA_KEY_MGMT_SAE_EXT_KEY)
2847
0
    return RSN_AUTH_KEY_MGMT_SAE_EXT_KEY;
2848
0
  if (akm & WPA_KEY_MGMT_FT_SAE)
2849
0
    return RSN_AUTH_KEY_MGMT_FT_SAE;
2850
0
  if (akm & WPA_KEY_MGMT_FT_SAE_EXT_KEY)
2851
0
    return RSN_AUTH_KEY_MGMT_FT_SAE_EXT_KEY;
2852
0
  if (akm & WPA_KEY_MGMT_OWE)
2853
0
    return RSN_AUTH_KEY_MGMT_OWE;
2854
0
  if (akm & WPA_KEY_MGMT_DPP)
2855
0
    return RSN_AUTH_KEY_MGMT_DPP;
2856
0
  return 0;
2857
0
}
2858
2859
2860
int wpa_compare_rsn_ie(int ft_initial_assoc,
2861
           const u8 *ie1, size_t ie1len,
2862
           const u8 *ie2, size_t ie2len)
2863
1.62k
{
2864
1.62k
  if (ie1 == NULL || ie2 == NULL)
2865
0
    return -1;
2866
2867
1.62k
  if (ie1len == ie2len && os_memcmp(ie1, ie2, ie1len) == 0)
2868
1.59k
    return 0; /* identical IEs */
2869
2870
30
#ifdef CONFIG_IEEE80211R
2871
30
  if (ft_initial_assoc) {
2872
0
    struct wpa_ie_data ie1d, ie2d;
2873
    /*
2874
     * The PMKID-List in RSN IE is different between Beacon/Probe
2875
     * Response/(Re)Association Request frames and EAPOL-Key
2876
     * messages in FT initial mobility domain association. Allow
2877
     * for this, but verify that other parts of the RSN IEs are
2878
     * identical.
2879
     */
2880
0
    if (wpa_parse_wpa_ie_rsn(ie1, ie1len, &ie1d) < 0 ||
2881
0
        wpa_parse_wpa_ie_rsn(ie2, ie2len, &ie2d) < 0)
2882
0
      return -1;
2883
0
    if (ie1d.proto == ie2d.proto &&
2884
0
        ie1d.pairwise_cipher == ie2d.pairwise_cipher &&
2885
0
        ie1d.group_cipher == ie2d.group_cipher &&
2886
0
        ie1d.key_mgmt == ie2d.key_mgmt &&
2887
0
        ie1d.capabilities == ie2d.capabilities &&
2888
0
        ie1d.mgmt_group_cipher == ie2d.mgmt_group_cipher)
2889
0
      return 0;
2890
0
  }
2891
30
#endif /* CONFIG_IEEE80211R */
2892
2893
30
  return -1;
2894
30
}
2895
2896
2897
int wpa_insert_pmkid(u8 *ies, size_t *ies_len, const u8 *pmkid)
2898
0
{
2899
0
  u8 *start, *end, *rpos, *rend;
2900
0
  int added = 0;
2901
2902
0
  start = ies;
2903
0
  end = ies + *ies_len;
2904
2905
0
  while (start < end) {
2906
0
    if (*start == WLAN_EID_RSN)
2907
0
      break;
2908
0
    start += 2 + start[1];
2909
0
  }
2910
0
  if (start >= end) {
2911
0
    wpa_printf(MSG_ERROR, "RSN: Could not find RSNE in IEs data");
2912
0
    return -1;
2913
0
  }
2914
0
  wpa_hexdump(MSG_DEBUG, "RSN: RSNE before modification",
2915
0
        start, 2 + start[1]);
2916
2917
  /* Find start of PMKID-Count */
2918
0
  rpos = start + 2;
2919
0
  rend = rpos + start[1];
2920
2921
  /* Skip Version and Group Data Cipher Suite */
2922
0
  rpos += 2 + 4;
2923
  /* Skip Pairwise Cipher Suite Count and List */
2924
0
  rpos += 2 + WPA_GET_LE16(rpos) * RSN_SELECTOR_LEN;
2925
  /* Skip AKM Suite Count and List */
2926
0
  rpos += 2 + WPA_GET_LE16(rpos) * RSN_SELECTOR_LEN;
2927
2928
0
  if (rpos == rend) {
2929
    /* Add RSN Capabilities */
2930
0
    os_memmove(rpos + 2, rpos, end - rpos);
2931
0
    *rpos++ = 0;
2932
0
    *rpos++ = 0;
2933
0
    added += 2;
2934
0
    start[1] += 2;
2935
0
    rend = rpos;
2936
0
  } else {
2937
    /* Skip RSN Capabilities */
2938
0
    rpos += 2;
2939
0
    if (rpos > rend) {
2940
0
      wpa_printf(MSG_ERROR,
2941
0
           "RSN: Could not parse RSNE in IEs data");
2942
0
      return -1;
2943
0
    }
2944
0
  }
2945
2946
0
  if (rpos == rend) {
2947
    /* No PMKID-Count field included; add it */
2948
0
    os_memmove(rpos + 2 + PMKID_LEN, rpos, end + added - rpos);
2949
0
    WPA_PUT_LE16(rpos, 1);
2950
0
    rpos += 2;
2951
0
    os_memcpy(rpos, pmkid, PMKID_LEN);
2952
0
    added += 2 + PMKID_LEN;
2953
0
    start[1] += 2 + PMKID_LEN;
2954
0
  } else {
2955
0
    u16 num_pmkid;
2956
2957
0
    if (rend - rpos < 2)
2958
0
      return -1;
2959
0
    num_pmkid = WPA_GET_LE16(rpos);
2960
    /* PMKID-Count was included; use it */
2961
0
    if (num_pmkid != 0) {
2962
0
      u8 *after;
2963
2964
0
      if (num_pmkid * PMKID_LEN > rend - rpos - 2)
2965
0
        return -1;
2966
      /*
2967
       * PMKID may have been included in RSN IE in
2968
       * (Re)Association Request frame, so remove the old
2969
       * PMKID(s) first before adding the new one.
2970
       */
2971
0
      wpa_printf(MSG_DEBUG,
2972
0
           "RSN: Remove %u old PMKID(s) from RSNE",
2973
0
           num_pmkid);
2974
0
      after = rpos + 2 + num_pmkid * PMKID_LEN;
2975
0
      os_memmove(rpos + 2, after, end - after);
2976
0
      start[1] -= num_pmkid * PMKID_LEN;
2977
0
      added -= num_pmkid * PMKID_LEN;
2978
0
    }
2979
0
    WPA_PUT_LE16(rpos, 1);
2980
0
    rpos += 2;
2981
0
    os_memmove(rpos + PMKID_LEN, rpos, end + added - rpos);
2982
0
    os_memcpy(rpos, pmkid, PMKID_LEN);
2983
0
    added += PMKID_LEN;
2984
0
    start[1] += PMKID_LEN;
2985
0
  }
2986
2987
0
  wpa_hexdump(MSG_DEBUG, "RSN: RSNE after modification (PMKID inserted)",
2988
0
        start, 2 + start[1]);
2989
2990
0
  *ies_len += added;
2991
2992
0
  return 0;
2993
0
}
2994
2995
2996
int wpa_cipher_key_len(int cipher)
2997
51.7k
{
2998
51.7k
  switch (cipher) {
2999
0
  case WPA_CIPHER_CCMP_256:
3000
0
  case WPA_CIPHER_GCMP_256:
3001
0
  case WPA_CIPHER_BIP_GMAC_256:
3002
0
  case WPA_CIPHER_BIP_CMAC_256:
3003
0
    return 32;
3004
17.9k
  case WPA_CIPHER_CCMP:
3005
17.9k
  case WPA_CIPHER_GCMP:
3006
51.7k
  case WPA_CIPHER_AES_128_CMAC:
3007
51.7k
  case WPA_CIPHER_BIP_GMAC_128:
3008
51.7k
    return 16;
3009
0
  case WPA_CIPHER_TKIP:
3010
0
    return 32;
3011
0
  default:
3012
0
    return 0;
3013
51.7k
  }
3014
51.7k
}
3015
3016
3017
int wpa_cipher_rsc_len(int cipher)
3018
227
{
3019
227
  switch (cipher) {
3020
0
  case WPA_CIPHER_CCMP_256:
3021
0
  case WPA_CIPHER_GCMP_256:
3022
227
  case WPA_CIPHER_CCMP:
3023
227
  case WPA_CIPHER_GCMP:
3024
227
  case WPA_CIPHER_TKIP:
3025
227
    return 6;
3026
0
  default:
3027
0
    return 0;
3028
227
  }
3029
227
}
3030
3031
3032
enum wpa_alg wpa_cipher_to_alg(int cipher)
3033
4.25k
{
3034
4.25k
  switch (cipher) {
3035
0
  case WPA_CIPHER_CCMP_256:
3036
0
    return WPA_ALG_CCMP_256;
3037
0
  case WPA_CIPHER_GCMP_256:
3038
0
    return WPA_ALG_GCMP_256;
3039
2.28k
  case WPA_CIPHER_CCMP:
3040
2.28k
    return WPA_ALG_CCMP;
3041
0
  case WPA_CIPHER_GCMP:
3042
0
    return WPA_ALG_GCMP;
3043
0
  case WPA_CIPHER_TKIP:
3044
0
    return WPA_ALG_TKIP;
3045
1.96k
  case WPA_CIPHER_AES_128_CMAC:
3046
1.96k
    return WPA_ALG_BIP_CMAC_128;
3047
0
  case WPA_CIPHER_BIP_GMAC_128:
3048
0
    return WPA_ALG_BIP_GMAC_128;
3049
0
  case WPA_CIPHER_BIP_GMAC_256:
3050
0
    return WPA_ALG_BIP_GMAC_256;
3051
0
  case WPA_CIPHER_BIP_CMAC_256:
3052
0
    return WPA_ALG_BIP_CMAC_256;
3053
0
  default:
3054
0
    return WPA_ALG_NONE;
3055
4.25k
  }
3056
4.25k
}
3057
3058
3059
int wpa_cipher_valid_pairwise(int cipher)
3060
7.69k
{
3061
#ifdef CONFIG_NO_TKIP
3062
  return cipher == WPA_CIPHER_CCMP_256 ||
3063
    cipher == WPA_CIPHER_GCMP_256 ||
3064
    cipher == WPA_CIPHER_CCMP ||
3065
    cipher == WPA_CIPHER_GCMP;
3066
#else /* CONFIG_NO_TKIP */
3067
7.69k
  return cipher == WPA_CIPHER_CCMP_256 ||
3068
7.69k
    cipher == WPA_CIPHER_GCMP_256 ||
3069
7.69k
    cipher == WPA_CIPHER_CCMP ||
3070
7.69k
    cipher == WPA_CIPHER_GCMP ||
3071
7.69k
    cipher == WPA_CIPHER_TKIP;
3072
7.69k
#endif /* CONFIG_NO_TKIP */
3073
7.69k
}
3074
3075
3076
u32 wpa_cipher_to_suite(int proto, int cipher)
3077
16.1k
{
3078
16.1k
  if (cipher & WPA_CIPHER_CCMP_256)
3079
0
    return RSN_CIPHER_SUITE_CCMP_256;
3080
16.1k
  if (cipher & WPA_CIPHER_GCMP_256)
3081
0
    return RSN_CIPHER_SUITE_GCMP_256;
3082
16.1k
  if (cipher & WPA_CIPHER_CCMP)
3083
16.1k
    return (proto == WPA_PROTO_RSN ?
3084
16.1k
      RSN_CIPHER_SUITE_CCMP : WPA_CIPHER_SUITE_CCMP);
3085
0
  if (cipher & WPA_CIPHER_GCMP)
3086
0
    return RSN_CIPHER_SUITE_GCMP;
3087
0
  if (cipher & WPA_CIPHER_TKIP)
3088
0
    return (proto == WPA_PROTO_RSN ?
3089
0
      RSN_CIPHER_SUITE_TKIP : WPA_CIPHER_SUITE_TKIP);
3090
0
  if (cipher & WPA_CIPHER_NONE)
3091
0
    return (proto == WPA_PROTO_RSN ?
3092
0
      RSN_CIPHER_SUITE_NONE : WPA_CIPHER_SUITE_NONE);
3093
0
  if (cipher & WPA_CIPHER_GTK_NOT_USED)
3094
0
    return RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED;
3095
0
  if (cipher & WPA_CIPHER_AES_128_CMAC)
3096
0
    return RSN_CIPHER_SUITE_AES_128_CMAC;
3097
0
  if (cipher & WPA_CIPHER_BIP_GMAC_128)
3098
0
    return RSN_CIPHER_SUITE_BIP_GMAC_128;
3099
0
  if (cipher & WPA_CIPHER_BIP_GMAC_256)
3100
0
    return RSN_CIPHER_SUITE_BIP_GMAC_256;
3101
0
  if (cipher & WPA_CIPHER_BIP_CMAC_256)
3102
0
    return RSN_CIPHER_SUITE_BIP_CMAC_256;
3103
0
  return 0;
3104
0
}
3105
3106
3107
int rsn_cipher_put_suites(u8 *start, int ciphers)
3108
1.96k
{
3109
1.96k
  u8 *pos = start;
3110
3111
1.96k
  if (ciphers & WPA_CIPHER_CCMP_256) {
3112
0
    RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP_256);
3113
0
    pos += RSN_SELECTOR_LEN;
3114
0
  }
3115
1.96k
  if (ciphers & WPA_CIPHER_GCMP_256) {
3116
0
    RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_GCMP_256);
3117
0
    pos += RSN_SELECTOR_LEN;
3118
0
  }
3119
1.96k
  if (ciphers & WPA_CIPHER_CCMP) {
3120
1.96k
    RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP);
3121
1.96k
    pos += RSN_SELECTOR_LEN;
3122
1.96k
  }
3123
1.96k
  if (ciphers & WPA_CIPHER_GCMP) {
3124
0
    RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_GCMP);
3125
0
    pos += RSN_SELECTOR_LEN;
3126
0
  }
3127
1.96k
  if (ciphers & WPA_CIPHER_TKIP) {
3128
0
    RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_TKIP);
3129
0
    pos += RSN_SELECTOR_LEN;
3130
0
  }
3131
1.96k
  if (ciphers & WPA_CIPHER_NONE) {
3132
0
    RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_NONE);
3133
0
    pos += RSN_SELECTOR_LEN;
3134
0
  }
3135
3136
1.96k
  return (pos - start) / RSN_SELECTOR_LEN;
3137
1.96k
}
3138
3139
3140
int wpa_cipher_put_suites(u8 *start, int ciphers)
3141
0
{
3142
0
  u8 *pos = start;
3143
3144
0
  if (ciphers & WPA_CIPHER_CCMP) {
3145
0
    RSN_SELECTOR_PUT(pos, WPA_CIPHER_SUITE_CCMP);
3146
0
    pos += WPA_SELECTOR_LEN;
3147
0
  }
3148
0
  if (ciphers & WPA_CIPHER_TKIP) {
3149
0
    RSN_SELECTOR_PUT(pos, WPA_CIPHER_SUITE_TKIP);
3150
0
    pos += WPA_SELECTOR_LEN;
3151
0
  }
3152
0
  if (ciphers & WPA_CIPHER_NONE) {
3153
0
    RSN_SELECTOR_PUT(pos, WPA_CIPHER_SUITE_NONE);
3154
0
    pos += WPA_SELECTOR_LEN;
3155
0
  }
3156
3157
0
  return (pos - start) / RSN_SELECTOR_LEN;
3158
0
}
3159
3160
3161
int wpa_pick_pairwise_cipher(int ciphers, int none_allowed)
3162
1.96k
{
3163
1.96k
  if (ciphers & WPA_CIPHER_CCMP_256)
3164
0
    return WPA_CIPHER_CCMP_256;
3165
1.96k
  if (ciphers & WPA_CIPHER_GCMP_256)
3166
0
    return WPA_CIPHER_GCMP_256;
3167
1.96k
  if (ciphers & WPA_CIPHER_CCMP)
3168
1.96k
    return WPA_CIPHER_CCMP;
3169
0
  if (ciphers & WPA_CIPHER_GCMP)
3170
0
    return WPA_CIPHER_GCMP;
3171
0
  if (ciphers & WPA_CIPHER_TKIP)
3172
0
    return WPA_CIPHER_TKIP;
3173
0
  if (none_allowed && (ciphers & WPA_CIPHER_NONE))
3174
0
    return WPA_CIPHER_NONE;
3175
0
  return -1;
3176
0
}
3177
3178
3179
int wpa_pick_group_cipher(int ciphers)
3180
0
{
3181
0
  if (ciphers & WPA_CIPHER_CCMP_256)
3182
0
    return WPA_CIPHER_CCMP_256;
3183
0
  if (ciphers & WPA_CIPHER_GCMP_256)
3184
0
    return WPA_CIPHER_GCMP_256;
3185
0
  if (ciphers & WPA_CIPHER_CCMP)
3186
0
    return WPA_CIPHER_CCMP;
3187
0
  if (ciphers & WPA_CIPHER_GCMP)
3188
0
    return WPA_CIPHER_GCMP;
3189
0
  if (ciphers & WPA_CIPHER_GTK_NOT_USED)
3190
0
    return WPA_CIPHER_GTK_NOT_USED;
3191
0
  if (ciphers & WPA_CIPHER_TKIP)
3192
0
    return WPA_CIPHER_TKIP;
3193
0
  return -1;
3194
0
}
3195
3196
3197
int wpa_parse_cipher(const char *value)
3198
0
{
3199
0
  int val = 0, last;
3200
0
  char *start, *end, *buf;
3201
3202
0
  buf = os_strdup(value);
3203
0
  if (buf == NULL)
3204
0
    return -1;
3205
0
  start = buf;
3206
3207
0
  while (*start != '\0') {
3208
0
    while (*start == ' ' || *start == '\t')
3209
0
      start++;
3210
0
    if (*start == '\0')
3211
0
      break;
3212
0
    end = start;
3213
0
    while (*end != ' ' && *end != '\t' && *end != '\0')
3214
0
      end++;
3215
0
    last = *end == '\0';
3216
0
    *end = '\0';
3217
0
    if (os_strcmp(start, "CCMP-256") == 0)
3218
0
      val |= WPA_CIPHER_CCMP_256;
3219
0
    else if (os_strcmp(start, "GCMP-256") == 0)
3220
0
      val |= WPA_CIPHER_GCMP_256;
3221
0
    else if (os_strcmp(start, "CCMP") == 0)
3222
0
      val |= WPA_CIPHER_CCMP;
3223
0
    else if (os_strcmp(start, "GCMP") == 0)
3224
0
      val |= WPA_CIPHER_GCMP;
3225
0
#ifndef CONFIG_NO_TKIP
3226
0
    else if (os_strcmp(start, "TKIP") == 0)
3227
0
      val |= WPA_CIPHER_TKIP;
3228
0
#endif /* CONFIG_NO_TKIP */
3229
#ifdef CONFIG_WEP
3230
    else if (os_strcmp(start, "WEP104") == 0)
3231
      val |= WPA_CIPHER_WEP104;
3232
    else if (os_strcmp(start, "WEP40") == 0)
3233
      val |= WPA_CIPHER_WEP40;
3234
#endif /* CONFIG_WEP */
3235
0
    else if (os_strcmp(start, "NONE") == 0)
3236
0
      val |= WPA_CIPHER_NONE;
3237
0
    else if (os_strcmp(start, "GTK_NOT_USED") == 0)
3238
0
      val |= WPA_CIPHER_GTK_NOT_USED;
3239
0
    else if (os_strcmp(start, "AES-128-CMAC") == 0)
3240
0
      val |= WPA_CIPHER_AES_128_CMAC;
3241
0
    else if (os_strcmp(start, "BIP-GMAC-128") == 0)
3242
0
      val |= WPA_CIPHER_BIP_GMAC_128;
3243
0
    else if (os_strcmp(start, "BIP-GMAC-256") == 0)
3244
0
      val |= WPA_CIPHER_BIP_GMAC_256;
3245
0
    else if (os_strcmp(start, "BIP-CMAC-256") == 0)
3246
0
      val |= WPA_CIPHER_BIP_CMAC_256;
3247
0
    else {
3248
0
      os_free(buf);
3249
0
      return -1;
3250
0
    }
3251
3252
0
    if (last)
3253
0
      break;
3254
0
    start = end + 1;
3255
0
  }
3256
0
  os_free(buf);
3257
3258
0
  return val;
3259
0
}
3260
3261
3262
int wpa_write_ciphers(char *start, char *end, int ciphers, const char *delim)
3263
0
{
3264
0
  char *pos = start;
3265
0
  int ret;
3266
3267
0
  if (ciphers & WPA_CIPHER_CCMP_256) {
3268
0
    ret = os_snprintf(pos, end - pos, "%sCCMP-256",
3269
0
          pos == start ? "" : delim);
3270
0
    if (os_snprintf_error(end - pos, ret))
3271
0
      return -1;
3272
0
    pos += ret;
3273
0
  }
3274
0
  if (ciphers & WPA_CIPHER_GCMP_256) {
3275
0
    ret = os_snprintf(pos, end - pos, "%sGCMP-256",
3276
0
          pos == start ? "" : delim);
3277
0
    if (os_snprintf_error(end - pos, ret))
3278
0
      return -1;
3279
0
    pos += ret;
3280
0
  }
3281
0
  if (ciphers & WPA_CIPHER_CCMP) {
3282
0
    ret = os_snprintf(pos, end - pos, "%sCCMP",
3283
0
          pos == start ? "" : delim);
3284
0
    if (os_snprintf_error(end - pos, ret))
3285
0
      return -1;
3286
0
    pos += ret;
3287
0
  }
3288
0
  if (ciphers & WPA_CIPHER_GCMP) {
3289
0
    ret = os_snprintf(pos, end - pos, "%sGCMP",
3290
0
          pos == start ? "" : delim);
3291
0
    if (os_snprintf_error(end - pos, ret))
3292
0
      return -1;
3293
0
    pos += ret;
3294
0
  }
3295
0
  if (ciphers & WPA_CIPHER_TKIP) {
3296
0
    ret = os_snprintf(pos, end - pos, "%sTKIP",
3297
0
          pos == start ? "" : delim);
3298
0
    if (os_snprintf_error(end - pos, ret))
3299
0
      return -1;
3300
0
    pos += ret;
3301
0
  }
3302
0
  if (ciphers & WPA_CIPHER_AES_128_CMAC) {
3303
0
    ret = os_snprintf(pos, end - pos, "%sAES-128-CMAC",
3304
0
          pos == start ? "" : delim);
3305
0
    if (os_snprintf_error(end - pos, ret))
3306
0
      return -1;
3307
0
    pos += ret;
3308
0
  }
3309
0
  if (ciphers & WPA_CIPHER_BIP_GMAC_128) {
3310
0
    ret = os_snprintf(pos, end - pos, "%sBIP-GMAC-128",
3311
0
          pos == start ? "" : delim);
3312
0
    if (os_snprintf_error(end - pos, ret))
3313
0
      return -1;
3314
0
    pos += ret;
3315
0
  }
3316
0
  if (ciphers & WPA_CIPHER_BIP_GMAC_256) {
3317
0
    ret = os_snprintf(pos, end - pos, "%sBIP-GMAC-256",
3318
0
          pos == start ? "" : delim);
3319
0
    if (os_snprintf_error(end - pos, ret))
3320
0
      return -1;
3321
0
    pos += ret;
3322
0
  }
3323
0
  if (ciphers & WPA_CIPHER_BIP_CMAC_256) {
3324
0
    ret = os_snprintf(pos, end - pos, "%sBIP-CMAC-256",
3325
0
          pos == start ? "" : delim);
3326
0
    if (os_snprintf_error(end - pos, ret))
3327
0
      return -1;
3328
0
    pos += ret;
3329
0
  }
3330
0
  if (ciphers & WPA_CIPHER_NONE) {
3331
0
    ret = os_snprintf(pos, end - pos, "%sNONE",
3332
0
          pos == start ? "" : delim);
3333
0
    if (os_snprintf_error(end - pos, ret))
3334
0
      return -1;
3335
0
    pos += ret;
3336
0
  }
3337
3338
0
  return pos - start;
3339
0
}
3340
3341
3342
int wpa_select_ap_group_cipher(int wpa, int wpa_pairwise, int rsn_pairwise)
3343
0
{
3344
0
  int pairwise = 0;
3345
3346
  /* Select group cipher based on the enabled pairwise cipher suites */
3347
0
  if (wpa & 1)
3348
0
    pairwise |= wpa_pairwise;
3349
0
  if (wpa & 2)
3350
0
    pairwise |= rsn_pairwise;
3351
3352
0
  if (pairwise & WPA_CIPHER_TKIP)
3353
0
    return WPA_CIPHER_TKIP;
3354
0
  if ((pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP)) == WPA_CIPHER_GCMP)
3355
0
    return WPA_CIPHER_GCMP;
3356
0
  if ((pairwise & (WPA_CIPHER_GCMP_256 | WPA_CIPHER_CCMP |
3357
0
       WPA_CIPHER_GCMP)) == WPA_CIPHER_GCMP_256)
3358
0
    return WPA_CIPHER_GCMP_256;
3359
0
  if ((pairwise & (WPA_CIPHER_CCMP_256 | WPA_CIPHER_CCMP |
3360
0
       WPA_CIPHER_GCMP)) == WPA_CIPHER_CCMP_256)
3361
0
    return WPA_CIPHER_CCMP_256;
3362
0
  return WPA_CIPHER_CCMP;
3363
0
}
3364
3365
3366
#ifdef CONFIG_FILS
3367
int fils_domain_name_hash(const char *domain, u8 *hash)
3368
0
{
3369
0
  char buf[255], *wpos = buf;
3370
0
  const char *pos = domain;
3371
0
  size_t len;
3372
0
  const u8 *addr[1];
3373
0
  u8 mac[SHA256_MAC_LEN];
3374
3375
0
  for (len = 0; len < sizeof(buf) && *pos; len++) {
3376
0
    if (isalpha(*pos) && isupper(*pos))
3377
0
      *wpos++ = tolower(*pos);
3378
0
    else
3379
0
      *wpos++ = *pos;
3380
0
    pos++;
3381
0
  }
3382
3383
0
  addr[0] = (const u8 *) buf;
3384
0
  if (sha256_vector(1, addr, &len, mac) < 0)
3385
0
    return -1;
3386
0
  os_memcpy(hash, mac, 2);
3387
0
  return 0;
3388
0
}
3389
#endif /* CONFIG_FILS */
3390
3391
3392
/**
3393
 * wpa_parse_vendor_specific - Parse Vendor Specific IEs
3394
 * @pos: Pointer to the IE header
3395
 * @end: Pointer to the end of the Key Data buffer
3396
 * @ie: Pointer to parsed IE data
3397
 */
3398
static void wpa_parse_vendor_specific(const u8 *pos, const u8 *end,
3399
              struct wpa_eapol_ie_parse *ie)
3400
28.9k
{
3401
28.9k
  unsigned int oui;
3402
3403
28.9k
  if (pos[1] < 4) {
3404
4.96k
    wpa_printf(MSG_MSGDUMP,
3405
4.96k
         "Too short vendor specific IE ignored (len=%u)",
3406
4.96k
         pos[1]);
3407
4.96k
    return;
3408
4.96k
  }
3409
3410
23.9k
  oui = WPA_GET_BE24(&pos[2]);
3411
23.9k
  if (oui == OUI_MICROSOFT && pos[5] == WMM_OUI_TYPE && pos[1] > 4) {
3412
3.29k
    if (pos[6] == WMM_OUI_SUBTYPE_INFORMATION_ELEMENT) {
3413
1.20k
      ie->wmm = &pos[2];
3414
1.20k
      ie->wmm_len = pos[1];
3415
1.20k
      wpa_hexdump(MSG_DEBUG, "WPA: WMM IE",
3416
1.20k
            ie->wmm, ie->wmm_len);
3417
2.09k
    } else if (pos[6] == WMM_OUI_SUBTYPE_PARAMETER_ELEMENT) {
3418
784
      ie->wmm = &pos[2];
3419
784
      ie->wmm_len = pos[1];
3420
784
      wpa_hexdump(MSG_DEBUG, "WPA: WMM Parameter Element",
3421
784
            ie->wmm, ie->wmm_len);
3422
784
    }
3423
3.29k
  }
3424
23.9k
}
3425
3426
3427
/**
3428
 * wpa_parse_generic - Parse EAPOL-Key Key Data Generic IEs
3429
 * @pos: Pointer to the IE header
3430
 * @ie: Pointer to parsed IE data
3431
 * Returns: 0 on success, 1 if end mark is found, 2 if KDE is not recognized
3432
 */
3433
static int wpa_parse_generic(const u8 *pos, struct wpa_eapol_ie_parse *ie)
3434
46.7k
{
3435
46.7k
  u8 len = pos[1];
3436
46.7k
  size_t dlen = 2 + len;
3437
46.7k
  u32 selector;
3438
46.7k
  const u8 *p;
3439
46.7k
  size_t left;
3440
46.7k
  u8 link_id;
3441
46.7k
  char title[50];
3442
46.7k
  int ret;
3443
3444
46.7k
  if (len == 0)
3445
0
    return 1;
3446
3447
46.7k
  if (len < RSN_SELECTOR_LEN)
3448
4.96k
    return 2;
3449
3450
41.8k
  p = pos + 2;
3451
41.8k
  selector = RSN_SELECTOR_GET(p);
3452
41.8k
  p += RSN_SELECTOR_LEN;
3453
41.8k
  left = len - RSN_SELECTOR_LEN;
3454
3455
41.8k
  if (left >= 2 && selector == WPA_OUI_TYPE && p[0] == 1 && p[1] == 0) {
3456
1.48k
    ie->wpa_ie = pos;
3457
1.48k
    ie->wpa_ie_len = dlen;
3458
1.48k
    wpa_hexdump(MSG_DEBUG, "WPA: WPA IE in EAPOL-Key",
3459
1.48k
          ie->wpa_ie, ie->wpa_ie_len);
3460
1.48k
    return 0;
3461
1.48k
  }
3462
3463
40.3k
  if (selector == OSEN_IE_VENDOR_TYPE) {
3464
787
    ie->osen = pos;
3465
787
    ie->osen_len = dlen;
3466
787
    return 0;
3467
787
  }
3468
3469
39.5k
  if (left >= PMKID_LEN && selector == RSN_KEY_DATA_PMKID) {
3470
877
    ie->pmkid = p;
3471
877
    wpa_hexdump(MSG_DEBUG, "WPA: PMKID in EAPOL-Key", pos, dlen);
3472
877
    return 0;
3473
877
  }
3474
3475
38.6k
  if (left >= 2 && selector == RSN_KEY_DATA_KEYID) {
3476
770
    ie->key_id = p;
3477
770
    wpa_hexdump(MSG_DEBUG, "WPA: KeyID in EAPOL-Key", pos, dlen);
3478
770
    return 0;
3479
770
  }
3480
3481
37.9k
  if (left > 2 && selector == RSN_KEY_DATA_GROUPKEY) {
3482
916
    ie->gtk = p;
3483
916
    ie->gtk_len = left;
3484
916
    wpa_hexdump_key(MSG_DEBUG, "WPA: GTK in EAPOL-Key", pos, dlen);
3485
916
    return 0;
3486
916
  }
3487
3488
36.9k
  if (left >= ETH_ALEN && selector == RSN_KEY_DATA_MAC_ADDR) {
3489
912
    ie->mac_addr = p;
3490
912
    wpa_printf(MSG_DEBUG, "WPA: MAC Address in EAPOL-Key: " MACSTR,
3491
912
         MAC2STR(ie->mac_addr));
3492
912
    return 0;
3493
912
  }
3494
3495
36.0k
  if (left > 2 && selector == RSN_KEY_DATA_IGTK) {
3496
1.03k
    ie->igtk = p;
3497
1.03k
    ie->igtk_len = left;
3498
1.03k
    wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK in EAPOL-Key",
3499
1.03k
        pos, dlen);
3500
1.03k
    return 0;
3501
1.03k
  }
3502
3503
35.0k
  if (left > 2 && selector == RSN_KEY_DATA_BIGTK) {
3504
1.86k
    ie->bigtk = p;
3505
1.86k
    ie->bigtk_len = left;
3506
1.86k
    wpa_hexdump_key(MSG_DEBUG, "WPA: BIGTK in EAPOL-Key",
3507
1.86k
        pos, dlen);
3508
1.86k
    return 0;
3509
1.86k
  }
3510
3511
33.1k
  if (left >= 1 && selector == WFA_KEY_DATA_IP_ADDR_REQ) {
3512
1.22k
    ie->ip_addr_req = p;
3513
1.22k
    wpa_hexdump(MSG_DEBUG, "WPA: IP Address Request in EAPOL-Key",
3514
1.22k
          ie->ip_addr_req, left);
3515
1.22k
    return 0;
3516
1.22k
  }
3517
3518
31.9k
  if (left >= 3 * 4 && selector == WFA_KEY_DATA_IP_ADDR_ALLOC) {
3519
713
    ie->ip_addr_alloc = p;
3520
713
    wpa_hexdump(MSG_DEBUG,
3521
713
          "WPA: IP Address Allocation in EAPOL-Key",
3522
713
          ie->ip_addr_alloc, left);
3523
713
    return 0;
3524
713
  }
3525
3526
31.2k
  if (left > 2 && selector == RSN_KEY_DATA_OCI) {
3527
875
    ie->oci = p;
3528
875
    ie->oci_len = left;
3529
875
    wpa_hexdump(MSG_DEBUG, "WPA: OCI KDE in EAPOL-Key",
3530
875
          pos, dlen);
3531
875
    return 0;
3532
875
  }
3533
3534
30.3k
  if (left >= 1 && selector == WFA_KEY_DATA_TRANSITION_DISABLE) {
3535
760
    ie->transition_disable = p;
3536
760
    ie->transition_disable_len = left;
3537
760
    wpa_hexdump(MSG_DEBUG,
3538
760
          "WPA: Transition Disable KDE in EAPOL-Key",
3539
760
          pos, dlen);
3540
760
    return 0;
3541
760
  }
3542
3543
29.6k
  if (left >= 2 && selector == WFA_KEY_DATA_DPP) {
3544
1.12k
    ie->dpp_kde = p;
3545
1.12k
    ie->dpp_kde_len = left;
3546
1.12k
    wpa_hexdump(MSG_DEBUG, "WPA: DPP KDE in EAPOL-Key", pos, dlen);
3547
1.12k
    return 0;
3548
1.12k
  }
3549
3550
28.4k
  if (left >= RSN_MLO_GTK_KDE_PREFIX_LENGTH &&
3551
28.4k
      selector == RSN_KEY_DATA_MLO_GTK) {
3552
1.61k
    link_id = (p[0] & RSN_MLO_GTK_KDE_PREFIX0_LINK_ID_MASK) >>
3553
1.61k
      RSN_MLO_GTK_KDE_PREFIX0_LINK_ID_SHIFT;
3554
1.61k
    if (link_id >= MAX_NUM_MLO_LINKS)
3555
640
      return 2;
3556
3557
973
    ie->valid_mlo_gtks |= BIT(link_id);
3558
973
    ie->mlo_gtk[link_id] = p;
3559
973
    ie->mlo_gtk_len[link_id] = left;
3560
973
    ret = os_snprintf(title, sizeof(title),
3561
973
          "RSN: Link ID %u - MLO GTK KDE in EAPOL-Key",
3562
973
          link_id);
3563
973
    if (!os_snprintf_error(sizeof(title), ret))
3564
973
      wpa_hexdump_key(MSG_DEBUG, title, pos, dlen);
3565
973
    return 0;
3566
1.61k
  }
3567
3568
26.8k
  if (left >= RSN_MLO_IGTK_KDE_PREFIX_LENGTH &&
3569
26.8k
      selector == RSN_KEY_DATA_MLO_IGTK) {
3570
2.01k
    link_id = (p[8] & RSN_MLO_IGTK_KDE_PREFIX8_LINK_ID_MASK) >>
3571
2.01k
        RSN_MLO_IGTK_KDE_PREFIX8_LINK_ID_SHIFT;
3572
2.01k
    if (link_id >= MAX_NUM_MLO_LINKS)
3573
1.04k
      return 2;
3574
3575
978
    ie->valid_mlo_igtks |= BIT(link_id);
3576
978
    ie->mlo_igtk[link_id] = p;
3577
978
    ie->mlo_igtk_len[link_id] = left;
3578
978
    ret = os_snprintf(title, sizeof(title),
3579
978
          "RSN: Link ID %u - MLO IGTK KDE in EAPOL-Key",
3580
978
          link_id);
3581
978
    if (!os_snprintf_error(sizeof(title), ret))
3582
978
      wpa_hexdump_key(MSG_DEBUG, title, pos, dlen);
3583
978
    return 0;
3584
2.01k
  }
3585
3586
24.8k
  if (left >= RSN_MLO_BIGTK_KDE_PREFIX_LENGTH &&
3587
24.8k
      selector == RSN_KEY_DATA_MLO_BIGTK) {
3588
2.86k
    link_id = (p[8] & RSN_MLO_BIGTK_KDE_PREFIX8_LINK_ID_MASK) >>
3589
2.86k
        RSN_MLO_BIGTK_KDE_PREFIX8_LINK_ID_SHIFT;
3590
2.86k
    if (link_id >= MAX_NUM_MLO_LINKS)
3591
1.51k
      return 2;
3592
3593
1.34k
    ie->valid_mlo_bigtks |= BIT(link_id);
3594
1.34k
    ie->mlo_bigtk[link_id] = p;
3595
1.34k
    ie->mlo_bigtk_len[link_id] = left;
3596
1.34k
    ret = os_snprintf(title, sizeof(title),
3597
1.34k
          "RSN: Link ID %u - MLO BIGTK KDE in EAPOL-Key",
3598
1.34k
          link_id);
3599
1.34k
    if (!os_snprintf_error(sizeof(title), ret))
3600
1.34k
      wpa_hexdump_key(MSG_DEBUG, title, pos, dlen);
3601
1.34k
    return 0;
3602
2.86k
  }
3603
3604
21.9k
  if (left >= RSN_MLO_LINK_KDE_FIXED_LENGTH &&
3605
21.9k
      selector == RSN_KEY_DATA_MLO_LINK) {
3606
2.01k
    link_id = (p[0] & RSN_MLO_LINK_KDE_LI_LINK_ID_MASK) >>
3607
2.01k
        RSN_MLO_LINK_KDE_LI_LINK_ID_SHIFT;
3608
2.01k
    if (link_id >= MAX_NUM_MLO_LINKS)
3609
788
      return 2;
3610
3611
1.22k
    ie->valid_mlo_links |= BIT(link_id);
3612
1.22k
    ie->mlo_link[link_id] = p;
3613
1.22k
    ie->mlo_link_len[link_id] = left;
3614
1.22k
    ret = os_snprintf(title, sizeof(title),
3615
1.22k
          "RSN: Link ID %u - MLO Link KDE in EAPOL-Key",
3616
1.22k
          link_id);
3617
1.22k
    if (!os_snprintf_error(sizeof(title), ret))
3618
1.22k
      wpa_hexdump(MSG_DEBUG, title, pos, dlen);
3619
1.22k
    return 0;
3620
2.01k
  }
3621
3622
19.9k
  return 2;
3623
21.9k
}
3624
3625
3626
/**
3627
 * wpa_parse_kde_ies - Parse EAPOL-Key Key Data IEs
3628
 * @buf: Pointer to the Key Data buffer
3629
 * @len: Key Data Length
3630
 * @ie: Pointer to parsed IE data
3631
 * Returns: 0 on success, -1 on failure
3632
 */
3633
int wpa_parse_kde_ies(const u8 *buf, size_t len, struct wpa_eapol_ie_parse *ie)
3634
13.5k
{
3635
13.5k
  const u8 *pos, *end;
3636
13.5k
  int ret = 0;
3637
13.5k
  size_t dlen = 0;
3638
3639
13.5k
  os_memset(ie, 0, sizeof(*ie));
3640
268k
  for (pos = buf, end = pos + len; end - pos > 1; pos += dlen) {
3641
256k
    if (pos[0] == 0xdd &&
3642
256k
        ((pos == buf + len - 1) || pos[1] == 0)) {
3643
      /* Ignore padding */
3644
230
      break;
3645
230
    }
3646
256k
    dlen = 2 + pos[1];
3647
256k
    if ((int) dlen > end - pos) {
3648
1.11k
      wpa_printf(MSG_DEBUG,
3649
1.11k
           "WPA: EAPOL-Key Key Data underflow (ie=%d len=%d pos=%d)",
3650
1.11k
           pos[0], pos[1], (int) (pos - buf));
3651
1.11k
      wpa_hexdump_key(MSG_DEBUG, "WPA: Key Data", buf, len);
3652
1.11k
      ret = -1;
3653
1.11k
      break;
3654
1.11k
    }
3655
255k
    if (*pos == WLAN_EID_RSN) {
3656
4.22k
      ie->rsn_ie = pos;
3657
4.22k
      ie->rsn_ie_len = dlen;
3658
4.22k
      wpa_hexdump(MSG_DEBUG, "WPA: RSN IE in EAPOL-Key",
3659
4.22k
            ie->rsn_ie, ie->rsn_ie_len);
3660
250k
    } else if (*pos == WLAN_EID_RSNX) {
3661
1.66k
      ie->rsnxe = pos;
3662
1.66k
      ie->rsnxe_len = dlen;
3663
1.66k
      wpa_hexdump(MSG_DEBUG, "WPA: RSNXE in EAPOL-Key",
3664
1.66k
            ie->rsnxe, ie->rsnxe_len);
3665
249k
    } else if (*pos == WLAN_EID_MOBILITY_DOMAIN) {
3666
1.55k
      ie->mdie = pos;
3667
1.55k
      ie->mdie_len = dlen;
3668
1.55k
      wpa_hexdump(MSG_DEBUG, "WPA: MDIE in EAPOL-Key",
3669
1.55k
            ie->mdie, ie->mdie_len);
3670
247k
    } else if (*pos == WLAN_EID_FAST_BSS_TRANSITION) {
3671
1.78k
      ie->ftie = pos;
3672
1.78k
      ie->ftie_len = dlen;
3673
1.78k
      wpa_hexdump(MSG_DEBUG, "WPA: FTIE in EAPOL-Key",
3674
1.78k
            ie->ftie, ie->ftie_len);
3675
245k
    } else if (*pos == WLAN_EID_TIMEOUT_INTERVAL && pos[1] >= 5) {
3676
2.55k
      if (pos[2] == WLAN_TIMEOUT_REASSOC_DEADLINE) {
3677
695
        ie->reassoc_deadline = pos;
3678
695
        wpa_hexdump(MSG_DEBUG, "WPA: Reassoc Deadline "
3679
695
              "in EAPOL-Key",
3680
695
              ie->reassoc_deadline, dlen);
3681
1.86k
      } else if (pos[2] == WLAN_TIMEOUT_KEY_LIFETIME) {
3682
720
        ie->key_lifetime = pos;
3683
720
        wpa_hexdump(MSG_DEBUG, "WPA: KeyLifetime "
3684
720
              "in EAPOL-Key",
3685
720
              ie->key_lifetime, dlen);
3686
1.14k
      } else {
3687
1.14k
        wpa_hexdump(MSG_DEBUG, "WPA: Unrecognized "
3688
1.14k
              "EAPOL-Key Key Data IE",
3689
1.14k
              pos, dlen);
3690
1.14k
      }
3691
243k
    } else if (*pos == WLAN_EID_LINK_ID) {
3692
2.25k
      if (pos[1] >= 18) {
3693
825
        ie->lnkid = pos;
3694
825
        ie->lnkid_len = dlen;
3695
825
      }
3696
241k
    } else if (*pos == WLAN_EID_EXT_CAPAB) {
3697
1.28k
      ie->ext_capab = pos;
3698
1.28k
      ie->ext_capab_len = dlen;
3699
239k
    } else if (*pos == WLAN_EID_SUPP_RATES) {
3700
7.74k
      ie->supp_rates = pos;
3701
7.74k
      ie->supp_rates_len = dlen;
3702
232k
    } else if (*pos == WLAN_EID_EXT_SUPP_RATES) {
3703
2.11k
      ie->ext_supp_rates = pos;
3704
2.11k
      ie->ext_supp_rates_len = dlen;
3705
229k
    } else if (*pos == WLAN_EID_HT_CAP &&
3706
229k
         pos[1] >= sizeof(struct ieee80211_ht_capabilities)) {
3707
752
      ie->ht_capabilities = pos + 2;
3708
229k
    } else if (*pos == WLAN_EID_AID) {
3709
3.39k
      if (pos[1] >= 2)
3710
1.13k
        ie->aid = WPA_GET_LE16(pos + 2) & 0x3fff;
3711
225k
    } else if (*pos == WLAN_EID_VHT_CAP &&
3712
225k
         pos[1] >= sizeof(struct ieee80211_vht_capabilities))
3713
718
    {
3714
718
      ie->vht_capabilities = pos + 2;
3715
225k
    } else if (*pos == WLAN_EID_EXTENSION &&
3716
225k
         pos[1] >= 1 + IEEE80211_HE_CAPAB_MIN_LEN &&
3717
225k
         pos[2] == WLAN_EID_EXT_HE_CAPABILITIES) {
3718
766
      ie->he_capabilities = pos + 3;
3719
766
      ie->he_capab_len = pos[1] - 1;
3720
224k
    } else if (*pos == WLAN_EID_EXTENSION &&
3721
224k
         pos[1] >= 1 +
3722
8.54k
         sizeof(struct ieee80211_he_6ghz_band_cap) &&
3723
224k
         pos[2] == WLAN_EID_EXT_HE_6GHZ_BAND_CAP) {
3724
729
      ie->he_6ghz_capabilities = pos + 3;
3725
223k
    } else if (*pos == WLAN_EID_EXTENSION &&
3726
223k
         pos[1] >= 1 + IEEE80211_EHT_CAPAB_MIN_LEN &&
3727
223k
         pos[2] == WLAN_EID_EXT_EHT_CAPABILITIES) {
3728
1.14k
      ie->eht_capabilities = pos + 3;
3729
1.14k
      ie->eht_capab_len = pos[1] - 1;
3730
222k
    } else if (*pos == WLAN_EID_QOS && pos[1] >= 1) {
3731
1.41k
      ie->qosinfo = pos[2];
3732
221k
    } else if (*pos == WLAN_EID_SUPPORTED_CHANNELS) {
3733
1.87k
      ie->supp_channels = pos + 2;
3734
1.87k
      ie->supp_channels_len = pos[1];
3735
219k
    } else if (*pos == WLAN_EID_SUPPORTED_OPERATING_CLASSES) {
3736
      /*
3737
       * The value of the Length field of the Supported
3738
       * Operating Classes element is between 2 and 253.
3739
       * Silently skip invalid elements to avoid interop
3740
       * issues when trying to use the value.
3741
       */
3742
4.07k
      if (pos[1] >= 2 && pos[1] <= 253) {
3743
1.43k
        ie->supp_oper_classes = pos + 2;
3744
1.43k
        ie->supp_oper_classes_len = pos[1];
3745
1.43k
      }
3746
215k
    } else if (*pos == WLAN_EID_VENDOR_SPECIFIC) {
3747
46.7k
      ret = wpa_parse_generic(pos, ie);
3748
46.7k
      if (ret == 1) {
3749
        /* end mark found */
3750
0
        ret = 0;
3751
0
        break;
3752
0
      }
3753
3754
46.7k
      if (ret == 2) {
3755
        /* not a known KDE */
3756
28.9k
        wpa_parse_vendor_specific(pos, end, ie);
3757
28.9k
      }
3758
3759
46.7k
      ret = 0;
3760
168k
    } else {
3761
168k
      wpa_hexdump(MSG_DEBUG,
3762
168k
            "WPA: Unrecognized EAPOL-Key Key Data IE",
3763
168k
            pos, dlen);
3764
168k
    }
3765
255k
  }
3766
3767
13.5k
  return ret;
3768
13.5k
}
3769
3770
3771
#ifdef CONFIG_PASN
3772
3773
/*
3774
 * wpa_pasn_build_auth_header - Add the MAC header and initialize Authentication
3775
 * frame for PASN
3776
 *
3777
 * @buf: Buffer in which the header will be added
3778
 * @bssid: The BSSID of the AP
3779
 * @src: Source address
3780
 * @dst: Destination address
3781
 * @trans_seq: Authentication transaction sequence number
3782
 * @status: Authentication status
3783
 */
3784
void wpa_pasn_build_auth_header(struct wpabuf *buf, const u8 *bssid,
3785
        const u8 *src, const u8 *dst,
3786
        u8 trans_seq, u16 status)
3787
5.55k
{
3788
5.55k
  struct ieee80211_mgmt *auth;
3789
3790
5.55k
  wpa_printf(MSG_DEBUG, "PASN: Add authentication header. trans_seq=%u",
3791
5.55k
       trans_seq);
3792
3793
5.55k
  auth = wpabuf_put(buf, offsetof(struct ieee80211_mgmt,
3794
5.55k
          u.auth.variable));
3795
3796
5.55k
  auth->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
3797
5.55k
             (WLAN_FC_STYPE_AUTH << 4));
3798
3799
5.55k
  os_memcpy(auth->da, dst, ETH_ALEN);
3800
5.55k
  os_memcpy(auth->sa, src, ETH_ALEN);
3801
5.55k
  os_memcpy(auth->bssid, bssid, ETH_ALEN);
3802
5.55k
  auth->seq_ctrl = 0;
3803
3804
5.55k
  auth->u.auth.auth_alg = host_to_le16(WLAN_AUTH_PASN);
3805
5.55k
  auth->u.auth.auth_transaction = host_to_le16(trans_seq);
3806
5.55k
  auth->u.auth.status_code = host_to_le16(status);
3807
5.55k
}
3808
3809
3810
/*
3811
 * wpa_pasn_add_rsne - Add an RSNE for PASN authentication
3812
 * @buf: Buffer in which the IE will be added
3813
 * @pmkid: Optional PMKID. Can be NULL.
3814
 * @akmp: Authentication and key management protocol
3815
 * @cipher: The cipher suite
3816
 */
3817
int wpa_pasn_add_rsne(struct wpabuf *buf, const u8 *pmkid, int akmp, int cipher)
3818
2.95k
{
3819
2.95k
  struct rsn_ie_hdr *hdr;
3820
2.95k
  u32 suite;
3821
2.95k
  u16 capab;
3822
2.95k
  u8 *pos;
3823
2.95k
  u8 rsne_len;
3824
3825
2.95k
  wpa_printf(MSG_DEBUG, "PASN: Add RSNE");
3826
3827
2.95k
  rsne_len = sizeof(*hdr) + RSN_SELECTOR_LEN +
3828
2.95k
    2 + RSN_SELECTOR_LEN + 2 + RSN_SELECTOR_LEN +
3829
2.95k
    2 + RSN_SELECTOR_LEN + 2 + (pmkid ? PMKID_LEN : 0);
3830
3831
2.95k
  if (wpabuf_tailroom(buf) < rsne_len)
3832
0
    return -1;
3833
2.95k
  hdr = wpabuf_put(buf, rsne_len);
3834
2.95k
  hdr->elem_id = WLAN_EID_RSN;
3835
2.95k
  hdr->len = rsne_len - 2;
3836
2.95k
  WPA_PUT_LE16(hdr->version, RSN_VERSION);
3837
2.95k
  pos = (u8 *) (hdr + 1);
3838
3839
  /* Group addressed data is not allowed */
3840
2.95k
  RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED);
3841
2.95k
  pos += RSN_SELECTOR_LEN;
3842
3843
  /* Add the pairwise cipher */
3844
2.95k
  WPA_PUT_LE16(pos, 1);
3845
2.95k
  pos += 2;
3846
2.95k
  suite = wpa_cipher_to_suite(WPA_PROTO_RSN, cipher);
3847
2.95k
  RSN_SELECTOR_PUT(pos, suite);
3848
2.95k
  pos += RSN_SELECTOR_LEN;
3849
3850
  /* Add the AKM suite */
3851
2.95k
  WPA_PUT_LE16(pos, 1);
3852
2.95k
  pos += 2;
3853
3854
2.95k
  switch (akmp) {
3855
2.95k
  case WPA_KEY_MGMT_PASN:
3856
2.95k
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_PASN);
3857
2.95k
    break;
3858
0
#ifdef CONFIG_SAE
3859
0
  case WPA_KEY_MGMT_SAE:
3860
0
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_SAE);
3861
0
    break;
3862
0
  case WPA_KEY_MGMT_SAE_EXT_KEY:
3863
0
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_SAE_EXT_KEY);
3864
0
    break;
3865
0
#endif /* CONFIG_SAE */
3866
0
#ifdef CONFIG_FILS
3867
0
  case WPA_KEY_MGMT_FILS_SHA256:
3868
0
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FILS_SHA256);
3869
0
    break;
3870
0
  case WPA_KEY_MGMT_FILS_SHA384:
3871
0
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FILS_SHA384);
3872
0
    break;
3873
0
#endif /* CONFIG_FILS */
3874
0
#ifdef CONFIG_IEEE80211R
3875
0
  case WPA_KEY_MGMT_FT_PSK:
3876
0
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_PSK);
3877
0
    break;
3878
0
  case WPA_KEY_MGMT_FT_IEEE8021X:
3879
0
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_802_1X);
3880
0
    break;
3881
0
  case WPA_KEY_MGMT_FT_IEEE8021X_SHA384:
3882
0
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_802_1X_SHA384);
3883
0
    break;
3884
0
#endif /* CONFIG_IEEE80211R */
3885
0
  default:
3886
0
    wpa_printf(MSG_ERROR, "PASN: Invalid AKMP=0x%x", akmp);
3887
0
    return -1;
3888
2.95k
  }
3889
2.95k
  pos += RSN_SELECTOR_LEN;
3890
3891
  /* RSN Capabilities: PASN mandates both MFP capable and required */
3892
2.95k
  capab = WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR;
3893
2.95k
  WPA_PUT_LE16(pos, capab);
3894
2.95k
  pos += 2;
3895
3896
2.95k
  if (pmkid) {
3897
0
    wpa_printf(MSG_DEBUG, "PASN: Adding PMKID");
3898
3899
0
    WPA_PUT_LE16(pos, 1);
3900
0
    pos += 2;
3901
0
    os_memcpy(pos, pmkid, PMKID_LEN);
3902
0
    pos += PMKID_LEN;
3903
2.95k
  } else {
3904
2.95k
    WPA_PUT_LE16(pos, 0);
3905
2.95k
    pos += 2;
3906
2.95k
  }
3907
3908
  /* Group addressed management is not allowed */
3909
2.95k
  RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED);
3910
3911
2.95k
  return 0;
3912
2.95k
}
3913
3914
3915
/*
3916
 * wpa_pasn_add_parameter_ie - Add PASN Parameters IE for PASN authentication
3917
 * @buf: Buffer in which the IE will be added
3918
 * @pasn_group: Finite Cyclic Group ID for PASN authentication
3919
 * @wrapped_data_format: Format of the data in the Wrapped Data IE
3920
 * @pubkey: A buffer holding the local public key. Can be NULL
3921
 * @compressed: In case pubkey is included, indicates if the public key is
3922
 *     compressed (only x coordinate is included) or not (both x and y
3923
 *     coordinates are included)
3924
 * @comeback: A buffer holding the comeback token. Can be NULL
3925
 * @after: If comeback is set, defined the comeback time in seconds. -1 to not
3926
 *  include the Comeback After field (frames from non-AP STA).
3927
 */
3928
void wpa_pasn_add_parameter_ie(struct wpabuf *buf, u16 pasn_group,
3929
             u8 wrapped_data_format,
3930
             const struct wpabuf *pubkey, bool compressed,
3931
             const struct wpabuf *comeback, int after)
3932
2.95k
{
3933
2.95k
  struct pasn_parameter_ie *params;
3934
3935
2.95k
  wpa_printf(MSG_DEBUG, "PASN: Add PASN Parameters element");
3936
3937
2.95k
  params = wpabuf_put(buf, sizeof(*params));
3938
3939
2.95k
  params->id = WLAN_EID_EXTENSION;
3940
2.95k
  params->len = sizeof(*params) - 2;
3941
2.95k
  params->id_ext = WLAN_EID_EXT_PASN_PARAMS;
3942
2.95k
  params->control = 0;
3943
2.95k
  params->wrapped_data_format = wrapped_data_format;
3944
3945
2.95k
  if (comeback) {
3946
0
    wpa_printf(MSG_DEBUG, "PASN: Adding comeback data");
3947
3948
    /*
3949
     * 2 octets for the 'after' field + 1 octet for the length +
3950
     * actual cookie data
3951
     */
3952
0
    if (after >= 0)
3953
0
      params->len += 2;
3954
0
    params->len += 1 + wpabuf_len(comeback);
3955
0
    params->control |= WPA_PASN_CTRL_COMEBACK_INFO_PRESENT;
3956
3957
0
    if (after >= 0)
3958
0
      wpabuf_put_le16(buf, after);
3959
0
    wpabuf_put_u8(buf, wpabuf_len(comeback));
3960
0
    wpabuf_put_buf(buf, comeback);
3961
0
  }
3962
3963
2.95k
  if (pubkey) {
3964
2.95k
    wpa_printf(MSG_DEBUG,
3965
2.95k
         "PASN: Adding public key and group ID %u",
3966
2.95k
         pasn_group);
3967
3968
    /*
3969
     * 2 octets for the finite cyclic group + 2 octets public key
3970
     * length + 1 octet for the compressed/uncompressed indication +
3971
     * the actual key.
3972
     */
3973
2.95k
    params->len += 2 + 1 + 1 + wpabuf_len(pubkey);
3974
2.95k
    params->control |= WPA_PASN_CTRL_GROUP_AND_KEY_PRESENT;
3975
3976
2.95k
    wpabuf_put_le16(buf, pasn_group);
3977
3978
    /*
3979
     * The first octet indicates whether the public key is
3980
     * compressed, as defined in RFC 5480 section 2.2.
3981
     */
3982
2.95k
    wpabuf_put_u8(buf, wpabuf_len(pubkey) + 1);
3983
2.95k
    wpabuf_put_u8(buf, compressed ? WPA_PASN_PUBKEY_COMPRESSED_0 :
3984
2.95k
            WPA_PASN_PUBKEY_UNCOMPRESSED);
3985
3986
2.95k
    wpabuf_put_buf(buf, pubkey);
3987
2.95k
  }
3988
2.95k
}
3989
3990
/*
3991
 * wpa_pasn_add_wrapped_data - Add a Wrapped Data IE to PASN Authentication
3992
 * frame. If needed, the Wrapped Data IE would be fragmented.
3993
 *
3994
 * @buf: Buffer in which the IE will be added
3995
 * @wrapped_data_buf: Buffer holding the wrapped data
3996
 */
3997
int wpa_pasn_add_wrapped_data(struct wpabuf *buf,
3998
            struct wpabuf *wrapped_data_buf)
3999
2.95k
{
4000
2.95k
  const u8 *data;
4001
2.95k
  size_t data_len;
4002
2.95k
  u8 len;
4003
4004
2.95k
  if (!wrapped_data_buf)
4005
2.95k
    return 0;
4006
4007
0
  wpa_printf(MSG_DEBUG, "PASN: Add wrapped data");
4008
4009
0
  data = wpabuf_head_u8(wrapped_data_buf);
4010
0
  data_len = wpabuf_len(wrapped_data_buf);
4011
4012
  /* nothing to add */
4013
0
  if (!data_len)
4014
0
    return 0;
4015
4016
0
  if (data_len <= 254)
4017
0
    len = 1 + data_len;
4018
0
  else
4019
0
    len = 255;
4020
4021
0
  if (wpabuf_tailroom(buf) < 3 + data_len)
4022
0
    return -1;
4023
4024
0
  wpabuf_put_u8(buf, WLAN_EID_EXTENSION);
4025
0
  wpabuf_put_u8(buf, len);
4026
0
  wpabuf_put_u8(buf, WLAN_EID_EXT_WRAPPED_DATA);
4027
0
  wpabuf_put_data(buf, data, len - 1);
4028
4029
0
  data += len - 1;
4030
0
  data_len -= len - 1;
4031
4032
0
  while (data_len) {
4033
0
    if (wpabuf_tailroom(buf) < 1 + data_len)
4034
0
      return -1;
4035
0
    wpabuf_put_u8(buf, WLAN_EID_FRAGMENT);
4036
0
    len = data_len > 255 ? 255 : data_len;
4037
0
    wpabuf_put_u8(buf, len);
4038
0
    wpabuf_put_data(buf, data, len);
4039
0
    data += len;
4040
0
    data_len -= len;
4041
0
  }
4042
4043
0
  return 0;
4044
0
}
4045
4046
4047
/*
4048
 * wpa_pasn_validate_rsne - Validate PSAN specific data of RSNE
4049
 * @data: Parsed representation of an RSNE
4050
 * Returns -1 for invalid data; otherwise 0
4051
 */
4052
int wpa_pasn_validate_rsne(const struct wpa_ie_data *data)
4053
1.36k
{
4054
1.36k
  u16 capab = WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR;
4055
4056
1.36k
  if (data->proto != WPA_PROTO_RSN)
4057
2
    return -1;
4058
4059
1.36k
  if ((data->capabilities & capab) != capab) {
4060
709
    wpa_printf(MSG_DEBUG, "PASN: Invalid RSNE capabilities");
4061
709
    return -1;
4062
709
  }
4063
4064
658
  if (!data->has_group || data->group_cipher != WPA_CIPHER_GTK_NOT_USED) {
4065
16
    wpa_printf(MSG_DEBUG, "PASN: Invalid group data cipher");
4066
16
    return -1;
4067
16
  }
4068
4069
642
  if (!data->has_pairwise || !data->pairwise_cipher ||
4070
642
      (data->pairwise_cipher & (data->pairwise_cipher - 1))) {
4071
37
    wpa_printf(MSG_DEBUG, "PASN: No valid pairwise suite");
4072
37
    return -1;
4073
37
  }
4074
4075
605
  switch (data->key_mgmt) {
4076
0
#ifdef CONFIG_SAE
4077
5
  case WPA_KEY_MGMT_SAE:
4078
9
  case WPA_KEY_MGMT_SAE_EXT_KEY:
4079
  /* fall through */
4080
9
#endif /* CONFIG_SAE */
4081
9
#ifdef CONFIG_FILS
4082
15
  case WPA_KEY_MGMT_FILS_SHA256:
4083
17
  case WPA_KEY_MGMT_FILS_SHA384:
4084
  /* fall through */
4085
17
#endif /* CONFIG_FILS */
4086
17
#ifdef CONFIG_IEEE80211R
4087
20
  case WPA_KEY_MGMT_FT_PSK:
4088
22
  case WPA_KEY_MGMT_FT_IEEE8021X:
4089
25
  case WPA_KEY_MGMT_FT_IEEE8021X_SHA384:
4090
  /* fall through */
4091
25
#endif /* CONFIG_IEEE80211R */
4092
289
  case WPA_KEY_MGMT_PASN:
4093
289
    break;
4094
316
  default:
4095
316
    wpa_printf(MSG_ERROR, "PASN: invalid key_mgmt: 0x%0x",
4096
316
         data->key_mgmt);
4097
316
    return -1;
4098
605
  }
4099
4100
289
  if (data->mgmt_group_cipher != WPA_CIPHER_GTK_NOT_USED) {
4101
23
    wpa_printf(MSG_DEBUG, "PASN: Invalid group mgmt cipher");
4102
23
    return -1;
4103
23
  }
4104
4105
266
  if (data->num_pmkid > 1) {
4106
2
    wpa_printf(MSG_DEBUG, "PASN: Invalid number of PMKIDs");
4107
2
    return -1;
4108
2
  }
4109
4110
264
  return 0;
4111
266
}
4112
4113
4114
/*
4115
 * wpa_pasn_parse_parameter_ie - Validates PASN Parameters IE
4116
 * @data: Pointer to the PASN Parameters IE (starting with the EID).
4117
 * @len: Length of the data in the PASN Parameters IE
4118
 * @from_ap: Whether this was received from an AP
4119
 * @pasn_params: On successful return would hold the parsed PASN parameters.
4120
 * Returns: -1 for invalid data; otherwise 0
4121
 *
4122
 * Note: On successful return, the pointers in &pasn_params point to the data in
4123
 * the IE and are not locally allocated (so they should not be freed etc.).
4124
 */
4125
int wpa_pasn_parse_parameter_ie(const u8 *data, u8 len, bool from_ap,
4126
        struct wpa_pasn_params_data *pasn_params)
4127
1.54k
{
4128
1.54k
  struct pasn_parameter_ie *params = (struct pasn_parameter_ie *) data;
4129
1.54k
  const u8 *pos = (const u8 *) (params + 1);
4130
4131
1.54k
  if (!pasn_params) {
4132
0
    wpa_printf(MSG_DEBUG, "PASN: Invalid params");
4133
0
    return -1;
4134
0
  }
4135
4136
1.54k
  if (!params || ((size_t) (params->len + 2) < sizeof(*params)) ||
4137
1.54k
      len < sizeof(*params) || params->len + 2 != len) {
4138
9
    wpa_printf(MSG_DEBUG,
4139
9
         "PASN: Invalid parameters IE. len=(%u, %u)",
4140
9
         params ? params->len : 0, len);
4141
9
    return -1;
4142
9
  }
4143
4144
1.54k
  os_memset(pasn_params, 0, sizeof(*pasn_params));
4145
4146
1.54k
  switch (params->wrapped_data_format) {
4147
1.01k
  case WPA_PASN_WRAPPED_DATA_NO:
4148
1.07k
  case WPA_PASN_WRAPPED_DATA_SAE:
4149
1.12k
  case WPA_PASN_WRAPPED_DATA_FILS_SK:
4150
1.52k
  case WPA_PASN_WRAPPED_DATA_FT:
4151
1.52k
    break;
4152
20
  default:
4153
20
    wpa_printf(MSG_DEBUG, "PASN: Invalid wrapped data format");
4154
20
    return -1;
4155
1.54k
  }
4156
4157
1.52k
  pasn_params->wrapped_data_format = params->wrapped_data_format;
4158
4159
1.52k
  len -= sizeof(*params);
4160
4161
1.52k
  if (params->control & WPA_PASN_CTRL_COMEBACK_INFO_PRESENT) {
4162
96
    if (from_ap) {
4163
53
      if (len < 2) {
4164
1
        wpa_printf(MSG_DEBUG,
4165
1
             "PASN: Invalid Parameters IE: Truncated Comeback After");
4166
1
        return -1;
4167
1
      }
4168
52
      pasn_params->after = WPA_GET_LE16(pos);
4169
52
      pos += 2;
4170
52
      len -= 2;
4171
52
    }
4172
4173
95
    if (len < 1 || len < 1 + *pos) {
4174
38
      wpa_printf(MSG_DEBUG,
4175
38
           "PASN: Invalid Parameters IE: comeback len");
4176
38
      return -1;
4177
38
    }
4178
4179
57
    pasn_params->comeback_len = *pos++;
4180
57
    len--;
4181
57
    pasn_params->comeback = pos;
4182
57
    len -=  pasn_params->comeback_len;
4183
57
    pos += pasn_params->comeback_len;
4184
57
  }
4185
4186
1.48k
  if (params->control & WPA_PASN_CTRL_GROUP_AND_KEY_PRESENT) {
4187
295
    if (len < 3 || len < 3 + pos[2]) {
4188
37
      wpa_printf(MSG_DEBUG,
4189
37
           "PASN: Invalid Parameters IE: group and key");
4190
37
      return -1;
4191
37
    }
4192
4193
258
    pasn_params->group = WPA_GET_LE16(pos);
4194
258
    pos += 2;
4195
258
    len -= 2;
4196
258
    pasn_params->pubkey_len = *pos++;
4197
258
    len--;
4198
258
    pasn_params->pubkey = pos;
4199
258
    len -= pasn_params->pubkey_len;
4200
258
    pos += pasn_params->pubkey_len;
4201
258
  }
4202
4203
1.44k
  if (len) {
4204
38
    wpa_printf(MSG_DEBUG,
4205
38
         "PASN: Invalid Parameters IE. Bytes left=%u", len);
4206
38
    return -1;
4207
38
  }
4208
4209
1.40k
  return 0;
4210
1.44k
}
4211
4212
4213
void wpa_pasn_add_rsnxe(struct wpabuf *buf, u16 capab)
4214
2.95k
{
4215
2.95k
  size_t flen;
4216
4217
2.95k
  flen = (capab & 0xff00) ? 2 : 1;
4218
2.95k
  if (!capab)
4219
2.95k
    return; /* no supported extended RSN capabilities */
4220
0
  if (wpabuf_tailroom(buf) < 2 + flen)
4221
0
    return;
4222
0
  capab |= flen - 1; /* bit 0-3 = Field length (n - 1) */
4223
4224
0
  wpabuf_put_u8(buf, WLAN_EID_RSNX);
4225
0
  wpabuf_put_u8(buf, flen);
4226
0
  wpabuf_put_u8(buf, capab & 0x00ff);
4227
0
  capab >>= 8;
4228
0
  if (capab)
4229
0
    wpabuf_put_u8(buf, capab);
4230
0
}
4231
4232
4233
/*
4234
 * wpa_pasn_add_extra_ies - Add protocol specific IEs in Authentication
4235
 * frame for PASN.
4236
 *
4237
 * @buf: Buffer in which the elements will be added
4238
 * @extra_ies: Protocol specific elements to add
4239
 * @len: Length of the elements
4240
 * Returns: 0 on success, -1 on failure
4241
 */
4242
4243
int wpa_pasn_add_extra_ies(struct wpabuf *buf, const u8 *extra_ies, size_t len)
4244
2.95k
{
4245
2.95k
  if (!len || !extra_ies || !buf)
4246
2.95k
    return 0;
4247
4248
0
  if (wpabuf_tailroom(buf) < sizeof(len))
4249
0
    return -1;
4250
4251
0
  wpabuf_put_data(buf, extra_ies, len);
4252
0
  return 0;
4253
0
}
4254
4255
#endif /* CONFIG_PASN */