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

« back to all changes in this revision

Viewing changes to wpa_supplicant/eapol_test.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
 
 * WPA Supplicant - test code
3
 
 * Copyright (c) 2003-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
 
 * IEEE 802.1X Supplicant test code (to be used in place of wpa_supplicant.c.
15
 
 * Not used in production version.
16
 
 */
17
 
 
18
 
#include "includes.h"
19
 
#include <assert.h>
20
 
 
21
 
#include "common.h"
22
 
#include "config.h"
23
 
#include "eapol_sm.h"
24
 
#include "eap_peer/eap.h"
25
 
#include "eloop.h"
26
 
#include "wpa.h"
27
 
#include "eap_peer/eap_i.h"
28
 
#include "wpa_supplicant_i.h"
29
 
#include "radius/radius.h"
30
 
#include "radius/radius_client.h"
31
 
#include "ctrl_iface.h"
32
 
#include "pcsc_funcs.h"
33
 
 
34
 
 
35
 
extern int wpa_debug_level;
36
 
extern int wpa_debug_show_keys;
37
 
 
38
 
struct wpa_driver_ops *wpa_supplicant_drivers[] = { NULL };
39
 
 
40
 
 
41
 
struct eapol_test_data {
42
 
        struct wpa_supplicant *wpa_s;
43
 
 
44
 
        int eapol_test_num_reauths;
45
 
        int no_mppe_keys;
46
 
        int num_mppe_ok, num_mppe_mismatch;
47
 
 
48
 
        u8 radius_identifier;
49
 
        struct radius_msg *last_recv_radius;
50
 
        struct in_addr own_ip_addr;
51
 
        struct radius_client_data *radius;
52
 
        struct hostapd_radius_servers *radius_conf;
53
 
 
54
 
        u8 *last_eap_radius; /* last received EAP Response from Authentication
55
 
                              * Server */
56
 
        size_t last_eap_radius_len;
57
 
 
58
 
        u8 authenticator_pmk[PMK_LEN];
59
 
        size_t authenticator_pmk_len;
60
 
        int radius_access_accept_received;
61
 
        int radius_access_reject_received;
62
 
        int auth_timed_out;
63
 
 
64
 
        u8 *eap_identity;
65
 
        size_t eap_identity_len;
66
 
 
67
 
        char *connect_info;
68
 
        u8 own_addr[ETH_ALEN];
69
 
};
70
 
 
71
 
static struct eapol_test_data eapol_test;
72
 
 
73
 
 
74
 
static void send_eap_request_identity(void *eloop_ctx, void *timeout_ctx);
75
 
 
76
 
 
77
 
static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
78
 
                              int level, const char *txt, size_t len)
79
 
{
80
 
        if (addr)
81
 
                wpa_printf(MSG_DEBUG, "STA " MACSTR ": %s\n",
82
 
                           MAC2STR(addr), txt);
83
 
        else
84
 
                wpa_printf(MSG_DEBUG, "%s", txt);
85
 
}
86
 
 
87
 
 
88
 
static void ieee802_1x_encapsulate_radius(struct eapol_test_data *e,
89
 
                                          const u8 *eap, size_t len)
90
 
{
91
 
        struct radius_msg *msg;
92
 
        char buf[128];
93
 
        const struct eap_hdr *hdr;
94
 
        const u8 *pos;
95
 
 
96
 
        wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS "
97
 
                   "packet");
98
 
 
99
 
        e->radius_identifier = radius_client_get_id(e->radius);
100
 
        msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
101
 
                             e->radius_identifier);
102
 
        if (msg == NULL) {
103
 
                printf("Could not create net RADIUS packet\n");
104
 
                return;
105
 
        }
106
 
 
107
 
        radius_msg_make_authenticator(msg, (u8 *) e, sizeof(*e));
108
 
 
109
 
        hdr = (const struct eap_hdr *) eap;
110
 
        pos = (const u8 *) (hdr + 1);
111
 
        if (len > sizeof(*hdr) && hdr->code == EAP_CODE_RESPONSE &&
112
 
            pos[0] == EAP_TYPE_IDENTITY) {
113
 
                pos++;
114
 
                os_free(e->eap_identity);
115
 
                e->eap_identity_len = len - sizeof(*hdr) - 1;
116
 
                e->eap_identity = os_malloc(e->eap_identity_len);
117
 
                if (e->eap_identity) {
118
 
                        os_memcpy(e->eap_identity, pos, e->eap_identity_len);
119
 
                        wpa_hexdump(MSG_DEBUG, "Learned identity from "
120
 
                                    "EAP-Response-Identity",
121
 
                                    e->eap_identity, e->eap_identity_len);
122
 
                }
123
 
        }
124
 
 
125
 
        if (e->eap_identity &&
126
 
            !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
127
 
                                 e->eap_identity, e->eap_identity_len)) {
128
 
                printf("Could not add User-Name\n");
129
 
                goto fail;
130
 
        }
131
 
 
132
 
        if (!radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
133
 
                                 (u8 *) &e->own_ip_addr, 4)) {
134
 
                printf("Could not add NAS-IP-Address\n");
135
 
                goto fail;
136
 
        }
137
 
 
138
 
        os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
139
 
                    MAC2STR(e->wpa_s->own_addr));
140
 
        if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
141
 
                                 (u8 *) buf, os_strlen(buf))) {
142
 
                printf("Could not add Calling-Station-Id\n");
143
 
                goto fail;
144
 
        }
145
 
 
146
 
        /* TODO: should probably check MTU from driver config; 2304 is max for
147
 
         * IEEE 802.11, but use 1400 to avoid problems with too large packets
148
 
         */
149
 
        if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
150
 
                printf("Could not add Framed-MTU\n");
151
 
                goto fail;
