]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/wpa_supplicant/eap_ttls.c
This commit was generated by cvs2svn to compensate for changes in r155094,
[FreeBSD/FreeBSD.git] / contrib / wpa_supplicant / eap_ttls.c
1 /*
2  * WPA Supplicant / EAP-TTLS (draft-ietf-pppext-eap-ttls-03.txt)
3  * Copyright (c) 2004-2005, Jouni Malinen <jkmaline@cc.hut.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 <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18
19 #include "common.h"
20 #include "eap_i.h"
21 #include "eap_tls_common.h"
22 #include "wpa_supplicant.h"
23 #include "config_ssid.h"
24 #include "ms_funcs.h"
25 #include "md5.h"
26 #include "tls.h"
27 #include "eap_ttls.h"
28
29
30 static void eap_ttls_deinit(struct eap_sm *sm, void *priv);
31
32
33 struct eap_ttls_data {
34         struct eap_ssl_data ssl;
35
36         const struct eap_method *phase2_method;
37         void *phase2_priv;
38         int phase2_success;
39         int phase2_start;
40
41         enum {
42                 EAP_TTLS_PHASE2_EAP,
43                 EAP_TTLS_PHASE2_MSCHAPV2,
44                 EAP_TTLS_PHASE2_MSCHAP,
45                 EAP_TTLS_PHASE2_PAP,
46                 EAP_TTLS_PHASE2_CHAP
47         } phase2_type;
48         u8 phase2_eap_type;
49         u8 *phase2_eap_types;
50         size_t num_phase2_eap_types;
51
52         u8 auth_response[20];
53         int auth_response_valid;
54         u8 ident;
55         int resuming; /* starting a resumed session */
56         int reauth; /* reauthentication */
57         u8 *key_data;
58
59         u8 *pending_phase2_req;
60         size_t pending_phase2_req_len;
61 };
62
63
64 static void * eap_ttls_init(struct eap_sm *sm)
65 {
66         struct eap_ttls_data *data;
67         struct wpa_ssid *config = eap_get_config(sm);
68         char *selected;
69
70         data = malloc(sizeof(*data));
71         if (data == NULL)
72                 return NULL;
73         memset(data, 0, sizeof(*data));
74         selected = "EAP";
75         data->phase2_type = EAP_TTLS_PHASE2_EAP;
76         if (config && config->phase2) {
77                 if (strstr(config->phase2, "autheap=")) {
78                         selected = "EAP";
79                         data->phase2_type = EAP_TTLS_PHASE2_EAP;
80                 } else if (strstr(config->phase2, "auth=MSCHAPV2")) {
81                         selected = "MSCHAPV2";
82                         data->phase2_type = EAP_TTLS_PHASE2_MSCHAPV2;
83                 } else if (strstr(config->phase2, "auth=MSCHAP")) {
84                         selected = "MSCHAP";
85                         data->phase2_type = EAP_TTLS_PHASE2_MSCHAP;
86                 } else if (strstr(config->phase2, "auth=PAP")) {
87                         selected = "PAP";
88                         data->phase2_type = EAP_TTLS_PHASE2_PAP;
89                 } else if (strstr(config->phase2, "auth=CHAP")) {
90                         selected = "CHAP";
91                         data->phase2_type = EAP_TTLS_PHASE2_CHAP;
92                 }
93         }
94         wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase2 type: %s", selected);
95
96         if (data->phase2_type == EAP_TTLS_PHASE2_EAP) {
97                 if (config && config->phase2) {
98                         char *start, *pos, *buf;
99                         u8 method, *methods = NULL, *_methods;
100                         size_t num_methods = 0;
101                         start = buf = strdup(config->phase2);
102                         if (buf == NULL) {
103                                 eap_ttls_deinit(sm, data);
104                                 return NULL;
105                         }
106                         while (start && *start != '\0') {
107                                 pos = strstr(start, "autheap=");
108                                 if (pos == NULL)
109                                         break;
110                                 if (start != pos && *(pos - 1) != ' ') {
111                                         start = pos + 8;
112                                         continue;
113                                 }
114
115                                 start = pos + 8;
116                                 pos = strchr(start, ' ');
117                                 if (pos)
118                                         *pos++ = '\0';
119                                 method = eap_get_phase2_type(start);
120                                 if (method == EAP_TYPE_NONE) {
121                                         wpa_printf(MSG_ERROR, "EAP-TTLS: "
122                                                    "Unsupported Phase2 EAP "
123                                                    "method '%s'", start);
124                                 } else {
125                                         num_methods++;
126                                         _methods = realloc(methods,
127                                                            num_methods);
128                                         if (_methods == NULL) {
129                                                 free(methods);
130                                                 eap_ttls_deinit(sm, data);
131                                                 return NULL;
132                                         }
133                                         methods = _methods;
134                                         methods[num_methods - 1] = method;
135                                 }
136
137                                 start = pos;
138                         }
139                         free(buf);
140                         data->phase2_eap_types = methods;
141                         data->num_phase2_eap_types = num_methods;
142                 }
143                 if (data->phase2_eap_types == NULL) {
144                         data->phase2_eap_types = eap_get_phase2_types(
145                                 config, &data->num_phase2_eap_types);
146                 }
147                 if (data->phase2_eap_types == NULL) {
148                         wpa_printf(MSG_ERROR, "EAP-TTLS: No Phase2 EAP method "
149                                    "available");
150                         eap_ttls_deinit(sm, data);
151                         return NULL;
152                 }
153                 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Phase2 EAP types",
154                             data->phase2_eap_types,
155                             data->num_phase2_eap_types);
156                 data->phase2_eap_type = EAP_TYPE_NONE;
157         }
158
159
160         if (eap_tls_ssl_init(sm, &data->ssl, config)) {
161                 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to initialize SSL.");
162                 eap_ttls_deinit(sm, data);
163                 return NULL;
164         }
165
166         return data;
167 }
168
169
170 static void eap_ttls_deinit(struct eap_sm *sm, void *priv)
171 {
172         struct eap_ttls_data *data = priv;
173         if (data == NULL)
174                 return;
175         if (data->phase2_priv && data->phase2_method)
176                 data->phase2_method->deinit(sm, data->phase2_priv);
177         free(data->phase2_eap_types);
178         eap_tls_ssl_deinit(sm, &data->ssl);
179         free(data->key_data);
180         free(data->pending_phase2_req);
181         free(data);
182 }
183
184
185 static int eap_ttls_encrypt(struct eap_sm *sm, struct eap_ttls_data *data,
186                             int id, u8 *plain, size_t plain_len,
187                             u8 **out_data, size_t *out_len)
188 {
189         int res;
190         u8 *pos;
191         struct eap_hdr *resp;
192
193         /* TODO: add support for fragmentation, if needed. This will need to
194          * add TLS Message Length field, if the frame is fragmented. */
195         resp = malloc(sizeof(struct eap_hdr) + 2 + data->ssl.tls_out_limit);
196         if (resp == NULL)
197                 return -1;
198
199         resp->code = EAP_CODE_RESPONSE;
200         resp->identifier = id;
201
202         pos = (u8 *) (resp + 1);
203         *pos++ = EAP_TYPE_TTLS;
204         *pos++ = 0;
205
206         res = tls_connection_encrypt(sm->ssl_ctx, data->ssl.conn,
207                                      plain, plain_len,
208                                      pos, data->ssl.tls_out_limit);
209         if (res < 0) {
210                 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to encrypt Phase 2 "
211                            "data");
212                 free(resp);
213                 return -1;
214         }
215
216         *out_len = sizeof(struct eap_hdr) + 2 + res;
217         resp->length = host_to_be16(*out_len);
218         *out_data = (u8 *) resp;
219         return 0;
220 }
221
222
223 static u8 * eap_ttls_avp_hdr(u8 *avphdr, u32 avp_code, u32 vendor_id,
224                              int mandatory, size_t len)
225 {
226         struct ttls_avp_vendor *avp;
227         u8 flags;
228         size_t hdrlen;
229
230         avp = (struct ttls_avp_vendor *) avphdr;
231         flags = mandatory ? AVP_FLAGS_MANDATORY : 0;
232         if (vendor_id) {
233                 flags |= AVP_FLAGS_VENDOR;
234                 hdrlen = sizeof(*avp);
235                 avp->vendor_id = host_to_be32(vendor_id);
236         } else {
237                 hdrlen = sizeof(struct ttls_avp);
238         }
239
240         avp->avp_code = host_to_be32(avp_code);
241         avp->avp_length = host_to_be32((flags << 24) | (hdrlen + len));
242
243         return avphdr + hdrlen;
244 }
245
246
247 static u8 * eap_ttls_avp_add(u8 *start, u8 *avphdr, u32 avp_code,
248                              u32 vendor_id, int mandatory,
249                              u8 *data, size_t len)
250 {
251         u8 *pos;
252         pos = eap_ttls_avp_hdr(avphdr, avp_code, vendor_id, mandatory, len);
253         memcpy(pos, data, len);
254         pos += len;
255         AVP_PAD(start, pos);
256         return pos;
257 }
258
259
260 static int eap_ttls_avp_encapsulate(u8 **resp, size_t *resp_len, u32 avp_code,
261                                     int mandatory)
262 {
263         u8 *avp, *pos;
264
265         avp = malloc(sizeof(struct ttls_avp) + *resp_len + 4);
266         if (avp == NULL) {
267                 free(*resp);
268                 *resp = NULL;
269                 *resp_len = 0;
270                 return -1;
271         }
272
273         pos = eap_ttls_avp_hdr(avp, avp_code, 0, mandatory, *resp_len);
274         memcpy(pos, *resp, *resp_len);
275         pos += *resp_len;
276         AVP_PAD(avp, pos);
277         free(*resp);
278         *resp = avp;
279         *resp_len = pos - avp;
280         return 0;
281 }
282
283
284 static int eap_ttls_phase2_nak(struct eap_sm *sm,
285                                struct eap_ttls_data *data,
286                                struct eap_hdr *hdr,
287                                u8 **resp, size_t *resp_len)
288 {
289         struct eap_hdr *resp_hdr;
290         u8 *pos = (u8 *) (hdr + 1);
291
292         wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 Request: Nak type=%d", *pos);
293         wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Allowed Phase2 EAP types",
294                     data->phase2_eap_types, data->num_phase2_eap_types);
295         *resp_len = sizeof(struct eap_hdr) + 1 + data->num_phase2_eap_types;
296         *resp = malloc(*resp_len);
297         if (*resp == NULL)
298                 return -1;
299
300         resp_hdr = (struct eap_hdr *) (*resp);
301         resp_hdr->code = EAP_CODE_RESPONSE;
302         resp_hdr->identifier = hdr->identifier;
303         resp_hdr->length = host_to_be16(*resp_len);
304         pos = (u8 *) (resp_hdr + 1);
305         *pos++ = EAP_TYPE_NAK;
306         memcpy(pos, data->phase2_eap_types, data->num_phase2_eap_types);
307
308         return 0;
309 }
310
311
312 static int eap_ttls_phase2_request_eap(struct eap_sm *sm,
313                                        struct eap_ttls_data *data,
314                                        struct eap_method_ret *ret,
315                                        struct eap_hdr *req,
316                                        struct eap_hdr *hdr,
317                                        u8 **resp, size_t *resp_len)
318 {
319         size_t len = be_to_host16(hdr->length);
320         u8 *pos;
321         struct eap_method_ret iret;
322         struct wpa_ssid *config = eap_get_config(sm);
323
324         if (len <= sizeof(struct eap_hdr)) {
325                 wpa_printf(MSG_INFO, "EAP-TTLS: too short "
326                            "Phase 2 request (len=%lu)", (unsigned long) len);
327                 return -1;
328         }
329         pos = (u8 *) (hdr + 1);
330         wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP Request: type=%d", *pos);
331         switch (*pos) {
332         case EAP_TYPE_IDENTITY:
333                 *resp = eap_sm_buildIdentity(sm, req->identifier, resp_len, 1);
334                 break;
335         default:
336                 if (data->phase2_eap_type == EAP_TYPE_NONE) {
337                         int i;
338                         for (i = 0; i < data->num_phase2_eap_types; i++) {
339                                 if (data->phase2_eap_types[i] != *pos)
340                                         continue;
341
342                                 data->phase2_eap_type = *pos;
343                                 wpa_printf(MSG_DEBUG, "EAP-TTLS: Selected "
344                                            "Phase 2 EAP method %d",
345                                            data->phase2_eap_type);
346                                 break;
347                         }
348                 }
349                 if (*pos != data->phase2_eap_type || *pos == EAP_TYPE_NONE) {
350                         if (eap_ttls_phase2_nak(sm, data, hdr, resp, resp_len))
351                                 return -1;
352                         break;
353                 }
354
355                 if (data->phase2_priv == NULL) {
356                         data->phase2_method = eap_sm_get_eap_methods(*pos);
357                         if (data->phase2_method) {
358                                 sm->init_phase2 = 1;
359                                 data->phase2_priv =
360                                         data->phase2_method->init(sm);
361                                 sm->init_phase2 = 0;
362                         }
363                 }
364                 if (data->phase2_priv == NULL || data->phase2_method == NULL) {
365                         wpa_printf(MSG_INFO, "EAP-TTLS: failed to initialize "
366                                    "Phase 2 EAP method %d", *pos);
367                         return -1;
368                 }
369                 memset(&iret, 0, sizeof(iret));
370                 *resp = data->phase2_method->process(sm, data->phase2_priv,
371                                                      &iret, (u8 *) hdr, len,
372                                                      resp_len);
373                 if ((iret.methodState == METHOD_DONE ||
374                      iret.methodState == METHOD_MAY_CONT) &&
375                     (iret.decision == DECISION_UNCOND_SUCC ||
376                      iret.decision == DECISION_COND_SUCC ||
377                      iret.decision == DECISION_FAIL)) {
378                         ret->methodState = iret.methodState;
379                         ret->decision = iret.decision;
380                 }
381                 break;
382         }
383
384         if (*resp == NULL &&
385             (config->pending_req_identity || config->pending_req_password ||
386              config->pending_req_otp)) {
387                 return 0;
388         }
389
390         if (*resp == NULL)
391                 return -1;
392
393         wpa_hexdump(MSG_DEBUG, "EAP-TTLS: AVP encapsulate EAP Response",
394                     *resp, *resp_len);
395         return eap_ttls_avp_encapsulate(resp, resp_len,
396                                         RADIUS_ATTR_EAP_MESSAGE, 1);
397 }
398
399
400 static int eap_ttls_phase2_request_mschapv2(struct eap_sm *sm,
401                                             struct eap_ttls_data *data,
402                                             struct eap_method_ret *ret,
403                                             struct eap_hdr *req,
404                                             struct eap_hdr *hdr,
405                                             u8 **resp, size_t *resp_len)
406 {
407         struct wpa_ssid *config = eap_get_config(sm);
408         u8 *buf, *pos, *challenge, *username, *peer_challenge;
409         size_t username_len;
410         int i;
411
412         wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAPV2 Request");
413
414         /* MSCHAPv2 does not include optional domain name in the
415          * challenge-response calculation, so remove domain prefix
416          * (if present). */
417         username = config->identity;
418         username_len = config->identity_len;
419         pos = username;
420         for (i = 0; i < username_len; i++) {
421                 if (username[i] == '\\') {
422                         username_len -= i + 1;
423                         username += i + 1;
424                         break;
425                 }
426         }
427
428         pos = buf = malloc(config->identity_len + 1000);
429         if (buf == NULL) {
430                 wpa_printf(MSG_ERROR,
431                            "EAP-TTLS/MSCHAPV2: Failed to allocate memory");
432                 return -1;
433         }
434
435         /* User-Name */
436         pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
437                                config->identity, config->identity_len);
438
439         /* MS-CHAP-Challenge */
440         challenge = eap_tls_derive_key(sm, &data->ssl, "ttls challenge",
441                                        EAP_TTLS_MSCHAPV2_CHALLENGE_LEN * 2 +
442                                        1);
443         if (challenge == NULL) {
444                 free(buf);
445                 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to derive "
446                            "implicit challenge");
447                 return -1;
448         }
449         peer_challenge = challenge + 1 + EAP_TTLS_MSCHAPV2_CHALLENGE_LEN;
450
451         pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_MS_CHAP_CHALLENGE,
452                                RADIUS_VENDOR_ID_MICROSOFT, 1,
453                                challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
454
455         /* MS-CHAP2-Response */
456         pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP2_RESPONSE,
457                                RADIUS_VENDOR_ID_MICROSOFT, 1,
458                                EAP_TTLS_MSCHAPV2_RESPONSE_LEN);
459         data->ident = challenge[EAP_TTLS_MSCHAPV2_CHALLENGE_LEN];
460         *pos++ = data->ident;
461         *pos++ = 0; /* Flags */
462         memcpy(pos, peer_challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
463         pos += EAP_TTLS_MSCHAPV2_CHALLENGE_LEN;
464         memset(pos, 0, 8); /* Reserved, must be zero */
465         pos += 8;
466         wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAPV2: implicit auth_challenge",
467                     challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
468         wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAPV2: peer_challenge",
469                     peer_challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
470         wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: MSCHAPV2 username",
471                           username, username_len);
472         wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: MSCHAPV2 password",
473                               config->password, config->password_len);
474         generate_nt_response(challenge, peer_challenge,
475                              username, username_len,
476                              config->password, config->password_len,
477                              pos);
478         wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAPV2 response", pos, 24);
479         generate_authenticator_response(config->password, config->password_len,
480                                         peer_challenge, challenge,
481                                         username, username_len,
482                                         pos, data->auth_response);
483         data->auth_response_valid = 1;
484
485         pos += 24;
486         free(challenge);
487         AVP_PAD(buf, pos);
488
489         *resp = buf;
490         *resp_len = pos - buf;
491
492         if (sm->workaround) {
493                 /* At least FreeRADIUS seems to be terminating
494                  * EAP-TTLS/MSHCAPV2 without the expected MS-CHAP-v2 Success
495                  * packet. */
496                 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: EAP workaround - "
497                            "allow success without tunneled response");
498                 ret->methodState = METHOD_MAY_CONT;
499                 ret->decision = DECISION_COND_SUCC;
500         }
501
502         return 0;
503 }
504
505
506 static int eap_ttls_phase2_request_mschap(struct eap_sm *sm,
507                                           struct eap_ttls_data *data,
508                                           struct eap_method_ret *ret,
509                                           struct eap_hdr *req,
510                                           struct eap_hdr *hdr,
511                                           u8 **resp, size_t *resp_len)
512 {
513         struct wpa_ssid *config = eap_get_config(sm);
514         u8 *buf, *pos, *challenge;
515
516         wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAP Request");
517
518         pos = buf = malloc(config->identity_len + 1000);
519         if (buf == NULL) {
520                 wpa_printf(MSG_ERROR,
521                            "EAP-TTLS/MSCHAP: Failed to allocate memory");
522                 return -1;
523         }
524
525         /* User-Name */
526         pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
527                                config->identity, config->identity_len);
528
529         /* MS-CHAP-Challenge */
530         challenge = eap_tls_derive_key(sm, &data->ssl, "ttls challenge",
531                                        EAP_TLS_KEY_LEN);
532         if (challenge == NULL) {
533                 free(buf);
534                 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAP: Failed to derive "
535                            "implicit challenge");
536                 return -1;
537         }
538
539         pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_MS_CHAP_CHALLENGE,
540                                RADIUS_VENDOR_ID_MICROSOFT, 1,
541                                challenge, EAP_TTLS_MSCHAP_CHALLENGE_LEN);
542
543         /* MS-CHAP-Response */
544         pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP_RESPONSE,
545                                RADIUS_VENDOR_ID_MICROSOFT, 1,
546                                EAP_TTLS_MSCHAP_RESPONSE_LEN);
547         data->ident = challenge[EAP_TTLS_MSCHAP_CHALLENGE_LEN];
548         *pos++ = data->ident;
549         *pos++ = 1; /* Flags: Use NT style passwords */
550         memset(pos, 0, 24); /* LM-Response */
551         pos += 24;
552         nt_challenge_response(challenge,
553                               config->password, config->password_len,
554                               pos); /* NT-Response */
555         wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: MSCHAP password",
556                               config->password, config->password_len);
557         wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP implicit challenge",
558                     challenge, EAP_TTLS_MSCHAP_CHALLENGE_LEN);
559         wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP response", pos, 24);
560         pos += 24;
561         free(challenge);
562         AVP_PAD(buf, pos);
563
564         *resp = buf;
565         *resp_len = pos - buf;
566
567         /* EAP-TTLS/MSCHAP does not provide tunneled success notification, so
568          * assume that Phase2 succeeds. */
569         ret->methodState = METHOD_DONE;
570         ret->decision = DECISION_COND_SUCC;
571
572         return 0;
573 }
574
575
576 static int eap_ttls_phase2_request_pap(struct eap_sm *sm,
577                                        struct eap_ttls_data *data,
578                                        struct eap_method_ret *ret,
579                                        struct eap_hdr *req,
580                                        struct eap_hdr *hdr,
581                                        u8 **resp, size_t *resp_len)
582 {
583         struct wpa_ssid *config = eap_get_config(sm);
584         u8 *buf, *pos;
585         size_t pad;
586
587         wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 PAP Request");
588
589         pos = buf = malloc(config->identity_len + config->password_len + 100);
590         if (buf == NULL) {
591                 wpa_printf(MSG_ERROR,
592                            "EAP-TTLS/PAP: Failed to allocate memory");
593                 return -1;
594         }
595
596         /* User-Name */
597         pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
598                                config->identity, config->identity_len);
599
600         /* User-Password; in RADIUS, this is encrypted, but EAP-TTLS encrypts
601          * the data, so no separate encryption is used in the AVP itself.
602          * However, the password is padded to obfuscate its length. */
603         pad = (16 - (config->password_len & 15)) & 15;
604         pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_USER_PASSWORD, 0, 1,
605                                config->password_len + pad);
606         memcpy(pos, config->password, config->password_len);
607         pos += config->password_len;
608         memset(pos, 0, pad);
609         pos += pad;
610         AVP_PAD(buf, pos);
611
612         *resp = buf;
613         *resp_len = pos - buf;
614
615         /* EAP-TTLS/PAP does not provide tunneled success notification, so
616          * assume that Phase2 succeeds. */
617         ret->methodState = METHOD_DONE;
618         ret->decision = DECISION_COND_SUCC;
619
620         return 0;
621 }
622
623
624 static int eap_ttls_phase2_request_chap(struct eap_sm *sm,
625                                         struct eap_ttls_data *data,
626                                         struct eap_method_ret *ret,
627                                         struct eap_hdr *req,
628                                         struct eap_hdr *hdr,
629                                         u8 **resp, size_t *resp_len)
630 {
631         struct wpa_ssid *config = eap_get_config(sm);
632         u8 *buf, *pos, *challenge;
633         MD5_CTX context;
634
635         wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 CHAP Request");
636
637         pos = buf = malloc(config->identity_len + 1000);
638         if (buf == NULL) {
639                 wpa_printf(MSG_ERROR,
640                            "EAP-TTLS/CHAP: Failed to allocate memory");
641                 return -1;
642         }
643
644         /* User-Name */
645         pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
646                                config->identity, config->identity_len);
647
648         /* CHAP-Challenge */
649         challenge = eap_tls_derive_key(sm, &data->ssl, "ttls challenge",
650                                        EAP_TLS_KEY_LEN);
651         if (challenge == NULL) {
652                 free(buf);
653                 wpa_printf(MSG_ERROR, "EAP-TTLS/CHAP: Failed to derive "
654                            "implicit challenge");
655                 return -1;
656         }
657
658         pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_CHAP_CHALLENGE, 0, 1,
659                                challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
660
661         /* CHAP-Password */
662         pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_CHAP_PASSWORD, 0, 1,
663                                1 + EAP_TTLS_CHAP_PASSWORD_LEN);
664         data->ident = challenge[EAP_TTLS_CHAP_CHALLENGE_LEN];
665         *pos++ = data->ident;
666
667         /* MD5(Ident + Password + Challenge) */
668         MD5Init(&context);
669         MD5Update(&context, &data->ident, 1);
670         MD5Update(&context, config->password, config->password_len);
671         MD5Update(&context, challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
672         MD5Final(pos, &context);
673
674         wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: CHAP username",
675                           config->identity, config->identity_len);
676         wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: CHAP password",
677                               config->password, config->password_len);
678         wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP implicit challenge",
679                     challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
680         wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP password",
681                     pos, EAP_TTLS_CHAP_PASSWORD_LEN);
682         pos += EAP_TTLS_CHAP_PASSWORD_LEN;
683         free(challenge);
684         AVP_PAD(buf, pos);
685
686         *resp = buf;
687         *resp_len = pos - buf;
688
689         /* EAP-TTLS/CHAP does not provide tunneled success notification, so
690          * assume that Phase2 succeeds. */
691         ret->methodState = METHOD_DONE;
692         ret->decision = DECISION_COND_SUCC;
693
694         return 0;
695 }
696
697
698 static int eap_ttls_phase2_request(struct eap_sm *sm,
699                                    struct eap_ttls_data *data,
700                                    struct eap_method_ret *ret,
701                                    struct eap_hdr *req,
702                                    struct eap_hdr *hdr,
703                                    u8 **resp, size_t *resp_len)
704 {
705         struct wpa_ssid *config = eap_get_config(sm);
706         int res = 0;
707
708         if (data->phase2_type == EAP_TTLS_PHASE2_MSCHAPV2 ||
709             data->phase2_type == EAP_TTLS_PHASE2_MSCHAP ||
710             data->phase2_type == EAP_TTLS_PHASE2_PAP ||
711             data->phase2_type == EAP_TTLS_PHASE2_CHAP) {
712                 if (config == NULL || config->identity == NULL) {
713                         wpa_printf(MSG_INFO,
714                                    "EAP-TTLS: Identity not configured");
715                         eap_sm_request_identity(sm, config);
716                         if (config->password == NULL)
717                                 eap_sm_request_password(sm, config);
718                         return 0;
719                 }
720
721                 if (config->password == NULL) {
722                         wpa_printf(MSG_INFO,
723                                    "EAP-TTLS: Password not configured");
724                         eap_sm_request_password(sm, config);
725                         return 0;
726                 }
727         }
728
729         switch (data->phase2_type) {
730         case EAP_TTLS_PHASE2_EAP:
731                 res = eap_ttls_phase2_request_eap(sm, data, ret, req, hdr,
732                                                   resp, resp_len);
733                 break;
734         case EAP_TTLS_PHASE2_MSCHAPV2:
735                 res = eap_ttls_phase2_request_mschapv2(sm, data, ret, req, hdr,
736                                                        resp, resp_len);
737                 break;
738         case EAP_TTLS_PHASE2_MSCHAP:
739                 res = eap_ttls_phase2_request_mschap(sm, data, ret, req, hdr,
740                                                      resp, resp_len);
741                 break;
742         case EAP_TTLS_PHASE2_PAP:
743                 res = eap_ttls_phase2_request_pap(sm, data, ret, req, hdr,
744                                                   resp, resp_len);
745                 break;
746         case EAP_TTLS_PHASE2_CHAP:
747                 res = eap_ttls_phase2_request_chap(sm, data, ret, req, hdr,
748                                                    resp, resp_len);
749                 break;
750         default:
751                 wpa_printf(MSG_ERROR, "EAP-TTLS: Phase 2 - Unknown");
752                 res = -1;
753                 break;
754         }
755
756         if (res < 0) {
757                 ret->methodState = METHOD_DONE;
758                 ret->decision = DECISION_FAIL;
759         }
760
761         return res;
762 }
763
764
765 static int eap_ttls_decrypt(struct eap_sm *sm, struct eap_ttls_data *data,
766                             struct eap_method_ret *ret, struct eap_hdr *req,
767                             u8 *in_data, size_t in_len,
768                             u8 **out_data, size_t *out_len)
769 {
770         u8 *in_decrypted = NULL, *pos;
771         int buf_len, len_decrypted = 0, len, left, retval = 0, res;
772         struct eap_hdr *hdr = NULL;
773         u8 *resp = NULL, *mschapv2 = NULL, *eapdata = NULL;
774         size_t resp_len, eap_len = 0;
775         struct ttls_avp *avp;
776         u8 recv_response[20];
777         int mschapv2_error = 0;
778         struct wpa_ssid *config = eap_get_config(sm);
779
780         wpa_printf(MSG_DEBUG, "EAP-TTLS: received %lu bytes encrypted data for"
781                    " Phase 2", (unsigned long) in_len);
782
783         if (data->pending_phase2_req) {
784                 wpa_printf(MSG_DEBUG, "EAP-TTLS: Pending Phase 2 request - "
785                            "skip decryption and use old data");
786                 /* Clear TLS reassembly state. */
787                 free(data->ssl.tls_in);
788                 data->ssl.tls_in = NULL;
789                 data->ssl.tls_in_len = 0;
790                 data->ssl.tls_in_left = 0;
791                 data->ssl.tls_in_total = 0;
792
793                 in_decrypted = data->pending_phase2_req;
794                 data->pending_phase2_req = NULL;
795                 len_decrypted = data->pending_phase2_req_len;
796                 if (data->pending_phase2_req_len == 0) {
797                         free(in_decrypted);
798                         in_decrypted = NULL;
799                         goto fake_req_identity;
800                 }
801                 goto continue_req;
802         }
803
804         if (in_len == 0 && data->phase2_start) {
805                 data->phase2_start = 0;
806                 /* EAP-TTLS does not use Phase2 on fast re-auth; this must be
807                  * done only if TLS part was indeed resuming a previous
808                  * session. Most Authentication Servers terminate EAP-TTLS
809                  * before reaching this point, but some do not. Make
810                  * wpa_supplicant stop phase 2 here, if needed. */
811                 if (data->reauth &&
812                     tls_connection_resumed(sm->ssl_ctx, data->ssl.conn)) {
813                         wpa_printf(MSG_DEBUG, "EAP-TTLS: Session resumption - "
814                                    "skip phase 2");
815                         *out_data = eap_tls_build_ack(&data->ssl, out_len,
816                                                       req->identifier,
817                                                       EAP_TYPE_TTLS, 0);
818                         ret->methodState = METHOD_DONE;
819                         ret->decision = DECISION_UNCOND_SUCC;
820                         data->phase2_success = 1;
821                         return 0;
822                 }
823         fake_req_identity:
824                 wpa_printf(MSG_DEBUG, "EAP-TTLS: empty data in beginning of "
825                            "Phase 2 - use fake EAP-Request Identity");
826                 buf_len = sizeof(*hdr) + 1;
827                 in_decrypted = malloc(buf_len);
828                 if (in_decrypted == NULL) {
829                         wpa_printf(MSG_WARNING, "EAP-TTLS: failed to allocate "
830                                    "memory for fake EAP-Identity Request");
831                         retval = -1;
832                         goto done;
833                 }
834                 hdr = (struct eap_hdr *) in_decrypted;
835                 hdr->code = EAP_CODE_REQUEST;
836                 hdr->identifier = 0;
837                 hdr->length = host_to_be16(sizeof(*hdr) + 1);
838                 in_decrypted[sizeof(*hdr)] = EAP_TYPE_IDENTITY;
839                 goto process_eap;
840         }
841
842         res = eap_tls_data_reassemble(sm, &data->ssl, &in_data, &in_len);
843         if (res < 0 || res == 1) {
844                 retval = res;
845                 goto done;
846         }
847
848         buf_len = in_len;
849         if (data->ssl.tls_in_total > buf_len)
850                 buf_len = data->ssl.tls_in_total;
851         in_decrypted = malloc(buf_len);
852         if (in_decrypted == NULL) {
853                 free(data->ssl.tls_in);
854                 data->ssl.tls_in = NULL;
855                 data->ssl.tls_in_len = 0;
856                 wpa_printf(MSG_WARNING, "EAP-TTLS: failed to allocate memory "
857                            "for decryption");
858                 retval = -1;
859                 goto done;
860         }
861
862         len_decrypted = tls_connection_decrypt(sm->ssl_ctx, data->ssl.conn,
863                                                in_data, in_len,
864                                                in_decrypted, buf_len);
865         free(data->ssl.tls_in);
866         data->ssl.tls_in = NULL;
867         data->ssl.tls_in_len = 0;
868         if (len_decrypted < 0) {
869                 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to decrypt Phase 2 "
870                            "data");
871                 retval = -1;
872                 goto done;
873         }
874
875 continue_req:
876         data->phase2_start = 0;
877
878         wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Decrypted Phase 2 AVPs",
879                     in_decrypted, len_decrypted);
880         if (len_decrypted < sizeof(struct ttls_avp)) {
881                 wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 AVP frame"
882                            " len=%d expected %lu or more - dropped",
883                            len_decrypted,
884                            (unsigned long) sizeof(struct ttls_avp));
885                 retval = -1;
886                 goto done;
887         }
888
889         /* Parse AVPs */
890         pos = in_decrypted;
891         left = len_decrypted;
892         mschapv2 = NULL;
893
894         while (left > 0) {
895                 u32 avp_code, avp_length, vendor_id = 0;
896                 u8 avp_flags, *dpos;
897                 size_t pad, dlen;
898                 avp = (struct ttls_avp *) pos;
899                 avp_code = be_to_host32(avp->avp_code);
900                 avp_length = be_to_host32(avp->avp_length);
901                 avp_flags = (avp_length >> 24) & 0xff;
902                 avp_length &= 0xffffff;
903                 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP: code=%d flags=0x%02x "
904                            "length=%d", (int) avp_code, avp_flags,
905                            (int) avp_length);
906                 if (avp_length > left) {
907                         wpa_printf(MSG_WARNING, "EAP-TTLS: AVP overflow "
908                                    "(len=%d, left=%d) - dropped",
909                                    (int) avp_length, left);
910                         retval = -1;
911                         goto done;
912                 }
913                 dpos = (u8 *) (avp + 1);
914                 dlen = avp_length - sizeof(*avp);
915                 if (avp_flags & AVP_FLAGS_VENDOR) {
916                         if (dlen < 4) {
917                                 wpa_printf(MSG_WARNING, "EAP-TTLS: vendor AVP "
918                                            "underflow");
919                                 retval = -1;
920                                 goto done;
921                         }
922                         vendor_id = be_to_host32(* (u32 *) dpos);
923                         wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP vendor_id %d",
924                                    (int) vendor_id);
925                         dpos += 4;
926                         dlen -= 4;
927                 }
928
929                 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: AVP data", dpos, dlen);
930
931                 if (vendor_id == 0 && avp_code == RADIUS_ATTR_EAP_MESSAGE) {
932                         wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP - EAP Message");
933                         if (eapdata == NULL) {
934                                 eapdata = malloc(dlen);
935                                 if (eapdata == NULL) {
936                                         retval = -1;
937                                         wpa_printf(MSG_WARNING, "EAP-TTLS: "
938                                                    "failed to allocate memory "
939                                                    "for Phase 2 EAP data");
940                                         goto done;
941                                 }
942                                 memcpy(eapdata, dpos, dlen);
943                                 eap_len = dlen;
944                         } else {
945                                 u8 *neweap = realloc(eapdata, eap_len + dlen);
946                                 if (neweap == NULL) {
947                                         retval = -1;
948                                         wpa_printf(MSG_WARNING, "EAP-TTLS: "
949                                                    "failed to allocate memory "
950                                                    "for Phase 2 EAP data");
951                                         goto done;
952                                 }
953                                 memcpy(neweap + eap_len, dpos, dlen);
954                                 eapdata = neweap;
955                                 eap_len += dlen;
956                         }
957                 } else if (vendor_id == 0 &&
958                            avp_code == RADIUS_ATTR_REPLY_MESSAGE) {
959                         /* This is an optional message that can be displayed to
960                          * the user. */
961                         wpa_hexdump_ascii(MSG_DEBUG,
962                                           "EAP-TTLS: AVP - Reply-Message",
963                                           dpos, dlen);
964                 } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
965                            avp_code == RADIUS_ATTR_MS_CHAP2_SUCCESS) {
966                         wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: "
967                                           "MS-CHAP2-Success", dpos, dlen);
968                         if (dlen != 43) {
969                                 wpa_printf(MSG_WARNING, "EAP-TTLS: Unexpected "
970                                            "MS-CHAP2-Success length "
971                                            "(len=%lu, expected 43)",
972                                            (unsigned long) dlen);
973                                 retval = -1;
974                                 break;
975                         }
976                         mschapv2 = dpos;
977                 } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
978                            avp_code == RADIUS_ATTR_MS_CHAP_ERROR) {
979                         wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: "
980                                           "MS-CHAP-Error", dpos, dlen);
981                         mschapv2_error = 1;
982                 } else if (avp_flags & AVP_FLAGS_MANDATORY) {
983                         wpa_printf(MSG_WARNING, "EAP-TTLS: Unsupported "
984                                    "mandatory AVP code %d vendor_id %d - "
985                                    "dropped", (int) avp_code, (int) vendor_id);
986                         retval = -1;
987                         goto done;
988                 } else {
989                         wpa_printf(MSG_DEBUG, "EAP-TTLS: Ignoring unsupported "
990                                    "AVP code %d vendor_id %d",
991                                    (int) avp_code, (int) vendor_id);
992                 }
993
994                 pad = (4 - (avp_length & 3)) & 3;
995                 pos += avp_length + pad;
996                 left -= avp_length + pad;
997         }
998
999         switch (data->phase2_type) {
1000         case EAP_TTLS_PHASE2_EAP:
1001                 if (eapdata == NULL) {
1002                         wpa_printf(MSG_WARNING, "EAP-TTLS: No EAP Message in "
1003                                    "the packet - dropped");
1004                         retval = -1;
1005                         goto done;
1006                 }
1007
1008                 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP",
1009                             eapdata, eap_len);
1010                 hdr = (struct eap_hdr *) eapdata;
1011
1012                 if (eap_len < sizeof(*hdr)) {
1013                         wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 "
1014                                    "EAP frame (len=%lu, expected %lu or more) "
1015                                    "- dropped", (unsigned long) eap_len,
1016                                    (unsigned long) sizeof(*hdr));
1017                         retval = -1;
1018                         goto done;
1019                 }
1020                 len = be_to_host16(hdr->length);
1021                 if (len > eap_len) {
1022                         wpa_printf(MSG_INFO, "EAP-TTLS: Length mismatch in "
1023                                    "Phase 2 EAP frame (EAP hdr len=%d, EAP "
1024                                    "data len in AVP=%lu)", len,
1025                                    (unsigned long) eap_len);
1026                         retval = -1;
1027                         goto done;
1028                 }
1029                 wpa_printf(MSG_DEBUG, "EAP-TTLS: received Phase 2: code=%d "
1030                            "identifier=%d length=%d",
1031                            hdr->code, hdr->identifier, len);
1032         process_eap:
1033                 switch (hdr->code) {
1034                 case EAP_CODE_REQUEST:
1035                         if (eap_ttls_phase2_request(sm, data, ret, req, hdr,
1036                                                     &resp, &resp_len)) {
1037                                 wpa_printf(MSG_INFO, "EAP-TTLS: Phase2 "
1038                                            "Request processing failed");
1039                                 retval = -1;
1040                                 goto done;
1041                         }
1042                         break;
1043                 default:
1044                         wpa_printf(MSG_INFO, "EAP-TTLS: Unexpected code=%d in "
1045                                    "Phase 2 EAP header", hdr->code);
1046                         retval = -1;
1047                         break;
1048                 }
1049                 break;
1050         case EAP_TTLS_PHASE2_MSCHAPV2:
1051                 if (mschapv2_error) {
1052                         wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Received "
1053                                    "MS-CHAP-Error - failed");
1054                         ret->methodState = METHOD_DONE;
1055                         ret->decision = DECISION_FAIL;
1056                         *out_data = eap_tls_build_ack(&data->ssl, out_len,
1057                                                       req->identifier,
1058                                                       EAP_TYPE_TTLS, 0);
1059                         break;
1060                 }
1061
1062                 if (mschapv2 == NULL) {
1063                         wpa_printf(MSG_WARNING, "EAP-TTLS: no MS-CHAP2-Success"
1064                                    " AVP received for Phase2 MSCHAPV2");
1065                         retval = -1;
1066                         break;
1067                 }
1068                 if (mschapv2[0] != data->ident) {
1069                         wpa_printf(MSG_WARNING, "EAP-TTLS: Ident mismatch "
1070                                    "for Phase 2 MSCHAPV2 (received Ident "
1071                                    "0x%02x, expected 0x%02x)",
1072                                    mschapv2[0], data->ident);
1073                         retval = -1;
1074                         break;
1075                 }
1076                 if (!data->auth_response_valid ||
1077                     mschapv2[1] != 'S' || mschapv2[2] != '=' ||
1078                     hexstr2bin((char *) (mschapv2 + 3), recv_response, 20) ||
1079                     memcmp(data->auth_response, recv_response, 20) != 0) {
1080                         wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid "
1081                                    "authenticator response in Phase 2 "
1082                                    "MSCHAPV2 success request");
1083                         retval = -1;
1084                         break;
1085                 }
1086
1087                 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 MSCHAPV2 "
1088                            "authentication succeeded");
1089                 ret->methodState = METHOD_DONE;
1090                 ret->decision = DECISION_UNCOND_SUCC;
1091                 data->phase2_success = 1;
1092
1093                 /* Reply with empty data; authentication server will reply
1094                  * with EAP-Success after this. */
1095                 retval = 1;
1096                 goto done;
1097         case EAP_TTLS_PHASE2_MSCHAP:
1098         case EAP_TTLS_PHASE2_PAP:
1099         case EAP_TTLS_PHASE2_CHAP:
1100                 /* EAP-TTLS/{MSCHAP,PAP,CHAP} should not send any TLS tunneled
1101                  * requests to the supplicant */
1102                 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received unexpected "
1103                            "tunneled data");
1104                 retval = -1;
1105                 break;
1106         }
1107
1108         if (resp) {
1109                 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Encrypting Phase 2 data",
1110                                 resp, resp_len);
1111
1112                 if (eap_ttls_encrypt(sm, data, req->identifier,
1113                                      resp, resp_len, out_data, out_len)) {
1114                         wpa_printf(MSG_INFO, "EAP-TTLS: Failed to encrypt "
1115                                    "a Phase 2 frame");
1116                 }
1117                 free(resp);
1118         } else if (config->pending_req_identity ||
1119                    config->pending_req_password ||
1120                    config->pending_req_otp) {
1121                 free(data->pending_phase2_req);
1122                 data->pending_phase2_req = malloc(len_decrypted);
1123                 if (data->pending_phase2_req) {
1124                         memcpy(data->pending_phase2_req, in_decrypted,
1125                                len_decrypted);
1126                         data->pending_phase2_req_len = len_decrypted;
1127                 }
1128         }
1129
1130 done:
1131         free(in_decrypted);
1132         free(eapdata);
1133
1134         if (retval < 0) {
1135                 ret->methodState = METHOD_DONE;
1136                 ret->decision = DECISION_FAIL;
1137         }
1138
1139         return retval;
1140 }
1141
1142
1143 static u8 * eap_ttls_process(struct eap_sm *sm, void *priv,
1144                              struct eap_method_ret *ret,
1145                              u8 *reqData, size_t reqDataLen,
1146                              size_t *respDataLen)
1147 {
1148         struct eap_hdr *req;
1149         int left, res;
1150         unsigned int tls_msg_len;
1151         u8 flags, *pos, *resp, id;
1152         struct eap_ttls_data *data = priv;
1153
1154         if (tls_get_errors(sm->ssl_ctx)) {
1155                 wpa_printf(MSG_INFO, "EAP-TTLS: TLS errors detected");
1156                 ret->ignore = TRUE;
1157                 return NULL;
1158         }
1159
1160         req = (struct eap_hdr *) reqData;
1161         pos = (u8 *) (req + 1);
1162         if (reqDataLen < sizeof(*req) + 2 || *pos != EAP_TYPE_TTLS ||
1163             (left = be_to_host16(req->length)) > reqDataLen) {
1164                 wpa_printf(MSG_INFO, "EAP-TTLS: Invalid frame");
1165                 ret->ignore = TRUE;
1166                 return NULL;
1167         }
1168         left -= sizeof(struct eap_hdr);
1169         id = req->identifier;
1170         pos++;
1171         flags = *pos++;
1172         left -= 2;
1173         wpa_printf(MSG_DEBUG, "EAP-TTLS: Received packet(len=%lu) - "
1174                    "Flags 0x%02x", (unsigned long) reqDataLen, flags);
1175         if (flags & EAP_TLS_FLAGS_LENGTH_INCLUDED) {
1176                 if (left < 4) {
1177                         wpa_printf(MSG_INFO, "EAP-TTLS: Short frame with TLS "
1178                                    "length");
1179                         ret->ignore = TRUE;
1180                         return NULL;
1181                 }
1182                 tls_msg_len = (pos[0] << 24) | (pos[1] << 16) | (pos[2] << 8) |
1183                         pos[3];
1184                 wpa_printf(MSG_DEBUG, "EAP-TTLS: TLS Message Length: %d",
1185                            tls_msg_len);
1186                 if (data->ssl.tls_in_left == 0) {
1187                         data->ssl.tls_in_total = tls_msg_len;
1188                         data->ssl.tls_in_left = tls_msg_len;
1189                         free(data->ssl.tls_in);
1190                         data->ssl.tls_in = NULL;
1191                         data->ssl.tls_in_len = 0;
1192                 }
1193                 pos += 4;
1194                 left -= 4;
1195         }
1196
1197         ret->ignore = FALSE;
1198         ret->methodState = METHOD_CONT;
1199         ret->decision = DECISION_FAIL;
1200         ret->allowNotifications = TRUE;
1201
1202         if (flags & EAP_TLS_FLAGS_START) {
1203                 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start");
1204                 /* draft-ietf-pppext-eap-ttls-03.txt, Ch. 8.1:
1205                  * EAP-TTLS Start packet may, in a future specification, be
1206                  * allowed to contain data. Client based on this draft version
1207                  * must ignore such data but must not reject the Start packet.
1208                  */
1209                 left = 0;
1210         }
1211
1212         resp = NULL;
1213         if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1214             !data->resuming) {
1215                 res = eap_ttls_decrypt(sm, data, ret, req, pos, left,
1216                                        &resp, respDataLen);
1217         } else {
1218                 res = eap_tls_process_helper(sm, &data->ssl, EAP_TYPE_TTLS, 0,
1219                                              id, pos, left,
1220                                              &resp, respDataLen);
1221
1222                 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
1223                         wpa_printf(MSG_DEBUG,
1224                                    "EAP-TTLS: TLS done, proceed to Phase 2");
1225                         if (data->resuming) {
1226                                 wpa_printf(MSG_DEBUG, "EAP-TTLS: fast reauth -"
1227                                            " may skip Phase 2");
1228                                 ret->decision = DECISION_COND_SUCC;
1229                                 ret->methodState = METHOD_MAY_CONT;
1230                         }
1231                         data->phase2_start = 1;
1232                         free(data->key_data);
1233                         data->key_data =
1234                                 eap_tls_derive_key(sm, &data->ssl,
1235                                                    "ttls keying material",
1236                                                    EAP_TLS_KEY_LEN);
1237                         if (data->key_data) {
1238                                 wpa_hexdump_key(MSG_DEBUG,
1239                                                 "EAP-TTLS: Derived key",
1240                                                 data->key_data,
1241                                                 EAP_TLS_KEY_LEN);
1242                         } else {
1243                                 wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to "
1244                                            "derive key");
1245                         }
1246
1247                         if (*respDataLen == 0) {
1248                                 if (eap_ttls_decrypt(sm, data, ret, req, NULL,
1249                                                      0, &resp, respDataLen)) {
1250                                         wpa_printf(MSG_WARNING, "EAP-TTLS: "
1251                                                    "failed to process early "
1252                                                    "start for Phase 2");
1253                                 }
1254                                 res = 0;
1255                         }
1256                         data->resuming = 0;
1257                 }
1258         }
1259
1260         if (ret->methodState == METHOD_DONE) {
1261                 ret->allowNotifications = FALSE;
1262                 if (ret->decision == DECISION_UNCOND_SUCC ||
1263                     ret->decision == DECISION_COND_SUCC) {
1264                         wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
1265                                    "completed successfully");
1266                         data->phase2_success = 1;
1267                 }
1268         } else if (sm->workaround && ret->methodState == METHOD_MAY_CONT &&
1269                    (ret->decision == DECISION_UNCOND_SUCC ||
1270                     ret->decision == DECISION_COND_SUCC)) {
1271                         wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
1272                                    "completed successfully (EAP workaround)");
1273                         data->phase2_success = 1;
1274         }
1275
1276         if (res == 1) {
1277                 return eap_tls_build_ack(&data->ssl, respDataLen, id,
1278                                          EAP_TYPE_TTLS, 0);
1279         }
1280         return resp;
1281 }
1282
1283
1284 static Boolean eap_ttls_has_reauth_data(struct eap_sm *sm, void *priv)
1285 {
1286         struct eap_ttls_data *data = priv;
1287         return tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1288                 data->phase2_success;
1289 }
1290
1291
1292 static void eap_ttls_deinit_for_reauth(struct eap_sm *sm, void *priv)
1293 {
1294         struct eap_ttls_data *data = priv;
1295         free(data->pending_phase2_req);
1296         data->pending_phase2_req = NULL;
1297 }
1298
1299
1300 static void * eap_ttls_init_for_reauth(struct eap_sm *sm, void *priv)
1301 {
1302         struct eap_ttls_data *data = priv;
1303         free(data->key_data);
1304         data->key_data = NULL;
1305         if (eap_tls_reauth_init(sm, &data->ssl)) {
1306                 free(data);
1307                 return NULL;
1308         }
1309         data->phase2_start = 0;
1310         data->phase2_success = 0;
1311         data->resuming = 1;
1312         data->reauth = 1;
1313         return priv;
1314 }
1315
1316
1317 static int eap_ttls_get_status(struct eap_sm *sm, void *priv, char *buf,
1318                                size_t buflen, int verbose)
1319 {
1320         struct eap_ttls_data *data = priv;
1321         int len;
1322
1323         len = eap_tls_status(sm, &data->ssl, buf, buflen, verbose);
1324         switch (data->phase2_type) {
1325         case EAP_TTLS_PHASE2_EAP:
1326                 len += snprintf(buf + len, buflen - len,
1327                                 "EAP-TTLS Phase2 method=EAP-%s\n",
1328                                 data->phase2_method ? data->phase2_method->name
1329                                 : "?");
1330                 break;
1331         case EAP_TTLS_PHASE2_MSCHAPV2:
1332                 len += snprintf(buf + len, buflen - len,
1333                                 "EAP-TTLS Phase2 method=MSCHAPV2\n");
1334                 break;
1335         case EAP_TTLS_PHASE2_MSCHAP:
1336                 len += snprintf(buf + len, buflen - len,
1337                                 "EAP-TTLS Phase2 method=MSCHAP\n");
1338                 break;
1339         case EAP_TTLS_PHASE2_PAP:
1340                 len += snprintf(buf + len, buflen - len,
1341                                 "EAP-TTLS Phase2 method=PAP\n");
1342                 break;
1343         case EAP_TTLS_PHASE2_CHAP:
1344                 len += snprintf(buf + len, buflen - len,
1345                                 "EAP-TTLS Phase2 method=CHAP\n");
1346                 break;
1347         }
1348
1349         return len;
1350 }
1351
1352
1353 static Boolean eap_ttls_isKeyAvailable(struct eap_sm *sm, void *priv)
1354 {
1355         struct eap_ttls_data *data = priv;
1356         return data->key_data != NULL && data->phase2_success;
1357 }
1358
1359
1360 static u8 * eap_ttls_getKey(struct eap_sm *sm, void *priv, size_t *len)
1361 {
1362         struct eap_ttls_data *data = priv;
1363         u8 *key;
1364
1365         if (data->key_data == NULL || !data->phase2_success)
1366                 return NULL;
1367
1368         key = malloc(EAP_TLS_KEY_LEN);
1369         if (key == NULL)
1370                 return NULL;
1371
1372         *len = EAP_TLS_KEY_LEN;
1373         memcpy(key, data->key_data, EAP_TLS_KEY_LEN);
1374
1375         return key;
1376 }
1377
1378
1379 const struct eap_method eap_method_ttls =
1380 {
1381         .method = EAP_TYPE_TTLS,
1382         .name = "TTLS",
1383         .init = eap_ttls_init,
1384         .deinit = eap_ttls_deinit,
1385         .process = eap_ttls_process,
1386         .isKeyAvailable = eap_ttls_isKeyAvailable,
1387         .getKey = eap_ttls_getKey,
1388         .get_status = eap_ttls_get_status,
1389         .has_reauth_data = eap_ttls_has_reauth_data,
1390         .deinit_for_reauth = eap_ttls_deinit_for_reauth,
1391         .init_for_reauth = eap_ttls_init_for_reauth,
1392 };