2 * WPA Supplicant - test code
3 * Copyright (c) 2003-2005, Jouni Malinen <jkmaline@cc.hut.fi>
5 * IEEE 802.1X Supplicant test code (to be used in place of wpa_supplicant.c.
6 * Not used in production version.
16 #include <netinet/in.h>
18 #include <arpa/inet.h>
26 #include "wpa_supplicant.h"
27 #include "wpa_supplicant_i.h"
29 #include "radius_client.h"
30 #include "l2_packet.h"
31 #include "ctrl_iface.h"
32 #include "pcsc_funcs.h"
35 extern int wpa_debug_level;
36 extern int wpa_debug_show_keys;
37 static int eapol_test_num_reauths = 0;
38 static int no_mppe_keys = 0;
39 static int num_mppe_ok = 0, num_mppe_mismatch = 0;
41 static void send_eap_request_identity(void *eloop_ctx, void *timeout_ctx);
44 void wpa_msg(struct wpa_supplicant *wpa_s, int level, char *fmt, ...)
48 const int buflen = 2048;
53 printf("Failed to allocate message buffer for:\n");
61 len = vsnprintf(buf, buflen, fmt, ap);
63 wpa_printf(level, "%s", buf);
64 wpa_supplicant_ctrl_iface_send(wpa_s, level, buf, len);
69 void wpa_supplicant_event(struct wpa_supplicant *wpa_s, wpa_event_type event,
70 union wpa_event_data *data)
75 int rsn_preauth_init(struct wpa_supplicant *wpa_s, u8 *dst)
81 void rsn_preauth_deinit(struct wpa_supplicant *wpa_s)
86 int pmksa_cache_list(struct wpa_supplicant *wpa_s, char *buf, size_t len)
92 int wpa_get_mib(struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
98 void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
103 const char * wpa_ssid_txt(u8 *ssid, size_t ssid_len)
109 int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
115 static void ieee802_1x_encapsulate_radius(struct wpa_supplicant *wpa_s,
118 struct radius_msg *msg;
123 wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS "
126 wpa_s->radius_identifier = radius_client_get_id(wpa_s);
127 msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
128 wpa_s->radius_identifier);
130 printf("Could not create net RADIUS packet\n");
134 radius_msg_make_authenticator(msg, (u8 *) wpa_s, sizeof(*wpa_s));
136 hdr = (struct eap_hdr *) eap;
137 pos = (u8 *) (hdr + 1);
138 if (len > sizeof(*hdr) && hdr->code == EAP_CODE_RESPONSE &&
139 pos[0] == EAP_TYPE_IDENTITY) {
141 free(wpa_s->eap_identity);
142 wpa_s->eap_identity_len = len - sizeof(*hdr) - 1;
143 wpa_s->eap_identity = malloc(wpa_s->eap_identity_len);
144 if (wpa_s->eap_identity) {
145 memcpy(wpa_s->eap_identity, pos,
146 wpa_s->eap_identity_len);
147 wpa_hexdump(MSG_DEBUG, "Learned identity from "
148 "EAP-Response-Identity",
150 wpa_s->eap_identity_len);
154 if (wpa_s->eap_identity &&
155 !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
157 wpa_s->eap_identity_len)) {
158 printf("Could not add User-Name\n");
162 if (!radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
163 (u8 *) &wpa_s->own_ip_addr, 4)) {
164 printf("Could not add NAS-IP-Address\n");
168 snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
169 MAC2STR(wpa_s->own_addr));
170 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
171 (u8 *) buf, strlen(buf))) {
172 printf("Could not add Calling-Station-Id\n");
176 /* TODO: should probably check MTU from driver config; 2304 is max for
177 * IEEE 802.11, but use 1400 to avoid problems with too large packets
179 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
180 printf("Could not add Framed-MTU\n");
184 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
185 RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
186 printf("Could not add NAS-Port-Type\n");
190 snprintf(buf, sizeof(buf), "CONNECT 11Mbps 802.11b");
191 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
192 (u8 *) buf, strlen(buf))) {
193 printf("Could not add Connect-Info\n");
197 if (eap && !radius_msg_add_eap(msg, eap, len)) {
198 printf("Could not add EAP-Message\n");
202 /* State attribute must be copied if and only if this packet is
203 * Access-Request reply to the previous Access-Challenge */
204 if (wpa_s->last_recv_radius && wpa_s->last_recv_radius->hdr->code ==
205 RADIUS_CODE_ACCESS_CHALLENGE) {
206 int res = radius_msg_copy_attr(msg, wpa_s->last_recv_radius,
209 printf("Could not copy State attribute from previous "
210 "Access-Challenge\n");
214 wpa_printf(MSG_DEBUG, " Copied RADIUS State "
219 radius_client_send(wpa_s, msg, RADIUS_AUTH, wpa_s->own_addr);
223 radius_msg_free(msg);
228 static int eapol_test_eapol_send(void *ctx, int type, u8 *buf, size_t len)
230 struct wpa_supplicant *wpa_s = ctx;
231 printf("WPA: wpa_eapol_send(type=%d len=%d)\n", type, len);
232 if (type == IEEE802_1X_TYPE_EAP_PACKET) {
233 wpa_hexdump(MSG_DEBUG, "TX EAP -> RADIUS", buf, len);
234 ieee802_1x_encapsulate_radius(wpa_s, buf, len);
240 static void eapol_test_eapol_done_cb(void *ctx)
242 printf("WPA: EAPOL processing complete\n");
246 static void eapol_sm_reauth(void *eloop_ctx, void *timeout_ctx)
248 struct wpa_supplicant *wpa_s = eloop_ctx;
249 printf("\n\n\n\n\neapol_test: Triggering EAP reauthentication\n\n");
250 wpa_s->radius_access_accept_received = 0;
251 send_eap_request_identity(eloop_ctx, timeout_ctx);
255 static int eapol_test_compare_pmk(struct wpa_supplicant *wpa_s)
260 if (eapol_sm_get_key(wpa_s->eapol, pmk, PMK_LEN) == 0) {
261 wpa_hexdump(MSG_DEBUG, "PMK from EAPOL", pmk, PMK_LEN);
262 if (memcmp(pmk, wpa_s->authenticator_pmk, PMK_LEN) != 0)
263 printf("WARNING: PMK mismatch\n");
264 else if (wpa_s->radius_access_accept_received)
266 } else if (wpa_s->authenticator_pmk_len == 16 &&
267 eapol_sm_get_key(wpa_s->eapol, pmk, 16) == 0) {
268 wpa_hexdump(MSG_DEBUG, "LEAP PMK from EAPOL", pmk, 16);
269 if (memcmp(pmk, wpa_s->authenticator_pmk, 16) != 0)
270 printf("WARNING: PMK mismatch\n");
271 else if (wpa_s->radius_access_accept_received)
273 } else if (wpa_s->radius_access_accept_received && no_mppe_keys) {
274 /* No keying material expected */
280 else if (!no_mppe_keys)
287 static void eapol_sm_cb(struct eapol_sm *eapol, int success, void *ctx)
289 struct wpa_supplicant *wpa_s = ctx;
290 printf("eapol_sm_cb: success=%d\n", success);
291 eapol_test_num_reauths--;
292 if (eapol_test_num_reauths < 0)
295 eapol_test_compare_pmk(wpa_s);
296 eloop_register_timeout(0, 100000, eapol_sm_reauth,
302 static int test_eapol(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
304 struct eapol_config eapol_conf;
305 struct eapol_ctx *ctx;
307 ctx = malloc(sizeof(*ctx));
309 printf("Failed to allocate EAPOL context.\n");
312 memset(ctx, 0, sizeof(*ctx));
314 ctx->msg_ctx = wpa_s;
315 ctx->scard_ctx = wpa_s->scard;
316 ctx->cb = eapol_sm_cb;
319 ctx->eapol_done_cb = eapol_test_eapol_done_cb;
320 ctx->eapol_send = eapol_test_eapol_send;
322 wpa_s->eapol = eapol_sm_init(ctx);
323 if (wpa_s->eapol == NULL) {
325 printf("Failed to initialize EAPOL state machines.\n");
329 wpa_s->current_ssid = ssid;
330 memset(&eapol_conf, 0, sizeof(eapol_conf));
331 eapol_conf.accept_802_1x_keys = 1;
332 eapol_conf.required_keys = 0;
333 eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
334 eapol_conf.workaround = ssid->eap_workaround;
335 eapol_sm_notify_config(wpa_s->eapol, ssid, &eapol_conf);
336 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
339 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
340 /* 802.1X::portControl = Auto */
341 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
347 static void test_eapol_clean(struct wpa_supplicant *wpa_s)
349 radius_client_deinit(wpa_s);
350 free(wpa_s->last_eap_radius);
351 if (wpa_s->last_recv_radius) {
352 radius_msg_free(wpa_s->last_recv_radius);
353 free(wpa_s->last_recv_radius);
355 free(wpa_s->eap_identity);
356 wpa_s->eap_identity = NULL;
357 eapol_sm_deinit(wpa_s->eapol);
359 if (wpa_s->auth_server) {
360 free(wpa_s->auth_server->shared_secret);
361 free(wpa_s->auth_server);
363 scard_deinit(wpa_s->scard);
364 wpa_supplicant_ctrl_iface_deinit(wpa_s);
365 wpa_config_free(wpa_s->conf);
369 static void send_eap_request_identity(void *eloop_ctx, void *timeout_ctx)
371 struct wpa_supplicant *wpa_s = eloop_ctx;
373 struct ieee802_1x_hdr *hdr;
376 hdr = (struct ieee802_1x_hdr *) buf;
377 hdr->version = EAPOL_VERSION;
378 hdr->type = IEEE802_1X_TYPE_EAP_PACKET;
379 hdr->length = htons(5);
381 eap = (struct eap_hdr *) (hdr + 1);
382 eap->code = EAP_CODE_REQUEST;
384 eap->length = htons(5);
385 pos = (u8 *) (eap + 1);
386 *pos = EAP_TYPE_IDENTITY;
388 printf("Sending fake EAP-Request-Identity\n");
389 eapol_sm_rx_eapol(wpa_s->eapol, wpa_s->bssid, buf,
394 static void eapol_test_timeout(void *eloop_ctx, void *timeout_ctx)
396 struct wpa_supplicant *wpa_s = eloop_ctx;
397 printf("EAPOL test timed out\n");
398 wpa_s->auth_timed_out = 1;
403 static char *eap_type_text(u8 type)
406 case EAP_TYPE_IDENTITY: return "Identity";
407 case EAP_TYPE_NOTIFICATION: return "Notification";
408 case EAP_TYPE_NAK: return "Nak";
409 case EAP_TYPE_TLS: return "TLS";
410 case EAP_TYPE_TTLS: return "TTLS";
411 case EAP_TYPE_PEAP: return "PEAP";
412 case EAP_TYPE_SIM: return "SIM";
413 case EAP_TYPE_GTC: return "GTC";
414 case EAP_TYPE_MD5: return "MD5";
415 case EAP_TYPE_OTP: return "OTP";
416 default: return "Unknown";
421 static void ieee802_1x_decapsulate_radius(struct wpa_supplicant *wpa_s)
428 struct radius_msg *msg;
430 if (wpa_s->last_recv_radius == NULL)
433 msg = wpa_s->last_recv_radius;
435 eap = radius_msg_get_eap(msg, &len);
437 /* draft-aboba-radius-rfc2869bis-20.txt, Chap. 2.6.3:
438 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
440 wpa_printf(MSG_DEBUG, "could not extract "
441 "EAP-Message from RADIUS message");
442 free(wpa_s->last_eap_radius);
443 wpa_s->last_eap_radius = NULL;
444 wpa_s->last_eap_radius_len = 0;
448 if (len < sizeof(*hdr)) {
449 wpa_printf(MSG_DEBUG, "too short EAP packet "
450 "received from authentication server");
455 if (len > sizeof(*hdr))
456 eap_type = eap[sizeof(*hdr)];
458 hdr = (struct eap_hdr *) eap;
460 case EAP_CODE_REQUEST:
461 snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
462 eap_type >= 0 ? eap_type_text(eap_type) : "??",
465 case EAP_CODE_RESPONSE:
466 snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
467 eap_type >= 0 ? eap_type_text(eap_type) : "??",
470 case EAP_CODE_SUCCESS:
471 snprintf(buf, sizeof(buf), "EAP Success");
472 /* LEAP uses EAP Success within an authentication, so must not
473 * stop here with eloop_terminate(); */
475 case EAP_CODE_FAILURE:
476 snprintf(buf, sizeof(buf), "EAP Failure");
480 snprintf(buf, sizeof(buf), "unknown EAP code");
481 wpa_hexdump(MSG_DEBUG, "Decapsulated EAP packet", eap, len);
484 wpa_printf(MSG_DEBUG, "decapsulated EAP packet (code=%d "
485 "id=%d len=%d) from RADIUS server: %s",
486 hdr->code, hdr->identifier, ntohs(hdr->length), buf);
488 /* sta->eapol_sm->be_auth.idFromServer = hdr->identifier; */
490 if (wpa_s->last_eap_radius)
491 free(wpa_s->last_eap_radius);
492 wpa_s->last_eap_radius = eap;
493 wpa_s->last_eap_radius_len = len;
496 struct ieee802_1x_hdr *hdr;
497 hdr = malloc(sizeof(*hdr) + len);
499 hdr->version = EAPOL_VERSION;
500 hdr->type = IEEE802_1X_TYPE_EAP_PACKET;
501 hdr->length = htons(len);
502 memcpy((u8 *) (hdr + 1), eap, len);
503 eapol_sm_rx_eapol(wpa_s->eapol, wpa_s->bssid,
504 (u8 *) hdr, sizeof(*hdr) + len);
510 static void ieee802_1x_get_keys(struct wpa_supplicant *wpa_s,
511 struct radius_msg *msg, struct radius_msg *req,
512 u8 *shared_secret, size_t shared_secret_len)
514 struct radius_ms_mppe_keys *keys;
516 keys = radius_msg_get_ms_keys(msg, req, shared_secret,
518 if (keys && keys->send == NULL && keys->recv == NULL) {
520 keys = radius_msg_get_cisco_keys(msg, req, shared_secret,
526 wpa_hexdump(MSG_DEBUG, "MS-MPPE-Send-Key (sign)",
527 keys->send, keys->send_len);
530 wpa_hexdump(MSG_DEBUG, "MS-MPPE-Recv-Key (crypt)",
531 keys->recv, keys->recv_len);
532 wpa_s->authenticator_pmk_len =
533 keys->recv_len > PMK_LEN ? PMK_LEN :
535 memcpy(wpa_s->authenticator_pmk, keys->recv,
536 wpa_s->authenticator_pmk_len);
546 /* Process the RADIUS frames from Authentication Server */
547 static RadiusRxResult
548 ieee802_1x_receive_auth(struct wpa_supplicant *wpa_s,
549 struct radius_msg *msg, struct radius_msg *req,
550 u8 *shared_secret, size_t shared_secret_len,
553 /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
554 * present when packet contains an EAP-Message attribute */
555 if (msg->hdr->code == RADIUS_CODE_ACCESS_REJECT &&
556 radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
558 radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
559 wpa_printf(MSG_DEBUG, "Allowing RADIUS "
560 "Access-Reject without Message-Authenticator "
561 "since it does not include EAP-Message\n");
562 } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
564 printf("Incoming RADIUS packet did not have correct "
565 "Message-Authenticator - dropped\n");
566 return RADIUS_RX_UNKNOWN;
569 if (msg->hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
570 msg->hdr->code != RADIUS_CODE_ACCESS_REJECT &&
571 msg->hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
572 printf("Unknown RADIUS message code\n");
573 return RADIUS_RX_UNKNOWN;
576 wpa_s->radius_identifier = -1;
577 wpa_printf(MSG_DEBUG, "RADIUS packet matching with station");
579 if (wpa_s->last_recv_radius) {
580 radius_msg_free(wpa_s->last_recv_radius);
581 free(wpa_s->last_recv_radius);
584 wpa_s->last_recv_radius = msg;
586 switch (msg->hdr->code) {
587 case RADIUS_CODE_ACCESS_ACCEPT:
588 wpa_s->radius_access_accept_received = 1;
589 ieee802_1x_get_keys(wpa_s, msg, req, shared_secret,
592 case RADIUS_CODE_ACCESS_REJECT:
593 wpa_s->radius_access_reject_received = 1;
597 ieee802_1x_decapsulate_radius(wpa_s);
599 if ((msg->hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
600 eapol_test_num_reauths < 0) ||
601 msg->hdr->code == RADIUS_CODE_ACCESS_REJECT) {
605 return RADIUS_RX_QUEUED;
609 static void wpa_supplicant_imsi_identity(struct wpa_supplicant *wpa_s,
610 struct wpa_ssid *ssid)
613 u8 *pos = ssid->eap_methods;
615 while (pos && *pos != EAP_TYPE_NONE) {
616 if (*pos == EAP_TYPE_AKA) {
623 if (ssid->identity == NULL && wpa_s->imsi) {
624 ssid->identity = malloc(1 + wpa_s->imsi_len);
625 if (ssid->identity) {
626 ssid->identity[0] = aka ? '0' : '1';
627 memcpy(ssid->identity + 1, wpa_s->imsi,
629 ssid->identity_len = 1 + wpa_s->imsi_len;
630 wpa_hexdump_ascii(MSG_DEBUG, "permanent identity from "
631 "IMSI", ssid->identity,
638 static void wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
639 struct wpa_ssid *ssid)
644 if (ssid->pcsc == NULL)
646 if (wpa_s->scard != NULL) {
647 wpa_supplicant_imsi_identity(wpa_s, ssid);
650 wpa_printf(MSG_DEBUG, "Selected network is configured to use SIM - "
652 wpa_s->scard = scard_init(SCARD_TRY_BOTH, ssid->pin);
653 if (wpa_s->scard == NULL) {
654 wpa_printf(MSG_WARNING, "Failed to initialize SIM "
656 /* TODO: what to do here? */
659 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
662 if (scard_get_imsi(wpa_s->scard, buf, &len)) {
663 wpa_printf(MSG_WARNING, "Failed to get IMSI from SIM");
664 /* TODO: what to do here? */
668 wpa_hexdump(MSG_DEBUG, "IMSI", (u8 *) buf, len);
670 wpa_s->imsi = malloc(len);
672 memcpy(wpa_s->imsi, buf, len);
673 wpa_s->imsi_len = len;
674 wpa_supplicant_imsi_identity(wpa_s, ssid);
679 static void wpa_init_conf(struct wpa_supplicant *wpa_s, const char *authsrv,
680 int port, const char *secret)
682 struct hostapd_radius_server *as;
686 wpa_s->own_addr[5] = 2;
687 wpa_s->own_ip_addr.s_addr = htonl((127 << 24) | 1);
688 strncpy(wpa_s->ifname, "test", sizeof(wpa_s->ifname));
690 wpa_s->num_auth_servers = 1;
691 as = malloc(sizeof(struct hostapd_radius_server));
693 inet_aton(authsrv, &as->addr);
695 as->shared_secret = (u8 *) strdup(secret);
696 as->shared_secret_len = strlen(secret);
697 wpa_s->auth_server = wpa_s->auth_servers = as;
700 res = radius_client_init(wpa_s);
703 res = radius_client_register(wpa_s, RADIUS_AUTH,
704 ieee802_1x_receive_auth, NULL);
709 static int scard_test(void)
711 struct scard_data *scard;
714 unsigned char rand[16];
716 unsigned char sres[4];
718 #endif /* PCSC_FUNCS */
719 #define num_triplets 5
720 unsigned char rand_[num_triplets][16];
721 unsigned char sres_[num_triplets][4];
722 unsigned char kc_[num_triplets][8];
725 #define AKA_RAND_LEN 16
726 #define AKA_AUTN_LEN 16
727 #define AKA_AUTS_LEN 14
728 #define RES_MAX_LEN 16
731 unsigned char aka_rand[AKA_RAND_LEN];
732 unsigned char aka_autn[AKA_AUTN_LEN];
733 unsigned char aka_auts[AKA_AUTS_LEN];
734 unsigned char aka_res[RES_MAX_LEN];
736 unsigned char aka_ik[IK_LEN];
737 unsigned char aka_ck[CK_LEN];
739 scard = scard_init(SCARD_TRY_BOTH, "1234");
744 if (scard_get_imsi(scard, imsi, &len))
746 wpa_hexdump_ascii(MSG_DEBUG, "SCARD: IMSI", (u8 *) imsi, len);
747 /* NOTE: Permanent Username: 1 | IMSI */
749 memset(rand, 0, sizeof(rand));
750 if (scard_gsm_auth(scard, rand, sres, kc))
753 memset(rand, 0xff, sizeof(rand));
754 if (scard_gsm_auth(scard, rand, sres, kc))
757 for (i = 0; i < num_triplets; i++) {
758 memset(rand_[i], i, sizeof(rand_[i]));
759 if (scard_gsm_auth(scard, rand_[i], sres_[i], kc_[i]))
763 for (i = 0; i < num_triplets; i++) {
765 for (j = 0; j < len; j++)
766 printf("%c", imsi[j]);
768 for (j = 0; j < 16; j++)
769 printf("%02X", rand_[i][j]);
771 for (j = 0; j < 4; j++)
772 printf("%02X", sres_[i][j]);
774 for (j = 0; j < 8; j++)
775 printf("%02X", kc_[i][j]);
779 wpa_printf(MSG_DEBUG, "Trying to use UMTS authentication");
782 memset(aka_rand, 0xaa, 16);
783 memcpy(aka_autn, "\x86\x71\x31\xcb\xa2\xfc\x61\xdf"
784 "\xa3\xb3\x97\x9d\x07\x32\xa2\x12", 16);
786 res = scard_umts_auth(scard, aka_rand, aka_autn, aka_res, &aka_res_len,
787 aka_ik, aka_ck, aka_auts);
789 wpa_printf(MSG_DEBUG, "UMTS auth completed successfully");
790 wpa_hexdump(MSG_DEBUG, "RES", aka_res, aka_res_len);
791 wpa_hexdump(MSG_DEBUG, "IK", aka_ik, IK_LEN);
792 wpa_hexdump(MSG_DEBUG, "CK", aka_ck, CK_LEN);
793 } else if (res == -2) {
794 wpa_printf(MSG_DEBUG, "UMTS auth resulted in synchronization "
796 wpa_hexdump(MSG_DEBUG, "AUTS", aka_auts, AKA_AUTS_LEN);
798 wpa_printf(MSG_DEBUG, "UMTS auth failed");
809 static int scard_get_triplets(int argc, char *argv[])
811 struct scard_data *scard;
814 unsigned char rand[16];
815 unsigned char sres[4];
820 if (argc < 2 || ((num_triplets = atoi(argv[1])) <= 0)) {
821 printf("invalid parameters for sim command\n");
825 if (argc <= 2 || strcmp(argv[2], "debug") != 0) {
826 /* disable debug output */
827 wpa_debug_level = 99;
830 scard = scard_init(SCARD_GSM_SIM_ONLY, argv[0]);
832 printf("Failed to open smartcard connection\n");
837 if (scard_get_imsi(scard, imsi, &len)) {
842 for (i = 0; i < num_triplets; i++) {
843 memset(rand, i, sizeof(rand));
844 if (scard_gsm_auth(scard, rand, sres, kc))
847 /* IMSI:Kc:SRES:RAND */
848 for (j = 0; j < len; j++)
849 printf("%c", imsi[j]);
851 for (j = 0; j < 8; j++)
852 printf("%02X", kc[j]);
854 for (j = 0; j < 4; j++)
855 printf("%02X", sres[j]);
857 for (j = 0; j < 16; j++)
858 printf("%02X", rand[j]);
868 static void eapol_test_terminate(int sig, void *eloop_ctx,
871 struct wpa_supplicant *wpa_s = eloop_ctx;
872 wpa_msg(wpa_s, MSG_INFO, "Signal %d received - terminating", sig);
877 static void usage(void)
880 "eapol_test [-n] -c<conf> [-a<AS IP>] [-p<AS port>] "
881 "[-s<AS secret>] [-r<count>]\n"
883 "eapol_test sim <PIN> <num triplets> [debug]\n"
886 " -c<conf> = configuration file\n"
887 " -a<AS IP> = IP address of the authentication server, "
888 "default 127.0.0.1\n"
889 " -p<AS port> = UDP port of the authentication server, "
891 " -s<AS secret> = shared secret with the authentication "
892 "server, default 'radius'\n"
893 " -r<count> = number of re-authentications\n"
894 " -n = no MPPE keys expected\n");
898 int main(int argc, char *argv[])
900 struct wpa_supplicant wpa_s;
902 char *as_addr = "127.0.0.1";
904 char *as_secret = "radius";
908 wpa_debug_show_keys = 1;
911 c = getopt(argc, argv, "a:c:np:r:s:");
925 as_port = atoi(optarg);
928 eapol_test_num_reauths = atoi(optarg);
939 if (argc > optind && strcmp(argv[optind], "scard") == 0) {
943 if (argc > optind && strcmp(argv[optind], "sim") == 0) {
944 return scard_get_triplets(argc - optind - 1,
950 printf("Configuration file is required.\n");
956 memset(&wpa_s, 0, sizeof(wpa_s));
957 wpa_s.conf = wpa_config_read(conf);
958 if (wpa_s.conf == NULL) {
959 printf("Failed to parse configuration file '%s'.\n", conf);
962 if (wpa_s.conf->ssid == NULL) {
963 printf("No networks defined.\n");
967 wpa_init_conf(&wpa_s, as_addr, as_port, as_secret);
968 if (wpa_supplicant_ctrl_iface_init(&wpa_s)) {
969 printf("Failed to initialize control interface '%s'.\n"
970 "You may have another eapol_test process already "
971 "running or the file was\n"
972 "left by an unclean termination of eapol_test in "
973 "which case you will need\n"
974 "to manually remove this file before starting "
975 "eapol_test again.\n",
976 wpa_s.conf->ctrl_interface);
979 wpa_supplicant_scard_init(&wpa_s, wpa_s.conf->ssid);
981 if (test_eapol(&wpa_s, wpa_s.conf->ssid))
984 eloop_register_timeout(30, 0, eapol_test_timeout, &wpa_s, NULL);
985 eloop_register_timeout(0, 0, send_eap_request_identity, &wpa_s, NULL);
986 eloop_register_signal(SIGINT, eapol_test_terminate, NULL);
987 eloop_register_signal(SIGTERM, eapol_test_terminate, NULL);
988 eloop_register_signal(SIGHUP, eapol_test_terminate, NULL);
991 if (eapol_test_compare_pmk(&wpa_s) == 0)
993 if (wpa_s.auth_timed_out)
995 if (wpa_s.radius_access_reject_received)
998 test_eapol_clean(&wpa_s);
1002 printf("MPPE keys OK: %d mismatch: %d\n",
1003 num_mppe_ok, num_mppe_mismatch);
1004 if (num_mppe_mismatch)
1007 printf("FAILURE\n");
1009 printf("SUCCESS\n");