~ubuntu-branches/ubuntu/gutsy/wpasupplicant/gutsy

« back to all changes in this revision

Viewing changes to src/eap_server/eap_ttls.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler, Alexander Sack
  • Date: 2007-08-26 16:06:57 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20070826160657-2m8pxoweuxe8f93t
Tags: 0.6.0+0.5.8-0ubuntu1
* New upstream release
* remove patch 11_erroneous_manpage_ref, applied upstream
* remove patch 25_wpas_dbus_unregister_iface_fix, applied upstream

[ Alexander Sack ]
* bumping upstream version to replace development version 0.6.0 with
  this package from stable release branch.
* attempt to fix wierd timeout and high latency issues by going
  back to stable upstream version (0.5.9) (LP: #140763,
  LP: #141233).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * hostapd / EAP-TTLS (draft-ietf-pppext-eap-ttls-05.txt)
3
 
 * Copyright (c) 2004-2007, Jouni Malinen <j@w1.fi>
4
 
 *
5
 
 * This program is free software; you can redistribute it and/or modify
6
 
 * it under the terms of the GNU General Public License version 2 as
7
 
 * published by the Free Software Foundation.
8
 
 *
9
 
 * Alternatively, this software may be distributed under the terms of BSD
10
 
 * license.
11
 
 *
12
 
 * See README and COPYING for more details.
13
 
 */
14
 
 
15
 
#include "includes.h"
16
 
 
17
 
#include "common.h"
18
 
#include "eap_server/eap_i.h"
19
 
#include "eap_server/eap_tls_common.h"
20
 
#include "ms_funcs.h"
21
 
#include "md5.h"
22
 
#include "sha1.h"
23
 
#include "crypto.h"
24
 
#include "tls.h"
25
 
#include "eap_common/eap_ttls.h"
26
 
 
27
 
 
28
 
/* Maximum supported TTLS version
29
 
 * 0 = draft-ietf-pppext-eap-ttls-03.txt / draft-funk-eap-ttls-v0-00.txt
30
 
 * 1 = draft-funk-eap-ttls-v1-00.txt
31
 
 */
32
 
#ifndef EAP_TTLS_VERSION
33
 
#define EAP_TTLS_VERSION 0 /* TTLSv1 implementation is not yet complete */
34
 
#endif /* EAP_TTLS_VERSION */
35
 
 
36
 
 
37
 
#define MSCHAPV2_KEY_LEN 16
38
 
 
39
 
 
40
 
static void eap_ttls_reset(struct eap_sm *sm, void *priv);
41
 
 
42
 
 
43
 
struct eap_ttls_data {
44
 
        struct eap_ssl_data ssl;
45
 
        enum {
46
 
                START, PHASE1, PHASE2_START, PHASE2_METHOD,
47
 
                PHASE2_MSCHAPV2_RESP, PHASE_FINISHED, SUCCESS, FAILURE
48
 
        } state;
49
 
 
50
 
        int ttls_version;
51
 
        int force_version;
52
 
        const struct eap_method *phase2_method;
53
 
        void *phase2_priv;
54
 
        int mschapv2_resp_ok;
55
 
        u8 mschapv2_auth_response[20];
56
 
        u8 mschapv2_ident;
57
 
        int tls_ia_configured;
58
 
};
59
 
 
60
 
 
61
 
static const char * eap_ttls_state_txt(int state)
62
 
{
63
 
        switch (state) {
64
 
        case START:
65
 
                return "START";
66
 
        case PHASE1:
67
 
                return "PHASE1";
68
 
        case PHASE2_START:
69
 
                return "PHASE2_START";
70
 
        case PHASE2_METHOD:
71
 
                return "PHASE2_METHOD";
72
 
        case PHASE2_MSCHAPV2_RESP:
73
 
                return "PHASE2_MSCHAPV2_RESP";
74
 
        case PHASE_FINISHED:
75
 
                return "PHASE_FINISHED";
76
 
        case SUCCESS:
77
 
                return "SUCCESS";
78
 
        case FAILURE:
79
 
                return "FAILURE";
80
 
        default:
81
 
                return "Unknown?!";
82
 
        }
83
 
}
84
 
 
85
 
 
86
 
static void eap_ttls_state(struct eap_ttls_data *data, int state)
87
 
{
88
 
        wpa_printf(MSG_DEBUG, "EAP-TTLS: %s -> %s",
89
 
                   eap_ttls_state_txt(data->state),
90
 
                   eap_ttls_state_txt(state));
91
 
        data->state = state;
92
 
}
93
 
 
94
 
 
95
 
static u8 * eap_ttls_avp_hdr(u8 *avphdr, u32 avp_code, u32 vendor_id,
96
 
                             int mandatory, size_t len)
97
 
{
98
 
        struct ttls_avp_vendor *avp;
99
 
        u8 flags;
100
 
        size_t hdrlen;
101
 
 
102
 
        avp = (struct ttls_avp_vendor *) avphdr;
103
 
        flags = mandatory ? AVP_FLAGS_MANDATORY : 0;
104
 
        if (vendor_id) {
105
 
                flags |= AVP_FLAGS_VENDOR;
106
 
                hdrlen = sizeof(*avp);
107
 
                avp->vendor_id = host_to_be32(vendor_id);
108
 
        } else {
109
 
                hdrlen = sizeof(struct ttls_avp);
110
 
        }
111
 
 
112
 
        avp->avp_code = host_to_be32(avp_code);
113
 
        avp->avp_length = host_to_be32((flags << 24) | (hdrlen + len));
114
 
 
115
 
        return avphdr + hdrlen;
116
 
}
117
 
 
118
 
 
119
 
static int eap_ttls_avp_encapsulate(u8 **resp, size_t *resp_len, u32 avp_code,
120
 
                                    int mandatory)
121
 
{
122
 
        u8 *avp, *pos;
123
 
 
124
 
        avp = malloc(sizeof(struct ttls_avp) + *resp_len + 4);
125
 
        if (avp == NULL) {
126
 
                free(*resp);
127
 
                *resp_len = 0;
128
 
                return -1;
129
 
        }
130
 
 
131
 
        pos = eap_ttls_avp_hdr(avp, avp_code, 0, mandatory, *resp_len);
132
 
        memcpy(pos, *resp, *resp_len);
133
 
        pos += *resp_len;
134
 
        AVP_PAD(avp, pos);
135
 
        free(*resp);
136
 
        *resp = avp;
137
 
        *resp_len = pos - avp;
138
 
        return 0;
139
 
}
140
 
 
141
 
 
142
 
struct eap_ttls_avp {
143
 
         /* Note: eap is allocated memory; caller is responsible for freeing
144
 
          * it. All the other pointers are pointing to the packet data, i.e.,
145
 
          * they must not be freed separately. */
146
 
        u8 *eap;
147
 
        size_t eap_len;
148
 
        u8 *user_name;
149
 
        size_t user_name_len;
150
 
        u8 *user_password;
151
 
        size_t user_password_len;
152
 
        u8 *chap_challenge;
153
 
        size_t chap_challenge_len;
154
 
        u8 *chap_password;
155
 
        size_t chap_password_len;
156
 
        u8 *mschap_challenge;
157
 
        size_t mschap_challenge_len;
158
 
        u8 *mschap_response;
159
 
        size_t mschap_response_len;
160
 
        u8 *mschap2_response;
161
 
        size_t mschap2_response_len;
162
 
};
163
 
 
164
 
 
165
 
static int eap_ttls_avp_parse(u8 *buf, size_t len, struct eap_ttls_avp *parse)
166
 
{
167
 
        struct ttls_avp *avp;
168
 
        u8 *pos;
169
 
        int left;
170
 
 
171
 
        pos = buf;
172
 
        left = len;
173
 
        memset(parse, 0, sizeof(*parse));
174
 
 
175
 
        while (left > 0) {
176
 
                u32 avp_code, avp_length, vendor_id = 0;
177
 
                u8 avp_flags, *dpos;
178
 
                size_t pad, dlen;
179
 
                avp = (struct ttls_avp *) pos;
180
 
                avp_code = be_to_host32(avp->avp_code);
181
 
                avp_length = be_to_host32(avp->avp_length);
182
 
                avp_flags = (avp_length >> 24) & 0xff;
183
 
                avp_length &= 0xffffff;
184
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP: code=%d flags=0x%02x "
185
 
                           "length=%d", (int) avp_code, avp_flags,
186
 
                           (int) avp_length);
187
 
                if ((int) avp_length > left) {
188
 
                        wpa_printf(MSG_WARNING, "EAP-TTLS: AVP overflow "
189
 
                                   "(len=%d, left=%d) - dropped",
190
 
                                   (int) avp_length, left);
191
 
                        goto fail;
192
 
                }
193
 
                if (avp_length < sizeof(*avp)) {
194
 
                        wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid AVP length "
195
 
                                   "%d", avp_length);
196
 
                        goto fail;
197
 
                }
198
 
                dpos = (u8 *) (avp + 1);
199
 
                dlen = avp_length - sizeof(*avp);
200
 
                if (avp_flags & AVP_FLAGS_VENDOR) {
201
 
                        if (dlen < 4) {
202
 
                                wpa_printf(MSG_WARNING, "EAP-TTLS: vendor AVP "
203
 
                                           "underflow");
204
 
                                goto fail;
205
 
                        }
206
 
                        vendor_id = be_to_host32(* (be32 *) dpos);
207
 
                        wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP vendor_id %d",
208
 
                                   (int) vendor_id);
209
 
                        dpos += 4;
210
 
                        dlen -= 4;
211
 
                }
