xref: /freebsd/contrib/wpa/src/wps/wps_enrollee.c (revision 4fd0d10e0fe684211328bc148edf89a792425b39)
1 /*
2  * Wi-Fi Protected Setup - Enrollee
3  * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "includes.h"
10 
11 #include "common.h"
12 #include "crypto/crypto.h"
13 #include "crypto/sha256.h"
14 #include "crypto/random.h"
15 #include "wps_i.h"
16 #include "wps_dev_attr.h"
17 
18 
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 enum wps_process_res wps_process_m2(struct wps_data *wps,
841 					   const struct wpabuf *msg,
842 					   struct wps_parse_attr *attr)
843 {
844 	wpa_printf(MSG_DEBUG, "WPS: Received M2");
845 
846 	if (wps->state != RECV_M2) {
847 		wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
848 			   "receiving M2", wps->state);
849 		wps->state = SEND_WSC_NACK;
850 		return WPS_CONTINUE;
851 	}
852 
853 	if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
854 	    wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
855 	    wps_process_uuid_r(wps, attr->uuid_r)) {
856 		wps->state = SEND_WSC_NACK;
857 		return WPS_CONTINUE;
858 	}
859 
860 	/*
861 	 * Stop here on an AP as an Enrollee if AP Setup is locked unless the
862 	 * special locked mode is used to allow protocol run up to M7 in order
863 	 * to support external Registrars that only learn the current AP
864 	 * configuration without changing it.
865 	 */
866 	if (wps->wps->ap &&
867 	    ((wps->wps->ap_setup_locked && wps->wps->ap_setup_locked != 2) ||
868 	     wps->dev_password == NULL)) {
869 		wpa_printf(MSG_DEBUG, "WPS: AP Setup is locked - refuse "
870 			   "registration of a new Registrar");
871 		wps->config_error = WPS_CFG_SETUP_LOCKED;
872 		wps->state = SEND_WSC_NACK;
873 		return WPS_CONTINUE;
874 	}
875 
876 	if (wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
877 	    wps_process_authenticator(wps, attr->authenticator, msg) ||
878 	    wps_process_device_attrs(&wps->peer_dev, attr)) {
879 		wps->state = SEND_WSC_NACK;
880 		return WPS_CONTINUE;
881 	}
882 
883 	wps->state = SEND_M3;
884 	return WPS_CONTINUE;
885 }
886 
887 
888 static enum wps_process_res wps_process_m2d(struct wps_data *wps,
889 					    struct wps_parse_attr *attr)
890 {
891 	wpa_printf(MSG_DEBUG, "WPS: Received M2D");
892 
893 	if (wps->state != RECV_M2) {
894 		wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
895 			   "receiving M2D", wps->state);
896 		wps->state = SEND_WSC_NACK;
897 		return WPS_CONTINUE;
898 	}
899 
900 	wpa_hexdump_ascii(MSG_DEBUG, "WPS: Manufacturer",
901 			  attr->manufacturer, attr->manufacturer_len);
902 	wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Name",
903 			  attr->model_name, attr->model_name_len);
904 	wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Number",
905 			  attr->model_number, attr->model_number_len);
906 	wpa_hexdump_ascii(MSG_DEBUG, "WPS: Serial Number",
907 			  attr->serial_number, attr->serial_number_len);
908 	wpa_hexdump_ascii(MSG_DEBUG, "WPS: Device Name",
909 			  attr->dev_name, attr->dev_name_len);
910 
911 	if (wps->wps->event_cb) {
912 		union wps_event_data data;
913 		struct wps_event_m2d *m2d = &data.m2d;
914 		os_memset(&data, 0, sizeof(data));
915 		if (attr->config_methods)
916 			m2d->config_methods =
917 				WPA_GET_BE16(attr->config_methods);
918 		m2d->manufacturer = attr->manufacturer;
919 		m2d->manufacturer_len = attr->manufacturer_len;
920 		m2d->model_name = attr->model_name;
921 		m2d->model_name_len = attr->model_name_len;
922 		m2d->model_number = attr->model_number;
923 		m2d->model_number_len = attr->model_number_len;
924 		m2d->serial_number = attr->serial_number;
925 		m2d->serial_number_len = attr->serial_number_len;
926 		m2d->dev_name = attr->dev_name;
927 		m2d->dev_name_len = attr->dev_name_len;
928 		m2d->primary_dev_type = attr->primary_dev_type;
929 		if (attr->config_error)
930 			m2d->config_error =
931 				WPA_GET_BE16(attr->config_error);
932 		if (attr->dev_password_id)
933 			m2d->dev_password_id =
934 				WPA_GET_BE16(attr->dev_password_id);
935 		wps->wps->event_cb(wps->wps->cb_ctx, WPS_EV_M2D, &data);
936 	}
937 
938 	wps->state = RECEIVED_M2D;
939 	return WPS_CONTINUE;
940 }
941 
942 
943 static enum wps_process_res wps_process_m4(struct wps_data *wps,
944 					   const struct wpabuf *msg,
945 					   struct wps_parse_attr *attr)
946 {
947 	struct wpabuf *decrypted;
948 	struct wps_parse_attr eattr;
949 
950 	wpa_printf(MSG_DEBUG, "WPS: Received M4");
951 
952 	if (wps->state != RECV_M4) {
953 		wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
954 			   "receiving M4", wps->state);
955 		wps->state = SEND_WSC_NACK;
956 		return WPS_CONTINUE;
957 	}
958 
959 	if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
960 	    wps_process_authenticator(wps, attr->authenticator, msg) ||
961 	    wps_process_r_hash1(wps, attr->r_hash1) ||
962 	    wps_process_r_hash2(wps, attr->r_hash2)) {
963 		wps->state = SEND_WSC_NACK;
964 		return WPS_CONTINUE;
965 	}
966 
967 	decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
968 					      attr->encr_settings_len);
969 	if (decrypted == NULL) {
970 		wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
971 			   "Settings attribute");
972 		wps->state = SEND_WSC_NACK;
973 		return WPS_CONTINUE;
974 	}
975 
976 	if (wps_validate_m4_encr(decrypted, attr->version2 != NULL) < 0) {
977 		wpabuf_free(decrypted);
978 		wps->state = SEND_WSC_NACK;
979 		return WPS_CONTINUE;
980 	}
981 
982 	wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
983 		   "attribute");
984 	if (wps_parse_msg(decrypted, &eattr) < 0 ||
985 	    wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
986 	    wps_process_r_snonce1(wps, eattr.r_snonce1)) {
987 		wpabuf_free(decrypted);
988 		wps->state = SEND_WSC_NACK;
989 		return WPS_CONTINUE;
990 	}
991 	wpabuf_free(decrypted);
992 
993 	wps->state = SEND_M5;
994 	return WPS_CONTINUE;
995 }
996 
997 
998 static enum wps_process_res wps_process_m6(struct wps_data *wps,
999 					   const struct wpabuf *msg,
1000 					   struct wps_parse_attr *attr)
1001 {
1002 	struct wpabuf *decrypted;
1003 	struct wps_parse_attr eattr;
1004 
1005 	wpa_printf(MSG_DEBUG, "WPS: Received M6");
1006 
1007 	if (wps->state != RECV_M6) {
1008 		wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1009 			   "receiving M6", wps->state);
1010 		wps->state = SEND_WSC_NACK;
1011 		return WPS_CONTINUE;
1012 	}
1013 
1014 	if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
1015 	    wps_process_authenticator(wps, attr->authenticator, msg)) {
1016 		wps->state = SEND_WSC_NACK;
1017 		return WPS_CONTINUE;
1018 	}
1019 
1020 	decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1021 					      attr->encr_settings_len);
1022 	if (decrypted == NULL) {
1023 		wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1024 			   "Settings attribute");
1025 		wps->state = SEND_WSC_NACK;
1026 		return WPS_CONTINUE;
1027 	}
1028 
1029 	if (wps_validate_m6_encr(decrypted, attr->version2 != NULL) < 0) {
1030 		wpabuf_free(decrypted);
1031 		wps->state = SEND_WSC_NACK;
1032 		return WPS_CONTINUE;
1033 	}
1034 
1035 	wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1036 		   "attribute");
1037 	if (wps_parse_msg(decrypted, &eattr) < 0 ||
1038 	    wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1039 	    wps_process_r_snonce2(wps, eattr.r_snonce2)) {
1040 		wpabuf_free(decrypted);
1041 		wps->state = SEND_WSC_NACK;
1042 		return WPS_CONTINUE;
1043 	}
1044 	wpabuf_free(decrypted);
1045 
1046 	if (wps->wps->ap)
1047 		wps->wps->event_cb(wps->wps->cb_ctx, WPS_EV_AP_PIN_SUCCESS,
1048 				   NULL);
1049 
1050 	wps->state = SEND_M7;
1051 	return WPS_CONTINUE;
1052 }
1053 
1054 
1055 static enum wps_process_res wps_process_m8(struct wps_data *wps,
1056 					   const struct wpabuf *msg,
1057 					   struct wps_parse_attr *attr)
1058 {
1059 	struct wpabuf *decrypted;
1060 	struct wps_parse_attr eattr;
1061 
1062 	wpa_printf(MSG_DEBUG, "WPS: Received M8");
1063 
1064 	if (wps->state != RECV_M8) {
1065 		wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1066 			   "receiving M8", wps->state);
1067 		wps->state = SEND_WSC_NACK;
1068 		return WPS_CONTINUE;
1069 	}
1070 
1071 	if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
1072 	    wps_process_authenticator(wps, attr->authenticator, msg)) {
1073 		wps->state = SEND_WSC_NACK;
1074 		return WPS_CONTINUE;
1075 	}
1076 
1077 	if (wps->wps->ap && wps->wps->ap_setup_locked) {
1078 		/*
1079 		 * Stop here if special ap_setup_locked == 2 mode allowed the
1080 		 * protocol to continue beyond M2. This allows ER to learn the
1081 		 * current AP settings without changing them.
1082 		 */
1083 		wpa_printf(MSG_DEBUG, "WPS: AP Setup is locked - refuse "
1084 			   "registration of a new Registrar");
1085 		wps->config_error = WPS_CFG_SETUP_LOCKED;
1086 		wps->state = SEND_WSC_NACK;
1087 		return WPS_CONTINUE;
1088 	}
1089 
1090 	decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1091 					      attr->encr_settings_len);
1092 	if (decrypted == NULL) {
1093 		wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1094 			   "Settings attribute");
1095 		wps->state = SEND_WSC_NACK;
1096 		return WPS_CONTINUE;
1097 	}
1098 
1099 	if (wps_validate_m8_encr(decrypted, wps->wps->ap,
1100 				 attr->version2 != NULL) < 0) {
1101 		wpabuf_free(decrypted);
1102 		wps->state = SEND_WSC_NACK;
1103 		return WPS_CONTINUE;
1104 	}
1105 
1106 	wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1107 		   "attribute");
1108 	if (wps_parse_msg(decrypted, &eattr) < 0 ||
1109 	    wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1110 	    wps_process_creds(wps, eattr.cred, eattr.cred_len,
1111 			      eattr.num_cred, attr->version2 != NULL) ||
1112 	    wps_process_ap_settings_e(wps, &eattr, decrypted,
1113 				      attr->version2 != NULL)) {
1114 		wpabuf_free(decrypted);
1115 		wps->state = SEND_WSC_NACK;
1116 		return WPS_CONTINUE;
1117 	}
1118 	wpabuf_free(decrypted);
1119 
1120 	wps->state = WPS_MSG_DONE;
1121 	return WPS_CONTINUE;
1122 }
1123 
1124 
1125 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
1126 						const struct wpabuf *msg)
1127 {
1128 	struct wps_parse_attr attr;
1129 	enum wps_process_res ret = WPS_CONTINUE;
1130 
1131 	wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
1132 
1133 	if (wps_parse_msg(msg, &attr) < 0)
1134 		return WPS_FAILURE;
1135 
1136 	if (attr.enrollee_nonce == NULL ||
1137 	    os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
1138 		wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1139 		return WPS_FAILURE;
1140 	}
1141 
1142 	if (attr.msg_type == NULL) {
1143 		wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1144 		wps->state = SEND_WSC_NACK;
1145 		return WPS_CONTINUE;
1146 	}
1147 
1148 	switch (*attr.msg_type) {
1149 	case WPS_M2:
1150 		if (wps_validate_m2(msg) < 0)
1151 			return WPS_FAILURE;
1152 		ret = wps_process_m2(wps, msg, &attr);
1153 		break;
1154 	case WPS_M2D:
1155 		if (wps_validate_m2d(msg) < 0)
1156 			return WPS_FAILURE;
1157 		ret = wps_process_m2d(wps, &attr);
1158 		break;
1159 	case WPS_M4:
1160 		if (wps_validate_m4(msg) < 0)
1161 			return WPS_FAILURE;
1162 		ret = wps_process_m4(wps, msg, &attr);
1163 		if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1164 			wps_fail_event(wps->wps, WPS_M4, wps->config_error,
1165 				       wps->error_indication);
1166 		break;
1167 	case WPS_M6:
1168 		if (wps_validate_m6(msg) < 0)
1169 			return WPS_FAILURE;
1170 		ret = wps_process_m6(wps, msg, &attr);
1171 		if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1172 			wps_fail_event(wps->wps, WPS_M6, wps->config_error,
1173 				       wps->error_indication);
1174 		break;
1175 	case WPS_M8:
1176 		if (wps_validate_m8(msg) < 0)
1177 			return WPS_FAILURE;
1178 		ret = wps_process_m8(wps, msg, &attr);
1179 		if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1180 			wps_fail_event(wps->wps, WPS_M8, wps->config_error,
1181 				       wps->error_indication);
1182 		break;
1183 	default:
1184 		wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
1185 			   *attr.msg_type);
1186 		return WPS_FAILURE;
1187 	}
1188 
1189 	/*
1190 	 * Save a copy of the last message for Authenticator derivation if we
1191 	 * are continuing. However, skip M2D since it is not authenticated and
1192 	 * neither is the ACK/NACK response frame. This allows the possibly
1193 	 * following M2 to be processed correctly by using the previously sent
1194 	 * M1 in Authenticator derivation.
1195 	 */
1196 	if (ret == WPS_CONTINUE && *attr.msg_type != WPS_M2D) {
1197 		/* Save a copy of the last message for Authenticator derivation
1198 		 */
1199 		wpabuf_free(wps->last_msg);
1200 		wps->last_msg = wpabuf_dup(msg);
1201 	}
1202 
1203 	return ret;
1204 }
1205 
1206 
1207 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
1208 						const struct wpabuf *msg)
1209 {
1210 	struct wps_parse_attr attr;
1211 
1212 	wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
1213 
1214 	if (wps_parse_msg(msg, &attr) < 0)
1215 		return WPS_FAILURE;
1216 
1217 	if (attr.msg_type == NULL) {
1218 		wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1219 		return WPS_FAILURE;
1220 	}
1221 
1222 	if (*attr.msg_type != WPS_WSC_ACK) {
1223 		wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1224 			   *attr.msg_type);
1225 		return WPS_FAILURE;
1226 	}
1227 
1228 	if (attr.registrar_nonce == NULL ||
1229 	    os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
1230 	{
1231 		wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1232 		return WPS_FAILURE;
1233 	}
1234 
1235 	if (attr.enrollee_nonce == NULL ||
1236 	    os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
1237 		wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1238 		return WPS_FAILURE;
1239 	}
1240 
1241 	if (wps->state == RECV_ACK && wps->wps->ap) {
1242 		wpa_printf(MSG_DEBUG, "WPS: External Registrar registration "
1243 			   "completed successfully");
1244 		wps_success_event(wps->wps);
1245 		wps->state = WPS_FINISHED;
1246 		return WPS_DONE;
1247 	}
1248 
1249 	return WPS_FAILURE;
1250 }
1251 
1252 
1253 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
1254 						 const struct wpabuf *msg)
1255 {
1256 	struct wps_parse_attr attr;
1257 	u16 config_error;
1258 
1259 	wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
1260 
1261 	if (wps_parse_msg(msg, &attr) < 0)
1262 		return WPS_FAILURE;
1263 
1264 	if (attr.msg_type == NULL) {
1265 		wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1266 		return WPS_FAILURE;
1267 	}
1268 
1269 	if (*attr.msg_type != WPS_WSC_NACK) {
1270 		wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1271 			   *attr.msg_type);
1272 		return WPS_FAILURE;
1273 	}
1274 
1275 	if (attr.registrar_nonce == NULL ||
1276 	    os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
1277 	{
1278 		wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1279 		wpa_hexdump(MSG_DEBUG, "WPS: Received Registrar Nonce",
1280 			    attr.registrar_nonce, WPS_NONCE_LEN);
1281 		wpa_hexdump(MSG_DEBUG, "WPS: Expected Registrar Nonce",
1282 			    wps->nonce_r, WPS_NONCE_LEN);
1283 		return WPS_FAILURE;
1284 	}
1285 
1286 	if (attr.enrollee_nonce == NULL ||
1287 	    os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
1288 		wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1289 		wpa_hexdump(MSG_DEBUG, "WPS: Received Enrollee Nonce",
1290 			    attr.enrollee_nonce, WPS_NONCE_LEN);
1291 		wpa_hexdump(MSG_DEBUG, "WPS: Expected Enrollee Nonce",
1292 			    wps->nonce_e, WPS_NONCE_LEN);
1293 		return WPS_FAILURE;
1294 	}
1295 
1296 	if (attr.config_error == NULL) {
1297 		wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
1298 			   "in WSC_NACK");
1299 		return WPS_FAILURE;
1300 	}
1301 
1302 	config_error = WPA_GET_BE16(attr.config_error);
1303 	wpa_printf(MSG_DEBUG, "WPS: Registrar terminated negotiation with "
1304 		   "Configuration Error %d", config_error);
1305 
1306 	switch (wps->state) {
1307 	case RECV_M4:
1308 		wps_fail_event(wps->wps, WPS_M3, config_error,
1309 			       wps->error_indication);
1310 		break;
1311 	case RECV_M6:
1312 		wps_fail_event(wps->wps, WPS_M5, config_error,
1313 			       wps->error_indication);
1314 		break;
1315 	case RECV_M8:
1316 		wps_fail_event(wps->wps, WPS_M7, config_error,
1317 			       wps->error_indication);
1318 		break;
1319 	default:
1320 		break;
1321 	}
1322 
1323 	/* Followed by NACK if Enrollee is Supplicant or EAP-Failure if
1324 	 * Enrollee is Authenticator */
1325 	wps->state = SEND_WSC_NACK;
1326 
1327 	return WPS_FAILURE;
1328 }
1329 
1330 
1331 enum wps_process_res wps_enrollee_process_msg(struct wps_data *wps,
1332 					      enum wsc_op_code op_code,
1333 					      const struct wpabuf *msg)
1334 {
1335 
1336 	wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
1337 		   "op_code=%d)",
1338 		   (unsigned long) wpabuf_len(msg), op_code);
1339 
1340 	if (op_code == WSC_UPnP) {
1341 		/* Determine the OpCode based on message type attribute */
1342 		struct wps_parse_attr attr;
1343 		if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type) {
1344 			if (*attr.msg_type == WPS_WSC_ACK)
1345 				op_code = WSC_ACK;
1346 			else if (*attr.msg_type == WPS_WSC_NACK)
1347 				op_code = WSC_NACK;
1348 		}
1349 	}
1350 
1351 	switch (op_code) {
1352 	case WSC_MSG:
1353 	case WSC_UPnP:
1354 		return wps_process_wsc_msg(wps, msg);
1355 	case WSC_ACK:
1356 		if (wps_validate_wsc_ack(msg) < 0)
1357 			return WPS_FAILURE;
1358 		return wps_process_wsc_ack(wps, msg);
1359 	case WSC_NACK:
1360 		if (wps_validate_wsc_nack(msg) < 0)
1361 			return WPS_FAILURE;
1362 		return wps_process_wsc_nack(wps, msg);
1363 	default:
1364 		wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
1365 		return WPS_FAILURE;
1366 	}
1367 }
1368