]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/wpa_supplicant/eap_ttls.c
This commit was generated by cvs2svn to compensate for changes in r169942,
[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 "crypto.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, const 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                                        const 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                                             const 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                                           const 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                                        const 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                                         const 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         const u8 *addr[3];
634         size_t len[3];
635
636         wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 CHAP Request");
637
638         pos = buf = malloc(config->identity_len + 1000);
639         if (buf == NULL) {
640                 wpa_printf(MSG_ERROR,
641                            "EAP-TTLS/CHAP: Failed to allocate memory");
642                 return -1;
643         }
644
645         /* User-Name */
646         pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
647                                config->identity, config->identity_len);
648
649         /* CHAP-Challenge */
650         challenge = eap_tls_derive_key(sm, &data->ssl, "ttls challenge",
651                                        EAP_TLS_KEY_LEN);
652         if (challenge == NULL) {
653                 free(buf);
654                 wpa_printf(MSG_ERROR, "EAP-TTLS/CHAP: Failed to derive "
655                            "implicit challenge");
656                 return -1;
657         }
658
659         pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_CHAP_CHALLENGE, 0, 1,
660                                challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
661
662         /* CHAP-Password */
663         pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_CHAP_PASSWORD, 0, 1,
664                                1 + EAP_TTLS_CHAP_PASSWORD_LEN);
665         data->ident = challenge[EAP_TTLS_CHAP_CHALLENGE_LEN];
666         *pos++ = data->ident;
667
668         /* MD5(Ident + Password + Challenge) */
669         addr[0] = &data->ident;
670         len[0] = 1;
671         addr[1] = config->password;
672         len[1] = config->password_len;
673         addr[2] = challenge;
674         len[2] = EAP_TTLS_CHAP_CHALLENGE_LEN;
675         md5_vector(3, addr, len, pos);
676
677         wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: CHAP username",
678                           config->identity, config->identity_len);
679         wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: CHAP password",
680                               config->password, config->password_len);
681         wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP implicit challenge",
682                     challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
683         wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP password",
684                     pos, EAP_TTLS_CHAP_PASSWORD_LEN);
685         pos += EAP_TTLS_CHAP_PASSWORD_LEN;
686         free(challenge);
687         AVP_PAD(buf, pos);
688
689         *resp = buf;
690         *resp_len = pos - buf;
691
692         /* EAP-TTLS/CHAP does not provide tunneled success notification, so
693          * assume that Phase2 succeeds. */
694         ret->methodState = METHOD_DONE;
695         ret->decision = DECISION_COND_SUCC;
696
697         return 0;
698 }
699
700
701 static int eap_ttls_phase2_request(struct eap_sm *sm,
702                                    struct eap_ttls_data *data,
703                                    struct eap_method_ret *ret,
704                                    const struct eap_hdr *req,
705                                    struct eap_hdr *hdr,
706                                    u8 **resp, size_t *resp_len)
707 {
708         struct wpa_ssid *config = eap_get_config(sm);
709         int res = 0;
710
711         if (data->phase2_type == EAP_TTLS_PHASE2_MSCHAPV2 ||
712             data->phase2_type == EAP_TTLS_PHASE2_MSCHAP ||
713             data->phase2_type == EAP_TTLS_PHASE2_PAP ||
714             data->phase2_type == EAP_TTLS_PHASE2_CHAP) {
715                 if (config == NULL || config->identity == NULL) {
716                         wpa_printf(MSG_INFO,
717                                    "EAP-TTLS: Identity not configured");
718                         eap_sm_request_identity(sm, config);
719                         if (config->password == NULL)
720                                 eap_sm_request_password(sm, config);
721                         return 0;
722                 }
723
724                 if (config->password == NULL) {
725                         wpa_printf(MSG_INFO,
726                                    "EAP-TTLS: Password not configured");
727                         eap_sm_request_password(sm, config);
728                         return 0;
729                 }
730         }
731
732         switch (data->phase2_type) {
733         case EAP_TTLS_PHASE2_EAP:
734                 res = eap_ttls_phase2_request_eap(sm, data, ret, req, hdr,
735                                                   resp, resp_len);
736                 break;
737         case EAP_TTLS_PHASE2_MSCHAPV2:
738                 res = eap_ttls_phase2_request_mschapv2(sm, data, ret, req, hdr,
739                                                        resp, resp_len);
740                 break;
741         case EAP_TTLS_PHASE2_MSCHAP:
742                 res = eap_ttls_phase2_request_mschap(sm, data, ret, req, hdr,
743                                                      resp, resp_len);
744                 break;
745         case EAP_TTLS_PHASE2_PAP:
746                 res = eap_ttls_phase2_request_pap(sm, data, ret, req, hdr,
747                                                   resp, resp_len);
748                 break;
749         case EAP_TTLS_PHASE2_CHAP:
750                 res = eap_ttls_phase2_request_chap(sm, data, ret, req, hdr,
751                                                    resp, resp_len);
752                 break;
753         default:
754                 wpa_printf(MSG_ERROR, "EAP-TTLS: Phase 2 - Unknown");
755                 res = -1;
756                 break;
757         }
758
759         if (res < 0) {
760                 ret->methodState = METHOD_DONE;
761                 ret->decision = DECISION_FAIL;
762         }
763
764         return res;
765 }
766
767
768 static int eap_ttls_decrypt(struct eap_sm *sm, struct eap_ttls_data *data,
769                             struct eap_method_ret *ret,
770                             const struct eap_hdr *req,
771                             const u8 *in_data, size_t in_len,
772                             u8 **out_data, size_t *out_len)
773 {
774         u8 *in_decrypted = NULL, *pos;
775         int buf_len, len_decrypted = 0, len, left, retval = 0;
776         struct eap_hdr *hdr = NULL;
777         u8 *resp = NULL, *mschapv2 = NULL, *eapdata = NULL;
778         size_t resp_len, eap_len = 0;
779         struct ttls_avp *avp;
780         u8 recv_response[20];
781         int mschapv2_error = 0;
782         struct wpa_ssid *config = eap_get_config(sm);
783         const u8 *msg;
784         size_t msg_len;
785         int need_more_input;
786
787         wpa_printf(MSG_DEBUG, "EAP-TTLS: received %lu bytes encrypted data for"
788                    " Phase 2", (unsigned long) in_len);
789
790         if (data->pending_phase2_req) {
791                 wpa_printf(MSG_DEBUG, "EAP-TTLS: Pending Phase 2 request - "
792                            "skip decryption and use old data");
793                 /* Clear TLS reassembly state. */
794                 free(data->ssl.tls_in);
795                 data->ssl.tls_in = NULL;
796                 data->ssl.tls_in_len = 0;
797                 data->ssl.tls_in_left = 0;
798                 data->ssl.tls_in_total = 0;
799
800                 in_decrypted = data->pending_phase2_req;
801                 data->pending_phase2_req = NULL;
802                 len_decrypted = data->pending_phase2_req_len;
803                 if (data->pending_phase2_req_len == 0) {
804                         free(in_decrypted);
805                         in_decrypted = NULL;
806                         goto fake_req_identity;
807                 }
808                 goto continue_req;
809         }
810
811         if (in_len == 0 && data->phase2_start) {
812                 data->phase2_start = 0;
813                 /* EAP-TTLS does not use Phase2 on fast re-auth; this must be
814                  * done only if TLS part was indeed resuming a previous
815                  * session. Most Authentication Servers terminate EAP-TTLS
816                  * before reaching this point, but some do not. Make
817                  * wpa_supplicant stop phase 2 here, if needed. */
818                 if (data->reauth &&
819                     tls_connection_resumed(sm->ssl_ctx, data->ssl.conn)) {
820                         wpa_printf(MSG_DEBUG, "EAP-TTLS: Session resumption - "
821                                    "skip phase 2");
822                         *out_data = eap_tls_build_ack(&data->ssl, out_len,
823                                                       req->identifier,
824                                                       EAP_TYPE_TTLS, 0);
825                         ret->methodState = METHOD_DONE;
826                         ret->decision = DECISION_UNCOND_SUCC;
827                         data->phase2_success = 1;
828                         return 0;
829                 }
830         fake_req_identity:
831                 wpa_printf(MSG_DEBUG, "EAP-TTLS: empty data in beginning of "
832                            "Phase 2 - use fake EAP-Request Identity");
833                 buf_len = sizeof(*hdr) + 1;
834                 in_decrypted = malloc(buf_len);
835                 if (in_decrypted == NULL) {
836                         wpa_printf(MSG_WARNING, "EAP-TTLS: failed to allocate "
837                                    "memory for fake EAP-Identity Request");
838                         retval = -1;
839                         goto done;
840                 }
841                 hdr = (struct eap_hdr *) in_decrypted;
842                 hdr->code = EAP_CODE_REQUEST;
843                 hdr->identifier = 0;
844                 hdr->length = host_to_be16(sizeof(*hdr) + 1);
845                 in_decrypted[sizeof(*hdr)] = EAP_TYPE_IDENTITY;
846                 goto process_eap;
847         }
848
849         msg = eap_tls_data_reassemble(sm, &data->ssl, in_data, in_len,
850                                       &msg_len, &need_more_input);
851         if (msg == NULL)
852                 return need_more_input ? 1 : -1;
853
854         buf_len = in_len;
855         if (data->ssl.tls_in_total > buf_len)
856                 buf_len = data->ssl.tls_in_total;
857         in_decrypted = malloc(buf_len);
858         if (in_decrypted == NULL) {
859                 free(data->ssl.tls_in);
860                 data->ssl.tls_in = NULL;
861                 data->ssl.tls_in_len = 0;
862                 wpa_printf(MSG_WARNING, "EAP-TTLS: failed to allocate memory "
863                            "for decryption");
864                 retval = -1;
865                 goto done;
866         }
867
868         len_decrypted = tls_connection_decrypt(sm->ssl_ctx, data->ssl.conn,
869                                                msg, msg_len,
870                                                in_decrypted, buf_len);
871         free(data->ssl.tls_in);
872         data->ssl.tls_in = NULL;
873         data->ssl.tls_in_len = 0;
874         if (len_decrypted < 0) {
875                 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to decrypt Phase 2 "
876                            "data");
877                 retval = -1;
878                 goto done;
879         }
880
881 continue_req:
882         data->phase2_start = 0;
883
884         wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Decrypted Phase 2 AVPs",
885                     in_decrypted, len_decrypted);
886         if (len_decrypted < sizeof(struct ttls_avp)) {
887                 wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 AVP frame"
888                            " len=%d expected %lu or more - dropped",
889                            len_decrypted,
890                            (unsigned long) sizeof(struct ttls_avp));
891                 retval = -1;
892                 goto done;
893         }
894
895         /* Parse AVPs */
896         pos = in_decrypted;
897         left = len_decrypted;
898         mschapv2 = NULL;
899
900         while (left > 0) {
901                 u32 avp_code, avp_length, vendor_id = 0;
902                 u8 avp_flags, *dpos;
903                 size_t pad, dlen;
904                 avp = (struct ttls_avp *) pos;
905                 avp_code = be_to_host32(avp->avp_code);
906                 avp_length = be_to_host32(avp->avp_length);
907                 avp_flags = (avp_length >> 24) & 0xff;
908                 avp_length &= 0xffffff;
909                 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP: code=%d flags=0x%02x "
910                            "length=%d", (int) avp_code, avp_flags,
911                            (int) avp_length);
912                 if (avp_length > left) {
913                         wpa_printf(MSG_WARNING, "EAP-TTLS: AVP overflow "
914                                    "(len=%d, left=%d) - dropped",
915                                    (int) avp_length, left);
916                         retval = -1;
917                         goto done;
918                 }
919                 dpos = (u8 *) (avp + 1);
920                 dlen = avp_length - sizeof(*avp);
921                 if (avp_flags & AVP_FLAGS_VENDOR) {
922                         if (dlen < 4) {
923                                 wpa_printf(MSG_WARNING, "EAP-TTLS: vendor AVP "
924                                            "underflow");
925                                 retval = -1;
926                                 goto done;
927                         }
928                         vendor_id = be_to_host32(* (u32 *) dpos);
929                         wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP vendor_id %d",
930                                    (int) vendor_id);
931                         dpos += 4;
932                         dlen -= 4;
933                 }
934
935                 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: AVP data", dpos, dlen);
936
937                 if (vendor_id == 0 && avp_code == RADIUS_ATTR_EAP_MESSAGE) {
938                         wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP - EAP Message");
939                         if (eapdata == NULL) {
940                                 eapdata = malloc(dlen);
941                                 if (eapdata == NULL) {
942                                         retval = -1;
943                                         wpa_printf(MSG_WARNING, "EAP-TTLS: "
944                                                    "failed to allocate memory "
945                                                    "for Phase 2 EAP data");
946                                         goto done;
947                                 }
948                                 memcpy(eapdata, dpos, dlen);
949                                 eap_len = dlen;
950                         } else {
951                                 u8 *neweap = realloc(eapdata, eap_len + dlen);
952                                 if (neweap == NULL) {
953                                         retval = -1;
954                                         wpa_printf(MSG_WARNING, "EAP-TTLS: "
955                                                    "failed to allocate memory "
956                                                    "for Phase 2 EAP data");
957                                         goto done;
958                                 }
959                                 memcpy(neweap + eap_len, dpos, dlen);
960                                 eapdata = neweap;
961                                 eap_len += dlen;
962                         }
963                 } else if (vendor_id == 0 &&
964                            avp_code == RADIUS_ATTR_REPLY_MESSAGE) {
965                         /* This is an optional message that can be displayed to
966                          * the user. */
967                         wpa_hexdump_ascii(MSG_DEBUG,
968                                           "EAP-TTLS: AVP - Reply-Message",
969                                           dpos, dlen);
970                 } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
971                            avp_code == RADIUS_ATTR_MS_CHAP2_SUCCESS) {
972                         wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: "
973                                           "MS-CHAP2-Success", dpos, dlen);
974                         if (dlen != 43) {
975                                 wpa_printf(MSG_WARNING, "EAP-TTLS: Unexpected "
976                                            "MS-CHAP2-Success length "
977                                            "(len=%lu, expected 43)",
978                                            (unsigned long) dlen);
979                                 retval = -1;
980                                 break;
981                         }
982                         mschapv2 = dpos;
983                 } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
984                            avp_code == RADIUS_ATTR_MS_CHAP_ERROR) {
985                         wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: "
986                                           "MS-CHAP-Error", dpos, dlen);
987                         mschapv2_error = 1;
988                 } else if (avp_flags & AVP_FLAGS_MANDATORY) {
989                         wpa_printf(MSG_WARNING, "EAP-TTLS: Unsupported "
990                                    "mandatory AVP code %d vendor_id %d - "
991                                    "dropped", (int) avp_code, (int) vendor_id);
992                         retval = -1;
993                         goto done;
994                 } else {
995                         wpa_printf(MSG_DEBUG, "EAP-TTLS: Ignoring unsupported "
996                                    "AVP code %d vendor_id %d",
997                                    (int) avp_code, (int) vendor_id);
998                 }
999
1000                 pad = (4 - (avp_length & 3)) & 3;
1001                 pos += avp_length + pad;
1002                 left -= avp_length + pad;
1003         }
1004
1005         switch (data->phase2_type) {
1006         case EAP_TTLS_PHASE2_EAP:
1007                 if (eapdata == NULL) {
1008                         wpa_printf(MSG_WARNING, "EAP-TTLS: No EAP Message in "
1009                                    "the packet - dropped");
1010                         retval = -1;
1011                         goto done;
1012                 }
1013
1014                 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP",
1015                             eapdata, eap_len);
1016                 hdr = (struct eap_hdr *) eapdata;
1017
1018                 if (eap_len < sizeof(*hdr)) {
1019                         wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 "
1020                                    "EAP frame (len=%lu, expected %lu or more) "
1021                                    "- dropped", (unsigned long) eap_len,
1022                                    (unsigned long) sizeof(*hdr));
1023                         retval = -1;
1024                         goto done;
1025                 }
1026                 len = be_to_host16(hdr->length);
1027                 if (len > eap_len) {
1028                         wpa_printf(MSG_INFO, "EAP-TTLS: Length mismatch in "
1029                                    "Phase 2 EAP frame (EAP hdr len=%d, EAP "
1030                                    "data len in AVP=%lu)", len,
1031                                    (unsigned long) eap_len);
1032                         retval = -1;
1033                         goto done;
1034                 }
1035                 wpa_printf(MSG_DEBUG, "EAP-TTLS: received Phase 2: code=%d "
1036                            "identifier=%d length=%d",
1037                            hdr->code, hdr->identifier, len);
1038         process_eap:
1039                 switch (hdr->code) {
1040                 case EAP_CODE_REQUEST:
1041                         if (eap_ttls_phase2_request(sm, data, ret, req, hdr,
1042                                                     &resp, &resp_len)) {
1043                                 wpa_printf(MSG_INFO, "EAP-TTLS: Phase2 "
1044                                            "Request processing failed");
1045                                 retval = -1;
1046                                 goto done;
1047                         }
1048                         break;
1049                 default:
1050                         wpa_printf(MSG_INFO, "EAP-TTLS: Unexpected code=%d in "
1051                                    "Phase 2 EAP header", hdr->code);
1052                         retval = -1;
1053                         break;
1054                 }
1055                 break;
1056         case EAP_TTLS_PHASE2_MSCHAPV2:
1057                 if (mschapv2_error) {
1058                         wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Received "
1059                                    "MS-CHAP-Error - failed");
1060                         ret->methodState = METHOD_DONE;
1061                         ret->decision = DECISION_FAIL;
1062                         *out_data = eap_tls_build_ack(&data->ssl, out_len,
1063                                                       req->identifier,
1064                                                       EAP_TYPE_TTLS, 0);
1065                         break;
1066                 }
1067
1068                 if (mschapv2 == NULL) {
1069                         wpa_printf(MSG_WARNING, "EAP-TTLS: no MS-CHAP2-Success"
1070                                    " AVP received for Phase2 MSCHAPV2");
1071                         retval = -1;
1072                         break;
1073                 }
1074                 if (mschapv2[0] != data->ident) {
1075                         wpa_printf(MSG_WARNING, "EAP-TTLS: Ident mismatch "
1076                                    "for Phase 2 MSCHAPV2 (received Ident "
1077                                    "0x%02x, expected 0x%02x)",
1078                                    mschapv2[0], data->ident);
1079                         retval = -1;
1080                         break;
1081                 }
1082                 if (!data->auth_response_valid ||
1083                     mschapv2[1] != 'S' || mschapv2[2] != '=' ||
1084                     hexstr2bin((char *) (mschapv2 + 3), recv_response, 20) ||
1085                     memcmp(data->auth_response, recv_response, 20) != 0) {
1086                         wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid "
1087                                    "authenticator response in Phase 2 "
1088                                    "MSCHAPV2 success request");
1089                         retval = -1;
1090                         break;
1091                 }
1092
1093                 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 MSCHAPV2 "
1094                            "authentication succeeded");
1095                 ret->methodState = METHOD_DONE;
1096                 ret->decision = DECISION_UNCOND_SUCC;
1097                 data->phase2_success = 1;
1098
1099                 /* Reply with empty data; authentication server will reply
1100                  * with EAP-Success after this. */
1101                 retval = 1;
1102                 goto done;
1103         case EAP_TTLS_PHASE2_MSCHAP:
1104         case EAP_TTLS_PHASE2_PAP:
1105         case EAP_TTLS_PHASE2_CHAP:
1106                 /* EAP-TTLS/{MSCHAP,PAP,CHAP} should not send any TLS tunneled
1107                  * requests to the supplicant */
1108                 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received unexpected "
1109                            "tunneled data");
1110                 retval = -1;
1111                 break;
1112         }
1113
1114         if (resp) {
1115                 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Encrypting Phase 2 data",
1116                                 resp, resp_len);
1117
1118                 if (eap_ttls_encrypt(sm, data, req->identifier,
1119                                      resp, resp_len, out_data, out_len)) {
1120                         wpa_printf(MSG_INFO, "EAP-TTLS: Failed to encrypt "
1121                                    "a Phase 2 frame");
1122                 }
1123                 free(resp);
1124         } else if (config->pending_req_identity ||
1125                    config->pending_req_password ||
1126                    config->pending_req_otp ||
1127                    config->pending_req_new_password) {
1128                 free(data->pending_phase2_req);
1129                 data->pending_phase2_req = malloc(len_decrypted);
1130                 if (data->pending_phase2_req) {
1131                         memcpy(data->pending_phase2_req, in_decrypted,
1132                                len_decrypted);
1133                         data->pending_phase2_req_len = len_decrypted;
1134                 }
1135         }
1136
1137 done:
1138         free(in_decrypted);
1139         free(eapdata);
1140
1141         if (retval < 0) {
1142                 ret->methodState = METHOD_DONE;
1143                 ret->decision = DECISION_FAIL;
1144         }
1145
1146         return retval;
1147 }
1148
1149
1150 static u8 * eap_ttls_process(struct eap_sm *sm, void *priv,
1151                              struct eap_method_ret *ret,
1152                              const u8 *reqData, size_t reqDataLen,
1153                              size_t *respDataLen)
1154 {
1155         const struct eap_hdr *req;
1156         size_t left;
1157         int res;
1158         u8 flags, *resp, id;
1159         const u8 *pos;
1160         struct eap_ttls_data *data = priv;
1161
1162         pos = eap_tls_process_init(sm, &data->ssl, EAP_TYPE_TTLS, ret,
1163                                    reqData, reqDataLen, &left, &flags);
1164         if (pos == NULL)
1165                 return NULL;
1166         req = (const struct eap_hdr *) reqData;
1167         id = req->identifier;
1168
1169         if (flags & EAP_TLS_FLAGS_START) {
1170                 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start");
1171                 /* draft-ietf-pppext-eap-ttls-03.txt, Ch. 8.1:
1172                  * EAP-TTLS Start packet may, in a future specification, be
1173                  * allowed to contain data. Client based on this draft version
1174                  * must ignore such data but must not reject the Start packet.
1175                  */
1176                 left = 0;
1177         }
1178
1179         resp = NULL;
1180         if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1181             !data->resuming) {
1182                 res = eap_ttls_decrypt(sm, data, ret, req, pos, left,
1183                                        &resp, respDataLen);
1184         } else {
1185                 res = eap_tls_process_helper(sm, &data->ssl, EAP_TYPE_TTLS, 0,
1186                                              id, pos, left,
1187                                              &resp, respDataLen);
1188
1189                 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
1190                         wpa_printf(MSG_DEBUG,
1191                                    "EAP-TTLS: TLS done, proceed to Phase 2");
1192                         if (data->resuming) {
1193                                 wpa_printf(MSG_DEBUG, "EAP-TTLS: fast reauth -"
1194                                            " may skip Phase 2");
1195                                 ret->decision = DECISION_COND_SUCC;
1196                                 ret->methodState = METHOD_MAY_CONT;
1197                         }
1198                         data->phase2_start = 1;
1199                         free(data->key_data);
1200                         data->key_data =
1201                                 eap_tls_derive_key(sm, &data->ssl,
1202                                                    "ttls keying material",
1203                                                    EAP_TLS_KEY_LEN);
1204                         if (data->key_data) {
1205                                 wpa_hexdump_key(MSG_DEBUG,
1206                                                 "EAP-TTLS: Derived key",
1207                                                 data->key_data,
1208                                                 EAP_TLS_KEY_LEN);
1209                         } else {
1210                                 wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to "
1211                                            "derive key");
1212                         }
1213
1214                         if (*respDataLen == 0) {
1215                                 if (eap_ttls_decrypt(sm, data, ret, req, NULL,
1216                                                      0, &resp, respDataLen)) {
1217                                         wpa_printf(MSG_WARNING, "EAP-TTLS: "
1218                                                    "failed to process early "
1219                                                    "start for Phase 2");
1220                                 }
1221                                 res = 0;
1222                         }
1223                         data->resuming = 0;
1224                 }
1225         }
1226
1227         if (ret->methodState == METHOD_DONE) {
1228                 ret->allowNotifications = FALSE;
1229                 if (ret->decision == DECISION_UNCOND_SUCC ||
1230                     ret->decision == DECISION_COND_SUCC) {
1231                         wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
1232                                    "completed successfully");
1233                         data->phase2_success = 1;
1234                 }
1235         } else if (sm->workaround && ret->methodState == METHOD_MAY_CONT &&
1236                    (ret->decision == DECISION_UNCOND_SUCC ||
1237                     ret->decision == DECISION_COND_SUCC)) {
1238                         wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
1239                                    "completed successfully (EAP workaround)");
1240                         data->phase2_success = 1;
1241         }
1242
1243         if (res == 1) {
1244                 return eap_tls_build_ack(&data->ssl, respDataLen, id,
1245                                          EAP_TYPE_TTLS, 0);
1246         }
1247         return resp;
1248 }
1249
1250
1251 static Boolean eap_ttls_has_reauth_data(struct eap_sm *sm, void *priv)
1252 {
1253         struct eap_ttls_data *data = priv;
1254         return tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1255                 data->phase2_success;
1256 }
1257
1258
1259 static void eap_ttls_deinit_for_reauth(struct eap_sm *sm, void *priv)
1260 {
1261         struct eap_ttls_data *data = priv;
1262         free(data->pending_phase2_req);
1263         data->pending_phase2_req = NULL;
1264 }
1265
1266
1267 static void * eap_ttls_init_for_reauth(struct eap_sm *sm, void *priv)
1268 {
1269         struct eap_ttls_data *data = priv;
1270         free(data->key_data);
1271         data->key_data = NULL;
1272         if (eap_tls_reauth_init(sm, &data->ssl)) {
1273                 free(data);
1274                 return NULL;
1275         }
1276         data->phase2_start = 0;
1277         data->phase2_success = 0;
1278         data->resuming = 1;
1279         data->reauth = 1;
1280         return priv;
1281 }
1282
1283
1284 static int eap_ttls_get_status(struct eap_sm *sm, void *priv, char *buf,
1285                                size_t buflen, int verbose)
1286 {
1287         struct eap_ttls_data *data = priv;
1288         int len;
1289
1290         len = eap_tls_status(sm, &data->ssl, buf, buflen, verbose);
1291         switch (data->phase2_type) {
1292         case EAP_TTLS_PHASE2_EAP:
1293                 len += snprintf(buf + len, buflen - len,
1294                                 "EAP-TTLS Phase2 method=EAP-%s\n",
1295                                 data->phase2_method ? data->phase2_method->name
1296                                 : "?");
1297                 break;
1298         case EAP_TTLS_PHASE2_MSCHAPV2:
1299                 len += snprintf(buf + len, buflen - len,
1300                                 "EAP-TTLS Phase2 method=MSCHAPV2\n");
1301                 break;
1302         case EAP_TTLS_PHASE2_MSCHAP:
1303                 len += snprintf(buf + len, buflen - len,
1304                                 "EAP-TTLS Phase2 method=MSCHAP\n");
1305                 break;
1306         case EAP_TTLS_PHASE2_PAP:
1307                 len += snprintf(buf + len, buflen - len,
1308                                 "EAP-TTLS Phase2 method=PAP\n");
1309                 break;
1310         case EAP_TTLS_PHASE2_CHAP:
1311                 len += snprintf(buf + len, buflen - len,
1312                                 "EAP-TTLS Phase2 method=CHAP\n");
1313                 break;
1314         }
1315
1316         return len;
1317 }
1318
1319
1320 static Boolean eap_ttls_isKeyAvailable(struct eap_sm *sm, void *priv)
1321 {
1322         struct eap_ttls_data *data = priv;
1323         return data->key_data != NULL && data->phase2_success;
1324 }
1325
1326
1327 static u8 * eap_ttls_getKey(struct eap_sm *sm, void *priv, size_t *len)
1328 {
1329         struct eap_ttls_data *data = priv;
1330         u8 *key;
1331
1332         if (data->key_data == NULL || !data->phase2_success)
1333                 return NULL;
1334
1335         key = malloc(EAP_TLS_KEY_LEN);
1336         if (key == NULL)
1337                 return NULL;
1338
1339         *len = EAP_TLS_KEY_LEN;
1340         memcpy(key, data->key_data, EAP_TLS_KEY_LEN);
1341
1342         return key;
1343 }
1344
1345
1346 const struct eap_method eap_method_ttls =
1347 {
1348         .method = EAP_TYPE_TTLS,
1349         .name = "TTLS",
1350         .init = eap_ttls_init,
1351         .deinit = eap_ttls_deinit,
1352         .process = eap_ttls_process,
1353         .isKeyAvailable = eap_ttls_isKeyAvailable,
1354         .getKey = eap_ttls_getKey,
1355         .get_status = eap_ttls_get_status,
1356         .has_reauth_data = eap_ttls_has_reauth_data,
1357         .deinit_for_reauth = eap_ttls_deinit_for_reauth,
1358         .init_for_reauth = eap_ttls_init_for_reauth,
1359 };