WPS: Add option to disable open networks by default
[mech_eap.git] / wpa_supplicant / wps_supplicant.c
1 /*
2  * wpa_supplicant / WPS integration
3  * Copyright (c) 2008-2010, 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 "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 "bss.h"
34 #include "scan.h"
35 #include "ap.h"
36 #include "p2p/p2p.h"
37 #include "p2p_supplicant.h"
38 #include "wps_supplicant.h"
39
40
41 #ifndef WPS_PIN_SCAN_IGNORE_SEL_REG
42 #define WPS_PIN_SCAN_IGNORE_SEL_REG 3
43 #endif /* WPS_PIN_SCAN_IGNORE_SEL_REG */
44
45 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
46 static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
47
48
49 int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
50 {
51         if (!wpa_s->wps_success &&
52             wpa_s->current_ssid &&
53             eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
54                 const u8 *bssid = wpa_s->bssid;
55                 if (is_zero_ether_addr(bssid))
56                         bssid = wpa_s->pending_bssid;
57
58                 wpa_printf(MSG_DEBUG, "WPS: PIN registration with " MACSTR
59                            " did not succeed - continue trying to find "
60                            "suitable AP", MAC2STR(bssid));
61                 wpa_blacklist_add(wpa_s, bssid);
62
63                 wpa_supplicant_deauthenticate(wpa_s,
64                                               WLAN_REASON_DEAUTH_LEAVING);
65                 wpa_s->reassociate = 1;
66                 wpa_supplicant_req_scan(wpa_s,
67                                         wpa_s->blacklist_cleared ? 5 : 0, 0);
68                 wpa_s->blacklist_cleared = 0;
69                 return 1;
70         }
71
72         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
73
74         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
75             !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
76                 int disabled = wpa_s->current_ssid->disabled;
77                 wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
78                            "try to associate with the received credential");
79                 wpa_supplicant_deauthenticate(wpa_s,
80                                               WLAN_REASON_DEAUTH_LEAVING);
81                 if (disabled) {
82                         wpa_printf(MSG_DEBUG, "WPS: Current network is "
83                                    "disabled - wait for user to enable");
84                         return 1;
85                 }
86                 wpa_s->after_wps = 5;
87                 wpa_s->wps_freq = wpa_s->assoc_freq;
88                 wpa_s->reassociate = 1;
89                 wpa_supplicant_req_scan(wpa_s, 0, 0);
90                 return 1;
91         }
92
93         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
94                 wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
95                            "for external credential processing");
96                 wpas_clear_wps(wpa_s);
97                 wpa_supplicant_deauthenticate(wpa_s,
98                                               WLAN_REASON_DEAUTH_LEAVING);
99                 return 1;
100         }
101
102         return 0;
103 }
104
105
106 static void wpas_wps_security_workaround(struct wpa_supplicant *wpa_s,
107                                          struct wpa_ssid *ssid,
108                                          const struct wps_credential *cred)
109 {
110         struct wpa_driver_capa capa;
111         struct wpa_bss *bss;
112         const u8 *ie;
113         struct wpa_ie_data adv;
114         int wpa2 = 0, ccmp = 0;
115
116         /*
117          * Many existing WPS APs do not know how to negotiate WPA2 or CCMP in
118          * case they are configured for mixed mode operation (WPA+WPA2 and
119          * TKIP+CCMP). Try to use scan results to figure out whether the AP
120          * actually supports stronger security and select that if the client
121          * has support for it, too.
122          */
123
124         if (wpa_drv_get_capa(wpa_s, &capa))
125                 return; /* Unknown what driver supports */
126
127         bss = wpa_bss_get(wpa_s, cred->mac_addr, ssid->ssid, ssid->ssid_len);
128         if (bss == NULL) {
129                 wpa_printf(MSG_DEBUG, "WPS: The AP was not found from BSS "
130                            "table - use credential as-is");
131                 return;
132         }
133
134         wpa_printf(MSG_DEBUG, "WPS: AP found from BSS table");
135
136         ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
137         if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0) {
138                 wpa2 = 1;
139                 if (adv.pairwise_cipher & WPA_CIPHER_CCMP)
140                         ccmp = 1;
141         } else {
142                 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
143                 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0 &&
144                     adv.pairwise_cipher & WPA_CIPHER_CCMP)
145                         ccmp = 1;
146         }
147
148         if (ie == NULL && (ssid->proto & WPA_PROTO_WPA) &&
149             (ssid->pairwise_cipher & WPA_CIPHER_TKIP)) {
150                 /*
151                  * TODO: This could be the initial AP configuration and the
152                  * Beacon contents could change shortly. Should request a new
153                  * scan and delay addition of the network until the updated
154                  * scan results are available.
155                  */
156                 wpa_printf(MSG_DEBUG, "WPS: The AP did not yet advertise WPA "
157                            "support - use credential as-is");
158                 return;
159         }
160
161         if (ccmp && !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
162             (ssid->pairwise_cipher & WPA_CIPHER_TKIP) &&
163             (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
164                 wpa_printf(MSG_DEBUG, "WPS: Add CCMP into the credential "
165                            "based on scan results");
166                 if (wpa_s->conf->ap_scan == 1)
167                         ssid->pairwise_cipher |= WPA_CIPHER_CCMP;
168                 else
169                         ssid->pairwise_cipher = WPA_CIPHER_CCMP;
170         }
171
172         if (wpa2 && !(ssid->proto & WPA_PROTO_RSN) &&
173             (ssid->proto & WPA_PROTO_WPA) &&
174             (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP)) {
175                 wpa_printf(MSG_DEBUG, "WPS: Add WPA2 into the credential "
176                            "based on scan results");
177                 if (wpa_s->conf->ap_scan == 1)
178                         ssid->proto |= WPA_PROTO_RSN;
179                 else
180                         ssid->proto = WPA_PROTO_RSN;
181         }
182 }
183
184
185 static int wpa_supplicant_wps_cred(void *ctx,
186                                    const struct wps_credential *cred)
187 {
188         struct wpa_supplicant *wpa_s = ctx;
189         struct wpa_ssid *ssid = wpa_s->current_ssid;
190         u8 key_idx = 0;
191         u16 auth_type;
192         int registrar = 0;
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                 if (ssid->eap.identity &&
247                     ssid->eap.identity_len == WSC_ID_REGISTRAR_LEN &&
248                     os_memcmp(ssid->eap.identity, WSC_ID_REGISTRAR,
249                               WSC_ID_REGISTRAR_LEN) == 0)
250                         registrar = 1;
251                 os_free(ssid->eap.identity);
252                 ssid->eap.identity = NULL;
253                 ssid->eap.identity_len = 0;
254                 os_free(ssid->eap.phase1);
255                 ssid->eap.phase1 = NULL;
256                 os_free(ssid->eap.eap_methods);
257                 ssid->eap.eap_methods = NULL;
258                 if (!ssid->p2p_group)
259                         ssid->temporary = 0;
260         } else {
261                 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
262                            "received credential");
263                 ssid = wpa_config_add_network(wpa_s->conf);
264                 if (ssid == NULL)
265                         return -1;
266                 wpas_notify_network_added(wpa_s, ssid);
267         }
268
269         wpa_config_set_network_defaults(ssid);
270
271         os_free(ssid->ssid);
272         ssid->ssid = os_malloc(cred->ssid_len);
273         if (ssid->ssid) {
274                 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
275                 ssid->ssid_len = cred->ssid_len;
276         }
277
278         switch (cred->encr_type) {
279         case WPS_ENCR_NONE:
280                 break;
281         case WPS_ENCR_WEP:
282                 if (cred->key_len <= 0)
283                         break;
284                 if (cred->key_len != 5 && cred->key_len != 13 &&
285                     cred->key_len != 10 && cred->key_len != 26) {
286                         wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key length "
287                                    "%lu", (unsigned long) cred->key_len);
288                         return -1;
289                 }
290                 if (cred->key_idx > NUM_WEP_KEYS) {
291                         wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key index %d",
292                                    cred->key_idx);
293                         return -1;
294                 }
295                 if (cred->key_idx)
296                         key_idx = cred->key_idx - 1;
297                 if (cred->key_len == 10 || cred->key_len == 26) {
298                         if (hexstr2bin((char *) cred->key,
299                                        ssid->wep_key[key_idx],
300                                        cred->key_len / 2) < 0) {
301                                 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key "
302                                            "%d", key_idx);
303                                 return -1;
304                         }
305                         ssid->wep_key_len[key_idx] = cred->key_len / 2;
306                 } else {
307                         os_memcpy(ssid->wep_key[key_idx], cred->key,
308                                   cred->key_len);
309                         ssid->wep_key_len[key_idx] = cred->key_len;
310                 }
311                 ssid->wep_tx_keyidx = key_idx;
312                 break;
313         case WPS_ENCR_TKIP:
314                 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
315                 break;
316         case WPS_ENCR_AES:
317                 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
318                 break;
319         }
320
321         switch (auth_type) {
322         case WPS_AUTH_OPEN:
323                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
324                 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
325                 ssid->proto = 0;
326 #ifdef CONFIG_WPS_REG_DISABLE_OPEN
327                 if (registrar) {
328                         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OPEN_NETWORK
329                                 "id=%d - Credentials for an open "
330                                 "network disabled by default - use "
331                                 "'select_network %d' to enable",
332                                 ssid->id, ssid->id);
333                         ssid->disabled = 1;
334                 }
335 #endif /* CONFIG_WPS_REG_DISABLE_OPEN */
336                 break;
337         case WPS_AUTH_SHARED:
338                 ssid->auth_alg = WPA_AUTH_ALG_SHARED;
339                 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
340                 ssid->proto = 0;
341                 break;
342         case WPS_AUTH_WPAPSK:
343                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
344                 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
345                 ssid->proto = WPA_PROTO_WPA;
346                 break;
347         case WPS_AUTH_WPA:
348                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
349                 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
350                 ssid->proto = WPA_PROTO_WPA;
351                 break;
352         case WPS_AUTH_WPA2:
353                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
354                 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
355                 ssid->proto = WPA_PROTO_RSN;
356                 break;
357         case WPS_AUTH_WPA2PSK:
358                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
359                 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
360                 ssid->proto = WPA_PROTO_RSN;
361                 break;
362         }
363
364         if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
365                 if (cred->key_len == 2 * PMK_LEN) {
366                         if (hexstr2bin((const char *) cred->key, ssid->psk,
367                                        PMK_LEN)) {
368                                 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
369                                            "Key");
370                                 return -1;
371                         }
372                         ssid->psk_set = 1;
373                 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
374                         os_free(ssid->passphrase);
375                         ssid->passphrase = os_malloc(cred->key_len + 1);
376                         if (ssid->passphrase == NULL)
377                                 return -1;
378                         os_memcpy(ssid->passphrase, cred->key, cred->key_len);
379                         ssid->passphrase[cred->key_len] = '\0';
380                         wpa_config_update_psk(ssid);
381                 } else {
382                         wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
383                                    "length %lu",
384                                    (unsigned long) cred->key_len);
385                         return -1;
386                 }
387         }
388
389         wpas_wps_security_workaround(wpa_s, ssid, cred);
390
391 #ifndef CONFIG_NO_CONFIG_WRITE
392         if (wpa_s->conf->update_config &&
393             wpa_config_write(wpa_s->confname, wpa_s->conf)) {
394                 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
395                 return -1;
396         }
397 #endif /* CONFIG_NO_CONFIG_WRITE */
398
399         return 0;
400 }
401
402
403 static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
404                                          struct wps_event_m2d *m2d)
405 {
406         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
407                 "dev_password_id=%d config_error=%d",
408                 m2d->dev_password_id, m2d->config_error);
409         wpas_notify_wps_event_m2d(wpa_s, m2d);
410 }
411
412
413 static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
414                                           struct wps_event_fail *fail)
415 {
416         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL "msg=%d config_error=%d",
417                 fail->msg, fail->config_error);
418         if (wpa_s->parent && wpa_s->parent != wpa_s)
419                 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
420                         "msg=%d config_error=%d",
421                         fail->msg, fail->config_error);
422         wpas_clear_wps(wpa_s);
423         wpas_notify_wps_event_fail(wpa_s, fail);
424 }
425
426
427 static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
428 {
429         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
430         wpa_s->wps_success = 1;
431         wpas_notify_wps_event_success(wpa_s);
432 #ifdef CONFIG_P2P
433         wpas_p2p_wps_success(wpa_s, wpa_s->bssid, 0);
434 #endif /* CONFIG_P2P */
435 }
436
437
438 static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
439                                                struct wps_event_er_ap *ap)
440 {
441         char uuid_str[100];
442         char dev_type[WPS_DEV_TYPE_BUFSIZE];
443
444         uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
445         if (ap->pri_dev_type)
446                 wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
447                                      sizeof(dev_type));
448         else
449                 dev_type[0] = '\0';
450
451         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
452                 " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
453                 uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
454                 ap->friendly_name ? ap->friendly_name : "",
455                 ap->manufacturer ? ap->manufacturer : "",
456                 ap->model_description ? ap->model_description : "",
457                 ap->model_name ? ap->model_name : "",
458                 ap->manufacturer_url ? ap->manufacturer_url : "",
459                 ap->model_url ? ap->model_url : "");
460 }
461
462
463 static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
464                                                   struct wps_event_er_ap *ap)
465 {
466         char uuid_str[100];
467         uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
468         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
469 }
470
471
472 static void wpa_supplicant_wps_event_er_enrollee_add(
473         struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
474 {
475         char uuid_str[100];
476         char dev_type[WPS_DEV_TYPE_BUFSIZE];
477
478         uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
479         if (enrollee->pri_dev_type)
480                 wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
481                                      sizeof(dev_type));
482         else
483                 dev_type[0] = '\0';
484
485         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
486                 " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
487                 "|%s|%s|%s|%s|%s|",
488                 uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
489                 enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
490                 enrollee->dev_name ? enrollee->dev_name : "",
491                 enrollee->manufacturer ? enrollee->manufacturer : "",
492                 enrollee->model_name ? enrollee->model_name : "",
493                 enrollee->model_number ? enrollee->model_number : "",
494                 enrollee->serial_number ? enrollee->serial_number : "");
495 }
496
497
498 static void wpa_supplicant_wps_event_er_enrollee_remove(
499         struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
500 {
501         char uuid_str[100];
502         uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
503         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
504                 uuid_str, MAC2STR(enrollee->mac_addr));
505 }
506
507
508 static void wpa_supplicant_wps_event_er_ap_settings(
509         struct wpa_supplicant *wpa_s,
510         struct wps_event_er_ap_settings *ap_settings)
511 {
512         char uuid_str[100];
513         char key_str[65];
514         const struct wps_credential *cred = ap_settings->cred;
515
516         key_str[0] = '\0';
517         if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
518                 if (cred->key_len >= 8 && cred->key_len <= 64) {
519                         os_memcpy(key_str, cred->key, cred->key_len);
520                         key_str[cred->key_len] = '\0';
521                 }
522         }
523
524         uuid_bin2str(ap_settings->uuid, uuid_str, sizeof(uuid_str));
525         /* Use wpa_msg_ctrl to avoid showing the key in debug log */
526         wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_SETTINGS
527                      "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
528                      "key=%s",
529                      uuid_str, wpa_ssid_txt(cred->ssid, cred->ssid_len),
530                      cred->auth_type, cred->encr_type, key_str);
531 }
532
533
534 static void wpa_supplicant_wps_event_er_set_sel_reg(
535         struct wpa_supplicant *wpa_s,
536         struct wps_event_er_set_selected_registrar *ev)
537 {
538         char uuid_str[100];
539
540         uuid_bin2str(ev->uuid, uuid_str, sizeof(uuid_str));
541         switch (ev->state) {
542         case WPS_ER_SET_SEL_REG_START:
543                 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
544                         "uuid=%s state=START sel_reg=%d dev_passwd_id=%u "
545                         "sel_reg_config_methods=0x%x",
546                         uuid_str, ev->sel_reg, ev->dev_passwd_id,
547                         ev->sel_reg_config_methods);
548                 break;
549         case WPS_ER_SET_SEL_REG_DONE:
550                 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
551                         "uuid=%s state=DONE", uuid_str);
552                 break;
553         case WPS_ER_SET_SEL_REG_FAILED:
554                 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_SET_SEL_REG
555                         "uuid=%s state=FAILED", uuid_str);
556                 break;
557         }
558 }
559
560
561 static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
562                                      union wps_event_data *data)
563 {
564         struct wpa_supplicant *wpa_s = ctx;
565         switch (event) {
566         case WPS_EV_M2D:
567                 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
568                 break;
569         case WPS_EV_FAIL:
570                 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
571                 break;
572         case WPS_EV_SUCCESS:
573                 wpa_supplicant_wps_event_success(wpa_s);
574                 break;
575         case WPS_EV_PWD_AUTH_FAIL:
576 #ifdef CONFIG_AP
577                 if (wpa_s->ap_iface && data->pwd_auth_fail.enrollee)
578                         wpa_supplicant_ap_pwd_auth_fail(wpa_s);
579 #endif /* CONFIG_AP */
580                 break;
581         case WPS_EV_PBC_OVERLAP:
582                 break;
583         case WPS_EV_PBC_TIMEOUT:
584                 break;
585         case WPS_EV_ER_AP_ADD:
586                 wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
587                 break;
588         case WPS_EV_ER_AP_REMOVE:
589                 wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
590                 break;
591         case WPS_EV_ER_ENROLLEE_ADD:
592                 wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
593                                                          &data->enrollee);
594                 break;
595         case WPS_EV_ER_ENROLLEE_REMOVE:
596                 wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
597                                                             &data->enrollee);
598                 break;
599         case WPS_EV_ER_AP_SETTINGS:
600                 wpa_supplicant_wps_event_er_ap_settings(wpa_s,
601                                                         &data->ap_settings);
602                 break;
603         case WPS_EV_ER_SET_SELECTED_REGISTRAR:
604                 wpa_supplicant_wps_event_er_set_sel_reg(wpa_s,
605                                                         &data->set_sel_reg);
606                 break;
607         }
608 }
609
610
611 enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
612 {
613         if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
614             eap_is_wps_pin_enrollee(&ssid->eap))
615                 return WPS_REQ_ENROLLEE;
616         else
617                 return WPS_REQ_REGISTRAR;
618 }
619
620
621 static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
622 {
623         int id;
624         struct wpa_ssid *ssid, *remove_ssid = NULL;
625
626         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
627
628         /* Remove any existing WPS network from configuration */
629         ssid = wpa_s->conf->ssid;
630         while (ssid) {
631                 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
632                         if (ssid == wpa_s->current_ssid) {
633                                 wpa_s->current_ssid = NULL;
634                                 if (ssid != NULL)
635                                         wpas_notify_network_changed(wpa_s);
636                         }
637                         id = ssid->id;
638                         remove_ssid = ssid;
639                 } else
640                         id = -1;
641                 ssid = ssid->next;
642                 if (id >= 0) {
643                         wpas_notify_network_removed(wpa_s, remove_ssid);
644                         wpa_config_remove_network(wpa_s->conf, id);
645                 }
646         }
647 }
648
649
650 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
651 {
652         struct wpa_supplicant *wpa_s = eloop_ctx;
653         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
654                 "out");
655         wpas_clear_wps(wpa_s);
656 }
657
658
659 static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
660                                               int registrar, const u8 *bssid)
661 {
662         struct wpa_ssid *ssid;
663
664         ssid = wpa_config_add_network(wpa_s->conf);
665         if (ssid == NULL)
666                 return NULL;
667         wpas_notify_network_added(wpa_s, ssid);
668         wpa_config_set_network_defaults(ssid);
669         ssid->temporary = 1;
670         if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
671             wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
672             wpa_config_set(ssid, "identity", registrar ?
673                            "\"" WSC_ID_REGISTRAR "\"" :
674                            "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
675                 wpas_notify_network_removed(wpa_s, ssid);
676                 wpa_config_remove_network(wpa_s->conf, ssid->id);
677                 return NULL;
678         }
679
680         if (bssid) {
681 #ifndef CONFIG_P2P
682                 struct wpa_bss *bss;
683                 int count = 0;
684 #endif /* CONFIG_P2P */
685
686                 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
687                 ssid->bssid_set = 1;
688
689                 /*
690                  * Note: With P2P, the SSID may change at the time the WPS
691                  * provisioning is started, so better not filter the AP based
692                  * on the current SSID in the scan results.
693                  */
694 #ifndef CONFIG_P2P
695                 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
696                         if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
697                                 continue;
698
699                         os_free(ssid->ssid);
700                         ssid->ssid = os_malloc(bss->ssid_len);
701                         if (ssid->ssid == NULL)
702                                 break;
703                         os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
704                         ssid->ssid_len = bss->ssid_len;
705                         wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
706                                           "scan results",
707                                           ssid->ssid, ssid->ssid_len);
708                         count++;
709                 }
710
711                 if (count > 1) {
712                         wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
713                                    "for the AP; use wildcard");
714                         os_free(ssid->ssid);
715                         ssid->ssid = NULL;
716                         ssid->ssid_len = 0;
717                 }
718 #endif /* CONFIG_P2P */
719         }
720
721         return ssid;
722 }
723
724
725 static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
726                              struct wpa_ssid *selected)
727 {
728         struct wpa_ssid *ssid;
729
730         /* Mark all other networks disabled and trigger reassociation */
731         ssid = wpa_s->conf->ssid;
732         while (ssid) {
733                 int was_disabled = ssid->disabled;
734                 ssid->disabled = ssid != selected;
735                 if (was_disabled != ssid->disabled)
736                         wpas_notify_network_enabled_changed(wpa_s, ssid);
737                 ssid = ssid->next;
738         }
739         wpa_s->disconnected = 0;
740         wpa_s->reassociate = 1;
741         wpa_s->scan_runs = 0;
742         wpa_s->wps_success = 0;
743         wpa_s->blacklist_cleared = 0;
744         wpa_supplicant_req_scan(wpa_s, 0, 0);
745 }
746
747
748 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
749                        int p2p_group)
750 {
751         struct wpa_ssid *ssid;
752         wpas_clear_wps(wpa_s);
753         ssid = wpas_wps_add_network(wpa_s, 0, bssid);
754         if (ssid == NULL)
755                 return -1;
756         ssid->temporary = 1;
757         ssid->p2p_group = p2p_group;
758 #ifdef CONFIG_P2P
759         if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
760                 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
761                 if (ssid->ssid) {
762                         ssid->ssid_len = wpa_s->go_params->ssid_len;
763                         os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
764                                   ssid->ssid_len);
765                         wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
766                                           "SSID", ssid->ssid, ssid->ssid_len);
767                 }
768         }
769 #endif /* CONFIG_P2P */
770         wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
771         if (wpa_s->wps_fragment_size)
772                 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
773         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
774                                wpa_s, NULL);
775         wpas_wps_reassoc(wpa_s, ssid);
776         return 0;
777 }
778
779
780 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
781                        const char *pin, int p2p_group, u16 dev_pw_id)
782 {
783         struct wpa_ssid *ssid;
784         char val[128];
785         unsigned int rpin = 0;
786
787         wpas_clear_wps(wpa_s);
788         ssid = wpas_wps_add_network(wpa_s, 0, bssid);
789         if (ssid == NULL)
790                 return -1;
791         ssid->temporary = 1;
792         ssid->p2p_group = p2p_group;
793 #ifdef CONFIG_P2P
794         if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
795                 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
796                 if (ssid->ssid) {
797                         ssid->ssid_len = wpa_s->go_params->ssid_len;
798                         os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
799                                   ssid->ssid_len);
800                         wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
801                                           "SSID", ssid->ssid, ssid->ssid_len);
802                 }
803         }
804 #endif /* CONFIG_P2P */
805         if (pin)
806                 os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u\"",
807                             pin, dev_pw_id);
808         else {
809                 rpin = wps_generate_pin();
810                 os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u\"",
811                             rpin, dev_pw_id);
812         }
813         wpa_config_set(ssid, "phase1", val, 0);
814         if (wpa_s->wps_fragment_size)
815                 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
816         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
817                                wpa_s, NULL);
818         wpas_wps_reassoc(wpa_s, ssid);
819         return rpin;
820 }
821
822
823 /* Cancel the wps pbc/pin requests */
824 int wpas_wps_cancel(struct wpa_supplicant *wpa_s)
825 {
826 #ifdef CONFIG_AP
827         if (wpa_s->ap_iface) {
828                 wpa_printf(MSG_DEBUG, "WPS: Cancelling in AP mode");
829                 return wpa_supplicant_ap_wps_cancel(wpa_s);
830         }
831 #endif /* CONFIG_AP */
832
833         if (wpa_s->wpa_state == WPA_SCANNING) {
834                 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - cancel scan");
835                 wpa_supplicant_cancel_scan(wpa_s);
836                 wpas_clear_wps(wpa_s);
837         } else if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
838                 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - "
839                            "deauthenticate");
840                 wpa_supplicant_deauthenticate(wpa_s,
841                                               WLAN_REASON_DEAUTH_LEAVING);
842                 wpas_clear_wps(wpa_s);
843         }
844
845         return 0;
846 }
847
848
849 #ifdef CONFIG_WPS_OOB
850 int wpas_wps_start_oob(struct wpa_supplicant *wpa_s, char *device_type,
851                        char *path, char *method, char *name)
852 {
853         struct wps_context *wps = wpa_s->wps;
854         struct oob_device_data *oob_dev;
855
856         oob_dev = wps_get_oob_device(device_type);
857         if (oob_dev == NULL)
858                 return -1;
859         oob_dev->device_path = path;
860         oob_dev->device_name = name;
861         wps->oob_conf.oob_method = wps_get_oob_method(method);
862
863         if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E) {
864                 /*
865                  * Use pre-configured DH keys in order to be able to write the
866                  * key hash into the OOB file.
867                  */
868                 wpabuf_free(wps->dh_pubkey);
869                 wpabuf_free(wps->dh_privkey);
870                 wps->dh_privkey = NULL;
871                 wps->dh_pubkey = NULL;
872                 dh5_free(wps->dh_ctx);
873                 wps->dh_ctx = dh5_init(&wps->dh_privkey, &wps->dh_pubkey);
874                 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
875                 if (wps->dh_ctx == NULL || wps->dh_pubkey == NULL) {
876                         wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
877                                    "Diffie-Hellman handshake");
878                         return -1;
879                 }
880         }
881
882         if (wps->oob_conf.oob_method == OOB_METHOD_CRED)
883                 wpas_clear_wps(wpa_s);
884
885         if (wps_process_oob(wps, oob_dev, 0) < 0)
886                 return -1;
887
888         if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
889              wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
890             wpas_wps_start_pin(wpa_s, NULL,
891                                wpabuf_head(wps->oob_conf.dev_password), 0,
892                                DEV_PW_DEFAULT) < 0)
893                         return -1;
894
895         return 0;
896 }
897 #endif /* CONFIG_WPS_OOB */
898
899
900 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
901                        const char *pin, struct wps_new_ap_settings *settings)
902 {
903         struct wpa_ssid *ssid;
904         char val[200];
905         char *pos, *end;
906         int res;
907
908         if (!pin)
909                 return -1;
910         wpas_clear_wps(wpa_s);
911         ssid = wpas_wps_add_network(wpa_s, 1, bssid);
912         if (ssid == NULL)
913                 return -1;
914         ssid->temporary = 1;
915         pos = val;
916         end = pos + sizeof(val);
917         res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
918         if (res < 0 || res >= end - pos)
919                 return -1;
920         pos += res;
921         if (settings) {
922                 res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
923                                   "new_encr=%s new_key=%s",
924                                   settings->ssid_hex, settings->auth,
925                                   settings->encr, settings->key_hex);
926                 if (res < 0 || res >= end - pos)
927                         return -1;
928                 pos += res;
929         }
930         res = os_snprintf(pos, end - pos, "\"");
931         if (res < 0 || res >= end - pos)
932                 return -1;
933         wpa_config_set(ssid, "phase1", val, 0);
934         if (wpa_s->wps_fragment_size)
935                 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
936         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
937                                wpa_s, NULL);
938         wpas_wps_reassoc(wpa_s, ssid);
939         return 0;
940 }
941
942
943 static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
944                                size_t psk_len)
945 {
946         wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
947                    "STA " MACSTR, MAC2STR(mac_addr));
948         wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
949
950         /* TODO */
951
952         return 0;
953 }
954
955
956 static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
957                                    const struct wps_device_data *dev)
958 {
959         char uuid[40], txt[400];
960         int len;
961         char devtype[WPS_DEV_TYPE_BUFSIZE];
962         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
963                 return;
964         wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
965         len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
966                           " [%s|%s|%s|%s|%s|%s]",
967                           uuid, MAC2STR(dev->mac_addr), dev->device_name,
968                           dev->manufacturer, dev->model_name,
969                           dev->model_number, dev->serial_number,
970                           wps_dev_type_bin2str(dev->pri_dev_type, devtype,
971                                                sizeof(devtype)));
972         if (len > 0 && len < (int) sizeof(txt))
973                 wpa_printf(MSG_INFO, "%s", txt);
974 }
975
976
977 static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
978                                     u16 sel_reg_config_methods)
979 {
980 #ifdef CONFIG_WPS_ER
981         struct wpa_supplicant *wpa_s = ctx;
982
983         if (wpa_s->wps_er == NULL)
984                 return;
985         wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
986                    "dev_password_id=%u sel_reg_config_methods=0x%x",
987                    sel_reg, dev_passwd_id, sel_reg_config_methods);
988         wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
989                            sel_reg_config_methods);
990 #endif /* CONFIG_WPS_ER */
991 }
992
993
994 static u16 wps_fix_config_methods(u16 config_methods)
995 {
996 #ifdef CONFIG_WPS2
997         if ((config_methods &
998              (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
999               WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
1000                 wpa_printf(MSG_INFO, "WPS: Converting display to "
1001                            "virtual_display for WPS 2.0 compliance");
1002                 config_methods |= WPS_CONFIG_VIRT_DISPLAY;
1003         }
1004         if ((config_methods &
1005              (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
1006               WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
1007                 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
1008                            "virtual_push_button for WPS 2.0 compliance");
1009                 config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
1010         }
1011 #endif /* CONFIG_WPS2 */
1012
1013         return config_methods;
1014 }
1015
1016
1017 int wpas_wps_init(struct wpa_supplicant *wpa_s)
1018 {
1019         struct wps_context *wps;
1020         struct wps_registrar_config rcfg;
1021
1022         wps = os_zalloc(sizeof(*wps));
1023         if (wps == NULL)
1024                 return -1;
1025
1026         wps->cred_cb = wpa_supplicant_wps_cred;
1027         wps->event_cb = wpa_supplicant_wps_event;
1028         wps->cb_ctx = wpa_s;
1029
1030         wps->dev.device_name = wpa_s->conf->device_name;
1031         wps->dev.manufacturer = wpa_s->conf->manufacturer;
1032         wps->dev.model_name = wpa_s->conf->model_name;
1033         wps->dev.model_number = wpa_s->conf->model_number;
1034         wps->dev.serial_number = wpa_s->conf->serial_number;
1035         wps->config_methods =
1036                 wps_config_methods_str2bin(wpa_s->conf->config_methods);
1037         if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1038             (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1039                 wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
1040                            "methods are not allowed at the same time");
1041                 os_free(wps);
1042                 return -1;
1043         }
1044         wps->config_methods = wps_fix_config_methods(wps->config_methods);
1045         if (wpa_s->conf->device_type &&
1046             wps_dev_type_str2bin(wpa_s->conf->device_type,
1047                                  wps->dev.pri_dev_type) < 0) {
1048                 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
1049                 os_free(wps);
1050                 return -1;
1051         }
1052         wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
1053         wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
1054         os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
1055         if (is_nil_uuid(wpa_s->conf->uuid)) {
1056                 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
1057                 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
1058                             wps->uuid, WPS_UUID_LEN);
1059         } else
1060                 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
1061
1062         wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
1063         wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
1064
1065         os_memset(&rcfg, 0, sizeof(rcfg));
1066         rcfg.new_psk_cb = wpas_wps_new_psk_cb;
1067         rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
1068         rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
1069         rcfg.cb_ctx = wpa_s;
1070
1071         wps->registrar = wps_registrar_init(wps, &rcfg);
1072         if (wps->registrar == NULL) {
1073                 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
1074                 os_free(wps);
1075                 return -1;
1076         }
1077
1078         wpa_s->wps = wps;
1079
1080         return 0;
1081 }
1082
1083
1084 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
1085 {
1086         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
1087
1088         if (wpa_s->wps == NULL)
1089                 return;
1090
1091 #ifdef CONFIG_WPS_ER
1092         wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1093         wpa_s->wps_er = NULL;
1094 #endif /* CONFIG_WPS_ER */
1095
1096         wps_registrar_deinit(wpa_s->wps->registrar);
1097         wpabuf_free(wpa_s->wps->dh_pubkey);
1098         wpabuf_free(wpa_s->wps->dh_privkey);
1099         wpabuf_free(wpa_s->wps->oob_conf.pubkey_hash);
1100         wpabuf_free(wpa_s->wps->oob_conf.dev_password);
1101         os_free(wpa_s->wps->network_key);
1102         os_free(wpa_s->wps);
1103         wpa_s->wps = NULL;
1104 }
1105
1106
1107 int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
1108                             struct wpa_ssid *ssid, struct wpa_scan_res *bss)
1109 {
1110         struct wpabuf *wps_ie;
1111
1112         if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
1113                 return -1;
1114
1115         wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1116         if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1117                 if (!wps_ie) {
1118                         wpa_printf(MSG_DEBUG, "   skip - non-WPS AP");
1119                         return 0;
1120                 }
1121
1122                 if (!wps_is_selected_pbc_registrar(wps_ie)) {
1123                         wpa_printf(MSG_DEBUG, "   skip - WPS AP "
1124                                    "without active PBC Registrar");
1125                         wpabuf_free(wps_ie);
1126                         return 0;
1127                 }
1128
1129                 /* TODO: overlap detection */
1130                 wpa_printf(MSG_DEBUG, "   selected based on WPS IE "
1131                            "(Active PBC)");
1132                 wpabuf_free(wps_ie);
1133                 return 1;
1134         }
1135
1136         if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1137                 if (!wps_ie) {
1138                         wpa_printf(MSG_DEBUG, "   skip - non-WPS AP");
1139                         return 0;
1140                 }
1141
1142                 /*
1143                  * Start with WPS APs that advertise our address as an
1144                  * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
1145                  * allow any WPS AP after couple of scans since some APs do not
1146                  * set Selected Registrar attribute properly when using
1147                  * external Registrar.
1148                  */
1149                 if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
1150                         if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
1151                                 wpa_printf(MSG_DEBUG, "   skip - WPS AP "
1152                                            "without active PIN Registrar");
1153                                 wpabuf_free(wps_ie);
1154                                 return 0;
1155                         }
1156                         wpa_printf(MSG_DEBUG, "   selected based on WPS IE");
1157                 } else {
1158                         wpa_printf(MSG_DEBUG, "   selected based on WPS IE "
1159                                    "(Authorized MAC or Active PIN)");
1160                 }
1161                 wpabuf_free(wps_ie);
1162                 return 1;
1163         }
1164
1165         if (wps_ie) {
1166                 wpa_printf(MSG_DEBUG, "   selected based on WPS IE");
1167                 wpabuf_free(wps_ie);
1168                 return 1;
1169         }
1170
1171         return -1;
1172 }
1173
1174
1175 int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
1176                               struct wpa_ssid *ssid,
1177                               struct wpa_scan_res *bss)
1178 {
1179         struct wpabuf *wps_ie = NULL;
1180         int ret = 0;
1181
1182         if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1183                 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1184                 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
1185                         /* allow wildcard SSID for WPS PBC */
1186                         ret = 1;
1187                 }
1188         } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1189                 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1190                 if (wps_ie &&
1191                     (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
1192                      wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
1193                         /* allow wildcard SSID for WPS PIN */
1194                         ret = 1;
1195                 }
1196         }
1197
1198         if (!ret && ssid->bssid_set &&
1199             os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
1200                 /* allow wildcard SSID due to hardcoded BSSID match */
1201                 ret = 1;
1202         }
1203
1204 #ifdef CONFIG_WPS_STRICT
1205         if (wps_ie) {
1206                 if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
1207                                                    0, bss->bssid) < 0)
1208                         ret = 0;
1209                 if (bss->beacon_ie_len) {
1210                         struct wpabuf *bcn_wps;
1211                         bcn_wps = wpa_scan_get_vendor_ie_multi_beacon(
1212                                 bss, WPS_IE_VENDOR_TYPE);
1213                         if (bcn_wps == NULL) {
1214                                 wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
1215                                            "missing from AP Beacon");
1216                                 ret = 0;
1217                         } else {
1218                                 if (wps_validate_beacon(wps_ie) < 0)
1219                                         ret = 0;
1220                                 wpabuf_free(bcn_wps);
1221                         }
1222                 }
1223         }
1224 #endif /* CONFIG_WPS_STRICT */
1225
1226         wpabuf_free(wps_ie);
1227
1228         return ret;
1229 }
1230
1231
1232 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
1233                               struct wpa_bss *selected, struct wpa_ssid *ssid)
1234 {
1235         const u8 *sel_uuid, *uuid;
1236         struct wpabuf *wps_ie;
1237         int ret = 0;
1238         struct wpa_bss *bss;
1239
1240         if (!eap_is_wps_pbc_enrollee(&ssid->eap))
1241                 return 0;
1242
1243         wpa_printf(MSG_DEBUG, "WPS: Check whether PBC session overlap is "
1244                    "present in scan results; selected BSSID " MACSTR,
1245                    MAC2STR(selected->bssid));
1246
1247         /* Make sure that only one AP is in active PBC mode */
1248         wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
1249         if (wps_ie) {
1250                 sel_uuid = wps_get_uuid_e(wps_ie);
1251                 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the selected BSS",
1252                             sel_uuid, UUID_LEN);
1253         } else {
1254                 wpa_printf(MSG_DEBUG, "WPS: Selected BSS does not include "
1255                            "WPS IE?!");
1256                 sel_uuid = NULL;
1257         }
1258
1259         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1260                 struct wpabuf *ie;
1261                 if (bss == selected)
1262                         continue;
1263                 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1264                 if (!ie)
1265                         continue;
1266                 if (!wps_is_selected_pbc_registrar(ie)) {
1267                         wpabuf_free(ie);
1268                         continue;
1269                 }
1270                 wpa_printf(MSG_DEBUG, "WPS: Another BSS in active PBC mode: "
1271                            MACSTR, MAC2STR(bss->bssid));
1272                 uuid = wps_get_uuid_e(ie);
1273                 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the other BSS",
1274                             uuid, UUID_LEN);
1275                 if (sel_uuid == NULL || uuid == NULL ||
1276                     os_memcmp(sel_uuid, uuid, UUID_LEN) != 0) {
1277                         ret = 1; /* PBC overlap */
1278                         wpa_msg(wpa_s, MSG_INFO, "WPS: PBC overlap detected: "
1279                                 MACSTR " and " MACSTR,
1280                                 MAC2STR(selected->bssid),
1281                                 MAC2STR(bss->bssid));
1282                         wpabuf_free(ie);
1283                         break;
1284                 }
1285
1286                 /* TODO: verify that this is reasonable dual-band situation */
1287
1288                 wpabuf_free(ie);
1289         }
1290
1291         wpabuf_free(wps_ie);
1292
1293         return ret;
1294 }
1295
1296
1297 void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
1298 {
1299         struct wpa_bss *bss;
1300
1301         if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
1302                 return;
1303
1304         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1305                 struct wpabuf *ie;
1306                 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1307                 if (!ie)
1308                         continue;
1309                 if (wps_is_selected_pbc_registrar(ie))
1310                         wpa_msg_ctrl(wpa_s, MSG_INFO,
1311                                      WPS_EVENT_AP_AVAILABLE_PBC);
1312                 else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
1313                         wpa_msg_ctrl(wpa_s, MSG_INFO,
1314                                      WPS_EVENT_AP_AVAILABLE_AUTH);
1315                 else if (wps_is_selected_pin_registrar(ie))
1316                         wpa_msg_ctrl(wpa_s, MSG_INFO,
1317                                      WPS_EVENT_AP_AVAILABLE_PIN);
1318                 else
1319                         wpa_msg_ctrl(wpa_s, MSG_INFO,
1320                                      WPS_EVENT_AP_AVAILABLE);
1321                 wpabuf_free(ie);
1322                 break;
1323         }
1324 }
1325
1326
1327 int wpas_wps_searching(struct wpa_supplicant *wpa_s)
1328 {
1329         struct wpa_ssid *ssid;
1330
1331         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1332                 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
1333                         return 1;
1334         }
1335
1336         return 0;
1337 }
1338
1339
1340 int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
1341                               char *end)
1342 {
1343         struct wpabuf *wps_ie;
1344         int ret;
1345
1346         wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
1347         if (wps_ie == NULL)
1348                 return 0;
1349
1350         ret = wps_attr_text(wps_ie, buf, end);
1351         wpabuf_free(wps_ie);
1352         return ret;
1353 }
1354
1355
1356 int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
1357 {
1358 #ifdef CONFIG_WPS_ER
1359         if (wpa_s->wps_er) {
1360                 wps_er_refresh(wpa_s->wps_er);
1361                 return 0;
1362         }
1363         wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
1364         if (wpa_s->wps_er == NULL)
1365                 return -1;
1366         return 0;
1367 #else /* CONFIG_WPS_ER */
1368         return 0;
1369 #endif /* CONFIG_WPS_ER */
1370 }
1371
1372
1373 int wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
1374 {
1375 #ifdef CONFIG_WPS_ER
1376         wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1377         wpa_s->wps_er = NULL;
1378 #endif /* CONFIG_WPS_ER */
1379         return 0;
1380 }
1381
1382
1383 #ifdef CONFIG_WPS_ER
1384 int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
1385                         const char *uuid, const char *pin)
1386 {
1387         u8 u[UUID_LEN];
1388         int any = 0;
1389
1390         if (os_strcmp(uuid, "any") == 0)
1391                 any = 1;
1392         else if (uuid_str2bin(uuid, u))
1393                 return -1;
1394         return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
1395                                      any ? NULL : u,
1396                                      (const u8 *) pin, os_strlen(pin), 300);
1397 }
1398
1399
1400 int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
1401 {
1402         u8 u[UUID_LEN];
1403
1404         if (uuid_str2bin(uuid, u))
1405                 return -1;
1406         return wps_er_pbc(wpa_s->wps_er, u);
1407 }
1408
1409
1410 int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
1411                       const char *pin)
1412 {
1413         u8 u[UUID_LEN];
1414
1415         if (uuid_str2bin(uuid, u))
1416                 return -1;
1417         return wps_er_learn(wpa_s->wps_er, u, (const u8 *) pin,
1418                             os_strlen(pin));
1419 }
1420
1421
1422 int wpas_wps_er_set_config(struct wpa_supplicant *wpa_s, const char *uuid,
1423                            int id)
1424 {
1425         u8 u[UUID_LEN];
1426         struct wpa_ssid *ssid;
1427         struct wps_credential cred;
1428
1429         if (uuid_str2bin(uuid, u))
1430                 return -1;
1431         ssid = wpa_config_get_network(wpa_s->conf, id);
1432         if (ssid == NULL || ssid->ssid == NULL)
1433                 return -1;
1434
1435         os_memset(&cred, 0, sizeof(cred));
1436         if (ssid->ssid_len > 32)
1437                 return -1;
1438         os_memcpy(cred.ssid, ssid->ssid, ssid->ssid_len);
1439         cred.ssid_len = ssid->ssid_len;
1440         if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
1441                 cred.auth_type = (ssid->proto & WPA_PROTO_RSN) ?
1442                         WPS_AUTH_WPA2PSK : WPS_AUTH_WPAPSK;
1443                 if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
1444                         cred.encr_type = WPS_ENCR_AES;
1445                 else
1446                         cred.encr_type = WPS_ENCR_TKIP;
1447                 if (ssid->passphrase) {
1448                         cred.key_len = os_strlen(ssid->passphrase);
1449                         if (cred.key_len >= 64)
1450                                 return -1;
1451                         os_memcpy(cred.key, ssid->passphrase, cred.key_len);
1452                 } else if (ssid->psk_set) {
1453                         cred.key_len = 32;
1454                         os_memcpy(cred.key, ssid->psk, 32);
1455                 } else
1456                         return -1;
1457         } else {
1458                 cred.auth_type = WPS_AUTH_OPEN;
1459                 cred.encr_type = WPS_ENCR_NONE;
1460         }
1461         return wps_er_set_config(wpa_s->wps_er, u, &cred);
1462 }
1463
1464
1465 int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
1466                        const char *pin, struct wps_new_ap_settings *settings)
1467 {
1468         u8 u[UUID_LEN];
1469         struct wps_credential cred;
1470         size_t len;
1471
1472         if (uuid_str2bin(uuid, u))
1473                 return -1;
1474         if (settings->ssid_hex == NULL || settings->auth == NULL ||
1475             settings->encr == NULL || settings->key_hex == NULL)
1476                 return -1;
1477
1478         os_memset(&cred, 0, sizeof(cred));
1479         len = os_strlen(settings->ssid_hex);
1480         if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1481             hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
1482                 return -1;
1483         cred.ssid_len = len / 2;
1484
1485         len = os_strlen(settings->key_hex);
1486         if ((len & 1) || len > 2 * sizeof(cred.key) ||
1487             hexstr2bin(settings->key_hex, cred.key, len / 2))
1488                 return -1;
1489         cred.key_len = len / 2;
1490
1491         if (os_strcmp(settings->auth, "OPEN") == 0)
1492                 cred.auth_type = WPS_AUTH_OPEN;
1493         else if (os_strcmp(settings->auth, "WPAPSK") == 0)
1494                 cred.auth_type = WPS_AUTH_WPAPSK;
1495         else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
1496                 cred.auth_type = WPS_AUTH_WPA2PSK;
1497         else
1498                 return -1;
1499
1500         if (os_strcmp(settings->encr, "NONE") == 0)
1501                 cred.encr_type = WPS_ENCR_NONE;
1502         else if (os_strcmp(settings->encr, "WEP") == 0)
1503                 cred.encr_type = WPS_ENCR_WEP;
1504         else if (os_strcmp(settings->encr, "TKIP") == 0)
1505                 cred.encr_type = WPS_ENCR_TKIP;
1506         else if (os_strcmp(settings->encr, "CCMP") == 0)
1507                 cred.encr_type = WPS_ENCR_AES;
1508         else
1509                 return -1;
1510
1511         return wps_er_config(wpa_s->wps_er, u, (const u8 *) pin,
1512                              os_strlen(pin), &cred);
1513 }
1514
1515
1516 static void wpas_wps_terminate_cb(void *ctx)
1517 {
1518         wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
1519         eloop_terminate();
1520 }
1521 #endif /* CONFIG_WPS_ER */
1522
1523
1524 int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
1525 {
1526 #ifdef CONFIG_WPS_ER
1527         if (wpa_s->wps_er) {
1528                 wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
1529                 wpa_s->wps_er = NULL;
1530                 return 1;
1531         }
1532 #endif /* CONFIG_WPS_ER */
1533         return 0;
1534 }
1535
1536
1537 int wpas_wps_in_progress(struct wpa_supplicant *wpa_s)
1538 {
1539         struct wpa_ssid *ssid;
1540
1541         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1542                 if (!ssid->disabled && ssid->key_mgmt == WPA_KEY_MGMT_WPS)
1543                         return 1;
1544         }
1545
1546         return 0;
1547 }
1548
1549
1550 void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
1551 {
1552         struct wps_context *wps = wpa_s->wps;
1553
1554         if (wps == NULL)
1555                 return;
1556
1557         if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
1558                 wps->config_methods = wps_config_methods_str2bin(
1559                         wpa_s->conf->config_methods);
1560                 if ((wps->config_methods &
1561                      (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1562                     (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1563                         wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
1564                                    "config methods are not allowed at the "
1565                                    "same time");
1566                         wps->config_methods &= ~WPS_CONFIG_LABEL;
1567                 }
1568         }
1569         wps->config_methods = wps_fix_config_methods(wps->config_methods);
1570
1571         if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE) {
1572                 if (wpa_s->conf->device_type &&
1573                     wps_dev_type_str2bin(wpa_s->conf->device_type,
1574                                          wps->dev.pri_dev_type) < 0)
1575                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
1576         }
1577
1578         if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
1579                 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
1580
1581         if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID) {
1582                 if (is_nil_uuid(wpa_s->conf->uuid)) {
1583                         uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
1584                         wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
1585                                     "address", wps->uuid, WPS_UUID_LEN);
1586                 } else
1587                         os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
1588         }
1589
1590         if (wpa_s->conf->changed_parameters &
1591             (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
1592                 /* Update pointers to make sure they refer current values */
1593                 wps->dev.device_name = wpa_s->conf->device_name;
1594                 wps->dev.manufacturer = wpa_s->conf->manufacturer;
1595                 wps->dev.model_name = wpa_s->conf->model_name;
1596                 wps->dev.model_number = wpa_s->conf->model_number;
1597                 wps->dev.serial_number = wpa_s->conf->serial_number;
1598         }
1599 }