152
 
        }
153
 
 
154
 
        if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
155
 
                                       RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
156
 
                printf("Could not add NAS-Port-Type\n");
157
 
                goto fail;
158
 
        }
159
 
 
160
 
        os_snprintf(buf, sizeof(buf), "%s", e->connect_info);
161
 
        if (!radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
162
 
                                 (u8 *) buf, os_strlen(buf))) {
163
 
                printf("Could not add Connect-Info\n");
164
 
                goto fail;
165
 
        }
166
 
 
167
 
        if (eap && !radius_msg_add_eap(msg, eap, len)) {
168
 
                printf("Could not add EAP-Message\n");
169
 
                goto fail;
170
 
        }
171
 
 
172
 
        /* State attribute must be copied if and only if this packet is
173
 
         * Access-Request reply to the previous Access-Challenge */
174
 
        if (e->last_recv_radius && e->last_recv_radius->hdr->code ==
175
 
            RADIUS_CODE_ACCESS_CHALLENGE) {
176
 
                int res = radius_msg_copy_attr(msg, e->last_recv_radius,
177
 
                                               RADIUS_ATTR_STATE);
178
 
                if (res < 0) {
179
 
                        printf("Could not copy State attribute from previous "
180
 
                               "Access-Challenge\n");
181
 
                        goto fail;
182
 
                }
183
 
                if (res > 0) {
184
 
                        wpa_printf(MSG_DEBUG, "  Copied RADIUS State "
185
 
                                   "Attribute");
186
 
                }
187
 
        }
188
 
 
189
 
        radius_client_send(e->radius, msg, RADIUS_AUTH, e->wpa_s->own_addr);
190
 
        return;
191
 
 
192
 
 fail:
193
 
        radius_msg_free(msg);
194
 
        os_free(msg);
195
 
}
196
 
 
197
 
 
198
 
static int eapol_test_eapol_send(void *ctx, int type, const u8 *buf,
199
 
                                 size_t len)
200
 
{
201
 
        /* struct wpa_supplicant *wpa_s = ctx; */
202
 
        printf("WPA: eapol_test_eapol_send(type=%d len=%d)\n", type, len);
203
 
        if (type == IEEE802_1X_TYPE_EAP_PACKET) {
204
 
                wpa_hexdump(MSG_DEBUG, "TX EAP -> RADIUS", buf, len);
205
 
                ieee802_1x_encapsulate_radius(&eapol_test, buf, len);
206
 
        }
207
 
        return 0;
208
 
}
209
 
 
210
 
 
211
 
static void eapol_test_set_config_blob(void *ctx,
212
 
                                       struct wpa_config_blob *blob)
213
 
{
214
 
        struct wpa_supplicant *wpa_s = ctx;
215
 
        wpa_config_set_blob(wpa_s->conf, blob);
216
 
}
217
 
 
218
 
 
219
 
static const struct wpa_config_blob *
220
 
eapol_test_get_config_blob(void *ctx, const char *name)
221
 
{
222
 
        struct wpa_supplicant *wpa_s = ctx;
223
 
        return wpa_config_get_blob(wpa_s->conf, name);
224
 
}
225
 
 
226
 
 
227
 
static void eapol_test_eapol_done_cb(void *ctx)
228
 
{
229
 
        printf("WPA: EAPOL processing complete\n");
230
 
}
231
 
 
232
 
 
233
 
static void eapol_sm_reauth(void *eloop_ctx, void *timeout_ctx)
234
 
{
235
 
        struct eapol_test_data *e = eloop_ctx;
236
 
        printf("\n\n\n\n\neapol_test: Triggering EAP reauthentication\n\n");
237
 
        e->radius_access_accept_received = 0;
238
 
        send_eap_request_identity(e->wpa_s, NULL);
239
 
}
240
 
 
241
 
 
242
 
static int eapol_test_compare_pmk(struct eapol_test_data *e)
243
 
{
244
 
        u8 pmk[PMK_LEN];
245
 
        int ret = 1;
246
 
 
247
 
        if (eapol_sm_get_key(e->wpa_s->eapol, pmk, PMK_LEN) == 0) {
248
 
                wpa_hexdump(MSG_DEBUG, "PMK from EAPOL", pmk, PMK_LEN);
249
 
                if (os_memcmp(pmk, e->authenticator_pmk, PMK_LEN) != 0) {
250
 
                        printf("WARNING: PMK mismatch\n");
251
 
                        wpa_hexdump(MSG_DEBUG, "PMK from AS",
252
 
                                    e->authenticator_pmk, PMK_LEN);
253
 
                } else if (e->radius_access_accept_received)
254
 
                        ret = 0;
255
 
        } else if (e->authenticator_pmk_len == 16 &&
256
 
                   eapol_sm_get_key(e->wpa_s->eapol, pmk, 16) == 0) {
257
 
                wpa_hexdump(MSG_DEBUG, "LEAP PMK from EAPOL", pmk, 16);
258
 
                if (os_memcmp(pmk, e->authenticator_pmk, 16) != 0) {
259
 
                        printf("WARNING: PMK mismatch\n");
260
 
                        wpa_hexdump(MSG_DEBUG, "PMK from AS",
261
 
                                    e->authenticator_pmk, 16);
262
 
                } else if (e->radius_access_accept_received)
263
 
                        ret = 0;
264
 
        } else if (e->radius_access_accept_received && e->no_mppe_keys) {
265
 
                /* No keying material expected */
266
 
                ret = 0;
267
 
        }
268
 
 
269
 
        if (ret && !e->no_mppe_keys)
270
 
                e->num_mppe_mismatch++;
271
 
        else if (!e->no_mppe_keys)
272
 
                e->num_mppe_ok++;
273
 
 
274
 
        return ret;
275
 
}
276
 
 
277
 
 
278
 
