Updated through tag hostap_2_5 from git://w1.fi/hostap.git
[mech_eap.git] / libeap / wpa_supplicant / eapol_test.c
index 4eed854..dce7d1f 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 "utils/base64.h"
 #include "rsn_supp/wpa.h"
-#include "eap_peer/eap_i.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_drivers[] = { NULL };
+const struct wpa_driver_ops *const wpa_drivers[] = { NULL };
 
 
 struct extra_radius_attr {
@@ -51,6 +46,7 @@ struct eapol_test_data {
        int eapol_test_num_reauths;
        int no_mppe_keys;
        int num_mppe_ok, num_mppe_mismatch;
+       int req_eap_key_name;
 
        u8 radius_identifier;
        struct radius_msg *last_recv_radius;
@@ -58,12 +54,13 @@ 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;
+       u8 authenticator_eap_key_name[256];
+       size_t authenticator_eap_key_name_len;
        int radius_access_accept_received;
        int radius_access_reject_received;
        int auth_timed_out;
@@ -74,6 +71,14 @@ struct eapol_test_data {
        char *connect_info;
        u8 own_addr[ETH_ALEN];
        struct extra_radius_attr *extra_attrs;
+
+       FILE *server_cert_file;
+
+       const char *pcsc_reader;
+       const char *pcsc_pin;
+
+       unsigned int ctrl_iface:1;
+       unsigned int id_req_sent:1;
 };
 
 static struct eapol_test_data eapol_test;
@@ -99,7 +104,7 @@ static int add_extra_attr(struct radius_msg *msg,
        size_t len;
        char *pos;
        u32 val;
-       char buf[128];
+       char buf[RADIUS_MAX_ATTR_LEN + 1];
 
        switch (attr->syntax) {
        case 's':
@@ -115,7 +120,7 @@ static int add_extra_attr(struct radius_msg *msg,
                if (pos[0] == '0' && pos[1] == 'x')
                        pos += 2;
                len = os_strlen(pos);
-               if ((len & 1) || (len / 2) > sizeof(buf)) {
+               if ((len & 1) || (len / 2) > RADIUS_MAX_ATTR_LEN) {
                        printf("Invalid extra attribute hexstring\n");
                        return -1;
                }
@@ -172,7 +177,7 @@ 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;
 
@@ -212,6 +217,13 @@ static void ieee802_1x_encapsulate_radius(struct eapol_test_data *e,
                goto fail;
        }
 
+       if (e->req_eap_key_name &&
+           !radius_msg_add_attr(msg, RADIUS_ATTR_EAP_KEY_NAME, (u8 *) "\0",
+                                1)) {
+               printf("Could not add EAP-Key-Name\n");
+               goto fail;
+       }
+
        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)) {
@@ -279,7 +291,9 @@ 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:
@@ -290,7 +304,6 @@ static void ieee802_1x_encapsulate_radius(struct eapol_test_data *e,
 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) {
@@ -304,22 +317,26 @@ 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);
 }
 
 
 static void eapol_test_eapol_done_cb(void *ctx)
 {
+       struct eapol_test_data *e = ctx;
+
        printf("WPA: EAPOL processing complete\n");
+       wpa_supplicant_cancel_auth_timeout(e->wpa_s);
+       wpa_supplicant_set_state(e->wpa_s, WPA_COMPLETED);
 }
 
 
@@ -336,6 +353,8 @@ static int eapol_test_compare_pmk(struct eapol_test_data *e)
 {
        u8 pmk[PMK_LEN];
        int ret = 1;
+       const u8 *sess_id;
+       size_t sess_id_len;
 
        if (eapol_sm_get_key(e->wpa_s->eapol, pmk, PMK_LEN) == 0) {
                wpa_hexdump(MSG_DEBUG, "PMK from EAPOL", pmk, PMK_LEN);
@@ -364,14 +383,40 @@ static int eapol_test_compare_pmk(struct eapol_test_data *e)
        else if (!e->no_mppe_keys)
                e->num_mppe_ok++;
 
+       sess_id = eapol_sm_get_session_id(e->wpa_s->eapol, &sess_id_len);
+       if (!sess_id)
+               return ret;
+       if (e->authenticator_eap_key_name_len == 0) {
+               wpa_printf(MSG_INFO, "No EAP-Key-Name received from server");
+               return ret;
+       }
+
+       if (e->authenticator_eap_key_name_len != sess_id_len ||
+           os_memcmp(e->authenticator_eap_key_name, sess_id, sess_id_len) != 0)
+       {
+               wpa_printf(MSG_INFO,
+                          "Locally derived EAP Session-Id does not match EAP-Key-Name from server");
+               wpa_hexdump(MSG_DEBUG, "EAP Session-Id", sess_id, sess_id_len);
+               wpa_hexdump(MSG_DEBUG, "EAP-Key-Name from server",
+                           e->authenticator_eap_key_name,
+                           e->authenticator_eap_key_name_len);
+       } else {
+               wpa_printf(MSG_INFO,
+                          "Locally derived EAP Session-Id matches EAP-Key-Name from server");
+       }
+
        return ret;
 }
 
 
-static void eapol_sm_cb(struct eapol_sm *eapol, int success, void *ctx)
+static void eapol_sm_cb(struct eapol_sm *eapol, enum eapol_supp_result result,
+                       void *ctx)
 {
        struct eapol_test_data *e = ctx;
-       printf("eapol_sm_cb: success=%d\n", success);
+       printf("eapol_sm_cb: result=%d\n", result);
+       e->id_req_sent = 0;
+       if (e->ctrl_iface)
+               return;
        e->eapol_test_num_reauths--;
        if (e->eapol_test_num_reauths < 0)
                eloop_terminate();
@@ -382,18 +427,163 @@ 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 (os_snprintf_error(buflen, len)) {
+               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 *altsubject[], int num_altsubject,
+                              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);
+       }
+
+       if (altsubject) {
+               int i;
+
+               for (i = 0; i < num_altsubject; i++)
+                       wpa_msg(e->wpa_s, MSG_INFO, WPA_EVENT_EAP_PEER_ALT
+                               "depth=%d %s", depth, altsubject[i]);
+       }
+}
+
+
+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 enum wpa_states eapol_test_get_state(void *ctx)
+{
+       struct eapol_test_data *e = ctx;
+       struct wpa_supplicant *wpa_s = e->wpa_s;
+
+       return wpa_s->wpa_state;
+}
+
+
 static int test_eapol(struct eapol_test_data *e, struct wpa_supplicant *wpa_s,
                      struct wpa_ssid *ssid)
 {
        struct eapol_config eapol_conf;
        struct eapol_ctx *ctx;
+       struct wpa_sm_ctx *wctx;
 
        ctx = os_zalloc(sizeof(*ctx));
        if (ctx == NULL) {
                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;
@@ -407,6 +597,11 @@ static int test_eapol(struct eapol_test_data *e, struct wpa_supplicant *wpa_s,
        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;
+       ctx->openssl_ciphers = wpa_s->conf->openssl_ciphers;
+       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) {
@@ -415,12 +610,32 @@ static int test_eapol(struct eapol_test_data *e, struct wpa_supplicant *wpa_s,
                return -1;
        }
 
+       wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
+       wctx = os_zalloc(sizeof(*wctx));
+       if (wctx == NULL) {
+               os_free(ctx);
+               return -1;
+       }
+       wctx->ctx = e;
+       wctx->msg_ctx = wpa_s;
+       wctx->get_state = eapol_test_get_state;
+       wpa_s->wpa = wpa_sm_init(wctx);
+       if (!wpa_s->wpa) {
+               os_free(ctx);
+               os_free(wctx);
+               return -1;
+       }
+
+       if (!ssid)
+               return 0;
+
        wpa_s->current_ssid = ssid;
        os_memset(&eapol_conf, 0, sizeof(eapol_conf));
        eapol_conf.accept_802_1x_keys = 1;
        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);
 
@@ -438,8 +653,10 @@ static void test_eapol_clean(struct eapol_test_data *e,
 {
        struct extra_radius_attr *p, *prev;
 
+       wpa_sm_deinit(wpa_s->wpa);
+       wpa_s->wpa = NULL;
        radius_client_deinit(e->radius);
-       os_free(e->last_eap_radius);
+       wpabuf_free(e->last_eap_radius);
        radius_msg_free(e->last_recv_radius);
        e->last_recv_radius = NULL;
        os_free(e->eap_identity);
@@ -457,6 +674,10 @@ 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;
@@ -525,9 +746,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;
@@ -537,30 +757,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)",
@@ -579,11 +798,13 @@ static void ieee802_1x_decapsulate_radius(struct eapol_test_data *e)
                break;
        case EAP_CODE_FAILURE:
                os_strlcpy(buf, "EAP Failure", sizeof(buf));
+               if (e->ctrl_iface)
+                       break;
                eloop_terminate();
                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 "
@@ -592,20 +813,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);
        }
 }
@@ -617,6 +839,8 @@ static void ieee802_1x_get_keys(struct eapol_test_data *e,
                                size_t shared_secret_len)
 {
        struct radius_ms_mppe_keys *keys;
+       u8 *buf;
+       size_t len;
 
        keys = radius_msg_get_ms_keys(msg, req, shared_secret,
                                      shared_secret_len);
@@ -655,6 +879,14 @@ static void ieee802_1x_get_keys(struct eapol_test_data *e,
                os_free(keys->recv);
                os_free(keys);
        }
+
+       if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_EAP_KEY_NAME, &buf, &len,
+                                   NULL) == 0) {
+               os_memcpy(e->authenticator_eap_key_name, buf, len);
+               e->authenticator_eap_key_name_len = len;
+       } else {
+               e->authenticator_eap_key_name_len = 0;
+       }
 }
 
 
@@ -712,25 +944,66 @@ ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
        if ((hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
             e->eapol_test_num_reauths < 0) ||
            hdr->code == RADIUS_CODE_ACCESS_REJECT) {
-               eloop_terminate();
+               if (!e->ctrl_iface)
+                       eloop_terminate();
        }
 
        return RADIUS_RX_QUEUED;
 }
 
 
+static int driver_get_ssid(void *priv, u8 *ssid)
+{
+       ssid[0] = 0;
+       return 0;
+}
+
+
+static int driver_get_bssid(void *priv, u8 *bssid)
+{
+       struct eapol_test_data *e = priv;
+
+       if (e->ctrl_iface && !e->id_req_sent) {
+               eloop_register_timeout(0, 0, send_eap_request_identity,
+                                      e->wpa_s, NULL);
+               e->id_req_sent = 1;
+       }
+
+       os_memset(bssid, 0, ETH_ALEN);
+       bssid[5] = 1;
+       return 0;
+}
+
+
+static int driver_get_capa(void *priv, struct wpa_driver_capa *capa)
+{
+       os_memset(capa, 0, sizeof(*capa));
+       capa->flags = WPA_DRIVER_FLAGS_WIRED;
+       return 0;
+}
+
+
+struct wpa_driver_ops eapol_test_drv_ops = {
+       .name = "test",
+       .get_ssid = driver_get_ssid,
+       .get_bssid = driver_get_bssid,
+       .get_capa = driver_get_capa,
+};
+
 static void wpa_init_conf(struct eapol_test_data *e,
                          struct wpa_supplicant *wpa_s, const char *authsrv,
                          int port, const char *secret,
-                         const char *cli_addr)
+                         const char *cli_addr, const char *ifname)
 {
        struct hostapd_radius_server *as;
        int res;
 
+       wpa_s->driver = &eapol_test_drv_ops;
+       wpa_s->drv_priv = e;
        wpa_s->bssid[5] = 1;
        os_memcpy(wpa_s->own_addr, e->own_addr, ETH_ALEN);
        e->own_ip_addr.s_addr = htonl((127 << 24) | 1);
-       os_strlcpy(wpa_s->ifname, "test", sizeof(wpa_s->ifname));
+       os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
 
        e->radius_conf = os_zalloc(sizeof(struct hostapd_radius_servers));
        assert(e->radius_conf != NULL);
@@ -749,9 +1022,12 @@ static void wpa_init_conf(struct eapol_test_data *e,
                *pos++ = a[3];
        }
 #else /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
-       inet_aton(authsrv, &as->addr.u.v4);
+       if (hostapd_parse_ip_addr(authsrv, &as->addr) < 0) {
+               wpa_printf(MSG_ERROR, "Invalid IP address '%s'",
+                          authsrv);
+               assert(0);
+       }
 #endif /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
-       as->addr.af = AF_INET;
        as->port = port;
        as->shared_secret = (u8 *) os_strdup(secret);
        as->shared_secret_len = os_strlen(secret);
@@ -778,7 +1054,7 @@ static void wpa_init_conf(struct eapol_test_data *e,
 }
 
 
-static int scard_test(void)
+static int scard_test(struct eapol_test_data *e)
 {
        struct scard_data *scard;
        size_t len;
@@ -809,10 +1085,10 @@ 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(e->pcsc_reader);
        if (scard == NULL)
                return -1;
-       if (scard_set_pin(scard, "1234")) {
+       if (scard_set_pin(scard, e->pcsc_pin)) {
                wpa_printf(MSG_WARNING, "PIN validation failed");
                scard_deinit(scard);
                return -1;
@@ -824,6 +1100,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;
@@ -884,7 +1163,7 @@ failed:
 }
 
 
-static int scard_get_triplets(int argc, char *argv[])
+static int scard_get_triplets(struct eapol_test_data *e, int argc, char *argv[])
 {
        struct scard_data *scard;
        size_t len;
@@ -906,7 +1185,7 @@ static int scard_get_triplets(int argc, char *argv[])
                wpa_debug_level = 99;
        }
 
-       scard = scard_init(SCARD_GSM_SIM_ONLY);
+       scard = scard_init(e->pcsc_reader);
        if (scard == NULL) {
                printf("Failed to open smartcard connection\n");
                return -1;
@@ -960,12 +1239,13 @@ static void eapol_test_terminate(int sig, void *signal_ctx)
 static void usage(void)
 {
        printf("usage:\n"
-              "eapol_test [-nWS] -c<conf> [-a<AS IP>] [-p<AS port>] "
+              "eapol_test [-enWS] -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"
-              "           [-N<attr spec>] \\\n"
-              "           [-A<client IP>]\n"
+              "           [-M<client MAC address>] [-o<server cert file] \\\n"
+              "           [-N<attr spec>] [-R<PC/SC reader>] "
+              "[-P<PC/SC PIN>] \\\n"
+              "           [-A<client IP>] [-i<ifname>] [-T<ctrl_iface>]\n"
               "eapol_test scard\n"
               "eapol_test sim <PIN> <num triplets> [debug]\n"
               "\n");
@@ -980,6 +1260,7 @@ static void usage(void)
               "  -A<client IP> = IP address of the client, default: select "
               "automatically\n"
               "  -r<count> = number of re-authentications\n"
+              "  -e = Request EAP-Key-Name\n"
               "  -W = wait for a control interface monitor before starting\n"
               "  -S = save configuration after authentication\n"
               "  -n = no MPPE keys expected\n"
@@ -989,6 +1270,8 @@ static void usage(void)
               "  -M<client MAC address> = Set own MAC address "
               "(Calling-Station-Id,\n"
               "                           default: 02:00:00:00:00:01)\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"
@@ -1006,6 +1289,7 @@ static void usage(void)
 
 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";
@@ -1016,6 +1300,8 @@ int main(int argc, char *argv[])
        int timeout = 30;
        char *pos;
        struct extra_radius_attr *p = NULL, *p1;
+       const char *ifname = "test";
+       const char *ctrl_iface = NULL;
 
        if (os_program_init())
                return -1;
@@ -1025,12 +1311,13 @@ int main(int argc, char *argv[])
        os_memset(&eapol_test, 0, sizeof(eapol_test));
        eapol_test.connect_info = "CONNECT 11Mbps 802.11b";
        os_memcpy(eapol_test.own_addr, "\x02\x00\x00\x00\x00\x01", ETH_ALEN);
+       eapol_test.pcsc_pin = "1234";
 
        wpa_debug_level = 0;
        wpa_debug_show_keys = 1;
 
        for (;;) {
-               c = getopt(argc, argv, "a:A:c:C:M:nN:p:r:s:St:W");
+               c = getopt(argc, argv, "a:A:c:C:ei:M:nN:o:p:P:r:R:s:St:T:W");
                if (c < 0)
                        break;
                switch (c) {
@@ -1046,6 +1333,12 @@ int main(int argc, char *argv[])
                case 'C':
                        eapol_test.connect_info = optarg;
                        break;
+               case 'e':
+                       eapol_test.req_eap_key_name = 1;
+                       break;
+               case 'i':
+                       ifname = optarg;
+                       break;
                case 'M':
                        if (hwaddr_aton(optarg, eapol_test.own_addr)) {
                                usage();
@@ -1055,12 +1348,28 @@ 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;
+               case 'P':
+                       eapol_test.pcsc_pin = optarg;
+                       break;
                case 'r':
                        eapol_test.eapol_test_num_reauths = atoi(optarg);
                        break;
+               case 'R':
+                       eapol_test.pcsc_reader = optarg;
+                       break;
                case 's':
                        as_secret = optarg;
                        break;
@@ -1070,11 +1379,15 @@ int main(int argc, char *argv[])
                case 't':
                        timeout = atoi(optarg);
                        break;
+               case 'T':
+                       ctrl_iface = optarg;
+                       eapol_test.ctrl_iface = 1;
+                       break;
                case 'W':
                        wait_for_monitor++;
                        break;
                case 'N':
-                       p1 = os_zalloc(sizeof(p1));
+                       p1 = os_zalloc(sizeof(*p1));
                        if (p1 == NULL)
                                break;
                        if (!p)
@@ -1108,15 +1421,15 @@ int main(int argc, char *argv[])
        }
 
        if (argc > optind && os_strcmp(argv[optind], "scard") == 0) {
-               return scard_test();
+               return scard_test(&eapol_test);
        }
 
        if (argc > optind && os_strcmp(argv[optind], "sim") == 0) {
-               return scard_get_triplets(argc - optind - 1,
+               return scard_get_triplets(&eapol_test, argc - optind - 1,
                                          &argv[optind + 1]);
        }
 
-       if (conf == NULL) {
+       if (conf == NULL && !ctrl_iface) {
                usage();
                printf("Configuration file is required.\n");
                return -1;
@@ -1132,20 +1445,32 @@ int main(int argc, char *argv[])
                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);
+       if (conf)
+               wpa_s.conf = wpa_config_read(conf, NULL);
+       else
+               wpa_s.conf = wpa_config_alloc_empty(ctrl_iface, NULL);
        if (wpa_s.conf == NULL) {
                printf("Failed to parse configuration file '%s'.\n", conf);
                return -1;
        }
-       if (wpa_s.conf->ssid == NULL) {
+       if (!ctrl_iface && wpa_s.conf->ssid == NULL) {
                printf("No networks defined.\n");
                return -1;
        }
 
+       if (eapol_test.pcsc_reader) {
+               os_free(wpa_s.conf->pcsc_reader);
+               wpa_s.conf->pcsc_reader = os_strdup(eapol_test.pcsc_reader);
+       }
+
        wpa_init_conf(&eapol_test, &wpa_s, as_addr, as_port, as_secret,
-                     cli_addr);
+                     cli_addr, ifname);
        wpa_s.ctrl_iface = wpa_supplicant_ctrl_iface_init(&wpa_s);
        if (wpa_s.ctrl_iface == NULL) {
                printf("Failed to initialize control interface '%s'.\n"
@@ -1158,18 +1483,25 @@ int main(int argc, char *argv[])
                       wpa_s.conf->ctrl_interface);
                return -1;
        }
-       if (wpa_supplicant_scard_init(&wpa_s, wpa_s.conf->ssid))
+       if (wpa_s.conf->ssid &&
+           wpa_supplicant_scard_init(&wpa_s, wpa_s.conf->ssid))
                return -1;
 
        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);
+       if (!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, &wpa_s);
        eloop_register_signal_reconfig(eapol_test_terminate, &wpa_s);
        eloop_run();
@@ -1191,9 +1523,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)