summaryrefslogtreecommitdiff
path: root/src/lib/merchant_api_tip_pickup.c
blob: 593efa43dca0bbe15c0a30bf7085f33a7cbc03dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
  This file is part of TALER
  Copyright (C) 2014-2022 Taler Systems SA

  TALER is free software; you can redistribute it and/or modify it under the
  terms of the GNU Lesser General Public License as published by the Free Software
  Foundation; either version 2.1, or (at your option) any later version.

  TALER is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License along with
  TALER; see the file COPYING.LGPL.  If not, see
  <http://www.gnu.org/licenses/>
*/
/**
 * @file merchant_api_tip_pickup.c
 * @brief Implementation of the /tip-pickup request of the merchant's HTTP API
 * @author Marcello Stanisci
 * @author Christian Grothoff
 */
#include "platform.h"
#include <curl/curl.h>
#include <jansson.h>
#include <microhttpd.h> /* just for HTTP status codes */
#include <gnunet/gnunet_util_lib.h>
#include <gnunet/gnunet_curl_lib.h>
#include "taler_merchant_service.h"
#include <taler/taler_json_lib.h>
#include <taler/taler_signatures.h>
#include <taler/taler_curl_lib.h>


/**
 * Data we keep per planchet.
 */
struct PlanchetData
{
  /**
   * Secrets of the planchet.
   */
  struct TALER_PlanchetMasterSecretP ps;

  /**
   * Denomination key we are withdrawing.
   */
  struct TALER_EXCHANGE_DenomPublicKey pk;

  /**
   * Hash of the public key of the coin we are signing.
   */
  struct TALER_CoinPubHashP c_hash;

  /**
   * Nonce used for @e csr request, if any.
   */
  struct TALER_CsNonce nonce;

  /**
   * Handle for a /csr request we may optionally need
   * to trigger.
   */
  struct TALER_EXCHANGE_CsRWithdrawHandle *csr;

  /**
   * Handle for the /tip-pickup operation we are part of.
   */
  struct TALER_MERCHANT_TipPickupHandle *tp;

  /**
   * Offset of this entry in the array.
   */
  unsigned int off;
};


/**
 * Handle for a /tip-pickup operation.
 */
struct TALER_MERCHANT_TipPickupHandle
{

  /**
   * Function to call with the result.
   */
  TALER_MERCHANT_TipPickupCallback cb;

  /**
   * Closure for @a cb.
   */
  void *cb_cls;

  /**
   * Handle for the actual (internal) withdraw operation.
   */
  struct TALER_MERCHANT_TipPickup2Handle *tpo2;

  /**
   * Array of length @e num_planchets.
   */
  struct PlanchetData *planchets;

  /**
   * Array of length @e num_planchets.
   */
  struct TALER_EXCHANGE_PrivateCoinDetails *pcds;

  /**
   * Context for making HTTP requests.
   */
  struct GNUNET_CURL_Context *ctx;

  /**
   * URL of the merchant backend.
   */
  char *backend_url;

  /**
   * ID of the tip we are picking up.
   */
  struct TALER_TipIdentifierP tip_id;

  /**
   * Number of planchets/coins used for this operation.
   */
  unsigned int num_planchets;

  /**
   * Number of remaining active /csr-withdraw requests.
   */
  unsigned int csr_active;
};


/**
 * Fail the pickup operation @a tp, returning @a ec.
 * Also cancels @a tp.
 *
 * @param[in] tp operation to fail
 * @param ec reason for the failure
 */
static void
fail_pickup (struct TALER_MERCHANT_TipPickupHandle *tp,
             enum TALER_ErrorCode ec)
{
  struct TALER_MERCHANT_PickupDetails pd = {
    .hr.ec = ec
  };

  tp->cb (tp->cb_cls,
          &pd);
  TALER_MERCHANT_tip_pickup_cancel (tp);
}


/**
 * Callback for a /tip-pickup request.  Returns the result of the operation.
 * Note that the client MUST still do the unblinding of the @a blind_sigs.
 *
 * @param cls closure, a `struct TALER_MERCHANT_TipPickupHandle *`
 * @param hr HTTP response details
 * @param num_blind_sigs length of the @a reserve_sigs array, 0 on error
 * @param blind_sigs array of blind signatures over the planchets, NULL on error
 */
