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 1401264779 Lines: 404 526 76.8 %
Date: 2014-05-28 Functions: 12 13 92.3 %

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

Generated by: LCOV version 1.10