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