2 * EAP peer method: EAP-FAST (RFC 4851)
3 * Copyright (c) 2004-2008, 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;
73 static int eap_fast_session_ticket_cb(void *ctx, const u8 *ticket, size_t len,
74 const u8 *client_random,
75 const u8 *server_random,
78 struct eap_fast_data *data = ctx;
80 wpa_printf(MSG_DEBUG, "EAP-FAST: SessionTicket callback");
82 if (client_random == NULL || server_random == NULL ||
83 master_secret == NULL) {
84 wpa_printf(MSG_DEBUG, "EAP-FAST: SessionTicket failed - fall "
85 "back to full TLS handshake");
86 data->session_ticket_used = 0;
87 if (data->provisioning_allowed) {
88 wpa_printf(MSG_DEBUG, "EAP-FAST: Try to provision a "
90 data->provisioning = 1;
91 data->current_pac = NULL;
96 wpa_hexdump(MSG_DEBUG, "EAP-FAST: SessionTicket", ticket, len);
98 if (data->current_pac == NULL) {
99 wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC-Key available for "
100 "using SessionTicket");
101 data->session_ticket_used = 0;
105 eap_fast_derive_master_secret(data->current_pac->pac_key,
106 server_random, client_random,
109 data->session_ticket_used = 1;
115 static int eap_fast_parse_phase1(struct eap_fast_data *data,
120 pos = os_strstr(phase1, "fast_provisioning=");
122 data->provisioning_allowed = atoi(pos + 18);
123 wpa_printf(MSG_DEBUG, "EAP-FAST: Automatic PAC provisioning "
124 "mode: %d", data->provisioning_allowed);
127 pos = os_strstr(phase1, "fast_max_pac_list_len=");
129 data->max_pac_list_len = atoi(pos + 22);
130 if (data->max_pac_list_len == 0)
131 data->max_pac_list_len = 1;
132 wpa_printf(MSG_DEBUG, "EAP-FAST: Maximum PAC list length: %lu",
133 (unsigned long) data->max_pac_list_len);
136 pos = os_strstr(phase1, "fast_pac_format=binary");
138 data->use_pac_binary_format = 1;
139 wpa_printf(MSG_DEBUG, "EAP-FAST: Using binary format for PAC "
147 static void * eap_fast_init(struct eap_sm *sm)
149 struct eap_fast_data *data;
150 struct eap_peer_config *config = eap_get_config(sm);
155 data = os_zalloc(sizeof(*data));
158 data->fast_version = EAP_FAST_VERSION;
159 data->max_pac_list_len = 10;
161 if (config->phase1 && eap_fast_parse_phase1(data, config->phase1) < 0) {
162 eap_fast_deinit(sm, data);
166 if (eap_peer_select_phase2_methods(config, "auth=",
168 &data->num_phase2_types) < 0) {
169 eap_fast_deinit(sm, data);
173 data->phase2_type.vendor = EAP_VENDOR_IETF;
174 data->phase2_type.method = EAP_TYPE_NONE;
176 if (eap_peer_tls_ssl_init(sm, &data->ssl, config, EAP_TYPE_FAST)) {
177 wpa_printf(MSG_INFO, "EAP-FAST: Failed to initialize SSL.");
178 eap_fast_deinit(sm, data);
182 if (tls_connection_set_session_ticket_cb(sm->ssl_ctx, data->ssl.conn,
183 eap_fast_session_ticket_cb,
185 wpa_printf(MSG_INFO, "EAP-FAST: Failed to set SessionTicket "
187 eap_fast_deinit(sm, data);
192 * The local RADIUS server in a Cisco AP does not seem to like empty
193 * fragments before data, so disable that workaround for CBC.
194 * TODO: consider making this configurable
196 if (tls_connection_enable_workaround(sm->ssl_ctx, data->ssl.conn)) {
197 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to enable TLS "
201 if (!config->pac_file) {
202 wpa_printf(MSG_INFO, "EAP-FAST: No PAC file configured");
203 eap_fast_deinit(sm, data);
207 if (data->use_pac_binary_format &&
208 eap_fast_load_pac_bin(sm, &data->pac, config->pac_file) < 0) {
209 wpa_printf(MSG_INFO, "EAP-FAST: Failed to load PAC file");
210 eap_fast_deinit(sm, data);
214 if (!data->use_pac_binary_format &&
215 eap_fast_load_pac(sm, &data->pac, config->pac_file) < 0) {
216 wpa_printf(MSG_INFO, "EAP-FAST: Failed to load PAC file");
217 eap_fast_deinit(sm, data);
220 eap_fast_pac_list_truncate(data->pac, data->max_pac_list_len);
222 if (data->pac == NULL && !data->provisioning_allowed) {
223 wpa_printf(MSG_INFO, "EAP-FAST: No PAC configured and "
224 "provisioning disabled");
225 eap_fast_deinit(sm, data);
233 static void eap_fast_deinit(struct eap_sm *sm, void *priv)
235 struct eap_fast_data *data = priv;
236 struct eap_fast_pac *pac, *prev;
240 if (data->phase2_priv && data->phase2_method)
241 data->phase2_method->deinit(sm, data->phase2_priv);
242 os_free(data->phase2_types);
243 os_free(data->key_block_p);
244 eap_peer_tls_ssl_deinit(sm, &data->ssl);
251 eap_fast_free_pac(prev);
253 os_memset(data->key_data, 0, EAP_FAST_KEY_LEN);
254 os_memset(data->emsk, 0, EAP_EMSK_LEN);
255 os_free(data->session_id);
256 wpabuf_free(data->pending_phase2_req);
261 static int eap_fast_derive_msk(struct eap_fast_data *data)
263 eap_fast_derive_eap_msk(data->simck, data->key_data);
264 eap_fast_derive_eap_emsk(data->simck, data->emsk);
270 static void eap_fast_derive_key_auth(struct eap_sm *sm,
271 struct eap_fast_data *data)
275 /* RFC 4851, Section 5.1:
276 * Extra key material after TLS key_block: session_key_seed[40]
279 sks = eap_fast_derive_key(sm->ssl_ctx, data->ssl.conn, "key expansion",
282 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive "
288 * RFC 4851, Section 5.2:
289 * S-IMCK[0] = session_key_seed
291 wpa_hexdump_key(MSG_DEBUG,
292 "EAP-FAST: session_key_seed (SKS = S-IMCK[0])",
293 sks, EAP_FAST_SKS_LEN);
295 os_memcpy(data->simck, sks, EAP_FAST_SIMCK_LEN);
300 static void 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,
307 sizeof(*data->key_block_p));
308 if (data->key_block_p == NULL) {
309 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive key block");
313 * RFC 4851, Section 5.2:
314 * S-IMCK[0] = session_key_seed
316 wpa_hexdump_key(MSG_DEBUG,
317 "EAP-FAST: session_key_seed (SKS = S-IMCK[0])",
318 data->key_block_p->session_key_seed,
319 sizeof(data->key_block_p->session_key_seed));
321 os_memcpy(data->simck, data->key_block_p->session_key_seed,
323 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: server_challenge",
324 data->key_block_p->server_challenge,
325 sizeof(data->key_block_p->server_challenge));
326 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: client_challenge",
327 data->key_block_p->client_challenge,
328 sizeof(data->key_block_p->client_challenge));
332 static void eap_fast_derive_keys(struct eap_sm *sm, struct eap_fast_data *data)
334 if (data->anon_provisioning)
335 eap_fast_derive_key_provisioning(sm, data);
337 eap_fast_derive_key_auth(sm, data);
341 static int eap_fast_init_phase2_method(struct eap_sm *sm,
342 struct eap_fast_data *data)
344 data->phase2_method =
345 eap_peer_get_eap_method(data->phase2_type.vendor,
346 data->phase2_type.method);
347 if (data->phase2_method == NULL)
350 if (data->key_block_p) {
351 sm->auth_challenge = data->key_block_p->server_challenge;
352 sm->peer_challenge = data->key_block_p->client_challenge;
355 data->phase2_priv = data->phase2_method->init(sm);
357 sm->auth_challenge = NULL;
358 sm->peer_challenge = NULL;
360 return data->phase2_priv == NULL ? -1 : 0;
364 static int eap_fast_select_phase2_method(struct eap_fast_data *data, u8 type)
368 /* TODO: TNC with anonymous provisioning; need to require both
369 * completed MSCHAPv2 and TNC */
371 if (data->anon_provisioning && type != EAP_TYPE_MSCHAPV2) {
372 wpa_printf(MSG_INFO, "EAP-FAST: Only EAP-MSCHAPv2 is allowed "
373 "during unauthenticated provisioning; reject phase2"
379 if (type == EAP_TYPE_TNC) {
380 data->phase2_type.vendor = EAP_VENDOR_IETF;
381 data->phase2_type.method = EAP_TYPE_TNC;
382 wpa_printf(MSG_DEBUG, "EAP-FAST: Selected Phase 2 EAP "
383 "vendor %d method %d for TNC",
384 data->phase2_type.vendor,
385 data->phase2_type.method);
390 for (i = 0; i < data->num_phase2_types; i++) {
391 if (data->phase2_types[i].vendor != EAP_VENDOR_IETF ||
392 data->phase2_types[i].method != type)
395 data->phase2_type.vendor = data->phase2_types[i].vendor;
396 data->phase2_type.method = data->phase2_types[i].method;
397 wpa_printf(MSG_DEBUG, "EAP-FAST: Selected Phase 2 EAP "
398 "vendor %d method %d",
399 data->phase2_type.vendor,
400 data->phase2_type.method);
404 if (type != data->phase2_type.method || type == EAP_TYPE_NONE)
411 static int eap_fast_phase2_request(struct eap_sm *sm,
412 struct eap_fast_data *data,
413 struct eap_method_ret *ret,
415 struct wpabuf **resp)
417 size_t len = be_to_host16(hdr->length);
419 struct eap_method_ret iret;
420 struct eap_peer_config *config = eap_get_config(sm);
423 if (len <= sizeof(struct eap_hdr)) {
424 wpa_printf(MSG_INFO, "EAP-FAST: too short "
425 "Phase 2 request (len=%lu)", (unsigned long) len);
428 pos = (u8 *) (hdr + 1);
429 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 Request: type=%d", *pos);
430 if (*pos == EAP_TYPE_IDENTITY) {
431 *resp = eap_sm_buildIdentity(sm, hdr->identifier, 1);
435 if (data->phase2_priv && data->phase2_method &&
436 *pos != data->phase2_type.method) {
437 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 EAP sequence - "
438 "deinitialize previous method");
439 data->phase2_method->deinit(sm, data->phase2_priv);
440 data->phase2_method = NULL;
441 data->phase2_priv = NULL;
442 data->phase2_type.vendor = EAP_VENDOR_IETF;
443 data->phase2_type.method = EAP_TYPE_NONE;
446 if (data->phase2_type.vendor == EAP_VENDOR_IETF &&
447 data->phase2_type.method == EAP_TYPE_NONE &&
448 eap_fast_select_phase2_method(data, *pos) < 0) {
449 if (eap_peer_tls_phase2_nak(data->phase2_types,
450 data->num_phase2_types,
456 if ((data->phase2_priv == NULL &&
457 eap_fast_init_phase2_method(sm, data) < 0) ||
458 data->phase2_method == NULL) {
459 wpa_printf(MSG_INFO, "EAP-FAST: Failed to initialize "
460 "Phase 2 EAP method %d", *pos);
461 ret->methodState = METHOD_DONE;
462 ret->decision = DECISION_FAIL;
466 os_memset(&iret, 0, sizeof(iret));
467 wpabuf_set(&msg, hdr, len);
468 *resp = data->phase2_method->process(sm, data->phase2_priv, &iret,
471 (iret.methodState == METHOD_DONE &&
472 iret.decision == DECISION_FAIL)) {
473 ret->methodState = METHOD_DONE;
474 ret->decision = DECISION_FAIL;
475 } else if ((iret.methodState == METHOD_DONE ||
476 iret.methodState == METHOD_MAY_CONT) &&
477 (iret.decision == DECISION_UNCOND_SUCC ||
478 iret.decision == DECISION_COND_SUCC)) {
479 data->phase2_success = 1;
482 if (*resp == NULL && config &&
483 (config->pending_req_identity || config->pending_req_password ||
484 config->pending_req_otp || config->pending_req_new_password)) {
485 wpabuf_free(data->pending_phase2_req);
486 data->pending_phase2_req = wpabuf_alloc_copy(hdr, len);
487 } else if (*resp == NULL)
494 static struct wpabuf * eap_fast_tlv_nak(int vendor_id, int tlv_type)
497 struct eap_tlv_nak_tlv *nak;
498 buf = wpabuf_alloc(sizeof(*nak));
501 nak = wpabuf_put(buf, sizeof(*nak));
502 nak->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY | EAP_TLV_NAK_TLV);
503 nak->length = host_to_be16(6);
504 nak->vendor_id = host_to_be32(vendor_id);
505 nak->nak_type = host_to_be16(tlv_type);
510 static struct wpabuf * eap_fast_tlv_result(int status, int intermediate)
513 struct eap_tlv_intermediate_result_tlv *result;
514 buf = wpabuf_alloc(sizeof(*result));
517 wpa_printf(MSG_DEBUG, "EAP-FAST: Add %sResult TLV(status=%d)",
518 intermediate ? "Intermediate " : "", status);
519 result = wpabuf_put(buf, sizeof(*result));
520 result->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
522 EAP_TLV_INTERMEDIATE_RESULT_TLV :
523 EAP_TLV_RESULT_TLV));
524 result->length = host_to_be16(2);
525 result->status = host_to_be16(status);
530 static struct wpabuf * eap_fast_tlv_pac_ack(void)
533 struct eap_tlv_result_tlv *res;
534 struct eap_tlv_pac_ack_tlv *ack;
536 buf = wpabuf_alloc(sizeof(*res) + sizeof(*ack));
540 wpa_printf(MSG_DEBUG, "EAP-FAST: Add PAC TLV (ack)");
541 ack = wpabuf_put(buf, sizeof(*ack));
542 ack->tlv_type = host_to_be16(EAP_TLV_PAC_TLV |
543 EAP_TLV_TYPE_MANDATORY);
544 ack->length = host_to_be16(sizeof(*ack) - sizeof(struct eap_tlv_hdr));
545 ack->pac_type = host_to_be16(PAC_TYPE_PAC_ACKNOWLEDGEMENT);
546 ack->pac_len = host_to_be16(2);
547 ack->result = host_to_be16(EAP_TLV_RESULT_SUCCESS);
553 static struct wpabuf * eap_fast_process_eap_payload_tlv(
554 struct eap_sm *sm, struct eap_fast_data *data,
555 struct eap_method_ret *ret,
556 u8 *eap_payload_tlv, size_t eap_payload_tlv_len)
559 struct wpabuf *resp = NULL;
561 if (eap_payload_tlv_len < sizeof(*hdr)) {
562 wpa_printf(MSG_DEBUG, "EAP-FAST: too short EAP "
563 "Payload TLV (len=%lu)",
564 (unsigned long) eap_payload_tlv_len);
568 hdr = (struct eap_hdr *) eap_payload_tlv;
569 if (be_to_host16(hdr->length) > eap_payload_tlv_len) {
570 wpa_printf(MSG_DEBUG, "EAP-FAST: EAP packet overflow in "
575 if (hdr->code != EAP_CODE_REQUEST) {
576 wpa_printf(MSG_INFO, "EAP-FAST: Unexpected code=%d in "
577 "Phase 2 EAP header", hdr->code);
581 if (eap_fast_phase2_request(sm, data, ret, hdr, &resp)) {
582 wpa_printf(MSG_INFO, "EAP-FAST: Phase2 Request processing "
587 return eap_fast_tlv_eap_payload(resp);
591 static int eap_fast_validate_crypto_binding(
592 struct eap_tlv_crypto_binding_tlv *_bind)
594 wpa_printf(MSG_DEBUG, "EAP-FAST: Crypto-Binding TLV: Version %d "
595 "Received Version %d SubType %d",
596 _bind->version, _bind->received_version, _bind->subtype);
597 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE",
598 _bind->nonce, sizeof(_bind->nonce));
599 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC",
600 _bind->compound_mac, sizeof(_bind->compound_mac));
602 if (_bind->version != EAP_FAST_VERSION ||
603 _bind->received_version != EAP_FAST_VERSION ||
604 _bind->subtype != EAP_TLV_CRYPTO_BINDING_SUBTYPE_REQUEST) {
605 wpa_printf(MSG_INFO, "EAP-FAST: Invalid version/subtype in "
606 "Crypto-Binding TLV: Version %d "
607 "Received Version %d SubType %d",
608 _bind->version, _bind->received_version,
617 static void eap_fast_write_crypto_binding(
618 struct eap_tlv_crypto_binding_tlv *rbind,
619 struct eap_tlv_crypto_binding_tlv *_bind, const u8 *cmk)
621 rbind->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
622 EAP_TLV_CRYPTO_BINDING_TLV);
623 rbind->length = host_to_be16(sizeof(*rbind) -
624 sizeof(struct eap_tlv_hdr));
625 rbind->version = EAP_FAST_VERSION;
626 rbind->received_version = _bind->version;
627 rbind->subtype = EAP_TLV_CRYPTO_BINDING_SUBTYPE_RESPONSE;
628 os_memcpy(rbind->nonce, _bind->nonce, sizeof(_bind->nonce));
629 inc_byte_array(rbind->nonce, sizeof(rbind->nonce));
630 hmac_sha1(cmk, EAP_FAST_CMK_LEN, (u8 *) rbind, sizeof(*rbind),
631 rbind->compound_mac);
633 wpa_printf(MSG_DEBUG, "EAP-FAST: Reply Crypto-Binding TLV: Version %d "
634 "Received Version %d SubType %d",
635 rbind->version, rbind->received_version, rbind->subtype);
636 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE",
637 rbind->nonce, sizeof(rbind->nonce));
638 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC",
639 rbind->compound_mac, sizeof(rbind->compound_mac));
643 static int eap_fast_get_phase2_key(struct eap_sm *sm,
644 struct eap_fast_data *data,
645 u8 *isk, size_t isk_len)
650 os_memset(isk, 0, isk_len);
652 if (data->phase2_method == NULL || data->phase2_priv == NULL) {
653 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 method not "
658 if (data->phase2_method->isKeyAvailable == NULL ||
659 data->phase2_method->getKey == NULL)
662 if (!data->phase2_method->isKeyAvailable(sm, data->phase2_priv) ||
663 (key = data->phase2_method->getKey(sm, data->phase2_priv,
664 &key_len)) == NULL) {
665 wpa_printf(MSG_DEBUG, "EAP-FAST: Could not get key material "
670 if (key_len > isk_len)
673 data->phase2_method->vendor == EAP_VENDOR_IETF &&
674 data->phase2_method->method == EAP_TYPE_MSCHAPV2) {
676 * EAP-FAST uses reverse order for MS-MPPE keys when deriving
677 * MSK from EAP-MSCHAPv2. Swap the keys here to get the correct
678 * ISK for EAP-FAST cryptobinding.
680 os_memcpy(isk, key + 16, 16);
681 os_memcpy(isk + 16, key, 16);
683 os_memcpy(isk, key, key_len);
690 static int eap_fast_get_cmk(struct eap_sm *sm, struct eap_fast_data *data,
693 u8 isk[32], imck[60];
695 wpa_printf(MSG_DEBUG, "EAP-FAST: Determining CMK[%d] for Compound MIC "
696 "calculation", data->simck_idx + 1);
699 * RFC 4851, Section 5.2:
700 * IMCK[j] = T-PRF(S-IMCK[j-1], "Inner Methods Compound Keys",
702 * S-IMCK[j] = first 40 octets of IMCK[j]
703 * CMK[j] = last 20 octets of IMCK[j]
706 if (eap_fast_get_phase2_key(sm, data, isk, sizeof(isk)) < 0)
708 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: ISK[j]", isk, sizeof(isk));
709 sha1_t_prf(data->simck, EAP_FAST_SIMCK_LEN,
710 "Inner Methods Compound Keys",
711 isk, sizeof(isk), imck, sizeof(imck));
713 os_memcpy(data->simck, imck, EAP_FAST_SIMCK_LEN);
714 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: S-IMCK[j]",
715 data->simck, EAP_FAST_SIMCK_LEN);
716 os_memcpy(cmk, imck + EAP_FAST_SIMCK_LEN, EAP_FAST_CMK_LEN);
717 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: CMK[j]",
718 cmk, EAP_FAST_CMK_LEN);
724 static u8 * eap_fast_write_pac_request(u8 *pos, u16 pac_type)
726 struct eap_tlv_hdr *pac;
727 struct eap_tlv_request_action_tlv *act;
728 struct eap_tlv_pac_type_tlv *type;
730 act = (struct eap_tlv_request_action_tlv *) pos;
731 act->tlv_type = host_to_be16(EAP_TLV_REQUEST_ACTION_TLV);
732 act->length = host_to_be16(2);
733 act->action = host_to_be16(EAP_TLV_ACTION_PROCESS_TLV);
735 pac = (struct eap_tlv_hdr *) (act + 1);
736 pac->tlv_type = host_to_be16(EAP_TLV_PAC_TLV);
737 pac->length = host_to_be16(sizeof(*type));
739 type = (struct eap_tlv_pac_type_tlv *) (pac + 1);
740 type->tlv_type = host_to_be16(PAC_TYPE_PAC_TYPE);
741 type->length = host_to_be16(2);
742 type->pac_type = host_to_be16(pac_type);
744 return (u8 *) (type + 1);
748 static struct wpabuf * eap_fast_process_crypto_binding(
749 struct eap_sm *sm, struct eap_fast_data *data,
750 struct eap_method_ret *ret,
751 struct eap_tlv_crypto_binding_tlv *_bind, size_t bind_len)
755 u8 cmk[EAP_FAST_CMK_LEN], cmac[SHA1_MAC_LEN];
759 if (eap_fast_validate_crypto_binding(_bind) < 0)
762 if (eap_fast_get_cmk(sm, data, cmk) < 0)
765 /* Validate received Compound MAC */
766 os_memcpy(cmac, _bind->compound_mac, sizeof(cmac));
767 os_memset(_bind->compound_mac, 0, sizeof(cmac));
768 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Crypto-Binding TLV for Compound "
769 "MAC calculation", (u8 *) _bind, bind_len);
770 hmac_sha1(cmk, EAP_FAST_CMK_LEN, (u8 *) _bind, bind_len,
771 _bind->compound_mac);
772 res = os_memcmp_const(cmac, _bind->compound_mac, sizeof(cmac));
773 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Received Compound MAC",
775 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Calculated Compound MAC",
776 _bind->compound_mac, sizeof(cmac));
778 wpa_printf(MSG_INFO, "EAP-FAST: Compound MAC did not match");
779 os_memcpy(_bind->compound_mac, cmac, sizeof(cmac));
784 * Compound MAC was valid, so authentication succeeded. Reply with
785 * crypto binding to allow server to complete authentication.
788 len = sizeof(struct eap_tlv_crypto_binding_tlv);
789 resp = wpabuf_alloc(len);
793 if (!data->anon_provisioning && data->phase2_success &&
794 eap_fast_derive_msk(data) < 0) {
795 wpa_printf(MSG_INFO, "EAP-FAST: Failed to generate MSK");
796 ret->methodState = METHOD_DONE;
797 ret->decision = DECISION_FAIL;
798 data->phase2_success = 0;
803 if (!data->anon_provisioning && data->phase2_success) {
804 os_free(data->session_id);
805 data->session_id = eap_peer_tls_derive_session_id(
806 sm, &data->ssl, EAP_TYPE_FAST, &data->id_len);
807 if (data->session_id) {
808 wpa_hexdump(MSG_DEBUG, "EAP-FAST: Derived Session-Id",
809 data->session_id, data->id_len);
811 wpa_printf(MSG_ERROR, "EAP-FAST: Failed to derive "
818 pos = wpabuf_put(resp, sizeof(struct eap_tlv_crypto_binding_tlv));
819 eap_fast_write_crypto_binding((struct eap_tlv_crypto_binding_tlv *)
826 static void eap_fast_parse_pac_tlv(struct eap_fast_pac *entry, int type,
827 u8 *pos, size_t len, int *pac_key_found)
829 switch (type & 0x7fff) {
830 case PAC_TYPE_PAC_KEY:
831 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: PAC-Key", pos, len);
832 if (len != EAP_FAST_PAC_KEY_LEN) {
833 wpa_printf(MSG_DEBUG, "EAP-FAST: Invalid PAC-Key "
834 "length %lu", (unsigned long) len);
838 os_memcpy(entry->pac_key, pos, len);
840 case PAC_TYPE_PAC_OPAQUE:
841 wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Opaque", pos, len);
842 entry->pac_opaque = pos;
843 entry->pac_opaque_len = len;
845 case PAC_TYPE_PAC_INFO:
846 wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Info", pos, len);
847 entry->pac_info = pos;
848 entry->pac_info_len = len;
851 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignored unknown PAC type %d",
858 static int eap_fast_process_pac_tlv(struct eap_fast_pac *entry,
859 u8 *pac, size_t pac_len)
861 struct pac_tlv_hdr *hdr;
864 int type, pac_key_found = 0;
869 while (left > sizeof(*hdr)) {
870 hdr = (struct pac_tlv_hdr *) pos;
871 type = be_to_host16(hdr->type);
872 len = be_to_host16(hdr->len);
874 left -= sizeof(*hdr);
876 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV overrun "
877 "(type=%d len=%lu left=%lu)",
878 type, (unsigned long) len,
879 (unsigned long) left);
883 eap_fast_parse_pac_tlv(entry, type, pos, len, &pac_key_found);
889 if (!pac_key_found || !entry->pac_opaque || !entry->pac_info) {
890 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV does not include "
891 "all the required fields");
899 static int eap_fast_parse_pac_info(struct eap_fast_pac *entry, int type,
906 switch (type & 0x7fff) {
907 case PAC_TYPE_CRED_LIFETIME:
909 wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Info - "
910 "Invalid CRED_LIFETIME length - ignored",
916 * This is not currently saved separately in PAC files since
917 * the server can automatically initiate PAC update when
918 * needed. Anyway, the information is available from PAC-Info
919 * dump if it is needed for something in the future.
921 lifetime = WPA_GET_BE32(pos);
923 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info - CRED_LIFETIME %d "
925 lifetime, (lifetime - (u32) now.sec) / 86400);
928 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - A-ID",
931 entry->a_id_len = len;
934 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - I-ID",
937 entry->i_id_len = len;
939 case PAC_TYPE_A_ID_INFO:
940 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - A-ID-Info",
942 entry->a_id_info = pos;
943 entry->a_id_info_len = len;
945 case PAC_TYPE_PAC_TYPE:
946 /* RFC 5422, Section 4.2.6 - PAC-Type TLV */
948 wpa_printf(MSG_INFO, "EAP-FAST: Invalid PAC-Type "
949 "length %lu (expected 2)",
950 (unsigned long) len);
951 wpa_hexdump_ascii(MSG_DEBUG,
952 "EAP-FAST: PAC-Info - PAC-Type",
956 pac_type = WPA_GET_BE16(pos);
957 if (pac_type != PAC_TYPE_TUNNEL_PAC &&
958 pac_type != PAC_TYPE_USER_AUTHORIZATION &&
959 pac_type != PAC_TYPE_MACHINE_AUTHENTICATION) {
960 wpa_printf(MSG_INFO, "EAP-FAST: Unsupported PAC Type "
965 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info - PAC-Type %d",
967 entry->pac_type = pac_type;
970 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignored unknown PAC-Info "
979 static int eap_fast_process_pac_info(struct eap_fast_pac *entry)
981 struct pac_tlv_hdr *hdr;
986 /* RFC 5422, Section 4.2.4 */
988 /* PAC-Type defaults to Tunnel PAC (Type 1) */
989 entry->pac_type = PAC_TYPE_TUNNEL_PAC;
991 pos = entry->pac_info;
992 left = entry->pac_info_len;
993 while (left > sizeof(*hdr)) {
994 hdr = (struct pac_tlv_hdr *) pos;
995 type = be_to_host16(hdr->type);
996 len = be_to_host16(hdr->len);
998 left -= sizeof(*hdr);
1000 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info overrun "
1001 "(type=%d len=%lu left=%lu)",
1002 type, (unsigned long) len,
1003 (unsigned long) left);
1007 if (eap_fast_parse_pac_info(entry, type, pos, len) < 0)
1014 if (entry->a_id == NULL || entry->a_id_info == NULL) {
1015 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info does not include "
1016 "all the required fields");
1024 static struct wpabuf * eap_fast_process_pac(struct eap_sm *sm,
1025 struct eap_fast_data *data,
1026 struct eap_method_ret *ret,
1027 u8 *pac, size_t pac_len)
1029 struct eap_peer_config *config = eap_get_config(sm);
1030 struct eap_fast_pac entry;
1032 os_memset(&entry, 0, sizeof(entry));
1033 if (eap_fast_process_pac_tlv(&entry, pac, pac_len) ||
1034 eap_fast_process_pac_info(&entry))
1037 eap_fast_add_pac(&data->pac, &data->current_pac, &entry);
1038 eap_fast_pac_list_truncate(data->pac, data->max_pac_list_len);
1039 if (data->use_pac_binary_format)
1040 eap_fast_save_pac_bin(sm, data->pac, config->pac_file);
1042 eap_fast_save_pac(sm, data->pac, config->pac_file);
1044 if (data->provisioning) {
1045 if (data->anon_provisioning) {
1047 * Unauthenticated provisioning does not provide keying
1048 * material and must end with an EAP-Failure.
1049 * Authentication will be done separately after this.
1052 ret->decision = DECISION_FAIL;
1055 * Server may or may not allow authenticated
1056 * provisioning also for key generation.
1058 ret->decision = DECISION_COND_SUCC;
1060 wpa_printf(MSG_DEBUG, "EAP-FAST: Send PAC-Acknowledgement TLV "
1061 "- Provisioning completed successfully");
1062 sm->expected_failure = 1;
1065 * This is PAC refreshing, i.e., normal authentication that is
1066 * expected to be completed with an EAP-Success. However,
1067 * RFC 5422, Section 3.5 allows EAP-Failure to be sent even
1068 * after protected success exchange in case of EAP-Fast
1069 * provisioning, so we better use DECISION_COND_SUCC here
1070 * instead of DECISION_UNCOND_SUCC.
1072 wpa_printf(MSG_DEBUG, "EAP-FAST: Send PAC-Acknowledgement TLV "
1073 "- PAC refreshing completed successfully");
1074 ret->decision = DECISION_COND_SUCC;
1076 ret->methodState = METHOD_DONE;
1077 return eap_fast_tlv_pac_ack();
1081 static int eap_fast_parse_decrypted(struct wpabuf *decrypted,
1082 struct eap_fast_tlv_parse *tlv,
1083 struct wpabuf **resp)
1085 int mandatory, tlv_type, res;
1089 os_memset(tlv, 0, sizeof(*tlv));
1091 /* Parse TLVs from the decrypted Phase 2 data */
1092 pos = wpabuf_mhead(decrypted);
1093 end = pos + wpabuf_len(decrypted);
1094 while (pos + 4 < end) {
1095 mandatory = pos[0] & 0x80;
1096 tlv_type = WPA_GET_BE16(pos) & 0x3fff;
1098 len = WPA_GET_BE16(pos);
1100 if (len > (size_t) (end - pos)) {
1101 wpa_printf(MSG_INFO, "EAP-FAST: TLV overflow");
1104 wpa_printf(MSG_DEBUG, "EAP-FAST: Received Phase 2: "
1105 "TLV type %d length %u%s",
1106 tlv_type, (unsigned int) len,
1107 mandatory ? " (mandatory)" : "");
1109 res = eap_fast_parse_tlv(tlv, tlv_type, pos, len);
1114 wpa_printf(MSG_DEBUG, "EAP-FAST: Nak unknown "
1115 "mandatory TLV type %d", tlv_type);
1116 *resp = eap_fast_tlv_nak(0, tlv_type);
1119 wpa_printf(MSG_DEBUG, "EAP-FAST: ignored "
1120 "unknown optional TLV type %d",
1132 static int eap_fast_encrypt_response(struct eap_sm *sm,
1133 struct eap_fast_data *data,
1134 struct wpabuf *resp,
1135 u8 identifier, struct wpabuf **out_data)
1140 wpa_hexdump_buf(MSG_DEBUG, "EAP-FAST: Encrypting Phase 2 data",
1142 if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_FAST,
1143 data->fast_version, identifier,
1145 wpa_printf(MSG_INFO, "EAP-FAST: Failed to encrypt a Phase 2 "
1154 static struct wpabuf * eap_fast_pac_request(void)
1159 tmp = wpabuf_alloc(sizeof(struct eap_tlv_hdr) +
1160 sizeof(struct eap_tlv_request_action_tlv) +
1161 sizeof(struct eap_tlv_pac_type_tlv));
1165 pos = wpabuf_put(tmp, 0);
1166 pos2 = eap_fast_write_pac_request(pos, PAC_TYPE_TUNNEL_PAC);
1167 wpabuf_put(tmp, pos2 - pos);
1172 static int eap_fast_process_decrypted(struct eap_sm *sm,
1173 struct eap_fast_data *data,
1174 struct eap_method_ret *ret,
1175 const struct eap_hdr *req,
1176 struct wpabuf *decrypted,
1177 struct wpabuf **out_data)
1179 struct wpabuf *resp = NULL, *tmp;
1180 struct eap_fast_tlv_parse tlv;
1183 if (eap_fast_parse_decrypted(decrypted, &tlv, &resp) < 0)
1186 return eap_fast_encrypt_response(sm, data, resp,
1187 req->identifier, out_data);
1189 if (tlv.result == EAP_TLV_RESULT_FAILURE) {
1190 resp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 0);
1191 return eap_fast_encrypt_response(sm, data, resp,
1192 req->identifier, out_data);
1195 if (tlv.iresult == EAP_TLV_RESULT_FAILURE) {
1196 resp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 1);
1197 return eap_fast_encrypt_response(sm, data, resp,
1198 req->identifier, out_data);
1201 if (tlv.crypto_binding) {
1202 tmp = eap_fast_process_crypto_binding(sm, data, ret,
1204 tlv.crypto_binding_len);
1208 resp = wpabuf_concat(resp, tmp);
1211 if (tlv.iresult == EAP_TLV_RESULT_SUCCESS) {
1212 tmp = eap_fast_tlv_result(failed ? EAP_TLV_RESULT_FAILURE :
1213 EAP_TLV_RESULT_SUCCESS, 1);
1214 resp = wpabuf_concat(resp, tmp);
1217 if (tlv.eap_payload_tlv) {
1218 tmp = eap_fast_process_eap_payload_tlv(
1219 sm, data, ret, tlv.eap_payload_tlv,
1220 tlv.eap_payload_tlv_len);
1221 resp = wpabuf_concat(resp, tmp);
1224 if (tlv.pac && tlv.result != EAP_TLV_RESULT_SUCCESS) {
1225 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV without Result TLV "
1226 "acknowledging success");
1228 } else if (tlv.pac && tlv.result == EAP_TLV_RESULT_SUCCESS) {
1229 tmp = eap_fast_process_pac(sm, data, ret, tlv.pac,
1231 resp = wpabuf_concat(resp, tmp);
1234 if (data->current_pac == NULL && data->provisioning &&
1235 !data->anon_provisioning && !tlv.pac &&
1236 (tlv.iresult == EAP_TLV_RESULT_SUCCESS ||
1237 tlv.result == EAP_TLV_RESULT_SUCCESS)) {
1239 * Need to request Tunnel PAC when using authenticated
1242 wpa_printf(MSG_DEBUG, "EAP-FAST: Request Tunnel PAC");
1243 tmp = eap_fast_pac_request();
1244 resp = wpabuf_concat(resp, tmp);
1247 if (tlv.result == EAP_TLV_RESULT_SUCCESS && !failed) {
1248 tmp = eap_fast_tlv_result(EAP_TLV_RESULT_SUCCESS, 0);
1249 resp = wpabuf_concat(tmp, resp);
1250 } else if (failed) {
1251 tmp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 0);
1252 resp = wpabuf_concat(tmp, resp);
1255 if (resp && tlv.result == EAP_TLV_RESULT_SUCCESS && !failed &&
1256 tlv.crypto_binding && data->phase2_success) {
1257 if (data->anon_provisioning) {
1258 wpa_printf(MSG_DEBUG, "EAP-FAST: Unauthenticated "
1259 "provisioning completed successfully.");
1260 ret->methodState = METHOD_DONE;
1261 ret->decision = DECISION_FAIL;
1262 sm->expected_failure = 1;
1264 wpa_printf(MSG_DEBUG, "EAP-FAST: Authentication "
1265 "completed successfully.");
1266 if (data->provisioning)
1267 ret->methodState = METHOD_MAY_CONT;
1269 ret->methodState = METHOD_DONE;
1270 ret->decision = DECISION_UNCOND_SUCC;
1275 wpa_printf(MSG_DEBUG, "EAP-FAST: No recognized TLVs - send "
1276 "empty response packet");
1277 resp = wpabuf_alloc(1);
1280 return eap_fast_encrypt_response(sm, data, resp, req->identifier,
1285 static int eap_fast_decrypt(struct eap_sm *sm, struct eap_fast_data *data,
1286 struct eap_method_ret *ret,
1287 const struct eap_hdr *req,
1288 const struct wpabuf *in_data,
1289 struct wpabuf **out_data)
1291 struct wpabuf *in_decrypted;
1294 wpa_printf(MSG_DEBUG, "EAP-FAST: Received %lu bytes encrypted data for"
1295 " Phase 2", (unsigned long) wpabuf_len(in_data));
1297 if (data->pending_phase2_req) {
1298 wpa_printf(MSG_DEBUG, "EAP-FAST: Pending Phase 2 request - "
1299 "skip decryption and use old data");
1300 /* Clear TLS reassembly state. */
1301 eap_peer_tls_reset_input(&data->ssl);
1303 in_decrypted = data->pending_phase2_req;
1304 data->pending_phase2_req = NULL;
1308 if (wpabuf_len(in_data) == 0) {
1309 /* Received TLS ACK - requesting more fragments */
1310 return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_FAST,
1312 req->identifier, NULL, out_data);
1315 res = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
1320 wpa_hexdump_buf(MSG_MSGDUMP, "EAP-FAST: Decrypted Phase 2 TLV(s)",
1323 if (wpabuf_len(in_decrypted) < 4) {
1324 wpa_printf(MSG_INFO, "EAP-FAST: Too short Phase 2 "
1325 "TLV frame (len=%lu)",
1326 (unsigned long) wpabuf_len(in_decrypted));
1327 wpabuf_free(in_decrypted);
1331 res = eap_fast_process_decrypted(sm, data, ret, req,
1332 in_decrypted, out_data);
1334 wpabuf_free(in_decrypted);
1340 static const u8 * eap_fast_get_a_id(const u8 *buf, size_t len, size_t *id_len)
1343 struct pac_tlv_hdr *hdr;
1346 * Parse authority identity (A-ID) from the EAP-FAST/Start. This
1347 * supports both raw A-ID and one inside an A-ID TLV.
1351 if (len > sizeof(*hdr)) {
1353 hdr = (struct pac_tlv_hdr *) buf;
1354 tlen = be_to_host16(hdr->len);
1355 if (be_to_host16(hdr->type) == PAC_TYPE_A_ID &&
1356 sizeof(*hdr) + tlen <= len) {
1357 wpa_printf(MSG_DEBUG, "EAP-FAST: A-ID was in TLV "
1359 a_id = (u8 *) (hdr + 1);
1363 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: A-ID", a_id, *id_len);
1369 static void eap_fast_select_pac(struct eap_fast_data *data,
1370 const u8 *a_id, size_t a_id_len)
1372 data->current_pac = eap_fast_get_pac(data->pac, a_id, a_id_len,
1373 PAC_TYPE_TUNNEL_PAC);
1374 if (data->current_pac == NULL) {
1376 * Tunnel PAC was not available for this A-ID. Try to use
1377 * Machine Authentication PAC, if one is available.
1379 data->current_pac = eap_fast_get_pac(
1380 data->pac, a_id, a_id_len,
1381 PAC_TYPE_MACHINE_AUTHENTICATION);
1384 if (data->current_pac) {
1385 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC found for this A-ID "
1386 "(PAC-Type %d)", data->current_pac->pac_type);
1387 wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-FAST: A-ID-Info",
1388 data->current_pac->a_id_info,
1389 data->current_pac->a_id_info_len);
1394 static int eap_fast_use_pac_opaque(struct eap_sm *sm,
1395 struct eap_fast_data *data,
1396 struct eap_fast_pac *pac)
1399 size_t tlv_len, olen;
1400 struct eap_tlv_hdr *ehdr;
1402 olen = pac->pac_opaque_len;
1403 tlv_len = sizeof(*ehdr) + olen;
1404 tlv = os_malloc(tlv_len);
1406 ehdr = (struct eap_tlv_hdr *) tlv;
1407 ehdr->tlv_type = host_to_be16(PAC_TYPE_PAC_OPAQUE);
1408 ehdr->length = host_to_be16(olen);
1409 os_memcpy(ehdr + 1, pac->pac_opaque, olen);
1412 tls_connection_client_hello_ext(sm->ssl_ctx, data->ssl.conn,
1414 tlv, tlv_len) < 0) {
1415 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to add PAC-Opaque TLS "
1426 static int eap_fast_clear_pac_opaque_ext(struct eap_sm *sm,
1427 struct eap_fast_data *data)
1429 if (tls_connection_client_hello_ext(sm->ssl_ctx, data->ssl.conn,
1430 TLS_EXT_PAC_OPAQUE, NULL, 0) < 0) {
1431 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to remove PAC-Opaque "
1439 static int eap_fast_set_provisioning_ciphers(struct eap_sm *sm,
1440 struct eap_fast_data *data)
1445 if (data->provisioning_allowed & EAP_FAST_PROV_UNAUTH) {
1446 wpa_printf(MSG_DEBUG, "EAP-FAST: Enabling unauthenticated "
1447 "provisioning TLS cipher suites");
1448 ciphers[count++] = TLS_CIPHER_ANON_DH_AES128_SHA;
1451 if (data->provisioning_allowed & EAP_FAST_PROV_AUTH) {
1452 wpa_printf(MSG_DEBUG, "EAP-FAST: Enabling authenticated "
1453 "provisioning TLS cipher suites");
1454 ciphers[count++] = TLS_CIPHER_RSA_DHE_AES128_SHA;
1455 ciphers[count++] = TLS_CIPHER_AES128_SHA;
1456 ciphers[count++] = TLS_CIPHER_RC4_SHA;
1459 ciphers[count++] = TLS_CIPHER_NONE;
1461 if (tls_connection_set_cipher_list(sm->ssl_ctx, data->ssl.conn,
1463 wpa_printf(MSG_INFO, "EAP-FAST: Could not configure TLS "
1464 "cipher suites for provisioning");
1472 static int eap_fast_process_start(struct eap_sm *sm,
1473 struct eap_fast_data *data, u8 flags,
1474 const u8 *pos, size_t left)
1479 /* EAP-FAST Version negotiation (section 3.1) */
1480 wpa_printf(MSG_DEBUG, "EAP-FAST: Start (server ver=%d, own ver=%d)",
1481 flags & EAP_TLS_VERSION_MASK, data->fast_version);
1482 if ((flags & EAP_TLS_VERSION_MASK) < data->fast_version)
1483 data->fast_version = flags & EAP_TLS_VERSION_MASK;
1484 wpa_printf(MSG_DEBUG, "EAP-FAST: Using FAST version %d",
1485 data->fast_version);
1487 a_id = eap_fast_get_a_id(pos, left, &a_id_len);
1488 eap_fast_select_pac(data, a_id, a_id_len);
1490 if (data->resuming && data->current_pac) {
1491 wpa_printf(MSG_DEBUG, "EAP-FAST: Trying to resume session - "
1492 "do not add PAC-Opaque to TLS ClientHello");
1493 if (eap_fast_clear_pac_opaque_ext(sm, data) < 0)
1495 } else if (data->current_pac) {
1497 * PAC found for the A-ID and we are not resuming an old
1498 * session, so add PAC-Opaque extension to ClientHello.
1500 if (eap_fast_use_pac_opaque(sm, data, data->current_pac) < 0)
1503 /* No PAC found, so we must provision one. */
1504 if (!data->provisioning_allowed) {
1505 wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC found and "
1506 "provisioning disabled");
1509 wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC found - "
1510 "starting provisioning");
1511 if (eap_fast_set_provisioning_ciphers(sm, data) < 0 ||
1512 eap_fast_clear_pac_opaque_ext(sm, data) < 0)
1514 data->provisioning = 1;
1521 static struct wpabuf * eap_fast_process(struct eap_sm *sm, void *priv,
1522 struct eap_method_ret *ret,
1523 const struct wpabuf *reqData)
1525 const struct eap_hdr *req;
1529 struct wpabuf *resp;
1531 struct eap_fast_data *data = priv;
1533 pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_FAST, ret,
1534 reqData, &left, &flags);
1538 req = wpabuf_head(reqData);
1539 id = req->identifier;
1541 if (flags & EAP_TLS_FLAGS_START) {
1542 if (eap_fast_process_start(sm, data, flags, pos, left) < 0)
1545 left = 0; /* A-ID is not used in further packet processing */
1549 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1551 /* Process tunneled (encrypted) phase 2 data. */
1553 wpabuf_set(&msg, pos, left);
1554 res = eap_fast_decrypt(sm, data, ret, req, &msg, &resp);
1556 ret->methodState = METHOD_DONE;
1557 ret->decision = DECISION_FAIL;
1559 * Ack possible Alert that may have caused failure in
1565 /* Continue processing TLS handshake (phase 1). */
1566 res = eap_peer_tls_process_helper(sm, &data->ssl,
1568 data->fast_version, id, pos,
1571 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
1573 wpa_printf(MSG_DEBUG,
1574 "EAP-FAST: TLS done, proceed to Phase 2");
1575 if (data->provisioning &&
1576 (!(data->provisioning_allowed &
1577 EAP_FAST_PROV_AUTH) ||
1578 tls_get_cipher(sm->ssl_ctx, data->ssl.conn,
1579 cipher, sizeof(cipher)) < 0 ||
1580 os_strstr(cipher, "ADH-") ||
1581 os_strstr(cipher, "anon"))) {
1582 wpa_printf(MSG_DEBUG, "EAP-FAST: Using "
1583 "anonymous (unauthenticated) "
1585 data->anon_provisioning = 1;
1587 data->anon_provisioning = 0;
1589 eap_fast_derive_keys(sm, data);
1595 * Application data included in the handshake message.
1597 wpabuf_free(data->pending_phase2_req);
1598 data->pending_phase2_req = resp;
1600 wpabuf_set(&msg, pos, left);
1601 res = eap_fast_decrypt(sm, data, ret, req, &msg,
1608 return eap_peer_tls_build_ack(id, EAP_TYPE_FAST,
1609 data->fast_version);
1617 static Boolean eap_fast_has_reauth_data(struct eap_sm *sm, void *priv)
1619 struct eap_fast_data *data = priv;
1620 return tls_connection_established(sm->ssl_ctx, data->ssl.conn);
1624 static void eap_fast_deinit_for_reauth(struct eap_sm *sm, void *priv)
1626 struct eap_fast_data *data = priv;
1627 os_free(data->key_block_p);
1628 data->key_block_p = NULL;
1629 wpabuf_free(data->pending_phase2_req);
1630 data->pending_phase2_req = NULL;
1634 static void * eap_fast_init_for_reauth(struct eap_sm *sm, void *priv)
1636 struct eap_fast_data *data = priv;
1637 if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
1641 os_memset(data->key_data, 0, EAP_FAST_KEY_LEN);
1642 os_memset(data->emsk, 0, EAP_EMSK_LEN);
1643 os_free(data->session_id);
1644 data->session_id = NULL;
1645 if (data->phase2_priv && data->phase2_method &&
1646 data->phase2_method->init_for_reauth)
1647 data->phase2_method->init_for_reauth(sm, data->phase2_priv);
1648 data->phase2_success = 0;
1650 data->provisioning = 0;
1651 data->anon_provisioning = 0;
1652 data->simck_idx = 0;
1658 static int eap_fast_get_status(struct eap_sm *sm, void *priv, char *buf,
1659 size_t buflen, int verbose)
1661 struct eap_fast_data *data = priv;
1664 len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
1665 if (data->phase2_method) {
1666 ret = os_snprintf(buf + len, buflen - len,
1667 "EAP-FAST Phase2 method=%s\n",
1668 data->phase2_method->name);
1669 if (os_snprintf_error(buflen - len, ret))
1677 static Boolean eap_fast_isKeyAvailable(struct eap_sm *sm, void *priv)
1679 struct eap_fast_data *data = priv;
1680 return data->success;
1684 static u8 * eap_fast_getKey(struct eap_sm *sm, void *priv, size_t *len)
1686 struct eap_fast_data *data = priv;
1692 key = os_malloc(EAP_FAST_KEY_LEN);
1696 *len = EAP_FAST_KEY_LEN;
1697 os_memcpy(key, data->key_data, EAP_FAST_KEY_LEN);
1703 static u8 * eap_fast_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
1705 struct eap_fast_data *data = priv;
1711 id = os_malloc(data->id_len);
1715 *len = data->id_len;
1716 os_memcpy(id, data->session_id, data->id_len);
1722 static u8 * eap_fast_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
1724 struct eap_fast_data *data = priv;
1730 key = os_malloc(EAP_EMSK_LEN);
1734 *len = EAP_EMSK_LEN;
1735 os_memcpy(key, data->emsk, EAP_EMSK_LEN);
1741 int eap_peer_fast_register(void)
1743 struct eap_method *eap;
1746 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
1747 EAP_VENDOR_IETF, EAP_TYPE_FAST, "FAST");
1751 eap->init = eap_fast_init;
1752 eap->deinit = eap_fast_deinit;
1753 eap->process = eap_fast_process;
1754 eap->isKeyAvailable = eap_fast_isKeyAvailable;
1755 eap->getKey = eap_fast_getKey;
1756 eap->getSessionId = eap_fast_get_session_id;
1757 eap->get_status = eap_fast_get_status;
1759 eap->has_reauth_data = eap_fast_has_reauth_data;
1760 eap->deinit_for_reauth = eap_fast_deinit_for_reauth;
1761 eap->init_for_reauth = eap_fast_init_for_reauth;
1763 eap->get_emsk = eap_fast_get_emsk;
1765 ret = eap_peer_method_register(eap);
1767 eap_peer_method_free(eap);