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