LCOV - code coverage report
Current view: top level - exchange - taler-exchange-httpd_refresh_melt.c (source / functions) Hit Total Coverage
Test: rcoverage.info Lines: 208 285 73.0 %
Date: 2017-11-25 11:31:41 Functions: 10 10 100.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-2017 Inria & GNUnet e.V.
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify it under the
       6             :   terms of the GNU Affero General Public License as published by the Free Software
       7             :   Foundation; either version 3, or (at your option) any later version.
       8             : 
       9             :   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
      10             :   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
      11             :   A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
      12             : 
      13             :   You should have received a copy of the GNU Affero General Public License along with
      14             :   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
      15             : */
      16             : /**
      17             :  * @file taler-exchange-httpd_refresh_melt.c
      18             :  * @brief Handle /refresh/melt requests
      19             :  * @author Florian Dold
      20             :  * @author Benedikt Mueller
      21             :  * @author Christian Grothoff
      22             :  */
      23             : #include "platform.h"
      24             : #include <gnunet/gnunet_util_lib.h>
      25             : #include <jansson.h>
      26             : #include <microhttpd.h>
      27             : #include "taler-exchange-httpd_parsing.h"
      28             : #include "taler-exchange-httpd_mhd.h"
      29             : #include "taler-exchange-httpd_refresh_melt.h"
      30             : #include "taler-exchange-httpd_responses.h"
      31             : #include "taler-exchange-httpd_keystate.h"
      32             : 
      33             : 
      34             : /**
      35             :  * @brief Details about a melt operation of an individual coin.
      36             :  */
      37             : struct TEH_DB_MeltDetails
      38             : {
      39             : 
      40             :   /**
      41             :    * Information about the coin being melted.
      42             :    */
      43             :   struct TALER_CoinPublicInfo coin_info;
      44             : 
      45             :   /**
      46             :    * Signature allowing the melt (using
      47             :    * a `struct TALER_EXCHANGEDB_RefreshMeltConfirmSignRequestBody`) to sign over.
      48             :    */
      49             :   struct TALER_CoinSpendSignatureP melt_sig;
      50             : 
      51             :   /**
      52             :    * How much of the coin's value did the client allow to be melted?
      53             :    * This amount includes the fees, so the final amount contributed
      54             :    * to the melt is this value minus the fee for melting the coin.
      55             :    */
      56             :   struct TALER_Amount melt_amount_with_fee;
      57             : 
      58             :   /**
      59             :    * What fee is earned by the exchange?  Set delayed during
      60             :    * #verify_coin_public_info().
      61             :    */
      62             :   struct TALER_Amount melt_fee;
      63             : };
      64             : 
      65             : 
      66             : /**
      67             :  * Send a response for a failed "/refresh/melt" request.  The
      68             :  * transaction history of the given coin demonstrates that the
      69             :  * @a residual value of the coin is below the @a requested
      70             :  * contribution of the coin for the melt.  Thus, the exchange
      71             :  * refuses the melt operation.
      72             :  *
      73             :  * @param connection the connection to send the response to
      74             :  * @param coin_pub public key of the coin
      75             :  * @param coin_value original value of the coin
      76             :  * @param tl transaction history for the coin
      77             :  * @param requested how much this coin was supposed to contribute, including fee
      78             :  * @param residual remaining value of the coin (after subtracting @a tl)
      79             :  * @return a MHD result code
      80             :  */
      81             : static int
      82           1 : reply_refresh_melt_insufficient_funds (struct MHD_Connection *connection,
      83             :                                        const struct TALER_CoinSpendPublicKeyP *coin_pub,
      84             :                                        struct TALER_Amount coin_value,
      85             :                                        struct TALER_EXCHANGEDB_TransactionList *tl,
      86             :                                        const struct TALER_Amount *requested,
      87             :                                        const struct TALER_Amount *residual)
      88             : {
      89             :   json_t *history;
      90             : 
      91           1 :   history = TEH_RESPONSE_compile_transaction_history (tl);
      92           1 :   if (NULL == history)
      93           0 :     return TEH_RESPONSE_reply_internal_db_error (connection,
      94             :                                                  TALER_EC_REFRESH_MELT_HISTORY_DB_ERROR_INSUFFICIENT_FUNDS);
      95           1 :   return TEH_RESPONSE_reply_json_pack (connection,
      96             :                                        MHD_HTTP_FORBIDDEN,
      97             :                                        "{s:s, s:I, s:o, s:o, s:o, s:o, s:o}",
      98             :                                        "error",
      99             :                                        "insufficient funds",
     100             :                                        "code",
     101             :                                        (json_int_t) TALER_EC_REFRESH_MELT_INSUFFICIENT_FUNDS,
     102             :                                        "coin_pub",
     103             :                                        GNUNET_JSON_from_data_auto (coin_pub),
     104             :                                        "original_value",
     105             :                                        TALER_JSON_from_amount (&coin_value),
     106             :                                        "residual_value",
     107             :                                        TALER_JSON_from_amount (residual),
     108             :                                        "requested_value",
     109             :                                        TALER_JSON_from_amount (requested),
     110             :                                        "history",
     111             :                                        history);
     112             : }
     113             : 
     114             : 
     115             : /**
     116             :  * Send a response to a "/refresh/melt" request.
     117             :  *
     118             :  * @param connection the connection to send the response to
     119             :  * @param session_hash hash of the refresh session
     120             :  * @param noreveal_index which index will the client not have to reveal
     121             :  * @return a MHD status code
     122             :  */
     123             : static int
     124           1 : reply_refresh_melt_success (struct MHD_Connection *connection,
     125             :                             const struct GNUNET_HashCode *session_hash,
     126             :                             uint16_t noreveal_index)
     127             : {
     128             :   struct TALER_RefreshMeltConfirmationPS body;
     129             :   struct TALER_ExchangePublicKeyP pub;
     130             :   struct TALER_ExchangeSignatureP sig;
     131             :   json_t *sig_json;
     132             : 
     133           1 :   body.purpose.size = htonl (sizeof (struct TALER_RefreshMeltConfirmationPS));
     134           1 :   body.purpose.purpose = htonl (TALER_SIGNATURE_EXCHANGE_CONFIRM_MELT);
     135           1 :   body.session_hash = *session_hash;
     136           1 :   body.noreveal_index = htons (noreveal_index);
     137           1 :   body.reserved = htons (0);
     138           1 :   if (GNUNET_OK !=
     139           1 :       TEH_KS_sign (&body.purpose,
     140             :                    &pub,
     141             :                    &sig))
     142             :   {
     143           0 :     return TEH_RESPONSE_reply_internal_error (connection,
     144             :                                               TALER_EC_EXCHANGE_BAD_CONFIGURATION,
     145             :                                               "no keys");
     146             :   }
     147           1 :   sig_json = GNUNET_JSON_from_data_auto (&sig);
     148           1 :   GNUNET_assert (NULL != sig_json);
     149           1 :   return TEH_RESPONSE_reply_json_pack (connection,
     150             :                                        MHD_HTTP_OK,
     151             :                                        "{s:i, s:o, s:o}",
     152             :                                        "noreveal_index", (int) noreveal_index,
     153             :                                        "exchange_sig", sig_json,
     154             :                                        "exchange_pub", GNUNET_JSON_from_data_auto (&pub));
     155             : }
     156             : 
     157             : 
     158             : /**
     159             :  * Context for the /refresh/melt operation.
     160             :  */
     161             : struct RefreshMeltContext
     162             : {
     163             : 
     164             :   /**
     165             :    * Key state that can be used to lookup keys.
     166             :    */
     167             :   struct TEH_KS_StateHandle *key_state;
     168             : 
     169             :   /**
     170             :    * Information about the denomination key of the coin being
     171             :    * melted.
     172             :    */
     173             :   struct TALER_EXCHANGEDB_DenominationKeyIssueInformation *dki;
     174             : 
     175             :   /**
     176             :    * Array of denominations of the fresh coins.
     177             :    */
     178             :   struct TALER_DenominationPublicKey *denom_pubs;
     179             : 
     180             :   /**
     181             :    * Number of new coins to be generated in the melt.
     182             :    * Size of the @e denom_pubs array.
     183             :    */
     184             :   unsigned int num_newcoins;
     185             : 
     186             :   /**
     187             :    * Details about the coin to be melted.
     188             :    */
     189             :   struct TEH_DB_MeltDetails coin_melt_details;
     190             : 
     191             :   /**
     192             :    * Set to the session hash once the @e hash_context has finished.
     193             :    */
     194             :   struct GNUNET_HashCode session_hash;
     195             : 
     196             :   /**
     197             :    * Hash operation used to calculate the session hash.
     198             :    */
     199             :   struct GNUNET_HashContext *hash_context;
     200             : 
     201             :   /**
     202             :    * Committments to the blinded envelopes for the fresh coins.
     203             :    */
     204             :   struct TALER_EXCHANGEDB_RefreshCommitCoin *commit_coin[TALER_CNC_KAPPA];
     205             : 
     206             :   /**
     207             :    * Commmittments to the transfer public keys.
     208             :    */
     209             :   struct TALER_TransferPublicKeyP transfer_pub[TALER_CNC_KAPPA];
     210             : 
     211             :   /**
     212             :    * Initialized during #refresh_melt_transaction().
     213             :    */
     214             :   struct TALER_EXCHANGEDB_RefreshSession refresh_session;
     215             : 
     216             : };
     217             : 
     218             : 
     219             : /**
     220             :  * Parse coin melt requests from a JSON object and write them to
     221             :  * the database.
     222             :  *
     223             :  * @param connection the connection to send errors to
     224             :  * @param session the database connection
     225             :  * @param[in,out] rmc melt context
     226             :  * @param[out] mhd_ret status code to return to MHD on hard error
     227             :  * @return transaction status code
     228             :  */
     229             : static enum GNUNET_DB_QueryStatus
     230           2 : refresh_check_melt (struct MHD_Connection *connection,
     231             :                     struct TALER_EXCHANGEDB_Session *session,
     232             :                     struct RefreshMeltContext *rmc,
     233             :                     int *mhd_ret)
     234             : {
     235             :   struct TALER_EXCHANGEDB_TransactionList *tl;
     236           2 :   struct TALER_EXCHANGEDB_RefreshMelt *meltp = &rmc->refresh_session.melt;
     237             :   struct TALER_Amount coin_value;
     238             :   struct TALER_Amount coin_residual;
     239             :   struct TALER_Amount spent;
     240             :   enum GNUNET_DB_QueryStatus qs;
     241             : 
     242           2 :   TALER_amount_ntoh (&coin_value,
     243           2 :                      &rmc->dki->issue.properties.value);
     244             :   /* fee for THIS transaction; the melt amount includes the fee! */
     245           2 :   spent = rmc->coin_melt_details.melt_amount_with_fee;
     246             :   /* add historic transaction costs of this coin */
     247           4 :   qs = TEH_plugin->get_coin_transactions (TEH_plugin->cls,
     248             :                                           session,
     249           2 :                                           &rmc->coin_melt_details.coin_info.coin_pub,
     250             :                                           &tl);
     251           2 :   if (0 > qs)
     252             :   {
     253           0 :     if (GNUNET_DB_STATUS_HARD_ERROR == qs)
     254           0 :       *mhd_ret = TEH_RESPONSE_reply_internal_db_error (connection,
     255             :                                                        TALER_EC_REFRESH_MELT_DB_FETCH_ERROR);
     256           0 :     return qs;
     257             :   }
     258           2 :   if (GNUNET_OK !=
     259           2 :       TEH_DB_calculate_transaction_list_totals (tl,
     260             :                                                 &spent,
     261             :                                                 &spent))
     262             :   {
     263           0 :     GNUNET_break (0);
     264           0 :     TEH_plugin->free_coin_transaction_list (TEH_plugin->cls,
     265             :                                             tl);
     266           0 :     *mhd_ret = TEH_RESPONSE_reply_internal_db_error (connection,
     267             :                                                      TALER_EC_REFRESH_MELT_COIN_HISTORY_COMPUTATION_FAILED);
     268           0 :     return GNUNET_DB_STATUS_HARD_ERROR;
     269             :   }
     270             :   /* Refuse to refresh when the coin's value is insufficient
     271             :      for the cost of all transactions. */
     272           2 :   if (TALER_amount_cmp (&coin_value,
     273             :                         &spent) < 0)
     274             :   {
     275           1 :     GNUNET_assert (GNUNET_SYSERR !=
     276             :                    TALER_amount_subtract (&coin_residual,
     277             :                                           &spent,
     278             :                                           &rmc->coin_melt_details.melt_amount_with_fee));
     279           2 :     *mhd_ret = reply_refresh_melt_insufficient_funds (connection,
     280           1 :                                                       &rmc->coin_melt_details.coin_info.coin_pub,
     281             :                                                       coin_value,
     282             :                                                       tl,
     283           1 :                                                       &rmc->coin_melt_details.melt_amount_with_fee,
     284             :                                                       &coin_residual);
     285           1 :     TEH_plugin->free_coin_transaction_list (TEH_plugin->cls,
     286             :                                             tl);
     287           1 :     return GNUNET_DB_STATUS_HARD_ERROR;
     288             :   }
     289           1 :   TEH_plugin->free_coin_transaction_list (TEH_plugin->cls,
     290             :                                           tl);
     291             : 
     292           1 :   meltp->coin = rmc->coin_melt_details.coin_info;
     293           1 :   meltp->coin_sig = rmc->coin_melt_details.melt_sig;
     294           1 :   meltp->session_hash = rmc->session_hash;
     295           1 :   meltp->amount_with_fee = rmc->coin_melt_details.melt_amount_with_fee;
     296           1 :   meltp->melt_fee = rmc->coin_melt_details.melt_fee;
     297           1 :   return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
     298             : }
     299             : 
     300             : 
     301             : /**
     302             :  * Execute a "/refresh/melt".  We have been given a list of valid
     303             :  * coins and a request to melt them into the given @a
     304             :  * refresh_session_pub.  Check that the coins all have the required
     305             :  * value left and if so, store that they have been melted and confirm
     306             :  * the melting operation to the client.
     307             :  *
     308             :  * If it returns a non-error code, the transaction logic MUST NOT
     309             :  * queue a MHD response.  IF it returns an hard error, the transaction
     310             :  * logic MUST queue a MHD response and set @a mhd_ret.  If it returns
     311             :  * the soft error code, the function MAY be called again to retry and
     312             :  * MUST not queue a MHD response.
     313             :  *
     314             :  * @param cls our `struct RefreshMeltContext`
     315             :  * @param connection MHD request which triggered the transaction
     316             :  * @param session database session to use
     317             :  * @param[out] mhd_ret set to MHD response status for @a connection,
     318             :  *             if transaction failed (!)
     319             :  * @return transaction status
     320             :  */
     321             : static enum GNUNET_DB_QueryStatus
     322           2 : refresh_melt_transaction (void *cls,
     323             :                           struct MHD_Connection *connection,
     324             :                           struct TALER_EXCHANGEDB_Session *session,
     325             :                           int *mhd_ret)
     326             : {
     327           2 :   struct RefreshMeltContext *rmc = cls;
     328             :   enum GNUNET_DB_QueryStatus qs;
     329             : 
     330           4 :   qs = TEH_plugin->get_refresh_session (TEH_plugin->cls,
     331             :                                         session,
     332           2 :                                         &rmc->session_hash,
     333             :                                         &rmc->refresh_session);
     334           2 :   if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs)
     335             :   {
     336           0 :     *mhd_ret = reply_refresh_melt_success (connection,
     337           0 :                                            &rmc->session_hash,
     338           0 :                                            rmc->refresh_session.noreveal_index);
     339           0 :     return GNUNET_DB_STATUS_HARD_ERROR;
     340             :   }
     341           2 :   if (0 > qs)
     342             :   {
     343           0 :     if hGNUNeT_EB_STATUS_HARD_ERROR == qs)
     344           0 :       *mhd_ret = TEH_RESPONSE_reply_internal_db_error (connection,
     345             :                                                        TALER_EC_REFRESH_MELT_DB_FETCH_ERROR);
     346           0 :     return qs;
     347             :   }
     348             : 
     349             :   /* store 'global' session data */
     350           2 :   rmc->refresh_session.num_newcoins = rmc->num_newcoins;
     351             :   rmc->refresh_session.noreveal_index
     352           2 :     = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
     353             :                                 TALER_CNC_KAPPA);
     354             : 
     355           2 :   qs = refresh_check_melt (connection,
     356             :                            session,
     357             :                            rmc,
     358             :                            mhd_ret);
     359           2 :   if (0 > qs)
     360           1 :     return qs;
     361             : 
     362           1 :   if ( (0 >=
     363           2 :         (qs = TEH_plugin->create_refresh_session (TEH_plugin->cls,
     364             :                                                   session,
     365           1 :                                                   &rmc->session_hash,
     366           2 :                                                   &rmc->refresh_session))) ||
     367             :        (0 >=
     368           3 :         (qs = TEH_plugin->insert_refresh_order (TEH_plugin->cls,
     369             :                                                 session,
     370           1 :                                                 &rmc->session_hash,
     371           1 :                                                 rmc->num_newcoins,
     372           2 :                                                 rmc->denom_pubs))) ||
     373             :        (0 >=
     374           3 :         (qs = TEH_plugin->insert_refresh_commit_coins (TEH_plugin->cls,
     375             :                                                        session,
     376           1 :                                                        &rmc->session_hash,
     377           1 :                                                        rmc->num_newcoins,
     378           2 :                                                        rmc->commit_coin[rmc->refresh_session.noreveal_index]))) ||
     379             :        (0 >=
     380           2 :         (qs = TEH_plugin->insert_refresh_transfer_public_key (TEH_plugin->cls,
     381             :                                                               session,
     382           1 :                                                               &rmc->session_hash,
     383           1 :                                                               &rmc->transfer_pub[rmc->refresh_session.noreveal_index]))) )
     384             :   {
     385           0 :     if (GNUNET_DB_STATUS_SOFT_ERROR != qs)
     386             :     {
     387           0 :       *mhd_ret = TEH_RESPONSE_reply_internal_db_error (connection,
     388             :                                                        TALER_EC_REFRESH_MELT_DB_STORE_SESSION_ERROR);
     389           0 :       return GNUNET_DB_STATUS_HARD_ERROR;
     390             :     }
     391           0 :     return qs;
     392             :   }
     393           1 :   return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
     394             : }
     395             : 
     396             : 
     397             : /**
     398             :  * Handle a "/refresh/melt" request after the main JSON parsing has
     399             :  * happened.  We now need to validate the coins being melted and the
     400             :  * session signature and then hand things of to execute the melt
     401             :  * operation.
     402             :  *
     403             :  * @param connection the MHD connection to handle
     404             :  * @param[out] mhd_ret set on failure to return value for MHD
     405             :  * @param rmc information about the melt to process
     406             :  * @return MHD result code
     407             :  */
     408             : static int
     409           2 : refresh_melt_prepare (struct MHD_Connection *connection,
     410             :                       int *mhd_ret,
     411             :                       struct RefreshMeltContext *rmc)
     412             : {
     413             :   struct TALER_EXCHANGEDB_DenominationKeyIssueInformation *dk;
     414             :   struct TALER_EXCHANGEDB_DenominationKeyInformationP *dki;
     415             :   struct TALER_Amount cost;
     416             :   struct TALER_Amount total_cost;
     417             :   struct TALER_Amount value;
     418             :   struct TALER_Amount fee_withdraw;
     419             :   struct TALER_Amount fee_melt;
     420             :   struct TALER_Amount total_melt;
     421             : 
     422           2 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     423             :               "/refresh/melt request for session %s\n",
     424             :               GNUNET_h2s (&rmc->session_hash));
     425             : 
     426           2 :   GNUNET_assert (GNUNET_OK ==
     427             :                  TALER_amount_get_zero (TEH_exchange_currency_string,
     428             :                                         &total_cost));
     429          36 :   for (unsigned int i=0;i<rmc->num_newcoins;i++)
     430             :   {
     431          34 :     dk = TEH_KS_denomination_key_lookup (rmc->key_state,
     432          34 :                                          &rmc->denom_pubs[i],
     433             :                                          TEH_KS_DKU_WITHDRAW);
     434          34 :     if (NULL == dk)
     435             :     {
     436           0 :       GNUNET_break_op (0);
     437           0 :       *mhd_ret = TEH_RESPONSE_reply_arg_invalid (connection,
     438             :                                                  TALER_EC_REFRESH_MELT_FRESH_DENOMINATION_KEY_NOT_FOUND,
     439             :                                                  "new_denoms");
     440           0 :       return GNUNET_SYSERR;
     441             :     }
     442          34 :     dki = &dk->issue;
     443          34 :     TALER_amount_ntoh (&value,
     444          34 :                        &dki->properties.value);
     445          34 :     TALER_amount_ntoh (&fee_withdraw,
     446          34 :                        &dki->properties.fee_withdraw);
     447          34 :     if ( (GNUNET_OK !=
     448          34 :           TALER_amount_add (&cost,
     449             :                             &value,
     450          34 :                             &fee_withdraw)) ||
     451             :          (GNUNET_OK !=
     452          34 :           TALER_amount_add (&total_cost,
     453             :                             &cost,
     454             :                             &total_cost)) )
     455             :     {
     456           0 :       GNUNET_break_op (0);
     457           0 :       *mhd_ret = TEH_RESPONSE_reply_internal_error (connection,
     458             :                                                     TALER_EC_REFRESH_MELT_COST_CALCULATION_OVERFLOW,
     459             :                                                     "cost calculation failure");
     460           0 :       return GNUNET_SYSERR;
     461             :     }
     462             :   }
     463             : 
     464           2 :   dki = &rmc->dki->issue;
     465           2 :   TALER_amount_ntoh (&fee_melt,
     466           2 :                      &dki->properties.fee_refresh);
     467           2 :   if (GNUNET_OK !=
     468           2 :       TALER_amount_subtract (&total_melt,
     469           2 :                              &rmc->coin_melt_details.melt_amount_with_fee,
     470             :                              &fee_melt))
     471             :   {
     472           0 :     GNUNET_break_op (0);
     473           0 :     *mhd_ret = TEH_RESPONSE_reply_external_error (connection,
     474             :                                                   TALER_EC_REFRESH_MELT_FEES_EXCEED_CONTRIBUTION,
     475             :                                                   "Melt contribution below melting fee");
     476           0 :     return GNUNET_SYSERR;
     477             :   }
     478           2 :   if (0 !=
     479           2 :       TALER_amount_cmp (&total_cost,
     480             :                         &total_melt))
     481             :   {
     482           0 :     GNUNET_break_op (0);
     483             :     /* We require total value of coins being melted and
     484             :        total value of coins being generated to match! */
     485           0 :     *mhd_ret = TEH_RESPONSE_reply_json_pack (connection,
     486             :                                              MHD_HTTP_BAD_REQUEST,
     487             :                                              "{s:s, s:I}",
     488             :                                              "error", "value mismatch",
     489             :                                              "code", (json_int_t) TALER_EC_REFRESH_MELT_FEES_MISSMATCH);
     490           0 :     return GNUNET_SYSERR;
     491             :   }
     492           2 :   return TEH_DB_run_transaction (connection,
     493             :                                  mhd_ret,
     494             :                                  &refresh_melt_transaction,
     495             :                                  rmc);
     496             : }
     497             : 
     498             : 
     499             : /**
     500             :  * Extract public coin information from a JSON object.
     501             :  *
     502             :  * @param connection the connection to send error responses to
     503             :  * @param coin_info the JSON object to extract the coin info from
     504             :  * @param[out] r_melt_detail set to details about the coin's melting permission (if valid)
     505             :  * @return #GNUNET_YES if coin public info in JSON was valid
     506             :  *         #GNUNET_NO JSON was invalid, response was generated
     507             :  *         #GNUNET_SYSERR on internal error
     508             :  */
     509             : static int
     510           2 : get_coin_public_info (struct MHD_Connection *connection,
     511             :                       const json_t *coin_info,
     512             :                       struct TEH_DB_MeltDetails *r_melt_detail)
     513             : {
     514             :   int ret;
     515             :   struct TALER_CoinSpendSignatureP melt_sig;
     516             :   struct TALER_DenominationSignature sig;
     517             :   struct TALER_DenominationPublicKey pk;
     518             :   struct TALER_Amount amount;
     519           2 :   struct GNUNET_JSON_Specification spec[] = {
     520           2 :     GNUNET_JSON_spec_fixed_auto ("coin_pub", &r_melt_detail->coin_info.coin_pub),
     521             :     TALER_JSON_spec_denomination_signature ("denom_sig", &sig),
     522             :     TALER_JSON_spec_denomination_public_key ("denom_pub", &pk),
     523             :     GNUNET_JSON_spec_fixed_auto ("confirm_sig", &melt_sig),
     524             :     TALER_JSON_spec_amount ("value_with_fee", &amount),
     525             :     GNUNET_JSON_spec_end ()
     526             :   };
     527             : 
     528           2 :   ret = TEH_PARSE_json_data (connection,
     529             :                              coin_info,
     530             :                              spec);
     531           2 :   if (GNUNET_OK != ret)
     532             :   {
     533           0 :     GNUNET_break_op (0);
     534           0 :     return ret;
     535             :   }
     536             :   /* check exchange signature on the coin */
     537           2 :   r_melt_detail->coin_info.denom_sig = sig;
     538           2 :   r_melt_detail->coin_info.denom_pub = pk;
     539           2 :   if (GNUNET_OK !=
     540           2 :       TALER_test_coin_valid (&r_melt_detail->coin_info))
     541             :   {
     542           0 :     GNUNET_break_op (0);
     543           0 :     GNUNET_JSON_parse_free (spec);
     544           0 :     r_melt_detail->coin_info.denom_sig.rsa_signature = NULL;
     545           0 :     r_melt_detail->coin_info.denom_pub.rsa_public_key = NULL;
     546             :     return (MHD_YES ==
     547           0 :             TEH_RESPONSE_reply_signature_invalid (connection,
     548             :                                                   TALER_EC_REFRESH_MELT_DENOMINATION_SIGNATURE_INVALID,
     549             :                                                   "denom_sig"))
     550           0 :       ? GNUNET_NO : GNUNET_SYSERR;
     551             :   }
     552           2 :   r_melt_detail->melt_sig = melt_sig;
     553           2 :   r_melt_detail->melt_amount_with_fee = amount;
     554           2 :   return GNUNET_OK;
     555             : }
     556             : 
     557             : 
     558             : /**
     559             :  * Release memory from the @a commit_coin array.
     560             :  *
     561             :  * @param commit_coin array to release
     562             :  * @param kappa size of 1st dimension
     563             :  * @param num_new_coins size of 2nd dimension
     564             :  */
     565             : static void
     566           2 : free_commit_coins (struct TALER_EXCHANGEDB_RefreshCommitCoin **commit_coin,
     567             :                    unsigned int kappa,
     568             :                    unsigned int num_new_coins)
     569             : {
     570           8 :   for (unsigned int i=0;i<kappa;i++)
     571             :   {
     572           6 :     if (NULL == commit_coin[i])
     573           0 :       break;
     574         108 :     for (unsigned int j=0;j<num_new_coins;j++)
     575         102 :       GNUNET_free_non_null (commit_coin[i][j].coin_ev);
     576           6 :     GNUNET_free (commit_coin[i]);
     577           6 :     commit_coin[i] = NULL;
     578             :   }
     579           2 : }
     580             : 
     581             : 
     582             : /**
     583             :  * Cleanup state kept in the @a rmc.
     584             :  *
     585             :  * @param rmc state to clean up; does not free @a rmc itself
     586             :  */
     587             : static void
     588           2 : cleanup_rmc (struct RefreshMeltContext *rmc)
     589             : {
     590           2 :   free_commit_coins (rmc->commit_coin,
     591             :                      TALER_CNC_KAPPA,
     592             :                      rmc->num_newcoins);
     593           2 :   if (NULL != rmc->coin_melt_details.coin_info.denom_pub.rsa_public_key)
     594             :   {
     595           2 :     GNUNET_CRYPTO_rsa_public_key_free (rmc->coin_melt_details.coin_info.denom_pub.rsa_public_key);
     596           2 :     rmc->coin_melt_details.coin_info.denom_pub.rsa_public_key = NULL;
     597             :   }
     598           2 :   if (NULL != rmc->coin_melt_details.coin_info.denom_sig.rsa_signature)
     599             :   {
     600           2 :     GNUNET_CRYPTO_rsa_signature_free (rmc->coin_melt_details.coin_info.denom_sig.rsa_signature);
     601           2 :     rmc->coin_melt_details.coin_info.denom_sig.rsa_signature = NULL;
     602             :   }
     603           2 :   if (NULL != rmc->denom_pubs)
     604             :   {
     605          36 :     for (unsigned int j=0;j<rmc->num_newcoins;j++)
     606          34 :       if (NULL != rmc->denom_pubs[j].rsa_public_key)
     607          34 :         GNUNET_CRYPTO_rsa_public_key_free (rmc->denom_pubs[j].rsa_public_key);
     608           2 :     GNUNET_free (rmc->denom_pubs);
     609           2 :     rmc->denom_pubs = NULL;
     610             :   }
     611           2 :   if (NULL != rmc->hash_context)
     612             :   {
     613           0 :     GNUNET_CRYPTO_hash_context_abort (rmc->hash_context);
     614           0 :     rmc->hash_context = NULL;
     615             :   }
     616           2 :   if (NULL != rmc->key_state)
     617             :   {
     618           2 :     TEH_KS_release (rmc->key_state);
     619           2 :     rmc->key_state = NULL;
     620             :   }
     621           2 : }
     622             : 
     623             : 
     624             : /**
     625             :  * Handle a "/refresh/melt" request after the first parsing has
     626             :  * happened.  We now need to validate the coins being melted and the
     627             :  * session signature and then hand things of to execute the melt
     628             :  * operation.  This function parses the JSON arrays and then passes
     629             :  * processing on to #handle_refresh_melt_binary().
     630             :  *
     631             :  * @param connection the MHD connection to handle
     632             :  * @param new_denoms array of denomination keys
     633             :  * @param melt_coin coin to melt
     634             :  * @param transfer_pubs #TALER_CNC_KAPPA-dimensional array of transfer keys
     635             :  * @param coin_evs #TALER_CNC_KAPPA-dimensional array of envelopes to sign
     636             :  * @return MHD result code
     637             :  */
     638             : static int
     639           2 : handle_refresh_melt_json (struct MHD_Connection *connection,
     640             :                           const json_t *new_denoms,
     641             :                           const json_t *melt_coin,
     642             :                           const json_t *transfer_pubs,
     643             :                           const json_t *coin_evs)
     644             : {
     645             :   int res;
     646             :   int mhd_ret;
     647             :   struct RefreshMeltContext rmc;
     648             : 
     649           2 :   memset (&rmc,
     650             :           0,
     651             :           sizeof (rmc));
     652             :   /* For the signature check, we hash most of the inputs together
     653             :      (except for the signatures on the coins). */
     654           2 :   rmc.hash_context = GNUNET_CRYPTO_hash_context_start ();
     655           8 :   for (unsigned int i = 0; i < TALER_CNC_KAPPA; i++)
     656             :   {
     657           6 :     struct GNUNET_JSON_Specification trans_spec[] = {
     658           6 :       GNUNET_JSON_spec_fixed_auto (NULL, &rmc.transfer_pub[i]),
     659             :       GNUNET_JSON_spec_end ()
     660             :     };
     661             : 
     662           6 :     res = TEH_PARSE_json_array (connection,
     663             :                                 transfer_pubs,
     664             :                                 trans_spec,
     665             :                                 i, -1);
     666           6 :     if (GNUNET_OK != res)
     667             :     {
     668           0 :       GNUNET_break_op (0);
     669           0 :       mhd_ret = (GNUNET_SYSERR == res) ? MHD_NO : MHD_YES;
     670           0 :       cleanup_rmc (&rmc);
     671           0 :       return mhd_ret;
     672             :     }
     673           6 :     GNUNET_CRYPTO_hash_context_read (rmc.hash_context,
     674           6 :                                      &rmc.transfer_pub[i],
     675             :                                      sizeof (struct TALER_TransferPublicKeyP));
     676             :   }
     677             : 
     678           2 :   rmc.num_newcoins = json_array_size (new_denoms);
     679           2 :   rmc.denom_pubs = GNUNET_new_array (rmc.num_newcoins,
     680             :                                      struct TALER_DenominationPublicKey);
     681          36 :   for (unsigned int i=0;i<rmc.num_newcoins;i++)
     682             :   {
     683             :     char *buf;
     684             :     size_t buf_size;
     685          34 :     struct GNUNET_JSON_Specification spec[] = {
     686          34 :       TALER_JSON_spec_denomination_public_key (NULL,
     687          34 :                                                &rmc.denom_pubs[i]),
     688             :       GNUNET_JSON_spec_end ()
     689             :     };
     690             : 
     691          34 :     res = TEH_PARSE_json_array (connection,
     692             :                                 new_denoms,
     693             :                                 spec,
     694             :                                 i,
     695             :                                 -1);
     696          34 :     if (GNUNET_OK != res)
     697             :     {
     698           0 :       mhd_ret = (GNUNET_NO == res) ? MHD_YES : MHD_NO;
     699           0 :       cleanup_rmc (&rmc);
     700           0 :       return mhd_ret;
     701             :     }
     702          34 :     buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rmc.denom_pubs[i].rsa_public_key,
     703             :                                                     &buf);
     704          34 :     GNUNET_CRYPTO_hash_context_read (rmc.hash_context,
     705             :                                      buf,
     706             :                                      buf_size);
     707          34 :     GNUNET_free (buf);
     708             :   }
     709             : 
     710             :   /* decode JSON data on coin to melt and check that this is a
     711             :      valid coin */
     712             :   {
     713             :     struct TALER_AmountNBO melt_amount;
     714             : 
     715           2 :     res = get_coin_public_info (connection,
     716             :                                 melt_coin,
     717             :                                 &rmc.coin_melt_details);
     718           2 :     if (GNUNET_OK != res)
     719             :     {
     720           0 :       GNUNET_break_op (0);
     721           0 :       mhd_ret = (GNUNET_NO == res) ? MHD_YES : MHD_NO;
     722           0 :       cleanup_rmc (&rmc);
     723           0 :       return mhd_ret;
     724             :     }
     725           2 :     TALER_amount_hton (&melt_amount,
     726             :                        &rmc.coin_melt_details.melt_amount_with_fee);
     727           2 :     GNUNET_CRYPTO_hash_context_read (rmc.hash_context,
     728             :                                      &rmc.coin_melt_details.coin_info.coin_pub,
     729             :                                      sizeof (struct TALER_CoinSpendPublicKeyP));
     730           2 :     GNUNET_CRYPTO_hash_context_read (rmc.hash_context,
     731             :                                      &melt_amount,
     732             :                                      sizeof (struct TALER_AmountNBO));
     733             :   }
     734             : 
     735             :   /* parse JSON arrays into binary arrays and hash everything
     736             :      together for the signature check */
     737           8 :   for (unsigned int i = 0; i < TALER_CNC_KAPPA; i++)
     738             :   {
     739           6 :     rmc.commit_coin[i] = GNUNET_new_array (rmc.num_newcoins,
     740             :                                            struct TALER_EXCHANGEDB_RefreshCommitCoin);
     741         108 :     for (unsigned int j = 0; j < rmc.num_newcoins; j++)
     742             :     {
     743         102 :       struct TALER_EXCHANGEDB_RefreshCommitCoin *rcc = &rmc.commit_coin[i][j];
     744         102 :       struct GNUNET_JSON_Specification coin_spec[] = {
     745         102 :         GNUNET_JSON_spec_varsize (NULL,
     746         102 :                                   (void **) &rcc->coin_ev,
     747             :                                   &rcc->coin_ev_size),
     748             :         GNUNET_JSON_spec_end ()
     749             :       };
     750             : 
     751         102 :       res = TEH_PARSE_json_array (connection,
     752             :                                   coin_evs,
     753             :                                   coin_spec,
     754             :                                   i,
     755             :                                   j,
     756             :                                   -1);
     757         102 :       if (GNUNET_OK != res)
     758             :       {
     759           0 :         GNUNET_break_op (0);
     760           0 :         mhd_ret = (GNUNET_SYSERR == res) ? MHD_NO : MHD_YES;
     761           0 :         cleanup_rmc (&rmc);
     762           0 :         return mhd_ret;
     763             :       }
     764             : 
     765         204 :       GNUNET_CRYPTO_hash_context_read (rmc.hash_context,
     766         102 :                                        rcc->coin_ev,
     767             :                                        rcc->coin_ev_size);
     768             :     }
     769             :   }
     770             : 
     771           2 :   GNUNET_CRYPTO_hash_context_finish (rmc.hash_context,
     772             :                                      &rmc.session_hash);
     773           2 :   rmc.hash_context = NULL;
     774             : 
     775           2 :   rmc.key_state = TEH_KS_acquire ();
     776           2 :   if (NULL == rmc.key_state)
     777             :   {
     778           0 :     TALER_LOG_ERROR ("Lacking keys to operate\n");
     779           0 :     return TEH_RESPONSE_reply_internal_error (connection,
     780             :                                               TALER_EC_EXCHANGE_BAD_CONFIGURATION,
     781             :                                               "no keys");
     782             :   }
     783           2 :   rmc.dki = TEH_KS_denomination_key_lookup (rmc.key_state,
     784             :                                             &rmc.coin_melt_details.coin_info.denom_pub,
     785             :                                             TEH_KS_DKU_DEPOSIT);
     786           2 :   if (NULL == rmc.dki)
     787             :   {
     788           0 :     TEH_KS_release (rmc.key_state);
     789           0 :     TALER_LOG_WARNING ("Unknown denomination key in /refresh/melt request\n");
     790           0 :     return TEH_RESPONSE_reply_arg_unknown (connection,
     791             :                                            TALER_EC_REFRESH_MELT_DENOMINATION_KEY_NOT_FOUND,
     792             :                                            "denom_pub");
     793             :   }
     794             : 
     795             :   /* verify signature of coin for melt operation */
     796             :   {
     797             :     struct TALER_RefreshMeltCoinAffirmationPS body;
     798             :     struct TALER_Amount fee_refresh;
     799             : 
     800           2 :     TALER_amount_ntoh (&fee_refresh,
     801           2 :                        &rmc.dki->issue.properties.fee_refresh);
     802           2 :     rmc.coin_melt_details.melt_fee = fee_refresh;
     803           2 :     body.purpose.size = htonl (sizeof (struct TALER_RefreshMeltCoinAffirmationPS));
     804           2 :     body.purpose.purpose = htonl (TALER_SIGNATURE_WALLET_COIN_MELT);
     805           2 :     body.session_hash = rmc.session_hash;
     806           2 :     TALER_amount_hton (&body.amount_with_fee,
     807             :                        &rmc.coin_melt_details.melt_amount_with_fee);
     808           2 :     TALER_amount_hton (&body.melt_fee,
     809             :                        &fee_refresh);
     810           2 :     body.coin_pub = rmc.coin_melt_details.coin_info.coin_pub;
     811           2 :     if (TALER_amount_cmp (&fee_refresh,
     812             :                           &rmc.coin_melt_details.melt_amount_with_fee) > 0)
     813             :     {
     814           0 :       GNUNET_break_op (0);
     815           0 :       cleanup_rmc (&rmc);
     816           0 :       return TEH_RESPONSE_reply_external_error (connection,
     817             :                                                 TALER_EC_REFRESH_MELT_AMOUNT_INSUFFICIENT,
     818             :                                                 "melt amount smaller than melting fee");
     819             :     }
     820             : 
     821           2 :     if (GNUNET_OK !=
     822           2 :         GNUNET_CRYPTO_eddsa_verify (TALER_SIGNATURE_WALLET_COIN_MELT,
     823             :                                     &body.purpose,
     824             :                                     &rmc.coin_melt_details.melt_sig.eddsa_signature,
     825             :                                     &rmc.coin_melt_details.coin_info.coin_pub.eddsa_pub))
     826             :     {
     827           0 :       GNUNET_break_op (0);
     828           0 :       cleanup_rmc (&rmc);
     829           0 :       return TEH_RESPONSE_reply_signature_invalid (connection,
     830             :                                                    TALER_EC_REFRESH_MELT_COIN_SIGNATURE_INVALID,
     831             :                                                    "confirm_sig");
     832             :     }
     833             :   }
     834             : 
     835             :   /* prepare commit */
     836           2 :   if (GNUNET_OK !=
     837           2 :       refresh_melt_prepare (connection,
     838             :                             &mhd_ret,
     839             :                             &rmc))
     840             :   {
     841           1 :     cleanup_rmc (&rmc);
     842           1 :     return mhd_ret;
     843             :   }
     844           1 :   mhd_ret = reply_refresh_melt_success (connection,
     845             :                                         &rmc.session_hash,
     846           1 :                                         rmc.refresh_session.noreveal_index);
     847           1 :   cleanup_rmc (&rmc);
     848           1 :   return mhd_ret;
     849             : }
     850             : 
     851             : 
     852             : /**
     853             :  * Handle a "/refresh/melt" request.  Parses the request into the JSON
     854             :  * components and then hands things of to #handle_refresh_melt_json()
     855             :  * to validate the melted coins, the signature and execute the melt
     856             :  * using TEH_DB_execute_refresh_melt().
     857             :  *
     858             :  * @param rh context of the handler
     859             :  * @param connection the MHD connection to handle
     860             :  * @param[in,out] connection_cls the connection's closure (can be updated)
     861             :  * @param upload_data upload data
     862             :  * @param[in,out] upload_data_size number of bytes (left) in @a upload_data
     863             :  * @return MHD result code
     864             :  */
     865             : int
     866           8 : TEH_REFRESH_handler_refresh_melt (struct TEH_RequestHandler *rh,
     867             :                                   struct MHD_Connection *connection,
     868             :                                   void **connection_cls,
     869             :                                   const char *upload_data,
     870             :                                   size_t *upload_data_size)
     871             : {
     872             :   json_t *root;
     873             :   json_t *new_denoms;
     874             :   json_t *melt_coin;
     875             :   json_t *coin_evs;
     876             :   json_t *transfer_pubs;
     877             :   int res;
     878           8 :   struct GNUNET_JSON_Specification spec[] = {
     879             :     GNUNET_JSON_spec_json ("new_denoms", &new_denoms),
     880             :     GNUNET_JSON_spec_json ("melt_coin", &melt_coin),
     881             :     GNUNET_JSON_spec_json ("coin_evs", &coin_evs),
     882             :     GNUNET_JSON_spec_json ("transfer_pubs", &transfer_pubs),
     883             :     GNUNET_JSON_spec_end ()
     884             :   };
     885             : 
     886           8 :   res = TEH_PARSE_post_json (connection,
     887             :                              connection_cls,
     888             :                              upload_data,
     889             :                              upload_data_size,
     890             :                              &root);
     891           8 :   if (GNUNET_SYSERR == res)
     892           0 :     return MHD_NO;
     893          16 :   if ( (GNUNET_NO == res) ||
     894           8 :        (NULL == root) )
     895           6 :     return MHD_YES;
     896             : 
     897           2 :   res = TEH_PARSE_json_data (connection,
     898             :                              root,
     899             :                              spec);
     900           2 :   json_decref (root);
     901           2 :   if (GNUNET_OK != res)
     902           0 :     return (GNUNET_SYSERR == res) ? MHD_NO : MHD_YES;
     903             : 
     904             :   /* Determine dimensionality of the request (kappa, #old and #new coins) */
     905           2 :   if (TALER_CNC_KAPPA != json_array_size (coin_evs))
     906             :   {
     907           0 :     GNUNET_break_op (0);
     908           0 :     GNUNET_JSON_parse_free (spec);
     909           0 :     return TEH_RESPONSE_reply_arg_invalid (connection,
     910             :                                            TALER_EC_REFRESH_MELT_CNC_COIN_ARRAY_SIZE_INVALID,
     911             :                                            "coin_evs");
     912             :   }
     913           2 :   if (TALER_CNC_KAPPA != json_array_size (transfer_pubs))
     914             :   {
     915           0 :     GNUNET_break_op (0);
     916           0 :     GNUNET_JSON_parse_free (spec);
     917           0 :     return TEH_RESPONSE_reply_arg_invalid (connection,
     918             :                                            TALER_EC_REFRESH_MELT_CNC_TRANSFER_ARRAY_SIZE_INVALID,
     919             :                                            "transfer_pubs");
     920             :   }
     921           2 :   res = handle_refresh_melt_json (connection,
     922             :                                   new_denoms,
     923             :                                   melt_coin,
     924             :                                   transfer_pubs,
     925             :                                   coin_evs);
     926           2 :   GNUNET_JSON_parse_free (spec);
     927           2 :   return res;
     928             : }
     929             : 
     930             : 
     931             : /* end of taler-exchange-httpd_refresh_melt.c */

Generated by: LCOV version 1.13