]> git.ipfire.org Git - thirdparty/hostap.git/blob - src/wps/wps_enrollee.c
Fix Enrollee WPS state attribute in AP
[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 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 #ifdef CONFIG_WPS_OOB
521 if (wps->dev_pw_id != DEV_PW_DEFAULT &&
522 wps->wps->oob_conf.pubkey_hash) {
523 const u8 *addr[1];
524 u8 hash[WPS_HASH_LEN];
525
526 addr[0] = pk;
527 sha256_vector(1, addr, &pk_len, hash);
528 if (os_memcmp(hash,
529 wpabuf_head(wps->wps->oob_conf.pubkey_hash),
530 WPS_OOB_PUBKEY_HASH_LEN) != 0) {
531 wpa_printf(MSG_ERROR, "WPS: Public Key hash error");
532 return -1;
533 }
534 }
535 #endif /* CONFIG_WPS_OOB */
536
537 wpabuf_free(wps->dh_pubkey_r);
538 wps->dh_pubkey_r = wpabuf_alloc_copy(pk, pk_len);
539 if (wps->dh_pubkey_r == NULL)
540 return -1;
541
542 if (wps_derive_keys(wps) < 0)
543 return -1;
544
545 if (wps->request_type == WPS_REQ_WLAN_MANAGER_REGISTRAR &&
546 wps_derive_mgmt_keys(wps) < 0)
547 return -1;
548
549 return 0;
550 }
551
552
553 static int wps_process_r_hash1(struct wps_data *wps, const u8 *r_hash1)
554 {
555 if (r_hash1 == NULL) {
556 wpa_printf(MSG_DEBUG, "WPS: No R-Hash1 received");
557 return -1;
558 }
559
560 os_memcpy(wps->peer_hash1, r_hash1, WPS_HASH_LEN);
561 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", wps->peer_hash1, WPS_HASH_LEN);
562
563 return 0;
564 }
565
566
567 static int wps_process_r_hash2(struct wps_data *wps, const u8 *r_hash2)
568 {
569 if (r_hash2 == NULL) {
570 wpa_printf(MSG_DEBUG, "WPS: No R-Hash2 received");
571 return -1;
572 }
573
574 os_memcpy(wps->peer_hash2, r_hash2, WPS_HASH_LEN);
575 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", wps->peer_hash2, WPS_HASH_LEN);
576
577 return 0;
578 }
579
580
581 static int wps_process_r_snonce1(struct wps_data *wps, const u8 *r_snonce1)
582 {
583 u8 hash[SHA256_MAC_LEN];
584 const u8 *addr[4];
585 size_t len[4];
586
587 if (r_snonce1 == NULL) {
588 wpa_printf(MSG_DEBUG, "WPS: No R-SNonce1 received");
589 return -1;
590 }
591
592 wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce1", r_snonce1,
593 WPS_SECRET_NONCE_LEN);
594
595 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
596 addr[0] = r_snonce1;
597 len[0] = WPS_SECRET_NONCE_LEN;
598 addr[1] = wps->psk1;
599 len[1] = WPS_PSK_LEN;
600 addr[2] = wpabuf_head(wps->dh_pubkey_e);
601 len[2] = wpabuf_len(wps->dh_pubkey_e);
602 addr[3] = wpabuf_head(wps->dh_pubkey_r);
603 len[3] = wpabuf_len(wps->dh_pubkey_r);
604 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
605
606 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
607 wpa_printf(MSG_DEBUG, "WPS: R-Hash1 derived from R-S1 does "
608 "not match with the pre-committed value");
609 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
610 wps_pwd_auth_fail_event(wps->wps, 1, 1);
611 return -1;
612 }
613
614 wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the first "
615 "half of the device password");
616
617 return 0;
618 }
619
620
621 static int wps_process_r_snonce2(struct wps_data *wps, const u8 *r_snonce2)
622 {
623 u8 hash[SHA256_MAC_LEN];
624 const u8 *addr[4];
625 size_t len[4];
626
627 if (r_snonce2 == NULL) {
628 wpa_printf(MSG_DEBUG, "WPS: No R-SNonce2 received");
629 return -1;
630 }
631
632 wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce2", r_snonce2,
633 WPS_SECRET_NONCE_LEN);
634
635 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
636 addr[0] = r_snonce2;
637 len[0] = WPS_SECRET_NONCE_LEN;
638 addr[1] = wps->psk2;
639 len[1] = WPS_PSK_LEN;
640 addr[2] = wpabuf_head(wps->dh_pubkey_e);
641 len[2] = wpabuf_len(wps->dh_pubkey_e);
642 addr[3] = wpabuf_head(wps->dh_pubkey_r);
643 len[3] = wpabuf_len(wps->dh_pubkey_r);
644 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
645
646 if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
647 wpa_printf(MSG_DEBUG, "WPS: R-Hash2 derived from R-S2 does "
648 "not match with the pre-committed value");
649 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
650 wps_pwd_auth_fail_event(wps->wps, 1, 2);
651 return -1;
652 }
653
654 wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the second "
655 "half of the device password");
656
657 return 0;
658 }
659
660
661 static int wps_process_cred_e(struct wps_data *wps, const u8 *cred,
662 size_t cred_len)
663 {
664 struct wps_parse_attr attr;
665 struct wpabuf msg;
666
667 wpa_printf(MSG_DEBUG, "WPS: Received Credential");
668 os_memset(&wps->cred, 0, sizeof(wps->cred));
669 wpabuf_set(&msg, cred, cred_len);
670 if (wps_parse_msg(&msg, &attr) < 0 ||
671 wps_process_cred(&attr, &wps->cred))
672 return -1;
673
674 if (wps->wps->cred_cb) {
675 wps->cred.cred_attr = cred - 4;
676 wps->cred.cred_attr_len = cred_len + 4;
677 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
678 wps->cred.cred_attr = NULL;
679 wps->cred.cred_attr_len = 0;
680 }
681
682 return 0;
683 }
684
685
686 static int wps_process_creds(struct wps_data *wps, const u8 *cred[],
687 size_t cred_len[], size_t num_cred)
688 {
689 size_t i;
690
691 if (wps->wps->ap)
692 return 0;
693
694 if (num_cred == 0) {
695 wpa_printf(MSG_DEBUG, "WPS: No Credential attributes "
696 "received");
697 return -1;
698 }
699
700 for (i = 0; i < num_cred; i++) {
701 if (wps_process_cred_e(wps, cred[i], cred_len[i]))
702 return -1;
703 }
704
705 return 0;
706 }
707
708
709 static int wps_process_ap_settings_e(struct wps_data *wps,
710 struct wps_parse_attr *attr,
711 struct wpabuf *attrs)
712 {
713 struct wps_credential cred;
714
715 if (!wps->wps->ap)
716 return 0;
717
718 if (wps_process_ap_settings(attr, &cred) < 0)
719 return -1;
720
721 wpa_printf(MSG_INFO, "WPS: Received new AP configuration from "
722 "Registrar");
723
724 if (wps->wps->cred_cb) {
725 cred.cred_attr = wpabuf_head(attrs);
726 cred.cred_attr_len = wpabuf_len(attrs);
727 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
728 }
729
730 return 0;
731 }
732
733
734 static enum wps_process_res wps_process_m2(struct wps_data *wps,
735 const struct wpabuf *msg,
736 struct wps_parse_attr *attr)
737 {
738 wpa_printf(MSG_DEBUG, "WPS: Received M2");
739
740 if (wps->state != RECV_M2) {
741 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
742 "receiving M2", wps->state);
743 wps->state = SEND_WSC_NACK;
744 return WPS_CONTINUE;
745 }
746
747 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
748 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
749 wps_process_uuid_r(wps, attr->uuid_r) ||
750 wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
751 wps_process_authenticator(wps, attr->authenticator, msg)) {
752 wps->state = SEND_WSC_NACK;
753 return WPS_CONTINUE;
754 }
755
756 if (wps->wps->ap && wps->wps->ap_setup_locked) {
757 wpa_printf(MSG_DEBUG, "WPS: AP Setup is locked - refuse "
758 "registration of a new Registrar");
759 wps->config_error = WPS_CFG_SETUP_LOCKED;
760 wps->state = SEND_WSC_NACK;
761 return WPS_CONTINUE;
762 }
763
764 wps->state = SEND_M3;
765 return WPS_CONTINUE;
766 }
767
768
769 static enum wps_process_res wps_process_m2d(struct wps_data *wps,
770 struct wps_parse_attr *attr)
771 {
772 wpa_printf(MSG_DEBUG, "WPS: Received M2D");
773
774 if (wps->state != RECV_M2) {
775 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
776 "receiving M2D", wps->state);
777 wps->state = SEND_WSC_NACK;
778 return WPS_CONTINUE;
779 }
780
781 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Manufacturer",
782 attr->manufacturer, attr->manufacturer_len);
783 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Name",
784 attr->model_name, attr->model_name_len);
785 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Number",
786 attr->model_number, attr->model_number_len);
787 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Serial Number",
788 attr->serial_number, attr->serial_number_len);
789 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Device Name",
790 attr->dev_name, attr->dev_name_len);
791
792 if (wps->wps->event_cb) {
793 union wps_event_data data;
794 struct wps_event_m2d *m2d = &data.m2d;
795 os_memset(&data, 0, sizeof(data));
796 if (attr->config_methods)
797 m2d->config_methods =
798 WPA_GET_BE16(attr->config_methods);
799 m2d->manufacturer = attr->manufacturer;
800 m2d->manufacturer_len = attr->manufacturer_len;
801 m2d->model_name = attr->model_name;
802 m2d->model_name_len = attr->model_name_len;
803 m2d->model_number = attr->model_number;
804 m2d->model_number_len = attr->model_number_len;
805 m2d->serial_number = attr->serial_number;
806 m2d->serial_number_len = attr->serial_number_len;
807 m2d->dev_name = attr->dev_name;
808 m2d->dev_name_len = attr->dev_name_len;
809 m2d->primary_dev_type = attr->primary_dev_type;
810 if (attr->config_error)
811 m2d->config_error =
812 WPA_GET_BE16(attr->config_error);
813 if (attr->dev_password_id)
814 m2d->dev_password_id =
815 WPA_GET_BE16(attr->dev_password_id);
816 wps->wps->event_cb(wps->wps->cb_ctx, WPS_EV_M2D, &data);
817 }
818
819 wps->state = RECEIVED_M2D;
820 return WPS_CONTINUE;
821 }
822
823
824 static enum wps_process_res wps_process_m4(struct wps_data *wps,
825 const struct wpabuf *msg,
826 struct wps_parse_attr *attr)
827 {
828 struct wpabuf *decrypted;
829 struct wps_parse_attr eattr;
830
831 wpa_printf(MSG_DEBUG, "WPS: Received M4");
832
833 if (wps->state != RECV_M4) {
834 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
835 "receiving M4", wps->state);
836 wps->state = SEND_WSC_NACK;
837 return WPS_CONTINUE;
838 }
839
840 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
841 wps_process_authenticator(wps, attr->authenticator, msg) ||
842 wps_process_r_hash1(wps, attr->r_hash1) ||
843 wps_process_r_hash2(wps, attr->r_hash2)) {
844 wps->state = SEND_WSC_NACK;
845 return WPS_CONTINUE;
846 }
847
848 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
849 attr->encr_settings_len);
850 if (decrypted == NULL) {
851 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
852 "Settings attribute");
853 wps->state = SEND_WSC_NACK;
854 return WPS_CONTINUE;
855 }
856
857 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
858 "attribute");
859 if (wps_parse_msg(decrypted, &eattr) < 0 ||
860 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
861 wps_process_r_snonce1(wps, eattr.r_snonce1)) {
862 wpabuf_free(decrypted);
863 wps->state = SEND_WSC_NACK;
864 return WPS_CONTINUE;
865 }
866 wpabuf_free(decrypted);
867
868 wps->state = SEND_M5;
869 return WPS_CONTINUE;
870 }
871
872
873 static enum wps_process_res wps_process_m6(struct wps_data *wps,
874 const struct wpabuf *msg,
875 struct wps_parse_attr *attr)
876 {
877 struct wpabuf *decrypted;
878 struct wps_parse_attr eattr;
879
880 wpa_printf(MSG_DEBUG, "WPS: Received M6");
881
882 if (wps->state != RECV_M6) {
883 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
884 "receiving M6", wps->state);
885 wps->state = SEND_WSC_NACK;
886 return WPS_CONTINUE;
887 }
888
889 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
890 wps_process_authenticator(wps, attr->authenticator, msg)) {
891 wps->state = SEND_WSC_NACK;
892 return WPS_CONTINUE;
893 }
894
895 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
896 attr->encr_settings_len);
897 if (decrypted == NULL) {
898 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
899 "Settings attribute");
900 wps->state = SEND_WSC_NACK;
901 return WPS_CONTINUE;
902 }
903
904 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
905 "attribute");
906 if (wps_parse_msg(decrypted, &eattr) < 0 ||
907 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
908 wps_process_r_snonce2(wps, eattr.r_snonce2)) {
909 wpabuf_free(decrypted);
910 wps->state = SEND_WSC_NACK;
911 return WPS_CONTINUE;
912 }
913 wpabuf_free(decrypted);
914
915 wps->state = SEND_M7;
916 return WPS_CONTINUE;
917 }
918
919
920 static enum wps_process_res wps_process_m8(struct wps_data *wps,
921 const struct wpabuf *msg,
922 struct wps_parse_attr *attr)
923 {
924 struct wpabuf *decrypted;
925 struct wps_parse_attr eattr;
926
927 wpa_printf(MSG_DEBUG, "WPS: Received M8");
928
929 if (wps->state != RECV_M8) {
930 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
931 "receiving M8", wps->state);
932 wps->state = SEND_WSC_NACK;
933 return WPS_CONTINUE;
934 }
935
936 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
937 wps_process_authenticator(wps, attr->authenticator, msg)) {
938 wps->state = SEND_WSC_NACK;
939 return WPS_CONTINUE;
940 }
941
942 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
943 attr->encr_settings_len);
944 if (decrypted == NULL) {
945 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
946 "Settings attribute");
947 wps->state = SEND_WSC_NACK;
948 return WPS_CONTINUE;
949 }
950
951 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
952 "attribute");
953 if (wps_parse_msg(decrypted, &eattr) < 0 ||
954 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
955 wps_process_creds(wps, eattr.cred, eattr.cred_len,
956 eattr.num_cred) ||
957 wps_process_ap_settings_e(wps, &eattr, decrypted)) {
958 wpabuf_free(decrypted);
959 wps->state = SEND_WSC_NACK;
960 return WPS_CONTINUE;
961 }
962 wpabuf_free(decrypted);
963
964 wps->state = WPS_MSG_DONE;
965 return WPS_CONTINUE;
966 }
967
968
969 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
970 const struct wpabuf *msg)
971 {
972 struct wps_parse_attr attr;
973 enum wps_process_res ret = WPS_CONTINUE;
974
975 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
976
977 if (wps_parse_msg(msg, &attr) < 0)
978 return WPS_FAILURE;
979
980 if (!wps_version_supported(attr.version)) {
981 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
982 attr.version ? *attr.version : 0);
983 return WPS_FAILURE;
984 }
985
986 if (attr.enrollee_nonce == NULL ||
987 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
988 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
989 return WPS_FAILURE;
990 }
991
992 if (attr.msg_type == NULL) {
993 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
994 return WPS_FAILURE;
995 }
996
997 switch (*attr.msg_type) {
998 case WPS_M2:
999 ret = wps_process_m2(wps, msg, &attr);
1000 break;
1001 case WPS_M2D:
1002 ret = wps_process_m2d(wps, &attr);
1003 break;
1004 case WPS_M4:
1005 ret = wps_process_m4(wps, msg, &attr);
1006 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1007 wps_fail_event(wps->wps, WPS_M4);
1008 break;
1009 case WPS_M6:
1010 ret = wps_process_m6(wps, msg, &attr);
1011 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1012 wps_fail_event(wps->wps, WPS_M6);
1013 break;
1014 case WPS_M8:
1015 ret = wps_process_m8(wps, msg, &attr);
1016 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1017 wps_fail_event(wps->wps, WPS_M8);
1018 break;
1019 default:
1020 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
1021 *attr.msg_type);
1022 return WPS_FAILURE;
1023 }
1024
1025 /*
1026 * Save a copy of the last message for Authenticator derivation if we
1027 * are continuing. However, skip M2D since it is not authenticated and
1028 * neither is the ACK/NACK response frame. This allows the possibly
1029 * following M2 to be processed correctly by using the previously sent
1030 * M1 in Authenticator derivation.
1031 */
1032 if (ret == WPS_CONTINUE && *attr.msg_type != WPS_M2D) {
1033 /* Save a copy of the last message for Authenticator derivation
1034 */
1035 wpabuf_free(wps->last_msg);
1036 wps->last_msg = wpabuf_dup(msg);
1037 }
1038
1039 return ret;
1040 }
1041
1042
1043 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
1044 const struct wpabuf *msg)
1045 {
1046 struct wps_parse_attr attr;
1047
1048 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
1049
1050 if (wps_parse_msg(msg, &attr) < 0)
1051 return WPS_FAILURE;
1052
1053 if (!wps_version_supported(attr.version)) {
1054 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1055 attr.version ? *attr.version : 0);
1056 return WPS_FAILURE;
1057 }
1058
1059 if (attr.msg_type == NULL) {
1060 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1061 return WPS_FAILURE;
1062 }
1063
1064 if (*attr.msg_type != WPS_WSC_ACK) {
1065 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1066 *attr.msg_type);
1067 return WPS_FAILURE;
1068 }
1069
1070 if (attr.registrar_nonce == NULL ||
1071 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1072 {
1073 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1074 return WPS_FAILURE;
1075 }
1076
1077 if (attr.enrollee_nonce == NULL ||
1078 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1079 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1080 return WPS_FAILURE;
1081 }
1082
1083 if (wps->state == RECV_ACK && wps->wps->ap) {
1084 wpa_printf(MSG_DEBUG, "WPS: External Registrar registration "
1085 "completed successfully");
1086 wps_success_event(wps->wps);
1087 wps->state = WPS_FINISHED;
1088 return WPS_DONE;
1089 }
1090
1091 return WPS_FAILURE;
1092 }
1093
1094
1095 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
1096 const struct wpabuf *msg)
1097 {
1098 struct wps_parse_attr attr;
1099
1100 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
1101
1102 if (wps_parse_msg(msg, &attr) < 0)
1103 return WPS_FAILURE;
1104
1105 if (!wps_version_supported(attr.version)) {
1106 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1107 attr.version ? *attr.version : 0);
1108 return WPS_FAILURE;
1109 }
1110
1111 if (attr.msg_type == NULL) {
1112 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1113 return WPS_FAILURE;
1114 }
1115
1116 if (*attr.msg_type != WPS_WSC_NACK) {
1117 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1118 *attr.msg_type);
1119 return WPS_FAILURE;
1120 }
1121
1122 if (attr.registrar_nonce == NULL ||
1123 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1124 {
1125 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1126 wpa_hexdump(MSG_DEBUG, "WPS: Received Registrar Nonce",
1127 attr.registrar_nonce, WPS_NONCE_LEN);
1128 wpa_hexdump(MSG_DEBUG, "WPS: Expected Registrar Nonce",
1129 wps->nonce_r, WPS_NONCE_LEN);
1130 return WPS_FAILURE;
1131 }
1132
1133 if (attr.enrollee_nonce == NULL ||
1134 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1135 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1136 wpa_hexdump(MSG_DEBUG, "WPS: Received Enrollee Nonce",
1137 attr.enrollee_nonce, WPS_NONCE_LEN);
1138 wpa_hexdump(MSG_DEBUG, "WPS: Expected Enrollee Nonce",
1139 wps->nonce_e, WPS_NONCE_LEN);
1140 return WPS_FAILURE;
1141 }
1142
1143 if (attr.config_error == NULL) {
1144 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
1145 "in WSC_NACK");
1146 return WPS_FAILURE;
1147 }
1148
1149 wpa_printf(MSG_DEBUG, "WPS: Registrar terminated negotiation with "
1150 "Configuration Error %d", WPA_GET_BE16(attr.config_error));
1151
1152 switch (wps->state) {
1153 case RECV_M4:
1154 wps_fail_event(wps->wps, WPS_M3);
1155 break;
1156 case RECV_M6:
1157 wps_fail_event(wps->wps, WPS_M5);
1158 break;
1159 case RECV_M8:
1160 wps_fail_event(wps->wps, WPS_M7);
1161 break;
1162 default:
1163 break;
1164 }
1165
1166 /* Followed by NACK if Enrollee is Supplicant or EAP-Failure if
1167 * Enrollee is Authenticator */
1168 wps->state = SEND_WSC_NACK;
1169
1170 return WPS_FAILURE;
1171 }
1172
1173
1174 enum wps_process_res wps_enrollee_process_msg(struct wps_data *wps,
1175 enum wsc_op_code op_code,
1176 const struct wpabuf *msg)
1177 {
1178
1179 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
1180 "op_code=%d)",
1181 (unsigned long) wpabuf_len(msg), op_code);
1182
1183 switch (op_code) {
1184 case WSC_MSG:
1185 case WSC_UPnP:
1186 return wps_process_wsc_msg(wps, msg);
1187 case WSC_ACK:
1188 return wps_process_wsc_ack(wps, msg);
1189 case WSC_NACK:
1190 return wps_process_wsc_nack(wps, msg);
1191 default:
1192 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
1193 return WPS_FAILURE;
1194 }
1195 }