2008-11-23 18:34:26 +01:00
|
|
|
/*
|
|
|
|
* Wi-Fi Protected Setup - common functionality
|
2012-06-28 19:54:53 +02:00
|
|
|
* Copyright (c) 2008-2012, Jouni Malinen <j@w1.fi>
|
2008-11-23 18:34:26 +01:00
|
|
|
*
|
2012-02-11 15:46:35 +01:00
|
|
|
* This software may be distributed under the terms of the BSD license.
|
|
|
|
* See README for more details.
|
2008-11-23 18:34:26 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "includes.h"
|
|
|
|
|
|
|
|
#include "common.h"
|
2009-11-29 22:04:43 +01:00
|
|
|
#include "crypto/aes_wrap.h"
|
|
|
|
#include "crypto/crypto.h"
|
|
|
|
#include "crypto/dh_group5.h"
|
|
|
|
#include "crypto/sha1.h"
|
|
|
|
#include "crypto/sha256.h"
|
2010-11-24 00:05:20 +01:00
|
|
|
#include "crypto/random.h"
|
2008-11-23 18:34:26 +01:00
|
|
|
#include "wps_i.h"
|
|
|
|
|
|
|
|
|
2008-11-29 11:11:56 +01:00
|
|
|
void wps_kdf(const u8 *key, const u8 *label_prefix, size_t label_prefix_len,
|
|
|
|
const char *label, u8 *res, size_t res_len)
|
2008-11-23 18:34:26 +01:00
|
|
|
{
|
|
|
|
u8 i_buf[4], key_bits[4];
|
2008-11-29 11:11:56 +01:00
|
|
|
const u8 *addr[4];
|
|
|
|
size_t len[4];
|
2008-11-23 18:34:26 +01:00
|
|
|
int i, iter;
|
|
|
|
u8 hash[SHA256_MAC_LEN], *opos;
|
|
|
|
size_t left;
|
|
|
|
|
|
|
|
WPA_PUT_BE32(key_bits, res_len * 8);
|
|
|
|
|
|
|
|
addr[0] = i_buf;
|
|
|
|
len[0] = sizeof(i_buf);
|
2008-11-29 11:11:56 +01:00
|
|
|
addr[1] = label_prefix;
|
|
|
|
len[1] = label_prefix_len;
|
|
|
|
addr[2] = (const u8 *) label;
|
|
|
|
len[2] = os_strlen(label);
|
|
|
|
addr[3] = key_bits;
|
|
|
|
len[3] = sizeof(key_bits);
|
2008-11-23 18:34:26 +01:00
|
|
|
|
|
|
|
iter = (res_len + SHA256_MAC_LEN - 1) / SHA256_MAC_LEN;
|
|
|
|
opos = res;
|
|
|
|
left = res_len;
|
|
|
|
|
|
|
|
for (i = 1; i <= iter; i++) {
|
|
|
|
WPA_PUT_BE32(i_buf, i);
|
2008-11-29 11:11:56 +01:00
|
|
|
hmac_sha256_vector(key, SHA256_MAC_LEN, 4, addr, len, hash);
|
2008-11-23 18:34:26 +01:00
|
|
|
if (i < iter) {
|
|
|
|
os_memcpy(opos, hash, SHA256_MAC_LEN);
|
|
|
|
opos += SHA256_MAC_LEN;
|
|
|
|
left -= SHA256_MAC_LEN;
|
|
|
|
} else
|
|
|
|
os_memcpy(opos, hash, left);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int wps_derive_keys(struct wps_data *wps)
|
|
|
|
{
|
|
|
|
struct wpabuf *pubkey, *dh_shared;
|
|
|
|
u8 dhkey[SHA256_MAC_LEN], kdk[SHA256_MAC_LEN];
|
|
|
|
const u8 *addr[3];
|
|
|
|
size_t len[3];
|
|
|
|
u8 keys[WPS_AUTHKEY_LEN + WPS_KEYWRAPKEY_LEN + WPS_EMSK_LEN];
|
|
|
|
|
|
|
|
if (wps->dh_privkey == NULL) {
|
|
|
|
wpa_printf(MSG_DEBUG, "WPS: Own DH private key not available");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pubkey = wps->registrar ? wps->dh_pubkey_e : wps->dh_pubkey_r;
|
|
|
|
if (pubkey == NULL) {
|
|
|
|
wpa_printf(MSG_DEBUG, "WPS: Peer DH public key not available");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-07-18 05:23:55 +02:00
|
|
|
wpa_hexdump_buf_key(MSG_DEBUG, "WPS: DH Private Key", wps->dh_privkey);
|
2010-06-27 06:06:44 +02:00
|
|
|
wpa_hexdump_buf(MSG_DEBUG, "WPS: DH peer Public Key", pubkey);
|
2009-10-11 18:17:22 +02:00
|
|
|
dh_shared = dh5_derive_shared(wps->dh_ctx, pubkey, wps->dh_privkey);
|
|
|
|
dh5_free(wps->dh_ctx);
|
|
|
|
wps->dh_ctx = NULL;
|
2009-01-22 18:32:58 +01:00
|
|
|
dh_shared = wpabuf_zeropad(dh_shared, 192);
|
2008-11-23 18:34:26 +01:00
|
|
|
if (dh_shared == NULL) {
|
|
|
|
wpa_printf(MSG_DEBUG, "WPS: Failed to derive DH shared key");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Own DH private key is not needed anymore */
|
|
|
|
wpabuf_free(wps->dh_privkey);
|
|
|
|
wps->dh_privkey = NULL;
|
|
|
|
|
|
|
|
wpa_hexdump_buf_key(MSG_DEBUG, "WPS: DH shared key", dh_shared);
|
|
|
|
|
|
|
|
/* DHKey = SHA-256(g^AB mod p) */
|
|
|
|
addr[0] = wpabuf_head(dh_shared);
|
|
|
|
len[0] = wpabuf_len(dh_shared);
|
|
|
|
sha256_vector(1, addr, len, dhkey);
|
|
|
|
wpa_hexdump_key(MSG_DEBUG, "WPS: DHKey", dhkey, sizeof(dhkey));
|
|
|
|
wpabuf_free(dh_shared);
|
|
|
|
|
|
|
|
/* KDK = HMAC-SHA-256_DHKey(N1 || EnrolleeMAC || N2) */
|
|
|
|
addr[0] = wps->nonce_e;
|
|
|
|
len[0] = WPS_NONCE_LEN;
|
|
|
|
addr[1] = wps->mac_addr_e;
|
|
|
|
len[1] = ETH_ALEN;
|
|
|
|
addr[2] = wps->nonce_r;
|
|
|
|
len[2] = WPS_NONCE_LEN;
|
|
|
|
hmac_sha256_vector(dhkey, sizeof(dhkey), 3, addr, len, kdk);
|
|
|
|
wpa_hexdump_key(MSG_DEBUG, "WPS: KDK", kdk, sizeof(kdk));
|
|
|
|
|
2008-11-29 11:11:56 +01:00
|
|
|
wps_kdf(kdk, NULL, 0, "Wi-Fi Easy and Secure Key Derivation",
|
2008-11-23 18:34:26 +01:00
|
|
|
keys, sizeof(keys));
|
|
|
|
os_memcpy(wps->authkey, keys, WPS_AUTHKEY_LEN);
|
|
|
|
os_memcpy(wps->keywrapkey, keys + WPS_AUTHKEY_LEN, WPS_KEYWRAPKEY_LEN);
|
|
|
|
os_memcpy(wps->emsk, keys + WPS_AUTHKEY_LEN + WPS_KEYWRAPKEY_LEN,
|
|
|
|
WPS_EMSK_LEN);
|
|
|
|
|
|
|
|
wpa_hexdump_key(MSG_DEBUG, "WPS: AuthKey",
|
|
|
|
wps->authkey, WPS_AUTHKEY_LEN);
|
|
|
|
wpa_hexdump_key(MSG_DEBUG, "WPS: KeyWrapKey",
|
|
|
|
wps->keywrapkey, WPS_KEYWRAPKEY_LEN);
|
|
|
|
wpa_hexdump_key(MSG_DEBUG, "WPS: EMSK", wps->emsk, WPS_EMSK_LEN);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void wps_derive_psk(struct wps_data *wps, const u8 *dev_passwd,
|
|
|
|
size_t dev_passwd_len)
|
|
|
|
{
|
|
|
|
u8 hash[SHA256_MAC_LEN];
|
|
|
|
|
|
|
|
hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN, dev_passwd,
|
|
|
|
(dev_passwd_len + 1) / 2, hash);
|
|
|
|
os_memcpy(wps->psk1, hash, WPS_PSK_LEN);
|
|
|
|
hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN,
|
|
|
|
dev_passwd + (dev_passwd_len + 1) / 2,
|
|
|
|
dev_passwd_len / 2, hash);
|
|
|
|
os_memcpy(wps->psk2, hash, WPS_PSK_LEN);
|
|
|
|
|
|
|
|
wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Device Password",
|
|
|
|
dev_passwd, dev_passwd_len);
|
|
|
|
wpa_hexdump_key(MSG_DEBUG, "WPS: PSK1", wps->psk1, WPS_PSK_LEN);
|
|
|
|
wpa_hexdump_key(MSG_DEBUG, "WPS: PSK2", wps->psk2, WPS_PSK_LEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct wpabuf * wps_decrypt_encr_settings(struct wps_data *wps, const u8 *encr,
|
|
|
|
size_t encr_len)
|
|
|
|
{
|
|
|
|
struct wpabuf *decrypted;
|
|
|
|
const size_t block_size = 16;
|
|
|
|
size_t i;
|
|
|
|
u8 pad;
|
|
|
|
const u8 *pos;
|
|
|
|
|
|
|
|
/* AES-128-CBC */
|
|
|
|
if (encr == NULL || encr_len < 2 * block_size || encr_len % block_size)
|
|
|
|
{
|
|
|
|
wpa_printf(MSG_DEBUG, "WPS: No Encrypted Settings received");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
decrypted = wpabuf_alloc(encr_len - block_size);
|
|
|
|
if (decrypted == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
wpa_hexdump(MSG_MSGDUMP, "WPS: Encrypted Settings", encr, encr_len);
|
|
|
|
wpabuf_put_data(decrypted, encr + block_size, encr_len - block_size);
|
|
|
|
if (aes_128_cbc_decrypt(wps->keywrapkey, encr, wpabuf_mhead(decrypted),
|
|
|
|
wpabuf_len(decrypted))) {
|
|
|
|
wpabuf_free(decrypted);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
wpa_hexdump_buf_key(MSG_MSGDUMP, "WPS: Decrypted Encrypted Settings",
|
|
|
|
decrypted);
|
|
|
|
|
|
|
|
pos = wpabuf_head_u8(decrypted) + wpabuf_len(decrypted) - 1;
|
|
|
|
pad = *pos;
|
|
|
|
if (pad > wpabuf_len(decrypted)) {
|
|
|
|
wpa_printf(MSG_DEBUG, "WPS: Invalid PKCS#5 v2.0 pad value");
|
|
|
|
wpabuf_free(decrypted);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
for (i = 0; i < pad; i++) {
|
|
|
|
if (*pos-- != pad) {
|
|
|
|
wpa_printf(MSG_DEBUG, "WPS: Invalid PKCS#5 v2.0 pad "
|
|
|
|
"string");
|
|
|
|
wpabuf_free(decrypted);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
decrypted->used -= pad;
|
|
|
|
|
|
|
|
return decrypted;
|
|
|
|
}
|
2008-11-29 13:02:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* wps_pin_checksum - Compute PIN checksum
|
|
|
|
* @pin: Seven digit PIN (i.e., eight digit PIN without the checksum digit)
|
|
|
|
* Returns: Checksum digit
|
|
|
|
*/
|
|
|
|
unsigned int wps_pin_checksum(unsigned int pin)
|
|
|
|
{
|
|
|
|
unsigned int accum = 0;
|
|
|
|
while (pin) {
|
|
|
|
accum += 3 * (pin % 10);
|
|
|
|
pin /= 10;
|
|
|
|
accum += pin % 10;
|
|
|
|
pin /= 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (10 - accum % 10) % 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* wps_pin_valid - Check whether a PIN has a valid checksum
|
|
|
|
* @pin: Eight digit PIN (i.e., including the checksum digit)
|
|
|
|
* Returns: 1 if checksum digit is valid, or 0 if not
|
|
|
|
*/
|
|
|
|
unsigned int wps_pin_valid(unsigned int pin)
|
|
|
|
{
|
|
|
|
return wps_pin_checksum(pin / 10) == (pin % 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* wps_generate_pin - Generate a random PIN
|
|
|
|
* Returns: Eight digit PIN (i.e., including the checksum digit)
|
|
|
|
*/
|
|
|
|
unsigned int wps_generate_pin(void)
|
|
|
|
{
|
|
|
|
unsigned int val;
|
|
|
|
|
|
|
|
/* Generate seven random digits for the PIN */
|
2010-11-24 00:05:20 +01:00
|
|
|
if (random_get_bytes((unsigned char *) &val, sizeof(val)) < 0) {
|
2008-11-29 13:02:09 +01:00
|
|
|
struct os_time now;
|
|
|
|
os_get_time(&now);
|
|
|
|
val = os_random() ^ now.sec ^ now.usec;
|
|
|
|
}
|
|
|
|
val %= 10000000;
|
|
|
|
|
|
|
|
/* Append checksum digit */
|
|
|
|
return val * 10 + wps_pin_checksum(val);
|
|
|
|
}
|
2008-12-19 21:19:41 +01:00
|
|
|
|
|
|
|
|
2012-06-30 20:27:32 +02:00
|
|
|
int wps_pin_str_valid(const char *pin)
|
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
p = pin;
|
|
|
|
while (*p >= '0' && *p <= '9')
|
|
|
|
p++;
|
|
|
|
if (*p != '\0')
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
len = p - pin;
|
|
|
|
return len == 4 || len == 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-14 19:49:54 +02:00
|
|
|
void wps_fail_event(struct wps_context *wps, enum wps_msg_type msg,
|
2011-01-13 16:50:59 +01:00
|
|
|
u16 config_error, u16 error_indication)
|
2008-12-19 21:19:41 +01:00
|
|
|
{
|
|
|
|
union wps_event_data data;
|
|
|
|
|
|
|
|
if (wps->event_cb == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
os_memset(&data, 0, sizeof(data));
|
|
|
|
data.fail.msg = msg;
|
2010-10-14 19:49:54 +02:00
|
|
|
data.fail.config_error = config_error;
|
2011-01-13 16:50:59 +01:00
|
|
|
data.fail.error_indication = error_indication;
|
2008-12-19 21:19:41 +01:00
|
|
|
wps->event_cb(wps->cb_ctx, WPS_EV_FAIL, &data);
|
|
|
|
}
|
2008-12-19 21:34:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
void wps_success_event(struct wps_context *wps)
|
|
|
|
{
|
|
|
|
if (wps->event_cb == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
wps->event_cb(wps->cb_ctx, WPS_EV_SUCCESS, NULL);
|
|
|
|
}
|
2009-01-23 20:57:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
void wps_pwd_auth_fail_event(struct wps_context *wps, int enrollee, int part)
|
|
|
|
{
|
|
|
|
union wps_event_data data;
|
|
|
|
|
|
|
|
if (wps->event_cb == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
os_memset(&data, 0, sizeof(data));
|
|
|
|
data.pwd_auth_fail.enrollee = enrollee;
|
|
|
|
data.pwd_auth_fail.part = part;
|
|
|
|
wps->event_cb(wps->cb_ctx, WPS_EV_PWD_AUTH_FAIL, &data);
|
|
|
|
}
|
2009-02-26 20:57:38 +01:00
|
|
|
|
|
|
|
|
2009-11-01 20:26:13 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-23 15:01:30 +02:00
|
|
|
void wps_pbc_active_event(struct wps_context *wps)
|
|
|
|
{
|
|
|
|
if (wps->event_cb == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
wps->event_cb(wps->cb_ctx, WPS_EV_PBC_ACTIVE, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void wps_pbc_disable_event(struct wps_context *wps)
|
|
|
|
{
|
|
|
|
if (wps->event_cb == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
wps->event_cb(wps->cb_ctx, WPS_EV_PBC_DISABLE, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-26 21:10:21 +01:00
|
|
|
#ifdef CONFIG_WPS_OOB
|
|
|
|
|
2012-06-28 12:25:48 +02:00
|
|
|
struct wpabuf * wps_get_oob_cred(struct wps_context *wps)
|
2009-02-26 20:57:38 +01:00
|
|
|
{
|
|
|
|
struct wps_data data;
|
|
|
|
struct wpabuf *plain;
|
|
|
|
|
|
|
|
plain = wpabuf_alloc(500);
|
|
|
|
if (plain == NULL) {
|
|
|
|
wpa_printf(MSG_ERROR, "WPS: Failed to allocate memory for OOB "
|
|
|
|
"credential");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
os_memset(&data, 0, sizeof(data));
|
|
|
|
data.wps = wps;
|
|
|
|
data.auth_type = wps->auth_types;
|
|
|
|
data.encr_type = wps->encr_types;
|
2009-12-11 17:00:10 +01:00
|
|
|
if (wps_build_version(plain) ||
|
|
|
|
wps_build_cred(&data, plain) ||
|
2010-09-20 02:18:43 +02:00
|
|
|
wps_build_wfa_ext(plain, 0, NULL, 0)) {
|
2013-04-04 16:49:53 +02:00
|
|
|
os_free(data.new_psk);
|
2009-02-26 20:57:38 +01:00
|
|
|
wpabuf_free(plain);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-04-04 16:49:53 +02:00
|
|
|
if (wps->wps_state == WPS_STATE_NOT_CONFIGURED && data.new_psk &&
|
|
|
|
wps->ap) {
|
|
|
|
struct wps_credential cred;
|
|
|
|
|
|
|
|
wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
|
|
|
|
"on credential token generation");
|
|
|
|
|
|
|
|
os_memset(&cred, 0, sizeof(cred));
|
|
|
|
os_memcpy(cred.ssid, wps->ssid, wps->ssid_len);
|
|
|
|
cred.ssid_len = wps->ssid_len;
|
|
|
|
cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
|
|
|
|
cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
|
|
|
|
os_memcpy(cred.key, data.new_psk, data.new_psk_len);
|
|
|
|
cred.key_len = data.new_psk_len;
|
|
|
|
|
|
|
|
wps->wps_state = WPS_STATE_CONFIGURED;
|
|
|
|
wpa_hexdump_ascii_key(MSG_DEBUG,
|
|
|
|
"WPS: Generated random passphrase",
|
|
|
|
data.new_psk, data.new_psk_len);
|
|
|
|
if (wps->cred_cb)
|
|
|
|
wps->cred_cb(wps->cb_ctx, &cred);
|
|
|
|
}
|
|
|
|
|
|
|
|
os_free(data.new_psk);
|
|
|
|
|
2009-02-26 20:57:38 +01:00
|
|
|
return plain;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-27 17:56:41 +02:00
|
|
|
struct wpabuf * wps_build_nfc_pw_token(u16 dev_pw_id,
|
|
|
|
const struct wpabuf *pubkey,
|
|
|
|
const struct wpabuf *dev_pw)
|
|
|
|
{
|
|
|
|
struct wpabuf *data;
|
|
|
|
|
|
|
|
data = wpabuf_alloc(200);
|
|
|
|
if (data == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (wps_build_version(data) ||
|
|
|
|
wps_build_oob_dev_pw(data, dev_pw_id, pubkey,
|
|
|
|
wpabuf_head(dev_pw), wpabuf_len(dev_pw)) ||
|
|
|
|
wps_build_wfa_ext(data, 0, NULL, 0)) {
|
|
|
|
wpa_printf(MSG_ERROR, "WPS: Failed to build NFC password "
|
|
|
|
"token");
|
|
|
|
wpabuf_free(data);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-28 12:59:29 +02:00
|
|
|
int wps_oob_use_cred(struct wps_context *wps, struct wps_parse_attr *attr)
|
2009-02-26 20:57:38 +01:00
|
|
|
{
|
|
|
|
struct wpabuf msg;
|
|
|
|
size_t i;
|
|
|
|
|
2012-06-28 12:59:29 +02:00
|
|
|
for (i = 0; i < attr->num_cred; i++) {
|
2009-02-26 20:57:38 +01:00
|
|
|
struct wps_credential local_cred;
|
|
|
|
struct wps_parse_attr cattr;
|
|
|
|
|
|
|
|
os_memset(&local_cred, 0, sizeof(local_cred));
|
2012-06-28 12:59:29 +02:00
|
|
|
wpabuf_set(&msg, attr->cred[i], attr->cred_len[i]);
|
2009-02-26 20:57:38 +01:00
|
|
|
if (wps_parse_msg(&msg, &cattr) < 0 ||
|
|
|
|
wps_process_cred(&cattr, &local_cred)) {
|
|
|
|
wpa_printf(MSG_ERROR, "WPS: Failed to parse OOB "
|
|
|
|
"credential");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
wps->cred_cb(wps->cb_ctx, &local_cred);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-26 21:10:21 +01:00
|
|
|
#endif /* CONFIG_WPS_OOB */
|
2009-11-26 10:39:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2009-11-29 12:15:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2009-12-21 14:59:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
u16 wps_config_methods_str2bin(const char *str)
|
|
|
|
{
|
|
|
|
u16 methods = 0;
|
|
|
|
|
|
|
|
if (str == NULL) {
|
|
|
|
/* Default to enabling methods based on build configuration */
|
2010-04-03 18:35:42 +02:00
|
|
|
methods |= WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD;
|
2010-06-18 03:35:18 +02:00
|
|
|
#ifdef CONFIG_WPS2
|
2009-12-23 00:21:12 +01:00
|
|
|
methods |= WPS_CONFIG_VIRT_DISPLAY;
|
2010-06-18 03:35:18 +02:00
|
|
|
#endif /* CONFIG_WPS2 */
|
2009-12-21 14:59:25 +01:00
|
|
|
#ifdef CONFIG_WPS_NFC
|
|
|
|
methods |= WPS_CONFIG_NFC_INTERFACE;
|
|
|
|
#endif /* CONFIG_WPS_NFC */
|
|
|
|
} else {
|
|
|
|
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;
|
2010-06-18 03:35:18 +02:00
|
|
|
#ifdef CONFIG_WPS2
|
2009-12-23 00:21:12 +01:00
|
|
|
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;
|
2010-06-18 03:35:18 +02:00
|
|
|
#endif /* CONFIG_WPS2 */
|
2009-12-21 14:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return methods;
|
|
|
|
}
|
2010-11-04 17:16:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
|
|
|
|
{
|
|
|
|
struct wpabuf *msg;
|
|
|
|
|
|
|
|
wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
|
|
|
|
|
|
|
|
msg = wpabuf_alloc(1000);
|
|
|
|
if (msg == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (wps_build_version(msg) ||
|
|
|
|
wps_build_msg_type(msg, WPS_WSC_ACK) ||
|
|
|
|
wps_build_enrollee_nonce(wps, msg) ||
|
|
|
|
wps_build_registrar_nonce(wps, msg) ||
|
|
|
|
wps_build_wfa_ext(msg, 0, NULL, 0)) {
|
|
|
|
wpabuf_free(msg);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
|
|
|
|
{
|
|
|
|
struct wpabuf *msg;
|
|
|
|
|
|
|
|
wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
|
|
|
|
|
|
|
|
msg = wpabuf_alloc(1000);
|
|
|
|
if (msg == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (wps_build_version(msg) ||
|
|
|
|
wps_build_msg_type(msg, WPS_WSC_NACK) ||
|
|
|
|
wps_build_enrollee_nonce(wps, msg) ||
|
|
|
|
wps_build_registrar_nonce(wps, msg) ||
|
|
|
|
wps_build_config_error(msg, wps->config_error) ||
|
|
|
|
wps_build_wfa_ext(msg, 0, NULL, 0)) {
|
|
|
|
wpabuf_free(msg);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
2012-06-28 19:54:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
#ifdef CONFIG_WPS_NFC
|
2013-02-15 10:24:29 +01:00
|
|
|
|
|
|
|
struct wpabuf * wps_nfc_token_build(int ndef, int id, struct wpabuf *pubkey,
|
|
|
|
struct wpabuf *dev_pw)
|
|
|
|
{
|
|
|
|
struct wpabuf *ret;
|
|
|
|
|
|
|
|
if (pubkey == NULL || dev_pw == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ret = wps_build_nfc_pw_token(id, pubkey, dev_pw);
|
|
|
|
if (ndef && ret) {
|
|
|
|
struct wpabuf *tmp;
|
|
|
|
tmp = ndef_build_wifi(ret);
|
|
|
|
wpabuf_free(ret);
|
|
|
|
if (tmp == NULL)
|
|
|
|
return NULL;
|
|
|
|
ret = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-28 19:54:53 +02:00
|
|
|
struct wpabuf * wps_nfc_token_gen(int ndef, int *id, struct wpabuf **pubkey,
|
|
|
|
struct wpabuf **privkey,
|
|
|
|
struct wpabuf **dev_pw)
|
|
|
|
{
|
2013-02-15 10:24:29 +01:00
|
|
|
struct wpabuf *priv = NULL, *pub = NULL, *pw;
|
2012-06-28 19:54:53 +02:00
|
|
|
void *dh_ctx;
|
|
|
|
u16 val;
|
|
|
|
|
|
|
|
pw = wpabuf_alloc(WPS_OOB_DEVICE_PASSWORD_LEN);
|
|
|
|
if (pw == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (random_get_bytes(wpabuf_put(pw, WPS_OOB_DEVICE_PASSWORD_LEN),
|
|
|
|
WPS_OOB_DEVICE_PASSWORD_LEN) ||
|
|
|
|
random_get_bytes((u8 *) &val, sizeof(val))) {
|
|
|
|
wpabuf_free(pw);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
dh_ctx = dh5_init(&priv, &pub);
|
|
|
|
if (dh_ctx == NULL) {
|
|
|
|
wpabuf_free(pw);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
dh5_free(dh_ctx);
|
|
|
|
|
|
|
|
*id = 0x10 + val % 0xfff0;
|
|
|
|
wpabuf_free(*pubkey);
|
|
|
|
*pubkey = pub;
|
|
|
|
wpabuf_free(*privkey);
|
|
|
|
*privkey = priv;
|
|
|
|
wpabuf_free(*dev_pw);
|
|
|
|
*dev_pw = pw;
|
|
|
|
|
2013-02-15 10:24:29 +01:00
|
|
|
return wps_nfc_token_build(ndef, *id, *pubkey, *dev_pw);
|
2012-06-28 19:54:53 +02:00
|
|
|
}
|
2013-02-15 10:24:29 +01:00
|
|
|
|
2012-06-28 19:54:53 +02:00
|
|
|
#endif /* CONFIG_WPS_NFC */
|