exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

exchange_api_post-recoup-refresh.c (11223B)


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