2 * WPA Supplicant - test code
3 * Copyright (c) 2003-2013, 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"
17 #include "common/version.h"
18 #include "crypto/tls.h"
20 #include "eapol_supp/eapol_supp_sm.h"
21 #include "eap_peer/eap.h"
22 #include "eap_server/eap_methods.h"
24 #include "utils/base64.h"
25 #include "rsn_supp/wpa.h"
26 #include "wpa_supplicant_i.h"
27 #include "radius/radius.h"
28 #include "radius/radius_client.h"
29 #include "common/wpa_ctrl.h"
30 #include "ctrl_iface.h"
31 #include "pcsc_funcs.h"
32 #include "wpas_glue.h"
35 const struct wpa_driver_ops *const 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;
54 struct radius_msg *last_recv_radius;
55 struct in_addr own_ip_addr;
56 struct radius_client_data *radius;
57 struct hostapd_radius_servers *radius_conf;
59 /* last received EAP Response from Authentication Server */
60 struct wpabuf *last_eap_radius;
62 u8 authenticator_pmk[PMK_LEN];
63 size_t authenticator_pmk_len;
64 u8 authenticator_eap_key_name[256];
65 size_t authenticator_eap_key_name_len;
66 int radius_access_accept_received;
67 int radius_access_reject_received;
71 size_t eap_identity_len;
74 u8 own_addr[ETH_ALEN];
75 struct extra_radius_attr *extra_attrs;
77 FILE *server_cert_file;
79 const char *pcsc_reader;
82 unsigned int ctrl_iface:1;
83 unsigned int id_req_sent:1;
86 static struct eapol_test_data eapol_test;
89 static void send_eap_request_identity(void *eloop_ctx, void *timeout_ctx);
92 static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
93 int level, const char *txt, size_t len)
96 wpa_printf(MSG_DEBUG, "STA " MACSTR ": %s\n",
99 wpa_printf(MSG_DEBUG, "%s", txt);
103 static int add_extra_attr(struct radius_msg *msg,
104 struct extra_radius_attr *attr)
109 char buf[RADIUS_MAX_ATTR_LEN + 1];
111 switch (attr->syntax) {
113 os_snprintf(buf, sizeof(buf), "%s", attr->data);
114 len = os_strlen(buf);
122 if (pos[0] == '0' && pos[1] == 'x')
124 len = os_strlen(pos);
125 if ((len & 1) || (len / 2) > RADIUS_MAX_ATTR_LEN) {
126 printf("Invalid extra attribute hexstring\n");
130 if (hexstr2bin(pos, (u8 *) buf, len) < 0) {
131 printf("Invalid extra attribute hexstring\n");
136 val = htonl(atoi(attr->data));
137 os_memcpy(buf, &val, 4);
141 printf("Incorrect extra attribute syntax specification\n");
145 if (!radius_msg_add_attr(msg, attr->type, (u8 *) buf, len)) {
146 printf("Could not add attribute %d\n", attr->type);
154 static int add_extra_attrs(struct radius_msg *msg,
155 struct extra_radius_attr *attrs)
157 struct extra_radius_attr *p;
158 for (p = attrs; p; p = p->next) {
159 if (add_extra_attr(msg, p) < 0)
166 static struct extra_radius_attr *
167 find_extra_attr(struct extra_radius_attr *attrs, u8 type)
169 struct extra_radius_attr *p;
170 for (p = attrs; p; p = p->next) {
178 static void ieee802_1x_encapsulate_radius(struct eapol_test_data *e,
179 const u8 *eap, size_t len)
181 struct radius_msg *msg;
182 char buf[RADIUS_MAX_ATTR_LEN + 1];
183 const struct eap_hdr *hdr;
186 wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS "
189 e->radius_identifier = radius_client_get_id(e->radius);
190 msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
191 e->radius_identifier);
193 printf("Could not create net RADIUS packet\n");
197 radius_msg_make_authenticator(msg);
199 hdr = (const struct eap_hdr *) eap;
200 pos = (const u8 *) (hdr + 1);
201 if (len > sizeof(*hdr) && hdr->code == EAP_CODE_RESPONSE &&
202 pos[0] == EAP_TYPE_IDENTITY) {
204 os_free(e->eap_identity);
205 e->eap_identity_len = len - sizeof(*hdr) - 1;
206 e->eap_identity = os_malloc(e->eap_identity_len);
207 if (e->eap_identity) {
208 os_memcpy(e->eap_identity, pos, e->eap_identity_len);
209 wpa_hexdump(MSG_DEBUG, "Learned identity from "
210 "EAP-Response-Identity",
211 e->eap_identity, e->eap_identity_len);
215 if (e->eap_identity &&
216 !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
217 e->eap_identity, e->eap_identity_len)) {
218 printf("Could not add User-Name\n");
222 if (e->req_eap_key_name &&
223 !radius_msg_add_attr(msg, RADIUS_ATTR_EAP_KEY_NAME, (u8 *) "\0",
225 printf("Could not add EAP-Key-Name\n");
229 if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_NAS_IP_ADDRESS) &&
230 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
231 (u8 *) &e->own_ip_addr, 4)) {
232 printf("Could not add NAS-IP-Address\n");
236 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
237 MAC2STR(e->wpa_s->own_addr));
238 if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_CALLING_STATION_ID)
240 !radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
241 (u8 *) buf, os_strlen(buf))) {
242 printf("Could not add Calling-Station-Id\n");
246 /* TODO: should probably check MTU from driver config; 2304 is max for
247 * IEEE 802.11, but use 1400 to avoid problems with too large packets
249 if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_FRAMED_MTU) &&
250 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
251 printf("Could not add Framed-MTU\n");
255 if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_NAS_PORT_TYPE) &&
256 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
257 RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
258 printf("Could not add NAS-Port-Type\n");
262 if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_SERVICE_TYPE) &&
263 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_SERVICE_TYPE,
264 RADIUS_SERVICE_TYPE_FRAMED)) {
265 printf("Could not add Service-Type\n");
269 os_snprintf(buf, sizeof(buf), "%s", e->connect_info);
270 if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_CONNECT_INFO) &&
271 !radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
272 (u8 *) buf, os_strlen(buf))) {
273 printf("Could not add Connect-Info\n");
277 if (add_extra_attrs(msg, e->extra_attrs) < 0)
280 if (eap && !radius_msg_add_eap(msg, eap, len)) {
281 printf("Could not add EAP-Message\n");
285 /* State attribute must be copied if and only if this packet is
286 * Access-Request reply to the previous Access-Challenge */
287 if (e->last_recv_radius &&
288 radius_msg_get_hdr(e->last_recv_radius)->code ==
289 RADIUS_CODE_ACCESS_CHALLENGE) {
290 int res = radius_msg_copy_attr(msg, e->last_recv_radius,
293 printf("Could not copy State attribute from previous "
294 "Access-Challenge\n");
298 wpa_printf(MSG_DEBUG, " Copied RADIUS State "
303 if (radius_client_send(e->radius, msg, RADIUS_AUTH, e->wpa_s->own_addr)
309 radius_msg_free(msg);
313 static int eapol_test_eapol_send(void *ctx, int type, const u8 *buf,
316 printf("WPA: eapol_test_eapol_send(type=%d len=%lu)\n",
317 type, (unsigned long) len);
318 if (type == IEEE802_1X_TYPE_EAP_PACKET) {
319 wpa_hexdump(MSG_DEBUG, "TX EAP -> RADIUS", buf, len);
320 ieee802_1x_encapsulate_radius(&eapol_test, buf, len);
326 static void eapol_test_set_config_blob(void *ctx,
327 struct wpa_config_blob *blob)
329 struct eapol_test_data *e = ctx;
330 wpa_config_set_blob(e->wpa_s->conf, blob);
334 static const struct wpa_config_blob *
335 eapol_test_get_config_blob(void *ctx, const char *name)
337 struct eapol_test_data *e = ctx;
338 return wpa_config_get_blob(e->wpa_s->conf, name);
342 static void eapol_test_eapol_done_cb(void *ctx)
344 struct eapol_test_data *e = ctx;
346 printf("WPA: EAPOL processing complete\n");
347 wpa_supplicant_cancel_auth_timeout(e->wpa_s);
348 wpa_supplicant_set_state(e->wpa_s, WPA_COMPLETED);
352 static void eapol_sm_reauth(void *eloop_ctx, void *timeout_ctx)
354 struct eapol_test_data *e = eloop_ctx;
355 printf("\n\n\n\n\neapol_test: Triggering EAP reauthentication\n\n");
356 e->radius_access_accept_received = 0;
357 send_eap_request_identity(e->wpa_s, NULL);
361 static int eapol_test_compare_pmk(struct eapol_test_data *e)
368 if (eapol_sm_get_key(e->wpa_s->eapol, pmk, PMK_LEN) == 0) {
369 wpa_hexdump(MSG_DEBUG, "PMK from EAPOL", pmk, PMK_LEN);
370 if (os_memcmp(pmk, e->authenticator_pmk, PMK_LEN) != 0) {
371 printf("WARNING: PMK mismatch\n");
372 wpa_hexdump(MSG_DEBUG, "PMK from AS",
373 e->authenticator_pmk, PMK_LEN);
374 } else if (e->radius_access_accept_received)
376 } else if (e->authenticator_pmk_len == 16 &&
377 eapol_sm_get_key(e->wpa_s->eapol, pmk, 16) == 0) {
378 wpa_hexdump(MSG_DEBUG, "LEAP PMK from EAPOL", pmk, 16);
379 if (os_memcmp(pmk, e->authenticator_pmk, 16) != 0) {
380 printf("WARNING: PMK mismatch\n");
381 wpa_hexdump(MSG_DEBUG, "PMK from AS",
382 e->authenticator_pmk, 16);
383 } else if (e->radius_access_accept_received)
385 } else if (e->radius_access_accept_received && e->no_mppe_keys) {
386 /* No keying material expected */
390 if (ret && !e->no_mppe_keys)
391 e->num_mppe_mismatch++;
392 else if (!e->no_mppe_keys)
395 sess_id = eapol_sm_get_session_id(e->wpa_s->eapol, &sess_id_len);
398 if (e->authenticator_eap_key_name_len == 0) {
399 wpa_printf(MSG_INFO, "No EAP-Key-Name received from server");
403 if (e->authenticator_eap_key_name_len != sess_id_len ||
404 os_memcmp(e->authenticator_eap_key_name, sess_id, sess_id_len) != 0)
407 "Locally derived EAP Session-Id does not match EAP-Key-Name from server");
408 wpa_hexdump(MSG_DEBUG, "EAP Session-Id", sess_id, sess_id_len);
409 wpa_hexdump(MSG_DEBUG, "EAP-Key-Name from server",
410 e->authenticator_eap_key_name,
411 e->authenticator_eap_key_name_len);
414 "Locally derived EAP Session-Id matches EAP-Key-Name from server");
421 static void eapol_sm_cb(struct eapol_sm *eapol, enum eapol_supp_result result,
424 struct eapol_test_data *e = ctx;
425 printf("eapol_sm_cb: result=%d\n", result);
429 e->eapol_test_num_reauths--;
430 if (e->eapol_test_num_reauths < 0)
433 eapol_test_compare_pmk(e);
434 eloop_register_timeout(0, 100000, eapol_sm_reauth, e, NULL);
439 static void eapol_test_write_cert(FILE *f, const char *subject,
440 const struct wpabuf *cert)
442 unsigned char *encoded;
444 encoded = base64_encode(wpabuf_head(cert), wpabuf_len(cert), NULL);
447 fprintf(f, "%s\n-----BEGIN CERTIFICATE-----\n%s"
448 "-----END CERTIFICATE-----\n\n", subject, encoded);
453 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
454 static void eapol_test_eap_param_needed(void *ctx, enum wpa_ctrl_req_type field,
455 const char *default_txt)
457 struct eapol_test_data *e = ctx;
458 struct wpa_supplicant *wpa_s = e->wpa_s;
459 struct wpa_ssid *ssid = wpa_s->current_ssid;
460 const char *field_name, *txt = NULL;
468 field_name = wpa_supplicant_ctrl_req_to_string(field, default_txt,
470 if (field_name == NULL) {
471 wpa_printf(MSG_WARNING, "Unhandled EAP param %d needed",
476 buflen = 100 + os_strlen(txt) + ssid->ssid_len;
477 buf = os_malloc(buflen);
480 len = os_snprintf(buf, buflen,
481 WPA_CTRL_REQ "%s-%d:%s needed for SSID ",
482 field_name, ssid->id, txt);
483 if (os_snprintf_error(buflen, len)) {
487 if (ssid->ssid && buflen > len + ssid->ssid_len) {
488 os_memcpy(buf + len, ssid->ssid, ssid->ssid_len);
489 len += ssid->ssid_len;
492 buf[buflen - 1] = '\0';
493 wpa_msg(wpa_s, MSG_INFO, "%s", buf);
496 #else /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
497 #define eapol_test_eap_param_needed NULL
498 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
501 static void eapol_test_cert_cb(void *ctx, struct tls_cert_data *cert,
502 const char *cert_hash)
504 struct eapol_test_data *e = ctx;
507 wpa_msg(e->wpa_s, MSG_INFO, WPA_EVENT_EAP_PEER_CERT
508 "depth=%d subject='%s'%s%s",
509 cert->depth, cert->subject,
510 cert_hash ? " hash=" : "",
511 cert_hash ? cert_hash : "");
515 size_t len = wpabuf_len(cert->cert) * 2 + 1;
516 cert_hex = os_malloc(len);
518 wpa_snprintf_hex(cert_hex, len, wpabuf_head(cert->cert),
519 wpabuf_len(cert->cert));
520 wpa_msg_ctrl(e->wpa_s, MSG_INFO,
521 WPA_EVENT_EAP_PEER_CERT
522 "depth=%d subject='%s' cert=%s",
523 cert->depth, cert->subject, cert_hex);
527 if (e->server_cert_file)
528 eapol_test_write_cert(e->server_cert_file,
529 cert->subject, cert->cert);
532 for (i = 0; i < cert->num_altsubject; i++)
533 wpa_msg(e->wpa_s, MSG_INFO, WPA_EVENT_EAP_PEER_ALT
534 "depth=%d %s", cert->depth, cert->altsubject[i]);
538 static void eapol_test_set_anon_id(void *ctx, const u8 *id, size_t len)
540 struct eapol_test_data *e = ctx;
541 struct wpa_supplicant *wpa_s = e->wpa_s;
545 wpa_hexdump_ascii(MSG_DEBUG, "EAP method updated anonymous_identity",
548 if (wpa_s->current_ssid == NULL)
552 if (wpa_config_set(wpa_s->current_ssid, "anonymous_identity",
556 str = os_malloc(len * 2 + 1);
559 wpa_snprintf_hex(str, len * 2 + 1, id, len);
560 res = wpa_config_set(wpa_s->current_ssid, "anonymous_identity",
569 static enum wpa_states eapol_test_get_state(void *ctx)
571 struct eapol_test_data *e = ctx;
572 struct wpa_supplicant *wpa_s = e->wpa_s;
574 return wpa_s->wpa_state;
578 static int test_eapol(struct eapol_test_data *e, struct wpa_supplicant *wpa_s,
579 struct wpa_ssid *ssid)
581 struct eapol_config eapol_conf;
582 struct eapol_ctx *ctx;
583 struct wpa_sm_ctx *wctx;
585 ctx = os_zalloc(sizeof(*ctx));
587 printf("Failed to allocate EAPOL context.\n");
591 ctx->msg_ctx = wpa_s;
592 ctx->scard_ctx = wpa_s->scard;
593 ctx->cb = eapol_sm_cb;
595 ctx->eapol_send_ctx = wpa_s;
597 ctx->eapol_done_cb = eapol_test_eapol_done_cb;
598 ctx->eapol_send = eapol_test_eapol_send;
599 ctx->set_config_blob = eapol_test_set_config_blob;
600 ctx->get_config_blob = eapol_test_get_config_blob;
601 ctx->opensc_engine_path = wpa_s->conf->opensc_engine_path;
602 ctx->pkcs11_engine_path = wpa_s->conf->pkcs11_engine_path;
603 ctx->pkcs11_module_path = wpa_s->conf->pkcs11_module_path;
604 ctx->openssl_ciphers = wpa_s->conf->openssl_ciphers;
605 ctx->eap_param_needed = eapol_test_eap_param_needed;
606 ctx->cert_cb = eapol_test_cert_cb;
608 ctx->set_anon_id = eapol_test_set_anon_id;
610 wpa_s->eapol = eapol_sm_init(ctx);
611 if (wpa_s->eapol == NULL) {
613 printf("Failed to initialize EAPOL state machines.\n");
617 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
618 wctx = os_zalloc(sizeof(*wctx));
624 wctx->msg_ctx = wpa_s;
625 wctx->get_state = eapol_test_get_state;
626 wpa_s->wpa = wpa_sm_init(wctx);
636 wpa_s->current_ssid = ssid;
637 os_memset(&eapol_conf, 0, sizeof(eapol_conf));
638 eapol_conf.accept_802_1x_keys = 1;
639 eapol_conf.required_keys = 0;
640 eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
641 eapol_conf.workaround = ssid->eap_workaround;
642 eapol_conf.external_sim = wpa_s->conf->external_sim;
643 eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
644 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
647 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
648 /* 802.1X::portControl = Auto */
649 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
655 static void test_eapol_clean(struct eapol_test_data *e,
656 struct wpa_supplicant *wpa_s)
658 struct extra_radius_attr *p, *prev;
660 wpa_sm_deinit(wpa_s->wpa);
662 radius_client_deinit(e->radius);
663 wpabuf_free(e->last_eap_radius);
664 radius_msg_free(e->last_recv_radius);
665 e->last_recv_radius = NULL;
666 os_free(e->eap_identity);
667 e->eap_identity = NULL;
668 eapol_sm_deinit(wpa_s->eapol);
670 if (e->radius_conf && e->radius_conf->auth_server) {
671 os_free(e->radius_conf->auth_server->shared_secret);
672 os_free(e->radius_conf->auth_server);
674 os_free(e->radius_conf);
675 e->radius_conf = NULL;
676 scard_deinit(wpa_s->scard);
677 if (wpa_s->ctrl_iface) {
678 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
679 wpa_s->ctrl_iface = NULL;
682 ext_password_deinit(wpa_s->ext_pw);
683 wpa_s->ext_pw = NULL;
685 wpa_config_free(wpa_s->conf);
696 static void send_eap_request_identity(void *eloop_ctx, void *timeout_ctx)
698 struct wpa_supplicant *wpa_s = eloop_ctx;
700 struct ieee802_1x_hdr *hdr;
703 hdr = (struct ieee802_1x_hdr *) buf;
704 hdr->version = EAPOL_VERSION;
705 hdr->type = IEEE802_1X_TYPE_EAP_PACKET;
706 hdr->length = htons(5);
708 eap = (struct eap_hdr *) (hdr + 1);
709 eap->code = EAP_CODE_REQUEST;
710 if (os_get_random((u8 *) &eap->identifier, sizeof(eap->identifier)) < 0)
711 eap->identifier = os_random() & 0xff;
712 eap->length = htons(5);
713 pos = (u8 *) (eap + 1);
714 *pos = EAP_TYPE_IDENTITY;
716 printf("Sending fake EAP-Request-Identity\n");
717 eapol_sm_rx_eapol(wpa_s->eapol, wpa_s->bssid, buf,
722 static void eapol_test_timeout(void *eloop_ctx, void *timeout_ctx)
724 struct eapol_test_data *e = eloop_ctx;
725 printf("EAPOL test timed out\n");
726 e->auth_timed_out = 1;
731 static char *eap_type_text(u8 type)
734 case EAP_TYPE_IDENTITY: return "Identity";
735 case EAP_TYPE_NOTIFICATION: return "Notification";
736 case EAP_TYPE_NAK: return "Nak";
737 case EAP_TYPE_TLS: return "TLS";
738 case EAP_TYPE_TTLS: return "TTLS";
739 case EAP_TYPE_PEAP: return "PEAP";
740 case EAP_TYPE_SIM: return "SIM";
741 case EAP_TYPE_GTC: return "GTC";
742 case EAP_TYPE_MD5: return "MD5";
743 case EAP_TYPE_OTP: return "OTP";
744 case EAP_TYPE_FAST: return "FAST";
745 case EAP_TYPE_SAKE: return "SAKE";
746 case EAP_TYPE_PSK: return "PSK";
747 default: return "Unknown";
752 static void ieee802_1x_decapsulate_radius(struct eapol_test_data *e)
755 const struct eap_hdr *hdr;
758 struct radius_msg *msg;
760 if (e->last_recv_radius == NULL)
763 msg = e->last_recv_radius;
765 eap = radius_msg_get_eap(msg);
767 /* draft-aboba-radius-rfc2869bis-20.txt, Chap. 2.6.3:
768 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
770 wpa_printf(MSG_DEBUG, "could not extract "
771 "EAP-Message from RADIUS message");
772 wpabuf_free(e->last_eap_radius);
773 e->last_eap_radius = NULL;
777 if (wpabuf_len(eap) < sizeof(*hdr)) {
778 wpa_printf(MSG_DEBUG, "too short EAP packet "
779 "received from authentication server");
784 if (wpabuf_len(eap) > sizeof(*hdr))
785 eap_type = (wpabuf_head_u8(eap))[sizeof(*hdr)];
787 hdr = wpabuf_head(eap);
789 case EAP_CODE_REQUEST:
790 os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
791 eap_type >= 0 ? eap_type_text(eap_type) : "??",
794 case EAP_CODE_RESPONSE:
795 os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
796 eap_type >= 0 ? eap_type_text(eap_type) : "??",
799 case EAP_CODE_SUCCESS:
800 os_strlcpy(buf, "EAP Success", sizeof(buf));
801 /* LEAP uses EAP Success within an authentication, so must not
802 * stop here with eloop_terminate(); */
804 case EAP_CODE_FAILURE:
805 os_strlcpy(buf, "EAP Failure", sizeof(buf));
811 os_strlcpy(buf, "unknown EAP code", sizeof(buf));
812 wpa_hexdump_buf(MSG_DEBUG, "Decapsulated EAP packet", eap);
815 wpa_printf(MSG_DEBUG, "decapsulated EAP packet (code=%d "
816 "id=%d len=%d) from RADIUS server: %s",
817 hdr->code, hdr->identifier, ntohs(hdr->length), buf);
819 /* sta->eapol_sm->be_auth.idFromServer = hdr->identifier; */
821 wpabuf_free(e->last_eap_radius);
822 e->last_eap_radius = eap;
825 struct ieee802_1x_hdr *dot1x;
826 dot1x = os_malloc(sizeof(*dot1x) + wpabuf_len(eap));
827 assert(dot1x != NULL);
828 dot1x->version = EAPOL_VERSION;
829 dot1x->type = IEEE802_1X_TYPE_EAP_PACKET;
830 dot1x->length = htons(wpabuf_len(eap));
831 os_memcpy((u8 *) (dot1x + 1), wpabuf_head(eap),
833 eapol_sm_rx_eapol(e->wpa_s->eapol, e->wpa_s->bssid,
835 sizeof(*dot1x) + wpabuf_len(eap));
841 static void ieee802_1x_get_keys(struct eapol_test_data *e,
842 struct radius_msg *msg, struct radius_msg *req,
843 const u8 *shared_secret,
844 size_t shared_secret_len)
846 struct radius_ms_mppe_keys *keys;
850 keys = radius_msg_get_ms_keys(msg, req, shared_secret,
852 if (keys && keys->send == NULL && keys->recv == NULL) {
854 keys = radius_msg_get_cisco_keys(msg, req, shared_secret,
860 wpa_hexdump(MSG_DEBUG, "MS-MPPE-Send-Key (sign)",
861 keys->send, keys->send_len);
864 wpa_hexdump(MSG_DEBUG, "MS-MPPE-Recv-Key (crypt)",
865 keys->recv, keys->recv_len);
866 e->authenticator_pmk_len =
867 keys->recv_len > PMK_LEN ? PMK_LEN :
869 os_memcpy(e->authenticator_pmk, keys->recv,
870 e->authenticator_pmk_len);
871 if (e->authenticator_pmk_len == 16 && keys->send &&
872 keys->send_len == 16) {
873 /* MS-CHAP-v2 derives 16 octet keys */
874 wpa_printf(MSG_DEBUG, "Use MS-MPPE-Send-Key "
875 "to extend PMK to 32 octets");
876 os_memcpy(e->authenticator_pmk +
877 e->authenticator_pmk_len,
878 keys->send, keys->send_len);
879 e->authenticator_pmk_len += keys->send_len;
888 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_EAP_KEY_NAME, &buf, &len,
890 os_memcpy(e->authenticator_eap_key_name, buf, len);
891 e->authenticator_eap_key_name_len = len;
893 e->authenticator_eap_key_name_len = 0;
898 /* Process the RADIUS frames from Authentication Server */
899 static RadiusRxResult
900 ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
901 const u8 *shared_secret, size_t shared_secret_len,
904 struct eapol_test_data *e = data;
905 struct radius_hdr *hdr = radius_msg_get_hdr(msg);
907 /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
908 * present when packet contains an EAP-Message attribute */
909 if (hdr->code == RADIUS_CODE_ACCESS_REJECT &&
910 radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
912 radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
913 wpa_printf(MSG_DEBUG, "Allowing RADIUS "
914 "Access-Reject without Message-Authenticator "
915 "since it does not include EAP-Message\n");
916 } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
918 printf("Incoming RADIUS packet did not have correct "
919 "Message-Authenticator - dropped\n");
920 return RADIUS_RX_UNKNOWN;
923 if (hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
924 hdr->code != RADIUS_CODE_ACCESS_REJECT &&
925 hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
926 printf("Unknown RADIUS message code\n");
927 return RADIUS_RX_UNKNOWN;
930 e->radius_identifier = -1;
931 wpa_printf(MSG_DEBUG, "RADIUS packet matching with station");
933 radius_msg_free(e->last_recv_radius);
934 e->last_recv_radius = msg;
937 case RADIUS_CODE_ACCESS_ACCEPT:
938 e->radius_access_accept_received = 1;
939 ieee802_1x_get_keys(e, msg, req, shared_secret,
942 case RADIUS_CODE_ACCESS_REJECT:
943 e->radius_access_reject_received = 1;
947 ieee802_1x_decapsulate_radius(e);
949 if ((hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
950 e->eapol_test_num_reauths < 0) ||
951 hdr->code == RADIUS_CODE_ACCESS_REJECT) {
956 return RADIUS_RX_QUEUED;
960 static int driver_get_ssid(void *priv, u8 *ssid)
967 static int driver_get_bssid(void *priv, u8 *bssid)
969 struct eapol_test_data *e = priv;
971 if (e->ctrl_iface && !e->id_req_sent) {
972 eloop_register_timeout(0, 0, send_eap_request_identity,
977 os_memset(bssid, 0, ETH_ALEN);
983 static int driver_get_capa(void *priv, struct wpa_driver_capa *capa)
985 os_memset(capa, 0, sizeof(*capa));
986 capa->flags = WPA_DRIVER_FLAGS_WIRED;
991 struct wpa_driver_ops eapol_test_drv_ops = {
993 .get_ssid = driver_get_ssid,
994 .get_bssid = driver_get_bssid,
995 .get_capa = driver_get_capa,
998 static void wpa_init_conf(struct eapol_test_data *e,
999 struct wpa_supplicant *wpa_s, const char *authsrv,
1000 int port, const char *secret,
1001 const char *cli_addr, const char *ifname)
1003 struct hostapd_radius_server *as;
1006 wpa_s->driver = &eapol_test_drv_ops;
1007 wpa_s->drv_priv = e;
1008 wpa_s->bssid[5] = 1;
1009 os_memcpy(wpa_s->own_addr, e->own_addr, ETH_ALEN);
1010 e->own_ip_addr.s_addr = htonl((127 << 24) | 1);
1011 os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
1013 e->radius_conf = os_zalloc(sizeof(struct hostapd_radius_servers));
1014 assert(e->radius_conf != NULL);
1015 e->radius_conf->num_auth_servers = 1;
1016 as = os_zalloc(sizeof(struct hostapd_radius_server));
1018 #if defined(CONFIG_NATIVE_WINDOWS) || defined(CONFIG_ANSI_C_EXTRA)
1022 sscanf(authsrv, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
1023 pos = (u8 *) &as->addr.u.v4;
1029 #else /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
1030 if (hostapd_parse_ip_addr(authsrv, &as->addr) < 0) {
1031 wpa_printf(MSG_ERROR, "Invalid IP address '%s'",
1035 #endif /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
1037 as->shared_secret = (u8 *) os_strdup(secret);
1038 as->shared_secret_len = os_strlen(secret);
1039 e->radius_conf->auth_server = as;
1040 e->radius_conf->auth_servers = as;
1041 e->radius_conf->msg_dumps = 1;
1043 if (hostapd_parse_ip_addr(cli_addr,
1044 &e->radius_conf->client_addr) == 0)
1045 e->radius_conf->force_client_addr = 1;
1047 wpa_printf(MSG_ERROR, "Invalid IP address '%s'",
1053 e->radius = radius_client_init(wpa_s, e->radius_conf);
1054 assert(e->radius != NULL);
1056 res = radius_client_register(e->radius, RADIUS_AUTH,
1057 ieee802_1x_receive_auth, e);
1062 static int scard_test(struct eapol_test_data *e)
1064 struct scard_data *scard;
1067 unsigned char _rand[16];
1069 unsigned char sres[4];
1070 unsigned char kc[8];
1071 #endif /* PCSC_FUNCS */
1072 #define num_triplets 5
1073 unsigned char rand_[num_triplets][16];
1074 unsigned char sres_[num_triplets][4];
1075 unsigned char kc_[num_triplets][8];
1079 #define AKA_RAND_LEN 16
1080 #define AKA_AUTN_LEN 16
1081 #define AKA_AUTS_LEN 14
1082 #define RES_MAX_LEN 16
1085 unsigned char aka_rand[AKA_RAND_LEN];
1086 unsigned char aka_autn[AKA_AUTN_LEN];
1087 unsigned char aka_auts[AKA_AUTS_LEN];
1088 unsigned char aka_res[RES_MAX_LEN];
1090 unsigned char aka_ik[IK_LEN];
1091 unsigned char aka_ck[CK_LEN];
1093 scard = scard_init(e->pcsc_reader);
1096 if (scard_set_pin(scard, e->pcsc_pin)) {
1097 wpa_printf(MSG_WARNING, "PIN validation failed");
1098 scard_deinit(scard);
1103 if (scard_get_imsi(scard, imsi, &len))
1105 wpa_hexdump_ascii(MSG_DEBUG, "SCARD: IMSI", (u8 *) imsi, len);
1106 /* NOTE: Permanent Username: 1 | IMSI */
1108 wpa_printf(MSG_DEBUG, "SCARD: MNC length %d",
1109 scard_get_mnc_len(scard));
1111 os_memset(_rand, 0, sizeof(_rand));
1112 if (scard_gsm_auth(scard, _rand, sres, kc))
1115 os_memset(_rand, 0xff, sizeof(_rand));
1116 if (scard_gsm_auth(scard, _rand, sres, kc))
1119 for (i = 0; i < num_triplets; i++) {
1120 os_memset(rand_[i], i, sizeof(rand_[i]));
1121 if (scard_gsm_auth(scard, rand_[i], sres_[i], kc_[i]))
1125 for (i = 0; i < num_triplets; i++) {
1127 for (j = 0; j < len; j++)
1128 printf("%c", imsi[j]);
1130 for (j = 0; j < 16; j++)
1131 printf("%02X", rand_[i][j]);
1133 for (j = 0; j < 4; j++)
1134 printf("%02X", sres_[i][j]);
1136 for (j = 0; j < 8; j++)
1137 printf("%02X", kc_[i][j]);
1141 wpa_printf(MSG_DEBUG, "Trying to use UMTS authentication");
1144 os_memset(aka_rand, 0xaa, 16);
1145 os_memcpy(aka_autn, "\x86\x71\x31\xcb\xa2\xfc\x61\xdf"
1146 "\xa3\xb3\x97\x9d\x07\x32\xa2\x12", 16);
1148 res = scard_umts_auth(scard, aka_rand, aka_autn, aka_res, &aka_res_len,
1149 aka_ik, aka_ck, aka_auts);
1151 wpa_printf(MSG_DEBUG, "UMTS auth completed successfully");
1152 wpa_hexdump(MSG_DEBUG, "RES", aka_res, aka_res_len);
1153 wpa_hexdump(MSG_DEBUG, "IK", aka_ik, IK_LEN);
1154 wpa_hexdump(MSG_DEBUG, "CK", aka_ck, CK_LEN);
1155 } else if (res == -2) {
1156 wpa_printf(MSG_DEBUG, "UMTS auth resulted in synchronization "
1158 wpa_hexdump(MSG_DEBUG, "AUTS", aka_auts, AKA_AUTS_LEN);
1160 wpa_printf(MSG_DEBUG, "UMTS auth failed");
1164 scard_deinit(scard);
1171 static int scard_get_triplets(struct eapol_test_data *e, int argc, char *argv[])
1173 struct scard_data *scard;
1176 unsigned char _rand[16];
1177 unsigned char sres[4];
1178 unsigned char kc[8];
1183 if (argc < 2 || ((num_triplets = atoi(argv[1])) <= 0)) {
1184 printf("invalid parameters for sim command\n");
1188 if (argc <= 2 || os_strcmp(argv[2], "debug") != 0) {
1189 /* disable debug output */
1190 wpa_debug_level = 99;
1193 scard = scard_init(e->pcsc_reader);
1194 if (scard == NULL) {
1195 printf("Failed to open smartcard connection\n");
1198 if (scard_set_pin(scard, argv[0])) {
1199 wpa_printf(MSG_WARNING, "PIN validation failed");
1200 scard_deinit(scard);
1205 if (scard_get_imsi(scard, imsi, &len)) {
1206 scard_deinit(scard);
1210 for (i = 0; i < num_triplets; i++) {
1211 os_memset(_rand, i, sizeof(_rand));
1212 if (scard_gsm_auth(scard, _rand, sres, kc))
1215 /* IMSI:Kc:SRES:RAND */
1216 for (j = 0; j < len; j++)
1217 printf("%c", imsi[j]);
1219 for (j = 0; j < 8; j++)
1220 printf("%02X", kc[j]);
1222 for (j = 0; j < 4; j++)
1223 printf("%02X", sres[j]);
1225 for (j = 0; j < 16; j++)
1226 printf("%02X", _rand[j]);
1230 scard_deinit(scard);
1236 static void eapol_test_terminate(int sig, void *signal_ctx)
1238 struct wpa_supplicant *wpa_s = signal_ctx;
1239 wpa_msg(wpa_s, MSG_INFO, "Signal %d received - terminating", sig);
1244 static void usage(void)
1247 "eapol_test [-enWSv] -c<conf> [-a<AS IP>] [-p<AS port>] "
1248 "[-s<AS secret>]\\\n"
1249 " [-r<count>] [-t<timeout>] [-C<Connect-Info>] \\\n"
1250 " [-M<client MAC address>] [-o<server cert file] \\\n"
1251 " [-N<attr spec>] [-R<PC/SC reader>] "
1252 "[-P<PC/SC PIN>] \\\n"
1253 " [-A<client IP>] [-i<ifname>] [-T<ctrl_iface>]\n"
1254 "eapol_test scard\n"
1255 "eapol_test sim <PIN> <num triplets> [debug]\n"
1258 " -c<conf> = configuration file\n"
1259 " -a<AS IP> = IP address of the authentication server, "
1260 "default 127.0.0.1\n"
1261 " -p<AS port> = UDP port of the authentication server, "
1263 " -s<AS secret> = shared secret with the authentication "
1264 "server, default 'radius'\n"
1265 " -A<client IP> = IP address of the client, default: select "
1267 " -r<count> = number of re-authentications\n"
1268 " -e = Request EAP-Key-Name\n"
1269 " -W = wait for a control interface monitor before starting\n"
1270 " -S = save configuration after authentication\n"
1271 " -n = no MPPE keys expected\n"
1272 " -v = show version\n"
1273 " -t<timeout> = sets timeout in seconds (default: 30 s)\n"
1274 " -C<Connect-Info> = RADIUS Connect-Info (default: "
1275 "CONNECT 11Mbps 802.11b)\n"
1276 " -M<client MAC address> = Set own MAC address "
1277 "(Calling-Station-Id,\n"
1278 " default: 02:00:00:00:00:01)\n"
1279 " -o<server cert file> = Write received server certificate\n"
1280 " chain to the specified file\n"
1281 " -N<attr spec> = send arbitrary attribute specified by:\n"
1282 " attr_id:syntax:value or attr_id\n"
1283 " attr_id - number id of the attribute\n"
1284 " syntax - one of: s, d, x\n"
1287 " x = octet string\n"
1288 " value - attribute value.\n"
1289 " When only attr_id is specified, NULL will be used as "
1291 " Multiple attributes can be specified by using the "
1292 "option several times.\n");
1296 int main(int argc, char *argv[])
1298 struct wpa_global global;
1299 struct wpa_supplicant wpa_s;
1300 int c, ret = 1, wait_for_monitor = 0, save_config = 0;
1301 char *as_addr = "127.0.0.1";
1303 char *as_secret = "radius";
1304 char *cli_addr = NULL;
1308 struct extra_radius_attr *p = NULL, *p1;
1309 const char *ifname = "test";
1310 const char *ctrl_iface = NULL;
1312 if (os_program_init())
1315 hostapd_logger_register_cb(hostapd_logger_cb);
1317 os_memset(&eapol_test, 0, sizeof(eapol_test));
1318 eapol_test.connect_info = "CONNECT 11Mbps 802.11b";
1319 os_memcpy(eapol_test.own_addr, "\x02\x00\x00\x00\x00\x01", ETH_ALEN);
1320 eapol_test.pcsc_pin = "1234";
1322 wpa_debug_level = 0;
1323 wpa_debug_show_keys = 1;
1326 c = getopt(argc, argv, "a:A:c:C:ei:M:nN:o:p:P:r:R:s:St:T:vW");
1340 eapol_test.connect_info = optarg;
1343 eapol_test.req_eap_key_name = 1;
1349 if (hwaddr_aton(optarg, eapol_test.own_addr)) {
1355 eapol_test.no_mppe_keys++;
1358 if (eapol_test.server_cert_file)
1359 fclose(eapol_test.server_cert_file);
1360 eapol_test.server_cert_file = fopen(optarg, "w");
1361 if (eapol_test.server_cert_file == NULL) {
1362 printf("Could not open '%s' for writing\n",
1368 as_port = atoi(optarg);
1371 eapol_test.pcsc_pin = optarg;
1374 eapol_test.eapol_test_num_reauths = atoi(optarg);
1377 eapol_test.pcsc_reader = optarg;
1386 timeout = atoi(optarg);
1389 ctrl_iface = optarg;
1390 eapol_test.ctrl_iface = 1;
1393 printf("eapol_test v" VERSION_STR "\n");
1399 p1 = os_zalloc(sizeof(*p1));
1403 eapol_test.extra_attrs = p1;
1408 p->type = atoi(optarg);
1409 pos = os_strchr(optarg, ':');
1417 if (pos[0] == '\0' || pos[1] != ':') {
1418 printf("Incorrect format of attribute "
1432 if (argc > optind && os_strcmp(argv[optind], "scard") == 0) {
1433 return scard_test(&eapol_test);
1436 if (argc > optind && os_strcmp(argv[optind], "sim") == 0) {
1437 return scard_get_triplets(&eapol_test, argc - optind - 1,
1441 if (conf == NULL && !ctrl_iface) {
1443 printf("Configuration file is required.\n");
1447 if (eap_register_methods()) {
1448 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
1453 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
1457 os_memset(&global, 0, sizeof(global));
1458 os_memset(&wpa_s, 0, sizeof(wpa_s));
1459 wpa_s.global = &global;
1460 eapol_test.wpa_s = &wpa_s;
1461 dl_list_init(&wpa_s.bss);
1462 dl_list_init(&wpa_s.bss_id);
1464 wpa_s.conf = wpa_config_read(conf, NULL);
1466 wpa_s.conf = wpa_config_alloc_empty(ctrl_iface, NULL);
1467 if (wpa_s.conf == NULL) {
1468 printf("Failed to parse configuration file '%s'.\n", conf);
1471 if (!ctrl_iface && wpa_s.conf->ssid == NULL) {
1472 printf("No networks defined.\n");
1476 if (eapol_test.pcsc_reader) {
1477 os_free(wpa_s.conf->pcsc_reader);
1478 wpa_s.conf->pcsc_reader = os_strdup(eapol_test.pcsc_reader);
1481 wpa_init_conf(&eapol_test, &wpa_s, as_addr, as_port, as_secret,
1483 wpa_s.ctrl_iface = wpa_supplicant_ctrl_iface_init(&wpa_s);
1484 if (wpa_s.ctrl_iface == NULL) {
1485 printf("Failed to initialize control interface '%s'.\n"
1486 "You may have another eapol_test process already "
1487 "running or the file was\n"
1488 "left by an unclean termination of eapol_test in "
1489 "which case you will need\n"
1490 "to manually remove this file before starting "
1491 "eapol_test again.\n",
1492 wpa_s.conf->ctrl_interface);
1495 if (wpa_s.conf->ssid &&
1496 wpa_supplicant_scard_init(&wpa_s, wpa_s.conf->ssid))
1499 if (test_eapol(&eapol_test, &wpa_s, wpa_s.conf->ssid))
1502 if (wpas_init_ext_pw(&wpa_s) < 0)
1505 if (wait_for_monitor)
1506 wpa_supplicant_ctrl_iface_wait(wpa_s.ctrl_iface);
1509 eloop_register_timeout(timeout, 0, eapol_test_timeout,
1511 eloop_register_timeout(0, 0, send_eap_request_identity, &wpa_s,
1514 eloop_register_signal_terminate(eapol_test_terminate, &wpa_s);
1515 eloop_register_signal_reconfig(eapol_test_terminate, &wpa_s);
1518 eloop_cancel_timeout(eapol_test_timeout, &eapol_test, NULL);
1519 eloop_cancel_timeout(eapol_sm_reauth, &eapol_test, NULL);
1521 if (eapol_test_compare_pmk(&eapol_test) == 0 ||
1522 eapol_test.no_mppe_keys)
1524 if (eapol_test.auth_timed_out)
1526 if (eapol_test.radius_access_reject_received)
1530 wpa_config_write(conf, wpa_s.conf);
1532 test_eapol_clean(&eapol_test, &wpa_s);
1534 eap_peer_unregister_methods();
1536 eap_server_unregister_methods();
1537 #endif /* CONFIG_AP */
1541 if (eapol_test.server_cert_file)
1542 fclose(eapol_test.server_cert_file);
1544 printf("MPPE keys OK: %d mismatch: %d\n",
1545 eapol_test.num_mppe_ok, eapol_test.num_mppe_mismatch);
1546 if (eapol_test.num_mppe_mismatch)
1549 printf("FAILURE\n");
1551 printf("SUCCESS\n");
1553 os_program_deinit();