1194676Sthompsa/* 2194676Sthompsa * Wi-Fi Protected Setup - common functionality 3194676Sthompsa * Copyright (c) 2008-2012, Jouni Malinen <j@w1.fi> 4194676Sthompsa * 5194676Sthompsa * This software may be distributed under the terms of the BSD license. 6194676Sthompsa * See README for more details. 7194676Sthompsa */ 8194676Sthompsa 9194676Sthompsa#include "includes.h" 10194676Sthompsa 11194676Sthompsa#include "common.h" 12194676Sthompsa#include "crypto/aes_wrap.h" 13194676Sthompsa#include "crypto/crypto.h" 14194676Sthompsa#include "crypto/dh_group5.h" 15194676Sthompsa#include "crypto/sha1.h" 16194676Sthompsa#include "crypto/sha256.h" 17194676Sthompsa#include "crypto/random.h" 18194676Sthompsa#include "wps_i.h" 19194676Sthompsa 20194676Sthompsa 21194676Sthompsavoid wps_kdf(const u8 *key, const u8 *label_prefix, size_t label_prefix_len, 22194676Sthompsa const char *label, u8 *res, size_t res_len) 23194676Sthompsa{ 24194676Sthompsa u8 i_buf[4], key_bits[4]; 25194676Sthompsa const u8 *addr[4]; 26194676Sthompsa size_t len[4]; 27194676Sthompsa int i, iter; 28194676Sthompsa u8 hash[SHA256_MAC_LEN], *opos; 29194676Sthompsa size_t left; 30195560Sthompsa 31195560Sthompsa WPA_PUT_BE32(key_bits, res_len * 8); 32195560Sthompsa 33195560Sthompsa addr[0] = i_buf; 34195560Sthompsa len[0] = sizeof(i_buf); 35194676Sthompsa addr[1] = label_prefix; 36194676Sthompsa len[1] = label_prefix_len; 37194676Sthompsa addr[2] = (const u8 *) label; 38194676Sthompsa len[2] = os_strlen(label); 39194676Sthompsa addr[3] = key_bits; 40194676Sthompsa len[3] = sizeof(key_bits); 41194676Sthompsa 42194676Sthompsa iter = (res_len + SHA256_MAC_LEN - 1) / SHA256_MAC_LEN; 43194676Sthompsa opos = res; 44194676Sthompsa left = res_len; 45194676Sthompsa 46194676Sthompsa for (i = 1; i <= iter; i++) { 47194676Sthompsa WPA_PUT_BE32(i_buf, i); 48194676Sthompsa hmac_sha256_vector(key, SHA256_MAC_LEN, 4, addr, len, hash); 49194676Sthompsa if (i < iter) { 50194676Sthompsa os_memcpy(opos, hash, SHA256_MAC_LEN); 51194676Sthompsa opos += SHA256_MAC_LEN; 52194676Sthompsa left -= SHA256_MAC_LEN; 53194676Sthompsa } else 54194676Sthompsa os_memcpy(opos, hash, left); 55194676Sthompsa } 56194676Sthompsa} 57194676Sthompsa 58194676Sthompsa 59194676Sthompsaint wps_derive_keys(struct wps_data *wps) 60194676Sthompsa{ 61194676Sthompsa struct wpabuf *pubkey, *dh_shared; 62194676Sthompsa u8 dhkey[SHA256_MAC_LEN], kdk[SHA256_MAC_LEN]; 63194676Sthompsa const u8 *addr[3]; 64194676Sthompsa size_t len[3]; 65194676Sthompsa u8 keys[WPS_AUTHKEY_LEN + WPS_KEYWRAPKEY_LEN + WPS_EMSK_LEN]; 66194676Sthompsa 67194676Sthompsa if (wps->dh_privkey == NULL) { 68194676Sthompsa wpa_printf(MSG_DEBUG, "WPS: Own DH private key not available"); 69194676Sthompsa return -1; 70194676Sthompsa } 71194676Sthompsa 72194676Sthompsa pubkey = wps->registrar ? wps->dh_pubkey_e : wps->dh_pubkey_r; 73194676Sthompsa if (pubkey == NULL) { 74194676Sthompsa wpa_printf(MSG_DEBUG, "WPS: Peer DH public key not available"); 75194676Sthompsa return -1; 76194676Sthompsa } 77194676Sthompsa 78194676Sthompsa wpa_hexdump_buf_key(MSG_DEBUG, "WPS: DH Private Key", wps->dh_privkey); 79194676Sthompsa wpa_hexdump_buf(MSG_DEBUG, "WPS: DH peer Public Key", pubkey); 80194676Sthompsa dh_shared = dh5_derive_shared(wps->dh_ctx, pubkey, wps->dh_privkey); 81194676Sthompsa dh5_free(wps->dh_ctx); 82194676Sthompsa wps->dh_ctx = NULL; 83194676Sthompsa dh_shared = wpabuf_zeropad(dh_shared, 192); 84194676Sthompsa if (dh_shared == NULL) { 85194676Sthompsa wpa_printf(MSG_DEBUG, "WPS: Failed to derive DH shared key"); 86194676Sthompsa return -1; 87194676Sthompsa } 88194676Sthompsa 89194676Sthompsa /* Own DH private key is not needed anymore */ 90194676Sthompsa wpabuf_free(wps->dh_privkey); 91194676Sthompsa wps->dh_privkey = NULL; 92194676Sthompsa 93194676Sthompsa wpa_hexdump_buf_key(MSG_DEBUG, "WPS: DH shared key", dh_shared); 94194676Sthompsa 95194676Sthompsa /* DHKey = SHA-256(g^AB mod p) */ 96194676Sthompsa addr[0] = wpabuf_head(dh_shared); 97194676Sthompsa len[0] = wpabuf_len(dh_shared); 98194676Sthompsa sha256_vector(1, addr, len, dhkey); 99194676Sthompsa wpa_hexdump_key(MSG_DEBUG, "WPS: DHKey", dhkey, sizeof(dhkey)); 100194676Sthompsa wpabuf_free(dh_shared); 101194676Sthompsa 102194676Sthompsa /* KDK = HMAC-SHA-256_DHKey(N1 || EnrolleeMAC || N2) */ 103194676Sthompsa addr[0] = wps->nonce_e; 104194676Sthompsa len[0] = WPS_NONCE_LEN; 105194676Sthompsa addr[1] = wps->mac_addr_e; 106194676Sthompsa len[1] = ETH_ALEN; 107194676Sthompsa addr[2] = wps->nonce_r; 108194676Sthompsa len[2] = WPS_NONCE_LEN; 109194676Sthompsa hmac_sha256_vector(dhkey, sizeof(dhkey), 3, addr, len, kdk); 110194676Sthompsa wpa_hexdump_key(MSG_DEBUG, "WPS: KDK", kdk, sizeof(kdk)); 111194676Sthompsa 112194676Sthompsa wps_kdf(kdk, NULL, 0, "Wi-Fi Easy and Secure Key Derivation", 113194676Sthompsa keys, sizeof(keys)); 114194676Sthompsa os_memcpy(wps->authkey, keys, WPS_AUTHKEY_LEN); 115194676Sthompsa os_memcpy(wps->keywrapkey, keys + WPS_AUTHKEY_LEN, WPS_KEYWRAPKEY_LEN); 116194676Sthompsa os_memcpy(wps->emsk, keys + WPS_AUTHKEY_LEN + WPS_KEYWRAPKEY_LEN, 117194676Sthompsa WPS_EMSK_LEN); 118194676Sthompsa 119194676Sthompsa wpa_hexdump_key(MSG_DEBUG, "WPS: AuthKey", 120194676Sthompsa wps->authkey, WPS_AUTHKEY_LEN); 121194676Sthompsa wpa_hexdump_key(MSG_DEBUG, "WPS: KeyWrapKey", 122194676Sthompsa wps->keywrapkey, WPS_KEYWRAPKEY_LEN); 123194676Sthompsa wpa_hexdump_key(MSG_DEBUG, "WPS: EMSK", wps->emsk, WPS_EMSK_LEN); 124194676Sthompsa 125194676Sthompsa return 0; 126194676Sthompsa} 127194676Sthompsa 128194676Sthompsa 129194676Sthompsavoid wps_derive_psk(struct wps_data *wps, const u8 *dev_passwd, 130194676Sthompsa size_t dev_passwd_len) 131194676Sthompsa{ 132194676Sthompsa u8 hash[SHA256_MAC_LEN]; 133194676Sthompsa 134194676Sthompsa hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN, dev_passwd, 135194676Sthompsa (dev_passwd_len + 1) / 2, hash); 136194676Sthompsa os_memcpy(wps->psk1, hash, WPS_PSK_LEN); 137194676Sthompsa hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN, 138194676Sthompsa dev_passwd + (dev_passwd_len + 1) / 2, 139194676Sthompsa dev_passwd_len / 2, hash); 140194676Sthompsa os_memcpy(wps->psk2, hash, WPS_PSK_LEN); 141194676Sthompsa 142194676Sthompsa wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Device Password", 143194676Sthompsa dev_passwd, dev_passwd_len); 144194676Sthompsa wpa_hexdump_key(MSG_DEBUG, "WPS: PSK1", wps->psk1, WPS_PSK_LEN); 145194676Sthompsa wpa_hexdump_key(MSG_DEBUG, "WPS: PSK2", wps->psk2, WPS_PSK_LEN); 146194676Sthompsa} 147194676Sthompsa 148194676Sthompsa 149194676Sthompsastruct wpabuf * wps_decrypt_encr_settings(struct wps_data *wps, const u8 *encr, 150194676Sthompsa size_t encr_len) 151194676Sthompsa{ 152194676Sthompsa struct wpabuf *decrypted; 153194676Sthompsa const size_t block_size = 16; 154194676Sthompsa size_t i; 155194676Sthompsa u8 pad; 156194676Sthompsa const u8 *pos; 157194676Sthompsa 158194676Sthompsa /* AES-128-CBC */ 159194676Sthompsa if (encr == NULL || encr_len < 2 * block_size || encr_len % block_size) 160194676Sthompsa { 161194676Sthompsa wpa_printf(MSG_DEBUG, "WPS: No Encrypted Settings received"); 162194676Sthompsa return NULL; 163194676Sthompsa } 164194676Sthompsa 165194676Sthompsa decrypted = wpabuf_alloc(encr_len - block_size); 166194676Sthompsa if (decrypted == NULL) 167194676Sthompsa return NULL; 168194676Sthompsa 169194676Sthompsa wpa_hexdump(MSG_MSGDUMP, "WPS: Encrypted Settings", encr, encr_len); 170194676Sthompsa wpabuf_put_data(decrypted, encr + block_size, encr_len - block_size); 171194676Sthompsa if (aes_128_cbc_decrypt(wps->keywrapkey, encr, wpabuf_mhead(decrypted), 172194676Sthompsa wpabuf_len(decrypted))) { 173194676Sthompsa wpabuf_free(decrypted); 174194676Sthompsa return NULL; 175194676Sthompsa } 176194676Sthompsa 177194676Sthompsa wpa_hexdump_buf_key(MSG_MSGDUMP, "WPS: Decrypted Encrypted Settings", 178194676Sthompsa decrypted); 179194676Sthompsa 180194676Sthompsa pos = wpabuf_head_u8(decrypted) + wpabuf_len(decrypted) - 1; 181194676Sthompsa pad = *pos; 182194676Sthompsa if (pad > wpabuf_len(decrypted)) { 183194676Sthompsa wpa_printf(MSG_DEBUG, "WPS: Invalid PKCS#5 v2.0 pad value"); 184195560Sthompsa wpabuf_free(decrypted); 185195560Sthompsa return NULL; 186195560Sthompsa } 187195560Sthompsa for (i = 0; i < pad; i++) { 188195560Sthompsa if (*pos-- != pad) { 189195560Sthompsa wpa_printf(MSG_DEBUG, "WPS: Invalid PKCS#5 v2.0 pad " 190195560Sthompsa "string"); 191195560Sthompsa wpabuf_free(decrypted); 192195560Sthompsa return NULL; 193195560Sthompsa } 194195560Sthompsa } 195195560Sthompsa decrypted->used -= pad; 196195560Sthompsa 197195560Sthompsa return decrypted; 198195560Sthompsa} 199195560Sthompsa 200195560Sthompsa 201195560Sthompsa/** 202195560Sthompsa * wps_pin_checksum - Compute PIN checksum 203195560Sthompsa * @pin: Seven digit PIN (i.e., eight digit PIN without the checksum digit) 204195560Sthompsa * Returns: Checksum digit 205195560Sthompsa */ 206195560Sthompsaunsigned int wps_pin_checksum(unsigned int pin) 207195560Sthompsa{ 208195560Sthompsa unsigned int accum = 0; 209195560Sthompsa while (pin) { 210195560Sthompsa accum += 3 * (pin % 10); 211194676Sthompsa pin /= 10; 212194676Sthompsa accum += pin % 10; 213194676Sthompsa pin /= 10; 214194676Sthompsa } 215194676Sthompsa 216194676Sthompsa return (10 - accum % 10) % 10; 217194676Sthompsa} 218194676Sthompsa 219194676Sthompsa 220194676Sthompsa/** 221194676Sthompsa * wps_pin_valid - Check whether a PIN has a valid checksum 222195560Sthompsa * @pin: Eight digit PIN (i.e., including the checksum digit) 223194676Sthompsa * Returns: 1 if checksum digit is valid, or 0 if not 224194676Sthompsa */ 225195560Sthompsaunsigned int wps_pin_valid(unsigned int pin) 226194676Sthompsa{ 227194676Sthompsa return wps_pin_checksum(pin / 10) == (pin % 10); 228195560Sthompsa} 229194676Sthompsa 230194676Sthompsa 231195560Sthompsa/** 232194676Sthompsa * wps_generate_pin - Generate a random PIN 233194676Sthompsa * Returns: Eight digit PIN (i.e., including the checksum digit) 234194676Sthompsa */ 235194676Sthompsaunsigned int wps_generate_pin(void) 236194676Sthompsa{ 237194676Sthompsa unsigned int val; 238194676Sthompsa 239194676Sthompsa /* Generate seven random digits for the PIN */ 240194676Sthompsa if (random_get_bytes((unsigned char *) &val, sizeof(val)) < 0) { 241194676Sthompsa struct os_time now; 242194676Sthompsa os_get_time(&now); 243194676Sthompsa val = os_random() ^ now.sec ^ now.usec; 244194676Sthompsa } 245194676Sthompsa val %= 10000000; 246194676Sthompsa 247194676Sthompsa /* Append checksum digit */ 248194676Sthompsa return val * 10 + wps_pin_checksum(val); 249194676Sthompsa} 250194676Sthompsa 251194676Sthompsa 252194676Sthompsaint wps_pin_str_valid(const char *pin) 253194676Sthompsa{ 254194676Sthompsa const char *p; 255194676Sthompsa size_t len; 256194676Sthompsa 257194676Sthompsa p = pin; 258195560Sthompsa while (*p >= '0' && *p <= '9') 259194676Sthompsa p++; 260194676Sthompsa if (*p != '\0') 261194676Sthompsa return 0; 262194676Sthompsa 263194676Sthompsa len = p - pin; 264194676Sthompsa return len == 4 || len == 8; 265194676Sthompsa} 266194676Sthompsa 267195560Sthompsa 268194676Sthompsavoid wps_fail_event(struct wps_context *wps, enum wps_msg_type msg, 269194676Sthompsa u16 config_error, u16 error_indication) 270195560Sthompsa{ 271194676Sthompsa union wps_event_data data; 272194676Sthompsa 273194676Sthompsa if (wps->event_cb == NULL) 274194676Sthompsa return; 275194676Sthompsa 276194676Sthompsa os_memset(&data, 0, sizeof(data)); 277194676Sthompsa data.fail.msg = msg; 278194676Sthompsa data.fail.config_error = config_error; 279194676Sthompsa data.fail.error_indication = error_indication; 280194676Sthompsa wps->event_cb(wps->cb_ctx, WPS_EV_FAIL, &data); 281194676Sthompsa} 282194676Sthompsa 283194676Sthompsa 284194676Sthompsavoid wps_success_event(struct wps_context *wps) 285194676Sthompsa{ 286194676Sthompsa if (wps->event_cb == NULL) 287194676Sthompsa return; 288194676Sthompsa 289194676Sthompsa wps->event_cb(wps->cb_ctx, WPS_EV_SUCCESS, NULL); 290194676Sthompsa} 291194676Sthompsa 292194676Sthompsa 293194676Sthompsavoid wps_pwd_auth_fail_event(struct wps_context *wps, int enrollee, int part) 294194676Sthompsa{ 295194676Sthompsa union wps_event_data data; 296194676Sthompsa 297194676Sthompsa if (wps->event_cb == NULL) 298194676Sthompsa return; 299194676Sthompsa 300194676Sthompsa os_memset(&data, 0, sizeof(data)); 301194676Sthompsa data.pwd_auth_fail.enrollee = enrollee; 302194676Sthompsa data.pwd_auth_fail.part = part; 303194676Sthompsa wps->event_cb(wps->cb_ctx, WPS_EV_PWD_AUTH_FAIL, &data); 304194676Sthompsa} 305194676Sthompsa 306194676Sthompsa 307194676Sthompsavoid wps_pbc_overlap_event(struct wps_context *wps) 308194676Sthompsa{ 309194676Sthompsa if (wps->event_cb == NULL) 310194676Sthompsa return; 311194676Sthompsa 312194676Sthompsa wps->event_cb(wps->cb_ctx, WPS_EV_PBC_OVERLAP, NULL); 313194676Sthompsa} 314194676Sthompsa 315194676Sthompsa 316194676Sthompsavoid wps_pbc_timeout_event(struct wps_context *wps) 317194676Sthompsa{ 318194676Sthompsa if (wps->event_cb == NULL) 319194676Sthompsa return; 320194676Sthompsa 321194676Sthompsa wps->event_cb(wps->cb_ctx, WPS_EV_PBC_TIMEOUT, NULL); 322194676Sthompsa} 323194676Sthompsa 324194676Sthompsa 325194676Sthompsa#ifdef CONFIG_WPS_OOB 326194676Sthompsa 327194676Sthompsastruct wpabuf * wps_get_oob_cred(struct wps_context *wps) 328194676Sthompsa{ 329194676Sthompsa struct wps_data data; 330194676Sthompsa struct wpabuf *plain; 331194676Sthompsa 332194676Sthompsa plain = wpabuf_alloc(500); 333194676Sthompsa if (plain == NULL) { 334194676Sthompsa wpa_printf(MSG_ERROR, "WPS: Failed to allocate memory for OOB " 335194676Sthompsa "credential"); 336194676Sthompsa return NULL; 337194676Sthompsa } 338194676Sthompsa 339194676Sthompsa os_memset(&data, 0, sizeof(data)); 340194676Sthompsa data.wps = wps; 341194676Sthompsa data.auth_type = wps->auth_types; 342194676Sthompsa data.encr_type = wps->encr_types; 343194676Sthompsa if (wps_build_version(plain) || 344194676Sthompsa wps_build_cred(&data, plain) || 345194676Sthompsa wps_build_wfa_ext(plain, 0, NULL, 0)) { 346194676Sthompsa wpabuf_free(plain); 347194676Sthompsa return NULL; 348194676Sthompsa } 349194676Sthompsa 350194676Sthompsa return plain; 351194676Sthompsa} 352194676Sthompsa 353194676Sthompsa 354194676Sthompsastruct wpabuf * wps_build_nfc_pw_token(u16 dev_pw_id, 355194676Sthompsa const struct wpabuf *pubkey, 356194676Sthompsa const struct wpabuf *dev_pw) 357194676Sthompsa{ 358194676Sthompsa struct wpabuf *data; 359194676Sthompsa 360194676Sthompsa data = wpabuf_alloc(200); 361194676Sthompsa if (data == NULL) 362194676Sthompsa return NULL; 363194676Sthompsa 364194676Sthompsa if (wps_build_version(data) || 365194676Sthompsa wps_build_oob_dev_pw(data, dev_pw_id, pubkey, 366194676Sthompsa wpabuf_head(dev_pw), wpabuf_len(dev_pw)) || 367194676Sthompsa wps_build_wfa_ext(data, 0, NULL, 0)) { 368194676Sthompsa wpa_printf(MSG_ERROR, "WPS: Failed to build NFC password " 369194676Sthompsa "token"); 370194676Sthompsa wpabuf_free(data); 371194676Sthompsa return NULL; 372194676Sthompsa } 373194676Sthompsa 374194676Sthompsa return data; 375194676Sthompsa} 376194676Sthompsa 377194676Sthompsa 378194676Sthompsaint wps_oob_use_cred(struct wps_context *wps, struct wps_parse_attr *attr) 379194676Sthompsa{ 380194676Sthompsa struct wpabuf msg; 381194676Sthompsa size_t i; 382194676Sthompsa 383194676Sthompsa for (i = 0; i < attr->num_cred; i++) { 384195560Sthompsa struct wps_credential local_cred; 385194676Sthompsa struct wps_parse_attr cattr; 386194676Sthompsa 387194676Sthompsa os_memset(&local_cred, 0, sizeof(local_cred)); 388194676Sthompsa wpabuf_set(&msg, attr->cred[i], attr->cred_len[i]); 389194676Sthompsa if (wps_parse_msg(&msg, &cattr) < 0 || 390194676Sthompsa wps_process_cred(&cattr, &local_cred)) { 391194676Sthompsa wpa_printf(MSG_ERROR, "WPS: Failed to parse OOB " 392194676Sthompsa "credential"); 393194676Sthompsa return -1; 394194676Sthompsa } 395194676Sthompsa wps->cred_cb(wps->cb_ctx, &local_cred); 396194676Sthompsa } 397194676Sthompsa 398194676Sthompsa return 0; 399194676Sthompsa} 400194676Sthompsa 401194676Sthompsa 402194676Sthompsa#endif /* CONFIG_WPS_OOB */ 403194676Sthompsa 404194676Sthompsa 405194676Sthompsaint wps_dev_type_str2bin(const char *str, u8 dev_type[WPS_DEV_TYPE_LEN]) 406194676Sthompsa{ 407194676Sthompsa const char *pos; 408194676Sthompsa 409194676Sthompsa /* <categ>-<OUI>-<subcateg> */ 410194676Sthompsa WPA_PUT_BE16(dev_type, atoi(str)); 411194676Sthompsa pos = os_strchr(str, '-'); 412194676Sthompsa if (pos == NULL) 413194676Sthompsa return -1; 414194676Sthompsa pos++; 415194676Sthompsa if (hexstr2bin(pos, &dev_type[2], 4)) 416194676Sthompsa return -1; 417194676Sthompsa pos = os_strchr(pos, '-'); 418194676Sthompsa if (pos == NULL) 419194676Sthompsa return -1; 420194676Sthompsa pos++; 421194676Sthompsa WPA_PUT_BE16(&dev_type[6], atoi(pos)); 422194676Sthompsa 423194676Sthompsa 424194676Sthompsa return 0; 425194676Sthompsa} 426194676Sthompsa 427194676Sthompsa 428194676Sthompsachar * wps_dev_type_bin2str(const u8 dev_type[WPS_DEV_TYPE_LEN], char *buf, 429194676Sthompsa size_t buf_len) 430194676Sthompsa{ 431194676Sthompsa int ret; 432194676Sthompsa 433194676Sthompsa ret = os_snprintf(buf, buf_len, "%u-%08X-%u", 434194676Sthompsa WPA_GET_BE16(dev_type), WPA_GET_BE32(&dev_type[2]), 435194676Sthompsa WPA_GET_BE16(&dev_type[6])); 436194676Sthompsa if (ret < 0 || (unsigned int) ret >= buf_len) 437194676Sthompsa return NULL; 438194676Sthompsa 439194676Sthompsa return buf; 440194676Sthompsa} 441194676Sthompsa 442194676Sthompsa 443194676Sthompsavoid uuid_gen_mac_addr(const u8 *mac_addr, u8 *uuid) 444194676Sthompsa{ 445194676Sthompsa const u8 *addr[2]; 446194676Sthompsa size_t len[2]; 447194676Sthompsa u8 hash[SHA1_MAC_LEN]; 448194676Sthompsa u8 nsid[16] = { 449194676Sthompsa 0x52, 0x64, 0x80, 0xf8, 450 0xc9, 0x9b, 451 0x4b, 0xe5, 452 0xa6, 0x55, 453 0x58, 0xed, 0x5f, 0x5d, 0x60, 0x84 454 }; 455 456 addr[0] = nsid; 457 len[0] = sizeof(nsid); 458 addr[1] = mac_addr; 459 len[1] = 6; 460 sha1_vector(2, addr, len, hash); 461 os_memcpy(uuid, hash, 16); 462 463 /* Version: 5 = named-based version using SHA-1 */ 464 uuid[6] = (5 << 4) | (uuid[6] & 0x0f); 465 466 /* Variant specified in RFC 4122 */ 467 uuid[8] = 0x80 | (uuid[8] & 0x3f); 468} 469 470 471u16 wps_config_methods_str2bin(const char *str) 472{ 473 u16 methods = 0; 474 475 if (str == NULL) { 476 /* Default to enabling methods based on build configuration */ 477 methods |= WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD; 478#ifdef CONFIG_WPS2 479 methods |= WPS_CONFIG_VIRT_DISPLAY; 480#endif /* CONFIG_WPS2 */ 481#ifdef CONFIG_WPS_NFC 482 methods |= WPS_CONFIG_NFC_INTERFACE; 483#endif /* CONFIG_WPS_NFC */ 484 } else { 485 if (os_strstr(str, "ethernet")) 486 methods |= WPS_CONFIG_ETHERNET; 487 if (os_strstr(str, "label")) 488 methods |= WPS_CONFIG_LABEL; 489 if (os_strstr(str, "display")) 490 methods |= WPS_CONFIG_DISPLAY; 491 if (os_strstr(str, "ext_nfc_token")) 492 methods |= WPS_CONFIG_EXT_NFC_TOKEN; 493 if (os_strstr(str, "int_nfc_token")) 494 methods |= WPS_CONFIG_INT_NFC_TOKEN; 495 if (os_strstr(str, "nfc_interface")) 496 methods |= WPS_CONFIG_NFC_INTERFACE; 497 if (os_strstr(str, "push_button")) 498 methods |= WPS_CONFIG_PUSHBUTTON; 499 if (os_strstr(str, "keypad")) 500 methods |= WPS_CONFIG_KEYPAD; 501#ifdef CONFIG_WPS2 502 if (os_strstr(str, "virtual_display")) 503 methods |= WPS_CONFIG_VIRT_DISPLAY; 504 if (os_strstr(str, "physical_display")) 505 methods |= WPS_CONFIG_PHY_DISPLAY; 506 if (os_strstr(str, "virtual_push_button")) 507 methods |= WPS_CONFIG_VIRT_PUSHBUTTON; 508 if (os_strstr(str, "physical_push_button")) 509 methods |= WPS_CONFIG_PHY_PUSHBUTTON; 510#endif /* CONFIG_WPS2 */ 511 } 512 513 return methods; 514} 515 516 517struct wpabuf * wps_build_wsc_ack(struct wps_data *wps) 518{ 519 struct wpabuf *msg; 520 521 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK"); 522 523 msg = wpabuf_alloc(1000); 524 if (msg == NULL) 525 return NULL; 526 527 if (wps_build_version(msg) || 528 wps_build_msg_type(msg, WPS_WSC_ACK) || 529 wps_build_enrollee_nonce(wps, msg) || 530 wps_build_registrar_nonce(wps, msg) || 531 wps_build_wfa_ext(msg, 0, NULL, 0)) { 532 wpabuf_free(msg); 533 return NULL; 534 } 535 536 return msg; 537} 538 539 540struct wpabuf * wps_build_wsc_nack(struct wps_data *wps) 541{ 542 struct wpabuf *msg; 543 544 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK"); 545 546 msg = wpabuf_alloc(1000); 547 if (msg == NULL) 548 return NULL; 549 550 if (wps_build_version(msg) || 551 wps_build_msg_type(msg, WPS_WSC_NACK) || 552 wps_build_enrollee_nonce(wps, msg) || 553 wps_build_registrar_nonce(wps, msg) || 554 wps_build_config_error(msg, wps->config_error) || 555 wps_build_wfa_ext(msg, 0, NULL, 0)) { 556 wpabuf_free(msg); 557 return NULL; 558 } 559 560 return msg; 561} 562 563 564#ifdef CONFIG_WPS_NFC 565struct wpabuf * wps_nfc_token_gen(int ndef, int *id, struct wpabuf **pubkey, 566 struct wpabuf **privkey, 567 struct wpabuf **dev_pw) 568{ 569 struct wpabuf *priv = NULL, *pub = NULL, *pw, *ret; 570 void *dh_ctx; 571 u16 val; 572 573 pw = wpabuf_alloc(WPS_OOB_DEVICE_PASSWORD_LEN); 574 if (pw == NULL) 575 return NULL; 576 577 if (random_get_bytes(wpabuf_put(pw, WPS_OOB_DEVICE_PASSWORD_LEN), 578 WPS_OOB_DEVICE_PASSWORD_LEN) || 579 random_get_bytes((u8 *) &val, sizeof(val))) { 580 wpabuf_free(pw); 581 return NULL; 582 } 583 584 dh_ctx = dh5_init(&priv, &pub); 585 if (dh_ctx == NULL) { 586 wpabuf_free(pw); 587 return NULL; 588 } 589 dh5_free(dh_ctx); 590 591 *id = 0x10 + val % 0xfff0; 592 wpabuf_free(*pubkey); 593 *pubkey = pub; 594 wpabuf_free(*privkey); 595 *privkey = priv; 596 wpabuf_free(*dev_pw); 597 *dev_pw = pw; 598 599 ret = wps_build_nfc_pw_token(*id, *pubkey, *dev_pw); 600 if (ndef && ret) { 601 struct wpabuf *tmp; 602 tmp = ndef_build_wifi(ret); 603 wpabuf_free(ret); 604 if (tmp == NULL) 605 return NULL; 606 ret = tmp; 607 } 608 609 return ret; 610} 611#endif /* CONFIG_WPS_NFC */ 612