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