WPS: Fix CONFIG_WPS_OOB build
[libeap.git] / src / wps / wps_common.c
index 8340b27..82e6311 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Wi-Fi Protected Setup - common functionality
- * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
+ * Copyright (c) 2008-2009, 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
 #include "includes.h"
 
 #include "common.h"
-#include "dh_group5.h"
-#include "sha256.h"
-#include "aes_wrap.h"
-#include "crypto.h"
+#include "crypto/aes_wrap.h"
+#include "crypto/crypto.h"
+#include "crypto/dh_group5.h"
+#include "crypto/sha1.h"
+#include "crypto/sha256.h"
 #include "wps_i.h"
 #include "wps_dev_attr.h"
 
@@ -80,6 +81,8 @@ int wps_derive_keys(struct wps_data *wps)
                return -1;
        }
 
+       wpa_hexdump_buf_key(MSG_DEBUG, "WPS: DH Private Key", wps->dh_privkey);
+       wpa_hexdump_buf(MSG_DEBUG, "WPS: DH peer Public Key", pubkey);
        dh_shared = dh5_derive_shared(wps->dh_ctx, pubkey, wps->dh_privkey);
        dh5_free(wps->dh_ctx);
        wps->dh_ctx = NULL;
@@ -129,56 +132,6 @@ int wps_derive_keys(struct wps_data *wps)
 }
 
 
-int wps_derive_mgmt_keys(struct wps_data *wps)
-{
-       u8 nonces[2 * WPS_NONCE_LEN];
-       u8 keys[WPS_MGMTAUTHKEY_LEN + WPS_MGMTENCKEY_LEN];
-       u8 hash[SHA256_MAC_LEN];
-       const u8 *addr[2];
-       size_t len[2];
-       const char *auth_label = "WFA-WLAN-Management-MgmtAuthKey";
-       const char *enc_label = "WFA-WLAN-Management-MgmtEncKey";
-
-       /* MgmtAuthKey || MgmtEncKey =
-        * kdf(EMSK, N1 || N2 || "WFA-WLAN-Management-Keys", 384) */
-       os_memcpy(nonces, wps->nonce_e, WPS_NONCE_LEN);
-       os_memcpy(nonces + WPS_NONCE_LEN, wps->nonce_r, WPS_NONCE_LEN);
-       wps_kdf(wps->emsk, nonces, sizeof(nonces), "WFA-WLAN-Management-Keys",
-               keys, sizeof(keys));
-       os_memcpy(wps->mgmt_auth_key, keys, WPS_MGMTAUTHKEY_LEN);
-       os_memcpy(wps->mgmt_enc_key, keys + WPS_MGMTAUTHKEY_LEN,
-                 WPS_MGMTENCKEY_LEN);
-
-       addr[0] = nonces;
-       len[0] = sizeof(nonces);
-
-       /* MgmtEncKeyID = first 128 bits of
-        * SHA-256(N1 || N2 || "WFA-WLAN-Management-MgmtAuthKey") */
-       addr[1] = (const u8 *) auth_label;
-       len[1] = os_strlen(auth_label);
-       sha256_vector(2, addr, len, hash);
-       os_memcpy(wps->mgmt_auth_key_id, hash, WPS_MGMT_KEY_ID_LEN);
-
-       /* MgmtEncKeyID = first 128 bits of
-        * SHA-256(N1 || N2 || "WFA-WLAN-Management-MgmtEncKey") */
-       addr[1] = (const u8 *) enc_label;
-       len[1] = os_strlen(enc_label);
-       sha256_vector(2, addr, len, hash);
-       os_memcpy(wps->mgmt_enc_key_id, hash, WPS_MGMT_KEY_ID_LEN);
-
-       wpa_hexdump_key(MSG_DEBUG, "WPS: MgmtAuthKey",
-                       wps->mgmt_auth_key, WPS_MGMTAUTHKEY_LEN);
-       wpa_hexdump(MSG_DEBUG, "WPS: MgmtAuthKeyID",
-                   wps->mgmt_auth_key_id, WPS_MGMT_KEY_ID_LEN);
-       wpa_hexdump_key(MSG_DEBUG, "WPS: MgmtEncKey",
-                       wps->mgmt_enc_key, WPS_MGMTENCKEY_LEN);
-       wpa_hexdump(MSG_DEBUG, "WPS: MgmtEncKeyID",
-                   wps->mgmt_enc_key_id, WPS_MGMT_KEY_ID_LEN);
-
-       return 0;
-}
-
-
 void wps_derive_psk(struct wps_data *wps, const u8 *dev_passwd,
                    size_t dev_passwd_len)
 {
@@ -338,6 +291,24 @@ void wps_pwd_auth_fail_event(struct wps_context *wps, int enrollee, int part)
 }
 
 
+void wps_pbc_overlap_event(struct wps_context *wps)
+{
+       if (wps->event_cb == NULL)
+               return;
+
+       wps->event_cb(wps->cb_ctx, WPS_EV_PBC_OVERLAP, NULL);
+}
+
+
+void wps_pbc_timeout_event(struct wps_context *wps)
+{
+       if (wps->event_cb == NULL)
+               return;
+
+       wps->event_cb(wps->cb_ctx, WPS_EV_PBC_TIMEOUT, NULL);
+}
+
+
 #ifdef CONFIG_WPS_OOB
 
 static struct wpabuf * wps_get_oob_cred(struct wps_context *wps)
@@ -356,7 +327,9 @@ static struct wpabuf * wps_get_oob_cred(struct wps_context *wps)
        data.wps = wps;
        data.auth_type = wps->auth_types;
        data.encr_type = wps->encr_types;
