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_peer/eap_tlv_common.h"
15 #include "eap_peer/eap_methods.h"
16 #include "eap_i.h"
17 #include "eap_tls_common.h"
18 #include "eap_config.h"
19 #include "eap_fast_pac.h"
20 
21 #include "eap_fast_pac.c"
22 
23 /* TODO:
24  * - test session resumption and enable it if it interoperates
25  * - password change (pending mschapv2 packet; replay decrypted packet)
26  */
27 
28 
29 static void eap_fast_deinit(struct eap_sm *sm, void *priv);
30 
31 
32 struct eap_fast_data {
33 	struct eap_ssl_data ssl;
34 
35 	int fast_version;
36 
37 	const struct eap_method *phase2_method;
38 	void *phase2_priv;
39 	int phase2_success;
40 
41 	struct eap_method_type phase2_type;
42 	struct eap_method_type *phase2_types;
43 	size_t num_phase2_types;
44 	int resuming; /* starting a resumed session */
45 	struct eap_fast_key_block_provisioning *key_block_p;
46 #define EAP_FAST_PROV_UNAUTH 1
47 #define EAP_FAST_PROV_AUTH 2
48 	int provisioning_allowed; /* Allowed PAC provisioning modes */
49 	int provisioning; /* doing PAC provisioning (not the normal auth) */
50 	int anon_provisioning; /* doing anonymous (unauthenticated)
51 				* provisioning */
52 	int session_ticket_used;
53 
54 	u8 key_data[EAP_FAST_KEY_LEN];
55 	u8 *session_id;
56 	size_t id_len;
57 	u8 emsk[EAP_EMSK_LEN];
58 	int success;
59 
60 	struct eap_fast_pac *pac;
61 	struct eap_fast_pac *current_pac;
62 	size_t max_pac_list_len;
63 	int use_pac_binary_format;
64 
65 	u8 simck[EAP_FAST_SIMCK_LEN];
66 	int simck_idx;
67 
68 	struct wpabuf *pending_phase2_req;
69 	struct wpabuf *pending_resp;
70 };
71 
72 
eap_fast_session_ticket_cb(void * ctx,const u8 * ticket,size_t len,const u8 * client_random,const u8 * server_random,u8 * master_secret)73 static int eap_fast_session_ticket_cb(void *ctx, const u8 *ticket, size_t len,
74 				      const u8 *client_random,
75 				      const u8 *server_random,
76 				      u8 *master_secret)
77 {
78 	struct eap_fast_data *data = ctx;
79 
80 	wpa_printf(MSG_DEBUG, "EAP-FAST: SessionTicket callback");
81 
82 	if (client_random == NULL || server_random == NULL ||
83 	    master_secret == NULL) {
84 		wpa_printf(MSG_DEBUG, "EAP-FAST: SessionTicket failed - fall "
85 			   "back to full TLS handshake");
86 		data->session_ticket_used = 0;
87 		if (data->provisioning_allowed) {
88 			wpa_printf(MSG_DEBUG, "EAP-FAST: Try to provision a "
89 				   "new PAC-Key");
90 			data->provisioning = 1;
91 			data->current_pac = NULL;
92 		}
93 		return 0;
94 	}
95 
96 	wpa_hexdump(MSG_DEBUG, "EAP-FAST: SessionTicket", ticket, len);
97 
98 	if (data->current_pac == NULL) {
99 		wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC-Key available for "
100 			   "using SessionTicket");
101 		data->session_ticket_used = 0;
102 		return 0;
103 	}
104 
105 	eap_fast_derive_master_secret(data->current_pac->pac_key,
106 				      server_random, client_random,
107 				      master_secret);
108 
109 	data->session_ticket_used = 1;
110 
111 	return 1;
112 }
113 
114 
eap_fast_parse_phase1(struct eap_fast_data * data,const char * phase1)115 static void eap_fast_parse_phase1(struct eap_fast_data *data,
116 				  const char *phase1)
117 {
118 	const char *pos;
119 
120 	pos = os_strstr(phase1, "fast_provisioning=");
121 	if (pos) {
122 		data->provisioning_allowed = atoi(pos + 18);
123 		wpa_printf(MSG_INFO, "EAP-FAST: Automatic PAC provisioning "
124 			   "mode: %d", data->provisioning_allowed);
125 	}
126 
127 	pos = os_strstr(phase1, "fast_max_pac_list_len=");
128 	if (pos) {
129 		data->max_pac_list_len = atoi(pos + 22);
130 		if (data->max_pac_list_len == 0)
131 			data->max_pac_list_len = 1;
132 		wpa_printf(MSG_INFO, "EAP-FAST: Maximum PAC list length: %lu",
133 			   (unsigned long) data->max_pac_list_len);
134 	}
135 
136 	pos = os_strstr(phase1, "fast_pac_format=binary");
137 	if (pos) {
138 		data->use_pac_binary_format = 1;
139 		wpa_printf(MSG_INFO, "EAP-FAST: Using binary format for PAC "
140 			   "list");
141 	}
142 }
143 
144 
eap_fast_init(struct eap_sm * sm)145 static void * eap_fast_init(struct eap_sm *sm)
146 {
147 	struct eap_fast_data *data;
148 	struct eap_peer_config *config = eap_get_config(sm);
149 
150 	if (config == NULL)
151 		return NULL;
152 
153 	data = os_zalloc(sizeof(*data));
154 	if (data == NULL)
155 		return NULL;
156 	data->fast_version = EAP_FAST_VERSION;
157 	data->max_pac_list_len = 10;
158 
159 	if (config->phase1)
160 		eap_fast_parse_phase1(data, config->phase1);
161 
162 	if (eap_peer_select_phase2_methods(config, "auth=",
163 					   &data->phase2_types,
164 					   &data->num_phase2_types) < 0) {
165 		eap_fast_deinit(sm, data);
166 		return NULL;
167 	}
168 
169 	data->phase2_type.vendor = EAP_VENDOR_IETF;
170 	data->phase2_type.method = EAP_TYPE_NONE;
171 
172 	if (eap_peer_tls_ssl_init(sm, &data->ssl, config, EAP_TYPE_FAST)) {
173 		wpa_printf(MSG_INFO, "EAP-FAST: Failed to initialize SSL.");
174 		eap_fast_deinit(sm, data);
175 		return NULL;
176 	}
177 
178 	if (tls_connection_set_session_ticket_cb(sm->ssl_ctx, data->ssl.conn,
179 						 eap_fast_session_ticket_cb,
180 						 data) < 0) {
181 		wpa_printf(MSG_INFO, "EAP-FAST: Failed to set SessionTicket "
182 			   "callback");
183 		eap_fast_deinit(sm, data);
184 		return NULL;
185 	}
186 
187 	/*
188 	 * The local RADIUS server in a Cisco AP does not seem to like empty
189 	 * fragments before data, so disable that workaround for CBC.
190 	 * TODO: consider making this configurable
191 	 */
192 	if (tls_connection_enable_workaround(sm->ssl_ctx, data->ssl.conn)) {
193 		wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to enable TLS "
194 			   "workarounds");
195 	}
196 
197 	if (!config->pac_file) {
198 		wpa_printf(MSG_INFO, "EAP-FAST: No PAC file configured");
199 		eap_fast_deinit(sm, data);
200 		return NULL;
201 	}
202 
203 	if (data->use_pac_binary_format &&
204 	    eap_fast_load_pac_bin(sm, &data->pac, config->pac_file) < 0) {
205 		wpa_printf(MSG_INFO, "EAP-FAST: Failed to load PAC file");
206 		eap_fast_deinit(sm, data);
207 		return NULL;
208 	}
209 
210 	if (!data->use_pac_binary_format &&
211 	    eap_fast_load_pac(sm, &data->pac, config->pac_file) < 0) {
212 		wpa_printf(MSG_INFO, "EAP-FAST: Failed to load PAC file");
213 		eap_fast_deinit(sm, data);
214 		return NULL;
215 	}
216 	eap_fast_pac_list_truncate(data->pac, data->max_pac_list_len);
217 
218 	if (data->pac == NULL && !data->provisioning_allowed) {
219 		wpa_printf(MSG_INFO, "EAP-FAST: No PAC configured and "
220 			   "provisioning disabled");
221 		eap_fast_deinit(sm, data);
222 		return NULL;
223 	}
224 
225 	return data;
226 }
227 
228 
eap_fast_deinit(struct eap_sm * sm,void * priv)229 static void eap_fast_deinit(struct eap_sm *sm, void *priv)
230 {
231 	struct eap_fast_data *data = priv;
232 	struct eap_fast_pac *pac, *prev;
233 
234 	if (data == NULL)
235 		return;
236 	if (data->phase2_priv && data->phase2_method)
237 		data->phase2_method->deinit(sm, data->phase2_priv);
238 	os_free(data->phase2_types);
239 	os_free(data->key_block_p);
240 	eap_peer_tls_ssl_deinit(sm, &data->ssl);
241 
242 	pac = data->pac;
243 	prev = NULL;
244 	while (pac) {
245 		prev = pac;
246 		pac = pac->next;
247 		eap_fast_free_pac(prev);
248 	}
249 	os_memset(data->key_data, 0, EAP_FAST_KEY_LEN);
250 	os_memset(data->emsk, 0, EAP_EMSK_LEN);
251 	os_free(data->session_id);
252 	wpabuf_free(data->pending_phase2_req);
253 	wpabuf_free(data->pending_resp);
254 	os_free(data);
255 }
256 
257 
eap_fast_derive_msk(struct eap_fast_data * data)258 static int eap_fast_derive_msk(struct eap_fast_data *data)
259 {
260 	if (eap_fast_derive_eap_msk(data->simck, data->key_data) < 0 ||
261 	    eap_fast_derive_eap_emsk(data->simck, data->emsk) < 0)
262 		return -1;
263 	data->success = 1;
264 	return 0;
265 }
266 
267 
eap_fast_derive_key_auth(struct eap_sm * sm,struct eap_fast_data * data)268 static int eap_fast_derive_key_auth(struct eap_sm *sm,
269 				    struct eap_fast_data *data)
270 {
271 	u8 *sks;
272 
273 	/* RFC 4851, Section 5.1:
274 	 * Extra key material after TLS key_block: session_key_seed[40]
275 	 */
276 
277 	sks = eap_fast_derive_key(sm->ssl_ctx, data->ssl.conn,
278 				  EAP_FAST_SKS_LEN);
279 	if (sks == NULL) {
280 		wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive "
281 			   "session_key_seed");
282 		return -1;
283 	}
284 
285 	/*
286 	 * RFC 4851, Section 5.2:
287 	 * S-IMCK[0] = session_key_seed
288 	 */
289 	wpa_hexdump_key(MSG_DEBUG,
290 			"EAP-FAST: session_key_seed (SKS = S-IMCK[0])",
291 			sks, EAP_FAST_SKS_LEN);
292 	data->simck_idx = 0;
293 	os_memcpy(data->simck, sks, EAP_FAST_SIMCK_LEN);
294 	os_free(sks);
295 	return 0;
296 }
297 
298 
eap_fast_derive_key_provisioning(struct eap_sm * sm,struct eap_fast_data * data)299 static int eap_fast_derive_key_provisioning(struct eap_sm *sm,
300 					    struct eap_fast_data *data)
301 {
302 	os_free(data->key_block_p);
303 	data->key_block_p = (struct eap_fast_key_block_provisioning *)
304 		eap_fast_derive_key(sm->ssl_ctx, data->ssl.conn,
305 				    sizeof(*data->key_block_p));
306 	if (data->key_block_p == NULL) {
307 		wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive key block");
308 		return -1;
309 	}
310 	/*
311 	 * RFC 4851, Section 5.2:
312 	 * S-IMCK[0] = session_key_seed
313 	 */
314 	wpa_hexdump_key(MSG_DEBUG,
315 			"EAP-FAST: session_key_seed (SKS = S-IMCK[0])",
316 			data->key_block_p->session_key_seed,
317 			sizeof(data->key_block_p->session_key_seed));
318 	data->simck_idx = 0;
319 	os_memcpy(data->simck, data->key_block_p->session_key_seed,
320 		  EAP_FAST_SIMCK_LEN);
321 	wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: server_challenge",
322 			data->key_block_p->server_challenge,
323 			sizeof(data->key_block_p->server_challenge));
324 	wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: client_challenge",
325 			data->key_block_p->client_challenge,
326 			sizeof(data->key_block_p->client_challenge));
327 	return 0;
328 }
329 
330 
eap_fast_derive_keys(struct eap_sm * sm,struct eap_fast_data * data)331 static int eap_fast_derive_keys(struct eap_sm *sm, struct eap_fast_data *data)
332 {
333 	int res;
334 
335 	if (data->anon_provisioning)
336 		res = eap_fast_derive_key_provisioning(sm, data);
337 	else
338 		res = eap_fast_derive_key_auth(sm, data);
339 	return res;
340 }
341 
342 
eap_fast_init_phase2_method(struct eap_sm * sm,struct eap_fast_data * data)343 static int eap_fast_init_phase2_method(struct eap_sm *sm,
344 				       struct eap_fast_data *data)
345 {
346 	data->phase2_method =
347 		eap_peer_get_eap_method(data->phase2_type.vendor,
348 					data->phase2_type.method);
349 	if (data->phase2_method == NULL)
350 		return -1;
351 
352 	if (data->key_block_p) {
353 		sm->auth_challenge = data->key_block_p->server_challenge;
354 		sm->peer_challenge = data->key_block_p->client_challenge;
355 	}
356 	sm->init_phase2 = 1;
357 	data->phase2_priv = data->phase2_method->init(sm);
358 	sm->init_phase2 = 0;
359 	sm->auth_challenge = NULL;
360 	sm->peer_challenge = NULL;
361 
362 	return data->phase2_priv == NULL ? -1 : 0;
363 }
364 
365 
eap_fast_select_phase2_method(struct eap_fast_data * data,u8 type)366 static int eap_fast_select_phase2_method(struct eap_fast_data *data, u8 type)
367 {
368 	size_t i;
369 
370 	/* TODO: TNC with anonymous provisioning; need to require both
371 	 * completed MSCHAPv2 and TNC */
372 
373 	if (data->anon_provisioning && type != EAP_TYPE_MSCHAPV2) {
374 		wpa_printf(MSG_INFO, "EAP-FAST: Only EAP-MSCHAPv2 is allowed "
375 			   "during unauthenticated provisioning; reject phase2"
376 			   " type %d", type);
377 		return -1;
378 	}
379 
380 #ifdef EAP_TNC
381 	if (type == EAP_TYPE_TNC) {
382 		data->phase2_type.vendor = EAP_VENDOR_IETF;
383 		data->phase2_type.method = EAP_TYPE_TNC;
384 		wpa_printf(MSG_DEBUG, "EAP-FAST: Selected Phase 2 EAP "
385 			   "vendor %d method %d for TNC",
386 			   data->phase2_type.vendor,
387 			   data->phase2_type.method);
388 		return 0;
389 	}
390 #endif /* EAP_TNC */
391 
392 	for (i = 0; i < data->num_phase2_types; i++) {
393 		if (data->phase2_types[i].vendor != EAP_VENDOR_IETF ||
394 		    data->phase2_types[i].method != type)
395 			continue;
396 
397 		data->phase2_type.vendor = data->phase2_types[i].vendor;
398 		data->phase2_type.method = data->phase2_types[i].method;
399 		wpa_printf(MSG_DEBUG, "EAP-FAST: Selected Phase 2 EAP "
400 			   "vendor %d method %d",
401 			   data->phase2_type.vendor,
402 			   data->phase2_type.method);
403 		break;
404 	}
405 
406 	if (type != data->phase2_type.method || type == EAP_TYPE_NONE)
407 		return -1;
408 
409 	return 0;
410 }
411 
412 
eap_fast_phase2_request(struct eap_sm * sm,struct eap_fast_data * data,struct eap_method_ret * ret,struct eap_hdr * hdr,struct wpabuf ** resp)413 static int eap_fast_phase2_request(struct eap_sm *sm,
414 				   struct eap_fast_data *data,
415 				   struct eap_method_ret *ret,
416 				   struct eap_hdr *hdr,
417 				   struct wpabuf **resp)
418 {
419 	size_t len = be_to_host16(hdr->length);
420 	u8 *pos;
421 	struct eap_method_ret iret;
422 	struct eap_peer_config *config = eap_get_config(sm);
423 	struct wpabuf msg;
424 
425 	if (len <= sizeof(struct eap_hdr)) {
426 		wpa_printf(MSG_INFO, "EAP-FAST: too short "
427 			   "Phase 2 request (len=%lu)", (unsigned long) len);
428 		return -1;
429 	}
430 	pos = (u8 *) (hdr + 1);
431 	wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 Request: type=%d", *pos);
432 	if (*pos == EAP_TYPE_IDENTITY) {
433 		*resp = eap_sm_buildIdentity(sm, hdr->identifier, 1);
434 		return 0;
435 	}
436 
437 	if (data->phase2_priv && data->phase2_method &&
438 	    *pos != data->phase2_type.method) {
439 		wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 EAP sequence - "
440 			   "deinitialize previous method");
441 		data->phase2_method->deinit(sm, data->phase2_priv);
442 		data->phase2_method = NULL;
443 		data->phase2_priv = NULL;
444 		data->phase2_type.vendor = EAP_VENDOR_IETF;
445 		data->phase2_type.method = EAP_TYPE_NONE;
446 	}
447 
448 	if (data->phase2_type.vendor == EAP_VENDOR_IETF &&
449 	    data->phase2_type.method == EAP_TYPE_NONE &&
450 	    eap_fast_select_phase2_method(data, *pos) < 0) {
451 		if (eap_peer_tls_phase2_nak(data->phase2_types,
452 					    data->num_phase2_types,
453 					    hdr, resp))
454 			return -1;
455 		return 0;
456 	}
457 
458 	if ((data->phase2_priv == NULL &&
459 	     eap_fast_init_phase2_method(sm, data) < 0) ||
460 	    data->phase2_method == NULL) {
461 		wpa_printf(MSG_INFO, "EAP-FAST: Failed to initialize "
462 			   "Phase 2 EAP method %d", *pos);
463 		ret->methodState = METHOD_DONE;
464 		ret->decision = DECISION_FAIL;
465 		return -1;
466 	}
467 
468 	os_memset(&iret, 0, sizeof(iret));
469 	wpabuf_set(&msg, hdr, len);
470 	*resp = data->phase2_method->process(sm, data->phase2_priv, &iret,
471 					     &msg);
472 	if (*resp == NULL ||
473 	    (iret.methodState == METHOD_DONE &&
474 	     iret.decision == DECISION_FAIL)) {
475 		ret->methodState = METHOD_DONE;
476 		ret->decision = DECISION_FAIL;
477 	} else if ((iret.methodState == METHOD_DONE ||
478 		    iret.methodState == METHOD_MAY_CONT) &&
479 		   (iret.decision == DECISION_UNCOND_SUCC ||
480 		    iret.decision == DECISION_COND_SUCC)) {
481 		data->phase2_success = 1;
482 	}
483 
484 	if (*resp == NULL && config &&
485 	    (config->pending_req_identity || config->pending_req_password ||
486 	     config->pending_req_otp || config->pending_req_new_password ||
487 	     config->pending_req_sim)) {
488 		wpabuf_clear_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 
eap_fast_tlv_nak(int vendor_id,int tlv_type)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 
eap_fast_tlv_result(int status,int intermediate)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 
eap_fast_tlv_pac_ack(void)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 
eap_fast_process_eap_payload_tlv(struct eap_sm * sm,struct eap_fast_data * data,struct eap_method_ret * ret,u8 * eap_payload_tlv,size_t eap_payload_tlv_len)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 
eap_fast_validate_crypto_binding(struct eap_tlv_crypto_binding_tlv * _bind)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 
eap_fast_write_crypto_binding(struct eap_tlv_crypto_binding_tlv * rbind,struct eap_tlv_crypto_binding_tlv * _bind,const u8 * cmk)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 
eap_fast_get_phase2_key(struct eap_sm * sm,struct eap_fast_data * data,u8 * isk,size_t isk_len)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 
eap_fast_get_cmk(struct eap_sm * sm,struct eap_fast_data * data,u8 * cmk)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 
eap_fast_write_pac_request(u8 * pos,u16 pac_type)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 
eap_fast_process_crypto_binding(struct eap_sm * sm,struct eap_fast_data * data,struct eap_method_ret * ret,struct eap_tlv_crypto_binding_tlv * _bind,size_t bind_len)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_DEBUG, "EAP-FAST: Received Compound MAC",
778 		    cmac, sizeof(cmac));
779 	wpa_hexdump(MSG_DEBUG, "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 
eap_fast_parse_pac_tlv(struct eap_fast_pac * entry,int type,u8 * pos,size_t len,int * pac_key_found)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 
eap_fast_process_pac_tlv(struct eap_fast_pac * entry,u8 * pac,size_t pac_len)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 
eap_fast_parse_pac_info(struct eap_fast_pac * entry,int type,u8 * pos,size_t len)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 
908 	switch (type & 0x7fff) {
909 	case PAC_TYPE_CRED_LIFETIME:
910 		if (len != 4) {
911 			wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Info - "
912 				    "Invalid CRED_LIFETIME length - ignored",
913 				    pos, len);
914 			return 0;
915 		}
916 		break;
917 	case PAC_TYPE_A_ID:
918 		wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - A-ID",
919 				  pos, len);
920 		entry->a_id = pos;
921 		entry->a_id_len = len;
922 		break;
923 	case PAC_TYPE_I_ID:
924 		wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - I-ID",
925 				  pos, len);
926 		entry->i_id = pos;
927 		entry->i_id_len = len;
928 		break;
929 	case PAC_TYPE_A_ID_INFO:
930 		wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - A-ID-Info",
931 				  pos, len);
932 		entry->a_id_info = pos;
933 		entry->a_id_info_len = len;
934 		break;
935 	case PAC_TYPE_PAC_TYPE:
936 		/* RFC 5422, Section 4.2.6 - PAC-Type TLV */
937 		if (len != 2) {
938 			wpa_printf(MSG_INFO, "EAP-FAST: Invalid PAC-Type "
939 				   "length %lu (expected 2)",
940 				   (unsigned long) len);
941 			wpa_hexdump_ascii(MSG_DEBUG,
942 					  "EAP-FAST: PAC-Info - PAC-Type",
943 					  pos, len);
944 			return -1;
945 		}
946 		pac_type = WPA_GET_BE16(pos);
947 		if (pac_type != PAC_TYPE_TUNNEL_PAC &&
948 		    pac_type != PAC_TYPE_USER_AUTHORIZATION &&
949 		    pac_type != PAC_TYPE_MACHINE_AUTHENTICATION) {
950 			wpa_printf(MSG_INFO, "EAP-FAST: Unsupported PAC Type "
951 				   "%d", pac_type);
952 			return -1;
953 		}
954 
955 		wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info - PAC-Type %d",
956 			   pac_type);
957 		entry->pac_type = pac_type;
958 		break;
959 	default:
960 		wpa_printf(MSG_DEBUG, "EAP-FAST: Ignored unknown PAC-Info "
961 			   "type %d", type);
962 		break;
963 	}
964 
965 	return 0;
966 }
967 
968 
eap_fast_process_pac_info(struct eap_fast_pac * entry)969 static int eap_fast_process_pac_info(struct eap_fast_pac *entry)
970 {
971 	struct pac_tlv_hdr *hdr;
972 	u8 *pos;
973 	size_t left, len;
974 	int type;
975 
976 	/* RFC 5422, Section 4.2.4 */
977 
978 	/* PAC-Type defaults to Tunnel PAC (Type 1) */
979 	entry->pac_type = PAC_TYPE_TUNNEL_PAC;
980 
981 	pos = entry->pac_info;
982 	left = entry->pac_info_len;
983 	while (left > sizeof(*hdr)) {
984 		hdr = (struct pac_tlv_hdr *) pos;
985 		type = be_to_host16(hdr->type);
986 		len = be_to_host16(hdr->len);
987 		pos += sizeof(*hdr);
988 		left -= sizeof(*hdr);
989 		if (len > left) {
990 			wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info overrun "
991 				   "(type=%d len=%lu left=%lu)",
992 				   type, (unsigned long) len,
993 				   (unsigned long) left);
994 			return -1;
995 		}
996 
997 		if (eap_fast_parse_pac_info(entry, type, pos, len) < 0)
998 			return -1;
999 
1000 		pos += len;
1001 		left -= len;
1002 	}
1003 
1004 	if (entry->a_id == NULL || entry->a_id_info == NULL) {
1005 		wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info does not include "
1006 			   "all the required fields");
1007 		return -1;
1008 	}
1009 
1010 	return 0;
1011 }
1012 
1013 
eap_fast_process_pac(struct eap_sm * sm,struct eap_fast_data * data,struct eap_method_ret * ret,u8 * pac,size_t pac_len)1014 static struct wpabuf * eap_fast_process_pac(struct eap_sm *sm,
1015 					    struct eap_fast_data *data,
1016 					    struct eap_method_ret *ret,
1017 					    u8 *pac, size_t pac_len)
1018 {
1019 	struct eap_peer_config *config = eap_get_config(sm);
1020 	struct eap_fast_pac entry;
1021 
1022 	os_memset(&entry, 0, sizeof(entry));
1023 	if (eap_fast_process_pac_tlv(&entry, pac, pac_len) ||
1024 	    eap_fast_process_pac_info(&entry))
1025 		return NULL;
1026 
1027 	eap_fast_add_pac(&data->pac, &data->current_pac, &entry);
1028 	eap_fast_pac_list_truncate(data->pac, data->max_pac_list_len);
1029 	if (data->use_pac_binary_format)
1030 		eap_fast_save_pac_bin(sm, data->pac, config->pac_file);
1031 	else
1032 		eap_fast_save_pac(sm, data->pac, config->pac_file);
1033 
1034 	if (data->provisioning) {
1035 		if (data->anon_provisioning) {
1036 			/*
1037 			 * Unauthenticated provisioning does not provide keying
1038 			 * material and must end with an EAP-Failure.
1039 			 * Authentication will be done separately after this.
1040 			 */
1041 			data->success = 0;
1042 			ret->decision = DECISION_FAIL;
1043 		} else {
1044 			/*
1045 			 * Server may or may not allow authenticated
1046 			 * provisioning also for key generation.
1047 			 */
1048 			ret->decision = DECISION_COND_SUCC;
1049 		}
1050 		wpa_printf(MSG_DEBUG, "EAP-FAST: Send PAC-Acknowledgement TLV "
1051 			   "- Provisioning completed successfully");
1052 		sm->expected_failure = 1;
1053 	} else {
1054 		/*
1055 		 * This is PAC refreshing, i.e., normal authentication that is
1056 		 * expected to be completed with an EAP-Success. However,
1057 		 * RFC 5422, Section 3.5 allows EAP-Failure to be sent even
1058 		 * after protected success exchange in case of EAP-Fast
1059 		 * provisioning, so we better use DECISION_COND_SUCC here
1060 		 * instead of DECISION_UNCOND_SUCC.
1061 		 */
1062 		wpa_printf(MSG_DEBUG, "EAP-FAST: Send PAC-Acknowledgement TLV "
1063 			   "- PAC refreshing completed successfully");
1064 		ret->decision = DECISION_COND_SUCC;
1065 	}
1066 	ret->methodState = METHOD_DONE;
1067 	return eap_fast_tlv_pac_ack();
1068 }
1069 
1070 
eap_fast_parse_decrypted(struct wpabuf * decrypted,struct eap_fast_tlv_parse * tlv,struct wpabuf ** resp)1071 static int eap_fast_parse_decrypted(struct wpabuf *decrypted,
1072 				    struct eap_fast_tlv_parse *tlv,
1073 				    struct wpabuf **resp)
1074 {
1075 	int mandatory, tlv_type, res;
1076 	size_t len;
1077 	u8 *pos, *end;
1078 
1079 	os_memset(tlv, 0, sizeof(*tlv));
1080 
1081 	/* Parse TLVs from the decrypted Phase 2 data */
1082 	pos = wpabuf_mhead(decrypted);
1083 	end = pos + wpabuf_len(decrypted);
1084 	while (end - pos > 4) {
1085 		mandatory = pos[0] & 0x80;
1086 		tlv_type = WPA_GET_BE16(pos) & 0x3fff;
1087 		pos += 2;
1088 		len = WPA_GET_BE16(pos);
1089 		pos += 2;
1090 		if (len > (size_t) (end - pos)) {
1091 			wpa_printf(MSG_INFO, "EAP-FAST: TLV overflow");
1092 			return -1;
1093 		}
1094 		wpa_printf(MSG_DEBUG, "EAP-FAST: Received Phase 2: "
1095 			   "TLV type %d length %u%s",
1096 			   tlv_type, (unsigned int) len,
1097 			   mandatory ? " (mandatory)" : "");
1098 
1099 		res = eap_fast_parse_tlv(tlv, tlv_type, pos, len);
1100 		if (res == -2)
1101 			break;
1102 		if (res < 0) {
1103 			if (mandatory) {
1104 				wpa_printf(MSG_DEBUG, "EAP-FAST: Nak unknown "
1105 					   "mandatory TLV type %d", tlv_type);
1106 				*resp = eap_fast_tlv_nak(0, tlv_type);
1107 				break;
1108 			} else {
1109 				wpa_printf(MSG_DEBUG, "EAP-FAST: ignored "
1110 					   "unknown optional TLV type %d",
1111 					   tlv_type);
1112 			}
1113 		}
1114 
1115 		pos += len;
1116 	}
1117 
1118 	return 0;
1119 }
1120 
1121 
eap_fast_encrypt_response(struct eap_sm * sm,struct eap_fast_data * data,struct wpabuf * resp,u8 identifier,struct wpabuf ** out_data)1122 static int eap_fast_encrypt_response(struct eap_sm *sm,
1123 				     struct eap_fast_data *data,
1124 				     struct wpabuf *resp,
1125 				     u8 identifier, struct wpabuf **out_data)
1126 {
1127 	if (resp == NULL)
1128 		return 0;
1129 
1130 	wpa_hexdump_buf(MSG_DEBUG, "EAP-FAST: Encrypting Phase 2 data",
1131 			resp);
1132 	if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_FAST,
1133 				 data->fast_version, identifier,
1134 				 resp, out_data)) {
1135 		wpa_printf(MSG_INFO, "EAP-FAST: Failed to encrypt a Phase 2 "
1136 			   "frame");
1137 	}
1138 	wpabuf_free(resp);
1139 
1140 	return 0;
1141 }
1142 
1143 
eap_fast_pac_request(void)1144 static struct wpabuf * eap_fast_pac_request(void)
1145 {
1146 	struct wpabuf *tmp;
1147 	u8 *pos, *pos2;
1148 
1149 	tmp = wpabuf_alloc(sizeof(struct eap_tlv_hdr) +
1150 			   sizeof(struct eap_tlv_request_action_tlv) +
1151 			   sizeof(struct eap_tlv_pac_type_tlv));
1152 	if (tmp == NULL)
1153 		return NULL;
1154 
1155 	pos = wpabuf_put(tmp, 0);
1156 	pos2 = eap_fast_write_pac_request(pos, PAC_TYPE_TUNNEL_PAC);
1157 	wpabuf_put(tmp, pos2 - pos);
1158 	return tmp;
1159 }
1160 
1161 
eap_fast_process_decrypted(struct eap_sm * sm,struct eap_fast_data * data,struct eap_method_ret * ret,u8 identifier,struct wpabuf * decrypted,struct wpabuf ** out_data)1162 static int eap_fast_process_decrypted(struct eap_sm *sm,
1163 				      struct eap_fast_data *data,
1164 				      struct eap_method_ret *ret,
1165 				      u8 identifier,
1166 				      struct wpabuf *decrypted,
1167 				      struct wpabuf **out_data)
1168 {
1169 	struct wpabuf *resp = NULL, *tmp;
1170 	struct eap_fast_tlv_parse tlv;
1171 	int failed = 0;
1172 
1173 	if (eap_fast_parse_decrypted(decrypted, &tlv, &resp) < 0)
1174 		return 0;
1175 	if (resp)
1176 		return eap_fast_encrypt_response(sm, data, resp,
1177 						 identifier, out_data);
1178 
1179 	if (tlv.result == EAP_TLV_RESULT_FAILURE) {
1180 		resp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 0);
1181 		return eap_fast_encrypt_response(sm, data, resp,
1182 						 identifier, out_data);
1183 	}
1184 
1185 	if (tlv.iresult == EAP_TLV_RESULT_FAILURE) {
1186 		resp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 1);
1187 		return eap_fast_encrypt_response(sm, data, resp,
1188 						 identifier, out_data);
1189 	}
1190 
1191 	if (tlv.crypto_binding) {
1192 		tmp = eap_fast_process_crypto_binding(sm, data, ret,
1193 						      tlv.crypto_binding,
1194 						      tlv.crypto_binding_len);
1195 		if (tmp == NULL)
1196 			failed = 1;
1197 		else
1198 			resp = wpabuf_concat(resp, tmp);
1199 	}
1200 
1201 	if (tlv.iresult == EAP_TLV_RESULT_SUCCESS) {
1202 		tmp = eap_fast_tlv_result(failed ? EAP_TLV_RESULT_FAILURE :
1203 					  EAP_TLV_RESULT_SUCCESS, 1);
1204 		resp = wpabuf_concat(resp, tmp);
1205 	}
1206 
1207 	if (tlv.eap_payload_tlv) {
1208 		tmp = eap_fast_process_eap_payload_tlv(
1209 			sm, data, ret, tlv.eap_payload_tlv,
1210 			tlv.eap_payload_tlv_len);
1211 		resp = wpabuf_concat(resp, tmp);
1212 	}
1213 
1214 	if (tlv.pac && tlv.result != EAP_TLV_RESULT_SUCCESS) {
1215 		wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV without Result TLV "
1216 			   "acknowledging success");
1217 		failed = 1;
1218 	} else if (tlv.pac && tlv.result == EAP_TLV_RESULT_SUCCESS) {
1219 		tmp = eap_fast_process_pac(sm, data, ret, tlv.pac,
1220 					   tlv.pac_len);
1221 		resp = wpabuf_concat(resp, tmp);
1222 	}
1223 
1224 	if (data->current_pac == NULL && data->provisioning &&
1225 	    !data->anon_provisioning && !tlv.pac &&
1226 	    (tlv.iresult == EAP_TLV_RESULT_SUCCESS ||
1227 	     tlv.result == EAP_TLV_RESULT_SUCCESS)) {
1228 		/*
1229 		 * Need to request Tunnel PAC when using authenticated
1230 		 * provisioning.
1231 		 */
1232 		wpa_printf(MSG_DEBUG, "EAP-FAST: Request Tunnel PAC");
1233 		tmp = eap_fast_pac_request();
1234 		resp = wpabuf_concat(resp, tmp);
1235 	}
1236 
1237 	if (tlv.result == EAP_TLV_RESULT_SUCCESS && !failed) {
1238 		tmp = eap_fast_tlv_result(EAP_TLV_RESULT_SUCCESS, 0);
1239 		resp = wpabuf_concat(tmp, resp);
1240 	} else if (failed) {
1241 		tmp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 0);
1242 		resp = wpabuf_concat(tmp, resp);
1243 	}
1244 
1245 	if (resp && tlv.result == EAP_TLV_RESULT_SUCCESS && !failed &&
1246 	    tlv.crypto_binding && data->phase2_success) {
1247 		if (data->anon_provisioning) {
1248 			wpa_printf(MSG_DEBUG, "EAP-FAST: Unauthenticated "
1249 				   "provisioning completed successfully.");
1250 			ret->methodState = METHOD_DONE;
1251 			ret->decision = DECISION_FAIL;
1252 			sm->expected_failure = 1;
1253 		} else {
1254 			wpa_printf(MSG_DEBUG, "EAP-FAST: Authentication "
1255 				   "completed successfully.");
1256 			if (data->provisioning)
1257 				ret->methodState = METHOD_MAY_CONT;
1258 			else
1259 				ret->methodState = METHOD_DONE;
1260 			ret->decision = DECISION_UNCOND_SUCC;
1261 		}
1262 	}
1263 
1264 	if (resp == NULL) {
1265 		wpa_printf(MSG_DEBUG, "EAP-FAST: No recognized TLVs - send "
1266 			   "empty response packet");
1267 		resp = wpabuf_alloc(1);
1268 	}
1269 
1270 	return eap_fast_encrypt_response(sm, data, resp, identifier,
1271 					 out_data);
1272 }
1273 
1274 
eap_fast_decrypt(struct eap_sm * sm,struct eap_fast_data * data,struct eap_method_ret * ret,u8 identifier,const struct wpabuf * in_data,struct wpabuf ** out_data)1275 static int eap_fast_decrypt(struct eap_sm *sm, struct eap_fast_data *data,
1276 			    struct eap_method_ret *ret, u8 identifier,
1277 			    const struct wpabuf *in_data,
1278 			    struct wpabuf **out_data)
1279 {
1280 	struct wpabuf *in_decrypted;
1281 	int res;
1282 
1283 	wpa_printf(MSG_DEBUG, "EAP-FAST: Received %lu bytes encrypted data for"
1284 		   " Phase 2", (unsigned long) wpabuf_len(in_data));
1285 
1286 	if (data->pending_phase2_req) {
1287 		wpa_printf(MSG_DEBUG, "EAP-FAST: Pending Phase 2 request - "
1288 			   "skip decryption and use old data");
1289 		/* Clear TLS reassembly state. */
1290 		eap_peer_tls_reset_input(&data->ssl);
1291 
1292 		in_decrypted = data->pending_phase2_req;
1293 		data->pending_phase2_req = NULL;
1294 		goto continue_req;
1295 	}
1296 
1297 	if (wpabuf_len(in_data) == 0) {
1298 		/* Received TLS ACK - requesting more fragments */
1299 		return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_FAST,
1300 					    data->fast_version,
1301 					    identifier, NULL, out_data);
1302 	}
1303 
1304 	res = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
1305 	if (res)
1306 		return res;
1307 
1308 continue_req:
1309 	wpa_hexdump_buf(MSG_MSGDUMP, "EAP-FAST: Decrypted Phase 2 TLV(s)",
1310 			in_decrypted);
1311 
1312 	if (wpabuf_len(in_decrypted) < 4) {
1313 		wpa_printf(MSG_INFO, "EAP-FAST: Too short Phase 2 "
1314 			   "TLV frame (len=%lu)",
1315 			   (unsigned long) wpabuf_len(in_decrypted));
1316 		wpabuf_free(in_decrypted);
1317 		return -1;
1318 	}
1319 
1320 	res = eap_fast_process_decrypted(sm, data, ret, identifier,
1321 					 in_decrypted, out_data);
1322 
1323 	wpabuf_free(in_decrypted);
1324 
1325 	return res;
1326 }
1327 
1328 
eap_fast_get_a_id(const u8 * buf,size_t len,size_t * id_len)1329 static const u8 * eap_fast_get_a_id(const u8 *buf, size_t len, size_t *id_len)
1330 {
1331 	const u8 *a_id;
1332 	const struct pac_tlv_hdr *hdr;
1333 
1334 	/*
1335 	 * Parse authority identity (A-ID) from the EAP-FAST/Start. This
1336 	 * supports both raw A-ID and one inside an A-ID TLV.
1337 	 */
1338 	a_id = buf;
1339 	*id_len = len;
1340 	if (len > sizeof(*hdr)) {
1341 		int tlen;
1342 		hdr = (const struct pac_tlv_hdr *) buf;
1343 		tlen = be_to_host16(hdr->len);
1344 		if (be_to_host16(hdr->type) == PAC_TYPE_A_ID &&
1345 		    sizeof(*hdr) + tlen <= len) {
1346 			wpa_printf(MSG_DEBUG, "EAP-FAST: A-ID was in TLV "
1347 				   "(Start)");
1348 			a_id = (const u8 *) (hdr + 1);
1349 			*id_len = tlen;
1350 		}
1351 	}
1352 	wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: A-ID", a_id, *id_len);
1353 
1354 	return a_id;
1355 }
1356 
1357 
eap_fast_select_pac(struct eap_fast_data * data,const u8 * a_id,size_t a_id_len)1358 static void eap_fast_select_pac(struct eap_fast_data *data,
1359 				const u8 *a_id, size_t a_id_len)
1360 {
1361 	data->current_pac = eap_fast_get_pac(data->pac, a_id, a_id_len,
1362 					     PAC_TYPE_TUNNEL_PAC);
1363 	if (data->current_pac == NULL) {
1364 		/*
1365 		 * Tunnel PAC was not available for this A-ID. Try to use
1366 		 * Machine Authentication PAC, if one is available.
1367 		 */
1368 		data->current_pac = eap_fast_get_pac(
1369 			data->pac, a_id, a_id_len,
1370 			PAC_TYPE_MACHINE_AUTHENTICATION);
1371 	}
1372 
1373 	if (data->current_pac) {
1374 		wpa_printf(MSG_DEBUG, "EAP-FAST: PAC found for this A-ID "
1375 			   "(PAC-Type %d)", data->current_pac->pac_type);
1376 		wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-FAST: A-ID-Info",
1377 				  data->current_pac->a_id_info,
1378 				  data->current_pac->a_id_info_len);
1379 	}
1380 }
1381 
1382 
eap_fast_use_pac_opaque(struct eap_sm * sm,struct eap_fast_data * data,struct eap_fast_pac * pac)1383 static int eap_fast_use_pac_opaque(struct eap_sm *sm,
1384 				   struct eap_fast_data *data,
1385 				   struct eap_fast_pac *pac)
1386 {
1387 	u8 *tlv;
1388 	size_t tlv_len, olen;
1389 	struct eap_tlv_hdr *ehdr;
1390 
1391 	olen = pac->pac_opaque_len;
1392 	tlv_len = sizeof(*ehdr) + olen;
1393 	tlv = os_malloc(tlv_len);
1394 	if (tlv) {
1395 		ehdr = (struct eap_tlv_hdr *) tlv;
1396 		ehdr->tlv_type = host_to_be16(PAC_TYPE_PAC_OPAQUE);
1397 		ehdr->length = host_to_be16(olen);
1398 		os_memcpy(ehdr + 1, pac->pac_opaque, olen);
1399 	}
1400 	if (tlv == NULL ||
1401 	    tls_connection_client_hello_ext(sm->ssl_ctx, data->ssl.conn,
1402 					    TLS_EXT_PAC_OPAQUE,
1403 					    tlv, tlv_len) < 0) {
1404 		wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to add PAC-Opaque TLS "
1405 			   "extension");
1406 		os_free(tlv);
1407 		return -1;
1408 	}
1409 	os_free(tlv);
1410 
1411 	return 0;
1412 }
1413 
1414 
eap_fast_clear_pac_opaque_ext(struct eap_sm * sm,struct eap_fast_data * data)1415 static int eap_fast_clear_pac_opaque_ext(struct eap_sm *sm,
1416 					 struct eap_fast_data *data)
1417 {
1418 	if (tls_connection_client_hello_ext(sm->ssl_ctx, data->ssl.conn,
1419 					    TLS_EXT_PAC_OPAQUE, NULL, 0) < 0) {
1420 		wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to remove PAC-Opaque "
1421 			   "TLS extension");
1422 		return -1;
1423 	}
1424 	return 0;
1425 }
1426 
1427 
eap_fast_set_provisioning_ciphers(struct eap_sm * sm,struct eap_fast_data * data)1428 static int eap_fast_set_provisioning_ciphers(struct eap_sm *sm,
1429 					     struct eap_fast_data *data)
1430 {
1431 	u8 ciphers[7];
1432 	int count = 0;
1433 
1434 	if (data->provisioning_allowed & EAP_FAST_PROV_UNAUTH) {
1435 		wpa_printf(MSG_DEBUG, "EAP-FAST: Enabling unauthenticated "
1436 			   "provisioning TLS cipher suites");
1437 		ciphers[count++] = TLS_CIPHER_ANON_DH_AES128_SHA;
1438 	}
1439 
1440 	if (data->provisioning_allowed & EAP_FAST_PROV_AUTH) {
1441 		wpa_printf(MSG_DEBUG, "EAP-FAST: Enabling authenticated "
1442 			   "provisioning TLS cipher suites");
1443 		ciphers[count++] = TLS_CIPHER_RSA_DHE_AES256_SHA;
1444 		ciphers[count++] = TLS_CIPHER_RSA_DHE_AES128_SHA;
1445 		ciphers[count++] = TLS_CIPHER_AES256_SHA;
1446 		ciphers[count++] = TLS_CIPHER_AES128_SHA;
1447 		ciphers[count++] = TLS_CIPHER_RC4_SHA;
1448 	}
1449 
1450 	ciphers[count++] = TLS_CIPHER_NONE;
1451 
1452 	if (tls_connection_set_cipher_list(sm->ssl_ctx, data->ssl.conn,
1453 					   ciphers)) {
1454 		wpa_printf(MSG_INFO, "EAP-FAST: Could not configure TLS "
1455 			   "cipher suites for provisioning");
1456 		return -1;
1457 	}
1458 
1459 	return 0;
1460 }
1461 
1462 
eap_fast_process_start(struct eap_sm * sm,struct eap_fast_data * data,u8 flags,const u8 * pos,size_t left)1463 static int eap_fast_process_start(struct eap_sm *sm,
1464 				  struct eap_fast_data *data, u8 flags,
1465 				  const u8 *pos, size_t left)
1466 {
1467 	const u8 *a_id;
1468 	size_t a_id_len;
1469 
1470 	/* EAP-FAST Version negotiation (section 3.1) */
1471 	wpa_printf(MSG_DEBUG, "EAP-FAST: Start (server ver=%d, own ver=%d)",
1472 		   flags & EAP_TLS_VERSION_MASK, data->fast_version);
1473 	if ((flags & EAP_TLS_VERSION_MASK) < data->fast_version)
1474 		data->fast_version = flags & EAP_TLS_VERSION_MASK;
1475 	wpa_printf(MSG_DEBUG, "EAP-FAST: Using FAST version %d",
1476 		   data->fast_version);
1477 
1478 	a_id = eap_fast_get_a_id(pos, left, &a_id_len);
1479 	eap_fast_select_pac(data, a_id, a_id_len);
1480 
1481 	if (data->resuming && data->current_pac) {
1482 		wpa_printf(MSG_DEBUG, "EAP-FAST: Trying to resume session - "
1483 			   "do not add PAC-Opaque to TLS ClientHello");
1484 		if (eap_fast_clear_pac_opaque_ext(sm, data) < 0)
1485 			return -1;
1486 	} else if (data->current_pac) {
1487 		/*
1488 		 * PAC found for the A-ID and we are not resuming an old
1489 		 * session, so add PAC-Opaque extension to ClientHello.
1490 		 */
1491 		if (eap_fast_use_pac_opaque(sm, data, data->current_pac) < 0)
1492 			return -1;
1493 	} else {
1494 		/* No PAC found, so we must provision one. */
1495 		if (!data->provisioning_allowed) {
1496 			wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC found and "
1497 				   "provisioning disabled");
1498 			return -1;
1499 		}
1500 		wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC found - "
1501 			   "starting provisioning");
1502 		if (eap_fast_set_provisioning_ciphers(sm, data) < 0 ||
1503 		    eap_fast_clear_pac_opaque_ext(sm, data) < 0)
1504 			return -1;
1505 		data->provisioning = 1;
1506 	}
1507 
1508 	return 0;
1509 }
1510 
1511 
eap_fast_process(struct eap_sm * sm,void * priv,struct eap_method_ret * ret,const struct wpabuf * reqData)1512 static struct wpabuf * eap_fast_process(struct eap_sm *sm, void *priv,
1513 					struct eap_method_ret *ret,
1514 					const struct wpabuf *reqData)
1515 {
1516 	const struct eap_hdr *req;
1517 	size_t left;
1518 	int res;
1519 	u8 flags, id;
1520 	struct wpabuf *resp;
1521 	const u8 *pos;
1522 	struct eap_fast_data *data = priv;
1523 	struct wpabuf msg;
1524 
1525 	pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_FAST, ret,
1526 					reqData, &left, &flags);
1527 	if (pos == NULL)
1528 		return NULL;
1529 
1530 	req = wpabuf_head(reqData);
1531 	id = req->identifier;
1532 
1533 	if (flags & EAP_TLS_FLAGS_START) {
1534 		if (eap_fast_process_start(sm, data, flags, pos, left) < 0)
1535 			return NULL;
1536 
1537 		left = 0; /* A-ID is not used in further packet processing */
1538 	}
1539 
1540 	wpabuf_set(&msg, pos, left);
1541 
1542 	resp = NULL;
1543 	if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1544 	    !data->resuming) {
1545 		/* Process tunneled (encrypted) phase 2 data. */
1546 		res = eap_fast_decrypt(sm, data, ret, id, &msg, &resp);
1547 		if (res < 0) {
1548 			ret->methodState = METHOD_DONE;
1549 			ret->decision = DECISION_FAIL;
1550 			/*
1551 			 * Ack possible Alert that may have caused failure in
1552 			 * decryption.
1553 			 */
1554 			res = 1;
1555 		}
1556 	} else {
1557 		if (sm->waiting_ext_cert_check && data->pending_resp) {
1558 			struct eap_peer_config *config = eap_get_config(sm);
1559 
1560 			if (config->pending_ext_cert_check ==
1561 			    EXT_CERT_CHECK_GOOD) {
1562 				wpa_printf(MSG_DEBUG,
1563 					   "EAP-FAST: External certificate check succeeded - continue handshake");
1564 				resp = data->pending_resp;
1565 				data->pending_resp = NULL;
1566 				sm->waiting_ext_cert_check = 0;
1567 				return resp;
1568 			}
1569 
1570 			if (config->pending_ext_cert_check ==
1571 			    EXT_CERT_CHECK_BAD) {
1572 				wpa_printf(MSG_DEBUG,
1573 					   "EAP-FAST: External certificate check failed - force authentication failure");
1574 				ret->methodState = METHOD_DONE;
1575 				ret->decision = DECISION_FAIL;
1576 				sm->waiting_ext_cert_check = 0;
1577 				return NULL;
1578 			}
1579 
1580 			wpa_printf(MSG_DEBUG,
1581 				   "EAP-FAST: Continuing to wait external server certificate validation");
1582 			return NULL;
1583 		}
1584 
1585 		/* Continue processing TLS handshake (phase 1). */
1586 		res = eap_peer_tls_process_helper(sm, &data->ssl,
1587 						  EAP_TYPE_FAST,
1588 						  data->fast_version, id, pos,
1589 						  left, &resp);
1590 		if (res < 0) {
1591 			wpa_printf(MSG_DEBUG,
1592 				   "EAP-FAST: TLS processing failed");
1593 			ret->methodState = METHOD_DONE;
1594 			ret->decision = DECISION_FAIL;
1595 			return resp;
1596 		}
1597 
1598 		if (sm->waiting_ext_cert_check) {
1599 			wpa_printf(MSG_DEBUG,
1600 				   "EAP-FAST: Waiting external server certificate validation");
1601 			wpabuf_free(data->pending_resp);
1602 			data->pending_resp = resp;
1603 			return NULL;
1604 		}
1605 
1606 		if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
1607 			char cipher[80];
1608 			wpa_printf(MSG_DEBUG,
1609 				   "EAP-FAST: TLS done, proceed to Phase 2");
1610 			if (data->provisioning &&
1611 			    (!(data->provisioning_allowed &
1612 			       EAP_FAST_PROV_AUTH) ||
1613 			     tls_get_cipher(sm->ssl_ctx, data->ssl.conn,
1614 					    cipher, sizeof(cipher)) < 0 ||
1615 			     os_strstr(cipher, "ADH-") ||
1616 			     os_strstr(cipher, "anon"))) {
1617 				wpa_printf(MSG_DEBUG, "EAP-FAST: Using "
1618 					   "anonymous (unauthenticated) "
1619 					   "provisioning");
1620 				data->anon_provisioning = 1;
1621 			} else {
1622 				data->anon_provisioning = 0;
1623             }
1624 			data->resuming = 0;
1625 			if (eap_fast_derive_keys(sm, data) < 0) {
1626 				wpa_printf(MSG_DEBUG,
1627 					   "EAP-FAST: Could not derive keys");
1628 				ret->methodState = METHOD_DONE;
1629 				ret->decision = DECISION_FAIL;
1630 				wpabuf_free(resp);
1631 				return NULL;
1632 			}
1633 		}
1634 
1635 		if (res == 2) {
1636 			/*
1637 			 * Application data included in the handshake message.
1638 			 */
1639 			wpabuf_free(data->pending_phase2_req);
1640 			data->pending_phase2_req = resp;
1641 			resp = NULL;
1642 			res = eap_fast_decrypt(sm, data, ret, id, &msg, &resp);
1643 		}
1644 	}
1645 
1646 	if (res == 1) {
1647 		wpabuf_free(resp);
1648 		return eap_peer_tls_build_ack(id, EAP_TYPE_FAST,
1649 					      data->fast_version);
1650 	}
1651 
1652 	return resp;
1653 }
1654 
1655 
1656 #if 0 /* FIX */
1657 static Boolean eap_fast_has_reauth_data(struct eap_sm *sm, void *priv)
1658 {
1659 	struct eap_fast_data *data = priv;
1660 	return tls_connection_established(sm->ssl_ctx, data->ssl.conn);
1661 }
1662 
1663 
1664 static void eap_fast_deinit_for_reauth(struct eap_sm *sm, void *priv)
1665 {
1666 	struct eap_fast_data *data = priv;
1667 
1668 	if (data->phase2_priv && data->phase2_method &&
1669 	    data->phase2_method->deinit_for_reauth)
1670 		data->phase2_method->deinit_for_reauth(sm, data->phase2_priv);
1671 	os_free(data->key_block_p);
1672 	data->key_block_p = NULL;
1673 	wpabuf_free(data->pending_phase2_req);
1674 	data->pending_phase2_req = NULL;
1675 	wpabuf_free(data->pending_resp);
1676 	data->pending_resp = NULL;
1677 }
1678 
1679 
1680 static void * eap_fast_init_for_reauth(struct eap_sm *sm, void *priv)
1681 {
1682 	struct eap_fast_data *data = priv;
1683 	if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
1684 		os_free(data);
1685 		return NULL;
1686 	}
1687 	os_memset(data->key_data, 0, EAP_FAST_KEY_LEN);
1688 	os_memset(data->emsk, 0, EAP_EMSK_LEN);
1689 	os_free(data->session_id);
1690 	data->session_id = NULL;
1691 	if (data->phase2_priv && data->phase2_method &&
1692 	    data->phase2_method->init_for_reauth)
1693 		data->phase2_method->init_for_reauth(sm, data->phase2_priv);
1694 	data->phase2_success = 0;
1695 	data->resuming = 1;
1696 	data->provisioning = 0;
1697 	data->anon_provisioning = 0;
1698 	data->simck_idx = 0;
1699 	return priv;
1700 }
1701 #endif
1702 
1703 
eap_fast_get_status(struct eap_sm * sm,void * priv,char * buf,size_t buflen,int verbose)1704 static int eap_fast_get_status(struct eap_sm *sm, void *priv, char *buf,
1705 			       size_t buflen, int verbose)
1706 {
1707 	struct eap_fast_data *data = priv;
1708 	int len, ret;
1709 
1710 	len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
1711 	if (data->phase2_method) {
1712 		ret = os_snprintf(buf + len, buflen - len,
1713 				  "EAP-FAST Phase2 method=%s\n",
1714 				  data->phase2_method->name);
1715 		if (os_snprintf_error(buflen - len, ret))
1716 			return len;
1717 		len += ret;
1718 	}
1719 	return len;
1720 }
1721 
1722 
eap_fast_isKeyAvailable(struct eap_sm * sm,void * priv)1723 static bool eap_fast_isKeyAvailable(struct eap_sm *sm, void *priv)
1724 {
1725 	struct eap_fast_data *data = priv;
1726 	return data->success;
1727 }
1728 
1729 
eap_fast_getKey(struct eap_sm * sm,void * priv,size_t * len)1730 static u8 * eap_fast_getKey(struct eap_sm *sm, void *priv, size_t *len)
1731 {
1732 	struct eap_fast_data *data = priv;
1733 	u8 *key;
1734 
1735 	if (!data->success)
1736 		return NULL;
1737 
1738 	key = os_memdup(data->key_data, EAP_FAST_KEY_LEN);
1739 	if (key == NULL)
1740 		return NULL;
1741 
1742 	*len = EAP_FAST_KEY_LEN;
1743 
1744 	return key;
1745 }
1746 
1747 
eap_fast_get_session_id(struct eap_sm * sm,void * priv,size_t * len)1748 static u8 * eap_fast_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
1749 {
1750 	struct eap_fast_data *data = priv;
1751 	u8 *id;
1752 
1753 	if (!data->success || !data->session_id)
1754 		return NULL;
1755 
1756 	id = os_memdup(data->session_id, data->id_len);
1757 	if (id == NULL)
1758 		return NULL;
1759 
1760 	*len = data->id_len;
1761 
1762 	return id;
1763 }
1764 
1765 
eap_fast_get_emsk(struct eap_sm * sm,void * priv,size_t * len)1766 static u8 * eap_fast_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
1767 {
1768 	struct eap_fast_data *data = priv;
1769 	u8 *key;
1770 
1771 	if (!data->success)
1772 		return NULL;
1773 
1774 	key = os_memdup(data->emsk, EAP_EMSK_LEN);
1775 	if (key == NULL)
1776 		return NULL;
1777 
1778 	*len = EAP_EMSK_LEN;
1779 
1780 	return key;
1781 }
1782 
1783 
eap_peer_fast_register(void)1784 int eap_peer_fast_register(void)
1785 {
1786 	struct eap_method *eap;
1787 
1788 	eap = eap_peer_method_alloc(EAP_VENDOR_IETF, EAP_TYPE_FAST, "FAST");
1789 	if (eap == NULL)
1790 		return -1;
1791 
1792 	eap->init = eap_fast_init;
1793 	eap->deinit = eap_fast_deinit;
1794 	eap->process = eap_fast_process;
1795 	eap->isKeyAvailable = eap_fast_isKeyAvailable;
1796 	eap->getKey = eap_fast_getKey;
1797 	eap->getSessionId = eap_fast_get_session_id;
1798 	eap->get_status = eap_fast_get_status;
1799 #if 0
1800 	eap->has_reauth_data = eap_fast_has_reauth_data;
1801 	eap->deinit_for_reauth = eap_fast_deinit_for_reauth;
1802 	eap->init_for_reauth = eap_fast_init_for_reauth;
1803 #endif
1804 	eap->get_emsk = eap_fast_get_emsk;
1805 
1806 	return eap_peer_method_register(eap);
1807 }
1808