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