dbus: Use strdup instead of malloc+strncpy
[hostap-gosc2009.git] / wpa_supplicant / wps_supplicant.c
blobf34b549efde0462b586df6fd03fe5284d228f975
1 /*
2 * wpa_supplicant / WPS integration
3 * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
12 * See README and COPYING for more details.
15 #include "includes.h"
17 #include "common.h"
18 #include "eloop.h"
19 #include "uuid.h"
20 #include "crypto/dh_group5.h"
21 #include "common/ieee802_11_defs.h"
22 #include "common/ieee802_11_common.h"
23 #include "common/wpa_common.h"
24 #include "common/wpa_ctrl.h"
25 #include "eap_common/eap_wsc_common.h"
26 #include "eap_peer/eap.h"
27 #include "rsn_supp/wpa.h"
28 #include "config.h"
29 #include "wpa_supplicant_i.h"
30 #include "driver_i.h"
31 #include "notify.h"
32 #include "blacklist.h"
33 #include "wps_supplicant.h"
36 #define WPS_PIN_SCAN_IGNORE_SEL_REG 3
38 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
39 static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
42 int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
44 if (!wpa_s->wps_success &&
45 wpa_s->current_ssid &&
46 eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
47 const u8 *bssid = wpa_s->bssid;
48 if (is_zero_ether_addr(bssid))
49 bssid = wpa_s->pending_bssid;
51 wpa_printf(MSG_DEBUG, "WPS: PIN registration with " MACSTR
52 " did not succeed - continue trying to find "
53 "suitable AP", MAC2STR(bssid));
54 wpa_blacklist_add(wpa_s, bssid);
56 wpa_supplicant_deauthenticate(wpa_s,
57 WLAN_REASON_DEAUTH_LEAVING);
58 wpa_s->reassociate = 1;
59 wpa_supplicant_req_scan(wpa_s,
60 wpa_s->blacklist_cleared ? 5 : 0, 0);
61 wpa_s->blacklist_cleared = 0;
62 return 1;
65 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
67 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
68 !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
69 wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
70 "try to associate with the received credential");
71 wpa_supplicant_deauthenticate(wpa_s,
72 WLAN_REASON_DEAUTH_LEAVING);
73 wpa_s->reassociate = 1;
74 wpa_supplicant_req_scan(wpa_s, 0, 0);
75 return 1;
78 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
79 wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
80 "for external credential processing");
81 wpas_clear_wps(wpa_s);
82 wpa_supplicant_deauthenticate(wpa_s,
83 WLAN_REASON_DEAUTH_LEAVING);
84 return 1;
87 return 0;
91 static void wpas_wps_security_workaround(struct wpa_supplicant *wpa_s,
92 struct wpa_ssid *ssid,
93 const struct wps_credential *cred)
95 struct wpa_driver_capa capa;
96 size_t i;
97 struct wpa_scan_res *bss;
98 const u8 *ie;
99 struct wpa_ie_data adv;
100 int wpa2 = 0, ccmp = 0;
103 * Many existing WPS APs do not know how to negotiate WPA2 or CCMP in
104 * case they are configured for mixed mode operation (WPA+WPA2 and
105 * TKIP+CCMP). Try to use scan results to figure out whether the AP
106 * actually supports stronger security and select that if the client
107 * has support for it, too.
110 if (wpa_drv_get_capa(wpa_s, &capa))
111 return; /* Unknown what driver supports */
113 if (wpa_supplicant_get_scan_results(wpa_s) || wpa_s->scan_res == NULL)
114 return; /* Could not get scan results for checking advertised
115 * parameters */
117 for (i = 0; i < wpa_s->scan_res->num; i++) {
118 bss = wpa_s->scan_res->res[i];
119 if (os_memcmp(bss->bssid, cred->mac_addr, ETH_ALEN) != 0)
120 continue;
121 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
122 if (ie == NULL)
123 continue;
124 if (ie[1] != ssid->ssid_len || ssid->ssid == NULL ||
125 os_memcmp(ie + 2, ssid->ssid, ssid->ssid_len) != 0)
126 continue;
128 wpa_printf(MSG_DEBUG, "WPS: AP found from scan results");
129 break;
132 if (i == wpa_s->scan_res->num) {
133 wpa_printf(MSG_DEBUG, "WPS: The AP was not found from scan "
134 "results - use credential as-is");
135 return;
138 ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
139 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0) {
140 wpa2 = 1;
141 if (adv.pairwise_cipher & WPA_CIPHER_CCMP)
142 ccmp = 1;
143 } else {
144 ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
145 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0 &&
146 adv.pairwise_cipher & WPA_CIPHER_CCMP)
147 ccmp = 1;
150 if (ie == NULL && (ssid->proto & WPA_PROTO_WPA) &&
151 (ssid->pairwise_cipher & WPA_CIPHER_TKIP)) {
153 * TODO: This could be the initial AP configuration and the
154 * Beacon contents could change shortly. Should request a new
155 * scan and delay addition of the network until the updated
156 * scan results are available.
158 wpa_printf(MSG_DEBUG, "WPS: The AP did not yet advertise WPA "
159 "support - use credential as-is");
160 return;
163 if (ccmp && !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
164 (ssid->pairwise_cipher & WPA_CIPHER_TKIP) &&
165 (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
166 wpa_printf(MSG_DEBUG, "WPS: Add CCMP into the credential "
167 "based on scan results");
168 if (wpa_s->conf->ap_scan == 1)
169 ssid->pairwise_cipher |= WPA_CIPHER_CCMP;
170 else
171 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
174 if (wpa2 && !(ssid->proto & WPA_PROTO_RSN) &&
175 (ssid->proto & WPA_PROTO_WPA) &&
176 (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP)) {
177 wpa_printf(MSG_DEBUG, "WPS: Add WPA2 into the credential "
178 "based on scan results");
179 if (wpa_s->conf->ap_scan == 1)
180 ssid->proto |= WPA_PROTO_RSN;
181 else
182 ssid->proto = WPA_PROTO_RSN;
187 static int wpa_supplicant_wps_cred(void *ctx,
188 const struct wps_credential *cred)
190 struct wpa_supplicant *wpa_s = ctx;
191 struct wpa_ssid *ssid = wpa_s->current_ssid;
192 u8 key_idx = 0;
193 u16 auth_type;
195 if ((wpa_s->conf->wps_cred_processing == 1 ||
196 wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
197 size_t blen = cred->cred_attr_len * 2 + 1;
198 char *buf = os_malloc(blen);
199 if (buf) {
200 wpa_snprintf_hex(buf, blen,
201 cred->cred_attr, cred->cred_attr_len);
202 wpa_msg(wpa_s, MSG_INFO, "%s%s",
203 WPS_EVENT_CRED_RECEIVED, buf);
204 os_free(buf);
207 wpas_notify_wps_credential(wpa_s, cred);
208 } else
209 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
211 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
212 cred->cred_attr, cred->cred_attr_len);
214 if (wpa_s->conf->wps_cred_processing == 1)
215 return 0;
217 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
218 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
219 cred->auth_type);
220 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
221 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
222 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
223 cred->key, cred->key_len);
224 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
225 MAC2STR(cred->mac_addr));
227 auth_type = cred->auth_type;
228 if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
229 wpa_printf(MSG_DEBUG, "WPS: Workaround - convert mixed-mode "
230 "auth_type into WPA2PSK");
231 auth_type = WPS_AUTH_WPA2PSK;
234 if (auth_type != WPS_AUTH_OPEN &&
235 auth_type != WPS_AUTH_SHARED &&
236 auth_type != WPS_AUTH_WPAPSK &&
237 auth_type != WPS_AUTH_WPA2PSK) {
238 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
239 "unsupported authentication type 0x%x",
240 auth_type);
241 return 0;
244 if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
245 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
246 "on the received credential");
247 os_free(ssid->eap.identity);
248 ssid->eap.identity = NULL;
249 ssid->eap.identity_len = 0;
250 os_free(ssid->eap.phase1);
251 ssid->eap.phase1 = NULL;
252 os_free(ssid->eap.eap_methods);
253 ssid->eap.eap_methods = NULL;
254 } else {
255 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
256 "received credential");
257 ssid = wpa_config_add_network(wpa_s->conf);
258 if (ssid == NULL)
259 return -1;
260 wpas_notify_network_added(wpa_s, ssid);
263 wpa_config_set_network_defaults(ssid);
265 os_free(ssid->ssid);
266 ssid->ssid = os_malloc(cred->ssid_len);
267 if (ssid->ssid) {
268 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
269 ssid->ssid_len = cred->ssid_len;
272 switch (cred->encr_type) {
273 case WPS_ENCR_NONE:
274 break;
275 case WPS_ENCR_WEP:
276 if (cred->key_len <= 0)
277 break;
278 if (cred->key_len != 5 && cred->key_len != 13 &&
279 cred->key_len != 10 && cred->key_len != 26) {
280 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key length "
281 "%lu", (unsigned long) cred->key_len);
282 return -1;
284 if (cred->key_idx > NUM_WEP_KEYS) {
285 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key index %d",
286 cred->key_idx);
287 return -1;
289 if (cred->key_idx)
290 key_idx = cred->key_idx - 1;
291 if (cred->key_len == 10 || cred->key_len == 26) {
292 if (hexstr2bin((char *) cred->key,
293 ssid->wep_key[key_idx],
294 cred->key_len / 2) < 0) {
295 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key "
296 "%d", key_idx);
297 return -1;
299 ssid->wep_key_len[key_idx] = cred->key_len / 2;
300 } else {
301 os_memcpy(ssid->wep_key[key_idx], cred->key,
302 cred->key_len);
303 ssid->wep_key_len[key_idx] = cred->key_len;
305 ssid->wep_tx_keyidx = key_idx;
306 break;
307 case WPS_ENCR_TKIP:
308 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
309 break;
310 case WPS_ENCR_AES:
311 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
312 break;
315 switch (auth_type) {
316 case WPS_AUTH_OPEN:
317 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
318 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
319 ssid->proto = 0;
320 break;
321 case WPS_AUTH_SHARED:
322 ssid->auth_alg = WPA_AUTH_ALG_SHARED;
323 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
324 ssid->proto = 0;
325 break;
326 case WPS_AUTH_WPAPSK:
327 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
328 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
329 ssid->proto = WPA_PROTO_WPA;
330 break;
331 case WPS_AUTH_WPA:
332 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
333 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
334 ssid->proto = WPA_PROTO_WPA;
335 break;
336 case WPS_AUTH_WPA2:
337 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
338 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
339 ssid->proto = WPA_PROTO_RSN;
340 break;
341 case WPS_AUTH_WPA2PSK:
342 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
343 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
344 ssid->proto = WPA_PROTO_RSN;
345 break;
348 if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
349 if (cred->key_len == 2 * PMK_LEN) {
350 if (hexstr2bin((const char *) cred->key, ssid->psk,
351 PMK_LEN)) {
352 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
353 "Key");
354 return -1;
356 ssid->psk_set = 1;
357 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
358 os_free(ssid->passphrase);
359 ssid->passphrase = os_malloc(cred->key_len + 1);
360 if (ssid->passphrase == NULL)
361 return -1;
362 os_memcpy(ssid->passphrase, cred->key, cred->key_len);
363 ssid->passphrase[cred->key_len] = '\0';
364 wpa_config_update_psk(ssid);
365 } else {
366 wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
367 "length %lu",
368 (unsigned long) cred->key_len);
369 return -1;
373 wpas_wps_security_workaround(wpa_s, ssid, cred);
375 #ifndef CONFIG_NO_CONFIG_WRITE
376 if (wpa_s->conf->update_config &&
377 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
378 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
379 return -1;
381 #endif /* CONFIG_NO_CONFIG_WRITE */
383 return 0;
387 static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
388 struct wps_event_m2d *m2d)
390 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
391 "dev_password_id=%d config_error=%d",
392 m2d->dev_password_id, m2d->config_error);
393 wpas_notify_wps_event_m2d(wpa_s, m2d);
397 static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
398 struct wps_event_fail *fail)
400 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL "msg=%d", fail->msg);
401 wpas_clear_wps(wpa_s);
402 wpas_notify_wps_event_fail(wpa_s, fail);
406 static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
408 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
409 wpa_s->wps_success = 1;
410 wpas_notify_wps_event_success(wpa_s);
414 static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
415 struct wps_event_er_ap *ap)
417 char uuid_str[100];
418 char dev_type[WPS_DEV_TYPE_BUFSIZE];
420 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
421 if (ap->pri_dev_type)
422 wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
423 sizeof(dev_type));
424 else
425 dev_type[0] = '\0';
427 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
428 " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
429 uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
430 ap->friendly_name ? ap->friendly_name : "",
431 ap->manufacturer ? ap->manufacturer : "",
432 ap->model_description ? ap->model_description : "",
433 ap->model_name ? ap->model_name : "",
434 ap->manufacturer_url ? ap->manufacturer_url : "",
435 ap->model_url ? ap->model_url : "");
439 static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
440 struct wps_event_er_ap *ap)
442 char uuid_str[100];
443 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
444 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
448 static void wpa_supplicant_wps_event_er_enrollee_add(
449 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
451 char uuid_str[100];
452 char dev_type[WPS_DEV_TYPE_BUFSIZE];
454 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
455 if (enrollee->pri_dev_type)
456 wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
457 sizeof(dev_type));
458 else
459 dev_type[0] = '\0';
461 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
462 " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
463 "|%s|%s|%s|%s|%s|",
464 uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
465 enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
466 enrollee->dev_name ? enrollee->dev_name : "",
467 enrollee->manufacturer ? enrollee->manufacturer : "",
468 enrollee->model_name ? enrollee->model_name : "",
469 enrollee->model_number ? enrollee->model_number : "",
470 enrollee->serial_number ? enrollee->serial_number : "");
474 static void wpa_supplicant_wps_event_er_enrollee_remove(
475 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
477 char uuid_str[100];
478 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
479 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
480 uuid_str, MAC2STR(enrollee->mac_addr));
484 static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
485 union wps_event_data *data)
487 struct wpa_supplicant *wpa_s = ctx;
488 switch (event) {
489 case WPS_EV_M2D:
490 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
491 break;
492 case WPS_EV_FAIL:
493 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
494 break;
495 case WPS_EV_SUCCESS:
496 wpa_supplicant_wps_event_success(wpa_s);
497 break;
498 case WPS_EV_PWD_AUTH_FAIL:
499 break;
500 case WPS_EV_PBC_OVERLAP:
501 break;
502 case WPS_EV_PBC_TIMEOUT:
503 break;
504 case WPS_EV_ER_AP_ADD:
505 wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
506 break;
507 case WPS_EV_ER_AP_REMOVE:
508 wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
509 break;
510 case WPS_EV_ER_ENROLLEE_ADD:
511 wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
512 &data->enrollee);
513 break;
514 case WPS_EV_ER_ENROLLEE_REMOVE:
515 wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
516 &data->enrollee);
517 break;
522 enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
524 if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
525 eap_is_wps_pin_enrollee(&ssid->eap))
526 return WPS_REQ_ENROLLEE;
527 else
528 return WPS_REQ_REGISTRAR;
532 static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
534 int id;
535 struct wpa_ssid *ssid, *remove_ssid = NULL;
537 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
539 /* Remove any existing WPS network from configuration */
540 ssid = wpa_s->conf->ssid;
541 while (ssid) {
542 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
543 if (ssid == wpa_s->current_ssid) {
544 wpa_s->current_ssid = NULL;
545 if (ssid != NULL)
546 wpas_notify_network_changed(wpa_s);
548 id = ssid->id;
549 remove_ssid = ssid;
550 } else
551 id = -1;
552 ssid = ssid->next;
553 if (id >= 0) {
554 wpas_notify_network_removed(wpa_s, remove_ssid);
555 wpa_config_remove_network(wpa_s->conf, id);
561 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
563 struct wpa_supplicant *wpa_s = eloop_ctx;
564 wpa_printf(MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
565 "out");
566 wpas_clear_wps(wpa_s);
570 static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
571 int registrar, const u8 *bssid)
573 struct wpa_ssid *ssid;
575 ssid = wpa_config_add_network(wpa_s->conf);
576 if (ssid == NULL)
577 return NULL;
578 wpas_notify_network_added(wpa_s, ssid);
579 wpa_config_set_network_defaults(ssid);
580 if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
581 wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
582 wpa_config_set(ssid, "identity", registrar ?
583 "\"" WSC_ID_REGISTRAR "\"" :
584 "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
585 wpas_notify_network_removed(wpa_s, ssid);
586 wpa_config_remove_network(wpa_s->conf, ssid->id);
587 return NULL;
590 if (bssid) {
591 size_t i;
592 int count = 0;
594 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
595 ssid->bssid_set = 1;
597 /* Try to get SSID from scan results */
598 if (wpa_s->scan_res == NULL &&
599 wpa_supplicant_get_scan_results(wpa_s) < 0)
600 return ssid; /* Could not find any scan results */
602 for (i = 0; i < wpa_s->scan_res->num; i++) {
603 const u8 *ie;
604 struct wpa_scan_res *res;
606 res = wpa_s->scan_res->res[i];
607 if (os_memcmp(bssid, res->bssid, ETH_ALEN) != 0)
608 continue;
610 ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
611 if (ie == NULL)
612 break;
613 os_free(ssid->ssid);
614 ssid->ssid = os_malloc(ie[1]);
615 if (ssid->ssid == NULL)
616 break;
617 os_memcpy(ssid->ssid, ie + 2, ie[1]);
618 ssid->ssid_len = ie[1];
619 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
620 "scan results",
621 ssid->ssid, ssid->ssid_len);
622 count++;
625 if (count > 1) {
626 wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
627 "for the AP; use wildcard");
628 os_free(ssid->ssid);
629 ssid->ssid = NULL;
630 ssid->ssid_len = 0;
634 return ssid;
638 static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
639 struct wpa_ssid *selected)
641 struct wpa_ssid *ssid;
643 /* Mark all other networks disabled and trigger reassociation */
644 ssid = wpa_s->conf->ssid;
645 while (ssid) {
646 int was_disabled = ssid->disabled;
647 ssid->disabled = ssid != selected;
648 if (was_disabled != ssid->disabled)
649 wpas_notify_network_enabled_changed(wpa_s, ssid);
650 ssid = ssid->next;
652 wpa_s->disconnected = 0;
653 wpa_s->reassociate = 1;
654 wpa_s->scan_runs = 0;
655 wpa_s->wps_success = 0;
656 wpa_s->blacklist_cleared = 0;
657 wpa_supplicant_req_scan(wpa_s, 0, 0);
661 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
663 struct wpa_ssid *ssid;
664 wpas_clear_wps(wpa_s);
665 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
666 if (ssid == NULL)
667 return -1;
668 wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
669 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
670 wpa_s, NULL);
671 wpas_wps_reassoc(wpa_s, ssid);
672 return 0;
676 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
677 const char *pin)
679 struct wpa_ssid *ssid;
680 char val[128];
681 unsigned int rpin = 0;
683 wpas_clear_wps(wpa_s);
684 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
685 if (ssid == NULL)
686 return -1;
687 if (pin)
688 os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
689 else {
690 rpin = wps_generate_pin();
691 os_snprintf(val, sizeof(val), "\"pin=%08d\"", rpin);
693 wpa_config_set(ssid, "phase1", val, 0);
694 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
695 wpa_s, NULL);
696 wpas_wps_reassoc(wpa_s, ssid);
697 return rpin;
701 #ifdef CONFIG_WPS_OOB
702 int wpas_wps_start_oob(struct wpa_supplicant *wpa_s, char *device_type,
703 char *path, char *method, char *name)
705 struct wps_context *wps = wpa_s->wps;
706 struct oob_device_data *oob_dev;
708 oob_dev = wps_get_oob_device(device_type);
709 if (oob_dev == NULL)
710 return -1;
711 oob_dev->device_path = path;
712 oob_dev->device_name = name;
713 wps->oob_conf.oob_method = wps_get_oob_method(method);
715 if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E) {
717 * Use pre-configured DH keys in order to be able to write the
718 * key hash into the OOB file.
720 wpabuf_free(wps->dh_pubkey);
721 wpabuf_free(wps->dh_privkey);
722 wps->dh_privkey = NULL;
723 wps->dh_pubkey = NULL;
724 dh5_free(wps->dh_ctx);
725 wps->dh_ctx = dh5_init(&wps->dh_privkey, &wps->dh_pubkey);
726 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
727 if (wps->dh_ctx == NULL || wps->dh_pubkey == NULL) {
728 wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
729 "Diffie-Hellman handshake");
730 return -1;
734 if (wps->oob_conf.oob_method == OOB_METHOD_CRED)
735 wpas_clear_wps(wpa_s);
737 if (wps_process_oob(wps, oob_dev, 0) < 0)
738 return -1;
740 if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
741 wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
742 wpas_wps_start_pin(wpa_s, NULL,
743 wpabuf_head(wps->oob_conf.dev_password)) < 0)
744 return -1;
746 return 0;
748 #endif /* CONFIG_WPS_OOB */
751 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
752 const char *pin, struct wps_new_ap_settings *settings)
754 struct wpa_ssid *ssid;
755 char val[200];
756 char *pos, *end;
757 int res;
759 if (!pin)
760 return -1;
761 wpas_clear_wps(wpa_s);
762 ssid = wpas_wps_add_network(wpa_s, 1, bssid);
763 if (ssid == NULL)
764 return -1;
765 pos = val;
766 end = pos + sizeof(val);
767 res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
768 if (res < 0 || res >= end - pos)
769 return -1;
770 pos += res;
771 if (settings) {
772 res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
773 "new_encr=%s new_key=%s",
774 settings->ssid_hex, settings->auth,
775 settings->encr, settings->key_hex);
776 if (res < 0 || res >= end - pos)
777 return -1;
778 pos += res;
780 res = os_snprintf(pos, end - pos, "\"");
781 if (res < 0 || res >= end - pos)
782 return -1;
783 wpa_config_set(ssid, "phase1", val, 0);
784 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
785 wpa_s, NULL);
786 wpas_wps_reassoc(wpa_s, ssid);
787 return 0;
791 static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
792 size_t psk_len)
794 wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
795 "STA " MACSTR, MAC2STR(mac_addr));
796 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
798 /* TODO */
800 return 0;
804 static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
805 const struct wps_device_data *dev)
807 char uuid[40], txt[400];
808 int len;
809 char devtype[WPS_DEV_TYPE_BUFSIZE];
810 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
811 return;
812 wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
813 len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
814 " [%s|%s|%s|%s|%s|%s]",
815 uuid, MAC2STR(dev->mac_addr), dev->device_name,
816 dev->manufacturer, dev->model_name,
817 dev->model_number, dev->serial_number,
818 wps_dev_type_bin2str(dev->pri_dev_type, devtype,
819 sizeof(devtype)));
820 if (len > 0 && len < (int) sizeof(txt))
821 wpa_printf(MSG_INFO, "%s", txt);
825 static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
826 u16 sel_reg_config_methods)
828 #ifdef CONFIG_WPS_ER
829 struct wpa_supplicant *wpa_s = ctx;
831 if (wpa_s->wps_er == NULL)
832 return;
833 wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
834 sel_reg_config_methods);
835 #endif /* CONFIG_WPS_ER */
839 int wpas_wps_init(struct wpa_supplicant *wpa_s)
841 struct wps_context *wps;
842 struct wps_registrar_config rcfg;
844 wps = os_zalloc(sizeof(*wps));
845 if (wps == NULL)
846 return -1;
848 wps->cred_cb = wpa_supplicant_wps_cred;
849 wps->event_cb = wpa_supplicant_wps_event;
850 wps->cb_ctx = wpa_s;
852 wps->dev.device_name = wpa_s->conf->device_name;
853 wps->dev.manufacturer = wpa_s->conf->manufacturer;
854 wps->dev.model_name = wpa_s->conf->model_name;
855 wps->dev.model_number = wpa_s->conf->model_number;
856 wps->dev.serial_number = wpa_s->conf->serial_number;
857 wps->config_methods =
858 wps_config_methods_str2bin(wpa_s->conf->config_methods);
859 if (wpa_s->conf->device_type &&
860 wps_dev_type_str2bin(wpa_s->conf->device_type,
861 wps->dev.pri_dev_type) < 0) {
862 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
863 os_free(wps);
864 return -1;
866 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
867 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
868 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
869 if (is_nil_uuid(wpa_s->conf->uuid)) {
870 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
871 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
872 wps->uuid, WPS_UUID_LEN);
873 } else
874 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
876 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
877 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
879 os_memset(&rcfg, 0, sizeof(rcfg));
880 rcfg.new_psk_cb = wpas_wps_new_psk_cb;
881 rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
882 rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
883 rcfg.cb_ctx = wpa_s;
885 wps->registrar = wps_registrar_init(wps, &rcfg);
886 if (wps->registrar == NULL) {
887 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
888 os_free(wps);
889 return -1;
892 wpa_s->wps = wps;
894 return 0;
898 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
900 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
902 if (wpa_s->wps == NULL)
903 return;
905 #ifdef CONFIG_WPS_ER
906 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
907 wpa_s->wps_er = NULL;
908 #endif /* CONFIG_WPS_ER */
910 wps_registrar_deinit(wpa_s->wps->registrar);
911 wpabuf_free(wpa_s->wps->dh_pubkey);
912 wpabuf_free(wpa_s->wps->dh_privkey);
913 wpabuf_free(wpa_s->wps->oob_conf.pubkey_hash);
914 wpabuf_free(wpa_s->wps->oob_conf.dev_password);
915 os_free(wpa_s->wps->network_key);
916 os_free(wpa_s->wps);
917 wpa_s->wps = NULL;
921 int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
922 struct wpa_ssid *ssid, struct wpa_scan_res *bss)
924 struct wpabuf *wps_ie;
926 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
927 return -1;
929 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
930 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
931 if (!wps_ie) {
932 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
933 return 0;
936 if (!wps_is_selected_pbc_registrar(wps_ie)) {
937 wpa_printf(MSG_DEBUG, " skip - WPS AP "
938 "without active PBC Registrar");
939 wpabuf_free(wps_ie);
940 return 0;
943 /* TODO: overlap detection */
944 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
945 "(Active PBC)");
946 wpabuf_free(wps_ie);
947 return 1;
950 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
951 if (!wps_ie) {
952 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
953 return 0;
957 * Start with WPS APs that advertise active PIN Registrar and
958 * allow any WPS AP after third scan since some APs do not set
959 * Selected Registrar attribute properly when using external
960 * Registrar.
962 if (!wps_is_selected_pin_registrar(wps_ie)) {
963 if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
964 wpa_printf(MSG_DEBUG, " skip - WPS AP "
965 "without active PIN Registrar");
966 wpabuf_free(wps_ie);
967 return 0;
969 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
970 } else {
971 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
972 "(Active PIN)");
974 wpabuf_free(wps_ie);
975 return 1;
978 if (wps_ie) {
979 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
980 wpabuf_free(wps_ie);
981 return 1;
984 return -1;
988 int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
989 struct wpa_ssid *ssid,
990 struct wpa_scan_res *bss)
992 struct wpabuf *wps_ie = NULL;
993 int ret = 0;
995 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
996 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
997 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
998 /* allow wildcard SSID for WPS PBC */
999 ret = 1;
1001 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1002 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1003 if (wps_ie &&
1004 (wps_is_selected_pin_registrar(wps_ie) ||
1005 wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
1006 /* allow wildcard SSID for WPS PIN */
1007 ret = 1;
1011 if (!ret && ssid->bssid_set &&
1012 os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
1013 /* allow wildcard SSID due to hardcoded BSSID match */
1014 ret = 1;
1017 wpabuf_free(wps_ie);
1019 return ret;
1023 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
1024 struct wpa_scan_res *selected,
1025 struct wpa_ssid *ssid)
1027 const u8 *sel_uuid, *uuid;
1028 size_t i;
1029 struct wpabuf *wps_ie;
1030 int ret = 0;
1032 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
1033 return 0;
1035 /* Make sure that only one AP is in active PBC mode */
1036 wps_ie = wpa_scan_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
1037 if (wps_ie)
1038 sel_uuid = wps_get_uuid_e(wps_ie);
1039 else
1040 sel_uuid = NULL;
1042 for (i = 0; i < wpa_s->scan_res->num; i++) {
1043 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
1044 struct wpabuf *ie;
1045 if (bss == selected)
1046 continue;
1047 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1048 if (!ie)
1049 continue;
1050 if (!wps_is_selected_pbc_registrar(ie)) {
1051 wpabuf_free(ie);
1052 continue;
1054 uuid = wps_get_uuid_e(ie);
1055 if (sel_uuid == NULL || uuid == NULL ||
1056 os_memcmp(sel_uuid, uuid, 16) != 0) {
1057 ret = 1; /* PBC overlap */
1058 wpabuf_free(ie);
1059 break;
1062 /* TODO: verify that this is reasonable dual-band situation */
1064 wpabuf_free(ie);
1067 wpabuf_free(wps_ie);
1069 return ret;
1073 void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
1075 size_t i;
1077 if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
1078 return;
1080 for (i = 0; i < wpa_s->scan_res->num; i++) {
1081 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
1082 struct wpabuf *ie;
1083 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1084 if (!ie)
1085 continue;
1086 if (wps_is_selected_pbc_registrar(ie))
1087 wpa_msg_ctrl(wpa_s, MSG_INFO,
1088 WPS_EVENT_AP_AVAILABLE_PBC);
1089 else if (wps_is_selected_pin_registrar(ie))
1090 wpa_msg_ctrl(wpa_s, MSG_INFO,
1091 WPS_EVENT_AP_AVAILABLE_PIN);
1092 else
1093 wpa_msg_ctrl(wpa_s, MSG_INFO,
1094 WPS_EVENT_AP_AVAILABLE);
1095 wpabuf_free(ie);
1096 break;
1101 int wpas_wps_searching(struct wpa_supplicant *wpa_s)
1103 struct wpa_ssid *ssid;
1105 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1106 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
1107 return 1;
1110 return 0;
1114 int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
1115 char *end)
1117 struct wpabuf *wps_ie;
1118 int ret;
1120 wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
1121 if (wps_ie == NULL)
1122 return 0;
1124 ret = wps_attr_text(wps_ie, buf, end);
1125 wpabuf_free(wps_ie);
1126 return ret;
1130 int wpas_wps_er_start(struct wpa_supplicant *wpa_s)
1132 #ifdef CONFIG_WPS_ER
1133 if (wpa_s->wps_er) {
1134 wps_er_refresh(wpa_s->wps_er);
1135 return 0;
1137 wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname);
1138 if (wpa_s->wps_er == NULL)
1139 return -1;
1140 return 0;
1141 #else /* CONFIG_WPS_ER */
1142 return 0;
1143 #endif /* CONFIG_WPS_ER */
1147 int wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
1149 #ifdef CONFIG_WPS_ER
1150 wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1151 wpa_s->wps_er = NULL;
1152 #endif /* CONFIG_WPS_ER */
1153 return 0;
1157 #ifdef CONFIG_WPS_ER
1158 int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const char *uuid,
1159 const char *pin)
1161 u8 u[UUID_LEN];
1162 int any = 0;
1164 if (os_strcmp(uuid, "any") == 0)
1165 any = 1;
1166 else if (uuid_str2bin(uuid, u))
1167 return -1;
1168 return wps_registrar_add_pin(wpa_s->wps->registrar, any ? NULL : u,
1169 (const u8 *) pin, os_strlen(pin), 300);
1173 int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
1175 u8 u[UUID_LEN];
1177 if (uuid_str2bin(uuid, u))
1178 return -1;
1179 return wps_er_pbc(wpa_s->wps_er, u);
1183 int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
1184 const char *pin)
1186 u8 u[UUID_LEN];
1188 if (uuid_str2bin(uuid, u))
1189 return -1;
1190 return wps_er_learn(wpa_s->wps_er, u, (const u8 *) pin,
1191 os_strlen(pin));
1195 static void wpas_wps_terminate_cb(void *ctx)
1197 wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
1198 eloop_terminate();
1200 #endif /* CONFIG_WPS_ER */
1203 int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
1205 #ifdef CONFIG_WPS_ER
1206 if (wpa_s->wps_er) {
1207 wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
1208 wpa_s->wps_er = NULL;
1209 return 1;
1211 #endif /* CONFIG_WPS_ER */
1212 return 0;