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