212
 
 
213
 
                wpa_hexdump(MSG_DEBUG, "EAP-TTLS: AVP data", dpos, dlen);
214
 
 
215
 
                if (vendor_id == 0 && avp_code == RADIUS_ATTR_EAP_MESSAGE) {
216
 
                        wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP - EAP Message");
217
 
                        if (parse->eap == NULL) {
218
 
                                parse->eap = malloc(dlen);
219
 
                                if (parse->eap == NULL) {
220
 
                                        wpa_printf(MSG_WARNING, "EAP-TTLS: "
221
 
                                                   "failed to allocate memory "
222
 
                                                   "for Phase 2 EAP data");
223
 
                                        goto fail;
224
 
                                }
225
 
                                memcpy(parse->eap, dpos, dlen);
226
 
                                parse->eap_len = dlen;
227
 
                        } else {
228
 
                                u8 *neweap = realloc(parse->eap,
229
 
                                                     parse->eap_len + dlen);
230
 
                                if (neweap == NULL) {
231
 
                                        wpa_printf(MSG_WARNING, "EAP-TTLS: "
232
 
                                                   "failed to allocate memory "
233
 
                                                   "for Phase 2 EAP data");
234
 
                                        goto fail;
235
 
                                }
236
 
                                memcpy(neweap + parse->eap_len, dpos, dlen);
237
 
                                parse->eap = neweap;
238
 
                                parse->eap_len += dlen;
239
 
                        }
240
 
                } else if (vendor_id == 0 &&
241
 
                           avp_code == RADIUS_ATTR_USER_NAME) {
242
 
                        wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: User-Name",
243
 
                                          dpos, dlen);
244
 
                        parse->user_name = dpos;
245
 
                        parse->user_name_len = dlen;
246
 
                } else if (vendor_id == 0 &&
247
 
                           avp_code == RADIUS_ATTR_USER_PASSWORD) {
248
 
                        u8 *password = dpos;
249
 
                        size_t password_len = dlen;
250
 
                        while (password_len > 0 &&
251
 
                               password[password_len - 1] == '\0') {
252
 
                                password_len--;
253
 
                        }
254
 
                        wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: "
255
 
                                              "User-Password (PAP)",
256
 
                                              password, password_len);
257
 
                        parse->user_password = password;
258
 
                        parse->user_password_len = password_len;
259
 
                } else if (vendor_id == 0 &&
260
 
                           avp_code == RADIUS_ATTR_CHAP_CHALLENGE) {
261
 
                        wpa_hexdump(MSG_DEBUG,
262
 
                                    "EAP-TTLS: CHAP-Challenge (CHAP)",
263
 
                                    dpos, dlen);
264
 
                        parse->chap_challenge = dpos;
265
 
                        parse->chap_challenge_len = dlen;
266
 
                } else if (vendor_id == 0 &&
267
 
                           avp_code == RADIUS_ATTR_CHAP_PASSWORD) {
268
 
                        wpa_hexdump(MSG_DEBUG,
269
 
                                    "EAP-TTLS: CHAP-Password (CHAP)",
270
 
                                    dpos, dlen);
271
 
                        parse->chap_password = dpos;
272
 
                        parse->chap_password_len = dlen;
273
 
                } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
274
 
                           avp_code == RADIUS_ATTR_MS_CHAP_CHALLENGE) {
275
 
                        wpa_hexdump(MSG_DEBUG,
276
 
                                    "EAP-TTLS: MS-CHAP-Challenge",
277
 
                                    dpos, dlen);
278
 
                        parse->mschap_challenge = dpos;
279
 
                        parse->mschap_challenge_len = dlen;
280
 
                } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
281
 
                           avp_code == RADIUS_ATTR_MS_CHAP_RESPONSE) {
282
 
                        wpa_hexdump(MSG_DEBUG,
283
 
                                    "EAP-TTLS: MS-CHAP-Response (MSCHAP)",
284
 
                                    dpos, dlen);
285
 
                        parse->mschap_response = dpos;
286
 
                        parse->mschap_response_len = dlen;
287
 
                } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
288
 
                           avp_code == RADIUS_ATTR_MS_CHAP2_RESPONSE) {
289
 
                        wpa_hexdump(MSG_DEBUG,
290
 
                                    "EAP-TTLS: MS-CHAP2-Response (MSCHAPV2)",
291
 
                                    dpos, dlen);
292
 
                        parse->mschap2_response = dpos;
293
 
                        parse->mschap2_response_len = dlen;
294
 
                } else if (avp_flags & AVP_FLAGS_MANDATORY) {
295
 
                        wpa_printf(MSG_WARNING, "EAP-TTLS: Unsupported "
296
 
                                   "mandatory AVP code %d vendor_id %d - "
297
 
                                   "dropped", (int) avp_code, (int) vendor_id);
298
 
                        goto fail;
299
 
                } else {
300
 
                        wpa_printf(MSG_DEBUG, "EAP-TTLS: Ignoring unsupported "
301
 
                                   "AVP code %d vendor_id %d",
302
 
                                   (int) avp_code, (int) vendor_id);
303
 
                }
304
 
 
305
 
                pad = (4 - (avp_length & 3)) & 3;
306
 
                pos += avp_length + pad;
307
 
                left -= avp_length + pad;
308
 
        }
309
 
 
310
 
        return 0;
311
 
 
312
 
fail:
313
 
        free(parse->eap);
314
 
        parse->eap = NULL;
315
 
        return -1;
316
 
}
317
 
 
318
 
 
319
 
static u8 * eap_ttls_implicit_challenge(struct eap_sm *sm,
320
 
                                        struct eap_ttls_data *data, size_t len)
321
 
{
322
 
        struct tls_keys keys;
323
 
        u8 *challenge, *rnd;
324
 
 
325
 
        if (data->ttls_version == 0) {
326
 
                return eap_server_tls_derive_key(sm, &data->ssl,
327
 
                                                 "ttls challenge", len);
328
 
        }
329
 
 
330
 
        memset(&keys, 0, sizeof(keys));
331
 
        if (tls_connection_get_keys(sm->ssl_ctx, data->ssl.conn, &keys) ||
332
 
            keys.client_random == NULL || keys.server_random == NULL ||
333
 
            keys.inner_secret == NULL) {
334
 
                wpa_printf(MSG_INFO, "EAP-TTLS: Could not get inner secret, "
335
 
                           "client random, or server random to derive "
336
 
                           "implicit challenge");
337
 
                return NULL;
338
 
        }
339
 
 
340
 
        rnd = malloc(keys.client_random_len + keys.server_random_len);
341
 
        challenge = malloc(len);
342
 
        if (rnd == NULL || challenge == NULL) {
343
 
                wpa_printf(MSG_INFO, "EAP-TTLS: No memory for implicit "
344
 
                           "challenge derivation");
345
 
                free(rnd);
346
 
                free(challenge);
347
 
                return NULL;
348
 
        }
349
 
        memcpy(rnd, keys.server_random, keys.server_random_len);
350
 
        memcpy(rnd + keys.server_random_len, keys.client_random,
351
 
               keys.client_random_len);
352
 
 
353
 
        if (tls_prf(keys.inner_secret, keys.inner_secret_len,
354
 
                    "inner application challenge", rnd,
355
 
                    keys.client_random_len + keys.server_random_len,
356
 
                    challenge, len)) {
357
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to derive implicit "
358
 
                           "challenge");
359
 
                free(rnd);
360
 
                free(challenge);
361
 
                return NULL;
362
 
        }
