LCOV - code coverage report
Current view: top level - lib - exchange_api_recoup_refresh.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 0.0 % 118 0
Test Date: 2026-01-04 22:17:00 Functions: 0.0 % 4 0

            Line data    Source code
       1              : /*
       2              :   This file is part of TALER
       3              :   Copyright (C) 2017-2023 Taler Systems SA
       4              : 
       5              :   TALER is free software; you can redistribute it and/or modify it under the
       6              :   terms of the GNU 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 General Public License for more details.
      12              : 
      13              :   You should have received a copy of the GNU General Public License along with
      14              :   TALER; see the file COPYING.  If not, see
      15              :   <http://www.gnu.org/licenses/>
      16              : */
      17              : /**
      18              :  * @file lib/exchange_api_recoup_refresh.c
      19              :  * @brief Implementation of the /recoup-refresh request of the exchange's HTTP API
      20              :  * @author Christian Grothoff
      21              :  */
      22              : #include "taler/platform.h"
      23              : #include <jansson.h>
      24              : #include <microhttpd.h> /* just for HTTP status codes */
      25              : #include <gnunet/gnunet_util_lib.h>
      26              : #include <gnunet/gnunet_json_lib.h>
      27              : #include <gnunet/gnunet_curl_lib.h>
      28              : #include "taler/taler_json_lib.h"
      29              : #include "taler/taler_exchange_service.h"
      30              : #include "exchange_api_common.h"
      31              : #include "exchange_api_handle.h"
      32              : #include "taler/taler_signatures.h"
      33              : #include "exchange_api_curl_defaults.h"
      34              : 
      35              : 
      36              : /**
      37              :  * @brief A Recoup Handle
      38              :  */
      39              : struct TALER_EXCHANGE_RecoupRefreshHandle
      40              : {
      41              : 
      42              :   /**
      43              :    * The keys of the exchange this request handle will use
      44              :    */
      45              :   struct TALER_EXCHANGE_Keys *keys;
      46              : 
      47              :   /**
      48              :    * The url for this request.
      49              :    */
      50              :   char *url;
      51              : 
      52              :   /**
      53              :    * Context for #TEH_curl_easy_post(). Keeps the data that must
      54              :    * persist for Curl to make the upload.
      55              :    */
      56              :   struct TALER_CURL_PostContext ctx;
      57              : 
      58              :   /**
      59              :    * Denomination key of the coin.
      60              :    */
      61              :   struct TALER_EXCHANGE_DenomPublicKey pk;
      62              : 
      63              :   /**
      64              :    * Handle for the request.
      65              :    */
      66              :   struct GNUNET_CURL_Job *job;
      67              : 
      68              :   /**
      69              :    * Function to call with the result.
      70              :    */
      71              :   TALER_EXCHANGE_RecoupRefreshResultCallback cb;
      72              : 
      73              :   /**
      74              :    * Closure for @a cb.
      75              :    */
      76              :   void *cb_cls;
      77              : 
      78              :   /**
      79              :    * Public key of the coin we are trying to get paid back.
      80              :    */
      81              :   struct TALER_CoinSpendPublicKeyP coin_pub;
      82              : 
      83              :   /**
      84              :    * Signature affirming the recoup-refresh operation.
      85              :    */
      86              :   struct TALER_CoinSpendSignatureP coin_sig;
      87              : 
      88              : };
      89              : 
      90              : 
      91              : /**
      92              :  * Parse a recoup-refresh response.  If it is valid, call the callback.
      93              :  *
      94              :  * @param ph recoup handle
      95              :  * @param json json reply with the signature
      96              :  * @return #GNUNET_OK if the signature is valid and we called the callback;
      97              :  *         #GNUNET_SYSERR if not (callback must still be called)
      98              :  */
      99              : static enum GNUNET_GenericReturnValue
     100            0 : process_recoup_response (
     101              :   const struct TALER_EXCHANGE_RecoupRefreshHandle *ph,
     102              :   const json_t *json)
     103              : {
     104            0 :   struct TALER_EXCHANGE_RecoupRefreshResponse rrr = {
     105              :     .hr.reply = json,
     106              :     .hr.http_status = MHD_HTTP_OK
     107              :   };
     108              :   struct GNUNET_JSON_Specification spec_refresh[] = {
     109            0 :     GNUNET_JSON_spec_fixed_auto ("old_coin_pub",
     110              :                                  &rrr.details.ok.old_coin_pub),
     111            0 :     GNUNET_JSON_spec_end ()
     112              :   };
     113              : 
     114            0 :   if (GNUNET_OK !=
     115            0 :       GNUNET_JSON_parse (json,
     116              :                          spec_refresh,
     117              :                          NULL, NULL))
     118              :   {
     119            0 :     GNUNET_break_op (0);
     120            0 :     return GNUNET_SYSERR;
     121              :   }
     122            0 :   ph->cb (ph->cb_cls,
     123              :           &rrr);
     124            0 :   return GNUNET_OK;
     125              : }
     126              : 
     127              : 
     128              : /**
     129              :  * Function called when we're done processing the
     130              :  * HTTP /recoup-refresh request.
     131              :  *
     132              :  * @param cls the `struct TALER_EXCHANGE_RecoupRefreshHandle`
     133              :  * @param response_code HTTP response code, 0 on error
     134              :  * @param response parsed JSON result, NULL on error
     135              :  */
     136              : static void
     137            0 : handle_recoup_refresh_finished (void *cls,
     138              :                                 long response_code,
     139              :                                 const void *response)
     140              : {
     141            0 :   struct TALER_EXCHANGE_RecoupRefreshHandle *ph = cls;
     142            0 :   const json_t *j = response;
     143            0 :   struct TALER_EXCHANGE_RecoupRefreshResponse rrr = {
     144              :     .hr.reply = j,
     145            0 :     .hr.http_status = (unsigned int) response_code
     146              :   };
     147              : 
     148            0 :   ph->job = NULL;
     149            0 :   switch (response_code)
     150              :   {
     151            0 :   case 0:
     152            0 :     rrr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     153            0 :     break;
     154            0 :   case MHD_HTTP_OK:
     155            0 :     if (GNUNET_OK !=
     156            0 :         process_recoup_response (ph,
     157              :                                  j))
     158              :     {
     159            0 :       GNUNET_break_op (0);
     160            0 :       rrr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
     161            0 :       rrr.hr.http_status = 0;
     162            0 :       break;
     163              :     }
     164            0 :     TALER_EXCHANGE_recoup_refresh_cancel (ph);
     165            0 :     return;
     166            0 :   case MHD_HTTP_BAD_REQUEST:
     167              :     /* This should never happen, either us or the exchange is buggy
     168              :        (or API version conflict); just pass JSON reply to the application */
     169            0 :     rrr.hr.ec = TALER_JSON_get_error_code (j);
     170            0 :     rrr.hr.hint = TALER_JSON_get_error_hint (j);
     171            0 :     break;
     172            0 :   case MHD_HTTP_FORBIDDEN:
     173              :     /* Nothing really to verify, exchange says one of the signatures is
     174              :        invalid; as we checked them, this should never happen, we
     175              :        should pass the JSON reply to the application */
     176            0 :     rrr.hr.ec = TALER_JSON_get_error_code (j);
     177            0 :     rrr.hr.hint = TALER_JSON_get_error_hint (j);
     178            0 :     break;
     179            0 :   case MHD_HTTP_NOT_FOUND:
     180              :     /* Nothing really to verify, this should never
     181              :        happen, we should pass the JSON reply to the application */
     182            0 :     rrr.hr.ec = TALER_JSON_get_error_code (j);
     183            0 :     rrr.hr.hint = TALER_JSON_get_error_hint (j);
     184            0 :     break;
     185            0 :   case MHD_HTTP_CONFLICT:
     186            0 :     rrr.hr.ec = TALER_JSON_get_error_code (j);
     187            0 :     rrr.hr.hint = TALER_JSON_get_error_hint (j);
     188            0 :     break;
     189            0 :   case MHD_HTTP_GONE:
     190              :     /* Kind of normal: the money was already sent to the merchant
     191              :        (it was too late for the refund). */
     192            0 :     rrr.hr.ec = TALER_JSON_get_error_code (j);
     193            0 :     rrr.hr.hint = TALER_JSON_get_error_hint (j);
     194            0 :     break;
     195            0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     196              :     /* Server had an internal issue; we should retry, but this API
     197              :        leaves this to the application */
     198            0 :     rrr.hr.ec = TALER_JSON_get_error_code (j);
     199            0 :     rrr.hr.hint = TALER_JSON_get_error_hint (j);
     200            0 :     break;
     201            0 :   default:
     202              :     /* unexpected response code */
     203            0 :     rrr.hr.ec = TALER_JSON_get_error_code (j);
     204            0 :     rrr.hr.hint = TALER_JSON_get_error_hint (j);
     205            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     206              :                 "Unexpected response code %u/%d for exchange recoup\n",
     207              :                 (unsigned int) response_code,
     208              :                 (int) rrr.hr.ec);
     209            0 :     GNUNET_break (0);
     210            0 :     break;
     211              :   }
     212            0 :   ph->cb (ph->cb_cls,
     213              :           &rrr);
     214            0 :   TALER_EXCHANGE_recoup_refresh_cancel (ph);
     215              : }
     216              : 
     217              : 
     218              : #pragma message "TODO[oec]: rewrite TALER_EXCHANGE_RecoupRefreshHandle"
     219              : struct TALER_EXCHANGE_RecoupRefreshHandle *
     220            0 : TALER_EXCHANGE_recoup_refresh (
     221              :   struct GNUNET_CURL_Context *ctx,
     222              :   const char *url,
     223              :   struct TALER_EXCHANGE_Keys *keys,
     224              :   const struct TALER_EXCHANGE_DenomPublicKey *pk,
     225              :   const struct TALER_DenominationSignature *denom_sig,
     226              :   const struct TALER_ExchangeBlindingValues *blinding_values,
     227              :   const struct TALER_PublicRefreshMasterSeedP *rms,
     228              :   const struct TALER_PlanchetMasterSecretP *ps,
     229              :   unsigned int idx,
     230              :   TALER_EXCHANGE_RecoupRefreshResultCallback recoup_cb,
     231              :   void *recoup_cb_cls)
     232              : {
     233              :   struct TALER_EXCHANGE_RecoupRefreshHandle *ph;
     234              :   struct TALER_DenominationHashP h_denom_pub;
     235              :   json_t *recoup_obj;
     236              :   CURL *eh;
     237              :   char arg_str[sizeof (struct TALER_CoinSpendPublicKeyP) * 2 + 32];
     238              :   struct TALER_CoinSpendPrivateKeyP coin_priv;
     239              :   union GNUNET_CRYPTO_BlindingSecretP bks;
     240              : 
     241            0 :   GNUNET_assert (NULL != recoup_cb);
     242            0 :   ph = GNUNET_new (struct TALER_EXCHANGE_RecoupRefreshHandle);
     243            0 :   ph->pk = *pk;
     244            0 :   memset (&ph->pk.key,
     245              :           0,
     246              :           sizeof (ph->pk.key)); /* zero out, as lifetime cannot be warranted */
     247            0 :   ph->cb = recoup_cb;
     248            0 :   ph->cb_cls = recoup_cb_cls;
     249            0 :   TALER_planchet_setup_coin_priv (ps,
     250              :                                   blinding_values,
     251              :                                   &coin_priv);
     252            0 :   TALER_planchet_blinding_secret_create (ps,
     253              :                                          blinding_values,
     254              :                                          &bks);
     255            0 :   GNUNET_CRYPTO_eddsa_key_get_public (&coin_priv.eddsa_priv,
     256              :                                       &ph->coin_pub.eddsa_pub);
     257            0 :   TALER_denom_pub_hash (&pk->key,
     258              :                         &h_denom_pub);
     259            0 :   TALER_wallet_recoup_refresh_sign (&h_denom_pub,
     260              :                                     &bks,
     261              :                                     &coin_priv,
     262              :                                     &ph->coin_sig);
     263            0 :   recoup_obj = GNUNET_JSON_PACK (
     264              :     GNUNET_JSON_pack_data_auto ("denom_pub_hash",
     265              :                                 &h_denom_pub),
     266              :     TALER_JSON_pack_denom_sig ("denom_sig",
     267              :                                denom_sig),
     268              :     TALER_JSON_pack_exchange_blinding_values ("ewv",
     269              :                                               blinding_values),
     270              :     GNUNET_JSON_pack_data_auto ("coin_sig",
     271              :                                 &ph->coin_sig),
     272              :     GNUNET_JSON_pack_data_auto ("coin_blind_key_secret",
     273              :                                 &bks));
     274              : 
     275            0 :   switch (denom_sig->unblinded_sig->cipher)
     276              :   {
     277            0 :   case GNUNET_CRYPTO_BSA_INVALID:
     278            0 :     json_decref (recoup_obj);
     279            0 :     GNUNET_break (0);
     280            0 :     GNUNET_free (ph);
     281            0 :     return NULL;
     282            0 :   case GNUNET_CRYPTO_BSA_RSA:
     283            0 :     break;
     284            0 :   case GNUNET_CRYPTO_BSA_CS:
     285              :     {
     286              : 
     287              :     }
     288            0 :     break;
     289              :   }
     290              : 
     291              :   {
     292              :     char pub_str[sizeof (struct TALER_CoinSpendPublicKeyP) * 2];
     293              :     char *end;
     294              : 
     295            0 :     end = GNUNET_STRINGS_data_to_string (
     296            0 :       &ph->coin_pub,
     297              :       sizeof (struct TALER_CoinSpendPublicKeyP),
     298              :       pub_str,
     299              :       sizeof (pub_str));
     300            0 :     *end = '\0';
     301            0 :     GNUNET_snprintf (arg_str,
     302              :                      sizeof (arg_str),
     303              :                      "coins/%s/recoup-refresh",
     304              :                      pub_str);
     305              :   }
     306              : 
     307            0 :   ph->url = TALER_url_join (url,
     308              :                             arg_str,
     309              :                             NULL);
     310            0 :   if (NULL == ph->url)
     311              :   {
     312            0 :     json_decref (recoup_obj);
     313            0 :     GNUNET_free (ph);
     314            0 :     return NULL;
     315              :   }
     316            0 :   eh = TALER_EXCHANGE_curl_easy_get_ (ph->url);
     317            0 :   if ( (NULL == eh) ||
     318              :        (GNUNET_OK !=
     319            0 :         TALER_curl_easy_post (&ph->ctx,
     320              :                               eh,
     321              :                               recoup_obj)) )
     322              :   {
     323            0 :     GNUNET_break (0);
     324            0 :     if (NULL != eh)
     325            0 :       curl_easy_cleanup (eh);
     326            0 :     json_decref (recoup_obj);
     327            0 :     GNUNET_free (ph->url);
     328            0 :     GNUNET_free (ph);
     329            0 :     return NULL;
     330              :   }
     331            0 :   json_decref (recoup_obj);
     332            0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     333              :               "URL for recoup-refresh: `%s'\n",
     334              :               ph->url);
     335            0 :   ph->keys = TALER_EXCHANGE_keys_incref (keys);
     336            0 :   ph->job = GNUNET_CURL_job_add2 (ctx,
     337              :                                   eh,
     338            0 :                                   ph->ctx.headers,
     339              :                                   &handle_recoup_refresh_finished,
     340              :                                   ph);
     341            0 :   return ph;
     342              : }
     343              : 
     344              : 
     345              : void
     346            0 : TALER_EXCHANGE_recoup_refresh_cancel (
     347              :   struct TALER_EXCHANGE_RecoupRefreshHandle *ph)
     348              : {
     349            0 :   if (NULL != ph->job)
     350              :   {
     351            0 :     GNUNET_CURL_job_cancel (ph->job);
     352            0 :     ph->job = NULL;
     353              :   }
     354            0 :   GNUNET_free (ph->url);
     355            0 :   TALER_curl_easy_post_finished (&ph->ctx);
     356            0 :   TALER_EXCHANGE_keys_decref (ph->keys);
     357            0 :   GNUNET_free (ph);
     358            0 : }
     359              : 
     360              : 
     361              : /* end of exchange_api_recoup_refresh.c */
        

Generated by: LCOV version 2.0-1