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_free(data->pending_phase2_req);
254 wpabuf_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 wpabuf_free(data->pending_phase2_req);
489 data->pending_phase2_req = wpabuf_alloc_copy(hdr, len);
490 } else if (*resp == NULL)
497 static struct wpabuf * eap_fast_tlv_nak(int vendor_id, int tlv_type)
500 struct eap_tlv_nak_tlv *nak;
501 buf = wpabuf_alloc(sizeof(*nak));
504 nak = wpabuf_put(buf, sizeof(*nak));
505 nak->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY | EAP_TLV_NAK_TLV);
506 nak->length = host_to_be16(6);
507 nak->vendor_id = host_to_be32(vendor_id);
508 nak->nak_type = host_to_be16(tlv_type);
513 static struct wpabuf * eap_fast_tlv_result(int status, int intermediate)
516 struct eap_tlv_intermediate_result_tlv *result;
517 buf = wpabuf_alloc(sizeof(*result));
520 wpa_printf(MSG_DEBUG, "EAP-FAST: Add %sResult TLV(status=%d)",
521 intermediate ? "Intermediate " : "", status);
522 result = wpabuf_put(buf, sizeof(*result));
523 result->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
525 EAP_TLV_INTERMEDIATE_RESULT_TLV :
526 EAP_TLV_RESULT_TLV));
527 result->length = host_to_be16(2);
528 result->status = host_to_be16(status);
533 static struct wpabuf * eap_fast_tlv_pac_ack(void)
536 struct eap_tlv_result_tlv *res;
537 struct eap_tlv_pac_ack_tlv *ack;
539 buf = wpabuf_alloc(sizeof(*res) + sizeof(*ack));
543 wpa_printf(MSG_DEBUG, "EAP-FAST: Add PAC TLV (ack)");
544 ack = wpabuf_put(buf, sizeof(*ack));
545 ack->tlv_type = host_to_be16(EAP_TLV_PAC_TLV |
546 EAP_TLV_TYPE_MANDATORY);
547 ack->length = host_to_be16(sizeof(*ack) - sizeof(struct eap_tlv_hdr));
548 ack->pac_type = host_to_be16(PAC_TYPE_PAC_ACKNOWLEDGEMENT);
549 ack->pac_len = host_to_be16(2);
550 ack->result = host_to_be16(EAP_TLV_RESULT_SUCCESS);
556 static struct wpabuf * eap_fast_process_eap_payload_tlv(
557 struct eap_sm *sm, struct eap_fast_data *data,
558 struct eap_method_ret *ret,
559 u8 *eap_payload_tlv, size_t eap_payload_tlv_len)
562 struct wpabuf *resp = NULL;
564 if (eap_payload_tlv_len < sizeof(*hdr)) {
565 wpa_printf(MSG_DEBUG, "EAP-FAST: too short EAP "
566 "Payload TLV (len=%lu)",
567 (unsigned long) eap_payload_tlv_len);
571 hdr = (struct eap_hdr *) eap_payload_tlv;
572 if (be_to_host16(hdr->length) > eap_payload_tlv_len) {
573 wpa_printf(MSG_DEBUG, "EAP-FAST: EAP packet overflow in "
578 if (hdr->code != EAP_CODE_REQUEST) {
579 wpa_printf(MSG_INFO, "EAP-FAST: Unexpected code=%d in "
580 "Phase 2 EAP header", hdr->code);
584 if (eap_fast_phase2_request(sm, data, ret, hdr, &resp)) {
585 wpa_printf(MSG_INFO, "EAP-FAST: Phase2 Request processing "
590 return eap_fast_tlv_eap_payload(resp);
594 static int eap_fast_validate_crypto_binding(
595 struct eap_tlv_crypto_binding_tlv *_bind)
597 wpa_printf(MSG_DEBUG, "EAP-FAST: Crypto-Binding TLV: Version %d "
598 "Received Version %d SubType %d",
599 _bind->version, _bind->received_version, _bind->subtype);
600 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE",
601 _bind->nonce, sizeof(_bind->nonce));
602 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC",
603 _bind->compound_mac, sizeof(_bind->compound_mac));
605 if (_bind->version != EAP_FAST_VERSION ||
606 _bind->received_version != EAP_FAST_VERSION ||
607 _bind->subtype != EAP_TLV_CRYPTO_BINDING_SUBTYPE_REQUEST) {
608 wpa_printf(MSG_INFO, "EAP-FAST: Invalid version/subtype in "
609 "Crypto-Binding TLV: Version %d "
610 "Received Version %d SubType %d",
611 _bind->version, _bind->received_version,
620 static void eap_fast_write_crypto_binding(
621 struct eap_tlv_crypto_binding_tlv *rbind,
622 struct eap_tlv_crypto_binding_tlv *_bind, const u8 *cmk)
624 rbind->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
625 EAP_TLV_CRYPTO_BINDING_TLV);
626 rbind->length = host_to_be16(sizeof(*rbind) -
627 sizeof(struct eap_tlv_hdr));
628 rbind->version = EAP_FAST_VERSION;
629 rbind->received_version = _bind->version;
630 rbind->subtype = EAP_TLV_CRYPTO_BINDING_SUBTYPE_RESPONSE;
631 os_memcpy(rbind->nonce, _bind->nonce, sizeof(_bind->nonce));
632 inc_byte_array(rbind->nonce, sizeof(rbind->nonce));
633 hmac_sha1(cmk, EAP_FAST_CMK_LEN, (u8 *) rbind, sizeof(*rbind),
634 rbind->compound_mac);
636 wpa_printf(MSG_DEBUG, "EAP-FAST: Reply Crypto-Binding TLV: Version %d "
637 "Received Version %d SubType %d",
638 rbind->version, rbind->received_version, rbind->subtype);
639 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE",
640 rbind->nonce, sizeof(rbind->nonce));
641 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC",
642 rbind->compound_mac, sizeof(rbind->compound_mac));
646 static int eap_fast_get_phase2_key(struct eap_sm *sm,
647 struct eap_fast_data *data,
648 u8 *isk, size_t isk_len)
653 os_memset(isk, 0, isk_len);
655 if (data->phase2_method == NULL || data->phase2_priv == NULL) {
656 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 method not "
661 if (data->phase2_method->isKeyAvailable == NULL ||
662 data->phase2_method->getKey == NULL)
665 if (!data->phase2_method->isKeyAvailable(sm, data->phase2_priv) ||
666 (key = data->phase2_method->getKey(sm, data->phase2_priv,
667 &key_len)) == NULL) {
668 wpa_printf(MSG_DEBUG, "EAP-FAST: Could not get key material "
673 if (key_len > isk_len)
676 data->phase2_method->vendor == EAP_VENDOR_IETF &&
677 data->phase2_method->method == EAP_TYPE_MSCHAPV2) {
679 * EAP-FAST uses reverse order for MS-MPPE keys when deriving
680 * MSK from EAP-MSCHAPv2. Swap the keys here to get the correct
681 * ISK for EAP-FAST cryptobinding.
683 os_memcpy(isk, key + 16, 16);
684 os_memcpy(isk + 16, key, 16);
686 os_memcpy(isk, key, key_len);
693 static int eap_fast_get_cmk(struct eap_sm *sm, struct eap_fast_data *data,
696 u8 isk[32], imck[60];
698 wpa_printf(MSG_DEBUG, "EAP-FAST: Determining CMK[%d] for Compound MIC "
699 "calculation", data->simck_idx + 1);
702 * RFC 4851, Section 5.2:
703 * IMCK[j] = T-PRF(S-IMCK[j-1], "Inner Methods Compound Keys",
705 * S-IMCK[j] = first 40 octets of IMCK[j]
706 * CMK[j] = last 20 octets of IMCK[j]
709 if (eap_fast_get_phase2_key(sm, data, isk, sizeof(isk)) < 0)
711 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: ISK[j]", isk, sizeof(isk));
712 if (sha1_t_prf(data->simck, EAP_FAST_SIMCK_LEN,
713 "Inner Methods Compound Keys",
714 isk, sizeof(isk), imck, sizeof(imck)) < 0)
717 os_memcpy(data->simck, imck, EAP_FAST_SIMCK_LEN);
718 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: S-IMCK[j]",
719 data->simck, EAP_FAST_SIMCK_LEN);
720 os_memcpy(cmk, imck + EAP_FAST_SIMCK_LEN, EAP_FAST_CMK_LEN);
721 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: CMK[j]",
722 cmk, EAP_FAST_CMK_LEN);
728 static u8 * eap_fast_write_pac_request(u8 *pos, u16 pac_type)
730 struct eap_tlv_hdr *pac;
731 struct eap_tlv_request_action_tlv *act;
732 struct eap_tlv_pac_type_tlv *type;
734 act = (struct eap_tlv_request_action_tlv *) pos;
735 act->tlv_type = host_to_be16(EAP_TLV_REQUEST_ACTION_TLV);
736 act->length = host_to_be16(2);
737 act->action = host_to_be16(EAP_TLV_ACTION_PROCESS_TLV);
739 pac = (struct eap_tlv_hdr *) (act + 1);
740 pac->tlv_type = host_to_be16(EAP_TLV_PAC_TLV);
741 pac->length = host_to_be16(sizeof(*type));
743 type = (struct eap_tlv_pac_type_tlv *) (pac + 1);
744 type->tlv_type = host_to_be16(PAC_TYPE_PAC_TYPE);
745 type->length = host_to_be16(2);
746 type->pac_type = host_to_be16(pac_type);
748 return (u8 *) (type + 1);
752 static struct wpabuf * eap_fast_process_crypto_binding(
753 struct eap_sm *sm, struct eap_fast_data *data,
754 struct eap_method_ret *ret,
755 struct eap_tlv_crypto_binding_tlv *_bind, size_t bind_len)
759 u8 cmk[EAP_FAST_CMK_LEN], cmac[SHA1_MAC_LEN];
763 if (eap_fast_validate_crypto_binding(_bind) < 0)
766 if (eap_fast_get_cmk(sm, data, cmk) < 0)
769 /* Validate received Compound MAC */
770 os_memcpy(cmac, _bind->compound_mac, sizeof(cmac));
771 os_memset(_bind->compound_mac, 0, sizeof(cmac));
772 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Crypto-Binding TLV for Compound "
773 "MAC calculation", (u8 *) _bind, bind_len);
774 hmac_sha1(cmk, EAP_FAST_CMK_LEN, (u8 *) _bind, bind_len,
775 _bind->compound_mac);
776 res = os_memcmp_const(cmac, _bind->compound_mac, sizeof(cmac));
777 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Received Compound MAC",
779 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Calculated Compound MAC",
780 _bind->compound_mac, sizeof(cmac));
782 wpa_printf(MSG_INFO, "EAP-FAST: Compound MAC did not match");
783 os_memcpy(_bind->compound_mac, cmac, sizeof(cmac));
788 * Compound MAC was valid, so authentication succeeded. Reply with
789 * crypto binding to allow server to complete authentication.
792 len = sizeof(struct eap_tlv_crypto_binding_tlv);
793 resp = wpabuf_alloc(len);
797 if (!data->anon_provisioning && data->phase2_success &&
798 eap_fast_derive_msk(data) < 0) {
799 wpa_printf(MSG_INFO, "EAP-FAST: Failed to generate MSK");
800 ret->methodState = METHOD_DONE;
801 ret->decision = DECISION_FAIL;
802 data->phase2_success = 0;
807 if (!data->anon_provisioning && data->phase2_success) {
808 os_free(data->session_id);
809 data->session_id = eap_peer_tls_derive_session_id(
810 sm, &data->ssl, EAP_TYPE_FAST, &data->id_len);
811 if (data->session_id) {
812 wpa_hexdump(MSG_DEBUG, "EAP-FAST: Derived Session-Id",
813 data->session_id, data->id_len);
815 wpa_printf(MSG_ERROR, "EAP-FAST: Failed to derive "
822 pos = wpabuf_put(resp, sizeof(struct eap_tlv_crypto_binding_tlv));
823 eap_fast_write_crypto_binding((struct eap_tlv_crypto_binding_tlv *)
830 static void eap_fast_parse_pac_tlv(struct eap_fast_pac *entry, int type,
831 u8 *pos, size_t len, int *pac_key_found)
833 switch (type & 0x7fff) {
834 case PAC_TYPE_PAC_KEY:
835 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: PAC-Key", pos, len);
836 if (len != EAP_FAST_PAC_KEY_LEN) {
837 wpa_printf(MSG_DEBUG, "EAP-FAST: Invalid PAC-Key "
838 "length %lu", (unsigned long) len);
842 os_memcpy(entry->pac_key, pos, len);
844 case PAC_TYPE_PAC_OPAQUE:
845 wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Opaque", pos, len);
846 entry->pac_opaque = pos;
847 entry->pac_opaque_len = len;
849 case PAC_TYPE_PAC_INFO:
850 wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Info", pos, len);
851 entry->pac_info = pos;
852 entry->pac_info_len = len;
855 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignored unknown PAC type %d",
862 static int eap_fast_process_pac_tlv(struct eap_fast_pac *entry,
863 u8 *pac, size_t pac_len)
865 struct pac_tlv_hdr *hdr;
868 int type, pac_key_found = 0;
873 while (left > sizeof(*hdr)) {
874 hdr = (struct pac_tlv_hdr *) pos;
875 type = be_to_host16(hdr->type);
876 len = be_to_host16(hdr->len);
878 left -= sizeof(*hdr);
880 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV overrun "
881 "(type=%d len=%lu left=%lu)",
882 type, (unsigned long) len,
883 (unsigned long) left);
887 eap_fast_parse_pac_tlv(entry, type, pos, len, &pac_key_found);
893 if (!pac_key_found || !entry->pac_opaque || !entry->pac_info) {
894 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV does not include "
895 "all the required fields");
903 static int eap_fast_parse_pac_info(struct eap_fast_pac *entry, int type,
910 switch (type & 0x7fff) {
911 case PAC_TYPE_CRED_LIFETIME:
913 wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Info - "
914 "Invalid CRED_LIFETIME length - ignored",
920 * This is not currently saved separately in PAC files since
921 * the server can automatically initiate PAC update when
922 * needed. Anyway, the information is available from PAC-Info
923 * dump if it is needed for something in the future.
925 lifetime = WPA_GET_BE32(pos);
927 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info - CRED_LIFETIME %d "
929 lifetime, (lifetime - (u32) now.sec) / 86400);
932 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - A-ID",
935 entry->a_id_len = len;
938 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - I-ID",
941 entry->i_id_len = len;
943 case PAC_TYPE_A_ID_INFO:
944 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - A-ID-Info",
946 entry->a_id_info = pos;
947 entry->a_id_info_len = len;
949 case PAC_TYPE_PAC_TYPE:
950 /* RFC 5422, Section 4.2.6 - PAC-Type TLV */
952 wpa_printf(MSG_INFO, "EAP-FAST: Invalid PAC-Type "
953 "length %lu (expected 2)",
954 (unsigned long) len);
955 wpa_hexdump_ascii(MSG_DEBUG,
956 "EAP-FAST: PAC-Info - PAC-Type",
960 pac_type = WPA_GET_BE16(pos);
961 if (pac_type != PAC_TYPE_TUNNEL_PAC &&
962 pac_type != PAC_TYPE_USER_AUTHORIZATION &&
963 pac_type != PAC_TYPE_MACHINE_AUTHENTICATION) {
964 wpa_printf(MSG_INFO, "EAP-FAST: Unsupported PAC Type "
969 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info - PAC-Type %d",
971 entry->pac_type = pac_type;
974 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignored unknown PAC-Info "
983 static int eap_fast_process_pac_info(struct eap_fast_pac *entry)
985 struct pac_tlv_hdr *hdr;
990 /* RFC 5422, Section 4.2.4 */
992 /* PAC-Type defaults to Tunnel PAC (Type 1) */
993 entry->pac_type = PAC_TYPE_TUNNEL_PAC;
995 pos = entry->pac_info;
996 left = entry->pac_info_len;
997 while (left > sizeof(*hdr)) {
998 hdr = (struct pac_tlv_hdr *) pos;
999 type = be_to_host16(hdr->type);
1000 len = be_to_host16(hdr->len);
1001 pos += sizeof(*hdr);
1002 left -= sizeof(*hdr);
1004 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info overrun "
1005 "(type=%d len=%lu left=%lu)",
1006 type, (unsigned long) len,
1007 (unsigned long) left);
1011 if (eap_fast_parse_pac_info(entry, type, pos, len) < 0)
1018 if (entry->a_id == NULL || entry->a_id_info == NULL) {
1019 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info does not include "
1020 "all the required fields");
1028 static struct wpabuf * eap_fast_process_pac(struct eap_sm *sm,
1029 struct eap_fast_data *data,
1030 struct eap_method_ret *ret,
1031 u8 *pac, size_t pac_len)
1033 struct eap_peer_config *config = eap_get_config(sm);
1034 struct eap_fast_pac entry;
1036 os_memset(&entry, 0, sizeof(entry));
1037 if (eap_fast_process_pac_tlv(&entry, pac, pac_len) ||
1038 eap_fast_process_pac_info(&entry))
1041 eap_fast_add_pac(&data->pac, &data->current_pac, &entry);
1042 eap_fast_pac_list_truncate(data->pac, data->max_pac_list_len);
1043 if (data->use_pac_binary_format)
1044 eap_fast_save_pac_bin(sm, data->pac, config->pac_file);
1046 eap_fast_save_pac(sm, data->pac, config->pac_file);
1048 if (data->provisioning) {
1049 if (data->anon_provisioning) {
1051 * Unauthenticated provisioning does not provide keying
1052 * material and must end with an EAP-Failure.
1053 * Authentication will be done separately after this.
1056 ret->decision = DECISION_FAIL;
1059 * Server may or may not allow authenticated
1060 * provisioning also for key generation.
1062 ret->decision = DECISION_COND_SUCC;
1064 wpa_printf(MSG_DEBUG, "EAP-FAST: Send PAC-Acknowledgement TLV "
1065 "- Provisioning completed successfully");
1066 sm->expected_failure = 1;
1069 * This is PAC refreshing, i.e., normal authentication that is
1070 * expected to be completed with an EAP-Success. However,
1071 * RFC 5422, Section 3.5 allows EAP-Failure to be sent even
1072 * after protected success exchange in case of EAP-Fast
1073 * provisioning, so we better use DECISION_COND_SUCC here
1074 * instead of DECISION_UNCOND_SUCC.
1076 wpa_printf(MSG_DEBUG, "EAP-FAST: Send PAC-Acknowledgement TLV "
1077 "- PAC refreshing completed successfully");
1078 ret->decision = DECISION_COND_SUCC;
1080 ret->methodState = METHOD_DONE;
1081 return eap_fast_tlv_pac_ack();
1085 static int eap_fast_parse_decrypted(struct wpabuf *decrypted,
1086 struct eap_fast_tlv_parse *tlv,
1087 struct wpabuf **resp)
1089 int mandatory, tlv_type, res;
1093 os_memset(tlv, 0, sizeof(*tlv));
1095 /* Parse TLVs from the decrypted Phase 2 data */
1096 pos = wpabuf_mhead(decrypted);
1097 end = pos + wpabuf_len(decrypted);
1098 while (end - pos > 4) {
1099 mandatory = pos[0] & 0x80;
1100 tlv_type = WPA_GET_BE16(pos) & 0x3fff;
1102 len = WPA_GET_BE16(pos);
1104 if (len > (size_t) (end - pos)) {
1105 wpa_printf(MSG_INFO, "EAP-FAST: TLV overflow");
1108 wpa_printf(MSG_DEBUG, "EAP-FAST: Received Phase 2: "
1109 "TLV type %d length %u%s",
1110 tlv_type, (unsigned int) len,
1111 mandatory ? " (mandatory)" : "");
1113 res = eap_fast_parse_tlv(tlv, tlv_type, pos, len);
1118 wpa_printf(MSG_DEBUG, "EAP-FAST: Nak unknown "
1119 "mandatory TLV type %d", tlv_type);
1120 *resp = eap_fast_tlv_nak(0, tlv_type);
1123 wpa_printf(MSG_DEBUG, "EAP-FAST: ignored "
1124 "unknown optional TLV type %d",
1136 static int eap_fast_encrypt_response(struct eap_sm *sm,
1137 struct eap_fast_data *data,
1138 struct wpabuf *resp,
1139 u8 identifier, struct wpabuf **out_data)
1144 wpa_hexdump_buf(MSG_DEBUG, "EAP-FAST: Encrypting Phase 2 data",
1146 if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_FAST,
1147 data->fast_version, identifier,
1149 wpa_printf(MSG_INFO, "EAP-FAST: Failed to encrypt a Phase 2 "
1158 static struct wpabuf * eap_fast_pac_request(void)
1163 tmp = wpabuf_alloc(sizeof(struct eap_tlv_hdr) +
1164 sizeof(struct eap_tlv_request_action_tlv) +
1165 sizeof(struct eap_tlv_pac_type_tlv));
1169 pos = wpabuf_put(tmp, 0);
1170 pos2 = eap_fast_write_pac_request(pos, PAC_TYPE_TUNNEL_PAC);
1171 wpabuf_put(tmp, pos2 - pos);
1176 static int eap_fast_process_decrypted(struct eap_sm *sm,
1177 struct eap_fast_data *data,
1178 struct eap_method_ret *ret,
1180 struct wpabuf *decrypted,
1181 struct wpabuf **out_data)
1183 struct wpabuf *resp = NULL, *tmp;
1184 struct eap_fast_tlv_parse tlv;
1187 if (eap_fast_parse_decrypted(decrypted, &tlv, &resp) < 0)
1190 return eap_fast_encrypt_response(sm, data, resp,
1191 identifier, out_data);
1193 if (tlv.result == EAP_TLV_RESULT_FAILURE) {
1194 resp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 0);
1195 return eap_fast_encrypt_response(sm, data, resp,
1196 identifier, out_data);
1199 if (tlv.iresult == EAP_TLV_RESULT_FAILURE) {
1200 resp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 1);
1201 return eap_fast_encrypt_response(sm, data, resp,
1202 identifier, out_data);
1205 if (tlv.crypto_binding) {
1206 tmp = eap_fast_process_crypto_binding(sm, data, ret,
1208 tlv.crypto_binding_len);
1212 resp = wpabuf_concat(resp, tmp);
1215 if (tlv.iresult == EAP_TLV_RESULT_SUCCESS) {
1216 tmp = eap_fast_tlv_result(failed ? EAP_TLV_RESULT_FAILURE :
1217 EAP_TLV_RESULT_SUCCESS, 1);
1218 resp = wpabuf_concat(resp, tmp);
1221 if (tlv.eap_payload_tlv) {
1222 tmp = eap_fast_process_eap_payload_tlv(
1223 sm, data, ret, tlv.eap_payload_tlv,
1224 tlv.eap_payload_tlv_len);
1225 resp = wpabuf_concat(resp, tmp);
1228 if (tlv.pac && tlv.result != EAP_TLV_RESULT_SUCCESS) {
1229 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV without Result TLV "
1230 "acknowledging success");
1232 } else if (tlv.pac && tlv.result == EAP_TLV_RESULT_SUCCESS) {
1233 tmp = eap_fast_process_pac(sm, data, ret, tlv.pac,
1235 resp = wpabuf_concat(resp, tmp);
1238 if (data->current_pac == NULL && data->provisioning &&
1239 !data->anon_provisioning && !tlv.pac &&
1240 (tlv.iresult == EAP_TLV_RESULT_SUCCESS ||
1241 tlv.result == EAP_TLV_RESULT_SUCCESS)) {
1243 * Need to request Tunnel PAC when using authenticated
1246 wpa_printf(MSG_DEBUG, "EAP-FAST: Request Tunnel PAC");
1247 tmp = eap_fast_pac_request();
1248 resp = wpabuf_concat(resp, tmp);
1251 if (tlv.result == EAP_TLV_RESULT_SUCCESS && !failed) {
1252 tmp = eap_fast_tlv_result(EAP_TLV_RESULT_SUCCESS, 0);
1253 resp = wpabuf_concat(tmp, resp);
1254 } else if (failed) {
1255 tmp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 0);
1256 resp = wpabuf_concat(tmp, resp);
1259 if (resp && tlv.result == EAP_TLV_RESULT_SUCCESS && !failed &&
1260 tlv.crypto_binding && data->phase2_success) {
1261 if (data->anon_provisioning) {
1262 wpa_printf(MSG_DEBUG, "EAP-FAST: Unauthenticated "
1263 "provisioning completed successfully.");
1264 ret->methodState = METHOD_DONE;
1265 ret->decision = DECISION_FAIL;
1266 sm->expected_failure = 1;
1268 wpa_printf(MSG_DEBUG, "EAP-FAST: Authentication "
1269 "completed successfully.");
1270 if (data->provisioning)
1271 ret->methodState = METHOD_MAY_CONT;
1273 ret->methodState = METHOD_DONE;
1274 ret->decision = DECISION_UNCOND_SUCC;
1279 wpa_printf(MSG_DEBUG, "EAP-FAST: No recognized TLVs - send "
1280 "empty response packet");
1281 resp = wpabuf_alloc(1);
1284 return eap_fast_encrypt_response(sm, data, resp, identifier,
1289 static int eap_fast_decrypt(struct eap_sm *sm, struct eap_fast_data *data,
1290 struct eap_method_ret *ret, u8 identifier,
1291 const struct wpabuf *in_data,
1292 struct wpabuf **out_data)
1294 struct wpabuf *in_decrypted;
1297 wpa_printf(MSG_DEBUG, "EAP-FAST: Received %lu bytes encrypted data for"
1298 " Phase 2", (unsigned long) wpabuf_len(in_data));
1300 if (data->pending_phase2_req) {
1301 wpa_printf(MSG_DEBUG, "EAP-FAST: Pending Phase 2 request - "
1302 "skip decryption and use old data");
1303 /* Clear TLS reassembly state. */
1304 eap_peer_tls_reset_input(&data->ssl);
1306 in_decrypted = data->pending_phase2_req;
1307 data->pending_phase2_req = NULL;
1311 if (wpabuf_len(in_data) == 0) {
1312 /* Received TLS ACK - requesting more fragments */
1313 return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_FAST,
1315 identifier, NULL, out_data);
1318 res = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
1323 wpa_hexdump_buf(MSG_MSGDUMP, "EAP-FAST: Decrypted Phase 2 TLV(s)",
1326 if (wpabuf_len(in_decrypted) < 4) {
1327 wpa_printf(MSG_INFO, "EAP-FAST: Too short Phase 2 "
1328 "TLV frame (len=%lu)",
1329 (unsigned long) wpabuf_len(in_decrypted));
1330 wpabuf_free(in_decrypted);
1334 res = eap_fast_process_decrypted(sm, data, ret, identifier,
1335 in_decrypted, out_data);
1337 wpabuf_free(in_decrypted);
1343 static const u8 * eap_fast_get_a_id(const u8 *buf, size_t len, size_t *id_len)
1346 const struct pac_tlv_hdr *hdr;
1349 * Parse authority identity (A-ID) from the EAP-FAST/Start. This
1350 * supports both raw A-ID and one inside an A-ID TLV.
1354 if (len > sizeof(*hdr)) {
1356 hdr = (const struct pac_tlv_hdr *) buf;
1357 tlen = be_to_host16(hdr->len);
1358 if (be_to_host16(hdr->type) == PAC_TYPE_A_ID &&
1359 sizeof(*hdr) + tlen <= len) {
1360 wpa_printf(MSG_DEBUG, "EAP-FAST: A-ID was in TLV "
1362 a_id = (const u8 *) (hdr + 1);
1366 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: A-ID", a_id, *id_len);
1372 static void eap_fast_select_pac(struct eap_fast_data *data,
1373 const u8 *a_id, size_t a_id_len)
1375 data->current_pac = eap_fast_get_pac(data->pac, a_id, a_id_len,
1376 PAC_TYPE_TUNNEL_PAC);
1377 if (data->current_pac == NULL) {
1379 * Tunnel PAC was not available for this A-ID. Try to use
1380 * Machine Authentication PAC, if one is available.
1382 data->current_pac = eap_fast_get_pac(
1383 data->pac, a_id, a_id_len,
1384 PAC_TYPE_MACHINE_AUTHENTICATION);
1387 if (data->current_pac) {
1388 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC found for this A-ID "
1389 "(PAC-Type %d)", data->current_pac->pac_type);
1390 wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-FAST: A-ID-Info",
1391 data->current_pac->a_id_info,
1392 data->current_pac->a_id_info_len);
1397 static int eap_fast_use_pac_opaque(struct eap_sm *sm,
1398 struct eap_fast_data *data,
1399 struct eap_fast_pac *pac)
1402 size_t tlv_len, olen;
1403 struct eap_tlv_hdr *ehdr;
1405 olen = pac->pac_opaque_len;
1406 tlv_len = sizeof(*ehdr) + olen;
1407 tlv = os_malloc(tlv_len);
1409 ehdr = (struct eap_tlv_hdr *) tlv;
1410 ehdr->tlv_type = host_to_be16(PAC_TYPE_PAC_OPAQUE);
1411 ehdr->length = host_to_be16(olen);
1412 os_memcpy(ehdr + 1, pac->pac_opaque, olen);
1415 tls_connection_client_hello_ext(sm->ssl_ctx, data->ssl.conn,
1417 tlv, tlv_len) < 0) {
1418 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to add PAC-Opaque TLS "
1429 static int eap_fast_clear_pac_opaque_ext(struct eap_sm *sm,
1430 struct eap_fast_data *data)
1432 if (tls_connection_client_hello_ext(sm->ssl_ctx, data->ssl.conn,
1433 TLS_EXT_PAC_OPAQUE, NULL, 0) < 0) {
1434 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to remove PAC-Opaque "
1442 static int eap_fast_set_provisioning_ciphers(struct eap_sm *sm,
1443 struct eap_fast_data *data)
1448 if (data->provisioning_allowed & EAP_FAST_PROV_UNAUTH) {
1449 wpa_printf(MSG_DEBUG, "EAP-FAST: Enabling unauthenticated "
1450 "provisioning TLS cipher suites");
1451 ciphers[count++] = TLS_CIPHER_ANON_DH_AES128_SHA;
1454 if (data->provisioning_allowed & EAP_FAST_PROV_AUTH) {
1455 wpa_printf(MSG_DEBUG, "EAP-FAST: Enabling authenticated "
1456 "provisioning TLS cipher suites");
1457 ciphers[count++] = TLS_CIPHER_RSA_DHE_AES256_SHA;
1458 ciphers[count++] = TLS_CIPHER_RSA_DHE_AES128_SHA;
1459 ciphers[count++] = TLS_CIPHER_AES256_SHA;
1460 ciphers[count++] = TLS_CIPHER_AES128_SHA;
1461 ciphers[count++] = TLS_CIPHER_RC4_SHA;
1464 ciphers[count++] = TLS_CIPHER_NONE;
1466 if (tls_connection_set_cipher_list(sm->ssl_ctx, data->ssl.conn,
1468 wpa_printf(MSG_INFO, "EAP-FAST: Could not configure TLS "
1469 "cipher suites for provisioning");
1477 static int eap_fast_process_start(struct eap_sm *sm,
1478 struct eap_fast_data *data, u8 flags,
1479 const u8 *pos, size_t left)
1484 /* EAP-FAST Version negotiation (section 3.1) */
1485 wpa_printf(MSG_DEBUG, "EAP-FAST: Start (server ver=%d, own ver=%d)",
1486 flags & EAP_TLS_VERSION_MASK, data->fast_version);
1487 if ((flags & EAP_TLS_VERSION_MASK) < data->fast_version)
1488 data->fast_version = flags & EAP_TLS_VERSION_MASK;
1489 wpa_printf(MSG_DEBUG, "EAP-FAST: Using FAST version %d",
1490 data->fast_version);
1492 a_id = eap_fast_get_a_id(pos, left, &a_id_len);
1493 eap_fast_select_pac(data, a_id, a_id_len);
1495 if (data->resuming && data->current_pac) {
1496 wpa_printf(MSG_DEBUG, "EAP-FAST: Trying to resume session - "
1497 "do not add PAC-Opaque to TLS ClientHello");
1498 if (eap_fast_clear_pac_opaque_ext(sm, data) < 0)
1500 } else if (data->current_pac) {
1502 * PAC found for the A-ID and we are not resuming an old
1503 * session, so add PAC-Opaque extension to ClientHello.
1505 if (eap_fast_use_pac_opaque(sm, data, data->current_pac) < 0)
1508 /* No PAC found, so we must provision one. */
1509 if (!data->provisioning_allowed) {
1510 wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC found and "
1511 "provisioning disabled");
1514 wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC found - "
1515 "starting provisioning");
1516 if (eap_fast_set_provisioning_ciphers(sm, data) < 0 ||
1517 eap_fast_clear_pac_opaque_ext(sm, data) < 0)
1519 data->provisioning = 1;
1526 static struct wpabuf * eap_fast_process(struct eap_sm *sm, void *priv,
1527 struct eap_method_ret *ret,
1528 const struct wpabuf *reqData)
1530 const struct eap_hdr *req;
1534 struct wpabuf *resp;
1536 struct eap_fast_data *data = priv;
1539 pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_FAST, ret,
1540 reqData, &left, &flags);
1544 req = wpabuf_head(reqData);
1545 id = req->identifier;
1547 if (flags & EAP_TLS_FLAGS_START) {
1548 if (eap_fast_process_start(sm, data, flags, pos, left) < 0)
1551 left = 0; /* A-ID is not used in further packet processing */
1554 wpabuf_set(&msg, pos, left);
1557 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1559 /* Process tunneled (encrypted) phase 2 data. */
1560 res = eap_fast_decrypt(sm, data, ret, id, &msg, &resp);
1562 ret->methodState = METHOD_DONE;
1563 ret->decision = DECISION_FAIL;
1565 * Ack possible Alert that may have caused failure in
1571 if (sm->waiting_ext_cert_check && data->pending_resp) {
1572 struct eap_peer_config *config = eap_get_config(sm);
1574 if (config->pending_ext_cert_check ==
1575 EXT_CERT_CHECK_GOOD) {
1576 wpa_printf(MSG_DEBUG,
1577 "EAP-FAST: External certificate check succeeded - continue handshake");
1578 resp = data->pending_resp;
1579 data->pending_resp = NULL;
1580 sm->waiting_ext_cert_check = 0;
1584 if (config->pending_ext_cert_check ==
1585 EXT_CERT_CHECK_BAD) {
1586 wpa_printf(MSG_DEBUG,
1587 "EAP-FAST: External certificate check failed - force authentication failure");
1588 ret->methodState = METHOD_DONE;
1589 ret->decision = DECISION_FAIL;
1590 sm->waiting_ext_cert_check = 0;
1594 wpa_printf(MSG_DEBUG,
1595 "EAP-FAST: Continuing to wait external server certificate validation");
1599 /* Continue processing TLS handshake (phase 1). */
1600 res = eap_peer_tls_process_helper(sm, &data->ssl,
1602 data->fast_version, id, &msg,
1605 wpa_printf(MSG_DEBUG,
1606 "EAP-FAST: TLS processing failed");
1607 ret->methodState = METHOD_DONE;
1608 ret->decision = DECISION_FAIL;
1612 if (sm->waiting_ext_cert_check) {
1613 wpa_printf(MSG_DEBUG,
1614 "EAP-FAST: Waiting external server certificate validation");
1615 wpabuf_free(data->pending_resp);
1616 data->pending_resp = resp;
1620 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
1622 wpa_printf(MSG_DEBUG,
1623 "EAP-FAST: TLS done, proceed to Phase 2");
1624 if (data->provisioning &&
1625 (!(data->provisioning_allowed &
1626 EAP_FAST_PROV_AUTH) ||
1627 tls_get_cipher(sm->ssl_ctx, data->ssl.conn,
1628 cipher, sizeof(cipher)) < 0 ||
1629 os_strstr(cipher, "ADH-") ||
1630 os_strstr(cipher, "anon"))) {
1631 wpa_printf(MSG_DEBUG, "EAP-FAST: Using "
1632 "anonymous (unauthenticated) "
1634 data->anon_provisioning = 1;
1636 data->anon_provisioning = 0;
1638 if (eap_fast_derive_keys(sm, data) < 0) {
1639 wpa_printf(MSG_DEBUG,
1640 "EAP-FAST: Could not derive keys");
1641 ret->methodState = METHOD_DONE;
1642 ret->decision = DECISION_FAIL;
1650 * Application data included in the handshake message.
1652 wpabuf_free(data->pending_phase2_req);
1653 data->pending_phase2_req = resp;
1655 res = eap_fast_decrypt(sm, data, ret, id, &msg, &resp);
1661 return eap_peer_tls_build_ack(id, EAP_TYPE_FAST,
1662 data->fast_version);
1670 static Boolean eap_fast_has_reauth_data(struct eap_sm *sm, void *priv)
1672 struct eap_fast_data *data = priv;
1673 return tls_connection_established(sm->ssl_ctx, data->ssl.conn);
1677 static void eap_fast_deinit_for_reauth(struct eap_sm *sm, void *priv)
1679 struct eap_fast_data *data = priv;
1680 os_free(data->key_block_p);
1681 data->key_block_p = NULL;
1682 wpabuf_free(data->pending_phase2_req);
1683 data->pending_phase2_req = NULL;
1684 wpabuf_free(data->pending_resp);
1685 data->pending_resp = NULL;
1689 static void * eap_fast_init_for_reauth(struct eap_sm *sm, void *priv)
1691 struct eap_fast_data *data = priv;
1692 if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
1696 os_memset(data->key_data, 0, EAP_FAST_KEY_LEN);
1697 os_memset(data->emsk, 0, EAP_EMSK_LEN);
1698 os_free(data->session_id);
1699 data->session_id = NULL;
1700 if (data->phase2_priv && data->phase2_method &&
1701 data->phase2_method->init_for_reauth)
1702 data->phase2_method->init_for_reauth(sm, data->phase2_priv);
1703 data->phase2_success = 0;
1705 data->provisioning = 0;
1706 data->anon_provisioning = 0;
1707 data->simck_idx = 0;
1713 static int eap_fast_get_status(struct eap_sm *sm, void *priv, char *buf,
1714 size_t buflen, int verbose)
1716 struct eap_fast_data *data = priv;
1719 len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
1720 if (data->phase2_method) {
1721 ret = os_snprintf(buf + len, buflen - len,
1722 "EAP-FAST Phase2 method=%s\n",
1723 data->phase2_method->name);
1724 if (os_snprintf_error(buflen - len, ret))
1732 static Boolean eap_fast_isKeyAvailable(struct eap_sm *sm, void *priv)
1734 struct eap_fast_data *data = priv;
1735 return data->success;
1739 static u8 * eap_fast_getKey(struct eap_sm *sm, void *priv, size_t *len)
1741 struct eap_fast_data *data = priv;
1747 key = os_malloc(EAP_FAST_KEY_LEN);
1751 *len = EAP_FAST_KEY_LEN;
1752 os_memcpy(key, data->key_data, EAP_FAST_KEY_LEN);
1758 static u8 * eap_fast_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
1760 struct eap_fast_data *data = priv;
1763 if (!data->success || !data->session_id)
1766 id = os_malloc(data->id_len);
1770 *len = data->id_len;
1771 os_memcpy(id, data->session_id, data->id_len);
1777 static u8 * eap_fast_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
1779 struct eap_fast_data *data = priv;
1785 key = os_malloc(EAP_EMSK_LEN);
1789 *len = EAP_EMSK_LEN;
1790 os_memcpy(key, data->emsk, EAP_EMSK_LEN);
1796 int eap_peer_fast_register(void)
1798 struct eap_method *eap;
1800 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
1801 EAP_VENDOR_IETF, EAP_TYPE_FAST, "FAST");
1805 eap->init = eap_fast_init;
1806 eap->deinit = eap_fast_deinit;
1807 eap->process = eap_fast_process;
1808 eap->isKeyAvailable = eap_fast_isKeyAvailable;
1809 eap->getKey = eap_fast_getKey;
1810 eap->getSessionId = eap_fast_get_session_id;
1811 eap->get_status = eap_fast_get_status;
1813 eap->has_reauth_data = eap_fast_has_reauth_data;
1814 eap->deinit_for_reauth = eap_fast_deinit_for_reauth;
1815 eap->init_for_reauth = eap_fast_init_for_reauth;
1817 eap->get_emsk = eap_fast_get_emsk;
1819 return eap_peer_method_register(eap);