363
 
 
364
 
        free(rnd);
365
 
 
366
 
        wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived implicit challenge",
367
 
                        challenge, len);
368
 
 
369
 
        return challenge;
370
 
}
371
 
 
372
 
 
373
 
static void * eap_ttls_init(struct eap_sm *sm)
374
 
{
375
 
        struct eap_ttls_data *data;
376
 
 
377
 
        data = os_zalloc(sizeof(*data));
378
 
        if (data == NULL)
379
 
                return NULL;
380
 
        data->ttls_version = EAP_TTLS_VERSION;
381
 
        data->force_version = -1;
382
 
        if (sm->user && sm->user->force_version >= 0) {
383
 
                data->force_version = sm->user->force_version;
384
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS: forcing version %d",
385
 
                           data->force_version);
386
 
                data->ttls_version = data->force_version;
387
 
        }
388
 
        data->state = START;
389
 
 
390
 
        if (!(tls_capabilities(sm->ssl_ctx) & TLS_CAPABILITY_IA) &&
391
 
            data->ttls_version > 0) {
392
 
                if (data->force_version > 0) {
393
 
                        wpa_printf(MSG_INFO, "EAP-TTLS: Forced TTLSv%d and "
394
 
                                   "TLS library does not support TLS/IA.",
395
 
                                   data->force_version);
396
 
                        eap_ttls_reset(sm, data);
397
 
                        return NULL;
398
 
                }
399
 
                data->ttls_version = 0;
400
 
        }
401
 
 
402
 
        if (eap_server_tls_ssl_init(sm, &data->ssl, 0)) {
403
 
                wpa_printf(MSG_INFO, "EAP-TTLS: Failed to initialize SSL.");
404
 
                eap_ttls_reset(sm, data);
405
 
                return NULL;
406
 
        }
407
 
 
408
 
        return data;
409
 
}
410
 
 
411
 
 
412
 
static void eap_ttls_reset(struct eap_sm *sm, void *priv)
413
 
{
414
 
        struct eap_ttls_data *data = priv;
415
 
        if (data == NULL)
416
 
                return;
417
 
        if (data->phase2_priv && data->phase2_method)
418
 
                data->phase2_method->reset(sm, data->phase2_priv);
419
 
        eap_server_tls_ssl_deinit(sm, &data->ssl);
420
 
        free(data);
421
 
}
422
 
 
423
 
 
424
 
static u8 * eap_ttls_build_start(struct eap_sm *sm, struct eap_ttls_data *data,
425
 
                                 int id, size_t *reqDataLen)
426
 
{
427
 
        struct eap_hdr *req;
428
 
        u8 *pos;
429
 
 
430
 
        *reqDataLen = sizeof(*req) + 2;
431
 
        req = malloc(*reqDataLen);
432
 
        if (req == NULL) {
433
 
                wpa_printf(MSG_ERROR, "EAP-TTLS: Failed to allocate memory for"
434
 
                           " request");
435
 
                eap_ttls_state(data, FAILURE);
436
 
                return NULL;
437
 
        }
438
 
 
439
 
        req->code = EAP_CODE_REQUEST;
440
 
        req->identifier = id;
441
 
        req->length = host_to_be16(*reqDataLen);
442
 
        pos = (u8 *) (req + 1);
443
 
        *pos++ = EAP_TYPE_TTLS;
444
 
        *pos = EAP_TLS_FLAGS_START | data->ttls_version;
445
 
 
446
 
        eap_ttls_state(data, PHASE1);
447
 
 
448
 
        return (u8 *) req;
449
 
}
450
 
 
451
 
 
452
 
static u8 * eap_ttls_build_req(struct eap_sm *sm, struct eap_ttls_data *data,
453
 
                               int id, size_t *reqDataLen)
454
 
{
455
 
        int res;
456
 
        u8 *req;
457
 
 
458
 
        res = eap_server_tls_buildReq_helper(sm, &data->ssl, EAP_TYPE_TTLS,
459
 
                                             data->ttls_version, id, &req,
460
 
                                             reqDataLen);
461
 
 
462
 
        if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
463
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase1 done, starting "
464
 
                           "Phase2");
465
 
                eap_ttls_state(data, PHASE2_START);
466
 
        }
467
 
 
468
 
        if (res == 1)
469
 
                return eap_server_tls_build_ack(reqDataLen, id, EAP_TYPE_TTLS,
470
 
                                                data->ttls_version);
471
 
        return req;
472
 
}
473
 
 
474
 
 
475
 
static u8 * eap_ttls_encrypt(struct eap_sm *sm, struct eap_ttls_data *data,
476
 
                             int id, u8 *plain, size_t plain_len,
477
 
                             size_t *out_len)
478
 
{
479
 
        int res;
480
 
        u8 *pos;
481
 
        struct eap_hdr *req;
482
 
 
483
 
        /* TODO: add support for fragmentation, if needed. This will need to
484
 
         * add TLS Message Length field, if the frame is fragmented. */
485
 
        req = malloc(sizeof(struct eap_hdr) + 2 + data->ssl.tls_out_limit);
486
 
        if (req == NULL)
487
 
                return NULL;
488
 
 
489
 
        req->code = EAP_CODE_REQUEST;
490
 
        req->identifier = id;
491
 
 
492
 
        pos = (u8 *) (req + 1);
493
 
        *pos++ = EAP_TYPE_TTLS;
494
 
        *pos++ = data->ttls_version;
495
 
 
496
 
        res = tls_connection_encrypt(sm->ssl_ctx, data->ssl.conn,
497
 
                                     plain, plain_len,
498
 
                                     pos, data->ssl.tls_out_limit);
499
 
        if (res < 0) {
500
 
                wpa_printf(MSG_INFO, "EAP-TTLS: Failed to encrypt Phase 2 "
501
 
                           "data");
502
 
                free(req);
503
 
                return NULL;
504
 
        }
505
 
 
506
 
        *out_len = sizeof(struct eap_hdr) + 2 + res;
507
 
        req->length = host_to_be16(*out_len);
508
 
        return (u8 *) req;
509
 
}
510
 
 
511
 
 
512
 
static u8 * eap_ttls_build_phase2_eap_req(struct eap_sm *sm,
513
 
                                          struct eap_ttls_data *data,
514
 
                                          int id, size_t *reqDataLen)
515
 
{
516
 
        u8 *req, *encr_req;
517
 
        size_t req_len;
518
 
 
519
 
 
520
 
        req = data->phase2_method->buildReq(sm, data->phase2_priv, id,
521
 
                                            &req_len);
522
 
        if (req == NULL)
523
 
                return NULL;
524
 
 
525
 
        wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS/EAP: Encapsulate Phase 2 data",
526
 
                        req, req_len);
527
 
 
528
 
        if (eap_ttls_avp_encapsulate(&req, &req_len, RADIUS_ATTR_EAP_MESSAGE,
529
 
                                     1) < 0) {
530
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS/EAP: Failed to encapsulate "
531
 
                           "packet");
532
 
                return NULL;
533
 
        }
534
 
 
535
 
        wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS/EAP: Encrypt encapsulated Phase "
536
 
                        "2 data", req, req_len);
537
 
 
538
 
        encr_req = eap_ttls_encrypt(sm, data, id, req, req_len, reqDataLen);
539
 
        free(req);
540
 
 
541
 
        return encr_req;
542
 
}
543
 
 
544
 
 
545
 
static u8 * eap_ttls_build_phase2_mschapv2(struct eap_sm *sm,
546
 
                                           struct eap_ttls_data *data,
547
 
                                           int id, size_t *reqDataLen)
548
 
{
549
 
        u8 *req, *encr_req, *pos, *end;
550
 
        int ret;
551
 
        size_t req_len;
552
 
 
553
 
        pos = req = malloc(100);
554
 
        if (req == NULL)
555
 
                return NULL;
556
 
        end = req + 100;
557
 
 
558
 
        if (data->mschapv2_resp_ok) {
559
 
                pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP2_SUCCESS,
560
 
                                       RADIUS_VENDOR_ID_MICROSOFT, 1, 43);
561
 
                *pos++ = data->mschapv2_ident;
562
 
                ret = snprintf((char *) pos, end - pos, "S=");
563
 
                if (ret >= 0 && ret < end - pos)
564
 
                        pos += ret;
565
 
                pos += wpa_snprintf_hex_uppercase(
566
 
                        (char *) pos, end - pos, data->mschapv2_auth_response,
567
 
                        sizeof(data->mschapv2_auth_response));
568
 
        } else {
569
 
                pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP_ERROR,
570
 
                                       RADIUS_VENDOR_ID_MICROSOFT, 1, 6);