static void eapol_sm_cb(struct eapol_sm *eapol, int success, void *ctx)
279
 
{
280
 
        struct eapol_test_data *e = ctx;
281
 
        printf("eapol_sm_cb: success=%d\n", success);
282
 
        e->eapol_test_num_reauths--;
283
 
        if (e->eapol_test_num_reauths < 0)
284
 
                eloop_terminate();
285
 
        else {
286
 
                eapol_test_compare_pmk(e);
287
 
                eloop_register_timeout(0, 100000, eapol_sm_reauth, e, NULL);
288
 
        }
289
 
}
290
 
 
291
 
 
292
 
static int test_eapol(struct eapol_test_data *e, struct wpa_supplicant *wpa_s,
293
 
                      struct wpa_ssid *ssid)
294
 
{
295
 
        struct eapol_config eapol_conf;
296
 
        struct eapol_ctx *ctx;
297
 
 
298
 
        ctx = os_zalloc(sizeof(*ctx));
299
 
        if (ctx == NULL) {
300
 
                printf("Failed to allocate EAPOL context.\n");
301
 
                return -1;
302
 
        }
303
 
        ctx->ctx = wpa_s;
304
 
        ctx->msg_ctx = wpa_s;
305
 
        ctx->scard_ctx = wpa_s->scard;
306
 
        ctx->cb = eapol_sm_cb;
307
 
        ctx->cb_ctx = e;
308
 
        ctx->eapol_send_ctx = wpa_s;
309
 
        ctx->preauth = 0;
310
 
        ctx->eapol_done_cb = eapol_test_eapol_done_cb;
311
 
        ctx->eapol_send = eapol_test_eapol_send;
312
 
        ctx->set_config_blob = eapol_test_set_config_blob;
313
 
        ctx->get_config_blob = eapol_test_get_config_blob;
314
 
        ctx->opensc_engine_path = wpa_s->conf->opensc_engine_path;
315
 
        ctx->pkcs11_engine_path = wpa_s->conf->pkcs11_engine_path;
316
 
        ctx->pkcs11_module_path = wpa_s->conf->pkcs11_module_path;
317
 
 
318
 
        wpa_s->eapol = eapol_sm_init(ctx);
319
 
        if (wpa_s->eapol == NULL) {
320
 
                os_free(ctx);
321
 
                printf("Failed to initialize EAPOL state machines.\n");
322
 
                return -1;
323
 
        }
324
 
 
325
 
        wpa_s->current_ssid = ssid;
326
 
        os_memset(&eapol_conf, 0, sizeof(eapol_conf));
327
 
        eapol_conf.accept_802_1x_keys = 1;
328
 
        eapol_conf.required_keys = 0;
329
 
        eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
330
 
        eapol_conf.workaround = ssid->eap_workaround;
331
 
        eapol_sm_notify_config(wpa_s->eapol, ssid, &eapol_conf);
332
 
        eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
333
 
 
334
 
 
335
 
        eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
336
 
        /* 802.1X::portControl = Auto */
337
 
        eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
338
 
 
339
 
        return 0;
340
 
}
341
 
 
342
 
 
343
 
static void test_eapol_clean(struct eapol_test_data *e,
344
 
                             struct wpa_supplicant *wpa_s)
345
 
{
346
 
        radius_client_deinit(e->radius);
347
 
        os_free(e->last_eap_radius);
348
 
        if (e->last_recv_radius) {
349
 
                radius_msg_free(e->last_recv_radius);
350
 
                os_free(e->last_recv_radius);
351
 
        }
352
 
        os_free(e->eap_identity);
353
 
        e->eap_identity = NULL;
354
 
        eapol_sm_deinit(wpa_s->eapol);
355
 
        wpa_s->eapol = NULL;
356
 
        if (e->radius_conf && e->radius_conf->auth_server) {
357
 
                os_free(e->radius_conf->auth_server->shared_secret);
358
 
                os_free(e->radius_conf->auth_server);
359
 
        }
360
 
        os_free(e->radius_conf);
361
 
        e->radius_conf = NULL;
362
 
        scard_deinit(wpa_s->scard);
363
 
        if (wpa_s->ctrl_iface) {
364
 
                wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
365
 
                wpa_s->ctrl_iface = NULL;
366
 
        }
367
 
        wpa_config_free(wpa_s->conf);
368
 
}
369
 
 
370
 
 
371
 
static void send_eap_request_identity(void *eloop_ctx, void *timeout_ctx)
372
 
{
373
 
        struct wpa_supplicant *wpa_s = eloop_ctx;
374
 
        u8 buf[100], *pos;
375
 
        struct ieee802_1x_hdr *hdr;
376
 
        struct eap_hdr *eap;
377
 
 
378
 
        hdr = (struct ieee802_1x_hdr *) buf;
379
 
        hdr->version = EAPOL_VERSION;
380
 
        hdr->type = IEEE802_1X_TYPE_EAP_PACKET;
381
 
        hdr->length = htons(5);
382
 
 
383
 
        eap = (struct eap_hdr *) (hdr + 1);
384
 
        eap->code = EAP_CODE_REQUEST;
385
 
        eap->identifier = 0;
386
 
        eap->length = htons(5);
387
 
        pos = (u8 *) (eap + 1);
388
 
        *pos = EAP_TYPE_IDENTITY;
389
 
 
390
 
        printf("Sending fake EAP-Request-Identity\n");
391
 
        eapol_sm_rx_eapol(wpa_s->eapol, wpa_s->bssid, buf,
392
 
                          sizeof(*hdr) + 5);
393
 
}
394
 
 
395
 
 
396
 
static void eapol_test_timeout(void *eloop_ctx, void *timeout_ctx)
397
 
