]> git.ipfire.org Git - thirdparty/hostap.git/blob - src/wps/wps_enrollee.c
WPS: Add support for NFC out-of-band mechanism
[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 "sha256.h"
19 #include "wps_i.h"
20 #include "wps_dev_attr.h"
21 #include "crypto.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 u16 methods;
123
124 if (os_get_random(wps->nonce_e, WPS_NONCE_LEN) < 0)
125 return NULL;
126 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
127 wps->nonce_e, WPS_NONCE_LEN);
128
129 wpa_printf(MSG_DEBUG, "WPS: Building Message M1");
130 msg = wpabuf_alloc(1000);
131 if (msg == NULL)
132 return NULL;
133
134 methods = WPS_CONFIG_LABEL | WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD;
135 #ifdef CONFIG_WPS_UFD
136 methods |= WPS_CONFIG_USBA;
137 #endif /* CONFIG_WPS_UFD */
138 #ifdef CONFIG_WPS_NFC
139 methods |= WPS_CONFIG_NFC_INTERFACE;
140 #endif /* CONFIG_WPS_NFC */
141 if (wps->pbc)
142 methods |= WPS_CONFIG_PUSHBUTTON;
143
144 if (wps_build_version(msg) ||
145 wps_build_msg_type(msg, WPS_M1) ||
146 wps_build_uuid_e(msg, wps->uuid_e) ||
147 wps_build_mac_addr(wps, msg) ||
148 wps_build_enrollee_nonce(wps, msg) ||
149 wps_build_public_key(wps, msg) ||
150 wps_build_auth_type_flags(wps, msg) ||
151 wps_build_encr_type_flags(wps, msg) ||
152 wps_build_conn_type_flags(wps, msg) ||
153 wps_build_config_methods(msg, methods) ||
154 wps_build_wps_state(wps, msg) ||
155 wps_build_device_attrs(&wps->wps->dev, msg) ||
156 wps_build_rf_bands(&wps->wps->dev, msg) ||
157 wps_build_assoc_state(wps, msg) ||
158 wps_build_dev_password_id(msg, wps->dev_pw_id) ||
159 wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
160 wps_build_os_version(&wps->wps->dev, msg)) {
161 wpabuf_free(msg);
162 return NULL;
163 }
164
165 wps->state = RECV_M2;
166 return msg;
167 }
168
169
170 static struct wpabuf * wps_build_m3(struct wps_data *wps)
171 {
172 struct wpabuf *msg;
173
174 wpa_printf(MSG_DEBUG, "WPS: Building Message M3");
175
176 if (wps->dev_password == NULL) {
177 wpa_printf(MSG_DEBUG, "WPS: No Device Password available");
178 return NULL;
179 }
180 wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
181
182 msg = wpabuf_alloc(1000);
183 if (msg == NULL)
184 return NULL;
185
186 if (wps_build_version(msg) ||
187 wps_build_msg_type(msg, WPS_M3) ||
188 wps_build_registrar_nonce(wps, msg) ||
189 wps_build_e_hash(wps, msg) ||
190 wps_build_authenticator(wps, msg)) {
191 wpabuf_free(msg);
192 return NULL;
193 }
194
195 wps->state = RECV_M4;
196 return msg;
197 }
198
199
200 static struct wpabuf * wps_build_m5(struct wps_data *wps)
201 {
202 struct wpabuf *msg, *plain;
203
204 wpa_printf(MSG_DEBUG, "WPS: Building Message M5");
205
206 plain = wpabuf_alloc(200);
207 if (plain == NULL)
208 return NULL;
209
210 msg = wpabuf_alloc(1000);
211 if (msg == NULL) {
212 wpabuf_free(plain);
213 return NULL;
214 }
215
216 if (wps_build_version(msg) ||
217 wps_build_msg_type(msg, WPS_M5) ||
218 wps_build_registrar_nonce(wps, msg) ||
219 wps_build_e_snonce1(wps, plain) ||
220 wps_build_key_wrap_auth(wps, plain) ||
221 wps_build_encr_settings(wps, msg, plain) ||
222 wps_build_authenticator(wps, msg)) {
223 wpabuf_free(plain);
224 wpabuf_free(msg);
225 return NULL;
226 }
227 wpabuf_free(plain);
228
229 wps->state = RECV_M6;
230 return msg;
231 }
232
233
234 static int wps_build_cred_ssid(struct wps_data *wps, struct wpabuf *msg)
235 {
236 wpa_printf(MSG_DEBUG, "WPS: * SSID");
237 wpabuf_put_be16(msg, ATTR_SSID);
238 wpabuf_put_be16(msg, wps->wps->ssid_len);
239 wpabuf_put_data(msg, wps->wps->ssid, wps->wps->ssid_len);
240 return 0;
241 }
242
243
244 static int wps_build_cred_auth_type(struct wps_data *wps, struct wpabuf *msg)
245 {
246 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type");
247 wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
248 wpabuf_put_be16(msg, 2);
249 wpabuf_put_be16(msg, wps->wps->auth_types);
250 return 0;
251 }
252
253
254 static int wps_build_cred_encr_type(struct wps_data *wps, struct wpabuf *msg)
255 {
256 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type");
257 wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
258 wpabuf_put_be16(msg, 2);
259 wpabuf_put_be16(msg, wps->wps->encr_types);
260 return 0;
261 }
262
263
264 static int wps_build_cred_network_key(struct wps_data *wps, struct wpabuf *msg)
265 {
266 wpa_printf(MSG_DEBUG, "WPS: * Network Key");
267 wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
268 wpabuf_put_be16(msg, wps->wps->network_key_len);
269 wpabuf_put_data(msg, wps->wps->network_key, wps->wps->network_key_len);
270 return 0;
271 }
272
273
274 static int wps_build_cred_mac_addr(struct wps_data *wps, struct wpabuf *msg)
275 {
276 wpa_printf(MSG_DEBUG, "WPS: * MAC Address (AP BSSID)");
277 wpabuf_put_be16(msg, ATTR_MAC_ADDR);
278 wpabuf_put_be16(msg, ETH_ALEN);
279 wpabuf_put_data(msg, wps->wps->dev.mac_addr, ETH_ALEN);
280 return 0;
281 }
282
283
284 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *plain)
285 {
286 if (wps->wps->ap_settings) {
287 wpa_printf(MSG_DEBUG, "WPS: * AP Settings (pre-configured)");
288 wpabuf_put_data(plain, wps->wps->ap_settings,
289 wps->wps->ap_settings_len);
290 return 0;
291 }
292
293 return wps_build_cred_ssid(wps, plain) ||
294 wps_build_cred_mac_addr(wps, plain) ||
295 wps_build_cred_auth_type(wps, plain) ||
296 wps_build_cred_encr_type(wps, plain) ||
297 wps_build_cred_network_key(wps, plain);
298 }
299
300
301 static struct wpabuf * wps_build_m7(struct wps_data *wps)
302 {
303 struct wpabuf *msg, *plain;
304
305 wpa_printf(MSG_DEBUG, "WPS: Building Message M7");
306
307 plain = wpabuf_alloc(500 + wps->wps->ap_settings_len);
308 if (plain == NULL)
309 return NULL;
310
311 msg = wpabuf_alloc(1000 + wps->wps->ap_settings_len);
312 if (msg == NULL) {
313 wpabuf_free(plain);
314 return NULL;
315 }
316
317 if (wps_build_version(msg) ||
318 wps_build_msg_type(msg, WPS_M7) ||
319 wps_build_registrar_nonce(wps, msg) ||
320 wps_build_e_snonce2(wps, plain) ||
321 (wps->wps->ap && wps_build_ap_settings(wps, plain)) ||
322 wps_build_key_wrap_auth(wps, plain) ||
323 wps_build_encr_settings(wps, msg, plain) ||
324 wps_build_authenticator(wps, msg)) {
325 wpabuf_free(plain);
326 wpabuf_free(msg);
327 return NULL;
328 }
329 wpabuf_free(plain);
330
331 wps->state = RECV_M8;
332 return msg;
333 }
334
335
336 static struct wpabuf * wps_build_wsc_done(struct wps_data *wps)
337 {
338 struct wpabuf *msg;
339
340 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_Done");
341
342 msg = wpabuf_alloc(1000);
343 if (msg == NULL)
344 return NULL;
345
346 if (wps_build_version(msg) ||
347 wps_build_msg_type(msg, WPS_WSC_DONE) ||
348 wps_build_enrollee_nonce(wps, msg) ||
349 wps_build_registrar_nonce(wps, msg)) {
350 wpabuf_free(msg);
351 return NULL;
352 }
353
354 if (wps->wps->ap)
355 wps->state = RECV_ACK;
356 else {
357 wps_success_event(wps->wps);
358 wps->state = WPS_FINISHED;
359 }
360 return msg;
361 }
362
363
364 static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
365 {
366 struct wpabuf *msg;
367
368 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
369
370 msg = wpabuf_alloc(1000);
371 if (msg == NULL)
372 return NULL;
373
374 if (wps_build_version(msg) ||
375 wps_build_msg_type(msg, WPS_WSC_ACK) ||
376 wps_build_enrollee_nonce(wps, msg) ||
377 wps_build_registrar_nonce(wps, msg)) {
378 wpabuf_free(msg);
379 return NULL;
380 }
381
382 return msg;
383 }
384
385
386 static struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
387 {
388 struct wpabuf *msg;
389
390 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
391
392 msg = wpabuf_alloc(1000);
393 if (msg == NULL)
394 return NULL;
395
396 if (wps_build_version(msg) ||
397 wps_build_msg_type(msg, WPS_WSC_NACK) ||
398 wps_build_enrollee_nonce(wps, msg) ||
399 wps_build_registrar_nonce(wps, msg) ||
400 wps_build_config_error(msg, wps->config_error)) {
401 wpabuf_free(msg);
402 return NULL;
403 }
404
405 return msg;
406 }
407
408
409 struct wpabuf * wps_enrollee_get_msg(struct wps_data *wps,
410 enum wsc_op_code *op_code)
411 {
412 struct wpabuf *msg;
413
414 switch (wps->state) {
415 case SEND_M1:
416 msg = wps_build_m1(wps);
417 *op_code = WSC_MSG;
418 break;
419 case SEND_M3:
420 msg = wps_build_m3(wps);
421 *op_code = WSC_MSG;
422 break;
423 case SEND_M5:
424 msg = wps_build_m5(wps);
425 *op_code = WSC_MSG;
426 break;
427 case SEND_M7:
428 msg = wps_build_m7(wps);
429 *op_code = WSC_MSG;
430 break;
431 case RECEIVED_M2D:
432 if (wps->wps->ap) {
433 msg = wps_build_wsc_nack(wps);
434 *op_code = WSC_NACK;
435 break;
436 }
437 msg = wps_build_wsc_ack(wps);
438 *op_code = WSC_ACK;
439 if (msg) {
440 /* Another M2/M2D may be received */
441 wps->state = RECV_M2;
442 }
443 break;
444 case SEND_WSC_NACK:
445 msg = wps_build_wsc_nack(wps);
446 *op_code = WSC_NACK;
447 break;
448 case WPS_MSG_DONE:
449 msg = wps_build_wsc_done(wps);
450 *op_code = WSC_Done;
451 break;
452 default:
453 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
454 "a message", wps->state);
455 msg = NULL;
456 break;
457 }
458
459 if (*op_code == WSC_MSG && msg) {
460 /* Save a copy of the last message for Authenticator derivation
461 */
462 wpabuf_free(wps->last_msg);
463 wps->last_msg = wpabuf_dup(msg);
464 }
465
466 return msg;
467 }
468
469
470 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
471 {
472 if (r_nonce == NULL) {
473 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
474 return -1;
475 }
476
477 os_memcpy(wps->nonce_r, r_nonce, WPS_NONCE_LEN);
478 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
479 wps->nonce_r, WPS_NONCE_LEN);
480
481 return 0;
482 }
483
484
485 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
486 {
487 if (e_nonce == NULL) {
488 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
489 return -1;
490 }
491
492 if (os_memcmp(wps->nonce_e, e_nonce, WPS_NONCE_LEN) != 0) {
493 wpa_printf(MSG_DEBUG, "WPS: Invalid Enrollee Nonce received");
494 return -1;
495 }
496
497 return 0;
498 }
499
500
501 static int wps_process_uuid_r(struct wps_data *wps, const u8 *uuid_r)
502 {
503 if (uuid_r == NULL) {
504 wpa_printf(MSG_DEBUG, "WPS: No UUID-R received");
505 return -1;
506 }
507
508 os_memcpy(wps->uuid_r, uuid_r, WPS_UUID_LEN);
509 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
510
511 return 0;
512 }
513
514
515 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
516 size_t pk_len)
517 {
518 if (pk == NULL || pk_len == 0) {
519 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
520 return -1;
521 }
522
523 #ifdef CONFIG_WPS_OOB
524 if (wps->dev_pw_id != DEV_PW_DEFAULT &&
525 wps->wps->oob_conf.pubkey_hash) {
526 const u8 *addr[1];
527 u8 hash[WPS_HASH_LEN];
528
529 addr[0] = pk;
530 sha256_vector(1, addr, &pk_len, hash);
531 if (os_memcmp(hash,
532 wpabuf_head(wps->wps->oob_conf.pubkey_hash),
533 WPS_OOB_PUBKEY_HASH_LEN) != 0) {
534 wpa_printf(MSG_ERROR, "WPS: Public Key hash error");
535 return -1;
536 }
537 }
538 #endif /* CONFIG_WPS_OOB */
539
540 wpabuf_free(wps->dh_pubkey_r);
541 wps->dh_pubkey_r = wpabuf_alloc_copy(pk, pk_len);
542 if (wps->dh_pubkey_r == NULL)
543 return -1;
544
545 if (wps_derive_keys(wps) < 0)
546 return -1;
547
548 if (wps->request_type == WPS_REQ_WLAN_MANAGER_REGISTRAR &&
549 wps_derive_mgmt_keys(wps) < 0)
550 return -1;
551
552 return 0;
553 }
554
555
556 static int wps_process_r_hash1(struct wps_data *wps, const u8 *r_hash1)
557 {
558 if (r_hash1 == NULL) {
559 wpa_printf(MSG_DEBUG, "WPS: No R-Hash1 received");
560 return -1;
561 }
562
563 os_memcpy(wps->peer_hash1, r_hash1, WPS_HASH_LEN);
564 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", wps->peer_hash1, WPS_HASH_LEN);
565
566 return 0;
567 }
568
569
570 static int wps_process_r_hash2(struct wps_data *wps, const u8 *r_hash2)
571 {
572 if (r_hash2 == NULL) {
573 wpa_printf(MSG_DEBUG, "WPS: No R-Hash2 received");
574 return -1;
575 }
576
577 os_memcpy(wps->peer_hash2, r_hash2, WPS_HASH_LEN);
578 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", wps->peer_hash2, WPS_HASH_LEN);
579
580 return 0;
581 }
582
583
584 static int wps_process_r_snonce1(struct wps_data *wps, const u8 *r_snonce1)
585 {
586 u8 hash[SHA256_MAC_LEN];
587 const u8 *addr[4];
588 size_t len[4];
589
590 if (r_snonce1 == NULL) {
591 wpa_printf(MSG_DEBUG, "WPS: No R-SNonce1 received");
592 return -1;
593 }
594
595 wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce1", r_snonce1,
596 WPS_SECRET_NONCE_LEN);
597
598 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
599 addr[0] = r_snonce1;
600 len[0] = WPS_SECRET_NONCE_LEN;
601 addr[1] = wps->psk1;
602 len[1] = WPS_PSK_LEN;
603 addr[2] = wpabuf_head(wps->dh_pubkey_e);
604 len[2] = wpabuf_len(wps->dh_pubkey_e);
605 addr[3] = wpabuf_head(wps->dh_pubkey_r);
606 len[3] = wpabuf_len(wps->dh_pubkey_r);
607 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
608
609 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
610 wpa_printf(MSG_DEBUG, "WPS: R-Hash1 derived from R-S1 does "
611 "not match with the pre-committed value");
612 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
613 wps_pwd_auth_fail_event(wps->wps, 1, 1);
614 return -1;
615 }
616
617 wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the first "
618 "half of the device password");
619
620 return 0;
621 }
622
623
624 static int wps_process_r_snonce2(struct wps_data *wps, const u8 *r_snonce2)
625 {
626 u8 hash[SHA256_MAC_LEN];
627 const u8 *addr[4];
628 size_t len[4];
629
630 if (r_snonce2 == NULL) {
631 wpa_printf(MSG_DEBUG, "WPS: No R-SNonce2 received");
632 return -1;
633 }
634
635 wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce2", r_snonce2,
636 WPS_SECRET_NONCE_LEN);
637
638 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
639 addr[0] = r_snonce2;
640 len[0] = WPS_SECRET_NONCE_LEN;
641 addr[1] = wps->psk2;
642 len[1] = WPS_PSK_LEN;
643 addr[2] = wpabuf_head(wps->dh_pubkey_e);
644 len[2] = wpabuf_len(wps->dh_pubkey_e);
645 addr[3] = wpabuf_head(wps->dh_pubkey_r);
646 len[3] = wpabuf_len(wps->dh_pubkey_r);
647 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
648
649 if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
650 wpa_printf(MSG_DEBUG, "WPS: R-Hash2 derived from R-S2 does "
651 "not match with the pre-committed value");
652 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
653 wps_pwd_auth_fail_event(wps->wps, 1, 2);
654 return -1;
655 }
656
657 wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the second "
658 "half of the device password");
659
660 return 0;
661 }
662
663
664 static int wps_process_cred_e(struct wps_data *wps, const u8 *cred,
665 size_t cred_len)
666 {
667 struct wps_parse_attr attr;
668 struct wpabuf msg;
669
670 wpa_printf(MSG_DEBUG, "WPS: Received Credential");
671 os_memset(&wps->cred, 0, sizeof(wps->cred));
672 wpabuf_set(&msg, cred, cred_len);
673 if (wps_parse_msg(&msg, &attr) < 0 ||
674 wps_process_cred(&attr, &wps->cred))
675 return -1;
676
677 if (wps->wps->cred_cb) {
678 wps->cred.cred_attr = cred - 4;
679 wps->cred.cred_attr_len = cred_len + 4;
680 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
681 wps->cred.cred_attr = NULL;
682 wps->cred.cred_attr_len = 0;
683 }
684
685 return 0;
686 }
687
688
689 static int wps_process_creds(struct wps_data *wps, const u8 *cred[],
690 size_t cred_len[], size_t num_cred)
691 {
692 size_t i;
693
694 if (wps->wps->ap)
695 return 0;
696
697 if (num_cred == 0) {
698 wpa_printf(MSG_DEBUG, "WPS: No Credential attributes "
699 "received");
700 return -1;
701 }
702
703 for (i = 0; i < num_cred; i++) {
704 if (wps_process_cred_e(wps, cred[i], cred_len[i]))
705 return -1;
706 }
707
708 return 0;
709 }
710
711
712 static int wps_process_ap_settings_e(struct wps_data *wps,
713 struct wps_parse_attr *attr,
714 struct wpabuf *attrs)
715 {
716 struct wps_credential cred;
717
718 if (!wps->wps->ap)
719 return 0;
720
721 if (wps_process_ap_settings(attr, &cred) < 0)
722 return -1;
723
724 wpa_printf(MSG_INFO, "WPS: Received new AP configuration from "
725 "Registrar");
726
727 if (wps->wps->cred_cb) {
728 cred.cred_attr = wpabuf_head(attrs);
729 cred.cred_attr_len = wpabuf_len(attrs);
730 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
731 }
732
733 return 0;
734 }
735
736
737 static enum wps_process_res wps_process_m2(struct wps_data *wps,
738 const struct wpabuf *msg,
739 struct wps_parse_attr *attr)
740 {
741 wpa_printf(MSG_DEBUG, "WPS: Received M2");
742
743 if (wps->state != RECV_M2) {
744 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
745 "receiving M2", wps->state);
746 wps->state = SEND_WSC_NACK;
747 return WPS_CONTINUE;
748 }
749
750 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
751 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
752 wps_process_uuid_r(wps, attr->uuid_r) ||
753 wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
754 wps_process_authenticator(wps, attr->authenticator, msg)) {
755 wps->state = SEND_WSC_NACK;
756 return WPS_CONTINUE;
757 }
758
759 if (wps->wps->ap && wps->wps->ap_setup_locked) {
760 wpa_printf(MSG_DEBUG, "WPS: AP Setup is locked - refuse "
761 "registration of a new Registrar");
762 wps->config_error = WPS_CFG_SETUP_LOCKED;
763 wps->state = SEND_WSC_NACK;
764 return WPS_CONTINUE;
765 }
766
767 wps->state = SEND_M3;
768 return WPS_CONTINUE;
769 }
770
771
772 static enum wps_process_res wps_process_m2d(struct wps_data *wps,
773 struct wps_parse_attr *attr)
774 {
775 wpa_printf(MSG_DEBUG, "WPS: Received M2D");
776
777 if (wps->state != RECV_M2) {
778 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
779 "receiving M2D", wps->state);
780 wps->state = SEND_WSC_NACK;
781 return WPS_CONTINUE;
782 }
783
784 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Manufacturer",
785 attr->manufacturer, attr->manufacturer_len);
786 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Name",
787 attr->model_name, attr->model_name_len);
788 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Number",
789 attr->model_number, attr->model_number_len);
790 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Serial Number",
791 attr->serial_number, attr->serial_number_len);
792 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Device Name",
793 attr->dev_name, attr->dev_name_len);
794
795 if (wps->wps->event_cb) {
796 union wps_event_data data;
797 struct wps_event_m2d *m2d = &data.m2d;
798 os_memset(&data, 0, sizeof(data));
799 if (attr->config_methods)
800 m2d->config_methods =
801 WPA_GET_BE16(attr->config_methods);
802 m2d->manufacturer = attr->manufacturer;
803 m2d->manufacturer_len = attr->manufacturer_len;
804 m2d->model_name = attr->model_name;
805 m2d->model_name_len = attr->model_name_len;
806 m2d->model_number = attr->model_number;
807 m2d->model_number_len = attr->model_number_len;
808 m2d->serial_number = attr->serial_number;
809 m2d->serial_number_len = attr->serial_number_len;
810 m2d->dev_name = attr->dev_name;
811 m2d->dev_name_len = attr->dev_name_len;
812 m2d->primary_dev_type = attr->primary_dev_type;
813 if (attr->config_error)
814 m2d->config_error =
815 WPA_GET_BE16(attr->config_error);
816 if (attr->dev_password_id)
817 m2d->dev_password_id =
818 WPA_GET_BE16(attr->dev_password_id);
819 wps->wps->event_cb(wps->wps->cb_ctx, WPS_EV_M2D, &data);
820 }
821
822 wps->state = RECEIVED_M2D;
823 return WPS_CONTINUE;
824 }
825
826
827 static enum wps_process_res wps_process_m4(struct wps_data *wps,
828 const struct wpabuf *msg,
829 struct wps_parse_attr *attr)
830 {
831 struct wpabuf *decrypted;
832 struct wps_parse_attr eattr;
833
834 wpa_printf(MSG_DEBUG, "WPS: Received M4");
835
836 if (wps->state != RECV_M4) {
837 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
838 "receiving M4", wps->state);
839 wps->state = SEND_WSC_NACK;
840 return WPS_CONTINUE;
841 }
842
843 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
844 wps_process_authenticator(wps, attr->authenticator, msg) ||
845 wps_process_r_hash1(wps, attr->r_hash1) ||
846 wps_process_r_hash2(wps, attr->r_hash2)) {
847 wps->state = SEND_WSC_NACK;
848 return WPS_CONTINUE;
849 }
850
851 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
852 attr->encr_settings_len);
853 if (decrypted == NULL) {
854 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
855 "Settings attribute");
856 wps->state = SEND_WSC_NACK;
857 return WPS_CONTINUE;
858 }
859
860 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
861 "attribute");
862 if (wps_parse_msg(decrypted, &eattr) < 0 ||
863 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
864 wps_process_r_snonce1(wps, eattr.r_snonce1)) {
865 wpabuf_free(decrypted);
866 wps->state = SEND_WSC_NACK;
867 return WPS_CONTINUE;
868 }
869 wpabuf_free(decrypted);
870
871 wps->state = SEND_M5;
872 return WPS_CONTINUE;
873 }
874
875
876 static enum wps_process_res wps_process_m6(struct wps_data *wps,
877 const struct wpabuf *msg,
878 struct wps_parse_attr *attr)
879 {
880 struct wpabuf *decrypted;
881 struct wps_parse_attr eattr;
882
883 wpa_printf(MSG_DEBUG, "WPS: Received M6");
884
885 if (wps->state != RECV_M6) {
886 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
887 "receiving M6", wps->state);
888 wps->state = SEND_WSC_NACK;
889 return WPS_CONTINUE;
890 }
891
892 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
893 wps_process_authenticator(wps, attr->authenticator, msg)) {
894 wps->state = SEND_WSC_NACK;
895 return WPS_CONTINUE;
896 }
897
898 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
899 attr->encr_settings_len);
900 if (decrypted == NULL) {
901 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
902 "Settings attribute");
903 wps->state = SEND_WSC_NACK;
904 return WPS_CONTINUE;
905 }
906
907 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
908 "attribute");
909 if (wps_parse_msg(decrypted, &eattr) < 0 ||
910 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
911 wps_process_r_snonce2(wps, eattr.r_snonce2)) {
912 wpabuf_free(decrypted);
913 wps->state = SEND_WSC_NACK;
914 return WPS_CONTINUE;
915 }
916 wpabuf_free(decrypted);
917
918 wps->state = SEND_M7;
919 return WPS_CONTINUE;
920 }
921
922
923 static enum wps_process_res wps_process_m8(struct wps_data *wps,
924 const struct wpabuf *msg,
925 struct wps_parse_attr *attr)
926 {
927 struct wpabuf *decrypted;
928 struct wps_parse_attr eattr;
929
930 wpa_printf(MSG_DEBUG, "WPS: Received M8");
931
932 if (wps->state != RECV_M8) {
933 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
934 "receiving M8", wps->state);
935 wps->state = SEND_WSC_NACK;
936 return WPS_CONTINUE;
937 }
938
939 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
940 wps_process_authenticator(wps, attr->authenticator, msg)) {
941 wps->state = SEND_WSC_NACK;
942 return WPS_CONTINUE;
943 }
944
945 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
946 attr->encr_settings_len);
947 if (decrypted == NULL) {
948 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
949 "Settings attribute");
950 wps->state = SEND_WSC_NACK;
951 return WPS_CONTINUE;
952 }
953
954 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
955 "attribute");
956 if (wps_parse_msg(decrypted, &eattr) < 0 ||
957 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
958 wps_process_creds(wps, eattr.cred, eattr.cred_len,
959 eattr.num_cred) ||
960 wps_process_ap_settings_e(wps, &eattr, decrypted)) {
961 wpabuf_free(decrypted);
962 wps->state = SEND_WSC_NACK;
963 return WPS_CONTINUE;
964 }
965 wpabuf_free(decrypted);
966
967 wps->state = WPS_MSG_DONE;
968 return WPS_CONTINUE;
969 }
970
971
972 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
973 const struct wpabuf *msg)
974 {
975 struct wps_parse_attr attr;
976 enum wps_process_res ret = WPS_CONTINUE;
977
978 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
979
980 if (wps_parse_msg(msg, &attr) < 0)
981 return WPS_FAILURE;
982
983 if (!wps_version_supported(attr.version)) {
984 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
985 attr.version ? *attr.version : 0);
986 return WPS_FAILURE;
987 }
988
989 if (attr.enrollee_nonce == NULL ||
990 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
991 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
992 return WPS_FAILURE;
993 }
994
995 if (attr.msg_type == NULL) {
996 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
997 return WPS_FAILURE;
998 }
999
1000 switch (*attr.msg_type) {
1001 case WPS_M2:
1002 ret = wps_process_m2(wps, msg, &attr);
1003 break;
1004 case WPS_M2D:
1005 ret = wps_process_m2d(wps, &attr);
1006 break;
1007 case WPS_M4:
1008 ret = wps_process_m4(wps, msg, &attr);
1009 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1010 wps_fail_event(wps->wps, WPS_M4);
1011 break;
1012 case WPS_M6:
1013 ret = wps_process_m6(wps, msg, &attr);
1014 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1015 wps_fail_event(wps->wps, WPS_M6);
1016 break;
1017 case WPS_M8:
1018 ret = wps_process_m8(wps, msg, &attr);
1019 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1020 wps_fail_event(wps->wps, WPS_M8);
1021 break;
1022 default:
1023 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
1024 *attr.msg_type);
1025 return WPS_FAILURE;
1026 }
1027
1028 /*
1029 * Save a copy of the last message for Authenticator derivation if we
1030 * are continuing. However, skip M2D since it is not authenticated and
1031 * neither is the ACK/NACK response frame. This allows the possibly
1032 * following M2 to be processed correctly by using the previously sent
1033 * M1 in Authenticator derivation.
1034 */
1035 if (ret == WPS_CONTINUE && *attr.msg_type != WPS_M2D) {
1036 /* Save a copy of the last message for Authenticator derivation
1037 */
1038 wpabuf_free(wps->last_msg);
1039 wps->last_msg = wpabuf_dup(msg);
1040 }
1041
1042 return ret;
1043 }
1044
1045
1046 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
1047 const struct wpabuf *msg)
1048 {
1049 struct wps_parse_attr attr;
1050
1051 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
1052
1053 if (wps_parse_msg(msg, &attr) < 0)
1054 return WPS_FAILURE;
1055
1056 if (!wps_version_supported(attr.version)) {
1057 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1058 attr.version ? *attr.version : 0);
1059 return WPS_FAILURE;
1060 }
1061
1062 if (attr.msg_type == NULL) {
1063 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1064 return WPS_FAILURE;
1065 }
1066
1067 if (*attr.msg_type != WPS_WSC_ACK) {
1068 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1069 *attr.msg_type);
1070 return WPS_FAILURE;
1071 }
1072
1073 if (attr.registrar_nonce == NULL ||
1074 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1075 {
1076 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1077 return WPS_FAILURE;
1078 }
1079
1080 if (attr.enrollee_nonce == NULL ||
1081 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1082 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1083 return WPS_FAILURE;
1084 }
1085
1086 if (wps->state == RECV_ACK && wps->wps->ap) {
1087 wpa_printf(MSG_DEBUG, "WPS: External Registrar registration "
1088 "completed successfully");
1089 wps_success_event(wps->wps);
1090 wps->state = WPS_FINISHED;
1091 return WPS_DONE;
1092 }
1093
1094 return WPS_FAILURE;
1095 }
1096
1097
1098 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
1099 const struct wpabuf *msg)
1100 {
1101 struct wps_parse_attr attr;
1102
1103 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
1104
1105 if (wps_parse_msg(msg, &attr) < 0)
1106 return WPS_FAILURE;
1107
1108 if (!wps_version_supported(attr.version)) {
1109 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1110 attr.version ? *attr.version : 0);
1111 return WPS_FAILURE;
1112 }
1113
1114 if (attr.msg_type == NULL) {
1115 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1116 return WPS_FAILURE;
1117 }
1118
1119 if (*attr.msg_type != WPS_WSC_NACK) {
1120 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1121 *attr.msg_type);
1122 return WPS_FAILURE;
1123 }
1124
1125 if (attr.registrar_nonce == NULL ||
1126 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1127 {
1128 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1129 wpa_hexdump(MSG_DEBUG, "WPS: Received Registrar Nonce",
1130 attr.registrar_nonce, WPS_NONCE_LEN);
1131 wpa_hexdump(MSG_DEBUG, "WPS: Expected Registrar Nonce",
1132 wps->nonce_r, WPS_NONCE_LEN);
1133 return WPS_FAILURE;
1134 }
1135
1136 if (attr.enrollee_nonce == NULL ||
1137 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1138 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1139 wpa_hexdump(MSG_DEBUG, "WPS: Received Enrollee Nonce",
1140 attr.enrollee_nonce, WPS_NONCE_LEN);
1141 wpa_hexdump(MSG_DEBUG, "WPS: Expected Enrollee Nonce",
1142 wps->nonce_e, WPS_NONCE_LEN);
1143 return WPS_FAILURE;
1144 }
1145
1146 if (attr.config_error == NULL) {
1147 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
1148 "in WSC_NACK");
1149 return WPS_FAILURE;
1150 }
1151
1152 wpa_printf(MSG_DEBUG, "WPS: Registrar terminated negotiation with "
1153 "Configuration Error %d", WPA_GET_BE16(attr.config_error));
1154
1155 switch (wps->state) {
1156 case RECV_M4:
1157 wps_fail_event(wps->wps, WPS_M3);
1158 break;
1159 case RECV_M6:
1160 wps_fail_event(wps->wps, WPS_M5);
1161 break;
1162 case RECV_M8:
1163 wps_fail_event(wps->wps, WPS_M7);
1164 break;
1165 default:
1166 break;
1167 }
1168
1169 /* Followed by NACK if Enrollee is Supplicant or EAP-Failure if
1170 * Enrollee is Authenticator */
1171 wps->state = SEND_WSC_NACK;
1172
1173 return WPS_FAILURE;
1174 }
1175
1176
1177 enum wps_process_res wps_enrollee_process_msg(struct wps_data *wps,
1178 enum wsc_op_code op_code,
1179 const struct wpabuf *msg)
1180 {
1181
1182 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
1183 "op_code=%d)",
1184 (unsigned long) wpabuf_len(msg), op_code);
1185
1186 switch (op_code) {
1187 case WSC_MSG:
1188 case WSC_UPnP:
1189 return wps_process_wsc_msg(wps, msg);
1190 case WSC_ACK:
1191 return wps_process_wsc_ack(wps, msg);
1192 case WSC_NACK:
1193 return wps_process_wsc_nack(wps, msg);
1194 default:
1195 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
1196 return WPS_FAILURE;
1197 }
1198 }