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