LCOV - code coverage report
Current view: top level - src/ap - wps_hostapd.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1393793999 Lines: 763 1119 68.2 %
Date: 2014-03-02 Functions: 67 71 94.4 %
Branches: 276 608 45.4 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * hostapd / 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 "utils/includes.h"
      10                 :            : 
      11                 :            : #include "utils/common.h"
      12                 :            : #include "utils/eloop.h"
      13                 :            : #include "utils/uuid.h"
      14                 :            : #include "common/wpa_ctrl.h"
      15                 :            : #include "common/ieee802_11_defs.h"
      16                 :            : #include "common/ieee802_11_common.h"
      17                 :            : #include "eapol_auth/eapol_auth_sm.h"
      18                 :            : #include "eapol_auth/eapol_auth_sm_i.h"
      19                 :            : #include "wps/wps.h"
      20                 :            : #include "wps/wps_defs.h"
      21                 :            : #include "wps/wps_dev_attr.h"
      22                 :            : #include "wps/wps_attr_parse.h"
      23                 :            : #include "hostapd.h"
      24                 :            : #include "ap_config.h"
      25                 :            : #include "ap_drv_ops.h"
      26                 :            : #include "beacon.h"
      27                 :            : #include "sta_info.h"
      28                 :            : #include "wps_hostapd.h"
      29                 :            : 
      30                 :            : 
      31                 :            : #ifdef CONFIG_WPS_UPNP
      32                 :            : #include "wps/wps_upnp.h"
      33                 :            : static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
      34                 :            :                                  struct wps_context *wps);
      35                 :            : static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd);
      36                 :            : #endif /* CONFIG_WPS_UPNP */
      37                 :            : 
      38                 :            : static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr, const u8 *da,
      39                 :            :                                     const u8 *bssid,
      40                 :            :                                     const u8 *ie, size_t ie_len,
      41                 :            :                                     int ssi_signal);
      42                 :            : static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx);
      43                 :            : 
      44                 :            : 
      45                 :            : struct wps_for_each_data {
      46                 :            :         int (*func)(struct hostapd_data *h, void *ctx);
      47                 :            :         void *ctx;
      48                 :            :         struct hostapd_data *calling_hapd;
      49                 :            : };
      50                 :            : 
      51                 :            : 
      52                 :        252 : static int wps_for_each(struct hostapd_iface *iface, void *ctx)
      53                 :            : {
      54                 :        252 :         struct wps_for_each_data *data = ctx;
      55                 :            :         size_t j;
      56                 :            : 
      57         [ -  + ]:        252 :         if (iface == NULL)
      58                 :          0 :                 return 0;
      59         [ +  + ]:        503 :         for (j = 0; j < iface->num_bss; j++) {
      60                 :        252 :                 struct hostapd_data *hapd = iface->bss[j];
      61                 :            :                 int ret;
      62                 :            : 
      63 [ +  + ][ +  + ]:        252 :                 if (hapd != data->calling_hapd &&
      64         [ -  + ]:          6 :                     (hapd->conf->wps_independent ||
      65                 :          6 :                      data->calling_hapd->conf->wps_independent))
      66                 :          2 :                         continue;
      67                 :            : 
      68                 :        250 :                 ret = data->func(hapd, data->ctx);
      69         [ +  + ]:        250 :                 if (ret)
      70                 :          1 :                         return ret;
      71                 :            :         }
      72                 :            : 
      73                 :        252 :         return 0;
      74                 :            : }
      75                 :            : 
      76                 :            : 
      77                 :        244 : static int hostapd_wps_for_each(struct hostapd_data *hapd,
      78                 :            :                                 int (*func)(struct hostapd_data *h, void *ctx),
      79                 :            :                                 void *ctx)
      80                 :            : {
      81                 :        244 :         struct hostapd_iface *iface = hapd->iface;
      82                 :            :         struct wps_for_each_data data;
      83                 :        244 :         data.func = func;
      84                 :        244 :         data.ctx = ctx;
      85                 :        244 :         data.calling_hapd = hapd;
      86 [ +  + ][ -  + ]:        244 :         if (iface->interfaces == NULL ||
      87                 :         89 :             iface->interfaces->for_each_interface == NULL)
      88                 :        155 :                 return wps_for_each(iface, &data);
      89                 :        244 :         return iface->interfaces->for_each_interface(iface->interfaces,
      90                 :            :                                                      wps_for_each, &data);
      91                 :            : }
      92                 :            : 
      93                 :            : 
      94                 :          8 : static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr,
      95                 :            :                                   const u8 *p2p_dev_addr, const u8 *psk,
      96                 :            :                                   size_t psk_len)
      97                 :            : {
      98                 :          8 :         struct hostapd_data *hapd = ctx;
      99                 :            :         struct hostapd_wpa_psk *p;
     100                 :          8 :         struct hostapd_ssid *ssid = &hapd->conf->ssid;
     101                 :            : 
     102         [ +  + ]:          8 :         if (is_zero_ether_addr(p2p_dev_addr)) {
     103                 :          1 :                 wpa_printf(MSG_DEBUG,
     104                 :            :                            "Received new WPA/WPA2-PSK from WPS for STA " MACSTR,
     105                 :          6 :                            MAC2STR(mac_addr));
     106                 :            :         } else {
     107                 :          7 :                 wpa_printf(MSG_DEBUG,
     108                 :            :                            "Received new WPA/WPA2-PSK from WPS for STA " MACSTR
     109                 :            :                            " P2P Device Addr " MACSTR,
     110                 :         84 :                            MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
     111                 :            :         }
     112                 :          8 :         wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
     113                 :            : 
     114         [ -  + ]:          8 :         if (psk_len != PMK_LEN) {
     115                 :          0 :                 wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu",
     116                 :            :                            (unsigned long) psk_len);
     117                 :          0 :                 return -1;
     118                 :            :         }
     119                 :            : 
     120                 :            :         /* Add the new PSK to runtime PSK list */
     121                 :          8 :         p = os_zalloc(sizeof(*p));
     122         [ -  + ]:          8 :         if (p == NULL)
     123                 :          0 :                 return -1;
     124                 :          8 :         os_memcpy(p->addr, mac_addr, ETH_ALEN);
     125                 :          8 :         os_memcpy(p->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
     126                 :          8 :         os_memcpy(p->psk, psk, PMK_LEN);
     127                 :            : 
     128         [ +  - ]:          8 :         if (hapd->new_psk_cb) {
     129                 :          8 :                 hapd->new_psk_cb(hapd->new_psk_cb_ctx, mac_addr, p2p_dev_addr,
     130                 :            :                                  psk, psk_len);
     131                 :            :         }
     132                 :            : 
     133                 :          8 :         p->next = ssid->wpa_psk;
     134                 :          8 :         ssid->wpa_psk = p;
     135                 :            : 
     136         [ -  + ]:          8 :         if (ssid->wpa_psk_file) {
     137                 :            :                 FILE *f;
     138                 :            :                 char hex[PMK_LEN * 2 + 1];
     139                 :            :                 /* Add the new PSK to PSK list file */
     140                 :          0 :                 f = fopen(ssid->wpa_psk_file, "a");
     141         [ #  # ]:          0 :                 if (f == NULL) {
     142                 :          0 :                         wpa_printf(MSG_DEBUG, "Failed to add the PSK to "
     143                 :            :                                    "'%s'", ssid->wpa_psk_file);
     144                 :          0 :                         return -1;
     145                 :            :                 }
     146                 :            : 
     147                 :          0 :                 wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len);
     148                 :          0 :                 fprintf(f, MACSTR " %s\n", MAC2STR(mac_addr), hex);
     149                 :          0 :                 fclose(f);
     150                 :            :         }
     151                 :            : 
     152                 :          8 :         return 0;
     153                 :            : }
     154                 :            : 
     155                 :            : 
     156                 :        489 : static int hostapd_wps_set_ie_cb(void *ctx, struct wpabuf *beacon_ie,
     157                 :            :                                  struct wpabuf *probe_resp_ie)
     158                 :            : {
     159                 :        489 :         struct hostapd_data *hapd = ctx;
     160                 :        489 :         wpabuf_free(hapd->wps_beacon_ie);
     161                 :        489 :         hapd->wps_beacon_ie = beacon_ie;
     162                 :        489 :         wpabuf_free(hapd->wps_probe_resp_ie);
     163                 :        489 :         hapd->wps_probe_resp_ie = probe_resp_ie;
     164         [ +  + ]:        489 :         if (hapd->beacon_set_done)
     165                 :        353 :                 ieee802_11_set_beacon(hapd);
     166                 :        489 :         return hostapd_set_ap_wps_ie(hapd);
     167                 :            : }
     168                 :            : 
     169                 :            : 
     170                 :          8 : static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
     171                 :            :                                       const struct wps_device_data *dev)
     172                 :            : {
     173                 :          8 :         struct hostapd_data *hapd = ctx;
     174                 :            :         char uuid[40], txt[400];
     175                 :            :         int len;
     176                 :            :         char devtype[WPS_DEV_TYPE_BUFSIZE];
     177         [ -  + ]:          8 :         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
     178                 :          0 :                 return;
     179                 :          8 :         wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid);
     180                 :          8 :         len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED
     181                 :            :                           "%s " MACSTR " [%s|%s|%s|%s|%s|%s]",
     182                 :         48 :                           uuid, MAC2STR(dev->mac_addr), dev->device_name,
     183                 :            :                           dev->manufacturer, dev->model_name,
     184                 :            :                           dev->model_number, dev->serial_number,
     185                 :          8 :                           wps_dev_type_bin2str(dev->pri_dev_type, devtype,
     186                 :            :                                                sizeof(devtype)));
     187 [ +  - ][ +  - ]:          8 :         if (len > 0 && len < (int) sizeof(txt))
     188                 :          8 :                 wpa_msg(hapd->msg_ctx, MSG_INFO, "%s", txt);
     189                 :            : 
     190         [ -  + ]:          8 :         if (hapd->conf->wps_pin_requests) {
     191                 :            :                 FILE *f;
     192                 :            :                 struct os_time t;
     193                 :          0 :                 f = fopen(hapd->conf->wps_pin_requests, "a");
     194         [ #  # ]:          0 :                 if (f == NULL)
     195                 :          0 :                         return;
     196                 :          0 :                 os_get_time(&t);
     197                 :          0 :                 fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s"
     198                 :            :                         "\t%s\n",
     199                 :          0 :                         t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name,
     200                 :            :                         dev->manufacturer, dev->model_name, dev->model_number,
     201                 :            :                         dev->serial_number,
     202                 :          0 :                         wps_dev_type_bin2str(dev->pri_dev_type, devtype,
     203                 :            :                                              sizeof(devtype)));
     204                 :          8 :                 fclose(f);
     205                 :            :         }
     206                 :            : }
     207                 :            : 
     208                 :            : 
     209                 :            : struct wps_stop_reg_data {
     210                 :            :         struct hostapd_data *current_hapd;
     211                 :            :         const u8 *uuid_e;
     212                 :            :         const u8 *dev_pw;
     213                 :            :         size_t dev_pw_len;
     214                 :            : };
     215                 :            : 
     216                 :        102 : static int wps_stop_registrar(struct hostapd_data *hapd, void *ctx)
     217                 :            : {
     218                 :        102 :         struct wps_stop_reg_data *data = ctx;
     219 [ +  + ][ +  - ]:        102 :         if (hapd != data->current_hapd && hapd->wps != NULL)
     220                 :          2 :                 wps_registrar_complete(hapd->wps->registrar, data->uuid_e,
     221                 :            :                                        data->dev_pw, data->dev_pw_len);
     222                 :        102 :         return 0;
     223                 :            : }
     224                 :            : 
     225                 :            : 
     226                 :        100 : static void hostapd_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
     227                 :            :                                        const u8 *uuid_e, const u8 *dev_pw,
     228                 :            :                                        size_t dev_pw_len)
     229                 :            : {
     230                 :        100 :         struct hostapd_data *hapd = ctx;
     231                 :            :         char uuid[40];
     232                 :            :         struct wps_stop_reg_data data;
     233         [ -  + ]:        100 :         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
     234                 :        100 :                 return;
     235                 :        100 :         wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR " %s",
     236                 :        600 :                 MAC2STR(mac_addr), uuid);
     237         [ +  + ]:        100 :         if (hapd->wps_reg_success_cb)
     238                 :         81 :                 hapd->wps_reg_success_cb(hapd->wps_reg_success_cb_ctx,
     239                 :            :                                          mac_addr, uuid_e);
     240                 :        100 :         data.current_hapd = hapd;
     241                 :        100 :         data.uuid_e = uuid_e;
     242                 :        100 :         data.dev_pw = dev_pw;
     243                 :        100 :         data.dev_pw_len = dev_pw_len;
     244                 :        100 :         hostapd_wps_for_each(hapd, wps_stop_registrar, &data);
     245                 :            : }
     246                 :            : 
     247                 :            : 
     248                 :        229 : static void hostapd_wps_enrollee_seen_cb(void *ctx, const u8 *addr,
     249                 :            :                                          const u8 *uuid_e,
     250                 :            :                                          const u8 *pri_dev_type,
     251                 :            :                                          u16 config_methods,
     252                 :            :                                          u16 dev_password_id, u8 request_type,
     253                 :            :                                          const char *dev_name)
     254                 :            : {
     255                 :        229 :         struct hostapd_data *hapd = ctx;
     256                 :            :         char uuid[40];
     257                 :            :         char devtype[WPS_DEV_TYPE_BUFSIZE];
     258         [ -  + ]:        229 :         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
     259                 :        229 :                 return;
     260         [ -  + ]:        229 :         if (dev_name == NULL)
     261                 :          0 :                 dev_name = "";
     262                 :        229 :         wpa_msg_ctrl(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ENROLLEE_SEEN MACSTR
     263                 :            :                      " %s %s 0x%x %u %u [%s]",
     264                 :       1374 :                      MAC2STR(addr), uuid,
     265                 :            :                      wps_dev_type_bin2str(pri_dev_type, devtype,
     266                 :            :                                           sizeof(devtype)),
     267                 :            :                      config_methods, dev_password_id, request_type, dev_name);
     268                 :            : }
     269                 :            : 
     270                 :            : 
     271                 :          0 : static int str_starts(const char *str, const char *start)
     272                 :            : {
     273                 :          0 :         return os_strncmp(str, start, os_strlen(start)) == 0;
     274                 :            : }
     275                 :            : 
     276                 :            : 
     277                 :         14 : static void wps_reload_config(void *eloop_data, void *user_ctx)
     278                 :            : {
     279                 :         14 :         struct hostapd_iface *iface = eloop_data;
     280                 :            : 
     281                 :         14 :         wpa_printf(MSG_DEBUG, "WPS: Reload configuration data");
     282   [ +  -  -  + ]:         28 :         if (iface->interfaces == NULL ||
     283                 :         14 :             iface->interfaces->reload_config(iface) < 0) {
     284                 :          0 :                 wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated "
     285                 :            :                            "configuration");
     286                 :            :         }
     287                 :         14 : }
     288                 :            : 
     289                 :            : 
     290                 :        170 : void hostapd_wps_eap_completed(struct hostapd_data *hapd)
     291                 :            : {
     292                 :            :         /*
     293                 :            :          * Reduce race condition of the station trying to reconnect immediately
     294                 :            :          * after AP reconfiguration through WPS by rescheduling the reload
     295                 :            :          * timeout to happen after EAP completion rather than the originally
     296                 :            :          * scheduled 100 ms after new configuration became known.
     297                 :            :          */
     298         [ +  + ]:        170 :         if (eloop_deplete_timeout(0, 0, wps_reload_config, hapd->iface, NULL) ==
     299                 :            :             1)
     300                 :          9 :                 wpa_printf(MSG_DEBUG, "WPS: Reschedule immediate configuration reload");
     301                 :        170 : }
     302                 :            : 
     303                 :            : 
     304                 :          0 : static void hapd_new_ap_event(struct hostapd_data *hapd, const u8 *attr,
     305                 :            :                               size_t attr_len)
     306                 :            : {
     307                 :          0 :         size_t blen = attr_len * 2 + 1;
     308                 :          0 :         char *buf = os_malloc(blen);
     309         [ #  # ]:          0 :         if (buf) {
     310                 :          0 :                 wpa_snprintf_hex(buf, blen, attr, attr_len);
     311                 :          0 :                 wpa_msg(hapd->msg_ctx, MSG_INFO,
     312                 :            :                         WPS_EVENT_NEW_AP_SETTINGS "%s", buf);
     313                 :          0 :                 os_free(buf);
     314                 :            :         }
     315                 :          0 : }
     316                 :            : 
     317                 :            : 
     318                 :         14 : static int hapd_wps_reconfig_in_memory(struct hostapd_data *hapd,
     319                 :            :                                        const struct wps_credential *cred)
     320                 :            : {
     321                 :         14 :         struct hostapd_bss_config *bss = hapd->conf;
     322                 :            : 
     323                 :         14 :         wpa_printf(MSG_DEBUG, "WPS: Updating in-memory configuration");
     324                 :            : 
     325                 :         14 :         bss->wps_state = 2;
     326         [ +  - ]:         14 :         if (cred->ssid_len <= HOSTAPD_MAX_SSID_LEN) {
     327                 :         14 :                 os_memcpy(bss->ssid.ssid, cred->ssid, cred->ssid_len);
     328                 :         14 :                 bss->ssid.ssid_len = cred->ssid_len;
     329                 :         14 :                 bss->ssid.ssid_set = 1;
     330                 :            :         }
     331                 :            : 
     332 [ +  - ][ +  + ]:         14 :         if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
     333                 :         14 :             (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
     334                 :          9 :                 bss->wpa = 3;
     335         [ +  - ]:          5 :         else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
     336                 :          5 :                 bss->wpa = 2;
     337         [ #  # ]:          0 :         else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
     338                 :          0 :                 bss->wpa = 1;
     339                 :            :         else
     340                 :          0 :                 bss->wpa = 0;
     341                 :            : 
     342         [ +  - ]:         14 :         if (bss->wpa) {
     343         [ -  + ]:         14 :                 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA))
     344                 :          0 :                         bss->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X;
     345         [ +  - ]:         14 :                 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
     346                 :         14 :                         bss->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
     347                 :            : 
     348                 :         14 :                 bss->wpa_pairwise = 0;
     349         [ +  - ]:         14 :                 if (cred->encr_type & WPS_ENCR_AES)
     350                 :         14 :                         bss->wpa_pairwise |= WPA_CIPHER_CCMP;
     351         [ +  + ]:         14 :                 if (cred->encr_type & WPS_ENCR_TKIP)
     352                 :          9 :                         bss->wpa_pairwise |= WPA_CIPHER_TKIP;
     353                 :         14 :                 bss->rsn_pairwise = bss->wpa_pairwise;
     354                 :         14 :                 bss->wpa_group = wpa_select_ap_group_cipher(bss->wpa,
     355                 :            :                                                             bss->wpa_pairwise,
     356                 :            :                                                             bss->rsn_pairwise);
     357                 :            : 
     358 [ +  - ][ +  - ]:         14 :                 if (cred->key_len >= 8 && cred->key_len < 64) {
     359                 :         14 :                         os_free(bss->ssid.wpa_passphrase);
     360                 :         14 :                         bss->ssid.wpa_passphrase = os_zalloc(cred->key_len + 1);
     361         [ +  - ]:         14 :                         if (bss->ssid.wpa_passphrase)
     362                 :         14 :                                 os_memcpy(bss->ssid.wpa_passphrase, cred->key,
     363                 :            :                                           cred->key_len);
     364                 :         14 :                         os_free(bss->ssid.wpa_psk);
     365                 :         14 :                         bss->ssid.wpa_psk = NULL;
     366         [ #  # ]:          0 :                 } else if (cred->key_len == 64) {
     367                 :          0 :                         os_free(bss->ssid.wpa_psk);
     368                 :          0 :                         bss->ssid.wpa_psk =
     369                 :          0 :                                 os_zalloc(sizeof(struct hostapd_wpa_psk));
     370   [ #  #  #  # ]:          0 :                         if (bss->ssid.wpa_psk &&
     371                 :          0 :                             hexstr2bin((const char *) cred->key,
     372                 :          0 :                                        bss->ssid.wpa_psk->psk, PMK_LEN) == 0) {
     373                 :          0 :                                 bss->ssid.wpa_psk->group = 1;
     374                 :          0 :                                 os_free(bss->ssid.wpa_passphrase);
     375                 :          0 :                                 bss->ssid.wpa_passphrase = NULL;
     376                 :            :                         }
     377                 :            :                 }
     378                 :         14 :                 bss->auth_algs = 1;
     379                 :            :         } else {
     380 [ #  # ][ #  # ]:          0 :                 if ((cred->auth_type & WPS_AUTH_OPEN) &&
     381                 :          0 :                     (cred->auth_type & WPS_AUTH_SHARED))
     382                 :          0 :                         bss->auth_algs = 3;
     383         [ #  # ]:          0 :                 else if (cred->auth_type & WPS_AUTH_SHARED)
     384                 :          0 :                         bss->auth_algs = 2;
     385                 :            :                 else
     386                 :          0 :                         bss->auth_algs = 1;
     387 [ #  # ][ #  # ]:          0 :                 if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx > 0 &&
                 [ #  # ]
     388                 :          0 :                     cred->key_idx <= 4) {
     389                 :          0 :                         struct hostapd_wep_keys *wep = &bss->ssid.wep;
     390                 :          0 :                         int idx = cred->key_idx;
     391         [ #  # ]:          0 :                         if (idx)
     392                 :          0 :                                 idx--;
     393                 :          0 :                         wep->idx = idx;
     394 [ #  # ][ #  # ]:          0 :                         if (cred->key_len == 10 || cred->key_len == 26) {
     395                 :          0 :                                 os_free(wep->key[idx]);
     396                 :          0 :                                 wep->key[idx] = os_malloc(cred->key_len / 2);
     397   [ #  #  #  # ]:          0 :                                 if (wep->key[idx] == NULL ||
     398                 :          0 :                                     hexstr2bin((const char *) cred->key,
     399                 :            :                                                wep->key[idx],
     400                 :          0 :                                                cred->key_len / 2))
     401                 :          0 :                                         return -1;
     402                 :          0 :                                 wep->len[idx] = cred->key_len / 2;
     403                 :            :                         } else {
     404                 :          0 :                                 os_free(wep->key[idx]);
     405                 :          0 :                                 wep->key[idx] = os_malloc(cred->key_len);
     406         [ #  # ]:          0 :                                 if (wep->key[idx] == NULL)
     407                 :          0 :                                         return -1;
     408                 :          0 :                                 os_memcpy(wep->key[idx], cred->key,
     409                 :            :                                           cred->key_len);
     410                 :          0 :                                 wep->len[idx] = cred->key_len;
     411                 :            :                         }
     412                 :          0 :                         wep->keys_set = 1;
     413                 :            :                 }
     414                 :            :         }
     415                 :            : 
     416                 :            :         /* Schedule configuration reload after short period of time to allow
     417                 :            :          * EAP-WSC to be finished.
     418                 :            :          */
     419                 :         14 :         eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
     420                 :            :                                NULL);
     421                 :            : 
     422                 :         14 :         return 0;
     423                 :            : }
     424                 :            : 
     425                 :            : 
     426                 :         14 : static int hapd_wps_cred_cb(struct hostapd_data *hapd, void *ctx)
     427                 :            : {
     428                 :         14 :         const struct wps_credential *cred = ctx;
     429                 :            :         FILE *oconf, *nconf;
     430                 :            :         size_t len, i;
     431                 :            :         char *tmp_fname;
     432                 :            :         char buf[1024];
     433                 :            :         int multi_bss;
     434                 :            :         int wpa;
     435                 :            : 
     436         [ -  + ]:         14 :         if (hapd->wps == NULL)
     437                 :          0 :                 return 0;
     438                 :            : 
     439                 :         14 :         wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
     440                 :         14 :                         cred->cred_attr, cred->cred_attr_len);
     441                 :            : 
     442                 :         14 :         wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
     443                 :         14 :         wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
     444                 :         14 :         wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
     445                 :         14 :                    cred->auth_type);
     446                 :         14 :         wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
     447                 :         14 :         wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
     448                 :         14 :         wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
     449                 :         14 :                         cred->key, cred->key_len);
     450                 :         14 :         wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
     451                 :         84 :                    MAC2STR(cred->mac_addr));
     452                 :            : 
     453 [ -  + ][ +  - ]:         14 :         if ((hapd->conf->wps_cred_processing == 1 ||
     454         [ #  # ]:          0 :              hapd->conf->wps_cred_processing == 2) && cred->cred_attr) {
     455                 :          0 :                 hapd_new_ap_event(hapd, cred->cred_attr, cred->cred_attr_len);
     456 [ +  - ][ -  + ]:         14 :         } else if (hapd->conf->wps_cred_processing == 1 ||
     457                 :          0 :                    hapd->conf->wps_cred_processing == 2) {
     458                 :            :                 struct wpabuf *attr;
     459                 :          0 :                 attr = wpabuf_alloc(200);
     460 [ #  # ][ #  # ]:          0 :                 if (attr && wps_build_credential_wrap(attr, cred) == 0)
     461                 :          0 :                         hapd_new_ap_event(hapd, wpabuf_head_u8(attr),
     462                 :            :                                           wpabuf_len(attr));
     463                 :          0 :                 wpabuf_free(attr);
     464                 :            :         } else
     465                 :         14 :                 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS);
     466                 :            : 
     467         [ -  + ]:         14 :         if (hapd->conf->wps_cred_processing == 1)
     468                 :          0 :                 return 0;
     469                 :            : 
     470                 :         14 :         os_memcpy(hapd->wps->ssid, cred->ssid, cred->ssid_len);
     471                 :         14 :         hapd->wps->ssid_len = cred->ssid_len;
     472                 :         14 :         hapd->wps->encr_types = cred->encr_type;
     473                 :         14 :         hapd->wps->auth_types = cred->auth_type;
     474                 :         14 :         hapd->wps->ap_encr_type = cred->encr_type;
     475                 :         14 :         hapd->wps->ap_auth_type = cred->auth_type;
     476         [ -  + ]:         14 :         if (cred->key_len == 0) {
     477                 :          0 :                 os_free(hapd->wps->network_key);
     478                 :          0 :                 hapd->wps->network_key = NULL;
     479                 :          0 :                 hapd->wps->network_key_len = 0;
     480                 :            :         } else {
     481 [ +  + ][ +  - ]:         14 :                 if (hapd->wps->network_key == NULL ||
     482                 :          1 :                     hapd->wps->network_key_len < cred->key_len) {
     483                 :         14 :                         hapd->wps->network_key_len = 0;
     484                 :         14 :                         os_free(hapd->wps->network_key);
     485                 :         14 :                         hapd->wps->network_key = os_malloc(cred->key_len);
     486         [ -  + ]:         14 :                         if (hapd->wps->network_key == NULL)
     487                 :          0 :                                 return -1;
     488                 :            :                 }
     489                 :         14 :                 hapd->wps->network_key_len = cred->key_len;
     490                 :         14 :                 os_memcpy(hapd->wps->network_key, cred->key, cred->key_len);
     491                 :            :         }
     492                 :         14 :         hapd->wps->wps_state = WPS_STATE_CONFIGURED;
     493                 :            : 
     494         [ +  - ]:         14 :         if (hapd->iface->config_fname == NULL)
     495                 :         14 :                 return hapd_wps_reconfig_in_memory(hapd, cred);
     496                 :          0 :         len = os_strlen(hapd->iface->config_fname) + 5;
     497                 :          0 :         tmp_fname = os_malloc(len);
     498         [ #  # ]:          0 :         if (tmp_fname == NULL)
     499                 :          0 :                 return -1;
     500                 :          0 :         os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
     501                 :            : 
     502                 :          0 :         oconf = fopen(hapd->iface->config_fname, "r");
     503         [ #  # ]:          0 :         if (oconf == NULL) {
     504                 :          0 :                 wpa_printf(MSG_WARNING, "WPS: Could not open current "
     505                 :            :                            "configuration file");
     506                 :          0 :                 os_free(tmp_fname);
     507                 :          0 :                 return -1;
     508                 :            :         }
     509                 :            : 
     510                 :          0 :         nconf = fopen(tmp_fname, "w");
     511         [ #  # ]:          0 :         if (nconf == NULL) {
     512                 :          0 :                 wpa_printf(MSG_WARNING, "WPS: Could not write updated "
     513                 :            :                            "configuration file");
     514                 :          0 :                 os_free(tmp_fname);
     515                 :          0 :                 fclose(oconf);
     516                 :          0 :                 return -1;
     517                 :            :         }
     518                 :            : 
     519                 :          0 :         fprintf(nconf, "# WPS configuration - START\n");
     520                 :            : 
     521                 :          0 :         fprintf(nconf, "wps_state=2\n");
     522                 :            : 
     523         [ #  # ]:          0 :         if (is_hex(cred->ssid, cred->ssid_len)) {
     524                 :          0 :                 fprintf(nconf, "ssid2=");
     525         [ #  # ]:          0 :                 for (i = 0; i < cred->ssid_len; i++)
     526                 :          0 :                         fprintf(nconf, "%02x", cred->ssid[i]);
     527                 :          0 :                 fprintf(nconf, "\n");
     528                 :            :         } else {
     529                 :          0 :                 fprintf(nconf, "ssid=");
     530         [ #  # ]:          0 :                 for (i = 0; i < cred->ssid_len; i++)
     531                 :          0 :                         fputc(cred->ssid[i], nconf);
     532                 :          0 :                 fprintf(nconf, "\n");
     533                 :            :         }
     534                 :            : 
     535 [ #  # ][ #  # ]:          0 :         if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
     536                 :          0 :             (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
     537                 :          0 :                 wpa = 3;
     538         [ #  # ]:          0 :         else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
     539                 :          0 :                 wpa = 2;
     540         [ #  # ]:          0 :         else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
     541                 :          0 :                 wpa = 1;
     542                 :            :         else
     543                 :          0 :                 wpa = 0;
     544                 :            : 
     545         [ #  # ]:          0 :         if (wpa) {
     546                 :            :                 char *prefix;
     547                 :          0 :                 fprintf(nconf, "wpa=%d\n", wpa);
     548                 :            : 
     549                 :          0 :                 fprintf(nconf, "wpa_key_mgmt=");
     550                 :          0 :                 prefix = "";
     551         [ #  # ]:          0 :                 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
     552                 :          0 :                         fprintf(nconf, "WPA-EAP");
     553                 :          0 :                         prefix = " ";
     554                 :            :                 }
     555         [ #  # ]:          0 :                 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
     556                 :          0 :                         fprintf(nconf, "%sWPA-PSK", prefix);
     557                 :          0 :                 fprintf(nconf, "\n");
     558                 :            : 
     559                 :          0 :                 fprintf(nconf, "wpa_pairwise=");
     560                 :          0 :                 prefix = "";
     561         [ #  # ]:          0 :                 if (cred->encr_type & WPS_ENCR_AES) {
     562                 :          0 :                         fprintf(nconf, "CCMP");
     563                 :          0 :                         prefix = " ";
     564                 :            :                 }
     565         [ #  # ]:          0 :                 if (cred->encr_type & WPS_ENCR_TKIP) {
     566                 :          0 :                         fprintf(nconf, "%sTKIP", prefix);
     567                 :            :                 }
     568                 :          0 :                 fprintf(nconf, "\n");
     569                 :            : 
     570 [ #  # ][ #  # ]:          0 :                 if (cred->key_len >= 8 && cred->key_len < 64) {
     571                 :          0 :                         fprintf(nconf, "wpa_passphrase=");
     572         [ #  # ]:          0 :                         for (i = 0; i < cred->key_len; i++)
     573                 :          0 :                                 fputc(cred->key[i], nconf);
     574                 :          0 :                         fprintf(nconf, "\n");
     575         [ #  # ]:          0 :                 } else if (cred->key_len == 64) {
     576                 :          0 :                         fprintf(nconf, "wpa_psk=");
     577         [ #  # ]:          0 :                         for (i = 0; i < cred->key_len; i++)
     578                 :          0 :                                 fputc(cred->key[i], nconf);
     579                 :          0 :                         fprintf(nconf, "\n");
     580                 :            :                 } else {
     581                 :          0 :                         wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
     582                 :            :                                    "for WPA/WPA2",
     583                 :            :                                    (unsigned long) cred->key_len);
     584                 :            :                 }
     585                 :            : 
     586                 :          0 :                 fprintf(nconf, "auth_algs=1\n");
     587                 :            :         } else {
     588 [ #  # ][ #  # ]:          0 :                 if ((cred->auth_type & WPS_AUTH_OPEN) &&
     589                 :          0 :                     (cred->auth_type & WPS_AUTH_SHARED))
     590                 :          0 :                         fprintf(nconf, "auth_algs=3\n");
     591         [ #  # ]:          0 :                 else if (cred->auth_type & WPS_AUTH_SHARED)
     592                 :          0 :                         fprintf(nconf, "auth_algs=2\n");
     593                 :            :                 else
     594                 :          0 :                         fprintf(nconf, "auth_algs=1\n");
     595                 :            : 
     596 [ #  # ][ #  # ]:          0 :                 if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx <= 4) {
     597                 :          0 :                         int key_idx = cred->key_idx;
     598         [ #  # ]:          0 :                         if (key_idx)
     599                 :          0 :                                 key_idx--;
     600                 :          0 :                         fprintf(nconf, "wep_default_key=%d\n", key_idx);
     601                 :          0 :                         fprintf(nconf, "wep_key%d=", key_idx);
     602 [ #  # ][ #  # ]:          0 :                         if (cred->key_len == 10 || cred->key_len == 26) {
     603                 :            :                                 /* WEP key as a hex string */
     604         [ #  # ]:          0 :                                 for (i = 0; i < cred->key_len; i++)
     605                 :          0 :                                         fputc(cred->key[i], nconf);
     606                 :            :                         } else {
     607                 :            :                                 /* Raw WEP key; convert to hex */
     608         [ #  # ]:          0 :                                 for (i = 0; i < cred->key_len; i++)
     609                 :          0 :                                         fprintf(nconf, "%02x", cred->key[i]);
     610                 :            :                         }
     611                 :          0 :                         fprintf(nconf, "\n");
     612                 :            :                 }
     613                 :            :         }
     614                 :            : 
     615                 :          0 :         fprintf(nconf, "# WPS configuration - END\n");
     616                 :            : 
     617                 :          0 :         multi_bss = 0;
     618         [ #  # ]:          0 :         while (fgets(buf, sizeof(buf), oconf)) {
     619         [ #  # ]:          0 :                 if (os_strncmp(buf, "bss=", 4) == 0)
     620                 :          0 :                         multi_bss = 1;
     621   [ #  #  #  # ]:          0 :                 if (!multi_bss &&
     622         [ #  # ]:          0 :                     (str_starts(buf, "ssid=") ||
     623         [ #  # ]:          0 :                      str_starts(buf, "ssid2=") ||
     624         [ #  # ]:          0 :                      str_starts(buf, "auth_algs=") ||
     625         [ #  # ]:          0 :                      str_starts(buf, "wep_default_key=") ||
     626         [ #  # ]:          0 :                      str_starts(buf, "wep_key") ||
     627         [ #  # ]:          0 :                      str_starts(buf, "wps_state=") ||
     628         [ #  # ]:          0 :                      str_starts(buf, "wpa=") ||
     629         [ #  # ]:          0 :                      str_starts(buf, "wpa_psk=") ||
     630         [ #  # ]:          0 :                      str_starts(buf, "wpa_pairwise=") ||
     631         [ #  # ]:          0 :                      str_starts(buf, "rsn_pairwise=") ||
     632         [ #  # ]:          0 :                      str_starts(buf, "wpa_key_mgmt=") ||
     633                 :          0 :                      str_starts(buf, "wpa_passphrase="))) {
     634                 :          0 :                         fprintf(nconf, "#WPS# %s", buf);
     635                 :            :                 } else
     636                 :          0 :                         fprintf(nconf, "%s", buf);
     637                 :            :         }
     638                 :            : 
     639                 :          0 :         fclose(nconf);
     640                 :          0 :         fclose(oconf);
     641                 :            : 
     642         [ #  # ]:          0 :         if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
     643                 :          0 :                 wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
     644                 :          0 :                            "configuration file: %s", strerror(errno));
     645                 :          0 :                 os_free(tmp_fname);
     646                 :          0 :                 return -1;
     647                 :            :         }
     648                 :            : 
     649                 :          0 :         os_free(tmp_fname);
     650                 :            : 
     651                 :            :         /* Schedule configuration reload after short period of time to allow
     652                 :            :          * EAP-WSC to be finished.
     653                 :            :          */
     654                 :          0 :         eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
     655                 :            :                                NULL);
     656                 :            : 
     657                 :          0 :         wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
     658                 :            : 
     659                 :         14 :         return 0;
     660                 :            : }
     661                 :            : 
     662                 :            : 
     663                 :         12 : static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
     664                 :            : {
     665                 :         12 :         struct hostapd_data *hapd = ctx;
     666                 :         12 :         return hostapd_wps_for_each(hapd, hapd_wps_cred_cb, (void *) cred);
     667                 :            : }
     668                 :            : 
     669                 :            : 
     670                 :          0 : static void hostapd_wps_reenable_ap_pin(void *eloop_data, void *user_ctx)
     671                 :            : {
     672                 :          0 :         struct hostapd_data *hapd = eloop_data;
     673                 :            : 
     674         [ #  # ]:          0 :         if (hapd->conf->ap_setup_locked)
     675                 :          0 :                 return;
     676         [ #  # ]:          0 :         if (hapd->ap_pin_failures_consecutive >= 10)
     677                 :          0 :                 return;
     678                 :            : 
     679                 :          0 :         wpa_printf(MSG_DEBUG, "WPS: Re-enable AP PIN");
     680                 :          0 :         wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
     681                 :          0 :         hapd->wps->ap_setup_locked = 0;
     682                 :          0 :         wps_registrar_update_ie(hapd->wps->registrar);
     683                 :            : }
     684                 :            : 
     685                 :            : 
     686                 :          6 : static int wps_pwd_auth_fail(struct hostapd_data *hapd, void *ctx)
     687                 :            : {
     688                 :          6 :         struct wps_event_pwd_auth_fail *data = ctx;
     689                 :            : 
     690 [ +  - ][ +  - ]:          6 :         if (!data->enrollee || hapd->conf->ap_pin == NULL || hapd->wps == NULL)
                 [ -  + ]
     691                 :          0 :                 return 0;
     692                 :            : 
     693                 :            :         /*
     694                 :            :          * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup
     695                 :            :          * for some time if this happens multiple times to slow down brute
     696                 :            :          * force attacks.
     697                 :            :          */
     698                 :          6 :         hapd->ap_pin_failures++;
     699                 :          6 :         hapd->ap_pin_failures_consecutive++;
     700                 :          6 :         wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u "
     701                 :            :                    "(%u consecutive)",
     702                 :            :                    hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive);
     703         [ +  + ]:          6 :         if (hapd->ap_pin_failures < 3)
     704                 :          4 :                 return 0;
     705                 :            : 
     706                 :          2 :         wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED);
     707                 :          2 :         hapd->wps->ap_setup_locked = 1;
     708                 :            : 
     709                 :          2 :         wps_registrar_update_ie(hapd->wps->registrar);
     710                 :            : 
     711 [ -  + ][ +  + ]:          2 :         if (!hapd->conf->ap_setup_locked &&
     712                 :          1 :             hapd->ap_pin_failures_consecutive >= 10) {
     713                 :            :                 /*
     714                 :            :                  * In indefinite lockdown - disable automatic AP PIN
     715                 :            :                  * reenablement.
     716                 :            :                  */
     717                 :          0 :                 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
     718                 :          0 :                 wpa_printf(MSG_DEBUG, "WPS: AP PIN disabled indefinitely");
     719         [ +  + ]:          2 :         } else if (!hapd->conf->ap_setup_locked) {
     720         [ +  - ]:          1 :                 if (hapd->ap_pin_lockout_time == 0)
     721                 :          1 :                         hapd->ap_pin_lockout_time = 60;
     722 [ #  # ][ #  # ]:          0 :                 else if (hapd->ap_pin_lockout_time < 365 * 24 * 60 * 60 &&
     723                 :          0 :                          (hapd->ap_pin_failures % 3) == 0)
     724                 :          0 :                         hapd->ap_pin_lockout_time *= 2;
     725                 :            : 
     726                 :          1 :                 wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN for %u seconds",
     727                 :            :                            hapd->ap_pin_lockout_time);
     728                 :          1 :                 eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
     729                 :          1 :                 eloop_register_timeout(hapd->ap_pin_lockout_time, 0,
     730                 :            :                                        hostapd_wps_reenable_ap_pin, hapd,
     731                 :            :                                        NULL);
     732                 :            :         }
     733                 :            : 
     734                 :          6 :         return 0;
     735                 :            : }
     736                 :            : 
     737                 :            : 
     738                 :          6 : static void hostapd_pwd_auth_fail(struct hostapd_data *hapd,
     739                 :            :                                   struct wps_event_pwd_auth_fail *data)
     740                 :            : {
     741                 :            :         /* Update WPS Status - Authentication Failure */
     742                 :          6 :         wpa_printf(MSG_DEBUG, "WPS: Authentication failure update");
     743                 :          6 :         hapd->wps_stats.status = WPS_STATUS_FAILURE;
     744                 :          6 :         hapd->wps_stats.failure_reason = WPS_EI_AUTH_FAILURE;
     745                 :          6 :         os_memcpy(hapd->wps_stats.peer_addr, data->peer_macaddr, ETH_ALEN);
     746                 :            : 
     747                 :          6 :         hostapd_wps_for_each(hapd, wps_pwd_auth_fail, data);
     748                 :          6 : }
     749                 :            : 
     750                 :            : 
     751                 :         18 : static int wps_ap_pin_success(struct hostapd_data *hapd, void *ctx)
     752                 :            : {
     753 [ +  + ][ -  + ]:         18 :         if (hapd->conf->ap_pin == NULL || hapd->wps == NULL)
     754                 :          1 :                 return 0;
     755                 :            : 
     756         [ +  - ]:         17 :         if (hapd->ap_pin_failures_consecutive == 0)
     757                 :         17 :                 return 0;
     758                 :            : 
     759                 :          0 :         wpa_printf(MSG_DEBUG, "WPS: Clear consecutive AP PIN failure counter "
     760                 :            :                    "- total validation failures %u (%u consecutive)",
     761                 :            :                    hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive);
     762                 :          0 :         hapd->ap_pin_failures_consecutive = 0;
     763                 :            : 
     764                 :         18 :         return 0;
     765                 :            : }
     766                 :            : 
     767                 :            : 
     768                 :         18 : static void hostapd_wps_ap_pin_success(struct hostapd_data *hapd)
     769                 :            : {
     770                 :         18 :         hostapd_wps_for_each(hapd, wps_ap_pin_success, NULL);
     771                 :         18 : }
     772                 :            : 
     773                 :            : 
     774                 :          4 : static void hostapd_wps_event_pbc_overlap(struct hostapd_data *hapd)
     775                 :            : {
     776                 :            :         /* Update WPS Status - PBC Overlap */
     777                 :          4 :         hapd->wps_stats.pbc_status = WPS_PBC_STATUS_OVERLAP;
     778                 :          4 : }
     779                 :            : 
     780                 :            : 
     781                 :          3 : static void hostapd_wps_event_pbc_timeout(struct hostapd_data *hapd)
     782                 :            : {
     783                 :            :         /* Update WPS PBC Status:PBC Timeout */
     784                 :          3 :         hapd->wps_stats.pbc_status = WPS_PBC_STATUS_TIMEOUT;
     785                 :          3 : }
     786                 :            : 
     787                 :            : 
     788                 :         20 : static void hostapd_wps_event_pbc_active(struct hostapd_data *hapd)
     789                 :            : {
     790                 :            :         /* Update WPS PBC status - Active */
     791                 :         20 :         hapd->wps_stats.pbc_status = WPS_PBC_STATUS_ACTIVE;
     792                 :         20 : }
     793                 :            : 
     794                 :            : 
     795                 :         15 : static void hostapd_wps_event_pbc_disable(struct hostapd_data *hapd)
     796                 :            : {
     797                 :            :         /* Update WPS PBC status - Active */
     798                 :         15 :         hapd->wps_stats.pbc_status = WPS_PBC_STATUS_DISABLE;
     799                 :         15 : }
     800                 :            : 
     801                 :            : 
     802                 :        104 : static void hostapd_wps_event_success(struct hostapd_data *hapd,
     803                 :            :                                       struct wps_event_success *success)
     804                 :            : {
     805                 :            :         /* Update WPS status - Success */
     806                 :        104 :         hapd->wps_stats.pbc_status = WPS_PBC_STATUS_DISABLE;
     807                 :        104 :         hapd->wps_stats.status = WPS_STATUS_SUCCESS;
     808                 :        104 :         os_memcpy(hapd->wps_stats.peer_addr, success->peer_macaddr, ETH_ALEN);
     809                 :        104 : }
     810                 :            : 
     811                 :            : 
     812                 :         25 : static void hostapd_wps_event_fail(struct hostapd_data *hapd,
     813                 :            :                                    struct wps_event_fail *fail)
     814                 :            : {
     815                 :            :         /* Update WPS status - Failure */
     816                 :         25 :         hapd->wps_stats.status = WPS_STATUS_FAILURE;
     817                 :         25 :         os_memcpy(hapd->wps_stats.peer_addr, fail->peer_macaddr, ETH_ALEN);
     818                 :            : 
     819                 :         25 :         hapd->wps_stats.failure_reason = fail->error_indication;
     820                 :            : 
     821 [ +  + ][ +  - ]:         25 :         if (fail->error_indication > 0 &&
     822                 :          1 :             fail->error_indication < NUM_WPS_EI_VALUES) {
     823                 :          1 :                 wpa_msg(hapd->msg_ctx, MSG_INFO,
     824                 :            :                         WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
     825                 :          2 :                         fail->msg, fail->config_error, fail->error_indication,
     826                 :          1 :                         wps_ei_str(fail->error_indication));
     827                 :            :         } else {
     828                 :         24 :                 wpa_msg(hapd->msg_ctx, MSG_INFO,
     829                 :            :                         WPS_EVENT_FAIL "msg=%d config_error=%d",
     830                 :         24 :                         fail->msg, fail->config_error);
     831                 :            :         }
     832                 :         25 : }
     833                 :            : 
     834                 :            : 
     835                 :        195 : static void hostapd_wps_event_cb(void *ctx, enum wps_event event,
     836                 :            :                                  union wps_event_data *data)
     837                 :            : {
     838                 :        195 :         struct hostapd_data *hapd = ctx;
     839                 :            : 
     840   [ -  +  +  +  :        195 :         switch (event) {
          +  +  +  +  -  
          -  -  -  -  -  
                   +  - ]
     841                 :            :         case WPS_EV_M2D:
     842                 :          0 :                 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_M2D);
     843                 :          0 :                 break;
     844                 :            :         case WPS_EV_FAIL:
     845                 :         25 :                 hostapd_wps_event_fail(hapd, &data->fail);
     846                 :         25 :                 break;
     847                 :            :         case WPS_EV_SUCCESS:
     848                 :        104 :                 hostapd_wps_event_success(hapd, &data->success);
     849                 :        104 :                 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_SUCCESS);
     850                 :        104 :                 break;
     851                 :            :         case WPS_EV_PWD_AUTH_FAIL:
     852                 :          6 :                 hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail);
     853                 :          6 :                 break;
     854                 :            :         case WPS_EV_PBC_OVERLAP:
     855                 :          4 :                 hostapd_wps_event_pbc_overlap(hapd);
     856                 :          4 :                 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_OVERLAP);
     857                 :          4 :                 break;
     858                 :            :         case WPS_EV_PBC_TIMEOUT:
     859                 :          3 :                 hostapd_wps_event_pbc_timeout(hapd);
     860                 :          3 :                 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_TIMEOUT);
     861                 :          3 :                 break;
     862                 :            :         case WPS_EV_PBC_ACTIVE:
     863                 :         20 :                 hostapd_wps_event_pbc_active(hapd);
     864                 :         20 :                 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ACTIVE);
     865                 :         20 :                 break;
     866                 :            :         case WPS_EV_PBC_DISABLE:
     867                 :         15 :                 hostapd_wps_event_pbc_disable(hapd);
     868                 :         15 :                 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_DISABLE);
     869                 :         15 :                 break;
     870                 :            :         case WPS_EV_ER_AP_ADD:
     871                 :          0 :                 break;
     872                 :            :         case WPS_EV_ER_AP_REMOVE:
     873                 :          0 :                 break;
     874                 :            :         case WPS_EV_ER_ENROLLEE_ADD:
     875                 :          0 :                 break;
     876                 :            :         case WPS_EV_ER_ENROLLEE_REMOVE:
     877                 :          0 :                 break;
     878                 :            :         case WPS_EV_ER_AP_SETTINGS:
     879                 :          0 :                 break;
     880                 :            :         case WPS_EV_ER_SET_SELECTED_REGISTRAR:
     881                 :          0 :                 break;
     882                 :            :         case WPS_EV_AP_PIN_SUCCESS:
     883                 :         18 :                 hostapd_wps_ap_pin_success(hapd);
     884                 :         18 :                 break;
     885                 :            :         }
     886         [ +  + ]:        195 :         if (hapd->wps_event_cb)
     887                 :        102 :                 hapd->wps_event_cb(hapd->wps_event_cb_ctx, event, data);
     888                 :        195 : }
     889                 :            : 
     890                 :            : 
     891                 :        159 : static int hostapd_wps_rf_band_cb(void *ctx)
     892                 :            : {
     893                 :        159 :         struct hostapd_data *hapd = ctx;
     894                 :            : 
     895                 :        159 :         return hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
     896         [ -  + ]:        159 :                 WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
     897                 :            : }
     898                 :            : 
     899                 :            : 
     900                 :        429 : static void hostapd_wps_clear_ies(struct hostapd_data *hapd)
     901                 :            : {
     902                 :        429 :         wpabuf_free(hapd->wps_beacon_ie);
     903                 :        429 :         hapd->wps_beacon_ie = NULL;
     904                 :            : 
     905                 :        429 :         wpabuf_free(hapd->wps_probe_resp_ie);
     906                 :        429 :         hapd->wps_probe_resp_ie = NULL;
     907                 :            : 
     908                 :        429 :         hostapd_set_ap_wps_ie(hapd);
     909                 :        429 : }
     910                 :            : 
     911                 :            : 
     912                 :         37 : static int get_uuid_cb(struct hostapd_iface *iface, void *ctx)
     913                 :            : {
     914                 :         37 :         const u8 **uuid = ctx;
     915                 :            :         size_t j;
     916                 :            : 
     917         [ -  + ]:         37 :         if (iface == NULL)
     918                 :          0 :                 return 0;
     919         [ +  + ]:         72 :         for (j = 0; j < iface->num_bss; j++) {
     920                 :         37 :                 struct hostapd_data *hapd = iface->bss[j];
     921         [ +  + ]:         39 :                 if (hapd->wps && !hapd->conf->wps_independent &&
           [ +  +  +  - ]
     922                 :          2 :                     !is_nil_uuid(hapd->wps->uuid)) {
     923                 :          2 :                         *uuid = hapd->wps->uuid;
     924                 :          2 :                         return 1;
     925                 :            :                 }
     926                 :            :         }
     927                 :            : 
     928                 :         37 :         return 0;
     929                 :            : }
     930                 :            : 
     931                 :            : 
     932                 :         36 : static const u8 * get_own_uuid(struct hostapd_iface *iface)
     933                 :            : {
     934                 :            :         const u8 *uuid;
     935 [ +  - ][ -  + ]:         36 :         if (iface->interfaces == NULL ||
     936                 :         36 :             iface->interfaces->for_each_interface == NULL)
     937                 :          0 :                 return NULL;
     938                 :         36 :         uuid = NULL;
     939                 :         36 :         iface->interfaces->for_each_interface(iface->interfaces, get_uuid_cb,
     940                 :            :                                               &uuid);
     941                 :         36 :         return uuid;
     942                 :            : }
     943                 :            : 
     944                 :            : 
     945                 :         54 : static int count_interface_cb(struct hostapd_iface *iface, void *ctx)
     946                 :            : {
     947                 :         54 :         int *count= ctx;
     948                 :         54 :         (*count)++;
     949                 :         54 :         return 0;
     950                 :            : }
     951                 :            : 
     952                 :            : 
     953                 :        136 : static int interface_count(struct hostapd_iface *iface)
     954                 :            : {
     955                 :        136 :         int count = 0;
     956 [ +  + ][ -  + ]:        136 :         if (iface->interfaces == NULL ||
     957                 :         51 :             iface->interfaces->for_each_interface == NULL)
     958                 :         85 :                 return 0;
     959                 :         51 :         iface->interfaces->for_each_interface(iface->interfaces,
     960                 :            :                                               count_interface_cb, &count);
     961                 :        136 :         return count;
     962                 :            : }
     963                 :            : 
     964                 :            : 
     965                 :        150 : static int hostapd_wps_set_vendor_ext(struct hostapd_data *hapd,
     966                 :            :                                       struct wps_context *wps)
     967                 :            : {
     968                 :            :         int i;
     969                 :            : 
     970         [ +  + ]:       1650 :         for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
     971                 :       1500 :                 wpabuf_free(wps->dev.vendor_ext[i]);
     972                 :       1500 :                 wps->dev.vendor_ext[i] = NULL;
     973                 :            : 
     974         [ +  - ]:       1500 :                 if (hapd->conf->wps_vendor_ext[i] == NULL)
     975                 :       1500 :                         continue;
     976                 :            : 
     977                 :          0 :                 wps->dev.vendor_ext[i] =
     978                 :          0 :                         wpabuf_dup(hapd->conf->wps_vendor_ext[i]);
     979         [ #  # ]:          0 :                 if (wps->dev.vendor_ext[i] == NULL) {
     980         [ #  # ]:          0 :                         while (--i >= 0)
     981                 :          0 :                                 wpabuf_free(wps->dev.vendor_ext[i]);
     982                 :          0 :                         return -1;
     983                 :            :                 }
     984                 :            :         }
     985                 :            : 
     986                 :        150 :         return 0;
     987                 :            : }
     988                 :            : 
     989                 :            : 
     990                 :        429 : int hostapd_init_wps(struct hostapd_data *hapd,
     991                 :            :                      struct hostapd_bss_config *conf)
     992                 :            : {
     993                 :            :         struct wps_context *wps;
     994                 :            :         struct wps_registrar_config cfg;
     995                 :            : 
     996         [ +  + ]:        429 :         if (conf->wps_state == 0) {
     997                 :        293 :                 hostapd_wps_clear_ies(hapd);
     998                 :        293 :                 return 0;
     999                 :            :         }
    1000                 :            : 
    1001                 :        136 :         wps = os_zalloc(sizeof(*wps));
    1002         [ -  + ]:        136 :         if (wps == NULL)
    1003                 :          0 :                 return -1;
    1004                 :            : 
    1005                 :        136 :         wps->cred_cb = hostapd_wps_cred_cb;
    1006                 :        136 :         wps->event_cb = hostapd_wps_event_cb;
    1007                 :        136 :         wps->rf_band_cb = hostapd_wps_rf_band_cb;
    1008                 :        136 :         wps->cb_ctx = hapd;
    1009                 :            : 
    1010                 :        136 :         os_memset(&cfg, 0, sizeof(cfg));
    1011                 :        136 :         wps->wps_state = hapd->conf->wps_state;
    1012                 :        136 :         wps->ap_setup_locked = hapd->conf->ap_setup_locked;
    1013         [ +  + ]:        136 :         if (is_nil_uuid(hapd->conf->uuid)) {
    1014                 :            :                 const u8 *uuid;
    1015                 :         36 :                 uuid = get_own_uuid(hapd->iface);
    1016 [ +  - ][ +  + ]:         36 :                 if (uuid && !conf->wps_independent) {
    1017                 :          2 :                         os_memcpy(wps->uuid, uuid, UUID_LEN);
    1018                 :          2 :                         wpa_hexdump(MSG_DEBUG, "WPS: Clone UUID from another "
    1019                 :          2 :                                     "interface", wps->uuid, UUID_LEN);
    1020                 :            :                 } else {
    1021                 :         34 :                         uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
    1022                 :         36 :                         wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
    1023                 :         34 :                                     "address", wps->uuid, UUID_LEN);
    1024                 :            :                 }
    1025                 :            :         } else {
    1026                 :        100 :                 os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
    1027                 :        100 :                 wpa_hexdump(MSG_DEBUG, "WPS: Use configured UUID",
    1028                 :        100 :                             wps->uuid, UUID_LEN);
    1029                 :            :         }
    1030                 :        136 :         wps->ssid_len = hapd->conf->ssid.ssid_len;
    1031                 :        136 :         os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
    1032                 :        136 :         wps->ap = 1;
    1033                 :        136 :         os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
    1034                 :        272 :         wps->dev.device_name = hapd->conf->device_name ?
    1035         [ +  + ]:        136 :                 os_strdup(hapd->conf->device_name) : NULL;
    1036                 :        272 :         wps->dev.manufacturer = hapd->conf->manufacturer ?
    1037         [ +  + ]:        136 :                 os_strdup(hapd->conf->manufacturer) : NULL;
    1038                 :        272 :         wps->dev.model_name = hapd->conf->model_name ?
    1039         [ +  + ]:        136 :                 os_strdup(hapd->conf->model_name) : NULL;
    1040                 :        272 :         wps->dev.model_number = hapd->conf->model_number ?
    1041         [ +  + ]:        136 :                 os_strdup(hapd->conf->model_number) : NULL;
    1042                 :        272 :         wps->dev.serial_number = hapd->conf->serial_number ?
    1043         [ +  + ]:        136 :                 os_strdup(hapd->conf->serial_number) : NULL;
    1044                 :        136 :         wps->config_methods =
    1045                 :        136 :                 wps_config_methods_str2bin(hapd->conf->config_methods);
    1046                 :            : #ifdef CONFIG_WPS2
    1047         [ -  + ]:        136 :         if ((wps->config_methods &
    1048                 :            :              (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
    1049                 :            :               WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
    1050                 :          0 :                 wpa_printf(MSG_INFO, "WPS: Converting display to "
    1051                 :            :                            "virtual_display for WPS 2.0 compliance");
    1052                 :          0 :                 wps->config_methods |= WPS_CONFIG_VIRT_DISPLAY;
    1053                 :            :         }
    1054         [ +  + ]:        136 :         if ((wps->config_methods &
    1055                 :            :              (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
    1056                 :            :               WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
    1057                 :         15 :                 wpa_printf(MSG_INFO, "WPS: Converting push_button to "
    1058                 :            :                            "virtual_push_button for WPS 2.0 compliance");
    1059                 :         15 :                 wps->config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
    1060                 :            :         }
    1061                 :            : #endif /* CONFIG_WPS2 */
    1062                 :        136 :         os_memcpy(wps->dev.pri_dev_type, hapd->conf->device_type,
    1063                 :            :                   WPS_DEV_TYPE_LEN);
    1064                 :            : 
    1065         [ -  + ]:        136 :         if (hostapd_wps_set_vendor_ext(hapd, wps) < 0) {
    1066                 :          0 :                 os_free(wps);
    1067                 :          0 :                 return -1;
    1068                 :            :         }
    1069                 :            : 
    1070                 :        136 :         wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
    1071                 :            : 
    1072         [ -  + ]:        136 :         if (conf->wps_rf_bands) {
    1073                 :          0 :                 wps->dev.rf_bands = conf->wps_rf_bands;
    1074                 :            :         } else {
    1075         [ -  + ]:        136 :                 wps->dev.rf_bands =
    1076                 :        136 :                         hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
    1077                 :            :                         WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
    1078                 :            :         }
    1079                 :            : 
    1080         [ +  + ]:        136 :         if (conf->wpa & WPA_PROTO_RSN) {
    1081         [ +  - ]:        119 :                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
    1082                 :        119 :                         wps->auth_types |= WPS_AUTH_WPA2PSK;
    1083         [ +  + ]:        119 :                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
    1084                 :          1 :                         wps->auth_types |= WPS_AUTH_WPA2;
    1085                 :            : 
    1086         [ +  - ]:        119 :                 if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
    1087                 :        119 :                         wps->encr_types |= WPS_ENCR_AES;
    1088         [ +  + ]:        119 :                 if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
    1089                 :          1 :                         wps->encr_types |= WPS_ENCR_TKIP;
    1090                 :            :         }
    1091                 :            : 
    1092         [ +  + ]:        136 :         if (conf->wpa & WPA_PROTO_WPA) {
    1093         [ +  - ]:          2 :                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
    1094                 :          2 :                         wps->auth_types |= WPS_AUTH_WPAPSK;
    1095         [ +  + ]:          2 :                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
    1096                 :          1 :                         wps->auth_types |= WPS_AUTH_WPA;
    1097                 :            : 
    1098         [ +  + ]:          2 :                 if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
    1099                 :          1 :                         wps->encr_types |= WPS_ENCR_AES;
    1100         [ +  - ]:          2 :                 if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
    1101                 :          2 :                         wps->encr_types |= WPS_ENCR_TKIP;
    1102                 :            :         }
    1103                 :            : 
    1104         [ +  + ]:        136 :         if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
    1105                 :         17 :                 wps->encr_types |= WPS_ENCR_NONE;
    1106                 :         17 :                 wps->auth_types |= WPS_AUTH_OPEN;
    1107         [ -  + ]:        119 :         } else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) {
    1108                 :          0 :                 wps->encr_types |= WPS_ENCR_WEP;
    1109         [ #  # ]:          0 :                 if (conf->auth_algs & WPA_AUTH_ALG_OPEN)
    1110                 :          0 :                         wps->auth_types |= WPS_AUTH_OPEN;
    1111         [ #  # ]:          0 :                 if (conf->auth_algs & WPA_AUTH_ALG_SHARED)
    1112                 :          0 :                         wps->auth_types |= WPS_AUTH_SHARED;
    1113         [ -  + ]:        119 :         } else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) {
    1114                 :          0 :                 wps->auth_types |= WPS_AUTH_OPEN;
    1115         [ #  # ]:          0 :                 if (conf->default_wep_key_len)
    1116                 :          0 :                         wps->encr_types |= WPS_ENCR_WEP;
    1117                 :            :                 else
    1118                 :          0 :                         wps->encr_types |= WPS_ENCR_NONE;
    1119                 :            :         }
    1120                 :            : 
    1121         [ +  - ]:        136 :         if (conf->ssid.wpa_psk_file) {
    1122                 :            :                 /* Use per-device PSKs */
    1123         [ +  + ]:        136 :         } else if (conf->ssid.wpa_passphrase) {
    1124                 :         37 :                 wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
    1125                 :         37 :                 wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
    1126         [ +  + ]:         99 :         } else if (conf->ssid.wpa_psk) {
    1127                 :         82 :                 wps->network_key = os_malloc(2 * PMK_LEN + 1);
    1128         [ -  + ]:         82 :                 if (wps->network_key == NULL) {
    1129                 :          0 :                         os_free(wps);
    1130                 :          0 :                         return -1;
    1131                 :            :                 }
    1132                 :         82 :                 wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
    1133                 :         82 :                                  conf->ssid.wpa_psk->psk, PMK_LEN);
    1134                 :         82 :                 wps->network_key_len = 2 * PMK_LEN;
    1135 [ -  + ][ #  # ]:         17 :         } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
    1136                 :          0 :                 wps->network_key = os_malloc(conf->ssid.wep.len[0]);
    1137         [ #  # ]:          0 :                 if (wps->network_key == NULL) {
    1138                 :          0 :                         os_free(wps);
    1139                 :          0 :                         return -1;
    1140                 :            :                 }
    1141                 :          0 :                 os_memcpy(wps->network_key, conf->ssid.wep.key[0],
    1142                 :            :                           conf->ssid.wep.len[0]);
    1143                 :          0 :                 wps->network_key_len = conf->ssid.wep.len[0];
    1144                 :            :         }
    1145                 :            : 
    1146         [ +  + ]:        136 :         if (conf->ssid.wpa_psk) {
    1147                 :        119 :                 os_memcpy(wps->psk, conf->ssid.wpa_psk->psk, PMK_LEN);
    1148                 :        119 :                 wps->psk_set = 1;
    1149                 :            :         }
    1150                 :            : 
    1151                 :        136 :         wps->ap_auth_type = wps->auth_types;
    1152                 :        136 :         wps->ap_encr_type = wps->encr_types;
    1153         [ +  + ]:        136 :         if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
    1154                 :            :                 /* Override parameters to enable security by default */
    1155                 :         12 :                 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
    1156                 :         12 :                 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
    1157                 :            :         }
    1158                 :            : 
    1159                 :        136 :         wps->ap_settings = conf->ap_settings;
    1160                 :        136 :         wps->ap_settings_len = conf->ap_settings_len;
    1161                 :            : 
    1162                 :        136 :         cfg.new_psk_cb = hostapd_wps_new_psk_cb;
    1163                 :        136 :         cfg.set_ie_cb = hostapd_wps_set_ie_cb;
    1164                 :        136 :         cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
    1165                 :        136 :         cfg.reg_success_cb = hostapd_wps_reg_success_cb;
    1166                 :        136 :         cfg.enrollee_seen_cb = hostapd_wps_enrollee_seen_cb;
    1167                 :        136 :         cfg.cb_ctx = hapd;
    1168                 :        136 :         cfg.skip_cred_build = conf->skip_cred_build;
    1169                 :        136 :         cfg.extra_cred = conf->extra_cred;
    1170                 :        136 :         cfg.extra_cred_len = conf->extra_cred_len;
    1171 [ -  + ][ #  # ]:        136 :         cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
    1172                 :          0 :                 conf->skip_cred_build;
    1173         [ -  + ]:        136 :         if (conf->ssid.security_policy == SECURITY_STATIC_WEP)
    1174                 :          0 :                 cfg.static_wep_only = 1;
    1175                 :        136 :         cfg.dualband = interface_count(hapd->iface) > 1;
    1176         [ -  + ]:        136 :         if ((wps->dev.rf_bands & (WPS_RF_50GHZ | WPS_RF_24GHZ)) ==
    1177                 :            :             (WPS_RF_50GHZ | WPS_RF_24GHZ))
    1178                 :          0 :                 cfg.dualband = 1;
    1179         [ +  + ]:        136 :         if (cfg.dualband)
    1180                 :          3 :                 wpa_printf(MSG_DEBUG, "WPS: Dualband AP");
    1181                 :        136 :         cfg.force_per_enrollee_psk = conf->force_per_enrollee_psk;
    1182                 :            : 
    1183                 :        136 :         wps->registrar = wps_registrar_init(wps, &cfg);
    1184         [ -  + ]:        136 :         if (wps->registrar == NULL) {
    1185                 :          0 :                 wpa_printf(MSG_ERROR, "Failed to initialize WPS Registrar");
    1186                 :          0 :                 os_free(wps->network_key);
    1187                 :          0 :                 os_free(wps);
    1188                 :          0 :                 return -1;
    1189                 :            :         }
    1190                 :            : 
    1191                 :            : #ifdef CONFIG_WPS_UPNP
    1192                 :        136 :         wps->friendly_name = hapd->conf->friendly_name;
    1193                 :        136 :         wps->manufacturer_url = hapd->conf->manufacturer_url;
    1194                 :        136 :         wps->model_description = hapd->conf->model_description;
    1195                 :        136 :         wps->model_url = hapd->conf->model_url;
    1196                 :        136 :         wps->upc = hapd->conf->upc;
    1197                 :            : #endif /* CONFIG_WPS_UPNP */
    1198                 :            : 
    1199                 :        136 :         hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd);
    1200                 :            : 
    1201                 :        136 :         hapd->wps = wps;
    1202                 :            : 
    1203                 :        429 :         return 0;
    1204                 :            : }
    1205                 :            : 
    1206                 :            : 
    1207                 :        419 : int hostapd_init_wps_complete(struct hostapd_data *hapd)
    1208                 :            : {
    1209                 :        419 :         struct wps_context *wps = hapd->wps;
    1210                 :            : 
    1211         [ +  + ]:        419 :         if (wps == NULL)
    1212                 :        283 :                 return 0;
    1213                 :            : 
    1214                 :            : #ifdef CONFIG_WPS_UPNP
    1215         [ -  + ]:        136 :         if (hostapd_wps_upnp_init(hapd, wps) < 0) {
    1216                 :          0 :                 wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP");
    1217                 :          0 :                 wps_registrar_deinit(wps->registrar);
    1218                 :          0 :                 os_free(wps->network_key);
    1219                 :          0 :                 os_free(wps);
    1220                 :          0 :                 hapd->wps = NULL;
    1221                 :          0 :                 return -1;
    1222                 :            :         }
    1223                 :            : #endif /* CONFIG_WPS_UPNP */
    1224                 :            : 
    1225                 :        419 :         return 0;
    1226                 :            : }
    1227                 :            : 
    1228                 :            : 
    1229                 :        144 : static void hostapd_wps_nfc_clear(struct wps_context *wps)
    1230                 :            : {
    1231                 :            : #ifdef CONFIG_WPS_NFC
    1232                 :        144 :         wpa_printf(MSG_DEBUG, "WPS: Clear NFC Tag context %p", wps);
    1233                 :        144 :         wps->ap_nfc_dev_pw_id = 0;
    1234                 :        144 :         wpabuf_free(wps->ap_nfc_dh_pubkey);
    1235                 :        144 :         wps->ap_nfc_dh_pubkey = NULL;
    1236                 :        144 :         wpabuf_free(wps->ap_nfc_dh_privkey);
    1237                 :        144 :         wps->ap_nfc_dh_privkey = NULL;
    1238                 :        144 :         wpabuf_free(wps->ap_nfc_dev_pw);
    1239                 :        144 :         wps->ap_nfc_dev_pw = NULL;
    1240                 :            : #endif /* CONFIG_WPS_NFC */
    1241                 :        144 : }
    1242                 :            : 
    1243                 :            : 
    1244                 :        429 : void hostapd_deinit_wps(struct hostapd_data *hapd)
    1245                 :            : {
    1246                 :        429 :         eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
    1247                 :        429 :         eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
    1248                 :        429 :         eloop_cancel_timeout(wps_reload_config, hapd->iface, NULL);
    1249         [ +  + ]:        429 :         if (hapd->wps == NULL)
    1250                 :        429 :                 return;
    1251                 :            : #ifdef CONFIG_WPS_UPNP
    1252                 :        136 :         hostapd_wps_upnp_deinit(hapd);
    1253                 :            : #endif /* CONFIG_WPS_UPNP */
    1254                 :        136 :         wps_registrar_deinit(hapd->wps->registrar);
    1255                 :        136 :         os_free(hapd->wps->network_key);
    1256                 :        136 :         wps_device_data_free(&hapd->wps->dev);
    1257                 :        136 :         wpabuf_free(hapd->wps->dh_pubkey);
    1258                 :        136 :         wpabuf_free(hapd->wps->dh_privkey);
    1259                 :        136 :         wps_free_pending_msgs(hapd->wps->upnp_msgs);
    1260                 :        136 :         hostapd_wps_nfc_clear(hapd->wps);
    1261                 :        136 :         os_free(hapd->wps);
    1262                 :        136 :         hapd->wps = NULL;
    1263                 :        136 :         hostapd_wps_clear_ies(hapd);
    1264                 :            : }
    1265                 :            : 
    1266                 :            : 
    1267                 :         15 : void hostapd_update_wps(struct hostapd_data *hapd)
    1268                 :            : {
    1269         [ +  + ]:         15 :         if (hapd->wps == NULL)
    1270                 :         15 :                 return;
    1271                 :            : 
    1272                 :            : #ifdef CONFIG_WPS_UPNP
    1273                 :         14 :         hapd->wps->friendly_name = hapd->conf->friendly_name;
    1274                 :         14 :         hapd->wps->manufacturer_url = hapd->conf->manufacturer_url;
    1275                 :         14 :         hapd->wps->model_description = hapd->conf->model_description;
    1276                 :         14 :         hapd->wps->model_url = hapd->conf->model_url;
    1277                 :         14 :         hapd->wps->upc = hapd->conf->upc;
    1278                 :            : #endif /* CONFIG_WPS_UPNP */
    1279                 :            : 
    1280                 :         14 :         hostapd_wps_set_vendor_ext(hapd, hapd->wps);
    1281                 :            : 
    1282         [ +  - ]:         14 :         if (hapd->conf->wps_state)
    1283                 :         14 :                 wps_registrar_update_ie(hapd->wps->registrar);
    1284                 :            :         else
    1285                 :          0 :                 hostapd_deinit_wps(hapd);
    1286                 :            : }
    1287                 :            : 
    1288                 :            : 
    1289                 :            : struct wps_add_pin_data {
    1290                 :            :         const u8 *addr;
    1291                 :            :         const u8 *uuid;
    1292                 :            :         const u8 *pin;
    1293                 :            :         size_t pin_len;
    1294                 :            :         int timeout;
    1295                 :            :         int added;
    1296                 :            : };
    1297                 :            : 
    1298                 :            : 
    1299                 :         73 : static int wps_add_pin(struct hostapd_data *hapd, void *ctx)
    1300                 :            : {
    1301                 :         73 :         struct wps_add_pin_data *data = ctx;
    1302                 :            :         int ret;
    1303                 :            : 
    1304         [ -  + ]:         73 :         if (hapd->wps == NULL)
    1305                 :          0 :                 return 0;
    1306                 :         73 :         ret = wps_registrar_add_pin(hapd->wps->registrar, data->addr,
    1307                 :            :                                     data->uuid, data->pin, data->pin_len,
    1308                 :            :                                     data->timeout);
    1309         [ +  - ]:         73 :         if (ret == 0)
    1310                 :         73 :                 data->added++;
    1311                 :         73 :         return ret;
    1312                 :            : }
    1313                 :            : 
    1314                 :            : 
    1315                 :         72 : int hostapd_wps_add_pin(struct hostapd_data *hapd, const u8 *addr,
    1316                 :            :                         const char *uuid, const char *pin, int timeout)
    1317                 :            : {
    1318                 :            :         u8 u[UUID_LEN];
    1319                 :            :         struct wps_add_pin_data data;
    1320                 :            : 
    1321                 :         72 :         data.addr = addr;
    1322                 :         72 :         data.uuid = u;
    1323                 :         72 :         data.pin = (const u8 *) pin;
    1324                 :         72 :         data.pin_len = os_strlen(pin);
    1325                 :         72 :         data.timeout = timeout;
    1326                 :         72 :         data.added = 0;
    1327                 :            : 
    1328         [ +  + ]:         72 :         if (os_strcmp(uuid, "any") == 0)
    1329                 :         70 :                 data.uuid = NULL;
    1330                 :            :         else {
    1331         [ -  + ]:          2 :                 if (uuid_str2bin(uuid, u))
    1332                 :          0 :                         return -1;
    1333                 :          2 :                 data.uuid = u;
    1334                 :            :         }
    1335         [ -  + ]:         72 :         if (hostapd_wps_for_each(hapd, wps_add_pin, &data) < 0)
    1336                 :          0 :                 return -1;
    1337         [ +  - ]:         72 :         return data.added ? 0 : -1;
    1338                 :            : }
    1339                 :            : 
    1340                 :            : 
    1341                 :         21 : static int wps_button_pushed(struct hostapd_data *hapd, void *ctx)
    1342                 :            : {
    1343                 :         21 :         const u8 *p2p_dev_addr = ctx;
    1344         [ -  + ]:         21 :         if (hapd->wps == NULL)
    1345                 :          0 :                 return 0;
    1346                 :         21 :         return wps_registrar_button_pushed(hapd->wps->registrar, p2p_dev_addr);
    1347                 :            : }
    1348                 :            : 
    1349                 :            : 
    1350                 :         20 : int hostapd_wps_button_pushed(struct hostapd_data *hapd,
    1351                 :            :                               const u8 *p2p_dev_addr)
    1352                 :            : {
    1353                 :         20 :         return hostapd_wps_for_each(hapd, wps_button_pushed,
    1354                 :            :                                     (void *) p2p_dev_addr);
    1355                 :            : }
    1356                 :            : 
    1357                 :            : 
    1358                 :          3 : static int wps_cancel(struct hostapd_data *hapd, void *ctx)
    1359                 :            : {
    1360         [ -  + ]:          3 :         if (hapd->wps == NULL)
    1361                 :          0 :                 return 0;
    1362                 :            : 
    1363                 :          3 :         wps_registrar_wps_cancel(hapd->wps->registrar);
    1364                 :          3 :         ap_for_each_sta(hapd, ap_sta_wps_cancel, NULL);
    1365                 :            : 
    1366                 :          3 :         return 0;
    1367                 :            : }
    1368                 :            : 
    1369                 :            : 
    1370                 :          3 : int hostapd_wps_cancel(struct hostapd_data *hapd)
    1371                 :            : {
    1372                 :          3 :         return hostapd_wps_for_each(hapd, wps_cancel, NULL);
    1373                 :            : }
    1374                 :            : 
    1375                 :            : 
    1376                 :        290 : static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr, const u8 *da,
    1377                 :            :                                     const u8 *bssid,
    1378                 :            :                                     const u8 *ie, size_t ie_len,
    1379                 :            :                                     int ssi_signal)
    1380                 :            : {
    1381                 :        290 :         struct hostapd_data *hapd = ctx;
    1382                 :            :         struct wpabuf *wps_ie;
    1383                 :            :         struct ieee802_11_elems elems;
    1384                 :            : 
    1385         [ -  + ]:        290 :         if (hapd->wps == NULL)
    1386                 :          0 :                 return 0;
    1387                 :            : 
    1388         [ -  + ]:        290 :         if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
    1389                 :          0 :                 wpa_printf(MSG_DEBUG, "WPS: Could not parse ProbeReq from "
    1390                 :          0 :                            MACSTR, MAC2STR(addr));
    1391                 :          0 :                 return 0;
    1392                 :            :         }
    1393                 :            : 
    1394 [ +  - ][ +  + ]:        290 :         if (elems.ssid && elems.ssid_len > 0 &&
                 [ +  + ]
    1395         [ -  + ]:         87 :             (elems.ssid_len != hapd->conf->ssid.ssid_len ||
    1396                 :         87 :              os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) !=
    1397                 :            :              0))
    1398                 :         60 :                 return 0; /* Not for us */
    1399                 :            : 
    1400                 :        230 :         wps_ie = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
    1401         [ +  + ]:        230 :         if (wps_ie == NULL)
    1402                 :          1 :                 return 0;
    1403         [ -  + ]:        229 :         if (wps_validate_probe_req(wps_ie, addr) < 0) {
    1404                 :          0 :                 wpabuf_free(wps_ie);
    1405                 :          0 :                 return 0;
    1406                 :            :         }
    1407                 :            : 
    1408         [ +  - ]:        229 :         if (wpabuf_len(wps_ie) > 0) {
    1409                 :        229 :                 int p2p_wildcard = 0;
    1410                 :            : #ifdef CONFIG_P2P
    1411 [ +  - ][ -  + ]:        114 :                 if (elems.ssid && elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
                 [ #  # ]
    1412                 :          0 :                     os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
    1413                 :            :                               P2P_WILDCARD_SSID_LEN) == 0)
    1414                 :          0 :                         p2p_wildcard = 1;
    1415                 :            : #endif /* CONFIG_P2P */
    1416                 :        229 :                 wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie,
    1417                 :            :                                            p2p_wildcard);
    1418                 :            : #ifdef CONFIG_WPS_UPNP
    1419                 :            :                 /* FIX: what exactly should be included in the WLANEvent?
    1420                 :            :                  * WPS attributes? Full ProbeReq frame? */
    1421         [ +  - ]:        229 :                 if (!p2p_wildcard)
    1422                 :        229 :                         upnp_wps_device_send_wlan_event(
    1423                 :            :                                 hapd->wps_upnp, addr,
    1424                 :            :                                 UPNP_WPS_WLANEVENT_TYPE_PROBE, wps_ie);
    1425                 :            : #endif /* CONFIG_WPS_UPNP */
    1426                 :            :         }
    1427                 :            : 
    1428                 :        229 :         wpabuf_free(wps_ie);
    1429                 :            : 
    1430                 :        290 :         return 0;
    1431                 :            : }
    1432                 :            : 
    1433                 :            : 
    1434                 :            : #ifdef CONFIG_WPS_UPNP
    1435                 :            : 
    1436                 :         23 : static int hostapd_rx_req_put_wlan_response(
    1437                 :            :         void *priv, enum upnp_wps_wlanevent_type ev_type,
    1438                 :            :         const u8 *mac_addr, const struct wpabuf *msg,
    1439                 :            :         enum wps_msg_type msg_type)
    1440                 :            : {
    1441                 :         23 :         struct hostapd_data *hapd = priv;
    1442                 :            :         struct sta_info *sta;
    1443                 :            :         struct upnp_pending_message *p;
    1444                 :            : 
    1445                 :         23 :         wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
    1446                 :        138 :                    MACSTR, ev_type, MAC2STR(mac_addr));
    1447                 :         23 :         wpa_hexdump(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
    1448                 :            :                     wpabuf_head(msg), wpabuf_len(msg));
    1449         [ -  + ]:         23 :         if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) {
    1450                 :          0 :                 wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected "
    1451                 :            :                            "PutWLANResponse WLANEventType %d", ev_type);
    1452                 :          0 :                 return -1;
    1453                 :            :         }
    1454                 :            : 
    1455                 :            :         /*
    1456                 :            :          * EAP response to ongoing to WPS Registration. Send it to EAP-WSC
    1457                 :            :          * server implementation for delivery to the peer.
    1458                 :            :          */
    1459                 :            : 
    1460                 :         23 :         sta = ap_get_sta(hapd, mac_addr);
    1461                 :            : #ifndef CONFIG_WPS_STRICT
    1462         [ -  + ]:         23 :         if (!sta) {
    1463                 :            :                 /*
    1464                 :            :                  * Workaround - Intel wsccmd uses bogus NewWLANEventMAC:
    1465                 :            :                  * Pick STA that is in an ongoing WPS registration without
    1466                 :            :                  * checking the MAC address.
    1467                 :            :                  */
    1468                 :          0 :                 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based "
    1469                 :            :                            "on NewWLANEventMAC; try wildcard match");
    1470         [ #  # ]:          0 :                 for (sta = hapd->sta_list; sta; sta = sta->next) {
    1471 [ #  # ][ #  # ]:          0 :                         if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS))
    1472                 :          0 :                                 break;
    1473                 :            :                 }
    1474                 :            :         }
    1475                 :            : #endif /* CONFIG_WPS_STRICT */
    1476                 :            : 
    1477 [ +  - ][ -  + ]:         23 :         if (!sta || !(sta->flags & WLAN_STA_WPS)) {
    1478                 :          0 :                 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found");
    1479                 :          0 :                 return 0;
    1480                 :            :         }
    1481                 :            : 
    1482         [ -  + ]:         23 :         if (!sta->eapol_sm) {
    1483                 :            :                 /*
    1484                 :            :                  * This can happen, e.g., if an ER sends an extra message after
    1485                 :            :                  * the station has disassociated (but not fully
    1486                 :            :                  * deauthenticated).
    1487                 :            :                  */
    1488                 :          0 :                 wpa_printf(MSG_DEBUG, "WPS UPnP: Matching STA did not have EAPOL state machine initialized");
    1489                 :          0 :                 return 0;
    1490                 :            :         }
    1491                 :            : 
    1492                 :         23 :         p = os_zalloc(sizeof(*p));
    1493         [ -  + ]:         23 :         if (p == NULL)
    1494                 :          0 :                 return -1;
    1495                 :         23 :         os_memcpy(p->addr, sta->addr, ETH_ALEN);
    1496                 :         23 :         p->msg = wpabuf_dup(msg);
    1497                 :         23 :         p->type = msg_type;
    1498                 :         23 :         p->next = hapd->wps->upnp_msgs;
    1499                 :         23 :         hapd->wps->upnp_msgs = p;
    1500                 :            : 
    1501                 :         23 :         return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
    1502                 :            : }
    1503                 :            : 
    1504                 :            : 
    1505                 :        136 : static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
    1506                 :            :                                  struct wps_context *wps)
    1507                 :            : {
    1508                 :            :         struct upnp_wps_device_ctx *ctx;
    1509                 :            : 
    1510         [ +  + ]:        136 :         if (!hapd->conf->upnp_iface)
    1511                 :        121 :                 return 0;
    1512                 :         15 :         ctx = os_zalloc(sizeof(*ctx));
    1513         [ -  + ]:         15 :         if (ctx == NULL)
    1514                 :          0 :                 return -1;
    1515                 :            : 
    1516                 :         15 :         ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
    1517         [ +  + ]:         15 :         if (hapd->conf->ap_pin)
    1518                 :         14 :                 ctx->ap_pin = os_strdup(hapd->conf->ap_pin);
    1519                 :            : 
    1520                 :         15 :         hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd,
    1521                 :         15 :                                               hapd->conf->upnp_iface);
    1522         [ -  + ]:         15 :         if (hapd->wps_upnp == NULL)
    1523                 :          0 :                 return -1;
    1524                 :         15 :         wps->wps_upnp = hapd->wps_upnp;
    1525                 :            : 
    1526                 :        136 :         return 0;
    1527                 :            : }
    1528                 :            : 
    1529                 :            : 
    1530                 :        136 : static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
    1531                 :            : {
    1532                 :        136 :         upnp_wps_device_deinit(hapd->wps_upnp, hapd);
    1533                 :        136 : }
    1534                 :            : 
    1535                 :            : #endif /* CONFIG_WPS_UPNP */
    1536                 :            : 
    1537                 :            : 
    1538                 :         19 : int hostapd_wps_get_mib_sta(struct hostapd_data *hapd, const u8 *addr,
    1539                 :            :                             char *buf, size_t buflen)
    1540                 :            : {
    1541         [ +  + ]:         19 :         if (hapd->wps == NULL)
    1542                 :         15 :                 return 0;
    1543                 :         19 :         return wps_registrar_get_info(hapd->wps->registrar, addr, buf, buflen);
    1544                 :            : }
    1545                 :            : 
    1546                 :            : 
    1547                 :          2 : static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
    1548                 :            : {
    1549                 :          2 :         struct hostapd_data *hapd = eloop_data;
    1550                 :          2 :         wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
    1551                 :          2 :         hostapd_wps_ap_pin_disable(hapd);
    1552                 :          2 :         wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_PIN_DISABLED);
    1553                 :          2 : }
    1554                 :            : 
    1555                 :            : 
    1556                 :          4 : static void hostapd_wps_ap_pin_enable(struct hostapd_data *hapd, int timeout)
    1557                 :            : {
    1558                 :          4 :         wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
    1559                 :          4 :         hapd->ap_pin_failures = 0;
    1560                 :          4 :         hapd->ap_pin_failures_consecutive = 0;
    1561                 :          4 :         hapd->conf->ap_setup_locked = 0;
    1562         [ -  + ]:          4 :         if (hapd->wps->ap_setup_locked) {
    1563                 :          0 :                 wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
    1564                 :          0 :                 hapd->wps->ap_setup_locked = 0;
    1565                 :          0 :                 wps_registrar_update_ie(hapd->wps->registrar);
    1566                 :            :         }
    1567                 :          4 :         eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
    1568         [ +  - ]:          4 :         if (timeout > 0)
    1569                 :          4 :                 eloop_register_timeout(timeout, 0,
    1570                 :            :                                        hostapd_wps_ap_pin_timeout, hapd, NULL);
    1571                 :          4 : }
    1572                 :            : 
    1573                 :            : 
    1574                 :          5 : static int wps_ap_pin_disable(struct hostapd_data *hapd, void *ctx)
    1575                 :            : {
    1576                 :          5 :         os_free(hapd->conf->ap_pin);
    1577                 :          5 :         hapd->conf->ap_pin = NULL;
    1578                 :            : #ifdef CONFIG_WPS_UPNP
    1579                 :          5 :         upnp_wps_set_ap_pin(hapd->wps_upnp, NULL);
    1580                 :            : #endif /* CONFIG_WPS_UPNP */
    1581                 :          5 :         eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
    1582                 :          5 :         return 0;
    1583                 :            : }
    1584                 :            : 
    1585                 :            : 
    1586                 :          5 : void hostapd_wps_ap_pin_disable(struct hostapd_data *hapd)
    1587                 :            : {
    1588                 :          5 :         wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
    1589                 :          5 :         hostapd_wps_for_each(hapd, wps_ap_pin_disable, NULL);
    1590                 :          5 : }
    1591                 :            : 
    1592                 :            : 
    1593                 :            : struct wps_ap_pin_data {
    1594                 :            :         char pin_txt[9];
    1595                 :            :         int timeout;
    1596                 :            : };
    1597                 :            : 
    1598                 :            : 
    1599                 :          4 : static int wps_ap_pin_set(struct hostapd_data *hapd, void *ctx)
    1600                 :            : {
    1601                 :          4 :         struct wps_ap_pin_data *data = ctx;
    1602                 :          4 :         os_free(hapd->conf->ap_pin);
    1603                 :          4 :         hapd->conf->ap_pin = os_strdup(data->pin_txt);
    1604                 :            : #ifdef CONFIG_WPS_UPNP
    1605                 :          4 :         upnp_wps_set_ap_pin(hapd->wps_upnp, data->pin_txt);
    1606                 :            : #endif /* CONFIG_WPS_UPNP */
    1607                 :          4 :         hostapd_wps_ap_pin_enable(hapd, data->timeout);
    1608                 :          4 :         return 0;
    1609                 :            : }
    1610                 :            : 
    1611                 :            : 
    1612                 :          2 : const char * hostapd_wps_ap_pin_random(struct hostapd_data *hapd, int timeout)
    1613                 :            : {
    1614                 :            :         unsigned int pin;
    1615                 :            :         struct wps_ap_pin_data data;
    1616                 :            : 
    1617                 :          2 :         pin = wps_generate_pin();
    1618                 :          2 :         os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%08u", pin);
    1619                 :          2 :         data.timeout = timeout;
    1620                 :          2 :         hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
    1621                 :          2 :         return hapd->conf->ap_pin;
    1622                 :            : }
    1623                 :            : 
    1624                 :            : 
    1625                 :          3 : const char * hostapd_wps_ap_pin_get(struct hostapd_data *hapd)
    1626                 :            : {
    1627                 :          3 :         return hapd->conf->ap_pin;
    1628                 :            : }
    1629                 :            : 
    1630                 :            : 
    1631                 :          2 : int hostapd_wps_ap_pin_set(struct hostapd_data *hapd, const char *pin,
    1632                 :            :                            int timeout)
    1633                 :            : {
    1634                 :            :         struct wps_ap_pin_data data;
    1635                 :            :         int ret;
    1636                 :            : 
    1637                 :          2 :         ret = os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%s", pin);
    1638 [ +  - ][ -  + ]:          2 :         if (ret < 0 || ret >= (int) sizeof(data.pin_txt))
    1639                 :          0 :                 return -1;
    1640                 :          2 :         data.timeout = timeout;
    1641                 :          2 :         return hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
    1642                 :            : }
    1643                 :            : 
    1644                 :            : 
    1645                 :          2 : static int wps_update_ie(struct hostapd_data *hapd, void *ctx)
    1646                 :            : {
    1647         [ +  - ]:          2 :         if (hapd->wps)
    1648                 :          2 :                 wps_registrar_update_ie(hapd->wps->registrar);
    1649                 :          2 :         return 0;
    1650                 :            : }
    1651                 :            : 
    1652                 :            : 
    1653                 :          2 : void hostapd_wps_update_ie(struct hostapd_data *hapd)
    1654                 :            : {
    1655                 :          2 :         hostapd_wps_for_each(hapd, wps_update_ie, NULL);
    1656                 :          2 : }
    1657                 :            : 
    1658                 :            : 
    1659                 :          1 : int hostapd_wps_config_ap(struct hostapd_data *hapd, const char *ssid,
    1660                 :            :                           const char *auth, const char *encr, const char *key)
    1661                 :            : {
    1662                 :            :         struct wps_credential cred;
    1663                 :            :         size_t len;
    1664                 :            : 
    1665                 :          1 :         os_memset(&cred, 0, sizeof(cred));
    1666                 :            : 
    1667                 :          1 :         len = os_strlen(ssid);
    1668         [ +  - ]:          2 :         if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
           [ +  -  -  + ]
    1669                 :          1 :             hexstr2bin(ssid, cred.ssid, len / 2))
    1670                 :          0 :                 return -1;
    1671                 :          1 :         cred.ssid_len = len / 2;
    1672                 :            : 
    1673         [ -  + ]:          1 :         if (os_strncmp(auth, "OPEN", 4) == 0)
    1674                 :          0 :                 cred.auth_type = WPS_AUTH_OPEN;
    1675         [ -  + ]:          1 :         else if (os_strncmp(auth, "WPAPSK", 6) == 0)
    1676                 :          0 :                 cred.auth_type = WPS_AUTH_WPAPSK;
    1677         [ +  - ]:          1 :         else if (os_strncmp(auth, "WPA2PSK", 7) == 0)
    1678                 :          1 :                 cred.auth_type = WPS_AUTH_WPA2PSK;
    1679                 :            :         else
    1680                 :          0 :                 return -1;
    1681                 :            : 
    1682         [ +  - ]:          1 :         if (encr) {
    1683         [ -  + ]:          1 :                 if (os_strncmp(encr, "NONE", 4) == 0)
    1684                 :          0 :                         cred.encr_type = WPS_ENCR_NONE;
    1685         [ -  + ]:          1 :                 else if (os_strncmp(encr, "WEP", 3) == 0)
    1686                 :          0 :                         cred.encr_type = WPS_ENCR_WEP;
    1687         [ -  + ]:          1 :                 else if (os_strncmp(encr, "TKIP", 4) == 0)
    1688                 :          0 :                         cred.encr_type = WPS_ENCR_TKIP;
    1689         [ +  - ]:          1 :                 else if (os_strncmp(encr, "CCMP", 4) == 0)
    1690                 :          1 :                         cred.encr_type = WPS_ENCR_AES;
    1691                 :            :                 else
    1692                 :          0 :                         return -1;
    1693                 :            :         } else
    1694                 :          0 :                 cred.encr_type = WPS_ENCR_NONE;
    1695                 :            : 
    1696         [ +  - ]:          1 :         if (key) {
    1697                 :          1 :                 len = os_strlen(key);
    1698         [ +  - ]:          2 :                 if ((len & 1) || len > 2 * sizeof(cred.key) ||
           [ +  -  -  + ]
    1699                 :          1 :                     hexstr2bin(key, cred.key, len / 2))
    1700                 :          0 :                         return -1;
    1701                 :          1 :                 cred.key_len = len / 2;
    1702                 :            :         }
    1703                 :            : 
    1704                 :          1 :         return wps_registrar_config_ap(hapd->wps->registrar, &cred);
    1705                 :            : }
    1706                 :            : 
    1707                 :            : 
    1708                 :            : #ifdef CONFIG_WPS_NFC
    1709                 :            : 
    1710                 :            : struct wps_nfc_password_token_data {
    1711                 :            :         const u8 *oob_dev_pw;
    1712                 :            :         size_t oob_dev_pw_len;
    1713                 :            :         int added;
    1714                 :            : };
    1715                 :            : 
    1716                 :            : 
    1717                 :          2 : static int wps_add_nfc_password_token(struct hostapd_data *hapd, void *ctx)
    1718                 :            : {
    1719                 :          2 :         struct wps_nfc_password_token_data *data = ctx;
    1720                 :            :         int ret;
    1721                 :            : 
    1722         [ -  + ]:          2 :         if (hapd->wps == NULL)
    1723                 :          0 :                 return 0;
    1724                 :          2 :         ret = wps_registrar_add_nfc_password_token(hapd->wps->registrar,
    1725                 :            :                                                    data->oob_dev_pw,
    1726                 :            :                                                    data->oob_dev_pw_len);
    1727         [ +  - ]:          2 :         if (ret == 0)
    1728                 :          2 :                 data->added++;
    1729                 :          2 :         return ret;
    1730                 :            : }
    1731                 :            : 
    1732                 :            : 
    1733                 :          2 : static int hostapd_wps_add_nfc_password_token(struct hostapd_data *hapd,
    1734                 :            :                                               struct wps_parse_attr *attr)
    1735                 :            : {
    1736                 :            :         struct wps_nfc_password_token_data data;
    1737                 :            : 
    1738                 :          2 :         data.oob_dev_pw = attr->oob_dev_password;
    1739                 :          2 :         data.oob_dev_pw_len = attr->oob_dev_password_len;
    1740                 :          2 :         data.added = 0;
    1741         [ -  + ]:          2 :         if (hostapd_wps_for_each(hapd, wps_add_nfc_password_token, &data) < 0)
    1742                 :          0 :                 return -1;
    1743         [ +  - ]:          2 :         return data.added ? 0 : -1;
    1744                 :            : }
    1745                 :            : 
    1746                 :            : 
    1747                 :          2 : static int hostapd_wps_nfc_tag_process(struct hostapd_data *hapd,
    1748                 :            :                                        const struct wpabuf *wps)
    1749                 :            : {
    1750                 :            :         struct wps_parse_attr attr;
    1751                 :            : 
    1752                 :          2 :         wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
    1753                 :            : 
    1754         [ -  + ]:          2 :         if (wps_parse_msg(wps, &attr)) {
    1755                 :          0 :                 wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
    1756                 :          0 :                 return -1;
    1757                 :            :         }
    1758                 :            : 
    1759         [ +  - ]:          2 :         if (attr.oob_dev_password)
    1760                 :          2 :                 return hostapd_wps_add_nfc_password_token(hapd, &attr);
    1761                 :            : 
    1762                 :          0 :         wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
    1763                 :          2 :         return -1;
    1764                 :            : }
    1765                 :            : 
    1766                 :            : 
    1767                 :          2 : int hostapd_wps_nfc_tag_read(struct hostapd_data *hapd,
    1768                 :            :                              const struct wpabuf *data)
    1769                 :            : {
    1770                 :          2 :         const struct wpabuf *wps = data;
    1771                 :          2 :         struct wpabuf *tmp = NULL;
    1772                 :            :         int ret;
    1773                 :            : 
    1774         [ -  + ]:          2 :         if (wpabuf_len(data) < 4)
    1775                 :          0 :                 return -1;
    1776                 :            : 
    1777         [ +  - ]:          2 :         if (*wpabuf_head_u8(data) != 0x10) {
    1778                 :            :                 /* Assume this contains full NDEF record */
    1779                 :          2 :                 tmp = ndef_parse_wifi(data);
    1780         [ -  + ]:          2 :                 if (tmp == NULL) {
    1781                 :          0 :                         wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
    1782                 :          0 :                         return -1;
    1783                 :            :                 }
    1784                 :          2 :                 wps = tmp;
    1785                 :            :         }
    1786                 :            : 
    1787                 :          2 :         ret = hostapd_wps_nfc_tag_process(hapd, wps);
    1788                 :          2 :         wpabuf_free(tmp);
    1789                 :          2 :         return ret;
    1790                 :            : }
    1791                 :            : 
    1792                 :            : 
    1793                 :          3 : struct wpabuf * hostapd_wps_nfc_config_token(struct hostapd_data *hapd,
    1794                 :            :                                              int ndef)
    1795                 :            : {
    1796                 :            :         struct wpabuf *ret;
    1797                 :            : 
    1798         [ -  + ]:          3 :         if (hapd->wps == NULL)
    1799                 :          0 :                 return NULL;
    1800                 :            : 
    1801                 :          3 :         ret = wps_get_oob_cred(hapd->wps, hostapd_wps_rf_band_cb(hapd),
    1802                 :          3 :                                hapd->iconf->channel);
    1803 [ +  - ][ +  - ]:          3 :         if (ndef && ret) {
    1804                 :            :                 struct wpabuf *tmp;
    1805                 :          3 :                 tmp = ndef_build_wifi(ret);
    1806                 :          3 :                 wpabuf_free(ret);
    1807         [ -  + ]:          3 :                 if (tmp == NULL)
    1808                 :          0 :                         return NULL;
    1809                 :          3 :                 ret = tmp;
    1810                 :            :         }
    1811                 :            : 
    1812                 :          3 :         return ret;
    1813                 :            : }
    1814                 :            : 
    1815                 :            : 
    1816                 :          6 : struct wpabuf * hostapd_wps_nfc_hs_cr(struct hostapd_data *hapd, int ndef)
    1817                 :            : {
    1818                 :            :         struct wpabuf *ret;
    1819                 :            : 
    1820         [ -  + ]:          6 :         if (hapd->wps == NULL)
    1821                 :          0 :                 return NULL;
    1822                 :            : 
    1823         [ +  + ]:          6 :         if (hapd->conf->wps_nfc_dh_pubkey == NULL) {
    1824                 :          5 :                 struct wps_context *wps = hapd->wps;
    1825         [ -  + ]:          5 :                 if (wps_nfc_gen_dh(&hapd->conf->wps_nfc_dh_pubkey,
    1826                 :          5 :                                    &hapd->conf->wps_nfc_dh_privkey) < 0)
    1827                 :          0 :                         return NULL;
    1828                 :          5 :                 hostapd_wps_nfc_clear(wps);
    1829                 :          5 :                 wps->ap_nfc_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
    1830                 :          5 :                 wps->ap_nfc_dh_pubkey =
    1831                 :          5 :                         wpabuf_dup(hapd->conf->wps_nfc_dh_pubkey);
    1832                 :          5 :                 wps->ap_nfc_dh_privkey =
    1833                 :          5 :                         wpabuf_dup(hapd->conf->wps_nfc_dh_privkey);
    1834 [ -  + ][ +  - ]:          5 :                 if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey) {
    1835                 :          0 :                         hostapd_wps_nfc_clear(wps);
    1836                 :          0 :                         return NULL;
    1837                 :            :                 }
    1838                 :            :         }
    1839                 :            : 
    1840                 :          6 :         ret = wps_build_nfc_handover_sel(hapd->wps,
    1841                 :          6 :                                          hapd->conf->wps_nfc_dh_pubkey,
    1842                 :         12 :                                          hapd->own_addr, hapd->iface->freq);
    1843                 :            : 
    1844 [ +  - ][ +  - ]:          6 :         if (ndef && ret) {
    1845                 :            :                 struct wpabuf *tmp;
    1846                 :          6 :                 tmp = ndef_build_wifi(ret);
    1847                 :          6 :                 wpabuf_free(ret);
    1848         [ -  + ]:          6 :                 if (tmp == NULL)
    1849                 :          0 :                         return NULL;
    1850                 :          6 :                 ret = tmp;
    1851                 :            :         }
    1852                 :            : 
    1853                 :          6 :         return ret;
    1854                 :            : }
    1855                 :            : 
    1856                 :            : 
    1857                 :          6 : int hostapd_wps_nfc_report_handover(struct hostapd_data *hapd,
    1858                 :            :                                     const struct wpabuf *req,
    1859                 :            :                                     const struct wpabuf *sel)
    1860                 :            : {
    1861                 :            :         struct wpabuf *wps;
    1862                 :          6 :         int ret = -1;
    1863                 :            :         u16 wsc_len;
    1864                 :            :         const u8 *pos;
    1865                 :            :         struct wpabuf msg;
    1866                 :            :         struct wps_parse_attr attr;
    1867                 :            :         u16 dev_pw_id;
    1868                 :            : 
    1869                 :            :         /*
    1870                 :            :          * Enrollee/station is always initiator of the NFC connection handover,
    1871                 :            :          * so use the request message here to find Enrollee public key hash.
    1872                 :            :          */
    1873                 :          6 :         wps = ndef_parse_wifi(req);
    1874         [ -  + ]:          6 :         if (wps == NULL)
    1875                 :          0 :                 return -1;
    1876                 :          6 :         wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
    1877                 :            :                    "payload from NFC connection handover");
    1878                 :          6 :         wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
    1879         [ -  + ]:          6 :         if (wpabuf_len(wps) < 2) {
    1880                 :          0 :                 wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Request "
    1881                 :            :                            "Message");
    1882                 :          0 :                 goto out;
    1883                 :            :         }
    1884                 :          6 :         pos = wpabuf_head(wps);
    1885                 :          6 :         wsc_len = WPA_GET_BE16(pos);
    1886         [ -  + ]:          6 :         if (wsc_len > wpabuf_len(wps) - 2) {
    1887                 :          0 :                 wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
    1888                 :            :                            "in rt Wi-Fi Handover Request Message", wsc_len);
    1889                 :          0 :                 goto out;
    1890                 :            :         }
    1891                 :          6 :         pos += 2;
    1892                 :            : 
    1893                 :          6 :         wpa_hexdump(MSG_DEBUG,
    1894                 :            :                     "WPS: WSC attributes in Wi-Fi Handover Request Message",
    1895                 :            :                     pos, wsc_len);
    1896         [ -  + ]:          6 :         if (wsc_len < wpabuf_len(wps) - 2) {
    1897                 :          0 :                 wpa_hexdump(MSG_DEBUG,
    1898                 :            :                             "WPS: Ignore extra data after WSC attributes",
    1899                 :          0 :                             pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
    1900                 :            :         }
    1901                 :            : 
    1902                 :          6 :         wpabuf_set(&msg, pos, wsc_len);
    1903                 :          6 :         ret = wps_parse_msg(&msg, &attr);
    1904         [ -  + ]:          6 :         if (ret < 0) {
    1905                 :          0 :                 wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
    1906                 :            :                            "Wi-Fi Handover Request Message");
    1907                 :          0 :                 goto out;
    1908                 :            :         }
    1909                 :            : 
    1910 [ +  - ][ -  + ]:          6 :         if (attr.oob_dev_password == NULL ||
    1911                 :          6 :             attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
    1912                 :          0 :                 wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
    1913                 :            :                            "included in Wi-Fi Handover Request Message");
    1914                 :          0 :                 ret = -1;
    1915                 :          0 :                 goto out;
    1916                 :            :         }
    1917                 :            : 
    1918         [ -  + ]:          6 :         if (attr.uuid_e == NULL) {
    1919                 :          0 :                 wpa_printf(MSG_DEBUG, "WPS: No UUID-E included in Wi-Fi "
    1920                 :            :                            "Handover Request Message");
    1921                 :          0 :                 ret = -1;
    1922                 :          0 :                 goto out;
    1923                 :            :         }
    1924                 :            : 
    1925                 :          6 :         wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", attr.uuid_e, WPS_UUID_LEN);
    1926                 :            : 
    1927                 :          6 :         wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
    1928                 :          6 :                     attr.oob_dev_password, attr.oob_dev_password_len);
    1929                 :          6 :         dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
    1930                 :            :                                  WPS_OOB_PUBKEY_HASH_LEN);
    1931         [ -  + ]:          6 :         if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
    1932                 :          0 :                 wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
    1933                 :            :                            "%u in Wi-Fi Handover Request Message", dev_pw_id);
    1934                 :          0 :                 ret = -1;
    1935                 :          0 :                 goto out;
    1936                 :            :         }
    1937                 :          6 :         wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Public Key hash",
    1938                 :          6 :                     attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
    1939                 :            : 
    1940                 :          6 :         ret = wps_registrar_add_nfc_pw_token(hapd->wps->registrar,
    1941                 :            :                                              attr.oob_dev_password,
    1942                 :            :                                              DEV_PW_NFC_CONNECTION_HANDOVER,
    1943                 :            :                                              NULL, 0, 1);
    1944                 :            : 
    1945                 :            : out:
    1946                 :          6 :         wpabuf_free(wps);
    1947                 :          6 :         return ret;
    1948                 :            : }
    1949                 :            : 
    1950                 :            : 
    1951                 :          2 : struct wpabuf * hostapd_wps_nfc_token_gen(struct hostapd_data *hapd, int ndef)
    1952                 :            : {
    1953         [ -  + ]:          2 :         if (hapd->conf->wps_nfc_pw_from_config) {
    1954                 :          0 :                 return wps_nfc_token_build(ndef,
    1955                 :          0 :                                            hapd->conf->wps_nfc_dev_pw_id,
    1956                 :          0 :                                            hapd->conf->wps_nfc_dh_pubkey,
    1957                 :          0 :                                            hapd->conf->wps_nfc_dev_pw);
    1958                 :            :         }
    1959                 :            : 
    1960                 :          2 :         return wps_nfc_token_gen(ndef, &hapd->conf->wps_nfc_dev_pw_id,
    1961                 :          2 :                                  &hapd->conf->wps_nfc_dh_pubkey,
    1962                 :          2 :                                  &hapd->conf->wps_nfc_dh_privkey,
    1963                 :          2 :                                  &hapd->conf->wps_nfc_dev_pw);
    1964                 :            : }
    1965                 :            : 
    1966                 :            : 
    1967                 :          3 : int hostapd_wps_nfc_token_enable(struct hostapd_data *hapd)
    1968                 :            : {
    1969                 :          3 :         struct wps_context *wps = hapd->wps;
    1970                 :            :         struct wpabuf *pw;
    1971                 :            : 
    1972         [ -  + ]:          3 :         if (wps == NULL)
    1973                 :          0 :                 return -1;
    1974                 :            : 
    1975 [ +  - ][ +  - ]:          3 :         if (!hapd->conf->wps_nfc_dh_pubkey ||
    1976         [ +  - ]:          3 :             !hapd->conf->wps_nfc_dh_privkey ||
    1977         [ -  + ]:          3 :             !hapd->conf->wps_nfc_dev_pw ||
    1978                 :          3 :             !hapd->conf->wps_nfc_dev_pw_id)
    1979                 :          0 :                 return -1;
    1980                 :            : 
    1981                 :          3 :         hostapd_wps_nfc_clear(wps);
    1982                 :          3 :         wpa_printf(MSG_DEBUG,
    1983                 :            :                    "WPS: Enable NFC Tag (Dev Pw Id %u) for AP interface %s (context %p)",
    1984                 :          3 :                    hapd->conf->wps_nfc_dev_pw_id, hapd->conf->iface, wps);
    1985                 :          3 :         wps->ap_nfc_dev_pw_id = hapd->conf->wps_nfc_dev_pw_id;
    1986                 :          3 :         wps->ap_nfc_dh_pubkey = wpabuf_dup(hapd->conf->wps_nfc_dh_pubkey);
    1987                 :          3 :         wps->ap_nfc_dh_privkey = wpabuf_dup(hapd->conf->wps_nfc_dh_privkey);
    1988                 :          3 :         pw = hapd->conf->wps_nfc_dev_pw;
    1989                 :          3 :         wps->ap_nfc_dev_pw = wpabuf_alloc(
    1990                 :          3 :                 wpabuf_len(pw) * 2 + 1);
    1991         [ +  - ]:          3 :         if (wps->ap_nfc_dev_pw) {
    1992                 :          6 :                 wpa_snprintf_hex_uppercase(
    1993                 :          3 :                         (char *) wpabuf_put(wps->ap_nfc_dev_pw,
    1994                 :          3 :                                             wpabuf_len(pw) * 2),
    1995                 :          3 :                         wpabuf_len(pw) * 2 + 1,
    1996                 :          3 :                         wpabuf_head(pw), wpabuf_len(pw));
    1997                 :            :         }
    1998                 :            : 
    1999 [ +  - ][ +  - ]:          3 :         if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey ||
                 [ -  + ]
    2000                 :          3 :             !wps->ap_nfc_dev_pw) {
    2001                 :          0 :                 hostapd_wps_nfc_clear(wps);
    2002                 :          0 :                 return -1;
    2003                 :            :         }
    2004                 :            : 
    2005                 :          3 :         return 0;
    2006                 :            : }
    2007                 :            : 
    2008                 :            : 
    2009                 :          0 : void hostapd_wps_nfc_token_disable(struct hostapd_data *hapd)
    2010                 :            : {
    2011                 :          0 :         wpa_printf(MSG_DEBUG, "WPS: Disable NFC token for AP interface %s",
    2012                 :          0 :                    hapd->conf->iface);
    2013                 :          0 :         hostapd_wps_nfc_clear(hapd->wps);
    2014                 :          0 : }
    2015                 :            : 
    2016                 :            : #endif /* CONFIG_WPS_NFC */

Generated by: LCOV version 1.9