LCOV - code coverage report
Current view: top level - lib - auditor_api_deposit_confirmation.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 59 110 53.6 %
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-2021 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/auditor_api_deposit_confirmation.c
      19             :  * @brief Implementation of the /deposit request of the auditor's HTTP API
      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_json_lib.h>
      27             : #include <gnunet/gnunet_curl_lib.h>
      28             : #include "taler_json_lib.h"
      29             : #include "taler_auditor_service.h"
      30             : #include "auditor_api_handle.h"
      31             : #include "taler_signatures.h"
      32             : #include "auditor_api_curl_defaults.h"
      33             : 
      34             : 
      35             : /**
      36             :  * @brief A DepositConfirmation Handle
      37             :  */
      38             : struct TALER_AUDITOR_DepositConfirmationHandle
      39             : {
      40             : 
      41             :   /**
      42             :    * The connection to auditor this request handle will use
      43             :    */
      44             :   struct TALER_AUDITOR_Handle *auditor;
      45             : 
      46             :   /**
      47             :    * The url for this request.
      48             :    */
      49             :   char *url;
      50             : 
      51             :   /**
      52             :    * Context for #TEH_curl_easy_post(). Keeps the data that must
      53             :    * persist for Curl to make the upload.
      54             :    */
      55             :   struct TALER_CURL_PostContext ctx;
      56             : 
      57             :   /**
      58             :    * Handle for the request.
      59             :    */
      60             :   struct GNUNET_CURL_Job *job;
      61             : 
      62             :   /**
      63             :    * Function to call with the result.
      64             :    */
      65             :   TALER_AUDITOR_DepositConfirmationResultCallback cb;
      66             : 
      67             :   /**
      68             :    * Closure for @a cb.
      69             :    */
      70             :   void *cb_cls;
      71             : 
      72             : };
      73             : 
      74             : 
      75             : /**
      76             :  * Function called when we're done processing the
      77             :  * HTTP /deposit-confirmation request.
      78             :  *
      79             :  * @param cls the `struct TALER_AUDITOR_DepositConfirmationHandle`
      80             :  * @param response_code HTTP response code, 0 on error
      81             :  * @param djson parsed JSON result, NULL on error
      82             :  */
      83             : static void
      84           1 : handle_deposit_confirmation_finished (void *cls,
      85             :                                       long response_code,
      86             :                                       const void *djson)
      87             : {
      88           1 :   const json_t *json = djson;
      89           1 :   struct TALER_AUDITOR_DepositConfirmationHandle *dh = cls;
      90           1 :   struct TALER_AUDITOR_HttpResponse hr = {
      91             :     .reply = json,
      92           1 :     .http_status = (unsigned int) response_code
      93             :   };
      94             : 
      95           1 :   dh->job = NULL;
      96           1 :   switch (response_code)
      97             :   {
      98           0 :   case 0:
      99           0 :     hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     100           0 :     break;
     101           1 :   case MHD_HTTP_OK:
     102           1 :     hr.ec = TALER_EC_NONE;
     103           1 :     break;
     104           0 :   case MHD_HTTP_BAD_REQUEST:
     105           0 :     hr.ec = TALER_JSON_get_error_code (json);
     106           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     107             :     /* This should never happen, either us or the auditor is buggy
     108             :        (or API version conflict); just pass JSON reply to the application */
     109           0 :     break;
     110           0 :   case MHD_HTTP_FORBIDDEN:
     111           0 :     hr.ec = TALER_JSON_get_error_code (json);
     112           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     113             :     /* Nothing really to verify, auditor says one of the signatures is
     114             :        invalid; as we checked them, this should never happen, we
     115             :        should pass the JSON reply to the application */
     116           0 :     break;
     117           0 :   case MHD_HTTP_NOT_FOUND:
     118           0 :     hr.ec = TALER_JSON_get_error_code (json);
     119           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     120             :     /* Nothing really to verify, this should never
     121             :        happen, we should pass the JSON reply to the application */
     122           0 :     break;
     123           0 :   case MHD_HTTP_GONE:
     124           0 :     hr.ec = TALER_JSON_get_error_code (json);
     125           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     126             :     /* Nothing really to verify, auditor says one of the signatures is
     127             :        invalid; as we checked them, this should never happen, we
     128             :        should pass the JSON reply to the application */
     129           0 :     break;
     130           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     131           0 :     hr.ec = TALER_JSON_get_error_code (json);
     132           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     133             :     /* Server had an internal issue; we should retry, but this API
     134             :        leaves this to the application */
     135           0 :     break;
     136           0 :   default:
     137             :     /* unexpected response code */
     138           0 :     hr.ec = TALER_JSON_get_error_code (json);
     139           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     140           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     141             :                 "Unexpected response code %u/%d for auditor deposit confirmation\n",
     142             :                 (unsigned int) response_code,
     143             :                 hr.ec);
     144           0 :     break;
     145             :   }
     146           1 :   dh->cb (dh->cb_cls,
     147             :           &hr);
     148           1 :   TALER_AUDITOR_deposit_confirmation_cancel (dh);
     149           1 : }
     150             : 
     151             : 
     152             : /**
     153             :  * Verify signature information about the deposit-confirmation.
     154             :  *
     155             :  * @param h_wire hash of merchant wire details
     156             :  * @param h_contract_terms hash of the contact of the merchant with the customer (further details are never disclosed to the auditor)
     157             :  * @param exchange_timestamp timestamp when the deposit was received by the wallet
     158             :  * @param refund_deadline date until which the merchant can issue a refund to the customer via the auditor (can be zero if refunds are not allowed); must not be after the @a wire_deadline
     159             :  * @param amount_without_fee the amount confirmed to be wired by the exchange to the merchant
     160             :  * @param coin_pub coin’s public key
     161             :  * @param merchant_pub the public key of the merchant (used to identify the merchant for refund requests)
     162             :  * @param exchange_sig the signature made with purpose #TALER_SIGNATURE_EXCHANGE_CONFIRM_DEPOSIT
     163             :  * @param exchange_pub the public key of the exchange that matches @a exchange_sig
     164             :  * @param master_pub master public key of the exchange
     165             :  * @param ep_start when does @a exchange_pub validity start
     166             :  * @param ep_expire when does @a exchange_pub usage end
     167             :  * @param ep_end when does @a exchange_pub legal validity end
     168             :  * @param master_sig master signature affirming validity of @a exchange_pub
     169             :  * @return #GNUNET_OK if signatures are OK, #GNUNET_SYSERR if not
     170             :  */
     171             : static int
     172           1 : verify_signatures (const struct GNUNET_HashCode *h_wire,
     173             :                    const struct GNUNET_HashCode *h_contract_terms,
     174             :                    struct GNUNET_TIME_Absolute exchange_timestamp,
     175             :                    struct GNUNET_TIME_Absolute refund_deadline,
     176             :                    const struct TALER_Amount *amount_without_fee,
     177             :                    const struct TALER_CoinSpendPublicKeyP *coin_pub,
     178             :                    const struct TALER_MerchantPublicKeyP *merchant_pub,
     179             :                    const struct TALER_ExchangePublicKeyP *exchange_pub,
     180             :                    const struct TALER_ExchangeSignatureP *exchange_sig,
     181             :                    const struct TALER_MasterPublicKeyP *master_pub,
     182             :                    struct GNUNET_TIME_Absolute ep_start,
     183             :                    struct GNUNET_TIME_Absolute ep_expire,
     184             :                    struct GNUNET_TIME_Absolute ep_end,
     185             :                    const struct TALER_MasterSignatureP *master_sig)
     186             : {
     187             :   {
     188           2 :     struct TALER_DepositConfirmationPS dc = {
     189           1 :       .purpose.purpose = htonl (TALER_SIGNATURE_EXCHANGE_CONFIRM_DEPOSIT),
     190           1 :       .purpose.size = htonl (sizeof (dc)),
     191             :       .h_contract_terms = *h_contract_terms,
     192             :       .h_wire = *h_wire,
     193           1 :       .exchange_timestamp = GNUNET_TIME_absolute_hton (exchange_timestamp),
     194           1 :       .refund_deadline = GNUNET_TIME_absolute_hton (refund_deadline),
     195             :       .coin_pub = *coin_pub,
     196             :       .merchant = *merchant_pub
     197             :     };
     198             : 
     199           1 :     TALER_amount_hton (&dc.amount_without_fee,
     200             :                        amount_without_fee);
     201           1 :     if (GNUNET_OK !=
     202           1 :         GNUNET_CRYPTO_eddsa_verify (TALER_SIGNATURE_EXCHANGE_CONFIRM_DEPOSIT,
     203             :                                     &dc,
     204             :                                     &exchange_sig->eddsa_signature,
     205             :                                     &exchange_pub->eddsa_pub))
     206             :     {
     207           0 :       GNUNET_break_op (0);
     208           0 :       TALER_LOG_WARNING (
     209             :         "Invalid signature on /deposit-confirmation request!\n");
     210             :       {
     211           0 :         TALER_LOG_DEBUG ("... amount_without_fee was %s\n",
     212             :                          TALER_amount2s (amount_without_fee));
     213             :       }
     214           0 :       return GNUNET_SYSERR;
     215             :     }
     216             :   }
     217           1 :   if (GNUNET_OK !=
     218           1 :       TALER_exchange_offline_signkey_validity_verify (
     219             :         exchange_pub,
     220             :         ep_start,
     221             :         ep_expire,
     222             :         ep_end,
     223             :         master_pub,
     224             :         master_sig))
     225             :   {
     226           0 :     GNUNET_break (0);
     227           0 :     TALER_LOG_WARNING ("Invalid signature on exchange signing key!\n");
     228           0 :     return GNUNET_SYSERR;
     229             :   }
     230           1 :   if (GNUNET_TIME_absolute_is_past (ep_end))
     231             :   {
     232           0 :     GNUNET_break (0);
     233           0 :     TALER_LOG_WARNING ("Exchange signing key is no longer valid!\n");
     234           0 :     return GNUNET_SYSERR;
     235             :   }
     236           1 :   return GNUNET_OK;
     237             : }
     238             : 
     239             : 
     240             : /**
     241             :  * Submit a deposit-confirmation permission to the auditor and get the
     242             :  * auditor's response.  Note that while we return the response
     243             :  * verbatim to the caller for further processing, we do already verify
     244             :  * that the response is well-formed.  If the auditor's reply is not
     245             :  * well-formed, we return an HTTP status code of zero to @a cb.
     246             :  *
     247             :  * We also verify that the @a exchange_sig is valid for this deposit-confirmation
     248             :  * request, and that the @a master_sig is a valid signature for @a
     249             :  * exchange_pub.  Also, the @a auditor must be ready to operate (i.e.  have
     250             :  * finished processing the /version reply).  If either check fails, we do
     251             :  * NOT initiate the transaction with the auditor and instead return NULL.
     252             :  *
     253             :  * @param auditor the auditor handle; the auditor must be ready to operate
     254             :  * @param h_wire hash of merchant wire details
     255             :  * @param h_contract_terms hash of the contact of the merchant with the customer (further details are never disclosed to the auditor)
     256             :  * @param exchange_timestamp timestamp when deposit was received by the exchange
     257             :  * @param refund_deadline date until which the merchant can issue a refund to the customer via the auditor (can be zero if refunds are not allowed); must not be after the @a wire_deadline
     258             :  * @param amount_without_fee the amount confirmed to be wired by the exchange to the merchant
     259             :  * @param coin_pub coin’s public key
     260             :  * @param merchant_pub the public key of the merchant (used to identify the merchant for refund requests)
     261             :  * @param exchange_sig the signature made with purpose #TALER_SIGNATURE_EXCHANGE_CONFIRM_DEPOSIT
     262             :  * @param exchange_pub the public key of the exchange that matches @a exchange_sig
     263             :  * @param master_pub master public key of the exchange
     264             :  * @param ep_start when does @a exchange_pub validity start
     265             :  * @param ep_expire when does @a exchange_pub usage end
     266             :  * @param ep_end when does @a exchange_pub legal validity end
     267             :  * @param master_sig master signature affirming validity of @a exchange_pub
     268             :  * @param cb the callback to call when a reply for this request is available
     269             :  * @param cb_cls closure for the above callback
     270             :  * @return a handle for this request; NULL if the inputs are invalid (i.e.
     271             :  *         signatures fail to verify).  In this case, the callback is not called.
     272             :  */
     273             : struct TALER_AUDITOR_DepositConfirmationHandle *
     274           1 : TALER_AUDITOR_deposit_confirmation (
     275             :   struct TALER_AUDITOR_Handle *auditor,
     276             :   const struct GNUNET_HashCode *h_wire,
     277             :   const struct GNUNET_HashCode *h_contract_terms,
     278             :   struct GNUNET_TIME_Absolute exchange_timestamp,
     279             :   struct GNUNET_TIME_Absolute refund_deadline,
     280             :   const struct TALER_Amount *amount_without_fee,
     281             :   const struct TALER_CoinSpendPublicKeyP *coin_pub,
     282             :   const struct TALER_MerchantPublicKeyP *merchant_pub,
     283             :   const struct TALER_ExchangePublicKeyP *exchange_pub,
     284             :   const struct TALER_ExchangeSignatureP *exchange_sig,
     285             :   const struct TALER_MasterPublicKeyP *master_pub,
     286             :   struct GNUNET_TIME_Absolute ep_start,
     287             :   struct GNUNET_TIME_Absolute ep_expire,
     288             :   struct GNUNET_TIME_Absolute ep_end,
     289             :   const struct TALER_MasterSignatureP *master_sig,
     290             :   TALER_AUDITOR_DepositConfirmationResultCallback cb,
     291             :   void *cb_cls)
     292             : {
     293             :   struct TALER_AUDITOR_DepositConfirmationHandle *dh;
     294             :   struct GNUNET_CURL_Context *ctx;
     295             :   json_t *deposit_confirmation_obj;
     296             :   CURL *eh;
     297             : 
     298           1 :   (void) GNUNET_TIME_round_abs (&exchange_timestamp);
     299           1 :   (void) GNUNET_TIME_round_abs (&refund_deadline);
     300           1 :   (void) GNUNET_TIME_round_abs (&ep_start);
     301           1 :   (void) GNUNET_TIME_round_abs (&ep_expire);
     302           1 :   (void) GNUNET_TIME_round_abs (&ep_end);
     303           1 :   GNUNET_assert (GNUNET_YES ==
     304             :                  TALER_AUDITOR_handle_is_ready_ (auditor));
     305           1 :   if (GNUNET_OK !=
     306           1 :       verify_signatures (h_wire,
     307             :                          h_contract_terms,
     308             :                          exchange_timestamp,
     309             :                          refund_deadline,
     310             :                          amount_without_fee,
     311             :                          coin_pub,
     312             :                          merchant_pub,
     313             :                          exchange_pub,
     314             :                          exchange_sig,
     315             :                          master_pub,
     316             :                          ep_start,
     317             :                          ep_expire,
     318             :                          ep_end,
     319             :                          master_sig))
     320             :   {
     321           0 :     GNUNET_break_op (0);
     322           0 :     return NULL;
     323             :   }
     324             : 
     325             :   deposit_confirmation_obj
     326           1 :     = GNUNET_JSON_PACK (
     327             :         GNUNET_JSON_pack_data_auto ("h_wire",
     328             :                                     h_wire),
     329             :         GNUNET_JSON_pack_data_auto ("h_contract_terms",
     330             :                                     h_contract_terms),
     331             :         GNUNET_JSON_pack_time_abs ("exchange_timestamp",
     332             :                                    exchange_timestamp),
     333             :         GNUNET_JSON_pack_time_abs ("refund_deadline",
     334             :                                    refund_deadline),
     335             :         TALER_JSON_pack_amount ("amount_without_fee",
     336             :                                 amount_without_fee),
     337             :         GNUNET_JSON_pack_data_auto ("coin_pub",
     338             :                                     coin_pub),
     339             :         GNUNET_JSON_pack_data_auto ("merchant_pub",
     340             :                                     merchant_pub),
     341             :         GNUNET_JSON_pack_data_auto ("exchange_sig",
     342             :                                     exchange_sig),
     343             :         GNUNET_JSON_pack_data_auto ("master_pub",
     344             :                                     master_pub),
     345             :         GNUNET_JSON_pack_time_abs ("ep_start",
     346             :                                    ep_start),
     347             :         GNUNET_JSON_pack_time_abs ("ep_expire",
     348             :                                    ep_expire),
     349             :         GNUNET_JSON_pack_time_abs ("ep_end",
     350             :                                    ep_end),
     351             :         GNUNET_JSON_pack_data_auto ("master_sig",
     352             :                                     master_sig),
     353             :         GNUNET_JSON_pack_data_auto ("exchange_pub",
     354             :                                     exchange_pub));
     355           1 :   dh = GNUNET_new (struct TALER_AUDITOR_DepositConfirmationHandle);
     356           1 :   dh->auditor = auditor;
     357           1 :   dh->cb = cb;
     358           1 :   dh->cb_cls = cb_cls;
     359           1 :   dh->url = TALER_AUDITOR_path_to_url_ (auditor,
     360             :                                         "/deposit-confirmation");
     361           1 :   if (NULL == dh->url)
     362             :   {
     363           0 :     GNUNET_free (dh);
     364           0 :     return NULL;
     365             :   }
     366           1 :   eh = TALER_AUDITOR_curl_easy_get_ (dh->url);
     367             : 
     368           2 :   if ( (NULL == eh) ||
     369             :        (CURLE_OK !=
     370           1 :         curl_easy_setopt (eh,
     371             :                           CURLOPT_CUSTOMREQUEST,
     372           1 :                           "PUT")) ||
     373             :        (GNUNET_OK !=
     374           1 :         TALER_curl_easy_post (&dh->ctx,
     375             :                               eh,
     376             :                               deposit_confirmation_obj)) )
     377             :   {
     378           0 :     GNUNET_break (0);
     379           0 :     if (NULL != eh)
     380           0 :       curl_easy_cleanup (eh);
     381           0 :     json_decref (deposit_confirmation_obj);
     382           0 :     GNUNET_free (dh->url);
     383           0 :     GNUNET_free (dh);
     384           0 :     return NULL;
     385             :   }
     386           1 :   json_decref (deposit_confirmation_obj);
     387           1 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     388             :               "URL for deposit-confirmation: `%s'\n",
     389             :               dh->url);
     390           1 :   ctx = TALER_AUDITOR_handle_to_context_ (auditor);
     391           2 :   dh->job = GNUNET_CURL_job_add2 (ctx,
     392             :                                   eh,
     393           1 :                                   dh->ctx.headers,
     394             :                                   &handle_deposit_confirmation_finished,
     395             :                                   dh);
     396           1 :   return dh;
     397             : }
     398             : 
     399             : 
     400             : /**
     401             :  * Cancel a deposit-confirmation permission request.  This function cannot be used
     402             :  * on a request handle if a response is already served for it.
     403             :  *
     404             :  * @param deposit_confirmation the deposit-confirmation permission request handle
     405             :  */
     406             : void
     407           1 : TALER_AUDITOR_deposit_confirmation_cancel (
     408             :   struct TALER_AUDITOR_DepositConfirmationHandle *deposit_confirmation)
     409             : {
     410           1 :   if (NULL != deposit_confirmation->job)
     411             :   {
     412           0 :     GNUNET_CURL_job_cancel (deposit_confirmation->job);
     413           0 :     deposit_confirmation->job = NULL;
     414             :   }
     415           1 :   GNUNET_free (deposit_confirmation->url);
     416           1 :   TALER_curl_easy_post_finished (&deposit_confirmation->ctx);
     417           1 :   GNUNET_free (deposit_confirmation);
     418           1 : }
     419             : 
     420             : 
     421             : /* end of auditor_api_deposit_confirmation.c */

Generated by: LCOV version 1.14