Coverage Report

Created: 2025-04-24 06:18

/src/hostap/src/rsn_supp/wpa_ft.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * WPA Supplicant - IEEE 802.11r - Fast BSS Transition
3
 * Copyright (c) 2006-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/aes_wrap.h"
13
#include "crypto/sha384.h"
14
#include "crypto/sha512.h"
15
#include "crypto/random.h"
16
#include "common/ieee802_11_defs.h"
17
#include "common/ieee802_11_common.h"
18
#include "common/ocv.h"
19
#include "common/wpa_ctrl.h"
20
#include "drivers/driver.h"
21
#include "wpa.h"
22
#include "wpa_i.h"
23
#include "wpa_ie.h"
24
#include "pmksa_cache.h"
25
26
#ifdef CONFIG_IEEE80211R
27
28
#ifdef CONFIG_PASN
29
static void wpa_ft_pasn_store_r1kh(struct wpa_sm *sm, const u8 *bssid);
30
#else /* CONFIG_PASN */
31
static void wpa_ft_pasn_store_r1kh(struct wpa_sm *sm, const u8 *bssid)
32
0
{
33
0
}
34
#endif /* CONFIG_PASN */
35
36
37
int wpa_derive_ptk_ft(struct wpa_sm *sm, const unsigned char *src_addr,
38
          const struct wpa_eapol_key *key, struct wpa_ptk *ptk)
39
0
{
40
0
  u8 ptk_name[WPA_PMK_NAME_LEN];
41
0
  const u8 *anonce = key->key_nonce;
42
0
  int use_sha384 = wpa_key_mgmt_sha384(sm->key_mgmt);
43
0
  const u8 *mpmk;
44
0
  size_t mpmk_len, kdk_len;
45
0
  int ret = 0;
46
47
0
  if (sm->xxkey_len > 0) {
48
0
    mpmk = sm->xxkey;
49
0
    mpmk_len = sm->xxkey_len;
50
0
  } else if (sm->cur_pmksa) {
51
0
    mpmk = sm->cur_pmksa->pmk;
52
0
    mpmk_len = sm->cur_pmksa->pmk_len;
53
0
  } else {
54
0
    wpa_printf(MSG_DEBUG, "FT: XXKey not available for key "
55
0
         "derivation");
56
0
    return -1;
57
0
  }
58
59
0
  if (wpa_key_mgmt_sae_ext_key(sm->key_mgmt))
60
0
    sm->pmk_r0_len = mpmk_len;
61
0
  else
62
0
    sm->pmk_r0_len = use_sha384 ? SHA384_MAC_LEN : PMK_LEN;
63
0
  if (wpa_derive_pmk_r0(mpmk, mpmk_len, sm->ssid,
64
0
            sm->ssid_len, sm->mobility_domain,
65
0
            sm->r0kh_id, sm->r0kh_id_len, sm->own_addr,
66
0
            sm->pmk_r0, sm->pmk_r0_name, sm->key_mgmt) < 0)
67
0
    return -1;
68
0
  sm->pmk_r1_len = sm->pmk_r0_len;
69
0
  if (wpa_derive_pmk_r1(sm->pmk_r0, sm->pmk_r0_len, sm->pmk_r0_name,
70
0
            sm->r1kh_id, sm->own_addr, sm->pmk_r1,
71
0
            sm->pmk_r1_name) < 0)
72
0
    return -1;
73
74
0
  wpa_ft_pasn_store_r1kh(sm, src_addr);
75
76
0
  if (sm->force_kdk_derivation ||
77
0
      (sm->secure_ltf &&
78
0
       ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF)))
79
0
    kdk_len = WPA_KDK_MAX_LEN;
80
0
  else
81
0
    kdk_len = 0;
82
83
0
  ret = wpa_pmk_r1_to_ptk(sm->pmk_r1, sm->pmk_r1_len, sm->snonce,
84
0
        anonce, sm->own_addr, wpa_sm_get_auth_addr(sm),
85
0
        sm->pmk_r1_name, ptk, ptk_name, sm->key_mgmt,
86
0
        sm->pairwise_cipher, kdk_len);
87
0
  if (ret) {
88
0
    wpa_printf(MSG_ERROR, "FT: PTK derivation failed");
89
0
    return ret;
90
0
  }
91
92
0
  os_memcpy(sm->key_mobility_domain, sm->mobility_domain,
93
0
      MOBILITY_DOMAIN_ID_LEN);
94
95
#ifdef CONFIG_PASN
96
  if (sm->secure_ltf &&
97
      ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF))
98
    ret = wpa_ltf_keyseed(ptk, sm->key_mgmt, sm->pairwise_cipher);
99
#endif /* CONFIG_PASN */
100
101
0
  return ret;
102
0
}
103
104
105
/**
106
 * wpa_sm_set_ft_params - Set FT (IEEE 802.11r) parameters
107
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
108
 * @ies: Association Response IEs or %NULL to clear FT parameters
109
 * @ies_len: Length of ies buffer in octets
110
 * Returns: 0 on success, -1 on failure
111
 */
112
int wpa_sm_set_ft_params(struct wpa_sm *sm, const u8 *ies, size_t ies_len)
113
0
{
114
0
  struct wpa_ft_ies ft;
115
116
0
  if (sm == NULL)
117
0
    return 0;
118
119
0
  if (!get_ie(ies, ies_len, WLAN_EID_MOBILITY_DOMAIN)) {
120
0
    os_free(sm->assoc_resp_ies);
121
0
    sm->assoc_resp_ies = NULL;
122
0
    sm->assoc_resp_ies_len = 0;
123
0
    os_memset(sm->mobility_domain, 0, MOBILITY_DOMAIN_ID_LEN);
124
0
    os_memset(sm->r0kh_id, 0, FT_R0KH_ID_MAX_LEN);
125
0
    sm->r0kh_id_len = 0;
126
0
    os_memset(sm->r1kh_id, 0, FT_R1KH_ID_LEN);
127
0
    return 0;
128
0
  }
129
130
0
  if (wpa_ft_parse_ies(ies, ies_len, &ft, sm->key_mgmt, false) < 0)
131
0
    return -1;
132
133
0
  if (ft.mdie_len < MOBILITY_DOMAIN_ID_LEN + 1) {
134
0
    wpa_ft_parse_ies_free(&ft);
135
0
    return -1;
136
0
  }
137
138
0
  wpa_hexdump(MSG_DEBUG, "FT: Mobility domain",
139
0
        ft.mdie, MOBILITY_DOMAIN_ID_LEN);
140
0
  os_memcpy(sm->mobility_domain, ft.mdie, MOBILITY_DOMAIN_ID_LEN);
141
0
  sm->mdie_ft_capab = ft.mdie[MOBILITY_DOMAIN_ID_LEN];
142
0
  wpa_printf(MSG_DEBUG, "FT: Capability and Policy: 0x%02x",
143
0
       sm->mdie_ft_capab);
144
145
0
  if (ft.r0kh_id) {
146
0
    wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID",
147
0
          ft.r0kh_id, ft.r0kh_id_len);
148
0
    os_memcpy(sm->r0kh_id, ft.r0kh_id, ft.r0kh_id_len);
149
0
    sm->r0kh_id_len = ft.r0kh_id_len;
150
0
  } else {
151
    /* FIX: When should R0KH-ID be cleared? We need to keep the
152
     * old R0KH-ID in order to be able to use this during FT. */
153
    /*
154
     * os_memset(sm->r0kh_id, 0, FT_R0KH_ID_LEN);
155
     * sm->r0kh_id_len = 0;
156
     */
157
0
  }
158
159
0
  if (ft.r1kh_id) {
160
0
    wpa_hexdump(MSG_DEBUG, "FT: R1KH-ID",
161
0
          ft.r1kh_id, FT_R1KH_ID_LEN);
162
0
    os_memcpy(sm->r1kh_id, ft.r1kh_id, FT_R1KH_ID_LEN);
163
0
  } else
164
0
    os_memset(sm->r1kh_id, 0, FT_R1KH_ID_LEN);
165
166
0
  os_free(sm->assoc_resp_ies);
167
0
  sm->assoc_resp_ies = os_malloc(ft.mdie_len + 2 + ft.ftie_len + 2);
