fils.c (15920B)
1 2 /* 3 * 4 * Wireless daemon for Linux 5 * 6 * Copyright (C) 2019 Intel Corporation. All rights reserved. 7 * 8 * This library is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU Lesser General Public 10 * License as published by the Free Software Foundation; either 11 * version 2.1 of the License, or (at your option) any later version. 12 * 13 * This library is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * Lesser General Public License for more details. 17 * 18 * You should have received a copy of the GNU Lesser General Public 19 * License along with this library; if not, write to the Free Software 20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 21 * 22 */ 23 24 #ifdef HAVE_CONFIG_H 25 #include <config.h> 26 #endif 27 28 #define _GNU_SOURCE 29 #include <stdlib.h> 30 #include <ell/ell.h> 31 32 #include "ell/useful.h" 33 #include "src/ie.h" 34 #include "src/fils.h" 35 #include "src/handshake.h" 36 #include "src/mpdu.h" 37 #include "src/crypto.h" 38 #include "src/util.h" 39 #include "src/missing.h" 40 #include "src/erp.h" 41 #include "src/auth-proto.h" 42 #include "src/band.h" 43 44 #define FILS_NONCE_LEN 16 45 #define FILS_SESSION_LEN 8 46 47 struct fils_sm { 48 struct auth_proto ap; 49 struct erp_state *erp; 50 struct handshake_state *hs; 51 void *user_data; 52 53 fils_tx_authenticate_func_t auth; 54 fils_tx_associate_func_t assoc; 55 fils_get_oci_func_t get_oci; 56 57 uint8_t nonce[FILS_NONCE_LEN]; 58 uint8_t anonce[FILS_NONCE_LEN]; 59 uint8_t session[FILS_SESSION_LEN]; 60 61 uint8_t ick[48]; 62 size_t ick_len; 63 uint8_t kek_and_tk[64 + 16]; 64 size_t kek_len; 65 uint8_t pmk[48]; 66 size_t pmk_len; 67 uint8_t pmkid[16]; 68 69 uint8_t fils_ft[48]; 70 size_t fils_ft_len; 71 }; 72 73 static void fils_derive_pmkid(struct fils_sm *fils, const uint8_t *erp_data, 74 size_t len) 75 { 76 struct l_checksum *sha; 77 enum l_checksum_type type; 78 79 if (fils->hs->akm_suite & (IE_RSN_AKM_SUITE_FILS_SHA256 | 80 IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA256)) 81 type = L_CHECKSUM_SHA256; 82 else 83 type = L_CHECKSUM_SHA384; 84 85 sha = l_checksum_new(type); 86 l_checksum_update(sha, erp_data, len); 87 l_checksum_get_digest(sha, fils->pmkid, sizeof(fils->pmkid)); 88 l_checksum_free(sha); 89 } 90 91 static void fils_erp_tx_func(const uint8_t *eap_data, size_t len, 92 void *user_data) 93 { 94 struct fils_sm *fils = user_data; 95 struct ie_tlv_builder builder; 96 uint8_t data[256]; 97 uint8_t *ptr = data; 98 struct ie_rsn_info rsn_info; 99 uint8_t *rsne; 100 101 l_getrandom(fils->nonce, 16); 102 l_getrandom(fils->session, 8); 103 104 fils_derive_pmkid(fils, eap_data, len); 105 106 /* transaction */ 107 l_put_le16(1, ptr); 108 ptr += 2; 109 /* status success */ 110 l_put_le16(0, ptr); 111 ptr += 2; 112 113 ie_tlv_builder_init(&builder, ptr, sizeof(data) - 4); 114 115 ie_parse_rsne_from_data(fils->hs->supplicant_ie, 116 fils->hs->supplicant_ie[1] + 2, 117 &rsn_info); 118 rsne = alloca(256); 119 ie_build_rsne(&rsn_info, rsne); 120 121 ie_tlv_builder_next(&builder, IE_TYPE_RSN); 122 ie_tlv_builder_set_data(&builder, rsne + 2, rsne[1]); 123 124 ie_tlv_builder_next(&builder, IE_TYPE_FILS_NONCE); 125 ie_tlv_builder_set_data(&builder, fils->nonce, sizeof(fils->nonce)); 126 127 ie_tlv_builder_next(&builder, IE_TYPE_FILS_SESSION); 128 ie_tlv_builder_set_data(&builder, fils->session, sizeof(fils->session)); 129 130 ie_tlv_builder_next(&builder, IE_TYPE_FILS_WRAPPED_DATA); 131 ie_tlv_builder_set_data(&builder, eap_data, len); 132 133 if (fils->hs->mde) { 134 ie_tlv_builder_next(&builder, IE_TYPE_MOBILITY_DOMAIN); 135 ie_tlv_builder_set_data(&builder, fils->hs->mde + 2, 136 fils->hs->mde[1]); 137 } 138 139 ie_tlv_builder_finalize(&builder, &len); 140 141 fils->auth(data, ptr - data + len, fils->user_data); 142 } 143 144 static int fils_derive_key_data(struct fils_sm *fils) 145 { 146 const void *rmsk; 147 size_t rmsk_len; 148 struct ie_tlv_builder builder; 149 uint8_t key[FILS_NONCE_LEN * 2]; 150 uint8_t key_data[64 + 48 + 16 + 48]; /* largest ICK, KEK, TK, FILS-FT */ 151 uint8_t key_auth[48]; 152 uint8_t data[44]; 153 uint8_t *ptr = data; 154 size_t hash_len; 155 struct iovec iov[5]; 156 size_t iov_elems = 0; 157 size_t fils_ft_len = 0; 158 bool sha384; 159 size_t ie_len; 160 uint8_t *rsne = NULL; 161 uint8_t oci[6]; 162 163 rmsk = erp_get_rmsk(fils->erp, &rmsk_len); 164 165 if (fils->hs->akm_suite == IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA256) 166 fils_ft_len = 32; 167 else if (fils->hs->akm_suite == IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA384) 168 fils_ft_len = 48; 169 /* 170 * IEEE 802.11ai - Section 12.12.2.5.3 171 */ 172 if (fils->hs->akm_suite & (IE_RSN_AKM_SUITE_FILS_SHA256 | 173 IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA256)) { 174 sha384 = false; 175 hash_len = 32; 176 } else { 177 sha384 = true; 178 hash_len = 48; 179 } 180 181 fils->kek_len = handshake_state_get_kek_len(fils->hs); 182 183 /* key is SNonce || ANonce */ 184 memcpy(key, fils->nonce, sizeof(fils->nonce)); 185 memcpy(key + FILS_NONCE_LEN, fils->anonce, sizeof(fils->anonce)); 186 187 if (sha384) 188 hmac_sha384(key, sizeof(key), rmsk, rmsk_len, 189 fils->pmk, hash_len); 190 else 191 hmac_sha256(key, sizeof(key), rmsk, rmsk_len, 192 fils->pmk, hash_len); 193 194 fils->pmk_len = hash_len; 195 196 /* 197 * IEEE 802.11ai - 12.12.2.5.3 PTKSA key derivation with FILS 198 * authentication 199 * 200 * FILS-Key-Data = PRF-X(PMK, "FILS PTK Derivation", SPA || AA || 201 * SNonce || ANonce) 202 */ 203 memcpy(ptr, fils->hs->spa, 6); 204 ptr += 6; 205 memcpy(ptr, fils->hs->aa, 6); 206 ptr += 6; 207 memcpy(ptr, fils->nonce, sizeof(fils->nonce)); 208 ptr += sizeof(fils->nonce); 209 memcpy(ptr, fils->anonce, sizeof(fils->anonce)); 210 ptr += sizeof(fils->anonce); 211 212 if (sha384) 213 kdf_sha384(fils->pmk, hash_len, "FILS PTK Derivation", 214 strlen("FILS PTK Derivation"), data, 215 sizeof(data), key_data, 216 hash_len + fils->kek_len + 16 + fils_ft_len); 217 else 218 kdf_sha256(fils->pmk, hash_len, "FILS PTK Derivation", 219 strlen("FILS PTK Derivation"), data, 220 sizeof(data), key_data, 221 hash_len + fils->kek_len + 16 + fils_ft_len); 222 223 ptr = data; 224 225 /* 226 * IEEE 802.11ai - 12.12.2.6.2 (Re)Association Request for FILS key 227 * confirmation 228 * 229 * Key-Auth = HMAC-Hash(ICK, SNonce || ANonce || STA-MAC || AP-BSSID) 230 */ 231 memcpy(ptr, fils->nonce, sizeof(fils->nonce)); 232 ptr += sizeof(fils->nonce); 233 memcpy(ptr, fils->anonce, sizeof(fils->anonce)); 234 ptr += sizeof(fils->anonce); 235 memcpy(ptr, fils->hs->spa, 6); 236 ptr += 6; 237 memcpy(ptr, fils->hs->aa, 6); 238 ptr += 6; 239 240 memcpy(fils->ick, key_data, hash_len); 241 fils->ick_len = hash_len; 242 243 if (fils_ft_len) { 244 memcpy(fils->fils_ft, key_data + hash_len + fils->kek_len + 16, 245 fils_ft_len); 246 fils->fils_ft_len = fils_ft_len; 247 } 248 249 handshake_state_set_fils_ft(fils->hs, fils->fils_ft, fils->fils_ft_len); 250 251 if (sha384) 252 hmac_sha384(fils->ick, hash_len, data, ptr - data, 253 key_auth, hash_len); 254 else 255 hmac_sha256(fils->ick, hash_len, data, ptr - data, 256 key_auth, hash_len); 257 258 ie_tlv_builder_init(&builder, NULL, 0); 259 260 ie_tlv_builder_next(&builder, IE_TYPE_FILS_KEY_CONFIRMATION); 261 ie_tlv_builder_set_data(&builder, key_auth, hash_len); 262 263 ie_tlv_builder_next(&builder, IE_TYPE_FILS_SESSION); 264 ie_tlv_builder_set_data(&builder, fils->session, sizeof(fils->session)); 265 266 iov[iov_elems].iov_base = ie_tlv_builder_finalize(&builder, &ie_len); 267 iov[iov_elems].iov_len = ie_len; 268 iov_elems++; 269 iov[iov_elems].iov_base = fils->hs->supplicant_ie; 270 iov[iov_elems].iov_len = fils->hs->supplicant_ie[1] + 2; 271 iov_elems++; 272 273 if (fils->hs->mde) { 274 struct ie_rsn_info rsn_info; 275 276 /* 277 * IEEE 8021.11ai Section 13.2.4: 278 * 279 * If a key hierarchy already exists for this STA belonging to 280 * the same mobility domain (i.e., having the same MDID), the 281 * R0KH shall delete the existing PMK-R0 security association 282 * and PMK-R1 security associations. 283 * 284 * All this means is we need to re-derive the new FT keys. This 285 * will rederive the PTK too, but it will be overwritten with 286 * the FILS PTK after associate 287 */ 288 handshake_state_derive_ptk(fils->hs); 289 290 iov[iov_elems].iov_base = fils->hs->mde; 291 iov[iov_elems].iov_len = fils->hs->mde[1] + 2; 292 iov_elems++; 293 294 if (ie_parse_rsne_from_data(fils->hs->supplicant_ie, 295 fils->hs->supplicant_ie[1] + 2, 296 &rsn_info) < 0) 297 return -EPROTO; 298 299 rsn_info.num_pmkids = 1; 300 rsn_info.pmkids = fils->hs->pmk_r1_name; 301 302 rsne = alloca(256); 303 ie_build_rsne(&rsn_info, rsne); 304 305 iov[iov_elems].iov_base = rsne; 306 iov[iov_elems].iov_len = rsne[1] + 2; 307 iov_elems += 1; 308 } 309 310 /* 311 * IEEE 802.11 Section 12.11.2.6.2 312 * "If dot11RSNAOperatingChannelValidationActivated is true and AP 313 * indicates OCVC capability, the STA shall include OCI element in the 314 * request" 315 */ 316 if (fils->hs->supplicant_ocvc && fils->hs->chandef) { 317 oci[0] = IE_TYPE_EXTENSION; 318 oci[1] = 4; 319 oci[2] = IE_TYPE_OCI & 0xff; 320 oci_from_chandef(fils->hs->chandef, oci + 3); 321 322 iov[iov_elems].iov_base = oci; 323 iov[iov_elems].iov_len = 6; 324 iov_elems++; 325 } 326 327 memcpy(data, fils->nonce, sizeof(fils->nonce)); 328 memcpy(data + sizeof(fils->nonce), fils->anonce, sizeof(fils->anonce)); 329 330 memcpy(fils->kek_and_tk, key_data + hash_len, fils->kek_len + 16); 331 332 fils->assoc(iov, iov_elems, fils->kek_and_tk, fils->kek_len, data, 333 FILS_NONCE_LEN * 2, fils->user_data); 334 335 return 0; 336 } 337 338 static bool fils_start(struct auth_proto *driver) 339 { 340 struct fils_sm *fils = l_container_of(driver, struct fils_sm, ap); 341 342 return erp_start(fils->erp); 343 } 344 345 static void fils_free(struct auth_proto *driver) 346 { 347 struct fils_sm *fils = l_container_of(driver, struct fils_sm, ap); 348 349 erp_free(fils->erp); 350 351 explicit_bzero(fils->ick, sizeof(fils->ick)); 352 explicit_bzero(fils->kek_and_tk, sizeof(fils->kek_and_tk)); 353 explicit_bzero(fils->pmk, fils->pmk_len); 354 explicit_bzero(fils->pmkid, sizeof(fils->pmkid)); 355 356 l_free(fils); 357 } 358 359 static int fils_rx_authenticate(struct auth_proto *driver, const uint8_t *frame, 360 size_t len) 361 { 362 struct fils_sm *fils = l_container_of(driver, struct fils_sm, ap); 363 const struct mmpdu_header *hdr = (const struct mmpdu_header *) frame; 364 const struct mmpdu_authentication *auth = mmpdu_body(hdr); 365 uint16_t alg; 366 struct ie_tlv_iter iter; 367 const uint8_t *anonce = NULL; 368 const uint8_t *session = NULL; 369 const uint8_t *wrapped = NULL; 370 size_t wrapped_len = 0; 371 const uint8_t *rsne = NULL; 372 const uint8_t *mde = NULL; 373 const uint8_t *fte = NULL; 374 375 if (auth->status != 0) { 376 l_debug("invalid status %u", auth->status); 377 return L_LE16_TO_CPU(auth->status); 378 } 379 380 alg = L_LE16_TO_CPU(auth->algorithm); 381 if (alg != MMPDU_AUTH_ALGO_FILS_SK && 382 alg != MMPDU_AUTH_ALGO_FILS_SK_PFS) { 383 l_debug("invalid auth algorithm %u", auth->algorithm); 384 return MMPDU_STATUS_CODE_UNSUP_AUTH_ALG; 385 } 386 387 ie_tlv_iter_init(&iter, auth->ies, (const uint8_t *) hdr + len - 388 auth->ies); 389 while (ie_tlv_iter_next(&iter)) { 390 switch (iter.tag) { 391 case IE_TYPE_FILS_NONCE: 392 if (iter.len != FILS_NONCE_LEN) 393 goto invalid_ies; 394 395 anonce = iter.data; 396 break; 397 case IE_TYPE_FILS_SESSION: 398 if (iter.len != FILS_SESSION_LEN) 399 goto invalid_ies; 400 401 session = iter.data; 402 break; 403 case IE_TYPE_FILS_WRAPPED_DATA: 404 wrapped = iter.data; 405 wrapped_len = iter.len; 406 break; 407 case IE_TYPE_RSN: 408 if (rsne) 409 goto invalid_ies; 410 411 rsne = ie_tlv_iter_get_data(&iter) - 2; 412 break; 413 414 case IE_TYPE_MOBILITY_DOMAIN: 415 if (mde) 416 goto invalid_ies; 417 418 mde = ie_tlv_iter_get_data(&iter) - 2; 419 break; 420 421 case IE_TYPE_FAST_BSS_TRANSITION: 422 if (fte) 423 goto invalid_ies; 424 425 fte = ie_tlv_iter_get_data(&iter) - 2; 426 break; 427 428 default: 429 continue; 430 } 431 } 432 433 if (!anonce || !session || !wrapped) { 434 l_debug("Auth did not include required IEs"); 435 goto invalid_ies; 436 } 437 438 if (mde) 439 handshake_state_set_mde(fils->hs, mde); 440 441 if (fte) { 442 struct handshake_state *hs = fils->hs; 443 uint32_t kck_len = handshake_state_get_kck_len(hs); 444 struct ie_ft_info ft_info; 445 446 if (ie_parse_fast_bss_transition_from_data(fte, fte[1] + 2, 447 kck_len, &ft_info) < 0) 448 goto invalid_ies; 449 450 handshake_state_set_authenticator_fte(fils->hs, fte); 451 handshake_state_set_kh_ids(fils->hs, ft_info.r0khid, 452 ft_info.r0khid_len, 453 ft_info.r1khid); 454 } 455 456 memcpy(fils->anonce, anonce, FILS_NONCE_LEN); 457 458 if (erp_rx_packet(fils->erp, wrapped, wrapped_len) < 0) 459 goto invalid_ies; 460 461 return fils->get_oci(fils->user_data); 462 463 464 invalid_ies: 465 return MMPDU_STATUS_CODE_INVALID_ELEMENT; 466 } 467 468 static int fils_rx_oci(struct auth_proto *driver) 469 { 470 struct fils_sm *fils = l_container_of(driver, struct fils_sm, ap); 471 472 return fils_derive_key_data(fils); 473 } 474 475 static int fils_rx_associate(struct auth_proto *driver, const uint8_t *frame, 476 size_t len) 477 { 478 struct fils_sm *fils = l_container_of(driver, struct fils_sm, ap); 479 const struct mmpdu_header *hdr = (const struct mmpdu_header *) frame; 480 const struct mmpdu_association_response *assoc = mmpdu_body(hdr); 481 struct ie_tlv_iter iter; 482 uint8_t key_rsc[8]; 483 const uint8_t *gtk = NULL; 484 size_t gtk_len; 485 uint8_t gtk_key_index; 486 const uint8_t *igtk = NULL; 487 size_t igtk_len; 488 uint8_t igtk_key_index; 489 const uint8_t *ap_key_auth = NULL; 490 uint8_t expected_key_auth[48]; 491 bool sha384 = (fils->hs->akm_suite & (IE_RSN_AKM_SUITE_FILS_SHA384 | 492 IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA384)); 493 uint8_t data[44]; 494 uint8_t *ptr; 495 496 if (assoc->status_code != 0) 497 return L_CPU_TO_LE16(assoc->status_code); 498 499 ie_tlv_iter_init(&iter, assoc->ies, (const uint8_t *) hdr + len - 500 assoc->ies); 501 502 while (ie_tlv_iter_next(&iter)) { 503 switch (iter.tag) { 504 case IE_TYPE_KEY_DELIVERY: 505 if (iter.len < 8) 506 goto invalid_ies; 507 508 memcpy(key_rsc, iter.data, 8); 509 510 gtk = handshake_util_find_gtk_kde(iter.data + 8, 511 iter.len - 8, 512 >k_len); 513 if (!gtk) 514 goto invalid_ies; 515 516 gtk_key_index = bit_field(gtk[0], 0, 2); 517 gtk += 2; 518 gtk_len -= 2; 519 520 if (!fils->hs->mfp) 521 break; 522 523 igtk = handshake_util_find_igtk_kde(iter.data + 8, 524 iter.len - 8, 525 &igtk_len); 526 if (!igtk) 527 goto invalid_ies; 528 529 igtk_key_index = l_get_le16(igtk); 530 igtk += 2; 531 igtk_len -= 2; 532 533 break; 534 case IE_TYPE_FILS_KEY_CONFIRMATION: 535 if (sha384 && iter.len != 48) 536 goto invalid_ies; 537 538 if (!sha384 && iter.len != 32) 539 goto invalid_ies; 540 541 ap_key_auth = iter.data; 542 break; 543 } 544 } 545 546 if (!ap_key_auth) { 547 l_debug("Associate did not include KeyAuth IE"); 548 goto invalid_ies; 549 } 550 551 ptr = data; 552 553 memcpy(ptr, fils->anonce, sizeof(fils->anonce)); 554 ptr += sizeof(fils->anonce); 555 memcpy(ptr, fils->nonce, sizeof(fils->nonce)); 556 ptr += sizeof(fils->nonce); 557 memcpy(ptr, fils->hs->aa, 6); 558 ptr += 6; 559 memcpy(ptr, fils->hs->spa, 6); 560 ptr += 6; 561 562 if (sha384) 563 hmac_sha384(fils->ick, fils->ick_len, data, ptr - data, 564 expected_key_auth, fils->ick_len); 565 else 566 hmac_sha256(fils->ick, fils->ick_len, data, ptr - data, 567 expected_key_auth, fils->ick_len); 568 569 if (memcmp(ap_key_auth, expected_key_auth, fils->ick_len)) { 570 l_error("AP KeyAuth did not verify"); 571 return -EBADMSG; 572 } 573 574 handshake_state_set_pmk(fils->hs, fils->pmk, fils->pmk_len); 575 handshake_state_set_pmkid(fils->hs, fils->pmkid); 576 577 _Pragma("GCC diagnostic push") 578 _Pragma("GCC diagnostic ignored \"-Wmaybe-uninitialized\"") 579 if (gtk) 580 handshake_state_install_gtk(fils->hs, gtk_key_index, gtk, 581 gtk_len, key_rsc, 6); 582 583 if (igtk) 584 handshake_state_install_igtk(fils->hs, igtk_key_index, 585 igtk + 6, igtk_len - 6, igtk); 586 _Pragma("GCC diagnostic pop") 587 588 handshake_state_set_ptk(fils->hs, fils->kek_and_tk, fils->kek_len + 16); 589 handshake_state_install_ptk(fils->hs); 590 591 return 0; 592 593 invalid_ies: 594 return MMPDU_STATUS_CODE_INVALID_ELEMENT; 595 } 596 597 struct auth_proto *fils_sm_new(struct handshake_state *hs, 598 fils_tx_authenticate_func_t auth, 599 fils_tx_associate_func_t assoc, 600 fils_get_oci_func_t get_oci, 601 void *user_data) 602 { 603 struct fils_sm *fils; 604 605 fils = l_new(struct fils_sm, 1); 606 607 fils->auth = auth; 608 fils->assoc = assoc; 609 fils->get_oci = get_oci; 610 fils->user_data = user_data; 611 fils->hs = hs; 612 613 fils->ap.start = fils_start; 614 fils->ap.free = fils_free; 615 fils->ap.rx_oci = fils_rx_oci; 616 fils->ap.rx_authenticate = fils_rx_authenticate; 617 fils->ap.rx_associate = fils_rx_associate; 618 619 fils->erp = erp_new(hs->erp_cache, fils_erp_tx_func, fils); 620 621 return &fils->ap; 622 }