{
398
 
        struct eapol_test_data *e = eloop_ctx;
399
 
        printf("EAPOL test timed out\n");
400
 
        e->auth_timed_out = 1;
401
 
        eloop_terminate();
402
 
}
403
 
 
404
 
 
405
 
static char *eap_type_text(u8 type)
406
 
{
407
 
        switch (type) {
408
 
        case EAP_TYPE_IDENTITY: return "Identity";
409
 
        case EAP_TYPE_NOTIFICATION: return "Notification";
410
 
        case EAP_TYPE_NAK: return "Nak";
411
 
        case EAP_TYPE_TLS: return "TLS";
412
 
        case EAP_TYPE_TTLS: return "TTLS";
413
 
        case EAP_TYPE_PEAP: return "PEAP";
414
 
        case EAP_TYPE_SIM: return "SIM";
415
 
        case EAP_TYPE_GTC: return "GTC";
416
 
        case EAP_TYPE_MD5: return "MD5";
417
 
        case EAP_TYPE_OTP: return "OTP";
418
 
        case EAP_TYPE_FAST: return "FAST";
419
 
        case EAP_TYPE_SAKE: return "SAKE";
420
 
        case EAP_TYPE_PSK: return "PSK";
421
 
        default: return "Unknown";
422
 
        }
423
 
}
424
 
 
425
 
 
426
 
static void ieee802_1x_decapsulate_radius(struct eapol_test_data *e)
427
 
{
428
 
        u8 *eap;
429
 
        size_t len;
430
 
        struct eap_hdr *hdr;
431
 
        int eap_type = -1;
432
 
        char buf[64];
433
 
        struct radius_msg *msg;
434
 
 
435
 
        if (e->last_recv_radius == NULL)
436
 
                return;
437
 
 
438
 
        msg = e->last_recv_radius;
439
 
 
440
 
        eap = radius_msg_get_eap(msg, &len);
441
 
        if (eap == NULL) {
442
 
                /* draft-aboba-radius-rfc2869bis-20.txt, Chap. 2.6.3:
443
 
                 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
444
 
                 * attribute */
445
 
                wpa_printf(MSG_DEBUG, "could not extract "
446
 
                               "EAP-Message from RADIUS message");
447
 
                os_free(e->last_eap_radius);
448
 
                e->last_eap_radius = NULL;
449
 
                e->last_eap_radius_len = 0;
450
 
                return;
451
 
        }
452
 
 
453
 
        if (len < sizeof(*hdr)) {
454
 
                wpa_printf(MSG_DEBUG, "too short EAP packet "
455
 
                               "received from authentication server");
456
 
                os_free(eap);
457
 
                return;
458
 
        }
459
 
 
460
 
        if (len > sizeof(*hdr))
461
 
                eap_type = eap[sizeof(*hdr)];
462
 
 
463
 
        hdr = (struct eap_hdr *) eap;
464
 
        switch (hdr->code) {
465
 
        case EAP_CODE_REQUEST:
466
 
                os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
467
 
                            eap_type >= 0 ? eap_type_text(eap_type) : "??",
468
 
                            eap_type);
469
 
                break;
470
 
        case EAP_CODE_RESPONSE:
471
 
                os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
472
 
                            eap_type >= 0 ? eap_type_text(eap_type) : "??",
473
 
                            eap_type);
474
 
                break;
475
 
        case EAP_CODE_SUCCESS:
476
 
                os_strlcpy(buf, "EAP Success", sizeof(buf));
477
 
                /* LEAP uses EAP Success within an authentication, so must not
478
 
                 * stop here with eloop_terminate(); */
479
 
                break;
480
 
        case EAP_CODE_FAILURE:
481
 
                os_strlcpy(buf, "EAP Failure", sizeof(buf));
482
 
                eloop_terminate();
483
 
                break;
484
 
        default:
485
 
                os_strlcpy(buf, "unknown EAP code", sizeof(buf));
486
 
                wpa_hexdump(MSG_DEBUG, "Decapsulated EAP packet", eap, len);
487
 
                break;
488
 
        }
489
 
        wpa_printf(MSG_DEBUG, "decapsulated EAP packet (code=%d "
490
 
                       "id=%d len=%d) from RADIUS server: %s",
491
 
                      hdr->code, hdr->identifier, ntohs(hdr->length), buf);
492
 
 
493
 
        /* sta->eapol_sm->be_auth.idFromServer = hdr->identifier; */
494
 
 
495
 
        os_free(e->last_eap_radius);
496
 
        e->last_eap_radius = eap;
497
 
        e->last_eap_radius_len = len;
498
 
 
499
 
        {
500
 
                struct ieee802_1x_hdr *dot1x;
501
 
                dot1x = os_malloc(sizeof(*dot1x) + len);
502
 
                assert(dot1x != NULL);
503
 
                dot1x->version = EAPOL_VERSION;
504
 
                dot1x->type = IEEE802_1X_TYPE_EAP_PACKET;
505
 
                dot1x->length = htons(len);
506
 
                os_memcpy((u8 *) (dot1x + 1), eap, len);
507
 
                eapol_sm_rx_eapol(e->wpa_s->eapol, e->wpa_s->bssid,
508
 
                                  (u8 *) dot1x, sizeof(*dot1x) + len);
509
 
                os_free(dot1x);
510
 
        }
511
 
}
512
 
 
513
 
 
514
 
static void ieee802_1x_get_keys(struct eapol_test_data *e,
515
 
                                struct radius_msg *msg, struct radius_msg *req,
516
 
                                u8 *shared_secret, size_t shared_secret_len)
517
 
