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