]> git.ipfire.org Git - thirdparty/hostap.git/blob - wpa_supplicant/wps_supplicant.c
WPS ER: Add PIN configuration and SetSelectedRegistrar call
[thirdparty/hostap.git] / wpa_supplicant / wps_supplicant.c
1 /*
2 * wpa_supplicant / WPS integration
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 "ieee802_11_defs.h"
19 #include "ieee802_11_common.h"
20 #include "wpa_common.h"
21 #include "config.h"
22 #include "eap_peer/eap.h"
23 #include "wpa_supplicant_i.h"
24 #include "driver_i.h"
25 #include "eloop.h"
26 #include "uuid.h"
27 #include "wpa_ctrl.h"
28 #include "notify.h"
29 #include "eap_common/eap_wsc_common.h"
30 #include "blacklist.h"
31 #include "wpa.h"
32 #include "wps_supplicant.h"
33 #include "dh_group5.h"
34
35 #define WPS_PIN_SCAN_IGNORE_SEL_REG 3
36
37 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
38 static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
39
40
41 int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
42 {
43 if (!wpa_s->wps_success &&
44 wpa_s->current_ssid &&
45 eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
46 const u8 *bssid = wpa_s->bssid;
47 if (is_zero_ether_addr(bssid))
48 bssid = wpa_s->pending_bssid;
49
50 wpa_printf(MSG_DEBUG, "WPS: PIN registration with " MACSTR
51 " did not succeed - continue trying to find "
52 "suitable AP", MAC2STR(bssid));
53 wpa_blacklist_add(wpa_s, bssid);
54
55 wpa_supplicant_deauthenticate(wpa_s,
56 WLAN_REASON_DEAUTH_LEAVING);
57 wpa_s->reassociate = 1;
58 wpa_supplicant_req_scan(wpa_s,
59 wpa_s->blacklist_cleared ? 5 : 0, 0);
60 wpa_s->blacklist_cleared = 0;
61 return 1;
62 }
63
64 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
65
66 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
67 !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
68 wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
69 "try to associate with the received credential");
70 wpa_supplicant_deauthenticate(wpa_s,
71 WLAN_REASON_DEAUTH_LEAVING);
72 wpa_s->reassociate = 1;
73 wpa_supplicant_req_scan(wpa_s, 0, 0);
74 return 1;
75 }
76
77 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
78 wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
79 "for external credential processing");
80 wpas_clear_wps(wpa_s);
81 wpa_supplicant_deauthenticate(wpa_s,
82 WLAN_REASON_DEAUTH_LEAVING);
83 return 1;
84 }
85
86 return 0;
87 }
88
89
90 static void wpas_wps_security_workaround(struct wpa_supplicant *wpa_s,
91 struct wpa_ssid *ssid,
92 const struct wps_credential *cred)
93 {
94 struct wpa_driver_capa capa;
95 size_t i;
96 struct wpa_scan_res *bss;
97 const u8 *ie;
98 struct wpa_ie_data adv;
99 int wpa2 = 0, ccmp = 0;
100
101 /*
102 * Many existing WPS APs do not know how to negotiate WPA2 or CCMP in
103 * case they are configured for mixed mode operation (WPA+WPA2 and
104 * TKIP+CCMP). Try to use scan results to figure out whether the AP
105 * actually supports stronger security and select that if the client
106 * has support for it, too.
107 */
108
109 if (wpa_drv_get_capa(wpa_s, &capa))
110 return; /* Unknown what driver supports */
111
112 if (wpa_supplicant_get_scan_results(wpa_s) || wpa_s->scan_res == NULL)
113 return; /* Could not get scan results for checking advertised
114 * parameters */
115
116 for (i = 0; i < wpa_s->scan_res->num; i++) {
117 bss = wpa_s->scan_res->res[i];
118 if (os_memcmp(bss->bssid, cred->mac_addr, ETH_ALEN) != 0)
119 continue;
120 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
121 if (ie == NULL)
122 continue;
123 if (ie[1] != ssid->ssid_len || ssid->ssid == NULL ||
124 os_memcmp(ie + 2, ssid->ssid, ssid->ssid_len) != 0)
125 continue;
126
127 wpa_printf(MSG_DEBUG, "WPS: AP found from scan results");
128 break;
129 }
130
131 if (i == wpa_s->scan_res->num) {
132 wpa_printf(MSG_DEBUG, "WPS: The AP was not found from scan "
133 "results - use credential as-is");
134 return;
135 }
136
137 ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
138 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0) {
139 wpa2 = 1;
140 if (adv.pairwise_cipher & WPA_CIPHER_CCMP)
141 ccmp = 1;
142 } else {
143 ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
144 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0 &&
145 adv.pairwise_cipher & WPA_CIPHER_CCMP)
146 ccmp = 1;
147 }
148
149 if (ie == NULL && (ssid->proto & WPA_PROTO_WPA) &&
150 (ssid->pairwise_cipher & WPA_CIPHER_TKIP)) {
151 /*
152 * TODO: This could be the initial AP configuration and the
153 * Beacon contents could change shortly. Should request a new
154 * scan and delay addition of the network until the updated
155 * scan results are available.
156 */
157 wpa_printf(MSG_DEBUG, "WPS: The AP did not yet advertise WPA "
158 "support - use credential as-is");
159 return;
160 }
161
162 if (ccmp && !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
163 (ssid->pairwise_cipher & WPA_CIPHER_TKIP) &&
164 (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
165 wpa_printf(MSG_DEBUG, "WPS: Add CCMP into the credential "
166 "based on scan results");
167 if (wpa_s->conf->ap_scan == 1)
168 ssid->pairwise_cipher |= WPA_CIPHER_CCMP;
169 else
170 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
171 }
172
173 if (wpa2 && !(ssid->proto & WPA_PROTO_RSN) &&
174 (ssid->proto & WPA_PROTO_WPA) &&
175 (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP)) {
176 wpa_printf(MSG_DEBUG, "WPS: Add WPA2 into the credential "
177 "based on scan results");
178 if (wpa_s->conf->ap_scan == 1)
179 ssid->proto |= WPA_PROTO_RSN;
180 else
181 ssid->proto = WPA_PROTO_RSN;
182 }
183 }
184
185
186 static int wpa_supplicant_wps_cred(void *ctx,
187 const struct wps_credential *cred)
188 {
189 struct wpa_supplicant *wpa_s = ctx;
190 struct wpa_ssid *ssid = wpa_s->current_ssid;
191 u8 key_idx = 0;
192 u16 auth_type;
193
194 if ((wpa_s->conf->wps_cred_processing == 1 ||
195 wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
196 size_t blen = cred->cred_attr_len * 2 + 1;
197 char *buf = os_malloc(blen);
198 if (buf) {
199 wpa_snprintf_hex(buf, blen,
200 cred->cred_attr, cred->cred_attr_len);
201 wpa_msg(wpa_s, MSG_INFO, "%s%s",
202 WPS_EVENT_CRED_RECEIVED, buf);
203 os_free(buf);
204 }
205
206 wpas_notify_wps_credential(wpa_s, cred);
207 } else
208 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
209
210 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
211 cred->cred_attr, cred->cred_attr_len);
212
213 if (wpa_s->conf->wps_cred_processing == 1)
214 return 0;
215
216 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
217 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
218 cred->auth_type);
219 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
220 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
221 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
222 cred->key, cred->key_len);
223 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
224 MAC2STR(cred->mac_addr));
225
226 auth_type = cred->auth_type;
227 if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
228 wpa_printf(MSG_DEBUG, "WPS: Workaround - convert mixed-mode "
229 "auth_type into WPA2PSK");
230 auth_type = WPS_AUTH_WPA2PSK;
231 }
232
233 if (auth_type != WPS_AUTH_OPEN &&
234 auth_type != WPS_AUTH_SHARED &&
235 auth_type != WPS_AUTH_WPAPSK &&
236 auth_type != WPS_AUTH_WPA2PSK) {
237 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
238 "unsupported authentication type 0x%x",
239 auth_type);
240 return 0;
241 }
242
243 if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
244 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
245 "on the received credential");
246 os_free(ssid->eap.identity);
247 ssid->eap.identity = NULL;
248 ssid->eap.identity_len = 0;
249 os_free(ssid->eap.phase1);
250 ssid->eap.phase1 = NULL;
251 os_free(ssid->eap.eap_methods);
252 ssid->eap.eap_methods = NULL;
253 } else {
254 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
255 "received credential");
256 ssid = wpa_config_add_network(wpa_s->conf);
257 if (ssid == NULL)
258 return -1;
259 wpas_notify_network_added(wpa_s, ssid);
260 }
261
262 wpa_config_set_network_defaults(ssid);
263
264 os_free(ssid->ssid);
265 ssid->ssid = os_malloc(cred->ssid_len);
266 if (ssid->ssid) {
267 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
268 ssid->ssid_len = cred->ssid_len;
269 }
270
271 switch (cred->encr_type) {
272 case WPS_ENCR_NONE:
273 break;
274 case WPS_ENCR_WEP:
275 if (cred->key_len <= 0)
276 break;
277 if (cred->key_len != 5 && cred->key_len != 13 &&
278 cred->key_len != 10 && cred->key_len != 26) {
279 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key length "
280 "%lu", (unsigned long) cred->key_len);
281 return -1;
282 }
283 if (cred->key_idx > NUM_WEP_KEYS) {
284 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key index %d",
285 cred->key_idx);
286 return -1;
287 }
288 if (cred->key_idx)
289 key_idx = cred->key_idx - 1;
290 if (cred->key_len == 10 || cred->key_len == 26) {
291 if (hexstr2bin((char *) cred->key,
292 ssid->wep_key[key_idx],
293 cred->key_len / 2) < 0) {
294 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key "
295 "%d", key_idx);
296 return -1;
297 }
298 ssid->wep_key_len[key_idx] = cred->key_len / 2;
299 } else {
300 os_memcpy(ssid->wep_key[key_idx], cred->key,
301 cred->key_len);
302 ssid->wep_key_len[key_idx] = cred->key_len;
303 }
304 ssid->wep_tx_keyidx = key_idx;
305 break;
306 case WPS_ENCR_TKIP:
307 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
308 break;
309 case WPS_ENCR_AES:
310 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
311 break;
312 }
313
314 switch (auth_type) {
315 case WPS_AUTH_OPEN:
316 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
317 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
318 ssid->proto = 0;
319 break;
320 case WPS_AUTH_SHARED:
321 ssid->auth_alg = WPA_AUTH_ALG_SHARED;
322 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
323 ssid->proto = 0;
324 break;
325 case WPS_AUTH_WPAPSK:
326 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
327 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
328 ssid->proto = WPA_PROTO_WPA;
329 break;
330 case WPS_AUTH_WPA:
331 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
332 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
333 ssid->proto = WPA_PROTO_WPA;
334 break;
335 case WPS_AUTH_WPA2:
336 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
337 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
338 ssid->proto = WPA_PROTO_RSN;
339 break;
340 case WPS_AUTH_WPA2PSK:
341 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
342 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
343 ssid->proto = WPA_PROTO_RSN;
344 break;
345 }
346
347 if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
348 if (cred->key_len == 2 * PMK_LEN) {
349 if (hexstr2bin((const char *) cred->key, ssid->psk,
350 PMK_LEN)) {
351 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
352 "Key");
353 return -1;
354 }
355 ssid->psk_set = 1;
356 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
357 os_free(ssid->passphrase);
358 ssid->passphrase = os_malloc(cred->key_len + 1);
359 if (ssid->passphrase == NULL)
360 return -1;
361 os_memcpy(ssid->passphrase, cred->key, cred->key_len);
362 ssid->passphrase[cred->key_len] = '\0';
363 wpa_config_update_psk(ssid);
364 } else {
365 wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
366 "length %lu",
367 (unsigned long) cred->key_len);
368 return -1;
369 }
370 }
371
372 wpas_wps_security_workaround(wpa_s, ssid, cred);
373
374 #ifndef CONFIG_NO_CONFIG_WRITE
375 if (wpa_s->conf->update_config &&
376 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
377 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
378 return -1;
379 }
380 #endif /* CONFIG_NO_CONFIG_WRITE */
381
382 return 0;
383 }
384
385
386 static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
387 struct wps_event_m2d *m2d)
388 {
389 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
390 "dev_password_id=%d config_error=%d",
391 m2d->dev_password_id, m2d->config_error);
392 wpas_notify_wps_event_m2d(wpa_s, m2d);
393 }
394
395
396 static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
397 struct wps_event_fail *fail)
398 {
399 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL "msg=%d", fail->msg);
400 wpas_clear_wps(wpa_s);
401 wpas_notify_wps_event_fail(wpa_s, fail);
402 }
403
404
405 static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
406 {
407 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
408 wpa_s->wps_success = 1;
409 wpas_notify_wps_event_success(wpa_s);
410 }
411
412
413 static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
414 union wps_event_data *data)
415 {
416 struct wpa_supplicant *wpa_s = ctx;
417 switch (event) {
418 case WPS_EV_M2D:
419 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
420 break;
421 case WPS_EV_FAIL:
422 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
423 break;
424 case WPS_EV_SUCCESS:
425 wpa_supplicant_wps_event_success(wpa_s);
426 break;
427 case WPS_EV_PWD_AUTH_FAIL:
428 break;
429 case WPS_EV_PBC_OVERLAP:
430 break;
431 case WPS_EV_PBC_TIMEOUT:
432 break;
433 }
434 }
435
436
437 enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
438 {
439 if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
440 eap_is_wps_pin_enrollee(&ssid->eap))
441 return WPS_REQ_ENROLLEE;
442 else
443 return WPS_REQ_REGISTRAR;
444 }
445
446
447 static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
448 {
449 int id;
450 struct wpa_ssid *ssid, *remove_ssid = NULL;
451
452 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
453
454 /* Remove any existing WPS network from configuration */
455 ssid = wpa_s->conf->ssid;
456 while (ssid) {
457 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
458 if (ssid == wpa_s->current_ssid) {
459 wpa_s->current_ssid = NULL;
460 if (ssid != NULL)
461 wpas_notify_network_changed(wpa_s);
462 }
463 id = ssid->id;
464 remove_ssid = ssid;
465 } else
466 id = -1;
467 ssid = ssid->next;
468 if (id >= 0) {
469 wpas_notify_network_removed(wpa_s, remove_ssid);
470 wpa_config_remove_network(wpa_s->conf, id);
471 }
472 }
473 }
474
475
476 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
477 {
478 struct wpa_supplicant *wpa_s = eloop_ctx;
479 wpa_printf(MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
480 "out");
481 wpas_clear_wps(wpa_s);
482 }
483
484
485 static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
486 int registrar, const u8 *bssid)
487 {
488 struct wpa_ssid *ssid;
489
490 ssid = wpa_config_add_network(wpa_s->conf);
491 if (ssid == NULL)
492 return NULL;
493 wpas_notify_network_added(wpa_s, ssid);
494 wpa_config_set_network_defaults(ssid);
495 if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
496 wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
497 wpa_config_set(ssid, "identity", registrar ?
498 "\"" WSC_ID_REGISTRAR "\"" :
499 "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
500 wpas_notify_network_removed(wpa_s, ssid);
501 wpa_config_remove_network(wpa_s->conf, ssid->id);
502 return NULL;
503 }
504
505 if (bssid) {
506 size_t i;
507 struct wpa_scan_res *res;
508
509 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
510 ssid->bssid_set = 1;
511
512 /* Try to get SSID from scan results */
513 if (wpa_s->scan_res == NULL &&
514 wpa_supplicant_get_scan_results(wpa_s) < 0)
515 return ssid; /* Could not find any scan results */
516
517 for (i = 0; i < wpa_s->scan_res->num; i++) {
518 const u8 *ie;
519
520 res = wpa_s->scan_res->res[i];
521 if (os_memcmp(bssid, res->bssid, ETH_ALEN) != 0)
522 continue;
523
524 ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
525 if (ie == NULL)
526 break;
527 os_free(ssid->ssid);
528 ssid->ssid = os_malloc(ie[1]);
529 if (ssid->ssid == NULL)
530 break;
531 os_memcpy(ssid->ssid, ie + 2, ie[1]);
532 ssid->ssid_len = ie[1];
533 break;
534 }
535 }
536
537 return ssid;
538 }
539
540
541 static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
542 struct wpa_ssid *selected)
543 {
544 struct wpa_ssid *ssid;
545
546 /* Mark all other networks disabled and trigger reassociation */
547 ssid = wpa_s->conf->ssid;
548 while (ssid) {
549 int was_disabled = ssid->disabled;
550 ssid->disabled = ssid != selected;
551 if (was_disabled != ssid->disabled)
552 wpas_notify_network_enabled_changed(wpa_s, ssid);
553 ssid = ssid->next;
554 }
555 wpa_s->disconnected = 0;
556 wpa_s->reassociate = 1;
557 wpa_s->scan_runs = 0;
558 wpa_s->wps_success = 0;
559 wpa_s->blacklist_cleared = 0;
560 wpa_supplicant_req_scan(wpa_s, 0, 0);
561 }
562
563
564 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
565 {
566 struct wpa_ssid *ssid;
567 wpas_clear_wps(wpa_s);
568 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
569 if (ssid == NULL)
570 return -1;
571 wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
572 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
573 wpa_s, NULL);
574 wpas_wps_reassoc(wpa_s, ssid);
575 return 0;
576 }
577
578
579 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
580 const char *pin)
581 {
582 struct wpa_ssid *ssid;
583 char val[128];
584 unsigned int rpin = 0;
585
586 wpas_clear_wps(wpa_s);
587 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
588 if (ssid == NULL)
589 return -1;
590 if (pin)
591 os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
592 else {
593 rpin = wps_generate_pin();
594 os_snprintf(val, sizeof(val), "\"pin=%08d\"", rpin);
595 }
596 wpa_config_set(ssid, "phase1", val, 0);
597 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
598 wpa_s, NULL);
599 wpas_wps_reassoc(wpa_s, ssid);
600 return rpin;
601 }
602
603
604 #ifdef CONFIG_WPS_OOB
605 int wpas_wps_start_oob(struct wpa_supplicant *wpa_s, char *device_type,
606 char *path, char *method, char *name)
607 {
608 struct wps_context *wps = wpa_s->wps;
609 struct oob_device_data *oob_dev;
610
611 oob_dev = wps_get_oob_device(device_type);
612 if (oob_dev == NULL)
613 return -1;
614 oob_dev->device_path = path;
615 oob_dev->device_name = name;
616 wps->oob_conf.oob_method = wps_get_oob_method(method);
617
618 if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E) {
619 /*
620 * Use pre-configured DH keys in order to be able to write the
621 * key hash into the OOB file.
622 */
623 wpabuf_free(wps->dh_pubkey);
624 wpabuf_free(wps->dh_privkey);
625 wps->dh_privkey = NULL;
626 wps->dh_pubkey = NULL;
627 dh5_free(wps->dh_ctx);
628 wps->dh_ctx = dh5_init(&wps->dh_privkey, &wps->dh_pubkey);
629 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
630 if (wps->dh_ctx == NULL || wps->dh_pubkey == NULL) {
631 wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
632 "Diffie-Hellman handshake");
633 return -1;
634 }
635 }
636
637 if (wps->oob_conf.oob_method == OOB_METHOD_CRED)
638 wpas_clear_wps(wpa_s);
639
640 if (wps_process_oob(wps, oob_dev, 0) < 0)
641 return -1;
642
643 if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
644 wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
645 wpas_wps_start_pin(wpa_s, NULL,
646 wpabuf_head(wps->oob_conf.dev_password)) < 0)
647 return -1;
648
649 return 0;
650 }
651 #endif /* CONFIG_WPS_OOB */
652
653
654 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
655 const char *pin, struct wps_new_ap_settings *settings)
656 {
657 struct wpa_ssid *ssid;
658 char val[200];
659 char *pos, *end;
660 int res;
661
662 if (!pin)
663 return -1;
664 wpas_clear_wps(wpa_s);
665 ssid = wpas_wps_add_network(wpa_s, 1, bssid);
666 if (ssid == NULL)
667 return -1;
668 pos = val;
669 end = pos + sizeof(val);
670 res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
671 if (res < 0 || res >= end - pos)
672 return -1;
673 pos += res;
674 if (settings) {
675 res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
676 "new_encr=%s new_key=%s",
677 settings->ssid_hex, settings->auth,
678 settings->encr, settings->key_hex);
679 if (res < 0 || res >= end - pos)
680 return -1;
681 pos += res;
682 }
683 res = os_snprintf(pos, end - pos, "\"");
684 if (res < 0 || res >= end - pos)
685 return -1;
686 wpa_config_set(ssid, "phase1", val, 0);
687 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
688 wpa_s, NULL);
689 wpas_wps_reassoc(wpa_s, ssid);
690 return 0;
691 }
692
693
694 static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
695 size_t psk_len)
696 {
697 wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
698 "STA " MACSTR, MAC2STR(mac_addr));
699 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
700
701 /* TODO */
702
703 return 0;
704 }
705
706
707 static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
708 const struct wps_device_data *dev)
709 {
710 char uuid[40], txt[400];
711 int len;
712 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
713 return;
714 wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
715 len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
716 " [%s|%s|%s|%s|%s|%d-%08X-%d]",
717 uuid, MAC2STR(dev->mac_addr), dev->device_name,
718 dev->manufacturer, dev->model_name,
719 dev->model_number, dev->serial_number,
720 dev->categ, dev->oui, dev->sub_categ);
721 if (len > 0 && len < (int) sizeof(txt))
722 wpa_printf(MSG_INFO, "%s", txt);
723 }
724
725
726 static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
727 u16 sel_reg_config_methods)
728 {
729 #ifdef CONFIG_WPS_ER
730 struct wpa_supplicant *wpa_s = ctx;
731
732 if (wpa_s->wps_er == NULL)
733 return;
734 wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
735 sel_reg_config_methods);
736 #endif /* CONFIG_WPS_ER */
737 }
738
739
740 int wpas_wps_init(struct wpa_supplicant *wpa_s)
741 {
742 struct wps_context *wps;
743 struct wps_registrar_config rcfg;
744
745 wps = os_zalloc(sizeof(*wps));
746 if (wps == NULL)
747 return -1;
748
749 wps->cred_cb = wpa_supplicant_wps_cred;
750 wps->event_cb = wpa_supplicant_wps_event;
751 wps->cb_ctx = wpa_s;
752
753 wps->dev.device_name = wpa_s->conf->device_name;
754 wps->dev.manufacturer = wpa_s->conf->manufacturer;
755 wps->dev.model_name = wpa_s->conf->model_name;
756 wps->dev.model_number = wpa_s->conf->model_number;
757 wps->dev.serial_number = wpa_s->conf->serial_number;
758 if (wpa_s->conf->device_type) {
759 char *pos;
760 u8 oui[4];
761 /* <categ>-<OUI>-<subcateg> */
762 wps->dev.categ = atoi(wpa_s->conf->device_type);
763 pos = os_strchr(wpa_s->conf->device_type, '-');
764 if (pos == NULL) {
765 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
766 os_free(wps);
767 return -1;
768 }
769 pos++;
770 if (hexstr2bin(pos, oui, 4)) {
771 wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
772 os_free(wps);
773 return -1;
774 }
775 wps->dev.oui = WPA_GET_BE32(oui);
776 pos = os_strchr(pos, '-');
777 if (pos == NULL) {
778 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
779 os_free(wps);
780 return -1;
781 }
782 pos++;
783 wps->dev.sub_categ = atoi(pos);
784 }
785 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
786 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
787 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
788 if (is_nil_uuid(wpa_s->conf->uuid)) {
789 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
790 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
791 wps->uuid, WPS_UUID_LEN);
792 } else
793 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
794
795 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
796 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
797
798 os_memset(&rcfg, 0, sizeof(rcfg));
799 rcfg.new_psk_cb = wpas_wps_new_psk_cb;
800 rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
801 rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
802 rcfg.cb_ctx = wpa_s;
803
804 wps->registrar = wps_registrar_init(wps, &rcfg);
805 if (wps->registrar == NULL) {
806 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
807 os_free(wps);
808 return -1;
809 }
810
811 wpa_s->wps = wps;
812
813 return 0;
814 }
815
816
817 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
818 {
819 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
820
821 if (wpa_s->wps == NULL)
822 return;
823
824 wps_registrar_deinit(wpa_s->wps->registrar);
825 wpabuf_free(wpa_s->wps->dh_pubkey);
826 wpabuf_free(wpa_s->wps->dh_privkey);
827 wpabuf_free(wpa_s->wps->oob_conf.pubkey_hash);
828 wpabuf_free(wpa_s->wps->oob_conf.dev_password);
829 os_free(wpa_s->wps->network_key);
830 os_free(wpa_s->wps);
831 wpa_s->wps = NULL;
832
833 #ifdef CONFIG_WPS_ER
834 wps_er_deinit(wpa_s->wps_er);
835 wpa_s->wps_er = NULL;
836 #endif /* CONFIG_WPS_ER */
837 }
838
839
840 int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
841 struct wpa_ssid *ssid, struct wpa_scan_res *bss)
842 {
843 struct wpabuf *wps_ie;
844
845 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
846 return -1;
847
848 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
849 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
850 if (!wps_ie) {
851 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
852 return 0;
853 }
854
855 if (!wps_is_selected_pbc_registrar(wps_ie)) {
856 wpa_printf(MSG_DEBUG, " skip - WPS AP "
857 "without active PBC Registrar");
858 wpabuf_free(wps_ie);
859 return 0;
860 }
861
862 /* TODO: overlap detection */
863 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
864 "(Active PBC)");
865 wpabuf_free(wps_ie);
866 return 1;
867 }
868
869 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
870 if (!wps_ie) {
871 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
872 return 0;
873 }
874
875 /*
876 * Start with WPS APs that advertise active PIN Registrar and
877 * allow any WPS AP after third scan since some APs do not set
878 * Selected Registrar attribute properly when using external
879 * Registrar.
880 */
881 if (!wps_is_selected_pin_registrar(wps_ie)) {
882 if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
883 wpa_printf(MSG_DEBUG, " skip - WPS AP "
884 "without active PIN Registrar");
885 wpabuf_free(wps_ie);
886 return 0;
887 }
888 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
889 } else {
890 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
891 "(Active PIN)");
892 }
893 wpabuf_free(wps_ie);
894 return 1;
895 }
896
897 if (wps_ie) {
898 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
899 wpabuf_free(wps_ie);
900 return 1;
901 }
902
903 return -1;
904 }
905
906
907 int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
908 struct wpa_ssid *ssid,
909 struct wpa_scan_res *bss)
910 {
911 struct wpabuf *wps_ie = NULL;
912 int ret = 0;
913
914 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
915 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
916 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
917 /* allow wildcard SSID for WPS PBC */
918 ret = 1;
919 }
920 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
921 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
922 if (wps_ie &&
923 (wps_is_selected_pin_registrar(wps_ie) ||
924 wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
925 /* allow wildcard SSID for WPS PIN */
926 ret = 1;
927 }
928 }
929
930 if (!ret && ssid->bssid_set &&
931 os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
932 /* allow wildcard SSID due to hardcoded BSSID match */
933 ret = 1;
934 }
935
936 wpabuf_free(wps_ie);
937
938 return ret;
939 }
940
941
942 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
943 struct wpa_scan_res *selected,
944 struct wpa_ssid *ssid)
945 {
946 const u8 *sel_uuid, *uuid;
947 size_t i;
948 struct wpabuf *wps_ie;
949 int ret = 0;
950
951 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
952 return 0;
953
954 /* Make sure that only one AP is in active PBC mode */
955 wps_ie = wpa_scan_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
956 if (wps_ie)
957 sel_uuid = wps_get_uuid_e(wps_ie);
958 else
959 sel_uuid = NULL;
960
961 for (i = 0; i < wpa_s->scan_res->num; i++) {
962 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
963 struct wpabuf *ie;
964 if (bss == selected)
965 continue;
966 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
967 if (!ie)
968 continue;
969 if (!wps_is_selected_pbc_registrar(ie)) {
970 wpabuf_free(ie);
971 continue;
972 }
973 uuid = wps_get_uuid_e(ie);
974 if (sel_uuid == NULL || uuid == NULL ||
975 os_memcmp(sel_uuid, uuid, 16) != 0) {
976 ret = 1; /* PBC overlap */
977 wpabuf_free(ie);
978 break;
979 }
980
981 /* TODO: verify that this is reasonable dual-band situation */
982
983 wpabuf_free(ie);
984 }
985
986 wpabuf_free(wps_ie);
987
988 return ret;
989 }
990
991
992 void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
993 {
994 size_t i;
995
996 if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
997 return;
998
999 for (i = 0; i < wpa_s->scan_res->num; i++) {
1000 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
1001 struct wpabuf *ie;
1002 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1003 if (!ie)
1004 continue;
1005 if (wps_is_selected_pbc_registrar(ie))
1006 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
1007 else if (wps_is_selected_pin_registrar(ie))
1008 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
1009 else
1010 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
1011 wpabuf_free(ie);
1012 break;
1013 }
1014 }
1015
1016
1017 int wpas_wps_searching(struct wpa_supplicant *wpa_s)
1018 {
1019 struct wpa_ssid *ssid;
1020
1021 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1022 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
1023 return 1;
1024 }
1025
1026 return 0;
1027 }
1028
1029
1030 int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
1031 char *end)
1032 {
1033 struct wpabuf *wps_ie;
1034 int ret;
1035
1036 wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
1037 if (wps_ie == NULL)
1038 return 0;
1039
1040 ret = wps_attr_text(wps_ie, buf, end);
1041 wpabuf_free(wps_ie);
1042 return ret;
1043 }
1044
1045
1046 int wpas_wps_er_start(struct wpa_supplicant *wpa_s)
1047 {
1048 #ifdef CONFIG_WPS_ER
1049 if (wpa_s->wps_er) {
1050 /* TODO: re-send ctrl_iface events for current data? */
1051 return 0;
1052 }
1053 wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname);
1054 if (wpa_s->wps_er == NULL)
1055 return -1;
1056 return 0;
1057 #else /* CONFIG_WPS_ER */
1058 return 0;
1059 #endif /* CONFIG_WPS_ER */
1060 }
1061
1062
1063 int wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
1064 {
1065 #ifdef CONFIG_WPS_ER
1066 wps_er_deinit(wpa_s->wps_er);
1067 wpa_s->wps_er = NULL;
1068 #endif /* CONFIG_WPS_ER */
1069 return 0;
1070 }
1071
1072
1073 #ifdef CONFIG_WPS_ER
1074 int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const char *uuid,
1075 const char *pin)
1076 {
1077 u8 u[UUID_LEN];
1078 int any = 0;
1079
1080 if (os_strcmp(uuid, "any") == 0)
1081 any = 1;
1082 else if (uuid_str2bin(uuid, u))
1083 return -1;
1084 return wps_registrar_add_pin(wpa_s->wps->registrar, any ? NULL : u,
1085 (const u8 *) pin, os_strlen(pin), 300);
1086 }
1087 #endif /* CONFIG_WPS_ER */