168
0
  if (sm->assoc_resp_ies) {
169
0
    u8 *pos = sm->assoc_resp_ies;
170
171
0
    os_memcpy(pos, ft.mdie - 2, ft.mdie_len + 2);
172
0
    pos += ft.mdie_len + 2;
173
174
0
    if (ft.ftie) {
175
0
      os_memcpy(pos, ft.ftie - 2, ft.ftie_len + 2);
176
0
      pos += ft.ftie_len + 2;
177
0
    }
178
0
    sm->assoc_resp_ies_len = pos - sm->assoc_resp_ies;
179
0
    wpa_hexdump(MSG_DEBUG, "FT: Stored MDIE and FTIE from "
180
0
          "(Re)Association Response",
181
0
          sm->assoc_resp_ies, sm->assoc_resp_ies_len);
182
0
  }
183
184
0
  wpa_ft_parse_ies_free(&ft);
185
0
  return 0;
186
0
}
187
188
189
/**
190
 * wpa_ft_gen_req_ies - Generate FT (IEEE 802.11r) IEs for Auth/ReAssoc Request
191
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
192
 * @len: Buffer for returning the length of the IEs
193
 * @anonce: ANonce or %NULL if not yet available
194
 * @pmk_name: PMKR0Name or PMKR1Name to be added into the RSN IE PMKID List
195
 * @kck: KCK for MIC or %NULL if no MIC is used
196
 * @kck_len: KCK length in octets
197
 * @target_ap: Target AP address
198
 * @ric_ies: Optional IE(s), e.g., WMM TSPEC(s), for RIC-Request or %NULL
199
 * @ric_ies_len: Length of ric_ies buffer in octets
200
 * @ap_mdie: Mobility Domain IE from the target AP
201
 * @omit_rsnxe: Whether RSNXE is omitted from Reassociation Request frame
202
 * Returns: Pointer to buffer with IEs or %NULL on failure
203
 *
204
 * Caller is responsible for freeing the returned buffer with os_free();
205
 */
206
static u8 * wpa_ft_gen_req_ies(struct wpa_sm *sm, size_t *len,
207
             const u8 *anonce, const u8 *pmk_name,
208
             const u8 *kck, size_t kck_len,
209
             const u8 *target_ap,
210
             const u8 *ric_ies, size_t ric_ies_len,
211
             const u8 *ap_mdie, int omit_rsnxe)
212
0
{
213
0
  size_t buf_len;
214
0
  u8 *buf, *pos, *ftie_len, *ftie_pos, *fte_mic, *elem_count;
215
0
  struct rsn_mdie *mdie;
216
0
  struct rsn_ie_hdr *rsnie;
217
0
  int mdie_len;
218
0
  u8 rsnxe[257];
219
0
  size_t rsnxe_len;
220
0
  int rsnxe_used;
221
0
  int res;
222
0
  u8 mic_control;
223
224
0
  sm->ft_completed = 0;
225
0
  sm->ft_reassoc_completed = 0;
226
227
0
  buf_len = 2 + sizeof(struct rsn_mdie) + 2 +
228
0
    sizeof(struct rsn_ftie_sha512) +
229
0
    2 + sm->r0kh_id_len + ric_ies_len + 100;
230
0
  buf = os_zalloc(buf_len);
231
0
  if (buf == NULL)
232
0
    return NULL;
233
0
  pos = buf;
234
235
  /* RSNIE[PMKR0Name/PMKR1Name] */
236
0
  rsnie = (struct rsn_ie_hdr *) pos;
237
0
  rsnie->elem_id = WLAN_EID_RSN;
238
0
  WPA_PUT_LE16(rsnie->version, RSN_VERSION);
239
0
  pos = (u8 *) (rsnie + 1);
240
241
  /* Group Suite Selector */
242
0
  if (!wpa_cipher_valid_group(sm->group_cipher)) {
243
0
    wpa_printf(MSG_WARNING, "FT: Invalid group cipher (%d)",
244
0
         sm->group_cipher);
245
0
    os_free(buf);
246
0
    return NULL;
247
0
  }
248
0
  RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
249
0
              sm->group_cipher));
250
0
  pos += RSN_SELECTOR_LEN;
251
252
  /* Pairwise Suite Count */
253
0
  WPA_PUT_LE16(pos, 1);
254
0
  pos += 2;
255
256
  /* Pairwise Suite List */
257
0
  if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
258
0
    wpa_printf(MSG_WARNING, "FT: Invalid pairwise cipher (%d)",
259
0
         sm->pairwise_cipher);
260
0
    os_free(buf);
261
0
    return NULL;
262
0
  }
263
0
  RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
264
0
              sm->pairwise_cipher));
265
0
  pos += RSN_SELECTOR_LEN;
266
267
  /* Authenticated Key Management Suite Count */
268
0
  WPA_PUT_LE16(pos, 1);
269
0
  pos += 2;
270
271
  /* Authenticated Key Management Suite List */
272
0
  if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X)
273
0
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_802_1X);
274
#ifdef CONFIG_SHA384
275
  else if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X_SHA384)
276
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_802_1X_SHA384);
277
#endif /* CONFIG_SHA384 */
278
0
  else if (sm->key_mgmt == WPA_KEY_MGMT_FT_PSK)
279
0
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_PSK);
280
0
  else if (sm->key_mgmt == WPA_KEY_MGMT_FT_SAE)
281
0
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_SAE);
282
0
  else if (sm->key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY)
283
0
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_SAE_EXT_KEY);
284
#ifdef CONFIG_FILS
285
  else if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA256)
286
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA256);
287
  else if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA384)
288
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA384);
289
#endif /* CONFIG_FILS */
290
0
  else {
291
0
    wpa_printf(MSG_WARNING, "FT: Invalid key management type (%d)",
292
0
         sm->key_mgmt);
293
0
    os_free(buf);
294
0
    return NULL;
295
0
  }
296
0
  pos += RSN_SELECTOR_LEN;
297
298
  /* RSN Capabilities */
299
0
  WPA_PUT_LE16(pos, rsn_supp_capab(sm));
300
0
  pos += 2;
301
302
  /* PMKID Count */
303
0
  WPA_PUT_LE16(pos, 1);
304
0
  pos += 2;
305
306
  /* PMKID List [PMKR0Name/PMKR1Name] */
307
0
  os_memcpy(pos, pmk_name, WPA_PMK_NAME_LEN);
308
0
  pos += WPA_PMK_NAME_LEN;
309
310
  /* Management Group Cipher Suite */
311
0
  switch (sm->mgmt_group_cipher) {
312
0
  case WPA_CIPHER_AES_128_CMAC:
313
0
    RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
314
0
    pos += RSN_SELECTOR_LEN;
315
0
    break;
316
0
  case WPA_CIPHER_BIP_GMAC_128:
317
0
    RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_BIP_GMAC_128);
318
0
    pos += RSN_SELECTOR_LEN;
319
0
    break;
320
0
  case WPA_CIPHER_BIP_GMAC_256:
321
0
    RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_BIP_GMAC_256);
322
0
    pos += RSN_SELECTOR_LEN;
323
0
    break;
324
0
  case WPA_CIPHER_BIP_CMAC_256:
325
0
    RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_BIP_CMAC_256);
326
0
    pos += RSN_SELECTOR_LEN;
327
0
    break;
328
0
  }
329
330
0
  rsnie->len = (pos - (u8 *) rsnie) - 2;
331
332
  /* MDIE */
333
0
  mdie_len = wpa_ft_add_mdie(sm, pos, buf_len - (pos - buf), ap_mdie);
334
0
  if (mdie_len <= 0) {
335
0
    os_free(buf);
336
0
    return NULL;
337
0
  }
338
0
  mdie = (struct rsn_mdie *) (pos + 2);
339
0
  pos += mdie_len;
340
341
  /* FTIE[SNonce, [R1KH-ID,] R0KH-ID ] */
342
0
  ftie_pos = pos;
343
0
  *pos++ = WLAN_EID_FAST_BSS_TRANSITION;
344
0
  ftie_len = pos++;
345
0
  rsnxe_used = wpa_key_mgmt_sae(sm->key_mgmt) && anonce &&
346
0
    (sm->sae_pwe == SAE_PWE_HASH_TO_ELEMENT ||
347
0
     sm->sae_pwe == SAE_PWE_BOTH);
348
#ifdef CONFIG_TESTING_OPTIONS
349
  if (anonce && sm->ft_rsnxe_used) {
350
    rsnxe_used = sm->ft_rsnxe_used == 1;
351
    wpa_printf(MSG_DEBUG, "TESTING: FT: Force RSNXE Used %d",
352
         rsnxe_used);
353
  }
354
#endif /* CONFIG_TESTING_OPTIONS */
355
0
  mic_control = rsnxe_used ? FTE_MIC_CTRL_RSNXE_USED : 0;
