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