Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Wi-Fi Protected Setup - common functionality |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3 | * Copyright (c) 2008-2012, Jouni Malinen <j@w1.fi> |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4 | * |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5 | * This software may be distributed under the terms of the BSD license. |
| 6 | * See README for more details. |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include "includes.h" |
| 10 | |
| 11 | #include "common.h" |
| 12 | #include "crypto/aes_wrap.h" |
| 13 | #include "crypto/crypto.h" |
| 14 | #include "crypto/dh_group5.h" |
| 15 | #include "crypto/sha1.h" |
| 16 | #include "crypto/sha256.h" |
| 17 | #include "crypto/random.h" |
| 18 | #include "wps_i.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 19 | |
| 20 | |
| 21 | void wps_kdf(const u8 *key, const u8 *label_prefix, size_t label_prefix_len, |
| 22 | const char *label, u8 *res, size_t res_len) |
| 23 | { |
| 24 | u8 i_buf[4], key_bits[4]; |
| 25 | const u8 *addr[4]; |
| 26 | size_t len[4]; |
| 27 | int i, iter; |
| 28 | u8 hash[SHA256_MAC_LEN], *opos; |
| 29 | size_t left; |
| 30 | |
| 31 | WPA_PUT_BE32(key_bits, res_len * 8); |
| 32 | |
| 33 | addr[0] = i_buf; |
| 34 | len[0] = sizeof(i_buf); |
| 35 | addr[1] = label_prefix; |
| 36 | len[1] = label_prefix_len; |
| 37 | addr[2] = (const u8 *) label; |
| 38 | len[2] = os_strlen(label); |
| 39 | addr[3] = key_bits; |
| 40 | len[3] = sizeof(key_bits); |
| 41 | |
| 42 | iter = (res_len + SHA256_MAC_LEN - 1) / SHA256_MAC_LEN; |
| 43 | opos = res; |
| 44 | left = res_len; |
| 45 | |
| 46 | for (i = 1; i <= iter; i++) { |
| 47 | WPA_PUT_BE32(i_buf, i); |
| 48 | hmac_sha256_vector(key, SHA256_MAC_LEN, 4, addr, len, hash); |
| 49 | if (i < iter) { |
| 50 | os_memcpy(opos, hash, SHA256_MAC_LEN); |
| 51 | opos += SHA256_MAC_LEN; |
| 52 | left -= SHA256_MAC_LEN; |
| 53 | } else |
| 54 | os_memcpy(opos, hash, left); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | |
| 59 | int wps_derive_keys(struct wps_data *wps) |
| 60 | { |
| 61 | struct wpabuf *pubkey, *dh_shared; |
| 62 | u8 dhkey[SHA256_MAC_LEN], kdk[SHA256_MAC_LEN]; |
| 63 | const u8 *addr[3]; |
| 64 | size_t len[3]; |
| 65 | u8 keys[WPS_AUTHKEY_LEN + WPS_KEYWRAPKEY_LEN + WPS_EMSK_LEN]; |
| 66 | |
| 67 | if (wps->dh_privkey == NULL) { |
| 68 | wpa_printf(MSG_DEBUG, "WPS: Own DH private key not available"); |
| 69 | return -1; |
| 70 | } |
| 71 | |
| 72 | pubkey = wps->registrar ? wps->dh_pubkey_e : wps->dh_pubkey_r; |
| 73 | if (pubkey == NULL) { |
| 74 | wpa_printf(MSG_DEBUG, "WPS: Peer DH public key not available"); |
| 75 | return -1; |
| 76 | } |
| 77 | |
| 78 | wpa_hexdump_buf_key(MSG_DEBUG, "WPS: DH Private Key", wps->dh_privkey); |
| 79 | wpa_hexdump_buf(MSG_DEBUG, "WPS: DH peer Public Key", pubkey); |
| 80 | dh_shared = dh5_derive_shared(wps->dh_ctx, pubkey, wps->dh_privkey); |
| 81 | dh5_free(wps->dh_ctx); |
| 82 | wps->dh_ctx = NULL; |
| 83 | dh_shared = wpabuf_zeropad(dh_shared, 192); |
| 84 | if (dh_shared == NULL) { |
| 85 | wpa_printf(MSG_DEBUG, "WPS: Failed to derive DH shared key"); |
| 86 | return -1; |
| 87 | } |
| 88 | |
| 89 | /* Own DH private key is not needed anymore */ |
| 90 | wpabuf_free(wps->dh_privkey); |
| 91 | wps->dh_privkey = NULL; |
| 92 | |
| 93 | wpa_hexdump_buf_key(MSG_DEBUG, "WPS: DH shared key", dh_shared); |
| 94 | |
| 95 | /* DHKey = SHA-256(g^AB mod p) */ |
| 96 | addr[0] = wpabuf_head(dh_shared); |
| 97 | len[0] = wpabuf_len(dh_shared); |
| 98 | sha256_vector(1, addr, len, dhkey); |
| 99 | wpa_hexdump_key(MSG_DEBUG, "WPS: DHKey", dhkey, sizeof(dhkey)); |
| 100 | wpabuf_free(dh_shared); |
| 101 | |
| 102 | /* KDK = HMAC-SHA-256_DHKey(N1 || EnrolleeMAC || N2) */ |
| 103 | addr[0] = wps->nonce_e; |
| 104 | len[0] = WPS_NONCE_LEN; |
| 105 | addr[1] = wps->mac_addr_e; |
| 106 | len[1] = ETH_ALEN; |
| 107 | addr[2] = wps->nonce_r; |
| 108 | len[2] = WPS_NONCE_LEN; |
| 109 | hmac_sha256_vector(dhkey, sizeof(dhkey), 3, addr, len, kdk); |
| 110 | wpa_hexdump_key(MSG_DEBUG, "WPS: KDK", kdk, sizeof(kdk)); |
| 111 | |
| 112 | wps_kdf(kdk, NULL, 0, "Wi-Fi Easy and Secure Key Derivation", |
| 113 | keys, sizeof(keys)); |
| 114 | os_memcpy(wps->authkey, keys, WPS_AUTHKEY_LEN); |
| 115 | os_memcpy(wps->keywrapkey, keys + WPS_AUTHKEY_LEN, WPS_KEYWRAPKEY_LEN); |
| 116 | os_memcpy(wps->emsk, keys + WPS_AUTHKEY_LEN + WPS_KEYWRAPKEY_LEN, |
| 117 | WPS_EMSK_LEN); |
| 118 | |
| 119 | wpa_hexdump_key(MSG_DEBUG, "WPS: AuthKey", |
| 120 | wps->authkey, WPS_AUTHKEY_LEN); |
| 121 | wpa_hexdump_key(MSG_DEBUG, "WPS: KeyWrapKey", |
| 122 | wps->keywrapkey, WPS_KEYWRAPKEY_LEN); |
| 123 | wpa_hexdump_key(MSG_DEBUG, "WPS: EMSK", wps->emsk, WPS_EMSK_LEN); |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | |
| 129 | void wps_derive_psk(struct wps_data *wps, const u8 *dev_passwd, |
| 130 | size_t dev_passwd_len) |
| 131 | { |
| 132 | u8 hash[SHA256_MAC_LEN]; |
| 133 | |
| 134 | hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN, dev_passwd, |
| 135 | (dev_passwd_len + 1) / 2, hash); |
| 136 | os_memcpy(wps->psk1, hash, WPS_PSK_LEN); |
| 137 | hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN, |
| 138 | dev_passwd + (dev_passwd_len + 1) / 2, |
| 139 | dev_passwd_len / 2, hash); |
| 140 | os_memcpy(wps->psk2, hash, WPS_PSK_LEN); |
| 141 | |
| 142 | wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Device Password", |
| 143 | dev_passwd, dev_passwd_len); |
| 144 | wpa_hexdump_key(MSG_DEBUG, "WPS: PSK1", wps->psk1, WPS_PSK_LEN); |
| 145 | wpa_hexdump_key(MSG_DEBUG, "WPS: PSK2", wps->psk2, WPS_PSK_LEN); |
| 146 | } |
| 147 | |
| 148 | |
| 149 | struct wpabuf * wps_decrypt_encr_settings(struct wps_data *wps, const u8 *encr, |
| 150 | size_t encr_len) |
| 151 | { |
| 152 | struct wpabuf *decrypted; |
| 153 | const size_t block_size = 16; |
| 154 | size_t i; |
| 155 | u8 pad; |
| 156 | const u8 *pos; |
| 157 | |
| 158 | /* AES-128-CBC */ |
| 159 | if (encr == NULL || encr_len < 2 * block_size || encr_len % block_size) |
| 160 | { |
| 161 | wpa_printf(MSG_DEBUG, "WPS: No Encrypted Settings received"); |
| 162 | return NULL; |
| 163 | } |
| 164 | |
| 165 | decrypted = wpabuf_alloc(encr_len - block_size); |
| 166 | if (decrypted == NULL) |
| 167 | return NULL; |
| 168 | |
| 169 | wpa_hexdump(MSG_MSGDUMP, "WPS: Encrypted Settings", encr, encr_len); |
| 170 | wpabuf_put_data(decrypted, encr + block_size, encr_len - block_size); |
| 171 | if (aes_128_cbc_decrypt(wps->keywrapkey, encr, wpabuf_mhead(decrypted), |
| 172 | wpabuf_len(decrypted))) { |
| 173 | wpabuf_free(decrypted); |
| 174 | return NULL; |
| 175 | } |
| 176 | |
| 177 | wpa_hexdump_buf_key(MSG_MSGDUMP, "WPS: Decrypted Encrypted Settings", |
| 178 | decrypted); |
| 179 | |
| 180 | pos = wpabuf_head_u8(decrypted) + wpabuf_len(decrypted) - 1; |
| 181 | pad = *pos; |
| 182 | if (pad > wpabuf_len(decrypted)) { |
| 183 | wpa_printf(MSG_DEBUG, "WPS: Invalid PKCS#5 v2.0 pad value"); |
| 184 | wpabuf_free(decrypted); |
| 185 | return NULL; |
| 186 | } |
| 187 | for (i = 0; i < pad; i++) { |
| 188 | if (*pos-- != pad) { |
| 189 | wpa_printf(MSG_DEBUG, "WPS: Invalid PKCS#5 v2.0 pad " |
| 190 | "string"); |
| 191 | wpabuf_free(decrypted); |
| 192 | return NULL; |
| 193 | } |
| 194 | } |
| 195 | decrypted->used -= pad; |
| 196 | |
| 197 | return decrypted; |
| 198 | } |
| 199 | |
| 200 | |
| 201 | /** |
| 202 | * wps_pin_checksum - Compute PIN checksum |
| 203 | * @pin: Seven digit PIN (i.e., eight digit PIN without the checksum digit) |
| 204 | * Returns: Checksum digit |
| 205 | */ |
| 206 | unsigned int wps_pin_checksum(unsigned int pin) |
| 207 | { |
| 208 | unsigned int accum = 0; |
| 209 | while (pin) { |
| 210 | accum += 3 * (pin % 10); |
| 211 | pin /= 10; |
| 212 | accum += pin % 10; |
| 213 | pin /= 10; |
| 214 | } |
| 215 | |
| 216 | return (10 - accum % 10) % 10; |
| 217 | } |
| 218 | |
| 219 | |
| 220 | /** |
| 221 | * wps_pin_valid - Check whether a PIN has a valid checksum |
| 222 | * @pin: Eight digit PIN (i.e., including the checksum digit) |
| 223 | * Returns: 1 if checksum digit is valid, or 0 if not |
| 224 | */ |
| 225 | unsigned int wps_pin_valid(unsigned int pin) |
| 226 | { |
| 227 | return wps_pin_checksum(pin / 10) == (pin % 10); |
| 228 | } |
| 229 | |
| 230 | |
| 231 | /** |
| 232 | * wps_generate_pin - Generate a random PIN |
| 233 | * Returns: Eight digit PIN (i.e., including the checksum digit) |
| 234 | */ |
| 235 | unsigned int wps_generate_pin(void) |
| 236 | { |
| 237 | unsigned int val; |
| 238 | |
| 239 | /* Generate seven random digits for the PIN */ |
| 240 | if (random_get_bytes((unsigned char *) &val, sizeof(val)) < 0) { |
| 241 | struct os_time now; |
| 242 | os_get_time(&now); |
| 243 | val = os_random() ^ now.sec ^ now.usec; |
| 244 | } |
| 245 | val %= 10000000; |
| 246 | |
| 247 | /* Append checksum digit */ |
| 248 | return val * 10 + wps_pin_checksum(val); |
| 249 | } |
| 250 | |
| 251 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 252 | int wps_pin_str_valid(const char *pin) |
| 253 | { |
| 254 | const char *p; |
| 255 | size_t len; |
| 256 | |
| 257 | p = pin; |
| 258 | while (*p >= '0' && *p <= '9') |
| 259 | p++; |
| 260 | if (*p != '\0') |
| 261 | return 0; |
| 262 | |
| 263 | len = p - pin; |
| 264 | return len == 4 || len == 8; |
| 265 | } |
| 266 | |
| 267 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 268 | void wps_fail_event(struct wps_context *wps, enum wps_msg_type msg, |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 269 | u16 config_error, u16 error_indication, const u8 *mac_addr) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 270 | { |
| 271 | union wps_event_data data; |
| 272 | |
| 273 | if (wps->event_cb == NULL) |
| 274 | return; |
| 275 | |
| 276 | os_memset(&data, 0, sizeof(data)); |
| 277 | data.fail.msg = msg; |
| 278 | data.fail.config_error = config_error; |
| 279 | data.fail.error_indication = error_indication; |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 280 | os_memcpy(data.fail.peer_macaddr, mac_addr, ETH_ALEN); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 281 | wps->event_cb(wps->cb_ctx, WPS_EV_FAIL, &data); |
| 282 | } |
| 283 | |
| 284 | |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 285 | void wps_success_event(struct wps_context *wps, const u8 *mac_addr) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 286 | { |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 287 | union wps_event_data data; |
| 288 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 289 | if (wps->event_cb == NULL) |
| 290 | return; |
| 291 | |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 292 | os_memset(&data, 0, sizeof(data)); |
| 293 | os_memcpy(data.success.peer_macaddr, mac_addr, ETH_ALEN); |
| 294 | wps->event_cb(wps->cb_ctx, WPS_EV_SUCCESS, &data); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 298 | void wps_pwd_auth_fail_event(struct wps_context *wps, int enrollee, int part, |
| 299 | const u8 *mac_addr) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 300 | { |
| 301 | union wps_event_data data; |
| 302 | |
| 303 | if (wps->event_cb == NULL) |
| 304 | return; |
| 305 | |
| 306 | os_memset(&data, 0, sizeof(data)); |
| 307 | data.pwd_auth_fail.enrollee = enrollee; |
| 308 | data.pwd_auth_fail.part = part; |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 309 | os_memcpy(data.pwd_auth_fail.peer_macaddr, mac_addr, ETH_ALEN); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 310 | wps->event_cb(wps->cb_ctx, WPS_EV_PWD_AUTH_FAIL, &data); |
| 311 | } |
| 312 | |
| 313 | |
| 314 | void wps_pbc_overlap_event(struct wps_context *wps) |
| 315 | { |
| 316 | if (wps->event_cb == NULL) |
| 317 | return; |
| 318 | |
| 319 | wps->event_cb(wps->cb_ctx, WPS_EV_PBC_OVERLAP, NULL); |
| 320 | } |
| 321 | |
| 322 | |
| 323 | void wps_pbc_timeout_event(struct wps_context *wps) |
| 324 | { |
| 325 | if (wps->event_cb == NULL) |
| 326 | return; |
| 327 | |
| 328 | wps->event_cb(wps->cb_ctx, WPS_EV_PBC_TIMEOUT, NULL); |
| 329 | } |
| 330 | |
| 331 | |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 332 | void wps_pbc_active_event(struct wps_context *wps) |
| 333 | { |
| 334 | if (wps->event_cb == NULL) |
| 335 | return; |
| 336 | |
| 337 | wps->event_cb(wps->cb_ctx, WPS_EV_PBC_ACTIVE, NULL); |
| 338 | } |
| 339 | |
| 340 | |
| 341 | void wps_pbc_disable_event(struct wps_context *wps) |
| 342 | { |
| 343 | if (wps->event_cb == NULL) |
| 344 | return; |
| 345 | |
| 346 | wps->event_cb(wps->cb_ctx, WPS_EV_PBC_DISABLE, NULL); |
| 347 | } |
| 348 | |
| 349 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 350 | #ifdef CONFIG_WPS_OOB |
| 351 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 352 | struct wpabuf * wps_get_oob_cred(struct wps_context *wps) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 353 | { |
| 354 | struct wps_data data; |
| 355 | struct wpabuf *plain; |
| 356 | |
| 357 | plain = wpabuf_alloc(500); |
| 358 | if (plain == NULL) { |
| 359 | wpa_printf(MSG_ERROR, "WPS: Failed to allocate memory for OOB " |
| 360 | "credential"); |
| 361 | return NULL; |
| 362 | } |
| 363 | |
| 364 | os_memset(&data, 0, sizeof(data)); |
| 365 | data.wps = wps; |
| 366 | data.auth_type = wps->auth_types; |
| 367 | data.encr_type = wps->encr_types; |
| 368 | if (wps_build_version(plain) || |
| 369 | wps_build_cred(&data, plain) || |
| 370 | wps_build_wfa_ext(plain, 0, NULL, 0)) { |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 371 | os_free(data.new_psk); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 372 | wpabuf_free(plain); |
| 373 | return NULL; |
| 374 | } |
| 375 | |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 376 | if (wps->wps_state == WPS_STATE_NOT_CONFIGURED && data.new_psk && |
| 377 | wps->ap) { |
| 378 | struct wps_credential cred; |
| 379 | |
| 380 | wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based " |
| 381 | "on credential token generation"); |
| 382 | |
| 383 | os_memset(&cred, 0, sizeof(cred)); |
| 384 | os_memcpy(cred.ssid, wps->ssid, wps->ssid_len); |
| 385 | cred.ssid_len = wps->ssid_len; |
| 386 | cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK; |
| 387 | cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES; |
| 388 | os_memcpy(cred.key, data.new_psk, data.new_psk_len); |
| 389 | cred.key_len = data.new_psk_len; |
| 390 | |
| 391 | wps->wps_state = WPS_STATE_CONFIGURED; |
| 392 | wpa_hexdump_ascii_key(MSG_DEBUG, |
| 393 | "WPS: Generated random passphrase", |
| 394 | data.new_psk, data.new_psk_len); |
| 395 | if (wps->cred_cb) |
| 396 | wps->cred_cb(wps->cb_ctx, &cred); |
| 397 | } |
| 398 | |
| 399 | os_free(data.new_psk); |
| 400 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 401 | return plain; |
| 402 | } |
| 403 | |
| 404 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 405 | struct wpabuf * wps_build_nfc_pw_token(u16 dev_pw_id, |
| 406 | const struct wpabuf *pubkey, |
| 407 | const struct wpabuf *dev_pw) |
| 408 | { |
| 409 | struct wpabuf *data; |
| 410 | |
| 411 | data = wpabuf_alloc(200); |
| 412 | if (data == NULL) |
| 413 | return NULL; |
| 414 | |
| 415 | if (wps_build_version(data) || |
| 416 | wps_build_oob_dev_pw(data, dev_pw_id, pubkey, |
| 417 | wpabuf_head(dev_pw), wpabuf_len(dev_pw)) || |
| 418 | wps_build_wfa_ext(data, 0, NULL, 0)) { |
| 419 | wpa_printf(MSG_ERROR, "WPS: Failed to build NFC password " |
| 420 | "token"); |
| 421 | wpabuf_free(data); |
| 422 | return NULL; |
| 423 | } |
| 424 | |
| 425 | return data; |
| 426 | } |
| 427 | |
| 428 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 429 | int wps_oob_use_cred(struct wps_context *wps, struct wps_parse_attr *attr) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 430 | { |
| 431 | struct wpabuf msg; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 432 | size_t i; |
| 433 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 434 | for (i = 0; i < attr->num_cred; i++) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 435 | struct wps_credential local_cred; |
| 436 | struct wps_parse_attr cattr; |
| 437 | |
| 438 | os_memset(&local_cred, 0, sizeof(local_cred)); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 439 | wpabuf_set(&msg, attr->cred[i], attr->cred_len[i]); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 440 | if (wps_parse_msg(&msg, &cattr) < 0 || |
| 441 | wps_process_cred(&cattr, &local_cred)) { |
| 442 | wpa_printf(MSG_ERROR, "WPS: Failed to parse OOB " |
| 443 | "credential"); |
| 444 | return -1; |
| 445 | } |
| 446 | wps->cred_cb(wps->cb_ctx, &local_cred); |
| 447 | } |
| 448 | |
| 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 453 | #endif /* CONFIG_WPS_OOB */ |
| 454 | |
| 455 | |
| 456 | int wps_dev_type_str2bin(const char *str, u8 dev_type[WPS_DEV_TYPE_LEN]) |
| 457 | { |
| 458 | const char *pos; |
| 459 | |
| 460 | /* <categ>-<OUI>-<subcateg> */ |
| 461 | WPA_PUT_BE16(dev_type, atoi(str)); |
| 462 | pos = os_strchr(str, '-'); |
| 463 | if (pos == NULL) |
| 464 | return -1; |
| 465 | pos++; |
| 466 | if (hexstr2bin(pos, &dev_type[2], 4)) |
| 467 | return -1; |
| 468 | pos = os_strchr(pos, '-'); |
| 469 | if (pos == NULL) |
| 470 | return -1; |
| 471 | pos++; |
| 472 | WPA_PUT_BE16(&dev_type[6], atoi(pos)); |
| 473 | |
| 474 | |
| 475 | return 0; |
| 476 | } |
| 477 | |
| 478 | |
| 479 | char * wps_dev_type_bin2str(const u8 dev_type[WPS_DEV_TYPE_LEN], char *buf, |
| 480 | size_t buf_len) |
| 481 | { |
| 482 | int ret; |
| 483 | |
| 484 | ret = os_snprintf(buf, buf_len, "%u-%08X-%u", |
| 485 | WPA_GET_BE16(dev_type), WPA_GET_BE32(&dev_type[2]), |
| 486 | WPA_GET_BE16(&dev_type[6])); |
| 487 | if (ret < 0 || (unsigned int) ret >= buf_len) |
| 488 | return NULL; |
| 489 | |
| 490 | return buf; |
| 491 | } |
| 492 | |
| 493 | |
| 494 | void uuid_gen_mac_addr(const u8 *mac_addr, u8 *uuid) |
| 495 | { |
| 496 | const u8 *addr[2]; |
| 497 | size_t len[2]; |
| 498 | u8 hash[SHA1_MAC_LEN]; |
| 499 | u8 nsid[16] = { |
| 500 | 0x52, 0x64, 0x80, 0xf8, |
| 501 | 0xc9, 0x9b, |
| 502 | 0x4b, 0xe5, |
| 503 | 0xa6, 0x55, |
| 504 | 0x58, 0xed, 0x5f, 0x5d, 0x60, 0x84 |
| 505 | }; |
| 506 | |
| 507 | addr[0] = nsid; |
| 508 | len[0] = sizeof(nsid); |
| 509 | addr[1] = mac_addr; |
| 510 | len[1] = 6; |
| 511 | sha1_vector(2, addr, len, hash); |
| 512 | os_memcpy(uuid, hash, 16); |
| 513 | |
| 514 | /* Version: 5 = named-based version using SHA-1 */ |
| 515 | uuid[6] = (5 << 4) | (uuid[6] & 0x0f); |
| 516 | |
| 517 | /* Variant specified in RFC 4122 */ |
| 518 | uuid[8] = 0x80 | (uuid[8] & 0x3f); |
| 519 | } |
| 520 | |
| 521 | |
| 522 | u16 wps_config_methods_str2bin(const char *str) |
| 523 | { |
| 524 | u16 methods = 0; |
| 525 | |
| 526 | if (str == NULL) { |
| 527 | /* Default to enabling methods based on build configuration */ |
| 528 | methods |= WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD; |
| 529 | #ifdef CONFIG_WPS2 |
| 530 | methods |= WPS_CONFIG_VIRT_DISPLAY; |
| 531 | #endif /* CONFIG_WPS2 */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 532 | #ifdef CONFIG_WPS_NFC |
| 533 | methods |= WPS_CONFIG_NFC_INTERFACE; |
| 534 | #endif /* CONFIG_WPS_NFC */ |
| 535 | } else { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 536 | if (os_strstr(str, "ethernet")) |
| 537 | methods |= WPS_CONFIG_ETHERNET; |
| 538 | if (os_strstr(str, "label")) |
| 539 | methods |= WPS_CONFIG_LABEL; |
| 540 | if (os_strstr(str, "display")) |
| 541 | methods |= WPS_CONFIG_DISPLAY; |
| 542 | if (os_strstr(str, "ext_nfc_token")) |
| 543 | methods |= WPS_CONFIG_EXT_NFC_TOKEN; |
| 544 | if (os_strstr(str, "int_nfc_token")) |
| 545 | methods |= WPS_CONFIG_INT_NFC_TOKEN; |
| 546 | if (os_strstr(str, "nfc_interface")) |
| 547 | methods |= WPS_CONFIG_NFC_INTERFACE; |
| 548 | if (os_strstr(str, "push_button")) |
| 549 | methods |= WPS_CONFIG_PUSHBUTTON; |
| 550 | if (os_strstr(str, "keypad")) |
| 551 | methods |= WPS_CONFIG_KEYPAD; |
| 552 | #ifdef CONFIG_WPS2 |
| 553 | if (os_strstr(str, "virtual_display")) |
| 554 | methods |= WPS_CONFIG_VIRT_DISPLAY; |
| 555 | if (os_strstr(str, "physical_display")) |
| 556 | methods |= WPS_CONFIG_PHY_DISPLAY; |
| 557 | if (os_strstr(str, "virtual_push_button")) |
| 558 | methods |= WPS_CONFIG_VIRT_PUSHBUTTON; |
| 559 | if (os_strstr(str, "physical_push_button")) |
| 560 | methods |= WPS_CONFIG_PHY_PUSHBUTTON; |
| 561 | #endif /* CONFIG_WPS2 */ |
| 562 | } |
| 563 | |
| 564 | return methods; |
| 565 | } |
| 566 | |
| 567 | |
| 568 | struct wpabuf * wps_build_wsc_ack(struct wps_data *wps) |
| 569 | { |
| 570 | struct wpabuf *msg; |
| 571 | |
| 572 | wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK"); |
| 573 | |
| 574 | msg = wpabuf_alloc(1000); |
| 575 | if (msg == NULL) |
| 576 | return NULL; |
| 577 | |
| 578 | if (wps_build_version(msg) || |
| 579 | wps_build_msg_type(msg, WPS_WSC_ACK) || |
| 580 | wps_build_enrollee_nonce(wps, msg) || |
| 581 | wps_build_registrar_nonce(wps, msg) || |
| 582 | wps_build_wfa_ext(msg, 0, NULL, 0)) { |
| 583 | wpabuf_free(msg); |
| 584 | return NULL; |
| 585 | } |
| 586 | |
| 587 | return msg; |
| 588 | } |
| 589 | |
| 590 | |
| 591 | struct wpabuf * wps_build_wsc_nack(struct wps_data *wps) |
| 592 | { |
| 593 | struct wpabuf *msg; |
| 594 | |
| 595 | wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK"); |
| 596 | |
| 597 | msg = wpabuf_alloc(1000); |
| 598 | if (msg == NULL) |
| 599 | return NULL; |
| 600 | |
| 601 | if (wps_build_version(msg) || |
| 602 | wps_build_msg_type(msg, WPS_WSC_NACK) || |
| 603 | wps_build_enrollee_nonce(wps, msg) || |
| 604 | wps_build_registrar_nonce(wps, msg) || |
| 605 | wps_build_config_error(msg, wps->config_error) || |
| 606 | wps_build_wfa_ext(msg, 0, NULL, 0)) { |
| 607 | wpabuf_free(msg); |
| 608 | return NULL; |
| 609 | } |
| 610 | |
| 611 | return msg; |
| 612 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 613 | |
| 614 | |
| 615 | #ifdef CONFIG_WPS_NFC |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 616 | |
| 617 | struct wpabuf * wps_nfc_token_build(int ndef, int id, struct wpabuf *pubkey, |
| 618 | struct wpabuf *dev_pw) |
| 619 | { |
| 620 | struct wpabuf *ret; |
| 621 | |
| 622 | if (pubkey == NULL || dev_pw == NULL) |
| 623 | return NULL; |
| 624 | |
| 625 | ret = wps_build_nfc_pw_token(id, pubkey, dev_pw); |
| 626 | if (ndef && ret) { |
| 627 | struct wpabuf *tmp; |
| 628 | tmp = ndef_build_wifi(ret); |
| 629 | wpabuf_free(ret); |
| 630 | if (tmp == NULL) |
| 631 | return NULL; |
| 632 | ret = tmp; |
| 633 | } |
| 634 | |
| 635 | return ret; |
| 636 | } |
| 637 | |
| 638 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 639 | struct wpabuf * wps_nfc_token_gen(int ndef, int *id, struct wpabuf **pubkey, |
| 640 | struct wpabuf **privkey, |
| 641 | struct wpabuf **dev_pw) |
| 642 | { |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 643 | struct wpabuf *priv = NULL, *pub = NULL, *pw; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 644 | void *dh_ctx; |
| 645 | u16 val; |
| 646 | |
| 647 | pw = wpabuf_alloc(WPS_OOB_DEVICE_PASSWORD_LEN); |
| 648 | if (pw == NULL) |
| 649 | return NULL; |
| 650 | |
| 651 | if (random_get_bytes(wpabuf_put(pw, WPS_OOB_DEVICE_PASSWORD_LEN), |
| 652 | WPS_OOB_DEVICE_PASSWORD_LEN) || |
| 653 | random_get_bytes((u8 *) &val, sizeof(val))) { |
| 654 | wpabuf_free(pw); |
| 655 | return NULL; |
| 656 | } |
| 657 | |
| 658 | dh_ctx = dh5_init(&priv, &pub); |
| 659 | if (dh_ctx == NULL) { |
| 660 | wpabuf_free(pw); |
| 661 | return NULL; |
| 662 | } |
| 663 | dh5_free(dh_ctx); |
| 664 | |
| 665 | *id = 0x10 + val % 0xfff0; |
| 666 | wpabuf_free(*pubkey); |
| 667 | *pubkey = pub; |
| 668 | wpabuf_free(*privkey); |
| 669 | *privkey = priv; |
| 670 | wpabuf_free(*dev_pw); |
| 671 | *dev_pw = pw; |
| 672 | |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 673 | return wps_nfc_token_build(ndef, *id, *pubkey, *dev_pw); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 674 | } |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 675 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 676 | #endif /* CONFIG_WPS_NFC */ |