356
0
  if (sm->key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY &&
357
0
      sm->pmk_r0_len == SHA512_MAC_LEN) {
358
0
    struct rsn_ftie_sha512 *ftie;
359
360
0
    ftie = (struct rsn_ftie_sha512 *) pos;
361
0
    mic_control |= FTE_MIC_LEN_32 << FTE_MIC_CTRL_MIC_LEN_SHIFT;
362
0
    ftie->mic_control[0] = mic_control;
363
0
    fte_mic = ftie->mic;
364
0
    elem_count = &ftie->mic_control[1];
365
0
    pos += sizeof(*ftie);
366
0
    os_memcpy(ftie->snonce, sm->snonce, WPA_NONCE_LEN);
367
0
    if (anonce)
368
0
      os_memcpy(ftie->anonce, anonce, WPA_NONCE_LEN);
369
0
  } else if ((sm->key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY &&
370
0
        sm->pmk_r0_len == SHA384_MAC_LEN) ||
371
0
       wpa_key_mgmt_sha384(sm->key_mgmt)) {
372
0
    struct rsn_ftie_sha384 *ftie;
373
374
0
    ftie = (struct rsn_ftie_sha384 *) pos;
375
0
    mic_control |= FTE_MIC_LEN_24 << FTE_MIC_CTRL_MIC_LEN_SHIFT;
376
0
    ftie->mic_control[0] = mic_control;
377
0
    fte_mic = ftie->mic;
378
0
    elem_count = &ftie->mic_control[1];
379
0
    pos += sizeof(*ftie);
380
0
    os_memcpy(ftie->snonce, sm->snonce, WPA_NONCE_LEN);
381
0
    if (anonce)
382
0
      os_memcpy(ftie->anonce, anonce, WPA_NONCE_LEN);
383
0
  } else {
384
0
    struct rsn_ftie *ftie;
385
386
0
    ftie = (struct rsn_ftie *) pos;
387
0
    mic_control |= FTE_MIC_LEN_16 << FTE_MIC_CTRL_MIC_LEN_SHIFT;
388
0
    ftie->mic_control[0] = mic_control;
389
0
    fte_mic = ftie->mic;
390
0
    elem_count = &ftie->mic_control[1];
391
0
    pos += sizeof(*ftie);
392
0
    os_memcpy(ftie->snonce, sm->snonce, WPA_NONCE_LEN);
393
0
    if (anonce)
394
0
      os_memcpy(ftie->anonce, anonce, WPA_NONCE_LEN);
395
0
  }
396
0
  if (kck) {
397
    /* R1KH-ID sub-element in third FT message */
398
0
    *pos++ = FTIE_SUBELEM_R1KH_ID;
399
0
    *pos++ = FT_R1KH_ID_LEN;
400
0
    os_memcpy(pos, sm->r1kh_id, FT_R1KH_ID_LEN);
401
0
    pos += FT_R1KH_ID_LEN;
402
0
  }
403
  /* R0KH-ID sub-element */
404
0
  *pos++ = FTIE_SUBELEM_R0KH_ID;
405
0
  *pos++ = sm->r0kh_id_len;
406
0
  os_memcpy(pos, sm->r0kh_id, sm->r0kh_id_len);
407
0
  pos += sm->r0kh_id_len;
408
#ifdef CONFIG_OCV
409
  if (kck && wpa_sm_ocv_enabled(sm)) {
410
    /* OCI sub-element in the third FT message */
411
    struct wpa_channel_info ci;
412
413
    if (wpa_sm_channel_info(sm, &ci) != 0) {
414
      wpa_printf(MSG_WARNING,
415
           "Failed to get channel info for OCI element in FTE");
416
      os_free(buf);
417
      return NULL;
418
    }
419
#ifdef CONFIG_TESTING_OPTIONS
420
    if (sm->oci_freq_override_ft_assoc) {
421
      wpa_printf(MSG_INFO,
422
           "TEST: Override OCI KDE frequency %d -> %d MHz",
423
           ci.frequency, sm->oci_freq_override_ft_assoc);
424
      ci.frequency = sm->oci_freq_override_ft_assoc;
425
    }
426
#endif /* CONFIG_TESTING_OPTIONS */
427
428
    *pos++ = FTIE_SUBELEM_OCI;
429
    *pos++ = OCV_OCI_LEN;
430
    if (ocv_insert_oci(&ci, &pos) < 0) {
431
      os_free(buf);
432
      return NULL;
433
    }
434
  }
435
#endif /* CONFIG_OCV */
436
0
  *ftie_len = pos - ftie_len - 1;
437
438
0
  if (ric_ies) {
439
    /* RIC Request */
440
0
    os_memcpy(pos, ric_ies, ric_ies_len);
441
0
    pos += ric_ies_len;
442
0
  }
443
444
0
  if (omit_rsnxe) {
445
0
    rsnxe_len = 0;
446
0
  } else {
447
0
    res = wpa_gen_rsnxe(sm, rsnxe, sizeof(rsnxe));
448
0
    if (res < 0) {
449
0
      os_free(buf);
450
0
      return NULL;
451
0
    }
452
0
    rsnxe_len = res;
453
0
  }
454
455
0
  if (kck) {
456
    /*
457
     * IEEE Std 802.11r-2008, 11A.8.4
458
     * MIC shall be calculated over:
459
     * non-AP STA MAC address
460
     * Target AP MAC address
461
     * Transaction seq number (5 for ReassocReq, 3 otherwise)
462
     * RSN IE
463
     * MDIE
464
     * FTIE (with MIC field set to 0)
465
     * RIC-Request (if present)
466
     * RSNXE (if present)
467
     */
468
    /* Information element count */
469
0
    *elem_count = 3 + ieee802_11_ie_count(ric_ies, ric_ies_len);
470
0
    if (rsnxe_len)
471
0
      *elem_count += 1;
472
0
    if (wpa_ft_mic(sm->key_mgmt, kck, kck_len,
473
0
             sm->own_addr, target_ap, 5,
474
0
             ((u8 *) mdie) - 2, 2 + sizeof(*mdie),
475
0
             ftie_pos, 2 + *ftie_len,
476
0
             (u8 *) rsnie, 2 + rsnie->len, ric_ies,
477
0
             ric_ies_len, rsnxe_len ? rsnxe : NULL, rsnxe_len,
478
0
             NULL,
479
0
             fte_mic) < 0) {
480
0
      wpa_printf(MSG_INFO, "FT: Failed to calculate MIC");
481
0
      os_free(buf);
482
0
      return NULL;
483
0
    }
484
0
  }
485
486
0
  *len = pos - buf;
487
488
0
  return buf;
489
0
}
490
491
492
static int wpa_ft_install_ptk(struct wpa_sm *sm, const u8 *bssid)
493
0
{
494
0
  int keylen;
495
0
  enum wpa_alg alg;
496
0
  u8 null_rsc[6] = { 0, 0, 0, 0, 0, 0 };
497
498
0
  wpa_printf(MSG_DEBUG, "FT: Installing PTK to the driver.");
499
500
0
  if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
501
0
    wpa_printf(MSG_WARNING, "FT: Unsupported pairwise cipher %d",
502
0
         sm->pairwise_cipher);
503
0
    return -1;
504
0
  }
505
506
0
  alg = wpa_cipher_to_alg(sm->pairwise_cipher);
507
0
  keylen = wpa_cipher_key_len(sm->pairwise_cipher);
508
509
  /* TODO: AP MLD address for MLO */
510
0
  if (wpa_sm_set_key(sm, -1, alg, bssid, 0, 1, null_rsc, sizeof(null_rsc),
511
0
         (u8 *) sm->ptk.tk, keylen,
512
0
         KEY_FLAG_PAIRWISE_RX_TX) < 0) {
513
0
    wpa_printf(MSG_WARNING, "FT: Failed to set PTK to the driver");
514
0
    return -1;
515
0
  }
516
0
  sm->tk_set = true;
517
518
0
  wpa_sm_store_ptk(sm, bssid, sm->pairwise_cipher,
519
0
       sm->dot11RSNAConfigPMKLifetime, &sm->ptk);
520
0
  return 0;
521
0
}
522
523
524
/**
525
 * wpa_ft_prepare_auth_request - Generate over-the-air auth request
526
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
527
 * @mdie: Target AP MDIE
528
 * Returns: 0 on success, -1 on failure
529
 */
