2 * hostapd / EAP-PEAP (draft-josefsson-pppext-eap-tls-eap-10.txt)
3 * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
18 #include "crypto/sha1.h"
19 #include "crypto/tls.h"
20 #include "crypto/random.h"
22 #include "eap_tls_common.h"
23 #include "eap_common/eap_tlv_common.h"
24 #include "eap_common/eap_peap_common.h"
28 /* Maximum supported PEAP version
29 * 0 = Microsoft's PEAP version 0; draft-kamath-pppext-peapv0-00.txt
30 * 1 = draft-josefsson-ppext-eap-tls-eap-05.txt
31 * 2 = draft-josefsson-ppext-eap-tls-eap-10.txt
33 #define EAP_PEAP_VERSION 1
36 static void eap_peap_reset(struct eap_sm *sm, void *priv);
39 struct eap_peap_data {
40 struct eap_ssl_data ssl;
42 START, PHASE1, PHASE1_ID2, PHASE2_START, PHASE2_ID,
43 PHASE2_METHOD, PHASE2_SOH,
44 PHASE2_TLV, SUCCESS_REQ, FAILURE_REQ, SUCCESS, FAILURE
49 const struct eap_method *phase2_method;
52 struct wpabuf *pending_phase2_resp;
53 enum { TLV_REQ_NONE, TLV_REQ_SUCCESS, TLV_REQ_FAILURE } tlv_request;
54 int crypto_binding_sent;
55 int crypto_binding_used;
56 enum { NO_BINDING, OPTIONAL_BINDING, REQUIRE_BINDING } crypto_binding;
61 size_t phase2_key_len;
62 struct wpabuf *soh_response;
66 static const char * eap_peap_state_txt(int state)
76 return "PHASE2_START";
80 return "PHASE2_METHOD";
99 static void eap_peap_state(struct eap_peap_data *data, int state)
101 wpa_printf(MSG_DEBUG, "EAP-PEAP: %s -> %s",
102 eap_peap_state_txt(data->state),
103 eap_peap_state_txt(state));
108 static struct wpabuf * eap_peapv2_tlv_eap_payload(struct wpabuf *buf)
111 struct eap_tlv_hdr *tlv;
116 /* Encapsulate EAP packet in EAP-Payload TLV */
117 wpa_printf(MSG_DEBUG, "EAP-PEAPv2: Add EAP-Payload TLV");
118 e = wpabuf_alloc(sizeof(*tlv) + wpabuf_len(buf));
120 wpa_printf(MSG_DEBUG, "EAP-PEAPv2: Failed to allocate memory "
121 "for TLV encapsulation");
125 tlv = wpabuf_put(e, sizeof(*tlv));
126 tlv->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
127 EAP_TLV_EAP_PAYLOAD_TLV);
128 tlv->length = host_to_be16(wpabuf_len(buf));
129 wpabuf_put_buf(e, buf);
135 static void eap_peap_req_success(struct eap_sm *sm,
136 struct eap_peap_data *data)
138 if (data->state == FAILURE || data->state == FAILURE_REQ) {
139 eap_peap_state(data, FAILURE);
143 if (data->peap_version == 0) {
144 data->tlv_request = TLV_REQ_SUCCESS;
145 eap_peap_state(data, PHASE2_TLV);
147 eap_peap_state(data, SUCCESS_REQ);
152 static void eap_peap_req_failure(struct eap_sm *sm,
153 struct eap_peap_data *data)
155 if (data->state == FAILURE || data->state == FAILURE_REQ ||
156 data->state == SUCCESS_REQ || data->tlv_request != TLV_REQ_NONE) {
157 eap_peap_state(data, FAILURE);
161 if (data->peap_version == 0) {
162 data->tlv_request = TLV_REQ_FAILURE;
163 eap_peap_state(data, PHASE2_TLV);
165 eap_peap_state(data, FAILURE_REQ);
170 static void * eap_peap_init(struct eap_sm *sm)
172 struct eap_peap_data *data;
174 data = os_zalloc(sizeof(*data));
177 data->peap_version = EAP_PEAP_VERSION;
178 data->force_version = -1;
179 if (sm->user && sm->user->force_version >= 0) {
180 data->force_version = sm->user->force_version;
181 wpa_printf(MSG_DEBUG, "EAP-PEAP: forcing version %d",
182 data->force_version);
183 data->peap_version = data->force_version;
186 data->crypto_binding = OPTIONAL_BINDING;
188 if (eap_server_tls_ssl_init(sm, &data->ssl, 0)) {
189 wpa_printf(MSG_INFO, "EAP-PEAP: Failed to initialize SSL.");
190 eap_peap_reset(sm, data);
198 static void eap_peap_reset(struct eap_sm *sm, void *priv)
200 struct eap_peap_data *data = priv;
203 if (data->phase2_priv && data->phase2_method)
204 data->phase2_method->reset(sm, data->phase2_priv);
205 eap_server_tls_ssl_deinit(sm, &data->ssl);
206 wpabuf_free(data->pending_phase2_resp);
207 os_free(data->phase2_key);
208 wpabuf_free(data->soh_response);
213 static struct wpabuf * eap_peap_build_start(struct eap_sm *sm,
214 struct eap_peap_data *data, u8 id)
218 req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_PEAP, 1,
219 EAP_CODE_REQUEST, id);
221 wpa_printf(MSG_ERROR, "EAP-PEAP: Failed to allocate memory for"
223 eap_peap_state(data, FAILURE);
227 wpabuf_put_u8(req, EAP_TLS_FLAGS_START | data->peap_version);
229 eap_peap_state(data, PHASE1);
235 static struct wpabuf * eap_peap_build_phase2_req(struct eap_sm *sm,
236 struct eap_peap_data *data,
239 struct wpabuf *buf, *encr_req, msgbuf;
243 if (data->phase2_method == NULL || data->phase2_priv == NULL) {
244 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 method not ready");
247 buf = data->phase2_method->buildReq(sm, data->phase2_priv, id);
248 if (data->peap_version >= 2 && buf)
249 buf = eap_peapv2_tlv_eap_payload(buf);
253 req = wpabuf_head(buf);
254 req_len = wpabuf_len(buf);
255 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: Encrypting Phase 2 data",
258 if (data->peap_version == 0 &&
259 data->phase2_method->method != EAP_TYPE_TLV) {
260 req += sizeof(struct eap_hdr);
261 req_len -= sizeof(struct eap_hdr);
264 wpabuf_set(&msgbuf, req, req_len);
265 encr_req = eap_server_tls_encrypt(sm, &data->ssl, &msgbuf);
272 #ifdef EAP_SERVER_TNC
273 static struct wpabuf * eap_peap_build_phase2_soh(struct eap_sm *sm,
274 struct eap_peap_data *data,
277 struct wpabuf *buf1, *buf, *encr_req, msgbuf;
281 buf1 = tncs_build_soh_request();
285 buf = eap_msg_alloc(EAP_VENDOR_MICROSOFT, 0x21, wpabuf_len(buf1),
286 EAP_CODE_REQUEST, id);
291 wpabuf_put_buf(buf, buf1);
294 req = wpabuf_head(buf);
295 req_len = wpabuf_len(buf);
297 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: Encrypting Phase 2 SOH data",
300 req += sizeof(struct eap_hdr);
301 req_len -= sizeof(struct eap_hdr);
302 wpabuf_set(&msgbuf, req, req_len);
304 encr_req = eap_server_tls_encrypt(sm, &data->ssl, &msgbuf);
309 #endif /* EAP_SERVER_TNC */
312 static void eap_peap_get_isk(struct eap_peap_data *data,
313 u8 *isk, size_t isk_len)
317 os_memset(isk, 0, isk_len);
318 if (data->phase2_key == NULL)
321 key_len = data->phase2_key_len;
322 if (key_len > isk_len)
324 os_memcpy(isk, data->phase2_key, key_len);
328 static int eap_peap_derive_cmk(struct eap_sm *sm, struct eap_peap_data *data)
331 u8 isk[32], imck[60];
334 * Tunnel key (TK) is the first 60 octets of the key generated by
335 * phase 1 of PEAP (based on TLS).
337 tk = eap_server_tls_derive_key(sm, &data->ssl, "client EAP encryption",
341 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: TK", tk, 60);
343 eap_peap_get_isk(data, isk, sizeof(isk));
344 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: ISK", isk, sizeof(isk));
347 * IPMK Seed = "Inner Methods Compound Keys" | ISK
348 * TempKey = First 40 octets of TK
349 * IPMK|CMK = PRF+(TempKey, IPMK Seed, 60)
350 * (note: draft-josefsson-pppext-eap-tls-eap-10.txt includes a space
351 * in the end of the label just before ISK; is that just a typo?)
353 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: TempKey", tk, 40);
354 if (peap_prfplus(data->peap_version, tk, 40,
355 "Inner Methods Compound Keys",
356 isk, sizeof(isk), imck, sizeof(imck)) < 0) {
360 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: IMCK (IPMKj)",
365 /* TODO: fast-connect: IPMK|CMK = TK */
366 os_memcpy(data->ipmk, imck, 40);
367 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: IPMK (S-IPMKj)", data->ipmk, 40);
368 os_memcpy(data->cmk, imck + 40, 20);
369 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: CMK (CMKj)", data->cmk, 20);
375 static struct wpabuf * eap_peap_build_phase2_tlv(struct eap_sm *sm,
376 struct eap_peap_data *data,
379 struct wpabuf *buf, *encr_req;
382 mlen = 6; /* Result TLV */
383 if (data->crypto_binding != NO_BINDING)
384 mlen += 60; /* Cryptobinding TLV */
385 #ifdef EAP_SERVER_TNC
386 if (data->soh_response)
387 mlen += wpabuf_len(data->soh_response);
388 #endif /* EAP_SERVER_TNC */
390 buf = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TLV, mlen,
391 EAP_CODE_REQUEST, id);
395 wpabuf_put_u8(buf, 0x80); /* Mandatory */
396 wpabuf_put_u8(buf, EAP_TLV_RESULT_TLV);
398 wpabuf_put_be16(buf, 2);
400 wpabuf_put_be16(buf, data->tlv_request == TLV_REQ_SUCCESS ?
401 EAP_TLV_RESULT_SUCCESS : EAP_TLV_RESULT_FAILURE);
403 if (data->peap_version == 0 && data->tlv_request == TLV_REQ_SUCCESS &&
404 data->crypto_binding != NO_BINDING) {
406 u8 eap_type = EAP_TYPE_PEAP;
411 #ifdef EAP_SERVER_TNC
412 if (data->soh_response) {
413 wpa_printf(MSG_DEBUG, "EAP-PEAP: Adding MS-SOH "
415 wpabuf_put_buf(buf, data->soh_response);
416 wpabuf_free(data->soh_response);
417 data->soh_response = NULL;
419 #endif /* EAP_SERVER_TNC */
421 if (eap_peap_derive_cmk(sm, data) < 0 ||
422 random_get_bytes(data->binding_nonce, 32)) {
427 /* Compound_MAC: HMAC-SHA1-160(cryptobinding TLV | EAP type) */
428 addr[0] = wpabuf_put(buf, 0);
433 tlv_type = EAP_TLV_CRYPTO_BINDING_TLV;
434 if (data->peap_version >= 2)
435 tlv_type |= EAP_TLV_TYPE_MANDATORY;
436 wpabuf_put_be16(buf, tlv_type);
437 wpabuf_put_be16(buf, 56);
439 wpabuf_put_u8(buf, 0); /* Reserved */
440 wpabuf_put_u8(buf, data->peap_version); /* Version */
441 wpabuf_put_u8(buf, data->recv_version); /* RecvVersion */
442 wpabuf_put_u8(buf, 0); /* SubType: 0 = Request, 1 = Response */
443 wpabuf_put_data(buf, data->binding_nonce, 32); /* Nonce */
444 mac = wpabuf_put(buf, 20); /* Compound_MAC */
445 wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC CMK",
447 wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC data 1",
449 wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC data 2",
451 hmac_sha1_vector(data->cmk, 20, 2, addr, len, mac);
452 wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC",
454 data->crypto_binding_sent = 1;
457 wpa_hexdump_buf_key(MSG_DEBUG, "EAP-PEAP: Encrypting Phase 2 TLV data",
460 encr_req = eap_server_tls_encrypt(sm, &data->ssl, buf);
467 static struct wpabuf * eap_peap_build_phase2_term(struct eap_sm *sm,
468 struct eap_peap_data *data,
471 struct wpabuf *encr_req, msgbuf;
475 req_len = sizeof(*hdr);
476 hdr = os_zalloc(req_len);
480 hdr->code = success ? EAP_CODE_SUCCESS : EAP_CODE_FAILURE;
481 hdr->identifier = id;
482 hdr->length = host_to_be16(req_len);
484 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: Encrypting Phase 2 data",
485 (u8 *) hdr, req_len);
487 wpabuf_set(&msgbuf, hdr, req_len);
488 encr_req = eap_server_tls_encrypt(sm, &data->ssl, &msgbuf);
495 static struct wpabuf * eap_peap_buildReq(struct eap_sm *sm, void *priv, u8 id)
497 struct eap_peap_data *data = priv;
499 if (data->ssl.state == FRAG_ACK) {
500 return eap_server_tls_build_ack(id, EAP_TYPE_PEAP,
504 if (data->ssl.state == WAIT_FRAG_ACK) {
505 return eap_server_tls_build_msg(&data->ssl, EAP_TYPE_PEAP,
506 data->peap_version, id);
509 switch (data->state) {
511 return eap_peap_build_start(sm, data, id);
514 if (data->peap_version < 2 &&
515 tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
516 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase1 done, "
518 eap_peap_state(data, PHASE2_START);
523 wpabuf_free(data->ssl.tls_out);
524 data->ssl.tls_out_pos = 0;
525 data->ssl.tls_out = eap_peap_build_phase2_req(sm, data, id);
527 #ifdef EAP_SERVER_TNC
529 wpabuf_free(data->ssl.tls_out);
530 data->ssl.tls_out_pos = 0;
531 data->ssl.tls_out = eap_peap_build_phase2_soh(sm, data, id);
533 #endif /* EAP_SERVER_TNC */
535 wpabuf_free(data->ssl.tls_out);
536 data->ssl.tls_out_pos = 0;
537 data->ssl.tls_out = eap_peap_build_phase2_tlv(sm, data, id);
540 wpabuf_free(data->ssl.tls_out);
541 data->ssl.tls_out_pos = 0;
542 data->ssl.tls_out = eap_peap_build_phase2_term(sm, data, id,
546 wpabuf_free(data->ssl.tls_out);
547 data->ssl.tls_out_pos = 0;
548 data->ssl.tls_out = eap_peap_build_phase2_term(sm, data, id,
552 wpa_printf(MSG_DEBUG, "EAP-PEAP: %s - unexpected state %d",
553 __func__, data->state);
557 return eap_server_tls_build_msg(&data->ssl, EAP_TYPE_PEAP,
558 data->peap_version, id);
562 static Boolean eap_peap_check(struct eap_sm *sm, void *priv,
563 struct wpabuf *respData)
568 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_PEAP, respData, &len);
569 if (pos == NULL || len < 1) {
570 wpa_printf(MSG_INFO, "EAP-PEAP: Invalid frame");
578 static int eap_peap_phase2_init(struct eap_sm *sm, struct eap_peap_data *data,
581 if (data->phase2_priv && data->phase2_method) {
582 data->phase2_method->reset(sm, data->phase2_priv);
583 data->phase2_method = NULL;
584 data->phase2_priv = NULL;
586 data->phase2_method = eap_server_get_eap_method(EAP_VENDOR_IETF,
588 if (!data->phase2_method)
592 data->phase2_priv = data->phase2_method->init(sm);
598 static int eap_tlv_validate_cryptobinding(struct eap_sm *sm,
599 struct eap_peap_data *data,
600 const u8 *crypto_tlv,
601 size_t crypto_tlv_len)
603 u8 buf[61], mac[SHA1_MAC_LEN];
606 if (crypto_tlv_len != 4 + 56) {
607 wpa_printf(MSG_DEBUG, "EAP-PEAP: Invalid cryptobinding TLV "
608 "length %d", (int) crypto_tlv_len);
613 pos += 4; /* TLV header */
614 if (pos[1] != data->peap_version) {
615 wpa_printf(MSG_DEBUG, "EAP-PEAP: Cryptobinding TLV Version "
616 "mismatch (was %d; expected %d)",
617 pos[1], data->peap_version);
622 wpa_printf(MSG_DEBUG, "EAP-PEAP: Unexpected Cryptobinding TLV "
623 "SubType %d", pos[3]);
627 pos += 32; /* Nonce */
629 /* Compound_MAC: HMAC-SHA1-160(cryptobinding TLV | EAP type) */
630 os_memcpy(buf, crypto_tlv, 60);
631 os_memset(buf + 4 + 4 + 32, 0, 20); /* Compound_MAC */
632 buf[60] = EAP_TYPE_PEAP;
633 hmac_sha1(data->cmk, 20, buf, sizeof(buf), mac);
635 if (os_memcmp(mac, pos, SHA1_MAC_LEN) != 0) {
636 wpa_printf(MSG_DEBUG, "EAP-PEAP: Invalid Compound_MAC in "
637 "cryptobinding TLV");
638 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: CMK", data->cmk, 20);
639 wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Cryptobinding seed data",
644 wpa_printf(MSG_DEBUG, "EAP-PEAP: Valid cryptobinding TLV received");
650 static void eap_peap_process_phase2_tlv(struct eap_sm *sm,
651 struct eap_peap_data *data,
652 struct wpabuf *in_data)
656 const u8 *result_tlv = NULL, *crypto_tlv = NULL;
657 size_t result_tlv_len = 0, crypto_tlv_len = 0;
658 int tlv_type, mandatory, tlv_len;
660 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_TLV, in_data, &left);
662 wpa_printf(MSG_DEBUG, "EAP-PEAP: Invalid EAP-TLV header");
667 wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Received TLVs", pos, left);
669 mandatory = !!(pos[0] & 0x80);
670 tlv_type = pos[0] & 0x3f;
671 tlv_type = (tlv_type << 8) | pos[1];
672 tlv_len = ((int) pos[2] << 8) | pos[3];
675 if ((size_t) tlv_len > left) {
676 wpa_printf(MSG_DEBUG, "EAP-PEAP: TLV underrun "
677 "(tlv_len=%d left=%lu)", tlv_len,
678 (unsigned long) left);
679 eap_peap_state(data, FAILURE);
683 case EAP_TLV_RESULT_TLV:
685 result_tlv_len = tlv_len;
687 case EAP_TLV_CRYPTO_BINDING_TLV:
689 crypto_tlv_len = tlv_len;
692 wpa_printf(MSG_DEBUG, "EAP-PEAP: Unsupported TLV Type "
694 mandatory ? " (mandatory)" : "");
696 eap_peap_state(data, FAILURE);
699 /* Ignore this TLV, but process other TLVs */
707 wpa_printf(MSG_DEBUG, "EAP-PEAP: Last TLV too short in "
708 "Request (left=%lu)", (unsigned long) left);
709 eap_peap_state(data, FAILURE);
713 /* Process supported TLVs */
714 if (crypto_tlv && data->crypto_binding_sent) {
715 wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Cryptobinding TLV",
716 crypto_tlv, crypto_tlv_len);
717 if (eap_tlv_validate_cryptobinding(sm, data, crypto_tlv - 4,
718 crypto_tlv_len + 4) < 0) {
719 eap_peap_state(data, FAILURE);
722 data->crypto_binding_used = 1;
723 } else if (!crypto_tlv && data->crypto_binding_sent &&
724 data->crypto_binding == REQUIRE_BINDING) {
725 wpa_printf(MSG_DEBUG, "EAP-PEAP: No cryptobinding TLV");
726 eap_peap_state(data, FAILURE);
732 const char *requested;
734 wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Result TLV",
735 result_tlv, result_tlv_len);
736 if (result_tlv_len < 2) {
737 wpa_printf(MSG_INFO, "EAP-PEAP: Too short Result TLV "
739 (unsigned long) result_tlv_len);
740 eap_peap_state(data, FAILURE);
743 requested = data->tlv_request == TLV_REQ_SUCCESS ? "Success" :
745 status = WPA_GET_BE16(result_tlv);
746 if (status == EAP_TLV_RESULT_SUCCESS) {
747 wpa_printf(MSG_INFO, "EAP-PEAP: TLV Result - Success "
748 "- requested %s", requested);
749 if (data->tlv_request == TLV_REQ_SUCCESS)
750 eap_peap_state(data, SUCCESS);
752 eap_peap_state(data, FAILURE);
754 } else if (status == EAP_TLV_RESULT_FAILURE) {
755 wpa_printf(MSG_INFO, "EAP-PEAP: TLV Result - Failure "
756 "- requested %s", requested);
757 eap_peap_state(data, FAILURE);
759 wpa_printf(MSG_INFO, "EAP-PEAP: Unknown TLV Result "
760 "Status %d", status);
761 eap_peap_state(data, FAILURE);
767 #ifdef EAP_SERVER_TNC
768 static void eap_peap_process_phase2_soh(struct eap_sm *sm,
769 struct eap_peap_data *data,
770 struct wpabuf *in_data)
772 const u8 *pos, *vpos;
774 const u8 *soh_tlv = NULL;
775 size_t soh_tlv_len = 0;
776 int tlv_type, mandatory, tlv_len, vtlv_len;
780 pos = eap_hdr_validate(EAP_VENDOR_MICROSOFT, 0x21, in_data, &left);
782 wpa_printf(MSG_DEBUG, "EAP-PEAP: Not a valid SoH EAP "
783 "Extensions Method header - skip TNC");
788 wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Received TLVs (SoH)", pos, left);
790 mandatory = !!(pos[0] & 0x80);
791 tlv_type = pos[0] & 0x3f;
792 tlv_type = (tlv_type << 8) | pos[1];
793 tlv_len = ((int) pos[2] << 8) | pos[3];
796 if ((size_t) tlv_len > left) {
797 wpa_printf(MSG_DEBUG, "EAP-PEAP: TLV underrun "
798 "(tlv_len=%d left=%lu)", tlv_len,
799 (unsigned long) left);
800 eap_peap_state(data, FAILURE);
804 case EAP_TLV_VENDOR_SPECIFIC_TLV:
806 wpa_printf(MSG_DEBUG, "EAP-PEAP: Too short "
807 "vendor specific TLV (len=%d)",
809 eap_peap_state(data, FAILURE);
813 vendor_id = WPA_GET_BE32(pos);
814 if (vendor_id != EAP_VENDOR_MICROSOFT) {
816 eap_peap_state(data, FAILURE);
823 mandatory = !!(vpos[0] & 0x80);
824 tlv_type = vpos[0] & 0x3f;
825 tlv_type = (tlv_type << 8) | vpos[1];
826 vtlv_len = ((int) vpos[2] << 8) | vpos[3];
828 if (vpos + vtlv_len > pos + left) {
829 wpa_printf(MSG_DEBUG, "EAP-PEAP: Vendor TLV "
831 eap_peap_state(data, FAILURE);
837 soh_tlv_len = vtlv_len;
841 wpa_printf(MSG_DEBUG, "EAP-PEAP: Unsupported MS-TLV "
842 "Type %d%s", tlv_type,
843 mandatory ? " (mandatory)" : "");
845 eap_peap_state(data, FAILURE);
848 /* Ignore this TLV, but process other TLVs */
851 wpa_printf(MSG_DEBUG, "EAP-PEAP: Unsupported TLV Type "
853 mandatory ? " (mandatory)" : "");
855 eap_peap_state(data, FAILURE);
858 /* Ignore this TLV, but process other TLVs */
866 wpa_printf(MSG_DEBUG, "EAP-PEAP: Last TLV too short in "
867 "Request (left=%lu)", (unsigned long) left);
868 eap_peap_state(data, FAILURE);
872 /* Process supported TLVs */
875 wpabuf_free(data->soh_response);
876 data->soh_response = tncs_process_soh(soh_tlv, soh_tlv_len,
879 eap_peap_state(data, FAILURE);
883 wpa_printf(MSG_DEBUG, "EAP-PEAP: No SoH TLV received");
884 eap_peap_state(data, FAILURE);
889 eap_peap_state(data, PHASE2_METHOD);
890 next_type = sm->user->methods[0].method;
891 sm->user_eap_method_index = 1;
892 wpa_printf(MSG_DEBUG, "EAP-PEAP: try EAP type %d", next_type);
893 eap_peap_phase2_init(sm, data, next_type);
895 #endif /* EAP_SERVER_TNC */
898 static void eap_peap_process_phase2_response(struct eap_sm *sm,
899 struct eap_peap_data *data,
900 struct wpabuf *in_data)
902 u8 next_type = EAP_TYPE_NONE;
903 const struct eap_hdr *hdr;
907 if (data->state == PHASE2_TLV) {
908 eap_peap_process_phase2_tlv(sm, data, in_data);
912 #ifdef EAP_SERVER_TNC
913 if (data->state == PHASE2_SOH) {
914 eap_peap_process_phase2_soh(sm, data, in_data);
917 #endif /* EAP_SERVER_TNC */
919 if (data->phase2_priv == NULL) {
920 wpa_printf(MSG_DEBUG, "EAP-PEAP: %s - Phase2 not "
921 "initialized?!", __func__);
925 hdr = wpabuf_head(in_data);
926 pos = (const u8 *) (hdr + 1);
928 if (wpabuf_len(in_data) > sizeof(*hdr) && *pos == EAP_TYPE_NAK) {
929 left = wpabuf_len(in_data) - sizeof(*hdr);
930 wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Phase2 type Nak'ed; "
931 "allowed types", pos + 1, left - 1);
932 eap_sm_process_nak(sm, pos + 1, left - 1);
933 if (sm->user && sm->user_eap_method_index < EAP_MAX_METHODS &&
934 sm->user->methods[sm->user_eap_method_index].method !=
936 next_type = sm->user->methods[
937 sm->user_eap_method_index++].method;
938 wpa_printf(MSG_DEBUG, "EAP-PEAP: try EAP type %d",
941 eap_peap_req_failure(sm, data);
942 next_type = EAP_TYPE_NONE;
944 eap_peap_phase2_init(sm, data, next_type);
948 if (data->phase2_method->check(sm, data->phase2_priv, in_data)) {
949 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase2 check() asked to "
950 "ignore the packet");
954 data->phase2_method->process(sm, data->phase2_priv, in_data);
956 if (sm->method_pending == METHOD_PENDING_WAIT) {
957 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase2 method is in "
958 "pending wait state - save decrypted response");
959 wpabuf_free(data->pending_phase2_resp);
960 data->pending_phase2_resp = wpabuf_dup(in_data);
963 if (!data->phase2_method->isDone(sm, data->phase2_priv))
966 if (!data->phase2_method->isSuccess(sm, data->phase2_priv)) {
967 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase2 method failed");
968 eap_peap_req_failure(sm, data);
969 next_type = EAP_TYPE_NONE;
970 eap_peap_phase2_init(sm, data, next_type);
974 os_free(data->phase2_key);
975 if (data->phase2_method->getKey) {
976 data->phase2_key = data->phase2_method->getKey(
977 sm, data->phase2_priv, &data->phase2_key_len);
978 if (data->phase2_key == NULL) {
979 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase2 getKey "
981 eap_peap_req_failure(sm, data);
982 eap_peap_phase2_init(sm, data, EAP_TYPE_NONE);
987 switch (data->state) {
991 if (eap_user_get(sm, sm->identity, sm->identity_len, 1) != 0) {
992 wpa_hexdump_ascii(MSG_DEBUG, "EAP_PEAP: Phase2 "
993 "Identity not found in the user "
995 sm->identity, sm->identity_len);
996 eap_peap_req_failure(sm, data);
997 next_type = EAP_TYPE_NONE;
1001 #ifdef EAP_SERVER_TNC
1002 if (data->state != PHASE2_SOH && sm->tnc &&
1003 data->peap_version == 0) {
1004 eap_peap_state(data, PHASE2_SOH);
1005 wpa_printf(MSG_DEBUG, "EAP-PEAP: Try to initialize "
1007 next_type = EAP_TYPE_NONE;
1010 #endif /* EAP_SERVER_TNC */
1012 eap_peap_state(data, PHASE2_METHOD);
1013 next_type = sm->user->methods[0].method;
1014 sm->user_eap_method_index = 1;
1015 wpa_printf(MSG_DEBUG, "EAP-PEAP: try EAP type %d", next_type);
1018 eap_peap_req_success(sm, data);
1019 next_type = EAP_TYPE_NONE;
1024 wpa_printf(MSG_DEBUG, "EAP-PEAP: %s - unexpected state %d",
1025 __func__, data->state);
1029 eap_peap_phase2_init(sm, data, next_type);
1033 static void eap_peap_process_phase2(struct eap_sm *sm,
1034 struct eap_peap_data *data,
1035 const struct wpabuf *respData,
1036 struct wpabuf *in_buf)
1038 struct wpabuf *in_decrypted;
1039 const struct eap_hdr *hdr;
1042 wpa_printf(MSG_DEBUG, "EAP-PEAP: received %lu bytes encrypted data for"
1043 " Phase 2", (unsigned long) wpabuf_len(in_buf));
1045 if (data->pending_phase2_resp) {
1046 wpa_printf(MSG_DEBUG, "EAP-PEAP: Pending Phase 2 response - "
1047 "skip decryption and use old data");
1048 eap_peap_process_phase2_response(sm, data,
1049 data->pending_phase2_resp);
1050 wpabuf_free(data->pending_phase2_resp);
1051 data->pending_phase2_resp = NULL;
1055 in_decrypted = tls_connection_decrypt(sm->ssl_ctx, data->ssl.conn,
1057 if (in_decrypted == NULL) {
1058 wpa_printf(MSG_INFO, "EAP-PEAP: Failed to decrypt Phase 2 "
1060 eap_peap_state(data, FAILURE);
1064 wpa_hexdump_buf_key(MSG_DEBUG, "EAP-PEAP: Decrypted Phase 2 EAP",
1067 if (data->peap_version == 0 && data->state != PHASE2_TLV) {
1068 const struct eap_hdr *resp;
1069 struct eap_hdr *nhdr;
1070 struct wpabuf *nbuf =
1071 wpabuf_alloc(sizeof(struct eap_hdr) +
1072 wpabuf_len(in_decrypted));
1074 wpabuf_free(in_decrypted);
1078 resp = wpabuf_head(respData);
1079 nhdr = wpabuf_put(nbuf, sizeof(*nhdr));
1080 nhdr->code = resp->code;
1081 nhdr->identifier = resp->identifier;
1082 nhdr->length = host_to_be16(sizeof(struct eap_hdr) +
1083 wpabuf_len(in_decrypted));
1084 wpabuf_put_buf(nbuf, in_decrypted);
1085 wpabuf_free(in_decrypted);
1087 in_decrypted = nbuf;
1088 } else if (data->peap_version >= 2) {
1089 struct eap_tlv_hdr *tlv;
1090 struct wpabuf *nmsg;
1092 if (wpabuf_len(in_decrypted) < sizeof(*tlv) + sizeof(*hdr)) {
1093 wpa_printf(MSG_INFO, "EAP-PEAPv2: Too short Phase 2 "
1095 wpabuf_free(in_decrypted);
1098 tlv = wpabuf_mhead(in_decrypted);
1099 if ((be_to_host16(tlv->tlv_type) & EAP_TLV_TYPE_MASK) !=
1100 EAP_TLV_EAP_PAYLOAD_TLV) {
1101 wpa_printf(MSG_INFO, "EAP-PEAPv2: Not an EAP TLV");
1102 wpabuf_free(in_decrypted);
1105 if (sizeof(*tlv) + be_to_host16(tlv->length) >
1106 wpabuf_len(in_decrypted)) {
1107 wpa_printf(MSG_INFO, "EAP-PEAPv2: Invalid EAP TLV "
1109 wpabuf_free(in_decrypted);
1112 hdr = (struct eap_hdr *) (tlv + 1);
1113 if (be_to_host16(hdr->length) > be_to_host16(tlv->length)) {
1114 wpa_printf(MSG_INFO, "EAP-PEAPv2: No room for full "
1115 "EAP packet in EAP TLV");
1116 wpabuf_free(in_decrypted);
1120 nmsg = wpabuf_alloc(be_to_host16(hdr->length));
1122 wpabuf_free(in_decrypted);
1126 wpabuf_put_data(nmsg, hdr, be_to_host16(hdr->length));
1127 wpabuf_free(in_decrypted);
1128 in_decrypted = nmsg;
1131 hdr = wpabuf_head(in_decrypted);
1132 if (wpabuf_len(in_decrypted) < (int) sizeof(*hdr)) {
1133 wpa_printf(MSG_INFO, "EAP-PEAP: Too short Phase 2 "
1134 "EAP frame (len=%lu)",
1135 (unsigned long) wpabuf_len(in_decrypted));
1136 wpabuf_free(in_decrypted);
1137 eap_peap_req_failure(sm, data);
1140 len = be_to_host16(hdr->length);
1141 if (len > wpabuf_len(in_decrypted)) {
1142 wpa_printf(MSG_INFO, "EAP-PEAP: Length mismatch in "
1143 "Phase 2 EAP frame (len=%lu hdr->length=%lu)",
1144 (unsigned long) wpabuf_len(in_decrypted),
1145 (unsigned long) len);
1146 wpabuf_free(in_decrypted);
1147 eap_peap_req_failure(sm, data);
1150 wpa_printf(MSG_DEBUG, "EAP-PEAP: received Phase 2: code=%d "
1151 "identifier=%d length=%lu", hdr->code, hdr->identifier,
1152 (unsigned long) len);
1153 switch (hdr->code) {
1154 case EAP_CODE_RESPONSE:
1155 eap_peap_process_phase2_response(sm, data, in_decrypted);
1157 case EAP_CODE_SUCCESS:
1158 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 Success");
1159 if (data->state == SUCCESS_REQ) {
1160 eap_peap_state(data, SUCCESS);
1163 case EAP_CODE_FAILURE:
1164 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 Failure");
1165 eap_peap_state(data, FAILURE);
1168 wpa_printf(MSG_INFO, "EAP-PEAP: Unexpected code=%d in "
1169 "Phase 2 EAP header", hdr->code);
1173 wpabuf_free(in_decrypted);
1177 static int eap_peapv2_start_phase2(struct eap_sm *sm,
1178 struct eap_peap_data *data)
1180 struct wpabuf *buf, *buf2;
1182 wpa_printf(MSG_DEBUG, "EAP-PEAPv2: Phase1 done, include first Phase2 "
1183 "payload in the same message");
1184 eap_peap_state(data, PHASE1_ID2);
1185 if (eap_peap_phase2_init(sm, data, EAP_TYPE_IDENTITY))
1188 /* TODO: which Id to use here? */
1189 buf = data->phase2_method->buildReq(sm, data->phase2_priv, 6);
1193 buf2 = eap_peapv2_tlv_eap_payload(buf);
1197 wpa_hexdump_buf(MSG_DEBUG, "EAP-PEAPv2: Identity Request", buf2);
1199 buf = tls_connection_encrypt(sm->ssl_ctx, data->ssl.conn,
1204 wpa_printf(MSG_INFO, "EAP-PEAPv2: Failed to encrypt Phase 2 "
1209 wpa_hexdump_buf(MSG_DEBUG, "EAP-PEAPv2: Encrypted Identity Request",
1212 /* Append TLS data into the pending buffer after the Server Finished */
1213 if (wpabuf_resize(&data->ssl.tls_out, wpabuf_len(buf)) < 0) {
1217 wpabuf_put_buf(data->ssl.tls_out, buf);
1224 static int eap_peap_process_version(struct eap_sm *sm, void *priv,
1227 struct eap_peap_data *data = priv;
1229 data->recv_version = peer_version;
1230 if (data->force_version >= 0 && peer_version != data->force_version) {
1231 wpa_printf(MSG_INFO, "EAP-PEAP: peer did not select the forced"
1232 " version (forced=%d peer=%d) - reject",
1233 data->force_version, peer_version);
1236 if (peer_version < data->peap_version) {
1237 wpa_printf(MSG_DEBUG, "EAP-PEAP: peer ver=%d, own ver=%d; "
1239 peer_version, data->peap_version, peer_version);
1240 data->peap_version = peer_version;
1247 static void eap_peap_process_msg(struct eap_sm *sm, void *priv,
1248 const struct wpabuf *respData)
1250 struct eap_peap_data *data = priv;
1252 switch (data->state) {
1254 if (eap_server_tls_phase1(sm, &data->ssl) < 0) {
1255 eap_peap_state(data, FAILURE);
1259 if (data->peap_version >= 2 &&
1260 tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
1261 if (eap_peapv2_start_phase2(sm, data)) {
1262 eap_peap_state(data, FAILURE);
1268 eap_peap_state(data, PHASE2_ID);
1269 eap_peap_phase2_init(sm, data, EAP_TYPE_IDENTITY);
1276 eap_peap_process_phase2(sm, data, respData, data->ssl.tls_in);
1279 eap_peap_state(data, SUCCESS);
1282 eap_peap_state(data, FAILURE);
1285 wpa_printf(MSG_DEBUG, "EAP-PEAP: Unexpected state %d in %s",
1286 data->state, __func__);
1292 static void eap_peap_process(struct eap_sm *sm, void *priv,
1293 struct wpabuf *respData)
1295 struct eap_peap_data *data = priv;
1296 if (eap_server_tls_process(sm, &data->ssl, respData, data,
1297 EAP_TYPE_PEAP, eap_peap_process_version,
1298 eap_peap_process_msg) < 0)
1299 eap_peap_state(data, FAILURE);
1303 static Boolean eap_peap_isDone(struct eap_sm *sm, void *priv)
1305 struct eap_peap_data *data = priv;
1306 return data->state == SUCCESS || data->state == FAILURE;
1310 static u8 * eap_peap_getKey(struct eap_sm *sm, void *priv, size_t *len)
1312 struct eap_peap_data *data = priv;
1315 if (data->state != SUCCESS)
1318 if (data->crypto_binding_used) {
1321 * Note: It looks like Microsoft implementation requires null
1322 * termination for this label while the one used for deriving
1323 * IPMK|CMK did not use null termination.
1325 if (peap_prfplus(data->peap_version, data->ipmk, 40,
1326 "Session Key Generating Function",
1327 (u8 *) "\00", 1, csk, sizeof(csk)) < 0)
1329 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: CSK", csk, sizeof(csk));
1330 eapKeyData = os_malloc(EAP_TLS_KEY_LEN);
1332 os_memcpy(eapKeyData, csk, EAP_TLS_KEY_LEN);
1333 *len = EAP_TLS_KEY_LEN;
1334 wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Derived key",
1335 eapKeyData, EAP_TLS_KEY_LEN);
1337 wpa_printf(MSG_DEBUG, "EAP-PEAP: Failed to derive "
1344 /* TODO: PEAPv1 - different label in some cases */
1345 eapKeyData = eap_server_tls_derive_key(sm, &data->ssl,
1346 "client EAP encryption",
1349 *len = EAP_TLS_KEY_LEN;
1350 wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Derived key",
1351 eapKeyData, EAP_TLS_KEY_LEN);
1353 wpa_printf(MSG_DEBUG, "EAP-PEAP: Failed to derive key");
1360 static Boolean eap_peap_isSuccess(struct eap_sm *sm, void *priv)
1362 struct eap_peap_data *data = priv;
1363 return data->state == SUCCESS;
1367 int eap_server_peap_register(void)
1369 struct eap_method *eap;
1372 eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
1373 EAP_VENDOR_IETF, EAP_TYPE_PEAP, "PEAP");
1377 eap->init = eap_peap_init;
1378 eap->reset = eap_peap_reset;
1379 eap->buildReq = eap_peap_buildReq;
1380 eap->check = eap_peap_check;
1381 eap->process = eap_peap_process;
1382 eap->isDone = eap_peap_isDone;
1383 eap->getKey = eap_peap_getKey;
1384 eap->isSuccess = eap_peap_isSuccess;
1386 ret = eap_server_method_register(eap);
1388 eap_server_method_free(eap);