eap-ttls.c (29315B)
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 <stdio.h> 30 #include <errno.h> 31 #include <ell/ell.h> 32 33 #include "src/missing.h" 34 #include "src/util.h" 35 #include "src/mschaputil.h" 36 #include "src/eap.h" 37 #include "src/eap-private.h" 38 #include "src/eap-tls-common.h" 39 40 #define TTLS_AVP_HEADER_LEN 8 41 #define TTLS_AVP_LEN_MASK 0xFFFFFF 42 43 enum ttls_avp_flag { 44 TTLS_AVP_FLAG_M = 0x40, 45 TTLS_AVP_FLAG_V = 0x80, 46 TTLS_AVP_FLAG_MASK = 0xFF, 47 }; 48 49 enum radius_attr { 50 RADIUS_ATTR_USER_NAME = 1, 51 RADIUS_ATTR_USER_PASSWORD = 2, 52 RADIUS_ATTR_MSCHAPV2_ERROR = 2, 53 RADIUS_ATTR_CHAP_PASSWORD = 3, 54 RADIUS_ATTR_MS_CHAP_CHALLENGE = 11, 55 RADIUS_ATTR_MSCHAPV2_RESPONSE = 25, 56 RADIUS_ATTR_MSCHAPV2_SUCCESS = 26, 57 RADIUS_ATTR_CHAP_CHALLENGE = 60, 58 RADIUS_ATTR_EAP_MESSAGE = 79, 59 }; 60 61 struct avp_builder { 62 uint32_t capacity; 63 uint8_t *buf; 64 uint32_t pos; 65 uint8_t *avp_start; 66 }; 67 68 static uint8_t *avp_builder_reserve(struct avp_builder *builder, 69 uint32_t alignment, size_t len) 70 { 71 size_t aligned_pos = align_len(builder->pos, alignment); 72 size_t end = aligned_pos + len; 73 74 if (end > builder->capacity) { 75 builder->buf = l_realloc(builder->buf, end); 76 builder->capacity = end; 77 } 78 79 if (aligned_pos - builder->pos > 0) 80 memset(builder->buf + builder->pos, 0, 81 aligned_pos - builder->pos); 82 83 builder->pos = end; 84 85 return builder->buf + aligned_pos; 86 } 87 88 static bool avp_builder_finalize_avp(struct avp_builder *builder) 89 { 90 uint8_t *p; 91 uint32_t len; 92 93 if (!builder->avp_start) 94 return false; 95 96 p = builder->buf + builder->pos; 97 98 len = l_get_be32(builder->avp_start + 4); 99 len |= p - builder->avp_start; 100 l_put_be32(len, builder->avp_start + 4); 101 102 builder->avp_start = 0; 103 104 return true; 105 } 106 107 static bool avp_builder_start_avp(struct avp_builder *builder, 108 enum radius_attr type, 109 bool mandatory, uint32_t vendor_id) 110 { 111 uint32_t flags; 112 113 if (builder->avp_start) 114 return false; 115 116 builder->avp_start = avp_builder_reserve(builder, 4, 117 TTLS_AVP_HEADER_LEN + 118 (vendor_id ? 4 : 0)); 119 120 l_put_be32(type, builder->avp_start); 121 122 flags = 0; 123 124 if (mandatory) 125 flags |= TTLS_AVP_FLAG_M; 126 127 if (vendor_id) { 128 flags |= TTLS_AVP_FLAG_V; 129 l_put_be32(vendor_id, builder->avp_start + TTLS_AVP_HEADER_LEN); 130 } 131 132 l_put_be32(flags << 24, builder->avp_start + 4); 133 134 return true; 135 } 136 137 static struct avp_builder *avp_builder_new(size_t capacity) 138 { 139 struct avp_builder *builder; 140 141 if (!capacity) 142 return NULL; 143 144 builder = l_new(struct avp_builder, 1); 145 146 builder->buf = l_malloc(capacity); 147 memset(builder->buf, 0, capacity); 148 149 builder->capacity = capacity; 150 151 return builder; 152 } 153 154 static uint8_t *avp_builder_free(struct avp_builder *builder, bool free_data, 155 size_t *out_size) 156 { 157 uint8_t *ret; 158 159 if (free_data) { 160 explicit_bzero(builder->buf, builder->pos); 161 l_free(builder->buf); 162 builder->buf = NULL; 163 } 164 165 ret = builder->buf; 166 167 if (out_size) 168 *out_size = builder->pos; 169 170 l_free(builder); 171 172 return ret; 173 } 174 175 static void build_avp_user_name(struct avp_builder *builder, 176 const char *user_name) 177 { 178 size_t len = strlen(user_name); 179 uint8_t *to; 180 181 avp_builder_start_avp(builder, RADIUS_ATTR_USER_NAME, true, 0); 182 183 to = avp_builder_reserve(builder, 1, len); 184 memcpy(to, user_name, len); 185 186 avp_builder_finalize_avp(builder); 187 } 188 189 static void build_avp_user_password(struct avp_builder *builder, 190 const char *user_password) 191 { 192 size_t len = strlen(user_password); 193 uint8_t *to; 194 195 avp_builder_start_avp(builder, RADIUS_ATTR_USER_PASSWORD, true, 0); 196 197 /* 198 * Null-pad the password to a multiple of 16 octets, to obfuscate 199 * its length 200 */ 201 to = avp_builder_reserve(builder, 1, align_len(len, 16)); 202 memcpy(to, user_password, len); 203 204 avp_builder_finalize_avp(builder); 205 } 206 207 #define CHAP_IDENT_LEN 1 208 #define CHAP_CHALLENGE_LEN 16 209 #define CHAP_PASSWORD_LEN 16 210 211 static void build_avp_chap_challenge(struct avp_builder *builder, 212 const uint8_t *challenge) 213 { 214 avp_builder_start_avp(builder, RADIUS_ATTR_CHAP_CHALLENGE, true, 0); 215 memcpy(avp_builder_reserve(builder, 1, CHAP_CHALLENGE_LEN), challenge, 216 CHAP_CHALLENGE_LEN); 217 avp_builder_finalize_avp(builder); 218 } 219 220 static void build_avp_chap_password(struct avp_builder *builder, 221 const uint8_t *ident, 222 const uint8_t *password_hash) 223 { 224 avp_builder_start_avp(builder, RADIUS_ATTR_CHAP_PASSWORD, true, 0); 225 226 memcpy(avp_builder_reserve(builder, 1, CHAP_IDENT_LEN), ident, 227 CHAP_IDENT_LEN); 228 memcpy(avp_builder_reserve(builder, 1, CHAP_PASSWORD_LEN), 229 password_hash, CHAP_PASSWORD_LEN); 230 avp_builder_finalize_avp(builder); 231 } 232 233 #define RADIUS_VENDOR_ID_MICROSOFT 311 234 #define RADIUS_ATTR_MS_CHAP_RESPONSE 1 235 236 #define MS_CHAP_CHALLENGE_LEN 8 237 #define MS_CHAP_LM_RESPONSE_LEN 24 238 #define MS_CHAP_NT_RESPONSE_LEN 24 239 240 static void build_avp_ms_chap_challenge(struct avp_builder *builder, 241 const uint8_t *challenge) 242 { 243 avp_builder_start_avp(builder, RADIUS_ATTR_MS_CHAP_CHALLENGE, true, 244 RADIUS_VENDOR_ID_MICROSOFT); 245 memcpy(avp_builder_reserve(builder, 1, MS_CHAP_CHALLENGE_LEN), 246 challenge, MS_CHAP_CHALLENGE_LEN); 247 avp_builder_finalize_avp(builder); 248 } 249 250 static void build_avp_ms_chap_response(struct avp_builder *builder, 251 const uint8_t *ident, 252 const uint8_t *challenge, 253 const uint8_t *password_hash) 254 { 255 uint8_t *flags; 256 uint8_t nt_challenge_response[NT_CHALLENGE_RESPONSE_LEN]; 257 258 avp_builder_start_avp(builder, RADIUS_ATTR_MS_CHAP_RESPONSE, true, 259 RADIUS_VENDOR_ID_MICROSOFT); 260 261 memcpy(avp_builder_reserve(builder, 1, CHAP_IDENT_LEN), 262 ident, CHAP_IDENT_LEN); 263 264 /* 265 * RFC 2548: Section 2.1.3 266 * 267 * The Flags field is set to one (0x01), the NT-Response field is to 268 * be used in preference to the LM-Response field for authentication. 269 */ 270 flags = avp_builder_reserve(builder, 1, 1); 271 *flags = 1; 272 273 /* The LM-Response field is left empty */ 274 avp_builder_reserve(builder, 1, MS_CHAP_LM_RESPONSE_LEN); 275 276 mschap_challenge_response(challenge, password_hash, 277 nt_challenge_response); 278 279 memcpy(avp_builder_reserve(builder, 1, NT_CHALLENGE_RESPONSE_LEN), 280 nt_challenge_response, NT_CHALLENGE_RESPONSE_LEN); 281 282 avp_builder_finalize_avp(builder); 283 } 284 285 #define MSCHAPV2_RESERVED_LEN 8 286 #define MSCHAPV2_RESPONSE_LEN 24 287 #define MSCHAPV2_CHALLENGE_LEN 16 288 #define MSCHAPV2_SERVER_RESPONSE_LEN 42 289 290 static void build_avp_mschapv2_challenge(struct avp_builder *builder, 291 const uint8_t *challenge) 292 { 293 avp_builder_start_avp(builder, RADIUS_ATTR_MS_CHAP_CHALLENGE, true, 294 RADIUS_VENDOR_ID_MICROSOFT); 295 memcpy(avp_builder_reserve(builder, 1, MSCHAPV2_CHALLENGE_LEN), 296 challenge, MSCHAPV2_CHALLENGE_LEN); 297 avp_builder_finalize_avp(builder); 298 } 299 300 static void build_avp_mschapv2_response(struct avp_builder *builder, 301 const uint8_t *ident, 302 const uint8_t *peer_challenge, 303 const uint8_t *response) 304 { 305 uint8_t *flags; 306 307 avp_builder_start_avp(builder, RADIUS_ATTR_MSCHAPV2_RESPONSE, true, 308 RADIUS_VENDOR_ID_MICROSOFT); 309 310 memcpy(avp_builder_reserve(builder, 1, CHAP_IDENT_LEN), 311 ident, CHAP_IDENT_LEN); 312 313 /* 314 * RFC 2548: Section 2.3.2. 315 * 316 * The Flags field is one octet in length. It is reserved for future 317 * use and MUST be zero. 318 */ 319 flags = avp_builder_reserve(builder, 1, 1); 320 *flags = 0; 321 322 memcpy(avp_builder_reserve(builder, 1, MSCHAPV2_CHALLENGE_LEN), 323 peer_challenge, 324 MSCHAPV2_CHALLENGE_LEN); 325 326 /* 327 * Reserved - This field is 8 octets long and MUST be zero. 328 */ 329 avp_builder_reserve(builder, 1, MSCHAPV2_RESERVED_LEN); 330 331 memcpy(avp_builder_reserve(builder, 1, MSCHAPV2_RESPONSE_LEN), 332 response, MSCHAPV2_RESPONSE_LEN); 333 334 avp_builder_finalize_avp(builder); 335 } 336 337 struct avp_iter { 338 enum radius_attr type; 339 uint8_t flags; 340 uint32_t len; 341 uint32_t vendor_id; 342 const uint8_t *data; 343 const uint8_t *buf; 344 size_t buf_len; 345 size_t offset; 346 }; 347 348 static void avp_iter_init(struct avp_iter *iter, const uint8_t *buf, size_t len) 349 { 350 iter->buf = buf; 351 iter->buf_len = len; 352 iter->offset = 0; 353 } 354 355 static bool avp_iter_next(struct avp_iter *iter) 356 { 357 const uint8_t *start = iter->buf + iter->offset; 358 const uint8_t *end = iter->buf + iter->buf_len; 359 enum radius_attr type; 360 uint32_t len; 361 uint8_t flags; 362 uint8_t pad_len; 363 364 /* Make sure we have at least the header fields */ 365 if (iter->offset + TTLS_AVP_HEADER_LEN >= iter->buf_len) 366 return false; 367 368 type = l_get_be32(start); 369 start += 4; 370 371 len = l_get_be32(start); 372 start += 4; 373 374 flags = (len >> 24) & TTLS_AVP_FLAG_MASK; 375 len &= TTLS_AVP_LEN_MASK; 376 377 len -= TTLS_AVP_HEADER_LEN; 378 379 if ((ptrdiff_t) len > end - start) 380 return false; 381 382 if (flags & TTLS_AVP_FLAG_V) { 383 if (len < 4) 384 return false; 385 386 iter->vendor_id = l_get_be32(start); 387 start += 4; 388 len -= 4; 389 } else { 390 iter->vendor_id = 0; 391 } 392 393 iter->type = type; 394 iter->flags = flags; 395 iter->len = len; 396 iter->data = start; 397 398 if (len & 3) 399 pad_len = 4 - (len & 3); 400 else 401 pad_len = 0; 402 403 iter->offset = start + len + pad_len - iter->buf; 404 405 return true; 406 } 407 408 struct phase2_credentials { 409 char *username; 410 char *password; 411 }; 412 413 struct phase2_method { 414 void *state; 415 struct phase2_credentials credentials; 416 const struct phase2_method_ops *ops; 417 }; 418 419 struct phase2_method_ops { 420 bool (*init)(struct eap_state *eap); 421 bool (*handle_avp)(struct eap_state *eap, enum radius_attr type, 422 uint32_t vendor_id, const uint8_t *data, 423 size_t len); 424 void (*destroy)(struct phase2_method *phase2); 425 void (*reset)(struct phase2_method *phase2); 426 }; 427 428 static void eap_ttls_phase2_credentials_destroy( 429 struct phase2_credentials *credentials) 430 { 431 if (!credentials) 432 return; 433 434 if (credentials->password) 435 explicit_bzero(credentials->password, 436 strlen(credentials->password)); 437 438 l_free(credentials->username); 439 l_free(credentials->password); 440 } 441 442 static bool eap_ttls_phase2_non_eap_load_settings(struct phase2_method *phase2, 443 struct l_settings *settings, 444 const char *prefix) 445 { 446 char setting[128]; 447 448 snprintf(setting, sizeof(setting), "%sIdentity", prefix); 449 phase2->credentials.username = 450 l_settings_get_string(settings, "Security", setting); 451 452 if (!phase2->credentials.username) { 453 l_error("Phase 2 Identity is missing."); 454 return false; 455 } 456 457 snprintf(setting, sizeof(setting), "%sPassword", prefix); 458 phase2->credentials.password = 459 l_settings_get_string(settings, "Security", setting); 460 461 if (!phase2->credentials.password) { 462 l_error("Phase 2 Password is missing."); 463 l_free(phase2->credentials.username); 464 465 return false; 466 } 467 468 return true; 469 } 470 471 static bool eap_ttls_phase2_chap_generate_challenge(struct eap_state *eap, 472 uint8_t *challenge, 473 size_t challenge_len) 474 { 475 return eap_tls_common_tunnel_prf_get_bytes(eap, true, "ttls challenge", 476 challenge, 477 challenge_len); 478 } 479 480 static bool eap_ttls_phase2_chap_init(struct eap_state *eap) 481 { 482 struct phase2_method *phase2 = eap_tls_common_get_variant_data(eap); 483 struct phase2_credentials *credentials = &phase2->credentials; 484 struct avp_builder *builder; 485 uint8_t challenge[CHAP_CHALLENGE_LEN + CHAP_IDENT_LEN]; 486 uint8_t password_hash[CHAP_PASSWORD_LEN]; 487 uint8_t ident; 488 struct l_checksum *hash; 489 uint8_t *data; 490 size_t data_len; 491 492 if (!eap_ttls_phase2_chap_generate_challenge(eap, challenge, 493 CHAP_CHALLENGE_LEN + 494 CHAP_IDENT_LEN)) { 495 l_error("TTLS Tunneled-CHAP: Failed to generate CHAP " 496 "challenge."); 497 return false; 498 } 499 500 ident = challenge[CHAP_CHALLENGE_LEN]; 501 502 hash = l_checksum_new(L_CHECKSUM_MD5); 503 if (!hash) { 504 l_error("Can't create the MD5 checksum"); 505 return false; 506 } 507 508 l_checksum_update(hash, &ident, CHAP_IDENT_LEN); 509 l_checksum_update(hash, credentials->password, 510 strlen(credentials->password)); 511 l_checksum_update(hash, challenge, CHAP_CHALLENGE_LEN); 512 513 l_checksum_get_digest(hash, password_hash, CHAP_PASSWORD_LEN); 514 l_checksum_free(hash); 515 516 builder = avp_builder_new(512); 517 518 build_avp_user_name(builder, credentials->username); 519 build_avp_chap_challenge(builder, challenge); 520 build_avp_chap_password(builder, &ident, password_hash); 521 explicit_bzero(password_hash, sizeof(password_hash)); 522 523 data = avp_builder_free(builder, false, &data_len); 524 525 eap_tls_common_tunnel_send(eap, data, data_len); 526 explicit_bzero(data, data_len); 527 l_free(data); 528 529 return true; 530 } 531 532 static const struct phase2_method_ops phase2_chap_ops = { 533 .init = eap_ttls_phase2_chap_init, 534 }; 535 536 static bool eap_ttls_phase2_ms_chap_init(struct eap_state *eap) 537 { 538 struct phase2_method *phase2 = eap_tls_common_get_variant_data(eap); 539 struct phase2_credentials *credentials = &phase2->credentials; 540 struct avp_builder *builder; 541 uint8_t challenge[MS_CHAP_CHALLENGE_LEN + CHAP_IDENT_LEN]; 542 uint8_t password_hash[16]; 543 uint8_t ident; 544 uint8_t *data; 545 size_t data_len; 546 547 if (!eap_ttls_phase2_chap_generate_challenge(eap, challenge, 548 MS_CHAP_CHALLENGE_LEN + 549 CHAP_IDENT_LEN)) { 550 l_error("TTLS Tunneled-MSCHAP: Failed to generate MS-CHAP " 551 "challenge."); 552 return false; 553 } 554 555 ident = challenge[MS_CHAP_CHALLENGE_LEN]; 556 557 builder = avp_builder_new(512); 558 559 build_avp_user_name(builder, credentials->username); 560 build_avp_ms_chap_challenge(builder, challenge); 561 562 mschap_nt_password_hash(credentials->password, password_hash); 563 564 build_avp_ms_chap_response(builder, &ident, challenge, password_hash); 565 explicit_bzero(password_hash, sizeof(password_hash)); 566 567 data = avp_builder_free(builder, false, &data_len); 568 569 eap_tls_common_tunnel_send(eap, data, data_len); 570 explicit_bzero(data, data_len); 571 l_free(data); 572 573 return true; 574 } 575 576 static const struct phase2_method_ops phase2_mschap_ops = { 577 .init = eap_ttls_phase2_ms_chap_init, 578 }; 579 580 struct mschapv2_state { 581 uint8_t server_challenge[MSCHAPV2_CHALLENGE_LEN + CHAP_IDENT_LEN]; 582 uint8_t peer_challenge[MSCHAPV2_CHALLENGE_LEN]; 583 uint8_t password_hash[16]; 584 }; 585 586 static void mschapv2_state_destroy(struct phase2_method *phase2) 587 { 588 struct mschapv2_state *state = phase2->state; 589 590 if (!state) 591 return; 592 593 explicit_bzero(state->server_challenge, MSCHAPV2_CHALLENGE_LEN + 594 CHAP_IDENT_LEN); 595 explicit_bzero(state->peer_challenge, MSCHAPV2_CHALLENGE_LEN); 596 explicit_bzero(state->password_hash, 16); 597 598 l_free(state); 599 phase2->state = NULL; 600 } 601 602 static bool eap_ttls_phase2_mschapv2_init(struct eap_state *eap) 603 { 604 struct phase2_method *phase2 = eap_tls_common_get_variant_data(eap); 605 struct phase2_credentials *credentials = &phase2->credentials; 606 struct mschapv2_state *mschapv2_state; 607 struct avp_builder *builder; 608 uint8_t response[MSCHAPV2_RESPONSE_LEN]; 609 uint8_t ident; 610 uint8_t *data; 611 size_t data_len; 612 613 phase2->state = mschapv2_state = l_new(struct mschapv2_state, 1); 614 615 if (!l_getrandom(mschapv2_state->peer_challenge, 616 MSCHAPV2_CHALLENGE_LEN)) { 617 l_error("TTLS Tunneled-MSCHAPv2: Failed to generate random for " 618 "peer challenge."); 619 return false; 620 } 621 622 if (!eap_ttls_phase2_chap_generate_challenge(eap, 623 mschapv2_state->server_challenge, 624 MSCHAPV2_CHALLENGE_LEN + 625 CHAP_IDENT_LEN)) { 626 l_error("TTLS Tunneled-MSCHAPv2: Failed to generate CHAP " 627 "challenge."); 628 return false; 629 } 630 631 if (!mschap_nt_password_hash(credentials->password, 632 mschapv2_state->password_hash)) { 633 l_error("TTLS Tunneled-MSCHAPv2: Failed to generate password " 634 "hash."); 635 return false; 636 } 637 638 if (!mschapv2_generate_nt_response(mschapv2_state->password_hash, 639 mschapv2_state->peer_challenge, 640 mschapv2_state->server_challenge, 641 credentials->username, response)) { 642 l_error("TTLS Tunneled-MSCHAPv2: Failed to generate " 643 "NT response."); 644 return false; 645 } 646 647 ident = mschapv2_state->server_challenge[MSCHAPV2_CHALLENGE_LEN]; 648 649 builder = avp_builder_new(512); 650 651 build_avp_user_name(builder, credentials->username); 652 build_avp_mschapv2_challenge(builder, mschapv2_state->server_challenge); 653 build_avp_mschapv2_response(builder, &ident, 654 mschapv2_state->peer_challenge, 655 response); 656 657 data = avp_builder_free(builder, false, &data_len); 658 659 eap_tls_common_tunnel_send(eap, data, data_len); 660 l_free(data); 661 662 return true; 663 } 664 665 static bool eap_ttls_phase2_mschapv2_handle_success(struct eap_state *eap, 666 const uint8_t *data, 667 size_t len) 668 { 669 struct phase2_method *phase2 = eap_tls_common_get_variant_data(eap); 670 struct phase2_credentials *credentials = &phase2->credentials; 671 struct mschapv2_state *mschapv2_state = phase2->state; 672 uint8_t nt_response[MSCHAPV2_RESPONSE_LEN]; 673 char nt_server_response[MSCHAPV2_SERVER_RESPONSE_LEN]; 674 uint8_t password_hash_hash[16]; 675 bool r; 676 677 if (len != CHAP_IDENT_LEN + MSCHAPV2_SERVER_RESPONSE_LEN) { 678 l_error("TTLS Tunneled MSCHAPv2: Server response has invalid " 679 "length."); 680 goto error; 681 } 682 683 if (!mschapv2_generate_nt_response(mschapv2_state->password_hash, 684 mschapv2_state->peer_challenge, 685 mschapv2_state->server_challenge, 686 credentials->username, 687 nt_response)) { 688 l_error("TTLS Tunneled-MSCHAPv2: Failed to generate " 689 "NT response."); 690 goto error; 691 } 692 693 if (!mschapv2_hash_nt_password_hash(mschapv2_state->password_hash, 694 password_hash_hash)) { 695 l_error("TTLS Tunneled-MSCHAPv2: Failed to generate " 696 "hash of the password hash."); 697 goto error; 698 } 699 700 r = mschapv2_generate_authenticator_response( 701 password_hash_hash, nt_response, 702 mschapv2_state->peer_challenge, 703 mschapv2_state->server_challenge, 704 credentials->username, 705 nt_server_response); 706 explicit_bzero(password_hash_hash, sizeof(password_hash_hash)); 707 708 if (!r) { 709 l_error("TTLS Tunneled-MSCHAPv2: Failed to generate server " 710 "response."); 711 goto error; 712 } 713 714 if (memcmp(nt_server_response, data + CHAP_IDENT_LEN, 715 MSCHAPV2_SERVER_RESPONSE_LEN)) { 716 l_error("TTLS Tunneled-MSCHAPv2: Invalid server response."); 717 718 goto error; 719 } 720 721 eap_tls_common_send_empty_response(eap); 722 723 return true; 724 725 error: 726 eap_tls_common_set_phase2_failed(eap); 727 728 return false; 729 } 730 731 static bool eap_ttls_phase2_mschapv2_handle_error(struct eap_state *eap, 732 const uint8_t *data, 733 size_t len) 734 { 735 l_error("TTLS Tunneled-MSCHAPv2: Authentication failed."); 736 737 eap_tls_common_set_phase2_failed(eap); 738 739 return false; 740 } 741 742 static bool eap_ttls_phase2_mschapv2_handle_avp(struct eap_state *eap, 743 enum radius_attr type, 744 uint32_t vendor_id, 745 const uint8_t *data, 746 size_t len) 747 { 748 if (vendor_id != RADIUS_VENDOR_ID_MICROSOFT) 749 return false; 750 751 if (type == RADIUS_ATTR_MSCHAPV2_SUCCESS) 752 return eap_ttls_phase2_mschapv2_handle_success(eap, data, len); 753 else if (type == RADIUS_ATTR_MSCHAPV2_ERROR) 754 return eap_ttls_phase2_mschapv2_handle_error(eap, data, len); 755 756 return false; 757 } 758 759 static const struct phase2_method_ops phase2_mschapv2_ops = { 760 .init = eap_ttls_phase2_mschapv2_init, 761 .handle_avp = eap_ttls_phase2_mschapv2_handle_avp, 762 .reset = mschapv2_state_destroy, 763 .destroy = mschapv2_state_destroy, 764 }; 765 766 static bool eap_ttls_phase2_pap_init(struct eap_state *eap) 767 { 768 struct phase2_method *phase2 = eap_tls_common_get_variant_data(eap); 769 struct phase2_credentials *credentials = &phase2->credentials; 770 struct avp_builder *builder; 771 uint8_t *buf; 772 size_t buf_len; 773 774 builder = avp_builder_new(512); 775 776 build_avp_user_name(builder, credentials->username); 777 build_avp_user_password(builder, credentials->password); 778 779 buf = avp_builder_free(builder, false, &buf_len); 780 781 eap_tls_common_tunnel_send(eap, buf, buf_len); 782 explicit_bzero(buf, buf_len); 783 l_free(buf); 784 785 return true; 786 } 787 788 static const struct phase2_method_ops phase2_pap_ops = { 789 .init = eap_ttls_phase2_pap_init, 790 }; 791 792 static void eap_ttls_phase2_eap_send_response(const uint8_t *data, size_t len, 793 void *user_data) 794 { 795 struct eap_state *eap = user_data; 796 struct avp_builder *builder; 797 uint8_t *msg_data; 798 size_t msg_data_len; 799 800 builder = avp_builder_new(TTLS_AVP_HEADER_LEN + len); 801 802 avp_builder_start_avp(builder, RADIUS_ATTR_EAP_MESSAGE, true, 0); 803 memcpy(avp_builder_reserve(builder, 1, len), data, len); 804 avp_builder_finalize_avp(builder); 805 806 msg_data = avp_builder_free(builder, false, &msg_data_len); 807 808 eap_tls_common_tunnel_send(eap, msg_data, msg_data_len); 809 l_free(msg_data); 810 } 811 812 static void eap_ttls_phase2_eap_complete(enum eap_result result, 813 void *user_data) 814 { 815 struct eap_state *eap = user_data; 816 817 eap_tls_common_set_completed(eap); 818 819 if (result != EAP_RESULT_SUCCESS) { 820 eap_tls_common_set_phase2_failed(eap); 821 822 return; 823 } 824 825 eap_method_success(eap); 826 } 827 828 static bool eap_ttls_phase2_eap_load_settings(struct eap_state *eap, 829 struct phase2_method *phase2, 830 struct l_settings *settings, 831 const char *prefix) 832 { 833 phase2->state = eap_new(eap_ttls_phase2_eap_send_response, 834 eap_ttls_phase2_eap_complete, 835 eap); 836 if (!phase2->state) { 837 l_error("Could not create the TTLS Phase 2 EAP instance"); 838 return false; 839 } 840 841 if (!eap_load_settings(phase2->state, settings, prefix)) { 842 eap_free(phase2->state); 843 return false; 844 } 845 846 return true; 847 } 848 849 static bool eap_ttls_phase2_eap_init(struct eap_state *eap) 850 { 851 struct phase2_method *phase2 = eap_tls_common_get_variant_data(eap); 852 uint8_t packet[5] = { EAP_CODE_REQUEST, 0, 0, 5, EAP_TYPE_IDENTITY }; 853 854 if (!phase2->state) 855 return false; 856 /* 857 * Consume a fake Request/Identity packet so that the EAP instance 858 * starts with its Response/Identity right away. 859 */ 860 eap_rx_packet(phase2->state, packet, sizeof(packet)); 861 862 return true; 863 } 864 865 static bool eap_ttls_phase2_eap_handle_avp(struct eap_state *eap, 866 enum radius_attr type, 867 uint32_t vendor_id, 868 const uint8_t *data, 869 size_t len) 870 { 871 struct phase2_method *phase2 = eap_tls_common_get_variant_data(eap); 872 873 if (type != RADIUS_ATTR_EAP_MESSAGE) 874 return false; 875 876 eap_rx_packet(phase2->state, data, len); 877 878 return true; 879 } 880 881 static void eap_ttls_phase2_eap_destroy(struct phase2_method *phase2) 882 { 883 if (!phase2->state) 884 return; 885 886 eap_reset(phase2->state); 887 eap_free(phase2->state); 888 } 889 890 static void eap_ttls_phase2_eap_reset(struct phase2_method *phase2) 891 { 892 if (!phase2->state) 893 return; 894 895 eap_reset(phase2->state); 896 } 897 898 static const struct phase2_method_ops phase2_eap_ops = { 899 .init = eap_ttls_phase2_eap_init, 900 .handle_avp = eap_ttls_phase2_eap_handle_avp, 901 .destroy = eap_ttls_phase2_eap_destroy, 902 .reset = eap_ttls_phase2_eap_reset, 903 }; 904 905 static bool eap_ttls_tunnel_ready(struct eap_state *eap, 906 const char *peer_identity, 907 bool resumed) 908 { 909 struct phase2_method *phase2 = eap_tls_common_get_variant_data(eap); 910 uint8_t msk_emsk[128]; 911 912 /* 913 * TTLSv0 seems to assume that the TLS handshake phase authenticates 914 * the server to the client enough that the inner method success or 915 * failure status doesn't matter as long as the server lets us in, 916 * although in various places it says the client may also have a 917 * specific policy. 918 */ 919 eap_method_success(eap); 920 921 /* MSK, EMSK and challenge derivation */ 922 eap_tls_common_tunnel_prf_get_bytes(eap, true, "ttls keying material", 923 msk_emsk, 128); 924 925 eap_set_key_material(eap, msk_emsk + 0, 64, msk_emsk + 64, 64, NULL, 0, 926 NULL, 0); 927 explicit_bzero(msk_emsk, sizeof(msk_emsk)); 928 929 if (phase2->ops->init) 930 return phase2->ops->init(eap); 931 932 return true; 933 } 934 935 static bool eap_ttls_tunnel_handle_request(struct eap_state *eap, 936 const uint8_t *data, 937 size_t data_len) 938 { 939 struct phase2_method *phase2 = eap_tls_common_get_variant_data(eap); 940 struct avp_iter iter; 941 942 if (!phase2->ops->handle_avp) 943 return true; 944 945 avp_iter_init(&iter, data, data_len); 946 947 while (avp_iter_next(&iter)) { 948 if (phase2->ops->handle_avp(eap, iter.type, iter.vendor_id, 949 iter.data, iter.len)) 950 continue; 951 952 if (iter.flags & TTLS_AVP_FLAG_M) 953 return false; 954 } 955 956 return true; 957 } 958 959 static void eap_ttls_state_reset(void *data) 960 { 961 struct phase2_method *phase2 = data; 962 963 if (!phase2->ops->reset) 964 return; 965 966 phase2->ops->reset(phase2); 967 } 968 969 static void eap_ttls_state_destroy(void *data) 970 { 971 struct phase2_method *phase2 = data; 972 973 eap_ttls_phase2_credentials_destroy(&phase2->credentials); 974 975 if (phase2->ops->destroy) 976 phase2->ops->destroy(phase2); 977 978 l_free(phase2); 979 } 980 981 static const struct { 982 const char *name; 983 const struct phase2_method_ops *method_ops; 984 } tunneled_non_eap_method_ops[] = { 985 { "Tunneled-CHAP", &phase2_chap_ops }, 986 { "Tunneled-MSCHAP", &phase2_mschap_ops }, 987 { "Tunneled-MSCHAPv2", &phase2_mschapv2_ops }, 988 { "Tunneled-PAP", &phase2_pap_ops }, 989 { } 990 }; 991 992 static int eap_ttls_check_tunneled_auth_settings(struct l_settings *settings, 993 struct l_queue *secrets, 994 const char *prefix, 995 struct l_queue **out_missing) 996 { 997 const struct eap_secret_info *secret; 998 char identity_key[128]; 999 char password_key[128]; 1000 1001 L_AUTO_FREE_VAR(char *, identity); 1002 L_AUTO_FREE_VAR(char *, password) = NULL; 1003 1004 snprintf(identity_key, sizeof(identity_key), "%sIdentity", prefix); 1005 snprintf(password_key, sizeof(password_key), "%sPassword", prefix); 1006 1007 identity = l_settings_get_string(settings, "Security", identity_key); 1008 1009 if (!identity) { 1010 secret = l_queue_find(secrets, eap_secret_info_match, 1011 identity_key); 1012 if (!secret) { 1013 eap_append_secret(out_missing, 1014 EAP_SECRET_REMOTE_USER_PASSWORD, 1015 identity_key, password_key, NULL, 1016 EAP_CACHE_TEMPORARY); 1017 } 1018 1019 return 0; 1020 } 1021 1022 password = l_settings_get_string(settings, "Security", password_key); 1023 1024 if (!password) { 1025 secret = l_queue_find(secrets, eap_secret_info_match, 1026 password_key); 1027 if (!secret) { 1028 eap_append_secret(out_missing, 1029 EAP_SECRET_REMOTE_PASSWORD, 1030 password_key, NULL, identity, 1031 EAP_CACHE_TEMPORARY); 1032 } 1033 } else 1034 explicit_bzero(password, strlen(password)); 1035 1036 return 0; 1037 } 1038 1039 static int eap_ttls_settings_check(struct l_settings *settings, 1040 struct l_queue *secrets, 1041 const char *prefix, 1042 struct l_queue **out_missing) 1043 { 1044 char setting_key[72]; 1045 char setting_prefix[72]; 1046 const char *phase2_method_name; 1047 uint8_t i; 1048 int r; 1049 1050 snprintf(setting_prefix, sizeof(setting_prefix), "%sTTLS-", prefix); 1051 r = eap_tls_common_settings_check(settings, secrets, setting_prefix, 1052 out_missing); 1053 if (r) 1054 return r; 1055 1056 snprintf(setting_key, sizeof(setting_key), "%sTTLS-Phase2-Method", 1057 prefix); 1058 phase2_method_name = l_settings_get_value(settings, "Security", 1059 setting_key); 1060 if (!phase2_method_name) { 1061 l_error("Setting %s is missing", setting_key); 1062 return -ENOENT; 1063 } 1064 1065 snprintf(setting_prefix, sizeof(setting_prefix), "%sTTLS-Phase2-", 1066 prefix); 1067 1068 for (i = 0; tunneled_non_eap_method_ops[i].name; i++) { 1069 if (strcmp(tunneled_non_eap_method_ops[i].name, 1070 phase2_method_name)) 1071 continue; 1072 1073 return eap_ttls_check_tunneled_auth_settings(settings, secrets, 1074 setting_prefix, 1075 out_missing); 1076 } 1077 1078 return __eap_check_settings(settings, secrets, setting_prefix, false, 1079 out_missing); 1080 } 1081 1082 static const struct eap_tls_variant_ops eap_ttls_ops = { 1083 .version_max_supported = EAP_TLS_VERSION_0, 1084 .tunnel_ready = eap_ttls_tunnel_ready, 1085 .tunnel_handle_request = eap_ttls_tunnel_handle_request, 1086 .reset = eap_ttls_state_reset, 1087 .destroy = eap_ttls_state_destroy, 1088 }; 1089 1090 static bool eap_ttls_settings_load(struct eap_state *eap, 1091 struct l_settings *settings, 1092 const char *prefix) 1093 { 1094 struct phase2_method *phase2; 1095 const char *phase2_method_name; 1096 char setting[72]; 1097 uint8_t i; 1098 1099 snprintf(setting, sizeof(setting), "%sTTLS-Phase2-Method", prefix); 1100 phase2_method_name = l_settings_get_value(settings, "Security", 1101 setting); 1102 if (!phase2_method_name) 1103 return false; 1104 1105 phase2 = l_new(struct phase2_method, 1); 1106 1107 snprintf(setting, sizeof(setting), "%sTTLS-Phase2-", prefix); 1108 1109 for (i = 0; tunneled_non_eap_method_ops[i].name; i++) { 1110 if (strcmp(tunneled_non_eap_method_ops[i].name, 1111 phase2_method_name)) 1112 continue; 1113 1114 phase2->ops = tunneled_non_eap_method_ops[i].method_ops; 1115 1116 if (!eap_ttls_phase2_non_eap_load_settings(phase2, settings, 1117 setting)) 1118 goto error; 1119 1120 break; 1121 } 1122 1123 if (!phase2->ops) { 1124 phase2->ops = &phase2_eap_ops; 1125 1126 if (!eap_ttls_phase2_eap_load_settings(eap, phase2, settings, 1127 setting)) 1128 goto error; 1129 } 1130 1131 snprintf(setting, sizeof(setting), "%sTTLS-", prefix); 1132 1133 if (!eap_tls_common_settings_load(eap, settings, setting, 1134 &eap_ttls_ops, phase2)) 1135 goto error; 1136 1137 return true; 1138 error: 1139 l_free(phase2); 1140 1141 return false; 1142 } 1143 1144 static struct eap_method eap_ttls = { 1145 .request_type = EAP_TYPE_TTLS, 1146 .exports_msk = true, 1147 .name = "TTLS", 1148 1149 .handle_request = eap_tls_common_handle_request, 1150 .handle_retransmit = eap_tls_common_handle_retransmit, 1151 .reset_state = eap_tls_common_state_reset, 1152 .free = eap_tls_common_state_free, 1153 1154 .check_settings = eap_ttls_settings_check, 1155 .load_settings = eap_ttls_settings_load, 1156 }; 1157 1158 static int eap_ttls_init(void) 1159 { 1160 l_debug(""); 1161 return eap_register_method(&eap_ttls); 1162 } 1163 1164 static void eap_ttls_exit(void) 1165 { 1166 l_debug(""); 1167 eap_unregister_method(&eap_ttls); 1168 } 1169 1170 EAP_METHOD_BUILTIN(eap_ttls, eap_ttls_init, eap_ttls_exit)