Disable network block temporarily on authentication failures
[mech_eap.git] / wpa_supplicant / wps_supplicant.c
1 /*
2  * wpa_supplicant / WPS integration
3  * Copyright (c) 2008-2012, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "includes.h"
10
11 #include "common.h"
12 #include "eloop.h"
13 #include "uuid.h"
14 #include "crypto/random.h"
15 #include "crypto/dh_group5.h"
16 #include "common/ieee802_11_defs.h"
17 #include "common/ieee802_11_common.h"
18 #include "common/wpa_common.h"
19 #include "common/wpa_ctrl.h"
20 #include "eap_common/eap_wsc_common.h"
21 #include "eap_peer/eap.h"
22 #include "eapol_supp/eapol_supp_sm.h"
23 #include "rsn_supp/wpa.h"
24 #include "wps/wps_attr_parse.h"
25 #include "config.h"
26 #include "wpa_supplicant_i.h"
27 #include "driver_i.h"
28 #include "notify.h"
29 #include "blacklist.h"
30 #include "bss.h"
31 #include "scan.h"
32 #include "ap.h"
33 #include "p2p/p2p.h"
34 #include "p2p_supplicant.h"
35 #include "wps_supplicant.h"
36
37
38 #ifndef WPS_PIN_SCAN_IGNORE_SEL_REG
39 #define WPS_PIN_SCAN_IGNORE_SEL_REG 3
40 #endif /* WPS_PIN_SCAN_IGNORE_SEL_REG */
41
42 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
43 static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
44
45
46 int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
47 {
48         if (!wpa_s->wps_success &&
49             wpa_s->current_ssid &&
50             eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
51                 const u8 *bssid = wpa_s->bssid;
52                 if (is_zero_ether_addr(bssid))
53                         bssid = wpa_s->pending_bssid;
54
55                 wpa_printf(MSG_DEBUG, "WPS: PIN registration with " MACSTR
56                            " did not succeed - continue trying to find "
57                            "suitable AP", MAC2STR(bssid));
58                 wpa_blacklist_add(wpa_s, bssid);
59
60                 wpa_supplicant_deauthenticate(wpa_s,
61                                               WLAN_REASON_DEAUTH_LEAVING);
62                 wpa_s->reassociate = 1;
63                 wpa_supplicant_req_scan(wpa_s,
64                                         wpa_s->blacklist_cleared ? 5 : 0, 0);
65                 wpa_s->blacklist_cleared = 0;
66                 return 1;
67         }
68
69         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
70         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && !wpa_s->wps_success)
71                 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL);
72
73         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
74             !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
75                 int disabled = wpa_s->current_ssid->disabled;
76                 unsigned int freq = wpa_s->assoc_freq;
77                 wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
78                            "try to associate with the received credential "
79                            "(freq=%u)", freq);
80                 wpa_supplicant_deauthenticate(wpa_s,
81                                               WLAN_REASON_DEAUTH_LEAVING);
82                 if (disabled) {
83                         wpa_printf(MSG_DEBUG, "WPS: Current network is "
84                                    "disabled - wait for user to enable");
85                         return 1;
86                 }
87                 wpa_s->after_wps = 5;
88                 wpa_s->wps_freq = freq;
89                 wpa_s->normal_scans = 0;
90                 wpa_s->reassociate = 1;
91                 wpa_supplicant_req_scan(wpa_s, 0, 0);
92                 return 1;
93         }
94
95         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
96                 wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
97                            "for external credential processing");
98                 wpas_clear_wps(wpa_s);
99                 wpa_supplicant_deauthenticate(wpa_s,
100                                               WLAN_REASON_DEAUTH_LEAVING);
101                 return 1;
102         }
103
104         return 0;
105 }
106
107
108 static void wpas_wps_security_workaround(struct wpa_supplicant *wpa_s,
109                                          struct wpa_ssid *ssid,
110                                          const struct wps_credential *cred)
111 {
112         struct wpa_driver_capa capa;
113         struct wpa_bss *bss;
114         const u8 *ie;
115         struct wpa_ie_data adv;
116         int wpa2 = 0, ccmp = 0;
117
118         /*
119          * Many existing WPS APs do not know how to negotiate WPA2 or CCMP in
120          * case they are configured for mixed mode operation (WPA+WPA2 and
121          * TKIP+CCMP). Try to use scan results to figure out whether the AP
122          * actually supports stronger security and select that if the client
123          * has support for it, too.
124          */
125
126         if (wpa_drv_get_capa(wpa_s, &capa))
127                 return; /* Unknown what driver supports */
128
129         if (ssid->ssid == NULL)
130                 return;
131         bss = wpa_bss_get(wpa_s, cred->mac_addr, ssid->ssid, ssid->ssid_len);
132         if (bss == NULL) {
133                 wpa_printf(MSG_DEBUG, "WPS: The AP was not found from BSS "
134                            "table - use credential as-is");
135                 return;
136         }
137
138         wpa_printf(MSG_DEBUG, "WPS: AP found from BSS table");
139
140         ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
141         if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0) {
142                 wpa2 = 1;
143                 if (adv.pairwise_cipher & WPA_CIPHER_CCMP)
144                         ccmp = 1;
145         } else {
146                 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
147                 if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0 &&
148                     adv.pairwise_cipher & WPA_CIPHER_CCMP)
149                         ccmp = 1;
150         }
151
152         if (ie == NULL && (ssid->proto & WPA_PROTO_WPA) &&
153             (ssid->pairwise_cipher & WPA_CIPHER_TKIP)) {
154                 /*
155                  * TODO: This could be the initial AP configuration and the
156                  * Beacon contents could change shortly. Should request a new
157                  * scan and delay addition of the network until the updated
158                  * scan results are available.
159                  */
160                 wpa_printf(MSG_DEBUG, "WPS: The AP did not yet advertise WPA "
161                            "support - use credential as-is");
162                 return;
163         }
164
165         if (ccmp && !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
166             (ssid->pairwise_cipher & WPA_CIPHER_TKIP) &&
167             (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
168                 wpa_printf(MSG_DEBUG, "WPS: Add CCMP into the credential "
169                            "based on scan results");
170                 if (wpa_s->conf->ap_scan == 1)
171                         ssid->pairwise_cipher |= WPA_CIPHER_CCMP;
172                 else
173                         ssid->pairwise_cipher = WPA_CIPHER_CCMP;
174         }
175
176         if (wpa2 && !(ssid->proto & WPA_PROTO_RSN) &&
177             (ssid->proto & WPA_PROTO_WPA) &&
178             (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP)) {
179                 wpa_printf(MSG_DEBUG, "WPS: Add WPA2 into the credential "
180                            "based on scan results");
181                 if (wpa_s->conf->ap_scan == 1)
182                         ssid->proto |= WPA_PROTO_RSN;
183                 else
184                         ssid->proto = WPA_PROTO_RSN;
185         }
186 }
187
188
189 static int wpa_supplicant_wps_cred(void *ctx,
190                                    const struct wps_credential *cred)
191 {
192         struct wpa_supplicant *wpa_s = ctx;
193         struct wpa_ssid *ssid = wpa_s->current_ssid;
194         u8 key_idx = 0;
195         u16 auth_type;
196 #ifdef CONFIG_WPS_REG_DISABLE_OPEN
197         int registrar = 0;
198 #endif /* CONFIG_WPS_REG_DISABLE_OPEN */
199
200         if ((wpa_s->conf->wps_cred_processing == 1 ||
201              wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
202                 size_t blen = cred->cred_attr_len * 2 + 1;
203                 char *buf = os_malloc(blen);
204                 if (buf) {
205                         wpa_snprintf_hex(buf, blen,
206                                          cred->cred_attr, cred->cred_attr_len);
207                         wpa_msg(wpa_s, MSG_INFO, "%s%s",
208                                 WPS_EVENT_CRED_RECEIVED, buf);
209                         os_free(buf);
210                 }
211
212                 wpas_notify_wps_credential(wpa_s, cred);
213         } else
214                 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
215
216         wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
217                         cred->cred_attr, cred->cred_attr_len);
218
219         if (wpa_s->conf->wps_cred_processing == 1)
220                 return 0;
221
222         wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
223         wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
224                    cred->auth_type);
225         wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
226         wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
227         wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
228                         cred->key, cred->key_len);
229         wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
230                    MAC2STR(cred->mac_addr));
231
232         auth_type = cred->auth_type;
233         if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
234                 wpa_printf(MSG_DEBUG, "WPS: Workaround - convert mixed-mode "
235                            "auth_type into WPA2PSK");
236                 auth_type = WPS_AUTH_WPA2PSK;
237         }
238
239         if (auth_type != WPS_AUTH_OPEN &&
240             auth_type != WPS_AUTH_SHARED &&
241             auth_type != WPS_AUTH_WPAPSK &&
242             auth_type != WPS_AUTH_WPA2PSK) {
243                 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
244                            "unsupported authentication type 0x%x",
245                            auth_type);
246                 return 0;
247         }
248
249         if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
250                 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
251                            "on the received credential");
252 #ifdef CONFIG_WPS_REG_DISABLE_OPEN
253                 if (ssid->eap.identity &&
254                     ssid->eap.identity_len == WSC_ID_REGISTRAR_LEN &&
255                     os_memcmp(ssid->eap.identity, WSC_ID_REGISTRAR,
256                               WSC_ID_REGISTRAR_LEN) == 0)
257                         registrar = 1;
258 #endif /* CONFIG_WPS_REG_DISABLE_OPEN */
259                 os_free(ssid->eap.identity);
260                 ssid->eap.identity = NULL;
261                 ssid->eap.identity_len = 0;
262                 os_free(ssid->eap.phase1);
263                 ssid->eap.phase1 = NULL;
264                 os_free(ssid->eap.eap_methods);
265                 ssid->eap.eap_methods = NULL;
266                 if (!ssid->p2p_group) {
267                         ssid->temporary = 0;
268                         ssid->bssid_set = 0;
269                 }
270                 ssid->disabled_until.sec = 0;
271                 ssid->disabled_until.usec = 0;
272                 ssid->auth_failures = 0;
273         } else {
274                 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
275                            "received credential");
276                 ssid = wpa_config_add_network(wpa_s->conf);
277                 if (ssid == NULL)
278                         return -1;
279                 wpas_notify_network_added(wpa_s, ssid);
280         }
281
282         wpa_config_set_network_defaults(ssid);
283
284         os_free(ssid->ssid);
285         ssid->ssid = os_malloc(cred->ssid_len);
286         if (ssid->ssid) {
287                 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
288                 ssid->ssid_len = cred->ssid_len;
289         }
290
291         switch (cred->encr_type) {
292         case WPS_ENCR_NONE:
293                 break;
294         case WPS_ENCR_WEP:
295                 if (cred->key_len <= 0)
296                         break;
297                 if (cred->key_len != 5 && cred->key_len != 13 &&
298                     cred->key_len != 10 && cred->key_len != 26) {
299                         wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key length "
300                                    "%lu", (unsigned long) cred->key_len);
301                         return -1;
302                 }
303                 if (cred->key_idx > NUM_WEP_KEYS) {
304                         wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key index %d",
305                                    cred->key_idx);
306                         return -1;
307                 }
308                 if (cred->key_idx)
309                         key_idx = cred->key_idx - 1;
310                 if (cred->key_len == 10 || cred->key_len == 26) {
311                         if (hexstr2bin((char *) cred->key,
312                                        ssid->wep_key[key_idx],
313                                        cred->key_len / 2) < 0) {
314                                 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key "
315                                            "%d", key_idx);
316                                 return -1;
317                         }
318                         ssid->wep_key_len[key_idx] = cred->key_len / 2;
319                 } else {
320                         os_memcpy(ssid->wep_key[key_idx], cred->key,
321                                   cred->key_len);
322                         ssid->wep_key_len[key_idx] = cred->key_len;
323                 }
324                 ssid->wep_tx_keyidx = key_idx;
325                 break;
326         case WPS_ENCR_TKIP:
327                 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
328                 break;
329         case WPS_ENCR_AES:
330                 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
331                 break;
332         }
333
334         switch (auth_type) {
335         case WPS_AUTH_OPEN:
336                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
337                 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
338                 ssid->proto = 0;
339 #ifdef CONFIG_WPS_REG_DISABLE_OPEN
340                 if (registrar) {
341                         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OPEN_NETWORK
342                                 "id=%d - Credentials for an open "
343                                 "network disabled by default - use "
344                                 "'select_network %d' to enable",
345                                 ssid->id, ssid->id);
346                         ssid->disabled = 1;
347                 }
348 #endif /* CONFIG_WPS_REG_DISABLE_OPEN */
349                 break;
350         case WPS_AUTH_SHARED:
351                 ssid->auth_alg = WPA_AUTH_ALG_SHARED;
352                 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
353                 ssid->proto = 0;
354                 break;
355         case WPS_AUTH_WPAPSK:
356                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
357                 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
358                 ssid->proto = WPA_PROTO_WPA;
359                 break;
360         case WPS_AUTH_WPA2PSK:
361                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
362                 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
363                 ssid->proto = WPA_PROTO_RSN;
364                 break;
365         }
366
367         if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
368                 if (cred->key_len == 2 * PMK_LEN) {
369                         if (hexstr2bin((const char *) cred->key, ssid->psk,
370                                        PMK_LEN)) {
371                                 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
372                                            "Key");
373                                 return -1;
374                         }
375                         ssid->psk_set = 1;
376                         ssid->export_keys = 1;
377                 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
378                         os_free(ssid->passphrase);
379                         ssid->passphrase = os_malloc(cred->key_len + 1);
380                         if (ssid->passphrase == NULL)
381                                 return -1;
382                         os_memcpy(ssid->passphrase, cred->key, cred->key_len);
383                         ssid->passphrase[cred->key_len] = '\0';
384                         wpa_config_update_psk(ssid);
385                         ssid->export_keys = 1;
386                 } else {
387                         wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
388                                    "length %lu",
389                                    (unsigned long) cred->key_len);
390                         return -1;
391                 }
392         }
393
394         wpas_wps_security_workaround(wpa_s, ssid, cred);
395
396 #ifndef CONFIG_NO_CONFIG_WRITE
397         if (wpa_s->conf->update_config &&
398             wpa_config_write(wpa_s->confname, wpa_s->conf)) {
399                 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
400                 return -1;
401         }
402 #endif /* CONFIG_NO_CONFIG_WRITE */
403
404         return 0;
405 }
406
407
408 #ifdef CONFIG_P2P
409 static void wpas_wps_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
410 {
411         struct wpa_supplicant *wpa_s = eloop_ctx;
412         wpas_p2p_notif_pbc_overlap(wpa_s);
413 }
414 #endif /* CONFIG_P2P */
415
416
417 static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
418                                          struct wps_event_m2d *m2d)
419 {
420         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
421                 "dev_password_id=%d config_error=%d",
422                 m2d->dev_password_id, m2d->config_error);
423         wpas_notify_wps_event_m2d(wpa_s, m2d);
424 #ifdef CONFIG_P2P
425         if (wpa_s->parent && wpa_s->parent != wpa_s) {
426                 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_M2D
427                         "dev_password_id=%d config_error=%d",
428                         m2d->dev_password_id, m2d->config_error);
429         }
430         if (m2d->config_error == WPS_CFG_MULTIPLE_PBC_DETECTED) {
431                 /*
432                  * Notify P2P from eloop timeout to avoid issues with the
433                  * interface getting removed while processing a message.
434                  */
435                 eloop_register_timeout(0, 0, wpas_wps_pbc_overlap_cb, wpa_s,
436                                        NULL);
437         }
438 #endif /* CONFIG_P2P */
439 }
440
441
442 static const char * wps_event_fail_reason[NUM_WPS_EI_VALUES] = {
443         "No Error", /* WPS_EI_NO_ERROR */
444         "TKIP Only Prohibited", /* WPS_EI_SECURITY_TKIP_ONLY_PROHIBITED */
445         "WEP Prohibited" /* WPS_EI_SECURITY_WEP_PROHIBITED */
446 };
447
448 static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
449                                           struct wps_event_fail *fail)
450 {
451         if (fail->error_indication > 0 &&
452             fail->error_indication < NUM_WPS_EI_VALUES) {
453                 wpa_msg(wpa_s, MSG_INFO,
454                         WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
455                         fail->msg, fail->config_error, fail->error_indication,
456                         wps_event_fail_reason[fail->error_indication]);
457                 if (wpa_s->parent && wpa_s->parent != wpa_s)
458                         wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
459                                 "msg=%d config_error=%d reason=%d (%s)",
460                                 fail->msg, fail->config_error,
461                                 fail->error_indication,
462                                 wps_event_fail_reason[fail->error_indication]);
463         } else {
464                 wpa_msg(wpa_s, MSG_INFO,
465                         WPS_EVENT_FAIL "msg=%d config_error=%d",
466                         fail->msg, fail->config_error);
467                 if (wpa_s->parent && wpa_s->parent != wpa_s)
468                         wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
469                                 "msg=%d config_error=%d",
470                                 fail->msg, fail->config_error);
471         }
472         wpas_clear_wps(wpa_s);
473         wpas_notify_wps_event_fail(wpa_s, fail);
474 #ifdef CONFIG_P2P
475         wpas_p2p_wps_failed(wpa_s, fail);
476 #endif /* CONFIG_P2P */
477 }
478
479
480 static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
481 {
482         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
483         wpa_s->wps_success = 1;
484         wpas_notify_wps_event_success(wpa_s);
485 #ifdef CONFIG_P2P
486         wpas_p2p_wps_success(wpa_s, wpa_s->bssid, 0);
487 #endif /* CONFIG_P2P */
488 }
489
490
491 static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
492                                                struct wps_event_er_ap *ap)
493 {
494         char uuid_str[100];
495         char dev_type[WPS_DEV_TYPE_BUFSIZE];
496
497         uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
498         if (ap->pri_dev_type)
499                 wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
500                                      sizeof(dev_type));
501         else
502                 dev_type[0] = '\0';
503
504         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
505                 " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
506                 uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
507                 ap->friendly_name ? ap->friendly_name : "",
508                 ap->manufacturer ? ap->manufacturer : "",
509                 ap->model_description ? ap->model_description : "",
510                 ap->model_name ? ap->model_name : "",
511                 ap->manufacturer_url ? ap->manufacturer_url : "",
512                 ap->model_url ? ap->model_url : "");
513 }
514
515
516 static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
517                                                   struct wps_event_er_ap *ap)
518 {
519         char uuid_str[100];
520         uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
521         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
522 }
523
524
525 static void wpa_supplicant_wps_event_er_enrollee_add(
526         struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
527 {
528         char uuid_str[100];
529         char dev_type[WPS_DEV_TYPE_BUFSIZE];
530
531         uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
532         if (enrollee->pri_dev_type)
533                 wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
534                                      sizeof(dev_type));
535         else
536                 dev_type[0] = '\0';
537
538         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
539                 " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
540                 "|%s|%s|%s|%s|%s|",
541                 uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
542                 enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
543                 enrollee->dev_name ? enrollee->dev_name : "",
544                 enrollee->manufacturer ? enrollee->manufacturer : "",
545                 enrollee->model_name ? enrollee->model_name : "",
546                 enrollee->model_number ? enrollee->model_number : "",
547                 enrollee->serial_number ? enrollee->serial_number : "");
548 }
549
550
551 static void wpa_supplicant_wps_event_er_enrollee_remove(
552         struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
553 {
554         char uuid_str[100];
555         uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
556         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
557                 uuid_str, MAC2STR(enrollee->mac_addr));
558 }
559
560
561 static void wpa_supplicant_wps_event_er_ap_settings(
562         struct wpa_supplicant *wpa_s,
563         struct wps_event_er_ap_settings *ap_settings)
564 {
565         char uuid_str[100];
566         char key_str[65];
567         const struct wps_credential *cred = ap_settings->cred;
568
569         key_str[0] = '\0';
570         if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
571                 if (cred->key_len >= 8 && cred->key_len <= 64) {
572                         os_memcpy(key_str, cred->key, cred->key_len);
573                         key_str[cred->key_len] = '\0';
574                 }
575         }
576
577         uuid_bin2str(ap_settings->uuid, uuid_str, sizeof(uuid_str));
578         /* Use wpa_msg_ctrl to avoid showing the key in debug log */
579         wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_SETTINGS
580                      "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
581                      "key=%s",
582                      uuid_str, wpa_ssid_txt(cred->ssid, cred->ssid_len),
583                      cred->auth_type, cred->encr_type, key_str);
584 }
585
586
587 static void wpa_supplicant_wps_event_er_set_sel_reg(
588         struct wpa_supplicant *wpa_s,
589         struct wps_event_er_set_selected_registrar *ev)
590 {
591         char uuid_str[100];
592
593         uuid_bin2str(ev->uuid, uuid_str, sizeof(uuid_str));
594         switch (ev->state) {
595         case WPS_ER_SET_SEL_REG_START:
596                 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
597                         "uuid=%s state=START sel_reg=%d dev_passwd_id=%u "
598                         "sel_reg_config_methods=0x%x",
599                         uuid_str, ev->sel_reg, ev->dev_passwd_id,
600                         ev->sel_reg_config_methods);
601                 break;
602         case WPS_ER_SET_SEL_REG_DONE:
603                 wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
604                         "uuid=%s state=DONE", uuid_str);
605                 break;
606         case WPS_ER_SET_SEL_REG_FAILED:
607                 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_SET_SEL_REG
608                         "uuid=%s state=FAILED", uuid_str);
609                 break;
610         }
611 }
612
613
614 static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
615                                      union wps_event_data *data)
616 {
617         struct wpa_supplicant *wpa_s = ctx;
618         switch (event) {
619         case WPS_EV_M2D:
620                 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
621                 break;
622         case WPS_EV_FAIL:
623                 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
624                 break;
625         case WPS_EV_SUCCESS:
626                 wpa_supplicant_wps_event_success(wpa_s);
627                 break;
628         case WPS_EV_PWD_AUTH_FAIL:
629 #ifdef CONFIG_AP
630                 if (wpa_s->ap_iface && data->pwd_auth_fail.enrollee)
631                         wpa_supplicant_ap_pwd_auth_fail(wpa_s);
632 #endif /* CONFIG_AP */
633                 break;
634         case WPS_EV_PBC_OVERLAP:
635                 break;
636         case WPS_EV_PBC_TIMEOUT:
637                 break;
638         case WPS_EV_ER_AP_ADD:
639                 wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
640                 break;
641         case WPS_EV_ER_AP_REMOVE:
642                 wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
643                 break;
644         case WPS_EV_ER_ENROLLEE_ADD:
645                 wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
646                                                          &data->enrollee);
647                 break;
648         case WPS_EV_ER_ENROLLEE_REMOVE:
649                 wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
650                                                             &data->enrollee);
651                 break;
652         case WPS_EV_ER_AP_SETTINGS:
653                 wpa_supplicant_wps_event_er_ap_settings(wpa_s,
654                                                         &data->ap_settings);
655                 break;
656         case WPS_EV_ER_SET_SELECTED_REGISTRAR:
657                 wpa_supplicant_wps_event_er_set_sel_reg(wpa_s,
658                                                         &data->set_sel_reg);
659                 break;
660         case WPS_EV_AP_PIN_SUCCESS:
661                 break;
662         }
663 }
664
665
666 enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
667 {
668         if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
669             eap_is_wps_pin_enrollee(&ssid->eap))
670                 return WPS_REQ_ENROLLEE;
671         else
672                 return WPS_REQ_REGISTRAR;
673 }
674
675
676 static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
677 {
678         int id;
679         struct wpa_ssid *ssid, *remove_ssid = NULL, *prev_current;
680
681         prev_current = wpa_s->current_ssid;
682
683         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
684
685         /* Remove any existing WPS network from configuration */
686         ssid = wpa_s->conf->ssid;
687         while (ssid) {
688                 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
689                         if (ssid == wpa_s->current_ssid) {
690                                 wpa_s->current_ssid = NULL;
691                                 if (ssid != NULL)
692                                         wpas_notify_network_changed(wpa_s);
693                         }
694                         id = ssid->id;
695                         remove_ssid = ssid;
696                 } else
697                         id = -1;
698                 ssid = ssid->next;
699                 if (id >= 0) {
700                         if (prev_current == remove_ssid) {
701                                 wpa_sm_set_config(wpa_s->wpa, NULL);
702                                 eapol_sm_notify_config(wpa_s->eapol, NULL,
703                                                        NULL);
704                         }
705                         wpas_notify_network_removed(wpa_s, remove_ssid);
706                         wpa_config_remove_network(wpa_s->conf, id);
707                 }
708         }
709 }
710
711
712 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
713 {
714         struct wpa_supplicant *wpa_s = eloop_ctx;
715         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
716                 "out");
717         wpas_clear_wps(wpa_s);
718 }
719
720
721 static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
722                                               int registrar, const u8 *bssid)
723 {
724         struct wpa_ssid *ssid;
725
726         ssid = wpa_config_add_network(wpa_s->conf);
727         if (ssid == NULL)
728                 return NULL;
729         wpas_notify_network_added(wpa_s, ssid);
730         wpa_config_set_network_defaults(ssid);
731         ssid->temporary = 1;
732         if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
733             wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
734             wpa_config_set(ssid, "identity", registrar ?
735                            "\"" WSC_ID_REGISTRAR "\"" :
736                            "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
737                 wpas_notify_network_removed(wpa_s, ssid);
738                 wpa_config_remove_network(wpa_s->conf, ssid->id);
739                 return NULL;
740         }
741
742         if (bssid) {
743 #ifndef CONFIG_P2P
744                 struct wpa_bss *bss;
745                 int count = 0;
746 #endif /* CONFIG_P2P */
747
748                 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
749                 ssid->bssid_set = 1;
750
751                 /*
752                  * Note: With P2P, the SSID may change at the time the WPS
753                  * provisioning is started, so better not filter the AP based
754                  * on the current SSID in the scan results.
755                  */
756 #ifndef CONFIG_P2P
757                 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
758                         if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
759                                 continue;
760
761                         os_free(ssid->ssid);
762                         ssid->ssid = os_malloc(bss->ssid_len);
763                         if (ssid->ssid == NULL)
764                                 break;
765                         os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
766                         ssid->ssid_len = bss->ssid_len;
767                         wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
768                                           "scan results",
769                                           ssid->ssid, ssid->ssid_len);
770                         count++;
771                 }
772
773                 if (count > 1) {
774                         wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
775                                    "for the AP; use wildcard");
776                         os_free(ssid->ssid);
777                         ssid->ssid = NULL;
778                         ssid->ssid_len = 0;
779                 }
780 #endif /* CONFIG_P2P */
781         }
782
783         return ssid;
784 }
785
786
787 static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
788                              struct wpa_ssid *selected, const u8 *bssid)
789 {
790         struct wpa_ssid *ssid;
791         struct wpa_bss *bss;
792
793         wpa_s->known_wps_freq = 0;
794         if (bssid) {
795                 bss = wpa_bss_get_bssid(wpa_s, bssid);
796                 if (bss && bss->freq > 0) {
797                         wpa_s->known_wps_freq = 1;
798                         wpa_s->wps_freq = bss->freq;
799                 }
800         }
801
802         if (wpa_s->current_ssid)
803                 wpa_supplicant_deauthenticate(
804                         wpa_s, WLAN_REASON_DEAUTH_LEAVING);
805
806         /* Mark all other networks disabled and trigger reassociation */
807         ssid = wpa_s->conf->ssid;
808         while (ssid) {
809                 int was_disabled = ssid->disabled;
810                 /*
811                  * In case the network object corresponds to a persistent group
812                  * then do not send out network disabled signal. In addition,
813                  * do not change disabled status of persistent network objects
814                  * from 2 to 1 should we connect to another network.
815                  */
816                 if (was_disabled != 2) {
817                         ssid->disabled = ssid != selected;
818                         if (was_disabled != ssid->disabled)
819                                 wpas_notify_network_enabled_changed(wpa_s,
820                                                                     ssid);
821                 }
822                 ssid = ssid->next;
823         }
824         wpa_s->disconnected = 0;
825         wpa_s->reassociate = 1;
826         wpa_s->scan_runs = 0;
827         wpa_s->normal_scans = 0;
828         wpa_s->wps_success = 0;
829         wpa_s->blacklist_cleared = 0;
830         wpa_supplicant_req_scan(wpa_s, 0, 0);
831 }
832
833
834 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
835                        int p2p_group)
836 {
837         struct wpa_ssid *ssid;
838         wpas_clear_wps(wpa_s);
839         ssid = wpas_wps_add_network(wpa_s, 0, bssid);
840         if (ssid == NULL)
841                 return -1;
842         ssid->temporary = 1;
843         ssid->p2p_group = p2p_group;
844 #ifdef CONFIG_P2P
845         if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
846                 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
847                 if (ssid->ssid) {
848                         ssid->ssid_len = wpa_s->go_params->ssid_len;
849                         os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
850                                   ssid->ssid_len);
851                         wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
852                                           "SSID", ssid->ssid, ssid->ssid_len);
853                 }
854         }
855 #endif /* CONFIG_P2P */
856         wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
857         if (wpa_s->wps_fragment_size)
858                 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
859         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
860                                wpa_s, NULL);
861         wpas_wps_reassoc(wpa_s, ssid, bssid);
862         return 0;
863 }
864
865
866 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
867                        const char *pin, int p2p_group, u16 dev_pw_id)
868 {
869         struct wpa_ssid *ssid;
870         char val[128];
871         unsigned int rpin = 0;
872
873         wpas_clear_wps(wpa_s);
874         ssid = wpas_wps_add_network(wpa_s, 0, bssid);
875         if (ssid == NULL)
876                 return -1;
877         ssid->temporary = 1;
878         ssid->p2p_group = p2p_group;
879 #ifdef CONFIG_P2P
880         if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
881                 ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
882                 if (ssid->ssid) {
883                         ssid->ssid_len = wpa_s->go_params->ssid_len;
884                         os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
885                                   ssid->ssid_len);
886                         wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
887                                           "SSID", ssid->ssid, ssid->ssid_len);
888                 }
889         }
890 #endif /* CONFIG_P2P */
891         if (pin)
892                 os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u\"",
893                             pin, dev_pw_id);
894         else {
895                 rpin = wps_generate_pin();
896                 os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u\"",
897                             rpin, dev_pw_id);
898         }
899         wpa_config_set(ssid, "phase1", val, 0);
900         if (wpa_s->wps_fragment_size)
901                 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
902         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
903                                wpa_s, NULL);
904         wpas_wps_reassoc(wpa_s, ssid, bssid);
905         return rpin;
906 }
907
908
909 /* Cancel the wps pbc/pin requests */
910 int wpas_wps_cancel(struct wpa_supplicant *wpa_s)
911 {
912 #ifdef CONFIG_AP
913         if (wpa_s->ap_iface) {
914                 wpa_printf(MSG_DEBUG, "WPS: Cancelling in AP mode");
915                 return wpa_supplicant_ap_wps_cancel(wpa_s);
916         }
917 #endif /* CONFIG_AP */
918
919         if (wpa_s->wpa_state == WPA_SCANNING ||
920             wpa_s->wpa_state == WPA_DISCONNECTED) {
921                 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - cancel scan");
922                 wpa_supplicant_cancel_scan(wpa_s);
923                 wpas_clear_wps(wpa_s);
924         } else if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
925                 wpa_printf(MSG_DEBUG, "WPS: Cancel operation - "
926                            "deauthenticate");
927                 wpa_supplicant_deauthenticate(wpa_s,
928                                               WLAN_REASON_DEAUTH_LEAVING);
929                 wpas_clear_wps(wpa_s);
930         }
931
932         return 0;
933 }
934
935
936 #ifdef CONFIG_WPS_OOB
937 int wpas_wps_start_oob(struct wpa_supplicant *wpa_s, char *device_type,
938                        char *path, char *method, char *name)
939 {
940         struct wps_context *wps = wpa_s->wps;
941         struct oob_device_data *oob_dev;
942
943         oob_dev = wps_get_oob_device(device_type);
944         if (oob_dev == NULL)
945                 return -1;
946         oob_dev->device_path = path;
947         oob_dev->device_name = name;
948         wps->oob_conf.oob_method = wps_get_oob_method(method);
949
950         if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E) {
951                 /*
952                  * Use pre-configured DH keys in order to be able to write the
953                  * key hash into the OOB file.
954                  */
955                 wpabuf_free(wps->dh_pubkey);
956                 wpabuf_free(wps->dh_privkey);
957                 wps->dh_privkey = NULL;
958                 wps->dh_pubkey = NULL;
959                 dh5_free(wps->dh_ctx);
960                 wps->dh_ctx = dh5_init(&wps->dh_privkey, &wps->dh_pubkey);
961                 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
962                 if (wps->dh_ctx == NULL || wps->dh_pubkey == NULL) {
963                         wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
964                                    "Diffie-Hellman handshake");
965                         return -1;
966                 }
967         }
968
969         if (wps->oob_conf.oob_method == OOB_METHOD_CRED)
970                 wpas_clear_wps(wpa_s);
971
972         if (wps_process_oob(wps, oob_dev, 0) < 0)
973                 return -1;
974
975         if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
976              wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
977             wpas_wps_start_pin(wpa_s, NULL,
978                                wpabuf_head(wps->oob_conf.dev_password), 0,
979                                DEV_PW_DEFAULT) < 0)
980                         return -1;
981
982         return 0;
983 }
984 #endif /* CONFIG_WPS_OOB */
985
986
987 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
988                        const char *pin, struct wps_new_ap_settings *settings)
989 {
990         struct wpa_ssid *ssid;
991         char val[200];
992         char *pos, *end;
993         int res;
994
995         if (!pin)
996                 return -1;
997         wpas_clear_wps(wpa_s);
998         ssid = wpas_wps_add_network(wpa_s, 1, bssid);
999         if (ssid == NULL)
1000                 return -1;
1001         ssid->temporary = 1;
1002         pos = val;
1003         end = pos + sizeof(val);
1004         res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
1005         if (res < 0 || res >= end - pos)
1006                 return -1;
1007         pos += res;
1008         if (settings) {
1009                 res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
1010                                   "new_encr=%s new_key=%s",
1011                                   settings->ssid_hex, settings->auth,
1012                                   settings->encr, settings->key_hex);
1013                 if (res < 0 || res >= end - pos)
1014                         return -1;
1015                 pos += res;
1016         }
1017         res = os_snprintf(pos, end - pos, "\"");
1018         if (res < 0 || res >= end - pos)
1019                 return -1;
1020         wpa_config_set(ssid, "phase1", val, 0);
1021         if (wpa_s->wps_fragment_size)
1022                 ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1023         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1024                                wpa_s, NULL);
1025         wpas_wps_reassoc(wpa_s, ssid, bssid);
1026         return 0;
1027 }
1028
1029
1030 static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
1031                                size_t psk_len)
1032 {
1033         wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
1034                    "STA " MACSTR, MAC2STR(mac_addr));
1035         wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
1036
1037         /* TODO */
1038
1039         return 0;
1040 }
1041
1042
1043 static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
1044                                    const struct wps_device_data *dev)
1045 {
1046         char uuid[40], txt[400];
1047         int len;
1048         char devtype[WPS_DEV_TYPE_BUFSIZE];
1049         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
1050                 return;
1051         wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
1052         len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
1053                           " [%s|%s|%s|%s|%s|%s]",
1054                           uuid, MAC2STR(dev->mac_addr), dev->device_name,
1055                           dev->manufacturer, dev->model_name,
1056                           dev->model_number, dev->serial_number,
1057                           wps_dev_type_bin2str(dev->pri_dev_type, devtype,
1058                                                sizeof(devtype)));
1059         if (len > 0 && len < (int) sizeof(txt))
1060                 wpa_printf(MSG_INFO, "%s", txt);
1061 }
1062
1063
1064 static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
1065                                     u16 sel_reg_config_methods)
1066 {
1067 #ifdef CONFIG_WPS_ER
1068         struct wpa_supplicant *wpa_s = ctx;
1069
1070         if (wpa_s->wps_er == NULL)
1071                 return;
1072         wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
1073                    "dev_password_id=%u sel_reg_config_methods=0x%x",
1074                    sel_reg, dev_passwd_id, sel_reg_config_methods);
1075         wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
1076                            sel_reg_config_methods);
1077 #endif /* CONFIG_WPS_ER */
1078 }
1079
1080
1081 static u16 wps_fix_config_methods(u16 config_methods)
1082 {
1083 #ifdef CONFIG_WPS2
1084         if ((config_methods &
1085              (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
1086               WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
1087                 wpa_printf(MSG_INFO, "WPS: Converting display to "
1088                            "virtual_display for WPS 2.0 compliance");
1089                 config_methods |= WPS_CONFIG_VIRT_DISPLAY;
1090         }
1091         if ((config_methods &
1092              (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
1093               WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
1094                 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
1095                            "virtual_push_button for WPS 2.0 compliance");
1096                 config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
1097         }
1098 #endif /* CONFIG_WPS2 */
1099
1100         return config_methods;
1101 }
1102
1103
1104 static void wpas_wps_set_uuid(struct wpa_supplicant *wpa_s,
1105                               struct wps_context *wps)
1106 {
1107         wpa_printf(MSG_DEBUG, "WPS: Set UUID for interface %s", wpa_s->ifname);
1108         if (is_nil_uuid(wpa_s->conf->uuid)) {
1109                 struct wpa_supplicant *first;
1110                 first = wpa_s->global->ifaces;
1111                 while (first && first->next)
1112                         first = first->next;
1113                 if (first && first != wpa_s) {
1114                         if (wps != wpa_s->global->ifaces->wps)
1115                                 os_memcpy(wps->uuid,
1116                                           wpa_s->global->ifaces->wps->uuid,
1117                                           WPS_UUID_LEN);
1118                         wpa_hexdump(MSG_DEBUG, "WPS: UUID from the first "
1119                                     "interface", wps->uuid, WPS_UUID_LEN);
1120                 } else {
1121                         uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
1122                         wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
1123                                     "address", wps->uuid, WPS_UUID_LEN);
1124                 }
1125         } else {
1126                 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
1127                 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on configuration",
1128                             wps->uuid, WPS_UUID_LEN);
1129         }
1130 }
1131
1132
1133 static void wpas_wps_set_vendor_ext_m1(struct wpa_supplicant *wpa_s,
1134                                        struct wps_context *wps)
1135 {
1136         wpabuf_free(wps->dev.vendor_ext_m1);
1137         wps->dev.vendor_ext_m1 = NULL;
1138
1139         if (wpa_s->conf->wps_vendor_ext_m1) {
1140                 wps->dev.vendor_ext_m1 =
1141                         wpabuf_dup(wpa_s->conf->wps_vendor_ext_m1);
1142                 if (!wps->dev.vendor_ext_m1) {
1143                         wpa_printf(MSG_ERROR, "WPS: Cannot "
1144                                    "allocate memory for vendor_ext_m1");
1145                 }
1146         }
1147 }
1148
1149
1150 int wpas_wps_init(struct wpa_supplicant *wpa_s)
1151 {
1152         struct wps_context *wps;
1153         struct wps_registrar_config rcfg;
1154         struct hostapd_hw_modes *modes;
1155         u16 m;
1156
1157         wps = os_zalloc(sizeof(*wps));
1158         if (wps == NULL)
1159                 return -1;
1160
1161         wps->cred_cb = wpa_supplicant_wps_cred;
1162         wps->event_cb = wpa_supplicant_wps_event;
1163         wps->cb_ctx = wpa_s;
1164
1165         wps->dev.device_name = wpa_s->conf->device_name;
1166         wps->dev.manufacturer = wpa_s->conf->manufacturer;
1167         wps->dev.model_name = wpa_s->conf->model_name;
1168         wps->dev.model_number = wpa_s->conf->model_number;
1169         wps->dev.serial_number = wpa_s->conf->serial_number;
1170         wps->config_methods =
1171                 wps_config_methods_str2bin(wpa_s->conf->config_methods);
1172         if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1173             (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1174                 wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
1175                            "methods are not allowed at the same time");
1176                 os_free(wps);
1177                 return -1;
1178         }
1179         wps->config_methods = wps_fix_config_methods(wps->config_methods);
1180         wps->dev.config_methods = wps->config_methods;
1181         os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
1182                   WPS_DEV_TYPE_LEN);
1183
1184         wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
1185         os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
1186                   WPS_DEV_TYPE_LEN * wps->dev.num_sec_dev_types);
1187
1188         wpas_wps_set_vendor_ext_m1(wpa_s, wps);
1189
1190         wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
1191         modes = wpa_s->hw.modes;
1192         if (modes) {
1193                 for (m = 0; m < wpa_s->hw.num_modes; m++) {
1194                         if (modes[m].mode == HOSTAPD_MODE_IEEE80211B ||
1195                             modes[m].mode == HOSTAPD_MODE_IEEE80211G)
1196                                 wps->dev.rf_bands |= WPS_RF_24GHZ;
1197                         else if (modes[m].mode == HOSTAPD_MODE_IEEE80211A)
1198                                 wps->dev.rf_bands |= WPS_RF_50GHZ;
1199                 }
1200         }
1201         if (wps->dev.rf_bands == 0) {
1202                 /*
1203                  * Default to claiming support for both bands if the driver
1204                  * does not provide support for fetching supported bands.
1205                  */
1206                 wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ;
1207         }
1208         os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
1209         wpas_wps_set_uuid(wpa_s, wps);
1210
1211         wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
1212         wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
1213
1214         os_memset(&rcfg, 0, sizeof(rcfg));
1215         rcfg.new_psk_cb = wpas_wps_new_psk_cb;
1216         rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
1217         rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
1218         rcfg.cb_ctx = wpa_s;
1219
1220         wps->registrar = wps_registrar_init(wps, &rcfg);
1221         if (wps->registrar == NULL) {
1222                 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
1223                 os_free(wps);
1224                 return -1;
1225         }
1226
1227         wpa_s->wps = wps;
1228
1229         return 0;
1230 }
1231
1232
1233 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
1234 {
1235         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
1236
1237         if (wpa_s->wps == NULL)
1238                 return;
1239
1240 #ifdef CONFIG_WPS_ER
1241         wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1242         wpa_s->wps_er = NULL;
1243 #endif /* CONFIG_WPS_ER */
1244
1245         wps_registrar_deinit(wpa_s->wps->registrar);
1246         wpabuf_free(wpa_s->wps->dh_pubkey);
1247         wpabuf_free(wpa_s->wps->dh_privkey);
1248         wpabuf_free(wpa_s->wps->oob_conf.pubkey_hash);
1249         wpabuf_free(wpa_s->wps->oob_conf.dev_password);
1250         wpabuf_free(wpa_s->wps->dev.vendor_ext_m1);
1251         os_free(wpa_s->wps->network_key);
1252         os_free(wpa_s->wps);
1253         wpa_s->wps = NULL;
1254 }
1255
1256
1257 int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
1258                             struct wpa_ssid *ssid, struct wpa_scan_res *bss)
1259 {
1260         struct wpabuf *wps_ie;
1261
1262         if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
1263                 return -1;
1264
1265         wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1266         if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1267                 if (!wps_ie) {
1268                         wpa_printf(MSG_DEBUG, "   skip - non-WPS AP");
1269                         return 0;
1270                 }
1271
1272                 if (!wps_is_selected_pbc_registrar(wps_ie)) {
1273                         wpa_printf(MSG_DEBUG, "   skip - WPS AP "
1274                                    "without active PBC Registrar");
1275                         wpabuf_free(wps_ie);
1276                         return 0;
1277                 }
1278
1279                 /* TODO: overlap detection */
1280                 wpa_printf(MSG_DEBUG, "   selected based on WPS IE "
1281                            "(Active PBC)");
1282                 wpabuf_free(wps_ie);
1283                 return 1;
1284         }
1285
1286         if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1287                 if (!wps_ie) {
1288                         wpa_printf(MSG_DEBUG, "   skip - non-WPS AP");
1289                         return 0;
1290                 }
1291
1292                 /*
1293                  * Start with WPS APs that advertise our address as an
1294                  * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
1295                  * allow any WPS AP after couple of scans since some APs do not
1296                  * set Selected Registrar attribute properly when using
1297                  * external Registrar.
1298                  */
1299                 if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
1300                         if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
1301                                 wpa_printf(MSG_DEBUG, "   skip - WPS AP "
1302                                            "without active PIN Registrar");
1303                                 wpabuf_free(wps_ie);
1304                                 return 0;
1305                         }
1306                         wpa_printf(MSG_DEBUG, "   selected based on WPS IE");
1307                 } else {
1308                         wpa_printf(MSG_DEBUG, "   selected based on WPS IE "
1309                                    "(Authorized MAC or Active PIN)");
1310                 }
1311                 wpabuf_free(wps_ie);
1312                 return 1;
1313         }
1314
1315         if (wps_ie) {
1316                 wpa_printf(MSG_DEBUG, "   selected based on WPS IE");
1317                 wpabuf_free(wps_ie);
1318                 return 1;
1319         }
1320
1321         return -1;
1322 }
1323
1324
1325 int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
1326                               struct wpa_ssid *ssid,
1327                               struct wpa_scan_res *bss)
1328 {
1329         struct wpabuf *wps_ie = NULL;
1330         int ret = 0;
1331
1332         if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1333                 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1334                 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
1335                         /* allow wildcard SSID for WPS PBC */
1336                         ret = 1;
1337                 }
1338         } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1339                 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1340                 if (wps_ie &&
1341                     (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
1342                      wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
1343                         /* allow wildcard SSID for WPS PIN */
1344                         ret = 1;
1345                 }
1346         }
1347
1348         if (!ret && ssid->bssid_set &&
1349             os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
1350                 /* allow wildcard SSID due to hardcoded BSSID match */
1351                 ret = 1;
1352         }
1353
1354 #ifdef CONFIG_WPS_STRICT
1355         if (wps_ie) {
1356                 if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
1357                                                    0, bss->bssid) < 0)
1358                         ret = 0;
1359                 if (bss->beacon_ie_len) {
1360                         struct wpabuf *bcn_wps;
1361                         bcn_wps = wpa_scan_get_vendor_ie_multi_beacon(
1362                                 bss, WPS_IE_VENDOR_TYPE);
1363                         if (bcn_wps == NULL) {
1364                                 wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
1365                                            "missing from AP Beacon");
1366                                 ret = 0;
1367                         } else {
1368                                 if (wps_validate_beacon(wps_ie) < 0)
1369                                         ret = 0;
1370                                 wpabuf_free(bcn_wps);
1371                         }
1372                 }
1373         }
1374 #endif /* CONFIG_WPS_STRICT */
1375
1376         wpabuf_free(wps_ie);
1377
1378         return ret;
1379 }
1380
1381
1382 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
1383                               struct wpa_bss *selected, struct wpa_ssid *ssid)
1384 {
1385         const u8 *sel_uuid, *uuid;
1386         struct wpabuf *wps_ie;
1387         int ret = 0;
1388         struct wpa_bss *bss;
1389
1390         if (!eap_is_wps_pbc_enrollee(&ssid->eap))
1391                 return 0;
1392
1393         wpa_printf(MSG_DEBUG, "WPS: Check whether PBC session overlap is "
1394                    "present in scan results; selected BSSID " MACSTR,
1395                    MAC2STR(selected->bssid));
1396
1397         /* Make sure that only one AP is in active PBC mode */
1398         wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
1399         if (wps_ie) {
1400                 sel_uuid = wps_get_uuid_e(wps_ie);
1401                 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the selected BSS",
1402                             sel_uuid, UUID_LEN);
1403         } else {
1404                 wpa_printf(MSG_DEBUG, "WPS: Selected BSS does not include "
1405                            "WPS IE?!");
1406                 sel_uuid = NULL;
1407         }
1408
1409         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1410                 struct wpabuf *ie;
1411                 if (bss == selected)
1412                         continue;
1413                 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1414                 if (!ie)
1415                         continue;
1416                 if (!wps_is_selected_pbc_registrar(ie)) {
1417                         wpabuf_free(ie);
1418                         continue;
1419                 }
1420                 wpa_printf(MSG_DEBUG, "WPS: Another BSS in active PBC mode: "
1421                            MACSTR, MAC2STR(bss->bssid));
1422                 uuid = wps_get_uuid_e(ie);
1423                 wpa_hexdump(MSG_DEBUG, "WPS: UUID of the other BSS",
1424                             uuid, UUID_LEN);
1425                 if (sel_uuid == NULL || uuid == NULL ||
1426                     os_memcmp(sel_uuid, uuid, UUID_LEN) != 0) {
1427                         ret = 1; /* PBC overlap */
1428                         wpa_msg(wpa_s, MSG_INFO, "WPS: PBC overlap detected: "
1429                                 MACSTR " and " MACSTR,
1430                                 MAC2STR(selected->bssid),
1431                                 MAC2STR(bss->bssid));
1432                         wpabuf_free(ie);
1433                         break;
1434                 }
1435
1436                 /* TODO: verify that this is reasonable dual-band situation */
1437
1438                 wpabuf_free(ie);
1439         }
1440
1441         wpabuf_free(wps_ie);
1442
1443         return ret;
1444 }
1445
1446
1447 void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
1448 {
1449         struct wpa_bss *bss;
1450         unsigned int pbc = 0, auth = 0, pin = 0, wps = 0;
1451
1452         if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
1453                 return;
1454
1455         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1456                 struct wpabuf *ie;
1457                 ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1458                 if (!ie)
1459                         continue;
1460                 if (wps_is_selected_pbc_registrar(ie))
1461                         pbc++;
1462                 else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
1463                         auth++;
1464                 else if (wps_is_selected_pin_registrar(ie))
1465                         pin++;
1466                 else
1467                         wps++;
1468                 wpabuf_free(ie);
1469         }
1470
1471         if (pbc)
1472                 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
1473         else if (auth)
1474                 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_AUTH);
1475         else if (pin)
1476                 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
1477         else if (wps)
1478                 wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
1479 }
1480
1481
1482 int wpas_wps_searching(struct wpa_supplicant *wpa_s)
1483 {
1484         struct wpa_ssid *ssid;
1485
1486         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1487                 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
1488                         return 1;
1489         }
1490
1491         return 0;
1492 }
1493
1494
1495 int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
1496                               char *end)
1497 {
1498         struct wpabuf *wps_ie;
1499         int ret;
1500
1501         wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
1502         if (wps_ie == NULL)
1503                 return 0;
1504
1505         ret = wps_attr_text(wps_ie, buf, end);
1506         wpabuf_free(wps_ie);
1507         return ret;
1508 }
1509
1510
1511 int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
1512 {
1513 #ifdef CONFIG_WPS_ER
1514         if (wpa_s->wps_er) {
1515                 wps_er_refresh(wpa_s->wps_er);
1516                 return 0;
1517         }
1518         wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
1519         if (wpa_s->wps_er == NULL)
1520                 return -1;
1521         return 0;
1522 #else /* CONFIG_WPS_ER */
1523         return 0;
1524 #endif /* CONFIG_WPS_ER */
1525 }
1526
1527
1528 int wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
1529 {
1530 #ifdef CONFIG_WPS_ER
1531         wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1532         wpa_s->wps_er = NULL;
1533 #endif /* CONFIG_WPS_ER */
1534         return 0;
1535 }
1536
1537
1538 #ifdef CONFIG_WPS_ER
1539 int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
1540                         const char *uuid, const char *pin)
1541 {
1542         u8 u[UUID_LEN];
1543         int any = 0;
1544
1545         if (os_strcmp(uuid, "any") == 0)
1546                 any = 1;
1547         else if (uuid_str2bin(uuid, u))
1548                 return -1;
1549         return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
1550                                      any ? NULL : u,
1551                                      (const u8 *) pin, os_strlen(pin), 300);
1552 }
1553
1554
1555 int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
1556 {
1557         u8 u[UUID_LEN];
1558
1559         if (uuid_str2bin(uuid, u))
1560                 return -1;
1561         return wps_er_pbc(wpa_s->wps_er, u);
1562 }
1563
1564
1565 int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
1566                       const char *pin)
1567 {
1568         u8 u[UUID_LEN];
1569
1570         if (uuid_str2bin(uuid, u))
1571                 return -1;
1572         return wps_er_learn(wpa_s->wps_er, u, (const u8 *) pin,
1573                             os_strlen(pin));
1574 }
1575
1576
1577 int wpas_wps_er_set_config(struct wpa_supplicant *wpa_s, const char *uuid,
1578                            int id)
1579 {
1580         u8 u[UUID_LEN];
1581         struct wpa_ssid *ssid;
1582         struct wps_credential cred;
1583
1584         if (uuid_str2bin(uuid, u))
1585                 return -1;
1586         ssid = wpa_config_get_network(wpa_s->conf, id);
1587         if (ssid == NULL || ssid->ssid == NULL)
1588                 return -1;
1589
1590         os_memset(&cred, 0, sizeof(cred));
1591         if (ssid->ssid_len > 32)
1592                 return -1;
1593         os_memcpy(cred.ssid, ssid->ssid, ssid->ssid_len);
1594         cred.ssid_len = ssid->ssid_len;
1595         if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
1596                 cred.auth_type = (ssid->proto & WPA_PROTO_RSN) ?
1597                         WPS_AUTH_WPA2PSK : WPS_AUTH_WPAPSK;
1598                 if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
1599                         cred.encr_type = WPS_ENCR_AES;
1600                 else
1601                         cred.encr_type = WPS_ENCR_TKIP;
1602                 if (ssid->passphrase) {
1603                         cred.key_len = os_strlen(ssid->passphrase);
1604                         if (cred.key_len >= 64)
1605                                 return -1;
1606                         os_memcpy(cred.key, ssid->passphrase, cred.key_len);
1607                 } else if (ssid->psk_set) {
1608                         cred.key_len = 32;
1609                         os_memcpy(cred.key, ssid->psk, 32);
1610                 } else
1611                         return -1;
1612         } else {
1613                 cred.auth_type = WPS_AUTH_OPEN;
1614                 cred.encr_type = WPS_ENCR_NONE;
1615         }
1616         return wps_er_set_config(wpa_s->wps_er, u, &cred);
1617 }
1618
1619
1620 int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
1621                        const char *pin, struct wps_new_ap_settings *settings)
1622 {
1623         u8 u[UUID_LEN];
1624         struct wps_credential cred;
1625         size_t len;
1626
1627         if (uuid_str2bin(uuid, u))
1628                 return -1;
1629         if (settings->ssid_hex == NULL || settings->auth == NULL ||
1630             settings->encr == NULL || settings->key_hex == NULL)
1631                 return -1;
1632
1633         os_memset(&cred, 0, sizeof(cred));
1634         len = os_strlen(settings->ssid_hex);
1635         if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1636             hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
1637                 return -1;
1638         cred.ssid_len = len / 2;
1639
1640         len = os_strlen(settings->key_hex);
1641         if ((len & 1) || len > 2 * sizeof(cred.key) ||
1642             hexstr2bin(settings->key_hex, cred.key, len / 2))
1643                 return -1;
1644         cred.key_len = len / 2;
1645
1646         if (os_strcmp(settings->auth, "OPEN") == 0)
1647                 cred.auth_type = WPS_AUTH_OPEN;
1648         else if (os_strcmp(settings->auth, "WPAPSK") == 0)
1649                 cred.auth_type = WPS_AUTH_WPAPSK;
1650         else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
1651                 cred.auth_type = WPS_AUTH_WPA2PSK;
1652         else
1653                 return -1;
1654
1655         if (os_strcmp(settings->encr, "NONE") == 0)
1656                 cred.encr_type = WPS_ENCR_NONE;
1657         else if (os_strcmp(settings->encr, "WEP") == 0)
1658                 cred.encr_type = WPS_ENCR_WEP;
1659         else if (os_strcmp(settings->encr, "TKIP") == 0)
1660                 cred.encr_type = WPS_ENCR_TKIP;
1661         else if (os_strcmp(settings->encr, "CCMP") == 0)
1662                 cred.encr_type = WPS_ENCR_AES;
1663         else
1664                 return -1;
1665
1666         return wps_er_config(wpa_s->wps_er, u, (const u8 *) pin,
1667                              os_strlen(pin), &cred);
1668 }
1669
1670
1671 #ifdef CONFIG_WPS_NFC
1672 struct wpabuf * wpas_wps_er_nfc_config_token(struct wpa_supplicant *wpa_s,
1673                                              int ndef, const char *uuid)
1674 {
1675         struct wpabuf *ret;
1676         u8 u[UUID_LEN];
1677
1678         if (!wpa_s->wps_er)
1679                 return NULL;
1680
1681         if (uuid_str2bin(uuid, u))
1682                 return NULL;
1683
1684         ret = wps_er_nfc_config_token(wpa_s->wps_er, u);
1685         if (ndef && ret) {
1686                 struct wpabuf *tmp;
1687                 tmp = ndef_build_wifi(ret);
1688                 wpabuf_free(ret);
1689                 if (tmp == NULL)
1690                         return NULL;
1691                 ret = tmp;
1692         }
1693
1694         return ret;
1695 }
1696 #endif /* CONFIG_WPS_NFC */
1697
1698
1699 static int callbacks_pending = 0;
1700
1701 static void wpas_wps_terminate_cb(void *ctx)
1702 {
1703         wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
1704         if (--callbacks_pending <= 0)
1705                 eloop_terminate();
1706 }
1707 #endif /* CONFIG_WPS_ER */
1708
1709
1710 int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
1711 {
1712 #ifdef CONFIG_WPS_ER
1713         if (wpa_s->wps_er) {
1714                 callbacks_pending++;
1715                 wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
1716                 wpa_s->wps_er = NULL;
1717                 return 1;
1718         }
1719 #endif /* CONFIG_WPS_ER */
1720         return 0;
1721 }
1722
1723
1724 int wpas_wps_in_progress(struct wpa_supplicant *wpa_s)
1725 {
1726         struct wpa_ssid *ssid;
1727
1728         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1729                 if (!ssid->disabled && ssid->key_mgmt == WPA_KEY_MGMT_WPS)
1730                         return 1;
1731         }
1732
1733         return 0;
1734 }
1735
1736
1737 void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
1738 {
1739         struct wps_context *wps = wpa_s->wps;
1740
1741         if (wps == NULL)
1742                 return;
1743
1744         if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
1745                 wps->config_methods = wps_config_methods_str2bin(
1746                         wpa_s->conf->config_methods);
1747                 if ((wps->config_methods &
1748                      (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1749                     (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1750                         wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
1751                                    "config methods are not allowed at the "
1752                                    "same time");
1753                         wps->config_methods &= ~WPS_CONFIG_LABEL;
1754                 }
1755         }
1756         wps->config_methods = wps_fix_config_methods(wps->config_methods);
1757         wps->dev.config_methods = wps->config_methods;
1758
1759         if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
1760                 os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
1761                           WPS_DEV_TYPE_LEN);
1762
1763         if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
1764                 wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
1765                 os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
1766                           wps->dev.num_sec_dev_types * WPS_DEV_TYPE_LEN);
1767         }
1768
1769         if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION)
1770                 wpas_wps_set_vendor_ext_m1(wpa_s, wps);
1771
1772         if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
1773                 wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
1774
1775         if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)
1776                 wpas_wps_set_uuid(wpa_s, wps);
1777
1778         if (wpa_s->conf->changed_parameters &
1779             (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
1780                 /* Update pointers to make sure they refer current values */
1781                 wps->dev.device_name = wpa_s->conf->device_name;
1782                 wps->dev.manufacturer = wpa_s->conf->manufacturer;
1783                 wps->dev.model_name = wpa_s->conf->model_name;
1784                 wps->dev.model_number = wpa_s->conf->model_number;
1785                 wps->dev.serial_number = wpa_s->conf->serial_number;
1786         }
1787 }
1788
1789
1790 #ifdef CONFIG_WPS_NFC
1791
1792 struct wpabuf * wpas_wps_nfc_token(struct wpa_supplicant *wpa_s, int ndef)
1793 {
1794         return wps_nfc_token_gen(ndef, &wpa_s->conf->wps_nfc_dev_pw_id,
1795                                  &wpa_s->conf->wps_nfc_dh_pubkey,
1796                                  &wpa_s->conf->wps_nfc_dh_privkey,
1797                                  &wpa_s->conf->wps_nfc_dev_pw);
1798 }
1799
1800
1801 int wpas_wps_start_nfc(struct wpa_supplicant *wpa_s, const u8 *bssid)
1802 {
1803         struct wps_context *wps = wpa_s->wps;
1804         char pw[32 * 2 + 1];
1805
1806         if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
1807             wpa_s->conf->wps_nfc_dh_privkey == NULL ||
1808             wpa_s->conf->wps_nfc_dev_pw == NULL)
1809                 return -1;
1810
1811         dh5_free(wps->dh_ctx);
1812         wpabuf_free(wps->dh_pubkey);
1813         wpabuf_free(wps->dh_privkey);
1814         wps->dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
1815         wps->dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
1816         if (wps->dh_privkey == NULL || wps->dh_pubkey == NULL) {
1817                 wps->dh_ctx = NULL;
1818                 wpabuf_free(wps->dh_pubkey);
1819                 wps->dh_pubkey = NULL;
1820                 wpabuf_free(wps->dh_privkey);
1821                 wps->dh_privkey = NULL;
1822                 return -1;
1823         }
1824         wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, wps->dh_pubkey);
1825         if (wps->dh_ctx == NULL)
1826                 return -1;
1827
1828         wpa_snprintf_hex_uppercase(pw, sizeof(pw),
1829                                    wpabuf_head(wpa_s->conf->wps_nfc_dev_pw),
1830                                    wpabuf_len(wpa_s->conf->wps_nfc_dev_pw));
1831         return wpas_wps_start_pin(wpa_s, bssid, pw, 0,
1832                                   wpa_s->conf->wps_nfc_dev_pw_id);
1833 }
1834
1835
1836 static int wpas_wps_use_cred(struct wpa_supplicant *wpa_s,
1837                              struct wps_parse_attr *attr)
1838 {
1839         if (wps_oob_use_cred(wpa_s->wps, attr) < 0)
1840                 return -1;
1841
1842         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
1843                 return 0;
1844
1845         wpa_printf(MSG_DEBUG, "WPS: Request reconnection with new network "
1846                    "based on the received credential added");
1847         wpa_s->normal_scans = 0;
1848         wpa_supplicant_reinit_autoscan(wpa_s);
1849         wpa_s->disconnected = 0;
1850         wpa_s->reassociate = 1;
1851         wpa_supplicant_req_scan(wpa_s, 0, 0);
1852
1853         return 0;
1854 }
1855
1856
1857 static int wpas_wps_add_nfc_password_token(struct wpa_supplicant *wpa_s,
1858                                            struct wps_parse_attr *attr)
1859 {
1860         return wps_registrar_add_nfc_password_token(
1861                 wpa_s->wps->registrar, attr->oob_dev_password,
1862                 attr->oob_dev_password_len);
1863 }
1864
1865
1866 static int wpas_wps_nfc_tag_process(struct wpa_supplicant *wpa_s,
1867                                     const struct wpabuf *wps)
1868 {
1869         struct wps_parse_attr attr;
1870
1871         wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
1872
1873         if (wps_parse_msg(wps, &attr)) {
1874                 wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
1875                 return -1;
1876         }
1877
1878         if (attr.num_cred)
1879                 return wpas_wps_use_cred(wpa_s, &attr);
1880
1881 #ifdef CONFIG_WPS_ER
1882         if (attr.oob_dev_password)
1883                 return wpas_wps_add_nfc_password_token(wpa_s, &attr);
1884 #endif /* CONFIG_WPS_ER */
1885
1886         wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
1887         return -1;
1888 }
1889
1890
1891 int wpas_wps_nfc_tag_read(struct wpa_supplicant *wpa_s,
1892                           const struct wpabuf *data)
1893 {
1894         const struct wpabuf *wps = data;
1895         struct wpabuf *tmp = NULL;
1896         int ret;
1897
1898         if (wpabuf_len(data) < 4)
1899                 return -1;
1900
1901         if (*wpabuf_head_u8(data) != 0x10) {
1902                 /* Assume this contains full NDEF record */
1903                 tmp = ndef_parse_wifi(data);
1904                 if (tmp == NULL) {
1905                         wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
1906                         return -1;
1907                 }
1908                 wps = tmp;
1909         }
1910
1911         ret = wpas_wps_nfc_tag_process(wpa_s, wps);
1912         wpabuf_free(tmp);
1913         return ret;
1914 }
1915
1916 #endif /* CONFIG_WPS_NFC */