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 int 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);
301 static int eap_fast_derive_key_provisioning(struct eap_sm *sm,
302 struct eap_fast_data *data)
304 os_free(data->key_block_p);
305 data->key_block_p = (struct eap_fast_key_block_provisioning *)
306 eap_fast_derive_key(sm->ssl_ctx, data->ssl.conn,
308 sizeof(*data->key_block_p));
309 if (data->key_block_p == NULL) {
310 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive key block");
314 * RFC 4851, Section 5.2:
315 * S-IMCK[0] = session_key_seed
317 wpa_hexdump_key(MSG_DEBUG,
318 "EAP-FAST: session_key_seed (SKS = S-IMCK[0])",
319 data->key_block_p->session_key_seed,
320 sizeof(data->key_block_p->session_key_seed));
322 os_memcpy(data->simck, data->key_block_p->session_key_seed,
324 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: server_challenge",
325 data->key_block_p->server_challenge,
326 sizeof(data->key_block_p->server_challenge));
327 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: client_challenge",
328 data->key_block_p->client_challenge,
329 sizeof(data->key_block_p->client_challenge));
334 static int eap_fast_derive_keys(struct eap_sm *sm, struct eap_fast_data *data)
338 if (data->anon_provisioning)
339 res = eap_fast_derive_key_provisioning(sm, data);
341 res = eap_fast_derive_key_auth(sm, data);
346 static int eap_fast_init_phase2_method(struct eap_sm *sm,
347 struct eap_fast_data *data)
349 data->phase2_method =
350 eap_peer_get_eap_method(data->phase2_type.vendor,
351 data->phase2_type.method);
352 if (data->phase2_method == NULL)
355 if (data->key_block_p) {
356 sm->auth_challenge = data->key_block_p->server_challenge;
357 sm->peer_challenge = data->key_block_p->client_challenge;
360 data->phase2_priv = data->phase2_method->init(sm);
362 sm->auth_challenge = NULL;
363 sm->peer_challenge = NULL;
365 return data->phase2_priv == NULL ? -1 : 0;
369 static int eap_fast_select_phase2_method(struct eap_fast_data *data, u8 type)
373 /* TODO: TNC with anonymous provisioning; need to require both
374 * completed MSCHAPv2 and TNC */
376 if (data->anon_provisioning && type != EAP_TYPE_MSCHAPV2) {
377 wpa_printf(MSG_INFO, "EAP-FAST: Only EAP-MSCHAPv2 is allowed "
378 "during unauthenticated provisioning; reject phase2"
384 if (type == EAP_TYPE_TNC) {
385 data->phase2_type.vendor = EAP_VENDOR_IETF;
386 data->phase2_type.method = EAP_TYPE_TNC;
387 wpa_printf(MSG_DEBUG, "EAP-FAST: Selected Phase 2 EAP "
388 "vendor %d method %d for TNC",
389 data->phase2_type.vendor,
390 data->phase2_type.method);
395 for (i = 0; i < data->num_phase2_types; i++) {
396 if (data->phase2_types[i].vendor != EAP_VENDOR_IETF ||
397 data->phase2_types[i].method != type)
400 data->phase2_type.vendor = data->phase2_types[i].vendor;
401 data->phase2_type.method = data->phase2_types[i].method;
402 wpa_printf(MSG_DEBUG, "EAP-FAST: Selected Phase 2 EAP "
403 "vendor %d method %d",
404 data->phase2_type.vendor,
405 data->phase2_type.method);
409 if (type != data->phase2_type.method || type == EAP_TYPE_NONE)
416 static int eap_fast_phase2_request(struct eap_sm *sm,
417 struct eap_fast_data *data,
418 struct eap_method_ret *ret,
420 struct wpabuf **resp)
422 size_t len = be_to_host16(hdr->length);
424 struct eap_method_ret iret;
425 struct eap_peer_config *config = eap_get_config(sm);
428 if (len <= sizeof(struct eap_hdr)) {
429 wpa_printf(MSG_INFO, "EAP-FAST: too short "
430 "Phase 2 request (len=%lu)", (unsigned long) len);
433 pos = (u8 *) (hdr + 1);
434 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 Request: type=%d", *pos);
435 if (*pos == EAP_TYPE_IDENTITY) {
436 *resp = eap_sm_buildIdentity(sm, hdr->identifier, 1);
440 if (data->phase2_priv && data->phase2_method &&
441 *pos != data->phase2_type.method) {
442 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 EAP sequence - "
443 "deinitialize previous method");
444 data->phase2_method->deinit(sm, data->phase2_priv);
445 data->phase2_method = NULL;
446 data->phase2_priv = NULL;
447 data->phase2_type.vendor = EAP_VENDOR_IETF;
448 data->phase2_type.method = EAP_TYPE_NONE;
451 if (data->phase2_type.vendor == EAP_VENDOR_IETF &&
452 data->phase2_type.method == EAP_TYPE_NONE &&
453 eap_fast_select_phase2_method(data, *pos) < 0) {
454 if (eap_peer_tls_phase2_nak(data->phase2_types,
455 data->num_phase2_types,
461 if ((data->phase2_priv == NULL &&
462 eap_fast_init_phase2_method(sm, data) < 0) ||
463 data->phase2_method == NULL) {
464 wpa_printf(MSG_INFO, "EAP-FAST: Failed to initialize "
465 "Phase 2 EAP method %d", *pos);
466 ret->methodState = METHOD_DONE;
467 ret->decision = DECISION_FAIL;
471 os_memset(&iret, 0, sizeof(iret));
472 wpabuf_set(&msg, hdr, len);
473 *resp = data->phase2_method->process(sm, data->phase2_priv, &iret,
476 (iret.methodState == METHOD_DONE &&
477 iret.decision == DECISION_FAIL)) {
478 ret->methodState = METHOD_DONE;
479 ret->decision = DECISION_FAIL;
480 } else if ((iret.methodState == METHOD_DONE ||
481 iret.methodState == METHOD_MAY_CONT) &&
482 (iret.decision == DECISION_UNCOND_SUCC ||
483 iret.decision == DECISION_COND_SUCC)) {
484 data->phase2_success = 1;
487 if (*resp == NULL && config &&
488 (config->pending_req_identity || config->pending_req_password ||
489 config->pending_req_otp || config->pending_req_new_password)) {
490 wpabuf_free(data->pending_phase2_req);
491 data->pending_phase2_req = wpabuf_alloc_copy(hdr, len);
492 } else if (*resp == NULL)
499 static struct wpabuf * eap_fast_tlv_nak(int vendor_id, int tlv_type)
502 struct eap_tlv_nak_tlv *nak;
503 buf = wpabuf_alloc(sizeof(*nak));
506 nak = wpabuf_put(buf, sizeof(*nak));
507 nak->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY | EAP_TLV_NAK_TLV);
508 nak->length = host_to_be16(6);
509 nak->vendor_id = host_to_be32(vendor_id);
510 nak->nak_type = host_to_be16(tlv_type);
515 static struct wpabuf * eap_fast_tlv_result(int status, int intermediate)
518 struct eap_tlv_intermediate_result_tlv *result;
519 buf = wpabuf_alloc(sizeof(*result));
522 wpa_printf(MSG_DEBUG, "EAP-FAST: Add %sResult TLV(status=%d)",
523 intermediate ? "Intermediate " : "", status);
524 result = wpabuf_put(buf, sizeof(*result));
525 result->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
527 EAP_TLV_INTERMEDIATE_RESULT_TLV :
528 EAP_TLV_RESULT_TLV));
529 result->length = host_to_be16(2);
530 result->status = host_to_be16(status);
535 static struct wpabuf * eap_fast_tlv_pac_ack(void)
538 struct eap_tlv_result_tlv *res;
539 struct eap_tlv_pac_ack_tlv *ack;
541 buf = wpabuf_alloc(sizeof(*res) + sizeof(*ack));
545 wpa_printf(MSG_DEBUG, "EAP-FAST: Add PAC TLV (ack)");
546 ack = wpabuf_put(buf, sizeof(*ack));
547 ack->tlv_type = host_to_be16(EAP_TLV_PAC_TLV |
548 EAP_TLV_TYPE_MANDATORY);
549 ack->length = host_to_be16(sizeof(*ack) - sizeof(struct eap_tlv_hdr));
550 ack->pac_type = host_to_be16(PAC_TYPE_PAC_ACKNOWLEDGEMENT);
551 ack->pac_len = host_to_be16(2);
552 ack->result = host_to_be16(EAP_TLV_RESULT_SUCCESS);
558 static struct wpabuf * eap_fast_process_eap_payload_tlv(
559 struct eap_sm *sm, struct eap_fast_data *data,
560 struct eap_method_ret *ret,
561 u8 *eap_payload_tlv, size_t eap_payload_tlv_len)
564 struct wpabuf *resp = NULL;
566 if (eap_payload_tlv_len < sizeof(*hdr)) {
567 wpa_printf(MSG_DEBUG, "EAP-FAST: too short EAP "
568 "Payload TLV (len=%lu)",
569 (unsigned long) eap_payload_tlv_len);
573 hdr = (struct eap_hdr *) eap_payload_tlv;
574 if (be_to_host16(hdr->length) > eap_payload_tlv_len) {
575 wpa_printf(MSG_DEBUG, "EAP-FAST: EAP packet overflow in "
580 if (hdr->code != EAP_CODE_REQUEST) {
581 wpa_printf(MSG_INFO, "EAP-FAST: Unexpected code=%d in "
582 "Phase 2 EAP header", hdr->code);
586 if (eap_fast_phase2_request(sm, data, ret, hdr, &resp)) {
587 wpa_printf(MSG_INFO, "EAP-FAST: Phase2 Request processing "
592 return eap_fast_tlv_eap_payload(resp);
596 static int eap_fast_validate_crypto_binding(
597 struct eap_tlv_crypto_binding_tlv *_bind)
599 wpa_printf(MSG_DEBUG, "EAP-FAST: Crypto-Binding TLV: Version %d "
600 "Received Version %d SubType %d",
601 _bind->version, _bind->received_version, _bind->subtype);
602 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE",
603 _bind->nonce, sizeof(_bind->nonce));
604 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC",
605 _bind->compound_mac, sizeof(_bind->compound_mac));
607 if (_bind->version != EAP_FAST_VERSION ||
608 _bind->received_version != EAP_FAST_VERSION ||
609 _bind->subtype != EAP_TLV_CRYPTO_BINDING_SUBTYPE_REQUEST) {
610 wpa_printf(MSG_INFO, "EAP-FAST: Invalid version/subtype in "
611 "Crypto-Binding TLV: Version %d "
612 "Received Version %d SubType %d",
613 _bind->version, _bind->received_version,
622 static void eap_fast_write_crypto_binding(
623 struct eap_tlv_crypto_binding_tlv *rbind,
624 struct eap_tlv_crypto_binding_tlv *_bind, const u8 *cmk)
626 rbind->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
627 EAP_TLV_CRYPTO_BINDING_TLV);
628 rbind->length = host_to_be16(sizeof(*rbind) -
629 sizeof(struct eap_tlv_hdr));
630 rbind->version = EAP_FAST_VERSION;
631 rbind->received_version = _bind->version;
632 rbind->subtype = EAP_TLV_CRYPTO_BINDING_SUBTYPE_RESPONSE;
633 os_memcpy(rbind->nonce, _bind->nonce, sizeof(_bind->nonce));
634 inc_byte_array(rbind->nonce, sizeof(rbind->nonce));
635 hmac_sha1(cmk, EAP_FAST_CMK_LEN, (u8 *) rbind, sizeof(*rbind),
636 rbind->compound_mac);
638 wpa_printf(MSG_DEBUG, "EAP-FAST: Reply Crypto-Binding TLV: Version %d "
639 "Received Version %d SubType %d",
640 rbind->version, rbind->received_version, rbind->subtype);
641 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE",
642 rbind->nonce, sizeof(rbind->nonce));
643 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC",
644 rbind->compound_mac, sizeof(rbind->compound_mac));
648 static int eap_fast_get_phase2_key(struct eap_sm *sm,
649 struct eap_fast_data *data,
650 u8 *isk, size_t isk_len)
655 os_memset(isk, 0, isk_len);
657 if (data->phase2_method == NULL || data->phase2_priv == NULL) {
658 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 method not "
663 if (data->phase2_method->isKeyAvailable == NULL ||
664 data->phase2_method->getKey == NULL)
667 if (!data->phase2_method->isKeyAvailable(sm, data->phase2_priv) ||
668 (key = data->phase2_method->getKey(sm, data->phase2_priv,
669 &key_len)) == NULL) {
670 wpa_printf(MSG_DEBUG, "EAP-FAST: Could not get key material "
675 if (key_len > isk_len)
678 data->phase2_method->vendor == EAP_VENDOR_IETF &&
679 data->phase2_method->method == EAP_TYPE_MSCHAPV2) {
681 * EAP-FAST uses reverse order for MS-MPPE keys when deriving
682 * MSK from EAP-MSCHAPv2. Swap the keys here to get the correct
683 * ISK for EAP-FAST cryptobinding.
685 os_memcpy(isk, key + 16, 16);
686 os_memcpy(isk + 16, key, 16);
688 os_memcpy(isk, key, key_len);
695 static int eap_fast_get_cmk(struct eap_sm *sm, struct eap_fast_data *data,
698 u8 isk[32], imck[60];
700 wpa_printf(MSG_DEBUG, "EAP-FAST: Determining CMK[%d] for Compound MIC "
701 "calculation", data->simck_idx + 1);
704 * RFC 4851, Section 5.2:
705 * IMCK[j] = T-PRF(S-IMCK[j-1], "Inner Methods Compound Keys",
707 * S-IMCK[j] = first 40 octets of IMCK[j]
708 * CMK[j] = last 20 octets of IMCK[j]
711 if (eap_fast_get_phase2_key(sm, data, isk, sizeof(isk)) < 0)
713 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: ISK[j]", isk, sizeof(isk));
714 sha1_t_prf(data->simck, EAP_FAST_SIMCK_LEN,
715 "Inner Methods Compound Keys",
716 isk, sizeof(isk), imck, sizeof(imck));
718 os_memcpy(data->simck, imck, EAP_FAST_SIMCK_LEN);
719 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: S-IMCK[j]",
720 data->simck, EAP_FAST_SIMCK_LEN);
721 os_memcpy(cmk, imck + EAP_FAST_SIMCK_LEN, EAP_FAST_CMK_LEN);
722 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: CMK[j]",
723 cmk, EAP_FAST_CMK_LEN);
729 static u8 * eap_fast_write_pac_request(u8 *pos, u16 pac_type)
731 struct eap_tlv_hdr *pac;
732 struct eap_tlv_request_action_tlv *act;
733 struct eap_tlv_pac_type_tlv *type;
735 act = (struct eap_tlv_request_action_tlv *) pos;
736 act->tlv_type = host_to_be16(EAP_TLV_REQUEST_ACTION_TLV);
737 act->length = host_to_be16(2);
738 act->action = host_to_be16(EAP_TLV_ACTION_PROCESS_TLV);
740 pac = (struct eap_tlv_hdr *) (act + 1);
741 pac->tlv_type = host_to_be16(EAP_TLV_PAC_TLV);
742 pac->length = host_to_be16(sizeof(*type));
744 type = (struct eap_tlv_pac_type_tlv *) (pac + 1);
745 type->tlv_type = host_to_be16(PAC_TYPE_PAC_TYPE);
746 type->length = host_to_be16(2);
747 type->pac_type = host_to_be16(pac_type);
749 return (u8 *) (type + 1);
753 static struct wpabuf * eap_fast_process_crypto_binding(
754 struct eap_sm *sm, struct eap_fast_data *data,
755 struct eap_method_ret *ret,
756 struct eap_tlv_crypto_binding_tlv *_bind, size_t bind_len)
760 u8 cmk[EAP_FAST_CMK_LEN], cmac[SHA1_MAC_LEN];
764 if (eap_fast_validate_crypto_binding(_bind) < 0)
767 if (eap_fast_get_cmk(sm, data, cmk) < 0)
770 /* Validate received Compound MAC */
771 os_memcpy(cmac, _bind->compound_mac, sizeof(cmac));
772 os_memset(_bind->compound_mac, 0, sizeof(cmac));
773 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Crypto-Binding TLV for Compound "
774 "MAC calculation", (u8 *) _bind, bind_len);
775 hmac_sha1(cmk, EAP_FAST_CMK_LEN, (u8 *) _bind, bind_len,
776 _bind->compound_mac);
777 res = os_memcmp_const(cmac, _bind->compound_mac, sizeof(cmac));
778 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Received Compound MAC",
780 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Calculated Compound MAC",
781 _bind->compound_mac, sizeof(cmac));
783 wpa_printf(MSG_INFO, "EAP-FAST: Compound MAC did not match");
784 os_memcpy(_bind->compound_mac, cmac, sizeof(cmac));
789 * Compound MAC was valid, so authentication succeeded. Reply with
790 * crypto binding to allow server to complete authentication.
793 len = sizeof(struct eap_tlv_crypto_binding_tlv);
794 resp = wpabuf_alloc(len);
798 if (!data->anon_provisioning && data->phase2_success &&
799 eap_fast_derive_msk(data) < 0) {
800 wpa_printf(MSG_INFO, "EAP-FAST: Failed to generate MSK");
801 ret->methodState = METHOD_DONE;
802 ret->decision = DECISION_FAIL;
803 data->phase2_success = 0;
808 if (!data->anon_provisioning && data->phase2_success) {
809 os_free(data->session_id);
810 data->session_id = eap_peer_tls_derive_session_id(
811 sm, &data->ssl, EAP_TYPE_FAST, &data->id_len);
812 if (data->session_id) {
813 wpa_hexdump(MSG_DEBUG, "EAP-FAST: Derived Session-Id",
814 data->session_id, data->id_len);
816 wpa_printf(MSG_ERROR, "EAP-FAST: Failed to derive "
823 pos = wpabuf_put(resp, sizeof(struct eap_tlv_crypto_binding_tlv));
824 eap_fast_write_crypto_binding((struct eap_tlv_crypto_binding_tlv *)
831 static void eap_fast_parse_pac_tlv(struct eap_fast_pac *entry, int type,
832 u8 *pos, size_t len, int *pac_key_found)
834 switch (type & 0x7fff) {
835 case PAC_TYPE_PAC_KEY:
836 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: PAC-Key", pos, len);
837 if (len != EAP_FAST_PAC_KEY_LEN) {
838 wpa_printf(MSG_DEBUG, "EAP-FAST: Invalid PAC-Key "
839 "length %lu", (unsigned long) len);
843 os_memcpy(entry->pac_key, pos, len);
845 case PAC_TYPE_PAC_OPAQUE:
846 wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Opaque", pos, len);
847 entry->pac_opaque = pos;
848 entry->pac_opaque_len = len;
850 case PAC_TYPE_PAC_INFO:
851 wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Info", pos, len);
852 entry->pac_info = pos;
853 entry->pac_info_len = len;
856 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignored unknown PAC type %d",
863 static int eap_fast_process_pac_tlv(struct eap_fast_pac *entry,
864 u8 *pac, size_t pac_len)
866 struct pac_tlv_hdr *hdr;
869 int type, pac_key_found = 0;
874 while (left > sizeof(*hdr)) {
875 hdr = (struct pac_tlv_hdr *) pos;
876 type = be_to_host16(hdr->type);
877 len = be_to_host16(hdr->len);
879 left -= sizeof(*hdr);
881 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV overrun "
882 "(type=%d len=%lu left=%lu)",
883 type, (unsigned long) len,
884 (unsigned long) left);
888 eap_fast_parse_pac_tlv(entry, type, pos, len, &pac_key_found);
894 if (!pac_key_found || !entry->pac_opaque || !entry->pac_info) {
895 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV does not include "
896 "all the required fields");
904 static int eap_fast_parse_pac_info(struct eap_fast_pac *entry, int type,
911 switch (type & 0x7fff) {
912 case PAC_TYPE_CRED_LIFETIME:
914 wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Info - "
915 "Invalid CRED_LIFETIME length - ignored",
921 * This is not currently saved separately in PAC files since
922 * the server can automatically initiate PAC update when
923 * needed. Anyway, the information is available from PAC-Info
924 * dump if it is needed for something in the future.
926 lifetime = WPA_GET_BE32(pos);
928 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info - CRED_LIFETIME %d "
930 lifetime, (lifetime - (u32) now.sec) / 86400);
933 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - A-ID",
936 entry->a_id_len = len;
939 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - I-ID",
942 entry->i_id_len = len;
944 case PAC_TYPE_A_ID_INFO:
945 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - A-ID-Info",
947 entry->a_id_info = pos;
948 entry->a_id_info_len = len;
950 case PAC_TYPE_PAC_TYPE:
951 /* RFC 5422, Section 4.2.6 - PAC-Type TLV */
953 wpa_printf(MSG_INFO, "EAP-FAST: Invalid PAC-Type "
954 "length %lu (expected 2)",
955 (unsigned long) len);
956 wpa_hexdump_ascii(MSG_DEBUG,
957 "EAP-FAST: PAC-Info - PAC-Type",
961 pac_type = WPA_GET_BE16(pos);
962 if (pac_type != PAC_TYPE_TUNNEL_PAC &&
963 pac_type != PAC_TYPE_USER_AUTHORIZATION &&
964 pac_type != PAC_TYPE_MACHINE_AUTHENTICATION) {
965 wpa_printf(MSG_INFO, "EAP-FAST: Unsupported PAC Type "
970 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info - PAC-Type %d",
972 entry->pac_type = pac_type;
975 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignored unknown PAC-Info "
984 static int eap_fast_process_pac_info(struct eap_fast_pac *entry)
986 struct pac_tlv_hdr *hdr;
991 /* RFC 5422, Section 4.2.4 */
993 /* PAC-Type defaults to Tunnel PAC (Type 1) */
994 entry->pac_type = PAC_TYPE_TUNNEL_PAC;
996 pos = entry->pac_info;
997 left = entry->pac_info_len;
998 while (left > sizeof(*hdr)) {
999 hdr = (struct pac_tlv_hdr *) pos;
1000 type = be_to_host16(hdr->type);
1001 len = be_to_host16(hdr->len);
1002 pos += sizeof(*hdr);
1003 left -= sizeof(*hdr);
1005 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info overrun "
1006 "(type=%d len=%lu left=%lu)",
1007 type, (unsigned long) len,
1008 (unsigned long) left);
1012 if (eap_fast_parse_pac_info(entry, type, pos, len) < 0)
1019 if (entry->a_id == NULL || entry->a_id_info == NULL) {
1020 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info does not include "
1021 "all the required fields");
1029 static struct wpabuf * eap_fast_process_pac(struct eap_sm *sm,
1030 struct eap_fast_data *data,
1031 struct eap_method_ret *ret,
1032 u8 *pac, size_t pac_len)
1034 struct eap_peer_config *config = eap_get_config(sm);
1035 struct eap_fast_pac entry;
1037 os_memset(&entry, 0, sizeof(entry));
1038 if (eap_fast_process_pac_tlv(&entry, pac, pac_len) ||
1039 eap_fast_process_pac_info(&entry))
1042 eap_fast_add_pac(&data->pac, &data->current_pac, &entry);
1043 eap_fast_pac_list_truncate(data->pac, data->max_pac_list_len);
1044 if (data->use_pac_binary_format)
1045 eap_fast_save_pac_bin(sm, data->pac, config->pac_file);
1047 eap_fast_save_pac(sm, data->pac, config->pac_file);
1049 if (data->provisioning) {
1050 if (data->anon_provisioning) {
1052 * Unauthenticated provisioning does not provide keying
1053 * material and must end with an EAP-Failure.
1054 * Authentication will be done separately after this.
1057 ret->decision = DECISION_FAIL;
1060 * Server may or may not allow authenticated
1061 * provisioning also for key generation.
1063 ret->decision = DECISION_COND_SUCC;
1065 wpa_printf(MSG_DEBUG, "EAP-FAST: Send PAC-Acknowledgement TLV "
1066 "- Provisioning completed successfully");
1067 sm->expected_failure = 1;
1070 * This is PAC refreshing, i.e., normal authentication that is
1071 * expected to be completed with an EAP-Success. However,
1072 * RFC 5422, Section 3.5 allows EAP-Failure to be sent even
1073 * after protected success exchange in case of EAP-Fast
1074 * provisioning, so we better use DECISION_COND_SUCC here
1075 * instead of DECISION_UNCOND_SUCC.
1077 wpa_printf(MSG_DEBUG, "EAP-FAST: Send PAC-Acknowledgement TLV "
1078 "- PAC refreshing completed successfully");
1079 ret->decision = DECISION_COND_SUCC;
1081 ret->methodState = METHOD_DONE;
1082 return eap_fast_tlv_pac_ack();
1086 static int eap_fast_parse_decrypted(struct wpabuf *decrypted,
1087 struct eap_fast_tlv_parse *tlv,
1088 struct wpabuf **resp)
1090 int mandatory, tlv_type, res;
1094 os_memset(tlv, 0, sizeof(*tlv));
1096 /* Parse TLVs from the decrypted Phase 2 data */
1097 pos = wpabuf_mhead(decrypted);
1098 end = pos + wpabuf_len(decrypted);
1099 while (pos + 4 < end) {
1100 mandatory = pos[0] & 0x80;
1101 tlv_type = WPA_GET_BE16(pos) & 0x3fff;
1103 len = WPA_GET_BE16(pos);
1105 if (len > (size_t) (end - pos)) {
1106 wpa_printf(MSG_INFO, "EAP-FAST: TLV overflow");
1109 wpa_printf(MSG_DEBUG, "EAP-FAST: Received Phase 2: "
1110 "TLV type %d length %u%s",
1111 tlv_type, (unsigned int) len,
1112 mandatory ? " (mandatory)" : "");
1114 res = eap_fast_parse_tlv(tlv, tlv_type, pos, len);
1119 wpa_printf(MSG_DEBUG, "EAP-FAST: Nak unknown "
1120 "mandatory TLV type %d", tlv_type);
1121 *resp = eap_fast_tlv_nak(0, tlv_type);
1124 wpa_printf(MSG_DEBUG, "EAP-FAST: ignored "
1125 "unknown optional TLV type %d",
1137 static int eap_fast_encrypt_response(struct eap_sm *sm,
1138 struct eap_fast_data *data,
1139 struct wpabuf *resp,
1140 u8 identifier, struct wpabuf **out_data)
1145 wpa_hexdump_buf(MSG_DEBUG, "EAP-FAST: Encrypting Phase 2 data",
1147 if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_FAST,
1148 data->fast_version, identifier,
1150 wpa_printf(MSG_INFO, "EAP-FAST: Failed to encrypt a Phase 2 "
1159 static struct wpabuf * eap_fast_pac_request(void)
1164 tmp = wpabuf_alloc(sizeof(struct eap_tlv_hdr) +
1165 sizeof(struct eap_tlv_request_action_tlv) +
1166 sizeof(struct eap_tlv_pac_type_tlv));
1170 pos = wpabuf_put(tmp, 0);
1171 pos2 = eap_fast_write_pac_request(pos, PAC_TYPE_TUNNEL_PAC);
1172 wpabuf_put(tmp, pos2 - pos);
1177 static int eap_fast_process_decrypted(struct eap_sm *sm,
1178 struct eap_fast_data *data,
1179 struct eap_method_ret *ret,
1181 struct wpabuf *decrypted,
1182 struct wpabuf **out_data)
1184 struct wpabuf *resp = NULL, *tmp;
1185 struct eap_fast_tlv_parse tlv;
1188 if (eap_fast_parse_decrypted(decrypted, &tlv, &resp) < 0)
1191 return eap_fast_encrypt_response(sm, data, resp,
1192 identifier, out_data);
1194 if (tlv.result == EAP_TLV_RESULT_FAILURE) {
1195 resp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 0);
1196 return eap_fast_encrypt_response(sm, data, resp,
1197 identifier, out_data);
1200 if (tlv.iresult == EAP_TLV_RESULT_FAILURE) {
1201 resp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 1);
1202 return eap_fast_encrypt_response(sm, data, resp,
1203 identifier, out_data);
1206 if (tlv.crypto_binding) {
1207 tmp = eap_fast_process_crypto_binding(sm, data, ret,
1209 tlv.crypto_binding_len);
1213 resp = wpabuf_concat(resp, tmp);
1216 if (tlv.iresult == EAP_TLV_RESULT_SUCCESS) {
1217 tmp = eap_fast_tlv_result(failed ? EAP_TLV_RESULT_FAILURE :
1218 EAP_TLV_RESULT_SUCCESS, 1);
1219 resp = wpabuf_concat(resp, tmp);
1222 if (tlv.eap_payload_tlv) {
1223 tmp = eap_fast_process_eap_payload_tlv(
1224 sm, data, ret, tlv.eap_payload_tlv,
1225 tlv.eap_payload_tlv_len);
1226 resp = wpabuf_concat(resp, tmp);
1229 if (tlv.pac && tlv.result != EAP_TLV_RESULT_SUCCESS) {
1230 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV without Result TLV "
1231 "acknowledging success");
1233 } else if (tlv.pac && tlv.result == EAP_TLV_RESULT_SUCCESS) {
1234 tmp = eap_fast_process_pac(sm, data, ret, tlv.pac,
1236 resp = wpabuf_concat(resp, tmp);
1239 if (data->current_pac == NULL && data->provisioning &&
1240 !data->anon_provisioning && !tlv.pac &&
1241 (tlv.iresult == EAP_TLV_RESULT_SUCCESS ||
1242 tlv.result == EAP_TLV_RESULT_SUCCESS)) {
1244 * Need to request Tunnel PAC when using authenticated
1247 wpa_printf(MSG_DEBUG, "EAP-FAST: Request Tunnel PAC");
1248 tmp = eap_fast_pac_request();
1249 resp = wpabuf_concat(resp, tmp);
1252 if (tlv.result == EAP_TLV_RESULT_SUCCESS && !failed) {
1253 tmp = eap_fast_tlv_result(EAP_TLV_RESULT_SUCCESS, 0);
1254 resp = wpabuf_concat(tmp, resp);
1255 } else if (failed) {
1256 tmp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 0);
1257 resp = wpabuf_concat(tmp, resp);
1260 if (resp && tlv.result == EAP_TLV_RESULT_SUCCESS && !failed &&
1261 tlv.crypto_binding && data->phase2_success) {
1262 if (data->anon_provisioning) {
1263 wpa_printf(MSG_DEBUG, "EAP-FAST: Unauthenticated "
1264 "provisioning completed successfully.");
1265 ret->methodState = METHOD_DONE;
1266 ret->decision = DECISION_FAIL;
1267 sm->expected_failure = 1;
1269 wpa_printf(MSG_DEBUG, "EAP-FAST: Authentication "
1270 "completed successfully.");
1271 if (data->provisioning)
1272 ret->methodState = METHOD_MAY_CONT;
1274 ret->methodState = METHOD_DONE;
1275 ret->decision = DECISION_UNCOND_SUCC;
1280 wpa_printf(MSG_DEBUG, "EAP-FAST: No recognized TLVs - send "
1281 "empty response packet");
1282 resp = wpabuf_alloc(1);
1285 return eap_fast_encrypt_response(sm, data, resp, identifier,
1290 static int eap_fast_decrypt(struct eap_sm *sm, struct eap_fast_data *data,
1291 struct eap_method_ret *ret, u8 identifier,
1292 const struct wpabuf *in_data,
1293 struct wpabuf **out_data)
1295 struct wpabuf *in_decrypted;
1298 wpa_printf(MSG_DEBUG, "EAP-FAST: Received %lu bytes encrypted data for"
1299 " Phase 2", (unsigned long) wpabuf_len(in_data));
1301 if (data->pending_phase2_req) {
1302 wpa_printf(MSG_DEBUG, "EAP-FAST: Pending Phase 2 request - "
1303 "skip decryption and use old data");
1304 /* Clear TLS reassembly state. */
1305 eap_peer_tls_reset_input(&data->ssl);
1307 in_decrypted = data->pending_phase2_req;
1308 data->pending_phase2_req = NULL;
1312 if (wpabuf_len(in_data) == 0) {
1313 /* Received TLS ACK - requesting more fragments */
1314 return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_FAST,
1316 identifier, NULL, out_data);
1319 res = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
1324 wpa_hexdump_buf(MSG_MSGDUMP, "EAP-FAST: Decrypted Phase 2 TLV(s)",
1327 if (wpabuf_len(in_decrypted) < 4) {
1328 wpa_printf(MSG_INFO, "EAP-FAST: Too short Phase 2 "
1329 "TLV frame (len=%lu)",
1330 (unsigned long) wpabuf_len(in_decrypted));
1331 wpabuf_free(in_decrypted);
1335 res = eap_fast_process_decrypted(sm, data, ret, identifier,
1336 in_decrypted, out_data);
1338 wpabuf_free(in_decrypted);
1344 static const u8 * eap_fast_get_a_id(const u8 *buf, size_t len, size_t *id_len)
1347 const struct pac_tlv_hdr *hdr;
1350 * Parse authority identity (A-ID) from the EAP-FAST/Start. This
1351 * supports both raw A-ID and one inside an A-ID TLV.
1355 if (len > sizeof(*hdr)) {
1357 hdr = (const struct pac_tlv_hdr *) buf;
1358 tlen = be_to_host16(hdr->len);
1359 if (be_to_host16(hdr->type) == PAC_TYPE_A_ID &&
1360 sizeof(*hdr) + tlen <= len) {
1361 wpa_printf(MSG_DEBUG, "EAP-FAST: A-ID was in TLV "
1363 a_id = (const u8 *) (hdr + 1);
1367 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: A-ID", a_id, *id_len);
1373 static void eap_fast_select_pac(struct eap_fast_data *data,
1374 const u8 *a_id, size_t a_id_len)
1376 data->current_pac = eap_fast_get_pac(data->pac, a_id, a_id_len,
1377 PAC_TYPE_TUNNEL_PAC);
1378 if (data->current_pac == NULL) {
1380 * Tunnel PAC was not available for this A-ID. Try to use
1381 * Machine Authentication PAC, if one is available.
1383 data->current_pac = eap_fast_get_pac(
1384 data->pac, a_id, a_id_len,
1385 PAC_TYPE_MACHINE_AUTHENTICATION);
1388 if (data->current_pac) {
1389 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC found for this A-ID "
1390 "(PAC-Type %d)", data->current_pac->pac_type);
1391 wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-FAST: A-ID-Info",
1392 data->current_pac->a_id_info,
1393 data->current_pac->a_id_info_len);
1398 static int eap_fast_use_pac_opaque(struct eap_sm *sm,
1399 struct eap_fast_data *data,
1400 struct eap_fast_pac *pac)
1403 size_t tlv_len, olen;
1404 struct eap_tlv_hdr *ehdr;
1406 olen = pac->pac_opaque_len;
1407 tlv_len = sizeof(*ehdr) + olen;
1408 tlv = os_malloc(tlv_len);
1410 ehdr = (struct eap_tlv_hdr *) tlv;
1411 ehdr->tlv_type = host_to_be16(PAC_TYPE_PAC_OPAQUE);
1412 ehdr->length = host_to_be16(olen);
1413 os_memcpy(ehdr + 1, pac->pac_opaque, olen);
1416 tls_connection_client_hello_ext(sm->ssl_ctx, data->ssl.conn,
1418 tlv, tlv_len) < 0) {
1419 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to add PAC-Opaque TLS "
1430 static int eap_fast_clear_pac_opaque_ext(struct eap_sm *sm,
1431 struct eap_fast_data *data)
1433 if (tls_connection_client_hello_ext(sm->ssl_ctx, data->ssl.conn,
1434 TLS_EXT_PAC_OPAQUE, NULL, 0) < 0) {
1435 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to remove PAC-Opaque "
1443 static int eap_fast_set_provisioning_ciphers(struct eap_sm *sm,
1444 struct eap_fast_data *data)
1449 if (data->provisioning_allowed & EAP_FAST_PROV_UNAUTH) {
1450 wpa_printf(MSG_DEBUG, "EAP-FAST: Enabling unauthenticated "
1451 "provisioning TLS cipher suites");
1452 ciphers[count++] = TLS_CIPHER_ANON_DH_AES128_SHA;
1455 if (data->provisioning_allowed & EAP_FAST_PROV_AUTH) {
1456 wpa_printf(MSG_DEBUG, "EAP-FAST: Enabling authenticated "
1457 "provisioning TLS cipher suites");
1458 ciphers[count++] = TLS_CIPHER_RSA_DHE_AES128_SHA;
1459 ciphers[count++] = TLS_CIPHER_AES128_SHA;
1460 ciphers[count++] = TLS_CIPHER_RC4_SHA;
1463 ciphers[count++] = TLS_CIPHER_NONE;
1465 if (tls_connection_set_cipher_list(sm->ssl_ctx, data->ssl.conn,
1467 wpa_printf(MSG_INFO, "EAP-FAST: Could not configure TLS "
1468 "cipher suites for provisioning");
1476 static int eap_fast_process_start(struct eap_sm *sm,
1477 struct eap_fast_data *data, u8 flags,
1478 const u8 *pos, size_t left)
1483 /* EAP-FAST Version negotiation (section 3.1) */
1484 wpa_printf(MSG_DEBUG, "EAP-FAST: Start (server ver=%d, own ver=%d)",
1485 flags & EAP_TLS_VERSION_MASK, data->fast_version);
1486 if ((flags & EAP_TLS_VERSION_MASK) < data->fast_version)
1487 data->fast_version = flags & EAP_TLS_VERSION_MASK;
1488 wpa_printf(MSG_DEBUG, "EAP-FAST: Using FAST version %d",
1489 data->fast_version);
1491 a_id = eap_fast_get_a_id(pos, left, &a_id_len);
1492 eap_fast_select_pac(data, a_id, a_id_len);
1494 if (data->resuming && data->current_pac) {
1495 wpa_printf(MSG_DEBUG, "EAP-FAST: Trying to resume session - "
1496 "do not add PAC-Opaque to TLS ClientHello");
1497 if (eap_fast_clear_pac_opaque_ext(sm, data) < 0)
1499 } else if (data->current_pac) {
1501 * PAC found for the A-ID and we are not resuming an old
1502 * session, so add PAC-Opaque extension to ClientHello.
1504 if (eap_fast_use_pac_opaque(sm, data, data->current_pac) < 0)
1507 /* No PAC found, so we must provision one. */
1508 if (!data->provisioning_allowed) {
1509 wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC found and "
1510 "provisioning disabled");
1513 wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC found - "
1514 "starting provisioning");
1515 if (eap_fast_set_provisioning_ciphers(sm, data) < 0 ||
1516 eap_fast_clear_pac_opaque_ext(sm, data) < 0)
1518 data->provisioning = 1;
1525 static struct wpabuf * eap_fast_process(struct eap_sm *sm, void *priv,
1526 struct eap_method_ret *ret,
1527 const struct wpabuf *reqData)
1529 const struct eap_hdr *req;
1533 struct wpabuf *resp;
1535 struct eap_fast_data *data = priv;
1538 pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_FAST, ret,
1539 reqData, &left, &flags);
1543 req = wpabuf_head(reqData);
1544 id = req->identifier;
1546 if (flags & EAP_TLS_FLAGS_START) {
1547 if (eap_fast_process_start(sm, data, flags, pos, left) < 0)
1550 left = 0; /* A-ID is not used in further packet processing */
1553 wpabuf_set(&msg, pos, left);
1556 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1558 /* Process tunneled (encrypted) phase 2 data. */
1559 res = eap_fast_decrypt(sm, data, ret, id, &msg, &resp);
1561 ret->methodState = METHOD_DONE;
1562 ret->decision = DECISION_FAIL;
1564 * Ack possible Alert that may have caused failure in
1570 /* Continue processing TLS handshake (phase 1). */
1571 res = eap_peer_tls_process_helper(sm, &data->ssl,
1573 data->fast_version, id, &msg,
1576 wpa_printf(MSG_DEBUG,
1577 "EAP-FAST: TLS processing failed");
1578 ret->methodState = METHOD_DONE;
1579 ret->decision = DECISION_FAIL;
1583 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
1585 wpa_printf(MSG_DEBUG,
1586 "EAP-FAST: TLS done, proceed to Phase 2");
1587 if (data->provisioning &&
1588 (!(data->provisioning_allowed &
1589 EAP_FAST_PROV_AUTH) ||
1590 tls_get_cipher(sm->ssl_ctx, data->ssl.conn,
1591 cipher, sizeof(cipher)) < 0 ||
1592 os_strstr(cipher, "ADH-") ||
1593 os_strstr(cipher, "anon"))) {
1594 wpa_printf(MSG_DEBUG, "EAP-FAST: Using "
1595 "anonymous (unauthenticated) "
1597 data->anon_provisioning = 1;
1599 data->anon_provisioning = 0;
1601 if (eap_fast_derive_keys(sm, data) < 0) {
1602 wpa_printf(MSG_DEBUG,
1603 "EAP-FAST: Could not derive keys");
1604 ret->methodState = METHOD_DONE;
1605 ret->decision = DECISION_FAIL;
1613 * Application data included in the handshake message.
1615 wpabuf_free(data->pending_phase2_req);
1616 data->pending_phase2_req = resp;
1618 res = eap_fast_decrypt(sm, data, ret, id, &msg, &resp);
1624 return eap_peer_tls_build_ack(id, EAP_TYPE_FAST,
1625 data->fast_version);
1633 static Boolean eap_fast_has_reauth_data(struct eap_sm *sm, void *priv)
1635 struct eap_fast_data *data = priv;
1636 return tls_connection_established(sm->ssl_ctx, data->ssl.conn);
1640 static void eap_fast_deinit_for_reauth(struct eap_sm *sm, void *priv)
1642 struct eap_fast_data *data = priv;
1643 os_free(data->key_block_p);
1644 data->key_block_p = NULL;
1645 wpabuf_free(data->pending_phase2_req);
1646 data->pending_phase2_req = NULL;
1650 static void * eap_fast_init_for_reauth(struct eap_sm *sm, void *priv)
1652 struct eap_fast_data *data = priv;
1653 if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
1657 os_memset(data->key_data, 0, EAP_FAST_KEY_LEN);
1658 os_memset(data->emsk, 0, EAP_EMSK_LEN);
1659 os_free(data->session_id);
1660 data->session_id = NULL;
1661 if (data->phase2_priv && data->phase2_method &&
1662 data->phase2_method->init_for_reauth)
1663 data->phase2_method->init_for_reauth(sm, data->phase2_priv);
1664 data->phase2_success = 0;
1666 data->provisioning = 0;
1667 data->anon_provisioning = 0;
1668 data->simck_idx = 0;
1674 static int eap_fast_get_status(struct eap_sm *sm, void *priv, char *buf,
1675 size_t buflen, int verbose)
1677 struct eap_fast_data *data = priv;
1680 len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
1681 if (data->phase2_method) {
1682 ret = os_snprintf(buf + len, buflen - len,
1683 "EAP-FAST Phase2 method=%s\n",
1684 data->phase2_method->name);
1685 if (os_snprintf_error(buflen - len, ret))
1693 static Boolean eap_fast_isKeyAvailable(struct eap_sm *sm, void *priv)
1695 struct eap_fast_data *data = priv;
1696 return data->success;
1700 static u8 * eap_fast_getKey(struct eap_sm *sm, void *priv, size_t *len)
1702 struct eap_fast_data *data = priv;
1708 key = os_malloc(EAP_FAST_KEY_LEN);
1712 *len = EAP_FAST_KEY_LEN;
1713 os_memcpy(key, data->key_data, EAP_FAST_KEY_LEN);
1719 static u8 * eap_fast_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
1721 struct eap_fast_data *data = priv;
1727 id = os_malloc(data->id_len);
1731 *len = data->id_len;
1732 os_memcpy(id, data->session_id, data->id_len);
1738 static u8 * eap_fast_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
1740 struct eap_fast_data *data = priv;
1746 key = os_malloc(EAP_EMSK_LEN);
1750 *len = EAP_EMSK_LEN;
1751 os_memcpy(key, data->emsk, EAP_EMSK_LEN);
1757 int eap_peer_fast_register(void)
1759 struct eap_method *eap;
1762 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
1763 EAP_VENDOR_IETF, EAP_TYPE_FAST, "FAST");
1767 eap->init = eap_fast_init;
1768 eap->deinit = eap_fast_deinit;
1769 eap->process = eap_fast_process;
1770 eap->isKeyAvailable = eap_fast_isKeyAvailable;
1771 eap->getKey = eap_fast_getKey;
1772 eap->getSessionId = eap_fast_get_session_id;
1773 eap->get_status = eap_fast_get_status;
1775 eap->has_reauth_data = eap_fast_has_reauth_data;
1776 eap->deinit_for_reauth = eap_fast_deinit_for_reauth;
1777 eap->init_for_reauth = eap_fast_init_for_reauth;
1779 eap->get_emsk = eap_fast_get_emsk;
1781 ret = eap_peer_method_register(eap);
1783 eap_peer_method_free(eap);