2 * EAP peer method: EAP-FAST (RFC 4851)
3 * Copyright (c) 2004-2015, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "crypto/tls.h"
13 #include "crypto/sha1.h"
14 #include "eap_common/eap_tlv_common.h"
16 #include "eap_tls_common.h"
17 #include "eap_config.h"
18 #include "eap_fast_pac.h"
20 #ifdef EAP_FAST_DYNAMIC
21 #include "eap_fast_pac.c"
22 #endif /* EAP_FAST_DYNAMIC */
25 * - test session resumption and enable it if it interoperates
26 * - password change (pending mschapv2 packet; replay decrypted packet)
30 static void eap_fast_deinit(struct eap_sm *sm, void *priv);
33 struct eap_fast_data {
34 struct eap_ssl_data ssl;
38 const struct eap_method *phase2_method;
42 struct eap_method_type phase2_type;
43 struct eap_method_type *phase2_types;
44 size_t num_phase2_types;
45 int resuming; /* starting a resumed session */
46 struct eap_fast_key_block_provisioning *key_block_p;
47 #define EAP_FAST_PROV_UNAUTH 1
48 #define EAP_FAST_PROV_AUTH 2
49 int provisioning_allowed; /* Allowed PAC provisioning modes */
50 int provisioning; /* doing PAC provisioning (not the normal auth) */
51 int anon_provisioning; /* doing anonymous (unauthenticated)
53 int session_ticket_used;
55 u8 key_data[EAP_FAST_KEY_LEN];
58 u8 emsk[EAP_EMSK_LEN];
61 struct eap_fast_pac *pac;
62 struct eap_fast_pac *current_pac;
63 size_t max_pac_list_len;
64 int use_pac_binary_format;
66 u8 simck[EAP_FAST_SIMCK_LEN];
69 struct wpabuf *pending_phase2_req;
70 struct wpabuf *pending_resp;
74 static int eap_fast_session_ticket_cb(void *ctx, const u8 *ticket, size_t len,
75 const u8 *client_random,
76 const u8 *server_random,
79 struct eap_fast_data *data = ctx;
81 wpa_printf(MSG_DEBUG, "EAP-FAST: SessionTicket callback");
83 if (client_random == NULL || server_random == NULL ||
84 master_secret == NULL) {
85 wpa_printf(MSG_DEBUG, "EAP-FAST: SessionTicket failed - fall "
86 "back to full TLS handshake");
87 data->session_ticket_used = 0;
88 if (data->provisioning_allowed) {
89 wpa_printf(MSG_DEBUG, "EAP-FAST: Try to provision a "
91 data->provisioning = 1;
92 data->current_pac = NULL;
97 wpa_hexdump(MSG_DEBUG, "EAP-FAST: SessionTicket", ticket, len);
99 if (data->current_pac == NULL) {
100 wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC-Key available for "
101 "using SessionTicket");
102 data->session_ticket_used = 0;
106 eap_fast_derive_master_secret(data->current_pac->pac_key,
107 server_random, client_random,
110 data->session_ticket_used = 1;
116 static void eap_fast_parse_phase1(struct eap_fast_data *data,
121 pos = os_strstr(phase1, "fast_provisioning=");
123 data->provisioning_allowed = atoi(pos + 18);
124 wpa_printf(MSG_DEBUG, "EAP-FAST: Automatic PAC provisioning "
125 "mode: %d", data->provisioning_allowed);
128 pos = os_strstr(phase1, "fast_max_pac_list_len=");
130 data->max_pac_list_len = atoi(pos + 22);
131 if (data->max_pac_list_len == 0)
132 data->max_pac_list_len = 1;
133 wpa_printf(MSG_DEBUG, "EAP-FAST: Maximum PAC list length: %lu",
134 (unsigned long) data->max_pac_list_len);
137 pos = os_strstr(phase1, "fast_pac_format=binary");
139 data->use_pac_binary_format = 1;
140 wpa_printf(MSG_DEBUG, "EAP-FAST: Using binary format for PAC "
146 static void * eap_fast_init(struct eap_sm *sm)
148 struct eap_fast_data *data;
149 struct eap_peer_config *config = eap_get_config(sm);
154 data = os_zalloc(sizeof(*data));
157 data->fast_version = EAP_FAST_VERSION;
158 data->max_pac_list_len = 10;
161 eap_fast_parse_phase1(data, config->phase1);
163 if (eap_peer_select_phase2_methods(config, "auth=",
165 &data->num_phase2_types) < 0) {
166 eap_fast_deinit(sm, data);
170 data->phase2_type.vendor = EAP_VENDOR_IETF;
171 data->phase2_type.method = EAP_TYPE_NONE;
173 if (eap_peer_tls_ssl_init(sm, &data->ssl, config, EAP_TYPE_FAST)) {
174 wpa_printf(MSG_INFO, "EAP-FAST: Failed to initialize SSL.");
175 eap_fast_deinit(sm, data);
179 if (tls_connection_set_session_ticket_cb(sm->ssl_ctx, data->ssl.conn,
180 eap_fast_session_ticket_cb,
182 wpa_printf(MSG_INFO, "EAP-FAST: Failed to set SessionTicket "
184 eap_fast_deinit(sm, data);
189 * The local RADIUS server in a Cisco AP does not seem to like empty
190 * fragments before data, so disable that workaround for CBC.
191 * TODO: consider making this configurable
193 if (tls_connection_enable_workaround(sm->ssl_ctx, data->ssl.conn)) {
194 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to enable TLS "
198 if (!config->pac_file) {
199 wpa_printf(MSG_INFO, "EAP-FAST: No PAC file configured");
200 eap_fast_deinit(sm, data);
204 if (data->use_pac_binary_format &&
205 eap_fast_load_pac_bin(sm, &data->pac, config->pac_file) < 0) {
206 wpa_printf(MSG_INFO, "EAP-FAST: Failed to load PAC file");
207 eap_fast_deinit(sm, data);
211 if (!data->use_pac_binary_format &&
212 eap_fast_load_pac(sm, &data->pac, config->pac_file) < 0) {
213 wpa_printf(MSG_INFO, "EAP-FAST: Failed to load PAC file");
214 eap_fast_deinit(sm, data);
217 eap_fast_pac_list_truncate(data->pac, data->max_pac_list_len);
219 if (data->pac == NULL && !data->provisioning_allowed) {
220 wpa_printf(MSG_INFO, "EAP-FAST: No PAC configured and "
221 "provisioning disabled");
222 eap_fast_deinit(sm, data);
230 static void eap_fast_deinit(struct eap_sm *sm, void *priv)
232 struct eap_fast_data *data = priv;
233 struct eap_fast_pac *pac, *prev;
237 if (data->phase2_priv && data->phase2_method)
238 data->phase2_method->deinit(sm, data->phase2_priv);
239 os_free(data->phase2_types);
240 os_free(data->key_block_p);
241 eap_peer_tls_ssl_deinit(sm, &data->ssl);
248 eap_fast_free_pac(prev);
250 os_memset(data->key_data, 0, EAP_FAST_KEY_LEN);
251 os_memset(data->emsk, 0, EAP_EMSK_LEN);
252 os_free(data->session_id);
253 wpabuf_clear_free(data->pending_phase2_req);
254 wpabuf_clear_free(data->pending_resp);
259 static int eap_fast_derive_msk(struct eap_fast_data *data)
261 if (eap_fast_derive_eap_msk(data->simck, data->key_data) < 0 ||
262 eap_fast_derive_eap_emsk(data->simck, data->emsk) < 0)
269 static int eap_fast_derive_key_auth(struct eap_sm *sm,
270 struct eap_fast_data *data)
274 /* RFC 4851, Section 5.1:
275 * Extra key material after TLS key_block: session_key_seed[40]
278 sks = eap_fast_derive_key(sm->ssl_ctx, data->ssl.conn,
281 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive "
287 * RFC 4851, Section 5.2:
288 * S-IMCK[0] = session_key_seed
290 wpa_hexdump_key(MSG_DEBUG,
291 "EAP-FAST: session_key_seed (SKS = S-IMCK[0])",
292 sks, EAP_FAST_SKS_LEN);
294 os_memcpy(data->simck, sks, EAP_FAST_SIMCK_LEN);
300 static int eap_fast_derive_key_provisioning(struct eap_sm *sm,
301 struct eap_fast_data *data)
303 os_free(data->key_block_p);
304 data->key_block_p = (struct eap_fast_key_block_provisioning *)
305 eap_fast_derive_key(sm->ssl_ctx, data->ssl.conn,
306 sizeof(*data->key_block_p));
307 if (data->key_block_p == NULL) {
308 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive key block");
312 * RFC 4851, Section 5.2:
313 * S-IMCK[0] = session_key_seed
315 wpa_hexdump_key(MSG_DEBUG,
316 "EAP-FAST: session_key_seed (SKS = S-IMCK[0])",
317 data->key_block_p->session_key_seed,
318 sizeof(data->key_block_p->session_key_seed));
320 os_memcpy(data->simck, data->key_block_p->session_key_seed,
322 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: server_challenge",
323 data->key_block_p->server_challenge,
324 sizeof(data->key_block_p->server_challenge));
325 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: client_challenge",
326 data->key_block_p->client_challenge,
327 sizeof(data->key_block_p->client_challenge));
332 static int eap_fast_derive_keys(struct eap_sm *sm, struct eap_fast_data *data)
336 if (data->anon_provisioning)
337 res = eap_fast_derive_key_provisioning(sm, data);
339 res = eap_fast_derive_key_auth(sm, data);
344 static int eap_fast_init_phase2_method(struct eap_sm *sm,
345 struct eap_fast_data *data)
347 data->phase2_method =
348 eap_peer_get_eap_method(data->phase2_type.vendor,
349 data->phase2_type.method);
350 if (data->phase2_method == NULL)
353 if (data->key_block_p) {
354 sm->auth_challenge = data->key_block_p->server_challenge;
355 sm->peer_challenge = data->key_block_p->client_challenge;
358 data->phase2_priv = data->phase2_method->init(sm);
360 sm->auth_challenge = NULL;
361 sm->peer_challenge = NULL;
363 return data->phase2_priv == NULL ? -1 : 0;
367 static int eap_fast_select_phase2_method(struct eap_fast_data *data, u8 type)
371 /* TODO: TNC with anonymous provisioning; need to require both
372 * completed MSCHAPv2 and TNC */
374 if (data->anon_provisioning && type != EAP_TYPE_MSCHAPV2) {
375 wpa_printf(MSG_INFO, "EAP-FAST: Only EAP-MSCHAPv2 is allowed "
376 "during unauthenticated provisioning; reject phase2"
382 if (type == EAP_TYPE_TNC) {
383 data->phase2_type.vendor = EAP_VENDOR_IETF;
384 data->phase2_type.method = EAP_TYPE_TNC;
385 wpa_printf(MSG_DEBUG, "EAP-FAST: Selected Phase 2 EAP "
386 "vendor %d method %d for TNC",
387 data->phase2_type.vendor,
388 data->phase2_type.method);
393 for (i = 0; i < data->num_phase2_types; i++) {
394 if (data->phase2_types[i].vendor != EAP_VENDOR_IETF ||
395 data->phase2_types[i].method != type)
398 data->phase2_type.vendor = data->phase2_types[i].vendor;
399 data->phase2_type.method = data->phase2_types[i].method;
400 wpa_printf(MSG_DEBUG, "EAP-FAST: Selected Phase 2 EAP "
401 "vendor %d method %d",
402 data->phase2_type.vendor,
403 data->phase2_type.method);
407 if (type != data->phase2_type.method || type == EAP_TYPE_NONE)
414 static int eap_fast_phase2_request(struct eap_sm *sm,
415 struct eap_fast_data *data,
416 struct eap_method_ret *ret,
418 struct wpabuf **resp)
420 size_t len = be_to_host16(hdr->length);
422 struct eap_method_ret iret;
423 struct eap_peer_config *config = eap_get_config(sm);
426 if (len <= sizeof(struct eap_hdr)) {
427 wpa_printf(MSG_INFO, "EAP-FAST: too short "
428 "Phase 2 request (len=%lu)", (unsigned long) len);
431 pos = (u8 *) (hdr + 1);
432 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 Request: type=%d", *pos);
433 if (*pos == EAP_TYPE_IDENTITY) {
434 *resp = eap_sm_buildIdentity(sm, hdr->identifier, 1);
438 if (data->phase2_priv && data->phase2_method &&
439 *pos != data->phase2_type.method) {
440 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 EAP sequence - "
441 "deinitialize previous method");
442 data->phase2_method->deinit(sm, data->phase2_priv);
443 data->phase2_method = NULL;
444 data->phase2_priv = NULL;
445 data->phase2_type.vendor = EAP_VENDOR_IETF;
446 data->phase2_type.method = EAP_TYPE_NONE;
449 if (data->phase2_type.vendor == EAP_VENDOR_IETF &&
450 data->phase2_type.method == EAP_TYPE_NONE &&
451 eap_fast_select_phase2_method(data, *pos) < 0) {
452 if (eap_peer_tls_phase2_nak(data->phase2_types,
453 data->num_phase2_types,
459 if ((data->phase2_priv == NULL &&
460 eap_fast_init_phase2_method(sm, data) < 0) ||
461 data->phase2_method == NULL) {
462 wpa_printf(MSG_INFO, "EAP-FAST: Failed to initialize "
463 "Phase 2 EAP method %d", *pos);
464 ret->methodState = METHOD_DONE;
465 ret->decision = DECISION_FAIL;
469 os_memset(&iret, 0, sizeof(iret));
470 wpabuf_set(&msg, hdr, len);
471 *resp = data->phase2_method->process(sm, data->phase2_priv, &iret,
474 (iret.methodState == METHOD_DONE &&
475 iret.decision == DECISION_FAIL)) {
476 ret->methodState = METHOD_DONE;
477 ret->decision = DECISION_FAIL;
478 } else if ((iret.methodState == METHOD_DONE ||
479 iret.methodState == METHOD_MAY_CONT) &&
480 (iret.decision == DECISION_UNCOND_SUCC ||
481 iret.decision == DECISION_COND_SUCC)) {
482 data->phase2_success = 1;
485 if (*resp == NULL && config &&
486 (config->pending_req_identity || config->pending_req_password ||
487 config->pending_req_otp || config->pending_req_new_password ||
488 config->pending_req_sim)) {
489 wpabuf_clear_free(data->pending_phase2_req);
490 data->pending_phase2_req = wpabuf_alloc_copy(hdr, len);
491 } else if (*resp == NULL)
498 static struct wpabuf * eap_fast_tlv_nak(int vendor_id, int tlv_type)
501 struct eap_tlv_nak_tlv *nak;
502 buf = wpabuf_alloc(sizeof(*nak));
505 nak = wpabuf_put(buf, sizeof(*nak));
506 nak->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY | EAP_TLV_NAK_TLV);
507 nak->length = host_to_be16(6);
508 nak->vendor_id = host_to_be32(vendor_id);
509 nak->nak_type = host_to_be16(tlv_type);
514 static struct wpabuf * eap_fast_tlv_result(int status, int intermediate)
517 struct eap_tlv_intermediate_result_tlv *result;
518 buf = wpabuf_alloc(sizeof(*result));
521 wpa_printf(MSG_DEBUG, "EAP-FAST: Add %sResult TLV(status=%d)",
522 intermediate ? "Intermediate " : "", status);
523 result = wpabuf_put(buf, sizeof(*result));
524 result->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
526 EAP_TLV_INTERMEDIATE_RESULT_TLV :
527 EAP_TLV_RESULT_TLV));
528 result->length = host_to_be16(2);
529 result->status = host_to_be16(status);
534 static struct wpabuf * eap_fast_tlv_pac_ack(void)
537 struct eap_tlv_result_tlv *res;
538 struct eap_tlv_pac_ack_tlv *ack;
540 buf = wpabuf_alloc(sizeof(*res) + sizeof(*ack));
544 wpa_printf(MSG_DEBUG, "EAP-FAST: Add PAC TLV (ack)");
545 ack = wpabuf_put(buf, sizeof(*ack));
546 ack->tlv_type = host_to_be16(EAP_TLV_PAC_TLV |
547 EAP_TLV_TYPE_MANDATORY);
548 ack->length = host_to_be16(sizeof(*ack) - sizeof(struct eap_tlv_hdr));
549 ack->pac_type = host_to_be16(PAC_TYPE_PAC_ACKNOWLEDGEMENT);
550 ack->pac_len = host_to_be16(2);
551 ack->result = host_to_be16(EAP_TLV_RESULT_SUCCESS);
557 static struct wpabuf * eap_fast_process_eap_payload_tlv(
558 struct eap_sm *sm, struct eap_fast_data *data,
559 struct eap_method_ret *ret,
560 u8 *eap_payload_tlv, size_t eap_payload_tlv_len)
563 struct wpabuf *resp = NULL;
565 if (eap_payload_tlv_len < sizeof(*hdr)) {
566 wpa_printf(MSG_DEBUG, "EAP-FAST: too short EAP "
567 "Payload TLV (len=%lu)",
568 (unsigned long) eap_payload_tlv_len);
572 hdr = (struct eap_hdr *) eap_payload_tlv;
573 if (be_to_host16(hdr->length) > eap_payload_tlv_len) {
574 wpa_printf(MSG_DEBUG, "EAP-FAST: EAP packet overflow in "
579 if (hdr->code != EAP_CODE_REQUEST) {
580 wpa_printf(MSG_INFO, "EAP-FAST: Unexpected code=%d in "
581 "Phase 2 EAP header", hdr->code);
585 if (eap_fast_phase2_request(sm, data, ret, hdr, &resp)) {
586 wpa_printf(MSG_INFO, "EAP-FAST: Phase2 Request processing "
591 return eap_fast_tlv_eap_payload(resp);
595 static int eap_fast_validate_crypto_binding(
596 struct eap_tlv_crypto_binding_tlv *_bind)
598 wpa_printf(MSG_DEBUG, "EAP-FAST: Crypto-Binding TLV: Version %d "
599 "Received Version %d SubType %d",
600 _bind->version, _bind->received_version, _bind->subtype);
601 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE",
602 _bind->nonce, sizeof(_bind->nonce));
603 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC",
604 _bind->compound_mac, sizeof(_bind->compound_mac));
606 if (_bind->version != EAP_FAST_VERSION ||
607 _bind->received_version != EAP_FAST_VERSION ||
608 _bind->subtype != EAP_TLV_CRYPTO_BINDING_SUBTYPE_REQUEST) {
609 wpa_printf(MSG_INFO, "EAP-FAST: Invalid version/subtype in "
610 "Crypto-Binding TLV: Version %d "
611 "Received Version %d SubType %d",
612 _bind->version, _bind->received_version,
621 static void eap_fast_write_crypto_binding(
622 struct eap_tlv_crypto_binding_tlv *rbind,
623 struct eap_tlv_crypto_binding_tlv *_bind, const u8 *cmk)
625 rbind->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
626 EAP_TLV_CRYPTO_BINDING_TLV);
627 rbind->length = host_to_be16(sizeof(*rbind) -
628 sizeof(struct eap_tlv_hdr));
629 rbind->version = EAP_FAST_VERSION;
630 rbind->received_version = _bind->version;
631 rbind->subtype = EAP_TLV_CRYPTO_BINDING_SUBTYPE_RESPONSE;
632 os_memcpy(rbind->nonce, _bind->nonce, sizeof(_bind->nonce));
633 inc_byte_array(rbind->nonce, sizeof(rbind->nonce));
634 hmac_sha1(cmk, EAP_FAST_CMK_LEN, (u8 *) rbind, sizeof(*rbind),
635 rbind->compound_mac);
637 wpa_printf(MSG_DEBUG, "EAP-FAST: Reply Crypto-Binding TLV: Version %d "
638 "Received Version %d SubType %d",
639 rbind->version, rbind->received_version, rbind->subtype);
640 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE",
641 rbind->nonce, sizeof(rbind->nonce));
642 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC",
643 rbind->compound_mac, sizeof(rbind->compound_mac));
647 static int eap_fast_get_phase2_key(struct eap_sm *sm,
648 struct eap_fast_data *data,
649 u8 *isk, size_t isk_len)
654 os_memset(isk, 0, isk_len);
656 if (data->phase2_method == NULL || data->phase2_priv == NULL) {
657 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 method not "
662 if (data->phase2_method->isKeyAvailable == NULL ||
663 data->phase2_method->getKey == NULL)
666 if (!data->phase2_method->isKeyAvailable(sm, data->phase2_priv) ||
667 (key = data->phase2_method->getKey(sm, data->phase2_priv,
668 &key_len)) == NULL) {
669 wpa_printf(MSG_DEBUG, "EAP-FAST: Could not get key material "
674 if (key_len > isk_len)
677 data->phase2_method->vendor == EAP_VENDOR_IETF &&
678 data->phase2_method->method == EAP_TYPE_MSCHAPV2) {
680 * EAP-FAST uses reverse order for MS-MPPE keys when deriving
681 * MSK from EAP-MSCHAPv2. Swap the keys here to get the correct
682 * ISK for EAP-FAST cryptobinding.
684 os_memcpy(isk, key + 16, 16);
685 os_memcpy(isk + 16, key, 16);
687 os_memcpy(isk, key, key_len);
694 static int eap_fast_get_cmk(struct eap_sm *sm, struct eap_fast_data *data,
697 u8 isk[32], imck[60];
699 wpa_printf(MSG_DEBUG, "EAP-FAST: Determining CMK[%d] for Compound MIC "
700 "calculation", data->simck_idx + 1);
703 * RFC 4851, Section 5.2:
704 * IMCK[j] = T-PRF(S-IMCK[j-1], "Inner Methods Compound Keys",
706 * S-IMCK[j] = first 40 octets of IMCK[j]
707 * CMK[j] = last 20 octets of IMCK[j]
710 if (eap_fast_get_phase2_key(sm, data, isk, sizeof(isk)) < 0)
712 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: ISK[j]", isk, sizeof(isk));
713 if (sha1_t_prf(data->simck, EAP_FAST_SIMCK_LEN,
714 "Inner Methods Compound Keys",
715 isk, sizeof(isk), imck, sizeof(imck)) < 0)
718 os_memcpy(data->simck, imck, EAP_FAST_SIMCK_LEN);
719 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: S-IMCK[j]",
720 data->simck, EAP_FAST_SIMCK_LEN);
721 os_memcpy(cmk, imck + EAP_FAST_SIMCK_LEN, EAP_FAST_CMK_LEN);
722 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: CMK[j]",
723 cmk, EAP_FAST_CMK_LEN);
729 static u8 * eap_fast_write_pac_request(u8 *pos, u16 pac_type)
731 struct eap_tlv_hdr *pac;
732 struct eap_tlv_request_action_tlv *act;
733 struct eap_tlv_pac_type_tlv *type;
735 act = (struct eap_tlv_request_action_tlv *) pos;
736 act->tlv_type = host_to_be16(EAP_TLV_REQUEST_ACTION_TLV);
737 act->length = host_to_be16(2);
738 act->action = host_to_be16(EAP_TLV_ACTION_PROCESS_TLV);
740 pac = (struct eap_tlv_hdr *) (act + 1);
741 pac->tlv_type = host_to_be16(EAP_TLV_PAC_TLV);
742 pac->length = host_to_be16(sizeof(*type));
744 type = (struct eap_tlv_pac_type_tlv *) (pac + 1);
745 type->tlv_type = host_to_be16(PAC_TYPE_PAC_TYPE);
746 type->length = host_to_be16(2);
747 type->pac_type = host_to_be16(pac_type);
749 return (u8 *) (type + 1);
753 static struct wpabuf * eap_fast_process_crypto_binding(
754 struct eap_sm *sm, struct eap_fast_data *data,
755 struct eap_method_ret *ret,
756 struct eap_tlv_crypto_binding_tlv *_bind, size_t bind_len)
760 u8 cmk[EAP_FAST_CMK_LEN], cmac[SHA1_MAC_LEN];
764 if (eap_fast_validate_crypto_binding(_bind) < 0)
767 if (eap_fast_get_cmk(sm, data, cmk) < 0)
770 /* Validate received Compound MAC */
771 os_memcpy(cmac, _bind->compound_mac, sizeof(cmac));
772 os_memset(_bind->compound_mac, 0, sizeof(cmac));
773 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Crypto-Binding TLV for Compound "
774 "MAC calculation", (u8 *) _bind, bind_len);
775 hmac_sha1(cmk, EAP_FAST_CMK_LEN, (u8 *) _bind, bind_len,
776 _bind->compound_mac);
777 res = os_memcmp_const(cmac, _bind->compound_mac, sizeof(cmac));
778 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Received Compound MAC",
780 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Calculated Compound MAC",
781 _bind->compound_mac, sizeof(cmac));
783 wpa_printf(MSG_INFO, "EAP-FAST: Compound MAC did not match");
784 os_memcpy(_bind->compound_mac, cmac, sizeof(cmac));
789 * Compound MAC was valid, so authentication succeeded. Reply with
790 * crypto binding to allow server to complete authentication.
793 len = sizeof(struct eap_tlv_crypto_binding_tlv);
794 resp = wpabuf_alloc(len);
798 if (!data->anon_provisioning && data->phase2_success &&
799 eap_fast_derive_msk(data) < 0) {
800 wpa_printf(MSG_INFO, "EAP-FAST: Failed to generate MSK");
801 ret->methodState = METHOD_DONE;
802 ret->decision = DECISION_FAIL;
803 data->phase2_success = 0;
804 wpabuf_clear_free(resp);
808 if (!data->anon_provisioning && data->phase2_success) {
809 os_free(data->session_id);
810 data->session_id = eap_peer_tls_derive_session_id(
811 sm, &data->ssl, EAP_TYPE_FAST, &data->id_len);
812 if (data->session_id) {
813 wpa_hexdump(MSG_DEBUG, "EAP-FAST: Derived Session-Id",
814 data->session_id, data->id_len);
816 wpa_printf(MSG_ERROR, "EAP-FAST: Failed to derive "
818 wpabuf_clear_free(resp);
823 pos = wpabuf_put(resp, sizeof(struct eap_tlv_crypto_binding_tlv));
824 eap_fast_write_crypto_binding((struct eap_tlv_crypto_binding_tlv *)
831 static void eap_fast_parse_pac_tlv(struct eap_fast_pac *entry, int type,
832 u8 *pos, size_t len, int *pac_key_found)
834 switch (type & 0x7fff) {
835 case PAC_TYPE_PAC_KEY:
836 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: PAC-Key", pos, len);
837 if (len != EAP_FAST_PAC_KEY_LEN) {
838 wpa_printf(MSG_DEBUG, "EAP-FAST: Invalid PAC-Key "
839 "length %lu", (unsigned long) len);
843 os_memcpy(entry->pac_key, pos, len);
845 case PAC_TYPE_PAC_OPAQUE:
846 wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Opaque", pos, len);
847 entry->pac_opaque = pos;
848 entry->pac_opaque_len = len;
850 case PAC_TYPE_PAC_INFO:
851 wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Info", pos, len);
852 entry->pac_info = pos;
853 entry->pac_info_len = len;
856 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignored unknown PAC type %d",
863 static int eap_fast_process_pac_tlv(struct eap_fast_pac *entry,
864 u8 *pac, size_t pac_len)
866 struct pac_tlv_hdr *hdr;
869 int type, pac_key_found = 0;
874 while (left > sizeof(*hdr)) {
875 hdr = (struct pac_tlv_hdr *) pos;
876 type = be_to_host16(hdr->type);
877 len = be_to_host16(hdr->len);
879 left -= sizeof(*hdr);
881 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV overrun "
882 "(type=%d len=%lu left=%lu)",
883 type, (unsigned long) len,
884 (unsigned long) left);
888 eap_fast_parse_pac_tlv(entry, type, pos, len, &pac_key_found);
894 if (!pac_key_found || !entry->pac_opaque || !entry->pac_info) {
895 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV does not include "
896 "all the required fields");
904 static int eap_fast_parse_pac_info(struct eap_fast_pac *entry, int type,
911 switch (type & 0x7fff) {
912 case PAC_TYPE_CRED_LIFETIME:
914 wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Info - "
915 "Invalid CRED_LIFETIME length - ignored",
921 * This is not currently saved separately in PAC files since
922 * the server can automatically initiate PAC update when
923 * needed. Anyway, the information is available from PAC-Info
924 * dump if it is needed for something in the future.
926 lifetime = WPA_GET_BE32(pos);
928 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info - CRED_LIFETIME %d "
930 lifetime, (lifetime - (u32) now.sec) / 86400);
933 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - A-ID",
936 entry->a_id_len = len;
939 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - I-ID",
942 entry->i_id_len = len;
944 case PAC_TYPE_A_ID_INFO:
945 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - A-ID-Info",
947 entry->a_id_info = pos;
948 entry->a_id_info_len = len;
950 case PAC_TYPE_PAC_TYPE:
951 /* RFC 5422, Section 4.2.6 - PAC-Type TLV */
953 wpa_printf(MSG_INFO, "EAP-FAST: Invalid PAC-Type "
954 "length %lu (expected 2)",
955 (unsigned long) len);
956 wpa_hexdump_ascii(MSG_DEBUG,
957 "EAP-FAST: PAC-Info - PAC-Type",
961 pac_type = WPA_GET_BE16(pos);
962 if (pac_type != PAC_TYPE_TUNNEL_PAC &&
963 pac_type != PAC_TYPE_USER_AUTHORIZATION &&
964 pac_type != PAC_TYPE_MACHINE_AUTHENTICATION) {
965 wpa_printf(MSG_INFO, "EAP-FAST: Unsupported PAC Type "
970 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info - PAC-Type %d",
972 entry->pac_type = pac_type;
975 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignored unknown PAC-Info "
984 static int eap_fast_process_pac_info(struct eap_fast_pac *entry)
986 struct pac_tlv_hdr *hdr;
991 /* RFC 5422, Section 4.2.4 */
993 /* PAC-Type defaults to Tunnel PAC (Type 1) */
994 entry->pac_type = PAC_TYPE_TUNNEL_PAC;
996 pos = entry->pac_info;
997 left = entry->pac_info_len;
998 while (left > sizeof(*hdr)) {
999 hdr = (struct pac_tlv_hdr *) pos;
1000 type = be_to_host16(hdr->type);
1001 len = be_to_host16(hdr->len);
1002 pos += sizeof(*hdr);
1003 left -= sizeof(*hdr);
1005 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info overrun "
1006 "(type=%d len=%lu left=%lu)",
1007 type, (unsigned long) len,
1008 (unsigned long) left);
1012 if (eap_fast_parse_pac_info(entry, type, pos, len) < 0)
1019 if (entry->a_id == NULL || entry->a_id_info == NULL) {
1020 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info does not include "
1021 "all the required fields");
1029 static struct wpabuf * eap_fast_process_pac(struct eap_sm *sm,
1030 struct eap_fast_data *data,
1031 struct eap_method_ret *ret,
1032 u8 *pac, size_t pac_len)
1034 struct eap_peer_config *config = eap_get_config(sm);
1035 struct eap_fast_pac entry;
1037 os_memset(&entry, 0, sizeof(entry));
1038 if (eap_fast_process_pac_tlv(&entry, pac, pac_len) ||
1039 eap_fast_process_pac_info(&entry))
1042 eap_fast_add_pac(&data->pac, &data->current_pac, &entry);
1043 eap_fast_pac_list_truncate(data->pac, data->max_pac_list_len);
1044 if (data->use_pac_binary_format)
1045 eap_fast_save_pac_bin(sm, data->pac, config->pac_file);
1047 eap_fast_save_pac(sm, data->pac, config->pac_file);
1049 if (data->provisioning) {
1050 if (data->anon_provisioning) {
1052 * Unauthenticated provisioning does not provide keying
1053 * material and must end with an EAP-Failure.
1054 * Authentication will be done separately after this.
1057 ret->decision = DECISION_FAIL;
1060 * Server may or may not allow authenticated
1061 * provisioning also for key generation.
1063 ret->decision = DECISION_COND_SUCC;
1065 wpa_printf(MSG_DEBUG, "EAP-FAST: Send PAC-Acknowledgement TLV "
1066 "- Provisioning completed successfully");
1067 sm->expected_failure = 1;
1070 * This is PAC refreshing, i.e., normal authentication that is
1071 * expected to be completed with an EAP-Success. However,
1072 * RFC 5422, Section 3.5 allows EAP-Failure to be sent even
1073 * after protected success exchange in case of EAP-Fast
1074 * provisioning, so we better use DECISION_COND_SUCC here
1075 * instead of DECISION_UNCOND_SUCC.
1077 wpa_printf(MSG_DEBUG, "EAP-FAST: Send PAC-Acknowledgement TLV "
1078 "- PAC refreshing completed successfully");
1079 ret->decision = DECISION_COND_SUCC;
1081 ret->methodState = METHOD_DONE;
1082 return eap_fast_tlv_pac_ack();
1086 static int eap_fast_parse_decrypted(struct wpabuf *decrypted,
1087 struct eap_fast_tlv_parse *tlv,
1088 struct wpabuf **resp)
1090 int mandatory, tlv_type, res;
1094 os_memset(tlv, 0, sizeof(*tlv));
1096 /* Parse TLVs from the decrypted Phase 2 data */
1097 pos = wpabuf_mhead(decrypted);
1098 end = pos + wpabuf_len(decrypted);
1099 while (end - pos > 4) {
1100 mandatory = pos[0] & 0x80;
1101 tlv_type = WPA_GET_BE16(pos) & 0x3fff;
1103 len = WPA_GET_BE16(pos);
1105 if (len > (size_t) (end - pos)) {
1106 wpa_printf(MSG_INFO, "EAP-FAST: TLV overflow");
1109 wpa_printf(MSG_DEBUG, "EAP-FAST: Received Phase 2: "
1110 "TLV type %d length %u%s",
1111 tlv_type, (unsigned int) len,
1112 mandatory ? " (mandatory)" : "");
1114 res = eap_fast_parse_tlv(tlv, tlv_type, pos, len);
1119 wpa_printf(MSG_DEBUG, "EAP-FAST: Nak unknown "
1120 "mandatory TLV type %d", tlv_type);
1121 *resp = eap_fast_tlv_nak(0, tlv_type);
1124 wpa_printf(MSG_DEBUG, "EAP-FAST: ignored "
1125 "unknown optional TLV type %d",
1137 static int eap_fast_encrypt_response(struct eap_sm *sm,
1138 struct eap_fast_data *data,
1139 struct wpabuf *resp,
1140 u8 identifier, struct wpabuf **out_data)
1145 wpa_hexdump_buf(MSG_DEBUG, "EAP-FAST: Encrypting Phase 2 data",
1147 if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_FAST,
1148 data->fast_version, identifier,
1150 wpa_printf(MSG_INFO, "EAP-FAST: Failed to encrypt a Phase 2 "
1153 wpabuf_clear_free(resp);
1159 static struct wpabuf * eap_fast_pac_request(void)
1164 tmp = wpabuf_alloc(sizeof(struct eap_tlv_hdr) +
1165 sizeof(struct eap_tlv_request_action_tlv) +
1166 sizeof(struct eap_tlv_pac_type_tlv));
1170 pos = wpabuf_put(tmp, 0);
1171 pos2 = eap_fast_write_pac_request(pos, PAC_TYPE_TUNNEL_PAC);
1172 wpabuf_put(tmp, pos2 - pos);
1177 static int eap_fast_process_decrypted(struct eap_sm *sm,
1178 struct eap_fast_data *data,
1179 struct eap_method_ret *ret,
1181 struct wpabuf *decrypted,
1182 struct wpabuf **out_data)
1184 struct wpabuf *resp = NULL, *tmp;
1185 struct eap_fast_tlv_parse tlv;
1188 if (eap_fast_parse_decrypted(decrypted, &tlv, &resp) < 0)
1191 return eap_fast_encrypt_response(sm, data, resp,
1192 identifier, out_data);
1194 if (tlv.result == EAP_TLV_RESULT_FAILURE) {
1195 resp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 0);
1196 return eap_fast_encrypt_response(sm, data, resp,
1197 identifier, out_data);
1200 if (tlv.iresult == EAP_TLV_RESULT_FAILURE) {
1201 resp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 1);
1202 return eap_fast_encrypt_response(sm, data, resp,
1203 identifier, out_data);
1206 if (tlv.crypto_binding) {
1207 tmp = eap_fast_process_crypto_binding(sm, data, ret,
1209 tlv.crypto_binding_len);
1213 resp = wpabuf_concat(resp, tmp);
1216 if (tlv.iresult == EAP_TLV_RESULT_SUCCESS) {
1217 tmp = eap_fast_tlv_result(failed ? EAP_TLV_RESULT_FAILURE :
1218 EAP_TLV_RESULT_SUCCESS, 1);
1219 resp = wpabuf_concat(resp, tmp);
1222 if (tlv.eap_payload_tlv) {
1223 tmp = eap_fast_process_eap_payload_tlv(
1224 sm, data, ret, tlv.eap_payload_tlv,
1225 tlv.eap_payload_tlv_len);
1226 resp = wpabuf_concat(resp, tmp);
1229 if (tlv.pac && tlv.result != EAP_TLV_RESULT_SUCCESS) {
1230 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV without Result TLV "
1231 "acknowledging success");
1233 } else if (tlv.pac && tlv.result == EAP_TLV_RESULT_SUCCESS) {
1234 tmp = eap_fast_process_pac(sm, data, ret, tlv.pac,
1236 resp = wpabuf_concat(resp, tmp);
1239 if (data->current_pac == NULL && data->provisioning &&
1240 !data->anon_provisioning && !tlv.pac &&
1241 (tlv.iresult == EAP_TLV_RESULT_SUCCESS ||
1242 tlv.result == EAP_TLV_RESULT_SUCCESS)) {
1244 * Need to request Tunnel PAC when using authenticated
1247 wpa_printf(MSG_DEBUG, "EAP-FAST: Request Tunnel PAC");
1248 tmp = eap_fast_pac_request();
1249 resp = wpabuf_concat(resp, tmp);
1252 if (tlv.result == EAP_TLV_RESULT_SUCCESS && !failed) {
1253 tmp = eap_fast_tlv_result(EAP_TLV_RESULT_SUCCESS, 0);
1254 resp = wpabuf_concat(tmp, resp);
1255 } else if (failed) {
1256 tmp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 0);
1257 resp = wpabuf_concat(tmp, resp);
1260 if (resp && tlv.result == EAP_TLV_RESULT_SUCCESS && !failed &&
1261 tlv.crypto_binding && data->phase2_success) {
1262 if (data->anon_provisioning) {
1263 wpa_printf(MSG_DEBUG, "EAP-FAST: Unauthenticated "
1264 "provisioning completed successfully.");
1265 ret->methodState = METHOD_DONE;
1266 ret->decision = DECISION_FAIL;
1267 sm->expected_failure = 1;
1269 wpa_printf(MSG_DEBUG, "EAP-FAST: Authentication "
1270 "completed successfully.");
1271 if (data->provisioning)
1272 ret->methodState = METHOD_MAY_CONT;
1274 ret->methodState = METHOD_DONE;
1275 ret->decision = DECISION_UNCOND_SUCC;
1280 wpa_printf(MSG_DEBUG, "EAP-FAST: No recognized TLVs - send "
1281 "empty response packet");
1282 resp = wpabuf_alloc(1);
1285 return eap_fast_encrypt_response(sm, data, resp, identifier,
1290 static int eap_fast_decrypt(struct eap_sm *sm, struct eap_fast_data *data,
1291 struct eap_method_ret *ret, u8 identifier,
1292 const struct wpabuf *in_data,
1293 struct wpabuf **out_data)
1295 struct wpabuf *in_decrypted;
1298 wpa_printf(MSG_DEBUG, "EAP-FAST: Received %lu bytes encrypted data for"
1299 " Phase 2", (unsigned long) wpabuf_len(in_data));
1301 if (data->pending_phase2_req) {
1302 wpa_printf(MSG_DEBUG, "EAP-FAST: Pending Phase 2 request - "
1303 "skip decryption and use old data");
1304 /* Clear TLS reassembly state. */
1305 eap_peer_tls_reset_input(&data->ssl);
1307 in_decrypted = data->pending_phase2_req;
1308 data->pending_phase2_req = NULL;
1312 if (wpabuf_len(in_data) == 0) {
1313 /* Received TLS ACK - requesting more fragments */
1314 return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_FAST,
1316 identifier, NULL, out_data);
1319 res = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
1324 wpa_hexdump_buf(MSG_MSGDUMP, "EAP-FAST: Decrypted Phase 2 TLV(s)",
1327 if (wpabuf_len(in_decrypted) < 4) {
1328 wpa_printf(MSG_INFO, "EAP-FAST: Too short Phase 2 "
1329 "TLV frame (len=%lu)",
1330 (unsigned long) wpabuf_len(in_decrypted));
1331 wpabuf_clear_free(in_decrypted);
1335 res = eap_fast_process_decrypted(sm, data, ret, identifier,
1336 in_decrypted, out_data);
1338 wpabuf_clear_free(in_decrypted);
1344 static const u8 * eap_fast_get_a_id(const u8 *buf, size_t len, size_t *id_len)
1347 const struct pac_tlv_hdr *hdr;
1350 * Parse authority identity (A-ID) from the EAP-FAST/Start. This
1351 * supports both raw A-ID and one inside an A-ID TLV.
1355 if (len > sizeof(*hdr)) {
1357 hdr = (const struct pac_tlv_hdr *) buf;
1358 tlen = be_to_host16(hdr->len);
1359 if (be_to_host16(hdr->type) == PAC_TYPE_A_ID &&
1360 sizeof(*hdr) + tlen <= len) {
1361 wpa_printf(MSG_DEBUG, "EAP-FAST: A-ID was in TLV "
1363 a_id = (const u8 *) (hdr + 1);
1367 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: A-ID", a_id, *id_len);
1373 static void eap_fast_select_pac(struct eap_fast_data *data,
1374 const u8 *a_id, size_t a_id_len)
1376 data->current_pac = eap_fast_get_pac(data->pac, a_id, a_id_len,
1377 PAC_TYPE_TUNNEL_PAC);
1378 if (data->current_pac == NULL) {
1380 * Tunnel PAC was not available for this A-ID. Try to use
1381 * Machine Authentication PAC, if one is available.
1383 data->current_pac = eap_fast_get_pac(
1384 data->pac, a_id, a_id_len,
1385 PAC_TYPE_MACHINE_AUTHENTICATION);
1388 if (data->current_pac) {
1389 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC found for this A-ID "
1390 "(PAC-Type %d)", data->current_pac->pac_type);
1391 wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-FAST: A-ID-Info",
1392 data->current_pac->a_id_info,
1393 data->current_pac->a_id_info_len);
1398 static int eap_fast_use_pac_opaque(struct eap_sm *sm,
1399 struct eap_fast_data *data,
1400 struct eap_fast_pac *pac)
1403 size_t tlv_len, olen;
1404 struct eap_tlv_hdr *ehdr;
1406 olen = pac->pac_opaque_len;
1407 tlv_len = sizeof(*ehdr) + olen;
1408 tlv = os_malloc(tlv_len);
1410 ehdr = (struct eap_tlv_hdr *) tlv;
1411 ehdr->tlv_type = host_to_be16(PAC_TYPE_PAC_OPAQUE);
1412 ehdr->length = host_to_be16(olen);
1413 os_memcpy(ehdr + 1, pac->pac_opaque, olen);
1416 tls_connection_client_hello_ext(sm->ssl_ctx, data->ssl.conn,
1418 tlv, tlv_len) < 0) {
1419 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to add PAC-Opaque TLS "
1430 static int eap_fast_clear_pac_opaque_ext(struct eap_sm *sm,
1431 struct eap_fast_data *data)
1433 if (tls_connection_client_hello_ext(sm->ssl_ctx, data->ssl.conn,
1434 TLS_EXT_PAC_OPAQUE, NULL, 0) < 0) {
1435 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to remove PAC-Opaque "
1443 static int eap_fast_set_provisioning_ciphers(struct eap_sm *sm,
1444 struct eap_fast_data *data)
1449 if (data->provisioning_allowed & EAP_FAST_PROV_UNAUTH) {
1450 wpa_printf(MSG_DEBUG, "EAP-FAST: Enabling unauthenticated "
1451 "provisioning TLS cipher suites");
1452 ciphers[count++] = TLS_CIPHER_ANON_DH_AES128_SHA;
1455 if (data->provisioning_allowed & EAP_FAST_PROV_AUTH) {
1456 wpa_printf(MSG_DEBUG, "EAP-FAST: Enabling authenticated "
1457 "provisioning TLS cipher suites");
1458 ciphers[count++] = TLS_CIPHER_RSA_DHE_AES256_SHA;
1459 ciphers[count++] = TLS_CIPHER_RSA_DHE_AES128_SHA;
1460 ciphers[count++] = TLS_CIPHER_AES256_SHA;
1461 ciphers[count++] = TLS_CIPHER_AES128_SHA;
1462 ciphers[count++] = TLS_CIPHER_RC4_SHA;
1465 ciphers[count++] = TLS_CIPHER_NONE;
1467 if (tls_connection_set_cipher_list(sm->ssl_ctx, data->ssl.conn,
1469 wpa_printf(MSG_INFO, "EAP-FAST: Could not configure TLS "
1470 "cipher suites for provisioning");
1478 static int eap_fast_process_start(struct eap_sm *sm,
1479 struct eap_fast_data *data, u8 flags,
1480 const u8 *pos, size_t left)
1485 /* EAP-FAST Version negotiation (section 3.1) */
1486 wpa_printf(MSG_DEBUG, "EAP-FAST: Start (server ver=%d, own ver=%d)",
1487 flags & EAP_TLS_VERSION_MASK, data->fast_version);
1488 if ((flags & EAP_TLS_VERSION_MASK) < data->fast_version)
1489 data->fast_version = flags & EAP_TLS_VERSION_MASK;
1490 wpa_printf(MSG_DEBUG, "EAP-FAST: Using FAST version %d",
1491 data->fast_version);
1493 a_id = eap_fast_get_a_id(pos, left, &a_id_len);
1494 eap_fast_select_pac(data, a_id, a_id_len);
1496 if (data->resuming && data->current_pac) {
1497 wpa_printf(MSG_DEBUG, "EAP-FAST: Trying to resume session - "
1498 "do not add PAC-Opaque to TLS ClientHello");
1499 if (eap_fast_clear_pac_opaque_ext(sm, data) < 0)
1501 } else if (data->current_pac) {
1503 * PAC found for the A-ID and we are not resuming an old
1504 * session, so add PAC-Opaque extension to ClientHello.
1506 if (eap_fast_use_pac_opaque(sm, data, data->current_pac) < 0)
1509 /* No PAC found, so we must provision one. */
1510 if (!data->provisioning_allowed) {
1511 wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC found and "
1512 "provisioning disabled");
1515 wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC found - "
1516 "starting provisioning");
1517 if (eap_fast_set_provisioning_ciphers(sm, data) < 0 ||
1518 eap_fast_clear_pac_opaque_ext(sm, data) < 0)
1520 data->provisioning = 1;
1527 static struct wpabuf * eap_fast_process(struct eap_sm *sm, void *priv,
1528 struct eap_method_ret *ret,
1529 const struct wpabuf *reqData)
1531 const struct eap_hdr *req;
1535 struct wpabuf *resp;
1537 struct eap_fast_data *data = priv;
1540 pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_FAST, ret,
1541 reqData, &left, &flags);
1545 req = wpabuf_head(reqData);
1546 id = req->identifier;
1548 if (flags & EAP_TLS_FLAGS_START) {
1549 if (eap_fast_process_start(sm, data, flags, pos, left) < 0)
1552 left = 0; /* A-ID is not used in further packet processing */
1555 wpabuf_set(&msg, pos, left);
1558 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1560 /* Process tunneled (encrypted) phase 2 data. */
1561 res = eap_fast_decrypt(sm, data, ret, id, &msg, &resp);
1563 ret->methodState = METHOD_DONE;
1564 ret->decision = DECISION_FAIL;
1566 * Ack possible Alert that may have caused failure in
1572 if (sm->waiting_ext_cert_check && data->pending_resp) {
1573 struct eap_peer_config *config = eap_get_config(sm);
1575 if (config->pending_ext_cert_check ==
1576 EXT_CERT_CHECK_GOOD) {
1577 wpa_printf(MSG_DEBUG,
1578 "EAP-FAST: External certificate check succeeded - continue handshake");
1579 resp = data->pending_resp;
1580 data->pending_resp = NULL;
1581 sm->waiting_ext_cert_check = 0;
1585 if (config->pending_ext_cert_check ==
1586 EXT_CERT_CHECK_BAD) {
1587 wpa_printf(MSG_DEBUG,
1588 "EAP-FAST: External certificate check failed - force authentication failure");
1589 ret->methodState = METHOD_DONE;
1590 ret->decision = DECISION_FAIL;
1591 sm->waiting_ext_cert_check = 0;
1595 wpa_printf(MSG_DEBUG,
1596 "EAP-FAST: Continuing to wait external server certificate validation");
1600 /* Continue processing TLS handshake (phase 1). */
1601 res = eap_peer_tls_process_helper(sm, &data->ssl,
1603 data->fast_version, id, &msg,
1606 wpa_printf(MSG_DEBUG,
1607 "EAP-FAST: TLS processing failed");
1608 ret->methodState = METHOD_DONE;
1609 ret->decision = DECISION_FAIL;
1613 if (sm->waiting_ext_cert_check) {
1614 wpa_printf(MSG_DEBUG,
1615 "EAP-FAST: Waiting external server certificate validation");
1616 wpabuf_clear_free(data->pending_resp);
1617 data->pending_resp = resp;
1621 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
1623 wpa_printf(MSG_DEBUG,
1624 "EAP-FAST: TLS done, proceed to Phase 2");
1625 if (data->provisioning &&
1626 (!(data->provisioning_allowed &
1627 EAP_FAST_PROV_AUTH) ||
1628 tls_get_cipher(sm->ssl_ctx, data->ssl.conn,
1629 cipher, sizeof(cipher)) < 0 ||
1630 os_strstr(cipher, "ADH-") ||
1631 os_strstr(cipher, "anon"))) {
1632 wpa_printf(MSG_DEBUG, "EAP-FAST: Using "
1633 "anonymous (unauthenticated) "
1635 data->anon_provisioning = 1;
1637 data->anon_provisioning = 0;
1639 if (eap_fast_derive_keys(sm, data) < 0) {
1640 wpa_printf(MSG_DEBUG,
1641 "EAP-FAST: Could not derive keys");
1642 ret->methodState = METHOD_DONE;
1643 ret->decision = DECISION_FAIL;
1644 wpabuf_clear_free(resp);
1651 * Application data included in the handshake message.
1653 wpabuf_clear_free(data->pending_phase2_req);
1654 data->pending_phase2_req = resp;
1656 res = eap_fast_decrypt(sm, data, ret, id, &msg, &resp);
1661 wpabuf_clear_free(resp);
1662 return eap_peer_tls_build_ack(id, EAP_TYPE_FAST,
1663 data->fast_version);
1671 static Boolean eap_fast_has_reauth_data(struct eap_sm *sm, void *priv)
1673 struct eap_fast_data *data = priv;
1674 return tls_connection_established(sm->ssl_ctx, data->ssl.conn);
1678 static void eap_fast_deinit_for_reauth(struct eap_sm *sm, void *priv)
1680 struct eap_fast_data *data = priv;
1682 if (data->phase2_priv && data->phase2_method &&
1683 data->phase2_method->deinit_for_reauth)
1684 data->phase2_method->deinit_for_reauth(sm, data->phase2_priv);
1685 os_free(data->key_block_p);
1686 data->key_block_p = NULL;
1687 wpabuf_clear_free(data->pending_phase2_req);
1688 data->pending_phase2_req = NULL;
1689 wpabuf_clear_free(data->pending_resp);
1690 data->pending_resp = NULL;
1694 static void * eap_fast_init_for_reauth(struct eap_sm *sm, void *priv)
1696 struct eap_fast_data *data = priv;
1697 if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
1701 os_memset(data->key_data, 0, EAP_FAST_KEY_LEN);
1702 os_memset(data->emsk, 0, EAP_EMSK_LEN);
1703 os_free(data->session_id);
1704 data->session_id = NULL;
1705 if (data->phase2_priv && data->phase2_method &&
1706 data->phase2_method->init_for_reauth)
1707 data->phase2_method->init_for_reauth(sm, data->phase2_priv);
1708 data->phase2_success = 0;
1710 data->provisioning = 0;
1711 data->anon_provisioning = 0;
1712 data->simck_idx = 0;
1718 static int eap_fast_get_status(struct eap_sm *sm, void *priv, char *buf,
1719 size_t buflen, int verbose)
1721 struct eap_fast_data *data = priv;
1724 len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
1725 if (data->phase2_method) {
1726 ret = os_snprintf(buf + len, buflen - len,
1727 "EAP-FAST Phase2 method=%s\n",
1728 data->phase2_method->name);
1729 if (os_snprintf_error(buflen - len, ret))
1737 static Boolean eap_fast_isKeyAvailable(struct eap_sm *sm, void *priv)
1739 struct eap_fast_data *data = priv;
1740 return data->success;
1744 static u8 * eap_fast_getKey(struct eap_sm *sm, void *priv, size_t *len)
1746 struct eap_fast_data *data = priv;
1752 key = os_memdup(data->key_data, EAP_FAST_KEY_LEN);
1756 *len = EAP_FAST_KEY_LEN;
1762 static u8 * eap_fast_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
1764 struct eap_fast_data *data = priv;
1767 if (!data->success || !data->session_id)
1770 id = os_memdup(data->session_id, data->id_len);
1774 *len = data->id_len;
1780 static u8 * eap_fast_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
1782 struct eap_fast_data *data = priv;
1788 key = os_memdup(data->emsk, EAP_EMSK_LEN);
1792 *len = EAP_EMSK_LEN;
1798 int eap_peer_fast_register(void)
1800 struct eap_method *eap;
1802 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
1803 EAP_VENDOR_IETF, EAP_TYPE_FAST, "FAST");
1807 eap->init = eap_fast_init;
1808 eap->deinit = eap_fast_deinit;
1809 eap->process = eap_fast_process;
1810 eap->isKeyAvailable = eap_fast_isKeyAvailable;
1811 eap->getKey = eap_fast_getKey;
1812 eap->getSessionId = eap_fast_get_session_id;
1813 eap->get_status = eap_fast_get_status;
1815 eap->has_reauth_data = eap_fast_has_reauth_data;
1816 eap->deinit_for_reauth = eap_fast_deinit_for_reauth;
1817 eap->init_for_reauth = eap_fast_init_for_reauth;
1819 eap->get_emsk = eap_fast_get_emsk;
1821 return eap_peer_method_register(eap);