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