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 */
340 wpa_s->eapol = eapol_sm_init(ctx);
341 if (wpa_s->eapol == NULL) {
343 printf("Failed to initialize EAPOL state machines.\n");
347 wpa_s->current_ssid = ssid;
348 os_memset(&eapol_conf, 0, sizeof(eapol_conf));
349 eapol_conf.accept_802_1x_keys = 1;
350 eapol_conf.required_keys = 0;
351 eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
352 eapol_conf.workaround = ssid->eap_workaround;
353 eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
354 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
357 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
358 /* 802.1X::portControl = Auto */
359 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
365 static void test_eapol_clean(struct eapol_test_data *e,
366 struct wpa_supplicant *wpa_s)
368 radius_client_deinit(e->radius);
369 os_free(e->last_eap_radius);
370 if (e->last_recv_radius) {
371 radius_msg_free(e->last_recv_radius);
372 os_free(e->last_recv_radius);
374 os_free(e->eap_identity);
375 e->eap_identity = NULL;
376 eapol_sm_deinit(wpa_s->eapol);
378 if (e->radius_conf && e->radius_conf->auth_server) {
379 os_free(e->radius_conf->auth_server->shared_secret);
380 os_free(e->radius_conf->auth_server);
382 os_free(e->radius_conf);
383 e->radius_conf = NULL;
384 scard_deinit(wpa_s->scard);
385 if (wpa_s->ctrl_iface) {
386 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
387 wpa_s->ctrl_iface = NULL;
389 wpa_config_free(wpa_s->conf);
393 static void send_eap_request_identity(void *eloop_ctx, void *timeout_ctx)
395 struct wpa_supplicant *wpa_s = eloop_ctx;
397 struct ieee802_1x_hdr *hdr;
400 hdr = (struct ieee802_1x_hdr *) buf;
401 hdr->version = EAPOL_VERSION;
402 hdr->type = IEEE802_1X_TYPE_EAP_PACKET;
403 hdr->length = htons(5);
405 eap = (struct eap_hdr *) (hdr + 1);
406 eap->code = EAP_CODE_REQUEST;
408 eap->length = htons(5);
409 pos = (u8 *) (eap + 1);
410 *pos = EAP_TYPE_IDENTITY;
412 printf("Sending fake EAP-Request-Identity\n");
413 eapol_sm_rx_eapol(wpa_s->eapol, wpa_s->bssid, buf,
418 static void eapol_test_timeout(void *eloop_ctx, void *timeout_ctx)
420 struct eapol_test_data *e = eloop_ctx;
421 printf("EAPOL test timed out\n");
422 e->auth_timed_out = 1;
427 static char *eap_type_text(u8 type)
430 case EAP_TYPE_IDENTITY: return "Identity";
431 case EAP_TYPE_NOTIFICATION: return "Notification";
432 case EAP_TYPE_NAK: return "Nak";
433 case EAP_TYPE_TLS: return "TLS";
434 case EAP_TYPE_TTLS: return "TTLS";
435 case EAP_TYPE_PEAP: return "PEAP";
436 case EAP_TYPE_SIM: return "SIM";
437 case EAP_TYPE_GTC: return "GTC";
438 case EAP_TYPE_MD5: return "MD5";
439 case EAP_TYPE_OTP: return "OTP";
440 case EAP_TYPE_FAST: return "FAST";
441 case EAP_TYPE_SAKE: return "SAKE";
442 case EAP_TYPE_PSK: return "PSK";
443 default: return "Unknown";
448 static void ieee802_1x_decapsulate_radius(struct eapol_test_data *e)
455 struct radius_msg *msg;
457 if (e->last_recv_radius == NULL)
460 msg = e->last_recv_radius;
462 eap = radius_msg_get_eap(msg, &len);
464 /* draft-aboba-radius-rfc2869bis-20.txt, Chap. 2.6.3:
465 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
467 wpa_printf(MSG_DEBUG, "could not extract "
468 "EAP-Message from RADIUS message");
469 os_free(e->last_eap_radius);
470 e->last_eap_radius = NULL;
471 e->last_eap_radius_len = 0;
475 if (len < sizeof(*hdr)) {
476 wpa_printf(MSG_DEBUG, "too short EAP packet "
477 "received from authentication server");
482 if (len > sizeof(*hdr))
483 eap_type = eap[sizeof(*hdr)];
485 hdr = (struct eap_hdr *) eap;
487 case EAP_CODE_REQUEST:
488 os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
489 eap_type >= 0 ? eap_type_text(eap_type) : "??",
492 case EAP_CODE_RESPONSE:
493 os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
494 eap_type >= 0 ? eap_type_text(eap_type) : "??",
497 case EAP_CODE_SUCCESS:
498 os_strlcpy(buf, "EAP Success", sizeof(buf));
499 /* LEAP uses EAP Success within an authentication, so must not
500 * stop here with eloop_terminate(); */
502 case EAP_CODE_FAILURE:
503 os_strlcpy(buf, "EAP Failure", sizeof(buf));
507 os_strlcpy(buf, "unknown EAP code", sizeof(buf));
508 wpa_hexdump(MSG_DEBUG, "Decapsulated EAP packet", eap, len);
511 wpa_printf(MSG_DEBUG, "decapsulated EAP packet (code=%d "
512 "id=%d len=%d) from RADIUS server: %s",
513 hdr->code, hdr->identifier, ntohs(hdr->length), buf);
515 /* sta->eapol_sm->be_auth.idFromServer = hdr->identifier; */
517 os_free(e->last_eap_radius);
518 e->last_eap_radius = eap;
519 e->last_eap_radius_len = len;
522 struct ieee802_1x_hdr *dot1x;
523 dot1x = os_malloc(sizeof(*dot1x) + len);
524 assert(dot1x != NULL);
525 dot1x->version = EAPOL_VERSION;
526 dot1x->type = IEEE802_1X_TYPE_EAP_PACKET;
527 dot1x->length = htons(len);
528 os_memcpy((u8 *) (dot1x + 1), eap, len);
529 eapol_sm_rx_eapol(e->wpa_s->eapol, e->wpa_s->bssid,
530 (u8 *) dot1x, sizeof(*dot1x) + len);
536 static void ieee802_1x_get_keys(struct eapol_test_data *e,
537 struct radius_msg *msg, struct radius_msg *req,
538 u8 *shared_secret, size_t shared_secret_len)
540 struct radius_ms_mppe_keys *keys;
542 keys = radius_msg_get_ms_keys(msg, req, shared_secret,
544 if (keys && keys->send == NULL && keys->recv == NULL) {
546 keys = radius_msg_get_cisco_keys(msg, req, shared_secret,
552 wpa_hexdump(MSG_DEBUG, "MS-MPPE-Send-Key (sign)",
553 keys->send, keys->send_len);
556 wpa_hexdump(MSG_DEBUG, "MS-MPPE-Recv-Key (crypt)",
557 keys->recv, keys->recv_len);
558 e->authenticator_pmk_len =
559 keys->recv_len > PMK_LEN ? PMK_LEN :
561 os_memcpy(e->authenticator_pmk, keys->recv,
562 e->authenticator_pmk_len);
563 if (e->authenticator_pmk_len == 16 && keys->send &&
564 keys->send_len == 16) {
565 /* MS-CHAP-v2 derives 16 octet keys */
566 wpa_printf(MSG_DEBUG, "Use MS-MPPE-Send-Key "
567 "to extend PMK to 32 octets");
568 os_memcpy(e->authenticator_pmk +
569 e->authenticator_pmk_len,
570 keys->send, keys->send_len);
571 e->authenticator_pmk_len += keys->send_len;
582 /* Process the RADIUS frames from Authentication Server */
583 static RadiusRxResult
584 ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
585 u8 *shared_secret, size_t shared_secret_len,
588 struct eapol_test_data *e = data;
590 /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
591 * present when packet contains an EAP-Message attribute */
592 if (msg->hdr->code == RADIUS_CODE_ACCESS_REJECT &&
593 radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
595 radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
596 wpa_printf(MSG_DEBUG, "Allowing RADIUS "
597 "Access-Reject without Message-Authenticator "
598 "since it does not include EAP-Message\n");
599 } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
601 printf("Incoming RADIUS packet did not have correct "
602 "Message-Authenticator - dropped\n");
603 return RADIUS_RX_UNKNOWN;
606 if (msg->hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
607 msg->hdr->code != RADIUS_CODE_ACCESS_REJECT &&
608 msg->hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
609 printf("Unknown RADIUS message code\n");
610 return RADIUS_RX_UNKNOWN;
613 e->radius_identifier = -1;
614 wpa_printf(MSG_DEBUG, "RADIUS packet matching with station");
616 if (e->last_recv_radius) {
617 radius_msg_free(e->last_recv_radius);
618 os_free(e->last_recv_radius);
621 e->last_recv_radius = msg;
623 switch (msg->hdr->code) {
624 case RADIUS_CODE_ACCESS_ACCEPT:
625 e->radius_access_accept_received = 1;
626 ieee802_1x_get_keys(e, msg, req, shared_secret,
629 case RADIUS_CODE_ACCESS_REJECT:
630 e->radius_access_reject_received = 1;
634 ieee802_1x_decapsulate_radius(e);
636 if ((msg->hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
637 e->eapol_test_num_reauths < 0) ||
638 msg->hdr->code == RADIUS_CODE_ACCESS_REJECT) {
642 return RADIUS_RX_QUEUED;
646 static void wpa_init_conf(struct eapol_test_data *e,
647 struct wpa_supplicant *wpa_s, const char *authsrv,
648 int port, const char *secret,
649 const char *cli_addr)
651 struct hostapd_radius_server *as;
655 os_memcpy(wpa_s->own_addr, e->own_addr, ETH_ALEN);
656 e->own_ip_addr.s_addr = htonl((127 << 24) | 1);
657 os_strlcpy(wpa_s->ifname, "test", sizeof(wpa_s->ifname));
659 e->radius_conf = os_zalloc(sizeof(struct hostapd_radius_servers));
660 assert(e->radius_conf != NULL);
661 e->radius_conf->num_auth_servers = 1;
662 as = os_zalloc(sizeof(struct hostapd_radius_server));
664 #if defined(CONFIG_NATIVE_WINDOWS) || defined(CONFIG_ANSI_C_EXTRA)
668 sscanf(authsrv, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
669 pos = (u8 *) &as->addr.u.v4;
675 #else /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
676 inet_aton(authsrv, &as->addr.u.v4);
677 #endif /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
678 as->addr.af = AF_INET;
680 as->shared_secret = (u8 *) os_strdup(secret);
681 as->shared_secret_len = os_strlen(secret);
682 e->radius_conf->auth_server = as;
683 e->radius_conf->auth_servers = as;
684 e->radius_conf->msg_dumps = 1;
686 if (hostapd_parse_ip_addr(cli_addr,
687 &e->radius_conf->client_addr) == 0)
688 e->radius_conf->force_client_addr = 1;
690 wpa_printf(MSG_ERROR, "Invalid IP address '%s'",
696 e->radius = radius_client_init(wpa_s, e->radius_conf);
697 assert(e->radius != NULL);
699 res = radius_client_register(e->radius, RADIUS_AUTH,
700 ieee802_1x_receive_auth, e);
705 static int scard_test(void)
707 struct scard_data *scard;
710 unsigned char _rand[16];
712 unsigned char sres[4];
714 #endif /* PCSC_FUNCS */
715 #define num_triplets 5
716 unsigned char rand_[num_triplets][16];
717 unsigned char sres_[num_triplets][4];
718 unsigned char kc_[num_triplets][8];
722 #define AKA_RAND_LEN 16
723 #define AKA_AUTN_LEN 16
724 #define AKA_AUTS_LEN 14
725 #define RES_MAX_LEN 16
728 unsigned char aka_rand[AKA_RAND_LEN];
729 unsigned char aka_autn[AKA_AUTN_LEN];
730 unsigned char aka_auts[AKA_AUTS_LEN];
731 unsigned char aka_res[RES_MAX_LEN];
733 unsigned char aka_ik[IK_LEN];
734 unsigned char aka_ck[CK_LEN];
736 scard = scard_init(SCARD_TRY_BOTH);
739 if (scard_set_pin(scard, "1234")) {
740 wpa_printf(MSG_WARNING, "PIN validation failed");
746 if (scard_get_imsi(scard, imsi, &len))
748 wpa_hexdump_ascii(MSG_DEBUG, "SCARD: IMSI", (u8 *) imsi, len);
749 /* NOTE: Permanent Username: 1 | IMSI */
751 os_memset(_rand, 0, sizeof(_rand));
752 if (scard_gsm_auth(scard, _rand, sres, kc))
755 os_memset(_rand, 0xff, sizeof(_rand));
756 if (scard_gsm_auth(scard, _rand, sres, kc))
759 for (i = 0; i < num_triplets; i++) {
760 os_memset(rand_[i], i, sizeof(rand_[i]));
761 if (scard_gsm_auth(scard, rand_[i], sres_[i], kc_[i]))
765 for (i = 0; i < num_triplets; i++) {
767 for (j = 0; j < len; j++)
768 printf("%c", imsi[j]);
770 for (j = 0; j < 16; j++)
771 printf("%02X", rand_[i][j]);
773 for (j = 0; j < 4; j++)
774 printf("%02X", sres_[i][j]);
776 for (j = 0; j < 8; j++)
777 printf("%02X", kc_[i][j]);
781 wpa_printf(MSG_DEBUG, "Trying to use UMTS authentication");
784 os_memset(aka_rand, 0xaa, 16);
785 os_memcpy(aka_autn, "\x86\x71\x31\xcb\xa2\xfc\x61\xdf"
786 "\xa3\xb3\x97\x9d\x07\x32\xa2\x12", 16);
788 res = scard_umts_auth(scard, aka_rand, aka_autn, aka_res, &aka_res_len,
789 aka_ik, aka_ck, aka_auts);
791 wpa_printf(MSG_DEBUG, "UMTS auth completed successfully");
792 wpa_hexdump(MSG_DEBUG, "RES", aka_res, aka_res_len);
793 wpa_hexdump(MSG_DEBUG, "IK", aka_ik, IK_LEN);
794 wpa_hexdump(MSG_DEBUG, "CK", aka_ck, CK_LEN);
795 } else if (res == -2) {
796 wpa_printf(MSG_DEBUG, "UMTS auth resulted in synchronization "
798 wpa_hexdump(MSG_DEBUG, "AUTS", aka_auts, AKA_AUTS_LEN);
800 wpa_printf(MSG_DEBUG, "UMTS auth failed");
811 static int scard_get_triplets(int argc, char *argv[])
813 struct scard_data *scard;
816 unsigned char _rand[16];
817 unsigned char sres[4];
823 if (argc < 2 || ((num_triplets = atoi(argv[1])) <= 0)) {
824 printf("invalid parameters for sim command\n");
828 if (argc <= 2 || os_strcmp(argv[2], "debug") != 0) {
829 /* disable debug output */
830 wpa_debug_level = 99;
833 scard = scard_init(SCARD_GSM_SIM_ONLY);
835 printf("Failed to open smartcard connection\n");
838 if (scard_set_pin(scard, argv[0])) {
839 wpa_printf(MSG_WARNING, "PIN validation failed");
845 if (scard_get_imsi(scard, imsi, &len)) {
850 for (i = 0; i < num_triplets; i++) {
851 os_memset(_rand, i, sizeof(_rand));
852 if (scard_gsm_auth(scard, _rand, sres, kc))
855 /* IMSI:Kc:SRES:RAND */
856 for (j = 0; j < len; j++)
857 printf("%c", imsi[j]);
859 for (j = 0; j < 8; j++)
860 printf("%02X", kc[j]);
862 for (j = 0; j < 4; j++)
863 printf("%02X", sres[j]);
865 for (j = 0; j < 16; j++)
866 printf("%02X", _rand[j]);
876 static void eapol_test_terminate(int sig, void *eloop_ctx,
879 struct wpa_supplicant *wpa_s = eloop_ctx;
880 wpa_msg(wpa_s, MSG_INFO, "Signal %d received - terminating", sig);
885 static void usage(void)
888 "eapol_test [-nWS] -c<conf> [-a<AS IP>] [-p<AS port>] "
889 "[-s<AS secret>]\\\n"
890 " [-r<count>] [-t<timeout>] [-C<Connect-Info>] \\\n"
891 " [-M<client MAC address>] \\\n"
892 " [-I<CUI>] [-i] [-A<client IP>]\n"
894 "eapol_test sim <PIN> <num triplets> [debug]\n"
897 " -c<conf> = configuration file\n"
898 " -a<AS IP> = IP address of the authentication server, "
899 "default 127.0.0.1\n"
900 " -p<AS port> = UDP port of the authentication server, "
902 " -s<AS secret> = shared secret with the authentication "
903 "server, default 'radius'\n"
904 " -A<client IP> = IP address of the client, default: select "
906 " -r<count> = number of re-authentications\n"
907 " -W = wait for a control interface monitor before starting\n"
908 " -S = save configuration after authentiation\n"
909 " -n = no MPPE keys expected\n"
910 " -t<timeout> = sets timeout in seconds (default: 30 s)\n"
911 " -C<Connect-Info> = RADIUS Connect-Info (default: "
912 "CONNECT 11Mbps 802.11b)\n"
913 " -M<client MAC address> = Set own MAC address "
914 "(Calling-Station-Id,\n"
915 " default: 02:00:00:00:00:01)\n"
916 " -I<CUI> = send Chargeable-User-Identity containing the "
918 " -i = send NUL value in Chargeable-User-Identity\n");
922 int main(int argc, char *argv[])
924 struct wpa_supplicant wpa_s;
925 int c, ret = 1, wait_for_monitor = 0, save_config = 0;
926 char *as_addr = "127.0.0.1";
928 char *as_secret = "radius";
929 char *cli_addr = NULL;
933 if (os_program_init())
936 hostapd_logger_register_cb(hostapd_logger_cb);
938 os_memset(&eapol_test, 0, sizeof(eapol_test));
939 eapol_test.connect_info = "CONNECT 11Mbps 802.11b";
940 os_memcpy(eapol_test.own_addr, "\x02\x00\x00\x00\x00\x01", ETH_ALEN);
943 wpa_debug_show_keys = 1;
946 c = getopt(argc, argv, "a:A:c:C:iI:M:np:r:s:St:W");
960 eapol_test.connect_info = optarg;
963 eapol_test.cui_flag = 1;
966 eapol_test.cui_flag = 2;
967 eapol_test.cui_str = optarg;
970 if (hwaddr_aton(optarg, eapol_test.own_addr)) {
976 eapol_test.no_mppe_keys++;
979 as_port = atoi(optarg);
982 eapol_test.eapol_test_num_reauths = atoi(optarg);
991 timeout = atoi(optarg);
1002 if (argc > optind && os_strcmp(argv[optind], "scard") == 0) {
1003 return scard_test();
1006 if (argc > optind && os_strcmp(argv[optind], "sim") == 0) {
1007 return scard_get_triplets(argc - optind - 1,
1013 printf("Configuration file is required.\n");
1017 if (eap_peer_register_methods()) {
1018 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
1022 if (eloop_init(&wpa_s)) {
1023 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
1027 os_memset(&wpa_s, 0, sizeof(wpa_s));
1028 eapol_test.wpa_s = &wpa_s;
1029 wpa_s.conf = wpa_config_read(conf);
1030 if (wpa_s.conf == NULL) {
1031 printf("Failed to parse configuration file '%s'.\n", conf);
1034 if (wpa_s.conf->ssid == NULL) {
1035 printf("No networks defined.\n");
1039 wpa_init_conf(&eapol_test, &wpa_s, as_addr, as_port, as_secret,
1041 wpa_s.ctrl_iface = wpa_supplicant_ctrl_iface_init(&wpa_s);
1042 if (wpa_s.ctrl_iface == NULL) {
1043 printf("Failed to initialize control interface '%s'.\n"
1044 "You may have another eapol_test process already "
1045 "running or the file was\n"
1046 "left by an unclean termination of eapol_test in "
1047 "which case you will need\n"
1048 "to manually remove this file before starting "
1049 "eapol_test again.\n",
1050 wpa_s.conf->ctrl_interface);
1053 if (wpa_supplicant_scard_init(&wpa_s, wpa_s.conf->ssid))
1056 if (test_eapol(&eapol_test, &wpa_s, wpa_s.conf->ssid))
1059 if (wait_for_monitor)
1060 wpa_supplicant_ctrl_iface_wait(wpa_s.ctrl_iface);
1062 eloop_register_timeout(timeout, 0, eapol_test_timeout, &eapol_test,
1064 eloop_register_timeout(0, 0, send_eap_request_identity, &wpa_s, NULL);
1065 eloop_register_signal_terminate(eapol_test_terminate, NULL);
1066 eloop_register_signal_reconfig(eapol_test_terminate, NULL);
1069 eloop_cancel_timeout(eapol_test_timeout, &eapol_test, NULL);
1070 eloop_cancel_timeout(eapol_sm_reauth, &eapol_test, NULL);
1072 if (eapol_test_compare_pmk(&eapol_test) == 0 ||
1073 eapol_test.no_mppe_keys)
1075 if (eapol_test.auth_timed_out)
1077 if (eapol_test.radius_access_reject_received)
1081 wpa_config_write(conf, wpa_s.conf);
1083 test_eapol_clean(&eapol_test, &wpa_s);
1085 eap_peer_unregister_methods();
1089 printf("MPPE keys OK: %d mismatch: %d\n",
1090 eapol_test.num_mppe_ok, eapol_test.num_mppe_mismatch);
1091 if (eapol_test.num_mppe_mismatch)
1094 printf("FAILURE\n");
1096 printf("SUCCESS\n");
1098 os_program_deinit();