WPS: Add mechanism for indicating non-standard WPS errors
[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 const char * wps_event_fail_reason[NUM_WPS_EI_VALUES] = {
414         "No Error", /* WPS_EI_NO_ERROR */
415         "TKIP Only Prohibited", /* WPS_EI_SECURITY_TKIP_ONLY_PROHIBITED */
416         "WEP Prohibited" /* WPS_EI_SECURITY_WEP_PROHIBITED */
417 };
418
419 static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
420                                           struct wps_event_fail *fail)
421 {
422         if (fail->error_indication > 0 &&
423             fail->error_indication < NUM_WPS_EI_VALUES) {
424                 wpa_msg(wpa_s, MSG_INFO,
425                         WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
426                         fail->msg, fail->config_error, fail->error_indication,
427                         wps_event_fail_reason[fail->error_indication]);
428                 if (wpa_s->parent && wpa_s->parent != wpa_s)
429                         wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
430                                 "msg=%d config_error=%d reason=%d (%s)",
431                                 fail->msg, fail->config_error,
432                                 fail->error_indication,
433                                 wps_event_fail_reason[fail->error_indication]);
434         } else {
435                 wpa_msg(wpa_s, MSG_INFO,
436                         WPS_EVENT_FAIL "msg=%d config_error=%d",
437                         fail->msg, fail->config_error);
438                 if (wpa_s->parent && wpa_s->parent != wpa_s)
439                         wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
440                                 "msg=%d config_error=%d",
441                                 fail->msg, fail->config_error);
442         }
443         wpas_clear_wps(wpa_s);
444         wpas_notify_wps_event_fail(wpa_s, fail);
445 }
446
447
448 static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
449 {
450         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
451         wpa_s->wps_success = 1;
452         wpas_notify_wps_event_success(wpa_s);
453 #ifdef CONFIG_P2P
454         wpas_p2p_wps_success(wpa_s, wpa_s->bssid, 0);
455 #endif /* CONFIG_P2P */
456 }
457
458
459 static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
460                                                struct wps_event_er_ap *ap)
461 {
462         char uuid_str[100];
463         char dev_type[WPS_DEV_TYPE_BUFSIZE];
464
465         uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
466         if (ap->pri_dev_type)
467                 wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
468                                      sizeof(dev_type));
469         else
470                 dev_type[0] = '\0';
471
472         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
473                 " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
474                 uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
475                 ap->friendly_name ? ap->friendly_name : "",
476                 ap->manufacturer ? ap->manufacturer : "",
477                 ap->model_description ? ap->model_description : "",
478                 ap->model_name ? ap->model_name : "",
479                 ap->manufacturer_url ? ap->manufacturer_url : "",
480                 ap->model_url ? ap->model_url : "");
481 }
482
483
484 static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
485                                                   struct wps_event_er_ap *ap)
486 {
487         char uuid_str[100];
488         uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
489         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
490 }
491
492
493 static void wpa_supplicant_wps_event_er_enrollee_add(
494         struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
495 {
496         char uuid_str[100];
497         char dev_type[WPS_DEV_TYPE_BUFSIZE];
498
499         uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
500         if (enrollee->pri_dev_type)
501                 wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
502                                      sizeof(dev_type));
503         else
504                 dev_type[0] = '\0';
505
506         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
507                 " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
508                 "|%s|%s|%s|%s|%s|",
509                 uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
510                 enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
511                 enrollee->dev_name ? enrollee->dev_name : "",
512                 enrollee->manufacturer ? enrollee->manufacturer : "",
513                 enrollee->model_name ? enrollee->model_name : "",
514                 enrollee->model_number ? enrollee->model_number : "",
515                 enrollee->serial_number ? enrollee->serial_number : "");
516 }
517
518
519 static void wpa_supplicant_wps_event_er_enrollee_remove(
520         struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
521 {
522         char uuid_str[100];
523         uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
524         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
525                 uuid_str, MAC2STR(enrollee->mac_addr));
526 }
527
528
529 static void wpa_supplicant_wps_event_er_ap_settings(
530         struct wpa_supplicant *wpa_s,
531         struct wps_event_er_ap_settings *ap_settings)
532 {
533         char uuid_str[100];
534         char key_str[65];
535         const struct wps_credential *cred = ap_settings->cred;
536
537         key_str[0] = '\0';
538         if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
539                 if (cred->key_len >= 8 && cred->key_len <= 64) {
540                         os_memcpy(key_str, cred->key, cred->key_len);
541                         key_str[cred->key_len] = '\0';
542                 }
543         }
544
545         uuid_bin2str(ap_settings->uuid, uuid_str, sizeof(uuid_str));
546         /* Use wpa_msg_ctrl to avoid showing the key in debug log */
547         wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_SETTINGS
548                      "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
549                      "key=%s",
550                      uuid_str, wpa_ssid_txt(cred->ssid, cred->ssid_len),
551                      cred->auth_type, cred->encr_type, key_str);
552 }
553
554
555 static void wpa_supplicant_wps_event_er_set_sel_reg(
556         struct wpa_supplicant *wpa_s,
557         struct wps_event_er_set_selected_registrar *ev)
558 {
559         char uuid_str[100];
560
561         uuid_bin2str(ev->uuid, uuid_str, sizeof(uuid_str));
562         switch (ev->state) {
563         case WPS_ER_SET_SEL_REG_START:
564                 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
565                         "uuid=%s state=START sel_reg=%d dev_passwd_id=%u "
566                         "sel_reg_config_methods=0x%x",
567                         uuid_str, ev->sel_reg, ev->dev_passwd_id,
568                         ev->sel_reg_config_methods);
569                 break;
570         case WPS_ER_SET_SEL_REG_DONE:
571                 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
572                         "uuid=%s state=DONE", uuid_str);
573                 break;
574         case WPS_ER_SET_SEL_REG_FAILED:
575                 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_SET_SEL_REG
576                         "uuid=%s state=FAILED", uuid_str);
577                 break;
578         }
579 }
580
581
582 static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
583                                      union wps_event_data *data)
584 {
585         struct wpa_supplicant *wpa_s = ctx;
586         switch (event) {
587         case WPS_EV_M2D:
588                 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
589                 break;
590         case WPS_EV_FAIL:
591                 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
592                 break;
593         case WPS_EV_SUCCESS:
594                 wpa_supplicant_wps_event_success(wpa_s);
595                 break;
596         case WPS_EV_PWD_AUTH_FAIL:
597 #ifdef CONFIG_AP
598                 if (wpa_s->ap_iface && data->pwd_auth_fail.enrollee)
599                         wpa_supplicant_ap_pwd_auth_fail(wpa_s);
600 #endif /* CONFIG_AP */
601                 break;
602         case WPS_EV_PBC_OVERLAP:
603                 break;
604         case WPS_EV_PBC_TIMEOUT:
605                 break;
606         case WPS_EV_ER_AP_ADD:
607                 wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
608                 break;
609         case WPS_EV_ER_AP_REMOVE:
610                 wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
611                 break;
612         case WPS_EV_ER_ENROLLEE_ADD:
613                 wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
614                                                          &data->enrollee);
615                 break;
616         case WPS_EV_ER_ENROLLEE_REMOVE:
617                 wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
618                                                             &data->enrollee);
619                 break;
620         case WPS_EV_ER_AP_SETTINGS:
621                 wpa_supplicant_wps_event_er_ap_settings(wpa_s,
622                                                         &data->ap_settings);
623                 break;
624         case WPS_EV_ER_SET_SELECTED_REGISTRAR:
625                 wpa_supplicant_wps_event_er_set_sel_reg(wpa_s,
626                                                         &data->set_sel_reg);
627                 break;
628         }
629 }
630
631
632 enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
633 {
634         if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
635             eap_is_wps_pin_enrollee(&ssid->eap))
636                 return WPS_REQ_ENROLLEE;
637         else
638                 return WPS_REQ_REGISTRAR;
639 }
640
641
642 static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
643 {
644         int id;
645         struct wpa_ssid *ssid, *remove_ssid = NULL;
646
647         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
648
649         /* Remove any existing WPS network from configuration */
650         ssid = wpa_s->conf->ssid;
651         while (ssid) {
652                 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
653                         if (ssid == wpa_s->current_ssid) {
654                                 wpa_s->current_ssid = NULL;
655                                 if (ssid != NULL)
656                                         wpas_notify_network_changed(wpa_s);
657                         }
658                         id = ssid->id;
659                         remove_ssid = ssid;
660                 } else
661                         id = -1;
662                 ssid = ssid->next;
663                 if (id >= 0) {
664                         wpas_notify_network_removed(wpa_s, remove_ssid);
665                         wpa_config_remove_network(wpa_s->conf, id);
666                 }
667         }
668 }
669
670
671 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
672 {
673         struct wpa_supplicant *wpa_s = eloop_ctx;
674         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
675                 "out");
676         wpas_clear_wps(wpa_s);
677 }
678
679
680 static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
681                                               int registrar, const u8 *bssid)
682 {
683         struct wpa_ssid *ssid;
684
685         ssid = wpa_config_add_network(wpa_s->conf);
686         if (ssid == NULL)
687                 return NULL;
688         wpas_notify_network_added(wpa_s, ssid);
689         wpa_config_set_network_defaults(ssid);
690         ssid->temporary = 1;
691         if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
692             wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
693             wpa_config_set(ssid, "identity", registrar ?
694                            "\"" WSC_ID_REGISTRAR "\"" :
695                            "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
696                 wpas_notify_network_removed(wpa_s, ssid);
697                 wpa_config_remove_network(wpa_s->conf, ssid->id);
698                 return NULL;
699         }
700
701         if (bssid) {
702 #ifndef CONFIG_P2P
703                 struct wpa_bss *bss;
704                 int count = 0;
705 #endif /* CONFIG_P2P */
706
707                 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
708                 ssid->bssid_set = 1;
709
710                 /*
711                  * Note: With P2P, the SSID may change at the time the WPS
712                  * provisioning is started, so better not filter the AP based
713                  * on the current SSID in the scan results.
714                  */
715 #ifndef CONFIG_P2P
716                 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
717                         if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
718                                 continue;
719
720                         os_free(ssid->ssid);
721                         ssid->ssid = os_malloc(bss->ssid_len);
722                         if (ssid->ssid == NULL)
723                                 break;
724                         os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
725                         ssid->ssid_len = bss->ssid_len;
726                         wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
727                                           "scan results",
728                                           ssid->ssid, ssid->ssid_len);
729                         count++;
730                 }
731
732                 if (count > 1) {
733                         wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
734                                    "for the AP; use wildcard");
735                         os_free(ssid->ssid);
736                         ssid->ssid = NULL;
737                         ssid->ssid_len = 0;
738                 }
739 #endif /* CONFIG_P2P */
740         }
741
742         return ssid;
743 }
744
745
746 static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
747                              struct wpa_ssid *selected)
748 {
749         struct wpa_ssid *ssid;
750
751         /* Mark all other networks disabled and trigger reassociation */
752         ssid = wpa_s->conf->ssid;
753         while (ssid) {
754                 int was_disabled = ssid->disabled;
755                 ssid->disabled = ssid != selected;
756                 if (was_disabled != ssid->disabled)
757                         wpas_notify_network_enabled_changed(wpa_s, ssid);
758                 ssid = ssid->next;
759         }
760         wpa_s->disconnected = 0;
761         wpa_s->reassociate = 1;
762         wpa_s->scan_runs = 0;
763         wpa_s->wps_success = 0;
764         wpa_s->blacklist_cleared = 0;
765         wpa_supplicant_req_scan(wpa_s, 0, 0);
766 }
767
768
769 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
770                        int p2p_group)
771 {
772         struct wpa_ssid *ssid;
773         wpas_clear_wps(wpa_s);
774         ssid = wpas_wps_add_network(wpa_s, 0, bssid);
775         if (ssid == NULL)
776                 return -1;
777         ssid->temporary = 1;
778         ssid->p2p_group = p2p_group;
779 #ifdef CONFIG_P2P
780         if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
781                 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
782                 if (ssid->ssid) {
783                         ssid->ssid_len = wpa_s->go_params->ssid_len;
784                         os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
785                                   ssid->ssid_len);
786                         wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
787                                           "SSID", ssid->ssid, ssid->ssid_len);
788                 }
789         }
790 #endif /* CONFIG_P2P */
791         wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
792         if (wpa_s->wps_fragment_size)
793                 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
794         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
795                                wpa_s, NULL);
796         wpas_wps_reassoc(wpa_s, ssid);
797         return 0;
798 }
799
800
801 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
802                        const char *pin, int p2p_group, u16 dev_pw_id)
803 {
804         struct wpa_ssid *ssid;
805         char val[128];
806         unsigned int rpin = 0;
807
808         wpas_clear_wps(wpa_s);
809         ssid = wpas_wps_add_network(wpa_s, 0, bssid);
810         if (ssid == NULL)
811                 return -1;
812         ssid->temporary = 1;
813         ssid->p2p_group = p2p_group;
814 #ifdef CONFIG_P2P
815         if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
816                 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
817                 if (ssid->ssid) {
818                         ssid->ssid_len = wpa_s->go_params->ssid_len;
819                         os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
820                                   ssid->ssid_len);
821                         wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
822                                           "SSID", ssid->ssid, ssid->ssid_len);
823                 }
824         }
825 #endif /* CONFIG_P2P */
826         if (pin)
827                 os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u\"",
828                             pin, dev_pw_id);
829         else {
830                 rpin = wps_generate_pin();
831                 os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u\"",
832                             rpin, dev_pw_id);
833         }
834         wpa_config_set(ssid, "phase1", val, 0);
835         if (wpa_s->wps_fragment_size)
836                 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
837         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
838                                wpa_s, NULL);
839         wpas_wps_reassoc(wpa_s, ssid);
840         return rpin;
841 }
842
843
844 /* Cancel the wps pbc/pin requests */
845 int wpas_wps_cancel(struct wpa_supplicant *wpa_s)
846 {
847 #ifdef CONFIG_AP
848         if (wpa_s->ap_iface) {
849                 wpa_printf(MSG_DEBUG, "WPS: Cancelling in AP mode");
850                 return wpa_supplicant_ap_wps_cancel(wpa_s);
851         }
852 #endif /* CONFIG_AP */
853
854         if (wpa_s->wpa_state == WPA_SCANNING) {
855                 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - cancel scan");
856                 wpa_supplicant_cancel_scan(wpa_s);
857                 wpas_clear_wps(wpa_s);
858         } else if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
859                 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - "
860                            "deauthenticate");
861                 wpa_supplicant_deauthenticate(wpa_s,
862                                               WLAN_REASON_DEAUTH_LEAVING);
863                 wpas_clear_wps(wpa_s);
864         }
865
866         return 0;
867 }
868
869
870 #ifdef CONFIG_WPS_OOB
871 int wpas_wps_start_oob(struct wpa_supplicant *wpa_s, char *device_type,
872                        char *path, char *method, char *name)
873 {
874         struct wps_context *wps = wpa_s->wps;
875         struct oob_device_data *oob_dev;
876
877         oob_dev = wps_get_oob_device(device_type);
878         if (oob_dev == NULL)
879                 return -1;
880         oob_dev->device_path = path;
881         oob_dev->device_name = name;
882         wps->oob_conf.oob_method = wps_get_oob_method(method);
883
884         if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E) {
885                 /*
886                  * Use pre-configured DH keys in order to be able to write the
887                  * key hash into the OOB file.
888                  */
889                 wpabuf_free(wps->dh_pubkey);
890                 wpabuf_free(wps->dh_privkey);
891                 wps->dh_privkey = NULL;
892                 wps->dh_pubkey = NULL;
893                 dh5_free(wps->dh_ctx);
894                 wps->dh_ctx = dh5_init(&wps->dh_privkey, &wps->dh_pubkey);
895                 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
896                 if (wps->dh_ctx == NULL || wps->dh_pubkey == NULL) {
897                         wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
898                                    "Diffie-Hellman handshake");
899                         return -1;
900                 }
901         }
902
903         if (wps->oob_conf.oob_method == OOB_METHOD_CRED)
904                 wpas_clear_wps(wpa_s);
905
906         if (wps_process_oob(wps, oob_dev, 0) < 0)
907                 return -1;
908
909         if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
910              wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
911             wpas_wps_start_pin(wpa_s, NULL,
912                                wpabuf_head(wps->oob_conf.dev_password), 0,
913                                DEV_PW_DEFAULT) < 0)
914                         return -1;
915
916         return 0;
917 }
918 #endif /* CONFIG_WPS_OOB */
919
920
921 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
922                        const char *pin, struct wps_new_ap_settings *settings)
923 {
924         struct wpa_ssid *ssid;
925         char val[200];
926         char *pos, *end;
927         int res;
928
929         if (!pin)
930                 return -1;
931         wpas_clear_wps(wpa_s);
932         ssid = wpas_wps_add_network(wpa_s, 1, bssid);
933         if (ssid == NULL)
934                 return -1;
935         ssid->temporary = 1;
936         pos = val;
937         end = pos + sizeof(val);
938         res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
939         if (res < 0 || res >= end - pos)
940                 return -1;
941         pos += res;
942         if (settings) {
943                 res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
944                                   "new_encr=%s new_key=%s",
945                                   settings->ssid_hex, settings->auth,
946                                   settings->encr, settings->key_hex);
947                 if (res < 0 || res >= end - pos)
948                         return -1;
949                 pos += res;
950         }
951         res = os_snprintf(pos, end - pos, "\"");
952         if (res < 0 || res >= end - pos)
953                 return -1;
954         wpa_config_set(ssid, "phase1", val, 0);
955         if (wpa_s->wps_fragment_size)
956                 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
957         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
958                                wpa_s, NULL);
959         wpas_wps_reassoc(wpa_s, ssid);
960         return 0;
961 }
962
963
964 static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
965                                size_t psk_len)
966 {
967         wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
968                    "STA " MACSTR, MAC2STR(mac_addr));
969         wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
970
971         /* TODO */
972
973         return 0;
974 }
975
976
977 static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
978                                    const struct wps_device_data *dev)
979 {
980         char uuid[40], txt[400];
981         int len;
982         char devtype[WPS_DEV_TYPE_BUFSIZE];
983         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
984                 return;
985         wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
986         len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
987                           " [%s|%s|%s|%s|%s|%s]",
988                           uuid, MAC2STR(dev->mac_addr), dev->device_name,
989                           dev->manufacturer, dev->model_name,
990                           dev->model_number, dev->serial_number,
991                           wps_dev_type_bin2str(dev->pri_dev_type, devtype,
992                                                sizeof(devtype)));
993         if (len > 0 && len < (int) sizeof(txt))
994                 wpa_printf(MSG_INFO, "%s", txt);
995 }
996
997
998 static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
999                                     u16 sel_reg_config_methods)
1000 {
1001 #ifdef CONFIG_WPS_ER
1002         struct wpa_supplicant *wpa_s = ctx;
1003
1004         if (wpa_s->wps_er == NULL)
1005                 return;
1006         wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
1007                    "dev_password_id=%u sel_reg_config_methods=0x%x",
1008                    sel_reg, dev_passwd_id, sel_reg_config_methods);
1009         wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
1010                            sel_reg_config_methods);
1011 #endif /* CONFIG_WPS_ER */
1012 }
1013
1014
1015 static u16 wps_fix_config_methods(u16 config_methods)
1016 {
1017 #ifdef CONFIG_WPS2
1018         if ((config_methods &
1019              (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
1020               WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
1021                 wpa_printf(MSG_INFO, "WPS: Converting display to "
1022                            "virtual_display for WPS 2.0 compliance");
1023                 config_methods |= WPS_CONFIG_VIRT_DISPLAY;
1024         }
1025         if ((config_methods &
1026              (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
1027               WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
1028                 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
1029                            "virtual_push_button for WPS 2.0 compliance");
1030                 config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
1031         }
1032 #endif /* CONFIG_WPS2 */
1033
1034         return config_methods;
1035 }
1036
1037
1038 int wpas_wps_init(struct wpa_supplicant *wpa_s)
1039 {
1040         struct wps_context *wps;
1041         struct wps_registrar_config rcfg;
1042
1043         wps = os_zalloc(sizeof(*wps));
1044         if (wps == NULL)
1045                 return -1;
1046
1047         wps->cred_cb = wpa_supplicant_wps_cred;
1048         wps->event_cb = wpa_supplicant_wps_event;
1049         wps->cb_ctx = wpa_s;
1050
1051         wps->dev.device_name = wpa_s->conf->device_name;
1052         wps->dev.manufacturer = wpa_s->conf->manufacturer;
1053         wps->dev.model_name = wpa_s->conf->model_name;
1054         wps->dev.model_number = wpa_s->conf->model_number;
1055         wps->dev.serial_number = wpa_s->conf->serial_number;
1056         wps->config_methods =
1057                 wps_config_methods_str2bin(wpa_s->conf->config_methods);
1058         if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1059             (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1060                 wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
1061                            "methods are not allowed at the same time");
1062                 os_free(wps);
1063                 return -1;
1064         }
1065         wps->config_methods = wps_fix_config_methods(wps->config_methods);
1066         if (wpa_s->conf->device_type &&
1067             wps_dev_type_str2bin(wpa_s->conf->device_type,
1068                                  wps->dev.pri_dev_type) < 0) {
1069                 wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
1070                 os_free(wps);
1071                 return -1;
1072         }
1073         wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
1074         wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
1075         os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
1076         if (is_nil_uuid(wpa_s->conf->uuid)) {
1077                 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
1078                 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
1079                             wps->uuid, WPS_UUID_LEN);
1080         } else
1081                 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
1082
1083         wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
1084         wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
1085
1086         os_memset(&rcfg, 0, sizeof(rcfg));
1087         rcfg.new_psk_cb = wpas_wps_new_psk_cb;
1088         rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
1089         rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
1090         rcfg.cb_ctx = wpa_s;
1091
1092         wps->registrar = wps_registrar_init(wps, &rcfg);
1093         if (wps->registrar == NULL) {
1094                 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
1095                 os_free(wps);
1096                 return -1;
1097         }
1098
1099         wpa_s->wps = wps;
1100
1101         return 0;
1102 }
1103
1104
1105 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
1106 {
1107         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
1108
1109         if (wpa_s->wps == NULL)
1110                 return;
1111
1112 #ifdef CONFIG_WPS_ER
1113         wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1114         wpa_s->wps_er = NULL;
1115 #endif /* CONFIG_WPS_ER */
1116
1117         wps_registrar_deinit(wpa_s->wps->registrar);
1118         wpabuf_free(wpa_s->wps->dh_pubkey);
1119         wpabuf_free(wpa_s->wps->dh_privkey);
1120         wpabuf_free(wpa_s->wps->oob_conf.pubkey_hash);
1121         wpabuf_free(wpa_s->wps->oob_conf.dev_password);
1122         os_free(wpa_s->wps->network_key);
1123         os_free(wpa_s->wps);
1124         wpa_s->wps = NULL;
1125 }
1126
1127
1128 int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
1129                             struct wpa_ssid *ssid, struct wpa_scan_res *bss)
1130 {
1131         struct wpabuf *wps_ie;
1132
1133         if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
1134                 return -1;
1135
1136         wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1137         if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1138                 if (!wps_ie) {
1139                         wpa_printf(MSG_DEBUG, "   skip - non-WPS AP");
1140                         return 0;
1141                 }
1142
1143                 if (!wps_is_selected_pbc_registrar(wps_ie)) {
1144                         wpa_printf(MSG_DEBUG, "   skip - WPS AP "
1145                                    "without active PBC Registrar");
1146                         wpabuf_free(wps_ie);
1147                         return 0;
1148                 }
1149
1150                 /* TODO: overlap detection */
1151                 wpa_printf(MSG_DEBUG, "   selected based on WPS IE "
1152                            "(Active PBC)");
1153                 wpabuf_free(wps_ie);
1154                 return 1;
1155         }
1156
1157         if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1158                 if (!wps_ie) {
1159                         wpa_printf(MSG_DEBUG, "   skip - non-WPS AP");
1160                         return 0;
1161                 }
1162
1163                 /*
1164                  * Start with WPS APs that advertise our address as an
1165                  * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
1166                  * allow any WPS AP after couple of scans since some APs do not
1167                  * set Selected Registrar attribute properly when using
1168                  * external Registrar.
1169                  */
1170                 if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
1171                         if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
1172                                 wpa_printf(MSG_DEBUG, "   skip - WPS AP "
1173                                            "without active PIN Registrar");
1174                                 wpabuf_free(wps_ie);
1175                                 return 0;
1176                         }
1177                         wpa_printf(MSG_DEBUG, "   selected based on WPS IE");
1178                 } else {
1179                         wpa_printf(MSG_DEBUG, "   selected based on WPS IE "
1180                                    "(Authorized MAC or Active PIN)");
1181                 }
1182                 wpabuf_free(wps_ie);
1183                 return 1;
1184         }
1185
1186         if (wps_ie) {
1187                 wpa_printf(MSG_DEBUG, "   selected based on WPS IE");
1188                 wpabuf_free(wps_ie);
1189                 return 1;
1190         }
1191
1192         return -1;
1193 }
1194
1195
1196 int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
1197                               struct wpa_ssid *ssid,
1198                               struct wpa_scan_res *bss)
1199 {
1200         struct wpabuf *wps_ie = NULL;
1201         int ret = 0;
1202
1203         if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1204                 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1205                 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
1206                         /* allow wildcard SSID for WPS PBC */
1207                         ret = 1;
1208                 }
1209         } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1210                 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1211                 if (wps_ie &&
1212                     (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
1213                      wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
1214                         /* allow wildcard SSID for WPS PIN */
1215                         ret = 1;
1216                 }
1217         }
1218
1219         if (!ret && ssid->bssid_set &&
1220             os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
1221                 /* allow wildcard SSID due to hardcoded BSSID match */
1222                 ret = 1;
1223         }
1224
1225 #ifdef CONFIG_WPS_STRICT
1226         if (wps_ie) {
1227                 if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
1228                                                    0, bss->bssid) < 0)
1229                         ret = 0;
1230                 if (bss->beacon_ie_len) {
1231                         struct wpabuf *bcn_wps;
1232                         bcn_wps = wpa_scan_get_vendor_ie_multi_beacon(
1233                                 bss, WPS_IE_VENDOR_TYPE);
1234                         if (bcn_wps == NULL) {
1235                                 wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
1236                                            "missing from AP Beacon");
1237                                 ret = 0;
1238                         } else {
1239                                 if (wps_validate_beacon(wps_ie) < 0)
1240                                         ret = 0;
1241                                 wpabuf_free(bcn_wps);
1242                         }
1243                 }
1244         }
1245 #endif /* CONFIG_WPS_STRICT */
1246
1247         wpabuf_free(wps_ie);
1248
1249         return ret;
1250 }
1251
1252
1253 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
1254                               struct wpa_bss *selected, struct wpa_ssid *ssid)
1255 {
1256         const u8 *sel_uuid, *uuid;
1257         struct wpabuf *wps_ie;
1258         int ret = 0;
1259         struct wpa_bss *bss;
1260
1261         if (!eap_is_wps_pbc_enrollee(&ssid->eap))
1262                 return 0;
1263
1264         wpa_printf(MSG_DEBUG, "WPS: Check whether PBC session overlap is "
1265                    "present in scan results; selected BSSID " MACSTR,
1266                    MAC2STR(selected->bssid));
1267
1268         /* Make sure that only one AP is in active PBC mode */
1269         wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
1270         if (wps_ie) {
1271                 sel_uuid = wps_get_uuid_e(wps_ie);
1272                 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the selected BSS",
1273                             sel_uuid, UUID_LEN);
1274         } else {
1275                 wpa_printf(MSG_DEBUG, "WPS: Selected BSS does not include "
1276                            "WPS IE?!");
1277                 sel_uuid = NULL;
1278         }
1279
1280         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1281                 struct wpabuf *ie;
1282                 if (bss == selected)
1283                         continue;
1284                 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1285                 if (!ie)
1286                         continue;
1287                 if (!wps_is_selected_pbc_registrar(ie)) {
1288                         wpabuf_free(ie);
1289                         continue;
1290                 }
1291                 wpa_printf(MSG_DEBUG, "WPS: Another BSS in active PBC mode: "
1292                            MACSTR, MAC2STR(bss->bssid));
1293                 uuid = wps_get_uuid_e(ie);
1294                 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the other BSS",
1295                             uuid, UUID_LEN);
1296                 if (sel_uuid == NULL || uuid == NULL ||
1297                     os_memcmp(sel_uuid, uuid, UUID_LEN) != 0) {
1298                         ret = 1; /* PBC overlap */
1299                         wpa_msg(wpa_s, MSG_INFO, "WPS: PBC overlap detected: "
1300                                 MACSTR " and " MACSTR,
1301                                 MAC2STR(selected->bssid),
1302                                 MAC2STR(bss->bssid));
1303                         wpabuf_free(ie);
1304                         break;
1305                 }
1306
1307                 /* TODO: verify that this is reasonable dual-band situation */
1308
1309                 wpabuf_free(ie);
1310         }
1311
1312         wpabuf_free(wps_ie);
1313
1314         return ret;
1315 }
1316
1317
1318 void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
1319 {
1320         struct wpa_bss *bss;
1321         unsigned int pbc = 0, auth = 0, pin = 0, wps = 0;
1322
1323         if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
1324                 return;
1325
1326         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1327                 struct wpabuf *ie;
1328                 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1329                 if (!ie)
1330                         continue;
1331                 if (wps_is_selected_pbc_registrar(ie))
1332                         pbc++;
1333                 else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
1334                         auth++;
1335                 else if (wps_is_selected_pin_registrar(ie))
1336                         pin++;
1337                 else
1338                         wps++;
1339                 wpabuf_free(ie);
1340         }
1341
1342         if (pbc)
1343                 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
1344         else if (auth)
1345                 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_AUTH);
1346         else if (pin)
1347                 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
1348         else if (wps)
1349                 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
1350 }
1351
1352
1353 int wpas_wps_searching(struct wpa_supplicant *wpa_s)
1354 {
1355         struct wpa_ssid *ssid;
1356
1357         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1358                 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
1359                         return 1;
1360         }
1361
1362         return 0;
1363 }
1364
1365
1366 int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
1367                               char *end)
1368 {
1369         struct wpabuf *wps_ie;
1370         int ret;
1371
1372         wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
1373         if (wps_ie == NULL)
1374                 return 0;
1375
1376         ret = wps_attr_text(wps_ie, buf, end);
1377         wpabuf_free(wps_ie);
1378         return ret;
1379 }
1380
1381
1382 int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
1383 {
1384 #ifdef CONFIG_WPS_ER
1385         if (wpa_s->wps_er) {
1386                 wps_er_refresh(wpa_s->wps_er);
1387                 return 0;
1388         }
1389         wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
1390         if (wpa_s->wps_er == NULL)
1391                 return -1;
1392         return 0;
1393 #else /* CONFIG_WPS_ER */
1394         return 0;
1395 #endif /* CONFIG_WPS_ER */
1396 }
1397
1398
1399 int wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
1400 {
1401 #ifdef CONFIG_WPS_ER
1402         wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1403         wpa_s->wps_er = NULL;
1404 #endif /* CONFIG_WPS_ER */
1405         return 0;
1406 }
1407
1408
1409 #ifdef CONFIG_WPS_ER
1410 int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
1411                         const char *uuid, const char *pin)
1412 {
1413         u8 u[UUID_LEN];
1414         int any = 0;
1415
1416         if (os_strcmp(uuid, "any") == 0)
1417                 any = 1;
1418         else if (uuid_str2bin(uuid, u))
1419                 return -1;
1420         return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
1421                                      any ? NULL : u,
1422                                      (const u8 *) pin, os_strlen(pin), 300);
1423 }
1424
1425
1426 int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
1427 {
1428         u8 u[UUID_LEN];
1429
1430         if (uuid_str2bin(uuid, u))
1431                 return -1;
1432         return wps_er_pbc(wpa_s->wps_er, u);
1433 }
1434
1435
1436 int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
1437                       const char *pin)
1438 {
1439         u8 u[UUID_LEN];
1440
1441         if (uuid_str2bin(uuid, u))
1442                 return -1;
1443         return wps_er_learn(wpa_s->wps_er, u, (const u8 *) pin,
1444                             os_strlen(pin));
1445 }
1446
1447
1448 int wpas_wps_er_set_config(struct wpa_supplicant *wpa_s, const char *uuid,
1449                            int id)
1450 {
1451         u8 u[UUID_LEN];
1452         struct wpa_ssid *ssid;
1453         struct wps_credential cred;
1454
1455         if (uuid_str2bin(uuid, u))
1456                 return -1;
1457         ssid = wpa_config_get_network(wpa_s->conf, id);
1458         if (ssid == NULL || ssid->ssid == NULL)
1459                 return -1;
1460
1461         os_memset(&cred, 0, sizeof(cred));
1462         if (ssid->ssid_len > 32)
1463                 return -1;
1464         os_memcpy(cred.ssid, ssid->ssid, ssid->ssid_len);
1465         cred.ssid_len = ssid->ssid_len;
1466         if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
1467                 cred.auth_type = (ssid->proto & WPA_PROTO_RSN) ?
1468                         WPS_AUTH_WPA2PSK : WPS_AUTH_WPAPSK;
1469                 if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
1470                         cred.encr_type = WPS_ENCR_AES;
1471                 else
1472                         cred.encr_type = WPS_ENCR_TKIP;
1473                 if (ssid->passphrase) {
1474                         cred.key_len = os_strlen(ssid->passphrase);
1475                         if (cred.key_len >= 64)
1476                                 return -1;
1477                         os_memcpy(cred.key, ssid->passphrase, cred.key_len);
1478                 } else if (ssid->psk_set) {
1479                         cred.key_len = 32;
1480                         os_memcpy(cred.key, ssid->psk, 32);
1481                 } else
1482                         return -1;
1483         } else {
1484                 cred.auth_type = WPS_AUTH_OPEN;
1485                 cred.encr_type = WPS_ENCR_NONE;
1486         }
1487         return wps_er_set_config(wpa_s->wps_er, u, &cred);
1488 }
1489
1490
1491 int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
1492                        const char *pin, struct wps_new_ap_settings *settings)
1493 {
1494         u8 u[UUID_LEN];
1495         struct wps_credential cred;
1496         size_t len;
1497
1498         if (uuid_str2bin(uuid, u))
1499                 return -1;
1500         if (settings->ssid_hex == NULL || settings->auth == NULL ||
1501             settings->encr == NULL || settings->key_hex == NULL)
1502                 return -1;
1503
1504         os_memset(&cred, 0, sizeof(cred));
1505         len = os_strlen(settings->ssid_hex);
1506         if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1507             hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
1508                 return -1;
1509         cred.ssid_len = len / 2;
1510
1511         len = os_strlen(settings->key_hex);
1512         if ((len & 1) || len > 2 * sizeof(cred.key) ||
1513             hexstr2bin(settings->key_hex, cred.key, len / 2))
1514                 return -1;
1515         cred.key_len = len / 2;
1516
1517         if (os_strcmp(settings->auth, "OPEN") == 0)
1518                 cred.auth_type = WPS_AUTH_OPEN;
1519         else if (os_strcmp(settings->auth, "WPAPSK") == 0)
1520                 cred.auth_type = WPS_AUTH_WPAPSK;
1521         else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
1522                 cred.auth_type = WPS_AUTH_WPA2PSK;
1523         else
1524                 return -1;
1525
1526         if (os_strcmp(settings->encr, "NONE") == 0)
1527                 cred.encr_type = WPS_ENCR_NONE;
1528         else if (os_strcmp(settings->encr, "WEP") == 0)
1529                 cred.encr_type = WPS_ENCR_WEP;
1530         else if (os_strcmp(settings->encr, "TKIP") == 0)
1531                 cred.encr_type = WPS_ENCR_TKIP;
1532         else if (os_strcmp(settings->encr, "CCMP") == 0)
1533                 cred.encr_type = WPS_ENCR_AES;
1534         else
1535                 return -1;
1536
1537         return wps_er_config(wpa_s->wps_er, u, (const u8 *) pin,
1538                              os_strlen(pin), &cred);
1539 }
1540
1541
1542 static void wpas_wps_terminate_cb(void *ctx)
1543 {
1544         wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
1545         eloop_terminate();
1546 }
1547 #endif /* CONFIG_WPS_ER */
1548
1549
1550 int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
1551 {
1552 #ifdef CONFIG_WPS_ER
1553         if (wpa_s->wps_er) {
1554                 wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
1555                 wpa_s->wps_er = NULL;
1556                 return 1;
1557         }
1558 #endif /* CONFIG_WPS_ER */
1559         return 0;
1560 }
1561
1562
1563 int wpas_wps_in_progress(struct wpa_supplicant *wpa_s)
1564 {
1565         struct wpa_ssid *ssid;
1566
1567         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1568                 if (!ssid->disabled && ssid->key_mgmt == WPA_KEY_MGMT_WPS)
1569                         return 1;
1570         }
1571
1572         return 0;
1573 }
1574
1575
1576 void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
1577 {
1578         struct wps_context *wps = wpa_s->wps;
1579
1580         if (wps == NULL)
1581                 return;
1582
1583         if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
1584                 wps->config_methods = wps_config_methods_str2bin(
1585                         wpa_s->conf->config_methods);
1586                 if ((wps->config_methods &
1587                      (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1588                     (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1589                         wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
1590                                    "config methods are not allowed at the "
1591                                    "same time");
1592                         wps->config_methods &= ~WPS_CONFIG_LABEL;
1593                 }
1594         }
1595         wps->config_methods = wps_fix_config_methods(wps->config_methods);
1596
1597         if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE) {
1598                 if (wpa_s->conf->device_type &&
1599                     wps_dev_type_str2bin(wpa_s->conf->device_type,
1600                                          wps->dev.pri_dev_type) < 0)
1601                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
1602         }
1603
1604         if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
1605                 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
1606
1607         if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID) {
1608                 if (is_nil_uuid(wpa_s->conf->uuid)) {
1609                         uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
1610                         wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
1611                                     "address", wps->uuid, WPS_UUID_LEN);
1612                 } else
1613                         os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
1614         }
1615
1616         if (wpa_s->conf->changed_parameters &
1617             (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
1618                 /* Update pointers to make sure they refer current values */
1619                 wps->dev.device_name = wpa_s->conf->device_name;
1620                 wps->dev.manufacturer = wpa_s->conf->manufacturer;
1621                 wps->dev.model_name = wpa_s->conf->model_name;
1622                 wps->dev.model_number = wpa_s->conf->model_number;
1623                 wps->dev.serial_number = wpa_s->conf->serial_number;
1624         }
1625 }