2 * EAP-FAST server (RFC 4851)
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.
21 #include "eap_tls_common.h"
23 #include "eap_common/eap_tlv_common.h"
24 #include "eap_common/eap_fast_common.h"
27 static void eap_fast_reset(struct eap_sm *sm, void *priv);
30 /* Private PAC-Opaque TLV types */
31 #define PAC_OPAQUE_TYPE_PAD 0
32 #define PAC_OPAQUE_TYPE_KEY 1
33 #define PAC_OPAQUE_TYPE_LIFETIME 2
34 #define PAC_OPAQUE_TYPE_IDENTITY 3
36 /* PAC-Key lifetime in seconds (hard limit) */
37 #define PAC_KEY_LIFETIME (7 * 24 * 60 * 60)
40 * PAC-Key refresh time in seconds (soft limit on remaining hard limit). The
41 * server will generate a new PAC-Key when this number of seconds (or fewer)
44 #define PAC_KEY_REFRESH_TIME (1 * 24 * 60 * 60)
47 struct eap_fast_data {
48 struct eap_ssl_data ssl;
50 START, PHASE1, PHASE2_START, PHASE2_ID, PHASE2_METHOD,
51 CRYPTO_BINDING, REQUEST_PAC, SUCCESS, FAILURE
55 const struct eap_method *phase2_method;
60 u8 crypto_binding_nonce[32];
63 struct eap_fast_key_block_provisioning *key_block_p;
65 u8 simck[EAP_FAST_SIMCK_LEN];
69 u8 pac_opaque_encr[16];
72 int anon_provisioning;
73 int send_new_pac; /* server triggered re-keying of Tunnel PAC */
74 struct wpabuf *pending_phase2_resp;
75 u8 *identity; /* from PAC-Opaque */
80 static const char * eap_fast_state_txt(int state)
88 return "PHASE2_START";
92 return "PHASE2_METHOD";
94 return "CRYPTO_BINDING";
107 static void eap_fast_state(struct eap_fast_data *data, int state)
109 wpa_printf(MSG_DEBUG, "EAP-FAST: %s -> %s",
110 eap_fast_state_txt(data->state),
111 eap_fast_state_txt(state));
116 static EapType eap_fast_req_failure(struct eap_sm *sm,
117 struct eap_fast_data *data)
119 /* TODO: send Result TLV(FAILURE) */
120 eap_fast_state(data, FAILURE);
121 return EAP_TYPE_NONE;
125 static int eap_fast_session_ticket_cb(void *ctx, const u8 *ticket, size_t len,
126 const u8 *client_random,
127 const u8 *server_random,
130 struct eap_fast_data *data = ctx;
131 const u8 *pac_opaque;
132 size_t pac_opaque_len;
133 u8 *buf, *pos, *end, *pac_key = NULL;
134 os_time_t lifetime = 0;
137 size_t identity_len = 0;
139 wpa_printf(MSG_DEBUG, "EAP-FAST: SessionTicket callback");
140 wpa_hexdump(MSG_DEBUG, "EAP-FAST: SessionTicket (PAC-Opaque)",
143 if (len < 4 || WPA_GET_BE16(ticket) != PAC_TYPE_PAC_OPAQUE) {
144 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignore invalid "
149 pac_opaque_len = WPA_GET_BE16(ticket + 2);
150 pac_opaque = ticket + 4;
151 if (pac_opaque_len < 8 || pac_opaque_len % 8 ||
152 pac_opaque_len > len - 4) {
153 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignore invalid PAC-Opaque "
154 "(len=%lu left=%lu)",
155 (unsigned long) pac_opaque_len,
156 (unsigned long) len);
159 wpa_hexdump(MSG_DEBUG, "EAP-FAST: Received PAC-Opaque",
160 pac_opaque, pac_opaque_len);
162 buf = os_malloc(pac_opaque_len - 8);
164 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to allocate memory "
165 "for decrypting PAC-Opaque");
169 if (aes_unwrap(data->pac_opaque_encr, (pac_opaque_len - 8) / 8,
170 pac_opaque, buf) < 0) {
171 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to decrypt "
175 * This may have been caused by server changing the PAC-Opaque
176 * encryption key, so just ignore this PAC-Opaque instead of
177 * failing the authentication completely. Provisioning can now
178 * be used to provision a new PAC.
183 end = buf + pac_opaque_len - 8;
184 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: Decrypted PAC-Opaque",
188 while (pos + 1 < end) {
189 if (pos + 2 + pos[1] > end)
193 case PAC_OPAQUE_TYPE_PAD:
196 case PAC_OPAQUE_TYPE_KEY:
197 if (pos[1] != EAP_FAST_PAC_KEY_LEN) {
198 wpa_printf(MSG_DEBUG, "EAP-FAST: Invalid "
199 "PAC-Key length %d", pos[1]);
204 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: PAC-Key from "
205 "decrypted PAC-Opaque",
206 pac_key, EAP_FAST_PAC_KEY_LEN);
208 case PAC_OPAQUE_TYPE_LIFETIME:
210 wpa_printf(MSG_DEBUG, "EAP-FAST: Invalid "
211 "PAC-Key lifetime length %d",
216 lifetime = WPA_GET_BE32(pos + 2);
218 case PAC_OPAQUE_TYPE_IDENTITY:
220 identity_len = pos[1];
227 if (pac_key == NULL) {
228 wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC-Key included in "
235 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: Identity from "
236 "PAC-Opaque", identity, identity_len);
237 os_free(data->identity);
238 data->identity = os_malloc(identity_len);
239 if (data->identity) {
240 os_memcpy(data->identity, identity, identity_len);
241 data->identity_len = identity_len;
245 if (os_get_time(&now) < 0 || lifetime <= 0 || now.sec > lifetime) {
246 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Key not valid anymore "
247 "(lifetime=%ld now=%ld)", lifetime, now.sec);
252 if (lifetime - now.sec < PAC_KEY_REFRESH_TIME)
253 data->send_new_pac = 1;
255 eap_fast_derive_master_secret(pac_key, server_random, client_random,
264 static void eap_fast_derive_key_auth(struct eap_sm *sm,
265 struct eap_fast_data *data)
269 /* RFC 4851, Section 5.1:
270 * Extra key material after TLS key_block: session_key_seed[40]
273 sks = eap_fast_derive_key(sm->ssl_ctx, data->ssl.conn, "key expansion",
276 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive "
282 * RFC 4851, Section 5.2:
283 * S-IMCK[0] = session_key_seed
285 wpa_hexdump_key(MSG_DEBUG,
286 "EAP-FAST: session_key_seed (SKS = S-IMCK[0])",
287 sks, EAP_FAST_SKS_LEN);
289 os_memcpy(data->simck, sks, EAP_FAST_SIMCK_LEN);
294 static void eap_fast_derive_key_provisioning(struct eap_sm *sm,
295 struct eap_fast_data *data)
297 os_free(data->key_block_p);
298 data->key_block_p = (struct eap_fast_key_block_provisioning *)
299 eap_fast_derive_key(sm->ssl_ctx, data->ssl.conn,
301 sizeof(*data->key_block_p));
302 if (data->key_block_p == NULL) {
303 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive key block");
307 * RFC 4851, Section 5.2:
308 * S-IMCK[0] = session_key_seed
310 wpa_hexdump_key(MSG_DEBUG,
311 "EAP-FAST: session_key_seed (SKS = S-IMCK[0])",
312 data->key_block_p->session_key_seed,
313 sizeof(data->key_block_p->session_key_seed));
315 os_memcpy(data->simck, data->key_block_p->session_key_seed,
317 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: server_challenge",
318 data->key_block_p->server_challenge,
319 sizeof(data->key_block_p->server_challenge));
320 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: client_challenge",
321 data->key_block_p->client_challenge,
322 sizeof(data->key_block_p->client_challenge));
326 static int eap_fast_get_phase2_key(struct eap_sm *sm,
327 struct eap_fast_data *data,
328 u8 *isk, size_t isk_len)
333 os_memset(isk, 0, isk_len);
335 if (data->phase2_method == NULL || data->phase2_priv == NULL) {
336 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 method not "
341 if (data->phase2_method->getKey == NULL)
344 if ((key = data->phase2_method->getKey(sm, data->phase2_priv,
345 &key_len)) == NULL) {
346 wpa_printf(MSG_DEBUG, "EAP-FAST: Could not get key material "
351 if (key_len > isk_len)
353 os_memcpy(isk, key, key_len);
360 static int eap_fast_update_icmk(struct eap_sm *sm, struct eap_fast_data *data)
362 u8 isk[32], imck[60];
364 wpa_printf(MSG_DEBUG, "EAP-FAST: Deriving ICMK[%d] (S-IMCK and CMK)",
365 data->simck_idx + 1);
368 * RFC 4851, Section 5.2:
369 * IMCK[j] = T-PRF(S-IMCK[j-1], "Inner Methods Compound Keys",
371 * S-IMCK[j] = first 40 octets of IMCK[j]
372 * CMK[j] = last 20 octets of IMCK[j]
375 if (eap_fast_get_phase2_key(sm, data, isk, sizeof(isk)) < 0)
377 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: ISK[j]", isk, sizeof(isk));
378 sha1_t_prf(data->simck, EAP_FAST_SIMCK_LEN,
379 "Inner Methods Compound Keys",
380 isk, sizeof(isk), imck, sizeof(imck));
382 os_memcpy(data->simck, imck, EAP_FAST_SIMCK_LEN);
383 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: S-IMCK[j]",
384 data->simck, EAP_FAST_SIMCK_LEN);
385 os_memcpy(data->cmk, imck + EAP_FAST_SIMCK_LEN, 20);
386 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: CMK[j]", data->cmk, 20);
392 static void * eap_fast_init(struct eap_sm *sm)
394 struct eap_fast_data *data;
396 TLS_CIPHER_ANON_DH_AES128_SHA,
397 TLS_CIPHER_AES128_SHA,
398 TLS_CIPHER_RSA_DHE_AES128_SHA,
403 data = os_zalloc(sizeof(*data));
406 data->fast_version = EAP_FAST_VERSION;
407 data->force_version = -1;
408 if (sm->user && sm->user->force_version >= 0) {
409 data->force_version = sm->user->force_version;
410 wpa_printf(MSG_DEBUG, "EAP-FAST: forcing version %d",
411 data->force_version);
412 data->fast_version = data->force_version;
416 if (eap_server_tls_ssl_init(sm, &data->ssl, 0)) {
417 wpa_printf(MSG_INFO, "EAP-FAST: Failed to initialize SSL.");
418 eap_fast_reset(sm, data);
422 if (tls_connection_set_cipher_list(sm->ssl_ctx, data->ssl.conn,
424 wpa_printf(MSG_INFO, "EAP-FAST: Failed to set TLS cipher "
426 eap_fast_reset(sm, data);
430 if (tls_connection_set_session_ticket_cb(sm->ssl_ctx, data->ssl.conn,
431 eap_fast_session_ticket_cb,
433 wpa_printf(MSG_INFO, "EAP-FAST: Failed to set SessionTicket "
435 eap_fast_reset(sm, data);
439 if (sm->pac_opaque_encr_key == NULL) {
440 wpa_printf(MSG_INFO, "EAP-FAST: No PAC-Opaque encryption key "
442 eap_fast_reset(sm, data);
445 os_memcpy(data->pac_opaque_encr, sm->pac_opaque_encr_key,
446 sizeof(data->pac_opaque_encr));
448 if (sm->eap_fast_a_id == NULL) {
449 wpa_printf(MSG_INFO, "EAP-FAST: No A-ID configured");
450 eap_fast_reset(sm, data);
453 data->srv_id = os_strdup(sm->eap_fast_a_id);
454 if (data->srv_id == NULL) {
455 eap_fast_reset(sm, data);
463 static void eap_fast_reset(struct eap_sm *sm, void *priv)
465 struct eap_fast_data *data = priv;
468 if (data->phase2_priv && data->phase2_method)
469 data->phase2_method->reset(sm, data->phase2_priv);
470 eap_server_tls_ssl_deinit(sm, &data->ssl);
471 os_free(data->srv_id);
472 os_free(data->key_block_p);
473 wpabuf_free(data->pending_phase2_resp);
474 os_free(data->identity);
479 static struct wpabuf * eap_fast_build_start(struct eap_sm *sm,
480 struct eap_fast_data *data, u8 id)
483 size_t srv_id_len = os_strlen(data->srv_id);
485 req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_FAST,
486 1 + sizeof(struct pac_tlv_hdr) + srv_id_len,
487 EAP_CODE_REQUEST, id);
489 wpa_printf(MSG_ERROR, "EAP-FAST: Failed to allocate memory for"
491 eap_fast_state(data, FAILURE);
495 wpabuf_put_u8(req, EAP_TLS_FLAGS_START | data->fast_version);
497 /* RFC 4851, 4.1.1. Authority ID Data */
498 eap_fast_put_tlv(req, PAC_TYPE_A_ID, data->srv_id, srv_id_len);
500 eap_fast_state(data, PHASE1);
506 static int eap_fast_phase1_done(struct eap_sm *sm, struct eap_fast_data *data)
510 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase1 done, starting Phase2");
512 if (tls_get_cipher(sm->ssl_ctx, data->ssl.conn, cipher, sizeof(cipher))
514 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to get cipher "
516 eap_fast_state(data, FAILURE);
519 data->anon_provisioning = os_strstr(cipher, "ADH") != NULL;
521 if (data->anon_provisioning) {
522 wpa_printf(MSG_DEBUG, "EAP-FAST: Anonymous provisioning");
523 eap_fast_derive_key_provisioning(sm, data);
525 eap_fast_derive_key_auth(sm, data);
527 eap_fast_state(data, PHASE2_START);
533 static struct wpabuf * eap_fast_build_req(struct eap_sm *sm,
534 struct eap_fast_data *data, u8 id)
539 res = eap_server_tls_buildReq_helper(sm, &data->ssl, EAP_TYPE_FAST,
540 data->fast_version, id, &req);
542 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
543 if (eap_fast_phase1_done(sm, data) < 0) {
550 return eap_server_tls_build_ack(id, EAP_TYPE_FAST,
556 static struct wpabuf * eap_fast_encrypt(struct eap_sm *sm,
557 struct eap_fast_data *data,
558 u8 id, u8 *plain, size_t plain_len)
563 /* TODO: add support for fragmentation, if needed. This will need to
564 * add TLS Message Length field, if the frame is fragmented. */
565 buf = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_FAST,
566 1 + data->ssl.tls_out_limit,
567 EAP_CODE_REQUEST, id);
571 wpabuf_put_u8(buf, data->fast_version);
573 res = tls_connection_encrypt(sm->ssl_ctx, data->ssl.conn,
574 plain, plain_len, wpabuf_put(buf, 0),
575 data->ssl.tls_out_limit);
577 wpa_printf(MSG_INFO, "EAP-FAST: Failed to encrypt Phase 2 "
583 wpabuf_put(buf, res);
590 static struct wpabuf * eap_fast_build_phase2_req(struct eap_sm *sm,
591 struct eap_fast_data *data,
596 if (data->phase2_priv == NULL) {
597 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 method not "
601 req = data->phase2_method->buildReq(sm, data->phase2_priv, id);
605 wpa_hexdump_buf_key(MSG_MSGDUMP, "EAP-FAST: Phase 2 EAP-Request", req);
606 return eap_fast_tlv_eap_payload(req);
610 static struct wpabuf * eap_fast_build_crypto_binding(
611 struct eap_sm *sm, struct eap_fast_data *data)
614 struct eap_tlv_result_tlv *result;
615 struct eap_tlv_crypto_binding__tlv *binding;
618 buf = wpabuf_alloc(sizeof(*result) + sizeof(*binding));
622 if (data->send_new_pac || data->anon_provisioning) {
623 type = EAP_TLV_INTERMEDIATE_RESULT_TLV;
624 data->final_result = 0;
626 type = EAP_TLV_RESULT_TLV;
627 data->final_result = 1;
631 wpa_printf(MSG_DEBUG, "EAP-FAST: Add Result TLV (status=SUCCESS)");
632 result = wpabuf_put(buf, sizeof(*result));
633 result->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY | type);
634 result->length = host_to_be16(2);
635 result->status = host_to_be16(EAP_TLV_RESULT_SUCCESS);
637 /* Crypto-Binding TLV */
638 binding = wpabuf_put(buf, sizeof(*binding));
639 binding->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
640 EAP_TLV_CRYPTO_BINDING_TLV);
641 binding->length = host_to_be16(sizeof(*binding) -
642 sizeof(struct eap_tlv_hdr));
643 binding->version = EAP_FAST_VERSION;
644 binding->received_version = data->peer_version;
645 binding->subtype = EAP_TLV_CRYPTO_BINDING_SUBTYPE_REQUEST;
646 if (os_get_random(binding->nonce, sizeof(binding->nonce)) < 0) {
652 * RFC 4851, Section 4.2.8:
653 * The nonce in a request MUST have its least significant bit set to 0.
655 binding->nonce[sizeof(binding->nonce) - 1] &= ~0x01;
657 os_memcpy(data->crypto_binding_nonce, binding->nonce,
658 sizeof(binding->nonce));
661 * RFC 4851, Section 5.3:
663 * Compound-MAC = HMAC-SHA1( CMK, Crypto-Binding TLV )
666 hmac_sha1(data->cmk, 20, (u8 *) binding, sizeof(*binding),
667 binding->compound_mac);
669 wpa_printf(MSG_DEBUG, "EAP-FAST: Add Crypto-Binding TLV: Version %d "
670 "Received Version %d SubType %d",
671 binding->version, binding->received_version,
673 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE",
674 binding->nonce, sizeof(binding->nonce));
675 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC",
676 binding->compound_mac, sizeof(binding->compound_mac));
682 static struct wpabuf * eap_fast_build_pac(struct eap_sm *sm,
683 struct eap_fast_data *data)
685 u8 pac_key[EAP_FAST_PAC_KEY_LEN];
686 u8 *pac_buf, *pac_opaque;
689 size_t buf_len, srv_id_len, pac_len;
690 struct eap_tlv_hdr *pac_tlv;
691 struct pac_tlv_hdr *pac_info;
692 struct eap_tlv_result_tlv *result;
695 if (os_get_random(pac_key, EAP_FAST_PAC_KEY_LEN) < 0 ||
696 os_get_time(&now) < 0)
698 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: Generated PAC-Key",
699 pac_key, EAP_FAST_PAC_KEY_LEN);
701 pac_len = (2 + EAP_FAST_PAC_KEY_LEN) + (2 + 4) +
702 (2 + sm->identity_len) + 8;
703 pac_buf = os_malloc(pac_len);
707 srv_id_len = os_strlen(data->srv_id);
710 *pos++ = PAC_OPAQUE_TYPE_KEY;
711 *pos++ = EAP_FAST_PAC_KEY_LEN;
712 os_memcpy(pos, pac_key, EAP_FAST_PAC_KEY_LEN);
713 pos += EAP_FAST_PAC_KEY_LEN;
715 *pos++ = PAC_OPAQUE_TYPE_LIFETIME;
717 WPA_PUT_BE32(pos, now.sec + PAC_KEY_LIFETIME);
721 *pos++ = PAC_OPAQUE_TYPE_IDENTITY;
722 *pos++ = sm->identity_len;
723 os_memcpy(pos, sm->identity, sm->identity_len);
724 pos += sm->identity_len;
727 pac_len = pos - pac_buf;
729 *pos++ = PAC_OPAQUE_TYPE_PAD;
733 pac_opaque = os_malloc(pac_len + 8);
734 if (pac_opaque == NULL) {
738 if (aes_wrap(data->pac_opaque_encr, pac_len / 8, pac_buf,
747 wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Opaque",
748 pac_opaque, pac_len);
750 buf_len = sizeof(*pac_tlv) +
751 sizeof(struct pac_tlv_hdr) + EAP_FAST_PAC_KEY_LEN +
752 sizeof(struct pac_tlv_hdr) + pac_len +
753 2 * srv_id_len + 100 + sizeof(*result);
754 buf = wpabuf_alloc(buf_len);
761 wpa_printf(MSG_DEBUG, "EAP-FAST: Add PAC TLV");
762 pac_tlv = wpabuf_put(buf, sizeof(*pac_tlv));
763 pac_tlv->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
767 eap_fast_put_tlv(buf, PAC_TYPE_PAC_KEY, pac_key, EAP_FAST_PAC_KEY_LEN);
770 eap_fast_put_tlv(buf, PAC_TYPE_PAC_OPAQUE, pac_opaque, pac_len);
774 pac_info = wpabuf_put(buf, sizeof(*pac_info));
775 pac_info->type = host_to_be16(PAC_TYPE_PAC_INFO);
777 /* PAC-Lifetime (inside PAC-Info) */
778 eap_fast_put_tlv_hdr(buf, PAC_TYPE_CRED_LIFETIME, 4);
779 wpabuf_put_be32(buf, now.sec + PAC_KEY_LIFETIME);
781 /* A-ID (inside PAC-Info) */
782 eap_fast_put_tlv(buf, PAC_TYPE_A_ID, data->srv_id, srv_id_len);
784 /* Note: headers may be misaligned after A-ID */
786 /* A-ID-Info (inside PAC-Info) */
787 eap_fast_put_tlv(buf, PAC_TYPE_A_ID_INFO, data->srv_id, srv_id_len);
789 /* PAC-Type (inside PAC-Info) */
790 eap_fast_put_tlv_hdr(buf, PAC_TYPE_PAC_TYPE, 2);
791 wpabuf_put_be16(buf, PAC_TYPE_TUNNEL_PAC);
793 /* Update PAC-Info and PAC TLV Length fields */
794 pos = wpabuf_put(buf, 0);
795 pac_info->len = host_to_be16(pos - (u8 *) (pac_info + 1));
796 pac_tlv->length = host_to_be16(pos - (u8 *) (pac_tlv + 1));
799 wpa_printf(MSG_DEBUG, "EAP-FAST: Add Result TLV (status=SUCCESS)");
800 result = wpabuf_put(buf, sizeof(*result));
801 WPA_PUT_BE16((u8 *) &result->tlv_type,
802 EAP_TLV_TYPE_MANDATORY | EAP_TLV_RESULT_TLV);
803 WPA_PUT_BE16((u8 *) &result->length, 2);
804 WPA_PUT_BE16((u8 *) &result->status, EAP_TLV_RESULT_SUCCESS);
810 static struct wpabuf * eap_fast_buildReq(struct eap_sm *sm, void *priv, u8 id)
812 struct eap_fast_data *data = priv;
816 if (data->state == START)
817 return eap_fast_build_start(sm, data, id);
819 if (data->state == PHASE1)
820 return eap_fast_build_req(sm, data, id);
822 switch (data->state) {
825 req = eap_fast_build_phase2_req(sm, data, id);
828 req = eap_fast_build_crypto_binding(sm, data);
831 req = eap_fast_build_pac(sm, data);
834 wpa_printf(MSG_DEBUG, "EAP-FAST: %s - unexpected state %d",
835 __func__, data->state);
842 wpa_hexdump_buf_key(MSG_DEBUG, "EAP-FAST: Encrypting Phase 2 TLVs",
844 encr = eap_fast_encrypt(sm, data, id, wpabuf_mhead(req),
852 static Boolean eap_fast_check(struct eap_sm *sm, void *priv,
853 struct wpabuf *respData)
858 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_FAST, respData, &len);
859 if (pos == NULL || len < 1) {
860 wpa_printf(MSG_INFO, "EAP-FAST: Invalid frame");
868 static int eap_fast_phase2_init(struct eap_sm *sm, struct eap_fast_data *data,
871 if (data->phase2_priv && data->phase2_method) {
872 data->phase2_method->reset(sm, data->phase2_priv);
873 data->phase2_method = NULL;
874 data->phase2_priv = NULL;
876 data->phase2_method = eap_server_get_eap_method(EAP_VENDOR_IETF,
878 if (!data->phase2_method)
881 if (data->key_block_p) {
882 sm->auth_challenge = data->key_block_p->server_challenge;
883 sm->peer_challenge = data->key_block_p->client_challenge;
886 data->phase2_priv = data->phase2_method->init(sm);
888 sm->auth_challenge = NULL;
889 sm->peer_challenge = NULL;
891 return data->phase2_priv == NULL ? -1 : 0;
895 static void eap_fast_process_phase2_response(struct eap_sm *sm,
896 struct eap_fast_data *data,
897 u8 *in_data, size_t in_len)
899 u8 next_type = EAP_TYPE_NONE;
904 const struct eap_method *m = data->phase2_method;
905 void *priv = data->phase2_priv;
908 wpa_printf(MSG_DEBUG, "EAP-FAST: %s - Phase2 not "
909 "initialized?!", __func__);
913 hdr = (struct eap_hdr *) in_data;
914 pos = (u8 *) (hdr + 1);
916 if (in_len > sizeof(*hdr) && *pos == EAP_TYPE_NAK) {
917 left = in_len - sizeof(*hdr);
918 wpa_hexdump(MSG_DEBUG, "EAP-FAST: Phase2 type Nak'ed; "
919 "allowed types", pos + 1, left - 1);
920 eap_sm_process_nak(sm, pos + 1, left - 1);
921 if (sm->user && sm->user_eap_method_index < EAP_MAX_METHODS &&
922 sm->user->methods[sm->user_eap_method_index].method !=
924 next_type = sm->user->methods[
925 sm->user_eap_method_index++].method;
926 wpa_printf(MSG_DEBUG, "EAP-FAST: try EAP type %d",
929 next_type = eap_fast_req_failure(sm, data);
931 eap_fast_phase2_init(sm, data, next_type);
935 wpabuf_set(&buf, in_data, in_len);
937 if (m->check(sm, priv, &buf)) {
938 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase2 check() asked to "
939 "ignore the packet");
940 next_type = eap_fast_req_failure(sm, data);
944 m->process(sm, priv, &buf);
946 if (!m->isDone(sm, priv))
949 if (!m->isSuccess(sm, priv)) {
950 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase2 method failed");
951 next_type = eap_fast_req_failure(sm, data);
952 eap_fast_phase2_init(sm, data, next_type);
956 switch (data->state) {
958 if (eap_user_get(sm, sm->identity, sm->identity_len, 1) != 0) {
959 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: Phase2 "
960 "Identity not found in the user "
962 sm->identity, sm->identity_len);
963 next_type = eap_fast_req_failure(sm, data);
967 eap_fast_state(data, PHASE2_METHOD);
968 if (data->anon_provisioning) {
970 * Only EAP-MSCHAPv2 is allowed for anonymous
973 next_type = EAP_TYPE_MSCHAPV2;
974 sm->user_eap_method_index = 0;
976 next_type = sm->user->methods[0].method;
977 sm->user_eap_method_index = 1;
979 wpa_printf(MSG_DEBUG, "EAP-FAST: try EAP type %d", next_type);
982 eap_fast_update_icmk(sm, data);
983 eap_fast_state(data, CRYPTO_BINDING);
984 next_type = EAP_TYPE_NONE;
989 wpa_printf(MSG_DEBUG, "EAP-FAST: %s - unexpected state %d",
990 __func__, data->state);
994 eap_fast_phase2_init(sm, data, next_type);
998 static void eap_fast_process_phase2_eap(struct eap_sm *sm,
999 struct eap_fast_data *data,
1000 u8 *in_data, size_t in_len)
1002 struct eap_hdr *hdr;
1005 hdr = (struct eap_hdr *) in_data;
1006 if (in_len < (int) sizeof(*hdr)) {
1007 wpa_printf(MSG_INFO, "EAP-FAST: Too short Phase 2 "
1008 "EAP frame (len=%d)", in_len);
1009 eap_fast_req_failure(sm, data);
1012 len = be_to_host16(hdr->length);
1014 wpa_printf(MSG_INFO, "EAP-FAST: Length mismatch in "
1015 "Phase 2 EAP frame (len=%d hdr->length=%d)",
1017 eap_fast_req_failure(sm, data);
1020 wpa_printf(MSG_DEBUG, "EAP-FAST: Received Phase 2: code=%d "
1021 "identifier=%d length=%d", hdr->code, hdr->identifier, len);
1022 switch (hdr->code) {
1023 case EAP_CODE_RESPONSE:
1024 eap_fast_process_phase2_response(sm, data, (u8 *) hdr, len);
1027 wpa_printf(MSG_INFO, "EAP-FAST: Unexpected code=%d in "
1028 "Phase 2 EAP header", hdr->code);
1034 static int eap_fast_parse_tlvs(u8 *data, size_t data_len,
1035 struct eap_fast_tlv_parse *tlv)
1037 int mandatory, tlv_type, len, res;
1040 os_memset(tlv, 0, sizeof(*tlv));
1043 end = data + data_len;
1044 while (pos + 4 < end) {
1045 mandatory = pos[0] & 0x80;
1046 tlv_type = WPA_GET_BE16(pos) & 0x3fff;
1048 len = WPA_GET_BE16(pos);
1050 if (pos + len > end) {
1051 wpa_printf(MSG_INFO, "EAP-FAST: TLV overflow");
1054 wpa_printf(MSG_DEBUG, "EAP-FAST: Received Phase 2: "
1055 "TLV type %d length %d%s",
1056 tlv_type, len, mandatory ? " (mandatory)" : "");
1058 res = eap_fast_parse_tlv(tlv, tlv_type, pos, len);
1063 wpa_printf(MSG_DEBUG, "EAP-FAST: Nak unknown "
1064 "mandatory TLV type %d", tlv_type);
1065 /* TODO: generate Nak TLV */
1068 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignored "
1069 "unknown optional TLV type %d",
1081 static int eap_fast_validate_crypto_binding(
1082 struct eap_fast_data *data, struct eap_tlv_crypto_binding__tlv *b,
1087 wpa_printf(MSG_DEBUG, "EAP-FAST: Reply Crypto-Binding TLV: "
1088 "Version %d Received Version %d SubType %d",
1089 b->version, b->received_version, b->subtype);
1090 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE",
1091 b->nonce, sizeof(b->nonce));
1092 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC",
1093 b->compound_mac, sizeof(b->compound_mac));
1095 if (b->version != EAP_FAST_VERSION ||
1096 b->received_version != EAP_FAST_VERSION) {
1097 wpa_printf(MSG_DEBUG, "EAP-FAST: Unexpected version "
1098 "in Crypto-Binding: version %d "
1099 "received_version %d", b->version,
1100 b->received_version);
1104 if (b->subtype != EAP_TLV_CRYPTO_BINDING_SUBTYPE_RESPONSE) {
1105 wpa_printf(MSG_DEBUG, "EAP-FAST: Unexpected subtype in "
1106 "Crypto-Binding: %d", b->subtype);
1110 if (os_memcmp(data->crypto_binding_nonce, b->nonce, 31) != 0 ||
1111 (data->crypto_binding_nonce[31] | 1) != b->nonce[31]) {
1112 wpa_printf(MSG_DEBUG, "EAP-FAST: Invalid nonce in "
1117 os_memcpy(cmac, b->compound_mac, sizeof(cmac));
1118 os_memset(b->compound_mac, 0, sizeof(cmac));
1119 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Crypto-Binding TLV for "
1120 "Compound MAC calculation",
1121 (u8 *) b, bind_len);
1122 hmac_sha1(data->cmk, 20, (u8 *) b, bind_len, b->compound_mac);
1123 if (os_memcmp(cmac, b->compound_mac, sizeof(cmac)) != 0) {
1124 wpa_hexdump(MSG_MSGDUMP,
1125 "EAP-FAST: Calculated Compound MAC",
1126 b->compound_mac, sizeof(cmac));
1127 wpa_printf(MSG_INFO, "EAP-FAST: Compound MAC did not "
1136 static int eap_fast_pac_type(u8 *pac, size_t len, u16 type)
1138 struct eap_tlv_pac_type_tlv *tlv;
1140 if (pac == NULL || len != sizeof(*tlv))
1143 tlv = (struct eap_tlv_pac_type_tlv *) pac;
1145 return be_to_host16(tlv->tlv_type) == PAC_TYPE_PAC_TYPE &&
1146 be_to_host16(tlv->length) == 2 &&
1147 be_to_host16(tlv->pac_type) == type;
1151 static void eap_fast_process_phase2_tlvs(struct eap_sm *sm,
1152 struct eap_fast_data *data,
1153 u8 *in_data, size_t in_len)
1155 struct eap_fast_tlv_parse tlv;
1156 int check_crypto_binding = data->state == CRYPTO_BINDING;
1158 if (eap_fast_parse_tlvs(in_data, in_len, &tlv) < 0) {
1159 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to parse received "
1164 if (tlv.result == EAP_TLV_RESULT_FAILURE) {
1165 wpa_printf(MSG_DEBUG, "EAP-FAST: Result TLV indicated "
1167 eap_fast_state(data, FAILURE);
1171 if (data->state == REQUEST_PAC) {
1173 if (tlv.pac == NULL || tlv.pac_len < 6) {
1174 wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC "
1175 "Acknowledgement received");
1176 eap_fast_state(data, FAILURE);
1180 type = WPA_GET_BE16(tlv.pac);
1181 len = WPA_GET_BE16(tlv.pac + 2);
1182 res = WPA_GET_BE16(tlv.pac + 4);
1184 if (type != PAC_TYPE_PAC_ACKNOWLEDGEMENT || len != 2 ||
1185 res != EAP_TLV_RESULT_SUCCESS) {
1186 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV did not "
1187 "contain acknowledgement");
1188 eap_fast_state(data, FAILURE);
1192 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Acknowledgement received "
1193 "- PAC provisioning succeeded");
1194 eap_fast_state(data, data->anon_provisioning ?
1199 if (tlv.eap_payload_tlv) {
1200 eap_fast_process_phase2_eap(sm, data, tlv.eap_payload_tlv,
1201 tlv.eap_payload_tlv_len);
1204 if (check_crypto_binding) {
1205 if (tlv.crypto_binding == NULL) {
1206 wpa_printf(MSG_DEBUG, "EAP-FAST: No Crypto-Binding "
1208 eap_fast_state(data, FAILURE);
1212 if (data->final_result &&
1213 tlv.result != EAP_TLV_RESULT_SUCCESS) {
1214 wpa_printf(MSG_DEBUG, "EAP-FAST: Crypto-Binding TLV "
1215 "without Success Result");
1216 eap_fast_state(data, FAILURE);
1220 if (!data->final_result &&
1221 tlv.iresult != EAP_TLV_RESULT_SUCCESS) {
1222 wpa_printf(MSG_DEBUG, "EAP-FAST: Crypto-Binding TLV "
1223 "without intermediate Success Result");
1224 eap_fast_state(data, FAILURE);
1228 if (eap_fast_validate_crypto_binding(data, tlv.crypto_binding,
1229 tlv.crypto_binding_len)) {
1230 eap_fast_state(data, FAILURE);
1234 wpa_printf(MSG_DEBUG, "EAP-FAST: Valid Crypto-Binding TLV "
1235 "received - authentication completed successfully");
1237 if (data->anon_provisioning ||
1238 (tlv.request_action == EAP_TLV_ACTION_PROCESS_TLV &&
1239 eap_fast_pac_type(tlv.pac, tlv.pac_len,
1240 PAC_TYPE_TUNNEL_PAC))) {
1241 wpa_printf(MSG_DEBUG, "EAP-FAST: Requested a new "
1243 eap_fast_state(data, REQUEST_PAC);
1244 } else if (data->send_new_pac) {
1245 wpa_printf(MSG_DEBUG, "EAP-FAST: Server triggered "
1246 "re-keying of Tunnel PAC");
1247 eap_fast_state(data, REQUEST_PAC);
1249 eap_fast_state(data, SUCCESS);
1254 static void eap_fast_process_phase2(struct eap_sm *sm,
1255 struct eap_fast_data *data,
1256 const u8 *in_data, size_t in_len)
1259 int len_decrypted, res;
1262 wpa_printf(MSG_DEBUG, "EAP-FAST: Received %lu bytes encrypted data for"
1263 " Phase 2", (unsigned long) in_len);
1265 if (data->pending_phase2_resp) {
1266 wpa_printf(MSG_DEBUG, "EAP-PEAP: Pending Phase 2 response - "
1267 "skip decryption and use old data");
1268 eap_fast_process_phase2_tlvs(
1269 sm, data, wpabuf_mhead(data->pending_phase2_resp),
1270 wpabuf_len(data->pending_phase2_resp));
1271 wpabuf_free(data->pending_phase2_resp);
1272 data->pending_phase2_resp = NULL;
1276 /* FIX: get rid of const -> non-const typecast */
1277 res = eap_server_tls_data_reassemble(sm, &data->ssl, (u8 **) &in_data,
1279 if (res < 0 || res == 1)
1283 if (data->ssl.tls_in_total > buf_len)
1284 buf_len = data->ssl.tls_in_total;
1285 in_decrypted = os_malloc(buf_len);
1286 if (in_decrypted == NULL) {
1287 os_free(data->ssl.tls_in);
1288 data->ssl.tls_in = NULL;
1289 data->ssl.tls_in_len = 0;
1290 wpa_printf(MSG_WARNING, "EAP-FAST: Failed to allocate memory "
1295 len_decrypted = tls_connection_decrypt(sm->ssl_ctx, data->ssl.conn,
1297 in_decrypted, buf_len);
1298 os_free(data->ssl.tls_in);
1299 data->ssl.tls_in = NULL;
1300 data->ssl.tls_in_len = 0;
1301 if (len_decrypted < 0) {
1302 wpa_printf(MSG_INFO, "EAP-FAST: Failed to decrypt Phase 2 "
1304 os_free(in_decrypted);
1305 eap_fast_state(data, FAILURE);
1309 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: Decrypted Phase 2 TLVs",
1310 in_decrypted, len_decrypted);
1312 eap_fast_process_phase2_tlvs(sm, data, in_decrypted, len_decrypted);
1314 if (sm->method_pending == METHOD_PENDING_WAIT) {
1315 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase2 method is in "
1316 "pending wait state - save decrypted response");
1317 wpabuf_free(data->pending_phase2_resp);
1318 data->pending_phase2_resp = wpabuf_alloc_copy(in_decrypted,
1322 os_free(in_decrypted);
1326 static void eap_fast_process(struct eap_sm *sm, void *priv,
1327 struct wpabuf *respData)
1329 struct eap_fast_data *data = priv;
1333 unsigned int tls_msg_len;
1337 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_FAST, respData,
1339 if (pos == NULL || left < 1)
1343 wpa_printf(MSG_DEBUG, "EAP-FAST: Received packet(len=%lu) - "
1344 "Flags 0x%02x", (unsigned long) wpabuf_len(respData),
1346 data->peer_version = peer_version = flags & EAP_PEAP_VERSION_MASK;
1347 if (data->force_version >= 0 && peer_version != data->force_version) {
1348 wpa_printf(MSG_INFO, "EAP-FAST: peer did not select the forced"
1349 " version (forced=%d peer=%d) - reject",
1350 data->force_version, peer_version);
1351 eap_fast_state(data, FAILURE);
1354 if (peer_version < data->fast_version) {
1355 wpa_printf(MSG_DEBUG, "EAP-FAST: peer ver=%d, own ver=%d; "
1357 peer_version, data->fast_version, peer_version);
1358 data->fast_version = peer_version;
1360 if (flags & EAP_TLS_FLAGS_LENGTH_INCLUDED) {
1362 wpa_printf(MSG_INFO, "EAP-FAST: Short frame with TLS "
1364 eap_fast_state(data, FAILURE);
1367 tls_msg_len = WPA_GET_BE32(pos);
1368 wpa_printf(MSG_DEBUG, "EAP-FAST: TLS Message Length: %d",
1370 if (data->ssl.tls_in_left == 0) {
1371 data->ssl.tls_in_total = tls_msg_len;
1372 data->ssl.tls_in_left = tls_msg_len;
1373 os_free(data->ssl.tls_in);
1374 data->ssl.tls_in = NULL;
1375 data->ssl.tls_in_len = 0;
1381 switch (data->state) {
1383 if (eap_server_tls_process_helper(sm, &data->ssl, pos, left) <
1385 wpa_printf(MSG_INFO, "EAP-FAST: TLS processing "
1387 eap_fast_state(data, FAILURE);
1390 if (!tls_connection_established(sm->ssl_ctx, data->ssl.conn) ||
1391 data->ssl.tls_out_len > 0)
1395 * Phase 1 was completed with the received message (e.g., when
1396 * using abbreviated handshake), so Phase 2 can be started
1397 * immediately without having to send through an empty message
1401 if (eap_fast_phase1_done(sm, data) < 0)
1404 /* fall through to PHASE2_START */
1406 if (data->identity) {
1407 os_free(sm->identity);
1408 sm->identity = data->identity;
1409 data->identity = NULL;
1410 sm->identity_len = data->identity_len;
1411 data->identity_len = 0;
1412 if (eap_user_get(sm, sm->identity, sm->identity_len, 1)
1414 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: "
1415 "Phase2 Identity not found "
1416 "in the user database",
1419 next_type = eap_fast_req_failure(sm, data);
1421 wpa_printf(MSG_DEBUG, "EAP-FAST: Identity "
1422 "already known - skip Phase 2 "
1423 "Identity Request");
1424 next_type = sm->user->methods[0].method;
1425 sm->user_eap_method_index = 1;
1428 eap_fast_state(data, PHASE2_METHOD);
1430 eap_fast_state(data, PHASE2_ID);
1431 next_type = EAP_TYPE_IDENTITY;
1434 eap_fast_phase2_init(sm, data, next_type);
1438 case CRYPTO_BINDING:
1440 eap_fast_process_phase2(sm, data, pos, left);
1443 wpa_printf(MSG_DEBUG, "EAP-FAST: Unexpected state %d in %s",
1444 data->state, __func__);
1448 if (tls_connection_get_write_alerts(sm->ssl_ctx, data->ssl.conn) > 1) {
1449 wpa_printf(MSG_INFO, "EAP-FAST: Locally detected fatal error "
1450 "in TLS processing");
1451 eap_fast_state(data, FAILURE);
1456 static Boolean eap_fast_isDone(struct eap_sm *sm, void *priv)
1458 struct eap_fast_data *data = priv;
1459 return data->state == SUCCESS || data->state == FAILURE;
1463 static u8 * eap_fast_getKey(struct eap_sm *sm, void *priv, size_t *len)
1465 struct eap_fast_data *data = priv;
1468 if (data->state != SUCCESS)
1471 eapKeyData = os_malloc(EAP_FAST_KEY_LEN);
1472 if (eapKeyData == NULL)
1475 eap_fast_derive_eap_msk(data->simck, eapKeyData);
1476 *len = EAP_FAST_KEY_LEN;
1482 static u8 * eap_fast_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
1484 struct eap_fast_data *data = priv;
1487 if (data->state != SUCCESS)
1490 eapKeyData = os_malloc(EAP_EMSK_LEN);
1491 if (eapKeyData == NULL)
1494 eap_fast_derive_eap_emsk(data->simck, eapKeyData);
1495 *len = EAP_EMSK_LEN;
1501 static Boolean eap_fast_isSuccess(struct eap_sm *sm, void *priv)
1503 struct eap_fast_data *data = priv;
1504 return data->state == SUCCESS;
1508 int eap_server_fast_register(void)
1510 struct eap_method *eap;
1513 eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
1514 EAP_VENDOR_IETF, EAP_TYPE_FAST, "FAST");
1518 eap->init = eap_fast_init;
1519 eap->reset = eap_fast_reset;
1520 eap->buildReq = eap_fast_buildReq;
1521 eap->check = eap_fast_check;
1522 eap->process = eap_fast_process;
1523 eap->isDone = eap_fast_isDone;
1524 eap->getKey = eap_fast_getKey;
1525 eap->get_emsk = eap_fast_get_emsk;
1526 eap->isSuccess = eap_fast_isSuccess;
1528 ret = eap_server_method_register(eap);
1530 eap_server_method_free(eap);