1 /*
2 * WPA/RSN - Shared functions for supplicant and authenticator
3 * Copyright (c) 2002-2018, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8 #ifdef ESP_SUPPLICANT
9
10 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "common/defs.h"
13 #include "common/ieee802_11_defs.h"
14 #include "common/wpa_common.h"
15 #include "rsn_supp/wpa.h"
16 #include "crypto/sha1.h"
17 #include "crypto/sha256.h"
18 #include "crypto/sha384.h"
19 #include "crypto/md5.h"
20 #include "crypto/aes.h"
21 #include "crypto/aes_wrap.h"
22 #include "crypto/crypto.h"
23
24 #define MD5_MAC_LEN 16
25 #ifdef CONFIG_IEEE80211R
wpa_ft_mic(const u8 * kck,size_t kck_len,const u8 * sta_addr,const u8 * ap_addr,u8 transaction_seqnum,const u8 * mdie,size_t mdie_len,const u8 * ftie,size_t ftie_len,const u8 * rsnie,size_t rsnie_len,const u8 * ric,size_t ric_len,u8 * mic)26 int wpa_ft_mic(const u8 *kck, size_t kck_len, const u8 *sta_addr,
27 const u8 *ap_addr, u8 transaction_seqnum,
28 const u8 *mdie, size_t mdie_len,
29 const u8 *ftie, size_t ftie_len,
30 const u8 *rsnie, size_t rsnie_len,
31 const u8 *ric, size_t ric_len, u8 *mic)
32 {
33 u8 *buf, *pos;
34 size_t buf_len;
35
36 if (kck_len != 16) {
37 wpa_printf(MSG_WARNING, "FT: Unsupported KCK length %u",
38 (unsigned int) kck_len);
39 return -1;
40 }
41
42 buf_len = 2 * ETH_ALEN + 1 + mdie_len + ftie_len + rsnie_len + ric_len;
43 buf = os_malloc(buf_len);
44 if (buf == NULL)
45 return -1;
46
47 pos = buf;
48 os_memcpy(pos, sta_addr, ETH_ALEN);
49 pos += ETH_ALEN;
50 os_memcpy(pos, ap_addr, ETH_ALEN);
51 pos += ETH_ALEN;
52 *pos++ = transaction_seqnum;
53 if (rsnie) {
54 os_memcpy(pos, rsnie, rsnie_len);
55 pos += rsnie_len;
56 }
57 if (mdie) {
58 os_memcpy(pos, mdie, mdie_len);
59 pos += mdie_len;
60 }
61 if (ftie) {
62 struct rsn_ftie *_ftie;
63 os_memcpy(pos, ftie, ftie_len);
64 if (ftie_len < 2 + sizeof(*_ftie)) {
65 os_free(buf);
66 return -1;
67 }
68 _ftie = (struct rsn_ftie *) (pos + 2);
69 os_memset(_ftie->mic, 0, sizeof(_ftie->mic));
70 pos += ftie_len;
71 }
72 if (ric) {
73 os_memcpy(pos, ric, ric_len);
74 pos += ric_len;
75 }
76
77 wpa_hexdump(MSG_MSGDUMP, "FT: MIC data", buf, pos - buf);
78 if (omac1_aes_128(kck, buf, pos - buf, mic)) {
79 os_free(buf);
80 return -1;
81 }
82
83 os_free(buf);
84
85 return 0;
86 }
87
88
wpa_ft_parse_ftie(const u8 * ie,size_t ie_len,struct wpa_ft_ies * parse)89 static int wpa_ft_parse_ftie(const u8 *ie, size_t ie_len,
90 struct wpa_ft_ies *parse)
91 {
92 const u8 *end, *pos;
93
94 parse->ftie = ie;
95 parse->ftie_len = ie_len;
96
97 pos = ie + sizeof(struct rsn_ftie);
98 end = ie + ie_len;
99
100 while (pos + 2 <= end && pos + 2 + pos[1] <= end) {
101 switch (pos[0]) {
102 case FTIE_SUBELEM_R1KH_ID:
103 if (pos[1] != FT_R1KH_ID_LEN) {
104 wpa_printf(MSG_DEBUG, "FT: Invalid R1KH-ID "
105 "length in FTIE: %d", pos[1]);
106 return -1;
107 }
108 parse->r1kh_id = pos + 2;
109 break;
110 case FTIE_SUBELEM_GTK:
111 parse->gtk = pos + 2;
112 parse->gtk_len = pos[1];
113 break;
114 case FTIE_SUBELEM_R0KH_ID:
115 if (pos[1] < 1 || pos[1] > FT_R0KH_ID_MAX_LEN) {
116 wpa_printf(MSG_DEBUG, "FT: Invalid R0KH-ID "
117 "length in FTIE: %d", pos[1]);
118 return -1;
119 }
120 parse->r0kh_id = pos + 2;
121 parse->r0kh_id_len = pos[1];
122 break;
123 #ifdef CONFIG_IEEE80211W
124 case FTIE_SUBELEM_IGTK:
125 parse->igtk = pos + 2;
126 parse->igtk_len = pos[1];
127 break;
128 #endif /* CONFIG_IEEE80211W */
129 }
130
131 pos += 2 + pos[1];
132 }
133
134 return 0;
135 }
136
137
wpa_ft_parse_ies(const u8 * ies,size_t ies_len,struct wpa_ft_ies * parse)138 int wpa_ft_parse_ies(const u8 *ies, size_t ies_len,
139 struct wpa_ft_ies *parse)
140 {
141 const u8 *end, *pos;
142 struct wpa_ie_data data;
143 int ret;
144 const struct rsn_ftie *ftie;
145 int prot_ie_count = 0;
146
147 os_memset(parse, 0, sizeof(*parse));
148 if (ies == NULL)
149 return 0;
150
151 pos = ies;
152 end = ies + ies_len;
153 while (pos + 2 <= end && pos + 2 + pos[1] <= end) {
154 switch (pos[0]) {
155 case WLAN_EID_RSN:
156 parse->rsn = pos + 2;
157 parse->rsn_len = pos[1];
158 ret = wpa_parse_wpa_ie_rsn(parse->rsn - 2,
159 parse->rsn_len + 2,
160 &data);
161 if (ret < 0) {
162 wpa_printf(MSG_DEBUG, "FT: Failed to parse "
163 "RSN IE: %d", ret);
164 return -1;
165 }
166 if (data.num_pmkid == 1 && data.pmkid)
167 parse->rsn_pmkid = data.pmkid;
168 break;
169 case WLAN_EID_MOBILITY_DOMAIN:
170 parse->mdie = pos + 2;
171 parse->mdie_len = pos[1];
172 break;
173 case WLAN_EID_FAST_BSS_TRANSITION:
174 if (pos[1] < sizeof(*ftie))
175 return -1;
176 ftie = (const struct rsn_ftie *) (pos + 2);
177 prot_ie_count = ftie->mic_control[1];
178 if (wpa_ft_parse_ftie(pos + 2, pos[1], parse) < 0)
179 return -1;
180 break;
181 case WLAN_EID_TIMEOUT_INTERVAL:
182 parse->tie = pos + 2;
183 parse->tie_len = pos[1];
184 break;
185 case WLAN_EID_RIC_DATA:
186 if (parse->ric == NULL)
187 parse->ric = pos;
188 break;
189 }
190
191 pos += 2 + pos[1];
192 }
193
194 if (prot_ie_count == 0)
195 return 0; /* no MIC */
196
197 /*
198 * Check that the protected IE count matches with IEs included in the
199 * frame.
200 */
201 if (parse->rsn)
202 prot_ie_count--;
203 if (parse->mdie)
204 prot_ie_count--;
205 if (parse->ftie)
206 prot_ie_count--;
207 if (prot_ie_count < 0) {
208 wpa_printf(MSG_DEBUG, "FT: Some required IEs not included in "
209 "the protected IE count");
210 return -1;
211 }
212
213 if (prot_ie_count == 0 && parse->ric) {
214 wpa_printf(MSG_DEBUG, "FT: RIC IE(s) in the frame, but not "
215 "included in protected IE count");
216 return -1;
217 }
218
219 /* Determine the end of the RIC IE(s) */
220 pos = parse->ric;
221 while (pos && pos + 2 <= end && pos + 2 + pos[1] <= end &&
222 prot_ie_count) {
223 prot_ie_count--;
224 pos += 2 + pos[1];
225 }
226 parse->ric_len = pos - parse->ric;
227 if (prot_ie_count) {
228 wpa_printf(MSG_DEBUG, "FT: %d protected IEs missing from "
229 "frame", (int) prot_ie_count);
230 return -1;
231 }
232
233 return 0;
234 }
235 #endif /* CONFIG_IEEE80211R */
236
237
wpa_kck_len(int akmp,size_t pmk_len)238 static unsigned int wpa_kck_len(int akmp, size_t pmk_len)
239 {
240 switch (akmp) {
241 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
242 return 24;
243 case WPA_KEY_MGMT_OWE:
244 return pmk_len / 2;
245 default:
246 return 16;
247 }
248 }
249
wpa_kek_len(int akmp,size_t pmk_len)250 static unsigned int wpa_kek_len(int akmp, size_t pmk_len)
251 {
252 switch (akmp) {
253 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
254 return 32;
255 case WPA_KEY_MGMT_OWE:
256 return pmk_len <= 32 ? 16 : 32;
257 default:
258 return 16;
259 }
260 }
261
262
wpa_mic_len(int akmp,size_t pmk_len)263 unsigned int wpa_mic_len(int akmp, size_t pmk_len)
264 {
265 switch (akmp) {
266 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
267 return 24;
268 default:
269 return 16;
270 }
271 }
272
rsn_selector_to_bitfield(const u8 * s)273 static int rsn_selector_to_bitfield(const u8 *s)
274 {
275 if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_NONE)
276 return WPA_CIPHER_NONE;
277 if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_WEP40)
278 return WPA_CIPHER_WEP40;
279 if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_TKIP)
280 return WPA_CIPHER_TKIP;
281 if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_CCMP)
282 return WPA_CIPHER_CCMP;
283 if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_WEP104)
284 return WPA_CIPHER_WEP104;
285 #ifdef CONFIG_GCMP
286 if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_GCMP)
287 return WPA_CIPHER_GCMP;
288 if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_GCMP_256)
289 return WPA_CIPHER_GCMP_256;
290 #endif
291 #ifdef CONFIG_IEEE80211W
292 if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_AES_128_CMAC)
293 return WPA_CIPHER_AES_128_CMAC;
294 #ifdef CONFIG_GMAC
295 if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_BIP_GMAC_128)
296 return WPA_CIPHER_BIP_GMAC_128;
297 if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_BIP_GMAC_256)
298 return WPA_CIPHER_BIP_GMAC_256;
299 #endif
300 #endif /* CONFIG_IEEE80211W */
301
302 return 0;
303 }
304
rsn_key_mgmt_to_bitfield(const u8 * s)305 static int rsn_key_mgmt_to_bitfield(const u8 *s)
306 {
307 if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_UNSPEC_802_1X)
308 return WPA_KEY_MGMT_IEEE8021X;
309 if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X)
310 return WPA_KEY_MGMT_PSK;
311 #ifdef CONFIG_IEEE80211R
312 if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_802_1X)
313 return WPA_KEY_MGMT_FT_IEEE8021X;
314 if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_PSK)
315 return WPA_KEY_MGMT_FT_PSK;
316 #endif /* CONFIG_IEEE80211R */
317 #ifdef CONFIG_IEEE80211W
318 #ifdef CONFIG_WPA3_SAE
319 if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_SAE)
320 return WPA_KEY_MGMT_SAE;
321 #endif /* CONFIG_WPA3_SAE */
322 if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_802_1X_SHA256)
323 return WPA_KEY_MGMT_IEEE8021X_SHA256;
324 if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_PSK_SHA256)
325 return WPA_KEY_MGMT_PSK_SHA256;
326 #endif /* CONFIG_IEEE80211W */
327 #ifdef CONFIG_SUITEB
328 if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_802_1X_SUITE_B)
329 return WPA_KEY_MGMT_IEEE8021X_SUITE_B;
330 #endif
331 #ifdef CONFIG_SUITEB192
332 if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192)
333 return WPA_KEY_MGMT_IEEE8021X_SUITE_B_192;
334 #endif
335 #ifdef CONFIG_WPA3_SAE
336 if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_SAE)
337 return WPA_KEY_MGMT_SAE;
338 #endif /* CONFIG_WPA3_SAE */
339 #ifdef CONFIG_OWE_STA
340 if(RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_OWE)
341 return WPA_KEY_MGMT_OWE;
342 #endif /* CONFIG_OWE_STA */
343
344 return 0;
345 }
346
wpa_selector_to_bitfield(const u8 * s)347 static int wpa_selector_to_bitfield(const u8 *s)
348 {
349 if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_NONE)
350 return WPA_CIPHER_NONE;
351 if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_WEP40)
352 return WPA_CIPHER_WEP40;
353 if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_TKIP)
354 return WPA_CIPHER_TKIP;
355 if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_CCMP)
356 return WPA_CIPHER_CCMP;
357 if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_WEP104)
358 return WPA_CIPHER_WEP104;
359 return 0;
360 }
361
wpa_key_mgmt_to_bitfield(const u8 * s)362 static int wpa_key_mgmt_to_bitfield(const u8 *s)
363 {
364 if (RSN_SELECTOR_GET(s) == WPA_AUTH_KEY_MGMT_UNSPEC_802_1X)
365 return WPA_KEY_MGMT_IEEE8021X;
366 if (RSN_SELECTOR_GET(s) == WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X)
367 return WPA_KEY_MGMT_PSK;
368 if (RSN_SELECTOR_GET(s) == WPA_AUTH_KEY_MGMT_NONE)
369 return WPA_KEY_MGMT_WPA_NONE;
370 return 0;
371 }
372
wpa_cipher_valid_mgmt_group(int cipher)373 int wpa_cipher_valid_mgmt_group(int cipher)
374 {
375 return cipher == WPA_CIPHER_AES_128_CMAC ||
376 cipher == WPA_CIPHER_BIP_GMAC_128 ||
377 cipher == WPA_CIPHER_BIP_GMAC_256;
378 }
379
wpa_parse_wpa_ie_rsnxe(const u8 * rsnxe_ie,size_t rsnxe_ie_len,struct wpa_ie_data * data)380 int wpa_parse_wpa_ie_rsnxe(const u8 *rsnxe_ie, size_t rsnxe_ie_len,
381 struct wpa_ie_data *data)
382 {
383 uint8_t rsnxe_capa = 0;
384 uint8_t sae_pwe = esp_wifi_get_config_sae_pwe_h2e_internal(WIFI_IF_STA);
385 memset(data, 0, sizeof(*data));
386
387 if (rsnxe_ie_len < 1) {
388 return -1;
389 }
390 rsnxe_capa = rsnxe_ie[2];
391 if (sae_pwe == 1 && !(rsnxe_capa & BIT(WLAN_RSNX_CAPAB_SAE_H2E))){
392 wpa_printf(MSG_ERROR, "SAE H2E required, but not supported by the AP");
393 return -1;
394 }
395 data->rsnxe_capa = rsnxe_capa;
396 return 0;
397 }
398
399 /**
400 * wpa_parse_wpa_ie_rsn - Parse RSN IE
401 * @rsn_ie: Buffer containing RSN IE
402 * @rsn_ie_len: RSN IE buffer length (including IE number and length octets)
403 * @data: Pointer to structure that will be filled in with parsed data
404 * Returns: 0 on success, <0 on failure
405 */
wpa_parse_wpa_ie_rsn(const u8 * rsn_ie,size_t rsn_ie_len,struct wpa_ie_data * data)406 int wpa_parse_wpa_ie_rsn(const u8 *rsn_ie, size_t rsn_ie_len,
407 struct wpa_ie_data *data)
408 {
409 const struct rsn_ie_hdr *hdr;
410 const u8 *pos;
411 int left;
412 int i, count;
413
414 os_memset(data, 0, sizeof(*data));
415 data->proto = WPA_PROTO_RSN;
416 data->pairwise_cipher = WPA_CIPHER_CCMP;
417 data->group_cipher = WPA_CIPHER_CCMP;
418 data->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
419 data->capabilities = 0;
420 data->pmkid = NULL;
421 data->num_pmkid = 0;
422 data->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
423
424 wpa_hexdump(MSG_MSGDUMP, "rsn_ie", rsn_ie, rsn_ie_len);
425 if (rsn_ie_len == 0) {
426 /* No RSN IE - fail silently */
427 return -1;
428 }
429
430 if (rsn_ie_len < sizeof(struct rsn_ie_hdr)) {
431 wpa_printf(MSG_DEBUG, "%s: ie len too short %lu",
432 __func__, (unsigned long) rsn_ie_len);
433 return -1;
434 }
435
436 hdr = (const struct rsn_ie_hdr *) rsn_ie;
437
438 if (hdr->elem_id != WLAN_EID_RSN ||
439 hdr->len != rsn_ie_len - 2 ||
440 WPA_GET_LE16(hdr->version) != RSN_VERSION) {
441 wpa_printf(MSG_DEBUG, "%s: malformed ie or unknown version",
442 __func__);
443 return -2;
444 }
445
446 pos = (const u8 *) (hdr + 1);
447 left = rsn_ie_len - sizeof(*hdr);
448
449 if (left >= RSN_SELECTOR_LEN) {
450 data->group_cipher = rsn_selector_to_bitfield(pos);
451 pos += RSN_SELECTOR_LEN;
452 left -= RSN_SELECTOR_LEN;
453 } else if (left > 0) {
454 wpa_printf(MSG_DEBUG, "%s: ie length mismatch, %u too much",
455 __func__, left);
456 return -3;
457 }
458
459 if (left >= 2) {
460 data->pairwise_cipher = 0;
461 count = WPA_GET_LE16(pos);
462 pos += 2;
463 left -= 2;
464 if (count == 0 || left < count * RSN_SELECTOR_LEN) {
465 wpa_printf(MSG_DEBUG, "%s: ie count botch (pairwise), "
466 "count %u left %u", __func__, count, left);
467 return -4;
468 }
469 for (i = 0; i < count; i++) {
470 data->pairwise_cipher |= rsn_selector_to_bitfield(pos);
471 pos += RSN_SELECTOR_LEN;
472 left -= RSN_SELECTOR_LEN;
473 }
474 } else if (left == 1) {
475 wpa_printf(MSG_DEBUG, "%s: ie too short (for key mgmt)",
476 __func__);
477 return -5;
478 }
479
480 if (left >= 2) {
481 data->key_mgmt = 0;
482 count = WPA_GET_LE16(pos);
483 pos += 2;
484 left -= 2;
485 if (count == 0 || left < count * RSN_SELECTOR_LEN) {
486 wpa_printf(MSG_DEBUG, "%s: ie count botch (key mgmt), "
487 "count %u left %u", __func__, count, left);
488 return -6;
489 }
490 for (i = 0; i < count; i++) {
491 data->key_mgmt |= rsn_key_mgmt_to_bitfield(pos);
492 pos += RSN_SELECTOR_LEN;
493 left -= RSN_SELECTOR_LEN;
494 }
495 } else if (left == 1) {
496 wpa_printf(MSG_DEBUG, "%s: ie too short (for capabilities)",
497 __func__);
498 return -7;
499 }
500
501 if (left >= 2) {
502 data->capabilities = WPA_GET_LE16(pos);
503 pos += 2;
504 left -= 2;
505 }
506
507 if (left >= 2) {
508 u16 num_pmkid = WPA_GET_LE16(pos);
509 pos += 2;
510 left -= 2;
511 if (num_pmkid > (unsigned int) left / PMKID_LEN) {
512 wpa_printf(MSG_DEBUG, "%s: PMKID underflow "
513 "(num_pmkid=%u left=%d)",
514 __func__, num_pmkid, left);
515 data->num_pmkid = 0;
516 return -9;
517 } else {
518 data->num_pmkid = num_pmkid;
519 data->pmkid = pos;
520 pos += data->num_pmkid * PMKID_LEN;
521 left -= data->num_pmkid * PMKID_LEN;
522 }
523 }
524
525 if (left >= 4) {
526 data->mgmt_group_cipher = rsn_selector_to_bitfield(pos);
527 if (!wpa_cipher_valid_mgmt_group(data->mgmt_group_cipher)) {
528 wpa_printf(MSG_DEBUG,
529 "%s: Unsupported management group cipher 0x%x (%08x)",
530 __func__, data->mgmt_group_cipher,
531 WPA_GET_BE32(pos));
532 return -10;
533 }
534 pos += RSN_SELECTOR_LEN;
535 left -= RSN_SELECTOR_LEN;
536 }
537
538 if (left > 0) {
539 wpa_printf(MSG_DEBUG, "%s: ie has %u trailing bytes - ignored",
540 __func__, left);
541 }
542
543 return 0;
544 }
545
wpa_parse_wpa_ie_wpa(const u8 * wpa_ie,size_t wpa_ie_len,struct wpa_ie_data * data)546 int wpa_parse_wpa_ie_wpa(const u8 *wpa_ie, size_t wpa_ie_len,
547 struct wpa_ie_data *data)
548 {
549 const struct wpa_ie_hdr *hdr;
550 const u8 *pos;
551 int left;
552 int i, count;
553
554 memset(data, 0, sizeof(*data));
555 data->proto = WPA_PROTO_WPA;
556 data->pairwise_cipher = WPA_CIPHER_TKIP;
557 data->group_cipher = WPA_CIPHER_TKIP;
558 data->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
559 data->capabilities = 0;
560 data->pmkid = NULL;
561 data->num_pmkid = 0;
562 data->mgmt_group_cipher = 0;
563
564 if (wpa_ie_len == 0) {
565 /* No WPA IE - fail silently */
566 return -1;
567 }
568
569 if (wpa_ie_len < sizeof(struct wpa_ie_hdr)) {
570 wpa_printf(MSG_DEBUG, "%s: ie len too short %lu",
571 __func__, (unsigned long) wpa_ie_len);
572 return -1;
573 }
574
575 hdr = (const struct wpa_ie_hdr *) wpa_ie;
576
577 if (hdr->elem_id != WLAN_EID_VENDOR_SPECIFIC ||
578 hdr->len != wpa_ie_len - 2 ||
579 RSN_SELECTOR_GET(hdr->oui) != WPA_OUI_TYPE ||
580 WPA_GET_LE16(hdr->version) != WPA_VERSION) {
581 wpa_printf(MSG_DEBUG, "%s: malformed ie or unknown version",
582 __func__);
583 return -2;
584 }
585
586 pos = (const u8 *) (hdr + 1);
587 left = wpa_ie_len - sizeof(*hdr);
588
589 if (left >= WPA_SELECTOR_LEN) {
590 data->group_cipher = wpa_selector_to_bitfield(pos);
591 pos += WPA_SELECTOR_LEN;
592 left -= WPA_SELECTOR_LEN;
593 } else if (left > 0) {
594 wpa_printf(MSG_DEBUG, "%s: ie length mismatch, %u too much",
595 __func__, left);
596 return -3;
597 }
598
599 if (left >= 2) {
600 data->pairwise_cipher = 0;
601 count = WPA_GET_LE16(pos);
602 pos += 2;
603 left -= 2;
604 if (count == 0 || left < count * WPA_SELECTOR_LEN) {
605 wpa_printf(MSG_DEBUG, "%s: ie count botch (pairwise), "
606 "count %u left %u", __func__, count, left);
607 return -4;
608 }
609 for (i = 0; i < count; i++) {
610 data->pairwise_cipher |= wpa_selector_to_bitfield(pos);
611 pos += WPA_SELECTOR_LEN;
612 left -= WPA_SELECTOR_LEN;
613 }
614 } else if (left == 1) {
615 wpa_printf(MSG_DEBUG, "%s: ie too short (for key mgmt)",
616 __func__);
617 return -5;
618 }
619
620 if (left >= 2) {
621 data->key_mgmt = 0;
622 count = WPA_GET_LE16(pos);
623 pos += 2;
624 left -= 2;
625 if (count == 0 || left < count * WPA_SELECTOR_LEN) {
626 wpa_printf(MSG_DEBUG, "%s: ie count botch (key mgmt), "
627 "count %u left %u", __func__, count, left);
628 return -6;
629 }
630 for (i = 0; i < count; i++) {
631 data->key_mgmt |= wpa_key_mgmt_to_bitfield(pos);
632 pos += WPA_SELECTOR_LEN;
633 left -= WPA_SELECTOR_LEN;
634 }
635 } else if (left == 1) {
636 wpa_printf(MSG_DEBUG, "%s: ie too short (for capabilities)",
637 __func__);
638 return -7;
639 }
640
641 if (left >= 2) {
642 data->capabilities = WPA_GET_LE16(pos);
643 pos += 2;
644 left -= 2;
645 }
646
647 if (left > 0) {
648 wpa_printf(MSG_DEBUG, "%s: ie has %u trailing bytes - ignored",
649 __func__, left);
650 }
651
652 return 0;
653 }
654
655 #ifdef CONFIG_IEEE80211R
656
657 /**
658 * wpa_derive_pmk_r0 - Derive PMK-R0 and PMKR0Name
659 *
660 * IEEE Std 802.11r-2008 - 8.5.1.5.3
661 */
wpa_derive_pmk_r0(const u8 * xxkey,size_t xxkey_len,const u8 * ssid,size_t ssid_len,const u8 * mdid,const u8 * r0kh_id,size_t r0kh_id_len,const u8 * s0kh_id,u8 * pmk_r0,u8 * pmk_r0_name)662 void wpa_derive_pmk_r0(const u8 *xxkey, size_t xxkey_len,
663 const u8 *ssid, size_t ssid_len,
664 const u8 *mdid, const u8 *r0kh_id, size_t r0kh_id_len,
665 const u8 *s0kh_id, u8 *pmk_r0, u8 *pmk_r0_name)
666 {
667 u8 buf[1 + WPA_MAX_SSID_LEN + MOBILITY_DOMAIN_ID_LEN + 1 +
668 FT_R0KH_ID_MAX_LEN + ETH_ALEN];
669 u8 *pos, r0_key_data[48], hash[32];
670 const u8 *addr[2];
671 size_t len[2];
672
673 /*
674 * R0-Key-Data = KDF-384(XXKey, "FT-R0",
675 * SSIDlength || SSID || MDID || R0KHlength ||
676 * R0KH-ID || S0KH-ID)
677 * XXKey is either the second 256 bits of MSK or PSK.
678 * PMK-R0 = L(R0-Key-Data, 0, 256)
679 * PMK-R0Name-Salt = L(R0-Key-Data, 256, 128)
680 */
681 if (ssid_len > WPA_MAX_SSID_LEN || r0kh_id_len > FT_R0KH_ID_MAX_LEN)
682 return;
683 pos = buf;
684 *pos++ = ssid_len;
685 os_memcpy(pos, ssid, ssid_len);
686 pos += ssid_len;
687 os_memcpy(pos, mdid, MOBILITY_DOMAIN_ID_LEN);
688 pos += MOBILITY_DOMAIN_ID_LEN;
689 *pos++ = r0kh_id_len;
690 os_memcpy(pos, r0kh_id, r0kh_id_len);
691 pos += r0kh_id_len;
692 os_memcpy(pos, s0kh_id, ETH_ALEN);
693 pos += ETH_ALEN;
694
695 sha256_prf(xxkey, xxkey_len, "FT-R0", buf, pos - buf,
696 r0_key_data, sizeof(r0_key_data));
697 os_memcpy(pmk_r0, r0_key_data, PMK_LEN);
698
699 /*
700 * PMKR0Name = Truncate-128(SHA-256("FT-R0N" || PMK-R0Name-Salt)
701 */
702 addr[0] = (const u8 *) "FT-R0N";
703 len[0] = 6;
704 addr[1] = r0_key_data + PMK_LEN;
705 len[1] = 16;
706
707 sha256_vector(2, addr, len, hash);
708 os_memcpy(pmk_r0_name, hash, WPA_PMK_NAME_LEN);
709 }
710
711
712 /**
713 * wpa_derive_pmk_r1_name - Derive PMKR1Name
714 *
715 * IEEE Std 802.11r-2008 - 8.5.1.5.4
716 */
wpa_derive_pmk_r1_name(const u8 * pmk_r0_name,const u8 * r1kh_id,const u8 * s1kh_id,u8 * pmk_r1_name)717 void wpa_derive_pmk_r1_name(const u8 *pmk_r0_name, const u8 *r1kh_id,
718 const u8 *s1kh_id, u8 *pmk_r1_name)
719 {
720 u8 hash[32];
721 const u8 *addr[4];
722 size_t len[4];
723
724 /*
725 * PMKR1Name = Truncate-128(SHA-256("FT-R1N" || PMKR0Name ||
726 * R1KH-ID || S1KH-ID))
727 */
728 addr[0] = (const u8 *) "FT-R1N";
729 len[0] = 6;
730 addr[1] = pmk_r0_name;
731 len[1] = WPA_PMK_NAME_LEN;
732 addr[2] = r1kh_id;
733 len[2] = FT_R1KH_ID_LEN;
734 addr[3] = s1kh_id;
735 len[3] = ETH_ALEN;
736
737 sha256_vector(4, addr, len, hash);
738 os_memcpy(pmk_r1_name, hash, WPA_PMK_NAME_LEN);
739 }
740
741
742 /**
743 * wpa_derive_pmk_r1 - Derive PMK-R1 and PMKR1Name from PMK-R0
744 *
745 * IEEE Std 802.11r-2008 - 8.5.1.5.4
746 */
wpa_derive_pmk_r1(const u8 * pmk_r0,const u8 * pmk_r0_name,const u8 * r1kh_id,const u8 * s1kh_id,u8 * pmk_r1,u8 * pmk_r1_name)747 void wpa_derive_pmk_r1(const u8 *pmk_r0, const u8 *pmk_r0_name,
748 const u8 *r1kh_id, const u8 *s1kh_id,
749 u8 *pmk_r1, u8 *pmk_r1_name)
750 {
751 u8 buf[FT_R1KH_ID_LEN + ETH_ALEN];
752 u8 *pos;
753
754 /* PMK-R1 = KDF-256(PMK-R0, "FT-R1", R1KH-ID || S1KH-ID) */
755 pos = buf;
756 os_memcpy(pos, r1kh_id, FT_R1KH_ID_LEN);
757 pos += FT_R1KH_ID_LEN;
758 os_memcpy(pos, s1kh_id, ETH_ALEN);
759 pos += ETH_ALEN;
760
761 sha256_prf(pmk_r0, PMK_LEN, "FT-R1", buf, pos - buf, pmk_r1, PMK_LEN);
762
763 wpa_derive_pmk_r1_name(pmk_r0_name, r1kh_id, s1kh_id, pmk_r1_name);
764 }
765
766
767 /**
768 * wpa_pmk_r1_to_ptk - Derive PTK and PTKName from PMK-R1
769 *
770 * IEEE Std 802.11r-2008 - 8.5.1.5.5
771 */
wpa_pmk_r1_to_ptk(const u8 * pmk_r1,const u8 * snonce,const u8 * anonce,const u8 * sta_addr,const u8 * bssid,const u8 * pmk_r1_name,struct wpa_ptk * ptk,u8 * ptk_name,int akmp,int cipher)772 int wpa_pmk_r1_to_ptk(const u8 *pmk_r1, const u8 *snonce, const u8 *anonce,
773 const u8 *sta_addr, const u8 *bssid,
774 const u8 *pmk_r1_name,
775 struct wpa_ptk *ptk, u8 *ptk_name, int akmp, int cipher)
776 {
777 u8 buf[2 * WPA_NONCE_LEN + 2 * ETH_ALEN];
778 u8 *pos, hash[32];
779 const u8 *addr[6];
780 size_t len[6];
781 u8 tmp[WPA_KCK_MAX_LEN + WPA_KEK_MAX_LEN + WPA_TK_MAX_LEN];
782 size_t ptk_len;
783
784 /*
785 * PTK = KDF-PTKLen(PMK-R1, "FT-PTK", SNonce || ANonce ||
786 * BSSID || STA-ADDR)
787 */
788 pos = buf;
789 os_memcpy(pos, snonce, WPA_NONCE_LEN);
790 pos += WPA_NONCE_LEN;
791 os_memcpy(pos, anonce, WPA_NONCE_LEN);
792 pos += WPA_NONCE_LEN;
793 os_memcpy(pos, bssid, ETH_ALEN);
794 pos += ETH_ALEN;
795 os_memcpy(pos, sta_addr, ETH_ALEN);
796 pos += ETH_ALEN;
797
798 ptk->kck_len = wpa_kck_len(akmp, PMK_LEN);
799 ptk->kek_len = wpa_kek_len(akmp, PMK_LEN);
800 ptk->tk_len = wpa_cipher_key_len(cipher);
801 ptk_len = ptk->kck_len + ptk->kek_len + ptk->tk_len;
802
803 sha256_prf(pmk_r1, PMK_LEN, "FT-PTK", buf, pos - buf, tmp, ptk_len);
804
805 /*
806 * PTKName = Truncate-128(SHA-256(PMKR1Name || "FT-PTKN" || SNonce ||
807 * ANonce || BSSID || STA-ADDR))
808 */
809 addr[0] = pmk_r1_name;
810 len[0] = WPA_PMK_NAME_LEN;
811 addr[1] = (const u8 *) "FT-PTKN";
812 len[1] = 7;
813 addr[2] = snonce;
814 len[2] = WPA_NONCE_LEN;
815 addr[3] = anonce;
816 len[3] = WPA_NONCE_LEN;
817 addr[4] = bssid;
818 len[4] = ETH_ALEN;
819 addr[5] = sta_addr;
820 len[5] = ETH_ALEN;
821
822 sha256_vector(6, addr, len, hash);
823 os_memcpy(ptk_name, hash, WPA_PMK_NAME_LEN);
824
825 os_memcpy(ptk->kck, tmp, ptk->kck_len);
826 os_memcpy(ptk->kek, tmp + ptk->kck_len, ptk->kek_len);
827 os_memcpy(ptk->tk, tmp + ptk->kck_len + ptk->kek_len, ptk->tk_len);
828
829 wpa_hexdump_key(MSG_DEBUG, "FT: KCK", ptk->kck, ptk->kck_len);
830 wpa_hexdump_key(MSG_DEBUG, "FT: KEK", ptk->kek, ptk->kek_len);
831 wpa_hexdump_key(MSG_DEBUG, "FT: TK", ptk->tk, ptk->tk_len);
832 wpa_hexdump(MSG_DEBUG, "FT: PTKName", ptk_name, WPA_PMK_NAME_LEN);
833
834 os_memset(tmp, 0, sizeof(tmp));
835
836 return 0;
837 }
838
839 #endif /* CONFIG_IEEE80211R */
840
841 /**
842 * wpa_use_akm_defined - Is AKM-defined Key Descriptor Version used
843 * @akmp: WPA_KEY_MGMT_* used in key derivation
844 * Returns: 1 if AKM-defined Key Descriptor Version is used; 0 otherwise
845 */
846
wpa_use_akm_defined(int akmp)847 int wpa_use_akm_defined(int akmp){
848 return akmp == WPA_KEY_MGMT_OSEN ||
849 akmp == WPA_KEY_MGMT_OWE ||
850 akmp == WPA_KEY_MGMT_DPP ||
851 wpa_key_mgmt_sae(akmp) ||
852 wpa_key_mgmt_suite_b(akmp);
853 }
854
855 /**
856 * wpa_use_aes_key_wrap - Is AES Keywrap algorithm used for EAPOL-Key Key Data
857 * @akmp: WPA_KEY_MGMT_* used in key derivation
858 * Returns: 1 if AES Keywrap is used; 0 otherwise
859 *
860 * Note: AKM 00-0F-AC:1 and 00-0F-AC:2 have special rules for selecting whether
861 * to use AES Keywrap based on the negotiated pairwise cipher. This function
862 * does not cover those special cases.
863 */
wpa_use_aes_key_wrap(int akmp)864 int wpa_use_aes_key_wrap(int akmp)
865 {
866 return akmp == WPA_KEY_MGMT_OSEN ||
867 akmp == WPA_KEY_MGMT_OWE ||
868 akmp == WPA_KEY_MGMT_DPP ||
869 wpa_key_mgmt_ft(akmp) ||
870 wpa_key_mgmt_sha256(akmp) ||
871 wpa_key_mgmt_sae(akmp) ||
872 wpa_key_mgmt_suite_b(akmp);
873 }
874
875 /**
876 * wpa_eapol_key_mic - Calculate EAPOL-Key MIC
877 * @key: EAPOL-Key Key Confirmation Key (KCK)
878 * @key_len: KCK length in octets
879 * @akmp: WPA_KEY_MGMT_* used in key derivation
880 * @ver: Key descriptor version (WPA_KEY_INFO_TYPE_*)
881 * @buf: Pointer to the beginning of the EAPOL header (version field)
882 * @len: Length of the EAPOL frame (from EAPOL header to the end of the frame)
883 * @mic: Pointer to the buffer to which the EAPOL-Key MIC is written
884 * Returns: 0 on success, -1 on failure
885 *
886 * Calculate EAPOL-Key MIC for an EAPOL-Key packet. The EAPOL-Key MIC field has
887 * to be cleared (all zeroes) when calling this function.
888 *
889 * Note: 'IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames' has an error in the
890 * description of the Key MIC calculation. It includes packet data from the
891 * beginning of the EAPOL-Key header, not EAPOL header. This incorrect change
892 * happened during final editing of the standard and the correct behavior is
893 * defined in the last draft (IEEE 802.11i/D10).
894 */
wpa_eapol_key_mic(const u8 * key,size_t key_len,int akmp,int ver,const u8 * buf,size_t len,u8 * mic)895 int wpa_eapol_key_mic(const u8 *key, size_t key_len, int akmp, int ver,
896 const u8 *buf, size_t len, u8 *mic)
897 {
898 u8 hash[SHA384_MAC_LEN];
899
900 switch (ver) {
901 case WPA_KEY_INFO_TYPE_HMAC_MD5_RC4:
902 return hmac_md5(key, key_len, buf, len, mic);
903 case WPA_KEY_INFO_TYPE_HMAC_SHA1_AES:
904 if (hmac_sha1(key, key_len, buf, len, hash))
905 return -1;
906 os_memcpy(mic, hash, MD5_MAC_LEN);
907 break;
908 case WPA_KEY_INFO_TYPE_AES_128_CMAC:
909 return omac1_aes_128(key, buf, len, mic);
910 #ifdef CONFIG_IEEE80211W
911 case WPA_KEY_INFO_TYPE_AKM_DEFINED:
912 switch (akmp) {
913 #ifdef CONFIG_WPA3_SAE
914 case WPA_KEY_MGMT_SAE:
915 return omac1_aes_128(key, buf, len, mic);
916 #endif /* CONFIG_WPA3_SAE */
917 #ifdef CONFIG_SUITEB
918 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
919 if (hmac_sha256(key, key_len, buf, len, hash))
920 return -1;
921 os_memcpy(mic, hash, MD5_MAC_LEN);
922 break;
923 #endif /* CONFIG_SUITEB */
924 #ifdef CONFIG_SUITEB192
925 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
926 if (hmac_sha384(key, key_len, buf, len, hash))
927 return -1;
928 os_memcpy(mic, hash, 24);
929 break;
930 #endif /* CONFIG_SUITEB192 */
931 #ifdef CONFIG_OWE_STA
932 case WPA_KEY_MGMT_OWE:
933 wpa_printf(MSG_DEBUG,
934 "WPA: EAPOL-Key MIC using HMAC-SHA%u (AKM-defined - OWE)",
935 (unsigned int) key_len * 8 * 2);
936 if (key_len == 128 / 8) {
937 if (hmac_sha256(key, key_len, buf, len, hash))
938 return -1;
939 } else {
940 wpa_printf(MSG_INFO,"OWE: Unsupported KCK length: %u",
941 (unsigned int) key_len);
942 return -1;
943 }
944 os_memcpy(mic, hash, key_len);
945 break;
946
947 #endif /* CONFIG_OWE_STA */
948 #endif /* CONFIG_IEEE80211W */
949 default:
950 return -1;
951 }
952 break;
953
954 default:
955 return -1;
956 }
957
958 return 0;
959 }
960
wpa_akm_to_suite(int akm)961 u32 wpa_akm_to_suite(int akm)
962 {
963 #ifdef CONFIG_IEEE80211R
964 if (akm & WPA_KEY_MGMT_FT_IEEE8021X)
965 return RSN_AUTH_KEY_MGMT_FT_802_1X;
966 if (akm & WPA_KEY_MGMT_FT_PSK)
967 return RSN_AUTH_KEY_MGMT_FT_PSK;
968 #endif /* CONFIG_IEEE80211R */
969 if (akm & WPA_KEY_MGMT_IEEE8021X_SHA256)
970 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
971 if (akm & WPA_KEY_MGMT_IEEE8021X)
972 return RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
973 if (akm & WPA_KEY_MGMT_PSK_SHA256)
974 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
975 if (akm & WPA_KEY_MGMT_PSK)
976 return RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
977 if (akm & WPA_KEY_MGMT_IEEE8021X_SUITE_B)
978 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
979 if (akm & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
980 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
981 if (akm & WPA_KEY_MGMT_SAE)
982 return RSN_AUTH_KEY_MGMT_SAE;
983 if (akm & WPA_KEY_MGMT_FT_SAE)
984 return RSN_AUTH_KEY_MGMT_FT_SAE;
985 if (akm & WPA_KEY_MGMT_OWE)
986 return RSN_AUTH_KEY_MGMT_OWE;
987 return 0;
988 }
989
wpa_compare_rsn_ie(int ft_initial_assoc,const u8 * ie1,size_t ie1len,const u8 * ie2,size_t ie2len)990 int wpa_compare_rsn_ie(int ft_initial_assoc,
991 const u8 *ie1, size_t ie1len,
992 const u8 *ie2, size_t ie2len)
993 {
994 if (ie1 == NULL || ie2 == NULL)
995 return -1;
996
997 if (ie1len == ie2len && os_memcmp(ie1, ie2, ie1len) == 0)
998 return 0; /* identical IEs */
999
1000 #ifdef CONFIG_IEEE80211R
1001 if (ft_initial_assoc) {
1002 struct wpa_ie_data ie1d, ie2d;
1003 /*
1004 * The PMKID-List in RSN IE is different between Beacon/Probe
1005 * Response/(Re)Association Request frames and EAPOL-Key
1006 * messages in FT initial mobility domain association. Allow
1007 * for this, but verify that other parts of the RSN IEs are
1008 * identical.
1009 */
1010 if (wpa_parse_wpa_ie_rsn(ie1, ie1len, &ie1d) < 0 ||
1011 wpa_parse_wpa_ie_rsn(ie2, ie2len, &ie2d) < 0)
1012 return -1;
1013 if (ie1d.proto == ie2d.proto &&
1014 ie1d.pairwise_cipher == ie2d.pairwise_cipher &&
1015 ie1d.group_cipher == ie2d.group_cipher &&
1016 ie1d.key_mgmt == ie2d.key_mgmt &&
1017 ie1d.capabilities == ie2d.capabilities &&
1018 ie1d.mgmt_group_cipher == ie2d.mgmt_group_cipher &&
1019 ie1d.rsnxe_capa == ie2d.rsnxe_capa)
1020 return 0;
1021 }
1022 #endif /* CONFIG_IEEE80211R */
1023
1024 return -1;
1025 }
1026
1027 #ifdef CONFIG_SUITEB
1028 /**
1029 * rsn_pmkid_suite_b - Calculate PMK identifier for Suite B AKM
1030 * @kck: Key confirmation key
1031 * @kck_len: Length of kck in bytes
1032 * @aa: Authenticator address
1033 * @spa: Supplicant address
1034 * @pmkid: Buffer for PMKID
1035 * Returns: 0 on success, -1 on failure
1036 *
1037 * IEEE Std 802.11ac-2013 - 11.6.1.3 Pairwise key hierarchy
1038 * PMKID = Truncate(HMAC-SHA-256(KCK, "PMK Name" || AA || SPA))
1039 */
rsn_pmkid_suite_b(const u8 * kck,size_t kck_len,const u8 * aa,const u8 * spa,u8 * pmkid)1040 int rsn_pmkid_suite_b(const u8 *kck, size_t kck_len, const u8 *aa,
1041 const u8 *spa, u8 *pmkid)
1042 {
1043 char *title = "PMK Name";
1044 const u8 *addr[3];
1045 const size_t len[3] = { 8, ETH_ALEN, ETH_ALEN };
1046 unsigned char hash[SHA256_MAC_LEN];
1047
1048 addr[0] = (u8 *) title;
1049 addr[1] = aa;
1050 addr[2] = spa;
1051
1052 if (hmac_sha256_vector(kck, kck_len, 3, addr, len, hash) < 0)
1053 return -1;
1054 os_memcpy(pmkid, hash, PMKID_LEN);
1055 return 0;
1056 }
1057 #endif /* CONFIG_SUITEB */
1058
1059 #ifdef CONFIG_SUITEB192
1060 /**
1061 * rsn_pmkid_suite_b_192 - Calculate PMK identifier for Suite B AKM
1062 * @kck: Key confirmation key
1063 * @kck_len: Length of kck in bytes
1064 * @aa: Authenticator address
1065 * @spa: Supplicant address
1066 * @pmkid: Buffer for PMKID
1067 * Returns: 0 on success, -1 on failure
1068 *
1069 * IEEE Std 802.11ac-2013 - 11.6.1.3 Pairwise key hierarchy
1070 * PMKID = Truncate(HMAC-SHA-384(KCK, "PMK Name" || AA || SPA))
1071 */
rsn_pmkid_suite_b_192(const u8 * kck,size_t kck_len,const u8 * aa,const u8 * spa,u8 * pmkid)1072 int rsn_pmkid_suite_b_192(const u8 *kck, size_t kck_len, const u8 *aa,
1073 const u8 *spa, u8 *pmkid)
1074 {
1075 char *title = "PMK Name";
1076 const u8 *addr[3];
1077 const size_t len[3] = { 8, ETH_ALEN, ETH_ALEN };
1078 unsigned char hash[SHA384_MAC_LEN];
1079
1080 addr[0] = (u8 *) title;
1081 addr[1] = aa;
1082 addr[2] = spa;
1083
1084 if (hmac_sha384_vector(kck, kck_len, 3, addr, len, hash) < 0)
1085 return -1;
1086 os_memcpy(pmkid, hash, PMKID_LEN);
1087 return 0;
1088 }
1089 #endif /* CONFIG_SUITEB192 */
1090
1091 /**
1092 * wpa_cipher_txt - Convert cipher suite to a text string
1093 * @cipher: Cipher suite (WPA_CIPHER_* enum)
1094 * Returns: Pointer to a text string of the cipher suite name
1095 */
wpa_cipher_txt(int cipher)1096 const char * wpa_cipher_txt(int cipher)
1097 {
1098 switch (cipher) {
1099 case WPA_CIPHER_NONE:
1100 return "NONE";
1101 case WPA_CIPHER_WEP40:
1102 return "WEP-40";
1103 case WPA_CIPHER_WEP104:
1104 return "WEP-104";
1105 case WPA_CIPHER_TKIP:
1106 return "TKIP";
1107 case WPA_CIPHER_CCMP:
1108 return "CCMP";
1109 case WPA_CIPHER_CCMP | WPA_CIPHER_TKIP:
1110 return "CCMP+TKIP";
1111 case WPA_CIPHER_GCMP:
1112 return "GCMP";
1113 case WPA_CIPHER_GCMP_256:
1114 return "GCMP-256";
1115 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
1116 return "WPA2-EAP-SUITE-B";
1117 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
1118 return "WPA2-EAP-SUITE-B-192";
1119 default:
1120 return "UNKNOWN";
1121 }
1122 }
1123
1124 /**
1125 * wpa_pmk_to_ptk - Calculate PTK from PMK, addresses, and nonces
1126 * @pmk: Pairwise master key
1127 * @pmk_len: Length of PMK
1128 * @label: Label to use in derivation
1129 * @addr1: AA or SA
1130 * @addr2: SA or AA
1131 * @nonce1: ANonce or SNonce
1132 * @nonce2: SNonce or ANonce
1133 * @ptk: Buffer for pairwise transient key
1134 * @akmp: Negotiated AKM
1135 * @cipher: Negotiated pairwise cipher
1136 * Returns: 0 on success, -1 on failure
1137 *
1138 * IEEE Std 802.11i-2004 - 8.5.1.2 Pairwise key hierarchy
1139 * PTK = PRF-X(PMK, "Pairwise key expansion",
1140 * Min(AA, SA) || Max(AA, SA) ||
1141 * Min(ANonce, SNonce) || Max(ANonce, SNonce))
1142 */
wpa_pmk_to_ptk(const u8 * pmk,size_t pmk_len,const char * label,const u8 * addr1,const u8 * addr2,const u8 * nonce1,const u8 * nonce2,struct wpa_ptk * ptk,int akmp,int cipher)1143 int wpa_pmk_to_ptk(const u8 *pmk, size_t pmk_len, const char *label,
1144 const u8 *addr1, const u8 *addr2,
1145 const u8 *nonce1, const u8 *nonce2,
1146 struct wpa_ptk *ptk, int akmp, int cipher)
1147 {
1148 if (pmk_len == 0) {
1149 wpa_printf(MSG_ERROR, "WPA: No PMK set for PTK derivation");
1150 return -1;
1151 }
1152 u8 data[2 * ETH_ALEN + 2 * WPA_NONCE_LEN];
1153 size_t data_len = 2 * ETH_ALEN + 2 * WPA_NONCE_LEN;
1154 u8 tmp[WPA_KCK_MAX_LEN + WPA_KEK_MAX_LEN + WPA_TK_MAX_LEN];
1155 size_t ptk_len;
1156
1157 if (memcmp(addr1, addr2, ETH_ALEN) < 0) {
1158 memcpy(data, addr1, ETH_ALEN);
1159 memcpy(data + ETH_ALEN, addr2, ETH_ALEN);
1160 } else {
1161 memcpy(data, addr2, ETH_ALEN);
1162 memcpy(data + ETH_ALEN, addr1, ETH_ALEN);
1163 }
1164
1165 if (memcmp(nonce1, nonce2, WPA_NONCE_LEN) < 0) {
1166 memcpy(data + 2 * ETH_ALEN, nonce1, WPA_NONCE_LEN);
1167 memcpy(data + 2 * ETH_ALEN + WPA_NONCE_LEN, nonce2,
1168 WPA_NONCE_LEN);
1169 } else {
1170 memcpy(data + 2 * ETH_ALEN, nonce2, WPA_NONCE_LEN);
1171 memcpy(data + 2 * ETH_ALEN + WPA_NONCE_LEN, nonce1,
1172 WPA_NONCE_LEN);
1173 }
1174
1175 ptk->kck_len = wpa_kck_len(akmp, pmk_len);
1176 ptk->kek_len = wpa_kek_len(akmp, pmk_len);
1177 ptk->tk_len = wpa_cipher_key_len(cipher);
1178 ptk_len = ptk->kck_len + ptk->kek_len + ptk->tk_len;
1179
1180 #if defined(CONFIG_SUITEB192)
1181 if (wpa_key_mgmt_sha384(akmp)) {
1182 wpa_printf(MSG_DEBUG, "WPA: PTK derivation using PRF(SHA384)");
1183 if (sha384_prf(pmk, pmk_len, label, data, data_len,
1184 tmp, ptk_len) < 0)
1185 return -1;
1186 } else
1187 #endif
1188 if (wpa_key_mgmt_sha256(akmp))
1189 sha256_prf(pmk, pmk_len, label, data, data_len,
1190 tmp, ptk_len);
1191 else
1192 sha1_prf(pmk, pmk_len, label, data, data_len, tmp, ptk_len);
1193
1194 wpa_printf(MSG_DEBUG, "WPA: PTK derivation - A1=" MACSTR " A2=" MACSTR"\n",
1195 MAC2STR(addr1), MAC2STR(addr2));
1196
1197 wpa_hexdump(MSG_MSGDUMP, "WPA: PMK", pmk, pmk_len);
1198 wpa_hexdump_key(MSG_DEBUG, "WPA: PTK", tmp, ptk_len);
1199
1200 os_memcpy(ptk->kck, tmp, ptk->kck_len);
1201 wpa_hexdump_key(MSG_DEBUG, "WPA: KCK", ptk->kck, ptk->kck_len);
1202
1203 os_memcpy(ptk->kek, tmp + ptk->kck_len, ptk->kek_len);
1204 wpa_hexdump_key(MSG_DEBUG, "WPA: KEK", ptk->kek, ptk->kek_len);
1205
1206 os_memcpy(ptk->tk, tmp + ptk->kck_len + ptk->kek_len, ptk->tk_len);
1207 wpa_hexdump_key(MSG_DEBUG, "WPA: TK", ptk->tk, ptk->tk_len);
1208
1209 os_memset(tmp, 0, sizeof(tmp));
1210 return 0;
1211 }
1212
1213 /**
1214 * rsn_pmkid - Calculate PMK identifier
1215 * @pmk: Pairwise master key
1216 * @pmk_len: Length of pmk in bytes
1217 * @aa: Authenticator address
1218 * @spa: Supplicant address
1219 * @pmkid: Buffer for PMKID
1220 * @use_sha256: Whether to use SHA256-based KDF
1221 *
1222 * IEEE Std 802.11i-2004 - 8.5.1.2 Pairwise key hierarchy
1223 * PMKID = HMAC-SHA1-128(PMK, "PMK Name" || AA || SPA)
1224 */
rsn_pmkid(const u8 * pmk,size_t pmk_len,const u8 * aa,const u8 * spa,u8 * pmkid,int akmp)1225 void rsn_pmkid(const u8 *pmk, size_t pmk_len, const u8 *aa, const u8 *spa,
1226 u8 *pmkid, int akmp)
1227 {
1228 char *title = "PMK Name";
1229 const u8 *addr[3];
1230 const size_t len[3] = { 8, ETH_ALEN, ETH_ALEN };
1231 unsigned char hash[SHA256_MAC_LEN];
1232
1233 addr[0] = (u8 *) title;
1234 addr[1] = aa;
1235 addr[2] = spa;
1236
1237 #ifdef CONFIG_IEEE80211W
1238 if (wpa_key_mgmt_sha256(akmp)) {
1239 wpa_printf(MSG_DEBUG, "RSN: Derive PMKID using HMAC-SHA-256");
1240 hmac_sha256_vector(pmk, pmk_len, 3, addr, len, hash);
1241 } else
1242 #endif /* CONFIG_IEEE80211W */
1243 {
1244 wpa_printf(MSG_DEBUG, "RSN: Derive PMKID using HMAC-SHA-1");
1245 hmac_sha1_vector(pmk, pmk_len, 3, addr, len, hash);
1246 }
1247 os_memcpy(pmkid, hash, PMKID_LEN);
1248 }
1249
1250
wpa_insert_pmkid(u8 * ies,size_t * ies_len,const u8 * pmkid)1251 int wpa_insert_pmkid(u8 *ies, size_t *ies_len, const u8 *pmkid)
1252 {
1253 u8 *start, *end, *rpos, *rend;
1254 int added = 0;
1255
1256 start = ies;
1257 end = ies + *ies_len;
1258
1259 while (start < end) {
1260 if (*start == WLAN_EID_RSN)
1261 break;
1262 start += 2 + start[1];
1263 }
1264 if (start >= end) {
1265 wpa_printf(MSG_ERROR, "RSN: Could not find RSNE in IEs data");
1266 return -1;
1267 }
1268 wpa_hexdump(MSG_DEBUG, "RSN: RSNE before modification",
1269 start, 2 + start[1]);
1270
1271 /* Find start of PMKID-Count */
1272 rpos = start + 2;
1273 rend = rpos + start[1];
1274
1275 /* Skip Version and Group Data Cipher Suite */
1276 rpos += 2 + 4;
1277 /* Skip Pairwise Cipher Suite Count and List */
1278 rpos += 2 + WPA_GET_LE16(rpos) * RSN_SELECTOR_LEN;
1279 /* Skip AKM Suite Count and List */
1280 rpos += 2 + WPA_GET_LE16(rpos) * RSN_SELECTOR_LEN;
1281
1282 if (rpos == rend) {
1283 /* Add RSN Capabilities */
1284 os_memmove(rpos + 2, rpos, end - rpos);
1285 *rpos++ = 0;
1286 *rpos++ = 0;
1287 added += 2;
1288 start[1] += 2;
1289 rend = rpos;
1290 } else {
1291 /* Skip RSN Capabilities */
1292 rpos += 2;
1293 if (rpos > rend) {
1294 wpa_printf(MSG_ERROR,
1295 "RSN: Could not parse RSNE in IEs data");
1296 return -1;
1297 }
1298 }
1299
1300 if (rpos == rend) {
1301 /* No PMKID-Count field included; add it */
1302 os_memmove(rpos + 2 + PMKID_LEN, rpos, end + added - rpos);
1303 WPA_PUT_LE16(rpos, 1);
1304 rpos += 2;
1305 os_memcpy(rpos, pmkid, PMKID_LEN);
1306 added += 2 + PMKID_LEN;
1307 start[1] += 2 + PMKID_LEN;
1308 } else {
1309 u16 num_pmkid;
1310
1311 if (rend - rpos < 2)
1312 return -1;
1313 num_pmkid = WPA_GET_LE16(rpos);
1314 /* PMKID-Count was included; use it */
1315 if (num_pmkid != 0) {
1316 u8 *after;
1317
1318 if (num_pmkid * PMKID_LEN > rend - rpos - 2)
1319 return -1;
1320 /*
1321 * PMKID may have been included in RSN IE in
1322 * (Re)Association Request frame, so remove the old
1323 * PMKID(s) first before adding the new one.
1324 */
1325 wpa_printf(MSG_DEBUG,
1326 "RSN: Remove %u old PMKID(s) from RSNE",
1327 num_pmkid);
1328 after = rpos + 2 + num_pmkid * PMKID_LEN;
1329 os_memmove(rpos + 2, after, end - after);
1330 start[1] -= num_pmkid * PMKID_LEN;
1331 added -= num_pmkid * PMKID_LEN;
1332 }
1333 WPA_PUT_LE16(rpos, 1);
1334 rpos += 2;
1335 os_memmove(rpos + PMKID_LEN, rpos, end + added - rpos);
1336 os_memcpy(rpos, pmkid, PMKID_LEN);
1337 added += PMKID_LEN;
1338 start[1] += PMKID_LEN;
1339 }
1340
1341 wpa_hexdump(MSG_DEBUG, "RSN: RSNE after modification (PMKID inserted)",
1342 start, 2 + start[1]);
1343
1344 *ies_len += added;
1345
1346 return 0;
1347 }
1348
1349
1350
1351
wpa_cipher_key_len(int cipher)1352 int wpa_cipher_key_len(int cipher)
1353 {
1354 switch (cipher) {
1355 case WPA_CIPHER_TKIP:
1356 #ifdef CONFIG_GCMP
1357 case WPA_CIPHER_GCMP_256:
1358 #endif
1359 #ifdef CONFIG_GMAC
1360 case WPA_CIPHER_BIP_GMAC_256:
1361 #endif
1362 return 32;
1363 case WPA_CIPHER_CCMP:
1364 #ifdef CONFIG_GCMP
1365 case WPA_CIPHER_GCMP:
1366 #endif
1367 #ifdef CONFIG_GMAC
1368 case WPA_CIPHER_BIP_GMAC_128:
1369 #endif
1370 case WPA_CIPHER_AES_128_CMAC:
1371 return 16;
1372 case WPA_CIPHER_WEP104:
1373 return 13;
1374 case WPA_CIPHER_WEP40:
1375 return 5;
1376 }
1377
1378 return 0;
1379 }
1380
wpa_cipher_rsc_len(int cipher)1381 int wpa_cipher_rsc_len(int cipher)
1382 {
1383 switch (cipher) {
1384 case WPA_CIPHER_GCMP_256:
1385 case WPA_CIPHER_CCMP:
1386 case WPA_CIPHER_GCMP:
1387 case WPA_CIPHER_TKIP:
1388 return 6;
1389 }
1390
1391 return 0;
1392 }
1393
wpa_cipher_to_alg(int cipher)1394 int wpa_cipher_to_alg(int cipher)
1395 {
1396 switch (cipher) {
1397 case WPA_CIPHER_CCMP:
1398 return WIFI_WPA_ALG_CCMP;
1399 #ifdef CONFIG_GCMP
1400 case WPA_CIPHER_GCMP_256:
1401 case WPA_CIPHER_GCMP:
1402 return WIFI_WPA_ALG_GCMP;
1403 #endif
1404 case WPA_CIPHER_TKIP:
1405 return WIFI_WPA_ALG_TKIP;
1406 case WPA_CIPHER_WEP104:
1407 return WIFI_WPA_ALG_WEP104;
1408 case WPA_CIPHER_WEP40:
1409 return WIFI_WPA_ALG_WEP40;
1410 }
1411 return WIFI_WPA_ALG_NONE;
1412 }
1413
1414
wpa_cipher_valid_pairwise(int cipher)1415 int wpa_cipher_valid_pairwise(int cipher)
1416 {
1417 return cipher == WPA_CIPHER_GCMP_256 ||
1418 cipher == WPA_CIPHER_CCMP ||
1419 cipher == WPA_CIPHER_GCMP ||
1420 cipher == WPA_CIPHER_TKIP;
1421 }
1422
1423
1424
wpa_cipher_to_suite(int proto,int cipher)1425 u32 wpa_cipher_to_suite(int proto, int cipher)
1426 {
1427 if (cipher & WPA_CIPHER_CCMP)
1428 return (proto == WPA_PROTO_RSN ?
1429 RSN_CIPHER_SUITE_CCMP : WPA_CIPHER_SUITE_CCMP);
1430 #ifdef CONFIG_GCMP
1431 if (cipher & WPA_CIPHER_GCMP_256)
1432 return RSN_CIPHER_SUITE_GCMP_256;
1433 if (cipher & WPA_CIPHER_GCMP)
1434 return RSN_CIPHER_SUITE_GCMP;
1435 #endif
1436 if (cipher & WPA_CIPHER_TKIP)
1437 return (proto == WPA_PROTO_RSN ?
1438 RSN_CIPHER_SUITE_TKIP : WPA_CIPHER_SUITE_TKIP);
1439 if (cipher & WPA_CIPHER_WEP104)
1440 return (proto == WPA_PROTO_RSN ?
1441 RSN_CIPHER_SUITE_WEP104 : WPA_CIPHER_SUITE_WEP104);
1442 if (cipher & WPA_CIPHER_WEP40)
1443 return (proto == WPA_PROTO_RSN ?
1444 RSN_CIPHER_SUITE_WEP40 : WPA_CIPHER_SUITE_WEP40);
1445 if (cipher & WPA_CIPHER_NONE)
1446 return (proto == WPA_PROTO_RSN ?
1447 RSN_CIPHER_SUITE_NONE : WPA_CIPHER_SUITE_NONE);
1448 if (cipher & WPA_CIPHER_AES_128_CMAC)
1449 return RSN_CIPHER_SUITE_AES_128_CMAC;
1450 #if CONFIG_GMAC
1451 if (cipher & WPA_CIPHER_BIP_GMAC_128)
1452 return RSN_CIPHER_SUITE_BIP_GMAC_128;
1453 if (cipher & WPA_CIPHER_BIP_GMAC_256)
1454 return RSN_CIPHER_SUITE_BIP_GMAC_256;
1455 #endif
1456 return 0;
1457 }
1458
rsn_cipher_put_suites(u8 * pos,int ciphers)1459 int rsn_cipher_put_suites(u8 *pos, int ciphers)
1460 {
1461 int num_suites = 0;
1462
1463 #ifdef CONFIG_GCMP
1464 if (ciphers & WPA_CIPHER_GCMP_256) {
1465 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_GCMP_256);
1466 pos += RSN_SELECTOR_LEN;
1467 num_suites++;
1468 }
1469 if (ciphers & WPA_CIPHER_GCMP) {
1470 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_GCMP);
1471 pos += RSN_SELECTOR_LEN;
1472 num_suites++;
1473 }
1474 #endif
1475 if (ciphers & WPA_CIPHER_CCMP) {
1476 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP);
1477 pos += RSN_SELECTOR_LEN;
1478 num_suites++;
1479 }
1480 if (ciphers & WPA_CIPHER_TKIP) {
1481 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_TKIP);
1482 pos += RSN_SELECTOR_LEN;
1483 num_suites++;
1484 }
1485 if (ciphers & WPA_CIPHER_NONE) {
1486 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_NONE);
1487 pos += RSN_SELECTOR_LEN;
1488 num_suites++;
1489 }
1490
1491 return num_suites;
1492 }
1493
wpa_cipher_put_suites(u8 * pos,int ciphers)1494 int wpa_cipher_put_suites(u8 *pos, int ciphers)
1495 {
1496 int num_suites = 0;
1497
1498 if (ciphers & WPA_CIPHER_CCMP) {
1499 RSN_SELECTOR_PUT(pos, WPA_CIPHER_SUITE_CCMP);
1500 pos += WPA_SELECTOR_LEN;
1501 num_suites++;
1502 }
1503 if (ciphers & WPA_CIPHER_TKIP) {
1504 RSN_SELECTOR_PUT(pos, WPA_CIPHER_SUITE_TKIP);
1505 pos += WPA_SELECTOR_LEN;
1506 num_suites++;
1507 }
1508 if (ciphers & WPA_CIPHER_NONE) {
1509 RSN_SELECTOR_PUT(pos, WPA_CIPHER_SUITE_NONE);
1510 pos += WPA_SELECTOR_LEN;
1511 num_suites++;
1512 }
1513
1514 return num_suites;
1515 }
1516
1517 #endif // ESP_SUPPLICANT
1518