static void
pickup_done_cb (void *cls,
                const struct TALER_MERCHANT_HttpResponse *hr,
                unsigned int num_blind_sigs,
                const struct TALER_BlindedDenominationSignature *blind_sigs)
{
  struct TALER_MERCHANT_TipPickupHandle *tp = cls;
  struct TALER_MERCHANT_PickupDetails pd = {
    .hr = *hr
  };

  tp->tpo2 = NULL;
  if (NULL == blind_sigs)
  {
    tp->cb (tp->cb_cls,
            &pd);
    TALER_MERCHANT_tip_pickup_cancel (tp);
    return;
  }
  {
    enum GNUNET_GenericReturnValue ok = GNUNET_OK;

    for (unsigned int i = 0; i<num_blind_sigs; i++)
    {
      struct TALER_EXCHANGE_PrivateCoinDetails *pcd = &tp->pcds[i];
      struct TALER_FreshCoin fc;

      if (GNUNET_OK !=
          TALER_planchet_to_coin (&tp->planchets[i].pk.key,
                                  &blind_sigs[i],
                                  &pcd->bks,
                                  &pcd->coin_priv,
                                  NULL,
                                  &tp->planchets[i].c_hash,
                                  &pcd->exchange_vals,
                                  &fc))
      {
        ok = GNUNET_SYSERR;
        break;
      }
      pcd->sig = fc.sig;
    }
    if (GNUNET_OK != ok)
    {
      struct TALER_MERCHANT_HttpResponse hrx = {
        .reply = hr->reply,
        .ec = TALER_EC_MERCHANT_TIP_PICKUP_UNBLIND_FAILURE
      };

      pd.hr = hrx;
      tp->cb (tp->cb_cls,
              &pd);
    }
    else
    {
      pd.details.success.num_sigs = num_blind_sigs;
      pd.details.success.pcds = tp->pcds;
      tp->cb (tp->cb_cls,
              &pd);
    }
  }
  TALER_MERCHANT_tip_pickup_cancel (tp);
}


/**
 * We have obtained all of the exchange inputs. Continue the pickup.
 *
 * @param[in,out] tp operation to continue
 */
static void
pickup_post_csr (struct TALER_MERCHANT_TipPickupHandle *tp)
{
  struct TALER_PlanchetDetail details[tp->num_planchets];

  for (unsigned int i = 0; i<tp->num_planchets; i++)
  {
    const struct PlanchetData *pd = &tp->planchets[i];
    struct TALER_EXCHANGE_PrivateCoinDetails *pcd = &tp->pcds[i];

    TALER_planchet_setup_coin_priv (&pd->ps,
                                    &pcd->exchange_vals,
                                    &pcd->coin_priv);
    TALER_planchet_blinding_secret_create (&pd->ps,
                                           &pcd->exchange_vals,
                                           &pcd->bks);
    if (TALER_DENOMINATION_CS == pcd->exchange_vals.cipher)
    {
      details[i].blinded_planchet.details.cs_blinded_planchet.nonce
        = pd->nonce;
    }
    if (GNUNET_OK !=
        TALER_planchet_prepare (&pd->pk.key,
                                &pcd->exchange_vals,
                                &pcd->bks,
                                &pcd->coin_priv,
                                NULL,
                                &tp->planchets[i].c_hash,
                                &details[i]))
    {
      GNUNET_break (0);
      for (unsigned int j = 0; j<i; j++)
        TALER_planchet_detail_free (&details[j]);
      fail_pickup (tp,
                   TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE);
      return;
    }
  }
  tp->tpo2 = TALER_MERCHANT_tip_pickup2 (tp->ctx,
                                         tp->backend_url,
                                         &tp->tip_id,
                                         tp->num_planchets,
                                         details,
                                         &pickup_done_cb,
                                         tp);
  for (unsigned int j = 0; j<tp->num_planchets; j++)
    TALER_planchet_detail_free (&details[j]);
  if (NULL == tp->tpo2)
  {
    GNUNET_break (0);
    fail_pickup (tp,
                 TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE);
    return;
  }
}


/**
 * Callbacks of this type are used to serve the result of submitting a
 * CS R request to a exchange.
 *
 * @param cls a `struct TALER_MERCHANT_TipPickupHandle`
 * @param csrr response details
 */
static void
csr_cb (void *cls,
        const struct TALER_EXCHANGE_CsRWithdrawResponse *csrr)
{
  struct PlanchetData *pd = cls;
  struct TALER_MERCHANT_TipPickupHandle *tp = pd->tp;

  pd->csr = NULL;
  tp->csr_active--;
  switch (csrr->hr.http_status)
  {
  case MHD_HTTP_OK:
    {
      struct TALER_EXCHANGE_PrivateCoinDetails *pcd = &tp->pcds[pd->off];

      pcd->exchange_vals = csrr->details.success.alg_values;
    }
    if (0 != tp->csr_active)
      return;
    pickup_post_csr (tp);
    return;
  default:
    {
      struct TALER_MERCHANT_PickupDetails pd = {
        .hr.hint = "/csr-withdraw failed",
        .hr.exchange_http_status = csrr->hr.http_status
      };

      tp->cb (tp->cb_cls,
              &pd);
      TALER_MERCHANT_tip_pickup_cancel (tp);
      return;
    }
  }
}


