2 * WPA Supplicant - test code
3 * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
8 * IEEE 802.1X Supplicant test code (to be used in place of wpa_supplicant.c.
9 * Not used in production version.
16 #include "utils/ext_password.h"
18 #include "eapol_supp/eapol_supp_sm.h"
19 #include "eap_peer/eap.h"
20 #include "eap_server/eap_methods.h"
22 #include "utils/base64.h"
23 #include "rsn_supp/wpa.h"
24 #include "wpa_supplicant_i.h"
25 #include "radius/radius.h"
26 #include "radius/radius_client.h"
27 #include "common/wpa_ctrl.h"
28 #include "ctrl_iface.h"
29 #include "pcsc_funcs.h"
32 extern int wpa_debug_level;
33 extern int wpa_debug_show_keys;
35 struct wpa_driver_ops *wpa_drivers[] = { NULL };
38 struct extra_radius_attr {
42 struct extra_radius_attr *next;
45 struct eapol_test_data {
46 struct wpa_supplicant *wpa_s;
48 int eapol_test_num_reauths;
50 int num_mppe_ok, num_mppe_mismatch;
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;
58 /* last received EAP Response from Authentication Server */
59 struct wpabuf *last_eap_radius;
61 u8 authenticator_pmk[PMK_LEN];
62 size_t authenticator_pmk_len;
63 int radius_access_accept_received;
64 int radius_access_reject_received;
68 size_t eap_identity_len;
71 u8 own_addr[ETH_ALEN];
72 struct extra_radius_attr *extra_attrs;
74 FILE *server_cert_file;
77 static struct eapol_test_data eapol_test;
80 static void send_eap_request_identity(void *eloop_ctx, void *timeout_ctx);
83 static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
84 int level, const char *txt, size_t len)
87 wpa_printf(MSG_DEBUG, "STA " MACSTR ": %s\n",
90 wpa_printf(MSG_DEBUG, "%s", txt);
94 static int add_extra_attr(struct radius_msg *msg,
95 struct extra_radius_attr *attr)
100 char buf[RADIUS_MAX_ATTR_LEN + 1];
102 switch (attr->syntax) {
104 os_snprintf(buf, sizeof(buf), "%s", attr->data);
105 len = os_strlen(buf);
113 if (pos[0] == '0' && pos[1] == 'x')
115 len = os_strlen(pos);
116 if ((len & 1) || (len / 2) > RADIUS_MAX_ATTR_LEN) {
117 printf("Invalid extra attribute hexstring\n");
121 if (hexstr2bin(pos, (u8 *) buf, len) < 0) {
122 printf("Invalid extra attribute hexstring\n");
127 val = htonl(atoi(attr->data));
128 os_memcpy(buf, &val, 4);
132 printf("Incorrect extra attribute syntax specification\n");
136 if (!radius_msg_add_attr(msg, attr->type, (u8 *) buf, len)) {
137 printf("Could not add attribute %d\n", attr->type);
145 static int add_extra_attrs(struct radius_msg *msg,
146 struct extra_radius_attr *attrs)
148 struct extra_radius_attr *p;
149 for (p = attrs; p; p = p->next) {
150 if (add_extra_attr(msg, p) < 0)
157 static struct extra_radius_attr *
158 find_extra_attr(struct extra_radius_attr *attrs, u8 type)
160 struct extra_radius_attr *p;
161 for (p = attrs; p; p = p->next) {
169 static void ieee802_1x_encapsulate_radius(struct eapol_test_data *e,
170 const u8 *eap, size_t len)
172 struct radius_msg *msg;
173 char buf[RADIUS_MAX_ATTR_LEN + 1];
174 const struct eap_hdr *hdr;
177 wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS "
180 e->radius_identifier = radius_client_get_id(e->radius);
181 msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
182 e->radius_identifier);
184 printf("Could not create net RADIUS packet\n");
188 radius_msg_make_authenticator(msg, (u8 *) e, sizeof(*e));
190 hdr = (const struct eap_hdr *) eap;
191 pos = (const u8 *) (hdr + 1);
192 if (len > sizeof(*hdr) && hdr->code == EAP_CODE_RESPONSE &&
193 pos[0] == EAP_TYPE_IDENTITY) {
195 os_free(e->eap_identity);
196 e->eap_identity_len = len - sizeof(*hdr) - 1;
197 e->eap_identity = os_malloc(e->eap_identity_len);
198 if (e->eap_identity) {
199 os_memcpy(e->eap_identity, pos, e->eap_identity_len);
200 wpa_hexdump(MSG_DEBUG, "Learned identity from "
201 "EAP-Response-Identity",
202 e->eap_identity, e->eap_identity_len);
206 if (e->eap_identity &&
207 !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
208 e->eap_identity, e->eap_identity_len)) {
209 printf("Could not add User-Name\n");
213 if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_NAS_IP_ADDRESS) &&
214 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
215 (u8 *) &e->own_ip_addr, 4)) {
216 printf("Could not add NAS-IP-Address\n");
220 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
221 MAC2STR(e->wpa_s->own_addr));
222 if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_CALLING_STATION_ID)
224 !radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
225 (u8 *) buf, os_strlen(buf))) {
226 printf("Could not add Calling-Station-Id\n");
230 /* TODO: should probably check MTU from driver config; 2304 is max for
231 * IEEE 802.11, but use 1400 to avoid problems with too large packets
233 if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_FRAMED_MTU) &&
234 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
235 printf("Could not add Framed-MTU\n");
239 if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_NAS_PORT_TYPE) &&
240 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
241 RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
242 printf("Could not add NAS-Port-Type\n");
246 os_snprintf(buf, sizeof(buf), "%s", e->connect_info);
247 if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_CONNECT_INFO) &&
248 !radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
249 (u8 *) buf, os_strlen(buf))) {
250 printf("Could not add Connect-Info\n");
254 if (add_extra_attrs(msg, e->extra_attrs) < 0)
257 if (eap && !radius_msg_add_eap(msg, eap, len)) {
258 printf("Could not add EAP-Message\n");
262 /* State attribute must be copied if and only if this packet is
263 * Access-Request reply to the previous Access-Challenge */
264 if (e->last_recv_radius &&
265 radius_msg_get_hdr(e->last_recv_radius)->code ==
266 RADIUS_CODE_ACCESS_CHALLENGE) {
267 int res = radius_msg_copy_attr(msg, e->last_recv_radius,
270 printf("Could not copy State attribute from previous "
271 "Access-Challenge\n");
275 wpa_printf(MSG_DEBUG, " Copied RADIUS State "
280 if (radius_client_send(e->radius, msg, RADIUS_AUTH, e->wpa_s->own_addr)
286 radius_msg_free(msg);
290 static int eapol_test_eapol_send(void *ctx, int type, const u8 *buf,
293 printf("WPA: eapol_test_eapol_send(type=%d len=%lu)\n",
294 type, (unsigned long) len);
295 if (type == IEEE802_1X_TYPE_EAP_PACKET) {
296 wpa_hexdump(MSG_DEBUG, "TX EAP -> RADIUS", buf, len);
297 ieee802_1x_encapsulate_radius(&eapol_test, buf, len);
303 static void eapol_test_set_config_blob(void *ctx,
304 struct wpa_config_blob *blob)
306 struct eapol_test_data *e = ctx;
307 wpa_config_set_blob(e->wpa_s->conf, blob);
311 static const struct wpa_config_blob *
312 eapol_test_get_config_blob(void *ctx, const char *name)
314 struct eapol_test_data *e = ctx;
315 return wpa_config_get_blob(e->wpa_s->conf, name);
319 static void eapol_test_eapol_done_cb(void *ctx)
321 printf("WPA: EAPOL processing complete\n");
325 static void eapol_sm_reauth(void *eloop_ctx, void *timeout_ctx)
327 struct eapol_test_data *e = eloop_ctx;
328 printf("\n\n\n\n\neapol_test: Triggering EAP reauthentication\n\n");
329 e->radius_access_accept_received = 0;
330 send_eap_request_identity(e->wpa_s, NULL);
334 static int eapol_test_compare_pmk(struct eapol_test_data *e)
339 if (eapol_sm_get_key(e->wpa_s->eapol, pmk, PMK_LEN) == 0) {
340 wpa_hexdump(MSG_DEBUG, "PMK from EAPOL", pmk, PMK_LEN);
341 if (os_memcmp(pmk, e->authenticator_pmk, PMK_LEN) != 0) {
342 printf("WARNING: PMK mismatch\n");
343 wpa_hexdump(MSG_DEBUG, "PMK from AS",
344 e->authenticator_pmk, PMK_LEN);
345 } else if (e->radius_access_accept_received)
347 } else if (e->authenticator_pmk_len == 16 &&
348 eapol_sm_get_key(e->wpa_s->eapol, pmk, 16) == 0) {
349 wpa_hexdump(MSG_DEBUG, "LEAP PMK from EAPOL", pmk, 16);
350 if (os_memcmp(pmk, e->authenticator_pmk, 16) != 0) {
351 printf("WARNING: PMK mismatch\n");
352 wpa_hexdump(MSG_DEBUG, "PMK from AS",
353 e->authenticator_pmk, 16);
354 } else if (e->radius_access_accept_received)
356 } else if (e->radius_access_accept_received && e->no_mppe_keys) {
357 /* No keying material expected */
361 if (ret && !e->no_mppe_keys)
362 e->num_mppe_mismatch++;
363 else if (!e->no_mppe_keys)
370 static void eapol_sm_cb(struct eapol_sm *eapol, int success, void *ctx)
372 struct eapol_test_data *e = ctx;
373 printf("eapol_sm_cb: success=%d\n", success);
374 e->eapol_test_num_reauths--;
375 if (e->eapol_test_num_reauths < 0)
378 eapol_test_compare_pmk(e);
379 eloop_register_timeout(0, 100000, eapol_sm_reauth, e, NULL);
384 static void eapol_test_write_cert(FILE *f, const char *subject,
385 const struct wpabuf *cert)
387 unsigned char *encoded;
389 encoded = base64_encode(wpabuf_head(cert), wpabuf_len(cert), NULL);
392 fprintf(f, "%s\n-----BEGIN CERTIFICATE-----\n%s"
393 "-----END CERTIFICATE-----\n\n", subject, encoded);
398 static void eapol_test_cert_cb(void *ctx, int depth, const char *subject,
399 const char *cert_hash,
400 const struct wpabuf *cert)
402 struct eapol_test_data *e = ctx;
404 wpa_msg(e->wpa_s, MSG_INFO, WPA_EVENT_EAP_PEER_CERT
405 "depth=%d subject='%s'%s%s",
407 cert_hash ? " hash=" : "",
408 cert_hash ? cert_hash : "");
412 size_t len = wpabuf_len(cert) * 2 + 1;
413 cert_hex = os_malloc(len);
415 wpa_snprintf_hex(cert_hex, len, wpabuf_head(cert),
417 wpa_msg_ctrl(e->wpa_s, MSG_INFO,
418 WPA_EVENT_EAP_PEER_CERT
419 "depth=%d subject='%s' cert=%s",
420 depth, subject, cert_hex);
424 if (e->server_cert_file)
425 eapol_test_write_cert(e->server_cert_file,
431 static void eapol_test_set_anon_id(void *ctx, const u8 *id, size_t len)
433 struct eapol_test_data *e = ctx;
434 struct wpa_supplicant *wpa_s = e->wpa_s;
438 wpa_hexdump_ascii(MSG_DEBUG, "EAP method updated anonymous_identity",
441 if (wpa_s->current_ssid == NULL)
445 if (wpa_config_set(wpa_s->current_ssid, "anonymous_identity",
449 str = os_malloc(len * 2 + 1);
452 wpa_snprintf_hex(str, len * 2 + 1, id, len);
453 res = wpa_config_set(wpa_s->current_ssid, "anonymous_identity",
462 static int test_eapol(struct eapol_test_data *e, struct wpa_supplicant *wpa_s,
463 struct wpa_ssid *ssid)
465 struct eapol_config eapol_conf;
466 struct eapol_ctx *ctx;
468 ctx = os_zalloc(sizeof(*ctx));
470 printf("Failed to allocate EAPOL context.\n");
474 ctx->msg_ctx = wpa_s;
475 ctx->scard_ctx = wpa_s->scard;
476 ctx->cb = eapol_sm_cb;
478 ctx->eapol_send_ctx = wpa_s;
480 ctx->eapol_done_cb = eapol_test_eapol_done_cb;
481 ctx->eapol_send = eapol_test_eapol_send;
482 ctx->set_config_blob = eapol_test_set_config_blob;
483 ctx->get_config_blob = eapol_test_get_config_blob;
484 ctx->opensc_engine_path = wpa_s->conf->opensc_engine_path;
485 ctx->pkcs11_engine_path = wpa_s->conf->pkcs11_engine_path;
486 ctx->pkcs11_module_path = wpa_s->conf->pkcs11_module_path;
487 ctx->cert_cb = eapol_test_cert_cb;
489 ctx->set_anon_id = eapol_test_set_anon_id;
491 wpa_s->eapol = eapol_sm_init(ctx);
492 if (wpa_s->eapol == NULL) {
494 printf("Failed to initialize EAPOL state machines.\n");
498 wpa_s->current_ssid = ssid;
499 os_memset(&eapol_conf, 0, sizeof(eapol_conf));
500 eapol_conf.accept_802_1x_keys = 1;
501 eapol_conf.required_keys = 0;
502 eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
503 eapol_conf.workaround = ssid->eap_workaround;
504 eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
505 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
508 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
509 /* 802.1X::portControl = Auto */
510 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
516 static void test_eapol_clean(struct eapol_test_data *e,
517 struct wpa_supplicant *wpa_s)
519 struct extra_radius_attr *p, *prev;
521 radius_client_deinit(e->radius);
522 wpabuf_free(e->last_eap_radius);
523 radius_msg_free(e->last_recv_radius);
524 e->last_recv_radius = NULL;
525 os_free(e->eap_identity);
526 e->eap_identity = NULL;
527 eapol_sm_deinit(wpa_s->eapol);
529 if (e->radius_conf && e->radius_conf->auth_server) {
530 os_free(e->radius_conf->auth_server->shared_secret);
531 os_free(e->radius_conf->auth_server);
533 os_free(e->radius_conf);
534 e->radius_conf = NULL;
535 scard_deinit(wpa_s->scard);
536 if (wpa_s->ctrl_iface) {
537 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
538 wpa_s->ctrl_iface = NULL;
541 ext_password_deinit(wpa_s->ext_pw);
542 wpa_s->ext_pw = NULL;
544 wpa_config_free(wpa_s->conf);
555 static void send_eap_request_identity(void *eloop_ctx, void *timeout_ctx)
557 struct wpa_supplicant *wpa_s = eloop_ctx;
559 struct ieee802_1x_hdr *hdr;
562 hdr = (struct ieee802_1x_hdr *) buf;
563 hdr->version = EAPOL_VERSION;
564 hdr->type = IEEE802_1X_TYPE_EAP_PACKET;
565 hdr->length = htons(5);
567 eap = (struct eap_hdr *) (hdr + 1);
568 eap->code = EAP_CODE_REQUEST;
570 eap->length = htons(5);
571 pos = (u8 *) (eap + 1);
572 *pos = EAP_TYPE_IDENTITY;
574 printf("Sending fake EAP-Request-Identity\n");
575 eapol_sm_rx_eapol(wpa_s->eapol, wpa_s->bssid, buf,
580 static void eapol_test_timeout(void *eloop_ctx, void *timeout_ctx)
582 struct eapol_test_data *e = eloop_ctx;
583 printf("EAPOL test timed out\n");
584 e->auth_timed_out = 1;
589 static char *eap_type_text(u8 type)
592 case EAP_TYPE_IDENTITY: return "Identity";
593 case EAP_TYPE_NOTIFICATION: return "Notification";
594 case EAP_TYPE_NAK: return "Nak";
595 case EAP_TYPE_TLS: return "TLS";
596 case EAP_TYPE_TTLS: return "TTLS";
597 case EAP_TYPE_PEAP: return "PEAP";
598 case EAP_TYPE_SIM: return "SIM";
599 case EAP_TYPE_GTC: return "GTC";
600 case EAP_TYPE_MD5: return "MD5";
601 case EAP_TYPE_OTP: return "OTP";
602 case EAP_TYPE_FAST: return "FAST";
603 case EAP_TYPE_SAKE: return "SAKE";
604 case EAP_TYPE_PSK: return "PSK";
605 default: return "Unknown";
610 static void ieee802_1x_decapsulate_radius(struct eapol_test_data *e)
613 const struct eap_hdr *hdr;
616 struct radius_msg *msg;
618 if (e->last_recv_radius == NULL)
621 msg = e->last_recv_radius;
623 eap = radius_msg_get_eap(msg);
625 /* draft-aboba-radius-rfc2869bis-20.txt, Chap. 2.6.3:
626 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
628 wpa_printf(MSG_DEBUG, "could not extract "
629 "EAP-Message from RADIUS message");
630 wpabuf_free(e->last_eap_radius);
631 e->last_eap_radius = NULL;
635 if (wpabuf_len(eap) < sizeof(*hdr)) {
636 wpa_printf(MSG_DEBUG, "too short EAP packet "
637 "received from authentication server");
642 if (wpabuf_len(eap) > sizeof(*hdr))
643 eap_type = (wpabuf_head_u8(eap))[sizeof(*hdr)];
645 hdr = wpabuf_head(eap);
647 case EAP_CODE_REQUEST:
648 os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
649 eap_type >= 0 ? eap_type_text(eap_type) : "??",
652 case EAP_CODE_RESPONSE:
653 os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
654 eap_type >= 0 ? eap_type_text(eap_type) : "??",
657 case EAP_CODE_SUCCESS:
658 os_strlcpy(buf, "EAP Success", sizeof(buf));
659 /* LEAP uses EAP Success within an authentication, so must not
660 * stop here with eloop_terminate(); */
662 case EAP_CODE_FAILURE:
663 os_strlcpy(buf, "EAP Failure", sizeof(buf));
667 os_strlcpy(buf, "unknown EAP code", sizeof(buf));
668 wpa_hexdump_buf(MSG_DEBUG, "Decapsulated EAP packet", eap);
671 wpa_printf(MSG_DEBUG, "decapsulated EAP packet (code=%d "
672 "id=%d len=%d) from RADIUS server: %s",
673 hdr->code, hdr->identifier, ntohs(hdr->length), buf);
675 /* sta->eapol_sm->be_auth.idFromServer = hdr->identifier; */
677 wpabuf_free(e->last_eap_radius);
678 e->last_eap_radius = eap;
681 struct ieee802_1x_hdr *dot1x;
682 dot1x = os_malloc(sizeof(*dot1x) + wpabuf_len(eap));
683 assert(dot1x != NULL);
684 dot1x->version = EAPOL_VERSION;
685 dot1x->type = IEEE802_1X_TYPE_EAP_PACKET;
686 dot1x->length = htons(wpabuf_len(eap));
687 os_memcpy((u8 *) (dot1x + 1), wpabuf_head(eap),
689 eapol_sm_rx_eapol(e->wpa_s->eapol, e->wpa_s->bssid,
691 sizeof(*dot1x) + wpabuf_len(eap));
697 static void ieee802_1x_get_keys(struct eapol_test_data *e,
698 struct radius_msg *msg, struct radius_msg *req,
699 const u8 *shared_secret,
700 size_t shared_secret_len)
702 struct radius_ms_mppe_keys *keys;
704 keys = radius_msg_get_ms_keys(msg, req, shared_secret,
706 if (keys && keys->send == NULL && keys->recv == NULL) {
708 keys = radius_msg_get_cisco_keys(msg, req, shared_secret,
714 wpa_hexdump(MSG_DEBUG, "MS-MPPE-Send-Key (sign)",
715 keys->send, keys->send_len);
718 wpa_hexdump(MSG_DEBUG, "MS-MPPE-Recv-Key (crypt)",
719 keys->recv, keys->recv_len);
720 e->authenticator_pmk_len =
721 keys->recv_len > PMK_LEN ? PMK_LEN :
723 os_memcpy(e->authenticator_pmk, keys->recv,
724 e->authenticator_pmk_len);
725 if (e->authenticator_pmk_len == 16 && keys->send &&
726 keys->send_len == 16) {
727 /* MS-CHAP-v2 derives 16 octet keys */
728 wpa_printf(MSG_DEBUG, "Use MS-MPPE-Send-Key "
729 "to extend PMK to 32 octets");
730 os_memcpy(e->authenticator_pmk +
731 e->authenticator_pmk_len,
732 keys->send, keys->send_len);
733 e->authenticator_pmk_len += keys->send_len;
744 /* Process the RADIUS frames from Authentication Server */
745 static RadiusRxResult
746 ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
747 const u8 *shared_secret, size_t shared_secret_len,
750 struct eapol_test_data *e = data;
751 struct radius_hdr *hdr = radius_msg_get_hdr(msg);
753 /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
754 * present when packet contains an EAP-Message attribute */
755 if (hdr->code == RADIUS_CODE_ACCESS_REJECT &&
756 radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
758 radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
759 wpa_printf(MSG_DEBUG, "Allowing RADIUS "
760 "Access-Reject without Message-Authenticator "
761 "since it does not include EAP-Message\n");
762 } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
764 printf("Incoming RADIUS packet did not have correct "
765 "Message-Authenticator - dropped\n");
766 return RADIUS_RX_UNKNOWN;
769 if (hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
770 hdr->code != RADIUS_CODE_ACCESS_REJECT &&
771 hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
772 printf("Unknown RADIUS message code\n");
773 return RADIUS_RX_UNKNOWN;
776 e->radius_identifier = -1;
777 wpa_printf(MSG_DEBUG, "RADIUS packet matching with station");
779 radius_msg_free(e->last_recv_radius);
780 e->last_recv_radius = msg;
783 case RADIUS_CODE_ACCESS_ACCEPT:
784 e->radius_access_accept_received = 1;
785 ieee802_1x_get_keys(e, msg, req, shared_secret,
788 case RADIUS_CODE_ACCESS_REJECT:
789 e->radius_access_reject_received = 1;
793 ieee802_1x_decapsulate_radius(e);
795 if ((hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
796 e->eapol_test_num_reauths < 0) ||
797 hdr->code == RADIUS_CODE_ACCESS_REJECT) {
801 return RADIUS_RX_QUEUED;
805 static void wpa_init_conf(struct eapol_test_data *e,
806 struct wpa_supplicant *wpa_s, const char *authsrv,
807 int port, const char *secret,
808 const char *cli_addr)
810 struct hostapd_radius_server *as;
814 os_memcpy(wpa_s->own_addr, e->own_addr, ETH_ALEN);
815 e->own_ip_addr.s_addr = htonl((127 << 24) | 1);
816 os_strlcpy(wpa_s->ifname, "test", sizeof(wpa_s->ifname));
818 e->radius_conf = os_zalloc(sizeof(struct hostapd_radius_servers));
819 assert(e->radius_conf != NULL);
820 e->radius_conf->num_auth_servers = 1;
821 as = os_zalloc(sizeof(struct hostapd_radius_server));
823 #if defined(CONFIG_NATIVE_WINDOWS) || defined(CONFIG_ANSI_C_EXTRA)
827 sscanf(authsrv, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
828 pos = (u8 *) &as->addr.u.v4;
834 #else /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
835 inet_aton(authsrv, &as->addr.u.v4);
836 #endif /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
837 as->addr.af = AF_INET;
839 as->shared_secret = (u8 *) os_strdup(secret);
840 as->shared_secret_len = os_strlen(secret);
841 e->radius_conf->auth_server = as;
842 e->radius_conf->auth_servers = as;
843 e->radius_conf->msg_dumps = 1;
845 if (hostapd_parse_ip_addr(cli_addr,
846 &e->radius_conf->client_addr) == 0)
847 e->radius_conf->force_client_addr = 1;
849 wpa_printf(MSG_ERROR, "Invalid IP address '%s'",
855 e->radius = radius_client_init(wpa_s, e->radius_conf);
856 assert(e->radius != NULL);
858 res = radius_client_register(e->radius, RADIUS_AUTH,
859 ieee802_1x_receive_auth, e);
864 static int scard_test(void)
866 struct scard_data *scard;
869 unsigned char _rand[16];
871 unsigned char sres[4];
873 #endif /* PCSC_FUNCS */
874 #define num_triplets 5
875 unsigned char rand_[num_triplets][16];
876 unsigned char sres_[num_triplets][4];
877 unsigned char kc_[num_triplets][8];
881 #define AKA_RAND_LEN 16
882 #define AKA_AUTN_LEN 16
883 #define AKA_AUTS_LEN 14
884 #define RES_MAX_LEN 16
887 unsigned char aka_rand[AKA_RAND_LEN];
888 unsigned char aka_autn[AKA_AUTN_LEN];
889 unsigned char aka_auts[AKA_AUTS_LEN];
890 unsigned char aka_res[RES_MAX_LEN];
892 unsigned char aka_ik[IK_LEN];
893 unsigned char aka_ck[CK_LEN];
895 scard = scard_init(SCARD_TRY_BOTH, NULL);
898 if (scard_set_pin(scard, "1234")) {
899 wpa_printf(MSG_WARNING, "PIN validation failed");
905 if (scard_get_imsi(scard, imsi, &len))
907 wpa_hexdump_ascii(MSG_DEBUG, "SCARD: IMSI", (u8 *) imsi, len);
908 /* NOTE: Permanent Username: 1 | IMSI */
910 wpa_printf(MSG_DEBUG, "SCARD: MNC length %d",
911 scard_get_mnc_len(scard));
913 os_memset(_rand, 0, sizeof(_rand));
914 if (scard_gsm_auth(scard, _rand, sres, kc))
917 os_memset(_rand, 0xff, sizeof(_rand));
918 if (scard_gsm_auth(scard, _rand, sres, kc))
921 for (i = 0; i < num_triplets; i++) {
922 os_memset(rand_[i], i, sizeof(rand_[i]));
923 if (scard_gsm_auth(scard, rand_[i], sres_[i], kc_[i]))
927 for (i = 0; i < num_triplets; i++) {
929 for (j = 0; j < len; j++)
930 printf("%c", imsi[j]);
932 for (j = 0; j < 16; j++)
933 printf("%02X", rand_[i][j]);
935 for (j = 0; j < 4; j++)
936 printf("%02X", sres_[i][j]);
938 for (j = 0; j < 8; j++)
939 printf("%02X", kc_[i][j]);
943 wpa_printf(MSG_DEBUG, "Trying to use UMTS authentication");
946 os_memset(aka_rand, 0xaa, 16);
947 os_memcpy(aka_autn, "\x86\x71\x31\xcb\xa2\xfc\x61\xdf"
948 "\xa3\xb3\x97\x9d\x07\x32\xa2\x12", 16);
950 res = scard_umts_auth(scard, aka_rand, aka_autn, aka_res, &aka_res_len,
951 aka_ik, aka_ck, aka_auts);
953 wpa_printf(MSG_DEBUG, "UMTS auth completed successfully");
954 wpa_hexdump(MSG_DEBUG, "RES", aka_res, aka_res_len);
955 wpa_hexdump(MSG_DEBUG, "IK", aka_ik, IK_LEN);
956 wpa_hexdump(MSG_DEBUG, "CK", aka_ck, CK_LEN);
957 } else if (res == -2) {
958 wpa_printf(MSG_DEBUG, "UMTS auth resulted in synchronization "
960 wpa_hexdump(MSG_DEBUG, "AUTS", aka_auts, AKA_AUTS_LEN);
962 wpa_printf(MSG_DEBUG, "UMTS auth failed");
973 static int scard_get_triplets(int argc, char *argv[])
975 struct scard_data *scard;
978 unsigned char _rand[16];
979 unsigned char sres[4];
985 if (argc < 2 || ((num_triplets = atoi(argv[1])) <= 0)) {
986 printf("invalid parameters for sim command\n");
990 if (argc <= 2 || os_strcmp(argv[2], "debug") != 0) {
991 /* disable debug output */
992 wpa_debug_level = 99;
995 scard = scard_init(SCARD_GSM_SIM_ONLY, NULL);
997 printf("Failed to open smartcard connection\n");
1000 if (scard_set_pin(scard, argv[0])) {
1001 wpa_printf(MSG_WARNING, "PIN validation failed");
1002 scard_deinit(scard);
1007 if (scard_get_imsi(scard, imsi, &len)) {
1008 scard_deinit(scard);
1012 for (i = 0; i < num_triplets; i++) {
1013 os_memset(_rand, i, sizeof(_rand));
1014 if (scard_gsm_auth(scard, _rand, sres, kc))
1017 /* IMSI:Kc:SRES:RAND */
1018 for (j = 0; j < len; j++)
1019 printf("%c", imsi[j]);
1021 for (j = 0; j < 8; j++)
1022 printf("%02X", kc[j]);
1024 for (j = 0; j < 4; j++)
1025 printf("%02X", sres[j]);
1027 for (j = 0; j < 16; j++)
1028 printf("%02X", _rand[j]);
1032 scard_deinit(scard);
1038 static void eapol_test_terminate(int sig, void *signal_ctx)
1040 struct wpa_supplicant *wpa_s = signal_ctx;
1041 wpa_msg(wpa_s, MSG_INFO, "Signal %d received - terminating", sig);
1046 static void usage(void)
1049 "eapol_test [-nWS] -c<conf> [-a<AS IP>] [-p<AS port>] "
1050 "[-s<AS secret>]\\\n"
1051 " [-r<count>] [-t<timeout>] [-C<Connect-Info>] \\\n"
1052 " [-M<client MAC address>] [-o<server cert file] \\\n"
1053 " [-N<attr spec>] \\\n"
1054 " [-A<client IP>]\n"
1055 "eapol_test scard\n"
1056 "eapol_test sim <PIN> <num triplets> [debug]\n"
1059 " -c<conf> = configuration file\n"
1060 " -a<AS IP> = IP address of the authentication server, "
1061 "default 127.0.0.1\n"
1062 " -p<AS port> = UDP port of the authentication server, "
1064 " -s<AS secret> = shared secret with the authentication "
1065 "server, default 'radius'\n"
1066 " -A<client IP> = IP address of the client, default: select "
1068 " -r<count> = number of re-authentications\n"
1069 " -W = wait for a control interface monitor before starting\n"
1070 " -S = save configuration after authentication\n"
1071 " -n = no MPPE keys expected\n"
1072 " -t<timeout> = sets timeout in seconds (default: 30 s)\n"
1073 " -C<Connect-Info> = RADIUS Connect-Info (default: "
1074 "CONNECT 11Mbps 802.11b)\n"
1075 " -M<client MAC address> = Set own MAC address "
1076 "(Calling-Station-Id,\n"
1077 " default: 02:00:00:00:00:01)\n"
1078 " -o<server cert file> = Write received server certificate\n"
1079 " chain to the specified file\n"
1080 " -N<attr spec> = send arbitrary attribute specified by:\n"
1081 " attr_id:syntax:value or attr_id\n"
1082 " attr_id - number id of the attribute\n"
1083 " syntax - one of: s, d, x\n"
1086 " x = octet string\n"
1087 " value - attribute value.\n"
1088 " When only attr_id is specified, NULL will be used as "
1090 " Multiple attributes can be specified by using the "
1091 "option several times.\n");
1095 int main(int argc, char *argv[])
1097 struct wpa_global global;
1098 struct wpa_supplicant wpa_s;
1099 int c, ret = 1, wait_for_monitor = 0, save_config = 0;
1100 char *as_addr = "127.0.0.1";
1102 char *as_secret = "radius";
1103 char *cli_addr = NULL;
1107 struct extra_radius_attr *p = NULL, *p1;
1109 if (os_program_init())
1112 hostapd_logger_register_cb(hostapd_logger_cb);
1114 os_memset(&eapol_test, 0, sizeof(eapol_test));
1115 eapol_test.connect_info = "CONNECT 11Mbps 802.11b";
1116 os_memcpy(eapol_test.own_addr, "\x02\x00\x00\x00\x00\x01", ETH_ALEN);
1118 wpa_debug_level = 0;
1119 wpa_debug_show_keys = 1;
1122 c = getopt(argc, argv, "a:A:c:C:M:nN:o:p:r:s:St:W");
1136 eapol_test.connect_info = optarg;
1139 if (hwaddr_aton(optarg, eapol_test.own_addr)) {
1145 eapol_test.no_mppe_keys++;
1148 if (eapol_test.server_cert_file)
1149 fclose(eapol_test.server_cert_file);
1150 eapol_test.server_cert_file = fopen(optarg, "w");
1151 if (eapol_test.server_cert_file == NULL) {
1152 printf("Could not open '%s' for writing\n",
1158 as_port = atoi(optarg);
1161 eapol_test.eapol_test_num_reauths = atoi(optarg);
1170 timeout = atoi(optarg);
1176 p1 = os_zalloc(sizeof(*p1));
1180 eapol_test.extra_attrs = p1;
1185 p->type = atoi(optarg);
1186 pos = os_strchr(optarg, ':');
1194 if (pos[0] == '\0' || pos[1] != ':') {
1195 printf("Incorrect format of attribute "
1209 if (argc > optind && os_strcmp(argv[optind], "scard") == 0) {
1210 return scard_test();
1213 if (argc > optind && os_strcmp(argv[optind], "sim") == 0) {
1214 return scard_get_triplets(argc - optind - 1,
1220 printf("Configuration file is required.\n");
1224 if (eap_register_methods()) {
1225 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
1230 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
1234 os_memset(&global, 0, sizeof(global));
1235 os_memset(&wpa_s, 0, sizeof(wpa_s));
1236 wpa_s.global = &global;
1237 eapol_test.wpa_s = &wpa_s;
1238 wpa_s.conf = wpa_config_read(conf, NULL);
1239 if (wpa_s.conf == NULL) {
1240 printf("Failed to parse configuration file '%s'.\n", conf);
1243 if (wpa_s.conf->ssid == NULL) {
1244 printf("No networks defined.\n");
1248 wpa_init_conf(&eapol_test, &wpa_s, as_addr, as_port, as_secret,
1250 wpa_s.ctrl_iface = wpa_supplicant_ctrl_iface_init(&wpa_s);
1251 if (wpa_s.ctrl_iface == NULL) {
1252 printf("Failed to initialize control interface '%s'.\n"
1253 "You may have another eapol_test process already "
1254 "running or the file was\n"
1255 "left by an unclean termination of eapol_test in "
1256 "which case you will need\n"
1257 "to manually remove this file before starting "
1258 "eapol_test again.\n",
1259 wpa_s.conf->ctrl_interface);
1262 if (wpa_supplicant_scard_init(&wpa_s, wpa_s.conf->ssid))
1265 if (test_eapol(&eapol_test, &wpa_s, wpa_s.conf->ssid))
1268 if (wpas_init_ext_pw(&wpa_s) < 0)
1271 if (wait_for_monitor)
1272 wpa_supplicant_ctrl_iface_wait(wpa_s.ctrl_iface);
1274 eloop_register_timeout(timeout, 0, eapol_test_timeout, &eapol_test,
1276 eloop_register_timeout(0, 0, send_eap_request_identity, &wpa_s, NULL);
1277 eloop_register_signal_terminate(eapol_test_terminate, &wpa_s);
1278 eloop_register_signal_reconfig(eapol_test_terminate, &wpa_s);
1281 eloop_cancel_timeout(eapol_test_timeout, &eapol_test, NULL);
1282 eloop_cancel_timeout(eapol_sm_reauth, &eapol_test, NULL);
1284 if (eapol_test_compare_pmk(&eapol_test) == 0 ||
1285 eapol_test.no_mppe_keys)
1287 if (eapol_test.auth_timed_out)
1289 if (eapol_test.radius_access_reject_received)
1293 wpa_config_write(conf, wpa_s.conf);
1295 test_eapol_clean(&eapol_test, &wpa_s);
1297 eap_peer_unregister_methods();
1299 eap_server_unregister_methods();
1300 #endif /* CONFIG_AP */
1304 if (eapol_test.server_cert_file)
1305 fclose(eapol_test.server_cert_file);
1307 printf("MPPE keys OK: %d mismatch: %d\n",
1308 eapol_test.num_mppe_ok, eapol_test.num_mppe_mismatch);
1309 if (eapol_test.num_mppe_mismatch)
1312 printf("FAILURE\n");
1314 printf("SUCCESS\n");
1316 os_program_deinit();