SCARD: Clean up SIM/USIM selection
[mech_eap.git] / wpa_supplicant / eapol_test.c
index ecb3591..2b25b69 100644 (file)
@@ -1,15 +1,9 @@
 /*
  * WPA Supplicant - test code
- * Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi>
+ * Copyright (c) 2003-2013, Jouni Malinen <j@w1.fi>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation.
- *
- * Alternatively, this software may be distributed under the terms of BSD
- * license.
- *
- * See README and COPYING for more details.
+ * This software may be distributed under the terms of the BSD license.
+ * See README for more details.
  *
  * IEEE 802.1X Supplicant test code (to be used in place of wpa_supplicant.c.
  * Not used in production version.
 #include <assert.h>
 
 #include "common.h"
+#include "utils/ext_password.h"
 #include "config.h"
 #include "eapol_supp/eapol_supp_sm.h"
 #include "eap_peer/eap.h"
+#include "eap_server/eap_methods.h"
 #include "eloop.h"
-#include "wpa.h"
-#include "eap_peer/eap_i.h"
+#include "utils/base64.h"
+#include "rsn_supp/wpa.h"
 #include "wpa_supplicant_i.h"
 #include "radius/radius.h"
 #include "radius/radius_client.h"
+#include "common/wpa_ctrl.h"
 #include "ctrl_iface.h"
 #include "pcsc_funcs.h"
+#include "wpas_glue.h"
 
 
 extern int wpa_debug_level;
 extern int wpa_debug_show_keys;
 
-struct wpa_driver_ops *wpa_supplicant_drivers[] = { NULL };
+struct wpa_driver_ops *wpa_drivers[] = { NULL };
 
 
+struct extra_radius_attr {
+       u8 type;
+       char syntax;
+       char *data;
+       struct extra_radius_attr *next;
+};
+
 struct eapol_test_data {
        struct wpa_supplicant *wpa_s;
 
@@ -51,9 +56,8 @@ struct eapol_test_data {
        struct radius_client_data *radius;
        struct hostapd_radius_servers *radius_conf;
 
-       u8 *last_eap_radius; /* last received EAP Response from Authentication
-                             * Server */
-       size_t last_eap_radius_len;
+        /* last received EAP Response from Authentication Server */
+       struct wpabuf *last_eap_radius;
 
        u8 authenticator_pmk[PMK_LEN];
        size_t authenticator_pmk_len;
@@ -66,8 +70,9 @@ struct eapol_test_data {
 
        char *connect_info;
        u8 own_addr[ETH_ALEN];
-       int cui_flag;
-       char *cui_str;
+       struct extra_radius_attr *extra_attrs;
+
+       FILE *server_cert_file;
 };
 
 static struct eapol_test_data eapol_test;
@@ -87,11 +92,86 @@ static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
 }
 
 
