wps_enrollee.c revision 96be6222df414a7dde5c5b1b72df22e67b1a77fc
1/* 2 * Wi-Fi Protected Setup - Enrollee 3 * Copyright (c) 2008, Jouni Malinen <j@w1.fi> 4 * 5 * This software may be distributed under the terms of the BSD license. 6 * See README for more details. 7 */ 8 9#include "includes.h" 10 11#include "common.h" 12#include "crypto/crypto.h" 13#include "crypto/sha256.h" 14#include "crypto/random.h" 15#include "wps_i.h" 16#include "wps_dev_attr.h" 17 18 19static int wps_build_wps_state(struct wps_data *wps, struct wpabuf *msg) 20{ 21 u8 state; 22 if (wps->wps->ap) 23 state = wps->wps->wps_state; 24 else 25 state = WPS_STATE_NOT_CONFIGURED; 26 wpa_printf(MSG_DEBUG, "WPS: * Wi-Fi Protected Setup State (%d)", 27 state); 28 wpabuf_put_be16(msg, ATTR_WPS_STATE); 29 wpabuf_put_be16(msg, 1); 30 wpabuf_put_u8(msg, state); 31 return 0; 32} 33 34 35static int wps_build_e_hash(struct wps_data *wps, struct wpabuf *msg) 36{ 37 u8 *hash; 38 const u8 *addr[4]; 39 size_t len[4]; 40 41 if (random_get_bytes(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0) 42 return -1; 43 wpa_hexdump(MSG_DEBUG, "WPS: E-S1", wps->snonce, WPS_SECRET_NONCE_LEN); 44 wpa_hexdump(MSG_DEBUG, "WPS: E-S2", 45 wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN); 46 47 if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) { 48 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for " 49 "E-Hash derivation"); 50 return -1; 51 } 52 53 wpa_printf(MSG_DEBUG, "WPS: * E-Hash1"); 54 wpabuf_put_be16(msg, ATTR_E_HASH1); 55 wpabuf_put_be16(msg, SHA256_MAC_LEN); 56 hash = wpabuf_put(msg, SHA256_MAC_LEN); 57 /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */ 58 addr[0] = wps->snonce; 59 len[0] = WPS_SECRET_NONCE_LEN; 60 addr[1] = wps->psk1; 61 len[1] = WPS_PSK_LEN; 62 addr[2] = wpabuf_head(wps->dh_pubkey_e); 63 len[2] = wpabuf_len(wps->dh_pubkey_e); 64 addr[3] = wpabuf_head(wps->dh_pubkey_r); 65 len[3] = wpabuf_len(wps->dh_pubkey_r); 66 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash); 67 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", hash, SHA256_MAC_LEN); 68 69 wpa_printf(MSG_DEBUG, "WPS: * E-Hash2"); 70 wpabuf_put_be16(msg, ATTR_E_HASH2); 71 wpabuf_put_be16(msg, SHA256_MAC_LEN); 72 hash = wpabuf_put(msg, SHA256_MAC_LEN); 73 /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */ 74 addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN; 75 addr[1] = wps->psk2; 76 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash); 77 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", hash, SHA256_MAC_LEN); 78 79 return 0; 80} 81 82 83static int wps_build_e_snonce1(struct wps_data *wps, struct wpabuf *msg) 84{ 85 wpa_printf(MSG_DEBUG, "WPS: * E-SNonce1"); 86 wpabuf_put_be16(msg, ATTR_E_SNONCE1); 87 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN); 88 wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN); 89 return 0; 90} 91 92 93static int wps_build_e_snonce2(struct wps_data *wps, struct wpabuf *msg) 94{ 95 wpa_printf(MSG_DEBUG, "WPS: * E-SNonce2"); 96 wpabuf_put_be16(msg, ATTR_E_SNONCE2); 97 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN); 98 wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN, 99 WPS_SECRET_NONCE_LEN); 100 return 0; 101} 102 103 104static struct wpabuf * wps_build_m1(struct wps_data *wps) 105{ 106 struct wpabuf *msg; 107 u16 config_methods; 108 109 if (random_get_bytes(wps->nonce_e, WPS_NONCE_LEN) < 0) 110 return NULL; 111 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce", 112 wps->nonce_e, WPS_NONCE_LEN); 113 114 wpa_printf(MSG_DEBUG, "WPS: Building Message M1"); 115 msg = wpabuf_alloc(1000); 116 if (msg == NULL) 117 return NULL; 118 119 config_methods = wps->wps->config_methods; 120 if (wps->wps->ap && !wps->pbc_in_m1 && 121 (wps->dev_password_len != 0 || 122 (config_methods & WPS_CONFIG_DISPLAY))) { 123 /* 124 * These are the methods that the AP supports as an Enrollee 125 * for adding external Registrars, so remove PushButton. 126 * 127 * As a workaround for Windows 7 mechanism for probing WPS 128 * capabilities from M1, leave PushButton option if no PIN 129 * method is available or if WPS configuration enables PBC 130 * workaround. 131 */ 132 config_methods &= ~WPS_CONFIG_PUSHBUTTON; 133#ifdef CONFIG_WPS2 134 config_methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON | 135 WPS_CONFIG_PHY_PUSHBUTTON); 136#endif /* CONFIG_WPS2 */ 137 } 138 139 if (wps_build_version(msg) || 140 wps_build_msg_type(msg, WPS_M1) || 141 wps_build_uuid_e(msg, wps->uuid_e) || 142 wps_build_mac_addr(msg, wps->mac_addr_e) || 143 wps_build_enrollee_nonce(wps, msg) || 144 wps_build_public_key(wps, msg) || 145 wps_build_auth_type_flags(wps, msg) || 146 wps_build_encr_type_flags(wps, msg) || 147 wps_build_conn_type_flags(wps, msg) || 148 wps_build_config_methods(msg, config_methods) || 149 wps_build_wps_state(wps, msg) || 150 wps_build_device_attrs(&wps->wps->dev, msg) || 151 wps_build_rf_bands(&wps->wps->dev, msg, 152 wps->wps->rf_band_cb(wps->wps->cb_ctx)) || 153 wps_build_assoc_state(wps, msg) || 154 wps_build_dev_password_id(msg, wps->dev_pw_id) || 155 wps_build_config_error(msg, WPS_CFG_NO_ERROR) || 156 wps_build_os_version(&wps->wps->dev, msg) || 157 wps_build_wfa_ext(msg, 0, NULL, 0) || 158 wps_build_vendor_ext_m1(&wps->wps->dev, msg)) { 159 wpabuf_free(msg); 160 return NULL; 161 } 162 163 wps->state = RECV_M2; 164 return msg; 165} 166 167 168static struct wpabuf * wps_build_m3(struct wps_data *wps) 169{ 170 struct wpabuf *msg; 171 172 wpa_printf(MSG_DEBUG, "WPS: Building Message M3"); 173 174 if (wps->dev_password == NULL) { 175 wpa_printf(MSG_DEBUG, "WPS: No Device Password available"); 176 return NULL; 177 } 178 wps_derive_psk(wps, wps->dev_password, wps->dev_password_len); 179 180 msg = wpabuf_alloc(1000); 181 if (msg == NULL) 182 return NULL; 183 184 if (wps_build_version(msg) || 185 wps_build_msg_type(msg, WPS_M3) || 186 wps_build_registrar_nonce(wps, msg) || 187 wps_build_e_hash(wps, msg) || 188 wps_build_wfa_ext(msg, 0, NULL, 0) || 189 wps_build_authenticator(wps, msg)) { 190 wpabuf_free(msg); 191 return NULL; 192 } 193 194 wps->state = RECV_M4; 195 return msg; 196} 197 198 199static struct wpabuf * wps_build_m5(struct wps_data *wps) 200{ 201 struct wpabuf *msg, *plain; 202 203 wpa_printf(MSG_DEBUG, "WPS: Building Message M5"); 204 205 plain = wpabuf_alloc(200); 206 if (plain == NULL) 207 return NULL; 208 209 msg = wpabuf_alloc(1000); 210 if (msg == NULL) { 211 wpabuf_free(plain); 212 return NULL; 213 } 214 215 if (wps_build_version(msg) || 216 wps_build_msg_type(msg, WPS_M5) || 217 wps_build_registrar_nonce(wps, msg) || 218 wps_build_e_snonce1(wps, plain) || 219 wps_build_key_wrap_auth(wps, plain) || 220 wps_build_encr_settings(wps, msg, plain) || 221 wps_build_wfa_ext(msg, 0, NULL, 0) || 222 wps_build_authenticator(wps, msg)) { 223 wpabuf_free(plain); 224 wpabuf_free(msg); 225 return NULL; 226 } 227 wpabuf_free(plain); 228 229 wps->state = RECV_M6; 230 return msg; 231} 232 233 234static int wps_build_cred_ssid(struct wps_data *wps, struct wpabuf *msg) 235{ 236 wpa_printf(MSG_DEBUG, "WPS: * SSID"); 237 wpabuf_put_be16(msg, ATTR_SSID); 238 wpabuf_put_be16(msg, wps->wps->ssid_len); 239 wpabuf_put_data(msg, wps->wps->ssid, wps->wps->ssid_len); 240 return 0; 241} 242 243 244static int wps_build_cred_auth_type(struct wps_data *wps, struct wpabuf *msg) 245{ 246 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type (0x%x)", 247 wps->wps->ap_auth_type); 248 wpabuf_put_be16(msg, ATTR_AUTH_TYPE); 249 wpabuf_put_be16(msg, 2); 250 wpabuf_put_be16(msg, wps->wps->ap_auth_type); 251 return 0; 252} 253 254 255static int wps_build_cred_encr_type(struct wps_data *wps, struct wpabuf *msg) 256{ 257 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type (0x%x)", 258 wps->wps->ap_encr_type); 259 wpabuf_put_be16(msg, ATTR_ENCR_TYPE); 260 wpabuf_put_be16(msg, 2); 261 wpabuf_put_be16(msg, wps->wps->ap_encr_type); 262 return 0; 263} 264 265 266static int wps_build_cred_network_key(struct wps_data *wps, struct wpabuf *msg) 267{ 268 wpa_printf(MSG_DEBUG, "WPS: * Network Key (len=%u)", 269 (unsigned int) wps->wps->network_key_len); 270 wpabuf_put_be16(msg, ATTR_NETWORK_KEY); 271 wpabuf_put_be16(msg, wps->wps->network_key_len); 272 wpabuf_put_data(msg, wps->wps->network_key, wps->wps->network_key_len); 273 return 0; 274} 275 276 277static int wps_build_cred_mac_addr(struct wps_data *wps, struct wpabuf *msg) 278{ 279 wpa_printf(MSG_DEBUG, "WPS: * MAC Address (AP BSSID)"); 280 wpabuf_put_be16(msg, ATTR_MAC_ADDR); 281 wpabuf_put_be16(msg, ETH_ALEN); 282 wpabuf_put_data(msg, wps->wps->dev.mac_addr, ETH_ALEN); 283 return 0; 284} 285 286 287static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *plain) 288{ 289 const u8 *start, *end; 290 int ret; 291 292 if (wps->wps->ap_settings) { 293 wpa_printf(MSG_DEBUG, "WPS: * AP Settings (pre-configured)"); 294 wpabuf_put_data(plain, wps->wps->ap_settings, 295 wps->wps->ap_settings_len); 296 return 0; 297 } 298 299 wpa_printf(MSG_DEBUG, "WPS: * AP Settings based on current configuration"); 300 start = wpabuf_put(plain, 0); 301 ret = wps_build_cred_ssid(wps, plain) || 302 wps_build_cred_mac_addr(wps, plain) || 303 wps_build_cred_auth_type(wps, plain) || 304 wps_build_cred_encr_type(wps, plain) || 305 wps_build_cred_network_key(wps, plain); 306 end = wpabuf_put(plain, 0); 307 308 wpa_hexdump_key(MSG_DEBUG, "WPS: Plaintext AP Settings", 309 start, end - start); 310 311 return ret; 312} 313 314 315static struct wpabuf * wps_build_m7(struct wps_data *wps) 316{ 317 struct wpabuf *msg, *plain; 318 319 wpa_printf(MSG_DEBUG, "WPS: Building Message M7"); 320 321 plain = wpabuf_alloc(500 + wps->wps->ap_settings_len); 322 if (plain == NULL) 323 return NULL; 324 325 msg = wpabuf_alloc(1000 + wps->wps->ap_settings_len); 326 if (msg == NULL) { 327 wpabuf_free(plain); 328 return NULL; 329 } 330 331 if (wps_build_version(msg) || 332 wps_build_msg_type(msg, WPS_M7) || 333 wps_build_registrar_nonce(wps, msg) || 334 wps_build_e_snonce2(wps, plain) || 335 (wps->wps->ap && wps_build_ap_settings(wps, plain)) || 336 wps_build_key_wrap_auth(wps, plain) || 337 wps_build_encr_settings(wps, msg, plain) || 338 wps_build_wfa_ext(msg, 0, NULL, 0) || 339 wps_build_authenticator(wps, msg)) { 340 wpabuf_free(plain); 341 wpabuf_free(msg); 342 return NULL; 343 } 344 wpabuf_free(plain); 345 346 if (wps->wps->ap && wps->wps->registrar) { 347 /* 348 * If the Registrar is only learning our current configuration, 349 * it may not continue protocol run to successful completion. 350 * Store information here to make sure it remains available. 351 */ 352 wps_device_store(wps->wps->registrar, &wps->peer_dev, 353 wps->uuid_r); 354 } 355 356 wps->state = RECV_M8; 357 return msg; 358} 359 360 361static struct wpabuf * wps_build_wsc_done(struct wps_data *wps) 362{ 363 struct wpabuf *msg; 364 365 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_Done"); 366 367 msg = wpabuf_alloc(1000); 368 if (msg == NULL) 369 return NULL; 370 371 if (wps_build_version(msg) || 372 wps_build_msg_type(msg, WPS_WSC_DONE) || 373 wps_build_enrollee_nonce(wps, msg) || 374 wps_build_registrar_nonce(wps, msg) || 375 wps_build_wfa_ext(msg, 0, NULL, 0)) { 376 wpabuf_free(msg); 377 return NULL; 378 } 379 380 if (wps->wps->ap) 381 wps->state = RECV_ACK; 382 else { 383 wps_success_event(wps->wps, wps->peer_dev.mac_addr); 384 wps->state = WPS_FINISHED; 385 } 386 return msg; 387} 388 389 390struct wpabuf * wps_enrollee_get_msg(struct wps_data *wps, 391 enum wsc_op_code *op_code) 392{ 393 struct wpabuf *msg; 394 395 switch (wps->state) { 396 case SEND_M1: 397 msg = wps_build_m1(wps); 398 *op_code = WSC_MSG; 399 break; 400 case SEND_M3: 401 msg = wps_build_m3(wps); 402 *op_code = WSC_MSG; 403 break; 404 case SEND_M5: 405 msg = wps_build_m5(wps); 406 *op_code = WSC_MSG; 407 break; 408 case SEND_M7: 409 msg = wps_build_m7(wps); 410 *op_code = WSC_MSG; 411 break; 412 case RECEIVED_M2D: 413 if (wps->wps->ap) { 414 msg = wps_build_wsc_nack(wps); 415 *op_code = WSC_NACK; 416 break; 417 } 418 msg = wps_build_wsc_ack(wps); 419 *op_code = WSC_ACK; 420 if (msg) { 421 /* Another M2/M2D may be received */ 422 wps->state = RECV_M2; 423 } 424 break; 425 case SEND_WSC_NACK: 426 msg = wps_build_wsc_nack(wps); 427 *op_code = WSC_NACK; 428 break; 429 case WPS_MSG_DONE: 430 msg = wps_build_wsc_done(wps); 431 *op_code = WSC_Done; 432 break; 433 default: 434 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building " 435 "a message", wps->state); 436 msg = NULL; 437 break; 438 } 439 440 if (*op_code == WSC_MSG && msg) { 441 /* Save a copy of the last message for Authenticator derivation 442 */ 443 wpabuf_free(wps->last_msg); 444 wps->last_msg = wpabuf_dup(msg); 445 } 446 447 return msg; 448} 449 450 451static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce) 452{ 453 if (r_nonce == NULL) { 454 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received"); 455 return -1; 456 } 457 458 os_memcpy(wps->nonce_r, r_nonce, WPS_NONCE_LEN); 459 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce", 460 wps->nonce_r, WPS_NONCE_LEN); 461 462 return 0; 463} 464 465 466static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce) 467{ 468 if (e_nonce == NULL) { 469 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received"); 470 return -1; 471 } 472 473 if (os_memcmp(wps->nonce_e, e_nonce, WPS_NONCE_LEN) != 0) { 474 wpa_printf(MSG_DEBUG, "WPS: Invalid Enrollee Nonce received"); 475 return -1; 476 } 477 478 return 0; 479} 480 481 482static int wps_process_uuid_r(struct wps_data *wps, const u8 *uuid_r) 483{ 484 if (uuid_r == NULL) { 485 wpa_printf(MSG_DEBUG, "WPS: No UUID-R received"); 486 return -1; 487 } 488 489 os_memcpy(wps->uuid_r, uuid_r, WPS_UUID_LEN); 490 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN); 491 492 return 0; 493} 494 495 496static int wps_process_pubkey(struct wps_data *wps, const u8 *pk, 497 size_t pk_len) 498{ 499 if (pk == NULL || pk_len == 0) { 500 wpa_printf(MSG_DEBUG, "WPS: No Public Key received"); 501 return -1; 502 } 503 504 if (wps->peer_pubkey_hash_set) { 505 u8 hash[WPS_HASH_LEN]; 506 sha256_vector(1, &pk, &pk_len, hash); 507 if (os_memcmp(hash, wps->peer_pubkey_hash, 508 WPS_OOB_PUBKEY_HASH_LEN) != 0) { 509 wpa_printf(MSG_ERROR, "WPS: Public Key hash mismatch"); 510 wpa_hexdump(MSG_DEBUG, "WPS: Received public key", 511 pk, pk_len); 512 wpa_hexdump(MSG_DEBUG, "WPS: Calculated public key " 513 "hash", hash, WPS_OOB_PUBKEY_HASH_LEN); 514 wpa_hexdump(MSG_DEBUG, "WPS: Expected public key hash", 515 wps->peer_pubkey_hash, 516 WPS_OOB_PUBKEY_HASH_LEN); 517 wps->config_error = WPS_CFG_PUBLIC_KEY_HASH_MISMATCH; 518 return -1; 519 } 520 } 521 522 wpabuf_free(wps->dh_pubkey_r); 523 wps->dh_pubkey_r = wpabuf_alloc_copy(pk, pk_len); 524 if (wps->dh_pubkey_r == NULL) 525 return -1; 526 527 if (wps_derive_keys(wps) < 0) 528 return -1; 529 530 return 0; 531} 532 533 534static int wps_process_r_hash1(struct wps_data *wps, const u8 *r_hash1) 535{ 536 if (r_hash1 == NULL) { 537 wpa_printf(MSG_DEBUG, "WPS: No R-Hash1 received"); 538 return -1; 539 } 540 541 os_memcpy(wps->peer_hash1, r_hash1, WPS_HASH_LEN); 542 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", wps->peer_hash1, WPS_HASH_LEN); 543 544 return 0; 545} 546 547 548static int wps_process_r_hash2(struct wps_data *wps, const u8 *r_hash2) 549{ 550 if (r_hash2 == NULL) { 551 wpa_printf(MSG_DEBUG, "WPS: No R-Hash2 received"); 552 return -1; 553 } 554 555 os_memcpy(wps->peer_hash2, r_hash2, WPS_HASH_LEN); 556 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", wps->peer_hash2, WPS_HASH_LEN); 557 558 return 0; 559} 560 561 562static int wps_process_r_snonce1(struct wps_data *wps, const u8 *r_snonce1) 563{ 564 u8 hash[SHA256_MAC_LEN]; 565 const u8 *addr[4]; 566 size_t len[4]; 567 568 if (r_snonce1 == NULL) { 569 wpa_printf(MSG_DEBUG, "WPS: No R-SNonce1 received"); 570 return -1; 571 } 572 573 wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce1", r_snonce1, 574 WPS_SECRET_NONCE_LEN); 575 576 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */ 577 addr[0] = r_snonce1; 578 len[0] = WPS_SECRET_NONCE_LEN; 579 addr[1] = wps->psk1; 580 len[1] = WPS_PSK_LEN; 581 addr[2] = wpabuf_head(wps->dh_pubkey_e); 582 len[2] = wpabuf_len(wps->dh_pubkey_e); 583 addr[3] = wpabuf_head(wps->dh_pubkey_r); 584 len[3] = wpabuf_len(wps->dh_pubkey_r); 585 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash); 586 587 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) { 588 wpa_printf(MSG_DEBUG, "WPS: R-Hash1 derived from R-S1 does " 589 "not match with the pre-committed value"); 590 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE; 591 wps_pwd_auth_fail_event(wps->wps, 1, 1, wps->peer_dev.mac_addr); 592 return -1; 593 } 594 595 wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the first " 596 "half of the device password"); 597 598 return 0; 599} 600 601 602static int wps_process_r_snonce2(struct wps_data *wps, const u8 *r_snonce2) 603{ 604 u8 hash[SHA256_MAC_LEN]; 605 const u8 *addr[4]; 606 size_t len[4]; 607 608 if (r_snonce2 == NULL) { 609 wpa_printf(MSG_DEBUG, "WPS: No R-SNonce2 received"); 610 return -1; 611 } 612 613 wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce2", r_snonce2, 614 WPS_SECRET_NONCE_LEN); 615 616 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */ 617 addr[0] = r_snonce2; 618 len[0] = WPS_SECRET_NONCE_LEN; 619 addr[1] = wps->psk2; 620 len[1] = WPS_PSK_LEN; 621 addr[2] = wpabuf_head(wps->dh_pubkey_e); 622 len[2] = wpabuf_len(wps->dh_pubkey_e); 623 addr[3] = wpabuf_head(wps->dh_pubkey_r); 624 len[3] = wpabuf_len(wps->dh_pubkey_r); 625 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash); 626 627 if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) { 628 wpa_printf(MSG_DEBUG, "WPS: R-Hash2 derived from R-S2 does " 629 "not match with the pre-committed value"); 630 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE; 631 wps_pwd_auth_fail_event(wps->wps, 1, 2, wps->peer_dev.mac_addr); 632 return -1; 633 } 634 635 wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the second " 636 "half of the device password"); 637 638 return 0; 639} 640 641 642static int wps_process_cred_e(struct wps_data *wps, const u8 *cred, 643 size_t cred_len, int wps2) 644{ 645 struct wps_parse_attr attr; 646 struct wpabuf msg; 647 int ret = 0; 648 649 wpa_printf(MSG_DEBUG, "WPS: Received Credential"); 650 os_memset(&wps->cred, 0, sizeof(wps->cred)); 651 wpabuf_set(&msg, cred, cred_len); 652 if (wps_parse_msg(&msg, &attr) < 0 || 653 wps_process_cred(&attr, &wps->cred)) 654 return -1; 655 656 if (os_memcmp(wps->cred.mac_addr, wps->wps->dev.mac_addr, ETH_ALEN) != 657 0) { 658 wpa_printf(MSG_DEBUG, "WPS: MAC Address in the Credential (" 659 MACSTR ") does not match with own address (" MACSTR 660 ")", MAC2STR(wps->cred.mac_addr), 661 MAC2STR(wps->wps->dev.mac_addr)); 662 /* 663 * In theory, this could be consider fatal error, but there are 664 * number of deployed implementations using other address here 665 * due to unclarity in the specification. For interoperability 666 * reasons, allow this to be processed since we do not really 667 * use the MAC Address information for anything. 668 */ 669#ifdef CONFIG_WPS_STRICT 670 if (wps2) { 671 wpa_printf(MSG_INFO, "WPS: Do not accept incorrect " 672 "MAC Address in AP Settings"); 673 return -1; 674 } 675#endif /* CONFIG_WPS_STRICT */ 676 } 677 678#ifdef CONFIG_WPS2 679 if (!(wps->cred.encr_type & 680 (WPS_ENCR_NONE | WPS_ENCR_TKIP | WPS_ENCR_AES))) { 681 if (wps->cred.encr_type & WPS_ENCR_WEP) { 682 wpa_printf(MSG_INFO, "WPS: Reject Credential " 683 "due to WEP configuration"); 684 wps->error_indication = WPS_EI_SECURITY_WEP_PROHIBITED; 685 return -2; 686 } 687 688 wpa_printf(MSG_INFO, "WPS: Reject Credential due to " 689 "invalid encr_type 0x%x", wps->cred.encr_type); 690 return -1; 691 } 692#endif /* CONFIG_WPS2 */ 693 694 if (wps->wps->cred_cb) { 695 wps->cred.cred_attr = cred - 4; 696 wps->cred.cred_attr_len = cred_len + 4; 697 ret = wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred); 698 wps->cred.cred_attr = NULL; 699 wps->cred.cred_attr_len = 0; 700 } 701 702 return ret; 703} 704 705 706static int wps_process_creds(struct wps_data *wps, const u8 *cred[], 707 size_t cred_len[], size_t num_cred, int wps2) 708{ 709 size_t i; 710 int ok = 0; 711 712 if (wps->wps->ap) 713 return 0; 714 715 if (num_cred == 0) { 716 wpa_printf(MSG_DEBUG, "WPS: No Credential attributes " 717 "received"); 718 return -1; 719 } 720 721 for (i = 0; i < num_cred; i++) { 722 int res; 723 res = wps_process_cred_e(wps, cred[i], cred_len[i], wps2); 724 if (res == 0) 725 ok++; 726 else if (res == -2) 727 wpa_printf(MSG_DEBUG, "WPS: WEP credential skipped"); 728 else 729 return -1; 730 } 731 732 if (ok == 0) { 733 wpa_printf(MSG_DEBUG, "WPS: No valid Credential attribute " 734 "received"); 735 return -1; 736 } 737 738 return 0; 739} 740 741 742static int wps_process_ap_settings_e(struct wps_data *wps, 743 struct wps_parse_attr *attr, 744 struct wpabuf *attrs, int wps2) 745{ 746 struct wps_credential cred; 747 748 if (!wps->wps->ap) 749 return 0; 750 751 if (wps_process_ap_settings(attr, &cred) < 0) 752 return -1; 753 754 wpa_printf(MSG_INFO, "WPS: Received new AP configuration from " 755 "Registrar"); 756 757 if (os_memcmp(cred.mac_addr, wps->wps->dev.mac_addr, ETH_ALEN) != 758 0) { 759 wpa_printf(MSG_DEBUG, "WPS: MAC Address in the AP Settings (" 760 MACSTR ") does not match with own address (" MACSTR 761 ")", MAC2STR(cred.mac_addr), 762 MAC2STR(wps->wps->dev.mac_addr)); 763 /* 764 * In theory, this could be consider fatal error, but there are 765 * number of deployed implementations using other address here 766 * due to unclarity in the specification. For interoperability 767 * reasons, allow this to be processed since we do not really 768 * use the MAC Address information for anything. 769 */ 770#ifdef CONFIG_WPS_STRICT 771 if (wps2) { 772 wpa_printf(MSG_INFO, "WPS: Do not accept incorrect " 773 "MAC Address in AP Settings"); 774 return -1; 775 } 776#endif /* CONFIG_WPS_STRICT */ 777 } 778 779#ifdef CONFIG_WPS2 780 if (!(cred.encr_type & (WPS_ENCR_NONE | WPS_ENCR_TKIP | WPS_ENCR_AES))) 781 { 782 if (cred.encr_type & WPS_ENCR_WEP) { 783 wpa_printf(MSG_INFO, "WPS: Reject new AP settings " 784 "due to WEP configuration"); 785 wps->error_indication = WPS_EI_SECURITY_WEP_PROHIBITED; 786 return -1; 787 } 788 789 wpa_printf(MSG_INFO, "WPS: Reject new AP settings due to " 790 "invalid encr_type 0x%x", cred.encr_type); 791 return -1; 792 } 793#endif /* CONFIG_WPS2 */ 794 795#ifdef CONFIG_WPS_STRICT 796 if (wps2) { 797 if ((cred.encr_type & (WPS_ENCR_TKIP | WPS_ENCR_AES)) == 798 WPS_ENCR_TKIP || 799 (cred.auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) == 800 WPS_AUTH_WPAPSK) { 801 wpa_printf(MSG_INFO, "WPS-STRICT: Invalid WSC 2.0 " 802 "AP Settings: WPA-Personal/TKIP only"); 803 wps->error_indication = 804 WPS_EI_SECURITY_TKIP_ONLY_PROHIBITED; 805 return -1; 806 } 807 } 808#endif /* CONFIG_WPS_STRICT */ 809 810#ifdef CONFIG_WPS2 811 if ((cred.encr_type & (WPS_ENCR_TKIP | WPS_ENCR_AES)) == WPS_ENCR_TKIP) 812 { 813 wpa_printf(MSG_DEBUG, "WPS: Upgrade encr_type TKIP -> " 814 "TKIP+AES"); 815 cred.encr_type |= WPS_ENCR_AES; 816 } 817 818 if ((cred.auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) == 819 WPS_AUTH_WPAPSK) { 820 wpa_printf(MSG_DEBUG, "WPS: Upgrade auth_type WPAPSK -> " 821 "WPAPSK+WPA2PSK"); 822 cred.auth_type |= WPS_AUTH_WPA2PSK; 823 } 824#endif /* CONFIG_WPS2 */ 825 826 if (wps->wps->cred_cb) { 827 cred.cred_attr = wpabuf_head(attrs); 828 cred.cred_attr_len = wpabuf_len(attrs); 829 wps->wps->cred_cb(wps->wps->cb_ctx, &cred); 830 } 831 832 return 0; 833} 834 835 836static int wps_process_dev_pw_id(struct wps_data *wps, const u8 *dev_pw_id) 837{ 838 u16 id; 839 840 if (dev_pw_id == NULL) { 841 wpa_printf(MSG_DEBUG, "WPS: Device Password ID"); 842 return -1; 843 } 844 845 id = WPA_GET_BE16(dev_pw_id); 846 if (wps->dev_pw_id == id) { 847 wpa_printf(MSG_DEBUG, "WPS: Device Password ID %u", id); 848 return 0; 849 } 850 851#ifdef CONFIG_P2P 852 if ((id == DEV_PW_DEFAULT && 853 wps->dev_pw_id == DEV_PW_REGISTRAR_SPECIFIED) || 854 (id == DEV_PW_REGISTRAR_SPECIFIED && 855 wps->dev_pw_id == DEV_PW_DEFAULT)) { 856 /* 857 * Common P2P use cases indicate whether the PIN is from the 858 * client or GO using Device Password Id in M1/M2 in a way that 859 * does not look fully compliant with WSC specification. Anyway, 860 * this is deployed and needs to be allowed, so ignore changes 861 * between Registrar-Specified and Default PIN. 862 */ 863 wpa_printf(MSG_DEBUG, "WPS: Allow PIN Device Password ID " 864 "change"); 865 return 0; 866 } 867#endif /* CONFIG_P2P */ 868 869 wpa_printf(MSG_DEBUG, "WPS: Registrar trying to change Device Password " 870 "ID from %u to %u", wps->dev_pw_id, id); 871 872 if (wps->dev_pw_id == DEV_PW_PUSHBUTTON && id == DEV_PW_DEFAULT) { 873 wpa_printf(MSG_DEBUG, 874 "WPS: Workaround - ignore PBC-to-PIN change"); 875 return 0; 876 } 877 878 if (wps->alt_dev_password && wps->alt_dev_pw_id == id) { 879 wpa_printf(MSG_DEBUG, "WPS: Found a matching Device Password"); 880 os_free(wps->dev_password); 881 wps->dev_pw_id = wps->alt_dev_pw_id; 882 wps->dev_password = wps->alt_dev_password; 883 wps->dev_password_len = wps->alt_dev_password_len; 884 wps->alt_dev_password = NULL; 885 wps->alt_dev_password_len = 0; 886 return 0; 887 } 888 889 return -1; 890} 891 892 893static enum wps_process_res wps_process_m2(struct wps_data *wps, 894 const struct wpabuf *msg, 895 struct wps_parse_attr *attr) 896{ 897 wpa_printf(MSG_DEBUG, "WPS: Received M2"); 898 899 if (wps->state != RECV_M2) { 900 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for " 901 "receiving M2", wps->state); 902 wps->state = SEND_WSC_NACK; 903 return WPS_CONTINUE; 904 } 905 906 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) || 907 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) || 908 wps_process_uuid_r(wps, attr->uuid_r) || 909 wps_process_dev_pw_id(wps, attr->dev_password_id)) { 910 wps->state = SEND_WSC_NACK; 911 return WPS_CONTINUE; 912 } 913 914 /* 915 * Stop here on an AP as an Enrollee if AP Setup is locked unless the 916 * special locked mode is used to allow protocol run up to M7 in order 917 * to support external Registrars that only learn the current AP 918 * configuration without changing it. 919 */ 920 if (wps->wps->ap && 921 ((wps->wps->ap_setup_locked && wps->wps->ap_setup_locked != 2) || 922 wps->dev_password == NULL)) { 923 wpa_printf(MSG_DEBUG, "WPS: AP Setup is locked - refuse " 924 "registration of a new Registrar"); 925 wps->config_error = WPS_CFG_SETUP_LOCKED; 926 wps->state = SEND_WSC_NACK; 927 return WPS_CONTINUE; 928 } 929 930 if (wps_process_pubkey(wps, attr->public_key, attr->public_key_len) || 931 wps_process_authenticator(wps, attr->authenticator, msg) || 932 wps_process_device_attrs(&wps->peer_dev, attr)) { 933 wps->state = SEND_WSC_NACK; 934 return WPS_CONTINUE; 935 } 936 937#ifdef CONFIG_WPS_NFC 938 if (wps->peer_pubkey_hash_set) { 939 struct wpabuf *decrypted; 940 struct wps_parse_attr eattr; 941 942 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings, 943 attr->encr_settings_len); 944 if (decrypted == NULL) { 945 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypt " 946 "Encrypted Settings attribute"); 947 wps->state = SEND_WSC_NACK; 948 return WPS_CONTINUE; 949 } 950 951 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted " 952 "Settings attribute"); 953 if (wps_parse_msg(decrypted, &eattr) < 0 || 954 wps_process_key_wrap_auth(wps, decrypted, 955 eattr.key_wrap_auth) || 956 wps_process_creds(wps, eattr.cred, eattr.cred_len, 957 eattr.num_cred, attr->version2 != NULL)) { 958 wpabuf_free(decrypted); 959 wps->state = SEND_WSC_NACK; 960 return WPS_CONTINUE; 961 } 962 wpabuf_free(decrypted); 963 964 wps->state = WPS_MSG_DONE; 965 return WPS_CONTINUE; 966 } 967#endif /* CONFIG_WPS_NFC */ 968 969 wps->state = SEND_M3; 970 return WPS_CONTINUE; 971} 972 973 974static enum wps_process_res wps_process_m2d(struct wps_data *wps, 975 struct wps_parse_attr *attr) 976{ 977 wpa_printf(MSG_DEBUG, "WPS: Received M2D"); 978 979 if (wps->state != RECV_M2) { 980 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for " 981 "receiving M2D", wps->state); 982 wps->state = SEND_WSC_NACK; 983 return WPS_CONTINUE; 984 } 985 986 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Manufacturer", 987 attr->manufacturer, attr->manufacturer_len); 988 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Name", 989 attr->model_name, attr->model_name_len); 990 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Number", 991 attr->model_number, attr->model_number_len); 992 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Serial Number", 993 attr->serial_number, attr->serial_number_len); 994 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Device Name", 995 attr->dev_name, attr->dev_name_len); 996 997 if (wps->wps->event_cb) { 998 union wps_event_data data; 999 struct wps_event_m2d *m2d = &data.m2d; 1000 os_memset(&data, 0, sizeof(data)); 1001 if (attr->config_methods) 1002 m2d->config_methods = 1003 WPA_GET_BE16(attr->config_methods); 1004 m2d->manufacturer = attr->manufacturer; 1005 m2d->manufacturer_len = attr->manufacturer_len; 1006 m2d->model_name = attr->model_name; 1007 m2d->model_name_len = attr->model_name_len; 1008 m2d->model_number = attr->model_number; 1009 m2d->model_number_len = attr->model_number_len; 1010 m2d->serial_number = attr->serial_number; 1011 m2d->serial_number_len = attr->serial_number_len; 1012 m2d->dev_name = attr->dev_name; 1013 m2d->dev_name_len = attr->dev_name_len; 1014 m2d->primary_dev_type = attr->primary_dev_type; 1015 if (attr->config_error) 1016 m2d->config_error = 1017 WPA_GET_BE16(attr->config_error); 1018 if (attr->dev_password_id) 1019 m2d->dev_password_id = 1020 WPA_GET_BE16(attr->dev_password_id); 1021 wps->wps->event_cb(wps->wps->cb_ctx, WPS_EV_M2D, &data); 1022 } 1023 1024 wps->state = RECEIVED_M2D; 1025 return WPS_CONTINUE; 1026} 1027 1028 1029static enum wps_process_res wps_process_m4(struct wps_data *wps, 1030 const struct wpabuf *msg, 1031 struct wps_parse_attr *attr) 1032{ 1033 struct wpabuf *decrypted; 1034 struct wps_parse_attr eattr; 1035 1036 wpa_printf(MSG_DEBUG, "WPS: Received M4"); 1037 1038 if (wps->state != RECV_M4) { 1039 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for " 1040 "receiving M4", wps->state); 1041 wps->state = SEND_WSC_NACK; 1042 return WPS_CONTINUE; 1043 } 1044 1045 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) || 1046 wps_process_authenticator(wps, attr->authenticator, msg) || 1047 wps_process_r_hash1(wps, attr->r_hash1) || 1048 wps_process_r_hash2(wps, attr->r_hash2)) { 1049 wps->state = SEND_WSC_NACK; 1050 return WPS_CONTINUE; 1051 } 1052 1053 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings, 1054 attr->encr_settings_len); 1055 if (decrypted == NULL) { 1056 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted " 1057 "Settings attribute"); 1058 wps->state = SEND_WSC_NACK; 1059 return WPS_CONTINUE; 1060 } 1061 1062 if (wps_validate_m4_encr(decrypted, attr->version2 != NULL) < 0) { 1063 wpabuf_free(decrypted); 1064 wps->state = SEND_WSC_NACK; 1065 return WPS_CONTINUE; 1066 } 1067 1068 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings " 1069 "attribute"); 1070 if (wps_parse_msg(decrypted, &eattr) < 0 || 1071 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) || 1072 wps_process_r_snonce1(wps, eattr.r_snonce1)) { 1073 wpabuf_free(decrypted); 1074 wps->state = SEND_WSC_NACK; 1075 return WPS_CONTINUE; 1076 } 1077 wpabuf_free(decrypted); 1078 1079 wps->state = SEND_M5; 1080 return WPS_CONTINUE; 1081} 1082 1083 1084static enum wps_process_res wps_process_m6(struct wps_data *wps, 1085 const struct wpabuf *msg, 1086 struct wps_parse_attr *attr) 1087{ 1088 struct wpabuf *decrypted; 1089 struct wps_parse_attr eattr; 1090 1091 wpa_printf(MSG_DEBUG, "WPS: Received M6"); 1092 1093 if (wps->state != RECV_M6) { 1094 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for " 1095 "receiving M6", wps->state); 1096 wps->state = SEND_WSC_NACK; 1097 return WPS_CONTINUE; 1098 } 1099 1100 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) || 1101 wps_process_authenticator(wps, attr->authenticator, msg)) { 1102 wps->state = SEND_WSC_NACK; 1103 return WPS_CONTINUE; 1104 } 1105 1106 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings, 1107 attr->encr_settings_len); 1108 if (decrypted == NULL) { 1109 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted " 1110 "Settings attribute"); 1111 wps->state = SEND_WSC_NACK; 1112 return WPS_CONTINUE; 1113 } 1114 1115 if (wps_validate_m6_encr(decrypted, attr->version2 != NULL) < 0) { 1116 wpabuf_free(decrypted); 1117 wps->state = SEND_WSC_NACK; 1118 return WPS_CONTINUE; 1119 } 1120 1121 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings " 1122 "attribute"); 1123 if (wps_parse_msg(decrypted, &eattr) < 0 || 1124 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) || 1125 wps_process_r_snonce2(wps, eattr.r_snonce2)) { 1126 wpabuf_free(decrypted); 1127 wps->state = SEND_WSC_NACK; 1128 return WPS_CONTINUE; 1129 } 1130 wpabuf_free(decrypted); 1131 1132 if (wps->wps->ap) 1133 wps->wps->event_cb(wps->wps->cb_ctx, WPS_EV_AP_PIN_SUCCESS, 1134 NULL); 1135 1136 wps->state = SEND_M7; 1137 return WPS_CONTINUE; 1138} 1139 1140 1141static enum wps_process_res wps_process_m8(struct wps_data *wps, 1142 const struct wpabuf *msg, 1143 struct wps_parse_attr *attr) 1144{ 1145 struct wpabuf *decrypted; 1146 struct wps_parse_attr eattr; 1147 1148 wpa_printf(MSG_DEBUG, "WPS: Received M8"); 1149 1150 if (wps->state != RECV_M8) { 1151 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for " 1152 "receiving M8", wps->state); 1153 wps->state = SEND_WSC_NACK; 1154 return WPS_CONTINUE; 1155 } 1156 1157 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) || 1158 wps_process_authenticator(wps, attr->authenticator, msg)) { 1159 wps->state = SEND_WSC_NACK; 1160 return WPS_CONTINUE; 1161 } 1162 1163 if (wps->wps->ap && wps->wps->ap_setup_locked) { 1164 /* 1165 * Stop here if special ap_setup_locked == 2 mode allowed the 1166 * protocol to continue beyond M2. This allows ER to learn the 1167 * current AP settings without changing them. 1168 */ 1169 wpa_printf(MSG_DEBUG, "WPS: AP Setup is locked - refuse " 1170 "registration of a new Registrar"); 1171 wps->config_error = WPS_CFG_SETUP_LOCKED; 1172 wps->state = SEND_WSC_NACK; 1173 return WPS_CONTINUE; 1174 } 1175 1176 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings, 1177 attr->encr_settings_len); 1178 if (decrypted == NULL) { 1179 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted " 1180 "Settings attribute"); 1181 wps->state = SEND_WSC_NACK; 1182 return WPS_CONTINUE; 1183 } 1184 1185 if (wps_validate_m8_encr(decrypted, wps->wps->ap, 1186 attr->version2 != NULL) < 0) { 1187 wpabuf_free(decrypted); 1188 wps->state = SEND_WSC_NACK; 1189 return WPS_CONTINUE; 1190 } 1191 1192 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings " 1193 "attribute"); 1194 if (wps_parse_msg(decrypted, &eattr) < 0 || 1195 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) || 1196 wps_process_creds(wps, eattr.cred, eattr.cred_len, 1197 eattr.num_cred, attr->version2 != NULL) || 1198 wps_process_ap_settings_e(wps, &eattr, decrypted, 1199 attr->version2 != NULL)) { 1200 wpabuf_free(decrypted); 1201 wps->state = SEND_WSC_NACK; 1202 return WPS_CONTINUE; 1203 } 1204 wpabuf_free(decrypted); 1205 1206 wps->state = WPS_MSG_DONE; 1207 return WPS_CONTINUE; 1208} 1209 1210 1211static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps, 1212 const struct wpabuf *msg) 1213{ 1214 struct wps_parse_attr attr; 1215 enum wps_process_res ret = WPS_CONTINUE; 1216 1217 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG"); 1218 1219 if (wps_parse_msg(msg, &attr) < 0) 1220 return WPS_FAILURE; 1221 1222 if (attr.enrollee_nonce == NULL || 1223 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) { 1224 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce"); 1225 return WPS_FAILURE; 1226 } 1227 1228 if (attr.msg_type == NULL) { 1229 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute"); 1230 wps->state = SEND_WSC_NACK; 1231 return WPS_CONTINUE; 1232 } 1233 1234 switch (*attr.msg_type) { 1235 case WPS_M2: 1236 if (wps_validate_m2(msg) < 0) 1237 return WPS_FAILURE; 1238 ret = wps_process_m2(wps, msg, &attr); 1239 break; 1240 case WPS_M2D: 1241 if (wps_validate_m2d(msg) < 0) 1242 return WPS_FAILURE; 1243 ret = wps_process_m2d(wps, &attr); 1244 break; 1245 case WPS_M4: 1246 if (wps_validate_m4(msg) < 0) 1247 return WPS_FAILURE; 1248 ret = wps_process_m4(wps, msg, &attr); 1249 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK) 1250 wps_fail_event(wps->wps, WPS_M4, wps->config_error, 1251 wps->error_indication, 1252 wps->peer_dev.mac_addr); 1253 break; 1254 case WPS_M6: 1255 if (wps_validate_m6(msg) < 0) 1256 return WPS_FAILURE; 1257 ret = wps_process_m6(wps, msg, &attr); 1258 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK) 1259 wps_fail_event(wps->wps, WPS_M6, wps->config_error, 1260 wps->error_indication, 1261 wps->peer_dev.mac_addr); 1262 break; 1263 case WPS_M8: 1264 if (wps_validate_m8(msg) < 0) 1265 return WPS_FAILURE; 1266 ret = wps_process_m8(wps, msg, &attr); 1267 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK) 1268 wps_fail_event(wps->wps, WPS_M8, wps->config_error, 1269 wps->error_indication, 1270 wps->peer_dev.mac_addr); 1271 break; 1272 default: 1273 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d", 1274 *attr.msg_type); 1275 return WPS_FAILURE; 1276 } 1277 1278 /* 1279 * Save a copy of the last message for Authenticator derivation if we 1280 * are continuing. However, skip M2D since it is not authenticated and 1281 * neither is the ACK/NACK response frame. This allows the possibly 1282 * following M2 to be processed correctly by using the previously sent 1283 * M1 in Authenticator derivation. 1284 */ 1285 if (ret == WPS_CONTINUE && *attr.msg_type != WPS_M2D) { 1286 /* Save a copy of the last message for Authenticator derivation 1287 */ 1288 wpabuf_free(wps->last_msg); 1289 wps->last_msg = wpabuf_dup(msg); 1290 } 1291 1292 return ret; 1293} 1294 1295 1296static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps, 1297 const struct wpabuf *msg) 1298{ 1299 struct wps_parse_attr attr; 1300 1301 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK"); 1302 1303 if (wps_parse_msg(msg, &attr) < 0) 1304 return WPS_FAILURE; 1305 1306 if (attr.msg_type == NULL) { 1307 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute"); 1308 return WPS_FAILURE; 1309 } 1310 1311 if (*attr.msg_type != WPS_WSC_ACK) { 1312 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d", 1313 *attr.msg_type); 1314 return WPS_FAILURE; 1315 } 1316 1317 if (attr.registrar_nonce == NULL || 1318 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0) 1319 { 1320 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce"); 1321 return WPS_FAILURE; 1322 } 1323 1324 if (attr.enrollee_nonce == NULL || 1325 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) { 1326 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce"); 1327 return WPS_FAILURE; 1328 } 1329 1330 if (wps->state == RECV_ACK && wps->wps->ap) { 1331 wpa_printf(MSG_DEBUG, "WPS: External Registrar registration " 1332 "completed successfully"); 1333 wps_success_event(wps->wps, wps->peer_dev.mac_addr); 1334 wps->state = WPS_FINISHED; 1335 return WPS_DONE; 1336 } 1337 1338 return WPS_FAILURE; 1339} 1340 1341 1342static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps, 1343 const struct wpabuf *msg) 1344{ 1345 struct wps_parse_attr attr; 1346 u16 config_error; 1347 1348 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK"); 1349 1350 if (wps_parse_msg(msg, &attr) < 0) 1351 return WPS_FAILURE; 1352 1353 if (attr.msg_type == NULL) { 1354 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute"); 1355 return WPS_FAILURE; 1356 } 1357 1358 if (*attr.msg_type != WPS_WSC_NACK) { 1359 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d", 1360 *attr.msg_type); 1361 return WPS_FAILURE; 1362 } 1363 1364 if (attr.registrar_nonce == NULL || 1365 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0) 1366 { 1367 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce"); 1368 wpa_hexdump(MSG_DEBUG, "WPS: Received Registrar Nonce", 1369 attr.registrar_nonce, WPS_NONCE_LEN); 1370 wpa_hexdump(MSG_DEBUG, "WPS: Expected Registrar Nonce", 1371 wps->nonce_r, WPS_NONCE_LEN); 1372 return WPS_FAILURE; 1373 } 1374 1375 if (attr.enrollee_nonce == NULL || 1376 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) { 1377 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce"); 1378 wpa_hexdump(MSG_DEBUG, "WPS: Received Enrollee Nonce", 1379 attr.enrollee_nonce, WPS_NONCE_LEN); 1380 wpa_hexdump(MSG_DEBUG, "WPS: Expected Enrollee Nonce", 1381 wps->nonce_e, WPS_NONCE_LEN); 1382 return WPS_FAILURE; 1383 } 1384 1385 if (attr.config_error == NULL) { 1386 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute " 1387 "in WSC_NACK"); 1388 return WPS_FAILURE; 1389 } 1390 1391 config_error = WPA_GET_BE16(attr.config_error); 1392 wpa_printf(MSG_DEBUG, "WPS: Registrar terminated negotiation with " 1393 "Configuration Error %d", config_error); 1394 1395 switch (wps->state) { 1396 case RECV_M4: 1397 wps_fail_event(wps->wps, WPS_M3, config_error, 1398 wps->error_indication, wps->peer_dev.mac_addr); 1399 break; 1400 case RECV_M6: 1401 wps_fail_event(wps->wps, WPS_M5, config_error, 1402 wps->error_indication, wps->peer_dev.mac_addr); 1403 break; 1404 case RECV_M8: 1405 wps_fail_event(wps->wps, WPS_M7, config_error, 1406 wps->error_indication, wps->peer_dev.mac_addr); 1407 break; 1408 default: 1409 break; 1410 } 1411 1412 /* Followed by NACK if Enrollee is Supplicant or EAP-Failure if 1413 * Enrollee is Authenticator */ 1414 wps->state = SEND_WSC_NACK; 1415 1416 return WPS_FAILURE; 1417} 1418 1419 1420enum wps_process_res wps_enrollee_process_msg(struct wps_data *wps, 1421 enum wsc_op_code op_code, 1422 const struct wpabuf *msg) 1423{ 1424 1425 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu " 1426 "op_code=%d)", 1427 (unsigned long) wpabuf_len(msg), op_code); 1428 1429 if (op_code == WSC_UPnP) { 1430 /* Determine the OpCode based on message type attribute */ 1431 struct wps_parse_attr attr; 1432 if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type) { 1433 if (*attr.msg_type == WPS_WSC_ACK) 1434 op_code = WSC_ACK; 1435 else if (*attr.msg_type == WPS_WSC_NACK) 1436 op_code = WSC_NACK; 1437 } 1438 } 1439 1440 switch (op_code) { 1441 case WSC_MSG: 1442 case WSC_UPnP: 1443 return wps_process_wsc_msg(wps, msg); 1444 case WSC_ACK: 1445 if (wps_validate_wsc_ack(msg) < 0) 1446 return WPS_FAILURE; 1447 return wps_process_wsc_ack(wps, msg); 1448 case WSC_NACK: 1449 if (wps_validate_wsc_nack(msg) < 0) 1450 return WPS_FAILURE; 1451 return wps_process_wsc_nack(wps, msg); 1452 default: 1453 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code); 1454 return WPS_FAILURE; 1455 } 1456} 1457