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