571
 
                memcpy(pos, "Failed", 6);
572
 
                pos += 6;
573
 
                AVP_PAD(req, pos);
574
 
        }
575
 
 
576
 
        req_len = pos - req;
577
 
        wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Encrypting Phase 2 "
578
 
                        "data", req, req_len);
579
 
 
580
 
        encr_req = eap_ttls_encrypt(sm, data, id, req, req_len, reqDataLen);
581
 
        free(req);
582
 
 
583
 
        return encr_req;
584
 
}
585
 
 
586
 
 
587
 
static u8 * eap_ttls_build_phase_finished(struct eap_sm *sm,
588
 
                                          struct eap_ttls_data *data,
589
 
                                          int id, int final,
590
 
                                          size_t *reqDataLen)
591
 
{
592
 
        int len;
593
 
        struct eap_hdr *req;
594
 
        u8 *pos;
595
 
        const int max_len = 300;
596
 
 
597
 
        len = sizeof(struct eap_hdr) + 2 + max_len;
598
 
        req = malloc(len);
599
 
        if (req == NULL)
600
 
                return NULL;
601
 
 
602
 
        req->code = EAP_CODE_REQUEST;
603
 
        req->identifier = id;
604
 
 
605
 
        pos = (u8 *) (req + 1);
606
 
        *pos++ = EAP_TYPE_TTLS;
607
 
        *pos++ = data->ttls_version;
608
 
 
609
 
        len = tls_connection_ia_send_phase_finished(sm->ssl_ctx,
610
 
                                                    data->ssl.conn,
611
 
                                                    final, pos, max_len);
612
 
        if (len < 0) {
613
 
                free(req);
614
 
                return NULL;
615
 
        }
616
 
 
617
 
        *reqDataLen = sizeof(struct eap_hdr) + 2 + len;
618
 
        req->length = host_to_be16(*reqDataLen);
619
 
 
620
 
        return (u8 *) req;
621
 
}
622
 
 
623
 
 
624
 
static u8 * eap_ttls_buildReq(struct eap_sm *sm, void *priv, int id,
625
 
                              size_t *reqDataLen)
626
 
{
627
 
        struct eap_ttls_data *data = priv;
628
 
 
629
 
        switch (data->state) {
630
 
        case START:
631
 
                return eap_ttls_build_start(sm, data, id, reqDataLen);
632
 
        case PHASE1:
633
 
                return eap_ttls_build_req(sm, data, id, reqDataLen);
634
 
        case PHASE2_METHOD:
635
 
                return eap_ttls_build_phase2_eap_req(sm, data, id, reqDataLen);
636
 
        case PHASE2_MSCHAPV2_RESP:
637
 
                return eap_ttls_build_phase2_mschapv2(sm, data, id,
638
 
                                                      reqDataLen);
639
 
        case PHASE_FINISHED:
640
 
                return eap_ttls_build_phase_finished(sm, data, id, 1,
641
 
                                                     reqDataLen);
642
 
        default:
643
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS: %s - unexpected state %d",
644
 
                           __func__, data->state);
645
 
                return NULL;
646
 
        }
647
 
}
648
 
 
649
 
 
650
 
static Boolean eap_ttls_check(struct eap_sm *sm, void *priv,
651
 
                              u8 *respData, size_t respDataLen)
652
 
{
653
 
        struct eap_hdr *resp;
654
 
        u8 *pos;
655
 
 
656
 
        resp = (struct eap_hdr *) respData;
657
 
        pos = (u8 *) (resp + 1);
658
 
        if (respDataLen < sizeof(*resp) + 2 || *pos != EAP_TYPE_TTLS ||
659
 
            (be_to_host16(resp->length)) > respDataLen) {
660
 
                wpa_printf(MSG_INFO, "EAP-TTLS: Invalid frame");
661
 
                return TRUE;
662
 
        }
663
 
 
664
 
        return FALSE;
665
 
}
666
 
 
667
 
 
668
 
static int eap_ttls_ia_permute_inner_secret(struct eap_sm *sm,
669
 
                                            struct eap_ttls_data *data,
670
 
                                            const u8 *key, size_t key_len)
671
 
{
672
 
        u8 *buf;
673
 
        size_t buf_len;
674
 
        int ret;
675
 
 
676
 
        if (key) {
677
 
                buf_len = 2 + key_len;
678
 
                buf = malloc(buf_len);
679
 
                if (buf == NULL)
680
 
                        return -1;
681
 
                WPA_PUT_BE16(buf, key_len);
682
 
                memcpy(buf + 2, key, key_len);
683
 
        } else {
684
 
                buf = NULL;
685
 
                buf_len = 0;
686
 
        }
687
 
 
688
 
        wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Session keys for TLS/IA inner "
689
 
                        "secret permutation", buf, buf_len);
690
 
        ret = tls_connection_ia_permute_inner_secret(sm->ssl_ctx,
691
 
                                                     data->ssl.conn,
692
 
                                                     buf, buf_len);
693
 
        free(buf);
694
 
 
695
 
        return ret;
696
 
}
697
 
 
698
 
 
699
 
static void eap_ttls_process_phase2_pap(struct eap_sm *sm,
700
 
                                        struct eap_ttls_data *data,
701
 
                                        const u8 *user_password,
702
 
                                        size_t user_password_len)
703
 
{
704
 
        /* TODO: add support for verifying that the user entry accepts
705
 
         * EAP-TTLS/PAP. */
706
 
        if (!sm->user || !sm->user->password || sm->user->password_hash) {
707
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP: No plaintext user "
708
 
                           "password configured");
709
 
                eap_ttls_state(data, FAILURE);
710
 
                return;
711
 
        }
712
 
 
713
 
        if (sm->user->password_len != user_password_len ||
714
 
            memcmp(sm->user->password, user_password, user_password_len) != 0)
715
 
        {
716
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP: Invalid user password");
717
 
                eap_ttls_state(data, FAILURE);
718
 
                return;
719
 
        }
720
 
 
721
 
        wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP: Correct user password");
722
 
        eap_ttls_state(data, data->ttls_version > 0 ? PHASE_FINISHED :
723
 
                       SUCCESS);
724
 
}
725
 
 
726
 
 
727
 
static void eap_ttls_process_phase2_chap(struct eap_sm *sm,
728
 
                                         struct eap_ttls_data *data,
729
 
                                         const u8 *challenge,
730
 
                                         size_t challenge_len,
731
 
                                         const u8 *password,
732
 
                                         size_t password_len)
733
 
{
734
 
        u8 *chal, hash[MD5_MAC_LEN];
735
 
        const u8 *addr[3];
736
 
        size_t len[3];
737
 
 
738
 
        if (challenge == NULL || password == NULL ||
739
 
            challenge_len != EAP_TTLS_CHAP_CHALLENGE_LEN ||
740
 
            password_len != 1 + EAP_TTLS_CHAP_PASSWORD_LEN) {
741
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP: Invalid CHAP attributes "
742
 
                           "(challenge len %lu password len %lu)",
743
 
                           (unsigned long) challenge_len,
744
 
                           (unsigned long) password_len);
745
 
                eap_ttls_state(data, FAILURE);
746
 
                return;
747
 
        }
748
 
 
749
 
        /* TODO: add support for verifying that the user entry accepts
750
 
         * EAP-TTLS/CHAP. */
751
 
        if (!sm->user || !sm->user->password || sm->user->password_hash) {
752
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP: No plaintext user "
753
 
                           "password configured");
754
 
                eap_ttls_state(data, FAILURE);
755
 
                return;
756
 
        }
757
 
 
758
 
        chal = eap_ttls_implicit_challenge(sm, data,
759
 
                                           EAP_TTLS_CHAP_CHALLENGE_LEN + 1);
760
 
        if (chal == NULL) {
761
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP: Failed to generate "
762
 
                           "challenge from TLS data");
763
 
                eap_ttls_state(data, FAILURE);
764
 
                return;
765
 
        }
766
 
 
767
 
        if (memcmp(challenge, chal, EAP_TTLS_CHAP_CHALLENGE_LEN) != 0 ||
768
 
            password[0] != chal[EAP_TTLS_CHAP_CHALLENGE_LEN]) {
769
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP: Challenge mismatch");
770
 
                free(chal);
771
 
                eap_ttls_state(data, FAILURE);
772
 
                return;
773
 
        }
