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