Home | History | Annotate | Download | only in eap_peer
      1 /*
      2  * EAP peer method: EAP-FAST (RFC 4851)
      3  * Copyright (c) 2004-2008, Jouni Malinen <j (at) 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/tls.h"
     13 #include "crypto/sha1.h"
     14 #include "eap_common/eap_tlv_common.h"
     15 #include "eap_i.h"
     16 #include "eap_tls_common.h"
     17 #include "eap_config.h"
     18 #include "eap_fast_pac.h"
     19 
     20 #ifdef EAP_FAST_DYNAMIC
     21 #include "eap_fast_pac.c"
     22 #endif /* EAP_FAST_DYNAMIC */
     23 
     24 /* TODO:
     25  * - test session resumption and enable it if it interoperates
     26  * - password change (pending mschapv2 packet; replay decrypted packet)
     27  */
     28 
     29 
     30 static void eap_fast_deinit(struct eap_sm *sm, void *priv);
     31 
     32 
     33 struct eap_fast_data {
     34 	struct eap_ssl_data ssl;
     35 
     36 	int fast_version;
     37 
     38 	const struct eap_method *phase2_method;
     39 	void *phase2_priv;
     40 	int phase2_success;
     41 
     42 	struct eap_method_type phase2_type;
     43 	struct eap_method_type *phase2_types;
     44 	size_t num_phase2_types;
     45 	int resuming; /* starting a resumed session */
     46 	struct eap_fast_key_block_provisioning *key_block_p;
     47 #define EAP_FAST_PROV_UNAUTH 1
     48 #define EAP_FAST_PROV_AUTH 2
     49 	int provisioning_allowed; /* Allowed PAC provisioning modes */
     50 	int provisioning; /* doing PAC provisioning (not the normal auth) */
     51 	int anon_provisioning; /* doing anonymous (unauthenticated)
     52 				* provisioning */
     53 	int session_ticket_used;
     54 
     55 	u8 key_data[EAP_FAST_KEY_LEN];
     56 	u8 *session_id;
     57 	size_t id_len;
     58 	u8 emsk[EAP_EMSK_LEN];
     59 	int success;
     60 
     61 	struct eap_fast_pac *pac;
     62 	struct eap_fast_pac *current_pac;
     63 	size_t max_pac_list_len;
     64 	int use_pac_binary_format;
     65 
     66 	u8 simck[EAP_FAST_SIMCK_LEN];
     67 	int simck_idx;
     68 
     69 	struct wpabuf *pending_phase2_req;
     70 };
     71 
     72 
     73 static int eap_fast_session_ticket_cb(void *ctx, const u8 *ticket, size_t len,
     74 				      const u8 *client_random,
     75 				      const u8 *server_random,
     76 				      u8 *master_secret)
     77 {
     78 	struct eap_fast_data *data = ctx;
     79 
     80 	wpa_printf(MSG_DEBUG, "EAP-FAST: SessionTicket callback");
     81 
     82 	if (client_random == NULL || server_random == NULL ||
     83 	    master_secret == NULL) {
     84 		wpa_printf(MSG_DEBUG, "EAP-FAST: SessionTicket failed - fall "
     85 			   "back to full TLS handshake");
     86 		data->session_ticket_used = 0;
     87 		if (data->provisioning_allowed) {
     88 			wpa_printf(MSG_DEBUG, "EAP-FAST: Try to provision a "
     89 				   "new PAC-Key");
     90 			data->provisioning = 1;
     91 			data->current_pac = NULL;
     92 		}
     93 		return 0;
     94 	}
     95 
     96 	wpa_hexdump(MSG_DEBUG, "EAP-FAST: SessionTicket", ticket, len);
     97 
     98 	if (data->current_pac == NULL) {
     99 		wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC-Key available for "
    100 			   "using SessionTicket");
    101 		data->session_ticket_used = 0;
    102 		return 0;
    103 	}
    104 
    105 	eap_fast_derive_master_secret(data->current_pac->pac_key,
    106 				      server_random, client_random,
    107 				      master_secret);
    108 
    109 	data->session_ticket_used = 1;
    110 
    111 	return 1;
    112 }
    113 
    114 
    115 static int eap_fast_parse_phase1(struct eap_fast_data *data,
    116 				 const char *phase1)
    117 {
    118 	const char *pos;
    119 
    120 	pos = os_strstr(phase1, "fast_provisioning=");
    121 	if (pos) {
    122 		data->provisioning_allowed = atoi(pos + 18);
    123 		wpa_printf(MSG_DEBUG, "EAP-FAST: Automatic PAC provisioning "
    124 			   "mode: %d", data->provisioning_allowed);
    125 	}
    126 
    127 	pos = os_strstr(phase1, "fast_max_pac_list_len=");
    128 	if (pos) {
    129 		data->max_pac_list_len = atoi(pos + 22);
    130 		if (data->max_pac_list_len == 0)
    131 			data->max_pac_list_len = 1;
    132 		wpa_printf(MSG_DEBUG, "EAP-FAST: Maximum PAC list length: %lu",
    133 			   (unsigned long) data->max_pac_list_len);
    134 	}
    135 
    136 	pos = os_strstr(phase1, "fast_pac_format=binary");
    137 	if (pos) {
    138 		data->use_pac_binary_format = 1;
    139 		wpa_printf(MSG_DEBUG, "EAP-FAST: Using binary format for PAC "
    140 			   "list");
    141 	}
    142 
    143 	return 0;
    144 }
    145 
    146 
    147 static void * eap_fast_init(struct eap_sm *sm)
    148 {
    149 	struct eap_fast_data *data;
    150 	struct eap_peer_config *config = eap_get_config(sm);
    151 
    152 	if (config == NULL)
    153 		return NULL;
    154 
    155 	data = os_zalloc(sizeof(*data));
    156 	if (data == NULL)
    157 		return NULL;
    158 	data->fast_version = EAP_FAST_VERSION;
    159 	data->max_pac_list_len = 10;
    160 
    161 	if (config->phase1 && eap_fast_parse_phase1(data, config->phase1) < 0) {
    162 		eap_fast_deinit(sm, data);
    163 		return NULL;
    164 	}
    165 
    166 	if (eap_peer_select_phase2_methods(config, "auth=",
    167 					   &data->phase2_types,
    168 					   &data->num_phase2_types) < 0) {
    169 		eap_fast_deinit(sm, data);
    170 		return NULL;
    171 	}
    172 
    173 	data->phase2_type.vendor = EAP_VENDOR_IETF;
    174 	data->phase2_type.method = EAP_TYPE_NONE;
    175 
    176 	if (eap_peer_tls_ssl_init(sm, &data->ssl, config, EAP_TYPE_FAST)) {
    177 		wpa_printf(MSG_INFO, "EAP-FAST: Failed to initialize SSL.");
    178 		eap_fast_deinit(sm, data);
    179 		return NULL;
    180 	}
    181 
    182 	if (tls_connection_set_session_ticket_cb(sm->ssl_ctx, data->ssl.conn,
    183 						 eap_fast_session_ticket_cb,
    184 						 data) < 0) {
    185 		wpa_printf(MSG_INFO, "EAP-FAST: Failed to set SessionTicket "
    186 			   "callback");
    187 		eap_fast_deinit(sm, data);
    188 		return NULL;
    189 	}
    190 
    191 	/*
    192 	 * The local RADIUS server in a Cisco AP does not seem to like empty
    193 	 * fragments before data, so disable that workaround for CBC.
    194 	 * TODO: consider making this configurable
    195 	 */
    196 	if (tls_connection_enable_workaround(sm->ssl_ctx, data->ssl.conn)) {
    197 		wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to enable TLS "
    198 			   "workarounds");
    199 	}
    200 
    201 	if (!config->pac_file) {
    202 		wpa_printf(MSG_INFO, "EAP-FAST: No PAC file configured");
    203 		eap_fast_deinit(sm, data);
    204 		return NULL;
    205 	}
    206 
    207 	if (data->use_pac_binary_format &&
    208 	    eap_fast_load_pac_bin(sm, &data->pac, config->pac_file) < 0) {
    209 		wpa_printf(MSG_INFO, "EAP-FAST: Failed to load PAC file");
    210 		eap_fast_deinit(sm, data);
    211 		return NULL;
    212 	}
    213 
    214 	if (!data->use_pac_binary_format &&
    215 	    eap_fast_load_pac(sm, &data->pac, config->pac_file) < 0) {
    216 		wpa_printf(MSG_INFO, "EAP-FAST: Failed to load PAC file");
    217 		eap_fast_deinit(sm, data);
    218 		return NULL;
    219 	}
    220 	eap_fast_pac_list_truncate(data->pac, data->max_pac_list_len);
    221 
    222 	if (data->pac == NULL && !data->provisioning_allowed) {
    223 		wpa_printf(MSG_INFO, "EAP-FAST: No PAC configured and "
    224 			   "provisioning disabled");
    225 		eap_fast_deinit(sm, data);
    226 		return NULL;
    227 	}
    228 
    229 	return data;
    230 }
    231 
    232 
    233 static void eap_fast_deinit(struct eap_sm *sm, void *priv)
    234 {
    235 	struct eap_fast_data *data = priv;
    236 	struct eap_fast_pac *pac, *prev;
    237 
    238 	if (data == NULL)
    239 		return;
    240 	if (data->phase2_priv && data->phase2_method)
    241 		data->phase2_method->deinit(sm, data->phase2_priv);
    242 	os_free(data->phase2_types);
    243 	os_free(data->key_block_p);
    244 	eap_peer_tls_ssl_deinit(sm, &data->ssl);
    245 
    246 	pac = data->pac;
    247 	prev = NULL;
    248 	while (pac) {
    249 		prev = pac;
    250 		pac = pac->next;
    251 		eap_fast_free_pac(prev);
    252 	}
    253 	os_memset(data->key_data, 0, EAP_FAST_KEY_LEN);
    254 	os_memset(data->emsk, 0, EAP_EMSK_LEN);
    255 	os_free(data->session_id);
    256 	wpabuf_free(data->pending_phase2_req);
    257 	os_free(data);
    258 }
    259 
    260 
    261 static int eap_fast_derive_msk(struct eap_fast_data *data)
    262 {
    263 	eap_fast_derive_eap_msk(data->simck, data->key_data);
    264 	eap_fast_derive_eap_emsk(data->simck, data->emsk);
    265 	data->success = 1;
    266 	return 0;
    267 }
    268 
    269 
    270 static void eap_fast_derive_key_auth(struct eap_sm *sm,
    271 				     struct eap_fast_data *data)
    272 {
    273 	u8 *sks;
    274 
    275 	/* RFC 4851, Section 5.1:
    276 	 * Extra key material after TLS key_block: session_key_seed[40]
    277 	 */
    278 
    279 	sks = eap_fast_derive_key(sm->ssl_ctx, data->ssl.conn, "key expansion",
    280 				  EAP_FAST_SKS_LEN);
    281 	if (sks == NULL) {
    282 		wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive "
    283 			   "session_key_seed");
    284 		return;
    285 	}
    286 
    287 	/*
    288 	 * RFC 4851, Section 5.2:
    289 	 * S-IMCK[0] = session_key_seed
    290 	 */
    291 	wpa_hexdump_key(MSG_DEBUG,
    292 			"EAP-FAST: session_key_seed (SKS = S-IMCK[0])",
    293 			sks, EAP_FAST_SKS_LEN);
    294 	data->simck_idx = 0;
    295 	os_memcpy(data->simck, sks, EAP_FAST_SIMCK_LEN);
    296 	os_free(sks);
    297 }
    298 
    299 
    300 static void eap_fast_derive_key_provisioning(struct eap_sm *sm,
    301 					     struct eap_fast_data *data)
    302 {
    303 	os_free(data->key_block_p);
    304 	data->key_block_p = (struct eap_fast_key_block_provisioning *)
    305 		eap_fast_derive_key(sm->ssl_ctx, data->ssl.conn,
    306 				    "key expansion",
    307 				    sizeof(*data->key_block_p));
    308 	if (data->key_block_p == NULL) {
    309 		wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive key block");
    310 		return;
    311 	}
    312 	/*
    313 	 * RFC 4851, Section 5.2:
    314 	 * S-IMCK[0] = session_key_seed
    315 	 */
    316 	wpa_hexdump_key(MSG_DEBUG,
    317 			"EAP-FAST: session_key_seed (SKS = S-IMCK[0])",
    318 			data->key_block_p->session_key_seed,
    319 			sizeof(data->key_block_p->session_key_seed));
    320 	data->simck_idx = 0;
    321 	os_memcpy(data->simck, data->key_block_p->session_key_seed,
    322 		  EAP_FAST_SIMCK_LEN);
    323 	wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: server_challenge",
    324 			data->key_block_p->server_challenge,
    325 			sizeof(data->key_block_p->server_challenge));
    326 	wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: client_challenge",
    327 			data->key_block_p->client_challenge,
    328 			sizeof(data->key_block_p->client_challenge));
    329 }
    330 
    331 
    332 static void eap_fast_derive_keys(struct eap_sm *sm, struct eap_fast_data *data)
    333 {
    334 	if (data->anon_provisioning)
    335 		eap_fast_derive_key_provisioning(sm, data);
    336 	else
    337 		eap_fast_derive_key_auth(sm, data);
    338 }
    339 
    340 
    341 static int eap_fast_init_phase2_method(struct eap_sm *sm,
    342 				       struct eap_fast_data *data)
    343 {
    344 	data->phase2_method =
    345 		eap_peer_get_eap_method(data->phase2_type.vendor,
    346 					data->phase2_type.method);
    347 	if (data->phase2_method == NULL)
    348 		return -1;
    349 
    350 	if (data->key_block_p) {
    351 		sm->auth_challenge = data->key_block_p->server_challenge;
    352 		sm->peer_challenge = data->key_block_p->client_challenge;
    353 	}
    354 	sm->init_phase2 = 1;
    355 	data->phase2_priv = data->phase2_method->init(sm);
    356 	sm->init_phase2 = 0;
    357 	sm->auth_challenge = NULL;
    358 	sm->peer_challenge = NULL;
    359 
    360 	return data->phase2_priv == NULL ? -1 : 0;
    361 }
    362 
    363 
    364 static int eap_fast_select_phase2_method(struct eap_fast_data *data, u8 type)
    365 {
    366 	size_t i;
    367 
    368 	/* TODO: TNC with anonymous provisioning; need to require both
    369 	 * completed MSCHAPv2 and TNC */
    370 
    371 	if (data->anon_provisioning && type != EAP_TYPE_MSCHAPV2) {
    372 		wpa_printf(MSG_INFO, "EAP-FAST: Only EAP-MSCHAPv2 is allowed "
    373 			   "during unauthenticated provisioning; reject phase2"
    374 			   " type %d", type);
    375 		return -1;
    376 	}
    377 
    378 #ifdef EAP_TNC
    379 	if (type == EAP_TYPE_TNC) {
    380 		data->phase2_type.vendor = EAP_VENDOR_IETF;
    381 		data->phase2_type.method = EAP_TYPE_TNC;
    382 		wpa_printf(MSG_DEBUG, "EAP-FAST: Selected Phase 2 EAP "
    383 			   "vendor %d method %d for TNC",
    384 			   data->phase2_type.vendor,
    385 			   data->phase2_type.method);
    386 		return 0;
    387 	}
    388 #endif /* EAP_TNC */
    389 
    390 	for (i = 0; i < data->num_phase2_types; i++) {
    391 		if (data->phase2_types[i].vendor != EAP_VENDOR_IETF ||
    392 		    data->phase2_types[i].method != type)
    393 			continue;
    394 
    395 		data->phase2_type.vendor = data->phase2_types[i].vendor;
    396 		data->phase2_type.method = data->phase2_types[i].method;
    397 		wpa_printf(MSG_DEBUG, "EAP-FAST: Selected Phase 2 EAP "
    398 			   "vendor %d method %d",
    399 			   data->phase2_type.vendor,
    400 			   data->phase2_type.method);
    401 		break;
    402 	}
    403 
    404 	if (type != data->phase2_type.method || type == EAP_TYPE_NONE)
    405 		return -1;
    406 
    407 	return 0;
    408 }
    409 
    410 
    411 static int eap_fast_phase2_request(struct eap_sm *sm,
    412 				   struct eap_fast_data *data,
    413 				   struct eap_method_ret *ret,
    414 				   struct eap_hdr *hdr,
    415 				   struct wpabuf **resp)
    416 {
    417 	size_t len = be_to_host16(hdr->length);
    418 	u8 *pos;
    419 	struct eap_method_ret iret;
    420 	struct eap_peer_config *config = eap_get_config(sm);
    421 	struct wpabuf msg;
    422 
    423 	if (len <= sizeof(struct eap_hdr)) {
    424 		wpa_printf(MSG_INFO, "EAP-FAST: too short "
    425 			   "Phase 2 request (len=%lu)", (unsigned long) len);
    426 		return -1;
    427 	}
    428 	pos = (u8 *) (hdr + 1);
    429 	wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 Request: type=%d", *pos);
    430 	if (*pos == EAP_TYPE_IDENTITY) {
    431 		*resp = eap_sm_buildIdentity(sm, hdr->identifier, 1);
    432 		return 0;
    433 	}
    434 
    435 	if (data->phase2_priv && data->phase2_method &&
    436 	    *pos != data->phase2_type.method) {
    437 		wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 EAP sequence - "
    438 			   "deinitialize previous method");
    439 		data->phase2_method->deinit(sm, data->phase2_priv);
    440 		data->phase2_method = NULL;
    441 		data->phase2_priv = NULL;
    442 		data->phase2_type.vendor = EAP_VENDOR_IETF;
    443 		data->phase2_type.method = EAP_TYPE_NONE;
    444 	}
    445 
    446 	if (data->phase2_type.vendor == EAP_VENDOR_IETF &&
    447 	    data->phase2_type.method == EAP_TYPE_NONE &&
    448 	    eap_fast_select_phase2_method(data, *pos) < 0) {
    449 		if (eap_peer_tls_phase2_nak(data->phase2_types,
    450 					    data->num_phase2_types,
    451 					    hdr, resp))
    452 			return -1;
    453 		return 0;
    454 	}
    455 
    456 	if ((data->phase2_priv == NULL &&
    457 	     eap_fast_init_phase2_method(sm, data) < 0) ||
    458 	    data->phase2_method == NULL) {
    459 		wpa_printf(MSG_INFO, "EAP-FAST: Failed to initialize "
    460 			   "Phase 2 EAP method %d", *pos);
    461 		ret->methodState = METHOD_DONE;
    462 		ret->decision = DECISION_FAIL;
    463 		return -1;
    464 	}
    465 
    466 	os_memset(&iret, 0, sizeof(iret));
    467 	wpabuf_set(&msg, hdr, len);
    468 	*resp = data->phase2_method->process(sm, data->phase2_priv, &iret,
    469 					     &msg);
    470 	if (*resp == NULL ||
    471 	    (iret.methodState == METHOD_DONE &&
    472 	     iret.decision == DECISION_FAIL)) {
    473 		ret->methodState = METHOD_DONE;
    474 		ret->decision = DECISION_FAIL;
    475 	} else if ((iret.methodState == METHOD_DONE ||
    476 		    iret.methodState == METHOD_MAY_CONT) &&
    477 		   (iret.decision == DECISION_UNCOND_SUCC ||
    478 		    iret.decision == DECISION_COND_SUCC)) {
    479 		data->phase2_success = 1;
    480 	}
    481 
    482 	if (*resp == NULL && config &&
    483 	    (config->pending_req_identity || config->pending_req_password ||
    484 	     config->pending_req_otp || config->pending_req_new_password)) {
    485 		wpabuf_free(data->pending_phase2_req);
    486 		data->pending_phase2_req = wpabuf_alloc_copy(hdr, len);
    487 	} else if (*resp == NULL)
    488 		return -1;
    489 
    490 	return 0;
    491 }
    492 
    493 
    494 static struct wpabuf * eap_fast_tlv_nak(int vendor_id, int tlv_type)
    495 {
    496 	struct wpabuf *buf;
    497 	struct eap_tlv_nak_tlv *nak;
    498 	buf = wpabuf_alloc(sizeof(*nak));
    499 	if (buf == NULL)
    500 		return NULL;
    501 	nak = wpabuf_put(buf, sizeof(*nak));
    502 	nak->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY | EAP_TLV_NAK_TLV);
    503 	nak->length = host_to_be16(6);
    504 	nak->vendor_id = host_to_be32(vendor_id);
    505 	nak->nak_type = host_to_be16(tlv_type);
    506 	return buf;
    507 }
    508 
    509 
    510 static struct wpabuf * eap_fast_tlv_result(int status, int intermediate)
    511 {
    512 	struct wpabuf *buf;
    513 	struct eap_tlv_intermediate_result_tlv *result;
    514 	buf = wpabuf_alloc(sizeof(*result));
    515 	if (buf == NULL)
    516 		return NULL;
    517 	wpa_printf(MSG_DEBUG, "EAP-FAST: Add %sResult TLV(status=%d)",
    518 		   intermediate ? "Intermediate " : "", status);
    519 	result = wpabuf_put(buf, sizeof(*result));
    520 	result->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
    521 					(intermediate ?
    522 					 EAP_TLV_INTERMEDIATE_RESULT_TLV :
    523 					 EAP_TLV_RESULT_TLV));
    524 	result->length = host_to_be16(2);
    525 	result->status = host_to_be16(status);
    526 	return buf;
    527 }
    528 
    529 
    530 static struct wpabuf * eap_fast_tlv_pac_ack(void)
    531 {
    532 	struct wpabuf *buf;
    533 	struct eap_tlv_result_tlv *res;
    534 	struct eap_tlv_pac_ack_tlv *ack;
    535 
    536 	buf = wpabuf_alloc(sizeof(*res) + sizeof(*ack));
    537 	if (buf == NULL)
    538 		return NULL;
    539 
    540 	wpa_printf(MSG_DEBUG, "EAP-FAST: Add PAC TLV (ack)");
    541 	ack = wpabuf_put(buf, sizeof(*ack));
    542 	ack->tlv_type = host_to_be16(EAP_TLV_PAC_TLV |
    543 				     EAP_TLV_TYPE_MANDATORY);
    544 	ack->length = host_to_be16(sizeof(*ack) - sizeof(struct eap_tlv_hdr));
    545 	ack->pac_type = host_to_be16(PAC_TYPE_PAC_ACKNOWLEDGEMENT);
    546 	ack->pac_len = host_to_be16(2);
    547 	ack->result = host_to_be16(EAP_TLV_RESULT_SUCCESS);
    548 
    549 	return buf;
    550 }
    551 
    552 
    553 static struct wpabuf * eap_fast_process_eap_payload_tlv(
    554 	struct eap_sm *sm, struct eap_fast_data *data,
    555 	struct eap_method_ret *ret,
    556 	u8 *eap_payload_tlv, size_t eap_payload_tlv_len)
    557 {
    558 	struct eap_hdr *hdr;
    559 	struct wpabuf *resp = NULL;
    560 
    561 	if (eap_payload_tlv_len < sizeof(*hdr)) {
    562 		wpa_printf(MSG_DEBUG, "EAP-FAST: too short EAP "
    563 			   "Payload TLV (len=%lu)",
    564 			   (unsigned long) eap_payload_tlv_len);
    565 		return NULL;
    566 	}
    567 
    568 	hdr = (struct eap_hdr *) eap_payload_tlv;
    569 	if (be_to_host16(hdr->length) > eap_payload_tlv_len) {
    570 		wpa_printf(MSG_DEBUG, "EAP-FAST: EAP packet overflow in "
    571 			   "EAP Payload TLV");
    572 		return NULL;
    573 	}
    574 
    575 	if (hdr->code != EAP_CODE_REQUEST) {
    576 		wpa_printf(MSG_INFO, "EAP-FAST: Unexpected code=%d in "
    577 			   "Phase 2 EAP header", hdr->code);
    578 		return NULL;
    579 	}
    580 
    581 	if (eap_fast_phase2_request(sm, data, ret, hdr, &resp)) {
    582 		wpa_printf(MSG_INFO, "EAP-FAST: Phase2 Request processing "
    583 			   "failed");
    584 		return NULL;
    585 	}
    586 
    587 	return eap_fast_tlv_eap_payload(resp);
    588 }
    589 
    590 
    591 static int eap_fast_validate_crypto_binding(
    592 	struct eap_tlv_crypto_binding_tlv *_bind)
    593 {
    594 	wpa_printf(MSG_DEBUG, "EAP-FAST: Crypto-Binding TLV: Version %d "
    595 		   "Received Version %d SubType %d",
    596 		   _bind->version, _bind->received_version, _bind->subtype);
    597 	wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE",
    598 		    _bind->nonce, sizeof(_bind->nonce));
    599 	wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC",
    600 		    _bind->compound_mac, sizeof(_bind->compound_mac));
    601 
    602 	if (_bind->version != EAP_FAST_VERSION ||
    603 	    _bind->received_version != EAP_FAST_VERSION ||
    604 	    _bind->subtype != EAP_TLV_CRYPTO_BINDING_SUBTYPE_REQUEST) {
    605 		wpa_printf(MSG_INFO, "EAP-FAST: Invalid version/subtype in "
    606 			   "Crypto-Binding TLV: Version %d "
    607 			   "Received Version %d SubType %d",
    608 			   _bind->version, _bind->received_version,
    609 			   _bind->subtype);
    610 		return -1;
    611 	}
    612 
    613 	return 0;
    614 }
    615 
    616 
    617 static void eap_fast_write_crypto_binding(
    618 	struct eap_tlv_crypto_binding_tlv *rbind,
    619 	struct eap_tlv_crypto_binding_tlv *_bind, const u8 *cmk)
    620 {
    621 	rbind->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
    622 				       EAP_TLV_CRYPTO_BINDING_TLV);
    623 	rbind->length = host_to_be16(sizeof(*rbind) -
    624 				     sizeof(struct eap_tlv_hdr));
    625 	rbind->version = EAP_FAST_VERSION;
    626 	rbind->received_version = _bind->version;
    627 	rbind->subtype = EAP_TLV_CRYPTO_BINDING_SUBTYPE_RESPONSE;
    628 	os_memcpy(rbind->nonce, _bind->nonce, sizeof(_bind->nonce));
    629 	inc_byte_array(rbind->nonce, sizeof(rbind->nonce));
    630 	hmac_sha1(cmk, EAP_FAST_CMK_LEN, (u8 *) rbind, sizeof(*rbind),
    631 		  rbind->compound_mac);
    632 
    633 	wpa_printf(MSG_DEBUG, "EAP-FAST: Reply Crypto-Binding TLV: Version %d "
    634 		   "Received Version %d SubType %d",
    635 		   rbind->version, rbind->received_version, rbind->subtype);
    636 	wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE",
    637 		    rbind->nonce, sizeof(rbind->nonce));
    638 	wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC",
    639 		    rbind->compound_mac, sizeof(rbind->compound_mac));
    640 }
    641 
    642 
    643 static int eap_fast_get_phase2_key(struct eap_sm *sm,
    644 				   struct eap_fast_data *data,
    645 				   u8 *isk, size_t isk_len)
    646 {
    647 	u8 *key;
    648 	size_t key_len;
    649 
    650 	os_memset(isk, 0, isk_len);
    651 
    652 	if (data->phase2_method == NULL || data->phase2_priv == NULL) {
    653 		wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 method not "
    654 			   "available");
    655 		return -1;
    656 	}
    657 
    658 	if (data->phase2_method->isKeyAvailable == NULL ||
    659 	    data->phase2_method->getKey == NULL)
    660 		return 0;
    661 
    662 	if (!data->phase2_method->isKeyAvailable(sm, data->phase2_priv) ||
    663 	    (key = data->phase2_method->getKey(sm, data->phase2_priv,
    664 					       &key_len)) == NULL) {
    665 		wpa_printf(MSG_DEBUG, "EAP-FAST: Could not get key material "
    666 			   "from Phase 2");
    667 		return -1;
    668 	}
    669 
    670 	if (key_len > isk_len)
    671 		key_len = isk_len;
    672 	if (key_len == 32 &&
    673 	    data->phase2_method->vendor == EAP_VENDOR_IETF &&
    674 	    data->phase2_method->method == EAP_TYPE_MSCHAPV2) {
    675 		/*
    676 		 * EAP-FAST uses reverse order for MS-MPPE keys when deriving
    677 		 * MSK from EAP-MSCHAPv2. Swap the keys here to get the correct
    678 		 * ISK for EAP-FAST cryptobinding.
    679 		 */
    680 		os_memcpy(isk, key + 16, 16);
    681 		os_memcpy(isk + 16, key, 16);
    682 	} else
    683 		os_memcpy(isk, key, key_len);
    684 	os_free(key);
    685 
    686 	return 0;
    687 }
    688 
    689 
    690 static int eap_fast_get_cmk(struct eap_sm *sm, struct eap_fast_data *data,
    691 			    u8 *cmk)
    692 {
    693 	u8 isk[32], imck[60];
    694 
    695 	wpa_printf(MSG_DEBUG, "EAP-FAST: Determining CMK[%d] for Compound MIC "
    696 		   "calculation", data->simck_idx + 1);
    697 
    698 	/*
    699 	 * RFC 4851, Section 5.2:
    700 	 * IMCK[j] = T-PRF(S-IMCK[j-1], "Inner Methods Compound Keys",
    701 	 *                 MSK[j], 60)
    702 	 * S-IMCK[j] = first 40 octets of IMCK[j]
    703 	 * CMK[j] = last 20 octets of IMCK[j]
    704 	 */
    705 
    706 	if (eap_fast_get_phase2_key(sm, data, isk, sizeof(isk)) < 0)
    707 		return -1;
    708 	wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: ISK[j]", isk, sizeof(isk));
    709 	sha1_t_prf(data->simck, EAP_FAST_SIMCK_LEN,
    710 		   "Inner Methods Compound Keys",
    711 		   isk, sizeof(isk), imck, sizeof(imck));
    712 	data->simck_idx++;
    713 	os_memcpy(data->simck, imck, EAP_FAST_SIMCK_LEN);
    714 	wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: S-IMCK[j]",
    715 			data->simck, EAP_FAST_SIMCK_LEN);
    716 	os_memcpy(cmk, imck + EAP_FAST_SIMCK_LEN, EAP_FAST_CMK_LEN);
    717 	wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: CMK[j]",
    718 			cmk, EAP_FAST_CMK_LEN);
    719 
    720 	return 0;
    721 }
    722 
    723 
    724 static u8 * eap_fast_write_pac_request(u8 *pos, u16 pac_type)
    725 {
    726 	struct eap_tlv_hdr *pac;
    727 	struct eap_tlv_request_action_tlv *act;
    728 	struct eap_tlv_pac_type_tlv *type;
    729 
    730 	act = (struct eap_tlv_request_action_tlv *) pos;
    731 	act->tlv_type = host_to_be16(EAP_TLV_REQUEST_ACTION_TLV);
    732 	act->length = host_to_be16(2);
    733 	act->action = host_to_be16(EAP_TLV_ACTION_PROCESS_TLV);
    734 
    735 	pac = (struct eap_tlv_hdr *) (act + 1);
    736 	pac->tlv_type = host_to_be16(EAP_TLV_PAC_TLV);
    737 	pac->length = host_to_be16(sizeof(*type));
    738 
    739 	type = (struct eap_tlv_pac_type_tlv *) (pac + 1);
    740 	type->tlv_type = host_to_be16(PAC_TYPE_PAC_TYPE);
    741 	type->length = host_to_be16(2);
    742 	type->pac_type = host_to_be16(pac_type);
    743 
    744 	return (u8 *) (type + 1);
    745 }
    746 
    747 
    748 static struct wpabuf * eap_fast_process_crypto_binding(
    749 	struct eap_sm *sm, struct eap_fast_data *data,
    750 	struct eap_method_ret *ret,
    751 	struct eap_tlv_crypto_binding_tlv *_bind, size_t bind_len)
    752 {
    753 	struct wpabuf *resp;
    754 	u8 *pos;
    755 	u8 cmk[EAP_FAST_CMK_LEN], cmac[SHA1_MAC_LEN];
    756 	int res;
    757 	size_t len;
    758 
    759 	if (eap_fast_validate_crypto_binding(_bind) < 0)
    760 		return NULL;
    761 
    762 	if (eap_fast_get_cmk(sm, data, cmk) < 0)
    763 		return NULL;
    764 
    765 	/* Validate received Compound MAC */
    766 	os_memcpy(cmac, _bind->compound_mac, sizeof(cmac));
    767 	os_memset(_bind->compound_mac, 0, sizeof(cmac));
    768 	wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Crypto-Binding TLV for Compound "
    769 		    "MAC calculation", (u8 *) _bind, bind_len);
    770 	hmac_sha1(cmk, EAP_FAST_CMK_LEN, (u8 *) _bind, bind_len,
    771 		  _bind->compound_mac);
    772 	res = os_memcmp_const(cmac, _bind->compound_mac, sizeof(cmac));
    773 	wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Received Compound MAC",
    774 		    cmac, sizeof(cmac));
    775 	wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Calculated Compound MAC",
    776 		    _bind->compound_mac, sizeof(cmac));
    777 	if (res != 0) {
    778 		wpa_printf(MSG_INFO, "EAP-FAST: Compound MAC did not match");
    779 		os_memcpy(_bind->compound_mac, cmac, sizeof(cmac));
    780 		return NULL;
    781 	}
    782 
    783 	/*
    784 	 * Compound MAC was valid, so authentication succeeded. Reply with
    785 	 * crypto binding to allow server to complete authentication.
    786 	 */
    787 
    788 	len = sizeof(struct eap_tlv_crypto_binding_tlv);
    789 	resp = wpabuf_alloc(len);
    790 	if (resp == NULL)
    791 		return NULL;
    792 
    793 	if (!data->anon_provisioning && data->phase2_success &&
    794 	    eap_fast_derive_msk(data) < 0) {
    795 		wpa_printf(MSG_INFO, "EAP-FAST: Failed to generate MSK");
    796 		ret->methodState = METHOD_DONE;
    797 		ret->decision = DECISION_FAIL;
    798 		data->phase2_success = 0;
    799 		wpabuf_free(resp);
    800 		return NULL;
    801 	}
    802 
    803 	if (!data->anon_provisioning && data->phase2_success) {
    804 		os_free(data->session_id);
    805 		data->session_id = eap_peer_tls_derive_session_id(
    806 			sm, &data->ssl, EAP_TYPE_FAST, &data->id_len);
    807 		if (data->session_id) {
    808 			wpa_hexdump(MSG_DEBUG, "EAP-FAST: Derived Session-Id",
    809 				    data->session_id, data->id_len);
    810 		} else {
    811 			wpa_printf(MSG_ERROR, "EAP-FAST: Failed to derive "
    812 				   "Session-Id");
    813 			wpabuf_free(resp);
    814 			return NULL;
    815 		}
    816 	}
    817 
    818 	pos = wpabuf_put(resp, sizeof(struct eap_tlv_crypto_binding_tlv));
    819 	eap_fast_write_crypto_binding((struct eap_tlv_crypto_binding_tlv *)
    820 				      pos, _bind, cmk);
    821 
    822 	return resp;
    823 }
    824 
    825 
    826 static void eap_fast_parse_pac_tlv(struct eap_fast_pac *entry, int type,
    827 				   u8 *pos, size_t len, int *pac_key_found)
    828 {
    829 	switch (type & 0x7fff) {
    830 	case PAC_TYPE_PAC_KEY:
    831 		wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: PAC-Key", pos, len);
    832 		if (len != EAP_FAST_PAC_KEY_LEN) {
    833 			wpa_printf(MSG_DEBUG, "EAP-FAST: Invalid PAC-Key "
    834 				   "length %lu", (unsigned long) len);
    835 			break;
    836 		}
    837 		*pac_key_found = 1;
    838 		os_memcpy(entry->pac_key, pos, len);
    839 		break;
    840 	case PAC_TYPE_PAC_OPAQUE:
    841 		wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Opaque", pos, len);
    842 		entry->pac_opaque = pos;
    843 		entry->pac_opaque_len = len;
    844 		break;
    845 	case PAC_TYPE_PAC_INFO:
    846 		wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Info", pos, len);
    847 		entry->pac_info = pos;
    848 		entry->pac_info_len = len;
    849 		break;
    850 	default:
    851 		wpa_printf(MSG_DEBUG, "EAP-FAST: Ignored unknown PAC type %d",
    852 			   type);
    853 		break;
    854 	}
    855 }
    856 
    857 
    858 static int eap_fast_process_pac_tlv(struct eap_fast_pac *entry,
    859 				    u8 *pac, size_t pac_len)
    860 {
    861 	struct pac_tlv_hdr *hdr;
    862 	u8 *pos;
    863 	size_t left, len;
    864 	int type, pac_key_found = 0;
    865 
    866 	pos = pac;
    867 	left = pac_len;
    868 
    869 	while (left > sizeof(*hdr)) {
    870 		hdr = (struct pac_tlv_hdr *) pos;
    871 		type = be_to_host16(hdr->type);
    872 		len = be_to_host16(hdr->len);
    873 		pos += sizeof(*hdr);
    874 		left -= sizeof(*hdr);
    875 		if (len > left) {
    876 			wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV overrun "
    877 				   "(type=%d len=%lu left=%lu)",
    878 				   type, (unsigned long) len,
    879 				   (unsigned long) left);
    880 			return -1;
    881 		}
    882 
    883 		eap_fast_parse_pac_tlv(entry, type, pos, len, &pac_key_found);
    884 
    885 		pos += len;
    886 		left -= len;
    887 	}
    888 
    889 	if (!pac_key_found || !entry->pac_opaque || !entry->pac_info) {
    890 		wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV does not include "
    891 			   "all the required fields");
    892 		return -1;
    893 	}
    894 
    895 	return 0;
    896 }
    897 
    898 
    899 static int eap_fast_parse_pac_info(struct eap_fast_pac *entry, int type,
    900 				   u8 *pos, size_t len)
    901 {
    902 	u16 pac_type;
    903 	u32 lifetime;
    904 	struct os_time now;
    905 
    906 	switch (type & 0x7fff) {
    907 	case PAC_TYPE_CRED_LIFETIME:
    908 		if (len != 4) {
    909 			wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Info - "
    910 				    "Invalid CRED_LIFETIME length - ignored",
    911 				    pos, len);
    912 			return 0;
    913 		}
    914 
    915 		/*
    916 		 * This is not currently saved separately in PAC files since
    917 		 * the server can automatically initiate PAC update when
    918 		 * needed. Anyway, the information is available from PAC-Info
    919 		 * dump if it is needed for something in the future.
    920 		 */
    921 		lifetime = WPA_GET_BE32(pos);
    922 		os_get_time(&now);
    923 		wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info - CRED_LIFETIME %d "
    924 			   "(%d days)",
    925 			   lifetime, (lifetime - (u32) now.sec) / 86400);
    926 		break;
    927 	case PAC_TYPE_A_ID:
    928 		wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - A-ID",
    929 				  pos, len);
    930 		entry->a_id = pos;
    931 		entry->a_id_len = len;
    932 		break;
    933 	case PAC_TYPE_I_ID:
    934 		wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - I-ID",
    935 				  pos, len);
    936 		entry->i_id = pos;
    937 		entry->i_id_len = len;
    938 		break;
    939 	case PAC_TYPE_A_ID_INFO:
    940 		wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - A-ID-Info",
    941 				  pos, len);
    942 		entry->a_id_info = pos;
    943 		entry->a_id_info_len = len;
    944 		break;
    945 	case PAC_TYPE_PAC_TYPE:
    946 		/* RFC 5422, Section 4.2.6 - PAC-Type TLV */
    947 		if (len != 2) {
    948 			wpa_printf(MSG_INFO, "EAP-FAST: Invalid PAC-Type "
    949 				   "length %lu (expected 2)",
    950 				   (unsigned long) len);
    951 			wpa_hexdump_ascii(MSG_DEBUG,
    952 					  "EAP-FAST: PAC-Info - PAC-Type",
    953 					  pos, len);
    954 			return -1;
    955 		}
    956 		pac_type = WPA_GET_BE16(pos);
    957 		if (pac_type != PAC_TYPE_TUNNEL_PAC &&
    958 		    pac_type != PAC_TYPE_USER_AUTHORIZATION &&
    959 		    pac_type != PAC_TYPE_MACHINE_AUTHENTICATION) {
    960 			wpa_printf(MSG_INFO, "EAP-FAST: Unsupported PAC Type "
    961 				   "%d", pac_type);
    962 			return -1;
    963 		}
    964 
    965 		wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info - PAC-Type %d",
    966 			   pac_type);
    967 		entry->pac_type = pac_type;
    968 		break;
    969 	default:
    970 		wpa_printf(MSG_DEBUG, "EAP-FAST: Ignored unknown PAC-Info "
    971 			   "type %d", type);
    972 		break;
    973 	}
    974 
    975 	return 0;
    976 }
    977 
    978 
    979 static int eap_fast_process_pac_info(struct eap_fast_pac *entry)
    980 {
    981 	struct pac_tlv_hdr *hdr;
    982 	u8 *pos;
    983 	size_t left, len;
    984 	int type;
    985 
    986 	/* RFC 5422, Section 4.2.4 */
    987 
    988 	/* PAC-Type defaults to Tunnel PAC (Type 1) */
    989 	entry->pac_type = PAC_TYPE_TUNNEL_PAC;
    990 
    991 	pos = entry->pac_info;
    992 	left = entry->pac_info_len;
    993 	while (left > sizeof(*hdr)) {
    994 		hdr = (struct pac_tlv_hdr *) pos;
    995 		type = be_to_host16(hdr->type);
    996 		len = be_to_host16(hdr->len);
    997 		pos += sizeof(*hdr);
    998 		left -= sizeof(*hdr);
    999 		if (len > left) {
   1000 			wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info overrun "
   1001 				   "(type=%d len=%lu left=%lu)",
   1002 				   type, (unsigned long) len,
   1003 				   (unsigned long) left);
   1004 			return -1;
   1005 		}
   1006 
   1007 		if (eap_fast_parse_pac_info(entry, type, pos, len) < 0)
   1008 			return -1;
   1009 
   1010 		pos += len;
   1011 		left -= len;
   1012 	}
   1013 
   1014 	if (entry->a_id == NULL || entry->a_id_info == NULL) {
   1015 		wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info does not include "
   1016 			   "all the required fields");
   1017 		return -1;
   1018 	}
   1019 
   1020 	return 0;
   1021 }
   1022 
   1023 
   1024 static struct wpabuf * eap_fast_process_pac(struct eap_sm *sm,
   1025 					    struct eap_fast_data *data,
   1026 					    struct eap_method_ret *ret,
   1027 					    u8 *pac, size_t pac_len)
   1028 {
   1029 	struct eap_peer_config *config = eap_get_config(sm);
   1030 	struct eap_fast_pac entry;
   1031 
   1032 	os_memset(&entry, 0, sizeof(entry));
   1033 	if (eap_fast_process_pac_tlv(&entry, pac, pac_len) ||
   1034 	    eap_fast_process_pac_info(&entry))
   1035 		return NULL;
   1036 
   1037 	eap_fast_add_pac(&data->pac, &data->current_pac, &entry);
   1038 	eap_fast_pac_list_truncate(data->pac, data->max_pac_list_len);
   1039 	if (data->use_pac_binary_format)
   1040 		eap_fast_save_pac_bin(sm, data->pac, config->pac_file);
   1041 	else
   1042 		eap_fast_save_pac(sm, data->pac, config->pac_file);
   1043 
   1044 	if (data->provisioning) {
   1045 		if (data->anon_provisioning) {
   1046 			/*
   1047 			 * Unauthenticated provisioning does not provide keying
   1048 			 * material and must end with an EAP-Failure.
   1049 			 * Authentication will be done separately after this.
   1050 			 */
   1051 			data->success = 0;
   1052 			ret->decision = DECISION_FAIL;
   1053 		} else {
   1054 			/*
   1055 			 * Server may or may not allow authenticated
   1056 			 * provisioning also for key generation.
   1057 			 */
   1058 			ret->decision = DECISION_COND_SUCC;
   1059 		}
   1060 		wpa_printf(MSG_DEBUG, "EAP-FAST: Send PAC-Acknowledgement TLV "
   1061 			   "- Provisioning completed successfully");
   1062 		sm->expected_failure = 1;
   1063 	} else {
   1064 		/*
   1065 		 * This is PAC refreshing, i.e., normal authentication that is
   1066 		 * expected to be completed with an EAP-Success. However,
   1067 		 * RFC 5422, Section 3.5 allows EAP-Failure to be sent even
   1068 		 * after protected success exchange in case of EAP-Fast
   1069 		 * provisioning, so we better use DECISION_COND_SUCC here
   1070 		 * instead of DECISION_UNCOND_SUCC.
   1071 		 */
   1072 		wpa_printf(MSG_DEBUG, "EAP-FAST: Send PAC-Acknowledgement TLV "
   1073 			   "- PAC refreshing completed successfully");
   1074 		ret->decision = DECISION_COND_SUCC;
   1075 	}
   1076 	ret->methodState = METHOD_DONE;
   1077 	return eap_fast_tlv_pac_ack();
   1078 }
   1079 
   1080 
   1081 static int eap_fast_parse_decrypted(struct wpabuf *decrypted,
   1082 				    struct eap_fast_tlv_parse *tlv,
   1083 				    struct wpabuf **resp)
   1084 {
   1085 	int mandatory, tlv_type, res;
   1086 	size_t len;
   1087 	u8 *pos, *end;
   1088 
   1089 	os_memset(tlv, 0, sizeof(*tlv));
   1090 
   1091 	/* Parse TLVs from the decrypted Phase 2 data */
   1092 	pos = wpabuf_mhead(decrypted);
   1093 	end = pos + wpabuf_len(decrypted);
   1094 	while (pos + 4 < end) {
   1095 		mandatory = pos[0] & 0x80;
   1096 		tlv_type = WPA_GET_BE16(pos) & 0x3fff;
   1097 		pos += 2;
   1098 		len = WPA_GET_BE16(pos);
   1099 		pos += 2;
   1100 		if (len > (size_t) (end - pos)) {
   1101 			wpa_printf(MSG_INFO, "EAP-FAST: TLV overflow");
   1102 			return -1;
   1103 		}
   1104 		wpa_printf(MSG_DEBUG, "EAP-FAST: Received Phase 2: "
   1105 			   "TLV type %d length %u%s",
   1106 			   tlv_type, (unsigned int) len,
   1107 			   mandatory ? " (mandatory)" : "");
   1108 
   1109 		res = eap_fast_parse_tlv(tlv, tlv_type, pos, len);
   1110 		if (res == -2)
   1111 			break;
   1112 		if (res < 0) {
   1113 			if (mandatory) {
   1114 				wpa_printf(MSG_DEBUG, "EAP-FAST: Nak unknown "
   1115 					   "mandatory TLV type %d", tlv_type);
   1116 				*resp = eap_fast_tlv_nak(0, tlv_type);
   1117 				break;
   1118 			} else {
   1119 				wpa_printf(MSG_DEBUG, "EAP-FAST: ignored "
   1120 					   "unknown optional TLV type %d",
   1121 					   tlv_type);
   1122 			}
   1123 		}
   1124 
   1125 		pos += len;
   1126 	}
   1127 
   1128 	return 0;
   1129 }
   1130 
   1131 
   1132 static int eap_fast_encrypt_response(struct eap_sm *sm,
   1133 				     struct eap_fast_data *data,
   1134 				     struct wpabuf *resp,
   1135 				     u8 identifier, struct wpabuf **out_data)
   1136 {
   1137 	if (resp == NULL)
   1138 		return 0;
   1139 
   1140 	wpa_hexdump_buf(MSG_DEBUG, "EAP-FAST: Encrypting Phase 2 data",
   1141 			resp);
   1142 	if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_FAST,
   1143 				 data->fast_version, identifier,
   1144 				 resp, out_data)) {
   1145 		wpa_printf(MSG_INFO, "EAP-FAST: Failed to encrypt a Phase 2 "
   1146 			   "frame");
   1147 	}
   1148 	wpabuf_free(resp);
   1149 
   1150 	return 0;
   1151 }
   1152 
   1153 
   1154 static struct wpabuf * eap_fast_pac_request(void)
   1155 {
   1156 	struct wpabuf *tmp;
   1157 	u8 *pos, *pos2;
   1158 
   1159 	tmp = wpabuf_alloc(sizeof(struct eap_tlv_hdr) +
   1160 			   sizeof(struct eap_tlv_request_action_tlv) +
   1161 			   sizeof(struct eap_tlv_pac_type_tlv));
   1162 	if (tmp == NULL)
   1163 		return NULL;
   1164 
   1165 	pos = wpabuf_put(tmp, 0);
   1166 	pos2 = eap_fast_write_pac_request(pos, PAC_TYPE_TUNNEL_PAC);
   1167 	wpabuf_put(tmp, pos2 - pos);
   1168 	return tmp;
   1169 }
   1170 
   1171 
   1172 static int eap_fast_process_decrypted(struct eap_sm *sm,
   1173 				      struct eap_fast_data *data,
   1174 				      struct eap_method_ret *ret,
   1175 				      const struct eap_hdr *req,
   1176 				      struct wpabuf *decrypted,
   1177 				      struct wpabuf **out_data)
   1178 {
   1179 	struct wpabuf *resp = NULL, *tmp;
   1180 	struct eap_fast_tlv_parse tlv;
   1181 	int failed = 0;
   1182 
   1183 	if (eap_fast_parse_decrypted(decrypted, &tlv, &resp) < 0)
   1184 		return 0;
   1185 	if (resp)
   1186 		return eap_fast_encrypt_response(sm, data, resp,
   1187 						 req->identifier, out_data);
   1188 
   1189 	if (tlv.result == EAP_TLV_RESULT_FAILURE) {
   1190 		resp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 0);
   1191 		return eap_fast_encrypt_response(sm, data, resp,
   1192 						 req->identifier, out_data);
   1193 	}
   1194 
   1195 	if (tlv.iresult == EAP_TLV_RESULT_FAILURE) {
   1196 		resp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 1);
   1197 		return eap_fast_encrypt_response(sm, data, resp,
   1198 						 req->identifier, out_data);
   1199 	}
   1200 
   1201 	if (tlv.crypto_binding) {
   1202 		tmp = eap_fast_process_crypto_binding(sm, data, ret,
   1203 						      tlv.crypto_binding,
   1204 						      tlv.crypto_binding_len);
   1205 		if (tmp == NULL)
   1206 			failed = 1;
   1207 		else
   1208 			resp = wpabuf_concat(resp, tmp);
   1209 	}
   1210 
   1211 	if (tlv.iresult == EAP_TLV_RESULT_SUCCESS) {
   1212 		tmp = eap_fast_tlv_result(failed ? EAP_TLV_RESULT_FAILURE :
   1213 					  EAP_TLV_RESULT_SUCCESS, 1);
   1214 		resp = wpabuf_concat(resp, tmp);
   1215 	}
   1216 
   1217 	if (tlv.eap_payload_tlv) {
   1218 		tmp = eap_fast_process_eap_payload_tlv(
   1219 			sm, data, ret, tlv.eap_payload_tlv,
   1220 			tlv.eap_payload_tlv_len);
   1221 		resp = wpabuf_concat(resp, tmp);
   1222 	}
   1223 
   1224 	if (tlv.pac && tlv.result != EAP_TLV_RESULT_SUCCESS) {
   1225 		wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV without Result TLV "
   1226 			   "acknowledging success");
   1227 		failed = 1;
   1228 	} else if (tlv.pac && tlv.result == EAP_TLV_RESULT_SUCCESS) {
   1229 		tmp = eap_fast_process_pac(sm, data, ret, tlv.pac,
   1230 					   tlv.pac_len);
   1231 		resp = wpabuf_concat(resp, tmp);
   1232 	}
   1233 
   1234 	if (data->current_pac == NULL && data->provisioning &&
   1235 	    !data->anon_provisioning && !tlv.pac &&
   1236 	    (tlv.iresult == EAP_TLV_RESULT_SUCCESS ||
   1237 	     tlv.result == EAP_TLV_RESULT_SUCCESS)) {
   1238 		/*
   1239 		 * Need to request Tunnel PAC when using authenticated
   1240 		 * provisioning.
   1241 		 */
   1242 		wpa_printf(MSG_DEBUG, "EAP-FAST: Request Tunnel PAC");
   1243 		tmp = eap_fast_pac_request();
   1244 		resp = wpabuf_concat(resp, tmp);
   1245 	}
   1246 
   1247 	if (tlv.result == EAP_TLV_RESULT_SUCCESS && !failed) {
   1248 		tmp = eap_fast_tlv_result(EAP_TLV_RESULT_SUCCESS, 0);
   1249 		resp = wpabuf_concat(tmp, resp);
   1250 	} else if (failed) {
   1251 		tmp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 0);
   1252 		resp = wpabuf_concat(tmp, resp);
   1253 	}
   1254 
   1255 	if (resp && tlv.result == EAP_TLV_RESULT_SUCCESS && !failed &&
   1256 	    tlv.crypto_binding && data->phase2_success) {
   1257 		if (data->anon_provisioning) {
   1258 			wpa_printf(MSG_DEBUG, "EAP-FAST: Unauthenticated "
   1259 				   "provisioning completed successfully.");
   1260 			ret->methodState = METHOD_DONE;
   1261 			ret->decision = DECISION_FAIL;
   1262 			sm->expected_failure = 1;
   1263 		} else {
   1264 			wpa_printf(MSG_DEBUG, "EAP-FAST: Authentication "
   1265 				   "completed successfully.");
   1266 			if (data->provisioning)
   1267 				ret->methodState = METHOD_MAY_CONT;
   1268 			else
   1269 				ret->methodState = METHOD_DONE;
   1270 			ret->decision = DECISION_UNCOND_SUCC;
   1271 		}
   1272 	}
   1273 
   1274 	if (resp == NULL) {
   1275 		wpa_printf(MSG_DEBUG, "EAP-FAST: No recognized TLVs - send "
   1276 			   "empty response packet");
   1277 		resp = wpabuf_alloc(1);
   1278 	}
   1279 
   1280 	return eap_fast_encrypt_response(sm, data, resp, req->identifier,
   1281 					 out_data);
   1282 }
   1283 
   1284 
   1285 static int eap_fast_decrypt(struct eap_sm *sm, struct eap_fast_data *data,
   1286 			    struct eap_method_ret *ret,
   1287 			    const struct eap_hdr *req,
   1288 			    const struct wpabuf *in_data,
   1289 			    struct wpabuf **out_data)
   1290 {
   1291 	struct wpabuf *in_decrypted;
   1292 	int res;
   1293 
   1294 	wpa_printf(MSG_DEBUG, "EAP-FAST: Received %lu bytes encrypted data for"
   1295 		   " Phase 2", (unsigned long) wpabuf_len(in_data));
   1296 
   1297 	if (data->pending_phase2_req) {
   1298 		wpa_printf(MSG_DEBUG, "EAP-FAST: Pending Phase 2 request - "
   1299 			   "skip decryption and use old data");
   1300 		/* Clear TLS reassembly state. */
   1301 		eap_peer_tls_reset_input(&data->ssl);
   1302 
   1303 		in_decrypted = data->pending_phase2_req;
   1304 		data->pending_phase2_req = NULL;
   1305 		goto continue_req;
   1306 	}
   1307 
   1308 	if (wpabuf_len(in_data) == 0) {
   1309 		/* Received TLS ACK - requesting more fragments */
   1310 		return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_FAST,
   1311 					    data->fast_version,
   1312 					    req->identifier, NULL, out_data);
   1313 	}
   1314 
   1315 	res = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
   1316 	if (res)
   1317 		return res;
   1318 
   1319 continue_req:
   1320 	wpa_hexdump_buf(MSG_MSGDUMP, "EAP-FAST: Decrypted Phase 2 TLV(s)",
   1321 			in_decrypted);
   1322 
   1323 	if (wpabuf_len(in_decrypted) < 4) {
   1324 		wpa_printf(MSG_INFO, "EAP-FAST: Too short Phase 2 "
   1325 			   "TLV frame (len=%lu)",
   1326 			   (unsigned long) wpabuf_len(in_decrypted));
   1327 		wpabuf_free(in_decrypted);
   1328 		return -1;
   1329 	}
   1330 
   1331 	res = eap_fast_process_decrypted(sm, data, ret, req,
   1332 					 in_decrypted, out_data);
   1333 
   1334 	wpabuf_free(in_decrypted);
   1335 
   1336 	return res;
   1337 }
   1338 
   1339 
   1340 static const u8 * eap_fast_get_a_id(const u8 *buf, size_t len, size_t *id_len)
   1341 {
   1342 	const u8 *a_id;
   1343 	struct pac_tlv_hdr *hdr;
   1344 
   1345 	/*
   1346 	 * Parse authority identity (A-ID) from the EAP-FAST/Start. This
   1347 	 * supports both raw A-ID and one inside an A-ID TLV.
   1348 	 */
   1349 	a_id = buf;
   1350 	*id_len = len;
   1351 	if (len > sizeof(*hdr)) {
   1352 		int tlen;
   1353 		hdr = (struct pac_tlv_hdr *) buf;
   1354 		tlen = be_to_host16(hdr->len);
   1355 		if (be_to_host16(hdr->type) == PAC_TYPE_A_ID &&
   1356 		    sizeof(*hdr) + tlen <= len) {
   1357 			wpa_printf(MSG_DEBUG, "EAP-FAST: A-ID was in TLV "
   1358 				   "(Start)");
   1359 			a_id = (u8 *) (hdr + 1);
   1360 			*id_len = tlen;
   1361 		}
   1362 	}
   1363 	wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: A-ID", a_id, *id_len);
   1364 
   1365 	return a_id;
   1366 }
   1367 
   1368 
   1369 static void eap_fast_select_pac(struct eap_fast_data *data,
   1370 				const u8 *a_id, size_t a_id_len)
   1371 {
   1372 	data->current_pac = eap_fast_get_pac(data->pac, a_id, a_id_len,
   1373 					     PAC_TYPE_TUNNEL_PAC);
   1374 	if (data->current_pac == NULL) {
   1375 		/*
   1376 		 * Tunnel PAC was not available for this A-ID. Try to use
   1377 		 * Machine Authentication PAC, if one is available.
   1378 		 */
   1379 		data->current_pac = eap_fast_get_pac(
   1380 			data->pac, a_id, a_id_len,
   1381 			PAC_TYPE_MACHINE_AUTHENTICATION);
   1382 	}
   1383 
   1384 	if (data->current_pac) {
   1385 		wpa_printf(MSG_DEBUG, "EAP-FAST: PAC found for this A-ID "
   1386 			   "(PAC-Type %d)", data->current_pac->pac_type);
   1387 		wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-FAST: A-ID-Info",
   1388 				  data->current_pac->a_id_info,
   1389 				  data->current_pac->a_id_info_len);
   1390 	}
   1391 }
   1392 
   1393 
   1394 static int eap_fast_use_pac_opaque(struct eap_sm *sm,
   1395 				   struct eap_fast_data *data,
   1396 				   struct eap_fast_pac *pac)
   1397 {
   1398 	u8 *tlv;
   1399 	size_t tlv_len, olen;
   1400 	struct eap_tlv_hdr *ehdr;
   1401 
   1402 	olen = pac->pac_opaque_len;
   1403 	tlv_len = sizeof(*ehdr) + olen;
   1404 	tlv = os_malloc(tlv_len);
   1405 	if (tlv) {
   1406 		ehdr = (struct eap_tlv_hdr *) tlv;
   1407 		ehdr->tlv_type = host_to_be16(PAC_TYPE_PAC_OPAQUE);
   1408 		ehdr->length = host_to_be16(olen);
   1409 		os_memcpy(ehdr + 1, pac->pac_opaque, olen);
   1410 	}
   1411 	if (tlv == NULL ||
   1412 	    tls_connection_client_hello_ext(sm->ssl_ctx, data->ssl.conn,
   1413 					    TLS_EXT_PAC_OPAQUE,
   1414 					    tlv, tlv_len) < 0) {
   1415 		wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to add PAC-Opaque TLS "
   1416 			   "extension");
   1417 		os_free(tlv);
   1418 		return -1;
   1419 	}
   1420 	os_free(tlv);
   1421 
   1422 	return 0;
   1423 }
   1424 
   1425 
   1426 static int eap_fast_clear_pac_opaque_ext(struct eap_sm *sm,
   1427 					 struct eap_fast_data *data)
   1428 {
   1429 	if (tls_connection_client_hello_ext(sm->ssl_ctx, data->ssl.conn,
   1430 					    TLS_EXT_PAC_OPAQUE, NULL, 0) < 0) {
   1431 		wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to remove PAC-Opaque "
   1432 			   "TLS extension");
   1433 		return -1;
   1434 	}
   1435 	return 0;
   1436 }
   1437 
   1438 
   1439 static int eap_fast_set_provisioning_ciphers(struct eap_sm *sm,
   1440 					     struct eap_fast_data *data)
   1441 {
   1442 	u8 ciphers[5];
   1443 	int count = 0;
   1444 
   1445 	if (data->provisioning_allowed & EAP_FAST_PROV_UNAUTH) {
   1446 		wpa_printf(MSG_DEBUG, "EAP-FAST: Enabling unauthenticated "
   1447 			   "provisioning TLS cipher suites");
   1448 		ciphers[count++] = TLS_CIPHER_ANON_DH_AES128_SHA;
   1449 	}
   1450 
   1451 	if (data->provisioning_allowed & EAP_FAST_PROV_AUTH) {
   1452 		wpa_printf(MSG_DEBUG, "EAP-FAST: Enabling authenticated "
   1453 			   "provisioning TLS cipher suites");
   1454 		ciphers[count++] = TLS_CIPHER_RSA_DHE_AES128_SHA;
   1455 		ciphers[count++] = TLS_CIPHER_AES128_SHA;
   1456 		ciphers[count++] = TLS_CIPHER_RC4_SHA;
   1457 	}
   1458 
   1459 	ciphers[count++] = TLS_CIPHER_NONE;
   1460 
   1461 	if (tls_connection_set_cipher_list(sm->ssl_ctx, data->ssl.conn,
   1462 					   ciphers)) {
   1463 		wpa_printf(MSG_INFO, "EAP-FAST: Could not configure TLS "
   1464 			   "cipher suites for provisioning");
   1465 		return -1;
   1466 	}
   1467 
   1468 	return 0;
   1469 }
   1470 
   1471 
   1472 static int eap_fast_process_start(struct eap_sm *sm,
   1473 				  struct eap_fast_data *data, u8 flags,
   1474 				  const u8 *pos, size_t left)
   1475 {
   1476 	const u8 *a_id;
   1477 	size_t a_id_len;
   1478 
   1479 	/* EAP-FAST Version negotiation (section 3.1) */
   1480 	wpa_printf(MSG_DEBUG, "EAP-FAST: Start (server ver=%d, own ver=%d)",
   1481 		   flags & EAP_TLS_VERSION_MASK, data->fast_version);
   1482 	if ((flags & EAP_TLS_VERSION_MASK) < data->fast_version)
   1483 		data->fast_version = flags & EAP_TLS_VERSION_MASK;
   1484 	wpa_printf(MSG_DEBUG, "EAP-FAST: Using FAST version %d",
   1485 		   data->fast_version);
   1486 
   1487 	a_id = eap_fast_get_a_id(pos, left, &a_id_len);
   1488 	eap_fast_select_pac(data, a_id, a_id_len);
   1489 
   1490 	if (data->resuming && data->current_pac) {
   1491 		wpa_printf(MSG_DEBUG, "EAP-FAST: Trying to resume session - "
   1492 			   "do not add PAC-Opaque to TLS ClientHello");
   1493 		if (eap_fast_clear_pac_opaque_ext(sm, data) < 0)
   1494 			return -1;
   1495 	} else if (data->current_pac) {
   1496 		/*
   1497 		 * PAC found for the A-ID and we are not resuming an old
   1498 		 * session, so add PAC-Opaque extension to ClientHello.
   1499 		 */
   1500 		if (eap_fast_use_pac_opaque(sm, data, data->current_pac) < 0)
   1501 			return -1;
   1502 	} else {
   1503 		/* No PAC found, so we must provision one. */
   1504 		if (!data->provisioning_allowed) {
   1505 			wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC found and "
   1506 				   "provisioning disabled");
   1507 			return -1;
   1508 		}
   1509 		wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC found - "
   1510 			   "starting provisioning");
   1511 		if (eap_fast_set_provisioning_ciphers(sm, data) < 0 ||
   1512 		    eap_fast_clear_pac_opaque_ext(sm, data) < 0)
   1513 			return -1;
   1514 		data->provisioning = 1;
   1515 	}
   1516 
   1517 	return 0;
   1518 }
   1519 
   1520 
   1521 static struct wpabuf * eap_fast_process(struct eap_sm *sm, void *priv,
   1522 					struct eap_method_ret *ret,
   1523 					const struct wpabuf *reqData)
   1524 {
   1525 	const struct eap_hdr *req;
   1526 	size_t left;
   1527 	int res;
   1528 	u8 flags, id;
   1529 	struct wpabuf *resp;
   1530 	const u8 *pos;
   1531 	struct eap_fast_data *data = priv;
   1532 
   1533 	pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_FAST, ret,
   1534 					reqData, &left, &flags);
   1535 	if (pos == NULL)
   1536 		return NULL;
   1537 
   1538 	req = wpabuf_head(reqData);
   1539 	id = req->identifier;
   1540 
   1541 	if (flags & EAP_TLS_FLAGS_START) {
   1542 		if (eap_fast_process_start(sm, data, flags, pos, left) < 0)
   1543 			return NULL;
   1544 
   1545 		left = 0; /* A-ID is not used in further packet processing */
   1546 	}
   1547 
   1548 	resp = NULL;
   1549 	if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
   1550 	    !data->resuming) {
   1551 		/* Process tunneled (encrypted) phase 2 data. */
   1552 		struct wpabuf msg;
   1553 		wpabuf_set(&msg, pos, left);
   1554 		res = eap_fast_decrypt(sm, data, ret, req, &msg, &resp);
   1555 		if (res < 0) {
   1556 			ret->methodState = METHOD_DONE;
   1557 			ret->decision = DECISION_FAIL;
   1558 			/*
   1559 			 * Ack possible Alert that may have caused failure in
   1560 			 * decryption.
   1561 			 */
   1562 			res = 1;
   1563 		}
   1564 	} else {
   1565 		/* Continue processing TLS handshake (phase 1). */
   1566 		res = eap_peer_tls_process_helper(sm, &data->ssl,
   1567 						  EAP_TYPE_FAST,
   1568 						  data->fast_version, id, pos,
   1569 						  left, &resp);
   1570 
   1571 		if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
   1572 			char cipher[80];
   1573 			wpa_printf(MSG_DEBUG,
   1574 				   "EAP-FAST: TLS done, proceed to Phase 2");
   1575 			if (data->provisioning &&
   1576 			    (!(data->provisioning_allowed &
   1577 			       EAP_FAST_PROV_AUTH) ||
   1578 			     tls_get_cipher(sm->ssl_ctx, data->ssl.conn,
   1579 					    cipher, sizeof(cipher)) < 0 ||
   1580 			     os_strstr(cipher, "ADH-") ||
   1581 			     os_strstr(cipher, "anon"))) {
   1582 				wpa_printf(MSG_DEBUG, "EAP-FAST: Using "
   1583 					   "anonymous (unauthenticated) "
   1584 					   "provisioning");
   1585 				data->anon_provisioning = 1;
   1586 			} else
   1587 				data->anon_provisioning = 0;
   1588 			data->resuming = 0;
   1589 			eap_fast_derive_keys(sm, data);
   1590 		}
   1591 
   1592 		if (res == 2) {
   1593 			struct wpabuf msg;
   1594 			/*
   1595 			 * Application data included in the handshake message.
   1596 			 */
   1597 			wpabuf_free(data->pending_phase2_req);
   1598 			data->pending_phase2_req = resp;
   1599 			resp = NULL;
   1600 			wpabuf_set(&msg, pos, left);
   1601 			res = eap_fast_decrypt(sm, data, ret, req, &msg,
   1602 					       &resp);
   1603 		}
   1604 	}
   1605 
   1606 	if (res == 1) {
   1607 		wpabuf_free(resp);
   1608 		return eap_peer_tls_build_ack(id, EAP_TYPE_FAST,
   1609 					      data->fast_version);
   1610 	}
   1611 
   1612 	return resp;
   1613 }
   1614 
   1615 
   1616 #if 0 /* FIX */
   1617 static Boolean eap_fast_has_reauth_data(struct eap_sm *sm, void *priv)
   1618 {
   1619 	struct eap_fast_data *data = priv;
   1620 	return tls_connection_established(sm->ssl_ctx, data->ssl.conn);
   1621 }
   1622 
   1623 
   1624 static void eap_fast_deinit_for_reauth(struct eap_sm *sm, void *priv)
   1625 {
   1626 	struct eap_fast_data *data = priv;
   1627 	os_free(data->key_block_p);
   1628 	data->key_block_p = NULL;
   1629 	wpabuf_free(data->pending_phase2_req);
   1630 	data->pending_phase2_req = NULL;
   1631 }
   1632 
   1633 
   1634 static void * eap_fast_init_for_reauth(struct eap_sm *sm, void *priv)
   1635 {
   1636 	struct eap_fast_data *data = priv;
   1637 	if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
   1638 		os_free(data);
   1639 		return NULL;
   1640 	}
   1641 	os_memset(data->key_data, 0, EAP_FAST_KEY_LEN);
   1642 	os_memset(data->emsk, 0, EAP_EMSK_LEN);
   1643 	os_free(data->session_id);
   1644 	data->session_id = NULL;
   1645 	if (data->phase2_priv && data->phase2_method &&
   1646 	    data->phase2_method->init_for_reauth)
   1647 		data->phase2_method->init_for_reauth(sm, data->phase2_priv);
   1648 	data->phase2_success = 0;
   1649 	data->resuming = 1;
   1650 	data->provisioning = 0;
   1651 	data->anon_provisioning = 0;
   1652 	data->simck_idx = 0;
   1653 	return priv;
   1654 }
   1655 #endif
   1656 
   1657 
   1658 static int eap_fast_get_status(struct eap_sm *sm, void *priv, char *buf,
   1659 			       size_t buflen, int verbose)
   1660 {
   1661 	struct eap_fast_data *data = priv;
   1662 	int len, ret;
   1663 
   1664 	len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
   1665 	if (data->phase2_method) {
   1666 		ret = os_snprintf(buf + len, buflen - len,
   1667 				  "EAP-FAST Phase2 method=%s\n",
   1668 				  data->phase2_method->name);
   1669 		if (ret < 0 || (size_t) ret >= buflen - len)
   1670 			return len;
   1671 		len += ret;
   1672 	}
   1673 	return len;
   1674 }
   1675 
   1676 
   1677 static Boolean eap_fast_isKeyAvailable(struct eap_sm *sm, void *priv)
   1678 {
   1679 	struct eap_fast_data *data = priv;
   1680 	return data->success;
   1681 }
   1682 
   1683 
   1684 static u8 * eap_fast_getKey(struct eap_sm *sm, void *priv, size_t *len)
   1685 {
   1686 	struct eap_fast_data *data = priv;
   1687 	u8 *key;
   1688 
   1689 	if (!data->success)
   1690 		return NULL;
   1691 
   1692 	key = os_malloc(EAP_FAST_KEY_LEN);
   1693 	if (key == NULL)
   1694 		return NULL;
   1695 
   1696 	*len = EAP_FAST_KEY_LEN;
   1697 	os_memcpy(key, data->key_data, EAP_FAST_KEY_LEN);
   1698 
   1699 	return key;
   1700 }
   1701 
   1702 
   1703 static u8 * eap_fast_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
   1704 {
   1705 	struct eap_fast_data *data = priv;
   1706 	u8 *id;
   1707 
   1708 	if (!data->success)
   1709 		return NULL;
   1710 
   1711 	id = os_malloc(data->id_len);
   1712 	if (id == NULL)
   1713 		return NULL;
   1714 
   1715 	*len = data->id_len;
   1716 	os_memcpy(id, data->session_id, data->id_len);
   1717 
   1718 	return id;
   1719 }
   1720 
   1721 
   1722 static u8 * eap_fast_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
   1723 {
   1724 	struct eap_fast_data *data = priv;
   1725 	u8 *key;
   1726 
   1727 	if (!data->success)
   1728 		return NULL;
   1729 
   1730 	key = os_malloc(EAP_EMSK_LEN);
   1731 	if (key == NULL)
   1732 		return NULL;
   1733 
   1734 	*len = EAP_EMSK_LEN;
   1735 	os_memcpy(key, data->emsk, EAP_EMSK_LEN);
   1736 
   1737 	return key;
   1738 }
   1739 
   1740 
   1741 int eap_peer_fast_register(void)
   1742 {
   1743 	struct eap_method *eap;
   1744 	int ret;
   1745 
   1746 	eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
   1747 				    EAP_VENDOR_IETF, EAP_TYPE_FAST, "FAST");
   1748 	if (eap == NULL)
   1749 		return -1;
   1750 
   1751 	eap->init = eap_fast_init;
   1752 	eap->deinit = eap_fast_deinit;
   1753 	eap->process = eap_fast_process;
   1754 	eap->isKeyAvailable = eap_fast_isKeyAvailable;
   1755 	eap->getKey = eap_fast_getKey;
   1756 	eap->getSessionId = eap_fast_get_session_id;
   1757 	eap->get_status = eap_fast_get_status;
   1758 #if 0
   1759 	eap->has_reauth_data = eap_fast_has_reauth_data;
   1760 	eap->deinit_for_reauth = eap_fast_deinit_for_reauth;
   1761 	eap->init_for_reauth = eap_fast_init_for_reauth;
   1762 #endif
   1763 	eap->get_emsk = eap_fast_get_emsk;
   1764 
   1765 	ret = eap_peer_method_register(eap);
   1766 	if (ret)
   1767 		eap_peer_method_free(eap);
   1768 	return ret;
   1769 }
   1770