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