2 * EAP peer method: EAP-PEAP (draft-josefsson-pppext-eap-tls-eap-10.txt)
3 * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
18 #include "crypto/sha1.h"
20 #include "eap_tls_common.h"
21 #include "eap_config.h"
23 #include "eap_common/eap_tlv_common.h"
24 #include "eap_common/eap_peap_common.h"
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
33 #define EAP_PEAP_VERSION 1
36 static void eap_peap_deinit(struct eap_sm *sm, void *priv);
39 struct eap_peap_data {
40 struct eap_ssl_data ssl;
42 int peap_version, force_peap_version, force_new_label;
44 const struct eap_method *phase2_method;
47 int phase2_eap_success;
48 int phase2_eap_started;
50 struct eap_method_type phase2_type;
51 struct eap_method_type *phase2_types;
52 size_t num_phase2_types;
54 int peap_outer_success; /* 0 = PEAP terminated on Phase 2 inner
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 */
65 struct wpabuf *pending_phase2_req;
66 enum { NO_BINDING, OPTIONAL_BINDING, REQUIRE_BINDING } crypto_binding;
67 int crypto_binding_used;
70 int soh; /* Whether IF-TNCCS-SOH (Statement of Health; Microsoft NAP)
75 static int eap_peap_parse_phase1(struct eap_peap_data *data,
80 pos = os_strstr(phase1, "peapver=");
82 data->force_peap_version = atoi(pos + 8);
83 data->peap_version = data->force_peap_version;
84 wpa_printf(MSG_DEBUG, "EAP-PEAP: Forced PEAP version %d",
85 data->force_peap_version);
88 if (os_strstr(phase1, "peaplabel=1")) {
89 data->force_new_label = 1;
90 wpa_printf(MSG_DEBUG, "EAP-PEAP: Force new label for key "
94 if (os_strstr(phase1, "peap_outer_success=0")) {
95 data->peap_outer_success = 0;
96 wpa_printf(MSG_DEBUG, "EAP-PEAP: terminate authentication on "
97 "tunneled EAP-Success");
98 } else if (os_strstr(phase1, "peap_outer_success=1")) {
99 data->peap_outer_success = 1;
100 wpa_printf(MSG_DEBUG, "EAP-PEAP: send tunneled EAP-Success "
101 "after receiving tunneled EAP-Success");
102 } else if (os_strstr(phase1, "peap_outer_success=2")) {
103 data->peap_outer_success = 2;
104 wpa_printf(MSG_DEBUG, "EAP-PEAP: send PEAP/TLS ACK after "
105 "receiving tunneled EAP-Success");
108 if (os_strstr(phase1, "crypto_binding=0")) {
109 data->crypto_binding = NO_BINDING;
110 wpa_printf(MSG_DEBUG, "EAP-PEAP: Do not use cryptobinding");
111 } else if (os_strstr(phase1, "crypto_binding=1")) {
112 data->crypto_binding = OPTIONAL_BINDING;
113 wpa_printf(MSG_DEBUG, "EAP-PEAP: Optional cryptobinding");
114 } else if (os_strstr(phase1, "crypto_binding=2")) {
115 data->crypto_binding = REQUIRE_BINDING;
116 wpa_printf(MSG_DEBUG, "EAP-PEAP: Require cryptobinding");
120 if (os_strstr(phase1, "tnc=soh")) {
122 wpa_printf(MSG_DEBUG, "EAP-PEAP: SoH enabled");
130 static void * eap_peap_init(struct eap_sm *sm)
132 struct eap_peap_data *data;
133 struct eap_peer_config *config = eap_get_config(sm);
135 data = os_zalloc(sizeof(*data));
138 sm->peap_done = FALSE;
139 data->peap_version = EAP_PEAP_VERSION;
140 data->force_peap_version = -1;
141 data->peap_outer_success = 2;
142 data->crypto_binding = OPTIONAL_BINDING;
144 if (config && config->phase1 &&
145 eap_peap_parse_phase1(data, config->phase1) < 0) {
146 eap_peap_deinit(sm, data);
150 if (eap_peer_select_phase2_methods(config, "auth=",
152 &data->num_phase2_types) < 0) {
153 eap_peap_deinit(sm, data);
157 data->phase2_type.vendor = EAP_VENDOR_IETF;
158 data->phase2_type.method = EAP_TYPE_NONE;
160 if (eap_peer_tls_ssl_init(sm, &data->ssl, config)) {
161 wpa_printf(MSG_INFO, "EAP-PEAP: Failed to initialize SSL.");
162 eap_peap_deinit(sm, data);
170 static void eap_peap_deinit(struct eap_sm *sm, void *priv)
172 struct eap_peap_data *data = priv;
175 if (data->phase2_priv && data->phase2_method)
176 data->phase2_method->deinit(sm, data->phase2_priv);
177 os_free(data->phase2_types);
178 eap_peer_tls_ssl_deinit(sm, &data->ssl);
179 os_free(data->key_data);
180 wpabuf_free(data->pending_phase2_req);
186 * eap_tlv_build_nak - Build EAP-TLV NAK message
187 * @id: EAP identifier for the header
188 * @nak_type: TLV type (EAP_TLV_*)
189 * Returns: Buffer to the allocated EAP-TLV NAK message or %NULL on failure
191 * This funtion builds an EAP-TLV NAK message. The caller is responsible for
192 * freeing the returned buffer.
194 static struct wpabuf * eap_tlv_build_nak(int id, u16 nak_type)
198 msg = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TLV, 10,
199 EAP_CODE_RESPONSE, id);
203 wpabuf_put_u8(msg, 0x80); /* Mandatory */
204 wpabuf_put_u8(msg, EAP_TLV_NAK_TLV);
205 wpabuf_put_be16(msg, 6); /* Length */
206 wpabuf_put_be32(msg, 0); /* Vendor-Id */
207 wpabuf_put_be16(msg, nak_type); /* NAK-Type */
213 static int eap_peap_get_isk(struct eap_sm *sm, struct eap_peap_data *data,
214 u8 *isk, size_t isk_len)
219 os_memset(isk, 0, isk_len);
220 if (data->phase2_method == NULL || data->phase2_priv == NULL ||
221 data->phase2_method->isKeyAvailable == NULL ||
222 data->phase2_method->getKey == NULL)
225 if (!data->phase2_method->isKeyAvailable(sm, data->phase2_priv) ||
226 (key = data->phase2_method->getKey(sm, data->phase2_priv,
227 &key_len)) == NULL) {
228 wpa_printf(MSG_DEBUG, "EAP-PEAP: Could not get key material "
234 data->phase2_method->vendor == EAP_VENDOR_IETF &&
235 data->phase2_method->method == EAP_TYPE_MSCHAPV2) {
237 * Microsoft uses reverse order for MS-MPPE keys in
238 * EAP-PEAP when compared to EAP-FAST derivation of
239 * ISK. Swap the keys here to get the correct ISK for
240 * EAP-PEAPv0 cryptobinding.
243 os_memcpy(tmp, key, 16);
244 os_memcpy(key, key + 16, 16);
245 os_memcpy(key + 16, tmp, 16);
248 if (key_len > isk_len)
250 os_memcpy(isk, key, key_len);
257 static int eap_peap_derive_cmk(struct eap_sm *sm, struct eap_peap_data *data)
260 u8 isk[32], imck[60];
263 * Tunnel key (TK) is the first 60 octets of the key generated by
264 * phase 1 of PEAP (based on TLS).
269 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: TK", tk, 60);
271 if (eap_peap_get_isk(sm, data, isk, sizeof(isk)) < 0)
273 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: ISK", isk, sizeof(isk));
276 * IPMK Seed = "Inner Methods Compound Keys" | ISK
277 * TempKey = First 40 octets of TK
278 * IPMK|CMK = PRF+(TempKey, IPMK Seed, 60)
279 * (note: draft-josefsson-pppext-eap-tls-eap-10.txt includes a space
280 * in the end of the label just before ISK; is that just a typo?)
282 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: TempKey", tk, 40);
283 peap_prfplus(data->peap_version, tk, 40, "Inner Methods Compound Keys",
284 isk, sizeof(isk), imck, sizeof(imck));
285 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: IMCK (IPMKj)",
288 /* TODO: fast-connect: IPMK|CMK = TK */
289 os_memcpy(data->ipmk, imck, 40);
290 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: IPMK (S-IPMKj)", data->ipmk, 40);
291 os_memcpy(data->cmk, imck + 40, 20);
292 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: CMK (CMKj)", data->cmk, 20);
298 static int eap_tlv_add_cryptobinding(struct eap_sm *sm,
299 struct eap_peap_data *data,
303 u8 eap_type = EAP_TYPE_PEAP;
307 u8 binding_nonce[32];
309 /* FIX: should binding_nonce be copied from request? */
310 if (os_get_random(binding_nonce, 32))
313 /* Compound_MAC: HMAC-SHA1-160(cryptobinding TLV | EAP type) */
314 addr[0] = wpabuf_put(buf, 0);
319 tlv_type = EAP_TLV_CRYPTO_BINDING_TLV;
320 if (data->peap_version >= 2)
321 tlv_type |= EAP_TLV_TYPE_MANDATORY;
322 wpabuf_put_be16(buf, tlv_type);
323 wpabuf_put_be16(buf, 56);
325 wpabuf_put_u8(buf, 0); /* Reserved */
326 wpabuf_put_u8(buf, data->peap_version); /* Version */
327 wpabuf_put_u8(buf, data->peap_version); /* RecvVersion */
328 wpabuf_put_u8(buf, 1); /* SubType: 0 = Request, 1 = Response */
329 wpabuf_put_data(buf, binding_nonce, 32); /* Nonce */
330 mac = wpabuf_put(buf, 20); /* Compound_MAC */
331 wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC CMK", data->cmk, 20);
332 wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC data 1",
334 wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC data 2",
336 hmac_sha1_vector(data->cmk, 20, 2, addr, len, mac);
337 wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC", mac, SHA1_MAC_LEN);
338 data->crypto_binding_used = 1;
345 * eap_tlv_build_result - Build EAP-TLV Result message
346 * @id: EAP identifier for the header
347 * @status: Status (EAP_TLV_RESULT_SUCCESS or EAP_TLV_RESULT_FAILURE)
348 * Returns: Buffer to the allocated EAP-TLV Result message or %NULL on failure
350 * This funtion builds an EAP-TLV Result message. The caller is responsible for
351 * freeing the returned buffer.
353 static struct wpabuf * eap_tlv_build_result(struct eap_sm *sm,
354 struct eap_peap_data *data,
361 if (data->crypto_binding == NO_BINDING)
366 len += 60; /* Cryptobinding TLV */
367 msg = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TLV, len,
368 EAP_CODE_RESPONSE, id);
372 wpabuf_put_u8(msg, 0x80); /* Mandatory */
373 wpabuf_put_u8(msg, EAP_TLV_RESULT_TLV);
374 wpabuf_put_be16(msg, 2); /* Length */
375 wpabuf_put_be16(msg, status); /* Status */
377 if (crypto_tlv_used && eap_tlv_add_cryptobinding(sm, data, msg)) {
386 static int eap_tlv_validate_cryptobinding(struct eap_sm *sm,
387 struct eap_peap_data *data,
388 const u8 *crypto_tlv,
389 size_t crypto_tlv_len)
391 u8 buf[61], mac[SHA1_MAC_LEN];
394 if (eap_peap_derive_cmk(sm, data) < 0) {
395 wpa_printf(MSG_DEBUG, "EAP-PEAP: Could not derive CMK");
399 if (crypto_tlv_len != 4 + 56) {
400 wpa_printf(MSG_DEBUG, "EAP-PEAP: Invalid cryptobinding TLV "
401 "length %d", (int) crypto_tlv_len);
406 pos += 4; /* TLV header */
407 if (pos[1] != data->peap_version) {
408 wpa_printf(MSG_DEBUG, "EAP-PEAP: Cryptobinding TLV Version "
409 "mismatch (was %d; expected %d)",
410 pos[1], data->peap_version);
415 wpa_printf(MSG_DEBUG, "EAP-PEAP: Unexpected Cryptobinding TLV "
416 "SubType %d", pos[3]);
420 pos += 32; /* Nonce */
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 hmac_sha1(data->cmk, 20, buf, sizeof(buf), mac);
428 if (os_memcmp(mac, pos, SHA1_MAC_LEN) != 0) {
429 wpa_printf(MSG_DEBUG, "EAP-PEAP: Invalid Compound_MAC in "
430 "cryptobinding TLV");
434 wpa_printf(MSG_DEBUG, "EAP-PEAP: Valid cryptobinding TLV received");
441 * eap_tlv_process - Process a received EAP-TLV message and generate a response
442 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
443 * @ret: Return values from EAP request validation and processing
444 * @req: EAP-TLV request to be processed. The caller must have validated that
445 * the buffer is large enough to contain full request (hdr->length bytes) and
446 * that the EAP type is EAP_TYPE_TLV.
447 * @resp: Buffer to return a pointer to the allocated response message. This
448 * field should be initialized to %NULL before the call. The value will be
449 * updated if a response message is generated. The caller is responsible for
450 * freeing the allocated message.
451 * @force_failure: Force negotiation to fail
452 * Returns: 0 on success, -1 on failure
454 static int eap_tlv_process(struct eap_sm *sm, struct eap_peap_data *data,
455 struct eap_method_ret *ret,
456 const struct wpabuf *req, struct wpabuf **resp,
459 size_t left, tlv_len;
461 const u8 *result_tlv = NULL, *crypto_tlv = NULL;
462 size_t result_tlv_len = 0, crypto_tlv_len = 0;
463 int tlv_type, mandatory;
466 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_TLV, req, &left);
469 wpa_hexdump(MSG_DEBUG, "EAP-TLV: Received TLVs", pos, left);
471 mandatory = !!(pos[0] & 0x80);
472 tlv_type = WPA_GET_BE16(pos) & 0x3fff;
474 tlv_len = WPA_GET_BE16(pos);
477 if (tlv_len > left) {
478 wpa_printf(MSG_DEBUG, "EAP-TLV: TLV underrun "
479 "(tlv_len=%lu left=%lu)",
480 (unsigned long) tlv_len,
481 (unsigned long) left);
485 case EAP_TLV_RESULT_TLV:
487 result_tlv_len = tlv_len;
489 case EAP_TLV_CRYPTO_BINDING_TLV:
491 crypto_tlv_len = tlv_len;
494 wpa_printf(MSG_DEBUG, "EAP-TLV: Unsupported TLV Type "
496 mandatory ? " (mandatory)" : "");
498 /* NAK TLV and ignore all TLVs in this packet.
500 *resp = eap_tlv_build_nak(eap_get_id(req),
502 return *resp == NULL ? -1 : 0;
504 /* Ignore this TLV, but process other TLVs */
512 wpa_printf(MSG_DEBUG, "EAP-TLV: Last TLV too short in "
513 "Request (left=%lu)", (unsigned long) left);
517 /* Process supported TLVs */
518 if (crypto_tlv && data->crypto_binding != NO_BINDING) {
519 wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Cryptobinding TLV",
520 crypto_tlv, crypto_tlv_len);
521 if (eap_tlv_validate_cryptobinding(sm, data, crypto_tlv - 4,
522 crypto_tlv_len + 4) < 0) {
523 if (result_tlv == NULL)
527 } else if (!crypto_tlv && data->crypto_binding == REQUIRE_BINDING) {
528 wpa_printf(MSG_DEBUG, "EAP-PEAP: No cryptobinding TLV");
533 int status, resp_status;
534 wpa_hexdump(MSG_DEBUG, "EAP-TLV: Result TLV",
535 result_tlv, result_tlv_len);
536 if (result_tlv_len < 2) {
537 wpa_printf(MSG_INFO, "EAP-TLV: Too short Result TLV "
539 (unsigned long) result_tlv_len);
542 status = WPA_GET_BE16(result_tlv);
543 if (status == EAP_TLV_RESULT_SUCCESS) {
544 wpa_printf(MSG_INFO, "EAP-TLV: TLV Result - Success "
545 "- EAP-TLV/Phase2 Completed");
547 wpa_printf(MSG_INFO, "EAP-TLV: Earlier failure"
548 " - force failed Phase 2");
549 resp_status = EAP_TLV_RESULT_FAILURE;
550 ret->decision = DECISION_FAIL;
552 resp_status = EAP_TLV_RESULT_SUCCESS;
553 ret->decision = DECISION_UNCOND_SUCC;
555 } else if (status == EAP_TLV_RESULT_FAILURE) {
556 wpa_printf(MSG_INFO, "EAP-TLV: TLV Result - Failure");
557 resp_status = EAP_TLV_RESULT_FAILURE;
558 ret->decision = DECISION_FAIL;
560 wpa_printf(MSG_INFO, "EAP-TLV: Unknown TLV Result "
561 "Status %d", status);
562 resp_status = EAP_TLV_RESULT_FAILURE;
563 ret->decision = DECISION_FAIL;
565 ret->methodState = METHOD_DONE;
567 *resp = eap_tlv_build_result(sm, data, crypto_tlv != NULL,
568 eap_get_id(req), resp_status);
575 static struct wpabuf * eap_peapv2_tlv_eap_payload(struct wpabuf *buf)
578 struct eap_tlv_hdr *tlv;
583 /* Encapsulate EAP packet in EAP-Payload TLV */
584 wpa_printf(MSG_DEBUG, "EAP-PEAPv2: Add EAP-Payload TLV");
585 e = wpabuf_alloc(sizeof(*tlv) + wpabuf_len(buf));
587 wpa_printf(MSG_DEBUG, "EAP-PEAPv2: Failed to allocate memory "
588 "for TLV encapsulation");
592 tlv = wpabuf_put(e, sizeof(*tlv));
593 tlv->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
594 EAP_TLV_EAP_PAYLOAD_TLV);
595 tlv->length = host_to_be16(wpabuf_len(buf));
596 wpabuf_put_buf(e, buf);
602 static int eap_peap_phase2_request(struct eap_sm *sm,
603 struct eap_peap_data *data,
604 struct eap_method_ret *ret,
606 struct wpabuf **resp)
608 struct eap_hdr *hdr = wpabuf_mhead(req);
609 size_t len = be_to_host16(hdr->length);
611 struct eap_method_ret iret;
612 struct eap_peer_config *config = eap_get_config(sm);
614 if (len <= sizeof(struct eap_hdr)) {
615 wpa_printf(MSG_INFO, "EAP-PEAP: too short "
616 "Phase 2 request (len=%lu)", (unsigned long) len);
619 pos = (u8 *) (hdr + 1);
620 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 Request: type=%d", *pos);
622 case EAP_TYPE_IDENTITY:
623 *resp = eap_sm_buildIdentity(sm, hdr->identifier, 1);
626 os_memset(&iret, 0, sizeof(iret));
627 if (eap_tlv_process(sm, data, &iret, req, resp,
628 data->phase2_eap_started &&
629 !data->phase2_eap_success)) {
630 ret->methodState = METHOD_DONE;
631 ret->decision = DECISION_FAIL;
634 if (iret.methodState == METHOD_DONE ||
635 iret.methodState == METHOD_MAY_CONT) {
636 ret->methodState = iret.methodState;
637 ret->decision = iret.decision;
638 data->phase2_success = 1;
641 case EAP_TYPE_EXPANDED:
647 epos = eap_hdr_validate(EAP_VENDOR_MICROSOFT, 0x21,
651 wpa_printf(MSG_DEBUG,
652 "EAP-PEAP: SoH EAP Extensions");
653 buf = tncc_process_soh_request(epos, eleft);
655 *resp = eap_msg_alloc(
656 EAP_VENDOR_MICROSOFT, 0x21,
661 ret->methodState = METHOD_DONE;
662 ret->decision = DECISION_FAIL;
665 wpabuf_put_buf(*resp, buf);
674 if (data->phase2_type.vendor == EAP_VENDOR_IETF &&
675 data->phase2_type.method == EAP_TYPE_NONE) {
677 for (i = 0; i < data->num_phase2_types; i++) {
678 if (data->phase2_types[i].vendor !=
680 data->phase2_types[i].method != *pos)
683 data->phase2_type.vendor =
684 data->phase2_types[i].vendor;
685 data->phase2_type.method =
686 data->phase2_types[i].method;
687 wpa_printf(MSG_DEBUG, "EAP-PEAP: Selected "
688 "Phase 2 EAP vendor %d method %d",
689 data->phase2_type.vendor,
690 data->phase2_type.method);
694 if (*pos != data->phase2_type.method ||
695 *pos == EAP_TYPE_NONE) {
696 if (eap_peer_tls_phase2_nak(data->phase2_types,
697 data->num_phase2_types,
703 if (data->phase2_priv == NULL) {
704 data->phase2_method = eap_peer_get_eap_method(
705 data->phase2_type.vendor,
706 data->phase2_type.method);
707 if (data->phase2_method) {
709 sm->mschapv2_full_key = 1;
711 data->phase2_method->init(sm);
713 sm->mschapv2_full_key = 0;
716 if (data->phase2_priv == NULL || data->phase2_method == NULL) {
717 wpa_printf(MSG_INFO, "EAP-PEAP: failed to initialize "
718 "Phase 2 EAP method %d", *pos);
719 ret->methodState = METHOD_DONE;
720 ret->decision = DECISION_FAIL;
723 data->phase2_eap_started = 1;
724 os_memset(&iret, 0, sizeof(iret));
725 *resp = data->phase2_method->process(sm, data->phase2_priv,
727 if ((iret.methodState == METHOD_DONE ||
728 iret.methodState == METHOD_MAY_CONT) &&
729 (iret.decision == DECISION_UNCOND_SUCC ||
730 iret.decision == DECISION_COND_SUCC)) {
731 data->phase2_eap_success = 1;
732 data->phase2_success = 1;
738 (config->pending_req_identity || config->pending_req_password ||
739 config->pending_req_otp || config->pending_req_new_password)) {
740 wpabuf_free(data->pending_phase2_req);
741 data->pending_phase2_req = wpabuf_alloc_copy(hdr, len);
748 static int eap_peap_decrypt(struct eap_sm *sm, struct eap_peap_data *data,
749 struct eap_method_ret *ret,
750 const struct eap_hdr *req,
751 const struct wpabuf *in_data,
752 struct wpabuf **out_data)
754 struct wpabuf *in_decrypted = NULL;
755 int res, skip_change = 0;
756 struct eap_hdr *hdr, *rhdr;
757 struct wpabuf *resp = NULL;
760 wpa_printf(MSG_DEBUG, "EAP-PEAP: received %lu bytes encrypted data for"
761 " Phase 2", (unsigned long) wpabuf_len(in_data));
763 if (data->pending_phase2_req) {
764 wpa_printf(MSG_DEBUG, "EAP-PEAP: Pending Phase 2 request - "
765 "skip decryption and use old data");
766 /* Clear TLS reassembly state. */
767 eap_peer_tls_reset_input(&data->ssl);
768 in_decrypted = data->pending_phase2_req;
769 data->pending_phase2_req = NULL;
774 if (wpabuf_len(in_data) == 0 && sm->workaround &&
775 data->phase2_success) {
777 * Cisco ACS seems to be using TLS ACK to terminate
778 * EAP-PEAPv0/GTC. Try to reply with TLS ACK.
780 wpa_printf(MSG_DEBUG, "EAP-PEAP: Received TLS ACK, but "
781 "expected data - acknowledge with TLS ACK since "
782 "Phase 2 has been completed");
783 ret->decision = DECISION_COND_SUCC;
784 ret->methodState = METHOD_DONE;
786 } else if (wpabuf_len(in_data) == 0) {
787 /* Received TLS ACK - requesting more fragments */
788 return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_PEAP,
790 req->identifier, NULL, out_data);
793 res = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
798 wpa_hexdump_buf(MSG_DEBUG, "EAP-PEAP: Decrypted Phase 2 EAP",
801 hdr = wpabuf_mhead(in_decrypted);
802 if (wpabuf_len(in_decrypted) == 5 && hdr->code == EAP_CODE_REQUEST &&
803 be_to_host16(hdr->length) == 5 &&
804 eap_get_type(in_decrypted) == EAP_TYPE_IDENTITY) {
805 /* At least FreeRADIUS seems to send full EAP header with
806 * EAP Request Identity */
809 if (wpabuf_len(in_decrypted) >= 5 && hdr->code == EAP_CODE_REQUEST &&
810 eap_get_type(in_decrypted) == EAP_TYPE_TLV) {
814 if (data->peap_version == 0 && !skip_change) {
815 struct eap_hdr *nhdr;
816 struct wpabuf *nmsg = wpabuf_alloc(sizeof(struct eap_hdr) +
817 wpabuf_len(in_decrypted));
819 wpabuf_free(in_decrypted);
822 nhdr = wpabuf_put(nmsg, sizeof(*nhdr));
823 wpabuf_put_buf(nmsg, in_decrypted);
824 nhdr->code = req->code;
825 nhdr->identifier = req->identifier;
826 nhdr->length = host_to_be16(sizeof(struct eap_hdr) +
827 wpabuf_len(in_decrypted));
829 wpabuf_free(in_decrypted);
833 if (data->peap_version >= 2) {
834 struct eap_tlv_hdr *tlv;
837 if (wpabuf_len(in_decrypted) < sizeof(*tlv) + sizeof(*hdr)) {
838 wpa_printf(MSG_INFO, "EAP-PEAPv2: Too short Phase 2 "
840 wpabuf_free(in_decrypted);
843 tlv = wpabuf_mhead(in_decrypted);
844 if ((be_to_host16(tlv->tlv_type) & 0x3fff) !=
845 EAP_TLV_EAP_PAYLOAD_TLV) {
846 wpa_printf(MSG_INFO, "EAP-PEAPv2: Not an EAP TLV");
847 wpabuf_free(in_decrypted);
850 if (sizeof(*tlv) + be_to_host16(tlv->length) >
851 wpabuf_len(in_decrypted)) {
852 wpa_printf(MSG_INFO, "EAP-PEAPv2: Invalid EAP TLV "
854 wpabuf_free(in_decrypted);
857 hdr = (struct eap_hdr *) (tlv + 1);
858 if (be_to_host16(hdr->length) > be_to_host16(tlv->length)) {
859 wpa_printf(MSG_INFO, "EAP-PEAPv2: No room for full "
860 "EAP packet in EAP TLV");
861 wpabuf_free(in_decrypted);
865 nmsg = wpabuf_alloc(be_to_host16(hdr->length));
867 wpabuf_free(in_decrypted);
871 wpabuf_put_data(nmsg, hdr, be_to_host16(hdr->length));
872 wpabuf_free(in_decrypted);
876 hdr = wpabuf_mhead(in_decrypted);
877 if (wpabuf_len(in_decrypted) < sizeof(*hdr)) {
878 wpa_printf(MSG_INFO, "EAP-PEAP: Too short Phase 2 "
879 "EAP frame (len=%lu)",
880 (unsigned long) wpabuf_len(in_decrypted));
881 wpabuf_free(in_decrypted);
884 len = be_to_host16(hdr->length);
885 if (len > wpabuf_len(in_decrypted)) {
886 wpa_printf(MSG_INFO, "EAP-PEAP: Length mismatch in "
887 "Phase 2 EAP frame (len=%lu hdr->length=%lu)",
888 (unsigned long) wpabuf_len(in_decrypted),
889 (unsigned long) len);
890 wpabuf_free(in_decrypted);
893 if (len < wpabuf_len(in_decrypted)) {
894 wpa_printf(MSG_INFO, "EAP-PEAP: Odd.. Phase 2 EAP header has "
895 "shorter length than full decrypted data "
898 (unsigned long) wpabuf_len(in_decrypted));
900 wpa_printf(MSG_DEBUG, "EAP-PEAP: received Phase 2: code=%d "
901 "identifier=%d length=%lu", hdr->code, hdr->identifier,
902 (unsigned long) len);
904 case EAP_CODE_REQUEST:
905 if (eap_peap_phase2_request(sm, data, ret, in_decrypted,
907 wpabuf_free(in_decrypted);
908 wpa_printf(MSG_INFO, "EAP-PEAP: Phase2 Request "
909 "processing failed");
913 case EAP_CODE_SUCCESS:
914 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 Success");
915 if (data->peap_version == 1) {
916 /* EAP-Success within TLS tunnel is used to indicate
917 * shutdown of the TLS channel. The authentication has
919 if (data->phase2_eap_started &&
920 !data->phase2_eap_success) {
921 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 "
922 "Success used to indicate success, "
923 "but Phase 2 EAP was not yet "
924 "completed successfully");
925 ret->methodState = METHOD_DONE;
926 ret->decision = DECISION_FAIL;
927 wpabuf_free(in_decrypted);
930 wpa_printf(MSG_DEBUG, "EAP-PEAP: Version 1 - "
931 "EAP-Success within TLS tunnel - "
932 "authentication completed");
933 ret->decision = DECISION_UNCOND_SUCC;
934 ret->methodState = METHOD_DONE;
935 data->phase2_success = 1;
936 if (data->peap_outer_success == 2) {
937 wpabuf_free(in_decrypted);
938 wpa_printf(MSG_DEBUG, "EAP-PEAP: Use TLS ACK "
939 "to finish authentication");
941 } else if (data->peap_outer_success == 1) {
942 /* Reply with EAP-Success within the TLS
943 * channel to complete the authentication. */
944 resp = wpabuf_alloc(sizeof(struct eap_hdr));
946 rhdr = wpabuf_put(resp, sizeof(*rhdr));
947 rhdr->code = EAP_CODE_SUCCESS;
948 rhdr->identifier = hdr->identifier;
950 host_to_be16(sizeof(*rhdr));
953 /* No EAP-Success expected for Phase 1 (outer,
954 * unencrypted auth), so force EAP state
955 * machine to SUCCESS state. */
956 sm->peap_done = TRUE;
962 case EAP_CODE_FAILURE:
963 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 Failure");
964 ret->decision = DECISION_FAIL;
965 ret->methodState = METHOD_MAY_CONT;
966 ret->allowNotifications = FALSE;
967 /* Reply with EAP-Failure within the TLS channel to complete
968 * failure reporting. */
969 resp = wpabuf_alloc(sizeof(struct eap_hdr));
971 rhdr = wpabuf_put(resp, sizeof(*rhdr));
972 rhdr->code = EAP_CODE_FAILURE;
973 rhdr->identifier = hdr->identifier;
974 rhdr->length = host_to_be16(sizeof(*rhdr));
978 wpa_printf(MSG_INFO, "EAP-PEAP: Unexpected code=%d in "
979 "Phase 2 EAP header", hdr->code);
983 wpabuf_free(in_decrypted);
986 int skip_change2 = 0;
987 struct wpabuf *rmsg, buf;
989 wpa_hexdump_buf_key(MSG_DEBUG,
990 "EAP-PEAP: Encrypting Phase 2 data", resp);
991 /* PEAP version changes */
992 if (data->peap_version >= 2) {
993 resp = eap_peapv2_tlv_eap_payload(resp);
997 if (wpabuf_len(resp) >= 5 &&
998 wpabuf_head_u8(resp)[0] == EAP_CODE_RESPONSE &&
999 eap_get_type(resp) == EAP_TYPE_TLV)
1002 if (data->peap_version == 0 && !skip_change2) {
1003 wpabuf_set(&buf, wpabuf_head_u8(resp) +
1004 sizeof(struct eap_hdr),
1005 wpabuf_len(resp) - sizeof(struct eap_hdr));
1009 if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_PEAP,
1010 data->peap_version, req->identifier,
1012 wpa_printf(MSG_INFO, "EAP-PEAP: Failed to encrypt "
1022 static struct wpabuf * eap_peap_process(struct eap_sm *sm, void *priv,
1023 struct eap_method_ret *ret,
1024 const struct wpabuf *reqData)
1026 const struct eap_hdr *req;
1030 struct wpabuf *resp;
1032 struct eap_peap_data *data = priv;
1034 pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_PEAP, ret,
1035 reqData, &left, &flags);
1038 req = wpabuf_head(reqData);
1039 id = req->identifier;
1041 if (flags & EAP_TLS_FLAGS_START) {
1042 wpa_printf(MSG_DEBUG, "EAP-PEAP: Start (server ver=%d, own "
1043 "ver=%d)", flags & EAP_PEAP_VERSION_MASK,
1044 data->peap_version);
1045 if ((flags & EAP_PEAP_VERSION_MASK) < data->peap_version)
1046 data->peap_version = flags & EAP_PEAP_VERSION_MASK;
1047 if (data->force_peap_version >= 0 &&
1048 data->force_peap_version != data->peap_version) {
1049 wpa_printf(MSG_WARNING, "EAP-PEAP: Failed to select "
1050 "forced PEAP version %d",
1051 data->force_peap_version);
1052 ret->methodState = METHOD_DONE;
1053 ret->decision = DECISION_FAIL;
1054 ret->allowNotifications = FALSE;
1057 wpa_printf(MSG_DEBUG, "EAP-PEAP: Using PEAP version %d",
1058 data->peap_version);
1059 left = 0; /* make sure that this frame is empty, even though it
1060 * should always be, anyway */
1064 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1067 wpabuf_set(&msg, pos, left);
1068 res = eap_peap_decrypt(sm, data, ret, req, &msg, &resp);
1070 res = eap_peer_tls_process_helper(sm, &data->ssl,
1072 data->peap_version, id, pos,
1075 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
1077 wpa_printf(MSG_DEBUG,
1078 "EAP-PEAP: TLS done, proceed to Phase 2");
1079 os_free(data->key_data);
1080 /* draft-josefsson-ppext-eap-tls-eap-05.txt
1081 * specifies that PEAPv1 would use "client PEAP
1082 * encryption" as the label. However, most existing
1083 * PEAPv1 implementations seem to be using the old
1084 * label, "client EAP encryption", instead. Use the old
1085 * label by default, but allow it to be configured with
1086 * phase1 parameter peaplabel=1. */
1087 if (data->peap_version > 1 || data->force_new_label)
1088 label = "client PEAP encryption";
1090 label = "client EAP encryption";
1091 wpa_printf(MSG_DEBUG, "EAP-PEAP: using label '%s' in "
1092 "key derivation", label);
1094 eap_peer_tls_derive_key(sm, &data->ssl, label,
1096 if (data->key_data) {
1097 wpa_hexdump_key(MSG_DEBUG,
1098 "EAP-PEAP: Derived key",
1102 wpa_printf(MSG_DEBUG, "EAP-PEAP: Failed to "
1106 if (sm->workaround && data->resuming) {
1108 * At least few RADIUS servers (Aegis v1.1.6;
1109 * but not v1.1.4; and Cisco ACS) seem to be
1110 * terminating PEAPv1 (Aegis) or PEAPv0 (Cisco
1111 * ACS) session resumption with outer
1112 * EAP-Success. This does not seem to follow
1113 * draft-josefsson-pppext-eap-tls-eap-05.txt
1114 * section 4.2, so only allow this if EAP
1115 * workarounds are enabled.
1117 wpa_printf(MSG_DEBUG, "EAP-PEAP: Workaround - "
1118 "allow outer EAP-Success to "
1119 "terminate PEAP resumption");
1120 ret->decision = DECISION_COND_SUCC;
1121 data->phase2_success = 1;
1130 * Application data included in the handshake message.
1132 wpabuf_free(data->pending_phase2_req);
1133 data->pending_phase2_req = resp;
1135 wpabuf_set(&msg, pos, left);
1136 res = eap_peap_decrypt(sm, data, ret, req, &msg,
1141 if (ret->methodState == METHOD_DONE) {
1142 ret->allowNotifications = FALSE;
1147 return eap_peer_tls_build_ack(id, EAP_TYPE_PEAP,
1148 data->peap_version);
1155 static Boolean eap_peap_has_reauth_data(struct eap_sm *sm, void *priv)
1157 struct eap_peap_data *data = priv;
1158 return tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1159 data->phase2_success;
1163 static void eap_peap_deinit_for_reauth(struct eap_sm *sm, void *priv)
1165 struct eap_peap_data *data = priv;
1166 wpabuf_free(data->pending_phase2_req);
1167 data->pending_phase2_req = NULL;
1168 data->crypto_binding_used = 0;
1172 static void * eap_peap_init_for_reauth(struct eap_sm *sm, void *priv)
1174 struct eap_peap_data *data = priv;
1175 os_free(data->key_data);
1176 data->key_data = NULL;
1177 if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
1181 if (data->phase2_priv && data->phase2_method &&
1182 data->phase2_method->init_for_reauth)
1183 data->phase2_method->init_for_reauth(sm, data->phase2_priv);
1184 data->phase2_success = 0;
1185 data->phase2_eap_success = 0;
1186 data->phase2_eap_started = 0;
1188 sm->peap_done = FALSE;
1193 static int eap_peap_get_status(struct eap_sm *sm, void *priv, char *buf,
1194 size_t buflen, int verbose)
1196 struct eap_peap_data *data = priv;
1199 len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
1200 if (data->phase2_method) {
1201 ret = os_snprintf(buf + len, buflen - len,
1202 "EAP-PEAPv%d Phase2 method=%s\n",
1204 data->phase2_method->name);
1205 if (ret < 0 || (size_t) ret >= buflen - len)
1213 static Boolean eap_peap_isKeyAvailable(struct eap_sm *sm, void *priv)
1215 struct eap_peap_data *data = priv;
1216 return data->key_data != NULL && data->phase2_success;
1220 static u8 * eap_peap_getKey(struct eap_sm *sm, void *priv, size_t *len)
1222 struct eap_peap_data *data = priv;
1225 if (data->key_data == NULL || !data->phase2_success)
1228 key = os_malloc(EAP_TLS_KEY_LEN);
1232 *len = EAP_TLS_KEY_LEN;
1234 if (data->crypto_binding_used) {
1237 * Note: It looks like Microsoft implementation requires null
1238 * termination for this label while the one used for deriving
1239 * IPMK|CMK did not use null termination.
1241 peap_prfplus(data->peap_version, data->ipmk, 40,
1242 "Session Key Generating Function",
1243 (u8 *) "\00", 1, csk, sizeof(csk));
1244 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: CSK", csk, sizeof(csk));
1245 os_memcpy(key, csk, EAP_TLS_KEY_LEN);
1246 wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Derived key",
1247 key, EAP_TLS_KEY_LEN);
1249 os_memcpy(key, data->key_data, EAP_TLS_KEY_LEN);
1255 int eap_peer_peap_register(void)
1257 struct eap_method *eap;
1260 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
1261 EAP_VENDOR_IETF, EAP_TYPE_PEAP, "PEAP");
1265 eap->init = eap_peap_init;
1266 eap->deinit = eap_peap_deinit;
1267 eap->process = eap_peap_process;
1268 eap->isKeyAvailable = eap_peap_isKeyAvailable;
1269 eap->getKey = eap_peap_getKey;
1270 eap->get_status = eap_peap_get_status;
1271 eap->has_reauth_data = eap_peap_has_reauth_data;
1272 eap->deinit_for_reauth = eap_peap_deinit_for_reauth;
1273 eap->init_for_reauth = eap_peap_init_for_reauth;
1275 ret = eap_peer_method_register(eap);
1277 eap_peer_method_free(eap);