774
 
        free(chal);
775
 
 
776
 
        /* MD5(Ident + Password + Challenge) */
777
 
        addr[0] = password;
778
 
        len[0] = 1;
779
 
        addr[1] = sm->user->password;
780
 
        len[1] = sm->user->password_len;
781
 
        addr[2] = challenge;
782
 
        len[2] = challenge_len;
783
 
        md5_vector(3, addr, len, hash);
784
 
 
785
 
        if (memcmp(hash, password + 1, EAP_TTLS_CHAP_PASSWORD_LEN) == 0) {
786
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP: Correct user password");
787
 
                eap_ttls_state(data, data->ttls_version > 0 ? PHASE_FINISHED :
788
 
                               SUCCESS);
789
 
        } else {
790
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP: Invalid user password");
791
 
                eap_ttls_state(data, FAILURE);
792
 
        }
793
 
}
794
 
 
795
 
 
796
 
static void eap_ttls_process_phase2_mschap(struct eap_sm *sm,
797
 
                                           struct eap_ttls_data *data,
798
 
                                           u8 *challenge, size_t challenge_len,
799
 
                                           u8 *response, size_t response_len)
800
 
{
801
 
        u8 *chal, nt_response[24];
802
 
 
803
 
        if (challenge == NULL || response == NULL ||
804
 
            challenge_len != EAP_TTLS_MSCHAP_CHALLENGE_LEN ||
805
 
            response_len != EAP_TTLS_MSCHAP_RESPONSE_LEN) {
806
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP: Invalid MS-CHAP "
807
 
                           "attributes (challenge len %lu response len %lu)",
808
 
                           (unsigned long) challenge_len,
809
 
                           (unsigned long) response_len);
810
 
                eap_ttls_state(data, FAILURE);
811
 
                return;
812
 
        }
813
 
 
814
 
        /* TODO: add support for verifying that the user entry accepts
815
 
         * EAP-TTLS/MSCHAP. */
816
 
        if (!sm->user || !sm->user->password) {
817
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP: No user password "
818
 
                           "configured");
819
 
                eap_ttls_state(data, FAILURE);
820
 
                return;
821
 
        }
822
 
 
823
 
        chal = eap_ttls_implicit_challenge(sm, data,
824
 
                                           EAP_TTLS_MSCHAP_CHALLENGE_LEN + 1);
825
 
        if (chal == NULL) {
826
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP: Failed to generate "
827
 
                           "challenge from TLS data");
828
 
                eap_ttls_state(data, FAILURE);
829
 
                return;
830
 
        }
831
 
 
832
 
        if (memcmp(challenge, chal, EAP_TTLS_MSCHAP_CHALLENGE_LEN) != 0 ||
833
 
            response[0] != chal[EAP_TTLS_MSCHAP_CHALLENGE_LEN]) {
834
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP: Challenge mismatch");
835
 
                free(chal);
836
 
                eap_ttls_state(data, FAILURE);
837
 
                return;
838
 
        }
839
 
        free(chal);
840
 
 
841
 
        if (sm->user->password_hash)
842
 
                challenge_response(challenge, sm->user->password, nt_response);
843
 
        else
844
 
                nt_challenge_response(challenge, sm->user->password,
845
 
                                      sm->user->password_len, nt_response);
846
 
 
847
 
        if (memcmp(nt_response, response + 2 + 24, 24) == 0) {
848
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP: Correct response");
849
 
                eap_ttls_state(data, data->ttls_version > 0 ? PHASE_FINISHED :
850
 
                               SUCCESS);
851
 
        } else {
852
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP: Invalid NT-Response");
853
 
                wpa_hexdump(MSG_MSGDUMP, "EAP-TTLS/MSCHAP: Received",
854
 
                            response + 2 + 24, 24);
855
 
                wpa_hexdump(MSG_MSGDUMP, "EAP-TTLS/MSCHAP: Expected",
856
 
                            nt_response, 24);
857
 
                eap_ttls_state(data, FAILURE);
858
 
        }
859
 
}
860
 
 
861
 
 
862
 
static void eap_ttls_process_phase2_mschapv2(struct eap_sm *sm,
863
 
                                             struct eap_ttls_data *data,
864
 
                                             u8 *challenge,
865
 
                                             size_t challenge_len,
866
 
                                             u8 *response, size_t response_len)
867
 
{
868
 
        u8 *chal, *username, nt_response[24], *rx_resp, *peer_challenge,
869
 
                *auth_challenge;
870
 
        size_t username_len, i;
871
 
 
872
 
        if (challenge == NULL || response == NULL ||
873
 
            challenge_len != EAP_TTLS_MSCHAPV2_CHALLENGE_LEN ||
874
 
            response_len != EAP_TTLS_MSCHAPV2_RESPONSE_LEN) {
875
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Invalid MS-CHAP2 "
876
 
                           "attributes (challenge len %lu response len %lu)",
877
 
                           (unsigned long) challenge_len,
878
 
                           (unsigned long) response_len);
879
 
                eap_ttls_state(data, FAILURE);
880
 
                return;
881
 
        }
882
 
 
883
 
        /* TODO: add support for verifying that the user entry accepts
884
 
         * EAP-TTLS/MSCHAPV2. */
885
 
        if (!sm->user || !sm->user->password) {
886
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: No user password "
887
 
                           "configured");
888
 
                eap_ttls_state(data, FAILURE);
889
 
                return;
890
 
        }
891
 
 
892
 
        /* MSCHAPv2 does not include optional domain name in the
893
 
         * challenge-response calculation, so remove domain prefix
894
 
         * (if present). */
895
 
        username = sm->identity;
896
 
        username_len = sm->identity_len;
897
 
        for (i = 0; i < username_len; i++) {
898
 
                if (username[i] == '\\') {
899
 
                        username_len -= i + 1;
900
 
                        username += i + 1;
901
 
                        break;
902
 
                }
903
 
        }
904
 
 
905
 
        chal = eap_ttls_implicit_challenge(
906
 
                sm, data, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN + 1);
907
 
        if (chal == NULL) {
908
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Failed to generate "
909
 
                           "challenge from TLS data");
910
 
                eap_ttls_state(data, FAILURE);
911
 
                return;
912
 
        }
913
 
 
914
 
        if (memcmp(challenge, chal, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN) != 0 ||
915
 
            response[0] != chal[EAP_TTLS_MSCHAPV2_CHALLENGE_LEN]) {
916
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Challenge mismatch");
917
 
                free(chal);
918
 
                eap_ttls_state(data, FAILURE);
919
 
                return;
920
 
        }
921
 
        free(chal);
922
 
 
923
 
        auth_challenge = challenge;
924
 
        peer_challenge = response + 2;
925
 
 
926
 
        wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-TTLS/MSCHAPV2: User",
927
 
                          username, username_len);
