]>
Commit | Line | Data |
---|---|---|
6fc6879b JM |
1 | /* |
2 | * WPA/RSN - Shared functions for supplicant and authenticator | |
c22bb5bb | 3 | * Copyright (c) 2002-2018, Jouni Malinen <j@w1.fi> |
6fc6879b | 4 | * |
0f3d578e JM |
5 | * This software may be distributed under the terms of the BSD license. |
6 | * See README for more details. | |
6fc6879b JM |
7 | */ |
8 | ||
9 | #include "includes.h" | |
10 | ||
11 | #include "common.h" | |
03da66bd JM |
12 | #include "crypto/md5.h" |
13 | #include "crypto/sha1.h" | |
14 | #include "crypto/sha256.h" | |
5e3b5197 | 15 | #include "crypto/sha384.h" |
567da5bb | 16 | #include "crypto/sha512.h" |
03da66bd JM |
17 | #include "crypto/aes_wrap.h" |
18 | #include "crypto/crypto.h" | |
6fc6879b JM |
19 | #include "ieee802_11_defs.h" |
20 | #include "defs.h" | |
21 | #include "wpa_common.h" | |
22 | ||
23 | ||
567da5bb | 24 | static unsigned int wpa_kck_len(int akmp, size_t pmk_len) |
98cd3d1c | 25 | { |
94318a0d JM |
26 | switch (akmp) { |
27 | case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192: | |
c22bb5bb | 28 | case WPA_KEY_MGMT_FT_IEEE8021X_SHA384: |
5e3b5197 | 29 | return 24; |
94318a0d JM |
30 | case WPA_KEY_MGMT_FILS_SHA256: |
31 | case WPA_KEY_MGMT_FT_FILS_SHA256: | |
32 | case WPA_KEY_MGMT_FILS_SHA384: | |
33 | case WPA_KEY_MGMT_FT_FILS_SHA384: | |
34 | return 0; | |
567da5bb JM |
35 | case WPA_KEY_MGMT_DPP: |
36 | return pmk_len / 2; | |
6c472618 JM |
37 | case WPA_KEY_MGMT_OWE: |
38 | return pmk_len / 2; | |
94318a0d JM |
39 | default: |
40 | return 16; | |
41 | } | |
98cd3d1c JM |
42 | } |
43 | ||
44 | ||
2f373878 JM |
45 | #ifdef CONFIG_IEEE80211R |
46 | static unsigned int wpa_kck2_len(int akmp) | |
47 | { | |
48 | switch (akmp) { | |
49 | case WPA_KEY_MGMT_FT_FILS_SHA256: | |
50 | return 16; | |
51 | case WPA_KEY_MGMT_FT_FILS_SHA384: | |
52 | return 24; | |
53 | default: | |
54 | return 0; | |
55 | } | |
56 | } | |
57 | #endif /* CONFIG_IEEE80211R */ | |
58 | ||
59 | ||
567da5bb | 60 | static unsigned int wpa_kek_len(int akmp, size_t pmk_len) |
98cd3d1c | 61 | { |
94318a0d JM |
62 | switch (akmp) { |
63 | case WPA_KEY_MGMT_FILS_SHA384: | |
64 | case WPA_KEY_MGMT_FT_FILS_SHA384: | |
65 | return 64; | |
66 | case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192: | |
67 | case WPA_KEY_MGMT_FILS_SHA256: | |
68 | case WPA_KEY_MGMT_FT_FILS_SHA256: | |
c22bb5bb | 69 | case WPA_KEY_MGMT_FT_IEEE8021X_SHA384: |
5e3b5197 | 70 | return 32; |
567da5bb JM |
71 | case WPA_KEY_MGMT_DPP: |
72 | return pmk_len <= 32 ? 16 : 32; | |
6c472618 JM |
73 | case WPA_KEY_MGMT_OWE: |
74 | return pmk_len <= 32 ? 16 : 32; | |
94318a0d JM |
75 | default: |
76 | return 16; | |
77 | } | |
5e3b5197 JM |
78 | } |
79 | ||
80 | ||
2f373878 JM |
81 | #ifdef CONFIG_IEEE80211R |
82 | static unsigned int wpa_kek2_len(int akmp) | |
83 | { | |
84 | switch (akmp) { | |
85 | case WPA_KEY_MGMT_FT_FILS_SHA256: | |
86 | return 16; | |
87 | case WPA_KEY_MGMT_FT_FILS_SHA384: | |
88 | return 32; | |
89 | default: | |
90 | return 0; | |
91 | } | |
92 | } | |
93 | #endif /* CONFIG_IEEE80211R */ | |
94 | ||
95 | ||
567da5bb | 96 | unsigned int wpa_mic_len(int akmp, size_t pmk_len) |
5e3b5197 | 97 | { |
94318a0d JM |
98 | switch (akmp) { |
99 | case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192: | |
c22bb5bb | 100 | case WPA_KEY_MGMT_FT_IEEE8021X_SHA384: |
5e3b5197 | 101 | return 24; |
94318a0d JM |
102 | case WPA_KEY_MGMT_FILS_SHA256: |
103 | case WPA_KEY_MGMT_FILS_SHA384: | |
104 | case WPA_KEY_MGMT_FT_FILS_SHA256: | |
105 | case WPA_KEY_MGMT_FT_FILS_SHA384: | |
106 | return 0; | |
567da5bb JM |
107 | case WPA_KEY_MGMT_DPP: |
108 | return pmk_len / 2; | |
6c472618 JM |
109 | case WPA_KEY_MGMT_OWE: |
110 | return pmk_len / 2; | |
94318a0d JM |
111 | default: |
112 | return 16; | |
113 | } | |
98cd3d1c JM |
114 | } |
115 | ||
116 | ||
4bc801ab JM |
117 | /** |
118 | * wpa_use_akm_defined - Is AKM-defined Key Descriptor Version used | |
119 | * @akmp: WPA_KEY_MGMT_* used in key derivation | |
120 | * Returns: 1 if AKM-defined Key Descriptor Version is used; 0 otherwise | |
121 | */ | |
122 | int wpa_use_akm_defined(int akmp) | |
123 | { | |
124 | return akmp == WPA_KEY_MGMT_OSEN || | |
125 | akmp == WPA_KEY_MGMT_OWE || | |
126 | akmp == WPA_KEY_MGMT_DPP || | |
c22bb5bb | 127 | akmp == WPA_KEY_MGMT_FT_IEEE8021X_SHA384 || |
4bc801ab JM |
128 | wpa_key_mgmt_sae(akmp) || |
129 | wpa_key_mgmt_suite_b(akmp) || | |
130 | wpa_key_mgmt_fils(akmp); | |
131 | } | |
132 | ||
133 | ||
134 | /** | |
135 | * wpa_use_cmac - Is CMAC integrity algorithm used for EAPOL-Key MIC | |
136 | * @akmp: WPA_KEY_MGMT_* used in key derivation | |
137 | * Returns: 1 if CMAC is used; 0 otherwise | |
138 | */ | |
139 | int wpa_use_cmac(int akmp) | |
140 | { | |
141 | return akmp == WPA_KEY_MGMT_OSEN || | |
142 | akmp == WPA_KEY_MGMT_OWE || | |
143 | akmp == WPA_KEY_MGMT_DPP || | |
144 | wpa_key_mgmt_ft(akmp) || | |
145 | wpa_key_mgmt_sha256(akmp) || | |
146 | wpa_key_mgmt_sae(akmp) || | |
147 | wpa_key_mgmt_suite_b(akmp); | |
148 | } | |
149 | ||
150 | ||
151 | /** | |
152 | * wpa_use_aes_key_wrap - Is AES Keywrap algorithm used for EAPOL-Key Key Data | |
153 | * @akmp: WPA_KEY_MGMT_* used in key derivation | |
154 | * Returns: 1 if AES Keywrap is used; 0 otherwise | |
155 | * | |
156 | * Note: AKM 00-0F-AC:1 and 00-0F-AC:2 have special rules for selecting whether | |
157 | * to use AES Keywrap based on the negotiated pairwise cipher. This function | |
158 | * does not cover those special cases. | |
159 | */ | |
160 | int wpa_use_aes_key_wrap(int akmp) | |
161 | { | |
162 | return akmp == WPA_KEY_MGMT_OSEN || | |
163 | akmp == WPA_KEY_MGMT_OWE || | |
164 | akmp == WPA_KEY_MGMT_DPP || | |
165 | wpa_key_mgmt_ft(akmp) || | |
166 | wpa_key_mgmt_sha256(akmp) || | |
167 | wpa_key_mgmt_sae(akmp) || | |
168 | wpa_key_mgmt_suite_b(akmp); | |
169 | } | |
170 | ||
171 | ||
6fc6879b JM |
172 | /** |
173 | * wpa_eapol_key_mic - Calculate EAPOL-Key MIC | |
174 | * @key: EAPOL-Key Key Confirmation Key (KCK) | |
98cd3d1c | 175 | * @key_len: KCK length in octets |
929a2ea5 | 176 | * @akmp: WPA_KEY_MGMT_* used in key derivation |
6fc6879b JM |
177 | * @ver: Key descriptor version (WPA_KEY_INFO_TYPE_*) |
178 | * @buf: Pointer to the beginning of the EAPOL header (version field) | |
179 | * @len: Length of the EAPOL frame (from EAPOL header to the end of the frame) | |
180 | * @mic: Pointer to the buffer to which the EAPOL-Key MIC is written | |
181 | * Returns: 0 on success, -1 on failure | |
182 | * | |
183 | * Calculate EAPOL-Key MIC for an EAPOL-Key packet. The EAPOL-Key MIC field has | |
184 | * to be cleared (all zeroes) when calling this function. | |
185 | * | |
186 | * Note: 'IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames' has an error in the | |
187 | * description of the Key MIC calculation. It includes packet data from the | |
188 | * beginning of the EAPOL-Key header, not EAPOL header. This incorrect change | |
189 | * happened during final editing of the standard and the correct behavior is | |
190 | * defined in the last draft (IEEE 802.11i/D10). | |
191 | */ | |
98cd3d1c JM |
192 | int wpa_eapol_key_mic(const u8 *key, size_t key_len, int akmp, int ver, |
193 | const u8 *buf, size_t len, u8 *mic) | |
6fc6879b | 194 | { |
567da5bb | 195 | u8 hash[SHA512_MAC_LEN]; |
6fc6879b | 196 | |
a6ea6653 JM |
197 | if (key_len == 0) { |
198 | wpa_printf(MSG_DEBUG, | |
199 | "WPA: KCK not set - cannot calculate MIC"); | |
200 | return -1; | |
201 | } | |
202 | ||
6fc6879b | 203 | switch (ver) { |
6e6909a9 | 204 | #ifndef CONFIG_FIPS |
6fc6879b | 205 | case WPA_KEY_INFO_TYPE_HMAC_MD5_RC4: |
ef9627cb | 206 | wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key MIC using HMAC-MD5"); |
98cd3d1c | 207 | return hmac_md5(key, key_len, buf, len, mic); |
6e6909a9 | 208 | #endif /* CONFIG_FIPS */ |
6fc6879b | 209 | case WPA_KEY_INFO_TYPE_HMAC_SHA1_AES: |
ef9627cb | 210 | wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key MIC using HMAC-SHA1"); |
98cd3d1c | 211 | if (hmac_sha1(key, key_len, buf, len, hash)) |
04b6b3ed | 212 | return -1; |
6fc6879b JM |
213 | os_memcpy(mic, hash, MD5_MAC_LEN); |
214 | break; | |
a20088e5 | 215 | #if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W) |
6fc6879b | 216 | case WPA_KEY_INFO_TYPE_AES_128_CMAC: |
ef9627cb | 217 | wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key MIC using AES-CMAC"); |
6fc6879b | 218 | return omac1_aes_128(key, buf, len, mic); |
a20088e5 | 219 | #endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */ |
a5d75636 | 220 | case WPA_KEY_INFO_TYPE_AKM_DEFINED: |
929a2ea5 | 221 | switch (akmp) { |
4bc801ab JM |
222 | #ifdef CONFIG_SAE |
223 | case WPA_KEY_MGMT_SAE: | |
224 | case WPA_KEY_MGMT_FT_SAE: | |
225 | wpa_printf(MSG_DEBUG, | |
226 | "WPA: EAPOL-Key MIC using AES-CMAC (AKM-defined - SAE)"); | |
227 | return omac1_aes_128(key, buf, len, mic); | |
228 | #endif /* CONFIG_SAE */ | |
929a2ea5 JM |
229 | #ifdef CONFIG_HS20 |
230 | case WPA_KEY_MGMT_OSEN: | |
ef9627cb JM |
231 | wpa_printf(MSG_DEBUG, |
232 | "WPA: EAPOL-Key MIC using AES-CMAC (AKM-defined - OSEN)"); | |
929a2ea5 | 233 | return omac1_aes_128(key, buf, len, mic); |
a5d75636 | 234 | #endif /* CONFIG_HS20 */ |
929a2ea5 JM |
235 | #ifdef CONFIG_SUITEB |
236 | case WPA_KEY_MGMT_IEEE8021X_SUITE_B: | |
ef9627cb JM |
237 | wpa_printf(MSG_DEBUG, |
238 | "WPA: EAPOL-Key MIC using HMAC-SHA256 (AKM-defined - Suite B)"); | |
98cd3d1c | 239 | if (hmac_sha256(key, key_len, buf, len, hash)) |
929a2ea5 JM |
240 | return -1; |
241 | os_memcpy(mic, hash, MD5_MAC_LEN); | |
242 | break; | |
243 | #endif /* CONFIG_SUITEB */ | |
5e3b5197 JM |
244 | #ifdef CONFIG_SUITEB192 |
245 | case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192: | |
ef9627cb JM |
246 | wpa_printf(MSG_DEBUG, |
247 | "WPA: EAPOL-Key MIC using HMAC-SHA384 (AKM-defined - Suite B 192-bit)"); | |
5e3b5197 JM |
248 | if (hmac_sha384(key, key_len, buf, len, hash)) |
249 | return -1; | |
250 | os_memcpy(mic, hash, 24); | |
251 | break; | |
252 | #endif /* CONFIG_SUITEB192 */ | |
07a5fe82 JM |
253 | #ifdef CONFIG_OWE |
254 | case WPA_KEY_MGMT_OWE: | |
255 | wpa_printf(MSG_DEBUG, | |
6c472618 JM |
256 | "WPA: EAPOL-Key MIC using HMAC-SHA%u (AKM-defined - OWE)", |
257 | (unsigned int) key_len * 8 * 2); | |
258 | if (key_len == 128 / 8) { | |
259 | if (hmac_sha256(key, key_len, buf, len, hash)) | |
260 | return -1; | |
261 | } else if (key_len == 192 / 8) { | |
262 | if (hmac_sha384(key, key_len, buf, len, hash)) | |
263 | return -1; | |
264 | } else if (key_len == 256 / 8) { | |
265 | if (hmac_sha512(key, key_len, buf, len, hash)) | |
266 | return -1; | |
267 | } else { | |
268 | wpa_printf(MSG_INFO, | |
269 | "OWE: Unsupported KCK length: %u", | |
270 | (unsigned int) key_len); | |
07a5fe82 | 271 | return -1; |
6c472618 JM |
272 | } |
273 | os_memcpy(mic, hash, key_len); | |
07a5fe82 JM |
274 | break; |
275 | #endif /* CONFIG_OWE */ | |
567da5bb JM |
276 | #ifdef CONFIG_DPP |
277 | case WPA_KEY_MGMT_DPP: | |
278 | wpa_printf(MSG_DEBUG, | |
279 | "WPA: EAPOL-Key MIC using HMAC-SHA%u (AKM-defined - DPP)", | |
280 | (unsigned int) key_len * 8 * 2); | |
281 | if (key_len == 128 / 8) { | |
282 | if (hmac_sha256(key, key_len, buf, len, hash)) | |
283 | return -1; | |
284 | } else if (key_len == 192 / 8) { | |
285 | if (hmac_sha384(key, key_len, buf, len, hash)) | |
286 | return -1; | |
287 | } else if (key_len == 256 / 8) { | |
8b5579e1 | 288 | if (hmac_sha512(key, key_len, buf, len, hash)) |
567da5bb JM |
289 | return -1; |
290 | } else { | |
291 | wpa_printf(MSG_INFO, | |
292 | "DPP: Unsupported KCK length: %u", | |
293 | (unsigned int) key_len); | |
294 | return -1; | |
295 | } | |
8b5579e1 | 296 | os_memcpy(mic, hash, key_len); |
567da5bb JM |
297 | break; |
298 | #endif /* CONFIG_DPP */ | |
c49a9d6b JM |
299 | #if defined(CONFIG_IEEE80211R) && defined(CONFIG_SHA384) |
300 | case WPA_KEY_MGMT_FT_IEEE8021X_SHA384: | |
301 | wpa_printf(MSG_DEBUG, | |
302 | "WPA: EAPOL-Key MIC using HMAC-SHA384 (AKM-defined - FT 802.1X SHA384)"); | |
303 | if (hmac_sha384(key, key_len, buf, len, hash)) | |
304 | return -1; | |
305 | os_memcpy(mic, hash, 24); | |
306 | break; | |
307 | #endif /* CONFIG_IEEE80211R && CONFIG_SHA384 */ | |
929a2ea5 | 308 | default: |
ef9627cb JM |
309 | wpa_printf(MSG_DEBUG, |
310 | "WPA: EAPOL-Key MIC algorithm not known (AKM-defined - akmp=0x%x)", | |
311 | akmp); | |
929a2ea5 JM |
312 | return -1; |
313 | } | |
314 | break; | |
6fc6879b | 315 | default: |
ef9627cb JM |
316 | wpa_printf(MSG_DEBUG, |
317 | "WPA: EAPOL-Key MIC algorithm not known (ver=%d)", | |
318 | ver); | |
6fc6879b JM |
319 | return -1; |
320 | } | |
321 | ||
322 | return 0; | |
323 | } | |
324 | ||
325 | ||
326 | /** | |
327 | * wpa_pmk_to_ptk - Calculate PTK from PMK, addresses, and nonces | |
328 | * @pmk: Pairwise master key | |
329 | * @pmk_len: Length of PMK | |
330 | * @label: Label to use in derivation | |
331 | * @addr1: AA or SA | |
332 | * @addr2: SA or AA | |
333 | * @nonce1: ANonce or SNonce | |
334 | * @nonce2: SNonce or ANonce | |
335 | * @ptk: Buffer for pairwise transient key | |
98cd3d1c JM |
336 | * @akmp: Negotiated AKM |
337 | * @cipher: Negotiated pairwise cipher | |
338 | * Returns: 0 on success, -1 on failure | |
6fc6879b JM |
339 | * |
340 | * IEEE Std 802.11i-2004 - 8.5.1.2 Pairwise key hierarchy | |
341 | * PTK = PRF-X(PMK, "Pairwise key expansion", | |
342 | * Min(AA, SA) || Max(AA, SA) || | |
ecacd9cc JM |
343 | * Min(ANonce, SNonce) || Max(ANonce, SNonce) |
344 | * [ || Z.x ]) | |
345 | * | |
346 | * The optional Z.x component is used only with DPP and that part is not defined | |
347 | * in IEEE 802.11. | |
6fc6879b | 348 | */ |
98cd3d1c JM |
349 | int wpa_pmk_to_ptk(const u8 *pmk, size_t pmk_len, const char *label, |
350 | const u8 *addr1, const u8 *addr2, | |
351 | const u8 *nonce1, const u8 *nonce2, | |
ecacd9cc JM |
352 | struct wpa_ptk *ptk, int akmp, int cipher, |
353 | const u8 *z, size_t z_len) | |
6fc6879b | 354 | { |
ecacd9cc JM |
355 | #define MAX_Z_LEN 66 /* with NIST P-521 */ |
356 | u8 data[2 * ETH_ALEN + 2 * WPA_NONCE_LEN + MAX_Z_LEN]; | |
357 | size_t data_len = 2 * ETH_ALEN + 2 * WPA_NONCE_LEN; | |
98cd3d1c JM |
358 | u8 tmp[WPA_KCK_MAX_LEN + WPA_KEK_MAX_LEN + WPA_TK_MAX_LEN]; |
359 | size_t ptk_len; | |
6fc6879b | 360 | |
b488a129 | 361 | if (pmk_len == 0) { |
8e402d16 | 362 | wpa_printf(MSG_ERROR, "WPA: No PMK set for PTK derivation"); |
b488a129 JM |
363 | return -1; |
364 | } | |
365 | ||
ecacd9cc JM |
366 | if (z_len > MAX_Z_LEN) |
367 | return -1; | |
368 | ||
6fc6879b JM |
369 | if (os_memcmp(addr1, addr2, ETH_ALEN) < 0) { |
370 | os_memcpy(data, addr1, ETH_ALEN); | |
371 | os_memcpy(data + ETH_ALEN, addr2, ETH_ALEN); | |
372 | } else { | |
373 | os_memcpy(data, addr2, ETH_ALEN); | |
374 | os_memcpy(data + ETH_ALEN, addr1, ETH_ALEN); | |
375 | } | |
376 | ||
377 | if (os_memcmp(nonce1, nonce2, WPA_NONCE_LEN) < 0) { | |
378 | os_memcpy(data + 2 * ETH_ALEN, nonce1, WPA_NONCE_LEN); | |
379 | os_memcpy(data + 2 * ETH_ALEN + WPA_NONCE_LEN, nonce2, | |
380 | WPA_NONCE_LEN); | |
381 | } else { | |
382 | os_memcpy(data + 2 * ETH_ALEN, nonce2, WPA_NONCE_LEN); | |
383 | os_memcpy(data + 2 * ETH_ALEN + WPA_NONCE_LEN, nonce1, | |
384 | WPA_NONCE_LEN); | |
385 | } | |
386 | ||
ecacd9cc JM |
387 | if (z && z_len) { |
388 | os_memcpy(data + 2 * ETH_ALEN + 2 * WPA_NONCE_LEN, z, z_len); | |
389 | data_len += z_len; | |
390 | } | |
391 | ||
567da5bb JM |
392 | ptk->kck_len = wpa_kck_len(akmp, pmk_len); |
393 | ptk->kek_len = wpa_kek_len(akmp, pmk_len); | |
98cd3d1c | 394 | ptk->tk_len = wpa_cipher_key_len(cipher); |
1d624a07 JM |
395 | if (ptk->tk_len == 0) { |
396 | wpa_printf(MSG_ERROR, | |
397 | "WPA: Unsupported cipher (0x%x) used in PTK derivation", | |
398 | cipher); | |
399 | return -1; | |
400 | } | |
98cd3d1c JM |
401 | ptk_len = ptk->kck_len + ptk->kek_len + ptk->tk_len; |
402 | ||
ef9627cb | 403 | if (wpa_key_mgmt_sha384(akmp)) { |
e491389e | 404 | #if defined(CONFIG_SUITEB192) || defined(CONFIG_FILS) |
ef9627cb | 405 | wpa_printf(MSG_DEBUG, "WPA: PTK derivation using PRF(SHA384)"); |
ecacd9cc | 406 | if (sha384_prf(pmk, pmk_len, label, data, data_len, |
ef9627cb JM |
407 | tmp, ptk_len) < 0) |
408 | return -1; | |
409 | #else /* CONFIG_SUITEB192 || CONFIG_FILS */ | |
410 | return -1; | |
e491389e | 411 | #endif /* CONFIG_SUITEB192 || CONFIG_FILS */ |
07a5fe82 | 412 | } else if (wpa_key_mgmt_sha256(akmp) || akmp == WPA_KEY_MGMT_OWE) { |
2598e693 | 413 | #if defined(CONFIG_IEEE80211W) || defined(CONFIG_SAE) || defined(CONFIG_FILS) |
ef9627cb | 414 | wpa_printf(MSG_DEBUG, "WPA: PTK derivation using PRF(SHA256)"); |
ecacd9cc | 415 | if (sha256_prf(pmk, pmk_len, label, data, data_len, |
ef9627cb JM |
416 | tmp, ptk_len) < 0) |
417 | return -1; | |
2598e693 | 418 | #else /* CONFIG_IEEE80211W or CONFIG_SAE or CONFIG_FILS */ |
ef9627cb | 419 | return -1; |
2598e693 | 420 | #endif /* CONFIG_IEEE80211W or CONFIG_SAE or CONFIG_FILS */ |
567da5bb JM |
421 | #ifdef CONFIG_DPP |
422 | } else if (akmp == WPA_KEY_MGMT_DPP && pmk_len == 32) { | |
423 | wpa_printf(MSG_DEBUG, "WPA: PTK derivation using PRF(SHA256)"); | |
ecacd9cc | 424 | if (sha256_prf(pmk, pmk_len, label, data, data_len, |
567da5bb JM |
425 | tmp, ptk_len) < 0) |
426 | return -1; | |
427 | } else if (akmp == WPA_KEY_MGMT_DPP && pmk_len == 48) { | |
428 | wpa_printf(MSG_DEBUG, "WPA: PTK derivation using PRF(SHA384)"); | |
ecacd9cc | 429 | if (sha384_prf(pmk, pmk_len, label, data, data_len, |
567da5bb JM |
430 | tmp, ptk_len) < 0) |
431 | return -1; | |
432 | } else if (akmp == WPA_KEY_MGMT_DPP && pmk_len == 64) { | |
433 | wpa_printf(MSG_DEBUG, "WPA: PTK derivation using PRF(SHA512)"); | |
ecacd9cc | 434 | if (sha512_prf(pmk, pmk_len, label, data, data_len, |
567da5bb JM |
435 | tmp, ptk_len) < 0) |
436 | return -1; | |
437 | } else if (akmp == WPA_KEY_MGMT_DPP) { | |
438 | wpa_printf(MSG_INFO, "DPP: Unknown PMK length %u", | |
439 | (unsigned int) pmk_len); | |
440 | return -1; | |
441 | #endif /* CONFIG_DPP */ | |
ef9627cb JM |
442 | } else { |
443 | wpa_printf(MSG_DEBUG, "WPA: PTK derivation using PRF(SHA1)"); | |
ecacd9cc | 444 | if (sha1_prf(pmk, pmk_len, label, data, data_len, tmp, |
ef9627cb JM |
445 | ptk_len) < 0) |
446 | return -1; | |
447 | } | |
6fc6879b JM |
448 | |
449 | wpa_printf(MSG_DEBUG, "WPA: PTK derivation - A1=" MACSTR " A2=" MACSTR, | |
450 | MAC2STR(addr1), MAC2STR(addr2)); | |
bc8318ac JM |
451 | wpa_hexdump(MSG_DEBUG, "WPA: Nonce1", nonce1, WPA_NONCE_LEN); |
452 | wpa_hexdump(MSG_DEBUG, "WPA: Nonce2", nonce2, WPA_NONCE_LEN); | |
ecacd9cc JM |
453 | if (z && z_len) |
454 | wpa_hexdump_key(MSG_DEBUG, "WPA: Z.x", z, z_len); | |
6fc6879b | 455 | wpa_hexdump_key(MSG_DEBUG, "WPA: PMK", pmk, pmk_len); |
98cd3d1c JM |
456 | wpa_hexdump_key(MSG_DEBUG, "WPA: PTK", tmp, ptk_len); |
457 | ||
458 | os_memcpy(ptk->kck, tmp, ptk->kck_len); | |
459 | wpa_hexdump_key(MSG_DEBUG, "WPA: KCK", ptk->kck, ptk->kck_len); | |
460 | ||
461 | os_memcpy(ptk->kek, tmp + ptk->kck_len, ptk->kek_len); | |
462 | wpa_hexdump_key(MSG_DEBUG, "WPA: KEK", ptk->kek, ptk->kek_len); | |
463 | ||
464 | os_memcpy(ptk->tk, tmp + ptk->kck_len + ptk->kek_len, ptk->tk_len); | |
465 | wpa_hexdump_key(MSG_DEBUG, "WPA: TK", ptk->tk, ptk->tk_len); | |
466 | ||
2f373878 JM |
467 | ptk->kek2_len = 0; |
468 | ptk->kck2_len = 0; | |
469 | ||
98cd3d1c | 470 | os_memset(tmp, 0, sizeof(tmp)); |
ecacd9cc | 471 | os_memset(data, 0, data_len); |
98cd3d1c | 472 | return 0; |
6fc6879b JM |
473 | } |
474 | ||
c089bc57 | 475 | #ifdef CONFIG_FILS |
a852ab4c | 476 | |
fcd3d6ce JM |
477 | int fils_rmsk_to_pmk(int akmp, const u8 *rmsk, size_t rmsk_len, |
478 | const u8 *snonce, const u8 *anonce, const u8 *dh_ss, | |
479 | size_t dh_ss_len, u8 *pmk, size_t *pmk_len) | |
480 | { | |
481 | u8 nonces[2 * FILS_NONCE_LEN]; | |
482 | const u8 *addr[2]; | |
483 | size_t len[2]; | |
484 | size_t num_elem; | |
485 | int res; | |
486 | ||
487 | /* PMK = HMAC-Hash(SNonce || ANonce, rMSK [ || DHss ]) */ | |
488 | wpa_printf(MSG_DEBUG, "FILS: rMSK to PMK derivation"); | |
489 | ||
490 | if (wpa_key_mgmt_sha384(akmp)) | |
491 | *pmk_len = SHA384_MAC_LEN; | |
492 | else if (wpa_key_mgmt_sha256(akmp)) | |
493 | *pmk_len = SHA256_MAC_LEN; | |
494 | else | |
495 | return -1; | |
496 | ||
497 | wpa_hexdump_key(MSG_DEBUG, "FILS: rMSK", rmsk, rmsk_len); | |
498 | wpa_hexdump(MSG_DEBUG, "FILS: SNonce", snonce, FILS_NONCE_LEN); | |
499 | wpa_hexdump(MSG_DEBUG, "FILS: ANonce", anonce, FILS_NONCE_LEN); | |
500 | wpa_hexdump(MSG_DEBUG, "FILS: DHss", dh_ss, dh_ss_len); | |
501 | ||
502 | os_memcpy(nonces, snonce, FILS_NONCE_LEN); | |
503 | os_memcpy(&nonces[FILS_NONCE_LEN], anonce, FILS_NONCE_LEN); | |
504 | addr[0] = rmsk; | |
505 | len[0] = rmsk_len; | |
506 | num_elem = 1; | |
507 | if (dh_ss) { | |
508 | addr[1] = dh_ss; | |
509 | len[1] = dh_ss_len; | |
510 | num_elem++; | |
511 | } | |
512 | if (wpa_key_mgmt_sha384(akmp)) | |
513 | res = hmac_sha384_vector(nonces, 2 * FILS_NONCE_LEN, num_elem, | |
514 | addr, len, pmk); | |
515 | else | |
516 | res = hmac_sha256_vector(nonces, 2 * FILS_NONCE_LEN, num_elem, | |
517 | addr, len, pmk); | |
518 | if (res == 0) | |
519 | wpa_hexdump_key(MSG_DEBUG, "FILS: PMK", pmk, *pmk_len); | |
1bd13110 JM |
520 | else |
521 | *pmk_len = 0; | |
fcd3d6ce JM |
522 | return res; |
523 | } | |
524 | ||
525 | ||
526 | int fils_pmkid_erp(int akmp, const u8 *reauth, size_t reauth_len, | |
527 | u8 *pmkid) | |
528 | { | |
529 | const u8 *addr[1]; | |
530 | size_t len[1]; | |
531 | u8 hash[SHA384_MAC_LEN]; | |
532 | int res; | |
533 | ||
534 | /* PMKID = Truncate-128(Hash(EAP-Initiate/Reauth)) */ | |
535 | addr[0] = reauth; | |
536 | len[0] = reauth_len; | |
537 | if (wpa_key_mgmt_sha384(akmp)) | |
538 | res = sha384_vector(1, addr, len, hash); | |
539 | else if (wpa_key_mgmt_sha256(akmp)) | |
540 | res = sha256_vector(1, addr, len, hash); | |
541 | else | |
542 | return -1; | |
543 | if (res) | |
544 | return res; | |
545 | os_memcpy(pmkid, hash, PMKID_LEN); | |
546 | wpa_hexdump(MSG_DEBUG, "FILS: PMKID", pmkid, PMKID_LEN); | |
547 | return 0; | |
548 | } | |
549 | ||
550 | ||
c089bc57 | 551 | int fils_pmk_to_ptk(const u8 *pmk, size_t pmk_len, const u8 *spa, const u8 *aa, |
4cada9dc JM |
552 | const u8 *snonce, const u8 *anonce, const u8 *dhss, |
553 | size_t dhss_len, struct wpa_ptk *ptk, | |
215eaa74 JM |
554 | u8 *ick, size_t *ick_len, int akmp, int cipher, |
555 | u8 *fils_ft, size_t *fils_ft_len) | |
c089bc57 | 556 | { |
4cada9dc JM |
557 | u8 *data, *pos; |
558 | size_t data_len; | |
215eaa74 JM |
559 | u8 tmp[FILS_ICK_MAX_LEN + WPA_KEK_MAX_LEN + WPA_TK_MAX_LEN + |
560 | FILS_FT_MAX_LEN]; | |
c089bc57 JM |
561 | size_t key_data_len; |
562 | const char *label = "FILS PTK Derivation"; | |
4cada9dc | 563 | int ret = -1; |
c089bc57 JM |
564 | |
565 | /* | |
566 | * FILS-Key-Data = PRF-X(PMK, "FILS PTK Derivation", | |
4cada9dc | 567 | * SPA || AA || SNonce || ANonce [ || DHss ]) |
c089bc57 JM |
568 | * ICK = L(FILS-Key-Data, 0, ICK_bits) |
569 | * KEK = L(FILS-Key-Data, ICK_bits, KEK_bits) | |
570 | * TK = L(FILS-Key-Data, ICK_bits + KEK_bits, TK_bits) | |
571 | * If doing FT initial mobility domain association: | |
572 | * FILS-FT = L(FILS-Key-Data, ICK_bits + KEK_bits + TK_bits, | |
573 | * FILS-FT_bits) | |
574 | */ | |
4cada9dc JM |
575 | data_len = 2 * ETH_ALEN + 2 * FILS_NONCE_LEN + dhss_len; |
576 | data = os_malloc(data_len); | |
577 | if (!data) | |
578 | goto err; | |
579 | pos = data; | |
580 | os_memcpy(pos, spa, ETH_ALEN); | |
581 | pos += ETH_ALEN; | |
582 | os_memcpy(pos, aa, ETH_ALEN); | |
583 | pos += ETH_ALEN; | |
584 | os_memcpy(pos, snonce, FILS_NONCE_LEN); | |
585 | pos += FILS_NONCE_LEN; | |
586 | os_memcpy(pos, anonce, FILS_NONCE_LEN); | |
587 | pos += FILS_NONCE_LEN; | |
588 | if (dhss) | |
589 | os_memcpy(pos, dhss, dhss_len); | |
c089bc57 JM |
590 | |
591 | ptk->kck_len = 0; | |
567da5bb | 592 | ptk->kek_len = wpa_kek_len(akmp, pmk_len); |
c089bc57 JM |
593 | ptk->tk_len = wpa_cipher_key_len(cipher); |
594 | if (wpa_key_mgmt_sha384(akmp)) | |
595 | *ick_len = 48; | |
596 | else if (wpa_key_mgmt_sha256(akmp)) | |
597 | *ick_len = 32; | |
598 | else | |
4cada9dc | 599 | goto err; |
c089bc57 JM |
600 | key_data_len = *ick_len + ptk->kek_len + ptk->tk_len; |
601 | ||
215eaa74 JM |
602 | if (fils_ft && fils_ft_len) { |
603 | if (akmp == WPA_KEY_MGMT_FT_FILS_SHA256) { | |
604 | *fils_ft_len = 32; | |
605 | } else if (akmp == WPA_KEY_MGMT_FT_FILS_SHA384) { | |
606 | *fils_ft_len = 48; | |
607 | } else { | |
608 | *fils_ft_len = 0; | |
609 | fils_ft = NULL; | |
610 | } | |
611 | key_data_len += *fils_ft_len; | |
612 | } | |
613 | ||
ef9627cb JM |
614 | if (wpa_key_mgmt_sha384(akmp)) { |
615 | wpa_printf(MSG_DEBUG, "FILS: PTK derivation using PRF(SHA384)"); | |
4cada9dc | 616 | if (sha384_prf(pmk, pmk_len, label, data, data_len, |
ef9627cb | 617 | tmp, key_data_len) < 0) |
4cada9dc | 618 | goto err; |
ef9627cb JM |
619 | } else { |
620 | wpa_printf(MSG_DEBUG, "FILS: PTK derivation using PRF(SHA256)"); | |
4cada9dc | 621 | if (sha256_prf(pmk, pmk_len, label, data, data_len, |
ef9627cb | 622 | tmp, key_data_len) < 0) |
4cada9dc | 623 | goto err; |
ef9627cb | 624 | } |
c089bc57 JM |
625 | |
626 | wpa_printf(MSG_DEBUG, "FILS: PTK derivation - SPA=" MACSTR | |
627 | " AA=" MACSTR, MAC2STR(spa), MAC2STR(aa)); | |
628 | wpa_hexdump(MSG_DEBUG, "FILS: SNonce", snonce, FILS_NONCE_LEN); | |
629 | wpa_hexdump(MSG_DEBUG, "FILS: ANonce", anonce, FILS_NONCE_LEN); | |
4cada9dc JM |
630 | if (dhss) |
631 | wpa_hexdump_key(MSG_DEBUG, "FILS: DHss", dhss, dhss_len); | |
c089bc57 JM |
632 | wpa_hexdump_key(MSG_DEBUG, "FILS: PMK", pmk, pmk_len); |
633 | wpa_hexdump_key(MSG_DEBUG, "FILS: FILS-Key-Data", tmp, key_data_len); | |
634 | ||
635 | os_memcpy(ick, tmp, *ick_len); | |
636 | wpa_hexdump_key(MSG_DEBUG, "FILS: ICK", ick, *ick_len); | |
637 | ||
638 | os_memcpy(ptk->kek, tmp + *ick_len, ptk->kek_len); | |
639 | wpa_hexdump_key(MSG_DEBUG, "FILS: KEK", ptk->kek, ptk->kek_len); | |
640 | ||
641 | os_memcpy(ptk->tk, tmp + *ick_len + ptk->kek_len, ptk->tk_len); | |
642 | wpa_hexdump_key(MSG_DEBUG, "FILS: TK", ptk->tk, ptk->tk_len); | |
643 | ||
215eaa74 JM |
644 | if (fils_ft && fils_ft_len) { |
645 | os_memcpy(fils_ft, tmp + *ick_len + ptk->kek_len + ptk->tk_len, | |
646 | *fils_ft_len); | |
647 | wpa_hexdump_key(MSG_DEBUG, "FILS: FILS-FT", | |
648 | fils_ft, *fils_ft_len); | |
649 | } | |
c089bc57 | 650 | |
2f373878 JM |
651 | ptk->kek2_len = 0; |
652 | ptk->kck2_len = 0; | |
653 | ||
c089bc57 | 654 | os_memset(tmp, 0, sizeof(tmp)); |
4cada9dc JM |
655 | ret = 0; |
656 | err: | |
657 | bin_clear_free(data, data_len); | |
658 | return ret; | |
c089bc57 JM |
659 | } |
660 | ||
a852ab4c JM |
661 | |
662 | int fils_key_auth_sk(const u8 *ick, size_t ick_len, const u8 *snonce, | |
663 | const u8 *anonce, const u8 *sta_addr, const u8 *bssid, | |
664 | const u8 *g_sta, size_t g_sta_len, | |
665 | const u8 *g_ap, size_t g_ap_len, | |
666 | int akmp, u8 *key_auth_sta, u8 *key_auth_ap, | |
667 | size_t *key_auth_len) | |
668 | { | |
669 | const u8 *addr[6]; | |
670 | size_t len[6]; | |
671 | size_t num_elem = 4; | |
672 | int res; | |
673 | ||
4d0a61c5 JM |
674 | wpa_printf(MSG_DEBUG, "FILS: Key-Auth derivation: STA-MAC=" MACSTR |
675 | " AP-BSSID=" MACSTR, MAC2STR(sta_addr), MAC2STR(bssid)); | |
676 | wpa_hexdump_key(MSG_DEBUG, "FILS: ICK", ick, ick_len); | |
677 | wpa_hexdump(MSG_DEBUG, "FILS: SNonce", snonce, FILS_NONCE_LEN); | |
678 | wpa_hexdump(MSG_DEBUG, "FILS: ANonce", anonce, FILS_NONCE_LEN); | |
679 | wpa_hexdump(MSG_DEBUG, "FILS: gSTA", g_sta, g_sta_len); | |
680 | wpa_hexdump(MSG_DEBUG, "FILS: gAP", g_ap, g_ap_len); | |
681 | ||
a852ab4c JM |
682 | /* |
683 | * For (Re)Association Request frame (STA->AP): | |
684 | * Key-Auth = HMAC-Hash(ICK, SNonce || ANonce || STA-MAC || AP-BSSID | |
685 | * [ || gSTA || gAP ]) | |
686 | */ | |
687 | addr[0] = snonce; | |
688 | len[0] = FILS_NONCE_LEN; | |
689 | addr[1] = anonce; | |
690 | len[1] = FILS_NONCE_LEN; | |
691 | addr[2] = sta_addr; | |
692 | len[2] = ETH_ALEN; | |
693 | addr[3] = bssid; | |
694 | len[3] = ETH_ALEN; | |
695 | if (g_sta && g_ap_len && g_ap && g_ap_len) { | |
696 | addr[4] = g_sta; | |
697 | len[4] = g_sta_len; | |
698 | addr[5] = g_ap; | |
699 | len[5] = g_ap_len; | |
700 | num_elem = 6; | |
701 | } | |
702 | ||
703 | if (wpa_key_mgmt_sha384(akmp)) { | |
704 | *key_auth_len = 48; | |
705 | res = hmac_sha384_vector(ick, ick_len, num_elem, addr, len, | |
706 | key_auth_sta); | |
707 | } else if (wpa_key_mgmt_sha256(akmp)) { | |
708 | *key_auth_len = 32; | |
709 | res = hmac_sha256_vector(ick, ick_len, num_elem, addr, len, | |
710 | key_auth_sta); | |
711 | } else { | |
712 | return -1; | |
713 | } | |
714 | if (res < 0) | |
715 | return res; | |
716 | ||
717 | /* | |
718 | * For (Re)Association Response frame (AP->STA): | |
719 | * Key-Auth = HMAC-Hash(ICK, ANonce || SNonce || AP-BSSID || STA-MAC | |
720 | * [ || gAP || gSTA ]) | |
721 | */ | |
722 | addr[0] = anonce; | |
723 | addr[1] = snonce; | |
724 | addr[2] = bssid; | |
725 | addr[3] = sta_addr; | |
726 | if (g_sta && g_ap_len && g_ap && g_ap_len) { | |
727 | addr[4] = g_ap; | |
728 | len[4] = g_ap_len; | |
729 | addr[5] = g_sta; | |
730 | len[5] = g_sta_len; | |
731 | } | |
732 | ||
733 | if (wpa_key_mgmt_sha384(akmp)) | |
734 | res = hmac_sha384_vector(ick, ick_len, num_elem, addr, len, | |
735 | key_auth_ap); | |
736 | else if (wpa_key_mgmt_sha256(akmp)) | |
737 | res = hmac_sha256_vector(ick, ick_len, num_elem, addr, len, | |
738 | key_auth_ap); | |
739 | if (res < 0) | |
740 | return res; | |
741 | ||
742 | wpa_hexdump(MSG_DEBUG, "FILS: Key-Auth (STA)", | |
743 | key_auth_sta, *key_auth_len); | |
744 | wpa_hexdump(MSG_DEBUG, "FILS: Key-Auth (AP)", | |
745 | key_auth_ap, *key_auth_len); | |
746 | ||
747 | return 0; | |
748 | } | |
749 | ||
c089bc57 JM |
750 | #endif /* CONFIG_FILS */ |
751 | ||
6fc6879b JM |
752 | |
753 | #ifdef CONFIG_IEEE80211R | |
98cd3d1c JM |
754 | int wpa_ft_mic(const u8 *kck, size_t kck_len, const u8 *sta_addr, |
755 | const u8 *ap_addr, u8 transaction_seqnum, | |
756 | const u8 *mdie, size_t mdie_len, | |
6fc6879b JM |
757 | const u8 *ftie, size_t ftie_len, |
758 | const u8 *rsnie, size_t rsnie_len, | |
759 | const u8 *ric, size_t ric_len, u8 *mic) | |
760 | { | |
8b949804 JM |
761 | const u8 *addr[9]; |
762 | size_t len[9]; | |
763 | size_t i, num_elem = 0; | |
b327026a JM |
764 | u8 zero_mic[24]; |
765 | size_t mic_len, fte_fixed_len; | |
766 | ||
767 | if (kck_len == 16) { | |
768 | mic_len = 16; | |
769 | #ifdef CONFIG_SHA384 | |
770 | } else if (kck_len == 24) { | |
771 | mic_len = 24; | |
772 | #endif /* CONFIG_SHA384 */ | |
773 | } else { | |
98cd3d1c JM |
774 | wpa_printf(MSG_WARNING, "FT: Unsupported KCK length %u", |
775 | (unsigned int) kck_len); | |
776 | return -1; | |
777 | } | |
778 | ||
b327026a JM |
779 | fte_fixed_len = sizeof(struct rsn_ftie) - 16 + mic_len; |
780 | ||
8b949804 JM |
781 | addr[num_elem] = sta_addr; |
782 | len[num_elem] = ETH_ALEN; | |
783 | num_elem++; | |
784 | ||
785 | addr[num_elem] = ap_addr; | |
786 | len[num_elem] = ETH_ALEN; | |
787 | num_elem++; | |
788 | ||
789 | addr[num_elem] = &transaction_seqnum; | |
790 | len[num_elem] = 1; | |
791 | num_elem++; | |
6fc6879b | 792 | |
6fc6879b | 793 | if (rsnie) { |
8b949804 JM |
794 | addr[num_elem] = rsnie; |
795 | len[num_elem] = rsnie_len; | |
796 | num_elem++; | |
6fc6879b JM |
797 | } |
798 | if (mdie) { | |
8b949804 JM |
799 | addr[num_elem] = mdie; |
800 | len[num_elem] = mdie_len; | |
801 | num_elem++; | |
6fc6879b JM |
802 | } |
803 | if (ftie) { | |
b327026a | 804 | if (ftie_len < 2 + fte_fixed_len) |
6fc6879b | 805 | return -1; |
8b949804 JM |
806 | |
807 | /* IE hdr and mic_control */ | |
808 | addr[num_elem] = ftie; | |
809 | len[num_elem] = 2 + 2; | |
810 | num_elem++; | |
811 | ||
812 | /* MIC field with all zeros */ | |
b327026a | 813 | os_memset(zero_mic, 0, mic_len); |
8b949804 | 814 | addr[num_elem] = zero_mic; |
b327026a | 815 | len[num_elem] = mic_len; |
8b949804 JM |
816 | num_elem++; |
817 | ||
818 | /* Rest of FTIE */ | |
b327026a JM |
819 | addr[num_elem] = ftie + 2 + 2 + mic_len; |
820 | len[num_elem] = ftie_len - (2 + 2 + mic_len); | |
8b949804 | 821 | num_elem++; |
6fc6879b JM |
822 | } |
823 | if (ric) { | |
8b949804 JM |
824 | addr[num_elem] = ric; |
825 | len[num_elem] = ric_len; | |
826 | num_elem++; | |
6fc6879b JM |
827 | } |
828 | ||
8b949804 JM |
829 | for (i = 0; i < num_elem; i++) |
830 | wpa_hexdump(MSG_MSGDUMP, "FT: MIC data", addr[i], len[i]); | |
b327026a JM |
831 | #ifdef CONFIG_SHA384 |
832 | if (kck_len == 24) { | |
833 | u8 hash[SHA384_MAC_LEN]; | |
834 | ||
835 | if (hmac_sha384_vector(kck, kck_len, num_elem, addr, len, hash)) | |
836 | return -1; | |
837 | os_memcpy(mic, hash, 24); | |
838 | } | |
839 | #endif /* CONFIG_SHA384 */ | |
840 | if (kck_len == 16 && | |
841 | omac1_aes_128_vector(kck, num_elem, addr, len, mic)) | |
6fc6879b | 842 | return -1; |
6fc6879b JM |
843 | |
844 | return 0; | |
845 | } | |
6554237f JM |
846 | |
847 | ||
848 | static int wpa_ft_parse_ftie(const u8 *ie, size_t ie_len, | |
9a33737a | 849 | struct wpa_ft_ies *parse, int use_sha384) |
6554237f JM |
850 | { |
851 | const u8 *end, *pos; | |
852 | ||
853 | parse->ftie = ie; | |
854 | parse->ftie_len = ie_len; | |
855 | ||
9a33737a JM |
856 | pos = ie + (use_sha384 ? sizeof(struct rsn_ftie_sha384) : |
857 | sizeof(struct rsn_ftie)); | |
6554237f | 858 | end = ie + ie_len; |
9a33737a | 859 | wpa_hexdump(MSG_DEBUG, "FT: Parse FTE subelements", pos, end - pos); |
6554237f | 860 | |
7b5880fc JM |
861 | while (end - pos >= 2) { |
862 | u8 id, len; | |
863 | ||
864 | id = *pos++; | |
865 | len = *pos++; | |
9a33737a JM |
866 | if (len > end - pos) { |
867 | wpa_printf(MSG_DEBUG, "FT: Truncated subelement"); | |
7b5880fc | 868 | break; |
9a33737a | 869 | } |
7b5880fc JM |
870 | |
871 | switch (id) { | |
6554237f | 872 | case FTIE_SUBELEM_R1KH_ID: |
7b5880fc JM |
873 | if (len != FT_R1KH_ID_LEN) { |
874 | wpa_printf(MSG_DEBUG, | |
875 | "FT: Invalid R1KH-ID length in FTIE: %d", | |
876 | len); | |
6554237f JM |
877 | return -1; |
878 | } | |
7b5880fc | 879 | parse->r1kh_id = pos; |
6554237f JM |
880 | break; |
881 | case FTIE_SUBELEM_GTK: | |
7b5880fc JM |
882 | parse->gtk = pos; |
883 | parse->gtk_len = len; | |
6554237f JM |
884 | break; |
885 | case FTIE_SUBELEM_R0KH_ID: | |
7b5880fc JM |
886 | if (len < 1 || len > FT_R0KH_ID_MAX_LEN) { |
887 | wpa_printf(MSG_DEBUG, | |
888 | "FT: Invalid R0KH-ID length in FTIE: %d", | |
889 | len); | |
6554237f JM |
890 | return -1; |
891 | } | |
7b5880fc JM |
892 | parse->r0kh_id = pos; |
893 | parse->r0kh_id_len = len; | |
6554237f JM |
894 | break; |
895 | #ifdef CONFIG_IEEE80211W | |
896 | case FTIE_SUBELEM_IGTK: | |
7b5880fc JM |
897 | parse->igtk = pos; |
898 | parse->igtk_len = len; | |
6554237f JM |
899 | break; |
900 | #endif /* CONFIG_IEEE80211W */ | |
f53aeff4 MV |
901 | #ifdef CONFIG_OCV |
902 | case FTIE_SUBELEM_OCI: | |
903 | parse->oci = pos; | |
904 | parse->oci_len = len; | |
905 | break; | |
906 | #endif /* CONFIG_OCV */ | |
9a33737a JM |
907 | default: |
908 | wpa_printf(MSG_DEBUG, "FT: Unknown subelem id %u", id); | |
909 | break; | |
6554237f JM |
910 | } |
911 | ||
7b5880fc | 912 | pos += len; |
6554237f JM |
913 | } |
914 | ||
915 | return 0; | |
916 | } | |
917 | ||
918 | ||
919 | int wpa_ft_parse_ies(const u8 *ies, size_t ies_len, | |
9a33737a | 920 | struct wpa_ft_ies *parse, int use_sha384) |
6554237f JM |
921 | { |
922 | const u8 *end, *pos; | |
923 | struct wpa_ie_data data; | |
924 | int ret; | |
925 | const struct rsn_ftie *ftie; | |
926 | int prot_ie_count = 0; | |
9a33737a JM |
927 | int update_use_sha384 = 0; |
928 | ||
929 | if (use_sha384 < 0) { | |
930 | use_sha384 = 0; | |
931 | update_use_sha384 = 1; | |
932 | } | |
6554237f JM |
933 | |
934 | os_memset(parse, 0, sizeof(*parse)); | |
935 | if (ies == NULL) | |
936 | return 0; | |
937 | ||
938 | pos = ies; | |
939 | end = ies + ies_len; | |
7b5880fc JM |
940 | while (end - pos >= 2) { |
941 | u8 id, len; | |
942 | ||
943 | id = *pos++; | |
944 | len = *pos++; | |
945 | if (len > end - pos) | |
946 | break; | |
947 | ||
948 | switch (id) { | |
6554237f | 949 | case WLAN_EID_RSN: |
df3bf687 | 950 | wpa_hexdump(MSG_DEBUG, "FT: RSNE", pos, len); |
7b5880fc JM |
951 | parse->rsn = pos; |
952 | parse->rsn_len = len; | |
6554237f JM |
953 | ret = wpa_parse_wpa_ie_rsn(parse->rsn - 2, |
954 | parse->rsn_len + 2, | |
955 | &data); | |
956 | if (ret < 0) { | |
957 | wpa_printf(MSG_DEBUG, "FT: Failed to parse " | |
958 | "RSN IE: %d", ret); | |
959 | return -1; | |
960 | } | |
528f263c | 961 | parse->rsn_capab = data.capabilities; |
6554237f JM |
962 | if (data.num_pmkid == 1 && data.pmkid) |
963 | parse->rsn_pmkid = data.pmkid; | |
96590564 MB |
964 | parse->key_mgmt = data.key_mgmt; |
965 | parse->pairwise_cipher = data.pairwise_cipher; | |
9a33737a JM |
966 | if (update_use_sha384) { |
967 | use_sha384 = | |
968 | wpa_key_mgmt_sha384(parse->key_mgmt); | |
969 | update_use_sha384 = 0; | |
970 | } | |
6554237f JM |
971 | break; |
972 | case WLAN_EID_MOBILITY_DOMAIN: | |
df3bf687 | 973 | wpa_hexdump(MSG_DEBUG, "FT: MDE", pos, len); |
7b5880fc | 974 | if (len < sizeof(struct rsn_mdie)) |
ae7a42bd | 975 | return -1; |
7b5880fc JM |
976 | parse->mdie = pos; |
977 | parse->mdie_len = len; | |
6554237f JM |
978 | break; |
979 | case WLAN_EID_FAST_BSS_TRANSITION: | |
df3bf687 | 980 | wpa_hexdump(MSG_DEBUG, "FT: FTE", pos, len); |
9a33737a JM |
981 | if (use_sha384) { |
982 | const struct rsn_ftie_sha384 *ftie_sha384; | |
983 | ||
984 | if (len < sizeof(*ftie_sha384)) | |
985 | return -1; | |
986 | ftie_sha384 = | |
987 | (const struct rsn_ftie_sha384 *) pos; | |
df3bf687 JM |
988 | wpa_hexdump(MSG_DEBUG, "FT: FTE-MIC Control", |
989 | ftie_sha384->mic_control, 2); | |
990 | wpa_hexdump(MSG_DEBUG, "FT: FTE-MIC", | |
991 | ftie_sha384->mic, | |
992 | sizeof(ftie_sha384->mic)); | |
c38c62ff | 993 | parse->fte_anonce = ftie_sha384->anonce; |
df3bf687 JM |
994 | wpa_hexdump(MSG_DEBUG, "FT: FTE-ANonce", |
995 | ftie_sha384->anonce, | |
996 | WPA_NONCE_LEN); | |
c38c62ff | 997 | parse->fte_snonce = ftie_sha384->snonce; |
df3bf687 JM |
998 | wpa_hexdump(MSG_DEBUG, "FT: FTE-SNonce", |
999 | ftie_sha384->snonce, | |
1000 | WPA_NONCE_LEN); | |
9a33737a JM |
1001 | prot_ie_count = ftie_sha384->mic_control[1]; |
1002 | if (wpa_ft_parse_ftie(pos, len, parse, 1) < 0) | |
1003 | return -1; | |
1004 | break; | |
1005 | } | |
1006 | ||
7b5880fc | 1007 | if (len < sizeof(*ftie)) |
6554237f | 1008 | return -1; |
7b5880fc | 1009 | ftie = (const struct rsn_ftie *) pos; |
df3bf687 JM |
1010 | wpa_hexdump(MSG_DEBUG, "FT: FTE-MIC Control", |
1011 | ftie->mic_control, 2); | |
1012 | wpa_hexdump(MSG_DEBUG, "FT: FTE-MIC", | |
1013 | ftie->mic, sizeof(ftie->mic)); | |
c38c62ff | 1014 | parse->fte_anonce = ftie->anonce; |
df3bf687 JM |
1015 | wpa_hexdump(MSG_DEBUG, "FT: FTE-ANonce", |
1016 | ftie->anonce, WPA_NONCE_LEN); | |
c38c62ff | 1017 | parse->fte_snonce = ftie->snonce; |
df3bf687 JM |
1018 | wpa_hexdump(MSG_DEBUG, "FT: FTE-SNonce", |
1019 | ftie->snonce, WPA_NONCE_LEN); | |
6554237f | 1020 | prot_ie_count = ftie->mic_control[1]; |
9a33737a | 1021 | if (wpa_ft_parse_ftie(pos, len, parse, 0) < 0) |
6554237f JM |
1022 | return -1; |
1023 | break; | |
1024 | case WLAN_EID_TIMEOUT_INTERVAL: | |
df3bf687 JM |
1025 | wpa_hexdump(MSG_DEBUG, "FT: Timeout Interval", |
1026 | pos, len); | |
7b5880fc | 1027 | if (len != 5) |
ae7a42bd | 1028 | break; |
7b5880fc JM |
1029 | parse->tie = pos; |
1030 | parse->tie_len = len; | |
6554237f JM |
1031 | break; |
1032 | case WLAN_EID_RIC_DATA: | |
1033 | if (parse->ric == NULL) | |
7b5880fc | 1034 | parse->ric = pos - 2; |
6554237f JM |
1035 | break; |
1036 | } | |
1037 | ||
7b5880fc | 1038 | pos += len; |
6554237f JM |
1039 | } |
1040 | ||
1041 | if (prot_ie_count == 0) | |
1042 | return 0; /* no MIC */ | |
1043 | ||
1044 | /* | |
1045 | * Check that the protected IE count matches with IEs included in the | |
1046 | * frame. | |
1047 | */ | |
1048 | if (parse->rsn) | |
1049 | prot_ie_count--; | |
1050 | if (parse->mdie) | |
1051 | prot_ie_count--; | |
1052 | if (parse->ftie) | |
1053 | prot_ie_count--; | |
1054 | if (prot_ie_count < 0) { | |
1055 | wpa_printf(MSG_DEBUG, "FT: Some required IEs not included in " | |
1056 | "the protected IE count"); | |
1057 | return -1; | |
1058 | } | |
1059 | ||
1060 | if (prot_ie_count == 0 && parse->ric) { | |
1061 | wpa_printf(MSG_DEBUG, "FT: RIC IE(s) in the frame, but not " | |
1062 | "included in protected IE count"); | |
1063 | return -1; | |
1064 | } | |
1065 | ||
1066 | /* Determine the end of the RIC IE(s) */ | |
7b5880fc JM |
1067 | if (parse->ric) { |
1068 | pos = parse->ric; | |
1069 | while (end - pos >= 2 && 2 + pos[1] <= end - pos && | |
1070 | prot_ie_count) { | |
1071 | prot_ie_count--; | |
1072 | pos += 2 + pos[1]; | |
1073 | } | |
1074 | parse->ric_len = pos - parse->ric; | |
6554237f | 1075 | } |
6554237f JM |
1076 | if (prot_ie_count) { |
1077 | wpa_printf(MSG_DEBUG, "FT: %d protected IEs missing from " | |
1078 | "frame", (int) prot_ie_count); | |
1079 | return -1; | |
1080 | } | |
1081 | ||
1082 | return 0; | |
1083 | } | |
6fc6879b JM |
1084 | #endif /* CONFIG_IEEE80211R */ |
1085 | ||
1086 | ||
6fc6879b JM |
1087 | static int rsn_selector_to_bitfield(const u8 *s) |
1088 | { | |
1089 | if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_NONE) | |
1090 | return WPA_CIPHER_NONE; | |
6fc6879b JM |
1091 | if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_TKIP) |
1092 | return WPA_CIPHER_TKIP; | |
1093 | if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_CCMP) | |
1094 | return WPA_CIPHER_CCMP; | |
6fc6879b JM |
1095 | #ifdef CONFIG_IEEE80211W |
1096 | if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_AES_128_CMAC) | |
1097 | return WPA_CIPHER_AES_128_CMAC; | |
1098 | #endif /* CONFIG_IEEE80211W */ | |
eb7719ff JM |
1099 | if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_GCMP) |
1100 | return WPA_CIPHER_GCMP; | |
30675c34 JM |
1101 | if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_CCMP_256) |
1102 | return WPA_CIPHER_CCMP_256; | |
1103 | if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_GCMP_256) | |
1104 | return WPA_CIPHER_GCMP_256; | |
1105 | if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_BIP_GMAC_128) | |
1106 | return WPA_CIPHER_BIP_GMAC_128; | |
1107 | if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_BIP_GMAC_256) | |
1108 | return WPA_CIPHER_BIP_GMAC_256; | |
1109 | if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_BIP_CMAC_256) | |
1110 | return WPA_CIPHER_BIP_CMAC_256; | |
8dd9f9cd JM |
1111 | if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED) |
1112 | return WPA_CIPHER_GTK_NOT_USED; | |
6fc6879b JM |
1113 | return 0; |
1114 | } | |
1115 | ||
1116 | ||
1117 | static int rsn_key_mgmt_to_bitfield(const u8 *s) | |
1118 | { | |
1119 | if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_UNSPEC_802_1X) | |
1120 | return WPA_KEY_MGMT_IEEE8021X; | |
1121 | if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X) | |
1122 | return WPA_KEY_MGMT_PSK; | |
1123 | #ifdef CONFIG_IEEE80211R | |
1124 | if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_802_1X) | |
1125 | return WPA_KEY_MGMT_FT_IEEE8021X; | |
1126 | if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_PSK) | |
1127 | return WPA_KEY_MGMT_FT_PSK; | |
c22bb5bb JM |
1128 | #ifdef CONFIG_SHA384 |
1129 | if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_802_1X_SHA384) | |
1130 | return WPA_KEY_MGMT_FT_IEEE8021X_SHA384; | |
1131 | #endif /* CONFIG_SHA384 */ | |
6fc6879b | 1132 | #endif /* CONFIG_IEEE80211R */ |
56586197 JM |
1133 | #ifdef CONFIG_IEEE80211W |
1134 | if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_802_1X_SHA256) | |
1135 | return WPA_KEY_MGMT_IEEE8021X_SHA256; | |
1136 | if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_PSK_SHA256) | |
1137 | return WPA_KEY_MGMT_PSK_SHA256; | |
1138 | #endif /* CONFIG_IEEE80211W */ | |
c10347f2 JM |
1139 | #ifdef CONFIG_SAE |
1140 | if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_SAE) | |
1141 | return WPA_KEY_MGMT_SAE; | |
1142 | if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_SAE) | |
1143 | return WPA_KEY_MGMT_FT_SAE; | |
1144 | #endif /* CONFIG_SAE */ | |
666497c8 JM |
1145 | if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_802_1X_SUITE_B) |
1146 | return WPA_KEY_MGMT_IEEE8021X_SUITE_B; | |
5e3b5197 JM |
1147 | if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192) |
1148 | return WPA_KEY_MGMT_IEEE8021X_SUITE_B_192; | |
94318a0d JM |
1149 | if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FILS_SHA256) |
1150 | return WPA_KEY_MGMT_FILS_SHA256; | |
1151 | if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FILS_SHA384) | |
1152 | return WPA_KEY_MGMT_FILS_SHA384; | |
1153 | if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_FILS_SHA256) | |
1154 | return WPA_KEY_MGMT_FT_FILS_SHA256; | |
1155 | if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_FILS_SHA384) | |
1156 | return WPA_KEY_MGMT_FT_FILS_SHA384; | |
a1ea1b45 JM |
1157 | #ifdef CONFIG_OWE |
1158 | if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_OWE) | |
1159 | return WPA_KEY_MGMT_OWE; | |
1160 | #endif /* CONFIG_OWE */ | |
567da5bb JM |
1161 | #ifdef CONFIG_DPP |
1162 | if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_DPP) | |
1163 | return WPA_KEY_MGMT_DPP; | |
1164 | #endif /* CONFIG_DPP */ | |
0f8385e6 BG |
1165 | if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_OSEN) |
1166 | return WPA_KEY_MGMT_OSEN; | |
6fc6879b JM |
1167 | return 0; |
1168 | } | |
6fc6879b JM |
1169 | |
1170 | ||
05a90d78 | 1171 | int wpa_cipher_valid_group(int cipher) |
8dd9f9cd JM |
1172 | { |
1173 | return wpa_cipher_valid_pairwise(cipher) || | |
8dd9f9cd JM |
1174 | cipher == WPA_CIPHER_GTK_NOT_USED; |
1175 | } | |
1176 | ||
1177 | ||
1178 | #ifdef CONFIG_IEEE80211W | |
1179 | int wpa_cipher_valid_mgmt_group(int cipher) | |
1180 | { | |
1181 | return cipher == WPA_CIPHER_AES_128_CMAC || | |
1182 | cipher == WPA_CIPHER_BIP_GMAC_128 || | |
1183 | cipher == WPA_CIPHER_BIP_GMAC_256 || | |
1184 | cipher == WPA_CIPHER_BIP_CMAC_256; | |
1185 | } | |
1186 | #endif /* CONFIG_IEEE80211W */ | |
1187 | ||
1188 | ||
6fc6879b JM |
1189 | /** |
1190 | * wpa_parse_wpa_ie_rsn - Parse RSN IE | |
1191 | * @rsn_ie: Buffer containing RSN IE | |
1192 | * @rsn_ie_len: RSN IE buffer length (including IE number and length octets) | |
1193 | * @data: Pointer to structure that will be filled in with parsed data | |
1194 | * Returns: 0 on success, <0 on failure | |
1195 | */ | |
1196 | int wpa_parse_wpa_ie_rsn(const u8 *rsn_ie, size_t rsn_ie_len, | |
1197 | struct wpa_ie_data *data) | |
1198 | { | |
6fc6879b JM |
1199 | const u8 *pos; |
1200 | int left; | |
1201 | int i, count; | |
1202 | ||
1203 | os_memset(data, 0, sizeof(*data)); | |
1204 | data->proto = WPA_PROTO_RSN; | |
1205 | data->pairwise_cipher = WPA_CIPHER_CCMP; | |
1206 | data->group_cipher = WPA_CIPHER_CCMP; | |
1207 | data->key_mgmt = WPA_KEY_MGMT_IEEE8021X; | |
1208 | data->capabilities = 0; | |
1209 | data->pmkid = NULL; | |
1210 | data->num_pmkid = 0; | |
1211 | #ifdef CONFIG_IEEE80211W | |
1212 | data->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC; | |
1213 | #else /* CONFIG_IEEE80211W */ | |
1214 | data->mgmt_group_cipher = 0; | |
1215 | #endif /* CONFIG_IEEE80211W */ | |
1216 | ||
1217 | if (rsn_ie_len == 0) { | |
1218 | /* No RSN IE - fail silently */ | |
1219 | return -1; | |
1220 | } | |
1221 | ||
1222 | if (rsn_ie_len < sizeof(struct rsn_ie_hdr)) { | |
1223 | wpa_printf(MSG_DEBUG, "%s: ie len too short %lu", | |
1224 | __func__, (unsigned long) rsn_ie_len); | |
1225 | return -1; | |
1226 | } | |
1227 | ||
0f8385e6 BG |
1228 | if (rsn_ie_len >= 6 && rsn_ie[1] >= 4 && |
1229 | rsn_ie[1] == rsn_ie_len - 2 && | |
1230 | WPA_GET_BE32(&rsn_ie[2]) == OSEN_IE_VENDOR_TYPE) { | |
1231 | pos = rsn_ie + 6; | |
1232 | left = rsn_ie_len - 6; | |
6fc6879b | 1233 | |
dc673aec | 1234 | data->group_cipher = WPA_CIPHER_GTK_NOT_USED; |
2c129a1b | 1235 | data->has_group = 1; |
dc673aec | 1236 | data->key_mgmt = WPA_KEY_MGMT_OSEN; |
0f8385e6 BG |
1237 | data->proto = WPA_PROTO_OSEN; |
1238 | } else { | |
1239 | const struct rsn_ie_hdr *hdr; | |
1240 | ||
1241 | hdr = (const struct rsn_ie_hdr *) rsn_ie; | |
6fc6879b | 1242 | |
0f8385e6 BG |
1243 | if (hdr->elem_id != WLAN_EID_RSN || |
1244 | hdr->len != rsn_ie_len - 2 || | |
1245 | WPA_GET_LE16(hdr->version) != RSN_VERSION) { | |
1246 | wpa_printf(MSG_DEBUG, "%s: malformed ie or unknown version", | |
1247 | __func__); | |
1248 | return -2; | |
1249 | } | |
1250 | ||
1251 | pos = (const u8 *) (hdr + 1); | |
1252 | left = rsn_ie_len - sizeof(*hdr); | |
1253 | } | |
6fc6879b JM |
1254 | |
1255 | if (left >= RSN_SELECTOR_LEN) { | |
1256 | data->group_cipher = rsn_selector_to_bitfield(pos); | |
2c129a1b | 1257 | data->has_group = 1; |
8dd9f9cd | 1258 | if (!wpa_cipher_valid_group(data->group_cipher)) { |
0b905c8a JM |
1259 | wpa_printf(MSG_DEBUG, |
1260 | "%s: invalid group cipher 0x%x (%08x)", | |
1261 | __func__, data->group_cipher, | |
1262 | WPA_GET_BE32(pos)); | |
6fc6879b JM |
1263 | return -1; |
1264 | } | |
6fc6879b JM |
1265 | pos += RSN_SELECTOR_LEN; |
1266 | left -= RSN_SELECTOR_LEN; | |
1267 | } else if (left > 0) { | |
1268 | wpa_printf(MSG_DEBUG, "%s: ie length mismatch, %u too much", | |
1269 | __func__, left); | |
1270 | return -3; | |
1271 | } | |
1272 | ||
1273 | if (left >= 2) { | |
1274 | data->pairwise_cipher = 0; | |
1275 | count = WPA_GET_LE16(pos); | |
1276 | pos += 2; | |
1277 | left -= 2; | |
649c0a69 | 1278 | if (count == 0 || count > left / RSN_SELECTOR_LEN) { |
6fc6879b JM |
1279 | wpa_printf(MSG_DEBUG, "%s: ie count botch (pairwise), " |
1280 | "count %u left %u", __func__, count, left); | |
1281 | return -4; | |
1282 | } | |
2c129a1b LD |
1283 | if (count) |
1284 | data->has_pairwise = 1; | |
6fc6879b JM |
1285 | for (i = 0; i < count; i++) { |
1286 | data->pairwise_cipher |= rsn_selector_to_bitfield(pos); | |
1287 | pos += RSN_SELECTOR_LEN; | |
1288 | left -= RSN_SELECTOR_LEN; | |
1289 | } | |
1290 | #ifdef CONFIG_IEEE80211W | |
1291 | if (data->pairwise_cipher & WPA_CIPHER_AES_128_CMAC) { | |
1292 | wpa_printf(MSG_DEBUG, "%s: AES-128-CMAC used as " | |
1293 | "pairwise cipher", __func__); | |
1294 | return -1; | |
1295 | } | |
1296 | #endif /* CONFIG_IEEE80211W */ | |
1297 | } else if (left == 1) { | |
1298 | wpa_printf(MSG_DEBUG, "%s: ie too short (for key mgmt)", | |
1299 | __func__); | |
1300 | return -5; | |
1301 | } | |
1302 | ||
1303 | if (left >= 2) { | |
1304 | data->key_mgmt = 0; | |
1305 | count = WPA_GET_LE16(pos); | |
1306 | pos += 2; | |
1307 | left -= 2; | |
649c0a69 | 1308 | if (count == 0 || count > left / RSN_SELECTOR_LEN) { |
6fc6879b JM |
1309 | wpa_printf(MSG_DEBUG, "%s: ie count botch (key mgmt), " |
1310 | "count %u left %u", __func__, count, left); | |
1311 | return -6; | |
1312 | } | |
1313 | for (i = 0; i < count; i++) { | |
1314 | data->key_mgmt |= rsn_key_mgmt_to_bitfield(pos); | |
1315 | pos += RSN_SELECTOR_LEN; | |
1316 | left -= RSN_SELECTOR_LEN; | |
1317 | } | |
1318 | } else if (left == 1) { | |
1319 | wpa_printf(MSG_DEBUG, "%s: ie too short (for capabilities)", | |
1320 | __func__); | |
1321 | return -7; | |
1322 | } | |
1323 | ||
1324 | if (left >= 2) { | |
1325 | data->capabilities = WPA_GET_LE16(pos); | |
1326 | pos += 2; | |
1327 | left -= 2; | |
1328 | } | |
1329 | ||
1330 | if (left >= 2) { | |
46a03525 | 1331 | u16 num_pmkid = WPA_GET_LE16(pos); |
6fc6879b JM |
1332 | pos += 2; |
1333 | left -= 2; | |
46a03525 | 1334 | if (num_pmkid > (unsigned int) left / PMKID_LEN) { |
6fc6879b | 1335 | wpa_printf(MSG_DEBUG, "%s: PMKID underflow " |
46a03525 JM |
1336 | "(num_pmkid=%u left=%d)", |
1337 | __func__, num_pmkid, left); | |
6fc6879b JM |
1338 | data->num_pmkid = 0; |
1339 | return -9; | |
1340 | } else { | |
46a03525 | 1341 | data->num_pmkid = num_pmkid; |
6fc6879b JM |
1342 | data->pmkid = pos; |
1343 | pos += data->num_pmkid * PMKID_LEN; | |
1344 | left -= data->num_pmkid * PMKID_LEN; | |
1345 | } | |
1346 | } | |
1347 | ||
1348 | #ifdef CONFIG_IEEE80211W | |
1349 | if (left >= 4) { | |
1350 | data->mgmt_group_cipher = rsn_selector_to_bitfield(pos); | |
8dd9f9cd | 1351 | if (!wpa_cipher_valid_mgmt_group(data->mgmt_group_cipher)) { |
b0ecbd3a JM |
1352 | wpa_printf(MSG_DEBUG, |
1353 | "%s: Unsupported management group cipher 0x%x (%08x)", | |
1354 | __func__, data->mgmt_group_cipher, | |
1355 | WPA_GET_BE32(pos)); | |
6fc6879b JM |
1356 | return -10; |
1357 | } | |
1358 | pos += RSN_SELECTOR_LEN; | |
1359 | left -= RSN_SELECTOR_LEN; | |
1360 | } | |
1361 | #endif /* CONFIG_IEEE80211W */ | |
1362 | ||
1363 | if (left > 0) { | |
749fa140 JM |
1364 | wpa_hexdump(MSG_DEBUG, |
1365 | "wpa_parse_wpa_ie_rsn: ignore trailing bytes", | |
1366 | pos, left); | |
6fc6879b JM |
1367 | } |
1368 | ||
1369 | return 0; | |
6fc6879b JM |
1370 | } |
1371 | ||
1372 | ||
f3b87561 JM |
1373 | static int wpa_selector_to_bitfield(const u8 *s) |
1374 | { | |
1375 | if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_NONE) | |
1376 | return WPA_CIPHER_NONE; | |
f3b87561 JM |
1377 | if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_TKIP) |
1378 | return WPA_CIPHER_TKIP; | |
1379 | if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_CCMP) | |
1380 | return WPA_CIPHER_CCMP; | |
f3b87561 JM |
1381 | return 0; |
1382 | } | |
1383 | ||
1384 | ||
1385 | static int wpa_key_mgmt_to_bitfield(const u8 *s) | |
1386 | { | |
1387 | if (RSN_SELECTOR_GET(s) == WPA_AUTH_KEY_MGMT_UNSPEC_802_1X) | |
1388 | return WPA_KEY_MGMT_IEEE8021X; | |
1389 | if (RSN_SELECTOR_GET(s) == WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X) | |
1390 | return WPA_KEY_MGMT_PSK; | |
1391 | if (RSN_SELECTOR_GET(s) == WPA_AUTH_KEY_MGMT_NONE) | |
1392 | return WPA_KEY_MGMT_WPA_NONE; | |
1393 | return 0; | |
1394 | } | |
1395 | ||
1396 | ||
1397 | int wpa_parse_wpa_ie_wpa(const u8 *wpa_ie, size_t wpa_ie_len, | |
1398 | struct wpa_ie_data *data) | |
1399 | { | |
1400 | const struct wpa_ie_hdr *hdr; | |
1401 | const u8 *pos; | |
1402 | int left; | |
1403 | int i, count; | |
1404 | ||
1405 | os_memset(data, 0, sizeof(*data)); | |
1406 | data->proto = WPA_PROTO_WPA; | |
1407 | data->pairwise_cipher = WPA_CIPHER_TKIP; | |
1408 | data->group_cipher = WPA_CIPHER_TKIP; | |
1409 | data->key_mgmt = WPA_KEY_MGMT_IEEE8021X; | |
1410 | data->capabilities = 0; | |
1411 | data->pmkid = NULL; | |
1412 | data->num_pmkid = 0; | |
1413 | data->mgmt_group_cipher = 0; | |
1414 | ||
f3b87561 JM |
1415 | if (wpa_ie_len < sizeof(struct wpa_ie_hdr)) { |
1416 | wpa_printf(MSG_DEBUG, "%s: ie len too short %lu", | |
1417 | __func__, (unsigned long) wpa_ie_len); | |
1418 | return -1; | |
1419 | } | |
1420 | ||
1421 | hdr = (const struct wpa_ie_hdr *) wpa_ie; | |
1422 | ||
1423 | if (hdr->elem_id != WLAN_EID_VENDOR_SPECIFIC || | |
1424 | hdr->len != wpa_ie_len - 2 || | |
1425 | RSN_SELECTOR_GET(hdr->oui) != WPA_OUI_TYPE || | |
1426 | WPA_GET_LE16(hdr->version) != WPA_VERSION) { | |
1427 | wpa_printf(MSG_DEBUG, "%s: malformed ie or unknown version", | |
1428 | __func__); | |
1429 | return -2; | |
1430 | } | |
1431 | ||
1432 | pos = (const u8 *) (hdr + 1); | |
1433 | left = wpa_ie_len - sizeof(*hdr); | |
1434 | ||
1435 | if (left >= WPA_SELECTOR_LEN) { | |
1436 | data->group_cipher = wpa_selector_to_bitfield(pos); | |
1437 | pos += WPA_SELECTOR_LEN; | |
1438 | left -= WPA_SELECTOR_LEN; | |
1439 | } else if (left > 0) { | |
1440 | wpa_printf(MSG_DEBUG, "%s: ie length mismatch, %u too much", | |
1441 | __func__, left); | |
1442 | return -3; | |
1443 | } | |
1444 | ||
1445 | if (left >= 2) { | |
1446 | data->pairwise_cipher = 0; | |
1447 | count = WPA_GET_LE16(pos); | |
1448 | pos += 2; | |
1449 | left -= 2; | |
649c0a69 | 1450 | if (count == 0 || count > left / WPA_SELECTOR_LEN) { |
f3b87561 JM |
1451 | wpa_printf(MSG_DEBUG, "%s: ie count botch (pairwise), " |
1452 | "count %u left %u", __func__, count, left); | |
1453 | return -4; | |
1454 | } | |
1455 | for (i = 0; i < count; i++) { | |
1456 | data->pairwise_cipher |= wpa_selector_to_bitfield(pos); | |
1457 | pos += WPA_SELECTOR_LEN; | |
1458 | left -= WPA_SELECTOR_LEN; | |
1459 | } | |
1460 | } else if (left == 1) { | |
1461 | wpa_printf(MSG_DEBUG, "%s: ie too short (for key mgmt)", | |
1462 | __func__); | |
1463 | return -5; | |
1464 | } | |
1465 | ||
1466 | if (left >= 2) { | |
1467 | data->key_mgmt = 0; | |
1468 | count = WPA_GET_LE16(pos); | |
1469 | pos += 2; | |
1470 | left -= 2; | |
649c0a69 | 1471 | if (count == 0 || count > left / WPA_SELECTOR_LEN) { |
f3b87561 JM |
1472 | wpa_printf(MSG_DEBUG, "%s: ie count botch (key mgmt), " |
1473 | "count %u left %u", __func__, count, left); | |
1474 | return -6; | |
1475 | } | |
1476 | for (i = 0; i < count; i++) { | |
1477 | data->key_mgmt |= wpa_key_mgmt_to_bitfield(pos); | |
1478 | pos += WPA_SELECTOR_LEN; | |
1479 | left -= WPA_SELECTOR_LEN; | |
1480 | } | |
1481 | } else if (left == 1) { | |
1482 | wpa_printf(MSG_DEBUG, "%s: ie too short (for capabilities)", | |
1483 | __func__); | |
1484 | return -7; | |
1485 | } | |
1486 | ||
1487 | if (left >= 2) { | |
1488 | data->capabilities = WPA_GET_LE16(pos); | |
1489 | pos += 2; | |
1490 | left -= 2; | |
1491 | } | |
1492 | ||
1493 | if (left > 0) { | |
749fa140 JM |
1494 | wpa_hexdump(MSG_DEBUG, |
1495 | "wpa_parse_wpa_ie_wpa: ignore trailing bytes", | |
1496 | pos, left); | |
f3b87561 JM |
1497 | } |
1498 | ||
1499 | return 0; | |
1500 | } | |
1501 | ||
1502 | ||
2c129a1b LD |
1503 | int wpa_default_rsn_cipher(int freq) |
1504 | { | |
1505 | if (freq > 56160) | |
1506 | return WPA_CIPHER_GCMP; /* DMG */ | |
1507 | ||
1508 | return WPA_CIPHER_CCMP; | |
1509 | } | |
1510 | ||
1511 | ||
6fc6879b JM |
1512 | #ifdef CONFIG_IEEE80211R |
1513 | ||
1514 | /** | |
1515 | * wpa_derive_pmk_r0 - Derive PMK-R0 and PMKR0Name | |
1516 | * | |
c1e033b0 | 1517 | * IEEE Std 802.11r-2008 - 8.5.1.5.3 |
6fc6879b | 1518 | */ |
364c064a JM |
1519 | int wpa_derive_pmk_r0(const u8 *xxkey, size_t xxkey_len, |
1520 | const u8 *ssid, size_t ssid_len, | |
1521 | const u8 *mdid, const u8 *r0kh_id, size_t r0kh_id_len, | |
994eac7e JM |
1522 | const u8 *s0kh_id, u8 *pmk_r0, u8 *pmk_r0_name, |
1523 | int use_sha384) | |
6fc6879b | 1524 | { |
6fb761c6 | 1525 | u8 buf[1 + SSID_MAX_LEN + MOBILITY_DOMAIN_ID_LEN + 1 + |
6fc6879b | 1526 | FT_R0KH_ID_MAX_LEN + ETH_ALEN]; |
c3e4f40c | 1527 | u8 *pos, r0_key_data[64], hash[48]; |
6fc6879b JM |
1528 | const u8 *addr[2]; |
1529 | size_t len[2]; | |
994eac7e JM |
1530 | size_t q = use_sha384 ? 48 : 32; |
1531 | size_t r0_key_data_len = q + 16; | |
6fc6879b JM |
1532 | |
1533 | /* | |
1534 | * R0-Key-Data = KDF-384(XXKey, "FT-R0", | |
1535 | * SSIDlength || SSID || MDID || R0KHlength || | |
1536 | * R0KH-ID || S0KH-ID) | |
994eac7e JM |
1537 | * XXKey is either the second 256 bits of MSK or PSK; or the first |
1538 | * 384 bits of MSK for FT-EAP-SHA384. | |
1539 | * PMK-R0 = L(R0-Key-Data, 0, Q) | |
1540 | * PMK-R0Name-Salt = L(R0-Key-Data, Q, 128) | |
1541 | * Q = 384 for FT-EAP-SHA384; otherwise, 256 | |
6fc6879b | 1542 | */ |
6fb761c6 | 1543 | if (ssid_len > SSID_MAX_LEN || r0kh_id_len > FT_R0KH_ID_MAX_LEN) |
364c064a | 1544 | return -1; |
994eac7e JM |
1545 | wpa_printf(MSG_DEBUG, "FT: Derive PMK-R0 using KDF-%s", |
1546 | use_sha384 ? "SHA384" : "SHA256"); | |
1547 | wpa_hexdump_key(MSG_DEBUG, "FT: XXKey", xxkey, xxkey_len); | |
1548 | wpa_hexdump_ascii(MSG_DEBUG, "FT: SSID", ssid, ssid_len); | |
1549 | wpa_hexdump(MSG_DEBUG, "FT: MDID", mdid, MOBILITY_DOMAIN_ID_LEN); | |
1550 | wpa_hexdump_ascii(MSG_DEBUG, "FT: R0KH-ID", r0kh_id, r0kh_id_len); | |
1551 | wpa_printf(MSG_DEBUG, "FT: S0KH-ID: " MACSTR, MAC2STR(s0kh_id)); | |
6fc6879b JM |
1552 | pos = buf; |
1553 | *pos++ = ssid_len; | |
1554 | os_memcpy(pos, ssid, ssid_len); | |
1555 | pos += ssid_len; | |
1556 | os_memcpy(pos, mdid, MOBILITY_DOMAIN_ID_LEN); | |
1557 | pos += MOBILITY_DOMAIN_ID_LEN; | |
1558 | *pos++ = r0kh_id_len; | |
1559 | os_memcpy(pos, r0kh_id, r0kh_id_len); | |
1560 | pos += r0kh_id_len; | |
1561 | os_memcpy(pos, s0kh_id, ETH_ALEN); | |
1562 | pos += ETH_ALEN; | |
1563 | ||
994eac7e JM |
1564 | #ifdef CONFIG_SHA384 |
1565 | if (use_sha384) { | |
1566 | if (xxkey_len != SHA384_MAC_LEN) { | |
1567 | wpa_printf(MSG_ERROR, | |
1568 | "FT: Unexpected XXKey length %d (expected %d)", | |
1569 | (int) xxkey_len, SHA384_MAC_LEN); | |
1570 | return -1; | |
1571 | } | |
1572 | if (sha384_prf(xxkey, xxkey_len, "FT-R0", buf, pos - buf, | |
1573 | r0_key_data, r0_key_data_len) < 0) | |
1574 | return -1; | |
1575 | } | |
1576 | #endif /* CONFIG_SHA384 */ | |
1577 | if (!use_sha384) { | |
1578 | if (xxkey_len != PMK_LEN) { | |
1579 | wpa_printf(MSG_ERROR, | |
1580 | "FT: Unexpected XXKey length %d (expected %d)", | |
1581 | (int) xxkey_len, PMK_LEN); | |
1582 | return -1; | |
1583 | } | |
1584 | if (sha256_prf(xxkey, xxkey_len, "FT-R0", buf, pos - buf, | |
1585 | r0_key_data, r0_key_data_len) < 0) | |
1586 | return -1; | |
1587 | } | |
1588 | os_memcpy(pmk_r0, r0_key_data, q); | |
1589 | wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R0", pmk_r0, q); | |
1590 | wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R0Name-Salt", &r0_key_data[q], 16); | |
6fc6879b JM |
1591 | |
1592 | /* | |
c3e4f40c | 1593 | * PMKR0Name = Truncate-128(Hash("FT-R0N" || PMK-R0Name-Salt) |
6fc6879b JM |
1594 | */ |
1595 | addr[0] = (const u8 *) "FT-R0N"; | |
1596 | len[0] = 6; | |
994eac7e | 1597 | addr[1] = &r0_key_data[q]; |
6fc6879b JM |
1598 | len[1] = 16; |
1599 | ||
c3e4f40c JM |
1600 | #ifdef CONFIG_SHA384 |
1601 | if (use_sha384 && sha384_vector(2, addr, len, hash) < 0) | |
1602 | return -1; | |
1603 | #endif /* CONFIG_SHA384 */ | |
1604 | if (!use_sha384 && sha256_vector(2, addr, len, hash) < 0) | |
364c064a | 1605 | return -1; |
6fc6879b | 1606 | os_memcpy(pmk_r0_name, hash, WPA_PMK_NAME_LEN); |
994eac7e | 1607 | os_memset(r0_key_data, 0, sizeof(r0_key_data)); |
364c064a | 1608 | return 0; |
6fc6879b JM |
1609 | } |
1610 | ||
1611 | ||
1612 | /** | |
1613 | * wpa_derive_pmk_r1_name - Derive PMKR1Name | |
1614 | * | |
c1e033b0 | 1615 | * IEEE Std 802.11r-2008 - 8.5.1.5.4 |
6fc6879b | 1616 | */ |
364c064a | 1617 | int wpa_derive_pmk_r1_name(const u8 *pmk_r0_name, const u8 *r1kh_id, |
c3e4f40c | 1618 | const u8 *s1kh_id, u8 *pmk_r1_name, int use_sha384) |
6fc6879b | 1619 | { |
c3e4f40c | 1620 | u8 hash[48]; |
6fc6879b JM |
1621 | const u8 *addr[4]; |
1622 | size_t len[4]; | |
1623 | ||
1624 | /* | |
c3e4f40c JM |
1625 | * PMKR1Name = Truncate-128(Hash("FT-R1N" || PMKR0Name || |
1626 | * R1KH-ID || S1KH-ID)) | |
6fc6879b JM |
1627 | */ |
1628 | addr[0] = (const u8 *) "FT-R1N"; | |
1629 | len[0] = 6; | |
1630 | addr[1] = pmk_r0_name; | |
1631 | len[1] = WPA_PMK_NAME_LEN; | |
1632 | addr[2] = r1kh_id; | |
1633 | len[2] = FT_R1KH_ID_LEN; | |
1634 | addr[3] = s1kh_id; | |
1635 | len[3] = ETH_ALEN; | |
1636 | ||
c3e4f40c JM |
1637 | #ifdef CONFIG_SHA384 |
1638 | if (use_sha384 && sha384_vector(4, addr, len, hash) < 0) | |
1639 | return -1; | |
1640 | #endif /* CONFIG_SHA384 */ | |
1641 | if (!use_sha384 && sha256_vector(4, addr, len, hash) < 0) | |
364c064a | 1642 | return -1; |
6fc6879b | 1643 | os_memcpy(pmk_r1_name, hash, WPA_PMK_NAME_LEN); |
364c064a | 1644 | return 0; |
6fc6879b JM |
1645 | } |
1646 | ||
1647 | ||
1648 | /** | |
1649 | * wpa_derive_pmk_r1 - Derive PMK-R1 and PMKR1Name from PMK-R0 | |
1650 | * | |
c1e033b0 | 1651 | * IEEE Std 802.11r-2008 - 8.5.1.5.4 |
6fc6879b | 1652 | */ |
a3e18dbb JM |
1653 | int wpa_derive_pmk_r1(const u8 *pmk_r0, size_t pmk_r0_len, |
1654 | const u8 *pmk_r0_name, | |
364c064a JM |
1655 | const u8 *r1kh_id, const u8 *s1kh_id, |
1656 | u8 *pmk_r1, u8 *pmk_r1_name) | |
6fc6879b JM |
1657 | { |
1658 | u8 buf[FT_R1KH_ID_LEN + ETH_ALEN]; | |
1659 | u8 *pos; | |
1660 | ||
1661 | /* PMK-R1 = KDF-256(PMK-R0, "FT-R1", R1KH-ID || S1KH-ID) */ | |
7880a6a2 JM |
1662 | wpa_printf(MSG_DEBUG, "FT: Derive PMK-R1 using KDF-%s", |
1663 | pmk_r0_len == SHA384_MAC_LEN ? "SHA384" : "SHA256"); | |
1664 | wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R0", pmk_r0, pmk_r0_len); | |
1665 | wpa_hexdump(MSG_DEBUG, "FT: R1KH-ID", r1kh_id, FT_R1KH_ID_LEN); | |
1666 | wpa_printf(MSG_DEBUG, "FT: S1KH-ID: " MACSTR, MAC2STR(s1kh_id)); | |
6fc6879b JM |
1667 | pos = buf; |
1668 | os_memcpy(pos, r1kh_id, FT_R1KH_ID_LEN); | |
1669 | pos += FT_R1KH_ID_LEN; | |
1670 | os_memcpy(pos, s1kh_id, ETH_ALEN); | |
1671 | pos += ETH_ALEN; | |
1672 | ||
7880a6a2 JM |
1673 | #ifdef CONFIG_SHA384 |
1674 | if (pmk_r0_len == SHA384_MAC_LEN && | |
1675 | sha384_prf(pmk_r0, pmk_r0_len, "FT-R1", | |
1676 | buf, pos - buf, pmk_r1, pmk_r0_len) < 0) | |
1677 | return -1; | |
1678 | #endif /* CONFIG_SHA384 */ | |
1679 | if (pmk_r0_len == PMK_LEN && | |
1680 | sha256_prf(pmk_r0, pmk_r0_len, "FT-R1", | |
1681 | buf, pos - buf, pmk_r1, pmk_r0_len) < 0) | |
364c064a | 1682 | return -1; |
7880a6a2 JM |
1683 | if (pmk_r0_len != SHA384_MAC_LEN && pmk_r0_len != PMK_LEN) { |
1684 | wpa_printf(MSG_ERROR, "FT: Unexpected PMK-R0 length %d", | |
1685 | (int) pmk_r0_len); | |
1686 | return -1; | |
1687 | } | |
1688 | wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", pmk_r1, pmk_r0_len); | |
6fc6879b | 1689 | |
364c064a | 1690 | return wpa_derive_pmk_r1_name(pmk_r0_name, r1kh_id, s1kh_id, |
c3e4f40c JM |
1691 | pmk_r1_name, |
1692 | pmk_r0_len == SHA384_MAC_LEN); | |
6fc6879b JM |
1693 | } |
1694 | ||
1695 | ||
1696 | /** | |
1697 | * wpa_pmk_r1_to_ptk - Derive PTK and PTKName from PMK-R1 | |
1698 | * | |
c1e033b0 | 1699 | * IEEE Std 802.11r-2008 - 8.5.1.5.5 |
6fc6879b | 1700 | */ |
a3e18dbb JM |
1701 | int wpa_pmk_r1_to_ptk(const u8 *pmk_r1, size_t pmk_r1_len, |
1702 | const u8 *snonce, const u8 *anonce, | |
98cd3d1c JM |
1703 | const u8 *sta_addr, const u8 *bssid, |
1704 | const u8 *pmk_r1_name, | |
1705 | struct wpa_ptk *ptk, u8 *ptk_name, int akmp, int cipher) | |
6fc6879b JM |
1706 | { |
1707 | u8 buf[2 * WPA_NONCE_LEN + 2 * ETH_ALEN]; | |
1708 | u8 *pos, hash[32]; | |
1709 | const u8 *addr[6]; | |
1710 | size_t len[6]; | |
2f373878 JM |
1711 | u8 tmp[2 * WPA_KCK_MAX_LEN + 2 * WPA_KEK_MAX_LEN + WPA_TK_MAX_LEN]; |
1712 | size_t ptk_len, offset; | |
40a2eb11 | 1713 | int use_sha384 = wpa_key_mgmt_sha384(akmp); |
6fc6879b JM |
1714 | |
1715 | /* | |
1716 | * PTK = KDF-PTKLen(PMK-R1, "FT-PTK", SNonce || ANonce || | |
1717 | * BSSID || STA-ADDR) | |
1718 | */ | |
40a2eb11 JM |
1719 | wpa_printf(MSG_DEBUG, "FT: Derive PTK using KDF-%s", |
1720 | use_sha384 ? "SHA384" : "SHA256"); | |
1721 | wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", pmk_r1, pmk_r1_len); | |
1722 | wpa_hexdump(MSG_DEBUG, "FT: SNonce", snonce, WPA_NONCE_LEN); | |
1723 | wpa_hexdump(MSG_DEBUG, "FT: ANonce", anonce, WPA_NONCE_LEN); | |
1724 | wpa_printf(MSG_DEBUG, "FT: BSSID=" MACSTR " STA-ADDR=" MACSTR, | |
1725 | MAC2STR(bssid), MAC2STR(sta_addr)); | |
6fc6879b JM |
1726 | pos = buf; |
1727 | os_memcpy(pos, snonce, WPA_NONCE_LEN); | |
1728 | pos += WPA_NONCE_LEN; | |
1729 | os_memcpy(pos, anonce, WPA_NONCE_LEN); | |
1730 | pos += WPA_NONCE_LEN; | |
1731 | os_memcpy(pos, bssid, ETH_ALEN); | |
1732 | pos += ETH_ALEN; | |
1733 | os_memcpy(pos, sta_addr, ETH_ALEN); | |
1734 | pos += ETH_ALEN; | |
1735 | ||
567da5bb | 1736 | ptk->kck_len = wpa_kck_len(akmp, PMK_LEN); |
2f373878 | 1737 | ptk->kck2_len = wpa_kck2_len(akmp); |
567da5bb | 1738 | ptk->kek_len = wpa_kek_len(akmp, PMK_LEN); |
2f373878 | 1739 | ptk->kek2_len = wpa_kek2_len(akmp); |
98cd3d1c | 1740 | ptk->tk_len = wpa_cipher_key_len(cipher); |
2f373878 JM |
1741 | ptk_len = ptk->kck_len + ptk->kek_len + ptk->tk_len + |
1742 | ptk->kck2_len + ptk->kek2_len; | |
98cd3d1c | 1743 | |
40a2eb11 JM |
1744 | #ifdef CONFIG_SHA384 |
1745 | if (use_sha384) { | |
1746 | if (pmk_r1_len != SHA384_MAC_LEN) { | |
1747 | wpa_printf(MSG_ERROR, | |
1748 | "FT: Unexpected PMK-R1 length %d (expected %d)", | |
1749 | (int) pmk_r1_len, SHA384_MAC_LEN); | |
1750 | return -1; | |
1751 | } | |
1752 | if (sha384_prf(pmk_r1, pmk_r1_len, "FT-PTK", | |
1753 | buf, pos - buf, tmp, ptk_len) < 0) | |
1754 | return -1; | |
1755 | } | |
1756 | #endif /* CONFIG_SHA384 */ | |
1757 | if (!use_sha384) { | |
1758 | if (pmk_r1_len != PMK_LEN) { | |
1759 | wpa_printf(MSG_ERROR, | |
1760 | "FT: Unexpected PMK-R1 length %d (expected %d)", | |
1761 | (int) pmk_r1_len, PMK_LEN); | |
1762 | return -1; | |
1763 | } | |
1764 | if (sha256_prf(pmk_r1, pmk_r1_len, "FT-PTK", | |
1765 | buf, pos - buf, tmp, ptk_len) < 0) | |
1766 | return -1; | |
1767 | } | |
1768 | wpa_hexdump_key(MSG_DEBUG, "FT: PTK", tmp, ptk_len); | |
6fc6879b JM |
1769 | |
1770 | /* | |
1771 | * PTKName = Truncate-128(SHA-256(PMKR1Name || "FT-PTKN" || SNonce || | |
1772 | * ANonce || BSSID || STA-ADDR)) | |
1773 | */ | |
40a2eb11 | 1774 | wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", pmk_r1_name, WPA_PMK_NAME_LEN); |
6fc6879b JM |
1775 | addr[0] = pmk_r1_name; |
1776 | len[0] = WPA_PMK_NAME_LEN; | |
1777 | addr[1] = (const u8 *) "FT-PTKN"; | |
1778 | len[1] = 7; | |
1779 | addr[2] = snonce; | |
1780 | len[2] = WPA_NONCE_LEN; | |
1781 | addr[3] = anonce; | |
1782 | len[3] = WPA_NONCE_LEN; | |
1783 | addr[4] = bssid; | |
1784 | len[4] = ETH_ALEN; | |
1785 | addr[5] = sta_addr; | |
1786 | len[5] = ETH_ALEN; | |
1787 | ||
364c064a JM |
1788 | if (sha256_vector(6, addr, len, hash) < 0) |
1789 | return -1; | |
6fc6879b | 1790 | os_memcpy(ptk_name, hash, WPA_PMK_NAME_LEN); |
98cd3d1c JM |
1791 | |
1792 | os_memcpy(ptk->kck, tmp, ptk->kck_len); | |
2f373878 JM |
1793 | offset = ptk->kck_len; |
1794 | os_memcpy(ptk->kek, tmp + offset, ptk->kek_len); | |
1795 | offset += ptk->kek_len; | |
1796 | os_memcpy(ptk->tk, tmp + offset, ptk->tk_len); | |
1797 | offset += ptk->tk_len; | |
1798 | os_memcpy(ptk->kck2, tmp + offset, ptk->kck2_len); | |
13044473 | 1799 | offset += ptk->kck2_len; |
2f373878 | 1800 | os_memcpy(ptk->kek2, tmp + offset, ptk->kek2_len); |
98cd3d1c JM |
1801 | |
1802 | wpa_hexdump_key(MSG_DEBUG, "FT: KCK", ptk->kck, ptk->kck_len); | |
1803 | wpa_hexdump_key(MSG_DEBUG, "FT: KEK", ptk->kek, ptk->kek_len); | |
2f373878 JM |
1804 | if (ptk->kck2_len) |
1805 | wpa_hexdump_key(MSG_DEBUG, "FT: KCK2", | |
1806 | ptk->kck2, ptk->kck2_len); | |
1807 | if (ptk->kek2_len) | |
1808 | wpa_hexdump_key(MSG_DEBUG, "FT: KEK2", | |
1809 | ptk->kek2, ptk->kek2_len); | |
98cd3d1c JM |
1810 | wpa_hexdump_key(MSG_DEBUG, "FT: TK", ptk->tk, ptk->tk_len); |
1811 | wpa_hexdump(MSG_DEBUG, "FT: PTKName", ptk_name, WPA_PMK_NAME_LEN); | |
1812 | ||
1813 | os_memset(tmp, 0, sizeof(tmp)); | |
1814 | ||
1815 | return 0; | |
6fc6879b JM |
1816 | } |
1817 | ||
1818 | #endif /* CONFIG_IEEE80211R */ | |
13268290 JM |
1819 | |
1820 | ||
1821 | /** | |
1822 | * rsn_pmkid - Calculate PMK identifier | |
1823 | * @pmk: Pairwise master key | |
1824 | * @pmk_len: Length of pmk in bytes | |
1825 | * @aa: Authenticator address | |
1826 | * @spa: Supplicant address | |
1827 | * @pmkid: Buffer for PMKID | |
41b81914 | 1828 | * @akmp: Negotiated key management protocol |
13268290 | 1829 | * |
41b81914 JM |
1830 | * IEEE Std 802.11-2016 - 12.7.1.3 Pairwise key hierarchy |
1831 | * AKM: 00-0F-AC:5, 00-0F-AC:6, 00-0F-AC:14, 00-0F-AC:16 | |
1832 | * PMKID = Truncate-128(HMAC-SHA-256(PMK, "PMK Name" || AA || SPA)) | |
1833 | * AKM: 00-0F-AC:11 | |
1834 | * See rsn_pmkid_suite_b() | |
1835 | * AKM: 00-0F-AC:12 | |
1836 | * See rsn_pmkid_suite_b_192() | |
1655e81c | 1837 | * AKM: 00-0F-AC:13, 00-0F-AC:15, 00-0F-AC:17 |
41b81914 JM |
1838 | * PMKID = Truncate-128(HMAC-SHA-384(PMK, "PMK Name" || AA || SPA)) |
1839 | * Otherwise: | |
1840 | * PMKID = Truncate-128(HMAC-SHA-1(PMK, "PMK Name" || AA || SPA)) | |
13268290 JM |
1841 | */ |
1842 | void rsn_pmkid(const u8 *pmk, size_t pmk_len, const u8 *aa, const u8 *spa, | |
41b81914 | 1843 | u8 *pmkid, int akmp) |
13268290 JM |
1844 | { |
1845 | char *title = "PMK Name"; | |
1846 | const u8 *addr[3]; | |
1847 | const size_t len[3] = { 8, ETH_ALEN, ETH_ALEN }; | |
41b81914 | 1848 | unsigned char hash[SHA384_MAC_LEN]; |
13268290 JM |
1849 | |
1850 | addr[0] = (u8 *) title; | |
1851 | addr[1] = aa; | |
1852 | addr[2] = spa; | |
1853 | ||
41b81914 | 1854 | if (0) { |
1655e81c | 1855 | #if defined(CONFIG_FILS) || defined(CONFIG_SHA384) |
41b81914 JM |
1856 | } else if (wpa_key_mgmt_sha384(akmp)) { |
1857 | wpa_printf(MSG_DEBUG, "RSN: Derive PMKID using HMAC-SHA-384"); | |
1858 | hmac_sha384_vector(pmk, pmk_len, 3, addr, len, hash); | |
1655e81c | 1859 | #endif /* CONFIG_FILS || CONFIG_SHA384 */ |
41b81914 JM |
1860 | #if defined(CONFIG_IEEE80211W) || defined(CONFIG_FILS) |
1861 | } else if (wpa_key_mgmt_sha256(akmp)) { | |
1862 | wpa_printf(MSG_DEBUG, "RSN: Derive PMKID using HMAC-SHA-256"); | |
13268290 | 1863 | hmac_sha256_vector(pmk, pmk_len, 3, addr, len, hash); |
41b81914 JM |
1864 | #endif /* CONFIG_IEEE80211W || CONFIG_FILS */ |
1865 | } else { | |
1866 | wpa_printf(MSG_DEBUG, "RSN: Derive PMKID using HMAC-SHA-1"); | |
13268290 | 1867 | hmac_sha1_vector(pmk, pmk_len, 3, addr, len, hash); |
41b81914 JM |
1868 | } |
1869 | wpa_hexdump(MSG_DEBUG, "RSN: Derived PMKID", hash, PMKID_LEN); | |
13268290 JM |
1870 | os_memcpy(pmkid, hash, PMKID_LEN); |
1871 | } | |
43fb5297 JM |
1872 | |
1873 | ||
087a1f4e JM |
1874 | #ifdef CONFIG_SUITEB |
1875 | /** | |
1876 | * rsn_pmkid_suite_b - Calculate PMK identifier for Suite B AKM | |
1877 | * @kck: Key confirmation key | |
1878 | * @kck_len: Length of kck in bytes | |
1879 | * @aa: Authenticator address | |
1880 | * @spa: Supplicant address | |
1881 | * @pmkid: Buffer for PMKID | |
1882 | * Returns: 0 on success, -1 on failure | |
1883 | * | |
1884 | * IEEE Std 802.11ac-2013 - 11.6.1.3 Pairwise key hierarchy | |
1885 | * PMKID = Truncate(HMAC-SHA-256(KCK, "PMK Name" || AA || SPA)) | |
1886 | */ | |
1887 | int rsn_pmkid_suite_b(const u8 *kck, size_t kck_len, const u8 *aa, | |
1888 | const u8 *spa, u8 *pmkid) | |
1889 | { | |
1890 | char *title = "PMK Name"; | |
1891 | const u8 *addr[3]; | |
1892 | const size_t len[3] = { 8, ETH_ALEN, ETH_ALEN }; | |
1893 | unsigned char hash[SHA256_MAC_LEN]; | |
1894 | ||
1895 | addr[0] = (u8 *) title; | |
1896 | addr[1] = aa; | |
1897 | addr[2] = spa; | |
1898 | ||
1899 | if (hmac_sha256_vector(kck, kck_len, 3, addr, len, hash) < 0) | |
1900 | return -1; | |
1901 | os_memcpy(pmkid, hash, PMKID_LEN); | |
1902 | return 0; | |
1903 | } | |
1904 | #endif /* CONFIG_SUITEB */ | |
1905 | ||
1906 | ||
5e3b5197 JM |
1907 | #ifdef CONFIG_SUITEB192 |
1908 | /** | |
1909 | * rsn_pmkid_suite_b_192 - Calculate PMK identifier for Suite B AKM | |
1910 | * @kck: Key confirmation key | |
1911 | * @kck_len: Length of kck in bytes | |
1912 | * @aa: Authenticator address | |
1913 | * @spa: Supplicant address | |
1914 | * @pmkid: Buffer for PMKID | |
1915 | * Returns: 0 on success, -1 on failure | |
1916 | * | |
1917 | * IEEE Std 802.11ac-2013 - 11.6.1.3 Pairwise key hierarchy | |
1918 | * PMKID = Truncate(HMAC-SHA-384(KCK, "PMK Name" || AA || SPA)) | |
1919 | */ | |
1920 | int rsn_pmkid_suite_b_192(const u8 *kck, size_t kck_len, const u8 *aa, | |
1921 | const u8 *spa, u8 *pmkid) | |
1922 | { | |
1923 | char *title = "PMK Name"; | |
1924 | const u8 *addr[3]; | |
1925 | const size_t len[3] = { 8, ETH_ALEN, ETH_ALEN }; | |
1926 | unsigned char hash[SHA384_MAC_LEN]; | |
1927 | ||
1928 | addr[0] = (u8 *) title; | |
1929 | addr[1] = aa; | |
1930 | addr[2] = spa; | |
1931 | ||
1932 | if (hmac_sha384_vector(kck, kck_len, 3, addr, len, hash) < 0) | |
1933 | return -1; | |
1934 | os_memcpy(pmkid, hash, PMKID_LEN); | |
1935 | return 0; | |
1936 | } | |
1937 | #endif /* CONFIG_SUITEB192 */ | |
1938 | ||
1939 | ||
43fb5297 JM |
1940 | /** |
1941 | * wpa_cipher_txt - Convert cipher suite to a text string | |
1942 | * @cipher: Cipher suite (WPA_CIPHER_* enum) | |
1943 | * Returns: Pointer to a text string of the cipher suite name | |
1944 | */ | |
1945 | const char * wpa_cipher_txt(int cipher) | |
1946 | { | |
1947 | switch (cipher) { | |
1948 | case WPA_CIPHER_NONE: | |
1949 | return "NONE"; | |
1950 | case WPA_CIPHER_WEP40: | |
1951 | return "WEP-40"; | |
1952 | case WPA_CIPHER_WEP104: | |
1953 | return "WEP-104"; | |
1954 | case WPA_CIPHER_TKIP: | |
1955 | return "TKIP"; | |
1956 | case WPA_CIPHER_CCMP: | |
1957 | return "CCMP"; | |
1958 | case WPA_CIPHER_CCMP | WPA_CIPHER_TKIP: | |
1959 | return "CCMP+TKIP"; | |
eb7719ff JM |
1960 | case WPA_CIPHER_GCMP: |
1961 | return "GCMP"; | |
30675c34 JM |
1962 | case WPA_CIPHER_GCMP_256: |
1963 | return "GCMP-256"; | |
1964 | case WPA_CIPHER_CCMP_256: | |
1965 | return "CCMP-256"; | |
3a5954ef JM |
1966 | case WPA_CIPHER_AES_128_CMAC: |
1967 | return "BIP"; | |
1968 | case WPA_CIPHER_BIP_GMAC_128: | |
1969 | return "BIP-GMAC-128"; | |
1970 | case WPA_CIPHER_BIP_GMAC_256: | |
1971 | return "BIP-GMAC-256"; | |
1972 | case WPA_CIPHER_BIP_CMAC_256: | |
1973 | return "BIP-CMAC-256"; | |
dff1e285 JM |
1974 | case WPA_CIPHER_GTK_NOT_USED: |
1975 | return "GTK_NOT_USED"; | |
43fb5297 JM |
1976 | default: |
1977 | return "UNKNOWN"; | |
1978 | } | |
1979 | } | |
1980 | ||
1981 | ||
1982 | /** | |
1983 | * wpa_key_mgmt_txt - Convert key management suite to a text string | |
1984 | * @key_mgmt: Key management suite (WPA_KEY_MGMT_* enum) | |
1985 | * @proto: WPA/WPA2 version (WPA_PROTO_*) | |
1986 | * Returns: Pointer to a text string of the key management suite name | |
1987 | */ | |
1988 | const char * wpa_key_mgmt_txt(int key_mgmt, int proto) | |
1989 | { | |
1990 | switch (key_mgmt) { | |
1991 | case WPA_KEY_MGMT_IEEE8021X: | |
1992 | if (proto == (WPA_PROTO_RSN | WPA_PROTO_WPA)) | |
1993 | return "WPA2+WPA/IEEE 802.1X/EAP"; | |
1994 | return proto == WPA_PROTO_RSN ? | |
1995 | "WPA2/IEEE 802.1X/EAP" : "WPA/IEEE 802.1X/EAP"; | |
1996 | case WPA_KEY_MGMT_PSK: | |
1997 | if (proto == (WPA_PROTO_RSN | WPA_PROTO_WPA)) | |
1998 | return "WPA2-PSK+WPA-PSK"; | |
1999 | return proto == WPA_PROTO_RSN ? | |
2000 | "WPA2-PSK" : "WPA-PSK"; | |
2001 | case WPA_KEY_MGMT_NONE: | |
2002 | return "NONE"; | |
4ac5f2f8 SB |
2003 | case WPA_KEY_MGMT_WPA_NONE: |
2004 | return "WPA-NONE"; | |
43fb5297 JM |
2005 | case WPA_KEY_MGMT_IEEE8021X_NO_WPA: |
2006 | return "IEEE 802.1X (no WPA)"; | |
2007 | #ifdef CONFIG_IEEE80211R | |
2008 | case WPA_KEY_MGMT_FT_IEEE8021X: | |
2009 | return "FT-EAP"; | |
c22bb5bb JM |
2010 | case WPA_KEY_MGMT_FT_IEEE8021X_SHA384: |
2011 | return "FT-EAP-SHA384"; | |
43fb5297 JM |
2012 | case WPA_KEY_MGMT_FT_PSK: |
2013 | return "FT-PSK"; | |
2014 | #endif /* CONFIG_IEEE80211R */ | |
2015 | #ifdef CONFIG_IEEE80211W | |
2016 | case WPA_KEY_MGMT_IEEE8021X_SHA256: | |
2017 | return "WPA2-EAP-SHA256"; | |
2018 | case WPA_KEY_MGMT_PSK_SHA256: | |
2019 | return "WPA2-PSK-SHA256"; | |
2020 | #endif /* CONFIG_IEEE80211W */ | |
72c5c289 JM |
2021 | case WPA_KEY_MGMT_WPS: |
2022 | return "WPS"; | |
2023 | case WPA_KEY_MGMT_SAE: | |
2024 | return "SAE"; | |
2025 | case WPA_KEY_MGMT_FT_SAE: | |
2026 | return "FT-SAE"; | |
2027 | case WPA_KEY_MGMT_OSEN: | |
2028 | return "OSEN"; | |
666497c8 JM |
2029 | case WPA_KEY_MGMT_IEEE8021X_SUITE_B: |
2030 | return "WPA2-EAP-SUITE-B"; | |
5e3b5197 JM |
2031 | case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192: |
2032 | return "WPA2-EAP-SUITE-B-192"; | |
94318a0d JM |
2033 | case WPA_KEY_MGMT_FILS_SHA256: |
2034 | return "FILS-SHA256"; | |
2035 | case WPA_KEY_MGMT_FILS_SHA384: | |
2036 | return "FILS-SHA384"; | |
2037 | case WPA_KEY_MGMT_FT_FILS_SHA256: | |
2038 | return "FT-FILS-SHA256"; | |
2039 | case WPA_KEY_MGMT_FT_FILS_SHA384: | |
2040 | return "FT-FILS-SHA384"; | |
88850232 JM |
2041 | case WPA_KEY_MGMT_OWE: |
2042 | return "OWE"; | |
2043 | case WPA_KEY_MGMT_DPP: | |
2044 | return "DPP"; | |
43fb5297 JM |
2045 | default: |
2046 | return "UNKNOWN"; | |
2047 | } | |
2048 | } | |
26e23750 JM |
2049 | |
2050 | ||
6c460eaf JM |
2051 | u32 wpa_akm_to_suite(int akm) |
2052 | { | |
c22bb5bb JM |
2053 | if (akm & WPA_KEY_MGMT_FT_IEEE8021X_SHA384) |
2054 | return RSN_AUTH_KEY_MGMT_FT_802_1X_SHA384; | |
6c460eaf | 2055 | if (akm & WPA_KEY_MGMT_FT_IEEE8021X) |
bf9f8a05 | 2056 | return RSN_AUTH_KEY_MGMT_FT_802_1X; |
6c460eaf | 2057 | if (akm & WPA_KEY_MGMT_FT_PSK) |
bf9f8a05 | 2058 | return RSN_AUTH_KEY_MGMT_FT_PSK; |
6c460eaf | 2059 | if (akm & WPA_KEY_MGMT_IEEE8021X_SHA256) |
bf9f8a05 | 2060 | return RSN_AUTH_KEY_MGMT_802_1X_SHA256; |
6c460eaf | 2061 | if (akm & WPA_KEY_MGMT_IEEE8021X) |
bf9f8a05 | 2062 | return RSN_AUTH_KEY_MGMT_UNSPEC_802_1X; |
6c460eaf | 2063 | if (akm & WPA_KEY_MGMT_PSK_SHA256) |
bf9f8a05 | 2064 | return RSN_AUTH_KEY_MGMT_PSK_SHA256; |
6c460eaf | 2065 | if (akm & WPA_KEY_MGMT_PSK) |
bf9f8a05 | 2066 | return RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X; |
6c460eaf | 2067 | if (akm & WPA_KEY_MGMT_CCKM) |
bf9f8a05 | 2068 | return RSN_AUTH_KEY_MGMT_CCKM; |
6c460eaf | 2069 | if (akm & WPA_KEY_MGMT_OSEN) |
bf9f8a05 | 2070 | return RSN_AUTH_KEY_MGMT_OSEN; |
666497c8 | 2071 | if (akm & WPA_KEY_MGMT_IEEE8021X_SUITE_B) |
bf9f8a05 | 2072 | return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B; |
5e3b5197 | 2073 | if (akm & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) |
bf9f8a05 | 2074 | return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192; |
94318a0d | 2075 | if (akm & WPA_KEY_MGMT_FILS_SHA256) |
bf9f8a05 | 2076 | return RSN_AUTH_KEY_MGMT_FILS_SHA256; |
94318a0d | 2077 | if (akm & WPA_KEY_MGMT_FILS_SHA384) |
bf9f8a05 | 2078 | return RSN_AUTH_KEY_MGMT_FILS_SHA384; |
94318a0d | 2079 | if (akm & WPA_KEY_MGMT_FT_FILS_SHA256) |
bf9f8a05 | 2080 | return RSN_AUTH_KEY_MGMT_FT_FILS_SHA256; |
94318a0d | 2081 | if (akm & WPA_KEY_MGMT_FT_FILS_SHA384) |
bf9f8a05 | 2082 | return RSN_AUTH_KEY_MGMT_FT_FILS_SHA384; |
43aafef8 JM |
2083 | if (akm & WPA_KEY_MGMT_SAE) |
2084 | return RSN_AUTH_KEY_MGMT_SAE; | |
2085 | if (akm & WPA_KEY_MGMT_FT_SAE) | |
2086 | return RSN_AUTH_KEY_MGMT_FT_SAE; | |
2087 | if (akm & WPA_KEY_MGMT_OWE) | |
2088 | return RSN_AUTH_KEY_MGMT_OWE; | |
2089 | if (akm & WPA_KEY_MGMT_DPP) | |
2090 | return RSN_AUTH_KEY_MGMT_DPP; | |
2091 | if (akm & WPA_KEY_MGMT_OSEN) | |
2092 | return RSN_AUTH_KEY_MGMT_OSEN; | |
6c460eaf JM |
2093 | return 0; |
2094 | } | |
2095 | ||
2096 | ||
26e23750 JM |
2097 | int wpa_compare_rsn_ie(int ft_initial_assoc, |
2098 | const u8 *ie1, size_t ie1len, | |
2099 | const u8 *ie2, size_t ie2len) | |
2100 | { | |
d3ccead3 JM |
2101 | if (ie1 == NULL || ie2 == NULL) |
2102 | return -1; | |
2103 | ||
26e23750 JM |
2104 | if (ie1len == ie2len && os_memcmp(ie1, ie2, ie1len) == 0) |
2105 | return 0; /* identical IEs */ | |
2106 | ||
2107 | #ifdef CONFIG_IEEE80211R | |
2108 | if (ft_initial_assoc) { | |
2109 | struct wpa_ie_data ie1d, ie2d; | |
2110 | /* | |
2111 | * The PMKID-List in RSN IE is different between Beacon/Probe | |
2112 | * Response/(Re)Association Request frames and EAPOL-Key | |
2113 | * messages in FT initial mobility domain association. Allow | |
2114 | * for this, but verify that other parts of the RSN IEs are | |
2115 | * identical. | |
2116 | */ | |
2117 | if (wpa_parse_wpa_ie_rsn(ie1, ie1len, &ie1d) < 0 || | |
2118 | wpa_parse_wpa_ie_rsn(ie2, ie2len, &ie2d) < 0) | |
2119 | return -1; | |
2120 | if (ie1d.proto == ie2d.proto && | |
2121 | ie1d.pairwise_cipher == ie2d.pairwise_cipher && | |
2122 | ie1d.group_cipher == ie2d.group_cipher && | |
2123 | ie1d.key_mgmt == ie2d.key_mgmt && | |
2124 | ie1d.capabilities == ie2d.capabilities && | |
2125 | ie1d.mgmt_group_cipher == ie2d.mgmt_group_cipher) | |
2126 | return 0; | |
2127 | } | |
2128 | #endif /* CONFIG_IEEE80211R */ | |
2129 | ||
2130 | return -1; | |
2131 | } | |
2132 | ||
2133 | ||
59e78c24 | 2134 | int wpa_insert_pmkid(u8 *ies, size_t *ies_len, const u8 *pmkid) |
26e23750 JM |
2135 | { |
2136 | u8 *start, *end, *rpos, *rend; | |
2137 | int added = 0; | |
2138 | ||
2139 | start = ies; | |
59e78c24 | 2140 | end = ies + *ies_len; |
26e23750 JM |
2141 | |
2142 | while (start < end) { | |
2143 | if (*start == WLAN_EID_RSN) | |
2144 | break; | |
2145 | start += 2 + start[1]; | |
2146 | } | |
2147 | if (start >= end) { | |
2ca23faf | 2148 | wpa_printf(MSG_ERROR, "RSN: Could not find RSNE in IEs data"); |
26e23750 JM |
2149 | return -1; |
2150 | } | |
2ca23faf | 2151 | wpa_hexdump(MSG_DEBUG, "RSN: RSNE before modification", |
26e23750 JM |
2152 | start, 2 + start[1]); |
2153 | ||
2154 | /* Find start of PMKID-Count */ | |
2155 | rpos = start + 2; | |
2156 | rend = rpos + start[1]; | |
2157 | ||
2158 | /* Skip Version and Group Data Cipher Suite */ | |
2159 | rpos += 2 + 4; | |
2160 | /* Skip Pairwise Cipher Suite Count and List */ | |
2161 | rpos += 2 + WPA_GET_LE16(rpos) * RSN_SELECTOR_LEN; | |
2162 | /* Skip AKM Suite Count and List */ | |
2163 | rpos += 2 + WPA_GET_LE16(rpos) * RSN_SELECTOR_LEN; | |
2164 | ||
2165 | if (rpos == rend) { | |
2166 | /* Add RSN Capabilities */ | |
2167 | os_memmove(rpos + 2, rpos, end - rpos); | |
2168 | *rpos++ = 0; | |
2169 | *rpos++ = 0; | |
3bc25adb JM |
2170 | added += 2; |
2171 | start[1] += 2; | |
2172 | rend = rpos; | |
26e23750 JM |
2173 | } else { |
2174 | /* Skip RSN Capabilities */ | |
2175 | rpos += 2; | |
2176 | if (rpos > rend) { | |
2ca23faf JM |
2177 | wpa_printf(MSG_ERROR, |
2178 | "RSN: Could not parse RSNE in IEs data"); | |
26e23750 JM |
2179 | return -1; |
2180 | } | |
2181 | } | |
2182 | ||
2183 | if (rpos == rend) { | |
2184 | /* No PMKID-Count field included; add it */ | |
3bc25adb | 2185 | os_memmove(rpos + 2 + PMKID_LEN, rpos, end + added - rpos); |
26e23750 JM |
2186 | WPA_PUT_LE16(rpos, 1); |
2187 | rpos += 2; | |
2188 | os_memcpy(rpos, pmkid, PMKID_LEN); | |
2189 | added += 2 + PMKID_LEN; | |
2190 | start[1] += 2 + PMKID_LEN; | |
2191 | } else { | |
59e78c24 JM |
2192 | u16 num_pmkid; |
2193 | ||
2194 | if (rend - rpos < 2) | |
26e23750 | 2195 | return -1; |
59e78c24 JM |
2196 | num_pmkid = WPA_GET_LE16(rpos); |
2197 | /* PMKID-Count was included; use it */ | |
2198 | if (num_pmkid != 0) { | |
2199 | u8 *after; | |
2200 | ||
2201 | if (num_pmkid * PMKID_LEN > rend - rpos - 2) | |
2202 | return -1; | |
2203 | /* | |
2204 | * PMKID may have been included in RSN IE in | |
2205 | * (Re)Association Request frame, so remove the old | |
2206 | * PMKID(s) first before adding the new one. | |
2207 | */ | |
2208 | wpa_printf(MSG_DEBUG, | |
2ca23faf | 2209 | "RSN: Remove %u old PMKID(s) from RSNE", |
59e78c24 JM |
2210 | num_pmkid); |
2211 | after = rpos + 2 + num_pmkid * PMKID_LEN; | |
2212 | os_memmove(rpos + 2, after, rend - after); | |
2213 | start[1] -= num_pmkid * PMKID_LEN; | |
2214 | added -= num_pmkid * PMKID_LEN; | |
26e23750 JM |
2215 | } |
2216 | WPA_PUT_LE16(rpos, 1); | |
2217 | rpos += 2; | |
3bc25adb | 2218 | os_memmove(rpos + PMKID_LEN, rpos, end + added - rpos); |
26e23750 JM |
2219 | os_memcpy(rpos, pmkid, PMKID_LEN); |
2220 | added += PMKID_LEN; | |
2221 | start[1] += PMKID_LEN; | |
2222 | } | |
2223 | ||
2ca23faf JM |
2224 | wpa_hexdump(MSG_DEBUG, "RSN: RSNE after modification (PMKID inserted)", |
2225 | start, 2 + start[1]); | |
26e23750 | 2226 | |
59e78c24 JM |
2227 | *ies_len += added; |
2228 | ||
2229 | return 0; | |
26e23750 | 2230 | } |
c3550295 JM |
2231 | |
2232 | ||
2233 | int wpa_cipher_key_len(int cipher) | |
2234 | { | |
2235 | switch (cipher) { | |
30675c34 JM |
2236 | case WPA_CIPHER_CCMP_256: |
2237 | case WPA_CIPHER_GCMP_256: | |
8dd9f9cd JM |
2238 | case WPA_CIPHER_BIP_GMAC_256: |
2239 | case WPA_CIPHER_BIP_CMAC_256: | |
30675c34 | 2240 | return 32; |
c3550295 JM |
2241 | case WPA_CIPHER_CCMP: |
2242 | case WPA_CIPHER_GCMP: | |
8dd9f9cd JM |
2243 | case WPA_CIPHER_AES_128_CMAC: |
2244 | case WPA_CIPHER_BIP_GMAC_128: | |
c3550295 JM |
2245 | return 16; |
2246 | case WPA_CIPHER_TKIP: | |
2247 | return 32; | |
c3550295 JM |
2248 | } |
2249 | ||
2250 | return 0; | |
2251 | } | |
2252 | ||
2253 | ||
2254 | int wpa_cipher_rsc_len(int cipher) | |
2255 | { | |
2256 | switch (cipher) { | |
30675c34 JM |
2257 | case WPA_CIPHER_CCMP_256: |
2258 | case WPA_CIPHER_GCMP_256: | |
c3550295 JM |
2259 | case WPA_CIPHER_CCMP: |
2260 | case WPA_CIPHER_GCMP: | |
2261 | case WPA_CIPHER_TKIP: | |
2262 | return 6; | |
c3550295 JM |
2263 | } |
2264 | ||
2265 | return 0; | |
2266 | } | |
2267 | ||
2268 | ||
04f02faa | 2269 | enum wpa_alg wpa_cipher_to_alg(int cipher) |
c3550295 JM |
2270 | { |
2271 | switch (cipher) { | |
30675c34 JM |
2272 | case WPA_CIPHER_CCMP_256: |
2273 | return WPA_ALG_CCMP_256; | |
2274 | case WPA_CIPHER_GCMP_256: | |
2275 | return WPA_ALG_GCMP_256; | |
c3550295 JM |
2276 | case WPA_CIPHER_CCMP: |
2277 | return WPA_ALG_CCMP; | |
2278 | case WPA_CIPHER_GCMP: | |
2279 | return WPA_ALG_GCMP; | |
2280 | case WPA_CIPHER_TKIP: | |
2281 | return WPA_ALG_TKIP; | |
8dd9f9cd JM |
2282 | case WPA_CIPHER_AES_128_CMAC: |
2283 | return WPA_ALG_IGTK; | |
2284 | case WPA_CIPHER_BIP_GMAC_128: | |
2285 | return WPA_ALG_BIP_GMAC_128; | |
2286 | case WPA_CIPHER_BIP_GMAC_256: | |
2287 | return WPA_ALG_BIP_GMAC_256; | |
2288 | case WPA_CIPHER_BIP_CMAC_256: | |
2289 | return WPA_ALG_BIP_CMAC_256; | |
c3550295 JM |
2290 | } |
2291 | return WPA_ALG_NONE; | |
2292 | } | |
2293 | ||
2294 | ||
2295 | int wpa_cipher_valid_pairwise(int cipher) | |
2296 | { | |
30675c34 JM |
2297 | return cipher == WPA_CIPHER_CCMP_256 || |
2298 | cipher == WPA_CIPHER_GCMP_256 || | |
2299 | cipher == WPA_CIPHER_CCMP || | |
c3550295 JM |
2300 | cipher == WPA_CIPHER_GCMP || |
2301 | cipher == WPA_CIPHER_TKIP; | |
2302 | } | |
2303 | ||
2304 | ||
2305 | u32 wpa_cipher_to_suite(int proto, int cipher) | |
2306 | { | |
30675c34 JM |
2307 | if (cipher & WPA_CIPHER_CCMP_256) |
2308 | return RSN_CIPHER_SUITE_CCMP_256; | |
2309 | if (cipher & WPA_CIPHER_GCMP_256) | |
2310 | return RSN_CIPHER_SUITE_GCMP_256; | |
c3550295 JM |
2311 | if (cipher & WPA_CIPHER_CCMP) |
2312 | return (proto == WPA_PROTO_RSN ? | |
2313 | RSN_CIPHER_SUITE_CCMP : WPA_CIPHER_SUITE_CCMP); | |
2314 | if (cipher & WPA_CIPHER_GCMP) | |
2315 | return RSN_CIPHER_SUITE_GCMP; | |
2316 | if (cipher & WPA_CIPHER_TKIP) | |
2317 | return (proto == WPA_PROTO_RSN ? | |
2318 | RSN_CIPHER_SUITE_TKIP : WPA_CIPHER_SUITE_TKIP); | |
c3550295 JM |
2319 | if (cipher & WPA_CIPHER_NONE) |
2320 | return (proto == WPA_PROTO_RSN ? | |
2321 | RSN_CIPHER_SUITE_NONE : WPA_CIPHER_SUITE_NONE); | |
dff1e285 JM |
2322 | if (cipher & WPA_CIPHER_GTK_NOT_USED) |
2323 | return RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED; | |
8dd9f9cd JM |
2324 | if (cipher & WPA_CIPHER_AES_128_CMAC) |
2325 | return RSN_CIPHER_SUITE_AES_128_CMAC; | |
2326 | if (cipher & WPA_CIPHER_BIP_GMAC_128) | |
2327 | return RSN_CIPHER_SUITE_BIP_GMAC_128; | |
2328 | if (cipher & WPA_CIPHER_BIP_GMAC_256) | |
2329 | return RSN_CIPHER_SUITE_BIP_GMAC_256; | |
2330 | if (cipher & WPA_CIPHER_BIP_CMAC_256) | |
2331 | return RSN_CIPHER_SUITE_BIP_CMAC_256; | |
c3550295 JM |
2332 | return 0; |
2333 | } | |
2334 | ||
2335 | ||
8a4ce280 | 2336 | int rsn_cipher_put_suites(u8 *start, int ciphers) |
c3550295 | 2337 | { |
8a4ce280 | 2338 | u8 *pos = start; |
c3550295 | 2339 | |
30675c34 JM |
2340 | if (ciphers & WPA_CIPHER_CCMP_256) { |
2341 | RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP_256); | |
2342 | pos += RSN_SELECTOR_LEN; | |
30675c34 JM |
2343 | } |
2344 | if (ciphers & WPA_CIPHER_GCMP_256) { | |
2345 | RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_GCMP_256); | |
2346 | pos += RSN_SELECTOR_LEN; | |
30675c34 | 2347 | } |
c3550295 JM |
2348 | if (ciphers & WPA_CIPHER_CCMP) { |
2349 | RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP); | |
2350 | pos += RSN_SELECTOR_LEN; | |
c3550295 JM |
2351 | } |
2352 | if (ciphers & WPA_CIPHER_GCMP) { | |
2353 | RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_GCMP); | |
2354 | pos += RSN_SELECTOR_LEN; | |
c3550295 JM |
2355 | } |
2356 | if (ciphers & WPA_CIPHER_TKIP) { | |
2357 | RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_TKIP); | |
2358 | pos += RSN_SELECTOR_LEN; | |
c3550295 JM |
2359 | } |
2360 | if (ciphers & WPA_CIPHER_NONE) { | |
2361 | RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_NONE); | |
2362 | pos += RSN_SELECTOR_LEN; | |
c3550295 JM |
2363 | } |
2364 | ||
8a4ce280 | 2365 | return (pos - start) / RSN_SELECTOR_LEN; |
c3550295 JM |
2366 | } |
2367 | ||
2368 | ||
8a4ce280 | 2369 | int wpa_cipher_put_suites(u8 *start, int ciphers) |
c3550295 | 2370 | { |
8a4ce280 | 2371 | u8 *pos = start; |
c3550295 JM |
2372 | |
2373 | if (ciphers & WPA_CIPHER_CCMP) { | |
2374 | RSN_SELECTOR_PUT(pos, WPA_CIPHER_SUITE_CCMP); | |
2375 | pos += WPA_SELECTOR_LEN; | |
c3550295 JM |
2376 | } |
2377 | if (ciphers & WPA_CIPHER_TKIP) { | |
2378 | RSN_SELECTOR_PUT(pos, WPA_CIPHER_SUITE_TKIP); | |
2379 | pos += WPA_SELECTOR_LEN; | |
c3550295 JM |
2380 | } |
2381 | if (ciphers & WPA_CIPHER_NONE) { | |
2382 | RSN_SELECTOR_PUT(pos, WPA_CIPHER_SUITE_NONE); | |
2383 | pos += WPA_SELECTOR_LEN; | |
c3550295 JM |
2384 | } |
2385 | ||
8a4ce280 | 2386 | return (pos - start) / RSN_SELECTOR_LEN; |
c3550295 | 2387 | } |
edbd2a19 JM |
2388 | |
2389 | ||
2390 | int wpa_pick_pairwise_cipher(int ciphers, int none_allowed) | |
2391 | { | |
30675c34 JM |
2392 | if (ciphers & WPA_CIPHER_CCMP_256) |
2393 | return WPA_CIPHER_CCMP_256; | |
2394 | if (ciphers & WPA_CIPHER_GCMP_256) | |
2395 | return WPA_CIPHER_GCMP_256; | |
edbd2a19 JM |
2396 | if (ciphers & WPA_CIPHER_CCMP) |
2397 | return WPA_CIPHER_CCMP; | |
2398 | if (ciphers & WPA_CIPHER_GCMP) | |
2399 | return WPA_CIPHER_GCMP; | |
2400 | if (ciphers & WPA_CIPHER_TKIP) | |
2401 | return WPA_CIPHER_TKIP; | |
2402 | if (none_allowed && (ciphers & WPA_CIPHER_NONE)) | |
2403 | return WPA_CIPHER_NONE; | |
2404 | return -1; | |
2405 | } | |
2406 | ||
2407 | ||
2408 | int wpa_pick_group_cipher(int ciphers) | |
2409 | { | |
30675c34 JM |
2410 | if (ciphers & WPA_CIPHER_CCMP_256) |
2411 | return WPA_CIPHER_CCMP_256; | |
2412 | if (ciphers & WPA_CIPHER_GCMP_256) | |
2413 | return WPA_CIPHER_GCMP_256; | |
edbd2a19 JM |
2414 | if (ciphers & WPA_CIPHER_CCMP) |
2415 | return WPA_CIPHER_CCMP; | |
2416 | if (ciphers & WPA_CIPHER_GCMP) | |
2417 | return WPA_CIPHER_GCMP; | |
dff1e285 JM |
2418 | if (ciphers & WPA_CIPHER_GTK_NOT_USED) |
2419 | return WPA_CIPHER_GTK_NOT_USED; | |
edbd2a19 JM |
2420 | if (ciphers & WPA_CIPHER_TKIP) |
2421 | return WPA_CIPHER_TKIP; | |
edbd2a19 JM |
2422 | return -1; |
2423 | } | |
a39c78be JM |
2424 | |
2425 | ||
2426 | int wpa_parse_cipher(const char *value) | |
2427 | { | |
2428 | int val = 0, last; | |
2429 | char *start, *end, *buf; | |
2430 | ||
2431 | buf = os_strdup(value); | |
2432 | if (buf == NULL) | |
2433 | return -1; | |
2434 | start = buf; | |
2435 | ||
2436 | while (*start != '\0') { | |
2437 | while (*start == ' ' || *start == '\t') | |
2438 | start++; | |
2439 | if (*start == '\0') | |
2440 | break; | |
2441 | end = start; | |
2442 | while (*end != ' ' && *end != '\t' && *end != '\0') | |
2443 | end++; | |
2444 | last = *end == '\0'; | |
2445 | *end = '\0'; | |
30675c34 JM |
2446 | if (os_strcmp(start, "CCMP-256") == 0) |
2447 | val |= WPA_CIPHER_CCMP_256; | |
2448 | else if (os_strcmp(start, "GCMP-256") == 0) | |
2449 | val |= WPA_CIPHER_GCMP_256; | |
2450 | else if (os_strcmp(start, "CCMP") == 0) | |
a39c78be JM |
2451 | val |= WPA_CIPHER_CCMP; |
2452 | else if (os_strcmp(start, "GCMP") == 0) | |
2453 | val |= WPA_CIPHER_GCMP; | |
2454 | else if (os_strcmp(start, "TKIP") == 0) | |
2455 | val |= WPA_CIPHER_TKIP; | |
2456 | else if (os_strcmp(start, "WEP104") == 0) | |
2457 | val |= WPA_CIPHER_WEP104; | |
2458 | else if (os_strcmp(start, "WEP40") == 0) | |
2459 | val |= WPA_CIPHER_WEP40; | |
2460 | else if (os_strcmp(start, "NONE") == 0) | |
2461 | val |= WPA_CIPHER_NONE; | |
dff1e285 JM |
2462 | else if (os_strcmp(start, "GTK_NOT_USED") == 0) |
2463 | val |= WPA_CIPHER_GTK_NOT_USED; | |
61a56c14 JM |
2464 | else if (os_strcmp(start, "AES-128-CMAC") == 0) |
2465 | val |= WPA_CIPHER_AES_128_CMAC; | |
2466 | else if (os_strcmp(start, "BIP-GMAC-128") == 0) | |
2467 | val |= WPA_CIPHER_BIP_GMAC_128; | |
2468 | else if (os_strcmp(start, "BIP-GMAC-256") == 0) | |
2469 | val |= WPA_CIPHER_BIP_GMAC_256; | |
2470 | else if (os_strcmp(start, "BIP-CMAC-256") == 0) | |
2471 | val |= WPA_CIPHER_BIP_CMAC_256; | |
a39c78be JM |
2472 | else { |
2473 | os_free(buf); | |
2474 | return -1; | |
2475 | } | |
2476 | ||
2477 | if (last) | |
2478 | break; | |
2479 | start = end + 1; | |
2480 | } | |
2481 | os_free(buf); | |
2482 | ||
2483 | return val; | |
2484 | } | |
0282a8c4 JM |
2485 | |
2486 | ||
2487 | int wpa_write_ciphers(char *start, char *end, int ciphers, const char *delim) | |
2488 | { | |
2489 | char *pos = start; | |
2490 | int ret; | |
2491 | ||
30675c34 JM |
2492 | if (ciphers & WPA_CIPHER_CCMP_256) { |
2493 | ret = os_snprintf(pos, end - pos, "%sCCMP-256", | |
2494 | pos == start ? "" : delim); | |
d85e1fc8 | 2495 | if (os_snprintf_error(end - pos, ret)) |
30675c34 JM |
2496 | return -1; |
2497 | pos += ret; | |
2498 | } | |
2499 | if (ciphers & WPA_CIPHER_GCMP_256) { | |
2500 | ret = os_snprintf(pos, end - pos, "%sGCMP-256", | |
2501 | pos == start ? "" : delim); | |
d85e1fc8 | 2502 | if (os_snprintf_error(end - pos, ret)) |
30675c34 JM |
2503 | return -1; |
2504 | pos += ret; | |
2505 | } | |
0282a8c4 JM |
2506 | if (ciphers & WPA_CIPHER_CCMP) { |
2507 | ret = os_snprintf(pos, end - pos, "%sCCMP", | |
2508 | pos == start ? "" : delim); | |
d85e1fc8 | 2509 | if (os_snprintf_error(end - pos, ret)) |
0282a8c4 JM |
2510 | return -1; |
2511 | pos += ret; | |
2512 | } | |
2513 | if (ciphers & WPA_CIPHER_GCMP) { | |
2514 | ret = os_snprintf(pos, end - pos, "%sGCMP", | |
2515 | pos == start ? "" : delim); | |
d85e1fc8 | 2516 | if (os_snprintf_error(end - pos, ret)) |
0282a8c4 JM |
2517 | return -1; |
2518 | pos += ret; | |
2519 | } | |
2520 | if (ciphers & WPA_CIPHER_TKIP) { | |
2521 | ret = os_snprintf(pos, end - pos, "%sTKIP", | |
2522 | pos == start ? "" : delim); | |
d85e1fc8 | 2523 | if (os_snprintf_error(end - pos, ret)) |
0282a8c4 JM |
2524 | return -1; |
2525 | pos += ret; | |
2526 | } | |
61a56c14 JM |
2527 | if (ciphers & WPA_CIPHER_AES_128_CMAC) { |
2528 | ret = os_snprintf(pos, end - pos, "%sAES-128-CMAC", | |
2529 | pos == start ? "" : delim); | |
2530 | if (os_snprintf_error(end - pos, ret)) | |
2531 | return -1; | |
2532 | pos += ret; | |
2533 | } | |
2534 | if (ciphers & WPA_CIPHER_BIP_GMAC_128) { | |
2535 | ret = os_snprintf(pos, end - pos, "%sBIP-GMAC-128", | |
2536 | pos == start ? "" : delim); | |
2537 | if (os_snprintf_error(end - pos, ret)) | |
2538 | return -1; | |
2539 | pos += ret; | |
2540 | } | |
2541 | if (ciphers & WPA_CIPHER_BIP_GMAC_256) { | |
2542 | ret = os_snprintf(pos, end - pos, "%sBIP-GMAC-256", | |
2543 | pos == start ? "" : delim); | |
2544 | if (os_snprintf_error(end - pos, ret)) | |
2545 | return -1; | |
2546 | pos += ret; | |
2547 | } | |
2548 | if (ciphers & WPA_CIPHER_BIP_CMAC_256) { | |
2549 | ret = os_snprintf(pos, end - pos, "%sBIP-CMAC-256", | |
2550 | pos == start ? "" : delim); | |
2551 | if (os_snprintf_error(end - pos, ret)) | |
2552 | return -1; | |
2553 | pos += ret; | |
2554 | } | |
0282a8c4 JM |
2555 | if (ciphers & WPA_CIPHER_NONE) { |
2556 | ret = os_snprintf(pos, end - pos, "%sNONE", | |
2557 | pos == start ? "" : delim); | |
d85e1fc8 | 2558 | if (os_snprintf_error(end - pos, ret)) |
0282a8c4 JM |
2559 | return -1; |
2560 | pos += ret; | |
2561 | } | |
2562 | ||
2563 | return pos - start; | |
2564 | } | |
cf830c1c JM |
2565 | |
2566 | ||
2567 | int wpa_select_ap_group_cipher(int wpa, int wpa_pairwise, int rsn_pairwise) | |
2568 | { | |
2569 | int pairwise = 0; | |
2570 | ||
2571 | /* Select group cipher based on the enabled pairwise cipher suites */ | |
2572 | if (wpa & 1) | |
2573 | pairwise |= wpa_pairwise; | |
2574 | if (wpa & 2) | |
2575 | pairwise |= rsn_pairwise; | |
2576 | ||
2577 | if (pairwise & WPA_CIPHER_TKIP) | |
2578 | return WPA_CIPHER_TKIP; | |
2579 | if ((pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP)) == WPA_CIPHER_GCMP) | |
2580 | return WPA_CIPHER_GCMP; | |
30675c34 JM |
2581 | if ((pairwise & (WPA_CIPHER_GCMP_256 | WPA_CIPHER_CCMP | |
2582 | WPA_CIPHER_GCMP)) == WPA_CIPHER_GCMP_256) | |
2583 | return WPA_CIPHER_GCMP_256; | |
2584 | if ((pairwise & (WPA_CIPHER_CCMP_256 | WPA_CIPHER_CCMP | | |
2585 | WPA_CIPHER_GCMP)) == WPA_CIPHER_CCMP_256) | |
2586 | return WPA_CIPHER_CCMP_256; | |
cf830c1c JM |
2587 | return WPA_CIPHER_CCMP; |
2588 | } | |
94f66e8a JM |
2589 | |
2590 | ||
2591 | #ifdef CONFIG_FILS | |
42b847ac | 2592 | int fils_domain_name_hash(const char *domain, u8 *hash) |
94f66e8a JM |
2593 | { |
2594 | char buf[255], *wpos = buf; | |
2595 | const char *pos = domain; | |
2596 | size_t len; | |
42b847ac JM |
2597 | const u8 *addr[1]; |
2598 | u8 mac[SHA256_MAC_LEN]; | |
94f66e8a JM |
2599 | |
2600 | for (len = 0; len < sizeof(buf) && *pos; len++) { | |
2601 | if (isalpha(*pos) && isupper(*pos)) | |
2602 | *wpos++ = tolower(*pos); | |
2603 | else | |
2604 | *wpos++ = *pos; | |
2605 | pos++; | |
2606 | } | |
2607 | ||
42b847ac JM |
2608 | addr[0] = (const u8 *) buf; |
2609 | if (sha256_vector(1, addr, &len, mac) < 0) | |
2610 | return -1; | |
2611 | os_memcpy(hash, mac, 2); | |
2612 | return 0; | |
94f66e8a JM |
2613 | } |
2614 | #endif /* CONFIG_FILS */ |