LCOV - code coverage report
Current view: top level - lib - exchange_api_post-purses-PURSE_PUB-merge.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 57.1 % 170 97
Test Date: 2026-04-04 21:36:01 Functions: 100.0 % 4 4

            Line data    Source code
       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              :    * Our merge key.
     121              :    */
     122              :   struct TALER_PurseMergePrivateKeyP merge_priv;
     123              : 
     124              :   /**
     125              :    * Reserve signature affirming the merge.
     126              :    */
     127              :   struct TALER_ReserveSignatureP reserve_sig;
     128              : 
     129              :   /**
     130              :    * The JSON body to post, built during _create and posted during _start.
     131              :    */
     132              :   json_t *body;
     133              : 
     134              : };
     135              : 
     136              : 
     137              : /**
     138              :  * Function called when we're done processing the
     139              :  * HTTP /purse/$PID/merge request.
     140              :  *
     141              :  * @param cls the `struct TALER_EXCHANGE_PostPursesMergeHandle`
     142              :  * @param response_code HTTP response code, 0 on error
     143              :  * @param response parsed JSON result, NULL on error
     144              :  */
     145              : static void
     146            6 : handle_purse_merge_finished (void *cls,
     147              :                              long response_code,
     148              :                              const void *response)
     149              : {
     150            6 :   struct TALER_EXCHANGE_PostPursesMergeHandle *pch = cls;
     151            6 :   const json_t *j = response;
     152            6 :   struct TALER_EXCHANGE_PostPursesMergeResponse dr = {
     153              :     .hr.reply = j,
     154            6 :     .hr.http_status = (unsigned int) response_code,
     155            6 :     .reserve_sig = &pch->reserve_sig
     156              :   };
     157              : 
     158            6 :   pch->job = NULL;
     159            6 :   switch (response_code)
     160              :   {
     161            0 :   case 0:
     162            0 :     dr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     163            0 :     break;
     164            3 :   case MHD_HTTP_OK:
     165              :     {
     166              :       struct GNUNET_JSON_Specification spec[] = {
     167            3 :         GNUNET_JSON_spec_fixed_auto ("exchange_sig",
     168              :                                      &dr.details.ok.exchange_sig),
     169            3 :         GNUNET_JSON_spec_fixed_auto ("exchange_pub",
     170              :                                      &dr.details.ok.exchange_pub),
     171            3 :         GNUNET_JSON_spec_timestamp ("exchange_timestamp",
     172              :                                     &dr.details.ok.etime),
     173            3 :         TALER_JSON_spec_amount ("merge_amount",
     174            3 :                                 pch->purse_value_after_fees.currency,
     175              :                                 &dr.details.ok.merge_amount),
     176            3 :         GNUNET_JSON_spec_end ()
     177              :       };
     178              : 
     179            3 :       if (GNUNET_OK !=
     180            3 :           GNUNET_JSON_parse (j,
     181              :                              spec,
     182              :                              NULL, NULL))
     183              :       {
     184            0 :         GNUNET_break_op (0);
     185            0 :         dr.hr.http_status = 0;
     186            0 :         dr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
     187            0 :         break;
     188              :       }
     189            3 :       if (GNUNET_OK !=
     190            3 :           TALER_EXCHANGE_test_signing_key (pch->keys,
     191              :                                            &dr.details.ok.exchange_pub))
     192              :       {
     193            0 :         GNUNET_break_op (0);
     194            0 :         dr.hr.http_status = 0;
     195            0 :         dr.hr.ec = TALER_EC_EXCHANGE_PURSE_MERGE_EXCHANGE_SIGNATURE_INVALID;
     196            0 :         break;
     197              :       }
     198            3 :       if (GNUNET_OK !=
     199            3 :           TALER_exchange_online_purse_merged_verify (
     200              :             dr.details.ok.etime,
     201              :             pch->purse_expiration,
     202            3 :             &pch->purse_value_after_fees,
     203            3 :             &pch->purse_pub,
     204            3 :             &pch->h_contract_terms,
     205            3 :             &pch->reserve_pub,
     206            3 :             pch->provider_url,
     207              :             &dr.details.ok.exchange_pub,
     208              :             &dr.details.ok.exchange_sig))
     209              :       {
     210            0 :         GNUNET_break_op (0);
     211            0 :         dr.hr.http_status = 0;
     212            0 :         dr.hr.ec = TALER_EC_EXCHANGE_PURSE_MERGE_EXCHANGE_SIGNATURE_INVALID;
     213            0 :         break;
     214              :       }
     215              :     }
     216            3 :     break;
     217            0 :   case MHD_HTTP_BAD_REQUEST:
     218              :     /* This should never happen, either us or the exchange is buggy
     219              :        (or API version conflict); just pass JSON reply to the application */
     220            0 :     dr.hr.ec = TALER_JSON_get_error_code (j);
     221            0 :     dr.hr.hint = TALER_JSON_get_error_hint (j);
     222            0 :     break;
     223            0 :   case MHD_HTTP_PAYMENT_REQUIRED:
     224              :     /* purse was not (yet) full */
     225            0 :     dr.hr.ec = TALER_JSON_get_error_code (j);
     226            0 :     dr.hr.hint = TALER_JSON_get_error_hint (j);
     227            0 :     break;
     228            0 :   case MHD_HTTP_FORBIDDEN:
     229            0 :     dr.hr.ec = TALER_JSON_get_error_code (j);
     230            0 :     dr.hr.hint = TALER_JSON_get_error_hint (j);
     231              :     /* Nothing really to verify, exchange says one of the signatures is
     232              :        invalid; as we checked them, this should never happen, we
     233              :        should pass the JSON reply to the application */
     234            0 :     break;
     235            0 :   case MHD_HTTP_NOT_FOUND:
     236            0 :     dr.hr.ec = TALER_JSON_get_error_code (j);
     237            0 :     dr.hr.hint = TALER_JSON_get_error_hint (j);
     238              :     /* Nothing really to verify, this should never
     239              :        happen, we should pass the JSON reply to the application */
     240            0 :     break;
     241            2 :   case MHD_HTTP_CONFLICT:
     242              :     {
     243              :       struct TALER_PurseMergePublicKeyP merge_pub;
     244              : 
     245            2 :       GNUNET_CRYPTO_eddsa_key_get_public (&pch->merge_priv.eddsa_priv,
     246              :                                           &merge_pub.eddsa_pub);
     247            2 :       if (GNUNET_OK !=
     248            2 :           TALER_EXCHANGE_check_purse_merge_conflict_ (
     249            2 :             &pch->merge_sig,
     250              :             &merge_pub,
     251            2 :             &pch->purse_pub,
     252            2 :             pch->provider_url,
     253              :             j))
     254              :       {
     255            0 :         GNUNET_break_op (0);
     256            0 :         dr.hr.http_status = 0;
     257            0 :         dr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
     258            0 :         break;
     259              :       }
     260            2 :       break;
     261              :     }
     262              :     break;
     263            0 :   case MHD_HTTP_GONE:
     264              :     /* could happen if denomination was revoked */
     265              :     /* Note: one might want to check /keys for revocation
     266              :        signature here, alas tricky in case our /keys
     267              :        is outdated => left to clients */
     268            0 :     dr.hr.ec = TALER_JSON_get_error_code (j);
     269            0 :     dr.hr.hint = TALER_JSON_get_error_hint (j);
     270            0 :     break;
     271            1 :   case MHD_HTTP_UNAVAILABLE_FOR_LEGAL_REASONS:
     272            1 :     dr.hr.ec = TALER_JSON_get_error_code (j);
     273            1 :     dr.hr.hint = TALER_JSON_get_error_hint (j);
     274            1 :     if (GNUNET_OK !=
     275            1 :         TALER_EXCHANGE_parse_451 (&dr.details.unavailable_for_legal_reasons,
     276              :                                   j))
     277              :     {
     278            0 :       GNUNET_break_op (0);
     279            0 :       dr.hr.http_status = 0;
     280            0 :       dr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
     281            0 :       break;
     282              :     }
     283            1 :     break;
     284            0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     285            0 :     dr.hr.ec = TALER_JSON_get_error_code (j);
     286            0 :     dr.hr.hint = TALER_JSON_get_error_hint (j);
     287              :     /* Server had an internal issue; we should retry, but this API
     288              :        leaves this to the application */
     289            0 :     break;
     290            0 :   default:
     291              :     /* unexpected response code */
     292            0 :     dr.hr.ec = TALER_JSON_get_error_code (j);
     293            0 :     dr.hr.hint = TALER_JSON_get_error_hint (j);
     294            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     295              :                 "Unexpected response code %u/%d for exchange deposit\n",
     296              :                 (unsigned int) response_code,
     297              :                 dr.hr.ec);
     298            0 :     GNUNET_break_op (0);
     299            0 :     break;
     300              :   }
     301            6 :   if (NULL != pch->cb)
     302              :   {
     303            6 :     pch->cb (pch->cb_cls,
     304              :              &dr);
     305            6 :     pch->cb = NULL;
     306              :   }
     307            6 :   TALER_EXCHANGE_post_purses_merge_cancel (pch);
     308            6 : }
     309              : 
     310              : 
     311              : struct TALER_EXCHANGE_PostPursesMergeHandle *
     312            6 : TALER_EXCHANGE_post_purses_merge_create (
     313              :   struct GNUNET_CURL_Context *ctx,
     314              :   const char *url,
     315              :   struct TALER_EXCHANGE_Keys *keys,
     316              :   const char *reserve_exchange_url,
     317              :   const struct TALER_ReservePrivateKeyP *reserve_priv,
     318              :   const struct TALER_PurseContractPublicKeyP *purse_pub,
     319              :   const struct TALER_PurseMergePrivateKeyP *merge_priv,
     320              :   const struct TALER_PrivateContractHashP *h_contract_terms,
     321              :   uint8_t min_age,
     322              :   const struct TALER_Amount *purse_value_after_fees,
     323              :   struct GNUNET_TIME_Timestamp purse_expiration,
     324              :   struct GNUNET_TIME_Timestamp merge_timestamp)
     325              : {
     326              :   struct TALER_EXCHANGE_PostPursesMergeHandle *pch;
     327              :   struct TALER_NormalizedPayto reserve_url;
     328              : 
     329            6 :   pch = GNUNET_new (struct TALER_EXCHANGE_PostPursesMergeHandle);
     330            6 :   pch->ctx = ctx;
     331            6 :   pch->base_url = GNUNET_strdup (url);
     332            6 :   pch->merge_priv = *merge_priv;
     333            6 :   pch->purse_pub = *purse_pub;
     334            6 :   pch->h_contract_terms = *h_contract_terms;
     335            6 :   pch->purse_expiration = purse_expiration;
     336            6 :   pch->purse_value_after_fees = *purse_value_after_fees;
     337            6 :   if (NULL == reserve_exchange_url)
     338            6 :     pch->provider_url = GNUNET_strdup (url);
     339              :   else
     340            0 :     pch->provider_url = GNUNET_strdup (reserve_exchange_url);
     341            6 :   GNUNET_CRYPTO_eddsa_key_get_public (&reserve_priv->eddsa_priv,
     342              :                                       &pch->reserve_pub.eddsa_pub);
     343              : 
     344            6 :   reserve_url = TALER_reserve_make_payto (pch->provider_url,
     345            6 :                                           &pch->reserve_pub);
     346            6 :   if (NULL == reserve_url.normalized_payto)
     347              :   {
     348            0 :     GNUNET_break (0);
     349            0 :     GNUNET_free (pch->provider_url);
     350            0 :     GNUNET_free (pch->base_url);
     351            0 :     GNUNET_free (pch);
     352            0 :     return NULL;
     353              :   }
     354            6 :   TALER_wallet_purse_merge_sign (reserve_url,
     355              :                                  merge_timestamp,
     356              :                                  purse_pub,
     357              :                                  merge_priv,
     358              :                                  &pch->merge_sig);
     359              :   {
     360              :     struct TALER_Amount zero_purse_fee;
     361              : 
     362            6 :     GNUNET_assert (GNUNET_OK ==
     363              :                    TALER_amount_set_zero (purse_value_after_fees->currency,
     364              :                                           &zero_purse_fee));
     365            6 :     TALER_wallet_account_merge_sign (merge_timestamp,
     366              :                                      purse_pub,
     367              :                                      purse_expiration,
     368              :                                      h_contract_terms,
     369              :                                      purse_value_after_fees,
     370              :                                      &zero_purse_fee,
     371              :                                      min_age,
     372              :                                      TALER_WAMF_MODE_MERGE_FULLY_PAID_PURSE,
     373              :                                      reserve_priv,
     374              :                                      &pch->reserve_sig);
     375              :   }
     376              :   // FIXME: move JSON construction into _start() function
     377            6 :   pch->body = GNUNET_JSON_PACK (
     378              :     TALER_JSON_pack_normalized_payto ("payto_uri",
     379              :                                       reserve_url),
     380              :     GNUNET_JSON_pack_data_auto ("merge_sig",
     381              :                                 &pch->merge_sig),
     382              :     GNUNET_JSON_pack_data_auto ("reserve_sig",
     383              :                                 &pch->reserve_sig),
     384              :     GNUNET_JSON_pack_timestamp ("merge_timestamp",
     385              :                                 merge_timestamp));
     386            6 :   GNUNET_free (reserve_url.normalized_payto);
     387            6 :   if (NULL == pch->body)
     388              :   {
     389            0 :     GNUNET_break (0);
     390            0 :     GNUNET_free (pch->provider_url);
     391            0 :     GNUNET_free (pch->base_url);
     392            0 :     GNUNET_free (pch);
     393            0 :     return NULL;
     394              :   }
     395            6 :   pch->keys = TALER_EXCHANGE_keys_incref (keys);
     396            6 :   return pch;
     397              : }
     398              : 
     399              : 
     400              : enum TALER_ErrorCode
     401            6 : TALER_EXCHANGE_post_purses_merge_start (
     402              :   struct TALER_EXCHANGE_PostPursesMergeHandle *pch,
     403              :   TALER_EXCHANGE_PostPursesMergeCallback cb,
     404              :   TALER_EXCHANGE_POST_PURSES_MERGE_RESULT_CLOSURE *cb_cls)
     405              : {
     406              :   char arg_str[sizeof (pch->purse_pub) * 2 + 32];
     407              :   CURL *eh;
     408              : 
     409            6 :   pch->cb = cb;
     410            6 :   pch->cb_cls = cb_cls;
     411              : 
     412              :   {
     413              :     char pub_str[sizeof (pch->purse_pub) * 2];
     414              :     char *end;
     415              : 
     416            6 :     end = GNUNET_STRINGS_data_to_string (
     417            6 :       &pch->purse_pub,
     418              :       sizeof (pch->purse_pub),
     419              :       pub_str,
     420              :       sizeof (pub_str));
     421            6 :     *end = '\0';
     422            6 :     GNUNET_snprintf (arg_str,
     423              :                      sizeof (arg_str),
     424              :                      "purses/%s/merge",
     425              :                      pub_str);
     426              :   }
     427            6 :   pch->url = TALER_url_join (pch->base_url,
     428              :                              arg_str,
     429              :                              NULL);
     430            6 :   if (NULL == pch->url)
     431              :   {
     432            0 :     GNUNET_break (0);
     433            0 :     return TALER_EC_GENERIC_CONFIGURATION_INVALID;
     434              :   }
     435            6 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     436              :               "URL for purse merge: `%s'\n",
     437              :               pch->url);
     438            6 :   eh = TALER_EXCHANGE_curl_easy_get_ (pch->url);
     439           12 :   if ( (NULL == eh) ||
     440              :        (GNUNET_OK !=
     441            6 :         TALER_curl_easy_post (&pch->post_ctx,
     442              :                               eh,
     443            6 :                               pch->body)) )
     444              :   {
     445            0 :     GNUNET_break (0);
     446            0 :     if (NULL != eh)
     447            0 :       curl_easy_cleanup (eh);
     448            0 :     return TALER_EC_GENERIC_CURL_ALLOCATION_FAILURE;
     449              :   }
     450           12 :   pch->job = GNUNET_CURL_job_add2 (pch->ctx,
     451              :                                    eh,
     452            6 :                                    pch->post_ctx.headers,
     453              :                                    &handle_purse_merge_finished,
     454              :                                    pch);
     455            6 :   if (NULL == pch->job)
     456            0 :     return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
     457            6 :   return TALER_EC_NONE;
     458              : }
     459              : 
     460              : 
     461              : void
     462            6 : TALER_EXCHANGE_post_purses_merge_cancel (
     463              :   struct TALER_EXCHANGE_PostPursesMergeHandle *pch)
     464              : {
     465            6 :   if (NULL != pch->job)
     466              :   {
     467            0 :     GNUNET_CURL_job_cancel (pch->job);
     468            0 :     pch->job = NULL;
     469              :   }
     470            6 :   GNUNET_free (pch->url);
     471            6 :   GNUNET_free (pch->base_url);
     472            6 :   GNUNET_free (pch->provider_url);
     473            6 :   TALER_curl_easy_post_finished (&pch->post_ctx);
     474            6 :   TALER_EXCHANGE_keys_decref (pch->keys);
     475            6 :   json_decref (pch->body);
     476            6 :   GNUNET_free (pch);
     477            6 : }
     478              : 
     479              : 
     480              : /* end of exchange_api_post-purses-PURSE_PUB-merge.c */
        

Generated by: LCOV version 2.0-1