530
int wpa_ft_prepare_auth_request(struct wpa_sm *sm, const u8 *mdie)
531
0
{
532
0
  u8 *ft_ies;
533
0
  size_t ft_ies_len;
534
535
  /* Generate a new SNonce */
536
0
  if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
537
0
    wpa_printf(MSG_INFO, "FT: Failed to generate a new SNonce");
538
0
    return -1;
539
0
  }
540
541
0
  ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, NULL, sm->pmk_r0_name,
542
0
            NULL, 0, sm->bssid, NULL, 0, mdie, 0);
543
0
  if (ft_ies) {
544
0
    wpa_sm_update_ft_ies(sm, sm->mobility_domain,
545
0
             ft_ies, ft_ies_len);
546
0
    os_free(ft_ies);
547
0
  }
548
549
0
  return 0;
550
0
}
551
552
553
int wpa_ft_add_mdie(struct wpa_sm *sm, u8 *buf, size_t buf_len,
554
        const u8 *ap_mdie)
555
0
{
556
0
  u8 *pos = buf;
557
0
  struct rsn_mdie *mdie;
558
559
0
  if (buf_len < 2 + sizeof(*mdie)) {
560
0
    wpa_printf(MSG_INFO,
561
0
         "FT: Failed to add MDIE: short buffer, length=%zu",
562
0
         buf_len);
563
0
    return 0;
564
0
  }
565
566
0
  *pos++ = WLAN_EID_MOBILITY_DOMAIN;
567
0
  *pos++ = sizeof(*mdie);
568
0
  mdie = (struct rsn_mdie *) pos;
569
0
  os_memcpy(mdie->mobility_domain, sm->mobility_domain,
570
0
      MOBILITY_DOMAIN_ID_LEN);
571
0
  mdie->ft_capab = ap_mdie && ap_mdie[1] >= 3 ? ap_mdie[4] :
572
0
    sm->mdie_ft_capab;
573
574
0
  return 2 + sizeof(*mdie);
575
0
}
576
577
578
const u8 * wpa_sm_get_ft_md(struct wpa_sm *sm)
579
0
{
580
0
  return sm->mobility_domain;
581
0
}
582
583
584
int wpa_ft_process_response(struct wpa_sm *sm, const u8 *ies, size_t ies_len,
585
          int ft_action, const u8 *target_ap,
586
          const u8 *ric_ies, size_t ric_ies_len)
587
0
{
588
0
  u8 *ft_ies;
589
0
  size_t ft_ies_len;
590
0
  struct wpa_ft_ies parse;
591
0
  struct rsn_mdie *mdie;
592
0
  u8 ptk_name[WPA_PMK_NAME_LEN];
593
0
  int ret = -1, res;
594
0
  const u8 *bssid;
595
0
  const u8 *kck;
596
0
  size_t kck_len, kdk_len;
597
598
0
  os_memset(&parse, 0, sizeof(parse));
599
600
0
  wpa_hexdump(MSG_DEBUG, "FT: Response IEs", ies, ies_len);
601
0
  wpa_hexdump(MSG_DEBUG, "FT: RIC IEs", ric_ies, ric_ies_len);
602
603
0
  if (ft_action) {
604
0
    if (!sm->over_the_ds_in_progress) {
605
0
      wpa_printf(MSG_DEBUG, "FT: No over-the-DS in progress "
606
0
           "- drop FT Action Response");
607
0
      goto fail;
608
0
    }
609
610
0
    if (!ether_addr_equal(target_ap, sm->target_ap)) {
611
0
      wpa_printf(MSG_DEBUG, "FT: No over-the-DS in progress "
612
0
           "with this Target AP - drop FT Action "
613
0
           "Response");
614
0
      goto fail;
615
0
    }
616
0
  }
617
618
0
  if (!wpa_key_mgmt_ft(sm->key_mgmt)) {
619
0
    wpa_printf(MSG_DEBUG, "FT: Reject FT IEs since FT is not "
620
0
         "enabled for this connection");
621
0
    goto fail;
622
0
  }
623
624
0
  if (wpa_ft_parse_ies(ies, ies_len, &parse, sm->key_mgmt,
625
0
           !ft_action) < 0) {
626
0
    wpa_printf(MSG_DEBUG, "FT: Failed to parse IEs");
627
0
    goto fail;
628
0
  }
629
630
0
  mdie = (struct rsn_mdie *) parse.mdie;
631
0
  if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
632
0
      os_memcmp(mdie->mobility_domain, sm->mobility_domain,
633
0
          MOBILITY_DOMAIN_ID_LEN) != 0) {
634
0
    wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
635
0
    goto fail;
636
0
  }
637
638
0
  if (!parse.ftie || !parse.fte_anonce || !parse.fte_snonce) {
639
0
    wpa_printf(MSG_DEBUG, "FT: Invalid FTE");
640
0
    goto fail;
641
0
  }
642
643
0
  if (os_memcmp(parse.fte_snonce, sm->snonce, WPA_NONCE_LEN) != 0) {
644
0
    wpa_printf(MSG_DEBUG, "FT: SNonce mismatch in FTIE");
645
0
    wpa_hexdump(MSG_DEBUG, "FT: Received SNonce",
646
0
          parse.fte_snonce, WPA_NONCE_LEN);
647
0
    wpa_hexdump(MSG_DEBUG, "FT: Expected SNonce",
648
0
          sm->snonce, WPA_NONCE_LEN);
649
0
    goto fail;
650
0
  }
651
652
0
  if (parse.r0kh_id == NULL) {
653
0
    wpa_printf(MSG_DEBUG, "FT: No R0KH-ID subelem in FTIE");
654
0
    goto fail;
655
0
  }
656
657
0
  if (parse.r0kh_id_len != sm->r0kh_id_len ||
658
0
      os_memcmp_const(parse.r0kh_id, sm->r0kh_id, parse.r0kh_id_len) != 0)
659
0
  {
660
0
    wpa_printf(MSG_DEBUG, "FT: R0KH-ID in FTIE did not match with "
661
0
         "the current R0KH-ID");
662
0
    wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID in FTIE",
663
0
          parse.r0kh_id, parse.r0kh_id_len);
664
0
    wpa_hexdump(MSG_DEBUG, "FT: The current R0KH-ID",
665
0
          sm->r0kh_id, sm->r0kh_id_len);
666
0
    goto fail;
667
0
  }
668
669
0
  if (parse.r1kh_id == NULL) {
670
0
    wpa_printf(MSG_DEBUG, "FT: No R1KH-ID subelem in FTIE");
671
0
    goto fail;
672
0
  }
673
674
0
  if (parse.rsn_pmkid == NULL ||
675
0
      os_memcmp_const(parse.rsn_pmkid, sm->pmk_r0_name, WPA_PMK_NAME_LEN))
676
0
  {
677
0
    wpa_printf(MSG_DEBUG, "FT: No matching PMKR0Name (PMKID) in "
678
0
         "RSNIE");
679
0
    goto fail;
680
0
  }
681
682
0
  if (sm->mfp == 2 && !(parse.rsn_capab & WPA_CAPABILITY_MFPC)) {
683
0
    wpa_printf(MSG_INFO,
684
0
         "FT: Target AP does not support PMF, but local configuration requires that");
685
0
    goto fail;
686
0
  }
687
688
0
  os_memcpy(sm->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN);
689
0
  wpa_hexdump(MSG_DEBUG, "FT: R1KH-ID", sm->r1kh_id, FT_R1KH_ID_LEN);
690
0
  wpa_hexdump(MSG_DEBUG, "FT: SNonce", sm->snonce, WPA_NONCE_LEN);
691
0
  wpa_hexdump(MSG_DEBUG, "FT: ANonce", parse.fte_anonce, WPA_NONCE_LEN);
692
0
  os_memcpy(sm->anonce, parse.fte_anonce, WPA_NONCE_LEN);
693
0
  if (wpa_derive_pmk_r1(sm->pmk_r0, sm->pmk_r0_len, sm->pmk_r0_name,
694
0
            sm->r1kh_id, sm->own_addr, sm->pmk_r1,
695
0
            sm->pmk_r1_name) < 0)
696
0
    goto fail;
697
0
  sm->pmk_r1_len = sm->pmk_r0_len;
698
699
0
  bssid = target_ap;
700
701
0
  wpa_ft_pasn_store_r1kh(sm, bssid);
702
703
0
  if (sm->force_kdk_derivation ||
704
0
      (sm->secure_ltf &&
705
0
       ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF)))
706
0
    kdk_len = WPA_KDK_MAX_LEN;
707
0
  else
708
0
    kdk_len = 0;
709
710
  /* TODO: AP MLD address for MLO */
