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