LCOV - code coverage report
Current view: top level - lib - exchange_api_transfers_get.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 104 164 63.4 %
Date: 2021-08-30 06:43:37 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-2020 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_transfers_get.c
      19             :  * @brief Implementation of the GET /transfers/ request
      20             :  * @author Christian Grothoff
      21             :  */
      22             : #include "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_curl_lib.h>
      27             : #include "taler_exchange_service.h"
      28             : #include "taler_json_lib.h"
      29             : #include "exchange_api_handle.h"
      30             : #include "taler_signatures.h"
      31             : #include "exchange_api_curl_defaults.h"
      32             : 
      33             : 
      34             : /**
      35             :  * @brief A /transfers/ GET Handle
      36             :  */
      37             : struct TALER_EXCHANGE_TransfersGetHandle
      38             : {
      39             : 
      40             :   /**
      41             :    * The connection to exchange this request handle will use
      42             :    */
      43             :   struct TALER_EXCHANGE_Handle *exchange;
      44             : 
      45             :   /**
      46             :    * The url for this request.
      47             :    */
      48             :   char *url;
      49             : 
      50             :   /**
      51             :    * Handle for the request.
      52             :    */
      53             :   struct GNUNET_CURL_Job *job;
      54             : 
      55             :   /**
      56             :    * Function to call with the result.
      57             :    */
      58             :   TALER_EXCHANGE_TransfersGetCallback cb;
      59             : 
      60             :   /**
      61             :    * Closure for @a cb.
      62             :    */
      63             :   void *cb_cls;
      64             : 
      65             : };
      66             : 
      67             : 
      68             : /**
      69             :  * We got a #MHD_HTTP_OK response for the /transfers/ request.
      70             :  * Check that the response is well-formed and if it is, call the
      71             :  * callback.  If not, return an error code.
      72             :  *
      73             :  * This code is very similar to
      74             :  * merchant_api_track_transfer.c::check_transfers_get_response_ok.
      75             :  * Any changes should likely be reflected there as well.
      76             :  *
      77             :  * @param wdh handle to the operation
      78             :  * @param json response we got
      79             :  * @return #GNUNET_OK if we are done and all is well,
      80             :  *         #GNUNET_SYSERR if the response was bogus
      81             :  */
      82             : static int
      83           2 : check_transfers_get_response_ok (
      84             :   struct TALER_EXCHANGE_TransfersGetHandle *wdh,
      85             :   const json_t *json)
      86             : {
      87             :   json_t *details_j;
      88             :   struct TALER_EXCHANGE_TransferData td;
      89             :   struct TALER_Amount total_expected;
      90             :   struct TALER_MerchantPublicKeyP merchant_pub;
      91             :   struct GNUNET_JSON_Specification spec[] = {
      92           2 :     TALER_JSON_spec_amount_any ("total", &td.total_amount),
      93           2 :     TALER_JSON_spec_amount_any ("wire_fee", &td.wire_fee),
      94           2 :     GNUNET_JSON_spec_fixed_auto ("merchant_pub", &merchant_pub),
      95           2 :     GNUNET_JSON_spec_fixed_auto ("h_wire", &td.h_wire),
      96           2 :     TALER_JSON_spec_absolute_time ("execution_time", &td.execution_time),
      97           2 :     GNUNET_JSON_spec_json ("deposits", &details_j),
      98           2 :     GNUNET_JSON_spec_fixed_auto ("exchange_sig", &td.exchange_sig),
      99           2 :     GNUNET_JSON_spec_fixed_auto ("exchange_pub", &td.exchange_pub),
     100           2 :     GNUNET_JSON_spec_end ()
     101             :   };
     102           2 :   struct TALER_EXCHANGE_HttpResponse hr = {
     103             :     .reply = json,
     104             :     .http_status = MHD_HTTP_OK
     105             :   };
     106             : 
     107           2 :   if (GNUNET_OK !=
     108           2 :       GNUNET_JSON_parse (json,
     109             :                          spec,
     110             :                          NULL, NULL))
     111             :   {
     112           0 :     GNUNET_break_op (0);
     113           0 :     return GNUNET_SYSERR;
     114             :   }
     115           2 :   if (GNUNET_OK !=
     116           2 :       TALER_amount_set_zero (td.total_amount.currency,
     117             :                              &total_expected))
     118             :   {
     119           0 :     GNUNET_break_op (0);
     120           0 :     GNUNET_JSON_parse_free (spec);
     121           0 :     return GNUNET_SYSERR;
     122             :   }
     123           2 :   if (GNUNET_OK !=
     124           2 :       TALER_EXCHANGE_test_signing_key (
     125             :         TALER_EXCHANGE_get_keys (wdh->exchange),
     126             :         &td.exchange_pub))
     127             :   {
     128           0 :     GNUNET_break_op (0);
     129           0 :     GNUNET_JSON_parse_free (spec);
     130           0 :     return GNUNET_SYSERR;
     131             :   }
     132           2 :   td.details_length = json_array_size (details_j);
     133             :   {
     134             :     struct GNUNET_HashContext *hash_context;
     135             :     struct TALER_TrackTransferDetails *details;
     136             : 
     137           2 :     details = GNUNET_new_array (td.details_length,
     138             :                                 struct TALER_TrackTransferDetails);
     139           2 :     td.details = details;
     140           2 :     hash_context = GNUNET_CRYPTO_hash_context_start ();
     141           4 :     for (unsigned int i = 0; i<td.details_length; i++)
     142             :     {
     143           2 :       struct TALER_TrackTransferDetails *detail = &details[i];
     144           2 :       struct json_t *detail_j = json_array_get (details_j, i);
     145             :       struct GNUNET_JSON_Specification spec_detail[] = {
     146           2 :         GNUNET_JSON_spec_fixed_auto ("h_contract_terms",
     147             :                                      &detail->h_contract_terms),
     148           2 :         GNUNET_JSON_spec_fixed_auto ("coin_pub", &detail->coin_pub),
     149           2 :         TALER_JSON_spec_amount_any ("deposit_value", &detail->coin_value),
     150           2 :         TALER_JSON_spec_amount_any ("deposit_fee", &detail->coin_fee),
     151           2 :         GNUNET_JSON_spec_end ()
     152             :       };
     153             : 
     154           2 :       if ( (GNUNET_OK !=
     155           2 :             GNUNET_JSON_parse (detail_j,
     156             :                                spec_detail,
     157           2 :                                NULL, NULL)) ||
     158             :            (GNUNET_OK !=
     159           2 :             TALER_amount_cmp_currency (&total_expected,
     160           4 :                                        &detail->coin_value)) ||
     161             :            (GNUNET_OK !=
     162           2 :             TALER_amount_cmp_currency (&total_expected,
     163           4 :                                        &detail->coin_fee)) ||
     164             :            (0 >
     165           2 :             TALER_amount_add (&total_expected,
     166             :                               &total_expected,
     167           4 :                               &detail->coin_value)) ||
     168             :            (0 >
     169           2 :             TALER_amount_subtract (&total_expected,
     170             :                                    &total_expected,
     171           2 :                                    &detail->coin_fee)) )
     172             :       {
     173           0 :         GNUNET_break_op (0);
     174           0 :         GNUNET_CRYPTO_hash_context_abort (hash_context);
     175           0 :         GNUNET_JSON_parse_free (spec);
     176           0 :         GNUNET_free (details);
     177           0 :         return GNUNET_SYSERR;
     178             :       }
     179             :       /* build up big hash for signature checking later */
     180             :       {
     181             :         struct TALER_WireDepositDetailP dd;
     182             : 
     183           2 :         dd.h_contract_terms = detail->h_contract_terms;
     184           2 :         dd.execution_time = GNUNET_TIME_absolute_hton (td.execution_time);
     185           2 :         dd.coin_pub = detail->coin_pub;
     186           2 :         TALER_amount_hton (&dd.deposit_value,
     187           2 :                            &detail->coin_value);
     188           2 :         TALER_amount_hton (&dd.deposit_fee,
     189           2 :                            &detail->coin_fee);
     190           2 :         GNUNET_CRYPTO_hash_context_read (hash_context,
     191             :                                          &dd,
     192             :                                          sizeof (dd));
     193             :       }
     194             :     }
     195             :     /* Check signature */
     196             :     {
     197           2 :       struct TALER_WireDepositDataPS wdp = {
     198           2 :         .purpose.purpose = htonl (
     199             :           TALER_SIGNATURE_EXCHANGE_CONFIRM_WIRE_DEPOSIT),
     200           2 :         .purpose.size = htonl (sizeof (wdp)),
     201             :         .merchant_pub = merchant_pub,
     202             :         .h_wire = td.h_wire
     203             :       };
     204             : 
     205           2 :       TALER_amount_hton (&wdp.total,
     206             :                          &td.total_amount);
     207           2 :       TALER_amount_hton (&wdp.wire_fee,
     208             :                          &td.wire_fee);
     209           2 :       GNUNET_CRYPTO_hash_context_finish (hash_context,
     210             :                                          &wdp.h_details);
     211           2 :       if (GNUNET_OK !=
     212           2 :           GNUNET_CRYPTO_eddsa_verify (
     213             :             TALER_SIGNATURE_EXCHANGE_CONFIRM_WIRE_DEPOSIT,
     214             :             &wdp,
     215             :             &td.exchange_sig.eddsa_signature,
     216             :             &td.exchange_pub.eddsa_pub))
     217             :       {
     218           0 :         GNUNET_break_op (0);
     219           0 :         GNUNET_JSON_parse_free (spec);
     220           0 :         GNUNET_free (details);
     221           0 :         return GNUNET_SYSERR;
     222             :       }
     223             :     }
     224             : 
     225           2 :     if (0 >
     226           2 :         TALER_amount_subtract (&total_expected,
     227             :                                &total_expected,
     228             :                                &td.wire_fee))
     229             :     {
     230           0 :       GNUNET_break_op (0);
     231           0 :       GNUNET_JSON_parse_free (spec);
     232           0 :       GNUNET_free (details);
     233           0 :       return GNUNET_SYSERR;
     234             :     }
     235           2 :     if (0 !=
     236           2 :         TALER_amount_cmp (&total_expected,
     237             :                           &td.total_amount))
     238             :     {
     239           0 :       GNUNET_break_op (0);
     240           0 :       GNUNET_JSON_parse_free (spec);
     241           0 :       GNUNET_free (details);
     242           0 :       return GNUNET_SYSERR;
     243             :     }
     244           2 :     wdh->cb (wdh->cb_cls,
     245             :              &hr,
     246             :              &td);
     247           2 :     GNUNET_free (details);
     248             :   }
     249           2 :   GNUNET_JSON_parse_free (spec);
     250           2 :   TALER_EXCHANGE_transfers_get_cancel (wdh);
     251           2 :   return GNUNET_OK;
     252             : }
     253             : 
     254             : 
     255             : /**
     256             :  * Function called when we're done processing the
     257             :  * HTTP /transfers/ request.
     258             :  *
     259             :  * @param cls the `struct TALER_EXCHANGE_TransfersGetHandle`
     260             :  * @param response_code HTTP response code, 0 on error
     261             :  * @param response parsed JSON result, NULL on error
     262             :  */
     263             : static void
     264           3 : handle_transfers_get_finished (void *cls,
     265             :                                long response_code,
     266             :                                const void *response)
     267             : {
     268           3 :   struct TALER_EXCHANGE_TransfersGetHandle *wdh = cls;
     269           3 :   const json_t *j = response;
     270           3 :   struct TALER_EXCHANGE_HttpResponse hr = {
     271             :     .reply = j,
     272           3 :     .http_status = (unsigned int) response_code
     273             :   };
     274             : 
     275           3 :   wdh->job = NULL;
     276           3 :   switch (response_code)
     277             :   {
     278           0 :   case 0:
     279           0 :     hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     280           0 :     break;
     281           2 :   case MHD_HTTP_OK:
     282           2 :     if (GNUNET_OK ==
     283           2 :         check_transfers_get_response_ok (wdh,
     284             :                                          j))
     285           2 :       return;
     286           0 :     GNUNET_break_op (0);
     287           0 :     hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
     288           0 :     hr.http_status = 0;
     289           0 :     break;
     290           0 :   case MHD_HTTP_BAD_REQUEST:
     291             :     /* This should never happen, either us or the exchange is buggy
     292             :        (or API version conflict); just pass JSON reply to the application */
     293           0 :     hr.ec = TALER_JSON_get_error_code (j);
     294           0 :     hr.hint = TALER_JSON_get_error_hint (j);
     295           0 :     break;
     296           0 :   case MHD_HTTP_FORBIDDEN:
     297             :     /* Nothing really to verify, exchange says one of the signatures is
     298             :        invalid; as we checked them, this should never happen, we
     299             :        should pass the JSON reply to the application */
     300           0 :     hr.ec = TALER_JSON_get_error_code (j);
     301           0 :     hr.hint = TALER_JSON_get_error_hint (j);
     302           0 :     break;
     303           1 :   case MHD_HTTP_NOT_FOUND:
     304             :     /* Exchange does not know about transaction;
     305             :        we should pass the reply to the application */
     306           1 :     hr.ec = TALER_JSON_get_error_code (j);
     307           1 :     hr.hint = TALER_JSON_get_error_hint (j);
     308           1 :     break;
     309           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     310             :     /* Server had an internal issue; we should retry, but this API
     311             :        leaves this to the application */
     312           0 :     hr.ec = TALER_JSON_get_error_code (j);
     313           0 :     hr.hint = TALER_JSON_get_error_hint (j);
     314           0 :     break;
     315           0 :   default:
     316             :     /* unexpected response code */
     317           0 :     GNUNET_break_op (0);
     318           0 :     hr.ec = TALER_JSON_get_error_code (j);
     319           0 :     hr.hint = TALER_JSON_get_error_hint (j);
     320           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     321             :                 "Unexpected response code %u/%d for transfers get\n",
     322             :                 (unsigned int) response_code,
     323             :                 (int) hr.ec);
     324           0 :     break;
     325             :   }
     326           1 :   wdh->cb (wdh->cb_cls,
     327             :            &hr,
     328             :            NULL);
     329           1 :   TALER_EXCHANGE_transfers_get_cancel (wdh);
     330             : }
     331             : 
     332             : 
     333             : /**
     334             :  * Query the exchange about which transactions were combined
     335             :  * to create a wire transfer.
     336             :  *
     337             :  * @param exchange exchange to query
     338             :  * @param wtid raw wire transfer identifier to get information about
     339             :  * @param cb callback to call
     340             :  * @param cb_cls closure for @a cb
     341             :  * @return handle to cancel operation
     342             :  */
     343             : struct TALER_EXCHANGE_TransfersGetHandle *
     344           3 : TALER_EXCHANGE_transfers_get (
     345             :   struct TALER_EXCHANGE_Handle *exchange,
     346             :   const struct TALER_WireTransferIdentifierRawP *wtid,
     347             :   TALER_EXCHANGE_TransfersGetCallback cb,
     348             :   void *cb_cls)
     349             : {
     350             :   struct TALER_EXCHANGE_TransfersGetHandle *wdh;
     351             :   struct GNUNET_CURL_Context *ctx;
     352             :   CURL *eh;
     353             :   char arg_str[sizeof (struct TALER_WireTransferIdentifierRawP) * 2 + 32];
     354             : 
     355           3 :   if (GNUNET_YES !=
     356           3 :       TEAH_handle_is_ready (exchange))
     357             :   {
     358           0 :     GNUNET_break (0);
     359           0 :     return NULL;
     360             :   }
     361             : 
     362           3 :   wdh = GNUNET_new (struct TALER_EXCHANGE_TransfersGetHandle);
     363           3 :   wdh->exchange = exchange;
     364           3 :   wdh->cb = cb;
     365           3 :   wdh->cb_cls = cb_cls;
     366             : 
     367             :   {
     368             :     char wtid_str[sizeof (struct TALER_WireTransferIdentifierRawP) * 2];
     369             :     char *end;
     370             : 
     371           3 :     end = GNUNET_STRINGS_data_to_string (wtid,
     372             :                                          sizeof (struct
     373             :                                                  TALER_WireTransferIdentifierRawP),
     374             :                                          wtid_str,
     375             :                                          sizeof (wtid_str));
     376           3 :     *end = '\0';
     377           3 :     GNUNET_snprintf (arg_str,
     378             :                      sizeof (arg_str),
     379             :                      "/transfers/%s",
     380             :                      wtid_str);
     381             :   }
     382           3 :   wdh->url = TEAH_path_to_url (wdh->exchange,
     383             :                                arg_str);
     384           3 :   if (NULL == wdh->url)
     385             :   {
     386           0 :     GNUNET_free (wdh);
     387           0 :     return NULL;
     388             :   }
     389           3 :   eh = TALER_EXCHANGE_curl_easy_get_ (wdh->url);
     390           3 :   if (NULL == eh)
     391             :   {
     392           0 :     GNUNET_break (0);
     393           0 :     GNUNET_free (wdh->url);
     394           0 :     GNUNET_free (wdh);
     395           0 :     return NULL;
     396             :   }
     397           3 :   ctx = TEAH_handle_to_context (exchange);
     398           3 :   wdh->job = GNUNET_CURL_job_add_with_ct_json (ctx,
     399             :                                                eh,
     400             :                                                &handle_transfers_get_finished,
     401             :                                                wdh);
     402           3 :   return wdh;
     403             : }
     404             : 
     405             : 
     406             : /**
     407             :  * Cancel wire deposits request.  This function cannot be used on a request
     408             :  * handle if a response is already served for it.
     409             :  *
     410             :  * @param wdh the wire deposits request handle
     411             :  */
     412             : void
     413           3 : TALER_EXCHANGE_transfers_get_cancel (
     414             :   struct TALER_EXCHANGE_TransfersGetHandle *wdh)
     415             : {
     416           3 :   if (NULL != wdh->job)
     417             :   {
     418           0 :     GNUNET_CURL_job_cancel (wdh->job);
     419           0 :     wdh->job = NULL;
     420             :   }
     421           3 :   GNUNET_free (wdh->url);
     422           3 :   GNUNET_free (wdh);
     423           3 : }
     424             : 
     425             : 
     426             : /* end of exchange_api_transfers_get.c */

Generated by: LCOV version 1.14