+static int add_extra_attr(struct radius_msg *msg,
+                         struct extra_radius_attr *attr)
+{
+       size_t len;
+       char *pos;
+       u32 val;
+       char buf[RADIUS_MAX_ATTR_LEN + 1];
+
+       switch (attr->syntax) {
+       case 's':
+               os_snprintf(buf, sizeof(buf), "%s", attr->data);
+               len = os_strlen(buf);
+               break;
+       case 'n':
+               buf[0] = '\0';
+               len = 1;
+               break;
+       case 'x':
+               pos = attr->data;
+               if (pos[0] == '0' && pos[1] == 'x')
+                       pos += 2;
+               len = os_strlen(pos);
+               if ((len & 1) || (len / 2) > RADIUS_MAX_ATTR_LEN) {
+                       printf("Invalid extra attribute hexstring\n");
+                       return -1;
+               }
+               len /= 2;
+               if (hexstr2bin(pos, (u8 *) buf, len) < 0) {
+                       printf("Invalid extra attribute hexstring\n");
+                       return -1;
+               }
+               break;
+       case 'd':
+               val = htonl(atoi(attr->data));
+               os_memcpy(buf, &val, 4);
+               len = 4;
+               break;
+       default:
+               printf("Incorrect extra attribute syntax specification\n");
+               return -1;
+       }
+
+       if (!radius_msg_add_attr(msg, attr->type, (u8 *) buf, len)) {
+               printf("Could not add attribute %d\n", attr->type);
+               return -1;
+       }
+
+       return 0;
+}
+
+
+static int add_extra_attrs(struct radius_msg *msg,
+                          struct extra_radius_attr *attrs)
+{
+       struct extra_radius_attr *p;
+       for (p = attrs; p; p = p->next) {
+               if (add_extra_attr(msg, p) < 0)
+                       return -1;
+       }
+       return 0;
+}
+
+
+static struct extra_radius_attr *
+find_extra_attr(struct extra_radius_attr *attrs, u8 type)
+{
+       struct extra_radius_attr *p;
+       for (p = attrs; p; p = p->next) {
+               if (p->type == type)
+                       return p;
+       }
+       return NULL;
+}
+
+
 static void ieee802_1x_encapsulate_radius(struct eapol_test_data *e,
                                          const u8 *eap, size_t len)
 {
        struct radius_msg *msg;
-       char buf[128];
+       char buf[RADIUS_MAX_ATTR_LEN + 1];
        const struct eap_hdr *hdr;
        const u8 *pos;
 
@@ -131,7 +211,8 @@ static void ieee802_1x_encapsulate_radius(struct eapol_test_data *e,
                goto fail;
        }
 
-       if (!radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
+       if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_NAS_IP_ADDRESS) &&
+           !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
                                 (u8 *) &e->own_ip_addr, 4)) {
                printf("Could not add NAS-IP-Address\n");
                goto fail;
@@ -139,7 +220,9 @@ static void ieee802_1x_encapsulate_radius(struct eapol_test_data *e,
 
        os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
                    MAC2STR(e->wpa_s->own_addr));
-       if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
+       if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_CALLING_STATION_ID)
+           &&
+           !radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
                                 (u8 *) buf, os_strlen(buf))) {
                printf("Could not add Calling-Station-Id\n");
                goto fail;
@@ -148,40 +231,29 @@ static void ieee802_1x_encapsulate_radius(struct eapol_test_data *e,
        /* TODO: should probably check MTU from driver config; 2304 is max for
         * IEEE 802.11, but use 1400 to avoid problems with too large packets
         */
-       if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
+       if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_FRAMED_MTU) &&
+           !radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
                printf("Could not add Framed-MTU\n");
                goto fail;
        }
 
-       if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
+       if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_NAS_PORT_TYPE) &&
+           !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
                                       RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
                printf("Could not add NAS-Port-Type\n");
                goto fail;
        }
 
        os_snprintf(buf, sizeof(buf), "%s", e->connect_info);
-       if (!radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
+       if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_CONNECT_INFO) &&
+           !radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
                                 (u8 *) buf, os_strlen(buf))) {
                printf("Could not add Connect-Info\n");
                goto fail;
        }
 
