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