711
0
  if (wpa_pmk_r1_to_ptk(sm->pmk_r1, sm->pmk_r1_len, sm->snonce,
712
0
            parse.fte_anonce, sm->own_addr, bssid,
713
0
            sm->pmk_r1_name, &sm->ptk, ptk_name, sm->key_mgmt,
714
0
            sm->pairwise_cipher,
715
0
            kdk_len) < 0)
716
0
    goto fail;
717
718
0
  os_memcpy(sm->key_mobility_domain, sm->mobility_domain,
719
0
      MOBILITY_DOMAIN_ID_LEN);
720
721
#ifdef CONFIG_PASN
722
  if (sm->secure_ltf &&
723
      ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF) &&
724
      wpa_ltf_keyseed(&sm->ptk, sm->key_mgmt, sm->pairwise_cipher)) {
725
    wpa_printf(MSG_DEBUG, "FT: Failed to derive LTF keyseed");
726
    goto fail;
727
  }
728
#endif /* CONFIG_PASN */
729
730
0
  if (wpa_key_mgmt_fils(sm->key_mgmt)) {
731
0
    kck = sm->ptk.kck2;
732
0
    kck_len = sm->ptk.kck2_len;
733
0
  } else {
734
0
    kck = sm->ptk.kck;
735
0
    kck_len = sm->ptk.kck_len;
736
0
  }
737
0
  ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, parse.fte_anonce,
738
0
            sm->pmk_r1_name,
739
0
            kck, kck_len, bssid,
740
0
            ric_ies, ric_ies_len,
741
0
            parse.mdie ? parse.mdie - 2 : NULL,
742
0
            !sm->ap_rsnxe);
743
0
  if (ft_ies) {
744
0
    wpa_sm_update_ft_ies(sm, sm->mobility_domain,
745
0
             ft_ies, ft_ies_len);
746
0
    os_free(ft_ies);
747
0
  }
748
749
0
  wpa_sm_mark_authenticated(sm, bssid);
750
0
  res = wpa_ft_install_ptk(sm, bssid);
751
0
  if (res) {
752
    /*
753
     * Some drivers do not support key configuration when we are
754
     * not associated with the target AP. Work around this by
755
     * trying again after the following reassociation gets
756
     * completed.
757
     */
758
0
    wpa_printf(MSG_DEBUG, "FT: Failed to set PTK prior to "
759
0
         "association - try again after reassociation");
760
0
    sm->set_ptk_after_assoc = 1;
761
0
  } else
762
0
    sm->set_ptk_after_assoc = 0;
763
764
0
  sm->ft_completed = 1;
765
0
  if (ft_action) {
766
    /*
767
     * The caller is expected trigger re-association with the
768
     * Target AP.
769
     */
770
0
    os_memcpy(sm->bssid, target_ap, ETH_ALEN);
771
0
  }
772
773
0
  ret = 0;
774
0
fail:
775
0
  wpa_ft_parse_ies_free(&parse);
776
0
  return ret;
777
0
}
778
779
780
int wpa_ft_is_completed(struct wpa_sm *sm)
781
2.02k
{
782
2.02k
  if (sm == NULL)
783
0
    return 0;
784
785
2.02k
  if (!wpa_key_mgmt_ft(sm->key_mgmt))
786
2.02k
    return 0;
787
788
0
  return sm->ft_completed;
789
2.02k
}
790
791
792
void wpa_reset_ft_completed(struct wpa_sm *sm)
793
0
{
794
0
  if (sm != NULL)
795
0
    sm->ft_completed = 0;
796
0
}
797
798
799
static int wpa_ft_process_gtk_subelem(struct wpa_sm *sm, const u8 *gtk_elem,
800
              size_t gtk_elem_len)
801
0
{
802
0
  u8 gtk[32];
803
0
  int keyidx;
804
0
  enum wpa_alg alg;
805
0
  size_t gtk_len, keylen, rsc_len;
806
0
  const u8 *kek;
807
0
  size_t kek_len;
808
809
0
  if (wpa_key_mgmt_fils(sm->key_mgmt)) {
810
0
    kek = sm->ptk.kek2;
811
0
    kek_len = sm->ptk.kek2_len;
812
0
  } else {
813
0
    kek = sm->ptk.kek;
814
0
    kek_len = sm->ptk.kek_len;
815
0
  }
816
817
0
  if (gtk_elem == NULL) {
818
0
    wpa_printf(MSG_DEBUG, "FT: No GTK included in FTIE");
819
0
    return 0;
820
0
  }
821
822
0
  wpa_hexdump_key(MSG_DEBUG, "FT: Received GTK in Reassoc Resp",
823
0
      gtk_elem, gtk_elem_len);
824
825
0
  if (gtk_elem_len < 11 + 24 || (gtk_elem_len - 11) % 8 ||
826
0
      gtk_elem_len - 19 > sizeof(gtk)) {
827
0
    wpa_printf(MSG_DEBUG, "FT: Invalid GTK sub-elem "
828
0
         "length %lu", (unsigned long) gtk_elem_len);
829
0
    return -1;
830
0
  }
831
0
  gtk_len = gtk_elem_len - 19;
832
0
  if (aes_unwrap(kek, kek_len, gtk_len / 8, gtk_elem + 11, gtk)) {
833
0
    wpa_printf(MSG_WARNING, "FT: AES unwrap failed - could not "
834
0
         "decrypt GTK");
835
0
    return -1;
836
0
  }
837
838
0
  keylen = wpa_cipher_key_len(sm->group_cipher);
839
0
  rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
840
0
  alg = wpa_cipher_to_alg(sm->group_cipher);
841
0
  if (alg == WPA_ALG_NONE) {
842
0
    wpa_printf(MSG_WARNING, "WPA: Unsupported Group Cipher %d",
843
0
         sm->group_cipher);
844
0
    return -1;
845
0
  }
846
847
0
  if (gtk_len < keylen) {
848
0
    wpa_printf(MSG_DEBUG, "FT: Too short GTK in FTIE");
849
0
    return -1;
850
0
  }
851
852
  /* Key Info[2] | Key Length[1] | RSC[8] | Key[5..32]. */
853
854
0
  keyidx = WPA_GET_LE16(gtk_elem) & 0x03;
855
856
0
  if (gtk_elem[2] != keylen) {
857
0
    wpa_printf(MSG_DEBUG, "FT: GTK length mismatch: received %d "
858
0
         "negotiated %lu",
859
0
         gtk_elem[2], (unsigned long) keylen);
860
0
    return -1;
861
0
  }
862
863
0
  wpa_hexdump_key(MSG_DEBUG, "FT: GTK from Reassoc Resp", gtk, keylen);
864
0
  if (sm->group_cipher == WPA_CIPHER_TKIP) {
865
    /* Swap Tx/Rx keys for Michael MIC */
866
0
    u8 tmp[8];
867
0
    os_memcpy(tmp, gtk + 16, 8);
868
0
    os_memcpy(gtk + 16, gtk + 24, 8);
869
0
    os_memcpy(gtk + 24, tmp, 8);
870
0
  }
871
0
  if (wpa_sm_set_key(sm, -1, alg, broadcast_ether_addr, keyidx, 0,
872
0
         gtk_elem + 3, rsc_len, gtk, keylen,
873
0
         KEY_FLAG_GROUP_RX) < 0) {
874
0
    wpa_printf(MSG_WARNING, "WPA: Failed to set GTK to the "
875
0
         "driver.");
876
0
    return -1;
877
0
  }
878
879
0
  return 0;
880
0
}
881
882
883
static int wpa_ft_process_igtk_subelem(struct wpa_sm *sm, const u8 *igtk_elem,
884
               size_t igtk_elem_len)
