Coverage Report

Created: 2026-11-30 06:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hostap/src/rsn_supp/wpa.c
Line
Count
Source
1
/*
2
 * WPA Supplicant - WPA state machine and EAPOL-Key processing
3
 * Copyright (c) 2003-2018, Jouni Malinen <j@w1.fi>
4
 * Copyright(c) 2015 Intel Deutschland GmbH
5
 *
6
 * This software may be distributed under the terms of the BSD license.
7
 * See README for more details.
8
 */
9
10
#include "includes.h"
11
12
#include "common.h"
13
#include "crypto/aes.h"
14
#include "crypto/aes_wrap.h"
15
#include "crypto/crypto.h"
16
#include "crypto/random.h"
17
#include "crypto/aes_siv.h"
18
#include "crypto/sha256.h"
19
#include "crypto/sha384.h"
20
#include "crypto/sha512.h"
21
#include "common/ieee802_11_defs.h"
22
#include "common/ieee802_11_common.h"
23
#include "common/ocv.h"
24
#include "common/dpp.h"
25
#include "common/wpa_ctrl.h"
26
#include "eap_common/eap_defs.h"
27
#include "eapol_supp/eapol_supp_sm.h"
28
#include "drivers/driver.h"
29
#include "wpa.h"
30
#include "eloop.h"
31
#include "preauth.h"
32
#include "pmksa_cache.h"
33
#include "wpa_i.h"
34
#include "wpa_ie.h"
35
36
37
static const u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
38
39
static const u8 * wpa_sm_get_ap_rsnxe(struct wpa_sm *sm, size_t *len);
40
41
42
static void _wpa_hexdump_link(int level, u8 link_id, const char *title,
43
            const void *buf, size_t len, bool key)
44
0
{
45
0
  char *link_title = NULL;
46
47
0
  if (link_id >= MAX_NUM_MLD_LINKS)
48
0
    goto out;
49
50
0
  link_title = os_malloc(os_strlen(title) + 20);
51
0
  if (!link_title)
52
0
    goto out;
53
54
0
  os_snprintf(link_title, os_strlen(title) + 20, "MLO link[%u]: %s",
55
0
        link_id, title);
56
57
0
out:
58
0
  if (key)
59
0
    wpa_hexdump_key(level, link_title ? link_title : title, buf,
60
0
        len);
61
0
  else
62
0
    wpa_hexdump(level, link_title ? link_title : title, buf, len);
63
0
  os_free(link_title);
64
0
}
65
66
67
static void wpa_hexdump_link(int level, u8 link_id, const char *title,
68
           const void *buf, size_t len)
69
0
{
70
0
  _wpa_hexdump_link(level, link_id, title, buf, len, false);
71
0
}
72
73
74
static void wpa_hexdump_link_key(int level, u8 link_id, const char *title,
75
         const void *buf, size_t len)
76
0
{
77
0
  _wpa_hexdump_link(level, link_id, title, buf, len, true);
78
0
}
79
80
81
/**
82
 * wpa_eapol_key_send - Send WPA/RSN EAPOL-Key message
83
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
84
 * @ptk: PTK for Key Confirmation/Encryption Key
85
 * @ver: Version field from Key Info
86
 * @dest: Destination address for the frame
87
 * @proto: Ethertype (usually ETH_P_EAPOL)
88
 * @msg: EAPOL-Key message
89
 * @msg_len: Length of message
90
 * @key_mic: Pointer to the buffer to which the EAPOL-Key MIC is written
91
 * Returns: >= 0 on success, < 0 on failure
92
 */
93
int wpa_eapol_key_send(struct wpa_sm *sm, struct wpa_ptk *ptk,
94
           int ver, const u8 *dest, u16 proto,
95
           u8 *msg, size_t msg_len, u8 *key_mic)
96
0
{
97
0
  int ret = -1;
98
0
  size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len, sm->hash_alg,
99
0
             sm->pasn_group);
100
101
0
  wpa_printf(MSG_DEBUG, "WPA: Send EAPOL-Key frame to " MACSTR
102
0
       " ver=%d mic_len=%d key_mgmt=0x%x",
103
0
       MAC2STR(dest), ver, (int) mic_len, sm->key_mgmt);
104
0
  if (is_zero_ether_addr(dest) && is_zero_ether_addr(sm->bssid)) {
105
    /*
106
     * Association event was not yet received; try to fetch
107
     * BSSID from the driver.
108
     */
109
0
    if (wpa_sm_get_bssid(sm, sm->bssid) < 0) {
110
0
      wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
111
0
        "WPA: Failed to read BSSID for "
112
0
        "EAPOL-Key destination address");
113
0
    } else {
114
0
      dest = sm->bssid;
115
0
      wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
116
0
        "WPA: Use BSSID (" MACSTR
117
0
        ") as the destination for EAPOL-Key",
118
0
        MAC2STR(dest));
119
0
    }
120
0
  }
121
122
0
  if (mic_len) {
123
0
    if (key_mic && (!ptk || !ptk->kck_len))
124
0
      goto out;
125
126
0
    if (key_mic &&
127
0
        wpa_eapol_key_mic(ptk->kck, ptk->kck_len, sm->key_mgmt,
128
0
              sm->hash_alg, ver, msg, msg_len,
129
0
              key_mic, sm->pasn_group)) {
130
0
      wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
131
0
        "WPA: Failed to generate EAPOL-Key version %d key_mgmt 0x%x MIC",
132
0
        ver, sm->key_mgmt);
133
0
      goto out;
134
0
    }
135
0
    if (ptk)
136
0
      wpa_hexdump_key(MSG_DEBUG, "WPA: KCK",
137
0
          ptk->kck, ptk->kck_len);
138
0
    wpa_hexdump(MSG_DEBUG, "WPA: Derived Key MIC",
139
0
          key_mic, mic_len);
140
0
  } else {
141
#ifdef CONFIG_FILS
142
    /* AEAD cipher - Key MIC field not used */
143
    struct ieee802_1x_hdr *s_hdr, *hdr;
144
    struct wpa_eapol_key *s_key, *key;
145
    u8 *buf, *s_key_data, *key_data;
146
    size_t buf_len = msg_len + AES_BLOCK_SIZE;
147
    size_t key_data_len;
148
    u16 eapol_len;
149
    const u8 *aad[1];
150
    size_t aad_len[1];
151
152
    if (!ptk || !ptk->kek_len)
153
      goto out;
154
155
    key_data_len = msg_len - sizeof(struct ieee802_1x_hdr) -
156
      sizeof(struct wpa_eapol_key) - 2;
157
158
    buf = os_malloc(buf_len);
159
    if (!buf)
160
      goto out;
161
162
    os_memcpy(buf, msg, msg_len);
163
    hdr = (struct ieee802_1x_hdr *) buf;
164
    key = (struct wpa_eapol_key *) (hdr + 1);
165
    key_data = ((u8 *) (key + 1)) + 2;
166
167
    /* Update EAPOL header to include AES-SIV overhead */
168
    eapol_len = be_to_host16(hdr->length);
169
    eapol_len += AES_BLOCK_SIZE;
170
    hdr->length = host_to_be16(eapol_len);
171
172
    /* Update Key Data Length field to include AES-SIV overhead */
173
    WPA_PUT_BE16((u8 *) (key + 1), AES_BLOCK_SIZE + key_data_len);
174
175
    s_hdr = (struct ieee802_1x_hdr *) msg;
176
    s_key = (struct wpa_eapol_key *) (s_hdr + 1);
177
    s_key_data = ((u8 *) (s_key + 1)) + 2;
178
179
    wpa_hexdump_key(MSG_DEBUG, "WPA: Plaintext Key Data",
180
        s_key_data, key_data_len);
181
182
    wpa_hexdump_key(MSG_DEBUG, "WPA: KEK", ptk->kek, ptk->kek_len);
183
     /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
184
      * to Key Data (exclusive). */
185
    aad[0] = buf;
186
    aad_len[0] = key_data - buf;
187
    if (aes_siv_encrypt(ptk->kek, ptk->kek_len,
188
            s_key_data, key_data_len,
189
            1, aad, aad_len, key_data) < 0) {
190
      os_free(buf);
191
      goto out;
192
    }
193
194
    wpa_hexdump(MSG_DEBUG, "WPA: Encrypted Key Data from SIV",
195
          key_data, AES_BLOCK_SIZE + key_data_len);
196
197
    os_free(msg);
198
    msg = buf;
199
    msg_len = buf_len;
200
#else /* CONFIG_FILS */
201
0
    goto out;
202
0
#endif /* CONFIG_FILS */
203
0
  }
204
205
0
  wpa_hexdump(MSG_MSGDUMP, "WPA: TX EAPOL-Key", msg, msg_len);
206
0
  ret = wpa_sm_ether_send(sm, dest, proto, msg, msg_len);
207
0
  eapol_sm_notify_tx_eapol_key(sm->eapol);
208
0
out:
209
0
  os_free(msg);
210
0
  return ret;
211
0
}
212
213
214
/**
215
 * wpa_sm_key_request - Send EAPOL-Key Request
216
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
217
 * @error: Indicate whether this is an Michael MIC error report
218
 * @pairwise: 1 = error report for pairwise packet, 0 = for group packet
219
 *
220
 * Send an EAPOL-Key Request to the current authenticator. This function is
221
 * used to request rekeying and it is usually called when a local Michael MIC
222
 * failure is detected.
223
 */
224
void wpa_sm_key_request(struct wpa_sm *sm, int error, int pairwise)
225
0
{
226
0
  size_t mic_len, hdrlen, rlen;
227
0
  struct wpa_eapol_key *reply;
228
0
  int key_info, ver;
229
0
  u8 *rbuf, *key_mic, *mic;
230
231
0
  if (pairwise && sm->wpa_deny_ptk0_rekey && !sm->use_ext_key_id &&
232
0
      wpa_sm_get_state(sm) == WPA_COMPLETED && !error) {
233
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
234
0
      "WPA: PTK0 rekey not allowed, reconnecting");
235
0
    wpa_sm_reconnect(sm);
236
0
    return;
237
0
  }
238
239
0
  if (!sm->ptk_set) {
240
0
    wpa_printf(MSG_INFO,
241
0
         "WPA: No PTK derived yet - cannot send EAPOL-Key Request");
242
0
    return;
243
0
  }
244
245
0
  if (wpa_use_akm_defined(sm->key_mgmt))
246
0
    ver = WPA_KEY_INFO_TYPE_AKM_DEFINED;
247
0
  else if (wpa_key_mgmt_ft(sm->key_mgmt) ||
248
0
     wpa_key_mgmt_sha256(sm->key_mgmt))
249
0
    ver = WPA_KEY_INFO_TYPE_AES_128_CMAC;
250
0
  else if (sm->pairwise_cipher != WPA_CIPHER_TKIP)
251
0
    ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
252
0
  else
253
0
    ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
254
255
0
  mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len, sm->hash_alg,
256
0
            sm->pasn_group);
257
0
  hdrlen = sizeof(*reply) + mic_len + 2;
258
0
  rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
259
0
          hdrlen, &rlen, (void *) &reply);
260
0
  if (rbuf == NULL)
261
0
    return;
262
263
0
  reply->type = (sm->proto == WPA_PROTO_RSN) ?
264
0
    EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
265
0
  key_info = WPA_KEY_INFO_REQUEST | ver;
266
0
  key_info |= WPA_KEY_INFO_SECURE;
267
0
  if (mic_len)
268
0
    key_info |= WPA_KEY_INFO_MIC;
269
0
  else
270
0
    key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
271
0
  if (error)
272
0
    key_info |= WPA_KEY_INFO_ERROR;
273
0
  if (pairwise)
274
0
    key_info |= WPA_KEY_INFO_KEY_TYPE;
275
0
  WPA_PUT_BE16(reply->key_info, key_info);
276
0
  WPA_PUT_BE16(reply->key_length, 0);
277
0
  os_memcpy(reply->replay_counter, sm->request_counter,
278
0
      WPA_REPLAY_COUNTER_LEN);
279
0
  inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
280
281
0
  mic = (u8 *) (reply + 1);
282
0
  WPA_PUT_BE16(mic + mic_len, 0);
283
0
  if (!(key_info & WPA_KEY_INFO_MIC))
284
0
    key_mic = NULL;
285
0
  else
286
0
    key_mic = mic;
287
288
0
  wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
289
0
    "WPA: Sending EAPOL-Key Request (error=%d "
290
0
    "pairwise=%d ptk_set=%d len=%lu)",
291
0
    error, pairwise, sm->ptk_set, (unsigned long) rlen);
292
0
  wpa_eapol_key_send(sm, &sm->ptk, ver, wpa_sm_get_auth_addr(sm),
293
0
         ETH_P_EAPOL, rbuf, rlen, key_mic);
294
0
}
295
296
297
static void wpa_supplicant_key_mgmt_set_pmk(struct wpa_sm *sm)
298
0
{
299
0
#ifdef CONFIG_IEEE80211R
300
0
  if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X) {
301
0
    if (wpa_sm_key_mgmt_set_pmk(sm, sm->xxkey, sm->xxkey_len))
302
0
      wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
303
0
        "RSN: Cannot set low order 256 bits of MSK for key management offload");
304
0
  } else {
305
0
#endif /* CONFIG_IEEE80211R */
306
0
    if (wpa_sm_key_mgmt_set_pmk(sm, sm->pmk, sm->pmk_len))
307
0
      wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
308
0
        "RSN: Cannot set PMK for key management offload");
309
0
#ifdef CONFIG_IEEE80211R
310
0
  }
311
0
#endif /* CONFIG_IEEE80211R */
312
0
}
313
314
315
static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
316
          const unsigned char *src_addr,
317
          const u8 *pmkid)
318
0
{
319
0
  int abort_cached = 0;
320
321
0
  if (pmkid && !sm->cur_pmksa) {
322
    /* When using drivers that generate RSN IE, wpa_supplicant may
323
     * not have enough time to get the association information
324
     * event before receiving this 1/4 message, so try to find a
325
     * matching PMKSA cache entry here. */
326
0
    sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr,
327
0
            sm->own_addr, pmkid,
328
0
            sm->network_ctx, sm->key_mgmt);
329
0
    if (sm->cur_pmksa) {
330
0
      wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
331
0
        "RSN: found matching PMKID from PMKSA cache");
332
0
    } else {
333
0
      wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
334
0
        "RSN: no matching PMKID found");
335
0
      abort_cached = 1;
336
0
    }
337
0
  }
338
339
0
  if (pmkid && sm->cur_pmksa &&
340
0
      os_memcmp_const(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
341
0
    wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN);
342
0
    wpa_sm_set_pmk_from_pmksa(sm);
343
0
    wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache",
344
0
        sm->pmk, sm->pmk_len);
345
0
    eapol_sm_notify_cached(sm->eapol);
346
0
#ifdef CONFIG_IEEE80211R
347
0
    sm->xxkey_len = 0;
348
#ifdef CONFIG_SAE
349
    if ((sm->key_mgmt == WPA_KEY_MGMT_FT_SAE ||
350
         sm->key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY) &&
351
        sm->pmk_len == PMK_LEN) {
352
      /* Need to allow FT key derivation to proceed with
353
       * PMK from SAE being used as the XXKey in cases where
354
       * the PMKID in msg 1/4 matches the PMKSA entry that was
355
       * just added based on SAE authentication for the
356
       * initial mobility domain association. */
357
      os_memcpy(sm->xxkey, sm->pmk, sm->pmk_len);
358
      sm->xxkey_len = sm->pmk_len;
359
    }
360
#endif /* CONFIG_SAE */
361
0
#endif /* CONFIG_IEEE80211R */
362
0
  } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) {
363
0
    int res, pmk_len;
364
0
#ifdef CONFIG_IEEE80211R
365
0
    u8 buf[2 * PMK_LEN];
366
0
#endif /* CONFIG_IEEE80211R */
367
368
0
    if (wpa_key_mgmt_sha384(sm->key_mgmt))
369
0
      pmk_len = PMK_LEN_SUITE_B_192;
370
0
    else
371
0
      pmk_len = PMK_LEN;
372
#ifdef CONFIG_PQC
373
    if (wpa_key_mgmt_pqc(sm->key_mgmt) && sm->security_profile) {
374
      const struct ieee80211_pqc_profile *c;
375
376
      c = pqc_profile_get(sm->security_profile->number);
377
      if (c)
378
        pmk_len = wpa_hash_len(c->hash);
379
    }
380
#endif /* CONFIG_PQC */
381
0
    res = eapol_sm_get_key(sm->eapol, sm->pmk, pmk_len);
382
0
    if (res) {
383
0
      if (pmk_len == PMK_LEN) {
384
        /*
385
         * EAP-LEAP is an exception from other EAP
386
         * methods: it uses only 16-byte PMK.
387
         */
388
0
        res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
389
0
        pmk_len = 16;
390
0
      }
391
0
    }
392
0
#ifdef CONFIG_IEEE80211R
393
0
    if (res == 0 &&
394
0
        eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0) {
395
0
      if (wpa_key_mgmt_sha384(sm->key_mgmt)) {
396
0
        os_memcpy(sm->xxkey, buf, SHA384_MAC_LEN);
397
0
        sm->xxkey_len = SHA384_MAC_LEN;
398
0
      } else {
399
0
        os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN);
400
0
        sm->xxkey_len = PMK_LEN;
401
0
      }
402
0
      forced_memzero(buf, sizeof(buf));
403
0
      if (sm->proto == WPA_PROTO_RSN &&
404
0
          wpa_key_mgmt_ft(sm->key_mgmt)) {
405
0
        struct rsn_pmksa_cache_entry *sa = NULL;
406
0
        const u8 *fils_cache_id = NULL;
407
408
#ifdef CONFIG_FILS
409
        if (sm->fils_cache_id_set)
410
          fils_cache_id = sm->fils_cache_id;
411
#endif /* CONFIG_FILS */
412
0
        wpa_hexdump_key(MSG_DEBUG,
413
0
            "FT: Cache XXKey/MPMK",
414
0
            sm->xxkey, sm->xxkey_len);
415
0
        sa = pmksa_cache_add(sm->pmksa,
416
0
                 sm->xxkey, sm->xxkey_len,
417
0
                 NULL, NULL, 0,
418
0
                 src_addr, sm->own_addr,
419
0
                 sm->network_ctx,
420
0
                 sm->key_mgmt,
421
0
                 fils_cache_id, 0,
422
0
                 RSN_HASH_NOT_SPECIFIED);
423
0
        if (!sm->cur_pmksa)
424
0
          sm->cur_pmksa = sa;
425
0
      }
426
0
    }
427
0
#endif /* CONFIG_IEEE80211R */
428
0
    if (res == 0) {
429
0
      struct rsn_pmksa_cache_entry *sa = NULL;
430
0
      const u8 *fils_cache_id = NULL;
431
0
      enum rsn_hash_alg hash = sm->hash_alg;
432
433
#ifdef CONFIG_FILS
434
      if (sm->fils_cache_id_set)
435
        fils_cache_id = sm->fils_cache_id;
436
#endif /* CONFIG_FILS */
437
438
0
      wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
439
0
          "machines", sm->pmk, pmk_len);
440
0
      sm->pmk_len = pmk_len;
441
0
      wpa_supplicant_key_mgmt_set_pmk(sm);
442
0
      if (sm->proto == WPA_PROTO_RSN &&
443
0
          !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
444
0
          !wpa_key_mgmt_ft(sm->key_mgmt)) {
445
0
        sa = pmksa_cache_add(sm->pmksa,
446
0
                 sm->pmk, pmk_len, NULL,
447
0
                 NULL, 0,
448
0
                 src_addr, sm->own_addr,
449
0
                 sm->network_ctx,
450
0
                 sm->key_mgmt,
451
0
                 fils_cache_id, 0, hash);
452
0
      }
453
0
      if (!sm->cur_pmksa && pmkid &&
454
0
          sm->disable_pmksa_caching &&
455
0
          sm->proto == WPA_PROTO_RSN &&
456
0
          !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
457
0
          !wpa_key_mgmt_ft(sm->key_mgmt)) {
458
0
        u8 new_pmkid[PMKID_LEN];
459
460
0
        rsn_pmkid(sm->pmk, sm->pmk_len, src_addr,
461
0
            sm->own_addr, new_pmkid,
462
0
            sm->key_mgmt, hash);
463
0
        if (os_memcmp_const(pmkid, new_pmkid,
464
0
                PMKID_LEN) != 0) {
465
0
          wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
466
0
            "RSN: PMKID mismatch with PMKSA caching disabled");
467
0
          return -1;
468
0
        }
469
470
0
        wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
471
0
          "RSN: PMKID matches fresh PMK with PMKSA caching disabled");
472
0
        abort_cached = 0;
473
0
      } else if (!sm->cur_pmksa && pmkid &&
474
0
          pmksa_cache_get(sm->pmksa, src_addr, sm->own_addr,
475
0
              pmkid, NULL, 0)) {
476
0
        wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
477
0
          "RSN: the new PMK matches with the "
478
0
          "PMKID");
479
0
        abort_cached = 0;
480
0
      } else if (sa && !sm->cur_pmksa && pmkid) {
481
        /*
482
         * It looks like the authentication server
483
         * derived mismatching MSK. This should not
484
         * really happen, but bugs happen.. There is not
485
         * much we can do here without knowing what
486
         * exactly caused the server to misbehave.
487
         */
488
0
        wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
489
0
          "RSN: PMKID mismatch - authentication server may have derived different MSK?!");
490
0
        return -1;
491
0
      }
492
493
0
      if (!sm->cur_pmksa)
494
0
        sm->cur_pmksa = sa;
495
0
#ifdef CONFIG_IEEE80211R
496
0
    } else if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->ft_protocol) {
497
0
      wpa_printf(MSG_DEBUG,
498
0
           "FT: Continue 4-way handshake without PMK/PMKID for association using FT protocol");
499
0
#endif /* CONFIG_IEEE80211R */
500
0
    } else {
501
0
      wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
502
0
        "WPA: Failed to get master session key from "
503
0
        "EAPOL state machines - key handshake "
504
0
        "aborted");
505
0
      if (sm->cur_pmksa) {
506
0
        wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
507
0
          "RSN: Cancelled PMKSA caching "
508
0
          "attempt");
509
0
        sm->cur_pmksa = NULL;
510
0
        abort_cached = 1;
511
0
      } else if (!abort_cached) {
512
0
        return -1;
513
0
      }
514
0
    }
515
0
  }
516
517
0
  if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) &&
518
0
      !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
519
0
      !wpa_key_mgmt_ft(sm->key_mgmt)) {
520
    /* Send EAPOL-Start to trigger full EAP authentication. */
521
0
    u8 *buf;
522
0
    size_t buflen;
523
524
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
525
0
      "RSN: no PMKSA entry found - trigger "
526
0
      "full EAP authentication");
527
0
    buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
528
0
           NULL, 0, &buflen, NULL);
529
0
    if (buf) {
530
      /* Set and reset eapFail to allow EAP state machine to
531
       * proceed with new authentication. */
532
0
      eapol_sm_notify_eap_fail(sm->eapol, true);
533
0
      eapol_sm_notify_eap_fail(sm->eapol, false);
534
0
      wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
535
0
            buf, buflen);
536
0
      os_free(buf);
537
0
      return -2;
538
0
    }
539
540
0
    return -1;
541
0
  }
542
543
0
  return 0;
544
0
}
545
546
547
/**
548
 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
549
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
550
 * @dst: Destination address for the frame
551
 * @key: Pointer to the EAPOL-Key frame header
552
 * @ver: Version bits from EAPOL-Key Key Info
553
 * @nonce: Nonce value for the EAPOL-Key frame
554
 * @wpa_ie: WPA/RSN IE
555
 * @wpa_ie_len: Length of the WPA/RSN IE
556
 * @ptk: PTK to use for keyed hash and encryption
557
 * Returns: >= 0 on success, < 0 on failure
558
 */
559
int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
560
             const struct wpa_eapol_key *key,
561
             int ver, const u8 *nonce,
562
             const u8 *wpa_ie, size_t wpa_ie_len,
563
             struct wpa_ptk *ptk)
564
0
{
565
0
  size_t mic_len, hdrlen, rlen, extra_len = 0;
566
0
  struct wpa_eapol_key *reply;
567
0
  u8 *rbuf, *key_mic;
568
0
  u8 *rsn_ie_buf = NULL, *buf2 = NULL;
569
0
  u16 key_info;
570
#ifdef CONFIG_TESTING_OPTIONS
571
  size_t pad_len = 0;
572
#endif /* CONFIG_TESTING_OPTIONS */
573
574
0
  if (wpa_ie == NULL) {
575
0
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No wpa_ie set - "
576
0
      "cannot generate msg 2/4");
577
0
    return -1;
578
0
  }
579
580
0
#ifdef CONFIG_IEEE80211R
581
0
  if (wpa_key_mgmt_ft(sm->key_mgmt)) {
582
0
    int res;
583
584
0
    wpa_hexdump(MSG_DEBUG, "WPA: WPA IE before FT processing",
585
0
          wpa_ie, wpa_ie_len);
586
    /*
587
     * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and
588
     * FTIE from (Re)Association Response.
589
     */
590
0
    rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN +
591
0
               sm->assoc_resp_ies_len);
592
0
    if (rsn_ie_buf == NULL)
593
0
      return -1;
594
0
    os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len);
595
0
    res = wpa_insert_pmkid(rsn_ie_buf, &wpa_ie_len,
596
0
               sm->pmk_r1_name, !sm->ft_prepend_pmkid);
597
0
    if (res < 0) {
598
0
      os_free(rsn_ie_buf);
599
0
      return -1;
600
0
    }
601
0
    wpa_hexdump(MSG_DEBUG,
602
0
          "WPA: WPA IE after PMKID[PMKR1Name] addition into RSNE",
603
0
          rsn_ie_buf, wpa_ie_len);
604
605
0
    if (sm->assoc_resp_ies) {
606
0
      wpa_hexdump(MSG_DEBUG, "WPA: Add assoc_resp_ies",
607
0
            sm->assoc_resp_ies,
608
0
            sm->assoc_resp_ies_len);
609
0
      os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies,
610
0
          sm->assoc_resp_ies_len);
611
0
      wpa_ie_len += sm->assoc_resp_ies_len;
612
0
    }
613
614
0
    wpa_ie = rsn_ie_buf;
615
0
  }
616
0
#endif /* CONFIG_IEEE80211R */
617
618
0
  if (sm->rsn_override != RSN_OVERRIDE_NOT_USED) {
619
0
    u8 *pos;
620
621
0
    buf2 = os_malloc(wpa_ie_len + 2 + 4 + 1);
622
0
    if (!buf2) {
623
0
      os_free(rsn_ie_buf);
624
0
      return -1;
625
0
    }
626
0
    os_memcpy(buf2, wpa_ie, wpa_ie_len);
627
0
    pos = buf2 + wpa_ie_len;
628
0
    *pos++ = WLAN_EID_VENDOR_SPECIFIC;
629
0
    *pos++ = 4 + 1;
630
0
    WPA_PUT_BE32(pos, RSN_SELECTION_IE_VENDOR_TYPE);
631
0
    pos += 4;
632
0
    if (sm->rsn_override == RSN_OVERRIDE_RSNE) {
633
0
      *pos++ = RSN_SELECTION_RSNE;
634
0
    } else if (sm->rsn_override == RSN_OVERRIDE_RSNE_OVERRIDE) {
635
0
      *pos++ = RSN_SELECTION_RSNE_OVERRIDE;
636
0
    } else if (sm->rsn_override == RSN_OVERRIDE_RSNE_OVERRIDE_2) {
637
0
      *pos++ = RSN_SELECTION_RSNE_OVERRIDE_2;
638
0
    } else {
639
0
      os_free(rsn_ie_buf);
640
0
      os_free(buf2);
641
0
      return -1;
642
0
    }
643
644
0
    wpa_ie = buf2;
645
0
    wpa_ie_len += 2 + 4 + 1;
646
647
0
  }
648
649
0
  wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
650
651
#ifdef CONFIG_TESTING_OPTIONS
652
  if (sm->test_eapol_m2_elems)
653
    extra_len = wpabuf_len(sm->test_eapol_m2_elems);
654
  if (sm->encrypt_eapol_m2) {
655
    pad_len = (wpa_ie_len + extra_len) % 8;
656
    if (pad_len)
657
      pad_len = 8 - pad_len;
658
    extra_len += pad_len + 8;
659
  }
660
#endif /* CONFIG_TESTING_OPTIONS */
661
662
0
  mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len, sm->hash_alg,
663
0
            sm->pasn_group);
664
0
  hdrlen = sizeof(*reply) + mic_len + 2;
665
0
  rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
666
0
          NULL, hdrlen + wpa_ie_len + extra_len,
667
0
          &rlen, (void *) &reply);
668
0
  if (rbuf == NULL) {
669
0
    os_free(rsn_ie_buf);
670
0
    os_free(buf2);
671
0
    return -1;
672
0
  }
673
674
0
  reply->type = (sm->proto == WPA_PROTO_RSN) ?
675
0
    EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
676
0
  key_info = ver | WPA_KEY_INFO_KEY_TYPE;
677
0
  if (sm->ptk_set && sm->proto != WPA_PROTO_WPA)
678
0
    key_info |= WPA_KEY_INFO_SECURE;
679
0
  if (mic_len)
680
0
    key_info |= WPA_KEY_INFO_MIC;
681
0
  else
682
0
    key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
683
#ifdef CONFIG_TESTING_OPTIONS
684
  if (sm->encrypt_eapol_m2)
685
    key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
686
  if (sm->eapol_2_key_info_set_mask)
687
    key_info |= sm->eapol_2_key_info_set_mask;
688
#endif /* CONFIG_TESTING_OPTIONS */
689
0
  WPA_PUT_BE16(reply->key_info, key_info);
690
0
  if (sm->proto == WPA_PROTO_RSN)
691
0
    WPA_PUT_BE16(reply->key_length, 0);
692
0
  else
693
0
    os_memcpy(reply->key_length, key->key_length, 2);
694
0
  os_memcpy(reply->replay_counter, key->replay_counter,
695
0
      WPA_REPLAY_COUNTER_LEN);
696
0
  wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", reply->replay_counter,
697
0
        WPA_REPLAY_COUNTER_LEN);
698
699
0
  key_mic = (u8 *) (reply + 1);
700
  /* Key Data Length */
701
0
  WPA_PUT_BE16(key_mic + mic_len, wpa_ie_len + extra_len);
702
0
  os_memcpy(key_mic + mic_len + 2, wpa_ie, wpa_ie_len); /* Key Data */
703
0
  os_free(rsn_ie_buf);
704
0
  os_free(buf2);
705
#ifdef CONFIG_TESTING_OPTIONS
706
  if (sm->test_eapol_m2_elems) {
707
    os_memcpy(key_mic + mic_len + 2 + wpa_ie_len,
708
        wpabuf_head(sm->test_eapol_m2_elems),
709
        wpabuf_len(sm->test_eapol_m2_elems));
710
  }
711
712
  if (sm->encrypt_eapol_m2) {
713
    u8 *plain;
714
    size_t plain_len;
715
716
    if (sm->test_eapol_m2_elems)
717
      extra_len = wpabuf_len(sm->test_eapol_m2_elems);
718
    else
719
      extra_len = 0;
720
    plain_len = wpa_ie_len + extra_len + pad_len;
721
    plain = os_memdup(key_mic + mic_len + 2, plain_len);
722
    if (!plain) {
723
      os_free(rbuf);
724
      return -1;
725
    }
726
    if (pad_len)
727
      plain[plain_len - pad_len] = 0xdd;
728
729
    wpa_hexdump_key(MSG_DEBUG, "RSN: AES-WRAP using KEK",
730
        ptk->kek, ptk->kek_len);
731
    if (aes_wrap(ptk->kek, ptk->kek_len, plain_len / 8, plain,
732
           key_mic + mic_len + 2)) {
733
      os_free(plain);
734
      os_free(rbuf);
735
      return -1;
736
    }
737
    wpa_hexdump(MSG_DEBUG,
738
          "RSN: Encrypted Key Data from AES-WRAP",
739
          key_mic + mic_len + 2, plain_len + 8);
740
    os_free(plain);
741
  }
742
#endif /* CONFIG_TESTING_OPTIONS */
743
744
0
  os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
745
746
0
  wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/4");
747
0
  return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
748
0
          key_mic);
749
0
}
750
751
752
static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
753
        const struct wpa_eapol_key *key, struct wpa_ptk *ptk)
754
0
{
755
0
  int ret;
756
0
  const u8 *z = NULL, *ap_rsnxe;
757
0
  size_t z_len = 0, kdk_len, ap_rsnxe_len;
758
0
  int akmp;
759
760
0
  ap_rsnxe = wpa_sm_get_ap_rsnxe(sm, &ap_rsnxe_len);
761
762
0
#ifdef CONFIG_IEEE80211R
763
0
  if (wpa_key_mgmt_ft(sm->key_mgmt))
764
0
    return wpa_derive_ptk_ft(sm, src_addr, key, ptk);
765
0
#endif /* CONFIG_IEEE80211R */
766
767
#ifdef CONFIG_DPP2
768
  if (sm->key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_z) {
769
    z = wpabuf_head(sm->dpp_z);
770
    z_len = wpabuf_len(sm->dpp_z);
771
  }
772
#endif /* CONFIG_DPP2 */
773
774
0
  akmp = sm->key_mgmt;
775
#ifdef CONFIG_OWE
776
  if (sm->owe_ptk_workaround && akmp == WPA_KEY_MGMT_OWE &&
777
      sm->pmk_len > 32) {
778
    wpa_printf(MSG_DEBUG,
779
         "OWE: Force SHA256 for PTK derivation");
780
    akmp |= WPA_KEY_MGMT_PSK_SHA256;
781
  }
782
#endif /* CONFIG_OWE */
783
784
0
  if (sm->force_kdk_derivation ||
785
0
      (sm->secure_ltf &&
786
0
       ieee802_11_rsnx_capab(ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF)))
787
0
    kdk_len = WPA_KDK_MAX_LEN;
788
0
  else
789
0
    kdk_len = 0;
790
791
0
  ret = wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
792
0
           sm->own_addr, wpa_sm_get_auth_addr(sm), sm->snonce,
793
0
           key->key_nonce, ptk, akmp,
794
0
           sm->pairwise_cipher, z, z_len,
795
0
           kdk_len, sm->pasn_group);
796
0
  if (ret) {
797
0
    wpa_printf(MSG_ERROR, "WPA: PTK derivation failed");
798
0
    return ret;
799
0
  }
800
801
#ifdef CONFIG_PASN
802
  if (sm->secure_ltf &&
803
      ieee802_11_rsnx_capab(ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF))
804
    ret = wpa_ltf_keyseed(ptk, akmp, sm->pairwise_cipher);
805
#endif /* CONFIG_PASN */
806
807
0
  return ret;
808
0
}
809
810
811
static int wpa_handle_ext_key_id(struct wpa_sm *sm,
812
         struct wpa_eapol_ie_parse *kde)
813
0
{
814
0
  if (sm->ext_key_id) {
815
0
    u16 key_id;
816
817
0
    if (!kde->key_id) {
818
0
      wpa_msg(sm->ctx->msg_ctx,
819
0
        sm->use_ext_key_id ? MSG_INFO : MSG_DEBUG,
820
0
        "RSN: No Key ID in Extended Key ID handshake");
821
0
      sm->keyidx_active = 0;
822
0
      return sm->use_ext_key_id ? -1 : 0;
823
0
    }
824
825
0
    key_id = kde->key_id[0] & 0x03;
826
0
    if (key_id > 1) {
827
0
      wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
828
0
        "RSN: Invalid Extended Key ID: %d", key_id);
829
0
      return -1;
830
0
    }
831
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
832
0
      "RSN: Using Extended Key ID %d", key_id);
833
0
    sm->keyidx_active = key_id;
834
0
    sm->use_ext_key_id = 1;
835
0
  } else {
836
0
    if (kde->key_id && (kde->key_id[0] & 0x03)) {
837
0
      wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
838
0
        "RSN: Non-zero Extended Key ID Key ID in PTK0 handshake");
839
0
      return -1;
840
0
    }
841
842
0
    if (kde->key_id) {
843
      /* This is not supposed to be included here, but ignore
844
       * the case of matching Key ID 0 just in case. */
845
0
      wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
846
0
        "RSN: Extended Key ID Key ID 0 in PTK0 handshake");
847
0
    }
848
0
    sm->keyidx_active = 0;
849
0
    sm->use_ext_key_id = 0;
850
0
  }
851
852
0
  return 0;
853
0
}
854
855
856
static u8 * rsn_add_kde(u8 *pos, u32 kde, const u8 *data, size_t data_len)
857
0
{
858
0
  *pos++ = WLAN_EID_VENDOR_SPECIFIC;
859
0
  *pos++ = RSN_SELECTOR_LEN + data_len;
860
0
  RSN_SELECTOR_PUT(pos, kde);
861
0
  pos += RSN_SELECTOR_LEN;
862
0
  os_memcpy(pos, data, data_len);
863
0
  pos += data_len;
864
865
0
  return pos;
866
0
}
867
868
869
static size_t wpa_mlo_link_kde_len(struct wpa_sm *sm)
870
0
{
871
0
  int i;
872
0
  unsigned int num_links = 0;
873
874
0
  for_each_link(sm->mlo.req_links, i) {
875
0
    if (sm->mlo.assoc_link_id != i)
876
0
      num_links++;
877
0
  }
878
879
0
  return num_links * (RSN_SELECTOR_LEN + 1 + ETH_ALEN + 2);
880
0
}
881
882
883
static u8 * wpa_mlo_link_kde(struct wpa_sm *sm, u8 *pos)
884
0
{
885
0
  int i;
886
0
  u8 hdr[1 + ETH_ALEN];
887
888
0
  for_each_link(sm->mlo.req_links, i) {
889
0
    if (sm->mlo.assoc_link_id == i)
890
0
      continue;
891
892
0
    wpa_printf(MSG_DEBUG,
893
0
         "MLO: Add MLO Link %d KDE in EAPOL-Key 2/4", i);
894
0
    hdr[0] = i & 0xF; /* LinkID; no RSNE or RSNXE */
895
0
    os_memcpy(&hdr[1], sm->mlo.links[i].addr, ETH_ALEN);
896
0
    pos = rsn_add_kde(pos, RSN_KEY_DATA_MLO_LINK, hdr, sizeof(hdr));
897
0
  }
898
899
0
  return pos;
900
0
}
901
902
903
static bool is_valid_ap_mld_mac_kde(struct wpa_sm *sm, const u8 *mac_kde)
904
0
{
905
0
  return mac_kde &&
906
0
    ether_addr_equal(mac_kde, sm->mlo.ap_mld_addr);
907
0
}
908
909
910
static void wpas_swap_tkip_mic_keys(struct wpa_ptk *ptk)
911
0
{
912
0
  u8 buf[8];
913
914
  /* Supplicant: swap tx/rx Mic keys */
915
0
  os_memcpy(buf, &ptk->tk[16], 8);
916
0
  os_memcpy(&ptk->tk[16], &ptk->tk[24], 8);
917
0
  os_memcpy(&ptk->tk[24], buf, 8);
918
0
  forced_memzero(buf, sizeof(buf));
919
0
}
920
921
922
static void wpa_supplicant_process_1_of_4_wpa(struct wpa_sm *sm,
923
                const unsigned char *src_addr,
924
                const struct wpa_eapol_key *key,
925
                u16 ver, const u8 *key_data,
926
                size_t key_data_len,
927
                enum frame_encryption encrypted)
928
0
{
929
0
  struct wpa_eapol_ie_parse ie;
930
0
  struct wpa_ptk *ptk;
931
0
  int res;
932
933
0
  if (wpa_sm_get_network_ctx(sm) == NULL) {
934
0
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
935
0
      "WPA: No SSID info found (msg 1 of 4)");
936
0
    return;
937
0
  }
938
939
0
  wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
940
0
    "WPA: RX message 1 of 4-Way Handshake from " MACSTR
941
0
    " (ver=%d)", MAC2STR(src_addr), ver);
942
943
0
  os_memset(&ie, 0, sizeof(ie));
944
945
0
  res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
946
0
  if (res == -2) {
947
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
948
0
      "WPA: Do not reply to msg 1/4 - requesting full EAP authentication");
949
0
    return;
950
0
  }
951
0
  if (res)
952
0
    goto failed;
953
954
0
  wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
955
956
0
  if (sm->renew_snonce) {
957
0
    if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
958
0
      wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
959
0
        "WPA: Failed to get random data for SNonce");
960
0
      goto failed;
961
0
    }
962
0
    sm->renew_snonce = 0;
963
0
    wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
964
0
          sm->snonce, WPA_NONCE_LEN);
965
0
  }
966
967
  /* Calculate PTK which will be stored as a temporary PTK until it has
968
   * been verified when processing message 3/4. */
969
0
  ptk = &sm->tptk;
970
0
  if (wpa_derive_ptk(sm, src_addr, key, ptk) < 0)
971
0
    goto failed;
972
0
  if (sm->pairwise_cipher == WPA_CIPHER_TKIP)
973
0
    wpas_swap_tkip_mic_keys(ptk);
974
0
  sm->tptk_set = 1;
975
0
  sm->hash_alg = ptk->hash_alg;
976
977
0
  if (wpa_supplicant_send_2_of_4(sm, wpa_sm_get_auth_addr(sm), key, ver,
978
0
               sm->snonce, sm->assoc_wpa_ie,
979
0
               sm->assoc_wpa_ie_len, ptk) < 0)
980
0
    goto failed;
981
982
0
  os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
983
0
  return;
984
985
0
failed:
986
0
  wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
987
0
}
988
989
990
static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
991
            const unsigned char *src_addr,
992
            const struct wpa_eapol_key *key,
993
            u16 ver, const u8 *key_data,
994
            size_t key_data_len,
995
            enum frame_encryption encrypted)
996
0
{
997
0
  struct wpa_eapol_ie_parse ie;
998
0
  struct wpa_ptk *ptk;
999
0
  int res;
1000
0
  u8 *kde, *kde_buf = NULL;
1001
0
  size_t kde_len;
1002
0
  size_t mlo_kde_len = 0;
1003
1004
0
  if (encrypted == FRAME_NOT_ENCRYPTED && sm->tk_set &&
1005
0
      wpa_sm_pmf_enabled(sm)) {
1006
0
    wpa_printf(MSG_DEBUG,
1007
0
         "RSN: Discard unencrypted EAPOL-Key msg 1/4 when TK is set and PMF is enabled");
1008
0
    return;
1009
0
  }
1010
1011
0
  if (wpa_sm_get_network_ctx(sm) == NULL) {
1012
0
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info "
1013
0
      "found (msg 1 of 4)");
1014
0
    return;
1015
0
  }
1016
1017
0
  if (sm->wpa_deny_ptk0_rekey && !sm->use_ext_key_id &&
1018
0
      wpa_sm_get_state(sm) == WPA_COMPLETED) {
1019
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1020
0
      "WPA: PTK0 rekey not allowed, reconnecting");
1021
0
    wpa_sm_reconnect(sm);
1022
0
    return;
1023
0
  }
1024
1025
0
  wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of 4-Way "
1026
0
    "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
1027
1028
0
  os_memset(&ie, 0, sizeof(ie));
1029
1030
  /* RSN: msg 1/4 should contain PMKID for the selected PMK */
1031
0
  wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data", key_data, key_data_len);
1032
0
  if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0) {
1033
0
    wpa_printf(MSG_DEBUG,
1034
0
         "RSN: Discard EAPOL-Key msg 1/4 with invalid IEs/KDEs");
1035
0
    return;
1036
0
  }
1037
0
  if (ie.pmkid) {
1038
0
    wpa_hexdump(MSG_DEBUG, "RSN: PMKID from Authenticator",
1039
0
          ie.pmkid, PMKID_LEN);
1040
0
  }
1041
1042
0
  if (sm->mlo.valid_links && !is_valid_ap_mld_mac_kde(sm, ie.mac_addr)) {
1043
0
    wpa_printf(MSG_INFO,
1044
0
         "RSN: Discard EAPOL-Key msg 1/4 with invalid AP MLD MAC address KDE");
1045
0
    return;
1046
0
  }
1047
1048
0
  res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
1049
0
  if (res == -2) {
1050
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to "
1051
0
      "msg 1/4 - requesting full EAP authentication");
1052
0
    return;
1053
0
  }
1054
0
  if (res)
1055
0
    goto failed;
1056
1057
0
  wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
1058
1059
0
  if (sm->renew_snonce) {
1060
0
    if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
1061
0
      wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1062
0
        "WPA: Failed to get random data for SNonce");
1063
0
      goto failed;
1064
0
    }
1065
0
    if (wpa_sm_rsn_overriding_supported(sm))
1066
0
      rsn_set_snonce_cookie(sm->snonce);
1067
0
    sm->renew_snonce = 0;
1068
0
    wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
1069
0
          sm->snonce, WPA_NONCE_LEN);
1070
0
  }
1071
1072
  /* Calculate PTK which will be stored as a temporary PTK until it has
1073
   * been verified when processing message 3/4. */
1074
0
  ptk = &sm->tptk;
1075
0
  if (wpa_derive_ptk(sm, src_addr, key, ptk) < 0)
1076
0
    goto failed;
1077
0
  if (sm->pairwise_cipher == WPA_CIPHER_TKIP)
1078
0
    wpas_swap_tkip_mic_keys(ptk);
1079
0
  sm->tptk_set = 1;
1080
0
  sm->hash_alg = ptk->hash_alg;
1081
1082
  /* Add MLO Link KDE and MAC KDE in M2 for ML connection */
1083
0
  if (sm->mlo.valid_links)
1084
0
    mlo_kde_len = wpa_mlo_link_kde_len(sm) +
1085
0
      RSN_SELECTOR_LEN + ETH_ALEN + 2;
1086
1087
0
  kde = sm->assoc_wpa_ie;
1088
0
  kde_len = sm->assoc_wpa_ie_len;
1089
0
  kde_buf = os_malloc(kde_len +
1090
0
          2 + RSN_SELECTOR_LEN + 3 +
1091
0
          sm->assoc_rsnxe_len +
1092
0
          2 + RSN_SELECTOR_LEN + 1 +
1093
0
          2 + RSN_SELECTOR_LEN + 2 + mlo_kde_len);
1094
1095
0
  if (!kde_buf)
1096
0
    goto failed;
1097
0
  os_memcpy(kde_buf, kde, kde_len);
1098
0
  kde = kde_buf;
1099
1100
#ifdef CONFIG_OCV
1101
  if (wpa_sm_ocv_enabled(sm)) {
1102
    struct wpa_channel_info ci;
1103
    u8 *pos;
1104
1105
    pos = kde + kde_len;
1106
    if (wpa_sm_channel_info(sm, &ci) != 0) {
1107
      wpa_printf(MSG_WARNING,
1108
           "Failed to get channel info for OCI element in EAPOL-Key 2/4");
1109
      goto failed;
1110
    }
1111
#ifdef CONFIG_TESTING_OPTIONS
1112
    if (sm->oci_freq_override_eapol) {
1113
      wpa_printf(MSG_INFO,
1114
           "TEST: Override OCI KDE frequency %d -> %d MHz",
1115
           ci.frequency, sm->oci_freq_override_eapol);
1116
      ci.frequency = sm->oci_freq_override_eapol;
1117
    }
1118
#endif /* CONFIG_TESTING_OPTIONS */
1119
1120
    if (ocv_insert_oci_kde(&ci, &pos) < 0)
1121
      goto failed;
1122
    kde_len = pos - kde;
1123
  }
1124
#endif /* CONFIG_OCV */
1125
1126
0
  if (sm->assoc_rsnxe && sm->assoc_rsnxe_len) {
1127
0
    os_memcpy(kde + kde_len, sm->assoc_rsnxe, sm->assoc_rsnxe_len);
1128
0
    kde_len += sm->assoc_rsnxe_len;
1129
0
  }
1130
1131
#ifdef CONFIG_P2P
1132
  if (sm->p2p) {
1133
    u8 *pos;
1134
1135
    wpa_printf(MSG_DEBUG,
1136
         "P2P: Add IP Address Request KDE into EAPOL-Key 2/4");
1137
    pos = kde + kde_len;
1138
    *pos++ = WLAN_EID_VENDOR_SPECIFIC;
1139
    *pos++ = RSN_SELECTOR_LEN + 1;
1140
    RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_IP_ADDR_REQ);
1141
    pos += RSN_SELECTOR_LEN;
1142
    *pos++ = 0x01;
1143
    kde_len = pos - kde;
1144
  }
1145
#endif /* CONFIG_P2P */
1146
1147
#ifdef CONFIG_DPP2
1148
  if (DPP_VERSION > 1 && sm->key_mgmt == WPA_KEY_MGMT_DPP) {
1149
    u8 *pos;
1150
1151
    wpa_printf(MSG_DEBUG, "DPP: Add DPP KDE into EAPOL-Key 2/4");
1152
    pos = kde + kde_len;
1153
    *pos++ = WLAN_EID_VENDOR_SPECIFIC;
1154
    *pos++ = RSN_SELECTOR_LEN + 2;
1155
    RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_DPP);
1156
    pos += RSN_SELECTOR_LEN;
1157
    *pos++ = DPP_VERSION; /* Protocol Version */
1158
    *pos = 0; /* Flags */
1159
    if (sm->dpp_pfs == 0)
1160
      *pos |= DPP_KDE_PFS_ALLOWED;
1161
    else if (sm->dpp_pfs == 1)
1162
      *pos |= DPP_KDE_PFS_ALLOWED | DPP_KDE_PFS_REQUIRED;
1163
    pos++;
1164
    kde_len = pos - kde;
1165
  }
1166
#endif /* CONFIG_DPP2 */
1167
1168
0
  if (sm->mlo.valid_links) {
1169
0
    u8 *pos;
1170
1171
    /* Add MAC KDE */
1172
0
    wpa_printf(MSG_DEBUG, "MLO: Add MAC KDE into EAPOL-Key 2/4");
1173
0
    pos = kde + kde_len;
1174
0
    pos = rsn_add_kde(pos, RSN_KEY_DATA_MAC_ADDR, sm->own_addr,
1175
0
          ETH_ALEN);
1176
1177
    /* Add MLO Link KDE */
1178
0
    wpa_printf(MSG_DEBUG, "Add MLO Link KDE(s) into EAPOL-Key 2/4");
1179
0
    pos = wpa_mlo_link_kde(sm, pos);
1180
0
    kde_len = pos - kde;
1181
0
  }
1182
1183
0
  if (wpa_supplicant_send_2_of_4(sm, wpa_sm_get_auth_addr(sm), key, ver,
1184
0
               sm->snonce, kde, kde_len, ptk) < 0)
1185
0
    goto failed;
1186
1187
0
  os_free(kde_buf);
1188
0
  os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
1189
0
  return;
1190
1191
0
failed:
1192
0
  os_free(kde_buf);
1193
0
  wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1194
0
}
1195
1196
1197
static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
1198
0
{
1199
0
  struct wpa_sm *sm = eloop_ctx;
1200
0
  rsn_preauth_candidate_process(sm);
1201
0
}
1202
1203
1204
static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
1205
              const u8 *addr, int secure)
1206
0
{
1207
0
  wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1208
0
    "WPA: Key negotiation completed with "
1209
0
    MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
1210
0
    wpa_cipher_txt(sm->pairwise_cipher),
1211
0
    wpa_cipher_txt(sm->group_cipher));
1212
0
  wpa_sm_cancel_auth_timeout(sm);
1213
0
  wpa_sm_set_state(sm, WPA_COMPLETED);
1214
1215
0
  if (secure) {
1216
0
    wpa_sm_mlme_setprotection(
1217
0
      sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
1218
0
      MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
1219
0
    eapol_sm_notify_portValid(sm->eapol, true);
1220
0
    if (wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
1221
0
        sm->key_mgmt == WPA_KEY_MGMT_DPP ||
1222
0
        sm->key_mgmt == WPA_KEY_MGMT_OWE)
1223
0
      eapol_sm_notify_eap_success(sm->eapol, true);
1224
    /*
1225
     * Start preauthentication after a short wait to avoid a
1226
     * possible race condition between the data receive and key
1227
     * configuration after the 4-Way Handshake. This increases the
1228
     * likelihood of the first preauth EAPOL-Start frame getting to
1229
     * the target AP.
1230
     */
1231
0
    if (!dl_list_empty(&sm->pmksa_candidates))
1232
0
      eloop_register_timeout(1, 0, wpa_sm_start_preauth,
1233
0
                 sm, NULL);
1234
0
  }
1235
1236
0
  if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
1237
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1238
0
      "RSN: Authenticator accepted "
1239
0
      "opportunistic PMKSA entry - marking it valid");
1240
0
    sm->cur_pmksa->opportunistic = 0;
1241
0
  }
1242
1243
0
#ifdef CONFIG_IEEE80211R
1244
0
  if (wpa_key_mgmt_ft(sm->key_mgmt)) {
1245
    /* Prepare for the next transition */
1246
0
    wpa_ft_prepare_auth_request(sm, NULL);
1247
0
  }
1248
0
#endif /* CONFIG_IEEE80211R */
1249
0
}
1250
1251
1252
static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
1253
0
{
1254
0
  struct wpa_sm *sm = eloop_ctx;
1255
0
  wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
1256
0
  wpa_sm_key_request(sm, 0, 1);
1257
0
}
1258
1259
1260
static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
1261
              const struct wpa_eapol_key *key,
1262
              enum key_flag key_flag)
1263
0
{
1264
0
  int keylen, rsclen;
1265
0
  enum wpa_alg alg;
1266
0
  const u8 *key_rsc;
1267
#ifdef CONFIG_PASN
1268
  const u8 *ap_rsnxe;
1269
  size_t ap_rsnxe_len;
1270
#endif /* CONFIG_PASN */
1271
1272
0
  if (sm->ptk.installed ||
1273
0
      (sm->ptk.installed_rx && (key_flag & KEY_FLAG_NEXT))) {
1274
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1275
0
      "WPA: Do not re-install same PTK to the driver");
1276
0
    return 0;
1277
0
  }
1278
1279
0
  wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1280
0
    "WPA: Installing %sTK to the driver",
1281
0
    (key_flag & KEY_FLAG_NEXT) ? "next " : "");
1282
1283
0
  if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
1284
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
1285
0
      "Suite: NONE - do not use pairwise keys");
1286
0
    return 0;
1287
0
  }
1288
1289
0
  if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
1290
0
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1291
0
      "WPA: Unsupported pairwise cipher %d",
1292
0
      sm->pairwise_cipher);
1293
0
    return -1;
1294
0
  }
1295
1296
0
  alg = wpa_cipher_to_alg(sm->pairwise_cipher);
1297
0
  keylen = wpa_cipher_key_len(sm->pairwise_cipher);
1298
0
  if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) {
1299
0
    wpa_printf(MSG_DEBUG, "WPA: TK length mismatch: %d != %lu",
1300
0
         keylen, (long unsigned int) sm->ptk.tk_len);
1301
0
    return -1;
1302
0
  }
1303
0
  rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
1304
1305
0
  if (sm->proto == WPA_PROTO_RSN) {
1306
0
    key_rsc = null_rsc;
1307
0
  } else {
1308
0
    key_rsc = key->key_rsc;
1309
0
    wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
1310
0
  }
1311
1312
0
  if (wpa_sm_set_key(sm, -1, alg, wpa_sm_get_auth_addr(sm),
1313
0
         sm->keyidx_active, 1, key_rsc, rsclen, sm->ptk.tk,
1314
0
         keylen, KEY_FLAG_PAIRWISE | key_flag) < 0) {
1315
0
    if (key_flag & KEY_FLAG_NEXT)
1316
0
      return 0; /* Not all drivers support this, so do not
1317
           * report failures on the RX-only set_key */
1318
0
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1319
0
      "WPA: Failed to set PTK to the driver (alg=%d keylen=%d auth_addr="
1320
0
      MACSTR " idx=%d key_flag=0x%x)",
1321
0
      alg, keylen, MAC2STR(wpa_sm_get_auth_addr(sm)),
1322
0
      sm->keyidx_active, key_flag);
1323
0
    return -1;
1324
0
  }
1325
1326
#ifdef CONFIG_PASN
1327
  ap_rsnxe = wpa_sm_get_ap_rsnxe(sm, &ap_rsnxe_len);
1328
  if (sm->secure_ltf &&
1329
      ieee802_11_rsnx_capab(ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF) &&
1330
      wpa_sm_set_ltf_keyseed(sm, sm->own_addr, sm->bssid,
1331
           sm->ptk.ltf_keyseed_len,
1332
           sm->ptk.ltf_keyseed) < 0) {
1333
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1334
      "WPA: Failed to set LTF keyseed to the driver (keylen=%zu bssid="
1335
      MACSTR ")", sm->ptk.ltf_keyseed_len,
1336
      MAC2STR(sm->bssid));
1337
    return -1;
1338
  }
1339
#endif /* CONFIG_PASN */
1340
1341
0
  wpa_sm_store_ptk(sm, wpa_sm_get_auth_addr(sm), sm->pairwise_cipher,
1342
0
       sm->dot11RSNAConfigPMKLifetime, &sm->ptk);
1343
1344
0
  if (key_flag & KEY_FLAG_NEXT) {
1345
0
    sm->ptk.installed_rx = true;
1346
0
  } else {
1347
0
#ifndef CONFIG_TESTING_OPTIONS
1348
    /* TK is not needed anymore in supplicant */
1349
0
    os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
1350
0
    sm->ptk.tk_len = 0;
1351
0
#endif /* CONFIG_TESTING_OPTIONS */
1352
0
    sm->ptk.installed = 1;
1353
0
    sm->tk_set = true;
1354
0
  }
1355
1356
0
  if (sm->wpa_ptk_rekey) {
1357
0
    eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
1358
0
    eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
1359
0
               sm, NULL);
1360
0
  }
1361
0
  return 0;
1362
0
}
1363
1364
1365
static int wpa_supplicant_activate_ptk(struct wpa_sm *sm)
1366
0
{
1367
0
  wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1368
0
    "WPA: Activate PTK (idx=%d auth_addr=" MACSTR ")",
1369
0
    sm->keyidx_active, MAC2STR(wpa_sm_get_auth_addr(sm)));
1370
1371
0
  if (wpa_sm_set_key(sm, -1, 0, wpa_sm_get_auth_addr(sm),
1372
0
         sm->keyidx_active, 0, NULL, 0, NULL, 0,
1373
0
         KEY_FLAG_PAIRWISE_RX_TX_MODIFY) < 0) {
1374
0
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1375
0
      "WPA: Failed to activate PTK for TX (idx=%d auth_addr="
1376
0
      MACSTR ")", sm->keyidx_active,
1377
0
      MAC2STR(wpa_sm_get_auth_addr(sm)));
1378
0
    return -1;
1379
0
  }
1380
0
  return 0;
1381
0
}
1382
1383
1384
static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
1385
               int group_cipher,
1386
               int keylen, int maxkeylen,
1387
               int *key_rsc_len,
1388
               enum wpa_alg *alg)
1389
0
{
1390
0
  int klen;
1391
1392
0
  *alg = wpa_cipher_to_alg(group_cipher);
1393
0
  if (*alg == WPA_ALG_NONE) {
1394
0
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1395
0
      "WPA: Unsupported Group Cipher %d",
1396
0
      group_cipher);
1397
0
    return -1;
1398
0
  }
1399
0
  *key_rsc_len = wpa_cipher_rsc_len(group_cipher);
1400
1401
0
  klen = wpa_cipher_key_len(group_cipher);
1402
0
  if (keylen != klen || maxkeylen < klen) {
1403
0
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1404
0
      "WPA: Unsupported %s Group Cipher key length %d (%d)",
1405
0
      wpa_cipher_txt(group_cipher), keylen, maxkeylen);
1406
0
    return -1;
1407
0
  }
1408
0
  return 0;
1409
0
}
1410
1411
1412
struct wpa_gtk_data {
1413
  enum wpa_alg alg;
1414
  int tx, key_rsc_len, keyidx;
1415
  u8 gtk[WPA_GTK_MAX_LEN];
1416
  int gtk_len;
1417
};
1418
1419
1420
static int wpa_supplicant_validate_gtk_kde_len(size_t gtk_len)
1421
0
{
1422
0
  if (gtk_len < 2 || gtk_len - 2 > WPA_GTK_MAX_LEN)
1423
0
    return -1;
1424
1425
0
  return 0;
1426
0
}
1427
1428
1429
static int wpa_supplicant_validate_mlo_gtk_kde_len(size_t gtk_len)
1430
0
{
1431
0
  if (gtk_len < RSN_MLO_GTK_KDE_PREFIX_LENGTH ||
1432
0
      gtk_len - RSN_MLO_GTK_KDE_PREFIX_LENGTH > WPA_GTK_MAX_LEN)
1433
0
    return -1;
1434
1435
0
  return 0;
1436
0
}
1437
1438
1439
static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
1440
              const struct wpa_gtk_data *gd,
1441
              const u8 *key_rsc, int wnm_sleep)
1442
0
{
1443
0
  const u8 *_gtk = gd->gtk;
1444
0
  u8 gtk_buf[32];
1445
1446
  /* Detect possible key reinstallation */
1447
0
  if ((sm->gtk.gtk_len == (size_t) gd->gtk_len &&
1448
0
       os_memcmp(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len) == 0) ||
1449
0
      (sm->gtk_wnm_sleep.gtk_len == (size_t) gd->gtk_len &&
1450
0
       os_memcmp(sm->gtk_wnm_sleep.gtk, gd->gtk,
1451
0
           sm->gtk_wnm_sleep.gtk_len) == 0)) {
1452
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1453
0
      "WPA: Not reinstalling already in-use GTK to the driver (keyidx=%d tx=%d len=%d)",
1454
0
      gd->keyidx, gd->tx, gd->gtk_len);
1455
0
    return 0;
1456
0
  }
1457
1458
0
  wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
1459
0
  wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1460
0
    "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
1461
0
    gd->keyidx, gd->tx, gd->gtk_len);
1462
0
  wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
1463
0
  if (sm->group_cipher == WPA_CIPHER_TKIP) {
1464
    /* Swap Tx/Rx keys for Michael MIC */
1465
0
    os_memcpy(gtk_buf, gd->gtk, 16);
1466
0
    os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
1467
0
    os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
1468
0
    _gtk = gtk_buf;
1469
0
  }
1470
0
  if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
1471
0
    if (wpa_sm_set_key(sm, -1, gd->alg, NULL,
1472
0
           gd->keyidx, 1, key_rsc, gd->key_rsc_len,
1473
0
           _gtk, gd->gtk_len,
1474
0
           KEY_FLAG_GROUP_RX_TX_DEFAULT) < 0) {
1475
0
      wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1476
0
        "WPA: Failed to set GTK to the driver "
1477
0
        "(Group only)");
1478
0
      forced_memzero(gtk_buf, sizeof(gtk_buf));
1479
0
      return -1;
1480
0
    }
1481
0
  } else if (wpa_sm_set_key(sm, -1, gd->alg, broadcast_ether_addr,
1482
0
          gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
1483
0
          _gtk, gd->gtk_len, KEY_FLAG_GROUP_RX) < 0) {
1484
0
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1485
0
      "WPA: Failed to set GTK to "
1486
0
      "the driver (alg=%d keylen=%d keyidx=%d)",
1487
0
      gd->alg, gd->gtk_len, gd->keyidx);
1488
0
    forced_memzero(gtk_buf, sizeof(gtk_buf));
1489
0
    return -1;
1490
0
  }
1491
0
  forced_memzero(gtk_buf, sizeof(gtk_buf));
1492
1493
0
  if (wnm_sleep) {
1494
0
    sm->gtk_wnm_sleep.gtk_len = gd->gtk_len;
1495
0
    os_memcpy(sm->gtk_wnm_sleep.gtk, gd->gtk,
1496
0
        sm->gtk_wnm_sleep.gtk_len);
1497
0
  } else {
1498
0
    sm->gtk.gtk_len = gd->gtk_len;
1499
0
    os_memcpy(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len);
1500
0
  }
1501
1502
0
  return 0;
1503
0
}
1504
1505
1506
static int wpa_supplicant_install_mlo_gtk(struct wpa_sm *sm, u8 link_id,
1507
            const struct wpa_gtk_data *gd,
1508
            const u8 *key_rsc, int wnm_sleep)
1509
0
{
1510
0
  const u8 *gtk = gd->gtk;
1511
0
  u8 gtk_buf[32];
1512
1513
  /* Detect possible key reinstallation */
1514
0
  if ((sm->mlo.links[link_id].gtk.gtk_len == (size_t) gd->gtk_len &&
1515
0
       os_memcmp(sm->mlo.links[link_id].gtk.gtk, gd->gtk,
1516
0
           sm->mlo.links[link_id].gtk.gtk_len) == 0) ||
1517
0
      (sm->mlo.links[link_id].gtk_wnm_sleep.gtk_len ==
1518
0
       (size_t) gd->gtk_len &&
1519
0
       os_memcmp(sm->mlo.links[link_id].gtk_wnm_sleep.gtk, gd->gtk,
1520
0
           sm->mlo.links[link_id].gtk_wnm_sleep.gtk_len) == 0)) {
1521
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1522
0
      "RSN: Not reinstalling already in-use GTK to the driver (link_id=%d keyidx=%d tx=%d len=%d)",
1523
0
      link_id, gd->keyidx, gd->tx, gd->gtk_len);
1524
0
    return 0;
1525
0
  }
1526
1527
0
  wpa_hexdump_link_key(MSG_DEBUG, link_id, "RSN: Group Key", gd->gtk,
1528
0
           gd->gtk_len);
1529
0
  wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1530
0
    "RSN: Installing GTK to the driver (link_id=%d keyidx=%d tx=%d len=%d)",
1531
0
    link_id, gd->keyidx, gd->tx, gd->gtk_len);
1532
0
  wpa_hexdump_link(MSG_DEBUG, link_id, "RSN: RSC",
1533
0
       key_rsc, gd->key_rsc_len);
1534
0
  if (sm->group_cipher == WPA_CIPHER_TKIP) {
1535
    /* Swap Tx/Rx keys for Michael MIC */
1536
0
    os_memcpy(gtk_buf, gd->gtk, 16);
1537
0
    os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
1538
0
    os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
1539
0
    gtk = gtk_buf;
1540
0
  }
1541
0
  if (wpa_sm_set_key(sm, link_id, gd->alg, broadcast_ether_addr,
1542
0
         gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len, gtk,
1543
0
         gd->gtk_len, KEY_FLAG_GROUP_RX) < 0) {
1544
0
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1545
0
      "RSN: Failed to set GTK to the driver (link_id=%d alg=%d keylen=%d keyidx=%d)",
1546
0
      link_id, gd->alg, gd->gtk_len, gd->keyidx);
1547
0
    forced_memzero(gtk_buf, sizeof(gtk_buf));
1548
0
    return -1;
1549
0
  }
1550
0
  forced_memzero(gtk_buf, sizeof(gtk_buf));
1551
1552
0
  if (wnm_sleep) {
1553
0
    sm->mlo.links[link_id].gtk_wnm_sleep.gtk_len = gd->gtk_len;
1554
0
    os_memcpy(sm->mlo.links[link_id].gtk_wnm_sleep.gtk, gd->gtk,
1555
0
        sm->mlo.links[link_id].gtk_wnm_sleep.gtk_len);
1556
0
  } else {
1557
0
    sm->mlo.links[link_id].gtk.gtk_len = gd->gtk_len;
1558
0
    os_memcpy(sm->mlo.links[link_id].gtk.gtk, gd->gtk,
1559
0
        sm->mlo.links[link_id].gtk.gtk_len);
1560
0
  }
1561
1562
0
  return 0;
1563
0
}
1564
1565
1566
static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
1567
            int tx)
1568
0
{
1569
0
  if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
1570
    /* Ignore Tx bit for GTK if a pairwise key is used. One AP
1571
     * seemed to set this bit (incorrectly, since Tx is only when
1572
     * doing Group Key only APs) and without this workaround, the
1573
     * data connection does not work because wpa_supplicant
1574
     * configured non-zero keyidx to be used for unicast. */
1575
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1576
0
      "WPA: Tx bit set for GTK, but pairwise "
1577
0
      "keys are used - ignore Tx bit");
1578
0
    return 0;
1579
0
  }
1580
0
  return tx;
1581
0
}
1582
1583
1584
static int wpa_supplicant_rsc_relaxation(const struct wpa_sm *sm,
1585
           const u8 *rsc)
1586
0
{
1587
0
  int rsclen;
1588
1589
0
  if (!sm->wpa_rsc_relaxation)
1590
0
    return 0;
1591
1592
0
  rsclen = wpa_cipher_rsc_len(sm->group_cipher);
1593
1594
  /*
1595
   * Try to detect RSC (endian) corruption issue where the AP sends
1596
   * the RSC bytes in EAPOL-Key message in the wrong order, both if
1597
   * it's actually a 6-byte field (as it should be) and if it treats
1598
   * it as an 8-byte field.
1599
   * An AP model known to have this bug is the Sapido RB-1632.
1600
   */
1601
0
  if (rsclen == 6 && ((rsc[5] && !rsc[0]) || rsc[6] || rsc[7])) {
1602
0
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1603
0
      "RSC %02x%02x%02x%02x%02x%02x%02x%02x is likely bogus, using 0",
1604
0
      rsc[0], rsc[1], rsc[2], rsc[3],
1605
0
      rsc[4], rsc[5], rsc[6], rsc[7]);
1606
1607
0
    return 1;
1608
0
  }
1609
1610
0
  return 0;
1611
0
}
1612
1613
1614
static int wpa_supplicant_mlo_gtk(struct wpa_sm *sm, u8 link_id, const u8 *gtk,
1615
          size_t gtk_len, int key_info)
1616
0
{
1617
0
  struct wpa_gtk_data gd;
1618
0
  const u8 *key_rsc;
1619
0
  int ret;
1620
1621
  /*
1622
   * MLO GTK KDE format:
1623
   * KeyID[bits 0-1], Tx [bit 2], Reserved [bit 3], link id [4-7]
1624
   * PN
1625
   * GTK
1626
   */
1627
0
  os_memset(&gd, 0, sizeof(gd));
1628
0
  wpa_hexdump_link_key(MSG_DEBUG, link_id,
1629
0
           "RSN: received GTK in pairwise handshake",
1630
0
           gtk, gtk_len);
1631
1632
0
  if (wpa_supplicant_validate_mlo_gtk_kde_len(gtk_len) < 0)
1633
0
    return -1;
1634
1635
0
  gd.keyidx = gtk[0] & 0x3;
1636
0
  gtk += 1;
1637
0
  gtk_len -= 1;
1638
1639
0
  key_rsc = gtk;
1640
1641
0
  gtk += 6;
1642
0
  gtk_len -= 6;
1643
1644
0
  os_memcpy(gd.gtk, gtk, gtk_len);
1645
0
  gd.gtk_len = gtk_len;
1646
1647
0
  ret = 0;
1648
0
  if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher, gtk_len,
1649
0
                gtk_len, &gd.key_rsc_len,
1650
0
                &gd.alg) ||
1651
0
       wpa_supplicant_install_mlo_gtk(sm, link_id, &gd, key_rsc, 0)) {
1652
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1653
0
      "RSN: Failed to install GTK for MLO Link ID %u",
1654
0
      link_id);
1655
0
    ret = -1;
1656
0
    goto out;
1657
0
  }
1658
1659
0
out:
1660
0
  forced_memzero(&gd, sizeof(gd));
1661
0
  return ret;
1662
0
}
1663
1664
1665
static int wpa_supplicant_pairwise_mlo_gtk(struct wpa_sm *sm,
1666
             const struct wpa_eapol_key *key,
1667
             struct wpa_eapol_ie_parse *ie,
1668
             int key_info)
1669
0
{
1670
0
  u8 i;
1671
1672
0
  for_each_link(sm->mlo.valid_links, i) {
1673
0
    if (!ie->mlo_gtk[i]) {
1674
0
      wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1675
0
        "MLO RSN: GTK not found for link ID %u", i);
1676
0
      return -1;
1677
0
    }
1678
1679
0
    if (wpa_supplicant_mlo_gtk(sm, i, ie->mlo_gtk[i],
1680
0
             ie->mlo_gtk_len[i], key_info))
1681
0
      return -1;
1682
0
  }
1683
1684
0
  return 0;
1685
0
}
1686
1687
1688
static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
1689
               const struct wpa_eapol_key *key,
1690
               const u8 *gtk, size_t gtk_len,
1691
               int key_info)
1692
0
{
1693
0
  struct wpa_gtk_data gd;
1694
0
  const u8 *key_rsc;
1695
1696
  /*
1697
   * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
1698
   * GTK KDE format:
1699
   * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
1700
   * Reserved [bits 0-7]
1701
   * GTK
1702
   */
1703
1704
0
  os_memset(&gd, 0, sizeof(gd));
1705
0
  wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
1706
0
      gtk, gtk_len);
1707
1708
0
  if (wpa_supplicant_validate_gtk_kde_len(gtk_len) < 0)
1709
0
    return -1;
1710
1711
0
  gd.keyidx = gtk[0] & 0x3;
1712
0
  gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1713
0
                 !!(gtk[0] & BIT(2)));
1714
0
  gtk += 2;
1715
0
  gtk_len -= 2;
1716
1717
0
  os_memcpy(gd.gtk, gtk, gtk_len);
1718
0
  gd.gtk_len = gtk_len;
1719
1720
0
  key_rsc = key->key_rsc;
1721
0
  if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
1722
0
    key_rsc = null_rsc;
1723
1724
0
  if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED &&
1725
0
      (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1726
0
                 gtk_len, gtk_len,
1727
0
                 &gd.key_rsc_len, &gd.alg) ||
1728
0
       wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0))) {
1729
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1730
0
      "RSN: Failed to install GTK");
1731
0
    forced_memzero(&gd, sizeof(gd));
1732
0
    return -1;
1733
0
  }
1734
0
  forced_memzero(&gd, sizeof(gd));
1735
1736
0
  return 0;
1737
0
}
1738
1739
1740
static int wpa_supplicant_install_igtk(struct wpa_sm *sm,
1741
               const struct wpa_igtk_kde *igtk,
1742
               int wnm_sleep)
1743
0
{
1744
0
  size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1745
0
  u16 keyidx = WPA_GET_LE16(igtk->keyid);
1746
1747
  /* Detect possible key reinstallation */
1748
0
  if ((sm->igtk.igtk_len == len &&
1749
0
       os_memcmp(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len) == 0) ||
1750
0
      (sm->igtk_wnm_sleep.igtk_len == len &&
1751
0
       os_memcmp(sm->igtk_wnm_sleep.igtk, igtk->igtk,
1752
0
           sm->igtk_wnm_sleep.igtk_len) == 0)) {
1753
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1754
0
      "WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)",
1755
0
      keyidx);
1756
0
    return  0;
1757
0
  }
1758
1759
0
  wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1760
0
    "WPA: IGTK keyid %d pn " COMPACT_MACSTR,
1761
0
    keyidx, MAC2STR(igtk->pn));
1762
0
  wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK", igtk->igtk, len);
1763
0
  if (keyidx > 4095) {
1764
0
    u16 masked = keyidx & 0x00FF;
1765
1766
0
    if (masked == 4 || masked == 5) {
1767
      /* Some APs send an IGTK KeyID larger than 4095 with
1768
       * a valid key index (4 or 5) in the lower octet and
1769
       * spurious bits in the higher octet. Mask out the
1770
       * higher octet and use the lower octet as the key index
1771
       * instead of rejecting the IGTK and disconnecting. */
1772
0
      wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1773
0
        "WPA: Workaround for non-compliant AP: corrected IGTK KeyID %d to %d",
1774
0
        keyidx, masked);
1775
0
      keyidx = masked;
1776
0
    } else {
1777
0
      wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1778
0
        "WPA: Invalid IGTK KeyID %d", keyidx);
1779
0
      return -1;
1780
0
    }
1781
0
  }
1782
0
  if (wpa_sm_set_key(sm, -1, wpa_cipher_to_alg(sm->mgmt_group_cipher),
1783
0
         broadcast_ether_addr,
1784
0
         keyidx, 0, igtk->pn, sizeof(igtk->pn),
1785
0
         igtk->igtk, len, KEY_FLAG_GROUP_RX) < 0) {
1786
0
    if (keyidx == 0x0400 || keyidx == 0x0500) {
1787
      /* Assume the AP has broken PMF implementation since it
1788
       * seems to have swapped the KeyID bytes. The AP cannot
1789
       * be trusted to implement BIP correctly or provide a
1790
       * valid IGTK, so do not try to configure this key with
1791
       * swapped KeyID bytes. Instead, continue without
1792
       * configuring the IGTK so that the driver can drop any
1793
       * received group-addressed robust management frames due
1794
       * to missing keys.
1795
       *
1796
       * Normally, this error behavior would result in us
1797
       * disconnecting, but there are number of deployed APs
1798
       * with this broken behavior, so as an interoperability
1799
       * workaround, allow the connection to proceed. */
1800
0
      wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1801
0
        "WPA: Ignore IGTK configuration error due to invalid IGTK KeyID byte order");
1802
0
    } else {
1803
0
      wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1804
0
        "WPA: Failed to configure IGTK to the driver");
1805
0
      return -1;
1806
0
    }
1807
0
  }
1808
1809
0
  if (wnm_sleep) {
1810
0
    sm->igtk_wnm_sleep.igtk_len = len;
1811
0
    os_memcpy(sm->igtk_wnm_sleep.igtk, igtk->igtk,
1812
0
        sm->igtk_wnm_sleep.igtk_len);
1813
0
  } else {
1814
0
    sm->igtk.igtk_len = len;
1815
0
    os_memcpy(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len);
1816
0
  }
1817
1818
0
  return 0;
1819
0
}
1820
1821
1822
static int wpa_supplicant_install_bigtk(struct wpa_sm *sm,
1823
               const struct wpa_bigtk_kde *bigtk,
1824
               int wnm_sleep)
1825
0
{
1826
0
  size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1827
0
  u16 keyidx = WPA_GET_LE16(bigtk->keyid);
1828
1829
  /* Detect possible key reinstallation */
1830
0
  if ((sm->bigtk.bigtk_len == len &&
1831
0
       os_memcmp(sm->bigtk.bigtk, bigtk->bigtk,
1832
0
           sm->bigtk.bigtk_len) == 0) ||
1833
0
      (sm->bigtk_wnm_sleep.bigtk_len == len &&
1834
0
       os_memcmp(sm->bigtk_wnm_sleep.bigtk, bigtk->bigtk,
1835
0
           sm->bigtk_wnm_sleep.bigtk_len) == 0)) {
1836
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1837
0
      "WPA: Not reinstalling already in-use BIGTK to the driver (keyidx=%d)",
1838
0
      keyidx);
1839
0
    return  0;
1840
0
  }
1841
1842
0
  wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1843
0
    "WPA: BIGTK keyid %d pn " COMPACT_MACSTR,
1844
0
    keyidx, MAC2STR(bigtk->pn));
1845
0
  wpa_hexdump_key(MSG_DEBUG, "WPA: BIGTK", bigtk->bigtk, len);
1846
0
  if (keyidx < 6 || keyidx > 7) {
1847
0
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1848
0
      "WPA: Invalid BIGTK KeyID %d", keyidx);
1849
0
    return -1;
1850
0
  }
1851
0
  if (wpa_sm_set_key(sm, -1, wpa_cipher_to_alg(sm->mgmt_group_cipher),
1852
0
         broadcast_ether_addr,
1853
0
         keyidx, 0, bigtk->pn, sizeof(bigtk->pn),
1854
0
         bigtk->bigtk, len, KEY_FLAG_GROUP_RX) < 0) {
1855
0
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1856
0
      "WPA: Failed to configure BIGTK to the driver");
1857
0
    return -1;
1858
0
  }
1859
1860
0
  if (wnm_sleep) {
1861
0
    sm->bigtk_wnm_sleep.bigtk_len = len;
1862
0
    os_memcpy(sm->bigtk_wnm_sleep.bigtk, bigtk->bigtk,
1863
0
        sm->bigtk_wnm_sleep.bigtk_len);
1864
0
  } else {
1865
0
    sm->bigtk.bigtk_len = len;
1866
0
    os_memcpy(sm->bigtk.bigtk, bigtk->bigtk, sm->bigtk.bigtk_len);
1867
0
  }
1868
1869
0
  return 0;
1870
0
}
1871
1872
1873
static int wpa_supplicant_install_mlo_igtk(struct wpa_sm *sm, u8 link_id,
1874
             const struct rsn_mlo_igtk_kde *igtk,
1875
             int wnm_sleep)
1876
0
{
1877
0
  size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1878
0
  u16 keyidx = WPA_GET_LE16(igtk->keyid);
1879
1880
  /* Detect possible key reinstallation */
1881
0
  if ((sm->mlo.links[link_id].igtk.igtk_len == len &&
1882
0
       os_memcmp(sm->mlo.links[link_id].igtk.igtk, igtk->igtk,
1883
0
           sm->mlo.links[link_id].igtk.igtk_len) == 0) ||
1884
0
      (sm->mlo.links[link_id].igtk_wnm_sleep.igtk_len == len &&
1885
0
       os_memcmp(sm->mlo.links[link_id].igtk_wnm_sleep.igtk, igtk->igtk,
1886
0
           sm->mlo.links[link_id].igtk_wnm_sleep.igtk_len) == 0)) {
1887
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1888
0
      "RSN: Not reinstalling already in-use IGTK to the driver (link_id=%d keyidx=%d)",
1889
0
      link_id, keyidx);
1890
0
    return 0;
1891
0
  }
1892
1893
0
  wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1894
0
    "RSN: MLO Link %u IGTK keyid %d pn " COMPACT_MACSTR,
1895
0
    link_id, keyidx, MAC2STR(igtk->pn));
1896
0
  wpa_hexdump_link_key(MSG_DEBUG, link_id, "RSN: IGTK", igtk->igtk, len);
1897
0
  if (keyidx > 4095) {
1898
0
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1899
0
      "RSN: Invalid MLO Link %d IGTK KeyID %d", link_id,
1900
0
      keyidx);
1901
0
    return -1;
1902
0
  }
1903
0
  if (wpa_sm_set_key(sm, link_id,
1904
0
         wpa_cipher_to_alg(sm->mgmt_group_cipher),
1905
0
         broadcast_ether_addr, keyidx, 0, igtk->pn,
1906
0
         sizeof(igtk->pn), igtk->igtk, len,
1907
0
         KEY_FLAG_GROUP_RX) < 0) {
1908
0
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1909
0
      "RSN: Failed to configure MLO Link %d IGTK to the driver",
1910
0
      link_id);
1911
0
    return -1;
1912
0
  }
1913
1914
0
  if (wnm_sleep) {
1915
0
    sm->mlo.links[link_id].igtk_wnm_sleep.igtk_len = len;
1916
0
    os_memcpy(sm->mlo.links[link_id].igtk_wnm_sleep.igtk,
1917
0
        igtk->igtk,
1918
0
        sm->mlo.links[link_id].igtk_wnm_sleep.igtk_len);
1919
0
  } else {
1920
0
    sm->mlo.links[link_id].igtk.igtk_len = len;
1921
0
    os_memcpy(sm->mlo.links[link_id].igtk.igtk, igtk->igtk,
1922
0
        sm->mlo.links[link_id].igtk.igtk_len);
1923
0
  }
1924
1925
0
  return 0;
1926
0
}
1927
1928
1929
static int
1930
wpa_supplicant_install_mlo_bigtk(struct wpa_sm *sm, u8 link_id,
1931
         const struct rsn_mlo_bigtk_kde *bigtk,
1932
         int wnm_sleep)
1933
0
{
1934
0
  size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1935
0
  u16 keyidx = WPA_GET_LE16(bigtk->keyid);
1936
1937
  /* Detect possible key reinstallation */
1938
0
  if ((sm->mlo.links[link_id].bigtk.bigtk_len == len &&
1939
0
       os_memcmp(sm->mlo.links[link_id].bigtk.bigtk, bigtk->bigtk,
1940
0
           sm->mlo.links[link_id].bigtk.bigtk_len) == 0) ||
1941
0
      (sm->mlo.links[link_id].bigtk_wnm_sleep.bigtk_len == len &&
1942
0
       os_memcmp(sm->mlo.links[link_id].bigtk_wnm_sleep.bigtk,
1943
0
           bigtk->bigtk,
1944
0
           sm->mlo.links[link_id].bigtk_wnm_sleep.bigtk_len) ==
1945
0
       0)) {
1946
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1947
0
      "RSN: Not reinstalling already in-use BIGTK to the driver (link_id=%d keyidx=%d)",
1948
0
      link_id, keyidx);
1949
0
    return  0;
1950
0
  }
1951
1952
0
  wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1953
0
    "RSN: MLO Link %u BIGTK keyid %d pn " COMPACT_MACSTR,
1954
0
    link_id, keyidx, MAC2STR(bigtk->pn));
1955
0
  wpa_hexdump_link_key(MSG_DEBUG, link_id, "RSN: BIGTK", bigtk->bigtk,
1956
0
           len);
1957
0
  if (keyidx < 6 || keyidx > 7) {
1958
0
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1959
0
      "RSN: Invalid MLO Link %d BIGTK KeyID %d", link_id,
1960
0
      keyidx);
1961
0
    return -1;
1962
0
  }
1963
0
  if (wpa_sm_set_key(sm, link_id,
1964
0
         wpa_cipher_to_alg(sm->mgmt_group_cipher),
1965
0
         broadcast_ether_addr, keyidx, 0, bigtk->pn,
1966
0
         sizeof(bigtk->pn), bigtk->bigtk, len,
1967
0
         KEY_FLAG_GROUP_RX) < 0) {
1968
0
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1969
0
      "RSN: Failed to configure MLO Link %d BIGTK to the driver",
1970
0
      link_id);
1971
0
    return -1;
1972
0
  }
1973
1974
0
  if (wnm_sleep) {
1975
0
    sm->mlo.links[link_id].bigtk_wnm_sleep.bigtk_len = len;
1976
0
    os_memcpy(sm->mlo.links[link_id].bigtk_wnm_sleep.bigtk,
1977
0
        bigtk->bigtk,
1978
0
        sm->mlo.links[link_id].bigtk_wnm_sleep.bigtk_len);
1979
0
  } else {
1980
0
    sm->mlo.links[link_id].bigtk.bigtk_len = len;
1981
0
    os_memcpy(sm->mlo.links[link_id].bigtk.bigtk, bigtk->bigtk,
1982
0
        sm->mlo.links[link_id].bigtk.bigtk_len);
1983
0
  }
1984
1985
0
  return 0;
1986
0
}
1987
1988
1989
static int _mlo_ieee80211w_set_keys(struct wpa_sm *sm, u8 link_id,
1990
            struct wpa_eapol_ie_parse *ie)
1991
0
{
1992
0
  size_t len;
1993
1994
0
  if (ie->mlo_igtk[link_id]) {
1995
0
    len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1996
0
    if (ie->mlo_igtk_len[link_id] !=
1997
0
        RSN_MLO_IGTK_KDE_PREFIX_LENGTH + len)
1998
0
      return -1;
1999
2000
0
    if (wpa_supplicant_install_mlo_igtk(
2001
0
          sm, link_id,
2002
0
          (const struct rsn_mlo_igtk_kde *)
2003
0
          ie->mlo_igtk[link_id],
2004
0
          0) < 0)
2005
0
      return -1;
2006
0
  }
2007
2008
0
  if (ie->mlo_bigtk[link_id] && sm->beacon_prot) {
2009
0
    len = wpa_cipher_key_len(sm->mgmt_group_cipher);
2010
0
    if (ie->mlo_bigtk_len[link_id] !=
2011
0
        RSN_MLO_BIGTK_KDE_PREFIX_LENGTH + len)
2012
0
      return -1;
2013
2014
0
    if (wpa_supplicant_install_mlo_bigtk(
2015
0
          sm, link_id,
2016
0
          (const struct rsn_mlo_bigtk_kde *)
2017
0
          ie->mlo_bigtk[link_id],
2018
0
          0) < 0)
2019
0
      return -1;
2020
0
  }
2021
2022
0
  return 0;
2023
0
}
2024
2025
2026
static int mlo_ieee80211w_set_keys(struct wpa_sm *sm,
2027
           struct wpa_eapol_ie_parse *ie)
2028
0
{
2029
0
  u8 i;
2030
2031
0
  if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) ||
2032
0
      sm->mgmt_group_cipher == WPA_CIPHER_GTK_NOT_USED)
2033
0
    return 0;
2034
2035
0
  for_each_link(sm->mlo.valid_links, i) {
2036
0
    if (_mlo_ieee80211w_set_keys(sm, i, ie))
2037
0
      return -1;
2038
0
  }
2039
2040
0
  return 0;
2041
0
}
2042
2043
2044
static int ieee80211w_set_keys(struct wpa_sm *sm,
2045
             struct wpa_eapol_ie_parse *ie)
2046
0
{
2047
0
  size_t len;
2048
2049
0
  if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) ||
2050
0
      sm->mgmt_group_cipher == WPA_CIPHER_GTK_NOT_USED)
2051
0
    return 0;
2052
2053
0
  if (ie->igtk) {
2054
0
    const struct wpa_igtk_kde *igtk;
2055
2056
0
    len = wpa_cipher_key_len(sm->mgmt_group_cipher);
2057
0
    if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
2058
0
      return -1;
2059
2060
0
    igtk = (const struct wpa_igtk_kde *) ie->igtk;
2061
0
    if (wpa_supplicant_install_igtk(sm, igtk, 0) < 0)
2062
0
      return -1;
2063
0
  }
2064
2065
0
  if (ie->bigtk && sm->beacon_prot) {
2066
0
    const struct wpa_bigtk_kde *bigtk;
2067
2068
0
    len = wpa_cipher_key_len(sm->mgmt_group_cipher);
2069
0
    if (ie->bigtk_len != WPA_BIGTK_KDE_PREFIX_LEN + len)
2070
0
      return -1;
2071
2072
0
    bigtk = (const struct wpa_bigtk_kde *) ie->bigtk;
2073
0
    if (wpa_supplicant_install_bigtk(sm, bigtk, 0) < 0)
2074
0
      return -1;
2075
0
  }
2076
2077
0
  return 0;
2078
0
}
2079
2080
2081
static void wpa_report_ie_mismatch(struct wpa_sm *sm,
2082
           const char *reason, const u8 *src_addr,
2083
           const u8 *wpa_ie, size_t wpa_ie_len,
2084
           const u8 *rsn_ie, size_t rsn_ie_len)
2085
0
{
2086
0
  wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
2087
0
    reason, MAC2STR(src_addr));
2088
2089
0
  if (sm->ap_wpa_ie) {
2090
0
    wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
2091
0
          sm->ap_wpa_ie, sm->ap_wpa_ie_len);
2092
0
  }
2093
0
  if (wpa_ie) {
2094
0
    if (!sm->ap_wpa_ie) {
2095
0
      wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2096
0
        "WPA: No WPA IE in Beacon/ProbeResp");
2097
0
    }
2098
0
    wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
2099
0
          wpa_ie, wpa_ie_len);
2100
0
  }
2101
2102
0
  if (sm->ap_rsn_ie) {
2103
0
    wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
2104
0
          sm->ap_rsn_ie, sm->ap_rsn_ie_len);
2105
0
  }
2106
0
  if (rsn_ie) {
2107
0
    if (!sm->ap_rsn_ie) {
2108
0
      wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2109
0
        "WPA: No RSN IE in Beacon/ProbeResp");
2110
0
    }
2111
0
    wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
2112
0
          rsn_ie, rsn_ie_len);
2113
0
  }
2114
2115
0
  wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
2116
0
}
2117
2118
2119
#ifdef CONFIG_IEEE80211R
2120
2121
static int ft_validate_mdie(struct wpa_sm *sm,
2122
          const unsigned char *src_addr,
2123
          struct wpa_eapol_ie_parse *ie,
2124
          const u8 *assoc_resp_mdie)
2125
0
{
2126
0
  struct rsn_mdie *mdie;
2127
2128
0
  mdie = (struct rsn_mdie *) (ie->mdie + 2);
2129
0
  if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
2130
0
      os_memcmp(mdie->mobility_domain, sm->mobility_domain,
2131
0
          MOBILITY_DOMAIN_ID_LEN) != 0) {
2132
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
2133
0
      "not match with the current mobility domain");
2134
0
    return -1;
2135
0
  }
2136
2137
0
  if (assoc_resp_mdie &&
2138
0
      (assoc_resp_mdie[1] != ie->mdie[1] ||
2139
0
       os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
2140
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
2141
0
    wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
2142
0
          ie->mdie, 2 + ie->mdie[1]);
2143
0
    wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
2144
0
          assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
2145
0
    return -1;
2146
0
  }
2147
2148
0
  return 0;
2149
0
}
2150
2151
2152
static int ft_validate_ftie(struct wpa_sm *sm,
2153
          const unsigned char *src_addr,
2154
          struct wpa_eapol_ie_parse *ie,
2155
          const u8 *assoc_resp_ftie)
2156
0
{
2157
0
  if (ie->ftie == NULL) {
2158
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2159
0
      "FT: No FTIE in EAPOL-Key msg 3/4");
2160
0
    return -1;
2161
0
  }
2162
2163
0
  if (assoc_resp_ftie == NULL)
2164
0
    return 0;
2165
2166
0
  if (assoc_resp_ftie[1] != ie->ftie[1] ||
2167
0
      os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
2168
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
2169
0
    wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
2170
0
          ie->ftie, 2 + ie->ftie[1]);
2171
0
    wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
2172
0
          assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
2173
0
    return -1;
2174
0
  }
2175
2176
0
  return 0;
2177
0
}
2178
2179
2180
static int ft_validate_rsnie(struct wpa_sm *sm,
2181
           const unsigned char *src_addr,
2182
           struct wpa_eapol_ie_parse *ie)
2183
0
{
2184
0
  struct wpa_ie_data rsn;
2185
2186
0
  if (!ie->rsn_ie)
2187
0
    return 0;
2188
2189
  /*
2190
   * Verify that PMKR1Name from EAPOL-Key message 3/4
2191
   * matches with the value we derived.
2192
   */
2193
0
  if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
2194
0
      rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
2195
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
2196
0
      "FT 4-way handshake message 3/4");
2197
0
    return -1;
2198
0
  }
2199
2200
0
  if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
2201
0
  {
2202
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2203
0
      "FT: PMKR1Name mismatch in "
2204
0
      "FT 4-way handshake message 3/4");
2205
0
    wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
2206
0
          rsn.pmkid, WPA_PMK_NAME_LEN);
2207
0
    wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
2208
0
          sm->pmk_r1_name, WPA_PMK_NAME_LEN);
2209
0
    return -1;
2210
0
  }
2211
2212
0
  return 0;
2213
0
}
2214
2215
2216
static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
2217
           const unsigned char *src_addr,
2218
           struct wpa_eapol_ie_parse *ie)
2219
0
{
2220
0
  const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
2221
2222
0
  if (sm->assoc_resp_ies) {
2223
0
    pos = sm->assoc_resp_ies;
2224
0
    end = pos + sm->assoc_resp_ies_len;
2225
0
    while (end - pos > 2) {
2226
0
      if (2 + pos[1] > end - pos)
2227
0
        break;
2228
0
      switch (*pos) {
2229
0
      case WLAN_EID_MOBILITY_DOMAIN:
2230
0
        mdie = pos;
2231
0
        break;
2232
0
      case WLAN_EID_FAST_BSS_TRANSITION:
2233
0
        ftie = pos;
2234
0
        break;
2235
0
      }
2236
0
      pos += 2 + pos[1];
2237
0
    }
2238
0
  }
2239
2240
0
  if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
2241
0
      ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
2242
0
      ft_validate_rsnie(sm, src_addr, ie) < 0)
2243
0
    return -1;
2244
2245
0
  return 0;
2246
0
}
2247
2248
#endif /* CONFIG_IEEE80211R */
2249
2250
2251
static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
2252
              const unsigned char *src_addr,
2253
              struct wpa_eapol_ie_parse *ie)
2254
0
{
2255
0
  if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
2256
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2257
0
      "WPA: No WPA/RSN IE for this AP known. "
2258
0
      "Trying to get from scan results");
2259
0
    if (wpa_sm_get_beacon_ie(sm) < 0) {
2260
0
      wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2261
0
        "WPA: Could not find AP from "
2262
0
        "the scan results");
2263
0
      return -1;
2264
0
    }
2265
0
    wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
2266
0
      "WPA: Found the current AP from updated scan results");
2267
0
  }
2268
2269
0
  if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
2270
0
      (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
2271
0
    wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
2272
0
               "with IE in Beacon/ProbeResp (no IE?)",
2273
0
               src_addr, ie->wpa_ie, ie->wpa_ie_len,
2274
0
               ie->rsn_ie, ie->rsn_ie_len);
2275
0
    return -1;
2276
0
  }
2277
2278
0
  if ((ie->wpa_ie && sm->ap_wpa_ie &&
2279
0
       (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
2280
0
        os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
2281
0
      (ie->rsn_ie && sm->ap_rsn_ie &&
2282
0
       wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
2283
0
        sm->ap_rsn_ie, sm->ap_rsn_ie_len,
2284
0
        ie->rsn_ie, ie->rsn_ie_len))) {
2285
0
    wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
2286
0
               "with IE in Beacon/ProbeResp",
2287
0
               src_addr, ie->wpa_ie, ie->wpa_ie_len,
2288
0
               ie->rsn_ie, ie->rsn_ie_len);
2289
0
    return -1;
2290
0
  }
2291
2292
0
  if (sm->proto == WPA_PROTO_WPA &&
2293
0
      ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
2294
0
    wpa_report_ie_mismatch(sm, "Possible downgrade attack "
2295
0
               "detected - RSN was enabled and RSN IE "
2296
0
               "was in msg 3/4, but not in "
2297
0
               "Beacon/ProbeResp",
2298
0
               src_addr, ie->wpa_ie, ie->wpa_ie_len,
2299
0
               ie->rsn_ie, ie->rsn_ie_len);
2300
0
    return -1;
2301
0
  }
2302
2303
0
  if (sm->proto == WPA_PROTO_RSN &&
2304
0
      ((sm->ap_rsnxe && !ie->rsnxe) ||
2305
0
       (!sm->ap_rsnxe && ie->rsnxe) ||
2306
0
       (sm->ap_rsnxe && ie->rsnxe &&
2307
0
        (sm->ap_rsnxe_len != ie->rsnxe_len ||
2308
0
         os_memcmp(sm->ap_rsnxe, ie->rsnxe, sm->ap_rsnxe_len) != 0)))) {
2309
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2310
0
      "WPA: RSNXE mismatch between Beacon/ProbeResp and EAPOL-Key msg 3/4");
2311
0
    wpa_hexdump(MSG_INFO, "RSNXE in Beacon/ProbeResp",
2312
0
          sm->ap_rsnxe, sm->ap_rsnxe_len);
2313
0
    wpa_hexdump(MSG_INFO, "RSNXE in EAPOL-Key msg 3/4",
2314
0
          ie->rsnxe, ie->rsnxe_len);
2315
0
    wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
2316
0
    return -1;
2317
0
  }
2318
2319
0
  if (sm->proto == WPA_PROTO_RSN && wpa_sm_rsn_overriding_supported(sm)) {
2320
0
    if ((sm->ap_rsne_override && !ie->rsne_override) ||
2321
0
        (!sm->ap_rsne_override && ie->rsne_override) ||
2322
0
        (sm->ap_rsne_override && ie->rsne_override &&
2323
0
         (sm->ap_rsne_override_len != ie->rsne_override_len ||
2324
0
          os_memcmp(sm->ap_rsne_override, ie->rsne_override,
2325
0
        sm->ap_rsne_override_len) != 0))) {
2326
0
      wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2327
0
        "RSN: RSNE Override element mismatch between Beacon/ProbeResp and EAPOL-Key msg 3/4");
2328
0
      wpa_hexdump(MSG_INFO,
2329
0
            "RSNE Override element in Beacon/ProbeResp",
2330
0
            sm->ap_rsne_override,
2331
0
            sm->ap_rsne_override_len);
2332
0
      wpa_hexdump(MSG_INFO,
2333
0
            "RSNE Override element in EAPOL-Key msg 3/4",
2334
0
            ie->rsne_override, ie->rsne_override_len);
2335
0
      wpa_sm_deauthenticate(sm,
2336
0
                WLAN_REASON_IE_IN_4WAY_DIFFERS);
2337
0
      return -1;
2338
0
    }
2339
2340
0
    if ((sm->ap_rsne_override_2 && !ie->rsne_override_2) ||
2341
0
        (!sm->ap_rsne_override_2 && ie->rsne_override_2) ||
2342
0
        (sm->ap_rsne_override_2 && ie->rsne_override_2 &&
2343
0
         (sm->ap_rsne_override_2_len != ie->rsne_override_2_len ||
2344
0
          os_memcmp(sm->ap_rsne_override_2, ie->rsne_override_2,
2345
0
        sm->ap_rsne_override_2_len) != 0))) {
2346
0
      wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2347
0
        "RSN: RSNE Override 2 element mismatch between Beacon/ProbeResp and EAPOL-Key msg 3/4");
2348
0
      wpa_hexdump(MSG_INFO,
2349
0
            "RSNE Override 2 element in Beacon/ProbeResp",
2350
0
            sm->ap_rsne_override_2,
2351
0
            sm->ap_rsne_override_2_len);
2352
0
      wpa_hexdump(MSG_INFO,
2353
0
            "RSNE Override 2 element in EAPOL-Key msg 3/4",
2354
0
            ie->rsne_override_2, ie->rsne_override_2_len);
2355
0
      wpa_sm_deauthenticate(sm,
2356
0
                WLAN_REASON_IE_IN_4WAY_DIFFERS);
2357
0
      return -1;
2358
0
    }
2359
2360
0
    if ((sm->ap_rsnxe_override && !ie->rsnxe_override) ||
2361
0
        (!sm->ap_rsnxe_override && ie->rsnxe_override) ||
2362
0
        (sm->ap_rsnxe_override && ie->rsnxe_override &&
2363
0
         (sm->ap_rsnxe_override_len != ie->rsnxe_override_len ||
2364
0
          os_memcmp(sm->ap_rsnxe_override, ie->rsnxe_override,
2365
0
        sm->ap_rsnxe_override_len) != 0))) {
2366
0
      wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2367
0
        "RSN: RSNXE Override element mismatch between Beacon/ProbeResp and EAPOL-Key msg 3/4");
2368
0
      wpa_hexdump(MSG_INFO,
2369
0
            "RSNXE Override element in Beacon/ProbeResp",
2370
0
            sm->ap_rsnxe_override,
2371
0
            sm->ap_rsnxe_override_len);
2372
0
      wpa_hexdump(MSG_INFO,
2373
0
            "RSNXE Override element in EAPOL-Key msg 3/4",
2374
0
            ie->rsnxe_override, ie->rsnxe_override_len);
2375
0
      wpa_sm_deauthenticate(sm,
2376
0
                WLAN_REASON_IE_IN_4WAY_DIFFERS);
2377
0
      return -1;
2378
0
    }
2379
0
  }
2380
2381
0
#ifdef CONFIG_IEEE80211R
2382
0
  if (wpa_key_mgmt_ft(sm->key_mgmt) &&
2383
0
      wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
2384
0
    return -1;
2385
0
#endif /* CONFIG_IEEE80211R */
2386
2387
0
  return 0;
2388
0
}
2389
2390
2391
static int wpa_supplicant_validate_security_profile_elem(
2392
  struct wpa_sm *sm, const u8 *msg3_sp, size_t msg3_sp_len)
2393
0
{
2394
0
  const char *sp_fail = NULL;
2395
2396
  /*
2397
   * The Security Profile element is a BSS-wide attribute compared against
2398
   * the Beacon/Probe Response copy, so this check applies to both MLO and
2399
   * non-MLO connections. It is therefore kept out of
2400
   * wpa_supplicant_validate_ie() (which is gated on !mlo) and called
2401
   * unconditionally from wpa_supplicant_process_3_of_4().
2402
   */
2403
0
  if (!sm->security_profile_active)
2404
0
    return 0;
2405
2406
0
  if (sm->ap_security_profile && sm->ap_security_profile_len > 0) {
2407
0
    if (!msg3_sp || msg3_sp_len == 0) {
2408
0
      sp_fail = "present in Beacon/Probe Response frame but absent in EAPOL-Key msg 3/4";
2409
0
    } else if (msg3_sp_len != sm->ap_security_profile_len ||
2410
0
         os_memcmp(msg3_sp, sm->ap_security_profile,
2411
0
             sm->ap_security_profile_len) != 0) {
2412
0
      sp_fail = "in EAPOL-Key msg 3/4 does not match Beacon/Probe Response frame";
2413
0
    } else {
2414
0
      wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2415
0
        "Security Profile element in EAPOL-Key msg 3/4 matches Beacon/Probe Response frame");
2416
0
    }
2417
0
  } else if (msg3_sp && msg3_sp_len > 0) {
2418
0
    sp_fail = "present in EAPOL-Key msg 3/4 but absent in Beacon/Probe Response frame";
2419
0
  }
2420
2421
0
  if (!sp_fail)
2422
0
    return 0;
2423
2424
0
  wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "Security Profile element %s",
2425
0
    sp_fail);
2426
0
  wpa_hexdump(MSG_DEBUG,
2427
0
        "Security Profile element in Beacon/Probe Response frame",
2428
0
        sm->ap_security_profile, sm->ap_security_profile_len);
2429
0
  wpa_hexdump(MSG_DEBUG, "Security Profile element in EAPOL-Key msg 3/4",
2430
0
        msg3_sp, msg3_sp_len);
2431
0
  return -1;
2432
0
}
2433
2434
2435
/**
2436
 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
2437
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2438
 * @dst: Destination address for the frame
2439
 * @key: Pointer to the EAPOL-Key frame header
2440
 * @ver: Version bits from EAPOL-Key Key Info
2441
 * @key_info: Key Info
2442
 * @ptk: PTK to use for keyed hash and encryption
2443
 * Returns: >= 0 on success, < 0 on failure
2444
 */
2445
int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
2446
             const struct wpa_eapol_key *key,
2447
             u16 ver, u16 key_info,
2448
             struct wpa_ptk *ptk)
2449
0
{
2450
0
  size_t mic_len, hdrlen, rlen;
2451
0
  struct wpa_eapol_key *reply;
2452
0
  u8 *rbuf, *key_mic;
2453
0
  u8 *kde = NULL;
2454
0
  size_t kde_len = 0, extra_len = 0;
2455
#ifdef CONFIG_TESTING_OPTIONS
2456
  size_t pad_len = 0;
2457
#endif /* CONFIG_TESTING_OPTIONS */
2458
2459
0
  if (sm->mlo.valid_links) {
2460
0
    u8 *pos;
2461
2462
0
    kde = os_malloc(RSN_SELECTOR_LEN + ETH_ALEN + 2);
2463
0
    if (!kde)
2464
0
      return -1;
2465
2466
    /* Add MAC KDE */
2467
0
    wpa_printf(MSG_DEBUG, "MLO: Add MAC KDE into EAPOL-Key 4/4");
2468
0
    pos = kde;
2469
0
    pos = rsn_add_kde(pos, RSN_KEY_DATA_MAC_ADDR, sm->own_addr,
2470
0
          ETH_ALEN);
2471
0
    kde_len = pos - kde;
2472
0
  }
2473
2474
#ifdef CONFIG_TESTING_OPTIONS
2475
  if (sm->test_eapol_m4_elems)
2476
    extra_len = wpabuf_len(sm->test_eapol_m4_elems);
2477
  if (sm->encrypt_eapol_m4) {
2478
    pad_len = (kde_len + extra_len) % 8;
2479
    if (pad_len)
2480
      pad_len = 8 - pad_len;
2481
    extra_len += pad_len + 8;
2482
  }
2483
#endif /* CONFIG_TESTING_OPTIONS */
2484
2485
0
  mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len, sm->hash_alg,
2486
0
            sm->pasn_group);
2487
0
  hdrlen = sizeof(*reply) + mic_len + 2;
2488
0
  rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
2489
0
          hdrlen + kde_len + extra_len, &rlen,
2490
0
          (void *) &reply);
2491
0
  if (!rbuf) {
2492
0
    os_free(kde);
2493
0
    return -1;
2494
0
  }
2495
2496
0
  reply->type = (sm->proto == WPA_PROTO_RSN) ?
2497
0
    EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
2498
0
  key_info &= WPA_KEY_INFO_SECURE;
2499
0
  key_info |= ver | WPA_KEY_INFO_KEY_TYPE;
2500
0
  if (mic_len)
2501
0
    key_info |= WPA_KEY_INFO_MIC;
2502
0
  else
2503
0
    key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
2504
#ifdef CONFIG_TESTING_OPTIONS
2505
  if (sm->encrypt_eapol_m4)
2506
    key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
2507
#endif /* CONFIG_TESTING_OPTIONS */
2508
0
  WPA_PUT_BE16(reply->key_info, key_info);
2509
0
  if (sm->proto == WPA_PROTO_RSN)
2510
0
    WPA_PUT_BE16(reply->key_length, 0);
2511
0
  else
2512
0
    os_memcpy(reply->key_length, key->key_length, 2);
2513
0
  os_memcpy(reply->replay_counter, key->replay_counter,
2514
0
      WPA_REPLAY_COUNTER_LEN);
2515
2516
0
  key_mic = (u8 *) (reply + 1);
2517
  /* Key Data length */
2518
0
  WPA_PUT_BE16(key_mic + mic_len, kde_len + extra_len);
2519
0
  if (kde) {
2520
0
    os_memcpy(key_mic + mic_len + 2, kde, kde_len); /* Key Data */
2521
0
    os_free(kde);
2522
0
  }
2523
2524
#ifdef CONFIG_TESTING_OPTIONS
2525
  if (sm->test_eapol_m4_elems) {
2526
    os_memcpy(key_mic + mic_len + 2 + kde_len,
2527
        wpabuf_head(sm->test_eapol_m4_elems),
2528
        wpabuf_len(sm->test_eapol_m4_elems));
2529
  }
2530
2531
  if (sm->encrypt_eapol_m4) {
2532
    u8 *plain;
2533
    size_t plain_len;
2534
2535
    if (sm->test_eapol_m4_elems)
2536
      extra_len = wpabuf_len(sm->test_eapol_m4_elems);
2537
    else
2538
      extra_len = 0;
2539
    plain_len = kde_len + extra_len + pad_len;
2540
    plain = os_memdup(key_mic + mic_len + 2, plain_len);
2541
    if (!plain) {
2542
      os_free(rbuf);
2543
      return -1;
2544
    }
2545
    if (pad_len)
2546
      plain[plain_len - pad_len] = 0xdd;
2547
2548
    wpa_hexdump_key(MSG_DEBUG, "RSN: AES-WRAP using KEK",
2549
        ptk->kek, ptk->kek_len);
2550
    if (aes_wrap(ptk->kek, ptk->kek_len, plain_len / 8, plain,
2551
           key_mic + mic_len + 2)) {
2552
      os_free(plain);
2553
      os_free(rbuf);
2554
      return -1;
2555
    }
2556
    wpa_hexdump(MSG_DEBUG,
2557
          "RSN: Encrypted Key Data from AES-WRAP",
2558
          key_mic + mic_len + 2, plain_len + 8);
2559
    os_free(plain);
2560
  }
2561
#endif /* CONFIG_TESTING_OPTIONS */
2562
2563
0
  wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 4/4");
2564
0
  return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
2565
0
          key_mic);
2566
0
}
2567
2568
2569
static int wpa_supplicant_validate_link_kde(struct wpa_sm *sm, u8 link_id,
2570
              const u8 *link_kde,
2571
              size_t link_kde_len,
2572
              const u8 *rsn_override_link_kde,
2573
              size_t rsn_override_link_kde_len)
2574
0
{
2575
0
  size_t rsne_len = 0, rsnxe_len = 0, rsnoe_len = 0, rsno2e_len = 0,
2576
0
    rsnxoe_len = 0;
2577
0
  const u8 *rsne = NULL, *rsnxe = NULL, *rsnoe = NULL, *rsno2e = NULL,
2578
0
    *rsnxoe = NULL;
2579
2580
0
  if (!link_kde ||
2581
0
      link_kde_len < RSN_MLO_LINK_KDE_LINK_MAC_INDEX + ETH_ALEN) {
2582
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2583
0
      "RSN: MLO Link KDE is not found for link ID %d",
2584
0
      link_id);
2585
0
    return -1;
2586
0
  }
2587
2588
0
  if (!ether_addr_equal(sm->mlo.links[link_id].bssid,
2589
0
            &link_kde[RSN_MLO_LINK_KDE_LINK_MAC_INDEX])) {
2590
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2591
0
      "RSN: MLO Link %u MAC address (" MACSTR
2592
0
      ") not matching association response (" MACSTR ")",
2593
0
      link_id,
2594
0
      MAC2STR(&link_kde[RSN_MLO_LINK_KDE_LINK_MAC_INDEX]),
2595
0
      MAC2STR(sm->mlo.links[link_id].bssid));
2596
0
    return -1;
2597
0
  }
2598
2599
0
  if (link_kde[0] & RSN_MLO_LINK_KDE_LI_RSNE_INFO) {
2600
0
    rsne = link_kde + RSN_MLO_LINK_KDE_FIXED_LENGTH;
2601
0
    if (link_kde_len < RSN_MLO_LINK_KDE_FIXED_LENGTH + 2 ||
2602
0
        link_kde_len <
2603
0
        (size_t) (RSN_MLO_LINK_KDE_FIXED_LENGTH + 2 + rsne[1])) {
2604
0
      wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2605
0
        "RSN: No room for link %u RSNE in MLO Link KDE",
2606
0
        link_id);
2607
0
      return -1;
2608
0
    }
2609
2610
0
    rsne_len = rsne[1] + 2;
2611
0
  }
2612
2613
0
  if (!rsne) {
2614
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2615
0
      "RSN: RSNE not present in MLO Link %u KDE", link_id);
2616
0
    return -1;
2617
0
  }
2618
2619
0
  if (link_kde[0] & RSN_MLO_LINK_KDE_LI_RSNXE_INFO) {
2620
0
    rsnxe = link_kde + RSN_MLO_LINK_KDE_FIXED_LENGTH + rsne_len;
2621
0
    if (link_kde_len <
2622
0
        (RSN_MLO_LINK_KDE_FIXED_LENGTH + rsne_len + 2) ||
2623
0
        link_kde_len <
2624
0
        (RSN_MLO_LINK_KDE_FIXED_LENGTH + rsne_len + 2 + rsnxe[1])) {
2625
0
      wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2626
0
        "RSN: No room for link %u RSNXE in MLO Link KDE",
2627
0
        link_id);
2628
0
      return -1;
2629
0
    }
2630
2631
0
    rsnxe_len = rsnxe[1] + 2;
2632
0
  }
2633
2634
0
  if (wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
2635
0
             sm->mlo.links[link_id].ap_rsne,
2636
0
             sm->mlo.links[link_id].ap_rsne_len,
2637
0
             rsne, rsne_len)) {
2638
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2639
0
      "RSN MLO: RSNE in 3/4 msg does not match with IE in Beacon/ProbeResp for link ID %u",
2640
0
      link_id);
2641
0
    wpa_hexdump(MSG_INFO, "RSNE in Beacon/ProbeResp",
2642
0
          sm->mlo.links[link_id].ap_rsne,
2643
0
          sm->mlo.links[link_id].ap_rsne_len);
2644
0
    wpa_hexdump(MSG_INFO, "RSNE in EAPOL-Key msg 3/4",
2645
0
          rsne, rsne_len);
2646
0
    goto fail;
2647
0
  }
2648
2649
0
  if ((sm->mlo.links[link_id].ap_rsnxe && !rsnxe) ||
2650
0
      (!sm->mlo.links[link_id].ap_rsnxe && rsnxe) ||
2651
0
      (sm->mlo.links[link_id].ap_rsnxe && rsnxe &&
2652
0
       (sm->mlo.links[link_id].ap_rsnxe_len != rsnxe_len ||
2653
0
        os_memcmp(sm->mlo.links[link_id].ap_rsnxe, rsnxe,
2654
0
      sm->mlo.links[link_id].ap_rsnxe_len) != 0))) {
2655
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2656
0
      "RSN MLO: RSNXE mismatch between Beacon/ProbeResp and EAPOL-Key msg 3/4 for link ID %u",
2657
0
      link_id);
2658
0
    wpa_hexdump(MSG_INFO, "RSNXE in Beacon/ProbeResp",
2659
0
          sm->mlo.links[link_id].ap_rsnxe,
2660
0
          sm->mlo.links[link_id].ap_rsnxe_len);
2661
0
    wpa_hexdump(MSG_INFO, "RSNXE in EAPOL-Key msg 3/4",
2662
0
          rsnxe, rsnxe_len);
2663
0
    goto fail;
2664
0
  }
2665
2666
0
  if (!wpa_sm_rsn_overriding_supported(sm))
2667
0
    return 0;
2668
2669
0
  if (rsn_override_link_kde) {
2670
0
    rsnoe = get_vendor_ie(rsn_override_link_kde + 1,
2671
0
              rsn_override_link_kde_len - 1,
2672
0
              RSNE_OVERRIDE_IE_VENDOR_TYPE);
2673
0
    if (rsnoe)
2674
0
      rsnoe_len = 2 + rsnoe[1];
2675
2676
0
    rsno2e = get_vendor_ie(rsn_override_link_kde + 1,
2677
0
               rsn_override_link_kde_len - 1,
2678
0
               RSNE_OVERRIDE_2_IE_VENDOR_TYPE);
2679
0
    if (rsno2e)
2680
0
      rsno2e_len = 2 + rsno2e[1];
2681
2682
0
    rsnxoe = get_vendor_ie(rsn_override_link_kde + 1,
2683
0
               rsn_override_link_kde_len - 1,
2684
0
               RSNXE_OVERRIDE_IE_VENDOR_TYPE);
2685
0
    if (rsnxoe)
2686
0
      rsnxoe_len = 2 + rsnxoe[1];
2687
0
  }
2688
2689
0
  if ((sm->mlo.links[link_id].ap_rsnoe && !rsnoe) ||
2690
0
      (!sm->mlo.links[link_id].ap_rsnoe && rsnoe) ||
2691
0
      (sm->mlo.links[link_id].ap_rsnoe && rsnoe &&
2692
0
       wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
2693
0
        sm->mlo.links[link_id].ap_rsnoe,
2694
0
        sm->mlo.links[link_id].ap_rsnoe_len,
2695
0
        rsnoe, rsnoe_len))) {
2696
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2697
0
      "RSN MLO: RSNOE in 3/4 msg does not match with IE in Beacon/ProbeResp for link ID %u",
2698
0
      link_id);
2699
0
    wpa_hexdump(MSG_INFO, "RSNOE in Beacon/ProbeResp",
2700
0
          sm->mlo.links[link_id].ap_rsnoe,
2701
0
          sm->mlo.links[link_id].ap_rsnoe_len);
2702
0
    wpa_hexdump(MSG_INFO, "RSNOE in EAPOL-Key msg 3/4",
2703
0
          rsnoe, rsnoe_len);
2704
0
    goto fail;
2705
0
  }
2706
2707
0
  if ((sm->mlo.links[link_id].ap_rsno2e && !rsno2e) ||
2708
0
      (!sm->mlo.links[link_id].ap_rsno2e && rsno2e) ||
2709
0
      (sm->mlo.links[link_id].ap_rsno2e && rsno2e &&
2710
0
       wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
2711
0
        sm->mlo.links[link_id].ap_rsno2e,
2712
0
        sm->mlo.links[link_id].ap_rsno2e_len,
2713
0
        rsno2e, rsno2e_len))) {
2714
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2715
0
      "RSN MLO: RSNO2E in 3/4 msg does not match with IE in Beacon/ProbeResp for link ID %u",
2716
0
      link_id);
2717
0
    wpa_hexdump(MSG_INFO, "RSNO2E in Beacon/ProbeResp",
2718
0
          sm->mlo.links[link_id].ap_rsno2e,
2719
0
          sm->mlo.links[link_id].ap_rsno2e_len);
2720
0
    wpa_hexdump(MSG_INFO, "RSNOE in EAPOL-Key msg 3/4",
2721
0
          rsno2e, rsno2e_len);
2722
0
    goto fail;
2723
0
  }
2724
2725
0
  if ((sm->mlo.links[link_id].ap_rsnxoe && !rsnxoe) ||
2726
0
      (!sm->mlo.links[link_id].ap_rsnxoe && rsnxoe) ||
2727
0
      (sm->mlo.links[link_id].ap_rsnxoe && rsnxoe &&
2728
0
       (sm->mlo.links[link_id].ap_rsnxoe_len != rsnxoe_len ||
2729
0
        os_memcmp(sm->mlo.links[link_id].ap_rsnxoe, rsnxoe,
2730
0
      sm->mlo.links[link_id].ap_rsnxoe_len) != 0))) {
2731
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2732
0
      "RSN MLO: RSNXOE mismatch between Beacon/ProbeResp and EAPOL-Key msg 3/4 for link ID %u",
2733
0
      link_id);
2734
0
    wpa_hexdump(MSG_INFO, "RSNXOE in Beacon/ProbeResp",
2735
0
          sm->mlo.links[link_id].ap_rsnxoe,
2736
0
          sm->mlo.links[link_id].ap_rsnxoe_len);
2737
0
    wpa_hexdump(MSG_INFO, "RSNXOE in EAPOL-Key msg 3/4",
2738
0
          rsnxoe, rsnxoe_len);
2739
0
    goto fail;
2740
0
  }
2741
2742
0
  return 0;
2743
0
fail:
2744
0
  wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
2745
0
  return -1;
2746
0
}
2747
2748
2749
static int wpa_validate_mlo_ieee80211w_kdes(struct wpa_sm *sm,
2750
              u8 link_id,
2751
              struct wpa_eapol_ie_parse *ie)
2752
0
{
2753
0
  if (ie->mlo_igtk[link_id] &&
2754
0
      ie->mlo_igtk_len[link_id] != RSN_MLO_IGTK_KDE_PREFIX_LENGTH +
2755
0
      (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
2756
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2757
0
      "RSN MLO: Invalid IGTK KDE length %lu for link ID %u",
2758
0
      (unsigned long) ie->mlo_igtk_len[link_id], link_id);
2759
0
    return -1;
2760
0
  }
2761
2762
0
  if (!sm->beacon_prot)
2763
0
    return 0;
2764
2765
0
  if (ie->mlo_bigtk[link_id] &&
2766
0
      ie->mlo_bigtk_len[link_id] != RSN_MLO_BIGTK_KDE_PREFIX_LENGTH +
2767
0
      (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
2768
0
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2769
0
      "RSN MLO: Invalid BIGTK KDE length %lu for link ID %u",
2770
0
      (unsigned long) ie->mlo_bigtk_len[link_id], link_id);
2771
0
    return -1;
2772
0
  }
2773
2774
0
  return 0;
2775
0
}
2776
2777
2778
static void wpa_supplicant_process_3_of_4_wpa(struct wpa_sm *sm,
2779
                const struct wpa_eapol_key *key,
2780
                u16 ver, const u8 *key_data,
2781
                size_t key_data_len)
2782
0
{
2783
0
  u16 key_info, keylen;
2784
0
  struct wpa_eapol_ie_parse ie;
2785
2786
0
  wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
2787
0
  wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2788
0
    "WPA: RX message 3 of 4-Way Handshake from " MACSTR
2789
0
    " (ver=%d)", MAC2STR(sm->bssid), ver);
2790
2791
0
  key_info = WPA_GET_BE16(key->key_info);
2792
2793
0
  wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
2794
0
  if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
2795
0
    goto failed;
2796
2797
0
  if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
2798
0
    goto failed;
2799
2800
0
  if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
2801
0
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2802
0
      "WPA: ANonce from message 1 of 4-Way Handshake differs from 3 of 4-Way Handshake - drop packet (src="
2803
0
      MACSTR ")", MAC2STR(sm->bssid));
2804
0
    goto failed;
2805
0
  }
2806
2807
0
  keylen = WPA_GET_BE16(key->key_length);
2808
0
  if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
2809
0
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2810
0
      "WPA: Invalid %s key length %d (src=" MACSTR ")",
2811
0
      wpa_cipher_txt(sm->pairwise_cipher), keylen,
2812
0
      MAC2STR(sm->bssid));
2813
0
    goto failed;
2814
0
  }
2815
2816
0
  if (wpa_supplicant_send_4_of_4(sm, wpa_sm_get_auth_addr(sm), key, ver,
2817
0
               key_info, &sm->ptk) < 0)
2818
0
    goto failed;
2819
2820
  /* SNonce was successfully used in msg 3/4, so mark it to be renewed
2821
   * for the next 4-Way Handshake. If msg 3 is received again, the old
2822
   * SNonce will still be used to avoid changing PTK. */
2823
0
  sm->renew_snonce = 1;
2824
2825
0
  if ((key_info & WPA_KEY_INFO_INSTALL) &&
2826
0
      wpa_supplicant_install_ptk(sm, key, KEY_FLAG_RX_TX))
2827
0
    goto failed;
2828
2829
0
  if (key_info & WPA_KEY_INFO_SECURE) {
2830
0
    wpa_sm_mlme_setprotection(
2831
0
      sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
2832
0
      MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
2833
0
    eapol_sm_notify_portValid(sm->eapol, true);
2834
0
  }
2835
0
  wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
2836
2837
0
  sm->msg_3_of_4_ok = 1;
2838
0
  return;
2839
2840
0
failed:
2841
0
  wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2842
0
}
2843
2844
2845
static void wpa_sm_sae_pw_id_change(struct wpa_sm *sm, const u8 *kde,
2846
            size_t kde_len)
2847
0
{
2848
0
  const u8 *pos = kde, *end = kde + kde_len;
2849
0
  u8 flags;
2850
0
  struct wpabuf_array *wa;
2851
2852
0
  wpa_hexdump(MSG_DEBUG, "RSN: Received SAE Password Identifiers KDE",
2853
0
        kde, kde_len);
2854
2855
0
  if (end - pos < 1)
2856
0
    return;
2857
0
  flags = *pos++;
2858
0
  wpa_printf(MSG_DEBUG, "RSN: SAE Password Identifiers Flags: 0x%x",
2859
0
       flags);
2860
2861
0
  if (!sm->ctx->sae_pw_id_change)
2862
0
    return;
2863
2864
0
  wa = wpabuf_array_alloc();
2865
0
  if (!wa)
2866
0
    return;
2867
2868
0
  while (end > pos) {
2869
0
    u8 len;
2870
2871
0
    len = *pos++;
2872
0
    if (end - pos < len) {
2873
0
      wpa_printf(MSG_INFO,
2874
0
           "RSN: Truncated SAE Password Identifier Tuple");
2875
0
      wpabuf_array_free(wa);
2876
0
      return;
2877
0
    }
2878
0
    wpa_hexdump(MSG_DEBUG, "RSN: SAE Password Identifier",
2879
0
          pos, len);
2880
0
    if (wpabuf_array_add(wa, wpabuf_alloc_copy(pos, len))) {
2881
0
      wpabuf_array_free(wa);
2882
0
      return;
2883
0
    }
2884
0
    pos += len;
2885
0
  }
2886
2887
0
  sm->ctx->sae_pw_id_change(sm->ctx->ctx, wa);
2888
0
}
2889
2890
2891
static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
2892
            const struct wpa_eapol_key *key,
2893
            u16 ver, const u8 *key_data,
2894
            size_t key_data_len)
2895
0
{
2896
0
  u16 key_info, keylen;
2897
0
  struct wpa_eapol_ie_parse ie;
2898
0
  bool mlo = sm->mlo.valid_links;
2899
0
  int i;
2900
2901
0
  wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
2902
0
  wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2903
0
    "RSN: RX message 3 of 4-Way Handshake from " MACSTR
2904
0
    " (ver=%d)%s", MAC2STR(sm->bssid), ver, mlo ? " (MLO)" : "");
2905
2906
0
  key_info = WPA_GET_BE16(key->key_info);
2907
2908
0
  wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
2909
0
  if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
2910
0
    goto failed;
2911
2912
0
  if (sm->ssid_protection) {
2913
0
    if (!ie.ssid) {
2914
0
      wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2915
0
        "RSN: No SSID included in EAPOL-Key msg 3/4");
2916
0
      goto failed;
2917
0
    }
2918
2919
0
    if (ie.ssid_len != sm->ssid_len ||
2920
0
        os_memcmp(ie.ssid, sm->ssid, sm->ssid_len) != 0) {
2921
0
      wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2922
0
        "RSN: SSID mismatch in EAPOL-Key msg 3/4");
2923
0
      wpa_hexdump_ascii(MSG_DEBUG, "RSN: Received SSID",
2924
0
            ie.ssid, ie.ssid_len);
2925
0
      wpa_hexdump_ascii(MSG_DEBUG, "RSN: Expected SSID",
2926
0
            sm->ssid, sm->ssid_len);
2927
0
      goto failed;
2928
0
    }
2929
2930
0
    wpa_sm_ssid_verified(sm);
2931
0
  }
2932
2933
0
  if (mlo && !ie.valid_mlo_gtks) {
2934
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2935
0
      "MLO RSN: No GTK KDE included in EAPOL-Key msg 3/4");
2936
0
    goto failed;
2937
0
  }
2938
0
  if (mlo &&
2939
0
      (key_info &
2940
0
       (WPA_KEY_INFO_ENCR_KEY_DATA | WPA_KEY_INFO_INSTALL |
2941
0
        WPA_KEY_INFO_SECURE)) !=
2942
0
      (WPA_KEY_INFO_ENCR_KEY_DATA | WPA_KEY_INFO_INSTALL |
2943
0
       WPA_KEY_INFO_SECURE)) {
2944
0
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2945
0
      "RSN MLO: Invalid key info (0x%x) in EAPOL-Key msg 3/4",
2946
0
      key_info);
2947
0
    goto failed;
2948
0
  }
2949
2950
0
  if (mlo && !is_valid_ap_mld_mac_kde(sm, ie.mac_addr)) {
2951
0
    wpa_printf(MSG_DEBUG, "RSN: Invalid AP MLD MAC address KDE");
2952
0
    goto failed;
2953
0
  }
2954
2955
0
  for (i = 0; mlo && i < MAX_NUM_MLD_LINKS; i++) {
2956
0
    if (!(sm->mlo.req_links & BIT(i)))
2957
0
      continue;
2958
2959
0
    if (wpa_supplicant_validate_link_kde(
2960
0
          sm, i, ie.mlo_link[i], ie.mlo_link_len[i],
2961
0
          ie.rsn_override_link[i],
2962
0
          ie.rsn_override_link_len[i]) < 0)
2963
0
      goto failed;
2964
2965
0
    if (!(sm->mlo.valid_links & BIT(i)))
2966
0
      continue;
2967
2968
0
    if (!ie.mlo_gtk[i]) {
2969
0
      wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2970
0
        "RSN: GTK not found for link ID %u", i);
2971
0
      goto failed;
2972
0
    }
2973
2974
0
    if (wpa_supplicant_validate_mlo_gtk_kde_len(
2975
0
          ie.mlo_gtk_len[i]) < 0) {
2976
0
      wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2977
0
        "RSN: Invalid MLO GTK KDE length %zu for link ID %u",
2978
0
        ie.mlo_gtk_len[i], i);
2979
0
      goto failed;
2980
0
    }
2981
2982
0
    if (sm->mgmt_group_cipher != WPA_CIPHER_GTK_NOT_USED &&
2983
0
        wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
2984
0
        wpa_validate_mlo_ieee80211w_kdes(sm, i, &ie) < 0)
2985
0
      goto failed;
2986
0
  }
2987
2988
0
#ifdef CONFIG_IEEE80211R
2989
0
  if (mlo && wpa_key_mgmt_ft(sm->key_mgmt) &&
2990
0
      wpa_supplicant_validate_ie_ft(sm, sm->bssid, &ie) < 0)
2991
0
    goto failed;
2992
0
#endif /* CONFIG_IEEE80211R */
2993
2994
0
  if (!mlo && ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
2995
0
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2996
0
      "WPA: GTK IE in unencrypted key data");
2997
0
    goto failed;
2998
0
  }
2999
3000
0
  if (!mlo && ie.gtk &&
3001
0
      wpa_supplicant_validate_gtk_kde_len(ie.gtk_len) < 0) {
3002
0
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3003
0
      "WPA: Invalid GTK KDE length %zu", ie.gtk_len);
3004
0
    goto failed;
3005
0
  }
3006
3007
0
  if (!mlo && ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
3008
0
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3009
0
      "WPA: IGTK KDE in unencrypted key data");
3010
0
    goto failed;
3011
0
  }
3012
3013
0
  if (!mlo && ie.igtk &&
3014
0
      sm->mgmt_group_cipher != WPA_CIPHER_GTK_NOT_USED &&
3015
0
      wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
3016
0
      ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN +
3017
0
      (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
3018
0
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3019
0
      "WPA: Invalid IGTK KDE length %lu",
3020
0
      (unsigned long) ie.igtk_len);
3021
0
    goto failed;
3022
0
  }
3023
3024
0
  if (!mlo && wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
3025
0
    goto failed;
3026
3027
  /*
3028
   * The Security Profile element check compares the EAPOL-Key msg 3/4
3029
   * copy against the Beacon/Probe Response frame copy and applies to
3030
   * both MLO and non-MLO associations.
3031
   */
3032
0
  if (wpa_supplicant_validate_security_profile_elem(
3033
0
        sm, ie.security_profile, ie.security_profile_len) < 0)
3034
0
    goto failed;
3035
3036
0
  if (wpa_handle_ext_key_id(sm, &ie))
3037
0
    goto failed;
3038
3039
0
  if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
3040
0
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3041
0
      "WPA: ANonce from message 1 of 4-Way Handshake "
3042
0
      "differs from 3 of 4-Way Handshake - drop packet (src="
3043
0
      MACSTR ")", MAC2STR(sm->bssid));
3044
0
    goto failed;
3045
0
  }
3046
3047
0
  keylen = WPA_GET_BE16(key->key_length);
3048
0
  if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
3049
0
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3050
0
      "WPA: Invalid %s key length %d (src=" MACSTR
3051
0
      ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
3052
0
      MAC2STR(sm->bssid));
3053
0
    goto failed;
3054
0
  }
3055
3056
#ifdef CONFIG_P2P
3057
  if (ie.ip_addr_alloc) {
3058
    os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4);
3059
    wpa_hexdump(MSG_DEBUG, "P2P: IP address info",
3060
          sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr));
3061
  }
3062
#endif /* CONFIG_P2P */
3063
3064
#ifdef CONFIG_OCV
3065
  if (wpa_sm_ocv_enabled(sm)) {
3066
    struct wpa_channel_info ci;
3067
3068
    if (wpa_sm_channel_info(sm, &ci) != 0) {
3069
      wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3070
        "Failed to get channel info to validate received OCI in EAPOL-Key 3/4");
3071
      return;
3072
    }
3073
3074
    if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
3075
           channel_width_to_int(ci.chanwidth),
3076
           ci.seg1_idx) != OCI_SUCCESS) {
3077
      wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE
3078
        "addr=" MACSTR " frame=eapol-key-m3 error=%s",
3079
        MAC2STR(sm->bssid), ocv_errorstr);
3080
      return;
3081
    }
3082
  }
3083
#endif /* CONFIG_OCV */
3084
3085
#ifdef CONFIG_DPP2
3086
  if (DPP_VERSION > 1 && ie.dpp_kde) {
3087
    wpa_printf(MSG_DEBUG,
3088
         "DPP: peer Protocol Version %u Flags 0x%x",
3089
         ie.dpp_kde[0], ie.dpp_kde[1]);
3090
    if (sm->key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_pfs != 2 &&
3091
        (ie.dpp_kde[1] & DPP_KDE_PFS_ALLOWED) && !sm->dpp_z) {
3092
      wpa_printf(MSG_INFO,
3093
           "DPP: Peer indicated it supports PFS and local configuration allows this, but PFS was not negotiated for the association");
3094
      goto failed;
3095
    }
3096
  }
3097
#endif /* CONFIG_DPP2 */
3098
3099
0
  if (sm->use_ext_key_id &&
3100
0
      wpa_supplicant_install_ptk(sm, key, KEY_FLAG_RX))
3101
0
    goto failed;
3102
3103
0
  if (!sm->use_ext_key_id &&
3104
0
      wpa_supplicant_install_ptk(sm, key, KEY_FLAG_RX | KEY_FLAG_NEXT)) {
3105
    /* Continue anyway since the many drivers do not support
3106
     * configuration of the TK for RX-only purposes for cases where
3107
     * multiple keys might be in use in parallel and this being an
3108
     * optional optimization to avoid race condition during TK
3109
     * changes that could result in some protected frames getting
3110
     * discarded. */
3111
0
  }
3112
3113
0
  if (wpa_supplicant_send_4_of_4(sm, wpa_sm_get_auth_addr(sm), key, ver,
3114
0
               key_info, &sm->ptk) < 0)
3115
0
    goto failed;
3116
3117
  /* SNonce was successfully used in msg 3/4, so mark it to be renewed
3118
   * for the next 4-Way Handshake. If msg 3 is received again, the old
3119
   * SNonce will still be used to avoid changing PTK. */
3120
0
  sm->renew_snonce = 1;
3121
3122
0
  if (key_info & WPA_KEY_INFO_INSTALL) {
3123
0
    int res;
3124
3125
0
    if (sm->use_ext_key_id)
3126
0
      res = wpa_supplicant_activate_ptk(sm);
3127
0
    else
3128
0
      res = wpa_supplicant_install_ptk(sm, key,
3129
0
               KEY_FLAG_RX_TX);
3130
0
    if (res)
3131
0
      goto failed;
3132
0
  }
3133
3134
0
  if (key_info & WPA_KEY_INFO_SECURE) {
3135
0
    wpa_sm_mlme_setprotection(
3136
0
      sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
3137
0
      MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
3138
0
    eapol_sm_notify_portValid(sm->eapol, true);
3139
0
  }
3140
0
  wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
3141
3142
0
  if (mlo) {
3143
0
    if (wpa_supplicant_pairwise_mlo_gtk(sm, key, &ie,
3144
0
                key_info) < 0) {
3145
0
      wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3146
0
        "MLO RSN: Failed to configure MLO GTKs");
3147
0
      goto failed;
3148
0
    }
3149
0
  } else if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) {
3150
    /* No GTK to be set to the driver */
3151
0
  } else if (!ie.gtk && sm->proto == WPA_PROTO_RSN) {
3152
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3153
0
      "RSN: No GTK KDE included in EAPOL-Key msg 3/4");
3154
0
    goto failed;
3155
0
  } else if (ie.gtk &&
3156
0
      wpa_supplicant_pairwise_gtk(sm, key,
3157
0
          ie.gtk, ie.gtk_len, key_info) < 0) {
3158
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3159
0
      "RSN: Failed to configure GTK");
3160
0
    goto failed;
3161
0
  }
3162
3163
0
  if ((mlo && mlo_ieee80211w_set_keys(sm, &ie) < 0) ||
3164
0
      (!mlo && ieee80211w_set_keys(sm, &ie) < 0)) {
3165
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3166
0
      "RSN: Failed to configure IGTK");
3167
0
    goto failed;
3168
0
  }
3169
3170
0
  if (mlo || sm->group_cipher == WPA_CIPHER_GTK_NOT_USED || ie.gtk)
3171
0
    wpa_supplicant_key_neg_complete(sm, sm->bssid,
3172
0
            key_info & WPA_KEY_INFO_SECURE);
3173
3174
0
  if (mlo || ie.gtk)
3175
0
    wpa_sm_set_rekey_offload(sm);
3176
3177
  /* Add PMKSA cache entry for Suite B AKMs here since PMKID can be
3178
   * calculated only after KCK has been derived. Though, do not replace an
3179
   * existing PMKSA entry after each 4-way handshake (i.e., new KCK/PMKID)
3180
   * to avoid unnecessary changes of PMKID while continuing to use the
3181
   * same PMK. */
3182
0
  if (sm->proto == WPA_PROTO_RSN && wpa_key_mgmt_suite_b(sm->key_mgmt) &&
3183
0
      !sm->cur_pmksa) {
3184
0
    struct rsn_pmksa_cache_entry *sa;
3185
3186
0
    sa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, NULL,
3187
0
             sm->ptk.kck, sm->ptk.kck_len,
3188
0
             wpa_sm_get_auth_addr(sm), sm->own_addr,
3189
0
             sm->network_ctx, sm->key_mgmt, NULL, 0,
3190
0
             RSN_HASH_NOT_SPECIFIED);
3191
0
    if (!sm->cur_pmksa)
3192
0
      sm->cur_pmksa = sa;
3193
0
  }
3194
3195
0
  if (ie.transition_disable)
3196
0
    wpa_sm_transition_disable(sm, ie.transition_disable[0]);
3197
0
  if (ie.sae_pw_ids && wpa_key_mgmt_sae(sm->key_mgmt) &&
3198
0
      sm->sae_pw_id_change)
3199
0
    wpa_sm_sae_pw_id_change(sm, ie.sae_pw_ids, ie.sae_pw_ids_len);
3200
0
  sm->msg_3_of_4_ok = 1;
3201
0
  return;
3202
3203
0
failed:
3204
0
  wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
3205
0
}
3206
3207
3208
static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
3209
              const struct wpa_eapol_key *key,
3210
              int ver, u16 key_info)
3211
0
{
3212
0
  size_t mic_len, hdrlen, rlen;
3213
0
  struct wpa_eapol_key *reply;
3214
0
  u8 *rbuf, *key_mic;
3215
0
  size_t kde_len = 0;
3216
3217
#ifdef CONFIG_TESTING_OPTIONS
3218
  if (sm->disable_eapol_g2_tx) {
3219
    wpa_printf(MSG_INFO, "TEST: Disable sending EAPOL-Key 2/2");
3220
    return 0;
3221
  }
3222
#endif /* CONFIG_TESTING_OPTIONS */
3223
3224
#ifdef CONFIG_OCV
3225
  if (wpa_sm_ocv_enabled(sm))
3226
    kde_len = OCV_OCI_KDE_LEN;
3227
#endif /* CONFIG_OCV */
3228
3229
0
  mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len, sm->hash_alg,
3230
0
            sm->pasn_group);
3231
0
  hdrlen = sizeof(*reply) + mic_len + 2;
3232
0
  rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
3233
0
          hdrlen + kde_len, &rlen, (void *) &reply);
3234
0
  if (rbuf == NULL)
3235
0
    return -1;
3236
3237
0
  reply->type = (sm->proto == WPA_PROTO_RSN) ?
3238
0
    EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
3239
0
  key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
3240
0
  key_info |= ver | WPA_KEY_INFO_SECURE;
3241
0
  if (mic_len)
3242
0
    key_info |= WPA_KEY_INFO_MIC;
3243
0
  else
3244
0
    key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
3245
0
  WPA_PUT_BE16(reply->key_info, key_info);
3246
0
  if (sm->proto == WPA_PROTO_RSN)
3247
0
    WPA_PUT_BE16(reply->key_length, 0);
3248
0
  else
3249
0
    os_memcpy(reply->key_length, key->key_length, 2);
3250
0
  os_memcpy(reply->replay_counter, key->replay_counter,
3251
0
      WPA_REPLAY_COUNTER_LEN);
3252
3253
0
  key_mic = (u8 *) (reply + 1);
3254
0
  WPA_PUT_BE16(key_mic + mic_len, kde_len); /* Key Data Length */
3255
3256
#ifdef CONFIG_OCV
3257
  if (wpa_sm_ocv_enabled(sm)) {
3258
    struct wpa_channel_info ci;
3259
    u8 *pos;
3260
3261
    if (wpa_sm_channel_info(sm, &ci) != 0) {
3262
      wpa_printf(MSG_WARNING,
3263
           "Failed to get channel info for OCI element in EAPOL-Key 2/2");
3264
      os_free(rbuf);
3265
      return -1;
3266
    }
3267
#ifdef CONFIG_TESTING_OPTIONS
3268
    if (sm->oci_freq_override_eapol_g2) {
3269
      wpa_printf(MSG_INFO,
3270
           "TEST: Override OCI KDE frequency %d -> %d MHz",
3271
           ci.frequency,
3272
           sm->oci_freq_override_eapol_g2);
3273
      ci.frequency = sm->oci_freq_override_eapol_g2;
3274
    }
3275
#endif /* CONFIG_TESTING_OPTIONS */
3276
3277
    pos = key_mic + mic_len + 2; /* Key Data */
3278
    if (ocv_insert_oci_kde(&ci, &pos) < 0) {
3279
      os_free(rbuf);
3280
      return -1;
3281
    }
3282
  }
3283
#endif /* CONFIG_OCV */
3284
3285
0
  wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
3286
0
  return wpa_eapol_key_send(sm, &sm->ptk, ver, wpa_sm_get_auth_addr(sm),
3287
0
          ETH_P_EAPOL, rbuf, rlen, key_mic);
3288
0
}
3289
3290
3291
static void wpa_supplicant_process_mlo_1_of_2(struct wpa_sm *sm,
3292
                const unsigned char *src_addr,
3293
                const struct wpa_eapol_key *key,
3294
                const u8 *key_data,
3295
                size_t key_data_len, u16 ver)
3296
0
{
3297
0
  u16 key_info;
3298
0
  u8 i;
3299
0
  struct wpa_eapol_ie_parse ie;
3300
3301
0
  if (!sm->msg_3_of_4_ok && !wpa_fils_is_completed(sm) &&
3302
0
      !wpa_eppke_is_completed(sm) &&
3303
0
      !wpa_eap_over_auth_frame_is_completed(sm)) {
3304
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3305
0
      "MLO RSN: Group Key Handshake started prior to completion of 4-way handshake");
3306
0
    goto failed;
3307
0
  }
3308
3309
0
  wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "MLO RSN: RX message 1 of Group "
3310
0
    "Key Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr),
3311
0
    ver);
3312
3313
0
  key_info = WPA_GET_BE16(key->key_info);
3314
3315
0
  wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
3316
3317
0
  wpa_hexdump_key(MSG_DEBUG, "MLO RSN: msg 1/2 key data", key_data,
3318
0
      key_data_len);
3319
0
  if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
3320
0
    goto failed;
3321
3322
0
  if (!ie.valid_mlo_gtks) {
3323
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3324
0
      "MLO RSN: No MLO GTK KDE in Group Key msg 1/2");
3325
0
    goto failed;
3326
0
  }
3327
3328
0
  if (!(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
3329
0
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3330
0
      "MLO RSN: MLO GTK KDE in unencrypted key data");
3331
0
    goto failed;
3332
0
  }
3333
3334
#ifdef CONFIG_OCV
3335
  if (wpa_sm_ocv_enabled(sm)) {
3336
    struct wpa_channel_info ci;
3337
3338
    if (wpa_sm_channel_info(sm, &ci) != 0) {
3339
      wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3340
        "Failed to get channel info to validate received OCI in EAPOL-Key group msg 1/2");
3341
      goto failed;
3342
    }
3343
3344
    if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
3345
           channel_width_to_int(ci.chanwidth),
3346
           ci.seg1_idx) != OCI_SUCCESS) {
3347
      wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE
3348
        "addr=" MACSTR " frame=eapol-key-g1 error=%s",
3349
        MAC2STR(sm->bssid), ocv_errorstr);
3350
      goto failed;
3351
    }
3352
  }
3353
#endif /* CONFIG_OCV */
3354
3355
0
  if (mlo_ieee80211w_set_keys(sm, &ie) < 0)
3356
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3357
0
      "MLO RSN: Failed to configure MLO IGTK");
3358
3359
0
  for_each_link(sm->mlo.valid_links, i) {
3360
    /*
3361
     * AP may send group keys for subset of the all links during
3362
     * rekey
3363
     */
3364
0
    if (!ie.mlo_gtk[i])
3365
0
      continue;
3366
3367
0
    if (wpa_supplicant_mlo_gtk(sm, i, ie.mlo_gtk[i],
3368
0
             ie.mlo_gtk_len[i], key_info))
3369
0
      goto failed;
3370
0
  }
3371
3372
0
  if (wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
3373
0
    goto failed;
3374
3375
0
  wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "MLO RSN: Group rekeying completed "
3376
0
    "with " MACSTR " [GTK=%s]", MAC2STR(sm->mlo.ap_mld_addr),
3377
0
    wpa_cipher_txt(sm->group_cipher));
3378
0
  wpa_sm_cancel_auth_timeout(sm);
3379
0
  wpa_sm_set_state(sm, WPA_COMPLETED);
3380
3381
0
  wpa_sm_set_rekey_offload(sm);
3382
3383
0
  return;
3384
3385
0
failed:
3386
0
  wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
3387
0
}
3388
3389
3390
static void wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
3391
                const unsigned char *src_addr,
3392
                const struct wpa_eapol_key *key,
3393
                const u8 *key_data,
3394
                size_t key_data_len, u16 ver)
3395
0
{
3396
0
  u16 key_info;
3397
0
  int rekey;
3398
0
  struct wpa_gtk_data gd;
3399
0
  const u8 *key_rsc;
3400
0
  size_t maxkeylen;
3401
0
  u16 gtk_len;
3402
3403
0
  if (!sm->msg_3_of_4_ok) {
3404
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3405
0
      "WPA: Group Key Handshake started prior to completion of 4-way handshake");
3406
0
    goto failed;
3407
0
  }
3408
3409
0
  os_memset(&gd, 0, sizeof(gd));
3410
3411
0
  rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
3412
0
  wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3413
0
    "WPA: RX message 1 of Group Key Handshake from " MACSTR
3414
0
    " (ver=%d)", MAC2STR(src_addr), ver);
3415
3416
0
  key_info = WPA_GET_BE16(key->key_info);
3417
3418
0
  gtk_len = WPA_GET_BE16(key->key_length);
3419
0
  maxkeylen = key_data_len;
3420
0
  if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
3421
0
    if (maxkeylen < 8) {
3422
0
      wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3423
0
        "WPA: Too short maxkeylen (%lu)",
3424
0
        (unsigned long) maxkeylen);
3425
0
      goto failed;
3426
0
    }
3427
0
    maxkeylen -= 8;
3428
0
  }
3429
3430
0
  if (gtk_len > maxkeylen ||
3431
0
      wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
3432
0
                gtk_len, maxkeylen,
3433
0
                &gd.key_rsc_len, &gd.alg))
3434
0
    goto failed;
3435
3436
0
  wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
3437
3438
0
  gd.gtk_len = gtk_len;
3439
0
  gd.keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
3440
0
    WPA_KEY_INFO_KEY_INDEX_SHIFT;
3441
0
  if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
3442
#if defined(CONFIG_NO_RC4) || defined(CONFIG_FIPS)
3443
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3444
      "WPA: RC4 not supported in the build");
3445
    goto failed;
3446
#else /* CONFIG_NO_RC4 || CONFIG_FIPS */
3447
0
    u8 ek[32];
3448
0
    if (key_data_len > sizeof(gd.gtk)) {
3449
0
      wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3450
0
        "WPA: RC4 key data too long (%lu)",
3451
0
        (unsigned long) key_data_len);
3452
0
      goto failed;
3453
0
    }
3454
0
    os_memcpy(ek, key->key_iv, 16);
3455
0
    os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
3456
0
    os_memcpy(gd.gtk, key_data, key_data_len);
3457
0
    if (rc4_skip(ek, 32, 256, gd.gtk, key_data_len)) {
3458
0
      forced_memzero(ek, sizeof(ek));
3459
0
      wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
3460
0
        "WPA: RC4 failed");
3461
0
      goto failed;
3462
0
    }
3463
0
    forced_memzero(ek, sizeof(ek));
3464
0
#endif /* CONFIG_NO_RC4 || CONFIG_FIPS */
3465
0
  } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
3466
0
    if (maxkeylen % 8) {
3467
0
      wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3468
0
        "WPA: Unsupported AES-WRAP len %lu",
3469
0
        (unsigned long) maxkeylen);
3470
0
      goto failed;
3471
0
    }
3472
0
    if (maxkeylen > sizeof(gd.gtk)) {
3473
0
      wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3474
0
        "WPA: AES-WRAP key data "
3475
0
        "too long (keydatalen=%lu maxkeylen=%lu)",
3476
0
        (unsigned long) key_data_len,
3477
0
        (unsigned long) maxkeylen);
3478
0
      goto failed;
3479
0
    }
3480
0
    if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, maxkeylen / 8,
3481
0
             key_data, gd.gtk)) {
3482
0
      wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3483
0
        "WPA: AES unwrap failed - could not decrypt "
3484
0
        "GTK");
3485
0
      goto failed;
3486
0
    }
3487
0
  } else {
3488
0
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3489
0
      "WPA: Unsupported key_info type %d", ver);
3490
0
    goto failed;
3491
0
  }
3492
0
  gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
3493
0
    sm, !!(key_info & WPA_KEY_INFO_TXRX));
3494
3495
0
  key_rsc = key->key_rsc;
3496
0
  if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
3497
0
    key_rsc = null_rsc;
3498
3499
0
  if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0) ||
3500
0
      wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
3501
0
    goto failed;
3502
0
  forced_memzero(&gd, sizeof(gd));
3503
3504
0
  if (rekey) {
3505
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3506
0
      "WPA: Group rekeying completed with " MACSTR
3507
0
      " [GTK=%s]",
3508
0
      MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
3509
0
    wpa_sm_cancel_auth_timeout(sm);
3510
0
    wpa_sm_set_state(sm, WPA_COMPLETED);
3511
0
  } else {
3512
0
    wpa_supplicant_key_neg_complete(sm, sm->bssid,
3513
0
            key_info & WPA_KEY_INFO_SECURE);
3514
0
  }
3515
3516
0
  wpa_sm_set_rekey_offload(sm);
3517
3518
0
  return;
3519
3520
0
failed:
3521
0
  forced_memzero(&gd, sizeof(gd));
3522
0
  wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
3523
0
}
3524
3525
3526
static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
3527
            const unsigned char *src_addr,
3528
            const struct wpa_eapol_key *key,
3529
            const u8 *key_data,
3530
            size_t key_data_len, u16 ver)
3531
0
{
3532
0
  u16 key_info;
3533
0
  struct wpa_gtk_data gd;
3534
0
  const u8 *key_rsc;
3535
0
  int maxkeylen;
3536
0
  struct wpa_eapol_ie_parse ie;
3537
0
  u16 gtk_len;
3538
3539
0
  if (!sm->msg_3_of_4_ok && !wpa_fils_is_completed(sm) &&
3540
0
      !wpa_eppke_is_completed(sm) &&
3541
0
      !wpa_eap_over_auth_frame_is_completed(sm)) {
3542
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3543
0
      "RSN: Group Key Handshake started prior to completion of 4-way handshake");
3544
0
    goto failed;
3545
0
  }
3546
3547
0
  os_memset(&gd, 0, sizeof(gd));
3548
3549
0
  wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3550
0
    "RSN: RX message 1 of Group Key Handshake from " MACSTR
3551
0
    " (ver=%d)", MAC2STR(src_addr), ver);
3552
3553
0
  key_info = WPA_GET_BE16(key->key_info);
3554
3555
0
  wpa_hexdump_key(MSG_DEBUG, "RSN: msg 1/2 key data",
3556
0
      key_data, key_data_len);
3557
0
  if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
3558
0
    goto failed;
3559
3560
0
  wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
3561
3562
0
  if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
3563
0
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3564
0
      "RSN: GTK KDE in unencrypted key data");
3565
0
    goto failed;
3566
0
  }
3567
0
  if (!ie.gtk) {
3568
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3569
0
      "RSN: No GTK KDE in Group Key msg 1/2");
3570
0
    goto failed;
3571
0
  }
3572
0
  gtk_len = ie.gtk_len;
3573
0
  if (wpa_supplicant_validate_gtk_kde_len(gtk_len) < 0) {
3574
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3575
0
      "RSN: Invalid GTK KDE length (%u) in Group Key msg 1/2",
3576
0
      gtk_len);
3577
0
    goto failed;
3578
0
  }
3579
0
  gtk_len -= 2;
3580
0
  maxkeylen = gd.gtk_len = gtk_len;
3581
3582
#ifdef CONFIG_OCV
3583
  if (wpa_sm_ocv_enabled(sm)) {
3584
    struct wpa_channel_info ci;
3585
3586
    if (wpa_sm_channel_info(sm, &ci) != 0) {
3587
      wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3588
        "Failed to get channel info to validate received OCI in EAPOL-Key group msg 1/2");
3589
      goto failed;
3590
    }
3591
3592
    if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
3593
           channel_width_to_int(ci.chanwidth),
3594
           ci.seg1_idx) != OCI_SUCCESS) {
3595
      wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE
3596
        "addr=" MACSTR " frame=eapol-key-g1 error=%s",
3597
        MAC2STR(sm->bssid), ocv_errorstr);
3598
      goto failed;
3599
    }
3600
  }
3601
#endif /* CONFIG_OCV */
3602
3603
0
  if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
3604
0
                gtk_len, maxkeylen,
3605
0
                &gd.key_rsc_len, &gd.alg))
3606
0
    goto failed;
3607
3608
0
  wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in group key handshake",
3609
0
      ie.gtk, 2 + gtk_len);
3610
0
  gd.keyidx = ie.gtk[0] & 0x3;
3611
0
  gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
3612
0
                  !!(ie.gtk[0] & BIT(2)));
3613
0
  os_memcpy(gd.gtk, ie.gtk + 2, gtk_len);
3614
3615
0
  if (ieee80211w_set_keys(sm, &ie) < 0)
3616
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3617
0
      "RSN: Failed to configure IGTK");
3618
3619
0
  key_rsc = key->key_rsc;
3620
0
  if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
3621
0
    key_rsc = null_rsc;
3622
3623
0
  if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0) ||
3624
0
      wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
3625
0
    goto failed;
3626
0
  forced_memzero(&gd, sizeof(gd));
3627
3628
0
  wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3629
0
    "RSN: Group rekeying completed with " MACSTR " [GTK=%s]",
3630
0
    MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
3631
0
  wpa_sm_cancel_auth_timeout(sm);
3632
0
  wpa_sm_set_state(sm, WPA_COMPLETED);
3633
3634
0
  wpa_sm_set_rekey_offload(sm);
3635
3636
0
  return;
3637
3638
0
failed:
3639
0
  forced_memzero(&gd, sizeof(gd));
3640
0
  wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
3641
0
}
3642
3643
3644
static void wpa_sm_tptk_to_ptk(struct wpa_sm *sm)
3645
0
{
3646
0
  sm->tptk_set = 0;
3647
0
  sm->ptk_set = 1;
3648
0
  os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
3649
0
  os_memset(&sm->tptk, 0, sizeof(sm->tptk));
3650
0
  sm->hash_alg = sm->ptk.hash_alg;
3651
3652
0
  if (wpa_sm_pmf_enabled(sm)) {
3653
0
    os_memcpy(sm->last_kck, sm->ptk.kck, sm->ptk.kck_len);
3654
0
    sm->last_kck_len = sm->ptk.kck_len;
3655
0
    sm->last_kck_pmk_len = sm->pmk_len;
3656
0
    sm->last_kck_key_mgmt = sm->key_mgmt;
3657
0
    sm->last_kck_eapol_key_ver = sm->last_eapol_key_ver;
3658
0
    os_memcpy(sm->last_kck_aa, wpa_sm_get_auth_addr(sm), ETH_ALEN);
3659
0
  } else {
3660
0
    os_memset(sm->last_kck, 0, sizeof(sm->last_kck));
3661
0
    sm->last_kck_len = 0;
3662
0
    os_memset(sm->last_kck_aa, 0, ETH_ALEN);
3663
0
  }
3664
0
}
3665
3666
3667
static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
3668
                 struct wpa_eapol_key *key,
3669
                 u16 ver,
3670
                 const u8 *buf, size_t len)
3671
0
{
3672
0
  u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
3673
0
  int ok = 0;
3674
0
  size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len, sm->hash_alg,
3675
0
             sm->pasn_group);
3676
3677
0
  os_memcpy(mic, key + 1, mic_len);
3678
0
  if (sm->tptk_set) {
3679
0
    os_memset(key + 1, 0, mic_len);
3680
0
    if (wpa_eapol_key_mic(sm->tptk.kck, sm->tptk.kck_len,
3681
0
              sm->key_mgmt, sm->hash_alg,
3682
0
              ver, buf, len, (u8 *) (key + 1),
3683
0
              sm->pasn_group) < 0 ||
3684
0
        os_memcmp_const(mic, key + 1, mic_len) != 0) {
3685
0
      wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3686
0
        "WPA: Invalid EAPOL-Key MIC "
3687
0
        "when using TPTK - ignoring TPTK");
3688
0
#ifdef TEST_FUZZ
3689
0
      wpa_printf(MSG_INFO,
3690
0
           "TEST: Ignore Key MIC failure for fuzz testing");
3691
0
      goto continue_fuzz;
3692
0
#endif /* TEST_FUZZ */
3693
0
    } else {
3694
0
#ifdef TEST_FUZZ
3695
0
    continue_fuzz:
3696
0
#endif /* TEST_FUZZ */
3697
0
      ok = 1;
3698
0
      wpa_sm_tptk_to_ptk(sm);
3699
      /*
3700
       * This assures the same TPTK in sm->tptk can never be
3701
       * copied twice to sm->ptk as the new PTK. In
3702
       * combination with the installed flag in the wpa_ptk
3703
       * struct, this assures the same PTK is only installed
3704
       * once.
3705
       */
3706
0
      sm->renew_snonce = 1;
3707
0
    }
3708
0
  }
3709
3710
0
  if (!ok && sm->ptk_set) {
3711
0
    os_memset(key + 1, 0, mic_len);
3712
0
    if (wpa_eapol_key_mic(sm->ptk.kck, sm->ptk.kck_len,
3713
0
              sm->key_mgmt, sm->hash_alg,
3714
0
              ver, buf, len, (u8 *) (key + 1),
3715
0
              sm->pasn_group) < 0 ||
3716
0
        os_memcmp_const(mic, key + 1, mic_len) != 0) {
3717
0
      wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3718
0
        "WPA: Invalid EAPOL-Key MIC - "
3719
0
        "dropping packet");
3720
0
#ifdef TEST_FUZZ
3721
0
      wpa_printf(MSG_INFO,
3722
0
           "TEST: Ignore Key MIC failure for fuzz testing");
3723
0
      goto continue_fuzz2;
3724
0
#endif /* TEST_FUZZ */
3725
0
      return -1;
3726
0
    }
3727
0
#ifdef TEST_FUZZ
3728
0
  continue_fuzz2:
3729
0
#endif /* TEST_FUZZ */
3730
0
    ok = 1;
3731
0
  }
3732
3733
0
  if (!ok) {
3734
0
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3735
0
      "WPA: Could not verify EAPOL-Key MIC - "
3736
0
      "dropping packet");
3737
0
    return -1;
3738
0
  }
3739
3740
0
  os_memcpy(sm->rx_replay_counter, key->replay_counter,
3741
0
      WPA_REPLAY_COUNTER_LEN);
3742
0
  sm->rx_replay_counter_set = 1;
3743
0
  return 0;
3744
0
}
3745
3746
3747
/* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
3748
static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
3749
             struct wpa_eapol_key *key,
3750
             size_t mic_len, u16 ver,
3751
             u8 *key_data, size_t *key_data_len)
3752
0
{
3753
0
  wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
3754
0
        key_data, *key_data_len);
3755
0
  if (!sm->ptk_set) {
3756
0
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3757
0
      "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
3758
0
      "Data");
3759
0
    return -1;
3760
0
  }
3761
3762
  /* Decrypt key data here so that this operation does not need
3763
   * to be implemented separately for each message type. */
3764
0
  if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
3765
#if defined(CONFIG_NO_RC4) || defined(CONFIG_FIPS)
3766
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3767
      "WPA: RC4 not supported in the build");
3768
    return -1;
3769
#else /* CONFIG_NO_RC4 || CONFIG_FIPS */
3770
0
    u8 ek[32];
3771
3772
0
    wpa_printf(MSG_DEBUG, "WPA: Decrypt Key Data using RC4");
3773
0
    os_memcpy(ek, key->key_iv, 16);
3774
0
    os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
3775
0
    if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
3776
0
      forced_memzero(ek, sizeof(ek));
3777
0
      wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
3778
0
        "WPA: RC4 failed");
3779
0
      return -1;
3780
0
    }
3781
0
    forced_memzero(ek, sizeof(ek));
3782
0
#endif /* CONFIG_NO_RC4 || CONFIG_FIPS */
3783
0
  } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
3784
0
       ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
3785
0
       wpa_use_aes_key_wrap(sm->key_mgmt)) {
3786
0
    u8 *buf;
3787
3788
0
    wpa_printf(MSG_DEBUG,
3789
0
         "WPA: Decrypt Key Data using AES-UNWRAP (KEK length %u)",
3790
0
         (unsigned int) sm->ptk.kek_len);
3791
0
    if (*key_data_len < 8 || *key_data_len % 8) {
3792
0
      wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3793
0
        "WPA: Unsupported AES-WRAP len %u",
3794
0
        (unsigned int) *key_data_len);
3795
0
      return -1;
3796
0
    }
3797
0
    *key_data_len -= 8; /* AES-WRAP adds 8 bytes */
3798
0
    buf = os_malloc(*key_data_len);
3799
0
    if (buf == NULL) {
3800
0
      wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3801
0
        "WPA: No memory for AES-UNWRAP buffer");
3802
0
      return -1;
3803
0
    }
3804
0
#ifdef TEST_FUZZ
3805
0
    os_memset(buf, 0x11, *key_data_len);
3806
0
#endif /* TEST_FUZZ */
3807
0
    if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, *key_data_len / 8,
3808
0
             key_data, buf)) {
3809
0
#ifdef TEST_FUZZ
3810
0
      wpa_printf(MSG_INFO,
3811
0
           "TEST: Ignore AES unwrap failure for fuzz testing");
3812
0
      goto continue_fuzz;
3813
0
#endif /* TEST_FUZZ */
3814
0
      bin_clear_free(buf, *key_data_len);
3815
0
      wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3816
0
        "WPA: AES unwrap failed - "
3817
0
        "could not decrypt EAPOL-Key key data");
3818
0
      return -1;
3819
0
    }
3820
0
#ifdef TEST_FUZZ
3821
0
  continue_fuzz:
3822
0
#endif /* TEST_FUZZ */
3823
0
    os_memcpy(key_data, buf, *key_data_len);
3824
0
    bin_clear_free(buf, *key_data_len);
3825
0
    WPA_PUT_BE16(((u8 *) (key + 1)) + mic_len, *key_data_len);
3826
0
  } else {
3827
0
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3828
0
      "WPA: Unsupported key_info type %d", ver);
3829
0
    return -1;
3830
0
  }
3831
0
  wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
3832
0
      key_data, *key_data_len);
3833
0
  return 0;
3834
0
}
3835
3836
3837
/**
3838
 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
3839
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3840
 */
3841
void wpa_sm_aborted_cached(struct wpa_sm *sm)
3842
0
{
3843
0
  if (sm && sm->cur_pmksa) {
3844
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3845
0
      "RSN: Cancelling PMKSA caching attempt");
3846
0
    sm->cur_pmksa = NULL;
3847
0
  }
3848
0
}
3849
3850
3851
void wpa_sm_aborted_external_cached(struct wpa_sm *sm)
3852
0
{
3853
0
  if (sm && sm->cur_pmksa && sm->cur_pmksa->external) {
3854
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3855
0
      "RSN: Cancelling external PMKSA caching attempt");
3856
0
    sm->cur_pmksa = NULL;
3857
0
  }
3858
0
}
3859
3860
3861
static void wpa_eapol_key_dump(struct wpa_sm *sm,
3862
             const struct wpa_eapol_key *key,
3863
             unsigned int key_data_len,
3864
             const u8 *mic, unsigned int mic_len)
3865
0
{
3866
0
#ifndef CONFIG_NO_STDOUT_DEBUG
3867
0
  u16 key_info = WPA_GET_BE16(key->key_info);
3868
3869
0
  wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "  EAPOL-Key type=%d", key->type);
3870
0
  wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3871
0
    "  key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
3872
0
    key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
3873
0
    (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
3874
0
    WPA_KEY_INFO_KEY_INDEX_SHIFT,
3875
0
    (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
3876
0
    key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
3877
0
    key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
3878
0
    key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
3879
0
    key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
3880
0
    key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
3881
0
    key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
3882
0
    key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
3883
0
    key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
3884
0
  wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3885
0
    "  key_length=%u key_data_length=%u",
3886
0
    WPA_GET_BE16(key->key_length), key_data_len);
3887
0
  wpa_hexdump(MSG_DEBUG, "  replay_counter",
3888
0
        key->replay_counter, WPA_REPLAY_COUNTER_LEN);
3889
0
  wpa_hexdump(MSG_DEBUG, "  key_nonce", key->key_nonce, WPA_NONCE_LEN);
3890
0
  wpa_hexdump(MSG_DEBUG, "  key_iv", key->key_iv, 16);
3891
0
  wpa_hexdump(MSG_DEBUG, "  key_rsc", key->key_rsc, 8);
3892
0
  wpa_hexdump(MSG_DEBUG, "  key_id (reserved)", key->key_id, 8);
3893
0
  wpa_hexdump(MSG_DEBUG, "  key_mic", mic, mic_len);
3894
0
#endif /* CONFIG_NO_STDOUT_DEBUG */
3895
0
}
3896
3897
3898
#ifdef CONFIG_FILS
3899
static int wpa_supp_aead_decrypt(struct wpa_sm *sm, u8 *buf, size_t buf_len,
3900
         size_t *key_data_len)
3901
{
3902
  struct wpa_ptk *ptk;
3903
  struct ieee802_1x_hdr *hdr;
3904
  struct wpa_eapol_key *key;
3905
  u8 *pos, *tmp;
3906
  const u8 *aad[1];
3907
  size_t aad_len[1];
3908
3909
  if (*key_data_len < AES_BLOCK_SIZE) {
3910
    wpa_printf(MSG_INFO, "No room for AES-SIV data in the frame");
3911
    return -1;
3912
  }
3913
3914
  if (sm->tptk_set)
3915
    ptk = &sm->tptk;
3916
  else if (sm->ptk_set)
3917
    ptk = &sm->ptk;
3918
  else
3919
    return -1;
3920
3921
  hdr = (struct ieee802_1x_hdr *) buf;
3922
  key = (struct wpa_eapol_key *) (hdr + 1);
3923
  pos = (u8 *) (key + 1);
3924
  pos += 2; /* Pointing at the Encrypted Key Data field */
3925
3926
  tmp = os_malloc(*key_data_len);
3927
  if (!tmp)
3928
    return -1;
3929
3930
  /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
3931
   * to Key Data (exclusive). */
3932
  aad[0] = buf;
3933
  aad_len[0] = pos - buf;
3934
  if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, *key_data_len,
3935
          1, aad, aad_len, tmp) < 0) {
3936
    wpa_printf(MSG_INFO, "Invalid AES-SIV data in the frame");
3937
    bin_clear_free(tmp, *key_data_len);
3938
    return -1;
3939
  }
3940
3941
  /* AEAD decryption and validation completed successfully */
3942
  (*key_data_len) -= AES_BLOCK_SIZE;
3943
  wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data",
3944
      tmp, *key_data_len);
3945
3946
  /* Replace Key Data field with the decrypted version */
3947
  os_memcpy(pos, tmp, *key_data_len);
3948
  pos -= 2; /* Key Data Length field */
3949
  WPA_PUT_BE16(pos, *key_data_len);
3950
  bin_clear_free(tmp, *key_data_len);
3951
3952
  if (sm->tptk_set)
3953
    wpa_sm_tptk_to_ptk(sm);
3954
3955
  os_memcpy(sm->rx_replay_counter, key->replay_counter,
3956
      WPA_REPLAY_COUNTER_LEN);
3957
  sm->rx_replay_counter_set = 1;
3958
3959
  return 0;
3960
}
3961
#endif /* CONFIG_FILS */
3962
3963
3964
static int wpa_sm_rx_eapol_wpa(struct wpa_sm *sm, const u8 *src_addr,
3965
             struct wpa_eapol_key *key,
3966
             enum frame_encryption encrypted,
3967
             const u8 *tmp, size_t data_len,
3968
             u8 *key_data, size_t key_data_len)
3969
0
{
3970
0
  u16 key_info, ver;
3971
3972
0
  key_info = WPA_GET_BE16(key->key_info);
3973
3974
0
  if (key->type != EAPOL_KEY_TYPE_WPA) {
3975
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3976
0
      "WPA: Unsupported EAPOL-Key type %d", key->type);
3977
0
    return -1;
3978
0
  }
3979
3980
0
  ver = key_info & WPA_KEY_INFO_TYPE_MASK;
3981
0
  if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
3982
0
      ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
3983
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3984
0
      "WPA: Unsupported EAPOL-Key descriptor version %d",
3985
0
      ver);
3986
0
    return -1;
3987
0
  }
3988
3989
0
  if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
3990
0
       ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
3991
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3992
0
      "WPA: CCMP is used, but EAPOL-Key descriptor version (%d) is not 2",
3993
0
      ver);
3994
0
    if (sm->group_cipher != WPA_CIPHER_CCMP &&
3995
0
        !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
3996
      /* Earlier versions of IEEE 802.11i did not explicitly
3997
       * require version 2 descriptor for all EAPOL-Key
3998
       * packets, so allow group keys to use version 1 if
3999
       * CCMP is not used for them. */
4000
0
      wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
4001
0
        "WPA: Backwards compatibility: allow invalid version for non-CCMP group keys");
4002
0
    } else
4003
0
      return -1;
4004
0
  }
4005
4006
0
  if ((key_info & WPA_KEY_INFO_MIC) &&
4007
0
      wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
4008
0
    return -1;
4009
4010
0
  if (key_info & WPA_KEY_INFO_KEY_TYPE) {
4011
0
    if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
4012
0
      wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
4013
0
        "WPA: Ignored EAPOL-Key (Pairwise) with non-zero key index");
4014
0
      return -1;
4015
0
    }
4016
0
    if (key_info & (WPA_KEY_INFO_MIC |
4017
0
        WPA_KEY_INFO_ENCR_KEY_DATA)) {
4018
      /* 3/4 4-Way Handshake */
4019
0
      wpa_supplicant_process_3_of_4_wpa(sm, key, ver,
4020
0
                key_data,
4021
0
                key_data_len);
4022
0
    } else {
4023
      /* 1/4 4-Way Handshake */
4024
0
      wpa_supplicant_process_1_of_4_wpa(sm, src_addr, key,
4025
0
                ver, key_data,
4026
0
                key_data_len,
4027
0
                encrypted);
4028
0
    }
4029
0
  } else {
4030
0
    if (key_info & WPA_KEY_INFO_MIC) {
4031
      /* 1/2 Group Key Handshake */
4032
0
      wpa_supplicant_process_1_of_2_wpa(sm, src_addr, key,
4033
0
                key_data,
4034
0
                key_data_len,
4035
0
                ver);
4036
0
    } else {
4037
0
      wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
4038
0
        "WPA: EAPOL-Key (Group) without Mic/Encr bit - dropped");
4039
0
    }
4040
0
  }
4041
4042
0
  return 1;
4043
0
}
4044
4045
4046
/**
4047
 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
4048
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4049
 * @src_addr: Source MAC address of the EAPOL packet
4050
 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
4051
 * @len: Length of the EAPOL frame
4052
 * @encrypted: Whether the frame was encrypted
4053
 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure,
4054
 *      -2 = reply counter did not increase.
4055
 *
4056
 * This function is called for each received EAPOL frame. Other than EAPOL-Key
4057
 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
4058
 * only processing WPA and WPA2 EAPOL-Key frames.
4059
 *
4060
 * The received EAPOL-Key packets are validated and valid packets are replied
4061
 * to. In addition, key material (PTK, GTK) is configured at the end of a
4062
 * successful key handshake.
4063
 */
4064
int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
4065
        const u8 *buf, size_t len, enum frame_encryption encrypted)
4066
0
{
4067
0
  size_t plen, data_len, key_data_len;
4068
0
  const struct ieee802_1x_hdr *hdr;
4069
0
  struct wpa_eapol_key *key;
4070
0
  u16 key_info, ver;
4071
0
  u8 *tmp = NULL;
4072
0
  int ret = -1;
4073
0
  u8 *mic, *key_data;
4074
0
  size_t mic_len, keyhdrlen, pmk_len;
4075
4076
0
#ifdef CONFIG_IEEE80211R
4077
0
  sm->ft_completed = 0;
4078
0
#endif /* CONFIG_IEEE80211R */
4079
4080
0
  pmk_len = sm->pmk_len;
4081
0
  if (!pmk_len && sm->cur_pmksa)
4082
0
    pmk_len = sm->cur_pmksa->pmk_len;
4083
0
  mic_len = wpa_mic_len(sm->key_mgmt, pmk_len, sm->hash_alg,
4084
0
            sm->pasn_group);
4085
0
  keyhdrlen = sizeof(*key) + mic_len + 2;
4086
4087
0
  if (len < sizeof(*hdr) + keyhdrlen) {
4088
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
4089
0
      "WPA: EAPOL frame too short to be a WPA "
4090
0
      "EAPOL-Key (len %lu, expecting at least %lu)",
4091
0
      (unsigned long) len,
4092
0
      (unsigned long) sizeof(*hdr) + keyhdrlen);
4093
0
    return 0;
4094
0
  }
4095
4096
0
  hdr = (const struct ieee802_1x_hdr *) buf;
4097
0
  plen = be_to_host16(hdr->length);
4098
0
  data_len = plen + sizeof(*hdr);
4099
0
  wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
4100
0
    "IEEE 802.1X RX: version=%d type=%d length=%lu",
4101
0
    hdr->version, hdr->type, (unsigned long) plen);
4102
4103
0
  if (hdr->version < EAPOL_VERSION) {
4104
    /* TODO: backwards compatibility */
4105
0
  }
4106
0
  if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
4107
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
4108
0
      "WPA: EAPOL frame (type %u) discarded, "
4109
0
      "not a Key frame", hdr->type);
4110
0
    ret = 0;
4111
0
    goto out;
4112
0
  }
4113
0
  wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len);
4114
0
  if (plen > len - sizeof(*hdr) || plen < keyhdrlen) {
4115
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
4116
0
      "WPA: EAPOL frame payload size %lu "
4117
0
      "invalid (frame size %lu)",
4118
0
      (unsigned long) plen, (unsigned long) len);
4119
0
    ret = 0;
4120
0
    goto out;
4121
0
  }
4122
0
  if (data_len < len) {
4123
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
4124
0
      "WPA: ignoring %lu bytes after the IEEE 802.1X data",
4125
0
      (unsigned long) len - data_len);
4126
0
  }
4127
4128
  /*
4129
   * Make a copy of the frame since we need to modify the buffer during
4130
   * MAC validation and Key Data decryption.
4131
   */
4132
0
  tmp = os_memdup(buf, data_len);
4133
0
  if (tmp == NULL)
4134
0
    goto out;
4135
0
  key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr));
4136
0
  mic = (u8 *) (key + 1);
4137
0
  key_data = mic + mic_len + 2;
4138
4139
0
  if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
4140
0
  {
4141
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
4142
0
      "WPA: EAPOL-Key type (%d) unknown, discarded",
4143
0
      key->type);
4144
0
    ret = 0;
4145
0
    goto out;
4146
0
  }
4147
4148
0
  key_data_len = WPA_GET_BE16(mic + mic_len);
4149
0
  wpa_eapol_key_dump(sm, key, key_data_len, mic, mic_len);
4150
4151
0
  if (key_data_len > plen - keyhdrlen) {
4152
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
4153
0
      "frame - key_data overflow (%u > %u)",
4154
0
      (unsigned int) key_data_len,
4155
0
      (unsigned int) (plen - keyhdrlen));
4156
0
    goto out;
4157
0
  }
4158
4159
0
  if (sm->rx_replay_counter_set &&
4160
0
      os_memcmp(key->replay_counter, sm->rx_replay_counter,
4161
0
          WPA_REPLAY_COUNTER_LEN) <= 0) {
4162
0
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
4163
0
      "WPA: EAPOL-Key Replay Counter did not increase - dropping packet");
4164
0
    ret = -2;
4165
0
    goto out;
4166
0
  }
4167
4168
0
  eapol_sm_notify_lower_layer_success(sm->eapol, 0);
4169
4170
0
  key_info = WPA_GET_BE16(key->key_info);
4171
4172
0
  if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
4173
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
4174
0
      "WPA: Unsupported SMK bit in key_info");
4175
0
    goto out;
4176
0
  }
4177
4178
0
  if (!(key_info & WPA_KEY_INFO_ACK)) {
4179
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
4180
0
      "WPA: No Ack bit in key_info");
4181
0
    goto out;
4182
0
  }
4183
4184
0
  if (key_info & WPA_KEY_INFO_REQUEST) {
4185
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
4186
0
      "WPA: EAPOL-Key with Request bit - dropped");
4187
0
    goto out;
4188
0
  }
4189
4190
0
  if (sm->proto == WPA_PROTO_WPA) {
4191
0
    ret = wpa_sm_rx_eapol_wpa(sm, src_addr, key, encrypted,
4192
0
            tmp, data_len,
4193
0
            key_data, key_data_len);
4194
0
    goto out;
4195
0
  }
4196
4197
0
  if (key->type != EAPOL_KEY_TYPE_RSN) {
4198
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
4199
0
      "RSN: Unsupported EAPOL-Key type %d", key->type);
4200
0
    goto out;
4201
0
  }
4202
4203
0
  ver = key_info & WPA_KEY_INFO_TYPE_MASK;
4204
0
  if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
4205
0
      ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
4206
0
      ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
4207
0
      !wpa_use_akm_defined(sm->key_mgmt)) {
4208
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
4209
0
      "RSN: Unsupported EAPOL-Key descriptor version %d",
4210
0
      ver);
4211
0
    goto out;
4212
0
  }
4213
4214
0
  if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
4215
0
      sm->pairwise_cipher != WPA_CIPHER_TKIP) {
4216
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
4217
0
      "RSN: EAPOL-Key descriptor version %d not allowed without TKIP as the pairwise cipher",
4218
0
      ver);
4219
0
    goto out;
4220
0
  }
4221
4222
0
  if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
4223
0
      (sm->key_mgmt != WPA_KEY_MGMT_IEEE8021X &&
4224
0
       sm->key_mgmt != WPA_KEY_MGMT_PSK &&
4225
0
       sm->key_mgmt != WPA_KEY_MGMT_EPPKE)) {
4226
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
4227
0
      "RSN: EAPOL-Key descriptor version %d not allowed due to negotiated AKM (0x%x)",
4228
0
      ver, sm->key_mgmt);
4229
0
    goto out;
4230
0
  }
4231
4232
0
  if (wpa_use_akm_defined(sm->key_mgmt) &&
4233
0
      ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
4234
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
4235
0
      "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
4236
0
      ver);
4237
0
    goto out;
4238
0
  }
4239
4240
0
#ifdef CONFIG_IEEE80211R
4241
0
  if (wpa_key_mgmt_ft(sm->key_mgmt)) {
4242
    /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
4243
0
    if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
4244
0
        !wpa_use_akm_defined(sm->key_mgmt)) {
4245
0
      wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
4246
0
        "FT: AP did not use AES-128-CMAC");
4247
0
      goto out;
4248
0
    }
4249
0
  } else
4250
0
#endif /* CONFIG_IEEE80211R */
4251
0
  if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
4252
0
    if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
4253
0
        !wpa_use_akm_defined(sm->key_mgmt)) {
4254
0
      wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
4255
0
        "RSN: AP did not use the negotiated AES-128-CMAC");
4256
0
      goto out;
4257
0
    }
4258
0
  } else if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
4259
0
       !wpa_use_akm_defined(sm->key_mgmt) &&
4260
0
       ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
4261
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
4262
0
      "RSN: CCMP is used, but EAPOL-Key descriptor version (%d) is not 2", ver);
4263
0
    if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
4264
0
      wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
4265
0
        "RSN: Interoperability workaround: allow incorrect (should have been HMAC-SHA1), but stronger (is AES-128-CMAC), descriptor version to be used");
4266
0
    } else {
4267
0
      wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
4268
0
        "RSN: Unexpected descriptor version %u", ver);
4269
0
      goto out;
4270
0
    }
4271
0
  } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
4272
0
       !wpa_use_akm_defined(sm->key_mgmt) &&
4273
0
       ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
4274
0
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
4275
0
      "RSN: GCMP is used, but EAPOL-Key descriptor version (%d) is not 2",
4276
0
      ver);
4277
0
    goto out;
4278
0
  }
4279
4280
0
  sm->last_eapol_key_ver = ver;
4281
4282
0
  if ((key_info & WPA_KEY_INFO_MIC) &&
4283
0
      wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
4284
0
    goto out;
4285
4286
#ifdef CONFIG_FILS
4287
  if (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
4288
    if (wpa_supp_aead_decrypt(sm, tmp, data_len, &key_data_len))
4289
      goto out;
4290
  }
4291
#endif /* CONFIG_FILS */
4292
4293
0
  if (sm->proto == WPA_PROTO_RSN &&
4294
0
      (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) && mic_len) {
4295
    /*
4296
     * Only decrypt the Key Data field if the frame's authenticity
4297
     * was verified. When using AES-SIV (FILS), the MIC flag is not
4298
     * set, so this check should only be performed if mic_len != 0
4299
     * which is the case in this code branch.
4300
     */
4301
0
    if (!(key_info & WPA_KEY_INFO_MIC)) {
4302
0
      wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
4303
0
        "WPA: Ignore EAPOL-Key with encrypted but unauthenticated data");
4304
0
      goto out;
4305
0
    }
4306
0
    if (wpa_supplicant_decrypt_key_data(sm, key, mic_len,
4307
0
                ver, key_data,
4308
0
                &key_data_len))
4309
0
      goto out;
4310
0
  }
4311
4312
0
  if (key_info & WPA_KEY_INFO_KEY_TYPE) {
4313
0
    if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
4314
0
      wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
4315
0
        "RSN: Ignored EAPOL-Key (Pairwise) with non-zero key index");
4316
0
      goto out;
4317
0
    }
4318
0
    if (key_info & (WPA_KEY_INFO_MIC |
4319
0
        WPA_KEY_INFO_ENCR_KEY_DATA)) {
4320
      /* 3/4 4-Way Handshake */
4321
0
      wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
4322
0
                  key_data_len);
4323
0
    } else {
4324
      /* 1/4 4-Way Handshake */
4325
0
      wpa_supplicant_process_1_of_4(sm, src_addr, key,
4326
0
                  ver, key_data,
4327
0
                  key_data_len,
4328
0
                  encrypted);
4329
0
    }
4330
0
  } else {
4331
0
    if ((mic_len && (key_info & WPA_KEY_INFO_MIC)) ||
4332
0
        (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA))) {
4333
      /* 1/2 Group Key Handshake */
4334
0
      if (sm->wnm_sleep_mode) {
4335
0
        wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
4336
0
          "RSN: Discard unexpected group key handshake message 1 while in WNM sleep mode");
4337
0
        goto out;
4338
0
      }
4339
0
      if (sm->mlo.valid_links)
4340
0
        wpa_supplicant_process_mlo_1_of_2(sm, src_addr,
4341
0
                  key, key_data,
4342
0
                  key_data_len,
4343
0
                  ver);
4344
0
      else
4345
0
        wpa_supplicant_process_1_of_2(sm, src_addr, key,
4346
0
                    key_data,
4347
0
                    key_data_len,
4348
0
                    ver);
4349
0
    } else {
4350
0
      wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
4351
0
        "RSN: EAPOL-Key (Group) without Mic/Encr bit - dropped");
4352
0
    }
4353
0
  }
4354
4355
0
  ret = 1;
4356
4357
0
out:
4358
0
  bin_clear_free(tmp, data_len);
4359
0
  return ret;
4360
0
}
4361
4362
4363
#ifdef CONFIG_CTRL_IFACE
4364
static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
4365
{
4366
  switch (sm->key_mgmt) {
4367
  case WPA_KEY_MGMT_IEEE8021X:
4368
    return ((sm->proto == WPA_PROTO_RSN) ?
4369
      RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
4370
      WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
4371
  case WPA_KEY_MGMT_PSK:
4372
    return (sm->proto == WPA_PROTO_RSN ?
4373
      RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
4374
      WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
4375
#ifdef CONFIG_IEEE80211R
4376
  case WPA_KEY_MGMT_FT_IEEE8021X:
4377
    return RSN_AUTH_KEY_MGMT_FT_802_1X;
4378
  case WPA_KEY_MGMT_FT_PSK:
4379
    return RSN_AUTH_KEY_MGMT_FT_PSK;
4380
#endif /* CONFIG_IEEE80211R */
4381
  case WPA_KEY_MGMT_IEEE8021X_SHA256:
4382
    return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
4383
  case WPA_KEY_MGMT_PSK_SHA256:
4384
    return RSN_AUTH_KEY_MGMT_PSK_SHA256;
4385
  case WPA_KEY_MGMT_CCKM:
4386
    return (sm->proto == WPA_PROTO_RSN ?
4387
      RSN_AUTH_KEY_MGMT_CCKM:
4388
      WPA_AUTH_KEY_MGMT_CCKM);
4389
  case WPA_KEY_MGMT_WPA_NONE:
4390
    return WPA_AUTH_KEY_MGMT_NONE;
4391
  case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
4392
    return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
4393
  case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
4394
    return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
4395
  case WPA_KEY_MGMT_IEEE8021X_SHA384:
4396
    return RSN_AUTH_KEY_MGMT_802_1X_SHA384;
4397
  default:
4398
    return 0;
4399
  }
4400
}
4401
4402
4403
#define RSN_SUITE "%02x-%02x-%02x-%d"
4404
#define RSN_SUITE_ARG(s) \
4405
((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
4406
4407
/**
4408
 * wpa_sm_get_mib - Dump text list of MIB entries
4409
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4410
 * @buf: Buffer for the list
4411
 * @buflen: Length of the buffer
4412
 * Returns: Number of bytes written to buffer
4413
 *
4414
 * This function is used fetch dot11 MIB variables.
4415
 */
4416
int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
4417
{
4418
  char pmkid_txt[PMKID_LEN * 2 + 1];
4419
  bool rsna;
4420
  int ret;
4421
  size_t len;
4422
4423
  if (sm->cur_pmksa) {
4424
    wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
4425
         sm->cur_pmksa->pmkid, PMKID_LEN);
4426
  } else
4427
    pmkid_txt[0] = '\0';
4428
4429
  rsna = (wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
4430
    wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
4431
    sm->proto == WPA_PROTO_RSN;
4432
4433
  ret = os_snprintf(buf, buflen,
4434
        "dot11RSNAOptionImplemented=TRUE\n"
4435
        "dot11RSNAPreauthenticationImplemented=TRUE\n"
4436
        "dot11RSNAEnabled=%s\n"
4437
        "dot11RSNAPreauthenticationEnabled=%s\n"
4438
        "dot11RSNAConfigVersion=%d\n"
4439
        "dot11RSNAConfigPairwiseKeysSupported=5\n"
4440
        "dot11RSNAConfigGroupCipherSize=%d\n"
4441
        "dot11RSNAConfigPMKLifetime=%d\n"
4442
        "dot11RSNAConfigPMKReauthThreshold=%d\n"
4443
        "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
4444
        "dot11RSNAConfigSATimeout=%d\n",
4445
        rsna ? "TRUE" : "FALSE",
4446
        rsna ? "TRUE" : "FALSE",
4447
        RSN_VERSION,
4448
        wpa_cipher_key_len(sm->group_cipher) * 8,
4449
        sm->dot11RSNAConfigPMKLifetime,
4450
        sm->dot11RSNAConfigPMKReauthThreshold,
4451
        sm->dot11RSNAConfigSATimeout);
4452
  if (os_snprintf_error(buflen, ret))
4453
    return 0;
4454
  len = ret;
4455
4456
  ret = os_snprintf(
4457
    buf + len, buflen - len,
4458
    "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
4459
    "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
4460
    "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
4461
    "dot11RSNAPMKIDUsed=%s\n"
4462
    "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
4463
    "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
4464
    "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
4465
    "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
4466
    "dot11RSNA4WayHandshakeFailures=%u\n",
4467
    RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
4468
    RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
4469
              sm->pairwise_cipher)),
4470
    RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
4471
              sm->group_cipher)),
4472
    pmkid_txt,
4473
    RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
4474
    RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
4475
              sm->pairwise_cipher)),
4476
    RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
4477
              sm->group_cipher)),
4478
    sm->dot11RSNA4WayHandshakeFailures);
4479
  if (!os_snprintf_error(buflen - len, ret))
4480
    len += ret;
4481
4482
  return (int) len;
4483
}
4484
#endif /* CONFIG_CTRL_IFACE */
4485
4486
4487
static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
4488
         void *ctx, enum pmksa_free_reason reason)
4489
0
{
4490
0
  struct wpa_sm *sm = ctx;
4491
0
  int deauth = 0;
4492
4493
0
  wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
4494
0
    MACSTR " reason=%d", MAC2STR(entry->aa), reason);
4495
4496
0
  if (sm->cur_pmksa == entry) {
4497
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
4498
0
      "RSN: %s current PMKSA entry",
4499
0
      reason == PMKSA_REPLACE ? "replaced" : "removed");
4500
0
    pmksa_cache_clear_current(sm);
4501
4502
    /*
4503
     * If an entry is simply being replaced, there's no need to
4504
     * deauthenticate because it will be immediately re-added.
4505
     * This happens when EAP authentication is completed again
4506
     * (reauth or failed PMKSA caching attempt).
4507
     */
4508
0
    if (reason != PMKSA_REPLACE)
4509
0
      deauth = 1;
4510
0
  }
4511
4512
0
  if (reason == PMKSA_EXPIRE &&
4513
0
      (sm->pmk_len == entry->pmk_len &&
4514
0
       os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
4515
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
4516
0
      "RSN: deauthenticating due to expired PMK");
4517
0
    pmksa_cache_clear_current(sm);
4518
0
    deauth = 1;
4519
0
  }
4520
4521
0
  if (deauth) {
4522
0
    sm->pmk_len = 0;
4523
0
    os_memset(sm->pmk, 0, sizeof(sm->pmk));
4524
0
    wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
4525
0
  }
4526
0
}
4527
4528
4529
static bool wpa_sm_pmksa_is_current_cb(struct rsn_pmksa_cache_entry *entry,
4530
               void *ctx)
4531
0
{
4532
0
  struct wpa_sm *sm = ctx;
4533
4534
0
  return sm->cur_pmksa == entry;
4535
0
}
4536
4537
4538
static void wpa_sm_pmksa_notify_cb(struct rsn_pmksa_cache_entry *entry,
4539
           void *ctx)
4540
0
{
4541
0
  struct wpa_sm *sm = ctx;
4542
4543
0
  wpa_sm_notify_pmksa_cache_entry(sm, entry);
4544
0
}
4545
4546
4547
/**
4548
 * wpa_sm_init - Initialize WPA state machine
4549
 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
4550
 * Returns: Pointer to the allocated WPA state machine data
4551
 *
4552
 * This function is used to allocate a new WPA state machine and the returned
4553
 * value is passed to all WPA state machine calls.
4554
 */
4555
struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
4556
0
{
4557
0
  struct wpa_sm *sm;
4558
4559
0
  sm = os_zalloc(sizeof(*sm));
4560
0
  if (sm == NULL)
4561
0
    return NULL;
4562
0
  dl_list_init(&sm->pmksa_candidates);
4563
0
  sm->renew_snonce = 1;
4564
0
  sm->ctx = ctx;
4565
4566
0
  sm->dot11RSNAConfigPMKLifetime = 43200;
4567
0
  sm->dot11RSNAConfigPMKReauthThreshold = 70;
4568
0
  sm->dot11RSNAConfigSATimeout = 60;
4569
4570
0
  sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb,
4571
0
             wpa_sm_pmksa_is_current_cb,
4572
0
             wpa_sm_pmksa_notify_cb, sm, sm);
4573
0
  if (sm->pmksa == NULL) {
4574
0
    wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
4575
0
      "RSN: PMKSA cache initialization failed");
4576
0
    os_free(sm);
4577
0
    return NULL;
4578
0
  }
4579
4580
0
  return sm;
4581
0
}
4582
4583
4584
/**
4585
 * wpa_sm_deinit - Deinitialize WPA state machine
4586
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4587
 */
4588
void wpa_sm_deinit(struct wpa_sm *sm)
4589
0
{
4590
0
  int i;
4591
4592
0
  if (sm == NULL)
4593
0
    return;
4594
0
  pmksa_cache_deinit(sm->pmksa);
4595
0
  eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
4596
0
  eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
4597
0
  os_free(sm->assoc_wpa_ie);
4598
0
  os_free(sm->assoc_rsnxe);
4599
0
  os_free(sm->ap_wpa_ie);
4600
0
  os_free(sm->ap_rsn_ie);
4601
0
  os_free(sm->ap_rsnxe);
4602
0
  os_free(sm->ap_rsne_override);
4603
0
  os_free(sm->ap_rsne_override_2);
4604
0
  os_free(sm->ap_rsnxe_override);
4605
0
  os_free(sm->ap_security_profile);
4606
0
  for (i = 0; i < MAX_NUM_MLD_LINKS; i++) {
4607
0
    os_free(sm->mlo.links[i].ap_rsne);
4608
0
    os_free(sm->mlo.links[i].ap_rsnxe);
4609
0
    os_free(sm->mlo.links[i].ap_rsnoe);
4610
0
    os_free(sm->mlo.links[i].ap_rsno2e);
4611
0
    os_free(sm->mlo.links[i].ap_rsnxoe);
4612
0
  }
4613
0
  wpa_sm_drop_sa(sm);
4614
0
  os_free(sm->ctx);
4615
0
#ifdef CONFIG_IEEE80211R
4616
0
  os_free(sm->assoc_resp_ies);
4617
0
#endif /* CONFIG_IEEE80211R */
4618
#ifdef CONFIG_TESTING_OPTIONS
4619
  wpabuf_free(sm->test_assoc_ie);
4620
  wpabuf_free(sm->test_eapol_m2_elems);
4621
  wpabuf_free(sm->test_eapol_m4_elems);
4622
  wpabuf_free(sm->test_rsnxe_data);
4623
  wpabuf_free(sm->test_rsnxe_mask);
4624
#endif /* CONFIG_TESTING_OPTIONS */
4625
#ifdef CONFIG_FILS_SK_PFS
4626
  crypto_ecdh_deinit(sm->fils_ecdh);
4627
#endif /* CONFIG_FILS_SK_PFS */
4628
#ifdef CONFIG_FILS
4629
  wpabuf_free(sm->fils_ft_ies);
4630
#endif /* CONFIG_FILS */
4631
#ifdef CONFIG_OWE
4632
  crypto_ecdh_deinit(sm->owe_ecdh);
4633
#endif /* CONFIG_OWE */
4634
#ifdef CONFIG_DPP2
4635
  wpabuf_clear_free(sm->dpp_z);
4636
#endif /* CONFIG_DPP2 */
4637
0
  os_memset(sm->last_kck, 0, sizeof(sm->last_kck));
4638
0
  os_free(sm);
4639
0
}
4640
4641
4642
static void wpa_sm_clear_ptk(struct wpa_sm *sm)
4643
0
{
4644
0
  int i;
4645
4646
0
  sm->ptk_set = 0;
4647
0
  os_memset(&sm->ptk, 0, sizeof(sm->ptk));
4648
0
  sm->tptk_set = 0;
4649
0
  os_memset(&sm->tptk, 0, sizeof(sm->tptk));
4650
0
  os_memset(&sm->gtk, 0, sizeof(sm->gtk));
4651
0
  os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
4652
0
  os_memset(&sm->igtk, 0, sizeof(sm->igtk));
4653
0
  os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
4654
0
  os_memset(&sm->bigtk, 0, sizeof(sm->bigtk));
4655
0
  os_memset(&sm->bigtk_wnm_sleep, 0, sizeof(sm->bigtk_wnm_sleep));
4656
0
  sm->tk_set = false;
4657
0
  for (i = 0; i < MAX_NUM_MLD_LINKS; i++) {
4658
0
    os_memset(&sm->mlo.links[i].gtk, 0,
4659
0
        sizeof(sm->mlo.links[i].gtk));
4660
0
    os_memset(&sm->mlo.links[i].gtk_wnm_sleep, 0,
4661
0
        sizeof(sm->mlo.links[i].gtk_wnm_sleep));
4662
0
    os_memset(&sm->mlo.links[i].igtk, 0,
4663
0
        sizeof(sm->mlo.links[i].igtk));
4664
0
    os_memset(&sm->mlo.links[i].igtk_wnm_sleep, 0,
4665
0
        sizeof(sm->mlo.links[i].igtk_wnm_sleep));
4666
0
    os_memset(&sm->mlo.links[i].bigtk, 0,
4667
0
        sizeof(sm->mlo.links[i].bigtk));
4668
0
    os_memset(&sm->mlo.links[i].bigtk_wnm_sleep, 0,
4669
0
        sizeof(sm->mlo.links[i].bigtk_wnm_sleep));
4670
0
  }
4671
0
}
4672
4673
4674
/**
4675
 * wpa_sm_notify_assoc - Notify WPA state machine about association
4676
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4677
 * @bssid: The BSSID of the new association
4678
 *
4679
 * This function is called to let WPA state machine know that the connection
4680
 * was established.
4681
 */
4682
void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
4683
0
{
4684
0
  int clear_keys = 1;
4685
4686
0
  if (sm == NULL)
4687
0
    return;
4688
4689
0
  wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
4690
0
    "WPA: Association event - clear replay counter");
4691
0
  os_memcpy(sm->bssid, bssid, ETH_ALEN);
4692
0
  os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
4693
0
  sm->rx_replay_counter_set = 0;
4694
0
  sm->renew_snonce = 1;
4695
0
  if (ether_addr_equal(sm->preauth_bssid, bssid))
4696
0
    rsn_preauth_deinit(sm);
4697
4698
0
#ifdef CONFIG_IEEE80211R
4699
0
  if (wpa_ft_is_completed(sm)) {
4700
    /*
4701
     * Clear portValid to kick EAPOL state machine to re-enter
4702
     * AUTHENTICATED state to get the EAPOL port Authorized.
4703
     */
4704
0
    eapol_sm_notify_portValid(sm->eapol, false);
4705
0
    wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
4706
4707
    /* Prepare for the next transition */
4708
0
    wpa_ft_prepare_auth_request(sm, NULL);
4709
4710
0
    clear_keys = 0;
4711
0
    sm->ft_protocol = 1;
4712
0
  } else {
4713
0
    sm->ft_protocol = 0;
4714
0
  }
4715
0
#endif /* CONFIG_IEEE80211R */
4716
#ifdef CONFIG_FILS
4717
  if (sm->fils_completed) {
4718
    /*
4719
     * Clear portValid to kick EAPOL state machine to re-enter
4720
     * AUTHENTICATED state to get the EAPOL port Authorized.
4721
     */
4722
    wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
4723
    clear_keys = 0;
4724
  }
4725
#endif /* CONFIG_FILS */
4726
#ifdef CONFIG_ENC_ASSOC
4727
  if (sm->eppke_completed || sm->eap_over_auth_frame_completed) {
4728
    /*
4729
     * Clear portValid to kick EAPOL state machine to re-enter
4730
     * AUTHENTICATED state to get the EAPOL port Authorized.
4731
     */
4732
    wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
4733
    clear_keys = 0;
4734
  }
4735
#endif /* CONFIG_ENC_ASSOC */
4736
4737
0
  if (clear_keys) {
4738
    /*
4739
     * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
4740
     * this is not part of a Fast BSS Transition.
4741
     */
4742
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
4743
0
    wpa_sm_clear_ptk(sm);
4744
0
    sm->hash_alg = RSN_HASH_NOT_SPECIFIED;
4745
0
  }
4746
4747
0
#ifdef CONFIG_TDLS
4748
0
  wpa_tdls_assoc(sm);
4749
0
#endif /* CONFIG_TDLS */
4750
4751
#ifdef CONFIG_P2P
4752
  os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
4753
#endif /* CONFIG_P2P */
4754
4755
0
  sm->keyidx_active = 0;
4756
0
  sm->wnm_sleep_mode = false;
4757
0
}
4758
4759
4760
/**
4761
 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
4762
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4763
 *
4764
 * This function is called to let WPA state machine know that the connection
4765
 * was lost. This will abort any existing pre-authentication session.
4766
 */
4767
void wpa_sm_notify_disassoc(struct wpa_sm *sm)
4768
0
{
4769
0
  eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
4770
0
  eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
4771
0
  rsn_preauth_deinit(sm);
4772
0
  pmksa_cache_clear_current(sm);
4773
0
  if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
4774
0
    sm->dot11RSNA4WayHandshakeFailures++;
4775
0
#ifdef CONFIG_TDLS
4776
0
  wpa_tdls_disassoc(sm);
4777
0
#endif /* CONFIG_TDLS */
4778
#ifdef CONFIG_FILS
4779
  sm->fils_completed = 0;
4780
#endif /* CONFIG_FILS */
4781
0
#ifdef CONFIG_IEEE80211R
4782
0
  sm->ft_reassoc_completed = 0;
4783
0
  sm->ft_protocol = 0;
4784
0
#endif /* CONFIG_IEEE80211R */
4785
0
  wpa_sm_clear_enc_assoc_completed(sm);
4786
4787
  /* Keys are not needed in the WPA state machine anymore */
4788
0
  wpa_sm_drop_sa(sm);
4789
0
  sm->keyidx_active = 0;
4790
4791
0
  sm->msg_3_of_4_ok = 0;
4792
0
  os_memset(sm->bssid, 0, ETH_ALEN);
4793
0
  sm->hash_alg = RSN_HASH_NOT_SPECIFIED;
4794
0
  sm->wnm_sleep_mode = false;
4795
0
}
4796
4797
4798
void wpa_sm_notify_wnm_sleep_mode(struct wpa_sm *sm, bool active)
4799
0
{
4800
0
  if (sm)
4801
0
    sm->wnm_sleep_mode = active;
4802
0
}
4803
4804
4805
/**
4806
 * wpa_sm_set_pmk - Set PMK
4807
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4808
 * @pmk: The new PMK
4809
 * @pmk_len: The length of the new PMK in bytes
4810
 * @pmkid: Calculated PMKID
4811
 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
4812
 * @pasn_group: Negotiated group during PASN/EPPKE exchange
4813
 *
4814
 * Configure the PMK for WPA state machine.
4815
 */
4816
void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
4817
        const u8 *pmkid, const u8 *bssid, u16 pasn_group)
4818
0
{
4819
0
  if (sm == NULL)
4820
0
    return;
4821
4822
0
  wpa_hexdump_key(MSG_DEBUG, "WPA: Set PMK based on external data",
4823
0
      pmk, pmk_len);
4824
0
  sm->pmk_len = pmk_len;
4825
0
  os_memcpy(sm->pmk, pmk, pmk_len);
4826
4827
#ifdef CONFIG_SAE
4828
  if (wpa_key_mgmt_sae_ext_key(sm->key_mgmt)) {
4829
    if (pmk_len == 32)
4830
      sm->hash_alg = RSN_HASH_SHA256;
4831
    else if (pmk_len == 48)
4832
      sm->hash_alg = RSN_HASH_SHA384;
4833
    else if (pmk_len == 64)
4834
      sm->hash_alg = RSN_HASH_SHA512;
4835
  }
4836
#endif /* CONFIG_SAE */
4837
4838
#ifdef CONFIG_ENC_ASSOC
4839
  if (sm->key_mgmt == WPA_KEY_MGMT_EPPKE)
4840
    sm->pasn_group = pasn_group;
4841
#endif /* CONFIG_ENC_ASSOC */
4842
4843
0
#ifdef CONFIG_IEEE80211R
4844
  /* Set XXKey to be PSK for FT key derivation */
4845
0
  sm->xxkey_len = pmk_len;
4846
0
  os_memcpy(sm->xxkey, pmk, pmk_len);
4847
0
#endif /* CONFIG_IEEE80211R */
4848
4849
0
  if (bssid) {
4850
0
    sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len,
4851
0
            pmkid, NULL, 0, bssid,
4852
0
            sm->own_addr,
4853
0
            sm->network_ctx, sm->key_mgmt,
4854
0
            NULL, 0,
4855
0
            RSN_HASH_NOT_SPECIFIED);
4856
0
  }
4857
0
}
4858
4859
4860
/**
4861
 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
4862
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4863
 *
4864
 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
4865
 * will be cleared.
4866
 */
4867
void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
4868
0
{
4869
0
  if (sm == NULL)
4870
0
    return;
4871
4872
0
  if (sm->cur_pmksa) {
4873
0
    wpa_hexdump_key(MSG_DEBUG,
4874
0
        "WPA: Set PMK based on current PMKSA",
4875
0
        sm->cur_pmksa->pmk, sm->cur_pmksa->pmk_len);
4876
0
    sm->pmk_len = sm->cur_pmksa->pmk_len;
4877
0
    os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
4878
0
  } else {
4879
0
    wpa_printf(MSG_DEBUG, "WPA: No current PMKSA - clear PMK");
4880
0
    sm->pmk_len = 0;
4881
0
    os_memset(sm->pmk, 0, PMK_LEN_MAX);
4882
0
  }
4883
0
}
4884
4885
4886
/**
4887
 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
4888
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4889
 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
4890
 */
4891
void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
4892
0
{
4893
0
  if (sm)
4894
0
    sm->fast_reauth = fast_reauth;
4895
0
}
4896
4897
4898
/**
4899
 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
4900
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4901
 * @scard_ctx: Context pointer for smartcard related callback functions
4902
 */
4903
void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
4904
0
{
4905
0
  if (sm == NULL)
4906
0
    return;
4907
0
  sm->scard_ctx = scard_ctx;
4908
0
  if (sm->preauth_eapol)
4909
0
    eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
4910
0
}
4911
4912
4913
/**
4914
 * wpa_sm_set_config - Notification of current configuration change
4915
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4916
 * @config: Pointer to current network configuration
4917
 *
4918
 * Notify WPA state machine that configuration has changed. config will be
4919
 * stored as a backpointer to network configuration. This can be %NULL to clear
4920
 * the stored pointed.
4921
 */
4922
void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
4923
0
{
4924
0
  if (!sm)
4925
0
    return;
4926
4927
0
  if (config) {
4928
0
    sm->network_ctx = config->network_ctx;
4929
0
    sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
4930
0
    sm->proactive_key_caching = config->proactive_key_caching;
4931
0
    sm->eap_workaround = config->eap_workaround;
4932
0
    sm->eap_conf_ctx = config->eap_conf_ctx;
4933
0
    if (config->ssid) {
4934
0
      os_memcpy(sm->ssid, config->ssid, config->ssid_len);
4935
0
      sm->ssid_len = config->ssid_len;
4936
0
    } else
4937
0
      sm->ssid_len = 0;
4938
0
    sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
4939
0
    sm->p2p = config->p2p;
4940
0
    sm->wpa_rsc_relaxation = config->wpa_rsc_relaxation;
4941
0
    sm->owe_ptk_workaround = config->owe_ptk_workaround;
4942
0
    sm->force_kdk_derivation = config->force_kdk_derivation;
4943
#ifdef CONFIG_FILS
4944
    if (config->fils_cache_id) {
4945
      sm->fils_cache_id_set = 1;
4946
      os_memcpy(sm->fils_cache_id, config->fils_cache_id,
4947
          FILS_CACHE_ID_LEN);
4948
    } else {
4949
      sm->fils_cache_id_set = 0;
4950
    }
4951
#endif /* CONFIG_FILS */
4952
0
    sm->beacon_prot = config->beacon_prot;
4953
0
    sm->disable_pmksa_caching = config->disable_pmksa_caching;
4954
0
    if (sm->disable_pmksa_caching) {
4955
0
      pmksa_cache_clear_current(sm);
4956
0
      if (wpa_sm_get_state(sm) != WPA_COMPLETED)
4957
0
        pmksa_cache_flush(sm->pmksa, sm->network_ctx,
4958
0
              NULL, 0, false, NULL);
4959
0
    }
4960
0
  } else {
4961
0
    sm->network_ctx = NULL;
4962
0
    sm->allowed_pairwise_cipher = 0;
4963
0
    sm->proactive_key_caching = 0;
4964
0
    sm->eap_workaround = 0;
4965
0
    sm->eap_conf_ctx = NULL;
4966
0
    sm->ssid_len = 0;
4967
0
    sm->wpa_ptk_rekey = 0;
4968
0
    sm->p2p = 0;
4969
0
    sm->wpa_rsc_relaxation = 0;
4970
0
    sm->owe_ptk_workaround = 0;
4971
0
    sm->beacon_prot = 0;
4972
0
    sm->force_kdk_derivation = false;
4973
0
    sm->disable_pmksa_caching = false;
4974
0
  }
4975
0
}
4976
4977
4978
void wpa_sm_set_ssid(struct wpa_sm *sm, const u8 *ssid, size_t ssid_len)
4979
0
{
4980
0
  if (!sm)
4981
0
    return;
4982
4983
0
  if (ssid) {
4984
0
    os_memcpy(sm->ssid, ssid, ssid_len);
4985
0
    sm->ssid_len = ssid_len;
4986
0
  } else {
4987
0
    sm->ssid_len = 0;
4988
0
  }
4989
0
}
4990
4991
4992
static void wpa_sm_clear_mlo_group_keys(struct wpa_sm *sm, int link_id)
4993
0
{
4994
0
  if (!sm)
4995
0
    return;
4996
4997
0
  os_memset(&sm->mlo.links[link_id].gtk, 0,
4998
0
      sizeof(sm->mlo.links[link_id].gtk));
4999
0
  os_memset(&sm->mlo.links[link_id].gtk_wnm_sleep, 0,
5000
0
      sizeof(sm->mlo.links[link_id].gtk_wnm_sleep));
5001
0
  os_memset(&sm->mlo.links[link_id].igtk, 0,
5002
0
      sizeof(sm->mlo.links[link_id].igtk));
5003
0
  os_memset(&sm->mlo.links[link_id].igtk_wnm_sleep, 0,
5004
0
      sizeof(sm->mlo.links[link_id].igtk_wnm_sleep));
5005
0
  os_memset(&sm->mlo.links[link_id].bigtk, 0,
5006
0
      sizeof(sm->mlo.links[link_id].bigtk));
5007
0
  os_memset(&sm->mlo.links[link_id].bigtk_wnm_sleep, 0,
5008
0
      sizeof(sm->mlo.links[link_id].bigtk_wnm_sleep));
5009
0
}
5010
5011
5012
int wpa_sm_set_mlo_params(struct wpa_sm *sm, const struct wpa_sm_mlo *mlo)
5013
0
{
5014
0
  int i;
5015
0
  u16 removed_links;
5016
5017
0
  if (!sm)
5018
0
    return -1;
5019
5020
0
  os_memcpy(sm->mlo.ap_mld_addr, mlo->ap_mld_addr, ETH_ALEN);
5021
0
  removed_links = ~mlo->valid_links & sm->mlo.valid_links;
5022
0
  sm->mlo.assoc_link_id =  mlo->assoc_link_id;
5023
0
  sm->mlo.valid_links = mlo->valid_links;
5024
0
  sm->mlo.req_links = mlo->req_links;
5025
5026
0
  for (i = 0; i < MAX_NUM_MLD_LINKS; i++) {
5027
0
    const u8 *ie;
5028
0
    size_t len;
5029
5030
0
    if (sm->mlo.req_links & BIT(i)) {
5031
0
      if (!mlo->links[i].ap_rsne ||
5032
0
          mlo->links[i].ap_rsne_len == 0) {
5033
0
        wpa_dbg(sm->ctx->msg_ctx, MSG_INFO,
5034
0
          "RSN: No RSNE for AP MLO link %d with BSSID "
5035
0
          MACSTR,
5036
0
          i, MAC2STR(mlo->links[i].bssid));
5037
0
        return -1;
5038
5039
0
      }
5040
0
      os_memcpy(sm->mlo.links[i].addr, mlo->links[i].addr,
5041
0
          ETH_ALEN);
5042
0
      os_memcpy(sm->mlo.links[i].bssid, mlo->links[i].bssid,
5043
0
          ETH_ALEN);
5044
0
    }
5045
5046
0
    ie = mlo->links[i].ap_rsne;
5047
0
    len = mlo->links[i].ap_rsne_len;
5048
0
    os_free(sm->mlo.links[i].ap_rsne);
5049
0
    if (!ie || len == 0) {
5050
0
      if (sm->mlo.links[i].ap_rsne)
5051
0
        wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
5052
0
          "RSN: Clearing MLO link[%u] AP RSNE",
5053
0
          i);
5054
0
      sm->mlo.links[i].ap_rsne = NULL;
5055
0
      sm->mlo.links[i].ap_rsne_len = 0;
5056
0
    } else {
5057
0
      wpa_hexdump_link(MSG_DEBUG, i, "RSN: Set AP RSNE",
5058
0
           ie, len);
5059
0
      sm->mlo.links[i].ap_rsne = os_memdup(ie, len);
5060
0
      if (!sm->mlo.links[i].ap_rsne) {
5061
0
        sm->mlo.links[i].ap_rsne_len = 0;
5062
0
        return -1;
5063
0
      }
5064
0
      sm->mlo.links[i].ap_rsne_len = len;
5065
0
    }
5066
5067
0
    ie = mlo->links[i].ap_rsnxe;
5068
0
    len = mlo->links[i].ap_rsnxe_len;
5069
0
    os_free(sm->mlo.links[i].ap_rsnxe);
5070
0
    if (!ie || len == 0) {
5071
0
      if (sm->mlo.links[i].ap_rsnxe)
5072
0
        wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
5073
0
          "RSN: Clearing MLO link[%u] AP RSNXE",
5074
0
          i);
5075
0
      sm->mlo.links[i].ap_rsnxe = NULL;
5076
0
      sm->mlo.links[i].ap_rsnxe_len = 0;
5077
0
    } else {
5078
0
      wpa_hexdump_link(MSG_DEBUG, i, "RSN: Set AP RSNXE", ie,
5079
0
           len);
5080
0
      sm->mlo.links[i].ap_rsnxe = os_memdup(ie, len);
5081
0
      if (!sm->mlo.links[i].ap_rsnxe) {
5082
0
        sm->mlo.links[i].ap_rsnxe_len = 0;
5083
0
        return -1;
5084
0
      }
5085
0
      sm->mlo.links[i].ap_rsnxe_len = len;
5086
0
    }
5087
5088
0
    ie = mlo->links[i].ap_rsnoe;
5089
0
    len = mlo->links[i].ap_rsnoe_len;
5090
0
    os_free(sm->mlo.links[i].ap_rsnoe);
5091
0
    if (!ie || len == 0) {
5092
0
      if (sm->mlo.links[i].ap_rsnoe)
5093
0
        wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
5094
0
          "RSN: Clearing MLO link[%u] AP RSNOE",
5095
0
          i);
5096
0
      sm->mlo.links[i].ap_rsnoe = NULL;
5097
0
      sm->mlo.links[i].ap_rsnoe_len = 0;
5098
0
    } else {
5099
0
      wpa_hexdump_link(MSG_DEBUG, i, "RSN: Set AP RSNOE",
5100
0
           ie, len);
5101
0
      sm->mlo.links[i].ap_rsnoe = os_memdup(ie, len);
5102
0
      if (!sm->mlo.links[i].ap_rsnoe) {
5103
0
        sm->mlo.links[i].ap_rsnoe_len = 0;
5104
0
        return -1;
5105
0
      }
5106
0
      sm->mlo.links[i].ap_rsnoe_len = len;
5107
0
    }
5108
5109
0
    ie = mlo->links[i].ap_rsno2e;
5110
0
    len = mlo->links[i].ap_rsno2e_len;
5111
0
    os_free(sm->mlo.links[i].ap_rsno2e);
5112
0
    if (!ie || len == 0) {
5113
0
      if (sm->mlo.links[i].ap_rsno2e)
5114
0
        wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
5115
0
          "RSN: Clearing MLO link[%u] AP RSNO2E",
5116
0
          i);
5117
0
      sm->mlo.links[i].ap_rsno2e = NULL;
5118
0
      sm->mlo.links[i].ap_rsno2e_len = 0;
5119
0
    } else {
5120
0
      wpa_hexdump_link(MSG_DEBUG, i, "RSN: Set AP RSNO2E",
5121
0
           ie, len);
5122
0
      sm->mlo.links[i].ap_rsno2e = os_memdup(ie, len);
5123
0
      if (!sm->mlo.links[i].ap_rsno2e) {
5124
0
        sm->mlo.links[i].ap_rsno2e_len = 0;
5125
0
        return -1;
5126
0
      }
5127
0
      sm->mlo.links[i].ap_rsno2e_len = len;
5128
0
    }
5129
5130
0
    ie = mlo->links[i].ap_rsnxoe;
5131
0
    len = mlo->links[i].ap_rsnxoe_len;
5132
0
    os_free(sm->mlo.links[i].ap_rsnxoe);
5133
0
    if (!ie || len == 0) {
5134
0
      if (sm->mlo.links[i].ap_rsnxoe)
5135
0
        wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
5136
0
          "RSN: Clearing MLO link[%u] AP RSNXOE",
5137
0
          i);
5138
0
      sm->mlo.links[i].ap_rsnxoe = NULL;
5139
0
      sm->mlo.links[i].ap_rsnxoe_len = 0;
5140
0
    } else {
5141
0
      wpa_hexdump_link(MSG_DEBUG, i, "RSN: Set AP RSNXOE",
5142
0
           ie, len);
5143
0
      sm->mlo.links[i].ap_rsnxoe = os_memdup(ie, len);
5144
0
      if (!sm->mlo.links[i].ap_rsnxoe) {
5145
0
        sm->mlo.links[i].ap_rsnxoe_len = 0;
5146
0
        return -1;
5147
0
      }
5148
0
      sm->mlo.links[i].ap_rsnxoe_len = len;
5149
0
    }
5150
5151
0
    if (removed_links & BIT(i)) {
5152
0
      wpa_printf(MSG_DEBUG,
5153
0
           "RSN: Clearing MLO group keys for link[%u]",
5154
0
           i);
5155
0
      wpa_sm_clear_mlo_group_keys(sm, i);
5156
0
    }
5157
0
  }
5158
5159
0
  return 0;
5160
0
}
5161
5162
5163
/**
5164
 * wpa_sm_set_own_addr - Set own MAC address
5165
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
5166
 * @addr: Own MAC address
5167
 */
5168
void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
5169
0
{
5170
0
  if (sm)
5171
0
    os_memcpy(sm->own_addr, addr, ETH_ALEN);
5172
0
}
5173
5174
5175
/**
5176
 * wpa_sm_set_ifname - Set network interface name
5177
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
5178
 * @ifname: Interface name
5179
 * @bridge_ifname: Optional bridge interface name (for pre-auth)
5180
 */
5181
void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
5182
           const char *bridge_ifname)
5183
0
{
5184
0
  if (sm) {
5185
0
    sm->ifname = ifname;
5186
0
    sm->bridge_ifname = bridge_ifname;
5187
0
  }
5188
0
}
5189
5190
5191
/**
5192
 * wpa_sm_set_eapol - Set EAPOL state machine pointer
5193
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
5194
 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
5195
 */
5196
void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
5197
0
{
5198
0
  if (sm)
5199
0
    sm->eapol = eapol;
5200
0
}
5201
5202
5203
/**
5204
 * wpa_sm_set_param - Set WPA state machine parameters
5205
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
5206
 * @param: Parameter field
5207
 * @value: Parameter value
5208
 * Returns: 0 on success, -1 on failure
5209
 */
5210
int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
5211
         unsigned int value)
5212
0
{
5213
0
  int ret = 0;
5214
5215
0
  if (sm == NULL)
5216
0
    return -1;
5217
5218
0
  switch (param) {
5219
0
  case RSNA_PMK_LIFETIME:
5220
0
    if (value > 0)
5221
0
      sm->dot11RSNAConfigPMKLifetime = value;
5222
0
    else
5223
0
      ret = -1;
5224
0
    break;
5225
0
  case RSNA_PMK_REAUTH_THRESHOLD:
5226
0
    if (value > 0 && value <= 100)
5227
0
      sm->dot11RSNAConfigPMKReauthThreshold = value;
5228
0
    else
5229
0
      ret = -1;
5230
0
    break;
5231
0
  case RSNA_SA_TIMEOUT:
5232
0
    if (value > 0)
5233
0
      sm->dot11RSNAConfigSATimeout = value;
5234
0
    else
5235
0
      ret = -1;
5236
0
    break;
5237
0
  case WPA_PARAM_PROTO:
5238
0
    sm->proto = value;
5239
0
    break;
5240
0
  case WPA_PARAM_PAIRWISE:
5241
0
    sm->pairwise_cipher = value;
5242
0
    break;
5243
0
  case WPA_PARAM_GROUP:
5244
0
    sm->group_cipher = value;
5245
0
    break;
5246
0
  case WPA_PARAM_KEY_MGMT:
5247
0
    sm->key_mgmt = value;
5248
0
    break;
5249
0
  case WPA_PARAM_MGMT_GROUP:
5250
0
    sm->mgmt_group_cipher = value;
5251
0
    break;
5252
0
  case WPA_PARAM_RSN_ENABLED:
5253
0
    sm->rsn_enabled = value;
5254
0
    break;
5255
0
  case WPA_PARAM_MFP:
5256
0
    sm->mfp = value;
5257
0
    break;
5258
0
  case WPA_PARAM_OCV:
5259
0
    sm->ocv = value;
5260
0
    break;
5261
0
  case WPA_PARAM_SAE_PWE:
5262
0
    sm->sae_pwe = value;
5263
0
    break;
5264
0
  case WPA_PARAM_SAE_PK:
5265
0
    sm->sae_pk = value;
5266
0
    break;
5267
0
  case WPA_PARAM_DENY_PTK0_REKEY:
5268
0
    sm->wpa_deny_ptk0_rekey = value;
5269
0
    break;
5270
0
  case WPA_PARAM_EXT_KEY_ID:
5271
0
    sm->ext_key_id = value;
5272
0
    break;
5273
0
  case WPA_PARAM_USE_EXT_KEY_ID:
5274
0
    sm->use_ext_key_id = value;
5275
0
    break;
5276
0
  case WPA_PARAM_SPP_AMSDU:
5277
0
    sm->spp_amsdu = !!value;
5278
0
    break;
5279
#ifdef CONFIG_TESTING_OPTIONS
5280
  case WPA_PARAM_FT_RSNXE_USED:
5281
    sm->ft_rsnxe_used = value;
5282
    break;
5283
  case WPA_PARAM_OCI_FREQ_EAPOL:
5284
    sm->oci_freq_override_eapol = value;
5285
    break;
5286
  case WPA_PARAM_OCI_FREQ_EAPOL_G2:
5287
    sm->oci_freq_override_eapol_g2 = value;
5288
    break;
5289
  case WPA_PARAM_OCI_FREQ_FT_ASSOC:
5290
    sm->oci_freq_override_ft_assoc = value;
5291
    break;
5292
  case WPA_PARAM_OCI_FREQ_FILS_ASSOC:
5293
    sm->oci_freq_override_fils_assoc = value;
5294
    break;
5295
  case WPA_PARAM_DISABLE_EAPOL_G2_TX:
5296
    sm->disable_eapol_g2_tx = value;
5297
    break;
5298
  case WPA_PARAM_ENCRYPT_EAPOL_M2:
5299
    sm->encrypt_eapol_m2 = value;
5300
    break;
5301
  case WPA_PARAM_ENCRYPT_EAPOL_M4:
5302
    sm->encrypt_eapol_m4 = value;
5303
    break;
5304
  case WPA_PARAM_EAPOL_2_KEY_INFO_SET_MASK:
5305
    sm->eapol_2_key_info_set_mask = value;
5306
    break;
5307
#endif /* CONFIG_TESTING_OPTIONS */
5308
0
  case WPA_PARAM_URNM_MFPR:
5309
0
    sm->prot_range_neg = value;
5310
0
    break;
5311
0
  case WPA_PARAM_URNM_MFPR_X20:
5312
0
    sm->prot_range_neg_x20 = value;
5313
0
    break;
5314
#ifdef CONFIG_DPP2
5315
  case WPA_PARAM_DPP_PFS:
5316
    sm->dpp_pfs = value;
5317
    break;
5318
#endif /* CONFIG_DPP2 */
5319
0
  case WPA_PARAM_WMM_ENABLED:
5320
0
    sm->wmm_enabled = value;
5321
0
    break;
5322
0
  case WPA_PARAM_FT_PREPEND_PMKID:
5323
0
    sm->ft_prepend_pmkid = value;
5324
0
    break;
5325
0
  case WPA_PARAM_SSID_PROTECTION:
5326
0
    sm->ssid_protection = value;
5327
0
    break;
5328
0
  case WPA_PARAM_RSN_OVERRIDE:
5329
0
    sm->rsn_override = value;
5330
0
    break;
5331
0
  case WPA_PARAM_RSN_OVERRIDE_SUPPORT:
5332
0
    sm->rsn_override_support = value;
5333
0
    break;
5334
0
  case WPA_PARAM_SAE_PW_ID_CHANGE:
5335
0
    sm->sae_pw_id_change = !!value;
5336
0
    break;
5337
0
  case WPA_PARAM_ASSOC_ENC:
5338
0
    sm->assoc_encryption = !!value;
5339
0
    break;
5340
0
  case WPA_PARAM_PMKSA_CACHING_PRIVACY:
5341
0
    sm->pmksa_privacy = !!value;
5342
0
    break;
5343
0
  case WPA_PARAM_SECURITY_PROFILE_ACTIVE:
5344
0
    sm->security_profile_active = !!value;
5345
0
    break;
5346
0
  default:
5347
0
    break;
5348
0
  }
5349
5350
0
  return ret;
5351
0
}
5352
5353
5354
static const u8 * wpa_sm_get_ap_rsne(struct wpa_sm *sm, size_t *len)
5355
0
{
5356
0
  if (sm->rsn_override == RSN_OVERRIDE_RSNE_OVERRIDE) {
5357
0
    *len = sm->ap_rsne_override_len;
5358
0
    return sm->ap_rsne_override;
5359
0
  }
5360
5361
0
  if (sm->rsn_override == RSN_OVERRIDE_RSNE_OVERRIDE_2) {
5362
0
    *len = sm->ap_rsne_override_2_len;
5363
0
    return sm->ap_rsne_override_2;
5364
0
  }
5365
5366
0
  *len = sm->ap_rsn_ie_len;
5367
0
  return sm->ap_rsn_ie;
5368
0
}
5369
5370
5371
static const u8 * wpa_sm_get_ap_rsnxe(struct wpa_sm *sm, size_t *len)
5372
0
{
5373
0
  if (sm->rsn_override != RSN_OVERRIDE_NOT_USED &&
5374
0
      sm->rsn_override != RSN_OVERRIDE_RSNE &&
5375
0
      sm->ap_rsnxe_override && sm->ap_rsnxe_override_len) {
5376
0
    *len = sm->ap_rsnxe_override_len;
5377
0
    return sm->ap_rsnxe_override;
5378
0
  }
5379
5380
0
  *len = sm->ap_rsnxe_len;
5381
0
  return sm->ap_rsnxe;
5382
0
}
5383
5384
5385
/**
5386
 * wpa_sm_get_status - Get WPA state machine
5387
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
5388
 * @buf: Buffer for status information
5389
 * @buflen: Maximum buffer length
5390
 * @verbose: Whether to include verbose status information
5391
 * Returns: Number of bytes written to buf.
5392
 *
5393
 * Query WPA state machine for status information. This function fills in
5394
 * a text area with current status information. If the buffer (buf) is not
5395
 * large enough, status information will be truncated to fit the buffer.
5396
 */
5397
int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
5398
          int verbose)
5399
0
{
5400
0
  char *pos = buf, *end = buf + buflen;
5401
0
  int ret;
5402
0
  const u8 *rsne;
5403
0
  size_t rsne_len;
5404
0
  size_t mic_len;
5405
5406
0
  rsne = wpa_sm_get_ap_rsne(sm, &rsne_len);
5407
5408
0
  ret = os_snprintf(pos, end - pos,
5409
0
        "pairwise_cipher=%s\n"
5410
0
        "group_cipher=%s\n"
5411
0
        "key_mgmt=%s\n",
5412
0
        wpa_cipher_txt(sm->pairwise_cipher),
5413
0
        wpa_cipher_txt(sm->group_cipher),
5414
0
        wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
5415
0
  if (os_snprintf_error(end - pos, ret))
5416
0
    return pos - buf;
5417
0
  pos += ret;
5418
5419
#ifdef CONFIG_DPP2
5420
  if (sm->key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_z) {
5421
    ret = os_snprintf(pos, end - pos, "dpp_pfs=1\n");
5422
    if (os_snprintf_error(end - pos, ret))
5423
      return pos - buf;
5424
    pos += ret;
5425
  }
5426
#endif /* CONFIG_DPP2 */
5427
5428
0
  if (sm->mfp != NO_MGMT_FRAME_PROTECTION && rsne) {
5429
0
    struct wpa_ie_data rsn;
5430
5431
0
    if (wpa_parse_wpa_ie_rsn(rsne, rsne_len, &rsn) >= 0 &&
5432
0
        rsn.capabilities & (WPA_CAPABILITY_MFPR |
5433
0
          WPA_CAPABILITY_MFPC)) {
5434
0
      ret = os_snprintf(pos, end - pos, "pmf=%d\n"
5435
0
            "mgmt_group_cipher=%s\n",
5436
0
            (rsn.capabilities &
5437
0
             WPA_CAPABILITY_MFPR) ? 2 : 1,
5438
0
            wpa_cipher_txt(
5439
0
              sm->mgmt_group_cipher));
5440
0
      if (os_snprintf_error(end - pos, ret))
5441
0
        return pos - buf;
5442
0
      pos += ret;
5443
0
    }
5444
0
  }
5445
5446
0
  if (sm->security_profile) {
5447
0
    ret = os_snprintf(pos, end - pos, "security_profile=%u\n",
5448
0
      sm->security_profile->number);
5449
0
    if (os_snprintf_error(end - pos, ret))
5450
0
      return pos - buf;
5451
0
    pos += ret;
5452
0
  }
5453
5454
0
  if (sm->pmk_len) {
5455
0
    ret = os_snprintf(pos, end - pos, "pmk_len=%zu\n", sm->pmk_len);
5456
0
    if (os_snprintf_error(end - pos, ret))
5457
0
      return pos - buf;
5458
0
    pos += ret;
5459
0
  }
5460
0
  if (sm->ptk.ptk_len) {
5461
0
    ret = os_snprintf(pos, end - pos, "ptk_len=%zu\n",
5462
0
          sm->ptk.ptk_len);
5463
0
    if (os_snprintf_error(end - pos, ret))
5464
0
      return pos - buf;
5465
0
    pos += ret;
5466
0
  }
5467
0
  if (sm->ptk.kck_len) {
5468
0
    ret = os_snprintf(pos, end - pos, "kck_len=%zu\n",
5469
0
          sm->ptk.kck_len);
5470
0
    if (os_snprintf_error(end - pos, ret))
5471
0
      return pos - buf;
5472
0
    pos += ret;
5473
0
  }
5474
0
  if (sm->ptk.kek_len) {
5475
0
    ret = os_snprintf(pos, end - pos, "kek_len=%zu\n",
5476
0
          sm->ptk.kek_len);
5477
0
    if (os_snprintf_error(end - pos, ret))
5478
0
      return pos - buf;
5479
0
    pos += ret;
5480
0
  }
5481
0
  if (sm->ptk.tk_len) {
5482
0
    ret = os_snprintf(pos, end - pos, "tk_len=%zu\n",
5483
0
          sm->ptk.tk_len);
5484
0
    if (os_snprintf_error(end - pos, ret))
5485
0
      return pos - buf;
5486
0
    pos += ret;
5487
0
  }
5488
0
  if (sm->ptk.kck2_len) {
5489
0
    ret = os_snprintf(pos, end - pos, "kck2_len=%zu\n",
5490
0
          sm->ptk.kck2_len);
5491
0
    if (os_snprintf_error(end - pos, ret))
5492
0
      return pos - buf;
5493
0
    pos += ret;
5494
0
  }
5495
0
  if (sm->ptk.kek2_len) {
5496
0
    ret = os_snprintf(pos, end - pos, "kek2_len=%zu\n",
5497
0
          sm->ptk.kek2_len);
5498
0
    if (os_snprintf_error(end - pos, ret))
5499
0
      return pos - buf;
5500
0
    pos += ret;
5501
0
  }
5502
0
  if (sm->ptk.kdk_len) {
5503
0
    ret = os_snprintf(pos, end - pos, "kdk_len=%zu\n",
5504
0
          sm->ptk.kdk_len);
5505
0
    if (os_snprintf_error(end - pos, ret))
5506
0
      return pos - buf;
5507
0
    pos += ret;
5508
0
  }
5509
0
  if (sm->ptk.ltf_keyseed_len) {
5510
0
    ret = os_snprintf(pos, end - pos, "ltf_keyseed_len=%zu\n",
5511
0
          sm->ptk.ltf_keyseed_len);
5512
0
    if (os_snprintf_error(end - pos, ret))
5513
0
      return pos - buf;
5514
0
    pos += ret;
5515
0
  }
5516
0
  mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len, sm->hash_alg,
5517
0
            sm->pasn_group);
5518
0
  if (mic_len) {
5519
0
    ret = os_snprintf(pos, end - pos, "mic_len=%zu\n", mic_len);
5520
0
    if (os_snprintf_error(end - pos, ret))
5521
0
      return pos - buf;
5522
0
    pos += ret;
5523
0
  }
5524
5525
0
  return pos - buf;
5526
0
}
5527
5528
5529
int wpa_sm_pmf_enabled(struct wpa_sm *sm)
5530
0
{
5531
0
  struct wpa_ie_data rsn;
5532
0
  const u8 *rsne;
5533
0
  size_t rsne_len;
5534
5535
0
  rsne = wpa_sm_get_ap_rsne(sm, &rsne_len);
5536
5537
0
  if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !rsne)
5538
0
    return 0;
5539
5540
0
  if (wpa_parse_wpa_ie_rsn(rsne, rsne_len, &rsn) >= 0 &&
5541
0
      rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
5542
0
    return 1;
5543
5544
0
  return 0;
5545
0
}
5546
5547
5548
bool wpa_sm_rsn_overriding_supported(struct wpa_sm *sm)
5549
0
{
5550
0
  const u8 *rsne;
5551
0
  size_t rsne_len;
5552
5553
0
  rsne = wpa_sm_get_ap_rsne(sm, &rsne_len);
5554
5555
0
  return sm->rsn_override_support && rsne;
5556
0
}
5557
5558
5559
int wpa_sm_ext_key_id(struct wpa_sm *sm)
5560
0
{
5561
0
  return sm ? sm->ext_key_id : 0;
5562
0
}
5563
5564
5565
int wpa_sm_ext_key_id_active(struct wpa_sm *sm)
5566
0
{
5567
0
  return sm ? sm->use_ext_key_id : 0;
5568
0
}
5569
5570
5571
int wpa_sm_ocv_enabled(struct wpa_sm *sm)
5572
0
{
5573
0
  struct wpa_ie_data rsn;
5574
0
  const u8 *rsne;
5575
0
  size_t rsne_len;
5576
5577
0
  rsne = wpa_sm_get_ap_rsne(sm, &rsne_len);
5578
0
  if (!sm->ocv || !rsne)
5579
0
    return 0;
5580
5581
0
  return wpa_parse_wpa_ie_rsn(rsne, rsne_len, &rsn) >= 0 &&
5582
0
    (rsn.capabilities & WPA_CAPABILITY_OCVC);
5583
0
}
5584
5585
5586
/**
5587
 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
5588
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
5589
 * @wpa_ie: Pointer to buffer for WPA/RSN IE
5590
 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
5591
 * Returns: 0 on success, -1 on failure
5592
 */
5593
int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
5594
            size_t *wpa_ie_len)
5595
0
{
5596
0
  int res;
5597
5598
0
  if (sm == NULL)
5599
0
    return -1;
5600
5601
#ifdef CONFIG_TESTING_OPTIONS
5602
  if (sm->test_assoc_ie) {
5603
    wpa_printf(MSG_DEBUG,
5604
         "TESTING: Replace association WPA/RSN IE");
5605
    if (*wpa_ie_len < wpabuf_len(sm->test_assoc_ie))
5606
      return -1;
5607
    os_memcpy(wpa_ie, wpabuf_head(sm->test_assoc_ie),
5608
        wpabuf_len(sm->test_assoc_ie));
5609
    res = wpabuf_len(sm->test_assoc_ie);
5610
  } else
5611
#endif /* CONFIG_TESTING_OPTIONS */
5612
0
  res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
5613
0
  if (res < 0)
5614
0
    return -1;
5615
0
  *wpa_ie_len = res;
5616
5617
0
  wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
5618
0
        wpa_ie, *wpa_ie_len);
5619
5620
0
  if (sm->assoc_wpa_ie == NULL) {
5621
    /*
5622
     * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
5623
     * the correct version of the IE even if PMKSA caching is
5624
     * aborted (which would remove PMKID from IE generation).
5625
     */
5626
0
    sm->assoc_wpa_ie = os_memdup(wpa_ie, *wpa_ie_len);
5627
0
    if (sm->assoc_wpa_ie == NULL)
5628
0
      return -1;
5629
5630
0
    sm->assoc_wpa_ie_len = *wpa_ie_len;
5631
0
  } else {
5632
0
    wpa_hexdump(MSG_DEBUG,
5633
0
          "WPA: Leave previously set WPA IE default",
5634
0
          sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
5635
0
  }
5636
5637
0
  return 0;
5638
0
}
5639
5640
5641
/**
5642
 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
5643
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
5644
 * @ie: Pointer to IE data (starting from id)
5645
 * @len: IE length
5646
 * Returns: 0 on success, -1 on failure
5647
 *
5648
 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
5649
 * Request frame. The IE will be used to override the default value generated
5650
 * with wpa_sm_set_assoc_wpa_ie_default().
5651
 */
5652
int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
5653
0
{
5654
0
  if (sm == NULL)
5655
0
    return -1;
5656
5657
0
  os_free(sm->assoc_wpa_ie);
5658
0
  if (ie == NULL || len == 0) {
5659
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
5660
0
      "WPA: clearing own WPA/RSN IE");
5661
0
    sm->assoc_wpa_ie = NULL;
5662
0
    sm->assoc_wpa_ie_len = 0;
5663
0
  } else {
5664
0
    wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
5665
0
    sm->assoc_wpa_ie = os_memdup(ie, len);
5666
0
    if (sm->assoc_wpa_ie == NULL)
5667
0
      return -1;
5668
5669
0
    sm->assoc_wpa_ie_len = len;
5670
0
  }
5671
5672
0
  return 0;
5673
0
}
5674
5675
5676
#ifdef CONFIG_TESTING_OPTIONS
5677
5678
int wpa_sm_set_test_rsnxe_data(struct wpa_sm *sm, struct wpabuf *data,
5679
             struct wpabuf *mask)
5680
{
5681
  size_t data_len = 0, mask_len = 0;
5682
5683
  wpabuf_free(sm->test_rsnxe_data);
5684
  sm->test_rsnxe_data = NULL;
5685
  wpabuf_free(sm->test_rsnxe_mask);
5686
  sm->test_rsnxe_mask = NULL;
5687
5688
  if (!data && !mask)
5689
    return 0;
5690
5691
  if (data)
5692
    data_len = wpabuf_len(data);
5693
  if (mask)
5694
    mask_len = wpabuf_len(mask);
5695
5696
  if (data_len != mask_len || data_len > 255)
5697
    return -1;
5698
5699
  sm->test_rsnxe_data = data;
5700
  sm->test_rsnxe_mask = mask;
5701
5702
  return 0;
5703
}
5704
5705
5706
static int wpa_set_test_rsnxe_data(struct wpa_sm *sm, u8 *rsnxe,
5707
           size_t orig_len, size_t max_len)
5708
{
5709
  const u8 *data, *mask;
5710
  size_t i, data_len;
5711
5712
  if (!sm->test_rsnxe_data || !sm->test_rsnxe_mask)
5713
    return orig_len;
5714
5715
  mask = wpabuf_head(sm->test_rsnxe_mask);
5716
  data = wpabuf_head(sm->test_rsnxe_data);
5717
  data_len = wpabuf_len(sm->test_rsnxe_data);
5718
  if (max_len < data_len + 2) {
5719
    wpa_printf(MSG_ERROR, "Couldn't fit RSNXE test data");
5720
    return -1;
5721
  }
5722
5723
  /* Set data after original RSNXE to zero */
5724
  if (orig_len < data_len + 2)
5725
    os_memset(&rsnxe[orig_len], 0, data_len + 2 - orig_len);
5726
5727
  /* Set EID and length fields */
5728
  *rsnxe++ = WLAN_EID_RSNX;
5729
  *rsnxe++ = data_len;
5730
5731
  /* Preserve original RSNXE bit value when mask bit is zero */
5732
  for (i = 0; i < data_len; i++) {
5733
    if (!mask[i])
5734
      continue;
5735
5736
    rsnxe[i] &= ~mask[i];
5737
    rsnxe[i] |= data[i] & mask[i];
5738
  }
5739
5740
  return data_len + 2;
5741
}
5742
5743
#endif /* CONFIG_TESTING_OPTIONS */
5744
5745
5746
/**
5747
 * wpa_sm_set_assoc_rsnxe_default - Generate own RSNXE from configuration
5748
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
5749
 * @rsnxe: Pointer to buffer for RSNXE
5750
 * @rsnxe_len: Pointer to the length of the rsne buffer
5751
 * Returns: 0 on success, -1 on failure
5752
 */
5753
int wpa_sm_set_assoc_rsnxe_default(struct wpa_sm *sm, u8 *rsnxe,
5754
           size_t *rsnxe_len)
5755
0
{
5756
0
  int res;
5757
5758
0
  if (!sm)
5759
0
    return -1;
5760
5761
0
  res = wpa_gen_rsnxe(sm, rsnxe, *rsnxe_len);
5762
0
  if (res < 0)
5763
0
    return -1;
5764
#ifdef CONFIG_TESTING_OPTIONS
5765
  res = wpa_set_test_rsnxe_data(sm, rsnxe, res, *rsnxe_len);
5766
  if (res < 0)
5767
    return -1;
5768
#endif /* CONFIG_TESTING_OPTIONS */
5769
0
  *rsnxe_len = res;
5770
5771
0
  wpa_hexdump(MSG_DEBUG, "RSN: Set own RSNXE default", rsnxe, *rsnxe_len);
5772
5773
0
  if (sm->assoc_rsnxe) {
5774
0
    wpa_hexdump(MSG_DEBUG,
5775
0
          "RSN: Leave previously set RSNXE default",
5776
0
          sm->assoc_rsnxe, sm->assoc_rsnxe_len);
5777
0
  } else if (*rsnxe_len > 0) {
5778
    /*
5779
     * Make a copy of the RSNXE so that 4-Way Handshake gets the
5780
     * correct version of the IE even if it gets changed.
5781
     */
5782
0
    sm->assoc_rsnxe = os_memdup(rsnxe, *rsnxe_len);
5783
0
    if (!sm->assoc_rsnxe)
5784
0
      return -1;
5785
5786
0
    sm->assoc_rsnxe_len = *rsnxe_len;
5787
0
  }
5788
5789
0
  return 0;
5790
0
}
5791
5792
5793
/**
5794
 * wpa_sm_set_assoc_rsnxe - Set own RSNXE from (Re)AssocReq
5795
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
5796
 * @ie: Pointer to IE data (starting from id)
5797
 * @len: IE length
5798
 * Returns: 0 on success, -1 on failure
5799
 *
5800
 * Inform WPA state machine about the RSNXE used in (Re)Association Request
5801
 * frame. The IE will be used to override the default value generated
5802
 * with wpa_sm_set_assoc_rsnxe_default().
5803
 */
5804
int wpa_sm_set_assoc_rsnxe(struct wpa_sm *sm, const u8 *ie, size_t len)
5805
0
{
5806
0
  if (!sm)
5807
0
    return -1;
5808
5809
0
  os_free(sm->assoc_rsnxe);
5810
0
  if (!ie || len == 0) {
5811
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
5812
0
      "RSN: clearing own RSNXE");
5813
0
    sm->assoc_rsnxe = NULL;
5814
0
    sm->assoc_rsnxe_len = 0;
5815
0
  } else {
5816
0
    wpa_hexdump(MSG_DEBUG, "RSN: set own RSNXE", ie, len);
5817
0
    sm->assoc_rsnxe = os_memdup(ie, len);
5818
0
    if (!sm->assoc_rsnxe)
5819
0
      return -1;
5820
5821
0
    sm->assoc_rsnxe_len = len;
5822
0
  }
5823
5824
0
  if (sm->ssid_protection &&
5825
0
      !ieee802_11_rsnx_capab(sm->assoc_rsnxe,
5826
0
           WLAN_RSNX_CAPAB_SSID_PROTECTION)) {
5827
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
5828
0
      "RSN: Disabling SSID protection based on own RSNXE update");
5829
0
    sm->ssid_protection = 0;
5830
0
  }
5831
5832
0
  return 0;
5833
0
}
5834
5835
5836
/**
5837
 * wpa_sm_set_security_profile - Set selected security profile for association
5838
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
5839
 * @sp: Pointer to the security profile entyr of %NULL for none
5840
 * Returns: 0 on success, -1 on failure
5841
 *
5842
 * Inform WPA state machine about the security profile that was selected for
5843
 * association.
5844
 */
5845
int wpa_sm_set_security_profile(struct wpa_sm *sm,
5846
        const struct security_profile_entry *sp)
5847
0
{
5848
0
  if (!sm)
5849
0
    return -1;
5850
5851
0
  sm->security_profile = sp;
5852
5853
0
  return 0;
5854
0
}
5855
5856
5857
/**
5858
 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
5859
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
5860
 * @ie: Pointer to IE data (starting from id)
5861
 * @len: IE length
5862
 * Returns: 0 on success, -1 on failure
5863
 *
5864
 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
5865
 * frame.
5866
 */
5867
int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
5868
0
{
5869
0
  if (sm == NULL)
5870
0
    return -1;
5871
5872
0
  os_free(sm->ap_wpa_ie);
5873
0
  if (ie == NULL || len == 0) {
5874
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
5875
0
      "WPA: clearing AP WPA IE");
5876
0
    sm->ap_wpa_ie = NULL;
5877
0
    sm->ap_wpa_ie_len = 0;
5878
0
  } else {
5879
0
    wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
5880
0
    sm->ap_wpa_ie = os_memdup(ie, len);
5881
0
    if (sm->ap_wpa_ie == NULL)
5882
0
      return -1;
5883
5884
0
    sm->ap_wpa_ie_len = len;
5885
0
  }
5886
5887
0
  return 0;
5888
0
}
5889
5890
5891
/**
5892
 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
5893
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
5894
 * @ie: Pointer to IE data (starting from id)
5895
 * @len: IE length
5896
 * Returns: 0 on success, -1 on failure
5897
 *
5898
 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
5899
 * frame.
5900
 */
5901
int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
5902
0
{
5903
0
  if (sm == NULL)
5904
0
    return -1;
5905
5906
0
  os_free(sm->ap_rsn_ie);
5907
0
  if (ie == NULL || len == 0) {
5908
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
5909
0
      "WPA: clearing AP RSN IE");
5910
0
    sm->ap_rsn_ie = NULL;
5911
0
    sm->ap_rsn_ie_len = 0;
5912
0
  } else {
5913
0
    wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
5914
0
    sm->ap_rsn_ie = os_memdup(ie, len);
5915
0
    if (sm->ap_rsn_ie == NULL)
5916
0
      return -1;
5917
5918
0
    sm->ap_rsn_ie_len = len;
5919
0
  }
5920
5921
0
  return 0;
5922
0
}
5923
5924
5925
/**
5926
 * wpa_sm_set_ap_rsnxe - Set AP RSNXE from Beacon/ProbeResp
5927
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
5928
 * @ie: Pointer to IE data (starting from id)
5929
 * @len: IE length
5930
 * Returns: 0 on success, -1 on failure
5931
 *
5932
 * Inform WPA state machine about the RSNXE used in Beacon / Probe Response
5933
 * frame.
5934
 */
5935
int wpa_sm_set_ap_rsnxe(struct wpa_sm *sm, const u8 *ie, size_t len)
5936
0
{
5937
0
  if (!sm)
5938
0
    return -1;
5939
5940
0
  os_free(sm->ap_rsnxe);
5941
0
  if (!ie || len == 0) {
5942
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: clearing AP RSNXE");
5943
0
    sm->ap_rsnxe = NULL;
5944
0
    sm->ap_rsnxe_len = 0;
5945
0
  } else {
5946
0
    wpa_hexdump(MSG_DEBUG, "WPA: set AP RSNXE", ie, len);
5947
0
    sm->ap_rsnxe = os_memdup(ie, len);
5948
0
    if (!sm->ap_rsnxe)
5949
0
      return -1;
5950
5951
0
    sm->ap_rsnxe_len = len;
5952
0
  }
5953
5954
0
  return 0;
5955
0
}
5956
5957
5958
int wpa_sm_set_ap_rsne_override(struct wpa_sm *sm, const u8 *ie, size_t len)
5959
0
{
5960
0
  if (!sm)
5961
0
    return -1;
5962
5963
0
  os_free(sm->ap_rsne_override);
5964
0
  if (!ie || len == 0) {
5965
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
5966
0
      "RSN: Clearing AP RSNE Override element");
5967
0
    sm->ap_rsne_override = NULL;
5968
0
    sm->ap_rsne_override_len = 0;
5969
0
  } else {
5970
0
    wpa_hexdump(MSG_DEBUG, "RSN: Set AP RSNE Override element",
5971
0
          ie, len);
5972
0
    sm->ap_rsne_override = os_memdup(ie, len);
5973
0
    if (!sm->ap_rsne_override)
5974
0
      return -1;
5975
5976
0
    sm->ap_rsne_override_len = len;
5977
0
  }
5978
5979
0
  return 0;
5980
0
}
5981
5982
5983
int wpa_sm_set_ap_rsne_override_2(struct wpa_sm *sm, const u8 *ie, size_t len)
5984
0
{
5985
0
  if (!sm)
5986
0
    return -1;
5987
5988
0
  os_free(sm->ap_rsne_override_2);
5989
0
  if (!ie || len == 0) {
5990
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
5991
0
      "RSN: Clearing AP RSNE Override 2 element");
5992
0
    sm->ap_rsne_override_2 = NULL;
5993
0
    sm->ap_rsne_override_2_len = 0;
5994
0
  } else {
5995
0
    wpa_hexdump(MSG_DEBUG, "RSN: Set AP RSNE Override 2 element",
5996
0
          ie, len);
5997
0
    sm->ap_rsne_override_2 = os_memdup(ie, len);
5998
0
    if (!sm->ap_rsne_override_2)
5999
0
      return -1;
6000
6001
0
    sm->ap_rsne_override_2_len = len;
6002
0
  }
6003
6004
0
  return 0;
6005
0
}
6006
6007
6008
int wpa_sm_set_ap_rsnxe_override(struct wpa_sm *sm, const u8 *ie, size_t len)
6009
0
{
6010
0
  if (!sm)
6011
0
    return -1;
6012
6013
0
  os_free(sm->ap_rsnxe_override);
6014
0
  if (!ie || len == 0) {
6015
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
6016
0
      "RSN: Clearing AP RSNXE Override element");
6017
0
    sm->ap_rsnxe_override = NULL;
6018
0
    sm->ap_rsnxe_override_len = 0;
6019
0
  } else {
6020
0
    wpa_hexdump(MSG_DEBUG, "RSN: Set AP RSNXE Override element",
6021
0
          ie, len);
6022
0
    sm->ap_rsnxe_override = os_memdup(ie, len);
6023
0
    if (!sm->ap_rsnxe_override)
6024
0
      return -1;
6025
6026
0
    sm->ap_rsnxe_override_len = len;
6027
0
  }
6028
6029
0
  return 0;
6030
0
}
6031
6032
6033
/**
6034
 * wpa_sm_set_ap_security_profile - Set AP Security Profile element from a Beacon/Probe Response frame
6035
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
6036
 * @elem: Pointer to the element (starting from id), or NULL to clear
6037
 * @len: Element length
6038
 * Returns: 0 on success, -1 on failure
6039
 *
6040
 * Inform the RSN state machine about the Security Profile element used in
6041
 * Beacon or Probe Response frames. The copy is stored and compared against the
6042
 * element received in EAPOL-Key msg 3/4 and encrypted (Re)Association
6043
 * Response frames.
6044
 */
6045
int wpa_sm_set_ap_security_profile(struct wpa_sm *sm, const u8 *elem,
6046
           size_t len)
6047
0
{
6048
0
  if (!sm)
6049
0
    return -1;
6050
6051
0
  os_free(sm->ap_security_profile);
6052
0
  if (!elem || len == 0) {
6053
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
6054
0
      "RSN: Clearing AP Security Profile element");
6055
0
    sm->ap_security_profile = NULL;
6056
0
    sm->ap_security_profile_len = 0;
6057
0
  } else {
6058
0
    wpa_hexdump(MSG_DEBUG, "RSN: Set AP Security Profile element",
6059
0
          elem, len);
6060
0
    sm->ap_security_profile = os_memdup(elem, len);
6061
0
    if (!sm->ap_security_profile) {
6062
0
      sm->ap_security_profile_len = 0;
6063
0
      return -1;
6064
0
    }
6065
6066
0
    sm->ap_security_profile_len = len;
6067
0
  }
6068
6069
0
  return 0;
6070
0
}
6071
6072
6073
/**
6074
 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
6075
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
6076
 * @data: Pointer to data area for parsing results
6077
 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
6078
 *
6079
 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
6080
 * parsed data into data.
6081
 */
6082
int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
6083
0
{
6084
0
  if (sm == NULL)
6085
0
    return -1;
6086
6087
0
  if (sm->assoc_wpa_ie == NULL) {
6088
0
    wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
6089
0
      "WPA: No WPA/RSN IE available from association info");
6090
0
    return -1;
6091
0
  }
6092
0
  if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
6093
0
    return -2;
6094
0
  return 0;
6095
0
}
6096
6097
6098
int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
6099
0
{
6100
0
  return pmksa_cache_list(sm->pmksa, buf, len);
6101
0
}
6102
6103
6104
struct rsn_pmksa_cache_entry * wpa_sm_pmksa_cache_head(struct wpa_sm *sm)
6105
0
{
6106
0
  return pmksa_cache_head(sm->pmksa);
6107
0
}
6108
6109
6110
struct rsn_pmksa_cache_entry *
6111
wpa_sm_pmksa_cache_add_entry(struct wpa_sm *sm,
6112
           struct rsn_pmksa_cache_entry * entry)
6113
0
{
6114
0
  return pmksa_cache_add_entry(sm->pmksa, entry);
6115
0
}
6116
6117
6118
void wpa_sm_pmksa_cache_add(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
6119
          const u8 *pmkid, const u8 *bssid,
6120
          const u8 *fils_cache_id)
6121
0
{
6122
0
  sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
6123
0
          bssid, sm->own_addr, sm->network_ctx,
6124
0
          sm->key_mgmt, fils_cache_id, 0,
6125
0
          RSN_HASH_NOT_SPECIFIED);
6126
0
}
6127
6128
6129
int wpa_sm_pmksa_exists(struct wpa_sm *sm, const u8 *bssid, const u8 *own_addr,
6130
      const void *network_ctx)
6131
0
{
6132
0
  if (sm->disable_pmksa_caching)
6133
0
    return 0;
6134
6135
0
  return pmksa_cache_get(sm->pmksa, bssid, own_addr, NULL, network_ctx,
6136
0
             0) != NULL;
6137
0
}
6138
6139
6140
struct rsn_pmksa_cache_entry * wpa_sm_pmksa_cache_get(struct wpa_sm *sm,
6141
                  const u8 *aa,
6142
                  const u8 *pmkid,
6143
                  const void *network_ctx,
6144
                  int akmp)
6145
0
{
6146
0
  if (sm->disable_pmksa_caching)
6147
0
    return NULL;
6148
6149
0
  return pmksa_cache_get(sm->pmksa, aa, sm->own_addr, pmkid, network_ctx,
6150
0
             akmp);
6151
0
}
6152
6153
6154
int wpa_sm_pmksa_get_pmk(struct wpa_sm *sm, const u8 *aa, const u8 **pmk,
6155
       size_t *pmk_len, const u8 **pmkid)
6156
0
{
6157
0
  struct rsn_pmksa_cache_entry *pmksa;
6158
6159
0
  pmksa = wpa_sm_pmksa_cache_get(sm, aa, NULL, NULL, 0);
6160
0
  if (!pmksa) {
6161
0
    wpa_printf(MSG_DEBUG, "RSN: Failed to get PMKSA for " MACSTR,
6162
0
         MAC2STR(aa));
6163
0
    return -1;
6164
0
  }
6165
6166
0
  *pmk = pmksa->pmk;
6167
0
  *pmk_len = pmksa->pmk_len;
6168
0
  *pmkid = pmksa->pmkid;
6169
0
  return 0;
6170
0
}
6171
6172
6173
void wpa_sm_pmksa_cache_remove(struct wpa_sm *sm,
6174
             struct rsn_pmksa_cache_entry *entry)
6175
0
{
6176
0
  if (sm && sm->pmksa)
6177
0
    pmksa_cache_remove(sm->pmksa, entry);
6178
0
}
6179
6180
6181
void wpa_sm_drop_sa(struct wpa_sm *sm)
6182
0
{
6183
0
  wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
6184
0
  wpa_sm_clear_ptk(sm);
6185
0
  sm->pmk_len = 0;
6186
0
  os_memset(sm->pmk, 0, sizeof(sm->pmk));
6187
0
#ifdef CONFIG_IEEE80211R
6188
0
  os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
6189
0
  sm->xxkey_len = 0;
6190
0
  os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0));
6191
0
  sm->pmk_r0_len = 0;
6192
0
  os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
6193
0
  sm->pmk_r1_len = 0;
6194
#ifdef CONFIG_PASN
6195
  os_free(sm->pasn_r1kh);
6196
  sm->pasn_r1kh = NULL;
6197
  sm->n_pasn_r1kh = 0;
6198
#endif /* CONFIG_PASN */
6199
0
#endif /* CONFIG_IEEE80211R */
6200
0
}
6201
6202
6203
#ifdef CONFIG_IEEE80211R
6204
bool wpa_sm_has_ft_keys(struct wpa_sm *sm, const u8 *md)
6205
0
{
6206
0
  if (!sm)
6207
0
    return false;
6208
0
  if (!wpa_key_mgmt_ft(sm->key_mgmt) ||
6209
0
      os_memcmp(md, sm->key_mobility_domain,
6210
0
          MOBILITY_DOMAIN_ID_LEN) != 0) {
6211
    /* Do not allow FT protocol to be used even if we were to have
6212
     * an PTK since the mobility domain has changed. */
6213
0
    return false;
6214
0
  }
6215
0
  return sm->ptk_set;
6216
0
}
6217
#endif /* CONFIG_IEEE80211R */
6218
6219
6220
int wpa_sm_has_ptk_installed(struct wpa_sm *sm)
6221
0
{
6222
0
  if (!sm)
6223
0
    return 0;
6224
0
  return sm->tk_set || sm->ptk.installed;
6225
0
}
6226
6227
6228
void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
6229
0
{
6230
0
  os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
6231
0
}
6232
6233
6234
void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
6235
0
{
6236
0
  pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0, false, NULL);
6237
0
}
6238
6239
6240
void wpa_sm_pmksa_cache_flush_addr(struct wpa_sm *sm, void *network_ctx,
6241
           const u8 *addr)
6242
0
{
6243
0
  pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0, false, addr);
6244
0
}
6245
6246
6247
void wpa_sm_external_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
6248
0
{
6249
0
  pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0, true, NULL);
6250
0
}
6251
6252
6253
#ifdef CONFIG_WNM
6254
int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
6255
0
{
6256
0
  u16 keyinfo;
6257
0
  u8 keylen;  /* plaintext key len */
6258
0
  u8 *key_rsc;
6259
6260
0
  if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
6261
0
    struct wpa_gtk_data gd;
6262
6263
0
    os_memset(&gd, 0, sizeof(gd));
6264
0
    keylen = wpa_cipher_key_len(sm->group_cipher);
6265
0
    gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
6266
0
    gd.alg = wpa_cipher_to_alg(sm->group_cipher);
6267
0
    if (gd.alg == WPA_ALG_NONE) {
6268
0
      wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
6269
0
      return -1;
6270
0
    }
6271
6272
0
    key_rsc = buf + 5;
6273
0
    keyinfo = WPA_GET_LE16(buf + 2);
6274
0
    gd.gtk_len = keylen;
6275
0
    if (gd.gtk_len != buf[4]) {
6276
0
      wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
6277
0
           gd.gtk_len, buf[4]);
6278
0
      return -1;
6279
0
    }
6280
0
    gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
6281
0
    gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
6282
0
             sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
6283
6284
0
    os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
6285
6286
0
    wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
6287
0
        gd.gtk, gd.gtk_len);
6288
0
    if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 1)) {
6289
0
      forced_memzero(&gd, sizeof(gd));
6290
0
      wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
6291
0
           "WNM mode");
6292
0
      return -1;
6293
0
    }
6294
0
    forced_memzero(&gd, sizeof(gd));
6295
0
  } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
6296
0
    const struct wpa_igtk_kde *igtk;
6297
6298
0
    igtk = (const struct wpa_igtk_kde *) (buf + 2);
6299
0
    if (wpa_supplicant_install_igtk(sm, igtk, 1) < 0)
6300
0
      return -1;
6301
0
  } else if (subelem_id == WNM_SLEEP_SUBELEM_BIGTK) {
6302
0
    const struct wpa_bigtk_kde *bigtk;
6303
6304
0
    bigtk = (const struct wpa_bigtk_kde *) (buf + 2);
6305
0
    if (sm->beacon_prot &&
6306
0
        wpa_supplicant_install_bigtk(sm, bigtk, 1) < 0)
6307
0
      return -1;
6308
0
  } else {
6309
0
    wpa_printf(MSG_DEBUG, "Unknown element id");
6310
0
    return -1;
6311
0
  }
6312
6313
0
  return 0;
6314
0
}
6315
#endif /* CONFIG_WNM */
6316
6317
6318
#ifdef CONFIG_P2P
6319
6320
int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
6321
{
6322
  if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
6323
    return -1;
6324
  os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
6325
  return 0;
6326
}
6327
6328
#endif /* CONFIG_P2P */
6329
6330
6331
void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter)
6332
0
{
6333
0
  if (rx_replay_counter == NULL)
6334
0
    return;
6335
6336
0
  os_memcpy(sm->rx_replay_counter, rx_replay_counter,
6337
0
      WPA_REPLAY_COUNTER_LEN);
6338
0
  sm->rx_replay_counter_set = 1;
6339
0
  wpa_printf(MSG_DEBUG, "Updated key replay counter");
6340
0
}
6341
6342
6343
void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm, enum rsn_hash_alg hash,
6344
          const u8 *ptk_kck, size_t ptk_kck_len,
6345
          const u8 *ptk_kek, size_t ptk_kek_len)
6346
0
{
6347
0
  if (ptk_kck && ptk_kck_len <= WPA_KCK_MAX_LEN) {
6348
0
    os_memcpy(sm->ptk.kck, ptk_kck, ptk_kck_len);
6349
0
    sm->ptk.kck_len = ptk_kck_len;
6350
0
    wpa_printf(MSG_DEBUG, "Updated PTK KCK");
6351
0
  }
6352
0
  if (ptk_kek && ptk_kek_len <= WPA_KEK_MAX_LEN) {
6353
0
    os_memcpy(sm->ptk.kek, ptk_kek, ptk_kek_len);
6354
0
    sm->ptk.kek_len = ptk_kek_len;
6355
0
    wpa_printf(MSG_DEBUG, "Updated PTK KEK");
6356
0
  }
6357
0
  sm->ptk.hash_alg = hash;
6358
0
  if (sm->hash_alg != hash) {
6359
0
    wpa_printf(MSG_DEBUG,
6360
0
         "Updated hash algorithm for PTKSA: %d -> %d",
6361
0
         sm->hash_alg, hash);
6362
0
    sm->hash_alg = hash;
6363
0
  }
6364
0
  sm->ptk_set = 1;
6365
0
}
6366
6367
6368
#ifdef CONFIG_TESTING_OPTIONS
6369
6370
void wpa_sm_set_ptk_tk(struct wpa_sm *sm, const u8 *ptk_tk, size_t ptk_tk_len)
6371
{
6372
  if (ptk_tk && ptk_tk_len <= WPA_TK_MAX_LEN) {
6373
    os_memcpy(sm->ptk.tk, ptk_tk, ptk_tk_len);
6374
    sm->ptk.tk_len = ptk_tk_len;
6375
    wpa_printf(MSG_DEBUG, "Updated PTK TK");
6376
  }
6377
}
6378
6379
6380
void wpa_sm_set_test_assoc_ie(struct wpa_sm *sm, struct wpabuf *buf)
6381
{
6382
  wpabuf_free(sm->test_assoc_ie);
6383
  sm->test_assoc_ie = buf;
6384
}
6385
6386
6387
void wpa_sm_set_test_eapol_m2_elems(struct wpa_sm *sm, struct wpabuf *buf)
6388
{
6389
  wpabuf_free(sm->test_eapol_m2_elems);
6390
  sm->test_eapol_m2_elems = buf;
6391
}
6392
6393
6394
void wpa_sm_set_test_eapol_m4_elems(struct wpa_sm *sm, struct wpabuf *buf)
6395
{
6396
  wpabuf_free(sm->test_eapol_m4_elems);
6397
  sm->test_eapol_m4_elems = buf;
6398
}
6399
6400
6401
const u8 * wpa_sm_get_anonce(struct wpa_sm *sm)
6402
{
6403
  return sm->anonce;
6404
}
6405
6406
6407
void wpa_sm_set_test_random_pmkid_count(struct wpa_sm *sm, unsigned int count)
6408
{
6409
  if (!sm)
6410
    return;
6411
  wpa_printf(MSG_DEBUG,
6412
       "TESTING: Set random PMKID injection count to %u", count);
6413
  sm->test_random_pmkid_count = count;
6414
}
6415
6416
#endif /* CONFIG_TESTING_OPTIONS */
6417
6418
6419
unsigned int wpa_sm_get_key_mgmt(struct wpa_sm *sm)
6420
0
{
6421
0
  return sm->key_mgmt;
6422
0
}
6423
6424
6425
const u8 * wpa_sm_get_auth_addr(struct wpa_sm *sm)
6426
0
{
6427
0
  return sm->mlo.valid_links ? sm->mlo.ap_mld_addr : sm->bssid;
6428
0
}
6429
6430
6431
#ifdef CONFIG_FILS
6432
6433
struct wpabuf * fils_build_auth(struct wpa_sm *sm, int dh_group, const u8 *md)
6434
{
6435
  struct wpabuf *buf = NULL;
6436
  struct wpabuf *erp_msg;
6437
  struct wpabuf *pub = NULL;
6438
6439
  erp_msg = eapol_sm_build_erp_reauth_start(sm->eapol);
6440
  if (!erp_msg && !sm->cur_pmksa) {
6441
    wpa_printf(MSG_DEBUG,
6442
         "FILS: Neither ERP EAP-Initiate/Re-auth nor PMKSA cache entry is available - skip FILS");
6443
    goto fail;
6444
  }
6445
6446
  wpa_printf(MSG_DEBUG, "FILS: Try to use FILS (erp=%d pmksa_cache=%d)",
6447
       erp_msg != NULL, sm->cur_pmksa != NULL);
6448
6449
  sm->fils_completed = 0;
6450
6451
  if (!sm->assoc_wpa_ie) {
6452
    wpa_printf(MSG_INFO, "FILS: No own RSN IE set for FILS");
6453
    goto fail;
6454
  }
6455
6456
  if (random_get_bytes(sm->fils_nonce, NONCE_LEN) < 0 ||
6457
      random_get_bytes(sm->fils_session, FILS_SESSION_LEN) < 0)
6458
    goto fail;
6459
6460
  wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Nonce",
6461
        sm->fils_nonce, NONCE_LEN);
6462
  wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Session",
6463
        sm->fils_session, FILS_SESSION_LEN);
6464
6465
#ifdef CONFIG_FILS_SK_PFS
6466
  sm->fils_dh_group = dh_group;
6467
  if (dh_group) {
6468
    crypto_ecdh_deinit(sm->fils_ecdh);
6469
    sm->fils_ecdh = crypto_ecdh_init(dh_group);
6470
    if (!sm->fils_ecdh) {
6471
      wpa_printf(MSG_INFO,
6472
           "FILS: Could not initialize ECDH with group %d",
6473
           dh_group);
6474
      goto fail;
6475
    }
6476
    pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
6477
    if (!pub)
6478
      goto fail;
6479
    wpa_hexdump_buf(MSG_DEBUG, "FILS: Element (DH public key)",
6480
        pub);
6481
    sm->fils_dh_elem_len = wpabuf_len(pub);
6482
  }
6483
#endif /* CONFIG_FILS_SK_PFS */
6484
6485
  buf = wpabuf_alloc(1000 + sm->assoc_wpa_ie_len +
6486
         (pub ? wpabuf_len(pub) : 0));
6487
  if (!buf)
6488
    goto fail;
6489
6490
  /* Fields following the Authentication algorithm number field */
6491
6492
  /* Authentication Transaction seq# */
6493
  wpabuf_put_le16(buf, 1);
6494
6495
  /* Status Code */
6496
  wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
6497
6498
  /* TODO: FILS PK */
6499
#ifdef CONFIG_FILS_SK_PFS
6500
  if (dh_group) {
6501
    /* Finite Cyclic Group */
6502
    wpabuf_put_le16(buf, dh_group);
6503
    /* Element */
6504
    wpabuf_put_buf(buf, pub);
6505
  }
6506
#endif /* CONFIG_FILS_SK_PFS */
6507
6508
  /* RSNE */
6509
  wpa_hexdump(MSG_DEBUG, "FILS: RSNE in FILS Authentication frame",
6510
        sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
6511
  wpabuf_put_data(buf, sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
6512
6513
  if (md) {
6514
    /* MDE when using FILS for FT initial association */
6515
    struct rsn_mdie *mdie;
6516
6517
    wpabuf_put_u8(buf, WLAN_EID_MOBILITY_DOMAIN);
6518
    wpabuf_put_u8(buf, sizeof(*mdie));
6519
    mdie = wpabuf_put(buf, sizeof(*mdie));
6520
    os_memcpy(mdie->mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
6521
    mdie->ft_capab = 0;
6522
  }
6523
6524
  /* FILS Nonce */
6525
  wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
6526
  wpabuf_put_u8(buf, 1 + NONCE_LEN); /* Length */
6527
  /* Element ID Extension */
6528
  wpabuf_put_u8(buf, WLAN_EID_EXT_NONCE);
6529
  wpabuf_put_data(buf, sm->fils_nonce, NONCE_LEN);
6530
6531
  /* FILS Session */
6532
  wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
6533
  wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
6534
  /* Element ID Extension */
6535
  wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
6536
  wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
6537
6538
  /* Wrapped Data */
6539
  sm->fils_erp_pmkid_set = 0;
6540
  if (erp_msg) {
6541
    wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
6542
    wpabuf_put_u8(buf, 1 + wpabuf_len(erp_msg)); /* Length */
6543
    /* Element ID Extension */
6544
    wpabuf_put_u8(buf, WLAN_EID_EXT_WRAPPED_DATA);
6545
    wpabuf_put_buf(buf, erp_msg);
6546
    /* Calculate pending PMKID here so that we do not need to
6547
     * maintain a copy of the EAP-Initiate/Reauth message. */
6548
    if (fils_pmkid_erp(sm->key_mgmt, wpabuf_head(erp_msg),
6549
           wpabuf_len(erp_msg),
6550
           sm->fils_erp_pmkid) == 0)
6551
      sm->fils_erp_pmkid_set = 1;
6552
  }
6553
6554
  wpa_hexdump_buf(MSG_DEBUG, "RSN: FILS fields for Authentication frame",
6555
      buf);
6556
6557
fail:
6558
  wpabuf_free(erp_msg);
6559
  wpabuf_free(pub);
6560
  return buf;
6561
}
6562
6563
6564
int fils_process_auth(struct wpa_sm *sm, const u8 *bssid, const u8 *data,
6565
          size_t len)
6566
{
6567
  const u8 *pos, *end;
6568
  struct ieee802_11_elems elems;
6569
  struct wpa_ie_data rsn;
6570
  int pmkid_match = 0;
6571
  u8 ick[FILS_ICK_MAX_LEN];
6572
  size_t ick_len;
6573
  int res;
6574
  struct wpabuf *dh_ss = NULL;
6575
  const u8 *g_sta = NULL;
6576
  size_t g_sta_len = 0;
6577
  const u8 *g_ap = NULL;
6578
  size_t g_ap_len = 0, kdk_len;
6579
  struct wpabuf *pub = NULL;
6580
#ifdef CONFIG_IEEE80211R
6581
  struct wpa_ft_ies parse;
6582
6583
  os_memset(&parse, 0, sizeof(parse));
6584
#endif /* CONFIG_IEEE80211R */
6585
6586
  os_memcpy(sm->bssid, bssid, ETH_ALEN);
6587
6588
  wpa_hexdump(MSG_DEBUG, "FILS: Authentication frame fields",
6589
        data, len);
6590
  pos = data;
6591
  end = data + len;
6592
6593
  /* TODO: FILS PK */
6594
#ifdef CONFIG_FILS_SK_PFS
6595
  if (sm->fils_dh_group) {
6596
    u16 group;
6597
6598
    /* Using FILS PFS */
6599
6600
    /* Finite Cyclic Group */
6601
    if (end - pos < 2) {
6602
      wpa_printf(MSG_DEBUG,
6603
           "FILS: No room for Finite Cyclic Group");
6604
      goto fail;
6605
    }
6606
    group = WPA_GET_LE16(pos);
6607
    pos += 2;
6608
    if (group != sm->fils_dh_group) {
6609
      wpa_printf(MSG_DEBUG,
6610
           "FILS: Unexpected change in Finite Cyclic Group: %u (expected %u)",
6611
           group, sm->fils_dh_group);
6612
      goto fail;
6613
    }
6614
6615
    /* Element */
6616
    if ((size_t) (end - pos) < sm->fils_dh_elem_len) {
6617
      wpa_printf(MSG_DEBUG, "FILS: No room for Element");
6618
      goto fail;
6619
    }
6620
6621
    if (!sm->fils_ecdh) {
6622
      wpa_printf(MSG_DEBUG, "FILS: No ECDH state available");
6623
      goto fail;
6624
    }
6625
    wpa_hexdump(MSG_DEBUG, "FILS: Peer ECDH public key",
6626
          pos, sm->fils_dh_elem_len);
6627
    dh_ss = crypto_ecdh_set_peerkey(sm->fils_ecdh, 1, pos,
6628
            sm->fils_dh_elem_len);
6629
    if (!dh_ss) {
6630
      wpa_printf(MSG_DEBUG, "FILS: ECDH operation failed");
6631
      goto fail;
6632
    }
6633
    wpa_hexdump_buf_key(MSG_DEBUG, "FILS: DH_SS", dh_ss);
6634
    g_ap = pos;
6635
    g_ap_len = sm->fils_dh_elem_len;
6636
    pos += sm->fils_dh_elem_len;
6637
  }
6638
#endif /* CONFIG_FILS_SK_PFS */
6639
6640
  wpa_hexdump(MSG_DEBUG, "FILS: Remaining IEs", pos, end - pos);
6641
  if (ieee802_11_parse_elems(pos, end - pos, &elems, 1) == ParseFailed) {
6642
    wpa_printf(MSG_DEBUG, "FILS: Could not parse elements");
6643
    goto fail;
6644
  }
6645
6646
  /* RSNE */
6647
  wpa_hexdump(MSG_DEBUG, "FILS: RSN element", elems.rsn_ie,
6648
        elems.rsn_ie_len);
6649
  if (!elems.rsn_ie ||
6650
      wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
6651
         &rsn) < 0) {
6652
    wpa_printf(MSG_DEBUG, "FILS: No RSN element");
6653
    goto fail;
6654
  }
6655
6656
  if (!elems.nonce) {
6657
    wpa_printf(MSG_DEBUG, "FILS: No FILS Nonce field");
6658
    goto fail;
6659
  }
6660
  os_memcpy(sm->fils_anonce, elems.nonce, NONCE_LEN);
6661
  wpa_hexdump(MSG_DEBUG, "FILS: ANonce", sm->fils_anonce, NONCE_LEN);
6662
6663
#ifdef CONFIG_IEEE80211R
6664
  if (wpa_key_mgmt_ft(sm->key_mgmt)) {
6665
    if (!elems.mdie || !elems.ftie) {
6666
      wpa_printf(MSG_DEBUG, "FILS+FT: No MDE or FTE");
6667
      goto fail;
6668
    }
6669
6670
    if (wpa_ft_parse_ies(pos, end - pos, &parse,
6671
             sm->key_mgmt, false) < 0) {
6672
      wpa_printf(MSG_DEBUG, "FILS+FT: Failed to parse IEs");
6673
      goto fail;
6674
    }
6675
6676
    if (!parse.r0kh_id) {
6677
      wpa_printf(MSG_DEBUG,
6678
           "FILS+FT: No R0KH-ID subelem in FTE");
6679
      goto fail;
6680
    }
6681
    os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len);
6682
    sm->r0kh_id_len = parse.r0kh_id_len;
6683
    wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
6684
          sm->r0kh_id, sm->r0kh_id_len);
6685
6686
    if (!parse.r1kh_id) {
6687
      wpa_printf(MSG_DEBUG,
6688
           "FILS+FT: No R1KH-ID subelem in FTE");
6689
      goto fail;
6690
    }
6691
    os_memcpy(sm->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN);
6692
    wpa_hexdump(MSG_DEBUG, "FILS+FT: R1KH-ID",
6693
          sm->r1kh_id, FT_R1KH_ID_LEN);
6694
6695
    /* TODO: Check MDE and FTE payload */
6696
6697
    wpabuf_free(sm->fils_ft_ies);
6698
    sm->fils_ft_ies = wpabuf_alloc(2 + elems.mdie_len +
6699
                 2 + elems.ftie_len);
6700
    if (!sm->fils_ft_ies)
6701
      goto fail;
6702
    wpabuf_put_data(sm->fils_ft_ies, elems.mdie - 2,
6703
        2 + elems.mdie_len);
6704
    wpabuf_put_data(sm->fils_ft_ies, elems.ftie - 2,
6705
        2 + elems.ftie_len);
6706
  } else {
6707
    wpabuf_free(sm->fils_ft_ies);
6708
    sm->fils_ft_ies = NULL;
6709
  }
6710
#endif /* CONFIG_IEEE80211R */
6711
6712
  /* PMKID List */
6713
  if (rsn.pmkid && rsn.num_pmkid > 0) {
6714
    wpa_hexdump(MSG_DEBUG, "FILS: PMKID List",
6715
          rsn.pmkid, rsn.num_pmkid * PMKID_LEN);
6716
6717
    if (!sm->cur_pmksa) {
6718
      wpa_printf(MSG_DEBUG,
6719
           "FILS: Unexpected PMKID in Authentication frame when no PMKSA was proposed");
6720
      goto fail;
6721
    }
6722
6723
    if (rsn.num_pmkid != 1) {
6724
      wpa_printf(MSG_DEBUG, "FILS: Invalid PMKID selection");
6725
      goto fail;
6726
    }
6727
    wpa_hexdump(MSG_DEBUG, "FILS: PMKID", rsn.pmkid, PMKID_LEN);
6728
    if (os_memcmp(sm->cur_pmksa->pmkid, rsn.pmkid, PMKID_LEN) != 0)
6729
    {
6730
      wpa_printf(MSG_DEBUG, "FILS: PMKID mismatch");
6731
      wpa_hexdump(MSG_DEBUG, "FILS: Expected PMKID",
6732
            sm->cur_pmksa->pmkid, PMKID_LEN);
6733
      goto fail;
6734
    }
6735
    wpa_printf(MSG_DEBUG,
6736
         "FILS: Matching PMKID - continue using PMKSA caching");
6737
    pmkid_match = 1;
6738
  }
6739
  if (!pmkid_match && sm->cur_pmksa) {
6740
    wpa_printf(MSG_DEBUG,
6741
         "FILS: No PMKID match - cannot use cached PMKSA entry");
6742
    sm->cur_pmksa = NULL;
6743
  }
6744
6745
  /* FILS Session */
6746
  if (!elems.fils_session) {
6747
    wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
6748
    goto fail;
6749
  }
6750
  wpa_hexdump(MSG_DEBUG, "FILS: FILS Session", elems.fils_session,
6751
        FILS_SESSION_LEN);
6752
  if (os_memcmp(sm->fils_session, elems.fils_session, FILS_SESSION_LEN)
6753
      != 0) {
6754
    wpa_printf(MSG_DEBUG, "FILS: Session mismatch");
6755
    wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
6756
          sm->fils_session, FILS_SESSION_LEN);
6757
    goto fail;
6758
  }
6759
6760
  /* Wrapped Data */
6761
  if (!sm->cur_pmksa && elems.wrapped_data) {
6762
    u8 rmsk[ERP_MAX_KEY_LEN];
6763
    size_t rmsk_len;
6764
6765
    wpa_hexdump(MSG_DEBUG, "FILS: Wrapped Data",
6766
          elems.wrapped_data,
6767
          elems.wrapped_data_len);
6768
    eapol_sm_process_erp_finish(sm->eapol, elems.wrapped_data,
6769
              elems.wrapped_data_len);
6770
    if (eapol_sm_failed(sm->eapol))
6771
      goto fail;
6772
6773
    rmsk_len = ERP_MAX_KEY_LEN;
6774
    res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
6775
    if (res == PMK_LEN) {
6776
      rmsk_len = PMK_LEN;
6777
      res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
6778
    }
6779
    if (res)
6780
      goto fail;
6781
6782
    res = fils_rmsk_to_pmk(sm->key_mgmt, rmsk, rmsk_len,
6783
               sm->fils_nonce, sm->fils_anonce,
6784
               dh_ss ? wpabuf_head(dh_ss) : NULL,
6785
               dh_ss ? wpabuf_len(dh_ss) : 0,
6786
               sm->pmk, &sm->pmk_len);
6787
    forced_memzero(rmsk, sizeof(rmsk));
6788
6789
    /* Don't use DHss in PTK derivation if PMKSA caching is not
6790
     * used. */
6791
    wpabuf_clear_free(dh_ss);
6792
    dh_ss = NULL;
6793
6794
    if (res)
6795
      goto fail;
6796
6797
    if (!sm->fils_erp_pmkid_set) {
6798
      wpa_printf(MSG_DEBUG, "FILS: PMKID not available");
6799
      goto fail;
6800
    }
6801
    wpa_hexdump(MSG_DEBUG, "FILS: PMKID", sm->fils_erp_pmkid,
6802
          PMKID_LEN);
6803
    wpa_printf(MSG_DEBUG, "FILS: ERP processing succeeded - add PMKSA cache entry for the result");
6804
    sm->cur_pmksa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len,
6805
            sm->fils_erp_pmkid, NULL, 0,
6806
            sm->bssid, sm->own_addr,
6807
            sm->network_ctx, sm->key_mgmt,
6808
            NULL, 0,
6809
            RSN_HASH_NOT_SPECIFIED);
6810
  }
6811
6812
  if (!sm->cur_pmksa) {
6813
    wpa_printf(MSG_DEBUG,
6814
         "FILS: No remaining options to continue FILS authentication");
6815
    goto fail;
6816
  }
6817
6818
  if (sm->force_kdk_derivation ||
6819
      (sm->secure_ltf &&
6820
       ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF)))
6821
    kdk_len = WPA_KDK_MAX_LEN;
6822
  else
6823
    kdk_len = 0;
6824
6825
  if (fils_pmk_to_ptk(sm->pmk, sm->pmk_len, sm->own_addr,
6826
          wpa_sm_get_auth_addr(sm),
6827
          sm->fils_nonce, sm->fils_anonce,
6828
          dh_ss ? wpabuf_head(dh_ss) : NULL,
6829
          dh_ss ? wpabuf_len(dh_ss) : 0,
6830
          &sm->ptk, ick, &ick_len,
6831
          sm->key_mgmt, sm->pairwise_cipher,
6832
          sm->fils_ft, &sm->fils_ft_len,
6833
          kdk_len) < 0) {
6834
    wpa_printf(MSG_DEBUG, "FILS: Failed to derive PTK");
6835
    goto fail;
6836
  }
6837
6838
#ifdef CONFIG_PASN
6839
  if (sm->secure_ltf &&
6840
      ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF) &&
6841
      wpa_ltf_keyseed(&sm->ptk, sm->key_mgmt, sm->pairwise_cipher)) {
6842
    wpa_printf(MSG_DEBUG, "FILS: Failed to derive LTF keyseed");
6843
    goto fail;
6844
  }
6845
#endif /* CONFIG_PASN */
6846
6847
  wpabuf_clear_free(dh_ss);
6848
  dh_ss = NULL;
6849
6850
  sm->ptk_set = 1;
6851
  sm->tptk_set = 0;
6852
  os_memset(&sm->tptk, 0, sizeof(sm->tptk));
6853
6854
#ifdef CONFIG_FILS_SK_PFS
6855
  if (sm->fils_dh_group) {
6856
    if (!sm->fils_ecdh) {
6857
      wpa_printf(MSG_INFO, "FILS: ECDH not initialized");
6858
      goto fail;
6859
    }
6860
    pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
6861
    if (!pub)
6862
      goto fail;
6863
    wpa_hexdump_buf(MSG_DEBUG, "FILS: gSTA", pub);
6864
    g_sta = wpabuf_head(pub);
6865
    g_sta_len = wpabuf_len(pub);
6866
    if (!g_ap) {
6867
      wpa_printf(MSG_INFO, "FILS: gAP not available");
6868
      goto fail;
6869
    }
6870
    wpa_hexdump(MSG_DEBUG, "FILS: gAP", g_ap, g_ap_len);
6871
  }
6872
#endif /* CONFIG_FILS_SK_PFS */
6873
6874
  res = fils_key_auth_sk(ick, ick_len, sm->fils_nonce,
6875
             sm->fils_anonce, sm->own_addr, sm->bssid,
6876
             g_sta, g_sta_len, g_ap, g_ap_len,
6877
             sm->key_mgmt, sm->fils_key_auth_sta,
6878
             sm->fils_key_auth_ap,
6879
             &sm->fils_key_auth_len);
6880
  wpabuf_free(pub);
6881
  forced_memzero(ick, sizeof(ick));
6882
#ifdef CONFIG_IEEE80211R
6883
  wpa_ft_parse_ies_free(&parse);
6884
#endif /* CONFIG_IEEE80211R */
6885
  return res;
6886
fail:
6887
  wpabuf_free(pub);
6888
  wpabuf_clear_free(dh_ss);
6889
#ifdef CONFIG_IEEE80211R
6890
  wpa_ft_parse_ies_free(&parse);
6891
#endif /* CONFIG_IEEE80211R */
6892
  return -1;
6893
}
6894
6895
6896
#ifdef CONFIG_IEEE80211R
6897
static int fils_ft_build_assoc_req_rsne(struct wpa_sm *sm, struct wpabuf *buf)
6898
{
6899
  struct rsn_ie_hdr *rsnie;
6900
  u16 capab;
6901
  u8 *pos;
6902
  int use_sha384 = wpa_key_mgmt_sha384(sm->key_mgmt);
6903
6904
  /* RSNIE[PMKR0Name/PMKR1Name] */
6905
  rsnie = wpabuf_put(buf, sizeof(*rsnie));
6906
  rsnie->elem_id = WLAN_EID_RSN;
6907
  WPA_PUT_LE16(rsnie->version, RSN_VERSION);
6908
6909
  /* Group Suite Selector */
6910
  if (!wpa_cipher_valid_group(sm->group_cipher)) {
6911
    wpa_printf(MSG_WARNING, "FT: Invalid group cipher (%d)",
6912
         sm->group_cipher);
6913
    return -1;
6914
  }
6915
  pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
6916
  RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
6917
              sm->group_cipher));
6918
6919
  /* Pairwise Suite Count */
6920
  wpabuf_put_le16(buf, 1);
6921
6922
  /* Pairwise Suite List */
6923
  if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
6924
    wpa_printf(MSG_WARNING, "FT: Invalid pairwise cipher (%d)",
6925
         sm->pairwise_cipher);
6926
    return -1;
6927
  }
6928
  pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
6929
  RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
6930
              sm->pairwise_cipher));
6931
6932
  /* Authenticated Key Management Suite Count */
6933
  wpabuf_put_le16(buf, 1);
6934
6935
  /* Authenticated Key Management Suite List */
6936
  pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
6937
  if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA256)
6938
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA256);
6939
  else if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA384)
6940
    RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA384);
6941
  else {
6942
    wpa_printf(MSG_WARNING,
6943
         "FILS+FT: Invalid key management type (%d)",
6944
         sm->key_mgmt);
6945
    return -1;
6946
  }
6947
6948
  /* RSN Capabilities */
6949
  capab = 0;
6950
  if (sm->mfp)
6951
    capab |= WPA_CAPABILITY_MFPC;
6952
  if (sm->mfp == 2)
6953
    capab |= WPA_CAPABILITY_MFPR;
6954
  if (sm->ocv)
6955
    capab |= WPA_CAPABILITY_OCVC;
6956
  if (sm->ext_key_id)
6957
    capab |= WPA_CAPABILITY_EXT_KEY_ID_FOR_UNICAST;
6958
  wpabuf_put_le16(buf, capab);
6959
6960
  /* PMKID Count */
6961
  wpabuf_put_le16(buf, 1);
6962
6963
  /* PMKID List [PMKR1Name] */
6964
  wpa_hexdump_key(MSG_DEBUG, "FILS+FT: XXKey (FILS-FT)",
6965
      sm->fils_ft, sm->fils_ft_len);
6966
  wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: SSID", sm->ssid, sm->ssid_len);
6967
  wpa_hexdump(MSG_DEBUG, "FILS+FT: MDID",
6968
        sm->mobility_domain, MOBILITY_DOMAIN_ID_LEN);
6969
  wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
6970
        sm->r0kh_id, sm->r0kh_id_len);
6971
  if (wpa_derive_pmk_r0(sm->fils_ft, sm->fils_ft_len, sm->ssid,
6972
            sm->ssid_len, sm->mobility_domain,
6973
            sm->r0kh_id, sm->r0kh_id_len, sm->own_addr,
6974
            sm->pmk_r0, sm->pmk_r0_name, sm->key_mgmt) < 0) {
6975
    wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMK-R0");
6976
    return -1;
6977
  }
6978
  if (wpa_key_mgmt_sae_ext_key(sm->key_mgmt))
6979
    sm->pmk_r0_len = sm->fils_ft_len;
6980
  else
6981
    sm->pmk_r0_len = use_sha384 ? SHA384_MAC_LEN : PMK_LEN;
6982
  wpa_printf(MSG_DEBUG, "FILS+FT: R1KH-ID: " MACSTR,
6983
       MAC2STR(sm->r1kh_id));
6984
  pos = wpabuf_put(buf, WPA_PMK_NAME_LEN);
6985
  if (wpa_derive_pmk_r1_name(sm->pmk_r0_name, sm->r1kh_id, sm->own_addr,
6986
           sm->pmk_r1_name, sm->fils_ft_len) < 0) {
6987
    wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMKR1Name");
6988
    return -1;
6989
  }
6990
  os_memcpy(pos, sm->pmk_r1_name, WPA_PMK_NAME_LEN);
6991
6992
  os_memcpy(sm->key_mobility_domain, sm->mobility_domain,
6993
      MOBILITY_DOMAIN_ID_LEN);
6994
6995
  if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) {
6996
    /* Management Group Cipher Suite */
6997
    pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
6998
    RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
6999
  }
7000
7001
  rsnie->len = ((u8 *) wpabuf_put(buf, 0) - (u8 *) rsnie) - 2;
7002
  return 0;
7003
}
7004
#endif /* CONFIG_IEEE80211R */
7005
7006
7007
struct wpabuf * fils_build_assoc_req(struct wpa_sm *sm, const u8 **kek,
7008
             size_t *kek_len, const u8 **snonce,
7009
             const u8 **anonce,
7010
             const struct wpabuf **hlp,
7011
             unsigned int num_hlp)
7012
{
7013
  struct wpabuf *buf;
7014
  size_t len;
7015
  unsigned int i;
7016
7017
  len = 1000;
7018
#ifdef CONFIG_IEEE80211R
7019
  if (sm->fils_ft_ies)
7020
    len += wpabuf_len(sm->fils_ft_ies);
7021
  if (wpa_key_mgmt_ft(sm->key_mgmt))
7022
    len += 256;
7023
#endif /* CONFIG_IEEE80211R */
7024
  for (i = 0; hlp && i < num_hlp; i++)
7025
    len += 10 + wpabuf_len(hlp[i]);
7026
  buf = wpabuf_alloc(len);
7027
  if (!buf)
7028
    return NULL;
7029
7030
#ifdef CONFIG_IEEE80211R
7031
  if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) {
7032
    /* MDE and FTE when using FILS+FT */
7033
    wpabuf_put_buf(buf, sm->fils_ft_ies);
7034
    /* RSNE with PMKR1Name in PMKID field */
7035
    if (fils_ft_build_assoc_req_rsne(sm, buf) < 0) {
7036
      wpabuf_free(buf);
7037
      return NULL;
7038
    }
7039
  }
7040
#endif /* CONFIG_IEEE80211R */
7041
7042
  /* FILS Session */
7043
  wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
7044
  wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
7045
  /* Element ID Extension */
7046
  wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
7047
  wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
7048
7049
  /* Everything after FILS Session element gets encrypted in the driver
7050
   * with KEK. The buffer returned from here is the plaintext version. */
7051
7052
  /* TODO: FILS Public Key */
7053
7054
  /* FILS Key Confirm */
7055
  wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
7056
  wpabuf_put_u8(buf, 1 + sm->fils_key_auth_len); /* Length */
7057
  /* Element ID Extension */
7058
  wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_KEY_CONFIRM);
7059
  wpabuf_put_data(buf, sm->fils_key_auth_sta, sm->fils_key_auth_len);
7060
7061
  /* FILS HLP Container */
7062
  for (i = 0; hlp && i < num_hlp; i++) {
7063
    const u8 *pos = wpabuf_head(hlp[i]);
7064
    size_t left = wpabuf_len(hlp[i]);
7065
7066
    wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
7067
    if (left <= 254)
7068
      len = 1 + left;
7069
    else
7070
      len = 255;
7071
    wpabuf_put_u8(buf, len); /* Length */
7072
    /* Element ID Extension */
7073
    wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_HLP_CONTAINER);
7074
    /* Destination MAC Address, Source MAC Address, HLP Packet.
7075
     * HLP Packet is in MSDU format (i.e., included the LLC/SNAP
7076
     * header when LPD is used). */
7077
    wpabuf_put_data(buf, pos, len - 1);
7078
    pos += len - 1;
7079
    left -= len - 1;
7080
    while (left) {
7081
      wpabuf_put_u8(buf, WLAN_EID_FRAGMENT);
7082
      len = left > 255 ? 255 : left;
7083
      wpabuf_put_u8(buf, len);
7084
      wpabuf_put_data(buf, pos, len);
7085
      pos += len;
7086
      left -= len;
7087
    }
7088
  }
7089
7090
  /* TODO: FILS IP Address Assignment */
7091
7092
#ifdef CONFIG_OCV
7093
  if (wpa_sm_ocv_enabled(sm)) {
7094
    struct wpa_channel_info ci;
7095
    u8 *pos;
7096
7097
    if (wpa_sm_channel_info(sm, &ci) != 0) {
7098
      wpa_printf(MSG_WARNING,
7099
           "FILS: Failed to get channel info for OCI element");
7100
      wpabuf_free(buf);
7101
      return NULL;
7102
    }
7103
#ifdef CONFIG_TESTING_OPTIONS
7104
    if (sm->oci_freq_override_fils_assoc) {
7105
      wpa_printf(MSG_INFO,
7106
           "TEST: Override OCI KDE frequency %d -> %d MHz",
7107
           ci.frequency,
7108
           sm->oci_freq_override_fils_assoc);
7109
      ci.frequency = sm->oci_freq_override_fils_assoc;
7110
    }
7111
#endif /* CONFIG_TESTING_OPTIONS */
7112
7113
    pos = wpabuf_put(buf, OCV_OCI_EXTENDED_LEN);
7114
    if (ocv_insert_extended_oci(&ci, pos) < 0) {
7115
      wpabuf_free(buf);
7116
      return NULL;
7117
    }
7118
  }
7119
#endif /* CONFIG_OCV */
7120
7121
  wpa_hexdump_buf(MSG_DEBUG, "FILS: Association Request plaintext", buf);
7122
7123
  *kek = sm->ptk.kek;
7124
  *kek_len = sm->ptk.kek_len;
7125
  wpa_hexdump_key(MSG_DEBUG, "FILS: KEK for AEAD", *kek, *kek_len);
7126
  *snonce = sm->fils_nonce;
7127
  wpa_hexdump(MSG_DEBUG, "FILS: SNonce for AEAD AAD", *snonce, NONCE_LEN);
7128
  *anonce = sm->fils_anonce;
7129
  wpa_hexdump(MSG_DEBUG, "FILS: ANonce for AEAD AAD", *anonce, NONCE_LEN);
7130
7131
  return buf;
7132
}
7133
7134
7135
static void fils_process_hlp_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
7136
{
7137
  const u8 *pos, *end;
7138
7139
  wpa_hexdump(MSG_MSGDUMP, "FILS: HLP response", resp, len);
7140
  if (len < 2 * ETH_ALEN)
7141
    return;
7142
  pos = resp + 2 * ETH_ALEN;
7143
  end = resp + len;
7144
  if (end - pos >= 6 &&
7145
      os_memcmp(pos, "\xaa\xaa\x03\x00\x00\x00", 6) == 0)
7146
    pos += 6; /* Remove SNAP/LLC header */
7147
  wpa_sm_fils_hlp_rx(sm, resp, resp + ETH_ALEN, pos, end - pos);
7148
}
7149
7150
7151
static void fils_process_hlp_container(struct wpa_sm *sm, const u8 *pos,
7152
               size_t len)
7153
{
7154
  const u8 *end = pos + len;
7155
  u8 *tmp, *tmp_pos;
7156
7157
  /* Check if there are any FILS HLP Container elements */
7158
  while (end - pos >= 2) {
7159
    if (2 + pos[1] > end - pos)
7160
      return;
7161
    if (pos[0] == WLAN_EID_EXTENSION &&
7162
        pos[1] >= 1 + 2 * ETH_ALEN &&
7163
        pos[2] == WLAN_EID_EXT_FILS_HLP_CONTAINER)
7164
      break;
7165
    pos += 2 + pos[1];
7166
  }
7167
  if (end - pos < 2)
7168
    return; /* No FILS HLP Container elements */
7169
7170
  tmp = os_malloc(end - pos);
7171
  if (!tmp)
7172
    return;
7173
7174
  while (end - pos >= 2) {
7175
    if (2 + pos[1] > end - pos ||
7176
        pos[0] != WLAN_EID_EXTENSION ||
7177
        pos[1] < 1 + 2 * ETH_ALEN ||
7178
        pos[2] != WLAN_EID_EXT_FILS_HLP_CONTAINER)
7179
      break;
7180
    tmp_pos = tmp;
7181
    os_memcpy(tmp_pos, pos + 3, pos[1] - 1);
7182
    tmp_pos += pos[1] - 1;
7183
    pos += 2 + pos[1];
7184
7185
    /* Add possible fragments */
7186
    while (end - pos >= 2 && pos[0] == WLAN_EID_FRAGMENT &&
7187
           2 + pos[1] <= end - pos) {
7188
      os_memcpy(tmp_pos, pos + 2, pos[1]);
7189
      tmp_pos += pos[1];
7190
      pos += 2 + pos[1];
7191
    }
7192
7193
    fils_process_hlp_resp(sm, tmp, tmp_pos - tmp);
7194
  }
7195
7196
  os_free(tmp);
7197
}
7198
7199
7200
int fils_process_assoc_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
7201
{
7202
  const struct ieee80211_mgmt *mgmt;
7203
  const u8 *end, *ie_start;
7204
  struct ieee802_11_elems elems;
7205
  int keylen, rsclen;
7206
  enum wpa_alg alg;
7207
  struct wpa_gtk_data gd;
7208
  int maxkeylen;
7209
  struct wpa_eapol_ie_parse kde;
7210
7211
  if (!sm || !sm->ptk_set) {
7212
    wpa_printf(MSG_DEBUG, "FILS: No KEK available");
7213
    return -1;
7214
  }
7215
7216
  if (!wpa_key_mgmt_fils(sm->key_mgmt)) {
7217
    wpa_printf(MSG_DEBUG, "FILS: Not a FILS AKM");
7218
    return -1;
7219
  }
7220
7221
  if (sm->fils_completed) {
7222
    wpa_printf(MSG_DEBUG,
7223
         "FILS: Association has already been completed for this FILS authentication - ignore unexpected retransmission");
7224
    return -1;
7225
  }
7226
7227
  wpa_hexdump(MSG_DEBUG, "FILS: (Re)Association Response frame",
7228
        resp, len);
7229
7230
  mgmt = (const struct ieee80211_mgmt *) resp;
7231
  if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_resp))
7232
    return -1;
7233
7234
  end = resp + len;
7235
  /* Same offset for Association Response and Reassociation Response */
7236
  ie_start = mgmt->u.assoc_resp.variable;
7237
7238
  if (ieee802_11_parse_elems(ie_start, end - ie_start, &elems, 1) ==
7239
      ParseFailed) {
7240
    wpa_printf(MSG_DEBUG,
7241
         "FILS: Failed to parse decrypted elements");
7242
    goto fail;
7243
  }
7244
7245
  if (!elems.fils_session) {
7246
    wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
7247
    return -1;
7248
  }
7249
  if (os_memcmp(elems.fils_session, sm->fils_session,
7250
          FILS_SESSION_LEN) != 0) {
7251
    wpa_printf(MSG_DEBUG, "FILS: FILS Session mismatch");
7252
    wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session",
7253
          elems.fils_session, FILS_SESSION_LEN);
7254
    wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
7255
          sm->fils_session, FILS_SESSION_LEN);
7256
  }
7257
7258
  if (!elems.rsn_ie) {
7259
    wpa_printf(MSG_DEBUG,
7260
         "FILS: No RSNE in (Re)Association Response");
7261
    /* As an interop workaround, allow this for now since IEEE Std
7262
     * 802.11ai-2016 did not include all the needed changes to make
7263
     * a FILS AP include RSNE in the frame. This workaround might
7264
     * eventually be removed and replaced with rejection (goto fail)
7265
     * to follow a strict interpretation of the standard. */
7266
  } else if (wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
7267
              sm->ap_rsn_ie, sm->ap_rsn_ie_len,
7268
              elems.rsn_ie - 2, elems.rsn_ie_len + 2)) {
7269
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
7270
      "FILS: RSNE mismatch between Beacon/Probe Response and (Re)Association Response");
7271
    wpa_hexdump(MSG_DEBUG, "FILS: RSNE in Beacon/Probe Response",
7272
          sm->ap_rsn_ie, sm->ap_rsn_ie_len);
7273
    wpa_hexdump(MSG_DEBUG, "FILS: RSNE in (Re)Association Response",
7274
          elems.rsn_ie, elems.rsn_ie_len);
7275
    goto fail;
7276
  }
7277
7278
  if ((sm->ap_rsnxe && !elems.rsnxe) ||
7279
      (!sm->ap_rsnxe && elems.rsnxe) ||
7280
      (sm->ap_rsnxe && elems.rsnxe && sm->ap_rsnxe_len >= 2 &&
7281
       (sm->ap_rsnxe_len != 2U + elems.rsnxe_len ||
7282
        os_memcmp(sm->ap_rsnxe + 2, elems.rsnxe, sm->ap_rsnxe_len - 2) !=
7283
        0))) {
7284
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
7285
      "FILS: RSNXE mismatch between Beacon/Probe Response and (Re)Association Response");
7286
    wpa_hexdump(MSG_INFO, "FILS: RSNXE in Beacon/Probe Response",
7287
          sm->ap_rsnxe, sm->ap_rsnxe_len);
7288
    wpa_hexdump(MSG_INFO, "RSNXE in (Re)Association Response",
7289
          elems.rsnxe, elems.rsnxe_len);
7290
    /* As an interop workaround, allow this for now if we did not
7291
     * include the RSNXE in (Re)Association Request frame since
7292
     * IEEE Std 802.11-2020 does not say anything about verifying
7293
     * the RSNXE in FILS cases and there have been hostapd releases
7294
     * that might omit the RSNXE in cases where the STA did not
7295
     * include it in the Association Request frame. This workaround
7296
     * might eventually be removed. */
7297
    if (sm->assoc_rsnxe && sm->assoc_rsnxe_len)
7298
      goto fail;
7299
  }
7300
7301
  /* TODO: FILS Public Key */
7302
7303
  if (!elems.fils_key_confirm) {
7304
    wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element");
7305
    goto fail;
7306
  }
7307
  if (elems.fils_key_confirm_len != sm->fils_key_auth_len) {
7308
    wpa_printf(MSG_DEBUG,
7309
         "FILS: Unexpected Key-Auth length %d (expected %d)",
7310
         elems.fils_key_confirm_len,
7311
         (int) sm->fils_key_auth_len);
7312
    goto fail;
7313
  }
7314
  if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_ap,
7315
          sm->fils_key_auth_len) != 0) {
7316
    wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch");
7317
    wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth",
7318
          elems.fils_key_confirm,
7319
          elems.fils_key_confirm_len);
7320
    wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth",
7321
          sm->fils_key_auth_ap, sm->fils_key_auth_len);
7322
    goto fail;
7323
  }
7324
7325
#ifdef CONFIG_OCV
7326
  if (wpa_sm_ocv_enabled(sm)) {
7327
    struct wpa_channel_info ci;
7328
7329
    if (wpa_sm_channel_info(sm, &ci) != 0) {
7330
      wpa_printf(MSG_WARNING,
7331
           "Failed to get channel info to validate received OCI in FILS (Re)Association Response frame");
7332
      goto fail;
7333
    }
7334
7335
    if (ocv_verify_tx_params(elems.oci, elems.oci_len, &ci,
7336
           channel_width_to_int(ci.chanwidth),
7337
           ci.seg1_idx) != OCI_SUCCESS) {
7338
      wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE
7339
        "addr=" MACSTR " frame=fils-assoc error=%s",
7340
        MAC2STR(sm->bssid), ocv_errorstr);
7341
      goto fail;
7342
    }
7343
  }
7344
#endif /* CONFIG_OCV */
7345
7346
#ifdef CONFIG_IEEE80211R
7347
  if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) {
7348
    struct wpa_ie_data rsn;
7349
7350
    /* Check that PMKR1Name derived by the AP matches */
7351
    if (!elems.rsn_ie ||
7352
        wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
7353
           &rsn) < 0 ||
7354
        !rsn.pmkid || rsn.num_pmkid != 1 ||
7355
        os_memcmp(rsn.pmkid, sm->pmk_r1_name,
7356
            WPA_PMK_NAME_LEN) != 0) {
7357
      wpa_printf(MSG_DEBUG,
7358
           "FILS+FT: No RSNE[PMKR1Name] match in AssocResp");
7359
      goto fail;
7360
    }
7361
  }
7362
#endif /* CONFIG_IEEE80211R */
7363
7364
  /* Key Delivery */
7365
  if (!elems.key_delivery) {
7366
    wpa_printf(MSG_DEBUG, "FILS: No Key Delivery element");
7367
    goto fail;
7368
  }
7369
7370
  /* Parse GTK and set the key to the driver */
7371
  os_memset(&gd, 0, sizeof(gd));
7372
  if (wpa_supplicant_parse_ies(elems.key_delivery + WPA_KEY_RSC_LEN,
7373
             elems.key_delivery_len - WPA_KEY_RSC_LEN,
7374
             &kde) < 0) {
7375
    wpa_printf(MSG_DEBUG, "FILS: Failed to parse KDEs");
7376
    goto fail;
7377
  }
7378
  if (!kde.gtk) {
7379
    wpa_printf(MSG_DEBUG, "FILS: No GTK KDE");
7380
    goto fail;
7381
  }
7382
  maxkeylen = gd.gtk_len = kde.gtk_len - 2;
7383
  if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
7384
                gd.gtk_len, maxkeylen,
7385
                &gd.key_rsc_len, &gd.alg))
7386
    goto fail;
7387
7388
  wpa_hexdump_key(MSG_DEBUG, "FILS: Received GTK", kde.gtk, kde.gtk_len);
7389
  gd.keyidx = kde.gtk[0] & 0x3;
7390
  gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
7391
                 !!(kde.gtk[0] & BIT(2)));
7392
  if (kde.gtk_len - 2 > sizeof(gd.gtk)) {
7393
    wpa_printf(MSG_DEBUG, "FILS: Too long GTK in GTK KDE (len=%lu)",
7394
         (unsigned long) kde.gtk_len - 2);
7395
    goto fail;
7396
  }
7397
  os_memcpy(gd.gtk, kde.gtk + 2, kde.gtk_len - 2);
7398
7399
  wpa_printf(MSG_DEBUG, "FILS: Set GTK to driver");
7400
  if (wpa_supplicant_install_gtk(sm, &gd, elems.key_delivery, 0) < 0) {
7401
    wpa_printf(MSG_DEBUG, "FILS: Failed to set GTK");
7402
    goto fail;
7403
  }
7404
7405
  if (ieee80211w_set_keys(sm, &kde) < 0) {
7406
    wpa_printf(MSG_DEBUG, "FILS: Failed to set IGTK");
7407
    goto fail;
7408
  }
7409
7410
  alg = wpa_cipher_to_alg(sm->pairwise_cipher);
7411
  keylen = wpa_cipher_key_len(sm->pairwise_cipher);
7412
  if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) {
7413
    wpa_printf(MSG_DEBUG, "FILS: TK length mismatch: %u != %lu",
7414
         keylen, (long unsigned int) sm->ptk.tk_len);
7415
    goto fail;
7416
  }
7417
7418
  rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
7419
  wpa_hexdump_key(MSG_DEBUG, "FILS: Set TK to driver",
7420
      sm->ptk.tk, keylen);
7421
  if (wpa_sm_set_key(sm, -1, alg, wpa_sm_get_auth_addr(sm), 0, 1,
7422
         null_rsc, rsclen,
7423
         sm->ptk.tk, keylen, KEY_FLAG_PAIRWISE_RX_TX) < 0) {
7424
    wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
7425
      "FILS: Failed to set PTK to the driver (alg=%d keylen=%d auth_addr="
7426
      MACSTR ")",
7427
      alg, keylen, MAC2STR(wpa_sm_get_auth_addr(sm)));
7428
    goto fail;
7429
  }
7430
7431
  wpa_sm_store_ptk(sm, wpa_sm_get_auth_addr(sm), sm->pairwise_cipher,
7432
       sm->dot11RSNAConfigPMKLifetime, &sm->ptk);
7433
7434
  /* TODO: TK could be cleared after auth frame exchange now that driver
7435
   * takes care of association frame encryption/decryption. */
7436
  /* TK is not needed anymore in supplicant */
7437
  os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
7438
  sm->ptk.tk_len = 0;
7439
  sm->ptk.installed = 1;
7440
  sm->tk_set = true;
7441
7442
  /* FILS HLP Container */
7443
  fils_process_hlp_container(sm, ie_start, end - ie_start);
7444
7445
  /* TODO: FILS IP Address Assignment */
7446
7447
  wpa_printf(MSG_DEBUG, "FILS: Auth+Assoc completed successfully");
7448
  sm->fils_completed = 1;
7449
  forced_memzero(&gd, sizeof(gd));
7450
7451
  if (kde.transition_disable)
7452
    wpa_sm_transition_disable(sm, kde.transition_disable[0]);
7453
7454
  return 0;
7455
fail:
7456
  forced_memzero(&gd, sizeof(gd));
7457
  return -1;
7458
}
7459
7460
7461
void wpa_sm_set_reset_fils_completed(struct wpa_sm *sm, int set)
7462
{
7463
  if (sm)
7464
    sm->fils_completed = !!set;
7465
}
7466
7467
#endif /* CONFIG_FILS */
7468
7469
7470
int wpa_fils_is_completed(struct wpa_sm *sm)
7471
0
{
7472
#ifdef CONFIG_FILS
7473
  return sm && sm->fils_completed;
7474
#else /* CONFIG_FILS */
7475
0
  return 0;
7476
0
#endif /* CONFIG_FILS */
7477
0
}
7478
7479
7480
#ifdef CONFIG_OWE
7481
7482
struct wpabuf * owe_build_assoc_req(struct wpa_sm *sm, u16 group)
7483
{
7484
  struct wpabuf *ie = NULL, *pub = NULL;
7485
  size_t prime_len;
7486
7487
  if (group == 19)
7488
    prime_len = 32;
7489
  else if (group == 20)
7490
    prime_len = 48;
7491
  else if (group == 21)
7492
    prime_len = 66;
7493
  else
7494
    return NULL;
7495
7496
  crypto_ecdh_deinit(sm->owe_ecdh);
7497
  sm->owe_ecdh = crypto_ecdh_init(group);
7498
  if (!sm->owe_ecdh)
7499
    goto fail;
7500
  sm->owe_group = group;
7501
  pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
7502
  pub = wpabuf_zeropad(pub, prime_len);
7503
  if (!pub)
7504
    goto fail;
7505
7506
  ie = wpabuf_alloc(5 + wpabuf_len(pub));
7507
  if (!ie)
7508
    goto fail;
7509
  wpabuf_put_u8(ie, WLAN_EID_EXTENSION);
7510
  wpabuf_put_u8(ie, 1 + 2 + wpabuf_len(pub));
7511
  wpabuf_put_u8(ie, WLAN_EID_EXT_OWE_DH_PARAM);
7512
  wpabuf_put_le16(ie, group);
7513
  wpabuf_put_buf(ie, pub);
7514
  wpabuf_free(pub);
7515
  wpa_hexdump_buf(MSG_DEBUG, "OWE: Diffie-Hellman Parameter element",
7516
      ie);
7517
7518
  return ie;
7519
fail:
7520
  wpabuf_free(pub);
7521
  crypto_ecdh_deinit(sm->owe_ecdh);
7522
  sm->owe_ecdh = NULL;
7523
  return NULL;
7524
}
7525
7526
7527
int owe_process_assoc_resp(struct wpa_sm *sm, const u8 *bssid,
7528
         const u8 *resp_ies, size_t resp_ies_len)
7529
{
7530
  struct ieee802_11_elems elems;
7531
  u16 group;
7532
  struct wpabuf *secret, *pub, *hkey;
7533
  int res;
7534
  u8 prk[SHA512_MAC_LEN], pmkid[SHA512_MAC_LEN];
7535
  const char *info = "OWE Key Generation";
7536
  const u8 *addr[2];
7537
  size_t len[2];
7538
  size_t hash_len, prime_len;
7539
  struct wpa_ie_data data;
7540
7541
  if (!resp_ies ||
7542
      ieee802_11_parse_elems(resp_ies, resp_ies_len, &elems, 1) ==
7543
      ParseFailed) {
7544
    wpa_printf(MSG_INFO,
7545
         "OWE: Could not parse Association Response frame elements");
7546
    return -1;
7547
  }
7548
7549
  if (sm->cur_pmksa && elems.rsn_ie &&
7550
      wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, 2 + elems.rsn_ie_len,
7551
         &data) == 0 &&
7552
      data.num_pmkid == 1 && data.pmkid &&
7553
      os_memcmp(sm->cur_pmksa->pmkid, data.pmkid, PMKID_LEN) == 0) {
7554
    wpa_printf(MSG_DEBUG, "OWE: Use PMKSA caching");
7555
    wpa_sm_set_pmk_from_pmksa(sm);
7556
    return 0;
7557
  }
7558
7559
  if (!elems.owe_dh) {
7560
    wpa_printf(MSG_INFO,
7561
         "OWE: No Diffie-Hellman Parameter element found in Association Response frame");
7562
    return -1;
7563
  }
7564
7565
  group = WPA_GET_LE16(elems.owe_dh);
7566
  if (group != sm->owe_group) {
7567
    wpa_printf(MSG_INFO,
7568
         "OWE: Unexpected Diffie-Hellman group in response: %u",
7569
         group);
7570
    return -1;
7571
  }
7572
7573
  if (!sm->owe_ecdh) {
7574
    wpa_printf(MSG_INFO, "OWE: No ECDH state available");
7575
    return -1;
7576
  }
7577
7578
  if (group == 19)
7579
    prime_len = 32;
7580
  else if (group == 20)
7581
    prime_len = 48;
7582
  else if (group == 21)
7583
    prime_len = 66;
7584
  else
7585
    return -1;
7586
7587
  wpa_hexdump(MSG_DEBUG, "OWE: Peer ECDH public key",
7588
        elems.owe_dh + 2, elems.owe_dh_len - 2);
7589
  secret = crypto_ecdh_set_peerkey(sm->owe_ecdh, 0,
7590
           elems.owe_dh + 2,
7591
           elems.owe_dh_len - 2);
7592
  secret = wpabuf_zeropad(secret, prime_len);
7593
  if (!secret) {
7594
    wpa_printf(MSG_DEBUG, "OWE: Invalid peer DH public key");
7595
    return -1;
7596
  }
7597
  wpa_hexdump_buf_key(MSG_DEBUG, "OWE: ECDH shared secret", secret);
7598
7599
  /* prk = HKDF-extract(C | A | group, z) */
7600
7601
  pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
7602
  if (!pub) {
7603
    wpabuf_clear_free(secret);
7604
    return -1;
7605
  }
7606
7607
  /* PMKID = Truncate-128(Hash(C | A)) */
7608
  addr[0] = wpabuf_head(pub);
7609
  len[0] = wpabuf_len(pub);
7610
  addr[1] = elems.owe_dh + 2;
7611
  len[1] = elems.owe_dh_len - 2;
7612
  if (group == 19) {
7613
    res = sha256_vector(2, addr, len, pmkid);
7614
    hash_len = SHA256_MAC_LEN;
7615
  } else if (group == 20) {
7616
    res = sha384_vector(2, addr, len, pmkid);
7617
    hash_len = SHA384_MAC_LEN;
7618
  } else if (group == 21) {
7619
    res = sha512_vector(2, addr, len, pmkid);
7620
    hash_len = SHA512_MAC_LEN;
7621
  } else {
7622
    res = -1;
7623
    hash_len = 0;
7624
  }
7625
  pub = wpabuf_zeropad(pub, prime_len);
7626
  if (res < 0 || !pub) {
7627
    wpabuf_free(pub);
7628
    wpabuf_clear_free(secret);
7629
    return -1;
7630
  }
7631
7632
  hkey = wpabuf_alloc(wpabuf_len(pub) + elems.owe_dh_len - 2 + 2);
7633
  if (!hkey) {
7634
    wpabuf_free(pub);
7635
    wpabuf_clear_free(secret);
7636
    return -1;
7637
  }
7638
7639
  wpabuf_put_buf(hkey, pub); /* C */
7640
  wpabuf_free(pub);
7641
  wpabuf_put_data(hkey, elems.owe_dh + 2, elems.owe_dh_len - 2); /* A */
7642
  wpabuf_put_le16(hkey, sm->owe_group); /* group */
7643
  if (group == 19)
7644
    res = hmac_sha256(wpabuf_head(hkey), wpabuf_len(hkey),
7645
          wpabuf_head(secret), wpabuf_len(secret), prk);
7646
  else if (group == 20)
7647
    res = hmac_sha384(wpabuf_head(hkey), wpabuf_len(hkey),
7648
          wpabuf_head(secret), wpabuf_len(secret), prk);
7649
  else if (group == 21)
7650
    res = hmac_sha512(wpabuf_head(hkey), wpabuf_len(hkey),
7651
          wpabuf_head(secret), wpabuf_len(secret), prk);
7652
  wpabuf_clear_free(hkey);
7653
  wpabuf_clear_free(secret);
7654
  if (res < 0)
7655
    return -1;
7656
7657
  wpa_hexdump_key(MSG_DEBUG, "OWE: prk", prk, hash_len);
7658
7659
  /* PMK = HKDF-expand(prk, "OWE Key Generation", n) */
7660
7661
  if (group == 19)
7662
    res = hmac_sha256_kdf(prk, hash_len, NULL, (const u8 *) info,
7663
              os_strlen(info), sm->pmk, hash_len);
7664
  else if (group == 20)
7665
    res = hmac_sha384_kdf(prk, hash_len, NULL, (const u8 *) info,
7666
              os_strlen(info), sm->pmk, hash_len);
7667
  else if (group == 21)
7668
    res = hmac_sha512_kdf(prk, hash_len, NULL, (const u8 *) info,
7669
              os_strlen(info), sm->pmk, hash_len);
7670
  forced_memzero(prk, SHA512_MAC_LEN);
7671
  if (res < 0) {
7672
    sm->pmk_len = 0;
7673
    return -1;
7674
  }
7675
  sm->pmk_len = hash_len;
7676
7677
  wpa_hexdump_key(MSG_DEBUG, "OWE: PMK", sm->pmk, sm->pmk_len);
7678
  wpa_hexdump(MSG_DEBUG, "OWE: PMKID", pmkid, PMKID_LEN);
7679
  pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, pmkid, NULL, 0,
7680
      bssid, sm->own_addr, sm->network_ctx, sm->key_mgmt,
7681
      NULL, 0, RSN_HASH_NOT_SPECIFIED);
7682
7683
  return 0;
7684
}
7685
7686
#endif /* CONFIG_OWE */
7687
7688
7689
void wpa_sm_set_fils_cache_id(struct wpa_sm *sm, const u8 *fils_cache_id)
7690
0
{
7691
#ifdef CONFIG_FILS
7692
  if (sm && fils_cache_id) {
7693
    sm->fils_cache_id_set = 1;
7694
    os_memcpy(sm->fils_cache_id, fils_cache_id, FILS_CACHE_ID_LEN);
7695
  }
7696
#endif /* CONFIG_FILS */
7697
0
}
7698
7699
7700
#ifdef CONFIG_DPP2
7701
void wpa_sm_set_dpp_z(struct wpa_sm *sm, const struct wpabuf *z)
7702
{
7703
  if (sm) {
7704
    wpabuf_clear_free(sm->dpp_z);
7705
    sm->dpp_z = z ? wpabuf_dup(z) : NULL;
7706
  }
7707
}
7708
#endif /* CONFIG_DPP2 */
7709
7710
7711
#ifdef CONFIG_PASN
7712
7713
void wpa_pasn_sm_set_caps(struct wpa_sm *sm, unsigned int flags2)
7714
{
7715
  if (flags2 & WPA_DRIVER_FLAGS2_SEC_LTF_STA)
7716
    sm->secure_ltf = 1;
7717
  if (flags2 & WPA_DRIVER_FLAGS2_SEC_RTT_STA)
7718
    sm->secure_rtt = 1;
7719
  if (flags2 & WPA_DRIVER_FLAGS2_PROT_RANGE_NEG_STA)
7720
    sm->prot_range_neg = 1;
7721
}
7722
7723
#endif /* CONFIG_PASN */
7724
7725
7726
void wpa_sm_pmksa_cache_reconfig(struct wpa_sm *sm)
7727
0
{
7728
0
  if (sm)
7729
0
    pmksa_cache_reconfig(sm->pmksa);
7730
0
}
7731
7732
7733
bool wpa_sm_uses_spp_amsdu(struct wpa_sm *sm)
7734
0
{
7735
0
  return sm ? sm->spp_amsdu : false;
7736
0
}
7737
7738
7739
struct rsn_pmksa_cache * wpa_sm_get_pmksa_cache(struct wpa_sm *sm)
7740
0
{
7741
0
  return sm ? sm->pmksa : NULL;
7742
0
}
7743
7744
7745
void wpa_sm_set_cur_pmksa(struct wpa_sm *sm,
7746
        struct rsn_pmksa_cache_entry *entry)
7747
0
{
7748
0
  if (sm)
7749
0
    sm->cur_pmksa = entry;
7750
0
}
7751
7752
7753
void wpa_sm_set_driver_bss_selection(struct wpa_sm *sm,
7754
             bool driver_bss_selection)
7755
0
{
7756
0
  if (sm)
7757
0
    sm->driver_bss_selection = driver_bss_selection;
7758
0
}
7759
7760
7761
struct wpabuf * wpa_sm_known_sta_identification(struct wpa_sm *sm, const u8 *aa,
7762
            u64 timestamp)
7763
0
{
7764
0
  struct wpabuf *ie;
7765
0
  unsigned int mic_len;
7766
0
  const u8 *start;
7767
0
  u8 *mic;
7768
7769
0
  if (!sm || sm->last_kck_len == 0)
7770
0
    return NULL;
7771
7772
0
  if (!ether_addr_equal(aa, sm->last_kck_aa))
7773
0
    return NULL;
7774
7775
0
  mic_len = wpa_mic_len(sm->last_kck_key_mgmt, sm->last_kck_pmk_len,
7776
0
            sm->hash_alg, sm->pasn_group);
7777
7778
0
  ie = wpabuf_alloc(3 + 8 + 1 + mic_len);
7779
0
  if (!ie)
7780
0
    return NULL;
7781
7782
0
  wpabuf_put_u8(ie, WLAN_EID_EXTENSION);
7783
0
  wpabuf_put_u8(ie, 1 + 8 + 1 + mic_len);
7784
0
  wpabuf_put_u8(ie, WLAN_EID_EXT_KNOWN_STA_IDENTIFICATION);
7785
0
  start = wpabuf_put(ie, 0);
7786
0
  wpabuf_put_le64(ie, timestamp);
7787
0
  wpabuf_put_u8(ie, mic_len);
7788
0
  mic = wpabuf_put(ie, mic_len);
7789
0
  if (wpa_eapol_key_mic(sm->last_kck, sm->last_kck_len,
7790
0
            sm->last_kck_key_mgmt, sm->hash_alg,
7791
0
            sm->last_kck_eapol_key_ver,
7792
0
            start, 8, mic, sm->pasn_group) < 0) {
7793
0
    wpabuf_free(ie);
7794
0
    return NULL;
7795
0
  }
7796
7797
0
  return ie;
7798
0
}
7799
7800
7801
static int mlo_ieee80211w_set_keys_for_new_links(struct wpa_sm *sm,
7802
             struct wpa_eapol_ie_parse *ie,
7803
             u16 added_links_bitmap)
7804
0
{
7805
0
  int i;
7806
7807
0
  if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) ||
7808
0
      sm->mgmt_group_cipher == WPA_CIPHER_GTK_NOT_USED)
7809
0
    return 0;
7810
7811
0
  for_each_link(added_links_bitmap, i) {
7812
0
    if (_mlo_ieee80211w_set_keys(sm, i, ie))
7813
0
      return -1;
7814
0
  }
7815
7816
0
  return 0;
7817
0
}
7818
7819
7820
static int wpa_supplicant_install_mlo_gtk_keys(struct wpa_sm *sm,
7821
                 struct wpa_eapol_ie_parse *ie,
7822
                 u16 added_links_bitmap)
7823
0
{
7824
0
  int i;
7825
7826
0
  for_each_link(added_links_bitmap, i) {
7827
0
    if (!ie->mlo_gtk[i]) {
7828
0
      wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
7829
0
        "MLO RSN: GTK not found for link ID %u", i);
7830
0
      return -1;
7831
0
    }
7832
7833
0
    if (wpa_supplicant_mlo_gtk(sm, i, ie->mlo_gtk[i],
7834
0
             ie->mlo_gtk_len[i], 0))
7835
0
      return -1;
7836
0
  }
7837
7838
0
  return 0;
7839
0
}
7840
7841
7842
int wpa_sm_install_mlo_group_keys(struct wpa_sm *sm, const u8 *key_data,
7843
          size_t key_data_len, u16 added_links_bitmap)
7844
0
{
7845
0
  struct wpa_eapol_ie_parse ie;
7846
7847
0
  if (!sm)
7848
0
    return -1;
7849
7850
0
  wpa_hexdump_key(MSG_DEBUG, "RSN: IE KeyData for MLO link reconfig",
7851
0
      key_data, key_data_len);
7852
7853
0
  if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
7854
0
    return -1;
7855
7856
0
  if (wpa_supplicant_install_mlo_gtk_keys(
7857
0
        sm, &ie, added_links_bitmap) < 0)
7858
0
    return -1;
7859
7860
0
  if (mlo_ieee80211w_set_keys_for_new_links(sm, &ie,
7861
0
              added_links_bitmap) < 0)
7862
0
    return -1;
7863
7864
0
  return 0;
7865
0
}
7866
7867
7868
#ifdef CONFIG_ENC_ASSOC
7869
7870
static int process_key_delivery_link(struct wpa_sm *sm, u8 i,
7871
             struct wpa_eapol_ie_parse *kde)
7872
{
7873
  if (wpa_supplicant_mlo_gtk(sm, i, kde->mlo_gtk[i],
7874
           kde->mlo_gtk_len[i], 0))
7875
    return -1;
7876
7877
  if (_mlo_ieee80211w_set_keys(sm, i, kde) < 0) {
7878
    wpa_printf(MSG_DEBUG,
7879
         "ENC_ASSOC: Failed to set MLO IGTK/BIGTK (link=%u)",
7880
         i);
7881
    return -1;
7882
  }
7883
7884
  return 0;
7885
}
7886
7887
7888
static int process_key_delivery_ml(struct wpa_sm *sm,
7889
           struct wpa_eapol_ie_parse *kde,
7890
           int valid_links)
7891
{
7892
  u8 i;
7893
7894
  for_each_link(valid_links, i) {
7895
    if (process_key_delivery_link(sm, i, kde) < 0)
7896
      return -1;
7897
  }
7898
7899
  return 0;
7900
}
7901
7902
7903
static int process_key_delivery(struct wpa_sm *sm,
7904
        struct wpa_eapol_ie_parse *kde,
7905
        const u8 *rsc)
7906
{
7907
  struct wpa_gtk_data gd;
7908
  int ret = -1;
7909
  int maxkeylen;
7910
7911
  os_memset(&gd, 0, sizeof(gd));
7912
7913
  if (!kde->gtk) {
7914
    wpa_printf(MSG_DEBUG, "ENC_ASSOC: No GTK KDE");
7915
    goto fail;
7916
  }
7917
7918
  maxkeylen = gd.gtk_len = kde->gtk_len - 2;
7919
  if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
7920
                gd.gtk_len, maxkeylen,
7921
                &gd.key_rsc_len, &gd.alg))
7922
    goto fail;
7923
7924
  wpa_hexdump_key(MSG_DEBUG, "ENC_ASSOC: Received GTK",
7925
      kde->gtk, kde->gtk_len);
7926
  gd.keyidx = kde->gtk[0] & 0x3;
7927
  if (kde->gtk_len - 2 > sizeof(gd.gtk)) {
7928
    wpa_printf(MSG_DEBUG,
7929
         "ENC_ASSOC: Too long GTK in GTK KDE (len=%zu)",
7930
         kde->gtk_len - 2);
7931
    goto fail;
7932
  }
7933
  os_memcpy(gd.gtk, kde->gtk + 2, kde->gtk_len - 2);
7934
7935
  wpa_printf(MSG_DEBUG, "ENC_ASSOC: Set GTK to driver");
7936
  if (wpa_supplicant_install_gtk(sm, &gd, rsc, 0) < 0) {
7937
    wpa_printf(MSG_DEBUG, "ENC_ASSOC: Failed to set GTK");
7938
    goto fail;
7939
  }
7940
7941
  if (ieee80211w_set_keys(sm, kde) < 0) {
7942
    wpa_printf(MSG_DEBUG, "ENC_ASSOC: Failed to set IGTK/BIGTK");
7943
    goto fail;
7944
  }
7945
7946
  ret = 0;
7947
fail:
7948
  forced_memzero(&gd, sizeof(gd));
7949
  return ret;
7950
}
7951
7952
7953
int process_encrypted_assoc_resp(struct wpa_sm *sm, int valid_links,
7954
         u16 auth_alg, const u8 *ies, size_t ies_len)
7955
{
7956
  struct ieee802_11_elems elems;
7957
  struct wpa_eapol_ie_parse kde;
7958
  const u8 *rsc;
7959
  struct wpabuf *buf = NULL;
7960
  int ret = -1;
7961
7962
  if (!sm)
7963
    return -1;
7964
7965
  wpa_sm_clear_enc_assoc_completed(sm);
7966
7967
  if (!sm->ptk_set) {
7968
    wpa_printf(MSG_DEBUG, "ENC_ASSOC: No KEK available");
7969
    return -1;
7970
  }
7971
7972
  wpa_hexdump_key(MSG_DEBUG, "ENC_ASSOC: (Re)Association Response frame",
7973
      ies, ies_len);
7974
7975
  if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
7976
    return -1;
7977
7978
  if (!elems.rsn_ie) {
7979
    wpa_printf(MSG_DEBUG,
7980
         "ENC_ASSOC: No RSNE in (Re)Association Response");
7981
    return -1;
7982
  }
7983
7984
  if (wpa_compare_rsn_ie(wpa_key_mgmt_sae(sm->key_mgmt) ||
7985
             wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt),
7986
             sm->ap_rsn_ie, sm->ap_rsn_ie_len,
7987
             elems.rsn_ie - 2, elems.rsn_ie_len + 2)) {
7988
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
7989
      "ENC_ASSOC: RSNE mismatch between Beacon/Probe Response and (Re)Association Response");
7990
    wpa_hexdump(MSG_DEBUG,
7991
          "ENC_ASSOC: RSNE in Beacon/Probe Response",
7992
          sm->ap_rsn_ie, sm->ap_rsn_ie_len);
7993
    wpa_hexdump(MSG_DEBUG,
7994
          "ENC_ASSOC: RSNE in (Re)Association Response",
7995
          elems.rsn_ie, elems.rsn_ie_len);
7996
    return -1;
7997
  }
7998
7999
  if ((sm->ap_rsnxe && !elems.rsnxe) ||
8000
      (!sm->ap_rsnxe && elems.rsnxe) ||
8001
      (sm->ap_rsnxe && elems.rsnxe && sm->ap_rsnxe_len >= 2 &&
8002
       (sm->ap_rsnxe_len != 2U + elems.rsnxe_len ||
8003
        os_memcmp(sm->ap_rsnxe + 2, elems.rsnxe, sm->ap_rsnxe_len - 2) !=
8004
        0))) {
8005
    wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
8006
      "ENC_ASSOC: RSNXE mismatch between Beacon/Probe Response and (Re)Association Response");
8007
    wpa_hexdump(MSG_DEBUG,
8008
          "ENC_ASSOC: RSNXE in Beacon/Probe Response",
8009
          sm->ap_rsnxe, sm->ap_rsnxe_len);
8010
    wpa_hexdump(MSG_DEBUG,
8011
          "ENC_ASSOC: RSNXE in (Re)Association Response",
8012
          elems.rsnxe, elems.rsnxe_len);
8013
    if (sm->assoc_rsnxe && sm->assoc_rsnxe_len)
8014
      return -1;
8015
  }
8016
8017
  if (sm->security_profile_active) {
8018
    const u8 *assoc_sp = elems.security_profile;
8019
    size_t assoc_sp_len = elems.security_profile_len;
8020
8021
    if (sm->ap_security_profile &&
8022
        sm->ap_security_profile_len > 2) {
8023
      if (!assoc_sp || assoc_sp_len == 0) {
8024
        wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
8025
          "ENC_ASSOC: Security Profile element present in Beacon/Probe Response frame but absent in (Re)Association Response frame");
8026
        wpa_hexdump(MSG_DEBUG,
8027
              "ENC_ASSOC: Security Profile element in Beacon/Probe Response frame",
8028
              sm->ap_security_profile,
8029
              sm->ap_security_profile_len);
8030
        return -1;
8031
      }
8032
      if (sm->ap_security_profile_len - 3 != assoc_sp_len ||
8033
          os_memcmp(sm->ap_security_profile + 3,
8034
              assoc_sp, assoc_sp_len) != 0) {
8035
        wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
8036
          "ENC_ASSOC: Security Profile element mismatch between Beacon/Probe Response frame and (Re)Association Response frame");
8037
        wpa_hexdump(MSG_DEBUG,
8038
              "ENC_ASSOC: Security Profile element in Beacon/Probe Response frame",
8039
              sm->ap_security_profile,
8040
              sm->ap_security_profile_len);
8041
        wpa_hexdump(MSG_DEBUG,
8042
              "ENC_ASSOC: Security Profile element in (Re)Association Response frame",
8043
              assoc_sp, assoc_sp_len);
8044
        return -1;
8045
      }
8046
      wpa_printf(MSG_DEBUG,
8047
           "ENC_ASSOC: Security Profile element in (Re)Association Response frame matches Beacon/Probe Response frame");
8048
    } else if (assoc_sp && assoc_sp_len > 0) {
8049
      wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
8050
        "ENC_ASSOC: Security Profile element present in (Re)Association Response frame but absent in Beacon/Probe Response frame");
8051
      wpa_hexdump(MSG_DEBUG,
8052
            "ENC_ASSOC: Security Profile element in (Re)Association Response frame",
8053
            assoc_sp, assoc_sp_len);
8054
      return -1;
8055
    }
8056
  }
8057
8058
  /* TODO: Check for RSNE/RSNXE mismatch for per-STA profile for MLO */
8059
8060
  /* Key Delivery element */
8061
  buf = ieee802_11_defrag(elems.key_delivery, elems.key_delivery_len,
8062
        true);
8063
  if (!buf) {
8064
    wpa_printf(MSG_DEBUG, "ENC_ASSOC: No Key Delivery element");
8065
    return -1;
8066
  }
8067
  elems.key_delivery = wpabuf_head(buf);
8068
  elems.key_delivery_len = wpabuf_len(buf);
8069
8070
  /* Parse Key Delivery element: RSC followed by group key KDEs */
8071
  rsc = elems.key_delivery;
8072
  if (wpa_supplicant_parse_ies(elems.key_delivery + WPA_KEY_RSC_LEN,
8073
             elems.key_delivery_len - WPA_KEY_RSC_LEN,
8074
             &kde) < 0) {
8075
    wpa_printf(MSG_DEBUG, "ENC_ASSOC: Failed to parse KDEs");
8076
    goto fail;
8077
  }
8078
8079
  if ((valid_links == -1 &&
8080
       process_key_delivery(sm, &kde, rsc) < 0) ||
8081
      (valid_links != -1 &&
8082
       process_key_delivery_ml(sm, &kde, valid_links) < 0))
8083
    goto fail;
8084
8085
  /* Process SAE Password Identifiers KDE if present in the encrypted
8086
   * (Re)Association Response frame (EPPKE path). */
8087
  if (kde.sae_pw_ids && wpa_key_mgmt_sae(sm->key_mgmt) &&
8088
      sm->sae_pw_id_change)
8089
    wpa_sm_sae_pw_id_change(sm, kde.sae_pw_ids,
8090
          kde.sae_pw_ids_len);
8091
8092
  wpa_sm_set_rekey_offload(sm);
8093
8094
  wpa_printf(MSG_DEBUG, "ENC_ASSOC: Association completed successfully");
8095
  sm->eppke_completed = auth_alg == WLAN_AUTH_EPPKE;
8096
  sm->eap_over_auth_frame_completed = auth_alg == WLAN_AUTH_802_1X;
8097
8098
  ret = 0;
8099
fail:
8100
  wpabuf_free(buf);
8101
  return ret;
8102
}
8103
8104
#endif /* CONFIG_ENC_ASSOC */
8105
8106
8107
bool wpa_eppke_is_completed(struct wpa_sm *sm)
8108
0
{
8109
#ifdef CONFIG_ENC_ASSOC
8110
  return sm && sm->eppke_completed;
8111
#else /* CONFIG_ENC_ASSOC */
8112
0
  return false;
8113
0
#endif /* CONFIG_ENC_ASSOC */
8114
0
}
8115
8116
8117
bool wpa_eap_over_auth_frame_is_completed(struct wpa_sm *sm)
8118
0
{
8119
#ifdef CONFIG_ENC_ASSOC
8120
  return sm && sm->eap_over_auth_frame_completed;
8121
#else /* CONFIG_ENC_ASSOC */
8122
0
  return false;
8123
0
#endif /* CONFIG_ENC_ASSOC */
8124
0
}
8125
8126
8127
/**
8128
 * wpa_sm_clear_enc_assoc_completed - Clear encrypted association state
8129
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
8130
 *
8131
 * This function is called on every (re)association indication before
8132
 * processing the (Re)Association Response frame so that the EPPKE and EAP
8133
 * over Authentication frame completion state from a previous association does
8134
 * not get used for the new association, e.g., when roaming without an
8135
 * explicit disassociation indication.
8136
 */
8137
void wpa_sm_clear_enc_assoc_completed(struct wpa_sm *sm)
8138
0
{
8139
#ifdef CONFIG_ENC_ASSOC
8140
  if (!sm)
8141
    return;
8142
  sm->eppke_completed = 0;
8143
  sm->eap_over_auth_frame_completed = 0;
8144
#endif /* CONFIG_ENC_ASSOC */
8145
0
}
8146
8147
8148
bool wpa_sm_pmksa_privacy_supported(struct wpa_sm *sm)
8149
0
{
8150
0
  return sm && sm->pmksa_privacy;
8151
0
}
8152
8153
8154
#ifdef CONFIG_IEEE8021X_AUTH
8155
8156
void wpa_sm_set_802_1x_auth_caps(struct wpa_sm *sm, u64 flags2)
8157
{
8158
  sm->eap_over_auth_frame =
8159
    !!(flags2 & WPA_DRIVER_FLAGS2_802_1X_AUTH);
8160
}
8161
8162
#endif /* CONFIG_IEEE8021X_AUTH */
8163
8164
8165
#ifdef CONFIG_TESTING_OPTIONS
8166
/**
8167
 * wpa_sm_get_cached_tk - Get cached TK for testing purposes
8168
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
8169
 * @tk: Buffer to store the TK
8170
 * @tk_len: Pointer to store the TK length
8171
 * Returns: 0 on success, -1 if TK is not available
8172
 *
8173
 * This function retrieves the cached TK from wpa_sm. The TK is only available
8174
 * if PTK is set and TK is not null.
8175
 */
8176
int wpa_sm_get_cached_tk(struct wpa_sm *sm, u8 *tk, size_t *tk_len)
8177
{
8178
  if (!sm || !sm->ptk_set ||
8179
      sm->ptk.tk_len == 0 || sm->ptk.tk_len > WPA_TK_MAX_LEN)
8180
    return -1;
8181
8182
  os_memcpy(tk, sm->ptk.tk, sm->ptk.tk_len);
8183
  *tk_len = sm->ptk.tk_len;
8184
8185
  return 0;
8186
}
8187
#endif /* CONFIG_TESTING_OPTIONS */