1 /* 2 * EAP peer: EAP-TLS/PEAP/TTLS/FAST common functions 3 * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi> 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License version 2 as 7 * published by the Free Software Foundation. 8 * 9 * Alternatively, this software may be distributed under the terms of BSD 10 * license. 11 * 12 * See README and COPYING for more details. 13 */ 14 15 #include "includes.h" 16 17 #include "common.h" 18 #include "crypto/sha1.h" 19 #include "crypto/tls.h" 20 #include "eap_i.h" 21 #include "eap_tls_common.h" 22 #include "eap_config.h" 23 24 25 static int eap_tls_check_blob(struct eap_sm *sm, const char **name, 26 const u8 **data, size_t *data_len) 27 { 28 const struct wpa_config_blob *blob; 29 30 if (*name == NULL || os_strncmp(*name, "blob://", 7) != 0) 31 return 0; 32 33 blob = eap_get_config_blob(sm, *name + 7); 34 if (blob == NULL) { 35 wpa_printf(MSG_ERROR, "%s: Named configuration blob '%s' not " 36 "found", __func__, *name + 7); 37 return -1; 38 } 39 40 *name = NULL; 41 *data = blob->data; 42 *data_len = blob->len; 43 44 return 0; 45 } 46 47 48 static void eap_tls_params_flags(struct tls_connection_params *params, 49 const char *txt) 50 { 51 if (txt == NULL) 52 return; 53 if (os_strstr(txt, "tls_allow_md5=1")) 54 params->flags |= TLS_CONN_ALLOW_SIGN_RSA_MD5; 55 if (os_strstr(txt, "tls_disable_time_checks=1")) 56 params->flags |= TLS_CONN_DISABLE_TIME_CHECKS; 57 } 58 59 60 static void eap_tls_params_from_conf1(struct tls_connection_params *params, 61 struct eap_peer_config *config) 62 { 63 params->ca_cert = (char *) config->ca_cert; 64 params->ca_path = (char *) config->ca_path; 65 params->client_cert = (char *) config->client_cert; 66 params->private_key = (char *) config->private_key; 67 params->private_key_passwd = (char *) config->private_key_passwd; 68 params->dh_file = (char *) config->dh_file; 69 params->subject_match = (char *) config->subject_match; 70 params->altsubject_match = (char *) config->altsubject_match; 71 params->engine = config->engine; 72 params->engine_id = config->engine_id; 73 params->pin = config->pin; 74 params->key_id = config->key_id; 75 params->cert_id = config->cert_id; 76 params->ca_cert_id = config->ca_cert_id; 77 eap_tls_params_flags(params, config->phase1); 78 } 79 80 81 static void eap_tls_params_from_conf2(struct tls_connection_params *params, 82 struct eap_peer_config *config) 83 { 84 params->ca_cert = (char *) config->ca_cert2; 85 params->ca_path = (char *) config->ca_path2; 86 params->client_cert = (char *) config->client_cert2; 87 params->private_key = (char *) config->private_key2; 88 params->private_key_passwd = (char *) config->private_key2_passwd; 89 params->dh_file = (char *) config->dh_file2; 90 params->subject_match = (char *) config->subject_match2; 91 params->altsubject_match = (char *) config->altsubject_match2; 92 params->engine = config->engine2; 93 params->engine_id = config->engine2_id; 94 params->pin = config->pin2; 95 params->key_id = config->key2_id; 96 params->cert_id = config->cert2_id; 97 params->ca_cert_id = config->ca_cert2_id; 98 eap_tls_params_flags(params, config->phase2); 99 } 100 101 102 static int eap_tls_params_from_conf(struct eap_sm *sm, 103 struct eap_ssl_data *data, 104 struct tls_connection_params *params, 105 struct eap_peer_config *config, int phase2) 106 { 107 os_memset(params, 0, sizeof(*params)); 108 if (phase2) { 109 wpa_printf(MSG_DEBUG, "TLS: using phase2 config options"); 110 eap_tls_params_from_conf2(params, config); 111 } else { 112 wpa_printf(MSG_DEBUG, "TLS: using phase1 config options"); 113 eap_tls_params_from_conf1(params, config); 114 } 115 params->tls_ia = data->tls_ia; 116 117 /* 118 * Use blob data, if available. Otherwise, leave reference to external 119 * file as-is. 120 */ 121 if (eap_tls_check_blob(sm, ¶ms->ca_cert, ¶ms->ca_cert_blob, 122 ¶ms->ca_cert_blob_len) || 123 eap_tls_check_blob(sm, ¶ms->client_cert, 124 ¶ms->client_cert_blob, 125 ¶ms->client_cert_blob_len) || 126 eap_tls_check_blob(sm, ¶ms->private_key, 127 ¶ms->private_key_blob, 128 ¶ms->private_key_blob_len) || 129 eap_tls_check_blob(sm, ¶ms->dh_file, ¶ms->dh_blob, 130 ¶ms->dh_blob_len)) { 131 wpa_printf(MSG_INFO, "SSL: Failed to get configuration blobs"); 132 return -1; 133 } 134 135 return 0; 136 } 137 138 139 static int eap_tls_init_connection(struct eap_sm *sm, 140 struct eap_ssl_data *data, 141 struct eap_peer_config *config, 142 struct tls_connection_params *params) 143 { 144 int res; 145 146 data->conn = tls_connection_init(sm->ssl_ctx); 147 if (data->conn == NULL) { 148 wpa_printf(MSG_INFO, "SSL: Failed to initialize new TLS " 149 "connection"); 150 return -1; 151 } 152 153 res = tls_connection_set_params(sm->ssl_ctx, data->conn, params); 154 if (res == TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED) { 155 /* 156 * At this point with the pkcs11 engine the PIN might be wrong. 157 * We reset the PIN in the configuration to be sure to not use 158 * it again and the calling function must request a new one. 159 */ 160 os_free(config->pin); 161 config->pin = NULL; 162 } else if (res == TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED) { 163 wpa_printf(MSG_INFO, "TLS: Failed to load private key"); 164 /* 165 * We do not know exactly but maybe the PIN was wrong, 166 * so ask for a new one. 167 */ 168 os_free(config->pin); 169 config->pin = NULL; 170 eap_sm_request_pin(sm); 171 sm->ignore = TRUE; 172 tls_connection_deinit(sm->ssl_ctx, data->conn); 173 data->conn = NULL; 174 return -1; 175 } else if (res) { 176 wpa_printf(MSG_INFO, "TLS: Failed to set TLS connection " 177 "parameters"); 178 tls_connection_deinit(sm->ssl_ctx, data->conn); 179 data->conn = NULL; 180 return -1; 181 } 182 183 return 0; 184 } 185 186 187 /** 188 * eap_peer_tls_ssl_init - Initialize shared TLS functionality 189 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 190 * @data: Data for TLS processing 191 * @config: Pointer to the network configuration 192 * Returns: 0 on success, -1 on failure 193 * 194 * This function is used to initialize shared TLS functionality for EAP-TLS, 195 * EAP-PEAP, EAP-TTLS, and EAP-FAST. 196 */ 197 int eap_peer_tls_ssl_init(struct eap_sm *sm, struct eap_ssl_data *data, 198 struct eap_peer_config *config) 199 { 200 struct tls_connection_params params; 201 202 if (config == NULL) 203 return -1; 204 205 data->eap = sm; 206 data->phase2 = sm->init_phase2; 207 if (eap_tls_params_from_conf(sm, data, ¶ms, config, data->phase2) < 208 0) 209 return -1; 210 211 if (eap_tls_init_connection(sm, data, config, ¶ms) < 0) 212 return -1; 213 214 data->tls_out_limit = config->fragment_size; 215 if (data->phase2) { 216 /* Limit the fragment size in the inner TLS authentication 217 * since the outer authentication with EAP-PEAP does not yet 218 * support fragmentation */ 219 if (data->tls_out_limit > 100) 220 data->tls_out_limit -= 100; 221 } 222 223 if (config->phase1 && 224 os_strstr(config->phase1, "include_tls_length=1")) { 225 wpa_printf(MSG_DEBUG, "TLS: Include TLS Message Length in " 226 "unfragmented packets"); 227 data->include_tls_length = 1; 228 } 229 230 return 0; 231 } 232 233 234 /** 235 * eap_peer_tls_ssl_deinit - Deinitialize shared TLS functionality 236 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 237 * @data: Data for TLS processing 238 * 239 * This function deinitializes shared TLS functionality that was initialized 240 * with eap_peer_tls_ssl_init(). 241 */ 242 void eap_peer_tls_ssl_deinit(struct eap_sm *sm, struct eap_ssl_data *data) 243 { 244 tls_connection_deinit(sm->ssl_ctx, data->conn); 245 eap_peer_tls_reset_input(data); 246 eap_peer_tls_reset_output(data); 247 } 248 249 250 /** 251 * eap_peer_tls_derive_key - Derive a key based on TLS session data 252 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 253 * @data: Data for TLS processing 254 * @label: Label string for deriving the keys, e.g., "client EAP encryption" 255 * @len: Length of the key material to generate (usually 64 for MSK) 256 * Returns: Pointer to allocated key on success or %NULL on failure 257 * 258 * This function uses TLS-PRF to generate pseudo-random data based on the TLS 259 * session data (client/server random and master key). Each key type may use a 260 * different label to bind the key usage into the generated material. 261 * 262 * The caller is responsible for freeing the returned buffer. 263 */ 264 u8 * eap_peer_tls_derive_key(struct eap_sm *sm, struct eap_ssl_data *data, 265 const char *label, size_t len) 266 { 267 struct tls_keys keys; 268 u8 *rnd = NULL, *out; 269 270 out = os_malloc(len); 271 if (out == NULL) 272 return NULL; 273 274 /* First, try to use TLS library function for PRF, if available. */ 275 if (tls_connection_prf(sm->ssl_ctx, data->conn, label, 0, out, len) == 276 0) 277 return out; 278 279 /* 280 * TLS library did not support key generation, so get the needed TLS 281 * session parameters and use an internal implementation of TLS PRF to 282 * derive the key. 283 */ 284 if (tls_connection_get_keys(sm->ssl_ctx, data->conn, &keys)) 285 goto fail; 286 287 if (keys.client_random == NULL || keys.server_random == NULL || 288 keys.master_key == NULL) 289 goto fail; 290 291 rnd = os_malloc(keys.client_random_len + keys.server_random_len); 292 if (rnd == NULL) 293 goto fail; 294 os_memcpy(rnd, keys.client_random, keys.client_random_len); 295 os_memcpy(rnd + keys.client_random_len, keys.server_random, 296 keys.server_random_len); 297 298 if (tls_prf(keys.master_key, keys.master_key_len, 299 label, rnd, keys.client_random_len + 300 keys.server_random_len, out, len)) 301 goto fail; 302 303 os_free(rnd); 304 return out; 305 306 fail: 307 os_free(out); 308 os_free(rnd); 309 return NULL; 310 } 311 312 313 /** 314 * eap_peer_tls_reassemble_fragment - Reassemble a received fragment 315 * @data: Data for TLS processing 316 * @in_data: Next incoming TLS segment 317 * Returns: 0 on success, 1 if more data is needed for the full message, or 318 * -1 on error 319 */ 320 static int eap_peer_tls_reassemble_fragment(struct eap_ssl_data *data, 321 const struct wpabuf *in_data) 322 { 323 size_t tls_in_len, in_len; 324 325 tls_in_len = data->tls_in ? wpabuf_len(data->tls_in) : 0; 326 in_len = in_data ? wpabuf_len(in_data) : 0; 327 328 if (tls_in_len + in_len == 0) { 329 /* No message data received?! */ 330 wpa_printf(MSG_WARNING, "SSL: Invalid reassembly state: " 331 "tls_in_left=%lu tls_in_len=%lu in_len=%lu", 332 (unsigned long) data->tls_in_left, 333 (unsigned long) tls_in_len, 334 (unsigned long) in_len); 335 eap_peer_tls_reset_input(data); 336 return -1; 337 } 338 339 if (tls_in_len + in_len > 65536) { 340 /* 341 * Limit length to avoid rogue servers from causing large 342 * memory allocations. 343 */ 344 wpa_printf(MSG_INFO, "SSL: Too long TLS fragment (size over " 345 "64 kB)"); 346 eap_peer_tls_reset_input(data); 347 return -1; 348 } 349 350 if (in_len > data->tls_in_left) { 351 /* Sender is doing something odd - reject message */ 352 wpa_printf(MSG_INFO, "SSL: more data than TLS message length " 353 "indicated"); 354 eap_peer_tls_reset_input(data); 355 return -1; 356 } 357 358 if (wpabuf_resize(&data->tls_in, in_len) < 0) { 359 wpa_printf(MSG_INFO, "SSL: Could not allocate memory for TLS " 360 "data"); 361 eap_peer_tls_reset_input(data); 362 return -1; 363 } 364 wpabuf_put_buf(data->tls_in, in_data); 365 data->tls_in_left -= in_len; 366 367 if (data->tls_in_left > 0) { 368 wpa_printf(MSG_DEBUG, "SSL: Need %lu bytes more input " 369 "data", (unsigned long) data->tls_in_left); 370 return 1; 371 } 372 373 return 0; 374 } 375 376 377 /** 378 * eap_peer_tls_data_reassemble - Reassemble TLS data 379 * @data: Data for TLS processing 380 * @in_data: Next incoming TLS segment 381 * @need_more_input: Variable for returning whether more input data is needed 382 * to reassemble this TLS packet 383 * Returns: Pointer to output data, %NULL on error or when more data is needed 384 * for the full message (in which case, *need_more_input is also set to 1). 385 * 386 * This function reassembles TLS fragments. Caller must not free the returned 387 * data buffer since an internal pointer to it is maintained. 388 */ 389 static const struct wpabuf * eap_peer_tls_data_reassemble( 390 struct eap_ssl_data *data, const struct wpabuf *in_data, 391 int *need_more_input) 392 { 393 *need_more_input = 0; 394 395 if (data->tls_in_left > wpabuf_len(in_data) || data->tls_in) { 396 /* Message has fragments */ 397 int res = eap_peer_tls_reassemble_fragment(data, in_data); 398 if (res) { 399 if (res == 1) 400 *need_more_input = 1; 401 return NULL; 402 } 403 404 /* Message is now fully reassembled. */ 405 } else { 406 /* No fragments in this message, so just make a copy of it. */ 407 data->tls_in_left = 0; 408 data->tls_in = wpabuf_dup(in_data); 409 if (data->tls_in == NULL) 410 return NULL; 411 } 412 413 return data->tls_in; 414 } 415 416 417 /** 418 * eap_tls_process_input - Process incoming TLS message 419 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 420 * @data: Data for TLS processing 421 * @in_data: Message received from the server 422 * @in_len: Length of in_data 423 * @out_data: Buffer for returning a pointer to application data (if available) 424 * Returns: 0 on success, 1 if more input data is needed, 2 if application data 425 * is available, -1 on failure 426 */ 427 static int eap_tls_process_input(struct eap_sm *sm, struct eap_ssl_data *data, 428 const u8 *in_data, size_t in_len, 429 struct wpabuf **out_data) 430 { 431 const struct wpabuf *msg; 432 int need_more_input; 433 struct wpabuf *appl_data; 434 struct wpabuf buf; 435 436 wpabuf_set(&buf, in_data, in_len); 437 msg = eap_peer_tls_data_reassemble(data, &buf, &need_more_input); 438 if (msg == NULL) 439 return need_more_input ? 1 : -1; 440 441 /* Full TLS message reassembled - continue handshake processing */ 442 if (data->tls_out) { 443 /* This should not happen.. */ 444 wpa_printf(MSG_INFO, "SSL: eap_tls_process_input - pending " 445 "tls_out data even though tls_out_len = 0"); 446 wpabuf_free(data->tls_out); 447 WPA_ASSERT(data->tls_out == NULL); 448 } 449 appl_data = NULL; 450 data->tls_out = tls_connection_handshake(sm->ssl_ctx, data->conn, 451 msg, &appl_data); 452 453 eap_peer_tls_reset_input(data); 454 455 if (appl_data && 456 tls_connection_established(sm->ssl_ctx, data->conn) && 457 !tls_connection_get_failed(sm->ssl_ctx, data->conn)) { 458 wpa_hexdump_buf_key(MSG_MSGDUMP, "SSL: Application data", 459 appl_data); 460 *out_data = appl_data; 461 return 2; 462 } 463 464 wpabuf_free(appl_data); 465 466 return 0; 467 } 468 469 470 /** 471 * eap_tls_process_output - Process outgoing TLS message 472 * @data: Data for TLS processing 473 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...) 474 * @peap_version: Version number for EAP-PEAP/TTLS 475 * @id: EAP identifier for the response 476 * @ret: Return value to use on success 477 * @out_data: Buffer for returning the allocated output buffer 478 * Returns: ret (0 or 1) on success, -1 on failure 479 */ 480 static int eap_tls_process_output(struct eap_ssl_data *data, EapType eap_type, 481 int peap_version, u8 id, int ret, 482 struct wpabuf **out_data) 483 { 484 size_t len; 485 u8 *flags; 486 int more_fragments, length_included; 487 488 if (data->tls_out == NULL) 489 return -1; 490 len = wpabuf_len(data->tls_out) - data->tls_out_pos; 491 wpa_printf(MSG_DEBUG, "SSL: %lu bytes left to be sent out (of total " 492 "%lu bytes)", 493 (unsigned long) len, 494 (unsigned long) wpabuf_len(data->tls_out)); 495 496 /* 497 * Limit outgoing message to the configured maximum size. Fragment 498 * message if needed. 499 */ 500 if (len > data->tls_out_limit) { 501 more_fragments = 1; 502 len = data->tls_out_limit; 503 wpa_printf(MSG_DEBUG, "SSL: sending %lu bytes, more fragments " 504 "will follow", (unsigned long) len); 505 } else 506 more_fragments = 0; 507 508 length_included = data->tls_out_pos == 0 && 509 (wpabuf_len(data->tls_out) > data->tls_out_limit || 510 data->include_tls_length); 511 if (!length_included && 512 eap_type == EAP_TYPE_PEAP && peap_version == 0 && 513 !tls_connection_established(data->eap->ssl_ctx, data->conn)) { 514 /* 515 * Windows Server 2008 NPS really wants to have the TLS Message 516 * length included in phase 0 even for unfragmented frames or 517 * it will get very confused with Compound MAC calculation and 518 * Outer TLVs. 519 */ 520 length_included = 1; 521 } 522 523 *out_data = eap_msg_alloc(EAP_VENDOR_IETF, eap_type, 524 1 + length_included * 4 + len, 525 EAP_CODE_RESPONSE, id); 526 if (*out_data == NULL) 527 return -1; 528 529 flags = wpabuf_put(*out_data, 1); 530 *flags = peap_version; 531 if (more_fragments) 532 *flags |= EAP_TLS_FLAGS_MORE_FRAGMENTS; 533 if (length_included) { 534 *flags |= EAP_TLS_FLAGS_LENGTH_INCLUDED; 535 wpabuf_put_be32(*out_data, wpabuf_len(data->tls_out)); 536 } 537 538 wpabuf_put_data(*out_data, 539 wpabuf_head_u8(data->tls_out) + data->tls_out_pos, 540 len); 541 data->tls_out_pos += len; 542 543 if (!more_fragments) 544 eap_peer_tls_reset_output(data); 545 546 return ret; 547 } 548 549 550 /** 551 * eap_peer_tls_process_helper - Process TLS handshake message 552 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 553 * @data: Data for TLS processing 554 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...) 555 * @peap_version: Version number for EAP-PEAP/TTLS 556 * @id: EAP identifier for the response 557 * @in_data: Message received from the server 558 * @in_len: Length of in_data 559 * @out_data: Buffer for returning a pointer to the response message 560 * Returns: 0 on success, 1 if more input data is needed, 2 if application data 561 * is available, or -1 on failure 562 * 563 * This function can be used to process TLS handshake messages. It reassembles 564 * the received fragments and uses a TLS library to process the messages. The 565 * response data from the TLS library is fragmented to suitable output messages 566 * that the caller can send out. 567 * 568 * out_data is used to return the response message if the return value of this 569 * function is 0, 2, or -1. In case of failure, the message is likely a TLS 570 * alarm message. The caller is responsible for freeing the allocated buffer if 571 * *out_data is not %NULL. 572 * 573 * This function is called for each received TLS message during the TLS 574 * handshake after eap_peer_tls_process_init() call and possible processing of 575 * TLS Flags field. Once the handshake has been completed, i.e., when 576 * tls_connection_established() returns 1, EAP method specific decrypting of 577 * the tunneled data is used. 578 */ 579 int eap_peer_tls_process_helper(struct eap_sm *sm, struct eap_ssl_data *data, 580 EapType eap_type, int peap_version, 581 u8 id, const u8 *in_data, size_t in_len, 582 struct wpabuf **out_data) 583 { 584 int ret = 0; 585 586 *out_data = NULL; 587 588 if (data->tls_out && wpabuf_len(data->tls_out) > 0 && in_len > 0) { 589 wpa_printf(MSG_DEBUG, "SSL: Received non-ACK when output " 590 "fragments are waiting to be sent out"); 591 return -1; 592 } 593 594 if (data->tls_out == NULL || wpabuf_len(data->tls_out) == 0) { 595 /* 596 * No more data to send out - expect to receive more data from 597 * the AS. 598 */ 599 int res = eap_tls_process_input(sm, data, in_data, in_len, 600 out_data); 601 if (res) { 602 /* 603 * Input processing failed (res = -1) or more data is 604 * needed (res = 1). 605 */ 606 return res; 607 } 608 609 /* 610 * The incoming message has been reassembled and processed. The 611 * response was allocated into data->tls_out buffer. 612 */ 613 } 614 615 if (data->tls_out == NULL) { 616 /* 617 * No outgoing fragments remaining from the previous message 618 * and no new message generated. This indicates an error in TLS 619 * processing. 620 */ 621 eap_peer_tls_reset_output(data); 622 return -1; 623 } 624 625 if (tls_connection_get_failed(sm->ssl_ctx, data->conn)) { 626 /* TLS processing has failed - return error */ 627 wpa_printf(MSG_DEBUG, "SSL: Failed - tls_out available to " 628 "report error"); 629 ret = -1; 630 /* TODO: clean pin if engine used? */ 631 } 632 633 if (data->tls_out == NULL || wpabuf_len(data->tls_out) == 0) { 634 /* 635 * TLS negotiation should now be complete since all other cases 636 * needing more data should have been caught above based on 637 * the TLS Message Length field. 638 */ 639 wpa_printf(MSG_DEBUG, "SSL: No data to be sent out"); 640 wpabuf_free(data->tls_out); 641 data->tls_out = NULL; 642 return 1; 643 } 644 645 /* Send the pending message (in fragments, if needed). */ 646 return eap_tls_process_output(data, eap_type, peap_version, id, ret, 647 out_data); 648 } 649 650 651 /** 652 * eap_peer_tls_build_ack - Build a TLS ACK frame 653 * @id: EAP identifier for the response 654 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...) 655 * @peap_version: Version number for EAP-PEAP/TTLS 656 * Returns: Pointer to the allocated ACK frame or %NULL on failure 657 */ 658 struct wpabuf * eap_peer_tls_build_ack(u8 id, EapType eap_type, 659 int peap_version) 660 { 661 struct wpabuf *resp; 662 663 resp = eap_msg_alloc(EAP_VENDOR_IETF, eap_type, 1, EAP_CODE_RESPONSE, 664 id); 665 if (resp == NULL) 666 return NULL; 667 wpa_printf(MSG_DEBUG, "SSL: Building ACK (type=%d id=%d ver=%d)", 668 (int) eap_type, id, peap_version); 669 wpabuf_put_u8(resp, peap_version); /* Flags */ 670 return resp; 671 } 672 673 674 /** 675 * eap_peer_tls_reauth_init - Re-initialize shared TLS for session resumption 676 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 677 * @data: Data for TLS processing 678 * Returns: 0 on success, -1 on failure 679 */ 680 int eap_peer_tls_reauth_init(struct eap_sm *sm, struct eap_ssl_data *data) 681 { 682 eap_peer_tls_reset_input(data); 683 eap_peer_tls_reset_output(data); 684 return tls_connection_shutdown(sm->ssl_ctx, data->conn); 685 } 686 687 688 /** 689 * eap_peer_tls_status - Get TLS status 690 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 691 * @data: Data for TLS processing 692 * @buf: Buffer for status information 693 * @buflen: Maximum buffer length 694 * @verbose: Whether to include verbose status information 695 * Returns: Number of bytes written to buf. 696 */ 697 int eap_peer_tls_status(struct eap_sm *sm, struct eap_ssl_data *data, 698 char *buf, size_t buflen, int verbose) 699 { 700 char name[128]; 701 int len = 0, ret; 702 703 if (tls_get_cipher(sm->ssl_ctx, data->conn, name, sizeof(name)) == 0) { 704 ret = os_snprintf(buf + len, buflen - len, 705 "EAP TLS cipher=%s\n", name); 706 if (ret < 0 || (size_t) ret >= buflen - len) 707 return len; 708 len += ret; 709 } 710 711 return len; 712 } 713 714 715 /** 716 * eap_peer_tls_process_init - Initial validation/processing of EAP requests 717 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 718 * @data: Data for TLS processing 719 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...) 720 * @ret: Return values from EAP request validation and processing 721 * @reqData: EAP request to be processed (eapReqData) 722 * @len: Buffer for returning length of the remaining payload 723 * @flags: Buffer for returning TLS flags 724 * Returns: Pointer to payload after TLS flags and length or %NULL on failure 725 * 726 * This function validates the EAP header and processes the optional TLS 727 * Message Length field. If this is the first fragment of a TLS message, the 728 * TLS reassembly code is initialized to receive the indicated number of bytes. 729 * 730 * EAP-TLS, EAP-PEAP, EAP-TTLS, and EAP-FAST methods are expected to use this 731 * function as the first step in processing received messages. They will need 732 * to process the flags (apart from Message Length Included) that are returned 733 * through the flags pointer and the message payload that will be returned (and 734 * the length is returned through the len pointer). Return values (ret) are set 735 * for continuation of EAP method processing. The caller is responsible for 736 * setting these to indicate completion (either success or failure) based on 737 * the authentication result. 738 */ 739 const u8 * eap_peer_tls_process_init(struct eap_sm *sm, 740 struct eap_ssl_data *data, 741 EapType eap_type, 742 struct eap_method_ret *ret, 743 const struct wpabuf *reqData, 744 size_t *len, u8 *flags) 745 { 746 const u8 *pos; 747 size_t left; 748 unsigned int tls_msg_len; 749 750 if (tls_get_errors(sm->ssl_ctx)) { 751 wpa_printf(MSG_INFO, "SSL: TLS errors detected"); 752 ret->ignore = TRUE; 753 return NULL; 754 } 755 756 pos = eap_hdr_validate(EAP_VENDOR_IETF, eap_type, reqData, &left); 757 if (pos == NULL) { 758 ret->ignore = TRUE; 759 return NULL; 760 } 761 if (left == 0) { 762 wpa_printf(MSG_DEBUG, "SSL: Invalid TLS message: no Flags " 763 "octet included"); 764 if (!sm->workaround) { 765 ret->ignore = TRUE; 766 return NULL; 767 } 768 769 wpa_printf(MSG_DEBUG, "SSL: Workaround - assume no Flags " 770 "indicates ACK frame"); 771 *flags = 0; 772 } else { 773 *flags = *pos++; 774 left--; 775 } 776 wpa_printf(MSG_DEBUG, "SSL: Received packet(len=%lu) - " 777 "Flags 0x%02x", (unsigned long) wpabuf_len(reqData), 778 *flags); 779 if (*flags & EAP_TLS_FLAGS_LENGTH_INCLUDED) { 780 if (left < 4) { 781 wpa_printf(MSG_INFO, "SSL: Short frame with TLS " 782 "length"); 783 ret->ignore = TRUE; 784 return NULL; 785 } 786 tls_msg_len = WPA_GET_BE32(pos); 787 wpa_printf(MSG_DEBUG, "SSL: TLS Message Length: %d", 788 tls_msg_len); 789 if (data->tls_in_left == 0) { 790 data->tls_in_total = tls_msg_len; 791 data->tls_in_left = tls_msg_len; 792 wpabuf_free(data->tls_in); 793 data->tls_in = NULL; 794 } 795 pos += 4; 796 left -= 4; 797 } 798 799 ret->ignore = FALSE; 800 ret->methodState = METHOD_MAY_CONT; 801 ret->decision = DECISION_FAIL; 802 ret->allowNotifications = TRUE; 803 804 *len = left; 805 return pos; 806 } 807 808 809 /** 810 * eap_peer_tls_reset_input - Reset input buffers 811 * @data: Data for TLS processing 812 * 813 * This function frees any allocated memory for input buffers and resets input 814 * state. 815 */ 816 void eap_peer_tls_reset_input(struct eap_ssl_data *data) 817 { 818 data->tls_in_left = data->tls_in_total = 0; 819 wpabuf_free(data->tls_in); 820 data->tls_in = NULL; 821 } 822 823 824 /** 825 * eap_peer_tls_reset_output - Reset output buffers 826 * @data: Data for TLS processing 827 * 828 * This function frees any allocated memory for output buffers and resets 829 * output state. 830 */ 831 void eap_peer_tls_reset_output(struct eap_ssl_data *data) 832 { 833 data->tls_out_pos = 0; 834 wpabuf_free(data->tls_out); 835 data->tls_out = NULL; 836 } 837 838 839 /** 840 * eap_peer_tls_decrypt - Decrypt received phase 2 TLS message 841 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 842 * @data: Data for TLS processing 843 * @in_data: Message received from the server 844 * @in_decrypted: Buffer for returning a pointer to the decrypted message 845 * Returns: 0 on success, 1 if more input data is needed, or -1 on failure 846 */ 847 int eap_peer_tls_decrypt(struct eap_sm *sm, struct eap_ssl_data *data, 848 const struct wpabuf *in_data, 849 struct wpabuf **in_decrypted) 850 { 851 const struct wpabuf *msg; 852 int need_more_input; 853 854 msg = eap_peer_tls_data_reassemble(data, in_data, &need_more_input); 855 if (msg == NULL) 856 return need_more_input ? 1 : -1; 857 858 *in_decrypted = tls_connection_decrypt(sm->ssl_ctx, data->conn, msg); 859 eap_peer_tls_reset_input(data); 860 if (*in_decrypted == NULL) { 861 wpa_printf(MSG_INFO, "SSL: Failed to decrypt Phase 2 data"); 862 return -1; 863 } 864 return 0; 865 } 866 867 868 /** 869 * eap_peer_tls_encrypt - Encrypt phase 2 TLS message 870 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 871 * @data: Data for TLS processing 872 * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...) 873 * @peap_version: Version number for EAP-PEAP/TTLS 874 * @id: EAP identifier for the response 875 * @in_data: Plaintext phase 2 data to encrypt or %NULL to continue fragments 876 * @out_data: Buffer for returning a pointer to the encrypted response message 877 * Returns: 0 on success, -1 on failure 878 */ 879 int eap_peer_tls_encrypt(struct eap_sm *sm, struct eap_ssl_data *data, 880 EapType eap_type, int peap_version, u8 id, 881 const struct wpabuf *in_data, 882 struct wpabuf **out_data) 883 { 884 if (in_data) { 885 eap_peer_tls_reset_output(data); 886 data->tls_out = tls_connection_encrypt(sm->ssl_ctx, data->conn, 887 in_data); 888 if (data->tls_out == NULL) { 889 wpa_printf(MSG_INFO, "SSL: Failed to encrypt Phase 2 " 890 "data (in_len=%lu)", 891 (unsigned long) wpabuf_len(in_data)); 892 eap_peer_tls_reset_output(data); 893 return -1; 894 } 895 } 896 897 return eap_tls_process_output(data, eap_type, peap_version, id, 0, 898 out_data); 899 } 900 901 902 /** 903 * eap_peer_select_phase2_methods - Select phase 2 EAP method 904 * @config: Pointer to the network configuration 905 * @prefix: 'phase2' configuration prefix, e.g., "auth=" 906 * @types: Buffer for returning allocated list of allowed EAP methods 907 * @num_types: Buffer for returning number of allocated EAP methods 908 * Returns: 0 on success, -1 on failure 909 * 910 * This function is used to parse EAP method list and select allowed methods 911 * for Phase2 authentication. 912 */ 913 int eap_peer_select_phase2_methods(struct eap_peer_config *config, 914 const char *prefix, 915 struct eap_method_type **types, 916 size_t *num_types) 917 { 918 char *start, *pos, *buf; 919 struct eap_method_type *methods = NULL, *_methods; 920 u8 method; 921 size_t num_methods = 0, prefix_len; 922 923 if (config == NULL || config->phase2 == NULL) 924 goto get_defaults; 925 926 start = buf = os_strdup(config->phase2); 927 if (buf == NULL) 928 return -1; 929 930 prefix_len = os_strlen(prefix); 931 932 while (start && *start != '\0') { 933 int vendor; 934 pos = os_strstr(start, prefix); 935 if (pos == NULL) 936 break; 937 if (start != pos && *(pos - 1) != ' ') { 938 start = pos + prefix_len; 939 continue; 940 } 941 942 start = pos + prefix_len; 943 pos = os_strchr(start, ' '); 944 if (pos) 945 *pos++ = '\0'; 946 method = eap_get_phase2_type(start, &vendor); 947 if (vendor == EAP_VENDOR_IETF && method == EAP_TYPE_NONE) { 948 wpa_printf(MSG_ERROR, "TLS: Unsupported Phase2 EAP " 949 "method '%s'", start); 950 } else { 951 num_methods++; 952 _methods = os_realloc(methods, 953 num_methods * sizeof(*methods)); 954 if (_methods == NULL) { 955 os_free(methods); 956 os_free(buf); 957 return -1; 958 } 959 methods = _methods; 960 methods[num_methods - 1].vendor = vendor; 961 methods[num_methods - 1].method = method; 962 } 963 964 start = pos; 965 } 966 967 os_free(buf); 968 969 get_defaults: 970 if (methods == NULL) 971 methods = eap_get_phase2_types(config, &num_methods); 972 973 if (methods == NULL) { 974 wpa_printf(MSG_ERROR, "TLS: No Phase2 EAP methods available"); 975 return -1; 976 } 977 wpa_hexdump(MSG_DEBUG, "TLS: Phase2 EAP types", 978 (u8 *) methods, 979 num_methods * sizeof(struct eap_method_type)); 980 981 *types = methods; 982 *num_types = num_methods; 983 984 return 0; 985 } 986 987 988 /** 989 * eap_peer_tls_phase2_nak - Generate EAP-Nak for Phase 2 990 * @types: Buffer for returning allocated list of allowed EAP methods 991 * @num_types: Buffer for returning number of allocated EAP methods 992 * @hdr: EAP-Request header (and the following EAP type octet) 993 * @resp: Buffer for returning the EAP-Nak message 994 * Returns: 0 on success, -1 on failure 995 */ 996 int eap_peer_tls_phase2_nak(struct eap_method_type *types, size_t num_types, 997 struct eap_hdr *hdr, struct wpabuf **resp) 998 { 999 u8 *pos = (u8 *) (hdr + 1); 1000 size_t i; 1001 1002 /* TODO: add support for expanded Nak */ 1003 wpa_printf(MSG_DEBUG, "TLS: Phase 2 Request: Nak type=%d", *pos); 1004 wpa_hexdump(MSG_DEBUG, "TLS: Allowed Phase2 EAP types", 1005 (u8 *) types, num_types * sizeof(struct eap_method_type)); 1006 *resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_NAK, num_types, 1007 EAP_CODE_RESPONSE, hdr->identifier); 1008 if (*resp == NULL) 1009 return -1; 1010 1011 for (i = 0; i < num_types; i++) { 1012 if (types[i].vendor == EAP_VENDOR_IETF && 1013 types[i].method < 256) 1014 wpabuf_put_u8(*resp, types[i].method); 1015 } 1016 1017 eap_update_len(*resp); 1018 1019 return 0; 1020 } 1021