2 * EAP peer method: EAP-PEAP (draft-josefsson-pppext-eap-tls-eap-10.txt)
3 * Copyright (c) 2004-2019, 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 struct wpabuf *pending_resp;
63 enum { NO_BINDING, OPTIONAL_BINDING, REQUIRE_BINDING } crypto_binding;
64 int crypto_binding_used;
68 int soh; /* Whether IF-TNCCS-SOH (Statement of Health; Microsoft NAP)
73 static void eap_peap_parse_phase1(struct eap_peap_data *data,
78 pos = os_strstr(phase1, "peapver=");
80 data->force_peap_version = atoi(pos + 8);
81 data->peap_version = data->force_peap_version;
82 wpa_printf(MSG_DEBUG, "EAP-PEAP: Forced PEAP version %d",
83 data->force_peap_version);
86 if (os_strstr(phase1, "peaplabel=1")) {
87 data->force_new_label = 1;
88 wpa_printf(MSG_DEBUG, "EAP-PEAP: Force new label for key "
92 if (os_strstr(phase1, "peap_outer_success=0")) {
93 data->peap_outer_success = 0;
94 wpa_printf(MSG_DEBUG, "EAP-PEAP: terminate authentication on "
95 "tunneled EAP-Success");
96 } else if (os_strstr(phase1, "peap_outer_success=1")) {
97 data->peap_outer_success = 1;
98 wpa_printf(MSG_DEBUG, "EAP-PEAP: send tunneled EAP-Success "
99 "after receiving tunneled EAP-Success");
100 } else if (os_strstr(phase1, "peap_outer_success=2")) {
101 data->peap_outer_success = 2;
102 wpa_printf(MSG_DEBUG, "EAP-PEAP: send PEAP/TLS ACK after "
103 "receiving tunneled EAP-Success");
106 if (os_strstr(phase1, "crypto_binding=0")) {
107 data->crypto_binding = NO_BINDING;
108 wpa_printf(MSG_DEBUG, "EAP-PEAP: Do not use cryptobinding");
109 } else if (os_strstr(phase1, "crypto_binding=1")) {
110 data->crypto_binding = OPTIONAL_BINDING;
111 wpa_printf(MSG_DEBUG, "EAP-PEAP: Optional cryptobinding");
112 } else if (os_strstr(phase1, "crypto_binding=2")) {
113 data->crypto_binding = REQUIRE_BINDING;
114 wpa_printf(MSG_DEBUG, "EAP-PEAP: Require cryptobinding");
118 if (os_strstr(phase1, "tnc=soh2")) {
120 wpa_printf(MSG_DEBUG, "EAP-PEAP: SoH version 2 enabled");
121 } else if (os_strstr(phase1, "tnc=soh1")) {
123 wpa_printf(MSG_DEBUG, "EAP-PEAP: SoH version 1 enabled");
124 } else if (os_strstr(phase1, "tnc=soh")) {
126 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);
149 if (eap_peer_select_phase2_methods(config, "auth=",
151 &data->num_phase2_types) < 0) {
152 eap_peap_deinit(sm, data);
156 data->phase2_type.vendor = EAP_VENDOR_IETF;
157 data->phase2_type.method = EAP_TYPE_NONE;
159 if (eap_peer_tls_ssl_init(sm, &data->ssl, config, EAP_TYPE_PEAP)) {
160 wpa_printf(MSG_INFO, "EAP-PEAP: Failed to initialize SSL.");
161 eap_peap_deinit(sm, data);
169 static void eap_peap_free_key(struct eap_peap_data *data)
171 if (data->key_data) {
172 bin_clear_free(data->key_data, EAP_TLS_KEY_LEN + EAP_EMSK_LEN);
173 data->key_data = NULL;
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 eap_peap_free_key(data);
188 os_free(data->session_id);
189 wpabuf_clear_free(data->pending_phase2_req);
190 wpabuf_clear_free(data->pending_resp);
191 bin_clear_free(data, sizeof(*data));
196 * eap_tlv_build_nak - Build EAP-TLV NAK message
197 * @id: EAP identifier for the header
198 * @nak_type: TLV type (EAP_TLV_*)
199 * Returns: Buffer to the allocated EAP-TLV NAK message or %NULL on failure
201 * This function builds an EAP-TLV NAK message. The caller is responsible for
202 * freeing the returned buffer.
204 static struct wpabuf * eap_tlv_build_nak(int id, u16 nak_type)
208 msg = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TLV, 10,
209 EAP_CODE_RESPONSE, id);
213 wpabuf_put_u8(msg, 0x80); /* Mandatory */
214 wpabuf_put_u8(msg, EAP_TLV_NAK_TLV);
215 wpabuf_put_be16(msg, 6); /* Length */
216 wpabuf_put_be32(msg, 0); /* Vendor-Id */
217 wpabuf_put_be16(msg, nak_type); /* NAK-Type */
223 static int eap_peap_get_isk(struct eap_sm *sm, struct eap_peap_data *data,
224 u8 *isk, size_t isk_len)
229 os_memset(isk, 0, isk_len);
230 if (data->phase2_method == NULL || data->phase2_priv == NULL ||
231 data->phase2_method->isKeyAvailable == NULL ||
232 data->phase2_method->getKey == NULL)
235 if (!data->phase2_method->isKeyAvailable(sm, data->phase2_priv) ||
236 (key = data->phase2_method->getKey(sm, data->phase2_priv,
237 &key_len)) == NULL) {
238 wpa_printf(MSG_DEBUG, "EAP-PEAP: Could not get key material "
243 if (key_len > isk_len)
245 os_memcpy(isk, key, key_len);
252 static int eap_peap_derive_cmk(struct eap_sm *sm, struct eap_peap_data *data)
255 u8 isk[32], imck[60];
259 * Tunnel key (TK) is the first 60 octets of the key generated by
260 * phase 1 of PEAP (based on TLS).
265 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: TK", tk, 60);
267 resumed = tls_connection_resumed(sm->ssl_ctx, data->ssl.conn);
268 wpa_printf(MSG_DEBUG,
269 "EAP-PEAP: CMK derivation - reauth=%d resumed=%d phase2_eap_started=%d phase2_success=%d",
270 data->reauth, resumed, data->phase2_eap_started,
271 data->phase2_success);
272 if (data->reauth && !data->phase2_eap_started && resumed) {
273 /* Fast-connect: IPMK|CMK = TK */
274 os_memcpy(data->ipmk, tk, 40);
275 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: IPMK from TK",
277 os_memcpy(data->cmk, tk + 40, 20);
278 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: CMK from TK",
283 if (eap_peap_get_isk(sm, data, isk, sizeof(isk)) < 0)
285 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: ISK", isk, sizeof(isk));
288 * IPMK Seed = "Inner Methods Compound Keys" | ISK
289 * TempKey = First 40 octets of TK
290 * IPMK|CMK = PRF+(TempKey, IPMK Seed, 60)
291 * (note: draft-josefsson-pppext-eap-tls-eap-10.txt includes a space
292 * in the end of the label just before ISK; is that just a typo?)
294 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: TempKey", tk, 40);
295 res = peap_prfplus(data->peap_version, tk, 40,
296 "Inner Methods Compound Keys",
297 isk, sizeof(isk), imck, sizeof(imck));
298 os_memset(isk, 0, sizeof(isk));
301 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: IMCK (IPMKj)",
304 os_memcpy(data->ipmk, imck, 40);
305 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: IPMK (S-IPMKj)", data->ipmk, 40);
306 os_memcpy(data->cmk, imck + 40, 20);
307 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: CMK (CMKj)", data->cmk, 20);
308 os_memset(imck, 0, sizeof(imck));
314 static int eap_tlv_add_cryptobinding(struct eap_sm *sm,
315 struct eap_peap_data *data,
319 u8 eap_type = EAP_TYPE_PEAP;
324 /* Compound_MAC: HMAC-SHA1-160(cryptobinding TLV | EAP type) */
325 addr[0] = wpabuf_put(buf, 0);
330 tlv_type = EAP_TLV_CRYPTO_BINDING_TLV;
331 wpabuf_put_be16(buf, tlv_type);
332 wpabuf_put_be16(buf, 56);
334 wpabuf_put_u8(buf, 0); /* Reserved */
335 wpabuf_put_u8(buf, data->peap_version); /* Version */
336 wpabuf_put_u8(buf, data->peap_version); /* RecvVersion */
337 wpabuf_put_u8(buf, 1); /* SubType: 0 = Request, 1 = Response */
338 wpabuf_put_data(buf, data->binding_nonce, 32); /* Nonce */
339 mac = wpabuf_put(buf, 20); /* Compound_MAC */
340 wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC CMK", data->cmk, 20);
341 wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC data 1",
343 wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC data 2",
345 if (hmac_sha1_vector(data->cmk, 20, 2, addr, len, mac) < 0)
347 wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC", mac, SHA1_MAC_LEN);
348 data->crypto_binding_used = 1;
355 * eap_tlv_build_result - Build EAP-TLV Result message
356 * @id: EAP identifier for the header
357 * @status: Status (EAP_TLV_RESULT_SUCCESS or EAP_TLV_RESULT_FAILURE)
358 * Returns: Buffer to the allocated EAP-TLV Result message or %NULL on failure
360 * This function builds an EAP-TLV Result message. The caller is responsible
361 * for freeing the returned buffer.
363 static struct wpabuf * eap_tlv_build_result(struct eap_sm *sm,
364 struct eap_peap_data *data,
371 if (data->crypto_binding == NO_BINDING)
376 len += 60; /* Cryptobinding TLV */
377 msg = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TLV, len,
378 EAP_CODE_RESPONSE, id);
382 wpabuf_put_u8(msg, 0x80); /* Mandatory */
383 wpabuf_put_u8(msg, EAP_TLV_RESULT_TLV);
384 wpabuf_put_be16(msg, 2); /* Length */
385 wpabuf_put_be16(msg, status); /* Status */
387 if (crypto_tlv_used && eap_tlv_add_cryptobinding(sm, data, msg)) {
388 wpabuf_clear_free(msg);
396 static int eap_tlv_validate_cryptobinding(struct eap_sm *sm,
397 struct eap_peap_data *data,
398 const u8 *crypto_tlv,
399 size_t crypto_tlv_len)
401 u8 buf[61], mac[SHA1_MAC_LEN];
404 if (eap_peap_derive_cmk(sm, data) < 0) {
405 wpa_printf(MSG_DEBUG, "EAP-PEAP: Could not derive CMK");
409 if (crypto_tlv_len != 4 + 56) {
410 wpa_printf(MSG_DEBUG, "EAP-PEAP: Invalid cryptobinding TLV "
411 "length %d", (int) crypto_tlv_len);
416 pos += 4; /* TLV header */
417 if (pos[1] != data->peap_version) {
418 wpa_printf(MSG_DEBUG, "EAP-PEAP: Cryptobinding TLV Version "
419 "mismatch (was %d; expected %d)",
420 pos[1], data->peap_version);
425 wpa_printf(MSG_DEBUG, "EAP-PEAP: Unexpected Cryptobinding TLV "
426 "SubType %d", pos[3]);
430 os_memcpy(data->binding_nonce, pos, 32);
431 pos += 32; /* Nonce */
433 /* Compound_MAC: HMAC-SHA1-160(cryptobinding TLV | EAP type) */
434 os_memcpy(buf, crypto_tlv, 60);
435 os_memset(buf + 4 + 4 + 32, 0, 20); /* Compound_MAC */
436 buf[60] = EAP_TYPE_PEAP;
437 wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Compound_MAC data",
439 hmac_sha1(data->cmk, 20, buf, sizeof(buf), mac);
441 if (os_memcmp_const(mac, pos, SHA1_MAC_LEN) != 0) {
442 wpa_printf(MSG_DEBUG, "EAP-PEAP: Invalid Compound_MAC in "
443 "cryptobinding TLV");
444 wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Received MAC",
446 wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Expected MAC",
451 wpa_printf(MSG_DEBUG, "EAP-PEAP: Valid cryptobinding TLV received");
458 * eap_tlv_process - Process a received EAP-TLV message and generate a response
459 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
460 * @ret: Return values from EAP request validation and processing
461 * @req: EAP-TLV request to be processed. The caller must have validated that
462 * the buffer is large enough to contain full request (hdr->length bytes) and
463 * that the EAP type is EAP_TYPE_TLV.
464 * @resp: Buffer to return a pointer to the allocated response message. This
465 * field should be initialized to %NULL before the call. The value will be
466 * updated if a response message is generated. The caller is responsible for
467 * freeing the allocated message.
468 * @force_failure: Force negotiation to fail
469 * Returns: 0 on success, -1 on failure
471 static int eap_tlv_process(struct eap_sm *sm, struct eap_peap_data *data,
472 struct eap_method_ret *ret,
473 const struct wpabuf *req, struct wpabuf **resp,
476 size_t left, tlv_len;
478 const u8 *result_tlv = NULL, *crypto_tlv = NULL;
479 size_t result_tlv_len = 0, crypto_tlv_len = 0;
480 int tlv_type, mandatory;
483 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_TLV, req, &left);
486 wpa_hexdump(MSG_DEBUG, "EAP-TLV: Received TLVs", pos, left);
488 mandatory = !!(pos[0] & 0x80);
489 tlv_type = WPA_GET_BE16(pos) & 0x3fff;
491 tlv_len = WPA_GET_BE16(pos);
494 if (tlv_len > left) {
495 wpa_printf(MSG_DEBUG, "EAP-TLV: TLV underrun "
496 "(tlv_len=%lu left=%lu)",
497 (unsigned long) tlv_len,
498 (unsigned long) left);
502 case EAP_TLV_RESULT_TLV:
504 result_tlv_len = tlv_len;
506 case EAP_TLV_CRYPTO_BINDING_TLV:
508 crypto_tlv_len = tlv_len;
511 wpa_printf(MSG_DEBUG, "EAP-TLV: Unsupported TLV Type "
513 mandatory ? " (mandatory)" : "");
515 /* NAK TLV and ignore all TLVs in this packet.
517 *resp = eap_tlv_build_nak(eap_get_id(req),
519 return *resp == NULL ? -1 : 0;
521 /* Ignore this TLV, but process other TLVs */
529 wpa_printf(MSG_DEBUG, "EAP-TLV: Last TLV too short in "
530 "Request (left=%lu)", (unsigned long) left);
534 /* Process supported TLVs */
535 if (crypto_tlv && data->crypto_binding != NO_BINDING) {
536 wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Cryptobinding TLV",
537 crypto_tlv, crypto_tlv_len);
538 if (eap_tlv_validate_cryptobinding(sm, data, crypto_tlv - 4,
539 crypto_tlv_len + 4) < 0) {
540 if (result_tlv == NULL)
543 crypto_tlv = NULL; /* do not include Cryptobinding TLV
544 * in response, if the received
545 * cryptobinding was invalid. */
547 } else if (!crypto_tlv && data->crypto_binding == REQUIRE_BINDING) {
548 wpa_printf(MSG_DEBUG, "EAP-PEAP: No cryptobinding TLV");
553 int status, resp_status;
554 wpa_hexdump(MSG_DEBUG, "EAP-TLV: Result TLV",
555 result_tlv, result_tlv_len);
556 if (result_tlv_len < 2) {
557 wpa_printf(MSG_INFO, "EAP-TLV: Too short Result TLV "
559 (unsigned long) result_tlv_len);
562 status = WPA_GET_BE16(result_tlv);
563 if (status == EAP_TLV_RESULT_SUCCESS) {
564 wpa_printf(MSG_INFO, "EAP-TLV: TLV Result - Success "
565 "- EAP-TLV/Phase2 Completed");
567 wpa_printf(MSG_INFO, "EAP-TLV: Earlier failure"
568 " - force failed Phase 2");
569 resp_status = EAP_TLV_RESULT_FAILURE;
570 ret->decision = DECISION_FAIL;
572 resp_status = EAP_TLV_RESULT_SUCCESS;
573 ret->decision = DECISION_UNCOND_SUCC;
575 } else if (status == EAP_TLV_RESULT_FAILURE) {
576 wpa_printf(MSG_INFO, "EAP-TLV: TLV Result - Failure");
577 resp_status = EAP_TLV_RESULT_FAILURE;
578 ret->decision = DECISION_FAIL;
580 wpa_printf(MSG_INFO, "EAP-TLV: Unknown TLV Result "
581 "Status %d", status);
582 resp_status = EAP_TLV_RESULT_FAILURE;
583 ret->decision = DECISION_FAIL;
585 ret->methodState = METHOD_DONE;
587 *resp = eap_tlv_build_result(sm, data, crypto_tlv != NULL,
588 eap_get_id(req), resp_status);
595 static int eap_peap_phase2_request(struct eap_sm *sm,
596 struct eap_peap_data *data,
597 struct eap_method_ret *ret,
599 struct wpabuf **resp)
601 struct eap_hdr *hdr = wpabuf_mhead(req);
602 size_t len = be_to_host16(hdr->length);
604 struct eap_method_ret iret;
605 struct eap_peer_config *config = eap_get_config(sm);
607 if (len <= sizeof(struct eap_hdr)) {
608 wpa_printf(MSG_INFO, "EAP-PEAP: too short "
609 "Phase 2 request (len=%lu)", (unsigned long) len);
612 pos = (u8 *) (hdr + 1);
613 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 Request: type=%d", *pos);
615 case EAP_TYPE_IDENTITY:
616 *resp = eap_sm_buildIdentity(sm, hdr->identifier, 1);
619 os_memset(&iret, 0, sizeof(iret));
620 if (eap_tlv_process(sm, data, &iret, req, resp,
621 data->phase2_eap_started &&
622 !data->phase2_eap_success)) {
623 ret->methodState = METHOD_DONE;
624 ret->decision = DECISION_FAIL;
627 if (iret.methodState == METHOD_DONE ||
628 iret.methodState == METHOD_MAY_CONT) {
629 ret->methodState = iret.methodState;
630 ret->decision = iret.decision;
631 data->phase2_success = 1;
634 case EAP_TYPE_EXPANDED:
640 epos = eap_hdr_validate(EAP_VENDOR_MICROSOFT, 0x21,
644 wpa_printf(MSG_DEBUG,
645 "EAP-PEAP: SoH EAP Extensions");
646 buf = tncc_process_soh_request(data->soh,
649 *resp = eap_msg_alloc(
650 EAP_VENDOR_MICROSOFT, 0x21,
655 ret->methodState = METHOD_DONE;
656 ret->decision = DECISION_FAIL;
657 wpabuf_clear_free(buf);
660 wpabuf_put_buf(*resp, buf);
661 wpabuf_clear_free(buf);
669 if (data->phase2_type.vendor == EAP_VENDOR_IETF &&
670 data->phase2_type.method == EAP_TYPE_NONE) {
672 for (i = 0; i < data->num_phase2_types; i++) {
673 if (data->phase2_types[i].vendor !=
675 data->phase2_types[i].method != *pos)
678 data->phase2_type.vendor =
679 data->phase2_types[i].vendor;
680 data->phase2_type.method =
681 data->phase2_types[i].method;
682 wpa_printf(MSG_DEBUG, "EAP-PEAP: Selected "
683 "Phase 2 EAP vendor %d method %d",
684 data->phase2_type.vendor,
685 data->phase2_type.method);
689 if (*pos != data->phase2_type.method ||
690 *pos == EAP_TYPE_NONE) {
691 if (eap_peer_tls_phase2_nak(data->phase2_types,
692 data->num_phase2_types,
698 if (data->phase2_priv == NULL) {
699 data->phase2_method = eap_peer_get_eap_method(
700 data->phase2_type.vendor,
701 data->phase2_type.method);
702 if (data->phase2_method) {
705 data->phase2_method->init(sm);
709 if (data->phase2_priv == NULL || data->phase2_method == NULL) {
710 wpa_printf(MSG_INFO, "EAP-PEAP: failed to initialize "
711 "Phase 2 EAP method %d", *pos);
712 ret->methodState = METHOD_DONE;
713 ret->decision = DECISION_FAIL;
716 data->phase2_eap_started = 1;
717 os_memset(&iret, 0, sizeof(iret));
718 *resp = data->phase2_method->process(sm, data->phase2_priv,
720 if ((iret.methodState == METHOD_DONE ||
721 iret.methodState == METHOD_MAY_CONT) &&
722 (iret.decision == DECISION_UNCOND_SUCC ||
723 iret.decision == DECISION_COND_SUCC)) {
724 data->phase2_eap_success = 1;
725 data->phase2_success = 1;
731 (config->pending_req_identity || config->pending_req_password ||
732 config->pending_req_otp || config->pending_req_new_password ||
733 config->pending_req_sim)) {
734 wpabuf_clear_free(data->pending_phase2_req);
735 data->pending_phase2_req = wpabuf_alloc_copy(hdr, len);
742 static int eap_peap_decrypt(struct eap_sm *sm, struct eap_peap_data *data,
743 struct eap_method_ret *ret,
744 const struct eap_hdr *req,
745 const struct wpabuf *in_data,
746 struct wpabuf **out_data)
748 struct wpabuf *in_decrypted = NULL;
749 int res, skip_change = 0;
750 struct eap_hdr *hdr, *rhdr;
751 struct wpabuf *resp = NULL;
754 wpa_printf(MSG_DEBUG, "EAP-PEAP: received %lu bytes encrypted data for"
755 " Phase 2", (unsigned long) wpabuf_len(in_data));
757 if (data->pending_phase2_req) {
758 wpa_printf(MSG_DEBUG, "EAP-PEAP: Pending Phase 2 request - "
759 "skip decryption and use old data");
760 /* Clear TLS reassembly state. */
761 eap_peer_tls_reset_input(&data->ssl);
762 in_decrypted = data->pending_phase2_req;
763 data->pending_phase2_req = NULL;
768 if (wpabuf_len(in_data) == 0 && sm->workaround &&
769 data->phase2_success) {
771 * Cisco ACS seems to be using TLS ACK to terminate
772 * EAP-PEAPv0/GTC. Try to reply with TLS ACK.
774 wpa_printf(MSG_DEBUG, "EAP-PEAP: Received TLS ACK, but "
775 "expected data - acknowledge with TLS ACK since "
776 "Phase 2 has been completed");
777 ret->decision = DECISION_COND_SUCC;
778 ret->methodState = METHOD_DONE;
780 } else if (wpabuf_len(in_data) == 0) {
781 /* Received TLS ACK - requesting more fragments */
782 return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_PEAP,
784 req->identifier, NULL, out_data);
787 res = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
792 wpa_hexdump_buf(MSG_DEBUG, "EAP-PEAP: Decrypted Phase 2 EAP",
795 hdr = wpabuf_mhead(in_decrypted);
796 if (wpabuf_len(in_decrypted) == 5 && hdr->code == EAP_CODE_REQUEST &&
797 be_to_host16(hdr->length) == 5 &&
798 eap_get_type(in_decrypted) == EAP_TYPE_IDENTITY) {
799 /* At least FreeRADIUS seems to send full EAP header with
800 * EAP Request Identity */
803 if (wpabuf_len(in_decrypted) >= 5 && hdr->code == EAP_CODE_REQUEST &&
804 eap_get_type(in_decrypted) == EAP_TYPE_TLV) {
808 if (data->peap_version == 0 && !skip_change) {
809 struct eap_hdr *nhdr;
810 struct wpabuf *nmsg = wpabuf_alloc(sizeof(struct eap_hdr) +
811 wpabuf_len(in_decrypted));
813 wpabuf_clear_free(in_decrypted);
816 nhdr = wpabuf_put(nmsg, sizeof(*nhdr));
817 wpabuf_put_buf(nmsg, in_decrypted);
818 nhdr->code = req->code;
819 nhdr->identifier = req->identifier;
820 nhdr->length = host_to_be16(sizeof(struct eap_hdr) +
821 wpabuf_len(in_decrypted));
823 wpabuf_clear_free(in_decrypted);
827 hdr = wpabuf_mhead(in_decrypted);
828 if (wpabuf_len(in_decrypted) < sizeof(*hdr)) {
829 wpa_printf(MSG_INFO, "EAP-PEAP: Too short Phase 2 "
830 "EAP frame (len=%lu)",
831 (unsigned long) wpabuf_len(in_decrypted));
832 wpabuf_clear_free(in_decrypted);
835 len = be_to_host16(hdr->length);
836 if (len > wpabuf_len(in_decrypted)) {
837 wpa_printf(MSG_INFO, "EAP-PEAP: Length mismatch in "
838 "Phase 2 EAP frame (len=%lu hdr->length=%lu)",
839 (unsigned long) wpabuf_len(in_decrypted),
840 (unsigned long) len);
841 wpabuf_clear_free(in_decrypted);
844 if (len < wpabuf_len(in_decrypted)) {
845 wpa_printf(MSG_INFO, "EAP-PEAP: Odd.. Phase 2 EAP header has "
846 "shorter length than full decrypted data "
849 (unsigned long) wpabuf_len(in_decrypted));
851 wpa_printf(MSG_DEBUG, "EAP-PEAP: received Phase 2: code=%d "
852 "identifier=%d length=%lu", hdr->code, hdr->identifier,
853 (unsigned long) len);
855 case EAP_CODE_REQUEST:
856 if (eap_peap_phase2_request(sm, data, ret, in_decrypted,
858 wpabuf_clear_free(in_decrypted);
859 wpa_printf(MSG_INFO, "EAP-PEAP: Phase2 Request "
860 "processing failed");
864 case EAP_CODE_SUCCESS:
865 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 Success");
866 if (data->peap_version == 1) {
867 /* EAP-Success within TLS tunnel is used to indicate
868 * shutdown of the TLS channel. The authentication has
870 if (data->phase2_eap_started &&
871 !data->phase2_eap_success) {
872 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 "
873 "Success used to indicate success, "
874 "but Phase 2 EAP was not yet "
875 "completed successfully");
876 ret->methodState = METHOD_DONE;
877 ret->decision = DECISION_FAIL;
878 wpabuf_clear_free(in_decrypted);
881 wpa_printf(MSG_DEBUG, "EAP-PEAP: Version 1 - "
882 "EAP-Success within TLS tunnel - "
883 "authentication completed");
884 ret->decision = DECISION_UNCOND_SUCC;
885 ret->methodState = METHOD_DONE;
886 data->phase2_success = 1;
887 if (data->peap_outer_success == 2) {
888 wpabuf_clear_free(in_decrypted);
889 wpa_printf(MSG_DEBUG, "EAP-PEAP: Use TLS ACK "
890 "to finish authentication");
892 } else if (data->peap_outer_success == 1) {
893 /* Reply with EAP-Success within the TLS
894 * channel to complete the authentication. */
895 resp = wpabuf_alloc(sizeof(struct eap_hdr));
897 rhdr = wpabuf_put(resp, sizeof(*rhdr));
898 rhdr->code = EAP_CODE_SUCCESS;
899 rhdr->identifier = hdr->identifier;
901 host_to_be16(sizeof(*rhdr));
904 /* No EAP-Success expected for Phase 1 (outer,
905 * unencrypted auth), so force EAP state
906 * machine to SUCCESS state. */
907 sm->peap_done = TRUE;
913 case EAP_CODE_FAILURE:
914 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 Failure");
915 ret->decision = DECISION_FAIL;
916 ret->methodState = METHOD_MAY_CONT;
917 ret->allowNotifications = FALSE;
918 /* Reply with EAP-Failure within the TLS channel to complete
919 * failure reporting. */
920 resp = wpabuf_alloc(sizeof(struct eap_hdr));
922 rhdr = wpabuf_put(resp, sizeof(*rhdr));
923 rhdr->code = EAP_CODE_FAILURE;
924 rhdr->identifier = hdr->identifier;
925 rhdr->length = host_to_be16(sizeof(*rhdr));
929 wpa_printf(MSG_INFO, "EAP-PEAP: Unexpected code=%d in "
930 "Phase 2 EAP header", hdr->code);
934 wpabuf_clear_free(in_decrypted);
937 int skip_change2 = 0;
938 struct wpabuf *rmsg, buf;
940 wpa_hexdump_buf_key(MSG_DEBUG,
941 "EAP-PEAP: Encrypting Phase 2 data", resp);
942 /* PEAP version changes */
943 if (wpabuf_len(resp) >= 5 &&
944 wpabuf_head_u8(resp)[0] == EAP_CODE_RESPONSE &&
945 eap_get_type(resp) == EAP_TYPE_TLV)
948 if (data->peap_version == 0 && !skip_change2) {
949 wpabuf_set(&buf, wpabuf_head_u8(resp) +
950 sizeof(struct eap_hdr),
951 wpabuf_len(resp) - sizeof(struct eap_hdr));
955 if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_PEAP,
956 data->peap_version, req->identifier,
958 wpa_printf(MSG_INFO, "EAP-PEAP: Failed to encrypt "
961 wpabuf_clear_free(resp);
968 static struct wpabuf * eap_peap_process(struct eap_sm *sm, void *priv,
969 struct eap_method_ret *ret,
970 const struct wpabuf *reqData)
972 const struct eap_hdr *req;
978 struct eap_peap_data *data = priv;
981 pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_PEAP, ret,
982 reqData, &left, &flags);
985 req = wpabuf_head(reqData);
986 id = req->identifier;
988 if (flags & EAP_TLS_FLAGS_START) {
989 wpa_printf(MSG_DEBUG, "EAP-PEAP: Start (server ver=%d, own "
990 "ver=%d)", flags & EAP_TLS_VERSION_MASK,
992 if ((flags & EAP_TLS_VERSION_MASK) < data->peap_version)
993 data->peap_version = flags & EAP_TLS_VERSION_MASK;
994 if (data->force_peap_version >= 0 &&
995 data->force_peap_version != data->peap_version) {
996 wpa_printf(MSG_WARNING, "EAP-PEAP: Failed to select "
997 "forced PEAP version %d",
998 data->force_peap_version);
999 ret->methodState = METHOD_DONE;
1000 ret->decision = DECISION_FAIL;
1001 ret->allowNotifications = FALSE;
1004 wpa_printf(MSG_DEBUG, "EAP-PEAP: Using PEAP version %d",
1005 data->peap_version);
1006 left = 0; /* make sure that this frame is empty, even though it
1007 * should always be, anyway */
1010 wpabuf_set(&msg, pos, left);
1013 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1015 res = eap_peap_decrypt(sm, data, ret, req, &msg, &resp);
1017 if (sm->waiting_ext_cert_check && data->pending_resp) {
1018 struct eap_peer_config *config = eap_get_config(sm);
1020 if (config->pending_ext_cert_check ==
1021 EXT_CERT_CHECK_GOOD) {
1022 wpa_printf(MSG_DEBUG,
1023 "EAP-PEAP: External certificate check succeeded - continue handshake");
1024 resp = data->pending_resp;
1025 data->pending_resp = NULL;
1026 sm->waiting_ext_cert_check = 0;
1030 if (config->pending_ext_cert_check ==
1031 EXT_CERT_CHECK_BAD) {
1032 wpa_printf(MSG_DEBUG,
1033 "EAP-PEAP: External certificate check failed - force authentication failure");
1034 ret->methodState = METHOD_DONE;
1035 ret->decision = DECISION_FAIL;
1036 sm->waiting_ext_cert_check = 0;
1040 wpa_printf(MSG_DEBUG,
1041 "EAP-PEAP: Continuing to wait external server certificate validation");
1045 res = eap_peer_tls_process_helper(sm, &data->ssl,
1047 data->peap_version, id, &msg,
1051 wpa_printf(MSG_DEBUG,
1052 "EAP-PEAP: TLS processing failed");
1053 ret->methodState = METHOD_DONE;
1054 ret->decision = DECISION_FAIL;
1059 if (sm->waiting_ext_cert_check) {
1060 wpa_printf(MSG_DEBUG,
1061 "EAP-PEAP: Waiting external server certificate validation");
1062 wpabuf_clear_free(data->pending_resp);
1063 data->pending_resp = resp;
1067 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
1069 wpa_printf(MSG_DEBUG,
1070 "EAP-PEAP: TLS done, proceed to Phase 2");
1071 eap_peap_free_key(data);
1072 /* draft-josefsson-ppext-eap-tls-eap-05.txt
1073 * specifies that PEAPv1 would use "client PEAP
1074 * encryption" as the label. However, most existing
1075 * PEAPv1 implementations seem to be using the old
1076 * label, "client EAP encryption", instead. Use the old
1077 * label by default, but allow it to be configured with
1078 * phase1 parameter peaplabel=1. */
1079 if (data->force_new_label)
1080 label = "client PEAP encryption";
1082 label = "client EAP encryption";
1083 wpa_printf(MSG_DEBUG, "EAP-PEAP: using label '%s' in "
1084 "key derivation", label);
1086 eap_peer_tls_derive_key(sm, &data->ssl, label,
1090 if (data->key_data) {
1091 wpa_hexdump_key(MSG_DEBUG,
1092 "EAP-PEAP: Derived key",
1095 wpa_hexdump_key(MSG_DEBUG,
1096 "EAP-PEAP: Derived EMSK",
1101 wpa_printf(MSG_DEBUG, "EAP-PEAP: Failed to "
1105 os_free(data->session_id);
1107 eap_peer_tls_derive_session_id(sm, &data->ssl,
1110 if (data->session_id) {
1111 wpa_hexdump(MSG_DEBUG,
1112 "EAP-PEAP: Derived Session-Id",
1113 data->session_id, data->id_len);
1115 wpa_printf(MSG_ERROR, "EAP-PEAP: Failed to "
1116 "derive Session-Id");
1119 if (sm->workaround && data->resuming) {
1121 * At least few RADIUS servers (Aegis v1.1.6;
1122 * but not v1.1.4; and Cisco ACS) seem to be
1123 * terminating PEAPv1 (Aegis) or PEAPv0 (Cisco
1124 * ACS) session resumption with outer
1125 * EAP-Success. This does not seem to follow
1126 * draft-josefsson-pppext-eap-tls-eap-05.txt
1127 * section 4.2, so only allow this if EAP
1128 * workarounds are enabled.
1130 wpa_printf(MSG_DEBUG, "EAP-PEAP: Workaround - "
1131 "allow outer EAP-Success to "
1132 "terminate PEAP resumption");
1133 ret->decision = DECISION_COND_SUCC;
1134 data->phase2_success = 1;
1142 * Application data included in the handshake message.
1144 wpabuf_clear_free(data->pending_phase2_req);
1145 data->pending_phase2_req = resp;
1147 res = eap_peap_decrypt(sm, data, ret, req, &msg,
1152 if (ret->methodState == METHOD_DONE) {
1153 ret->allowNotifications = FALSE;
1157 wpabuf_clear_free(resp);
1158 return eap_peer_tls_build_ack(id, EAP_TYPE_PEAP,
1159 data->peap_version);
1166 static Boolean eap_peap_has_reauth_data(struct eap_sm *sm, void *priv)
1168 struct eap_peap_data *data = priv;
1169 return tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1170 data->phase2_success;
1174 static void eap_peap_deinit_for_reauth(struct eap_sm *sm, void *priv)
1176 struct eap_peap_data *data = priv;
1178 if (data->phase2_priv && data->phase2_method &&
1179 data->phase2_method->deinit_for_reauth)
1180 data->phase2_method->deinit_for_reauth(sm, data->phase2_priv);
1181 wpabuf_clear_free(data->pending_phase2_req);
1182 data->pending_phase2_req = NULL;
1183 wpabuf_clear_free(data->pending_resp);
1184 data->pending_resp = NULL;
1185 data->crypto_binding_used = 0;
1189 static void * eap_peap_init_for_reauth(struct eap_sm *sm, void *priv)
1191 struct eap_peap_data *data = priv;
1192 eap_peap_free_key(data);
1193 os_free(data->session_id);
1194 data->session_id = NULL;
1195 if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
1199 if (data->phase2_priv && data->phase2_method &&
1200 data->phase2_method->init_for_reauth)
1201 data->phase2_method->init_for_reauth(sm, data->phase2_priv);
1202 data->phase2_success = 0;
1203 data->phase2_eap_success = 0;
1204 data->phase2_eap_started = 0;
1207 sm->peap_done = FALSE;
1212 static int eap_peap_get_status(struct eap_sm *sm, void *priv, char *buf,
1213 size_t buflen, int verbose)
1215 struct eap_peap_data *data = priv;
1218 len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
1219 if (data->phase2_method) {
1220 ret = os_snprintf(buf + len, buflen - len,
1221 "EAP-PEAPv%d Phase2 method=%s\n",
1223 data->phase2_method->name);
1224 if (os_snprintf_error(buflen - len, ret))
1232 static Boolean eap_peap_isKeyAvailable(struct eap_sm *sm, void *priv)
1234 struct eap_peap_data *data = priv;
1235 return data->key_data != NULL && data->phase2_success;
1239 static u8 * eap_peap_getKey(struct eap_sm *sm, void *priv, size_t *len)
1241 struct eap_peap_data *data = priv;
1244 if (data->key_data == NULL || !data->phase2_success)
1247 key = os_malloc(EAP_TLS_KEY_LEN);
1251 *len = EAP_TLS_KEY_LEN;
1253 if (data->crypto_binding_used) {
1256 * Note: It looks like Microsoft implementation requires null
1257 * termination for this label while the one used for deriving
1258 * IPMK|CMK did not use null termination.
1260 if (peap_prfplus(data->peap_version, data->ipmk, 40,
1261 "Session Key Generating Function",
1262 (u8 *) "\00", 1, csk, sizeof(csk)) < 0) {
1266 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: CSK", csk, sizeof(csk));
1267 os_memcpy(key, csk, EAP_TLS_KEY_LEN);
1268 wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Derived key",
1269 key, EAP_TLS_KEY_LEN);
1270 os_memset(csk, 0, sizeof(csk));
1272 os_memcpy(key, data->key_data, EAP_TLS_KEY_LEN);
1278 static u8 * eap_peap_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
1280 struct eap_peap_data *data = priv;
1283 if (!data->key_data || !data->phase2_success)
1286 if (data->crypto_binding_used) {
1287 /* [MS-PEAP] does not define EMSK derivation */
1291 key = os_memdup(data->key_data + EAP_TLS_KEY_LEN, EAP_EMSK_LEN);
1295 *len = EAP_EMSK_LEN;
1301 static u8 * eap_peap_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
1303 struct eap_peap_data *data = priv;
1306 if (data->session_id == NULL || !data->phase2_success)
1309 id = os_memdup(data->session_id, data->id_len);
1313 *len = data->id_len;
1319 int eap_peer_peap_register(void)
1321 struct eap_method *eap;
1323 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
1324 EAP_VENDOR_IETF, EAP_TYPE_PEAP, "PEAP");
1328 eap->init = eap_peap_init;
1329 eap->deinit = eap_peap_deinit;
1330 eap->process = eap_peap_process;
1331 eap->isKeyAvailable = eap_peap_isKeyAvailable;
1332 eap->getKey = eap_peap_getKey;
1333 eap->get_emsk = eap_peap_get_emsk;
1334 eap->get_status = eap_peap_get_status;
1335 eap->has_reauth_data = eap_peap_has_reauth_data;
1336 eap->deinit_for_reauth = eap_peap_deinit_for_reauth;
1337 eap->init_for_reauth = eap_peap_init_for_reauth;
1338 eap->getSessionId = eap_peap_get_session_id;
1340 return eap_peer_method_register(eap);