-       if (wps_build_version(plain) || wps_build_cred(&data, plain)) {
+       if (wps_build_version(plain) ||
+           wps_build_cred(&data, plain) ||
+           wps_build_wfa_ext(plain, 0, NULL, 0)) {
                wpabuf_free(plain);
                return NULL;
        }
@@ -387,7 +360,8 @@ static struct wpabuf * wps_get_oob_dev_pwd(struct wps_context *wps)
        }
 
        if (wps_build_version(data) ||
-           wps_build_oob_dev_password(data, wps)) {
+           wps_build_oob_dev_password(data, wps) ||
+           wps_build_wfa_ext(data, 0, NULL, 0)) {
                wpa_printf(MSG_ERROR, "WPS: Build OOB device password "
                           "attribute error");
                wpabuf_free(data);
@@ -560,3 +534,120 @@ int wps_get_oob_method(char *method)
 }
 
 #endif /* CONFIG_WPS_OOB */
+
+
+int wps_dev_type_str2bin(const char *str, u8 dev_type[WPS_DEV_TYPE_LEN])
+{
+       const char *pos;
+
+       /* <categ>-<OUI>-<subcateg> */
+       WPA_PUT_BE16(dev_type, atoi(str));
+       pos = os_strchr(str, '-');
+       if (pos == NULL)
+               return -1;
+       pos++;
+       if (hexstr2bin(pos, &dev_type[2], 4))
+               return -1;
+       pos = os_strchr(pos, '-');
+       if (pos == NULL)
+               return -1;
+       pos++;
+       WPA_PUT_BE16(&dev_type[6], atoi(pos));
+
+
+       return 0;
+}
+
+
+char * wps_dev_type_bin2str(const u8 dev_type[WPS_DEV_TYPE_LEN], char *buf,
+                           size_t buf_len)
+{
+       int ret;
+
+       ret = os_snprintf(buf, buf_len, "%u-%08X-%u",
+                         WPA_GET_BE16(dev_type), WPA_GET_BE32(&dev_type[2]),
+                         WPA_GET_BE16(&dev_type[6]));
+       if (ret < 0 || (unsigned int) ret >= buf_len)
+               return NULL;
+
+       return buf;
+}
+
+
+void uuid_gen_mac_addr(const u8 *mac_addr, u8 *uuid)
+{
+       const u8 *addr[2];
+       size_t len[2];
+       u8 hash[SHA1_MAC_LEN];
+       u8 nsid[16] = {
+               0x52, 0x64, 0x80, 0xf8,
+               0xc9, 0x9b,
+               0x4b, 0xe5,
+               0xa6, 0x55,
+               0x58, 0xed, 0x5f, 0x5d, 0x60, 0x84
+       };
+
+       addr[0] = nsid;
+       len[0] = sizeof(nsid);
+       addr[1] = mac_addr;
+       len[1] = 6;
+       sha1_vector(2, addr, len, hash);
+       os_memcpy(uuid, hash, 16);
+
+       /* Version: 5 = named-based version using SHA-1 */
+       uuid[6] = (5 << 4) | (uuid[6] & 0x0f);
+
+       /* Variant specified in RFC 4122 */
+       uuid[8] = 0x80 | (uuid[8] & 0x3f);
+}
+
+
+u16 wps_config_methods_str2bin(const char *str)
+{
+       u16 methods = 0;
+
+       if (str == NULL) {
+               /* Default to enabling methods based on build configuration */
+               methods |= WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD;
+#ifdef CONFIG_WPS2
+               methods |= WPS_CONFIG_VIRT_DISPLAY;
+#endif /* CONFIG_WPS2 */
+#ifdef CONFIG_WPS_UFD
+               methods |= WPS_CONFIG_USBA;
+#endif /* CONFIG_WPS_UFD */
+#ifdef CONFIG_WPS_NFC
+               methods |= WPS_CONFIG_NFC_INTERFACE;
+#endif /* CONFIG_WPS_NFC */
+       } else {
+               if (os_strstr(str, "usba"))
+                       methods |= WPS_CONFIG_USBA;
+               if (os_strstr(str, "ethernet"))
+                       methods |= WPS_CONFIG_ETHERNET;
+               if (os_strstr(str, "label"))
+                       methods |= WPS_CONFIG_LABEL;
+               if (os_strstr(str, "display"))
+                       methods |= WPS_CONFIG_DISPLAY;
+               if (os_strstr(str, "ext_nfc_token"))
+                       methods |= WPS_CONFIG_EXT_NFC_TOKEN;
+               if (os_strstr(str, "int_nfc_token"))
+                       methods |= WPS_CONFIG_INT_NFC_TOKEN;
+               if (os_strstr(str, "nfc_interface"))
+                       methods |= WPS_CONFIG_NFC_INTERFACE;
+               if (os_strstr(str, "push_button"))
+                       methods |= WPS_CONFIG_PUSHBUTTON;
+               if (os_strstr(str, "keypad"))
+                       methods |= WPS_CONFIG_KEYPAD;
+#ifdef CONFIG_WPS2
+               if (os_strstr(str, "virtual_display"))
+                       methods |= WPS_CONFIG_VIRT_DISPLAY;
+               if (os_strstr(str, "physical_display"))
+                       methods |= WPS_CONFIG_PHY_DISPLAY;
+               if (os_strstr(str, "virtual_push_button"))
+                       methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
+               if (os_strstr(str, "physical_push_button"))
+                       methods |= WPS_CONFIG_PHY_PUSHBUTTON;
+#endif /* CONFIG_WPS2 */
+       }
+
+       return methods;
+}