]> git.ipfire.org Git - thirdparty/hostap.git/blob - src/eap_server/eap_fast.c
EAP-FAST: Cleaned up TLV processing and added support for EAP Sequences
[thirdparty/hostap.git] / src / eap_server / eap_fast.c
1 /*
2 * EAP-FAST server (RFC 4851)
3 * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
4 *
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.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 */
14
15 #include "includes.h"
16
17 #include "common.h"
18 #include "aes_wrap.h"
19 #include "sha1.h"
20 #include "eap_i.h"
21 #include "eap_tls_common.h"
22 #include "tls.h"
23 #include "eap_common/eap_tlv_common.h"
24 #include "eap_common/eap_fast_common.h"
25
26
27 static void eap_fast_reset(struct eap_sm *sm, void *priv);
28
29
30 /* Private PAC-Opaque TLV types */
31 #define PAC_OPAQUE_TYPE_PAD 0
32 #define PAC_OPAQUE_TYPE_KEY 1
33 #define PAC_OPAQUE_TYPE_LIFETIME 2
34 #define PAC_OPAQUE_TYPE_IDENTITY 3
35
36 /* PAC-Key lifetime in seconds (hard limit) */
37 #define PAC_KEY_LIFETIME (7 * 24 * 60 * 60)
38
39 /*
40 * PAC-Key refresh time in seconds (soft limit on remaining hard limit). The
41 * server will generate a new PAC-Key when this number of seconds (or fewer)
42 * of the lifetime.
43 */
44 #define PAC_KEY_REFRESH_TIME (1 * 24 * 60 * 60)
45
46
47 struct eap_fast_data {
48 struct eap_ssl_data ssl;
49 enum {
50 START, PHASE1, PHASE2_START, PHASE2_ID, PHASE2_METHOD,
51 CRYPTO_BINDING, REQUEST_PAC, SUCCESS, FAILURE
52 } state;
53
54 int fast_version;
55 const struct eap_method *phase2_method;
56 void *phase2_priv;
57 int force_version;
58 int peer_version;
59
60 u8 crypto_binding_nonce[32];
61 int final_result;
62
63 struct eap_fast_key_block_provisioning *key_block_p;
64
65 u8 simck[EAP_FAST_SIMCK_LEN];
66 u8 cmk[EAP_FAST_CMK_LEN];
67 int simck_idx;
68
69 u8 pac_opaque_encr[16];
70 char *srv_id;
71
72 int anon_provisioning;
73 int send_new_pac; /* server triggered re-keying of Tunnel PAC */
74 struct wpabuf *pending_phase2_resp;
75 u8 *identity; /* from PAC-Opaque */
76 size_t identity_len;
77 int eap_seq;
78 };
79
80
81 static const char * eap_fast_state_txt(int state)
82 {
83 switch (state) {
84 case START:
85 return "START";
86 case PHASE1:
87 return "PHASE1";
88 case PHASE2_START:
89 return "PHASE2_START";
90 case PHASE2_ID:
91 return "PHASE2_ID";
92 case PHASE2_METHOD:
93 return "PHASE2_METHOD";
94 case CRYPTO_BINDING:
95 return "CRYPTO_BINDING";
96 case REQUEST_PAC:
97 return "REQUEST_PAC";
98 case SUCCESS:
99 return "SUCCESS";
100 case FAILURE:
101 return "FAILURE";
102 default:
103 return "Unknown?!";
104 }
105 }
106
107
108 static void eap_fast_state(struct eap_fast_data *data, int state)
109 {
110 wpa_printf(MSG_DEBUG, "EAP-FAST: %s -> %s",
111 eap_fast_state_txt(data->state),
112 eap_fast_state_txt(state));
113 data->state = state;
114 }
115
116
117 static EapType eap_fast_req_failure(struct eap_sm *sm,
118 struct eap_fast_data *data)
119 {
120 /* TODO: send Result TLV(FAILURE) */
121 eap_fast_state(data, FAILURE);
122 return EAP_TYPE_NONE;
123 }
124
125
126 static int eap_fast_session_ticket_cb(void *ctx, const u8 *ticket, size_t len,
127 const u8 *client_random,
128 const u8 *server_random,
129 u8 *master_secret)
130 {
131 struct eap_fast_data *data = ctx;
132 const u8 *pac_opaque;
133 size_t pac_opaque_len;
134 u8 *buf, *pos, *end, *pac_key = NULL;
135 os_time_t lifetime = 0;
136 struct os_time now;
137 u8 *identity = NULL;
138 size_t identity_len = 0;
139
140 wpa_printf(MSG_DEBUG, "EAP-FAST: SessionTicket callback");
141 wpa_hexdump(MSG_DEBUG, "EAP-FAST: SessionTicket (PAC-Opaque)",
142 ticket, len);
143
144 if (len < 4 || WPA_GET_BE16(ticket) != PAC_TYPE_PAC_OPAQUE) {
145 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignore invalid "
146 "SessionTicket");
147 return 0;
148 }
149
150 pac_opaque_len = WPA_GET_BE16(ticket + 2);
151 pac_opaque = ticket + 4;
152 if (pac_opaque_len < 8 || pac_opaque_len % 8 ||
153 pac_opaque_len > len - 4) {
154 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignore invalid PAC-Opaque "
155 "(len=%lu left=%lu)",
156 (unsigned long) pac_opaque_len,
157 (unsigned long) len);
158 return 0;
159 }
160 wpa_hexdump(MSG_DEBUG, "EAP-FAST: Received PAC-Opaque",
161 pac_opaque, pac_opaque_len);
162
163 buf = os_malloc(pac_opaque_len - 8);
164 if (buf == NULL) {
165 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to allocate memory "
166 "for decrypting PAC-Opaque");
167 return 0;
168 }
169
170 if (aes_unwrap(data->pac_opaque_encr, (pac_opaque_len - 8) / 8,
171 pac_opaque, buf) < 0) {
172 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to decrypt "
173 "PAC-Opaque");
174 os_free(buf);
175 /*
176 * This may have been caused by server changing the PAC-Opaque
177 * encryption key, so just ignore this PAC-Opaque instead of
178 * failing the authentication completely. Provisioning can now
179 * be used to provision a new PAC.
180 */
181 return 0;
182 }
183
184 end = buf + pac_opaque_len - 8;
185 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: Decrypted PAC-Opaque",
186 buf, end - buf);
187
188 pos = buf;
189 while (pos + 1 < end) {
190 if (pos + 2 + pos[1] > end)
191 break;
192
193 switch (*pos) {
194 case PAC_OPAQUE_TYPE_PAD:
195 pos = end;
196 break;
197 case PAC_OPAQUE_TYPE_KEY:
198 if (pos[1] != EAP_FAST_PAC_KEY_LEN) {
199 wpa_printf(MSG_DEBUG, "EAP-FAST: Invalid "
200 "PAC-Key length %d", pos[1]);
201 os_free(buf);
202 return -1;
203 }
204 pac_key = pos + 2;
205 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: PAC-Key from "
206 "decrypted PAC-Opaque",
207 pac_key, EAP_FAST_PAC_KEY_LEN);
208 break;
209 case PAC_OPAQUE_TYPE_LIFETIME:
210 if (pos[1] != 4) {
211 wpa_printf(MSG_DEBUG, "EAP-FAST: Invalid "
212 "PAC-Key lifetime length %d",
213 pos[1]);
214 os_free(buf);
215 return -1;
216 }
217 lifetime = WPA_GET_BE32(pos + 2);
218 break;
219 case PAC_OPAQUE_TYPE_IDENTITY:
220 identity = pos + 2;
221 identity_len = pos[1];
222 break;
223 }
224
225 pos += 2 + pos[1];
226 }
227
228 if (pac_key == NULL) {
229 wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC-Key included in "
230 "PAC-Opaque");
231 os_free(buf);
232 return -1;
233 }
234
235 if (identity) {
236 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: Identity from "
237 "PAC-Opaque", identity, identity_len);
238 os_free(data->identity);
239 data->identity = os_malloc(identity_len);
240 if (data->identity) {
241 os_memcpy(data->identity, identity, identity_len);
242 data->identity_len = identity_len;
243 }
244 }
245
246 if (os_get_time(&now) < 0 || lifetime <= 0 || now.sec > lifetime) {
247 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Key not valid anymore "
248 "(lifetime=%ld now=%ld)", lifetime, now.sec);
249 os_free(buf);
250 return 0;
251 }
252
253 if (lifetime - now.sec < PAC_KEY_REFRESH_TIME)
254 data->send_new_pac = 1;
255
256 eap_fast_derive_master_secret(pac_key, server_random, client_random,
257 master_secret);
258
259 os_free(buf);
260
261 return 1;
262 }
263
264
265 static void eap_fast_derive_key_auth(struct eap_sm *sm,
266 struct eap_fast_data *data)
267 {
268 u8 *sks;
269
270 /* RFC 4851, Section 5.1:
271 * Extra key material after TLS key_block: session_key_seed[40]
272 */
273
274 sks = eap_fast_derive_key(sm->ssl_ctx, data->ssl.conn, "key expansion",
275 EAP_FAST_SKS_LEN);
276 if (sks == NULL) {
277 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive "
278 "session_key_seed");
279 return;
280 }
281
282 /*
283 * RFC 4851, Section 5.2:
284 * S-IMCK[0] = session_key_seed
285 */
286 wpa_hexdump_key(MSG_DEBUG,
287 "EAP-FAST: session_key_seed (SKS = S-IMCK[0])",
288 sks, EAP_FAST_SKS_LEN);
289 data->simck_idx = 0;
290 os_memcpy(data->simck, sks, EAP_FAST_SIMCK_LEN);
291 os_free(sks);
292 }
293
294
295 static void eap_fast_derive_key_provisioning(struct eap_sm *sm,
296 struct eap_fast_data *data)
297 {
298 os_free(data->key_block_p);
299 data->key_block_p = (struct eap_fast_key_block_provisioning *)
300 eap_fast_derive_key(sm->ssl_ctx, data->ssl.conn,
301 "key expansion",
302 sizeof(*data->key_block_p));
303 if (data->key_block_p == NULL) {
304 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive key block");
305 return;
306 }
307 /*
308 * RFC 4851, Section 5.2:
309 * S-IMCK[0] = session_key_seed
310 */
311 wpa_hexdump_key(MSG_DEBUG,
312 "EAP-FAST: session_key_seed (SKS = S-IMCK[0])",
313 data->key_block_p->session_key_seed,
314 sizeof(data->key_block_p->session_key_seed));
315 data->simck_idx = 0;
316 os_memcpy(data->simck, data->key_block_p->session_key_seed,
317 EAP_FAST_SIMCK_LEN);
318 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: server_challenge",
319 data->key_block_p->server_challenge,
320 sizeof(data->key_block_p->server_challenge));
321 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: client_challenge",
322 data->key_block_p->client_challenge,
323 sizeof(data->key_block_p->client_challenge));
324 }
325
326
327 static int eap_fast_get_phase2_key(struct eap_sm *sm,
328 struct eap_fast_data *data,
329 u8 *isk, size_t isk_len)
330 {
331 u8 *key;
332 size_t key_len;
333
334 os_memset(isk, 0, isk_len);
335
336 if (data->phase2_method == NULL || data->phase2_priv == NULL) {
337 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 method not "
338 "available");
339 return -1;
340 }
341
342 if (data->phase2_method->getKey == NULL)
343 return 0;
344
345 if ((key = data->phase2_method->getKey(sm, data->phase2_priv,
346 &key_len)) == NULL) {
347 wpa_printf(MSG_DEBUG, "EAP-FAST: Could not get key material "
348 "from Phase 2");
349 return -1;
350 }
351
352 if (key_len > isk_len)
353 key_len = isk_len;
354 os_memcpy(isk, key, key_len);
355 os_free(key);
356
357 return 0;
358 }
359
360
361 static int eap_fast_update_icmk(struct eap_sm *sm, struct eap_fast_data *data)
362 {
363 u8 isk[32], imck[60];
364
365 wpa_printf(MSG_DEBUG, "EAP-FAST: Deriving ICMK[%d] (S-IMCK and CMK)",
366 data->simck_idx + 1);
367
368 /*
369 * RFC 4851, Section 5.2:
370 * IMCK[j] = T-PRF(S-IMCK[j-1], "Inner Methods Compound Keys",
371 * MSK[j], 60)
372 * S-IMCK[j] = first 40 octets of IMCK[j]
373 * CMK[j] = last 20 octets of IMCK[j]
374 */
375
376 if (eap_fast_get_phase2_key(sm, data, isk, sizeof(isk)) < 0)
377 return -1;
378 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: ISK[j]", isk, sizeof(isk));
379 sha1_t_prf(data->simck, EAP_FAST_SIMCK_LEN,
380 "Inner Methods Compound Keys",
381 isk, sizeof(isk), imck, sizeof(imck));
382 data->simck_idx++;
383 os_memcpy(data->simck, imck, EAP_FAST_SIMCK_LEN);
384 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: S-IMCK[j]",
385 data->simck, EAP_FAST_SIMCK_LEN);
386 os_memcpy(data->cmk, imck + EAP_FAST_SIMCK_LEN, EAP_FAST_CMK_LEN);
387 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: CMK[j]",
388 data->cmk, EAP_FAST_CMK_LEN);
389
390 return 0;
391 }
392
393
394 static void * eap_fast_init(struct eap_sm *sm)
395 {
396 struct eap_fast_data *data;
397 u8 ciphers[5] = {
398 TLS_CIPHER_ANON_DH_AES128_SHA,
399 TLS_CIPHER_AES128_SHA,
400 TLS_CIPHER_RSA_DHE_AES128_SHA,
401 TLS_CIPHER_RC4_SHA,
402 TLS_CIPHER_NONE
403 };
404
405 data = os_zalloc(sizeof(*data));
406 if (data == NULL)
407 return NULL;
408 data->fast_version = EAP_FAST_VERSION;
409 data->force_version = -1;
410 if (sm->user && sm->user->force_version >= 0) {
411 data->force_version = sm->user->force_version;
412 wpa_printf(MSG_DEBUG, "EAP-FAST: forcing version %d",
413 data->force_version);
414 data->fast_version = data->force_version;
415 }
416 data->state = START;
417
418 if (eap_server_tls_ssl_init(sm, &data->ssl, 0)) {
419 wpa_printf(MSG_INFO, "EAP-FAST: Failed to initialize SSL.");
420 eap_fast_reset(sm, data);
421 return NULL;
422 }
423
424 if (tls_connection_set_cipher_list(sm->ssl_ctx, data->ssl.conn,
425 ciphers) < 0) {
426 wpa_printf(MSG_INFO, "EAP-FAST: Failed to set TLS cipher "
427 "suites");
428 eap_fast_reset(sm, data);
429 return NULL;
430 }
431
432 if (tls_connection_set_session_ticket_cb(sm->ssl_ctx, data->ssl.conn,
433 eap_fast_session_ticket_cb,
434 data) < 0) {
435 wpa_printf(MSG_INFO, "EAP-FAST: Failed to set SessionTicket "
436 "callback");
437 eap_fast_reset(sm, data);
438 return NULL;
439 }
440
441 if (sm->pac_opaque_encr_key == NULL) {
442 wpa_printf(MSG_INFO, "EAP-FAST: No PAC-Opaque encryption key "
443 "configured");
444 eap_fast_reset(sm, data);
445 return NULL;
446 }
447 os_memcpy(data->pac_opaque_encr, sm->pac_opaque_encr_key,
448 sizeof(data->pac_opaque_encr));
449
450 if (sm->eap_fast_a_id == NULL) {
451 wpa_printf(MSG_INFO, "EAP-FAST: No A-ID configured");
452 eap_fast_reset(sm, data);
453 return NULL;
454 }
455 data->srv_id = os_strdup(sm->eap_fast_a_id);
456 if (data->srv_id == NULL) {
457 eap_fast_reset(sm, data);
458 return NULL;
459 }
460
461 return data;
462 }
463
464
465 static void eap_fast_reset(struct eap_sm *sm, void *priv)
466 {
467 struct eap_fast_data *data = priv;
468 if (data == NULL)
469 return;
470 if (data->phase2_priv && data->phase2_method)
471 data->phase2_method->reset(sm, data->phase2_priv);
472 eap_server_tls_ssl_deinit(sm, &data->ssl);
473 os_free(data->srv_id);
474 os_free(data->key_block_p);
475 wpabuf_free(data->pending_phase2_resp);
476 os_free(data->identity);
477 os_free(data);
478 }
479
480
481 static struct wpabuf * eap_fast_build_start(struct eap_sm *sm,
482 struct eap_fast_data *data, u8 id)
483 {
484 struct wpabuf *req;
485 size_t srv_id_len = os_strlen(data->srv_id);
486
487 req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_FAST,
488 1 + sizeof(struct pac_tlv_hdr) + srv_id_len,
489 EAP_CODE_REQUEST, id);
490 if (req == NULL) {
491 wpa_printf(MSG_ERROR, "EAP-FAST: Failed to allocate memory for"
492 " request");
493 eap_fast_state(data, FAILURE);
494 return NULL;
495 }
496
497 wpabuf_put_u8(req, EAP_TLS_FLAGS_START | data->fast_version);
498
499 /* RFC 4851, 4.1.1. Authority ID Data */
500 eap_fast_put_tlv(req, PAC_TYPE_A_ID, data->srv_id, srv_id_len);
501
502 eap_fast_state(data, PHASE1);
503
504 return req;
505 }
506
507
508 static int eap_fast_phase1_done(struct eap_sm *sm, struct eap_fast_data *data)
509 {
510 char cipher[64];
511
512 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase1 done, starting Phase2");
513
514 if (tls_get_cipher(sm->ssl_ctx, data->ssl.conn, cipher, sizeof(cipher))
515 < 0) {
516 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to get cipher "
517 "information");
518 eap_fast_state(data, FAILURE);
519 return -1;
520 }
521 data->anon_provisioning = os_strstr(cipher, "ADH") != NULL;
522
523 if (data->anon_provisioning) {
524 wpa_printf(MSG_DEBUG, "EAP-FAST: Anonymous provisioning");
525 eap_fast_derive_key_provisioning(sm, data);
526 } else
527 eap_fast_derive_key_auth(sm, data);
528
529 eap_fast_state(data, PHASE2_START);
530
531 return 0;
532 }
533
534
535 static struct wpabuf * eap_fast_build_req(struct eap_sm *sm,
536 struct eap_fast_data *data, u8 id)
537 {
538 int res;
539 struct wpabuf *req;
540
541 res = eap_server_tls_buildReq_helper(sm, &data->ssl, EAP_TYPE_FAST,
542 data->fast_version, id, &req);
543
544 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
545 if (eap_fast_phase1_done(sm, data) < 0) {
546 os_free(req);
547 return NULL;
548 }
549 }
550
551 if (res == 1)
552 return eap_server_tls_build_ack(id, EAP_TYPE_FAST,
553 data->fast_version);
554 return req;
555 }
556
557
558 static struct wpabuf * eap_fast_encrypt(struct eap_sm *sm,
559 struct eap_fast_data *data,
560 u8 id, u8 *plain, size_t plain_len)
561 {
562 int res;
563 struct wpabuf *buf;
564
565 /* TODO: add support for fragmentation, if needed. This will need to
566 * add TLS Message Length field, if the frame is fragmented. */
567 buf = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_FAST,
568 1 + data->ssl.tls_out_limit,
569 EAP_CODE_REQUEST, id);
570 if (buf == NULL)
571 return NULL;
572
573 wpabuf_put_u8(buf, data->fast_version);
574
575 res = tls_connection_encrypt(sm->ssl_ctx, data->ssl.conn,
576 plain, plain_len, wpabuf_put(buf, 0),
577 data->ssl.tls_out_limit);
578 if (res < 0) {
579 wpa_printf(MSG_INFO, "EAP-FAST: Failed to encrypt Phase 2 "
580 "data");
581 wpabuf_free(buf);
582 return NULL;
583 }
584
585 wpabuf_put(buf, res);
586 eap_update_len(buf);
587
588 return buf;
589 }
590
591
592 static struct wpabuf * eap_fast_build_phase2_req(struct eap_sm *sm,
593 struct eap_fast_data *data,
594 u8 id)
595 {
596 struct wpabuf *req;
597
598 if (data->phase2_priv == NULL) {
599 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 method not "
600 "initialized");
601 return NULL;
602 }
603 req = data->phase2_method->buildReq(sm, data->phase2_priv, id);
604 if (req == NULL)
605 return NULL;
606
607 wpa_hexdump_buf_key(MSG_MSGDUMP, "EAP-FAST: Phase 2 EAP-Request", req);
608 return eap_fast_tlv_eap_payload(req);
609 }
610
611
612 static struct wpabuf * eap_fast_build_crypto_binding(
613 struct eap_sm *sm, struct eap_fast_data *data)
614 {
615 struct wpabuf *buf;
616 struct eap_tlv_result_tlv *result;
617 struct eap_tlv_crypto_binding_tlv *binding;
618
619 buf = wpabuf_alloc(2 * sizeof(*result) + sizeof(*binding));
620 if (buf == NULL)
621 return NULL;
622
623 if (data->send_new_pac || data->anon_provisioning ||
624 data->phase2_method)
625 data->final_result = 0;
626 else
627 data->final_result = 1;
628
629 if (!data->final_result || data->eap_seq > 1) {
630 /* Intermediate-Result */
631 wpa_printf(MSG_DEBUG, "EAP-FAST: Add Intermediate-Result TLV "
632 "(status=SUCCESS)");
633 result = wpabuf_put(buf, sizeof(*result));
634 result->tlv_type = host_to_be16(
635 EAP_TLV_TYPE_MANDATORY |
636 EAP_TLV_INTERMEDIATE_RESULT_TLV);
637 result->length = host_to_be16(2);
638 result->status = host_to_be16(EAP_TLV_RESULT_SUCCESS);
639 }
640
641 if (data->final_result) {
642 /* Result TLV */
643 wpa_printf(MSG_DEBUG, "EAP-FAST: Add Result TLV "
644 "(status=SUCCESS)");
645 result = wpabuf_put(buf, sizeof(*result));
646 result->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
647 EAP_TLV_RESULT_TLV);
648 result->length = host_to_be16(2);
649 result->status = host_to_be16(EAP_TLV_RESULT_SUCCESS);
650 }
651
652 /* Crypto-Binding TLV */
653 binding = wpabuf_put(buf, sizeof(*binding));
654 binding->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
655 EAP_TLV_CRYPTO_BINDING_TLV);
656 binding->length = host_to_be16(sizeof(*binding) -
657 sizeof(struct eap_tlv_hdr));
658 binding->version = EAP_FAST_VERSION;
659 binding->received_version = data->peer_version;
660 binding->subtype = EAP_TLV_CRYPTO_BINDING_SUBTYPE_REQUEST;
661 if (os_get_random(binding->nonce, sizeof(binding->nonce)) < 0) {
662 wpabuf_free(buf);
663 return NULL;
664 }
665
666 /*
667 * RFC 4851, Section 4.2.8:
668 * The nonce in a request MUST have its least significant bit set to 0.
669 */
670 binding->nonce[sizeof(binding->nonce) - 1] &= ~0x01;
671
672 os_memcpy(data->crypto_binding_nonce, binding->nonce,
673 sizeof(binding->nonce));
674
675 /*
676 * RFC 4851, Section 5.3:
677 * CMK = CMK[j]
678 * Compound-MAC = HMAC-SHA1( CMK, Crypto-Binding TLV )
679 */
680
681 hmac_sha1(data->cmk, EAP_FAST_CMK_LEN,
682 (u8 *) binding, sizeof(*binding),
683 binding->compound_mac);
684
685 wpa_printf(MSG_DEBUG, "EAP-FAST: Add Crypto-Binding TLV: Version %d "
686 "Received Version %d SubType %d",
687 binding->version, binding->received_version,
688 binding->subtype);
689 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE",
690 binding->nonce, sizeof(binding->nonce));
691 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC",
692 binding->compound_mac, sizeof(binding->compound_mac));
693
694 return buf;
695 }
696
697
698 static struct wpabuf * eap_fast_build_pac(struct eap_sm *sm,
699 struct eap_fast_data *data)
700 {
701 u8 pac_key[EAP_FAST_PAC_KEY_LEN];
702 u8 *pac_buf, *pac_opaque;
703 struct wpabuf *buf;
704 u8 *pos;
705 size_t buf_len, srv_id_len, pac_len;
706 struct eap_tlv_hdr *pac_tlv;
707 struct pac_tlv_hdr *pac_info;
708 struct eap_tlv_result_tlv *result;
709 struct os_time now;
710
711 if (os_get_random(pac_key, EAP_FAST_PAC_KEY_LEN) < 0 ||
712 os_get_time(&now) < 0)
713 return NULL;
714 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: Generated PAC-Key",
715 pac_key, EAP_FAST_PAC_KEY_LEN);
716
717 pac_len = (2 + EAP_FAST_PAC_KEY_LEN) + (2 + 4) +
718 (2 + sm->identity_len) + 8;
719 pac_buf = os_malloc(pac_len);
720 if (pac_buf == NULL)
721 return NULL;
722
723 srv_id_len = os_strlen(data->srv_id);
724
725 pos = pac_buf;
726 *pos++ = PAC_OPAQUE_TYPE_KEY;
727 *pos++ = EAP_FAST_PAC_KEY_LEN;
728 os_memcpy(pos, pac_key, EAP_FAST_PAC_KEY_LEN);
729 pos += EAP_FAST_PAC_KEY_LEN;
730
731 *pos++ = PAC_OPAQUE_TYPE_LIFETIME;
732 *pos++ = 4;
733 WPA_PUT_BE32(pos, now.sec + PAC_KEY_LIFETIME);
734 pos += 4;
735
736 if (sm->identity) {
737 *pos++ = PAC_OPAQUE_TYPE_IDENTITY;
738 *pos++ = sm->identity_len;
739 os_memcpy(pos, sm->identity, sm->identity_len);
740 pos += sm->identity_len;
741 }
742
743 pac_len = pos - pac_buf;
744 if (pac_len % 8) {
745 *pos++ = PAC_OPAQUE_TYPE_PAD;
746 pac_len++;
747 }
748
749 pac_opaque = os_malloc(pac_len + 8);
750 if (pac_opaque == NULL) {
751 os_free(pac_buf);
752 return NULL;
753 }
754 if (aes_wrap(data->pac_opaque_encr, pac_len / 8, pac_buf,
755 pac_opaque) < 0) {
756 os_free(pac_buf);
757 os_free(pac_opaque);
758 return NULL;
759 }
760 os_free(pac_buf);
761
762 pac_len += 8;
763 wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Opaque",
764 pac_opaque, pac_len);
765
766 buf_len = sizeof(*pac_tlv) +
767 sizeof(struct pac_tlv_hdr) + EAP_FAST_PAC_KEY_LEN +
768 sizeof(struct pac_tlv_hdr) + pac_len +
769 2 * srv_id_len + 100 + sizeof(*result);
770 buf = wpabuf_alloc(buf_len);
771 if (buf == NULL) {
772 os_free(pac_opaque);
773 return NULL;
774 }
775
776 /* PAC TLV */
777 wpa_printf(MSG_DEBUG, "EAP-FAST: Add PAC TLV");
778 pac_tlv = wpabuf_put(buf, sizeof(*pac_tlv));
779 pac_tlv->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
780 EAP_TLV_PAC_TLV);
781
782 /* PAC-Key */
783 eap_fast_put_tlv(buf, PAC_TYPE_PAC_KEY, pac_key, EAP_FAST_PAC_KEY_LEN);
784
785 /* PAC-Opaque */
786 eap_fast_put_tlv(buf, PAC_TYPE_PAC_OPAQUE, pac_opaque, pac_len);
787 os_free(pac_opaque);
788
789 /* PAC-Info */
790 pac_info = wpabuf_put(buf, sizeof(*pac_info));
791 pac_info->type = host_to_be16(PAC_TYPE_PAC_INFO);
792
793 /* PAC-Lifetime (inside PAC-Info) */
794 eap_fast_put_tlv_hdr(buf, PAC_TYPE_CRED_LIFETIME, 4);
795 wpabuf_put_be32(buf, now.sec + PAC_KEY_LIFETIME);
796
797 /* A-ID (inside PAC-Info) */
798 eap_fast_put_tlv(buf, PAC_TYPE_A_ID, data->srv_id, srv_id_len);
799
800 /* Note: headers may be misaligned after A-ID */
801
802 /* A-ID-Info (inside PAC-Info) */
803 eap_fast_put_tlv(buf, PAC_TYPE_A_ID_INFO, data->srv_id, srv_id_len);
804
805 /* PAC-Type (inside PAC-Info) */
806 eap_fast_put_tlv_hdr(buf, PAC_TYPE_PAC_TYPE, 2);
807 wpabuf_put_be16(buf, PAC_TYPE_TUNNEL_PAC);
808
809 /* Update PAC-Info and PAC TLV Length fields */
810 pos = wpabuf_put(buf, 0);
811 pac_info->len = host_to_be16(pos - (u8 *) (pac_info + 1));
812 pac_tlv->length = host_to_be16(pos - (u8 *) (pac_tlv + 1));
813
814 /* Result TLV */
815 wpa_printf(MSG_DEBUG, "EAP-FAST: Add Result TLV (status=SUCCESS)");
816 result = wpabuf_put(buf, sizeof(*result));
817 WPA_PUT_BE16((u8 *) &result->tlv_type,
818 EAP_TLV_TYPE_MANDATORY | EAP_TLV_RESULT_TLV);
819 WPA_PUT_BE16((u8 *) &result->length, 2);
820 WPA_PUT_BE16((u8 *) &result->status, EAP_TLV_RESULT_SUCCESS);
821
822 return buf;
823 }
824
825
826 static struct wpabuf * eap_fast_buildReq(struct eap_sm *sm, void *priv, u8 id)
827 {
828 struct eap_fast_data *data = priv;
829 struct wpabuf *req;
830 struct wpabuf *encr;
831
832 if (data->state == START)
833 return eap_fast_build_start(sm, data, id);
834
835 if (data->state == PHASE1)
836 return eap_fast_build_req(sm, data, id);
837
838 switch (data->state) {
839 case PHASE2_ID:
840 case PHASE2_METHOD:
841 req = eap_fast_build_phase2_req(sm, data, id);
842 break;
843 case CRYPTO_BINDING:
844 req = eap_fast_build_crypto_binding(sm, data);
845 if (data->phase2_method) {
846 /*
847 * Include the start of the next EAP method in the
848 * sequence in the same message with Crypto-Binding to
849 * save a round-trip.
850 */
851 struct wpabuf *eap;
852 eap = eap_fast_build_phase2_req(sm, data, id);
853 req = wpabuf_concat(req, eap);
854 }
855 break;
856 case REQUEST_PAC:
857 req = eap_fast_build_pac(sm, data);
858 break;
859 default:
860 wpa_printf(MSG_DEBUG, "EAP-FAST: %s - unexpected state %d",
861 __func__, data->state);
862 return NULL;
863 }
864
865 if (req == NULL)
866 return NULL;
867
868 wpa_hexdump_buf_key(MSG_DEBUG, "EAP-FAST: Encrypting Phase 2 TLVs",
869 req);
870 encr = eap_fast_encrypt(sm, data, id, wpabuf_mhead(req),
871 wpabuf_len(req));
872 wpabuf_free(req);
873
874 return encr;
875 }
876
877
878 static Boolean eap_fast_check(struct eap_sm *sm, void *priv,
879 struct wpabuf *respData)
880 {
881 const u8 *pos;
882 size_t len;
883
884 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_FAST, respData, &len);
885 if (pos == NULL || len < 1) {
886 wpa_printf(MSG_INFO, "EAP-FAST: Invalid frame");
887 return TRUE;
888 }
889
890 return FALSE;
891 }
892
893
894 static int eap_fast_phase2_init(struct eap_sm *sm, struct eap_fast_data *data,
895 EapType eap_type)
896 {
897 if (data->phase2_priv && data->phase2_method) {
898 data->phase2_method->reset(sm, data->phase2_priv);
899 data->phase2_method = NULL;
900 data->phase2_priv = NULL;
901 }
902 data->phase2_method = eap_server_get_eap_method(EAP_VENDOR_IETF,
903 eap_type);
904 if (!data->phase2_method)
905 return -1;
906
907 if (data->key_block_p) {
908 sm->auth_challenge = data->key_block_p->server_challenge;
909 sm->peer_challenge = data->key_block_p->client_challenge;
910 }
911 sm->init_phase2 = 1;
912 data->phase2_priv = data->phase2_method->init(sm);
913 sm->init_phase2 = 0;
914 sm->auth_challenge = NULL;
915 sm->peer_challenge = NULL;
916
917 return data->phase2_priv == NULL ? -1 : 0;
918 }
919
920
921 static void eap_fast_process_phase2_response(struct eap_sm *sm,
922 struct eap_fast_data *data,
923 u8 *in_data, size_t in_len)
924 {
925 u8 next_type = EAP_TYPE_NONE;
926 struct eap_hdr *hdr;
927 u8 *pos;
928 size_t left;
929 struct wpabuf buf;
930 const struct eap_method *m = data->phase2_method;
931 void *priv = data->phase2_priv;
932
933 if (priv == NULL) {
934 wpa_printf(MSG_DEBUG, "EAP-FAST: %s - Phase2 not "
935 "initialized?!", __func__);
936 return;
937 }
938
939 hdr = (struct eap_hdr *) in_data;
940 pos = (u8 *) (hdr + 1);
941
942 if (in_len > sizeof(*hdr) && *pos == EAP_TYPE_NAK) {
943 left = in_len - sizeof(*hdr);
944 wpa_hexdump(MSG_DEBUG, "EAP-FAST: Phase2 type Nak'ed; "
945 "allowed types", pos + 1, left - 1);
946 eap_sm_process_nak(sm, pos + 1, left - 1);
947 if (sm->user && sm->user_eap_method_index < EAP_MAX_METHODS &&
948 sm->user->methods[sm->user_eap_method_index].method !=
949 EAP_TYPE_NONE) {
950 next_type = sm->user->methods[
951 sm->user_eap_method_index++].method;
952 wpa_printf(MSG_DEBUG, "EAP-FAST: try EAP type %d",
953 next_type);
954 } else {
955 next_type = eap_fast_req_failure(sm, data);
956 }
957 eap_fast_phase2_init(sm, data, next_type);
958 return;
959 }
960
961 wpabuf_set(&buf, in_data, in_len);
962
963 if (m->check(sm, priv, &buf)) {
964 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase2 check() asked to "
965 "ignore the packet");
966 next_type = eap_fast_req_failure(sm, data);
967 return;
968 }
969
970 m->process(sm, priv, &buf);
971
972 if (!m->isDone(sm, priv))
973 return;
974
975 if (!m->isSuccess(sm, priv)) {
976 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase2 method failed");
977 next_type = eap_fast_req_failure(sm, data);
978 eap_fast_phase2_init(sm, data, next_type);
979 return;
980 }
981
982 switch (data->state) {
983 case PHASE2_ID:
984 if (eap_user_get(sm, sm->identity, sm->identity_len, 1) != 0) {
985 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: Phase2 "
986 "Identity not found in the user "
987 "database",
988 sm->identity, sm->identity_len);
989 next_type = eap_fast_req_failure(sm, data);
990 break;
991 }
992
993 eap_fast_state(data, PHASE2_METHOD);
994 if (data->anon_provisioning) {
995 /*
996 * Only EAP-MSCHAPv2 is allowed for anonymous
997 * provisioning.
998 */
999 next_type = EAP_TYPE_MSCHAPV2;
1000 sm->user_eap_method_index = 0;
1001 } else {
1002 next_type = sm->user->methods[0].method;
1003 sm->user_eap_method_index = 1;
1004 }
1005 wpa_printf(MSG_DEBUG, "EAP-FAST: try EAP type %d", next_type);
1006 break;
1007 case PHASE2_METHOD:
1008 case CRYPTO_BINDING:
1009 eap_fast_update_icmk(sm, data);
1010 eap_fast_state(data, CRYPTO_BINDING);
1011 data->eap_seq++;
1012 next_type = EAP_TYPE_NONE;
1013 /* TODO: could start another EAP method in sequence by setting
1014 * next_type to the selected method */
1015 break;
1016 case FAILURE:
1017 break;
1018 default:
1019 wpa_printf(MSG_DEBUG, "EAP-FAST: %s - unexpected state %d",
1020 __func__, data->state);
1021 break;
1022 }
1023
1024 eap_fast_phase2_init(sm, data, next_type);
1025 }
1026
1027
1028 static void eap_fast_process_phase2_eap(struct eap_sm *sm,
1029 struct eap_fast_data *data,
1030 u8 *in_data, size_t in_len)
1031 {
1032 struct eap_hdr *hdr;
1033 size_t len;
1034
1035 hdr = (struct eap_hdr *) in_data;
1036 if (in_len < (int) sizeof(*hdr)) {
1037 wpa_printf(MSG_INFO, "EAP-FAST: Too short Phase 2 "
1038 "EAP frame (len=%d)", in_len);
1039 eap_fast_req_failure(sm, data);
1040 return;
1041 }
1042 len = be_to_host16(hdr->length);
1043 if (len > in_len) {
1044 wpa_printf(MSG_INFO, "EAP-FAST: Length mismatch in "
1045 "Phase 2 EAP frame (len=%d hdr->length=%d)",
1046 in_len, len);
1047 eap_fast_req_failure(sm, data);
1048 return;
1049 }
1050 wpa_printf(MSG_DEBUG, "EAP-FAST: Received Phase 2: code=%d "
1051 "identifier=%d length=%d", hdr->code, hdr->identifier, len);
1052 switch (hdr->code) {
1053 case EAP_CODE_RESPONSE:
1054 eap_fast_process_phase2_response(sm, data, (u8 *) hdr, len);
1055 break;
1056 default:
1057 wpa_printf(MSG_INFO, "EAP-FAST: Unexpected code=%d in "
1058 "Phase 2 EAP header", hdr->code);
1059 break;
1060 }
1061 }
1062
1063
1064 static int eap_fast_parse_tlvs(u8 *data, size_t data_len,
1065 struct eap_fast_tlv_parse *tlv)
1066 {
1067 int mandatory, tlv_type, len, res;
1068 u8 *pos, *end;
1069
1070 os_memset(tlv, 0, sizeof(*tlv));
1071
1072 pos = data;
1073 end = data + data_len;
1074 while (pos + 4 < end) {
1075 mandatory = pos[0] & 0x80;
1076 tlv_type = WPA_GET_BE16(pos) & 0x3fff;
1077 pos += 2;
1078 len = WPA_GET_BE16(pos);
1079 pos += 2;
1080 if (pos + len > end) {
1081 wpa_printf(MSG_INFO, "EAP-FAST: TLV overflow");
1082 return -1;
1083 }
1084 wpa_printf(MSG_DEBUG, "EAP-FAST: Received Phase 2: "
1085 "TLV type %d length %d%s",
1086 tlv_type, len, mandatory ? " (mandatory)" : "");
1087
1088 res = eap_fast_parse_tlv(tlv, tlv_type, pos, len);
1089 if (res == -2)
1090 break;
1091 if (res < 0) {
1092 if (mandatory) {
1093 wpa_printf(MSG_DEBUG, "EAP-FAST: Nak unknown "
1094 "mandatory TLV type %d", tlv_type);
1095 /* TODO: generate Nak TLV */
1096 break;
1097 } else {
1098 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignored "
1099 "unknown optional TLV type %d",
1100 tlv_type);
1101 }
1102 }
1103
1104 pos += len;
1105 }
1106
1107 return 0;
1108 }
1109
1110
1111 static int eap_fast_validate_crypto_binding(
1112 struct eap_fast_data *data, struct eap_tlv_crypto_binding_tlv *b,
1113 size_t bind_len)
1114 {
1115 u8 cmac[SHA1_MAC_LEN];
1116
1117 wpa_printf(MSG_DEBUG, "EAP-FAST: Reply Crypto-Binding TLV: "
1118 "Version %d Received Version %d SubType %d",
1119 b->version, b->received_version, b->subtype);
1120 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE",
1121 b->nonce, sizeof(b->nonce));
1122 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC",
1123 b->compound_mac, sizeof(b->compound_mac));
1124
1125 if (b->version != EAP_FAST_VERSION ||
1126 b->received_version != EAP_FAST_VERSION) {
1127 wpa_printf(MSG_DEBUG, "EAP-FAST: Unexpected version "
1128 "in Crypto-Binding: version %d "
1129 "received_version %d", b->version,
1130 b->received_version);
1131 return -1;
1132 }
1133
1134 if (b->subtype != EAP_TLV_CRYPTO_BINDING_SUBTYPE_RESPONSE) {
1135 wpa_printf(MSG_DEBUG, "EAP-FAST: Unexpected subtype in "
1136 "Crypto-Binding: %d", b->subtype);
1137 return -1;
1138 }
1139
1140 if (os_memcmp(data->crypto_binding_nonce, b->nonce, 31) != 0 ||
1141 (data->crypto_binding_nonce[31] | 1) != b->nonce[31]) {
1142 wpa_printf(MSG_DEBUG, "EAP-FAST: Invalid nonce in "
1143 "Crypto-Binding");
1144 return -1;
1145 }
1146
1147 os_memcpy(cmac, b->compound_mac, sizeof(cmac));
1148 os_memset(b->compound_mac, 0, sizeof(cmac));
1149 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Crypto-Binding TLV for "
1150 "Compound MAC calculation",
1151 (u8 *) b, bind_len);
1152 hmac_sha1(data->cmk, EAP_FAST_CMK_LEN, (u8 *) b, bind_len,
1153 b->compound_mac);
1154 if (os_memcmp(cmac, b->compound_mac, sizeof(cmac)) != 0) {
1155 wpa_hexdump(MSG_MSGDUMP,
1156 "EAP-FAST: Calculated Compound MAC",
1157 b->compound_mac, sizeof(cmac));
1158 wpa_printf(MSG_INFO, "EAP-FAST: Compound MAC did not "
1159 "match");
1160 return -1;
1161 }
1162
1163 return 0;
1164 }
1165
1166
1167 static int eap_fast_pac_type(u8 *pac, size_t len, u16 type)
1168 {
1169 struct eap_tlv_pac_type_tlv *tlv;
1170
1171 if (pac == NULL || len != sizeof(*tlv))
1172 return 0;
1173
1174 tlv = (struct eap_tlv_pac_type_tlv *) pac;
1175
1176 return be_to_host16(tlv->tlv_type) == PAC_TYPE_PAC_TYPE &&
1177 be_to_host16(tlv->length) == 2 &&
1178 be_to_host16(tlv->pac_type) == type;
1179 }
1180
1181
1182 static void eap_fast_process_phase2_tlvs(struct eap_sm *sm,
1183 struct eap_fast_data *data,
1184 u8 *in_data, size_t in_len)
1185 {
1186 struct eap_fast_tlv_parse tlv;
1187 int check_crypto_binding = data->state == CRYPTO_BINDING;
1188
1189 if (eap_fast_parse_tlvs(in_data, in_len, &tlv) < 0) {
1190 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to parse received "
1191 "Phase 2 TLVs");
1192 return;
1193 }
1194
1195 if (tlv.result == EAP_TLV_RESULT_FAILURE) {
1196 wpa_printf(MSG_DEBUG, "EAP-FAST: Result TLV indicated "
1197 "failure");
1198 eap_fast_state(data, FAILURE);
1199 return;
1200 }
1201
1202 if (data->state == REQUEST_PAC) {
1203 u16 type, len, res;
1204 if (tlv.pac == NULL || tlv.pac_len < 6) {
1205 wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC "
1206 "Acknowledgement received");
1207 eap_fast_state(data, FAILURE);
1208 return;
1209 }
1210
1211 type = WPA_GET_BE16(tlv.pac);
1212 len = WPA_GET_BE16(tlv.pac + 2);
1213 res = WPA_GET_BE16(tlv.pac + 4);
1214
1215 if (type != PAC_TYPE_PAC_ACKNOWLEDGEMENT || len != 2 ||
1216 res != EAP_TLV_RESULT_SUCCESS) {
1217 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV did not "
1218 "contain acknowledgement");
1219 eap_fast_state(data, FAILURE);
1220 return;
1221 }
1222
1223 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Acknowledgement received "
1224 "- PAC provisioning succeeded");
1225 eap_fast_state(data, data->anon_provisioning ?
1226 FAILURE : SUCCESS);
1227 return;
1228 }
1229
1230 if (check_crypto_binding) {
1231 if (tlv.crypto_binding == NULL) {
1232 wpa_printf(MSG_DEBUG, "EAP-FAST: No Crypto-Binding "
1233 "TLV received");
1234 eap_fast_state(data, FAILURE);
1235 return;
1236 }
1237
1238 if (data->final_result &&
1239 tlv.result != EAP_TLV_RESULT_SUCCESS) {
1240 wpa_printf(MSG_DEBUG, "EAP-FAST: Crypto-Binding TLV "
1241 "without Success Result");
1242 eap_fast_state(data, FAILURE);
1243 return;
1244 }
1245
1246 if (!data->final_result &&
1247 tlv.iresult != EAP_TLV_RESULT_SUCCESS) {
1248 wpa_printf(MSG_DEBUG, "EAP-FAST: Crypto-Binding TLV "
1249 "without intermediate Success Result");
1250 eap_fast_state(data, FAILURE);
1251 return;
1252 }
1253
1254 if (eap_fast_validate_crypto_binding(data, tlv.crypto_binding,
1255 tlv.crypto_binding_len)) {
1256 eap_fast_state(data, FAILURE);
1257 return;
1258 }
1259
1260 wpa_printf(MSG_DEBUG, "EAP-FAST: Valid Crypto-Binding TLV "
1261 "received");
1262 if (data->final_result) {
1263 wpa_printf(MSG_DEBUG, "EAP-FAST: Authentication "
1264 "completed successfully");
1265 }
1266
1267 if (data->anon_provisioning ||
1268 (tlv.request_action == EAP_TLV_ACTION_PROCESS_TLV &&
1269 eap_fast_pac_type(tlv.pac, tlv.pac_len,
1270 PAC_TYPE_TUNNEL_PAC))) {
1271 wpa_printf(MSG_DEBUG, "EAP-FAST: Requested a new "
1272 "Tunnel PAC");
1273 eap_fast_state(data, REQUEST_PAC);
1274 } else if (data->send_new_pac) {
1275 wpa_printf(MSG_DEBUG, "EAP-FAST: Server triggered "
1276 "re-keying of Tunnel PAC");
1277 eap_fast_state(data, REQUEST_PAC);
1278 } else if (data->final_result)
1279 eap_fast_state(data, SUCCESS);
1280 }
1281
1282 if (tlv.eap_payload_tlv) {
1283 eap_fast_process_phase2_eap(sm, data, tlv.eap_payload_tlv,
1284 tlv.eap_payload_tlv_len);
1285 }
1286 }
1287
1288
1289 static void eap_fast_process_phase2(struct eap_sm *sm,
1290 struct eap_fast_data *data,
1291 const u8 *in_data, size_t in_len)
1292 {
1293 u8 *in_decrypted;
1294 int len_decrypted, res;
1295 size_t buf_len;
1296
1297 wpa_printf(MSG_DEBUG, "EAP-FAST: Received %lu bytes encrypted data for"
1298 " Phase 2", (unsigned long) in_len);
1299
1300 if (data->pending_phase2_resp) {
1301 wpa_printf(MSG_DEBUG, "EAP-PEAP: Pending Phase 2 response - "
1302 "skip decryption and use old data");
1303 eap_fast_process_phase2_tlvs(
1304 sm, data, wpabuf_mhead(data->pending_phase2_resp),
1305 wpabuf_len(data->pending_phase2_resp));
1306 wpabuf_free(data->pending_phase2_resp);
1307 data->pending_phase2_resp = NULL;
1308 return;
1309 }
1310
1311 /* FIX: get rid of const -> non-const typecast */
1312 res = eap_server_tls_data_reassemble(sm, &data->ssl, (u8 **) &in_data,
1313 &in_len);
1314 if (res < 0 || res == 1)
1315 return;
1316
1317 buf_len = in_len;
1318 if (data->ssl.tls_in_total > buf_len)
1319 buf_len = data->ssl.tls_in_total;
1320 in_decrypted = os_malloc(buf_len);
1321 if (in_decrypted == NULL) {
1322 os_free(data->ssl.tls_in);
1323 data->ssl.tls_in = NULL;
1324 data->ssl.tls_in_len = 0;
1325 wpa_printf(MSG_WARNING, "EAP-FAST: Failed to allocate memory "
1326 "for decryption");
1327 return;
1328 }
1329
1330 len_decrypted = tls_connection_decrypt(sm->ssl_ctx, data->ssl.conn,
1331 in_data, in_len,
1332 in_decrypted, buf_len);
1333 os_free(data->ssl.tls_in);
1334 data->ssl.tls_in = NULL;
1335 data->ssl.tls_in_len = 0;
1336 if (len_decrypted < 0) {
1337 wpa_printf(MSG_INFO, "EAP-FAST: Failed to decrypt Phase 2 "
1338 "data");
1339 os_free(in_decrypted);
1340 eap_fast_state(data, FAILURE);
1341 return;
1342 }
1343
1344 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: Decrypted Phase 2 TLVs",
1345 in_decrypted, len_decrypted);
1346
1347 eap_fast_process_phase2_tlvs(sm, data, in_decrypted, len_decrypted);
1348
1349 if (sm->method_pending == METHOD_PENDING_WAIT) {
1350 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase2 method is in "
1351 "pending wait state - save decrypted response");
1352 wpabuf_free(data->pending_phase2_resp);
1353 data->pending_phase2_resp = wpabuf_alloc_copy(in_decrypted,
1354 len_decrypted);
1355 }
1356
1357 os_free(in_decrypted);
1358 }
1359
1360
1361 static int eap_fast_process_version(struct eap_fast_data *data,
1362 int peer_version)
1363 {
1364 data->peer_version = peer_version;
1365
1366 if (data->force_version >= 0 && peer_version != data->force_version) {
1367 wpa_printf(MSG_INFO, "EAP-FAST: peer did not select the forced"
1368 " version (forced=%d peer=%d) - reject",
1369 data->force_version, peer_version);
1370 eap_fast_state(data, FAILURE);
1371 return -1;
1372 }
1373
1374 if (peer_version < data->fast_version) {
1375 wpa_printf(MSG_DEBUG, "EAP-FAST: peer ver=%d, own ver=%d; "
1376 "use version %d",
1377 peer_version, data->fast_version, peer_version);
1378 data->fast_version = peer_version;
1379 }
1380
1381 return 0;
1382 }
1383
1384
1385 static int eap_fast_process_length(struct eap_fast_data *data,
1386 const u8 **pos, size_t *left)
1387 {
1388 u32 tls_msg_len;
1389
1390 if (*left < 4) {
1391 wpa_printf(MSG_INFO, "EAP-FAST: Short frame with TLS "
1392 "length");
1393 eap_fast_state(data, FAILURE);
1394 return -1;
1395 }
1396
1397 tls_msg_len = WPA_GET_BE32(*pos);
1398 wpa_printf(MSG_DEBUG, "EAP-FAST: TLS Message Length: %d",
1399 tls_msg_len);
1400
1401 if (data->ssl.tls_in_left == 0) {
1402 data->ssl.tls_in_total = tls_msg_len;
1403 data->ssl.tls_in_left = tls_msg_len;
1404 os_free(data->ssl.tls_in);
1405 data->ssl.tls_in = NULL;
1406 data->ssl.tls_in_len = 0;
1407 }
1408
1409 *pos += 4;
1410 *left -= 4;
1411
1412 return 0;
1413 }
1414
1415
1416 static int eap_fast_process_phase1(struct eap_sm *sm,
1417 struct eap_fast_data *data,
1418 const u8 *pos, size_t left)
1419 {
1420 if (eap_server_tls_process_helper(sm, &data->ssl, pos, left) < 0) {
1421 wpa_printf(MSG_INFO, "EAP-FAST: TLS processing failed");
1422 eap_fast_state(data, FAILURE);
1423 return -1;
1424 }
1425
1426 if (!tls_connection_established(sm->ssl_ctx, data->ssl.conn) ||
1427 data->ssl.tls_out_len > 0)
1428 return 1;
1429
1430 /*
1431 * Phase 1 was completed with the received message (e.g., when using
1432 * abbreviated handshake), so Phase 2 can be started immediately
1433 * without having to send through an empty message to the peer.
1434 */
1435
1436 return eap_fast_phase1_done(sm, data);
1437 }
1438
1439
1440 static void eap_fast_process_phase2_start(struct eap_sm *sm,
1441 struct eap_fast_data *data)
1442 {
1443 u8 next_type;
1444
1445 if (data->identity) {
1446 os_free(sm->identity);
1447 sm->identity = data->identity;
1448 data->identity = NULL;
1449 sm->identity_len = data->identity_len;
1450 data->identity_len = 0;
1451 if (eap_user_get(sm, sm->identity, sm->identity_len, 1) != 0) {
1452 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: "
1453 "Phase2 Identity not found "
1454 "in the user database",
1455 sm->identity, sm->identity_len);
1456 next_type = eap_fast_req_failure(sm, data);
1457 } else {
1458 wpa_printf(MSG_DEBUG, "EAP-FAST: Identity already "
1459 "known - skip Phase 2 Identity Request");
1460 next_type = sm->user->methods[0].method;
1461 sm->user_eap_method_index = 1;
1462 }
1463
1464 eap_fast_state(data, PHASE2_METHOD);
1465 } else {
1466 eap_fast_state(data, PHASE2_ID);
1467 next_type = EAP_TYPE_IDENTITY;
1468 }
1469
1470 eap_fast_phase2_init(sm, data, next_type);
1471 }
1472
1473
1474 static void eap_fast_process(struct eap_sm *sm, void *priv,
1475 struct wpabuf *respData)
1476 {
1477 struct eap_fast_data *data = priv;
1478 const u8 *pos;
1479 u8 flags;
1480 size_t left;
1481
1482 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_FAST, respData,
1483 &left);
1484 if (pos == NULL || left < 1)
1485 return;
1486 flags = *pos++;
1487 left--;
1488 wpa_printf(MSG_DEBUG, "EAP-FAST: Received packet(len=%lu) - "
1489 "Flags 0x%02x", (unsigned long) wpabuf_len(respData),
1490 flags);
1491
1492 if (eap_fast_process_version(data, flags & EAP_PEAP_VERSION_MASK))
1493 return;
1494
1495 if ((flags & EAP_TLS_FLAGS_LENGTH_INCLUDED) &&
1496 eap_fast_process_length(data, &pos, &left))
1497 return;
1498
1499 switch (data->state) {
1500 case PHASE1:
1501 if (eap_fast_process_phase1(sm, data, pos, left))
1502 break;
1503
1504 /* fall through to PHASE2_START */
1505 case PHASE2_START:
1506 eap_fast_process_phase2_start(sm, data);
1507 break;
1508 case PHASE2_ID:
1509 case PHASE2_METHOD:
1510 case CRYPTO_BINDING:
1511 case REQUEST_PAC:
1512 eap_fast_process_phase2(sm, data, pos, left);
1513 break;
1514 default:
1515 wpa_printf(MSG_DEBUG, "EAP-FAST: Unexpected state %d in %s",
1516 data->state, __func__);
1517 break;
1518 }
1519
1520 if (tls_connection_get_write_alerts(sm->ssl_ctx, data->ssl.conn) > 1) {
1521 wpa_printf(MSG_INFO, "EAP-FAST: Locally detected fatal error "
1522 "in TLS processing");
1523 eap_fast_state(data, FAILURE);
1524 }
1525 }
1526
1527
1528 static Boolean eap_fast_isDone(struct eap_sm *sm, void *priv)
1529 {
1530 struct eap_fast_data *data = priv;
1531 return data->state == SUCCESS || data->state == FAILURE;
1532 }
1533
1534
1535 static u8 * eap_fast_getKey(struct eap_sm *sm, void *priv, size_t *len)
1536 {
1537 struct eap_fast_data *data = priv;
1538 u8 *eapKeyData;
1539
1540 if (data->state != SUCCESS)
1541 return NULL;
1542
1543 eapKeyData = os_malloc(EAP_FAST_KEY_LEN);
1544 if (eapKeyData == NULL)
1545 return NULL;
1546
1547 eap_fast_derive_eap_msk(data->simck, eapKeyData);
1548 *len = EAP_FAST_KEY_LEN;
1549
1550 return eapKeyData;
1551 }
1552
1553
1554 static u8 * eap_fast_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
1555 {
1556 struct eap_fast_data *data = priv;
1557 u8 *eapKeyData;
1558
1559 if (data->state != SUCCESS)
1560 return NULL;
1561
1562 eapKeyData = os_malloc(EAP_EMSK_LEN);
1563 if (eapKeyData == NULL)
1564 return NULL;
1565
1566 eap_fast_derive_eap_emsk(data->simck, eapKeyData);
1567 *len = EAP_EMSK_LEN;
1568
1569 return eapKeyData;
1570 }
1571
1572
1573 static Boolean eap_fast_isSuccess(struct eap_sm *sm, void *priv)
1574 {
1575 struct eap_fast_data *data = priv;
1576 return data->state == SUCCESS;
1577 }
1578
1579
1580 int eap_server_fast_register(void)
1581 {
1582 struct eap_method *eap;
1583 int ret;
1584
1585 eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
1586 EAP_VENDOR_IETF, EAP_TYPE_FAST, "FAST");
1587 if (eap == NULL)
1588 return -1;
1589
1590 eap->init = eap_fast_init;
1591 eap->reset = eap_fast_reset;
1592 eap->buildReq = eap_fast_buildReq;
1593 eap->check = eap_fast_check;
1594 eap->process = eap_fast_process;
1595 eap->isDone = eap_fast_isDone;
1596 eap->getKey = eap_fast_getKey;
1597 eap->get_emsk = eap_fast_get_emsk;
1598 eap->isSuccess = eap_fast_isSuccess;
1599
1600 ret = eap_server_method_register(eap);
1601 if (ret)
1602 eap_server_method_free(eap);
1603 return ret;
1604 }