928
 
        wpa_hexdump(MSG_MSGDUMP, "EAP-TTLS/MSCHAPV2: auth_challenge",
929
 
                    auth_challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
930
 
        wpa_hexdump(MSG_MSGDUMP, "EAP-TTLS/MSCHAPV2: peer_challenge",
931
 
                    peer_challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
932
 
 
933
 
        if (sm->user->password_hash) {
934
 
                generate_nt_response_pwhash(auth_challenge, peer_challenge,
935
 
                                            username, username_len,
936
 
                                            sm->user->password,
937
 
                                            nt_response);
938
 
        } else {
939
 
                generate_nt_response(auth_challenge, peer_challenge,
940
 
                                     username, username_len,
941
 
                                     sm->user->password,
942
 
                                     sm->user->password_len,
943
 
                                     nt_response);
944
 
        }
945
 
 
946
 
        rx_resp = response + 2 + EAP_TTLS_MSCHAPV2_CHALLENGE_LEN + 8;
947
 
        if (memcmp(nt_response, rx_resp, 24) == 0) {
948
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Correct "
949
 
                           "NT-Response");
950
 
                data->mschapv2_resp_ok = 1;
951
 
                if (data->ttls_version > 0) {
952
 
                        const u8 *pw_hash;
953
 
                        u8 pw_hash_buf[16], pw_hash_hash[16], master_key[16];
954
 
                        u8 session_key[2 * MSCHAPV2_KEY_LEN];
955
 
 
956
 
                        if (sm->user->password_hash)
957
 
                                pw_hash = sm->user->password;
958
 
                        else {
959
 
                                nt_password_hash(sm->user->password,
960
 
                                                 sm->user->password_len,
961
 
                                                 pw_hash_buf);
962
 
                                pw_hash = pw_hash_buf;
963
 
                        }
964
 
                        hash_nt_password_hash(pw_hash, pw_hash_hash);
965
 
                        get_master_key(pw_hash_hash, nt_response, master_key);
966
 
                        get_asymetric_start_key(master_key, session_key,
967
 
                                                MSCHAPV2_KEY_LEN, 0, 0);
968
 
                        get_asymetric_start_key(master_key,
969
 
                                                session_key + MSCHAPV2_KEY_LEN,
970
 
                                                MSCHAPV2_KEY_LEN, 1, 0);
971
 
                        eap_ttls_ia_permute_inner_secret(sm, data,
972
 
                                                         session_key,
973
 
                                                         sizeof(session_key));
974
 
                }
975
 
 
976
 
                if (sm->user->password_hash) {
977
 
                        generate_authenticator_response_pwhash(
978
 
                                sm->user->password,
979
 
                                peer_challenge, auth_challenge,
980
 
                                username, username_len, nt_response,
981
 
                                data->mschapv2_auth_response);
982
 
                } else {
983
 
                        generate_authenticator_response(
984
 
                                sm->user->password, sm->user->password_len,
985
 
                                peer_challenge, auth_challenge,
986
 
                                username, username_len, nt_response,
987
 
                                data->mschapv2_auth_response);
988
 
                }
989
 
        } else {
990
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Invalid "
991
 
                           "NT-Response");
992
 
                wpa_hexdump(MSG_MSGDUMP, "EAP-TTLS/MSCHAPV2: Received",
993
 
                            rx_resp, 24);
994
 
                wpa_hexdump(MSG_MSGDUMP, "EAP-TTLS/MSCHAPV2: Expected",
995
 
                            nt_response, 24);
996
 
                data->mschapv2_resp_ok = 0;
997
 
        }
998
 
        eap_ttls_state(data, PHASE2_MSCHAPV2_RESP);
999
 
        data->mschapv2_ident = response[0];
1000
 
}
1001
 
 
1002
 
 
1003
 
static int eap_ttls_phase2_eap_init(struct eap_sm *sm,
1004
 
                                    struct eap_ttls_data *data,
1005
 
                                    EapType eap_type)
1006
 
{
1007
 
        if (data->phase2_priv && data->phase2_method) {
1008
 
                data->phase2_method->reset(sm, data->phase2_priv);
1009
 
                data->phase2_method = NULL;
1010
 
                data->phase2_priv = NULL;
1011
 
        }
1012
 
        data->phase2_method = eap_server_get_eap_method(EAP_VENDOR_IETF,
1013
 
                                                        eap_type);
1014
 
        if (!data->phase2_method)
1015
 
                return -1;
1016
 
 
1017
 
        sm->init_phase2 = 1;
1018
 
        data->phase2_priv = data->phase2_method->init(sm);
1019
 
        sm->init_phase2 = 0;
1020
 
        return 0;
1021
 
}
1022
 
 
1023
 
 
1024
 
static void eap_ttls_process_phase2_eap_response(struct eap_sm *sm,
1025
 
                                                 struct eap_ttls_data *data,
1026
 
                                                 u8 *in_data, size_t in_len)
1027
 
{
1028
 
        u8 next_type = EAP_TYPE_NONE;
1029
 
        struct eap_hdr *hdr;
1030
 
        u8 *pos;
1031
 
        size_t left;
1032
 
        const struct eap_method *m = data->phase2_method;
1033
 
        void *priv = data->phase2_priv;
1034
 
 
1035
 
        if (data->phase2_priv == NULL) {
1036
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS/EAP: %s - Phase2 not "
1037
 
                           "initialized?!", __func__);
1038
 
                return;
1039
 
        }
1040
 
 
1041
 
        hdr = (struct eap_hdr *) in_data;
1042
 
        pos = (u8 *) (hdr + 1);
1043
 
 
1044
 
        if (in_len > sizeof(*hdr) && *pos == EAP_TYPE_NAK) {
1045
 
                left = in_len - sizeof(*hdr);
1046
 
                wpa_hexdump(MSG_DEBUG, "EAP-TTLS/EAP: Phase2 type Nak'ed; "
1047
 
                            "allowed types", pos + 1, left - 1);
1048
 
                eap_sm_process_nak(sm, pos + 1, left - 1);
1049
 
                if (sm->user && sm->user_eap_method_index < EAP_MAX_METHODS &&
1050
 
                    sm->user->methods[sm->user_eap_method_index].method !=
1051
 
                    EAP_TYPE_NONE) {
1052
 
                        next_type = sm->user->methods[
1053
 
                                sm->user_eap_method_index++].method;
1054
 
                        wpa_printf(MSG_DEBUG, "EAP-TTLS: try EAP type %d",
1055
 
                                   next_type);
1056
 
                        eap_ttls_phase2_eap_init(sm, data, next_type);
1057
 
                } else {
1058
 
                        eap_ttls_state(data, FAILURE);
1059
 
                }
1060
 
                return;
1061
 
        }
1062
 
 
1063
 
        if (m->check(sm, priv, in_data, in_len)) {
1064
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS/EAP: Phase2 check() asked to "
1065
 
                           "ignore the packet");
1066
 
                return;
1067
 
        }
1068
 
 
1069
 
        m->process(sm, priv, in_data, in_len);
1070
 
 
1071
 
        if (!m->isDone(sm, priv))
1072
 
                return;
1073
 
 
1074
 
        if (!m->isSuccess(sm, priv)) {
1075
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS/EAP: Phase2 method failed");
1076
 
                eap_ttls_state(data, FAILURE);
1077
 
                return;
1078
 
        }
1079
 
 
1080
 
        switch (data->state) {
1081
 
        case PHASE2_START:
1082
 
                if (eap_user_get(sm, sm->identity, sm->identity_len, 1) != 0) {
1083
 
                        wpa_hexdump_ascii(MSG_DEBUG, "EAP_TTLS: Phase2 "
1084
 
                                          "Identity not found in the user "
1085
 
                                          "database",
1086
 
                                          sm->identity, sm->identity_len);
1087
 
                        eap_ttls_state(data, FAILURE);
1088
 
                        break;
1089
 
                }
1090
 
 
1091
 
                eap_ttls_state(data, PHASE2_METHOD);
1092
 
                next_type = sm->user->methods[0].method;
1093
 
                sm->user_eap_method_index = 1;
1094
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS: try EAP type %d", next_type);
1095
 
                break;
1096
 
        case PHASE2_METHOD:
1097
 
                if (data->ttls_version > 0) {
1098
 
                        if (m->getKey) {
1099
 
                                u8 *key;
1100
 
                                size_t key_len;
1101
 
                                key = m->getKey(sm, priv, &key_len);
1102
 
                                eap_ttls_ia_permute_inner_secret(sm, data,
1103
 
                                                                 key, key_len);
1104
 
                        }
1105
 
                        eap_ttls_state(data, PHASE_FINISHED);
1106
 
                } else
1107
 
                        eap_ttls_state(data, SUCCESS);
1108
 
                break;
1109
 
        case FAILURE:
1110
 
                break;
1111
 
        default:
1112
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS: %s - unexpected state %d",
1113
 
                           __func__, data->state);
1114
 
                break;
1115
 
        }
1116
 
 
1117
 
        eap_ttls_phase2_eap_init(sm, data, next_type);
1118
 
}
1119
 
 
1120
 
 
1121
 
static void eap_ttls_process_phase2_eap(struct eap_sm *sm,
1122
 
                                        struct eap_ttls_data *data,
1123
 
                                        const u8 *eap, size_t eap_len)
1124
 
{
1125
 
        struct eap_hdr *hdr;
1126
 
        size_t len;
1127
 
 
1128
 
        if (data->state == PHASE2_START) {
1129
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS/EAP: initializing Phase 2");
1130
 
                if (eap_ttls_phase2_eap_init(sm, data, EAP_TYPE_IDENTITY) < 0)
1131
 
                {
1132
 
                        wpa_printf(MSG_DEBUG, "EAP-TTLS/EAP: failed to "
1133
 
                                   "initialize EAP-Identity");
1134
 
                        return;
1135
 
                }
1136
 
        }
1137
 
 
1138
 
        if (eap_len < sizeof(*hdr)) {
1139
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS/EAP: too short Phase 2 EAP "
1140
 
                           "packet (len=%lu)", (unsigned long) eap_len);
1141
 
                return;
1142
 
        }
1143
 
 
1144
 
        hdr = (struct eap_hdr *) eap;
