handshake.c (30210B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2013-2019 Intel Corporation. All rights reserved. 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 * 21 */ 22 23 #ifdef HAVE_CONFIG_H 24 #include <config.h> 25 #endif 26 27 #define _GNU_SOURCE 28 #include <string.h> 29 #include <unistd.h> 30 #include <errno.h> 31 #include <sys/socket.h> 32 #include <linux/if.h> 33 #include <linux/if_packet.h> 34 #include <linux/if_ether.h> 35 #include <arpa/inet.h> 36 #include <linux/filter.h> 37 38 #include <ell/ell.h> 39 40 #include "src/missing.h" 41 #include "src/crypto.h" 42 #include "src/ie.h" 43 #include "src/util.h" 44 #include "src/handshake.h" 45 #include "src/erp.h" 46 #include "src/band.h" 47 #include "src/pmksa.h" 48 49 static inline unsigned int n_ecc_groups(void) 50 { 51 const unsigned int *groups = l_ecc_supported_ike_groups(); 52 unsigned int j = 0; 53 54 while (groups[j]) 55 j += 1; 56 57 return j; 58 } 59 60 static inline int ecc_group_index(unsigned int group) 61 { 62 const unsigned int *groups = l_ecc_supported_ike_groups(); 63 int j; 64 65 for (j = 0; groups[j]; j++) 66 if (groups[j] == group) 67 return j; 68 69 return -ENOENT; 70 } 71 72 static bool handshake_get_nonce(uint8_t nonce[]) 73 { 74 return l_getrandom(nonce, 32); 75 } 76 77 static handshake_get_nonce_func_t get_nonce = handshake_get_nonce; 78 static handshake_install_tk_func_t install_tk = NULL; 79 static handshake_install_gtk_func_t install_gtk = NULL; 80 static handshake_install_igtk_func_t install_igtk = NULL; 81 static handshake_install_ext_tk_func_t install_ext_tk = NULL; 82 83 void __handshake_set_get_nonce_func(handshake_get_nonce_func_t func) 84 { 85 get_nonce = func; 86 } 87 88 void __handshake_set_install_tk_func(handshake_install_tk_func_t func) 89 { 90 install_tk = func; 91 } 92 93 void __handshake_set_install_gtk_func(handshake_install_gtk_func_t func) 94 { 95 install_gtk = func; 96 } 97 98 void __handshake_set_install_igtk_func(handshake_install_igtk_func_t func) 99 { 100 install_igtk = func; 101 } 102 103 void __handshake_set_install_ext_tk_func(handshake_install_ext_tk_func_t func) 104 { 105 install_ext_tk = func; 106 } 107 108 struct handshake_state *handshake_state_ref(struct handshake_state *s) 109 { 110 __sync_fetch_and_add(&s->refcount, 1); 111 112 return s; 113 } 114 115 void handshake_state_unref(struct handshake_state *s) 116 { 117 __typeof__(s->free) destroy; 118 119 if (!s) 120 return; 121 122 destroy = s->free; 123 124 if (s->in_event) { 125 s->in_event = false; 126 return; 127 } 128 129 if (__sync_sub_and_fetch(&s->refcount, 1)) 130 return; 131 132 l_free(s->authenticator_ie); 133 l_free(s->supplicant_ie); 134 l_free(s->authenticator_rsnxe); 135 l_free(s->supplicant_rsnxe); 136 l_free(s->mde); 137 l_free(s->authenticator_fte); 138 l_free(s->supplicant_fte); 139 l_free(s->fils_ip_req_ie); 140 l_free(s->fils_ip_resp_ie); 141 l_free(s->vendor_ies); 142 143 if (s->have_pmksa) 144 l_free(s->pmksa); 145 146 if (s->erp_cache) 147 erp_cache_put(s->erp_cache); 148 149 l_free(s->chandef); 150 151 if (s->passphrase) { 152 explicit_bzero(s->passphrase, strlen(s->passphrase)); 153 l_free(s->passphrase); 154 } 155 156 if (s->password_identifier) { 157 explicit_bzero(s->password_identifier, 158 strlen(s->password_identifier)); 159 l_free(s->password_identifier); 160 } 161 162 if (s->ecc_sae_pts) { 163 unsigned int i; 164 165 for (i = 0; i < n_ecc_groups(); i++) 166 l_ecc_point_free(s->ecc_sae_pts[i]); 167 168 l_free(s->ecc_sae_pts); 169 } 170 171 explicit_bzero(s, sizeof(*s)); 172 173 if (destroy) 174 destroy(s); 175 } 176 177 void handshake_state_set_supplicant_address(struct handshake_state *s, 178 const uint8_t *spa) 179 { 180 memcpy(s->spa, spa, sizeof(s->spa)); 181 } 182 183 void handshake_state_set_authenticator_address(struct handshake_state *s, 184 const uint8_t *aa) 185 { 186 memcpy(s->aa, aa, sizeof(s->aa)); 187 } 188 189 void handshake_state_set_authenticator(struct handshake_state *s, bool auth) 190 { 191 s->authenticator = auth; 192 } 193 194 void handshake_state_set_pmk(struct handshake_state *s, const uint8_t *pmk, 195 size_t pmk_len) 196 { 197 memcpy(s->pmk, pmk, pmk_len); 198 s->pmk_len = pmk_len; 199 s->have_pmk = true; 200 } 201 202 void handshake_state_set_ptk(struct handshake_state *s, const uint8_t *ptk, 203 size_t ptk_len) 204 { 205 memcpy(s->ptk, ptk, ptk_len); 206 s->ptk_complete = true; 207 } 208 209 void handshake_state_set_8021x_config(struct handshake_state *s, 210 struct l_settings *settings) 211 { 212 s->settings_8021x = settings; 213 } 214 215 bool handshake_state_set_authenticator_ie(struct handshake_state *s, 216 const uint8_t *ie) 217 { 218 struct ie_rsn_info info; 219 220 if (!ie_parse_rsne_from_data(ie, ie[1] + 2, &info)) 221 goto valid_ie; 222 223 if (!ie_parse_wpa_from_data(ie, ie[1] + 2, &info)) 224 goto valid_ie; 225 226 if (ie_parse_osen_from_data(ie, ie[1] + 2, &info) < 0) 227 return false; 228 229 valid_ie: 230 l_free(s->authenticator_ie); 231 s->authenticator_ie = l_memdup(ie, ie[1] + 2u); 232 233 s->authenticator_ocvc = info.ocvc; 234 235 return true; 236 } 237 238 bool handshake_state_set_supplicant_ie(struct handshake_state *s, 239 const uint8_t *ie) 240 { 241 struct ie_rsn_info info; 242 bool wpa_ie = false; 243 bool osen_ie = false; 244 245 if (!ie_parse_rsne_from_data(ie, ie[1] + 2, &info)) 246 goto valid_ie; 247 248 if (!ie_parse_wpa_from_data(ie, ie[1] + 2, &info)) { 249 wpa_ie = true; 250 goto valid_ie; 251 } 252 253 if (ie_parse_osen_from_data(ie, ie[1] + 2, &info) < 0) 254 return false; 255 256 osen_ie = true; 257 258 valid_ie: 259 if (__builtin_popcount(info.pairwise_ciphers) != 1) 260 return false; 261 262 if (__builtin_popcount(info.akm_suites) != 1) 263 return false; 264 265 l_free(s->supplicant_ie); 266 s->supplicant_ie = l_memdup(ie, ie[1] + 2u); 267 268 s->osen_ie = osen_ie; 269 s->wpa_ie = wpa_ie; 270 271 s->pairwise_cipher = info.pairwise_ciphers; 272 s->group_cipher = info.group_cipher; 273 s->group_management_cipher = info.group_management_cipher; 274 s->akm_suite = info.akm_suites; 275 s->supplicant_ocvc = info.ocvc; 276 s->ext_key_id_capable = info.extended_key_id; 277 278 /* 279 * Don't set MFP for OSEN otherwise EAPoL will attempt to negotiate a 280 * iGTK which is not allowed for OSEN. 281 */ 282 if (!s->osen_ie) 283 s->mfp = info.mfpc; 284 285 return true; 286 } 287 288 static void replace_ie(uint8_t **old, const uint8_t *new) 289 { 290 if (*old == NULL) { 291 *old = new ? l_memdup(new, new[1] + 2) : NULL; 292 return; 293 } 294 295 if (!new) { 296 l_free(*old); 297 *old = NULL; 298 return; 299 } 300 301 if ((*old)[1] == new[1] && !memcmp(*old, new, new[1] + 2)) 302 return; 303 304 l_free(*old); 305 *old = l_memdup(new, new[1] + 2); 306 } 307 308 void handshake_state_set_authenticator_rsnxe(struct handshake_state *s, 309 const uint8_t *ie) 310 { 311 l_free(s->authenticator_rsnxe); 312 s->authenticator_rsnxe = ie ? l_memdup(ie, ie[1] + 2) : NULL; 313 } 314 315 void handshake_state_set_supplicant_rsnxe(struct handshake_state *s, 316 const uint8_t *ie) 317 { 318 replace_ie(&s->supplicant_rsnxe, ie); 319 } 320 321 void handshake_state_set_ssid(struct handshake_state *s, const uint8_t *ssid, 322 size_t ssid_len) 323 { 324 memcpy(s->ssid, ssid, ssid_len); 325 s->ssid_len = ssid_len; 326 } 327 328 void handshake_state_set_mde(struct handshake_state *s, const uint8_t *mde) 329 { 330 replace_ie(&s->mde, mde); 331 } 332 333 void handshake_state_set_authenticator_fte(struct handshake_state *s, 334 const uint8_t *fte) 335 { 336 replace_ie(&s->authenticator_fte, fte); 337 } 338 339 void handshake_state_set_supplicant_fte(struct handshake_state *s, 340 const uint8_t *fte) 341 { 342 replace_ie(&s->supplicant_fte, fte); 343 } 344 345 void handshake_state_set_vendor_ies(struct handshake_state *s, 346 const struct iovec *iov, 347 size_t n_iovs) 348 { 349 size_t i; 350 size_t len; 351 352 l_free(s->vendor_ies); 353 s->vendor_ies = NULL; 354 355 if (n_iovs == 0) { 356 s->vendor_ies_len = 0; 357 return; 358 } 359 360 for (i = 0, len = 0; i < n_iovs; i++) 361 len += iov[i].iov_len; 362 363 s->vendor_ies_len = len; 364 s->vendor_ies = l_malloc(len); 365 366 for (i = 0, len = 0; i < n_iovs; i++) { 367 memcpy(s->vendor_ies + len, iov[i].iov_base, iov[i].iov_len); 368 len += iov[i].iov_len; 369 } 370 } 371 372 void handshake_state_set_vendor_quirks(struct handshake_state *s, 373 struct vendor_quirk quirks) 374 { 375 s->vendor_quirks = quirks; 376 } 377 378 void handshake_state_set_kh_ids(struct handshake_state *s, 379 const uint8_t *r0khid, size_t r0khid_len, 380 const uint8_t *r1khid) 381 { 382 memcpy(s->r0khid, r0khid, r0khid_len); 383 s->r0khid_len = r0khid_len; 384 385 memcpy(s->r1khid, r1khid, 6); 386 } 387 388 void handshake_state_set_event_func(struct handshake_state *s, 389 handshake_event_func_t func, 390 void *user_data) 391 { 392 s->event_func = func; 393 s->user_data = user_data; 394 } 395 396 void handshake_state_set_passphrase(struct handshake_state *s, 397 const char *passphrase) 398 { 399 s->passphrase = l_strdup(passphrase); 400 } 401 402 void handshake_state_set_password_identifier(struct handshake_state *s, 403 const char *id) 404 { 405 s->password_identifier = l_strdup(id); 406 } 407 408 void handshake_state_set_no_rekey(struct handshake_state *s, bool no_rekey) 409 { 410 s->no_rekey = no_rekey; 411 } 412 413 void handshake_state_set_fils_ft(struct handshake_state *s, 414 const uint8_t *fils_ft, 415 size_t fils_ft_len) 416 { 417 memcpy(s->fils_ft, fils_ft, fils_ft_len); 418 s->fils_ft_len = fils_ft_len; 419 } 420 421 /* 422 * Override the protocol version used for EAPoL packets. The selection is as 423 * follows: 424 * 0 -> Automatic, use same proto as the request for the response and 425 * 2004 when in authenticator mode 426 * 1 -> Chooses 2001 Protocol Version 427 * 2 -> Chooses 2004 Protocol Version 428 * 3 -> Chooses 2010 Protocol Version 429 */ 430 void handshake_state_set_protocol_version(struct handshake_state *s, 431 uint8_t proto_version) 432 { 433 s->proto_version = proto_version; 434 } 435 436 void handshake_state_new_snonce(struct handshake_state *s) 437 { 438 get_nonce(s->snonce); 439 440 s->have_snonce = true; 441 } 442 443 void handshake_state_new_anonce(struct handshake_state *s) 444 { 445 get_nonce(s->anonce); 446 447 s->have_anonce = true; 448 } 449 450 void handshake_state_set_anonce(struct handshake_state *s, 451 const uint8_t *anonce) 452 { 453 memcpy(s->anonce, anonce, 32); 454 } 455 456 /* A multi-purpose getter for key sizes */ 457 static bool handshake_get_key_sizes(struct handshake_state *s, size_t *ptk_size, 458 size_t *kck_size, size_t *kek_size) 459 { 460 size_t kck; 461 size_t kek; 462 size_t tk; 463 enum crypto_cipher cipher = 464 ie_rsn_cipher_suite_to_cipher(s->pairwise_cipher); 465 466 tk = crypto_cipher_key_len(cipher); 467 468 /* 469 * IEEE 802.11-2016 Table 12-8: Integrity and key-wrap algorithms 470 * 471 * From the table, only 00-0F-AC:12 and 00-0F-AC:13 use longer KCK and 472 * KEK keys, which are 24 and 32 bytes respectively. The remainder use 473 * 16 and 16 respectively. 474 */ 475 switch (s->akm_suite) { 476 case IE_RSN_AKM_SUITE_8021X_SUITE_B_SHA256: 477 case IE_RSN_AKM_SUITE_FT_OVER_8021X_SHA384: 478 kck = 24; 479 kek = 32; 480 break; 481 case IE_RSN_AKM_SUITE_OWE: 482 /* 483 * RFC 8110 Section 4.4 Table 2 484 * 485 * Luckily with OWE we can deduce the key lengths from the PMK 486 * size, since the PMK size maps to unique KCK/KEK lengths. 487 */ 488 switch (s->pmk_len) { 489 case 32: 490 /* SHA-256 used for PMK */ 491 kck = 16; 492 kek = 16; 493 break; 494 case 48: 495 /* SHA-384 used for PMK */ 496 kck = 24; 497 kek = 32; 498 break; 499 case 64: 500 /* SHA-512 used for PMK */ 501 kck = 32; 502 kek = 32; 503 break; 504 default: 505 l_error("Invalid PMK length for OWE %zu\n", s->pmk_len); 506 return false; 507 } 508 509 break; 510 case IE_RSN_AKM_SUITE_FILS_SHA256: 511 case IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA256: 512 kck = 0; 513 kek = 32; 514 break; 515 case IE_RSN_AKM_SUITE_FILS_SHA384: 516 case IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA384: 517 kck = 0; 518 kek = 64; 519 break; 520 default: 521 kck = 16; 522 kek = 16; 523 break; 524 } 525 526 if (ptk_size) { 527 *ptk_size = kck + kek + tk; 528 if (s->akm_suite == IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA256) 529 *ptk_size += 32; 530 else if (s->akm_suite == IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA384) 531 *ptk_size += 56; 532 } 533 534 if (kck_size) 535 *kck_size = kck; 536 537 if (kek_size) 538 *kek_size = kek; 539 540 return true; 541 } 542 543 bool handshake_state_derive_ptk(struct handshake_state *s) 544 { 545 size_t ptk_size; 546 enum l_checksum_type type; 547 548 if (!(s->akm_suite & (IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA256 | 549 IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA384))) 550 if (!s->have_snonce || !s->have_pmk) 551 return false; 552 553 if ((s->akm_suite & (IE_RSN_AKM_SUITE_FT_OVER_8021X | 554 IE_RSN_AKM_SUITE_FT_USING_PSK | 555 IE_RSN_AKM_SUITE_FT_OVER_SAE_SHA256 | 556 IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA256 | 557 IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA384)) && 558 (!s->mde || !s->authenticator_fte)) 559 return false; 560 561 s->ptk_complete = false; 562 563 if (s->akm_suite & IE_RSN_AKM_SUITE_OWE) { 564 if (s->pmk_len == 32) 565 type = L_CHECKSUM_SHA256; 566 else if (s->pmk_len == 48) 567 type = L_CHECKSUM_SHA384; 568 else if (s->pmk_len == 64) 569 type = L_CHECKSUM_SHA512; 570 else 571 return false; 572 } else if (s->akm_suite & (IE_RSN_AKM_SUITE_FILS_SHA384 | 573 IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA384)) 574 type = L_CHECKSUM_SHA384; 575 else if (s->akm_suite & (IE_RSN_AKM_SUITE_8021X_SHA256 | 576 IE_RSN_AKM_SUITE_PSK_SHA256 | 577 IE_RSN_AKM_SUITE_SAE_SHA256 | 578 IE_RSN_AKM_SUITE_FT_OVER_SAE_SHA256 | 579 IE_RSN_AKM_SUITE_FILS_SHA256 | 580 IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA256 | 581 IE_RSN_AKM_SUITE_OSEN)) 582 type = L_CHECKSUM_SHA256; 583 else 584 type = L_CHECKSUM_SHA1; 585 586 ptk_size = handshake_state_get_ptk_size(s); 587 588 if (s->akm_suite & (IE_RSN_AKM_SUITE_FT_OVER_8021X | 589 IE_RSN_AKM_SUITE_FT_USING_PSK | 590 IE_RSN_AKM_SUITE_FT_OVER_SAE_SHA256 | 591 IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA256 | 592 IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA384)) { 593 uint16_t mdid; 594 uint8_t ptk_name[16]; 595 const uint8_t *xxkey = s->pmk; 596 size_t xxkey_len = 32; 597 bool sha384 = (s->akm_suite & 598 IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA384); 599 600 /* 601 * In a Fast Transition initial mobility domain association 602 * the PMK maps to the XXKey, except with EAP: 603 * 802.11-2016 12.7.1.7.3: 604 * "If the AKM negotiated is 00-0F-AC:3, then [...] XXKey 605 * shall be the second 256 bits of the MSK (which is 606 * derived from the IEEE 802.1X authentication), i.e., 607 * XXKey = L(MSK, 256, 256)." 608 */ 609 if (s->akm_suite == IE_RSN_AKM_SUITE_FT_OVER_8021X) 610 xxkey = s->pmk + 32; 611 else if (s->akm_suite & (IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA256 | 612 IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA384)) { 613 xxkey = s->fils_ft; 614 xxkey_len = s->fils_ft_len; 615 } 616 617 ie_parse_mobility_domain_from_data(s->mde, s->mde[1] + 2, 618 &mdid, NULL, NULL); 619 620 if (!crypto_derive_pmk_r0(xxkey, xxkey_len, s->ssid, 621 s->ssid_len, mdid, 622 s->r0khid, s->r0khid_len, 623 s->spa, sha384, 624 s->pmk_r0, s->pmk_r0_name)) 625 return false; 626 627 if (!crypto_derive_pmk_r1(s->pmk_r0, s->r1khid, s->spa, 628 s->pmk_r0_name, sha384, 629 s->pmk_r1, s->pmk_r1_name)) 630 return false; 631 632 if (!crypto_derive_ft_ptk(s->pmk_r1, s->pmk_r1_name, s->aa, 633 s->spa, s->snonce, s->anonce, 634 sha384, s->ptk, ptk_size, 635 ptk_name)) 636 return false; 637 } else 638 if (!crypto_derive_pairwise_ptk(s->pmk, s->pmk_len, s->spa, 639 s->aa, s->anonce, s->snonce, 640 s->ptk, ptk_size, type)) 641 return false; 642 643 return true; 644 } 645 646 size_t handshake_state_get_ptk_size(struct handshake_state *s) 647 { 648 size_t ptk_size; 649 650 if (!handshake_get_key_sizes(s, &ptk_size, NULL, NULL)) 651 return 0; 652 653 return ptk_size; 654 } 655 656 const uint8_t *handshake_state_get_kck(struct handshake_state *s) 657 { 658 /* 659 * FILS itself does not derive a KCK, but FILS-FT derives additional 660 * key bytes at the end of the PTK, which contains a special KCK used 661 * for fast transition. Since the normal FILS protocol will never call 662 * this, we can assume that its only being called for FILS-FT and is 663 * requesting this special KCK. 664 */ 665 if (s->akm_suite & IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA256) 666 return s->ptk + 48; 667 else if (s->akm_suite & IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA384) 668 return s->ptk + 80; 669 670 return s->ptk; 671 } 672 673 size_t handshake_state_get_kck_len(struct handshake_state *s) 674 { 675 if (s->akm_suite & IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA384) 676 return 24; 677 678 return 16; 679 } 680 681 size_t handshake_state_get_kek_len(struct handshake_state *s) 682 { 683 size_t kek_size; 684 685 if (!handshake_get_key_sizes(s, NULL, NULL, &kek_size)) 686 return 0; 687 688 return kek_size; 689 } 690 691 const uint8_t *handshake_state_get_kek(struct handshake_state *s) 692 { 693 size_t kck_size; 694 695 if (!handshake_get_key_sizes(s, NULL, &kck_size, NULL)) 696 return NULL; 697 698 return s->ptk + kck_size; 699 } 700 701 static const uint8_t *handshake_get_tk(struct handshake_state *s) 702 { 703 size_t kck_size, kek_size; 704 705 if (!handshake_get_key_sizes(s, NULL, &kck_size, &kek_size)) 706 return NULL; 707 708 return s->ptk + kck_size + kek_size; 709 } 710 711 void handshake_state_install_ptk(struct handshake_state *s) 712 { 713 s->ptk_complete = true; 714 715 if (!s->have_pmksa && IE_AKM_IS_SAE(s->akm_suite)) { 716 l_debug("Adding PMKSA expiration"); 717 s->expiration = l_time_now() + pmksa_lifetime(); 718 } 719 720 if (install_tk) { 721 uint32_t cipher = ie_rsn_cipher_suite_to_cipher( 722 s->pairwise_cipher); 723 724 handshake_event(s, HANDSHAKE_EVENT_SETTING_KEYS); 725 726 install_tk(s, s->active_tk_index, handshake_get_tk(s), cipher); 727 } 728 } 729 730 void handshake_state_install_ext_ptk(struct handshake_state *s, 731 uint8_t key_idx, 732 struct eapol_frame *ek, uint16_t proto, 733 bool noencrypt) 734 { 735 s->ptk_complete = true; 736 737 if (install_ext_tk) { 738 uint32_t cipher = 739 ie_rsn_cipher_suite_to_cipher(s->pairwise_cipher); 740 741 install_ext_tk(s, key_idx, handshake_get_tk(s), cipher, ek, 742 proto, noencrypt); 743 } 744 } 745 746 747 void handshake_state_install_gtk(struct handshake_state *s, 748 uint16_t gtk_key_index, 749 const uint8_t *gtk, size_t gtk_len, 750 const uint8_t *rsc, uint8_t rsc_len) 751 { 752 if (install_gtk) { 753 uint32_t cipher = 754 ie_rsn_cipher_suite_to_cipher(s->group_cipher); 755 756 install_gtk(s, gtk_key_index, gtk, gtk_len, 757 rsc, rsc_len, cipher); 758 } 759 } 760 761 void handshake_state_install_igtk(struct handshake_state *s, 762 uint16_t igtk_key_index, 763 const uint8_t *igtk, size_t igtk_len, 764 const uint8_t *ipn) 765 { 766 if (install_igtk) { 767 uint32_t cipher = 768 ie_rsn_cipher_suite_to_cipher( 769 s->group_management_cipher); 770 771 install_igtk(s, igtk_key_index, igtk, igtk_len, 772 ipn, 6, cipher); 773 } 774 } 775 776 void handshake_state_override_pairwise_cipher(struct handshake_state *s, 777 enum ie_rsn_cipher_suite pairwise) 778 { 779 s->pairwise_cipher = pairwise; 780 } 781 782 void handshake_state_set_pmkid(struct handshake_state *s, const uint8_t *pmkid) 783 { 784 memcpy(s->pmkid, pmkid, 16); 785 s->have_pmkid = true; 786 } 787 788 bool handshake_state_get_pmkid(struct handshake_state *s, uint8_t *out_pmkid, 789 enum l_checksum_type sha) 790 { 791 /* SAE exports pmkid */ 792 if (s->have_pmkid) { 793 memcpy(out_pmkid, s->pmkid, 16); 794 return true; 795 } 796 797 if (!s->have_pmk) 798 return false; 799 800 return crypto_derive_pmkid(s->pmk, 32, s->spa, s->aa, out_pmkid, 801 sha); 802 } 803 804 bool handshake_state_pmkid_matches(struct handshake_state *s, 805 const uint8_t *check) 806 { 807 uint8_t own_pmkid[16]; 808 enum l_checksum_type sha; 809 810 /* 811 * 802.11-2020 Table 9-151 defines the hashing algorithm to use 812 * for various AKM's. Note some AKMs are omitted here because they 813 * export the PMKID individually (SAE/FILS/FT-PSK) 814 * 815 * SHA1: 816 * 00-0F-AC:1 (8021X) 817 * 00-0F-AC:2 (PSK) 818 * 819 * SHA256: 820 * 00-0F-AC:3 (FT-8021X) 821 * 00-0F-AC:5 (8021X-SHA256) 822 * 00-0F-AC:6 (PSK-SHA256) 823 * 824 * SHA384: 825 * 00-0F-AC:13 (FT-8021X-SHA384) 826 */ 827 if (s->akm_suite & (IE_RSN_AKM_SUITE_8021X_SHA256 | 828 IE_RSN_AKM_SUITE_PSK_SHA256 | 829 IE_RSN_AKM_SUITE_FT_OVER_8021X)) 830 sha = L_CHECKSUM_SHA256; 831 else 832 sha = L_CHECKSUM_SHA1; 833 834 if (!handshake_state_get_pmkid(s, own_pmkid, sha)) 835 return false; 836 837 if (l_secure_memcmp(own_pmkid, check, 16)) { 838 if (s->akm_suite != IE_RSN_AKM_SUITE_FT_OVER_8021X) 839 return false; 840 841 l_debug("PMKID did not match, trying SHA1 derivation"); 842 843 if (!handshake_state_get_pmkid(s, own_pmkid, L_CHECKSUM_SHA1)) 844 return false; 845 846 return l_secure_memcmp(own_pmkid, check, 16) == 0; 847 } 848 849 return true; 850 } 851 852 void handshake_state_set_gtk(struct handshake_state *s, const uint8_t *key, 853 unsigned int key_index, const uint8_t *rsc) 854 { 855 enum crypto_cipher cipher = 856 ie_rsn_cipher_suite_to_cipher(s->group_cipher); 857 int key_len = crypto_cipher_key_len(cipher); 858 859 if (!key_len) 860 return; 861 862 memcpy(s->gtk, key, key_len); 863 s->gtk_index = key_index; 864 memcpy(s->gtk_rsc, rsc, 6); 865 } 866 867 void handshake_state_set_igtk(struct handshake_state *s, const uint8_t *key, 868 unsigned int key_index, const uint8_t *rsc) 869 { 870 enum crypto_cipher cipher = 871 ie_rsn_cipher_suite_to_cipher(s->group_management_cipher); 872 int key_len = crypto_cipher_key_len(cipher); 873 874 if (!key_len) 875 return; 876 877 memcpy(s->igtk, key, key_len); 878 s->igtk_index = key_index; 879 memcpy(s->igtk_rsc, rsc, 6); 880 } 881 882 /* 883 * This function performs a match of the RSN/WPA IE obtained from the scan 884 * results vs the RSN/WPA IE obtained as part of the 4-way handshake. If they 885 * don't match, the EAPoL packet must be silently discarded. 886 */ 887 bool handshake_util_ap_ie_matches(struct handshake_state *s, 888 const struct ie_rsn_info *msg_info, 889 const uint8_t *scan_ie, bool is_wpa) 890 { 891 struct ie_rsn_info scan_info; 892 int r; 893 894 if (!is_wpa) 895 r = ie_parse_rsne_from_data(scan_ie, 896 scan_ie[1] + 2, &scan_info); 897 else 898 r = ie_parse_wpa_from_data(scan_ie, scan_ie[1] + 2, &scan_info); 899 900 if (r < 0) 901 return false; 902 903 if (msg_info->group_cipher != scan_info.group_cipher) 904 return false; 905 906 if (msg_info->pairwise_ciphers != scan_info.pairwise_ciphers) 907 return false; 908 909 if (msg_info->akm_suites != scan_info.akm_suites) 910 return false; 911 912 if (msg_info->preauthentication != scan_info.preauthentication) 913 return false; 914 915 if (msg_info->no_pairwise != scan_info.no_pairwise) 916 return false; 917 918 if (!(s->vendor_quirks.replay_counter_mismatch)) { 919 if (msg_info->ptksa_replay_counter != 920 scan_info.ptksa_replay_counter) 921 return false; 922 923 if (msg_info->gtksa_replay_counter != 924 scan_info.gtksa_replay_counter) 925 return false; 926 } 927 928 if (msg_info->mfpr != scan_info.mfpr) 929 return false; 930 931 if (msg_info->mfpc != scan_info.mfpc) 932 return false; 933 934 if (msg_info->peerkey_enabled != scan_info.peerkey_enabled) 935 return false; 936 937 if (msg_info->spp_a_msdu_capable != scan_info.spp_a_msdu_capable) 938 return false; 939 940 if (msg_info->spp_a_msdu_required != scan_info.spp_a_msdu_required) 941 return false; 942 943 if (msg_info->pbac != scan_info.pbac) 944 return false; 945 946 if (msg_info->extended_key_id != scan_info.extended_key_id) 947 return false; 948 949 if (msg_info->ocvc != scan_info.ocvc) 950 return false; 951 952 /* We don't check the PMKIDs since these might actually be different */ 953 954 if (msg_info->group_management_cipher != 955 scan_info.group_management_cipher) 956 return false; 957 958 return true; 959 } 960 961 const uint8_t *handshake_util_find_kde(enum handshake_kde selector, 962 const uint8_t *data, size_t data_len, 963 size_t *out_kde_len) 964 { 965 struct ie_tlv_iter iter; 966 const uint8_t *result; 967 unsigned int len; 968 969 ie_tlv_iter_init(&iter, data, data_len); 970 971 while (ie_tlv_iter_next(&iter)) { 972 if (ie_tlv_iter_get_tag(&iter) != IE_TYPE_VENDOR_SPECIFIC) 973 continue; 974 975 len = ie_tlv_iter_get_length(&iter); 976 if (len < 4) /* Take care of padding */ 977 return NULL; 978 979 /* Check OUI */ 980 result = ie_tlv_iter_get_data(&iter); 981 if (l_get_be32(result) != selector) 982 continue; 983 984 if (out_kde_len) 985 *out_kde_len = len - 4; 986 987 return result + 4; 988 } 989 990 return NULL; 991 } 992 993 const uint8_t *handshake_util_find_gtk_kde(const uint8_t *data, size_t data_len, 994 size_t *out_gtk_len) 995 { 996 size_t gtk_len; 997 const uint8_t *gtk = handshake_util_find_kde(HANDSHAKE_KDE_GTK, 998 data, data_len, >k_len); 999 1000 if (!gtk) 1001 return NULL; 1002 1003 /* 1004 * Account for KeyId, TX and Reserved octet 1005 * See 802.11-2016, Figure 12-35 1006 */ 1007 if (gtk_len < CRYPTO_MIN_GTK_LEN + 2) 1008 return NULL; 1009 1010 if (gtk_len > CRYPTO_MAX_GTK_LEN + 2) 1011 return NULL; 1012 1013 if (out_gtk_len) 1014 *out_gtk_len = gtk_len; 1015 1016 return gtk; 1017 } 1018 1019 const uint8_t *handshake_util_find_igtk_kde(const uint8_t *data, 1020 size_t data_len, 1021 size_t *out_igtk_len) 1022 { 1023 size_t igtk_len; 1024 const uint8_t *igtk = handshake_util_find_kde(HANDSHAKE_KDE_IGTK, 1025 data, data_len, &igtk_len); 1026 1027 if (!igtk) 1028 return NULL; 1029 1030 /* 1031 * Account for KeyId and IPN 1032 * See 802.11-2016, Figure 12-42 1033 */ 1034 if (igtk_len < CRYPTO_MIN_IGTK_LEN + 8) 1035 return NULL; 1036 1037 if (igtk_len > CRYPTO_MAX_IGTK_LEN + 8) 1038 return NULL; 1039 1040 if (out_igtk_len) 1041 *out_igtk_len = igtk_len; 1042 1043 return igtk; 1044 } 1045 1046 const uint8_t *handshake_util_find_pmkid_kde(const uint8_t *data, 1047 size_t data_len) 1048 { 1049 const uint8_t *pmkid; 1050 size_t pmkid_len; 1051 1052 pmkid = handshake_util_find_kde(HANDSHAKE_KDE_PMKID, data, data_len, 1053 &pmkid_len); 1054 1055 if (pmkid && pmkid_len != 16) 1056 return NULL; 1057 1058 return pmkid; 1059 } 1060 1061 /* Defined in 802.11-2016 12.7.2 j), Figure 12-34 */ 1062 void handshake_util_build_gtk_kde(enum crypto_cipher cipher, const uint8_t *key, 1063 unsigned int key_index, uint8_t *to) 1064 { 1065 size_t key_len = crypto_cipher_key_len(cipher); 1066 1067 *to++ = IE_TYPE_VENDOR_SPECIFIC; 1068 *to++ = 6 + key_len; 1069 l_put_be32(HANDSHAKE_KDE_GTK, to); 1070 to += 4; 1071 *to++ = key_index; 1072 *to++ = 0; 1073 memcpy(to, key, key_len); 1074 } 1075 1076 void handshake_util_build_igtk_kde(enum crypto_cipher cipher, const uint8_t *key, 1077 unsigned int key_index, uint8_t *to) 1078 { 1079 size_t key_len = crypto_cipher_key_len(cipher); 1080 1081 *to++ = IE_TYPE_VENDOR_SPECIFIC; 1082 *to++ = 12 + key_len; 1083 l_put_be32(HANDSHAKE_KDE_IGTK, to); 1084 to += 4; 1085 *to++ = key_index; 1086 *to++ = 0; 1087 1088 /** Initialize PN to zero **/ 1089 memset(to, 0, 6); 1090 to += 6; 1091 1092 memcpy(to, key, key_len); 1093 } 1094 1095 static const uint8_t *handshake_state_get_ft_fils_kek(struct handshake_state *s, 1096 size_t *len) 1097 { 1098 if (s->akm_suite & IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA256) { 1099 if (len) 1100 *len = 16; 1101 1102 return s->ptk + 64; 1103 } else if (s->akm_suite & IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA384) { 1104 if (len) 1105 *len = 32; 1106 1107 return s->ptk + 104; 1108 } 1109 1110 return NULL; 1111 } 1112 1113 /* 1114 * Unwrap a GTK / IGTK included in an FTE following 802.11-2012, Section 12.8.5: 1115 * 1116 * "If a GTK or an IGTK are included, the Key field of the subelement shall be 1117 * encrypted using KEK and the NIST AES key wrap algorithm. The Key field shall 1118 * be padded before encrypting if the key length is less than 16 octets or if 1119 * it is not a multiple of 8. The padding consists of appending a single octet 1120 * 0xdd followed by zero or more 0x00 octets. When processing a received 1121 * message, the receiver shall ignore this trailing padding. Addition of 1122 * padding does not change the value of the Key Length field. Note that the 1123 * length of the encrypted Key field can be determined from the length of the 1124 * GTK or IGTK subelement. 1125 */ 1126 bool handshake_decode_fte_key(struct handshake_state *s, const uint8_t *wrapped, 1127 size_t key_len, uint8_t *key_out) 1128 { 1129 const uint8_t *kek; 1130 size_t kek_len = handshake_state_get_kek_len(s); 1131 size_t padded_len = key_len < 16 ? 16 : align_len(key_len, 8); 1132 1133 if (s->akm_suite & (IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA256 | 1134 IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA384)) 1135 kek = handshake_state_get_ft_fils_kek(s, &kek_len); 1136 else 1137 kek = handshake_state_get_kek(s); 1138 1139 if (!aes_unwrap(kek, kek_len, wrapped, padded_len + 8, key_out)) 1140 return false; 1141 1142 if (key_len < padded_len && key_out[key_len++] != 0xdd) 1143 return false; 1144 1145 while (key_len < padded_len) 1146 if (key_out[key_len++] != 0x00) 1147 return false; 1148 1149 return true; 1150 } 1151 1152 /* Add SAE-PT for ECC groups. The group is carried by the point itself */ 1153 bool handshake_state_add_ecc_sae_pt(struct handshake_state *s, 1154 const struct l_ecc_point *pt) 1155 { 1156 const struct l_ecc_curve *curve; 1157 int i; 1158 1159 if (!pt) 1160 return false; 1161 1162 curve = l_ecc_point_get_curve(pt); 1163 1164 if (!s->ecc_sae_pts) 1165 s->ecc_sae_pts = l_new(struct l_ecc_point *, n_ecc_groups()); 1166 1167 if ((i = ecc_group_index(l_ecc_curve_get_ike_group(curve))) < 0) 1168 return false; 1169 1170 if (s->ecc_sae_pts[i]) 1171 return false; 1172 1173 s->ecc_sae_pts[i] = l_ecc_point_clone(pt); 1174 return true; 1175 } 1176 1177 void handshake_state_set_chandef(struct handshake_state *s, 1178 struct band_chandef *chandef) 1179 { 1180 if (s->chandef) 1181 l_free(s->chandef); 1182 1183 s->chandef = chandef; 1184 } 1185 1186 int handshake_state_verify_oci(struct handshake_state *s, const uint8_t *oci, 1187 size_t oci_len) 1188 { 1189 int r = -ENOENT; 1190 bool ocvc; 1191 1192 l_debug("oci_len: %zu", oci ? oci_len : 0); 1193 1194 if (!oci) 1195 goto done; 1196 1197 r = -EBADMSG; 1198 if (oci_len != 3) 1199 goto done; 1200 1201 l_debug("operating_class: %hu", oci[0]); 1202 l_debug("primary_channel_number: %hu", oci[1]); 1203 l_debug("frequency segment 1 channel number: %hu", oci[2]); 1204 1205 r = -EINVAL; 1206 1207 if (!s->chandef) { 1208 l_debug("Own chandef unavailable"); 1209 goto done; 1210 } 1211 1212 r = oci_verify(oci, s->chandef); 1213 if (r < 0) 1214 l_debug("OCI verification failed: %s", strerror(-r)); 1215 1216 done: 1217 if (!r) 1218 return r; 1219 1220 /* Only enforce validation if we're configured to do so */ 1221 ocvc = s->authenticator ? s->authenticator_ocvc : s->supplicant_ocvc; 1222 if (!ocvc) 1223 r = 0; 1224 1225 return r; 1226 } 1227 1228 bool handshake_state_set_pmksa(struct handshake_state *s, 1229 struct pmksa *pmksa) 1230 { 1231 /* checks for both expiration || pmksa being set */ 1232 if (s->expiration) 1233 return false; 1234 1235 s->pmksa = pmksa; 1236 s->have_pmksa = true; 1237 1238 handshake_state_set_pmkid(s, pmksa->pmkid); 1239 handshake_state_set_pmk(s, pmksa->pmk, pmksa->pmk_len); 1240 1241 return true; 1242 } 1243 1244 static struct pmksa *handshake_state_steal_pmksa(struct handshake_state *s) 1245 { 1246 struct pmksa *pmksa; 1247 uint64_t now = l_time_now(); 1248 1249 if (s->have_pmksa) { 1250 pmksa = l_steal_ptr(s->pmksa); 1251 s->have_pmksa = false; 1252 1253 if (l_time_after(now, pmksa->expiration)) { 1254 pmksa_cache_free(pmksa); 1255 pmksa = NULL; 1256 } 1257 1258 return pmksa; 1259 } 1260 1261 if (s->expiration && l_time_after(now, s->expiration)) { 1262 s->expiration = 0; 1263 return NULL; 1264 } 1265 1266 if (!s->have_pmkid) 1267 return NULL; 1268 1269 pmksa = l_new(struct pmksa, 1); 1270 pmksa->expiration = s->expiration; 1271 s->expiration = 0; 1272 memcpy(pmksa->spa, s->spa, sizeof(s->spa)); 1273 memcpy(pmksa->aa, s->aa, sizeof(s->aa)); 1274 memcpy(pmksa->ssid, s->ssid, s->ssid_len); 1275 pmksa->ssid_len = s->ssid_len; 1276 pmksa->akm = s->akm_suite; 1277 memcpy(pmksa->pmkid, s->pmkid, sizeof(s->pmkid)); 1278 pmksa->pmk_len = s->pmk_len; 1279 memcpy(pmksa->pmk, s->pmk, s->pmk_len); 1280 1281 return pmksa; 1282 } 1283 1284 void handshake_state_cache_pmksa(struct handshake_state *s) 1285 { 1286 struct pmksa *pmksa = handshake_state_steal_pmksa(s); 1287 1288 if (!pmksa) { 1289 l_debug("No PMKSA for "MAC, MAC_STR(s->aa)); 1290 return; 1291 } 1292 1293 l_debug("Caching PMKSA for "MAC, MAC_STR(s->aa)); 1294 1295 if (L_WARN_ON(pmksa_cache_put(pmksa) < 0)) 1296 pmksa_cache_free(pmksa); 1297 } 1298 1299 bool handshake_state_remove_pmksa(struct handshake_state *s) 1300 { 1301 struct pmksa *pmksa; 1302 1303 if (!s->have_pmksa) 1304 return false; 1305 1306 pmksa = handshake_state_steal_pmksa(s); 1307 if (!pmksa) 1308 return false; 1309 1310 pmksa_cache_free(pmksa); 1311 1312 return true; 1313 }