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