]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - contrib/wpa/src/eap_peer/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_peer / eap_fast.c
1 /*
2  * EAP peer method: EAP-FAST (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 "eap_i.h"
19 #include "eap_tls_common.h"
20 #include "eap_config.h"
21 #include "tls.h"
22 #include "eap_common/eap_tlv_common.h"
23 #include "sha1.h"
24 #include "eap_fast_pac.h"
25
26 #ifdef EAP_FAST_DYNAMIC
27 #include "eap_fast_pac.c"
28 #endif /* EAP_FAST_DYNAMIC */
29
30 /* TODO:
31  * - test session resumption and enable it if it interoperates
32  * - password change (pending mschapv2 packet; replay decrypted packet)
33  */
34
35
36 static void eap_fast_deinit(struct eap_sm *sm, void *priv);
37
38
39 struct eap_fast_data {
40         struct eap_ssl_data ssl;
41
42         int fast_version;
43
44         const struct eap_method *phase2_method;
45         void *phase2_priv;
46         int phase2_success;
47
48         struct eap_method_type phase2_type;
49         struct eap_method_type *phase2_types;
50         size_t num_phase2_types;
51         int resuming; /* starting a resumed session */
52         struct eap_fast_key_block_provisioning *key_block_p;
53 #define EAP_FAST_PROV_UNAUTH 1
54 #define EAP_FAST_PROV_AUTH 2
55         int provisioning_allowed; /* Allowed PAC provisioning modes */
56         int provisioning; /* doing PAC provisioning (not the normal auth) */
57         int anon_provisioning; /* doing anonymous (unauthenticated)
58                                 * provisioning */
59         int session_ticket_used;
60
61         u8 key_data[EAP_FAST_KEY_LEN];
62         u8 emsk[EAP_EMSK_LEN];
63         int success;
64
65         struct eap_fast_pac *pac;
66         struct eap_fast_pac *current_pac;
67         size_t max_pac_list_len;
68         int use_pac_binary_format;
69
70         u8 simck[EAP_FAST_SIMCK_LEN];
71         int simck_idx;
72
73         struct wpabuf *pending_phase2_req;
74 };
75
76
77 static int eap_fast_session_ticket_cb(void *ctx, const u8 *ticket, size_t len,
78                                       const u8 *client_random,
79                                       const u8 *server_random,
80                                       u8 *master_secret)
81 {
82         struct eap_fast_data *data = ctx;
83
84         wpa_printf(MSG_DEBUG, "EAP-FAST: SessionTicket callback");
85
86         if (client_random == NULL || server_random == NULL ||
87             master_secret == NULL) {
88                 wpa_printf(MSG_DEBUG, "EAP-FAST: SessionTicket failed - fall "
89                            "back to full TLS handshake");
90                 data->session_ticket_used = 0;
91                 if (data->provisioning_allowed) {
92                         wpa_printf(MSG_DEBUG, "EAP-FAST: Try to provision a "
93                                    "new PAC-Key");
94                         data->provisioning = 1;
95                         data->current_pac = NULL;
96                 }
97                 return 0;
98         }
99
100         wpa_hexdump(MSG_DEBUG, "EAP-FAST: SessionTicket", ticket, len);
101
102         if (data->current_pac == NULL) {
103                 wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC-Key available for "
104                            "using SessionTicket");
105                 data->session_ticket_used = 0;
106                 return 0;
107         }
108
109         eap_fast_derive_master_secret(data->current_pac->pac_key,
110                                       server_random, client_random,
111                                       master_secret);
112
113         data->session_ticket_used = 1;
114
115         return 1;
116 }
117
118
119 static int eap_fast_parse_phase1(struct eap_fast_data *data,
120                                  const char *phase1)
121 {
122         const char *pos;
123
124         pos = os_strstr(phase1, "fast_provisioning=");
125         if (pos) {
126                 data->provisioning_allowed = atoi(pos + 18);
127                 wpa_printf(MSG_DEBUG, "EAP-FAST: Automatic PAC provisioning "
128                            "mode: %d", data->provisioning_allowed);
129         }
130
131         pos = os_strstr(phase1, "fast_max_pac_list_len=");
132         if (pos) {
133                 data->max_pac_list_len = atoi(pos + 22);
134                 if (data->max_pac_list_len == 0)
135                         data->max_pac_list_len = 1;
136                 wpa_printf(MSG_DEBUG, "EAP-FAST: Maximum PAC list length: %lu",
137                            (unsigned long) data->max_pac_list_len);
138         }
139
140         pos = os_strstr(phase1, "fast_pac_format=binary");
141         if (pos) {
142                 data->use_pac_binary_format = 1;
143                 wpa_printf(MSG_DEBUG, "EAP-FAST: Using binary format for PAC "
144                            "list");
145         }
146
147         return 0;
148 }
149
150
151 static void * eap_fast_init(struct eap_sm *sm)
152 {
153         struct eap_fast_data *data;
154         struct eap_peer_config *config = eap_get_config(sm);
155
156         data = os_zalloc(sizeof(*data));
157         if (data == NULL)
158                 return NULL;
159         data->fast_version = EAP_FAST_VERSION;
160         data->max_pac_list_len = 10;
161
162         if (config && config->phase1 &&
163             eap_fast_parse_phase1(data, config->phase1) < 0) {
164                 eap_fast_deinit(sm, data);
165                 return NULL;
166         }
167
168         if (eap_peer_select_phase2_methods(config, "auth=",
169                                            &data->phase2_types,
170                                            &data->num_phase2_types) < 0) {
171                 eap_fast_deinit(sm, data);
172                 return NULL;
173         }
174
175         data->phase2_type.vendor = EAP_VENDOR_IETF;
176         data->phase2_type.method = EAP_TYPE_NONE;
177
178         if (eap_peer_tls_ssl_init(sm, &data->ssl, config)) {
179                 wpa_printf(MSG_INFO, "EAP-FAST: Failed to initialize SSL.");
180                 eap_fast_deinit(sm, data);
181                 return NULL;
182         }
183
184         if (tls_connection_set_session_ticket_cb(sm->ssl_ctx, data->ssl.conn,
185                                                  eap_fast_session_ticket_cb,
186                                                  data) < 0) {
187                 wpa_printf(MSG_INFO, "EAP-FAST: Failed to set SessionTicket "
188                            "callback");
189                 eap_fast_deinit(sm, data);
190                 return NULL;
191         }
192
193         /*
194          * The local RADIUS server in a Cisco AP does not seem to like empty
195          * fragments before data, so disable that workaround for CBC.
196          * TODO: consider making this configurable
197          */
198         if (tls_connection_enable_workaround(sm->ssl_ctx, data->ssl.conn)) {
199                 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to enable TLS "
200                            "workarounds");
201         }
202
203         if (data->use_pac_binary_format &&
204             eap_fast_load_pac_bin(sm, &data->pac, config->pac_file) < 0) {
205                 eap_fast_deinit(sm, data);
206                 return NULL;
207         }
208
209         if (!data->use_pac_binary_format &&
210             eap_fast_load_pac(sm, &data->pac, config->pac_file) < 0) {
211                 eap_fast_deinit(sm, data);
212                 return NULL;
213         }
214         eap_fast_pac_list_truncate(data->pac, data->max_pac_list_len);
215
216         if (data->pac == NULL && !data->provisioning_allowed) {
217                 wpa_printf(MSG_INFO, "EAP-FAST: No PAC configured and "
218                            "provisioning disabled");
219                 eap_fast_deinit(sm, data);
220                 return NULL;
221         }
222
223         return data;
224 }
225
226
227 static void eap_fast_deinit(struct eap_sm *sm, void *priv)
228 {
229         struct eap_fast_data *data = priv;
230         struct eap_fast_pac *pac, *prev;
231
232         if (data == NULL)
233                 return;
234         if (data->phase2_priv && data->phase2_method)
235                 data->phase2_method->deinit(sm, data->phase2_priv);
236         os_free(data->phase2_types);
237         os_free(data->key_block_p);
238         eap_peer_tls_ssl_deinit(sm, &data->ssl);
239
240         pac = data->pac;
241         prev = NULL;
242         while (pac) {
243                 prev = pac;
244                 pac = pac->next;
245                 eap_fast_free_pac(prev);
246         }
247         wpabuf_free(data->pending_phase2_req);
248         os_free(data);
249 }
250
251
252 static int eap_fast_derive_msk(struct eap_fast_data *data)
253 {
254         eap_fast_derive_eap_msk(data->simck, data->key_data);
255         eap_fast_derive_eap_emsk(data->simck, data->emsk);
256         data->success = 1;
257         return 0;
258 }
259
260
261 static void eap_fast_derive_key_auth(struct eap_sm *sm,
262                                      struct eap_fast_data *data)
263 {
264         u8 *sks;
265
266         /* RFC 4851, Section 5.1:
267          * Extra key material after TLS key_block: session_key_seed[40]
268          */
269
270         sks = eap_fast_derive_key(sm->ssl_ctx, data->ssl.conn, "key expansion",
271                                   EAP_FAST_SKS_LEN);
272         if (sks == NULL) {
273                 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive "
274                            "session_key_seed");
275                 return;
276         }
277
278         /*
279          * RFC 4851, Section 5.2:
280          * S-IMCK[0] = session_key_seed
281          */
282         wpa_hexdump_key(MSG_DEBUG,
283                         "EAP-FAST: session_key_seed (SKS = S-IMCK[0])",
284                         sks, EAP_FAST_SKS_LEN);
285         data->simck_idx = 0;
286         os_memcpy(data->simck, sks, EAP_FAST_SIMCK_LEN);
287         os_free(sks);
288 }
289
290
291 static void eap_fast_derive_key_provisioning(struct eap_sm *sm,
292                                              struct eap_fast_data *data)
293 {
294         os_free(data->key_block_p);
295         data->key_block_p = (struct eap_fast_key_block_provisioning *)
296                 eap_fast_derive_key(sm->ssl_ctx, data->ssl.conn,
297                                     "key expansion",
298                                     sizeof(*data->key_block_p));
299         if (data->key_block_p == NULL) {
300                 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive key block");
301                 return;
302         }
303         /*
304          * RFC 4851, Section 5.2:
305          * S-IMCK[0] = session_key_seed
306          */
307         wpa_hexdump_key(MSG_DEBUG,
308                         "EAP-FAST: session_key_seed (SKS = S-IMCK[0])",
309                         data->key_block_p->session_key_seed,
310                         sizeof(data->key_block_p->session_key_seed));
311         data->simck_idx = 0;
312         os_memcpy(data->simck, data->key_block_p->session_key_seed,
313                   EAP_FAST_SIMCK_LEN);
314         wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: server_challenge",
315                         data->key_block_p->server_challenge,
316                         sizeof(data->key_block_p->server_challenge));
317         wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: client_challenge",
318                         data->key_block_p->client_challenge,
319                         sizeof(data->key_block_p->client_challenge));
320 }
321
322
323 static void eap_fast_derive_keys(struct eap_sm *sm, struct eap_fast_data *data)
324 {
325         if (data->anon_provisioning)
326                 eap_fast_derive_key_provisioning(sm, data);
327         else
328                 eap_fast_derive_key_auth(sm, data);
329 }
330
331
332 static int eap_fast_init_phase2_method(struct eap_sm *sm,
333                                        struct eap_fast_data *data)
334 {
335         data->phase2_method =
336                 eap_peer_get_eap_method(data->phase2_type.vendor,
337                                         data->phase2_type.method);
338         if (data->phase2_method == NULL)
339                 return -1;
340
341         if (data->key_block_p) {
342                 sm->auth_challenge = data->key_block_p->server_challenge;
343                 sm->peer_challenge = data->key_block_p->client_challenge;
344         }
345         sm->init_phase2 = 1;
346         data->phase2_priv = data->phase2_method->init(sm);
347         sm->init_phase2 = 0;
348         sm->auth_challenge = NULL;
349         sm->peer_challenge = NULL;
350
351         return data->phase2_priv == NULL ? -1 : 0;
352 }
353
354
355 static int eap_fast_select_phase2_method(struct eap_fast_data *data, u8 type)
356 {
357         size_t i;
358
359         /* TODO: TNC with anonymous provisioning; need to require both
360          * completed MSCHAPv2 and TNC */
361
362         if (data->anon_provisioning && type != EAP_TYPE_MSCHAPV2) {
363                 wpa_printf(MSG_INFO, "EAP-FAST: Only EAP-MSCHAPv2 is allowed "
364                            "during unauthenticated provisioning; reject phase2"
365                            " type %d", type);
366                 return -1;
367         }
368
369 #ifdef EAP_TNC
370         if (type == EAP_TYPE_TNC) {
371                 data->phase2_type.vendor = EAP_VENDOR_IETF;
372                 data->phase2_type.method = EAP_TYPE_TNC;
373                 wpa_printf(MSG_DEBUG, "EAP-FAST: Selected Phase 2 EAP "
374                            "vendor %d method %d for TNC",
375                            data->phase2_type.vendor,
376                            data->phase2_type.method);
377                 return 0;
378         }
379 #endif /* EAP_TNC */
380
381         for (i = 0; i < data->num_phase2_types; i++) {
382                 if (data->phase2_types[i].vendor != EAP_VENDOR_IETF ||
383                     data->phase2_types[i].method != type)
384                         continue;
385
386                 data->phase2_type.vendor = data->phase2_types[i].vendor;
387                 data->phase2_type.method = data->phase2_types[i].method;
388                 wpa_printf(MSG_DEBUG, "EAP-FAST: Selected Phase 2 EAP "
389                            "vendor %d method %d",
390                            data->phase2_type.vendor,
391                            data->phase2_type.method);
392                 break;
393         }
394
395         if (type != data->phase2_type.method || type == EAP_TYPE_NONE)
396                 return -1;
397
398         return 0;
399 }
400
401
402 static int eap_fast_phase2_request(struct eap_sm *sm,
403                                    struct eap_fast_data *data,
404                                    struct eap_method_ret *ret,
405                                    struct eap_hdr *hdr,
406                                    struct wpabuf **resp)
407 {
408         size_t len = be_to_host16(hdr->length);
409         u8 *pos;
410         struct eap_method_ret iret;
411         struct eap_peer_config *config = eap_get_config(sm);
412         struct wpabuf msg;
413
414         if (len <= sizeof(struct eap_hdr)) {
415                 wpa_printf(MSG_INFO, "EAP-FAST: too short "
416                            "Phase 2 request (len=%lu)", (unsigned long) len);
417                 return -1;
418         }
419         pos = (u8 *) (hdr + 1);
420         wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 Request: type=%d", *pos);
421         if (*pos == EAP_TYPE_IDENTITY) {
422                 *resp = eap_sm_buildIdentity(sm, hdr->identifier, 1);
423                 return 0;
424         }
425
426         if (data->phase2_priv && data->phase2_method &&
427             *pos != data->phase2_type.method) {
428                 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 EAP sequence - "
429                            "deinitialize previous method");
430                 data->phase2_method->deinit(sm, data->phase2_priv);
431                 data->phase2_method = NULL;
432                 data->phase2_priv = NULL;
433                 data->phase2_type.vendor = EAP_VENDOR_IETF;
434                 data->phase2_type.method = EAP_TYPE_NONE;
435         }
436
437         if (data->phase2_type.vendor == EAP_VENDOR_IETF &&
438             data->phase2_type.method == EAP_TYPE_NONE &&
439             eap_fast_select_phase2_method(data, *pos) < 0) {
440                 if (eap_peer_tls_phase2_nak(data->phase2_types,
441                                             data->num_phase2_types,
442                                             hdr, resp))
443                         return -1;
444                 return 0;
445         }
446
447         if (data->phase2_priv == NULL &&
448             eap_fast_init_phase2_method(sm, data) < 0) {
449                 wpa_printf(MSG_INFO, "EAP-FAST: Failed to initialize "
450                            "Phase 2 EAP method %d", *pos);
451                 ret->methodState = METHOD_DONE;
452                 ret->decision = DECISION_FAIL;
453                 return -1;
454         }
455
456         os_memset(&iret, 0, sizeof(iret));
457         wpabuf_set(&msg, hdr, len);
458         *resp = data->phase2_method->process(sm, data->phase2_priv, &iret,
459                                              &msg);
460         if (*resp == NULL ||
461             (iret.methodState == METHOD_DONE &&
462              iret.decision == DECISION_FAIL)) {
463                 ret->methodState = METHOD_DONE;
464                 ret->decision = DECISION_FAIL;
465         } else if ((iret.methodState == METHOD_DONE ||
466                     iret.methodState == METHOD_MAY_CONT) &&
467                    (iret.decision == DECISION_UNCOND_SUCC ||
468                     iret.decision == DECISION_COND_SUCC)) {
469                 data->phase2_success = 1;
470         }
471
472         if (*resp == NULL && config &&
473             (config->pending_req_identity || config->pending_req_password ||
474              config->pending_req_otp || config->pending_req_new_password)) {
475                 wpabuf_free(data->pending_phase2_req);
476                 data->pending_phase2_req = wpabuf_alloc_copy(hdr, len);
477         } else if (*resp == NULL)
478                 return -1;
479
480         return 0;
481 }
482
483
484 static struct wpabuf * eap_fast_tlv_nak(int vendor_id, int tlv_type)
485 {
486         struct wpabuf *buf;
487         struct eap_tlv_nak_tlv *nak;
488         buf = wpabuf_alloc(sizeof(*nak));
489         if (buf == NULL)
490                 return NULL;
491         nak = wpabuf_put(buf, sizeof(*nak));
492         nak->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY | EAP_TLV_NAK_TLV);
493         nak->length = host_to_be16(6);
494         nak->vendor_id = host_to_be32(vendor_id);
495         nak->nak_type = host_to_be16(tlv_type);
496         return buf;
497 }
498
499
500 static struct wpabuf * eap_fast_tlv_result(int status, int intermediate)
501 {
502         struct wpabuf *buf;
503         struct eap_tlv_intermediate_result_tlv *result;
504         buf = wpabuf_alloc(sizeof(*result));
505         if (buf == NULL)
506                 return NULL;
507         wpa_printf(MSG_DEBUG, "EAP-FAST: Add %sResult TLV(status=%d)",
508                    intermediate ? "Intermediate " : "", status);
509         result = wpabuf_put(buf, sizeof(*result));
510         result->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
511                                         (intermediate ?
512                                          EAP_TLV_INTERMEDIATE_RESULT_TLV :
513                                          EAP_TLV_RESULT_TLV));
514         result->length = host_to_be16(2);
515         result->status = host_to_be16(status);
516         return buf;
517 }
518
519
520 static struct wpabuf * eap_fast_tlv_pac_ack(void)
521 {
522         struct wpabuf *buf;
523         struct eap_tlv_result_tlv *res;
524         struct eap_tlv_pac_ack_tlv *ack;
525
526         buf = wpabuf_alloc(sizeof(*res) + sizeof(*ack));
527         if (buf == NULL)
528                 return NULL;
529
530         wpa_printf(MSG_DEBUG, "EAP-FAST: Add PAC TLV (ack)");
531         ack = wpabuf_put(buf, sizeof(*ack));
532         ack->tlv_type = host_to_be16(EAP_TLV_PAC_TLV |
533                                      EAP_TLV_TYPE_MANDATORY);
534         ack->length = host_to_be16(sizeof(*ack) - sizeof(struct eap_tlv_hdr));
535         ack->pac_type = host_to_be16(PAC_TYPE_PAC_ACKNOWLEDGEMENT);
536         ack->pac_len = host_to_be16(2);
537         ack->result = host_to_be16(EAP_TLV_RESULT_SUCCESS);
538
539         return buf;
540 }
541
542
543 static struct wpabuf * eap_fast_process_eap_payload_tlv(
544         struct eap_sm *sm, struct eap_fast_data *data,
545         struct eap_method_ret *ret, const struct eap_hdr *req,
546         u8 *eap_payload_tlv, size_t eap_payload_tlv_len)
547 {
548         struct eap_hdr *hdr;
549         struct wpabuf *resp = NULL;
550
551         if (eap_payload_tlv_len < sizeof(*hdr)) {
552                 wpa_printf(MSG_DEBUG, "EAP-FAST: too short EAP "
553                            "Payload TLV (len=%lu)",
554                            (unsigned long) eap_payload_tlv_len);
555                 return NULL;
556         }
557
558         hdr = (struct eap_hdr *) eap_payload_tlv;
559         if (be_to_host16(hdr->length) > eap_payload_tlv_len) {
560                 wpa_printf(MSG_DEBUG, "EAP-FAST: EAP packet overflow in "
561                            "EAP Payload TLV");
562                 return NULL;
563         }
564
565         if (hdr->code != EAP_CODE_REQUEST) {
566                 wpa_printf(MSG_INFO, "EAP-FAST: Unexpected code=%d in "
567                            "Phase 2 EAP header", hdr->code);
568                 return NULL;
569         }
570
571         if (eap_fast_phase2_request(sm, data, ret, hdr, &resp)) {
572                 wpa_printf(MSG_INFO, "EAP-FAST: Phase2 Request processing "
573                            "failed");
574                 return NULL;
575         }
576
577         return eap_fast_tlv_eap_payload(resp);
578 }
579
580
581 static int eap_fast_validate_crypto_binding(
582         struct eap_tlv_crypto_binding_tlv *_bind)
583 {
584         wpa_printf(MSG_DEBUG, "EAP-FAST: Crypto-Binding TLV: Version %d "
585                    "Received Version %d SubType %d",
586                    _bind->version, _bind->received_version, _bind->subtype);
587         wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE",
588                     _bind->nonce, sizeof(_bind->nonce));
589         wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC",
590                     _bind->compound_mac, sizeof(_bind->compound_mac));
591
592         if (_bind->version != EAP_FAST_VERSION ||
593             _bind->received_version != EAP_FAST_VERSION ||
594             _bind->subtype != EAP_TLV_CRYPTO_BINDING_SUBTYPE_REQUEST) {
595                 wpa_printf(MSG_INFO, "EAP-FAST: Invalid version/subtype in "
596                            "Crypto-Binding TLV: Version %d "
597                            "Received Version %d SubType %d",
598                            _bind->version, _bind->received_version,
599                            _bind->subtype);
600                 return -1;
601         }
602
603         return 0;
604 }
605
606
607 static void eap_fast_write_crypto_binding(
608         struct eap_tlv_crypto_binding_tlv *rbind,
609         struct eap_tlv_crypto_binding_tlv *_bind, const u8 *cmk)
610 {
611         rbind->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
612                                        EAP_TLV_CRYPTO_BINDING_TLV);
613         rbind->length = host_to_be16(sizeof(*rbind) -
614                                      sizeof(struct eap_tlv_hdr));
615         rbind->version = EAP_FAST_VERSION;
616         rbind->received_version = _bind->version;
617         rbind->subtype = EAP_TLV_CRYPTO_BINDING_SUBTYPE_RESPONSE;
618         os_memcpy(rbind->nonce, _bind->nonce, sizeof(_bind->nonce));
619         inc_byte_array(rbind->nonce, sizeof(rbind->nonce));
620         hmac_sha1(cmk, EAP_FAST_CMK_LEN, (u8 *) rbind, sizeof(*rbind),
621                   rbind->compound_mac);
622
623         wpa_printf(MSG_DEBUG, "EAP-FAST: Reply Crypto-Binding TLV: Version %d "
624                    "Received Version %d SubType %d",
625                    rbind->version, rbind->received_version, rbind->subtype);
626         wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE",
627                     rbind->nonce, sizeof(rbind->nonce));
628         wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC",
629                     rbind->compound_mac, sizeof(rbind->compound_mac));
630 }
631
632
633 static int eap_fast_get_phase2_key(struct eap_sm *sm,
634                                    struct eap_fast_data *data,
635                                    u8 *isk, size_t isk_len)
636 {
637         u8 *key;
638         size_t key_len;
639
640         os_memset(isk, 0, isk_len);
641
642         if (data->phase2_method == NULL || data->phase2_priv == NULL) {
643                 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 method not "
644                            "available");
645                 return -1;
646         }
647
648         if (data->phase2_method->isKeyAvailable == NULL ||
649             data->phase2_method->getKey == NULL)
650                 return 0;
651
652         if (!data->phase2_method->isKeyAvailable(sm, data->phase2_priv) ||
653             (key = data->phase2_method->getKey(sm, data->phase2_priv,
654                                                &key_len)) == NULL) {
655                 wpa_printf(MSG_DEBUG, "EAP-FAST: Could not get key material "
656                            "from Phase 2");
657                 return -1;
658         }
659
660         if (key_len > isk_len)
661                 key_len = isk_len;
662         if (key_len == 32 &&
663             data->phase2_method->vendor == EAP_VENDOR_IETF &&
664             data->phase2_method->method == EAP_TYPE_MSCHAPV2) {
665                 /*
666                  * EAP-FAST uses reverse order for MS-MPPE keys when deriving
667                  * MSK from EAP-MSCHAPv2. Swap the keys here to get the correct
668                  * ISK for EAP-FAST cryptobinding.
669                  */
670                 os_memcpy(isk, key + 16, 16);
671                 os_memcpy(isk + 16, key, 16);
672         } else
673                 os_memcpy(isk, key, key_len);
674         os_free(key);
675
676         return 0;
677 }
678
679
680 static int eap_fast_get_cmk(struct eap_sm *sm, struct eap_fast_data *data,
681                             u8 *cmk)
682 {
683         u8 isk[32], imck[60];
684
685         wpa_printf(MSG_DEBUG, "EAP-FAST: Determining CMK[%d] for Compound MIC "
686                    "calculation", data->simck_idx + 1);
687
688         /*
689          * RFC 4851, Section 5.2:
690          * IMCK[j] = T-PRF(S-IMCK[j-1], "Inner Methods Compound Keys",
691          *                 MSK[j], 60)
692          * S-IMCK[j] = first 40 octets of IMCK[j]
693          * CMK[j] = last 20 octets of IMCK[j]
694          */
695
696         if (eap_fast_get_phase2_key(sm, data, isk, sizeof(isk)) < 0)
697                 return -1;
698         wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: ISK[j]", isk, sizeof(isk));
699         sha1_t_prf(data->simck, EAP_FAST_SIMCK_LEN,
700                    "Inner Methods Compound Keys",
701                    isk, sizeof(isk), imck, sizeof(imck));
702         data->simck_idx++;
703         os_memcpy(data->simck, imck, EAP_FAST_SIMCK_LEN);
704         wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: S-IMCK[j]",
705                         data->simck, EAP_FAST_SIMCK_LEN);
706         os_memcpy(cmk, imck + EAP_FAST_SIMCK_LEN, EAP_FAST_CMK_LEN);
707         wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: CMK[j]",
708                         cmk, EAP_FAST_CMK_LEN);
709
710         return 0;
711 }
712
713
714 static u8 * eap_fast_write_pac_request(u8 *pos, u16 pac_type)
715 {
716         struct eap_tlv_hdr *pac;
717         struct eap_tlv_request_action_tlv *act;
718         struct eap_tlv_pac_type_tlv *type;
719
720         act = (struct eap_tlv_request_action_tlv *) pos;
721         act->tlv_type = host_to_be16(EAP_TLV_REQUEST_ACTION_TLV);
722         act->length = host_to_be16(2);
723         act->action = host_to_be16(EAP_TLV_ACTION_PROCESS_TLV);
724
725         pac = (struct eap_tlv_hdr *) (act + 1);
726         pac->tlv_type = host_to_be16(EAP_TLV_PAC_TLV);
727         pac->length = host_to_be16(sizeof(*type));
728
729         type = (struct eap_tlv_pac_type_tlv *) (pac + 1);
730         type->tlv_type = host_to_be16(PAC_TYPE_PAC_TYPE);
731         type->length = host_to_be16(2);
732         type->pac_type = host_to_be16(pac_type);
733
734         return (u8 *) (type + 1);
735 }
736
737
738 static struct wpabuf * eap_fast_process_crypto_binding(
739         struct eap_sm *sm, struct eap_fast_data *data,
740         struct eap_method_ret *ret,
741         struct eap_tlv_crypto_binding_tlv *_bind, size_t bind_len)
742 {
743         struct wpabuf *resp;
744         u8 *pos;
745         u8 cmk[EAP_FAST_CMK_LEN], cmac[SHA1_MAC_LEN];
746         int res;
747         size_t len;
748
749         if (eap_fast_validate_crypto_binding(_bind) < 0)
750                 return NULL;
751
752         if (eap_fast_get_cmk(sm, data, cmk) < 0)
753                 return NULL;
754
755         /* Validate received Compound MAC */
756         os_memcpy(cmac, _bind->compound_mac, sizeof(cmac));
757         os_memset(_bind->compound_mac, 0, sizeof(cmac));
758         wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Crypto-Binding TLV for Compound "
759                     "MAC calculation", (u8 *) _bind, bind_len);
760         hmac_sha1(cmk, EAP_FAST_CMK_LEN, (u8 *) _bind, bind_len,
761                   _bind->compound_mac);
762         res = os_memcmp(cmac, _bind->compound_mac, sizeof(cmac));
763         wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Received Compound MAC",
764                     cmac, sizeof(cmac));
765         wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Calculated Compound MAC",
766                     _bind->compound_mac, sizeof(cmac));
767         if (res != 0) {
768                 wpa_printf(MSG_INFO, "EAP-FAST: Compound MAC did not match");
769                 os_memcpy(_bind->compound_mac, cmac, sizeof(cmac));
770                 return NULL;
771         }
772
773         /*
774          * Compound MAC was valid, so authentication succeeded. Reply with
775          * crypto binding to allow server to complete authentication.
776          */
777
778         len = sizeof(struct eap_tlv_crypto_binding_tlv);
779         resp = wpabuf_alloc(len);
780         if (resp == NULL)
781                 return NULL;
782
783         if (!data->anon_provisioning && data->phase2_success &&
784             eap_fast_derive_msk(data) < 0) {
785                 wpa_printf(MSG_INFO, "EAP-FAST: Failed to generate MSK");
786                 ret->methodState = METHOD_DONE;
787                 ret->decision = DECISION_FAIL;
788                 data->phase2_success = 0;
789                 wpabuf_free(resp);
790                 return NULL;
791         }
792
793         pos = wpabuf_put(resp, sizeof(struct eap_tlv_crypto_binding_tlv));
794         eap_fast_write_crypto_binding((struct eap_tlv_crypto_binding_tlv *)
795                                       pos, _bind, cmk);
796
797         return resp;
798 }
799
800
801 static void eap_fast_parse_pac_tlv(struct eap_fast_pac *entry, int type,
802                                    u8 *pos, size_t len, int *pac_key_found)
803 {
804         switch (type & 0x7fff) {
805         case PAC_TYPE_PAC_KEY:
806                 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: PAC-Key", pos, len);
807                 if (len != EAP_FAST_PAC_KEY_LEN) {
808                         wpa_printf(MSG_DEBUG, "EAP-FAST: Invalid PAC-Key "
809                                    "length %lu", (unsigned long) len);
810                         break;
811                 }
812                 *pac_key_found = 1;
813                 os_memcpy(entry->pac_key, pos, len);
814                 break;
815         case PAC_TYPE_PAC_OPAQUE:
816                 wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Opaque", pos, len);
817                 entry->pac_opaque = pos;
818                 entry->pac_opaque_len = len;
819                 break;
820         case PAC_TYPE_PAC_INFO:
821                 wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Info", pos, len);
822                 entry->pac_info = pos;
823                 entry->pac_info_len = len;
824                 break;
825         default:
826                 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignored unknown PAC type %d",
827                            type);
828                 break;
829         }
830 }
831
832
833 static int eap_fast_process_pac_tlv(struct eap_fast_pac *entry,
834                                     u8 *pac, size_t pac_len)
835 {
836         struct pac_tlv_hdr *hdr;
837         u8 *pos;
838         size_t left, len;
839         int type, pac_key_found = 0;
840
841         pos = pac;
842         left = pac_len;
843
844         while (left > sizeof(*hdr)) {
845                 hdr = (struct pac_tlv_hdr *) pos;
846                 type = be_to_host16(hdr->type);
847                 len = be_to_host16(hdr->len);
848                 pos += sizeof(*hdr);
849                 left -= sizeof(*hdr);
850                 if (len > left) {
851                         wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV overrun "
852                                    "(type=%d len=%lu left=%lu)",
853                                    type, (unsigned long) len,
854                                    (unsigned long) left);
855                         return -1;
856                 }
857
858                 eap_fast_parse_pac_tlv(entry, type, pos, len, &pac_key_found);
859
860                 pos += len;
861                 left -= len;
862         }
863
864         if (!pac_key_found || !entry->pac_opaque || !entry->pac_info) {
865                 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV does not include "
866                            "all the required fields");
867                 return -1;
868         }
869
870         return 0;
871 }
872
873
874 static int eap_fast_parse_pac_info(struct eap_fast_pac *entry, int type,
875                                    u8 *pos, size_t len)
876 {
877         u16 pac_type;
878         u32 lifetime;
879         struct os_time now;
880
881         switch (type & 0x7fff) {
882         case PAC_TYPE_CRED_LIFETIME:
883                 if (len != 4) {
884                         wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Info - "
885                                     "Invalid CRED_LIFETIME length - ignored",
886                                     pos, len);
887                         return 0;
888                 }
889
890                 /*
891                  * This is not currently saved separately in PAC files since
892                  * the server can automatically initiate PAC update when
893                  * needed. Anyway, the information is available from PAC-Info
894                  * dump if it is needed for something in the future.
895                  */
896                 lifetime = WPA_GET_BE32(pos);
897                 os_get_time(&now);
898                 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info - CRED_LIFETIME %d "
899                            "(%d days)",
900                            lifetime, (lifetime - (u32) now.sec) / 86400);
901                 break;
902         case PAC_TYPE_A_ID:
903                 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - A-ID",
904                                   pos, len);
905                 entry->a_id = pos;
906                 entry->a_id_len = len;
907                 break;
908         case PAC_TYPE_I_ID:
909                 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - I-ID",
910                                   pos, len);
911                 entry->i_id = pos;
912                 entry->i_id_len = len;
913                 break;
914         case PAC_TYPE_A_ID_INFO:
915                 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - A-ID-Info",
916                                   pos, len);
917                 entry->a_id_info = pos;
918                 entry->a_id_info_len = len;
919                 break;
920         case PAC_TYPE_PAC_TYPE:
921                 /*
922                  * draft-cam-winget-eap-fast-provisioning-04.txt,
923                  * Section 4.2.6 - PAC-Type TLV
924                  */
925                 if (len != 2) {
926                         wpa_printf(MSG_INFO, "EAP-FAST: Invalid PAC-Type "
927                                    "length %lu (expected 2)",
928                                    (unsigned long) len);
929                         wpa_hexdump_ascii(MSG_DEBUG,
930                                           "EAP-FAST: PAC-Info - PAC-Type",
931                                           pos, len);
932                         return -1;
933                 }
934                 pac_type = WPA_GET_BE16(pos);
935                 if (pac_type != PAC_TYPE_TUNNEL_PAC &&
936                     pac_type != PAC_TYPE_USER_AUTHORIZATION &&
937                     pac_type != PAC_TYPE_MACHINE_AUTHENTICATION) {
938                         wpa_printf(MSG_INFO, "EAP-FAST: Unsupported PAC Type "
939                                    "%d", pac_type);
940                         return -1;
941                 }
942
943                 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info - PAC-Type %d",
944                            pac_type);
945                 entry->pac_type = pac_type;
946                 break;
947         default:
948                 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignored unknown PAC-Info "
949                            "type %d", type);
950                 break;
951         }
952
953         return 0;
954 }
955
956
957 static int eap_fast_process_pac_info(struct eap_fast_pac *entry)
958 {
959         struct pac_tlv_hdr *hdr;
960         u8 *pos;
961         size_t left, len;
962         int type;
963
964         /* draft-cam-winget-eap-fast-provisioning-04.txt, Section 4.2.4 */
965
966         /* PAC-Type defaults to Tunnel PAC (Type 1) */
967         entry->pac_type = PAC_TYPE_TUNNEL_PAC;
968
969         pos = entry->pac_info;
970         left = entry->pac_info_len;
971         while (left > sizeof(*hdr)) {
972                 hdr = (struct pac_tlv_hdr *) pos;
973                 type = be_to_host16(hdr->type);
974                 len = be_to_host16(hdr->len);
975                 pos += sizeof(*hdr);
976                 left -= sizeof(*hdr);
977                 if (len > left) {
978                         wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info overrun "
979                                    "(type=%d len=%lu left=%lu)",
980                                    type, (unsigned long) len,
981                                    (unsigned long) left);
982                         return -1;
983                 }
984
985                 if (eap_fast_parse_pac_info(entry, type, pos, len) < 0)
986                         return -1;
987
988                 pos += len;
989                 left -= len;
990         }
991
992         if (entry->a_id == NULL || entry->a_id_info == NULL) {
993                 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info does not include "
994                            "all the required fields");
995                 return -1;
996         }
997
998         return 0;
999 }
1000
1001
1002 static struct wpabuf * eap_fast_process_pac(struct eap_sm *sm,
1003                                             struct eap_fast_data *data,
1004                                             struct eap_method_ret *ret,
1005                                             u8 *pac, size_t pac_len)
1006 {
1007         struct eap_peer_config *config = eap_get_config(sm);
1008         struct eap_fast_pac entry;
1009
1010         os_memset(&entry, 0, sizeof(entry));
1011         if (eap_fast_process_pac_tlv(&entry, pac, pac_len) ||
1012             eap_fast_process_pac_info(&entry))
1013                 return NULL;
1014
1015         eap_fast_add_pac(&data->pac, &data->current_pac, &entry);
1016         eap_fast_pac_list_truncate(data->pac, data->max_pac_list_len);
1017         if (data->use_pac_binary_format)
1018                 eap_fast_save_pac_bin(sm, data->pac, config->pac_file);
1019         else
1020                 eap_fast_save_pac(sm, data->pac, config->pac_file);
1021
1022         if (data->provisioning) {
1023                 if (data->anon_provisioning) {
1024                         /*
1025                          * Unauthenticated provisioning does not provide keying
1026                          * material and must end with an EAP-Failure.
1027                          * Authentication will be done separately after this.
1028                          */
1029                         data->success = 0;
1030                         ret->decision = DECISION_FAIL;
1031                 } else {
1032                         /*
1033                          * Server may or may not allow authenticated
1034                          * provisioning also for key generation.
1035                          */
1036                         ret->decision = DECISION_COND_SUCC;
1037                 }
1038                 wpa_printf(MSG_DEBUG, "EAP-FAST: Send PAC-Acknowledgement TLV "
1039                            "- Provisioning completed successfully");
1040         } else {
1041                 /*
1042                  * This is PAC refreshing, i.e., normal authentication that is
1043                  * expected to be completed with an EAP-Success.
1044                  */
1045                 wpa_printf(MSG_DEBUG, "EAP-FAST: Send PAC-Acknowledgement TLV "
1046                            "- PAC refreshing completed successfully");
1047                 ret->decision = DECISION_UNCOND_SUCC;
1048         }
1049         ret->methodState = METHOD_DONE;
1050         return eap_fast_tlv_pac_ack();
1051 }
1052
1053
1054 static int eap_fast_parse_decrypted(struct wpabuf *decrypted,
1055                                     struct eap_fast_tlv_parse *tlv,
1056                                     struct wpabuf **resp)
1057 {
1058         int mandatory, tlv_type, len, res;
1059         u8 *pos, *end;
1060
1061         os_memset(tlv, 0, sizeof(*tlv));
1062
1063         /* Parse TLVs from the decrypted Phase 2 data */
1064         pos = wpabuf_mhead(decrypted);
1065         end = pos + wpabuf_len(decrypted);
1066         while (pos + 4 < end) {
1067                 mandatory = pos[0] & 0x80;
1068                 tlv_type = WPA_GET_BE16(pos) & 0x3fff;
1069                 pos += 2;
1070                 len = WPA_GET_BE16(pos);
1071                 pos += 2;
1072                 if (pos + len > end) {
1073                         wpa_printf(MSG_INFO, "EAP-FAST: TLV overflow");
1074                         return -1;
1075                 }
1076                 wpa_printf(MSG_DEBUG, "EAP-FAST: Received Phase 2: "
1077                            "TLV type %d length %d%s",
1078                            tlv_type, len, mandatory ? " (mandatory)" : "");
1079
1080                 res = eap_fast_parse_tlv(tlv, tlv_type, pos, len);
1081                 if (res == -2)
1082                         break;
1083                 if (res < 0) {
1084                         if (mandatory) {
1085                                 wpa_printf(MSG_DEBUG, "EAP-FAST: Nak unknown "
1086                                            "mandatory TLV type %d", tlv_type);
1087                                 *resp = eap_fast_tlv_nak(0, tlv_type);
1088                                 break;
1089                         } else {
1090                                 wpa_printf(MSG_DEBUG, "EAP-FAST: ignored "
1091                                            "unknown optional TLV type %d",
1092                                            tlv_type);
1093                         }
1094                 }
1095
1096                 pos += len;
1097         }
1098
1099         return 0;
1100 }
1101
1102
1103 static int eap_fast_encrypt_response(struct eap_sm *sm,
1104                                      struct eap_fast_data *data,
1105                                      struct wpabuf *resp,
1106                                      u8 identifier, struct wpabuf **out_data)
1107 {
1108         if (resp == NULL)
1109                 return 0;
1110
1111         wpa_hexdump_buf(MSG_DEBUG, "EAP-FAST: Encrypting Phase 2 data",
1112                         resp);
1113         if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_FAST,
1114                                  data->fast_version, identifier,
1115                                  resp, out_data)) {
1116                 wpa_printf(MSG_INFO, "EAP-FAST: Failed to encrypt a Phase 2 "
1117                            "frame");
1118         }
1119         wpabuf_free(resp);
1120
1121         return 0;
1122 }
1123
1124
1125 static struct wpabuf * eap_fast_pac_request(void)
1126 {
1127         struct wpabuf *tmp;
1128         u8 *pos, *pos2;
1129
1130         tmp = wpabuf_alloc(sizeof(struct eap_tlv_hdr) +
1131                            sizeof(struct eap_tlv_request_action_tlv) +
1132                            sizeof(struct eap_tlv_pac_type_tlv));
1133         if (tmp == NULL)
1134                 return NULL;
1135
1136         pos = wpabuf_put(tmp, 0);
1137         pos2 = eap_fast_write_pac_request(pos, PAC_TYPE_TUNNEL_PAC);
1138         wpabuf_put(tmp, pos2 - pos);
1139         return tmp;
1140 }
1141
1142
1143 static int eap_fast_process_decrypted(struct eap_sm *sm,
1144                                       struct eap_fast_data *data,
1145                                       struct eap_method_ret *ret,
1146                                       const struct eap_hdr *req,
1147                                       struct wpabuf *decrypted,
1148                                       struct wpabuf **out_data)
1149 {
1150         struct wpabuf *resp = NULL, *tmp;
1151         struct eap_fast_tlv_parse tlv;
1152         int failed = 0;
1153
1154         if (eap_fast_parse_decrypted(decrypted, &tlv, &resp) < 0)
1155                 return 0;
1156         if (resp)
1157                 return eap_fast_encrypt_response(sm, data, resp,
1158                                                  req->identifier, out_data);
1159
1160         if (tlv.result == EAP_TLV_RESULT_FAILURE) {
1161                 resp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 0);
1162                 return eap_fast_encrypt_response(sm, data, resp,
1163                                                  req->identifier, out_data);
1164         }
1165
1166         if (tlv.iresult == EAP_TLV_RESULT_FAILURE) {
1167                 resp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 1);
1168                 return eap_fast_encrypt_response(sm, data, resp,
1169                                                  req->identifier, out_data);
1170         }
1171
1172         if (tlv.crypto_binding) {
1173                 tmp = eap_fast_process_crypto_binding(sm, data, ret,
1174                                                       tlv.crypto_binding,
1175                                                       tlv.crypto_binding_len);
1176                 if (tmp == NULL)
1177                         failed = 1;
1178                 else
1179                         resp = wpabuf_concat(resp, tmp);
1180         }
1181
1182         if (tlv.iresult == EAP_TLV_RESULT_SUCCESS) {
1183                 tmp = eap_fast_tlv_result(failed ? EAP_TLV_RESULT_FAILURE :
1184                                           EAP_TLV_RESULT_SUCCESS, 1);
1185                 resp = wpabuf_concat(resp, tmp);
1186         }
1187
1188         if (tlv.eap_payload_tlv) {
1189                 tmp = eap_fast_process_eap_payload_tlv(
1190                         sm, data, ret, req, tlv.eap_payload_tlv,
1191                         tlv.eap_payload_tlv_len);
1192                 resp = wpabuf_concat(resp, tmp);
1193         }
1194
1195         if (tlv.pac && tlv.result != EAP_TLV_RESULT_SUCCESS) {
1196                 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV without Result TLV "
1197                            "acknowledging success");
1198                 failed = 1;
1199         } else if (tlv.pac && tlv.result == EAP_TLV_RESULT_SUCCESS) {
1200                 tmp = eap_fast_process_pac(sm, data, ret, tlv.pac,
1201                                            tlv.pac_len);
1202                 resp = wpabuf_concat(resp, tmp);
1203         }
1204
1205         if (data->current_pac == NULL && data->provisioning &&
1206             !data->anon_provisioning && !tlv.pac &&
1207             (tlv.iresult == EAP_TLV_RESULT_SUCCESS ||
1208              tlv.result == EAP_TLV_RESULT_SUCCESS)) {
1209                 /*
1210                  * Need to request Tunnel PAC when using authenticated
1211                  * provisioning.
1212                  */
1213                 wpa_printf(MSG_DEBUG, "EAP-FAST: Request Tunnel PAC");
1214                 tmp = eap_fast_pac_request();
1215                 resp = wpabuf_concat(resp, tmp);
1216         }
1217
1218         if (tlv.result == EAP_TLV_RESULT_SUCCESS && !failed) {
1219                 tmp = eap_fast_tlv_result(EAP_TLV_RESULT_SUCCESS, 0);
1220                 resp = wpabuf_concat(tmp, resp);
1221         } else if (failed) {
1222                 tmp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 0);
1223                 resp = wpabuf_concat(tmp, resp);
1224         }
1225
1226         if (resp && tlv.result == EAP_TLV_RESULT_SUCCESS && !failed &&
1227             tlv.crypto_binding && data->phase2_success) {
1228                 if (data->anon_provisioning) {
1229                         wpa_printf(MSG_DEBUG, "EAP-FAST: Unauthenticated "
1230                                    "provisioning completed successfully.");
1231                         ret->methodState = METHOD_DONE;
1232                         ret->decision = DECISION_FAIL;
1233                 } else {
1234                         wpa_printf(MSG_DEBUG, "EAP-FAST: Authentication "
1235                                    "completed successfully.");
1236                         if (data->provisioning)
1237                                 ret->methodState = METHOD_MAY_CONT;
1238                         else
1239                                 ret->methodState = METHOD_DONE;
1240                         ret->decision = DECISION_UNCOND_SUCC;
1241                 }
1242         }
1243
1244         if (resp == NULL) {
1245                 wpa_printf(MSG_DEBUG, "EAP-FAST: No recognized TLVs - send "
1246                            "empty response packet");
1247                 resp = wpabuf_alloc(1);
1248         }
1249
1250         return eap_fast_encrypt_response(sm, data, resp, req->identifier,
1251                                          out_data);
1252 }
1253
1254
1255 static int eap_fast_decrypt(struct eap_sm *sm, struct eap_fast_data *data,
1256                             struct eap_method_ret *ret,
1257                             const struct eap_hdr *req,
1258                             const struct wpabuf *in_data,
1259                             struct wpabuf **out_data)
1260 {
1261         struct wpabuf *in_decrypted;
1262         int res;
1263
1264         wpa_printf(MSG_DEBUG, "EAP-FAST: Received %lu bytes encrypted data for"
1265                    " Phase 2", (unsigned long) wpabuf_len(in_data));
1266
1267         if (data->pending_phase2_req) {
1268                 wpa_printf(MSG_DEBUG, "EAP-FAST: Pending Phase 2 request - "
1269                            "skip decryption and use old data");
1270                 /* Clear TLS reassembly state. */
1271                 eap_peer_tls_reset_input(&data->ssl);
1272
1273                 in_decrypted = data->pending_phase2_req;
1274                 data->pending_phase2_req = NULL;
1275                 goto continue_req;
1276         }
1277
1278         if (wpabuf_len(in_data) == 0) {
1279                 /* Received TLS ACK - requesting more fragments */
1280                 return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_FAST,
1281                                             data->fast_version,
1282                                             req->identifier, NULL, out_data);
1283         }
1284
1285         res = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
1286         if (res)
1287                 return res;
1288
1289 continue_req:
1290         wpa_hexdump_buf(MSG_MSGDUMP, "EAP-FAST: Decrypted Phase 2 TLV(s)",
1291                         in_decrypted);
1292
1293         if (wpabuf_len(in_decrypted) < 4) {
1294                 wpa_printf(MSG_INFO, "EAP-FAST: Too short Phase 2 "
1295                            "TLV frame (len=%lu)",
1296                            (unsigned long) wpabuf_len(in_decrypted));
1297                 wpabuf_free(in_decrypted);
1298                 return -1;
1299         }
1300
1301         res = eap_fast_process_decrypted(sm, data, ret, req,
1302                                          in_decrypted, out_data);
1303
1304         wpabuf_free(in_decrypted);
1305
1306         return res;
1307 }
1308
1309
1310 static const u8 * eap_fast_get_a_id(const u8 *buf, size_t len, size_t *id_len)
1311 {
1312         const u8 *a_id;
1313         struct pac_tlv_hdr *hdr;
1314
1315         /*
1316          * Parse authority identity (A-ID) from the EAP-FAST/Start. This
1317          * supports both raw A-ID and one inside an A-ID TLV.
1318          */
1319         a_id = buf;
1320         *id_len = len;
1321         if (len > sizeof(*hdr)) {
1322                 int tlen;
1323                 hdr = (struct pac_tlv_hdr *) buf;
1324                 tlen = be_to_host16(hdr->len);
1325                 if (be_to_host16(hdr->type) == PAC_TYPE_A_ID &&
1326                     sizeof(*hdr) + tlen <= len) {
1327                         wpa_printf(MSG_DEBUG, "EAP-FAST: A-ID was in TLV "
1328                                    "(Start)");
1329                         a_id = (u8 *) (hdr + 1);
1330                         *id_len = tlen;
1331                 }
1332         }
1333         wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: A-ID", a_id, *id_len);
1334
1335         return a_id;
1336 }
1337
1338
1339 static void eap_fast_select_pac(struct eap_fast_data *data,
1340                                 const u8 *a_id, size_t a_id_len)
1341 {
1342         data->current_pac = eap_fast_get_pac(data->pac, a_id, a_id_len,
1343                                              PAC_TYPE_TUNNEL_PAC);
1344         if (data->current_pac == NULL) {
1345                 /*
1346                  * Tunnel PAC was not available for this A-ID. Try to use
1347                  * Machine Authentication PAC, if one is available.
1348                  */
1349                 data->current_pac = eap_fast_get_pac(
1350                         data->pac, a_id, a_id_len,
1351                         PAC_TYPE_MACHINE_AUTHENTICATION);
1352         }
1353
1354         if (data->current_pac) {
1355                 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC found for this A-ID "
1356                            "(PAC-Type %d)", data->current_pac->pac_type);
1357                 wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-FAST: A-ID-Info",
1358                                   data->current_pac->a_id_info,
1359                                   data->current_pac->a_id_info_len);
1360         }
1361 }
1362
1363
1364 static int eap_fast_use_pac_opaque(struct eap_sm *sm,
1365                                    struct eap_fast_data *data,
1366                                    struct eap_fast_pac *pac)
1367 {
1368         u8 *tlv;
1369         size_t tlv_len, olen;
1370         struct eap_tlv_hdr *ehdr;
1371
1372         olen = pac->pac_opaque_len;
1373         tlv_len = sizeof(*ehdr) + olen;
1374         tlv = os_malloc(tlv_len);
1375         if (tlv) {
1376                 ehdr = (struct eap_tlv_hdr *) tlv;
1377                 ehdr->tlv_type = host_to_be16(PAC_TYPE_PAC_OPAQUE);
1378                 ehdr->length = host_to_be16(olen);
1379                 os_memcpy(ehdr + 1, pac->pac_opaque, olen);
1380         }
1381         if (tlv == NULL ||
1382             tls_connection_client_hello_ext(sm->ssl_ctx, data->ssl.conn,
1383                                             TLS_EXT_PAC_OPAQUE,
1384                                             tlv, tlv_len) < 0) {
1385                 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to add PAC-Opaque TLS "
1386                            "extension");
1387                 os_free(tlv);
1388                 return -1;
1389         }
1390         os_free(tlv);
1391
1392         return 0;
1393 }
1394
1395
1396 static int eap_fast_clear_pac_opaque_ext(struct eap_sm *sm,
1397                                          struct eap_fast_data *data)
1398 {
1399         if (tls_connection_client_hello_ext(sm->ssl_ctx, data->ssl.conn,
1400                                             TLS_EXT_PAC_OPAQUE, NULL, 0) < 0) {
1401                 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to remove PAC-Opaque "
1402                            "TLS extension");
1403                 return -1;
1404         }
1405         return 0;
1406 }
1407
1408
1409 static int eap_fast_set_provisioning_ciphers(struct eap_sm *sm,
1410                                              struct eap_fast_data *data)
1411 {
1412         u8 ciphers[5];
1413         int count = 0;
1414
1415         if (data->provisioning_allowed & EAP_FAST_PROV_UNAUTH) {
1416                 wpa_printf(MSG_DEBUG, "EAP-FAST: Enabling unauthenticated "
1417                            "provisioning TLS cipher suites");
1418                 ciphers[count++] = TLS_CIPHER_ANON_DH_AES128_SHA;
1419         }
1420
1421         if (data->provisioning_allowed & EAP_FAST_PROV_AUTH) {
1422                 wpa_printf(MSG_DEBUG, "EAP-FAST: Enabling authenticated "
1423                            "provisioning TLS cipher suites");
1424                 ciphers[count++] = TLS_CIPHER_RSA_DHE_AES128_SHA;
1425                 ciphers[count++] = TLS_CIPHER_AES128_SHA;
1426                 ciphers[count++] = TLS_CIPHER_RC4_SHA;
1427         }
1428
1429         ciphers[count++] = TLS_CIPHER_NONE;
1430
1431         if (tls_connection_set_cipher_list(sm->ssl_ctx, data->ssl.conn,
1432                                            ciphers)) {
1433                 wpa_printf(MSG_INFO, "EAP-FAST: Could not configure TLS "
1434                            "cipher suites for provisioning");
1435                 return -1;
1436         }
1437
1438         return 0;
1439 }
1440
1441
1442 static int eap_fast_process_start(struct eap_sm *sm,
1443                                   struct eap_fast_data *data, u8 flags,
1444                                   const u8 *pos, size_t left)
1445 {
1446         const u8 *a_id;
1447         size_t a_id_len;
1448
1449         /* EAP-FAST Version negotiation (section 3.1) */
1450         wpa_printf(MSG_DEBUG, "EAP-FAST: Start (server ver=%d, own ver=%d)",
1451                    flags & EAP_PEAP_VERSION_MASK, data->fast_version);
1452         if ((flags & EAP_PEAP_VERSION_MASK) < data->fast_version)
1453                 data->fast_version = flags & EAP_PEAP_VERSION_MASK;
1454         wpa_printf(MSG_DEBUG, "EAP-FAST: Using FAST version %d",
1455                    data->fast_version);
1456
1457         a_id = eap_fast_get_a_id(pos, left, &a_id_len);
1458         eap_fast_select_pac(data, a_id, a_id_len);
1459
1460         if (data->resuming && data->current_pac) {
1461                 wpa_printf(MSG_DEBUG, "EAP-FAST: Trying to resume session - "
1462                            "do not add PAC-Opaque to TLS ClientHello");
1463                 if (eap_fast_clear_pac_opaque_ext(sm, data) < 0)
1464                         return -1;
1465         } else if (data->current_pac) {
1466                 /*
1467                  * PAC found for the A-ID and we are not resuming an old
1468                  * session, so add PAC-Opaque extension to ClientHello.
1469                  */
1470                 if (eap_fast_use_pac_opaque(sm, data, data->current_pac) < 0)
1471                         return -1;
1472         } else {
1473                 /* No PAC found, so we must provision one. */
1474                 if (!data->provisioning_allowed) {
1475                         wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC found and "
1476                                    "provisioning disabled");
1477                         return -1;
1478                 }
1479                 wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC found - "
1480                            "starting provisioning");
1481                 if (eap_fast_set_provisioning_ciphers(sm, data) < 0 ||
1482                     eap_fast_clear_pac_opaque_ext(sm, data) < 0)
1483                         return -1;
1484                 data->provisioning = 1;
1485         }
1486
1487         return 0;
1488 }
1489
1490
1491 static struct wpabuf * eap_fast_process(struct eap_sm *sm, void *priv,
1492                                         struct eap_method_ret *ret,
1493                                         const struct wpabuf *reqData)
1494 {
1495         const struct eap_hdr *req;
1496         size_t left;
1497         int res;
1498         u8 flags, id;
1499         struct wpabuf *resp;
1500         const u8 *pos;
1501         struct eap_fast_data *data = priv;
1502
1503         pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_FAST, ret,
1504                                         reqData, &left, &flags);
1505         if (pos == NULL)
1506                 return NULL;
1507
1508         req = wpabuf_head(reqData);
1509         id = req->identifier;
1510
1511         if (flags & EAP_TLS_FLAGS_START) {
1512                 if (eap_fast_process_start(sm, data, flags, pos, left) < 0)
1513                         return NULL;
1514
1515                 left = 0; /* A-ID is not used in further packet processing */
1516         }
1517
1518         resp = NULL;
1519         if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1520             !data->resuming) {
1521                 /* Process tunneled (encrypted) phase 2 data. */
1522                 struct wpabuf msg;
1523                 wpabuf_set(&msg, pos, left);
1524                 res = eap_fast_decrypt(sm, data, ret, req, &msg, &resp);
1525                 if (res < 0) {
1526                         ret->methodState = METHOD_DONE;
1527                         ret->decision = DECISION_FAIL;
1528                         /*
1529                          * Ack possible Alert that may have caused failure in
1530                          * decryption.
1531                          */
1532                         res = 1;
1533                 }
1534         } else {
1535                 /* Continue processing TLS handshake (phase 1). */
1536                 res = eap_peer_tls_process_helper(sm, &data->ssl,
1537                                                   EAP_TYPE_FAST,
1538                                                   data->fast_version, id, pos,
1539                                                   left, &resp);
1540
1541                 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
1542                         char cipher[80];
1543                         wpa_printf(MSG_DEBUG,
1544                                    "EAP-FAST: TLS done, proceed to Phase 2");
1545                         if (data->provisioning &&
1546                             (!(data->provisioning_allowed &
1547                                EAP_FAST_PROV_AUTH) ||
1548                              tls_get_cipher(sm->ssl_ctx, data->ssl.conn,
1549                                             cipher, sizeof(cipher)) < 0 ||
1550                              os_strstr(cipher, "ADH-") ||
1551                              os_strstr(cipher, "anon"))) {
1552                                 wpa_printf(MSG_DEBUG, "EAP-FAST: Using "
1553                                            "anonymous (unauthenticated) "
1554                                            "provisioning");
1555                                 data->anon_provisioning = 1;
1556                         } else
1557                                 data->anon_provisioning = 0;
1558                         data->resuming = 0;
1559                         eap_fast_derive_keys(sm, data);
1560                 }
1561
1562                 if (res == 2) {
1563                         struct wpabuf msg;
1564                         /*
1565                          * Application data included in the handshake message.
1566                          */
1567                         wpabuf_free(data->pending_phase2_req);
1568                         data->pending_phase2_req = resp;
1569                         resp = NULL;
1570                         wpabuf_set(&msg, pos, left);
1571                         res = eap_fast_decrypt(sm, data, ret, req, &msg,
1572                                                &resp);
1573                 }
1574         }
1575
1576         if (res == 1) {
1577                 wpabuf_free(resp);
1578                 return eap_peer_tls_build_ack(id, EAP_TYPE_FAST,
1579                                               data->fast_version);
1580         }
1581
1582         return resp;
1583 }
1584
1585
1586 #if 0 /* FIX */
1587 static Boolean eap_fast_has_reauth_data(struct eap_sm *sm, void *priv)
1588 {
1589         struct eap_fast_data *data = priv;
1590         return tls_connection_established(sm->ssl_ctx, data->ssl.conn);
1591 }
1592
1593
1594 static void eap_fast_deinit_for_reauth(struct eap_sm *sm, void *priv)
1595 {
1596         struct eap_fast_data *data = priv;
1597         os_free(data->key_block_p);
1598         data->key_block_p = NULL;
1599         wpabuf_free(data->pending_phase2_req);
1600         data->pending_phase2_req = NULL;
1601 }
1602
1603
1604 static void * eap_fast_init_for_reauth(struct eap_sm *sm, void *priv)
1605 {
1606         struct eap_fast_data *data = priv;
1607         if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
1608                 os_free(data);
1609                 return NULL;
1610         }
1611         if (data->phase2_priv && data->phase2_method &&
1612             data->phase2_method->init_for_reauth)
1613                 data->phase2_method->init_for_reauth(sm, data->phase2_priv);
1614         data->phase2_success = 0;
1615         data->resuming = 1;
1616         data->provisioning = 0;
1617         data->anon_provisioning = 0;
1618         data->simck_idx = 0;
1619         return priv;
1620 }
1621 #endif
1622
1623
1624 static int eap_fast_get_status(struct eap_sm *sm, void *priv, char *buf,
1625                                size_t buflen, int verbose)
1626 {
1627         struct eap_fast_data *data = priv;
1628         int len, ret;
1629
1630         len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
1631         if (data->phase2_method) {
1632                 ret = os_snprintf(buf + len, buflen - len,
1633                                   "EAP-FAST Phase2 method=%s\n",
1634                                   data->phase2_method->name);
1635                 if (ret < 0 || (size_t) ret >= buflen - len)
1636                         return len;
1637                 len += ret;
1638         }
1639         return len;
1640 }
1641
1642
1643 static Boolean eap_fast_isKeyAvailable(struct eap_sm *sm, void *priv)
1644 {
1645         struct eap_fast_data *data = priv;
1646         return data->success;
1647 }
1648
1649
1650 static u8 * eap_fast_getKey(struct eap_sm *sm, void *priv, size_t *len)
1651 {
1652         struct eap_fast_data *data = priv;
1653         u8 *key;
1654
1655         if (!data->success)
1656                 return NULL;
1657
1658         key = os_malloc(EAP_FAST_KEY_LEN);
1659         if (key == NULL)
1660                 return NULL;
1661
1662         *len = EAP_FAST_KEY_LEN;
1663         os_memcpy(key, data->key_data, EAP_FAST_KEY_LEN);
1664
1665         return key;
1666 }
1667
1668
1669 static u8 * eap_fast_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
1670 {
1671         struct eap_fast_data *data = priv;
1672         u8 *key;
1673
1674         if (!data->success)
1675                 return NULL;
1676
1677         key = os_malloc(EAP_EMSK_LEN);
1678         if (key == NULL)
1679                 return NULL;
1680
1681         *len = EAP_EMSK_LEN;
1682         os_memcpy(key, data->emsk, EAP_EMSK_LEN);
1683
1684         return key;
1685 }
1686
1687
1688 int eap_peer_fast_register(void)
1689 {
1690         struct eap_method *eap;
1691         int ret;
1692
1693         eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
1694                                     EAP_VENDOR_IETF, EAP_TYPE_FAST, "FAST");
1695         if (eap == NULL)
1696                 return -1;
1697
1698         eap->init = eap_fast_init;
1699         eap->deinit = eap_fast_deinit;
1700         eap->process = eap_fast_process;
1701         eap->isKeyAvailable = eap_fast_isKeyAvailable;
1702         eap->getKey = eap_fast_getKey;
1703         eap->get_status = eap_fast_get_status;
1704 #if 0
1705         eap->has_reauth_data = eap_fast_has_reauth_data;
1706         eap->deinit_for_reauth = eap_fast_deinit_for_reauth;
1707         eap->init_for_reauth = eap_fast_init_for_reauth;
1708 #endif
1709         eap->get_emsk = eap_fast_get_emsk;
1710
1711         ret = eap_peer_method_register(eap);
1712         if (ret)
1713                 eap_peer_method_free(eap);
1714         return ret;
1715 }