WPS: Add wps_check_pin command for processing PIN from user input
[libeap.git] / hostapd / ctrl_iface.c
index 083a097..7f628f1 100644 (file)
@@ -34,6 +34,8 @@
 #include "ap/accounting.h"
 #include "ap/wps_hostapd.h"
 #include "ap/ctrl_iface_ap.h"
+#include "wps/wps_defs.h"
+#include "wps/wps.h"
 #include "ctrl_iface.h"
 
 
@@ -368,6 +370,51 @@ static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
 }
 
 
+static int hostapd_ctrl_iface_wps_check_pin(
+       struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
+{
+       char pin[9];
+       size_t len;
+       char *pos;
+       int ret;
+
+       wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
+                             (u8 *) cmd, os_strlen(cmd));
+       for (pos = cmd, len = 0; *pos != '\0'; pos++) {
+               if (*pos < '0' || *pos > '9')
+                       continue;
+               pin[len++] = *pos;
+               if (len == 9) {
+                       wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
+                       return -1;
+               }
+       }
+       if (len != 4 && len != 8) {
+               wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
+               return -1;
+       }
+       pin[len] = '\0';
+
+       if (len == 8) {
+               unsigned int pin_val;
+               pin_val = atoi(pin);
+               if (!wps_pin_valid(pin_val)) {
+                       wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
+                       ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
+                       if (ret < 0 || (size_t) ret >= buflen)
+                               return -1;
+                       return ret;
+               }
+       }
+
+       ret = os_snprintf(buf, buflen, "%s", pin);
+       if (ret < 0 || (size_t) ret >= buflen)
+               return -1;
+
+       return ret;
+}
+
+
 #ifdef CONFIG_WPS_OOB
 static int hostapd_ctrl_iface_wps_oob(struct hostapd_data *hapd, char *txt)
 {
@@ -446,6 +493,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)
 {
@@ -548,6 +635,9 @@ static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
        } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
                if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
                        reply_len = -1;
+       } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
+               reply_len = hostapd_ctrl_iface_wps_check_pin(
+                       hapd, buf + 14, reply, reply_size);
        } else if (os_strcmp(buf, "WPS_PBC") == 0) {
                if (hostapd_wps_button_pushed(hapd))
                        reply_len = -1;
@@ -560,6 +650,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;