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