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