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