-       if (e->cui_flag) {
-               int l = 0;
-               if (e->cui_flag == 1) {
-                       l = 1;
-                       buf[0] = '\0';
-               } else if (e->cui_flag == 2) {
-                       os_snprintf(buf, sizeof(buf), "%s", e->cui_str);
-                       l = os_strlen(buf);
-               }
-               if (!radius_msg_add_attr(msg,
-                                        RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
-                                        (u8 *) buf, l)) {
-                       printf("Could not add Chargeable-User-Identity\n");
-                       goto fail;
-               }
-       }
+       if (add_extra_attrs(msg, e->extra_attrs) < 0)
+               goto fail;
 
        if (eap && !radius_msg_add_eap(msg, eap, len)) {
                printf("Could not add EAP-Message\n");
@@ -190,7 +262,8 @@ static void ieee802_1x_encapsulate_radius(struct eapol_test_data *e,
 
        /* State attribute must be copied if and only if this packet is
         * Access-Request reply to the previous Access-Challenge */
-       if (e->last_recv_radius && e->last_recv_radius->hdr->code ==
+       if (e->last_recv_radius &&
+           radius_msg_get_hdr(e->last_recv_radius)->code ==
            RADIUS_CODE_ACCESS_CHALLENGE) {
                int res = radius_msg_copy_attr(msg, e->last_recv_radius,
                                               RADIUS_ATTR_STATE);
@@ -205,19 +278,19 @@ static void ieee802_1x_encapsulate_radius(struct eapol_test_data *e,
                }
        }
 
-       radius_client_send(e->radius, msg, RADIUS_AUTH, e->wpa_s->own_addr);
+       if (radius_client_send(e->radius, msg, RADIUS_AUTH, e->wpa_s->own_addr)
+           < 0)
+               goto fail;
        return;
 
  fail:
        radius_msg_free(msg);
-       os_free(msg);
 }
 
 
 static int eapol_test_eapol_send(void *ctx, int type, const u8 *buf,
                                 size_t len)
 {
-       /* struct wpa_supplicant *wpa_s = ctx; */
        printf("WPA: eapol_test_eapol_send(type=%d len=%lu)\n",
               type, (unsigned long) len);
        if (type == IEEE802_1X_TYPE_EAP_PACKET) {
@@ -231,16 +304,16 @@ static int eapol_test_eapol_send(void *ctx, int type, const u8 *buf,
 static void eapol_test_set_config_blob(void *ctx,
                                       struct wpa_config_blob *blob)
 {
-       struct wpa_supplicant *wpa_s = ctx;
-       wpa_config_set_blob(wpa_s->conf, blob);
+       struct eapol_test_data *e = ctx;
+       wpa_config_set_blob(e->wpa_s->conf, blob);
 }
 
 
 static const struct wpa_config_blob *
 eapol_test_get_config_blob(void *ctx, const char *name)
 {
-       struct wpa_supplicant *wpa_s = ctx;
-       return wpa_config_get_blob(wpa_s->conf, name);
+       struct eapol_test_data *e = ctx;
+       return wpa_config_get_blob(e->wpa_s->conf, name);
 }
 
 
@@ -309,6 +382,132 @@ static void eapol_sm_cb(struct eapol_sm *eapol, int success, void *ctx)
 }
 
 
+static void eapol_test_write_cert(FILE *f, const char *subject,
+                                 const struct wpabuf *cert)
+{
+       unsigned char *encoded;
+
+       encoded = base64_encode(wpabuf_head(cert), wpabuf_len(cert), NULL);
+       if (encoded == NULL)
+               return;
+       fprintf(f, "%s\n-----BEGIN CERTIFICATE-----\n%s"
+               "-----END CERTIFICATE-----\n\n", subject, encoded);
+       os_free(encoded);
+}
+
+
+#if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
+static void eapol_test_eap_param_needed(void *ctx, enum wpa_ctrl_req_type field,
+                                       const char *default_txt)
+{
+       struct eapol_test_data *e = ctx;
+       struct wpa_supplicant *wpa_s = e->wpa_s;
+       struct wpa_ssid *ssid = wpa_s->current_ssid;
+       const char *field_name, *txt = NULL;
+       char *buf;
+       size_t buflen;
+       int len;
+
+       if (ssid == NULL)
+               return;
+
+       field_name = wpa_supplicant_ctrl_req_to_string(field, default_txt,
+                                                      &txt);
+       if (field_name == NULL) {
+               wpa_printf(MSG_WARNING, "Unhandled EAP param %d needed",
+                          field);
+               return;
+       }
+
+       buflen = 100 + os_strlen(txt) + ssid->ssid_len;
+       buf = os_malloc(buflen);
+       if (buf == NULL)
+               return;
+       len = os_snprintf(buf, buflen,
+                         WPA_CTRL_REQ "%s-%d:%s needed for SSID ",
+                         field_name, ssid->id, txt);
+       if (len < 0 || (size_t) len >= buflen) {
+               os_free(buf);
+               return;
+       }
+       if (ssid->ssid && buflen > len + ssid->ssid_len) {
+               os_memcpy(buf + len, ssid->ssid, ssid->ssid_len);
+               len += ssid->ssid_len;
+               buf[len] = '\0';
+       }
+       buf[buflen - 1] = '\0';
+       wpa_msg(wpa_s, MSG_INFO, "%s", buf);
+       os_free(buf);
+}
+#else /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
+#define eapol_test_eap_param_needed NULL
+#endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
+
+
+static void eapol_test_cert_cb(void *ctx, int depth, const char *subject,
+                              const char *cert_hash,
+                              const struct wpabuf *cert)
+{
+       struct eapol_test_data *e = ctx;
+
+       wpa_msg(e->wpa_s, MSG_INFO, WPA_EVENT_EAP_PEER_CERT
+               "depth=%d subject='%s'%s%s",
+               depth, subject,
+               cert_hash ? " hash=" : "",
+               cert_hash ? cert_hash : "");
+
+       if (cert) {
+               char *cert_hex;
+               size_t len = wpabuf_len(cert) * 2 + 1;
+               cert_hex = os_malloc(len);
+               if (cert_hex) {
+                       wpa_snprintf_hex(cert_hex, len, wpabuf_head(cert),
+                                        wpabuf_len(cert));
+                       wpa_msg_ctrl(e->wpa_s, MSG_INFO,
+                                    WPA_EVENT_EAP_PEER_CERT
+                                    "depth=%d subject='%s' cert=%s",
+                                    depth, subject, cert_hex);
+                       os_free(cert_hex);
+               }
+
+               if (e->server_cert_file)
+                       eapol_test_write_cert(e->server_cert_file,
+                                             subject, cert);
+       }
+}
+
+
+static void eapol_test_set_anon_id(void *ctx, const u8 *id, size_t len)
+{
+       struct eapol_test_data *e = ctx;
+       struct wpa_supplicant *wpa_s = e->wpa_s;
+       char *str;
+       int res;
+
+       wpa_hexdump_ascii(MSG_DEBUG, "EAP method updated anonymous_identity",
+                         id, len);
+
+       if (wpa_s->current_ssid == NULL)
+               return;
+
+       if (id == NULL) {
+               if (wpa_config_set(wpa_s->current_ssid, "anonymous_identity",
+                                  "NULL", 0) < 0)
+                       return;
+       } else {
+               str = os_malloc(len * 2 + 1);
+               if (str == NULL)
+                       return;
+               wpa_snprintf_hex(str, len * 2 + 1, id, len);
+               res = wpa_config_set(wpa_s->current_ssid, "anonymous_identity",
+                                    str, 0);
+               os_free(str);
+               if (res < 0)
+                       return;
+       }
+}
+
+
 static int test_eapol(struct eapol_test_data *e, struct wpa_supplicant *wpa_s,
                      struct wpa_ssid *ssid)
 {
@@ -320,7 +519,7 @@ static int test_eapol(struct eapol_test_data *e, struct wpa_supplicant *wpa_s,
                printf("Failed to allocate EAPOL context.\n");
                return -1;
        }
-       ctx->ctx = wpa_s;
+       ctx->ctx = e;
        ctx->msg_ctx = wpa_s;
        ctx->scard_ctx = wpa_s->scard;
        ctx->cb = eapol_sm_cb;
@@ -331,11 +530,13 @@ static int test_eapol(struct eapol_test_data *e, struct wpa_supplicant *wpa_s,
        ctx->eapol_send = eapol_test_eapol_send;
        ctx->set_config_blob = eapol_test_set_config_blob;
        ctx->get_config_blob = eapol_test_get_config_blob;
-#ifdef EAP_TLS_OPENSSL
        ctx->opensc_engine_path = wpa_s->conf->opensc_engine_path;
        ctx->pkcs11_engine_path = wpa_s->conf->pkcs11_engine_path;
        ctx->pkcs11_module_path = wpa_s->conf->pkcs11_module_path;
-#endif /* EAP_TLS_OPENSSL */
+       ctx->eap_param_needed = eapol_test_eap_param_needed;
+       ctx->cert_cb = eapol_test_cert_cb;
+       ctx->cert_in_cb = 1;
+       ctx->set_anon_id = eapol_test_set_anon_id;
 
        wpa_s->eapol = eapol_sm_init(ctx);
        if (wpa_s->eapol == NULL) {
@@ -350,6 +551,7 @@ static int test_eapol(struct eapol_test_data *e, struct wpa_supplicant *wpa_s,
        eapol_conf.required_keys = 0;
        eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
        eapol_conf.workaround = ssid->eap_workaround;
+       eapol_conf.external_sim = wpa_s->conf->external_sim;
        eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
        eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
 
@@ -365,12 +567,12 @@ static int test_eapol(struct eapol_test_data *e, struct wpa_supplicant *wpa_s,
 static void test_eapol_clean(struct eapol_test_data *e,
                             struct wpa_supplicant *wpa_s)
 {
+       struct extra_radius_attr *p, *prev;
+
        radius_client_deinit(e->radius);
-       os_free(e->last_eap_radius);
-       if (e->last_recv_radius) {
-               radius_msg_free(e->last_recv_radius);
-               os_free(e->last_recv_radius);
-       }
+       wpabuf_free(e->last_eap_radius);
+       radius_msg_free(e->last_recv_radius);
+       e->last_recv_radius = NULL;
        os_free(e->eap_identity);
        e->eap_identity = NULL;
        eapol_sm_deinit(wpa_s->eapol);
@@ -386,7 +588,18 @@ static void test_eapol_clean(struct eapol_test_data *e,
                wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
                wpa_s->ctrl_iface = NULL;
        }
+
+       ext_password_deinit(wpa_s->ext_pw);
+       wpa_s->ext_pw = NULL;
+
        wpa_config_free(wpa_s->conf);
+
+       p = e->extra_attrs;
+       while (p) {
+               prev = p;
+               p = p->next;
+               os_free(prev);
+       }
 }
 
 
@@ -447,9 +660,8 @@ static char *eap_type_text(u8 type)
 
 static void ieee802_1x_decapsulate_radius(struct eapol_test_data *e)
 {
-       u8 *eap;
-       size_t len;
-       struct eap_hdr *hdr;
+       struct wpabuf *eap;
+       const struct eap_hdr *hdr;
        int eap_type = -1;
        char buf[64];
        struct radius_msg *msg;
@@ -459,30 +671,29 @@ static void ieee802_1x_decapsulate_radius(struct eapol_test_data *e)
 
        msg = e->last_recv_radius;
 
-       eap = radius_msg_get_eap(msg, &len);
+       eap = radius_msg_get_eap(msg);
        if (eap == NULL) {
                /* draft-aboba-radius-rfc2869bis-20.txt, Chap. 2.6.3:
                 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
                 * attribute */
                wpa_printf(MSG_DEBUG, "could not extract "
                               "EAP-Message from RADIUS message");
-               os_free(e->last_eap_radius);
+               wpabuf_free(e->last_eap_radius);
                e->last_eap_radius = NULL;
-               e->last_eap_radius_len = 0;
                return;
        }
 
-       if (len < sizeof(*hdr)) {
+       if (wpabuf_len(eap) < sizeof(*hdr)) {
                wpa_printf(MSG_DEBUG, "too short EAP packet "
                               "received from authentication server");
-               os_free(eap);
+               wpabuf_free(eap);
                return;
        }
 
-       if (len > sizeof(*hdr))
-               eap_type = eap[sizeof(*hdr)];
+       if (wpabuf_len(eap) > sizeof(*hdr))
+               eap_type = (wpabuf_head_u8(eap))[sizeof(*hdr)];
 
-       hdr = (struct eap_hdr *) eap;
+       hdr = wpabuf_head(eap);
        switch (hdr->code) {
        case EAP_CODE_REQUEST:
                os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
@@ -505,7 +716,7 @@ static void ieee802_1x_decapsulate_radius(struct eapol_test_data *e)
                break;
        default:
                os_strlcpy(buf, "unknown EAP code", sizeof(buf));
-               wpa_hexdump(MSG_DEBUG, "Decapsulated EAP packet", eap, len);
+               wpa_hexdump_buf(MSG_DEBUG, "Decapsulated EAP packet", eap);
                break;
        }
        wpa_printf(MSG_DEBUG, "decapsulated EAP packet (code=%d "
@@ -514,20 +725,21 @@ static void ieee802_1x_decapsulate_radius(struct eapol_test_data *e)
 
        /* sta->eapol_sm->be_auth.idFromServer = hdr->identifier; */
 
-       os_free(e->last_eap_radius);
+       wpabuf_free(e->last_eap_radius);
        e->last_eap_radius = eap;
-       e->last_eap_radius_len = len;
 
        {
                struct ieee802_1x_hdr *dot1x;
-               dot1x = os_malloc(sizeof(*dot1x) + len);
+               dot1x = os_malloc(sizeof(*dot1x) + wpabuf_len(eap));
                assert(dot1x != NULL);
                dot1x->version = EAPOL_VERSION;
                dot1x->type = IEEE802_1X_TYPE_EAP_PACKET;
-               dot1x->length = htons(len);
-               os_memcpy((u8 *) (dot1x + 1), eap, len);
+               dot1x->length = htons(wpabuf_len(eap));
+               os_memcpy((u8 *) (dot1x + 1), wpabuf_head(eap),
+                         wpabuf_len(eap));
                eapol_sm_rx_eapol(e->wpa_s->eapol, e->wpa_s->bssid,
-                                 (u8 *) dot1x, sizeof(*dot1x) + len);
+                                 (u8 *) dot1x,
+                                 sizeof(*dot1x) + wpabuf_len(eap));
                os_free(dot1x);
        }
 }
@@ -535,7 +747,8 @@ static void ieee802_1x_decapsulate_radius(struct eapol_test_data *e)
 
 static void ieee802_1x_get_keys(struct eapol_test_data *e,
                                struct radius_msg *msg, struct radius_msg *req,
-                               u8 *shared_secret, size_t shared_secret_len)
+                               const u8 *shared_secret,
+                               size_t shared_secret_len)
 {
        struct radius_ms_mppe_keys *keys;
 
@@ -582,14 +795,15 @@ static void ieee802_1x_get_keys(struct eapol_test_data *e,
 /* Process the RADIUS frames from Authentication Server */
 static RadiusRxResult
 ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
-                       u8 *shared_secret, size_t shared_secret_len,
+                       const u8 *shared_secret, size_t shared_secret_len,
                        void *data)
 {
        struct eapol_test_data *e = data;
+       struct radius_hdr *hdr = radius_msg_get_hdr(msg);
 
        /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
         * present when packet contains an EAP-Message attribute */
-       if (msg->hdr->code == RADIUS_CODE_ACCESS_REJECT &&
+       if (hdr->code == RADIUS_CODE_ACCESS_REJECT &&
            radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
                                0) < 0 &&
            radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
@@ -603,9 +817,9 @@ ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
                return RADIUS_RX_UNKNOWN;
        }
 
-       if (msg->hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
-           msg->hdr->code != RADIUS_CODE_ACCESS_REJECT &&
-           msg->hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
+       if (hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
+           hdr->code != RADIUS_CODE_ACCESS_REJECT &&
+           hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
                printf("Unknown RADIUS message code\n");
                return RADIUS_RX_UNKNOWN;
        }
@@ -613,14 +827,10 @@ ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
        e->radius_identifier = -1;
        wpa_printf(MSG_DEBUG, "RADIUS packet matching with station");
 
-       if (e->last_recv_radius) {
-               radius_msg_free(e->last_recv_radius);
-               os_free(e->last_recv_radius);
-       }
-
+       radius_msg_free(e->last_recv_radius);
        e->last_recv_radius = msg;
 
-       switch (msg->hdr->code) {
+       switch (hdr->code) {
        case RADIUS_CODE_ACCESS_ACCEPT:
                e->radius_access_accept_received = 1;
                ieee802_1x_get_keys(e, msg, req, shared_secret,
@@ -633,9 +843,9 @@ ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
 
        ieee802_1x_decapsulate_radius(e);
 
-       if ((msg->hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
+       if ((hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
             e->eapol_test_num_reauths < 0) ||
-           msg->hdr->code == RADIUS_CODE_ACCESS_REJECT) {
+           hdr->code == RADIUS_CODE_ACCESS_REJECT) {
                eloop_terminate();
        }
 
@@ -733,7 +943,7 @@ static int scard_test(void)
        unsigned char aka_ik[IK_LEN];
        unsigned char aka_ck[CK_LEN];
 
-       scard = scard_init(SCARD_TRY_BOTH);
+       scard = scard_init(NULL);
        if (scard == NULL)
                return -1;
        if (scard_set_pin(scard, "1234")) {
@@ -748,6 +958,9 @@ static int scard_test(void)
        wpa_hexdump_ascii(MSG_DEBUG, "SCARD: IMSI", (u8 *) imsi, len);
        /* NOTE: Permanent Username: 1 | IMSI */
 
+       wpa_printf(MSG_DEBUG, "SCARD: MNC length %d",
+                  scard_get_mnc_len(scard));
+
        os_memset(_rand, 0, sizeof(_rand));
        if (scard_gsm_auth(scard, _rand, sres, kc))
                goto failed;
@@ -830,7 +1043,7 @@ static int scard_get_triplets(int argc, char *argv[])
                wpa_debug_level = 99;
        }
 
-       scard = scard_init(SCARD_GSM_SIM_ONLY);
+       scard = scard_init(NULL);
        if (scard == NULL) {
                printf("Failed to open smartcard connection\n");
                return -1;
@@ -873,10 +1086,9 @@ static int scard_get_triplets(int argc, char *argv[])
 }
 
 
-static void eapol_test_terminate(int sig, void *eloop_ctx,
-                                void *signal_ctx)
+static void eapol_test_terminate(int sig, void *signal_ctx)
 {
-       struct wpa_supplicant *wpa_s = eloop_ctx;
+       struct wpa_supplicant *wpa_s = signal_ctx;
        wpa_msg(wpa_s, MSG_INFO, "Signal %d received - terminating", sig);
        eloop_terminate();
 }
@@ -888,8 +1100,9 @@ static void usage(void)
               "eapol_test [-nWS] -c<conf> [-a<AS IP>] [-p<AS port>] "
               "[-s<AS secret>]\\\n"
               "           [-r<count>] [-t<timeout>] [-C<Connect-Info>] \\\n"
-              "           [-M<client MAC address>] \\\n"
-              "           [-I<CUI>] [-i] [-A<client IP>]\n"
+              "           [-M<client MAC address>] [-o<server cert file] \\\n"
+              "           [-N<attr spec>] \\\n"
+              "           [-A<client IP>]\n"
               "eapol_test scard\n"
               "eapol_test sim <PIN> <num triplets> [debug]\n"
               "\n");
@@ -905,7 +1118,7 @@ static void usage(void)
               "automatically\n"
               "  -r<count> = number of re-authentications\n"
               "  -W = wait for a control interface monitor before starting\n"
-              "  -S = save configuration after authentiation\n"
+              "  -S = save configuration after authentication\n"
               "  -n = no MPPE keys expected\n"
               "  -t<timeout> = sets timeout in seconds (default: 30 s)\n"
               "  -C<Connect-Info> = RADIUS Connect-Info (default: "
@@ -913,14 +1126,26 @@ static void usage(void)
               "  -M<client MAC address> = Set own MAC address "
               "(Calling-Station-Id,\n"
               "                           default: 02:00:00:00:00:01)\n"
-              "  -I<CUI> = send Chargeable-User-Identity containing the "
-              "value of CUI\n"
-              "  -i = send NUL value in Chargeable-User-Identity\n");
+              "  -o<server cert file> = Write received server certificate\n"
+              "                         chain to the specified file\n"
+              "  -N<attr spec> = send arbitrary attribute specified by:\n"
+              "                  attr_id:syntax:value or attr_id\n"
+              "                  attr_id - number id of the attribute\n"
+              "                  syntax - one of: s, d, x\n"
+              "                     s = string\n"
+              "                     d = integer\n"
+              "                     x = octet string\n"
+              "                  value - attribute value.\n"
+              "       When only attr_id is specified, NULL will be used as "
+              "value.\n"
+              "       Multiple attributes can be specified by using the "
+              "option several times.\n");
 }
 
 
 int main(int argc, char *argv[])
 {
+       struct wpa_global global;
        struct wpa_supplicant wpa_s;
        int c, ret = 1, wait_for_monitor = 0, save_config = 0;
        char *as_addr = "127.0.0.1";
@@ -929,6 +1154,8 @@ int main(int argc, char *argv[])
        char *cli_addr = NULL;
        char *conf = NULL;
        int timeout = 30;
+       char *pos;
+       struct extra_radius_attr *p = NULL, *p1;
 
        if (os_program_init())
                return -1;
@@ -943,7 +1170,7 @@ int main(int argc, char *argv[])
        wpa_debug_show_keys = 1;
 
        for (;;) {
-               c = getopt(argc, argv, "a:A:c:C:iI:M:np:r:s:St:W");
+               c = getopt(argc, argv, "a:A:c:C:M:nN:o:p:r:s:St:W");
                if (c < 0)
                        break;
                switch (c) {
@@ -959,13 +1186,6 @@ int main(int argc, char *argv[])
                case 'C':
                        eapol_test.connect_info = optarg;
                        break;
-               case 'i':
-                       eapol_test.cui_flag = 1;
-                       break;
-               case 'I':
-                       eapol_test.cui_flag = 2;
-                       eapol_test.cui_str = optarg;
-                       break;
                case 'M':
                        if (hwaddr_aton(optarg, eapol_test.own_addr)) {
                                usage();
@@ -975,6 +1195,16 @@ int main(int argc, char *argv[])
                case 'n':
                        eapol_test.no_mppe_keys++;
                        break;
+               case 'o':
+                       if (eapol_test.server_cert_file)
+                               fclose(eapol_test.server_cert_file);
+                       eapol_test.server_cert_file = fopen(optarg, "w");
+                       if (eapol_test.server_cert_file == NULL) {
+                               printf("Could not open '%s' for writing\n",
+                                      optarg);
+                               return -1;
+                       }
+                       break;
                case 'p':
                        as_port = atoi(optarg);
                        break;
@@ -993,6 +1223,34 @@ int main(int argc, char *argv[])
                case 'W':
                        wait_for_monitor++;
                        break;
+               case 'N':
+                       p1 = os_zalloc(sizeof(*p1));
+                       if (p1 == NULL)
+                               break;
+                       if (!p)
+                               eapol_test.extra_attrs = p1;
+                       else
+                               p->next = p1;
+                       p = p1;
+
+                       p->type = atoi(optarg);
+                       pos = os_strchr(optarg, ':');
+                       if (pos == NULL) {
+                               p->syntax = 'n';
+                               p->data = NULL;
+                               break;
+                       }
+
+                       pos++;
+                       if (pos[0] == '\0' || pos[1] != ':') {
+                               printf("Incorrect format of attribute "
+                                      "specification\n");
+                               break;
+                       }
+
+                       p->syntax = pos[0];
+                       p->data = pos + 2;
+                       break;
                default:
                        usage();
                        return -1;
@@ -1014,19 +1272,23 @@ int main(int argc, char *argv[])
                return -1;
        }
 
-       if (eap_peer_register_methods()) {
+       if (eap_register_methods()) {
                wpa_printf(MSG_ERROR, "Failed to register EAP methods");
                return -1;
        }
 
-       if (eloop_init(&wpa_s)) {
+       if (eloop_init()) {
                wpa_printf(MSG_ERROR, "Failed to initialize event loop");
                return -1;
        }
 
+       os_memset(&global, 0, sizeof(global));
        os_memset(&wpa_s, 0, sizeof(wpa_s));
+       wpa_s.global = &global;
        eapol_test.wpa_s = &wpa_s;
-       wpa_s.conf = wpa_config_read(conf);
+       dl_list_init(&wpa_s.bss);
+       dl_list_init(&wpa_s.bss_id);
+       wpa_s.conf = wpa_config_read(conf, NULL);
        if (wpa_s.conf == NULL) {
                printf("Failed to parse configuration file '%s'.\n", conf);
                return -1;
@@ -1056,14 +1318,17 @@ int main(int argc, char *argv[])
        if (test_eapol(&eapol_test, &wpa_s, wpa_s.conf->ssid))
                return -1;
 
+       if (wpas_init_ext_pw(&wpa_s) < 0)
+               return -1;
+
        if (wait_for_monitor)
                wpa_supplicant_ctrl_iface_wait(wpa_s.ctrl_iface);
 
        eloop_register_timeout(timeout, 0, eapol_test_timeout, &eapol_test,
                               NULL);
        eloop_register_timeout(0, 0, send_eap_request_identity, &wpa_s, NULL);
-       eloop_register_signal_terminate(eapol_test_terminate, NULL);
-       eloop_register_signal_reconfig(eapol_test_terminate, NULL);
+       eloop_register_signal_terminate(eapol_test_terminate, &wpa_s);
+       eloop_register_signal_reconfig(eapol_test_terminate, &wpa_s);
        eloop_run();
 
        eloop_cancel_timeout(eapol_test_timeout, &eapol_test, NULL);
@@ -1083,9 +1348,15 @@ int main(int argc, char *argv[])
        test_eapol_clean(&eapol_test, &wpa_s);
 
        eap_peer_unregister_methods();
+#ifdef CONFIG_AP
+       eap_server_unregister_methods();
+#endif /* CONFIG_AP */
 
        eloop_destroy();
 
+       if (eapol_test.server_cert_file)
+               fclose(eapol_test.server_cert_file);
+
        printf("MPPE keys OK: %d  mismatch: %d\n",
               eapol_test.num_mppe_ok, eapol_test.num_mppe_mismatch);
        if (eapol_test.num_mppe_mismatch)