2 * WPA Supplicant - test code
3 * Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
14 * IEEE 802.1X Supplicant test code (to be used in place of wpa_supplicant.c.
15 * Not used in production version.
23 #include "eapol_supp/eapol_supp_sm.h"
24 #include "eap_peer/eap.h"
27 #include "eap_peer/eap_i.h"
28 #include "wpa_supplicant_i.h"
29 #include "radius/radius.h"
30 #include "radius/radius_client.h"
31 #include "ctrl_iface.h"
32 #include "pcsc_funcs.h"
35 extern int wpa_debug_level;
36 extern int wpa_debug_show_keys;
38 struct wpa_driver_ops *wpa_supplicant_drivers[] = { NULL };
41 struct eapol_test_data {
42 struct wpa_supplicant *wpa_s;
44 int eapol_test_num_reauths;
46 int num_mppe_ok, num_mppe_mismatch;
49 struct radius_msg *last_recv_radius;
50 struct in_addr own_ip_addr;
51 struct radius_client_data *radius;
52 struct hostapd_radius_servers *radius_conf;
54 u8 *last_eap_radius; /* last received EAP Response from Authentication
56 size_t last_eap_radius_len;
58 u8 authenticator_pmk[PMK_LEN];
59 size_t authenticator_pmk_len;
60 int radius_access_accept_received;
61 int radius_access_reject_received;
65 size_t eap_identity_len;
68 u8 own_addr[ETH_ALEN];
73 static struct eapol_test_data eapol_test;
76 static void send_eap_request_identity(void *eloop_ctx, void *timeout_ctx);
79 static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
80 int level, const char *txt, size_t len)
83 wpa_printf(MSG_DEBUG, "STA " MACSTR ": %s\n",
86 wpa_printf(MSG_DEBUG, "%s", txt);
90 static void ieee802_1x_encapsulate_radius(struct eapol_test_data *e,
91 const u8 *eap, size_t len)
93 struct radius_msg *msg;
95 const struct eap_hdr *hdr;
98 wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS "
101 e->radius_identifier = radius_client_get_id(e->radius);
102 msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
103 e->radius_identifier);
105 printf("Could not create net RADIUS packet\n");
109 radius_msg_make_authenticator(msg, (u8 *) e, sizeof(*e));
111 hdr = (const struct eap_hdr *) eap;
112 pos = (const u8 *) (hdr + 1);
113 if (len > sizeof(*hdr) && hdr->code == EAP_CODE_RESPONSE &&
114 pos[0] == EAP_TYPE_IDENTITY) {
116 os_free(e->eap_identity);
117 e->eap_identity_len = len - sizeof(*hdr) - 1;
118 e->eap_identity = os_malloc(e->eap_identity_len);
119 if (e->eap_identity) {
120 os_memcpy(e->eap_identity, pos, e->eap_identity_len);
121 wpa_hexdump(MSG_DEBUG, "Learned identity from "
122 "EAP-Response-Identity",
123 e->eap_identity, e->eap_identity_len);
127 if (e->eap_identity &&
128 !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
129 e->eap_identity, e->eap_identity_len)) {
130 printf("Could not add User-Name\n");
134 if (!radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
135 (u8 *) &e->own_ip_addr, 4)) {
136 printf("Could not add NAS-IP-Address\n");
140 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
141 MAC2STR(e->wpa_s->own_addr));
142 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
143 (u8 *) buf, os_strlen(buf))) {
144 printf("Could not add Calling-Station-Id\n");
148 /* TODO: should probably check MTU from driver config; 2304 is max for
149 * IEEE 802.11, but use 1400 to avoid problems with too large packets
151 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
152 printf("Could not add Framed-MTU\n");
156 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
157 RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
158 printf("Could not add NAS-Port-Type\n");
162 os_snprintf(buf, sizeof(buf), "%s", e->connect_info);
163 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
164 (u8 *) buf, os_strlen(buf))) {
165 printf("Could not add Connect-Info\n");
171 if (e->cui_flag == 1) {
174 } else if (e->cui_flag == 2) {
175 os_snprintf(buf, sizeof(buf), "%s", e->cui_str);
178 if (!radius_msg_add_attr(msg,
179 RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
181 printf("Could not add Chargeable-User-Identity\n");
186 if (eap && !radius_msg_add_eap(msg, eap, len)) {
187 printf("Could not add EAP-Message\n");
191 /* State attribute must be copied if and only if this packet is
192 * Access-Request reply to the previous Access-Challenge */
193 if (e->last_recv_radius && e->last_recv_radius->hdr->code ==
194 RADIUS_CODE_ACCESS_CHALLENGE) {
195 int res = radius_msg_copy_attr(msg, e->last_recv_radius,
198 printf("Could not copy State attribute from previous "
199 "Access-Challenge\n");
203 wpa_printf(MSG_DEBUG, " Copied RADIUS State "
208 radius_client_send(e->radius, msg, RADIUS_AUTH, e->wpa_s->own_addr);
212 radius_msg_free(msg);
217 static int eapol_test_eapol_send(void *ctx, int type, const u8 *buf,
220 /* struct wpa_supplicant *wpa_s = ctx; */
221 printf("WPA: eapol_test_eapol_send(type=%d len=%lu)\n",
222 type, (unsigned long) len);
223 if (type == IEEE802_1X_TYPE_EAP_PACKET) {
224 wpa_hexdump(MSG_DEBUG, "TX EAP -> RADIUS", buf, len);
225 ieee802_1x_encapsulate_radius(&eapol_test, buf, len);
231 static void eapol_test_set_config_blob(void *ctx,
232 struct wpa_config_blob *blob)
234 struct wpa_supplicant *wpa_s = ctx;
235 wpa_config_set_blob(wpa_s->conf, blob);
239 static const struct wpa_config_blob *
240 eapol_test_get_config_blob(void *ctx, const char *name)
242 struct wpa_supplicant *wpa_s = ctx;
243 return wpa_config_get_blob(wpa_s->conf, name);
247 static void eapol_test_eapol_done_cb(void *ctx)
249 printf("WPA: EAPOL processing complete\n");
253 static void eapol_sm_reauth(void *eloop_ctx, void *timeout_ctx)
255 struct eapol_test_data *e = eloop_ctx;
256 printf("\n\n\n\n\neapol_test: Triggering EAP reauthentication\n\n");
257 e->radius_access_accept_received = 0;
258 send_eap_request_identity(e->wpa_s, NULL);
262 static int eapol_test_compare_pmk(struct eapol_test_data *e)
267 if (eapol_sm_get_key(e->wpa_s->eapol, pmk, PMK_LEN) == 0) {
268 wpa_hexdump(MSG_DEBUG, "PMK from EAPOL", pmk, PMK_LEN);
269 if (os_memcmp(pmk, e->authenticator_pmk, PMK_LEN) != 0) {
270 printf("WARNING: PMK mismatch\n");
271 wpa_hexdump(MSG_DEBUG, "PMK from AS",
272 e->authenticator_pmk, PMK_LEN);
273 } else if (e->radius_access_accept_received)
275 } else if (e->authenticator_pmk_len == 16 &&
276 eapol_sm_get_key(e->wpa_s->eapol, pmk, 16) == 0) {
277 wpa_hexdump(MSG_DEBUG, "LEAP PMK from EAPOL", pmk, 16);
278 if (os_memcmp(pmk, e->authenticator_pmk, 16) != 0) {
279 printf("WARNING: PMK mismatch\n");
280 wpa_hexdump(MSG_DEBUG, "PMK from AS",
281 e->authenticator_pmk, 16);
282 } else if (e->radius_access_accept_received)
284 } else if (e->radius_access_accept_received && e->no_mppe_keys) {
285 /* No keying material expected */
289 if (ret && !e->no_mppe_keys)
290 e->num_mppe_mismatch++;
291 else if (!e->no_mppe_keys)
298 static void eapol_sm_cb(struct eapol_sm *eapol, int success, void *ctx)
300 struct eapol_test_data *e = ctx;
301 printf("eapol_sm_cb: success=%d\n", success);
302 e->eapol_test_num_reauths--;
303 if (e->eapol_test_num_reauths < 0)
306 eapol_test_compare_pmk(e);
307 eloop_register_timeout(0, 100000, eapol_sm_reauth, e, NULL);
312 static int test_eapol(struct eapol_test_data *e, struct wpa_supplicant *wpa_s,
313 struct wpa_ssid *ssid)
315 struct eapol_config eapol_conf;
316 struct eapol_ctx *ctx;
318 ctx = os_zalloc(sizeof(*ctx));
320 printf("Failed to allocate EAPOL context.\n");
324 ctx->msg_ctx = wpa_s;
325 ctx->scard_ctx = wpa_s->scard;
326 ctx->cb = eapol_sm_cb;
328 ctx->eapol_send_ctx = wpa_s;
330 ctx->eapol_done_cb = eapol_test_eapol_done_cb;
331 ctx->eapol_send = eapol_test_eapol_send;
332 ctx->set_config_blob = eapol_test_set_config_blob;
333 ctx->get_config_blob = eapol_test_get_config_blob;
334 #ifdef EAP_TLS_OPENSSL
335 ctx->opensc_engine_path = wpa_s->conf->opensc_engine_path;
336 ctx->pkcs11_engine_path = wpa_s->conf->pkcs11_engine_path;
337 ctx->pkcs11_module_path = wpa_s->conf->pkcs11_module_path;
338 #endif /* EAP_TLS_OPENSSL */
339 ctx->mac_addr = wpa_s->own_addr;
341 wpa_s->eapol = eapol_sm_init(ctx);
342 if (wpa_s->eapol == NULL) {
344 printf("Failed to initialize EAPOL state machines.\n");
348 wpa_s->current_ssid = ssid;
349 os_memset(&eapol_conf, 0, sizeof(eapol_conf));
350 eapol_conf.accept_802_1x_keys = 1;
351 eapol_conf.required_keys = 0;
352 eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
353 eapol_conf.workaround = ssid->eap_workaround;
354 eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
355 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
358 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
359 /* 802.1X::portControl = Auto */
360 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
366 static void test_eapol_clean(struct eapol_test_data *e,
367 struct wpa_supplicant *wpa_s)
369 radius_client_deinit(e->radius);
370 os_free(e->last_eap_radius);
371 if (e->last_recv_radius) {
372 radius_msg_free(e->last_recv_radius);
373 os_free(e->last_recv_radius);
375 os_free(e->eap_identity);
376 e->eap_identity = NULL;
377 eapol_sm_deinit(wpa_s->eapol);
379 if (e->radius_conf && e->radius_conf->auth_server) {
380 os_free(e->radius_conf->auth_server->shared_secret);
381 os_free(e->radius_conf->auth_server);
383 os_free(e->radius_conf);
384 e->radius_conf = NULL;
385 scard_deinit(wpa_s->scard);
386 if (wpa_s->ctrl_iface) {
387 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
388 wpa_s->ctrl_iface = NULL;
390 wpa_config_free(wpa_s->conf);
394 static void send_eap_request_identity(void *eloop_ctx, void *timeout_ctx)
396 struct wpa_supplicant *wpa_s = eloop_ctx;
398 struct ieee802_1x_hdr *hdr;
401 hdr = (struct ieee802_1x_hdr *) buf;
402 hdr->version = EAPOL_VERSION;
403 hdr->type = IEEE802_1X_TYPE_EAP_PACKET;
404 hdr->length = htons(5);
406 eap = (struct eap_hdr *) (hdr + 1);
407 eap->code = EAP_CODE_REQUEST;
409 eap->length = htons(5);
410 pos = (u8 *) (eap + 1);
411 *pos = EAP_TYPE_IDENTITY;
413 printf("Sending fake EAP-Request-Identity\n");
414 eapol_sm_rx_eapol(wpa_s->eapol, wpa_s->bssid, buf,
419 static void eapol_test_timeout(void *eloop_ctx, void *timeout_ctx)
421 struct eapol_test_data *e = eloop_ctx;
422 printf("EAPOL test timed out\n");
423 e->auth_timed_out = 1;
428 static char *eap_type_text(u8 type)
431 case EAP_TYPE_IDENTITY: return "Identity";
432 case EAP_TYPE_NOTIFICATION: return "Notification";
433 case EAP_TYPE_NAK: return "Nak";
434 case EAP_TYPE_TLS: return "TLS";
435 case EAP_TYPE_TTLS: return "TTLS";
436 case EAP_TYPE_PEAP: return "PEAP";
437 case EAP_TYPE_SIM: return "SIM";
438 case EAP_TYPE_GTC: return "GTC";
439 case EAP_TYPE_MD5: return "MD5";
440 case EAP_TYPE_OTP: return "OTP";
441 case EAP_TYPE_FAST: return "FAST";
442 case EAP_TYPE_SAKE: return "SAKE";
443 case EAP_TYPE_PSK: return "PSK";
444 default: return "Unknown";
449 static void ieee802_1x_decapsulate_radius(struct eapol_test_data *e)
456 struct radius_msg *msg;
458 if (e->last_recv_radius == NULL)
461 msg = e->last_recv_radius;
463 eap = radius_msg_get_eap(msg, &len);
465 /* draft-aboba-radius-rfc2869bis-20.txt, Chap. 2.6.3:
466 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
468 wpa_printf(MSG_DEBUG, "could not extract "
469 "EAP-Message from RADIUS message");
470 os_free(e->last_eap_radius);
471 e->last_eap_radius = NULL;
472 e->last_eap_radius_len = 0;
476 if (len < sizeof(*hdr)) {
477 wpa_printf(MSG_DEBUG, "too short EAP packet "
478 "received from authentication server");
483 if (len > sizeof(*hdr))
484 eap_type = eap[sizeof(*hdr)];
486 hdr = (struct eap_hdr *) eap;
488 case EAP_CODE_REQUEST:
489 os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
490 eap_type >= 0 ? eap_type_text(eap_type) : "??",
493 case EAP_CODE_RESPONSE:
494 os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
495 eap_type >= 0 ? eap_type_text(eap_type) : "??",
498 case EAP_CODE_SUCCESS:
499 os_strlcpy(buf, "EAP Success", sizeof(buf));
500 /* LEAP uses EAP Success within an authentication, so must not
501 * stop here with eloop_terminate(); */
503 case EAP_CODE_FAILURE:
504 os_strlcpy(buf, "EAP Failure", sizeof(buf));
508 os_strlcpy(buf, "unknown EAP code", sizeof(buf));
509 wpa_hexdump(MSG_DEBUG, "Decapsulated EAP packet", eap, len);
512 wpa_printf(MSG_DEBUG, "decapsulated EAP packet (code=%d "
513 "id=%d len=%d) from RADIUS server: %s",
514 hdr->code, hdr->identifier, ntohs(hdr->length), buf);
516 /* sta->eapol_sm->be_auth.idFromServer = hdr->identifier; */
518 os_free(e->last_eap_radius);
519 e->last_eap_radius = eap;
520 e->last_eap_radius_len = len;
523 struct ieee802_1x_hdr *dot1x;
524 dot1x = os_malloc(sizeof(*dot1x) + len);
525 assert(dot1x != NULL);
526 dot1x->version = EAPOL_VERSION;
527 dot1x->type = IEEE802_1X_TYPE_EAP_PACKET;
528 dot1x->length = htons(len);
529 os_memcpy((u8 *) (dot1x + 1), eap, len);
530 eapol_sm_rx_eapol(e->wpa_s->eapol, e->wpa_s->bssid,
531 (u8 *) dot1x, sizeof(*dot1x) + len);
537 static void ieee802_1x_get_keys(struct eapol_test_data *e,
538 struct radius_msg *msg, struct radius_msg *req,
539 u8 *shared_secret, size_t shared_secret_len)
541 struct radius_ms_mppe_keys *keys;
543 keys = radius_msg_get_ms_keys(msg, req, shared_secret,
545 if (keys && keys->send == NULL && keys->recv == NULL) {
547 keys = radius_msg_get_cisco_keys(msg, req, shared_secret,
553 wpa_hexdump(MSG_DEBUG, "MS-MPPE-Send-Key (sign)",
554 keys->send, keys->send_len);
557 wpa_hexdump(MSG_DEBUG, "MS-MPPE-Recv-Key (crypt)",
558 keys->recv, keys->recv_len);
559 e->authenticator_pmk_len =
560 keys->recv_len > PMK_LEN ? PMK_LEN :
562 os_memcpy(e->authenticator_pmk, keys->recv,
563 e->authenticator_pmk_len);
573 /* Process the RADIUS frames from Authentication Server */
574 static RadiusRxResult
575 ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
576 u8 *shared_secret, size_t shared_secret_len,
579 struct eapol_test_data *e = data;
581 /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
582 * present when packet contains an EAP-Message attribute */
583 if (msg->hdr->code == RADIUS_CODE_ACCESS_REJECT &&
584 radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
586 radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
587 wpa_printf(MSG_DEBUG, "Allowing RADIUS "
588 "Access-Reject without Message-Authenticator "
589 "since it does not include EAP-Message\n");
590 } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
592 printf("Incoming RADIUS packet did not have correct "
593 "Message-Authenticator - dropped\n");
594 return RADIUS_RX_UNKNOWN;
597 if (msg->hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
598 msg->hdr->code != RADIUS_CODE_ACCESS_REJECT &&
599 msg->hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
600 printf("Unknown RADIUS message code\n");
601 return RADIUS_RX_UNKNOWN;
604 e->radius_identifier = -1;
605 wpa_printf(MSG_DEBUG, "RADIUS packet matching with station");
607 if (e->last_recv_radius) {
608 radius_msg_free(e->last_recv_radius);
609 os_free(e->last_recv_radius);
612 e->last_recv_radius = msg;
614 switch (msg->hdr->code) {
615 case RADIUS_CODE_ACCESS_ACCEPT:
616 e->radius_access_accept_received = 1;
617 ieee802_1x_get_keys(e, msg, req, shared_secret,
620 case RADIUS_CODE_ACCESS_REJECT:
621 e->radius_access_reject_received = 1;
625 ieee802_1x_decapsulate_radius(e);
627 if ((msg->hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
628 e->eapol_test_num_reauths < 0) ||
629 msg->hdr->code == RADIUS_CODE_ACCESS_REJECT) {
633 return RADIUS_RX_QUEUED;
637 static void wpa_init_conf(struct eapol_test_data *e,
638 struct wpa_supplicant *wpa_s, const char *authsrv,
639 int port, const char *secret,
640 const char *cli_addr)
642 struct hostapd_radius_server *as;
646 os_memcpy(wpa_s->own_addr, e->own_addr, ETH_ALEN);
647 e->own_ip_addr.s_addr = htonl((127 << 24) | 1);
648 os_strlcpy(wpa_s->ifname, "test", sizeof(wpa_s->ifname));
650 e->radius_conf = os_zalloc(sizeof(struct hostapd_radius_servers));
651 assert(e->radius_conf != NULL);
652 e->radius_conf->num_auth_servers = 1;
653 as = os_zalloc(sizeof(struct hostapd_radius_server));
655 #if defined(CONFIG_NATIVE_WINDOWS) || defined(CONFIG_ANSI_C_EXTRA)
659 sscanf(authsrv, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
660 pos = (u8 *) &as->addr.u.v4;
666 #else /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
667 inet_aton(authsrv, &as->addr.u.v4);
668 #endif /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
669 as->addr.af = AF_INET;
671 as->shared_secret = (u8 *) os_strdup(secret);
672 as->shared_secret_len = os_strlen(secret);
673 e->radius_conf->auth_server = as;
674 e->radius_conf->auth_servers = as;
675 e->radius_conf->msg_dumps = 1;
677 if (hostapd_parse_ip_addr(cli_addr,
678 &e->radius_conf->client_addr) == 0)
679 e->radius_conf->force_client_addr = 1;
681 wpa_printf(MSG_ERROR, "Invalid IP address '%s'",
687 e->radius = radius_client_init(wpa_s, e->radius_conf);
688 assert(e->radius != NULL);
690 res = radius_client_register(e->radius, RADIUS_AUTH,
691 ieee802_1x_receive_auth, e);
696 static int scard_test(void)
698 struct scard_data *scard;
701 unsigned char _rand[16];
703 unsigned char sres[4];
705 #endif /* PCSC_FUNCS */
706 #define num_triplets 5
707 unsigned char rand_[num_triplets][16];
708 unsigned char sres_[num_triplets][4];
709 unsigned char kc_[num_triplets][8];
713 #define AKA_RAND_LEN 16
714 #define AKA_AUTN_LEN 16
715 #define AKA_AUTS_LEN 14
716 #define RES_MAX_LEN 16
719 unsigned char aka_rand[AKA_RAND_LEN];
720 unsigned char aka_autn[AKA_AUTN_LEN];
721 unsigned char aka_auts[AKA_AUTS_LEN];
722 unsigned char aka_res[RES_MAX_LEN];
724 unsigned char aka_ik[IK_LEN];
725 unsigned char aka_ck[CK_LEN];
727 scard = scard_init(SCARD_TRY_BOTH);
730 if (scard_set_pin(scard, "1234")) {
731 wpa_printf(MSG_WARNING, "PIN validation failed");
737 if (scard_get_imsi(scard, imsi, &len))
739 wpa_hexdump_ascii(MSG_DEBUG, "SCARD: IMSI", (u8 *) imsi, len);
740 /* NOTE: Permanent Username: 1 | IMSI */
742 os_memset(_rand, 0, sizeof(_rand));
743 if (scard_gsm_auth(scard, _rand, sres, kc))
746 os_memset(_rand, 0xff, sizeof(_rand));
747 if (scard_gsm_auth(scard, _rand, sres, kc))
750 for (i = 0; i < num_triplets; i++) {
751 os_memset(rand_[i], i, sizeof(rand_[i]));
752 if (scard_gsm_auth(scard, rand_[i], sres_[i], kc_[i]))
756 for (i = 0; i < num_triplets; i++) {
758 for (j = 0; j < len; j++)
759 printf("%c", imsi[j]);
761 for (j = 0; j < 16; j++)
762 printf("%02X", rand_[i][j]);
764 for (j = 0; j < 4; j++)
765 printf("%02X", sres_[i][j]);
767 for (j = 0; j < 8; j++)
768 printf("%02X", kc_[i][j]);
772 wpa_printf(MSG_DEBUG, "Trying to use UMTS authentication");
775 os_memset(aka_rand, 0xaa, 16);
776 os_memcpy(aka_autn, "\x86\x71\x31\xcb\xa2\xfc\x61\xdf"
777 "\xa3\xb3\x97\x9d\x07\x32\xa2\x12", 16);
779 res = scard_umts_auth(scard, aka_rand, aka_autn, aka_res, &aka_res_len,
780 aka_ik, aka_ck, aka_auts);
782 wpa_printf(MSG_DEBUG, "UMTS auth completed successfully");
783 wpa_hexdump(MSG_DEBUG, "RES", aka_res, aka_res_len);
784 wpa_hexdump(MSG_DEBUG, "IK", aka_ik, IK_LEN);
785 wpa_hexdump(MSG_DEBUG, "CK", aka_ck, CK_LEN);
786 } else if (res == -2) {
787 wpa_printf(MSG_DEBUG, "UMTS auth resulted in synchronization "
789 wpa_hexdump(MSG_DEBUG, "AUTS", aka_auts, AKA_AUTS_LEN);
791 wpa_printf(MSG_DEBUG, "UMTS auth failed");
802 static int scard_get_triplets(int argc, char *argv[])
804 struct scard_data *scard;
807 unsigned char _rand[16];
808 unsigned char sres[4];
814 if (argc < 2 || ((num_triplets = atoi(argv[1])) <= 0)) {
815 printf("invalid parameters for sim command\n");
819 if (argc <= 2 || os_strcmp(argv[2], "debug") != 0) {
820 /* disable debug output */
821 wpa_debug_level = 99;
824 scard = scard_init(SCARD_GSM_SIM_ONLY);
826 printf("Failed to open smartcard connection\n");
829 if (scard_set_pin(scard, argv[0])) {
830 wpa_printf(MSG_WARNING, "PIN validation failed");
836 if (scard_get_imsi(scard, imsi, &len)) {
841 for (i = 0; i < num_triplets; i++) {
842 os_memset(_rand, i, sizeof(_rand));
843 if (scard_gsm_auth(scard, _rand, sres, kc))
846 /* IMSI:Kc:SRES:RAND */
847 for (j = 0; j < len; j++)
848 printf("%c", imsi[j]);
850 for (j = 0; j < 8; j++)
851 printf("%02X", kc[j]);
853 for (j = 0; j < 4; j++)
854 printf("%02X", sres[j]);
856 for (j = 0; j < 16; j++)
857 printf("%02X", _rand[j]);
867 static void eapol_test_terminate(int sig, void *eloop_ctx,
870 struct wpa_supplicant *wpa_s = eloop_ctx;
871 wpa_msg(wpa_s, MSG_INFO, "Signal %d received - terminating", sig);
876 static void usage(void)
879 "eapol_test [-nWS] -c<conf> [-a<AS IP>] [-p<AS port>] "
880 "[-s<AS secret>]\\\n"
881 " [-r<count>] [-t<timeout>] [-C<Connect-Info>] \\\n"
882 " [-M<client MAC address>] \\\n"
883 " [-I<CUI>] [-i] [-A<client IP>]\n"
885 "eapol_test sim <PIN> <num triplets> [debug]\n"
888 " -c<conf> = configuration file\n"
889 " -a<AS IP> = IP address of the authentication server, "
890 "default 127.0.0.1\n"
891 " -p<AS port> = UDP port of the authentication server, "
893 " -s<AS secret> = shared secret with the authentication "
894 "server, default 'radius'\n"
895 " -A<client IP> = IP address of the client, default: select "
897 " -r<count> = number of re-authentications\n"
898 " -W = wait for a control interface monitor before starting\n"
899 " -S = save configuration after authentiation\n"
900 " -n = no MPPE keys expected\n"
901 " -t<timeout> = sets timeout in seconds (default: 30 s)\n"
902 " -C<Connect-Info> = RADIUS Connect-Info (default: "
903 "CONNECT 11Mbps 802.11b)\n"
904 " -M<client MAC address> = Set own MAC address "
905 "(Calling-Station-Id,\n"
906 " default: 02:00:00:00:00:01)\n"
907 " -I<CUI> = send Chargeable-User-Identity containing the "
909 " -i = send NUL value in Chargeable-User-Identity\n");
913 int main(int argc, char *argv[])
915 struct wpa_supplicant wpa_s;
916 int c, ret = 1, wait_for_monitor = 0, save_config = 0;
917 char *as_addr = "127.0.0.1";
919 char *as_secret = "radius";
920 char *cli_addr = NULL;
924 if (os_program_init())
927 hostapd_logger_register_cb(hostapd_logger_cb);
929 os_memset(&eapol_test, 0, sizeof(eapol_test));
930 eapol_test.connect_info = "CONNECT 11Mbps 802.11b";
931 os_memcpy(eapol_test.own_addr, "\x02\x00\x00\x00\x00\x01", ETH_ALEN);
934 wpa_debug_show_keys = 1;
937 c = getopt(argc, argv, "a:A:c:C:iI:M:np:r:s:St:W");
951 eapol_test.connect_info = optarg;
954 eapol_test.cui_flag = 1;
957 eapol_test.cui_flag = 2;
958 eapol_test.cui_str = optarg;
961 if (hwaddr_aton(optarg, eapol_test.own_addr)) {
967 eapol_test.no_mppe_keys++;
970 as_port = atoi(optarg);
973 eapol_test.eapol_test_num_reauths = atoi(optarg);
982 timeout = atoi(optarg);
993 if (argc > optind && os_strcmp(argv[optind], "scard") == 0) {
997 if (argc > optind && os_strcmp(argv[optind], "sim") == 0) {
998 return scard_get_triplets(argc - optind - 1,
1004 printf("Configuration file is required.\n");
1008 if (eap_peer_register_methods()) {
1009 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
1013 if (eloop_init(&wpa_s)) {
1014 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
1018 os_memset(&wpa_s, 0, sizeof(wpa_s));
1019 eapol_test.wpa_s = &wpa_s;
1020 wpa_s.conf = wpa_config_read(conf);
1021 if (wpa_s.conf == NULL) {
1022 printf("Failed to parse configuration file '%s'.\n", conf);
1025 if (wpa_s.conf->ssid == NULL) {
1026 printf("No networks defined.\n");
1030 wpa_init_conf(&eapol_test, &wpa_s, as_addr, as_port, as_secret,
1032 wpa_s.ctrl_iface = wpa_supplicant_ctrl_iface_init(&wpa_s);
1033 if (wpa_s.ctrl_iface == NULL) {
1034 printf("Failed to initialize control interface '%s'.\n"
1035 "You may have another eapol_test process already "
1036 "running or the file was\n"
1037 "left by an unclean termination of eapol_test in "
1038 "which case you will need\n"
1039 "to manually remove this file before starting "
1040 "eapol_test again.\n",
1041 wpa_s.conf->ctrl_interface);
1044 if (wpa_supplicant_scard_init(&wpa_s, wpa_s.conf->ssid))
1047 if (test_eapol(&eapol_test, &wpa_s, wpa_s.conf->ssid))
1050 if (wait_for_monitor)
1051 wpa_supplicant_ctrl_iface_wait(wpa_s.ctrl_iface);
1053 eloop_register_timeout(timeout, 0, eapol_test_timeout, &eapol_test,
1055 eloop_register_timeout(0, 0, send_eap_request_identity, &wpa_s, NULL);
1056 eloop_register_signal_terminate(eapol_test_terminate, NULL);
1057 eloop_register_signal_reconfig(eapol_test_terminate, NULL);
1060 eloop_cancel_timeout(eapol_test_timeout, &eapol_test, NULL);
1061 eloop_cancel_timeout(eapol_sm_reauth, &eapol_test, NULL);
1063 if (eapol_test_compare_pmk(&eapol_test) == 0 ||
1064 eapol_test.no_mppe_keys)
1066 if (eapol_test.auth_timed_out)
1068 if (eapol_test.radius_access_reject_received)
1072 wpa_config_write(conf, wpa_s.conf);
1074 test_eapol_clean(&eapol_test, &wpa_s);
1076 eap_peer_unregister_methods();
1080 printf("MPPE keys OK: %d mismatch: %d\n",
1081 eapol_test.num_mppe_ok, eapol_test.num_mppe_mismatch);
1082 if (eapol_test.num_mppe_mismatch)
1085 printf("FAILURE\n");
1087 printf("SUCCESS\n");
1089 os_program_deinit();