{
518
 
        struct radius_ms_mppe_keys *keys;
519
 
 
520
 
        keys = radius_msg_get_ms_keys(msg, req, shared_secret,
521
 
                                      shared_secret_len);
522
 
        if (keys && keys->send == NULL && keys->recv == NULL) {
523
 
                os_free(keys);
524
 
                keys = radius_msg_get_cisco_keys(msg, req, shared_secret,
525
 
                                                 shared_secret_len);
526
 
        }
527
 
 
528
 
        if (keys) {
529
 
                if (keys->send) {
530
 
                        wpa_hexdump(MSG_DEBUG, "MS-MPPE-Send-Key (sign)",
531
 
                                    keys->send, keys->send_len);
532
 
                }
533
 
                if (keys->recv) {
534
 
                        wpa_hexdump(MSG_DEBUG, "MS-MPPE-Recv-Key (crypt)",
535
 
                                    keys->recv, keys->recv_len);
536
 
                        e->authenticator_pmk_len =
537
 
                                keys->recv_len > PMK_LEN ? PMK_LEN :
538
 
                                keys->recv_len;
539
 
                        os_memcpy(e->authenticator_pmk, keys->recv,
540
 
                                  e->authenticator_pmk_len);
541
 
                }
542
 
 
543
 
                os_free(keys->send);
544
 
                os_free(keys->recv);
545
 
                os_free(keys);
546
 
        }
547
 
}
548
 
 
549
 
 
550
 
/* Process the RADIUS frames from Authentication Server */
551
 
static RadiusRxResult
552
 
ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
553
 
                        u8 *shared_secret, size_t shared_secret_len,
554
 
                        void *data)
555
 
{
556
 
        struct eapol_test_data *e = data;
557
 
 
558
 
        /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
559
 
         * present when packet contains an EAP-Message attribute */
560
 
        if (msg->hdr->code == RADIUS_CODE_ACCESS_REJECT &&
561
 
            radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
562
 
                                0) < 0 &&
563
 
            radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
564
 
                wpa_printf(MSG_DEBUG, "Allowing RADIUS "
565
 
                              "Access-Reject without Message-Authenticator "
566
 
                              "since it does not include EAP-Message\n");
567
 
        } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
568
 
                                     req, 1)) {
569
 
                printf("Incoming RADIUS packet did not have correct "
570
 
                       "Message-Authenticator - dropped\n");
571
 
                return RADIUS_RX_UNKNOWN;
572
 
        }
573
 
 
574
 
        if (msg->hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
575
 
            msg->hdr->code != RADIUS_CODE_ACCESS_REJECT &&
576
 
            msg->hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
577
 
                printf("Unknown RADIUS message code\n");
578
 
                return RADIUS_RX_UNKNOWN;
579
 
        }
580
 
 
581
 
        e->radius_identifier = -1;
582
 
        wpa_printf(MSG_DEBUG, "RADIUS packet matching with station");
583
 
 
584
 
        if (e->last_recv_radius) {
585
 
                radius_msg_free(e->last_recv_radius);
586
 
                os_free(e->last_recv_radius);
587
 
        }
588
 
 
589
 
        e->last_recv_radius = msg;
590
 
 
591
 
        switch (msg->hdr->code) {
592
 
        case RADIUS_CODE_ACCESS_ACCEPT:
593
 
                e->radius_access_accept_received = 1;
594
 
                ieee802_1x_get_keys(e, msg, req, shared_secret,
595
 
                                    shared_secret_len);
596
 
                break;
597
 
        case RADIUS_CODE_ACCESS_REJECT:
598
 
                e->radius_access_reject_received = 1;
599
 
                break;
600
 
        }
601
 
 
602
 
        ieee802_1x_decapsulate_radius(e);
603
 
 
604
 
        if ((msg->hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
605
 
             e->eapol_test_num_reauths < 0) ||
606
 
            msg->hdr->code == RADIUS_CODE_ACCESS_REJECT) {
607
 
                eloop_terminate();
608
 
        }
609
 
 
610
 
        return RADIUS_RX_QUEUED;
611
 
}
612
 
 
613
 
 
614
 
static void wpa_init_conf(struct eapol_test_data *e,
615
 
                          struct wpa_supplicant *wpa_s, const char *authsrv,
616
 
                          int port, const char *secret)
617
 
