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