Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / wpa / src / eap_peer / eap_fast.c
blob5bc1de0f07321b936b6d76f5ecf2b58e6e3642dd
1 /*
2 * EAP peer method: EAP-FAST (RFC 4851)
3 * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
12 * See README and COPYING for more details.
15 #include "includes.h"
17 #include "common.h"
18 #include "eap_i.h"
19 #include "eap_tls_common.h"
20 #include "eap_config.h"
21 #include "tls.h"
22 #include "eap_common/eap_tlv_common.h"
23 #include "sha1.h"
24 #include "eap_fast_pac.h"
26 #ifdef EAP_FAST_DYNAMIC
27 #include "eap_fast_pac.c"
28 #endif /* EAP_FAST_DYNAMIC */
30 /* TODO:
31 * - test session resumption and enable it if it interoperates
32 * - password change (pending mschapv2 packet; replay decrypted packet)
36 static void eap_fast_deinit(struct eap_sm *sm, void *priv);
39 struct eap_fast_data {
40 struct eap_ssl_data ssl;
42 int fast_version;
44 const struct eap_method *phase2_method;
45 void *phase2_priv;
46 int phase2_success;
48 struct eap_method_type phase2_type;
49 struct eap_method_type *phase2_types;
50 size_t num_phase2_types;
51 int resuming; /* starting a resumed session */
52 struct eap_fast_key_block_provisioning *key_block_p;
53 #define EAP_FAST_PROV_UNAUTH 1
54 #define EAP_FAST_PROV_AUTH 2
55 int provisioning_allowed; /* Allowed PAC provisioning modes */
56 int provisioning; /* doing PAC provisioning (not the normal auth) */
57 int anon_provisioning; /* doing anonymous (unauthenticated)
58 * provisioning */
59 int session_ticket_used;
61 u8 key_data[EAP_FAST_KEY_LEN];
62 u8 emsk[EAP_EMSK_LEN];
63 int success;
65 struct eap_fast_pac *pac;
66 struct eap_fast_pac *current_pac;
67 size_t max_pac_list_len;
68 int use_pac_binary_format;
70 u8 simck[EAP_FAST_SIMCK_LEN];
71 int simck_idx;
73 struct wpabuf *pending_phase2_req;
77 static int eap_fast_session_ticket_cb(void *ctx, const u8 *ticket, size_t len,
78 const u8 *client_random,
79 const u8 *server_random,
80 u8 *master_secret)
82 struct eap_fast_data *data = ctx;
84 wpa_printf(MSG_DEBUG, "EAP-FAST: SessionTicket callback");
86 if (client_random == NULL || server_random == NULL ||
87 master_secret == NULL) {
88 wpa_printf(MSG_DEBUG, "EAP-FAST: SessionTicket failed - fall "
89 "back to full TLS handshake");
90 data->session_ticket_used = 0;
91 if (data->provisioning_allowed) {
92 wpa_printf(MSG_DEBUG, "EAP-FAST: Try to provision a "
93 "new PAC-Key");
94 data->provisioning = 1;
95 data->current_pac = NULL;
97 return 0;
100 wpa_hexdump(MSG_DEBUG, "EAP-FAST: SessionTicket", ticket, len);
102 if (data->current_pac == NULL) {
103 wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC-Key available for "
104 "using SessionTicket");
105 data->session_ticket_used = 0;
106 return 0;
109 eap_fast_derive_master_secret(data->current_pac->pac_key,
110 server_random, client_random,
111 master_secret);
113 data->session_ticket_used = 1;
115 return 1;
119 static int eap_fast_parse_phase1(struct eap_fast_data *data,
120 const char *phase1)
122 const char *pos;
124 pos = os_strstr(phase1, "fast_provisioning=");
125 if (pos) {
126 data->provisioning_allowed = atoi(pos + 18);
127 wpa_printf(MSG_DEBUG, "EAP-FAST: Automatic PAC provisioning "
128 "mode: %d", data->provisioning_allowed);
131 pos = os_strstr(phase1, "fast_max_pac_list_len=");
132 if (pos) {
133 data->max_pac_list_len = atoi(pos + 22);
134 if (data->max_pac_list_len == 0)
135 data->max_pac_list_len = 1;
136 wpa_printf(MSG_DEBUG, "EAP-FAST: Maximum PAC list length: %lu",
137 (unsigned long) data->max_pac_list_len);
140 pos = os_strstr(phase1, "fast_pac_format=binary");
141 if (pos) {
142 data->use_pac_binary_format = 1;
143 wpa_printf(MSG_DEBUG, "EAP-FAST: Using binary format for PAC "
144 "list");
147 return 0;
151 static void * eap_fast_init(struct eap_sm *sm)
153 struct eap_fast_data *data;
154 struct eap_peer_config *config = eap_get_config(sm);
156 data = os_zalloc(sizeof(*data));
157 if (data == NULL)
158 return NULL;
159 data->fast_version = EAP_FAST_VERSION;
160 data->max_pac_list_len = 10;
162 if (config && config->phase1 &&
163 eap_fast_parse_phase1(data, config->phase1) < 0) {
164 eap_fast_deinit(sm, data);
165 return NULL;
168 if (eap_peer_select_phase2_methods(config, "auth=",
169 &data->phase2_types,
170 &data->num_phase2_types) < 0) {
171 eap_fast_deinit(sm, data);
172 return NULL;
175 data->phase2_type.vendor = EAP_VENDOR_IETF;
176 data->phase2_type.method = EAP_TYPE_NONE;
178 if (eap_peer_tls_ssl_init(sm, &data->ssl, config)) {
179 wpa_printf(MSG_INFO, "EAP-FAST: Failed to initialize SSL.");
180 eap_fast_deinit(sm, data);
181 return NULL;
184 if (tls_connection_set_session_ticket_cb(sm->ssl_ctx, data->ssl.conn,
185 eap_fast_session_ticket_cb,
186 data) < 0) {
187 wpa_printf(MSG_INFO, "EAP-FAST: Failed to set SessionTicket "
188 "callback");
189 eap_fast_deinit(sm, data);
190 return NULL;
194 * The local RADIUS server in a Cisco AP does not seem to like empty
195 * fragments before data, so disable that workaround for CBC.
196 * TODO: consider making this configurable
198 if (tls_connection_enable_workaround(sm->ssl_ctx, data->ssl.conn)) {
199 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to enable TLS "
200 "workarounds");
203 if (data->use_pac_binary_format &&
204 eap_fast_load_pac_bin(sm, &data->pac, config->pac_file) < 0) {
205 eap_fast_deinit(sm, data);
206 return NULL;
209 if (!data->use_pac_binary_format &&
210 eap_fast_load_pac(sm, &data->pac, config->pac_file) < 0) {
211 eap_fast_deinit(sm, data);
212 return NULL;
214 eap_fast_pac_list_truncate(data->pac, data->max_pac_list_len);
216 if (data->pac == NULL && !data->provisioning_allowed) {
217 wpa_printf(MSG_INFO, "EAP-FAST: No PAC configured and "
218 "provisioning disabled");
219 eap_fast_deinit(sm, data);
220 return NULL;
223 return data;
227 static void eap_fast_deinit(struct eap_sm *sm, void *priv)
229 struct eap_fast_data *data = priv;
230 struct eap_fast_pac *pac, *prev;
232 if (data == NULL)
233 return;
234 if (data->phase2_priv && data->phase2_method)
235 data->phase2_method->deinit(sm, data->phase2_priv);
236 os_free(data->phase2_types);
237 os_free(data->key_block_p);
238 eap_peer_tls_ssl_deinit(sm, &data->ssl);
240 pac = data->pac;
241 prev = NULL;
242 while (pac) {
243 prev = pac;
244 pac = pac->next;
245 eap_fast_free_pac(prev);
247 wpabuf_free(data->pending_phase2_req);
248 os_free(data);
252 static int eap_fast_derive_msk(struct eap_fast_data *data)
254 eap_fast_derive_eap_msk(data->simck, data->key_data);
255 eap_fast_derive_eap_emsk(data->simck, data->emsk);
256 data->success = 1;
257 return 0;
261 static void eap_fast_derive_key_auth(struct eap_sm *sm,
262 struct eap_fast_data *data)
264 u8 *sks;
266 /* RFC 4851, Section 5.1:
267 * Extra key material after TLS key_block: session_key_seed[40]
270 sks = eap_fast_derive_key(sm->ssl_ctx, data->ssl.conn, "key expansion",
271 EAP_FAST_SKS_LEN);
272 if (sks == NULL) {
273 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive "
274 "session_key_seed");
275 return;
279 * RFC 4851, Section 5.2:
280 * S-IMCK[0] = session_key_seed
282 wpa_hexdump_key(MSG_DEBUG,
283 "EAP-FAST: session_key_seed (SKS = S-IMCK[0])",
284 sks, EAP_FAST_SKS_LEN);
285 data->simck_idx = 0;
286 os_memcpy(data->simck, sks, EAP_FAST_SIMCK_LEN);
287 os_free(sks);
291 static void eap_fast_derive_key_provisioning(struct eap_sm *sm,
292 struct eap_fast_data *data)
294 os_free(data->key_block_p);
295 data->key_block_p = (struct eap_fast_key_block_provisioning *)
296 eap_fast_derive_key(sm->ssl_ctx, data->ssl.conn,
297 "key expansion",
298 sizeof(*data->key_block_p));
299 if (data->key_block_p == NULL) {
300 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive key block");
301 return;
304 * RFC 4851, Section 5.2:
305 * S-IMCK[0] = session_key_seed
307 wpa_hexdump_key(MSG_DEBUG,
308 "EAP-FAST: session_key_seed (SKS = S-IMCK[0])",
309 data->key_block_p->session_key_seed,
310 sizeof(data->key_block_p->session_key_seed));
311 data->simck_idx = 0;
312 os_memcpy(data->simck, data->key_block_p->session_key_seed,
313 EAP_FAST_SIMCK_LEN);
314 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: server_challenge",
315 data->key_block_p->server_challenge,
316 sizeof(data->key_block_p->server_challenge));
317 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: client_challenge",
318 data->key_block_p->client_challenge,
319 sizeof(data->key_block_p->client_challenge));
323 static void eap_fast_derive_keys(struct eap_sm *sm, struct eap_fast_data *data)
325 if (data->anon_provisioning)
326 eap_fast_derive_key_provisioning(sm, data);
327 else
328 eap_fast_derive_key_auth(sm, data);
332 static int eap_fast_init_phase2_method(struct eap_sm *sm,
333 struct eap_fast_data *data)
335 data->phase2_method =
336 eap_peer_get_eap_method(data->phase2_type.vendor,
337 data->phase2_type.method);
338 if (data->phase2_method == NULL)
339 return -1;
341 if (data->key_block_p) {
342 sm->auth_challenge = data->key_block_p->server_challenge;
343 sm->peer_challenge = data->key_block_p->client_challenge;
345 sm->init_phase2 = 1;
346 sm->mschapv2_full_key = 1;
347 data->phase2_priv = data->phase2_method->init(sm);
348 sm->init_phase2 = 0;
349 sm->mschapv2_full_key = 0;
350 sm->auth_challenge = NULL;
351 sm->peer_challenge = NULL;
353 return data->phase2_priv == NULL ? -1 : 0;
357 static int eap_fast_select_phase2_method(struct eap_fast_data *data, u8 type)
359 size_t i;
361 /* TODO: TNC with anonymous provisioning; need to require both
362 * completed MSCHAPv2 and TNC */
364 if (data->anon_provisioning && type != EAP_TYPE_MSCHAPV2) {
365 wpa_printf(MSG_INFO, "EAP-FAST: Only EAP-MSCHAPv2 is allowed "
366 "during unauthenticated provisioning; reject phase2"
367 " type %d", type);
368 return -1;
371 #ifdef EAP_TNC
372 if (type == EAP_TYPE_TNC) {
373 data->phase2_type.vendor = EAP_VENDOR_IETF;
374 data->phase2_type.method = EAP_TYPE_TNC;
375 wpa_printf(MSG_DEBUG, "EAP-FAST: Selected Phase 2 EAP "
376 "vendor %d method %d for TNC",
377 data->phase2_type.vendor,
378 data->phase2_type.method);
379 return 0;
381 #endif /* EAP_TNC */
383 for (i = 0; i < data->num_phase2_types; i++) {
384 if (data->phase2_types[i].vendor != EAP_VENDOR_IETF ||
385 data->phase2_types[i].method != type)
386 continue;
388 data->phase2_type.vendor = data->phase2_types[i].vendor;
389 data->phase2_type.method = data->phase2_types[i].method;
390 wpa_printf(MSG_DEBUG, "EAP-FAST: Selected Phase 2 EAP "
391 "vendor %d method %d",
392 data->phase2_type.vendor,
393 data->phase2_type.method);
394 break;
397 if (type != data->phase2_type.method || type == EAP_TYPE_NONE)
398 return -1;
400 return 0;
404 static int eap_fast_phase2_request(struct eap_sm *sm,
405 struct eap_fast_data *data,
406 struct eap_method_ret *ret,
407 struct eap_hdr *hdr,
408 struct wpabuf **resp)
410 size_t len = be_to_host16(hdr->length);
411 u8 *pos;
412 struct eap_method_ret iret;
413 struct eap_peer_config *config = eap_get_config(sm);
414 struct wpabuf msg;
416 if (len <= sizeof(struct eap_hdr)) {
417 wpa_printf(MSG_INFO, "EAP-FAST: too short "
418 "Phase 2 request (len=%lu)", (unsigned long) len);
419 return -1;
421 pos = (u8 *) (hdr + 1);
422 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 Request: type=%d", *pos);
423 if (*pos == EAP_TYPE_IDENTITY) {
424 *resp = eap_sm_buildIdentity(sm, hdr->identifier, 1);
425 return 0;
428 if (data->phase2_priv && data->phase2_method &&
429 *pos != data->phase2_type.method) {
430 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 EAP sequence - "
431 "deinitialize previous method");
432 data->phase2_method->deinit(sm, data->phase2_priv);
433 data->phase2_method = NULL;
434 data->phase2_priv = NULL;
435 data->phase2_type.vendor = EAP_VENDOR_IETF;
436 data->phase2_type.method = EAP_TYPE_NONE;
439 if (data->phase2_type.vendor == EAP_VENDOR_IETF &&
440 data->phase2_type.method == EAP_TYPE_NONE &&
441 eap_fast_select_phase2_method(data, *pos) < 0) {
442 if (eap_peer_tls_phase2_nak(data->phase2_types,
443 data->num_phase2_types,
444 hdr, resp))
445 return -1;
446 return 0;
449 if (data->phase2_priv == NULL &&
450 eap_fast_init_phase2_method(sm, data) < 0) {
451 wpa_printf(MSG_INFO, "EAP-FAST: Failed to initialize "
452 "Phase 2 EAP method %d", *pos);
453 ret->methodState = METHOD_DONE;
454 ret->decision = DECISION_FAIL;
455 return -1;
458 os_memset(&iret, 0, sizeof(iret));
459 wpabuf_set(&msg, hdr, len);
460 *resp = data->phase2_method->process(sm, data->phase2_priv, &iret,
461 &msg);
462 if (*resp == NULL ||
463 (iret.methodState == METHOD_DONE &&
464 iret.decision == DECISION_FAIL)) {
465 ret->methodState = METHOD_DONE;
466 ret->decision = DECISION_FAIL;
467 } else if ((iret.methodState == METHOD_DONE ||
468 iret.methodState == METHOD_MAY_CONT) &&
469 (iret.decision == DECISION_UNCOND_SUCC ||
470 iret.decision == DECISION_COND_SUCC)) {
471 data->phase2_success = 1;
474 if (*resp == NULL && config &&
475 (config->pending_req_identity || config->pending_req_password ||
476 config->pending_req_otp || config->pending_req_new_password)) {
477 wpabuf_free(data->pending_phase2_req);
478 data->pending_phase2_req = wpabuf_alloc_copy(hdr, len);
479 } else if (*resp == NULL)
480 return -1;
482 return 0;
486 static struct wpabuf * eap_fast_tlv_nak(int vendor_id, int tlv_type)
488 struct wpabuf *buf;
489 struct eap_tlv_nak_tlv *nak;
490 buf = wpabuf_alloc(sizeof(*nak));
491 if (buf == NULL)
492 return NULL;
493 nak = wpabuf_put(buf, sizeof(*nak));
494 nak->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY | EAP_TLV_NAK_TLV);
495 nak->length = host_to_be16(6);
496 nak->vendor_id = host_to_be32(vendor_id);
497 nak->nak_type = host_to_be16(tlv_type);
498 return buf;
502 static struct wpabuf * eap_fast_tlv_result(int status, int intermediate)
504 struct wpabuf *buf;
505 struct eap_tlv_intermediate_result_tlv *result;
506 buf = wpabuf_alloc(sizeof(*result));
507 if (buf == NULL)
508 return NULL;
509 result = wpabuf_put(buf, sizeof(*result));
510 result->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
511 (intermediate ?
512 EAP_TLV_INTERMEDIATE_RESULT_TLV :
513 EAP_TLV_RESULT_TLV));
514 result->length = host_to_be16(2);
515 result->status = host_to_be16(status);
516 return buf;
520 static struct wpabuf * eap_fast_tlv_pac_ack(void)
522 struct wpabuf *buf;
523 struct eap_tlv_result_tlv *res;
524 struct eap_tlv_pac_ack_tlv *ack;
526 buf = wpabuf_alloc(sizeof(*res) + sizeof(*ack));
527 if (buf == NULL)
528 return NULL;
530 res = wpabuf_put(buf, sizeof(*res));
531 res->tlv_type = host_to_be16(EAP_TLV_RESULT_TLV |
532 EAP_TLV_TYPE_MANDATORY);
533 res->length = host_to_be16(sizeof(*res) - sizeof(struct eap_tlv_hdr));
534 res->status = host_to_be16(EAP_TLV_RESULT_SUCCESS);
536 ack = wpabuf_put(buf, sizeof(*ack));
537 ack->tlv_type = host_to_be16(EAP_TLV_PAC_TLV |
538 EAP_TLV_TYPE_MANDATORY);
539 ack->length = host_to_be16(sizeof(*ack) - sizeof(struct eap_tlv_hdr));
540 ack->pac_type = host_to_be16(PAC_TYPE_PAC_ACKNOWLEDGEMENT);
541 ack->pac_len = host_to_be16(2);
542 ack->result = host_to_be16(EAP_TLV_RESULT_SUCCESS);
544 return buf;
548 static struct wpabuf * eap_fast_process_eap_payload_tlv(
549 struct eap_sm *sm, struct eap_fast_data *data,
550 struct eap_method_ret *ret, const struct eap_hdr *req,
551 u8 *eap_payload_tlv, size_t eap_payload_tlv_len)
553 struct eap_hdr *hdr;
554 struct wpabuf *resp = NULL;
556 if (eap_payload_tlv_len < sizeof(*hdr)) {
557 wpa_printf(MSG_DEBUG, "EAP-FAST: too short EAP "
558 "Payload TLV (len=%lu)",
559 (unsigned long) eap_payload_tlv_len);
560 return NULL;
563 hdr = (struct eap_hdr *) eap_payload_tlv;
564 if (be_to_host16(hdr->length) > eap_payload_tlv_len) {
565 wpa_printf(MSG_DEBUG, "EAP-FAST: EAP packet overflow in "
566 "EAP Payload TLV");
567 return NULL;
570 if (hdr->code != EAP_CODE_REQUEST) {
571 wpa_printf(MSG_INFO, "EAP-FAST: Unexpected code=%d in "
572 "Phase 2 EAP header", hdr->code);
573 return NULL;
576 if (eap_fast_phase2_request(sm, data, ret, hdr, &resp)) {
577 wpa_printf(MSG_INFO, "EAP-FAST: Phase2 Request processing "
578 "failed");
579 return NULL;
582 return eap_fast_tlv_eap_payload(resp);
586 static int eap_fast_validate_crypto_binding(
587 struct eap_tlv_crypto_binding_tlv *_bind)
589 wpa_printf(MSG_DEBUG, "EAP-FAST: Crypto-Binding TLV: Version %d "
590 "Received Version %d SubType %d",
591 _bind->version, _bind->received_version, _bind->subtype);
592 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE",
593 _bind->nonce, sizeof(_bind->nonce));
594 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC",
595 _bind->compound_mac, sizeof(_bind->compound_mac));
597 if (_bind->version != EAP_FAST_VERSION ||
598 _bind->received_version != EAP_FAST_VERSION ||
599 _bind->subtype != EAP_TLV_CRYPTO_BINDING_SUBTYPE_REQUEST) {
600 wpa_printf(MSG_INFO, "EAP-FAST: Invalid version/subtype in "
601 "Crypto-Binding TLV: Version %d "
602 "Received Version %d SubType %d",
603 _bind->version, _bind->received_version,
604 _bind->subtype);
605 return -1;
608 return 0;
612 static void eap_fast_write_crypto_binding(
613 struct eap_tlv_crypto_binding_tlv *rbind,
614 struct eap_tlv_crypto_binding_tlv *_bind, const u8 *cmk)
616 rbind->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
617 EAP_TLV_CRYPTO_BINDING_TLV);
618 rbind->length = host_to_be16(sizeof(*rbind) -
619 sizeof(struct eap_tlv_hdr));
620 rbind->version = EAP_FAST_VERSION;
621 rbind->received_version = _bind->version;
622 rbind->subtype = EAP_TLV_CRYPTO_BINDING_SUBTYPE_RESPONSE;
623 os_memcpy(rbind->nonce, _bind->nonce, sizeof(_bind->nonce));
624 inc_byte_array(rbind->nonce, sizeof(rbind->nonce));
625 hmac_sha1(cmk, EAP_FAST_CMK_LEN, (u8 *) rbind, sizeof(*rbind),
626 rbind->compound_mac);
628 wpa_printf(MSG_DEBUG, "EAP-FAST: Reply Crypto-Binding TLV: Version %d "
629 "Received Version %d SubType %d",
630 rbind->version, rbind->received_version, rbind->subtype);
631 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE",
632 rbind->nonce, sizeof(rbind->nonce));
633 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC",
634 rbind->compound_mac, sizeof(rbind->compound_mac));
638 static int eap_fast_get_phase2_key(struct eap_sm *sm,
639 struct eap_fast_data *data,
640 u8 *isk, size_t isk_len)
642 u8 *key;
643 size_t key_len;
645 os_memset(isk, 0, isk_len);
647 if (data->phase2_method == NULL || data->phase2_priv == NULL) {
648 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 method not "
649 "available");
650 return -1;
653 if (data->phase2_method->isKeyAvailable == NULL ||
654 data->phase2_method->getKey == NULL)
655 return 0;
657 if (!data->phase2_method->isKeyAvailable(sm, data->phase2_priv) ||
658 (key = data->phase2_method->getKey(sm, data->phase2_priv,
659 &key_len)) == NULL) {
660 wpa_printf(MSG_DEBUG, "EAP-FAST: Could not get key material "
661 "from Phase 2");
662 return -1;
665 if (key_len > isk_len)
666 key_len = isk_len;
667 os_memcpy(isk, key, key_len);
668 os_free(key);
670 return 0;
674 static int eap_fast_get_cmk(struct eap_sm *sm, struct eap_fast_data *data,
675 u8 *cmk)
677 u8 isk[32], imck[60];
679 wpa_printf(MSG_DEBUG, "EAP-FAST: Determining CMK[%d] for Compound MIC "
680 "calculation", data->simck_idx + 1);
683 * RFC 4851, Section 5.2:
684 * IMCK[j] = T-PRF(S-IMCK[j-1], "Inner Methods Compound Keys",
685 * MSK[j], 60)
686 * S-IMCK[j] = first 40 octets of IMCK[j]
687 * CMK[j] = last 20 octets of IMCK[j]
690 if (eap_fast_get_phase2_key(sm, data, isk, sizeof(isk)) < 0)
691 return -1;
692 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: ISK[j]", isk, sizeof(isk));
693 sha1_t_prf(data->simck, EAP_FAST_SIMCK_LEN,
694 "Inner Methods Compound Keys",
695 isk, sizeof(isk), imck, sizeof(imck));
696 data->simck_idx++;
697 os_memcpy(data->simck, imck, EAP_FAST_SIMCK_LEN);
698 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: S-IMCK[j]",
699 data->simck, EAP_FAST_SIMCK_LEN);
700 os_memcpy(cmk, imck + EAP_FAST_SIMCK_LEN, EAP_FAST_CMK_LEN);
701 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: CMK[j]",
702 cmk, EAP_FAST_CMK_LEN);
704 return 0;
708 static u8 * eap_fast_write_pac_request(u8 *pos, u16 pac_type)
710 struct eap_tlv_hdr *pac;
711 struct eap_tlv_request_action_tlv *act;
712 struct eap_tlv_pac_type_tlv *type;
714 act = (struct eap_tlv_request_action_tlv *) pos;
715 act->tlv_type = host_to_be16(EAP_TLV_REQUEST_ACTION_TLV);
716 act->length = host_to_be16(2);
717 act->action = host_to_be16(EAP_TLV_ACTION_PROCESS_TLV);
719 pac = (struct eap_tlv_hdr *) (act + 1);
720 pac->tlv_type = host_to_be16(EAP_TLV_PAC_TLV);
721 pac->length = host_to_be16(sizeof(*type));
723 type = (struct eap_tlv_pac_type_tlv *) (pac + 1);
724 type->tlv_type = host_to_be16(PAC_TYPE_PAC_TYPE);
725 type->length = host_to_be16(2);
726 type->pac_type = host_to_be16(pac_type);
728 return (u8 *) (type + 1);
732 static struct wpabuf * eap_fast_process_crypto_binding(
733 struct eap_sm *sm, struct eap_fast_data *data,
734 struct eap_method_ret *ret,
735 struct eap_tlv_crypto_binding_tlv *_bind, size_t bind_len)
737 struct wpabuf *resp;
738 u8 *pos;
739 u8 cmk[EAP_FAST_CMK_LEN], cmac[SHA1_MAC_LEN];
740 int res;
741 size_t len;
743 if (eap_fast_validate_crypto_binding(_bind) < 0)
744 return NULL;
746 if (eap_fast_get_cmk(sm, data, cmk) < 0)
747 return NULL;
749 /* Validate received Compound MAC */
750 os_memcpy(cmac, _bind->compound_mac, sizeof(cmac));
751 os_memset(_bind->compound_mac, 0, sizeof(cmac));
752 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Crypto-Binding TLV for Compound "
753 "MAC calculation", (u8 *) _bind, bind_len);
754 hmac_sha1(cmk, EAP_FAST_CMK_LEN, (u8 *) _bind, bind_len,
755 _bind->compound_mac);
756 res = os_memcmp(cmac, _bind->compound_mac, sizeof(cmac));
757 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Received Compound MAC",
758 cmac, sizeof(cmac));
759 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Calculated Compound MAC",
760 _bind->compound_mac, sizeof(cmac));
761 if (res != 0) {
762 wpa_printf(MSG_INFO, "EAP-FAST: Compound MAC did not match");
763 os_memcpy(_bind->compound_mac, cmac, sizeof(cmac));
764 return NULL;
768 * Compound MAC was valid, so authentication succeeded. Reply with
769 * crypto binding to allow server to complete authentication.
772 len = sizeof(struct eap_tlv_crypto_binding_tlv);
773 resp = wpabuf_alloc(len);
774 if (resp == NULL)
775 return NULL;
777 if (!data->anon_provisioning && data->phase2_success &&
778 eap_fast_derive_msk(data) < 0) {
779 wpa_printf(MSG_INFO, "EAP-FAST: Failed to generate MSK");
780 ret->methodState = METHOD_DONE;
781 ret->decision = DECISION_FAIL;
782 data->phase2_success = 0;
783 wpabuf_free(resp);
784 return NULL;
787 pos = wpabuf_put(resp, sizeof(struct eap_tlv_crypto_binding_tlv));
788 eap_fast_write_crypto_binding((struct eap_tlv_crypto_binding_tlv *)
789 pos, _bind, cmk);
791 return resp;
795 static void eap_fast_parse_pac_tlv(struct eap_fast_pac *entry, int type,
796 u8 *pos, size_t len, int *pac_key_found)
798 switch (type & 0x7fff) {
799 case PAC_TYPE_PAC_KEY:
800 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: PAC-Key", pos, len);
801 if (len != EAP_FAST_PAC_KEY_LEN) {
802 wpa_printf(MSG_DEBUG, "EAP-FAST: Invalid PAC-Key "
803 "length %lu", (unsigned long) len);
804 break;
806 *pac_key_found = 1;
807 os_memcpy(entry->pac_key, pos, len);
808 break;
809 case PAC_TYPE_PAC_OPAQUE:
810 wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Opaque", pos, len);
811 entry->pac_opaque = pos;
812 entry->pac_opaque_len = len;
813 break;
814 case PAC_TYPE_PAC_INFO:
815 wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Info", pos, len);
816 entry->pac_info = pos;
817 entry->pac_info_len = len;
818 break;
819 default:
820 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignored unknown PAC type %d",
821 type);
822 break;
827 static int eap_fast_process_pac_tlv(struct eap_fast_pac *entry,
828 u8 *pac, size_t pac_len)
830 struct pac_tlv_hdr *hdr;
831 u8 *pos;
832 size_t left, len;
833 int type, pac_key_found = 0;
835 pos = pac;
836 left = pac_len;
838 while (left > sizeof(*hdr)) {
839 hdr = (struct pac_tlv_hdr *) pos;
840 type = be_to_host16(hdr->type);
841 len = be_to_host16(hdr->len);
842 pos += sizeof(*hdr);
843 left -= sizeof(*hdr);
844 if (len > left) {
845 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV overrun "
846 "(type=%d len=%lu left=%lu)",
847 type, (unsigned long) len,
848 (unsigned long) left);
849 return -1;
852 eap_fast_parse_pac_tlv(entry, type, pos, len, &pac_key_found);
854 pos += len;
855 left -= len;
858 if (!pac_key_found || !entry->pac_opaque || !entry->pac_info) {
859 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV does not include "
860 "all the required fields");
861 return -1;
864 return 0;
868 static int eap_fast_parse_pac_info(struct eap_fast_pac *entry, int type,
869 u8 *pos, size_t len)
871 u16 pac_type;
872 u32 lifetime;
873 struct os_time now;
875 switch (type & 0x7fff) {
876 case PAC_TYPE_CRED_LIFETIME:
877 if (len != 4) {
878 wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Info - "
879 "Invalid CRED_LIFETIME length - ignored",
880 pos, len);
881 return 0;
885 * This is not currently saved separately in PAC files since
886 * the server can automatically initiate PAC update when
887 * needed. Anyway, the information is available from PAC-Info
888 * dump if it is needed for something in the future.
890 lifetime = WPA_GET_BE32(pos);
891 os_get_time(&now);
892 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info - CRED_LIFETIME %d "
893 "(%d days)",
894 lifetime, (lifetime - (u32) now.sec) / 86400);
895 break;
896 case PAC_TYPE_A_ID:
897 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - A-ID",
898 pos, len);
899 entry->a_id = pos;
900 entry->a_id_len = len;
901 break;
902 case PAC_TYPE_I_ID:
903 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - I-ID",
904 pos, len);
905 entry->i_id = pos;
906 entry->i_id_len = len;
907 break;
908 case PAC_TYPE_A_ID_INFO:
909 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - A-ID-Info",
910 pos, len);
911 entry->a_id_info = pos;
912 entry->a_id_info_len = len;
913 break;
914 case PAC_TYPE_PAC_TYPE:
916 * draft-cam-winget-eap-fast-provisioning-04.txt,
917 * Section 4.2.6 - PAC-Type TLV
919 if (len != 2) {
920 wpa_printf(MSG_INFO, "EAP-FAST: Invalid PAC-Type "
921 "length %lu (expected 2)",
922 (unsigned long) len);
923 wpa_hexdump_ascii(MSG_DEBUG,
924 "EAP-FAST: PAC-Info - PAC-Type",
925 pos, len);
926 return -1;
928 pac_type = WPA_GET_BE16(pos);
929 if (pac_type != PAC_TYPE_TUNNEL_PAC &&
930 pac_type != PAC_TYPE_USER_AUTHORIZATION &&
931 pac_type != PAC_TYPE_MACHINE_AUTHENTICATION) {
932 wpa_printf(MSG_INFO, "EAP-FAST: Unsupported PAC Type "
933 "%d", pac_type);
934 return -1;
937 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info - PAC-Type %d",
938 pac_type);
939 entry->pac_type = pac_type;
940 break;
941 default:
942 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignored unknown PAC-Info "
943 "type %d", type);
944 break;
947 return 0;
951 static int eap_fast_process_pac_info(struct eap_fast_pac *entry)
953 struct pac_tlv_hdr *hdr;
954 u8 *pos;
955 size_t left, len;
956 int type;
958 /* draft-cam-winget-eap-fast-provisioning-04.txt, Section 4.2.4 */
960 /* PAC-Type defaults to Tunnel PAC (Type 1) */
961 entry->pac_type = PAC_TYPE_TUNNEL_PAC;
963 pos = entry->pac_info;
964 left = entry->pac_info_len;
965 while (left > sizeof(*hdr)) {
966 hdr = (struct pac_tlv_hdr *) pos;
967 type = be_to_host16(hdr->type);
968 len = be_to_host16(hdr->len);
969 pos += sizeof(*hdr);
970 left -= sizeof(*hdr);
971 if (len > left) {
972 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info overrun "
973 "(type=%d len=%lu left=%lu)",
974 type, (unsigned long) len,
975 (unsigned long) left);
976 return -1;
979 if (eap_fast_parse_pac_info(entry, type, pos, len) < 0)
980 return -1;
982 pos += len;
983 left -= len;
986 if (entry->a_id == NULL || entry->a_id_info == NULL) {
987 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info does not include "
988 "all the required fields");
989 return -1;
992 return 0;
996 static struct wpabuf * eap_fast_process_pac(struct eap_sm *sm,
997 struct eap_fast_data *data,
998 struct eap_method_ret *ret,
999 u8 *pac, size_t pac_len)
1001 struct eap_peer_config *config = eap_get_config(sm);
1002 struct eap_fast_pac entry;
1004 os_memset(&entry, 0, sizeof(entry));
1005 if (eap_fast_process_pac_tlv(&entry, pac, pac_len) ||
1006 eap_fast_process_pac_info(&entry))
1007 return NULL;
1009 eap_fast_add_pac(&data->pac, &data->current_pac, &entry);
1010 eap_fast_pac_list_truncate(data->pac, data->max_pac_list_len);
1011 if (data->use_pac_binary_format)
1012 eap_fast_save_pac_bin(sm, data->pac, config->pac_file);
1013 else
1014 eap_fast_save_pac(sm, data->pac, config->pac_file);
1016 if (data->provisioning) {
1017 if (data->anon_provisioning) {
1019 * Unauthenticated provisioning does not provide keying
1020 * material and must end with an EAP-Failure.
1021 * Authentication will be done separately after this.
1023 data->success = 0;
1024 ret->decision = DECISION_FAIL;
1025 } else {
1027 * Server may or may not allow authenticated
1028 * provisioning also for key generation.
1030 ret->decision = DECISION_COND_SUCC;
1032 wpa_printf(MSG_DEBUG, "EAP-FAST: Send PAC-Acknowledgement TLV "
1033 "- Provisioning completed successfully");
1034 } else {
1036 * This is PAC refreshing, i.e., normal authentication that is
1037 * expected to be completed with an EAP-Success.
1039 wpa_printf(MSG_DEBUG, "EAP-FAST: Send PAC-Acknowledgement TLV "
1040 "- PAC refreshing completed successfully");
1041 ret->decision = DECISION_UNCOND_SUCC;
1043 ret->methodState = METHOD_DONE;
1044 return eap_fast_tlv_pac_ack();
1048 static int eap_fast_parse_decrypted(struct wpabuf *decrypted,
1049 struct eap_fast_tlv_parse *tlv,
1050 struct wpabuf **resp)
1052 int mandatory, tlv_type, len, res;
1053 u8 *pos, *end;
1055 os_memset(tlv, 0, sizeof(*tlv));
1057 /* Parse TLVs from the decrypted Phase 2 data */
1058 pos = wpabuf_mhead(decrypted);
1059 end = pos + wpabuf_len(decrypted);
1060 while (pos + 4 < end) {
1061 mandatory = pos[0] & 0x80;
1062 tlv_type = WPA_GET_BE16(pos) & 0x3fff;
1063 pos += 2;
1064 len = WPA_GET_BE16(pos);
1065 pos += 2;
1066 if (pos + len > end) {
1067 wpa_printf(MSG_INFO, "EAP-FAST: TLV overflow");
1068 return -1;
1070 wpa_printf(MSG_DEBUG, "EAP-FAST: Received Phase 2: "
1071 "TLV type %d length %d%s",
1072 tlv_type, len, mandatory ? " (mandatory)" : "");
1074 res = eap_fast_parse_tlv(tlv, tlv_type, pos, len);
1075 if (res == -2)
1076 break;
1077 if (res < 0) {
1078 if (mandatory) {
1079 wpa_printf(MSG_DEBUG, "EAP-FAST: Nak unknown "
1080 "mandatory TLV type %d", tlv_type);
1081 *resp = eap_fast_tlv_nak(0, tlv_type);
1082 break;
1083 } else {
1084 wpa_printf(MSG_DEBUG, "EAP-FAST: ignored "
1085 "unknown optional TLV type %d",
1086 tlv_type);
1090 pos += len;
1093 return 0;
1097 static int eap_fast_encrypt_response(struct eap_sm *sm,
1098 struct eap_fast_data *data,
1099 struct wpabuf *resp,
1100 u8 identifier, struct wpabuf **out_data)
1102 if (resp == NULL)
1103 return 0;
1105 wpa_hexdump_buf(MSG_DEBUG, "EAP-FAST: Encrypting Phase 2 data",
1106 resp);
1107 if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_FAST,
1108 data->fast_version, identifier,
1109 resp, out_data)) {
1110 wpa_printf(MSG_INFO, "EAP-FAST: Failed to encrypt a Phase 2 "
1111 "frame");
1113 wpabuf_free(resp);
1115 return 0;
1119 static struct wpabuf * eap_fast_pac_request(void)
1121 struct wpabuf *tmp;
1122 u8 *pos, *pos2;
1124 tmp = wpabuf_alloc(sizeof(struct eap_tlv_hdr) +
1125 sizeof(struct eap_tlv_request_action_tlv) +
1126 sizeof(struct eap_tlv_pac_type_tlv));
1127 if (tmp == NULL)
1128 return NULL;
1130 pos = wpabuf_put(tmp, 0);
1131 pos2 = eap_fast_write_pac_request(pos, PAC_TYPE_TUNNEL_PAC);
1132 wpabuf_put(tmp, pos2 - pos);
1133 return tmp;
1137 static int eap_fast_process_decrypted(struct eap_sm *sm,
1138 struct eap_fast_data *data,
1139 struct eap_method_ret *ret,
1140 const struct eap_hdr *req,
1141 struct wpabuf *decrypted,
1142 struct wpabuf **out_data)
1144 struct wpabuf *resp = NULL, *tmp;
1145 struct eap_fast_tlv_parse tlv;
1146 int failed = 0;
1148 if (eap_fast_parse_decrypted(decrypted, &tlv, &resp) < 0)
1149 return 0;
1150 if (resp)
1151 return eap_fast_encrypt_response(sm, data, resp,
1152 req->identifier, out_data);
1154 if (tlv.result == EAP_TLV_RESULT_FAILURE) {
1155 resp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 0);
1156 return eap_fast_encrypt_response(sm, data, resp,
1157 req->identifier, out_data);
1160 if (tlv.iresult == EAP_TLV_RESULT_FAILURE) {
1161 resp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 1);
1162 return eap_fast_encrypt_response(sm, data, resp,
1163 req->identifier, out_data);
1166 if (tlv.crypto_binding) {
1167 tmp = eap_fast_process_crypto_binding(sm, data, ret,
1168 tlv.crypto_binding,
1169 tlv.crypto_binding_len);
1170 if (tmp == NULL)
1171 failed = 1;
1172 else
1173 resp = wpabuf_concat(resp, tmp);
1176 if (tlv.iresult == EAP_TLV_RESULT_SUCCESS) {
1177 tmp = eap_fast_tlv_result(failed ? EAP_TLV_RESULT_FAILURE :
1178 EAP_TLV_RESULT_SUCCESS, 1);
1179 resp = wpabuf_concat(resp, tmp);
1182 if (tlv.eap_payload_tlv) {
1183 tmp = eap_fast_process_eap_payload_tlv(
1184 sm, data, ret, req, tlv.eap_payload_tlv,
1185 tlv.eap_payload_tlv_len);
1186 resp = wpabuf_concat(resp, tmp);
1189 if (tlv.pac && tlv.result != EAP_TLV_RESULT_SUCCESS) {
1190 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV without Result TLV "
1191 "acknowledging success");
1192 failed = 1;
1193 } else if (tlv.pac && tlv.result == EAP_TLV_RESULT_SUCCESS) {
1194 tmp = eap_fast_process_pac(sm, data, ret, tlv.pac,
1195 tlv.pac_len);
1196 resp = wpabuf_concat(resp, tmp);
1199 if (data->current_pac == NULL && data->provisioning &&
1200 !data->anon_provisioning) {
1202 * Need to request Tunnel PAC when using authenticated
1203 * provisioning.
1205 wpa_printf(MSG_DEBUG, "EAP-FAST: Request Tunnel PAC");
1206 tmp = eap_fast_pac_request();
1207 resp = wpabuf_concat(resp, tmp);
1210 if (tlv.result == EAP_TLV_RESULT_SUCCESS && !failed) {
1211 tmp = eap_fast_tlv_result(EAP_TLV_RESULT_SUCCESS, 0);
1212 resp = wpabuf_concat(resp, tmp);
1213 } else if (failed) {
1214 tmp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 0);
1215 resp = wpabuf_concat(resp, tmp);
1218 if (resp && tlv.result == EAP_TLV_RESULT_SUCCESS && !failed &&
1219 tlv.crypto_binding && data->phase2_success) {
1220 if (data->anon_provisioning) {
1221 wpa_printf(MSG_DEBUG, "EAP-FAST: Unauthenticated "
1222 "provisioning completed successfully.");
1223 ret->methodState = METHOD_DONE;
1224 ret->decision = DECISION_FAIL;
1225 } else {
1226 wpa_printf(MSG_DEBUG, "EAP-FAST: Authentication "
1227 "completed successfully.");
1228 if (data->provisioning)
1229 ret->methodState = METHOD_MAY_CONT;
1230 else
1231 ret->methodState = METHOD_DONE;
1232 ret->decision = DECISION_UNCOND_SUCC;
1236 if (resp == NULL) {
1237 wpa_printf(MSG_DEBUG, "EAP-FAST: No recognized TLVs - send "
1238 "empty response packet");
1239 resp = wpabuf_alloc(1);
1242 return eap_fast_encrypt_response(sm, data, resp, req->identifier,
1243 out_data);
1247 static int eap_fast_decrypt(struct eap_sm *sm, struct eap_fast_data *data,
1248 struct eap_method_ret *ret,
1249 const struct eap_hdr *req,
1250 const struct wpabuf *in_data,
1251 struct wpabuf **out_data)
1253 struct wpabuf *in_decrypted;
1254 int res;
1256 wpa_printf(MSG_DEBUG, "EAP-FAST: Received %lu bytes encrypted data for"
1257 " Phase 2", (unsigned long) wpabuf_len(in_data));
1259 if (data->pending_phase2_req) {
1260 wpa_printf(MSG_DEBUG, "EAP-FAST: Pending Phase 2 request - "
1261 "skip decryption and use old data");
1262 /* Clear TLS reassembly state. */
1263 eap_peer_tls_reset_input(&data->ssl);
1265 in_decrypted = data->pending_phase2_req;
1266 data->pending_phase2_req = NULL;
1267 goto continue_req;
1270 if (wpabuf_len(in_data) == 0) {
1271 /* Received TLS ACK - requesting more fragments */
1272 return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_FAST,
1273 data->fast_version,
1274 req->identifier, NULL, out_data);
1277 res = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
1278 if (res)
1279 return res;
1281 continue_req:
1282 wpa_hexdump_buf(MSG_MSGDUMP, "EAP-FAST: Decrypted Phase 2 TLV(s)",
1283 in_decrypted);
1285 if (wpabuf_len(in_decrypted) < 4) {
1286 wpa_printf(MSG_INFO, "EAP-FAST: Too short Phase 2 "
1287 "TLV frame (len=%lu)",
1288 (unsigned long) wpabuf_len(in_decrypted));
1289 wpabuf_free(in_decrypted);
1290 return -1;
1293 res = eap_fast_process_decrypted(sm, data, ret, req,
1294 in_decrypted, out_data);
1296 wpabuf_free(in_decrypted);
1298 return res;
1302 static const u8 * eap_fast_get_a_id(const u8 *buf, size_t len, size_t *id_len)
1304 const u8 *a_id;
1305 struct pac_tlv_hdr *hdr;
1308 * Parse authority identity (A-ID) from the EAP-FAST/Start. This
1309 * supports both raw A-ID and one inside an A-ID TLV.
1311 a_id = buf;
1312 *id_len = len;
1313 if (len > sizeof(*hdr)) {
1314 int tlen;
1315 hdr = (struct pac_tlv_hdr *) buf;
1316 tlen = be_to_host16(hdr->len);
1317 if (be_to_host16(hdr->type) == PAC_TYPE_A_ID &&
1318 sizeof(*hdr) + tlen <= len) {
1319 wpa_printf(MSG_DEBUG, "EAP-FAST: A-ID was in TLV "
1320 "(Start)");
1321 a_id = (u8 *) (hdr + 1);
1322 *id_len = tlen;
1325 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: A-ID", a_id, *id_len);
1327 return a_id;
1331 static void eap_fast_select_pac(struct eap_fast_data *data,
1332 const u8 *a_id, size_t a_id_len)
1334 data->current_pac = eap_fast_get_pac(data->pac, a_id, a_id_len,
1335 PAC_TYPE_TUNNEL_PAC);
1336 if (data->current_pac == NULL) {
1338 * Tunnel PAC was not available for this A-ID. Try to use
1339 * Machine Authentication PAC, if one is available.
1341 data->current_pac = eap_fast_get_pac(
1342 data->pac, a_id, a_id_len,
1343 PAC_TYPE_MACHINE_AUTHENTICATION);
1346 if (data->current_pac) {
1347 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC found for this A-ID "
1348 "(PAC-Type %d)", data->current_pac->pac_type);
1349 wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-FAST: A-ID-Info",
1350 data->current_pac->a_id_info,
1351 data->current_pac->a_id_info_len);
1356 static int eap_fast_use_pac_opaque(struct eap_sm *sm,
1357 struct eap_fast_data *data,
1358 struct eap_fast_pac *pac)
1360 u8 *tlv;
1361 size_t tlv_len, olen;
1362 struct eap_tlv_hdr *ehdr;
1364 olen = pac->pac_opaque_len;
1365 tlv_len = sizeof(*ehdr) + olen;
1366 tlv = os_malloc(tlv_len);
1367 if (tlv) {
1368 ehdr = (struct eap_tlv_hdr *) tlv;
1369 ehdr->tlv_type = host_to_be16(PAC_TYPE_PAC_OPAQUE);
1370 ehdr->length = host_to_be16(olen);
1371 os_memcpy(ehdr + 1, pac->pac_opaque, olen);
1373 if (tlv == NULL ||
1374 tls_connection_client_hello_ext(sm->ssl_ctx, data->ssl.conn,
1375 TLS_EXT_PAC_OPAQUE,
1376 tlv, tlv_len) < 0) {
1377 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to add PAC-Opaque TLS "
1378 "extension");
1379 os_free(tlv);
1380 return -1;
1382 os_free(tlv);
1384 return 0;
1388 static int eap_fast_clear_pac_opaque_ext(struct eap_sm *sm,
1389 struct eap_fast_data *data)
1391 if (tls_connection_client_hello_ext(sm->ssl_ctx, data->ssl.conn,
1392 TLS_EXT_PAC_OPAQUE, NULL, 0) < 0) {
1393 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to remove PAC-Opaque "
1394 "TLS extension");
1395 return -1;
1397 return 0;
1401 static int eap_fast_set_provisioning_ciphers(struct eap_sm *sm,
1402 struct eap_fast_data *data)
1404 u8 ciphers[5];
1405 int count = 0;
1407 if (data->provisioning_allowed & EAP_FAST_PROV_UNAUTH) {
1408 wpa_printf(MSG_DEBUG, "EAP-FAST: Enabling unauthenticated "
1409 "provisioning TLS cipher suites");
1410 ciphers[count++] = TLS_CIPHER_ANON_DH_AES128_SHA;
1413 if (data->provisioning_allowed & EAP_FAST_PROV_AUTH) {
1414 wpa_printf(MSG_DEBUG, "EAP-FAST: Enabling authenticated "
1415 "provisioning TLS cipher suites");
1416 ciphers[count++] = TLS_CIPHER_RSA_DHE_AES128_SHA;
1417 ciphers[count++] = TLS_CIPHER_AES128_SHA;
1418 ciphers[count++] = TLS_CIPHER_RC4_SHA;
1421 ciphers[count++] = TLS_CIPHER_NONE;
1423 if (tls_connection_set_cipher_list(sm->ssl_ctx, data->ssl.conn,
1424 ciphers)) {
1425 wpa_printf(MSG_INFO, "EAP-FAST: Could not configure TLS "
1426 "cipher suites for provisioning");
1427 return -1;
1430 return 0;
1434 static int eap_fast_process_start(struct eap_sm *sm,
1435 struct eap_fast_data *data, u8 flags,
1436 const u8 *pos, size_t left)
1438 const u8 *a_id;
1439 size_t a_id_len;
1441 /* EAP-FAST Version negotiation (section 3.1) */
1442 wpa_printf(MSG_DEBUG, "EAP-FAST: Start (server ver=%d, own ver=%d)",
1443 flags & EAP_PEAP_VERSION_MASK, data->fast_version);
1444 if ((flags & EAP_PEAP_VERSION_MASK) < data->fast_version)
1445 data->fast_version = flags & EAP_PEAP_VERSION_MASK;
1446 wpa_printf(MSG_DEBUG, "EAP-FAST: Using FAST version %d",
1447 data->fast_version);
1449 a_id = eap_fast_get_a_id(pos, left, &a_id_len);
1450 eap_fast_select_pac(data, a_id, a_id_len);
1452 if (data->resuming && data->current_pac) {
1453 wpa_printf(MSG_DEBUG, "EAP-FAST: Trying to resume session - "
1454 "do not add PAC-Opaque to TLS ClientHello");
1455 if (eap_fast_clear_pac_opaque_ext(sm, data) < 0)
1456 return -1;
1457 } else if (data->current_pac) {
1459 * PAC found for the A-ID and we are not resuming an old
1460 * session, so add PAC-Opaque extension to ClientHello.
1462 if (eap_fast_use_pac_opaque(sm, data, data->current_pac) < 0)
1463 return -1;
1464 } else {
1465 /* No PAC found, so we must provision one. */
1466 if (!data->provisioning_allowed) {
1467 wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC found and "
1468 "provisioning disabled");
1469 return -1;
1471 wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC found - "
1472 "starting provisioning");
1473 if (eap_fast_set_provisioning_ciphers(sm, data) < 0 ||
1474 eap_fast_clear_pac_opaque_ext(sm, data) < 0)
1475 return -1;
1476 data->provisioning = 1;
1479 return 0;
1483 static struct wpabuf * eap_fast_process(struct eap_sm *sm, void *priv,
1484 struct eap_method_ret *ret,
1485 const struct wpabuf *reqData)
1487 const struct eap_hdr *req;
1488 size_t left;
1489 int res;
1490 u8 flags, id;
1491 struct wpabuf *resp;
1492 const u8 *pos;
1493 struct eap_fast_data *data = priv;
1495 pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_FAST, ret,
1496 reqData, &left, &flags);
1497 if (pos == NULL)
1498 return NULL;
1500 req = wpabuf_head(reqData);
1501 id = req->identifier;
1503 if (flags & EAP_TLS_FLAGS_START) {
1504 if (eap_fast_process_start(sm, data, flags, pos, left) < 0)
1505 return NULL;
1507 left = 0; /* A-ID is not used in further packet processing */
1510 resp = NULL;
1511 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1512 !data->resuming) {
1513 /* Process tunneled (encrypted) phase 2 data. */
1514 struct wpabuf msg;
1515 wpabuf_set(&msg, pos, left);
1516 res = eap_fast_decrypt(sm, data, ret, req, &msg, &resp);
1517 if (res < 0) {
1518 ret->methodState = METHOD_DONE;
1519 ret->decision = DECISION_FAIL;
1521 * Ack possible Alert that may have caused failure in
1522 * decryption.
1524 res = 1;
1526 } else {
1527 /* Continue processing TLS handshake (phase 1). */
1528 res = eap_peer_tls_process_helper(sm, &data->ssl,
1529 EAP_TYPE_FAST,
1530 data->fast_version, id, pos,
1531 left, &resp);
1533 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
1534 char cipher[80];
1535 wpa_printf(MSG_DEBUG,
1536 "EAP-FAST: TLS done, proceed to Phase 2");
1537 if (data->provisioning &&
1538 (!(data->provisioning_allowed &
1539 EAP_FAST_PROV_AUTH) ||
1540 tls_get_cipher(sm->ssl_ctx, data->ssl.conn,
1541 cipher, sizeof(cipher)) < 0 ||
1542 os_strstr(cipher, "ADH-") ||
1543 os_strstr(cipher, "anon"))) {
1544 wpa_printf(MSG_DEBUG, "EAP-FAST: Using "
1545 "anonymous (unauthenticated) "
1546 "provisioning");
1547 data->anon_provisioning = 1;
1548 } else
1549 data->anon_provisioning = 0;
1550 data->resuming = 0;
1551 eap_fast_derive_keys(sm, data);
1554 if (res == 2) {
1555 struct wpabuf msg;
1557 * Application data included in the handshake message.
1559 wpabuf_free(data->pending_phase2_req);
1560 data->pending_phase2_req = resp;
1561 resp = NULL;
1562 wpabuf_set(&msg, pos, left);
1563 res = eap_fast_decrypt(sm, data, ret, req, &msg,
1564 &resp);
1568 if (res == 1) {
1569 wpabuf_free(resp);
1570 return eap_peer_tls_build_ack(id, EAP_TYPE_FAST,
1571 data->fast_version);
1574 return resp;
1578 #if 0 /* FIX */
1579 static Boolean eap_fast_has_reauth_data(struct eap_sm *sm, void *priv)
1581 struct eap_fast_data *data = priv;
1582 return tls_connection_established(sm->ssl_ctx, data->ssl.conn);
1586 static void eap_fast_deinit_for_reauth(struct eap_sm *sm, void *priv)
1588 struct eap_fast_data *data = priv;
1589 os_free(data->key_block_p);
1590 data->key_block_p = NULL;
1591 wpabuf_free(data->pending_phase2_req);
1592 data->pending_phase2_req = NULL;
1596 static void * eap_fast_init_for_reauth(struct eap_sm *sm, void *priv)
1598 struct eap_fast_data *data = priv;
1599 if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
1600 os_free(data);
1601 return NULL;
1603 if (data->phase2_priv && data->phase2_method &&
1604 data->phase2_method->init_for_reauth)
1605 data->phase2_method->init_for_reauth(sm, data->phase2_priv);
1606 data->phase2_success = 0;
1607 data->resuming = 1;
1608 data->provisioning = 0;
1609 data->anon_provisioning = 0;
1610 data->simck_idx = 0;
1611 return priv;
1613 #endif
1616 static int eap_fast_get_status(struct eap_sm *sm, void *priv, char *buf,
1617 size_t buflen, int verbose)
1619 struct eap_fast_data *data = priv;
1620 int len, ret;
1622 len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
1623 if (data->phase2_method) {
1624 ret = os_snprintf(buf + len, buflen - len,
1625 "EAP-FAST Phase2 method=%s\n",
1626 data->phase2_method->name);
1627 if (ret < 0 || (size_t) ret >= buflen - len)
1628 return len;
1629 len += ret;
1631 return len;
1635 static Boolean eap_fast_isKeyAvailable(struct eap_sm *sm, void *priv)
1637 struct eap_fast_data *data = priv;
1638 return data->success;
1642 static u8 * eap_fast_getKey(struct eap_sm *sm, void *priv, size_t *len)
1644 struct eap_fast_data *data = priv;
1645 u8 *key;
1647 if (!data->success)
1648 return NULL;
1650 key = os_malloc(EAP_FAST_KEY_LEN);
1651 if (key == NULL)
1652 return NULL;
1654 *len = EAP_FAST_KEY_LEN;
1655 os_memcpy(key, data->key_data, EAP_FAST_KEY_LEN);
1657 return key;
1661 static u8 * eap_fast_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
1663 struct eap_fast_data *data = priv;
1664 u8 *key;
1666 if (!data->success)
1667 return NULL;
1669 key = os_malloc(EAP_EMSK_LEN);
1670 if (key == NULL)
1671 return NULL;
1673 *len = EAP_EMSK_LEN;
1674 os_memcpy(key, data->emsk, EAP_EMSK_LEN);
1676 return key;
1680 int eap_peer_fast_register(void)
1682 struct eap_method *eap;
1683 int ret;
1685 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
1686 EAP_VENDOR_IETF, EAP_TYPE_FAST, "FAST");
1687 if (eap == NULL)
1688 return -1;
1690 eap->init = eap_fast_init;
1691 eap->deinit = eap_fast_deinit;
1692 eap->process = eap_fast_process;
1693 eap->isKeyAvailable = eap_fast_isKeyAvailable;
1694 eap->getKey = eap_fast_getKey;
1695 eap->get_status = eap_fast_get_status;
1696 #if 0
1697 eap->has_reauth_data = eap_fast_has_reauth_data;
1698 eap->deinit_for_reauth = eap_fast_deinit_for_reauth;
1699 eap->init_for_reauth = eap_fast_init_for_reauth;
1700 #endif
1701 eap->get_emsk = eap_fast_get_emsk;
1703 ret = eap_peer_method_register(eap);
1704 if (ret)
1705 eap_peer_method_free(eap);
1706 return ret;