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
26 #define EAP_PEAP_VERSION 1
29 static void eap_peap_deinit(struct eap_sm *sm, void *priv);
32 struct eap_peap_data {
33 struct eap_ssl_data ssl;
35 int peap_version, force_peap_version, force_new_label;
37 const struct eap_method *phase2_method;
40 int phase2_eap_success;
41 int phase2_eap_started;
43 struct eap_method_type phase2_type;
44 struct eap_method_type *phase2_types;
45 size_t num_phase2_types;
47 int peap_outer_success; /* 0 = PEAP terminated on Phase 2 inner
49 * 1 = reply with tunneled EAP-Success to inner
50 * EAP-Success and expect AS to send outer
51 * (unencrypted) EAP-Success after this
52 * 2 = reply with PEAP/TLS ACK to inner
53 * EAP-Success and expect AS to send outer
54 * (unencrypted) EAP-Success after this */
55 int resuming; /* starting a resumed session */
56 int reauth; /* reauthentication */
61 struct wpabuf *pending_phase2_req;
62 enum { NO_BINDING, OPTIONAL_BINDING, REQUIRE_BINDING } crypto_binding;
63 int crypto_binding_used;
67 int soh; /* Whether IF-TNCCS-SOH (Statement of Health; Microsoft NAP)
72 static int eap_peap_parse_phase1(struct eap_peap_data *data,
77 pos = os_strstr(phase1, "peapver=");
79 data->force_peap_version = atoi(pos + 8);
80 data->peap_version = data->force_peap_version;
81 wpa_printf(MSG_DEBUG, "EAP-PEAP: Forced PEAP version %d",
82 data->force_peap_version);
85 if (os_strstr(phase1, "peaplabel=1")) {
86 data->force_new_label = 1;
87 wpa_printf(MSG_DEBUG, "EAP-PEAP: Force new label for key "
91 if (os_strstr(phase1, "peap_outer_success=0")) {
92 data->peap_outer_success = 0;
93 wpa_printf(MSG_DEBUG, "EAP-PEAP: terminate authentication on "
94 "tunneled EAP-Success");
95 } else if (os_strstr(phase1, "peap_outer_success=1")) {
96 data->peap_outer_success = 1;
97 wpa_printf(MSG_DEBUG, "EAP-PEAP: send tunneled EAP-Success "
98 "after receiving tunneled EAP-Success");
99 } else if (os_strstr(phase1, "peap_outer_success=2")) {
100 data->peap_outer_success = 2;
101 wpa_printf(MSG_DEBUG, "EAP-PEAP: send PEAP/TLS ACK after "
102 "receiving tunneled EAP-Success");
105 if (os_strstr(phase1, "crypto_binding=0")) {
106 data->crypto_binding = NO_BINDING;
107 wpa_printf(MSG_DEBUG, "EAP-PEAP: Do not use cryptobinding");
108 } else if (os_strstr(phase1, "crypto_binding=1")) {
109 data->crypto_binding = OPTIONAL_BINDING;
110 wpa_printf(MSG_DEBUG, "EAP-PEAP: Optional cryptobinding");
111 } else if (os_strstr(phase1, "crypto_binding=2")) {
112 data->crypto_binding = REQUIRE_BINDING;
113 wpa_printf(MSG_DEBUG, "EAP-PEAP: Require cryptobinding");
117 if (os_strstr(phase1, "tnc=soh2")) {
119 wpa_printf(MSG_DEBUG, "EAP-PEAP: SoH version 2 enabled");
120 } else if (os_strstr(phase1, "tnc=soh1")) {
122 wpa_printf(MSG_DEBUG, "EAP-PEAP: SoH version 1 enabled");
123 } else if (os_strstr(phase1, "tnc=soh")) {
125 wpa_printf(MSG_DEBUG, "EAP-PEAP: SoH version 2 enabled");
133 static void * eap_peap_init(struct eap_sm *sm)
135 struct eap_peap_data *data;
136 struct eap_peer_config *config = eap_get_config(sm);
138 data = os_zalloc(sizeof(*data));
141 sm->peap_done = FALSE;
142 data->peap_version = EAP_PEAP_VERSION;
143 data->force_peap_version = -1;
144 data->peap_outer_success = 2;
145 data->crypto_binding = OPTIONAL_BINDING;
147 if (config && config->phase1 &&
148 eap_peap_parse_phase1(data, config->phase1) < 0) {
149 eap_peap_deinit(sm, data);
153 if (eap_peer_select_phase2_methods(config, "auth=",
155 &data->num_phase2_types) < 0) {
156 eap_peap_deinit(sm, data);
160 data->phase2_type.vendor = EAP_VENDOR_IETF;
161 data->phase2_type.method = EAP_TYPE_NONE;
163 if (eap_peer_tls_ssl_init(sm, &data->ssl, config, EAP_TYPE_PEAP)) {
164 wpa_printf(MSG_INFO, "EAP-PEAP: Failed to initialize SSL.");
165 eap_peap_deinit(sm, data);
173 static void eap_peap_deinit(struct eap_sm *sm, void *priv)
175 struct eap_peap_data *data = priv;
178 if (data->phase2_priv && data->phase2_method)
179 data->phase2_method->deinit(sm, data->phase2_priv);
180 os_free(data->phase2_types);
181 eap_peer_tls_ssl_deinit(sm, &data->ssl);
182 os_free(data->key_data);
183 os_free(data->session_id);
184 wpabuf_free(data->pending_phase2_req);
190 * eap_tlv_build_nak - Build EAP-TLV NAK message
191 * @id: EAP identifier for the header
192 * @nak_type: TLV type (EAP_TLV_*)
193 * Returns: Buffer to the allocated EAP-TLV NAK message or %NULL on failure
195 * This function builds an EAP-TLV NAK message. The caller is responsible for
196 * freeing the returned buffer.
198 static struct wpabuf * eap_tlv_build_nak(int id, u16 nak_type)
202 msg = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TLV, 10,
203 EAP_CODE_RESPONSE, id);
207 wpabuf_put_u8(msg, 0x80); /* Mandatory */
208 wpabuf_put_u8(msg, EAP_TLV_NAK_TLV);
209 wpabuf_put_be16(msg, 6); /* Length */
210 wpabuf_put_be32(msg, 0); /* Vendor-Id */
211 wpabuf_put_be16(msg, nak_type); /* NAK-Type */
217 static int eap_peap_get_isk(struct eap_sm *sm, struct eap_peap_data *data,
218 u8 *isk, size_t isk_len)
223 os_memset(isk, 0, isk_len);
224 if (data->phase2_method == NULL || data->phase2_priv == NULL ||
225 data->phase2_method->isKeyAvailable == NULL ||
226 data->phase2_method->getKey == NULL)
229 if (!data->phase2_method->isKeyAvailable(sm, data->phase2_priv) ||
230 (key = data->phase2_method->getKey(sm, data->phase2_priv,
231 &key_len)) == NULL) {
232 wpa_printf(MSG_DEBUG, "EAP-PEAP: Could not get key material "
237 if (key_len > isk_len)
239 os_memcpy(isk, key, key_len);
246 static int eap_peap_derive_cmk(struct eap_sm *sm, struct eap_peap_data *data)
249 u8 isk[32], imck[60];
252 * Tunnel key (TK) is the first 60 octets of the key generated by
253 * phase 1 of PEAP (based on TLS).
258 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: TK", tk, 60);
261 tls_connection_resumed(sm->ssl_ctx, data->ssl.conn)) {
262 /* Fast-connect: IPMK|CMK = TK */
263 os_memcpy(data->ipmk, tk, 40);
264 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: IPMK from TK",
266 os_memcpy(data->cmk, tk + 40, 20);
267 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: CMK from TK",
272 if (eap_peap_get_isk(sm, data, isk, sizeof(isk)) < 0)
274 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: ISK", isk, sizeof(isk));
277 * IPMK Seed = "Inner Methods Compound Keys" | ISK
278 * TempKey = First 40 octets of TK
279 * IPMK|CMK = PRF+(TempKey, IPMK Seed, 60)
280 * (note: draft-josefsson-pppext-eap-tls-eap-10.txt includes a space
281 * in the end of the label just before ISK; is that just a typo?)
283 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: TempKey", tk, 40);
284 if (peap_prfplus(data->peap_version, tk, 40,
285 "Inner Methods Compound Keys",
286 isk, sizeof(isk), imck, sizeof(imck)) < 0)
288 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: IMCK (IPMKj)",
291 os_memcpy(data->ipmk, imck, 40);
292 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: IPMK (S-IPMKj)", data->ipmk, 40);
293 os_memcpy(data->cmk, imck + 40, 20);
294 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: CMK (CMKj)", data->cmk, 20);
300 static int eap_tlv_add_cryptobinding(struct eap_sm *sm,
301 struct eap_peap_data *data,
305 u8 eap_type = EAP_TYPE_PEAP;
310 /* Compound_MAC: HMAC-SHA1-160(cryptobinding TLV | EAP type) */
311 addr[0] = wpabuf_put(buf, 0);
316 tlv_type = EAP_TLV_CRYPTO_BINDING_TLV;
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 int eap_peap_phase2_request(struct eap_sm *sm,
581 struct eap_peap_data *data,
582 struct eap_method_ret *ret,
584 struct wpabuf **resp)
586 struct eap_hdr *hdr = wpabuf_mhead(req);
587 size_t len = be_to_host16(hdr->length);
589 struct eap_method_ret iret;
590 struct eap_peer_config *config = eap_get_config(sm);
592 if (len <= sizeof(struct eap_hdr)) {
593 wpa_printf(MSG_INFO, "EAP-PEAP: too short "
594 "Phase 2 request (len=%lu)", (unsigned long) len);
597 pos = (u8 *) (hdr + 1);
598 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 Request: type=%d", *pos);
600 case EAP_TYPE_IDENTITY:
601 *resp = eap_sm_buildIdentity(sm, hdr->identifier, 1);
604 os_memset(&iret, 0, sizeof(iret));
605 if (eap_tlv_process(sm, data, &iret, req, resp,
606 data->phase2_eap_started &&
607 !data->phase2_eap_success)) {
608 ret->methodState = METHOD_DONE;
609 ret->decision = DECISION_FAIL;
612 if (iret.methodState == METHOD_DONE ||
613 iret.methodState == METHOD_MAY_CONT) {
614 ret->methodState = iret.methodState;
615 ret->decision = iret.decision;
616 data->phase2_success = 1;
619 case EAP_TYPE_EXPANDED:
625 epos = eap_hdr_validate(EAP_VENDOR_MICROSOFT, 0x21,
629 wpa_printf(MSG_DEBUG,
630 "EAP-PEAP: SoH EAP Extensions");
631 buf = tncc_process_soh_request(data->soh,
634 *resp = eap_msg_alloc(
635 EAP_VENDOR_MICROSOFT, 0x21,
640 ret->methodState = METHOD_DONE;
641 ret->decision = DECISION_FAIL;
644 wpabuf_put_buf(*resp, buf);
653 if (data->phase2_type.vendor == EAP_VENDOR_IETF &&
654 data->phase2_type.method == EAP_TYPE_NONE) {
656 for (i = 0; i < data->num_phase2_types; i++) {
657 if (data->phase2_types[i].vendor !=
659 data->phase2_types[i].method != *pos)
662 data->phase2_type.vendor =
663 data->phase2_types[i].vendor;
664 data->phase2_type.method =
665 data->phase2_types[i].method;
666 wpa_printf(MSG_DEBUG, "EAP-PEAP: Selected "
667 "Phase 2 EAP vendor %d method %d",
668 data->phase2_type.vendor,
669 data->phase2_type.method);
673 if (*pos != data->phase2_type.method ||
674 *pos == EAP_TYPE_NONE) {
675 if (eap_peer_tls_phase2_nak(data->phase2_types,
676 data->num_phase2_types,
682 if (data->phase2_priv == NULL) {
683 data->phase2_method = eap_peer_get_eap_method(
684 data->phase2_type.vendor,
685 data->phase2_type.method);
686 if (data->phase2_method) {
689 data->phase2_method->init(sm);
693 if (data->phase2_priv == NULL || data->phase2_method == NULL) {
694 wpa_printf(MSG_INFO, "EAP-PEAP: failed to initialize "
695 "Phase 2 EAP method %d", *pos);
696 ret->methodState = METHOD_DONE;
697 ret->decision = DECISION_FAIL;
700 data->phase2_eap_started = 1;
701 os_memset(&iret, 0, sizeof(iret));
702 *resp = data->phase2_method->process(sm, data->phase2_priv,
704 if ((iret.methodState == METHOD_DONE ||
705 iret.methodState == METHOD_MAY_CONT) &&
706 (iret.decision == DECISION_UNCOND_SUCC ||
707 iret.decision == DECISION_COND_SUCC)) {
708 data->phase2_eap_success = 1;
709 data->phase2_success = 1;
715 (config->pending_req_identity || config->pending_req_password ||
716 config->pending_req_otp || config->pending_req_new_password)) {
717 wpabuf_free(data->pending_phase2_req);
718 data->pending_phase2_req = wpabuf_alloc_copy(hdr, len);
725 static int eap_peap_decrypt(struct eap_sm *sm, struct eap_peap_data *data,
726 struct eap_method_ret *ret,
727 const struct eap_hdr *req,
728 const struct wpabuf *in_data,
729 struct wpabuf **out_data)
731 struct wpabuf *in_decrypted = NULL;
732 int res, skip_change = 0;
733 struct eap_hdr *hdr, *rhdr;
734 struct wpabuf *resp = NULL;
737 wpa_printf(MSG_DEBUG, "EAP-PEAP: received %lu bytes encrypted data for"
738 " Phase 2", (unsigned long) wpabuf_len(in_data));
740 if (data->pending_phase2_req) {
741 wpa_printf(MSG_DEBUG, "EAP-PEAP: Pending Phase 2 request - "
742 "skip decryption and use old data");
743 /* Clear TLS reassembly state. */
744 eap_peer_tls_reset_input(&data->ssl);
745 in_decrypted = data->pending_phase2_req;
746 data->pending_phase2_req = NULL;
751 if (wpabuf_len(in_data) == 0 && sm->workaround &&
752 data->phase2_success) {
754 * Cisco ACS seems to be using TLS ACK to terminate
755 * EAP-PEAPv0/GTC. Try to reply with TLS ACK.
757 wpa_printf(MSG_DEBUG, "EAP-PEAP: Received TLS ACK, but "
758 "expected data - acknowledge with TLS ACK since "
759 "Phase 2 has been completed");
760 ret->decision = DECISION_COND_SUCC;
761 ret->methodState = METHOD_DONE;
763 } else if (wpabuf_len(in_data) == 0) {
764 /* Received TLS ACK - requesting more fragments */
765 return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_PEAP,
767 req->identifier, NULL, out_data);
770 res = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
775 wpa_hexdump_buf(MSG_DEBUG, "EAP-PEAP: Decrypted Phase 2 EAP",
778 hdr = wpabuf_mhead(in_decrypted);
779 if (wpabuf_len(in_decrypted) == 5 && hdr->code == EAP_CODE_REQUEST &&
780 be_to_host16(hdr->length) == 5 &&
781 eap_get_type(in_decrypted) == EAP_TYPE_IDENTITY) {
782 /* At least FreeRADIUS seems to send full EAP header with
783 * EAP Request Identity */
786 if (wpabuf_len(in_decrypted) >= 5 && hdr->code == EAP_CODE_REQUEST &&
787 eap_get_type(in_decrypted) == EAP_TYPE_TLV) {
791 if (data->peap_version == 0 && !skip_change) {
792 struct eap_hdr *nhdr;
793 struct wpabuf *nmsg = wpabuf_alloc(sizeof(struct eap_hdr) +
794 wpabuf_len(in_decrypted));
796 wpabuf_free(in_decrypted);
799 nhdr = wpabuf_put(nmsg, sizeof(*nhdr));
800 wpabuf_put_buf(nmsg, in_decrypted);
801 nhdr->code = req->code;
802 nhdr->identifier = req->identifier;
803 nhdr->length = host_to_be16(sizeof(struct eap_hdr) +
804 wpabuf_len(in_decrypted));
806 wpabuf_free(in_decrypted);
810 hdr = wpabuf_mhead(in_decrypted);
811 if (wpabuf_len(in_decrypted) < sizeof(*hdr)) {
812 wpa_printf(MSG_INFO, "EAP-PEAP: Too short Phase 2 "
813 "EAP frame (len=%lu)",
814 (unsigned long) wpabuf_len(in_decrypted));
815 wpabuf_free(in_decrypted);
818 len = be_to_host16(hdr->length);
819 if (len > wpabuf_len(in_decrypted)) {
820 wpa_printf(MSG_INFO, "EAP-PEAP: Length mismatch in "
821 "Phase 2 EAP frame (len=%lu hdr->length=%lu)",
822 (unsigned long) wpabuf_len(in_decrypted),
823 (unsigned long) len);
824 wpabuf_free(in_decrypted);
827 if (len < wpabuf_len(in_decrypted)) {
828 wpa_printf(MSG_INFO, "EAP-PEAP: Odd.. Phase 2 EAP header has "
829 "shorter length than full decrypted data "
832 (unsigned long) wpabuf_len(in_decrypted));
834 wpa_printf(MSG_DEBUG, "EAP-PEAP: received Phase 2: code=%d "
835 "identifier=%d length=%lu", hdr->code, hdr->identifier,
836 (unsigned long) len);
838 case EAP_CODE_REQUEST:
839 if (eap_peap_phase2_request(sm, data, ret, in_decrypted,
841 wpabuf_free(in_decrypted);
842 wpa_printf(MSG_INFO, "EAP-PEAP: Phase2 Request "
843 "processing failed");
847 case EAP_CODE_SUCCESS:
848 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 Success");
849 if (data->peap_version == 1) {
850 /* EAP-Success within TLS tunnel is used to indicate
851 * shutdown of the TLS channel. The authentication has
853 if (data->phase2_eap_started &&
854 !data->phase2_eap_success) {
855 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 "
856 "Success used to indicate success, "
857 "but Phase 2 EAP was not yet "
858 "completed successfully");
859 ret->methodState = METHOD_DONE;
860 ret->decision = DECISION_FAIL;
861 wpabuf_free(in_decrypted);
864 wpa_printf(MSG_DEBUG, "EAP-PEAP: Version 1 - "
865 "EAP-Success within TLS tunnel - "
866 "authentication completed");
867 ret->decision = DECISION_UNCOND_SUCC;
868 ret->methodState = METHOD_DONE;
869 data->phase2_success = 1;
870 if (data->peap_outer_success == 2) {
871 wpabuf_free(in_decrypted);
872 wpa_printf(MSG_DEBUG, "EAP-PEAP: Use TLS ACK "
873 "to finish authentication");
875 } else if (data->peap_outer_success == 1) {
876 /* Reply with EAP-Success within the TLS
877 * channel to complete the authentication. */
878 resp = wpabuf_alloc(sizeof(struct eap_hdr));
880 rhdr = wpabuf_put(resp, sizeof(*rhdr));
881 rhdr->code = EAP_CODE_SUCCESS;
882 rhdr->identifier = hdr->identifier;
884 host_to_be16(sizeof(*rhdr));
887 /* No EAP-Success expected for Phase 1 (outer,
888 * unencrypted auth), so force EAP state
889 * machine to SUCCESS state. */
890 sm->peap_done = TRUE;
896 case EAP_CODE_FAILURE:
897 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 Failure");
898 ret->decision = DECISION_FAIL;
899 ret->methodState = METHOD_MAY_CONT;
900 ret->allowNotifications = FALSE;
901 /* Reply with EAP-Failure within the TLS channel to complete
902 * failure reporting. */
903 resp = wpabuf_alloc(sizeof(struct eap_hdr));
905 rhdr = wpabuf_put(resp, sizeof(*rhdr));
906 rhdr->code = EAP_CODE_FAILURE;
907 rhdr->identifier = hdr->identifier;
908 rhdr->length = host_to_be16(sizeof(*rhdr));
912 wpa_printf(MSG_INFO, "EAP-PEAP: Unexpected code=%d in "
913 "Phase 2 EAP header", hdr->code);
917 wpabuf_free(in_decrypted);
920 int skip_change2 = 0;
921 struct wpabuf *rmsg, buf;
923 wpa_hexdump_buf_key(MSG_DEBUG,
924 "EAP-PEAP: Encrypting Phase 2 data", resp);
925 /* PEAP version changes */
926 if (wpabuf_len(resp) >= 5 &&
927 wpabuf_head_u8(resp)[0] == EAP_CODE_RESPONSE &&
928 eap_get_type(resp) == EAP_TYPE_TLV)
931 if (data->peap_version == 0 && !skip_change2) {
932 wpabuf_set(&buf, wpabuf_head_u8(resp) +
933 sizeof(struct eap_hdr),
934 wpabuf_len(resp) - sizeof(struct eap_hdr));
938 if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_PEAP,
939 data->peap_version, req->identifier,
941 wpa_printf(MSG_INFO, "EAP-PEAP: Failed to encrypt "
951 static struct wpabuf * eap_peap_process(struct eap_sm *sm, void *priv,
952 struct eap_method_ret *ret,
953 const struct wpabuf *reqData)
955 const struct eap_hdr *req;
961 struct eap_peap_data *data = priv;
963 pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_PEAP, ret,
964 reqData, &left, &flags);
967 req = wpabuf_head(reqData);
968 id = req->identifier;
970 if (flags & EAP_TLS_FLAGS_START) {
971 wpa_printf(MSG_DEBUG, "EAP-PEAP: Start (server ver=%d, own "
972 "ver=%d)", flags & EAP_TLS_VERSION_MASK,
974 if ((flags & EAP_TLS_VERSION_MASK) < data->peap_version)
975 data->peap_version = flags & EAP_TLS_VERSION_MASK;
976 if (data->force_peap_version >= 0 &&
977 data->force_peap_version != data->peap_version) {
978 wpa_printf(MSG_WARNING, "EAP-PEAP: Failed to select "
979 "forced PEAP version %d",
980 data->force_peap_version);
981 ret->methodState = METHOD_DONE;
982 ret->decision = DECISION_FAIL;
983 ret->allowNotifications = FALSE;
986 wpa_printf(MSG_DEBUG, "EAP-PEAP: Using PEAP version %d",
988 left = 0; /* make sure that this frame is empty, even though it
989 * should always be, anyway */
993 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
996 wpabuf_set(&msg, pos, left);
997 res = eap_peap_decrypt(sm, data, ret, req, &msg, &resp);
999 res = eap_peer_tls_process_helper(sm, &data->ssl,
1001 data->peap_version, id, pos,
1004 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
1006 wpa_printf(MSG_DEBUG,
1007 "EAP-PEAP: TLS done, proceed to Phase 2");
1008 os_free(data->key_data);
1009 /* draft-josefsson-ppext-eap-tls-eap-05.txt
1010 * specifies that PEAPv1 would use "client PEAP
1011 * encryption" as the label. However, most existing
1012 * PEAPv1 implementations seem to be using the old
1013 * label, "client EAP encryption", instead. Use the old
1014 * label by default, but allow it to be configured with
1015 * phase1 parameter peaplabel=1. */
1016 if (data->force_new_label)
1017 label = "client PEAP encryption";
1019 label = "client EAP encryption";
1020 wpa_printf(MSG_DEBUG, "EAP-PEAP: using label '%s' in "
1021 "key derivation", label);
1023 eap_peer_tls_derive_key(sm, &data->ssl, label,
1025 if (data->key_data) {
1026 wpa_hexdump_key(MSG_DEBUG,
1027 "EAP-PEAP: Derived key",
1031 wpa_printf(MSG_DEBUG, "EAP-PEAP: Failed to "
1035 os_free(data->session_id);
1037 eap_peer_tls_derive_session_id(sm, &data->ssl,
1040 if (data->session_id) {
1041 wpa_hexdump(MSG_DEBUG,
1042 "EAP-PEAP: Derived Session-Id",
1043 data->session_id, data->id_len);
1045 wpa_printf(MSG_ERROR, "EAP-PEAP: Failed to "
1046 "derive Session-Id");
1049 if (sm->workaround && data->resuming) {
1051 * At least few RADIUS servers (Aegis v1.1.6;
1052 * but not v1.1.4; and Cisco ACS) seem to be
1053 * terminating PEAPv1 (Aegis) or PEAPv0 (Cisco
1054 * ACS) session resumption with outer
1055 * EAP-Success. This does not seem to follow
1056 * draft-josefsson-pppext-eap-tls-eap-05.txt
1057 * section 4.2, so only allow this if EAP
1058 * workarounds are enabled.
1060 wpa_printf(MSG_DEBUG, "EAP-PEAP: Workaround - "
1061 "allow outer EAP-Success to "
1062 "terminate PEAP resumption");
1063 ret->decision = DECISION_COND_SUCC;
1064 data->phase2_success = 1;
1073 * Application data included in the handshake message.
1075 wpabuf_free(data->pending_phase2_req);
1076 data->pending_phase2_req = resp;
1078 wpabuf_set(&msg, pos, left);
1079 res = eap_peap_decrypt(sm, data, ret, req, &msg,
1084 if (ret->methodState == METHOD_DONE) {
1085 ret->allowNotifications = FALSE;
1090 return eap_peer_tls_build_ack(id, EAP_TYPE_PEAP,
1091 data->peap_version);
1098 static Boolean eap_peap_has_reauth_data(struct eap_sm *sm, void *priv)
1100 struct eap_peap_data *data = priv;
1101 return tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1102 data->phase2_success;
1106 static void eap_peap_deinit_for_reauth(struct eap_sm *sm, void *priv)
1108 struct eap_peap_data *data = priv;
1109 wpabuf_free(data->pending_phase2_req);
1110 data->pending_phase2_req = NULL;
1111 data->crypto_binding_used = 0;
1115 static void * eap_peap_init_for_reauth(struct eap_sm *sm, void *priv)
1117 struct eap_peap_data *data = priv;
1118 os_free(data->key_data);
1119 data->key_data = NULL;
1120 os_free(data->session_id);
1121 data->session_id = NULL;
1122 if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
1126 if (data->phase2_priv && data->phase2_method &&
1127 data->phase2_method->init_for_reauth)
1128 data->phase2_method->init_for_reauth(sm, data->phase2_priv);
1129 data->phase2_success = 0;
1130 data->phase2_eap_success = 0;
1131 data->phase2_eap_started = 0;
1134 sm->peap_done = FALSE;
1139 static int eap_peap_get_status(struct eap_sm *sm, void *priv, char *buf,
1140 size_t buflen, int verbose)
1142 struct eap_peap_data *data = priv;
1145 len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
1146 if (data->phase2_method) {
1147 ret = os_snprintf(buf + len, buflen - len,
1148 "EAP-PEAPv%d Phase2 method=%s\n",
1150 data->phase2_method->name);
1151 if (ret < 0 || (size_t) ret >= buflen - len)
1159 static Boolean eap_peap_isKeyAvailable(struct eap_sm *sm, void *priv)
1161 struct eap_peap_data *data = priv;
1162 return data->key_data != NULL && data->phase2_success;
1166 static u8 * eap_peap_getKey(struct eap_sm *sm, void *priv, size_t *len)
1168 struct eap_peap_data *data = priv;
1171 if (data->key_data == NULL || !data->phase2_success)
1174 key = os_malloc(EAP_TLS_KEY_LEN);
1178 *len = EAP_TLS_KEY_LEN;
1180 if (data->crypto_binding_used) {
1183 * Note: It looks like Microsoft implementation requires null
1184 * termination for this label while the one used for deriving
1185 * IPMK|CMK did not use null termination.
1187 if (peap_prfplus(data->peap_version, data->ipmk, 40,
1188 "Session Key Generating Function",
1189 (u8 *) "\00", 1, csk, sizeof(csk)) < 0) {
1193 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: CSK", csk, sizeof(csk));
1194 os_memcpy(key, csk, EAP_TLS_KEY_LEN);
1195 wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Derived key",
1196 key, EAP_TLS_KEY_LEN);
1198 os_memcpy(key, data->key_data, EAP_TLS_KEY_LEN);
1204 static u8 * eap_peap_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
1206 struct eap_peap_data *data = priv;
1209 if (data->session_id == NULL || !data->phase2_success)
1212 id = os_malloc(data->id_len);
1216 *len = data->id_len;
1217 os_memcpy(id, data->session_id, data->id_len);
1223 int eap_peer_peap_register(void)
1225 struct eap_method *eap;
1228 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
1229 EAP_VENDOR_IETF, EAP_TYPE_PEAP, "PEAP");
1233 eap->init = eap_peap_init;
1234 eap->deinit = eap_peap_deinit;
1235 eap->process = eap_peap_process;
1236 eap->isKeyAvailable = eap_peap_isKeyAvailable;
1237 eap->getKey = eap_peap_getKey;
1238 eap->get_status = eap_peap_get_status;
1239 eap->has_reauth_data = eap_peap_has_reauth_data;
1240 eap->deinit_for_reauth = eap_peap_deinit_for_reauth;
1241 eap->init_for_reauth = eap_peap_init_for_reauth;
1242 eap->getSessionId = eap_peap_get_session_id;
1244 ret = eap_peer_method_register(eap);
1246 eap_peer_method_free(eap);