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