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