Coverage Report

Created: 2026-10-28 06:04

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