eap-peap.c (16629B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2018-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 <stdio.h> 30 #include <errno.h> 31 #include <ell/ell.h> 32 33 #include "src/crypto.h" 34 #include "src/missing.h" 35 #include "src/eap.h" 36 #include "src/eap-private.h" 37 #include "src/eap-tls-common.h" 38 39 /* 40 * Protected EAP Protocol (PEAP): EAP type 25 as described in: 41 * 42 * PEAPv0: draft-kamath-pppext-peapv0-00 43 * PEAPv1: draft-josefsson-pppext-eap-tls-eap-05 44 */ 45 46 struct peap_state { 47 struct eap_state *phase2; 48 49 uint8_t key[128]; 50 uint8_t isk[32]; 51 }; 52 53 static void eap_peap_phase2_key_ready(const uint8_t *msk_data, size_t msk_len, 54 const uint8_t *emsk_data, size_t emsk_len, 55 const uint8_t *iv, size_t iv_len, 56 const uint8_t *session_id, size_t session_len, 57 void *user_data) 58 { 59 struct peap_state *peap_state = 60 eap_tls_common_get_variant_data(user_data); 61 62 l_debug("PEAP: New ISK received"); 63 64 memcpy(peap_state->isk, msk_data, sizeof(peap_state->isk)); 65 } 66 67 static void eap_peap_phase2_send_response(const uint8_t *pdu, size_t pdu_len, 68 void *user_data) 69 { 70 struct eap_state *eap = user_data; 71 72 if (eap_tls_common_get_negotiated_version(eap) == EAP_TLS_VERSION_0) { 73 if (pdu_len < 5) 74 return; 75 76 if (pdu[4] != EAP_TYPE_EXTENSIONS) { 77 pdu += 4; 78 pdu_len -= 4; 79 } 80 } 81 82 eap_tls_common_tunnel_send(eap, pdu, pdu_len); 83 } 84 85 static void eap_peap_phase2_complete(enum eap_result result, void *user_data) 86 { 87 struct eap_state *eap = user_data; 88 struct peap_state *peap_state; 89 90 l_debug("result: %d", result); 91 92 /* 93 * PEAPv1: draft-josefsson-pppext-eap-tls-eap-05, Section 2.2 94 * 95 * The receipt of a EAP-Failure or EAP-Success within the TLS protected 96 * channel results in a shutdown of the TLS channel by the peer. 97 */ 98 if (result == EAP_RESULT_SUCCESS) 99 /* 100 * Some of the EAP-PEAP server implementations seem to require a 101 * cleartext ACK for the tunneled EAP-Success messages instead 102 * of simply closing the tunnel. 103 */ 104 eap_tls_common_send_empty_response(eap); 105 else 106 eap_tls_common_tunnel_close(eap); 107 108 eap_discard_success_and_failure(eap, false); 109 eap_tls_common_set_completed(eap); 110 111 if (result != EAP_RESULT_SUCCESS) { 112 eap_tls_common_set_phase2_failed(eap); 113 114 return; 115 } 116 117 peap_state = eap_tls_common_get_variant_data(eap); 118 119 eap_set_key_material(eap, peap_state->key + 0, 64, NULL, 0, NULL, 120 0, NULL, 0); 121 explicit_bzero(peap_state->key, sizeof(peap_state->key)); 122 explicit_bzero(peap_state->isk, sizeof(peap_state->isk)); 123 124 eap_method_success(eap); 125 } 126 127 /* 128 * PEAPv0: draft-kamath-pppext-peapv0-00, Section 2 129 */ 130 #define EAP_EXTENSIONS_HEADER_LEN 5 131 #define EAP_EXTENSIONS_TLV_HEADER_LEN 4 132 #define EAP_EXTENSIONS_TLV_M_BIT_MASK 0x8000 133 134 enum eap_extensions_tlv_type { 135 /* Reserved = 0x0000, */ 136 /* Reserved = 0x0001, */ 137 /* Reserved = 0x0002, */ 138 EAP_EXTENSIONS_TLV_TYPE_RESULT = 0x8003, 139 EAP_EXTENSIONS_TLV_TYPE_CRYPTOBINDING = 0x000C, 140 }; 141 142 enum TLV_CRYPTOBINDING_TYPE { 143 TLV_CRYPTOBINDING_TYPE_REQUEST = 0, 144 TLV_CRYPTOBINDING_TYPE_RESPONSE = 1, 145 }; 146 147 static bool cryptobinding_tlv_generate_csk(struct eap_state *eap, uint8_t *imck) 148 { 149 struct peap_state *peap_state = eap_tls_common_get_variant_data(eap); 150 static const char *label = "Session Key Generating Function"; 151 152 if (!prf_plus_sha1(imck, 40, label, strlen(label), "\00", 1, 153 peap_state->key, sizeof(peap_state->key))) 154 return false; 155 156 return true; 157 } 158 159 static bool cryptobinding_tlv_generate_imck(struct eap_state *eap, 160 uint8_t *imck_out) 161 { 162 struct peap_state *peap_state = eap_tls_common_get_variant_data(eap); 163 static const char *label = "Inner Methods Compound Keys"; 164 165 if (!prf_plus_sha1(peap_state->key, 40, label, strlen(label), 166 peap_state->isk, 32, imck_out, 60)) 167 return false; 168 169 return true; 170 } 171 172 static int eap_extensions_handle_cryptobinding_tlv(struct eap_state *eap, 173 const uint8_t *data, 174 uint16_t tlv_value_len, 175 uint8_t *response) 176 { 177 static const uint8_t cryptobinding_val_len = 56; 178 static const uint8_t cryptobinding_compound_mac_len = 20; 179 static const uint8_t cryptobinding_nonce_len = 32; 180 const uint8_t *nonce; 181 const uint8_t *server_compound_mac; 182 uint8_t client_compound_mac[cryptobinding_compound_mac_len]; 183 const uint8_t *cryptobinding_tlv_value; 184 uint8_t buf[61]; 185 uint8_t imck[60]; 186 187 l_debug(""); 188 189 if (tlv_value_len != cryptobinding_val_len) 190 return -EBADMSG; 191 192 cryptobinding_tlv_value = data; 193 194 /* Reserved byte: must be ignored on receipt. */ 195 data += 1; 196 197 /* Version byte: must be set to 0. */ 198 if (*data) 199 return -EBADMSG; 200 201 data += 1; 202 203 /* RecvVersion byte: must be set to 0. */ 204 if (*data) 205 return -EBADMSG; 206 207 data += 1; 208 209 /* SubType byte: cryptobinding TLV request. */ 210 if (*data != TLV_CRYPTOBINDING_TYPE_REQUEST) 211 return -EBADMSG; 212 213 data += 1; 214 215 nonce = data; 216 data += cryptobinding_nonce_len; 217 218 server_compound_mac = data; 219 220 l_put_be16(EAP_EXTENSIONS_TLV_TYPE_CRYPTOBINDING, &buf[0]); 221 l_put_be16(tlv_value_len, &buf[2]); 222 memcpy(&buf[4], cryptobinding_tlv_value, 223 4 + cryptobinding_nonce_len); 224 memset(&buf[EAP_EXTENSIONS_TLV_HEADER_LEN + 4 + 225 cryptobinding_nonce_len], 226 0, cryptobinding_compound_mac_len); 227 buf[60] = EAP_TYPE_PEAP; 228 229 if (!cryptobinding_tlv_generate_imck(eap, imck)) { 230 l_error("PEAP: Failed to generate IMCK to validate " 231 "server compound MAC."); 232 233 return -EIO; 234 } 235 236 if (!hmac_sha1(imck + 40, 20, buf, sizeof(buf), client_compound_mac, 237 cryptobinding_compound_mac_len)) { 238 l_error("PEAP: Failed to generate compound MAC to validate " 239 "server compound MAC."); 240 241 return -EIO; 242 } 243 244 if (memcmp(server_compound_mac, client_compound_mac, 245 cryptobinding_compound_mac_len)) { 246 l_error("PEAP: Generated compound MAC and server compound MAC " 247 "don't match."); 248 return -EIO; 249 } 250 251 /* Build response Crypto-Binding TLV */ 252 data = response; 253 254 l_put_be16(EAP_EXTENSIONS_TLV_TYPE_CRYPTOBINDING, response); 255 response += 2; 256 257 l_put_be16(cryptobinding_val_len, response); 258 response += 2; 259 260 /* Reserved - must be set to 0. */ 261 l_put_u8(0, response); 262 response += 1; 263 264 /* Version */ 265 l_put_u8(EAP_TLS_VERSION_0, response); 266 response += 1; 267 268 /* Received Version */ 269 l_put_u8(EAP_TLS_VERSION_0, response); 270 response += 1; 271 272 /* Sub-Type */ 273 l_put_u8(TLV_CRYPTOBINDING_TYPE_RESPONSE, response); 274 response += 1; 275 276 memcpy(response, nonce, cryptobinding_nonce_len); 277 response += cryptobinding_nonce_len; 278 279 memcpy(buf, data, EAP_EXTENSIONS_TLV_HEADER_LEN + 4 + 280 cryptobinding_nonce_len); 281 282 if (!hmac_sha1(imck + 40, 20, buf, sizeof(buf), client_compound_mac, 283 cryptobinding_compound_mac_len)) { 284 l_error("PEAP: Failed to generate client compound MAC."); 285 286 return -EIO; 287 } 288 289 memcpy(response, client_compound_mac, cryptobinding_compound_mac_len); 290 291 if (!cryptobinding_tlv_generate_csk(eap, imck)) { 292 l_error("PEAP: Failed to generate Compound Session Key."); 293 294 return -EIO; 295 } 296 297 return EAP_EXTENSIONS_TLV_HEADER_LEN + cryptobinding_val_len; 298 } 299 300 enum eap_extensions_result { 301 EAP_EXTENSIONS_RESULT_SUCCCESS = 1, 302 EAP_EXTENSIONS_RESULT_FAILURE = 2, 303 }; 304 305 static int eap_extensions_handle_result_tlv(struct eap_state *eap, 306 const uint8_t *data, 307 size_t data_len, 308 uint8_t *response, 309 uint8_t *result) 310 { 311 static const uint8_t result_tlv_len = 2; 312 struct peap_state *peap_state; 313 314 if (data_len != result_tlv_len) 315 return -EBADMSG; 316 317 *result = l_get_be16(data); 318 319 l_debug("result: %d", *result); 320 321 switch (*result) { 322 case EAP_EXTENSIONS_RESULT_SUCCCESS: 323 peap_state = eap_tls_common_get_variant_data(eap); 324 325 *result = eap_method_is_success(peap_state->phase2) ? 326 EAP_EXTENSIONS_RESULT_SUCCCESS : 327 EAP_EXTENSIONS_RESULT_FAILURE; 328 /* fall through */ 329 case EAP_EXTENSIONS_RESULT_FAILURE: 330 break; 331 default: 332 return -ENOENT; 333 } 334 335 /* Build response Result TLV */ 336 337 l_put_be16(EAP_EXTENSIONS_TLV_TYPE_RESULT, response); 338 response += 2; 339 340 l_put_be16(result_tlv_len, response); 341 response += 2; 342 343 l_put_be16(*result, response); 344 345 return EAP_EXTENSIONS_TLV_HEADER_LEN + result_tlv_len; 346 } 347 348 static int eap_extensions_process_tlvs(struct eap_state *eap, 349 const uint8_t *data, 350 size_t data_len, 351 uint8_t *response, 352 uint8_t *result) 353 { 354 int response_len = 0; 355 uint16_t tlv_type; 356 uint16_t tlv_value_len; 357 bool seen_result_tlv = false; 358 bool seen_cryptobinding_tlv = false; 359 360 while (data_len >= EAP_EXTENSIONS_TLV_HEADER_LEN) { 361 int response_tlv_len = 0; 362 363 tlv_type = l_get_be16(data); 364 data += 2; 365 366 tlv_value_len = l_get_be16(data); 367 data += 2; 368 369 data_len -= EAP_EXTENSIONS_TLV_HEADER_LEN; 370 371 if (data_len < tlv_value_len) 372 return -EBADMSG; 373 374 switch (tlv_type) { 375 case EAP_EXTENSIONS_TLV_TYPE_RESULT: 376 if (seen_result_tlv) 377 return -EBADMSG; 378 379 seen_result_tlv = true; 380 381 response_tlv_len = eap_extensions_handle_result_tlv(eap, 382 data, tlv_value_len, response, 383 result); 384 385 break; 386 case EAP_EXTENSIONS_TLV_TYPE_CRYPTOBINDING: 387 if (seen_cryptobinding_tlv) 388 return -EBADMSG; 389 390 seen_cryptobinding_tlv = true; 391 392 response_tlv_len = 393 eap_extensions_handle_cryptobinding_tlv(eap, 394 data, tlv_value_len, response); 395 396 break; 397 default: 398 if (tlv_type & EAP_EXTENSIONS_TLV_M_BIT_MASK) 399 return -ENOENT; 400 401 break; 402 } 403 404 if (response_tlv_len < 0) 405 return response_tlv_len; 406 407 response += response_tlv_len; 408 response_len += response_tlv_len; 409 410 data += tlv_value_len; 411 data_len -= tlv_value_len; 412 } 413 414 return response_len; 415 } 416 417 static void eap_extensions_handle_request(struct eap_state *eap, 418 uint8_t id, 419 const uint8_t *pkt, 420 size_t len) 421 { 422 struct peap_state *peap_state; 423 uint8_t result = EAP_EXTENSIONS_RESULT_FAILURE; 424 /* 425 * The buffer size is chosen to satisfy the needs of the two supported 426 * TLVs. 427 */ 428 uint8_t response[256]; 429 int response_len; 430 431 response_len = eap_extensions_process_tlvs(eap, pkt, len, 432 &response[EAP_EXTENSIONS_HEADER_LEN], 433 &result); 434 435 if (response_len < 0) { 436 l_debug("PEAP: Failed to process TLVs %d", response_len); 437 438 return; 439 } 440 441 response_len += EAP_EXTENSIONS_HEADER_LEN; 442 443 response[0] = EAP_CODE_RESPONSE; 444 response[1] = id; 445 l_put_be16(response_len, &response[2]); 446 response[4] = EAP_TYPE_EXTENSIONS; 447 448 eap_peap_phase2_send_response(response, response_len, eap); 449 450 eap_discard_success_and_failure(eap, false); 451 eap_tls_common_set_completed(eap); 452 453 if (result != EAP_EXTENSIONS_RESULT_SUCCCESS) { 454 eap_tls_common_set_phase2_failed(eap); 455 456 eap_tls_common_tunnel_close(eap); 457 458 return; 459 } 460 461 peap_state = eap_tls_common_get_variant_data(eap); 462 463 eap_set_key_material(eap, peap_state->key + 0, 64, NULL, 0, NULL, 464 0, NULL, 0); 465 explicit_bzero(peap_state->key, sizeof(peap_state->key)); 466 explicit_bzero(peap_state->isk, sizeof(peap_state->isk)); 467 468 eap_method_success(eap); 469 } 470 471 static bool eap_peap_tunnel_ready(struct eap_state *eap, 472 const char *peer_identity, 473 bool resumed) 474 { 475 struct peap_state *peap_state = eap_tls_common_get_variant_data(eap); 476 477 /* 478 * PEAPv1: draft-josefsson-pppext-eap-tls-eap-05, Section 2.1.1 479 * 480 * Cleartext Success/Failure packets MUST be silently discarded once 481 * TLS tunnel has been brought up. 482 */ 483 eap_discard_success_and_failure(eap, true); 484 485 /* 486 * In case of a resumed session, the server will typically not run 487 * phase2 methods at all, but will instead send a result TLV right 488 * away. Treat this like a successful phase2. In case the server 489 * does proceed with phase2, the success/failure state will be updated. 490 */ 491 if (resumed) 492 eap_method_success(peap_state->phase2); 493 494 /* MSK, EMSK and challenge derivation */ 495 eap_tls_common_tunnel_prf_get_bytes(eap, true, "client EAP encryption", 496 peap_state->key, 128); 497 498 eap_tls_common_send_empty_response(eap); 499 500 return true; 501 } 502 503 static bool eap_peap_tunnel_handle_request(struct eap_state *eap, 504 const uint8_t *pkt, 505 size_t len) 506 { 507 struct peap_state *peap_state; 508 uint8_t id; 509 510 if (len > 4 && pkt[4] == EAP_TYPE_EXTENSIONS) { 511 uint16_t pkt_len; 512 uint8_t code = pkt[0]; 513 514 if (code != EAP_CODE_REQUEST) 515 return false; 516 517 pkt_len = l_get_be16(pkt + 2); 518 if (pkt_len != len) 519 return false; 520 521 id = pkt[1]; 522 523 eap_extensions_handle_request(eap, id, 524 pkt + EAP_EXTENSIONS_HEADER_LEN, 525 len - EAP_EXTENSIONS_HEADER_LEN); 526 527 return true; 528 } 529 530 peap_state = eap_tls_common_get_variant_data(eap); 531 532 if (eap_tls_common_get_negotiated_version(eap) == EAP_TLS_VERSION_0) { 533 if (len < 1) 534 return false; 535 536 /* 537 * The PEAPv0 phase2 packets are headerless. Our implementation 538 * of the EAP methods requires packet identifier. Therefore, 539 * PEAP packet identifier is used for the headerless 540 * phase2 packets. 541 */ 542 eap_save_last_id(eap, &id); 543 544 __eap_handle_request(peap_state->phase2, id, pkt, len); 545 546 return true; 547 } 548 549 eap_rx_packet(peap_state->phase2, pkt, len); 550 551 return true; 552 } 553 554 static void eap_peap_state_reset(void *variant_data) 555 { 556 struct peap_state *peap_state = variant_data; 557 558 if (!peap_state) 559 return; 560 561 eap_reset(peap_state->phase2); 562 563 explicit_bzero(peap_state->key, sizeof(peap_state->key)); 564 explicit_bzero(peap_state->isk, sizeof(peap_state->isk)); 565 } 566 567 static void eap_peap_state_destroy(void *variant_data) 568 { 569 struct peap_state *peap_state = variant_data; 570 571 if (!peap_state) 572 return; 573 574 eap_reset(peap_state->phase2); 575 eap_free(peap_state->phase2); 576 577 explicit_bzero(peap_state->key, sizeof(peap_state->key)); 578 explicit_bzero(peap_state->isk, sizeof(peap_state->isk)); 579 580 l_free(peap_state); 581 } 582 583 static int eap_peap_settings_check(struct l_settings *settings, 584 struct l_queue *secrets, 585 const char *prefix, 586 struct l_queue **out_missing) 587 { 588 char setting_key_prefix[72]; 589 int r; 590 591 snprintf(setting_key_prefix, sizeof(setting_key_prefix), 592 "%sPEAP-Phase2-", prefix); 593 594 r = __eap_check_settings(settings, secrets, setting_key_prefix, false, 595 out_missing); 596 if (r) 597 return r; 598 599 snprintf(setting_key_prefix, sizeof(setting_key_prefix), "%sPEAP-", 600 prefix); 601 return eap_tls_common_settings_check(settings, secrets, 602 setting_key_prefix, 603 out_missing); 604 } 605 606 static const struct eap_tls_variant_ops eap_ttls_ops = { 607 .version_max_supported = EAP_TLS_VERSION_1, 608 .tunnel_ready = eap_peap_tunnel_ready, 609 .tunnel_handle_request = eap_peap_tunnel_handle_request, 610 .reset = eap_peap_state_reset, 611 .destroy = eap_peap_state_destroy, 612 }; 613 614 static bool eap_peap_settings_load(struct eap_state *eap, 615 struct l_settings *settings, 616 const char *prefix) 617 { 618 char setting_key_prefix[72]; 619 struct peap_state *peap_state; 620 void *phase2; 621 622 phase2 = eap_new(eap_peap_phase2_send_response, 623 eap_peap_phase2_complete, eap); 624 625 if (!phase2) { 626 l_error("Could not create the PEAP phase two EAP instance"); 627 628 return false; 629 } 630 631 snprintf(setting_key_prefix, sizeof(setting_key_prefix), 632 "%sPEAP-Phase2-", prefix); 633 634 if (!eap_load_settings(phase2, settings, setting_key_prefix)) { 635 eap_free(phase2); 636 637 return false; 638 } 639 640 peap_state = l_new(struct peap_state, 1); 641 peap_state->phase2 = phase2; 642 eap_set_key_material_func(peap_state->phase2, 643 eap_peap_phase2_key_ready); 644 645 snprintf(setting_key_prefix, sizeof(setting_key_prefix), "%sPEAP-", 646 prefix); 647 648 if (!eap_tls_common_settings_load(eap, settings, setting_key_prefix, 649 &eap_ttls_ops, peap_state)) 650 return false; 651 652 return true; 653 } 654 655 static struct eap_method eap_peap = { 656 .request_type = EAP_TYPE_PEAP, 657 .name = "PEAP", 658 .exports_msk = true, 659 660 .handle_request = eap_tls_common_handle_request, 661 .handle_retransmit = eap_tls_common_handle_retransmit, 662 .free = eap_tls_common_state_free, 663 .reset_state = eap_tls_common_state_reset, 664 665 .check_settings = eap_peap_settings_check, 666 .load_settings = eap_peap_settings_load, 667 }; 668 669 static int eap_peap_init(void) 670 { 671 l_debug(""); 672 return eap_register_method(&eap_peap); 673 } 674 675 static void eap_peap_exit(void) 676 { 677 l_debug(""); 678 eap_unregister_method(&eap_peap); 679 } 680 681 EAP_METHOD_BUILTIN(eap_peap, eap_peap_init, eap_peap_exit)