source: rtems-libbsd/freebsd/contrib/wpa/src/eap_peer/eap_ttls.c @ 9c9d11b

55-freebsd-126-freebsd-12
Last change on this file since 9c9d11b was 9c9d11b, checked in by Sichen Zhao <1473996754@…>, on 08/01/17 at 12:43:41

Import wpa from FreeBSD

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