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