Updated through tag hostap_2_5 from git://w1.fi/hostap.git
[mech_eap.git] / libeap / src / eap_peer / eap_ttls.c
1 /*
2  * EAP peer method: EAP-TTLS (RFC 5281)
3  * Copyright (c) 2004-2011, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "includes.h"
10
11 #include "common.h"
12 #include "radius/radius.h"
13 #include "crypto/ms_funcs.h"
14 #include "crypto/sha1.h"
15 #include "crypto/tls.h"
16 #include "eap_common/chap.h"
17 #include "eap_common/eap_ttls.h"
18 #include "mschapv2.h"
19 #include "eap_i.h"
20 #include "eap_tls_common.h"
21 #include "eap_config.h"
22
23
24 #define EAP_TTLS_VERSION 0
25
26
27 static void eap_ttls_deinit(struct eap_sm *sm, void *priv);
28
29
30 struct eap_ttls_data {
31         struct eap_ssl_data ssl;
32
33         int ttls_version;
34
35         const struct eap_method *phase2_method;
36         void *phase2_priv;
37         int phase2_success;
38         int phase2_start;
39
40         enum phase2_types {
41                 EAP_TTLS_PHASE2_EAP,
42                 EAP_TTLS_PHASE2_MSCHAPV2,
43                 EAP_TTLS_PHASE2_MSCHAP,
44                 EAP_TTLS_PHASE2_PAP,
45                 EAP_TTLS_PHASE2_CHAP
46         } phase2_type;
47         struct eap_method_type phase2_eap_type;
48         struct eap_method_type *phase2_eap_types;
49         size_t num_phase2_eap_types;
50
51         u8 auth_response[MSCHAPV2_AUTH_RESPONSE_LEN];
52         int auth_response_valid;
53         u8 master_key[MSCHAPV2_MASTER_KEY_LEN]; /* MSCHAPv2 master key */
54         u8 ident;
55         int resuming; /* starting a resumed session */
56         int reauth; /* reauthentication */
57         u8 *key_data;
58         u8 *session_id;
59         size_t id_len;
60
61         struct wpabuf *pending_phase2_req;
62         int chbind_req_sent; /* channel binding request was sent */
63         int done_butfor_cb; /*we turned METHOD_DONE into METHOD_MAY_CONT to receive cb*/
64   EapDecision cbDecision;
65 #ifdef EAP_TNC
66         int ready_for_tnc;
67         int tnc_started;
68 #endif /* EAP_TNC */
69 };
70
71
72 /* draft-ietf-emu-chbind-13 section 5.3 */
73
74 #ifdef _MSC_VER
75 #pragma pack(push, 1)
76 #endif /* _MSC_VER */
77
78 struct chbind_hdr {
79         u16 len;
80         u8 nsid;
81 } STRUCT_PACKED;
82
83 #ifdef _MSC_VER
84 #pragma pack(pop)
85 #endif /* _MSC_VER */
86
87
88
89 static void * eap_ttls_init(struct eap_sm *sm)
90 {
91         struct eap_ttls_data *data;
92         struct eap_peer_config *config = eap_get_config(sm);
93         char *selected;
94
95         data = os_zalloc(sizeof(*data));
96         if (data == NULL)
97                 return NULL;
98         data->ttls_version = EAP_TTLS_VERSION;
99         selected = "EAP";
100         data->phase2_type = EAP_TTLS_PHASE2_EAP;
101
102         if (config && config->phase2) {
103                 if (os_strstr(config->phase2, "autheap=")) {
104                         selected = "EAP";
105                         data->phase2_type = EAP_TTLS_PHASE2_EAP;
106                 } else if (os_strstr(config->phase2, "auth=MSCHAPV2")) {
107                         selected = "MSCHAPV2";
108                         data->phase2_type = EAP_TTLS_PHASE2_MSCHAPV2;
109                 } else if (os_strstr(config->phase2, "auth=MSCHAP")) {
110                         selected = "MSCHAP";
111                         data->phase2_type = EAP_TTLS_PHASE2_MSCHAP;
112                 } else if (os_strstr(config->phase2, "auth=PAP")) {
113                         selected = "PAP";
114                         data->phase2_type = EAP_TTLS_PHASE2_PAP;
115                 } else if (os_strstr(config->phase2, "auth=CHAP")) {
116                         selected = "CHAP";
117                         data->phase2_type = EAP_TTLS_PHASE2_CHAP;
118                 }
119         }
120         wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase2 type: %s", selected);
121
122         if (data->phase2_type == EAP_TTLS_PHASE2_EAP) {
123                 if (eap_peer_select_phase2_methods(config, "autheap=",
124                                                    &data->phase2_eap_types,
125                                                    &data->num_phase2_eap_types)
126                     < 0) {
127                         eap_ttls_deinit(sm, data);
128                         return NULL;
129                 }
130
131                 data->phase2_eap_type.vendor = EAP_VENDOR_IETF;
132                 data->phase2_eap_type.method = EAP_TYPE_NONE;
133         }
134
135         if (eap_peer_tls_ssl_init(sm, &data->ssl, config, EAP_TYPE_TTLS)) {
136                 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to initialize SSL.");
137                 eap_ttls_deinit(sm, data);
138                 return NULL;
139         }
140
141         return data;
142 }
143
144
145 static void eap_ttls_phase2_eap_deinit(struct eap_sm *sm,
146                                        struct eap_ttls_data *data)
147 {
148         if (data->phase2_priv && data->phase2_method) {
149                 data->phase2_method->deinit(sm, data->phase2_priv);
150                 data->phase2_method = NULL;
151                 data->phase2_priv = NULL;
152         }
153 }
154
155
156 static void eap_ttls_free_key(struct eap_ttls_data *data)
157 {
158         if (data->key_data) {
159                 bin_clear_free(data->key_data, EAP_TLS_KEY_LEN + EAP_EMSK_LEN);
160                 data->key_data = NULL;
161         }
162 }
163
164
165 static void eap_ttls_deinit(struct eap_sm *sm, void *priv)
166 {
167         struct eap_ttls_data *data = priv;
168         if (data == NULL)
169                 return;
170         eap_ttls_phase2_eap_deinit(sm, data);
171         os_free(data->phase2_eap_types);
172         eap_peer_tls_ssl_deinit(sm, &data->ssl);
173         eap_ttls_free_key(data);
174         os_free(data->session_id);
175         wpabuf_free(data->pending_phase2_req);
176         os_free(data);
177 }
178
179
180 static u8 * eap_ttls_avp_hdr(u8 *avphdr, u32 avp_code, u32 vendor_id,
181                              int mandatory, size_t len)
182 {
183         struct ttls_avp_vendor *avp;
184         u8 flags;
185         size_t hdrlen;
186
187         avp = (struct ttls_avp_vendor *) avphdr;
188         flags = mandatory ? AVP_FLAGS_MANDATORY : 0;
189         if (vendor_id) {
190                 flags |= AVP_FLAGS_VENDOR;
191                 hdrlen = sizeof(*avp);
192                 avp->vendor_id = host_to_be32(vendor_id);
193         } else {
194                 hdrlen = sizeof(struct ttls_avp);
195         }
196
197         avp->avp_code = host_to_be32(avp_code);
198         avp->avp_length = host_to_be32(((u32) flags << 24) |
199                                        (u32) (hdrlen + len));
200
201         return avphdr + hdrlen;
202 }
203
204
205 static u8 * eap_ttls_avp_add(u8 *start, u8 *avphdr, u32 avp_code,
206                              u32 vendor_id, int mandatory,
207                              const u8 *data, size_t len)
208 {
209         u8 *pos;
210         pos = eap_ttls_avp_hdr(avphdr, avp_code, vendor_id, mandatory, len);
211         os_memcpy(pos, data, len);
212         pos += len;
213         AVP_PAD(start, pos);
214         return pos;
215 }
216
217
218 static int eap_ttls_avp_encapsulate(struct wpabuf **resp, u32 avp_code,
219                                     int mandatory)
220 {
221         struct wpabuf *msg;
222         u8 *avp, *pos;
223
224         msg = wpabuf_alloc(sizeof(struct ttls_avp) + wpabuf_len(*resp) + 4);
225         if (msg == NULL) {
226                 wpabuf_free(*resp);
227                 *resp = NULL;
228                 return -1;
229         }
230
231         avp = wpabuf_mhead(msg);
232         pos = eap_ttls_avp_hdr(avp, avp_code, 0, mandatory, wpabuf_len(*resp));
233         os_memcpy(pos, wpabuf_head(*resp), wpabuf_len(*resp));
234         pos += wpabuf_len(*resp);
235         AVP_PAD(avp, pos);
236         wpabuf_free(*resp);
237         wpabuf_put(msg, pos - avp);
238         *resp = msg;
239         return 0;
240 }
241
242 /* chop up resp into multiple vsa's as necessary*/
243 static int eap_ttls_avp_radius_vsa_encapsulate(struct wpabuf **resp, u32 vendor,
244                                         u8 attr, int mandatory)
245 {
246         struct wpabuf *msg;
247         u8 *avp, *pos, *src, *final;
248         size_t size = wpabuf_len(*resp);
249         size_t num_msgs = 1 + (size / 248);
250         size_t msg_wrapper_size = sizeof(struct ttls_avp_vendor) + 6;
251         size_t allocated_total = num_msgs * (4 + msg_wrapper_size) + size;
252
253         msg = wpabuf_alloc(allocated_total);
254         if (msg == NULL) {
255                 wpabuf_free(*resp);
256                 *resp = NULL;
257                 return -1;
258         }
259         src = wpabuf_mhead(*resp);
260         avp = wpabuf_mhead(msg);
261         while (size > 0) {
262                 int avp_size = size > 248 ? 248 : size;
263                 size -= avp_size;
264                 pos = eap_ttls_avp_hdr(avp, RADIUS_ATTR_VENDOR_SPECIFIC, 0, mandatory,
265                                        avp_size+6);
266                 wpabuf_put(msg, pos-avp);
267                 wpabuf_put_be32(msg, vendor);
268                 wpabuf_put_u8(msg, (u8) attr);
269                 wpabuf_put_u8(msg, (u8) avp_size+2);
270                 wpabuf_put_data(msg, src, avp_size);
271                 src += avp_size;
272                 pos = wpabuf_mhead_u8(msg) + wpabuf_len(msg);
273                 final = pos; /*keep pos so we know how much padding is added*/
274                 AVP_PAD(avp, final); /*final modified*/
275                 if (final > pos)
276                         wpabuf_put(msg, final-pos);
277                 avp = final;
278         }
279         /* check avp-wpabuf_mhead(msg) < allocated_total */
280         wpabuf_free(*resp);
281         *resp = msg;
282         return 0;
283 }
284
285 static int eap_ttls_v0_derive_key(struct eap_sm *sm,
286                                   struct eap_ttls_data *data)
287 {
288         eap_ttls_free_key(data);
289         data->key_data = eap_peer_tls_derive_key(sm, &data->ssl,
290                                                  "ttls keying material",
291                                                  EAP_TLS_KEY_LEN +
292                                                  EAP_EMSK_LEN);
293         if (!data->key_data) {
294                 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to derive key");
295                 return -1;
296         }
297
298         wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived key",
299                         data->key_data, EAP_TLS_KEY_LEN);
300         wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived EMSK",
301                         data->key_data + EAP_TLS_KEY_LEN,
302                         EAP_EMSK_LEN);
303
304         os_free(data->session_id);
305         data->session_id = eap_peer_tls_derive_session_id(sm, &data->ssl,
306                                                           EAP_TYPE_TTLS,
307                                                           &data->id_len);
308         if (data->session_id) {
309                 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Derived Session-Id",
310                             data->session_id, data->id_len);
311         } else {
312                 wpa_printf(MSG_ERROR, "EAP-TTLS: Failed to derive Session-Id");
313         }
314
315         return 0;
316 }
317
318
319 #ifndef CONFIG_FIPS
320 static u8 * eap_ttls_implicit_challenge(struct eap_sm *sm,
321                                         struct eap_ttls_data *data, size_t len)
322 {
323         return eap_peer_tls_derive_key(sm, &data->ssl, "ttls challenge", len);
324 }
325 #endif /* CONFIG_FIPS */
326
327
328 static void eap_ttls_phase2_select_eap_method(struct eap_ttls_data *data,
329                                               u8 method)
330 {
331         size_t i;
332         for (i = 0; i < data->num_phase2_eap_types; i++) {
333                 if (data->phase2_eap_types[i].vendor != EAP_VENDOR_IETF ||
334                     data->phase2_eap_types[i].method != method)
335                         continue;
336
337                 data->phase2_eap_type.vendor =
338                         data->phase2_eap_types[i].vendor;
339                 data->phase2_eap_type.method =
340                         data->phase2_eap_types[i].method;
341                 wpa_printf(MSG_DEBUG, "EAP-TTLS: Selected "
342                            "Phase 2 EAP vendor %d method %d",
343                            data->phase2_eap_type.vendor,
344                            data->phase2_eap_type.method);
345                 break;
346         }
347 }
348
349
350 static int eap_ttls_phase2_eap_process(struct eap_sm *sm,
351                                        struct eap_ttls_data *data,
352                                        struct eap_method_ret *ret,
353                                        struct eap_hdr *hdr, size_t len,
354                                        struct wpabuf **resp)
355 {
356         struct wpabuf msg;
357         struct eap_method_ret iret;
358
359         os_memset(&iret, 0, sizeof(iret));
360         wpabuf_set(&msg, hdr, len);
361         *resp = data->phase2_method->process(sm, data->phase2_priv, &iret,
362                                              &msg);
363         if ((iret.methodState == METHOD_DONE ||
364              iret.methodState == METHOD_MAY_CONT) &&
365             (iret.decision == DECISION_UNCOND_SUCC ||
366              iret.decision == DECISION_COND_SUCC ||
367              iret.decision == DECISION_FAIL)) {
368                 ret->methodState = iret.methodState;
369                 ret->decision = iret.decision;
370         }
371
372         return 0;
373 }
374
375
376 static int eap_ttls_phase2_request_eap_method(struct eap_sm *sm,
377                                               struct eap_ttls_data *data,
378                                               struct eap_method_ret *ret,
379                                               struct eap_hdr *hdr, size_t len,
380                                               u8 method, struct wpabuf **resp)
381 {
382 #ifdef EAP_TNC
383         if (data->tnc_started && data->phase2_method &&
384             data->phase2_priv && method == EAP_TYPE_TNC &&
385             data->phase2_eap_type.method == EAP_TYPE_TNC)
386                 return eap_ttls_phase2_eap_process(sm, data, ret, hdr, len,
387                                                    resp);
388
389         if (data->ready_for_tnc && !data->tnc_started &&
390             method == EAP_TYPE_TNC) {
391                 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start TNC after completed "
392                            "EAP method");
393                 data->tnc_started = 1;
394         }
395
396         if (data->tnc_started) {
397                 if (data->phase2_eap_type.vendor != EAP_VENDOR_IETF ||
398                     data->phase2_eap_type.method == EAP_TYPE_TNC) {
399                         wpa_printf(MSG_DEBUG, "EAP-TTLS: Unexpected EAP "
400                                    "type %d for TNC", method);
401                         return -1;
402                 }
403
404                 data->phase2_eap_type.vendor = EAP_VENDOR_IETF;
405                 data->phase2_eap_type.method = method;
406                 wpa_printf(MSG_DEBUG, "EAP-TTLS: Selected "
407                            "Phase 2 EAP vendor %d method %d (TNC)",
408                            data->phase2_eap_type.vendor,
409                            data->phase2_eap_type.method);
410
411                 if (data->phase2_type == EAP_TTLS_PHASE2_EAP)
412                         eap_ttls_phase2_eap_deinit(sm, data);
413         }
414 #endif /* EAP_TNC */
415
416         if (data->phase2_eap_type.vendor == EAP_VENDOR_IETF &&
417             data->phase2_eap_type.method == EAP_TYPE_NONE)
418                 eap_ttls_phase2_select_eap_method(data, method);
419
420         if (method != data->phase2_eap_type.method || method == EAP_TYPE_NONE)
421         {
422                 if (eap_peer_tls_phase2_nak(data->phase2_eap_types,
423                                             data->num_phase2_eap_types,
424                                             hdr, resp))
425                         return -1;
426                 return 0;
427         }
428
429         if (data->phase2_priv == NULL) {
430                 data->phase2_method = eap_peer_get_eap_method(
431                         EAP_VENDOR_IETF, method);
432                 if (data->phase2_method) {
433                         sm->init_phase2 = 1;
434                         data->phase2_priv = data->phase2_method->init(sm);
435                         sm->init_phase2 = 0;
436                 }
437         }
438         if (data->phase2_priv == NULL || data->phase2_method == NULL) {
439                 wpa_printf(MSG_INFO, "EAP-TTLS: failed to initialize "
440                            "Phase 2 EAP method %d", method);
441                 return -1;
442         }
443
444         return eap_ttls_phase2_eap_process(sm, data, ret, hdr, len, resp);
445 }
446
447
448 static int eap_ttls_phase2_request_eap(struct eap_sm *sm,
449                                        struct eap_ttls_data *data,
450                                        struct eap_method_ret *ret,
451                                        struct eap_hdr *hdr,
452                                        struct wpabuf **resp)
453 {
454         size_t len = be_to_host16(hdr->length);
455         u8 *pos;
456         struct eap_peer_config *config = eap_get_config(sm);
457
458         if (len <= sizeof(struct eap_hdr)) {
459                 wpa_printf(MSG_INFO, "EAP-TTLS: too short "
460                            "Phase 2 request (len=%lu)", (unsigned long) len);
461                 return -1;
462         }
463         pos = (u8 *) (hdr + 1);
464         wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP Request: type=%d", *pos);
465         switch (*pos) {
466         case EAP_TYPE_IDENTITY:
467                 *resp = eap_sm_buildIdentity(sm, hdr->identifier, 1);
468                 break;
469         default:
470                 if (eap_ttls_phase2_request_eap_method(sm, data, ret, hdr, len,
471                                                        *pos, resp) < 0)
472                         return -1;
473                 break;
474         }
475
476         if (*resp == NULL &&
477             (config->pending_req_identity || config->pending_req_password ||
478              config->pending_req_otp)) {
479                 return 0;
480         }
481
482         if (*resp == NULL)
483                 return -1;
484
485         wpa_hexdump_buf(MSG_DEBUG, "EAP-TTLS: AVP encapsulate EAP Response",
486                         *resp);
487         return eap_ttls_avp_encapsulate(resp, RADIUS_ATTR_EAP_MESSAGE, 1);
488 }
489
490
491 static int eap_ttls_phase2_request_mschapv2(struct eap_sm *sm,
492                                             struct eap_ttls_data *data,
493                                             struct eap_method_ret *ret,
494                                             struct wpabuf **resp)
495 {
496 #ifdef CONFIG_FIPS
497         wpa_printf(MSG_ERROR, "EAP-TTLS: MSCHAPV2 not supported in FIPS build");
498         return -1;
499 #else /* CONFIG_FIPS */
500 #ifdef EAP_MSCHAPv2
501         struct wpabuf *msg;
502         u8 *buf, *pos, *challenge, *peer_challenge;
503         const u8 *identity, *password;
504         size_t identity_len, password_len;
505         int pwhash;
506
507         wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAPV2 Request");
508
509         identity = eap_get_config_identity(sm, &identity_len);
510         password = eap_get_config_password2(sm, &password_len, &pwhash);
511         if (identity == NULL || password == NULL)
512                 return -1;
513
514         msg = wpabuf_alloc(identity_len + 1000);
515         if (msg == NULL) {
516                 wpa_printf(MSG_ERROR,
517                            "EAP-TTLS/MSCHAPV2: Failed to allocate memory");
518                 return -1;
519         }
520         pos = buf = wpabuf_mhead(msg);
521
522         /* User-Name */
523         pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
524                                identity, identity_len);
525
526         /* MS-CHAP-Challenge */
527         challenge = eap_ttls_implicit_challenge(
528                 sm, data, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN + 1);
529         if (challenge == NULL) {
530                 wpabuf_free(msg);
531                 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to derive "
532                            "implicit challenge");
533                 return -1;
534         }
535
536         pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_MS_CHAP_CHALLENGE,
537                                RADIUS_VENDOR_ID_MICROSOFT, 1,
538                                challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
539
540         /* MS-CHAP2-Response */
541         pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP2_RESPONSE,
542                                RADIUS_VENDOR_ID_MICROSOFT, 1,
543                                EAP_TTLS_MSCHAPV2_RESPONSE_LEN);
544         data->ident = challenge[EAP_TTLS_MSCHAPV2_CHALLENGE_LEN];
545         *pos++ = data->ident;
546         *pos++ = 0; /* Flags */
547         if (os_get_random(pos, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN) < 0) {
548                 os_free(challenge);
549                 wpabuf_free(msg);
550                 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to get "
551                            "random data for peer challenge");
552                 return -1;
553         }
554         peer_challenge = pos;
555         pos += EAP_TTLS_MSCHAPV2_CHALLENGE_LEN;
556         os_memset(pos, 0, 8); /* Reserved, must be zero */
557         pos += 8;
558         if (mschapv2_derive_response(identity, identity_len, password,
559                                      password_len, pwhash, challenge,
560                                      peer_challenge, pos, data->auth_response,
561                                      data->master_key)) {
562                 os_free(challenge);
563                 wpabuf_free(msg);
564                 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to derive "
565                            "response");
566                 return -1;
567         }
568         data->auth_response_valid = 1;
569
570         pos += 24;
571         os_free(challenge);
572         AVP_PAD(buf, pos);
573
574         wpabuf_put(msg, pos - buf);
575         *resp = msg;
576
577         return 0;
578 #else /* EAP_MSCHAPv2 */
579         wpa_printf(MSG_ERROR, "EAP-TTLS: MSCHAPv2 not included in the build");
580         return -1;
581 #endif /* EAP_MSCHAPv2 */
582 #endif /* CONFIG_FIPS */
583 }
584
585
586 static int eap_ttls_phase2_request_mschap(struct eap_sm *sm,
587                                           struct eap_ttls_data *data,
588                                           struct eap_method_ret *ret,
589                                           struct wpabuf **resp)
590 {
591 #ifdef CONFIG_FIPS
592         wpa_printf(MSG_ERROR, "EAP-TTLS: MSCHAP not supported in FIPS build");
593         return -1;
594 #else /* CONFIG_FIPS */
595         struct wpabuf *msg;
596         u8 *buf, *pos, *challenge;
597         const u8 *identity, *password;
598         size_t identity_len, password_len;
599         int pwhash;
600
601         wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAP Request");
602
603         identity = eap_get_config_identity(sm, &identity_len);
604         password = eap_get_config_password2(sm, &password_len, &pwhash);
605         if (identity == NULL || password == NULL)
606                 return -1;
607
608         msg = wpabuf_alloc(identity_len + 1000);
609         if (msg == NULL) {
610                 wpa_printf(MSG_ERROR,
611                            "EAP-TTLS/MSCHAP: Failed to allocate memory");
612                 return -1;
613         }
614         pos = buf = wpabuf_mhead(msg);
615
616         /* User-Name */
617         pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
618                                identity, identity_len);
619
620         /* MS-CHAP-Challenge */
621         challenge = eap_ttls_implicit_challenge(
622                 sm, data, EAP_TTLS_MSCHAP_CHALLENGE_LEN + 1);
623         if (challenge == NULL) {
624                 wpabuf_free(msg);
625                 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAP: Failed to derive "
626                            "implicit challenge");
627                 return -1;
628         }
629
630         pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_MS_CHAP_CHALLENGE,
631                                RADIUS_VENDOR_ID_MICROSOFT, 1,
632                                challenge, EAP_TTLS_MSCHAP_CHALLENGE_LEN);
633
634         /* MS-CHAP-Response */
635         pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP_RESPONSE,
636                                RADIUS_VENDOR_ID_MICROSOFT, 1,
637                                EAP_TTLS_MSCHAP_RESPONSE_LEN);
638         data->ident = challenge[EAP_TTLS_MSCHAP_CHALLENGE_LEN];
639         *pos++ = data->ident;
640         *pos++ = 1; /* Flags: Use NT style passwords */
641         os_memset(pos, 0, 24); /* LM-Response */
642         pos += 24;
643         if (pwhash) {
644                 challenge_response(challenge, password, pos); /* NT-Response */
645                 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: MSCHAP password hash",
646                                 password, 16);
647         } else {
648                 nt_challenge_response(challenge, password, password_len,
649                                       pos); /* NT-Response */
650                 wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: MSCHAP password",
651                                       password, password_len);
652         }
653         wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP implicit challenge",
654                     challenge, EAP_TTLS_MSCHAP_CHALLENGE_LEN);
655         wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP response", pos, 24);
656         pos += 24;
657         os_free(challenge);
658         AVP_PAD(buf, pos);
659
660         wpabuf_put(msg, pos - buf);
661         *resp = msg;
662
663         /* EAP-TTLS/MSCHAP does not provide tunneled success
664          * notification, so assume that Phase2 succeeds. */
665         ret->methodState = METHOD_DONE;
666         ret->decision = DECISION_COND_SUCC;
667
668         return 0;
669 #endif /* CONFIG_FIPS */
670 }
671
672
673 static int eap_ttls_phase2_request_pap(struct eap_sm *sm,
674                                        struct eap_ttls_data *data,
675                                        struct eap_method_ret *ret,
676                                        struct wpabuf **resp)
677 {
678         struct wpabuf *msg;
679         u8 *buf, *pos;
680         size_t pad;
681         const u8 *identity, *password;
682         size_t identity_len, password_len;
683
684         wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 PAP Request");
685
686         identity = eap_get_config_identity(sm, &identity_len);
687         password = eap_get_config_password(sm, &password_len);
688         if (identity == NULL || password == NULL)
689                 return -1;
690
691         msg = wpabuf_alloc(identity_len + password_len + 100);
692         if (msg == NULL) {
693                 wpa_printf(MSG_ERROR,
694                            "EAP-TTLS/PAP: Failed to allocate memory");
695                 return -1;
696         }
697         pos = buf = wpabuf_mhead(msg);
698
699         /* User-Name */
700         pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
701                                identity, identity_len);
702
703         /* User-Password; in RADIUS, this is encrypted, but EAP-TTLS encrypts
704          * the data, so no separate encryption is used in the AVP itself.
705          * However, the password is padded to obfuscate its length. */
706         pad = password_len == 0 ? 16 : (16 - (password_len & 15)) & 15;
707         pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_USER_PASSWORD, 0, 1,
708                                password_len + pad);
709         os_memcpy(pos, password, password_len);
710         pos += password_len;
711         os_memset(pos, 0, pad);
712         pos += pad;
713         AVP_PAD(buf, pos);
714
715         wpabuf_put(msg, pos - buf);
716         *resp = msg;
717
718         /* EAP-TTLS/PAP does not provide tunneled success notification,
719          * so assume that Phase2 succeeds. */
720         ret->methodState = METHOD_DONE;
721         ret->decision = DECISION_COND_SUCC;
722
723         return 0;
724 }
725
726
727 static int eap_ttls_phase2_request_chap(struct eap_sm *sm,
728                                         struct eap_ttls_data *data,
729                                         struct eap_method_ret *ret,
730                                         struct wpabuf **resp)
731 {
732 #ifdef CONFIG_FIPS
733         wpa_printf(MSG_ERROR, "EAP-TTLS: CHAP not supported in FIPS build");
734         return -1;
735 #else /* CONFIG_FIPS */
736         struct wpabuf *msg;
737         u8 *buf, *pos, *challenge;
738         const u8 *identity, *password;
739         size_t identity_len, password_len;
740
741         wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 CHAP Request");
742
743         identity = eap_get_config_identity(sm, &identity_len);
744         password = eap_get_config_password(sm, &password_len);
745         if (identity == NULL || password == NULL)
746                 return -1;
747
748         msg = wpabuf_alloc(identity_len + 1000);
749         if (msg == NULL) {
750                 wpa_printf(MSG_ERROR,
751                            "EAP-TTLS/CHAP: Failed to allocate memory");
752                 return -1;
753         }
754         pos = buf = wpabuf_mhead(msg);
755
756         /* User-Name */
757         pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
758                                identity, identity_len);
759
760         /* CHAP-Challenge */
761         challenge = eap_ttls_implicit_challenge(
762                 sm, data, EAP_TTLS_CHAP_CHALLENGE_LEN + 1);
763         if (challenge == NULL) {
764                 wpabuf_free(msg);
765                 wpa_printf(MSG_ERROR, "EAP-TTLS/CHAP: Failed to derive "
766                            "implicit challenge");
767                 return -1;
768         }
769
770         pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_CHAP_CHALLENGE, 0, 1,
771                                challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
772
773         /* CHAP-Password */
774         pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_CHAP_PASSWORD, 0, 1,
775                                1 + EAP_TTLS_CHAP_PASSWORD_LEN);
776         data->ident = challenge[EAP_TTLS_CHAP_CHALLENGE_LEN];
777         *pos++ = data->ident;
778
779         /* MD5(Ident + Password + Challenge) */
780         chap_md5(data->ident, password, password_len, challenge,
781                  EAP_TTLS_CHAP_CHALLENGE_LEN, pos);
782
783         wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: CHAP username",
784                           identity, identity_len);
785         wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: CHAP password",
786                               password, password_len);
787         wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP implicit challenge",
788                     challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
789         wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP password",
790                     pos, EAP_TTLS_CHAP_PASSWORD_LEN);
791         pos += EAP_TTLS_CHAP_PASSWORD_LEN;
792         os_free(challenge);
793         AVP_PAD(buf, pos);
794
795         wpabuf_put(msg, pos - buf);
796         *resp = msg;
797
798         /* EAP-TTLS/CHAP does not provide tunneled success
799          * notification, so assume that Phase2 succeeds. */
800         ret->methodState = METHOD_DONE;
801         ret->decision = DECISION_COND_SUCC;
802
803         return 0;
804 #endif /* CONFIG_FIPS */
805 }
806
807
808 static int eap_ttls_phase2_request(struct eap_sm *sm,
809                                    struct eap_ttls_data *data,
810                                    struct eap_method_ret *ret,
811                                    struct eap_hdr *hdr,
812                                    struct wpabuf **resp)
813 {
814         int res = 0;
815         size_t len;
816         enum phase2_types phase2_type = data->phase2_type;
817
818 #ifdef EAP_TNC
819         if (data->tnc_started) {
820                 wpa_printf(MSG_DEBUG, "EAP-TTLS: Processing TNC");
821                 phase2_type = EAP_TTLS_PHASE2_EAP;
822         }
823 #endif /* EAP_TNC */
824
825         if (phase2_type == EAP_TTLS_PHASE2_MSCHAPV2 ||
826             phase2_type == EAP_TTLS_PHASE2_MSCHAP ||
827             phase2_type == EAP_TTLS_PHASE2_PAP ||
828             phase2_type == EAP_TTLS_PHASE2_CHAP) {
829                 if (eap_get_config_identity(sm, &len) == NULL) {
830                         wpa_printf(MSG_INFO,
831                                    "EAP-TTLS: Identity not configured");
832                         eap_sm_request_identity(sm);
833                         if (eap_get_config_password(sm, &len) == NULL)
834                                 eap_sm_request_password(sm);
835                         return 0;
836                 }
837
838                 if (eap_get_config_password(sm, &len) == NULL) {
839                         wpa_printf(MSG_INFO,
840                                    "EAP-TTLS: Password not configured");
841                         eap_sm_request_password(sm);
842                         return 0;
843                 }
844         }
845
846         switch (phase2_type) {
847         case EAP_TTLS_PHASE2_EAP:
848                 res = eap_ttls_phase2_request_eap(sm, data, ret, hdr, resp);
849                 break;
850         case EAP_TTLS_PHASE2_MSCHAPV2:
851                 res = eap_ttls_phase2_request_mschapv2(sm, data, ret, resp);
852                 break;
853         case EAP_TTLS_PHASE2_MSCHAP:
854                 res = eap_ttls_phase2_request_mschap(sm, data, ret, resp);
855                 break;
856         case EAP_TTLS_PHASE2_PAP:
857                 res = eap_ttls_phase2_request_pap(sm, data, ret, resp);
858                 break;
859         case EAP_TTLS_PHASE2_CHAP:
860                 res = eap_ttls_phase2_request_chap(sm, data, ret, resp);
861                 break;
862         default:
863                 wpa_printf(MSG_ERROR, "EAP-TTLS: Phase 2 - Unknown");
864                 res = -1;
865                 break;
866         }
867
868         if (res < 0) {
869                 ret->methodState = METHOD_DONE;
870                 ret->decision = DECISION_FAIL;
871         }
872
873         return res;
874 }
875
876
877 struct ttls_parse_avp {
878         u8 *mschapv2;
879         u8 *eapdata;
880         size_t eap_len;
881         u8 *chbind_data;
882         size_t chbind_len;
883         int mschapv2_error;
884 };
885
886
887 static int eap_ttls_parse_attr_eap(const u8 *dpos, size_t dlen,
888                                    struct ttls_parse_avp *parse)
889 {
890         wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP - EAP Message");
891         if (parse->eapdata == NULL) {
892                 parse->eapdata = os_malloc(dlen);
893                 if (parse->eapdata == NULL) {
894                         wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
895                                    "memory for Phase 2 EAP data");
896                         return -1;
897                 }
898                 os_memcpy(parse->eapdata, dpos, dlen);
899                 parse->eap_len = dlen;
900         } else {
901                 u8 *neweap = os_realloc(parse->eapdata, parse->eap_len + dlen);
902                 if (neweap == NULL) {
903                         wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
904                                    "memory for Phase 2 EAP data");
905                         return -1;
906                 }
907                 os_memcpy(neweap + parse->eap_len, dpos, dlen);
908                 parse->eapdata = neweap;
909                 parse->eap_len += dlen;
910         }
911
912         return 0;
913 }
914
915
916 static int eap_ttls_parse_attr_chbind(const u8 *dpos, size_t dlen,
917                                    struct ttls_parse_avp *parse)
918 {
919         wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP - Channel Binding Message");
920
921         if (parse->chbind_data == NULL) {
922                 parse->chbind_data = os_malloc(dlen);
923                 if (parse->chbind_data == NULL) {
924                         wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
925                                    "memory for Phase 2 channel binding data");
926                         return -1;
927                 }
928                 os_memcpy(parse->chbind_data, dpos, dlen);
929                 parse->chbind_len = dlen;
930         } else {
931         /* TODO: can this really happen?  maybe just make this an error? */
932                 u8 *newchbind = os_realloc(parse->chbind_data,
933                                            parse->chbind_len + dlen);
934                 if (newchbind == NULL) {
935                         wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
936                                    "memory for Phase 2 channel binding data");
937                         return -1;
938                 }
939                 os_memcpy(newchbind + parse->chbind_len, dpos, dlen);
940                 parse->chbind_data = newchbind;
941                 parse->chbind_len += dlen;
942         }
943
944         return 0;
945 }
946
947
948 static int eap_ttls_parse_avp(u8 *pos, size_t left,
949                               struct ttls_parse_avp *parse)
950 {
951         struct ttls_avp *avp;
952         u32 avp_code, avp_length, vendor_id = 0;
953         u8 avp_flags, *dpos;
954         size_t dlen;
955
956         avp = (struct ttls_avp *) pos;
957         avp_code = be_to_host32(avp->avp_code);
958         avp_length = be_to_host32(avp->avp_length);
959         avp_flags = (avp_length >> 24) & 0xff;
960         avp_length &= 0xffffff;
961         wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP: code=%d flags=0x%02x "
962                    "length=%d", (int) avp_code, avp_flags,
963                    (int) avp_length);
964
965         if (avp_length > left) {
966                 wpa_printf(MSG_WARNING, "EAP-TTLS: AVP overflow "
967                            "(len=%d, left=%lu) - dropped",
968                            (int) avp_length, (unsigned long) left);
969                 return -1;
970         }
971
972         if (avp_length < sizeof(*avp)) {
973                 wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid AVP length %d",
974                            avp_length);
975                 return -1;
976         }
977
978         dpos = (u8 *) (avp + 1);
979         dlen = avp_length - sizeof(*avp);
980         if (avp_flags & AVP_FLAGS_VENDOR) {
981                 if (dlen < 4) {
982                         wpa_printf(MSG_WARNING, "EAP-TTLS: Vendor AVP "
983                                    "underflow");
984                         return -1;
985                 }
986                 vendor_id = WPA_GET_BE32(dpos);
987                 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP vendor_id %d",
988                            (int) vendor_id);
989                 dpos += 4;
990                 dlen -= 4;
991         }
992
993         wpa_hexdump(MSG_DEBUG, "EAP-TTLS: AVP data", dpos, dlen);
994
995         if (vendor_id == 0 && avp_code == RADIUS_ATTR_EAP_MESSAGE) {
996                 if (eap_ttls_parse_attr_eap(dpos, dlen, parse) < 0)
997                         return -1;
998         } else if (vendor_id == RADIUS_VENDOR_ID_UKERNA &&
999                    avp_code == RADIUS_ATTR_UKERNA_CHBIND) {
1000                 /* message containing channel binding data */
1001                 if (eap_ttls_parse_attr_chbind(dpos, dlen, parse) < 0)
1002                         return -1;
1003         } else if (vendor_id == 0 && avp_code == RADIUS_ATTR_REPLY_MESSAGE) {
1004                 /* This is an optional message that can be displayed to
1005                  * the user. */
1006                 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: AVP - Reply-Message",
1007                                   dpos, dlen);
1008         } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
1009                    avp_code == RADIUS_ATTR_MS_CHAP2_SUCCESS) {
1010                 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: MS-CHAP2-Success",
1011                                   dpos, dlen);
1012                 if (dlen != 43) {
1013                         wpa_printf(MSG_WARNING, "EAP-TTLS: Unexpected "
1014                                    "MS-CHAP2-Success length "
1015                                    "(len=%lu, expected 43)",
1016                                    (unsigned long) dlen);
1017                         return -1;
1018                 }
1019                 parse->mschapv2 = dpos;
1020         } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
1021                    avp_code == RADIUS_ATTR_MS_CHAP_ERROR) {
1022                 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: MS-CHAP-Error",
1023                                   dpos, dlen);
1024                 parse->mschapv2_error = 1;
1025         } else if (avp_flags & AVP_FLAGS_MANDATORY) {
1026                 wpa_printf(MSG_WARNING, "EAP-TTLS: Unsupported mandatory AVP "
1027                            "code %d vendor_id %d - dropped",
1028                            (int) avp_code, (int) vendor_id);
1029                 return -1;
1030         } else {
1031                 wpa_printf(MSG_DEBUG, "EAP-TTLS: Ignoring unsupported AVP "
1032                            "code %d vendor_id %d",
1033                            (int) avp_code, (int) vendor_id);
1034         }
1035
1036         return avp_length;
1037 }
1038
1039
1040 static int eap_ttls_parse_avps(struct wpabuf *in_decrypted,
1041                                struct ttls_parse_avp *parse)
1042 {
1043         u8 *pos;
1044         size_t left, pad;
1045         int avp_length;
1046
1047         pos = wpabuf_mhead(in_decrypted);
1048         left = wpabuf_len(in_decrypted);
1049         wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Decrypted Phase 2 AVPs", pos, left);
1050         if (left < sizeof(struct ttls_avp)) {
1051                 wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 AVP frame"
1052                            " len=%lu expected %lu or more - dropped",
1053                            (unsigned long) left,
1054                            (unsigned long) sizeof(struct ttls_avp));
1055                 return -1;
1056         }
1057
1058         /* Parse AVPs */
1059         os_memset(parse, 0, sizeof(*parse));
1060
1061         while (left > 0) {
1062                 avp_length = eap_ttls_parse_avp(pos, left, parse);
1063                 if (avp_length < 0)
1064                         return -1;
1065
1066                 pad = (4 - (avp_length & 3)) & 3;
1067                 pos += avp_length + pad;
1068                 if (left < avp_length + pad)
1069                         left = 0;
1070                 else
1071                         left -= avp_length + pad;
1072         }
1073
1074         return 0;
1075 }
1076
1077
1078 static u8 * eap_ttls_fake_identity_request(void)
1079 {
1080         struct eap_hdr *hdr;
1081         u8 *buf;
1082
1083         wpa_printf(MSG_DEBUG, "EAP-TTLS: empty data in beginning of "
1084                    "Phase 2 - use fake EAP-Request Identity");
1085         buf = os_malloc(sizeof(*hdr) + 1);
1086         if (buf == NULL) {
1087                 wpa_printf(MSG_WARNING, "EAP-TTLS: failed to allocate "
1088                            "memory for fake EAP-Identity Request");
1089                 return NULL;
1090         }
1091
1092         hdr = (struct eap_hdr *) buf;
1093         hdr->code = EAP_CODE_REQUEST;
1094         hdr->identifier = 0;
1095         hdr->length = host_to_be16(sizeof(*hdr) + 1);
1096         buf[sizeof(*hdr)] = EAP_TYPE_IDENTITY;
1097
1098         return buf;
1099 }
1100
1101
1102 static int eap_ttls_encrypt_response(struct eap_sm *sm,
1103                                      struct eap_ttls_data *data,
1104                                      struct wpabuf *resp, u8 identifier,
1105                                      struct wpabuf **out_data)
1106 {
1107         if (resp == NULL)
1108                 return 0;
1109
1110         wpa_hexdump_buf_key(MSG_DEBUG, "EAP-TTLS: Encrypting Phase 2 data",
1111                             resp);
1112         if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_TTLS,
1113                                  data->ttls_version, identifier,
1114                                  resp, out_data)) {
1115                 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to encrypt a Phase 2 "
1116                            "frame");
1117                 wpabuf_free(resp);
1118                 return -1;
1119         }
1120         wpabuf_free(resp);
1121
1122         return 0;
1123 }
1124
1125 static int eap_ttls_add_chbind_request(struct eap_sm *sm,
1126                                        struct eap_ttls_data *data,
1127                                        struct wpabuf **resp)
1128 {
1129         struct wpabuf *chbind_req, *res;
1130         int length = 1, i;
1131         struct eap_peer_config *config = eap_get_config(sm);
1132
1133         if (!config->chbind_config || config->chbind_config_len <= 0)
1134                 return -1;
1135
1136         for (i=0; i<config->chbind_config_len; i++) {
1137                 length += 3 + config->chbind_config[i].req_data_len;
1138         }
1139
1140         chbind_req = wpabuf_alloc(length);
1141         if (!chbind_req)
1142                 return -1;
1143
1144         wpabuf_put_u8(chbind_req, CHBIND_CODE_REQUEST);
1145         for (i=0; i<config->chbind_config_len; i++) {
1146                 struct eap_peer_chbind_config *chbind_config =
1147                         &config->chbind_config[i];
1148                 wpabuf_put_be16(chbind_req, chbind_config->req_data_len);
1149                 wpabuf_put_u8(chbind_req, chbind_config->nsid);
1150                 wpabuf_put_data(chbind_req, chbind_config->req_data,
1151                                 chbind_config->req_data_len);
1152         }
1153         if (eap_ttls_avp_radius_vsa_encapsulate(&chbind_req,
1154                                          RADIUS_VENDOR_ID_UKERNA,
1155                                          RADIUS_ATTR_UKERNA_CHBIND, 0) < 0)
1156                 return -1;
1157
1158         /* bleh. This will free *resp regardless of whether combined buffer
1159            alloc succeeds, which is not consistent with the other error
1160            condition behavior in this function */
1161         *resp = wpabuf_concat(chbind_req, *resp);
1162
1163         return (*resp) ? 0 : -1;
1164 }
1165
1166
1167 static int eap_ttls_process_chbind(struct eap_sm *sm,
1168                                    struct eap_ttls_data *data,
1169                                    struct eap_method_ret *ret,
1170                                    struct ttls_parse_avp *parse,
1171                                    struct wpabuf **resp)
1172 {
1173         size_t pos=0;
1174         u8 code;
1175         u16 len;
1176         struct chbind_hdr *hdr;
1177         struct eap_peer_config *config = eap_get_config(sm);
1178         int i, found;
1179
1180
1181         if (parse->chbind_data == NULL) {
1182                 wpa_printf(MSG_WARNING, "EAP-TTLS: No channel binding message "
1183                            "in the packet - dropped");
1184                 return -1;
1185         }
1186         if (parse->chbind_len < 1 ) {
1187                 wpa_printf(MSG_WARNING, "EAP-TTLS: bad channel binding response "
1188                            "frame (len=%lu, expected %lu or more) - dropped",
1189                            (unsigned long) parse->chbind_len,
1190                            (unsigned long) 1);
1191                 return -1;
1192         }
1193         code = parse->chbind_data[pos++];
1194         for (i=0; i<config->chbind_config_len; i++) {
1195                 struct eap_peer_chbind_config *chbind_config =
1196                         &config->chbind_config[i];
1197                 pos = 1;
1198                 found = 0;
1199                 while (pos+sizeof(*hdr) < parse->chbind_len) {
1200                         hdr = (struct chbind_hdr *)(&parse->chbind_data[pos]);
1201                         pos += sizeof(*hdr);
1202                         len = be_to_host16(hdr->len);
1203                         if (pos + len <= parse->chbind_len) {
1204                                 if (chbind_config->nsid == hdr->nsid)
1205                                         chbind_config->response_cb(
1206                                                                    chbind_config->ctx,
1207                                                                    code, hdr->nsid,
1208                                                                    &parse->chbind_data[pos], len);
1209                                 found  = 1;
1210                         }
1211                         pos += len;
1212                 }
1213                 if (pos != parse->chbind_len) {
1214                         wpa_printf(MSG_WARNING, "EAP-TTLS: bad channel binding response "
1215                                    "frame (parsed len=%lu, expected %lu) - dropped",
1216                                    (unsigned long) pos,
1217                                    (unsigned long) parse->chbind_len);
1218                         return -1;
1219                 }
1220                 if (!found) {
1221                         chbind_config->response_cb(
1222                                                    chbind_config->ctx,
1223                                                    code, chbind_config->nsid,
1224                                                    NULL, 0);
1225                 }
1226
1227         }
1228         return 0;
1229 }
1230
1231 static int eap_ttls_process_phase2_eap(struct eap_sm *sm,
1232                                        struct eap_ttls_data *data,
1233                                        struct eap_method_ret *ret,
1234                                        struct ttls_parse_avp *parse,
1235                                        struct wpabuf **resp)
1236 {
1237         struct eap_hdr *hdr;
1238         size_t len;
1239
1240         if (parse->eapdata == NULL) {
1241                 wpa_printf(MSG_WARNING, "EAP-TTLS: No EAP Message in the "
1242                            "packet - dropped");
1243                 return -1;
1244         }
1245
1246         wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP",
1247                     parse->eapdata, parse->eap_len);
1248         hdr = (struct eap_hdr *) parse->eapdata;
1249
1250         if (parse->eap_len < sizeof(*hdr)) {
1251                 wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 EAP "
1252                            "frame (len=%lu, expected %lu or more) - dropped",
1253                            (unsigned long) parse->eap_len,
1254                            (unsigned long) sizeof(*hdr));
1255                 return -1;
1256         }
1257         len = be_to_host16(hdr->length);
1258         if (len > parse->eap_len) {
1259                 wpa_printf(MSG_INFO, "EAP-TTLS: Length mismatch in Phase 2 "
1260                            "EAP frame (EAP hdr len=%lu, EAP data len in "
1261                            "AVP=%lu)",
1262                            (unsigned long) len,
1263                            (unsigned long) parse->eap_len);
1264                 return -1;
1265         }
1266         wpa_printf(MSG_DEBUG, "EAP-TTLS: received Phase 2: code=%d "
1267                    "identifier=%d length=%lu",
1268                    hdr->code, hdr->identifier, (unsigned long) len);
1269         switch (hdr->code) {
1270         case EAP_CODE_REQUEST:
1271                 if (eap_ttls_phase2_request(sm, data, ret, hdr, resp)) {
1272                         wpa_printf(MSG_INFO, "EAP-TTLS: Phase2 Request "
1273                                    "processing failed");
1274                         return -1;
1275                 }
1276                 break;
1277         default:
1278                 wpa_printf(MSG_INFO, "EAP-TTLS: Unexpected code=%d in "
1279                            "Phase 2 EAP header", hdr->code);
1280                 return -1;
1281         }
1282
1283         return 0;
1284 }
1285
1286
1287 static int eap_ttls_process_phase2_mschapv2(struct eap_sm *sm,
1288                                             struct eap_ttls_data *data,
1289                                             struct eap_method_ret *ret,
1290                                             struct ttls_parse_avp *parse)
1291 {
1292 #ifdef EAP_MSCHAPv2
1293         if (parse->mschapv2_error) {
1294                 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Received "
1295                            "MS-CHAP-Error - failed");
1296                 ret->methodState = METHOD_DONE;
1297                 ret->decision = DECISION_FAIL;
1298                 /* Reply with empty data to ACK error */
1299                 return 1;
1300         }
1301
1302         if (parse->mschapv2 == NULL) {
1303 #ifdef EAP_TNC
1304                 if (data->phase2_success && parse->eapdata) {
1305                         /*
1306                          * Allow EAP-TNC to be started after successfully
1307                          * completed MSCHAPV2.
1308                          */
1309                         return 1;
1310                 }
1311 #endif /* EAP_TNC */
1312                 wpa_printf(MSG_WARNING, "EAP-TTLS: no MS-CHAP2-Success AVP "
1313                            "received for Phase2 MSCHAPV2");
1314                 return -1;
1315         }
1316         if (parse->mschapv2[0] != data->ident) {
1317                 wpa_printf(MSG_WARNING, "EAP-TTLS: Ident mismatch for Phase 2 "
1318                            "MSCHAPV2 (received Ident 0x%02x, expected 0x%02x)",
1319                            parse->mschapv2[0], data->ident);
1320                 return -1;
1321         }
1322         if (!data->auth_response_valid ||
1323             mschapv2_verify_auth_response(data->auth_response,
1324                                           parse->mschapv2 + 1, 42)) {
1325                 wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid authenticator "
1326                            "response in Phase 2 MSCHAPV2 success request");
1327                 return -1;
1328         }
1329
1330         wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 MSCHAPV2 "
1331                    "authentication succeeded");
1332         ret->methodState = METHOD_DONE;
1333         ret->decision = DECISION_UNCOND_SUCC;
1334         data->phase2_success = 1;
1335
1336         /*
1337          * Reply with empty data; authentication server will reply
1338          * with EAP-Success after this.
1339          */
1340         return 1;
1341 #else /* EAP_MSCHAPv2 */
1342         wpa_printf(MSG_ERROR, "EAP-TTLS: MSCHAPv2 not included in the build");
1343         return -1;
1344 #endif /* EAP_MSCHAPv2 */
1345 }
1346
1347
1348 #ifdef EAP_TNC
1349 static int eap_ttls_process_tnc_start(struct eap_sm *sm,
1350                                       struct eap_ttls_data *data,
1351                                       struct eap_method_ret *ret,
1352                                       struct ttls_parse_avp *parse,
1353                                       struct wpabuf **resp)
1354 {
1355         /* TNC uses inner EAP method after non-EAP TTLS phase 2. */
1356         if (parse->eapdata == NULL) {
1357                 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received "
1358                            "unexpected tunneled data (no EAP)");
1359                 return -1;
1360         }
1361
1362         if (!data->ready_for_tnc) {
1363                 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received "
1364                            "EAP after non-EAP, but not ready for TNC");
1365                 return -1;
1366         }
1367
1368         wpa_printf(MSG_DEBUG, "EAP-TTLS: Start TNC after completed "
1369                    "non-EAP method");
1370         data->tnc_started = 1;
1371
1372         if (eap_ttls_process_phase2_eap(sm, data, ret, parse, resp) < 0)
1373                 return -1;
1374
1375         return 0;
1376 }
1377 #endif /* EAP_TNC */
1378
1379
1380 static int eap_ttls_process_decrypted(struct eap_sm *sm,
1381                                       struct eap_ttls_data *data,
1382                                       struct eap_method_ret *ret,
1383                                       u8 identifier,
1384                                       struct ttls_parse_avp *parse,
1385                                       struct wpabuf *in_decrypted,
1386                                       struct wpabuf **out_data)
1387 {
1388         struct wpabuf *resp = NULL;
1389         struct eap_peer_config *config = eap_get_config(sm);
1390         int res;
1391         enum phase2_types phase2_type = data->phase2_type;
1392
1393 #ifdef EAP_TNC
1394         if (data->tnc_started)
1395                 phase2_type = EAP_TTLS_PHASE2_EAP;
1396 #endif /* EAP_TNC */
1397
1398         /* handle channel binding response here */
1399         if (parse->chbind_data) {
1400                 /* received channel binding repsonse */
1401                 if (eap_ttls_process_chbind(sm, data, ret, parse, &resp) < 0)
1402                         return -1;
1403                 if (data->done_butfor_cb) {
1404                         ret->methodState = METHOD_DONE;
1405                         ret->decision = data->cbDecision;
1406                         data->phase2_success = 1;
1407                         return 1; /*request ack*/
1408                 }
1409         }
1410
1411         switch (phase2_type) {
1412         case EAP_TTLS_PHASE2_EAP:
1413                 if (eap_ttls_process_phase2_eap(sm, data, ret, parse, &resp) <
1414                     0)
1415                         return -1;
1416                 break;
1417         case EAP_TTLS_PHASE2_MSCHAPV2:
1418                 res = eap_ttls_process_phase2_mschapv2(sm, data, ret, parse);
1419 #ifdef EAP_TNC
1420                 if (res == 1 && parse->eapdata && data->phase2_success) {
1421                         /*
1422                          * TNC may be required as the next
1423                          * authentication method within the tunnel.
1424                          */
1425                         ret->methodState = METHOD_MAY_CONT;
1426                         data->ready_for_tnc = 1;
1427                         if (eap_ttls_process_tnc_start(sm, data, ret, parse,
1428                                                        &resp) == 0)
1429                                 break;
1430                 }
1431 #endif /* EAP_TNC */
1432                 return res;
1433         case EAP_TTLS_PHASE2_MSCHAP:
1434         case EAP_TTLS_PHASE2_PAP:
1435         case EAP_TTLS_PHASE2_CHAP:
1436 #ifdef EAP_TNC
1437                 if (eap_ttls_process_tnc_start(sm, data, ret, parse, &resp) <
1438                     0)
1439                         return -1;
1440                 break;
1441 #else /* EAP_TNC */
1442                 /* EAP-TTLS/{MSCHAP,PAP,CHAP} should not send any TLS tunneled
1443                  * requests to the supplicant */
1444                 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received unexpected "
1445                            "tunneled data");
1446                 return -1;
1447 #endif /* EAP_TNC */
1448         }
1449
1450         if (!resp && (config->pending_req_identity ||
1451                         config->pending_req_password ||
1452                         config->pending_req_otp ||
1453                         config->pending_req_new_password)) {
1454                 wpabuf_free(data->pending_phase2_req);
1455                 data->pending_phase2_req = wpabuf_dup(in_decrypted);
1456                 return 0;
1457         }
1458
1459         /* issue channel binding request when appropriate */
1460         if (config->chbind_config && config->chbind_config_len > 0 &&
1461                 !data->chbind_req_sent) {
1462                 if (eap_ttls_add_chbind_request(sm, data, &resp) < 0)
1463                         return -1;
1464                 data->chbind_req_sent = 1;
1465                 if (ret->methodState == METHOD_DONE) {
1466                         data->done_butfor_cb = 1;
1467                         data->cbDecision = ret->decision;
1468                         ret->methodState = METHOD_MAY_CONT;
1469                 }
1470         }
1471
1472         if (resp) {
1473                 if (eap_ttls_encrypt_response(sm, data, resp, identifier,
1474                                               out_data) < 0)
1475                         return -1;
1476         }
1477
1478         return 0;
1479 }
1480
1481
1482 static int eap_ttls_implicit_identity_request(struct eap_sm *sm,
1483                                               struct eap_ttls_data *data,
1484                                               struct eap_method_ret *ret,
1485                                               u8 identifier,
1486                                               struct wpabuf **out_data)
1487 {
1488         int retval = 0;
1489         struct eap_hdr *hdr;
1490         struct wpabuf *resp;
1491
1492         hdr = (struct eap_hdr *) eap_ttls_fake_identity_request();
1493         if (hdr == NULL) {
1494                 ret->methodState = METHOD_DONE;
1495                 ret->decision = DECISION_FAIL;
1496                 return -1;
1497         }
1498
1499         resp = NULL;
1500         if (eap_ttls_phase2_request(sm, data, ret, hdr, &resp)) {
1501                 wpa_printf(MSG_INFO, "EAP-TTLS: Phase2 Request "
1502                            "processing failed");
1503                 retval = -1;
1504         } else {
1505                 struct eap_peer_config *config = eap_get_config(sm);
1506                 if (resp == NULL &&
1507                     (config->pending_req_identity ||
1508                      config->pending_req_password ||
1509                      config->pending_req_otp ||
1510                      config->pending_req_new_password)) {
1511                         /*
1512                          * Use empty buffer to force implicit request
1513                          * processing when EAP request is re-processed after
1514                          * user input.
1515                          */
1516                         wpabuf_free(data->pending_phase2_req);
1517                         data->pending_phase2_req = wpabuf_alloc(0);
1518                 }
1519
1520                 retval = eap_ttls_encrypt_response(sm, data, resp, identifier,
1521                                                    out_data);
1522         }
1523
1524         os_free(hdr);
1525
1526         if (retval < 0) {
1527                 ret->methodState = METHOD_DONE;
1528                 ret->decision = DECISION_FAIL;
1529         }
1530
1531         return retval;
1532 }
1533
1534
1535 static int eap_ttls_phase2_start(struct eap_sm *sm, struct eap_ttls_data *data,
1536                                  struct eap_method_ret *ret, u8 identifier,
1537                                  struct wpabuf **out_data)
1538 {
1539         data->phase2_start = 0;
1540
1541         /*
1542          * EAP-TTLS does not use Phase2 on fast re-auth; this must be done only
1543          * if TLS part was indeed resuming a previous session. Most
1544          * Authentication Servers terminate EAP-TTLS before reaching this
1545          * point, but some do not. Make wpa_supplicant stop phase 2 here, if
1546          * needed.
1547          */
1548         if (data->reauth &&
1549             tls_connection_resumed(sm->ssl_ctx, data->ssl.conn)) {
1550                 wpa_printf(MSG_DEBUG, "EAP-TTLS: Session resumption - "
1551                            "skip phase 2");
1552                 *out_data = eap_peer_tls_build_ack(identifier, EAP_TYPE_TTLS,
1553                                                    data->ttls_version);
1554                 ret->methodState = METHOD_DONE;
1555                 ret->decision = DECISION_UNCOND_SUCC;
1556                 data->phase2_success = 1;
1557                 return 0;
1558         }
1559
1560         return eap_ttls_implicit_identity_request(sm, data, ret, identifier,
1561                                                   out_data);
1562 }
1563
1564
1565 static int eap_ttls_decrypt(struct eap_sm *sm, struct eap_ttls_data *data,
1566                             struct eap_method_ret *ret, u8 identifier,
1567                             const struct wpabuf *in_data,
1568                             struct wpabuf **out_data)
1569 {
1570         struct wpabuf *in_decrypted = NULL;
1571         int retval = 0;
1572         struct ttls_parse_avp parse;
1573
1574         os_memset(&parse, 0, sizeof(parse));
1575
1576         wpa_printf(MSG_DEBUG, "EAP-TTLS: received %lu bytes encrypted data for"
1577                    " Phase 2",
1578                    in_data ? (unsigned long) wpabuf_len(in_data) : 0);
1579
1580         if (data->pending_phase2_req) {
1581                 wpa_printf(MSG_DEBUG, "EAP-TTLS: Pending Phase 2 request - "
1582                            "skip decryption and use old data");
1583                 /* Clear TLS reassembly state. */
1584                 eap_peer_tls_reset_input(&data->ssl);
1585
1586                 in_decrypted = data->pending_phase2_req;
1587                 data->pending_phase2_req = NULL;
1588                 if (wpabuf_len(in_decrypted) == 0) {
1589                         wpabuf_free(in_decrypted);
1590                         return eap_ttls_implicit_identity_request(
1591                                 sm, data, ret, identifier, out_data);
1592                 }
1593                 goto continue_req;
1594         }
1595
1596         if ((in_data == NULL || wpabuf_len(in_data) == 0) &&
1597             data->phase2_start) {
1598                 return eap_ttls_phase2_start(sm, data, ret, identifier,
1599                                              out_data);
1600         }
1601
1602         if (in_data == NULL || wpabuf_len(in_data) == 0) {
1603                 /* Received TLS ACK - requesting more fragments */
1604                 return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_TTLS,
1605                                             data->ttls_version,
1606                                             identifier, NULL, out_data);
1607         }
1608
1609         retval = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
1610         if (retval)
1611                 goto done;
1612
1613 continue_req:
1614         data->phase2_start = 0;
1615
1616         if (eap_ttls_parse_avps(in_decrypted, &parse) < 0) {
1617                 retval = -1;
1618                 goto done;
1619         }
1620
1621         retval = eap_ttls_process_decrypted(sm, data, ret, identifier,
1622                                             &parse, in_decrypted, out_data);
1623
1624 done:
1625         wpabuf_free(in_decrypted);
1626         os_free(parse.eapdata);
1627
1628         if (retval < 0) {
1629                 ret->methodState = METHOD_DONE;
1630                 ret->decision = DECISION_FAIL;
1631         }
1632
1633         return retval;
1634 }
1635
1636
1637 static int eap_ttls_process_handshake(struct eap_sm *sm,
1638                                       struct eap_ttls_data *data,
1639                                       struct eap_method_ret *ret,
1640                                       u8 identifier,
1641                                       const struct wpabuf *in_data,
1642                                       struct wpabuf **out_data)
1643 {
1644         int res;
1645
1646         res = eap_peer_tls_process_helper(sm, &data->ssl, EAP_TYPE_TTLS,
1647                                           data->ttls_version, identifier,
1648                                           in_data, out_data);
1649         if (res < 0) {
1650                 wpa_printf(MSG_DEBUG, "EAP-TTLS: TLS processing failed");
1651                 ret->methodState = METHOD_DONE;
1652                 ret->decision = DECISION_FAIL;
1653                 return -1;
1654         }
1655
1656         if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
1657                 wpa_printf(MSG_DEBUG, "EAP-TTLS: TLS done, proceed to "
1658                            "Phase 2");
1659                 if (data->resuming) {
1660                         wpa_printf(MSG_DEBUG, "EAP-TTLS: fast reauth - may "
1661                                    "skip Phase 2");
1662                         ret->decision = DECISION_COND_SUCC;
1663                         ret->methodState = METHOD_MAY_CONT;
1664                 }
1665                 data->phase2_start = 1;
1666                 eap_ttls_v0_derive_key(sm, data);
1667
1668                 if (*out_data == NULL || wpabuf_len(*out_data) == 0) {
1669                         if (eap_ttls_decrypt(sm, data, ret, identifier,
1670                                              NULL, out_data)) {
1671                                 wpa_printf(MSG_WARNING, "EAP-TTLS: "
1672                                            "failed to process early "
1673                                            "start for Phase 2");
1674                         }
1675                         res = 0;
1676                 }
1677                 data->resuming = 0;
1678         }
1679
1680         if (res == 2) {
1681                 /*
1682                  * Application data included in the handshake message.
1683                  */
1684                 wpabuf_free(data->pending_phase2_req);
1685                 data->pending_phase2_req = *out_data;
1686                 *out_data = NULL;
1687                 res = eap_ttls_decrypt(sm, data, ret, identifier, in_data,
1688                                        out_data);
1689         }
1690
1691         return res;
1692 }
1693
1694
1695 static void eap_ttls_check_auth_status(struct eap_sm *sm, 
1696                                        struct eap_ttls_data *data,
1697                                        struct eap_method_ret *ret)
1698 {
1699         if (ret->methodState == METHOD_DONE) {
1700                 ret->allowNotifications = FALSE;
1701                 if (ret->decision == DECISION_UNCOND_SUCC ||
1702                     ret->decision == DECISION_COND_SUCC) {
1703                         wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
1704                                    "completed successfully");
1705                         data->phase2_success = 1;
1706 #ifdef EAP_TNC
1707                         if (!data->ready_for_tnc && !data->tnc_started) {
1708                                 /*
1709                                  * TNC may be required as the next
1710                                  * authentication method within the tunnel.
1711                                  */
1712                                 ret->methodState = METHOD_MAY_CONT;
1713                                 data->ready_for_tnc = 1;
1714                         }
1715 #endif /* EAP_TNC */
1716                 }
1717         } else if (ret->methodState == METHOD_MAY_CONT &&
1718                    (ret->decision == DECISION_UNCOND_SUCC ||
1719                     ret->decision == DECISION_COND_SUCC)) {
1720                         wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
1721                                    "completed successfully (MAY_CONT)");
1722                         data->phase2_success = 1;
1723         }
1724 }
1725
1726
1727 static struct wpabuf * eap_ttls_process(struct eap_sm *sm, void *priv,
1728                                         struct eap_method_ret *ret,
1729                                         const struct wpabuf *reqData)
1730 {
1731         size_t left;
1732         int res;
1733         u8 flags, id;
1734         struct wpabuf *resp;
1735         const u8 *pos;
1736         struct eap_ttls_data *data = priv;
1737         struct wpabuf msg;
1738
1739         pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_TTLS, ret,
1740                                         reqData, &left, &flags);
1741         if (pos == NULL)
1742                 return NULL;
1743         id = eap_get_id(reqData);
1744
1745         if (flags & EAP_TLS_FLAGS_START) {
1746                 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start (server ver=%d, own "
1747                            "ver=%d)", flags & EAP_TLS_VERSION_MASK,
1748                            data->ttls_version);
1749
1750                 /* RFC 5281, Ch. 9.2:
1751                  * "This packet MAY contain additional information in the form
1752                  * of AVPs, which may provide useful hints to the client"
1753                  * For now, ignore any potential extra data.
1754                  */
1755                 left = 0;
1756         }
1757
1758         wpabuf_set(&msg, pos, left);
1759
1760         resp = NULL;
1761         if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1762             !data->resuming) {
1763                 res = eap_ttls_decrypt(sm, data, ret, id, &msg, &resp);
1764         } else {
1765                 res = eap_ttls_process_handshake(sm, data, ret, id,
1766                                                  &msg, &resp);
1767         }
1768
1769         eap_ttls_check_auth_status(sm, data, ret);
1770
1771         /* FIX: what about res == -1? Could just move all error processing into
1772          * the other functions and get rid of this res==1 case here. */
1773         if (res == 1) {
1774                 wpabuf_free(resp);
1775                 return eap_peer_tls_build_ack(id, EAP_TYPE_TTLS,
1776                                               data->ttls_version);
1777         }
1778         return resp;
1779 }
1780
1781
1782 static Boolean eap_ttls_has_reauth_data(struct eap_sm *sm, void *priv)
1783 {
1784         struct eap_ttls_data *data = priv;
1785         return tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1786                 data->phase2_success;
1787 }
1788
1789
1790 static void eap_ttls_deinit_for_reauth(struct eap_sm *sm, void *priv)
1791 {
1792         struct eap_ttls_data *data = priv;
1793         wpabuf_free(data->pending_phase2_req);
1794         data->pending_phase2_req = NULL;
1795 #ifdef EAP_TNC
1796         data->ready_for_tnc = 0;
1797         data->tnc_started = 0;
1798 #endif /* EAP_TNC */
1799 }
1800
1801
1802 static void * eap_ttls_init_for_reauth(struct eap_sm *sm, void *priv)
1803 {
1804         struct eap_ttls_data *data = priv;
1805         eap_ttls_free_key(data);
1806         os_free(data->session_id);
1807         data->session_id = NULL;
1808         if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
1809                 os_free(data);
1810                 return NULL;
1811         }
1812         if (data->phase2_priv && data->phase2_method &&
1813             data->phase2_method->init_for_reauth)
1814                 data->phase2_method->init_for_reauth(sm, data->phase2_priv);
1815         data->phase2_start = 0;
1816         data->phase2_success = 0;
1817         data->resuming = 1;
1818         data->reauth = 1;
1819         return priv;
1820 }
1821
1822
1823 static int eap_ttls_get_status(struct eap_sm *sm, void *priv, char *buf,
1824                                size_t buflen, int verbose)
1825 {
1826         struct eap_ttls_data *data = priv;
1827         int len, ret;
1828
1829         len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
1830         ret = os_snprintf(buf + len, buflen - len,
1831                           "EAP-TTLSv%d Phase2 method=",
1832                           data->ttls_version);
1833         if (os_snprintf_error(buflen - len, ret))
1834                 return len;
1835         len += ret;
1836         switch (data->phase2_type) {
1837         case EAP_TTLS_PHASE2_EAP:
1838                 ret = os_snprintf(buf + len, buflen - len, "EAP-%s\n",
1839                                   data->phase2_method ?
1840                                   data->phase2_method->name : "?");
1841                 break;
1842         case EAP_TTLS_PHASE2_MSCHAPV2:
1843                 ret = os_snprintf(buf + len, buflen - len, "MSCHAPV2\n");
1844                 break;
1845         case EAP_TTLS_PHASE2_MSCHAP:
1846                 ret = os_snprintf(buf + len, buflen - len, "MSCHAP\n");
1847                 break;
1848         case EAP_TTLS_PHASE2_PAP:
1849                 ret = os_snprintf(buf + len, buflen - len, "PAP\n");
1850                 break;
1851         case EAP_TTLS_PHASE2_CHAP:
1852                 ret = os_snprintf(buf + len, buflen - len, "CHAP\n");
1853                 break;
1854         default:
1855                 ret = 0;
1856                 break;
1857         }
1858         if (os_snprintf_error(buflen - len, ret))
1859                 return len;
1860         len += ret;
1861
1862         return len;
1863 }
1864
1865
1866 static Boolean eap_ttls_isKeyAvailable(struct eap_sm *sm, void *priv)
1867 {
1868         struct eap_ttls_data *data = priv;
1869         return data->key_data != NULL && data->phase2_success;
1870 }
1871
1872
1873 static u8 * eap_ttls_getKey(struct eap_sm *sm, void *priv, size_t *len)
1874 {
1875         struct eap_ttls_data *data = priv;
1876         u8 *key;
1877
1878         if (data->key_data == NULL || !data->phase2_success)
1879                 return NULL;
1880
1881         key = os_malloc(EAP_TLS_KEY_LEN);
1882         if (key == NULL)
1883                 return NULL;
1884
1885         *len = EAP_TLS_KEY_LEN;
1886         os_memcpy(key, data->key_data, EAP_TLS_KEY_LEN);
1887
1888         return key;
1889 }
1890
1891
1892 static u8 * eap_ttls_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
1893 {
1894         struct eap_ttls_data *data = priv;
1895         u8 *id;
1896
1897         if (data->session_id == NULL || !data->phase2_success)
1898                 return NULL;
1899
1900         id = os_malloc(data->id_len);
1901         if (id == NULL)
1902                 return NULL;
1903
1904         *len = data->id_len;
1905         os_memcpy(id, data->session_id, data->id_len);
1906
1907         return id;
1908 }
1909
1910
1911 static u8 * eap_ttls_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
1912 {
1913         struct eap_ttls_data *data = priv;
1914         u8 *key;
1915
1916         if (data->key_data == NULL)
1917                 return NULL;
1918
1919         key = os_malloc(EAP_EMSK_LEN);
1920         if (key == NULL)
1921                 return NULL;
1922
1923         *len = EAP_EMSK_LEN;
1924         os_memcpy(key, data->key_data + EAP_TLS_KEY_LEN, EAP_EMSK_LEN);
1925
1926         return key;
1927 }
1928
1929
1930 int eap_peer_ttls_register(void)
1931 {
1932         struct eap_method *eap;
1933         int ret;
1934
1935         eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
1936                                     EAP_VENDOR_IETF, EAP_TYPE_TTLS, "TTLS");
1937         if (eap == NULL)
1938                 return -1;
1939
1940         eap->init = eap_ttls_init;
1941         eap->deinit = eap_ttls_deinit;
1942         eap->process = eap_ttls_process;
1943         eap->isKeyAvailable = eap_ttls_isKeyAvailable;
1944         eap->getKey = eap_ttls_getKey;
1945         eap->getSessionId = eap_ttls_get_session_id;
1946         eap->get_status = eap_ttls_get_status;
1947         eap->has_reauth_data = eap_ttls_has_reauth_data;
1948         eap->deinit_for_reauth = eap_ttls_deinit_for_reauth;
1949         eap->init_for_reauth = eap_ttls_init_for_reauth;
1950         eap->get_emsk = eap_ttls_get_emsk;
1951
1952         ret = eap_peer_method_register(eap);
1953         if (ret)
1954                 eap_peer_method_free(eap);
1955         return ret;
1956 }