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 struct eap_fast_data {
37 struct eap_ssl_data ssl;
39 START, PHASE1, PHASE2_START, PHASE2_ID, PHASE2_METHOD,
40 CRYPTO_BINDING, REQUEST_PAC, SUCCESS, FAILURE
44 const struct eap_method *phase2_method;
49 u8 crypto_binding_nonce[32];
52 struct eap_fast_key_block_provisioning *key_block_p;
54 u8 simck[EAP_FAST_SIMCK_LEN];
55 u8 cmk[EAP_FAST_CMK_LEN];
58 u8 pac_opaque_encr[16];
61 int anon_provisioning;
62 int send_new_pac; /* server triggered re-keying of Tunnel PAC */
63 struct wpabuf *pending_phase2_resp;
64 u8 *identity; /* from PAC-Opaque */
70 int pac_key_refresh_time;
74 static const char * eap_fast_state_txt(int state)
82 return "PHASE2_START";
86 return "PHASE2_METHOD";
88 return "CRYPTO_BINDING";
101 static void eap_fast_state(struct eap_fast_data *data, int state)
103 wpa_printf(MSG_DEBUG, "EAP-FAST: %s -> %s",
104 eap_fast_state_txt(data->state),
105 eap_fast_state_txt(state));
110 static EapType eap_fast_req_failure(struct eap_sm *sm,
111 struct eap_fast_data *data)
113 /* TODO: send Result TLV(FAILURE) */
114 eap_fast_state(data, FAILURE);
115 return EAP_TYPE_NONE;
119 static int eap_fast_session_ticket_cb(void *ctx, const u8 *ticket, size_t len,
120 const u8 *client_random,
121 const u8 *server_random,
124 struct eap_fast_data *data = ctx;
125 const u8 *pac_opaque;
126 size_t pac_opaque_len;
127 u8 *buf, *pos, *end, *pac_key = NULL;
128 os_time_t lifetime = 0;
131 size_t identity_len = 0;
133 wpa_printf(MSG_DEBUG, "EAP-FAST: SessionTicket callback");
134 wpa_hexdump(MSG_DEBUG, "EAP-FAST: SessionTicket (PAC-Opaque)",
137 if (len < 4 || WPA_GET_BE16(ticket) != PAC_TYPE_PAC_OPAQUE) {
138 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignore invalid "
143 pac_opaque_len = WPA_GET_BE16(ticket + 2);
144 pac_opaque = ticket + 4;
145 if (pac_opaque_len < 8 || pac_opaque_len % 8 ||
146 pac_opaque_len > len - 4) {
147 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignore invalid PAC-Opaque "
148 "(len=%lu left=%lu)",
149 (unsigned long) pac_opaque_len,
150 (unsigned long) len);
153 wpa_hexdump(MSG_DEBUG, "EAP-FAST: Received PAC-Opaque",
154 pac_opaque, pac_opaque_len);
156 buf = os_malloc(pac_opaque_len - 8);
158 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to allocate memory "
159 "for decrypting PAC-Opaque");
163 if (aes_unwrap(data->pac_opaque_encr, (pac_opaque_len - 8) / 8,
164 pac_opaque, buf) < 0) {
165 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to decrypt "
169 * This may have been caused by server changing the PAC-Opaque
170 * encryption key, so just ignore this PAC-Opaque instead of
171 * failing the authentication completely. Provisioning can now
172 * be used to provision a new PAC.
177 end = buf + pac_opaque_len - 8;
178 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: Decrypted PAC-Opaque",
182 while (pos + 1 < end) {
183 if (pos + 2 + pos[1] > end)
187 case PAC_OPAQUE_TYPE_PAD:
190 case PAC_OPAQUE_TYPE_KEY:
191 if (pos[1] != EAP_FAST_PAC_KEY_LEN) {
192 wpa_printf(MSG_DEBUG, "EAP-FAST: Invalid "
193 "PAC-Key length %d", pos[1]);
198 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: PAC-Key from "
199 "decrypted PAC-Opaque",
200 pac_key, EAP_FAST_PAC_KEY_LEN);
202 case PAC_OPAQUE_TYPE_LIFETIME:
204 wpa_printf(MSG_DEBUG, "EAP-FAST: Invalid "
205 "PAC-Key lifetime length %d",
210 lifetime = WPA_GET_BE32(pos + 2);
212 case PAC_OPAQUE_TYPE_IDENTITY:
214 identity_len = pos[1];
221 if (pac_key == NULL) {
222 wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC-Key included in "
229 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: Identity from "
230 "PAC-Opaque", identity, identity_len);
231 os_free(data->identity);
232 data->identity = os_malloc(identity_len);
233 if (data->identity) {
234 os_memcpy(data->identity, identity, identity_len);
235 data->identity_len = identity_len;
239 if (os_get_time(&now) < 0 || lifetime <= 0 || now.sec > lifetime) {
240 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Key not valid anymore "
241 "(lifetime=%ld now=%ld)", lifetime, now.sec);
246 if (lifetime - now.sec < data->pac_key_refresh_time)
247 data->send_new_pac = 1;
249 eap_fast_derive_master_secret(pac_key, server_random, client_random,
258 static void eap_fast_derive_key_auth(struct eap_sm *sm,
259 struct eap_fast_data *data)
263 /* RFC 4851, Section 5.1:
264 * Extra key material after TLS key_block: session_key_seed[40]
267 sks = eap_fast_derive_key(sm->ssl_ctx, data->ssl.conn, "key expansion",
270 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive "
276 * RFC 4851, Section 5.2:
277 * S-IMCK[0] = session_key_seed
279 wpa_hexdump_key(MSG_DEBUG,
280 "EAP-FAST: session_key_seed (SKS = S-IMCK[0])",
281 sks, EAP_FAST_SKS_LEN);
283 os_memcpy(data->simck, sks, EAP_FAST_SIMCK_LEN);
288 static void eap_fast_derive_key_provisioning(struct eap_sm *sm,
289 struct eap_fast_data *data)
291 os_free(data->key_block_p);
292 data->key_block_p = (struct eap_fast_key_block_provisioning *)
293 eap_fast_derive_key(sm->ssl_ctx, data->ssl.conn,
295 sizeof(*data->key_block_p));
296 if (data->key_block_p == NULL) {
297 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive key block");
301 * RFC 4851, Section 5.2:
302 * S-IMCK[0] = session_key_seed
304 wpa_hexdump_key(MSG_DEBUG,
305 "EAP-FAST: session_key_seed (SKS = S-IMCK[0])",
306 data->key_block_p->session_key_seed,
307 sizeof(data->key_block_p->session_key_seed));
309 os_memcpy(data->simck, data->key_block_p->session_key_seed,
311 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: server_challenge",
312 data->key_block_p->server_challenge,
313 sizeof(data->key_block_p->server_challenge));
314 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: client_challenge",
315 data->key_block_p->client_challenge,
316 sizeof(data->key_block_p->client_challenge));
320 static int eap_fast_get_phase2_key(struct eap_sm *sm,
321 struct eap_fast_data *data,
322 u8 *isk, size_t isk_len)
327 os_memset(isk, 0, isk_len);
329 if (data->phase2_method == NULL || data->phase2_priv == NULL) {
330 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 method not "
335 if (data->phase2_method->getKey == NULL)
338 if ((key = data->phase2_method->getKey(sm, data->phase2_priv,
339 &key_len)) == NULL) {
340 wpa_printf(MSG_DEBUG, "EAP-FAST: Could not get key material "
345 if (key_len > isk_len)
347 os_memcpy(isk, key, key_len);
354 static int eap_fast_update_icmk(struct eap_sm *sm, struct eap_fast_data *data)
356 u8 isk[32], imck[60];
358 wpa_printf(MSG_DEBUG, "EAP-FAST: Deriving ICMK[%d] (S-IMCK and CMK)",
359 data->simck_idx + 1);
362 * RFC 4851, Section 5.2:
363 * IMCK[j] = T-PRF(S-IMCK[j-1], "Inner Methods Compound Keys",
365 * S-IMCK[j] = first 40 octets of IMCK[j]
366 * CMK[j] = last 20 octets of IMCK[j]
369 if (eap_fast_get_phase2_key(sm, data, isk, sizeof(isk)) < 0)
371 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: ISK[j]", isk, sizeof(isk));
372 sha1_t_prf(data->simck, EAP_FAST_SIMCK_LEN,
373 "Inner Methods Compound Keys",
374 isk, sizeof(isk), imck, sizeof(imck));
376 os_memcpy(data->simck, imck, EAP_FAST_SIMCK_LEN);
377 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: S-IMCK[j]",
378 data->simck, EAP_FAST_SIMCK_LEN);
379 os_memcpy(data->cmk, imck + EAP_FAST_SIMCK_LEN, EAP_FAST_CMK_LEN);
380 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: CMK[j]",
381 data->cmk, EAP_FAST_CMK_LEN);
387 static void * eap_fast_init(struct eap_sm *sm)
389 struct eap_fast_data *data;
391 TLS_CIPHER_ANON_DH_AES128_SHA,
392 TLS_CIPHER_AES128_SHA,
393 TLS_CIPHER_RSA_DHE_AES128_SHA,
398 data = os_zalloc(sizeof(*data));
401 data->fast_version = EAP_FAST_VERSION;
402 data->force_version = -1;
403 if (sm->user && sm->user->force_version >= 0) {
404 data->force_version = sm->user->force_version;
405 wpa_printf(MSG_DEBUG, "EAP-FAST: forcing version %d",
406 data->force_version);
407 data->fast_version = data->force_version;
411 if (eap_server_tls_ssl_init(sm, &data->ssl, 0)) {
412 wpa_printf(MSG_INFO, "EAP-FAST: Failed to initialize SSL.");
413 eap_fast_reset(sm, data);
417 if (tls_connection_set_cipher_list(sm->ssl_ctx, data->ssl.conn,
419 wpa_printf(MSG_INFO, "EAP-FAST: Failed to set TLS cipher "
421 eap_fast_reset(sm, data);
425 if (tls_connection_set_session_ticket_cb(sm->ssl_ctx, data->ssl.conn,
426 eap_fast_session_ticket_cb,
428 wpa_printf(MSG_INFO, "EAP-FAST: Failed to set SessionTicket "
430 eap_fast_reset(sm, data);
434 if (sm->pac_opaque_encr_key == NULL) {
435 wpa_printf(MSG_INFO, "EAP-FAST: No PAC-Opaque encryption key "
437 eap_fast_reset(sm, data);
440 os_memcpy(data->pac_opaque_encr, sm->pac_opaque_encr_key,
441 sizeof(data->pac_opaque_encr));
443 if (sm->eap_fast_a_id == NULL) {
444 wpa_printf(MSG_INFO, "EAP-FAST: No A-ID configured");
445 eap_fast_reset(sm, data);
448 data->srv_id = os_strdup(sm->eap_fast_a_id);
449 if (data->srv_id == NULL) {
450 eap_fast_reset(sm, data);
454 /* PAC-Key lifetime in seconds (hard limit) */
455 data->pac_key_lifetime = sm->pac_key_lifetime;
458 * PAC-Key refresh time in seconds (soft limit on remaining hard
459 * limit). The server will generate a new PAC-Key when this number of
460 * seconds (or fewer) of the lifetime remains.
462 data->pac_key_refresh_time = sm->pac_key_refresh_time;
468 static void eap_fast_reset(struct eap_sm *sm, void *priv)
470 struct eap_fast_data *data = priv;
473 if (data->phase2_priv && data->phase2_method)
474 data->phase2_method->reset(sm, data->phase2_priv);
475 eap_server_tls_ssl_deinit(sm, &data->ssl);
476 os_free(data->srv_id);
477 os_free(data->key_block_p);
478 wpabuf_free(data->pending_phase2_resp);
479 os_free(data->identity);
484 static struct wpabuf * eap_fast_build_start(struct eap_sm *sm,
485 struct eap_fast_data *data, u8 id)
488 size_t srv_id_len = os_strlen(data->srv_id);
490 req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_FAST,
491 1 + sizeof(struct pac_tlv_hdr) + srv_id_len,
492 EAP_CODE_REQUEST, id);
494 wpa_printf(MSG_ERROR, "EAP-FAST: Failed to allocate memory for"
496 eap_fast_state(data, FAILURE);
500 wpabuf_put_u8(req, EAP_TLS_FLAGS_START | data->fast_version);
502 /* RFC 4851, 4.1.1. Authority ID Data */
503 eap_fast_put_tlv(req, PAC_TYPE_A_ID, data->srv_id, srv_id_len);
505 eap_fast_state(data, PHASE1);
511 static int eap_fast_phase1_done(struct eap_sm *sm, struct eap_fast_data *data)
515 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase1 done, starting Phase2");
517 if (tls_get_cipher(sm->ssl_ctx, data->ssl.conn, cipher, sizeof(cipher))
519 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to get cipher "
521 eap_fast_state(data, FAILURE);
524 data->anon_provisioning = os_strstr(cipher, "ADH") != NULL;
526 if (data->anon_provisioning) {
527 wpa_printf(MSG_DEBUG, "EAP-FAST: Anonymous provisioning");
528 eap_fast_derive_key_provisioning(sm, data);
530 eap_fast_derive_key_auth(sm, data);
532 eap_fast_state(data, PHASE2_START);
538 static struct wpabuf * eap_fast_build_phase2_req(struct eap_sm *sm,
539 struct eap_fast_data *data,
544 if (data->phase2_priv == NULL) {
545 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 method not "
549 req = data->phase2_method->buildReq(sm, data->phase2_priv, id);
553 wpa_hexdump_buf_key(MSG_MSGDUMP, "EAP-FAST: Phase 2 EAP-Request", req);
554 return eap_fast_tlv_eap_payload(req);
558 static struct wpabuf * eap_fast_build_crypto_binding(
559 struct eap_sm *sm, struct eap_fast_data *data)
562 struct eap_tlv_result_tlv *result;
563 struct eap_tlv_crypto_binding_tlv *binding;
565 buf = wpabuf_alloc(2 * sizeof(*result) + sizeof(*binding));
569 if (data->send_new_pac || data->anon_provisioning ||
571 data->final_result = 0;
573 data->final_result = 1;
575 if (!data->final_result || data->eap_seq > 1) {
576 /* Intermediate-Result */
577 wpa_printf(MSG_DEBUG, "EAP-FAST: Add Intermediate-Result TLV "
579 result = wpabuf_put(buf, sizeof(*result));
580 result->tlv_type = host_to_be16(
581 EAP_TLV_TYPE_MANDATORY |
582 EAP_TLV_INTERMEDIATE_RESULT_TLV);
583 result->length = host_to_be16(2);
584 result->status = host_to_be16(EAP_TLV_RESULT_SUCCESS);
587 if (data->final_result) {
589 wpa_printf(MSG_DEBUG, "EAP-FAST: Add Result TLV "
591 result = wpabuf_put(buf, sizeof(*result));
592 result->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
594 result->length = host_to_be16(2);
595 result->status = host_to_be16(EAP_TLV_RESULT_SUCCESS);
598 /* Crypto-Binding TLV */
599 binding = wpabuf_put(buf, sizeof(*binding));
600 binding->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
601 EAP_TLV_CRYPTO_BINDING_TLV);
602 binding->length = host_to_be16(sizeof(*binding) -
603 sizeof(struct eap_tlv_hdr));
604 binding->version = EAP_FAST_VERSION;
605 binding->received_version = data->peer_version;
606 binding->subtype = EAP_TLV_CRYPTO_BINDING_SUBTYPE_REQUEST;
607 if (os_get_random(binding->nonce, sizeof(binding->nonce)) < 0) {
613 * RFC 4851, Section 4.2.8:
614 * The nonce in a request MUST have its least significant bit set to 0.
616 binding->nonce[sizeof(binding->nonce) - 1] &= ~0x01;
618 os_memcpy(data->crypto_binding_nonce, binding->nonce,
619 sizeof(binding->nonce));
622 * RFC 4851, Section 5.3:
624 * Compound-MAC = HMAC-SHA1( CMK, Crypto-Binding TLV )
627 hmac_sha1(data->cmk, EAP_FAST_CMK_LEN,
628 (u8 *) binding, sizeof(*binding),
629 binding->compound_mac);
631 wpa_printf(MSG_DEBUG, "EAP-FAST: Add Crypto-Binding TLV: Version %d "
632 "Received Version %d SubType %d",
633 binding->version, binding->received_version,
635 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE",
636 binding->nonce, sizeof(binding->nonce));
637 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC",
638 binding->compound_mac, sizeof(binding->compound_mac));
644 static struct wpabuf * eap_fast_build_pac(struct eap_sm *sm,
645 struct eap_fast_data *data)
647 u8 pac_key[EAP_FAST_PAC_KEY_LEN];
648 u8 *pac_buf, *pac_opaque;
651 size_t buf_len, srv_id_len, pac_len;
652 struct eap_tlv_hdr *pac_tlv;
653 struct pac_tlv_hdr *pac_info;
654 struct eap_tlv_result_tlv *result;
657 if (os_get_random(pac_key, EAP_FAST_PAC_KEY_LEN) < 0 ||
658 os_get_time(&now) < 0)
660 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: Generated PAC-Key",
661 pac_key, EAP_FAST_PAC_KEY_LEN);
663 pac_len = (2 + EAP_FAST_PAC_KEY_LEN) + (2 + 4) +
664 (2 + sm->identity_len) + 8;
665 pac_buf = os_malloc(pac_len);
669 srv_id_len = os_strlen(data->srv_id);
672 *pos++ = PAC_OPAQUE_TYPE_KEY;
673 *pos++ = EAP_FAST_PAC_KEY_LEN;
674 os_memcpy(pos, pac_key, EAP_FAST_PAC_KEY_LEN);
675 pos += EAP_FAST_PAC_KEY_LEN;
677 *pos++ = PAC_OPAQUE_TYPE_LIFETIME;
679 WPA_PUT_BE32(pos, now.sec + data->pac_key_lifetime);
683 *pos++ = PAC_OPAQUE_TYPE_IDENTITY;
684 *pos++ = sm->identity_len;
685 os_memcpy(pos, sm->identity, sm->identity_len);
686 pos += sm->identity_len;
689 pac_len = pos - pac_buf;
690 while (pac_len % 8) {
691 *pos++ = PAC_OPAQUE_TYPE_PAD;
695 pac_opaque = os_malloc(pac_len + 8);
696 if (pac_opaque == NULL) {
700 if (aes_wrap(data->pac_opaque_encr, pac_len / 8, pac_buf,
709 wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Opaque",
710 pac_opaque, pac_len);
712 buf_len = sizeof(*pac_tlv) +
713 sizeof(struct pac_tlv_hdr) + EAP_FAST_PAC_KEY_LEN +
714 sizeof(struct pac_tlv_hdr) + pac_len +
715 2 * srv_id_len + 100 + sizeof(*result);
716 buf = wpabuf_alloc(buf_len);
723 wpa_printf(MSG_DEBUG, "EAP-FAST: Add Result TLV (status=SUCCESS)");
724 result = wpabuf_put(buf, sizeof(*result));
725 WPA_PUT_BE16((u8 *) &result->tlv_type,
726 EAP_TLV_TYPE_MANDATORY | EAP_TLV_RESULT_TLV);
727 WPA_PUT_BE16((u8 *) &result->length, 2);
728 WPA_PUT_BE16((u8 *) &result->status, EAP_TLV_RESULT_SUCCESS);
731 wpa_printf(MSG_DEBUG, "EAP-FAST: Add PAC TLV");
732 pac_tlv = wpabuf_put(buf, sizeof(*pac_tlv));
733 pac_tlv->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
737 eap_fast_put_tlv(buf, PAC_TYPE_PAC_KEY, pac_key, EAP_FAST_PAC_KEY_LEN);
740 eap_fast_put_tlv(buf, PAC_TYPE_PAC_OPAQUE, pac_opaque, pac_len);
744 pac_info = wpabuf_put(buf, sizeof(*pac_info));
745 pac_info->type = host_to_be16(PAC_TYPE_PAC_INFO);
747 /* PAC-Lifetime (inside PAC-Info) */
748 eap_fast_put_tlv_hdr(buf, PAC_TYPE_CRED_LIFETIME, 4);
749 wpabuf_put_be32(buf, now.sec + data->pac_key_lifetime);
751 /* A-ID (inside PAC-Info) */
752 eap_fast_put_tlv(buf, PAC_TYPE_A_ID, data->srv_id, srv_id_len);
754 /* Note: headers may be misaligned after A-ID */
756 /* A-ID-Info (inside PAC-Info) */
757 eap_fast_put_tlv(buf, PAC_TYPE_A_ID_INFO, data->srv_id, srv_id_len);
759 /* PAC-Type (inside PAC-Info) */
760 eap_fast_put_tlv_hdr(buf, PAC_TYPE_PAC_TYPE, 2);
761 wpabuf_put_be16(buf, PAC_TYPE_TUNNEL_PAC);
763 /* Update PAC-Info and PAC TLV Length fields */
764 pos = wpabuf_put(buf, 0);
765 pac_info->len = host_to_be16(pos - (u8 *) (pac_info + 1));
766 pac_tlv->length = host_to_be16(pos - (u8 *) (pac_tlv + 1));
772 static struct wpabuf * eap_fast_buildReq(struct eap_sm *sm, void *priv, u8 id)
774 struct eap_fast_data *data = priv;
775 struct wpabuf *req = NULL;
778 if (data->ssl.state == FRAG_ACK) {
779 return eap_server_tls_build_ack(id, EAP_TYPE_FAST,
783 if (data->ssl.state == WAIT_FRAG_ACK) {
784 return eap_server_tls_build_msg(&data->ssl, EAP_TYPE_FAST,
785 data->fast_version, id);
788 switch (data->state) {
790 return eap_fast_build_start(sm, data, id);
792 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
793 if (eap_fast_phase1_done(sm, data) < 0)
799 req = eap_fast_build_phase2_req(sm, data, id);
802 req = eap_fast_build_crypto_binding(sm, data);
803 if (data->phase2_method) {
805 * Include the start of the next EAP method in the
806 * sequence in the same message with Crypto-Binding to
810 eap = eap_fast_build_phase2_req(sm, data, id);
811 req = wpabuf_concat(req, eap);
812 eap_fast_state(data, PHASE2_METHOD);
816 req = eap_fast_build_pac(sm, data);
819 wpa_printf(MSG_DEBUG, "EAP-FAST: %s - unexpected state %d",
820 __func__, data->state);
825 wpa_hexdump_buf_key(MSG_DEBUG, "EAP-FAST: Encrypting Phase 2 "
827 encr = eap_server_tls_encrypt(sm, &data->ssl,
832 wpabuf_free(data->ssl.out_buf);
833 data->ssl.out_used = 0;
834 data->ssl.out_buf = encr;
837 return eap_server_tls_build_msg(&data->ssl, EAP_TYPE_FAST,
838 data->fast_version, id);
842 static Boolean eap_fast_check(struct eap_sm *sm, void *priv,
843 struct wpabuf *respData)
848 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_FAST, respData, &len);
849 if (pos == NULL || len < 1) {
850 wpa_printf(MSG_INFO, "EAP-FAST: Invalid frame");
858 static int eap_fast_phase2_init(struct eap_sm *sm, struct eap_fast_data *data,
861 if (data->phase2_priv && data->phase2_method) {
862 data->phase2_method->reset(sm, data->phase2_priv);
863 data->phase2_method = NULL;
864 data->phase2_priv = NULL;
866 data->phase2_method = eap_server_get_eap_method(EAP_VENDOR_IETF,
868 if (!data->phase2_method)
871 if (data->key_block_p) {
872 sm->auth_challenge = data->key_block_p->server_challenge;
873 sm->peer_challenge = data->key_block_p->client_challenge;
876 data->phase2_priv = data->phase2_method->init(sm);
878 sm->auth_challenge = NULL;
879 sm->peer_challenge = NULL;
881 return data->phase2_priv == NULL ? -1 : 0;
885 static void eap_fast_process_phase2_response(struct eap_sm *sm,
886 struct eap_fast_data *data,
887 u8 *in_data, size_t in_len)
889 u8 next_type = EAP_TYPE_NONE;
894 const struct eap_method *m = data->phase2_method;
895 void *priv = data->phase2_priv;
898 wpa_printf(MSG_DEBUG, "EAP-FAST: %s - Phase2 not "
899 "initialized?!", __func__);
903 hdr = (struct eap_hdr *) in_data;
904 pos = (u8 *) (hdr + 1);
906 if (in_len > sizeof(*hdr) && *pos == EAP_TYPE_NAK) {
907 left = in_len - sizeof(*hdr);
908 wpa_hexdump(MSG_DEBUG, "EAP-FAST: Phase2 type Nak'ed; "
909 "allowed types", pos + 1, left - 1);
911 if (m && m->vendor == EAP_VENDOR_IETF &&
912 m->method == EAP_TYPE_TNC) {
913 wpa_printf(MSG_DEBUG, "EAP-FAST: Peer Nak'ed required "
915 next_type = eap_fast_req_failure(sm, data);
916 eap_fast_phase2_init(sm, data, next_type);
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);
983 eap_fast_update_icmk(sm, data);
984 eap_fast_state(data, CRYPTO_BINDING);
986 next_type = EAP_TYPE_NONE;
988 if (sm->tnc && !data->tnc_started) {
989 wpa_printf(MSG_DEBUG, "EAP-FAST: Initialize TNC");
990 next_type = EAP_TYPE_TNC;
991 data->tnc_started = 1;
998 wpa_printf(MSG_DEBUG, "EAP-FAST: %s - unexpected state %d",
999 __func__, data->state);
1003 eap_fast_phase2_init(sm, data, next_type);
1007 static void eap_fast_process_phase2_eap(struct eap_sm *sm,
1008 struct eap_fast_data *data,
1009 u8 *in_data, size_t in_len)
1011 struct eap_hdr *hdr;
1014 hdr = (struct eap_hdr *) in_data;
1015 if (in_len < (int) sizeof(*hdr)) {
1016 wpa_printf(MSG_INFO, "EAP-FAST: Too short Phase 2 "
1017 "EAP frame (len=%d)", in_len);
1018 eap_fast_req_failure(sm, data);
1021 len = be_to_host16(hdr->length);
1023 wpa_printf(MSG_INFO, "EAP-FAST: Length mismatch in "
1024 "Phase 2 EAP frame (len=%d hdr->length=%d)",
1026 eap_fast_req_failure(sm, data);
1029 wpa_printf(MSG_DEBUG, "EAP-FAST: Received Phase 2: code=%d "
1030 "identifier=%d length=%d", hdr->code, hdr->identifier, len);
1031 switch (hdr->code) {
1032 case EAP_CODE_RESPONSE:
1033 eap_fast_process_phase2_response(sm, data, (u8 *) hdr, len);
1036 wpa_printf(MSG_INFO, "EAP-FAST: Unexpected code=%d in "
1037 "Phase 2 EAP header", hdr->code);
1043 static int eap_fast_parse_tlvs(u8 *data, size_t data_len,
1044 struct eap_fast_tlv_parse *tlv)
1046 int mandatory, tlv_type, len, res;
1049 os_memset(tlv, 0, sizeof(*tlv));
1052 end = data + data_len;
1053 while (pos + 4 < end) {
1054 mandatory = pos[0] & 0x80;
1055 tlv_type = WPA_GET_BE16(pos) & 0x3fff;
1057 len = WPA_GET_BE16(pos);
1059 if (pos + len > end) {
1060 wpa_printf(MSG_INFO, "EAP-FAST: TLV overflow");
1063 wpa_printf(MSG_DEBUG, "EAP-FAST: Received Phase 2: "
1064 "TLV type %d length %d%s",
1065 tlv_type, len, mandatory ? " (mandatory)" : "");
1067 res = eap_fast_parse_tlv(tlv, tlv_type, pos, len);
1072 wpa_printf(MSG_DEBUG, "EAP-FAST: Nak unknown "
1073 "mandatory TLV type %d", tlv_type);
1074 /* TODO: generate Nak TLV */
1077 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignored "
1078 "unknown optional TLV type %d",
1090 static int eap_fast_validate_crypto_binding(
1091 struct eap_fast_data *data, struct eap_tlv_crypto_binding_tlv *b,
1094 u8 cmac[SHA1_MAC_LEN];
1096 wpa_printf(MSG_DEBUG, "EAP-FAST: Reply Crypto-Binding TLV: "
1097 "Version %d Received Version %d SubType %d",
1098 b->version, b->received_version, b->subtype);
1099 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE",
1100 b->nonce, sizeof(b->nonce));
1101 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC",
1102 b->compound_mac, sizeof(b->compound_mac));
1104 if (b->version != EAP_FAST_VERSION ||
1105 b->received_version != EAP_FAST_VERSION) {
1106 wpa_printf(MSG_DEBUG, "EAP-FAST: Unexpected version "
1107 "in Crypto-Binding: version %d "
1108 "received_version %d", b->version,
1109 b->received_version);
1113 if (b->subtype != EAP_TLV_CRYPTO_BINDING_SUBTYPE_RESPONSE) {
1114 wpa_printf(MSG_DEBUG, "EAP-FAST: Unexpected subtype in "
1115 "Crypto-Binding: %d", b->subtype);
1119 if (os_memcmp(data->crypto_binding_nonce, b->nonce, 31) != 0 ||
1120 (data->crypto_binding_nonce[31] | 1) != b->nonce[31]) {
1121 wpa_printf(MSG_DEBUG, "EAP-FAST: Invalid nonce in "
1126 os_memcpy(cmac, b->compound_mac, sizeof(cmac));
1127 os_memset(b->compound_mac, 0, sizeof(cmac));
1128 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Crypto-Binding TLV for "
1129 "Compound MAC calculation",
1130 (u8 *) b, bind_len);
1131 hmac_sha1(data->cmk, EAP_FAST_CMK_LEN, (u8 *) b, bind_len,
1133 if (os_memcmp(cmac, b->compound_mac, sizeof(cmac)) != 0) {
1134 wpa_hexdump(MSG_MSGDUMP,
1135 "EAP-FAST: Calculated Compound MAC",
1136 b->compound_mac, sizeof(cmac));
1137 wpa_printf(MSG_INFO, "EAP-FAST: Compound MAC did not "
1146 static int eap_fast_pac_type(u8 *pac, size_t len, u16 type)
1148 struct eap_tlv_pac_type_tlv *tlv;
1150 if (pac == NULL || len != sizeof(*tlv))
1153 tlv = (struct eap_tlv_pac_type_tlv *) pac;
1155 return be_to_host16(tlv->tlv_type) == PAC_TYPE_PAC_TYPE &&
1156 be_to_host16(tlv->length) == 2 &&
1157 be_to_host16(tlv->pac_type) == type;
1161 static void eap_fast_process_phase2_tlvs(struct eap_sm *sm,
1162 struct eap_fast_data *data,
1163 u8 *in_data, size_t in_len)
1165 struct eap_fast_tlv_parse tlv;
1166 int check_crypto_binding = data->state == CRYPTO_BINDING;
1168 if (eap_fast_parse_tlvs(in_data, in_len, &tlv) < 0) {
1169 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to parse received "
1174 if (tlv.result == EAP_TLV_RESULT_FAILURE) {
1175 wpa_printf(MSG_DEBUG, "EAP-FAST: Result TLV indicated "
1177 eap_fast_state(data, FAILURE);
1181 if (data->state == REQUEST_PAC) {
1183 if (tlv.pac == NULL || tlv.pac_len < 6) {
1184 wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC "
1185 "Acknowledgement received");
1186 eap_fast_state(data, FAILURE);
1190 type = WPA_GET_BE16(tlv.pac);
1191 len = WPA_GET_BE16(tlv.pac + 2);
1192 res = WPA_GET_BE16(tlv.pac + 4);
1194 if (type != PAC_TYPE_PAC_ACKNOWLEDGEMENT || len != 2 ||
1195 res != EAP_TLV_RESULT_SUCCESS) {
1196 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV did not "
1197 "contain acknowledgement");
1198 eap_fast_state(data, FAILURE);
1202 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Acknowledgement received "
1203 "- PAC provisioning succeeded");
1204 eap_fast_state(data, data->anon_provisioning ?
1209 if (check_crypto_binding) {
1210 if (tlv.crypto_binding == NULL) {
1211 wpa_printf(MSG_DEBUG, "EAP-FAST: No Crypto-Binding "
1213 eap_fast_state(data, FAILURE);
1217 if (data->final_result &&
1218 tlv.result != EAP_TLV_RESULT_SUCCESS) {
1219 wpa_printf(MSG_DEBUG, "EAP-FAST: Crypto-Binding TLV "
1220 "without Success Result");
1221 eap_fast_state(data, FAILURE);
1225 if (!data->final_result &&
1226 tlv.iresult != EAP_TLV_RESULT_SUCCESS) {
1227 wpa_printf(MSG_DEBUG, "EAP-FAST: Crypto-Binding TLV "
1228 "without intermediate Success Result");
1229 eap_fast_state(data, FAILURE);
1233 if (eap_fast_validate_crypto_binding(data, tlv.crypto_binding,
1234 tlv.crypto_binding_len)) {
1235 eap_fast_state(data, FAILURE);
1239 wpa_printf(MSG_DEBUG, "EAP-FAST: Valid Crypto-Binding TLV "
1241 if (data->final_result) {
1242 wpa_printf(MSG_DEBUG, "EAP-FAST: Authentication "
1243 "completed successfully");
1246 if (data->anon_provisioning &&
1247 sm->eap_fast_prov != ANON_PROV &&
1248 sm->eap_fast_prov != BOTH_PROV) {
1249 wpa_printf(MSG_DEBUG, "EAP-FAST: Client is trying to "
1250 "use unauthenticated provisioning which is "
1252 eap_fast_state(data, FAILURE);
1256 if (sm->eap_fast_prov != AUTH_PROV &&
1257 sm->eap_fast_prov != BOTH_PROV &&
1258 tlv.request_action == EAP_TLV_ACTION_PROCESS_TLV &&
1259 eap_fast_pac_type(tlv.pac, tlv.pac_len,
1260 PAC_TYPE_TUNNEL_PAC)) {
1261 wpa_printf(MSG_DEBUG, "EAP-FAST: Client is trying to "
1262 "use authenticated provisioning which is "
1264 eap_fast_state(data, FAILURE);
1268 if (data->anon_provisioning ||
1269 (tlv.request_action == EAP_TLV_ACTION_PROCESS_TLV &&
1270 eap_fast_pac_type(tlv.pac, tlv.pac_len,
1271 PAC_TYPE_TUNNEL_PAC))) {
1272 wpa_printf(MSG_DEBUG, "EAP-FAST: Requested a new "
1274 eap_fast_state(data, REQUEST_PAC);
1275 } else if (data->send_new_pac) {
1276 wpa_printf(MSG_DEBUG, "EAP-FAST: Server triggered "
1277 "re-keying of Tunnel PAC");
1278 eap_fast_state(data, REQUEST_PAC);
1279 } else if (data->final_result)
1280 eap_fast_state(data, SUCCESS);
1283 if (tlv.eap_payload_tlv) {
1284 eap_fast_process_phase2_eap(sm, data, tlv.eap_payload_tlv,
1285 tlv.eap_payload_tlv_len);
1290 static void eap_fast_process_phase2(struct eap_sm *sm,
1291 struct eap_fast_data *data,
1292 struct wpabuf *in_buf)
1300 in_data = wpabuf_mhead(in_buf);
1301 in_len = wpabuf_len(in_buf);
1303 wpa_printf(MSG_DEBUG, "EAP-FAST: Received %lu bytes encrypted data for"
1304 " Phase 2", (unsigned long) in_len);
1306 if (data->pending_phase2_resp) {
1307 wpa_printf(MSG_DEBUG, "EAP-PEAP: Pending Phase 2 response - "
1308 "skip decryption and use old data");
1309 eap_fast_process_phase2_tlvs(
1310 sm, data, wpabuf_mhead(data->pending_phase2_resp),
1311 wpabuf_len(data->pending_phase2_resp));
1312 wpabuf_free(data->pending_phase2_resp);
1313 data->pending_phase2_resp = NULL;
1319 * Even though we try to disable TLS compression, it is possible that
1320 * this cannot be done with all TLS libraries. Add extra buffer space
1321 * to handle the possibility of the decrypted data being longer than
1326 in_decrypted = os_malloc(buf_len);
1327 if (in_decrypted == NULL) {
1328 wpa_printf(MSG_WARNING, "EAP-FAST: Failed to allocate memory "
1333 len_decrypted = tls_connection_decrypt(sm->ssl_ctx, data->ssl.conn,
1335 in_decrypted, buf_len);
1336 if (len_decrypted < 0) {
1337 wpa_printf(MSG_INFO, "EAP-FAST: Failed to decrypt Phase 2 "
1339 os_free(in_decrypted);
1340 eap_fast_state(data, FAILURE);
1344 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: Decrypted Phase 2 TLVs",
1345 in_decrypted, len_decrypted);
1347 eap_fast_process_phase2_tlvs(sm, data, in_decrypted, len_decrypted);
1349 if (sm->method_pending == METHOD_PENDING_WAIT) {
1350 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase2 method is in "
1351 "pending wait state - save decrypted response");
1352 wpabuf_free(data->pending_phase2_resp);
1353 data->pending_phase2_resp = wpabuf_alloc_copy(in_decrypted,
1357 os_free(in_decrypted);
1361 static int eap_fast_process_version(struct eap_sm *sm, void *priv,
1364 struct eap_fast_data *data = priv;
1366 data->peer_version = peer_version;
1368 if (data->force_version >= 0 && peer_version != data->force_version) {
1369 wpa_printf(MSG_INFO, "EAP-FAST: peer did not select the forced"
1370 " version (forced=%d peer=%d) - reject",
1371 data->force_version, peer_version);
1375 if (peer_version < data->fast_version) {
1376 wpa_printf(MSG_DEBUG, "EAP-FAST: peer ver=%d, own ver=%d; "
1378 peer_version, data->fast_version, peer_version);
1379 data->fast_version = peer_version;
1386 static int eap_fast_process_phase1(struct eap_sm *sm,
1387 struct eap_fast_data *data)
1389 if (eap_server_tls_phase1(sm, &data->ssl) < 0) {
1390 wpa_printf(MSG_INFO, "EAP-FAST: TLS processing failed");
1391 eap_fast_state(data, FAILURE);
1395 if (!tls_connection_established(sm->ssl_ctx, data->ssl.conn) ||
1396 wpabuf_len(data->ssl.out_buf) > 0)
1400 * Phase 1 was completed with the received message (e.g., when using
1401 * abbreviated handshake), so Phase 2 can be started immediately
1402 * without having to send through an empty message to the peer.
1405 return eap_fast_phase1_done(sm, data);
1409 static void eap_fast_process_phase2_start(struct eap_sm *sm,
1410 struct eap_fast_data *data)
1414 if (data->identity) {
1415 os_free(sm->identity);
1416 sm->identity = data->identity;
1417 data->identity = NULL;
1418 sm->identity_len = data->identity_len;
1419 data->identity_len = 0;
1420 sm->require_identity_match = 1;
1421 if (eap_user_get(sm, sm->identity, sm->identity_len, 1) != 0) {
1422 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: "
1423 "Phase2 Identity not found "
1424 "in the user database",
1425 sm->identity, sm->identity_len);
1426 next_type = eap_fast_req_failure(sm, data);
1428 wpa_printf(MSG_DEBUG, "EAP-FAST: Identity already "
1429 "known - skip Phase 2 Identity Request");
1430 next_type = sm->user->methods[0].method;
1431 sm->user_eap_method_index = 1;
1434 eap_fast_state(data, PHASE2_METHOD);
1436 eap_fast_state(data, PHASE2_ID);
1437 next_type = EAP_TYPE_IDENTITY;
1440 eap_fast_phase2_init(sm, data, next_type);
1444 static void eap_fast_process_msg(struct eap_sm *sm, void *priv,
1445 const struct wpabuf *respData)
1447 struct eap_fast_data *data = priv;
1449 switch (data->state) {
1451 if (eap_fast_process_phase1(sm, data))
1454 /* fall through to PHASE2_START */
1456 eap_fast_process_phase2_start(sm, data);
1460 case CRYPTO_BINDING:
1462 eap_fast_process_phase2(sm, data, data->ssl.in_buf);
1465 wpa_printf(MSG_DEBUG, "EAP-FAST: Unexpected state %d in %s",
1466 data->state, __func__);
1472 static void eap_fast_process(struct eap_sm *sm, void *priv,
1473 struct wpabuf *respData)
1475 struct eap_fast_data *data = priv;
1476 if (eap_server_tls_process(sm, &data->ssl, respData, data,
1477 EAP_TYPE_FAST, eap_fast_process_version,
1478 eap_fast_process_msg) < 0)
1479 eap_fast_state(data, FAILURE);
1483 static Boolean eap_fast_isDone(struct eap_sm *sm, void *priv)
1485 struct eap_fast_data *data = priv;
1486 return data->state == SUCCESS || data->state == FAILURE;
1490 static u8 * eap_fast_getKey(struct eap_sm *sm, void *priv, size_t *len)
1492 struct eap_fast_data *data = priv;
1495 if (data->state != SUCCESS)
1498 eapKeyData = os_malloc(EAP_FAST_KEY_LEN);
1499 if (eapKeyData == NULL)
1502 eap_fast_derive_eap_msk(data->simck, eapKeyData);
1503 *len = EAP_FAST_KEY_LEN;
1509 static u8 * eap_fast_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
1511 struct eap_fast_data *data = priv;
1514 if (data->state != SUCCESS)
1517 eapKeyData = os_malloc(EAP_EMSK_LEN);
1518 if (eapKeyData == NULL)
1521 eap_fast_derive_eap_emsk(data->simck, eapKeyData);
1522 *len = EAP_EMSK_LEN;
1528 static Boolean eap_fast_isSuccess(struct eap_sm *sm, void *priv)
1530 struct eap_fast_data *data = priv;
1531 return data->state == SUCCESS;
1535 int eap_server_fast_register(void)
1537 struct eap_method *eap;
1540 eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
1541 EAP_VENDOR_IETF, EAP_TYPE_FAST, "FAST");
1545 eap->init = eap_fast_init;
1546 eap->reset = eap_fast_reset;
1547 eap->buildReq = eap_fast_buildReq;
1548 eap->check = eap_fast_check;
1549 eap->process = eap_fast_process;
1550 eap->isDone = eap_fast_isDone;
1551 eap->getKey = eap_fast_getKey;
1552 eap->get_emsk = eap_fast_get_emsk;
1553 eap->isSuccess = eap_fast_isSuccess;
1555 ret = eap_server_method_register(eap);
1557 eap_server_method_free(eap);