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