LCOV - code coverage report
Current view: top level - src/eap_peer - eap_pwd.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1388338050 Lines: 320 497 64.4 %
Date: 2013-12-29 Functions: 11 12 91.7 %
Branches: 79 182 43.4 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * EAP peer method: EAP-pwd (RFC 5931)
       3                 :            :  * Copyright (c) 2010, Dan Harkins <dharkins@lounge.org>
       4                 :            :  *
       5                 :            :  * This software may be distributed under the terms of the BSD license.
       6                 :            :  * See README for more details.
       7                 :            :  */
       8                 :            : 
       9                 :            : #include "includes.h"
      10                 :            : 
      11                 :            : #include "common.h"
      12                 :            : #include "crypto/sha256.h"
      13                 :            : #include "eap_peer/eap_i.h"
      14                 :            : #include "eap_common/eap_pwd_common.h"
      15                 :            : 
      16                 :            : 
      17                 :            : struct eap_pwd_data {
      18                 :            :         enum {
      19                 :            :                 PWD_ID_Req, PWD_Commit_Req, PWD_Confirm_Req, SUCCESS, FAILURE
      20                 :            :         } state;
      21                 :            :         u8 *id_peer;
      22                 :            :         size_t id_peer_len;
      23                 :            :         u8 *id_server;
      24                 :            :         size_t id_server_len;
      25                 :            :         u8 *password;
      26                 :            :         size_t password_len;
      27                 :            :         u16 group_num;
      28                 :            :         EAP_PWD_group *grp;
      29                 :            : 
      30                 :            :         struct wpabuf *inbuf;
      31                 :            :         size_t in_frag_pos;
      32                 :            :         struct wpabuf *outbuf;
      33                 :            :         size_t out_frag_pos;
      34                 :            :         size_t mtu;
      35                 :            : 
      36                 :            :         BIGNUM *k;
      37                 :            :         BIGNUM *private_value;
      38                 :            :         BIGNUM *server_scalar;
      39                 :            :         BIGNUM *my_scalar;
      40                 :            :         EC_POINT *my_element;
      41                 :            :         EC_POINT *server_element;
      42                 :            : 
      43                 :            :         u8 msk[EAP_MSK_LEN];
      44                 :            :         u8 emsk[EAP_EMSK_LEN];
      45                 :            : 
      46                 :            :         BN_CTX *bnctx;
      47                 :            : };
      48                 :            : 
      49                 :            : 
      50                 :            : #ifndef CONFIG_NO_STDOUT_DEBUG
      51                 :         12 : static const char * eap_pwd_state_txt(int state)
      52                 :            : {
      53   [ +  +  +  +  :         12 :         switch (state) {
                   -  - ]
      54                 :            :         case PWD_ID_Req:
      55                 :          2 :                 return "PWD-ID-Req";
      56                 :            :         case PWD_Commit_Req:
      57                 :          4 :                 return "PWD-Commit-Req";
      58                 :            :         case PWD_Confirm_Req:
      59                 :          4 :                 return "PWD-Confirm-Req";
      60                 :            :         case SUCCESS:
      61                 :          2 :                 return "SUCCESS";
      62                 :            :         case FAILURE:
      63                 :          0 :                 return "FAILURE";
      64                 :            :         default:
      65                 :         12 :                 return "PWD-UNK";
      66                 :            :         }
      67                 :            : }
      68                 :            : #endif  /* CONFIG_NO_STDOUT_DEBUG */
      69                 :            : 
      70                 :            : 
      71                 :          6 : static void eap_pwd_state(struct eap_pwd_data *data, int state)
      72                 :            : {
      73                 :          6 :         wpa_printf(MSG_DEBUG, "EAP-PWD: %s -> %s",
      74                 :          6 :                    eap_pwd_state_txt(data->state), eap_pwd_state_txt(state));
      75                 :          6 :         data->state = state;
      76                 :          6 : }
      77                 :            : 
      78                 :            : 
      79                 :          2 : static void * eap_pwd_init(struct eap_sm *sm)
      80                 :            : {
      81                 :            :         struct eap_pwd_data *data;
      82                 :            :         const u8 *identity, *password;
      83                 :            :         size_t identity_len, password_len;
      84                 :            : 
      85                 :          2 :         password = eap_get_config_password(sm, &password_len);
      86         [ -  + ]:          2 :         if (password == NULL) {
      87                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PWD: No password configured!");
      88                 :          0 :                 return NULL;
      89                 :            :         }
      90                 :            : 
      91                 :          2 :         identity = eap_get_config_identity(sm, &identity_len);
      92         [ -  + ]:          2 :         if (identity == NULL) {
      93                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PWD: No identity configured!");
      94                 :          0 :                 return NULL;
      95                 :            :         }
      96                 :            : 
      97         [ -  + ]:          2 :         if ((data = os_zalloc(sizeof(*data))) == NULL) {
      98                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PWD: memory allocation data fail");
      99                 :          0 :                 return NULL;
     100                 :            :         }
     101                 :            : 
     102         [ -  + ]:          2 :         if ((data->bnctx = BN_CTX_new()) == NULL) {
     103                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PWD: bn context allocation fail");
     104                 :          0 :                 os_free(data);
     105                 :          0 :                 return NULL;
     106                 :            :         }
     107                 :            : 
     108         [ -  + ]:          2 :         if ((data->id_peer = os_malloc(identity_len)) == NULL) {
     109                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PWD: memory allocation id fail");
     110                 :          0 :                 BN_CTX_free(data->bnctx);
     111                 :          0 :                 os_free(data);
     112                 :          0 :                 return NULL;
     113                 :            :         }
     114                 :            : 
     115                 :          2 :         os_memcpy(data->id_peer, identity, identity_len);
     116                 :          2 :         data->id_peer_len = identity_len;
     117                 :            : 
     118         [ -  + ]:          2 :         if ((data->password = os_malloc(password_len)) == NULL) {
     119                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PWD: memory allocation psk fail");
     120                 :          0 :                 BN_CTX_free(data->bnctx);
     121                 :          0 :                 os_free(data->id_peer);
     122                 :          0 :                 os_free(data);
     123                 :          0 :                 return NULL;
     124                 :            :         }
     125                 :          2 :         os_memcpy(data->password, password, password_len);
     126                 :          2 :         data->password_len = password_len;
     127                 :            : 
     128                 :          2 :         data->out_frag_pos = data->in_frag_pos = 0;
     129                 :          2 :         data->inbuf = data->outbuf = NULL;
     130                 :          2 :         data->mtu = 1020; /* default from RFC 5931, make it configurable! */
     131                 :            : 
     132                 :          2 :         data->state = PWD_ID_Req;
     133                 :            : 
     134                 :          2 :         return data;
     135                 :            : }
     136                 :            : 
     137                 :            : 
     138                 :          2 : static void eap_pwd_deinit(struct eap_sm *sm, void *priv)
     139                 :            : {
     140                 :          2 :         struct eap_pwd_data *data = priv;
     141                 :            : 
     142                 :          2 :         BN_free(data->private_value);
     143                 :          2 :         BN_free(data->server_scalar);
     144                 :          2 :         BN_free(data->my_scalar);
     145                 :          2 :         BN_free(data->k);
     146                 :          2 :         BN_CTX_free(data->bnctx);
     147                 :          2 :         EC_POINT_free(data->my_element);
     148                 :          2 :         EC_POINT_free(data->server_element);
     149                 :          2 :         os_free(data->id_peer);
     150                 :          2 :         os_free(data->id_server);
     151                 :          2 :         os_free(data->password);
     152         [ +  - ]:          2 :         if (data->grp) {
     153                 :          2 :                 EC_GROUP_free(data->grp->group);
     154                 :          2 :                 EC_POINT_free(data->grp->pwe);
     155                 :          2 :                 BN_free(data->grp->order);
     156                 :          2 :                 BN_free(data->grp->prime);
     157                 :          2 :                 os_free(data->grp);
     158                 :            :         }
     159                 :          2 :         os_free(data);
     160                 :          2 : }
     161                 :            : 
     162                 :            : 
     163                 :          2 : static u8 * eap_pwd_getkey(struct eap_sm *sm, void *priv, size_t *len)
     164                 :            : {
     165                 :          2 :         struct eap_pwd_data *data = priv;
     166                 :            :         u8 *key;
     167                 :            : 
     168         [ -  + ]:          2 :         if (data->state != SUCCESS)
     169                 :          0 :                 return NULL;
     170                 :            : 
     171                 :          2 :         key = os_malloc(EAP_MSK_LEN);
     172         [ -  + ]:          2 :         if (key == NULL)
     173                 :          0 :                 return NULL;
     174                 :            : 
     175                 :          2 :         os_memcpy(key, data->msk, EAP_MSK_LEN);
     176                 :          2 :         *len = EAP_MSK_LEN;
     177                 :            : 
     178                 :          2 :         return key;
     179                 :            : }
     180                 :            : 
     181                 :            : 
     182                 :            : static void
     183                 :          2 : eap_pwd_perform_id_exchange(struct eap_sm *sm, struct eap_pwd_data *data,
     184                 :            :                             struct eap_method_ret *ret,
     185                 :            :                             const struct wpabuf *reqData,
     186                 :            :                             const u8 *payload, size_t payload_len)
     187                 :            : {
     188                 :            :         struct eap_pwd_id *id;
     189                 :            : 
     190         [ -  + ]:          2 :         if (data->state != PWD_ID_Req) {
     191                 :          0 :                 ret->ignore = TRUE;
     192                 :          0 :                 eap_pwd_state(data, FAILURE);
     193                 :          0 :                 return;
     194                 :            :         }
     195                 :            : 
     196         [ -  + ]:          2 :         if (payload_len < sizeof(struct eap_pwd_id)) {
     197                 :          0 :                 ret->ignore = TRUE;
     198                 :          0 :                 eap_pwd_state(data, FAILURE);
     199                 :          0 :                 return;
     200                 :            :         }
     201                 :            : 
     202                 :          2 :         id = (struct eap_pwd_id *) payload;
     203                 :          2 :         data->group_num = be_to_host16(id->group_num);
     204 [ +  - ][ -  + ]:          2 :         if ((id->random_function != EAP_PWD_DEFAULT_RAND_FUNC) ||
     205                 :          2 :             (id->prf != EAP_PWD_DEFAULT_PRF)) {
     206                 :          0 :                 ret->ignore = TRUE;
     207                 :          0 :                 eap_pwd_state(data, FAILURE);
     208                 :          0 :                 return;
     209                 :            :         }
     210                 :            : 
     211                 :          2 :         wpa_printf(MSG_DEBUG, "EAP-PWD (peer): using group %d",
     212                 :          2 :                    data->group_num);
     213                 :            : 
     214                 :          2 :         data->id_server = os_malloc(payload_len - sizeof(struct eap_pwd_id));
     215         [ -  + ]:          2 :         if (data->id_server == NULL) {
     216                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PWD: memory allocation id fail");
     217                 :          0 :                 eap_pwd_state(data, FAILURE);
     218                 :          0 :                 return;
     219                 :            :         }
     220                 :          2 :         data->id_server_len = payload_len - sizeof(struct eap_pwd_id);
     221                 :          2 :         os_memcpy(data->id_server, id->identity, data->id_server_len);
     222                 :          2 :         wpa_hexdump_ascii(MSG_INFO, "EAP-PWD (peer): server sent id of",
     223                 :          2 :                           data->id_server, data->id_server_len);
     224                 :            : 
     225         [ -  + ]:          2 :         if ((data->grp = (EAP_PWD_group *) os_malloc(sizeof(EAP_PWD_group))) ==
     226                 :            :             NULL) {
     227                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PWD: failed to allocate memory for "
     228                 :            :                            "group");
     229                 :          0 :                 eap_pwd_state(data, FAILURE);
     230                 :          0 :                 return;
     231                 :            :         }
     232                 :            : 
     233                 :            :         /* compute PWE */
     234         [ -  + ]:          2 :         if (compute_password_element(data->grp, data->group_num,
     235                 :          2 :                                      data->password, data->password_len,
     236                 :          2 :                                      data->id_server, data->id_server_len,
     237                 :          2 :                                      data->id_peer, data->id_peer_len,
     238                 :          2 :                                      id->token)) {
     239                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PWD (peer): unable to compute PWE");
     240                 :          0 :                 eap_pwd_state(data, FAILURE);
     241                 :          0 :                 return;
     242                 :            :         }
     243                 :            : 
     244                 :          2 :         wpa_printf(MSG_DEBUG, "EAP-PWD (peer): computed %d bit PWE...",
     245                 :          2 :                    BN_num_bits(data->grp->prime));
     246                 :            : 
     247                 :          2 :         data->outbuf = wpabuf_alloc(sizeof(struct eap_pwd_id) +
     248                 :          2 :                                     data->id_peer_len);
     249         [ -  + ]:          2 :         if (data->outbuf == NULL) {
     250                 :          0 :                 eap_pwd_state(data, FAILURE);
     251                 :          0 :                 return;
     252                 :            :         }
     253                 :          2 :         wpabuf_put_be16(data->outbuf, data->group_num);
     254                 :          2 :         wpabuf_put_u8(data->outbuf, EAP_PWD_DEFAULT_RAND_FUNC);
     255                 :          2 :         wpabuf_put_u8(data->outbuf, EAP_PWD_DEFAULT_PRF);
     256                 :          2 :         wpabuf_put_data(data->outbuf, id->token, sizeof(id->token));
     257                 :          2 :         wpabuf_put_u8(data->outbuf, EAP_PWD_PREP_NONE);
     258                 :          2 :         wpabuf_put_data(data->outbuf, data->id_peer, data->id_peer_len);
     259                 :            : 
     260                 :          2 :         eap_pwd_state(data, PWD_Commit_Req);
     261                 :            : }
     262                 :            : 
     263                 :            : 
     264                 :            : static void
     265                 :          2 : eap_pwd_perform_commit_exchange(struct eap_sm *sm, struct eap_pwd_data *data,
     266                 :            :                                 struct eap_method_ret *ret,
     267                 :            :                                 const struct wpabuf *reqData,
     268                 :            :                                 const u8 *payload, size_t payload_len)
     269                 :            : {
     270                 :          2 :         EC_POINT *K = NULL, *point = NULL;
     271                 :          2 :         BIGNUM *mask = NULL, *x = NULL, *y = NULL, *cofactor = NULL;
     272                 :            :         u16 offset;
     273                 :          2 :         u8 *ptr, *scalar = NULL, *element = NULL;
     274                 :            : 
     275   [ +  -  +  - ]:          4 :         if (((data->private_value = BN_new()) == NULL) ||
     276         [ +  - ]:          4 :             ((data->my_element = EC_POINT_new(data->grp->group)) == NULL) ||
     277         [ +  - ]:          2 :             ((cofactor = BN_new()) == NULL) ||
     278         [ -  + ]:          4 :             ((data->my_scalar = BN_new()) == NULL) ||
     279                 :            :             ((mask = BN_new()) == NULL)) {
     280                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PWD (peer): scalar allocation fail");
     281                 :          0 :                 goto fin;
     282                 :            :         }
     283                 :            : 
     284         [ -  + ]:          2 :         if (!EC_GROUP_get_cofactor(data->grp->group, cofactor, NULL)) {
     285                 :          0 :                 wpa_printf(MSG_INFO, "EAP-pwd (peer): unable to get cofactor "
     286                 :            :                            "for curve");
     287                 :          0 :                 goto fin;
     288                 :            :         }
     289                 :            : 
     290                 :          2 :         BN_rand_range(data->private_value, data->grp->order);
     291                 :          2 :         BN_rand_range(mask, data->grp->order);
     292                 :          2 :         BN_add(data->my_scalar, data->private_value, mask);
     293                 :          2 :         BN_mod(data->my_scalar, data->my_scalar, data->grp->order,
     294                 :            :                data->bnctx);
     295                 :            : 
     296         [ -  + ]:          2 :         if (!EC_POINT_mul(data->grp->group, data->my_element, NULL,
     297                 :          2 :                           data->grp->pwe, mask, data->bnctx)) {
     298                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PWD (peer): element allocation "
     299                 :            :                            "fail");
     300                 :          0 :                 eap_pwd_state(data, FAILURE);
     301                 :          0 :                 goto fin;
     302                 :            :         }
     303                 :            : 
     304         [ -  + ]:          2 :         if (!EC_POINT_invert(data->grp->group, data->my_element, data->bnctx))
     305                 :            :         {
     306                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PWD (peer): element inversion fail");
     307                 :          0 :                 goto fin;
     308                 :            :         }
     309                 :          2 :         BN_free(mask);
     310                 :            : 
     311 [ +  - ][ -  + ]:          2 :         if (((x = BN_new()) == NULL) ||
     312                 :            :             ((y = BN_new()) == NULL)) {
     313                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PWD (peer): point allocation fail");
     314                 :          0 :                 goto fin;
     315                 :            :         }
     316                 :            : 
     317                 :            :         /* process the request */
     318   [ +  -  +  - ]:          4 :         if (((data->server_scalar = BN_new()) == NULL) ||
     319         [ +  - ]:          4 :             ((data->k = BN_new()) == NULL) ||
     320         [ +  - ]:          2 :             ((K = EC_POINT_new(data->grp->group)) == NULL) ||
     321         [ -  + ]:          2 :             ((point = EC_POINT_new(data->grp->group)) == NULL) ||
     322                 :          2 :             ((data->server_element = EC_POINT_new(data->grp->group)) == NULL))
     323                 :            :         {
     324                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PWD (peer): peer data allocation "
     325                 :            :                            "fail");
     326                 :          0 :                 goto fin;
     327                 :            :         }
     328                 :            : 
     329                 :            :         /* element, x then y, followed by scalar */
     330                 :          2 :         ptr = (u8 *) payload;
     331                 :          2 :         BN_bin2bn(ptr, BN_num_bytes(data->grp->prime), x);
     332                 :          2 :         ptr += BN_num_bytes(data->grp->prime);
     333                 :          2 :         BN_bin2bn(ptr, BN_num_bytes(data->grp->prime), y);
     334                 :          2 :         ptr += BN_num_bytes(data->grp->prime);
     335                 :          2 :         BN_bin2bn(ptr, BN_num_bytes(data->grp->order), data->server_scalar);
     336         [ -  + ]:          2 :         if (!EC_POINT_set_affine_coordinates_GFp(data->grp->group,
     337                 :            :                                                  data->server_element, x, y,
     338                 :            :                                                  data->bnctx)) {
     339                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PWD (peer): setting peer element "
     340                 :            :                            "fail");
     341                 :          0 :                 goto fin;
     342                 :            :         }
     343                 :            : 
     344                 :            :         /* check to ensure server's element is not in a small sub-group */
     345         [ -  + ]:          2 :         if (BN_cmp(cofactor, BN_value_one())) {
     346         [ #  # ]:          0 :                 if (!EC_POINT_mul(data->grp->group, point, NULL,
     347                 :          0 :                                   data->server_element, cofactor, NULL)) {
     348                 :          0 :                         wpa_printf(MSG_INFO, "EAP-PWD (peer): cannot multiply "
     349                 :            :                                    "server element by order!\n");
     350                 :          0 :                         goto fin;
     351                 :            :                 }
     352         [ #  # ]:          0 :                 if (EC_POINT_is_at_infinity(data->grp->group, point)) {
     353                 :          0 :                         wpa_printf(MSG_INFO, "EAP-PWD (peer): server element "
     354                 :            :                                    "is at infinity!\n");
     355                 :          0 :                         goto fin;
     356                 :            :                 }
     357                 :            :         }
     358                 :            : 
     359                 :            :         /* compute the shared key, k */
     360         [ +  - ]:          2 :         if ((!EC_POINT_mul(data->grp->group, K, NULL, data->grp->pwe,
     361         [ +  - ]:          2 :                            data->server_scalar, data->bnctx)) ||
     362                 :          2 :             (!EC_POINT_add(data->grp->group, K, K, data->server_element,
     363         [ -  + ]:          2 :                            data->bnctx)) ||
     364                 :          2 :             (!EC_POINT_mul(data->grp->group, K, NULL, K, data->private_value,
     365                 :            :                            data->bnctx))) {
     366                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PWD (peer): computing shared key "
     367                 :            :                            "fail");
     368                 :          0 :                 goto fin;
     369                 :            :         }
     370                 :            : 
     371                 :            :         /* ensure that the shared key isn't in a small sub-group */
     372         [ -  + ]:          2 :         if (BN_cmp(cofactor, BN_value_one())) {
     373         [ #  # ]:          0 :                 if (!EC_POINT_mul(data->grp->group, K, NULL, K, cofactor,
     374                 :            :                                   NULL)) {
     375                 :          0 :                         wpa_printf(MSG_INFO, "EAP-PWD (peer): cannot multiply "
     376                 :            :                                    "shared key point by order");
     377                 :          0 :                         goto fin;
     378                 :            :                 }
     379                 :            :         }
     380                 :            : 
     381                 :            :         /*
     382                 :            :          * This check is strictly speaking just for the case above where
     383                 :            :          * co-factor > 1 but it was suggested that even though this is probably
     384                 :            :          * never going to happen it is a simple and safe check "just to be
     385                 :            :          * sure" so let's be safe.
     386                 :            :          */
     387         [ -  + ]:          2 :         if (EC_POINT_is_at_infinity(data->grp->group, K)) {
     388                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PWD (peer): shared key point is at "
     389                 :            :                            "infinity!\n");
     390                 :          0 :                 goto fin;
     391                 :            :         }
     392                 :            : 
     393         [ -  + ]:          2 :         if (!EC_POINT_get_affine_coordinates_GFp(data->grp->group, K, data->k,
     394                 :            :                                                  NULL, data->bnctx)) {
     395                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PWD (peer): unable to extract "
     396                 :            :                            "shared secret from point");
     397                 :          0 :                 goto fin;
     398                 :            :         }
     399                 :            : 
     400                 :            :         /* now do the response */
     401         [ -  + ]:          2 :         if (!EC_POINT_get_affine_coordinates_GFp(data->grp->group,
     402                 :          2 :                                                  data->my_element, x, y,
     403                 :            :                                                  data->bnctx)) {
     404                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PWD (peer): point assignment fail");
     405                 :          0 :                 goto fin;
     406                 :            :         }
     407                 :            : 
     408   [ +  -  -  + ]:          4 :         if (((scalar = os_malloc(BN_num_bytes(data->grp->order))) == NULL) ||
     409                 :          2 :             ((element = os_malloc(BN_num_bytes(data->grp->prime) * 2)) ==
     410                 :            :              NULL)) {
     411                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PWD (peer): data allocation fail");
     412                 :          0 :                 goto fin;
     413                 :            :         }
     414                 :            : 
     415                 :            :         /*
     416                 :            :          * bignums occupy as little memory as possible so one that is
     417                 :            :          * sufficiently smaller than the prime or order might need pre-pending
     418                 :            :          * with zeros.
     419                 :            :          */
     420                 :          2 :         os_memset(scalar, 0, BN_num_bytes(data->grp->order));
     421                 :          2 :         os_memset(element, 0, BN_num_bytes(data->grp->prime) * 2);
     422                 :          2 :         offset = BN_num_bytes(data->grp->order) -
     423                 :          2 :                 BN_num_bytes(data->my_scalar);
     424                 :          2 :         BN_bn2bin(data->my_scalar, scalar + offset);
     425                 :            : 
     426                 :          2 :         offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(x);
     427                 :          2 :         BN_bn2bin(x, element + offset);
     428                 :          2 :         offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(y);
     429                 :          2 :         BN_bn2bin(y, element + BN_num_bytes(data->grp->prime) + offset);
     430                 :            : 
     431                 :          4 :         data->outbuf = wpabuf_alloc(BN_num_bytes(data->grp->order) +
     432                 :          2 :                                     2 * BN_num_bytes(data->grp->prime));
     433         [ -  + ]:          2 :         if (data->outbuf == NULL)
     434                 :          0 :                 goto fin;
     435                 :            : 
     436                 :            :         /* we send the element as (x,y) follwed by the scalar */
     437                 :          2 :         wpabuf_put_data(data->outbuf, element,
     438                 :          2 :                         2 * BN_num_bytes(data->grp->prime));
     439                 :          2 :         wpabuf_put_data(data->outbuf, scalar, BN_num_bytes(data->grp->order));
     440                 :            : 
     441                 :            : fin:
     442                 :          2 :         os_free(scalar);
     443                 :          2 :         os_free(element);
     444                 :          2 :         BN_free(x);
     445                 :          2 :         BN_free(y);
     446                 :          2 :         BN_free(cofactor);
     447                 :          2 :         EC_POINT_free(K);
     448                 :          2 :         EC_POINT_free(point);
     449         [ -  + ]:          2 :         if (data->outbuf == NULL)
     450                 :          0 :                 eap_pwd_state(data, FAILURE);
     451                 :            :         else
     452                 :          2 :                 eap_pwd_state(data, PWD_Confirm_Req);
     453                 :          2 : }
     454                 :            : 
     455                 :            : 
     456                 :            : static void
     457                 :          2 : eap_pwd_perform_confirm_exchange(struct eap_sm *sm, struct eap_pwd_data *data,
     458                 :            :                                  struct eap_method_ret *ret,
     459                 :            :                                  const struct wpabuf *reqData,
     460                 :            :                                  const u8 *payload, size_t payload_len)
     461                 :            : {
     462                 :          2 :         BIGNUM *x = NULL, *y = NULL;
     463                 :            :         struct crypto_hash *hash;
     464                 :            :         u32 cs;
     465                 :            :         u16 grp;
     466                 :          2 :         u8 conf[SHA256_MAC_LEN], *cruft = NULL, *ptr;
     467                 :            :         int offset;
     468                 :            : 
     469                 :            :         /*
     470                 :            :          * first build up the ciphersuite which is group | random_function |
     471                 :            :          *      prf
     472                 :            :          */
     473                 :          2 :         grp = htons(data->group_num);
     474                 :          2 :         ptr = (u8 *) &cs;
     475                 :          2 :         os_memcpy(ptr, &grp, sizeof(u16));
     476                 :          2 :         ptr += sizeof(u16);
     477                 :          2 :         *ptr = EAP_PWD_DEFAULT_RAND_FUNC;
     478                 :          2 :         ptr += sizeof(u8);
     479                 :          2 :         *ptr = EAP_PWD_DEFAULT_PRF;
     480                 :            : 
     481                 :            :         /* each component of the cruft will be at most as big as the prime */
     482 [ +  - ][ +  - ]:          2 :         if (((cruft = os_malloc(BN_num_bytes(data->grp->prime))) == NULL) ||
     483         [ -  + ]:          2 :             ((x = BN_new()) == NULL) || ((y = BN_new()) == NULL)) {
     484                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PWD (server): confirm allocation "
     485                 :            :                            "fail");
     486                 :          0 :                 goto fin;
     487                 :            :         }
     488                 :            : 
     489                 :            :         /*
     490                 :            :          * server's commit is H(k | server_element | server_scalar |
     491                 :            :          *                      peer_element | peer_scalar | ciphersuite)
     492                 :            :          */
     493                 :          2 :         hash = eap_pwd_h_init();
     494         [ -  + ]:          2 :         if (hash == NULL)
     495                 :          0 :                 goto fin;
     496                 :            : 
     497                 :            :         /*
     498                 :            :          * zero the memory each time because this is mod prime math and some
     499                 :            :          * value may start with a few zeros and the previous one did not.
     500                 :            :          */
     501                 :          2 :         os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
     502                 :          2 :         offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(data->k);
     503                 :          2 :         BN_bn2bin(data->k, cruft + offset);
     504                 :          2 :         eap_pwd_h_update(hash, cruft, BN_num_bytes(data->grp->prime));
     505                 :            : 
     506                 :            :         /* server element: x, y */
     507         [ -  + ]:          2 :         if (!EC_POINT_get_affine_coordinates_GFp(data->grp->group,
     508                 :          2 :                                                  data->server_element, x, y,
     509                 :            :                                                  data->bnctx)) {
     510                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PWD (server): confirm point "
     511                 :            :                            "assignment fail");
     512                 :          0 :                 goto fin;
     513                 :            :         }
     514                 :          2 :         os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
     515                 :          2 :         offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(x);
     516                 :          2 :         BN_bn2bin(x, cruft + offset);
     517                 :          2 :         eap_pwd_h_update(hash, cruft, BN_num_bytes(data->grp->prime));
     518                 :          2 :         os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
     519                 :          2 :         offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(y);
     520                 :          2 :         BN_bn2bin(y, cruft + offset);
     521                 :          2 :         eap_pwd_h_update(hash, cruft, BN_num_bytes(data->grp->prime));
     522                 :            : 
     523                 :            :         /* server scalar */
     524                 :          2 :         os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
     525                 :          2 :         offset = BN_num_bytes(data->grp->order) -
     526                 :          2 :                 BN_num_bytes(data->server_scalar);
     527                 :          2 :         BN_bn2bin(data->server_scalar, cruft + offset);
     528                 :          2 :         eap_pwd_h_update(hash, cruft, BN_num_bytes(data->grp->order));
     529                 :            : 
     530                 :            :         /* my element: x, y */
     531         [ -  + ]:          2 :         if (!EC_POINT_get_affine_coordinates_GFp(data->grp->group,
     532                 :          2 :                                                  data->my_element, x, y,
     533                 :            :                                                  data->bnctx)) {
     534                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PWD (server): confirm point "
     535                 :            :                            "assignment fail");
     536                 :          0 :                 goto fin;
     537                 :            :         }
     538                 :            : 
     539                 :          2 :         os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
     540                 :          2 :         offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(x);
     541                 :          2 :         BN_bn2bin(x, cruft + offset);
     542                 :          2 :         eap_pwd_h_update(hash, cruft, BN_num_bytes(data->grp->prime));
     543                 :          2 :         os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
     544                 :          2 :         offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(y);
     545                 :          2 :         BN_bn2bin(y, cruft + offset);
     546                 :          2 :         eap_pwd_h_update(hash, cruft, BN_num_bytes(data->grp->prime));
     547                 :            : 
     548                 :            :         /* my scalar */
     549                 :          2 :         os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
     550                 :          2 :         offset = BN_num_bytes(data->grp->order) -
     551                 :          2 :                 BN_num_bytes(data->my_scalar);
     552                 :          2 :         BN_bn2bin(data->my_scalar, cruft + offset);
     553                 :          2 :         eap_pwd_h_update(hash, cruft, BN_num_bytes(data->grp->order));
     554                 :            : 
     555                 :            :         /* the ciphersuite */
     556                 :          2 :         eap_pwd_h_update(hash, (u8 *) &cs, sizeof(u32));
     557                 :            : 
     558                 :            :         /* random function fin */
     559                 :          2 :         eap_pwd_h_final(hash, conf);
     560                 :            : 
     561                 :          2 :         ptr = (u8 *) payload;
     562         [ -  + ]:          2 :         if (os_memcmp(conf, ptr, SHA256_MAC_LEN)) {
     563                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PWD (peer): confirm did not verify");
     564                 :          0 :                 goto fin;
     565                 :            :         }
     566                 :            : 
     567                 :          2 :         wpa_printf(MSG_DEBUG, "EAP-pwd (peer): confirm verified");
     568                 :            : 
     569                 :            :         /*
     570                 :            :          * compute confirm:
     571                 :            :          *  H(k | peer_element | peer_scalar | server_element | server_scalar |
     572                 :            :          *    ciphersuite)
     573                 :            :          */
     574                 :          2 :         hash = eap_pwd_h_init();
     575         [ -  + ]:          2 :         if (hash == NULL)
     576                 :          0 :                 goto fin;
     577                 :            : 
     578                 :            :         /* k */
     579                 :          2 :         os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
     580                 :          2 :         offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(data->k);
     581                 :          2 :         BN_bn2bin(data->k, cruft + offset);
     582                 :          2 :         eap_pwd_h_update(hash, cruft, BN_num_bytes(data->grp->prime));
     583                 :            : 
     584                 :            :         /* my element */
     585         [ -  + ]:          2 :         if (!EC_POINT_get_affine_coordinates_GFp(data->grp->group,
     586                 :          2 :                                                  data->my_element, x, y,
     587                 :            :                                                  data->bnctx)) {
     588                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PWD (peer): confirm point "
     589                 :            :                            "assignment fail");
     590                 :          0 :                 goto fin;
     591                 :            :         }
     592                 :          2 :         os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
     593                 :          2 :         offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(x);
     594                 :          2 :         BN_bn2bin(x, cruft + offset);
     595                 :          2 :         eap_pwd_h_update(hash, cruft, BN_num_bytes(data->grp->prime));
     596                 :          2 :         os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
     597                 :          2 :         offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(y);
     598                 :          2 :         BN_bn2bin(y, cruft + offset);
     599                 :          2 :         eap_pwd_h_update(hash, cruft, BN_num_bytes(data->grp->prime));
     600                 :            : 
     601                 :            :         /* my scalar */
     602                 :          2 :         os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
     603                 :          2 :         offset = BN_num_bytes(data->grp->order) -
     604                 :          2 :                 BN_num_bytes(data->my_scalar);
     605                 :          2 :         BN_bn2bin(data->my_scalar, cruft + offset);
     606                 :          2 :         eap_pwd_h_update(hash, cruft, BN_num_bytes(data->grp->order));
     607                 :            : 
     608                 :            :         /* server element: x, y */
     609         [ -  + ]:          2 :         if (!EC_POINT_get_affine_coordinates_GFp(data->grp->group,
     610                 :          2 :                                                  data->server_element, x, y,
     611                 :            :                                                  data->bnctx)) {
     612                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PWD (peer): confirm point "
     613                 :            :                            "assignment fail");
     614                 :          0 :                 goto fin;
     615                 :            :         }
     616                 :          2 :         os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
     617                 :          2 :         offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(x);
     618                 :          2 :         BN_bn2bin(x, cruft + offset);
     619                 :          2 :         eap_pwd_h_update(hash, cruft, BN_num_bytes(data->grp->prime));
     620                 :          2 :         os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
     621                 :          2 :         offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(y);
     622                 :          2 :         BN_bn2bin(y, cruft + offset);
     623                 :          2 :         eap_pwd_h_update(hash, cruft, BN_num_bytes(data->grp->prime));
     624                 :            : 
     625                 :            :         /* server scalar */
     626                 :          2 :         os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
     627                 :          2 :         offset = BN_num_bytes(data->grp->order) -
     628                 :          2 :                 BN_num_bytes(data->server_scalar);
     629                 :          2 :         BN_bn2bin(data->server_scalar, cruft + offset);
     630                 :          2 :         eap_pwd_h_update(hash, cruft, BN_num_bytes(data->grp->order));
     631                 :            : 
     632                 :            :         /* the ciphersuite */
     633                 :          2 :         eap_pwd_h_update(hash, (u8 *) &cs, sizeof(u32));
     634                 :            : 
     635                 :            :         /* all done */
     636                 :          2 :         eap_pwd_h_final(hash, conf);
     637                 :            : 
     638         [ -  + ]:          2 :         if (compute_keys(data->grp, data->bnctx, data->k,
     639                 :            :                          data->my_scalar, data->server_scalar, conf, ptr,
     640                 :          2 :                          &cs, data->msk, data->emsk) < 0) {
     641                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PWD (peer): unable to compute MSK | "
     642                 :            :                            "EMSK");
     643                 :          0 :                 goto fin;
     644                 :            :         }
     645                 :            : 
     646                 :          2 :         data->outbuf = wpabuf_alloc(SHA256_MAC_LEN);
     647         [ -  + ]:          2 :         if (data->outbuf == NULL)
     648                 :          0 :                 goto fin;
     649                 :            : 
     650                 :          2 :         wpabuf_put_data(data->outbuf, conf, SHA256_MAC_LEN);
     651                 :            : 
     652                 :            : fin:
     653                 :          2 :         os_free(cruft);
     654                 :          2 :         BN_free(x);
     655                 :          2 :         BN_free(y);
     656                 :          2 :         ret->methodState = METHOD_DONE;
     657         [ -  + ]:          2 :         if (data->outbuf == NULL) {
     658                 :          0 :                 ret->decision = DECISION_FAIL;
     659                 :          0 :                 eap_pwd_state(data, FAILURE);
     660                 :            :         } else {
     661                 :          2 :                 ret->decision = DECISION_UNCOND_SUCC;
     662                 :          2 :                 eap_pwd_state(data, SUCCESS);
     663                 :            :         }
     664                 :          2 : }
     665                 :            : 
     666                 :            : 
     667                 :            : static struct wpabuf *
     668                 :          6 : eap_pwd_process(struct eap_sm *sm, void *priv, struct eap_method_ret *ret,
     669                 :            :                 const struct wpabuf *reqData)
     670                 :            : {
     671                 :          6 :         struct eap_pwd_data *data = priv;
     672                 :          6 :         struct wpabuf *resp = NULL;
     673                 :            :         const u8 *pos, *buf;
     674                 :            :         size_t len;
     675                 :          6 :         u16 tot_len = 0;
     676                 :            :         u8 lm_exch;
     677                 :            : 
     678                 :          6 :         pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_PWD, reqData, &len);
     679 [ +  - ][ -  + ]:          6 :         if ((pos == NULL) || (len < 1)) {
     680         [ #  # ]:          0 :                 wpa_printf(MSG_DEBUG, "EAP-pwd: Got a frame but pos is %s and "
     681                 :            :                            "len is %d",
     682                 :            :                            pos == NULL ? "NULL" : "not NULL", (int) len);
     683                 :          0 :                 ret->ignore = TRUE;
     684                 :          0 :                 return NULL;
     685                 :            :         }
     686                 :            : 
     687                 :          6 :         ret->ignore = FALSE;
     688                 :          6 :         ret->methodState = METHOD_MAY_CONT;
     689                 :          6 :         ret->decision = DECISION_FAIL;
     690                 :          6 :         ret->allowNotifications = FALSE;
     691                 :            : 
     692                 :          6 :         lm_exch = *pos;
     693                 :          6 :         pos++;                  /* skip over the bits and the exch */
     694                 :          6 :         len--;
     695                 :            : 
     696                 :            :         /*
     697                 :            :          * we're fragmenting so send out the next fragment
     698                 :            :          */
     699         [ -  + ]:          6 :         if (data->out_frag_pos) {
     700                 :            :                 /*
     701                 :            :                  * this should be an ACK
     702                 :            :                  */
     703         [ #  # ]:          0 :                 if (len)
     704                 :          0 :                         wpa_printf(MSG_INFO, "Bad Response! Fragmenting but "
     705                 :            :                                    "not an ACK");
     706                 :            : 
     707                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-pwd: Got an ACK for a fragment");
     708                 :            :                 /*
     709                 :            :                  * check if there are going to be more fragments
     710                 :            :                  */
     711                 :          0 :                 len = wpabuf_len(data->outbuf) - data->out_frag_pos;
     712         [ #  # ]:          0 :                 if ((len + EAP_PWD_HDR_SIZE) > data->mtu) {
     713                 :          0 :                         len = data->mtu - EAP_PWD_HDR_SIZE;
     714                 :          0 :                         EAP_PWD_SET_MORE_BIT(lm_exch);
     715                 :            :                 }
     716                 :          0 :                 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_PWD,
     717                 :            :                                      EAP_PWD_HDR_SIZE + len,
     718                 :          0 :                                      EAP_CODE_RESPONSE, eap_get_id(reqData));
     719         [ #  # ]:          0 :                 if (resp == NULL) {
     720                 :          0 :                         wpa_printf(MSG_INFO, "Unable to allocate memory for "
     721                 :            :                                    "next fragment!");
     722                 :          0 :                         return NULL;
     723                 :            :                 }
     724                 :          0 :                 wpabuf_put_u8(resp, lm_exch);
     725                 :          0 :                 buf = wpabuf_head_u8(data->outbuf);
     726                 :          0 :                 wpabuf_put_data(resp, buf + data->out_frag_pos, len);
     727                 :          0 :                 data->out_frag_pos += len;
     728                 :            :                 /*
     729                 :            :                  * this is the last fragment so get rid of the out buffer
     730                 :            :                  */
     731         [ #  # ]:          0 :                 if (data->out_frag_pos >= wpabuf_len(data->outbuf)) {
     732                 :          0 :                         wpabuf_free(data->outbuf);
     733                 :          0 :                         data->outbuf = NULL;
     734                 :          0 :                         data->out_frag_pos = 0;
     735                 :            :                 }
     736         [ #  # ]:          0 :                 wpa_printf(MSG_DEBUG, "EAP-pwd: Send %s fragment of %d bytes",
     737                 :          0 :                            data->out_frag_pos == 0 ? "last" : "next",
     738                 :            :                            (int) len);
     739                 :          0 :                 return resp;
     740                 :            :         }
     741                 :            : 
     742                 :            :         /*
     743                 :            :          * see if this is a fragment that needs buffering
     744                 :            :          *
     745                 :            :          * if it's the first fragment there'll be a length field
     746                 :            :          */
     747         [ -  + ]:          6 :         if (EAP_PWD_GET_LENGTH_BIT(lm_exch)) {
     748                 :          0 :                 tot_len = WPA_GET_BE16(pos);
     749                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-pwd: Incoming fragments whose "
     750                 :            :                            "total length = %d", tot_len);
     751                 :          0 :                 data->inbuf = wpabuf_alloc(tot_len);
     752         [ #  # ]:          0 :                 if (data->inbuf == NULL) {
     753                 :          0 :                         wpa_printf(MSG_INFO, "Out of memory to buffer "
     754                 :            :                                    "fragments!");
     755                 :          0 :                         return NULL;
     756                 :            :                 }
     757                 :          0 :                 pos += sizeof(u16);
     758                 :          0 :                 len -= sizeof(u16);
     759                 :            :         }
     760                 :            :         /*
     761                 :            :          * buffer and ACK the fragment
     762                 :            :          */
     763         [ -  + ]:          6 :         if (EAP_PWD_GET_MORE_BIT(lm_exch)) {
     764                 :          0 :                 data->in_frag_pos += len;
     765         [ #  # ]:          0 :                 if (data->in_frag_pos > wpabuf_size(data->inbuf)) {
     766                 :          0 :                         wpa_printf(MSG_INFO, "EAP-pwd: Buffer overflow attack "
     767                 :            :                                    "detected (%d vs. %d)!",
     768                 :          0 :                                    (int) data->in_frag_pos,
     769                 :          0 :                                    (int) wpabuf_len(data->inbuf));
     770                 :          0 :                         wpabuf_free(data->inbuf);
     771                 :          0 :                         data->in_frag_pos = 0;
     772                 :          0 :                         return NULL;
     773                 :            :                 }
     774                 :          0 :                 wpabuf_put_data(data->inbuf, pos, len);
     775                 :            : 
     776                 :          0 :                 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_PWD,
     777                 :            :                                      EAP_PWD_HDR_SIZE,
     778                 :          0 :                                      EAP_CODE_RESPONSE, eap_get_id(reqData));
     779         [ #  # ]:          0 :                 if (resp != NULL)
     780                 :          0 :                         wpabuf_put_u8(resp, (EAP_PWD_GET_EXCHANGE(lm_exch)));
     781                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-pwd: ACKing a %d byte fragment",
     782                 :            :                            (int) len);
     783                 :          0 :                 return resp;
     784                 :            :         }
     785                 :            :         /*
     786                 :            :          * we're buffering and this is the last fragment
     787                 :            :          */
     788         [ -  + ]:          6 :         if (data->in_frag_pos) {
     789                 :          0 :                 wpabuf_put_data(data->inbuf, pos, len);
     790                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-pwd: Last fragment, %d bytes",
     791                 :            :                            (int) len);
     792                 :          0 :                 data->in_frag_pos += len;
     793                 :          0 :                 pos = wpabuf_head_u8(data->inbuf);
     794                 :          0 :                 len = data->in_frag_pos;
     795                 :            :         }
     796                 :          6 :         wpa_printf(MSG_DEBUG, "EAP-pwd: processing frame: exch %d, len %d",
     797                 :            :                    EAP_PWD_GET_EXCHANGE(lm_exch), (int) len);
     798                 :            : 
     799   [ +  +  +  - ]:          6 :         switch (EAP_PWD_GET_EXCHANGE(lm_exch)) {
     800                 :            :         case EAP_PWD_OPCODE_ID_EXCH:
     801                 :          2 :                 eap_pwd_perform_id_exchange(sm, data, ret, reqData,
     802                 :            :                                             pos, len);
     803                 :          2 :                 break;
     804                 :            :         case EAP_PWD_OPCODE_COMMIT_EXCH:
     805                 :          2 :                 eap_pwd_perform_commit_exchange(sm, data, ret, reqData,
     806                 :            :                                                 pos, len);
     807                 :          2 :                 break;
     808                 :            :         case EAP_PWD_OPCODE_CONFIRM_EXCH:
     809                 :          2 :                 eap_pwd_perform_confirm_exchange(sm, data, ret, reqData,
     810                 :            :                                                  pos, len);
     811                 :          2 :                 break;
     812                 :            :         default:
     813                 :          0 :                 wpa_printf(MSG_INFO, "EAP-pwd: Ignoring message with unknown "
     814                 :            :                            "opcode %d", lm_exch);
     815                 :          0 :                 break;
     816                 :            :         }
     817                 :            :         /*
     818                 :            :          * if we buffered the just processed input now's the time to free it
     819                 :            :          */
     820         [ -  + ]:          6 :         if (data->in_frag_pos) {
     821                 :          0 :                 wpabuf_free(data->inbuf);
     822                 :          0 :                 data->in_frag_pos = 0;
     823                 :            :         }
     824                 :            : 
     825         [ -  + ]:          6 :         if (data->outbuf == NULL)
     826                 :          0 :                 return NULL;        /* generic failure */
     827                 :            : 
     828                 :            :         /*
     829                 :            :          * we have output! Do we need to fragment it?
     830                 :            :          */
     831                 :          6 :         len = wpabuf_len(data->outbuf);
     832         [ -  + ]:          6 :         if ((len + EAP_PWD_HDR_SIZE) > data->mtu) {
     833                 :          0 :                 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_PWD, data->mtu,
     834                 :          0 :                                      EAP_CODE_RESPONSE, eap_get_id(reqData));
     835                 :            :                 /*
     836                 :            :                  * if so it's the first so include a length field
     837                 :            :                  */
     838                 :          0 :                 EAP_PWD_SET_LENGTH_BIT(lm_exch);
     839                 :          0 :                 EAP_PWD_SET_MORE_BIT(lm_exch);
     840                 :          0 :                 tot_len = len;
     841                 :            :                 /*
     842                 :            :                  * keep the packet at the MTU
     843                 :            :                  */
     844                 :          0 :                 len = data->mtu - EAP_PWD_HDR_SIZE - sizeof(u16);
     845                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-pwd: Fragmenting output, total "
     846                 :            :                            "length = %d", tot_len);
     847                 :            :         } else {
     848                 :          6 :                 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_PWD,
     849                 :            :                                      EAP_PWD_HDR_SIZE + len,
     850                 :          6 :                                      EAP_CODE_RESPONSE, eap_get_id(reqData));
     851                 :            :         }
     852         [ -  + ]:          6 :         if (resp == NULL)
     853                 :          0 :                 return NULL;
     854                 :            : 
     855                 :          6 :         wpabuf_put_u8(resp, lm_exch);
     856         [ -  + ]:          6 :         if (EAP_PWD_GET_LENGTH_BIT(lm_exch)) {
     857                 :          0 :                 wpabuf_put_be16(resp, tot_len);
     858                 :          0 :                 data->out_frag_pos += len;
     859                 :            :         }
     860                 :          6 :         buf = wpabuf_head_u8(data->outbuf);
     861                 :          6 :         wpabuf_put_data(resp, buf, len);
     862                 :            :         /*
     863                 :            :          * if we're not fragmenting then there's no need to carry this around
     864                 :            :          */
     865         [ +  - ]:          6 :         if (data->out_frag_pos == 0) {
     866                 :          6 :                 wpabuf_free(data->outbuf);
     867                 :          6 :                 data->outbuf = NULL;
     868                 :          6 :                 data->out_frag_pos = 0;
     869                 :            :         }
     870                 :            : 
     871                 :          6 :         return resp;
     872                 :            : }
     873                 :            : 
     874                 :            : 
     875                 :          6 : static Boolean eap_pwd_key_available(struct eap_sm *sm, void *priv)
     876                 :            : {
     877                 :          6 :         struct eap_pwd_data *data = priv;
     878                 :          6 :         return data->state == SUCCESS;
     879                 :            : }
     880                 :            : 
     881                 :            : 
     882                 :          0 : static u8 * eap_pwd_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
     883                 :            : {
     884                 :          0 :         struct eap_pwd_data *data = priv;
     885                 :            :         u8 *key;
     886                 :            : 
     887         [ #  # ]:          0 :         if (data->state != SUCCESS)
     888                 :          0 :                 return NULL;
     889                 :            : 
     890         [ #  # ]:          0 :         if ((key = os_malloc(EAP_EMSK_LEN)) == NULL)
     891                 :          0 :                 return NULL;
     892                 :            : 
     893                 :          0 :         os_memcpy(key, data->emsk, EAP_EMSK_LEN);
     894                 :          0 :         *len = EAP_EMSK_LEN;
     895                 :            : 
     896                 :          0 :         return key;
     897                 :            : }
     898                 :            : 
     899                 :            : 
     900                 :          3 : int eap_peer_pwd_register(void)
     901                 :            : {
     902                 :            :         struct eap_method *eap;
     903                 :            :         int ret;
     904                 :            : 
     905                 :          3 :         EVP_add_digest(EVP_sha256());
     906                 :          3 :         eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
     907                 :            :                                     EAP_VENDOR_IETF, EAP_TYPE_PWD, "PWD");
     908         [ -  + ]:          3 :         if (eap == NULL)
     909                 :          0 :                 return -1;
     910                 :            : 
     911                 :          3 :         eap->init = eap_pwd_init;
     912                 :          3 :         eap->deinit = eap_pwd_deinit;
     913                 :          3 :         eap->process = eap_pwd_process;
     914                 :          3 :         eap->isKeyAvailable = eap_pwd_key_available;
     915                 :          3 :         eap->getKey = eap_pwd_getkey;
     916                 :          3 :         eap->get_emsk = eap_pwd_get_emsk;
     917                 :            : 
     918                 :          3 :         ret = eap_peer_method_register(eap);
     919         [ -  + ]:          3 :         if (ret)
     920                 :          0 :                 eap_peer_method_free(eap);
     921                 :          3 :         return ret;
     922                 :            : }

Generated by: LCOV version 1.9