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 */
63 int reauth; /* reauthentication */
66 struct wpabuf *pending_phase2_req;
67 enum { NO_BINDING, OPTIONAL_BINDING, REQUIRE_BINDING } crypto_binding;
68 int crypto_binding_used;
72 int soh; /* Whether IF-TNCCS-SOH (Statement of Health; Microsoft NAP)
77 static int eap_peap_parse_phase1(struct eap_peap_data *data,
82 pos = os_strstr(phase1, "peapver=");
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);
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 "
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");
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");
122 if (os_strstr(phase1, "tnc=soh2")) {
124 wpa_printf(MSG_DEBUG, "EAP-PEAP: SoH version 2 enabled");
125 } else if (os_strstr(phase1, "tnc=soh1")) {
127 wpa_printf(MSG_DEBUG, "EAP-PEAP: SoH version 1 enabled");
128 } else if (os_strstr(phase1, "tnc=soh")) {
130 wpa_printf(MSG_DEBUG, "EAP-PEAP: SoH version 2 enabled");
138 static void * eap_peap_init(struct eap_sm *sm)
140 struct eap_peap_data *data;
141 struct eap_peer_config *config = eap_get_config(sm);
143 data = os_zalloc(sizeof(*data));
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;
152 if (config && config->phase1 &&
153 eap_peap_parse_phase1(data, config->phase1) < 0) {
154 eap_peap_deinit(sm, data);
158 if (eap_peer_select_phase2_methods(config, "auth=",
160 &data->num_phase2_types) < 0) {
161 eap_peap_deinit(sm, data);
165 data->phase2_type.vendor = EAP_VENDOR_IETF;
166 data->phase2_type.method = EAP_TYPE_NONE;
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);
178 static void eap_peap_deinit(struct eap_sm *sm, void *priv)
180 struct eap_peap_data *data = priv;
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);
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
199 * This funtion builds an EAP-TLV NAK message. The caller is responsible for
200 * freeing the returned buffer.
202 static struct wpabuf * eap_tlv_build_nak(int id, u16 nak_type)
206 msg = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TLV, 10,
207 EAP_CODE_RESPONSE, id);
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 */
221 static int eap_peap_get_isk(struct eap_sm *sm, struct eap_peap_data *data,
222 u8 *isk, size_t isk_len)
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)
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 "
241 if (key_len > isk_len)
243 os_memcpy(isk, key, key_len);
250 static int eap_peap_derive_cmk(struct eap_sm *sm, struct eap_peap_data *data)
253 u8 isk[32], imck[60];
256 * Tunnel key (TK) is the first 60 octets of the key generated by
257 * phase 1 of PEAP (based on TLS).
262 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: TK", tk, 60);
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",
270 os_memcpy(data->cmk, tk + 40, 20);
271 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: CMK from TK",
276 if (eap_peap_get_isk(sm, data, isk, sizeof(isk)) < 0)
278 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: ISK", isk, sizeof(isk));
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?)
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)",
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);
302 static int eap_tlv_add_cryptobinding(struct eap_sm *sm,
303 struct eap_peap_data *data,
307 u8 eap_type = EAP_TYPE_PEAP;
312 /* Compound_MAC: HMAC-SHA1-160(cryptobinding TLV | EAP type) */
313 addr[0] = wpabuf_put(buf, 0);
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);
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",
333 wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC data 2",
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;
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
349 * This funtion builds an EAP-TLV Result message. The caller is responsible for
350 * freeing the returned buffer.
352 static struct wpabuf * eap_tlv_build_result(struct eap_sm *sm,
353 struct eap_peap_data *data,
360 if (data->crypto_binding == NO_BINDING)
365 len += 60; /* Cryptobinding TLV */
366 msg = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TLV, len,
367 EAP_CODE_RESPONSE, id);
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 */
376 if (crypto_tlv_used && eap_tlv_add_cryptobinding(sm, data, msg)) {
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)
390 u8 buf[61], mac[SHA1_MAC_LEN];
393 if (eap_peap_derive_cmk(sm, data) < 0) {
394 wpa_printf(MSG_DEBUG, "EAP-PEAP: Could not derive CMK");
398 if (crypto_tlv_len != 4 + 56) {
399 wpa_printf(MSG_DEBUG, "EAP-PEAP: Invalid cryptobinding TLV "
400 "length %d", (int) crypto_tlv_len);
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);
414 wpa_printf(MSG_DEBUG, "EAP-PEAP: Unexpected Cryptobinding TLV "
415 "SubType %d", pos[3]);
419 os_memcpy(data->binding_nonce, pos, 32);
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 wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Compound_MAC data",
428 hmac_sha1(data->cmk, 20, buf, sizeof(buf), mac);
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",
435 wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Expected MAC",
440 wpa_printf(MSG_DEBUG, "EAP-PEAP: Valid cryptobinding TLV received");
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
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,
465 size_t left, tlv_len;
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;
472 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_TLV, req, &left);
475 wpa_hexdump(MSG_DEBUG, "EAP-TLV: Received TLVs", pos, left);
477 mandatory = !!(pos[0] & 0x80);
478 tlv_type = WPA_GET_BE16(pos) & 0x3fff;
480 tlv_len = WPA_GET_BE16(pos);
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);
491 case EAP_TLV_RESULT_TLV:
493 result_tlv_len = tlv_len;
495 case EAP_TLV_CRYPTO_BINDING_TLV:
497 crypto_tlv_len = tlv_len;
500 wpa_printf(MSG_DEBUG, "EAP-TLV: Unsupported TLV Type "
502 mandatory ? " (mandatory)" : "");
504 /* NAK TLV and ignore all TLVs in this packet.
506 *resp = eap_tlv_build_nak(eap_get_id(req),
508 return *resp == NULL ? -1 : 0;
510 /* Ignore this TLV, but process other TLVs */
518 wpa_printf(MSG_DEBUG, "EAP-TLV: Last TLV too short in "
519 "Request (left=%lu)", (unsigned long) left);
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)
532 crypto_tlv = NULL; /* do not include Cryptobinding TLV
533 * in response, if the received
534 * cryptobinding was invalid. */
536 } else if (!crypto_tlv && data->crypto_binding == REQUIRE_BINDING) {
537 wpa_printf(MSG_DEBUG, "EAP-PEAP: No cryptobinding 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 "
548 (unsigned long) result_tlv_len);
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");
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;
561 resp_status = EAP_TLV_RESULT_SUCCESS;
562 ret->decision = DECISION_UNCOND_SUCC;
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;
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;
574 ret->methodState = METHOD_DONE;
576 *resp = eap_tlv_build_result(sm, data, crypto_tlv != NULL,
577 eap_get_id(req), resp_status);
584 static struct wpabuf * eap_peapv2_tlv_eap_payload(struct wpabuf *buf)
587 struct eap_tlv_hdr *tlv;
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));
596 wpa_printf(MSG_DEBUG, "EAP-PEAPv2: Failed to allocate memory "
597 "for TLV encapsulation");
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);
611 static int eap_peap_phase2_request(struct eap_sm *sm,
612 struct eap_peap_data *data,
613 struct eap_method_ret *ret,
615 struct wpabuf **resp)
617 struct eap_hdr *hdr = wpabuf_mhead(req);
618 size_t len = be_to_host16(hdr->length);
620 struct eap_method_ret iret;
621 struct eap_peer_config *config = eap_get_config(sm);
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);
628 pos = (u8 *) (hdr + 1);
629 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 Request: type=%d", *pos);
631 case EAP_TYPE_IDENTITY:
632 *resp = eap_sm_buildIdentity(sm, hdr->identifier, 1);
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;
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;
650 case EAP_TYPE_EXPANDED:
656 epos = eap_hdr_validate(EAP_VENDOR_MICROSOFT, 0x21,
660 wpa_printf(MSG_DEBUG,
661 "EAP-PEAP: SoH EAP Extensions");
662 buf = tncc_process_soh_request(data->soh,
665 *resp = eap_msg_alloc(
666 EAP_VENDOR_MICROSOFT, 0x21,
671 ret->methodState = METHOD_DONE;
672 ret->decision = DECISION_FAIL;
675 wpabuf_put_buf(*resp, buf);
684 if (data->phase2_type.vendor == EAP_VENDOR_IETF &&
685 data->phase2_type.method == EAP_TYPE_NONE) {
687 for (i = 0; i < data->num_phase2_types; i++) {
688 if (data->phase2_types[i].vendor !=
690 data->phase2_types[i].method != *pos)
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);
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,
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) {
720 data->phase2_method->init(sm);
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;
731 data->phase2_eap_started = 1;
732 os_memset(&iret, 0, sizeof(iret));
733 *resp = data->phase2_method->process(sm, data->phase2_priv,
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;
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);
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)
762 struct wpabuf *in_decrypted = NULL;
763 int res, skip_change = 0;
764 struct eap_hdr *hdr, *rhdr;
765 struct wpabuf *resp = NULL;
768 wpa_printf(MSG_DEBUG, "EAP-PEAP: received %lu bytes encrypted data for"
769 " Phase 2", (unsigned long) wpabuf_len(in_data));
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;
782 if (wpabuf_len(in_data) == 0 && sm->workaround &&
783 data->phase2_success) {
785 * Cisco ACS seems to be using TLS ACK to terminate
786 * EAP-PEAPv0/GTC. Try to reply with TLS ACK.
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;
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,
798 req->identifier, NULL, out_data);
801 res = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
806 wpa_hexdump_buf(MSG_DEBUG, "EAP-PEAP: Decrypted Phase 2 EAP",
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 */
817 if (wpabuf_len(in_decrypted) >= 5 && hdr->code == EAP_CODE_REQUEST &&
818 eap_get_type(in_decrypted) == EAP_TYPE_TLV) {
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));
827 wpabuf_free(in_decrypted);
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));
837 wpabuf_free(in_decrypted);
841 if (data->peap_version >= 2) {
842 struct eap_tlv_hdr *tlv;
845 if (wpabuf_len(in_decrypted) < sizeof(*tlv) + sizeof(*hdr)) {
846 wpa_printf(MSG_INFO, "EAP-PEAPv2: Too short Phase 2 "
848 wpabuf_free(in_decrypted);
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);
858 if (sizeof(*tlv) + be_to_host16(tlv->length) >
859 wpabuf_len(in_decrypted)) {
860 wpa_printf(MSG_INFO, "EAP-PEAPv2: Invalid EAP TLV "
862 wpabuf_free(in_decrypted);
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);
873 nmsg = wpabuf_alloc(be_to_host16(hdr->length));
875 wpabuf_free(in_decrypted);
879 wpabuf_put_data(nmsg, hdr, be_to_host16(hdr->length));
880 wpabuf_free(in_decrypted);
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);
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);
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 "
906 (unsigned long) wpabuf_len(in_decrypted));
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);
912 case EAP_CODE_REQUEST:
913 if (eap_peap_phase2_request(sm, data, ret, in_decrypted,
915 wpabuf_free(in_decrypted);
916 wpa_printf(MSG_INFO, "EAP-PEAP: Phase2 Request "
917 "processing failed");
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
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);
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");
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));
954 rhdr = wpabuf_put(resp, sizeof(*rhdr));
955 rhdr->code = EAP_CODE_SUCCESS;
956 rhdr->identifier = hdr->identifier;
958 host_to_be16(sizeof(*rhdr));
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;
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));
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));
986 wpa_printf(MSG_INFO, "EAP-PEAP: Unexpected code=%d in "
987 "Phase 2 EAP header", hdr->code);
991 wpabuf_free(in_decrypted);
994 int skip_change2 = 0;
995 struct wpabuf *rmsg, buf;
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);
1005 if (wpabuf_len(resp) >= 5 &&
1006 wpabuf_head_u8(resp)[0] == EAP_CODE_RESPONSE &&
1007 eap_get_type(resp) == EAP_TYPE_TLV)
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));
1017 if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_PEAP,
1018 data->peap_version, req->identifier,
1020 wpa_printf(MSG_INFO, "EAP-PEAP: Failed to encrypt "
1030 static struct wpabuf * eap_peap_process(struct eap_sm *sm, void *priv,
1031 struct eap_method_ret *ret,
1032 const struct wpabuf *reqData)
1034 const struct eap_hdr *req;
1038 struct wpabuf *resp;
1040 struct eap_peap_data *data = priv;
1042 pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_PEAP, ret,
1043 reqData, &left, &flags);
1046 req = wpabuf_head(reqData);
1047 id = req->identifier;
1049 if (flags & EAP_TLS_FLAGS_START) {
1050 wpa_printf(MSG_DEBUG, "EAP-PEAP: Start (server ver=%d, own "
1051 "ver=%d)", flags & EAP_PEAP_VERSION_MASK,
1052 data->peap_version);
1053 if ((flags & EAP_PEAP_VERSION_MASK) < data->peap_version)
1054 data->peap_version = flags & EAP_PEAP_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;
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 */
1072 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1075 wpabuf_set(&msg, pos, left);
1076 res = eap_peap_decrypt(sm, data, ret, req, &msg, &resp);
1078 res = eap_peer_tls_process_helper(sm, &data->ssl,
1080 data->peap_version, id, pos,
1083 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
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";
1098 label = "client EAP encryption";
1099 wpa_printf(MSG_DEBUG, "EAP-PEAP: using label '%s' in "
1100 "key derivation", label);
1102 eap_peer_tls_derive_key(sm, &data->ssl, label,
1104 if (data->key_data) {
1105 wpa_hexdump_key(MSG_DEBUG,
1106 "EAP-PEAP: Derived key",
1110 wpa_printf(MSG_DEBUG, "EAP-PEAP: Failed to "
1114 if (sm->workaround && data->resuming) {
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.
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;
1138 * Application data included in the handshake message.
1140 wpabuf_free(data->pending_phase2_req);
1141 data->pending_phase2_req = resp;
1143 wpabuf_set(&msg, pos, left);
1144 res = eap_peap_decrypt(sm, data, ret, req, &msg,
1149 if (ret->methodState == METHOD_DONE) {
1150 ret->allowNotifications = FALSE;
1155 return eap_peer_tls_build_ack(id, EAP_TYPE_PEAP,
1156 data->peap_version);
1163 static Boolean eap_peap_has_reauth_data(struct eap_sm *sm, void *priv)
1165 struct eap_peap_data *data = priv;
1166 return tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1167 data->phase2_success;
1171 static void eap_peap_deinit_for_reauth(struct eap_sm *sm, void *priv)
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;
1180 static void * eap_peap_init_for_reauth(struct eap_sm *sm, void *priv)
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)) {
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;
1197 sm->peap_done = FALSE;
1202 static int eap_peap_get_status(struct eap_sm *sm, void *priv, char *buf,
1203 size_t buflen, int verbose)
1205 struct eap_peap_data *data = priv;
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",
1213 data->phase2_method->name);
1214 if (ret < 0 || (size_t) ret >= buflen - len)
1222 static Boolean eap_peap_isKeyAvailable(struct eap_sm *sm, void *priv)
1224 struct eap_peap_data *data = priv;
1225 return data->key_data != NULL && data->phase2_success;
1229 static u8 * eap_peap_getKey(struct eap_sm *sm, void *priv, size_t *len)
1231 struct eap_peap_data *data = priv;
1234 if (data->key_data == NULL || !data->phase2_success)
1237 key = os_malloc(EAP_TLS_KEY_LEN);
1241 *len = EAP_TLS_KEY_LEN;
1243 if (data->crypto_binding_used) {
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.
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);
1258 os_memcpy(key, data->key_data, EAP_TLS_KEY_LEN);
1264 int eap_peer_peap_register(void)
1266 struct eap_method *eap;
1269 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
1270 EAP_VENDOR_IETF, EAP_TYPE_PEAP, "PEAP");
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;
1284 ret = eap_peer_method_register(eap);
1286 eap_peer_method_free(eap);