LCOV - code coverage report
Current view: top level - src/ap - wpa_auth_ft.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1393793999 Lines: 494 867 57.0 %
Date: 2014-03-02 Functions: 26 33 78.8 %
Branches: 146 350 41.7 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * hostapd - IEEE 802.11r - Fast BSS Transition
       3                 :            :  * Copyright (c) 2004-2009, 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 "common/ieee802_11_defs.h"
      13                 :            : #include "common/ieee802_11_common.h"
      14                 :            : #include "crypto/aes_wrap.h"
      15                 :            : #include "crypto/random.h"
      16                 :            : #include "ap_config.h"
      17                 :            : #include "ieee802_11.h"
      18                 :            : #include "wmm.h"
      19                 :            : #include "wpa_auth.h"
      20                 :            : #include "wpa_auth_i.h"
      21                 :            : 
      22                 :            : 
      23                 :            : #ifdef CONFIG_IEEE80211R
      24                 :            : 
      25                 :         20 : static int wpa_ft_rrb_send(struct wpa_authenticator *wpa_auth, const u8 *dst,
      26                 :            :                            const u8 *data, size_t data_len)
      27                 :            : {
      28         [ -  + ]:         20 :         if (wpa_auth->cb.send_ether == NULL)
      29                 :          0 :                 return -1;
      30                 :         20 :         wpa_printf(MSG_DEBUG, "FT: RRB send to " MACSTR, MAC2STR(dst));
      31                 :         20 :         return wpa_auth->cb.send_ether(wpa_auth->cb.ctx, dst, ETH_P_RRB,
      32                 :            :                                        data, data_len);
      33                 :            : }
      34                 :            : 
      35                 :            : 
      36                 :          6 : static int wpa_ft_action_send(struct wpa_authenticator *wpa_auth,
      37                 :            :                               const u8 *dst, const u8 *data, size_t data_len)
      38                 :            : {
      39         [ -  + ]:          6 :         if (wpa_auth->cb.send_ft_action == NULL)
      40                 :          0 :                 return -1;
      41                 :          6 :         return wpa_auth->cb.send_ft_action(wpa_auth->cb.ctx, dst,
      42                 :            :                                            data, data_len);
      43                 :            : }
      44                 :            : 
      45                 :            : 
      46                 :            : static struct wpa_state_machine *
      47                 :          6 : wpa_ft_add_sta(struct wpa_authenticator *wpa_auth, const u8 *sta_addr)
      48                 :            : {
      49         [ -  + ]:          6 :         if (wpa_auth->cb.add_sta == NULL)
      50                 :          0 :                 return NULL;
      51                 :          6 :         return wpa_auth->cb.add_sta(wpa_auth->cb.ctx, sta_addr);
      52                 :            : }
      53                 :            : 
      54                 :            : 
      55                 :          0 : static int wpa_ft_add_tspec(struct wpa_authenticator *wpa_auth,
      56                 :            :                             const u8 *sta_addr,
      57                 :            :                             u8 *tspec_ie, size_t tspec_ielen)
      58                 :            : {
      59         [ #  # ]:          0 :         if (wpa_auth->cb.add_tspec == NULL) {
      60                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: add_tspec is not initialized");
      61                 :          0 :                 return -1;
      62                 :            :         }
      63                 :          0 :         return wpa_auth->cb.add_tspec(wpa_auth->cb.ctx, sta_addr, tspec_ie,
      64                 :            :                                       tspec_ielen);
      65                 :            : }
      66                 :            : 
      67                 :            : 
      68                 :         56 : int wpa_write_mdie(struct wpa_auth_config *conf, u8 *buf, size_t len)
      69                 :            : {
      70                 :         56 :         u8 *pos = buf;
      71                 :            :         u8 capab;
      72         [ -  + ]:         56 :         if (len < 2 + sizeof(struct rsn_mdie))
      73                 :          0 :                 return -1;
      74                 :            : 
      75                 :         56 :         *pos++ = WLAN_EID_MOBILITY_DOMAIN;
      76                 :         56 :         *pos++ = MOBILITY_DOMAIN_ID_LEN + 1;
      77                 :         56 :         os_memcpy(pos, conf->mobility_domain, MOBILITY_DOMAIN_ID_LEN);
      78                 :         56 :         pos += MOBILITY_DOMAIN_ID_LEN;
      79                 :         56 :         capab = 0;
      80         [ +  - ]:         56 :         if (conf->ft_over_ds)
      81                 :         56 :                 capab |= RSN_FT_CAPAB_FT_OVER_DS;
      82                 :         56 :         *pos++ = capab;
      83                 :            : 
      84                 :         56 :         return pos - buf;
      85                 :            : }
      86                 :            : 
      87                 :            : 
      88                 :         48 : int wpa_write_ftie(struct wpa_auth_config *conf, const u8 *r0kh_id,
      89                 :            :                    size_t r0kh_id_len,
      90                 :            :                    const u8 *anonce, const u8 *snonce,
      91                 :            :                    u8 *buf, size_t len, const u8 *subelem,
      92                 :            :                    size_t subelem_len)
      93                 :            : {
      94                 :         48 :         u8 *pos = buf, *ielen;
      95                 :            :         struct rsn_ftie *hdr;
      96                 :            : 
      97         [ -  + ]:         48 :         if (len < 2 + sizeof(*hdr) + 2 + FT_R1KH_ID_LEN + 2 + r0kh_id_len +
      98                 :            :             subelem_len)
      99                 :          0 :                 return -1;
     100                 :            : 
     101                 :         48 :         *pos++ = WLAN_EID_FAST_BSS_TRANSITION;
     102                 :         48 :         ielen = pos++;
     103                 :            : 
     104                 :         48 :         hdr = (struct rsn_ftie *) pos;
     105                 :         48 :         os_memset(hdr, 0, sizeof(*hdr));
     106                 :         48 :         pos += sizeof(*hdr);
     107                 :         48 :         WPA_PUT_LE16(hdr->mic_control, 0);
     108         [ +  + ]:         48 :         if (anonce)
     109                 :         32 :                 os_memcpy(hdr->anonce, anonce, WPA_NONCE_LEN);
     110         [ +  + ]:         48 :         if (snonce)
     111                 :         32 :                 os_memcpy(hdr->snonce, snonce, WPA_NONCE_LEN);
     112                 :            : 
     113                 :            :         /* Optional Parameters */
     114                 :         48 :         *pos++ = FTIE_SUBELEM_R1KH_ID;
     115                 :         48 :         *pos++ = FT_R1KH_ID_LEN;
     116                 :         48 :         os_memcpy(pos, conf->r1_key_holder, FT_R1KH_ID_LEN);
     117                 :         48 :         pos += FT_R1KH_ID_LEN;
     118                 :            : 
     119         [ +  - ]:         48 :         if (r0kh_id) {
     120                 :         48 :                 *pos++ = FTIE_SUBELEM_R0KH_ID;
     121                 :         48 :                 *pos++ = r0kh_id_len;
     122                 :         48 :                 os_memcpy(pos, r0kh_id, r0kh_id_len);
     123                 :         48 :                 pos += r0kh_id_len;
     124                 :            :         }
     125                 :            : 
     126         [ +  + ]:         48 :         if (subelem) {
     127                 :         16 :                 os_memcpy(pos, subelem, subelem_len);
     128                 :         16 :                 pos += subelem_len;
     129                 :            :         }
     130                 :            : 
     131                 :         48 :         *ielen = pos - buf - 2;
     132                 :            : 
     133                 :         48 :         return pos - buf;
     134                 :            : }
     135                 :            : 
     136                 :            : 
     137                 :            : struct wpa_ft_pmk_r0_sa {
     138                 :            :         struct wpa_ft_pmk_r0_sa *next;
     139                 :            :         u8 pmk_r0[PMK_LEN];
     140                 :            :         u8 pmk_r0_name[WPA_PMK_NAME_LEN];
     141                 :            :         u8 spa[ETH_ALEN];
     142                 :            :         int pairwise; /* Pairwise cipher suite, WPA_CIPHER_* */
     143                 :            :         /* TODO: expiration, identity, radius_class, EAP type, VLAN ID */
     144                 :            :         int pmk_r1_pushed;
     145                 :            : };
     146                 :            : 
     147                 :            : struct wpa_ft_pmk_r1_sa {
     148                 :            :         struct wpa_ft_pmk_r1_sa *next;
     149                 :            :         u8 pmk_r1[PMK_LEN];
     150                 :            :         u8 pmk_r1_name[WPA_PMK_NAME_LEN];
     151                 :            :         u8 spa[ETH_ALEN];
     152                 :            :         int pairwise; /* Pairwise cipher suite, WPA_CIPHER_* */
     153                 :            :         /* TODO: expiration, identity, radius_class, EAP type, VLAN ID */
     154                 :            : };
     155                 :            : 
     156                 :            : struct wpa_ft_pmk_cache {
     157                 :            :         struct wpa_ft_pmk_r0_sa *pmk_r0;
     158                 :            :         struct wpa_ft_pmk_r1_sa *pmk_r1;
     159                 :            : };
     160                 :            : 
     161                 :        350 : struct wpa_ft_pmk_cache * wpa_ft_pmk_cache_init(void)
     162                 :            : {
     163                 :            :         struct wpa_ft_pmk_cache *cache;
     164                 :            : 
     165                 :        350 :         cache = os_zalloc(sizeof(*cache));
     166                 :            : 
     167                 :        350 :         return cache;
     168                 :            : }
     169                 :            : 
     170                 :            : 
     171                 :        350 : void wpa_ft_pmk_cache_deinit(struct wpa_ft_pmk_cache *cache)
     172                 :            : {
     173                 :            :         struct wpa_ft_pmk_r0_sa *r0, *r0prev;
     174                 :            :         struct wpa_ft_pmk_r1_sa *r1, *r1prev;
     175                 :            : 
     176                 :        350 :         r0 = cache->pmk_r0;
     177         [ +  + ]:        358 :         while (r0) {
     178                 :          8 :                 r0prev = r0;
     179                 :          8 :                 r0 = r0->next;
     180                 :          8 :                 os_memset(r0prev->pmk_r0, 0, PMK_LEN);
     181                 :          8 :                 os_free(r0prev);
     182                 :            :         }
     183                 :            : 
     184                 :        350 :         r1 = cache->pmk_r1;
     185         [ +  + ]:        366 :         while (r1) {
     186                 :         16 :                 r1prev = r1;
     187                 :         16 :                 r1 = r1->next;
     188                 :         16 :                 os_memset(r1prev->pmk_r1, 0, PMK_LEN);
     189                 :         16 :                 os_free(r1prev);
     190                 :            :         }
     191                 :            : 
     192                 :        350 :         os_free(cache);
     193                 :        350 : }
     194                 :            : 
     195                 :            : 
     196                 :          8 : static int wpa_ft_store_pmk_r0(struct wpa_authenticator *wpa_auth,
     197                 :            :                                const u8 *spa, const u8 *pmk_r0,
     198                 :            :                                const u8 *pmk_r0_name, int pairwise)
     199                 :            : {
     200                 :          8 :         struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
     201                 :            :         struct wpa_ft_pmk_r0_sa *r0;
     202                 :            : 
     203                 :            :         /* TODO: add expiration and limit on number of entries in cache */
     204                 :            : 
     205                 :          8 :         r0 = os_zalloc(sizeof(*r0));
     206         [ -  + ]:          8 :         if (r0 == NULL)
     207                 :          0 :                 return -1;
     208                 :            : 
     209                 :          8 :         os_memcpy(r0->pmk_r0, pmk_r0, PMK_LEN);
     210                 :          8 :         os_memcpy(r0->pmk_r0_name, pmk_r0_name, WPA_PMK_NAME_LEN);
     211                 :          8 :         os_memcpy(r0->spa, spa, ETH_ALEN);
     212                 :          8 :         r0->pairwise = pairwise;
     213                 :            : 
     214                 :          8 :         r0->next = cache->pmk_r0;
     215                 :          8 :         cache->pmk_r0 = r0;
     216                 :            : 
     217                 :          8 :         return 0;
     218                 :            : }
     219                 :            : 
     220                 :            : 
     221                 :          0 : static int wpa_ft_fetch_pmk_r0(struct wpa_authenticator *wpa_auth,
     222                 :            :                                const u8 *spa, const u8 *pmk_r0_name,
     223                 :            :                                u8 *pmk_r0, int *pairwise)
     224                 :            : {
     225                 :          0 :         struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
     226                 :            :         struct wpa_ft_pmk_r0_sa *r0;
     227                 :            : 
     228                 :          0 :         r0 = cache->pmk_r0;
     229         [ #  # ]:          0 :         while (r0) {
     230 [ #  # ][ #  # ]:          0 :                 if (os_memcmp(r0->spa, spa, ETH_ALEN) == 0 &&
     231                 :          0 :                     os_memcmp(r0->pmk_r0_name, pmk_r0_name, WPA_PMK_NAME_LEN)
     232                 :            :                     == 0) {
     233                 :          0 :                         os_memcpy(pmk_r0, r0->pmk_r0, PMK_LEN);
     234         [ #  # ]:          0 :                         if (pairwise)
     235                 :          0 :                                 *pairwise = r0->pairwise;
     236                 :          0 :                         return 0;
     237                 :            :                 }
     238                 :            : 
     239                 :          0 :                 r0 = r0->next;
     240                 :            :         }
     241                 :            : 
     242                 :          0 :         return -1;
     243                 :            : }
     244                 :            : 
     245                 :            : 
     246                 :         16 : static int wpa_ft_store_pmk_r1(struct wpa_authenticator *wpa_auth,
     247                 :            :                                const u8 *spa, const u8 *pmk_r1,
     248                 :            :                                const u8 *pmk_r1_name, int pairwise)
     249                 :            : {
     250                 :         16 :         struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
     251                 :            :         struct wpa_ft_pmk_r1_sa *r1;
     252                 :            : 
     253                 :            :         /* TODO: add expiration and limit on number of entries in cache */
     254                 :            : 
     255                 :         16 :         r1 = os_zalloc(sizeof(*r1));
     256         [ -  + ]:         16 :         if (r1 == NULL)
     257                 :          0 :                 return -1;
     258                 :            : 
     259                 :         16 :         os_memcpy(r1->pmk_r1, pmk_r1, PMK_LEN);
     260                 :         16 :         os_memcpy(r1->pmk_r1_name, pmk_r1_name, WPA_PMK_NAME_LEN);
     261                 :         16 :         os_memcpy(r1->spa, spa, ETH_ALEN);
     262                 :         16 :         r1->pairwise = pairwise;
     263                 :            : 
     264                 :         16 :         r1->next = cache->pmk_r1;
     265                 :         16 :         cache->pmk_r1 = r1;
     266                 :            : 
     267                 :         16 :         return 0;
     268                 :            : }
     269                 :            : 
     270                 :            : 
     271                 :         16 : static int wpa_ft_fetch_pmk_r1(struct wpa_authenticator *wpa_auth,
     272                 :            :                                const u8 *spa, const u8 *pmk_r1_name,
     273                 :            :                                u8 *pmk_r1, int *pairwise)
     274                 :            : {
     275                 :         16 :         struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
     276                 :            :         struct wpa_ft_pmk_r1_sa *r1;
     277                 :            : 
     278                 :         16 :         r1 = cache->pmk_r1;
     279         [ +  - ]:         16 :         while (r1) {
     280 [ +  - ][ +  - ]:         16 :                 if (os_memcmp(r1->spa, spa, ETH_ALEN) == 0 &&
     281                 :         16 :                     os_memcmp(r1->pmk_r1_name, pmk_r1_name, WPA_PMK_NAME_LEN)
     282                 :            :                     == 0) {
     283                 :         16 :                         os_memcpy(pmk_r1, r1->pmk_r1, PMK_LEN);
     284         [ +  - ]:         16 :                         if (pairwise)
     285                 :         16 :                                 *pairwise = r1->pairwise;
     286                 :         16 :                         return 0;
     287                 :            :                 }
     288                 :            : 
     289                 :          0 :                 r1 = r1->next;
     290                 :            :         }
     291                 :            : 
     292                 :         16 :         return -1;
     293                 :            : }
     294                 :            : 
     295                 :            : 
     296                 :          0 : static int wpa_ft_pull_pmk_r1(struct wpa_authenticator *wpa_auth,
     297                 :            :                               const u8 *s1kh_id, const u8 *r0kh_id,
     298                 :            :                               size_t r0kh_id_len, const u8 *pmk_r0_name)
     299                 :            : {
     300                 :            :         struct ft_remote_r0kh *r0kh;
     301                 :            :         struct ft_r0kh_r1kh_pull_frame frame, f;
     302                 :            : 
     303                 :          0 :         r0kh = wpa_auth->conf.r0kh_list;
     304         [ #  # ]:          0 :         while (r0kh) {
     305 [ #  # ][ #  # ]:          0 :                 if (r0kh->id_len == r0kh_id_len &&
     306                 :          0 :                     os_memcmp(r0kh->id, r0kh_id, r0kh_id_len) == 0)
     307                 :          0 :                         break;
     308                 :          0 :                 r0kh = r0kh->next;
     309                 :            :         }
     310         [ #  # ]:          0 :         if (r0kh == NULL)
     311                 :          0 :                 return -1;
     312                 :            : 
     313                 :          0 :         wpa_printf(MSG_DEBUG, "FT: Send PMK-R1 pull request to remote R0KH "
     314                 :          0 :                    "address " MACSTR, MAC2STR(r0kh->addr));
     315                 :            : 
     316                 :          0 :         os_memset(&frame, 0, sizeof(frame));
     317                 :          0 :         frame.frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
     318                 :          0 :         frame.packet_type = FT_PACKET_R0KH_R1KH_PULL;
     319                 :          0 :         frame.data_length = host_to_le16(FT_R0KH_R1KH_PULL_DATA_LEN);
     320                 :          0 :         os_memcpy(frame.ap_address, wpa_auth->addr, ETH_ALEN);
     321                 :            : 
     322                 :            :         /* aes_wrap() does not support inplace encryption, so use a temporary
     323                 :            :          * buffer for the data. */
     324         [ #  # ]:          0 :         if (random_get_bytes(f.nonce, sizeof(f.nonce))) {
     325                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: Failed to get random data for "
     326                 :            :                            "nonce");
     327                 :          0 :                 return -1;
     328                 :            :         }
     329                 :          0 :         os_memcpy(f.pmk_r0_name, pmk_r0_name, WPA_PMK_NAME_LEN);
     330                 :          0 :         os_memcpy(f.r1kh_id, wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN);
     331                 :          0 :         os_memcpy(f.s1kh_id, s1kh_id, ETH_ALEN);
     332                 :          0 :         os_memset(f.pad, 0, sizeof(f.pad));
     333                 :            : 
     334         [ #  # ]:          0 :         if (aes_wrap(r0kh->key, (FT_R0KH_R1KH_PULL_DATA_LEN + 7) / 8,
     335                 :            :                      f.nonce, frame.nonce) < 0)
     336                 :          0 :                 return -1;
     337                 :            : 
     338                 :          0 :         wpa_ft_rrb_send(wpa_auth, r0kh->addr, (u8 *) &frame, sizeof(frame));
     339                 :            : 
     340                 :          0 :         return 0;
     341                 :            : }
     342                 :            : 
     343                 :            : 
     344                 :          8 : int wpa_auth_derive_ptk_ft(struct wpa_state_machine *sm, const u8 *pmk,
     345                 :            :                            struct wpa_ptk *ptk, size_t ptk_len)
     346                 :            : {
     347                 :            :         u8 pmk_r0[PMK_LEN], pmk_r0_name[WPA_PMK_NAME_LEN];
     348                 :            :         u8 pmk_r1[PMK_LEN];
     349                 :            :         u8 ptk_name[WPA_PMK_NAME_LEN];
     350                 :          8 :         const u8 *mdid = sm->wpa_auth->conf.mobility_domain;
     351                 :          8 :         const u8 *r0kh = sm->wpa_auth->conf.r0_key_holder;
     352                 :          8 :         size_t r0kh_len = sm->wpa_auth->conf.r0_key_holder_len;
     353                 :          8 :         const u8 *r1kh = sm->wpa_auth->conf.r1_key_holder;
     354                 :          8 :         const u8 *ssid = sm->wpa_auth->conf.ssid;
     355                 :          8 :         size_t ssid_len = sm->wpa_auth->conf.ssid_len;
     356                 :            : 
     357                 :            : 
     358         [ -  + ]:          8 :         if (sm->xxkey_len == 0) {
     359                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: XXKey not available for key "
     360                 :            :                            "derivation");
     361                 :          0 :                 return -1;
     362                 :            :         }
     363                 :            : 
     364                 :          8 :         wpa_derive_pmk_r0(sm->xxkey, sm->xxkey_len, ssid, ssid_len, mdid,
     365                 :          8 :                           r0kh, r0kh_len, sm->addr, pmk_r0, pmk_r0_name);
     366                 :          8 :         wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R0", pmk_r0, PMK_LEN);
     367                 :          8 :         wpa_hexdump(MSG_DEBUG, "FT: PMKR0Name", pmk_r0_name, WPA_PMK_NAME_LEN);
     368                 :          8 :         wpa_ft_store_pmk_r0(sm->wpa_auth, sm->addr, pmk_r0, pmk_r0_name,
     369                 :            :                             sm->pairwise);
     370                 :            : 
     371                 :          8 :         wpa_derive_pmk_r1(pmk_r0, pmk_r0_name, r1kh, sm->addr,
     372                 :          8 :                           pmk_r1, sm->pmk_r1_name);
     373                 :          8 :         wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", pmk_r1, PMK_LEN);
     374                 :          8 :         wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", sm->pmk_r1_name,
     375                 :            :                     WPA_PMK_NAME_LEN);
     376                 :          8 :         wpa_ft_store_pmk_r1(sm->wpa_auth, sm->addr, pmk_r1, sm->pmk_r1_name,
     377                 :            :                             sm->pairwise);
     378                 :            : 
     379                 :          8 :         wpa_pmk_r1_to_ptk(pmk_r1, sm->SNonce, sm->ANonce, sm->addr,
     380                 :          8 :                           sm->wpa_auth->addr, sm->pmk_r1_name,
     381                 :            :                           (u8 *) ptk, ptk_len, ptk_name);
     382                 :          8 :         wpa_hexdump_key(MSG_DEBUG, "FT: PTK", (u8 *) ptk, ptk_len);
     383                 :          8 :         wpa_hexdump(MSG_DEBUG, "FT: PTKName", ptk_name, WPA_PMK_NAME_LEN);
     384                 :            : 
     385                 :          8 :         return 0;
     386                 :            : }
     387                 :            : 
     388                 :            : 
     389                 :         20 : static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
     390                 :            :                                       const u8 *addr, int idx, u8 *seq)
     391                 :            : {
     392         [ -  + ]:         20 :         if (wpa_auth->cb.get_seqnum == NULL)
     393                 :          0 :                 return -1;
     394                 :         20 :         return wpa_auth->cb.get_seqnum(wpa_auth->cb.ctx, addr, idx, seq);
     395                 :            : }
     396                 :            : 
     397                 :            : 
     398                 :         16 : static u8 * wpa_ft_gtk_subelem(struct wpa_state_machine *sm, size_t *len)
     399                 :            : {
     400                 :            :         u8 *subelem;
     401                 :         16 :         struct wpa_group *gsm = sm->group;
     402                 :            :         size_t subelem_len, pad_len;
     403                 :            :         const u8 *key;
     404                 :            :         size_t key_len;
     405                 :            :         u8 keybuf[32];
     406                 :            : 
     407                 :         16 :         key_len = gsm->GTK_len;
     408         [ -  + ]:         16 :         if (key_len > sizeof(keybuf))
     409                 :          0 :                 return NULL;
     410                 :            : 
     411                 :            :         /*
     412                 :            :          * Pad key for AES Key Wrap if it is not multiple of 8 bytes or is less
     413                 :            :          * than 16 bytes.
     414                 :            :          */
     415                 :         16 :         pad_len = key_len % 8;
     416         [ -  + ]:         16 :         if (pad_len)
     417                 :          0 :                 pad_len = 8 - pad_len;
     418         [ -  + ]:         16 :         if (key_len + pad_len < 16)
     419                 :          0 :                 pad_len += 8;
     420 [ -  + ][ #  # ]:         16 :         if (pad_len && key_len < sizeof(keybuf)) {
     421                 :          0 :                 os_memcpy(keybuf, gsm->GTK[gsm->GN - 1], key_len);
     422                 :          0 :                 os_memset(keybuf + key_len, 0, pad_len);
     423                 :          0 :                 keybuf[key_len] = 0xdd;
     424                 :          0 :                 key_len += pad_len;
     425                 :          0 :                 key = keybuf;
     426                 :            :         } else
     427                 :         16 :                 key = gsm->GTK[gsm->GN - 1];
     428                 :            : 
     429                 :            :         /*
     430                 :            :          * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
     431                 :            :          * Key[5..32].
     432                 :            :          */
     433                 :         16 :         subelem_len = 13 + key_len + 8;
     434                 :         16 :         subelem = os_zalloc(subelem_len);
     435         [ -  + ]:         16 :         if (subelem == NULL)
     436                 :          0 :                 return NULL;
     437                 :            : 
     438                 :         16 :         subelem[0] = FTIE_SUBELEM_GTK;
     439                 :         16 :         subelem[1] = 11 + key_len + 8;
     440                 :            :         /* Key ID in B0-B1 of Key Info */
     441                 :         16 :         WPA_PUT_LE16(&subelem[2], gsm->GN & 0x03);
     442                 :         16 :         subelem[4] = gsm->GTK_len;
     443                 :         16 :         wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, subelem + 5);
     444         [ -  + ]:         16 :         if (aes_wrap(sm->PTK.kek, key_len / 8, key, subelem + 13)) {
     445                 :          0 :                 os_free(subelem);
     446                 :          0 :                 return NULL;
     447                 :            :         }
     448                 :            : 
     449                 :         16 :         *len = subelem_len;
     450                 :         16 :         return subelem;
     451                 :            : }
     452                 :            : 
     453                 :            : 
     454                 :            : #ifdef CONFIG_IEEE80211W
     455                 :          4 : static u8 * wpa_ft_igtk_subelem(struct wpa_state_machine *sm, size_t *len)
     456                 :            : {
     457                 :            :         u8 *subelem, *pos;
     458                 :          4 :         struct wpa_group *gsm = sm->group;
     459                 :            :         size_t subelem_len;
     460                 :            : 
     461                 :            :         /* Sub-elem ID[1] | Length[1] | KeyID[2] | IPN[6] | Key Length[1] |
     462                 :            :          * Key[16+8] */
     463                 :          4 :         subelem_len = 1 + 1 + 2 + 6 + 1 + WPA_IGTK_LEN + 8;
     464                 :          4 :         subelem = os_zalloc(subelem_len);
     465         [ -  + ]:          4 :         if (subelem == NULL)
     466                 :          0 :                 return NULL;
     467                 :            : 
     468                 :          4 :         pos = subelem;
     469                 :          4 :         *pos++ = FTIE_SUBELEM_IGTK;
     470                 :          4 :         *pos++ = subelem_len - 2;
     471                 :          4 :         WPA_PUT_LE16(pos, gsm->GN_igtk);
     472                 :          4 :         pos += 2;
     473                 :          4 :         wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos);
     474                 :          4 :         pos += 6;
     475                 :          4 :         *pos++ = WPA_IGTK_LEN;
     476         [ -  + ]:          4 :         if (aes_wrap(sm->PTK.kek, WPA_IGTK_LEN / 8,
     477                 :          4 :                      gsm->IGTK[gsm->GN_igtk - 4], pos)) {
     478                 :          0 :                 os_free(subelem);
     479                 :          0 :                 return NULL;
     480                 :            :         }
     481                 :            : 
     482                 :          4 :         *len = subelem_len;
     483                 :          4 :         return subelem;
     484                 :            : }
     485                 :            : #endif /* CONFIG_IEEE80211W */
     486                 :            : 
     487                 :            : 
     488                 :          0 : static u8 * wpa_ft_process_rdie(struct wpa_state_machine *sm,
     489                 :            :                                 u8 *pos, u8 *end, u8 id, u8 descr_count,
     490                 :            :                                 const u8 *ies, size_t ies_len)
     491                 :            : {
     492                 :            :         struct ieee802_11_elems parse;
     493                 :            :         struct rsn_rdie *rdie;
     494                 :            : 
     495                 :          0 :         wpa_printf(MSG_DEBUG, "FT: Resource Request: id=%d descr_count=%d",
     496                 :            :                    id, descr_count);
     497                 :          0 :         wpa_hexdump(MSG_MSGDUMP, "FT: Resource descriptor IE(s)",
     498                 :            :                     ies, ies_len);
     499                 :            : 
     500         [ #  # ]:          0 :         if (end - pos < (int) sizeof(*rdie)) {
     501                 :          0 :                 wpa_printf(MSG_ERROR, "FT: Not enough room for response RDIE");
     502                 :          0 :                 return pos;
     503                 :            :         }
     504                 :            : 
     505                 :          0 :         *pos++ = WLAN_EID_RIC_DATA;
     506                 :          0 :         *pos++ = sizeof(*rdie);
     507                 :          0 :         rdie = (struct rsn_rdie *) pos;
     508                 :          0 :         rdie->id = id;
     509                 :          0 :         rdie->descr_count = 0;
     510                 :          0 :         rdie->status_code = host_to_le16(WLAN_STATUS_SUCCESS);
     511                 :          0 :         pos += sizeof(*rdie);
     512                 :            : 
     513         [ #  # ]:          0 :         if (ieee802_11_parse_elems((u8 *) ies, ies_len, &parse, 1) ==
     514                 :            :             ParseFailed) {
     515                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: Failed to parse request IEs");
     516                 :          0 :                 rdie->status_code =
     517                 :            :                         host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE);
     518                 :          0 :                 return pos;
     519                 :            :         }
     520                 :            : 
     521                 :            : #ifdef NEED_AP_MLME
     522 [ #  # ][ #  # ]:          0 :         if (parse.wmm_tspec && sm->wpa_auth->conf.ap_mlme) {
     523                 :            :                 struct wmm_tspec_element *tspec;
     524                 :            :                 int res;
     525                 :            : 
     526         [ #  # ]:          0 :                 if (parse.wmm_tspec_len + 2 < (int) sizeof(*tspec)) {
     527                 :          0 :                         wpa_printf(MSG_DEBUG, "FT: Too short WMM TSPEC IE "
     528                 :          0 :                                    "(%d)", (int) parse.wmm_tspec_len);
     529                 :          0 :                         rdie->status_code =
     530                 :            :                                 host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE);
     531                 :          0 :                         return pos;
     532                 :            :                 }
     533         [ #  # ]:          0 :                 if (end - pos < (int) sizeof(*tspec)) {
     534                 :          0 :                         wpa_printf(MSG_ERROR, "FT: Not enough room for "
     535                 :            :                                    "response TSPEC");
     536                 :          0 :                         rdie->status_code =
     537                 :            :                                 host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE);
     538                 :          0 :                         return pos;
     539                 :            :                 }
     540                 :          0 :                 tspec = (struct wmm_tspec_element *) pos;
     541                 :          0 :                 os_memcpy(tspec, parse.wmm_tspec - 2, sizeof(*tspec));
     542                 :          0 :                 res = wmm_process_tspec(tspec);
     543                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: ADDTS processing result: %d", res);
     544         [ #  # ]:          0 :                 if (res == WMM_ADDTS_STATUS_INVALID_PARAMETERS)
     545                 :          0 :                         rdie->status_code =
     546                 :            :                                 host_to_le16(WLAN_STATUS_INVALID_PARAMETERS);
     547         [ #  # ]:          0 :                 else if (res == WMM_ADDTS_STATUS_REFUSED)
     548                 :          0 :                         rdie->status_code =
     549                 :            :                                 host_to_le16(WLAN_STATUS_REQUEST_DECLINED);
     550                 :            :                 else {
     551                 :            :                         /* TSPEC accepted; include updated TSPEC in response */
     552                 :          0 :                         rdie->descr_count = 1;
     553                 :          0 :                         pos += sizeof(*tspec);
     554                 :            :                 }
     555                 :          0 :                 return pos;
     556                 :            :         }
     557                 :            : #endif /* NEED_AP_MLME */
     558                 :            : 
     559 [ #  # ][ #  # ]:          0 :         if (parse.wmm_tspec && !sm->wpa_auth->conf.ap_mlme) {
     560                 :            :                 struct wmm_tspec_element *tspec;
     561                 :            :                 int res;
     562                 :            : 
     563                 :          0 :                 tspec = (struct wmm_tspec_element *) pos;
     564                 :          0 :                 os_memcpy(tspec, parse.wmm_tspec - 2, sizeof(*tspec));
     565                 :          0 :                 res = wpa_ft_add_tspec(sm->wpa_auth, sm->addr, pos,
     566                 :            :                                        sizeof(*tspec));
     567         [ #  # ]:          0 :                 if (res >= 0) {
     568         [ #  # ]:          0 :                         if (res)
     569                 :          0 :                                 rdie->status_code = host_to_le16(res);
     570                 :            :                         else {
     571                 :            :                                 /* TSPEC accepted; include updated TSPEC in
     572                 :            :                                  * response */
     573                 :          0 :                                 rdie->descr_count = 1;
     574                 :          0 :                                 pos += sizeof(*tspec);
     575                 :            :                         }
     576                 :          0 :                         return pos;
     577                 :            :                 }
     578                 :            :         }
     579                 :            : 
     580                 :          0 :         wpa_printf(MSG_DEBUG, "FT: No supported resource requested");
     581                 :          0 :         rdie->status_code = host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE);
     582                 :          0 :         return pos;
     583                 :            : }
     584                 :            : 
     585                 :            : 
     586                 :          0 : static u8 * wpa_ft_process_ric(struct wpa_state_machine *sm, u8 *pos, u8 *end,
     587                 :            :                                const u8 *ric, size_t ric_len)
     588                 :            : {
     589                 :            :         const u8 *rpos, *start;
     590                 :            :         const struct rsn_rdie *rdie;
     591                 :            : 
     592                 :          0 :         wpa_hexdump(MSG_MSGDUMP, "FT: RIC Request", ric, ric_len);
     593                 :            : 
     594                 :          0 :         rpos = ric;
     595         [ #  # ]:          0 :         while (rpos + sizeof(*rdie) < ric + ric_len) {
     596 [ #  # ][ #  # ]:          0 :                 if (rpos[0] != WLAN_EID_RIC_DATA || rpos[1] < sizeof(*rdie) ||
                 [ #  # ]
     597                 :          0 :                     rpos + 2 + rpos[1] > ric + ric_len)
     598                 :            :                         break;
     599                 :          0 :                 rdie = (const struct rsn_rdie *) (rpos + 2);
     600                 :          0 :                 rpos += 2 + rpos[1];
     601                 :          0 :                 start = rpos;
     602                 :            : 
     603 [ #  # ][ #  # ]:          0 :                 while (rpos + 2 <= ric + ric_len &&
     604                 :          0 :                        rpos + 2 + rpos[1] <= ric + ric_len) {
     605         [ #  # ]:          0 :                         if (rpos[0] == WLAN_EID_RIC_DATA)
     606                 :          0 :                                 break;
     607                 :          0 :                         rpos += 2 + rpos[1];
     608                 :            :                 }
     609                 :          0 :                 pos = wpa_ft_process_rdie(sm, pos, end, rdie->id,
     610                 :          0 :                                           rdie->descr_count,
     611                 :          0 :                                           start, rpos - start);
     612                 :            :         }
     613                 :            : 
     614                 :          0 :         return pos;
     615                 :            : }
     616                 :            : 
     617                 :            : 
     618                 :        674 : u8 * wpa_sm_write_assoc_resp_ies(struct wpa_state_machine *sm, u8 *pos,
     619                 :            :                                  size_t max_len, int auth_alg,
     620                 :            :                                  const u8 *req_ies, size_t req_ies_len)
     621                 :            : {
     622                 :        674 :         u8 *end, *mdie, *ftie, *rsnie = NULL, *r0kh_id, *subelem = NULL;
     623                 :        674 :         size_t mdie_len, ftie_len, rsnie_len = 0, r0kh_id_len, subelem_len = 0;
     624                 :            :         int res;
     625                 :            :         struct wpa_auth_config *conf;
     626                 :            :         struct rsn_ftie *_ftie;
     627                 :            :         struct wpa_ft_ies parse;
     628                 :            :         u8 *ric_start;
     629                 :            :         u8 *anonce, *snonce;
     630                 :            : 
     631         [ +  + ]:        674 :         if (sm == NULL)
     632                 :        246 :                 return pos;
     633                 :            : 
     634                 :        428 :         conf = &sm->wpa_auth->conf;
     635                 :            : 
     636         [ +  + ]:        428 :         if (!wpa_key_mgmt_ft(sm->wpa_key_mgmt))
     637                 :        404 :                 return pos;
     638                 :            : 
     639                 :         24 :         end = pos + max_len;
     640                 :            : 
     641         [ +  + ]:         24 :         if (auth_alg == WLAN_AUTH_FT) {
     642                 :            :                 /*
     643                 :            :                  * RSN (only present if this is a Reassociation Response and
     644                 :            :                  * part of a fast BSS transition)
     645                 :            :                  */
     646                 :         16 :                 res = wpa_write_rsn_ie(conf, pos, end - pos, sm->pmk_r1_name);
     647         [ -  + ]:         16 :                 if (res < 0)
     648                 :          0 :                         return pos;
     649                 :         16 :                 rsnie = pos;
     650                 :         16 :                 rsnie_len = res;
     651                 :         16 :                 pos += res;
     652                 :            :         }
     653                 :            : 
     654                 :            :         /* Mobility Domain Information */
     655                 :         24 :         res = wpa_write_mdie(conf, pos, end - pos);
     656         [ -  + ]:         24 :         if (res < 0)
     657                 :          0 :                 return pos;
     658                 :         24 :         mdie = pos;
     659                 :         24 :         mdie_len = res;
     660                 :         24 :         pos += res;
     661                 :            : 
     662                 :            :         /* Fast BSS Transition Information */
     663         [ +  + ]:         24 :         if (auth_alg == WLAN_AUTH_FT) {
     664                 :         16 :                 subelem = wpa_ft_gtk_subelem(sm, &subelem_len);
     665                 :         16 :                 r0kh_id = sm->r0kh_id;
     666                 :         16 :                 r0kh_id_len = sm->r0kh_id_len;
     667                 :         16 :                 anonce = sm->ANonce;
     668                 :         16 :                 snonce = sm->SNonce;
     669                 :            : #ifdef CONFIG_IEEE80211W
     670         [ +  + ]:         16 :                 if (sm->mgmt_frame_prot) {
     671                 :            :                         u8 *igtk;
     672                 :            :                         size_t igtk_len;
     673                 :            :                         u8 *nbuf;
     674                 :          4 :                         igtk = wpa_ft_igtk_subelem(sm, &igtk_len);
     675         [ -  + ]:          4 :                         if (igtk == NULL) {
     676                 :          0 :                                 os_free(subelem);
     677                 :          0 :                                 return pos;
     678                 :            :                         }
     679                 :          4 :                         nbuf = os_realloc(subelem, subelem_len + igtk_len);
     680         [ -  + ]:          4 :                         if (nbuf == NULL) {
     681                 :          0 :                                 os_free(subelem);
     682                 :          0 :                                 os_free(igtk);
     683                 :          0 :                                 return pos;
     684                 :            :                         }
     685                 :          4 :                         subelem = nbuf;
     686                 :          4 :                         os_memcpy(subelem + subelem_len, igtk, igtk_len);
     687                 :          4 :                         subelem_len += igtk_len;
     688                 :          4 :                         os_free(igtk);
     689                 :            :                 }
     690                 :            : #endif /* CONFIG_IEEE80211W */
     691                 :            :         } else {
     692                 :          8 :                 r0kh_id = conf->r0_key_holder;
     693                 :          8 :                 r0kh_id_len = conf->r0_key_holder_len;
     694                 :          8 :                 anonce = NULL;
     695                 :          8 :                 snonce = NULL;
     696                 :            :         }
     697                 :         24 :         res = wpa_write_ftie(conf, r0kh_id, r0kh_id_len, anonce, snonce, pos,
     698                 :         24 :                              end - pos, subelem, subelem_len);
     699                 :         24 :         os_free(subelem);
     700         [ -  + ]:         24 :         if (res < 0)
     701                 :          0 :                 return pos;
     702                 :         24 :         ftie = pos;
     703                 :         24 :         ftie_len = res;
     704                 :         24 :         pos += res;
     705                 :            : 
     706                 :         24 :         os_free(sm->assoc_resp_ftie);
     707                 :         24 :         sm->assoc_resp_ftie = os_malloc(ftie_len);
     708         [ +  - ]:         24 :         if (sm->assoc_resp_ftie)
     709                 :         24 :                 os_memcpy(sm->assoc_resp_ftie, ftie, ftie_len);
     710                 :            : 
     711                 :         24 :         _ftie = (struct rsn_ftie *) (ftie + 2);
     712         [ +  + ]:         24 :         if (auth_alg == WLAN_AUTH_FT)
     713                 :         16 :                 _ftie->mic_control[1] = 3; /* Information element count */
     714                 :            : 
     715                 :         24 :         ric_start = pos;
     716 [ +  - ][ -  + ]:         24 :         if (wpa_ft_parse_ies(req_ies, req_ies_len, &parse) == 0 && parse.ric) {
     717                 :          0 :                 pos = wpa_ft_process_ric(sm, pos, end, parse.ric,
     718                 :            :                                          parse.ric_len);
     719         [ #  # ]:          0 :                 if (auth_alg == WLAN_AUTH_FT)
     720                 :          0 :                         _ftie->mic_control[1] +=
     721                 :          0 :                                 ieee802_11_ie_count(ric_start,
     722                 :          0 :                                                     pos - ric_start);
     723                 :            :         }
     724         [ +  - ]:         24 :         if (ric_start == pos)
     725                 :         24 :                 ric_start = NULL;
     726                 :            : 
     727   [ +  +  -  + ]:         40 :         if (auth_alg == WLAN_AUTH_FT &&
     728         [ -  + ]:         16 :             wpa_ft_mic(sm->PTK.kck, sm->addr, sm->wpa_auth->addr, 6,
     729                 :            :                        mdie, mdie_len, ftie, ftie_len,
     730                 :            :                        rsnie, rsnie_len,
     731                 :          0 :                        ric_start, ric_start ? pos - ric_start : 0,
     732                 :         16 :                        _ftie->mic) < 0)
     733                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
     734                 :            : 
     735                 :        674 :         return pos;
     736                 :            : }
     737                 :            : 
     738                 :            : 
     739                 :         32 : static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
     740                 :            :                                    int vlan_id,
     741                 :            :                                    enum wpa_alg alg, const u8 *addr, int idx,
     742                 :            :                                    u8 *key, size_t key_len)
     743                 :            : {
     744         [ -  + ]:         32 :         if (wpa_auth->cb.set_key == NULL)
     745                 :          0 :                 return -1;
     746                 :         32 :         return wpa_auth->cb.set_key(wpa_auth->cb.ctx, vlan_id, alg, addr, idx,
     747                 :            :                                     key, key_len);
     748                 :            : }
     749                 :            : 
     750                 :            : 
     751                 :         32 : void wpa_ft_install_ptk(struct wpa_state_machine *sm)
     752                 :            : {
     753                 :            :         enum wpa_alg alg;
     754                 :            :         int klen;
     755                 :            : 
     756                 :            :         /* MLME-SETKEYS.request(PTK) */
     757                 :         32 :         alg = wpa_cipher_to_alg(sm->pairwise);
     758                 :         32 :         klen = wpa_cipher_key_len(sm->pairwise);
     759         [ -  + ]:         32 :         if (!wpa_cipher_valid_pairwise(sm->pairwise)) {
     760                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: Unknown pairwise alg 0x%x - skip "
     761                 :            :                            "PTK configuration", sm->pairwise);
     762                 :          0 :                 return;
     763                 :            :         }
     764                 :            : 
     765                 :            :         /* FIX: add STA entry to kernel/driver here? The set_key will fail
     766                 :            :          * most likely without this.. At the moment, STA entry is added only
     767                 :            :          * after association has been completed. This function will be called
     768                 :            :          * again after association to get the PTK configured, but that could be
     769                 :            :          * optimized by adding the STA entry earlier.
     770                 :            :          */
     771         [ +  + ]:         32 :         if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
     772                 :         32 :                              sm->PTK.tk1, klen))
     773                 :          8 :                 return;
     774                 :            : 
     775                 :            :         /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
     776                 :         32 :         sm->pairwise_set = TRUE;
     777                 :            : }
     778                 :            : 
     779                 :            : 
     780                 :         16 : static u16 wpa_ft_process_auth_req(struct wpa_state_machine *sm,
     781                 :            :                                    const u8 *ies, size_t ies_len,
     782                 :            :                                    u8 **resp_ies, size_t *resp_ies_len)
     783                 :            : {
     784                 :            :         struct rsn_mdie *mdie;
     785                 :            :         struct rsn_ftie *ftie;
     786                 :            :         u8 pmk_r1[PMK_LEN], pmk_r1_name[WPA_PMK_NAME_LEN];
     787                 :            :         u8 ptk_name[WPA_PMK_NAME_LEN];
     788                 :            :         struct wpa_auth_config *conf;
     789                 :            :         struct wpa_ft_ies parse;
     790                 :            :         size_t buflen, ptk_len;
     791                 :            :         int ret;
     792                 :            :         u8 *pos, *end;
     793                 :            :         int pairwise;
     794                 :            : 
     795                 :         16 :         *resp_ies = NULL;
     796                 :         16 :         *resp_ies_len = 0;
     797                 :            : 
     798                 :         16 :         sm->pmk_r1_name_valid = 0;
     799                 :         16 :         conf = &sm->wpa_auth->conf;
     800                 :            : 
     801                 :         16 :         wpa_hexdump(MSG_DEBUG, "FT: Received authentication frame IEs",
     802                 :            :                     ies, ies_len);
     803                 :            : 
     804         [ -  + ]:         16 :         if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
     805                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: Failed to parse FT IEs");
     806                 :          0 :                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
     807                 :            :         }
     808                 :            : 
     809                 :         16 :         mdie = (struct rsn_mdie *) parse.mdie;
     810 [ +  - ][ +  - ]:         16 :         if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
                 [ -  + ]
     811                 :         16 :             os_memcmp(mdie->mobility_domain,
     812                 :            :                       sm->wpa_auth->conf.mobility_domain,
     813                 :            :                       MOBILITY_DOMAIN_ID_LEN) != 0) {
     814                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
     815                 :          0 :                 return WLAN_STATUS_INVALID_MDIE;
     816                 :            :         }
     817                 :            : 
     818                 :         16 :         ftie = (struct rsn_ftie *) parse.ftie;
     819 [ +  - ][ -  + ]:         16 :         if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
     820                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
     821                 :          0 :                 return WLAN_STATUS_INVALID_FTIE;
     822                 :            :         }
     823                 :            : 
     824                 :         16 :         os_memcpy(sm->SNonce, ftie->snonce, WPA_NONCE_LEN);
     825                 :            : 
     826         [ -  + ]:         16 :         if (parse.r0kh_id == NULL) {
     827                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: Invalid FTIE - no R0KH-ID");
     828                 :          0 :                 return WLAN_STATUS_INVALID_FTIE;
     829                 :            :         }
     830                 :            : 
     831                 :         16 :         wpa_hexdump(MSG_DEBUG, "FT: STA R0KH-ID",
     832                 :         16 :                     parse.r0kh_id, parse.r0kh_id_len);
     833                 :         16 :         os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len);
     834                 :         16 :         sm->r0kh_id_len = parse.r0kh_id_len;
     835                 :            : 
     836         [ -  + ]:         16 :         if (parse.rsn_pmkid == NULL) {
     837                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: No PMKID in RSNIE");
     838                 :          0 :                 return WLAN_STATUS_INVALID_PMKID;
     839                 :            :         }
     840                 :            : 
     841                 :         16 :         wpa_hexdump(MSG_DEBUG, "FT: Requested PMKR0Name",
     842                 :         16 :                     parse.rsn_pmkid, WPA_PMK_NAME_LEN);
     843                 :         16 :         wpa_derive_pmk_r1_name(parse.rsn_pmkid,
     844                 :         16 :                                sm->wpa_auth->conf.r1_key_holder, sm->addr,
     845                 :            :                                pmk_r1_name);
     846                 :         16 :         wpa_hexdump(MSG_DEBUG, "FT: Derived requested PMKR1Name",
     847                 :            :                     pmk_r1_name, WPA_PMK_NAME_LEN);
     848                 :            : 
     849         [ -  + ]:         16 :         if (wpa_ft_fetch_pmk_r1(sm->wpa_auth, sm->addr, pmk_r1_name, pmk_r1,
     850                 :            :                     &pairwise) < 0) {
     851         [ #  # ]:          0 :                 if (wpa_ft_pull_pmk_r1(sm->wpa_auth, sm->addr, sm->r0kh_id,
     852                 :            :                                        sm->r0kh_id_len, parse.rsn_pmkid) < 0) {
     853                 :          0 :                         wpa_printf(MSG_DEBUG, "FT: Did not have matching "
     854                 :            :                                    "PMK-R1 and unknown R0KH-ID");
     855                 :          0 :                         return WLAN_STATUS_INVALID_PMKID;
     856                 :            :                 }
     857                 :            : 
     858                 :            :                 /*
     859                 :            :                  * TODO: Should return "status pending" (and the caller should
     860                 :            :                  * not send out response now). The real response will be sent
     861                 :            :                  * once the response from R0KH is received.
     862                 :            :                  */
     863                 :          0 :                 return WLAN_STATUS_INVALID_PMKID;
     864                 :            :         }
     865                 :            : 
     866                 :         16 :         wpa_hexdump_key(MSG_DEBUG, "FT: Selected PMK-R1", pmk_r1, PMK_LEN);
     867                 :         16 :         sm->pmk_r1_name_valid = 1;
     868                 :         16 :         os_memcpy(sm->pmk_r1_name, pmk_r1_name, WPA_PMK_NAME_LEN);
     869                 :            : 
     870         [ -  + ]:         16 :         if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
     871                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: Failed to get random data for "
     872                 :            :                            "ANonce");
     873                 :          0 :                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
     874                 :            :         }
     875                 :            : 
     876                 :         16 :         wpa_hexdump(MSG_DEBUG, "FT: Received SNonce",
     877                 :         16 :                     sm->SNonce, WPA_NONCE_LEN);
     878                 :         16 :         wpa_hexdump(MSG_DEBUG, "FT: Generated ANonce",
     879                 :         16 :                     sm->ANonce, WPA_NONCE_LEN);
     880                 :            : 
     881         [ -  + ]:         16 :         ptk_len = pairwise == WPA_CIPHER_TKIP ? 64 : 48;
     882                 :         16 :         wpa_pmk_r1_to_ptk(pmk_r1, sm->SNonce, sm->ANonce, sm->addr,
     883                 :         16 :                           sm->wpa_auth->addr, pmk_r1_name,
     884                 :         16 :                           (u8 *) &sm->PTK, ptk_len, ptk_name);
     885                 :         16 :         wpa_hexdump_key(MSG_DEBUG, "FT: PTK",
     886                 :         16 :                         (u8 *) &sm->PTK, ptk_len);
     887                 :         16 :         wpa_hexdump(MSG_DEBUG, "FT: PTKName", ptk_name, WPA_PMK_NAME_LEN);
     888                 :            : 
     889                 :         16 :         sm->pairwise = pairwise;
     890                 :         16 :         wpa_ft_install_ptk(sm);
     891                 :            : 
     892                 :         16 :         buflen = 2 + sizeof(struct rsn_mdie) + 2 + sizeof(struct rsn_ftie) +
     893                 :            :                 2 + FT_R1KH_ID_LEN + 200;
     894                 :         16 :         *resp_ies = os_zalloc(buflen);
     895         [ -  + ]:         16 :         if (*resp_ies == NULL) {
     896                 :          0 :                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
     897                 :            :         }
     898                 :            : 
     899                 :         16 :         pos = *resp_ies;
     900                 :         16 :         end = *resp_ies + buflen;
     901                 :            : 
     902                 :         16 :         ret = wpa_write_rsn_ie(conf, pos, end - pos, parse.rsn_pmkid);
     903         [ -  + ]:         16 :         if (ret < 0) {
     904                 :          0 :                 os_free(*resp_ies);
     905                 :          0 :                 *resp_ies = NULL;
     906                 :          0 :                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
     907                 :            :         }
     908                 :         16 :         pos += ret;
     909                 :            : 
     910                 :         16 :         ret = wpa_write_mdie(conf, pos, end - pos);
     911         [ -  + ]:         16 :         if (ret < 0) {
     912                 :          0 :                 os_free(*resp_ies);
     913                 :          0 :                 *resp_ies = NULL;
     914                 :          0 :                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
     915                 :            :         }
     916                 :         16 :         pos += ret;
     917                 :            : 
     918                 :         16 :         ret = wpa_write_ftie(conf, parse.r0kh_id, parse.r0kh_id_len,
     919                 :         32 :                              sm->ANonce, sm->SNonce, pos, end - pos, NULL, 0);
     920         [ -  + ]:         16 :         if (ret < 0) {
     921                 :          0 :                 os_free(*resp_ies);
     922                 :          0 :                 *resp_ies = NULL;
     923                 :          0 :                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
     924                 :            :         }
     925                 :         16 :         pos += ret;
     926                 :            : 
     927                 :         16 :         *resp_ies_len = pos - *resp_ies;
     928                 :            : 
     929                 :         16 :         return WLAN_STATUS_SUCCESS;
     930                 :            : }
     931                 :            : 
     932                 :            : 
     933                 :         10 : void wpa_ft_process_auth(struct wpa_state_machine *sm, const u8 *bssid,
     934                 :            :                          u16 auth_transaction, const u8 *ies, size_t ies_len,
     935                 :            :                          void (*cb)(void *ctx, const u8 *dst, const u8 *bssid,
     936                 :            :                                     u16 auth_transaction, u16 status,
     937                 :            :                                     const u8 *ies, size_t ies_len),
     938                 :            :                          void *ctx)
     939                 :            : {
     940                 :            :         u16 status;
     941                 :            :         u8 *resp_ies;
     942                 :            :         size_t resp_ies_len;
     943                 :            : 
     944         [ -  + ]:         10 :         if (sm == NULL) {
     945                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: Received authentication frame, but "
     946                 :            :                            "WPA SM not available");
     947                 :         10 :                 return;
     948                 :            :         }
     949                 :            : 
     950                 :         10 :         wpa_printf(MSG_DEBUG, "FT: Received authentication frame: STA=" MACSTR
     951                 :            :                    " BSSID=" MACSTR " transaction=%d",
     952                 :        120 :                    MAC2STR(sm->addr), MAC2STR(bssid), auth_transaction);
     953                 :         10 :         status = wpa_ft_process_auth_req(sm, ies, ies_len, &resp_ies,
     954                 :            :                                          &resp_ies_len);
     955                 :            : 
     956                 :         10 :         wpa_printf(MSG_DEBUG, "FT: FT authentication response: dst=" MACSTR
     957                 :            :                    " auth_transaction=%d status=%d",
     958                 :         60 :                    MAC2STR(sm->addr), auth_transaction + 1, status);
     959                 :         10 :         wpa_hexdump(MSG_DEBUG, "FT: Response IEs", resp_ies, resp_ies_len);
     960                 :         10 :         cb(ctx, sm->addr, bssid, auth_transaction + 1, status,
     961                 :            :            resp_ies, resp_ies_len);
     962                 :         10 :         os_free(resp_ies);
     963                 :            : }
     964                 :            : 
     965                 :            : 
     966                 :         16 : u16 wpa_ft_validate_reassoc(struct wpa_state_machine *sm, const u8 *ies,
     967                 :            :                             size_t ies_len)
     968                 :            : {
     969                 :            :         struct wpa_ft_ies parse;
     970                 :            :         struct rsn_mdie *mdie;
     971                 :            :         struct rsn_ftie *ftie;
     972                 :            :         u8 mic[16];
     973                 :            :         unsigned int count;
     974                 :            : 
     975         [ -  + ]:         16 :         if (sm == NULL)
     976                 :          0 :                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
     977                 :            : 
     978                 :         16 :         wpa_hexdump(MSG_DEBUG, "FT: Reassoc Req IEs", ies, ies_len);
     979                 :            : 
     980         [ -  + ]:         16 :         if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
     981                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: Failed to parse FT IEs");
     982                 :          0 :                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
     983                 :            :         }
     984                 :            : 
     985         [ -  + ]:         16 :         if (parse.rsn == NULL) {
     986                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: No RSNIE in Reassoc Req");
     987                 :          0 :                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
     988                 :            :         }
     989                 :            : 
     990         [ -  + ]:         16 :         if (parse.rsn_pmkid == NULL) {
     991                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: No PMKID in RSNIE");
     992                 :          0 :                 return WLAN_STATUS_INVALID_PMKID;
     993                 :            :         }
     994                 :            : 
     995         [ -  + ]:         16 :         if (os_memcmp(parse.rsn_pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
     996                 :            :         {
     997                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: PMKID in Reassoc Req did not match "
     998                 :            :                            "with the PMKR1Name derived from auth request");
     999                 :          0 :                 return WLAN_STATUS_INVALID_PMKID;
    1000                 :            :         }
    1001                 :            : 
    1002                 :         16 :         mdie = (struct rsn_mdie *) parse.mdie;
    1003 [ +  - ][ +  - ]:         16 :         if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
                 [ -  + ]
    1004                 :         16 :             os_memcmp(mdie->mobility_domain,
    1005                 :            :                       sm->wpa_auth->conf.mobility_domain,
    1006                 :            :                       MOBILITY_DOMAIN_ID_LEN) != 0) {
    1007                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
    1008                 :          0 :                 return WLAN_STATUS_INVALID_MDIE;
    1009                 :            :         }
    1010                 :            : 
    1011                 :         16 :         ftie = (struct rsn_ftie *) parse.ftie;
    1012 [ +  - ][ -  + ]:         16 :         if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
    1013                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
    1014                 :          0 :                 return WLAN_STATUS_INVALID_FTIE;
    1015                 :            :         }
    1016                 :            : 
    1017         [ -  + ]:         16 :         if (os_memcmp(ftie->snonce, sm->SNonce, WPA_NONCE_LEN) != 0) {
    1018                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: SNonce mismatch in FTIE");
    1019                 :          0 :                 wpa_hexdump(MSG_DEBUG, "FT: Received SNonce",
    1020                 :          0 :                             ftie->snonce, WPA_NONCE_LEN);
    1021                 :          0 :                 wpa_hexdump(MSG_DEBUG, "FT: Expected SNonce",
    1022                 :          0 :                             sm->SNonce, WPA_NONCE_LEN);
    1023                 :          0 :                 return -1;
    1024                 :            :         }
    1025                 :            : 
    1026         [ -  + ]:         16 :         if (os_memcmp(ftie->anonce, sm->ANonce, WPA_NONCE_LEN) != 0) {
    1027                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: ANonce mismatch in FTIE");
    1028                 :          0 :                 wpa_hexdump(MSG_DEBUG, "FT: Received ANonce",
    1029                 :          0 :                             ftie->anonce, WPA_NONCE_LEN);
    1030                 :          0 :                 wpa_hexdump(MSG_DEBUG, "FT: Expected ANonce",
    1031                 :          0 :                             sm->ANonce, WPA_NONCE_LEN);
    1032                 :          0 :                 return -1;
    1033                 :            :         }
    1034                 :            : 
    1035                 :            : 
    1036         [ -  + ]:         16 :         if (parse.r0kh_id == NULL) {
    1037                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: No R0KH-ID subelem in FTIE");
    1038                 :          0 :                 return -1;
    1039                 :            :         }
    1040                 :            : 
    1041 [ +  - ][ -  + ]:         16 :         if (parse.r0kh_id_len != sm->r0kh_id_len ||
    1042                 :         16 :             os_memcmp(parse.r0kh_id, sm->r0kh_id, parse.r0kh_id_len) != 0) {
    1043                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: R0KH-ID in FTIE did not match with "
    1044                 :            :                            "the current R0KH-ID");
    1045                 :          0 :                 wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID in FTIE",
    1046                 :          0 :                             parse.r0kh_id, parse.r0kh_id_len);
    1047                 :          0 :                 wpa_hexdump(MSG_DEBUG, "FT: The current R0KH-ID",
    1048                 :          0 :                             sm->r0kh_id, sm->r0kh_id_len);
    1049                 :          0 :                 return -1;
    1050                 :            :         }
    1051                 :            : 
    1052         [ -  + ]:         16 :         if (parse.r1kh_id == NULL) {
    1053                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: No R1KH-ID subelem in FTIE");
    1054                 :          0 :                 return -1;
    1055                 :            :         }
    1056                 :            : 
    1057         [ -  + ]:         16 :         if (os_memcmp(parse.r1kh_id, sm->wpa_auth->conf.r1_key_holder,
    1058                 :            :                       FT_R1KH_ID_LEN) != 0) {
    1059                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: Unknown R1KH-ID used in "
    1060                 :            :                            "ReassocReq");
    1061                 :          0 :                 wpa_hexdump(MSG_DEBUG, "FT: R1KH-ID in FTIE",
    1062                 :          0 :                             parse.r1kh_id, FT_R1KH_ID_LEN);
    1063                 :          0 :                 wpa_hexdump(MSG_DEBUG, "FT: Expected R1KH-ID",
    1064                 :          0 :                             sm->wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN);
    1065                 :          0 :                 return -1;
    1066                 :            :         }
    1067                 :            : 
    1068 [ +  - ][ -  + ]:         16 :         if (parse.rsn_pmkid == NULL ||
    1069                 :         16 :             os_memcmp(parse.rsn_pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN)) {
    1070                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: No matching PMKR1Name (PMKID) in "
    1071                 :          0 :                            "RSNIE (pmkid=%d)", !!parse.rsn_pmkid);
    1072                 :          0 :                 return -1;
    1073                 :            :         }
    1074                 :            : 
    1075                 :         16 :         count = 3;
    1076         [ -  + ]:         16 :         if (parse.ric)
    1077                 :          0 :                 count += ieee802_11_ie_count(parse.ric, parse.ric_len);
    1078         [ -  + ]:         16 :         if (ftie->mic_control[1] != count) {
    1079                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: Unexpected IE count in MIC "
    1080                 :            :                            "Control: received %u expected %u",
    1081                 :          0 :                            ftie->mic_control[1], count);
    1082                 :          0 :                 return -1;
    1083                 :            :         }
    1084                 :            : 
    1085         [ -  + ]:         16 :         if (wpa_ft_mic(sm->PTK.kck, sm->addr, sm->wpa_auth->addr, 5,
    1086                 :         32 :                        parse.mdie - 2, parse.mdie_len + 2,
    1087                 :         32 :                        parse.ftie - 2, parse.ftie_len + 2,
    1088                 :         32 :                        parse.rsn - 2, parse.rsn_len + 2,
    1089                 :            :                        parse.ric, parse.ric_len,
    1090                 :            :                        mic) < 0) {
    1091                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
    1092                 :          0 :                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
    1093                 :            :         }
    1094                 :            : 
    1095         [ -  + ]:         16 :         if (os_memcmp(mic, ftie->mic, 16) != 0) {
    1096                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: Invalid MIC in FTIE");
    1097                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: addr=" MACSTR " auth_addr=" MACSTR,
    1098                 :          0 :                            MAC2STR(sm->addr), MAC2STR(sm->wpa_auth->addr));
    1099                 :          0 :                 wpa_hexdump(MSG_MSGDUMP, "FT: Received MIC", ftie->mic, 16);
    1100                 :          0 :                 wpa_hexdump(MSG_MSGDUMP, "FT: Calculated MIC", mic, 16);
    1101                 :          0 :                 wpa_hexdump(MSG_MSGDUMP, "FT: MDIE",
    1102                 :          0 :                             parse.mdie - 2, parse.mdie_len + 2);
    1103                 :          0 :                 wpa_hexdump(MSG_MSGDUMP, "FT: FTIE",
    1104                 :          0 :                             parse.ftie - 2, parse.ftie_len + 2);
    1105                 :          0 :                 wpa_hexdump(MSG_MSGDUMP, "FT: RSN",
    1106                 :          0 :                             parse.rsn - 2, parse.rsn_len + 2);
    1107                 :          0 :                 return WLAN_STATUS_INVALID_FTIE;
    1108                 :            :         }
    1109                 :            : 
    1110                 :         16 :         return WLAN_STATUS_SUCCESS;
    1111                 :            : }
    1112                 :            : 
    1113                 :            : 
    1114                 :          6 : int wpa_ft_action_rx(struct wpa_state_machine *sm, const u8 *data, size_t len)
    1115                 :            : {
    1116                 :            :         const u8 *sta_addr, *target_ap;
    1117                 :            :         const u8 *ies;
    1118                 :            :         size_t ies_len;
    1119                 :            :         u8 action;
    1120                 :            :         struct ft_rrb_frame *frame;
    1121                 :            : 
    1122         [ -  + ]:          6 :         if (sm == NULL)
    1123                 :          0 :                 return -1;
    1124                 :            : 
    1125                 :            :         /*
    1126                 :            :          * data: Category[1] Action[1] STA_Address[6] Target_AP_Address[6]
    1127                 :            :          * FT Request action frame body[variable]
    1128                 :            :          */
    1129                 :            : 
    1130         [ -  + ]:          6 :         if (len < 14) {
    1131                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: Too short FT Action frame "
    1132                 :            :                            "(len=%lu)", (unsigned long) len);
    1133                 :          0 :                 return -1;
    1134                 :            :         }
    1135                 :            : 
    1136                 :          6 :         action = data[1];
    1137                 :          6 :         sta_addr = data + 2;
    1138                 :          6 :         target_ap = data + 8;
    1139                 :          6 :         ies = data + 14;
    1140                 :          6 :         ies_len = len - 14;
    1141                 :            : 
    1142                 :          6 :         wpa_printf(MSG_DEBUG, "FT: Received FT Action frame (STA=" MACSTR
    1143                 :            :                    " Target AP=" MACSTR " Action=%d)",
    1144                 :         72 :                    MAC2STR(sta_addr), MAC2STR(target_ap), action);
    1145                 :            : 
    1146         [ -  + ]:          6 :         if (os_memcmp(sta_addr, sm->addr, ETH_ALEN) != 0) {
    1147                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: Mismatch in FT Action STA address: "
    1148                 :            :                            "STA=" MACSTR " STA-Address=" MACSTR,
    1149                 :          0 :                            MAC2STR(sm->addr), MAC2STR(sta_addr));
    1150                 :          0 :                 return -1;
    1151                 :            :         }
    1152                 :            : 
    1153                 :            :         /*
    1154                 :            :          * Do some sanity checking on the target AP address (not own and not
    1155                 :            :          * broadcast. This could be extended to filter based on a list of known
    1156                 :            :          * APs in the MD (if such a list were configured).
    1157                 :            :          */
    1158 [ +  - ][ -  + ]:          6 :         if ((target_ap[0] & 0x01) ||
    1159                 :          6 :             os_memcmp(target_ap, sm->wpa_auth->addr, ETH_ALEN) == 0) {
    1160                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: Invalid Target AP in FT Action "
    1161                 :            :                            "frame");
    1162                 :          0 :                 return -1;
    1163                 :            :         }
    1164                 :            : 
    1165                 :          6 :         wpa_hexdump(MSG_MSGDUMP, "FT: Action frame body", ies, ies_len);
    1166                 :            : 
    1167                 :            :         /* RRB - Forward action frame to the target AP */
    1168                 :          6 :         frame = os_malloc(sizeof(*frame) + len);
    1169         [ -  + ]:          6 :         if (frame == NULL)
    1170                 :          0 :                 return -1;
    1171                 :          6 :         frame->frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
    1172                 :          6 :         frame->packet_type = FT_PACKET_REQUEST;
    1173                 :          6 :         frame->action_length = host_to_le16(len);
    1174                 :          6 :         os_memcpy(frame->ap_address, sm->wpa_auth->addr, ETH_ALEN);
    1175                 :          6 :         os_memcpy(frame + 1, data, len);
    1176                 :            : 
    1177                 :          6 :         wpa_ft_rrb_send(sm->wpa_auth, target_ap, (u8 *) frame,
    1178                 :            :                         sizeof(*frame) + len);
    1179                 :          6 :         os_free(frame);
    1180                 :            : 
    1181                 :          6 :         return 0;
    1182                 :            : }
    1183                 :            : 
    1184                 :            : 
    1185                 :          6 : static int wpa_ft_rrb_rx_request(struct wpa_authenticator *wpa_auth,
    1186                 :            :                                  const u8 *current_ap, const u8 *sta_addr,
    1187                 :            :                                  const u8 *body, size_t len)
    1188                 :            : {
    1189                 :            :         struct wpa_state_machine *sm;
    1190                 :            :         u16 status;
    1191                 :            :         u8 *resp_ies, *pos;
    1192                 :            :         size_t resp_ies_len, rlen;
    1193                 :            :         struct ft_rrb_frame *frame;
    1194                 :            : 
    1195                 :          6 :         sm = wpa_ft_add_sta(wpa_auth, sta_addr);
    1196         [ -  + ]:          6 :         if (sm == NULL) {
    1197                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: Failed to add new STA based on "
    1198                 :            :                            "RRB Request");
    1199                 :          0 :                 return -1;
    1200                 :            :         }
    1201                 :            : 
    1202                 :          6 :         wpa_hexdump(MSG_MSGDUMP, "FT: RRB Request Frame body", body, len);
    1203                 :            : 
    1204                 :          6 :         status = wpa_ft_process_auth_req(sm, body, len, &resp_ies,
    1205                 :            :                                          &resp_ies_len);
    1206                 :            : 
    1207                 :          6 :         wpa_printf(MSG_DEBUG, "FT: RRB authentication response: STA=" MACSTR
    1208                 :            :                    " CurrentAP=" MACSTR " status=%d",
    1209                 :         72 :                    MAC2STR(sm->addr), MAC2STR(current_ap), status);
    1210                 :          6 :         wpa_hexdump(MSG_DEBUG, "FT: Response IEs", resp_ies, resp_ies_len);
    1211                 :            : 
    1212                 :            :         /* RRB - Forward action frame response to the Current AP */
    1213                 :            : 
    1214                 :            :         /*
    1215                 :            :          * data: Category[1] Action[1] STA_Address[6] Target_AP_Address[6]
    1216                 :            :          * Status_Code[2] FT Request action frame body[variable]
    1217                 :            :          */
    1218                 :          6 :         rlen = 2 + 2 * ETH_ALEN + 2 + resp_ies_len;
    1219                 :            : 
    1220                 :          6 :         frame = os_malloc(sizeof(*frame) + rlen);
    1221         [ -  + ]:          6 :         if (frame == NULL) {
    1222                 :          0 :                 os_free(resp_ies);
    1223                 :          0 :                 return -1;
    1224                 :            :         }
    1225                 :          6 :         frame->frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
    1226                 :          6 :         frame->packet_type = FT_PACKET_RESPONSE;
    1227                 :          6 :         frame->action_length = host_to_le16(rlen);
    1228                 :          6 :         os_memcpy(frame->ap_address, wpa_auth->addr, ETH_ALEN);
    1229                 :          6 :         pos = (u8 *) (frame + 1);
    1230                 :          6 :         *pos++ = WLAN_ACTION_FT;
    1231                 :          6 :         *pos++ = 2; /* Action: Response */
    1232                 :          6 :         os_memcpy(pos, sta_addr, ETH_ALEN);
    1233                 :          6 :         pos += ETH_ALEN;
    1234                 :          6 :         os_memcpy(pos, wpa_auth->addr, ETH_ALEN);
    1235                 :          6 :         pos += ETH_ALEN;
    1236                 :          6 :         WPA_PUT_LE16(pos, status);
    1237                 :          6 :         pos += 2;
    1238         [ +  - ]:          6 :         if (resp_ies) {
    1239                 :          6 :                 os_memcpy(pos, resp_ies, resp_ies_len);
    1240                 :          6 :                 os_free(resp_ies);
    1241                 :            :         }
    1242                 :            : 
    1243                 :          6 :         wpa_ft_rrb_send(wpa_auth, current_ap, (u8 *) frame,
    1244                 :            :                         sizeof(*frame) + rlen);
    1245                 :          6 :         os_free(frame);
    1246                 :            : 
    1247                 :          6 :         return 0;
    1248                 :            : }
    1249                 :            : 
    1250                 :            : 
    1251                 :          0 : static int wpa_ft_rrb_rx_pull(struct wpa_authenticator *wpa_auth,
    1252                 :            :                               const u8 *src_addr,
    1253                 :            :                               const u8 *data, size_t data_len)
    1254                 :            : {
    1255                 :            :         struct ft_r0kh_r1kh_pull_frame *frame, f;
    1256                 :            :         struct ft_remote_r1kh *r1kh;
    1257                 :            :         struct ft_r0kh_r1kh_resp_frame resp, r;
    1258                 :            :         u8 pmk_r0[PMK_LEN];
    1259                 :            :         int pairwise;
    1260                 :            : 
    1261                 :          0 :         wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 pull");
    1262                 :            : 
    1263         [ #  # ]:          0 :         if (data_len < sizeof(*frame))
    1264                 :          0 :                 return -1;
    1265                 :            : 
    1266                 :          0 :         r1kh = wpa_auth->conf.r1kh_list;
    1267         [ #  # ]:          0 :         while (r1kh) {
    1268         [ #  # ]:          0 :                 if (os_memcmp(r1kh->addr, src_addr, ETH_ALEN) == 0)
    1269                 :          0 :                         break;
    1270                 :          0 :                 r1kh = r1kh->next;
    1271                 :            :         }
    1272         [ #  # ]:          0 :         if (r1kh == NULL) {
    1273                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: No matching R1KH address found for "
    1274                 :            :                            "PMK-R1 pull source address " MACSTR,
    1275                 :          0 :                            MAC2STR(src_addr));
    1276                 :          0 :                 return -1;
    1277                 :            :         }
    1278                 :            : 
    1279                 :          0 :         frame = (struct ft_r0kh_r1kh_pull_frame *) data;
    1280                 :            :         /* aes_unwrap() does not support inplace decryption, so use a temporary
    1281                 :            :          * buffer for the data. */
    1282         [ #  # ]:          0 :         if (aes_unwrap(r1kh->key, (FT_R0KH_R1KH_PULL_DATA_LEN + 7) / 8,
    1283                 :          0 :                        frame->nonce, f.nonce) < 0) {
    1284                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 pull "
    1285                 :          0 :                            "request from " MACSTR, MAC2STR(src_addr));
    1286                 :          0 :                 return -1;
    1287                 :            :         }
    1288                 :            : 
    1289                 :          0 :         wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - nonce",
    1290                 :            :                     f.nonce, sizeof(f.nonce));
    1291                 :          0 :         wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - PMKR0Name",
    1292                 :            :                     f.pmk_r0_name, WPA_PMK_NAME_LEN);
    1293                 :          0 :         wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull - R1KH-ID=" MACSTR "S1KH-ID="
    1294                 :          0 :                    MACSTR, MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id));
    1295                 :            : 
    1296                 :          0 :         os_memset(&resp, 0, sizeof(resp));
    1297                 :          0 :         resp.frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
    1298                 :          0 :         resp.packet_type = FT_PACKET_R0KH_R1KH_RESP;
    1299                 :          0 :         resp.data_length = host_to_le16(FT_R0KH_R1KH_RESP_DATA_LEN);
    1300                 :          0 :         os_memcpy(resp.ap_address, wpa_auth->addr, ETH_ALEN);
    1301                 :            : 
    1302                 :            :         /* aes_wrap() does not support inplace encryption, so use a temporary
    1303                 :            :          * buffer for the data. */
    1304                 :          0 :         os_memcpy(r.nonce, f.nonce, sizeof(f.nonce));
    1305                 :          0 :         os_memcpy(r.r1kh_id, f.r1kh_id, FT_R1KH_ID_LEN);
    1306                 :          0 :         os_memcpy(r.s1kh_id, f.s1kh_id, ETH_ALEN);
    1307         [ #  # ]:          0 :         if (wpa_ft_fetch_pmk_r0(wpa_auth, f.s1kh_id, f.pmk_r0_name, pmk_r0,
    1308                 :            :                                 &pairwise) < 0) {
    1309                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: No matching PMKR0Name found for "
    1310                 :            :                            "PMK-R1 pull");
    1311                 :          0 :                 return -1;
    1312                 :            :         }
    1313                 :            : 
    1314                 :          0 :         wpa_derive_pmk_r1(pmk_r0, f.pmk_r0_name, f.r1kh_id, f.s1kh_id,
    1315                 :            :                           r.pmk_r1, r.pmk_r1_name);
    1316                 :          0 :         wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", r.pmk_r1, PMK_LEN);
    1317                 :          0 :         wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", r.pmk_r1_name,
    1318                 :            :                     WPA_PMK_NAME_LEN);
    1319                 :          0 :         r.pairwise = host_to_le16(pairwise);
    1320                 :          0 :         os_memset(r.pad, 0, sizeof(r.pad));
    1321                 :            : 
    1322         [ #  # ]:          0 :         if (aes_wrap(r1kh->key, (FT_R0KH_R1KH_RESP_DATA_LEN + 7) / 8,
    1323                 :            :                      r.nonce, resp.nonce) < 0) {
    1324                 :          0 :                 os_memset(pmk_r0, 0, PMK_LEN);
    1325                 :          0 :                 return -1;
    1326                 :            :         }
    1327                 :            : 
    1328                 :          0 :         os_memset(pmk_r0, 0, PMK_LEN);
    1329                 :            : 
    1330                 :          0 :         wpa_ft_rrb_send(wpa_auth, src_addr, (u8 *) &resp, sizeof(resp));
    1331                 :            : 
    1332                 :          0 :         return 0;
    1333                 :            : }
    1334                 :            : 
    1335                 :            : 
    1336                 :          0 : static int wpa_ft_rrb_rx_resp(struct wpa_authenticator *wpa_auth,
    1337                 :            :                               const u8 *src_addr,
    1338                 :            :                               const u8 *data, size_t data_len)
    1339                 :            : {
    1340                 :            :         struct ft_r0kh_r1kh_resp_frame *frame, f;
    1341                 :            :         struct ft_remote_r0kh *r0kh;
    1342                 :            :         int pairwise;
    1343                 :            : 
    1344                 :          0 :         wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 pull response");
    1345                 :            : 
    1346         [ #  # ]:          0 :         if (data_len < sizeof(*frame))
    1347                 :          0 :                 return -1;
    1348                 :            : 
    1349                 :          0 :         r0kh = wpa_auth->conf.r0kh_list;
    1350         [ #  # ]:          0 :         while (r0kh) {
    1351         [ #  # ]:          0 :                 if (os_memcmp(r0kh->addr, src_addr, ETH_ALEN) == 0)
    1352                 :          0 :                         break;
    1353                 :          0 :                 r0kh = r0kh->next;
    1354                 :            :         }
    1355         [ #  # ]:          0 :         if (r0kh == NULL) {
    1356                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: No matching R0KH address found for "
    1357                 :            :                            "PMK-R0 pull response source address " MACSTR,
    1358                 :          0 :                            MAC2STR(src_addr));
    1359                 :          0 :                 return -1;
    1360                 :            :         }
    1361                 :            : 
    1362                 :          0 :         frame = (struct ft_r0kh_r1kh_resp_frame *) data;
    1363                 :            :         /* aes_unwrap() does not support inplace decryption, so use a temporary
    1364                 :            :          * buffer for the data. */
    1365         [ #  # ]:          0 :         if (aes_unwrap(r0kh->key, (FT_R0KH_R1KH_RESP_DATA_LEN + 7) / 8,
    1366                 :          0 :                        frame->nonce, f.nonce) < 0) {
    1367                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 pull "
    1368                 :          0 :                            "response from " MACSTR, MAC2STR(src_addr));
    1369                 :          0 :                 return -1;
    1370                 :            :         }
    1371                 :            : 
    1372         [ #  # ]:          0 :         if (os_memcmp(f.r1kh_id, wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN)
    1373                 :            :             != 0) {
    1374                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull response did not use a "
    1375                 :            :                            "matching R1KH-ID");
    1376                 :          0 :                 return -1;
    1377                 :            :         }
    1378                 :            : 
    1379                 :            :         /* TODO: verify that <nonce,s1kh_id> matches with a pending request
    1380                 :            :          * and call this requests callback function to finish request
    1381                 :            :          * processing */
    1382                 :            : 
    1383                 :          0 :         pairwise = le_to_host16(f.pairwise);
    1384                 :          0 :         wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - nonce",
    1385                 :            :                     f.nonce, sizeof(f.nonce));
    1386                 :          0 :         wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull - R1KH-ID=" MACSTR "S1KH-ID="
    1387                 :            :                    MACSTR " pairwise=0x%x",
    1388                 :          0 :                    MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id), pairwise);
    1389                 :          0 :         wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1 pull - PMK-R1",
    1390                 :            :                         f.pmk_r1, PMK_LEN);
    1391                 :          0 :         wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - PMKR1Name",
    1392                 :            :                         f.pmk_r1_name, WPA_PMK_NAME_LEN);
    1393                 :            : 
    1394                 :          0 :         wpa_ft_store_pmk_r1(wpa_auth, f.s1kh_id, f.pmk_r1, f.pmk_r1_name,
    1395                 :            :                             pairwise);
    1396                 :          0 :         os_memset(f.pmk_r1, 0, PMK_LEN);
    1397                 :            : 
    1398                 :          0 :         return 0;
    1399                 :            : }
    1400                 :            : 
    1401                 :            : 
    1402                 :          8 : static int wpa_ft_rrb_rx_push(struct wpa_authenticator *wpa_auth,
    1403                 :            :                               const u8 *src_addr,
    1404                 :            :                               const u8 *data, size_t data_len)
    1405                 :            : {
    1406                 :            :         struct ft_r0kh_r1kh_push_frame *frame, f;
    1407                 :            :         struct ft_remote_r0kh *r0kh;
    1408                 :            :         struct os_time now;
    1409                 :            :         os_time_t tsend;
    1410                 :            :         int pairwise;
    1411                 :            : 
    1412                 :          8 :         wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 push");
    1413                 :            : 
    1414         [ -  + ]:          8 :         if (data_len < sizeof(*frame))
    1415                 :          0 :                 return -1;
    1416                 :            : 
    1417                 :          8 :         r0kh = wpa_auth->conf.r0kh_list;
    1418         [ +  - ]:         16 :         while (r0kh) {
    1419         [ +  + ]:         16 :                 if (os_memcmp(r0kh->addr, src_addr, ETH_ALEN) == 0)
    1420                 :          8 :                         break;
    1421                 :          8 :                 r0kh = r0kh->next;
    1422                 :            :         }
    1423         [ -  + ]:          8 :         if (r0kh == NULL) {
    1424                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: No matching R0KH address found for "
    1425                 :            :                            "PMK-R0 push source address " MACSTR,
    1426                 :          0 :                            MAC2STR(src_addr));
    1427                 :          0 :                 return -1;
    1428                 :            :         }
    1429                 :            : 
    1430                 :          8 :         frame = (struct ft_r0kh_r1kh_push_frame *) data;
    1431                 :            :         /* aes_unwrap() does not support inplace decryption, so use a temporary
    1432                 :            :          * buffer for the data. */
    1433         [ -  + ]:          8 :         if (aes_unwrap(r0kh->key, (FT_R0KH_R1KH_PUSH_DATA_LEN + 7) / 8,
    1434                 :          8 :                        frame->timestamp, f.timestamp) < 0) {
    1435                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 push from "
    1436                 :          0 :                            MACSTR, MAC2STR(src_addr));
    1437                 :          0 :                 return -1;
    1438                 :            :         }
    1439                 :            : 
    1440                 :          8 :         os_get_time(&now);
    1441                 :          8 :         tsend = WPA_GET_LE32(f.timestamp);
    1442 [ #  # ][ -  + ]:          8 :         if ((now.sec > tsend && now.sec - tsend > 60) ||
                 [ -  + ]
    1443         [ #  # ]:          0 :             (now.sec < tsend && tsend - now.sec > 60)) {
    1444                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: PMK-R1 push did not have a valid "
    1445                 :            :                            "timestamp: sender time %d own time %d\n",
    1446                 :          0 :                            (int) tsend, (int) now.sec);
    1447                 :          0 :                 return -1;
    1448                 :            :         }
    1449                 :            : 
    1450         [ -  + ]:          8 :         if (os_memcmp(f.r1kh_id, wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN)
    1451                 :            :             != 0) {
    1452                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: PMK-R1 push did not use a matching "
    1453                 :            :                            "R1KH-ID (received " MACSTR " own " MACSTR ")",
    1454                 :          0 :                            MAC2STR(f.r1kh_id),
    1455                 :          0 :                            MAC2STR(wpa_auth->conf.r1_key_holder));
    1456                 :          0 :                 return -1;
    1457                 :            :         }
    1458                 :            : 
    1459                 :          8 :         pairwise = le_to_host16(f.pairwise);
    1460                 :          8 :         wpa_printf(MSG_DEBUG, "FT: PMK-R1 push - R1KH-ID=" MACSTR " S1KH-ID="
    1461                 :            :                    MACSTR " pairwise=0x%x",
    1462                 :         96 :                    MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id), pairwise);
    1463                 :          8 :         wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1 push - PMK-R1",
    1464                 :            :                         f.pmk_r1, PMK_LEN);
    1465                 :          8 :         wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 push - PMKR1Name",
    1466                 :            :                         f.pmk_r1_name, WPA_PMK_NAME_LEN);
    1467                 :            : 
    1468                 :          8 :         wpa_ft_store_pmk_r1(wpa_auth, f.s1kh_id, f.pmk_r1, f.pmk_r1_name,
    1469                 :            :                             pairwise);
    1470                 :          8 :         os_memset(f.pmk_r1, 0, PMK_LEN);
    1471                 :            : 
    1472                 :          8 :         return 0;
    1473                 :            : }
    1474                 :            : 
    1475                 :            : 
    1476                 :         28 : int wpa_ft_rrb_rx(struct wpa_authenticator *wpa_auth, const u8 *src_addr,
    1477                 :            :                   const u8 *data, size_t data_len)
    1478                 :            : {
    1479                 :            :         struct ft_rrb_frame *frame;
    1480                 :            :         u16 alen;
    1481                 :            :         const u8 *pos, *end, *start;
    1482                 :            :         u8 action;
    1483                 :            :         const u8 *sta_addr, *target_ap_addr;
    1484                 :            : 
    1485                 :         28 :         wpa_printf(MSG_DEBUG, "FT: RRB received frame from remote AP " MACSTR,
    1486                 :        168 :                    MAC2STR(src_addr));
    1487                 :            : 
    1488         [ -  + ]:         28 :         if (data_len < sizeof(*frame)) {
    1489                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: Too short RRB frame (data_len=%lu)",
    1490                 :            :                            (unsigned long) data_len);
    1491                 :          0 :                 return -1;
    1492                 :            :         }
    1493                 :            : 
    1494                 :         28 :         pos = data;
    1495                 :         28 :         frame = (struct ft_rrb_frame *) pos;
    1496                 :         28 :         pos += sizeof(*frame);
    1497                 :            : 
    1498                 :         28 :         alen = le_to_host16(frame->action_length);
    1499                 :         28 :         wpa_printf(MSG_DEBUG, "FT: RRB frame - frame_type=%d packet_type=%d "
    1500                 :            :                    "action_length=%d ap_address=" MACSTR,
    1501                 :         56 :                    frame->frame_type, frame->packet_type, alen,
    1502                 :        168 :                    MAC2STR(frame->ap_address));
    1503                 :            : 
    1504         [ +  + ]:         28 :         if (frame->frame_type != RSN_REMOTE_FRAME_TYPE_FT_RRB) {
    1505                 :            :                 /* Discard frame per IEEE Std 802.11r-2008, 11A.10.3 */
    1506                 :          8 :                 wpa_printf(MSG_DEBUG, "FT: RRB discarded frame with "
    1507                 :          8 :                            "unrecognized type %d", frame->frame_type);
    1508                 :          8 :                 return -1;
    1509                 :            :         }
    1510                 :            : 
    1511         [ -  + ]:         20 :         if (alen > data_len - sizeof(*frame)) {
    1512                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: RRB frame too short for action "
    1513                 :            :                            "frame");
    1514                 :          0 :                 return -1;
    1515                 :            :         }
    1516                 :            : 
    1517         [ -  + ]:         20 :         if (frame->packet_type == FT_PACKET_R0KH_R1KH_PULL)
    1518                 :          0 :                 return wpa_ft_rrb_rx_pull(wpa_auth, src_addr, data, data_len);
    1519         [ -  + ]:         20 :         if (frame->packet_type == FT_PACKET_R0KH_R1KH_RESP)
    1520                 :          0 :                 return wpa_ft_rrb_rx_resp(wpa_auth, src_addr, data, data_len);
    1521         [ +  + ]:         20 :         if (frame->packet_type == FT_PACKET_R0KH_R1KH_PUSH)
    1522                 :          8 :                 return wpa_ft_rrb_rx_push(wpa_auth, src_addr, data, data_len);
    1523                 :            : 
    1524                 :         12 :         wpa_hexdump(MSG_MSGDUMP, "FT: RRB - FT Action frame", pos, alen);
    1525                 :            : 
    1526         [ -  + ]:         12 :         if (alen < 1 + 1 + 2 * ETH_ALEN) {
    1527                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: Too short RRB frame (not enough "
    1528                 :            :                            "room for Action Frame body); alen=%lu",
    1529                 :            :                            (unsigned long) alen);
    1530                 :          0 :                 return -1;
    1531                 :            :         }
    1532                 :         12 :         start = pos;
    1533                 :         12 :         end = pos + alen;
    1534                 :            : 
    1535         [ -  + ]:         12 :         if (*pos != WLAN_ACTION_FT) {
    1536                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: Unexpected Action frame category "
    1537                 :          0 :                            "%d", *pos);
    1538                 :          0 :                 return -1;
    1539                 :            :         }
    1540                 :            : 
    1541                 :         12 :         pos++;
    1542                 :         12 :         action = *pos++;
    1543                 :         12 :         sta_addr = pos;
    1544                 :         12 :         pos += ETH_ALEN;
    1545                 :         12 :         target_ap_addr = pos;
    1546                 :         12 :         pos += ETH_ALEN;
    1547                 :         12 :         wpa_printf(MSG_DEBUG, "FT: RRB Action Frame: action=%d sta_addr="
    1548                 :            :                    MACSTR " target_ap_addr=" MACSTR,
    1549                 :        144 :                    action, MAC2STR(sta_addr), MAC2STR(target_ap_addr));
    1550                 :            : 
    1551         [ +  + ]:         12 :         if (frame->packet_type == FT_PACKET_REQUEST) {
    1552                 :          6 :                 wpa_printf(MSG_DEBUG, "FT: FT Packet Type - Request");
    1553                 :            : 
    1554         [ -  + ]:          6 :                 if (action != 1) {
    1555                 :          0 :                         wpa_printf(MSG_DEBUG, "FT: Unexpected Action %d in "
    1556                 :            :                                    "RRB Request", action);
    1557                 :          0 :                         return -1;
    1558                 :            :                 }
    1559                 :            : 
    1560         [ -  + ]:          6 :                 if (os_memcmp(target_ap_addr, wpa_auth->addr, ETH_ALEN) != 0) {
    1561                 :          0 :                         wpa_printf(MSG_DEBUG, "FT: Target AP address in the "
    1562                 :            :                                    "RRB Request does not match with own "
    1563                 :            :                                    "address");
    1564                 :          0 :                         return -1;
    1565                 :            :                 }
    1566                 :            : 
    1567         [ -  + ]:          6 :                 if (wpa_ft_rrb_rx_request(wpa_auth, frame->ap_address,
    1568                 :          6 :                                           sta_addr, pos, end - pos) < 0)
    1569                 :          0 :                         return -1;
    1570         [ +  - ]:          6 :         } else if (frame->packet_type == FT_PACKET_RESPONSE) {
    1571                 :            :                 u16 status_code;
    1572                 :            : 
    1573         [ -  + ]:          6 :                 if (end - pos < 2) {
    1574                 :          0 :                         wpa_printf(MSG_DEBUG, "FT: Not enough room for status "
    1575                 :            :                                    "code in RRB Response");
    1576                 :          0 :                         return -1;
    1577                 :            :                 }
    1578                 :          6 :                 status_code = WPA_GET_LE16(pos);
    1579                 :          6 :                 pos += 2;
    1580                 :            : 
    1581                 :          6 :                 wpa_printf(MSG_DEBUG, "FT: FT Packet Type - Response "
    1582                 :            :                            "(status_code=%d)", status_code);
    1583                 :            : 
    1584         [ -  + ]:          6 :                 if (wpa_ft_action_send(wpa_auth, sta_addr, start, alen) < 0)
    1585                 :          0 :                         return -1;
    1586                 :            :         } else {
    1587                 :          0 :                 wpa_printf(MSG_DEBUG, "FT: RRB discarded frame with unknown "
    1588                 :          0 :                            "packet_type %d", frame->packet_type);
    1589                 :          0 :                 return -1;
    1590                 :            :         }
    1591                 :            : 
    1592                 :         28 :         return 0;
    1593                 :            : }
    1594                 :            : 
    1595                 :            : 
    1596                 :          8 : static void wpa_ft_generate_pmk_r1(struct wpa_authenticator *wpa_auth,
    1597                 :            :                                    struct wpa_ft_pmk_r0_sa *pmk_r0,
    1598                 :            :                                    struct ft_remote_r1kh *r1kh,
    1599                 :            :                                    const u8 *s1kh_id, int pairwise)
    1600                 :            : {
    1601                 :            :         struct ft_r0kh_r1kh_push_frame frame, f;
    1602                 :            :         struct os_time now;
    1603                 :            : 
    1604                 :          8 :         os_memset(&frame, 0, sizeof(frame));
    1605                 :          8 :         frame.frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
    1606                 :          8 :         frame.packet_type = FT_PACKET_R0KH_R1KH_PUSH;
    1607                 :          8 :         frame.data_length = host_to_le16(FT_R0KH_R1KH_PUSH_DATA_LEN);
    1608                 :          8 :         os_memcpy(frame.ap_address, wpa_auth->addr, ETH_ALEN);
    1609                 :            : 
    1610                 :            :         /* aes_wrap() does not support inplace encryption, so use a temporary
    1611                 :            :          * buffer for the data. */
    1612                 :          8 :         os_memcpy(f.r1kh_id, r1kh->id, FT_R1KH_ID_LEN);
    1613                 :          8 :         os_memcpy(f.s1kh_id, s1kh_id, ETH_ALEN);
    1614                 :          8 :         os_memcpy(f.pmk_r0_name, pmk_r0->pmk_r0_name, WPA_PMK_NAME_LEN);
    1615                 :          8 :         wpa_derive_pmk_r1(pmk_r0->pmk_r0, pmk_r0->pmk_r0_name, r1kh->id,
    1616                 :            :                           s1kh_id, f.pmk_r1, f.pmk_r1_name);
    1617                 :          8 :         wpa_printf(MSG_DEBUG, "FT: R1KH-ID " MACSTR, MAC2STR(r1kh->id));
    1618                 :          8 :         wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", f.pmk_r1, PMK_LEN);
    1619                 :          8 :         wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", f.pmk_r1_name,
    1620                 :            :                     WPA_PMK_NAME_LEN);
    1621                 :          8 :         os_get_time(&now);
    1622                 :          8 :         WPA_PUT_LE32(f.timestamp, now.sec);
    1623                 :          8 :         f.pairwise = host_to_le16(pairwise);
    1624                 :          8 :         os_memset(f.pad, 0, sizeof(f.pad));
    1625         [ -  + ]:          8 :         if (aes_wrap(r1kh->key, (FT_R0KH_R1KH_PUSH_DATA_LEN + 7) / 8,
    1626                 :            :                      f.timestamp, frame.timestamp) < 0)
    1627                 :          8 :                 return;
    1628                 :            : 
    1629                 :          8 :         wpa_ft_rrb_send(wpa_auth, r1kh->addr, (u8 *) &frame, sizeof(frame));
    1630                 :            : }
    1631                 :            : 
    1632                 :            : 
    1633                 :        404 : void wpa_ft_push_pmk_r1(struct wpa_authenticator *wpa_auth, const u8 *addr)
    1634                 :            : {
    1635                 :            :         struct wpa_ft_pmk_r0_sa *r0;
    1636                 :            :         struct ft_remote_r1kh *r1kh;
    1637                 :            : 
    1638         [ +  + ]:        404 :         if (!wpa_auth->conf.pmk_r1_push)
    1639                 :        396 :                 return;
    1640                 :            : 
    1641                 :          8 :         r0 = wpa_auth->ft_pmk_cache->pmk_r0;
    1642         [ +  - ]:          8 :         while (r0) {
    1643         [ +  - ]:          8 :                 if (os_memcmp(r0->spa, addr, ETH_ALEN) == 0)
    1644                 :          8 :                         break;
    1645                 :          0 :                 r0 = r0->next;
    1646                 :            :         }
    1647                 :            : 
    1648 [ +  - ][ -  + ]:          8 :         if (r0 == NULL || r0->pmk_r1_pushed)
    1649                 :          0 :                 return;
    1650                 :          8 :         r0->pmk_r1_pushed = 1;
    1651                 :            : 
    1652                 :          8 :         wpa_printf(MSG_DEBUG, "FT: Deriving and pushing PMK-R1 keys to R1KHs "
    1653                 :         48 :                    "for STA " MACSTR, MAC2STR(addr));
    1654                 :            : 
    1655                 :          8 :         r1kh = wpa_auth->conf.r1kh_list;
    1656         [ +  + ]:        412 :         while (r1kh) {
    1657                 :          8 :                 wpa_ft_generate_pmk_r1(wpa_auth, r0, r1kh, addr, r0->pairwise);
    1658                 :          8 :                 r1kh = r1kh->next;
    1659                 :            :         }
    1660                 :            : }
    1661                 :            : 
    1662                 :            : #endif /* CONFIG_IEEE80211R */

Generated by: LCOV version 1.9