From b4e34f2fdf2d6c5b5f33a412908a3d1aa7d13f56 Mon Sep 17 00:00:00 2001 From: Jouni Malinen Date: Tue, 21 Sep 2010 19:51:23 -0700 Subject: [PATCH] WPS: Make testing operations configurable at runtime Instead of build time options (CONFIG_WPS_TESTING_EXTRA_CRED and CONFIG_WPS_EXTENSIBILITY_TESTING), use a single build option (CONFIG_WPS_TESTING) and runtime configuration of which testing operations are enabled. This allows a single binary to be used for various tests. The runtime configuration can be done through control interface with wpa_cli/hostapd_cli commands: Enable extensibility tests: set wps_version_number 0x57 Disable extensibility tests (WPS2 build): set wps_version_number 0x20 Enable extra credential tests: set wps_testing_dummy_cred 1 Disable extra credential tests: set wps_testing_dummy_cred 0 --- hostapd/Makefile | 4 ++++ hostapd/ctrl_iface.c | 44 +++++++++++++++++++++++++++++++++++++ hostapd/hostapd_cli.c | 21 ++++++++++++++++++ src/wps/wps.c | 6 +++++ src/wps/wps_attr_build.c | 16 ++++++++------ src/wps/wps_defs.h | 14 ++++++++---- src/wps/wps_registrar.c | 9 +++++--- wpa_supplicant/Makefile | 4 ++++ wpa_supplicant/ctrl_iface.c | 20 +++++++++++++++++ 9 files changed, 124 insertions(+), 14 deletions(-) diff --git a/hostapd/Makefile b/hostapd/Makefile index f43f4a595..7d4bab802 100644 --- a/hostapd/Makefile +++ b/hostapd/Makefile @@ -361,6 +361,10 @@ CFLAGS += -DCONFIG_WPS_STRICT OBJS += ../src/wps/wps_validate.o endif +ifdef CONFIG_WPS_TESTING +CFLAGS += -DCONFIG_WPS_TESTING +endif + endif ifdef CONFIG_EAP_IKEV2 diff --git a/hostapd/ctrl_iface.c b/hostapd/ctrl_iface.c index 083a097f4..bd25e1c89 100644 --- a/hostapd/ctrl_iface.c +++ b/hostapd/ctrl_iface.c @@ -34,6 +34,7 @@ #include "ap/accounting.h" #include "ap/wps_hostapd.h" #include "ap/ctrl_iface_ap.h" +#include "wps/wps_defs.h" #include "ctrl_iface.h" @@ -446,6 +447,46 @@ static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt, #endif /* CONFIG_WPS */ +static int hostapd_ctrl_iface_set(struct hostapd_data *wpa_s, char *cmd) +{ + char *value; + int ret = 0; + + value = os_strchr(cmd, ' '); + if (value == NULL) + return -1; + *value++ = '\0'; + + wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value); + if (0) { +#ifdef CONFIG_WPS_TESTING + } else if (os_strcasecmp(cmd, "wps_version_number") == 0) { + long int val; + val = strtol(value, NULL, 0); + if (val < 0 || val > 0xff) { + ret = -1; + wpa_printf(MSG_DEBUG, "WPS: Invalid " + "wps_version_number %ld", val); + } else { + wps_version_number = val; + wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS " + "version %u.%u", + (wps_version_number & 0xf0) >> 4, + wps_version_number & 0x0f); + } + } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) { + wps_testing_dummy_cred = atoi(value); + wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d", + wps_testing_dummy_cred); +#endif /* CONFIG_WPS_TESTING */ + } else { + ret = -1; + } + + return ret; +} + + static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx, void *sock_ctx) { @@ -560,6 +601,9 @@ static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx, reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11, reply, reply_size); #endif /* CONFIG_WPS */ + } else if (os_strncmp(buf, "SET ", 4) == 0) { + if (hostapd_ctrl_iface_set(hapd, buf + 4)) + reply_len = -1; } else { os_memcpy(reply, "UNKNOWN COMMAND\n", 16); reply_len = 16; diff --git a/hostapd/hostapd_cli.c b/hostapd/hostapd_cli.c index 4f22e853c..e80de487d 100644 --- a/hostapd/hostapd_cli.c +++ b/hostapd/hostapd_cli.c @@ -570,6 +570,26 @@ static int hostapd_cli_cmd_interface(struct wpa_ctrl *ctrl, int argc, } +static int hostapd_cli_cmd_set(struct wpa_ctrl *ctrl, int argc, char *argv[]) +{ + char cmd[256]; + int res; + + if (argc != 2) { + printf("Invalid SET command: needs two arguments (variable " + "name and value)\n"); + return -1; + } + + res = os_snprintf(cmd, sizeof(cmd), "SET %s %s", argv[0], argv[1]); + if (res < 0 || (size_t) res >= sizeof(cmd) - 1) { + printf("Too long SET command.\n"); + return -1; + } + return wpa_ctrl_command(ctrl, cmd); +} + + struct hostapd_cli_cmd { const char *cmd; int (*handler)(struct wpa_ctrl *ctrl, int argc, char *argv[]); @@ -599,6 +619,7 @@ static struct hostapd_cli_cmd hostapd_cli_commands[] = { { "level", hostapd_cli_cmd_level }, { "license", hostapd_cli_cmd_license }, { "quit", hostapd_cli_cmd_quit }, + { "set", hostapd_cli_cmd_set }, { NULL, NULL } }; diff --git a/src/wps/wps.c b/src/wps/wps.c index 01a2cdec4..1f259c212 100644 --- a/src/wps/wps.c +++ b/src/wps/wps.c @@ -21,6 +21,12 @@ #include "wps_dev_attr.h" +#ifdef CONFIG_WPS_TESTING +int wps_version_number = 0x20; +int wps_testing_dummy_cred = 0; +#endif /* CONFIG_WPS_TESTING */ + + /** * wps_init - Initialize WPS Registration protocol data * @cfg: WPS configuration diff --git a/src/wps/wps_attr_build.c b/src/wps/wps_attr_build.c index 6d19e10e8..681c465c1 100644 --- a/src/wps/wps_attr_build.c +++ b/src/wps/wps_attr_build.c @@ -209,13 +209,15 @@ int wps_build_wfa_ext(struct wpabuf *msg, int req_to_enroll, WPA_PUT_BE16(len, (u8 *) wpabuf_put(msg, 0) - len - 2); #endif /* CONFIG_WPS2 */ -#ifdef CONFIG_WPS_EXTENSIBILITY_TESTING - wpa_printf(MSG_DEBUG, "WPS: * Extensibility Testing - extra " - "attribute"; - wpabuf_put_be16(msg, ATTR_EXTENSIBILITY_TEST); - wpabuf_put_be16(msg, 1); - wpabuf_put_u8(msg, 42); -#endif /* CONFIG_WPS_EXTENSIBILITY_TESTING */ +#ifdef CONFIG_WPS_TESTING + if (WPS_VERSION > 0x20) { + wpa_printf(MSG_DEBUG, "WPS: * Extensibility Testing - extra " + "attribute"); + wpabuf_put_be16(msg, ATTR_EXTENSIBILITY_TEST); + wpabuf_put_be16(msg, 1); + wpabuf_put_u8(msg, 42); + } +#endif /* CONFIG_WPS_TESTING */ return 0; } diff --git a/src/wps/wps_defs.h b/src/wps/wps_defs.h index 719333ae4..868f8ad7e 100644 --- a/src/wps/wps_defs.h +++ b/src/wps/wps_defs.h @@ -15,16 +15,22 @@ #ifndef WPS_DEFS_H #define WPS_DEFS_H +#ifdef CONFIG_WPS_TESTING + +extern int wps_version_number; +extern int wps_testing_dummy_cred; +#define WPS_VERSION wps_version_number + +#else /* CONFIG_WPS_TESTING */ + #ifdef CONFIG_WPS2 -#ifdef CONFIG_WPS_EXTENSIBILITY_TESTING -#define WPS_VERSION 0x57 -#else /* CONFIG_WPS_EXTENSIBILITY_TESTING */ #define WPS_VERSION 0x20 -#endif /* CONFIG_WPS_EXTENSIBILITY_TESTING */ #else /* CONFIG_WPS2 */ #define WPS_VERSION 0x10 #endif /* CONFIG_WPS2 */ +#endif /* CONFIG_WPS_TESTING */ + /* Diffie-Hellman 1536-bit MODP Group; RFC 3526, Group 5 */ #define WPS_DH_GROUP 5 diff --git a/src/wps/wps_registrar.c b/src/wps/wps_registrar.c index 8a8f69127..0d8c72e68 100644 --- a/src/wps/wps_registrar.c +++ b/src/wps/wps_registrar.c @@ -1465,8 +1465,11 @@ int wps_build_cred(struct wps_data *wps, struct wpabuf *msg) } use_provided: -#ifdef CONFIG_WPS_TESTING_EXTRA_CRED - cred = wpabuf_alloc(200); +#ifdef CONFIG_WPS_TESTING + if (wps_testing_dummy_cred) + cred = wpabuf_alloc(200); + else + cred = NULL; if (cred) { struct wps_credential dummy; wpa_printf(MSG_DEBUG, "WPS: Add dummy credential"); @@ -1487,7 +1490,7 @@ use_provided: wpabuf_free(cred); } -#endif /* CONFIG_WPS_TESTING_EXTRA_CRED */ +#endif /* CONFIG_WPS_TESTING */ cred = wpabuf_alloc(200); if (cred == NULL) diff --git a/wpa_supplicant/Makefile b/wpa_supplicant/Makefile index a113cca24..0b6e8150f 100644 --- a/wpa_supplicant/Makefile +++ b/wpa_supplicant/Makefile @@ -559,6 +559,10 @@ CFLAGS += -DCONFIG_WPS_STRICT OBJS += ../src/wps/wps_validate.o endif +ifdef CONFIG_WPS_TESTING +CFLAGS += -DCONFIG_WPS_TESTING +endif + endif ifdef CONFIG_EAP_IKEV2 diff --git a/wpa_supplicant/ctrl_iface.c b/wpa_supplicant/ctrl_iface.c index 27a7ffcda..8c5dcdf98 100644 --- a/wpa_supplicant/ctrl_iface.c +++ b/wpa_supplicant/ctrl_iface.c @@ -84,6 +84,26 @@ static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s, ret = -1; } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) { wpa_s->wps_fragment_size = atoi(value); +#ifdef CONFIG_WPS_TESTING + } else if (os_strcasecmp(cmd, "wps_version_number") == 0) { + long int val; + val = strtol(value, NULL, 0); + if (val < 0 || val > 0xff) { + ret = -1; + wpa_printf(MSG_DEBUG, "WPS: Invalid " + "wps_version_number %ld", val); + } else { + wps_version_number = val; + wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS " + "version %u.%u", + (wps_version_number & 0xf0) >> 4, + wps_version_number & 0x0f); + } + } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) { + wps_testing_dummy_cred = atoi(value); + wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d", + wps_testing_dummy_cred); +#endif /* CONFIG_WPS_TESTING */ } else if (os_strcasecmp(cmd, "ampdu") == 0) { if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0) ret = -1;