SCARD: Clean up SIM/USIM selection
[mech_eap.git] / wpa_supplicant / eapol_test.c
index 03b8c7e..2b25b69 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * WPA Supplicant - test code
- * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
+ * Copyright (c) 2003-2013, Jouni Malinen <j@w1.fi>
  *
  * This software may be distributed under the terms of the BSD license.
  * See README for more details.
 #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;
@@ -98,7 +98,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':
@@ -114,7 +114,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;
                }
@@ -171,7 +171,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;
 
@@ -396,6 +396,54 @@ static void eapol_test_write_cert(FILE *f, const char *subject,
 }
 
 
+#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)
@@ -485,6 +533,7 @@ 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->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;
@@ -502,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);
 
@@ -893,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, NULL);
+       scard = scard_init(NULL);
        if (scard == NULL)
                return -1;
        if (scard_set_pin(scard, "1234")) {
@@ -993,7 +1043,7 @@ static int scard_get_triplets(int argc, char *argv[])
                wpa_debug_level = 99;
        }
 
-       scard = scard_init(SCARD_GSM_SIM_ONLY, NULL);
+       scard = scard_init(NULL);
        if (scard == NULL) {
                printf("Failed to open smartcard connection\n");
                return -1;
@@ -1095,6 +1145,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";
@@ -1173,7 +1224,7 @@ int main(int argc, char *argv[])
                        wait_for_monitor++;
                        break;
                case 'N':
-                       p1 = os_zalloc(sizeof(p1));
+                       p1 = os_zalloc(sizeof(*p1));
                        if (p1 == NULL)
                                break;
                        if (!p)
@@ -1231,9 +1282,13 @@ 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);
+       wpa_s.conf = wpa_config_read(conf, NULL);
        if (wpa_s.conf == NULL) {
                printf("Failed to parse configuration file '%s'.\n", conf);
                return -1;