LCOV - code coverage report
Current view: top level - lib - merchant_api_tip_pickup.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 68 0.0 %
Date: 2021-04-12 06:04:58 Functions: 0 3 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-2017, 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 Lesser General Public License as published by the Free Software
       7             :   Foundation; either version 2.1, 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 Lesser General Public License for more details.
      12             : 
      13             :   You should have received a copy of the GNU Lesser General Public License along with
      14             :   TALER; see the file COPYING.LGPL.  If not, see
      15             :   <http://www.gnu.org/licenses/>
      16             : */
      17             : /**
      18             :  * @file merchant_api_tip_pickup.c
      19             :  * @brief Implementation of the /tip-pickup request of the merchant's HTTP API
      20             :  * @author Marcello Stanisci
      21             :  * @author Christian Grothoff
      22             :  */
      23             : #include "platform.h"
      24             : #include <curl/curl.h>
      25             : #include <jansson.h>
      26             : #include <microhttpd.h> /* just for HTTP status codes */
      27             : #include <gnunet/gnunet_util_lib.h>
      28             : #include <gnunet/gnunet_curl_lib.h>
      29             : #include "taler_merchant_service.h"
      30             : #include <taler/taler_json_lib.h>
      31             : #include <taler/taler_signatures.h>
      32             : #include <taler/taler_curl_lib.h>
      33             : 
      34             : 
      35             : /**
      36             :  * Data we keep per planchet.
      37             :  */
      38             : struct PlanchetData
      39             : {
      40             :   /**
      41             :    * Secrets of the planchet.
      42             :    */
      43             :   struct TALER_PlanchetSecretsP ps;
      44             : 
      45             :   /**
      46             :    * Denomination key we are withdrawing.
      47             :    */
      48             :   struct TALER_EXCHANGE_DenomPublicKey pk;
      49             : 
      50             :   /**
      51             :    * Hash of the public key of the coin we are signing.
      52             :    */
      53             :   struct GNUNET_HashCode c_hash;
      54             : };
      55             : 
      56             : 
      57             : /**
      58             :  * Handle for a /tip-pickup operation.
      59             :  */
      60             : struct TALER_MERCHANT_TipPickupHandle
      61             : {
      62             : 
      63             :   /**
      64             :    * Function to call with the result.
      65             :    */
      66             :   TALER_MERCHANT_TipPickupCallback cb;
      67             : 
      68             :   /**
      69             :    * Closure for @a cb.
      70             :    */
      71             :   void *cb_cls;
      72             : 
      73             :   /**
      74             :    * Handle for the actual (internal) withdraw operation.
      75             :    */
      76             :   struct TALER_MERCHANT_TipPickup2Handle *tpo2;
      77             : 
      78             :   /**
      79             :    * Number of planchets/coins used for this operation.
      80             :    */
      81             :   unsigned int num_planchets;
      82             : 
      83             :   /**
      84             :    * Array of length @e num_planchets.
      85             :    */
      86             :   struct PlanchetData *planchets;
      87             : 
      88             : };
      89             : 
      90             : 
      91             : /**
      92             :  * Callback for a /tip-pickup request.  Returns the result of the operation.
      93             :  * Note that the client MUST still do the unblinding of the @a blind_sigs.
      94             :  *
      95             :  * @param cls closure, a `struct TALER_MERCHANT_TipPickupHandle *`
      96             :  * @param hr HTTP response details
      97             :  * @param num_blind_sigs length of the @a reserve_sigs array, 0 on error
      98             :  * @param blind_sigs array of blind signatures over the planchets, NULL on error
      99             :  */
     100             : static void
     101           0 : pickup_done_cb (void *cls,
     102             :                 const struct TALER_MERCHANT_HttpResponse *hr,
     103             :                 unsigned int num_blind_sigs,
     104             :                 const struct TALER_MERCHANT_BlindSignature *blind_sigs)
     105             : {
     106           0 :   struct TALER_MERCHANT_TipPickupHandle *tp = cls;
     107             : 
     108           0 :   tp->tpo2 = NULL;
     109           0 :   if (NULL == blind_sigs)
     110             :   {
     111           0 :     tp->cb (tp->cb_cls,
     112             :             hr,
     113             :             0,
     114             :             NULL);
     115           0 :     TALER_MERCHANT_tip_pickup_cancel (tp);
     116           0 :     return;
     117             :   }
     118           0 :   {
     119           0 :     struct TALER_DenominationSignature sigs[num_blind_sigs];
     120             :     int ok;
     121             : 
     122           0 :     ok = GNUNET_OK;
     123           0 :     memset (sigs,
     124             :             0,
     125             :             sizeof (sigs));
     126           0 :     for (unsigned int i = 0; i<num_blind_sigs; i++)
     127             :     {
     128             :       struct TALER_FreshCoin fc;
     129             : 
     130           0 :       if (GNUNET_OK !=
     131           0 :           TALER_planchet_to_coin (&tp->planchets[i].pk.key,
     132           0 :                                   blind_sigs[i].blind_sig,
     133           0 :                                   &tp->planchets[i].ps,
     134           0 :                                   &tp->planchets[i].c_hash,
     135             :                                   &fc))
     136             :       {
     137           0 :         ok = GNUNET_SYSERR;
     138           0 :         break;
     139             :       }
     140           0 :       sigs[i] = fc.sig;
     141             :     }
     142           0 :     if (GNUNET_OK == ok)
     143             :     {
     144           0 :       tp->cb (tp->cb_cls,
     145             :               hr,
     146             :               num_blind_sigs,
     147             :               sigs);
     148             :     }
     149             :     else
     150             :     {
     151           0 :       struct TALER_MERCHANT_HttpResponse hrx = {
     152           0 :         .reply = hr->reply,
     153             :         .http_status = 0,
     154             :         .ec = TALER_EC_MERCHANT_TIP_PICKUP_UNBLIND_FAILURE
     155             :       };
     156             : 
     157           0 :       tp->cb (tp->cb_cls,
     158             :               &hrx,
     159             :               0,
     160             :               NULL);
     161             :     }
     162           0 :     for (unsigned int i = 0; i<num_blind_sigs; i++)
     163           0 :       if (NULL != sigs[i].rsa_signature)
     164           0 :         GNUNET_CRYPTO_rsa_signature_free (sigs[i].rsa_signature);
     165             :   }
     166           0 :   TALER_MERCHANT_tip_pickup_cancel (tp);
     167             : }
     168             : 
     169             : 
     170             : struct TALER_MERCHANT_TipPickupHandle *
     171           0 : TALER_MERCHANT_tip_pickup (struct GNUNET_CURL_Context *ctx,
     172             :                            const char *backend_url,
     173             :                            const struct GNUNET_HashCode *tip_id,
     174             :                            unsigned int num_planchets,
     175             :                            const struct TALER_MERCHANT_PlanchetData *pds,
     176             :                            TALER_MERCHANT_TipPickupCallback pickup_cb,
     177             :                            void *pickup_cb_cls)
     178           0 : {
     179             :   struct TALER_MERCHANT_TipPickupHandle *tp;
     180           0 :   struct TALER_PlanchetDetail details[GNUNET_NZL (num_planchets)];
     181             : 
     182           0 :   if (0 == num_planchets)
     183             :   {
     184           0 :     GNUNET_break (0);
     185           0 :     return NULL;
     186             :   }
     187           0 :   tp = GNUNET_new (struct TALER_MERCHANT_TipPickupHandle);
     188           0 :   GNUNET_array_grow (tp->planchets,
     189             :                      tp->num_planchets,
     190             :                      num_planchets);
     191           0 :   for (unsigned int i = 0; i<num_planchets; i++)
     192             :   {
     193           0 :     tp->planchets[i].ps = pds[i].ps;
     194           0 :     if (GNUNET_OK !=
     195           0 :         TALER_planchet_prepare (&pds[i].pk->key,
     196           0 :                                 &tp->planchets[i].ps,
     197           0 :                                 &tp->planchets[i].c_hash,
     198             :                                 &details[i]))
     199             :     {
     200           0 :       GNUNET_break (0);
     201           0 :       GNUNET_array_grow (tp->planchets,
     202             :                          tp->num_planchets,
     203             :                          0);
     204           0 :       GNUNET_free (tp);
     205           0 :       return NULL;
     206             :     }
     207             :   }
     208           0 :   for (unsigned int i = 0; i<num_planchets; i++)
     209             :   {
     210           0 :     tp->planchets[i].pk = *pds[i].pk;
     211           0 :     tp->planchets[i].pk.key.rsa_public_key
     212           0 :       = GNUNET_CRYPTO_rsa_public_key_dup (pds[i].pk->key.rsa_public_key);
     213             :   }
     214           0 :   tp->cb = pickup_cb;
     215           0 :   tp->cb_cls = pickup_cb_cls;
     216           0 :   tp->tpo2 = TALER_MERCHANT_tip_pickup2 (ctx,
     217             :                                          backend_url,
     218             :                                          tip_id,
     219             :                                          num_planchets,
     220             :                                          details,
     221             :                                          &pickup_done_cb,
     222             :                                          tp);
     223           0 :   if (NULL == tp->tpo2)
     224             :   {
     225           0 :     GNUNET_break (0);
     226           0 :     TALER_MERCHANT_tip_pickup_cancel (tp);
     227           0 :     return NULL;
     228             :   }
     229           0 :   return tp;
     230             : }
     231             : 
     232             : 
     233             : void
     234           0 : TALER_MERCHANT_tip_pickup_cancel (struct TALER_MERCHANT_TipPickupHandle *tp)
     235             : {
     236           0 :   for (unsigned int i = 0; i<tp->num_planchets; i++)
     237           0 :     GNUNET_CRYPTO_rsa_public_key_dup (tp->planchets[i].pk.key.rsa_public_key);
     238           0 :   GNUNET_array_grow (tp->planchets,
     239             :                      tp->num_planchets,
     240             :                      0);
     241           0 :   if (NULL != tp->tpo2)
     242             :   {
     243           0 :     TALER_MERCHANT_tip_pickup2_cancel (tp->tpo2);
     244           0 :     tp->tpo2 = NULL;
     245             :   }
     246           0 :   GNUNET_free (tp);
     247           0 : }
     248             : 
     249             : 
     250             : /* end of merchant_api_tip_pickup.c */

Generated by: LCOV version 1.14