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