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