885
0
{
886
0
  u8 igtk[WPA_IGTK_MAX_LEN];
887
0
  size_t igtk_len;
888
0
  u16 keyidx;
889
0
  const u8 *kek;
890
0
  size_t kek_len;
891
892
0
  if (wpa_key_mgmt_fils(sm->key_mgmt)) {
893
0
    kek = sm->ptk.kek2;
894
0
    kek_len = sm->ptk.kek2_len;
895
0
  } else {
896
0
    kek = sm->ptk.kek;
897
0
    kek_len = sm->ptk.kek_len;
898
0
  }
899
900
0
  if (sm->mgmt_group_cipher != WPA_CIPHER_AES_128_CMAC &&
901
0
      sm->mgmt_group_cipher != WPA_CIPHER_BIP_GMAC_128 &&
902
0
      sm->mgmt_group_cipher != WPA_CIPHER_BIP_GMAC_256 &&
903
0
      sm->mgmt_group_cipher != WPA_CIPHER_BIP_CMAC_256)
904
0
    return 0;
905
906
0
  if (igtk_elem == NULL) {
907
0
    wpa_printf(MSG_DEBUG, "FT: No IGTK included in FTIE");
908
0
    return 0;
909
0
  }
910
911
0
  wpa_hexdump_key(MSG_DEBUG, "FT: Received IGTK in Reassoc Resp",
912
0
      igtk_elem, igtk_elem_len);
913
914
0
  igtk_len = wpa_cipher_key_len(sm->mgmt_group_cipher);
915
0
  if (igtk_elem_len != 2 + 6 + 1 + igtk_len + 8) {
916
0
    wpa_printf(MSG_DEBUG, "FT: Invalid IGTK sub-elem "
917
0
         "length %lu", (unsigned long) igtk_elem_len);
918
0
    return -1;
919
0
  }
920
0
  if (igtk_elem[8] != igtk_len) {
921
0
    wpa_printf(MSG_DEBUG, "FT: Invalid IGTK sub-elem Key Length "
922
0
         "%d", igtk_elem[8]);
923
0
    return -1;
924
0
  }
925
926
0
  if (aes_unwrap(kek, kek_len, igtk_len / 8, igtk_elem + 9, igtk)) {
927
0
    wpa_printf(MSG_WARNING, "FT: AES unwrap failed - could not "
928
0
         "decrypt IGTK");
929
0
    return -1;
930
0
  }
931
932
  /* KeyID[2] | IPN[6] | Key Length[1] | Key[16+8] */
933
934
0
  keyidx = WPA_GET_LE16(igtk_elem);
935
936
0
  wpa_hexdump_key(MSG_DEBUG, "FT: IGTK from Reassoc Resp", igtk,
937
0
      igtk_len);
938
0
  if (wpa_sm_set_key(sm, -1, wpa_cipher_to_alg(sm->mgmt_group_cipher),
939
0
         broadcast_ether_addr, keyidx, 0,
940
0
         igtk_elem + 2, 6, igtk, igtk_len,
941
0
         KEY_FLAG_GROUP_RX) < 0) {
942
0
    wpa_printf(MSG_WARNING, "WPA: Failed to set IGTK to the "
943
0
         "driver.");
944
0
    forced_memzero(igtk, sizeof(igtk));
945
0
    return -1;
946
0
  }
947
0
  forced_memzero(igtk, sizeof(igtk));
948
949
0
  return 0;
950
0
}
951
952
953
static int wpa_ft_process_bigtk_subelem(struct wpa_sm *sm, const u8 *bigtk_elem,
954
               size_t bigtk_elem_len)
955
0
{
956
0
  u8 bigtk[WPA_BIGTK_MAX_LEN];
957
0
  size_t bigtk_len;
958
0
  u16 keyidx;
959
0
  const u8 *kek;
960
0
  size_t kek_len;
961
962
0
  if (!sm->beacon_prot || !bigtk_elem ||
963
0
      (sm->mgmt_group_cipher != WPA_CIPHER_AES_128_CMAC &&
964
0
       sm->mgmt_group_cipher != WPA_CIPHER_BIP_GMAC_128 &&
965
0
       sm->mgmt_group_cipher != WPA_CIPHER_BIP_GMAC_256 &&
966
0
       sm->mgmt_group_cipher != WPA_CIPHER_BIP_CMAC_256))
967
0
    return 0;
968
969
0
  if (wpa_key_mgmt_fils(sm->key_mgmt)) {
970
0
    kek = sm->ptk.kek2;
971
0
    kek_len = sm->ptk.kek2_len;
972
0
  } else {
973
0
    kek = sm->ptk.kek;
974
0
    kek_len = sm->ptk.kek_len;
975
0
  }
976
977
0
  wpa_hexdump_key(MSG_DEBUG, "FT: Received BIGTK in Reassoc Resp",
978
0
      bigtk_elem, bigtk_elem_len);
979
980
0
  bigtk_len = wpa_cipher_key_len(sm->mgmt_group_cipher);
981
0
  if (bigtk_elem_len != 2 + 6 + 1 + bigtk_len + 8) {
982
0
    wpa_printf(MSG_DEBUG,
983
0
         "FT: Invalid BIGTK sub-elem length %lu",
984
0
         (unsigned long) bigtk_elem_len);
985
0
    return -1;
986
0
  }
987
0
  if (bigtk_elem[8] != bigtk_len) {
988
0
    wpa_printf(MSG_DEBUG,
989
0
         "FT: Invalid BIGTK sub-elem Key Length %d",
990
0
         bigtk_elem[8]);
991
0
    return -1;
992
0
  }
993
994
0
  if (aes_unwrap(kek, kek_len, bigtk_len / 8, bigtk_elem + 9, bigtk)) {
995
0
    wpa_printf(MSG_WARNING,
996
0
         "FT: AES unwrap failed - could not decrypt BIGTK");
997
0
    return -1;
998
0
  }
999
1000
  /* KeyID[2] | IPN[6] | Key Length[1] | Key[16+8] */
1001
1002
0
  keyidx = WPA_GET_LE16(bigtk_elem);
1003
1004
0
  wpa_hexdump_key(MSG_DEBUG, "FT: BIGTK from Reassoc Resp", bigtk,
1005
0
      bigtk_len);
1006
0
  if (wpa_sm_set_key(sm, -1, wpa_cipher_to_alg(sm->mgmt_group_cipher),
1007
0
         broadcast_ether_addr, keyidx, 0,
1008
0
         bigtk_elem + 2, 6, bigtk, bigtk_len,
1009
0
         KEY_FLAG_GROUP_RX) < 0) {
1010
0
    wpa_printf(MSG_WARNING,
1011
0
         "WPA: Failed to set BIGTK to the driver");
1012
0
    forced_memzero(bigtk, sizeof(bigtk));
1013
0
    return -1;
1014
0
  }
1015
0
  forced_memzero(bigtk, sizeof(bigtk));
1016
1017
0
  return 0;
1018
0
}
1019
1020
1021
int wpa_ft_validate_reassoc_resp(struct wpa_sm *sm, const u8 *ies,
1022
         size_t ies_len, const u8 *src_addr)
1023
0
{
1024
0
  struct wpa_ft_ies parse;
1025
0
  struct rsn_mdie *mdie;
1026
0
  unsigned int count;
1027
0
  u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
1028
0
  const u8 *kck;
1029
0
  size_t kck_len;
1030
0
  int own_rsnxe_used;
1031
0
  size_t mic_len;
1032
0
  int ret = -1;
1033
1034
0
  os_memset(&parse, 0, sizeof(parse));
1035
1036
0
  wpa_hexdump(MSG_DEBUG, "FT: Response IEs", ies, ies_len);
1037
1038
0
  if (!wpa_key_mgmt_ft(sm->key_mgmt)) {
1039
0
    wpa_printf(MSG_DEBUG, "FT: Reject FT IEs since FT is not "
1040
0
         "enabled for this connection");
1041
0
    goto fail;
1042
0
  }
1043
1044
0
  if (sm->ft_reassoc_completed) {
1045
0
    wpa_printf(MSG_DEBUG, "FT: Reassociation has already been completed for this FT protocol instance - ignore unexpected retransmission");
1046
0
    return 0;
1047
0
  }
1048
1049
0
  if (wpa_ft_parse_ies(ies, ies_len, &parse, sm->key_mgmt, true) < 0) {
1050
0
    wpa_printf(MSG_DEBUG, "FT: Failed to parse IEs");
1051
0
    goto fail;
1052
0
  }
1053
1054
0
  mdie = (struct rsn_mdie *) parse.mdie;
1055
0
  if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
1056
0
      os_memcmp(mdie->mobility_domain, sm->mobility_domain,
1057
0
          MOBILITY_DOMAIN_ID_LEN) != 0) {
1058
0
    wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
1059
0
    goto fail;
1060
0
  }
1061
1062
0
  if (sm->key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY &&
1063
0
      sm->pmk_r1_len == SHA512_MAC_LEN)
1064
0
    mic_len = 32;
1065
0
  else if ((sm->key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY &&
1066
0
      sm->pmk_r1_len == SHA384_MAC_LEN) ||
1067
0
     wpa_key_mgmt_sha384(sm->key_mgmt))
1068
0
    mic_len = 24;
1069
0
  else
1070
0
    mic_len = 16;
