1 /*
2 * Wi-Fi Protected Setup - Enrollee
3 * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9 #include "includes.h"
10
11 #include "common.h"
12 #include "crypto/crypto.h"
13 #include "crypto/sha256.h"
14 #include "crypto/random.h"
15 #include "wps_i.h"
16 #include "wps_dev_attr.h"
17
18
wps_build_wps_state(struct wps_data * wps,struct wpabuf * msg)19 static int wps_build_wps_state(struct wps_data *wps, struct wpabuf *msg)
20 {
21 u8 state;
22 if (wps->wps->ap)
23 state = wps->wps->wps_state;
24 else
25 state = WPS_STATE_NOT_CONFIGURED;
26 wpa_printf(MSG_DEBUG, "WPS: * Wi-Fi Protected Setup State (%d)",
27 state);
28 wpabuf_put_be16(msg, ATTR_WPS_STATE);
29 wpabuf_put_be16(msg, 1);
30 wpabuf_put_u8(msg, state);
31 return 0;
32 }
33
34
wps_build_e_hash(struct wps_data * wps,struct wpabuf * msg)35 static int wps_build_e_hash(struct wps_data *wps, struct wpabuf *msg)
36 {
37 u8 *hash;
38 const u8 *addr[4];
39 size_t len[4];
40
41 if (random_get_bytes(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
42 return -1;
43 wpa_hexdump(MSG_DEBUG, "WPS: E-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
44 wpa_hexdump(MSG_DEBUG, "WPS: E-S2",
45 wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
46
47 if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
48 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
49 "E-Hash derivation");
50 return -1;
51 }
52
53 wpa_printf(MSG_DEBUG, "WPS: * E-Hash1");
54 wpabuf_put_be16(msg, ATTR_E_HASH1);
55 wpabuf_put_be16(msg, SHA256_MAC_LEN);
56 hash = wpabuf_put(msg, SHA256_MAC_LEN);
57 /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
58 addr[0] = wps->snonce;
59 len[0] = WPS_SECRET_NONCE_LEN;
60 addr[1] = wps->psk1;
61 len[1] = WPS_PSK_LEN;
62 addr[2] = wpabuf_head(wps->dh_pubkey_e);
63 len[2] = wpabuf_len(wps->dh_pubkey_e);
64 addr[3] = wpabuf_head(wps->dh_pubkey_r);
65 len[3] = wpabuf_len(wps->dh_pubkey_r);
66 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
67 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", hash, SHA256_MAC_LEN);
68
69 wpa_printf(MSG_DEBUG, "WPS: * E-Hash2");
70 wpabuf_put_be16(msg, ATTR_E_HASH2);
71 wpabuf_put_be16(msg, SHA256_MAC_LEN);
72 hash = wpabuf_put(msg, SHA256_MAC_LEN);
73 /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
74 addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
75 addr[1] = wps->psk2;
76 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
77 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", hash, SHA256_MAC_LEN);
78
79 return 0;
80 }
81
82
wps_build_e_snonce1(struct wps_data * wps,struct wpabuf * msg)83 static int wps_build_e_snonce1(struct wps_data *wps, struct wpabuf *msg)
84 {
85 wpa_printf(MSG_DEBUG, "WPS: * E-SNonce1");
86 wpabuf_put_be16(msg, ATTR_E_SNONCE1);
87 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
88 wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
89 return 0;
90 }
91
92
wps_build_e_snonce2(struct wps_data * wps,struct wpabuf * msg)93 static int wps_build_e_snonce2(struct wps_data *wps, struct wpabuf *msg)
94 {
95 wpa_printf(MSG_DEBUG, "WPS: * E-SNonce2");
96 wpabuf_put_be16(msg, ATTR_E_SNONCE2);
97 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
98 wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
99 WPS_SECRET_NONCE_LEN);
100 return 0;
101 }
102
103
wps_build_m1(struct wps_data * wps)104 static struct wpabuf * wps_build_m1(struct wps_data *wps)
105 {
106 struct wpabuf *msg;
107 u16 config_methods;
108 u8 multi_ap_backhaul_sta = 0;
109
110 if (random_get_bytes(wps->nonce_e, WPS_NONCE_LEN) < 0)
111 return NULL;
112 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
113 wps->nonce_e, WPS_NONCE_LEN);
114
115 wpa_printf(MSG_DEBUG, "WPS: Building Message M1");
116 msg = wpabuf_alloc(1000);
117 if (msg == NULL)
118 return NULL;
119
120 config_methods = wps->wps->config_methods;
121 if (wps->wps->ap && !wps->pbc_in_m1 &&
122 (wps->dev_password_len != 0 ||
123 (config_methods & WPS_CONFIG_DISPLAY))) {
124 /*
125 * These are the methods that the AP supports as an Enrollee
126 * for adding external Registrars, so remove PushButton.
127 *
128 * As a workaround for Windows 7 mechanism for probing WPS
129 * capabilities from M1, leave PushButton option if no PIN
130 * method is available or if WPS configuration enables PBC
131 * workaround.
132 */
133 config_methods &= ~WPS_CONFIG_PUSHBUTTON;
134 config_methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
135 WPS_CONFIG_PHY_PUSHBUTTON);
136 }
137
138 if (wps->multi_ap_backhaul_sta)
139 multi_ap_backhaul_sta = MULTI_AP_BACKHAUL_STA;
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(msg, wps->mac_addr_e) ||
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, config_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->wps->rf_band_cb(wps->wps->cb_ctx)) ||
155 wps_build_assoc_state(wps, msg) ||
156 wps_build_dev_password_id(msg, wps->dev_pw_id) ||
157 wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
158 wps_build_os_version(&wps->wps->dev, msg) ||
159 wps_build_wfa_ext(msg, 0, NULL, 0, multi_ap_backhaul_sta) ||
160 wps_build_vendor_ext_m1(&wps->wps->dev, msg)) {
161 wpabuf_free(msg);
162 return NULL;
163 }
164
165 wps->state = RECV_M2;
166 return msg;
167 }
168
169
wps_build_m3(struct wps_data * wps)170 static struct wpabuf * wps_build_m3(struct wps_data *wps)
171 {
172 struct wpabuf *msg;
173
174 wpa_printf(MSG_DEBUG, "WPS: Building Message M3");
175
176 if (wps->dev_password == NULL) {
177 wpa_printf(MSG_DEBUG, "WPS: No Device Password available");
178 return NULL;
179 }
180 if (wps_derive_psk(wps, wps->dev_password, wps->dev_password_len) < 0)
181 return NULL;
182
183 if (wps->wps->ap && random_pool_ready() != 1) {
184 wpa_printf(MSG_INFO,
185 "WPS: Not enough entropy in random pool to proceed - do not allow AP PIN to be used");
186 return NULL;
187 }
188
189 msg = wpabuf_alloc(1000);
190 if (msg == NULL)
191 return NULL;
192
193 if (wps_build_version(msg) ||
194 wps_build_msg_type(msg, WPS_M3) ||
195 wps_build_registrar_nonce(wps, msg) ||
196 wps_build_e_hash(wps, msg) ||
197 wps_build_wfa_ext(msg, 0, NULL, 0, 0) ||
198 wps_build_authenticator(wps, msg)) {
199 wpabuf_free(msg);
200 return NULL;
201 }
202
203 wps->state = RECV_M4;
204 return msg;
205 }
206
207
wps_build_m5(struct wps_data * wps)208 static struct wpabuf * wps_build_m5(struct wps_data *wps)
209 {
210 struct wpabuf *msg, *plain;
211
212 wpa_printf(MSG_DEBUG, "WPS: Building Message M5");
213
214 plain = wpabuf_alloc(200);
215 if (plain == NULL)
216 return NULL;
217
218 msg = wpabuf_alloc(1000);
219 if (msg == NULL) {
220 wpabuf_free(plain);
221 return NULL;
222 }
223
224 if (wps_build_version(msg) ||
225 wps_build_msg_type(msg, WPS_M5) ||
226 wps_build_registrar_nonce(wps, msg) ||
227 wps_build_e_snonce1(wps, plain) ||
228 wps_build_key_wrap_auth(wps, plain) ||
229 wps_build_encr_settings(wps, msg, plain) ||
230 wps_build_wfa_ext(msg, 0, NULL, 0, 0) ||
231 wps_build_authenticator(wps, msg)) {
232 wpabuf_clear_free(plain);
233 wpabuf_free(msg);
234 return NULL;
235 }
236 wpabuf_clear_free(plain);
237
238 wps->state = RECV_M6;
239 return msg;
240 }
241
242
wps_build_cred_ssid(struct wps_data * wps,struct wpabuf * msg)243 static int wps_build_cred_ssid(struct wps_data *wps, struct wpabuf *msg)
244 {
245 wpa_printf(MSG_DEBUG, "WPS: * SSID");
246 wpabuf_put_be16(msg, ATTR_SSID);
247 wpabuf_put_be16(msg, wps->wps->ssid_len);
248 wpabuf_put_data(msg, wps->wps->ssid, wps->wps->ssid_len);
249 return 0;
250 }
251
252
wps_build_cred_auth_type(struct wps_data * wps,struct wpabuf * msg)253 static int wps_build_cred_auth_type(struct wps_data *wps, struct wpabuf *msg)
254 {
255 u16 auth_type = wps->wps->ap_auth_type;
256
257 /*
258 * Work around issues with Windows 7 WPS implementation not liking
259 * multiple Authentication Type bits in M7 AP Settings attribute by
260 * showing only the most secure option from current configuration.
261 */
262 if (auth_type & WPS_AUTH_WPA2PSK)
263 auth_type = WPS_AUTH_WPA2PSK;
264 else if (auth_type & WPS_AUTH_WPAPSK)
265 auth_type = WPS_AUTH_WPAPSK;
266 else if (auth_type & WPS_AUTH_OPEN)
267 auth_type = WPS_AUTH_OPEN;
268
269 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type (0x%x)", auth_type);
270 wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
271 wpabuf_put_be16(msg, 2);
272 wpabuf_put_be16(msg, auth_type);
273 return 0;
274 }
275
276
wps_build_cred_encr_type(struct wps_data * wps,struct wpabuf * msg)277 static int wps_build_cred_encr_type(struct wps_data *wps, struct wpabuf *msg)
278 {
279 u16 encr_type = wps->wps->ap_encr_type;
280
281 /*
282 * Work around issues with Windows 7 WPS implementation not liking
283 * multiple Encryption Type bits in M7 AP Settings attribute by
284 * showing only the most secure option from current configuration.
285 */
286 if (wps->wps->ap_auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK)) {
287 if (encr_type & WPS_ENCR_AES)
288 encr_type = WPS_ENCR_AES;
289 else if (encr_type & WPS_ENCR_TKIP)
290 encr_type = WPS_ENCR_TKIP;
291 }
292
293 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type (0x%x)", encr_type);
294 wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
295 wpabuf_put_be16(msg, 2);
296 wpabuf_put_be16(msg, encr_type);
297 return 0;
298 }
299
300
wps_build_cred_network_key(struct wps_data * wps,struct wpabuf * msg)301 static int wps_build_cred_network_key(struct wps_data *wps, struct wpabuf *msg)
302 {
303 if ((wps->wps->ap_auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) &&
304 wps->wps->network_key_len == 0) {
305 char hex[65];
306 u8 psk[32];
307 /* Generate a random per-device PSK */
308 if (random_pool_ready() != 1 ||
309 random_get_bytes(psk, sizeof(psk)) < 0) {
310 wpa_printf(MSG_INFO,
311 "WPS: Could not generate random PSK");
312 return -1;
313 }
314 wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
315 psk, sizeof(psk));
316 wpa_printf(MSG_DEBUG, "WPS: * Network Key (len=%u)",
317 (unsigned int) wps->new_psk_len * 2);
318 wpa_snprintf_hex(hex, sizeof(hex), psk, sizeof(psk));
319 wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
320 wpabuf_put_be16(msg, sizeof(psk) * 2);
321 wpabuf_put_data(msg, hex, sizeof(psk) * 2);
322 if (wps->wps->registrar) {
323 wps_cb_new_psk(wps->wps->registrar,
324 wps->peer_dev.mac_addr,
325 wps->p2p_dev_addr, psk, sizeof(psk));
326 }
327 return 0;
328 }
329
330 wpa_printf(MSG_DEBUG, "WPS: * Network Key (len=%u)",
331 (unsigned int) wps->wps->network_key_len);
332 wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
333 wpabuf_put_be16(msg, wps->wps->network_key_len);
334 wpabuf_put_data(msg, wps->wps->network_key, wps->wps->network_key_len);
335 return 0;
336 }
337
338
wps_build_cred_mac_addr(struct wps_data * wps,struct wpabuf * msg)339 static int wps_build_cred_mac_addr(struct wps_data *wps, struct wpabuf *msg)
340 {
341 wpa_printf(MSG_DEBUG, "WPS: * MAC Address (AP BSSID)");
342 wpabuf_put_be16(msg, ATTR_MAC_ADDR);
343 wpabuf_put_be16(msg, ETH_ALEN);
344 wpabuf_put_data(msg, wps->wps->dev.mac_addr, ETH_ALEN);
345 return 0;
346 }
347
348
wps_build_ap_settings(struct wps_data * wps,struct wpabuf * plain)349 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *plain)
350 {
351 const u8 *start, *end;
352 int ret;
353
354 if (wps->wps->ap_settings) {
355 wpa_printf(MSG_DEBUG, "WPS: * AP Settings (pre-configured)");
356 wpabuf_put_data(plain, wps->wps->ap_settings,
357 wps->wps->ap_settings_len);
358 return 0;
359 }
360
361 wpa_printf(MSG_DEBUG, "WPS: * AP Settings based on current configuration");
362 start = wpabuf_put(plain, 0);
363 ret = wps_build_cred_ssid(wps, plain) ||
364 wps_build_cred_mac_addr(wps, plain) ||
365 wps_build_cred_auth_type(wps, plain) ||
366 wps_build_cred_encr_type(wps, plain) ||
367 wps_build_cred_network_key(wps, plain);
368 end = wpabuf_put(plain, 0);
369
370 wpa_hexdump_key(MSG_DEBUG, "WPS: Plaintext AP Settings",
371 start, end - start);
372
373 return ret;
374 }
375
376
wps_build_m7(struct wps_data * wps)377 static struct wpabuf * wps_build_m7(struct wps_data *wps)
378 {
379 struct wpabuf *msg, *plain;
380
381 wpa_printf(MSG_DEBUG, "WPS: Building Message M7");
382
383 plain = wpabuf_alloc(500 + wps->wps->ap_settings_len);
384 if (plain == NULL)
385 return NULL;
386
387 msg = wpabuf_alloc(1000 + wps->wps->ap_settings_len);
388 if (msg == NULL) {
389 wpabuf_free(plain);
390 return NULL;
391 }
392
393 if (wps_build_version(msg) ||
394 wps_build_msg_type(msg, WPS_M7) ||
395 wps_build_registrar_nonce(wps, msg) ||
396 wps_build_e_snonce2(wps, plain) ||
397 (wps->wps->ap && wps_build_ap_settings(wps, plain)) ||
398 wps_build_key_wrap_auth(wps, plain) ||
399 wps_build_encr_settings(wps, msg, plain) ||
400 wps_build_wfa_ext(msg, 0, NULL, 0, 0) ||
401 wps_build_authenticator(wps, msg)) {
402 wpabuf_clear_free(plain);
403 wpabuf_free(msg);
404 return NULL;
405 }
406 wpabuf_clear_free(plain);
407
408 if (wps->wps->ap && wps->wps->registrar) {
409 /*
410 * If the Registrar is only learning our current configuration,
411 * it may not continue protocol run to successful completion.
412 * Store information here to make sure it remains available.
413 */
414 wps_device_store(wps->wps->registrar, &wps->peer_dev,
415 wps->uuid_r);
416 }
417
418 wps->state = RECV_M8;
419 return msg;
420 }
421
422
wps_build_wsc_done(struct wps_data * wps)423 static struct wpabuf * wps_build_wsc_done(struct wps_data *wps)
424 {
425 struct wpabuf *msg;
426
427 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_Done");
428
429 msg = wpabuf_alloc(1000);
430 if (msg == NULL)
431 return NULL;
432
433 if (wps_build_version(msg) ||
434 wps_build_msg_type(msg, WPS_WSC_DONE) ||
435 wps_build_enrollee_nonce(wps, msg) ||
436 wps_build_registrar_nonce(wps, msg) ||
437 wps_build_wfa_ext(msg, 0, NULL, 0, 0)) {
438 wpabuf_free(msg);
439 return NULL;
440 }
441
442 if (wps->wps->ap)
443 wps->state = RECV_ACK;
444 else {
445 wps_success_event(wps->wps, wps->peer_dev.mac_addr);
446 wps->state = WPS_FINISHED;
447 }
448 return msg;
449 }
450
451
wps_enrollee_get_msg(struct wps_data * wps,enum wsc_op_code * op_code)452 struct wpabuf * wps_enrollee_get_msg(struct wps_data *wps,
453 enum wsc_op_code *op_code)
454 {
455 struct wpabuf *msg;
456
457 switch (wps->state) {
458 case SEND_M1:
459 msg = wps_build_m1(wps);
460 *op_code = WSC_MSG;
461 break;
462 case SEND_M3:
463 msg = wps_build_m3(wps);
464 *op_code = WSC_MSG;
465 break;
466 case SEND_M5:
467 msg = wps_build_m5(wps);
468 *op_code = WSC_MSG;
469 break;
470 case SEND_M7:
471 msg = wps_build_m7(wps);
472 *op_code = WSC_MSG;
473 break;
474 case RECEIVED_M2D:
475 if (wps->wps->ap) {
476 msg = wps_build_wsc_nack(wps);
477 *op_code = WSC_NACK;
478 break;
479 }
480 msg = wps_build_wsc_ack(wps);
481 *op_code = WSC_ACK;
482 if (msg) {
483 /* Another M2/M2D may be received */
484 wps->state = RECV_M2;
485 }
486 break;
487 case SEND_WSC_NACK:
488 msg = wps_build_wsc_nack(wps);
489 *op_code = WSC_NACK;
490 break;
491 case WPS_MSG_DONE:
492 msg = wps_build_wsc_done(wps);
493 *op_code = WSC_Done;
494 break;
495 default:
496 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
497 "a message", wps->state);
498 msg = NULL;
499 break;
500 }
501
502 if (*op_code == WSC_MSG && msg) {
503 /* Save a copy of the last message for Authenticator derivation
504 */
505 wpabuf_free(wps->last_msg);
506 wps->last_msg = wpabuf_dup(msg);
507 }
508
509 return msg;
510 }
511
512
wps_process_registrar_nonce(struct wps_data * wps,const u8 * r_nonce)513 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
514 {
515 if (r_nonce == NULL) {
516 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
517 return -1;
518 }
519
520 os_memcpy(wps->nonce_r, r_nonce, WPS_NONCE_LEN);
521 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
522 wps->nonce_r, WPS_NONCE_LEN);
523
524 return 0;
525 }
526
527
wps_process_enrollee_nonce(struct wps_data * wps,const u8 * e_nonce)528 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
529 {
530 if (e_nonce == NULL) {
531 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
532 return -1;
533 }
534
535 if (os_memcmp(wps->nonce_e, e_nonce, WPS_NONCE_LEN) != 0) {
536 wpa_printf(MSG_DEBUG, "WPS: Invalid Enrollee Nonce received");
537 return -1;
538 }
539
540 return 0;
541 }
542
543
wps_process_uuid_r(struct wps_data * wps,const u8 * uuid_r)544 static int wps_process_uuid_r(struct wps_data *wps, const u8 *uuid_r)
545 {
546 if (uuid_r == NULL) {
547 wpa_printf(MSG_DEBUG, "WPS: No UUID-R received");
548 return -1;
549 }
550
551 os_memcpy(wps->uuid_r, uuid_r, WPS_UUID_LEN);
552 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
553
554 return 0;
555 }
556
557
wps_process_pubkey(struct wps_data * wps,const u8 * pk,size_t pk_len)558 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
559 size_t pk_len)
560 {
561 if (pk == NULL || pk_len == 0) {
562 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
563 return -1;
564 }
565
566 if (wps->peer_pubkey_hash_set) {
567 u8 hash[WPS_HASH_LEN];
568 sha256_vector(1, &pk, &pk_len, hash);
569 if (os_memcmp_const(hash, wps->peer_pubkey_hash,
570 WPS_OOB_PUBKEY_HASH_LEN) != 0) {
571 wpa_printf(MSG_ERROR, "WPS: Public Key hash mismatch");
572 wpa_hexdump(MSG_DEBUG, "WPS: Received public key",
573 pk, pk_len);
574 wpa_hexdump(MSG_DEBUG, "WPS: Calculated public key "
575 "hash", hash, WPS_OOB_PUBKEY_HASH_LEN);
576 wpa_hexdump(MSG_DEBUG, "WPS: Expected public key hash",
577 wps->peer_pubkey_hash,
578 WPS_OOB_PUBKEY_HASH_LEN);
579 wps->config_error = WPS_CFG_PUBLIC_KEY_HASH_MISMATCH;
580 return -1;
581 }
582 }
583
584 wpabuf_free(wps->dh_pubkey_r);
585 wps->dh_pubkey_r = wpabuf_alloc_copy(pk, pk_len);
586 if (wps->dh_pubkey_r == NULL)
587 return -1;
588
589 if (wps_derive_keys(wps) < 0)
590 return -1;
591
592 return 0;
593 }
594
595
wps_process_r_hash1(struct wps_data * wps,const u8 * r_hash1)596 static int wps_process_r_hash1(struct wps_data *wps, const u8 *r_hash1)
597 {
598 if (r_hash1 == NULL) {
599 wpa_printf(MSG_DEBUG, "WPS: No R-Hash1 received");
600 return -1;
601 }
602
603 os_memcpy(wps->peer_hash1, r_hash1, WPS_HASH_LEN);
604 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", wps->peer_hash1, WPS_HASH_LEN);
605
606 return 0;
607 }
608
609
wps_process_r_hash2(struct wps_data * wps,const u8 * r_hash2)610 static int wps_process_r_hash2(struct wps_data *wps, const u8 *r_hash2)
611 {
612 if (r_hash2 == NULL) {
613 wpa_printf(MSG_DEBUG, "WPS: No R-Hash2 received");
614 return -1;
615 }
616
617 os_memcpy(wps->peer_hash2, r_hash2, WPS_HASH_LEN);
618 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", wps->peer_hash2, WPS_HASH_LEN);
619
620 return 0;
621 }
622
623
wps_process_r_snonce1(struct wps_data * wps,const u8 * r_snonce1)624 static int wps_process_r_snonce1(struct wps_data *wps, const u8 *r_snonce1)
625 {
626 u8 hash[SHA256_MAC_LEN];
627 const u8 *addr[4];
628 size_t len[4];
629
630 if (r_snonce1 == NULL) {
631 wpa_printf(MSG_DEBUG, "WPS: No R-SNonce1 received");
632 return -1;
633 }
634
635 wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce1", r_snonce1,
636 WPS_SECRET_NONCE_LEN);
637
638 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
639 addr[0] = r_snonce1;
640 len[0] = WPS_SECRET_NONCE_LEN;
641 addr[1] = wps->psk1;
642 len[1] = WPS_PSK_LEN;
643 addr[2] = wpabuf_head(wps->dh_pubkey_e);
644 len[2] = wpabuf_len(wps->dh_pubkey_e);
645 addr[3] = wpabuf_head(wps->dh_pubkey_r);
646 len[3] = wpabuf_len(wps->dh_pubkey_r);
647 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
648
649 if (os_memcmp_const(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
650 wpa_printf(MSG_DEBUG, "WPS: R-Hash1 derived from R-S1 does "
651 "not match with the pre-committed value");
652 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
653 wps_pwd_auth_fail_event(wps->wps, 1, 1, wps->peer_dev.mac_addr);
654 return -1;
655 }
656
657 wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the first "
658 "half of the device password");
659
660 return 0;
661 }
662
663
wps_process_r_snonce2(struct wps_data * wps,const u8 * r_snonce2)664 static int wps_process_r_snonce2(struct wps_data *wps, const u8 *r_snonce2)
665 {
666 u8 hash[SHA256_MAC_LEN];
667 const u8 *addr[4];
668 size_t len[4];
669
670 if (r_snonce2 == NULL) {
671 wpa_printf(MSG_DEBUG, "WPS: No R-SNonce2 received");
672 return -1;
673 }
674
675 wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce2", r_snonce2,
676 WPS_SECRET_NONCE_LEN);
677
678 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
679 addr[0] = r_snonce2;
680 len[0] = WPS_SECRET_NONCE_LEN;
681 addr[1] = wps->psk2;
682 len[1] = WPS_PSK_LEN;
683 addr[2] = wpabuf_head(wps->dh_pubkey_e);
684 len[2] = wpabuf_len(wps->dh_pubkey_e);
685 addr[3] = wpabuf_head(wps->dh_pubkey_r);
686 len[3] = wpabuf_len(wps->dh_pubkey_r);
687 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
688
689 if (os_memcmp_const(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
690 wpa_printf(MSG_DEBUG, "WPS: R-Hash2 derived from R-S2 does "
691 "not match with the pre-committed value");
692 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
693 wps_pwd_auth_fail_event(wps->wps, 1, 2, wps->peer_dev.mac_addr);
694 return -1;
695 }
696
697 wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the second "
698 "half of the device password");
699
700 return 0;
701 }
702
703
wps_process_cred_e(struct wps_data * wps,const u8 * cred,size_t cred_len,int wps2)704 static int wps_process_cred_e(struct wps_data *wps, const u8 *cred,
705 size_t cred_len, int wps2)
706 {
707 struct wps_parse_attr attr;
708 struct wpabuf msg;
709 int ret = 0;
710
711 wpa_printf(MSG_DEBUG, "WPS: Received Credential");
712 os_memset(&wps->cred, 0, sizeof(wps->cred));
713 wpabuf_set(&msg, cred, cred_len);
714 if (wps_parse_msg(&msg, &attr) < 0 ||
715 wps_process_cred(&attr, &wps->cred))
716 return -1;
717
718 if (os_memcmp(wps->cred.mac_addr, wps->wps->dev.mac_addr, ETH_ALEN) !=
719 0) {
720 wpa_printf(MSG_DEBUG, "WPS: MAC Address in the Credential ("
721 MACSTR ") does not match with own address (" MACSTR
722 ")", MAC2STR(wps->cred.mac_addr),
723 MAC2STR(wps->wps->dev.mac_addr));
724 /*
725 * In theory, this could be consider fatal error, but there are
726 * number of deployed implementations using other address here
727 * due to unclarity in the specification. For interoperability
728 * reasons, allow this to be processed since we do not really
729 * use the MAC Address information for anything.
730 */
731 #ifdef CONFIG_WPS_STRICT
732 if (wps2) {
733 wpa_printf(MSG_INFO, "WPS: Do not accept incorrect "
734 "MAC Address in AP Settings");
735 return -1;
736 }
737 #endif /* CONFIG_WPS_STRICT */
738 }
739
740 if (!(wps->cred.encr_type &
741 (WPS_ENCR_NONE | WPS_ENCR_TKIP | WPS_ENCR_AES))) {
742 if (wps->cred.encr_type & WPS_ENCR_WEP) {
743 wpa_printf(MSG_INFO, "WPS: Reject Credential "
744 "due to WEP configuration");
745 wps->error_indication = WPS_EI_SECURITY_WEP_PROHIBITED;
746 return -2;
747 }
748
749 wpa_printf(MSG_INFO, "WPS: Reject Credential due to "
750 "invalid encr_type 0x%x", wps->cred.encr_type);
751 return -1;
752 }
753
754 if (wps->wps->cred_cb) {
755 wps->cred.cred_attr = cred - 4;
756 wps->cred.cred_attr_len = cred_len + 4;
757 ret = wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
758 wps->cred.cred_attr = NULL;
759 wps->cred.cred_attr_len = 0;
760 }
761
762 return ret;
763 }
764
765
wps_process_creds(struct wps_data * wps,const u8 * cred[],u16 cred_len[],unsigned int num_cred,int wps2)766 static int wps_process_creds(struct wps_data *wps, const u8 *cred[],
767 u16 cred_len[], unsigned int num_cred, int wps2)
768 {
769 size_t i;
770 int ok = 0;
771
772 if (wps->wps->ap)
773 return 0;
774
775 if (num_cred == 0) {
776 wpa_printf(MSG_DEBUG, "WPS: No Credential attributes "
777 "received");
778 return -1;
779 }
780
781 for (i = 0; i < num_cred; i++) {
782 int res;
783 res = wps_process_cred_e(wps, cred[i], cred_len[i], wps2);
784 if (res == 0)
785 ok++;
786 else if (res == -2)
787 wpa_printf(MSG_DEBUG, "WPS: WEP credential skipped");
788 else
789 return -1;
790 }
791
792 if (ok == 0) {
793 wpa_printf(MSG_DEBUG, "WPS: No valid Credential attribute "
794 "received");
795 return -1;
796 }
797
798 return 0;
799 }
800
801
wps_process_ap_settings_e(struct wps_data * wps,struct wps_parse_attr * attr,struct wpabuf * attrs,int wps2)802 static int wps_process_ap_settings_e(struct wps_data *wps,
803 struct wps_parse_attr *attr,
804 struct wpabuf *attrs, int wps2)
805 {
806 struct wps_credential cred;
807 int ret = 0;
808
809 if (!wps->wps->ap)
810 return 0;
811
812 if (wps_process_ap_settings(attr, &cred) < 0)
813 return -1;
814
815 wpa_printf(MSG_INFO, "WPS: Received new AP configuration from "
816 "Registrar");
817
818 if (os_memcmp(cred.mac_addr, wps->wps->dev.mac_addr, ETH_ALEN) !=
819 0) {
820 wpa_printf(MSG_DEBUG, "WPS: MAC Address in the AP Settings ("
821 MACSTR ") does not match with own address (" MACSTR
822 ")", MAC2STR(cred.mac_addr),
823 MAC2STR(wps->wps->dev.mac_addr));
824 /*
825 * In theory, this could be consider fatal error, but there are
826 * number of deployed implementations using other address here
827 * due to unclarity in the specification. For interoperability
828 * reasons, allow this to be processed since we do not really
829 * use the MAC Address information for anything.
830 */
831 #ifdef CONFIG_WPS_STRICT
832 if (wps2) {
833 wpa_printf(MSG_INFO, "WPS: Do not accept incorrect "
834 "MAC Address in AP Settings");
835 return -1;
836 }
837 #endif /* CONFIG_WPS_STRICT */
838 }
839
840 if (!(cred.encr_type & (WPS_ENCR_NONE | WPS_ENCR_TKIP | WPS_ENCR_AES)))
841 {
842 if (cred.encr_type & WPS_ENCR_WEP) {
843 wpa_printf(MSG_INFO, "WPS: Reject new AP settings "
844 "due to WEP configuration");
845 wps->error_indication = WPS_EI_SECURITY_WEP_PROHIBITED;
846 return -1;
847 }
848
849 wpa_printf(MSG_INFO, "WPS: Reject new AP settings due to "
850 "invalid encr_type 0x%x", cred.encr_type);
851 return -1;
852 }
853
854 #ifdef CONFIG_WPS_STRICT
855 if (wps2) {
856 if ((cred.encr_type & (WPS_ENCR_TKIP | WPS_ENCR_AES)) ==
857 WPS_ENCR_TKIP ||
858 (cred.auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) ==
859 WPS_AUTH_WPAPSK) {
860 wpa_printf(MSG_INFO, "WPS-STRICT: Invalid WSC 2.0 "
861 "AP Settings: WPA-Personal/TKIP only");
862 wps->error_indication =
863 WPS_EI_SECURITY_TKIP_ONLY_PROHIBITED;
864 return -1;
865 }
866 }
867 #endif /* CONFIG_WPS_STRICT */
868
869 if ((cred.encr_type & (WPS_ENCR_TKIP | WPS_ENCR_AES)) == WPS_ENCR_TKIP)
870 {
871 wpa_printf(MSG_DEBUG, "WPS: Upgrade encr_type TKIP -> "
872 "TKIP+AES");
873 cred.encr_type |= WPS_ENCR_AES;
874 }
875
876 if ((cred.auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) ==
877 WPS_AUTH_WPAPSK) {
878 wpa_printf(MSG_DEBUG, "WPS: Upgrade auth_type WPAPSK -> "
879 "WPAPSK+WPA2PSK");
880 cred.auth_type |= WPS_AUTH_WPA2PSK;
881 }
882
883 #ifdef CONFIG_NO_TKIP
884 if (cred.encr_type & WPS_ENCR_TKIP) {
885 wpa_printf(MSG_DEBUG, "WPS: Disable encr_type TKIP");
886 cred.encr_type &= ~WPS_ENCR_TKIP;
887 }
888 if (cred.auth_type & WPS_AUTH_WPAPSK) {
889 wpa_printf(MSG_DEBUG, "WPS: Disable auth_type WPAPSK");
890 cred.auth_type &= ~WPS_AUTH_WPAPSK;
891 }
892 #endif /* CONFIG_NO_TKIP */
893
894 if (wps->wps->cred_cb) {
895 cred.cred_attr = wpabuf_head(attrs);
896 cred.cred_attr_len = wpabuf_len(attrs);
897 ret = wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
898 }
899
900 return ret;
901 }
902
903
wps_process_dev_pw_id(struct wps_data * wps,const u8 * dev_pw_id)904 static int wps_process_dev_pw_id(struct wps_data *wps, const u8 *dev_pw_id)
905 {
906 u16 id;
907
908 if (dev_pw_id == NULL) {
909 wpa_printf(MSG_DEBUG, "WPS: Device Password ID");
910 return -1;
911 }
912
913 id = WPA_GET_BE16(dev_pw_id);
914 if (wps->dev_pw_id == id) {
915 wpa_printf(MSG_DEBUG, "WPS: Device Password ID %u", id);
916 return 0;
917 }
918
919 #ifdef CONFIG_P2P
920 if ((id == DEV_PW_DEFAULT &&
921 wps->dev_pw_id == DEV_PW_REGISTRAR_SPECIFIED) ||
922 (id == DEV_PW_REGISTRAR_SPECIFIED &&
923 wps->dev_pw_id == DEV_PW_DEFAULT)) {
924 /*
925 * Common P2P use cases indicate whether the PIN is from the
926 * client or GO using Device Password Id in M1/M2 in a way that
927 * does not look fully compliant with WSC specification. Anyway,
928 * this is deployed and needs to be allowed, so ignore changes
929 * between Registrar-Specified and Default PIN.
930 */
931 wpa_printf(MSG_DEBUG, "WPS: Allow PIN Device Password ID "
932 "change");
933 return 0;
934 }
935 #endif /* CONFIG_P2P */
936
937 wpa_printf(MSG_DEBUG, "WPS: Registrar trying to change Device Password "
938 "ID from %u to %u", wps->dev_pw_id, id);
939
940 if (wps->dev_pw_id == DEV_PW_PUSHBUTTON && id == DEV_PW_DEFAULT) {
941 wpa_printf(MSG_DEBUG,
942 "WPS: Workaround - ignore PBC-to-PIN change");
943 return 0;
944 }
945
946 if (wps->alt_dev_password && wps->alt_dev_pw_id == id) {
947 wpa_printf(MSG_DEBUG, "WPS: Found a matching Device Password");
948 bin_clear_free(wps->dev_password, wps->dev_password_len);
949 wps->dev_pw_id = wps->alt_dev_pw_id;
950 wps->dev_password = wps->alt_dev_password;
951 wps->dev_password_len = wps->alt_dev_password_len;
952 wps->alt_dev_password = NULL;
953 wps->alt_dev_password_len = 0;
954 return 0;
955 }
956
957 return -1;
958 }
959
960
wps_process_m2(struct wps_data * wps,const struct wpabuf * msg,struct wps_parse_attr * attr)961 static enum wps_process_res wps_process_m2(struct wps_data *wps,
962 const struct wpabuf *msg,
963 struct wps_parse_attr *attr)
964 {
965 wpa_printf(MSG_DEBUG, "WPS: Received M2");
966
967 if (wps->state != RECV_M2) {
968 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
969 "receiving M2", wps->state);
970 wps->state = SEND_WSC_NACK;
971 return WPS_CONTINUE;
972 }
973
974 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
975 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
976 wps_process_uuid_r(wps, attr->uuid_r) ||
977 wps_process_dev_pw_id(wps, attr->dev_password_id)) {
978 wps->state = SEND_WSC_NACK;
979 return WPS_CONTINUE;
980 }
981
982 /*
983 * Stop here on an AP as an Enrollee if AP Setup is locked unless the
984 * special locked mode is used to allow protocol run up to M7 in order
985 * to support external Registrars that only learn the current AP
986 * configuration without changing it.
987 */
988 if (wps->wps->ap &&
989 ((wps->wps->ap_setup_locked && wps->wps->ap_setup_locked != 2) ||
990 wps->dev_password == NULL)) {
991 wpa_printf(MSG_DEBUG, "WPS: AP Setup is locked - refuse "
992 "registration of a new Registrar");
993 wps->config_error = WPS_CFG_SETUP_LOCKED;
994 wps->state = SEND_WSC_NACK;
995 return WPS_CONTINUE;
996 }
997
998 if (wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
999 wps_process_authenticator(wps, attr->authenticator, msg) ||
1000 wps_process_device_attrs(&wps->peer_dev, attr)) {
1001 wps->state = SEND_WSC_NACK;
1002 return WPS_CONTINUE;
1003 }
1004
1005 #ifdef CONFIG_WPS_NFC
1006 if (wps->peer_pubkey_hash_set) {
1007 struct wpabuf *decrypted;
1008 struct wps_parse_attr eattr;
1009
1010 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1011 attr->encr_settings_len);
1012 if (decrypted == NULL) {
1013 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypt "
1014 "Encrypted Settings attribute");
1015 wps->state = SEND_WSC_NACK;
1016 return WPS_CONTINUE;
1017 }
1018
1019 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted "
1020 "Settings attribute");
1021 if (wps_parse_msg(decrypted, &eattr) < 0 ||
1022 wps_process_key_wrap_auth(wps, decrypted,
1023 eattr.key_wrap_auth) ||
1024 wps_process_creds(wps, eattr.cred, eattr.cred_len,
1025 eattr.num_cred, attr->version2 != NULL)) {
1026 wpabuf_clear_free(decrypted);
1027 wps->state = SEND_WSC_NACK;
1028 return WPS_CONTINUE;
1029 }
1030 wpabuf_clear_free(decrypted);
1031
1032 wps->state = WPS_MSG_DONE;
1033 return WPS_CONTINUE;
1034 }
1035 #endif /* CONFIG_WPS_NFC */
1036
1037 wps->state = SEND_M3;
1038 return WPS_CONTINUE;
1039 }
1040
1041
wps_process_m2d(struct wps_data * wps,struct wps_parse_attr * attr)1042 static enum wps_process_res wps_process_m2d(struct wps_data *wps,
1043 struct wps_parse_attr *attr)
1044 {
1045 wpa_printf(MSG_DEBUG, "WPS: Received M2D");
1046
1047 if (wps->state != RECV_M2) {
1048 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1049 "receiving M2D", wps->state);
1050 wps->state = SEND_WSC_NACK;
1051 return WPS_CONTINUE;
1052 }
1053
1054 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Manufacturer",
1055 attr->manufacturer, attr->manufacturer_len);
1056 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Name",
1057 attr->model_name, attr->model_name_len);
1058 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Number",
1059 attr->model_number, attr->model_number_len);
1060 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Serial Number",
1061 attr->serial_number, attr->serial_number_len);
1062 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Device Name",
1063 attr->dev_name, attr->dev_name_len);
1064
1065 if (wps->wps->event_cb) {
1066 union wps_event_data data;
1067 struct wps_event_m2d *m2d = &data.m2d;
1068 os_memset(&data, 0, sizeof(data));
1069 if (attr->config_methods)
1070 m2d->config_methods =
1071 WPA_GET_BE16(attr->config_methods);
1072 m2d->manufacturer = attr->manufacturer;
1073 m2d->manufacturer_len = attr->manufacturer_len;
1074 m2d->model_name = attr->model_name;
1075 m2d->model_name_len = attr->model_name_len;
1076 m2d->model_number = attr->model_number;
1077 m2d->model_number_len = attr->model_number_len;
1078 m2d->serial_number = attr->serial_number;
1079 m2d->serial_number_len = attr->serial_number_len;
1080 m2d->dev_name = attr->dev_name;
1081 m2d->dev_name_len = attr->dev_name_len;
1082 m2d->primary_dev_type = attr->primary_dev_type;
1083 if (attr->config_error)
1084 m2d->config_error =
1085 WPA_GET_BE16(attr->config_error);
1086 if (attr->dev_password_id)
1087 m2d->dev_password_id =
1088 WPA_GET_BE16(attr->dev_password_id);
1089 wps->wps->event_cb(wps->wps->cb_ctx, WPS_EV_M2D, &data);
1090 }
1091
1092 wps->state = RECEIVED_M2D;
1093 return WPS_CONTINUE;
1094 }
1095
1096
wps_process_m4(struct wps_data * wps,const struct wpabuf * msg,struct wps_parse_attr * attr)1097 static enum wps_process_res wps_process_m4(struct wps_data *wps,
1098 const struct wpabuf *msg,
1099 struct wps_parse_attr *attr)
1100 {
1101 struct wpabuf *decrypted;
1102 struct wps_parse_attr eattr;
1103
1104 wpa_printf(MSG_DEBUG, "WPS: Received M4");
1105
1106 if (wps->state != RECV_M4) {
1107 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1108 "receiving M4", wps->state);
1109 wps->state = SEND_WSC_NACK;
1110 return WPS_CONTINUE;
1111 }
1112
1113 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
1114 wps_process_authenticator(wps, attr->authenticator, msg) ||
1115 wps_process_r_hash1(wps, attr->r_hash1) ||
1116 wps_process_r_hash2(wps, attr->r_hash2)) {
1117 wps->state = SEND_WSC_NACK;
1118 return WPS_CONTINUE;
1119 }
1120
1121 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1122 attr->encr_settings_len);
1123 if (decrypted == NULL) {
1124 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1125 "Settings attribute");
1126 wps->state = SEND_WSC_NACK;
1127 return WPS_CONTINUE;
1128 }
1129
1130 if (wps_validate_m4_encr(decrypted, attr->version2 != NULL) < 0) {
1131 wpabuf_clear_free(decrypted);
1132 wps->state = SEND_WSC_NACK;
1133 return WPS_CONTINUE;
1134 }
1135
1136 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1137 "attribute");
1138 if (wps_parse_msg(decrypted, &eattr) < 0 ||
1139 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1140 wps_process_r_snonce1(wps, eattr.r_snonce1)) {
1141 wpabuf_clear_free(decrypted);
1142 wps->state = SEND_WSC_NACK;
1143 return WPS_CONTINUE;
1144 }
1145 wpabuf_clear_free(decrypted);
1146
1147 wps->state = SEND_M5;
1148 return WPS_CONTINUE;
1149 }
1150
1151
wps_process_m6(struct wps_data * wps,const struct wpabuf * msg,struct wps_parse_attr * attr)1152 static enum wps_process_res wps_process_m6(struct wps_data *wps,
1153 const struct wpabuf *msg,
1154 struct wps_parse_attr *attr)
1155 {
1156 struct wpabuf *decrypted;
1157 struct wps_parse_attr eattr;
1158
1159 wpa_printf(MSG_DEBUG, "WPS: Received M6");
1160
1161 if (wps->state != RECV_M6) {
1162 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1163 "receiving M6", wps->state);
1164 wps->state = SEND_WSC_NACK;
1165 return WPS_CONTINUE;
1166 }
1167
1168 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
1169 wps_process_authenticator(wps, attr->authenticator, msg)) {
1170 wps->state = SEND_WSC_NACK;
1171 return WPS_CONTINUE;
1172 }
1173
1174 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1175 attr->encr_settings_len);
1176 if (decrypted == NULL) {
1177 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1178 "Settings attribute");
1179 wps->state = SEND_WSC_NACK;
1180 return WPS_CONTINUE;
1181 }
1182
1183 if (wps_validate_m6_encr(decrypted, attr->version2 != NULL) < 0) {
1184 wpabuf_clear_free(decrypted);
1185 wps->state = SEND_WSC_NACK;
1186 return WPS_CONTINUE;
1187 }
1188
1189 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1190 "attribute");
1191 if (wps_parse_msg(decrypted, &eattr) < 0 ||
1192 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1193 wps_process_r_snonce2(wps, eattr.r_snonce2)) {
1194 wpabuf_clear_free(decrypted);
1195 wps->state = SEND_WSC_NACK;
1196 return WPS_CONTINUE;
1197 }
1198 wpabuf_clear_free(decrypted);
1199
1200 if (wps->wps->ap)
1201 wps->wps->event_cb(wps->wps->cb_ctx, WPS_EV_AP_PIN_SUCCESS,
1202 NULL);
1203
1204 wps->state = SEND_M7;
1205 return WPS_CONTINUE;
1206 }
1207
1208
wps_process_m8(struct wps_data * wps,const struct wpabuf * msg,struct wps_parse_attr * attr)1209 static enum wps_process_res wps_process_m8(struct wps_data *wps,
1210 const struct wpabuf *msg,
1211 struct wps_parse_attr *attr)
1212 {
1213 struct wpabuf *decrypted;
1214 struct wps_parse_attr eattr;
1215
1216 wpa_printf(MSG_DEBUG, "WPS: Received M8");
1217
1218 if (wps->state != RECV_M8) {
1219 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1220 "receiving M8", wps->state);
1221 wps->state = SEND_WSC_NACK;
1222 return WPS_CONTINUE;
1223 }
1224
1225 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
1226 wps_process_authenticator(wps, attr->authenticator, msg)) {
1227 wps->state = SEND_WSC_NACK;
1228 return WPS_CONTINUE;
1229 }
1230
1231 if (wps->wps->ap && wps->wps->ap_setup_locked) {
1232 /*
1233 * Stop here if special ap_setup_locked == 2 mode allowed the
1234 * protocol to continue beyond M2. This allows ER to learn the
1235 * current AP settings without changing them.
1236 */
1237 wpa_printf(MSG_DEBUG, "WPS: AP Setup is locked - refuse "
1238 "registration of a new Registrar");
1239 wps->config_error = WPS_CFG_SETUP_LOCKED;
1240 wps->state = SEND_WSC_NACK;
1241 return WPS_CONTINUE;
1242 }
1243
1244 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1245 attr->encr_settings_len);
1246 if (decrypted == NULL) {
1247 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1248 "Settings attribute");
1249 wps->state = SEND_WSC_NACK;
1250 return WPS_CONTINUE;
1251 }
1252
1253 if (wps_validate_m8_encr(decrypted, wps->wps->ap,
1254 attr->version2 != NULL) < 0) {
1255 wpabuf_clear_free(decrypted);
1256 wps->state = SEND_WSC_NACK;
1257 return WPS_CONTINUE;
1258 }
1259
1260 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1261 "attribute");
1262 if (wps_parse_msg(decrypted, &eattr) < 0 ||
1263 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1264 wps_process_creds(wps, eattr.cred, eattr.cred_len,
1265 eattr.num_cred, attr->version2 != NULL) ||
1266 wps_process_ap_settings_e(wps, &eattr, decrypted,
1267 attr->version2 != NULL)) {
1268 wpabuf_clear_free(decrypted);
1269 wps->state = SEND_WSC_NACK;
1270 return WPS_CONTINUE;
1271 }
1272 wpabuf_clear_free(decrypted);
1273
1274 wps->state = WPS_MSG_DONE;
1275 return WPS_CONTINUE;
1276 }
1277
1278
wps_process_wsc_msg(struct wps_data * wps,const struct wpabuf * msg)1279 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
1280 const struct wpabuf *msg)
1281 {
1282 struct wps_parse_attr attr;
1283 enum wps_process_res ret = WPS_CONTINUE;
1284
1285 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
1286
1287 if (wps_parse_msg(msg, &attr) < 0)
1288 return WPS_FAILURE;
1289
1290 if (attr.enrollee_nonce == NULL ||
1291 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
1292 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1293 return WPS_FAILURE;
1294 }
1295
1296 if (attr.msg_type == NULL) {
1297 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1298 wps->state = SEND_WSC_NACK;
1299 return WPS_CONTINUE;
1300 }
1301
1302 switch (*attr.msg_type) {
1303 case WPS_M2:
1304 if (wps_validate_m2(msg) < 0)
1305 return WPS_FAILURE;
1306 ret = wps_process_m2(wps, msg, &attr);
1307 break;
1308 case WPS_M2D:
1309 if (wps_validate_m2d(msg) < 0)
1310 return WPS_FAILURE;
1311 ret = wps_process_m2d(wps, &attr);
1312 break;
1313 case WPS_M4:
1314 if (wps_validate_m4(msg) < 0)
1315 return WPS_FAILURE;
1316 ret = wps_process_m4(wps, msg, &attr);
1317 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1318 wps_fail_event(wps->wps, WPS_M4, wps->config_error,
1319 wps->error_indication,
1320 wps->peer_dev.mac_addr);
1321 break;
1322 case WPS_M6:
1323 if (wps_validate_m6(msg) < 0)
1324 return WPS_FAILURE;
1325 ret = wps_process_m6(wps, msg, &attr);
1326 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1327 wps_fail_event(wps->wps, WPS_M6, wps->config_error,
1328 wps->error_indication,
1329 wps->peer_dev.mac_addr);
1330 break;
1331 case WPS_M8:
1332 if (wps_validate_m8(msg) < 0)
1333 return WPS_FAILURE;
1334 ret = wps_process_m8(wps, msg, &attr);
1335 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1336 wps_fail_event(wps->wps, WPS_M8, wps->config_error,
1337 wps->error_indication,
1338 wps->peer_dev.mac_addr);
1339 break;
1340 default:
1341 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
1342 *attr.msg_type);
1343 return WPS_FAILURE;
1344 }
1345
1346 /*
1347 * Save a copy of the last message for Authenticator derivation if we
1348 * are continuing. However, skip M2D since it is not authenticated and
1349 * neither is the ACK/NACK response frame. This allows the possibly
1350 * following M2 to be processed correctly by using the previously sent
1351 * M1 in Authenticator derivation.
1352 */
1353 if (ret == WPS_CONTINUE && *attr.msg_type != WPS_M2D) {
1354 /* Save a copy of the last message for Authenticator derivation
1355 */
1356 wpabuf_free(wps->last_msg);
1357 wps->last_msg = wpabuf_dup(msg);
1358 }
1359
1360 return ret;
1361 }
1362
1363
wps_process_wsc_ack(struct wps_data * wps,const struct wpabuf * msg)1364 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
1365 const struct wpabuf *msg)
1366 {
1367 struct wps_parse_attr attr;
1368
1369 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
1370
1371 if (wps_parse_msg(msg, &attr) < 0)
1372 return WPS_FAILURE;
1373
1374 if (attr.msg_type == NULL) {
1375 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1376 return WPS_FAILURE;
1377 }
1378
1379 if (*attr.msg_type != WPS_WSC_ACK) {
1380 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1381 *attr.msg_type);
1382 return WPS_FAILURE;
1383 }
1384
1385 if (attr.registrar_nonce == NULL ||
1386 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
1387 {
1388 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1389 return WPS_FAILURE;
1390 }
1391
1392 if (attr.enrollee_nonce == NULL ||
1393 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
1394 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1395 return WPS_FAILURE;
1396 }
1397
1398 if (wps->state == RECV_ACK && wps->wps->ap) {
1399 wpa_printf(MSG_DEBUG, "WPS: External Registrar registration "
1400 "completed successfully");
1401 wps_success_event(wps->wps, wps->peer_dev.mac_addr);
1402 wps->state = WPS_FINISHED;
1403 return WPS_DONE;
1404 }
1405
1406 return WPS_FAILURE;
1407 }
1408
1409
wps_process_wsc_nack(struct wps_data * wps,const struct wpabuf * msg)1410 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
1411 const struct wpabuf *msg)
1412 {
1413 struct wps_parse_attr attr;
1414 u16 config_error;
1415
1416 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
1417
1418 if (wps_parse_msg(msg, &attr) < 0)
1419 return WPS_FAILURE;
1420
1421 if (attr.msg_type == NULL) {
1422 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1423 return WPS_FAILURE;
1424 }
1425
1426 if (*attr.msg_type != WPS_WSC_NACK) {
1427 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1428 *attr.msg_type);
1429 return WPS_FAILURE;
1430 }
1431
1432 if (attr.registrar_nonce == NULL ||
1433 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
1434 {
1435 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1436 wpa_hexdump(MSG_DEBUG, "WPS: Received Registrar Nonce",
1437 attr.registrar_nonce, WPS_NONCE_LEN);
1438 wpa_hexdump(MSG_DEBUG, "WPS: Expected Registrar Nonce",
1439 wps->nonce_r, WPS_NONCE_LEN);
1440 return WPS_FAILURE;
1441 }
1442
1443 if (attr.enrollee_nonce == NULL ||
1444 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
1445 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1446 wpa_hexdump(MSG_DEBUG, "WPS: Received Enrollee Nonce",
1447 attr.enrollee_nonce, WPS_NONCE_LEN);
1448 wpa_hexdump(MSG_DEBUG, "WPS: Expected Enrollee Nonce",
1449 wps->nonce_e, WPS_NONCE_LEN);
1450 return WPS_FAILURE;
1451 }
1452
1453 if (attr.config_error == NULL) {
1454 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
1455 "in WSC_NACK");
1456 return WPS_FAILURE;
1457 }
1458
1459 config_error = WPA_GET_BE16(attr.config_error);
1460 wpa_printf(MSG_DEBUG, "WPS: Registrar terminated negotiation with "
1461 "Configuration Error %d", config_error);
1462
1463 switch (wps->state) {
1464 case RECV_M4:
1465 wps_fail_event(wps->wps, WPS_M3, config_error,
1466 wps->error_indication, wps->peer_dev.mac_addr);
1467 break;
1468 case RECV_M6:
1469 wps_fail_event(wps->wps, WPS_M5, config_error,
1470 wps->error_indication, wps->peer_dev.mac_addr);
1471 break;
1472 case RECV_M8:
1473 wps_fail_event(wps->wps, WPS_M7, config_error,
1474 wps->error_indication, wps->peer_dev.mac_addr);
1475 break;
1476 default:
1477 break;
1478 }
1479
1480 /* Followed by NACK if Enrollee is Supplicant or EAP-Failure if
1481 * Enrollee is Authenticator */
1482 wps->state = SEND_WSC_NACK;
1483
1484 return WPS_FAILURE;
1485 }
1486
1487
wps_enrollee_process_msg(struct wps_data * wps,enum wsc_op_code op_code,const struct wpabuf * msg)1488 enum wps_process_res wps_enrollee_process_msg(struct wps_data *wps,
1489 enum wsc_op_code op_code,
1490 const struct wpabuf *msg)
1491 {
1492
1493 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
1494 "op_code=%d)",
1495 (unsigned long) wpabuf_len(msg), op_code);
1496
1497 if (op_code == WSC_UPnP) {
1498 /* Determine the OpCode based on message type attribute */
1499 struct wps_parse_attr attr;
1500 if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type) {
1501 if (*attr.msg_type == WPS_WSC_ACK)
1502 op_code = WSC_ACK;
1503 else if (*attr.msg_type == WPS_WSC_NACK)
1504 op_code = WSC_NACK;
1505 }
1506 }
1507
1508 switch (op_code) {
1509 case WSC_MSG:
1510 case WSC_UPnP:
1511 return wps_process_wsc_msg(wps, msg);
1512 case WSC_ACK:
1513 if (wps_validate_wsc_ack(msg) < 0)
1514 return WPS_FAILURE;
1515 return wps_process_wsc_ack(wps, msg);
1516 case WSC_NACK:
1517 if (wps_validate_wsc_nack(msg) < 0)
1518 return WPS_FAILURE;
1519 return wps_process_wsc_nack(wps, msg);
1520 default:
1521 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
1522 return WPS_FAILURE;
1523 }
1524 }
1525