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