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
12 * See README and COPYING for more details.
18 #include "ieee802_11_defs.h"
19 #include "ieee802_11_common.h"
20 #include "wpa_common.h"
22 #include "eap_peer/eap.h"
23 #include "wpa_supplicant_i.h"
29 #include "eap_common/eap_wsc_common.h"
30 #include "blacklist.h"
32 #include "wps_supplicant.h"
33 #include "dh_group5.h"
35 #define WPS_PIN_SCAN_IGNORE_SEL_REG 3
37 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
38 static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
41 int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
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;
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);
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;
64 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
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);
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);
90 static void wpas_wps_security_workaround(struct wpa_supplicant *wpa_s,
91 struct wpa_ssid *ssid,
92 const struct wps_credential *cred)
94 struct wpa_driver_capa capa;
96 struct wpa_scan_res *bss;
98 struct wpa_ie_data adv;
99 int wpa2 = 0, ccmp = 0;
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.
109 if (wpa_drv_get_capa(wpa_s, &capa))
110 return; /* Unknown what driver supports */
112 if (wpa_supplicant_get_scan_results(wpa_s) || wpa_s->scan_res == NULL)
113 return; /* Could not get scan results for checking advertised
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)
120 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
123 if (ie[1] != ssid->ssid_len || ssid->ssid == NULL ||
124 os_memcmp(ie + 2, ssid->ssid, ssid->ssid_len) != 0)
127 wpa_printf(MSG_DEBUG, "WPS: AP found from scan results");
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");
137 ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
138 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0) {
140 if (adv.pairwise_cipher & WPA_CIPHER_CCMP)
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)
149 if (ie == NULL && (ssid->proto & WPA_PROTO_WPA) &&
150 (ssid->pairwise_cipher & WPA_CIPHER_TKIP)) {
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.
157 wpa_printf(MSG_DEBUG, "WPS: The AP did not yet advertise WPA "
158 "support - use credential as-is");
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;
170 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
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;
181 ssid->proto = WPA_PROTO_RSN;
186 static int wpa_supplicant_wps_cred(void *ctx,
187 const struct wps_credential *cred)
189 struct wpa_supplicant *wpa_s = ctx;
190 struct wpa_ssid *ssid = wpa_s->current_ssid;
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);
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);
206 wpas_notify_wps_credential(wpa_s, cred);
208 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
210 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
211 cred->cred_attr, cred->cred_attr_len);
213 if (wpa_s->conf->wps_cred_processing == 1)
216 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
217 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
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));
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;
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",
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;
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);
259 wpas_notify_network_added(wpa_s, ssid);
262 wpa_config_set_network_defaults(ssid);
265 ssid->ssid = os_malloc(cred->ssid_len);
267 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
268 ssid->ssid_len = cred->ssid_len;
271 switch (cred->encr_type) {
275 if (cred->key_len <= 0)
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);
283 if (cred->key_idx > NUM_WEP_KEYS) {
284 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key index %d",
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 "
298 ssid->wep_key_len[key_idx] = cred->key_len / 2;
300 os_memcpy(ssid->wep_key[key_idx], cred->key,
302 ssid->wep_key_len[key_idx] = cred->key_len;
304 ssid->wep_tx_keyidx = key_idx;
307 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
310 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
316 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
317 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
320 case WPS_AUTH_SHARED:
321 ssid->auth_alg = WPA_AUTH_ALG_SHARED;
322 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
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;
331 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
332 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
333 ssid->proto = WPA_PROTO_WPA;
336 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
337 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
338 ssid->proto = WPA_PROTO_RSN;
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;
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,
351 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
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)
361 os_memcpy(ssid->passphrase, cred->key, cred->key_len);
362 ssid->passphrase[cred->key_len] = '\0';
363 wpa_config_update_psk(ssid);
365 wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
367 (unsigned long) cred->key_len);
372 wpas_wps_security_workaround(wpa_s, ssid, cred);
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");
380 #endif /* CONFIG_NO_CONFIG_WRITE */
386 static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
387 struct wps_event_m2d *m2d)
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);
396 static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
397 struct wps_event_fail *fail)
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);
405 static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
407 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
408 wpa_s->wps_success = 1;
409 wpas_notify_wps_event_success(wpa_s);
413 static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
414 struct wps_event_er_ap *ap)
419 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
420 if (ap->pri_dev_type)
421 os_snprintf(dev_type, sizeof(dev_type), "%u-%08X-%u",
422 WPA_GET_BE16(ap->pri_dev_type),
423 WPA_GET_BE32(ap->pri_dev_type + 2),
424 WPA_GET_BE16(ap->pri_dev_type + 6));
428 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
429 " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
430 uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
431 ap->friendly_name ? ap->friendly_name : "",
432 ap->manufacturer ? ap->manufacturer : "",
433 ap->model_description ? ap->model_description : "",
434 ap->model_name ? ap->model_name : "",
435 ap->manufacturer_url ? ap->manufacturer_url : "",
436 ap->model_url ? ap->model_url : "");
440 static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
441 struct wps_event_er_ap *ap)
444 uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
445 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
449 static void wpa_supplicant_wps_event_er_enrollee_add(
450 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
455 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
456 if (enrollee->pri_dev_type)
457 os_snprintf(dev_type, sizeof(dev_type), "%u-%08X-%u",
458 WPA_GET_BE16(enrollee->pri_dev_type),
459 WPA_GET_BE32(enrollee->pri_dev_type + 2),
460 WPA_GET_BE16(enrollee->pri_dev_type + 6));
464 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
465 " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
467 uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
468 enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
469 enrollee->dev_name ? enrollee->dev_name : "",
470 enrollee->manufacturer ? enrollee->manufacturer : "",
471 enrollee->model_name ? enrollee->model_name : "",
472 enrollee->model_number ? enrollee->model_number : "",
473 enrollee->serial_number ? enrollee->serial_number : "");
477 static void wpa_supplicant_wps_event_er_enrollee_remove(
478 struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
481 uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
482 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
483 uuid_str, MAC2STR(enrollee->mac_addr));
487 static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
488 union wps_event_data *data)
490 struct wpa_supplicant *wpa_s = ctx;
493 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
496 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
499 wpa_supplicant_wps_event_success(wpa_s);
501 case WPS_EV_PWD_AUTH_FAIL:
503 case WPS_EV_PBC_OVERLAP:
505 case WPS_EV_PBC_TIMEOUT:
507 case WPS_EV_ER_AP_ADD:
508 wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
510 case WPS_EV_ER_AP_REMOVE:
511 wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
513 case WPS_EV_ER_ENROLLEE_ADD:
514 wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
517 case WPS_EV_ER_ENROLLEE_REMOVE:
518 wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
525 enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
527 if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
528 eap_is_wps_pin_enrollee(&ssid->eap))
529 return WPS_REQ_ENROLLEE;
531 return WPS_REQ_REGISTRAR;
535 static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
538 struct wpa_ssid *ssid, *remove_ssid = NULL;
540 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
542 /* Remove any existing WPS network from configuration */
543 ssid = wpa_s->conf->ssid;
545 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
546 if (ssid == wpa_s->current_ssid) {
547 wpa_s->current_ssid = NULL;
549 wpas_notify_network_changed(wpa_s);
557 wpas_notify_network_removed(wpa_s, remove_ssid);
558 wpa_config_remove_network(wpa_s->conf, id);
564 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
566 struct wpa_supplicant *wpa_s = eloop_ctx;
567 wpa_printf(MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
569 wpas_clear_wps(wpa_s);
573 static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
574 int registrar, const u8 *bssid)
576 struct wpa_ssid *ssid;
578 ssid = wpa_config_add_network(wpa_s->conf);
581 wpas_notify_network_added(wpa_s, ssid);
582 wpa_config_set_network_defaults(ssid);
583 if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
584 wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
585 wpa_config_set(ssid, "identity", registrar ?
586 "\"" WSC_ID_REGISTRAR "\"" :
587 "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
588 wpas_notify_network_removed(wpa_s, ssid);
589 wpa_config_remove_network(wpa_s->conf, ssid->id);
595 struct wpa_scan_res *res;
597 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
600 /* Try to get SSID from scan results */
601 if (wpa_s->scan_res == NULL &&
602 wpa_supplicant_get_scan_results(wpa_s) < 0)
603 return ssid; /* Could not find any scan results */
605 for (i = 0; i < wpa_s->scan_res->num; i++) {
608 res = wpa_s->scan_res->res[i];
609 if (os_memcmp(bssid, res->bssid, ETH_ALEN) != 0)
612 ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
616 ssid->ssid = os_malloc(ie[1]);
617 if (ssid->ssid == NULL)
619 os_memcpy(ssid->ssid, ie + 2, ie[1]);
620 ssid->ssid_len = ie[1];
629 static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
630 struct wpa_ssid *selected)
632 struct wpa_ssid *ssid;
634 /* Mark all other networks disabled and trigger reassociation */
635 ssid = wpa_s->conf->ssid;
637 int was_disabled = ssid->disabled;
638 ssid->disabled = ssid != selected;
639 if (was_disabled != ssid->disabled)
640 wpas_notify_network_enabled_changed(wpa_s, ssid);
643 wpa_s->disconnected = 0;
644 wpa_s->reassociate = 1;
645 wpa_s->scan_runs = 0;
646 wpa_s->wps_success = 0;
647 wpa_s->blacklist_cleared = 0;
648 wpa_supplicant_req_scan(wpa_s, 0, 0);
652 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
654 struct wpa_ssid *ssid;
655 wpas_clear_wps(wpa_s);
656 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
659 wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
660 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
662 wpas_wps_reassoc(wpa_s, ssid);
667 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
670 struct wpa_ssid *ssid;
672 unsigned int rpin = 0;
674 wpas_clear_wps(wpa_s);
675 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
679 os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
681 rpin = wps_generate_pin();
682 os_snprintf(val, sizeof(val), "\"pin=%08d\"", rpin);
684 wpa_config_set(ssid, "phase1", val, 0);
685 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
687 wpas_wps_reassoc(wpa_s, ssid);
692 #ifdef CONFIG_WPS_OOB
693 int wpas_wps_start_oob(struct wpa_supplicant *wpa_s, char *device_type,
694 char *path, char *method, char *name)
696 struct wps_context *wps = wpa_s->wps;
697 struct oob_device_data *oob_dev;
699 oob_dev = wps_get_oob_device(device_type);
702 oob_dev->device_path = path;
703 oob_dev->device_name = name;
704 wps->oob_conf.oob_method = wps_get_oob_method(method);
706 if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E) {
708 * Use pre-configured DH keys in order to be able to write the
709 * key hash into the OOB file.
711 wpabuf_free(wps->dh_pubkey);
712 wpabuf_free(wps->dh_privkey);
713 wps->dh_privkey = NULL;
714 wps->dh_pubkey = NULL;
715 dh5_free(wps->dh_ctx);
716 wps->dh_ctx = dh5_init(&wps->dh_privkey, &wps->dh_pubkey);
717 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
718 if (wps->dh_ctx == NULL || wps->dh_pubkey == NULL) {
719 wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
720 "Diffie-Hellman handshake");
725 if (wps->oob_conf.oob_method == OOB_METHOD_CRED)
726 wpas_clear_wps(wpa_s);
728 if (wps_process_oob(wps, oob_dev, 0) < 0)
731 if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
732 wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
733 wpas_wps_start_pin(wpa_s, NULL,
734 wpabuf_head(wps->oob_conf.dev_password)) < 0)
739 #endif /* CONFIG_WPS_OOB */
742 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
743 const char *pin, struct wps_new_ap_settings *settings)
745 struct wpa_ssid *ssid;
752 wpas_clear_wps(wpa_s);
753 ssid = wpas_wps_add_network(wpa_s, 1, bssid);
757 end = pos + sizeof(val);
758 res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
759 if (res < 0 || res >= end - pos)
763 res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
764 "new_encr=%s new_key=%s",
765 settings->ssid_hex, settings->auth,
766 settings->encr, settings->key_hex);
767 if (res < 0 || res >= end - pos)
771 res = os_snprintf(pos, end - pos, "\"");
772 if (res < 0 || res >= end - pos)
774 wpa_config_set(ssid, "phase1", val, 0);
775 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
777 wpas_wps_reassoc(wpa_s, ssid);
782 static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
785 wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
786 "STA " MACSTR, MAC2STR(mac_addr));
787 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
795 static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
796 const struct wps_device_data *dev)
798 char uuid[40], txt[400];
800 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
802 wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
803 len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
804 " [%s|%s|%s|%s|%s|%d-%08X-%d]",
805 uuid, MAC2STR(dev->mac_addr), dev->device_name,
806 dev->manufacturer, dev->model_name,
807 dev->model_number, dev->serial_number,
808 dev->categ, dev->oui, dev->sub_categ);
809 if (len > 0 && len < (int) sizeof(txt))
810 wpa_printf(MSG_INFO, "%s", txt);
814 static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
815 u16 sel_reg_config_methods)
818 struct wpa_supplicant *wpa_s = ctx;
820 if (wpa_s->wps_er == NULL)
822 wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
823 sel_reg_config_methods);
824 #endif /* CONFIG_WPS_ER */
828 int wpas_wps_init(struct wpa_supplicant *wpa_s)
830 struct wps_context *wps;
831 struct wps_registrar_config rcfg;
833 wps = os_zalloc(sizeof(*wps));
837 wps->cred_cb = wpa_supplicant_wps_cred;
838 wps->event_cb = wpa_supplicant_wps_event;
841 wps->dev.device_name = wpa_s->conf->device_name;
842 wps->dev.manufacturer = wpa_s->conf->manufacturer;
843 wps->dev.model_name = wpa_s->conf->model_name;
844 wps->dev.model_number = wpa_s->conf->model_number;
845 wps->dev.serial_number = wpa_s->conf->serial_number;
846 if (wpa_s->conf->device_type) {
849 /* <categ>-<OUI>-<subcateg> */
850 wps->dev.categ = atoi(wpa_s->conf->device_type);
851 pos = os_strchr(wpa_s->conf->device_type, '-');
853 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
858 if (hexstr2bin(pos, oui, 4)) {
859 wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
863 wps->dev.oui = WPA_GET_BE32(oui);
864 pos = os_strchr(pos, '-');
866 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
871 wps->dev.sub_categ = atoi(pos);
873 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
874 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
875 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
876 if (is_nil_uuid(wpa_s->conf->uuid)) {
877 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
878 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
879 wps->uuid, WPS_UUID_LEN);
881 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
883 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
884 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
886 os_memset(&rcfg, 0, sizeof(rcfg));
887 rcfg.new_psk_cb = wpas_wps_new_psk_cb;
888 rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
889 rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
892 wps->registrar = wps_registrar_init(wps, &rcfg);
893 if (wps->registrar == NULL) {
894 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
905 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
907 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
909 if (wpa_s->wps == NULL)
913 wps_er_deinit(wpa_s->wps_er);
914 wpa_s->wps_er = NULL;
915 #endif /* CONFIG_WPS_ER */
917 wps_registrar_deinit(wpa_s->wps->registrar);
918 wpabuf_free(wpa_s->wps->dh_pubkey);
919 wpabuf_free(wpa_s->wps->dh_privkey);
920 wpabuf_free(wpa_s->wps->oob_conf.pubkey_hash);
921 wpabuf_free(wpa_s->wps->oob_conf.dev_password);
922 os_free(wpa_s->wps->network_key);
928 int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
929 struct wpa_ssid *ssid, struct wpa_scan_res *bss)
931 struct wpabuf *wps_ie;
933 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
936 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
937 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
939 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
943 if (!wps_is_selected_pbc_registrar(wps_ie)) {
944 wpa_printf(MSG_DEBUG, " skip - WPS AP "
945 "without active PBC Registrar");
950 /* TODO: overlap detection */
951 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
957 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
959 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
964 * Start with WPS APs that advertise active PIN Registrar and
965 * allow any WPS AP after third scan since some APs do not set
966 * Selected Registrar attribute properly when using external
969 if (!wps_is_selected_pin_registrar(wps_ie)) {
970 if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
971 wpa_printf(MSG_DEBUG, " skip - WPS AP "
972 "without active PIN Registrar");
976 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
978 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
986 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
995 int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
996 struct wpa_ssid *ssid,
997 struct wpa_scan_res *bss)
999 struct wpabuf *wps_ie = NULL;
1002 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1003 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1004 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
1005 /* allow wildcard SSID for WPS PBC */
1008 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1009 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1011 (wps_is_selected_pin_registrar(wps_ie) ||
1012 wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
1013 /* allow wildcard SSID for WPS PIN */
1018 if (!ret && ssid->bssid_set &&
1019 os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
1020 /* allow wildcard SSID due to hardcoded BSSID match */
1024 wpabuf_free(wps_ie);
1030 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
1031 struct wpa_scan_res *selected,
1032 struct wpa_ssid *ssid)
1034 const u8 *sel_uuid, *uuid;
1036 struct wpabuf *wps_ie;
1039 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
1042 /* Make sure that only one AP is in active PBC mode */
1043 wps_ie = wpa_scan_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
1045 sel_uuid = wps_get_uuid_e(wps_ie);
1049 for (i = 0; i < wpa_s->scan_res->num; i++) {
1050 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
1052 if (bss == selected)
1054 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1057 if (!wps_is_selected_pbc_registrar(ie)) {
1061 uuid = wps_get_uuid_e(ie);
1062 if (sel_uuid == NULL || uuid == NULL ||
1063 os_memcmp(sel_uuid, uuid, 16) != 0) {
1064 ret = 1; /* PBC overlap */
1069 /* TODO: verify that this is reasonable dual-band situation */
1074 wpabuf_free(wps_ie);
1080 void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
1084 if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
1087 for (i = 0; i < wpa_s->scan_res->num; i++) {
1088 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
1090 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1093 if (wps_is_selected_pbc_registrar(ie))
1094 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
1095 else if (wps_is_selected_pin_registrar(ie))
1096 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
1098 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
1105 int wpas_wps_searching(struct wpa_supplicant *wpa_s)
1107 struct wpa_ssid *ssid;
1109 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1110 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
1118 int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
1121 struct wpabuf *wps_ie;
1124 wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
1128 ret = wps_attr_text(wps_ie, buf, end);
1129 wpabuf_free(wps_ie);
1134 int wpas_wps_er_start(struct wpa_supplicant *wpa_s)
1136 #ifdef CONFIG_WPS_ER
1137 if (wpa_s->wps_er) {
1138 wps_er_refresh(wpa_s->wps_er);
1141 wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname);
1142 if (wpa_s->wps_er == NULL)
1145 #else /* CONFIG_WPS_ER */
1147 #endif /* CONFIG_WPS_ER */
1151 int wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
1153 #ifdef CONFIG_WPS_ER
1154 wps_er_deinit(wpa_s->wps_er);
1155 wpa_s->wps_er = NULL;
1156 #endif /* CONFIG_WPS_ER */
1161 #ifdef CONFIG_WPS_ER
1162 int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const char *uuid,
1168 if (os_strcmp(uuid, "any") == 0)
1170 else if (uuid_str2bin(uuid, u))
1172 return wps_registrar_add_pin(wpa_s->wps->registrar, any ? NULL : u,
1173 (const u8 *) pin, os_strlen(pin), 300);
1177 int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
1181 if (uuid_str2bin(uuid, u))
1183 return wps_er_pbc(wpa_s->wps_er, u);
1187 int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
1192 if (uuid_str2bin(uuid, u))
1194 return wps_er_learn(wpa_s->wps_er, u, (const u8 *) pin,
1197 #endif /* CONFIG_WPS_ER */