]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/wps_supplicant.c
Remove extra whitespace
[thirdparty/hostap.git] / wpa_supplicant / wps_supplicant.c
CommitLineData
fa201b69
JM
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"
611ed491 19#include "ieee802_11_common.h"
fa201b69
JM
20#include "wpa_common.h"
21#include "config.h"
b01c18a8 22#include "eap_peer/eap.h"
fa201b69 23#include "wpa_supplicant_i.h"
2d5b792d 24#include "driver_i.h"
fcc60db4 25#include "eloop.h"
79da74a2 26#include "uuid.h"
a524f05e 27#include "wpa_ctrl.h"
47662164 28#include "ctrl_iface_dbus.h"
fcc60db4 29#include "eap_common/eap_wsc_common.h"
a6099152 30#include "blacklist.h"
7cc1b6c9 31#include "wpa.h"
fa201b69 32#include "wps_supplicant.h"
46bdb83a 33#include "dh_groups.h"
fa201b69 34
a6099152 35#define WPS_PIN_SCAN_IGNORE_SEL_REG 3
fa201b69 36
fcc60db4 37static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
469fc3a4 38static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
fcc60db4
JM
39
40
fa201b69
JM
41int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
42{
a6099152
JM
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
fcc60db4
JM
64 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
65
fa201b69
JM
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
47662164
JM
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
fa201b69
JM
86 return 0;
87}
88
89
7cc1b6c9
JM
90static 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
bcbbc7af
JM
186static int wpa_supplicant_wps_cred(void *ctx,
187 const struct wps_credential *cred)
fa201b69
JM
188{
189 struct wpa_supplicant *wpa_s = ctx;
190 struct wpa_ssid *ssid = wpa_s->current_ssid;
522b5b6e 191 u8 key_idx = 0;
49eba5f8 192 u16 auth_type;
fa201b69 193
47662164
JM
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 wpa_supplicant_dbus_notify_wps_cred(wpa_s, cred);
206 } else
207 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
fa201b69 208
eca6e0a9
JM
209 wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
210 cred->cred_attr, cred->cred_attr_len);
211
47662164
JM
212 if (wpa_s->conf->wps_cred_processing == 1)
213 return 0;
214
73267b9c
JM
215 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
216 wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
217 cred->auth_type);
218 wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
219 wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
220 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
221 cred->key, cred->key_len);
222 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
223 MAC2STR(cred->mac_addr));
224
49eba5f8
JM
225 auth_type = cred->auth_type;
226 if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
227 wpa_printf(MSG_DEBUG, "WPS: Workaround - convert mixed-mode "
228 "auth_type into WPA2PSK");
229 auth_type = WPS_AUTH_WPA2PSK;
230 }
231
232 if (auth_type != WPS_AUTH_OPEN &&
233 auth_type != WPS_AUTH_SHARED &&
234 auth_type != WPS_AUTH_WPAPSK &&
235 auth_type != WPS_AUTH_WPA2PSK) {
f286077d 236 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
49eba5f8
JM
237 "unsupported authentication type 0x%x",
238 auth_type);
f286077d
JM
239 return 0;
240 }
241
fa201b69
JM
242 if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
243 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
244 "on the received credential");
245 os_free(ssid->eap.identity);
246 ssid->eap.identity = NULL;
247 ssid->eap.identity_len = 0;
248 os_free(ssid->eap.phase1);
249 ssid->eap.phase1 = NULL;
250 os_free(ssid->eap.eap_methods);
251 ssid->eap.eap_methods = NULL;
252 } else {
253 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
254 "received credential");
255 ssid = wpa_config_add_network(wpa_s->conf);
256 if (ssid == NULL)
257 return -1;
258 }
259
260 wpa_config_set_network_defaults(ssid);
261
262 os_free(ssid->ssid);
263 ssid->ssid = os_malloc(cred->ssid_len);
264 if (ssid->ssid) {
265 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
266 ssid->ssid_len = cred->ssid_len;
267 }
268
269 switch (cred->encr_type) {
270 case WPS_ENCR_NONE:
fa201b69
JM
271 break;
272 case WPS_ENCR_WEP:
4b195a1b
AT
273 if (cred->key_len <= 0)
274 break;
275 if (cred->key_len != 5 && cred->key_len != 13 &&
276 cred->key_len != 10 && cred->key_len != 26) {
277 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key length "
278 "%lu", (unsigned long) cred->key_len);
279 return -1;
280 }
522b5b6e 281 if (cred->key_idx > NUM_WEP_KEYS) {
4b195a1b
AT
282 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key index %d",
283 cred->key_idx);
284 return -1;
285 }
522b5b6e
AT
286 if (cred->key_idx)
287 key_idx = cred->key_idx - 1;
4b195a1b
AT
288 if (cred->key_len == 10 || cred->key_len == 26) {
289 if (hexstr2bin((char *) cred->key,
290 ssid->wep_key[key_idx],
291 cred->key_len / 2) < 0) {
292 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key "
293 "%d", key_idx);
294 return -1;
295 }
296 ssid->wep_key_len[key_idx] = cred->key_len / 2;
297 } else {
298 os_memcpy(ssid->wep_key[key_idx], cred->key,
fa201b69 299 cred->key_len);
4b195a1b 300 ssid->wep_key_len[key_idx] = cred->key_len;
fa201b69 301 }
4b195a1b 302 ssid->wep_tx_keyidx = key_idx;
fa201b69
JM
303 break;
304 case WPS_ENCR_TKIP:
305 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
fa201b69
JM
306 break;
307 case WPS_ENCR_AES:
308 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
fa201b69
JM
309 break;
310 }
311
49eba5f8 312 switch (auth_type) {
fa201b69
JM
313 case WPS_AUTH_OPEN:
314 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
315 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
316 ssid->proto = 0;
317 break;
318 case WPS_AUTH_SHARED:
319 ssid->auth_alg = WPA_AUTH_ALG_SHARED;
320 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
321 ssid->proto = 0;
322 break;
323 case WPS_AUTH_WPAPSK:
324 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
325 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
326 ssid->proto = WPA_PROTO_WPA;
327 break;
328 case WPS_AUTH_WPA:
329 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
330 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
331 ssid->proto = WPA_PROTO_WPA;
332 break;
333 case WPS_AUTH_WPA2:
334 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
335 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
336 ssid->proto = WPA_PROTO_RSN;
337 break;
338 case WPS_AUTH_WPA2PSK:
339 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
340 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
341 ssid->proto = WPA_PROTO_RSN;
342 break;
343 }
344
345 if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
346 if (cred->key_len == 2 * PMK_LEN) {
347 if (hexstr2bin((const char *) cred->key, ssid->psk,
348 PMK_LEN)) {
349 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
350 "Key");
351 return -1;
352 }
353 ssid->psk_set = 1;
354 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
355 os_free(ssid->passphrase);
356 ssid->passphrase = os_malloc(cred->key_len + 1);
357 if (ssid->passphrase == NULL)
358 return -1;
359 os_memcpy(ssid->passphrase, cred->key, cred->key_len);
360 ssid->passphrase[cred->key_len] = '\0';
361 wpa_config_update_psk(ssid);
362 } else {
363 wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
364 "length %lu",
365 (unsigned long) cred->key_len);
366 return -1;
367 }
368 }
369
7cc1b6c9
JM
370 wpas_wps_security_workaround(wpa_s, ssid, cred);
371
fa201b69
JM
372#ifndef CONFIG_NO_CONFIG_WRITE
373 if (wpa_s->conf->update_config &&
374 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
375 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
376 return -1;
377 }
378#endif /* CONFIG_NO_CONFIG_WRITE */
379
380 return 0;
381}
382
383
4b68290e
JM
384static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
385 struct wps_event_m2d *m2d)
386{
387 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
388 "dev_password_id=%d config_error=%d",
389 m2d->dev_password_id, m2d->config_error);
390}
391
392
469fc3a4
JM
393static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
394 struct wps_event_fail *fail)
395{
396 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL "msg=%d", fail->msg);
397 wpas_clear_wps(wpa_s);
398}
399
400
ad5302a1
JM
401static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
402{
403 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
a6099152 404 wpa_s->wps_success = 1;
ad5302a1
JM
405}
406
407
4b68290e
JM
408static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
409 union wps_event_data *data)
410{
411 struct wpa_supplicant *wpa_s = ctx;
412 switch (event) {
413 case WPS_EV_M2D:
414 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
415 break;
469fc3a4
JM
416 case WPS_EV_FAIL:
417 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
418 break;
ad5302a1
JM
419 case WPS_EV_SUCCESS:
420 wpa_supplicant_wps_event_success(wpa_s);
421 break;
3b2cf800
JM
422 case WPS_EV_PWD_AUTH_FAIL:
423 break;
4b68290e
JM
424 }
425}
426
427
f90c86d4 428enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
fa201b69 429{
b01c18a8
JM
430 if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
431 eap_is_wps_pin_enrollee(&ssid->eap))
432 return WPS_REQ_ENROLLEE;
433 else
434 return WPS_REQ_REGISTRAR;
fa201b69 435}
116654ce
JM
436
437
fcc60db4
JM
438static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
439{
440 int id;
441 struct wpa_ssid *ssid;
442
443 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
444
445 /* Remove any existing WPS network from configuration */
446 ssid = wpa_s->conf->ssid;
447 while (ssid) {
f0477201
JM
448 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
449 if (ssid == wpa_s->current_ssid)
450 wpa_s->current_ssid = NULL;
fcc60db4 451 id = ssid->id;
f0477201 452 } else
fcc60db4
JM
453 id = -1;
454 ssid = ssid->next;
455 if (id >= 0)
456 wpa_config_remove_network(wpa_s->conf, id);
457 }
458}
459
460
461static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
462{
463 struct wpa_supplicant *wpa_s = eloop_ctx;
a6099152
JM
464 wpa_printf(MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
465 "out");
fcc60db4
JM
466 wpas_clear_wps(wpa_s);
467}
468
469
470static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
471 int registrar, const u8 *bssid)
472{
473 struct wpa_ssid *ssid;
474
475 ssid = wpa_config_add_network(wpa_s->conf);
476 if (ssid == NULL)
477 return NULL;
478 wpa_config_set_network_defaults(ssid);
479 if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
480 wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
481 wpa_config_set(ssid, "identity", registrar ?
482 "\"" WSC_ID_REGISTRAR "\"" :
483 "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
484 wpa_config_remove_network(wpa_s->conf, ssid->id);
485 return NULL;
486 }
487
488 if (bssid) {
489 size_t i;
490 struct wpa_scan_res *res;
491
492 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
24c23d1b 493 ssid->bssid_set = 1;
fcc60db4
JM
494
495 /* Try to get SSID from scan results */
496 if (wpa_s->scan_res == NULL &&
497 wpa_supplicant_get_scan_results(wpa_s) < 0)
498 return ssid; /* Could not find any scan results */
499
500 for (i = 0; i < wpa_s->scan_res->num; i++) {
501 const u8 *ie;
502
503 res = wpa_s->scan_res->res[i];
504 if (os_memcmp(bssid, res->bssid, ETH_ALEN) != 0)
505 continue;
506
507 ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
508 if (ie == NULL)
509 break;
510 os_free(ssid->ssid);
511 ssid->ssid = os_malloc(ie[1]);
512 if (ssid->ssid == NULL)
513 break;
514 os_memcpy(ssid->ssid, ie + 2, ie[1]);
515 ssid->ssid_len = ie[1];
516 break;
517 }
518 }
519
520 return ssid;
521}
522
523
524static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
525 struct wpa_ssid *selected)
526{
527 struct wpa_ssid *ssid;
528
529 /* Mark all other networks disabled and trigger reassociation */
530 ssid = wpa_s->conf->ssid;
531 while (ssid) {
532 ssid->disabled = ssid != selected;
533 ssid = ssid->next;
534 }
535 wpa_s->disconnected = 0;
536 wpa_s->reassociate = 1;
a6099152
JM
537 wpa_s->scan_runs = 0;
538 wpa_s->wps_success = 0;
539 wpa_s->blacklist_cleared = 0;
fcc60db4
JM
540 wpa_supplicant_req_scan(wpa_s, 0, 0);
541}
542
543
544int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
545{
546 struct wpa_ssid *ssid;
547 wpas_clear_wps(wpa_s);
548 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
549 if (ssid == NULL)
550 return -1;
551 wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
552 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
553 wpa_s, NULL);
554 wpas_wps_reassoc(wpa_s, ssid);
555 return 0;
556}
557
558
559int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
560 const char *pin)
561{
562 struct wpa_ssid *ssid;
46bdb83a 563 char val[128];
fcc60db4
JM
564 unsigned int rpin = 0;
565
566 wpas_clear_wps(wpa_s);
567 ssid = wpas_wps_add_network(wpa_s, 0, bssid);
568 if (ssid == NULL)
569 return -1;
570 if (pin)
571 os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
572 else {
573 rpin = wps_generate_pin();
574 os_snprintf(val, sizeof(val), "\"pin=%08d\"", rpin);
575 }
576 wpa_config_set(ssid, "phase1", val, 0);
577 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
578 wpa_s, NULL);
579 wpas_wps_reassoc(wpa_s, ssid);
580 return rpin;
581}
582
583
116f7bb0 584#ifdef CONFIG_WPS_OOB
46bdb83a 585int wpas_wps_start_oob(struct wpa_supplicant *wpa_s, char *device_type,
e1ee6b60 586 char *path, char *method, char *name)
46bdb83a
MH
587{
588 struct wps_context *wps = wpa_s->wps;
7cbf51bb 589 struct oob_device_data *oob_dev;
46bdb83a 590
7cbf51bb
JM
591 oob_dev = wps_get_oob_device(device_type);
592 if (oob_dev == NULL)
46bdb83a 593 return -1;
7cbf51bb 594 oob_dev->device_path = path;
e1ee6b60 595 oob_dev->device_name = name;
46bdb83a
MH
596 wps->oob_conf.oob_method = wps_get_oob_method(method);
597
d5e2b2d2
JM
598 if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E) {
599 /*
600 * Use pre-configured DH keys in order to be able to write the
601 * key hash into the OOB file.
602 */
603 wpabuf_free(wps->dh_pubkey);
604 wpabuf_free(wps->dh_privkey);
605 wps->dh_privkey = NULL;
606 wps->dh_pubkey = dh_init(dh_groups_get(WPS_DH_GROUP),
607 &wps->dh_privkey);
608 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
609 if (wps->dh_pubkey == NULL) {
610 wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
611 "Diffie-Hellman handshake");
612 return -1;
613 }
614 }
615
46bdb83a
MH
616 if (wps->oob_conf.oob_method == OOB_METHOD_CRED)
617 wpas_clear_wps(wpa_s);
618
7cbf51bb 619 if (wps_process_oob(wps, oob_dev, 0) < 0)
46bdb83a
MH
620 return -1;
621
622 if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
623 wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
624 wpas_wps_start_pin(wpa_s, NULL,
625 wpabuf_head(wps->oob_conf.dev_password)) < 0)
626 return -1;
627
628 return 0;
629}
116f7bb0 630#endif /* CONFIG_WPS_OOB */
46bdb83a
MH
631
632
fcc60db4 633int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
52eb293d 634 const char *pin, struct wps_new_ap_settings *settings)
fcc60db4
JM
635{
636 struct wpa_ssid *ssid;
52eb293d
JM
637 char val[200];
638 char *pos, *end;
639 int res;
fcc60db4
JM
640
641 if (!pin)
642 return -1;
643 wpas_clear_wps(wpa_s);
644 ssid = wpas_wps_add_network(wpa_s, 1, bssid);
645 if (ssid == NULL)
646 return -1;
52eb293d
JM
647 pos = val;
648 end = pos + sizeof(val);
649 res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
650 if (res < 0 || res >= end - pos)
651 return -1;
652 pos += res;
653 if (settings) {
654 res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
655 "new_encr=%s new_key=%s",
656 settings->ssid_hex, settings->auth,
657 settings->encr, settings->key_hex);
658 if (res < 0 || res >= end - pos)
659 return -1;
660 pos += res;
661 }
662 res = os_snprintf(pos, end - pos, "\"");
663 if (res < 0 || res >= end - pos)
664 return -1;
fcc60db4
JM
665 wpa_config_set(ssid, "phase1", val, 0);
666 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
667 wpa_s, NULL);
668 wpas_wps_reassoc(wpa_s, ssid);
669 return 0;
670}
671
672
c5adf528
JM
673static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
674 size_t psk_len)
675{
676 wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
677 "STA " MACSTR, MAC2STR(mac_addr));
678 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
679
680 /* TODO */
681
682 return 0;
683}
684
685
686static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
687 const struct wps_device_data *dev)
688{
689 char uuid[40], txt[400];
690 int len;
691 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
692 return;
693 wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
694 len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
695 " [%s|%s|%s|%s|%s|%d-%08X-%d]",
696 uuid, MAC2STR(dev->mac_addr), dev->device_name,
697 dev->manufacturer, dev->model_name,
698 dev->model_number, dev->serial_number,
699 dev->categ, dev->oui, dev->sub_categ);
700 if (len > 0 && len < (int) sizeof(txt))
701 wpa_printf(MSG_INFO, "%s", txt);
702}
703
704
116654ce
JM
705int wpas_wps_init(struct wpa_supplicant *wpa_s)
706{
707 struct wps_context *wps;
c5adf528 708 struct wps_registrar_config rcfg;
116654ce
JM
709
710 wps = os_zalloc(sizeof(*wps));
711 if (wps == NULL)
712 return -1;
713
714 wps->cred_cb = wpa_supplicant_wps_cred;
4b68290e 715 wps->event_cb = wpa_supplicant_wps_event;
116654ce
JM
716 wps->cb_ctx = wpa_s;
717
3c0b7aa4
JM
718 wps->dev.device_name = wpa_s->conf->device_name;
719 wps->dev.manufacturer = wpa_s->conf->manufacturer;
720 wps->dev.model_name = wpa_s->conf->model_name;
721 wps->dev.model_number = wpa_s->conf->model_number;
722 wps->dev.serial_number = wpa_s->conf->serial_number;
723 if (wpa_s->conf->device_type) {
724 char *pos;
725 u8 oui[4];
726 /* <categ>-<OUI>-<subcateg> */
727 wps->dev.categ = atoi(wpa_s->conf->device_type);
728 pos = os_strchr(wpa_s->conf->device_type, '-');
729 if (pos == NULL) {
730 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
731 os_free(wps);
732 return -1;
733 }
734 pos++;
735 if (hexstr2bin(pos, oui, 4)) {
736 wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
737 os_free(wps);
738 return -1;
739 }
740 wps->dev.oui = WPA_GET_BE32(oui);
741 pos = os_strchr(pos, '-');
742 if (pos == NULL) {
743 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
744 os_free(wps);
745 return -1;
746 }
747 pos++;
748 wps->dev.sub_categ = atoi(pos);
749 }
750 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
751 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
398cfbf6 752 os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
79da74a2
JM
753 if (is_nil_uuid(wpa_s->conf->uuid)) {
754 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
755 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
756 wps->uuid, WPS_UUID_LEN);
757 } else
758 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
116654ce 759
c5adf528
JM
760 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
761 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
762
763 os_memset(&rcfg, 0, sizeof(rcfg));
764 rcfg.new_psk_cb = wpas_wps_new_psk_cb;
765 rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
766 rcfg.cb_ctx = wpa_s;
767
768 wps->registrar = wps_registrar_init(wps, &rcfg);
769 if (wps->registrar == NULL) {
770 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
771 os_free(wps);
772 return -1;
773 }
774
116654ce
JM
775 wpa_s->wps = wps;
776
777 return 0;
778}
779
780
781void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
782{
fcc60db4
JM
783 eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
784
116654ce
JM
785 if (wpa_s->wps == NULL)
786 return;
787
c5adf528 788 wps_registrar_deinit(wpa_s->wps->registrar);
46bdb83a
MH
789 wpabuf_free(wpa_s->wps->dh_pubkey);
790 wpabuf_free(wpa_s->wps->dh_privkey);
791 wpabuf_free(wpa_s->wps->oob_conf.pubkey_hash);
792 wpabuf_free(wpa_s->wps->oob_conf.dev_password);
116654ce
JM
793 os_free(wpa_s->wps->network_key);
794 os_free(wpa_s->wps);
795 wpa_s->wps = NULL;
796}
351f09a2
JM
797
798
a6099152
JM
799int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
800 struct wpa_ssid *ssid, struct wpa_scan_res *bss)
351f09a2
JM
801{
802 struct wpabuf *wps_ie;
803
804 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
805 return -1;
806
807 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
808 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
809 if (!wps_ie) {
810 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
811 return 0;
812 }
813
814 if (!wps_is_selected_pbc_registrar(wps_ie)) {
815 wpa_printf(MSG_DEBUG, " skip - WPS AP "
816 "without active PBC Registrar");
817 wpabuf_free(wps_ie);
818 return 0;
819 }
820
821 /* TODO: overlap detection */
822 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
823 "(Active PBC)");
824 wpabuf_free(wps_ie);
825 return 1;
826 }
827
828 if (eap_is_wps_pin_enrollee(&ssid->eap)) {
829 if (!wps_ie) {
830 wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
831 return 0;
832 }
833
a6099152
JM
834 /*
835 * Start with WPS APs that advertise active PIN Registrar and
836 * allow any WPS AP after third scan since some APs do not set
837 * Selected Registrar attribute properly when using external
838 * Registrar.
839 */
351f09a2 840 if (!wps_is_selected_pin_registrar(wps_ie)) {
a6099152
JM
841 if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
842 wpa_printf(MSG_DEBUG, " skip - WPS AP "
843 "without active PIN Registrar");
844 wpabuf_free(wps_ie);
845 return 0;
846 }
847 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
848 } else {
849 wpa_printf(MSG_DEBUG, " selected based on WPS IE "
850 "(Active PIN)");
351f09a2 851 }
351f09a2
JM
852 wpabuf_free(wps_ie);
853 return 1;
854 }
855
856 if (wps_ie) {
857 wpa_printf(MSG_DEBUG, " selected based on WPS IE");
858 wpabuf_free(wps_ie);
859 return 1;
860 }
861
862 return -1;
863}
864
865
a6099152
JM
866int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
867 struct wpa_ssid *ssid,
351f09a2
JM
868 struct wpa_scan_res *bss)
869{
870 struct wpabuf *wps_ie = NULL;
871 int ret = 0;
872
873 if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
874 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
875 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
876 /* allow wildcard SSID for WPS PBC */
877 ret = 1;
878 }
879 } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
880 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
a6099152
JM
881 if (wps_ie &&
882 (wps_is_selected_pin_registrar(wps_ie) ||
883 wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
351f09a2
JM
884 /* allow wildcard SSID for WPS PIN */
885 ret = 1;
886 }
887 }
888
24c23d1b
JM
889 if (!ret && ssid->bssid_set &&
890 os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
891 /* allow wildcard SSID due to hardcoded BSSID match */
892 ret = 1;
893 }
894
351f09a2
JM
895 wpabuf_free(wps_ie);
896
897 return ret;
898}
899
900
901int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
902 struct wpa_scan_res *selected,
903 struct wpa_ssid *ssid)
904{
905 const u8 *sel_uuid, *uuid;
906 size_t i;
907 struct wpabuf *wps_ie;
908 int ret = 0;
909
910 if (!eap_is_wps_pbc_enrollee(&ssid->eap))
911 return 0;
912
913 /* Make sure that only one AP is in active PBC mode */
914 wps_ie = wpa_scan_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
915 if (wps_ie)
916 sel_uuid = wps_get_uuid_e(wps_ie);
917 else
918 sel_uuid = NULL;
351f09a2
JM
919
920 for (i = 0; i < wpa_s->scan_res->num; i++) {
921 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
922 struct wpabuf *ie;
923 if (bss == selected)
924 continue;
925 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
926 if (!ie)
927 continue;
928 if (!wps_is_selected_pbc_registrar(ie)) {
929 wpabuf_free(ie);
930 continue;
931 }
932 uuid = wps_get_uuid_e(ie);
44cd430f
JM
933 if (sel_uuid == NULL || uuid == NULL ||
934 os_memcmp(sel_uuid, uuid, 16) != 0) {
351f09a2
JM
935 ret = 1; /* PBC overlap */
936 wpabuf_free(ie);
937 break;
938 }
939
940 /* TODO: verify that this is reasonable dual-band situation */
484957ae
JM
941
942 wpabuf_free(ie);
351f09a2
JM
943 }
944
945 wpabuf_free(wps_ie);
946
947 return ret;
948}
a524f05e
JM
949
950
951void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
952{
953 size_t i;
954
955 if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
956 return;
957
958 for (i = 0; i < wpa_s->scan_res->num; i++) {
959 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
960 struct wpabuf *ie;
961 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
962 if (!ie)
963 continue;
964 if (wps_is_selected_pbc_registrar(ie))
965 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
966 else if (wps_is_selected_pin_registrar(ie))
967 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
968 else
969 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
970 wpabuf_free(ie);
971 break;
972 }
973}
24c23d1b
JM
974
975
976int wpas_wps_searching(struct wpa_supplicant *wpa_s)
977{
978 struct wpa_ssid *ssid;
979
980 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
981 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
982 return 1;
983 }
984
985 return 0;
986}
611ed491
JM
987
988
989int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
990 char *end)
991{
992 struct wpabuf *wps_ie;
993 int ret;
994
995 wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
996 if (wps_ie == NULL)
997 return 0;
998
999 ret = wps_attr_text(wps_ie, buf, end);
1000 wpabuf_free(wps_ie);
1001 return ret;
1002}