eap_fast.c revision 1f69aa52ea2e0a73ac502565df8c666ee49cab6a
1/* 2 * EAP peer method: EAP-FAST (RFC 4851) 3 * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi> 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License version 2 as 7 * published by the Free Software Foundation. 8 * 9 * Alternatively, this software may be distributed under the terms of BSD 10 * license. 11 * 12 * See README and COPYING for more details. 13 */ 14 15#include "includes.h" 16 17#include "common.h" 18#include "crypto/tls.h" 19#include "crypto/sha1.h" 20#include "eap_common/eap_tlv_common.h" 21#include "eap_i.h" 22#include "eap_tls_common.h" 23#include "eap_config.h" 24#include "eap_fast_pac.h" 25 26#ifdef EAP_FAST_DYNAMIC 27#include "eap_fast_pac.c" 28#endif /* EAP_FAST_DYNAMIC */ 29 30/* TODO: 31 * - test session resumption and enable it if it interoperates 32 * - password change (pending mschapv2 packet; replay decrypted packet) 33 */ 34 35 36static void eap_fast_deinit(struct eap_sm *sm, void *priv); 37 38 39struct eap_fast_data { 40 struct eap_ssl_data ssl; 41 42 int fast_version; 43 44 const struct eap_method *phase2_method; 45 void *phase2_priv; 46 int phase2_success; 47 48 struct eap_method_type phase2_type; 49 struct eap_method_type *phase2_types; 50 size_t num_phase2_types; 51 int resuming; /* starting a resumed session */ 52 struct eap_fast_key_block_provisioning *key_block_p; 53#define EAP_FAST_PROV_UNAUTH 1 54#define EAP_FAST_PROV_AUTH 2 55 int provisioning_allowed; /* Allowed PAC provisioning modes */ 56 int provisioning; /* doing PAC provisioning (not the normal auth) */ 57 int anon_provisioning; /* doing anonymous (unauthenticated) 58 * provisioning */ 59 int session_ticket_used; 60 61 u8 key_data[EAP_FAST_KEY_LEN]; 62 u8 emsk[EAP_EMSK_LEN]; 63 int success; 64 65 struct eap_fast_pac *pac; 66 struct eap_fast_pac *current_pac; 67 size_t max_pac_list_len; 68 int use_pac_binary_format; 69 70 u8 simck[EAP_FAST_SIMCK_LEN]; 71 int simck_idx; 72 73 struct wpabuf *pending_phase2_req; 74}; 75 76 77static int eap_fast_session_ticket_cb(void *ctx, const u8 *ticket, size_t len, 78 const u8 *client_random, 79 const u8 *server_random, 80 u8 *master_secret) 81{ 82 struct eap_fast_data *data = ctx; 83 84 wpa_printf(MSG_DEBUG, "EAP-FAST: SessionTicket callback"); 85 86 if (client_random == NULL || server_random == NULL || 87 master_secret == NULL) { 88 wpa_printf(MSG_DEBUG, "EAP-FAST: SessionTicket failed - fall " 89 "back to full TLS handshake"); 90 data->session_ticket_used = 0; 91 if (data->provisioning_allowed) { 92 wpa_printf(MSG_DEBUG, "EAP-FAST: Try to provision a " 93 "new PAC-Key"); 94 data->provisioning = 1; 95 data->current_pac = NULL; 96 } 97 return 0; 98 } 99 100 wpa_hexdump(MSG_DEBUG, "EAP-FAST: SessionTicket", ticket, len); 101 102 if (data->current_pac == NULL) { 103 wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC-Key available for " 104 "using SessionTicket"); 105 data->session_ticket_used = 0; 106 return 0; 107 } 108 109 eap_fast_derive_master_secret(data->current_pac->pac_key, 110 server_random, client_random, 111 master_secret); 112 113 data->session_ticket_used = 1; 114 115 return 1; 116} 117 118 119static int eap_fast_parse_phase1(struct eap_fast_data *data, 120 const char *phase1) 121{ 122 const char *pos; 123 124 pos = os_strstr(phase1, "fast_provisioning="); 125 if (pos) { 126 data->provisioning_allowed = atoi(pos + 18); 127 wpa_printf(MSG_DEBUG, "EAP-FAST: Automatic PAC provisioning " 128 "mode: %d", data->provisioning_allowed); 129 } 130 131 pos = os_strstr(phase1, "fast_max_pac_list_len="); 132 if (pos) { 133 data->max_pac_list_len = atoi(pos + 22); 134 if (data->max_pac_list_len == 0) 135 data->max_pac_list_len = 1; 136 wpa_printf(MSG_DEBUG, "EAP-FAST: Maximum PAC list length: %lu", 137 (unsigned long) data->max_pac_list_len); 138 } 139 140 pos = os_strstr(phase1, "fast_pac_format=binary"); 141 if (pos) { 142 data->use_pac_binary_format = 1; 143 wpa_printf(MSG_DEBUG, "EAP-FAST: Using binary format for PAC " 144 "list"); 145 } 146 147 return 0; 148} 149 150 151static void * eap_fast_init(struct eap_sm *sm) 152{ 153 struct eap_fast_data *data; 154 struct eap_peer_config *config = eap_get_config(sm); 155 156 data = os_zalloc(sizeof(*data)); 157 if (data == NULL) 158 return NULL; 159 data->fast_version = EAP_FAST_VERSION; 160 data->max_pac_list_len = 10; 161 162 if (config && config->phase1 && 163 eap_fast_parse_phase1(data, config->phase1) < 0) { 164 eap_fast_deinit(sm, data); 165 return NULL; 166 } 167 168 if (eap_peer_select_phase2_methods(config, "auth=", 169 &data->phase2_types, 170 &data->num_phase2_types) < 0) { 171 eap_fast_deinit(sm, data); 172 return NULL; 173 } 174 175 data->phase2_type.vendor = EAP_VENDOR_IETF; 176 data->phase2_type.method = EAP_TYPE_NONE; 177 178 if (eap_peer_tls_ssl_init(sm, &data->ssl, config)) { 179 wpa_printf(MSG_INFO, "EAP-FAST: Failed to initialize SSL."); 180 eap_fast_deinit(sm, data); 181 return NULL; 182 } 183 184 if (tls_connection_set_session_ticket_cb(sm->ssl_ctx, data->ssl.conn, 185 eap_fast_session_ticket_cb, 186 data) < 0) { 187 wpa_printf(MSG_INFO, "EAP-FAST: Failed to set SessionTicket " 188 "callback"); 189 eap_fast_deinit(sm, data); 190 return NULL; 191 } 192 193 /* 194 * The local RADIUS server in a Cisco AP does not seem to like empty 195 * fragments before data, so disable that workaround for CBC. 196 * TODO: consider making this configurable 197 */ 198 if (tls_connection_enable_workaround(sm->ssl_ctx, data->ssl.conn)) { 199 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to enable TLS " 200 "workarounds"); 201 } 202 203 if (data->use_pac_binary_format && 204 eap_fast_load_pac_bin(sm, &data->pac, config->pac_file) < 0) { 205 eap_fast_deinit(sm, data); 206 return NULL; 207 } 208 209 if (!data->use_pac_binary_format && 210 eap_fast_load_pac(sm, &data->pac, config->pac_file) < 0) { 211 eap_fast_deinit(sm, data); 212 return NULL; 213 } 214 eap_fast_pac_list_truncate(data->pac, data->max_pac_list_len); 215 216 if (data->pac == NULL && !data->provisioning_allowed) { 217 wpa_printf(MSG_INFO, "EAP-FAST: No PAC configured and " 218 "provisioning disabled"); 219 eap_fast_deinit(sm, data); 220 return NULL; 221 } 222 223 return data; 224} 225 226 227static void eap_fast_deinit(struct eap_sm *sm, void *priv) 228{ 229 struct eap_fast_data *data = priv; 230 struct eap_fast_pac *pac, *prev; 231 232 if (data == NULL) 233 return; 234 if (data->phase2_priv && data->phase2_method) 235 data->phase2_method->deinit(sm, data->phase2_priv); 236 os_free(data->phase2_types); 237 os_free(data->key_block_p); 238 eap_peer_tls_ssl_deinit(sm, &data->ssl); 239 240 pac = data->pac; 241 prev = NULL; 242 while (pac) { 243 prev = pac; 244 pac = pac->next; 245 eap_fast_free_pac(prev); 246 } 247 wpabuf_free(data->pending_phase2_req); 248 os_free(data); 249} 250 251 252static int eap_fast_derive_msk(struct eap_fast_data *data) 253{ 254 eap_fast_derive_eap_msk(data->simck, data->key_data); 255 eap_fast_derive_eap_emsk(data->simck, data->emsk); 256 data->success = 1; 257 return 0; 258} 259 260 261static void eap_fast_derive_key_auth(struct eap_sm *sm, 262 struct eap_fast_data *data) 263{ 264 u8 *sks; 265 266 /* RFC 4851, Section 5.1: 267 * Extra key material after TLS key_block: session_key_seed[40] 268 */ 269 270 sks = eap_fast_derive_key(sm->ssl_ctx, data->ssl.conn, "key expansion", 271 EAP_FAST_SKS_LEN); 272 if (sks == NULL) { 273 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive " 274 "session_key_seed"); 275 return; 276 } 277 278 /* 279 * RFC 4851, Section 5.2: 280 * S-IMCK[0] = session_key_seed 281 */ 282 wpa_hexdump_key(MSG_DEBUG, 283 "EAP-FAST: session_key_seed (SKS = S-IMCK[0])", 284 sks, EAP_FAST_SKS_LEN); 285 data->simck_idx = 0; 286 os_memcpy(data->simck, sks, EAP_FAST_SIMCK_LEN); 287 os_free(sks); 288} 289 290 291static void eap_fast_derive_key_provisioning(struct eap_sm *sm, 292 struct eap_fast_data *data) 293{ 294 os_free(data->key_block_p); 295 data->key_block_p = (struct eap_fast_key_block_provisioning *) 296 eap_fast_derive_key(sm->ssl_ctx, data->ssl.conn, 297 "key expansion", 298 sizeof(*data->key_block_p)); 299 if (data->key_block_p == NULL) { 300 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive key block"); 301 return; 302 } 303 /* 304 * RFC 4851, Section 5.2: 305 * S-IMCK[0] = session_key_seed 306 */ 307 wpa_hexdump_key(MSG_DEBUG, 308 "EAP-FAST: session_key_seed (SKS = S-IMCK[0])", 309 data->key_block_p->session_key_seed, 310 sizeof(data->key_block_p->session_key_seed)); 311 data->simck_idx = 0; 312 os_memcpy(data->simck, data->key_block_p->session_key_seed, 313 EAP_FAST_SIMCK_LEN); 314 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: server_challenge", 315 data->key_block_p->server_challenge, 316 sizeof(data->key_block_p->server_challenge)); 317 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: client_challenge", 318 data->key_block_p->client_challenge, 319 sizeof(data->key_block_p->client_challenge)); 320} 321 322 323static void eap_fast_derive_keys(struct eap_sm *sm, struct eap_fast_data *data) 324{ 325 if (data->anon_provisioning) 326 eap_fast_derive_key_provisioning(sm, data); 327 else 328 eap_fast_derive_key_auth(sm, data); 329} 330 331 332static int eap_fast_init_phase2_method(struct eap_sm *sm, 333 struct eap_fast_data *data) 334{ 335 data->phase2_method = 336 eap_peer_get_eap_method(data->phase2_type.vendor, 337 data->phase2_type.method); 338 if (data->phase2_method == NULL) 339 return -1; 340 341 if (data->key_block_p) { 342 sm->auth_challenge = data->key_block_p->server_challenge; 343 sm->peer_challenge = data->key_block_p->client_challenge; 344 } 345 sm->init_phase2 = 1; 346 data->phase2_priv = data->phase2_method->init(sm); 347 sm->init_phase2 = 0; 348 sm->auth_challenge = NULL; 349 sm->peer_challenge = NULL; 350 351 return data->phase2_priv == NULL ? -1 : 0; 352} 353 354 355static int eap_fast_select_phase2_method(struct eap_fast_data *data, u8 type) 356{ 357 size_t i; 358 359 /* TODO: TNC with anonymous provisioning; need to require both 360 * completed MSCHAPv2 and TNC */ 361 362 if (data->anon_provisioning && type != EAP_TYPE_MSCHAPV2) { 363 wpa_printf(MSG_INFO, "EAP-FAST: Only EAP-MSCHAPv2 is allowed " 364 "during unauthenticated provisioning; reject phase2" 365 " type %d", type); 366 return -1; 367 } 368 369#ifdef EAP_TNC 370 if (type == EAP_TYPE_TNC) { 371 data->phase2_type.vendor = EAP_VENDOR_IETF; 372 data->phase2_type.method = EAP_TYPE_TNC; 373 wpa_printf(MSG_DEBUG, "EAP-FAST: Selected Phase 2 EAP " 374 "vendor %d method %d for TNC", 375 data->phase2_type.vendor, 376 data->phase2_type.method); 377 return 0; 378 } 379#endif /* EAP_TNC */ 380 381 for (i = 0; i < data->num_phase2_types; i++) { 382 if (data->phase2_types[i].vendor != EAP_VENDOR_IETF || 383 data->phase2_types[i].method != type) 384 continue; 385 386 data->phase2_type.vendor = data->phase2_types[i].vendor; 387 data->phase2_type.method = data->phase2_types[i].method; 388 wpa_printf(MSG_DEBUG, "EAP-FAST: Selected Phase 2 EAP " 389 "vendor %d method %d", 390 data->phase2_type.vendor, 391 data->phase2_type.method); 392 break; 393 } 394 395 if (type != data->phase2_type.method || type == EAP_TYPE_NONE) 396 return -1; 397 398 return 0; 399} 400 401 402static int eap_fast_phase2_request(struct eap_sm *sm, 403 struct eap_fast_data *data, 404 struct eap_method_ret *ret, 405 struct eap_hdr *hdr, 406 struct wpabuf **resp) 407{ 408 size_t len = be_to_host16(hdr->length); 409 u8 *pos; 410 struct eap_method_ret iret; 411 struct eap_peer_config *config = eap_get_config(sm); 412 struct wpabuf msg; 413 414 if (len <= sizeof(struct eap_hdr)) { 415 wpa_printf(MSG_INFO, "EAP-FAST: too short " 416 "Phase 2 request (len=%lu)", (unsigned long) len); 417 return -1; 418 } 419 pos = (u8 *) (hdr + 1); 420 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 Request: type=%d", *pos); 421 if (*pos == EAP_TYPE_IDENTITY) { 422 *resp = eap_sm_buildIdentity(sm, hdr->identifier, 1); 423 return 0; 424 } 425 426 if (data->phase2_priv && data->phase2_method && 427 *pos != data->phase2_type.method) { 428 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 EAP sequence - " 429 "deinitialize previous method"); 430 data->phase2_method->deinit(sm, data->phase2_priv); 431 data->phase2_method = NULL; 432 data->phase2_priv = NULL; 433 data->phase2_type.vendor = EAP_VENDOR_IETF; 434 data->phase2_type.method = EAP_TYPE_NONE; 435 } 436 437 if (data->phase2_type.vendor == EAP_VENDOR_IETF && 438 data->phase2_type.method == EAP_TYPE_NONE && 439 eap_fast_select_phase2_method(data, *pos) < 0) { 440 if (eap_peer_tls_phase2_nak(data->phase2_types, 441 data->num_phase2_types, 442 hdr, resp)) 443 return -1; 444 return 0; 445 } 446 447 if ((data->phase2_priv == NULL && 448 eap_fast_init_phase2_method(sm, data) < 0) || 449 data->phase2_method == NULL) { 450 wpa_printf(MSG_INFO, "EAP-FAST: Failed to initialize " 451 "Phase 2 EAP method %d", *pos); 452 ret->methodState = METHOD_DONE; 453 ret->decision = DECISION_FAIL; 454 return -1; 455 } 456 457 os_memset(&iret, 0, sizeof(iret)); 458 wpabuf_set(&msg, hdr, len); 459 *resp = data->phase2_method->process(sm, data->phase2_priv, &iret, 460 &msg); 461 if (*resp == NULL || 462 (iret.methodState == METHOD_DONE && 463 iret.decision == DECISION_FAIL)) { 464 ret->methodState = METHOD_DONE; 465 ret->decision = DECISION_FAIL; 466 } else if ((iret.methodState == METHOD_DONE || 467 iret.methodState == METHOD_MAY_CONT) && 468 (iret.decision == DECISION_UNCOND_SUCC || 469 iret.decision == DECISION_COND_SUCC)) { 470 data->phase2_success = 1; 471 } 472 473 if (*resp == NULL && config && 474 (config->pending_req_identity || config->pending_req_password || 475 config->pending_req_otp || config->pending_req_new_password)) { 476 wpabuf_free(data->pending_phase2_req); 477 data->pending_phase2_req = wpabuf_alloc_copy(hdr, len); 478 } else if (*resp == NULL) 479 return -1; 480 481 return 0; 482} 483 484 485static struct wpabuf * eap_fast_tlv_nak(int vendor_id, int tlv_type) 486{ 487 struct wpabuf *buf; 488 struct eap_tlv_nak_tlv *nak; 489 buf = wpabuf_alloc(sizeof(*nak)); 490 if (buf == NULL) 491 return NULL; 492 nak = wpabuf_put(buf, sizeof(*nak)); 493 nak->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY | EAP_TLV_NAK_TLV); 494 nak->length = host_to_be16(6); 495 nak->vendor_id = host_to_be32(vendor_id); 496 nak->nak_type = host_to_be16(tlv_type); 497 return buf; 498} 499 500 501static struct wpabuf * eap_fast_tlv_result(int status, int intermediate) 502{ 503 struct wpabuf *buf; 504 struct eap_tlv_intermediate_result_tlv *result; 505 buf = wpabuf_alloc(sizeof(*result)); 506 if (buf == NULL) 507 return NULL; 508 wpa_printf(MSG_DEBUG, "EAP-FAST: Add %sResult TLV(status=%d)", 509 intermediate ? "Intermediate " : "", status); 510 result = wpabuf_put(buf, sizeof(*result)); 511 result->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY | 512 (intermediate ? 513 EAP_TLV_INTERMEDIATE_RESULT_TLV : 514 EAP_TLV_RESULT_TLV)); 515 result->length = host_to_be16(2); 516 result->status = host_to_be16(status); 517 return buf; 518} 519 520 521static struct wpabuf * eap_fast_tlv_pac_ack(void) 522{ 523 struct wpabuf *buf; 524 struct eap_tlv_result_tlv *res; 525 struct eap_tlv_pac_ack_tlv *ack; 526 527 buf = wpabuf_alloc(sizeof(*res) + sizeof(*ack)); 528 if (buf == NULL) 529 return NULL; 530 531 wpa_printf(MSG_DEBUG, "EAP-FAST: Add PAC TLV (ack)"); 532 ack = wpabuf_put(buf, sizeof(*ack)); 533 ack->tlv_type = host_to_be16(EAP_TLV_PAC_TLV | 534 EAP_TLV_TYPE_MANDATORY); 535 ack->length = host_to_be16(sizeof(*ack) - sizeof(struct eap_tlv_hdr)); 536 ack->pac_type = host_to_be16(PAC_TYPE_PAC_ACKNOWLEDGEMENT); 537 ack->pac_len = host_to_be16(2); 538 ack->result = host_to_be16(EAP_TLV_RESULT_SUCCESS); 539 540 return buf; 541} 542 543 544static struct wpabuf * eap_fast_process_eap_payload_tlv( 545 struct eap_sm *sm, struct eap_fast_data *data, 546 struct eap_method_ret *ret, 547 u8 *eap_payload_tlv, size_t eap_payload_tlv_len) 548{ 549 struct eap_hdr *hdr; 550 struct wpabuf *resp = NULL; 551 552 if (eap_payload_tlv_len < sizeof(*hdr)) { 553 wpa_printf(MSG_DEBUG, "EAP-FAST: too short EAP " 554 "Payload TLV (len=%lu)", 555 (unsigned long) eap_payload_tlv_len); 556 return NULL; 557 } 558 559 hdr = (struct eap_hdr *) eap_payload_tlv; 560 if (be_to_host16(hdr->length) > eap_payload_tlv_len) { 561 wpa_printf(MSG_DEBUG, "EAP-FAST: EAP packet overflow in " 562 "EAP Payload TLV"); 563 return NULL; 564 } 565 566 if (hdr->code != EAP_CODE_REQUEST) { 567 wpa_printf(MSG_INFO, "EAP-FAST: Unexpected code=%d in " 568 "Phase 2 EAP header", hdr->code); 569 return NULL; 570 } 571 572 if (eap_fast_phase2_request(sm, data, ret, hdr, &resp)) { 573 wpa_printf(MSG_INFO, "EAP-FAST: Phase2 Request processing " 574 "failed"); 575 return NULL; 576 } 577 578 return eap_fast_tlv_eap_payload(resp); 579} 580 581 582static int eap_fast_validate_crypto_binding( 583 struct eap_tlv_crypto_binding_tlv *_bind) 584{ 585 wpa_printf(MSG_DEBUG, "EAP-FAST: Crypto-Binding TLV: Version %d " 586 "Received Version %d SubType %d", 587 _bind->version, _bind->received_version, _bind->subtype); 588 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE", 589 _bind->nonce, sizeof(_bind->nonce)); 590 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC", 591 _bind->compound_mac, sizeof(_bind->compound_mac)); 592 593 if (_bind->version != EAP_FAST_VERSION || 594 _bind->received_version != EAP_FAST_VERSION || 595 _bind->subtype != EAP_TLV_CRYPTO_BINDING_SUBTYPE_REQUEST) { 596 wpa_printf(MSG_INFO, "EAP-FAST: Invalid version/subtype in " 597 "Crypto-Binding TLV: Version %d " 598 "Received Version %d SubType %d", 599 _bind->version, _bind->received_version, 600 _bind->subtype); 601 return -1; 602 } 603 604 return 0; 605} 606 607 608static void eap_fast_write_crypto_binding( 609 struct eap_tlv_crypto_binding_tlv *rbind, 610 struct eap_tlv_crypto_binding_tlv *_bind, const u8 *cmk) 611{ 612 rbind->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY | 613 EAP_TLV_CRYPTO_BINDING_TLV); 614 rbind->length = host_to_be16(sizeof(*rbind) - 615 sizeof(struct eap_tlv_hdr)); 616 rbind->version = EAP_FAST_VERSION; 617 rbind->received_version = _bind->version; 618 rbind->subtype = EAP_TLV_CRYPTO_BINDING_SUBTYPE_RESPONSE; 619 os_memcpy(rbind->nonce, _bind->nonce, sizeof(_bind->nonce)); 620 inc_byte_array(rbind->nonce, sizeof(rbind->nonce)); 621 hmac_sha1(cmk, EAP_FAST_CMK_LEN, (u8 *) rbind, sizeof(*rbind), 622 rbind->compound_mac); 623 624 wpa_printf(MSG_DEBUG, "EAP-FAST: Reply Crypto-Binding TLV: Version %d " 625 "Received Version %d SubType %d", 626 rbind->version, rbind->received_version, rbind->subtype); 627 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE", 628 rbind->nonce, sizeof(rbind->nonce)); 629 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC", 630 rbind->compound_mac, sizeof(rbind->compound_mac)); 631} 632 633 634static int eap_fast_get_phase2_key(struct eap_sm *sm, 635 struct eap_fast_data *data, 636 u8 *isk, size_t isk_len) 637{ 638 u8 *key; 639 size_t key_len; 640 641 os_memset(isk, 0, isk_len); 642 643 if (data->phase2_method == NULL || data->phase2_priv == NULL) { 644 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 method not " 645 "available"); 646 return -1; 647 } 648 649 if (data->phase2_method->isKeyAvailable == NULL || 650 data->phase2_method->getKey == NULL) 651 return 0; 652 653 if (!data->phase2_method->isKeyAvailable(sm, data->phase2_priv) || 654 (key = data->phase2_method->getKey(sm, data->phase2_priv, 655 &key_len)) == NULL) { 656 wpa_printf(MSG_DEBUG, "EAP-FAST: Could not get key material " 657 "from Phase 2"); 658 return -1; 659 } 660 661 if (key_len > isk_len) 662 key_len = isk_len; 663 if (key_len == 32 && 664 data->phase2_method->vendor == EAP_VENDOR_IETF && 665 data->phase2_method->method == EAP_TYPE_MSCHAPV2) { 666 /* 667 * EAP-FAST uses reverse order for MS-MPPE keys when deriving 668 * MSK from EAP-MSCHAPv2. Swap the keys here to get the correct 669 * ISK for EAP-FAST cryptobinding. 670 */ 671 os_memcpy(isk, key + 16, 16); 672 os_memcpy(isk + 16, key, 16); 673 } else 674 os_memcpy(isk, key, key_len); 675 os_free(key); 676 677 return 0; 678} 679 680 681static int eap_fast_get_cmk(struct eap_sm *sm, struct eap_fast_data *data, 682 u8 *cmk) 683{ 684 u8 isk[32], imck[60]; 685 686 wpa_printf(MSG_DEBUG, "EAP-FAST: Determining CMK[%d] for Compound MIC " 687 "calculation", data->simck_idx + 1); 688 689 /* 690 * RFC 4851, Section 5.2: 691 * IMCK[j] = T-PRF(S-IMCK[j-1], "Inner Methods Compound Keys", 692 * MSK[j], 60) 693 * S-IMCK[j] = first 40 octets of IMCK[j] 694 * CMK[j] = last 20 octets of IMCK[j] 695 */ 696 697 if (eap_fast_get_phase2_key(sm, data, isk, sizeof(isk)) < 0) 698 return -1; 699 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: ISK[j]", isk, sizeof(isk)); 700 sha1_t_prf(data->simck, EAP_FAST_SIMCK_LEN, 701 "Inner Methods Compound Keys", 702 isk, sizeof(isk), imck, sizeof(imck)); 703 data->simck_idx++; 704 os_memcpy(data->simck, imck, EAP_FAST_SIMCK_LEN); 705 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: S-IMCK[j]", 706 data->simck, EAP_FAST_SIMCK_LEN); 707 os_memcpy(cmk, imck + EAP_FAST_SIMCK_LEN, EAP_FAST_CMK_LEN); 708 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: CMK[j]", 709 cmk, EAP_FAST_CMK_LEN); 710 711 return 0; 712} 713 714 715static u8 * eap_fast_write_pac_request(u8 *pos, u16 pac_type) 716{ 717 struct eap_tlv_hdr *pac; 718 struct eap_tlv_request_action_tlv *act; 719 struct eap_tlv_pac_type_tlv *type; 720 721 act = (struct eap_tlv_request_action_tlv *) pos; 722 act->tlv_type = host_to_be16(EAP_TLV_REQUEST_ACTION_TLV); 723 act->length = host_to_be16(2); 724 act->action = host_to_be16(EAP_TLV_ACTION_PROCESS_TLV); 725 726 pac = (struct eap_tlv_hdr *) (act + 1); 727 pac->tlv_type = host_to_be16(EAP_TLV_PAC_TLV); 728 pac->length = host_to_be16(sizeof(*type)); 729 730 type = (struct eap_tlv_pac_type_tlv *) (pac + 1); 731 type->tlv_type = host_to_be16(PAC_TYPE_PAC_TYPE); 732 type->length = host_to_be16(2); 733 type->pac_type = host_to_be16(pac_type); 734 735 return (u8 *) (type + 1); 736} 737 738 739static struct wpabuf * eap_fast_process_crypto_binding( 740 struct eap_sm *sm, struct eap_fast_data *data, 741 struct eap_method_ret *ret, 742 struct eap_tlv_crypto_binding_tlv *_bind, size_t bind_len) 743{ 744 struct wpabuf *resp; 745 u8 *pos; 746 u8 cmk[EAP_FAST_CMK_LEN], cmac[SHA1_MAC_LEN]; 747 int res; 748 size_t len; 749 750 if (eap_fast_validate_crypto_binding(_bind) < 0) 751 return NULL; 752 753 if (eap_fast_get_cmk(sm, data, cmk) < 0) 754 return NULL; 755 756 /* Validate received Compound MAC */ 757 os_memcpy(cmac, _bind->compound_mac, sizeof(cmac)); 758 os_memset(_bind->compound_mac, 0, sizeof(cmac)); 759 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Crypto-Binding TLV for Compound " 760 "MAC calculation", (u8 *) _bind, bind_len); 761 hmac_sha1(cmk, EAP_FAST_CMK_LEN, (u8 *) _bind, bind_len, 762 _bind->compound_mac); 763 res = os_memcmp(cmac, _bind->compound_mac, sizeof(cmac)); 764 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Received Compound MAC", 765 cmac, sizeof(cmac)); 766 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Calculated Compound MAC", 767 _bind->compound_mac, sizeof(cmac)); 768 if (res != 0) { 769 wpa_printf(MSG_INFO, "EAP-FAST: Compound MAC did not match"); 770 os_memcpy(_bind->compound_mac, cmac, sizeof(cmac)); 771 return NULL; 772 } 773 774 /* 775 * Compound MAC was valid, so authentication succeeded. Reply with 776 * crypto binding to allow server to complete authentication. 777 */ 778 779 len = sizeof(struct eap_tlv_crypto_binding_tlv); 780 resp = wpabuf_alloc(len); 781 if (resp == NULL) 782 return NULL; 783 784 if (!data->anon_provisioning && data->phase2_success && 785 eap_fast_derive_msk(data) < 0) { 786 wpa_printf(MSG_INFO, "EAP-FAST: Failed to generate MSK"); 787 ret->methodState = METHOD_DONE; 788 ret->decision = DECISION_FAIL; 789 data->phase2_success = 0; 790 wpabuf_free(resp); 791 return NULL; 792 } 793 794 pos = wpabuf_put(resp, sizeof(struct eap_tlv_crypto_binding_tlv)); 795 eap_fast_write_crypto_binding((struct eap_tlv_crypto_binding_tlv *) 796 pos, _bind, cmk); 797 798 return resp; 799} 800 801 802static void eap_fast_parse_pac_tlv(struct eap_fast_pac *entry, int type, 803 u8 *pos, size_t len, int *pac_key_found) 804{ 805 switch (type & 0x7fff) { 806 case PAC_TYPE_PAC_KEY: 807 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: PAC-Key", pos, len); 808 if (len != EAP_FAST_PAC_KEY_LEN) { 809 wpa_printf(MSG_DEBUG, "EAP-FAST: Invalid PAC-Key " 810 "length %lu", (unsigned long) len); 811 break; 812 } 813 *pac_key_found = 1; 814 os_memcpy(entry->pac_key, pos, len); 815 break; 816 case PAC_TYPE_PAC_OPAQUE: 817 wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Opaque", pos, len); 818 entry->pac_opaque = pos; 819 entry->pac_opaque_len = len; 820 break; 821 case PAC_TYPE_PAC_INFO: 822 wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Info", pos, len); 823 entry->pac_info = pos; 824 entry->pac_info_len = len; 825 break; 826 default: 827 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignored unknown PAC type %d", 828 type); 829 break; 830 } 831} 832 833 834static int eap_fast_process_pac_tlv(struct eap_fast_pac *entry, 835 u8 *pac, size_t pac_len) 836{ 837 struct pac_tlv_hdr *hdr; 838 u8 *pos; 839 size_t left, len; 840 int type, pac_key_found = 0; 841 842 pos = pac; 843 left = pac_len; 844 845 while (left > sizeof(*hdr)) { 846 hdr = (struct pac_tlv_hdr *) pos; 847 type = be_to_host16(hdr->type); 848 len = be_to_host16(hdr->len); 849 pos += sizeof(*hdr); 850 left -= sizeof(*hdr); 851 if (len > left) { 852 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV overrun " 853 "(type=%d len=%lu left=%lu)", 854 type, (unsigned long) len, 855 (unsigned long) left); 856 return -1; 857 } 858 859 eap_fast_parse_pac_tlv(entry, type, pos, len, &pac_key_found); 860 861 pos += len; 862 left -= len; 863 } 864 865 if (!pac_key_found || !entry->pac_opaque || !entry->pac_info) { 866 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV does not include " 867 "all the required fields"); 868 return -1; 869 } 870 871 return 0; 872} 873 874 875static int eap_fast_parse_pac_info(struct eap_fast_pac *entry, int type, 876 u8 *pos, size_t len) 877{ 878 u16 pac_type; 879 u32 lifetime; 880 struct os_time now; 881 882 switch (type & 0x7fff) { 883 case PAC_TYPE_CRED_LIFETIME: 884 if (len != 4) { 885 wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Info - " 886 "Invalid CRED_LIFETIME length - ignored", 887 pos, len); 888 return 0; 889 } 890 891 /* 892 * This is not currently saved separately in PAC files since 893 * the server can automatically initiate PAC update when 894 * needed. Anyway, the information is available from PAC-Info 895 * dump if it is needed for something in the future. 896 */ 897 lifetime = WPA_GET_BE32(pos); 898 os_get_time(&now); 899 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info - CRED_LIFETIME %d " 900 "(%d days)", 901 lifetime, (lifetime - (u32) now.sec) / 86400); 902 break; 903 case PAC_TYPE_A_ID: 904 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - A-ID", 905 pos, len); 906 entry->a_id = pos; 907 entry->a_id_len = len; 908 break; 909 case PAC_TYPE_I_ID: 910 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - I-ID", 911 pos, len); 912 entry->i_id = pos; 913 entry->i_id_len = len; 914 break; 915 case PAC_TYPE_A_ID_INFO: 916 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - A-ID-Info", 917 pos, len); 918 entry->a_id_info = pos; 919 entry->a_id_info_len = len; 920 break; 921 case PAC_TYPE_PAC_TYPE: 922 /* RFC 5422, Section 4.2.6 - PAC-Type TLV */ 923 if (len != 2) { 924 wpa_printf(MSG_INFO, "EAP-FAST: Invalid PAC-Type " 925 "length %lu (expected 2)", 926 (unsigned long) len); 927 wpa_hexdump_ascii(MSG_DEBUG, 928 "EAP-FAST: PAC-Info - PAC-Type", 929 pos, len); 930 return -1; 931 } 932 pac_type = WPA_GET_BE16(pos); 933 if (pac_type != PAC_TYPE_TUNNEL_PAC && 934 pac_type != PAC_TYPE_USER_AUTHORIZATION && 935 pac_type != PAC_TYPE_MACHINE_AUTHENTICATION) { 936 wpa_printf(MSG_INFO, "EAP-FAST: Unsupported PAC Type " 937 "%d", pac_type); 938 return -1; 939 } 940 941 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info - PAC-Type %d", 942 pac_type); 943 entry->pac_type = pac_type; 944 break; 945 default: 946 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignored unknown PAC-Info " 947 "type %d", type); 948 break; 949 } 950 951 return 0; 952} 953 954 955static int eap_fast_process_pac_info(struct eap_fast_pac *entry) 956{ 957 struct pac_tlv_hdr *hdr; 958 u8 *pos; 959 size_t left, len; 960 int type; 961 962 /* RFC 5422, Section 4.2.4 */ 963 964 /* PAC-Type defaults to Tunnel PAC (Type 1) */ 965 entry->pac_type = PAC_TYPE_TUNNEL_PAC; 966 967 pos = entry->pac_info; 968 left = entry->pac_info_len; 969 while (left > sizeof(*hdr)) { 970 hdr = (struct pac_tlv_hdr *) pos; 971 type = be_to_host16(hdr->type); 972 len = be_to_host16(hdr->len); 973 pos += sizeof(*hdr); 974 left -= sizeof(*hdr); 975 if (len > left) { 976 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info overrun " 977 "(type=%d len=%lu left=%lu)", 978 type, (unsigned long) len, 979 (unsigned long) left); 980 return -1; 981 } 982 983 if (eap_fast_parse_pac_info(entry, type, pos, len) < 0) 984 return -1; 985 986 pos += len; 987 left -= len; 988 } 989 990 if (entry->a_id == NULL || entry->a_id_info == NULL) { 991 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info does not include " 992 "all the required fields"); 993 return -1; 994 } 995 996 return 0; 997} 998 999 1000static struct wpabuf * eap_fast_process_pac(struct eap_sm *sm, 1001 struct eap_fast_data *data, 1002 struct eap_method_ret *ret, 1003 u8 *pac, size_t pac_len) 1004{ 1005 struct eap_peer_config *config = eap_get_config(sm); 1006 struct eap_fast_pac entry; 1007 1008 os_memset(&entry, 0, sizeof(entry)); 1009 if (eap_fast_process_pac_tlv(&entry, pac, pac_len) || 1010 eap_fast_process_pac_info(&entry)) 1011 return NULL; 1012 1013 eap_fast_add_pac(&data->pac, &data->current_pac, &entry); 1014 eap_fast_pac_list_truncate(data->pac, data->max_pac_list_len); 1015 if (data->use_pac_binary_format) 1016 eap_fast_save_pac_bin(sm, data->pac, config->pac_file); 1017 else 1018 eap_fast_save_pac(sm, data->pac, config->pac_file); 1019 1020 if (data->provisioning) { 1021 if (data->anon_provisioning) { 1022 /* 1023 * Unauthenticated provisioning does not provide keying 1024 * material and must end with an EAP-Failure. 1025 * Authentication will be done separately after this. 1026 */ 1027 data->success = 0; 1028 ret->decision = DECISION_FAIL; 1029 } else { 1030 /* 1031 * Server may or may not allow authenticated 1032 * provisioning also for key generation. 1033 */ 1034 ret->decision = DECISION_COND_SUCC; 1035 } 1036 wpa_printf(MSG_DEBUG, "EAP-FAST: Send PAC-Acknowledgement TLV " 1037 "- Provisioning completed successfully"); 1038 } else { 1039 /* 1040 * This is PAC refreshing, i.e., normal authentication that is 1041 * expected to be completed with an EAP-Success. However, 1042 * RFC 5422, Section 3.5 allows EAP-Failure to be sent even 1043 * after protected success exchange in case of EAP-Fast 1044 * provisioning, so we better use DECISION_COND_SUCC here 1045 * instead of DECISION_UNCOND_SUCC. 1046 */ 1047 wpa_printf(MSG_DEBUG, "EAP-FAST: Send PAC-Acknowledgement TLV " 1048 "- PAC refreshing completed successfully"); 1049 ret->decision = DECISION_COND_SUCC; 1050 } 1051 ret->methodState = METHOD_DONE; 1052 return eap_fast_tlv_pac_ack(); 1053} 1054 1055 1056static int eap_fast_parse_decrypted(struct wpabuf *decrypted, 1057 struct eap_fast_tlv_parse *tlv, 1058 struct wpabuf **resp) 1059{ 1060 int mandatory, tlv_type, len, res; 1061 u8 *pos, *end; 1062 1063 os_memset(tlv, 0, sizeof(*tlv)); 1064 1065 /* Parse TLVs from the decrypted Phase 2 data */ 1066 pos = wpabuf_mhead(decrypted); 1067 end = pos + wpabuf_len(decrypted); 1068 while (pos + 4 < end) { 1069 mandatory = pos[0] & 0x80; 1070 tlv_type = WPA_GET_BE16(pos) & 0x3fff; 1071 pos += 2; 1072 len = WPA_GET_BE16(pos); 1073 pos += 2; 1074 if (pos + len > end) { 1075 wpa_printf(MSG_INFO, "EAP-FAST: TLV overflow"); 1076 return -1; 1077 } 1078 wpa_printf(MSG_DEBUG, "EAP-FAST: Received Phase 2: " 1079 "TLV type %d length %d%s", 1080 tlv_type, len, mandatory ? " (mandatory)" : ""); 1081 1082 res = eap_fast_parse_tlv(tlv, tlv_type, pos, len); 1083 if (res == -2) 1084 break; 1085 if (res < 0) { 1086 if (mandatory) { 1087 wpa_printf(MSG_DEBUG, "EAP-FAST: Nak unknown " 1088 "mandatory TLV type %d", tlv_type); 1089 *resp = eap_fast_tlv_nak(0, tlv_type); 1090 break; 1091 } else { 1092 wpa_printf(MSG_DEBUG, "EAP-FAST: ignored " 1093 "unknown optional TLV type %d", 1094 tlv_type); 1095 } 1096 } 1097 1098 pos += len; 1099 } 1100 1101 return 0; 1102} 1103 1104 1105static int eap_fast_encrypt_response(struct eap_sm *sm, 1106 struct eap_fast_data *data, 1107 struct wpabuf *resp, 1108 u8 identifier, struct wpabuf **out_data) 1109{ 1110 if (resp == NULL) 1111 return 0; 1112 1113 wpa_hexdump_buf(MSG_DEBUG, "EAP-FAST: Encrypting Phase 2 data", 1114 resp); 1115 if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_FAST, 1116 data->fast_version, identifier, 1117 resp, out_data)) { 1118 wpa_printf(MSG_INFO, "EAP-FAST: Failed to encrypt a Phase 2 " 1119 "frame"); 1120 } 1121 wpabuf_free(resp); 1122 1123 return 0; 1124} 1125 1126 1127static struct wpabuf * eap_fast_pac_request(void) 1128{ 1129 struct wpabuf *tmp; 1130 u8 *pos, *pos2; 1131 1132 tmp = wpabuf_alloc(sizeof(struct eap_tlv_hdr) + 1133 sizeof(struct eap_tlv_request_action_tlv) + 1134 sizeof(struct eap_tlv_pac_type_tlv)); 1135 if (tmp == NULL) 1136 return NULL; 1137 1138 pos = wpabuf_put(tmp, 0); 1139 pos2 = eap_fast_write_pac_request(pos, PAC_TYPE_TUNNEL_PAC); 1140 wpabuf_put(tmp, pos2 - pos); 1141 return tmp; 1142} 1143 1144 1145static int eap_fast_process_decrypted(struct eap_sm *sm, 1146 struct eap_fast_data *data, 1147 struct eap_method_ret *ret, 1148 const struct eap_hdr *req, 1149 struct wpabuf *decrypted, 1150 struct wpabuf **out_data) 1151{ 1152 struct wpabuf *resp = NULL, *tmp; 1153 struct eap_fast_tlv_parse tlv; 1154 int failed = 0; 1155 1156 if (eap_fast_parse_decrypted(decrypted, &tlv, &resp) < 0) 1157 return 0; 1158 if (resp) 1159 return eap_fast_encrypt_response(sm, data, resp, 1160 req->identifier, out_data); 1161 1162 if (tlv.result == EAP_TLV_RESULT_FAILURE) { 1163 resp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 0); 1164 return eap_fast_encrypt_response(sm, data, resp, 1165 req->identifier, out_data); 1166 } 1167 1168 if (tlv.iresult == EAP_TLV_RESULT_FAILURE) { 1169 resp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 1); 1170 return eap_fast_encrypt_response(sm, data, resp, 1171 req->identifier, out_data); 1172 } 1173 1174 if (tlv.crypto_binding) { 1175 tmp = eap_fast_process_crypto_binding(sm, data, ret, 1176 tlv.crypto_binding, 1177 tlv.crypto_binding_len); 1178 if (tmp == NULL) 1179 failed = 1; 1180 else 1181 resp = wpabuf_concat(resp, tmp); 1182 } 1183 1184 if (tlv.iresult == EAP_TLV_RESULT_SUCCESS) { 1185 tmp = eap_fast_tlv_result(failed ? EAP_TLV_RESULT_FAILURE : 1186 EAP_TLV_RESULT_SUCCESS, 1); 1187 resp = wpabuf_concat(resp, tmp); 1188 } 1189 1190 if (tlv.eap_payload_tlv) { 1191 tmp = eap_fast_process_eap_payload_tlv( 1192 sm, data, ret, tlv.eap_payload_tlv, 1193 tlv.eap_payload_tlv_len); 1194 resp = wpabuf_concat(resp, tmp); 1195 } 1196 1197 if (tlv.pac && tlv.result != EAP_TLV_RESULT_SUCCESS) { 1198 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV without Result TLV " 1199 "acknowledging success"); 1200 failed = 1; 1201 } else if (tlv.pac && tlv.result == EAP_TLV_RESULT_SUCCESS) { 1202 tmp = eap_fast_process_pac(sm, data, ret, tlv.pac, 1203 tlv.pac_len); 1204 resp = wpabuf_concat(resp, tmp); 1205 } 1206 1207 if (data->current_pac == NULL && data->provisioning && 1208 !data->anon_provisioning && !tlv.pac && 1209 (tlv.iresult == EAP_TLV_RESULT_SUCCESS || 1210 tlv.result == EAP_TLV_RESULT_SUCCESS)) { 1211 /* 1212 * Need to request Tunnel PAC when using authenticated 1213 * provisioning. 1214 */ 1215 wpa_printf(MSG_DEBUG, "EAP-FAST: Request Tunnel PAC"); 1216 tmp = eap_fast_pac_request(); 1217 resp = wpabuf_concat(resp, tmp); 1218 } 1219 1220 if (tlv.result == EAP_TLV_RESULT_SUCCESS && !failed) { 1221 tmp = eap_fast_tlv_result(EAP_TLV_RESULT_SUCCESS, 0); 1222 resp = wpabuf_concat(tmp, resp); 1223 } else if (failed) { 1224 tmp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 0); 1225 resp = wpabuf_concat(tmp, resp); 1226 } 1227 1228 if (resp && tlv.result == EAP_TLV_RESULT_SUCCESS && !failed && 1229 tlv.crypto_binding && data->phase2_success) { 1230 if (data->anon_provisioning) { 1231 wpa_printf(MSG_DEBUG, "EAP-FAST: Unauthenticated " 1232 "provisioning completed successfully."); 1233 ret->methodState = METHOD_DONE; 1234 ret->decision = DECISION_FAIL; 1235 } else { 1236 wpa_printf(MSG_DEBUG, "EAP-FAST: Authentication " 1237 "completed successfully."); 1238 if (data->provisioning) 1239 ret->methodState = METHOD_MAY_CONT; 1240 else 1241 ret->methodState = METHOD_DONE; 1242 ret->decision = DECISION_UNCOND_SUCC; 1243 } 1244 } 1245 1246 if (resp == NULL) { 1247 wpa_printf(MSG_DEBUG, "EAP-FAST: No recognized TLVs - send " 1248 "empty response packet"); 1249 resp = wpabuf_alloc(1); 1250 } 1251 1252 return eap_fast_encrypt_response(sm, data, resp, req->identifier, 1253 out_data); 1254} 1255 1256 1257static int eap_fast_decrypt(struct eap_sm *sm, struct eap_fast_data *data, 1258 struct eap_method_ret *ret, 1259 const struct eap_hdr *req, 1260 const struct wpabuf *in_data, 1261 struct wpabuf **out_data) 1262{ 1263 struct wpabuf *in_decrypted; 1264 int res; 1265 1266 wpa_printf(MSG_DEBUG, "EAP-FAST: Received %lu bytes encrypted data for" 1267 " Phase 2", (unsigned long) wpabuf_len(in_data)); 1268 1269 if (data->pending_phase2_req) { 1270 wpa_printf(MSG_DEBUG, "EAP-FAST: Pending Phase 2 request - " 1271 "skip decryption and use old data"); 1272 /* Clear TLS reassembly state. */ 1273 eap_peer_tls_reset_input(&data->ssl); 1274 1275 in_decrypted = data->pending_phase2_req; 1276 data->pending_phase2_req = NULL; 1277 goto continue_req; 1278 } 1279 1280 if (wpabuf_len(in_data) == 0) { 1281 /* Received TLS ACK - requesting more fragments */ 1282 return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_FAST, 1283 data->fast_version, 1284 req->identifier, NULL, out_data); 1285 } 1286 1287 res = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted); 1288 if (res) 1289 return res; 1290 1291continue_req: 1292 wpa_hexdump_buf(MSG_MSGDUMP, "EAP-FAST: Decrypted Phase 2 TLV(s)", 1293 in_decrypted); 1294 1295 if (wpabuf_len(in_decrypted) < 4) { 1296 wpa_printf(MSG_INFO, "EAP-FAST: Too short Phase 2 " 1297 "TLV frame (len=%lu)", 1298 (unsigned long) wpabuf_len(in_decrypted)); 1299 wpabuf_free(in_decrypted); 1300 return -1; 1301 } 1302 1303 res = eap_fast_process_decrypted(sm, data, ret, req, 1304 in_decrypted, out_data); 1305 1306 wpabuf_free(in_decrypted); 1307 1308 return res; 1309} 1310 1311 1312static const u8 * eap_fast_get_a_id(const u8 *buf, size_t len, size_t *id_len) 1313{ 1314 const u8 *a_id; 1315 struct pac_tlv_hdr *hdr; 1316 1317 /* 1318 * Parse authority identity (A-ID) from the EAP-FAST/Start. This 1319 * supports both raw A-ID and one inside an A-ID TLV. 1320 */ 1321 a_id = buf; 1322 *id_len = len; 1323 if (len > sizeof(*hdr)) { 1324 int tlen; 1325 hdr = (struct pac_tlv_hdr *) buf; 1326 tlen = be_to_host16(hdr->len); 1327 if (be_to_host16(hdr->type) == PAC_TYPE_A_ID && 1328 sizeof(*hdr) + tlen <= len) { 1329 wpa_printf(MSG_DEBUG, "EAP-FAST: A-ID was in TLV " 1330 "(Start)"); 1331 a_id = (u8 *) (hdr + 1); 1332 *id_len = tlen; 1333 } 1334 } 1335 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: A-ID", a_id, *id_len); 1336 1337 return a_id; 1338} 1339 1340 1341static void eap_fast_select_pac(struct eap_fast_data *data, 1342 const u8 *a_id, size_t a_id_len) 1343{ 1344 data->current_pac = eap_fast_get_pac(data->pac, a_id, a_id_len, 1345 PAC_TYPE_TUNNEL_PAC); 1346 if (data->current_pac == NULL) { 1347 /* 1348 * Tunnel PAC was not available for this A-ID. Try to use 1349 * Machine Authentication PAC, if one is available. 1350 */ 1351 data->current_pac = eap_fast_get_pac( 1352 data->pac, a_id, a_id_len, 1353 PAC_TYPE_MACHINE_AUTHENTICATION); 1354 } 1355 1356 if (data->current_pac) { 1357 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC found for this A-ID " 1358 "(PAC-Type %d)", data->current_pac->pac_type); 1359 wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-FAST: A-ID-Info", 1360 data->current_pac->a_id_info, 1361 data->current_pac->a_id_info_len); 1362 } 1363} 1364 1365 1366static int eap_fast_use_pac_opaque(struct eap_sm *sm, 1367 struct eap_fast_data *data, 1368 struct eap_fast_pac *pac) 1369{ 1370 u8 *tlv; 1371 size_t tlv_len, olen; 1372 struct eap_tlv_hdr *ehdr; 1373 1374 olen = pac->pac_opaque_len; 1375 tlv_len = sizeof(*ehdr) + olen; 1376 tlv = os_malloc(tlv_len); 1377 if (tlv) { 1378 ehdr = (struct eap_tlv_hdr *) tlv; 1379 ehdr->tlv_type = host_to_be16(PAC_TYPE_PAC_OPAQUE); 1380 ehdr->length = host_to_be16(olen); 1381 os_memcpy(ehdr + 1, pac->pac_opaque, olen); 1382 } 1383 if (tlv == NULL || 1384 tls_connection_client_hello_ext(sm->ssl_ctx, data->ssl.conn, 1385 TLS_EXT_PAC_OPAQUE, 1386 tlv, tlv_len) < 0) { 1387 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to add PAC-Opaque TLS " 1388 "extension"); 1389 os_free(tlv); 1390 return -1; 1391 } 1392 os_free(tlv); 1393 1394 return 0; 1395} 1396 1397 1398static int eap_fast_clear_pac_opaque_ext(struct eap_sm *sm, 1399 struct eap_fast_data *data) 1400{ 1401 if (tls_connection_client_hello_ext(sm->ssl_ctx, data->ssl.conn, 1402 TLS_EXT_PAC_OPAQUE, NULL, 0) < 0) { 1403 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to remove PAC-Opaque " 1404 "TLS extension"); 1405 return -1; 1406 } 1407 return 0; 1408} 1409 1410 1411static int eap_fast_set_provisioning_ciphers(struct eap_sm *sm, 1412 struct eap_fast_data *data) 1413{ 1414 u8 ciphers[5]; 1415 int count = 0; 1416 1417 if (data->provisioning_allowed & EAP_FAST_PROV_UNAUTH) { 1418 wpa_printf(MSG_DEBUG, "EAP-FAST: Enabling unauthenticated " 1419 "provisioning TLS cipher suites"); 1420 ciphers[count++] = TLS_CIPHER_ANON_DH_AES128_SHA; 1421 } 1422 1423 if (data->provisioning_allowed & EAP_FAST_PROV_AUTH) { 1424 wpa_printf(MSG_DEBUG, "EAP-FAST: Enabling authenticated " 1425 "provisioning TLS cipher suites"); 1426 ciphers[count++] = TLS_CIPHER_RSA_DHE_AES128_SHA; 1427 ciphers[count++] = TLS_CIPHER_AES128_SHA; 1428 ciphers[count++] = TLS_CIPHER_RC4_SHA; 1429 } 1430 1431 ciphers[count++] = TLS_CIPHER_NONE; 1432 1433 if (tls_connection_set_cipher_list(sm->ssl_ctx, data->ssl.conn, 1434 ciphers)) { 1435 wpa_printf(MSG_INFO, "EAP-FAST: Could not configure TLS " 1436 "cipher suites for provisioning"); 1437 return -1; 1438 } 1439 1440 return 0; 1441} 1442 1443 1444static int eap_fast_process_start(struct eap_sm *sm, 1445 struct eap_fast_data *data, u8 flags, 1446 const u8 *pos, size_t left) 1447{ 1448 const u8 *a_id; 1449 size_t a_id_len; 1450 1451 /* EAP-FAST Version negotiation (section 3.1) */ 1452 wpa_printf(MSG_DEBUG, "EAP-FAST: Start (server ver=%d, own ver=%d)", 1453 flags & EAP_TLS_VERSION_MASK, data->fast_version); 1454 if ((flags & EAP_TLS_VERSION_MASK) < data->fast_version) 1455 data->fast_version = flags & EAP_TLS_VERSION_MASK; 1456 wpa_printf(MSG_DEBUG, "EAP-FAST: Using FAST version %d", 1457 data->fast_version); 1458 1459 a_id = eap_fast_get_a_id(pos, left, &a_id_len); 1460 eap_fast_select_pac(data, a_id, a_id_len); 1461 1462 if (data->resuming && data->current_pac) { 1463 wpa_printf(MSG_DEBUG, "EAP-FAST: Trying to resume session - " 1464 "do not add PAC-Opaque to TLS ClientHello"); 1465 if (eap_fast_clear_pac_opaque_ext(sm, data) < 0) 1466 return -1; 1467 } else if (data->current_pac) { 1468 /* 1469 * PAC found for the A-ID and we are not resuming an old 1470 * session, so add PAC-Opaque extension to ClientHello. 1471 */ 1472 if (eap_fast_use_pac_opaque(sm, data, data->current_pac) < 0) 1473 return -1; 1474 } else { 1475 /* No PAC found, so we must provision one. */ 1476 if (!data->provisioning_allowed) { 1477 wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC found and " 1478 "provisioning disabled"); 1479 return -1; 1480 } 1481 wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC found - " 1482 "starting provisioning"); 1483 if (eap_fast_set_provisioning_ciphers(sm, data) < 0 || 1484 eap_fast_clear_pac_opaque_ext(sm, data) < 0) 1485 return -1; 1486 data->provisioning = 1; 1487 } 1488 1489 return 0; 1490} 1491 1492 1493static struct wpabuf * eap_fast_process(struct eap_sm *sm, void *priv, 1494 struct eap_method_ret *ret, 1495 const struct wpabuf *reqData) 1496{ 1497 const struct eap_hdr *req; 1498 size_t left; 1499 int res; 1500 u8 flags, id; 1501 struct wpabuf *resp; 1502 const u8 *pos; 1503 struct eap_fast_data *data = priv; 1504 1505 pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_FAST, ret, 1506 reqData, &left, &flags); 1507 if (pos == NULL) 1508 return NULL; 1509 1510 req = wpabuf_head(reqData); 1511 id = req->identifier; 1512 1513 if (flags & EAP_TLS_FLAGS_START) { 1514 if (eap_fast_process_start(sm, data, flags, pos, left) < 0) 1515 return NULL; 1516 1517 left = 0; /* A-ID is not used in further packet processing */ 1518 } 1519 1520 resp = NULL; 1521 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) && 1522 !data->resuming) { 1523 /* Process tunneled (encrypted) phase 2 data. */ 1524 struct wpabuf msg; 1525 wpabuf_set(&msg, pos, left); 1526 res = eap_fast_decrypt(sm, data, ret, req, &msg, &resp); 1527 if (res < 0) { 1528 ret->methodState = METHOD_DONE; 1529 ret->decision = DECISION_FAIL; 1530 /* 1531 * Ack possible Alert that may have caused failure in 1532 * decryption. 1533 */ 1534 res = 1; 1535 } 1536 } else { 1537 /* Continue processing TLS handshake (phase 1). */ 1538 res = eap_peer_tls_process_helper(sm, &data->ssl, 1539 EAP_TYPE_FAST, 1540 data->fast_version, id, pos, 1541 left, &resp); 1542 1543 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) { 1544 char cipher[80]; 1545 wpa_printf(MSG_DEBUG, 1546 "EAP-FAST: TLS done, proceed to Phase 2"); 1547 if (data->provisioning && 1548 (!(data->provisioning_allowed & 1549 EAP_FAST_PROV_AUTH) || 1550 tls_get_cipher(sm->ssl_ctx, data->ssl.conn, 1551 cipher, sizeof(cipher)) < 0 || 1552 os_strstr(cipher, "ADH-") || 1553 os_strstr(cipher, "anon"))) { 1554 wpa_printf(MSG_DEBUG, "EAP-FAST: Using " 1555 "anonymous (unauthenticated) " 1556 "provisioning"); 1557 data->anon_provisioning = 1; 1558 } else 1559 data->anon_provisioning = 0; 1560 data->resuming = 0; 1561 eap_fast_derive_keys(sm, data); 1562 } 1563 1564 if (res == 2) { 1565 struct wpabuf msg; 1566 /* 1567 * Application data included in the handshake message. 1568 */ 1569 wpabuf_free(data->pending_phase2_req); 1570 data->pending_phase2_req = resp; 1571 resp = NULL; 1572 wpabuf_set(&msg, pos, left); 1573 res = eap_fast_decrypt(sm, data, ret, req, &msg, 1574 &resp); 1575 } 1576 } 1577 1578 if (res == 1) { 1579 wpabuf_free(resp); 1580 return eap_peer_tls_build_ack(id, EAP_TYPE_FAST, 1581 data->fast_version); 1582 } 1583 1584 return resp; 1585} 1586 1587 1588#if 0 /* FIX */ 1589static Boolean eap_fast_has_reauth_data(struct eap_sm *sm, void *priv) 1590{ 1591 struct eap_fast_data *data = priv; 1592 return tls_connection_established(sm->ssl_ctx, data->ssl.conn); 1593} 1594 1595 1596static void eap_fast_deinit_for_reauth(struct eap_sm *sm, void *priv) 1597{ 1598 struct eap_fast_data *data = priv; 1599 os_free(data->key_block_p); 1600 data->key_block_p = NULL; 1601 wpabuf_free(data->pending_phase2_req); 1602 data->pending_phase2_req = NULL; 1603} 1604 1605 1606static void * eap_fast_init_for_reauth(struct eap_sm *sm, void *priv) 1607{ 1608 struct eap_fast_data *data = priv; 1609 if (eap_peer_tls_reauth_init(sm, &data->ssl)) { 1610 os_free(data); 1611 return NULL; 1612 } 1613 if (data->phase2_priv && data->phase2_method && 1614 data->phase2_method->init_for_reauth) 1615 data->phase2_method->init_for_reauth(sm, data->phase2_priv); 1616 data->phase2_success = 0; 1617 data->resuming = 1; 1618 data->provisioning = 0; 1619 data->anon_provisioning = 0; 1620 data->simck_idx = 0; 1621 return priv; 1622} 1623#endif 1624 1625 1626static int eap_fast_get_status(struct eap_sm *sm, void *priv, char *buf, 1627 size_t buflen, int verbose) 1628{ 1629 struct eap_fast_data *data = priv; 1630 int len, ret; 1631 1632 len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose); 1633 if (data->phase2_method) { 1634 ret = os_snprintf(buf + len, buflen - len, 1635 "EAP-FAST Phase2 method=%s\n", 1636 data->phase2_method->name); 1637 if (ret < 0 || (size_t) ret >= buflen - len) 1638 return len; 1639 len += ret; 1640 } 1641 return len; 1642} 1643 1644 1645static Boolean eap_fast_isKeyAvailable(struct eap_sm *sm, void *priv) 1646{ 1647 struct eap_fast_data *data = priv; 1648 return data->success; 1649} 1650 1651 1652static u8 * eap_fast_getKey(struct eap_sm *sm, void *priv, size_t *len) 1653{ 1654 struct eap_fast_data *data = priv; 1655 u8 *key; 1656 1657 if (!data->success) 1658 return NULL; 1659 1660 key = os_malloc(EAP_FAST_KEY_LEN); 1661 if (key == NULL) 1662 return NULL; 1663 1664 *len = EAP_FAST_KEY_LEN; 1665 os_memcpy(key, data->key_data, EAP_FAST_KEY_LEN); 1666 1667 return key; 1668} 1669 1670 1671static u8 * eap_fast_get_emsk(struct eap_sm *sm, void *priv, size_t *len) 1672{ 1673 struct eap_fast_data *data = priv; 1674 u8 *key; 1675 1676 if (!data->success) 1677 return NULL; 1678 1679 key = os_malloc(EAP_EMSK_LEN); 1680 if (key == NULL) 1681 return NULL; 1682 1683 *len = EAP_EMSK_LEN; 1684 os_memcpy(key, data->emsk, EAP_EMSK_LEN); 1685 1686 return key; 1687} 1688 1689 1690int eap_peer_fast_register(void) 1691{ 1692 struct eap_method *eap; 1693 int ret; 1694 1695 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION, 1696 EAP_VENDOR_IETF, EAP_TYPE_FAST, "FAST"); 1697 if (eap == NULL) 1698 return -1; 1699 1700 eap->init = eap_fast_init; 1701 eap->deinit = eap_fast_deinit; 1702 eap->process = eap_fast_process; 1703 eap->isKeyAvailable = eap_fast_isKeyAvailable; 1704 eap->getKey = eap_fast_getKey; 1705 eap->get_status = eap_fast_get_status; 1706#if 0 1707 eap->has_reauth_data = eap_fast_has_reauth_data; 1708 eap->deinit_for_reauth = eap_fast_deinit_for_reauth; 1709 eap->init_for_reauth = eap_fast_init_for_reauth; 1710#endif 1711 eap->get_emsk = eap_fast_get_emsk; 1712 1713 ret = eap_peer_method_register(eap); 1714 if (ret) 1715 eap_peer_method_free(eap); 1716 return ret; 1717} 1718