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 software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "crypto/sha1.h"
13 #include "crypto/tls.h"
14 #include "eap_common/eap_tlv_common.h"
15 #include "eap_common/eap_peap_common.h"
17 #include "eap_tls_common.h"
18 #include "eap_config.h"
22 /* Maximum supported PEAP version
23 * 0 = Microsoft's PEAP version 0; draft-kamath-pppext-peapv0-00.txt
24 * 1 = draft-josefsson-ppext-eap-tls-eap-05.txt
25 * 2 = draft-josefsson-ppext-eap-tls-eap-10.txt
27 #define EAP_PEAP_VERSION 1
30 static void eap_peap_deinit(struct eap_sm *sm, void *priv);
33 struct eap_peap_data {
34 struct eap_ssl_data ssl;
36 int peap_version, force_peap_version, force_new_label;
38 const struct eap_method *phase2_method;
41 int phase2_eap_success;
42 int phase2_eap_started;
44 struct eap_method_type phase2_type;
45 struct eap_method_type *phase2_types;
46 size_t num_phase2_types;
48 int peap_outer_success; /* 0 = PEAP terminated on Phase 2 inner
50 * 1 = reply with tunneled EAP-Success to inner
51 * EAP-Success and expect AS to send outer
52 * (unencrypted) EAP-Success after this
53 * 2 = reply with PEAP/TLS ACK to inner
54 * EAP-Success and expect AS to send outer
55 * (unencrypted) EAP-Success after this */
56 int resuming; /* starting a resumed session */
57 int reauth; /* reauthentication */
60 struct wpabuf *pending_phase2_req;
61 enum { NO_BINDING, OPTIONAL_BINDING, REQUIRE_BINDING } crypto_binding;
62 int crypto_binding_used;
66 int soh; /* Whether IF-TNCCS-SOH (Statement of Health; Microsoft NAP)
71 static int eap_peap_parse_phase1(struct eap_peap_data *data,
76 pos = os_strstr(phase1, "peapver=");
78 data->force_peap_version = atoi(pos + 8);
79 data->peap_version = data->force_peap_version;
80 wpa_printf(MSG_DEBUG, "EAP-PEAP: Forced PEAP version %d",
81 data->force_peap_version);
84 if (os_strstr(phase1, "peaplabel=1")) {
85 data->force_new_label = 1;
86 wpa_printf(MSG_DEBUG, "EAP-PEAP: Force new label for key "
90 if (os_strstr(phase1, "peap_outer_success=0")) {
91 data->peap_outer_success = 0;
92 wpa_printf(MSG_DEBUG, "EAP-PEAP: terminate authentication on "
93 "tunneled EAP-Success");
94 } else if (os_strstr(phase1, "peap_outer_success=1")) {
95 data->peap_outer_success = 1;
96 wpa_printf(MSG_DEBUG, "EAP-PEAP: send tunneled EAP-Success "
97 "after receiving tunneled EAP-Success");
98 } else if (os_strstr(phase1, "peap_outer_success=2")) {
99 data->peap_outer_success = 2;
100 wpa_printf(MSG_DEBUG, "EAP-PEAP: send PEAP/TLS ACK after "
101 "receiving tunneled EAP-Success");
104 if (os_strstr(phase1, "crypto_binding=0")) {
105 data->crypto_binding = NO_BINDING;
106 wpa_printf(MSG_DEBUG, "EAP-PEAP: Do not use cryptobinding");
107 } else if (os_strstr(phase1, "crypto_binding=1")) {
108 data->crypto_binding = OPTIONAL_BINDING;
109 wpa_printf(MSG_DEBUG, "EAP-PEAP: Optional cryptobinding");
110 } else if (os_strstr(phase1, "crypto_binding=2")) {
111 data->crypto_binding = REQUIRE_BINDING;
112 wpa_printf(MSG_DEBUG, "EAP-PEAP: Require cryptobinding");
116 if (os_strstr(phase1, "tnc=soh2")) {
118 wpa_printf(MSG_DEBUG, "EAP-PEAP: SoH version 2 enabled");
119 } else if (os_strstr(phase1, "tnc=soh1")) {
121 wpa_printf(MSG_DEBUG, "EAP-PEAP: SoH version 1 enabled");
122 } else if (os_strstr(phase1, "tnc=soh")) {
124 wpa_printf(MSG_DEBUG, "EAP-PEAP: SoH version 2 enabled");
132 static void * eap_peap_init(struct eap_sm *sm)
134 struct eap_peap_data *data;
135 struct eap_peer_config *config = eap_get_config(sm);
137 data = os_zalloc(sizeof(*data));
140 sm->peap_done = FALSE;
141 data->peap_version = EAP_PEAP_VERSION;
142 data->force_peap_version = -1;
143 data->peap_outer_success = 2;
144 data->crypto_binding = OPTIONAL_BINDING;
146 if (config && config->phase1 &&
147 eap_peap_parse_phase1(data, config->phase1) < 0) {
148 eap_peap_deinit(sm, data);
152 if (eap_peer_select_phase2_methods(config, "auth=",
154 &data->num_phase2_types) < 0) {
155 eap_peap_deinit(sm, data);
159 data->phase2_type.vendor = EAP_VENDOR_IETF;
160 data->phase2_type.method = EAP_TYPE_NONE;
162 if (eap_peer_tls_ssl_init(sm, &data->ssl, config, EAP_TYPE_PEAP)) {
163 wpa_printf(MSG_INFO, "EAP-PEAP: Failed to initialize SSL.");
164 eap_peap_deinit(sm, data);
172 static void eap_peap_deinit(struct eap_sm *sm, void *priv)
174 struct eap_peap_data *data = priv;
177 if (data->phase2_priv && data->phase2_method)
178 data->phase2_method->deinit(sm, data->phase2_priv);
179 os_free(data->phase2_types);
180 eap_peer_tls_ssl_deinit(sm, &data->ssl);
181 os_free(data->key_data);
182 wpabuf_free(data->pending_phase2_req);
188 * eap_tlv_build_nak - Build EAP-TLV NAK message
189 * @id: EAP identifier for the header
190 * @nak_type: TLV type (EAP_TLV_*)
191 * Returns: Buffer to the allocated EAP-TLV NAK message or %NULL on failure
193 * This function builds an EAP-TLV NAK message. The caller is responsible for
194 * freeing the returned buffer.
196 static struct wpabuf * eap_tlv_build_nak(int id, u16 nak_type)
200 msg = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TLV, 10,
201 EAP_CODE_RESPONSE, id);
205 wpabuf_put_u8(msg, 0x80); /* Mandatory */
206 wpabuf_put_u8(msg, EAP_TLV_NAK_TLV);
207 wpabuf_put_be16(msg, 6); /* Length */
208 wpabuf_put_be32(msg, 0); /* Vendor-Id */
209 wpabuf_put_be16(msg, nak_type); /* NAK-Type */
215 static int eap_peap_get_isk(struct eap_sm *sm, struct eap_peap_data *data,
216 u8 *isk, size_t isk_len)
221 os_memset(isk, 0, isk_len);
222 if (data->phase2_method == NULL || data->phase2_priv == NULL ||
223 data->phase2_method->isKeyAvailable == NULL ||
224 data->phase2_method->getKey == NULL)
227 if (!data->phase2_method->isKeyAvailable(sm, data->phase2_priv) ||
228 (key = data->phase2_method->getKey(sm, data->phase2_priv,
229 &key_len)) == NULL) {
230 wpa_printf(MSG_DEBUG, "EAP-PEAP: Could not get key material "
235 if (key_len > isk_len)
237 os_memcpy(isk, key, key_len);
244 static int eap_peap_derive_cmk(struct eap_sm *sm, struct eap_peap_data *data)
247 u8 isk[32], imck[60];
250 * Tunnel key (TK) is the first 60 octets of the key generated by
251 * phase 1 of PEAP (based on TLS).
256 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: TK", tk, 60);
259 tls_connection_resumed(sm->ssl_ctx, data->ssl.conn)) {
260 /* Fast-connect: IPMK|CMK = TK */
261 os_memcpy(data->ipmk, tk, 40);
262 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: IPMK from TK",
264 os_memcpy(data->cmk, tk + 40, 20);
265 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: CMK from TK",
270 if (eap_peap_get_isk(sm, data, isk, sizeof(isk)) < 0)
272 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: ISK", isk, sizeof(isk));
275 * IPMK Seed = "Inner Methods Compound Keys" | ISK
276 * TempKey = First 40 octets of TK
277 * IPMK|CMK = PRF+(TempKey, IPMK Seed, 60)
278 * (note: draft-josefsson-pppext-eap-tls-eap-10.txt includes a space
279 * in the end of the label just before ISK; is that just a typo?)
281 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: TempKey", tk, 40);
282 if (peap_prfplus(data->peap_version, tk, 40,
283 "Inner Methods Compound Keys",
284 isk, sizeof(isk), imck, sizeof(imck)) < 0)
286 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: IMCK (IPMKj)",
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;
308 /* Compound_MAC: HMAC-SHA1-160(cryptobinding TLV | EAP type) */
309 addr[0] = wpabuf_put(buf, 0);
314 tlv_type = EAP_TLV_CRYPTO_BINDING_TLV;
315 if (data->peap_version >= 2)
316 tlv_type |= EAP_TLV_TYPE_MANDATORY;
317 wpabuf_put_be16(buf, tlv_type);
318 wpabuf_put_be16(buf, 56);
320 wpabuf_put_u8(buf, 0); /* Reserved */
321 wpabuf_put_u8(buf, data->peap_version); /* Version */
322 wpabuf_put_u8(buf, data->peap_version); /* RecvVersion */
323 wpabuf_put_u8(buf, 1); /* SubType: 0 = Request, 1 = Response */
324 wpabuf_put_data(buf, data->binding_nonce, 32); /* Nonce */
325 mac = wpabuf_put(buf, 20); /* Compound_MAC */
326 wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC CMK", data->cmk, 20);
327 wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC data 1",
329 wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC data 2",
331 hmac_sha1_vector(data->cmk, 20, 2, addr, len, mac);
332 wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC", mac, SHA1_MAC_LEN);
333 data->crypto_binding_used = 1;
340 * eap_tlv_build_result - Build EAP-TLV Result message
341 * @id: EAP identifier for the header
342 * @status: Status (EAP_TLV_RESULT_SUCCESS or EAP_TLV_RESULT_FAILURE)
343 * Returns: Buffer to the allocated EAP-TLV Result message or %NULL on failure
345 * This function builds an EAP-TLV Result message. The caller is responsible
346 * for freeing the returned buffer.
348 static struct wpabuf * eap_tlv_build_result(struct eap_sm *sm,
349 struct eap_peap_data *data,
356 if (data->crypto_binding == NO_BINDING)
361 len += 60; /* Cryptobinding TLV */
362 msg = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TLV, len,
363 EAP_CODE_RESPONSE, id);
367 wpabuf_put_u8(msg, 0x80); /* Mandatory */
368 wpabuf_put_u8(msg, EAP_TLV_RESULT_TLV);
369 wpabuf_put_be16(msg, 2); /* Length */
370 wpabuf_put_be16(msg, status); /* Status */
372 if (crypto_tlv_used && eap_tlv_add_cryptobinding(sm, data, msg)) {
381 static int eap_tlv_validate_cryptobinding(struct eap_sm *sm,
382 struct eap_peap_data *data,
383 const u8 *crypto_tlv,
384 size_t crypto_tlv_len)
386 u8 buf[61], mac[SHA1_MAC_LEN];
389 if (eap_peap_derive_cmk(sm, data) < 0) {
390 wpa_printf(MSG_DEBUG, "EAP-PEAP: Could not derive CMK");
394 if (crypto_tlv_len != 4 + 56) {
395 wpa_printf(MSG_DEBUG, "EAP-PEAP: Invalid cryptobinding TLV "
396 "length %d", (int) crypto_tlv_len);
401 pos += 4; /* TLV header */
402 if (pos[1] != data->peap_version) {
403 wpa_printf(MSG_DEBUG, "EAP-PEAP: Cryptobinding TLV Version "
404 "mismatch (was %d; expected %d)",
405 pos[1], data->peap_version);
410 wpa_printf(MSG_DEBUG, "EAP-PEAP: Unexpected Cryptobinding TLV "
411 "SubType %d", pos[3]);
415 os_memcpy(data->binding_nonce, pos, 32);
416 pos += 32; /* Nonce */
418 /* Compound_MAC: HMAC-SHA1-160(cryptobinding TLV | EAP type) */
419 os_memcpy(buf, crypto_tlv, 60);
420 os_memset(buf + 4 + 4 + 32, 0, 20); /* Compound_MAC */
421 buf[60] = EAP_TYPE_PEAP;
422 wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Compound_MAC data",
424 hmac_sha1(data->cmk, 20, buf, sizeof(buf), mac);
426 if (os_memcmp(mac, pos, SHA1_MAC_LEN) != 0) {
427 wpa_printf(MSG_DEBUG, "EAP-PEAP: Invalid Compound_MAC in "
428 "cryptobinding TLV");
429 wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Received MAC",
431 wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Expected MAC",
436 wpa_printf(MSG_DEBUG, "EAP-PEAP: Valid cryptobinding TLV received");
443 * eap_tlv_process - Process a received EAP-TLV message and generate a response
444 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
445 * @ret: Return values from EAP request validation and processing
446 * @req: EAP-TLV request to be processed. The caller must have validated that
447 * the buffer is large enough to contain full request (hdr->length bytes) and
448 * that the EAP type is EAP_TYPE_TLV.
449 * @resp: Buffer to return a pointer to the allocated response message. This
450 * field should be initialized to %NULL before the call. The value will be
451 * updated if a response message is generated. The caller is responsible for
452 * freeing the allocated message.
453 * @force_failure: Force negotiation to fail
454 * Returns: 0 on success, -1 on failure
456 static int eap_tlv_process(struct eap_sm *sm, struct eap_peap_data *data,
457 struct eap_method_ret *ret,
458 const struct wpabuf *req, struct wpabuf **resp,
461 size_t left, tlv_len;
463 const u8 *result_tlv = NULL, *crypto_tlv = NULL;
464 size_t result_tlv_len = 0, crypto_tlv_len = 0;
465 int tlv_type, mandatory;
468 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_TLV, req, &left);
471 wpa_hexdump(MSG_DEBUG, "EAP-TLV: Received TLVs", pos, left);
473 mandatory = !!(pos[0] & 0x80);
474 tlv_type = WPA_GET_BE16(pos) & 0x3fff;
476 tlv_len = WPA_GET_BE16(pos);
479 if (tlv_len > left) {
480 wpa_printf(MSG_DEBUG, "EAP-TLV: TLV underrun "
481 "(tlv_len=%lu left=%lu)",
482 (unsigned long) tlv_len,
483 (unsigned long) left);
487 case EAP_TLV_RESULT_TLV:
489 result_tlv_len = tlv_len;
491 case EAP_TLV_CRYPTO_BINDING_TLV:
493 crypto_tlv_len = tlv_len;
496 wpa_printf(MSG_DEBUG, "EAP-TLV: Unsupported TLV Type "
498 mandatory ? " (mandatory)" : "");
500 /* NAK TLV and ignore all TLVs in this packet.
502 *resp = eap_tlv_build_nak(eap_get_id(req),
504 return *resp == NULL ? -1 : 0;
506 /* Ignore this TLV, but process other TLVs */
514 wpa_printf(MSG_DEBUG, "EAP-TLV: Last TLV too short in "
515 "Request (left=%lu)", (unsigned long) left);
519 /* Process supported TLVs */
520 if (crypto_tlv && data->crypto_binding != NO_BINDING) {
521 wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Cryptobinding TLV",
522 crypto_tlv, crypto_tlv_len);
523 if (eap_tlv_validate_cryptobinding(sm, data, crypto_tlv - 4,
524 crypto_tlv_len + 4) < 0) {
525 if (result_tlv == NULL)
528 crypto_tlv = NULL; /* do not include Cryptobinding TLV
529 * in response, if the received
530 * cryptobinding was invalid. */
532 } else if (!crypto_tlv && data->crypto_binding == REQUIRE_BINDING) {
533 wpa_printf(MSG_DEBUG, "EAP-PEAP: No cryptobinding TLV");
538 int status, resp_status;
539 wpa_hexdump(MSG_DEBUG, "EAP-TLV: Result TLV",
540 result_tlv, result_tlv_len);
541 if (result_tlv_len < 2) {
542 wpa_printf(MSG_INFO, "EAP-TLV: Too short Result TLV "
544 (unsigned long) result_tlv_len);
547 status = WPA_GET_BE16(result_tlv);
548 if (status == EAP_TLV_RESULT_SUCCESS) {
549 wpa_printf(MSG_INFO, "EAP-TLV: TLV Result - Success "
550 "- EAP-TLV/Phase2 Completed");
552 wpa_printf(MSG_INFO, "EAP-TLV: Earlier failure"
553 " - force failed Phase 2");
554 resp_status = EAP_TLV_RESULT_FAILURE;
555 ret->decision = DECISION_FAIL;
557 resp_status = EAP_TLV_RESULT_SUCCESS;
558 ret->decision = DECISION_UNCOND_SUCC;
560 } else if (status == EAP_TLV_RESULT_FAILURE) {
561 wpa_printf(MSG_INFO, "EAP-TLV: TLV Result - Failure");
562 resp_status = EAP_TLV_RESULT_FAILURE;
563 ret->decision = DECISION_FAIL;
565 wpa_printf(MSG_INFO, "EAP-TLV: Unknown TLV Result "
566 "Status %d", status);
567 resp_status = EAP_TLV_RESULT_FAILURE;
568 ret->decision = DECISION_FAIL;
570 ret->methodState = METHOD_DONE;
572 *resp = eap_tlv_build_result(sm, data, crypto_tlv != NULL,
573 eap_get_id(req), resp_status);
580 static struct wpabuf * eap_peapv2_tlv_eap_payload(struct wpabuf *buf)
583 struct eap_tlv_hdr *tlv;
588 /* Encapsulate EAP packet in EAP-Payload TLV */
589 wpa_printf(MSG_DEBUG, "EAP-PEAPv2: Add EAP-Payload TLV");
590 e = wpabuf_alloc(sizeof(*tlv) + wpabuf_len(buf));
592 wpa_printf(MSG_DEBUG, "EAP-PEAPv2: Failed to allocate memory "
593 "for TLV encapsulation");
597 tlv = wpabuf_put(e, sizeof(*tlv));
598 tlv->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
599 EAP_TLV_EAP_PAYLOAD_TLV);
600 tlv->length = host_to_be16(wpabuf_len(buf));
601 wpabuf_put_buf(e, buf);
607 static int eap_peap_phase2_request(struct eap_sm *sm,
608 struct eap_peap_data *data,
609 struct eap_method_ret *ret,
611 struct wpabuf **resp)
613 struct eap_hdr *hdr = wpabuf_mhead(req);
614 size_t len = be_to_host16(hdr->length);
616 struct eap_method_ret iret;
617 struct eap_peer_config *config = eap_get_config(sm);
619 if (len <= sizeof(struct eap_hdr)) {
620 wpa_printf(MSG_INFO, "EAP-PEAP: too short "
621 "Phase 2 request (len=%lu)", (unsigned long) len);
624 pos = (u8 *) (hdr + 1);
625 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 Request: type=%d", *pos);
627 case EAP_TYPE_IDENTITY:
628 *resp = eap_sm_buildIdentity(sm, hdr->identifier, 1);
631 os_memset(&iret, 0, sizeof(iret));
632 if (eap_tlv_process(sm, data, &iret, req, resp,
633 data->phase2_eap_started &&
634 !data->phase2_eap_success)) {
635 ret->methodState = METHOD_DONE;
636 ret->decision = DECISION_FAIL;
639 if (iret.methodState == METHOD_DONE ||
640 iret.methodState == METHOD_MAY_CONT) {
641 ret->methodState = iret.methodState;
642 ret->decision = iret.decision;
643 data->phase2_success = 1;
646 case EAP_TYPE_EXPANDED:
652 epos = eap_hdr_validate(EAP_VENDOR_MICROSOFT, 0x21,
656 wpa_printf(MSG_DEBUG,
657 "EAP-PEAP: SoH EAP Extensions");
658 buf = tncc_process_soh_request(data->soh,
661 *resp = eap_msg_alloc(
662 EAP_VENDOR_MICROSOFT, 0x21,
667 ret->methodState = METHOD_DONE;
668 ret->decision = DECISION_FAIL;
671 wpabuf_put_buf(*resp, buf);
680 if (data->phase2_type.vendor == EAP_VENDOR_IETF &&
681 data->phase2_type.method == EAP_TYPE_NONE) {
683 for (i = 0; i < data->num_phase2_types; i++) {
684 if (data->phase2_types[i].vendor !=
686 data->phase2_types[i].method != *pos)
689 data->phase2_type.vendor =
690 data->phase2_types[i].vendor;
691 data->phase2_type.method =
692 data->phase2_types[i].method;
693 wpa_printf(MSG_DEBUG, "EAP-PEAP: Selected "
694 "Phase 2 EAP vendor %d method %d",
695 data->phase2_type.vendor,
696 data->phase2_type.method);
700 if (*pos != data->phase2_type.method ||
701 *pos == EAP_TYPE_NONE) {
702 if (eap_peer_tls_phase2_nak(data->phase2_types,
703 data->num_phase2_types,
709 if (data->phase2_priv == NULL) {
710 data->phase2_method = eap_peer_get_eap_method(
711 data->phase2_type.vendor,
712 data->phase2_type.method);
713 if (data->phase2_method) {
716 data->phase2_method->init(sm);
720 if (data->phase2_priv == NULL || data->phase2_method == NULL) {
721 wpa_printf(MSG_INFO, "EAP-PEAP: failed to initialize "
722 "Phase 2 EAP method %d", *pos);
723 ret->methodState = METHOD_DONE;
724 ret->decision = DECISION_FAIL;
727 data->phase2_eap_started = 1;
728 os_memset(&iret, 0, sizeof(iret));
729 *resp = data->phase2_method->process(sm, data->phase2_priv,
731 if ((iret.methodState == METHOD_DONE ||
732 iret.methodState == METHOD_MAY_CONT) &&
733 (iret.decision == DECISION_UNCOND_SUCC ||
734 iret.decision == DECISION_COND_SUCC)) {
735 data->phase2_eap_success = 1;
736 data->phase2_success = 1;
742 (config->pending_req_identity || config->pending_req_password ||
743 config->pending_req_otp || config->pending_req_new_password)) {
744 wpabuf_free(data->pending_phase2_req);
745 data->pending_phase2_req = wpabuf_alloc_copy(hdr, len);
752 static int eap_peap_decrypt(struct eap_sm *sm, struct eap_peap_data *data,
753 struct eap_method_ret *ret,
754 const struct eap_hdr *req,
755 const struct wpabuf *in_data,
756 struct wpabuf **out_data)
758 struct wpabuf *in_decrypted = NULL;
759 int res, skip_change = 0;
760 struct eap_hdr *hdr, *rhdr;
761 struct wpabuf *resp = NULL;
764 wpa_printf(MSG_DEBUG, "EAP-PEAP: received %lu bytes encrypted data for"
765 " Phase 2", (unsigned long) wpabuf_len(in_data));
767 if (data->pending_phase2_req) {
768 wpa_printf(MSG_DEBUG, "EAP-PEAP: Pending Phase 2 request - "
769 "skip decryption and use old data");
770 /* Clear TLS reassembly state. */
771 eap_peer_tls_reset_input(&data->ssl);
772 in_decrypted = data->pending_phase2_req;
773 data->pending_phase2_req = NULL;
778 if (wpabuf_len(in_data) == 0 && sm->workaround &&
779 data->phase2_success) {
781 * Cisco ACS seems to be using TLS ACK to terminate
782 * EAP-PEAPv0/GTC. Try to reply with TLS ACK.
784 wpa_printf(MSG_DEBUG, "EAP-PEAP: Received TLS ACK, but "
785 "expected data - acknowledge with TLS ACK since "
786 "Phase 2 has been completed");
787 ret->decision = DECISION_COND_SUCC;
788 ret->methodState = METHOD_DONE;
790 } else if (wpabuf_len(in_data) == 0) {
791 /* Received TLS ACK - requesting more fragments */
792 return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_PEAP,
794 req->identifier, NULL, out_data);
797 res = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
802 wpa_hexdump_buf(MSG_DEBUG, "EAP-PEAP: Decrypted Phase 2 EAP",
805 hdr = wpabuf_mhead(in_decrypted);
806 if (wpabuf_len(in_decrypted) == 5 && hdr->code == EAP_CODE_REQUEST &&
807 be_to_host16(hdr->length) == 5 &&
808 eap_get_type(in_decrypted) == EAP_TYPE_IDENTITY) {
809 /* At least FreeRADIUS seems to send full EAP header with
810 * EAP Request Identity */
813 if (wpabuf_len(in_decrypted) >= 5 && hdr->code == EAP_CODE_REQUEST &&
814 eap_get_type(in_decrypted) == EAP_TYPE_TLV) {
818 if (data->peap_version == 0 && !skip_change) {
819 struct eap_hdr *nhdr;
820 struct wpabuf *nmsg = wpabuf_alloc(sizeof(struct eap_hdr) +
821 wpabuf_len(in_decrypted));
823 wpabuf_free(in_decrypted);
826 nhdr = wpabuf_put(nmsg, sizeof(*nhdr));
827 wpabuf_put_buf(nmsg, in_decrypted);
828 nhdr->code = req->code;
829 nhdr->identifier = req->identifier;
830 nhdr->length = host_to_be16(sizeof(struct eap_hdr) +
831 wpabuf_len(in_decrypted));
833 wpabuf_free(in_decrypted);
837 if (data->peap_version >= 2) {
838 struct eap_tlv_hdr *tlv;
841 if (wpabuf_len(in_decrypted) < sizeof(*tlv) + sizeof(*hdr)) {
842 wpa_printf(MSG_INFO, "EAP-PEAPv2: Too short Phase 2 "
844 wpabuf_free(in_decrypted);
847 tlv = wpabuf_mhead(in_decrypted);
848 if ((be_to_host16(tlv->tlv_type) & 0x3fff) !=
849 EAP_TLV_EAP_PAYLOAD_TLV) {
850 wpa_printf(MSG_INFO, "EAP-PEAPv2: Not an EAP TLV");
851 wpabuf_free(in_decrypted);
854 if (sizeof(*tlv) + be_to_host16(tlv->length) >
855 wpabuf_len(in_decrypted)) {
856 wpa_printf(MSG_INFO, "EAP-PEAPv2: Invalid EAP TLV "
858 wpabuf_free(in_decrypted);
861 hdr = (struct eap_hdr *) (tlv + 1);
862 if (be_to_host16(hdr->length) > be_to_host16(tlv->length)) {
863 wpa_printf(MSG_INFO, "EAP-PEAPv2: No room for full "
864 "EAP packet in EAP TLV");
865 wpabuf_free(in_decrypted);
869 nmsg = wpabuf_alloc(be_to_host16(hdr->length));
871 wpabuf_free(in_decrypted);
875 wpabuf_put_data(nmsg, hdr, be_to_host16(hdr->length));
876 wpabuf_free(in_decrypted);
880 hdr = wpabuf_mhead(in_decrypted);
881 if (wpabuf_len(in_decrypted) < sizeof(*hdr)) {
882 wpa_printf(MSG_INFO, "EAP-PEAP: Too short Phase 2 "
883 "EAP frame (len=%lu)",
884 (unsigned long) wpabuf_len(in_decrypted));
885 wpabuf_free(in_decrypted);
888 len = be_to_host16(hdr->length);
889 if (len > wpabuf_len(in_decrypted)) {
890 wpa_printf(MSG_INFO, "EAP-PEAP: Length mismatch in "
891 "Phase 2 EAP frame (len=%lu hdr->length=%lu)",
892 (unsigned long) wpabuf_len(in_decrypted),
893 (unsigned long) len);
894 wpabuf_free(in_decrypted);
897 if (len < wpabuf_len(in_decrypted)) {
898 wpa_printf(MSG_INFO, "EAP-PEAP: Odd.. Phase 2 EAP header has "
899 "shorter length than full decrypted data "
902 (unsigned long) wpabuf_len(in_decrypted));
904 wpa_printf(MSG_DEBUG, "EAP-PEAP: received Phase 2: code=%d "
905 "identifier=%d length=%lu", hdr->code, hdr->identifier,
906 (unsigned long) len);
908 case EAP_CODE_REQUEST:
909 if (eap_peap_phase2_request(sm, data, ret, in_decrypted,
911 wpabuf_free(in_decrypted);
912 wpa_printf(MSG_INFO, "EAP-PEAP: Phase2 Request "
913 "processing failed");
917 case EAP_CODE_SUCCESS:
918 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 Success");
919 if (data->peap_version == 1) {
920 /* EAP-Success within TLS tunnel is used to indicate
921 * shutdown of the TLS channel. The authentication has
923 if (data->phase2_eap_started &&
924 !data->phase2_eap_success) {
925 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 "
926 "Success used to indicate success, "
927 "but Phase 2 EAP was not yet "
928 "completed successfully");
929 ret->methodState = METHOD_DONE;
930 ret->decision = DECISION_FAIL;
931 wpabuf_free(in_decrypted);
934 wpa_printf(MSG_DEBUG, "EAP-PEAP: Version 1 - "
935 "EAP-Success within TLS tunnel - "
936 "authentication completed");
937 ret->decision = DECISION_UNCOND_SUCC;
938 ret->methodState = METHOD_DONE;
939 data->phase2_success = 1;
940 if (data->peap_outer_success == 2) {
941 wpabuf_free(in_decrypted);
942 wpa_printf(MSG_DEBUG, "EAP-PEAP: Use TLS ACK "
943 "to finish authentication");
945 } else if (data->peap_outer_success == 1) {
946 /* Reply with EAP-Success within the TLS
947 * channel to complete the authentication. */
948 resp = wpabuf_alloc(sizeof(struct eap_hdr));
950 rhdr = wpabuf_put(resp, sizeof(*rhdr));
951 rhdr->code = EAP_CODE_SUCCESS;
952 rhdr->identifier = hdr->identifier;
954 host_to_be16(sizeof(*rhdr));
957 /* No EAP-Success expected for Phase 1 (outer,
958 * unencrypted auth), so force EAP state
959 * machine to SUCCESS state. */
960 sm->peap_done = TRUE;
966 case EAP_CODE_FAILURE:
967 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 Failure");
968 ret->decision = DECISION_FAIL;
969 ret->methodState = METHOD_MAY_CONT;
970 ret->allowNotifications = FALSE;
971 /* Reply with EAP-Failure within the TLS channel to complete
972 * failure reporting. */
973 resp = wpabuf_alloc(sizeof(struct eap_hdr));
975 rhdr = wpabuf_put(resp, sizeof(*rhdr));
976 rhdr->code = EAP_CODE_FAILURE;
977 rhdr->identifier = hdr->identifier;
978 rhdr->length = host_to_be16(sizeof(*rhdr));
982 wpa_printf(MSG_INFO, "EAP-PEAP: Unexpected code=%d in "
983 "Phase 2 EAP header", hdr->code);
987 wpabuf_free(in_decrypted);
990 int skip_change2 = 0;
991 struct wpabuf *rmsg, buf;
993 wpa_hexdump_buf_key(MSG_DEBUG,
994 "EAP-PEAP: Encrypting Phase 2 data", resp);
995 /* PEAP version changes */
996 if (data->peap_version >= 2) {
997 resp = eap_peapv2_tlv_eap_payload(resp);
1001 if (wpabuf_len(resp) >= 5 &&
1002 wpabuf_head_u8(resp)[0] == EAP_CODE_RESPONSE &&
1003 eap_get_type(resp) == EAP_TYPE_TLV)
1006 if (data->peap_version == 0 && !skip_change2) {
1007 wpabuf_set(&buf, wpabuf_head_u8(resp) +
1008 sizeof(struct eap_hdr),
1009 wpabuf_len(resp) - sizeof(struct eap_hdr));
1013 if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_PEAP,
1014 data->peap_version, req->identifier,
1016 wpa_printf(MSG_INFO, "EAP-PEAP: Failed to encrypt "
1026 static struct wpabuf * eap_peap_process(struct eap_sm *sm, void *priv,
1027 struct eap_method_ret *ret,
1028 const struct wpabuf *reqData)
1030 const struct eap_hdr *req;
1034 struct wpabuf *resp;
1036 struct eap_peap_data *data = priv;
1038 pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_PEAP, ret,
1039 reqData, &left, &flags);
1042 req = wpabuf_head(reqData);
1043 id = req->identifier;
1045 if (flags & EAP_TLS_FLAGS_START) {
1046 wpa_printf(MSG_DEBUG, "EAP-PEAP: Start (server ver=%d, own "
1047 "ver=%d)", flags & EAP_TLS_VERSION_MASK,
1048 data->peap_version);
1049 if ((flags & EAP_TLS_VERSION_MASK) < data->peap_version)
1050 data->peap_version = flags & EAP_TLS_VERSION_MASK;
1051 if (data->force_peap_version >= 0 &&
1052 data->force_peap_version != data->peap_version) {
1053 wpa_printf(MSG_WARNING, "EAP-PEAP: Failed to select "
1054 "forced PEAP version %d",
1055 data->force_peap_version);
1056 ret->methodState = METHOD_DONE;
1057 ret->decision = DECISION_FAIL;
1058 ret->allowNotifications = FALSE;
1061 wpa_printf(MSG_DEBUG, "EAP-PEAP: Using PEAP version %d",
1062 data->peap_version);
1063 left = 0; /* make sure that this frame is empty, even though it
1064 * should always be, anyway */
1068 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1071 wpabuf_set(&msg, pos, left);
1072 res = eap_peap_decrypt(sm, data, ret, req, &msg, &resp);
1074 res = eap_peer_tls_process_helper(sm, &data->ssl,
1076 data->peap_version, id, pos,
1079 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
1081 wpa_printf(MSG_DEBUG,
1082 "EAP-PEAP: TLS done, proceed to Phase 2");
1083 os_free(data->key_data);
1084 /* draft-josefsson-ppext-eap-tls-eap-05.txt
1085 * specifies that PEAPv1 would use "client PEAP
1086 * encryption" as the label. However, most existing
1087 * PEAPv1 implementations seem to be using the old
1088 * label, "client EAP encryption", instead. Use the old
1089 * label by default, but allow it to be configured with
1090 * phase1 parameter peaplabel=1. */
1091 if (data->peap_version > 1 || data->force_new_label)
1092 label = "client PEAP encryption";
1094 label = "client EAP encryption";
1095 wpa_printf(MSG_DEBUG, "EAP-PEAP: using label '%s' in "
1096 "key derivation", label);
1098 eap_peer_tls_derive_key(sm, &data->ssl, label,
1100 if (data->key_data) {
1101 wpa_hexdump_key(MSG_DEBUG,
1102 "EAP-PEAP: Derived key",
1106 wpa_printf(MSG_DEBUG, "EAP-PEAP: Failed to "
1110 if (sm->workaround && data->resuming) {
1112 * At least few RADIUS servers (Aegis v1.1.6;
1113 * but not v1.1.4; and Cisco ACS) seem to be
1114 * terminating PEAPv1 (Aegis) or PEAPv0 (Cisco
1115 * ACS) session resumption with outer
1116 * EAP-Success. This does not seem to follow
1117 * draft-josefsson-pppext-eap-tls-eap-05.txt
1118 * section 4.2, so only allow this if EAP
1119 * workarounds are enabled.
1121 wpa_printf(MSG_DEBUG, "EAP-PEAP: Workaround - "
1122 "allow outer EAP-Success to "
1123 "terminate PEAP resumption");
1124 ret->decision = DECISION_COND_SUCC;
1125 data->phase2_success = 1;
1134 * Application data included in the handshake message.
1136 wpabuf_free(data->pending_phase2_req);
1137 data->pending_phase2_req = resp;
1139 wpabuf_set(&msg, pos, left);
1140 res = eap_peap_decrypt(sm, data, ret, req, &msg,
1145 if (ret->methodState == METHOD_DONE) {
1146 ret->allowNotifications = FALSE;
1151 return eap_peer_tls_build_ack(id, EAP_TYPE_PEAP,
1152 data->peap_version);
1159 static Boolean eap_peap_has_reauth_data(struct eap_sm *sm, void *priv)
1161 struct eap_peap_data *data = priv;
1162 return tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1163 data->phase2_success;
1167 static void eap_peap_deinit_for_reauth(struct eap_sm *sm, void *priv)
1169 struct eap_peap_data *data = priv;
1170 wpabuf_free(data->pending_phase2_req);
1171 data->pending_phase2_req = NULL;
1172 data->crypto_binding_used = 0;
1176 static void * eap_peap_init_for_reauth(struct eap_sm *sm, void *priv)
1178 struct eap_peap_data *data = priv;
1179 os_free(data->key_data);
1180 data->key_data = NULL;
1181 if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
1185 if (data->phase2_priv && data->phase2_method &&
1186 data->phase2_method->init_for_reauth)
1187 data->phase2_method->init_for_reauth(sm, data->phase2_priv);
1188 data->phase2_success = 0;
1189 data->phase2_eap_success = 0;
1190 data->phase2_eap_started = 0;
1193 sm->peap_done = FALSE;
1198 static int eap_peap_get_status(struct eap_sm *sm, void *priv, char *buf,
1199 size_t buflen, int verbose)
1201 struct eap_peap_data *data = priv;
1204 len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
1205 if (data->phase2_method) {
1206 ret = os_snprintf(buf + len, buflen - len,
1207 "EAP-PEAPv%d Phase2 method=%s\n",
1209 data->phase2_method->name);
1210 if (ret < 0 || (size_t) ret >= buflen - len)
1218 static Boolean eap_peap_isKeyAvailable(struct eap_sm *sm, void *priv)
1220 struct eap_peap_data *data = priv;
1221 return data->key_data != NULL && data->phase2_success;
1225 static u8 * eap_peap_getKey(struct eap_sm *sm, void *priv, size_t *len)
1227 struct eap_peap_data *data = priv;
1230 if (data->key_data == NULL || !data->phase2_success)
1233 key = os_malloc(EAP_TLS_KEY_LEN);
1237 *len = EAP_TLS_KEY_LEN;
1239 if (data->crypto_binding_used) {
1242 * Note: It looks like Microsoft implementation requires null
1243 * termination for this label while the one used for deriving
1244 * IPMK|CMK did not use null termination.
1246 if (peap_prfplus(data->peap_version, data->ipmk, 40,
1247 "Session Key Generating Function",
1248 (u8 *) "\00", 1, csk, sizeof(csk)) < 0) {
1252 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: CSK", csk, sizeof(csk));
1253 os_memcpy(key, csk, EAP_TLS_KEY_LEN);
1254 wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Derived key",
1255 key, EAP_TLS_KEY_LEN);
1257 os_memcpy(key, data->key_data, EAP_TLS_KEY_LEN);
1263 int eap_peer_peap_register(void)
1265 struct eap_method *eap;
1268 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
1269 EAP_VENDOR_IETF, EAP_TYPE_PEAP, "PEAP");
1273 eap->init = eap_peap_init;
1274 eap->deinit = eap_peap_deinit;
1275 eap->process = eap_peap_process;
1276 eap->isKeyAvailable = eap_peap_isKeyAvailable;
1277 eap->getKey = eap_peap_getKey;
1278 eap->get_status = eap_peap_get_status;
1279 eap->has_reauth_data = eap_peap_has_reauth_data;
1280 eap->deinit_for_reauth = eap_peap_deinit_for_reauth;
1281 eap->init_for_reauth = eap_peap_init_for_reauth;
1283 ret = eap_peer_method_register(eap);
1285 eap_peer_method_free(eap);