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