aboutsummaryrefslogtreecommitdiff
path: root/src/lib/exchange_api_batch_withdraw.c
blob: a1fcccce4ed638243b3466cfb3ad1859da98c82a (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
447
448
449
450
451
452
453
454
455
456
/*
  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 General Public License as published by the Free Software
  Foundation; either version 3, 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 General Public License for more details.

  You should have received a copy of the GNU General Public License along with
  TALER; see the file COPYING.  If not, see
  <http://www.gnu.org/licenses/>
*/
/**
 * @file lib/exchange_api_batch_withdraw.c
 * @brief Implementation of /reserves/$RESERVE_PUB/batch-withdraw requests with blinding/unblinding
 * @author Christian Grothoff
 */
#include "platform.h"
#include <jansson.h>
#include <microhttpd.h> /* just for HTTP status codes */
#include <gnunet/gnunet_util_lib.h>
#include <gnunet/gnunet_json_lib.h>
#include <gnunet/gnunet_curl_lib.h>
#include "taler_exchange_service.h"
#include "taler_json_lib.h"
#include "exchange_api_handle.h"
#include "taler_signatures.h"
#include "exchange_api_curl_defaults.h"


/**
 * Data we keep per coin in the batch.
 */
struct CoinData
{

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

  /**
   * Master key material for the coin.
   */
  struct TALER_PlanchetMasterSecretP ps;

  /**
   * Age commitment for the coin.
   */
  const struct TALER_AgeCommitmentHash *ach;

  /**
   *  blinding secret
   */
  union TALER_DenominationBlindingKeyP bks;

  /**
   * Private key of the coin we are withdrawing.
   */
  struct TALER_CoinSpendPrivateKeyP priv;

  /**
   * Details of the planchet.
   */
  struct TALER_PlanchetDetail pd;

  /**
   * Values of the @cipher selected
   */
  struct TALER_ExchangeWithdrawValues alg_values;

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

  /**
   * Handler for the CS R request (only used for TALER_DENOMINATION_CS denominations)
   */
  struct TALER_EXCHANGE_CsRWithdrawHandle *csrh;

  /**
   * Batch withdraw this coin is part of.
   */
  struct TALER_EXCHANGE_BatchWithdrawHandle *wh;
};


/**
 * @brief A batch withdraw handle
 */
struct TALER_EXCHANGE_BatchWithdrawHandle
{

  /**
   * The curl context to use
   */
  struct GNUNET_CURL_Context *curl_ctx;

  /**
   * The base URL to the exchange
   */
  const char *exchange_url;

  /**
   * The /keys information from the exchange
   */
  const struct TALER_EXCHANGE_Keys *keys;


  /**
   * Handle for the actual (internal) batch withdraw operation.
   */
  struct TALER_EXCHANGE_BatchWithdraw2Handle *wh2;

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

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

  /**
   * Reserve private key.
   */
  const struct TALER_ReservePrivateKeyP *reserve_priv;

  /**
   * Array of per-coin data.
   */
  struct CoinData *coins;

  /**
   * Length of the @e coins array.
   */
  unsigned int num_coins;

  /**
   * Number of CS requests still pending.
   */
  unsigned int cs_pending;

};


/**
 * Function called when we're done processing the
 * HTTP /reserves/$RESERVE_PUB/batch-withdraw request.
 *
 * @param cls the `struct TALER_EXCHANGE_BatchWithdrawHandle`
 * @param bw2r response data
 */
static void
handle_reserve_batch_withdraw_finished (
  void *cls,
  const struct TALER_EXCHANGE_BatchWithdraw2Response *bw2r)
{
  struct TALER_EXCHANGE_BatchWithdrawHandle *wh = cls;
  struct TALER_EXCHANGE_BatchWithdrawResponse wr = {
    .hr = bw2r->hr
  };
  struct TALER_EXCHANGE_PrivateCoinDetails coins[GNUNET_NZL (wh->num_coins)];

  wh->wh2 = NULL;
  memset (coins,
          0,
          sizeof (coins));
  switch (bw2r->hr.http_status)
  {
  case MHD_HTTP_OK:
    {
      if (bw2r->details.ok.blind_sigs_length != wh->num_coins)
      {
        GNUNET_break_op (0);
        wr.hr.http_status = 0;
        wr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
        break;
      }
      for (unsigned int i = 0; i<wh->num_coins; i++)
      {
        struct CoinData *cd = &wh->coins[i];
        struct TALER_EXCHANGE_PrivateCoinDetails *coin = &coins[i];
        struct TALER_FreshCoin fc;

        if (GNUNET_OK !=
            TALER_planchet_to_coin (&cd->pk.key,
                                    &bw2r->details.ok.blind_sigs[i],
                                    &cd->bks,
                                    &cd->priv,
                                    cd->ach,
                                    &cd->c_hash,
                                    &cd->alg_values,
                                    &fc))
        {
          wr.hr.http_status = 0;
          wr.hr.ec = TALER_EC_EXCHANGE_WITHDRAW_UNBLIND_FAILURE;
          break;
        }
        coin->coin_priv = cd->priv;
        coin->bks = cd->bks;
        coin->sig = fc.sig;
        coin->exchange_vals = cd->alg_values;
      }
      wr.details.ok.coins = coins;
      wr.details.ok.num_coins = wh->num_coins;
      break;
    }
  case MHD_HTTP_UNAVAILABLE_FOR_LEGAL_REASONS:
    {
      struct GNUNET_JSON_Specification spec[] = {
        GNUNET_JSON_spec_fixed_auto (
          "h_payto",
          &wr.details.unavailable_for_legal_reasons.h_payto),
        GNUNET_JSON_spec_uint64 (
          "requirement_row",
          &wr.details.unavailable_for_legal_reasons.requirement_row),
        GNUNET_JSON_spec_end ()
      };

      if (GNUNET_OK !=
          GNUNET_JSON_parse (bw2r->hr.reply,
                             spec,
                             NULL, NULL))
      {
        GNUNET_break_op (0);
        wr.hr.http_status = 0;
        wr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
        break;
      }
    }
    break;
  default:
    break;
  }
  wh->cb (wh->cb_cls,
          &wr);
  for (unsigned int i = 0; i<wh->num_coins; i++)
    TALER_denom_sig_free (&coins[i].sig);
  TALER_EXCHANGE_batch_withdraw_cancel (wh);
}


/**
 * Runs phase two, the actual withdraw operation.
 * Started once the preparation for CS-denominations is
 * done.
 *
 * @param[in,out] wh batch withdraw to start phase 2 for
 */
static void
phase_two (struct TALER_EXCHANGE_BatchWithdrawHandle *wh)
{
  struct TALER_PlanchetDetail pds[wh->num_coins];

  for (unsigned int i = 0; i<wh->num_coins; i++)
  {
    struct CoinData *cd = &wh->coins[i];

    pds[i] = cd->pd;
  }
  wh->wh2 = TALER_EXCHANGE_batch_withdraw2 (
    wh->curl_ctx,
    wh->exchange_url,
    wh->keys,
    wh->reserve_priv,
    wh->num_coins,
    pds,
    &handle_reserve_batch_withdraw_finished,
    wh);
}


/**
 * Function called when stage 1 of CS withdraw is finished (request r_pub's)
 *
 * @param cls the `struct CoinData *`
 * @param csrr replies from the /csr-withdraw request
 */
static void
withdraw_cs_stage_two_callback (
  void *cls,
  const struct TALER_EXCHANGE_CsRWithdrawResponse *csrr)
{
  struct CoinData *cd = cls;
  struct TALER_EXCHANGE_BatchWithdrawHandle *wh = cd->wh;
  struct TALER_EXCHANGE_BatchWithdrawResponse wr = {
    .hr = csrr->hr
  };

  cd->csrh = NULL;
  GNUNET_assert (TALER_DENOMINATION_CS == cd->pk.key.cipher);
  switch (csrr->hr.http_status)
  {
  case MHD_HTTP_OK:
    cd->alg_values = csrr->details.ok.alg_values;
    TALER_planchet_setup_coin_priv (&cd->ps,
                                    &cd->alg_values,
                                    &cd->priv);
    TALER_planchet_blinding_secret_create (&cd->ps,
                                           &cd->alg_values,
                                           &cd->bks);
    /* This initializes the 2nd half of the
       wh->pd.blinded_planchet! */
    if (GNUNET_OK !=
        TALER_planchet_prepare (&cd->pk.key,
                                &cd->alg_values,
                                &cd->bks,
                                &cd->priv,
                                cd->ach,
                                &cd->c_hash,
                                &cd->pd))
    {
      GNUNET_break (0);
      TALER_EXCHANGE_batch_withdraw_cancel (wh);
    }
    wh->cs_pending--;
    if (0 == wh->cs_pending)
      phase_two (wh);
    return;
  default:
    break;
  }
  wh->cb (wh->cb_cls,
          &wr);
  TALER_EXCHANGE_batch_withdraw_cancel (wh);
}


struct TALER_EXCHANGE_BatchWithdrawHandle *
TALER_EXCHANGE_batch_withdraw (
  struct GNUNET_CURL_Context *curl_ctx,
  const char *exchange_url,
  const struct TALER_EXCHANGE_Keys *keys,
  const struct TALER_ReservePrivateKeyP *reserve_priv,
  unsigned int wci_length,
  const struct TALER_EXCHANGE_WithdrawCoinInput wcis[static wci_length],
  TALER_EXCHANGE_BatchWithdrawCallback res_cb,
  void *res_cb_cls)
{
  struct TALER_EXCHANGE_BatchWithdrawHandle *wh;

  wh = GNUNET_new (struct TALER_EXCHANGE_BatchWithdrawHandle);
  wh->curl_ctx = curl_ctx;
  wh->exchange_url = exchange_url;
  wh->keys = keys;
  wh->cb = res_cb;
  wh->cb_cls = res_cb_cls;
  wh->reserve_priv = reserve_priv;
  wh->num_coins = wci_length;
  wh->coins = GNUNET_new_array (wh->num_coins,
                                struct CoinData);
  for (unsigned int i = 0; i<wci_length; i++)
  {
    struct CoinData *cd = &wh->coins[i];
    const struct TALER_EXCHANGE_WithdrawCoinInput *wci = &wcis[i];

    cd->wh = wh;
    cd->ps = *wci->ps;
    cd->ach = wci->ach;
    cd->pk = *wci->pk;
    TALER_denom_pub_deep_copy (&cd->pk.key,
                               &wci->pk->key);
    switch (wci->pk->key.cipher)
    {
    case TALER_DENOMINATION_RSA:
      {
        cd->alg_values.cipher = TALER_DENOMINATION_RSA;
        TALER_planchet_setup_coin_priv (&cd->ps,
                                        &cd->alg_values,
                                        &cd->priv);
        TALER_planchet_blinding_secret_create (&cd->ps,
                                               &cd->alg_values,
                                               &cd->bks);
        if (GNUNET_OK !=
            TALER_planchet_prepare (&cd->pk.key,
                                    &cd->alg_values,
                                    &cd->bks,
                                    &cd->priv,
                                    cd->ach,
                                    &cd->c_hash,
                                    &cd->pd))
        {
          GNUNET_break (0);
          TALER_EXCHANGE_batch_withdraw_cancel (wh);
          return NULL;
        }
        break;
      }
    case TALER_DENOMINATION_CS:
      {
        TALER_cs_withdraw_nonce_derive (
          &cd->ps,
          &cd->pd.blinded_planchet.details.cs_blinded_planchet.nonce);
        /* Note that we only initialize the first half
           of the blinded_planchet here; the other part
           will be done after the /csr-withdraw request! */
        cd->pd.blinded_planchet.cipher = TALER_DENOMINATION_CS;
        cd->csrh = TALER_EXCHANGE_csr_withdraw (
          curl_ctx,
          exchange_url,
          &cd->pk,
          &cd->pd.blinded_planchet.details.cs_blinded_planchet.nonce,
          &withdraw_cs_stage_two_callback,
          cd);
        if (NULL == cd->csrh)
        {
          GNUNET_break (0);
          TALER_EXCHANGE_batch_withdraw_cancel (wh);
          return NULL;
        }
        wh->cs_pending++;
        break;
      }
    default:
      GNUNET_break (0);
      TALER_EXCHANGE_batch_withdraw_cancel (wh);
      return NULL;
    }
  }
  if (0 == wh->cs_pending)
    phase_two (wh);
  return wh;
}


void
TALER_EXCHANGE_batch_withdraw_cancel (
  struct TALER_EXCHANGE_BatchWithdrawHandle *wh)
{
  for (unsigned int i = 0; i<wh->num_coins; i++)
  {
    struct CoinData *cd = &wh->coins[i];

    if (NULL != cd->csrh)
    {
      TALER_EXCHANGE_csr_withdraw_cancel (cd->csrh);
      cd->csrh = NULL;
    }
    TALER_blinded_planchet_free (&cd->pd.blinded_planchet);
    TALER_denom_pub_free (&cd->pk.key);
  }
  GNUNET_free (wh->coins);
  if (NULL != wh->wh2)
  {
    TALER_EXCHANGE_batch_withdraw2_cancel (wh->wh2);
    wh->wh2 = NULL;
  }
  GNUNET_free (wh);
}