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