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 int 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 "
148 static void * eap_fast_init(struct eap_sm *sm)
150 struct eap_fast_data *data;
151 struct eap_peer_config *config = eap_get_config(sm);
156 data = os_zalloc(sizeof(*data));
159 data->fast_version = EAP_FAST_VERSION;
160 data->max_pac_list_len = 10;
162 if (config->phase1 && eap_fast_parse_phase1(data, config->phase1) < 0) {
163 eap_fast_deinit(sm, data);
167 if (eap_peer_select_phase2_methods(config, "auth=",
169 &data->num_phase2_types) < 0) {
170 eap_fast_deinit(sm, data);
174 data->phase2_type.vendor = EAP_VENDOR_IETF;
175 data->phase2_type.method = EAP_TYPE_NONE;
177 if (eap_peer_tls_ssl_init(sm, &data->ssl, config, EAP_TYPE_FAST)) {
178 wpa_printf(MSG_INFO, "EAP-FAST: Failed to initialize SSL.");
179 eap_fast_deinit(sm, data);
183 if (tls_connection_set_session_ticket_cb(sm->ssl_ctx, data->ssl.conn,
184 eap_fast_session_ticket_cb,
186 wpa_printf(MSG_INFO, "EAP-FAST: Failed to set SessionTicket "
188 eap_fast_deinit(sm, data);
193 * The local RADIUS server in a Cisco AP does not seem to like empty
194 * fragments before data, so disable that workaround for CBC.
195 * TODO: consider making this configurable
197 if (tls_connection_enable_workaround(sm->ssl_ctx, data->ssl.conn)) {
198 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to enable TLS "
202 if (!config->pac_file) {
203 wpa_printf(MSG_INFO, "EAP-FAST: No PAC file configured");
204 eap_fast_deinit(sm, data);
208 if (data->use_pac_binary_format &&
209 eap_fast_load_pac_bin(sm, &data->pac, config->pac_file) < 0) {
210 wpa_printf(MSG_INFO, "EAP-FAST: Failed to load PAC file");
211 eap_fast_deinit(sm, data);
215 if (!data->use_pac_binary_format &&
216 eap_fast_load_pac(sm, &data->pac, config->pac_file) < 0) {
217 wpa_printf(MSG_INFO, "EAP-FAST: Failed to load PAC file");
218 eap_fast_deinit(sm, data);
221 eap_fast_pac_list_truncate(data->pac, data->max_pac_list_len);
223 if (data->pac == NULL && !data->provisioning_allowed) {
224 wpa_printf(MSG_INFO, "EAP-FAST: No PAC configured and "
225 "provisioning disabled");
226 eap_fast_deinit(sm, data);
234 static void eap_fast_deinit(struct eap_sm *sm, void *priv)
236 struct eap_fast_data *data = priv;
237 struct eap_fast_pac *pac, *prev;
241 if (data->phase2_priv && data->phase2_method)
242 data->phase2_method->deinit(sm, data->phase2_priv);
243 os_free(data->phase2_types);
244 os_free(data->key_block_p);
245 eap_peer_tls_ssl_deinit(sm, &data->ssl);
252 eap_fast_free_pac(prev);
254 os_memset(data->key_data, 0, EAP_FAST_KEY_LEN);
255 os_memset(data->emsk, 0, EAP_EMSK_LEN);
256 os_free(data->session_id);
257 wpabuf_free(data->pending_phase2_req);
258 wpabuf_free(data->pending_resp);
263 static int eap_fast_derive_msk(struct eap_fast_data *data)
265 if (eap_fast_derive_eap_msk(data->simck, data->key_data) < 0 ||
266 eap_fast_derive_eap_emsk(data->simck, data->emsk) < 0)
273 static int eap_fast_derive_key_auth(struct eap_sm *sm,
274 struct eap_fast_data *data)
278 /* RFC 4851, Section 5.1:
279 * Extra key material after TLS key_block: session_key_seed[40]
282 sks = eap_fast_derive_key(sm->ssl_ctx, data->ssl.conn, "key expansion",
285 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive "
291 * RFC 4851, Section 5.2:
292 * S-IMCK[0] = session_key_seed
294 wpa_hexdump_key(MSG_DEBUG,
295 "EAP-FAST: session_key_seed (SKS = S-IMCK[0])",
296 sks, EAP_FAST_SKS_LEN);
298 os_memcpy(data->simck, sks, EAP_FAST_SIMCK_LEN);
304 static int eap_fast_derive_key_provisioning(struct eap_sm *sm,
305 struct eap_fast_data *data)
307 os_free(data->key_block_p);
308 data->key_block_p = (struct eap_fast_key_block_provisioning *)
309 eap_fast_derive_key(sm->ssl_ctx, data->ssl.conn,
311 sizeof(*data->key_block_p));
312 if (data->key_block_p == NULL) {
313 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive key block");
317 * RFC 4851, Section 5.2:
318 * S-IMCK[0] = session_key_seed
320 wpa_hexdump_key(MSG_DEBUG,
321 "EAP-FAST: session_key_seed (SKS = S-IMCK[0])",
322 data->key_block_p->session_key_seed,
323 sizeof(data->key_block_p->session_key_seed));
325 os_memcpy(data->simck, data->key_block_p->session_key_seed,
327 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: server_challenge",
328 data->key_block_p->server_challenge,
329 sizeof(data->key_block_p->server_challenge));
330 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: client_challenge",
331 data->key_block_p->client_challenge,
332 sizeof(data->key_block_p->client_challenge));
337 static int eap_fast_derive_keys(struct eap_sm *sm, struct eap_fast_data *data)
341 if (data->anon_provisioning)
342 res = eap_fast_derive_key_provisioning(sm, data);
344 res = eap_fast_derive_key_auth(sm, data);
349 static int eap_fast_init_phase2_method(struct eap_sm *sm,
350 struct eap_fast_data *data)
352 data->phase2_method =
353 eap_peer_get_eap_method(data->phase2_type.vendor,
354 data->phase2_type.method);
355 if (data->phase2_method == NULL)
358 if (data->key_block_p) {
359 sm->auth_challenge = data->key_block_p->server_challenge;
360 sm->peer_challenge = data->key_block_p->client_challenge;
363 data->phase2_priv = data->phase2_method->init(sm);
365 sm->auth_challenge = NULL;
366 sm->peer_challenge = NULL;
368 return data->phase2_priv == NULL ? -1 : 0;
372 static int eap_fast_select_phase2_method(struct eap_fast_data *data, u8 type)
376 /* TODO: TNC with anonymous provisioning; need to require both
377 * completed MSCHAPv2 and TNC */
379 if (data->anon_provisioning && type != EAP_TYPE_MSCHAPV2) {
380 wpa_printf(MSG_INFO, "EAP-FAST: Only EAP-MSCHAPv2 is allowed "
381 "during unauthenticated provisioning; reject phase2"
387 if (type == EAP_TYPE_TNC) {
388 data->phase2_type.vendor = EAP_VENDOR_IETF;
389 data->phase2_type.method = EAP_TYPE_TNC;
390 wpa_printf(MSG_DEBUG, "EAP-FAST: Selected Phase 2 EAP "
391 "vendor %d method %d for TNC",
392 data->phase2_type.vendor,
393 data->phase2_type.method);
398 for (i = 0; i < data->num_phase2_types; i++) {
399 if (data->phase2_types[i].vendor != EAP_VENDOR_IETF ||
400 data->phase2_types[i].method != type)
403 data->phase2_type.vendor = data->phase2_types[i].vendor;
404 data->phase2_type.method = data->phase2_types[i].method;
405 wpa_printf(MSG_DEBUG, "EAP-FAST: Selected Phase 2 EAP "
406 "vendor %d method %d",
407 data->phase2_type.vendor,
408 data->phase2_type.method);
412 if (type != data->phase2_type.method || type == EAP_TYPE_NONE)
419 static int eap_fast_phase2_request(struct eap_sm *sm,
420 struct eap_fast_data *data,
421 struct eap_method_ret *ret,
423 struct wpabuf **resp)
425 size_t len = be_to_host16(hdr->length);
427 struct eap_method_ret iret;
428 struct eap_peer_config *config = eap_get_config(sm);
431 if (len <= sizeof(struct eap_hdr)) {
432 wpa_printf(MSG_INFO, "EAP-FAST: too short "
433 "Phase 2 request (len=%lu)", (unsigned long) len);
436 pos = (u8 *) (hdr + 1);
437 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 Request: type=%d", *pos);
438 if (*pos == EAP_TYPE_IDENTITY) {
439 *resp = eap_sm_buildIdentity(sm, hdr->identifier, 1);
443 if (data->phase2_priv && data->phase2_method &&
444 *pos != data->phase2_type.method) {
445 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 EAP sequence - "
446 "deinitialize previous method");
447 data->phase2_method->deinit(sm, data->phase2_priv);
448 data->phase2_method = NULL;
449 data->phase2_priv = NULL;
450 data->phase2_type.vendor = EAP_VENDOR_IETF;
451 data->phase2_type.method = EAP_TYPE_NONE;
454 if (data->phase2_type.vendor == EAP_VENDOR_IETF &&
455 data->phase2_type.method == EAP_TYPE_NONE &&
456 eap_fast_select_phase2_method(data, *pos) < 0) {
457 if (eap_peer_tls_phase2_nak(data->phase2_types,
458 data->num_phase2_types,
464 if ((data->phase2_priv == NULL &&
465 eap_fast_init_phase2_method(sm, data) < 0) ||
466 data->phase2_method == NULL) {
467 wpa_printf(MSG_INFO, "EAP-FAST: Failed to initialize "
468 "Phase 2 EAP method %d", *pos);
469 ret->methodState = METHOD_DONE;
470 ret->decision = DECISION_FAIL;
474 os_memset(&iret, 0, sizeof(iret));
475 wpabuf_set(&msg, hdr, len);
476 *resp = data->phase2_method->process(sm, data->phase2_priv, &iret,
479 (iret.methodState == METHOD_DONE &&
480 iret.decision == DECISION_FAIL)) {
481 ret->methodState = METHOD_DONE;
482 ret->decision = DECISION_FAIL;
483 } else if ((iret.methodState == METHOD_DONE ||
484 iret.methodState == METHOD_MAY_CONT) &&
485 (iret.decision == DECISION_UNCOND_SUCC ||
486 iret.decision == DECISION_COND_SUCC)) {
487 data->phase2_success = 1;
490 if (*resp == NULL && config &&
491 (config->pending_req_identity || config->pending_req_password ||
492 config->pending_req_otp || config->pending_req_new_password)) {
493 wpabuf_free(data->pending_phase2_req);
494 data->pending_phase2_req = wpabuf_alloc_copy(hdr, len);
495 } else if (*resp == NULL)
502 static struct wpabuf * eap_fast_tlv_nak(int vendor_id, int tlv_type)
505 struct eap_tlv_nak_tlv *nak;
506 buf = wpabuf_alloc(sizeof(*nak));
509 nak = wpabuf_put(buf, sizeof(*nak));
510 nak->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY | EAP_TLV_NAK_TLV);
511 nak->length = host_to_be16(6);
512 nak->vendor_id = host_to_be32(vendor_id);
513 nak->nak_type = host_to_be16(tlv_type);
518 static struct wpabuf * eap_fast_tlv_result(int status, int intermediate)
521 struct eap_tlv_intermediate_result_tlv *result;
522 buf = wpabuf_alloc(sizeof(*result));
525 wpa_printf(MSG_DEBUG, "EAP-FAST: Add %sResult TLV(status=%d)",
526 intermediate ? "Intermediate " : "", status);
527 result = wpabuf_put(buf, sizeof(*result));
528 result->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
530 EAP_TLV_INTERMEDIATE_RESULT_TLV :
531 EAP_TLV_RESULT_TLV));
532 result->length = host_to_be16(2);
533 result->status = host_to_be16(status);
538 static struct wpabuf * eap_fast_tlv_pac_ack(void)
541 struct eap_tlv_result_tlv *res;
542 struct eap_tlv_pac_ack_tlv *ack;
544 buf = wpabuf_alloc(sizeof(*res) + sizeof(*ack));
548 wpa_printf(MSG_DEBUG, "EAP-FAST: Add PAC TLV (ack)");
549 ack = wpabuf_put(buf, sizeof(*ack));
550 ack->tlv_type = host_to_be16(EAP_TLV_PAC_TLV |
551 EAP_TLV_TYPE_MANDATORY);
552 ack->length = host_to_be16(sizeof(*ack) - sizeof(struct eap_tlv_hdr));
553 ack->pac_type = host_to_be16(PAC_TYPE_PAC_ACKNOWLEDGEMENT);
554 ack->pac_len = host_to_be16(2);
555 ack->result = host_to_be16(EAP_TLV_RESULT_SUCCESS);
561 static struct wpabuf * eap_fast_process_eap_payload_tlv(
562 struct eap_sm *sm, struct eap_fast_data *data,
563 struct eap_method_ret *ret,
564 u8 *eap_payload_tlv, size_t eap_payload_tlv_len)
567 struct wpabuf *resp = NULL;
569 if (eap_payload_tlv_len < sizeof(*hdr)) {
570 wpa_printf(MSG_DEBUG, "EAP-FAST: too short EAP "
571 "Payload TLV (len=%lu)",
572 (unsigned long) eap_payload_tlv_len);
576 hdr = (struct eap_hdr *) eap_payload_tlv;
577 if (be_to_host16(hdr->length) > eap_payload_tlv_len) {
578 wpa_printf(MSG_DEBUG, "EAP-FAST: EAP packet overflow in "
583 if (hdr->code != EAP_CODE_REQUEST) {
584 wpa_printf(MSG_INFO, "EAP-FAST: Unexpected code=%d in "
585 "Phase 2 EAP header", hdr->code);
589 if (eap_fast_phase2_request(sm, data, ret, hdr, &resp)) {
590 wpa_printf(MSG_INFO, "EAP-FAST: Phase2 Request processing "
595 return eap_fast_tlv_eap_payload(resp);
599 static int eap_fast_validate_crypto_binding(
600 struct eap_tlv_crypto_binding_tlv *_bind)
602 wpa_printf(MSG_DEBUG, "EAP-FAST: Crypto-Binding TLV: Version %d "
603 "Received Version %d SubType %d",
604 _bind->version, _bind->received_version, _bind->subtype);
605 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE",
606 _bind->nonce, sizeof(_bind->nonce));
607 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC",
608 _bind->compound_mac, sizeof(_bind->compound_mac));
610 if (_bind->version != EAP_FAST_VERSION ||
611 _bind->received_version != EAP_FAST_VERSION ||
612 _bind->subtype != EAP_TLV_CRYPTO_BINDING_SUBTYPE_REQUEST) {
613 wpa_printf(MSG_INFO, "EAP-FAST: Invalid version/subtype in "
614 "Crypto-Binding TLV: Version %d "
615 "Received Version %d SubType %d",
616 _bind->version, _bind->received_version,
625 static void eap_fast_write_crypto_binding(
626 struct eap_tlv_crypto_binding_tlv *rbind,
627 struct eap_tlv_crypto_binding_tlv *_bind, const u8 *cmk)
629 rbind->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
630 EAP_TLV_CRYPTO_BINDING_TLV);
631 rbind->length = host_to_be16(sizeof(*rbind) -
632 sizeof(struct eap_tlv_hdr));
633 rbind->version = EAP_FAST_VERSION;
634 rbind->received_version = _bind->version;
635 rbind->subtype = EAP_TLV_CRYPTO_BINDING_SUBTYPE_RESPONSE;
636 os_memcpy(rbind->nonce, _bind->nonce, sizeof(_bind->nonce));
637 inc_byte_array(rbind->nonce, sizeof(rbind->nonce));
638 hmac_sha1(cmk, EAP_FAST_CMK_LEN, (u8 *) rbind, sizeof(*rbind),
639 rbind->compound_mac);
641 wpa_printf(MSG_DEBUG, "EAP-FAST: Reply Crypto-Binding TLV: Version %d "
642 "Received Version %d SubType %d",
643 rbind->version, rbind->received_version, rbind->subtype);
644 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE",
645 rbind->nonce, sizeof(rbind->nonce));
646 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC",
647 rbind->compound_mac, sizeof(rbind->compound_mac));
651 static int eap_fast_get_phase2_key(struct eap_sm *sm,
652 struct eap_fast_data *data,
653 u8 *isk, size_t isk_len)
658 os_memset(isk, 0, isk_len);
660 if (data->phase2_method == NULL || data->phase2_priv == NULL) {
661 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 method not "
666 if (data->phase2_method->isKeyAvailable == NULL ||
667 data->phase2_method->getKey == NULL)
670 if (!data->phase2_method->isKeyAvailable(sm, data->phase2_priv) ||
671 (key = data->phase2_method->getKey(sm, data->phase2_priv,
672 &key_len)) == NULL) {
673 wpa_printf(MSG_DEBUG, "EAP-FAST: Could not get key material "
678 if (key_len > isk_len)
681 data->phase2_method->vendor == EAP_VENDOR_IETF &&
682 data->phase2_method->method == EAP_TYPE_MSCHAPV2) {
684 * EAP-FAST uses reverse order for MS-MPPE keys when deriving
685 * MSK from EAP-MSCHAPv2. Swap the keys here to get the correct
686 * ISK for EAP-FAST cryptobinding.
688 os_memcpy(isk, key + 16, 16);
689 os_memcpy(isk + 16, key, 16);
691 os_memcpy(isk, key, key_len);
698 static int eap_fast_get_cmk(struct eap_sm *sm, struct eap_fast_data *data,
701 u8 isk[32], imck[60];
703 wpa_printf(MSG_DEBUG, "EAP-FAST: Determining CMK[%d] for Compound MIC "
704 "calculation", data->simck_idx + 1);
707 * RFC 4851, Section 5.2:
708 * IMCK[j] = T-PRF(S-IMCK[j-1], "Inner Methods Compound Keys",
710 * S-IMCK[j] = first 40 octets of IMCK[j]
711 * CMK[j] = last 20 octets of IMCK[j]
714 if (eap_fast_get_phase2_key(sm, data, isk, sizeof(isk)) < 0)
716 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: ISK[j]", isk, sizeof(isk));
717 sha1_t_prf(data->simck, EAP_FAST_SIMCK_LEN,
718 "Inner Methods Compound Keys",
719 isk, sizeof(isk), imck, sizeof(imck));
721 os_memcpy(data->simck, imck, EAP_FAST_SIMCK_LEN);
722 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: S-IMCK[j]",
723 data->simck, EAP_FAST_SIMCK_LEN);
724 os_memcpy(cmk, imck + EAP_FAST_SIMCK_LEN, EAP_FAST_CMK_LEN);
725 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: CMK[j]",
726 cmk, EAP_FAST_CMK_LEN);
732 static u8 * eap_fast_write_pac_request(u8 *pos, u16 pac_type)
734 struct eap_tlv_hdr *pac;
735 struct eap_tlv_request_action_tlv *act;
736 struct eap_tlv_pac_type_tlv *type;
738 act = (struct eap_tlv_request_action_tlv *) pos;
739 act->tlv_type = host_to_be16(EAP_TLV_REQUEST_ACTION_TLV);
740 act->length = host_to_be16(2);
741 act->action = host_to_be16(EAP_TLV_ACTION_PROCESS_TLV);
743 pac = (struct eap_tlv_hdr *) (act + 1);
744 pac->tlv_type = host_to_be16(EAP_TLV_PAC_TLV);
745 pac->length = host_to_be16(sizeof(*type));
747 type = (struct eap_tlv_pac_type_tlv *) (pac + 1);
748 type->tlv_type = host_to_be16(PAC_TYPE_PAC_TYPE);
749 type->length = host_to_be16(2);
750 type->pac_type = host_to_be16(pac_type);
752 return (u8 *) (type + 1);
756 static struct wpabuf * eap_fast_process_crypto_binding(
757 struct eap_sm *sm, struct eap_fast_data *data,
758 struct eap_method_ret *ret,
759 struct eap_tlv_crypto_binding_tlv *_bind, size_t bind_len)
763 u8 cmk[EAP_FAST_CMK_LEN], cmac[SHA1_MAC_LEN];
767 if (eap_fast_validate_crypto_binding(_bind) < 0)
770 if (eap_fast_get_cmk(sm, data, cmk) < 0)
773 /* Validate received Compound MAC */
774 os_memcpy(cmac, _bind->compound_mac, sizeof(cmac));
775 os_memset(_bind->compound_mac, 0, sizeof(cmac));
776 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Crypto-Binding TLV for Compound "
777 "MAC calculation", (u8 *) _bind, bind_len);
778 hmac_sha1(cmk, EAP_FAST_CMK_LEN, (u8 *) _bind, bind_len,
779 _bind->compound_mac);
780 res = os_memcmp_const(cmac, _bind->compound_mac, sizeof(cmac));
781 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Received Compound MAC",
783 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Calculated Compound MAC",
784 _bind->compound_mac, sizeof(cmac));
786 wpa_printf(MSG_INFO, "EAP-FAST: Compound MAC did not match");
787 os_memcpy(_bind->compound_mac, cmac, sizeof(cmac));
792 * Compound MAC was valid, so authentication succeeded. Reply with
793 * crypto binding to allow server to complete authentication.
796 len = sizeof(struct eap_tlv_crypto_binding_tlv);
797 resp = wpabuf_alloc(len);
801 if (!data->anon_provisioning && data->phase2_success &&
802 eap_fast_derive_msk(data) < 0) {
803 wpa_printf(MSG_INFO, "EAP-FAST: Failed to generate MSK");
804 ret->methodState = METHOD_DONE;
805 ret->decision = DECISION_FAIL;
806 data->phase2_success = 0;
811 if (!data->anon_provisioning && data->phase2_success) {
812 os_free(data->session_id);
813 data->session_id = eap_peer_tls_derive_session_id(
814 sm, &data->ssl, EAP_TYPE_FAST, &data->id_len);
815 if (data->session_id) {
816 wpa_hexdump(MSG_DEBUG, "EAP-FAST: Derived Session-Id",
817 data->session_id, data->id_len);
819 wpa_printf(MSG_ERROR, "EAP-FAST: Failed to derive "
826 pos = wpabuf_put(resp, sizeof(struct eap_tlv_crypto_binding_tlv));
827 eap_fast_write_crypto_binding((struct eap_tlv_crypto_binding_tlv *)
834 static void eap_fast_parse_pac_tlv(struct eap_fast_pac *entry, int type,
835 u8 *pos, size_t len, int *pac_key_found)
837 switch (type & 0x7fff) {
838 case PAC_TYPE_PAC_KEY:
839 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: PAC-Key", pos, len);
840 if (len != EAP_FAST_PAC_KEY_LEN) {
841 wpa_printf(MSG_DEBUG, "EAP-FAST: Invalid PAC-Key "
842 "length %lu", (unsigned long) len);
846 os_memcpy(entry->pac_key, pos, len);
848 case PAC_TYPE_PAC_OPAQUE:
849 wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Opaque", pos, len);
850 entry->pac_opaque = pos;
851 entry->pac_opaque_len = len;
853 case PAC_TYPE_PAC_INFO:
854 wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Info", pos, len);
855 entry->pac_info = pos;
856 entry->pac_info_len = len;
859 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignored unknown PAC type %d",
866 static int eap_fast_process_pac_tlv(struct eap_fast_pac *entry,
867 u8 *pac, size_t pac_len)
869 struct pac_tlv_hdr *hdr;
872 int type, pac_key_found = 0;
877 while (left > sizeof(*hdr)) {
878 hdr = (struct pac_tlv_hdr *) pos;
879 type = be_to_host16(hdr->type);
880 len = be_to_host16(hdr->len);
882 left -= sizeof(*hdr);
884 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV overrun "
885 "(type=%d len=%lu left=%lu)",
886 type, (unsigned long) len,
887 (unsigned long) left);
891 eap_fast_parse_pac_tlv(entry, type, pos, len, &pac_key_found);
897 if (!pac_key_found || !entry->pac_opaque || !entry->pac_info) {
898 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV does not include "
899 "all the required fields");
907 static int eap_fast_parse_pac_info(struct eap_fast_pac *entry, int type,
914 switch (type & 0x7fff) {
915 case PAC_TYPE_CRED_LIFETIME:
917 wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Info - "
918 "Invalid CRED_LIFETIME length - ignored",
924 * This is not currently saved separately in PAC files since
925 * the server can automatically initiate PAC update when
926 * needed. Anyway, the information is available from PAC-Info
927 * dump if it is needed for something in the future.
929 lifetime = WPA_GET_BE32(pos);
931 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info - CRED_LIFETIME %d "
933 lifetime, (lifetime - (u32) now.sec) / 86400);
936 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - A-ID",
939 entry->a_id_len = len;
942 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - I-ID",
945 entry->i_id_len = len;
947 case PAC_TYPE_A_ID_INFO:
948 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - A-ID-Info",
950 entry->a_id_info = pos;
951 entry->a_id_info_len = len;
953 case PAC_TYPE_PAC_TYPE:
954 /* RFC 5422, Section 4.2.6 - PAC-Type TLV */
956 wpa_printf(MSG_INFO, "EAP-FAST: Invalid PAC-Type "
957 "length %lu (expected 2)",
958 (unsigned long) len);
959 wpa_hexdump_ascii(MSG_DEBUG,
960 "EAP-FAST: PAC-Info - PAC-Type",
964 pac_type = WPA_GET_BE16(pos);
965 if (pac_type != PAC_TYPE_TUNNEL_PAC &&
966 pac_type != PAC_TYPE_USER_AUTHORIZATION &&
967 pac_type != PAC_TYPE_MACHINE_AUTHENTICATION) {
968 wpa_printf(MSG_INFO, "EAP-FAST: Unsupported PAC Type "
973 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info - PAC-Type %d",
975 entry->pac_type = pac_type;
978 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignored unknown PAC-Info "
987 static int eap_fast_process_pac_info(struct eap_fast_pac *entry)
989 struct pac_tlv_hdr *hdr;
994 /* RFC 5422, Section 4.2.4 */
996 /* PAC-Type defaults to Tunnel PAC (Type 1) */
997 entry->pac_type = PAC_TYPE_TUNNEL_PAC;
999 pos = entry->pac_info;
1000 left = entry->pac_info_len;
1001 while (left > sizeof(*hdr)) {
1002 hdr = (struct pac_tlv_hdr *) pos;
1003 type = be_to_host16(hdr->type);
1004 len = be_to_host16(hdr->len);
1005 pos += sizeof(*hdr);
1006 left -= sizeof(*hdr);
1008 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info overrun "
1009 "(type=%d len=%lu left=%lu)",
1010 type, (unsigned long) len,
1011 (unsigned long) left);
1015 if (eap_fast_parse_pac_info(entry, type, pos, len) < 0)
1022 if (entry->a_id == NULL || entry->a_id_info == NULL) {
1023 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info does not include "
1024 "all the required fields");
1032 static struct wpabuf * eap_fast_process_pac(struct eap_sm *sm,
1033 struct eap_fast_data *data,
1034 struct eap_method_ret *ret,
1035 u8 *pac, size_t pac_len)
1037 struct eap_peer_config *config = eap_get_config(sm);
1038 struct eap_fast_pac entry;
1040 os_memset(&entry, 0, sizeof(entry));
1041 if (eap_fast_process_pac_tlv(&entry, pac, pac_len) ||
1042 eap_fast_process_pac_info(&entry))
1045 eap_fast_add_pac(&data->pac, &data->current_pac, &entry);
1046 eap_fast_pac_list_truncate(data->pac, data->max_pac_list_len);
1047 if (data->use_pac_binary_format)
1048 eap_fast_save_pac_bin(sm, data->pac, config->pac_file);
1050 eap_fast_save_pac(sm, data->pac, config->pac_file);
1052 if (data->provisioning) {
1053 if (data->anon_provisioning) {
1055 * Unauthenticated provisioning does not provide keying
1056 * material and must end with an EAP-Failure.
1057 * Authentication will be done separately after this.
1060 ret->decision = DECISION_FAIL;
1063 * Server may or may not allow authenticated
1064 * provisioning also for key generation.
1066 ret->decision = DECISION_COND_SUCC;
1068 wpa_printf(MSG_DEBUG, "EAP-FAST: Send PAC-Acknowledgement TLV "
1069 "- Provisioning completed successfully");
1070 sm->expected_failure = 1;
1073 * This is PAC refreshing, i.e., normal authentication that is
1074 * expected to be completed with an EAP-Success. However,
1075 * RFC 5422, Section 3.5 allows EAP-Failure to be sent even
1076 * after protected success exchange in case of EAP-Fast
1077 * provisioning, so we better use DECISION_COND_SUCC here
1078 * instead of DECISION_UNCOND_SUCC.
1080 wpa_printf(MSG_DEBUG, "EAP-FAST: Send PAC-Acknowledgement TLV "
1081 "- PAC refreshing completed successfully");
1082 ret->decision = DECISION_COND_SUCC;
1084 ret->methodState = METHOD_DONE;
1085 return eap_fast_tlv_pac_ack();
1089 static int eap_fast_parse_decrypted(struct wpabuf *decrypted,
1090 struct eap_fast_tlv_parse *tlv,
1091 struct wpabuf **resp)
1093 int mandatory, tlv_type, res;
1097 os_memset(tlv, 0, sizeof(*tlv));
1099 /* Parse TLVs from the decrypted Phase 2 data */
1100 pos = wpabuf_mhead(decrypted);
1101 end = pos + wpabuf_len(decrypted);
1102 while (end - pos > 4) {
1103 mandatory = pos[0] & 0x80;
1104 tlv_type = WPA_GET_BE16(pos) & 0x3fff;
1106 len = WPA_GET_BE16(pos);
1108 if (len > (size_t) (end - pos)) {
1109 wpa_printf(MSG_INFO, "EAP-FAST: TLV overflow");
1112 wpa_printf(MSG_DEBUG, "EAP-FAST: Received Phase 2: "
1113 "TLV type %d length %u%s",
1114 tlv_type, (unsigned int) len,
1115 mandatory ? " (mandatory)" : "");
1117 res = eap_fast_parse_tlv(tlv, tlv_type, pos, len);
1122 wpa_printf(MSG_DEBUG, "EAP-FAST: Nak unknown "
1123 "mandatory TLV type %d", tlv_type);
1124 *resp = eap_fast_tlv_nak(0, tlv_type);
1127 wpa_printf(MSG_DEBUG, "EAP-FAST: ignored "
1128 "unknown optional TLV type %d",
1140 static int eap_fast_encrypt_response(struct eap_sm *sm,
1141 struct eap_fast_data *data,
1142 struct wpabuf *resp,
1143 u8 identifier, struct wpabuf **out_data)
1148 wpa_hexdump_buf(MSG_DEBUG, "EAP-FAST: Encrypting Phase 2 data",
1150 if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_FAST,
1151 data->fast_version, identifier,
1153 wpa_printf(MSG_INFO, "EAP-FAST: Failed to encrypt a Phase 2 "
1162 static struct wpabuf * eap_fast_pac_request(void)
1167 tmp = wpabuf_alloc(sizeof(struct eap_tlv_hdr) +
1168 sizeof(struct eap_tlv_request_action_tlv) +
1169 sizeof(struct eap_tlv_pac_type_tlv));
1173 pos = wpabuf_put(tmp, 0);
1174 pos2 = eap_fast_write_pac_request(pos, PAC_TYPE_TUNNEL_PAC);
1175 wpabuf_put(tmp, pos2 - pos);
1180 static int eap_fast_process_decrypted(struct eap_sm *sm,
1181 struct eap_fast_data *data,
1182 struct eap_method_ret *ret,
1184 struct wpabuf *decrypted,
1185 struct wpabuf **out_data)
1187 struct wpabuf *resp = NULL, *tmp;
1188 struct eap_fast_tlv_parse tlv;
1191 if (eap_fast_parse_decrypted(decrypted, &tlv, &resp) < 0)
1194 return eap_fast_encrypt_response(sm, data, resp,
1195 identifier, out_data);
1197 if (tlv.result == EAP_TLV_RESULT_FAILURE) {
1198 resp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 0);
1199 return eap_fast_encrypt_response(sm, data, resp,
1200 identifier, out_data);
1203 if (tlv.iresult == EAP_TLV_RESULT_FAILURE) {
1204 resp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 1);
1205 return eap_fast_encrypt_response(sm, data, resp,
1206 identifier, out_data);
1209 if (tlv.crypto_binding) {
1210 tmp = eap_fast_process_crypto_binding(sm, data, ret,
1212 tlv.crypto_binding_len);
1216 resp = wpabuf_concat(resp, tmp);
1219 if (tlv.iresult == EAP_TLV_RESULT_SUCCESS) {
1220 tmp = eap_fast_tlv_result(failed ? EAP_TLV_RESULT_FAILURE :
1221 EAP_TLV_RESULT_SUCCESS, 1);
1222 resp = wpabuf_concat(resp, tmp);
1225 if (tlv.eap_payload_tlv) {
1226 tmp = eap_fast_process_eap_payload_tlv(
1227 sm, data, ret, tlv.eap_payload_tlv,
1228 tlv.eap_payload_tlv_len);
1229 resp = wpabuf_concat(resp, tmp);
1232 if (tlv.pac && tlv.result != EAP_TLV_RESULT_SUCCESS) {
1233 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV without Result TLV "
1234 "acknowledging success");
1236 } else if (tlv.pac && tlv.result == EAP_TLV_RESULT_SUCCESS) {
1237 tmp = eap_fast_process_pac(sm, data, ret, tlv.pac,
1239 resp = wpabuf_concat(resp, tmp);
1242 if (data->current_pac == NULL && data->provisioning &&
1243 !data->anon_provisioning && !tlv.pac &&
1244 (tlv.iresult == EAP_TLV_RESULT_SUCCESS ||
1245 tlv.result == EAP_TLV_RESULT_SUCCESS)) {
1247 * Need to request Tunnel PAC when using authenticated
1250 wpa_printf(MSG_DEBUG, "EAP-FAST: Request Tunnel PAC");
1251 tmp = eap_fast_pac_request();
1252 resp = wpabuf_concat(resp, tmp);
1255 if (tlv.result == EAP_TLV_RESULT_SUCCESS && !failed) {
1256 tmp = eap_fast_tlv_result(EAP_TLV_RESULT_SUCCESS, 0);
1257 resp = wpabuf_concat(tmp, resp);
1258 } else if (failed) {
1259 tmp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 0);
1260 resp = wpabuf_concat(tmp, resp);
1263 if (resp && tlv.result == EAP_TLV_RESULT_SUCCESS && !failed &&
1264 tlv.crypto_binding && data->phase2_success) {
1265 if (data->anon_provisioning) {
1266 wpa_printf(MSG_DEBUG, "EAP-FAST: Unauthenticated "
1267 "provisioning completed successfully.");
1268 ret->methodState = METHOD_DONE;
1269 ret->decision = DECISION_FAIL;
1270 sm->expected_failure = 1;
1272 wpa_printf(MSG_DEBUG, "EAP-FAST: Authentication "
1273 "completed successfully.");
1274 if (data->provisioning)
1275 ret->methodState = METHOD_MAY_CONT;
1277 ret->methodState = METHOD_DONE;
1278 ret->decision = DECISION_UNCOND_SUCC;
1283 wpa_printf(MSG_DEBUG, "EAP-FAST: No recognized TLVs - send "
1284 "empty response packet");
1285 resp = wpabuf_alloc(1);
1288 return eap_fast_encrypt_response(sm, data, resp, identifier,
1293 static int eap_fast_decrypt(struct eap_sm *sm, struct eap_fast_data *data,
1294 struct eap_method_ret *ret, u8 identifier,
1295 const struct wpabuf *in_data,
1296 struct wpabuf **out_data)
1298 struct wpabuf *in_decrypted;
1301 wpa_printf(MSG_DEBUG, "EAP-FAST: Received %lu bytes encrypted data for"
1302 " Phase 2", (unsigned long) wpabuf_len(in_data));
1304 if (data->pending_phase2_req) {
1305 wpa_printf(MSG_DEBUG, "EAP-FAST: Pending Phase 2 request - "
1306 "skip decryption and use old data");
1307 /* Clear TLS reassembly state. */
1308 eap_peer_tls_reset_input(&data->ssl);
1310 in_decrypted = data->pending_phase2_req;
1311 data->pending_phase2_req = NULL;
1315 if (wpabuf_len(in_data) == 0) {
1316 /* Received TLS ACK - requesting more fragments */
1317 return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_FAST,
1319 identifier, NULL, out_data);
1322 res = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
1327 wpa_hexdump_buf(MSG_MSGDUMP, "EAP-FAST: Decrypted Phase 2 TLV(s)",
1330 if (wpabuf_len(in_decrypted) < 4) {
1331 wpa_printf(MSG_INFO, "EAP-FAST: Too short Phase 2 "
1332 "TLV frame (len=%lu)",
1333 (unsigned long) wpabuf_len(in_decrypted));
1334 wpabuf_free(in_decrypted);
1338 res = eap_fast_process_decrypted(sm, data, ret, identifier,
1339 in_decrypted, out_data);
1341 wpabuf_free(in_decrypted);
1347 static const u8 * eap_fast_get_a_id(const u8 *buf, size_t len, size_t *id_len)
1350 const struct pac_tlv_hdr *hdr;
1353 * Parse authority identity (A-ID) from the EAP-FAST/Start. This
1354 * supports both raw A-ID and one inside an A-ID TLV.
1358 if (len > sizeof(*hdr)) {
1360 hdr = (const struct pac_tlv_hdr *) buf;
1361 tlen = be_to_host16(hdr->len);
1362 if (be_to_host16(hdr->type) == PAC_TYPE_A_ID &&
1363 sizeof(*hdr) + tlen <= len) {
1364 wpa_printf(MSG_DEBUG, "EAP-FAST: A-ID was in TLV "
1366 a_id = (const u8 *) (hdr + 1);
1370 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: A-ID", a_id, *id_len);
1376 static void eap_fast_select_pac(struct eap_fast_data *data,
1377 const u8 *a_id, size_t a_id_len)
1379 data->current_pac = eap_fast_get_pac(data->pac, a_id, a_id_len,
1380 PAC_TYPE_TUNNEL_PAC);
1381 if (data->current_pac == NULL) {
1383 * Tunnel PAC was not available for this A-ID. Try to use
1384 * Machine Authentication PAC, if one is available.
1386 data->current_pac = eap_fast_get_pac(
1387 data->pac, a_id, a_id_len,
1388 PAC_TYPE_MACHINE_AUTHENTICATION);
1391 if (data->current_pac) {
1392 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC found for this A-ID "
1393 "(PAC-Type %d)", data->current_pac->pac_type);
1394 wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-FAST: A-ID-Info",
1395 data->current_pac->a_id_info,
1396 data->current_pac->a_id_info_len);
1401 static int eap_fast_use_pac_opaque(struct eap_sm *sm,
1402 struct eap_fast_data *data,
1403 struct eap_fast_pac *pac)
1406 size_t tlv_len, olen;
1407 struct eap_tlv_hdr *ehdr;
1409 olen = pac->pac_opaque_len;
1410 tlv_len = sizeof(*ehdr) + olen;
1411 tlv = os_malloc(tlv_len);
1413 ehdr = (struct eap_tlv_hdr *) tlv;
1414 ehdr->tlv_type = host_to_be16(PAC_TYPE_PAC_OPAQUE);
1415 ehdr->length = host_to_be16(olen);
1416 os_memcpy(ehdr + 1, pac->pac_opaque, olen);
1419 tls_connection_client_hello_ext(sm->ssl_ctx, data->ssl.conn,
1421 tlv, tlv_len) < 0) {
1422 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to add PAC-Opaque TLS "
1433 static int eap_fast_clear_pac_opaque_ext(struct eap_sm *sm,
1434 struct eap_fast_data *data)
1436 if (tls_connection_client_hello_ext(sm->ssl_ctx, data->ssl.conn,
1437 TLS_EXT_PAC_OPAQUE, NULL, 0) < 0) {
1438 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to remove PAC-Opaque "
1446 static int eap_fast_set_provisioning_ciphers(struct eap_sm *sm,
1447 struct eap_fast_data *data)
1452 if (data->provisioning_allowed & EAP_FAST_PROV_UNAUTH) {
1453 wpa_printf(MSG_DEBUG, "EAP-FAST: Enabling unauthenticated "
1454 "provisioning TLS cipher suites");
1455 ciphers[count++] = TLS_CIPHER_ANON_DH_AES128_SHA;
1458 if (data->provisioning_allowed & EAP_FAST_PROV_AUTH) {
1459 wpa_printf(MSG_DEBUG, "EAP-FAST: Enabling authenticated "
1460 "provisioning TLS cipher suites");
1461 ciphers[count++] = TLS_CIPHER_RSA_DHE_AES256_SHA;
1462 ciphers[count++] = TLS_CIPHER_RSA_DHE_AES128_SHA;
1463 ciphers[count++] = TLS_CIPHER_AES256_SHA;
1464 ciphers[count++] = TLS_CIPHER_AES128_SHA;
1465 ciphers[count++] = TLS_CIPHER_RC4_SHA;
1468 ciphers[count++] = TLS_CIPHER_NONE;
1470 if (tls_connection_set_cipher_list(sm->ssl_ctx, data->ssl.conn,
1472 wpa_printf(MSG_INFO, "EAP-FAST: Could not configure TLS "
1473 "cipher suites for provisioning");
1481 static int eap_fast_process_start(struct eap_sm *sm,
1482 struct eap_fast_data *data, u8 flags,
1483 const u8 *pos, size_t left)
1488 /* EAP-FAST Version negotiation (section 3.1) */
1489 wpa_printf(MSG_DEBUG, "EAP-FAST: Start (server ver=%d, own ver=%d)",
1490 flags & EAP_TLS_VERSION_MASK, data->fast_version);
1491 if ((flags & EAP_TLS_VERSION_MASK) < data->fast_version)
1492 data->fast_version = flags & EAP_TLS_VERSION_MASK;
1493 wpa_printf(MSG_DEBUG, "EAP-FAST: Using FAST version %d",
1494 data->fast_version);
1496 a_id = eap_fast_get_a_id(pos, left, &a_id_len);
1497 eap_fast_select_pac(data, a_id, a_id_len);
1499 if (data->resuming && data->current_pac) {
1500 wpa_printf(MSG_DEBUG, "EAP-FAST: Trying to resume session - "
1501 "do not add PAC-Opaque to TLS ClientHello");
1502 if (eap_fast_clear_pac_opaque_ext(sm, data) < 0)
1504 } else if (data->current_pac) {
1506 * PAC found for the A-ID and we are not resuming an old
1507 * session, so add PAC-Opaque extension to ClientHello.
1509 if (eap_fast_use_pac_opaque(sm, data, data->current_pac) < 0)
1512 /* No PAC found, so we must provision one. */
1513 if (!data->provisioning_allowed) {
1514 wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC found and "
1515 "provisioning disabled");
1518 wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC found - "
1519 "starting provisioning");
1520 if (eap_fast_set_provisioning_ciphers(sm, data) < 0 ||
1521 eap_fast_clear_pac_opaque_ext(sm, data) < 0)
1523 data->provisioning = 1;
1530 static struct wpabuf * eap_fast_process(struct eap_sm *sm, void *priv,
1531 struct eap_method_ret *ret,
1532 const struct wpabuf *reqData)
1534 const struct eap_hdr *req;
1538 struct wpabuf *resp;
1540 struct eap_fast_data *data = priv;
1543 pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_FAST, ret,
1544 reqData, &left, &flags);
1548 req = wpabuf_head(reqData);
1549 id = req->identifier;
1551 if (flags & EAP_TLS_FLAGS_START) {
1552 if (eap_fast_process_start(sm, data, flags, pos, left) < 0)
1555 left = 0; /* A-ID is not used in further packet processing */
1558 wpabuf_set(&msg, pos, left);
1561 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1563 /* Process tunneled (encrypted) phase 2 data. */
1564 res = eap_fast_decrypt(sm, data, ret, id, &msg, &resp);
1566 ret->methodState = METHOD_DONE;
1567 ret->decision = DECISION_FAIL;
1569 * Ack possible Alert that may have caused failure in
1575 if (sm->waiting_ext_cert_check && data->pending_resp) {
1576 struct eap_peer_config *config = eap_get_config(sm);
1578 if (config->pending_ext_cert_check ==
1579 EXT_CERT_CHECK_GOOD) {
1580 wpa_printf(MSG_DEBUG,
1581 "EAP-FAST: External certificate check succeeded - continue handshake");
1582 resp = data->pending_resp;
1583 data->pending_resp = NULL;
1584 sm->waiting_ext_cert_check = 0;
1588 if (config->pending_ext_cert_check ==
1589 EXT_CERT_CHECK_BAD) {
1590 wpa_printf(MSG_DEBUG,
1591 "EAP-FAST: External certificate check failed - force authentication failure");
1592 ret->methodState = METHOD_DONE;
1593 ret->decision = DECISION_FAIL;
1594 sm->waiting_ext_cert_check = 0;
1598 wpa_printf(MSG_DEBUG,
1599 "EAP-FAST: Continuing to wait external server certificate validation");
1603 /* Continue processing TLS handshake (phase 1). */
1604 res = eap_peer_tls_process_helper(sm, &data->ssl,
1606 data->fast_version, id, &msg,
1609 wpa_printf(MSG_DEBUG,
1610 "EAP-FAST: TLS processing failed");
1611 ret->methodState = METHOD_DONE;
1612 ret->decision = DECISION_FAIL;
1616 if (sm->waiting_ext_cert_check) {
1617 wpa_printf(MSG_DEBUG,
1618 "EAP-FAST: Waiting external server certificate validation");
1619 wpabuf_free(data->pending_resp);
1620 data->pending_resp = resp;
1624 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
1626 wpa_printf(MSG_DEBUG,
1627 "EAP-FAST: TLS done, proceed to Phase 2");
1628 if (data->provisioning &&
1629 (!(data->provisioning_allowed &
1630 EAP_FAST_PROV_AUTH) ||
1631 tls_get_cipher(sm->ssl_ctx, data->ssl.conn,
1632 cipher, sizeof(cipher)) < 0 ||
1633 os_strstr(cipher, "ADH-") ||
1634 os_strstr(cipher, "anon"))) {
1635 wpa_printf(MSG_DEBUG, "EAP-FAST: Using "
1636 "anonymous (unauthenticated) "
1638 data->anon_provisioning = 1;
1640 data->anon_provisioning = 0;
1642 if (eap_fast_derive_keys(sm, data) < 0) {
1643 wpa_printf(MSG_DEBUG,
1644 "EAP-FAST: Could not derive keys");
1645 ret->methodState = METHOD_DONE;
1646 ret->decision = DECISION_FAIL;
1654 * Application data included in the handshake message.
1656 wpabuf_free(data->pending_phase2_req);
1657 data->pending_phase2_req = resp;
1659 res = eap_fast_decrypt(sm, data, ret, id, &msg, &resp);
1665 return eap_peer_tls_build_ack(id, EAP_TYPE_FAST,
1666 data->fast_version);
1674 static Boolean eap_fast_has_reauth_data(struct eap_sm *sm, void *priv)
1676 struct eap_fast_data *data = priv;
1677 return tls_connection_established(sm->ssl_ctx, data->ssl.conn);
1681 static void eap_fast_deinit_for_reauth(struct eap_sm *sm, void *priv)
1683 struct eap_fast_data *data = priv;
1684 os_free(data->key_block_p);
1685 data->key_block_p = NULL;
1686 wpabuf_free(data->pending_phase2_req);
1687 data->pending_phase2_req = NULL;
1688 wpabuf_free(data->pending_resp);
1689 data->pending_resp = NULL;
1693 static void * eap_fast_init_for_reauth(struct eap_sm *sm, void *priv)
1695 struct eap_fast_data *data = priv;
1696 if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
1700 os_memset(data->key_data, 0, EAP_FAST_KEY_LEN);
1701 os_memset(data->emsk, 0, EAP_EMSK_LEN);
1702 os_free(data->session_id);
1703 data->session_id = NULL;
1704 if (data->phase2_priv && data->phase2_method &&
1705 data->phase2_method->init_for_reauth)
1706 data->phase2_method->init_for_reauth(sm, data->phase2_priv);
1707 data->phase2_success = 0;
1709 data->provisioning = 0;
1710 data->anon_provisioning = 0;
1711 data->simck_idx = 0;
1717 static int eap_fast_get_status(struct eap_sm *sm, void *priv, char *buf,
1718 size_t buflen, int verbose)
1720 struct eap_fast_data *data = priv;
1723 len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
1724 if (data->phase2_method) {
1725 ret = os_snprintf(buf + len, buflen - len,
1726 "EAP-FAST Phase2 method=%s\n",
1727 data->phase2_method->name);
1728 if (os_snprintf_error(buflen - len, ret))
1736 static Boolean eap_fast_isKeyAvailable(struct eap_sm *sm, void *priv)
1738 struct eap_fast_data *data = priv;
1739 return data->success;
1743 static u8 * eap_fast_getKey(struct eap_sm *sm, void *priv, size_t *len)
1745 struct eap_fast_data *data = priv;
1751 key = os_malloc(EAP_FAST_KEY_LEN);
1755 *len = EAP_FAST_KEY_LEN;
1756 os_memcpy(key, data->key_data, 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_malloc(data->id_len);
1774 *len = data->id_len;
1775 os_memcpy(id, data->session_id, data->id_len);
1781 static u8 * eap_fast_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
1783 struct eap_fast_data *data = priv;
1789 key = os_malloc(EAP_EMSK_LEN);
1793 *len = EAP_EMSK_LEN;
1794 os_memcpy(key, data->emsk, EAP_EMSK_LEN);
1800 int eap_peer_fast_register(void)
1802 struct eap_method *eap;
1804 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
1805 EAP_VENDOR_IETF, EAP_TYPE_FAST, "FAST");
1809 eap->init = eap_fast_init;
1810 eap->deinit = eap_fast_deinit;
1811 eap->process = eap_fast_process;
1812 eap->isKeyAvailable = eap_fast_isKeyAvailable;
1813 eap->getKey = eap_fast_getKey;
1814 eap->getSessionId = eap_fast_get_session_id;
1815 eap->get_status = eap_fast_get_status;
1817 eap->has_reauth_data = eap_fast_has_reauth_data;
1818 eap->deinit_for_reauth = eap_fast_deinit_for_reauth;
1819 eap->init_for_reauth = eap_fast_init_for_reauth;
1821 eap->get_emsk = eap_fast_get_emsk;
1823 return eap_peer_method_register(eap);