LCOV - code coverage report
Current view: top level - src/eap_server - eap_server_psk.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1393793999 Lines: 192 280 68.6 %
Date: 2014-03-02 Functions: 13 14 92.9 %
Branches: 70 128 54.7 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * hostapd / EAP-PSK (RFC 4764) server
       3                 :            :  * Copyright (c) 2005-2007, 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                 :            :  * Note: EAP-PSK is an EAP authentication method and as such, completely
       9                 :            :  * different from WPA-PSK. This file is not needed for WPA-PSK functionality.
      10                 :            :  */
      11                 :            : 
      12                 :            : #include "includes.h"
      13                 :            : 
      14                 :            : #include "common.h"
      15                 :            : #include "crypto/aes_wrap.h"
      16                 :            : #include "crypto/random.h"
      17                 :            : #include "eap_common/eap_psk_common.h"
      18                 :            : #include "eap_server/eap_i.h"
      19                 :            : 
      20                 :            : 
      21                 :            : struct eap_psk_data {
      22                 :            :         enum { PSK_1, PSK_3, SUCCESS, FAILURE } state;
      23                 :            :         u8 rand_s[EAP_PSK_RAND_LEN];
      24                 :            :         u8 rand_p[EAP_PSK_RAND_LEN];
      25                 :            :         u8 *id_p;
      26                 :            :         size_t id_p_len;
      27                 :            :         u8 ak[EAP_PSK_AK_LEN], kdk[EAP_PSK_KDK_LEN], tek[EAP_PSK_TEK_LEN];
      28                 :            :         u8 msk[EAP_MSK_LEN];
      29                 :            :         u8 emsk[EAP_EMSK_LEN];
      30                 :            : };
      31                 :            : 
      32                 :            : 
      33                 :         16 : static void * eap_psk_init(struct eap_sm *sm)
      34                 :            : {
      35                 :            :         struct eap_psk_data *data;
      36                 :            : 
      37                 :         16 :         data = os_zalloc(sizeof(*data));
      38         [ -  + ]:         16 :         if (data == NULL)
      39                 :          0 :                 return NULL;
      40                 :         16 :         data->state = PSK_1;
      41                 :            : 
      42                 :         16 :         return data;
      43                 :            : }
      44                 :            : 
      45                 :            : 
      46                 :         16 : static void eap_psk_reset(struct eap_sm *sm, void *priv)
      47                 :            : {
      48                 :         16 :         struct eap_psk_data *data = priv;
      49                 :         16 :         os_free(data->id_p);
      50                 :         16 :         os_free(data);
      51                 :         16 : }
      52                 :            : 
      53                 :            : 
      54                 :         16 : static struct wpabuf * eap_psk_build_1(struct eap_sm *sm,
      55                 :            :                                        struct eap_psk_data *data, u8 id)
      56                 :            : {
      57                 :            :         struct wpabuf *req;
      58                 :            :         struct eap_psk_hdr_1 *psk;
      59                 :            : 
      60                 :         16 :         wpa_printf(MSG_DEBUG, "EAP-PSK: PSK-1 (sending)");
      61                 :            : 
      62         [ -  + ]:         16 :         if (random_get_bytes(data->rand_s, EAP_PSK_RAND_LEN)) {
      63                 :          0 :                 wpa_printf(MSG_ERROR, "EAP-PSK: Failed to get random data");
      64                 :          0 :                 data->state = FAILURE;
      65                 :          0 :                 return NULL;
      66                 :            :         }
      67                 :         16 :         wpa_hexdump(MSG_MSGDUMP, "EAP-PSK: RAND_S (server rand)",
      68                 :         16 :                     data->rand_s, EAP_PSK_RAND_LEN);
      69                 :            : 
      70                 :         16 :         req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_PSK,
      71                 :         16 :                             sizeof(*psk) + sm->server_id_len,
      72                 :            :                             EAP_CODE_REQUEST, id);
      73         [ -  + ]:         16 :         if (req == NULL) {
      74                 :          0 :                 wpa_printf(MSG_ERROR, "EAP-PSK: Failed to allocate memory "
      75                 :            :                            "request");
      76                 :          0 :                 data->state = FAILURE;
      77                 :          0 :                 return NULL;
      78                 :            :         }
      79                 :            : 
      80                 :         16 :         psk = wpabuf_put(req, sizeof(*psk));
      81                 :         16 :         psk->flags = EAP_PSK_FLAGS_SET_T(0); /* T=0 */
      82                 :         16 :         os_memcpy(psk->rand_s, data->rand_s, EAP_PSK_RAND_LEN);
      83                 :         16 :         wpabuf_put_data(req, sm->server_id, sm->server_id_len);
      84                 :            : 
      85                 :         16 :         return req;
      86                 :            : }
      87                 :            : 
      88                 :            : 
      89                 :         15 : static struct wpabuf * eap_psk_build_3(struct eap_sm *sm,
      90                 :            :                                        struct eap_psk_data *data, u8 id)
      91                 :            : {
      92                 :            :         struct wpabuf *req;
      93                 :            :         struct eap_psk_hdr_3 *psk;
      94                 :            :         u8 *buf, *pchannel, nonce[16];
      95                 :            :         size_t buflen;
      96                 :            : 
      97                 :         15 :         wpa_printf(MSG_DEBUG, "EAP-PSK: PSK-3 (sending)");
      98                 :            : 
      99                 :         15 :         req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_PSK,
     100                 :            :                             sizeof(*psk) + 4 + 16 + 1, EAP_CODE_REQUEST, id);
     101         [ -  + ]:         15 :         if (req == NULL) {
     102                 :          0 :                 wpa_printf(MSG_ERROR, "EAP-PSK: Failed to allocate memory "
     103                 :            :                            "request");
     104                 :          0 :                 data->state = FAILURE;
     105                 :          0 :                 return NULL;
     106                 :            :         }
     107                 :            : 
     108                 :         15 :         psk = wpabuf_put(req, sizeof(*psk));
     109                 :         15 :         psk->flags = EAP_PSK_FLAGS_SET_T(2); /* T=2 */
     110                 :         15 :         os_memcpy(psk->rand_s, data->rand_s, EAP_PSK_RAND_LEN);
     111                 :            : 
     112                 :            :         /* MAC_S = OMAC1-AES-128(AK, ID_S||RAND_P) */
     113                 :         15 :         buflen = sm->server_id_len + EAP_PSK_RAND_LEN;
     114                 :         15 :         buf = os_malloc(buflen);
     115         [ -  + ]:         15 :         if (buf == NULL)
     116                 :          0 :                 goto fail;
     117                 :            : 
     118                 :         15 :         os_memcpy(buf, sm->server_id, sm->server_id_len);
     119                 :         15 :         os_memcpy(buf + sm->server_id_len, data->rand_p, EAP_PSK_RAND_LEN);
     120         [ -  + ]:         15 :         if (omac1_aes_128(data->ak, buf, buflen, psk->mac_s)) {
     121                 :          0 :                 os_free(buf);
     122                 :          0 :                 goto fail;
     123                 :            :         }
     124                 :         15 :         os_free(buf);
     125                 :            : 
     126         [ -  + ]:         15 :         if (eap_psk_derive_keys(data->kdk, data->rand_p, data->tek, data->msk,
     127                 :         15 :                                 data->emsk))
     128                 :          0 :                 goto fail;
     129                 :         15 :         wpa_hexdump_key(MSG_DEBUG, "EAP-PSK: TEK", data->tek, EAP_PSK_TEK_LEN);
     130                 :         15 :         wpa_hexdump_key(MSG_DEBUG, "EAP-PSK: MSK", data->msk, EAP_MSK_LEN);
     131                 :         15 :         wpa_hexdump_key(MSG_DEBUG, "EAP-PSK: EMSK", data->emsk, EAP_EMSK_LEN);
     132                 :            : 
     133                 :         15 :         os_memset(nonce, 0, sizeof(nonce));
     134                 :         15 :         pchannel = wpabuf_put(req, 4 + 16 + 1);
     135                 :         15 :         os_memcpy(pchannel, nonce + 12, 4);
     136                 :         15 :         os_memset(pchannel + 4, 0, 16); /* Tag */
     137                 :         15 :         pchannel[4 + 16] = EAP_PSK_R_FLAG_DONE_SUCCESS << 6;
     138                 :         15 :         wpa_hexdump(MSG_DEBUG, "EAP-PSK: PCHANNEL (plaintext)",
     139                 :            :                     pchannel, 4 + 16 + 1);
     140         [ -  + ]:         15 :         if (aes_128_eax_encrypt(data->tek, nonce, sizeof(nonce),
     141                 :         15 :                                 wpabuf_head(req), 22,
     142                 :            :                                 pchannel + 4 + 16, 1, pchannel + 4))
     143                 :          0 :                 goto fail;
     144                 :         15 :         wpa_hexdump(MSG_DEBUG, "EAP-PSK: PCHANNEL (encrypted)",
     145                 :            :                     pchannel, 4 + 16 + 1);
     146                 :            : 
     147                 :         15 :         return req;
     148                 :            : 
     149                 :            : fail:
     150                 :          0 :         wpabuf_free(req);
     151                 :          0 :         data->state = FAILURE;
     152                 :         15 :         return NULL;
     153                 :            : }
     154                 :            : 
     155                 :            : 
     156                 :         31 : static struct wpabuf * eap_psk_buildReq(struct eap_sm *sm, void *priv, u8 id)
     157                 :            : {
     158                 :         31 :         struct eap_psk_data *data = priv;
     159                 :            : 
     160      [ +  +  - ]:         31 :         switch (data->state) {
     161                 :            :         case PSK_1:
     162                 :         16 :                 return eap_psk_build_1(sm, data, id);
     163                 :            :         case PSK_3:
     164                 :         15 :                 return eap_psk_build_3(sm, data, id);
     165                 :            :         default:
     166                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-PSK: Unknown state %d in buildReq",
     167                 :          0 :                            data->state);
     168                 :          0 :                 break;
     169                 :            :         }
     170                 :         31 :         return NULL;
     171                 :            : }
     172                 :            : 
     173                 :            : 
     174                 :         31 : static Boolean eap_psk_check(struct eap_sm *sm, void *priv,
     175                 :            :                              struct wpabuf *respData)
     176                 :            : {
     177                 :         31 :         struct eap_psk_data *data = priv;
     178                 :            :         size_t len;
     179                 :            :         u8 t;
     180                 :            :         const u8 *pos;
     181                 :            : 
     182                 :         31 :         pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_PSK, respData, &len);
     183 [ -  + ][ +  - ]:         31 :         if (pos == NULL || len < 1) {
     184                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PSK: Invalid frame");
     185                 :          0 :                 return TRUE;
     186                 :            :         }
     187                 :         31 :         t = EAP_PSK_FLAGS_GET_T(*pos);
     188                 :            : 
     189                 :         31 :         wpa_printf(MSG_DEBUG, "EAP-PSK: received frame: T=%d", t);
     190                 :            : 
     191 [ -  + ][ +  + ]:         31 :         if (data->state == PSK_1 && t != 1) {
     192                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-PSK: Expected PSK-2 - "
     193                 :            :                            "ignore T=%d", t);
     194                 :          0 :                 return TRUE;
     195                 :            :         }
     196                 :            : 
     197 [ +  + ][ -  + ]:         31 :         if (data->state == PSK_3 && t != 3) {
     198                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-PSK: Expected PSK-4 - "
     199                 :            :                            "ignore T=%d", t);
     200                 :          0 :                 return TRUE;
     201                 :            :         }
     202                 :            : 
     203 [ +  + ][ +  - ]:         31 :         if ((t == 1 && len < sizeof(struct eap_psk_hdr_2)) ||
                 [ +  + ]
     204         [ -  + ]:         15 :             (t == 3 && len < sizeof(struct eap_psk_hdr_4))) {
     205                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-PSK: Too short frame");
     206                 :          0 :                 return TRUE;
     207                 :            :         }
     208                 :            : 
     209                 :         31 :         return FALSE;
     210                 :            : }
     211                 :            : 
     212                 :            : 
     213                 :         16 : static void eap_psk_process_2(struct eap_sm *sm,
     214                 :            :                               struct eap_psk_data *data,
     215                 :            :                               struct wpabuf *respData)
     216                 :            : {
     217                 :            :         const struct eap_psk_hdr_2 *resp;
     218                 :            :         u8 *pos, mac[EAP_PSK_MAC_LEN], *buf;
     219                 :            :         size_t left, buflen;
     220                 :            :         int i;
     221                 :            :         const u8 *cpos;
     222                 :            : 
     223         [ -  + ]:         16 :         if (data->state != PSK_1)
     224                 :          0 :                 return;
     225                 :            : 
     226                 :         16 :         wpa_printf(MSG_DEBUG, "EAP-PSK: Received PSK-2");
     227                 :            : 
     228                 :         16 :         cpos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_PSK, respData,
     229                 :            :                                 &left);
     230 [ -  + ][ +  - ]:         16 :         if (cpos == NULL || left < sizeof(*resp)) {
     231                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PSK: Invalid frame");
     232                 :          0 :                 return;
     233                 :            :         }
     234                 :         16 :         resp = (const struct eap_psk_hdr_2 *) cpos;
     235                 :         16 :         cpos = (const u8 *) (resp + 1);
     236                 :         16 :         left -= sizeof(*resp);
     237                 :            : 
     238                 :         16 :         os_free(data->id_p);
     239                 :         16 :         data->id_p = os_malloc(left);
     240         [ -  + ]:         16 :         if (data->id_p == NULL) {
     241                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PSK: Failed to allocate memory for "
     242                 :            :                            "ID_P");
     243                 :          0 :                 return;
     244                 :            :         }
     245                 :         16 :         os_memcpy(data->id_p, cpos, left);
     246                 :         16 :         data->id_p_len = left;
     247                 :         16 :         wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-PSK: ID_P",
     248                 :         16 :                           data->id_p, data->id_p_len);
     249                 :            : 
     250         [ -  + ]:         16 :         if (eap_user_get(sm, data->id_p, data->id_p_len, 0) < 0) {
     251                 :          0 :                 wpa_hexdump_ascii(MSG_DEBUG, "EAP-PSK: unknown ID_P",
     252                 :          0 :                                   data->id_p, data->id_p_len);
     253                 :          0 :                 data->state = FAILURE;
     254                 :          0 :                 return;
     255                 :            :         }
     256                 :            : 
     257         [ +  - ]:         16 :         for (i = 0;
     258         [ -  + ]:         16 :              i < EAP_MAX_METHODS &&
     259         [ +  - ]:         16 :                      (sm->user->methods[i].vendor != EAP_VENDOR_IETF ||
     260                 :         16 :                       sm->user->methods[i].method != EAP_TYPE_NONE);
     261                 :          0 :              i++) {
     262 [ +  - ][ +  - ]:         16 :                 if (sm->user->methods[i].vendor == EAP_VENDOR_IETF &&
     263                 :         16 :                     sm->user->methods[i].method == EAP_TYPE_PSK)
     264                 :         16 :                         break;
     265                 :            :         }
     266                 :            : 
     267 [ +  - ][ +  - ]:         16 :         if (i >= EAP_MAX_METHODS ||
     268         [ -  + ]:         16 :             sm->user->methods[i].vendor != EAP_VENDOR_IETF ||
     269                 :         16 :             sm->user->methods[i].method != EAP_TYPE_PSK) {
     270                 :          0 :                 wpa_hexdump_ascii(MSG_DEBUG,
     271                 :            :                                   "EAP-PSK: EAP-PSK not enabled for ID_P",
     272                 :          0 :                                   data->id_p, data->id_p_len);
     273                 :          0 :                 data->state = FAILURE;
     274                 :          0 :                 return;
     275                 :            :         }
     276                 :            : 
     277 [ +  - ][ -  + ]:         16 :         if (sm->user->password == NULL ||
     278                 :         16 :             sm->user->password_len != EAP_PSK_PSK_LEN) {
     279                 :          0 :                 wpa_hexdump_ascii(MSG_DEBUG, "EAP-PSK: invalid password in "
     280                 :            :                                   "user database for ID_P",
     281                 :          0 :                                   data->id_p, data->id_p_len);
     282                 :          0 :                 data->state = FAILURE;
     283                 :          0 :                 return;
     284                 :            :         }
     285         [ -  + ]:         16 :         if (eap_psk_key_setup(sm->user->password, data->ak, data->kdk)) {
     286                 :          0 :                 data->state = FAILURE;
     287                 :          0 :                 return;
     288                 :            :         }
     289                 :         16 :         wpa_hexdump_key(MSG_DEBUG, "EAP-PSK: AK", data->ak, EAP_PSK_AK_LEN);
     290                 :         16 :         wpa_hexdump_key(MSG_DEBUG, "EAP-PSK: KDK", data->kdk, EAP_PSK_KDK_LEN);
     291                 :            : 
     292                 :         16 :         wpa_hexdump(MSG_MSGDUMP, "EAP-PSK: RAND_P (client rand)",
     293                 :         16 :                     resp->rand_p, EAP_PSK_RAND_LEN);
     294                 :         16 :         os_memcpy(data->rand_p, resp->rand_p, EAP_PSK_RAND_LEN);
     295                 :            : 
     296                 :            :         /* MAC_P = OMAC1-AES-128(AK, ID_P||ID_S||RAND_S||RAND_P) */
     297                 :         16 :         buflen = data->id_p_len + sm->server_id_len + 2 * EAP_PSK_RAND_LEN;
     298                 :         16 :         buf = os_malloc(buflen);
     299         [ -  + ]:         16 :         if (buf == NULL) {
     300                 :          0 :                 data->state = FAILURE;
     301                 :          0 :                 return;
     302                 :            :         }
     303                 :         16 :         os_memcpy(buf, data->id_p, data->id_p_len);
     304                 :         16 :         pos = buf + data->id_p_len;
     305                 :         16 :         os_memcpy(pos, sm->server_id, sm->server_id_len);
     306                 :         16 :         pos += sm->server_id_len;
     307                 :         16 :         os_memcpy(pos, data->rand_s, EAP_PSK_RAND_LEN);
     308                 :         16 :         pos += EAP_PSK_RAND_LEN;
     309                 :         16 :         os_memcpy(pos, data->rand_p, EAP_PSK_RAND_LEN);
     310         [ -  + ]:         16 :         if (omac1_aes_128(data->ak, buf, buflen, mac)) {
     311                 :          0 :                 os_free(buf);
     312                 :          0 :                 data->state = FAILURE;
     313                 :          0 :                 return;
     314                 :            :         }
     315                 :         16 :         os_free(buf);
     316                 :         16 :         wpa_hexdump(MSG_DEBUG, "EAP-PSK: MAC_P", resp->mac_p, EAP_PSK_MAC_LEN);
     317         [ +  + ]:         16 :         if (os_memcmp(mac, resp->mac_p, EAP_PSK_MAC_LEN) != 0) {
     318                 :          1 :                 wpa_printf(MSG_INFO, "EAP-PSK: Invalid MAC_P");
     319                 :          1 :                 wpa_hexdump(MSG_MSGDUMP, "EAP-PSK: Expected MAC_P",
     320                 :            :                             mac, EAP_PSK_MAC_LEN);
     321                 :          1 :                 data->state = FAILURE;
     322                 :          1 :                 return;
     323                 :            :         }
     324                 :            : 
     325                 :         16 :         data->state = PSK_3;
     326                 :            : }
     327                 :            : 
     328                 :            : 
     329                 :         15 : static void eap_psk_process_4(struct eap_sm *sm,
     330                 :            :                               struct eap_psk_data *data,
     331                 :            :                               struct wpabuf *respData)
     332                 :            : {
     333                 :            :         const struct eap_psk_hdr_4 *resp;
     334                 :            :         u8 *decrypted, nonce[16];
     335                 :            :         size_t left;
     336                 :            :         const u8 *pos, *tag;
     337                 :            : 
     338         [ -  + ]:         15 :         if (data->state != PSK_3)
     339                 :          0 :                 return;
     340                 :            : 
     341                 :         15 :         wpa_printf(MSG_DEBUG, "EAP-PSK: Received PSK-4");
     342                 :            : 
     343                 :         15 :         pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_PSK, respData, &left);
     344 [ -  + ][ +  - ]:         15 :         if (pos == NULL || left < sizeof(*resp)) {
     345                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PSK: Invalid frame");
     346                 :          0 :                 return;
     347                 :            :         }
     348                 :         15 :         resp = (const struct eap_psk_hdr_4 *) pos;
     349                 :         15 :         pos = (const u8 *) (resp + 1);
     350                 :         15 :         left -= sizeof(*resp);
     351                 :            : 
     352                 :         15 :         wpa_hexdump(MSG_MSGDUMP, "EAP-PSK: Encrypted PCHANNEL", pos, left);
     353                 :            : 
     354         [ -  + ]:         15 :         if (left < 4 + 16 + 1) {
     355                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PSK: Too short PCHANNEL data in "
     356                 :            :                            "PSK-4 (len=%lu, expected 21)",
     357                 :            :                            (unsigned long) left);
     358                 :          0 :                 return;
     359                 :            :         }
     360                 :            : 
     361 [ +  - ][ +  - ]:         15 :         if (pos[0] == 0 && pos[1] == 0 && pos[2] == 0 && pos[3] == 0) {
         [ +  - ][ -  + ]
     362                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-PSK: Nonce did not increase");
     363                 :          0 :                 return;
     364                 :            :         }
     365                 :            : 
     366                 :         15 :         os_memset(nonce, 0, 12);
     367                 :         15 :         os_memcpy(nonce + 12, pos, 4);
     368                 :         15 :         pos += 4;
     369                 :         15 :         left -= 4;
     370                 :         15 :         tag = pos;
     371                 :         15 :         pos += 16;
     372                 :         15 :         left -= 16;
     373                 :            : 
     374                 :         15 :         decrypted = os_malloc(left);
     375         [ -  + ]:         15 :         if (decrypted == NULL)
     376                 :          0 :                 return;
     377                 :         15 :         os_memcpy(decrypted, pos, left);
     378                 :            : 
     379         [ -  + ]:         15 :         if (aes_128_eax_decrypt(data->tek, nonce, sizeof(nonce),
     380                 :         15 :                                 wpabuf_head(respData), 22, decrypted, left,
     381                 :            :                                 tag)) {
     382                 :          0 :                 wpa_printf(MSG_WARNING, "EAP-PSK: PCHANNEL decryption failed");
     383                 :          0 :                 os_free(decrypted);
     384                 :          0 :                 data->state = FAILURE;
     385                 :          0 :                 return;
     386                 :            :         }
     387                 :         15 :         wpa_hexdump(MSG_DEBUG, "EAP-PSK: Decrypted PCHANNEL message",
     388                 :            :                     decrypted, left);
     389                 :            : 
     390                 :            :         /* Verify R flag */
     391   [ -  +  -  - ]:         15 :         switch (decrypted[0] >> 6) {
     392                 :            :         case EAP_PSK_R_FLAG_CONT:
     393                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-PSK: R flag - CONT - unsupported");
     394                 :          0 :                 data->state = FAILURE;
     395                 :          0 :                 break;
     396                 :            :         case EAP_PSK_R_FLAG_DONE_SUCCESS:
     397                 :         15 :                 wpa_printf(MSG_DEBUG, "EAP-PSK: R flag - DONE_SUCCESS");
     398                 :         15 :                 data->state = SUCCESS;
     399                 :         15 :                 break;
     400                 :            :         case EAP_PSK_R_FLAG_DONE_FAILURE:
     401                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-PSK: R flag - DONE_FAILURE");
     402                 :          0 :                 data->state = FAILURE;
     403                 :          0 :                 break;
     404                 :            :         }
     405                 :         15 :         os_free(decrypted);
     406                 :            : }
     407                 :            : 
     408                 :            : 
     409                 :         31 : static void eap_psk_process(struct eap_sm *sm, void *priv,
     410                 :            :                             struct wpabuf *respData)
     411                 :            : {
     412                 :         31 :         struct eap_psk_data *data = priv;
     413                 :            :         const u8 *pos;
     414                 :            :         size_t len;
     415                 :            : 
     416 [ +  - ][ -  + ]:         31 :         if (sm->user == NULL || sm->user->password == NULL) {
     417                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PSK: Plaintext password not "
     418                 :            :                            "configured");
     419                 :          0 :                 data->state = FAILURE;
     420                 :          0 :                 return;
     421                 :            :         }
     422                 :            : 
     423                 :         31 :         pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_PSK, respData, &len);
     424 [ -  + ][ +  - ]:         31 :         if (pos == NULL || len < 1)
     425                 :          0 :                 return;
     426                 :            : 
     427      [ +  +  - ]:         31 :         switch (EAP_PSK_FLAGS_GET_T(*pos)) {
     428                 :            :         case 1:
     429                 :         16 :                 eap_psk_process_2(sm, data, respData);
     430                 :         16 :                 break;
     431                 :            :         case 3:
     432                 :         15 :                 eap_psk_process_4(sm, data, respData);
     433                 :         31 :                 break;
     434                 :            :         }
     435                 :            : }
     436                 :            : 
     437                 :            : 
     438                 :         32 : static Boolean eap_psk_isDone(struct eap_sm *sm, void *priv)
     439                 :            : {
     440                 :         32 :         struct eap_psk_data *data = priv;
     441 [ +  + ][ +  + ]:         32 :         return data->state == SUCCESS || data->state == FAILURE;
     442                 :            : }
     443                 :            : 
     444                 :            : 
     445                 :         16 : static u8 * eap_psk_getKey(struct eap_sm *sm, void *priv, size_t *len)
     446                 :            : {
     447                 :         16 :         struct eap_psk_data *data = priv;
     448                 :            :         u8 *key;
     449                 :            : 
     450         [ +  + ]:         16 :         if (data->state != SUCCESS)
     451                 :          1 :                 return NULL;
     452                 :            : 
     453                 :         15 :         key = os_malloc(EAP_MSK_LEN);
     454         [ -  + ]:         15 :         if (key == NULL)
     455                 :          0 :                 return NULL;
     456                 :         15 :         os_memcpy(key, data->msk, EAP_MSK_LEN);
     457                 :         15 :         *len = EAP_MSK_LEN;
     458                 :            : 
     459                 :         16 :         return key;
     460                 :            : }
     461                 :            : 
     462                 :            : 
     463                 :          0 : static u8 * eap_psk_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
     464                 :            : {
     465                 :          0 :         struct eap_psk_data *data = priv;
     466                 :            :         u8 *key;
     467                 :            : 
     468         [ #  # ]:          0 :         if (data->state != SUCCESS)
     469                 :          0 :                 return NULL;
     470                 :            : 
     471                 :          0 :         key = os_malloc(EAP_EMSK_LEN);
     472         [ #  # ]:          0 :         if (key == NULL)
     473                 :          0 :                 return NULL;
     474                 :          0 :         os_memcpy(key, data->emsk, EAP_EMSK_LEN);
     475                 :          0 :         *len = EAP_EMSK_LEN;
     476                 :            : 
     477                 :          0 :         return key;
     478                 :            : }
     479                 :            : 
     480                 :            : 
     481                 :         17 : static Boolean eap_psk_isSuccess(struct eap_sm *sm, void *priv)
     482                 :            : {
     483                 :         17 :         struct eap_psk_data *data = priv;
     484                 :         17 :         return data->state == SUCCESS;
     485                 :            : }
     486                 :            : 
     487                 :            : 
     488                 :          2 : int eap_server_psk_register(void)
     489                 :            : {
     490                 :            :         struct eap_method *eap;
     491                 :            :         int ret;
     492                 :            : 
     493                 :          2 :         eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
     494                 :            :                                       EAP_VENDOR_IETF, EAP_TYPE_PSK, "PSK");
     495         [ -  + ]:          2 :         if (eap == NULL)
     496                 :          0 :                 return -1;
     497                 :            : 
     498                 :          2 :         eap->init = eap_psk_init;
     499                 :          2 :         eap->reset = eap_psk_reset;
     500                 :          2 :         eap->buildReq = eap_psk_buildReq;
     501                 :          2 :         eap->check = eap_psk_check;
     502                 :          2 :         eap->process = eap_psk_process;
     503                 :          2 :         eap->isDone = eap_psk_isDone;
     504                 :          2 :         eap->getKey = eap_psk_getKey;
     505                 :          2 :         eap->isSuccess = eap_psk_isSuccess;
     506                 :          2 :         eap->get_emsk = eap_psk_get_emsk;
     507                 :            : 
     508                 :          2 :         ret = eap_server_method_register(eap);
     509         [ -  + ]:          2 :         if (ret)
     510                 :          0 :                 eap_server_method_free(eap);
     511                 :          2 :         return ret;
     512                 :            : }

Generated by: LCOV version 1.9