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