{
618
 
        struct hostapd_radius_server *as;
619
 
        int res;
620
 
 
621
 
        wpa_s->bssid[5] = 1;
622
 
        os_memcpy(wpa_s->own_addr, e->own_addr, ETH_ALEN);
623
 
        e->own_ip_addr.s_addr = htonl((127 << 24) | 1);
624
 
        os_strlcpy(wpa_s->ifname, "test", sizeof(wpa_s->ifname));
625
 
 
626
 
        e->radius_conf = os_zalloc(sizeof(struct hostapd_radius_servers));
627
 
        assert(e->radius_conf != NULL);
628
 
        e->radius_conf->num_auth_servers = 1;
629
 
        as = os_zalloc(sizeof(struct hostapd_radius_server));
630
 
        assert(as != NULL);
631
 
#if defined(CONFIG_NATIVE_WINDOWS) || defined(CONFIG_ANSI_C_EXTRA)
632
 
        {
633
 
                int a[4];
634
 
                u8 *pos;
635
 
                sscanf(authsrv, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
636
 
                pos = (u8 *) &as->addr.u.v4;
637
 
                *pos++ = a[0];
638
 
                *pos++ = a[1];
639
 
                *pos++ = a[2];
640
 
                *pos++ = a[3];
641
 
        }
642
 
#else /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
643
 
        inet_aton(authsrv, &as->addr.u.v4);
644
 
#endif /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
645
 
        as->addr.af = AF_INET;
646
 
        as->port = port;
647
 
        as->shared_secret = (u8 *) os_strdup(secret);
648
 
        as->shared_secret_len = os_strlen(secret);
649
 
        e->radius_conf->auth_server = as;
650
 
        e->radius_conf->auth_servers = as;
651
 
        e->radius_conf->msg_dumps = 1;
652
 
 
653
 
        e->radius = radius_client_init(wpa_s, e->radius_conf);
654
 
        assert(e->radius != NULL);
655
 
 
656
 
        res = radius_client_register(e->radius, RADIUS_AUTH,
657
 
                                     ieee802_1x_receive_auth, e);
658
 
        assert(res == 0);
659
 
}
660
 
 
661
 
 
662
 
static int scard_test(void)
663
 
{
664
 
        struct scard_data *scard;
665
 
        size_t len;
666
 
        char imsi[20];
667
 
        unsigned char _rand[16];
668
 
#ifdef PCSC_FUNCS
669
 
        unsigned char sres[4];
670
 
        unsigned char kc[8];
671
 
#endif /* PCSC_FUNCS */
672
 
#define num_triplets 5
673
 
        unsigned char rand_[num_triplets][16];
674
 
        unsigned char sres_[num_triplets][4];
675
 
        unsigned char kc_[num_triplets][8];
676
 
        int i, res;
677
 
        size_t j;
678
 
 
679
 
#define AKA_RAND_LEN 16
680
 
#define AKA_AUTN_LEN 16
681
 
#define AKA_AUTS_LEN 14
682
 
#define RES_MAX_LEN 16
683
 
#define IK_LEN 16
684
 
#define CK_LEN 16
685
 
        unsigned char aka_rand[AKA_RAND_LEN];
686
 
        unsigned char aka_autn[AKA_AUTN_LEN];
687
 
        unsigned char aka_auts[AKA_AUTS_LEN];
688
 
        unsigned char aka_res[RES_MAX_LEN];
689
 
        size_t aka_res_len;
690
 
        unsigned char aka_ik[IK_LEN];
691
 
        unsigned char aka_ck[CK_LEN];
692
 
 
693
 
        scard = scard_init(SCARD_TRY_BOTH);
694
 
        if (scard == NULL)
695
 
                return -1;
696
 
        if (scard_set_pin(scard, "1234")) {
697
 
                wpa_printf(MSG_WARNING, "PIN validation failed");
698
 
                scard_deinit(scard);
699
 
                return -1;
700
 
        }
701
 
 
702
 
        len = sizeof(imsi);
703
 
        if (scard_get_imsi(scard, imsi, &len))
704
 
                goto failed;
705
 
        wpa_hexdump_ascii(MSG_DEBUG, "SCARD: IMSI", (u8 *) imsi, len);
706
 
        /* NOTE: Permanent Username: 1 | IMSI */
707
 
 
708
 
        os_memset(_rand, 0, sizeof(_rand));
709
 
        if (scard_gsm_auth(scard, _rand, sres, kc))
710
 
                goto failed;
711
 
 
712
 
        os_memset(_rand, 0xff, sizeof(_rand));
713
 
        if (scard_gsm_auth(scard, _rand, sres, kc))
714
 
                goto failed;
715
 
 
716
 
        for (i = 0; i < num_triplets; i++) {
717
 
                os_memset(rand_[i], i, sizeof(rand_[i]));
718
 
                if (scard_gsm_auth(scard, rand_[i], sres_[i], kc_[i]))
719
 
                        goto failed;
720
 
        }
721
 
 
722
 
        for (i = 0; i < num_triplets; i++) {
723
 
                printf("1");
724
 
                for (j = 0; j < len; j++)
725
 
                        printf("%c", imsi[j]);
726
 
                printf(",");
727
 
                for (j = 0; j < 16; j++)
728
 
                        printf("%02X", rand_[i][j]);
729
 
                printf(",");
730
 
                for (j = 0; j < 4; j++)
731
 
                        printf("%02X", sres_[i][j]);
732
 
                printf(",");
733
 
                for (j = 0; j < 8; j++)
734
 
                        printf("%02X", kc_[i][j]);
735
 
                printf("\n");
736
 
        }
737
 
 
738
 
        wpa_printf(MSG_DEBUG, "Trying to use UMTS authentication");
739
 
 
740
 
        /* seq 39 (0x28) */
741
 
        os_memset(aka_rand, 0xaa, 16);
742
 
        os_memcpy(aka_autn, "\x86\x71\x31\xcb\xa2\xfc\x61\xdf"
743
 
                  "\xa3\xb3\x97\x9d\x07\x32\xa2\x12", 16);
744
 
 
745
 
        res = scard_umts_auth(scard, aka_rand, aka_autn, aka_res, &aka_res_len,
746
 
                              aka_ik, aka_ck, aka_auts);
747
 
        if (res == 0) {
748
 
                wpa_printf(MSG_DEBUG, "UMTS auth completed successfully");
749
 
                wpa_hexdump(MSG_DEBUG, "RES", aka_res, aka_res_len);
750
 
                wpa_hexdump(MSG_DEBUG, "IK", aka_ik, IK_LEN);
751
 
                wpa_hexdump(MSG_DEBUG, "CK", aka_ck, CK_LEN);
752
 
        } else if (res == -2) {
753
 
                wpa_printf(MSG_DEBUG, "UMTS auth resulted in synchronization "
754
 
                           "failure");
755
 
                wpa_hexdump(MSG_DEBUG, "AUTS", aka_auts, AKA_AUTS_LEN);
756
 
        } else {
757
 
                wpa_printf(MSG_DEBUG, "UMTS auth failed");
758
 
        }
759
 
 
760
 
failed:
761
 
        scard_deinit(scard);
762
 
 
763
 
        return 0;
764
 
#undef num_triplets
765
 
}
766
 
 
767
 
 
768
 
static int scard_get_triplets(int argc, char *argv[])
769
 
{
770
 
        struct scard_data *scard;
771
 
        size_t len;
772
 
        char imsi[20];
773
 
        unsigned char _rand[16];
774
 
        unsigned char sres[4];
775
 
        unsigned char kc[8];
776
 
        int num_triplets;
777
 
        int i;
778
 
        size_t j;
779
 
 
780
 
        if (argc < 2 || ((num_triplets = atoi(argv[1])) <= 0)) {
781
 
                printf("invalid parameters for sim command\n");
782
 
                return -1;
783
 
        }
784
 
 
785
 
        if (argc <= 2 || os_strcmp(argv[2], "debug") != 0) {
786
 
                /* disable debug output */
787
 
                wpa_debug_level = 99;
788
 
        }
789
 
 
790
 
        scard = scard_init(SCARD_GSM_SIM_ONLY);
791
 
        if (scard == NULL) {
792
 
                printf("Failed to open smartcard connection\n");
793
 
                return -1;
794
 
        }
795
 
        if (scard_set_pin(scard, argv[0])) {
796
 
                wpa_printf(MSG_WARNING, "PIN validation failed");
797
 
                scard_deinit(scard);
798
 
                return -1;
799
 
        }
800
 
 
801
 
        len = sizeof(imsi);
802
 
        if (scard_get_imsi(scard, imsi, &len)) {
803
 
                scard_deinit(scard);
804
 
                return -1;
805
 
        }
806
 
 
807
 
        for (i = 0; i < num_triplets; i++) {
808
 
                os_memset(_rand, i, sizeof(_rand));
809
 
                if (scard_gsm_auth(scard, _rand, sres, kc))
810
 
                        break;
811
 
 
812
 
                /* IMSI:Kc:SRES:RAND */
813
 
                for (j = 0; j < len; j++)
814
 
                        printf("%c", imsi[j]);
815
 
                printf(":");
816
 
                for (j = 0; j < 8; j++)
817
 
                        printf("%02X", kc[j]);
818
 
                printf(":");
819
 
                for (j = 0; j < 4; j++)
820
 
                        printf("%02X", sres[j]);
821
 
                printf(":");
822
 
                for (j = 0; j < 16; j++)
823
 
                        printf("%02X", _rand[j]);
824
 
                printf("\n");
825
 
        }
826
 
 
827
 
        scard_deinit(scard);
828
 
 
829
 
        return 0;
830
 
}
831
 
 
832
 
 
833
 
static void eapol_test_terminate(int sig, void *eloop_ctx,
834
 
                                 void *signal_ctx)
835
 
{
836
 
        struct wpa_supplicant *wpa_s = eloop_ctx;
837
 
        wpa_msg(wpa_s, MSG_INFO, "Signal %d received - terminating", sig);
838
 
        eloop_terminate();
839
 
}
840
 
 
841
 
 
842
 
static void usage(void)
843
 
{
844
 
        printf("usage:\n"
845
 
               "eapol_test [-nWS] -c<conf> [-a<AS IP>] [-p<AS port>] "
846
 
               "[-s<AS secret>] \\\n"
847
 
               "           [-r<count>] [-t<timeout>] [-C<Connect-Info>] \\\n"
848
 
               "           [-M<client MAC address>]\n"
849
 
               "eapol_test scard\n"
850
 
               "eapol_test sim <PIN> <num triplets> [debug]\n"
851
 
               "\n");
852
 
        printf("options:\n"
853
 
               "  -c<conf> = configuration file\n"
854
 
               "  -a<AS IP> = IP address of the authentication server, "
855
 
               "default 127.0.0.1\n"
856
 
               "  -p<AS port> = UDP port of the authentication server, "
857
 
               "default 1812\n"
858
 
               "  -s<AS secret> = shared secret with the authentication "
859
 
               "server, default 'radius'\n"
860
 
               "  -r<count> = number of re-authentications\n"
861
 
               "  -W = wait for a control interface monitor before starting\n"
862
 
               "  -S = save configuration after authentiation\n"
863
 
               "  -n = no MPPE keys expected\n"
864
 
               "  -t<timeout> = sets timeout in seconds (default: 30 s)\n"
865
 
               "  -C<Connect-Info> = RADIUS Connect-Info (default: "
866
 
               "CONNECT 11Mbps 802.11b)\n"
867
 
               "  -M<client MAC address> = Set own MAC address "
868
 
               "(Calling-Station-Id,\n"
869
 
               "                           default: 02:00:00:00:00:01)\n");
870
 
}
871
 
 
872
 
 
873
 
int main(int argc, char *argv[])
874
 
{
875
 
        struct wpa_supplicant wpa_s;
876
 
        int c, ret = 1, wait_for_monitor = 0, save_config = 0;
877
 
        char *as_addr = "127.0.0.1";
878
 
        int as_port = 1812;
879
 
        char *as_secret = "radius";
880
 
        char *conf = NULL;
881
 
        int timeout = 30;
882
 
 
883
 
        if (os_program_init())
884
 
                return -1;
885
 
 
886
 
        hostapd_logger_register_cb(hostapd_logger_cb);
887
 
 
888
 
        os_memset(&eapol_test, 0, sizeof(eapol_test));
889
 
        eapol_test.connect_info = "CONNECT 11Mbps 802.11b";
890
 
        os_memcpy(eapol_test.own_addr, "\x02\x00\x00\x00\x00\x01", ETH_ALEN);
891
 
 
892
 
        wpa_debug_level = 0;
893
 
        wpa_debug_show_keys = 1;
894
 
 
895
 
        for (;;) {
896
 
                c = getopt(argc, argv, "a:c:C:M:np:r:s:St:W");
897
 
                if (c < 0)
898
 
                        break;
899
 
                switch (c) {
900
 
                case 'a':
901
 
                        as_addr = optarg;
902
 
                        break;
903
 
                case 'c':
904
 
                        conf = optarg;
905
 
                        break;
906
 
                case 'C':
907
 
                        eapol_test.connect_info = optarg;
908
 
                        break;
909
 
                case 'M':
910
 
                        if (hwaddr_aton(optarg, eapol_test.own_addr)) {
911
 
                                usage();
912
 
                                return -1;
913
 
                        }
914
 
                        break;
915
 
                case 'n':
916
 
                        eapol_test.no_mppe_keys++;
917
 
                        break;
918
 
                case 'p':
919
 
                        as_port = atoi(optarg);
920
 
                        break;
921
 
                case 'r':
922
 
                        eapol_test.eapol_test_num_reauths = atoi(optarg);
923
 
                        break;
924
 
                case 's':
925
 
                        as_secret = optarg;
926
 
                        break;
927
 
                case 'S':
928
 
                        save_config++;
929
 
                        break;
930
 
                case 't':
931
 
                        timeout = atoi(optarg);
932
 
                        break;
933
 
                case 'W':
934
 
                        wait_for_monitor++;
935
 
                        break;
936
 
                default:
937
 
                        usage();
938
 
                        return -1;
939
 
                }
940
 
        }
941
 
 
942
 
        if (argc > optind && os_strcmp(argv[optind], "scard") == 0) {
943
 
                return scard_test();
944
 
        }
945
 
 
946
 
        if (argc > optind && os_strcmp(argv[optind], "sim") == 0) {
947
 
                return scard_get_triplets(argc - optind - 1,
948
 
                                          &argv[optind + 1]);
949
 
        }
950
 
 
951
 
        if (conf == NULL) {
952
 
                usage();
953
 
                printf("Configuration file is required.\n");
954
 
                return -1;
955
 
        }
956
 
 
957
 
        if (eap_peer_register_methods()) {
958
 
                wpa_printf(MSG_ERROR, "Failed to register EAP methods");
959
 
                return -1;
960
 
        }
961
 
 
962
 
        if (eloop_init(&wpa_s)) {
963
 
                wpa_printf(MSG_ERROR, "Failed to initialize event loop");
964
 
                return -1;
965
 
        }
966
 
 
967
 
        os_memset(&wpa_s, 0, sizeof(wpa_s));
968
 
        eapol_test.wpa_s = &wpa_s;
969
 
        wpa_s.conf = wpa_config_read(conf);
970
 
        if (wpa_s.conf == NULL) {
971
 
                printf("Failed to parse configuration file '%s'.\n", conf);
972
 
                return -1;
973
 
        }
974
 
        if (wpa_s.conf->ssid == NULL) {
975
 
                printf("No networks defined.\n");
976
 
                return -1;
977
 
        }
978
 
 
979
 
        wpa_init_conf(&eapol_test, &wpa_s, as_addr, as_port, as_secret);
980
 
        wpa_s.ctrl_iface = wpa_supplicant_ctrl_iface_init(&wpa_s);
981
 
        if (wpa_s.ctrl_iface == NULL) {
982
 
                printf("Failed to initialize control interface '%s'.\n"
983
 
                       "You may have another eapol_test process already "
984
 
                       "running or the file was\n"
985
 
                       "left by an unclean termination of eapol_test in "
986
 
                       "which case you will need\n"
987
 
                       "to manually remove this file before starting "
988
 
                       "eapol_test again.\n",
989
 
                       wpa_s.conf->ctrl_interface);
990
 
                return -1;
991
 
        }
992
 
        if (wpa_supplicant_scard_init(&wpa_s, wpa_s.conf->ssid))
993
 
                return -1;
994
 
 
995
 
        if (test_eapol(&eapol_test, &wpa_s, wpa_s.conf->ssid))
996
 
                return -1;
997
 
 
998
 
        if (wait_for_monitor)
999
 
                wpa_supplicant_ctrl_iface_wait(wpa_s.ctrl_iface);
1000
 
 
1001
 
        eloop_register_timeout(timeout, 0, eapol_test_timeout, &eapol_test,
1002
 
                               NULL);
1003
 
        eloop_register_timeout(0, 0, send_eap_request_identity, &wpa_s, NULL);
1004
 
        eloop_register_signal_terminate(eapol_test_terminate, NULL);
1005
 
        eloop_register_signal_reconfig(eapol_test_terminate, NULL);
1006
 
        eloop_run();
1007
 
 
1008
 
        if (eapol_test_compare_pmk(&eapol_test) == 0 ||
1009
 
            eapol_test.no_mppe_keys)
1010
 
                ret = 0;
1011
 
        if (eapol_test.auth_timed_out)
1012
 
                ret = -2;
1013
 
        if (eapol_test.radius_access_reject_received)
1014
 
                ret = -3;
1015
 
 
1016
 
        if (save_config)
1017
 
                wpa_config_write(conf, wpa_s.conf);
1018
 
 
1019
 
        test_eapol_clean(&eapol_test, &wpa_s);
1020
 
 
1021
 
        eap_peer_unregister_methods();
1022
 
 
1023
 
        eloop_destroy();
1024
 
 
1025
 
        printf("MPPE keys OK: %d  mismatch: %d\n",
1026
 
               eapol_test.num_mppe_ok, eapol_test.num_mppe_mismatch);
1027
 
        if (eapol_test.num_mppe_mismatch)
1028
 
                ret = -4;
1029
 
        if (ret)
1030
 
                printf("FAILURE\n");
1031
 
        else
1032
 
                printf("SUCCESS\n");
1033
 
 
1034
 
        os_program_deinit();
1035
 
 
1036
 
        return ret;
1037
 
}