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