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

Generated by: LCOV version 1.13