LCOV - code coverage report
Current view: top level - src/ap - wpa_auth.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1388338050 Lines: 1047 1521 68.8 %
Date: 2013-12-29 Functions: 82 102 80.4 %
Branches: 510 901 56.6 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * IEEE 802.11 RSN / WPA Authenticator
       3                 :            :  * Copyright (c) 2004-2013, 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/state_machine.h"
      14                 :            : #include "common/ieee802_11_defs.h"
      15                 :            : #include "crypto/aes_wrap.h"
      16                 :            : #include "crypto/crypto.h"
      17                 :            : #include "crypto/sha1.h"
      18                 :            : #include "crypto/sha256.h"
      19                 :            : #include "crypto/random.h"
      20                 :            : #include "eapol_auth/eapol_auth_sm.h"
      21                 :            : #include "ap_config.h"
      22                 :            : #include "ieee802_11.h"
      23                 :            : #include "wpa_auth.h"
      24                 :            : #include "pmksa_cache_auth.h"
      25                 :            : #include "wpa_auth_i.h"
      26                 :            : #include "wpa_auth_ie.h"
      27                 :            : 
      28                 :            : #define STATE_MACHINE_DATA struct wpa_state_machine
      29                 :            : #define STATE_MACHINE_DEBUG_PREFIX "WPA"
      30                 :            : #define STATE_MACHINE_ADDR sm->addr
      31                 :            : 
      32                 :            : 
      33                 :            : static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx);
      34                 :            : static int wpa_sm_step(struct wpa_state_machine *sm);
      35                 :            : static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len);
      36                 :            : static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx);
      37                 :            : static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
      38                 :            :                               struct wpa_group *group);
      39                 :            : static void wpa_request_new_ptk(struct wpa_state_machine *sm);
      40                 :            : static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
      41                 :            :                           struct wpa_group *group);
      42                 :            : static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
      43                 :            :                                        struct wpa_group *group);
      44                 :            : 
      45                 :            : static const u32 dot11RSNAConfigGroupUpdateCount = 4;
      46                 :            : static const u32 dot11RSNAConfigPairwiseUpdateCount = 4;
      47                 :            : static const u32 eapol_key_timeout_first = 100; /* ms */
      48                 :            : static const u32 eapol_key_timeout_subseq = 1000; /* ms */
      49                 :            : static const u32 eapol_key_timeout_first_group = 500; /* ms */
      50                 :            : 
      51                 :            : /* TODO: make these configurable */
      52                 :            : static const int dot11RSNAConfigPMKLifetime = 43200;
      53                 :            : static const int dot11RSNAConfigPMKReauthThreshold = 70;
      54                 :            : static const int dot11RSNAConfigSATimeout = 60;
      55                 :            : 
      56                 :            : 
      57                 :          0 : static inline int wpa_auth_mic_failure_report(
      58                 :            :         struct wpa_authenticator *wpa_auth, const u8 *addr)
      59                 :            : {
      60         [ #  # ]:          0 :         if (wpa_auth->cb.mic_failure_report)
      61                 :          0 :                 return wpa_auth->cb.mic_failure_report(wpa_auth->cb.ctx, addr);
      62                 :          0 :         return 0;
      63                 :            : }
      64                 :            : 
      65                 :            : 
      66                 :       3135 : static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth,
      67                 :            :                                       const u8 *addr, wpa_eapol_variable var,
      68                 :            :                                       int value)
      69                 :            : {
      70         [ +  - ]:       3135 :         if (wpa_auth->cb.set_eapol)
      71                 :       3135 :                 wpa_auth->cb.set_eapol(wpa_auth->cb.ctx, addr, var, value);
      72                 :       3135 : }
      73                 :            : 
      74                 :            : 
      75                 :       1393 : static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth,
      76                 :            :                                      const u8 *addr, wpa_eapol_variable var)
      77                 :            : {
      78         [ -  + ]:       1393 :         if (wpa_auth->cb.get_eapol == NULL)
      79                 :          0 :                 return -1;
      80                 :       1393 :         return wpa_auth->cb.get_eapol(wpa_auth->cb.ctx, addr, var);
      81                 :            : }
      82                 :            : 
      83                 :            : 
      84                 :        558 : static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth,
      85                 :            :                                           const u8 *addr,
      86                 :            :                                           const u8 *p2p_dev_addr,
      87                 :            :                                           const u8 *prev_psk)
      88                 :            : {
      89         [ -  + ]:        558 :         if (wpa_auth->cb.get_psk == NULL)
      90                 :          0 :                 return NULL;
      91                 :        558 :         return wpa_auth->cb.get_psk(wpa_auth->cb.ctx, addr, p2p_dev_addr,
      92                 :            :                                     prev_psk);
      93                 :            : }
      94                 :            : 
      95                 :            : 
      96                 :         82 : static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth,
      97                 :            :                                    const u8 *addr, u8 *msk, size_t *len)
      98                 :            : {
      99         [ -  + ]:         82 :         if (wpa_auth->cb.get_msk == NULL)
     100                 :          0 :                 return -1;
     101                 :         82 :         return wpa_auth->cb.get_msk(wpa_auth->cb.ctx, addr, msk, len);
     102                 :            : }
     103                 :            : 
     104                 :            : 
     105                 :       2065 : static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
     106                 :            :                                    int vlan_id,
     107                 :            :                                    enum wpa_alg alg, const u8 *addr, int idx,
     108                 :            :                                    u8 *key, size_t key_len)
     109                 :            : {
     110         [ -  + ]:       2065 :         if (wpa_auth->cb.set_key == NULL)
     111                 :          0 :                 return -1;
     112                 :       2065 :         return wpa_auth->cb.set_key(wpa_auth->cb.ctx, vlan_id, alg, addr, idx,
     113                 :            :                                     key, key_len);
     114                 :            : }
     115                 :            : 
     116                 :            : 
     117                 :        275 : static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
     118                 :            :                                       const u8 *addr, int idx, u8 *seq)
     119                 :            : {
     120         [ +  + ]:        275 :         if (wpa_auth->cb.get_seqnum == NULL)
     121                 :          6 :                 return -1;
     122                 :        275 :         return wpa_auth->cb.get_seqnum(wpa_auth->cb.ctx, addr, idx, seq);
     123                 :            : }
     124                 :            : 
     125                 :            : 
     126                 :            : static inline int
     127                 :        541 : wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr,
     128                 :            :                     const u8 *data, size_t data_len, int encrypt)
     129                 :            : {
     130         [ -  + ]:        541 :         if (wpa_auth->cb.send_eapol == NULL)
     131                 :          0 :                 return -1;
     132                 :        541 :         return wpa_auth->cb.send_eapol(wpa_auth->cb.ctx, addr, data, data_len,
     133                 :            :                                        encrypt);
     134                 :            : }
     135                 :            : 
     136                 :            : 
     137                 :         28 : int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
     138                 :            :                           int (*cb)(struct wpa_state_machine *sm, void *ctx),
     139                 :            :                           void *cb_ctx)
     140                 :            : {
     141         [ -  + ]:         28 :         if (wpa_auth->cb.for_each_sta == NULL)
     142                 :          0 :                 return 0;
     143                 :         28 :         return wpa_auth->cb.for_each_sta(wpa_auth->cb.ctx, cb, cb_ctx);
     144                 :            : }
     145                 :            : 
     146                 :            : 
     147                 :          1 : int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
     148                 :            :                            int (*cb)(struct wpa_authenticator *a, void *ctx),
     149                 :            :                            void *cb_ctx)
     150                 :            : {
     151         [ -  + ]:          1 :         if (wpa_auth->cb.for_each_auth == NULL)
     152                 :          0 :                 return 0;
     153                 :          1 :         return wpa_auth->cb.for_each_auth(wpa_auth->cb.ctx, cb, cb_ctx);
     154                 :            : }
     155                 :            : 
     156                 :            : 
     157                 :       2122 : void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr,
     158                 :            :                      logger_level level, const char *txt)
     159                 :            : {
     160         [ -  + ]:       2122 :         if (wpa_auth->cb.logger == NULL)
     161                 :       2122 :                 return;
     162                 :       2122 :         wpa_auth->cb.logger(wpa_auth->cb.ctx, addr, level, txt);
     163                 :            : }
     164                 :            : 
     165                 :            : 
     166                 :       1285 : void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr,
     167                 :            :                       logger_level level, const char *fmt, ...)
     168                 :            : {
     169                 :            :         char *format;
     170                 :            :         int maxlen;
     171                 :            :         va_list ap;
     172                 :            : 
     173         [ -  + ]:       1285 :         if (wpa_auth->cb.logger == NULL)
     174                 :          0 :                 return;
     175                 :            : 
     176                 :       1285 :         maxlen = os_strlen(fmt) + 100;
     177                 :       1285 :         format = os_malloc(maxlen);
     178         [ -  + ]:       1285 :         if (!format)
     179                 :          0 :                 return;
     180                 :            : 
     181                 :       1285 :         va_start(ap, fmt);
     182                 :       1285 :         vsnprintf(format, maxlen, fmt, ap);
     183                 :       1285 :         va_end(ap);
     184                 :            : 
     185                 :       1285 :         wpa_auth_logger(wpa_auth, addr, level, format);
     186                 :            : 
     187                 :       1285 :         os_free(format);
     188                 :            : }
     189                 :            : 
     190                 :            : 
     191                 :          6 : static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth,
     192                 :            :                                const u8 *addr)
     193                 :            : {
     194         [ -  + ]:          6 :         if (wpa_auth->cb.disconnect == NULL)
     195                 :          6 :                 return;
     196                 :          6 :         wpa_printf(MSG_DEBUG, "wpa_sta_disconnect STA " MACSTR, MAC2STR(addr));
     197                 :          6 :         wpa_auth->cb.disconnect(wpa_auth->cb.ctx, addr,
     198                 :            :                                 WLAN_REASON_PREV_AUTH_NOT_VALID);
     199                 :            : }
     200                 :            : 
     201                 :            : 
     202                 :       1563 : static int wpa_use_aes_cmac(struct wpa_state_machine *sm)
     203                 :            : {
     204                 :       1563 :         int ret = 0;
     205                 :            : #ifdef CONFIG_IEEE80211R
     206         [ +  + ]:       1563 :         if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
     207                 :         48 :                 ret = 1;
     208                 :            : #endif /* CONFIG_IEEE80211R */
     209                 :            : #ifdef CONFIG_IEEE80211W
     210         [ +  + ]:       1563 :         if (wpa_key_mgmt_sha256(sm->wpa_key_mgmt))
     211                 :         48 :                 ret = 1;
     212                 :            : #endif /* CONFIG_IEEE80211W */
     213                 :       1563 :         return ret;
     214                 :            : }
     215                 :            : 
     216                 :            : 
     217                 :          0 : static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)
     218                 :            : {
     219                 :          0 :         struct wpa_authenticator *wpa_auth = eloop_ctx;
     220                 :            : 
     221         [ #  # ]:          0 :         if (random_get_bytes(wpa_auth->group->GMK, WPA_GMK_LEN)) {
     222                 :          0 :                 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
     223                 :            :                            "initialization.");
     224                 :            :         } else {
     225                 :          0 :                 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd");
     226                 :          0 :                 wpa_hexdump_key(MSG_DEBUG, "GMK",
     227                 :          0 :                                 wpa_auth->group->GMK, WPA_GMK_LEN);
     228                 :            :         }
     229                 :            : 
     230         [ #  # ]:          0 :         if (wpa_auth->conf.wpa_gmk_rekey) {
     231                 :          0 :                 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
     232                 :            :                                        wpa_rekey_gmk, wpa_auth, NULL);
     233                 :            :         }
     234                 :          0 : }
     235                 :            : 
     236                 :            : 
     237                 :          0 : static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
     238                 :            : {
     239                 :          0 :         struct wpa_authenticator *wpa_auth = eloop_ctx;
     240                 :            :         struct wpa_group *group;
     241                 :            : 
     242                 :          0 :         wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK");
     243         [ #  # ]:          0 :         for (group = wpa_auth->group; group; group = group->next) {
     244                 :          0 :                 group->GTKReKey = TRUE;
     245                 :            :                 do {
     246                 :          0 :                         group->changed = FALSE;
     247                 :          0 :                         wpa_group_sm_step(wpa_auth, group);
     248         [ #  # ]:          0 :                 } while (group->changed);
     249                 :            :         }
     250                 :            : 
     251         [ #  # ]:          0 :         if (wpa_auth->conf.wpa_group_rekey) {
     252                 :          0 :                 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey,
     253                 :            :                                        0, wpa_rekey_gtk, wpa_auth, NULL);
     254                 :            :         }
     255                 :          0 : }
     256                 :            : 
     257                 :            : 
     258                 :          0 : static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
     259                 :            : {
     260                 :          0 :         struct wpa_authenticator *wpa_auth = eloop_ctx;
     261                 :          0 :         struct wpa_state_machine *sm = timeout_ctx;
     262                 :            : 
     263                 :          0 :         wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK");
     264                 :          0 :         wpa_request_new_ptk(sm);
     265                 :          0 :         wpa_sm_step(sm);
     266                 :          0 : }
     267                 :            : 
     268                 :            : 
     269                 :         26 : static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx)
     270                 :            : {
     271         [ -  + ]:         26 :         if (sm->pmksa == ctx)
     272                 :          0 :                 sm->pmksa = NULL;
     273                 :         26 :         return 0;
     274                 :            : }
     275                 :            : 
     276                 :            : 
     277                 :         26 : static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
     278                 :            :                                    void *ctx)
     279                 :            : {
     280                 :         26 :         struct wpa_authenticator *wpa_auth = ctx;
     281                 :         26 :         wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry);
     282                 :         26 : }
     283                 :            : 
     284                 :            : 
     285                 :        394 : static int wpa_group_init_gmk_and_counter(struct wpa_authenticator *wpa_auth,
     286                 :            :                                           struct wpa_group *group)
     287                 :            : {
     288                 :            :         u8 buf[ETH_ALEN + 8 + sizeof(unsigned long)];
     289                 :            :         u8 rkey[32];
     290                 :            :         unsigned long ptr;
     291                 :            : 
     292         [ -  + ]:        394 :         if (random_get_bytes(group->GMK, WPA_GMK_LEN) < 0)
     293                 :          0 :                 return -1;
     294                 :        394 :         wpa_hexdump_key(MSG_DEBUG, "GMK", group->GMK, WPA_GMK_LEN);
     295                 :            : 
     296                 :            :         /*
     297                 :            :          * Counter = PRF-256(Random number, "Init Counter",
     298                 :            :          *                   Local MAC Address || Time)
     299                 :            :          */
     300                 :        394 :         os_memcpy(buf, wpa_auth->addr, ETH_ALEN);
     301                 :        394 :         wpa_get_ntp_timestamp(buf + ETH_ALEN);
     302                 :        394 :         ptr = (unsigned long) group;
     303                 :        394 :         os_memcpy(buf + ETH_ALEN + 8, &ptr, sizeof(ptr));
     304         [ -  + ]:        394 :         if (random_get_bytes(rkey, sizeof(rkey)) < 0)
     305                 :          0 :                 return -1;
     306                 :            : 
     307         [ -  + ]:        394 :         if (sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
     308                 :        394 :                      group->Counter, WPA_NONCE_LEN) < 0)
     309                 :          0 :                 return -1;
     310                 :        394 :         wpa_hexdump_key(MSG_DEBUG, "Key Counter",
     311                 :        394 :                         group->Counter, WPA_NONCE_LEN);
     312                 :            : 
     313                 :        394 :         return 0;
     314                 :            : }
     315                 :            : 
     316                 :            : 
     317                 :        214 : static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth,
     318                 :            :                                          int vlan_id, int delay_init)
     319                 :            : {
     320                 :            :         struct wpa_group *group;
     321                 :            : 
     322                 :        214 :         group = os_zalloc(sizeof(struct wpa_group));
     323         [ -  + ]:        214 :         if (group == NULL)
     324                 :          0 :                 return NULL;
     325                 :            : 
     326                 :        214 :         group->GTKAuthenticator = TRUE;
     327                 :        214 :         group->vlan_id = vlan_id;
     328                 :        214 :         group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
     329                 :            : 
     330                 :            :         if (random_pool_ready() != 1) {
     331                 :            :                 wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
     332                 :            :                            "for secure operations - update keys later when "
     333                 :            :                            "the first station connects");
     334                 :            :         }
     335                 :            : 
     336                 :            :         /*
     337                 :            :          * Set initial GMK/Counter value here. The actual values that will be
     338                 :            :          * used in negotiations will be set once the first station tries to
     339                 :            :          * connect. This allows more time for collecting additional randomness
     340                 :            :          * on embedded devices.
     341                 :            :          */
     342         [ -  + ]:        214 :         if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0) {
     343                 :          0 :                 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
     344                 :            :                            "initialization.");
     345                 :          0 :                 os_free(group);
     346                 :          0 :                 return NULL;
     347                 :            :         }
     348                 :            : 
     349                 :        214 :         group->GInit = TRUE;
     350         [ +  - ]:        214 :         if (delay_init) {
     351                 :        214 :                 wpa_printf(MSG_DEBUG, "WPA: Delay group state machine start "
     352                 :            :                            "until Beacon frames have been configured");
     353                 :            :                 /* Initialization is completed in wpa_init_keys(). */
     354                 :            :         } else {
     355                 :          0 :                 wpa_group_sm_step(wpa_auth, group);
     356                 :          0 :                 group->GInit = FALSE;
     357                 :          0 :                 wpa_group_sm_step(wpa_auth, group);
     358                 :            :         }
     359                 :            : 
     360                 :        214 :         return group;
     361                 :            : }
     362                 :            : 
     363                 :            : 
     364                 :            : /**
     365                 :            :  * wpa_init - Initialize WPA authenticator
     366                 :            :  * @addr: Authenticator address
     367                 :            :  * @conf: Configuration for WPA authenticator
     368                 :            :  * @cb: Callback functions for WPA authenticator
     369                 :            :  * Returns: Pointer to WPA authenticator data or %NULL on failure
     370                 :            :  */
     371                 :        214 : struct wpa_authenticator * wpa_init(const u8 *addr,
     372                 :            :                                     struct wpa_auth_config *conf,
     373                 :            :                                     struct wpa_auth_callbacks *cb)
     374                 :            : {
     375                 :            :         struct wpa_authenticator *wpa_auth;
     376                 :            : 
     377                 :        214 :         wpa_auth = os_zalloc(sizeof(struct wpa_authenticator));
     378         [ -  + ]:        214 :         if (wpa_auth == NULL)
     379                 :          0 :                 return NULL;
     380                 :        214 :         os_memcpy(wpa_auth->addr, addr, ETH_ALEN);
     381                 :        214 :         os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
     382                 :        214 :         os_memcpy(&wpa_auth->cb, cb, sizeof(*cb));
     383                 :            : 
     384         [ -  + ]:        214 :         if (wpa_auth_gen_wpa_ie(wpa_auth)) {
     385                 :          0 :                 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
     386                 :          0 :                 os_free(wpa_auth);
     387                 :          0 :                 return NULL;
     388                 :            :         }
     389                 :            : 
     390                 :        214 :         wpa_auth->group = wpa_group_init(wpa_auth, 0, 1);
     391         [ -  + ]:        214 :         if (wpa_auth->group == NULL) {
     392                 :          0 :                 os_free(wpa_auth->wpa_ie);
     393                 :          0 :                 os_free(wpa_auth);
     394                 :          0 :                 return NULL;
     395                 :            :         }
     396                 :            : 
     397                 :        214 :         wpa_auth->pmksa = pmksa_cache_auth_init(wpa_auth_pmksa_free_cb,
     398                 :            :                                                 wpa_auth);
     399         [ -  + ]:        214 :         if (wpa_auth->pmksa == NULL) {
     400                 :          0 :                 wpa_printf(MSG_ERROR, "PMKSA cache initialization failed.");
     401                 :          0 :                 os_free(wpa_auth->wpa_ie);
     402                 :          0 :                 os_free(wpa_auth);
     403                 :          0 :                 return NULL;
     404                 :            :         }
     405                 :            : 
     406                 :            : #ifdef CONFIG_IEEE80211R
     407                 :        214 :         wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init();
     408         [ -  + ]:        214 :         if (wpa_auth->ft_pmk_cache == NULL) {
     409                 :          0 :                 wpa_printf(MSG_ERROR, "FT PMK cache initialization failed.");
     410                 :          0 :                 os_free(wpa_auth->wpa_ie);
     411                 :          0 :                 pmksa_cache_auth_deinit(wpa_auth->pmksa);
     412                 :          0 :                 os_free(wpa_auth);
     413                 :          0 :                 return NULL;
     414                 :            :         }
     415                 :            : #endif /* CONFIG_IEEE80211R */
     416                 :            : 
     417         [ +  + ]:        214 :         if (wpa_auth->conf.wpa_gmk_rekey) {
     418                 :        211 :                 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
     419                 :            :                                        wpa_rekey_gmk, wpa_auth, NULL);
     420                 :            :         }
     421                 :            : 
     422         [ +  - ]:        214 :         if (wpa_auth->conf.wpa_group_rekey) {
     423                 :        214 :                 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0,
     424                 :            :                                        wpa_rekey_gtk, wpa_auth, NULL);
     425                 :            :         }
     426                 :            : 
     427                 :        214 :         return wpa_auth;
     428                 :            : }
     429                 :            : 
     430                 :            : 
     431                 :        214 : int wpa_init_keys(struct wpa_authenticator *wpa_auth)
     432                 :            : {
     433                 :        214 :         struct wpa_group *group = wpa_auth->group;
     434                 :            : 
     435                 :        214 :         wpa_printf(MSG_DEBUG, "WPA: Start group state machine to set initial "
     436                 :            :                    "keys");
     437                 :        214 :         wpa_group_sm_step(wpa_auth, group);
     438                 :        214 :         group->GInit = FALSE;
     439                 :        214 :         wpa_group_sm_step(wpa_auth, group);
     440         [ -  + ]:        214 :         if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
     441                 :          0 :                 return -1;
     442                 :        214 :         return 0;
     443                 :            : }
     444                 :            : 
     445                 :            : 
     446                 :            : /**
     447                 :            :  * wpa_deinit - Deinitialize WPA authenticator
     448                 :            :  * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
     449                 :            :  */
     450                 :        214 : void wpa_deinit(struct wpa_authenticator *wpa_auth)
     451                 :            : {
     452                 :            :         struct wpa_group *group, *prev;
     453                 :            : 
     454                 :        214 :         eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL);
     455                 :        214 :         eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
     456                 :            : 
     457                 :            : #ifdef CONFIG_PEERKEY
     458         [ -  + ]:        214 :         while (wpa_auth->stsl_negotiations)
     459                 :          0 :                 wpa_stsl_remove(wpa_auth, wpa_auth->stsl_negotiations);
     460                 :            : #endif /* CONFIG_PEERKEY */
     461                 :            : 
     462                 :        214 :         pmksa_cache_auth_deinit(wpa_auth->pmksa);
     463                 :            : 
     464                 :            : #ifdef CONFIG_IEEE80211R
     465                 :        214 :         wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache);
     466                 :        214 :         wpa_auth->ft_pmk_cache = NULL;
     467                 :            : #endif /* CONFIG_IEEE80211R */
     468                 :            : 
     469                 :        214 :         os_free(wpa_auth->wpa_ie);
     470                 :            : 
     471                 :        214 :         group = wpa_auth->group;
     472         [ +  + ]:        428 :         while (group) {
     473                 :        214 :                 prev = group;
     474                 :        214 :                 group = group->next;
     475                 :        214 :                 os_free(prev);
     476                 :            :         }
     477                 :            : 
     478                 :        214 :         os_free(wpa_auth);
     479                 :        214 : }
     480                 :            : 
     481                 :            : 
     482                 :            : /**
     483                 :            :  * wpa_reconfig - Update WPA authenticator configuration
     484                 :            :  * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
     485                 :            :  * @conf: Configuration for WPA authenticator
     486                 :            :  */
     487                 :          2 : int wpa_reconfig(struct wpa_authenticator *wpa_auth,
     488                 :            :                  struct wpa_auth_config *conf)
     489                 :            : {
     490                 :            :         struct wpa_group *group;
     491         [ -  + ]:          2 :         if (wpa_auth == NULL)
     492                 :          0 :                 return 0;
     493                 :            : 
     494                 :          2 :         os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
     495         [ -  + ]:          2 :         if (wpa_auth_gen_wpa_ie(wpa_auth)) {
     496                 :          0 :                 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
     497                 :          0 :                 return -1;
     498                 :            :         }
     499                 :            : 
     500                 :            :         /*
     501                 :            :          * Reinitialize GTK to make sure it is suitable for the new
     502                 :            :          * configuration.
     503                 :            :          */
     504                 :          2 :         group = wpa_auth->group;
     505                 :          2 :         group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
     506                 :          2 :         group->GInit = TRUE;
     507                 :          2 :         wpa_group_sm_step(wpa_auth, group);
     508                 :          2 :         group->GInit = FALSE;
     509                 :          2 :         wpa_group_sm_step(wpa_auth, group);
     510                 :            : 
     511                 :          2 :         return 0;
     512                 :            : }
     513                 :            : 
     514                 :            : 
     515                 :            : struct wpa_state_machine *
     516                 :        240 : wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr,
     517                 :            :                   const u8 *p2p_dev_addr)
     518                 :            : {
     519                 :            :         struct wpa_state_machine *sm;
     520                 :            : 
     521         [ -  + ]:        240 :         if (wpa_auth->group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
     522                 :          0 :                 return NULL;
     523                 :            : 
     524                 :        240 :         sm = os_zalloc(sizeof(struct wpa_state_machine));
     525         [ -  + ]:        240 :         if (sm == NULL)
     526                 :          0 :                 return NULL;
     527                 :        240 :         os_memcpy(sm->addr, addr, ETH_ALEN);
     528         [ +  + ]:        240 :         if (p2p_dev_addr)
     529                 :         59 :                 os_memcpy(sm->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
     530                 :            : 
     531                 :        240 :         sm->wpa_auth = wpa_auth;
     532                 :        240 :         sm->group = wpa_auth->group;
     533                 :            : 
     534                 :        240 :         return sm;
     535                 :            : }
     536                 :            : 
     537                 :            : 
     538                 :        414 : int wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
     539                 :            :                             struct wpa_state_machine *sm)
     540                 :            : {
     541 [ +  + ][ +  - ]:        414 :         if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
                 [ +  + ]
     542                 :        157 :                 return -1;
     543                 :            : 
     544                 :            : #ifdef CONFIG_IEEE80211R
     545         [ +  + ]:        257 :         if (sm->ft_completed) {
     546                 :         16 :                 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
     547                 :            :                                 "FT authentication already completed - do not "
     548                 :            :                                 "start 4-way handshake");
     549                 :         16 :                 return 0;
     550                 :            :         }
     551                 :            : #endif /* CONFIG_IEEE80211R */
     552                 :            : 
     553         [ +  + ]:        241 :         if (sm->started) {
     554                 :          9 :                 os_memset(&sm->key_replay, 0, sizeof(sm->key_replay));
     555                 :          9 :                 sm->ReAuthenticationRequest = TRUE;
     556                 :          9 :                 return wpa_sm_step(sm);
     557                 :            :         }
     558                 :            : 
     559                 :        232 :         wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
     560                 :            :                         "start authentication");
     561                 :        232 :         sm->started = 1;
     562                 :            : 
     563                 :        232 :         sm->Init = TRUE;
     564         [ -  + ]:        232 :         if (wpa_sm_step(sm) == 1)
     565                 :          0 :                 return 1; /* should not really happen */
     566                 :        232 :         sm->Init = FALSE;
     567                 :        232 :         sm->AuthenticationRequest = TRUE;
     568                 :        414 :         return wpa_sm_step(sm);
     569                 :            : }
     570                 :            : 
     571                 :            : 
     572                 :        158 : void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm)
     573                 :            : {
     574                 :            :         /* WPA/RSN was not used - clear WPA state. This is needed if the STA
     575                 :            :          * reassociates back to the same AP while the previous entry for the
     576                 :            :          * STA has not yet been removed. */
     577         [ +  + ]:        158 :         if (sm == NULL)
     578                 :        158 :                 return;
     579                 :            : 
     580                 :          1 :         sm->wpa_key_mgmt = 0;
     581                 :            : }
     582                 :            : 
     583                 :            : 
     584                 :        240 : static void wpa_free_sta_sm(struct wpa_state_machine *sm)
     585                 :            : {
     586         [ -  + ]:        240 :         if (sm->GUpdateStationKeys) {
     587                 :          0 :                 sm->group->GKeyDoneStations--;
     588                 :          0 :                 sm->GUpdateStationKeys = FALSE;
     589                 :            :         }
     590                 :            : #ifdef CONFIG_IEEE80211R
     591                 :        240 :         os_free(sm->assoc_resp_ftie);
     592                 :            : #endif /* CONFIG_IEEE80211R */
     593                 :        240 :         os_free(sm->last_rx_eapol_key);
     594                 :        240 :         os_free(sm->wpa_ie);
     595                 :        240 :         os_free(sm);
     596                 :        240 : }
     597                 :            : 
     598                 :            : 
     599                 :        398 : void wpa_auth_sta_deinit(struct wpa_state_machine *sm)
     600                 :            : {
     601         [ +  + ]:        398 :         if (sm == NULL)
     602                 :        398 :                 return;
     603                 :            : 
     604 [ -  + ][ #  # ]:        240 :         if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) {
     605                 :          0 :                 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
     606                 :            :                                 "strict rekeying - force GTK rekey since STA "
     607                 :            :                                 "is leaving");
     608                 :          0 :                 eloop_cancel_timeout(wpa_rekey_gtk, sm->wpa_auth, NULL);
     609                 :          0 :                 eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth,
     610                 :            :                                        NULL);
     611                 :            :         }
     612                 :            : 
     613                 :        240 :         eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
     614                 :        240 :         sm->pending_1_of_4_timeout = 0;
     615                 :        240 :         eloop_cancel_timeout(wpa_sm_call_step, sm, NULL);
     616                 :        240 :         eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
     617         [ -  + ]:        240 :         if (sm->in_step_loop) {
     618                 :            :                 /* Must not free state machine while wpa_sm_step() is running.
     619                 :            :                  * Freeing will be completed in the end of wpa_sm_step(). */
     620                 :          0 :                 wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state "
     621                 :          0 :                            "machine deinit for " MACSTR, MAC2STR(sm->addr));
     622                 :          0 :                 sm->pending_deinit = 1;
     623                 :            :         } else
     624                 :        240 :                 wpa_free_sta_sm(sm);
     625                 :            : }
     626                 :            : 
     627                 :            : 
     628                 :          0 : static void wpa_request_new_ptk(struct wpa_state_machine *sm)
     629                 :            : {
     630         [ #  # ]:          0 :         if (sm == NULL)
     631                 :          0 :                 return;
     632                 :            : 
     633                 :          0 :         sm->PTKRequest = TRUE;
     634                 :          0 :         sm->PTK_valid = 0;
     635                 :            : }
     636                 :            : 
     637                 :            : 
     638                 :        539 : static int wpa_replay_counter_valid(struct wpa_key_replay_counter *ctr,
     639                 :            :                                     const u8 *replay_counter)
     640                 :            : {
     641                 :            :         int i;
     642         [ +  - ]:        539 :         for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
     643         [ -  + ]:        539 :                 if (!ctr[i].valid)
     644                 :          0 :                         break;
     645         [ +  - ]:        539 :                 if (os_memcmp(replay_counter, ctr[i].counter,
     646                 :            :                               WPA_REPLAY_COUNTER_LEN) == 0)
     647                 :        539 :                         return 1;
     648                 :            :         }
     649                 :        539 :         return 0;
     650                 :            : }
     651                 :            : 
     652                 :            : 
     653                 :       1078 : static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter *ctr,
     654                 :            :                                             const u8 *replay_counter)
     655                 :            : {
     656                 :            :         int i;
     657         [ +  + ]:       5390 :         for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
     658 [ +  + ][ +  - ]:       4312 :                 if (ctr[i].valid &&
     659         [ +  - ]:        539 :                     (replay_counter == NULL ||
     660                 :        539 :                      os_memcmp(replay_counter, ctr[i].counter,
     661                 :            :                                WPA_REPLAY_COUNTER_LEN) == 0))
     662                 :        539 :                         ctr[i].valid = FALSE;
     663                 :            :         }
     664                 :       1078 : }
     665                 :            : 
     666                 :            : 
     667                 :            : #ifdef CONFIG_IEEE80211R
     668                 :          8 : static int ft_check_msg_2_of_4(struct wpa_authenticator *wpa_auth,
     669                 :            :                                struct wpa_state_machine *sm,
     670                 :            :                                struct wpa_eapol_ie_parse *kde)
     671                 :            : {
     672                 :            :         struct wpa_ie_data ie;
     673                 :            :         struct rsn_mdie *mdie;
     674                 :            : 
     675 [ +  - ][ +  - ]:          8 :         if (wpa_parse_wpa_ie_rsn(kde->rsn_ie, kde->rsn_ie_len, &ie) < 0 ||
     676         [ -  + ]:          8 :             ie.num_pmkid != 1 || ie.pmkid == NULL) {
     677                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: No PMKR1Name in "
     678                 :            :                            "FT 4-way handshake message 2/4");
     679                 :          0 :                 return -1;
     680                 :            :         }
     681                 :            : 
     682                 :          8 :         os_memcpy(sm->sup_pmk_r1_name, ie.pmkid, PMKID_LEN);
     683                 :          8 :         wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Supplicant",
     684                 :          8 :                     sm->sup_pmk_r1_name, PMKID_LEN);
     685                 :            : 
     686 [ -  + ][ +  - ]:          8 :         if (!kde->mdie || !kde->ftie) {
     687         [ #  # ]:          0 :                 wpa_printf(MSG_DEBUG, "FT: No %s in FT 4-way handshake "
     688                 :          0 :                            "message 2/4", kde->mdie ? "FTIE" : "MDIE");
     689                 :          0 :                 return -1;
     690                 :            :         }
     691                 :            : 
     692                 :          8 :         mdie = (struct rsn_mdie *) (kde->mdie + 2);
     693 [ +  - ][ -  + ]:          8 :         if (kde->mdie[1] < sizeof(struct rsn_mdie) ||
     694                 :          8 :             os_memcmp(wpa_auth->conf.mobility_domain, mdie->mobility_domain,
     695                 :            :                       MOBILITY_DOMAIN_ID_LEN) != 0) {
     696                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: MDIE mismatch");
     697                 :          0 :                 return -1;
     698                 :            :         }
     699                 :            : 
     700 [ +  - ][ +  - ]:          8 :         if (sm->assoc_resp_ftie &&
     701         [ -  + ]:          8 :             (kde->ftie[1] != sm->assoc_resp_ftie[1] ||
     702                 :          8 :              os_memcmp(kde->ftie, sm->assoc_resp_ftie,
     703                 :            :                        2 + sm->assoc_resp_ftie[1]) != 0)) {
     704                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: FTIE mismatch");
     705                 :          0 :                 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 2/4",
     706                 :          0 :                             kde->ftie, kde->ftie_len);
     707                 :          0 :                 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)AssocResp",
     708                 :          0 :                             sm->assoc_resp_ftie, 2 + sm->assoc_resp_ftie[1]);
     709                 :          0 :                 return -1;
     710                 :            :         }
     711                 :            : 
     712                 :          8 :         return 0;
     713                 :            : }
     714                 :            : #endif /* CONFIG_IEEE80211R */
     715                 :            : 
     716                 :            : 
     717                 :          0 : static int wpa_receive_error_report(struct wpa_authenticator *wpa_auth,
     718                 :            :                                     struct wpa_state_machine *sm, int group)
     719                 :            : {
     720                 :            :         /* Supplicant reported a Michael MIC error */
     721                 :          0 :         wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
     722                 :            :                          "received EAPOL-Key Error Request "
     723                 :            :                          "(STA detected Michael MIC failure (group=%d))",
     724                 :            :                          group);
     725                 :            : 
     726 [ #  # ][ #  # ]:          0 :         if (group && wpa_auth->conf.wpa_group != WPA_CIPHER_TKIP) {
     727                 :          0 :                 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
     728                 :            :                                 "ignore Michael MIC failure report since "
     729                 :            :                                 "group cipher is not TKIP");
     730 [ #  # ][ #  # ]:          0 :         } else if (!group && sm->pairwise != WPA_CIPHER_TKIP) {
     731                 :          0 :                 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
     732                 :            :                                 "ignore Michael MIC failure report since "
     733                 :            :                                 "pairwise cipher is not TKIP");
     734                 :            :         } else {
     735         [ #  # ]:          0 :                 if (wpa_auth_mic_failure_report(wpa_auth, sm->addr) > 0)
     736                 :          0 :                         return 1; /* STA entry was removed */
     737                 :          0 :                 sm->dot11RSNAStatsTKIPRemoteMICFailures++;
     738                 :          0 :                 wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
     739                 :            :         }
     740                 :            : 
     741                 :            :         /*
     742                 :            :          * Error report is not a request for a new key handshake, but since
     743                 :            :          * Authenticator may do it, let's change the keys now anyway.
     744                 :            :          */
     745                 :          0 :         wpa_request_new_ptk(sm);
     746                 :          0 :         return 0;
     747                 :            : }
     748                 :            : 
     749                 :            : 
     750                 :        540 : void wpa_receive(struct wpa_authenticator *wpa_auth,
     751                 :            :                  struct wpa_state_machine *sm,
     752                 :            :                  u8 *data, size_t data_len)
     753                 :            : {
     754                 :            :         struct ieee802_1x_hdr *hdr;
     755                 :            :         struct wpa_eapol_key *key;
     756                 :            :         u16 key_info, key_data_length;
     757                 :            :         enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST,
     758                 :            :                SMK_M1, SMK_M3, SMK_ERROR } msg;
     759                 :            :         char *msgtxt;
     760                 :            :         struct wpa_eapol_ie_parse kde;
     761                 :            :         int ft;
     762                 :            :         const u8 *eapol_key_ie;
     763                 :            :         size_t eapol_key_ie_len;
     764                 :            : 
     765 [ +  - ][ +  - ]:        540 :         if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
                 [ -  + ]
     766                 :          0 :                 return;
     767                 :            : 
     768         [ -  + ]:        540 :         if (data_len < sizeof(*hdr) + sizeof(*key))
     769                 :          0 :                 return;
     770                 :            : 
     771                 :        540 :         hdr = (struct ieee802_1x_hdr *) data;
     772                 :        540 :         key = (struct wpa_eapol_key *) (hdr + 1);
     773                 :        540 :         key_info = WPA_GET_BE16(key->key_info);
     774                 :        540 :         key_data_length = WPA_GET_BE16(key->key_data_length);
     775                 :        540 :         wpa_printf(MSG_DEBUG, "WPA: Received EAPOL-Key from " MACSTR
     776                 :            :                    " key_info=0x%x type=%u key_data_length=%u",
     777                 :       3780 :                    MAC2STR(sm->addr), key_info, key->type, key_data_length);
     778         [ -  + ]:        540 :         if (key_data_length > data_len - sizeof(*hdr) - sizeof(*key)) {
     779                 :          0 :                 wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
     780                 :            :                            "key_data overflow (%d > %lu)",
     781                 :            :                            key_data_length,
     782                 :            :                            (unsigned long) (data_len - sizeof(*hdr) -
     783                 :            :                                             sizeof(*key)));
     784                 :          0 :                 return;
     785                 :            :         }
     786                 :            : 
     787         [ +  + ]:        540 :         if (sm->wpa == WPA_VERSION_WPA2) {
     788         [ -  + ]:        519 :                 if (key->type == EAPOL_KEY_TYPE_WPA) {
     789                 :            :                         /*
     790                 :            :                          * Some deployed station implementations seem to send
     791                 :            :                          * msg 4/4 with incorrect type value in WPA2 mode.
     792                 :            :                          */
     793                 :          0 :                         wpa_printf(MSG_DEBUG, "Workaround: Allow EAPOL-Key "
     794                 :            :                                    "with unexpected WPA type in RSN mode");
     795         [ -  + ]:        519 :                 } else if (key->type != EAPOL_KEY_TYPE_RSN) {
     796                 :          0 :                         wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
     797                 :            :                                    "unexpected type %d in RSN mode",
     798                 :          0 :                                    key->type);
     799                 :          0 :                         return;
     800                 :            :                 }
     801                 :            :         } else {
     802         [ -  + ]:         21 :                 if (key->type != EAPOL_KEY_TYPE_WPA) {
     803                 :          0 :                         wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
     804                 :            :                                    "unexpected type %d in WPA mode",
     805                 :          0 :                                    key->type);
     806                 :          0 :                         return;
     807                 :            :                 }
     808                 :            :         }
     809                 :            : 
     810                 :        540 :         wpa_hexdump(MSG_DEBUG, "WPA: Received Key Nonce", key->key_nonce,
     811                 :            :                     WPA_NONCE_LEN);
     812                 :        540 :         wpa_hexdump(MSG_DEBUG, "WPA: Received Replay Counter",
     813                 :        540 :                     key->replay_counter, WPA_REPLAY_COUNTER_LEN);
     814                 :            : 
     815                 :            :         /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
     816                 :            :          * are set */
     817                 :            : 
     818         [ +  + ]:        540 :         if ((key_info & (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) ==
     819                 :            :             (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) {
     820         [ -  + ]:          1 :                 if (key_info & WPA_KEY_INFO_ERROR) {
     821                 :          0 :                         msg = SMK_ERROR;
     822                 :          0 :                         msgtxt = "SMK Error";
     823                 :            :                 } else {
     824                 :          1 :                         msg = SMK_M1;
     825                 :          1 :                         msgtxt = "SMK M1";
     826                 :            :                 }
     827         [ +  + ]:        539 :         } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
     828                 :          1 :                 msg = SMK_M3;
     829                 :          1 :                 msgtxt = "SMK M3";
     830         [ -  + ]:        538 :         } else if (key_info & WPA_KEY_INFO_REQUEST) {
     831                 :          0 :                 msg = REQUEST;
     832                 :          0 :                 msgtxt = "Request";
     833         [ +  + ]:        538 :         } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
     834                 :          8 :                 msg = GROUP_2;
     835                 :          8 :                 msgtxt = "2/2 Group";
     836         [ +  + ]:        530 :         } else if (key_data_length == 0) {
     837                 :        253 :                 msg = PAIRWISE_4;
     838                 :        253 :                 msgtxt = "4/4 Pairwise";
     839                 :            :         } else {
     840                 :        277 :                 msg = PAIRWISE_2;
     841                 :        277 :                 msgtxt = "2/4 Pairwise";
     842                 :            :         }
     843                 :            : 
     844                 :            :         /* TODO: key_info type validation for PeerKey */
     845 [ +  - ][ +  + ]:        540 :         if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
         [ +  + ][ +  + ]
     846                 :            :             msg == GROUP_2) {
     847                 :        538 :                 u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
     848 [ +  + ][ +  + ]:        538 :                 if (sm->pairwise == WPA_CIPHER_CCMP ||
     849                 :         29 :                     sm->pairwise == WPA_CIPHER_GCMP) {
     850 [ +  + ][ -  + ]:        511 :                         if (wpa_use_aes_cmac(sm) &&
     851                 :            :                             ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
     852                 :          0 :                                 wpa_auth_logger(wpa_auth, sm->addr,
     853                 :            :                                                 LOGGER_WARNING,
     854                 :            :                                                 "advertised support for "
     855                 :            :                                                 "AES-128-CMAC, but did not "
     856                 :            :                                                 "use it");
     857                 :          0 :                                 return;
     858                 :            :                         }
     859                 :            : 
     860 [ +  + ][ -  + ]:        511 :                         if (!wpa_use_aes_cmac(sm) &&
     861                 :            :                             ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
     862                 :          0 :                                 wpa_auth_logger(wpa_auth, sm->addr,
     863                 :            :                                                 LOGGER_WARNING,
     864                 :            :                                                 "did not use HMAC-SHA1-AES "
     865                 :            :                                                 "with CCMP/GCMP");
     866                 :          0 :                                 return;
     867                 :            :                         }
     868                 :            :                 }
     869                 :            :         }
     870                 :            : 
     871         [ +  + ]:        540 :         if (key_info & WPA_KEY_INFO_REQUEST) {
     872 [ -  + ][ #  # ]:          1 :                 if (sm->req_replay_counter_used &&
     873                 :          0 :                     os_memcmp(key->replay_counter, sm->req_replay_counter,
     874                 :            :                               WPA_REPLAY_COUNTER_LEN) <= 0) {
     875                 :          0 :                         wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
     876                 :            :                                         "received EAPOL-Key request with "
     877                 :            :                                         "replayed counter");
     878                 :          0 :                         return;
     879                 :            :                 }
     880                 :            :         }
     881                 :            : 
     882   [ +  +  -  + ]:       1079 :         if (!(key_info & WPA_KEY_INFO_REQUEST) &&
     883                 :        539 :             !wpa_replay_counter_valid(sm->key_replay, key->replay_counter)) {
     884                 :            :                 int i;
     885                 :            : 
     886   [ #  #  #  # ]:          0 :                 if (msg == PAIRWISE_2 &&
     887                 :          0 :                     wpa_replay_counter_valid(sm->prev_key_replay,
     888         [ #  # ]:          0 :                                              key->replay_counter) &&
     889         [ #  # ]:          0 :                     sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
     890                 :          0 :                     os_memcmp(sm->SNonce, key->key_nonce, WPA_NONCE_LEN) != 0)
     891                 :            :                 {
     892                 :            :                         /*
     893                 :            :                          * Some supplicant implementations (e.g., Windows XP
     894                 :            :                          * WZC) update SNonce for each EAPOL-Key 2/4. This
     895                 :            :                          * breaks the workaround on accepting any of the
     896                 :            :                          * pending requests, so allow the SNonce to be updated
     897                 :            :                          * even if we have already sent out EAPOL-Key 3/4.
     898                 :            :                          */
     899                 :          0 :                         wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
     900                 :            :                                          "Process SNonce update from STA "
     901                 :            :                                          "based on retransmitted EAPOL-Key "
     902                 :            :                                          "1/4");
     903                 :          0 :                         sm->update_snonce = 1;
     904                 :          0 :                         wpa_replay_counter_mark_invalid(sm->prev_key_replay,
     905                 :          0 :                                                         key->replay_counter);
     906                 :          0 :                         goto continue_processing;
     907                 :            :                 }
     908                 :            : 
     909   [ #  #  #  # ]:          0 :                 if (msg == PAIRWISE_2 &&
     910                 :          0 :                     wpa_replay_counter_valid(sm->prev_key_replay,
     911         [ #  # ]:          0 :                                              key->replay_counter) &&
     912                 :          0 :                     sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING) {
     913                 :          0 :                         wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
     914                 :            :                                          "ignore retransmitted EAPOL-Key %s - "
     915                 :            :                                          "SNonce did not change", msgtxt);
     916                 :            :                 } else {
     917                 :          0 :                         wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
     918                 :            :                                          "received EAPOL-Key %s with "
     919                 :            :                                          "unexpected replay counter", msgtxt);
     920                 :            :                 }
     921         [ #  # ]:          0 :                 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
     922         [ #  # ]:          0 :                         if (!sm->key_replay[i].valid)
     923                 :          0 :                                 break;
     924                 :          0 :                         wpa_hexdump(MSG_DEBUG, "pending replay counter",
     925                 :          0 :                                     sm->key_replay[i].counter,
     926                 :            :                                     WPA_REPLAY_COUNTER_LEN);
     927                 :            :                 }
     928                 :          0 :                 wpa_hexdump(MSG_DEBUG, "received replay counter",
     929                 :          0 :                             key->replay_counter, WPA_REPLAY_COUNTER_LEN);
     930                 :          0 :                 return;
     931                 :            :         }
     932                 :            : 
     933                 :            : continue_processing:
     934   [ +  +  +  +  :        540 :         switch (msg) {
                   -  - ]
     935                 :            :         case PAIRWISE_2:
     936 [ -  + ][ #  # ]:        277 :                 if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
     937         [ #  # ]:          0 :                     sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING &&
     938         [ #  # ]:          0 :                     (!sm->update_snonce ||
     939                 :          0 :                      sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING)) {
     940                 :          0 :                         wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
     941                 :            :                                          "received EAPOL-Key msg 2/4 in "
     942                 :            :                                          "invalid state (%d) - dropped",
     943                 :          0 :                                          sm->wpa_ptk_state);
     944                 :          0 :                         return;
     945                 :            :                 }
     946                 :            :                 random_add_randomness(key->key_nonce, WPA_NONCE_LEN);
     947         [ -  + ]:        277 :                 if (sm->group->reject_4way_hs_for_entropy) {
     948                 :            :                         /*
     949                 :            :                          * The system did not have enough entropy to generate
     950                 :            :                          * strong random numbers. Reject the first 4-way
     951                 :            :                          * handshake(s) and collect some entropy based on the
     952                 :            :                          * information from it. Once enough entropy is
     953                 :            :                          * available, the next atempt will trigger GMK/Key
     954                 :            :                          * Counter update and the station will be allowed to
     955                 :            :                          * continue.
     956                 :            :                          */
     957                 :          0 :                         wpa_printf(MSG_DEBUG, "WPA: Reject 4-way handshake to "
     958                 :            :                                    "collect more entropy for random number "
     959                 :            :                                    "generation");
     960                 :            :                         random_mark_pool_ready();
     961                 :          0 :                         wpa_sta_disconnect(wpa_auth, sm->addr);
     962                 :          0 :                         return;
     963                 :            :                 }
     964         [ -  + ]:        277 :                 if (wpa_parse_kde_ies((u8 *) (key + 1), key_data_length,
     965                 :            :                                       &kde) < 0) {
     966                 :          0 :                         wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
     967                 :            :                                          "received EAPOL-Key msg 2/4 with "
     968                 :            :                                          "invalid Key Data contents");
     969                 :          0 :                         return;
     970                 :            :                 }
     971         [ +  + ]:        277 :                 if (kde.rsn_ie) {
     972                 :        270 :                         eapol_key_ie = kde.rsn_ie;
     973                 :        270 :                         eapol_key_ie_len = kde.rsn_ie_len;
     974                 :            :                 } else {
     975                 :          7 :                         eapol_key_ie = kde.wpa_ie;
     976                 :          7 :                         eapol_key_ie_len = kde.wpa_ie_len;
     977                 :            :                 }
     978   [ +  +  +  + ]:        547 :                 ft = sm->wpa == WPA_VERSION_WPA2 &&
     979                 :        270 :                         wpa_key_mgmt_ft(sm->wpa_key_mgmt);
     980   [ +  -  -  + ]:        554 :                 if (sm->wpa_ie == NULL ||
     981                 :        277 :                     wpa_compare_rsn_ie(ft,
     982                 :        277 :                                        sm->wpa_ie, sm->wpa_ie_len,
     983                 :            :                                        eapol_key_ie, eapol_key_ie_len)) {
     984                 :          0 :                         wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
     985                 :            :                                         "WPA IE from (Re)AssocReq did not "
     986                 :            :                                         "match with msg 2/4");
     987         [ #  # ]:          0 :                         if (sm->wpa_ie) {
     988                 :          0 :                                 wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
     989                 :          0 :                                             sm->wpa_ie, sm->wpa_ie_len);
     990                 :            :                         }
     991                 :          0 :                         wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
     992                 :            :                                     eapol_key_ie, eapol_key_ie_len);
     993                 :            :                         /* MLME-DEAUTHENTICATE.request */
     994                 :          0 :                         wpa_sta_disconnect(wpa_auth, sm->addr);
     995                 :          0 :                         return;
     996                 :            :                 }
     997                 :            : #ifdef CONFIG_IEEE80211R
     998 [ +  + ][ -  + ]:        277 :                 if (ft && ft_check_msg_2_of_4(wpa_auth, sm, &kde) < 0) {
     999                 :          0 :                         wpa_sta_disconnect(wpa_auth, sm->addr);
    1000                 :          0 :                         return;
    1001                 :            :                 }
    1002                 :            : #endif /* CONFIG_IEEE80211R */
    1003                 :        277 :                 break;
    1004                 :            :         case PAIRWISE_4:
    1005 [ +  - ][ -  + ]:        253 :                 if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
    1006                 :        253 :                     !sm->PTK_valid) {
    1007                 :          0 :                         wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
    1008                 :            :                                          "received EAPOL-Key msg 4/4 in "
    1009                 :            :                                          "invalid state (%d) - dropped",
    1010                 :          0 :                                          sm->wpa_ptk_state);
    1011                 :          0 :                         return;
    1012                 :            :                 }
    1013                 :        253 :                 break;
    1014                 :            :         case GROUP_2:
    1015         [ +  - ]:          8 :                 if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
    1016         [ -  + ]:          8 :                     || !sm->PTK_valid) {
    1017                 :          0 :                         wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
    1018                 :            :                                          "received EAPOL-Key msg 2/2 in "
    1019                 :            :                                          "invalid state (%d) - dropped",
    1020                 :          0 :                                          sm->wpa_ptk_group_state);
    1021                 :          0 :                         return;
    1022                 :            :                 }
    1023                 :          8 :                 break;
    1024                 :            : #ifdef CONFIG_PEERKEY
    1025                 :            :         case SMK_M1:
    1026                 :            :         case SMK_M3:
    1027                 :            :         case SMK_ERROR:
    1028         [ -  + ]:          2 :                 if (!wpa_auth->conf.peerkey) {
    1029                 :          0 :                         wpa_printf(MSG_DEBUG, "RSN: SMK M1/M3/Error, but "
    1030                 :            :                                    "PeerKey use disabled - ignoring message");
    1031                 :          0 :                         return;
    1032                 :            :                 }
    1033         [ -  + ]:          2 :                 if (!sm->PTK_valid) {
    1034                 :          0 :                         wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
    1035                 :            :                                         "received EAPOL-Key msg SMK in "
    1036                 :            :                                         "invalid state - dropped");
    1037                 :          0 :                         return;
    1038                 :            :                 }
    1039                 :          2 :                 break;
    1040                 :            : #else /* CONFIG_PEERKEY */
    1041                 :            :         case SMK_M1:
    1042                 :            :         case SMK_M3:
    1043                 :            :         case SMK_ERROR:
    1044                 :            :                 return; /* STSL disabled - ignore SMK messages */
    1045                 :            : #endif /* CONFIG_PEERKEY */
    1046                 :            :         case REQUEST:
    1047                 :          0 :                 break;
    1048                 :            :         }
    1049                 :            : 
    1050                 :        540 :         wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
    1051                 :            :                          "received EAPOL-Key frame (%s)", msgtxt);
    1052                 :            : 
    1053         [ -  + ]:        540 :         if (key_info & WPA_KEY_INFO_ACK) {
    1054                 :          0 :                 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
    1055                 :            :                                 "received invalid EAPOL-Key: Key Ack set");
    1056                 :          0 :                 return;
    1057                 :            :         }
    1058                 :            : 
    1059         [ -  + ]:        540 :         if (!(key_info & WPA_KEY_INFO_MIC)) {
    1060                 :          0 :                 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
    1061                 :            :                                 "received invalid EAPOL-Key: Key MIC not set");
    1062                 :          0 :                 return;
    1063                 :            :         }
    1064                 :            : 
    1065                 :        540 :         sm->MICVerified = FALSE;
    1066 [ +  + ][ +  - ]:        540 :         if (sm->PTK_valid && !sm->update_snonce) {
    1067         [ -  + ]:        263 :                 if (wpa_verify_key_mic(&sm->PTK, data, data_len)) {
    1068                 :          0 :                         wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
    1069                 :            :                                         "received EAPOL-Key with invalid MIC");
    1070                 :          0 :                         return;
    1071                 :            :                 }
    1072                 :        263 :                 sm->MICVerified = TRUE;
    1073                 :        263 :                 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
    1074                 :        263 :                 sm->pending_1_of_4_timeout = 0;
    1075                 :            :         }
    1076                 :            : 
    1077         [ +  + ]:        540 :         if (key_info & WPA_KEY_INFO_REQUEST) {
    1078         [ +  - ]:          1 :                 if (sm->MICVerified) {
    1079                 :          1 :                         sm->req_replay_counter_used = 1;
    1080                 :          1 :                         os_memcpy(sm->req_replay_counter, key->replay_counter,
    1081                 :            :                                   WPA_REPLAY_COUNTER_LEN);
    1082                 :            :                 } else {
    1083                 :          0 :                         wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
    1084                 :            :                                         "received EAPOL-Key request with "
    1085                 :            :                                         "invalid MIC");
    1086                 :          0 :                         return;
    1087                 :            :                 }
    1088                 :            : 
    1089                 :            :                 /*
    1090                 :            :                  * TODO: should decrypt key data field if encryption was used;
    1091                 :            :                  * even though MAC address KDE is not normally encrypted,
    1092                 :            :                  * supplicant is allowed to encrypt it.
    1093                 :            :                  */
    1094         [ -  + ]:          1 :                 if (msg == SMK_ERROR) {
    1095                 :            : #ifdef CONFIG_PEERKEY
    1096                 :          0 :                         wpa_smk_error(wpa_auth, sm, key);
    1097                 :            : #endif /* CONFIG_PEERKEY */
    1098                 :          0 :                         return;
    1099         [ -  + ]:          1 :                 } else if (key_info & WPA_KEY_INFO_ERROR) {
    1100         [ #  # ]:          0 :                         if (wpa_receive_error_report(
    1101                 :            :                                     wpa_auth, sm,
    1102                 :          0 :                                     !(key_info & WPA_KEY_INFO_KEY_TYPE)) > 0)
    1103                 :          0 :                                 return; /* STA entry was removed */
    1104         [ -  + ]:          1 :                 } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
    1105                 :          0 :                         wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
    1106                 :            :                                         "received EAPOL-Key Request for new "
    1107                 :            :                                         "4-Way Handshake");
    1108                 :          0 :                         wpa_request_new_ptk(sm);
    1109                 :            : #ifdef CONFIG_PEERKEY
    1110         [ +  - ]:          1 :                 } else if (msg == SMK_M1) {
    1111                 :          1 :                         wpa_smk_m1(wpa_auth, sm, key);
    1112                 :            : #endif /* CONFIG_PEERKEY */
    1113   [ #  #  #  # ]:          0 :                 } else if (key_data_length > 0 &&
    1114                 :          0 :                            wpa_parse_kde_ies((const u8 *) (key + 1),
    1115         [ #  # ]:          0 :                                              key_data_length, &kde) == 0 &&
    1116                 :          0 :                            kde.mac_addr) {
    1117                 :            :                 } else {
    1118                 :          0 :                         wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
    1119                 :            :                                         "received EAPOL-Key Request for GTK "
    1120                 :            :                                         "rekeying");
    1121                 :          0 :                         eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
    1122                 :          1 :                         wpa_rekey_gtk(wpa_auth, NULL);
    1123                 :            :                 }
    1124                 :            :         } else {
    1125                 :            :                 /* Do not allow the same key replay counter to be reused. */
    1126                 :        539 :                 wpa_replay_counter_mark_invalid(sm->key_replay,
    1127                 :        539 :                                                 key->replay_counter);
    1128                 :            : 
    1129         [ +  + ]:        539 :                 if (msg == PAIRWISE_2) {
    1130                 :            :                         /*
    1131                 :            :                          * Maintain a copy of the pending EAPOL-Key frames in
    1132                 :            :                          * case the EAPOL-Key frame was retransmitted. This is
    1133                 :            :                          * needed to allow EAPOL-Key msg 2/4 reply to another
    1134                 :            :                          * pending msg 1/4 to update the SNonce to work around
    1135                 :            :                          * unexpected supplicant behavior.
    1136                 :            :                          */
    1137                 :        277 :                         os_memcpy(sm->prev_key_replay, sm->key_replay,
    1138                 :            :                                   sizeof(sm->key_replay));
    1139                 :            :                 } else {
    1140                 :        262 :                         os_memset(sm->prev_key_replay, 0,
    1141                 :            :                                   sizeof(sm->prev_key_replay));
    1142                 :            :                 }
    1143                 :            : 
    1144                 :            :                 /*
    1145                 :            :                  * Make sure old valid counters are not accepted anymore and
    1146                 :            :                  * do not get copied again.
    1147                 :            :                  */
    1148                 :        539 :                 wpa_replay_counter_mark_invalid(sm->key_replay, NULL);
    1149                 :            :         }
    1150                 :            : 
    1151                 :            : #ifdef CONFIG_PEERKEY
    1152         [ +  + ]:        540 :         if (msg == SMK_M3) {
    1153                 :          1 :                 wpa_smk_m3(wpa_auth, sm, key);
    1154                 :          1 :                 return;
    1155                 :            :         }
    1156                 :            : #endif /* CONFIG_PEERKEY */
    1157                 :            : 
    1158                 :        539 :         os_free(sm->last_rx_eapol_key);
    1159                 :        539 :         sm->last_rx_eapol_key = os_malloc(data_len);
    1160         [ -  + ]:        539 :         if (sm->last_rx_eapol_key == NULL)
    1161                 :          0 :                 return;
    1162                 :        539 :         os_memcpy(sm->last_rx_eapol_key, data, data_len);
    1163                 :        539 :         sm->last_rx_eapol_key_len = data_len;
    1164                 :            : 
    1165                 :        539 :         sm->rx_eapol_key_secure = !!(key_info & WPA_KEY_INFO_SECURE);
    1166                 :        539 :         sm->EAPOLKeyReceived = TRUE;
    1167                 :        539 :         sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
    1168                 :        539 :         sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
    1169                 :        539 :         os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
    1170                 :        540 :         wpa_sm_step(sm);
    1171                 :            : }
    1172                 :            : 
    1173                 :            : 
    1174                 :        482 : static int wpa_gmk_to_gtk(const u8 *gmk, const char *label, const u8 *addr,
    1175                 :            :                           const u8 *gnonce, u8 *gtk, size_t gtk_len)
    1176                 :            : {
    1177                 :            :         u8 data[ETH_ALEN + WPA_NONCE_LEN + 8 + 16];
    1178                 :            :         u8 *pos;
    1179                 :        482 :         int ret = 0;
    1180                 :            : 
    1181                 :            :         /* GTK = PRF-X(GMK, "Group key expansion",
    1182                 :            :          *      AA || GNonce || Time || random data)
    1183                 :            :          * The example described in the IEEE 802.11 standard uses only AA and
    1184                 :            :          * GNonce as inputs here. Add some more entropy since this derivation
    1185                 :            :          * is done only at the Authenticator and as such, does not need to be
    1186                 :            :          * exactly same.
    1187                 :            :          */
    1188                 :        482 :         os_memcpy(data, addr, ETH_ALEN);
    1189                 :        482 :         os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
    1190                 :        482 :         pos = data + ETH_ALEN + WPA_NONCE_LEN;
    1191                 :        482 :         wpa_get_ntp_timestamp(pos);
    1192                 :        482 :         pos += 8;
    1193         [ -  + ]:        482 :         if (random_get_bytes(pos, 16) < 0)
    1194                 :          0 :                 ret = -1;
    1195                 :            : 
    1196                 :            : #ifdef CONFIG_IEEE80211W
    1197                 :        482 :         sha256_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len);
    1198                 :            : #else /* CONFIG_IEEE80211W */
    1199                 :            :         if (sha1_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len)
    1200                 :            :             < 0)
    1201                 :            :                 ret = -1;
    1202                 :            : #endif /* CONFIG_IEEE80211W */
    1203                 :            : 
    1204                 :        482 :         return ret;
    1205                 :            : }
    1206                 :            : 
    1207                 :            : 
    1208                 :         24 : static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
    1209                 :            : {
    1210                 :         24 :         struct wpa_authenticator *wpa_auth = eloop_ctx;
    1211                 :         24 :         struct wpa_state_machine *sm = timeout_ctx;
    1212                 :            : 
    1213                 :         24 :         sm->pending_1_of_4_timeout = 0;
    1214                 :         24 :         wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout");
    1215                 :         24 :         sm->TimeoutEvt = TRUE;
    1216                 :         24 :         wpa_sm_step(sm);
    1217                 :         24 : }
    1218                 :            : 
    1219                 :            : 
    1220                 :        541 : void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
    1221                 :            :                       struct wpa_state_machine *sm, int key_info,
    1222                 :            :                       const u8 *key_rsc, const u8 *nonce,
    1223                 :            :                       const u8 *kde, size_t kde_len,
    1224                 :            :                       int keyidx, int encr, int force_version)
    1225                 :            : {
    1226                 :            :         struct ieee802_1x_hdr *hdr;
    1227                 :            :         struct wpa_eapol_key *key;
    1228                 :            :         size_t len;
    1229                 :            :         int alg;
    1230                 :        541 :         int key_data_len, pad_len = 0;
    1231                 :            :         u8 *buf, *pos;
    1232                 :            :         int version, pairwise;
    1233                 :            :         int i;
    1234                 :            : 
    1235                 :        541 :         len = sizeof(struct ieee802_1x_hdr) + sizeof(struct wpa_eapol_key);
    1236                 :            : 
    1237         [ -  + ]:        541 :         if (force_version)
    1238                 :          0 :                 version = force_version;
    1239         [ +  + ]:        541 :         else if (wpa_use_aes_cmac(sm))
    1240                 :         32 :                 version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
    1241         [ +  + ]:        509 :         else if (sm->pairwise != WPA_CIPHER_TKIP)
    1242                 :        486 :                 version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
    1243                 :            :         else
    1244                 :         23 :                 version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
    1245                 :            : 
    1246                 :        541 :         pairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
    1247                 :            : 
    1248                 :        541 :         wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
    1249                 :            :                    "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
    1250                 :            :                    "encr=%d)",
    1251                 :            :                    version,
    1252                 :        541 :                    (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
    1253                 :        541 :                    (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
    1254                 :        541 :                    (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
    1255                 :        541 :                    (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
    1256                 :            :                    pairwise, (unsigned long) kde_len, keyidx, encr);
    1257                 :            : 
    1258                 :        541 :         key_data_len = kde_len;
    1259                 :            : 
    1260 [ +  + ][ +  + ]:        541 :         if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
    1261         [ +  + ]:        518 :              version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
    1262                 :        248 :                 pad_len = key_data_len % 8;
    1263         [ +  + ]:        248 :                 if (pad_len)
    1264                 :        246 :                         pad_len = 8 - pad_len;
    1265                 :        248 :                 key_data_len += pad_len + 8;
    1266                 :            :         }
    1267                 :            : 
    1268                 :        541 :         len += key_data_len;
    1269                 :            : 
    1270                 :        541 :         hdr = os_zalloc(len);
    1271         [ -  + ]:        541 :         if (hdr == NULL)
    1272                 :          0 :                 return;
    1273                 :        541 :         hdr->version = wpa_auth->conf.eapol_version;
    1274                 :        541 :         hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
    1275                 :        541 :         hdr->length = host_to_be16(len  - sizeof(*hdr));
    1276                 :        541 :         key = (struct wpa_eapol_key *) (hdr + 1);
    1277                 :            : 
    1278         [ +  + ]:        541 :         key->type = sm->wpa == WPA_VERSION_WPA2 ?
    1279                 :            :                 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
    1280                 :        541 :         key_info |= version;
    1281 [ +  + ][ +  + ]:        541 :         if (encr && sm->wpa == WPA_VERSION_WPA2)
    1282                 :        249 :                 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
    1283         [ +  + ]:        541 :         if (sm->wpa != WPA_VERSION_WPA2)
    1284                 :         21 :                 key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
    1285                 :        541 :         WPA_PUT_BE16(key->key_info, key_info);
    1286                 :            : 
    1287         [ +  + ]:        541 :         alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group;
    1288                 :        541 :         WPA_PUT_BE16(key->key_length, wpa_cipher_key_len(alg));
    1289         [ +  + ]:        541 :         if (key_info & WPA_KEY_INFO_SMK_MESSAGE)
    1290                 :          3 :                 WPA_PUT_BE16(key->key_length, 0);
    1291                 :            : 
    1292                 :            :         /* FIX: STSL: what to use as key_replay_counter? */
    1293         [ +  + ]:       2164 :         for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
    1294                 :       1623 :                 sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
    1295                 :       1623 :                 os_memcpy(sm->key_replay[i].counter,
    1296                 :            :                           sm->key_replay[i - 1].counter,
    1297                 :            :                           WPA_REPLAY_COUNTER_LEN);
    1298                 :            :         }
    1299                 :        541 :         inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
    1300                 :        541 :         os_memcpy(key->replay_counter, sm->key_replay[0].counter,
    1301                 :            :                   WPA_REPLAY_COUNTER_LEN);
    1302                 :        541 :         sm->key_replay[0].valid = TRUE;
    1303                 :            : 
    1304         [ +  - ]:        541 :         if (nonce)
    1305                 :        541 :                 os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
    1306                 :            : 
    1307         [ +  + ]:        541 :         if (key_rsc)
    1308                 :        254 :                 os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
    1309                 :            : 
    1310 [ +  + ][ +  + ]:        541 :         if (kde && !encr) {
    1311                 :         93 :                 os_memcpy(key + 1, kde, kde_len);
    1312                 :         93 :                 WPA_PUT_BE16(key->key_data_length, kde_len);
    1313 [ +  + ][ +  - ]:        448 :         } else if (encr && kde) {
    1314                 :        256 :                 buf = os_zalloc(key_data_len);
    1315         [ -  + ]:        256 :                 if (buf == NULL) {
    1316                 :          0 :                         os_free(hdr);
    1317                 :          0 :                         return;
    1318                 :            :                 }
    1319                 :        256 :                 pos = buf;
    1320                 :        256 :                 os_memcpy(pos, kde, kde_len);
    1321                 :        256 :                 pos += kde_len;
    1322                 :            : 
    1323         [ +  + ]:        256 :                 if (pad_len)
    1324                 :        246 :                         *pos++ = 0xdd;
    1325                 :            : 
    1326                 :        256 :                 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
    1327                 :            :                                 buf, key_data_len);
    1328 [ +  + ][ +  + ]:        256 :                 if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
    1329                 :            :                     version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
    1330         [ -  + ]:        248 :                         if (aes_wrap(sm->PTK.kek, (key_data_len - 8) / 8, buf,
    1331                 :            :                                      (u8 *) (key + 1))) {
    1332                 :          0 :                                 os_free(hdr);
    1333                 :          0 :                                 os_free(buf);
    1334                 :          0 :                                 return;
    1335                 :            :                         }
    1336                 :        248 :                         WPA_PUT_BE16(key->key_data_length, key_data_len);
    1337                 :            :                 } else {
    1338                 :            :                         u8 ek[32];
    1339                 :          8 :                         os_memcpy(key->key_iv,
    1340                 :            :                                   sm->group->Counter + WPA_NONCE_LEN - 16, 16);
    1341                 :          8 :                         inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
    1342                 :          8 :                         os_memcpy(ek, key->key_iv, 16);
    1343                 :          8 :                         os_memcpy(ek + 16, sm->PTK.kek, 16);
    1344                 :          8 :                         os_memcpy(key + 1, buf, key_data_len);
    1345                 :          8 :                         rc4_skip(ek, 32, 256, (u8 *) (key + 1), key_data_len);
    1346                 :          8 :                         WPA_PUT_BE16(key->key_data_length, key_data_len);
    1347                 :            :                 }
    1348                 :        256 :                 os_free(buf);
    1349                 :            :         }
    1350                 :            : 
    1351         [ +  + ]:        541 :         if (key_info & WPA_KEY_INFO_MIC) {
    1352         [ -  + ]:        264 :                 if (!sm->PTK_valid) {
    1353                 :          0 :                         wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
    1354                 :            :                                         "PTK not valid when sending EAPOL-Key "
    1355                 :            :                                         "frame");
    1356                 :          0 :                         os_free(hdr);
    1357                 :          0 :                         return;
    1358                 :            :                 }
    1359                 :        264 :                 wpa_eapol_key_mic(sm->PTK.kck, version, (u8 *) hdr, len,
    1360                 :        264 :                                   key->key_mic);
    1361                 :            : #ifdef CONFIG_TESTING_OPTIONS
    1362 [ -  + ][ +  + ]:        264 :                 if (!pairwise &&
    1363         [ #  # ]:          0 :                     wpa_auth->conf.corrupt_gtk_rekey_mic_probability > 0.0d &&
    1364                 :          0 :                     drand48() <
    1365                 :          0 :                     wpa_auth->conf.corrupt_gtk_rekey_mic_probability) {
    1366                 :          0 :                         wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
    1367                 :            :                                         "Corrupting group EAPOL-Key Key MIC");
    1368                 :          0 :                         key->key_mic[0]++;
    1369                 :            :                 }
    1370                 :            : #endif /* CONFIG_TESTING_OPTIONS */
    1371                 :            :         }
    1372                 :            : 
    1373                 :        541 :         wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx,
    1374                 :            :                            1);
    1375                 :        541 :         wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
    1376                 :        541 :                             sm->pairwise_set);
    1377                 :        541 :         os_free(hdr);
    1378                 :            : }
    1379                 :            : 
    1380                 :            : 
    1381                 :        538 : static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
    1382                 :            :                            struct wpa_state_machine *sm, int key_info,
    1383                 :            :                            const u8 *key_rsc, const u8 *nonce,
    1384                 :            :                            const u8 *kde, size_t kde_len,
    1385                 :            :                            int keyidx, int encr)
    1386                 :            : {
    1387                 :            :         int timeout_ms;
    1388                 :        538 :         int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
    1389                 :            :         int ctr;
    1390                 :            : 
    1391         [ -  + ]:        538 :         if (sm == NULL)
    1392                 :        538 :                 return;
    1393                 :            : 
    1394                 :        538 :         __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
    1395                 :            :                          keyidx, encr, 0);
    1396                 :            : 
    1397         [ +  + ]:        538 :         ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
    1398 [ +  + ][ +  + ]:        538 :         if (ctr == 1 && wpa_auth->conf.tx_status)
    1399         [ +  + ]:        501 :                 timeout_ms = pairwise ? eapol_key_timeout_first :
    1400                 :            :                         eapol_key_timeout_first_group;
    1401                 :            :         else
    1402                 :         37 :                 timeout_ms = eapol_key_timeout_subseq;
    1403 [ +  + ][ +  + ]:        538 :         if (pairwise && ctr == 1 && !(key_info & WPA_KEY_INFO_MIC))
                 [ +  + ]
    1404                 :        259 :                 sm->pending_1_of_4_timeout = 1;
    1405                 :        538 :         wpa_printf(MSG_DEBUG, "WPA: Use EAPOL-Key timeout of %u ms (retry "
    1406                 :            :                    "counter %d)", timeout_ms, ctr);
    1407                 :        538 :         eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
    1408                 :            :                                wpa_send_eapol_timeout, wpa_auth, sm);
    1409                 :            : }
    1410                 :            : 
    1411                 :            : 
    1412                 :        540 : static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len)
    1413                 :            : {
    1414                 :            :         struct ieee802_1x_hdr *hdr;
    1415                 :            :         struct wpa_eapol_key *key;
    1416                 :            :         u16 key_info;
    1417                 :        540 :         int ret = 0;
    1418                 :            :         u8 mic[16];
    1419                 :            : 
    1420         [ -  + ]:        540 :         if (data_len < sizeof(*hdr) + sizeof(*key))
    1421                 :          0 :                 return -1;
    1422                 :            : 
    1423                 :        540 :         hdr = (struct ieee802_1x_hdr *) data;
    1424                 :        540 :         key = (struct wpa_eapol_key *) (hdr + 1);
    1425                 :        540 :         key_info = WPA_GET_BE16(key->key_info);
    1426                 :        540 :         os_memcpy(mic, key->key_mic, 16);
    1427                 :        540 :         os_memset(key->key_mic, 0, 16);
    1428         [ +  - ]:        540 :         if (wpa_eapol_key_mic(PTK->kck, key_info & WPA_KEY_INFO_TYPE_MASK,
    1429         [ +  + ]:        540 :                               data, data_len, key->key_mic) ||
    1430                 :        540 :             os_memcmp(mic, key->key_mic, 16) != 0)
    1431                 :         24 :                 ret = -1;
    1432                 :        540 :         os_memcpy(key->key_mic, mic, 16);
    1433                 :        540 :         return ret;
    1434                 :            : }
    1435                 :            : 
    1436                 :            : 
    1437                 :       1330 : void wpa_remove_ptk(struct wpa_state_machine *sm)
    1438                 :            : {
    1439                 :       1330 :         sm->PTK_valid = FALSE;
    1440                 :       1330 :         os_memset(&sm->PTK, 0, sizeof(sm->PTK));
    1441                 :       1330 :         wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, NULL, 0);
    1442                 :       1330 :         sm->pairwise_set = FALSE;
    1443                 :       1330 :         eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
    1444                 :       1330 : }
    1445                 :            : 
    1446                 :            : 
    1447                 :       1261 : int wpa_auth_sm_event(struct wpa_state_machine *sm, wpa_event event)
    1448                 :            : {
    1449                 :       1261 :         int remove_ptk = 1;
    1450                 :            : 
    1451         [ +  + ]:       1261 :         if (sm == NULL)
    1452                 :        789 :                 return -1;
    1453                 :            : 
    1454                 :        472 :         wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
    1455                 :            :                          "event %d notification", event);
    1456                 :            : 
    1457   [ +  +  +  +  :        472 :         switch (event) {
                      - ]
    1458                 :            :         case WPA_AUTH:
    1459                 :            :         case WPA_ASSOC:
    1460                 :        250 :                 break;
    1461                 :            :         case WPA_DEAUTH:
    1462                 :            :         case WPA_DISASSOC:
    1463                 :        184 :                 sm->DeauthenticationRequest = TRUE;
    1464                 :        184 :                 break;
    1465                 :            :         case WPA_REAUTH:
    1466                 :            :         case WPA_REAUTH_EAPOL:
    1467         [ -  + ]:         22 :                 if (!sm->started) {
    1468                 :            :                         /*
    1469                 :            :                          * When using WPS, we may end up here if the STA
    1470                 :            :                          * manages to re-associate without the previous STA
    1471                 :            :                          * entry getting removed. Consequently, we need to make
    1472                 :            :                          * sure that the WPA state machines gets initialized
    1473                 :            :                          * properly at this point.
    1474                 :            :                          */
    1475                 :          0 :                         wpa_printf(MSG_DEBUG, "WPA state machine had not been "
    1476                 :            :                                    "started - initialize now");
    1477                 :          0 :                         sm->started = 1;
    1478                 :          0 :                         sm->Init = TRUE;
    1479         [ #  # ]:          0 :                         if (wpa_sm_step(sm) == 1)
    1480                 :          0 :                                 return 1; /* should not really happen */
    1481                 :          0 :                         sm->Init = FALSE;
    1482                 :          0 :                         sm->AuthenticationRequest = TRUE;
    1483                 :          0 :                         break;
    1484                 :            :                 }
    1485         [ -  + ]:         22 :                 if (sm->GUpdateStationKeys) {
    1486                 :            :                         /*
    1487                 :            :                          * Reauthentication cancels the pending group key
    1488                 :            :                          * update for this STA.
    1489                 :            :                          */
    1490                 :          0 :                         sm->group->GKeyDoneStations--;
    1491                 :          0 :                         sm->GUpdateStationKeys = FALSE;
    1492                 :          0 :                         sm->PtkGroupInit = TRUE;
    1493                 :            :                 }
    1494                 :         22 :                 sm->ReAuthenticationRequest = TRUE;
    1495                 :         22 :                 break;
    1496                 :            :         case WPA_ASSOC_FT:
    1497                 :            : #ifdef CONFIG_IEEE80211R
    1498                 :         16 :                 wpa_printf(MSG_DEBUG, "FT: Retry PTK configuration "
    1499                 :            :                            "after association");
    1500                 :         16 :                 wpa_ft_install_ptk(sm);
    1501                 :            : 
    1502                 :            :                 /* Using FT protocol, not WPA auth state machine */
    1503                 :         16 :                 sm->ft_completed = 1;
    1504                 :         16 :                 return 0;
    1505                 :            : #else /* CONFIG_IEEE80211R */
    1506                 :            :                 break;
    1507                 :            : #endif /* CONFIG_IEEE80211R */
    1508                 :            :         }
    1509                 :            : 
    1510                 :            : #ifdef CONFIG_IEEE80211R
    1511                 :        456 :         sm->ft_completed = 0;
    1512                 :            : #endif /* CONFIG_IEEE80211R */
    1513                 :            : 
    1514                 :            : #ifdef CONFIG_IEEE80211W
    1515 [ +  + ][ -  + ]:        456 :         if (sm->mgmt_frame_prot && event == WPA_AUTH)
    1516                 :          0 :                 remove_ptk = 0;
    1517                 :            : #endif /* CONFIG_IEEE80211W */
    1518                 :            : 
    1519         [ +  - ]:        456 :         if (remove_ptk) {
    1520                 :        456 :                 sm->PTK_valid = FALSE;
    1521                 :        456 :                 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
    1522                 :            : 
    1523         [ +  + ]:        456 :                 if (event != WPA_REAUTH_EAPOL)
    1524                 :        434 :                         wpa_remove_ptk(sm);
    1525                 :            :         }
    1526                 :            : 
    1527                 :       1261 :         return wpa_sm_step(sm);
    1528                 :            : }
    1529                 :            : 
    1530                 :            : 
    1531                 :        416 : SM_STATE(WPA_PTK, INITIALIZE)
    1532                 :            : {
    1533 [ -  + ][ #  # ]:        416 :         SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
    1534         [ +  + ]:        416 :         if (sm->Init) {
    1535                 :            :                 /* Init flag is not cleared here, so avoid busy
    1536                 :            :                  * loop by claiming nothing changed. */
    1537                 :        232 :                 sm->changed = FALSE;
    1538                 :            :         }
    1539                 :            : 
    1540                 :        416 :         sm->keycount = 0;
    1541         [ -  + ]:        416 :         if (sm->GUpdateStationKeys)
    1542                 :          0 :                 sm->group->GKeyDoneStations--;
    1543                 :        416 :         sm->GUpdateStationKeys = FALSE;
    1544         [ +  + ]:        416 :         if (sm->wpa == WPA_VERSION_WPA)
    1545                 :         12 :                 sm->PInitAKeys = FALSE;
    1546                 :            :         if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
    1547                 :            :                * Local AA > Remote AA)) */) {
    1548                 :        416 :                 sm->Pair = TRUE;
    1549                 :            :         }
    1550                 :        416 :         wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
    1551                 :        416 :         wpa_remove_ptk(sm);
    1552                 :        416 :         wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
    1553                 :        416 :         sm->TimeoutCtr = 0;
    1554         [ +  + ]:        416 :         if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
    1555                 :        286 :                 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
    1556                 :            :                                    WPA_EAPOL_authorized, 0);
    1557                 :            :         }
    1558                 :        416 : }
    1559                 :            : 
    1560                 :            : 
    1561                 :          6 : SM_STATE(WPA_PTK, DISCONNECT)
    1562                 :            : {
    1563 [ -  + ][ #  # ]:          6 :         SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
    1564                 :          6 :         sm->Disconnect = FALSE;
    1565                 :          6 :         wpa_sta_disconnect(sm->wpa_auth, sm->addr);
    1566                 :          6 : }
    1567                 :            : 
    1568                 :            : 
    1569                 :        184 : SM_STATE(WPA_PTK, DISCONNECTED)
    1570                 :            : {
    1571 [ -  + ][ #  # ]:        184 :         SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
    1572                 :        184 :         sm->DeauthenticationRequest = FALSE;
    1573                 :        184 : }
    1574                 :            : 
    1575                 :            : 
    1576                 :        232 : SM_STATE(WPA_PTK, AUTHENTICATION)
    1577                 :            : {
    1578 [ -  + ][ #  # ]:        232 :         SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
    1579                 :        232 :         os_memset(&sm->PTK, 0, sizeof(sm->PTK));
    1580                 :        232 :         sm->PTK_valid = FALSE;
    1581                 :        232 :         wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
    1582                 :            :                            1);
    1583                 :        232 :         wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
    1584                 :        232 :         sm->AuthenticationRequest = FALSE;
    1585                 :        232 : }
    1586                 :            : 
    1587                 :            : 
    1588                 :        263 : static void wpa_group_ensure_init(struct wpa_authenticator *wpa_auth,
    1589                 :            :                                   struct wpa_group *group)
    1590                 :            : {
    1591         [ +  + ]:        263 :         if (group->first_sta_seen)
    1592                 :        263 :                 return;
    1593                 :            :         /*
    1594                 :            :          * System has run bit further than at the time hostapd was started
    1595                 :            :          * potentially very early during boot up. This provides better chances
    1596                 :            :          * of collecting more randomness on embedded systems. Re-initialize the
    1597                 :            :          * GMK and Counter here to improve their strength if there was not
    1598                 :            :          * enough entropy available immediately after system startup.
    1599                 :            :          */
    1600                 :        180 :         wpa_printf(MSG_DEBUG, "WPA: Re-initialize GMK/Counter on first "
    1601                 :            :                    "station");
    1602                 :            :         if (random_pool_ready() != 1) {
    1603                 :            :                 wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
    1604                 :            :                            "to proceed - reject first 4-way handshake");
    1605                 :            :                 group->reject_4way_hs_for_entropy = TRUE;
    1606                 :            :         } else {
    1607                 :        180 :                 group->first_sta_seen = TRUE;
    1608                 :        180 :                 group->reject_4way_hs_for_entropy = FALSE;
    1609                 :            :         }
    1610                 :            : 
    1611                 :        180 :         wpa_group_init_gmk_and_counter(wpa_auth, group);
    1612                 :        180 :         wpa_gtk_update(wpa_auth, group);
    1613                 :        180 :         wpa_group_config_group_keys(wpa_auth, group);
    1614                 :            : }
    1615                 :            : 
    1616                 :            : 
    1617                 :        263 : SM_STATE(WPA_PTK, AUTHENTICATION2)
    1618                 :            : {
    1619 [ -  + ][ #  # ]:        263 :         SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
    1620                 :            : 
    1621                 :        263 :         wpa_group_ensure_init(sm->wpa_auth, sm->group);
    1622                 :        263 :         sm->ReAuthenticationRequest = FALSE;
    1623                 :            : 
    1624                 :            :         /*
    1625                 :            :          * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat
    1626                 :            :          * ambiguous. The Authenticator state machine uses a counter that is
    1627                 :            :          * incremented by one for each 4-way handshake. However, the security
    1628                 :            :          * analysis of 4-way handshake points out that unpredictable nonces
    1629                 :            :          * help in preventing precomputation attacks. Instead of the state
    1630                 :            :          * machine definition, use an unpredictable nonce value here to provide
    1631                 :            :          * stronger protection against potential precomputation attacks.
    1632                 :            :          */
    1633         [ -  + ]:        263 :         if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
    1634                 :          0 :                 wpa_printf(MSG_ERROR, "WPA: Failed to get random data for "
    1635                 :            :                            "ANonce.");
    1636                 :          0 :                 sm->Disconnect = TRUE;
    1637                 :        263 :                 return;
    1638                 :            :         }
    1639                 :        263 :         wpa_hexdump(MSG_DEBUG, "WPA: Assign ANonce", sm->ANonce,
    1640                 :            :                     WPA_NONCE_LEN);
    1641                 :            :         /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
    1642                 :            :          * logical place than INITIALIZE since AUTHENTICATION2 can be
    1643                 :            :          * re-entered on ReAuthenticationRequest without going through
    1644                 :            :          * INITIALIZE. */
    1645                 :        263 :         sm->TimeoutCtr = 0;
    1646                 :            : }
    1647                 :            : 
    1648                 :            : 
    1649                 :         87 : SM_STATE(WPA_PTK, INITPMK)
    1650                 :            : {
    1651                 :            :         u8 msk[2 * PMK_LEN];
    1652                 :         87 :         size_t len = 2 * PMK_LEN;
    1653                 :            : 
    1654 [ -  + ][ #  # ]:         87 :         SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
    1655                 :            : #ifdef CONFIG_IEEE80211R
    1656                 :         87 :         sm->xxkey_len = 0;
    1657                 :            : #endif /* CONFIG_IEEE80211R */
    1658         [ +  + ]:         87 :         if (sm->pmksa) {
    1659                 :          5 :                 wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
    1660                 :          5 :                 os_memcpy(sm->PMK, sm->pmksa->pmk, PMK_LEN);
    1661         [ +  - ]:         82 :         } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
    1662                 :         82 :                 wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
    1663                 :            :                            "(len=%lu)", (unsigned long) len);
    1664                 :         82 :                 os_memcpy(sm->PMK, msk, PMK_LEN);
    1665                 :            : #ifdef CONFIG_IEEE80211R
    1666         [ +  - ]:         82 :                 if (len >= 2 * PMK_LEN) {
    1667                 :         82 :                         os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
    1668                 :         82 :                         sm->xxkey_len = PMK_LEN;
    1669                 :            :                 }
    1670                 :            : #endif /* CONFIG_IEEE80211R */
    1671                 :            :         } else {
    1672                 :          0 :                 wpa_printf(MSG_DEBUG, "WPA: Could not get PMK");
    1673                 :            :         }
    1674                 :            : 
    1675                 :         87 :         sm->req_replay_counter_used = 0;
    1676                 :            :         /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
    1677                 :            :          * will break reauthentication since EAPOL state machines may not be
    1678                 :            :          * get into AUTHENTICATING state that clears keyRun before WPA state
    1679                 :            :          * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
    1680                 :            :          * state and takes PMK from the previously used AAA Key. This will
    1681                 :            :          * eventually fail in 4-Way Handshake because Supplicant uses PMK
    1682                 :            :          * derived from the new AAA Key. Setting keyRun = FALSE here seems to
    1683                 :            :          * be good workaround for this issue. */
    1684                 :         87 :         wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
    1685                 :         87 : }
    1686                 :            : 
    1687                 :            : 
    1688                 :        172 : SM_STATE(WPA_PTK, INITPSK)
    1689                 :            : {
    1690                 :            :         const u8 *psk;
    1691 [ -  + ][ #  # ]:        172 :         SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
    1692                 :        172 :         psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr, NULL);
    1693         [ +  - ]:        172 :         if (psk) {
    1694                 :        172 :                 os_memcpy(sm->PMK, psk, PMK_LEN);
    1695                 :            : #ifdef CONFIG_IEEE80211R
    1696                 :        172 :                 os_memcpy(sm->xxkey, psk, PMK_LEN);
    1697                 :        172 :                 sm->xxkey_len = PMK_LEN;
    1698                 :            : #endif /* CONFIG_IEEE80211R */
    1699                 :            :         }
    1700                 :        172 :         sm->req_replay_counter_used = 0;
    1701                 :        172 : }
    1702                 :            : 
    1703                 :            : 
    1704                 :        283 : SM_STATE(WPA_PTK, PTKSTART)
    1705                 :            : {
    1706                 :        283 :         u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
    1707                 :        283 :         size_t pmkid_len = 0;
    1708                 :            : 
    1709 [ -  + ][ #  # ]:        283 :         SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
    1710                 :        283 :         sm->PTKRequest = FALSE;
    1711                 :        283 :         sm->TimeoutEvt = FALSE;
    1712                 :            : 
    1713                 :        283 :         sm->TimeoutCtr++;
    1714         [ +  + ]:        283 :         if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
    1715                 :            :                 /* No point in sending the EAPOL-Key - we will disconnect
    1716                 :            :                  * immediately following this. */
    1717                 :        283 :                 return;
    1718                 :            :         }
    1719                 :            : 
    1720                 :        277 :         wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
    1721                 :            :                         "sending 1/4 msg of 4-Way Handshake");
    1722                 :            :         /*
    1723                 :            :          * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
    1724                 :            :          * one possible PSK for this STA.
    1725                 :            :          */
    1726   [ +  +  +  + ]:        547 :         if (sm->wpa == WPA_VERSION_WPA2 &&
    1727                 :        270 :             wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt)) {
    1728                 :         85 :                 pmkid = buf;
    1729                 :         85 :                 pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
    1730                 :         85 :                 pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
    1731                 :         85 :                 pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
    1732                 :         85 :                 RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
    1733         [ +  + ]:         85 :                 if (sm->pmksa)
    1734                 :          5 :                         os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
    1735                 :            :                                   sm->pmksa->pmkid, PMKID_LEN);
    1736                 :            :                 else {
    1737                 :            :                         /*
    1738                 :            :                          * Calculate PMKID since no PMKSA cache entry was
    1739                 :            :                          * available with pre-calculated PMKID.
    1740                 :            :                          */
    1741                 :         80 :                         rsn_pmkid(sm->PMK, PMK_LEN, sm->wpa_auth->addr,
    1742                 :         80 :                                   sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
    1743                 :            :                                   wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
    1744                 :            :                 }
    1745                 :            :         }
    1746                 :        277 :         wpa_send_eapol(sm->wpa_auth, sm,
    1747                 :            :                        WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
    1748                 :        277 :                        sm->ANonce, pmkid, pmkid_len, 0, 0);
    1749                 :            : }
    1750                 :            : 
    1751                 :            : 
    1752                 :        277 : static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *pmk,
    1753                 :            :                           struct wpa_ptk *ptk)
    1754                 :            : {
    1755         [ +  + ]:        277 :         size_t ptk_len = sm->pairwise != WPA_CIPHER_TKIP ? 48 : 64;
    1756                 :            : #ifdef CONFIG_IEEE80211R
    1757         [ +  + ]:        277 :         if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
    1758                 :          8 :                 return wpa_auth_derive_ptk_ft(sm, pmk, ptk, ptk_len);
    1759                 :            : #endif /* CONFIG_IEEE80211R */
    1760                 :            : 
    1761                 :        269 :         wpa_pmk_to_ptk(pmk, PMK_LEN, "Pairwise key expansion",
    1762                 :        269 :                        sm->wpa_auth->addr, sm->addr, sm->ANonce, sm->SNonce,
    1763                 :            :                        (u8 *) ptk, ptk_len,
    1764                 :            :                        wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
    1765                 :            : 
    1766                 :        277 :         return 0;
    1767                 :            : }
    1768                 :            : 
    1769                 :            : 
    1770                 :        277 : SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
    1771                 :            : {
    1772                 :            :         struct wpa_ptk PTK;
    1773                 :        277 :         int ok = 0;
    1774                 :        277 :         const u8 *pmk = NULL;
    1775                 :            : 
    1776 [ -  + ][ #  # ]:        277 :         SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
    1777                 :        277 :         sm->EAPOLKeyReceived = FALSE;
    1778                 :        277 :         sm->update_snonce = FALSE;
    1779                 :            : 
    1780                 :            :         /* WPA with IEEE 802.1X: use the derived PMK from EAP
    1781                 :            :          * WPA-PSK: iterate through possible PSKs and select the one matching
    1782                 :            :          * the packet */
    1783                 :            :         for (;;) {
    1784         [ +  + ]:        301 :                 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
    1785                 :        214 :                         pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
    1786                 :        214 :                                                sm->p2p_dev_addr, pmk);
    1787         [ +  + ]:        214 :                         if (pmk == NULL)
    1788                 :         24 :                                 break;
    1789                 :            :                 } else
    1790                 :         87 :                         pmk = sm->PMK;
    1791                 :            : 
    1792                 :        277 :                 wpa_derive_ptk(sm, pmk, &PTK);
    1793                 :            : 
    1794         [ +  + ]:        277 :                 if (wpa_verify_key_mic(&PTK, sm->last_rx_eapol_key,
    1795                 :            :                                        sm->last_rx_eapol_key_len) == 0) {
    1796                 :        253 :                         ok = 1;
    1797                 :        253 :                         break;
    1798                 :            :                 }
    1799                 :            : 
    1800         [ -  + ]:         24 :                 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
    1801                 :          0 :                         break;
    1802                 :         24 :         }
    1803                 :            : 
    1804         [ +  + ]:        277 :         if (!ok) {
    1805                 :         24 :                 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
    1806                 :            :                                 "invalid MIC in msg 2/4 of 4-Way Handshake");
    1807                 :         24 :                 return;
    1808                 :            :         }
    1809                 :            : 
    1810                 :            : #ifdef CONFIG_IEEE80211R
    1811 [ +  + ][ +  + ]:        253 :         if (sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
    1812                 :            :                 /*
    1813                 :            :                  * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
    1814                 :            :                  * with the value we derived.
    1815                 :            :                  */
    1816         [ -  + ]:          8 :                 if (os_memcmp(sm->sup_pmk_r1_name, sm->pmk_r1_name,
    1817                 :            :                               WPA_PMK_NAME_LEN) != 0) {
    1818                 :          0 :                         wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
    1819                 :            :                                         "PMKR1Name mismatch in FT 4-way "
    1820                 :            :                                         "handshake");
    1821                 :          0 :                         wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from "
    1822                 :            :                                     "Supplicant",
    1823                 :          0 :                                     sm->sup_pmk_r1_name, WPA_PMK_NAME_LEN);
    1824                 :          0 :                         wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
    1825                 :          0 :                                     sm->pmk_r1_name, WPA_PMK_NAME_LEN);
    1826                 :          0 :                         return;
    1827                 :            :                 }
    1828                 :            :         }
    1829                 :            : #endif /* CONFIG_IEEE80211R */
    1830                 :            : 
    1831                 :        253 :         sm->pending_1_of_4_timeout = 0;
    1832                 :        253 :         eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
    1833                 :            : 
    1834         [ +  + ]:        253 :         if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
    1835                 :            :                 /* PSK may have changed from the previous choice, so update
    1836                 :            :                  * state machine data based on whatever PSK was selected here.
    1837                 :            :                  */
    1838                 :        166 :                 os_memcpy(sm->PMK, pmk, PMK_LEN);
    1839                 :            :         }
    1840                 :            : 
    1841                 :        253 :         sm->MICVerified = TRUE;
    1842                 :            : 
    1843                 :        253 :         os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
    1844                 :        277 :         sm->PTK_valid = TRUE;
    1845                 :            : }
    1846                 :            : 
    1847                 :            : 
    1848                 :        253 : SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
    1849                 :            : {
    1850 [ -  + ][ #  # ]:        253 :         SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
    1851                 :        253 :         sm->TimeoutCtr = 0;
    1852                 :        253 : }
    1853                 :            : 
    1854                 :            : 
    1855                 :            : #ifdef CONFIG_IEEE80211W
    1856                 :            : 
    1857                 :        254 : static int ieee80211w_kde_len(struct wpa_state_machine *sm)
    1858                 :            : {
    1859         [ +  + ]:        254 :         if (sm->mgmt_frame_prot) {
    1860                 :         12 :                 return 2 + RSN_SELECTOR_LEN + sizeof(struct wpa_igtk_kde);
    1861                 :            :         }
    1862                 :            : 
    1863                 :        254 :         return 0;
    1864                 :            : }
    1865                 :            : 
    1866                 :            : 
    1867                 :        254 : static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
    1868                 :            : {
    1869                 :            :         struct wpa_igtk_kde igtk;
    1870                 :        254 :         struct wpa_group *gsm = sm->group;
    1871                 :            :         u8 rsc[WPA_KEY_RSC_LEN];
    1872                 :            : 
    1873         [ +  + ]:        254 :         if (!sm->mgmt_frame_prot)
    1874                 :        242 :                 return pos;
    1875                 :            : 
    1876                 :         12 :         igtk.keyid[0] = gsm->GN_igtk;
    1877                 :         12 :         igtk.keyid[1] = 0;
    1878   [ +  -  -  + ]:         24 :         if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE ||
    1879                 :         12 :             wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, rsc) < 0)
    1880                 :          0 :                 os_memset(igtk.pn, 0, sizeof(igtk.pn));
    1881                 :            :         else
    1882                 :         12 :                 os_memcpy(igtk.pn, rsc, sizeof(igtk.pn));
    1883                 :         12 :         os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], WPA_IGTK_LEN);
    1884         [ -  + ]:         12 :         if (sm->wpa_auth->conf.disable_gtk) {
    1885                 :            :                 /*
    1886                 :            :                  * Provide unique random IGTK to each STA to prevent use of
    1887                 :            :                  * IGTK in the BSS.
    1888                 :            :                  */
    1889         [ #  # ]:          0 :                 if (random_get_bytes(igtk.igtk, WPA_IGTK_LEN) < 0)
    1890                 :          0 :                         return pos;
    1891                 :            :         }
    1892                 :         12 :         pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
    1893                 :            :                           (const u8 *) &igtk, sizeof(igtk), NULL, 0);
    1894                 :            : 
    1895                 :        254 :         return pos;
    1896                 :            : }
    1897                 :            : 
    1898                 :            : #else /* CONFIG_IEEE80211W */
    1899                 :            : 
    1900                 :            : static int ieee80211w_kde_len(struct wpa_state_machine *sm)
    1901                 :            : {
    1902                 :            :         return 0;
    1903                 :            : }
    1904                 :            : 
    1905                 :            : 
    1906                 :            : static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
    1907                 :            : {
    1908                 :            :         return pos;
    1909                 :            : }
    1910                 :            : 
    1911                 :            : #endif /* CONFIG_IEEE80211W */
    1912                 :            : 
    1913                 :            : 
    1914                 :        253 : SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
    1915                 :            : {
    1916                 :            :         u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos, dummy_gtk[32];
    1917                 :            :         size_t gtk_len, kde_len;
    1918                 :        253 :         struct wpa_group *gsm = sm->group;
    1919                 :            :         u8 *wpa_ie;
    1920                 :        253 :         int wpa_ie_len, secure, keyidx, encr = 0;
    1921                 :            : 
    1922 [ -  + ][ #  # ]:        253 :         SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
    1923                 :        253 :         sm->TimeoutEvt = FALSE;
    1924                 :            : 
    1925                 :        253 :         sm->TimeoutCtr++;
    1926         [ -  + ]:        253 :         if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
    1927                 :            :                 /* No point in sending the EAPOL-Key - we will disconnect
    1928                 :            :                  * immediately following this. */
    1929                 :          0 :                 return;
    1930                 :            :         }
    1931                 :            : 
    1932                 :            :         /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
    1933                 :            :            GTK[GN], IGTK, [FTIE], [TIE * 2])
    1934                 :            :          */
    1935                 :        253 :         os_memset(rsc, 0, WPA_KEY_RSC_LEN);
    1936                 :        253 :         wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
    1937                 :            :         /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
    1938                 :        253 :         wpa_ie = sm->wpa_auth->wpa_ie;
    1939                 :        253 :         wpa_ie_len = sm->wpa_auth->wpa_ie_len;
    1940 [ +  + ][ +  + ]:        253 :         if (sm->wpa == WPA_VERSION_WPA &&
    1941         [ +  - ]:          2 :             (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
    1942         [ +  - ]:          2 :             wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
    1943                 :            :                 /* WPA-only STA, remove RSN IE */
    1944                 :          2 :                 wpa_ie = wpa_ie + wpa_ie[1] + 2;
    1945                 :          2 :                 wpa_ie_len = wpa_ie[1] + 2;
    1946                 :            :         }
    1947                 :        253 :         wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
    1948                 :            :                         "sending 3/4 msg of 4-Way Handshake");
    1949         [ +  + ]:        253 :         if (sm->wpa == WPA_VERSION_WPA2) {
    1950                 :            :                 /* WPA2 send GTK in the 4-way handshake */
    1951                 :        246 :                 secure = 1;
    1952                 :        246 :                 gtk = gsm->GTK[gsm->GN - 1];
    1953                 :        246 :                 gtk_len = gsm->GTK_len;
    1954         [ -  + ]:        246 :                 if (sm->wpa_auth->conf.disable_gtk) {
    1955                 :            :                         /*
    1956                 :            :                          * Provide unique random GTK to each STA to prevent use
    1957                 :            :                          * of GTK in the BSS.
    1958                 :            :                          */
    1959         [ #  # ]:          0 :                         if (random_get_bytes(dummy_gtk, gtk_len) < 0)
    1960                 :          0 :                                 return;
    1961                 :          0 :                         gtk = dummy_gtk;
    1962                 :            :                 }
    1963                 :        246 :                 keyidx = gsm->GN;
    1964                 :        246 :                 _rsc = rsc;
    1965                 :        246 :                 encr = 1;
    1966                 :            :         } else {
    1967                 :            :                 /* WPA does not include GTK in msg 3/4 */
    1968                 :          7 :                 secure = 0;
    1969                 :          7 :                 gtk = NULL;
    1970                 :          7 :                 gtk_len = 0;
    1971                 :          7 :                 keyidx = 0;
    1972                 :          7 :                 _rsc = NULL;
    1973         [ -  + ]:          7 :                 if (sm->rx_eapol_key_secure) {
    1974                 :            :                         /*
    1975                 :            :                          * It looks like Windows 7 supplicant tries to use
    1976                 :            :                          * Secure bit in msg 2/4 after having reported Michael
    1977                 :            :                          * MIC failure and it then rejects the 4-way handshake
    1978                 :            :                          * if msg 3/4 does not set Secure bit. Work around this
    1979                 :            :                          * by setting the Secure bit here even in the case of
    1980                 :            :                          * WPA if the supplicant used it first.
    1981                 :            :                          */
    1982                 :          0 :                         wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
    1983                 :            :                                         "STA used Secure bit in WPA msg 2/4 - "
    1984                 :            :                                         "set Secure for 3/4 as workaround");
    1985                 :          0 :                         secure = 1;
    1986                 :            :                 }
    1987                 :            :         }
    1988                 :            : 
    1989                 :        253 :         kde_len = wpa_ie_len + ieee80211w_kde_len(sm);
    1990         [ +  + ]:        253 :         if (gtk)
    1991                 :        246 :                 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
    1992                 :            : #ifdef CONFIG_IEEE80211R
    1993         [ +  + ]:        253 :         if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
    1994                 :          8 :                 kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
    1995                 :          8 :                 kde_len += 300; /* FTIE + 2 * TIE */
    1996                 :            :         }
    1997                 :            : #endif /* CONFIG_IEEE80211R */
    1998                 :        253 :         kde = os_malloc(kde_len);
    1999         [ -  + ]:        253 :         if (kde == NULL)
    2000                 :          0 :                 return;
    2001                 :            : 
    2002                 :        253 :         pos = kde;
    2003                 :        253 :         os_memcpy(pos, wpa_ie, wpa_ie_len);
    2004                 :        253 :         pos += wpa_ie_len;
    2005                 :            : #ifdef CONFIG_IEEE80211R
    2006         [ +  + ]:        253 :         if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
    2007                 :          8 :                 int res = wpa_insert_pmkid(kde, pos - kde, sm->pmk_r1_name);
    2008         [ -  + ]:          8 :                 if (res < 0) {
    2009                 :          0 :                         wpa_printf(MSG_ERROR, "FT: Failed to insert "
    2010                 :            :                                    "PMKR1Name into RSN IE in EAPOL-Key data");
    2011                 :          0 :                         os_free(kde);
    2012                 :          0 :                         return;
    2013                 :            :                 }
    2014                 :          8 :                 pos += res;
    2015                 :            :         }
    2016                 :            : #endif /* CONFIG_IEEE80211R */
    2017         [ +  + ]:        253 :         if (gtk) {
    2018                 :            :                 u8 hdr[2];
    2019                 :        246 :                 hdr[0] = keyidx & 0x03;
    2020                 :        246 :                 hdr[1] = 0;
    2021                 :        246 :                 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
    2022                 :            :                                   gtk, gtk_len);
    2023                 :            :         }
    2024                 :        253 :         pos = ieee80211w_kde_add(sm, pos);
    2025                 :            : 
    2026                 :            : #ifdef CONFIG_IEEE80211R
    2027         [ +  + ]:        253 :         if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
    2028                 :            :                 int res;
    2029                 :            :                 struct wpa_auth_config *conf;
    2030                 :            : 
    2031                 :          8 :                 conf = &sm->wpa_auth->conf;
    2032                 :          8 :                 res = wpa_write_ftie(conf, conf->r0_key_holder,
    2033                 :            :                                      conf->r0_key_holder_len,
    2034                 :          8 :                                      NULL, NULL, pos, kde + kde_len - pos,
    2035                 :            :                                      NULL, 0);
    2036         [ -  + ]:          8 :                 if (res < 0) {
    2037                 :          0 :                         wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE "
    2038                 :            :                                    "into EAPOL-Key Key Data");
    2039                 :          0 :                         os_free(kde);
    2040                 :          0 :                         return;
    2041                 :            :                 }
    2042                 :          8 :                 pos += res;
    2043                 :            : 
    2044                 :            :                 /* TIE[ReassociationDeadline] (TU) */
    2045                 :          8 :                 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
    2046                 :          8 :                 *pos++ = 5;
    2047                 :          8 :                 *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
    2048                 :          8 :                 WPA_PUT_LE32(pos, conf->reassociation_deadline);
    2049                 :          8 :                 pos += 4;
    2050                 :            : 
    2051                 :            :                 /* TIE[KeyLifetime] (seconds) */
    2052                 :          8 :                 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
    2053                 :          8 :                 *pos++ = 5;
    2054                 :          8 :                 *pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
    2055                 :          8 :                 WPA_PUT_LE32(pos, conf->r0_key_lifetime * 60);
    2056                 :          8 :                 pos += 4;
    2057                 :            :         }
    2058                 :            : #endif /* CONFIG_IEEE80211R */
    2059                 :            : 
    2060         [ +  + ]:        253 :         wpa_send_eapol(sm->wpa_auth, sm,
    2061                 :            :                        (secure ? WPA_KEY_INFO_SECURE : 0) | WPA_KEY_INFO_MIC |
    2062                 :            :                        WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
    2063                 :            :                        WPA_KEY_INFO_KEY_TYPE,
    2064                 :        506 :                        _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
    2065                 :        253 :         os_free(kde);
    2066                 :            : }
    2067                 :            : 
    2068                 :            : 
    2069                 :        253 : SM_STATE(WPA_PTK, PTKINITDONE)
    2070                 :            : {
    2071 [ -  + ][ #  # ]:        253 :         SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
    2072                 :        253 :         sm->EAPOLKeyReceived = FALSE;
    2073         [ +  - ]:        253 :         if (sm->Pair) {
    2074                 :        253 :                 enum wpa_alg alg = wpa_cipher_to_alg(sm->pairwise);
    2075                 :        253 :                 int klen = wpa_cipher_key_len(sm->pairwise);
    2076         [ -  + ]:        253 :                 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
    2077                 :        253 :                                      sm->PTK.tk1, klen)) {
    2078                 :          0 :                         wpa_sta_disconnect(sm->wpa_auth, sm->addr);
    2079                 :        253 :                         return;
    2080                 :            :                 }
    2081                 :            :                 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
    2082                 :        253 :                 sm->pairwise_set = TRUE;
    2083                 :            : 
    2084         [ -  + ]:        253 :                 if (sm->wpa_auth->conf.wpa_ptk_rekey) {
    2085                 :          0 :                         eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
    2086                 :          0 :                         eloop_register_timeout(sm->wpa_auth->conf.
    2087                 :            :                                                wpa_ptk_rekey, 0, wpa_rekey_ptk,
    2088                 :          0 :                                                sm->wpa_auth, sm);
    2089                 :            :                 }
    2090                 :            : 
    2091         [ +  + ]:        253 :                 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
    2092                 :        166 :                         wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
    2093                 :            :                                            WPA_EAPOL_authorized, 1);
    2094                 :            :                 }
    2095                 :            :         }
    2096                 :            : 
    2097                 :            :         if (0 /* IBSS == TRUE */) {
    2098                 :            :                 sm->keycount++;
    2099                 :            :                 if (sm->keycount == 2) {
    2100                 :            :                         wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
    2101                 :            :                                            WPA_EAPOL_portValid, 1);
    2102                 :            :                 }
    2103                 :            :         } else {
    2104                 :        253 :                 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
    2105                 :            :                                    1);
    2106                 :            :         }
    2107                 :        253 :         wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
    2108                 :        253 :         wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
    2109         [ +  + ]:        253 :         if (sm->wpa == WPA_VERSION_WPA)
    2110                 :          7 :                 sm->PInitAKeys = TRUE;
    2111                 :            :         else
    2112                 :        246 :                 sm->has_GTK = TRUE;
    2113         [ +  + ]:        253 :         wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
    2114                 :            :                          "pairwise key handshake completed (%s)",
    2115                 :        253 :                          sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
    2116                 :            : 
    2117                 :            : #ifdef CONFIG_IEEE80211R
    2118                 :        253 :         wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
    2119                 :            : #endif /* CONFIG_IEEE80211R */
    2120                 :            : }
    2121                 :            : 
    2122                 :            : 
    2123                 :       5296 : SM_STEP(WPA_PTK)
    2124                 :            : {
    2125                 :       5296 :         struct wpa_authenticator *wpa_auth = sm->wpa_auth;
    2126                 :            : 
    2127         [ +  + ]:       5296 :         if (sm->Init)
    2128                 :        232 :                 SM_ENTER(WPA_PTK, INITIALIZE);
    2129         [ -  + ]:       5064 :         else if (sm->Disconnect
    2130                 :            :                  /* || FIX: dot11RSNAConfigSALifetime timeout */) {
    2131                 :          0 :                 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
    2132                 :            :                                 "WPA_PTK: sm->Disconnect");
    2133                 :          0 :                 SM_ENTER(WPA_PTK, DISCONNECT);
    2134                 :            :         }
    2135         [ +  + ]:       5064 :         else if (sm->DeauthenticationRequest)
    2136                 :        184 :                 SM_ENTER(WPA_PTK, DISCONNECTED);
    2137         [ +  + ]:       4880 :         else if (sm->AuthenticationRequest)
    2138                 :        232 :                 SM_ENTER(WPA_PTK, AUTHENTICATION);
    2139         [ +  + ]:       4648 :         else if (sm->ReAuthenticationRequest)
    2140                 :         31 :                 SM_ENTER(WPA_PTK, AUTHENTICATION2);
    2141         [ -  + ]:       4617 :         else if (sm->PTKRequest)
    2142                 :          0 :                 SM_ENTER(WPA_PTK, PTKSTART);
    2143   [ +  -  +  +  :       4617 :         else switch (sm->wpa_ptk_state) {
          +  +  +  +  +  
             +  +  +  - ]
    2144                 :            :         case WPA_PTK_INITIALIZE:
    2145                 :        433 :                 break;
    2146                 :            :         case WPA_PTK_DISCONNECT:
    2147                 :          0 :                 SM_ENTER(WPA_PTK, DISCONNECTED);
    2148                 :          0 :                 break;
    2149                 :            :         case WPA_PTK_DISCONNECTED:
    2150                 :        184 :                 SM_ENTER(WPA_PTK, INITIALIZE);
    2151                 :        184 :                 break;
    2152                 :            :         case WPA_PTK_AUTHENTICATION:
    2153                 :        232 :                 SM_ENTER(WPA_PTK, AUTHENTICATION2);
    2154                 :        232 :                 break;
    2155                 :            :         case WPA_PTK_AUTHENTICATION2:
    2156   [ +  +  +  + ]:       2792 :                 if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
    2157                 :       1306 :                     wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
    2158                 :            :                                        WPA_EAPOL_keyRun) > 0)
    2159                 :         87 :                         SM_ENTER(WPA_PTK, INITPMK);
    2160         [ +  + ]:       1399 :                 else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)
    2161                 :            :                          /* FIX: && 802.1X::keyRun */)
    2162                 :        172 :                         SM_ENTER(WPA_PTK, INITPSK);
    2163                 :       1486 :                 break;
    2164                 :            :         case WPA_PTK_INITPMK:
    2165         [ +  - ]:         87 :                 if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
    2166                 :            :                                        WPA_EAPOL_keyAvailable) > 0)
    2167                 :         87 :                         SM_ENTER(WPA_PTK, PTKSTART);
    2168                 :            :                 else {
    2169                 :          0 :                         wpa_auth->dot11RSNA4WayHandshakeFailures++;
    2170                 :          0 :                         wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
    2171                 :            :                                         "INITPMK - keyAvailable = false");
    2172                 :          0 :                         SM_ENTER(WPA_PTK, DISCONNECT);
    2173                 :            :                 }
    2174                 :         87 :                 break;
    2175                 :            :         case WPA_PTK_INITPSK:
    2176         [ +  - ]:        172 :                 if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr,
    2177                 :            :                                      NULL))
    2178                 :        172 :                         SM_ENTER(WPA_PTK, PTKSTART);
    2179                 :            :                 else {
    2180                 :          0 :                         wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
    2181                 :            :                                         "no PSK configured for the STA");
    2182                 :          0 :                         wpa_auth->dot11RSNA4WayHandshakeFailures++;
    2183                 :          0 :                         SM_ENTER(WPA_PTK, DISCONNECT);
    2184                 :            :                 }
    2185                 :        172 :                 break;
    2186                 :            :         case WPA_PTK_PTKSTART:
    2187 [ +  + ][ +  - ]:        571 :                 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
                 [ +  - ]
    2188                 :        277 :                     sm->EAPOLKeyPairwise)
    2189                 :        277 :                         SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
    2190         [ +  + ]:        294 :                 else if (sm->TimeoutCtr >
    2191                 :        294 :                          (int) dot11RSNAConfigPairwiseUpdateCount) {
    2192                 :          6 :                         wpa_auth->dot11RSNA4WayHandshakeFailures++;
    2193                 :          6 :                         wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
    2194                 :            :                                          "PTKSTART: Retry limit %d reached",
    2195                 :            :                                          dot11RSNAConfigPairwiseUpdateCount);
    2196                 :          6 :                         SM_ENTER(WPA_PTK, DISCONNECT);
    2197         [ -  + ]:        288 :                 } else if (sm->TimeoutEvt)
    2198                 :          0 :                         SM_ENTER(WPA_PTK, PTKSTART);
    2199                 :        571 :                 break;
    2200                 :            :         case WPA_PTK_PTKCALCNEGOTIATING:
    2201         [ +  + ]:        301 :                 if (sm->MICVerified)
    2202                 :        253 :                         SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
    2203 [ -  + ][ #  # ]:         48 :                 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
                 [ #  # ]
    2204                 :          0 :                          sm->EAPOLKeyPairwise)
    2205                 :          0 :                         SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
    2206         [ +  + ]:         48 :                 else if (sm->TimeoutEvt)
    2207                 :         24 :                         SM_ENTER(WPA_PTK, PTKSTART);
    2208                 :        301 :                 break;
    2209                 :            :         case WPA_PTK_PTKCALCNEGOTIATING2:
    2210                 :        253 :                 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
    2211                 :        253 :                 break;
    2212                 :            :         case WPA_PTK_PTKINITNEGOTIATING:
    2213         [ -  + ]:        506 :                 if (sm->update_snonce)
    2214                 :          0 :                         SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
    2215 [ +  + ][ +  - ]:        506 :                 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
                 [ +  - ]
    2216         [ +  - ]:        253 :                          sm->EAPOLKeyPairwise && sm->MICVerified)
    2217                 :        253 :                         SM_ENTER(WPA_PTK, PTKINITDONE);
    2218         [ -  + ]:        253 :                 else if (sm->TimeoutCtr >
    2219                 :        253 :                          (int) dot11RSNAConfigPairwiseUpdateCount) {
    2220                 :          0 :                         wpa_auth->dot11RSNA4WayHandshakeFailures++;
    2221                 :          0 :                         wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
    2222                 :            :                                          "PTKINITNEGOTIATING: Retry limit %d "
    2223                 :            :                                          "reached",
    2224                 :            :                                          dot11RSNAConfigPairwiseUpdateCount);
    2225                 :          0 :                         SM_ENTER(WPA_PTK, DISCONNECT);
    2226         [ -  + ]:        253 :                 } else if (sm->TimeoutEvt)
    2227                 :          0 :                         SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
    2228                 :        506 :                 break;
    2229                 :            :         case WPA_PTK_PTKINITDONE:
    2230                 :        392 :                 break;
    2231                 :            :         }
    2232                 :       5296 : }
    2233                 :            : 
    2234                 :            : 
    2235                 :        240 : SM_STATE(WPA_PTK_GROUP, IDLE)
    2236                 :            : {
    2237 [ -  + ][ #  # ]:        240 :         SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
    2238         [ +  + ]:        240 :         if (sm->Init) {
    2239                 :            :                 /* Init flag is not cleared here, so avoid busy
    2240                 :            :                  * loop by claiming nothing changed. */
    2241                 :        232 :                 sm->changed = FALSE;
    2242                 :            :         }
    2243                 :        240 :         sm->GTimeoutCtr = 0;
    2244                 :        240 : }
    2245                 :            : 
    2246                 :            : 
    2247                 :          8 : SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
    2248                 :            : {
    2249                 :            :         u8 rsc[WPA_KEY_RSC_LEN];
    2250                 :          8 :         struct wpa_group *gsm = sm->group;
    2251                 :            :         u8 *kde, *pos, hdr[2];
    2252                 :            :         size_t kde_len;
    2253                 :            :         u8 *gtk, dummy_gtk[32];
    2254                 :            : 
    2255 [ -  + ][ #  # ]:          8 :         SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
    2256                 :            : 
    2257                 :          8 :         sm->GTimeoutCtr++;
    2258         [ -  + ]:          8 :         if (sm->GTimeoutCtr > (int) dot11RSNAConfigGroupUpdateCount) {
    2259                 :            :                 /* No point in sending the EAPOL-Key - we will disconnect
    2260                 :            :                  * immediately following this. */
    2261                 :          0 :                 return;
    2262                 :            :         }
    2263                 :            : 
    2264         [ +  + ]:          8 :         if (sm->wpa == WPA_VERSION_WPA)
    2265                 :          7 :                 sm->PInitAKeys = FALSE;
    2266                 :          8 :         sm->TimeoutEvt = FALSE;
    2267                 :            :         /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
    2268                 :          8 :         os_memset(rsc, 0, WPA_KEY_RSC_LEN);
    2269         [ +  - ]:          8 :         if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
    2270                 :          8 :                 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
    2271                 :          8 :         wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
    2272                 :            :                         "sending 1/2 msg of Group Key Handshake");
    2273                 :            : 
    2274                 :          8 :         gtk = gsm->GTK[gsm->GN - 1];
    2275         [ -  + ]:          8 :         if (sm->wpa_auth->conf.disable_gtk) {
    2276                 :            :                 /*
    2277                 :            :                  * Provide unique random GTK to each STA to prevent use
    2278                 :            :                  * of GTK in the BSS.
    2279                 :            :                  */
    2280         [ #  # ]:          0 :                 if (random_get_bytes(dummy_gtk, gsm->GTK_len) < 0)
    2281                 :          0 :                         return;
    2282                 :          0 :                 gtk = dummy_gtk;
    2283                 :            :         }
    2284         [ +  + ]:          8 :         if (sm->wpa == WPA_VERSION_WPA2) {
    2285                 :          2 :                 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
    2286                 :          1 :                         ieee80211w_kde_len(sm);
    2287                 :          1 :                 kde = os_malloc(kde_len);
    2288         [ -  + ]:          1 :                 if (kde == NULL)
    2289                 :          0 :                         return;
    2290                 :            : 
    2291                 :          1 :                 pos = kde;
    2292                 :          1 :                 hdr[0] = gsm->GN & 0x03;
    2293                 :          1 :                 hdr[1] = 0;
    2294                 :          1 :                 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
    2295                 :          1 :                                   gtk, gsm->GTK_len);
    2296                 :          1 :                 pos = ieee80211w_kde_add(sm, pos);
    2297                 :            :         } else {
    2298                 :          7 :                 kde = gtk;
    2299                 :          7 :                 pos = kde + gsm->GTK_len;
    2300                 :            :         }
    2301                 :            : 
    2302         [ -  + ]:          8 :         wpa_send_eapol(sm->wpa_auth, sm,
    2303                 :            :                        WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
    2304                 :            :                        WPA_KEY_INFO_ACK |
    2305                 :          8 :                        (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
    2306                 :         16 :                        rsc, gsm->GNonce, kde, pos - kde, gsm->GN, 1);
    2307         [ +  + ]:          8 :         if (sm->wpa == WPA_VERSION_WPA2)
    2308                 :          8 :                 os_free(kde);
    2309                 :            : }
    2310                 :            : 
    2311                 :            : 
    2312                 :          8 : SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
    2313                 :            : {
    2314 [ -  + ][ #  # ]:          8 :         SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
    2315                 :          8 :         sm->EAPOLKeyReceived = FALSE;
    2316         [ +  + ]:          8 :         if (sm->GUpdateStationKeys)
    2317                 :          1 :                 sm->group->GKeyDoneStations--;
    2318                 :          8 :         sm->GUpdateStationKeys = FALSE;
    2319                 :          8 :         sm->GTimeoutCtr = 0;
    2320                 :            :         /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
    2321         [ +  + ]:          8 :         wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
    2322                 :            :                          "group key handshake completed (%s)",
    2323                 :          8 :                          sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
    2324                 :          8 :         sm->has_GTK = TRUE;
    2325                 :          8 : }
    2326                 :            : 
    2327                 :            : 
    2328                 :          0 : SM_STATE(WPA_PTK_GROUP, KEYERROR)
    2329                 :            : {
    2330 [ #  # ][ #  # ]:          0 :         SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
    2331         [ #  # ]:          0 :         if (sm->GUpdateStationKeys)
    2332                 :          0 :                 sm->group->GKeyDoneStations--;
    2333                 :          0 :         sm->GUpdateStationKeys = FALSE;
    2334                 :          0 :         sm->Disconnect = TRUE;
    2335                 :          0 : }
    2336                 :            : 
    2337                 :            : 
    2338                 :       5296 : SM_STEP(WPA_PTK_GROUP)
    2339                 :            : {
    2340 [ +  + ][ -  + ]:       5296 :         if (sm->Init || sm->PtkGroupInit) {
    2341                 :        232 :                 SM_ENTER(WPA_PTK_GROUP, IDLE);
    2342                 :        232 :                 sm->PtkGroupInit = FALSE;
    2343   [ +  +  -  +  :       5064 :         } else switch (sm->wpa_ptk_group_state) {
                      - ]
    2344                 :            :         case WPA_PTK_GROUP_IDLE:
    2345 [ +  + ][ +  + ]:       5038 :                 if (sm->GUpdateStationKeys ||
    2346         [ +  + ]:        146 :                     (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
    2347                 :          8 :                         SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
    2348                 :       5038 :                 break;
    2349                 :            :         case WPA_PTK_GROUP_REKEYNEGOTIATING:
    2350 [ +  + ][ +  - ]:         18 :                 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
                 [ +  - ]
    2351         [ +  - ]:          8 :                     !sm->EAPOLKeyPairwise && sm->MICVerified)
    2352                 :          8 :                         SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
    2353         [ -  + ]:         10 :                 else if (sm->GTimeoutCtr >
    2354                 :         10 :                          (int) dot11RSNAConfigGroupUpdateCount)
    2355                 :          0 :                         SM_ENTER(WPA_PTK_GROUP, KEYERROR);
    2356         [ -  + ]:         10 :                 else if (sm->TimeoutEvt)
    2357                 :          0 :                         SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
    2358                 :         18 :                 break;
    2359                 :            :         case WPA_PTK_GROUP_KEYERROR:
    2360                 :          0 :                 SM_ENTER(WPA_PTK_GROUP, IDLE);
    2361                 :          0 :                 break;
    2362                 :            :         case WPA_PTK_GROUP_REKEYESTABLISHED:
    2363                 :          8 :                 SM_ENTER(WPA_PTK_GROUP, IDLE);
    2364                 :          8 :                 break;
    2365                 :            :         }
    2366                 :       5296 : }
    2367                 :            : 
    2368                 :            : 
    2369                 :        396 : static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
    2370                 :            :                           struct wpa_group *group)
    2371                 :            : {
    2372                 :        396 :         int ret = 0;
    2373                 :            : 
    2374                 :        396 :         os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
    2375                 :        396 :         inc_byte_array(group->Counter, WPA_NONCE_LEN);
    2376         [ -  + ]:        396 :         if (wpa_gmk_to_gtk(group->GMK, "Group key expansion",
    2377                 :        396 :                            wpa_auth->addr, group->GNonce,
    2378                 :        792 :                            group->GTK[group->GN - 1], group->GTK_len) < 0)
    2379                 :          0 :                 ret = -1;
    2380                 :        396 :         wpa_hexdump_key(MSG_DEBUG, "GTK",
    2381                 :        792 :                         group->GTK[group->GN - 1], group->GTK_len);
    2382                 :            : 
    2383                 :            : #ifdef CONFIG_IEEE80211W
    2384         [ +  + ]:        396 :         if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
    2385                 :         86 :                 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
    2386                 :         86 :                 inc_byte_array(group->Counter, WPA_NONCE_LEN);
    2387         [ -  + ]:         86 :                 if (wpa_gmk_to_gtk(group->GMK, "IGTK key expansion",
    2388                 :         86 :                                    wpa_auth->addr, group->GNonce,
    2389                 :         86 :                                    group->IGTK[group->GN_igtk - 4],
    2390                 :            :                                    WPA_IGTK_LEN) < 0)
    2391                 :          0 :                         ret = -1;
    2392                 :         86 :                 wpa_hexdump_key(MSG_DEBUG, "IGTK",
    2393                 :         86 :                                 group->IGTK[group->GN_igtk - 4], WPA_IGTK_LEN);
    2394                 :            :         }
    2395                 :            : #endif /* CONFIG_IEEE80211W */
    2396                 :            : 
    2397                 :        396 :         return ret;
    2398                 :            : }
    2399                 :            : 
    2400                 :            : 
    2401                 :        216 : static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
    2402                 :            :                                struct wpa_group *group)
    2403                 :            : {
    2404                 :        216 :         wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
    2405                 :            :                    "GTK_INIT (VLAN-ID %d)", group->vlan_id);
    2406                 :        216 :         group->changed = FALSE; /* GInit is not cleared here; avoid loop */
    2407                 :        216 :         group->wpa_group_state = WPA_GROUP_GTK_INIT;
    2408                 :            : 
    2409                 :            :         /* GTK[0..N] = 0 */
    2410                 :        216 :         os_memset(group->GTK, 0, sizeof(group->GTK));
    2411                 :        216 :         group->GN = 1;
    2412                 :        216 :         group->GM = 2;
    2413                 :            : #ifdef CONFIG_IEEE80211W
    2414                 :        216 :         group->GN_igtk = 4;
    2415                 :        216 :         group->GM_igtk = 5;
    2416                 :            : #endif /* CONFIG_IEEE80211W */
    2417                 :            :         /* GTK[GN] = CalcGTK() */
    2418                 :        216 :         wpa_gtk_update(wpa_auth, group);
    2419                 :        216 : }
    2420                 :            : 
    2421                 :            : 
    2422                 :          1 : static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
    2423                 :            : {
    2424 [ -  + ][ #  # ]:          1 :         if (ctx != NULL && ctx != sm->group)
    2425                 :          0 :                 return 0;
    2426                 :            : 
    2427         [ -  + ]:          1 :         if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
    2428                 :          0 :                 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
    2429                 :            :                                 "Not in PTKINITDONE; skip Group Key update");
    2430                 :          0 :                 sm->GUpdateStationKeys = FALSE;
    2431                 :          0 :                 return 0;
    2432                 :            :         }
    2433         [ -  + ]:          1 :         if (sm->GUpdateStationKeys) {
    2434                 :            :                 /*
    2435                 :            :                  * This should not really happen, so add a debug log entry.
    2436                 :            :                  * Since we clear the GKeyDoneStations before the loop, the
    2437                 :            :                  * station needs to be counted here anyway.
    2438                 :            :                  */
    2439                 :          0 :                 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
    2440                 :            :                                 "GUpdateStationKeys was already set when "
    2441                 :            :                                 "marking station for GTK rekeying");
    2442                 :            :         }
    2443                 :            : 
    2444                 :            :         /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
    2445         [ -  + ]:          1 :         if (sm->is_wnmsleep)
    2446                 :          0 :                 return 0;
    2447                 :            : 
    2448                 :          1 :         sm->group->GKeyDoneStations++;
    2449                 :          1 :         sm->GUpdateStationKeys = TRUE;
    2450                 :            : 
    2451                 :          1 :         wpa_sm_step(sm);
    2452                 :          1 :         return 0;
    2453                 :            : }
    2454                 :            : 
    2455                 :            : 
    2456                 :            : #ifdef CONFIG_WNM
    2457                 :            : /* update GTK when exiting WNM-Sleep Mode */
    2458                 :          2 : void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine *sm)
    2459                 :            : {
    2460 [ +  + ][ -  + ]:          2 :         if (sm == NULL || sm->is_wnmsleep)
    2461                 :          2 :                 return;
    2462                 :            : 
    2463                 :          1 :         wpa_group_update_sta(sm, NULL);
    2464                 :            : }
    2465                 :            : 
    2466                 :            : 
    2467                 :          6 : void wpa_set_wnmsleep(struct wpa_state_machine *sm, int flag)
    2468                 :            : {
    2469         [ +  + ]:          6 :         if (sm)
    2470                 :          4 :                 sm->is_wnmsleep = !!flag;
    2471                 :          6 : }
    2472                 :            : 
    2473                 :            : 
    2474                 :          1 : int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine *sm, u8 *pos)
    2475                 :            : {
    2476                 :          1 :         struct wpa_group *gsm = sm->group;
    2477                 :          1 :         u8 *start = pos;
    2478                 :            : 
    2479                 :            :         /*
    2480                 :            :          * GTK subelement:
    2481                 :            :          * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
    2482                 :            :          * Key[5..32]
    2483                 :            :          */
    2484                 :          1 :         *pos++ = WNM_SLEEP_SUBELEM_GTK;
    2485                 :          1 :         *pos++ = 11 + gsm->GTK_len;
    2486                 :            :         /* Key ID in B0-B1 of Key Info */
    2487                 :          1 :         WPA_PUT_LE16(pos, gsm->GN & 0x03);
    2488                 :          1 :         pos += 2;
    2489                 :          1 :         *pos++ = gsm->GTK_len;
    2490         [ -  + ]:          1 :         if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, pos) != 0)
    2491                 :          0 :                 return 0;
    2492                 :          1 :         pos += 8;
    2493                 :          1 :         os_memcpy(pos, gsm->GTK[gsm->GN - 1], gsm->GTK_len);
    2494                 :          1 :         pos += gsm->GTK_len;
    2495                 :            : 
    2496                 :          1 :         wpa_printf(MSG_DEBUG, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
    2497                 :            :                    gsm->GN);
    2498                 :          1 :         wpa_hexdump_key(MSG_DEBUG, "WNM: GTK in WNM-Sleep Mode exit",
    2499                 :          2 :                         gsm->GTK[gsm->GN - 1], gsm->GTK_len);
    2500                 :            : 
    2501                 :          1 :         return pos - start;
    2502                 :            : }
    2503                 :            : 
    2504                 :            : 
    2505                 :            : #ifdef CONFIG_IEEE80211W
    2506                 :          1 : int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine *sm, u8 *pos)
    2507                 :            : {
    2508                 :          1 :         struct wpa_group *gsm = sm->group;
    2509                 :          1 :         u8 *start = pos;
    2510                 :            : 
    2511                 :            :         /*
    2512                 :            :          * IGTK subelement:
    2513                 :            :          * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
    2514                 :            :          */
    2515                 :          1 :         *pos++ = WNM_SLEEP_SUBELEM_IGTK;
    2516                 :          1 :         *pos++ = 2 + 6 + WPA_IGTK_LEN;
    2517                 :          1 :         WPA_PUT_LE16(pos, gsm->GN_igtk);
    2518                 :          1 :         pos += 2;
    2519         [ -  + ]:          1 :         if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos) != 0)
    2520                 :          0 :                 return 0;
    2521                 :          1 :         pos += 6;
    2522                 :            : 
    2523                 :          1 :         os_memcpy(pos, gsm->IGTK[gsm->GN_igtk - 4], WPA_IGTK_LEN);
    2524                 :          1 :         pos += WPA_IGTK_LEN;
    2525                 :            : 
    2526                 :          1 :         wpa_printf(MSG_DEBUG, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
    2527                 :            :                    gsm->GN_igtk);
    2528                 :          1 :         wpa_hexdump_key(MSG_DEBUG, "WNM: IGTK in WNM-Sleep Mode exit",
    2529                 :          1 :                         gsm->IGTK[gsm->GN_igtk - 4], WPA_IGTK_LEN);
    2530                 :            : 
    2531                 :          1 :         return pos - start;
    2532                 :            : }
    2533                 :            : #endif /* CONFIG_IEEE80211W */
    2534                 :            : #endif /* CONFIG_WNM */
    2535                 :            : 
    2536                 :            : 
    2537                 :          0 : static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
    2538                 :            :                               struct wpa_group *group)
    2539                 :            : {
    2540                 :            :         int tmp;
    2541                 :            : 
    2542                 :          0 :         wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
    2543                 :            :                    "SETKEYS (VLAN-ID %d)", group->vlan_id);
    2544                 :          0 :         group->changed = TRUE;
    2545                 :          0 :         group->wpa_group_state = WPA_GROUP_SETKEYS;
    2546                 :          0 :         group->GTKReKey = FALSE;
    2547                 :          0 :         tmp = group->GM;
    2548                 :          0 :         group->GM = group->GN;
    2549                 :          0 :         group->GN = tmp;
    2550                 :            : #ifdef CONFIG_IEEE80211W
    2551                 :          0 :         tmp = group->GM_igtk;
    2552                 :          0 :         group->GM_igtk = group->GN_igtk;
    2553                 :          0 :         group->GN_igtk = tmp;
    2554                 :            : #endif /* CONFIG_IEEE80211W */
    2555                 :            :         /* "GKeyDoneStations = GNoStations" is done in more robust way by
    2556                 :            :          * counting the STAs that are marked with GUpdateStationKeys instead of
    2557                 :            :          * including all STAs that could be in not-yet-completed state. */
    2558                 :          0 :         wpa_gtk_update(wpa_auth, group);
    2559                 :            : 
    2560         [ #  # ]:          0 :         if (group->GKeyDoneStations) {
    2561                 :          0 :                 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: Unexpected "
    2562                 :            :                            "GKeyDoneStations=%d when starting new GTK rekey",
    2563                 :            :                            group->GKeyDoneStations);
    2564                 :          0 :                 group->GKeyDoneStations = 0;
    2565                 :            :         }
    2566                 :          0 :         wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, group);
    2567                 :          0 :         wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
    2568                 :            :                    group->GKeyDoneStations);
    2569                 :          0 : }
    2570                 :            : 
    2571                 :            : 
    2572                 :        396 : static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
    2573                 :            :                                        struct wpa_group *group)
    2574                 :            : {
    2575                 :        396 :         int ret = 0;
    2576                 :            : 
    2577         [ -  + ]:        396 :         if (wpa_auth_set_key(wpa_auth, group->vlan_id,
    2578                 :        396 :                              wpa_cipher_to_alg(wpa_auth->conf.wpa_group),
    2579                 :            :                              broadcast_ether_addr, group->GN,
    2580                 :        792 :                              group->GTK[group->GN - 1], group->GTK_len) < 0)
    2581                 :          0 :                 ret = -1;
    2582                 :            : 
    2583                 :            : #ifdef CONFIG_IEEE80211W
    2584   [ +  +  -  + ]:        482 :         if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION &&
    2585                 :         86 :             wpa_auth_set_key(wpa_auth, group->vlan_id, WPA_ALG_IGTK,
    2586                 :            :                              broadcast_ether_addr, group->GN_igtk,
    2587                 :         86 :                              group->IGTK[group->GN_igtk - 4],
    2588                 :            :                              WPA_IGTK_LEN) < 0)
    2589                 :          0 :                 ret = -1;
    2590                 :            : #endif /* CONFIG_IEEE80211W */
    2591                 :            : 
    2592                 :        396 :         return ret;
    2593                 :            : }
    2594                 :            : 
    2595                 :            : 
    2596                 :          0 : static int wpa_group_disconnect_cb(struct wpa_state_machine *sm, void *ctx)
    2597                 :            : {
    2598         [ #  # ]:          0 :         if (sm->group == ctx) {
    2599                 :          0 :                 wpa_printf(MSG_DEBUG, "WPA: Mark STA " MACSTR
    2600                 :            :                            " for discconnection due to fatal failure",
    2601                 :          0 :                            MAC2STR(sm->addr));
    2602                 :          0 :                 sm->Disconnect = TRUE;
    2603                 :            :         }
    2604                 :            : 
    2605                 :          0 :         return 0;
    2606                 :            : }
    2607                 :            : 
    2608                 :            : 
    2609                 :          0 : static void wpa_group_fatal_failure(struct wpa_authenticator *wpa_auth,
    2610                 :            :                                     struct wpa_group *group)
    2611                 :            : {
    2612                 :          0 :         wpa_printf(MSG_DEBUG, "WPA: group state machine entering state FATAL_FAILURE");
    2613                 :          0 :         group->changed = TRUE;
    2614                 :          0 :         group->wpa_group_state = WPA_GROUP_FATAL_FAILURE;
    2615                 :          0 :         wpa_auth_for_each_sta(wpa_auth, wpa_group_disconnect_cb, group);
    2616                 :          0 : }
    2617                 :            : 
    2618                 :            : 
    2619                 :        216 : static int wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
    2620                 :            :                                  struct wpa_group *group)
    2621                 :            : {
    2622                 :        216 :         wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
    2623                 :            :                    "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
    2624                 :        216 :         group->changed = TRUE;
    2625                 :        216 :         group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
    2626                 :            : 
    2627         [ -  + ]:        216 :         if (wpa_group_config_group_keys(wpa_auth, group) < 0) {
    2628                 :          0 :                 wpa_group_fatal_failure(wpa_auth, group);
    2629                 :          0 :                 return -1;
    2630                 :            :         }
    2631                 :            : 
    2632                 :        216 :         return 0;
    2633                 :            : }
    2634                 :            : 
    2635                 :            : 
    2636                 :       5728 : static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
    2637                 :            :                               struct wpa_group *group)
    2638                 :            : {
    2639         [ +  + ]:       5728 :         if (group->GInit) {
    2640                 :        216 :                 wpa_group_gtk_init(wpa_auth, group);
    2641         [ +  - ]:       5512 :         } else if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) {
    2642                 :            :                 /* Do not allow group operations */
    2643 [ +  + ][ +  - ]:       5512 :         } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
    2644                 :        216 :                    group->GTKAuthenticator) {
    2645                 :        216 :                 wpa_group_setkeysdone(wpa_auth, group);
    2646 [ +  - ][ -  + ]:       5296 :         } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
    2647                 :       5296 :                    group->GTKReKey) {
    2648                 :          0 :                 wpa_group_setkeys(wpa_auth, group);
    2649         [ -  + ]:       5296 :         } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
    2650         [ #  # ]:          0 :                 if (group->GKeyDoneStations == 0)
    2651                 :          0 :                         wpa_group_setkeysdone(wpa_auth, group);
    2652         [ #  # ]:          0 :                 else if (group->GTKReKey)
    2653                 :          0 :                         wpa_group_setkeys(wpa_auth, group);
    2654                 :            :         }
    2655                 :       5728 : }
    2656                 :            : 
    2657                 :            : 
    2658                 :       2838 : static int wpa_sm_step(struct wpa_state_machine *sm)
    2659                 :            : {
    2660         [ -  + ]:       2838 :         if (sm == NULL)
    2661                 :          0 :                 return 0;
    2662                 :            : 
    2663         [ +  + ]:       2838 :         if (sm->in_step_loop) {
    2664                 :            :                 /* This should not happen, but if it does, make sure we do not
    2665                 :            :                  * end up freeing the state machine too early by exiting the
    2666                 :            :                  * recursive call. */
    2667                 :          6 :                 wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
    2668                 :          6 :                 return 0;
    2669                 :            :         }
    2670                 :            : 
    2671                 :       2832 :         sm->in_step_loop = 1;
    2672                 :            :         do {
    2673         [ -  + ]:       5296 :                 if (sm->pending_deinit)
    2674                 :          0 :                         break;
    2675                 :            : 
    2676                 :       5296 :                 sm->changed = FALSE;
    2677                 :       5296 :                 sm->wpa_auth->group->changed = FALSE;
    2678                 :            : 
    2679                 :       5296 :                 SM_STEP_RUN(WPA_PTK);
    2680         [ -  + ]:       5296 :                 if (sm->pending_deinit)
    2681                 :          0 :                         break;
    2682                 :       5296 :                 SM_STEP_RUN(WPA_PTK_GROUP);
    2683         [ -  + ]:       5296 :                 if (sm->pending_deinit)
    2684                 :          0 :                         break;
    2685                 :       5296 :                 wpa_group_sm_step(sm->wpa_auth, sm->group);
    2686 [ -  + ][ +  + ]:       5296 :         } while (sm->changed || sm->wpa_auth->group->changed);
    2687                 :       2832 :         sm->in_step_loop = 0;
    2688                 :            : 
    2689         [ -  + ]:       2832 :         if (sm->pending_deinit) {
    2690                 :          0 :                 wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
    2691                 :          0 :                            "machine deinit for " MACSTR, MAC2STR(sm->addr));
    2692                 :          0 :                 wpa_free_sta_sm(sm);
    2693                 :          0 :                 return 1;
    2694                 :            :         }
    2695                 :       2838 :         return 0;
    2696                 :            : }
    2697                 :            : 
    2698                 :            : 
    2699                 :       1345 : static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
    2700                 :            : {
    2701                 :       1345 :         struct wpa_state_machine *sm = eloop_ctx;
    2702                 :       1345 :         wpa_sm_step(sm);
    2703                 :       1345 : }
    2704                 :            : 
    2705                 :            : 
    2706                 :       2124 : void wpa_auth_sm_notify(struct wpa_state_machine *sm)
    2707                 :            : {
    2708         [ +  + ]:       2124 :         if (sm == NULL)
    2709                 :       2124 :                 return;
    2710                 :       1349 :         eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
    2711                 :            : }
    2712                 :            : 
    2713                 :            : 
    2714                 :          0 : void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
    2715                 :            : {
    2716                 :            :         int tmp, i;
    2717                 :            :         struct wpa_group *group;
    2718                 :            : 
    2719         [ #  # ]:          0 :         if (wpa_auth == NULL)
    2720                 :          0 :                 return;
    2721                 :            : 
    2722                 :          0 :         group = wpa_auth->group;
    2723                 :            : 
    2724         [ #  # ]:          0 :         for (i = 0; i < 2; i++) {
    2725                 :          0 :                 tmp = group->GM;
    2726                 :          0 :                 group->GM = group->GN;
    2727                 :          0 :                 group->GN = tmp;
    2728                 :            : #ifdef CONFIG_IEEE80211W
    2729                 :          0 :                 tmp = group->GM_igtk;
    2730                 :          0 :                 group->GM_igtk = group->GN_igtk;
    2731                 :          0 :                 group->GN_igtk = tmp;
    2732                 :            : #endif /* CONFIG_IEEE80211W */
    2733                 :          0 :                 wpa_gtk_update(wpa_auth, group);
    2734                 :          0 :                 wpa_group_config_group_keys(wpa_auth, group);
    2735                 :            :         }
    2736                 :            : }
    2737                 :            : 
    2738                 :            : 
    2739                 :          0 : static const char * wpa_bool_txt(int bool)
    2740                 :            : {
    2741         [ #  # ]:          0 :         return bool ? "TRUE" : "FALSE";
    2742                 :            : }
    2743                 :            : 
    2744                 :            : 
    2745                 :            : #define RSN_SUITE "%02x-%02x-%02x-%d"
    2746                 :            : #define RSN_SUITE_ARG(s) \
    2747                 :            : ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
    2748                 :            : 
    2749                 :          0 : int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
    2750                 :            : {
    2751                 :          0 :         int len = 0, ret;
    2752                 :            :         char pmkid_txt[PMKID_LEN * 2 + 1];
    2753                 :            : #ifdef CONFIG_RSN_PREAUTH
    2754                 :          0 :         const int preauth = 1;
    2755                 :            : #else /* CONFIG_RSN_PREAUTH */
    2756                 :          0 :         const int preauth = 0;
    2757                 :            : #endif /* CONFIG_RSN_PREAUTH */
    2758                 :            : 
    2759         [ #  # ]:          0 :         if (wpa_auth == NULL)
    2760                 :          0 :                 return len;
    2761                 :            : 
    2762                 :          0 :         ret = os_snprintf(buf + len, buflen - len,
    2763                 :            :                           "dot11RSNAOptionImplemented=TRUE\n"
    2764                 :            :                           "dot11RSNAPreauthenticationImplemented=%s\n"
    2765                 :            :                           "dot11RSNAEnabled=%s\n"
    2766                 :            :                           "dot11RSNAPreauthenticationEnabled=%s\n",
    2767                 :            :                           wpa_bool_txt(preauth),
    2768                 :          0 :                           wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
    2769                 :            :                           wpa_bool_txt(wpa_auth->conf.rsn_preauth));
    2770 [ #  # ][ #  # ]:          0 :         if (ret < 0 || (size_t) ret >= buflen - len)
    2771                 :          0 :                 return len;
    2772                 :          0 :         len += ret;
    2773                 :            : 
    2774                 :          0 :         wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
    2775                 :          0 :                          wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
    2776                 :            : 
    2777                 :          0 :         ret = os_snprintf(
    2778                 :          0 :                 buf + len, buflen - len,
    2779                 :            :                 "dot11RSNAConfigVersion=%u\n"
    2780                 :            :                 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
    2781                 :            :                 /* FIX: dot11RSNAConfigGroupCipher */
    2782                 :            :                 /* FIX: dot11RSNAConfigGroupRekeyMethod */
    2783                 :            :                 /* FIX: dot11RSNAConfigGroupRekeyTime */
    2784                 :            :                 /* FIX: dot11RSNAConfigGroupRekeyPackets */
    2785                 :            :                 "dot11RSNAConfigGroupRekeyStrict=%u\n"
    2786                 :            :                 "dot11RSNAConfigGroupUpdateCount=%u\n"
    2787                 :            :                 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
    2788                 :            :                 "dot11RSNAConfigGroupCipherSize=%u\n"
    2789                 :            :                 "dot11RSNAConfigPMKLifetime=%u\n"
    2790                 :            :                 "dot11RSNAConfigPMKReauthThreshold=%u\n"
    2791                 :            :                 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
    2792                 :            :                 "dot11RSNAConfigSATimeout=%u\n"
    2793                 :            :                 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
    2794                 :            :                 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
    2795                 :            :                 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
    2796                 :            :                 "dot11RSNAPMKIDUsed=%s\n"
    2797                 :            :                 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
    2798                 :            :                 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
    2799                 :            :                 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
    2800                 :            :                 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
    2801                 :            :                 "dot11RSNA4WayHandshakeFailures=%u\n"
    2802                 :            :                 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
    2803                 :            :                 RSN_VERSION,
    2804                 :          0 :                 !!wpa_auth->conf.wpa_strict_rekey,
    2805                 :            :                 dot11RSNAConfigGroupUpdateCount,
    2806                 :            :                 dot11RSNAConfigPairwiseUpdateCount,
    2807                 :          0 :                 wpa_cipher_key_len(wpa_auth->conf.wpa_group) * 8,
    2808                 :            :                 dot11RSNAConfigPMKLifetime,
    2809                 :            :                 dot11RSNAConfigPMKReauthThreshold,
    2810                 :            :                 dot11RSNAConfigSATimeout,
    2811                 :          0 :                 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
    2812                 :          0 :                 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
    2813                 :          0 :                 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
    2814                 :            :                 pmkid_txt,
    2815                 :          0 :                 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
    2816                 :          0 :                 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
    2817                 :          0 :                 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
    2818                 :            :                 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
    2819                 :            :                 wpa_auth->dot11RSNA4WayHandshakeFailures);
    2820 [ #  # ][ #  # ]:          0 :         if (ret < 0 || (size_t) ret >= buflen - len)
    2821                 :          0 :                 return len;
    2822                 :          0 :         len += ret;
    2823                 :            : 
    2824                 :            :         /* TODO: dot11RSNAConfigPairwiseCiphersTable */
    2825                 :            :         /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
    2826                 :            : 
    2827                 :            :         /* Private MIB */
    2828                 :          0 :         ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
    2829                 :          0 :                           wpa_auth->group->wpa_group_state);
    2830 [ #  # ][ #  # ]:          0 :         if (ret < 0 || (size_t) ret >= buflen - len)
    2831                 :          0 :                 return len;
    2832                 :          0 :         len += ret;
    2833                 :            : 
    2834                 :          0 :         return len;
    2835                 :            : }
    2836                 :            : 
    2837                 :            : 
    2838                 :         10 : int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
    2839                 :            : {
    2840                 :         10 :         int len = 0, ret;
    2841                 :         10 :         u32 pairwise = 0;
    2842                 :            : 
    2843         [ +  + ]:         10 :         if (sm == NULL)
    2844                 :          3 :                 return 0;
    2845                 :            : 
    2846                 :            :         /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
    2847                 :            : 
    2848                 :            :         /* dot11RSNAStatsEntry */
    2849                 :            : 
    2850         [ +  - ]:          7 :         pairwise = wpa_cipher_to_suite(sm->wpa == WPA_VERSION_WPA2 ?
    2851                 :            :                                        WPA_PROTO_RSN : WPA_PROTO_WPA,
    2852                 :            :                                        sm->pairwise);
    2853         [ -  + ]:          7 :         if (pairwise == 0)
    2854                 :          0 :                 return 0;
    2855                 :            : 
    2856                 :         70 :         ret = os_snprintf(
    2857                 :          7 :                 buf + len, buflen - len,
    2858                 :            :                 /* TODO: dot11RSNAStatsIndex */
    2859                 :            :                 "dot11RSNAStatsSTAAddress=" MACSTR "\n"
    2860                 :            :                 "dot11RSNAStatsVersion=1\n"
    2861                 :            :                 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
    2862                 :            :                 /* TODO: dot11RSNAStatsTKIPICVErrors */
    2863                 :            :                 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
    2864                 :            :                 "dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
    2865                 :            :                 /* TODO: dot11RSNAStatsCCMPReplays */
    2866                 :            :                 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
    2867                 :            :                 /* TODO: dot11RSNAStatsTKIPReplays */,
    2868                 :         42 :                 MAC2STR(sm->addr),
    2869                 :         14 :                 RSN_SUITE_ARG(pairwise),
    2870                 :            :                 sm->dot11RSNAStatsTKIPLocalMICFailures,
    2871                 :            :                 sm->dot11RSNAStatsTKIPRemoteMICFailures);
    2872 [ +  - ][ -  + ]:          7 :         if (ret < 0 || (size_t) ret >= buflen - len)
    2873                 :          0 :                 return len;
    2874                 :          7 :         len += ret;
    2875                 :            : 
    2876                 :            :         /* Private MIB */
    2877                 :          7 :         ret = os_snprintf(buf + len, buflen - len,
    2878                 :            :                           "hostapdWPAPTKState=%d\n"
    2879                 :            :                           "hostapdWPAPTKGroupState=%d\n",
    2880                 :          7 :                           sm->wpa_ptk_state,
    2881                 :          7 :                           sm->wpa_ptk_group_state);
    2882 [ +  - ][ -  + ]:          7 :         if (ret < 0 || (size_t) ret >= buflen - len)
    2883                 :          0 :                 return len;
    2884                 :          7 :         len += ret;
    2885                 :            : 
    2886                 :         10 :         return len;
    2887                 :            : }
    2888                 :            : 
    2889                 :            : 
    2890                 :          0 : void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
    2891                 :            : {
    2892         [ #  # ]:          0 :         if (wpa_auth)
    2893                 :          0 :                 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
    2894                 :          0 : }
    2895                 :            : 
    2896                 :            : 
    2897                 :       1138 : int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
    2898                 :            : {
    2899 [ +  + ][ +  + ]:       1138 :         return sm && sm->pairwise_set;
    2900                 :            : }
    2901                 :            : 
    2902                 :            : 
    2903                 :         77 : int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
    2904                 :            : {
    2905                 :         77 :         return sm->pairwise;
    2906                 :            : }
    2907                 :            : 
    2908                 :            : 
    2909                 :       1145 : int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
    2910                 :            : {
    2911         [ +  + ]:       1145 :         if (sm == NULL)
    2912                 :        605 :                 return -1;
    2913                 :       1145 :         return sm->wpa_key_mgmt;
    2914                 :            : }
    2915                 :            : 
    2916                 :            : 
    2917                 :          0 : int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
    2918                 :            : {
    2919         [ #  # ]:          0 :         if (sm == NULL)
    2920                 :          0 :                 return 0;
    2921                 :          0 :         return sm->wpa;
    2922                 :            : }
    2923                 :            : 
    2924                 :            : 
    2925                 :          0 : int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
    2926                 :            :                              struct rsn_pmksa_cache_entry *entry)
    2927                 :            : {
    2928 [ #  # ][ #  # ]:          0 :         if (sm == NULL || sm->pmksa != entry)
    2929                 :          0 :                 return -1;
    2930                 :          0 :         sm->pmksa = NULL;
    2931                 :          0 :         return 0;
    2932                 :            : }
    2933                 :            : 
    2934                 :            : 
    2935                 :            : struct rsn_pmksa_cache_entry *
    2936                 :        248 : wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
    2937                 :            : {
    2938         [ +  + ]:        248 :         return sm ? sm->pmksa : NULL;
    2939                 :            : }
    2940                 :            : 
    2941                 :            : 
    2942                 :          0 : void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
    2943                 :            : {
    2944         [ #  # ]:          0 :         if (sm)
    2945                 :          0 :                 sm->dot11RSNAStatsTKIPLocalMICFailures++;
    2946                 :          0 : }
    2947                 :            : 
    2948                 :            : 
    2949                 :       2078 : const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
    2950                 :            : {
    2951         [ +  + ]:       2078 :         if (wpa_auth == NULL)
    2952                 :        489 :                 return NULL;
    2953                 :       1589 :         *len = wpa_auth->wpa_ie_len;
    2954                 :       2078 :         return wpa_auth->wpa_ie;
    2955                 :            : }
    2956                 :            : 
    2957                 :            : 
    2958                 :         82 : int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
    2959                 :            :                        int session_timeout, struct eapol_state_machine *eapol)
    2960                 :            : {
    2961 [ +  - ][ +  + ]:         82 :         if (sm == NULL || sm->wpa != WPA_VERSION_WPA2 ||
                 [ -  + ]
    2962                 :         80 :             sm->wpa_auth->conf.disable_pmksa_caching)
    2963                 :          2 :                 return -1;
    2964                 :            : 
    2965         [ +  - ]:         80 :         if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, PMK_LEN,
    2966                 :         80 :                                  sm->wpa_auth->addr, sm->addr, session_timeout,
    2967                 :            :                                  eapol, sm->wpa_key_mgmt))
    2968                 :         80 :                 return 0;
    2969                 :            : 
    2970                 :         82 :         return -1;
    2971                 :            : }
    2972                 :            : 
    2973                 :            : 
    2974                 :          0 : int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
    2975                 :            :                                const u8 *pmk, size_t len, const u8 *sta_addr,
    2976                 :            :                                int session_timeout,
    2977                 :            :                                struct eapol_state_machine *eapol)
    2978                 :            : {
    2979         [ #  # ]:          0 :         if (wpa_auth == NULL)
    2980                 :          0 :                 return -1;
    2981                 :            : 
    2982         [ #  # ]:          0 :         if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len, wpa_auth->addr,
    2983                 :            :                                  sta_addr, session_timeout, eapol,
    2984                 :            :                                  WPA_KEY_MGMT_IEEE8021X))
    2985                 :          0 :                 return 0;
    2986                 :            : 
    2987                 :          0 :         return -1;
    2988                 :            : }
    2989                 :            : 
    2990                 :            : 
    2991                 :          2 : void wpa_auth_pmksa_remove(struct wpa_authenticator *wpa_auth,
    2992                 :            :                            const u8 *sta_addr)
    2993                 :            : {
    2994                 :            :         struct rsn_pmksa_cache_entry *pmksa;
    2995                 :            : 
    2996 [ +  + ][ -  + ]:          2 :         if (wpa_auth == NULL || wpa_auth->pmksa == NULL)
    2997                 :          2 :                 return;
    2998                 :          1 :         pmksa = pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, NULL);
    2999         [ -  + ]:          1 :         if (pmksa) {
    3000                 :          0 :                 wpa_printf(MSG_DEBUG, "WPA: Remove PMKSA cache entry for "
    3001                 :          0 :                            MACSTR " based on request", MAC2STR(sta_addr));
    3002                 :          0 :                 pmksa_cache_free_entry(wpa_auth->pmksa, pmksa);
    3003                 :            :         }
    3004                 :            : }
    3005                 :            : 
    3006                 :            : 
    3007                 :            : static struct wpa_group *
    3008                 :          0 : wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
    3009                 :            : {
    3010                 :            :         struct wpa_group *group;
    3011                 :            : 
    3012 [ #  # ][ #  # ]:          0 :         if (wpa_auth == NULL || wpa_auth->group == NULL)
    3013                 :          0 :                 return NULL;
    3014                 :            : 
    3015                 :          0 :         wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
    3016                 :            :                    vlan_id);
    3017                 :          0 :         group = wpa_group_init(wpa_auth, vlan_id, 0);
    3018         [ #  # ]:          0 :         if (group == NULL)
    3019                 :          0 :                 return NULL;
    3020                 :            : 
    3021                 :          0 :         group->next = wpa_auth->group->next;
    3022                 :          0 :         wpa_auth->group->next = group;
    3023                 :            : 
    3024                 :          0 :         return group;
    3025                 :            : }
    3026                 :            : 
    3027                 :            : 
    3028                 :          0 : int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
    3029                 :            : {
    3030                 :            :         struct wpa_group *group;
    3031                 :            : 
    3032 [ #  # ][ #  # ]:          0 :         if (sm == NULL || sm->wpa_auth == NULL)
    3033                 :          0 :                 return 0;
    3034                 :            : 
    3035                 :          0 :         group = sm->wpa_auth->group;
    3036         [ #  # ]:          0 :         while (group) {
    3037         [ #  # ]:          0 :                 if (group->vlan_id == vlan_id)
    3038                 :          0 :                         break;
    3039                 :          0 :                 group = group->next;
    3040                 :            :         }
    3041                 :            : 
    3042         [ #  # ]:          0 :         if (group == NULL) {
    3043                 :          0 :                 group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
    3044         [ #  # ]:          0 :                 if (group == NULL)
    3045                 :          0 :                         return -1;
    3046                 :            :         }
    3047                 :            : 
    3048         [ #  # ]:          0 :         if (sm->group == group)
    3049                 :          0 :                 return 0;
    3050                 :            : 
    3051         [ #  # ]:          0 :         if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
    3052                 :          0 :                 return -1;
    3053                 :            : 
    3054                 :          0 :         wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
    3055                 :          0 :                    "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
    3056                 :            : 
    3057                 :          0 :         sm->group = group;
    3058                 :          0 :         return 0;
    3059                 :            : }
    3060                 :            : 
    3061                 :            : 
    3062                 :        389 : void wpa_auth_eapol_key_tx_status(struct wpa_authenticator *wpa_auth,
    3063                 :            :                                   struct wpa_state_machine *sm, int ack)
    3064                 :            : {
    3065 [ +  - ][ -  + ]:        389 :         if (wpa_auth == NULL || sm == NULL)
    3066                 :        389 :                 return;
    3067                 :        389 :         wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key TX status for STA " MACSTR
    3068                 :       2334 :                    " ack=%d", MAC2STR(sm->addr), ack);
    3069 [ +  - ][ +  + ]:        389 :         if (sm->pending_1_of_4_timeout && ack) {
    3070                 :            :                 /*
    3071                 :            :                  * Some deployed supplicant implementations update their SNonce
    3072                 :            :                  * for each EAPOL-Key 2/4 message even within the same 4-way
    3073                 :            :                  * handshake and then fail to use the first SNonce when
    3074                 :            :                  * deriving the PTK. This results in unsuccessful 4-way
    3075                 :            :                  * handshake whenever the relatively short initial timeout is
    3076                 :            :                  * reached and EAPOL-Key 1/4 is retransmitted. Try to work
    3077                 :            :                  * around this by increasing the timeout now that we know that
    3078                 :            :                  * the station has received the frame.
    3079                 :            :                  */
    3080                 :        188 :                 int timeout_ms = eapol_key_timeout_subseq;
    3081                 :        188 :                 wpa_printf(MSG_DEBUG, "WPA: Increase initial EAPOL-Key 1/4 "
    3082                 :            :                            "timeout by %u ms because of acknowledged frame",
    3083                 :            :                            timeout_ms);
    3084                 :        188 :                 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
    3085                 :        188 :                 eloop_register_timeout(timeout_ms / 1000,
    3086                 :        188 :                                        (timeout_ms % 1000) * 1000,
    3087                 :            :                                        wpa_send_eapol_timeout, wpa_auth, sm);
    3088                 :            :         }
    3089                 :            : }
    3090                 :            : 
    3091                 :            : 
    3092                 :        250 : int wpa_auth_uses_sae(struct wpa_state_machine *sm)
    3093                 :            : {
    3094         [ -  + ]:        250 :         if (sm == NULL)
    3095                 :          0 :                 return 0;
    3096                 :        250 :         return wpa_key_mgmt_sae(sm->wpa_key_mgmt);
    3097                 :            : }
    3098                 :            : 
    3099                 :            : 
    3100                 :          4 : int wpa_auth_uses_ft_sae(struct wpa_state_machine *sm)
    3101                 :            : {
    3102         [ -  + ]:          4 :         if (sm == NULL)
    3103                 :          0 :                 return 0;
    3104                 :          4 :         return sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE;
    3105                 :            : }

Generated by: LCOV version 1.9