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 #define TLS_RANDOM_LEN 32
132 #define TLS_MASTER_SECRET_LEN 48
133 u8 seed[2 * TLS_RANDOM_LEN];
134 const u8 *pac_opaque;
135 size_t pac_opaque_len;
136 u8 *buf, *pos, *end, *pac_key = NULL;
137 os_time_t lifetime = 0;
140 size_t identity_len = 0;
142 wpa_printf(MSG_DEBUG, "EAP-FAST: SessionTicket callback");
143 wpa_hexdump(MSG_DEBUG, "EAP-FAST: SessionTicket (PAC-Opaque)",
145 wpa_hexdump(MSG_DEBUG, "EAP-FAST: client_random",
146 client_random, TLS_RANDOM_LEN);
147 wpa_hexdump(MSG_DEBUG, "EAP-FAST: server_random",
148 server_random, TLS_RANDOM_LEN);
150 if (len < 4 || WPA_GET_BE16(ticket) != PAC_TYPE_PAC_OPAQUE) {
151 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignore invalid "
156 pac_opaque_len = WPA_GET_BE16(ticket + 2);
157 pac_opaque = ticket + 4;
158 if (pac_opaque_len < 8 || pac_opaque_len % 8 ||
159 pac_opaque_len > len - 4) {
160 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignore invalid PAC-Opaque "
161 "(len=%lu left=%lu)",
162 (unsigned long) pac_opaque_len,
163 (unsigned long) len);
166 wpa_hexdump(MSG_DEBUG, "EAP-FAST: Received PAC-Opaque",
167 pac_opaque, pac_opaque_len);
169 buf = os_malloc(pac_opaque_len - 8);
171 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to allocate memory "
172 "for decrypting PAC-Opaque");
176 if (aes_unwrap(data->pac_opaque_encr, (pac_opaque_len - 8) / 8,
177 pac_opaque, buf) < 0) {
178 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to decrypt "
182 * This may have been caused by server changing the PAC-Opaque
183 * encryption key, so just ignore this PAC-Opaque instead of
184 * failing the authentication completely. Provisioning can now
185 * be used to provision a new PAC.
190 end = buf + pac_opaque_len - 8;
191 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: Decrypted PAC-Opaque",
195 while (pos + 1 < end) {
196 if (pos + 2 + pos[1] > end)
200 case PAC_OPAQUE_TYPE_PAD:
203 case PAC_OPAQUE_TYPE_KEY:
204 if (pos[1] != EAP_FAST_PAC_KEY_LEN) {
205 wpa_printf(MSG_DEBUG, "EAP-FAST: Invalid "
206 "PAC-Key length %d", pos[1]);
211 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: PAC-Key from "
212 "decrypted PAC-Opaque",
213 pac_key, EAP_FAST_PAC_KEY_LEN);
215 case PAC_OPAQUE_TYPE_LIFETIME:
217 wpa_printf(MSG_DEBUG, "EAP-FAST: Invalid "
218 "PAC-Key lifetime length %d",
223 lifetime = WPA_GET_BE32(pos + 2);
225 case PAC_OPAQUE_TYPE_IDENTITY:
227 identity_len = pos[1];
234 if (pac_key == NULL) {
235 wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC-Key included in "
242 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: Identity from "
243 "PAC-Opaque", identity, identity_len);
244 os_free(data->identity);
245 data->identity = os_malloc(identity_len);
246 if (data->identity) {
247 os_memcpy(data->identity, identity, identity_len);
248 data->identity_len = identity_len;
252 if (os_get_time(&now) < 0 || lifetime <= 0 || now.sec > lifetime) {
253 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Key not valid anymore "
254 "(lifetime=%ld now=%ld)", lifetime, now.sec);
259 if (lifetime - now.sec < PAC_KEY_REFRESH_TIME)
260 data->send_new_pac = 1;
263 * RFC 4851, Section 5.1:
264 * master_secret = T-PRF(PAC-Key, "PAC to master secret label hash",
265 * server_random + client_random, 48)
267 os_memcpy(seed, server_random, TLS_RANDOM_LEN);
268 os_memcpy(seed + TLS_RANDOM_LEN, client_random, TLS_RANDOM_LEN);
269 sha1_t_prf(pac_key, EAP_FAST_PAC_KEY_LEN,
270 "PAC to master secret label hash",
271 seed, sizeof(seed), master_secret, TLS_MASTER_SECRET_LEN);
273 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: master_secret",
274 master_secret, TLS_MASTER_SECRET_LEN);
282 static u8 * eap_fast_derive_key(struct eap_sm *sm, struct eap_ssl_data *data,
283 char *label, size_t len)
285 struct tls_keys keys;
286 u8 *rnd = NULL, *out;
289 block_size = tls_connection_get_keyblock_size(sm->ssl_ctx, data->conn);
293 out = os_malloc(block_size + len);
297 if (tls_connection_prf(sm->ssl_ctx, data->conn, label, 1, out,
298 block_size + len) == 0) {
299 os_memmove(out, out + block_size, len);
303 if (tls_connection_get_keys(sm->ssl_ctx, data->conn, &keys))
306 rnd = os_malloc(keys.client_random_len + keys.server_random_len);
310 os_memcpy(rnd, keys.server_random, keys.server_random_len);
311 os_memcpy(rnd + keys.server_random_len, keys.client_random,
312 keys.client_random_len);
314 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: master_secret for key "
315 "expansion", keys.master_key, keys.master_key_len);
316 if (tls_prf(keys.master_key, keys.master_key_len,
317 label, rnd, keys.client_random_len +
318 keys.server_random_len, out, block_size + len))
321 os_memmove(out, out + block_size, len);
331 static void eap_fast_derive_key_auth(struct eap_sm *sm,
332 struct eap_fast_data *data)
336 /* RFC 4851, Section 5.1:
337 * Extra key material after TLS key_block: session_key_seed[40]
340 sks = eap_fast_derive_key(sm, &data->ssl, "key expansion",
343 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive "
349 * RFC 4851, Section 5.2:
350 * S-IMCK[0] = session_key_seed
352 wpa_hexdump_key(MSG_DEBUG,
353 "EAP-FAST: session_key_seed (SKS = S-IMCK[0])",
354 sks, EAP_FAST_SKS_LEN);
356 os_memcpy(data->simck, sks, EAP_FAST_SIMCK_LEN);
361 static void eap_fast_derive_key_provisioning(struct eap_sm *sm,
362 struct eap_fast_data *data)
364 os_free(data->key_block_p);
365 data->key_block_p = (struct eap_fast_key_block_provisioning *)
366 eap_fast_derive_key(sm, &data->ssl, "key expansion",
367 sizeof(*data->key_block_p));
368 if (data->key_block_p == NULL) {
369 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive key block");
373 * RFC 4851, Section 5.2:
374 * S-IMCK[0] = session_key_seed
376 wpa_hexdump_key(MSG_DEBUG,
377 "EAP-FAST: session_key_seed (SKS = S-IMCK[0])",
378 data->key_block_p->session_key_seed,
379 sizeof(data->key_block_p->session_key_seed));
381 os_memcpy(data->simck, data->key_block_p->session_key_seed,
383 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: server_challenge",
384 data->key_block_p->server_challenge,
385 sizeof(data->key_block_p->server_challenge));
386 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: client_challenge",
387 data->key_block_p->client_challenge,
388 sizeof(data->key_block_p->client_challenge));
392 static int eap_fast_get_phase2_key(struct eap_sm *sm,
393 struct eap_fast_data *data,
394 u8 *isk, size_t isk_len)
399 os_memset(isk, 0, isk_len);
401 if (data->phase2_method == NULL || data->phase2_priv == NULL) {
402 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 method not "
407 if (data->phase2_method->getKey == NULL)
410 if ((key = data->phase2_method->getKey(sm, data->phase2_priv,
411 &key_len)) == NULL) {
412 wpa_printf(MSG_DEBUG, "EAP-FAST: Could not get key material "
417 if (key_len > isk_len)
419 os_memcpy(isk, key, key_len);
426 static int eap_fast_update_icmk(struct eap_sm *sm, struct eap_fast_data *data)
428 u8 isk[32], imck[60];
430 wpa_printf(MSG_DEBUG, "EAP-FAST: Deriving ICMK[%d] (S-IMCK and CMK)",
431 data->simck_idx + 1);
434 * RFC 4851, Section 5.2:
435 * IMCK[j] = T-PRF(S-IMCK[j-1], "Inner Methods Compound Keys",
437 * S-IMCK[j] = first 40 octets of IMCK[j]
438 * CMK[j] = last 20 octets of IMCK[j]
441 if (eap_fast_get_phase2_key(sm, data, isk, sizeof(isk)) < 0)
443 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: ISK[j]", isk, sizeof(isk));
444 sha1_t_prf(data->simck, EAP_FAST_SIMCK_LEN,
445 "Inner Methods Compound Keys",
446 isk, sizeof(isk), imck, sizeof(imck));
448 os_memcpy(data->simck, imck, EAP_FAST_SIMCK_LEN);
449 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: S-IMCK[j]",
450 data->simck, EAP_FAST_SIMCK_LEN);
451 os_memcpy(data->cmk, imck + EAP_FAST_SIMCK_LEN, 20);
452 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: CMK[j]", data->cmk, 20);
458 static void * eap_fast_init(struct eap_sm *sm)
460 struct eap_fast_data *data;
462 TLS_CIPHER_ANON_DH_AES128_SHA,
463 TLS_CIPHER_AES128_SHA,
464 TLS_CIPHER_RSA_DHE_AES128_SHA,
469 data = os_zalloc(sizeof(*data));
472 data->fast_version = EAP_FAST_VERSION;
473 data->force_version = -1;
474 if (sm->user && sm->user->force_version >= 0) {
475 data->force_version = sm->user->force_version;
476 wpa_printf(MSG_DEBUG, "EAP-FAST: forcing version %d",
477 data->force_version);
478 data->fast_version = data->force_version;
482 if (eap_server_tls_ssl_init(sm, &data->ssl, 0)) {
483 wpa_printf(MSG_INFO, "EAP-FAST: Failed to initialize SSL.");
484 eap_fast_reset(sm, data);
488 if (tls_connection_set_cipher_list(sm->ssl_ctx, data->ssl.conn,
490 wpa_printf(MSG_INFO, "EAP-FAST: Failed to set TLS cipher "
492 eap_fast_reset(sm, data);
496 if (tls_connection_set_session_ticket_cb(sm->ssl_ctx, data->ssl.conn,
497 eap_fast_session_ticket_cb,
499 wpa_printf(MSG_INFO, "EAP-FAST: Failed to set SessionTicket "
501 eap_fast_reset(sm, data);
505 if (sm->pac_opaque_encr_key == NULL) {
506 wpa_printf(MSG_INFO, "EAP-FAST: No PAC-Opaque encryption key "
508 eap_fast_reset(sm, data);
511 os_memcpy(data->pac_opaque_encr, sm->pac_opaque_encr_key,
512 sizeof(data->pac_opaque_encr));
514 if (sm->eap_fast_a_id == NULL) {
515 wpa_printf(MSG_INFO, "EAP-FAST: No A-ID configured");
516 eap_fast_reset(sm, data);
519 data->srv_id = os_strdup(sm->eap_fast_a_id);
520 if (data->srv_id == NULL) {
521 eap_fast_reset(sm, data);
529 static void eap_fast_reset(struct eap_sm *sm, void *priv)
531 struct eap_fast_data *data = priv;
534 if (data->phase2_priv && data->phase2_method)
535 data->phase2_method->reset(sm, data->phase2_priv);
536 eap_server_tls_ssl_deinit(sm, &data->ssl);
537 os_free(data->srv_id);
538 os_free(data->key_block_p);
539 wpabuf_free(data->pending_phase2_resp);
540 os_free(data->identity);
545 static struct wpabuf * eap_fast_build_start(struct eap_sm *sm,
546 struct eap_fast_data *data, u8 id)
549 struct pac_tlv_hdr *a_id;
550 size_t srv_id_len = os_strlen(data->srv_id);
552 req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_FAST,
553 1 + sizeof(*a_id) + srv_id_len,
554 EAP_CODE_REQUEST, id);
556 wpa_printf(MSG_ERROR, "EAP-FAST: Failed to allocate memory for"
558 eap_fast_state(data, FAILURE);
562 wpabuf_put_u8(req, EAP_TLS_FLAGS_START | data->fast_version);
563 a_id = wpabuf_put(req, sizeof(*a_id));
564 a_id->type = host_to_be16(PAC_TYPE_A_ID);
565 a_id->len = host_to_be16(srv_id_len);
566 wpabuf_put_data(req, data->srv_id, srv_id_len);
568 eap_fast_state(data, PHASE1);
574 static int eap_fast_phase1_done(struct eap_sm *sm, struct eap_fast_data *data)
578 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase1 done, starting Phase2");
580 if (tls_get_cipher(sm->ssl_ctx, data->ssl.conn, cipher, sizeof(cipher))
582 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to get cipher "
584 eap_fast_state(data, FAILURE);
587 data->anon_provisioning = os_strstr(cipher, "ADH") != NULL;
589 if (data->anon_provisioning) {
590 wpa_printf(MSG_DEBUG, "EAP-FAST: Anonymous provisioning");
591 eap_fast_derive_key_provisioning(sm, data);
593 eap_fast_derive_key_auth(sm, data);
595 eap_fast_state(data, PHASE2_START);
601 static struct wpabuf * eap_fast_build_req(struct eap_sm *sm,
602 struct eap_fast_data *data, u8 id)
607 res = eap_server_tls_buildReq_helper(sm, &data->ssl, EAP_TYPE_FAST,
608 data->fast_version, id, &req);
610 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
611 if (eap_fast_phase1_done(sm, data) < 0) {
618 return eap_server_tls_build_ack(id, EAP_TYPE_FAST,
624 static struct wpabuf * eap_fast_encrypt(struct eap_sm *sm,
625 struct eap_fast_data *data,
626 u8 id, u8 *plain, size_t plain_len)
631 /* TODO: add support for fragmentation, if needed. This will need to
632 * add TLS Message Length field, if the frame is fragmented. */
633 buf = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_FAST,
634 1 + data->ssl.tls_out_limit,
635 EAP_CODE_REQUEST, id);
639 wpabuf_put_u8(buf, data->fast_version);
641 res = tls_connection_encrypt(sm->ssl_ctx, data->ssl.conn,
642 plain, plain_len, wpabuf_put(buf, 0),
643 data->ssl.tls_out_limit);
645 wpa_printf(MSG_INFO, "EAP-FAST: Failed to encrypt Phase 2 "
651 wpabuf_put(buf, res);
658 static struct wpabuf * eap_fast_tlv_eap_payload(struct wpabuf *buf)
661 struct eap_tlv_hdr *tlv;
666 /* Encapsulate EAP packet in EAP-Payload TLV */
667 wpa_printf(MSG_DEBUG, "EAP-FAST: Add EAP-Payload TLV");
668 e = wpabuf_alloc(sizeof(*tlv) + wpabuf_len(buf));
670 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to allocate memory "
671 "for TLV encapsulation");
675 tlv = wpabuf_put(e, sizeof(*tlv));
676 tlv->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
677 EAP_TLV_EAP_PAYLOAD_TLV);
678 tlv->length = host_to_be16(wpabuf_len(buf));
679 wpabuf_put_buf(e, buf);
685 static struct wpabuf * eap_fast_build_phase2_req(struct eap_sm *sm,
686 struct eap_fast_data *data,
691 if (data->phase2_priv == NULL) {
692 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 method not "
696 req = data->phase2_method->buildReq(sm, data->phase2_priv, id);
700 wpa_hexdump_buf_key(MSG_MSGDUMP, "EAP-FAST: Phase 2 EAP-Request", req);
701 return eap_fast_tlv_eap_payload(req);
705 static struct wpabuf * eap_fast_build_crypto_binding(
706 struct eap_sm *sm, struct eap_fast_data *data)
709 struct eap_tlv_result_tlv *result;
710 struct eap_tlv_crypto_binding__tlv *binding;
713 buf = wpabuf_alloc(sizeof(*result) + sizeof(*binding));
717 if (data->send_new_pac || data->anon_provisioning) {
718 type = EAP_TLV_INTERMEDIATE_RESULT_TLV;
719 data->final_result = 0;
721 type = EAP_TLV_RESULT_TLV;
722 data->final_result = 1;
726 wpa_printf(MSG_DEBUG, "EAP-FAST: Add Result TLV (status=SUCCESS)");
727 result = wpabuf_put(buf, sizeof(*result));
728 result->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY | type);
729 result->length = host_to_be16(2);
730 result->status = host_to_be16(EAP_TLV_RESULT_SUCCESS);
732 /* Crypto-Binding TLV */
733 binding = wpabuf_put(buf, sizeof(*binding));
734 binding->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
735 EAP_TLV_CRYPTO_BINDING_TLV);
736 binding->length = host_to_be16(sizeof(*binding) -
737 sizeof(struct eap_tlv_hdr));
738 binding->version = EAP_FAST_VERSION;
739 binding->received_version = data->peer_version;
740 binding->subtype = EAP_TLV_CRYPTO_BINDING_SUBTYPE_REQUEST;
741 if (os_get_random(binding->nonce, sizeof(binding->nonce)) < 0) {
747 * RFC 4851, Section 4.2.8:
748 * The nonce in a request MUST have its least significant bit set to 0.
750 binding->nonce[sizeof(binding->nonce) - 1] &= ~0x01;
752 os_memcpy(data->crypto_binding_nonce, binding->nonce,
753 sizeof(binding->nonce));
756 * RFC 4851, Section 5.3:
758 * Compound-MAC = HMAC-SHA1( CMK, Crypto-Binding TLV )
761 hmac_sha1(data->cmk, 20, (u8 *) binding, sizeof(*binding),
762 binding->compound_mac);
764 wpa_printf(MSG_DEBUG, "EAP-FAST: Add Crypto-Binding TLV: Version %d "
765 "Received Version %d SubType %d",
766 binding->version, binding->received_version,
768 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE",
769 binding->nonce, sizeof(binding->nonce));
770 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC",
771 binding->compound_mac, sizeof(binding->compound_mac));
777 static struct wpabuf * eap_fast_build_pac(struct eap_sm *sm,
778 struct eap_fast_data *data)
780 u8 pac_key[EAP_FAST_PAC_KEY_LEN];
781 u8 *pac_buf, *pac_opaque;
784 size_t buf_len, srv_id_len, pac_len;
785 struct eap_tlv_hdr *pac_tlv;
786 struct pac_tlv_hdr *hdr, *pac_info;
787 struct eap_tlv_result_tlv *result;
790 if (os_get_random(pac_key, EAP_FAST_PAC_KEY_LEN) < 0 ||
791 os_get_time(&now) < 0)
793 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: Generated PAC-Key",
794 pac_key, EAP_FAST_PAC_KEY_LEN);
796 pac_len = (2 + EAP_FAST_PAC_KEY_LEN) + (2 + 4) +
797 (2 + sm->identity_len) + 8;
798 pac_buf = os_malloc(pac_len);
802 srv_id_len = os_strlen(data->srv_id);
805 *pos++ = PAC_OPAQUE_TYPE_KEY;
806 *pos++ = EAP_FAST_PAC_KEY_LEN;
807 os_memcpy(pos, pac_key, EAP_FAST_PAC_KEY_LEN);
808 pos += EAP_FAST_PAC_KEY_LEN;
810 *pos++ = PAC_OPAQUE_TYPE_LIFETIME;
812 WPA_PUT_BE32(pos, now.sec + PAC_KEY_LIFETIME);
816 *pos++ = PAC_OPAQUE_TYPE_IDENTITY;
817 *pos++ = sm->identity_len;
818 os_memcpy(pos, sm->identity, sm->identity_len);
819 pos += sm->identity_len;
822 pac_len = pos - pac_buf;
824 *pos++ = PAC_OPAQUE_TYPE_PAD;
828 pac_opaque = os_malloc(pac_len + 8);
829 if (pac_opaque == NULL) {
833 if (aes_wrap(data->pac_opaque_encr, pac_len / 8, pac_buf,
842 wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Opaque",
843 pac_opaque, pac_len);
845 buf_len = sizeof(*pac_tlv) +
846 sizeof(*hdr) + EAP_FAST_PAC_KEY_LEN +
847 sizeof(*hdr) + pac_len +
848 2 * srv_id_len + 100 + sizeof(*result);
849 buf = wpabuf_alloc(buf_len);
856 wpa_printf(MSG_DEBUG, "EAP-FAST: Add PAC TLV");
857 pac_tlv = wpabuf_put(buf, sizeof(*pac_tlv));
858 pac_tlv->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
862 hdr = wpabuf_put(buf, sizeof(*hdr));
863 hdr->type = host_to_be16(PAC_TYPE_PAC_KEY);
864 hdr->len = host_to_be16(EAP_FAST_PAC_KEY_LEN);
865 wpabuf_put_data(buf, pac_key, EAP_FAST_PAC_KEY_LEN);
868 hdr = wpabuf_put(buf, sizeof(*hdr));
869 hdr->type = host_to_be16(PAC_TYPE_PAC_OPAQUE);
870 hdr->len = host_to_be16(pac_len);
871 wpabuf_put_data(buf, pac_opaque, pac_len);
875 pac_info = wpabuf_put(buf, sizeof(*pac_info));
876 pac_info->type = host_to_be16(PAC_TYPE_PAC_INFO);
878 /* PAC-Lifetime (inside PAC-Info) */
879 hdr = wpabuf_put(buf, sizeof(*hdr));
880 hdr->type = host_to_be16(PAC_TYPE_CRED_LIFETIME);
881 hdr->len = host_to_be16(4);
882 wpabuf_put_be32(buf, now.sec + PAC_KEY_LIFETIME);
884 /* A-ID (inside PAC-Info) */
885 hdr = wpabuf_put(buf, sizeof(*hdr));
886 hdr->type = host_to_be16(PAC_TYPE_A_ID);
887 hdr->len = host_to_be16(srv_id_len);
888 wpabuf_put_data(buf, data->srv_id, srv_id_len);
890 /* Note: headers may be misaligned after A-ID */
892 /* A-ID-Info (inside PAC-Info) */
893 hdr = wpabuf_put(buf, sizeof(*hdr));
894 WPA_PUT_BE16((u8 *) &hdr->type, PAC_TYPE_A_ID_INFO);
895 WPA_PUT_BE16((u8 *) &hdr->len, srv_id_len);
896 wpabuf_put_data(buf, data->srv_id, srv_id_len);
898 /* PAC-Type (inside PAC-Info) */
899 hdr = wpabuf_put(buf, sizeof(*hdr));
900 WPA_PUT_BE16((u8 *) &hdr->type, PAC_TYPE_PAC_TYPE);
901 WPA_PUT_BE16((u8 *) &hdr->len, 2);
902 wpabuf_put_be16(buf, PAC_TYPE_TUNNEL_PAC);
904 /* Update PAC-Info and PAC TLV Length fields */
905 pos = wpabuf_put(buf, 0);
906 pac_info->len = host_to_be16(pos - (u8 *) (pac_info + 1));
907 pac_tlv->length = host_to_be16(pos - (u8 *) (pac_tlv + 1));
910 wpa_printf(MSG_DEBUG, "EAP-FAST: Add Result TLV (status=SUCCESS)");
911 result = wpabuf_put(buf, sizeof(*result));
912 WPA_PUT_BE16((u8 *) &result->tlv_type,
913 EAP_TLV_TYPE_MANDATORY | EAP_TLV_RESULT_TLV);
914 WPA_PUT_BE16((u8 *) &result->length, 2);
915 WPA_PUT_BE16((u8 *) &result->status, EAP_TLV_RESULT_SUCCESS);
921 static struct wpabuf * eap_fast_buildReq(struct eap_sm *sm, void *priv, u8 id)
923 struct eap_fast_data *data = priv;
927 if (data->state == START)
928 return eap_fast_build_start(sm, data, id);
930 if (data->state == PHASE1)
931 return eap_fast_build_req(sm, data, id);
933 switch (data->state) {
936 req = eap_fast_build_phase2_req(sm, data, id);
939 req = eap_fast_build_crypto_binding(sm, data);
942 req = eap_fast_build_pac(sm, data);
945 wpa_printf(MSG_DEBUG, "EAP-FAST: %s - unexpected state %d",
946 __func__, data->state);
953 wpa_hexdump_buf_key(MSG_DEBUG, "EAP-FAST: Encrypting Phase 2 TLVs",
955 encr = eap_fast_encrypt(sm, data, id, wpabuf_mhead(req),
963 static Boolean eap_fast_check(struct eap_sm *sm, void *priv,
964 struct wpabuf *respData)
969 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_FAST, respData, &len);
970 if (pos == NULL || len < 1) {
971 wpa_printf(MSG_INFO, "EAP-FAST: Invalid frame");
979 static int eap_fast_phase2_init(struct eap_sm *sm, struct eap_fast_data *data,
982 if (data->phase2_priv && data->phase2_method) {
983 data->phase2_method->reset(sm, data->phase2_priv);
984 data->phase2_method = NULL;
985 data->phase2_priv = NULL;
987 data->phase2_method = eap_server_get_eap_method(EAP_VENDOR_IETF,
989 if (!data->phase2_method)
992 if (data->key_block_p) {
993 sm->auth_challenge = data->key_block_p->server_challenge;
994 sm->peer_challenge = data->key_block_p->client_challenge;
997 data->phase2_priv = data->phase2_method->init(sm);
999 sm->auth_challenge = NULL;
1000 sm->peer_challenge = NULL;
1002 return data->phase2_priv == NULL ? -1 : 0;
1006 static void eap_fast_process_phase2_response(struct eap_sm *sm,
1007 struct eap_fast_data *data,
1008 u8 *in_data, size_t in_len)
1010 u8 next_type = EAP_TYPE_NONE;
1011 struct eap_hdr *hdr;
1015 const struct eap_method *m = data->phase2_method;
1016 void *priv = data->phase2_priv;
1019 wpa_printf(MSG_DEBUG, "EAP-FAST: %s - Phase2 not "
1020 "initialized?!", __func__);
1024 hdr = (struct eap_hdr *) in_data;
1025 pos = (u8 *) (hdr + 1);
1027 if (in_len > sizeof(*hdr) && *pos == EAP_TYPE_NAK) {
1028 left = in_len - sizeof(*hdr);
1029 wpa_hexdump(MSG_DEBUG, "EAP-FAST: Phase2 type Nak'ed; "
1030 "allowed types", pos + 1, left - 1);
1031 eap_sm_process_nak(sm, pos + 1, left - 1);
1032 if (sm->user && sm->user_eap_method_index < EAP_MAX_METHODS &&
1033 sm->user->methods[sm->user_eap_method_index].method !=
1035 next_type = sm->user->methods[
1036 sm->user_eap_method_index++].method;
1037 wpa_printf(MSG_DEBUG, "EAP-FAST: try EAP type %d",
1040 next_type = eap_fast_req_failure(sm, data);
1042 eap_fast_phase2_init(sm, data, next_type);
1046 wpabuf_set(&buf, in_data, in_len);
1048 if (m->check(sm, priv, &buf)) {
1049 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase2 check() asked to "
1050 "ignore the packet");
1051 next_type = eap_fast_req_failure(sm, data);
1055 m->process(sm, priv, &buf);
1057 if (!m->isDone(sm, priv))
1060 if (!m->isSuccess(sm, priv)) {
1061 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase2 method failed");
1062 next_type = eap_fast_req_failure(sm, data);
1063 eap_fast_phase2_init(sm, data, next_type);
1067 switch (data->state) {
1069 if (eap_user_get(sm, sm->identity, sm->identity_len, 1) != 0) {
1070 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: Phase2 "
1071 "Identity not found in the user "
1073 sm->identity, sm->identity_len);
1074 next_type = eap_fast_req_failure(sm, data);
1078 eap_fast_state(data, PHASE2_METHOD);
1079 if (data->anon_provisioning) {
1081 * Only EAP-MSCHAPv2 is allowed for anonymous
1084 next_type = EAP_TYPE_MSCHAPV2;
1085 sm->user_eap_method_index = 0;
1087 next_type = sm->user->methods[0].method;
1088 sm->user_eap_method_index = 1;
1090 wpa_printf(MSG_DEBUG, "EAP-FAST: try EAP type %d", next_type);
1093 eap_fast_update_icmk(sm, data);
1094 eap_fast_state(data, CRYPTO_BINDING);
1095 next_type = EAP_TYPE_NONE;
1100 wpa_printf(MSG_DEBUG, "EAP-FAST: %s - unexpected state %d",
1101 __func__, data->state);
1105 eap_fast_phase2_init(sm, data, next_type);
1109 static void eap_fast_process_phase2_eap(struct eap_sm *sm,
1110 struct eap_fast_data *data,
1111 u8 *in_data, size_t in_len)
1113 struct eap_hdr *hdr;
1116 hdr = (struct eap_hdr *) in_data;
1117 if (in_len < (int) sizeof(*hdr)) {
1118 wpa_printf(MSG_INFO, "EAP-FAST: Too short Phase 2 "
1119 "EAP frame (len=%d)", in_len);
1120 eap_fast_req_failure(sm, data);
1123 len = be_to_host16(hdr->length);
1125 wpa_printf(MSG_INFO, "EAP-FAST: Length mismatch in "
1126 "Phase 2 EAP frame (len=%d hdr->length=%d)",
1128 eap_fast_req_failure(sm, data);
1131 wpa_printf(MSG_DEBUG, "EAP-FAST: Received Phase 2: code=%d "
1132 "identifier=%d length=%d", hdr->code, hdr->identifier, len);
1133 switch (hdr->code) {
1134 case EAP_CODE_RESPONSE:
1135 eap_fast_process_phase2_response(sm, data, (u8 *) hdr, len);
1138 wpa_printf(MSG_INFO, "EAP-FAST: Unexpected code=%d in "
1139 "Phase 2 EAP header", hdr->code);
1145 struct eap_fast_tlv_parse {
1146 u8 *eap_payload_tlv;
1147 size_t eap_payload_tlv_len;
1148 struct eap_tlv_crypto_binding__tlv *crypto_binding;
1149 size_t crypto_binding_len;
1158 static int eap_fast_parse_tlv(struct eap_fast_tlv_parse *tlv,
1159 int tlv_type, u8 *pos, int len)
1162 case EAP_TLV_EAP_PAYLOAD_TLV:
1163 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: EAP-Payload TLV",
1165 if (tlv->eap_payload_tlv) {
1166 wpa_printf(MSG_DEBUG, "EAP-FAST: More than one "
1167 "EAP-Payload TLV in the message");
1168 tlv->iresult = EAP_TLV_RESULT_FAILURE;
1171 tlv->eap_payload_tlv = pos;
1172 tlv->eap_payload_tlv_len = len;
1174 case EAP_TLV_RESULT_TLV:
1175 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Result TLV", pos, len);
1177 wpa_printf(MSG_DEBUG, "EAP-FAST: More than one "
1178 "Result TLV in the message");
1179 tlv->result = EAP_TLV_RESULT_FAILURE;
1183 wpa_printf(MSG_DEBUG, "EAP-FAST: Too short "
1185 tlv->result = EAP_TLV_RESULT_FAILURE;
1188 tlv->result = WPA_GET_BE16(pos);
1189 if (tlv->result != EAP_TLV_RESULT_SUCCESS &&
1190 tlv->result != EAP_TLV_RESULT_FAILURE) {
1191 wpa_printf(MSG_DEBUG, "EAP-FAST: Unknown Result %d",
1193 tlv->result = EAP_TLV_RESULT_FAILURE;
1195 wpa_printf(MSG_DEBUG, "EAP-FAST: Result: %s",
1196 tlv->result == EAP_TLV_RESULT_SUCCESS ?
1197 "Success" : "Failure");
1199 case EAP_TLV_INTERMEDIATE_RESULT_TLV:
1200 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Intermediate Result TLV",
1203 wpa_printf(MSG_DEBUG, "EAP-FAST: Too short "
1204 "Intermediate-Result TLV");
1205 tlv->iresult = EAP_TLV_RESULT_FAILURE;
1209 wpa_printf(MSG_DEBUG, "EAP-FAST: More than one "
1210 "Intermediate-Result TLV in the message");
1211 tlv->iresult = EAP_TLV_RESULT_FAILURE;
1214 tlv->iresult = WPA_GET_BE16(pos);
1215 if (tlv->iresult != EAP_TLV_RESULT_SUCCESS &&
1216 tlv->iresult != EAP_TLV_RESULT_FAILURE) {
1217 wpa_printf(MSG_DEBUG, "EAP-FAST: Unknown Intermediate "
1218 "Result %d", tlv->iresult);
1219 tlv->iresult = EAP_TLV_RESULT_FAILURE;
1221 wpa_printf(MSG_DEBUG, "EAP-FAST: Intermediate Result: %s",
1222 tlv->iresult == EAP_TLV_RESULT_SUCCESS ?
1223 "Success" : "Failure");
1225 case EAP_TLV_CRYPTO_BINDING_TLV:
1226 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Crypto-Binding TLV",
1228 if (tlv->crypto_binding) {
1229 wpa_printf(MSG_DEBUG, "EAP-FAST: More than one "
1230 "Crypto-Binding TLV in the message");
1231 tlv->iresult = EAP_TLV_RESULT_FAILURE;
1234 tlv->crypto_binding_len = sizeof(struct eap_tlv_hdr) + len;
1235 if (tlv->crypto_binding_len < sizeof(*tlv->crypto_binding)) {
1236 wpa_printf(MSG_DEBUG, "EAP-FAST: Too short "
1237 "Crypto-Binding TLV");
1238 tlv->iresult = EAP_TLV_RESULT_FAILURE;
1241 tlv->crypto_binding = (struct eap_tlv_crypto_binding__tlv *)
1242 (pos - sizeof(struct eap_tlv_hdr));
1244 case EAP_TLV_REQUEST_ACTION_TLV:
1245 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Request-Action TLV",
1247 if (tlv->request_action) {
1248 wpa_printf(MSG_DEBUG, "EAP-FAST: More than one "
1249 "Request-Action TLV in the message");
1250 tlv->iresult = EAP_TLV_RESULT_FAILURE;
1254 wpa_printf(MSG_DEBUG, "EAP-FAST: Too short "
1255 "Request-Action TLV");
1256 tlv->iresult = EAP_TLV_RESULT_FAILURE;
1259 tlv->request_action = WPA_GET_BE16(pos);
1260 wpa_printf(MSG_DEBUG, "EAP-FAST: Request-Action: %d",
1261 tlv->request_action);
1263 case EAP_TLV_PAC_TLV:
1264 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: PAC TLV", pos, len);
1266 wpa_printf(MSG_DEBUG, "EAP-FAST: More than one "
1267 "PAC TLV in the message");
1268 tlv->iresult = EAP_TLV_RESULT_FAILURE;
1283 static int eap_fast_parse_tlvs(u8 *data, size_t data_len,
1284 struct eap_fast_tlv_parse *tlv)
1286 int mandatory, tlv_type, len, res;
1289 os_memset(tlv, 0, sizeof(*tlv));
1292 end = data + data_len;
1293 while (pos + 4 < end) {
1294 mandatory = pos[0] & 0x80;
1295 tlv_type = WPA_GET_BE16(pos) & 0x3fff;
1297 len = WPA_GET_BE16(pos);
1299 if (pos + len > end) {
1300 wpa_printf(MSG_INFO, "EAP-FAST: TLV overflow");
1303 wpa_printf(MSG_DEBUG, "EAP-FAST: Received Phase 2: "
1304 "TLV type %d length %d%s",
1305 tlv_type, len, mandatory ? " (mandatory)" : "");
1307 res = eap_fast_parse_tlv(tlv, tlv_type, pos, len);
1312 wpa_printf(MSG_DEBUG, "EAP-FAST: Nak unknown "
1313 "mandatory TLV type %d", tlv_type);
1314 /* TODO: generate Nak TLV */
1317 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignored "
1318 "unknown optional TLV type %d",
1330 static int eap_fast_validate_crypto_binding(
1331 struct eap_fast_data *data, struct eap_tlv_crypto_binding__tlv *b,
1336 wpa_printf(MSG_DEBUG, "EAP-FAST: Reply Crypto-Binding TLV: "
1337 "Version %d Received Version %d SubType %d",
1338 b->version, b->received_version, b->subtype);
1339 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE",
1340 b->nonce, sizeof(b->nonce));
1341 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC",
1342 b->compound_mac, sizeof(b->compound_mac));
1344 if (b->version != EAP_FAST_VERSION ||
1345 b->received_version != EAP_FAST_VERSION) {
1346 wpa_printf(MSG_DEBUG, "EAP-FAST: Unexpected version "
1347 "in Crypto-Binding: version %d "
1348 "received_version %d", b->version,
1349 b->received_version);
1353 if (b->subtype != EAP_TLV_CRYPTO_BINDING_SUBTYPE_RESPONSE) {
1354 wpa_printf(MSG_DEBUG, "EAP-FAST: Unexpected subtype in "
1355 "Crypto-Binding: %d", b->subtype);
1359 if (os_memcmp(data->crypto_binding_nonce, b->nonce, 31) != 0 ||
1360 (data->crypto_binding_nonce[31] | 1) != b->nonce[31]) {
1361 wpa_printf(MSG_DEBUG, "EAP-FAST: Invalid nonce in "
1366 os_memcpy(cmac, b->compound_mac, sizeof(cmac));
1367 os_memset(b->compound_mac, 0, sizeof(cmac));
1368 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Crypto-Binding TLV for "
1369 "Compound MAC calculation",
1370 (u8 *) b, bind_len);
1371 hmac_sha1(data->cmk, 20, (u8 *) b, bind_len, b->compound_mac);
1372 if (os_memcmp(cmac, b->compound_mac, sizeof(cmac)) != 0) {
1373 wpa_hexdump(MSG_MSGDUMP,
1374 "EAP-FAST: Calculated Compound MAC",
1375 b->compound_mac, sizeof(cmac));
1376 wpa_printf(MSG_INFO, "EAP-FAST: Compound MAC did not "
1385 static int eap_fast_pac_type(u8 *pac, size_t len, u16 type)
1387 struct eap_tlv_pac_type_tlv *tlv;
1389 if (pac == NULL || len != sizeof(*tlv))
1392 tlv = (struct eap_tlv_pac_type_tlv *) pac;
1394 return be_to_host16(tlv->tlv_type) == PAC_TYPE_PAC_TYPE &&
1395 be_to_host16(tlv->length) == 2 &&
1396 be_to_host16(tlv->pac_type) == type;
1400 static void eap_fast_process_phase2_tlvs(struct eap_sm *sm,
1401 struct eap_fast_data *data,
1402 u8 *in_data, size_t in_len)
1404 struct eap_fast_tlv_parse tlv;
1405 int check_crypto_binding = data->state == CRYPTO_BINDING;
1407 if (eap_fast_parse_tlvs(in_data, in_len, &tlv) < 0) {
1408 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to parse received "
1413 if (tlv.result == EAP_TLV_RESULT_FAILURE) {
1414 wpa_printf(MSG_DEBUG, "EAP-FAST: Result TLV indicated "
1416 eap_fast_state(data, FAILURE);
1420 if (data->state == REQUEST_PAC) {
1422 if (tlv.pac == NULL || tlv.pac_len < 6) {
1423 wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC "
1424 "Acknowledgement received");
1425 eap_fast_state(data, FAILURE);
1429 type = WPA_GET_BE16(tlv.pac);
1430 len = WPA_GET_BE16(tlv.pac + 2);
1431 res = WPA_GET_BE16(tlv.pac + 4);
1433 if (type != PAC_TYPE_PAC_ACKNOWLEDGEMENT || len != 2 ||
1434 res != EAP_TLV_RESULT_SUCCESS) {
1435 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV did not "
1436 "contain acknowledgement");
1437 eap_fast_state(data, FAILURE);
1441 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Acknowledgement received "
1442 "- PAC provisioning succeeded");
1443 eap_fast_state(data, data->anon_provisioning ?
1448 if (tlv.eap_payload_tlv) {
1449 eap_fast_process_phase2_eap(sm, data, tlv.eap_payload_tlv,
1450 tlv.eap_payload_tlv_len);
1453 if (check_crypto_binding) {
1454 if (tlv.crypto_binding == NULL) {
1455 wpa_printf(MSG_DEBUG, "EAP-FAST: No Crypto-Binding "
1457 eap_fast_state(data, FAILURE);
1461 if (data->final_result &&
1462 tlv.result != EAP_TLV_RESULT_SUCCESS) {
1463 wpa_printf(MSG_DEBUG, "EAP-FAST: Crypto-Binding TLV "
1464 "without Success Result");
1465 eap_fast_state(data, FAILURE);
1469 if (!data->final_result &&
1470 tlv.iresult != EAP_TLV_RESULT_SUCCESS) {
1471 wpa_printf(MSG_DEBUG, "EAP-FAST: Crypto-Binding TLV "
1472 "without intermediate Success Result");
1473 eap_fast_state(data, FAILURE);
1477 if (eap_fast_validate_crypto_binding(data, tlv.crypto_binding,
1478 tlv.crypto_binding_len)) {
1479 eap_fast_state(data, FAILURE);
1483 wpa_printf(MSG_DEBUG, "EAP-FAST: Valid Crypto-Binding TLV "
1484 "received - authentication completed successfully");
1486 if (data->anon_provisioning ||
1487 (tlv.request_action == EAP_TLV_ACTION_PROCESS_TLV &&
1488 eap_fast_pac_type(tlv.pac, tlv.pac_len,
1489 PAC_TYPE_TUNNEL_PAC))) {
1490 wpa_printf(MSG_DEBUG, "EAP-FAST: Requested a new "
1492 eap_fast_state(data, REQUEST_PAC);
1493 } else if (data->send_new_pac) {
1494 wpa_printf(MSG_DEBUG, "EAP-FAST: Server triggered "
1495 "re-keying of Tunnel PAC");
1496 eap_fast_state(data, REQUEST_PAC);
1498 eap_fast_state(data, SUCCESS);
1503 static void eap_fast_process_phase2(struct eap_sm *sm,
1504 struct eap_fast_data *data,
1505 const u8 *in_data, size_t in_len)
1508 int len_decrypted, res;
1511 wpa_printf(MSG_DEBUG, "EAP-FAST: Received %lu bytes encrypted data for"
1512 " Phase 2", (unsigned long) in_len);
1514 if (data->pending_phase2_resp) {
1515 wpa_printf(MSG_DEBUG, "EAP-PEAP: Pending Phase 2 response - "
1516 "skip decryption and use old data");
1517 eap_fast_process_phase2_tlvs(
1518 sm, data, wpabuf_mhead(data->pending_phase2_resp),
1519 wpabuf_len(data->pending_phase2_resp));
1520 wpabuf_free(data->pending_phase2_resp);
1521 data->pending_phase2_resp = NULL;
1525 /* FIX: get rid of const -> non-const typecast */
1526 res = eap_server_tls_data_reassemble(sm, &data->ssl, (u8 **) &in_data,
1528 if (res < 0 || res == 1)
1532 if (data->ssl.tls_in_total > buf_len)
1533 buf_len = data->ssl.tls_in_total;
1534 in_decrypted = os_malloc(buf_len);
1535 if (in_decrypted == NULL) {
1536 os_free(data->ssl.tls_in);
1537 data->ssl.tls_in = NULL;
1538 data->ssl.tls_in_len = 0;
1539 wpa_printf(MSG_WARNING, "EAP-FAST: Failed to allocate memory "
1544 len_decrypted = tls_connection_decrypt(sm->ssl_ctx, data->ssl.conn,
1546 in_decrypted, buf_len);
1547 os_free(data->ssl.tls_in);
1548 data->ssl.tls_in = NULL;
1549 data->ssl.tls_in_len = 0;
1550 if (len_decrypted < 0) {
1551 wpa_printf(MSG_INFO, "EAP-FAST: Failed to decrypt Phase 2 "
1553 os_free(in_decrypted);
1554 eap_fast_state(data, FAILURE);
1558 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: Decrypted Phase 2 TLVs",
1559 in_decrypted, len_decrypted);
1561 eap_fast_process_phase2_tlvs(sm, data, in_decrypted, len_decrypted);
1563 if (sm->method_pending == METHOD_PENDING_WAIT) {
1564 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase2 method is in "
1565 "pending wait state - save decrypted response");
1566 wpabuf_free(data->pending_phase2_resp);
1567 data->pending_phase2_resp = wpabuf_alloc_copy(in_decrypted,
1571 os_free(in_decrypted);
1575 static void eap_fast_process(struct eap_sm *sm, void *priv,
1576 struct wpabuf *respData)
1578 struct eap_fast_data *data = priv;
1582 unsigned int tls_msg_len;
1586 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_FAST, respData,
1588 if (pos == NULL || left < 1)
1592 wpa_printf(MSG_DEBUG, "EAP-FAST: Received packet(len=%lu) - "
1593 "Flags 0x%02x", (unsigned long) wpabuf_len(respData),
1595 data->peer_version = peer_version = flags & EAP_PEAP_VERSION_MASK;
1596 if (data->force_version >= 0 && peer_version != data->force_version) {
1597 wpa_printf(MSG_INFO, "EAP-FAST: peer did not select the forced"
1598 " version (forced=%d peer=%d) - reject",
1599 data->force_version, peer_version);
1600 eap_fast_state(data, FAILURE);
1603 if (peer_version < data->fast_version) {
1604 wpa_printf(MSG_DEBUG, "EAP-FAST: peer ver=%d, own ver=%d; "
1606 peer_version, data->fast_version, peer_version);
1607 data->fast_version = peer_version;
1609 if (flags & EAP_TLS_FLAGS_LENGTH_INCLUDED) {
1611 wpa_printf(MSG_INFO, "EAP-FAST: Short frame with TLS "
1613 eap_fast_state(data, FAILURE);
1616 tls_msg_len = WPA_GET_BE32(pos);
1617 wpa_printf(MSG_DEBUG, "EAP-FAST: TLS Message Length: %d",
1619 if (data->ssl.tls_in_left == 0) {
1620 data->ssl.tls_in_total = tls_msg_len;
1621 data->ssl.tls_in_left = tls_msg_len;
1622 os_free(data->ssl.tls_in);
1623 data->ssl.tls_in = NULL;
1624 data->ssl.tls_in_len = 0;
1630 switch (data->state) {
1632 if (eap_server_tls_process_helper(sm, &data->ssl, pos, left) <
1634 wpa_printf(MSG_INFO, "EAP-FAST: TLS processing "
1636 eap_fast_state(data, FAILURE);
1639 if (!tls_connection_established(sm->ssl_ctx, data->ssl.conn) ||
1640 data->ssl.tls_out_len > 0)
1644 * Phase 1 was completed with the received message (e.g., when
1645 * using abbreviated handshake), so Phase 2 can be started
1646 * immediately without having to send through an empty message
1650 if (eap_fast_phase1_done(sm, data) < 0)
1653 /* fall through to PHASE2_START */
1655 if (data->identity) {
1656 os_free(sm->identity);
1657 sm->identity = data->identity;
1658 data->identity = NULL;
1659 sm->identity_len = data->identity_len;
1660 data->identity_len = 0;
1661 if (eap_user_get(sm, sm->identity, sm->identity_len, 1)
1663 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: "
1664 "Phase2 Identity not found "
1665 "in the user database",
1668 next_type = eap_fast_req_failure(sm, data);
1670 wpa_printf(MSG_DEBUG, "EAP-FAST: Identity "
1671 "already known - skip Phase 2 "
1672 "Identity Request");
1673 next_type = sm->user->methods[0].method;
1674 sm->user_eap_method_index = 1;
1677 eap_fast_state(data, PHASE2_METHOD);
1679 eap_fast_state(data, PHASE2_ID);
1680 next_type = EAP_TYPE_IDENTITY;
1683 eap_fast_phase2_init(sm, data, next_type);
1687 case CRYPTO_BINDING:
1689 eap_fast_process_phase2(sm, data, pos, left);
1692 wpa_printf(MSG_DEBUG, "EAP-FAST: Unexpected state %d in %s",
1693 data->state, __func__);
1697 if (tls_connection_get_write_alerts(sm->ssl_ctx, data->ssl.conn) > 1) {
1698 wpa_printf(MSG_INFO, "EAP-FAST: Locally detected fatal error "
1699 "in TLS processing");
1700 eap_fast_state(data, FAILURE);
1705 static Boolean eap_fast_isDone(struct eap_sm *sm, void *priv)
1707 struct eap_fast_data *data = priv;
1708 return data->state == SUCCESS || data->state == FAILURE;
1712 static u8 * eap_fast_getKey(struct eap_sm *sm, void *priv, size_t *len)
1714 struct eap_fast_data *data = priv;
1717 if (data->state != SUCCESS)
1721 * RFC 4851, Section 5.4: EAP Master Session Key Genreration
1722 * MSK = T-PRF(S-IMCK[j], "Session Key Generating Function", 64)
1725 eapKeyData = os_malloc(EAP_FAST_KEY_LEN);
1726 if (eapKeyData == NULL)
1729 sha1_t_prf(data->simck, EAP_FAST_SIMCK_LEN,
1730 "Session Key Generating Function", (u8 *) "", 0,
1731 eapKeyData, EAP_FAST_KEY_LEN);
1732 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: Derived key (MSK)",
1733 eapKeyData, EAP_FAST_KEY_LEN);
1734 *len = EAP_FAST_KEY_LEN;
1740 static u8 * eap_fast_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
1742 struct eap_fast_data *data = priv;
1745 if (data->state != SUCCESS)
1749 * RFC 4851, Section 5.4: EAP Master Session Key Genreration
1750 * EMSK = T-PRF(S-IMCK[j],
1751 * "Extended Session Key Generating Function", 64)
1754 eapKeyData = os_malloc(EAP_EMSK_LEN);
1755 if (eapKeyData == NULL)
1758 sha1_t_prf(data->simck, EAP_FAST_SIMCK_LEN,
1759 "Extended Session Key Generating Function",
1760 (u8 *) "", 0, eapKeyData, EAP_EMSK_LEN);
1761 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: Derived key (EMSK)",
1762 eapKeyData, EAP_EMSK_LEN);
1764 *len = EAP_EMSK_LEN;
1770 static Boolean eap_fast_isSuccess(struct eap_sm *sm, void *priv)
1772 struct eap_fast_data *data = priv;
1773 return data->state == SUCCESS;
1777 int eap_server_fast_register(void)
1779 struct eap_method *eap;
1782 eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
1783 EAP_VENDOR_IETF, EAP_TYPE_FAST, "FAST");
1787 eap->init = eap_fast_init;
1788 eap->reset = eap_fast_reset;
1789 eap->buildReq = eap_fast_buildReq;
1790 eap->check = eap_fast_check;
1791 eap->process = eap_fast_process;
1792 eap->isDone = eap_fast_isDone;
1793 eap->getKey = eap_fast_getKey;
1794 eap->get_emsk = eap_fast_get_emsk;
1795 eap->isSuccess = eap_fast_isSuccess;
1797 ret = eap_server_method_register(eap);
1799 eap_server_method_free(eap);