xref: /netbsd-src/external/bsd/wpa/dist/src/eap_peer/eap_peap.c (revision ccd9df534e375a4366c5b55f23782053c7a98d82)
1 /*
2  * EAP peer method: EAP-PEAP (draft-josefsson-pppext-eap-tls-eap-10.txt)
3  * Copyright (c) 2004-2019, 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/sha1.h"
13 #include "crypto/tls.h"
14 #include "eap_common/eap_tlv_common.h"
15 #include "eap_common/eap_peap_common.h"
16 #include "eap_i.h"
17 #include "eap_tls_common.h"
18 #include "eap_config.h"
19 #include "tncc.h"
20 
21 
22 /* Maximum supported PEAP version
23  * 0 = Microsoft's PEAP version 0; draft-kamath-pppext-peapv0-00.txt
24  * 1 = draft-josefsson-ppext-eap-tls-eap-05.txt
25  */
26 #define EAP_PEAP_VERSION 1
27 
28 
29 static void eap_peap_deinit(struct eap_sm *sm, void *priv);
30 
31 
32 struct eap_peap_data {
33 	struct eap_ssl_data ssl;
34 
35 	int peap_version, force_peap_version, force_new_label;
36 
37 	const struct eap_method *phase2_method;
38 	void *phase2_priv;
39 	int phase2_success;
40 	int phase2_eap_success;
41 	int phase2_eap_started;
42 
43 	struct eap_method_type phase2_type;
44 	struct eap_method_type *phase2_types;
45 	size_t num_phase2_types;
46 
47 	int peap_outer_success; /* 0 = PEAP terminated on Phase 2 inner
48 				 * EAP-Success
49 				 * 1 = reply with tunneled EAP-Success to inner
50 				 * EAP-Success and expect AS to send outer
51 				 * (unencrypted) EAP-Success after this
52 				 * 2 = reply with PEAP/TLS ACK to inner
53 				 * EAP-Success and expect AS to send outer
54 				 * (unencrypted) EAP-Success after this */
55 	int resuming; /* starting a resumed session */
56 	int reauth; /* reauthentication */
57 	u8 *key_data;
58 	u8 *session_id;
59 	size_t id_len;
60 
61 	struct wpabuf *pending_phase2_req;
62 	struct wpabuf *pending_resp;
63 	enum { NO_BINDING, OPTIONAL_BINDING, REQUIRE_BINDING } crypto_binding;
64 	int crypto_binding_used;
65 	u8 binding_nonce[32];
66 	u8 ipmk[40];
67 	u8 cmk[20];
68 	int soh; /* Whether IF-TNCCS-SOH (Statement of Health; Microsoft NAP)
69 		  * is enabled. */
70 	enum { NO_AUTH, FOR_INITIAL, ALWAYS } phase2_auth;
71 };
72 
73 
74 static void eap_peap_parse_phase1(struct eap_peap_data *data,
75 				  const char *phase1)
76 {
77 	const char *pos;
78 
79 	pos = os_strstr(phase1, "peapver=");
80 	if (pos) {
81 		data->force_peap_version = atoi(pos + 8);
82 		data->peap_version = data->force_peap_version;
83 		wpa_printf(MSG_DEBUG, "EAP-PEAP: Forced PEAP version %d",
84 			   data->force_peap_version);
85 	}
86 
87 	if (os_strstr(phase1, "peaplabel=1")) {
88 		data->force_new_label = 1;
89 		wpa_printf(MSG_DEBUG, "EAP-PEAP: Force new label for key "
90 			   "derivation");
91 	}
92 
93 	if (os_strstr(phase1, "peap_outer_success=0")) {
94 		data->peap_outer_success = 0;
95 		wpa_printf(MSG_DEBUG, "EAP-PEAP: terminate authentication on "
96 			   "tunneled EAP-Success");
97 	} else if (os_strstr(phase1, "peap_outer_success=1")) {
98 		data->peap_outer_success = 1;
99 		wpa_printf(MSG_DEBUG, "EAP-PEAP: send tunneled EAP-Success "
100 			   "after receiving tunneled EAP-Success");
101 	} else if (os_strstr(phase1, "peap_outer_success=2")) {
102 		data->peap_outer_success = 2;
103 		wpa_printf(MSG_DEBUG, "EAP-PEAP: send PEAP/TLS ACK after "
104 			   "receiving tunneled EAP-Success");
105 	}
106 
107 	if (os_strstr(phase1, "crypto_binding=0")) {
108 		data->crypto_binding = NO_BINDING;
109 		wpa_printf(MSG_DEBUG, "EAP-PEAP: Do not use cryptobinding");
110 	} else if (os_strstr(phase1, "crypto_binding=1")) {
111 		data->crypto_binding = OPTIONAL_BINDING;
112 		wpa_printf(MSG_DEBUG, "EAP-PEAP: Optional cryptobinding");
113 	} else if (os_strstr(phase1, "crypto_binding=2")) {
114 		data->crypto_binding = REQUIRE_BINDING;
115 		wpa_printf(MSG_DEBUG, "EAP-PEAP: Require cryptobinding");
116 	}
117 
118 	if (os_strstr(phase1, "phase2_auth=0")) {
119 		data->phase2_auth = NO_AUTH;
120 		wpa_printf(MSG_DEBUG,
121 			   "EAP-PEAP: Do not require Phase 2 authentication");
122 	} else if (os_strstr(phase1, "phase2_auth=1")) {
123 		data->phase2_auth = FOR_INITIAL;
124 		wpa_printf(MSG_DEBUG,
125 			   "EAP-PEAP: Require Phase 2 authentication for initial connection");
126 	} else if (os_strstr(phase1, "phase2_auth=2")) {
127 		data->phase2_auth = ALWAYS;
128 		wpa_printf(MSG_DEBUG,
129 			   "EAP-PEAP: Require Phase 2 authentication for all cases");
130 	}
131 #ifdef EAP_TNC
132 	if (os_strstr(phase1, "tnc=soh2")) {
133 		data->soh = 2;
134 		wpa_printf(MSG_DEBUG, "EAP-PEAP: SoH version 2 enabled");
135 	} else if (os_strstr(phase1, "tnc=soh1")) {
136 		data->soh = 1;
137 		wpa_printf(MSG_DEBUG, "EAP-PEAP: SoH version 1 enabled");
138 	} else if (os_strstr(phase1, "tnc=soh")) {
139 		data->soh = 2;
140 		wpa_printf(MSG_DEBUG, "EAP-PEAP: SoH version 2 enabled");
141 	}
142 #endif /* EAP_TNC */
143 }
144 
145 
146 static void * eap_peap_init(struct eap_sm *sm)
147 {
148 	struct eap_peap_data *data;
149 	struct eap_peer_config *config = eap_get_config(sm);
150 
151 	data = os_zalloc(sizeof(*data));
152 	if (data == NULL)
153 		return NULL;
154 	sm->peap_done = FALSE;
155 	data->peap_version = EAP_PEAP_VERSION;
156 	data->force_peap_version = -1;
157 	data->peap_outer_success = 2;
158 	data->crypto_binding = OPTIONAL_BINDING;
159 	data->phase2_auth = FOR_INITIAL;
160 
161 	if (config && config->phase1)
162 		eap_peap_parse_phase1(data, config->phase1);
163 
164 	if (eap_peer_select_phase2_methods(config, "auth=",
165 					   &data->phase2_types,
166 					   &data->num_phase2_types) < 0) {
167 		eap_peap_deinit(sm, data);
168 		return NULL;
169 	}
170 
171 	data->phase2_type.vendor = EAP_VENDOR_IETF;
172 	data->phase2_type.method = EAP_TYPE_NONE;
173 
174 	if (eap_peer_tls_ssl_init(sm, &data->ssl, config, EAP_TYPE_PEAP)) {
175 		wpa_printf(MSG_INFO, "EAP-PEAP: Failed to initialize SSL.");
176 		eap_peap_deinit(sm, data);
177 		return NULL;
178 	}
179 
180 	return data;
181 }
182 
183 
184 static void eap_peap_free_key(struct eap_peap_data *data)
185 {
186 	if (data->key_data) {
187 		bin_clear_free(data->key_data, EAP_TLS_KEY_LEN + EAP_EMSK_LEN);
188 		data->key_data = NULL;
189 	}
190 }
191 
192 
193 static void eap_peap_deinit(struct eap_sm *sm, void *priv)
194 {
195 	struct eap_peap_data *data = priv;
196 	if (data == NULL)
197 		return;
198 	if (data->phase2_priv && data->phase2_method)
199 		data->phase2_method->deinit(sm, data->phase2_priv);
200 	os_free(data->phase2_types);
201 	eap_peer_tls_ssl_deinit(sm, &data->ssl);
202 	eap_peap_free_key(data);
203 	os_free(data->session_id);
204 	wpabuf_clear_free(data->pending_phase2_req);
205 	wpabuf_clear_free(data->pending_resp);
206 	bin_clear_free(data, sizeof(*data));
207 }
208 
209 
210 /**
211  * eap_tlv_build_nak - Build EAP-TLV NAK message
212  * @id: EAP identifier for the header
213  * @nak_type: TLV type (EAP_TLV_*)
214  * Returns: Buffer to the allocated EAP-TLV NAK message or %NULL on failure
215  *
216  * This function builds an EAP-TLV NAK message. The caller is responsible for
217  * freeing the returned buffer.
218  */
219 static struct wpabuf * eap_tlv_build_nak(int id, u16 nak_type)
220 {
221 	struct wpabuf *msg;
222 
223 	msg = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TLV, 10,
224 			    EAP_CODE_RESPONSE, id);
225 	if (msg == NULL)
226 		return NULL;
227 
228 	wpabuf_put_u8(msg, 0x80); /* Mandatory */
229 	wpabuf_put_u8(msg, EAP_TLV_NAK_TLV);
230 	wpabuf_put_be16(msg, 6); /* Length */
231 	wpabuf_put_be32(msg, 0); /* Vendor-Id */
232 	wpabuf_put_be16(msg, nak_type); /* NAK-Type */
233 
234 	return msg;
235 }
236 
237 
238 static int eap_peap_get_isk(struct eap_sm *sm, struct eap_peap_data *data,
239 			    u8 *isk, size_t isk_len)
240 {
241 	u8 *key;
242 	size_t key_len;
243 
244 	os_memset(isk, 0, isk_len);
245 	if (data->phase2_method == NULL || data->phase2_priv == NULL ||
246 	    data->phase2_method->isKeyAvailable == NULL ||
247 	    data->phase2_method->getKey == NULL)
248 		return 0;
249 
250 	if (!data->phase2_method->isKeyAvailable(sm, data->phase2_priv) ||
251 	    (key = data->phase2_method->getKey(sm, data->phase2_priv,
252 					       &key_len)) == NULL) {
253 		wpa_printf(MSG_DEBUG, "EAP-PEAP: Could not get key material "
254 			   "from Phase 2");
255 		return -1;
256 	}
257 
258 	if (key_len > isk_len)
259 		key_len = isk_len;
260 	os_memcpy(isk, key, key_len);
261 	os_free(key);
262 
263 	return 0;
264 }
265 
266 
267 static int eap_peap_derive_cmk(struct eap_sm *sm, struct eap_peap_data *data)
268 {
269 	u8 *tk;
270 	u8 isk[32], imck[60];
271 	int resumed, res;
272 
273 	/*
274 	 * Tunnel key (TK) is the first 60 octets of the key generated by
275 	 * phase 1 of PEAP (based on TLS).
276 	 */
277 	tk = data->key_data;
278 	if (tk == NULL)
279 		return -1;
280 	wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: TK", tk, 60);
281 
282 	resumed = tls_connection_resumed(sm->ssl_ctx, data->ssl.conn);
283 	wpa_printf(MSG_DEBUG,
284 		   "EAP-PEAP: CMK derivation - reauth=%d resumed=%d phase2_eap_started=%d phase2_success=%d",
285 		   data->reauth, resumed, data->phase2_eap_started,
286 		   data->phase2_success);
287 	if (data->reauth && !data->phase2_eap_started && resumed) {
288 		/* Fast-connect: IPMK|CMK = TK */
289 		os_memcpy(data->ipmk, tk, 40);
290 		wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: IPMK from TK",
291 				data->ipmk, 40);
292 		os_memcpy(data->cmk, tk + 40, 20);
293 		wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: CMK from TK",
294 				data->cmk, 20);
295 		return 0;
296 	}
297 
298 	if (eap_peap_get_isk(sm, data, isk, sizeof(isk)) < 0)
299 		return -1;
300 	wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: ISK", isk, sizeof(isk));
301 
302 	/*
303 	 * IPMK Seed = "Inner Methods Compound Keys" | ISK
304 	 * TempKey = First 40 octets of TK
305 	 * IPMK|CMK = PRF+(TempKey, IPMK Seed, 60)
306 	 * (note: draft-josefsson-pppext-eap-tls-eap-10.txt includes a space
307 	 * in the end of the label just before ISK; is that just a typo?)
308 	 */
309 	wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: TempKey", tk, 40);
310 	res = peap_prfplus(data->peap_version, tk, 40,
311 			   "Inner Methods Compound Keys",
312 			   isk, sizeof(isk), imck, sizeof(imck));
313 	forced_memzero(isk, sizeof(isk));
314 	if (res < 0)
315 		return -1;
316 	wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: IMCK (IPMKj)",
317 			imck, sizeof(imck));
318 
319 	os_memcpy(data->ipmk, imck, 40);
320 	wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: IPMK (S-IPMKj)", data->ipmk, 40);
321 	os_memcpy(data->cmk, imck + 40, 20);
322 	wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: CMK (CMKj)", data->cmk, 20);
323 	forced_memzero(imck, sizeof(imck));
324 
325 	return 0;
326 }
327 
328 
329 static int eap_tlv_add_cryptobinding(struct eap_sm *sm,
330 				     struct eap_peap_data *data,
331 				     struct wpabuf *buf)
332 {
333 	u8 *mac;
334 	u8 eap_type = EAP_TYPE_PEAP;
335 	const u8 *addr[2];
336 	size_t len[2];
337 	u16 tlv_type;
338 
339 	/* Compound_MAC: HMAC-SHA1-160(cryptobinding TLV | EAP type) */
340 	addr[0] = wpabuf_put(buf, 0);
341 	len[0] = 60;
342 	addr[1] = &eap_type;
343 	len[1] = 1;
344 
345 	tlv_type = EAP_TLV_CRYPTO_BINDING_TLV;
346 	wpabuf_put_be16(buf, tlv_type);
347 	wpabuf_put_be16(buf, 56);
348 
349 	wpabuf_put_u8(buf, 0); /* Reserved */
350 	wpabuf_put_u8(buf, data->peap_version); /* Version */
351 	wpabuf_put_u8(buf, data->peap_version); /* RecvVersion */
352 	wpabuf_put_u8(buf, 1); /* SubType: 0 = Request, 1 = Response */
353 	wpabuf_put_data(buf, data->binding_nonce, 32); /* Nonce */
354 	mac = wpabuf_put(buf, 20); /* Compound_MAC */
355 	wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC CMK", data->cmk, 20);
356 	wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC data 1",
357 		    addr[0], len[0]);
358 	wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC data 2",
359 		    addr[1], len[1]);
360 	if (hmac_sha1_vector(data->cmk, 20, 2, addr, len, mac) < 0)
361 		return -1;
362 	wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC", mac, SHA1_MAC_LEN);
363 	data->crypto_binding_used = 1;
364 
365 	return 0;
366 }
367 
368 
369 /**
370  * eap_tlv_build_result - Build EAP-TLV Result message
371  * @id: EAP identifier for the header
372  * @status: Status (EAP_TLV_RESULT_SUCCESS or EAP_TLV_RESULT_FAILURE)
373  * Returns: Buffer to the allocated EAP-TLV Result message or %NULL on failure
374  *
375  * This function builds an EAP-TLV Result message. The caller is responsible
376  * for freeing the returned buffer.
377  */
378 static struct wpabuf * eap_tlv_build_result(struct eap_sm *sm,
379 					    struct eap_peap_data *data,
380 					    int crypto_tlv_used,
381 					    int id, u16 status)
382 {
383 	struct wpabuf *msg;
384 	size_t len;
385 
386 	if (data->crypto_binding == NO_BINDING)
387 		crypto_tlv_used = 0;
388 
389 	len = 6;
390 	if (crypto_tlv_used)
391 		len += 60; /* Cryptobinding TLV */
392 	msg = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TLV, len,
393 			    EAP_CODE_RESPONSE, id);
394 	if (msg == NULL)
395 		return NULL;
396 
397 	wpabuf_put_u8(msg, 0x80); /* Mandatory */
398 	wpabuf_put_u8(msg, EAP_TLV_RESULT_TLV);
399 	wpabuf_put_be16(msg, 2); /* Length */
400 	wpabuf_put_be16(msg, status); /* Status */
401 
402 	if (crypto_tlv_used && eap_tlv_add_cryptobinding(sm, data, msg)) {
403 		wpabuf_clear_free(msg);
404 		return NULL;
405 	}
406 
407 	return msg;
408 }
409 
410 
411 static int eap_tlv_validate_cryptobinding(struct eap_sm *sm,
412 					  struct eap_peap_data *data,
413 					  const u8 *crypto_tlv,
414 					  size_t crypto_tlv_len)
415 {
416 	u8 buf[61], mac[SHA1_MAC_LEN];
417 	const u8 *pos;
418 
419 	if (eap_peap_derive_cmk(sm, data) < 0) {
420 		wpa_printf(MSG_DEBUG, "EAP-PEAP: Could not derive CMK");
421 		return -1;
422 	}
423 
424 	if (crypto_tlv_len != 4 + 56) {
425 		wpa_printf(MSG_DEBUG, "EAP-PEAP: Invalid cryptobinding TLV "
426 			   "length %d", (int) crypto_tlv_len);
427 		return -1;
428 	}
429 
430 	pos = crypto_tlv;
431 	pos += 4; /* TLV header */
432 	if (pos[1] != data->peap_version) {
433 		wpa_printf(MSG_DEBUG, "EAP-PEAP: Cryptobinding TLV Version "
434 			   "mismatch (was %d; expected %d)",
435 			   pos[1], data->peap_version);
436 		return -1;
437 	}
438 
439 	if (pos[3] != 0) {
440 		wpa_printf(MSG_DEBUG, "EAP-PEAP: Unexpected Cryptobinding TLV "
441 			   "SubType %d", pos[3]);
442 		return -1;
443 	}
444 	pos += 4;
445 	os_memcpy(data->binding_nonce, pos, 32);
446 	pos += 32; /* Nonce */
447 
448 	/* Compound_MAC: HMAC-SHA1-160(cryptobinding TLV | EAP type) */
449 	os_memcpy(buf, crypto_tlv, 60);
450 	os_memset(buf + 4 + 4 + 32, 0, 20); /* Compound_MAC */
451 	buf[60] = EAP_TYPE_PEAP;
452 	wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Compound_MAC data",
453 		    buf, sizeof(buf));
454 	hmac_sha1(data->cmk, 20, buf, sizeof(buf), mac);
455 
456 	if (os_memcmp_const(mac, pos, SHA1_MAC_LEN) != 0) {
457 		wpa_printf(MSG_DEBUG, "EAP-PEAP: Invalid Compound_MAC in "
458 			   "cryptobinding TLV");
459 		wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Received MAC",
460 			    pos, SHA1_MAC_LEN);
461 		wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Expected MAC",
462 			    mac, SHA1_MAC_LEN);
463 		return -1;
464 	}
465 
466 	wpa_printf(MSG_DEBUG, "EAP-PEAP: Valid cryptobinding TLV received");
467 
468 	return 0;
469 }
470 
471 
472 static bool peap_phase2_sufficient(struct eap_sm *sm,
473 				   struct eap_peap_data *data)
474 {
475 	if ((data->phase2_auth == ALWAYS ||
476 	     (data->phase2_auth == FOR_INITIAL &&
477 	      !tls_connection_resumed(sm->ssl_ctx, data->ssl.conn) &&
478 	      !data->ssl.client_cert_conf) ||
479 	     data->phase2_eap_started) &&
480 	    !data->phase2_eap_success)
481 		return false;
482 	return true;
483 }
484 
485 
486 /**
487  * eap_tlv_process - Process a received EAP-TLV message and generate a response
488  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
489  * @ret: Return values from EAP request validation and processing
490  * @req: EAP-TLV request to be processed. The caller must have validated that
491  * the buffer is large enough to contain full request (hdr->length bytes) and
492  * that the EAP type is EAP_TYPE_TLV.
493  * @resp: Buffer to return a pointer to the allocated response message. This
494  * field should be initialized to %NULL before the call. The value will be
495  * updated if a response message is generated. The caller is responsible for
496  * freeing the allocated message.
497  * @force_failure: Force negotiation to fail
498  * Returns: 0 on success, -1 on failure
499  */
500 static int eap_tlv_process(struct eap_sm *sm, struct eap_peap_data *data,
501 			   struct eap_method_ret *ret,
502 			   const struct wpabuf *req, struct wpabuf **resp,
503 			   int force_failure)
504 {
505 	size_t left, tlv_len;
506 	const u8 *pos;
507 	const u8 *result_tlv = NULL, *crypto_tlv = NULL;
508 	size_t result_tlv_len = 0, crypto_tlv_len = 0;
509 	int tlv_type, mandatory;
510 
511 	/* Parse TLVs */
512 	pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_TLV, req, &left);
513 	if (pos == NULL)
514 		return -1;
515 	wpa_hexdump(MSG_DEBUG, "EAP-TLV: Received TLVs", pos, left);
516 	while (left >= 4) {
517 		mandatory = !!(pos[0] & 0x80);
518 		tlv_type = WPA_GET_BE16(pos) & 0x3fff;
519 		pos += 2;
520 		tlv_len = WPA_GET_BE16(pos);
521 		pos += 2;
522 		left -= 4;
523 		if (tlv_len > left) {
524 			wpa_printf(MSG_DEBUG, "EAP-TLV: TLV underrun "
525 				   "(tlv_len=%lu left=%lu)",
526 				   (unsigned long) tlv_len,
527 				   (unsigned long) left);
528 			return -1;
529 		}
530 		switch (tlv_type) {
531 		case EAP_TLV_RESULT_TLV:
532 			result_tlv = pos;
533 			result_tlv_len = tlv_len;
534 			break;
535 		case EAP_TLV_CRYPTO_BINDING_TLV:
536 			crypto_tlv = pos;
537 			crypto_tlv_len = tlv_len;
538 			break;
539 		default:
540 			wpa_printf(MSG_DEBUG, "EAP-TLV: Unsupported TLV Type "
541 				   "%d%s", tlv_type,
542 				   mandatory ? " (mandatory)" : "");
543 			if (mandatory) {
544 				/* NAK TLV and ignore all TLVs in this packet.
545 				 */
546 				*resp = eap_tlv_build_nak(eap_get_id(req),
547 							  tlv_type);
548 				return *resp == NULL ? -1 : 0;
549 			}
550 			/* Ignore this TLV, but process other TLVs */
551 			break;
552 		}
553 
554 		pos += tlv_len;
555 		left -= tlv_len;
556 	}
557 	if (left) {
558 		wpa_printf(MSG_DEBUG, "EAP-TLV: Last TLV too short in "
559 			   "Request (left=%lu)", (unsigned long) left);
560 		return -1;
561 	}
562 
563 	/* Process supported TLVs */
564 	if (crypto_tlv && data->crypto_binding != NO_BINDING) {
565 		wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Cryptobinding TLV",
566 			    crypto_tlv, crypto_tlv_len);
567 		if (eap_tlv_validate_cryptobinding(sm, data, crypto_tlv - 4,
568 						   crypto_tlv_len + 4) < 0) {
569 			if (result_tlv == NULL)
570 				return -1;
571 			force_failure = 1;
572 			crypto_tlv = NULL; /* do not include Cryptobinding TLV
573 					    * in response, if the received
574 					    * cryptobinding was invalid. */
575 		}
576 	} else if (!crypto_tlv && data->crypto_binding == REQUIRE_BINDING) {
577 		wpa_printf(MSG_DEBUG, "EAP-PEAP: No cryptobinding TLV");
578 		return -1;
579 	}
580 
581 	if (result_tlv) {
582 		int status, resp_status;
583 		wpa_hexdump(MSG_DEBUG, "EAP-TLV: Result TLV",
584 			    result_tlv, result_tlv_len);
585 		if (result_tlv_len < 2) {
586 			wpa_printf(MSG_INFO, "EAP-TLV: Too short Result TLV "
587 				   "(len=%lu)",
588 				   (unsigned long) result_tlv_len);
589 			return -1;
590 		}
591 		status = WPA_GET_BE16(result_tlv);
592 		if (status == EAP_TLV_RESULT_SUCCESS) {
593 			wpa_printf(MSG_INFO, "EAP-TLV: TLV Result - Success "
594 				   "- EAP-TLV/Phase2 Completed");
595 			if (force_failure) {
596 				wpa_printf(MSG_INFO, "EAP-TLV: Earlier failure"
597 					   " - force failed Phase 2");
598 				resp_status = EAP_TLV_RESULT_FAILURE;
599 				ret->decision = DECISION_FAIL;
600 			} else if (!peap_phase2_sufficient(sm, data)) {
601 				wpa_printf(MSG_INFO,
602 					   "EAP-PEAP: Server indicated Phase 2 success, but sufficient Phase 2 authentication has not been completed");
603 				resp_status = EAP_TLV_RESULT_FAILURE;
604 				ret->decision = DECISION_FAIL;
605 			} else {
606 				resp_status = EAP_TLV_RESULT_SUCCESS;
607 				ret->decision = DECISION_UNCOND_SUCC;
608 			}
609 		} else if (status == EAP_TLV_RESULT_FAILURE) {
610 			wpa_printf(MSG_INFO, "EAP-TLV: TLV Result - Failure");
611 			resp_status = EAP_TLV_RESULT_FAILURE;
612 			ret->decision = DECISION_FAIL;
613 		} else {
614 			wpa_printf(MSG_INFO, "EAP-TLV: Unknown TLV Result "
615 				   "Status %d", status);
616 			resp_status = EAP_TLV_RESULT_FAILURE;
617 			ret->decision = DECISION_FAIL;
618 		}
619 		ret->methodState = METHOD_DONE;
620 
621 		*resp = eap_tlv_build_result(sm, data, crypto_tlv != NULL,
622 					     eap_get_id(req), resp_status);
623 	}
624 
625 	return 0;
626 }
627 
628 
629 static int eap_peap_phase2_request(struct eap_sm *sm,
630 				   struct eap_peap_data *data,
631 				   struct eap_method_ret *ret,
632 				   struct wpabuf *req,
633 				   struct wpabuf **resp)
634 {
635 	struct eap_hdr *hdr = wpabuf_mhead(req);
636 	size_t len = be_to_host16(hdr->length);
637 	u8 *pos;
638 	struct eap_method_ret iret;
639 	struct eap_peer_config *config = eap_get_config(sm);
640 
641 	if (len <= sizeof(struct eap_hdr)) {
642 		wpa_printf(MSG_INFO, "EAP-PEAP: too short "
643 			   "Phase 2 request (len=%lu)", (unsigned long) len);
644 		return -1;
645 	}
646 	pos = (u8 *) (hdr + 1);
647 	wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 Request: type=%d", *pos);
648 	switch (*pos) {
649 	case EAP_TYPE_IDENTITY:
650 		*resp = eap_sm_buildIdentity(sm, hdr->identifier, 1);
651 		break;
652 	case EAP_TYPE_TLV:
653 		os_memset(&iret, 0, sizeof(iret));
654 		if (eap_tlv_process(sm, data, &iret, req, resp,
655 				    data->phase2_eap_started &&
656 				    !data->phase2_eap_success)) {
657 			ret->methodState = METHOD_DONE;
658 			ret->decision = DECISION_FAIL;
659 			return -1;
660 		}
661 		if (iret.methodState == METHOD_DONE ||
662 		    iret.methodState == METHOD_MAY_CONT) {
663 			ret->methodState = iret.methodState;
664 			ret->decision = iret.decision;
665 			data->phase2_success = 1;
666 		}
667 		break;
668 	case EAP_TYPE_EXPANDED:
669 #ifdef EAP_TNC
670 		if (data->soh) {
671 			const u8 *epos;
672 			size_t eleft;
673 
674 			epos = eap_hdr_validate(EAP_VENDOR_MICROSOFT, 0x21,
675 						req, &eleft);
676 			if (epos) {
677 				struct wpabuf *buf;
678 				wpa_printf(MSG_DEBUG,
679 					   "EAP-PEAP: SoH EAP Extensions");
680 				buf = tncc_process_soh_request(data->soh,
681 							       epos, eleft);
682 				if (buf) {
683 					*resp = eap_msg_alloc(
684 						EAP_VENDOR_MICROSOFT, 0x21,
685 						wpabuf_len(buf),
686 						EAP_CODE_RESPONSE,
687 						hdr->identifier);
688 					if (*resp == NULL) {
689 						ret->methodState = METHOD_DONE;
690 						ret->decision = DECISION_FAIL;
691 						wpabuf_clear_free(buf);
692 						return -1;
693 					}
694 					wpabuf_put_buf(*resp, buf);
695 					wpabuf_clear_free(buf);
696 					break;
697 				}
698 			}
699 		}
700 #endif /* EAP_TNC */
701 		/* fall through */
702 	default:
703 		if (data->phase2_type.vendor == EAP_VENDOR_IETF &&
704 		    data->phase2_type.method == EAP_TYPE_NONE) {
705 			size_t i;
706 			for (i = 0; i < data->num_phase2_types; i++) {
707 				if (data->phase2_types[i].vendor !=
708 				    EAP_VENDOR_IETF ||
709 				    data->phase2_types[i].method != *pos)
710 					continue;
711 
712 				data->phase2_type.vendor =
713 					data->phase2_types[i].vendor;
714 				data->phase2_type.method =
715 					data->phase2_types[i].method;
716 				wpa_printf(MSG_DEBUG, "EAP-PEAP: Selected "
717 					   "Phase 2 EAP vendor %d method %d",
718 					   data->phase2_type.vendor,
719 					   data->phase2_type.method);
720 				break;
721 			}
722 		}
723 		if (*pos != data->phase2_type.method ||
724 		    *pos == EAP_TYPE_NONE) {
725 			if (eap_peer_tls_phase2_nak(data->phase2_types,
726 						    data->num_phase2_types,
727 						    hdr, resp))
728 				return -1;
729 			return 0;
730 		}
731 
732 		if (data->phase2_priv == NULL) {
733 			data->phase2_method = eap_peer_get_eap_method(
734 				data->phase2_type.vendor,
735 				data->phase2_type.method);
736 			if (data->phase2_method) {
737 				sm->init_phase2 = 1;
738 				data->phase2_priv =
739 					data->phase2_method->init(sm);
740 				sm->init_phase2 = 0;
741 			}
742 		}
743 		if (data->phase2_priv == NULL || data->phase2_method == NULL) {
744 			wpa_printf(MSG_INFO, "EAP-PEAP: failed to initialize "
745 				   "Phase 2 EAP method %d", *pos);
746 			ret->methodState = METHOD_DONE;
747 			ret->decision = DECISION_FAIL;
748 			return -1;
749 		}
750 		data->phase2_eap_started = 1;
751 		os_memset(&iret, 0, sizeof(iret));
752 		*resp = data->phase2_method->process(sm, data->phase2_priv,
753 						     &iret, req);
754 		if ((iret.methodState == METHOD_DONE ||
755 		     iret.methodState == METHOD_MAY_CONT) &&
756 		    (iret.decision == DECISION_UNCOND_SUCC ||
757 		     iret.decision == DECISION_COND_SUCC)) {
758 			data->phase2_eap_success = 1;
759 			data->phase2_success = 1;
760 		}
761 		break;
762 	}
763 
764 	if (*resp == NULL &&
765 	    (config->pending_req_identity || config->pending_req_password ||
766 	     config->pending_req_otp || config->pending_req_new_password ||
767 	     config->pending_req_sim)) {
768 		wpabuf_clear_free(data->pending_phase2_req);
769 		data->pending_phase2_req = wpabuf_alloc_copy(hdr, len);
770 	}
771 
772 	return 0;
773 }
774 
775 
776 static int eap_peap_decrypt(struct eap_sm *sm, struct eap_peap_data *data,
777 			    struct eap_method_ret *ret,
778 			    const struct eap_hdr *req,
779 			    const struct wpabuf *in_data,
780 			    struct wpabuf **out_data)
781 {
782 	struct wpabuf *in_decrypted = NULL;
783 	int res, skip_change = 0;
784 	struct eap_hdr *hdr, *rhdr;
785 	struct wpabuf *resp = NULL;
786 	size_t len;
787 
788 	wpa_printf(MSG_DEBUG, "EAP-PEAP: received %lu bytes encrypted data for"
789 		   " Phase 2", (unsigned long) wpabuf_len(in_data));
790 
791 	if (data->pending_phase2_req) {
792 		wpa_printf(MSG_DEBUG, "EAP-PEAP: Pending Phase 2 request - "
793 			   "skip decryption and use old data");
794 		/* Clear TLS reassembly state. */
795 		eap_peer_tls_reset_input(&data->ssl);
796 		in_decrypted = data->pending_phase2_req;
797 		data->pending_phase2_req = NULL;
798 		skip_change = 1;
799 		goto continue_req;
800 	}
801 
802 	if (wpabuf_len(in_data) == 0 && sm->workaround &&
803 	    data->phase2_success) {
804 		/*
805 		 * Cisco ACS seems to be using TLS ACK to terminate
806 		 * EAP-PEAPv0/GTC. Try to reply with TLS ACK.
807 		 */
808 		wpa_printf(MSG_DEBUG, "EAP-PEAP: Received TLS ACK, but "
809 			   "expected data - acknowledge with TLS ACK since "
810 			   "Phase 2 has been completed");
811 		ret->decision = DECISION_COND_SUCC;
812 		ret->methodState = METHOD_DONE;
813 		return 1;
814 	} else if (wpabuf_len(in_data) == 0) {
815 		/* Received TLS ACK - requesting more fragments */
816 		return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_PEAP,
817 					    data->peap_version,
818 					    req->identifier, NULL, out_data);
819 	}
820 
821 	res = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
822 	if (res)
823 		return res;
824 
825 continue_req:
826 	wpa_hexdump_buf(MSG_DEBUG, "EAP-PEAP: Decrypted Phase 2 EAP",
827 			in_decrypted);
828 
829 	hdr = wpabuf_mhead(in_decrypted);
830 	if (wpabuf_len(in_decrypted) == 5 && hdr->code == EAP_CODE_REQUEST &&
831 	    be_to_host16(hdr->length) == 5 &&
832 	    eap_get_type(in_decrypted) == EAP_TYPE_IDENTITY) {
833 		/* At least FreeRADIUS seems to send full EAP header with
834 		 * EAP Request Identity */
835 		skip_change = 1;
836 	}
837 	if (wpabuf_len(in_decrypted) >= 5 && hdr->code == EAP_CODE_REQUEST &&
838 	    eap_get_type(in_decrypted) == EAP_TYPE_TLV) {
839 		skip_change = 1;
840 	}
841 
842 	if (data->peap_version == 0 && !skip_change) {
843 		struct eap_hdr *nhdr;
844 		struct wpabuf *nmsg = wpabuf_alloc(sizeof(struct eap_hdr) +
845 						   wpabuf_len(in_decrypted));
846 		if (nmsg == NULL) {
847 			wpabuf_clear_free(in_decrypted);
848 			return 0;
849 		}
850 		nhdr = wpabuf_put(nmsg, sizeof(*nhdr));
851 		wpabuf_put_buf(nmsg, in_decrypted);
852 		nhdr->code = req->code;
853 		nhdr->identifier = req->identifier;
854 		nhdr->length = host_to_be16(sizeof(struct eap_hdr) +
855 					    wpabuf_len(in_decrypted));
856 
857 		wpabuf_clear_free(in_decrypted);
858 		in_decrypted = nmsg;
859 	}
860 
861 	hdr = wpabuf_mhead(in_decrypted);
862 	if (wpabuf_len(in_decrypted) < sizeof(*hdr)) {
863 		wpa_printf(MSG_INFO, "EAP-PEAP: Too short Phase 2 "
864 			   "EAP frame (len=%lu)",
865 			   (unsigned long) wpabuf_len(in_decrypted));
866 		wpabuf_clear_free(in_decrypted);
867 		return 0;
868 	}
869 	len = be_to_host16(hdr->length);
870 	if (len > wpabuf_len(in_decrypted)) {
871 		wpa_printf(MSG_INFO, "EAP-PEAP: Length mismatch in "
872 			   "Phase 2 EAP frame (len=%lu hdr->length=%lu)",
873 			   (unsigned long) wpabuf_len(in_decrypted),
874 			   (unsigned long) len);
875 		wpabuf_clear_free(in_decrypted);
876 		return 0;
877 	}
878 	if (len < wpabuf_len(in_decrypted)) {
879 		wpa_printf(MSG_INFO, "EAP-PEAP: Odd.. Phase 2 EAP header has "
880 			   "shorter length than full decrypted data "
881 			   "(%lu < %lu)",
882 			   (unsigned long) len,
883 			   (unsigned long) wpabuf_len(in_decrypted));
884 	}
885 	wpa_printf(MSG_DEBUG, "EAP-PEAP: received Phase 2: code=%d "
886 		   "identifier=%d length=%lu", hdr->code, hdr->identifier,
887 		   (unsigned long) len);
888 	switch (hdr->code) {
889 	case EAP_CODE_REQUEST:
890 		if (eap_peap_phase2_request(sm, data, ret, in_decrypted,
891 					    &resp)) {
892 			wpabuf_clear_free(in_decrypted);
893 			wpa_printf(MSG_INFO, "EAP-PEAP: Phase2 Request "
894 				   "processing failed");
895 			return 0;
896 		}
897 		break;
898 	case EAP_CODE_SUCCESS:
899 		wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 Success");
900 		if (data->peap_version == 1) {
901 			/* EAP-Success within TLS tunnel is used to indicate
902 			 * shutdown of the TLS channel. The authentication has
903 			 * been completed. */
904 			if (!peap_phase2_sufficient(sm, data)) {
905 				wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 "
906 					   "Success used to indicate success, "
907 					   "but Phase 2 EAP was not yet "
908 					   "completed successfully");
909 				ret->methodState = METHOD_DONE;
910 				ret->decision = DECISION_FAIL;
911 				wpabuf_clear_free(in_decrypted);
912 				return 0;
913 			}
914 			wpa_printf(MSG_DEBUG, "EAP-PEAP: Version 1 - "
915 				   "EAP-Success within TLS tunnel - "
916 				   "authentication completed");
917 			ret->decision = DECISION_UNCOND_SUCC;
918 			ret->methodState = METHOD_DONE;
919 			data->phase2_success = 1;
920 			if (data->peap_outer_success == 2) {
921 				wpabuf_clear_free(in_decrypted);
922 				wpa_printf(MSG_DEBUG, "EAP-PEAP: Use TLS ACK "
923 					   "to finish authentication");
924 				return 1;
925 			} else if (data->peap_outer_success == 1) {
926 				/* Reply with EAP-Success within the TLS
927 				 * channel to complete the authentication. */
928 				resp = wpabuf_alloc(sizeof(struct eap_hdr));
929 				if (resp) {
930 					rhdr = wpabuf_put(resp, sizeof(*rhdr));
931 					rhdr->code = EAP_CODE_SUCCESS;
932 					rhdr->identifier = hdr->identifier;
933 					rhdr->length =
934 						host_to_be16(sizeof(*rhdr));
935 				}
936 			} else {
937 				/* No EAP-Success expected for Phase 1 (outer,
938 				 * unencrypted auth), so force EAP state
939 				 * machine to SUCCESS state. */
940 				sm->peap_done = TRUE;
941 			}
942 		} else {
943 			/* FIX: ? */
944 		}
945 		break;
946 	case EAP_CODE_FAILURE:
947 		wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 Failure");
948 		ret->decision = DECISION_FAIL;
949 		ret->methodState = METHOD_MAY_CONT;
950 		ret->allowNotifications = FALSE;
951 		/* Reply with EAP-Failure within the TLS channel to complete
952 		 * failure reporting. */
953 		resp = wpabuf_alloc(sizeof(struct eap_hdr));
954 		if (resp) {
955 			rhdr = wpabuf_put(resp, sizeof(*rhdr));
956 			rhdr->code = EAP_CODE_FAILURE;
957 			rhdr->identifier = hdr->identifier;
958 			rhdr->length = host_to_be16(sizeof(*rhdr));
959 		}
960 		break;
961 	default:
962 		wpa_printf(MSG_INFO, "EAP-PEAP: Unexpected code=%d in "
963 			   "Phase 2 EAP header", hdr->code);
964 		break;
965 	}
966 
967 	wpabuf_clear_free(in_decrypted);
968 
969 	if (resp) {
970 		int skip_change2 = 0;
971 		struct wpabuf *rmsg, buf;
972 
973 		wpa_hexdump_buf_key(MSG_DEBUG,
974 				    "EAP-PEAP: Encrypting Phase 2 data", resp);
975 		/* PEAP version changes */
976 		if (wpabuf_len(resp) >= 5 &&
977 		    wpabuf_head_u8(resp)[0] == EAP_CODE_RESPONSE &&
978 		    eap_get_type(resp) == EAP_TYPE_TLV)
979 			skip_change2 = 1;
980 		rmsg = resp;
981 		if (data->peap_version == 0 && !skip_change2) {
982 			wpabuf_set(&buf, wpabuf_head_u8(resp) +
983 				   sizeof(struct eap_hdr),
984 				   wpabuf_len(resp) - sizeof(struct eap_hdr));
985 			rmsg = &buf;
986 		}
987 
988 		if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_PEAP,
989 					 data->peap_version, req->identifier,
990 					 rmsg, out_data)) {
991 			wpa_printf(MSG_INFO, "EAP-PEAP: Failed to encrypt "
992 				   "a Phase 2 frame");
993 		}
994 		wpabuf_clear_free(resp);
995 	}
996 
997 	return 0;
998 }
999 
1000 
1001 static struct wpabuf * eap_peap_process(struct eap_sm *sm, void *priv,
1002 					struct eap_method_ret *ret,
1003 					const struct wpabuf *reqData)
1004 {
1005 	const struct eap_hdr *req;
1006 	size_t left;
1007 	int res;
1008 	u8 flags, id;
1009 	struct wpabuf *resp;
1010 	const u8 *pos;
1011 	struct eap_peap_data *data = priv;
1012 	struct wpabuf msg;
1013 
1014 	pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_PEAP, ret,
1015 					reqData, &left, &flags);
1016 	if (pos == NULL)
1017 		return NULL;
1018 	req = wpabuf_head(reqData);
1019 	id = req->identifier;
1020 
1021 	if (flags & EAP_TLS_FLAGS_START) {
1022 		wpa_printf(MSG_DEBUG, "EAP-PEAP: Start (server ver=%d, own "
1023 			   "ver=%d)", flags & EAP_TLS_VERSION_MASK,
1024 			data->peap_version);
1025 		if ((flags & EAP_TLS_VERSION_MASK) < data->peap_version)
1026 			data->peap_version = flags & EAP_TLS_VERSION_MASK;
1027 		if (data->force_peap_version >= 0 &&
1028 		    data->force_peap_version != data->peap_version) {
1029 			wpa_printf(MSG_WARNING, "EAP-PEAP: Failed to select "
1030 				   "forced PEAP version %d",
1031 				   data->force_peap_version);
1032 			ret->methodState = METHOD_DONE;
1033 			ret->decision = DECISION_FAIL;
1034 			ret->allowNotifications = FALSE;
1035 			return NULL;
1036 		}
1037 		wpa_printf(MSG_DEBUG, "EAP-PEAP: Using PEAP version %d",
1038 			   data->peap_version);
1039 		left = 0; /* make sure that this frame is empty, even though it
1040 			   * should always be, anyway */
1041 	}
1042 
1043 	wpabuf_set(&msg, pos, left);
1044 
1045 	resp = NULL;
1046 	if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1047 	    !data->resuming) {
1048 		res = eap_peap_decrypt(sm, data, ret, req, &msg, &resp);
1049 	} else {
1050 		if (sm->waiting_ext_cert_check && data->pending_resp) {
1051 			struct eap_peer_config *config = eap_get_config(sm);
1052 
1053 			if (config->pending_ext_cert_check ==
1054 			    EXT_CERT_CHECK_GOOD) {
1055 				wpa_printf(MSG_DEBUG,
1056 					   "EAP-PEAP: External certificate check succeeded - continue handshake");
1057 				resp = data->pending_resp;
1058 				data->pending_resp = NULL;
1059 				sm->waiting_ext_cert_check = 0;
1060 				return resp;
1061 			}
1062 
1063 			if (config->pending_ext_cert_check ==
1064 			    EXT_CERT_CHECK_BAD) {
1065 				wpa_printf(MSG_DEBUG,
1066 					   "EAP-PEAP: External certificate check failed - force authentication failure");
1067 				ret->methodState = METHOD_DONE;
1068 				ret->decision = DECISION_FAIL;
1069 				sm->waiting_ext_cert_check = 0;
1070 				return NULL;
1071 			}
1072 
1073 			wpa_printf(MSG_DEBUG,
1074 				   "EAP-PEAP: Continuing to wait external server certificate validation");
1075 			return NULL;
1076 		}
1077 
1078 		res = eap_peer_tls_process_helper(sm, &data->ssl,
1079 						  EAP_TYPE_PEAP,
1080 						  data->peap_version, id, &msg,
1081 						  &resp);
1082 
1083 		if (res < 0) {
1084 			wpa_printf(MSG_DEBUG,
1085 				   "EAP-PEAP: TLS processing failed");
1086 			ret->methodState = METHOD_DONE;
1087 			ret->decision = DECISION_FAIL;
1088 			return resp;
1089 		}
1090 
1091 
1092 		if (sm->waiting_ext_cert_check) {
1093 			wpa_printf(MSG_DEBUG,
1094 				   "EAP-PEAP: Waiting external server certificate validation");
1095 			wpabuf_clear_free(data->pending_resp);
1096 			data->pending_resp = resp;
1097 			return NULL;
1098 		}
1099 
1100 		if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
1101 			char *label;
1102 			wpa_printf(MSG_DEBUG,
1103 				   "EAP-PEAP: TLS done, proceed to Phase 2");
1104 			eap_peap_free_key(data);
1105 			/* draft-josefsson-ppext-eap-tls-eap-05.txt
1106 			 * specifies that PEAPv1 would use "client PEAP
1107 			 * encryption" as the label. However, most existing
1108 			 * PEAPv1 implementations seem to be using the old
1109 			 * label, "client EAP encryption", instead. Use the old
1110 			 * label by default, but allow it to be configured with
1111 			 * phase1 parameter peaplabel=1. */
1112 			if (data->force_new_label)
1113 				label = "client PEAP encryption";
1114 			else
1115 				label = "client EAP encryption";
1116 			wpa_printf(MSG_DEBUG, "EAP-PEAP: using label '%s' in "
1117 				   "key derivation", label);
1118 			data->key_data =
1119 				eap_peer_tls_derive_key(sm, &data->ssl, label,
1120 							NULL, 0,
1121 							EAP_TLS_KEY_LEN +
1122 							EAP_EMSK_LEN);
1123 			if (data->key_data) {
1124 				wpa_hexdump_key(MSG_DEBUG,
1125 						"EAP-PEAP: Derived key",
1126 						data->key_data,
1127 						EAP_TLS_KEY_LEN);
1128 				wpa_hexdump_key(MSG_DEBUG,
1129 						"EAP-PEAP: Derived EMSK",
1130 						data->key_data +
1131 						EAP_TLS_KEY_LEN,
1132 						EAP_EMSK_LEN);
1133 			} else {
1134 				wpa_printf(MSG_DEBUG, "EAP-PEAP: Failed to "
1135 					   "derive key");
1136 			}
1137 
1138 			os_free(data->session_id);
1139 			data->session_id =
1140 				eap_peer_tls_derive_session_id(sm, &data->ssl,
1141 							       EAP_TYPE_PEAP,
1142 							       &data->id_len);
1143 			if (data->session_id) {
1144 				wpa_hexdump(MSG_DEBUG,
1145 					    "EAP-PEAP: Derived Session-Id",
1146 					    data->session_id, data->id_len);
1147 			} else {
1148 				wpa_printf(MSG_ERROR, "EAP-PEAP: Failed to "
1149 					   "derive Session-Id");
1150 			}
1151 
1152 			if (sm->workaround && data->resuming) {
1153 				/*
1154 				 * At least few RADIUS servers (Aegis v1.1.6;
1155 				 * but not v1.1.4; and Cisco ACS) seem to be
1156 				 * terminating PEAPv1 (Aegis) or PEAPv0 (Cisco
1157 				 * ACS) session resumption with outer
1158 				 * EAP-Success. This does not seem to follow
1159 				 * draft-josefsson-pppext-eap-tls-eap-05.txt
1160 				 * section 4.2, so only allow this if EAP
1161 				 * workarounds are enabled.
1162 				 */
1163 				wpa_printf(MSG_DEBUG, "EAP-PEAP: Workaround - "
1164 					   "allow outer EAP-Success to "
1165 					   "terminate PEAP resumption");
1166 				ret->decision = DECISION_COND_SUCC;
1167 				data->phase2_success = 1;
1168 			}
1169 
1170 			data->resuming = 0;
1171 		}
1172 
1173 		if (res == 2) {
1174 			/*
1175 			 * Application data included in the handshake message.
1176 			 */
1177 			wpabuf_clear_free(data->pending_phase2_req);
1178 			data->pending_phase2_req = resp;
1179 			resp = NULL;
1180 			res = eap_peap_decrypt(sm, data, ret, req, &msg,
1181 					       &resp);
1182 		}
1183 	}
1184 
1185 	if (ret->methodState == METHOD_DONE) {
1186 		ret->allowNotifications = FALSE;
1187 	}
1188 
1189 	if (res == 1) {
1190 		wpabuf_clear_free(resp);
1191 		return eap_peer_tls_build_ack(id, EAP_TYPE_PEAP,
1192 					      data->peap_version);
1193 	}
1194 
1195 	return resp;
1196 }
1197 
1198 
1199 static Boolean eap_peap_has_reauth_data(struct eap_sm *sm, void *priv)
1200 {
1201 	struct eap_peap_data *data = priv;
1202 
1203 	return tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1204 		data->phase2_success && data->phase2_auth != ALWAYS;
1205 }
1206 
1207 
1208 static void eap_peap_deinit_for_reauth(struct eap_sm *sm, void *priv)
1209 {
1210 	struct eap_peap_data *data = priv;
1211 
1212 	if (data->phase2_priv && data->phase2_method &&
1213 	    data->phase2_method->deinit_for_reauth)
1214 		data->phase2_method->deinit_for_reauth(sm, data->phase2_priv);
1215 	wpabuf_clear_free(data->pending_phase2_req);
1216 	data->pending_phase2_req = NULL;
1217 	wpabuf_clear_free(data->pending_resp);
1218 	data->pending_resp = NULL;
1219 	data->crypto_binding_used = 0;
1220 }
1221 
1222 
1223 static void * eap_peap_init_for_reauth(struct eap_sm *sm, void *priv)
1224 {
1225 	struct eap_peap_data *data = priv;
1226 	eap_peap_free_key(data);
1227 	os_free(data->session_id);
1228 	data->session_id = NULL;
1229 	if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
1230 		os_free(data);
1231 		return NULL;
1232 	}
1233 	if (data->phase2_priv && data->phase2_method &&
1234 	    data->phase2_method->init_for_reauth)
1235 		data->phase2_method->init_for_reauth(sm, data->phase2_priv);
1236 	data->phase2_success = 0;
1237 	data->phase2_eap_success = 0;
1238 	data->phase2_eap_started = 0;
1239 	data->resuming = 1;
1240 	data->reauth = 1;
1241 	sm->peap_done = FALSE;
1242 	return priv;
1243 }
1244 
1245 
1246 static int eap_peap_get_status(struct eap_sm *sm, void *priv, char *buf,
1247 			       size_t buflen, int verbose)
1248 {
1249 	struct eap_peap_data *data = priv;
1250 	int len, ret;
1251 
1252 	len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
1253 	if (data->phase2_method) {
1254 		ret = os_snprintf(buf + len, buflen - len,
1255 				  "EAP-PEAPv%d Phase2 method=%s\n",
1256 				  data->peap_version,
1257 				  data->phase2_method->name);
1258 		if (os_snprintf_error(buflen - len, ret))
1259 			return len;
1260 		len += ret;
1261 	}
1262 	return len;
1263 }
1264 
1265 
1266 static Boolean eap_peap_isKeyAvailable(struct eap_sm *sm, void *priv)
1267 {
1268 	struct eap_peap_data *data = priv;
1269 	return data->key_data != NULL && data->phase2_success;
1270 }
1271 
1272 
1273 static u8 * eap_peap_getKey(struct eap_sm *sm, void *priv, size_t *len)
1274 {
1275 	struct eap_peap_data *data = priv;
1276 	u8 *key;
1277 
1278 	if (data->key_data == NULL || !data->phase2_success)
1279 		return NULL;
1280 
1281 	key = os_malloc(EAP_TLS_KEY_LEN);
1282 	if (key == NULL)
1283 		return NULL;
1284 
1285 	*len = EAP_TLS_KEY_LEN;
1286 
1287 	if (data->crypto_binding_used) {
1288 		u8 csk[128];
1289 		/*
1290 		 * Note: It looks like Microsoft implementation requires null
1291 		 * termination for this label while the one used for deriving
1292 		 * IPMK|CMK did not use null termination.
1293 		 */
1294 		if (peap_prfplus(data->peap_version, data->ipmk, 40,
1295 				 "Session Key Generating Function",
1296 				 (u8 *) "\00", 1, csk, sizeof(csk)) < 0) {
1297 			os_free(key);
1298 			return NULL;
1299 		}
1300 		wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: CSK", csk, sizeof(csk));
1301 		os_memcpy(key, csk, EAP_TLS_KEY_LEN);
1302 		wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Derived key",
1303 			    key, EAP_TLS_KEY_LEN);
1304 		forced_memzero(csk, sizeof(csk));
1305 	} else
1306 		os_memcpy(key, data->key_data, EAP_TLS_KEY_LEN);
1307 
1308 	return key;
1309 }
1310 
1311 
1312 static u8 * eap_peap_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
1313 {
1314 	struct eap_peap_data *data = priv;
1315 	u8 *key;
1316 
1317 	if (!data->key_data || !data->phase2_success)
1318 		return NULL;
1319 
1320 	if (data->crypto_binding_used) {
1321 		/* [MS-PEAP] does not define EMSK derivation */
1322 		return NULL;
1323 	}
1324 
1325 	key = os_memdup(data->key_data + EAP_TLS_KEY_LEN, EAP_EMSK_LEN);
1326 	if (!key)
1327 		return NULL;
1328 
1329 	*len = EAP_EMSK_LEN;
1330 
1331 	return key;
1332 }
1333 
1334 
1335 static u8 * eap_peap_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
1336 {
1337 	struct eap_peap_data *data = priv;
1338 	u8 *id;
1339 
1340 	if (data->session_id == NULL || !data->phase2_success)
1341 		return NULL;
1342 
1343 	id = os_memdup(data->session_id, data->id_len);
1344 	if (id == NULL)
1345 		return NULL;
1346 
1347 	*len = data->id_len;
1348 
1349 	return id;
1350 }
1351 
1352 
1353 int eap_peer_peap_register(void)
1354 {
1355 	struct eap_method *eap;
1356 
1357 	eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
1358 				    EAP_VENDOR_IETF, EAP_TYPE_PEAP, "PEAP");
1359 	if (eap == NULL)
1360 		return -1;
1361 
1362 	eap->init = eap_peap_init;
1363 	eap->deinit = eap_peap_deinit;
1364 	eap->process = eap_peap_process;
1365 	eap->isKeyAvailable = eap_peap_isKeyAvailable;
1366 	eap->getKey = eap_peap_getKey;
1367 	eap->get_emsk = eap_peap_get_emsk;
1368 	eap->get_status = eap_peap_get_status;
1369 	eap->has_reauth_data = eap_peap_has_reauth_data;
1370 	eap->deinit_for_reauth = eap_peap_deinit_for_reauth;
1371 	eap->init_for_reauth = eap_peap_init_for_reauth;
1372 	eap->getSessionId = eap_peap_get_session_id;
1373 
1374 	return eap_peer_method_register(eap);
1375 }
1376