1145
 
        len = be_to_host16(hdr->length);
1146
 
        wpa_printf(MSG_DEBUG, "EAP-TTLS/EAP: received Phase 2 EAP: code=%d "
1147
 
                   "identifier=%d length=%lu", hdr->code, hdr->identifier,
1148
 
                   (unsigned long) len);
1149
 
        if (len > eap_len) {
1150
 
                wpa_printf(MSG_INFO, "EAP-TTLS/EAP: Length mismatch in Phase 2"
1151
 
                           " EAP frame (hdr len=%lu, data len in AVP=%lu)",
1152
 
                           (unsigned long) len, (unsigned long) eap_len);
1153
 
                return;
1154
 
        }
1155
 
 
1156
 
        switch (hdr->code) {
1157
 
        case EAP_CODE_RESPONSE:
1158
 
                eap_ttls_process_phase2_eap_response(sm, data, (u8 *) hdr,
1159
 
                                                     len);
1160
 
                break;
1161
 
        default:
1162
 
                wpa_printf(MSG_INFO, "EAP-TTLS/EAP: Unexpected code=%d in "
1163
 
                           "Phase 2 EAP header", hdr->code);
1164
 
                break;
1165
 
        }
1166
 
}
1167
 
 
1168
 
 
1169
 
static void eap_ttls_process_phase2(struct eap_sm *sm,
1170
 
                                    struct eap_ttls_data *data,
1171
 
                                    struct eap_hdr *resp,
1172
 
                                    u8 *in_data, size_t in_len)
1173
 
{
1174
 
        u8 *in_decrypted;
1175
 
        int len_decrypted, res;
1176
 
        struct eap_ttls_avp parse;
1177
 
        size_t buf_len;
1178
 
 
1179
 
        wpa_printf(MSG_DEBUG, "EAP-TTLS: received %lu bytes encrypted data for"
1180
 
                   " Phase 2", (unsigned long) in_len);
1181
 
 
1182
 
        res = eap_server_tls_data_reassemble(sm, &data->ssl, &in_data,
1183
 
                                             &in_len);
1184
 
        if (res < 0 || res == 1)
1185
 
                return;
1186
 
 
1187
 
        buf_len = in_len;
1188
 
        if (data->ssl.tls_in_total > buf_len)
1189
 
                buf_len = data->ssl.tls_in_total;
1190
 
        in_decrypted = malloc(buf_len);
1191
 
        if (in_decrypted == NULL) {
1192
 
                free(data->ssl.tls_in);
1193
 
                data->ssl.tls_in = NULL;
1194
 
                data->ssl.tls_in_len = 0;
1195
 
                wpa_printf(MSG_WARNING, "EAP-TTLS: failed to allocate memory "
1196
 
                           "for decryption");
1197
 
                return;
1198
 
        }
1199
 
 
1200
 
        len_decrypted = tls_connection_decrypt(sm->ssl_ctx, data->ssl.conn,
1201
 
                                               in_data, in_len,
1202
 
                                               in_decrypted, buf_len);
1203
 
        free(data->ssl.tls_in);
1204
 
        data->ssl.tls_in = NULL;
1205
 
        data->ssl.tls_in_len = 0;
1206
 
        if (len_decrypted < 0) {
1207
 
                wpa_printf(MSG_INFO, "EAP-TTLS: Failed to decrypt Phase 2 "
1208
 
                           "data");
1209
 
                free(in_decrypted);
1210
 
                eap_ttls_state(data, FAILURE);
1211
 
                return;
1212
 
        }
1213
 
 
1214
 
        if (data->state == PHASE_FINISHED) {
1215
 
                if (len_decrypted == 0 &&
1216
 
                    tls_connection_ia_final_phase_finished(sm->ssl_ctx,
1217
 
                                                           data->ssl.conn)) {
1218
 
                        wpa_printf(MSG_DEBUG, "EAP-TTLS: FinalPhaseFinished "
1219
 
                                   "received");
1220
 
                        eap_ttls_state(data, SUCCESS);
1221
 
                } else {
1222
 
                        wpa_printf(MSG_INFO, "EAP-TTLS: Did not receive valid "
1223
 
                                   "FinalPhaseFinished");
1224
 
                        eap_ttls_state(data, FAILURE);
1225
 
                }
1226
 
 
1227
 
                free(in_decrypted);
1228
 
                return;
1229
 
        }
1230
 
 
1231
 
        wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Decrypted Phase 2 EAP",
1232
 
                        in_decrypted, len_decrypted);
1233
 
 
1234
 
        if (eap_ttls_avp_parse(in_decrypted, len_decrypted, &parse) < 0) {
1235
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to parse AVPs");
1236
 
                free(in_decrypted);
1237
 
                eap_ttls_state(data, FAILURE);
1238
 
                return;
1239
 
        }
1240
 
 
1241
 
        if (parse.user_name) {
1242
 
                free(sm->identity);
1243
 
                sm->identity = malloc(parse.user_name_len);
1244
 
                if (sm->identity) {
1245
 
                        memcpy(sm->identity, parse.user_name,
1246
 
                               parse.user_name_len);
1247
 
                        sm->identity_len = parse.user_name_len;
1248
 
                }
1249
 
                if (eap_user_get(sm, parse.user_name, parse.user_name_len, 1)
1250
 
                    != 0) {
1251
 
                        wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase2 Identity not "
1252
 
                                   "found in the user database");
1253
 
                        eap_ttls_state(data, FAILURE);
1254
 
                        goto done;
1255
 
                }
1256
 
        }
1257
 
 
1258
 
        if (parse.eap) {
1259
 
                eap_ttls_process_phase2_eap(sm, data, parse.eap,
1260
 
                                            parse.eap_len);
1261
 
        } else if (parse.user_password) {
1262
 
                eap_ttls_process_phase2_pap(sm, data, parse.user_password,
1263
 
                                            parse.user_password_len);
1264
 
        } else if (parse.chap_password) {
1265
 
                eap_ttls_process_phase2_chap(sm, data,
1266
 
                                             parse.chap_challenge,
1267
 
                                             parse.chap_challenge_len,
1268
 
                                             parse.chap_password,
1269
 
                                             parse.chap_password_len);
1270
 
        } else if (parse.mschap_response) {
1271
 
                eap_ttls_process_phase2_mschap(sm, data,
1272
 
                                               parse.mschap_challenge,
1273
 
                                               parse.mschap_challenge_len,
1274
 
                                               parse.mschap_response,
1275
 
                                               parse.mschap_response_len);
1276
 
        } else if (parse.mschap2_response) {
1277
 
                eap_ttls_process_phase2_mschapv2(sm, data,
1278
 
                                                 parse.mschap_challenge,
1279
 
                                                 parse.mschap_challenge_len,
1280
 
                                                 parse.mschap2_response,
1281
 
                                                 parse.mschap2_response_len);
1282
 
        }
1283
 
 
1284
 
done:
1285
 
        free(in_decrypted);
1286
 
        free(parse.eap);
1287
 
 }
1288
 
 
1289
 
 
1290
 
static void eap_ttls_process(struct eap_sm *sm, void *priv,
1291
 
                             u8 *respData, size_t respDataLen)
1292
 
{
1293
 
        struct eap_ttls_data *data = priv;
1294
 
        struct eap_hdr *resp;
1295
 
        u8 *pos, flags;
1296
 
        int left;
1297
 
        unsigned int tls_msg_len;
1298
 
        int peer_version;
1299
 
 
1300
 
        resp = (struct eap_hdr *) respData;
1301
 
        pos = (u8 *) (resp + 1);
1302
 
        pos++;
1303
 
        flags = *pos++;
1304
 
        left = be_to_host16(resp->length) - sizeof(struct eap_hdr) - 2;
1305
 
        wpa_printf(MSG_DEBUG, "EAP-TTLS: Received packet(len=%lu) - "
1306
 
                   "Flags 0x%02x", (unsigned long) respDataLen, flags);
1307
 
        peer_version = flags & EAP_PEAP_VERSION_MASK;
1308
 
        if (peer_version < data->ttls_version) {
1309
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS: peer ver=%d, own ver=%d; "
1310
 
                           "use version %d",
1311
 
                           peer_version, data->ttls_version, peer_version);
1312
 
                data->ttls_version = peer_version;
1313
 
        }
