1 /* 2 * EAP-FAST server (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/aes_wrap.h" 13 #include "crypto/sha1.h" 14 #include "crypto/tls.h" 15 #include "crypto/random.h" 16 #include "eap_common/eap_tlv_common.h" 17 #include "eap_common/eap_fast_common.h" 18 #include "eap_i.h" 19 #include "eap_tls_common.h" 20 21 22 static void eap_fast_reset(struct eap_sm *sm, void *priv); 23 24 25 /* Private PAC-Opaque TLV types */ 26 #define PAC_OPAQUE_TYPE_PAD 0 27 #define PAC_OPAQUE_TYPE_KEY 1 28 #define PAC_OPAQUE_TYPE_LIFETIME 2 29 #define PAC_OPAQUE_TYPE_IDENTITY 3 30 31 struct eap_fast_data { 32 struct eap_ssl_data ssl; 33 enum { 34 START, PHASE1, PHASE2_START, PHASE2_ID, PHASE2_METHOD, 35 CRYPTO_BINDING, REQUEST_PAC, SUCCESS, FAILURE 36 } state; 37 38 int fast_version; 39 const struct eap_method *phase2_method; 40 void *phase2_priv; 41 int force_version; 42 int peer_version; 43 44 u8 crypto_binding_nonce[32]; 45 int final_result; 46 47 struct eap_fast_key_block_provisioning *key_block_p; 48 49 u8 simck[EAP_FAST_SIMCK_LEN]; 50 u8 cmk[EAP_FAST_CMK_LEN]; 51 int simck_idx; 52 53 u8 pac_opaque_encr[16]; 54 u8 *srv_id; 55 size_t srv_id_len; 56 char *srv_id_info; 57 58 int anon_provisioning; 59 int send_new_pac; /* server triggered re-keying of Tunnel PAC */ 60 struct wpabuf *pending_phase2_resp; 61 u8 *identity; /* from PAC-Opaque */ 62 size_t identity_len; 63 int eap_seq; 64 int tnc_started; 65 66 int pac_key_lifetime; 67 int pac_key_refresh_time; 68 }; 69 70 71 static int eap_fast_process_phase2_start(struct eap_sm *sm, 72 struct eap_fast_data *data); 73 74 75 static const char * eap_fast_state_txt(int state) 76 { 77 switch (state) { 78 case START: 79 return "START"; 80 case PHASE1: 81 return "PHASE1"; 82 case PHASE2_START: 83 return "PHASE2_START"; 84 case PHASE2_ID: 85 return "PHASE2_ID"; 86 case PHASE2_METHOD: 87 return "PHASE2_METHOD"; 88 case CRYPTO_BINDING: 89 return "CRYPTO_BINDING"; 90 case REQUEST_PAC: 91 return "REQUEST_PAC"; 92 case SUCCESS: 93 return "SUCCESS"; 94 case FAILURE: 95 return "FAILURE"; 96 default: 97 return "Unknown?!"; 98 } 99 } 100 101 102 static void eap_fast_state(struct eap_fast_data *data, int state) 103 { 104 wpa_printf(MSG_DEBUG, "EAP-FAST: %s -> %s", 105 eap_fast_state_txt(data->state), 106 eap_fast_state_txt(state)); 107 data->state = state; 108 } 109 110 111 static enum eap_type eap_fast_req_failure(struct eap_sm *sm, 112 struct eap_fast_data *data) 113 { 114 /* TODO: send Result TLV(FAILURE) */ 115 eap_fast_state(data, FAILURE); 116 return EAP_TYPE_NONE; 117 } 118 119 120 static int eap_fast_session_ticket_cb(void *ctx, const u8 *ticket, size_t len, 121 const u8 *client_random, 122 const u8 *server_random, 123 u8 *master_secret) 124 { 125 struct eap_fast_data *data = ctx; 126 const u8 *pac_opaque; 127 size_t pac_opaque_len; 128 u8 *buf, *pos, *end, *pac_key = NULL; 129 os_time_t lifetime = 0; 130 struct os_time now; 131 u8 *identity = NULL; 132 size_t identity_len = 0; 133 134 wpa_printf(MSG_DEBUG, "EAP-FAST: SessionTicket callback"); 135 wpa_hexdump(MSG_DEBUG, "EAP-FAST: SessionTicket (PAC-Opaque)", 136 ticket, len); 137 138 if (len < 4 || WPA_GET_BE16(ticket) != PAC_TYPE_PAC_OPAQUE) { 139 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignore invalid " 140 "SessionTicket"); 141 return 0; 142 } 143 144 pac_opaque_len = WPA_GET_BE16(ticket + 2); 145 pac_opaque = ticket + 4; 146 if (pac_opaque_len < 8 || pac_opaque_len % 8 || 147 pac_opaque_len > len - 4) { 148 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignore invalid PAC-Opaque " 149 "(len=%lu left=%lu)", 150 (unsigned long) pac_opaque_len, 151 (unsigned long) len); 152 return 0; 153 } 154 wpa_hexdump(MSG_DEBUG, "EAP-FAST: Received PAC-Opaque", 155 pac_opaque, pac_opaque_len); 156 157 buf = os_malloc(pac_opaque_len - 8); 158 if (buf == NULL) { 159 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to allocate memory " 160 "for decrypting PAC-Opaque"); 161 return 0; 162 } 163 164 if (aes_unwrap(data->pac_opaque_encr, sizeof(data->pac_opaque_encr), 165 (pac_opaque_len - 8) / 8, pac_opaque, buf) < 0) { 166 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to decrypt " 167 "PAC-Opaque"); 168 os_free(buf); 169 /* 170 * This may have been caused by server changing the PAC-Opaque 171 * encryption key, so just ignore this PAC-Opaque instead of 172 * failing the authentication completely. Provisioning can now 173 * be used to provision a new PAC. 174 */ 175 return 0; 176 } 177 178 end = buf + pac_opaque_len - 8; 179 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: Decrypted PAC-Opaque", 180 buf, end - buf); 181 182 pos = buf; 183 while (end - pos > 1) { 184 u8 id, elen; 185 186 id = *pos++; 187 elen = *pos++; 188 if (elen > end - pos) 189 break; 190 191 switch (id) { 192 case PAC_OPAQUE_TYPE_PAD: 193 goto done; 194 case PAC_OPAQUE_TYPE_KEY: 195 if (elen != EAP_FAST_PAC_KEY_LEN) { 196 wpa_printf(MSG_DEBUG, 197 "EAP-FAST: Invalid PAC-Key length %d", 198 elen); 199 os_free(buf); 200 return -1; 201 } 202 pac_key = pos; 203 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: PAC-Key from " 204 "decrypted PAC-Opaque", 205 pac_key, EAP_FAST_PAC_KEY_LEN); 206 break; 207 case PAC_OPAQUE_TYPE_LIFETIME: 208 if (elen != 4) { 209 wpa_printf(MSG_DEBUG, "EAP-FAST: Invalid " 210 "PAC-Key lifetime length %d", 211 elen); 212 os_free(buf); 213 return -1; 214 } 215 lifetime = WPA_GET_BE32(pos); 216 break; 217 case PAC_OPAQUE_TYPE_IDENTITY: 218 identity = pos; 219 identity_len = elen; 220 break; 221 } 222 223 pos += elen; 224 } 225 done: 226 227 if (pac_key == NULL) { 228 wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC-Key included in " 229 "PAC-Opaque"); 230 os_free(buf); 231 return -1; 232 } 233 234 if (identity) { 235 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: Identity from " 236 "PAC-Opaque", identity, identity_len); 237 os_free(data->identity); 238 data->identity = os_malloc(identity_len); 239 if (data->identity) { 240 os_memcpy(data->identity, identity, identity_len); 241 data->identity_len = identity_len; 242 } 243 } 244 245 if (os_get_time(&now) < 0 || lifetime <= 0 || now.sec > lifetime) { 246 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Key not valid anymore " 247 "(lifetime=%ld now=%ld)", lifetime, now.sec); 248 data->send_new_pac = 2; 249 /* 250 * Allow PAC to be used to allow a PAC update with some level 251 * of server authentication (i.e., do not fall back to full TLS 252 * handshake since we cannot be sure that the peer would be 253 * able to validate server certificate now). However, reject 254 * the authentication since the PAC was not valid anymore. Peer 255 * can connect again with the newly provisioned PAC after this. 256 */ 257 } else if (lifetime - now.sec < data->pac_key_refresh_time) { 258 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Key soft timeout; send " 259 "an update if authentication succeeds"); 260 data->send_new_pac = 1; 261 } 262 263 eap_fast_derive_master_secret(pac_key, server_random, client_random, 264 master_secret); 265 266 os_free(buf); 267 268 return 1; 269 } 270 271 272 static void eap_fast_derive_key_auth(struct eap_sm *sm, 273 struct eap_fast_data *data) 274 { 275 u8 *sks; 276 277 /* RFC 4851, Section 5.1: 278 * Extra key material after TLS key_block: session_key_seed[40] 279 */ 280 281 sks = eap_fast_derive_key(sm->cfg->ssl_ctx, data->ssl.conn, 282 EAP_FAST_SKS_LEN); 283 if (sks == NULL) { 284 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive " 285 "session_key_seed"); 286 return; 287 } 288 289 /* 290 * RFC 4851, Section 5.2: 291 * S-IMCK[0] = session_key_seed 292 */ 293 wpa_hexdump_key(MSG_DEBUG, 294 "EAP-FAST: session_key_seed (SKS = S-IMCK[0])", 295 sks, EAP_FAST_SKS_LEN); 296 data->simck_idx = 0; 297 os_memcpy(data->simck, sks, EAP_FAST_SIMCK_LEN); 298 os_free(sks); 299 } 300 301 302 static void eap_fast_derive_key_provisioning(struct eap_sm *sm, 303 struct eap_fast_data *data) 304 { 305 os_free(data->key_block_p); 306 data->key_block_p = (struct eap_fast_key_block_provisioning *) 307 eap_fast_derive_key(sm->cfg->ssl_ctx, data->ssl.conn, 308 sizeof(*data->key_block_p)); 309 if (data->key_block_p == NULL) { 310 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive key block"); 311 return; 312 } 313 /* 314 * RFC 4851, Section 5.2: 315 * S-IMCK[0] = session_key_seed 316 */ 317 wpa_hexdump_key(MSG_DEBUG, 318 "EAP-FAST: session_key_seed (SKS = S-IMCK[0])", 319 data->key_block_p->session_key_seed, 320 sizeof(data->key_block_p->session_key_seed)); 321 data->simck_idx = 0; 322 os_memcpy(data->simck, data->key_block_p->session_key_seed, 323 EAP_FAST_SIMCK_LEN); 324 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: server_challenge", 325 data->key_block_p->server_challenge, 326 sizeof(data->key_block_p->server_challenge)); 327 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: client_challenge", 328 data->key_block_p->client_challenge, 329 sizeof(data->key_block_p->client_challenge)); 330 } 331 332 333 static int eap_fast_get_phase2_key(struct eap_sm *sm, 334 struct eap_fast_data *data, 335 u8 *isk, size_t isk_len) 336 { 337 u8 *key; 338 size_t key_len; 339 340 os_memset(isk, 0, isk_len); 341 342 if (data->phase2_method == NULL || data->phase2_priv == NULL) { 343 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 method not " 344 "available"); 345 return -1; 346 } 347 348 if (data->phase2_method->getKey == NULL) 349 return 0; 350 351 if ((key = data->phase2_method->getKey(sm, data->phase2_priv, 352 &key_len)) == NULL) { 353 wpa_printf(MSG_DEBUG, "EAP-FAST: Could not get key material " 354 "from Phase 2"); 355 return -1; 356 } 357 358 if (key_len > isk_len) 359 key_len = isk_len; 360 if (key_len == 32 && 361 data->phase2_method->vendor == EAP_VENDOR_IETF && 362 data->phase2_method->method == EAP_TYPE_MSCHAPV2) { 363 /* 364 * EAP-FAST uses reverse order for MS-MPPE keys when deriving 365 * MSK from EAP-MSCHAPv2. Swap the keys here to get the correct 366 * ISK for EAP-FAST cryptobinding. 367 */ 368 os_memcpy(isk, key + 16, 16); 369 os_memcpy(isk + 16, key, 16); 370 } else 371 os_memcpy(isk, key, key_len); 372 os_free(key); 373 374 return 0; 375 } 376 377 378 static int eap_fast_update_icmk(struct eap_sm *sm, struct eap_fast_data *data) 379 { 380 u8 isk[32], imck[60]; 381 382 wpa_printf(MSG_DEBUG, "EAP-FAST: Deriving ICMK[%d] (S-IMCK and CMK)", 383 data->simck_idx + 1); 384 385 /* 386 * RFC 4851, Section 5.2: 387 * IMCK[j] = T-PRF(S-IMCK[j-1], "Inner Methods Compound Keys", 388 * MSK[j], 60) 389 * S-IMCK[j] = first 40 octets of IMCK[j] 390 * CMK[j] = last 20 octets of IMCK[j] 391 */ 392 393 if (eap_fast_get_phase2_key(sm, data, isk, sizeof(isk)) < 0) 394 return -1; 395 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: ISK[j]", isk, sizeof(isk)); 396 sha1_t_prf(data->simck, EAP_FAST_SIMCK_LEN, 397 "Inner Methods Compound Keys", 398 isk, sizeof(isk), imck, sizeof(imck)); 399 data->simck_idx++; 400 os_memcpy(data->simck, imck, EAP_FAST_SIMCK_LEN); 401 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: S-IMCK[j]", 402 data->simck, EAP_FAST_SIMCK_LEN); 403 os_memcpy(data->cmk, imck + EAP_FAST_SIMCK_LEN, EAP_FAST_CMK_LEN); 404 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: CMK[j]", 405 data->cmk, EAP_FAST_CMK_LEN); 406 407 return 0; 408 } 409 410 411 static void * eap_fast_init(struct eap_sm *sm) 412 { 413 struct eap_fast_data *data; 414 u8 ciphers[7] = { 415 TLS_CIPHER_ANON_DH_AES128_SHA, 416 TLS_CIPHER_AES128_SHA, 417 TLS_CIPHER_RSA_DHE_AES128_SHA, 418 TLS_CIPHER_RC4_SHA, 419 TLS_CIPHER_RSA_DHE_AES256_SHA, 420 TLS_CIPHER_AES256_SHA, 421 TLS_CIPHER_NONE 422 }; 423 424 data = os_zalloc(sizeof(*data)); 425 if (data == NULL) 426 return NULL; 427 data->fast_version = EAP_FAST_VERSION; 428 data->force_version = -1; 429 if (sm->user && sm->user->force_version >= 0) { 430 data->force_version = sm->user->force_version; 431 wpa_printf(MSG_DEBUG, "EAP-FAST: forcing version %d", 432 data->force_version); 433 data->fast_version = data->force_version; 434 } 435 data->state = START; 436 437 if (eap_server_tls_ssl_init(sm, &data->ssl, 0, EAP_TYPE_FAST)) { 438 wpa_printf(MSG_INFO, "EAP-FAST: Failed to initialize SSL."); 439 eap_fast_reset(sm, data); 440 return NULL; 441 } 442 443 if (tls_connection_set_cipher_list(sm->cfg->ssl_ctx, data->ssl.conn, 444 ciphers) < 0) { 445 wpa_printf(MSG_INFO, "EAP-FAST: Failed to set TLS cipher " 446 "suites"); 447 eap_fast_reset(sm, data); 448 return NULL; 449 } 450 451 if (tls_connection_set_session_ticket_cb(sm->cfg->ssl_ctx, 452 data->ssl.conn, 453 eap_fast_session_ticket_cb, 454 data) < 0) { 455 wpa_printf(MSG_INFO, "EAP-FAST: Failed to set SessionTicket " 456 "callback"); 457 eap_fast_reset(sm, data); 458 return NULL; 459 } 460 461 if (sm->cfg->pac_opaque_encr_key == NULL) { 462 wpa_printf(MSG_INFO, "EAP-FAST: No PAC-Opaque encryption key " 463 "configured"); 464 eap_fast_reset(sm, data); 465 return NULL; 466 } 467 os_memcpy(data->pac_opaque_encr, sm->cfg->pac_opaque_encr_key, 468 sizeof(data->pac_opaque_encr)); 469 470 if (sm->cfg->eap_fast_a_id == NULL) { 471 wpa_printf(MSG_INFO, "EAP-FAST: No A-ID configured"); 472 eap_fast_reset(sm, data); 473 return NULL; 474 } 475 data->srv_id = os_memdup(sm->cfg->eap_fast_a_id, 476 sm->cfg->eap_fast_a_id_len); 477 if (data->srv_id == NULL) { 478 eap_fast_reset(sm, data); 479 return NULL; 480 } 481 data->srv_id_len = sm->cfg->eap_fast_a_id_len; 482 483 if (sm->cfg->eap_fast_a_id_info == NULL) { 484 wpa_printf(MSG_INFO, "EAP-FAST: No A-ID-Info configured"); 485 eap_fast_reset(sm, data); 486 return NULL; 487 } 488 data->srv_id_info = os_strdup(sm->cfg->eap_fast_a_id_info); 489 if (data->srv_id_info == NULL) { 490 eap_fast_reset(sm, data); 491 return NULL; 492 } 493 494 /* PAC-Key lifetime in seconds (hard limit) */ 495 data->pac_key_lifetime = sm->cfg->pac_key_lifetime; 496 497 /* 498 * PAC-Key refresh time in seconds (soft limit on remaining hard 499 * limit). The server will generate a new PAC-Key when this number of 500 * seconds (or fewer) of the lifetime remains. 501 */ 502 data->pac_key_refresh_time = sm->cfg->pac_key_refresh_time; 503 504 return data; 505 } 506 507 508 static void eap_fast_reset(struct eap_sm *sm, void *priv) 509 { 510 struct eap_fast_data *data = priv; 511 if (data == NULL) 512 return; 513 if (data->phase2_priv && data->phase2_method) 514 data->phase2_method->reset(sm, data->phase2_priv); 515 eap_server_tls_ssl_deinit(sm, &data->ssl); 516 os_free(data->srv_id); 517 os_free(data->srv_id_info); 518 os_free(data->key_block_p); 519 wpabuf_free(data->pending_phase2_resp); 520 os_free(data->identity); 521 bin_clear_free(data, sizeof(*data)); 522 } 523 524 525 static struct wpabuf * eap_fast_build_start(struct eap_sm *sm, 526 struct eap_fast_data *data, u8 id) 527 { 528 struct wpabuf *req; 529 530 req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_FAST, 531 1 + sizeof(struct pac_tlv_hdr) + data->srv_id_len, 532 EAP_CODE_REQUEST, id); 533 if (req == NULL) { 534 wpa_printf(MSG_ERROR, "EAP-FAST: Failed to allocate memory for" 535 " request"); 536 eap_fast_state(data, FAILURE); 537 return NULL; 538 } 539 540 wpabuf_put_u8(req, EAP_TLS_FLAGS_START | data->fast_version); 541 542 /* RFC 4851, 4.1.1. Authority ID Data */ 543 eap_fast_put_tlv(req, PAC_TYPE_A_ID, data->srv_id, data->srv_id_len); 544 545 eap_fast_state(data, PHASE1); 546 547 return req; 548 } 549 550 551 static int eap_fast_phase1_done(struct eap_sm *sm, struct eap_fast_data *data) 552 { 553 char cipher[64]; 554 555 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase1 done, starting Phase2"); 556 557 if (tls_get_cipher(sm->cfg->ssl_ctx, data->ssl.conn, 558 cipher, sizeof(cipher)) < 0) { 559 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to get cipher " 560 "information"); 561 eap_fast_state(data, FAILURE); 562 return -1; 563 } 564 data->anon_provisioning = os_strstr(cipher, "ADH") != NULL; 565 566 if (data->anon_provisioning) { 567 wpa_printf(MSG_DEBUG, "EAP-FAST: Anonymous provisioning"); 568 eap_fast_derive_key_provisioning(sm, data); 569 } else 570 eap_fast_derive_key_auth(sm, data); 571 572 eap_fast_state(data, PHASE2_START); 573 574 return 0; 575 } 576 577 578 static struct wpabuf * eap_fast_build_phase2_req(struct eap_sm *sm, 579 struct eap_fast_data *data, 580 u8 id) 581 { 582 struct wpabuf *req; 583 584 if (data->phase2_priv == NULL) { 585 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 method not " 586 "initialized"); 587 return NULL; 588 } 589 req = data->phase2_method->buildReq(sm, data->phase2_priv, id); 590 if (req == NULL) 591 return NULL; 592 593 wpa_hexdump_buf_key(MSG_MSGDUMP, "EAP-FAST: Phase 2 EAP-Request", req); 594 return eap_fast_tlv_eap_payload(req); 595 } 596 597 598 static struct wpabuf * eap_fast_build_crypto_binding( 599 struct eap_sm *sm, struct eap_fast_data *data) 600 { 601 struct wpabuf *buf; 602 struct eap_tlv_result_tlv *result; 603 struct eap_tlv_crypto_binding_tlv *binding; 604 605 buf = wpabuf_alloc(2 * sizeof(*result) + sizeof(*binding)); 606 if (buf == NULL) 607 return NULL; 608 609 if (data->send_new_pac || data->anon_provisioning || 610 data->phase2_method) 611 data->final_result = 0; 612 else 613 data->final_result = 1; 614 615 if (!data->final_result || data->eap_seq > 1) { 616 /* Intermediate-Result */ 617 wpa_printf(MSG_DEBUG, "EAP-FAST: Add Intermediate-Result TLV " 618 "(status=SUCCESS)"); 619 result = wpabuf_put(buf, sizeof(*result)); 620 result->tlv_type = host_to_be16( 621 EAP_TLV_TYPE_MANDATORY | 622 EAP_TLV_INTERMEDIATE_RESULT_TLV); 623 result->length = host_to_be16(2); 624 result->status = host_to_be16(EAP_TLV_RESULT_SUCCESS); 625 } 626 627 if (data->final_result) { 628 /* Result TLV */ 629 wpa_printf(MSG_DEBUG, "EAP-FAST: Add Result TLV " 630 "(status=SUCCESS)"); 631 result = wpabuf_put(buf, sizeof(*result)); 632 result->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY | 633 EAP_TLV_RESULT_TLV); 634 result->length = host_to_be16(2); 635 result->status = host_to_be16(EAP_TLV_RESULT_SUCCESS); 636 } 637 638 /* Crypto-Binding TLV */ 639 binding = wpabuf_put(buf, sizeof(*binding)); 640 binding->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY | 641 EAP_TLV_CRYPTO_BINDING_TLV); 642 binding->length = host_to_be16(sizeof(*binding) - 643 sizeof(struct eap_tlv_hdr)); 644 binding->version = EAP_FAST_VERSION; 645 binding->received_version = data->peer_version; 646 binding->subtype = EAP_TLV_CRYPTO_BINDING_SUBTYPE_REQUEST; 647 if (random_get_bytes(binding->nonce, sizeof(binding->nonce)) < 0) { 648 wpabuf_free(buf); 649 return NULL; 650 } 651 652 /* 653 * RFC 4851, Section 4.2.8: 654 * The nonce in a request MUST have its least significant bit set to 0. 655 */ 656 binding->nonce[sizeof(binding->nonce) - 1] &= ~0x01; 657 658 os_memcpy(data->crypto_binding_nonce, binding->nonce, 659 sizeof(binding->nonce)); 660 661 /* 662 * RFC 4851, Section 5.3: 663 * CMK = CMK[j] 664 * Compound-MAC = HMAC-SHA1( CMK, Crypto-Binding TLV ) 665 */ 666 667 hmac_sha1(data->cmk, EAP_FAST_CMK_LEN, 668 (u8 *) binding, sizeof(*binding), 669 binding->compound_mac); 670 671 wpa_printf(MSG_DEBUG, "EAP-FAST: Add Crypto-Binding TLV: Version %d " 672 "Received Version %d SubType %d", 673 binding->version, binding->received_version, 674 binding->subtype); 675 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE", 676 binding->nonce, sizeof(binding->nonce)); 677 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC", 678 binding->compound_mac, sizeof(binding->compound_mac)); 679 680 return buf; 681 } 682 683 684 static struct wpabuf * eap_fast_build_pac(struct eap_sm *sm, 685 struct eap_fast_data *data) 686 { 687 u8 pac_key[EAP_FAST_PAC_KEY_LEN]; 688 u8 *pac_buf, *pac_opaque; 689 struct wpabuf *buf; 690 u8 *pos; 691 size_t buf_len, srv_id_info_len, pac_len; 692 struct eap_tlv_hdr *pac_tlv; 693 struct pac_tlv_hdr *pac_info; 694 struct eap_tlv_result_tlv *result; 695 struct os_time now; 696 697 if (random_get_bytes(pac_key, EAP_FAST_PAC_KEY_LEN) < 0 || 698 os_get_time(&now) < 0) 699 return NULL; 700 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: Generated PAC-Key", 701 pac_key, EAP_FAST_PAC_KEY_LEN); 702 703 pac_len = (2 + EAP_FAST_PAC_KEY_LEN) + (2 + 4) + 704 (2 + sm->identity_len) + 8; 705 pac_buf = os_malloc(pac_len); 706 if (pac_buf == NULL) 707 return NULL; 708 709 srv_id_info_len = os_strlen(data->srv_id_info); 710 711 pos = pac_buf; 712 *pos++ = PAC_OPAQUE_TYPE_KEY; 713 *pos++ = EAP_FAST_PAC_KEY_LEN; 714 os_memcpy(pos, pac_key, EAP_FAST_PAC_KEY_LEN); 715 pos += EAP_FAST_PAC_KEY_LEN; 716 717 *pos++ = PAC_OPAQUE_TYPE_LIFETIME; 718 *pos++ = 4; 719 WPA_PUT_BE32(pos, now.sec + data->pac_key_lifetime); 720 pos += 4; 721 722 if (sm->identity) { 723 *pos++ = PAC_OPAQUE_TYPE_IDENTITY; 724 *pos++ = sm->identity_len; 725 os_memcpy(pos, sm->identity, sm->identity_len); 726 pos += sm->identity_len; 727 } 728 729 pac_len = pos - pac_buf; 730 while (pac_len % 8) { 731 *pos++ = PAC_OPAQUE_TYPE_PAD; 732 pac_len++; 733 } 734 735 pac_opaque = os_malloc(pac_len + 8); 736 if (pac_opaque == NULL) { 737 os_free(pac_buf); 738 return NULL; 739 } 740 if (aes_wrap(data->pac_opaque_encr, sizeof(data->pac_opaque_encr), 741 pac_len / 8, pac_buf, pac_opaque) < 0) { 742 os_free(pac_buf); 743 os_free(pac_opaque); 744 return NULL; 745 } 746 os_free(pac_buf); 747 748 pac_len += 8; 749 wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Opaque", 750 pac_opaque, pac_len); 751 752 buf_len = sizeof(*pac_tlv) + 753 sizeof(struct pac_tlv_hdr) + EAP_FAST_PAC_KEY_LEN + 754 sizeof(struct pac_tlv_hdr) + pac_len + 755 data->srv_id_len + srv_id_info_len + 100 + sizeof(*result); 756 buf = wpabuf_alloc(buf_len); 757 if (buf == NULL) { 758 os_free(pac_opaque); 759 return NULL; 760 } 761 762 /* Result TLV */ 763 wpa_printf(MSG_DEBUG, "EAP-FAST: Add Result TLV (status=SUCCESS)"); 764 result = wpabuf_put(buf, sizeof(*result)); 765 WPA_PUT_BE16((u8 *) &result->tlv_type, 766 EAP_TLV_TYPE_MANDATORY | EAP_TLV_RESULT_TLV); 767 WPA_PUT_BE16((u8 *) &result->length, 2); 768 WPA_PUT_BE16((u8 *) &result->status, EAP_TLV_RESULT_SUCCESS); 769 770 /* PAC TLV */ 771 wpa_printf(MSG_DEBUG, "EAP-FAST: Add PAC TLV"); 772 pac_tlv = wpabuf_put(buf, sizeof(*pac_tlv)); 773 pac_tlv->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY | 774 EAP_TLV_PAC_TLV); 775 776 /* PAC-Key */ 777 eap_fast_put_tlv(buf, PAC_TYPE_PAC_KEY, pac_key, EAP_FAST_PAC_KEY_LEN); 778 779 /* PAC-Opaque */ 780 eap_fast_put_tlv(buf, PAC_TYPE_PAC_OPAQUE, pac_opaque, pac_len); 781 os_free(pac_opaque); 782 783 /* PAC-Info */ 784 pac_info = wpabuf_put(buf, sizeof(*pac_info)); 785 pac_info->type = host_to_be16(PAC_TYPE_PAC_INFO); 786 787 /* PAC-Lifetime (inside PAC-Info) */ 788 eap_fast_put_tlv_hdr(buf, PAC_TYPE_CRED_LIFETIME, 4); 789 wpabuf_put_be32(buf, now.sec + data->pac_key_lifetime); 790 791 /* A-ID (inside PAC-Info) */ 792 eap_fast_put_tlv(buf, PAC_TYPE_A_ID, data->srv_id, data->srv_id_len); 793 794 /* Note: headers may be misaligned after A-ID */ 795 796 if (sm->identity) { 797 eap_fast_put_tlv(buf, PAC_TYPE_I_ID, sm->identity, 798 sm->identity_len); 799 } 800 801 /* A-ID-Info (inside PAC-Info) */ 802 eap_fast_put_tlv(buf, PAC_TYPE_A_ID_INFO, data->srv_id_info, 803 srv_id_info_len); 804 805 /* PAC-Type (inside PAC-Info) */ 806 eap_fast_put_tlv_hdr(buf, PAC_TYPE_PAC_TYPE, 2); 807 wpabuf_put_be16(buf, PAC_TYPE_TUNNEL_PAC); 808 809 /* Update PAC-Info and PAC TLV Length fields */ 810 pos = wpabuf_put(buf, 0); 811 pac_info->len = host_to_be16(pos - (u8 *) (pac_info + 1)); 812 pac_tlv->length = host_to_be16(pos - (u8 *) (pac_tlv + 1)); 813 814 return buf; 815 } 816 817 818 static int eap_fast_encrypt_phase2(struct eap_sm *sm, 819 struct eap_fast_data *data, 820 struct wpabuf *plain, int piggyback) 821 { 822 struct wpabuf *encr; 823 824 wpa_hexdump_buf_key(MSG_DEBUG, "EAP-FAST: Encrypting Phase 2 TLVs", 825 plain); 826 encr = eap_server_tls_encrypt(sm, &data->ssl, plain); 827 wpabuf_free(plain); 828 829 if (!encr) 830 return -1; 831 832 if (data->ssl.tls_out && piggyback) { 833 wpa_printf(MSG_DEBUG, "EAP-FAST: Piggyback Phase 2 data " 834 "(len=%d) with last Phase 1 Message (len=%d " 835 "used=%d)", 836 (int) wpabuf_len(encr), 837 (int) wpabuf_len(data->ssl.tls_out), 838 (int) data->ssl.tls_out_pos); 839 if (wpabuf_resize(&data->ssl.tls_out, wpabuf_len(encr)) < 0) { 840 wpa_printf(MSG_WARNING, "EAP-FAST: Failed to resize " 841 "output buffer"); 842 wpabuf_free(encr); 843 return -1; 844 } 845 wpabuf_put_buf(data->ssl.tls_out, encr); 846 wpabuf_free(encr); 847 } else { 848 wpabuf_free(data->ssl.tls_out); 849 data->ssl.tls_out_pos = 0; 850 data->ssl.tls_out = encr; 851 } 852 853 return 0; 854 } 855 856 857 static struct wpabuf * eap_fast_buildReq(struct eap_sm *sm, void *priv, u8 id) 858 { 859 struct eap_fast_data *data = priv; 860 struct wpabuf *req = NULL; 861 int piggyback = 0; 862 863 if (data->ssl.state == FRAG_ACK) { 864 return eap_server_tls_build_ack(id, EAP_TYPE_FAST, 865 data->fast_version); 866 } 867 868 if (data->ssl.state == WAIT_FRAG_ACK) { 869 return eap_server_tls_build_msg(&data->ssl, EAP_TYPE_FAST, 870 data->fast_version, id); 871 } 872 873 switch (data->state) { 874 case START: 875 return eap_fast_build_start(sm, data, id); 876 case PHASE1: 877 if (tls_connection_established(sm->cfg->ssl_ctx, 878 data->ssl.conn)) { 879 if (eap_fast_phase1_done(sm, data) < 0) 880 return NULL; 881 if (data->state == PHASE2_START) { 882 /* 883 * Try to generate Phase 2 data to piggyback 884 * with the end of Phase 1 to avoid extra 885 * roundtrip. 886 */ 887 wpa_printf(MSG_DEBUG, "EAP-FAST: Try to start " 888 "Phase 2"); 889 if (eap_fast_process_phase2_start(sm, data)) 890 break; 891 req = eap_fast_build_phase2_req(sm, data, id); 892 piggyback = 1; 893 } 894 } 895 break; 896 case PHASE2_ID: 897 case PHASE2_METHOD: 898 req = eap_fast_build_phase2_req(sm, data, id); 899 break; 900 case CRYPTO_BINDING: 901 req = eap_fast_build_crypto_binding(sm, data); 902 if (data->phase2_method) { 903 /* 904 * Include the start of the next EAP method in the 905 * sequence in the same message with Crypto-Binding to 906 * save a round-trip. 907 */ 908 struct wpabuf *eap; 909 eap = eap_fast_build_phase2_req(sm, data, id); 910 req = wpabuf_concat(req, eap); 911 eap_fast_state(data, PHASE2_METHOD); 912 } 913 break; 914 case REQUEST_PAC: 915 req = eap_fast_build_pac(sm, data); 916 break; 917 default: 918 wpa_printf(MSG_DEBUG, "EAP-FAST: %s - unexpected state %d", 919 __func__, data->state); 920 return NULL; 921 } 922 923 if (req && 924 eap_fast_encrypt_phase2(sm, data, req, piggyback) < 0) 925 return NULL; 926 927 return eap_server_tls_build_msg(&data->ssl, EAP_TYPE_FAST, 928 data->fast_version, id); 929 } 930 931 932 static bool eap_fast_check(struct eap_sm *sm, void *priv, 933 struct wpabuf *respData) 934 { 935 const u8 *pos; 936 size_t len; 937 938 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_FAST, respData, &len); 939 if (pos == NULL || len < 1) { 940 wpa_printf(MSG_INFO, "EAP-FAST: Invalid frame"); 941 return true; 942 } 943 944 return false; 945 } 946 947 948 static int eap_fast_phase2_init(struct eap_sm *sm, struct eap_fast_data *data, 949 int vendor, enum eap_type eap_type) 950 { 951 if (data->phase2_priv && data->phase2_method) { 952 data->phase2_method->reset(sm, data->phase2_priv); 953 data->phase2_method = NULL; 954 data->phase2_priv = NULL; 955 } 956 data->phase2_method = eap_server_get_eap_method(vendor, eap_type); 957 if (!data->phase2_method) 958 return -1; 959 960 if (data->key_block_p) { 961 sm->auth_challenge = data->key_block_p->server_challenge; 962 sm->peer_challenge = data->key_block_p->client_challenge; 963 } 964 sm->init_phase2 = 1; 965 data->phase2_priv = data->phase2_method->init(sm); 966 sm->init_phase2 = 0; 967 sm->auth_challenge = NULL; 968 sm->peer_challenge = NULL; 969 970 return data->phase2_priv == NULL ? -1 : 0; 971 } 972 973 974 static void eap_fast_process_phase2_response(struct eap_sm *sm, 975 struct eap_fast_data *data, 976 u8 *in_data, size_t in_len) 977 { 978 int next_vendor = EAP_VENDOR_IETF; 979 enum eap_type next_type = EAP_TYPE_NONE; 980 struct eap_hdr *hdr; 981 u8 *pos; 982 size_t left; 983 struct wpabuf buf; 984 const struct eap_method *m = data->phase2_method; 985 void *priv = data->phase2_priv; 986 987 if (priv == NULL) { 988 wpa_printf(MSG_DEBUG, "EAP-FAST: %s - Phase2 not " 989 "initialized?!", __func__); 990 return; 991 } 992 993 hdr = (struct eap_hdr *) in_data; 994 pos = (u8 *) (hdr + 1); 995 996 if (in_len > sizeof(*hdr) && *pos == EAP_TYPE_NAK) { 997 left = in_len - sizeof(*hdr); 998 wpa_hexdump(MSG_DEBUG, "EAP-FAST: Phase2 type Nak'ed; " 999 "allowed types", pos + 1, left - 1); 1000 #ifdef EAP_SERVER_TNC 1001 if (m && m->vendor == EAP_VENDOR_IETF && 1002 m->method == EAP_TYPE_TNC) { 1003 wpa_printf(MSG_DEBUG, "EAP-FAST: Peer Nak'ed required " 1004 "TNC negotiation"); 1005 next_vendor = EAP_VENDOR_IETF; 1006 next_type = eap_fast_req_failure(sm, data); 1007 eap_fast_phase2_init(sm, data, next_vendor, next_type); 1008 return; 1009 } 1010 #endif /* EAP_SERVER_TNC */ 1011 eap_sm_process_nak(sm, pos + 1, left - 1); 1012 if (sm->user && sm->user_eap_method_index < EAP_MAX_METHODS && 1013 sm->user->methods[sm->user_eap_method_index].method != 1014 EAP_TYPE_NONE) { 1015 next_vendor = sm->user->methods[ 1016 sm->user_eap_method_index].vendor; 1017 next_type = sm->user->methods[ 1018 sm->user_eap_method_index++].method; 1019 wpa_printf(MSG_DEBUG, "EAP-FAST: try EAP type %u:%u", 1020 next_vendor, next_type); 1021 } else { 1022 next_vendor = EAP_VENDOR_IETF; 1023 next_type = eap_fast_req_failure(sm, data); 1024 } 1025 eap_fast_phase2_init(sm, data, next_vendor, next_type); 1026 return; 1027 } 1028 1029 wpabuf_set(&buf, in_data, in_len); 1030 1031 if (m->check(sm, priv, &buf)) { 1032 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase2 check() asked to " 1033 "ignore the packet"); 1034 eap_fast_req_failure(sm, data); 1035 return; 1036 } 1037 1038 m->process(sm, priv, &buf); 1039 1040 if (!m->isDone(sm, priv)) 1041 return; 1042 1043 if (!m->isSuccess(sm, priv)) { 1044 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase2 method failed"); 1045 next_vendor = EAP_VENDOR_IETF; 1046 next_type = eap_fast_req_failure(sm, data); 1047 eap_fast_phase2_init(sm, data, next_vendor, next_type); 1048 return; 1049 } 1050 1051 switch (data->state) { 1052 case PHASE2_ID: 1053 if (eap_user_get(sm, sm->identity, sm->identity_len, 1) != 0) { 1054 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: Phase2 " 1055 "Identity not found in the user " 1056 "database", 1057 sm->identity, sm->identity_len); 1058 next_vendor = EAP_VENDOR_IETF; 1059 next_type = eap_fast_req_failure(sm, data); 1060 break; 1061 } 1062 1063 eap_fast_state(data, PHASE2_METHOD); 1064 if (data->anon_provisioning) { 1065 /* 1066 * Only EAP-MSCHAPv2 is allowed for anonymous 1067 * provisioning. 1068 */ 1069 next_vendor = EAP_VENDOR_IETF; 1070 next_type = EAP_TYPE_MSCHAPV2; 1071 sm->user_eap_method_index = 0; 1072 } else { 1073 next_vendor = sm->user->methods[0].vendor; 1074 next_type = sm->user->methods[0].method; 1075 sm->user_eap_method_index = 1; 1076 } 1077 wpa_printf(MSG_DEBUG, "EAP-FAST: try EAP type %u:%u", 1078 next_vendor, next_type); 1079 break; 1080 case PHASE2_METHOD: 1081 case CRYPTO_BINDING: 1082 eap_fast_update_icmk(sm, data); 1083 eap_fast_state(data, CRYPTO_BINDING); 1084 data->eap_seq++; 1085 next_vendor = EAP_VENDOR_IETF; 1086 next_type = EAP_TYPE_NONE; 1087 #ifdef EAP_SERVER_TNC 1088 if (sm->cfg->tnc && !data->tnc_started) { 1089 wpa_printf(MSG_DEBUG, "EAP-FAST: Initialize TNC"); 1090 next_vendor = EAP_VENDOR_IETF; 1091 next_type = EAP_TYPE_TNC; 1092 data->tnc_started = 1; 1093 } 1094 #endif /* EAP_SERVER_TNC */ 1095 break; 1096 case FAILURE: 1097 break; 1098 default: 1099 wpa_printf(MSG_DEBUG, "EAP-FAST: %s - unexpected state %d", 1100 __func__, data->state); 1101 break; 1102 } 1103 1104 eap_fast_phase2_init(sm, data, next_vendor, next_type); 1105 } 1106 1107 1108 static void eap_fast_process_phase2_eap(struct eap_sm *sm, 1109 struct eap_fast_data *data, 1110 u8 *in_data, size_t in_len) 1111 { 1112 struct eap_hdr *hdr; 1113 size_t len; 1114 1115 hdr = (struct eap_hdr *) in_data; 1116 if (in_len < (int) sizeof(*hdr)) { 1117 wpa_printf(MSG_INFO, "EAP-FAST: Too short Phase 2 " 1118 "EAP frame (len=%lu)", (unsigned long) in_len); 1119 eap_fast_req_failure(sm, data); 1120 return; 1121 } 1122 len = be_to_host16(hdr->length); 1123 if (len > in_len) { 1124 wpa_printf(MSG_INFO, "EAP-FAST: Length mismatch in " 1125 "Phase 2 EAP frame (len=%lu hdr->length=%lu)", 1126 (unsigned long) in_len, (unsigned long) len); 1127 eap_fast_req_failure(sm, data); 1128 return; 1129 } 1130 wpa_printf(MSG_DEBUG, "EAP-FAST: Received Phase 2: code=%d " 1131 "identifier=%d length=%lu", hdr->code, hdr->identifier, 1132 (unsigned long) len); 1133 switch (hdr->code) { 1134 case EAP_CODE_RESPONSE: 1135 eap_fast_process_phase2_response(sm, data, (u8 *) hdr, len); 1136 break; 1137 default: 1138 wpa_printf(MSG_INFO, "EAP-FAST: Unexpected code=%d in " 1139 "Phase 2 EAP header", hdr->code); 1140 break; 1141 } 1142 } 1143 1144 1145 static int eap_fast_parse_tlvs(struct wpabuf *data, 1146 struct eap_fast_tlv_parse *tlv) 1147 { 1148 int mandatory, tlv_type, res; 1149 size_t len; 1150 u8 *pos, *end; 1151 1152 os_memset(tlv, 0, sizeof(*tlv)); 1153 1154 pos = wpabuf_mhead(data); 1155 end = pos + wpabuf_len(data); 1156 while (end - pos > 4) { 1157 mandatory = pos[0] & 0x80; 1158 tlv_type = WPA_GET_BE16(pos) & 0x3fff; 1159 pos += 2; 1160 len = WPA_GET_BE16(pos); 1161 pos += 2; 1162 if (len > (size_t) (end - pos)) { 1163 wpa_printf(MSG_INFO, "EAP-FAST: TLV overflow"); 1164 return -1; 1165 } 1166 wpa_printf(MSG_DEBUG, "EAP-FAST: Received Phase 2: " 1167 "TLV type %d length %u%s", 1168 tlv_type, (unsigned int) len, 1169 mandatory ? " (mandatory)" : ""); 1170 1171 res = eap_fast_parse_tlv(tlv, tlv_type, pos, len); 1172 if (res == -2) 1173 break; 1174 if (res < 0) { 1175 if (mandatory) { 1176 wpa_printf(MSG_DEBUG, "EAP-FAST: Nak unknown " 1177 "mandatory TLV type %d", tlv_type); 1178 /* TODO: generate Nak TLV */ 1179 break; 1180 } else { 1181 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignored " 1182 "unknown optional TLV type %d", 1183 tlv_type); 1184 } 1185 } 1186 1187 pos += len; 1188 } 1189 1190 return 0; 1191 } 1192 1193 1194 static int eap_fast_validate_crypto_binding( 1195 struct eap_fast_data *data, struct eap_tlv_crypto_binding_tlv *b, 1196 size_t bind_len) 1197 { 1198 u8 cmac[SHA1_MAC_LEN]; 1199 1200 wpa_printf(MSG_DEBUG, "EAP-FAST: Reply Crypto-Binding TLV: " 1201 "Version %d Received Version %d SubType %d", 1202 b->version, b->received_version, b->subtype); 1203 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE", 1204 b->nonce, sizeof(b->nonce)); 1205 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC", 1206 b->compound_mac, sizeof(b->compound_mac)); 1207 1208 if (b->version != EAP_FAST_VERSION || 1209 b->received_version != EAP_FAST_VERSION) { 1210 wpa_printf(MSG_DEBUG, "EAP-FAST: Unexpected version " 1211 "in Crypto-Binding: version %d " 1212 "received_version %d", b->version, 1213 b->received_version); 1214 return -1; 1215 } 1216 1217 if (b->subtype != EAP_TLV_CRYPTO_BINDING_SUBTYPE_RESPONSE) { 1218 wpa_printf(MSG_DEBUG, "EAP-FAST: Unexpected subtype in " 1219 "Crypto-Binding: %d", b->subtype); 1220 return -1; 1221 } 1222 1223 if (os_memcmp_const(data->crypto_binding_nonce, b->nonce, 31) != 0 || 1224 (data->crypto_binding_nonce[31] | 1) != b->nonce[31]) { 1225 wpa_printf(MSG_DEBUG, "EAP-FAST: Invalid nonce in " 1226 "Crypto-Binding"); 1227 return -1; 1228 } 1229 1230 os_memcpy(cmac, b->compound_mac, sizeof(cmac)); 1231 os_memset(b->compound_mac, 0, sizeof(cmac)); 1232 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Crypto-Binding TLV for " 1233 "Compound MAC calculation", 1234 (u8 *) b, bind_len); 1235 hmac_sha1(data->cmk, EAP_FAST_CMK_LEN, (u8 *) b, bind_len, 1236 b->compound_mac); 1237 if (os_memcmp_const(cmac, b->compound_mac, sizeof(cmac)) != 0) { 1238 wpa_hexdump(MSG_MSGDUMP, 1239 "EAP-FAST: Calculated Compound MAC", 1240 b->compound_mac, sizeof(cmac)); 1241 wpa_printf(MSG_INFO, "EAP-FAST: Compound MAC did not " 1242 "match"); 1243 return -1; 1244 } 1245 1246 return 0; 1247 } 1248 1249 1250 static int eap_fast_pac_type(u8 *pac, size_t len, u16 type) 1251 { 1252 struct eap_tlv_pac_type_tlv *tlv; 1253 1254 if (pac == NULL || len != sizeof(*tlv)) 1255 return 0; 1256 1257 tlv = (struct eap_tlv_pac_type_tlv *) pac; 1258 1259 return be_to_host16(tlv->tlv_type) == PAC_TYPE_PAC_TYPE && 1260 be_to_host16(tlv->length) == 2 && 1261 be_to_host16(tlv->pac_type) == type; 1262 } 1263 1264 1265 static void eap_fast_process_phase2_tlvs(struct eap_sm *sm, 1266 struct eap_fast_data *data, 1267 struct wpabuf *in_data) 1268 { 1269 struct eap_fast_tlv_parse tlv; 1270 int check_crypto_binding = data->state == CRYPTO_BINDING; 1271 1272 if (eap_fast_parse_tlvs(in_data, &tlv) < 0) { 1273 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to parse received " 1274 "Phase 2 TLVs"); 1275 return; 1276 } 1277 1278 if (tlv.result == EAP_TLV_RESULT_FAILURE) { 1279 wpa_printf(MSG_DEBUG, "EAP-FAST: Result TLV indicated " 1280 "failure"); 1281 eap_fast_state(data, FAILURE); 1282 return; 1283 } 1284 1285 if (data->state == REQUEST_PAC) { 1286 u16 type, len, res; 1287 if (tlv.pac == NULL || tlv.pac_len < 6) { 1288 wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC " 1289 "Acknowledgement received"); 1290 eap_fast_state(data, FAILURE); 1291 return; 1292 } 1293 1294 type = WPA_GET_BE16(tlv.pac); 1295 len = WPA_GET_BE16(tlv.pac + 2); 1296 res = WPA_GET_BE16(tlv.pac + 4); 1297 1298 if (type != PAC_TYPE_PAC_ACKNOWLEDGEMENT || len != 2 || 1299 res != EAP_TLV_RESULT_SUCCESS) { 1300 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV did not " 1301 "contain acknowledgement"); 1302 eap_fast_state(data, FAILURE); 1303 return; 1304 } 1305 1306 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Acknowledgement received " 1307 "- PAC provisioning succeeded"); 1308 eap_fast_state(data, (data->anon_provisioning || 1309 data->send_new_pac == 2) ? 1310 FAILURE : SUCCESS); 1311 return; 1312 } 1313 1314 if (check_crypto_binding) { 1315 if (tlv.crypto_binding == NULL) { 1316 wpa_printf(MSG_DEBUG, "EAP-FAST: No Crypto-Binding " 1317 "TLV received"); 1318 eap_fast_state(data, FAILURE); 1319 return; 1320 } 1321 1322 if (data->final_result && 1323 tlv.result != EAP_TLV_RESULT_SUCCESS) { 1324 wpa_printf(MSG_DEBUG, "EAP-FAST: Crypto-Binding TLV " 1325 "without Success Result"); 1326 eap_fast_state(data, FAILURE); 1327 return; 1328 } 1329 1330 if (!data->final_result && 1331 tlv.iresult != EAP_TLV_RESULT_SUCCESS) { 1332 wpa_printf(MSG_DEBUG, "EAP-FAST: Crypto-Binding TLV " 1333 "without intermediate Success Result"); 1334 eap_fast_state(data, FAILURE); 1335 return; 1336 } 1337 1338 if (eap_fast_validate_crypto_binding(data, tlv.crypto_binding, 1339 tlv.crypto_binding_len)) { 1340 eap_fast_state(data, FAILURE); 1341 return; 1342 } 1343 1344 wpa_printf(MSG_DEBUG, "EAP-FAST: Valid Crypto-Binding TLV " 1345 "received"); 1346 if (data->final_result) { 1347 wpa_printf(MSG_DEBUG, "EAP-FAST: Authentication " 1348 "completed successfully"); 1349 } 1350 1351 if (data->anon_provisioning && 1352 sm->cfg->eap_fast_prov != ANON_PROV && 1353 sm->cfg->eap_fast_prov != BOTH_PROV) { 1354 wpa_printf(MSG_DEBUG, "EAP-FAST: Client is trying to " 1355 "use unauthenticated provisioning which is " 1356 "disabled"); 1357 eap_fast_state(data, FAILURE); 1358 return; 1359 } 1360 1361 if (sm->cfg->eap_fast_prov != AUTH_PROV && 1362 sm->cfg->eap_fast_prov != BOTH_PROV && 1363 tlv.request_action == EAP_TLV_ACTION_PROCESS_TLV && 1364 eap_fast_pac_type(tlv.pac, tlv.pac_len, 1365 PAC_TYPE_TUNNEL_PAC)) { 1366 wpa_printf(MSG_DEBUG, "EAP-FAST: Client is trying to " 1367 "use authenticated provisioning which is " 1368 "disabled"); 1369 eap_fast_state(data, FAILURE); 1370 return; 1371 } 1372 1373 if (data->anon_provisioning || 1374 (tlv.request_action == EAP_TLV_ACTION_PROCESS_TLV && 1375 eap_fast_pac_type(tlv.pac, tlv.pac_len, 1376 PAC_TYPE_TUNNEL_PAC))) { 1377 wpa_printf(MSG_DEBUG, "EAP-FAST: Requested a new " 1378 "Tunnel PAC"); 1379 eap_fast_state(data, REQUEST_PAC); 1380 } else if (data->send_new_pac) { 1381 wpa_printf(MSG_DEBUG, "EAP-FAST: Server triggered " 1382 "re-keying of Tunnel PAC"); 1383 eap_fast_state(data, REQUEST_PAC); 1384 } else if (data->final_result) 1385 eap_fast_state(data, SUCCESS); 1386 } 1387 1388 if (tlv.eap_payload_tlv) { 1389 eap_fast_process_phase2_eap(sm, data, tlv.eap_payload_tlv, 1390 tlv.eap_payload_tlv_len); 1391 } 1392 } 1393 1394 1395 static void eap_fast_process_phase2(struct eap_sm *sm, 1396 struct eap_fast_data *data, 1397 struct wpabuf *in_buf) 1398 { 1399 struct wpabuf *in_decrypted; 1400 1401 wpa_printf(MSG_DEBUG, "EAP-FAST: Received %lu bytes encrypted data for" 1402 " Phase 2", (unsigned long) wpabuf_len(in_buf)); 1403 1404 if (data->pending_phase2_resp) { 1405 wpa_printf(MSG_DEBUG, "EAP-PEAP: Pending Phase 2 response - " 1406 "skip decryption and use old data"); 1407 eap_fast_process_phase2_tlvs(sm, data, 1408 data->pending_phase2_resp); 1409 wpabuf_free(data->pending_phase2_resp); 1410 data->pending_phase2_resp = NULL; 1411 return; 1412 } 1413 1414 in_decrypted = tls_connection_decrypt(sm->cfg->ssl_ctx, data->ssl.conn, 1415 in_buf); 1416 if (in_decrypted == NULL) { 1417 wpa_printf(MSG_INFO, "EAP-FAST: Failed to decrypt Phase 2 " 1418 "data"); 1419 eap_fast_state(data, FAILURE); 1420 return; 1421 } 1422 1423 wpa_hexdump_buf_key(MSG_DEBUG, "EAP-FAST: Decrypted Phase 2 TLVs", 1424 in_decrypted); 1425 1426 eap_fast_process_phase2_tlvs(sm, data, in_decrypted); 1427 1428 if (sm->method_pending == METHOD_PENDING_WAIT) { 1429 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase2 method is in " 1430 "pending wait state - save decrypted response"); 1431 wpabuf_free(data->pending_phase2_resp); 1432 data->pending_phase2_resp = in_decrypted; 1433 return; 1434 } 1435 1436 wpabuf_free(in_decrypted); 1437 } 1438 1439 1440 static int eap_fast_process_version(struct eap_sm *sm, void *priv, 1441 int peer_version) 1442 { 1443 struct eap_fast_data *data = priv; 1444 1445 data->peer_version = peer_version; 1446 1447 if (data->force_version >= 0 && peer_version != data->force_version) { 1448 wpa_printf(MSG_INFO, "EAP-FAST: peer did not select the forced" 1449 " version (forced=%d peer=%d) - reject", 1450 data->force_version, peer_version); 1451 return -1; 1452 } 1453 1454 if (peer_version < data->fast_version) { 1455 wpa_printf(MSG_DEBUG, "EAP-FAST: peer ver=%d, own ver=%d; " 1456 "use version %d", 1457 peer_version, data->fast_version, peer_version); 1458 data->fast_version = peer_version; 1459 } 1460 1461 return 0; 1462 } 1463 1464 1465 static int eap_fast_process_phase1(struct eap_sm *sm, 1466 struct eap_fast_data *data) 1467 { 1468 if (eap_server_tls_phase1(sm, &data->ssl) < 0) { 1469 wpa_printf(MSG_INFO, "EAP-FAST: TLS processing failed"); 1470 eap_fast_state(data, FAILURE); 1471 return -1; 1472 } 1473 1474 if (!tls_connection_established(sm->cfg->ssl_ctx, data->ssl.conn) || 1475 wpabuf_len(data->ssl.tls_out) > 0) 1476 return 1; 1477 1478 /* 1479 * Phase 1 was completed with the received message (e.g., when using 1480 * abbreviated handshake), so Phase 2 can be started immediately 1481 * without having to send through an empty message to the peer. 1482 */ 1483 1484 return eap_fast_phase1_done(sm, data); 1485 } 1486 1487 1488 static int eap_fast_process_phase2_start(struct eap_sm *sm, 1489 struct eap_fast_data *data) 1490 { 1491 int next_vendor; 1492 enum eap_type next_type; 1493 1494 if (data->identity) { 1495 os_free(sm->identity); 1496 sm->identity = data->identity; 1497 data->identity = NULL; 1498 sm->identity_len = data->identity_len; 1499 data->identity_len = 0; 1500 sm->require_identity_match = 1; 1501 if (eap_user_get(sm, sm->identity, sm->identity_len, 1) != 0) { 1502 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: " 1503 "Phase2 Identity not found " 1504 "in the user database", 1505 sm->identity, sm->identity_len); 1506 next_vendor = EAP_VENDOR_IETF; 1507 next_type = eap_fast_req_failure(sm, data); 1508 } else { 1509 wpa_printf(MSG_DEBUG, "EAP-FAST: Identity already " 1510 "known - skip Phase 2 Identity Request"); 1511 next_vendor = sm->user->methods[0].vendor; 1512 next_type = sm->user->methods[0].method; 1513 sm->user_eap_method_index = 1; 1514 } 1515 1516 eap_fast_state(data, PHASE2_METHOD); 1517 } else { 1518 eap_fast_state(data, PHASE2_ID); 1519 next_vendor = EAP_VENDOR_IETF; 1520 next_type = EAP_TYPE_IDENTITY; 1521 } 1522 1523 return eap_fast_phase2_init(sm, data, next_vendor, next_type); 1524 } 1525 1526 1527 static void eap_fast_process_msg(struct eap_sm *sm, void *priv, 1528 const struct wpabuf *respData) 1529 { 1530 struct eap_fast_data *data = priv; 1531 1532 switch (data->state) { 1533 case PHASE1: 1534 if (eap_fast_process_phase1(sm, data)) 1535 break; 1536 1537 /* fall through */ 1538 case PHASE2_START: 1539 eap_fast_process_phase2_start(sm, data); 1540 break; 1541 case PHASE2_ID: 1542 case PHASE2_METHOD: 1543 case CRYPTO_BINDING: 1544 case REQUEST_PAC: 1545 eap_fast_process_phase2(sm, data, data->ssl.tls_in); 1546 break; 1547 default: 1548 wpa_printf(MSG_DEBUG, "EAP-FAST: Unexpected state %d in %s", 1549 data->state, __func__); 1550 break; 1551 } 1552 } 1553 1554 1555 static void eap_fast_process(struct eap_sm *sm, void *priv, 1556 struct wpabuf *respData) 1557 { 1558 struct eap_fast_data *data = priv; 1559 if (eap_server_tls_process(sm, &data->ssl, respData, data, 1560 EAP_TYPE_FAST, eap_fast_process_version, 1561 eap_fast_process_msg) < 0) 1562 eap_fast_state(data, FAILURE); 1563 } 1564 1565 1566 static bool eap_fast_isDone(struct eap_sm *sm, void *priv) 1567 { 1568 struct eap_fast_data *data = priv; 1569 return data->state == SUCCESS || data->state == FAILURE; 1570 } 1571 1572 1573 static u8 * eap_fast_getKey(struct eap_sm *sm, void *priv, size_t *len) 1574 { 1575 struct eap_fast_data *data = priv; 1576 u8 *eapKeyData; 1577 1578 if (data->state != SUCCESS) 1579 return NULL; 1580 1581 eapKeyData = os_malloc(EAP_FAST_KEY_LEN); 1582 if (eapKeyData == NULL) 1583 return NULL; 1584 1585 if (eap_fast_derive_eap_msk(data->simck, eapKeyData) < 0) { 1586 os_free(eapKeyData); 1587 return NULL; 1588 } 1589 *len = EAP_FAST_KEY_LEN; 1590 1591 return eapKeyData; 1592 } 1593 1594 1595 static u8 * eap_fast_get_emsk(struct eap_sm *sm, void *priv, size_t *len) 1596 { 1597 struct eap_fast_data *data = priv; 1598 u8 *eapKeyData; 1599 1600 if (data->state != SUCCESS) 1601 return NULL; 1602 1603 eapKeyData = os_malloc(EAP_EMSK_LEN); 1604 if (eapKeyData == NULL) 1605 return NULL; 1606 1607 if (eap_fast_derive_eap_emsk(data->simck, eapKeyData) < 0) { 1608 os_free(eapKeyData); 1609 return NULL; 1610 } 1611 *len = EAP_EMSK_LEN; 1612 1613 return eapKeyData; 1614 } 1615 1616 1617 static bool eap_fast_isSuccess(struct eap_sm *sm, void *priv) 1618 { 1619 struct eap_fast_data *data = priv; 1620 return data->state == SUCCESS; 1621 } 1622 1623 1624 static u8 * eap_fast_get_session_id(struct eap_sm *sm, void *priv, size_t *len) 1625 { 1626 struct eap_fast_data *data = priv; 1627 1628 if (data->state != SUCCESS) 1629 return NULL; 1630 1631 return eap_server_tls_derive_session_id(sm, &data->ssl, EAP_TYPE_FAST, 1632 len); 1633 } 1634 1635 1636 int eap_server_fast_register(void) 1637 { 1638 struct eap_method *eap; 1639 1640 eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION, 1641 EAP_VENDOR_IETF, EAP_TYPE_FAST, "FAST"); 1642 if (eap == NULL) 1643 return -1; 1644 1645 eap->init = eap_fast_init; 1646 eap->reset = eap_fast_reset; 1647 eap->buildReq = eap_fast_buildReq; 1648 eap->check = eap_fast_check; 1649 eap->process = eap_fast_process; 1650 eap->isDone = eap_fast_isDone; 1651 eap->getKey = eap_fast_getKey; 1652 eap->get_emsk = eap_fast_get_emsk; 1653 eap->isSuccess = eap_fast_isSuccess; 1654 eap->getSessionId = eap_fast_get_session_id; 1655 1656 return eap_server_method_register(eap); 1657 } 1658