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