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-purses-PURSE_PUB-merge.c (14499B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2022-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_post-purses-PURSE_PUB-merge.c
     19  * @brief Implementation of the client to merge a purse
     20  *        into an account
     21  * @author Christian Grothoff
     22  */
     23 #include "taler/platform.h"
     24 #include <jansson.h>
     25 #include <microhttpd.h> /* just for HTTP status codes */
     26 #include <gnunet/gnunet_util_lib.h>
     27 #include <gnunet/gnunet_json_lib.h>
     28 #include <gnunet/gnunet_curl_lib.h>
     29 #include "taler/taler_json_lib.h"
     30 #include "taler/taler_exchange_service.h"
     31 #include "exchange_api_handle.h"
     32 #include "exchange_api_common.h"
     33 #include "taler/taler_signatures.h"
     34 #include "exchange_api_curl_defaults.h"
     35 
     36 
     37 /**
     38  * @brief A purse merge with deposit handle
     39  */
     40 struct TALER_EXCHANGE_PostPursesMergeHandle
     41 {
     42 
     43   /**
     44    * The curl context for this request.
     45    */
     46   struct GNUNET_CURL_Context *ctx;
     47 
     48   /**
     49    * The base URL of the exchange.
     50    */
     51   char *base_url;
     52 
     53   /**
     54    * The keys of the exchange this request handle will use
     55    */
     56   struct TALER_EXCHANGE_Keys *keys;
     57 
     58   /**
     59    * The url for this request, set during _start.
     60    */
     61   char *url;
     62 
     63   /**
     64    * Context for #TEH_curl_easy_post(). Keeps the data that must
     65    * persist for Curl to make the upload.
     66    */
     67   struct TALER_CURL_PostContext post_ctx;
     68 
     69   /**
     70    * Handle for the request.
     71    */
     72   struct GNUNET_CURL_Job *job;
     73 
     74   /**
     75    * Function to call with the result.
     76    */
     77   TALER_EXCHANGE_PostPursesMergeCallback cb;
     78 
     79   /**
     80    * Closure for @a cb.
     81    */
     82   TALER_EXCHANGE_POST_PURSES_MERGE_RESULT_CLOSURE *cb_cls;
     83 
     84   /**
     85    * Base URL of the provider hosting the @e reserve_pub.
     86    */
     87   char *provider_url;
     88 
     89   /**
     90    * Signature for our operation.
     91    */
     92   struct TALER_PurseMergeSignatureP merge_sig;
     93 
     94   /**
     95    * Expected value in the purse after fees.
     96    */
     97   struct TALER_Amount purse_value_after_fees;
     98 
     99   /**
    100    * Public key of the reserve public key.
    101    */
    102   struct TALER_ReservePublicKeyP reserve_pub;
    103 
    104   /**
    105    * Public key of the purse.
    106    */
    107   struct TALER_PurseContractPublicKeyP purse_pub;
    108 
    109   /**
    110    * Hash over the purse's contract terms.
    111    */
    112   struct TALER_PrivateContractHashP h_contract_terms;
    113 
    114   /**
    115    * When does the purse expire.
    116    */
    117   struct GNUNET_TIME_Timestamp purse_expiration;
    118 
    119   /**
    120    * Time of the merge.
    121    */
    122   struct GNUNET_TIME_Timestamp merge_timestamp;
    123 
    124   /**
    125    * Our merge key.
    126    */
    127   struct TALER_PurseMergePrivateKeyP merge_priv;
    128 
    129   /**
    130    * Reserve signature affirming the merge.
    131    */
    132   struct TALER_ReserveSignatureP reserve_sig;
    133 
    134   /**
    135    * The JSON body to post, built during _create and posted during _start.
    136    */
    137   json_t *body;
    138 
    139 };
    140 
    141 
    142 /**
    143  * Function called when we're done processing the
    144  * HTTP /purse/$PID/merge request.
    145  *
    146  * @param cls the `struct TALER_EXCHANGE_PostPursesMergeHandle`
    147  * @param response_code HTTP response code, 0 on error
    148  * @param response parsed JSON result, NULL on error
    149  */
    150 static void
    151 handle_purse_merge_finished (void *cls,
    152                              long response_code,
    153                              const void *response)
    154 {
    155   struct TALER_EXCHANGE_PostPursesMergeHandle *pch = cls;
    156   const json_t *j = response;
    157   struct TALER_EXCHANGE_PostPursesMergeResponse dr = {
    158     .hr.reply = j,
    159     .hr.http_status = (unsigned int) response_code,
    160     .reserve_sig = &pch->reserve_sig
    161   };
    162 
    163   pch->job = NULL;
    164   switch (response_code)
    165   {
    166   case 0:
    167     dr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
    168     break;
    169   case MHD_HTTP_OK:
    170     {
    171       struct GNUNET_JSON_Specification spec[] = {
    172         GNUNET_JSON_spec_fixed_auto ("exchange_sig",
    173                                      &dr.details.ok.exchange_sig),
    174         GNUNET_JSON_spec_fixed_auto ("exchange_pub",
    175                                      &dr.details.ok.exchange_pub),
    176         GNUNET_JSON_spec_timestamp ("exchange_timestamp",
    177                                     &dr.details.ok.etime),
    178         TALER_JSON_spec_amount ("merge_amount",
    179                                 pch->purse_value_after_fees.currency,
    180                                 &dr.details.ok.merge_amount),
    181         GNUNET_JSON_spec_end ()
    182       };
    183 
    184       if (GNUNET_OK !=
    185           GNUNET_JSON_parse (j,
    186                              spec,
    187                              NULL, NULL))
    188       {
    189         GNUNET_break_op (0);
    190         dr.hr.http_status = 0;
    191         dr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
    192         break;
    193       }
    194       if (GNUNET_OK !=
    195           TALER_EXCHANGE_test_signing_key (pch->keys,
    196                                            &dr.details.ok.exchange_pub))
    197       {
    198         GNUNET_break_op (0);
    199         dr.hr.http_status = 0;
    200         dr.hr.ec = TALER_EC_EXCHANGE_PURSE_MERGE_EXCHANGE_SIGNATURE_INVALID;
    201         break;
    202       }
    203       if (GNUNET_OK !=
    204           TALER_exchange_online_purse_merged_verify (
    205             dr.details.ok.etime,
    206             pch->purse_expiration,
    207             &pch->purse_value_after_fees,
    208             &pch->purse_pub,
    209             &pch->h_contract_terms,
    210             &pch->reserve_pub,
    211             pch->provider_url,
    212             &dr.details.ok.exchange_pub,
    213             &dr.details.ok.exchange_sig))
    214       {
    215         GNUNET_break_op (0);
    216         dr.hr.http_status = 0;
    217         dr.hr.ec = TALER_EC_EXCHANGE_PURSE_MERGE_EXCHANGE_SIGNATURE_INVALID;
    218         break;
    219       }
    220     }
    221     break;
    222   case MHD_HTTP_BAD_REQUEST:
    223     /* This should never happen, either us or the exchange is buggy
    224        (or API version conflict); just pass JSON reply to the application */
    225     dr.hr.ec = TALER_JSON_get_error_code (j);
    226     dr.hr.hint = TALER_JSON_get_error_hint (j);
    227     break;
    228   case MHD_HTTP_PAYMENT_REQUIRED:
    229     /* purse was not (yet) full */
    230     dr.hr.ec = TALER_JSON_get_error_code (j);
    231     dr.hr.hint = TALER_JSON_get_error_hint (j);
    232     break;
    233   case MHD_HTTP_FORBIDDEN:
    234     dr.hr.ec = TALER_JSON_get_error_code (j);
    235     dr.hr.hint = TALER_JSON_get_error_hint (j);
    236     /* Nothing really to verify, exchange says one of the signatures is
    237        invalid; as we checked them, this should never happen, we
    238        should pass the JSON reply to the application */
    239     break;
    240   case MHD_HTTP_NOT_FOUND:
    241     dr.hr.ec = TALER_JSON_get_error_code (j);
    242     dr.hr.hint = TALER_JSON_get_error_hint (j);
    243     /* Nothing really to verify, this should never
    244        happen, we should pass the JSON reply to the application */
    245     break;
    246   case MHD_HTTP_CONFLICT:
    247     {
    248       struct TALER_PurseMergePublicKeyP merge_pub;
    249 
    250       GNUNET_CRYPTO_eddsa_key_get_public (&pch->merge_priv.eddsa_priv,
    251                                           &merge_pub.eddsa_pub);
    252       if (GNUNET_OK !=
    253           TALER_EXCHANGE_check_purse_merge_conflict_ (
    254             &pch->merge_sig,
    255             &merge_pub,
    256             &pch->purse_pub,
    257             pch->provider_url,
    258             j))
    259       {
    260         GNUNET_break_op (0);
    261         dr.hr.http_status = 0;
    262         dr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
    263         break;
    264       }
    265       break;
    266     }
    267     break;
    268   case MHD_HTTP_GONE:
    269     /* could happen if denomination was revoked */
    270     /* Note: one might want to check /keys for revocation
    271        signature here, alas tricky in case our /keys
    272        is outdated => left to clients */
    273     dr.hr.ec = TALER_JSON_get_error_code (j);
    274     dr.hr.hint = TALER_JSON_get_error_hint (j);
    275     break;
    276   case MHD_HTTP_UNAVAILABLE_FOR_LEGAL_REASONS:
    277     dr.hr.ec = TALER_JSON_get_error_code (j);
    278     dr.hr.hint = TALER_JSON_get_error_hint (j);
    279     if (GNUNET_OK !=
    280         TALER_EXCHANGE_parse_451 (&dr.details.unavailable_for_legal_reasons,
    281                                   j))
    282     {
    283       GNUNET_break_op (0);
    284       dr.hr.http_status = 0;
    285       dr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
    286       break;
    287     }
    288     break;
    289   case MHD_HTTP_INTERNAL_SERVER_ERROR:
    290     dr.hr.ec = TALER_JSON_get_error_code (j);
    291     dr.hr.hint = TALER_JSON_get_error_hint (j);
    292     /* Server had an internal issue; we should retry, but this API
    293        leaves this to the application */
    294     break;
    295   default:
    296     /* unexpected response code */
    297     dr.hr.ec = TALER_JSON_get_error_code (j);
    298     dr.hr.hint = TALER_JSON_get_error_hint (j);
    299     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    300                 "Unexpected response code %u/%d for exchange deposit\n",
    301                 (unsigned int) response_code,
    302                 dr.hr.ec);
    303     GNUNET_break_op (0);
    304     break;
    305   }
    306   if (NULL != pch->cb)
    307   {
    308     pch->cb (pch->cb_cls,
    309              &dr);
    310     pch->cb = NULL;
    311   }
    312   TALER_EXCHANGE_post_purses_merge_cancel (pch);
    313 }
    314 
    315 
    316 struct TALER_EXCHANGE_PostPursesMergeHandle *
    317 TALER_EXCHANGE_post_purses_merge_create (
    318   struct GNUNET_CURL_Context *ctx,
    319   const char *url,
    320   struct TALER_EXCHANGE_Keys *keys,
    321   const char *reserve_exchange_url,
    322   const struct TALER_ReservePrivateKeyP *reserve_priv,
    323   const struct TALER_PurseContractPublicKeyP *purse_pub,
    324   const struct TALER_PurseMergePrivateKeyP *merge_priv,
    325   const struct TALER_PrivateContractHashP *h_contract_terms,
    326   uint8_t min_age,
    327   const struct TALER_Amount *purse_value_after_fees,
    328   struct GNUNET_TIME_Timestamp purse_expiration,
    329   struct GNUNET_TIME_Timestamp merge_timestamp)
    330 {
    331   struct TALER_EXCHANGE_PostPursesMergeHandle *pch;
    332 
    333   pch = GNUNET_new (struct TALER_EXCHANGE_PostPursesMergeHandle);
    334   pch->ctx = ctx;
    335   pch->base_url = GNUNET_strdup (url);
    336   pch->merge_priv = *merge_priv;
    337   pch->purse_pub = *purse_pub;
    338   pch->h_contract_terms = *h_contract_terms;
    339   pch->purse_expiration = purse_expiration;
    340   pch->purse_value_after_fees = *purse_value_after_fees;
    341   pch->merge_timestamp = merge_timestamp;
    342   if (NULL == reserve_exchange_url)
    343     pch->provider_url = GNUNET_strdup (url);
    344   else
    345     pch->provider_url = GNUNET_strdup (reserve_exchange_url);
    346   GNUNET_CRYPTO_eddsa_key_get_public (&reserve_priv->eddsa_priv,
    347                                       &pch->reserve_pub.eddsa_pub);
    348 
    349   {
    350     struct TALER_Amount zero_purse_fee;
    351 
    352     GNUNET_assert (GNUNET_OK ==
    353                    TALER_amount_set_zero (purse_value_after_fees->currency,
    354                                           &zero_purse_fee));
    355     TALER_wallet_account_merge_sign (pch->merge_timestamp,
    356                                      &pch->purse_pub,
    357                                      purse_expiration,
    358                                      h_contract_terms,
    359                                      purse_value_after_fees,
    360                                      &zero_purse_fee,
    361                                      min_age,
    362                                      TALER_WAMF_MODE_MERGE_FULLY_PAID_PURSE,
    363                                      reserve_priv,
    364                                      &pch->reserve_sig);
    365   }
    366   pch->keys = TALER_EXCHANGE_keys_incref (keys);
    367   return pch;
    368 }
    369 
    370 
    371 enum TALER_ErrorCode
    372 TALER_EXCHANGE_post_purses_merge_start (
    373   struct TALER_EXCHANGE_PostPursesMergeHandle *pch,
    374   TALER_EXCHANGE_PostPursesMergeCallback cb,
    375   TALER_EXCHANGE_POST_PURSES_MERGE_RESULT_CLOSURE *cb_cls)
    376 {
    377   char arg_str[sizeof (pch->purse_pub) * 2 + 32];
    378   CURL *eh;
    379 
    380   pch->cb = cb;
    381   pch->cb_cls = cb_cls;
    382 
    383   {
    384     char pub_str[sizeof (pch->purse_pub) * 2];
    385     char *end;
    386 
    387     end = GNUNET_STRINGS_data_to_string (
    388       &pch->purse_pub,
    389       sizeof (pch->purse_pub),
    390       pub_str,
    391       sizeof (pub_str));
    392     *end = '\0';
    393     GNUNET_snprintf (arg_str,
    394                      sizeof (arg_str),
    395                      "purses/%s/merge",
    396                      pub_str);
    397   }
    398   pch->url = TALER_url_join (pch->base_url,
    399                              arg_str,
    400                              NULL);
    401   if (NULL == pch->url)
    402   {
    403     GNUNET_break (0);
    404     return TALER_EC_GENERIC_CONFIGURATION_INVALID;
    405   }
    406   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
    407               "URL for purse merge: `%s'\n",
    408               pch->url);
    409   {
    410     struct TALER_NormalizedPayto reserve_url;
    411 
    412     reserve_url = TALER_reserve_make_payto (pch->provider_url,
    413                                             &pch->reserve_pub);
    414     if (NULL == reserve_url.normalized_payto)
    415     {
    416       GNUNET_break (0);
    417       return TALER_EC_GENERIC_ALLOCATION_FAILURE;
    418     }
    419     TALER_wallet_purse_merge_sign (reserve_url,
    420                                    pch->merge_timestamp,
    421                                    &pch->purse_pub,
    422                                    &pch->merge_priv,
    423                                    &pch->merge_sig);
    424     pch->body = GNUNET_JSON_PACK (
    425       TALER_JSON_pack_normalized_payto ("payto_uri",
    426                                         reserve_url),
    427       GNUNET_JSON_pack_data_auto ("merge_sig",
    428                                   &pch->merge_sig),
    429       GNUNET_JSON_pack_data_auto ("reserve_sig",
    430                                   &pch->reserve_sig),
    431       GNUNET_JSON_pack_timestamp ("merge_timestamp",
    432                                   pch->merge_timestamp));
    433     GNUNET_free (reserve_url.normalized_payto);
    434   }
    435 
    436   eh = TALER_EXCHANGE_curl_easy_get_ (pch->url);
    437   if ( (NULL == eh) ||
    438        (GNUNET_OK !=
    439         TALER_curl_easy_post (&pch->post_ctx,
    440                               eh,
    441                               pch->body)) )
    442   {
    443     GNUNET_break (0);
    444     if (NULL != eh)
    445       curl_easy_cleanup (eh);
    446     return TALER_EC_GENERIC_CURL_ALLOCATION_FAILURE;
    447   }
    448   pch->job = GNUNET_CURL_job_add2 (pch->ctx,
    449                                    eh,
    450                                    pch->post_ctx.headers,
    451                                    &handle_purse_merge_finished,
    452                                    pch);
    453   if (NULL == pch->job)
    454     return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
    455   return TALER_EC_NONE;
    456 }
    457 
    458 
    459 void
    460 TALER_EXCHANGE_post_purses_merge_cancel (
    461   struct TALER_EXCHANGE_PostPursesMergeHandle *pch)
    462 {
    463   if (NULL != pch->job)
    464   {
    465     GNUNET_CURL_job_cancel (pch->job);
    466     pch->job = NULL;
    467   }
    468   GNUNET_free (pch->url);
    469   GNUNET_free (pch->base_url);
    470   GNUNET_free (pch->provider_url);
    471   TALER_curl_easy_post_finished (&pch->post_ctx);
    472   TALER_EXCHANGE_keys_decref (pch->keys);
    473   json_decref (pch->body);
    474   GNUNET_free (pch);
    475 }
    476 
    477 
    478 /* end of exchange_api_post-purses-PURSE_PUB-merge.c */