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