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