LCOV - code coverage report
Current view: top level - lib - exchange_api_post-recoup-withdraw.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 0.0 % 129 0
Test Date: 2026-04-14 15:39:31 Functions: 0.0 % 5 0

            Line data    Source code
       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-withdraw.c
      19              :  * @brief Implementation of the /recoup 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 Handle
      36              :  */
      37              : struct TALER_EXCHANGE_PostRecoupWithdrawHandle
      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_PostRecoupWithdrawCallback cb;
      64              : 
      65              :   /**
      66              :    * Closure for @a cb.
      67              :    */
      68              :   TALER_EXCHANGE_POST_RECOUP_WITHDRAW_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 response.  If it is valid, call the callback.
      95              :  *
      96              :  * @param ph recoup 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            0 : process_recoup_response (
     103              :   const struct TALER_EXCHANGE_PostRecoupWithdrawHandle *ph,
     104              :   const json_t *json)
     105              : {
     106            0 :   struct TALER_EXCHANGE_PostRecoupWithdrawResponse rr = {
     107              :     .hr.reply = json,
     108              :     .hr.http_status = MHD_HTTP_OK
     109              :   };
     110              :   struct GNUNET_JSON_Specification spec_withdraw[] = {
     111            0 :     GNUNET_JSON_spec_fixed_auto ("reserve_pub",
     112              :                                  &rr.details.ok.reserve_pub),
     113            0 :     GNUNET_JSON_spec_end ()
     114              :   };
     115              : 
     116            0 :   if (GNUNET_OK !=
     117            0 :       GNUNET_JSON_parse (json,
     118              :                          spec_withdraw,
     119              :                          NULL, NULL))
     120              :   {
     121            0 :     GNUNET_break_op (0);
     122            0 :     return GNUNET_SYSERR;
     123              :   }
     124            0 :   ph->cb (ph->cb_cls,
     125              :           &rr);
     126            0 :   return GNUNET_OK;
     127              : }
     128              : 
     129              : 
     130              : /**
     131              :  * Function called when we're done processing the
     132              :  * HTTP /recoup request.
     133              :  *
     134              :  * @param cls the `struct TALER_EXCHANGE_PostRecoupWithdrawHandle`
     135              :  * @param response_code HTTP response code, 0 on error
     136              :  * @param response parsed JSON result, NULL on error
     137              :  */
     138              : static void
     139            0 : handle_recoup_finished (void *cls,
     140              :                         long response_code,
     141              :                         const void *response)
     142              : {
     143            0 :   struct TALER_EXCHANGE_PostRecoupWithdrawHandle *ph = cls;
     144            0 :   const json_t *j = response;
     145            0 :   struct TALER_EXCHANGE_PostRecoupWithdrawResponse rr = {
     146              :     .hr.reply = j,
     147            0 :     .hr.http_status = (unsigned int) response_code
     148              :   };
     149              : 
     150            0 :   ph->job = NULL;
     151            0 :   switch (response_code)
     152              :   {
     153            0 :   case 0:
     154            0 :     rr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     155            0 :     break;
     156            0 :   case MHD_HTTP_OK:
     157            0 :     if (GNUNET_OK !=
     158            0 :         process_recoup_response (ph,
     159              :                                  j))
     160              :     {
     161            0 :       GNUNET_break_op (0);
     162            0 :       rr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
     163            0 :       rr.hr.http_status = 0;
     164            0 :       break;
     165              :     }
     166            0 :     TALER_EXCHANGE_post_recoup_withdraw_cancel (ph);
     167            0 :     return;
     168            0 :   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            0 :     rr.hr.ec = TALER_JSON_get_error_code (j);
     172            0 :     rr.hr.hint = TALER_JSON_get_error_hint (j);
     173            0 :     break;
     174            0 :   case MHD_HTTP_CONFLICT:
     175              :     {
     176              :       struct TALER_Amount min_key;
     177              : 
     178            0 :       rr.hr.ec = TALER_JSON_get_error_code (j);
     179            0 :       rr.hr.hint = TALER_JSON_get_error_hint (j);
     180            0 :       if (GNUNET_OK !=
     181            0 :           TALER_EXCHANGE_get_min_denomination_ (ph->keys,
     182              :                                                 &min_key))
     183              :       {
     184            0 :         GNUNET_break (0);
     185            0 :         rr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
     186            0 :         rr.hr.http_status = 0;
     187            0 :         break;
     188              :       }
     189            0 :       break;
     190              :     }
     191            0 :   case MHD_HTTP_FORBIDDEN:
     192              :     /* Nothing really to verify, exchange says one of the signatures is
     193              :        invalid; as we checked them, this should never happen, we
     194              :        should pass the JSON reply to the application */
     195            0 :     rr.hr.ec = TALER_JSON_get_error_code (j);
     196            0 :     rr.hr.hint = TALER_JSON_get_error_hint (j);
     197            0 :     break;
     198            0 :   case MHD_HTTP_NOT_FOUND:
     199              :     /* Nothing really to verify, this should never
     200              :        happen, we should pass the JSON reply to the application */
     201            0 :     rr.hr.ec = TALER_JSON_get_error_code (j);
     202            0 :     rr.hr.hint = TALER_JSON_get_error_hint (j);
     203            0 :     break;
     204            0 :   case MHD_HTTP_GONE:
     205              :     /* Kind of normal: the money was already sent to the merchant
     206              :        (it was too late for the refund). */
     207            0 :     rr.hr.ec = TALER_JSON_get_error_code (j);
     208            0 :     rr.hr.hint = TALER_JSON_get_error_hint (j);
     209            0 :     break;
     210            0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     211              :     /* Server had an internal issue; we should retry, but this API
     212              :        leaves this to the application */
     213            0 :     rr.hr.ec = TALER_JSON_get_error_code (j);
     214            0 :     rr.hr.hint = TALER_JSON_get_error_hint (j);
     215            0 :     break;
     216            0 :   default:
     217              :     /* unexpected response code */
     218            0 :     rr.hr.ec = TALER_JSON_get_error_code (j);
     219            0 :     rr.hr.hint = TALER_JSON_get_error_hint (j);
     220            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     221              :                 "Unexpected response code %u/%d for exchange recoup\n",
     222              :                 (unsigned int) response_code,
     223              :                 (int) rr.hr.ec);
     224            0 :     GNUNET_break (0);
     225            0 :     break;
     226              :   }
     227            0 :   ph->cb (ph->cb_cls,
     228              :           &rr);
     229            0 :   TALER_EXCHANGE_post_recoup_withdraw_cancel (ph);
     230              : }
     231              : 
     232              : 
     233              : struct TALER_EXCHANGE_PostRecoupWithdrawHandle *
     234            0 : TALER_EXCHANGE_post_recoup_withdraw_create (
     235              :   struct GNUNET_CURL_Context *ctx,
     236              :   const char *url,
     237              :   struct TALER_EXCHANGE_Keys *keys,
     238              :   const struct TALER_EXCHANGE_DenomPublicKey *pk,
     239              :   const struct TALER_DenominationSignature *denom_sig,
     240              :   const struct TALER_ExchangeBlindingValues *exchange_vals,
     241              :   const struct TALER_PlanchetMasterSecretP *ps,
     242              :   const struct TALER_HashBlindedPlanchetsP *h_planchets)
     243              : {
     244              :   struct TALER_EXCHANGE_PostRecoupWithdrawHandle *ph;
     245              :   struct TALER_DenominationHashP h_denom_pub;
     246              :   struct TALER_CoinSpendPrivateKeyP coin_priv;
     247              :   union GNUNET_CRYPTO_BlindingSecretP bks;
     248              :   struct TALER_CoinSpendSignatureP coin_sig;
     249              : 
     250            0 :   ph = GNUNET_new (struct TALER_EXCHANGE_PostRecoupWithdrawHandle);
     251            0 :   ph->ctx = ctx;
     252            0 :   ph->base_url = GNUNET_strdup (url);
     253            0 :   ph->keys = TALER_EXCHANGE_keys_incref (keys);
     254            0 :   TALER_planchet_setup_coin_priv (ps,
     255              :                                   exchange_vals,
     256              :                                   &coin_priv);
     257            0 :   TALER_planchet_blinding_secret_create (ps,
     258              :                                          exchange_vals,
     259              :                                          &bks);
     260            0 :   GNUNET_CRYPTO_eddsa_key_get_public (&coin_priv.eddsa_priv,
     261              :                                       &ph->coin_pub.eddsa_pub);
     262            0 :   TALER_denom_pub_hash (&pk->key,
     263              :                         &h_denom_pub);
     264            0 :   TALER_wallet_recoup_sign (&h_denom_pub,
     265              :                             &bks,
     266              :                             &coin_priv,
     267              :                             &coin_sig);
     268            0 :   ph->body = GNUNET_JSON_PACK (
     269              :     GNUNET_JSON_pack_data_auto ("denom_pub_hash",
     270              :                                 &h_denom_pub),
     271              :     TALER_JSON_pack_denom_sig ("denom_sig",
     272              :                                denom_sig),
     273              :     TALER_JSON_pack_exchange_blinding_values ("ewv",
     274              :                                               exchange_vals),
     275              :     GNUNET_JSON_pack_data_auto ("coin_sig",
     276              :                                 &coin_sig),
     277              :     GNUNET_JSON_pack_data_auto ("h_planchets",
     278              :                                 h_planchets),
     279              :     GNUNET_JSON_pack_data_auto ("coin_blind_key_secret",
     280              :                                 &bks));
     281            0 :   switch (denom_sig->unblinded_sig->cipher)
     282              :   {
     283            0 :   case GNUNET_CRYPTO_BSA_INVALID:
     284            0 :     json_decref (ph->body);
     285            0 :     GNUNET_free (ph->base_url);
     286            0 :     TALER_EXCHANGE_keys_decref (ph->keys);
     287            0 :     GNUNET_free (ph);
     288            0 :     GNUNET_break (0);
     289            0 :     return NULL;
     290            0 :   case GNUNET_CRYPTO_BSA_RSA:
     291            0 :     break;
     292            0 :   case GNUNET_CRYPTO_BSA_CS:
     293              :     {
     294              :       union GNUNET_CRYPTO_BlindSessionNonce nonce;
     295              : 
     296              :       /* NOTE: this is not elegant, and as per the note in TALER_coin_ev_hash()
     297              :          it is not strictly clear that the nonce is needed. Best case would be
     298              :          to find a way to include it more 'naturally' somehow, for example with
     299              :          the variant union version of bks! */
     300            0 :       TALER_cs_withdraw_nonce_derive (ps,
     301              :                                       &nonce.cs_nonce);
     302            0 :       GNUNET_assert (
     303              :         0 ==
     304              :         json_object_set_new (ph->body,
     305              :                              "nonce",
     306              :                              GNUNET_JSON_from_data_auto (
     307              :                                &nonce)));
     308              :     }
     309            0 :     break;
     310              :   }
     311            0 :   return ph;
     312              : }
     313              : 
     314              : 
     315              : enum TALER_ErrorCode
     316            0 : TALER_EXCHANGE_post_recoup_withdraw_start (
     317              :   struct TALER_EXCHANGE_PostRecoupWithdrawHandle *ph,
     318              :   TALER_EXCHANGE_PostRecoupWithdrawCallback cb,
     319              :   TALER_EXCHANGE_POST_RECOUP_WITHDRAW_RESULT_CLOSURE *cb_cls)
     320              : {
     321              :   CURL *eh;
     322              :   char arg_str[sizeof (struct TALER_CoinSpendPublicKeyP) * 2 + 32];
     323              : 
     324            0 :   ph->cb = cb;
     325            0 :   ph->cb_cls = cb_cls;
     326              :   {
     327              :     char pub_str[sizeof (struct TALER_CoinSpendPublicKeyP) * 2];
     328              :     char *end;
     329              : 
     330            0 :     end = GNUNET_STRINGS_data_to_string (
     331            0 :       &ph->coin_pub,
     332              :       sizeof (struct TALER_CoinSpendPublicKeyP),
     333              :       pub_str,
     334              :       sizeof (pub_str));
     335            0 :     *end = '\0';
     336            0 :     GNUNET_snprintf (arg_str,
     337              :                      sizeof (arg_str),
     338              :                      "coins/%s/recoup",
     339              :                      pub_str);
     340              :   }
     341            0 :   ph->url = TALER_url_join (ph->base_url,
     342              :                             arg_str,
     343              :                             NULL);
     344            0 :   if (NULL == ph->url)
     345              :   {
     346            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     347              :                 "Could not construct request URL.\n");
     348            0 :     return TALER_EC_GENERIC_CONFIGURATION_INVALID;
     349              :   }
     350            0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     351              :               "URL for recoup: `%s'\n",
     352              :               ph->url);
     353            0 :   eh = TALER_EXCHANGE_curl_easy_get_ (ph->url);
     354            0 :   if ( (NULL == eh) ||
     355              :        (GNUNET_OK !=
     356            0 :         TALER_curl_easy_post (&ph->post_ctx,
     357              :                               eh,
     358            0 :                               ph->body)) )
     359              :   {
     360            0 :     GNUNET_break (0);
     361            0 :     if (NULL != eh)
     362            0 :       curl_easy_cleanup (eh);
     363            0 :     return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
     364              :   }
     365            0 :   ph->job = GNUNET_CURL_job_add2 (ph->ctx,
     366              :                                   eh,
     367            0 :                                   ph->post_ctx.headers,
     368              :                                   &handle_recoup_finished,
     369              :                                   ph);
     370            0 :   if (NULL == ph->job)
     371            0 :     return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
     372            0 :   return TALER_EC_NONE;
     373              : }
     374              : 
     375              : 
     376              : void
     377            0 : TALER_EXCHANGE_post_recoup_withdraw_cancel (
     378              :   struct TALER_EXCHANGE_PostRecoupWithdrawHandle *ph)
     379              : {
     380            0 :   if (NULL != ph->job)
     381              :   {
     382            0 :     GNUNET_CURL_job_cancel (ph->job);
     383            0 :     ph->job = NULL;
     384              :   }
     385            0 :   TALER_curl_easy_post_finished (&ph->post_ctx);
     386            0 :   GNUNET_free (ph->url);
     387            0 :   GNUNET_free (ph->base_url);
     388            0 :   json_decref (ph->body);
     389            0 :   TALER_EXCHANGE_keys_decref (ph->keys);
     390            0 :   GNUNET_free (ph);
     391            0 : }
     392              : 
     393              : 
     394              : /* end of exchange_api_post-recoup-withdraw.c */
        

Generated by: LCOV version 2.0-1