struct TALER_MERCHANT_TipPickupHandle *
TALER_MERCHANT_tip_pickup (struct GNUNET_CURL_Context *ctx,
                           struct TALER_EXCHANGE_Handle *exchange,
                           const char *backend_url,
                           const struct TALER_TipIdentifierP *tip_id,
                           unsigned int num_planchets,
                           const struct TALER_MERCHANT_PlanchetData *pds,
                           TALER_MERCHANT_TipPickupCallback pickup_cb,
                           void *pickup_cb_cls)
{
  struct TALER_MERCHANT_TipPickupHandle *tp;

  if (0 == num_planchets)
  {
    GNUNET_break (0);
    return NULL;
  }
  tp = GNUNET_new (struct TALER_MERCHANT_TipPickupHandle);
  tp->cb = pickup_cb;
  tp->cb_cls = pickup_cb_cls;
  tp->ctx = ctx;
  tp->backend_url = GNUNET_strdup (backend_url);
  tp->tip_id = *tip_id;
  tp->num_planchets = num_planchets;
  tp->planchets = GNUNET_new_array (num_planchets,
                                    struct PlanchetData);
  tp->pcds = GNUNET_new_array (num_planchets,
                               struct TALER_EXCHANGE_PrivateCoinDetails);
  for (unsigned int i = 0; i<num_planchets; i++)
  {
    const struct TALER_MERCHANT_PlanchetData *mpd = &pds[i];
    const struct TALER_EXCHANGE_DenomPublicKey *pk = mpd->pk;
    struct TALER_EXCHANGE_PrivateCoinDetails *pcd = &tp->pcds[i];
    struct PlanchetData *pd = &tp->planchets[i];

    pd->off = i;
    pd->tp = tp;
    tp->planchets[i].ps = mpd->ps;
    tp->planchets[i].pk = *pds[i].pk;
    TALER_denom_pub_deep_copy (&tp->planchets[i].pk.key,
                               &pds[i].pk->key);
    switch (pk->key.cipher)
    {
    case TALER_DENOMINATION_RSA:
      pcd->exchange_vals.cipher = TALER_DENOMINATION_RSA;
      break;
    case TALER_DENOMINATION_CS:
      {
        TALER_cs_withdraw_nonce_derive (&pd->ps,
                                        &pd->nonce);
        pd->csr = TALER_EXCHANGE_csr_withdraw (exchange,
                                               &pd->pk,
                                               &pd->nonce,
                                               &csr_cb,
                                               pd);
        if (NULL == pd->csr)
        {
          GNUNET_break (0);
          TALER_MERCHANT_tip_pickup_cancel (tp);
          return NULL;
        }
        tp->csr_active++;
        break;
      }
    default:
      GNUNET_break (0);
      TALER_MERCHANT_tip_pickup_cancel (tp);
      return NULL;
    }
  }
  if (0 == tp->csr_active)
  {
    pickup_post_csr (tp);
    return tp;
  }
  return tp;
}


void
TALER_MERCHANT_tip_pickup_cancel (struct TALER_MERCHANT_TipPickupHandle *tp)
{
  for (unsigned int i = 0; i<tp->num_planchets; i++)
  {
    struct TALER_EXCHANGE_PrivateCoinDetails *pcd = &tp->pcds[i];
    struct PlanchetData *pd = &tp->planchets[i];

    TALER_denom_sig_free (&pcd->sig);
    TALER_denom_pub_free (&tp->planchets[i].pk.key);
    if (NULL != pd->csr)
    {
      TALER_EXCHANGE_csr_withdraw_cancel (pd->csr);
      pd->csr = NULL;
    }
  }
  GNUNET_array_grow (tp->planchets,
                     tp->num_planchets,
                     0);
  if (NULL != tp->tpo2)
  {
    TALER_MERCHANT_tip_pickup2_cancel (tp->tpo2);
    tp->tpo2 = NULL;
  }
  GNUNET_free (tp->backend_url);
  GNUNET_free (tp->pcds);
  GNUNET_free (tp);
}


/* end of merchant_api_tip_pickup.c */