1071
1072
0
  if (!parse.ftie || !parse.fte_anonce || !parse.fte_snonce ||
1073
0
      parse.fte_mic_len != mic_len) {
1074
0
    wpa_printf(MSG_DEBUG,
1075
0
         "FT: Invalid FTE (fte_mic_len=%zu mic_len=%zu)",
1076
0
         parse.fte_mic_len, mic_len);
1077
0
    goto fail;
1078
0
  }
1079
1080
0
  if (os_memcmp(parse.fte_snonce, sm->snonce, WPA_NONCE_LEN) != 0) {
1081
0
    wpa_printf(MSG_DEBUG, "FT: SNonce mismatch in FTIE");
1082
0
    wpa_hexdump(MSG_DEBUG, "FT: Received SNonce",
1083
0
          parse.fte_snonce, WPA_NONCE_LEN);
1084
0
    wpa_hexdump(MSG_DEBUG, "FT: Expected SNonce",
1085
0
          sm->snonce, WPA_NONCE_LEN);
1086
0
    goto fail;
1087
0
  }
1088
1089
0
  if (os_memcmp(parse.fte_anonce, sm->anonce, WPA_NONCE_LEN) != 0) {
1090
0
    wpa_printf(MSG_DEBUG, "FT: ANonce mismatch in FTIE");
1091
0
    wpa_hexdump(MSG_DEBUG, "FT: Received ANonce",
1092
0
          parse.fte_anonce, WPA_NONCE_LEN);
1093
0
    wpa_hexdump(MSG_DEBUG, "FT: Expected ANonce",
1094
0
          sm->anonce, WPA_NONCE_LEN);
1095
0
    goto fail;
1096
0
  }
1097
1098
0
  if (parse.r0kh_id == NULL) {
1099
0
    wpa_printf(MSG_DEBUG, "FT: No R0KH-ID subelem in FTIE");
1100
0
    goto fail;
1101
0
  }
1102
1103
0
  if (parse.r0kh_id_len != sm->r0kh_id_len ||
1104
0
      os_memcmp_const(parse.r0kh_id, sm->r0kh_id, parse.r0kh_id_len) != 0)
1105
0
  {
1106
0
    wpa_printf(MSG_DEBUG, "FT: R0KH-ID in FTIE did not match with "
1107
0
         "the current R0KH-ID");
1108
0
    wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID in FTIE",
1109
0
          parse.r0kh_id, parse.r0kh_id_len);
1110
0
    wpa_hexdump(MSG_DEBUG, "FT: The current R0KH-ID",
1111
0
          sm->r0kh_id, sm->r0kh_id_len);
1112
0
    goto fail;
1113
0
  }
1114
1115
0
  if (parse.r1kh_id == NULL) {
1116
0
    wpa_printf(MSG_DEBUG, "FT: No R1KH-ID subelem in FTIE");
1117
0
    goto fail;
1118
0
  }
1119
1120
0
  if (os_memcmp_const(parse.r1kh_id, sm->r1kh_id, FT_R1KH_ID_LEN) != 0) {
1121
0
    wpa_printf(MSG_DEBUG, "FT: Unknown R1KH-ID used in "
1122
0
         "ReassocResp");
1123
0
    goto fail;
1124
0
  }
1125
1126
0
  if (parse.rsn_pmkid == NULL ||
1127
0
      os_memcmp_const(parse.rsn_pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN))
1128
0
  {
1129
0
    wpa_printf(MSG_DEBUG, "FT: No matching PMKR1Name (PMKID) in "
1130
0
         "RSNIE (pmkid=%d)", !!parse.rsn_pmkid);
1131
0
    goto fail;
1132
0
  }
1133
1134
0
  count = 3;
1135
0
  if (parse.ric)
1136
0
    count += ieee802_11_ie_count(parse.ric, parse.ric_len);
1137
0
  if (parse.rsnxe)
1138
0
    count++;
1139
0
  if (parse.fte_elem_count != count) {
1140
0
    wpa_printf(MSG_DEBUG, "FT: Unexpected IE count in MIC "
1141
0
         "Control: received %u expected %u",
1142
0
         parse.fte_elem_count, count);
1143
0
    goto fail;
1144
0
  }
1145
1146
0
  if (wpa_key_mgmt_fils(sm->key_mgmt)) {
1147
0
    kck = sm->ptk.kck2;
1148
0
    kck_len = sm->ptk.kck2_len;
1149
0
  } else {
1150
0
    kck = sm->ptk.kck;
1151
0
    kck_len = sm->ptk.kck_len;
1152
0
  }
1153
1154
0
  if (wpa_ft_mic(sm->key_mgmt, kck, kck_len, sm->own_addr, src_addr, 6,
1155
0
           parse.mdie - 2, parse.mdie_len + 2,
1156
0
           parse.ftie - 2, parse.ftie_len + 2,
1157
0
           parse.rsn - 2, parse.rsn_len + 2,
1158
0
           parse.ric, parse.ric_len,
1159
0
           parse.rsnxe ? parse.rsnxe - 2 : NULL,
1160
0
           parse.rsnxe ? parse.rsnxe_len + 2 : 0,
1161
0
           NULL,
1162
0
           mic) < 0) {
1163
0
    wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
1164
0
    goto fail;
1165
0
  }
1166
1167
0
  if (os_memcmp_const(mic, parse.fte_mic, mic_len) != 0) {
1168
0
    wpa_printf(MSG_DEBUG, "FT: Invalid MIC in FTIE");
1169
0
    wpa_hexdump(MSG_MSGDUMP, "FT: Received MIC",
1170
0
          parse.fte_mic, mic_len);
1171
0
    wpa_hexdump(MSG_MSGDUMP, "FT: Calculated MIC", mic, mic_len);
1172
0
    goto fail;
1173
0
  }
1174
1175
0
  if (parse.fte_rsnxe_used && !sm->ap_rsnxe) {
1176
0
    wpa_printf(MSG_INFO,
1177
0
         "FT: FTE indicated that AP uses RSNXE, but RSNXE was not included in Beacon/Probe Response frames");
1178
0
    goto fail;
1179
0
  }
1180
1181
0
  if (!sm->ap_rsn_ie) {
1182
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1183
0
      "FT: No RSNE for this AP known - trying to get from scan results");
1184
0
    if (wpa_sm_get_beacon_ie(sm) < 0) {
1185
0
      wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1186
0
        "FT: Could not find AP from the scan results");
1187
0
      goto fail;
1188
0
    }
1189
0
    wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
1190
0
      "FT: Found the current AP from updated scan results");
1191
0
  }
1192
1193
0
  if (sm->ap_rsn_ie &&
1194
0
      wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
1195
0
             sm->ap_rsn_ie, sm->ap_rsn_ie_len,
1196
0
             parse.rsn - 2, parse.rsn_len + 2)) {
1197
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1198
0
      "FT: RSNE mismatch between Beacon/ProbeResp and FT protocol Reassociation Response frame");
1199
0
    wpa_hexdump(MSG_INFO, "RSNE in Beacon/ProbeResp",
1200
0
          sm->ap_rsn_ie, sm->ap_rsn_ie_len);
1201
0
    wpa_hexdump(MSG_INFO,
1202
0
          "RSNE in FT protocol Reassociation Response frame",
1203
0
          parse.rsn ? parse.rsn - 2 : NULL,
1204
0
          parse.rsn ? parse.rsn_len + 2 : 0);
1205
0
    goto fail;
1206
0
  }
1207
1208
0
  own_rsnxe_used = wpa_key_mgmt_sae(sm->key_mgmt) &&
1209
0
    (sm->sae_pwe == SAE_PWE_HASH_TO_ELEMENT ||
1210
0
     sm->sae_pwe == SAE_PWE_BOTH);
1211
0
  if ((sm->ap_rsnxe && !parse.rsnxe && own_rsnxe_used) ||
1212
0
      (!sm->ap_rsnxe && parse.rsnxe) ||
1213
0
      (sm->ap_rsnxe && parse.rsnxe &&
1214
0
       (sm->ap_rsnxe_len != 2 + parse.rsnxe_len ||
1215
0
        os_memcmp(sm->ap_rsnxe, parse.rsnxe - 2,
1216
0
      sm->ap_rsnxe_len) != 0))) {
1217
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1218
0
      "FT: RSNXE mismatch between Beacon/ProbeResp and FT protocol Reassociation Response frame");
1219
0
    wpa_hexdump(MSG_INFO, "RSNXE in Beacon/ProbeResp",
1220
0
          sm->ap_rsnxe, sm->ap_rsnxe_len);
