1/*
2 * Wi-Fi Protected Setup - Enrollee
3 * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "crypto/crypto.h"
13#include "crypto/sha256.h"
14#include "crypto/random.h"
15#include "wps_i.h"
16#include "wps_dev_attr.h"
17
18
19static int wps_build_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
29static 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
45static 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
93static 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
103static 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
114static 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
177static 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
208static 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
243static 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
253static 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
275static 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
300static 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
310static 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
320static 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
337static 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
383static 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
412struct 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
473static 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
488static 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
504static 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
518static 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#ifdef CONFIG_WPS_OOB
527	if (wps->dev_pw_id != DEV_PW_DEFAULT &&
528	    wps->wps->oob_conf.pubkey_hash) {
529		const u8 *addr[1];
530		u8 hash[WPS_HASH_LEN];
531
532		addr[0] = pk;
533		sha256_vector(1, addr, &pk_len, hash);
534		if (os_memcmp(hash,
535			      wpabuf_head(wps->wps->oob_conf.pubkey_hash),
536			      WPS_OOB_PUBKEY_HASH_LEN) != 0) {
537			wpa_printf(MSG_ERROR, "WPS: Public Key hash error");
538			return -1;
539		}
540	}
541#endif /* CONFIG_WPS_OOB */
542
543	wpabuf_free(wps->dh_pubkey_r);
544	wps->dh_pubkey_r = wpabuf_alloc_copy(pk, pk_len);
545	if (wps->dh_pubkey_r == NULL)
546		return -1;
547
548	if (wps_derive_keys(wps) < 0)
549		return -1;
550
551	return 0;
552}
553
554
555static int wps_process_r_hash1(struct wps_data *wps, const u8 *r_hash1)
556{
557	if (r_hash1 == NULL) {
558		wpa_printf(MSG_DEBUG, "WPS: No R-Hash1 received");
559		return -1;
560	}
561
562	os_memcpy(wps->peer_hash1, r_hash1, WPS_HASH_LEN);
563	wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", wps->peer_hash1, WPS_HASH_LEN);
564
565	return 0;
566}
567
568
569static int wps_process_r_hash2(struct wps_data *wps, const u8 *r_hash2)
570{
571	if (r_hash2 == NULL) {
572		wpa_printf(MSG_DEBUG, "WPS: No R-Hash2 received");
573		return -1;
574	}
575
576	os_memcpy(wps->peer_hash2, r_hash2, WPS_HASH_LEN);
577	wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", wps->peer_hash2, WPS_HASH_LEN);
578
579	return 0;
580}
581
582
583static int wps_process_r_snonce1(struct wps_data *wps, const u8 *r_snonce1)
584{
585	u8 hash[SHA256_MAC_LEN];
586	const u8 *addr[4];
587	size_t len[4];
588
589	if (r_snonce1 == NULL) {
590		wpa_printf(MSG_DEBUG, "WPS: No R-SNonce1 received");
591		return -1;
592	}
593
594	wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce1", r_snonce1,
595			WPS_SECRET_NONCE_LEN);
596
597	/* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
598	addr[0] = r_snonce1;
599	len[0] = WPS_SECRET_NONCE_LEN;
600	addr[1] = wps->psk1;
601	len[1] = WPS_PSK_LEN;
602	addr[2] = wpabuf_head(wps->dh_pubkey_e);
603	len[2] = wpabuf_len(wps->dh_pubkey_e);
604	addr[3] = wpabuf_head(wps->dh_pubkey_r);
605	len[3] = wpabuf_len(wps->dh_pubkey_r);
606	hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
607
608	if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
609		wpa_printf(MSG_DEBUG, "WPS: R-Hash1 derived from R-S1 does "
610			   "not match with the pre-committed value");
611		wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
612		wps_pwd_auth_fail_event(wps->wps, 1, 1);
613		return -1;
614	}
615
616	wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the first "
617		   "half of the device password");
618
619	return 0;
620}
621
622
623static int wps_process_r_snonce2(struct wps_data *wps, const u8 *r_snonce2)
624{
625	u8 hash[SHA256_MAC_LEN];
626	const u8 *addr[4];
627	size_t len[4];
628
629	if (r_snonce2 == NULL) {
630		wpa_printf(MSG_DEBUG, "WPS: No R-SNonce2 received");
631		return -1;
632	}
633
634	wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce2", r_snonce2,
635			WPS_SECRET_NONCE_LEN);
636
637	/* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
638	addr[0] = r_snonce2;
639	len[0] = WPS_SECRET_NONCE_LEN;
640	addr[1] = wps->psk2;
641	len[1] = WPS_PSK_LEN;
642	addr[2] = wpabuf_head(wps->dh_pubkey_e);
643	len[2] = wpabuf_len(wps->dh_pubkey_e);
644	addr[3] = wpabuf_head(wps->dh_pubkey_r);
645	len[3] = wpabuf_len(wps->dh_pubkey_r);
646	hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
647
648	if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
649		wpa_printf(MSG_DEBUG, "WPS: R-Hash2 derived from R-S2 does "
650			   "not match with the pre-committed value");
651		wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
652		wps_pwd_auth_fail_event(wps->wps, 1, 2);
653		return -1;
654	}
655
656	wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the second "
657		   "half of the device password");
658
659	return 0;
660}
661
662
663static int wps_process_cred_e(struct wps_data *wps, const u8 *cred,
664			      size_t cred_len, int wps2)
665{
666	struct wps_parse_attr attr;
667	struct wpabuf msg;
668
669	wpa_printf(MSG_DEBUG, "WPS: Received Credential");
670	os_memset(&wps->cred, 0, sizeof(wps->cred));
671	wpabuf_set(&msg, cred, cred_len);
672	if (wps_parse_msg(&msg, &attr) < 0 ||
673	    wps_process_cred(&attr, &wps->cred))
674		return -1;
675
676	if (os_memcmp(wps->cred.mac_addr, wps->wps->dev.mac_addr, ETH_ALEN) !=
677	    0) {
678		wpa_printf(MSG_DEBUG, "WPS: MAC Address in the Credential ("
679			   MACSTR ") does not match with own address (" MACSTR
680			   ")", MAC2STR(wps->cred.mac_addr),
681			   MAC2STR(wps->wps->dev.mac_addr));
682		/*
683		 * In theory, this could be consider fatal error, but there are
684		 * number of deployed implementations using other address here
685		 * due to unclarity in the specification. For interoperability
686		 * reasons, allow this to be processed since we do not really
687		 * use the MAC Address information for anything.
688		 */
689#ifdef CONFIG_WPS_STRICT
690		if (wps2) {
691			wpa_printf(MSG_INFO, "WPS: Do not accept incorrect "
692				   "MAC Address in AP Settings");
693			return -1;
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			return -2;
706		}
707
708		wpa_printf(MSG_INFO, "WPS: Reject Credential due to "
709			   "invalid encr_type 0x%x", wps->cred.encr_type);
710		return -1;
711	}
712#endif /* CONFIG_WPS2 */
713
714	if (wps->wps->cred_cb) {
715		wps->cred.cred_attr = cred - 4;
716		wps->cred.cred_attr_len = cred_len + 4;
717		wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
718		wps->cred.cred_attr = NULL;
719		wps->cred.cred_attr_len = 0;
720	}
721
722	return 0;
723}
724
725
726static int wps_process_creds(struct wps_data *wps, const u8 *cred[],
727			     size_t cred_len[], size_t num_cred, int wps2)
728{
729	size_t i;
730	int ok = 0;
731
732	if (wps->wps->ap)
733		return 0;
734
735	if (num_cred == 0) {
736		wpa_printf(MSG_DEBUG, "WPS: No Credential attributes "
737			   "received");
738		return -1;
739	}
740
741	for (i = 0; i < num_cred; i++) {
742		int res;
743		res = wps_process_cred_e(wps, cred[i], cred_len[i], wps2);
744		if (res == 0)
745			ok++;
746		else if (res == -2)
747			wpa_printf(MSG_DEBUG, "WPS: WEP credential skipped");
748		else
749			return -1;
750	}
751
752	if (ok == 0) {
753		wpa_printf(MSG_DEBUG, "WPS: No valid Credential attribute "
754			   "received");
755		return -1;
756	}
757
758	return 0;
759}
760
761
762static int wps_process_ap_settings_e(struct wps_data *wps,
763				     struct wps_parse_attr *attr,
764				     struct wpabuf *attrs, int wps2)
765{
766	struct wps_credential cred;
767
768	if (!wps->wps->ap)
769		return 0;
770
771	if (wps_process_ap_settings(attr, &cred) < 0)
772		return -1;
773
774	wpa_printf(MSG_INFO, "WPS: Received new AP configuration from "
775		   "Registrar");
776
777	if (os_memcmp(cred.mac_addr, wps->wps->dev.mac_addr, ETH_ALEN) !=
778	    0) {
779		wpa_printf(MSG_DEBUG, "WPS: MAC Address in the AP Settings ("
780			   MACSTR ") does not match with own address (" MACSTR
781			   ")", MAC2STR(cred.mac_addr),
782			   MAC2STR(wps->wps->dev.mac_addr));
783		/*
784		 * In theory, this could be consider fatal error, but there are
785		 * number of deployed implementations using other address here
786		 * due to unclarity in the specification. For interoperability
787		 * reasons, allow this to be processed since we do not really
788		 * use the MAC Address information for anything.
789		 */
790#ifdef CONFIG_WPS_STRICT
791		if (wps2) {
792			wpa_printf(MSG_INFO, "WPS: Do not accept incorrect "
793				   "MAC Address in AP Settings");
794			return -1;
795		}
796#endif /* CONFIG_WPS_STRICT */
797	}
798
799#ifdef CONFIG_WPS2
800	if (!(cred.encr_type & (WPS_ENCR_NONE | WPS_ENCR_TKIP | WPS_ENCR_AES)))
801	{
802		if (cred.encr_type & WPS_ENCR_WEP) {
803			wpa_printf(MSG_INFO, "WPS: Reject new AP settings "
804				   "due to WEP configuration");
805			wps->error_indication = WPS_EI_SECURITY_WEP_PROHIBITED;
806			return -1;
807		}
808
809		wpa_printf(MSG_INFO, "WPS: Reject new AP settings due to "
810			   "invalid encr_type 0x%x", cred.encr_type);
811		return -1;
812	}
813#endif /* CONFIG_WPS2 */
814
815#ifdef CONFIG_WPS_STRICT
816	if (wps2) {
817		if ((cred.encr_type & (WPS_ENCR_TKIP | WPS_ENCR_AES)) ==
818		    WPS_ENCR_TKIP ||
819		    (cred.auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) ==
820		    WPS_AUTH_WPAPSK) {
821			wpa_printf(MSG_INFO, "WPS-STRICT: Invalid WSC 2.0 "
822				   "AP Settings: WPA-Personal/TKIP only");
823			wps->error_indication =
824				WPS_EI_SECURITY_TKIP_ONLY_PROHIBITED;
825			return -1;
826		}
827	}
828#endif /* CONFIG_WPS_STRICT */
829
830#ifdef CONFIG_WPS2
831	if ((cred.encr_type & (WPS_ENCR_TKIP | WPS_ENCR_AES)) == WPS_ENCR_TKIP)
832	{
833		wpa_printf(MSG_DEBUG, "WPS: Upgrade encr_type TKIP -> "
834			   "TKIP+AES");
835		cred.encr_type |= WPS_ENCR_AES;
836	}
837
838	if ((cred.auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) ==
839	    WPS_AUTH_WPAPSK) {
840		wpa_printf(MSG_DEBUG, "WPS: Upgrade auth_type WPAPSK -> "
841			   "WPAPSK+WPA2PSK");
842		cred.auth_type |= WPS_AUTH_WPA2PSK;
843	}
844#endif /* CONFIG_WPS2 */
845
846	if (wps->wps->cred_cb) {
847		cred.cred_attr = wpabuf_head(attrs);
848		cred.cred_attr_len = wpabuf_len(attrs);
849		wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
850	}
851
852	return 0;
853}
854
855
856static enum wps_process_res wps_process_m2(struct wps_data *wps,
857					   const struct wpabuf *msg,
858					   struct wps_parse_attr *attr)
859{
860	wpa_printf(MSG_DEBUG, "WPS: Received M2");
861
862	if (wps->state != RECV_M2) {
863		wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
864			   "receiving M2", wps->state);
865		wps->state = SEND_WSC_NACK;
866		return WPS_CONTINUE;
867	}
868
869	if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
870	    wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
871	    wps_process_uuid_r(wps, attr->uuid_r)) {
872		wps->state = SEND_WSC_NACK;
873		return WPS_CONTINUE;
874	}
875
876	/*
877	 * Stop here on an AP as an Enrollee if AP Setup is locked unless the
878	 * special locked mode is used to allow protocol run up to M7 in order
879	 * to support external Registrars that only learn the current AP
880	 * configuration without changing it.
881	 */
882	if (wps->wps->ap &&
883	    ((wps->wps->ap_setup_locked && wps->wps->ap_setup_locked != 2) ||
884	     wps->dev_password == NULL)) {
885		wpa_printf(MSG_DEBUG, "WPS: AP Setup is locked - refuse "
886			   "registration of a new Registrar");
887		wps->config_error = WPS_CFG_SETUP_LOCKED;
888		wps->state = SEND_WSC_NACK;
889		return WPS_CONTINUE;
890	}
891
892	if (wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
893	    wps_process_authenticator(wps, attr->authenticator, msg) ||
894	    wps_process_device_attrs(&wps->peer_dev, attr)) {
895		wps->state = SEND_WSC_NACK;
896		return WPS_CONTINUE;
897	}
898
899	wps->state = SEND_M3;
900	return WPS_CONTINUE;
901}
902
903
904static enum wps_process_res wps_process_m2d(struct wps_data *wps,
905					    struct wps_parse_attr *attr)
906{
907	wpa_printf(MSG_DEBUG, "WPS: Received M2D");
908
909	if (wps->state != RECV_M2) {
910		wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
911			   "receiving M2D", wps->state);
912		wps->state = SEND_WSC_NACK;
913		return WPS_CONTINUE;
914	}
915
916	wpa_hexdump_ascii(MSG_DEBUG, "WPS: Manufacturer",
917			  attr->manufacturer, attr->manufacturer_len);
918	wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Name",
919			  attr->model_name, attr->model_name_len);
920	wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Number",
921			  attr->model_number, attr->model_number_len);
922	wpa_hexdump_ascii(MSG_DEBUG, "WPS: Serial Number",
923			  attr->serial_number, attr->serial_number_len);
924	wpa_hexdump_ascii(MSG_DEBUG, "WPS: Device Name",
925			  attr->dev_name, attr->dev_name_len);
926
927	if (wps->wps->event_cb) {
928		union wps_event_data data;
929		struct wps_event_m2d *m2d = &data.m2d;
930		os_memset(&data, 0, sizeof(data));
931		if (attr->config_methods)
932			m2d->config_methods =
933				WPA_GET_BE16(attr->config_methods);
934		m2d->manufacturer = attr->manufacturer;
935		m2d->manufacturer_len = attr->manufacturer_len;
936		m2d->model_name = attr->model_name;
937		m2d->model_name_len = attr->model_name_len;
938		m2d->model_number = attr->model_number;
939		m2d->model_number_len = attr->model_number_len;
940		m2d->serial_number = attr->serial_number;
941		m2d->serial_number_len = attr->serial_number_len;
942		m2d->dev_name = attr->dev_name;
943		m2d->dev_name_len = attr->dev_name_len;
944		m2d->primary_dev_type = attr->primary_dev_type;
945		if (attr->config_error)
946			m2d->config_error =
947				WPA_GET_BE16(attr->config_error);
948		if (attr->dev_password_id)
949			m2d->dev_password_id =
950				WPA_GET_BE16(attr->dev_password_id);
951		wps->wps->event_cb(wps->wps->cb_ctx, WPS_EV_M2D, &data);
952	}
953
954	wps->state = RECEIVED_M2D;
955	return WPS_CONTINUE;
956}
957
958
959static enum wps_process_res wps_process_m4(struct wps_data *wps,
960					   const struct wpabuf *msg,
961					   struct wps_parse_attr *attr)
962{
963	struct wpabuf *decrypted;
964	struct wps_parse_attr eattr;
965
966	wpa_printf(MSG_DEBUG, "WPS: Received M4");
967
968	if (wps->state != RECV_M4) {
969		wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
970			   "receiving M4", wps->state);
971		wps->state = SEND_WSC_NACK;
972		return WPS_CONTINUE;
973	}
974
975	if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
976	    wps_process_authenticator(wps, attr->authenticator, msg) ||
977	    wps_process_r_hash1(wps, attr->r_hash1) ||
978	    wps_process_r_hash2(wps, attr->r_hash2)) {
979		wps->state = SEND_WSC_NACK;
980		return WPS_CONTINUE;
981	}
982
983	decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
984					      attr->encr_settings_len);
985	if (decrypted == NULL) {
986		wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
987			   "Settings attribute");
988		wps->state = SEND_WSC_NACK;
989		return WPS_CONTINUE;
990	}
991
992	if (wps_validate_m4_encr(decrypted, attr->version2 != NULL) < 0) {
993		wpabuf_free(decrypted);
994		wps->state = SEND_WSC_NACK;
995		return WPS_CONTINUE;
996	}
997
998	wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
999		   "attribute");
1000	if (wps_parse_msg(decrypted, &eattr) < 0 ||
1001	    wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1002	    wps_process_r_snonce1(wps, eattr.r_snonce1)) {
1003		wpabuf_free(decrypted);
1004		wps->state = SEND_WSC_NACK;
1005		return WPS_CONTINUE;
1006	}
1007	wpabuf_free(decrypted);
1008
1009	wps->state = SEND_M5;
1010	return WPS_CONTINUE;
1011}
1012
1013
1014static enum wps_process_res wps_process_m6(struct wps_data *wps,
1015					   const struct wpabuf *msg,
1016					   struct wps_parse_attr *attr)
1017{
1018	struct wpabuf *decrypted;
1019	struct wps_parse_attr eattr;
1020
1021	wpa_printf(MSG_DEBUG, "WPS: Received M6");
1022
1023	if (wps->state != RECV_M6) {
1024		wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1025			   "receiving M6", wps->state);
1026		wps->state = SEND_WSC_NACK;
1027		return WPS_CONTINUE;
1028	}
1029
1030	if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
1031	    wps_process_authenticator(wps, attr->authenticator, msg)) {
1032		wps->state = SEND_WSC_NACK;
1033		return WPS_CONTINUE;
1034	}
1035
1036	decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1037					      attr->encr_settings_len);
1038	if (decrypted == NULL) {
1039		wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1040			   "Settings attribute");
1041		wps->state = SEND_WSC_NACK;
1042		return WPS_CONTINUE;
1043	}
1044
1045	if (wps_validate_m6_encr(decrypted, attr->version2 != NULL) < 0) {
1046		wpabuf_free(decrypted);
1047		wps->state = SEND_WSC_NACK;
1048		return WPS_CONTINUE;
1049	}
1050
1051	wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1052		   "attribute");
1053	if (wps_parse_msg(decrypted, &eattr) < 0 ||
1054	    wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1055	    wps_process_r_snonce2(wps, eattr.r_snonce2)) {
1056		wpabuf_free(decrypted);
1057		wps->state = SEND_WSC_NACK;
1058		return WPS_CONTINUE;
1059	}
1060	wpabuf_free(decrypted);
1061
1062	if (wps->wps->ap)
1063		wps->wps->event_cb(wps->wps->cb_ctx, WPS_EV_AP_PIN_SUCCESS,
1064				   NULL);
1065
1066	wps->state = SEND_M7;
1067	return WPS_CONTINUE;
1068}
1069
1070
1071static enum wps_process_res wps_process_m8(struct wps_data *wps,
1072					   const struct wpabuf *msg,
1073					   struct wps_parse_attr *attr)
1074{
1075	struct wpabuf *decrypted;
1076	struct wps_parse_attr eattr;
1077
1078	wpa_printf(MSG_DEBUG, "WPS: Received M8");
1079
1080	if (wps->state != RECV_M8) {
1081		wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1082			   "receiving M8", wps->state);
1083		wps->state = SEND_WSC_NACK;
1084		return WPS_CONTINUE;
1085	}
1086
1087	if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
1088	    wps_process_authenticator(wps, attr->authenticator, msg)) {
1089		wps->state = SEND_WSC_NACK;
1090		return WPS_CONTINUE;
1091	}
1092
1093	if (wps->wps->ap && wps->wps->ap_setup_locked) {
1094		/*
1095		 * Stop here if special ap_setup_locked == 2 mode allowed the
1096		 * protocol to continue beyond M2. This allows ER to learn the
1097		 * current AP settings without changing them.
1098		 */
1099		wpa_printf(MSG_DEBUG, "WPS: AP Setup is locked - refuse "
1100			   "registration of a new Registrar");
1101		wps->config_error = WPS_CFG_SETUP_LOCKED;
1102		wps->state = SEND_WSC_NACK;
1103		return WPS_CONTINUE;
1104	}
1105
1106	decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1107					      attr->encr_settings_len);
1108	if (decrypted == NULL) {
1109		wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1110			   "Settings attribute");
1111		wps->state = SEND_WSC_NACK;
1112		return WPS_CONTINUE;
1113	}
1114
1115	if (wps_validate_m8_encr(decrypted, wps->wps->ap,
1116				 attr->version2 != NULL) < 0) {
1117		wpabuf_free(decrypted);
1118		wps->state = SEND_WSC_NACK;
1119		return WPS_CONTINUE;
1120	}
1121
1122	wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1123		   "attribute");
1124	if (wps_parse_msg(decrypted, &eattr) < 0 ||
1125	    wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1126	    wps_process_creds(wps, eattr.cred, eattr.cred_len,
1127			      eattr.num_cred, attr->version2 != NULL) ||
1128	    wps_process_ap_settings_e(wps, &eattr, decrypted,
1129				      attr->version2 != NULL)) {
1130		wpabuf_free(decrypted);
1131		wps->state = SEND_WSC_NACK;
1132		return WPS_CONTINUE;
1133	}
1134	wpabuf_free(decrypted);
1135
1136	wps->state = WPS_MSG_DONE;
1137	return WPS_CONTINUE;
1138}
1139
1140
1141static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
1142						const struct wpabuf *msg)
1143{
1144	struct wps_parse_attr attr;
1145	enum wps_process_res ret = WPS_CONTINUE;
1146
1147	wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
1148
1149	if (wps_parse_msg(msg, &attr) < 0)
1150		return WPS_FAILURE;
1151
1152	if (attr.enrollee_nonce == NULL ||
1153	    os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
1154		wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1155		return WPS_FAILURE;
1156	}
1157
1158	if (attr.msg_type == NULL) {
1159		wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1160		wps->state = SEND_WSC_NACK;
1161		return WPS_CONTINUE;
1162	}
1163
1164	switch (*attr.msg_type) {
1165	case WPS_M2:
1166		if (wps_validate_m2(msg) < 0)
1167			return WPS_FAILURE;
1168		ret = wps_process_m2(wps, msg, &attr);
1169		break;
1170	case WPS_M2D:
1171		if (wps_validate_m2d(msg) < 0)
1172			return WPS_FAILURE;
1173		ret = wps_process_m2d(wps, &attr);
1174		break;
1175	case WPS_M4:
1176		if (wps_validate_m4(msg) < 0)
1177			return WPS_FAILURE;
1178		ret = wps_process_m4(wps, msg, &attr);
1179		if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1180			wps_fail_event(wps->wps, WPS_M4, wps->config_error,
1181				       wps->error_indication);
1182		break;
1183	case WPS_M6:
1184		if (wps_validate_m6(msg) < 0)
1185			return WPS_FAILURE;
1186		ret = wps_process_m6(wps, msg, &attr);
1187		if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1188			wps_fail_event(wps->wps, WPS_M6, wps->config_error,
1189				       wps->error_indication);
1190		break;
1191	case WPS_M8:
1192		if (wps_validate_m8(msg) < 0)
1193			return WPS_FAILURE;
1194		ret = wps_process_m8(wps, msg, &attr);
1195		if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1196			wps_fail_event(wps->wps, WPS_M8, wps->config_error,
1197				       wps->error_indication);
1198		break;
1199	default:
1200		wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
1201			   *attr.msg_type);
1202		return WPS_FAILURE;
1203	}
1204
1205	/*
1206	 * Save a copy of the last message for Authenticator derivation if we
1207	 * are continuing. However, skip M2D since it is not authenticated and
1208	 * neither is the ACK/NACK response frame. This allows the possibly
1209	 * following M2 to be processed correctly by using the previously sent
1210	 * M1 in Authenticator derivation.
1211	 */
1212	if (ret == WPS_CONTINUE && *attr.msg_type != WPS_M2D) {
1213		/* Save a copy of the last message for Authenticator derivation
1214		 */
1215		wpabuf_free(wps->last_msg);
1216		wps->last_msg = wpabuf_dup(msg);
1217	}
1218
1219	return ret;
1220}
1221
1222
1223static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
1224						const struct wpabuf *msg)
1225{
1226	struct wps_parse_attr attr;
1227
1228	wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
1229
1230	if (wps_parse_msg(msg, &attr) < 0)
1231		return WPS_FAILURE;
1232
1233	if (attr.msg_type == NULL) {
1234		wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1235		return WPS_FAILURE;
1236	}
1237
1238	if (*attr.msg_type != WPS_WSC_ACK) {
1239		wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1240			   *attr.msg_type);
1241		return WPS_FAILURE;
1242	}
1243
1244	if (attr.registrar_nonce == NULL ||
1245	    os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
1246	{
1247		wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1248		return WPS_FAILURE;
1249	}
1250
1251	if (attr.enrollee_nonce == NULL ||
1252	    os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
1253		wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1254		return WPS_FAILURE;
1255	}
1256
1257	if (wps->state == RECV_ACK && wps->wps->ap) {
1258		wpa_printf(MSG_DEBUG, "WPS: External Registrar registration "
1259			   "completed successfully");
1260		wps_success_event(wps->wps);
1261		wps->state = WPS_FINISHED;
1262		return WPS_DONE;
1263	}
1264
1265	return WPS_FAILURE;
1266}
1267
1268
1269static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
1270						 const struct wpabuf *msg)
1271{
1272	struct wps_parse_attr attr;
1273	u16 config_error;
1274
1275	wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
1276
1277	if (wps_parse_msg(msg, &attr) < 0)
1278		return WPS_FAILURE;
1279
1280	if (attr.msg_type == NULL) {
1281		wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1282		return WPS_FAILURE;
1283	}
1284
1285	if (*attr.msg_type != WPS_WSC_NACK) {
1286		wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1287			   *attr.msg_type);
1288		return WPS_FAILURE;
1289	}
1290
1291	if (attr.registrar_nonce == NULL ||
1292	    os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
1293	{
1294		wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1295		wpa_hexdump(MSG_DEBUG, "WPS: Received Registrar Nonce",
1296			    attr.registrar_nonce, WPS_NONCE_LEN);
1297		wpa_hexdump(MSG_DEBUG, "WPS: Expected Registrar Nonce",
1298			    wps->nonce_r, WPS_NONCE_LEN);
1299		return WPS_FAILURE;
1300	}
1301
1302	if (attr.enrollee_nonce == NULL ||
1303	    os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
1304		wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1305		wpa_hexdump(MSG_DEBUG, "WPS: Received Enrollee Nonce",
1306			    attr.enrollee_nonce, WPS_NONCE_LEN);
1307		wpa_hexdump(MSG_DEBUG, "WPS: Expected Enrollee Nonce",
1308			    wps->nonce_e, WPS_NONCE_LEN);
1309		return WPS_FAILURE;
1310	}
1311
1312	if (attr.config_error == NULL) {
1313		wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
1314			   "in WSC_NACK");
1315		return WPS_FAILURE;
1316	}
1317
1318	config_error = WPA_GET_BE16(attr.config_error);
1319	wpa_printf(MSG_DEBUG, "WPS: Registrar terminated negotiation with "
1320		   "Configuration Error %d", config_error);
1321
1322	switch (wps->state) {
1323	case RECV_M4:
1324		wps_fail_event(wps->wps, WPS_M3, config_error,
1325			       wps->error_indication);
1326		break;
1327	case RECV_M6:
1328		wps_fail_event(wps->wps, WPS_M5, config_error,
1329			       wps->error_indication);
1330		break;
1331	case RECV_M8:
1332		wps_fail_event(wps->wps, WPS_M7, config_error,
1333			       wps->error_indication);
1334		break;
1335	default:
1336		break;
1337	}
1338
1339	/* Followed by NACK if Enrollee is Supplicant or EAP-Failure if
1340	 * Enrollee is Authenticator */
1341	wps->state = SEND_WSC_NACK;
1342
1343	return WPS_FAILURE;
1344}
1345
1346
1347enum wps_process_res wps_enrollee_process_msg(struct wps_data *wps,
1348					      enum wsc_op_code op_code,
1349					      const struct wpabuf *msg)
1350{
1351
1352	wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
1353		   "op_code=%d)",
1354		   (unsigned long) wpabuf_len(msg), op_code);
1355
1356	if (op_code == WSC_UPnP) {
1357		/* Determine the OpCode based on message type attribute */
1358		struct wps_parse_attr attr;
1359		if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type) {
1360			if (*attr.msg_type == WPS_WSC_ACK)
1361				op_code = WSC_ACK;
1362			else if (*attr.msg_type == WPS_WSC_NACK)
1363				op_code = WSC_NACK;
1364		}
1365	}
1366
1367	switch (op_code) {
1368	case WSC_MSG:
1369	case WSC_UPnP:
1370		return wps_process_wsc_msg(wps, msg);
1371	case WSC_ACK:
1372		if (wps_validate_wsc_ack(msg) < 0)
1373			return WPS_FAILURE;
1374		return wps_process_wsc_ack(wps, msg);
1375	case WSC_NACK:
1376		if (wps_validate_wsc_nack(msg) < 0)
1377			return WPS_FAILURE;
1378		return wps_process_wsc_nack(wps, msg);
1379	default:
1380		wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
1381		return WPS_FAILURE;
1382	}
1383}
1384