1314
 
 
1315
 
        if (data->ttls_version > 0 && !data->tls_ia_configured) {
1316
 
                if (tls_connection_set_ia(sm->ssl_ctx, data->ssl.conn, 1)) {
1317
 
                        wpa_printf(MSG_INFO, "EAP-TTLS: Failed to enable "
1318
 
                                   "TLS/IA");
1319
 
                        eap_ttls_state(data, FAILURE);
1320
 
                        return;
1321
 
                }
1322
 
                data->tls_ia_configured = 1;
1323
 
        }
1324
 
 
1325
 
        if (flags & EAP_TLS_FLAGS_LENGTH_INCLUDED) {
1326
 
                if (left < 4) {
1327
 
                        wpa_printf(MSG_INFO, "EAP-TTLS: Short frame with TLS "
1328
 
                                   "length");
1329
 
                        eap_ttls_state(data, FAILURE);
1330
 
                        return;
1331
 
                }
1332
 
                tls_msg_len = WPA_GET_BE32(pos);
1333
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS: TLS Message Length: %d",
1334
 
                           tls_msg_len);
1335
 
                if (data->ssl.tls_in_left == 0) {
1336
 
                        data->ssl.tls_in_total = tls_msg_len;
1337
 
                        data->ssl.tls_in_left = tls_msg_len;
1338
 
                        free(data->ssl.tls_in);
1339
 
                        data->ssl.tls_in = NULL;
1340
 
                        data->ssl.tls_in_len = 0;
1341
 
                }
1342
 
                pos += 4;
1343
 
                left -= 4;
1344
 
        }
1345
 
 
1346
 
        switch (data->state) {
1347
 
        case PHASE1:
1348
 
                if (eap_server_tls_process_helper(sm, &data->ssl, pos, left) <
1349
 
                    0) {
1350
 
                        wpa_printf(MSG_INFO, "EAP-TTLS: TLS processing "
1351
 
                                   "failed");
1352
 
                        eap_ttls_state(data, FAILURE);
1353
 
                }
1354
 
                break;
1355
 
        case PHASE2_START:
1356
 
        case PHASE2_METHOD:
1357
 
        case PHASE_FINISHED:
1358
 
                eap_ttls_process_phase2(sm, data, resp, pos, left);
1359
 
                break;
1360
 
        case PHASE2_MSCHAPV2_RESP:
1361
 
                if (data->mschapv2_resp_ok && left == 0) {
1362
 
                        wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Peer "
1363
 
                                   "acknowledged response");
1364
 
                        eap_ttls_state(data, data->ttls_version > 0 ?
1365
 
                                       PHASE_FINISHED : SUCCESS);
1366
 
                } else if (!data->mschapv2_resp_ok) {
1367
 
                        wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Peer "
1368
 
                                   "acknowledged error");
1369
 
                        eap_ttls_state(data, FAILURE);
1370
 
                } else {
1371
 
                        wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Unexpected "
1372
 
                                   "frame from peer (payload len %d, expected "
1373
 
                                   "empty frame)", left);
1374
 
                        eap_ttls_state(data, FAILURE);
1375
 
                }
1376
 
                break;
1377
 
        default:
1378
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS: Unexpected state %d in %s",
1379
 
                           data->state, __func__);
1380
 
                break;
1381
 
        }
1382
 
 
1383
 
        if (tls_connection_get_write_alerts(sm->ssl_ctx, data->ssl.conn) > 1) {
1384
 
                wpa_printf(MSG_INFO, "EAP-TTLS: Locally detected fatal error "
1385
 
                           "in TLS processing");
1386
 
                eap_ttls_state(data, FAILURE);
1387
 
        }
1388
 
}
1389
 
 
1390
 
 
1391
 
static Boolean eap_ttls_isDone(struct eap_sm *sm, void *priv)
1392
 
{
1393
 
        struct eap_ttls_data *data = priv;
1394
 
        return data->state == SUCCESS || data->state == FAILURE;
1395
 
}
1396
 
 
1397
 
 
1398
 
static u8 * eap_ttls_v1_derive_key(struct eap_sm *sm,
1399
 
                                   struct eap_ttls_data *data)
1400
 
{
1401
 
        struct tls_keys keys;
1402
 
        u8 *rnd, *key;
1403
 
 
1404
 
        memset(&keys, 0, sizeof(keys));
1405
 
        if (tls_connection_get_keys(sm->ssl_ctx, data->ssl.conn, &keys) ||
1406
 
            keys.client_random == NULL || keys.server_random == NULL ||
1407
 
            keys.inner_secret == NULL) {
1408
 
                wpa_printf(MSG_INFO, "EAP-TTLS: Could not get inner secret, "
1409
 
                           "client random, or server random to derive keying "
1410
 
                           "material");
1411
 
                return NULL;
1412
 
        }
1413
 
 
1414
 
        rnd = malloc(keys.client_random_len + keys.server_random_len);
1415
 
        key = malloc(EAP_TLS_KEY_LEN);
1416
 
        if (rnd == NULL || key == NULL) {
1417
 
                wpa_printf(MSG_INFO, "EAP-TTLS: No memory for key derivation");
1418
 
                free(rnd);
1419
 
                free(key);
1420
 
                return NULL;
1421
 
        }
1422
 
        memcpy(rnd, keys.client_random, keys.client_random_len);
1423
 
        memcpy(rnd + keys.client_random_len, keys.server_random,
1424
 
               keys.server_random_len);
1425
 
 
1426
 
        if (tls_prf(keys.inner_secret, keys.inner_secret_len,
1427
 
                    "ttls v1 keying material", rnd, keys.client_random_len +
1428
 
                    keys.server_random_len, key, EAP_TLS_KEY_LEN)) {
1429
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to derive key");
1430
 
                free(rnd);
1431
 
                free(key);
1432
 
                return NULL;
1433
 
        }
1434
 
 
1435
 
        wpa_hexdump(MSG_DEBUG, "EAP-TTLS: client/server random",
1436
 
                    rnd, keys.client_random_len + keys.server_random_len);
1437
 
        wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: TLS/IA inner secret",
1438
 
                        keys.inner_secret, keys.inner_secret_len);
1439
 
 
1440
 
        free(rnd);
1441
 
 
1442
 
        return key;
1443
 
}
1444
 
 
1445
 
 
1446
 
static u8 * eap_ttls_getKey(struct eap_sm *sm, void *priv, size_t *len)
1447
 
{
1448
 
        struct eap_ttls_data *data = priv;
1449
 
        u8 *eapKeyData;
1450
 
 
1451
 
        if (data->state != SUCCESS)
1452
 
                return NULL;
1453
 
 
1454
 
        if (data->ttls_version == 0) {
1455
 
                eapKeyData = eap_server_tls_derive_key(sm, &data->ssl,
1456
 
                                                       "ttls keying material",
1457
 
                                                       EAP_TLS_KEY_LEN);
1458
 
        } else {
1459
 
                eapKeyData = eap_ttls_v1_derive_key(sm, data);
1460
 
        }
1461
 
 
1462
 
        if (eapKeyData) {
1463
 
                *len = EAP_TLS_KEY_LEN;
1464
 
                wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived key",
1465
 
                                eapKeyData, EAP_TLS_KEY_LEN);
1466
 
        } else {
1467
 
                wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to derive key");
1468
 
        }
1469
 
 
1470
 
        return eapKeyData;
1471
 
}
1472
 
 
1473
 
 
1474
 
static Boolean eap_ttls_isSuccess(struct eap_sm *sm, void *priv)
1475
 
{
1476
 
        struct eap_ttls_data *data = priv;
1477
 
        return data->state == SUCCESS;
1478
 
}
1479
 
 
1480
 
 
1481
 
int eap_server_ttls_register(void)
1482
 
{
1483
 
        struct eap_method *eap;
1484
 
        int ret;
1485
 
 
1486
 
        eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
1487
 
                                      EAP_VENDOR_IETF, EAP_TYPE_TTLS, "TTLS");
1488
 
        if (eap == NULL)
1489
 
                return -1;
1490
 
 
1491
 
        eap->init = eap_ttls_init;
1492
 
        eap->reset = eap_ttls_reset;
1493
 
        eap->buildReq = eap_ttls_buildReq;
1494
 
        eap->check = eap_ttls_check;
1495
 
        eap->process = eap_ttls_process;
1496
 
        eap->isDone = eap_ttls_isDone;
1497
 
        eap->getKey = eap_ttls_getKey;
1498
 
        eap->isSuccess = eap_ttls_isSuccess;
1499
 
 
1500
 
        ret = eap_server_method_register(eap);
1501
 
        if (ret)
1502
 
                eap_server_method_free(eap);
1503
 
        return ret;
1504
 
}