1221
0
    wpa_hexdump(MSG_INFO,
1222
0
          "RSNXE in FT protocol Reassociation Response frame",
1223
0
          parse.rsnxe ? parse.rsnxe - 2 : NULL,
1224
0
          parse.rsnxe ? parse.rsnxe_len + 2 : 0);
1225
0
    goto fail;
1226
0
  }
1227
1228
#ifdef CONFIG_OCV
1229
  if (wpa_sm_ocv_enabled(sm)) {
1230
    struct wpa_channel_info ci;
1231
1232
    if (wpa_sm_channel_info(sm, &ci) != 0) {
1233
      wpa_printf(MSG_WARNING,
1234
           "Failed to get channel info to validate received OCI in (Re)Assoc Response");
1235
      goto fail;
1236
    }
1237
1238
    if (ocv_verify_tx_params(parse.oci, parse.oci_len, &ci,
1239
           channel_width_to_int(ci.chanwidth),
1240
           ci.seg1_idx) != OCI_SUCCESS) {
1241
      wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE
1242
        "addr=" MACSTR " frame=ft-assoc error=%s",
1243
        MAC2STR(src_addr), ocv_errorstr);
1244
      goto fail;
1245
    }
1246
  }
1247
#endif /* CONFIG_OCV */
1248
1249
0
  sm->ft_reassoc_completed = 1;
1250
1251
0
  if (wpa_ft_process_gtk_subelem(sm, parse.gtk, parse.gtk_len) < 0 ||
1252
0
      wpa_ft_process_igtk_subelem(sm, parse.igtk, parse.igtk_len) < 0 ||
1253
0
      wpa_ft_process_bigtk_subelem(sm, parse.bigtk, parse.bigtk_len) < 0)
1254
0
    goto fail;
1255
1256
0
  if (sm->set_ptk_after_assoc) {
1257
0
    wpa_printf(MSG_DEBUG, "FT: Try to set PTK again now that we "
1258
0
         "are associated");
1259
0
    if (wpa_ft_install_ptk(sm, src_addr) < 0)
1260
0
      goto fail;
1261
0
    sm->set_ptk_after_assoc = 0;
1262
0
  }
1263
1264
0
  if (parse.ric) {
1265
0
    wpa_hexdump(MSG_MSGDUMP, "FT: RIC Response",
1266
0
          parse.ric, parse.ric_len);
1267
    /* TODO: parse response and inform driver about results when
1268
     * using wpa_supplicant SME */
1269
0
  }
1270
1271
0
  wpa_printf(MSG_DEBUG, "FT: Completed successfully");
1272
1273
0
  ret = 0;
1274
0
fail:
1275
0
  wpa_ft_parse_ies_free(&parse);
1276
0
  return ret;
1277
0
}
1278
1279
1280
/**
1281
 * wpa_ft_start_over_ds - Generate over-the-DS auth request
1282
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1283
 * @target_ap: Target AP Address
1284
 * @mdie: Mobility Domain IE from the target AP
1285
 * @force: Whether to force the request and ignore mobility domain differences
1286
 *  (only for testing purposes)
1287
 * Returns: 0 on success, -1 on failure
1288
 */
1289
int wpa_ft_start_over_ds(struct wpa_sm *sm, const u8 *target_ap,
1290
       const u8 *mdie, bool force)
1291
0
{
1292
0
  u8 *ft_ies;
1293
0
  size_t ft_ies_len;
1294
1295
0
  if (!force &&
1296
0
      (!mdie || mdie[1] < 3 || !wpa_sm_has_ft_keys(sm, mdie + 2))) {
1297
0
    wpa_printf(MSG_DEBUG, "FT: Cannot use over-the DS with " MACSTR
1298
0
         " - no keys matching the mobility domain",
1299
0
         MAC2STR(target_ap));
1300
0
    return -1;
1301
0
  }
1302
1303
0
  wpa_printf(MSG_DEBUG, "FT: Request over-the-DS with " MACSTR,
1304
0
       MAC2STR(target_ap));
1305
1306
  /* Generate a new SNonce */
1307
0
  if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
1308
0
    wpa_printf(MSG_INFO, "FT: Failed to generate a new SNonce");
1309
0
    return -1;
1310
0
  }
1311
1312
0
  ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, NULL, sm->pmk_r0_name,
1313
0
            NULL, 0, target_ap, NULL, 0, mdie, 0);
1314
0
  if (ft_ies) {
1315
0
    sm->over_the_ds_in_progress = 1;
1316
0
    os_memcpy(sm->target_ap, target_ap, ETH_ALEN);
1317
0
    wpa_sm_send_ft_action(sm, 1, target_ap, ft_ies, ft_ies_len);
1318
0
    os_free(ft_ies);
1319
0
  }
1320
1321
0
  return 0;
1322
0
}
1323
1324
1325
#ifdef CONFIG_PASN
1326
1327
static struct pasn_ft_r1kh * wpa_ft_pasn_get_r1kh(struct wpa_sm *sm,
1328
              const u8 *bssid)
1329
{
1330
  size_t i;
1331
1332
  for (i = 0; i < sm->n_pasn_r1kh; i++)
1333
    if (ether_addr_equal(sm->pasn_r1kh[i].bssid, bssid))
1334
      return &sm->pasn_r1kh[i];
1335
1336
  return NULL;
1337
}
1338
1339
1340
static void wpa_ft_pasn_store_r1kh(struct wpa_sm *sm, const u8 *bssid)
1341
{
1342
  struct pasn_ft_r1kh *tmp = wpa_ft_pasn_get_r1kh(sm, bssid);
1343
1344
  if (tmp)
1345
    return;
1346
1347
  tmp = os_realloc_array(sm->pasn_r1kh, sm->n_pasn_r1kh + 1,
1348
             sizeof(*tmp));
1349
  if (!tmp) {
1350
    wpa_printf(MSG_DEBUG, "PASN: FT: Failed to store R1KH");
1351
    return;
1352
  }
1353
1354
  sm->pasn_r1kh = tmp;
1355
  tmp = &sm->pasn_r1kh[sm->n_pasn_r1kh];
1356
1357
  wpa_printf(MSG_DEBUG, "PASN: FT: Store R1KH for " MACSTR,
1358
       MAC2STR(bssid));
1359
1360
  os_memcpy(tmp->bssid, bssid, ETH_ALEN);
1361
  os_memcpy(tmp->r1kh_id, sm->r1kh_id, FT_R1KH_ID_LEN);
1362
1363
  sm->n_pasn_r1kh++;
1364
}
1365
1366
1367
int wpa_pasn_ft_derive_pmk_r1(struct wpa_sm *sm, int akmp, const u8 *bssid,
1368
            u8 *pmk_r1, size_t *pmk_r1_len, u8 *pmk_r1_name)
1369
{
1370
  struct pasn_ft_r1kh *r1kh_entry;
1371
1372
  if (sm->key_mgmt != (unsigned int) akmp) {
1373
    wpa_printf(MSG_DEBUG,
1374
         "PASN: FT: Key management mismatch: %u != %u",
1375
         sm->key_mgmt, akmp);
1376
    return -1;
1377
  }
1378
1379
  r1kh_entry = wpa_ft_pasn_get_r1kh(sm, bssid);
1380
  if (!r1kh_entry) {
1381
    wpa_printf(MSG_DEBUG,
1382
         "PASN: FT: Cannot find R1KH-ID for " MACSTR,
1383
         MAC2STR(bssid));
1384
    return -1;
1385
  }
1386
1387
  /*
1388
   * Note: PMK R0 etc. were already derived and are maintained by the
1389
   * state machine, and as the same key hierarchy is used, there is no
1390
   * need to derive them again, so only derive PMK R1 etc.
1391
   */
1392
  if (wpa_derive_pmk_r1(sm->pmk_r0, sm->pmk_r0_len, sm->pmk_r0_name,
1393
            r1kh_entry->r1kh_id, sm->own_addr, pmk_r1,
1394
            pmk_r1_name) < 0)
1395
    return -1;
1396
1397
  *pmk_r1_len = sm->pmk_r0_len;
1398
1399
  wpa_hexdump_key(MSG_DEBUG, "PASN: FT: PMK-R1", pmk_r1, sm->pmk_r0_len);
1400
  wpa_hexdump(MSG_DEBUG, "PASN: FT: PMKR1Name", pmk_r1_name,
1401
        WPA_PMK_NAME_LEN);
1402
1403
  return 0;
1404
}
1405
1406
#endif /* CONFIG_PASN */
1407
1408
#endif /* CONFIG_IEEE80211R */