summaryrefslogtreecommitdiff
path: root/src/lib/exchange_api_age_withdraw_reveal.c
blob: cade528d20284fa9d1d5f09569ba10149299d9e7 (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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
  This file is part of TALER
  Copyright (C) 2023 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_age_withdraw_reveal.c
 * @brief Implementation of /age-withdraw/$ACH/reveal requests
 * @author Özgür Kesim
 */

#include "platform.h"
#include <gnunet/gnunet_common.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_curl_lib.h"
#include "taler_json_lib.h"
#include "taler_exchange_service.h"
#include "exchange_api_common.h"
#include "exchange_api_handle.h"
#include "taler_signatures.h"
#include "exchange_api_curl_defaults.h"

/**
 * Handler for a running age-withdraw-reveal  request
 */
struct TALER_EXCHANGE_AgeWithdrawRevealHandle
{

  /* The index not to be disclosed */
  uint8_t noreveal_index;

  /* The age-withdraw commitment */
  struct TALER_AgeWithdrawCommitmentHashP h_commitment;

  /* The reserve's public key */
  const struct TALER_ReservePublicKeyP *reserve_pub;

  /* Number of coins */
  size_t num_coins;

  /* The @e num_coins * kappa coin secrets from the age-withdraw commitment */
  const struct TALER_EXCHANGE_AgeWithdrawCoinInput *coins_input;

  /* The url for the reveal request */
  char *request_url;

  /**
   * CURL handle for the request job.
   */
  struct GNUNET_CURL_Job *job;

  /**
   * Post Context
   */
  struct TALER_CURL_PostContext post_ctx;

  /* Callback */
  TALER_EXCHANGE_AgeWithdrawRevealCallback callback;

  /* Reveal */
  void *callback_cls;
};


/**
 * We got a 200 OK response for the /age-withdraw/$ACH/reveal operation.
 * Extract the signed blindedcoins and return it to the caller.
 *
 * @param awrh operation handle
 * @param j_response reply from the exchange
 * @return #GNUNET_OK on success, #GNUNET_SYSERR on errors
 */
static enum GNUNET_GenericReturnValue
age_withdraw_reveal_ok (
  struct TALER_EXCHANGE_AgeWithdrawRevealHandle *awrh,
  const json_t *j_response)
{
  struct TALER_EXCHANGE_AgeWithdrawRevealResponse response = {
    .hr.reply = j_response,
    .hr.http_status = MHD_HTTP_OK,
  };
  const json_t *j_sigs;
  struct GNUNET_JSON_Specification spec[] = {
    GNUNET_JSON_spec_array_const ("ev_sigs",
                                  &j_sigs),
    GNUNET_JSON_spec_end ()
  };

  if (GNUNET_OK != GNUNET_JSON_parse (j_response,
                                      spec,
                                      NULL, NULL))
  {
    GNUNET_break_op (0);
    return GNUNET_SYSERR;
  }

  if (awrh->num_coins != json_array_size (j_sigs))
  {
    /* Number of coins generated does not match our expectation */
    GNUNET_break_op (0);
    return GNUNET_SYSERR;
  }

  {
    struct TALER_BlindedDenominationSignature denom_sigs[awrh->num_coins];
    json_t *j_sig;
    size_t n;

    /* Reconstruct the coins and unblind the signatures */
    json_array_foreach (j_sigs, n, j_sig)
    {
      struct GNUNET_JSON_Specification spec[] = {
        TALER_JSON_spec_blinded_denom_sig (NULL,
                                           &denom_sigs[n]),
        GNUNET_JSON_spec_end ()
      };

      if (GNUNET_OK != GNUNET_JSON_parse (j_sig,
                                          spec,
                                          NULL, NULL))
      {
        GNUNET_break_op (0);
        return GNUNET_SYSERR;
      }

    }

    response.details.ok.num_sigs = awrh->num_coins;
    response.details.ok.blinded_denom_sigs = denom_sigs;
    awrh->callback (awrh->callback_cls,
                    &response);
    /* Make sure the callback isn't called again */
    awrh->callback = NULL;
    /* Free resources */
    for (size_t i = 0; i < awrh->num_coins; i++)
      TALER_blinded_denom_sig_free (&denom_sigs[i]);
  }

  return GNUNET_OK;
}


/**
 * Function called when we're done processing the
 * HTTP /age-withdraw/$ACH/reveal request.
 *
 * @param cls the `struct TALER_EXCHANGE_AgeWithdrawRevealHandle`
 * @param response_code The HTTP response code
 * @param response response data
 */
static void
handle_age_withdraw_reveal_finished (
  void *cls,
  long response_code,
  const void *response)
{
  struct TALER_EXCHANGE_AgeWithdrawRevealHandle *awrh = cls;
  const json_t *j_response = response;
  struct TALER_EXCHANGE_AgeWithdrawRevealResponse awr = {
    .hr.reply = j_response,
    .hr.http_status = (unsigned int) response_code
  };

  awrh->job = NULL;
  /* FIXME[oec]: Only handle response-codes that are in the spec */
  switch (response_code)
  {
  case 0:
    awr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
    break;
  case MHD_HTTP_OK:
    {
      enum GNUNET_GenericReturnValue ret;

      ret = age_withdraw_reveal_ok (awrh,
                                    j_response);
      if (GNUNET_OK != ret)
      {
        GNUNET_break_op (0);
        awr.hr.http_status = 0;
        awr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
        break;
      }
      GNUNET_assert (NULL == awrh->callback);
      TALER_EXCHANGE_age_withdraw_reveal_cancel (awrh);
      return;
    }
  case MHD_HTTP_UNAVAILABLE_FOR_LEGAL_REASONS:
    /* only validate reply is well-formed */
    {
      uint64_t ptu;
      struct GNUNET_JSON_Specification spec[] = {
        GNUNET_JSON_spec_uint64 ("legitimization_uuid",
                                 &ptu),
        GNUNET_JSON_spec_end ()
      };

      if (GNUNET_OK !=
          GNUNET_JSON_parse (j_response,
                             spec,
                             NULL, NULL))
      {
        GNUNET_break_op (0);
        awr.hr.http_status = 0;
        awr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
        break;
      }
    }
  case MHD_HTTP_BAD_REQUEST:
    /* This should never happen, either us or the exchange is buggy
       (or API version conflict); just pass JSON reply to the application */
    awr.hr.ec = TALER_JSON_get_error_code (j_response);
    awr.hr.hint = TALER_JSON_get_error_hint (j_response);
    break;
  case MHD_HTTP_FORBIDDEN:
    GNUNET_break_op (0);
    /**
     * This should never happen, as we don't sent any signatures
     * to the exchange to verify.  We should simply pass the JSON reply
     * to the application
     **/
    awr.hr.ec = TALER_JSON_get_error_code (j_response);
    awr.hr.hint = TALER_JSON_get_error_hint (j_response);
    break;
  case MHD_HTTP_NOT_FOUND:
    /* Nothing really to verify, the exchange basically just says
       that it doesn't know this age-withdraw commitment. */
    awr.hr.ec = TALER_JSON_get_error_code (j_response);
    awr.hr.hint = TALER_JSON_get_error_hint (j_response);
    break;
  case MHD_HTTP_CONFLICT:
    /* An age commitment for one of the coins did not fulfill
     * the required maximum age requirement of the corresponding
     * reserve.
     * Error code: TALER_EC_EXCHANGE_GENERIC_COIN_AGE_REQUIREMENT_FAILURE.
     */
    awr.hr.ec = TALER_JSON_get_error_code (j_response);
    awr.hr.hint = TALER_JSON_get_error_hint (j_response);
    break;
  case MHD_HTTP_GONE:
    /* could happen if denomination was revoked */
    /* Note: one might want to check /keys for revocation
       signature here, alas tricky in case our /keys
       is outdated => left to clients */
    awr.hr.ec = TALER_JSON_get_error_code (j_response);
    awr.hr.hint = TALER_JSON_get_error_hint (j_response);
    break;
  case MHD_HTTP_INTERNAL_SERVER_ERROR:
    /* Server had an internal issue; we should retry, but this API
       leaves this to the application */
    awr.hr.ec = TALER_JSON_get_error_code (j_response);
    awr.hr.hint = TALER_JSON_get_error_hint (j_response);
    break;
  default:
    /* unexpected response code */
    GNUNET_break_op (0);
    awr.hr.ec = TALER_JSON_get_error_code (j_response);
    awr.hr.hint = TALER_JSON_get_error_hint (j_response);
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Unexpected response code %u/%d for exchange age-withdraw\n",
                (unsigned int) response_code,
                (int) awr.hr.ec);
    break;
  }
  awrh->callback (awrh->callback_cls,
                  &awr);
  TALER_EXCHANGE_age_withdraw_reveal_cancel (awrh);
}


/**
 * Prepares the request URL for the age-withdraw-reveal request
 *
 * @param exchange_url The base-URL to the exchange
 * @param[in,out] awrh The handler
 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
 */
static
enum GNUNET_GenericReturnValue
prepare_url (
  const char *exchange_url,
  struct TALER_EXCHANGE_AgeWithdrawRevealHandle *awrh)
{
  char arg_str[sizeof (struct TALER_AgeWithdrawCommitmentHashP) * 2 + 32];
  char pub_str[sizeof (struct TALER_AgeWithdrawCommitmentHashP) * 2];
  char *end;

  end = GNUNET_STRINGS_data_to_string (&awrh->h_commitment,
                                       sizeof (awrh->h_commitment),
                                       pub_str,
                                       sizeof (pub_str));
  *end = '\0';
  GNUNET_snprintf (arg_str,
                   sizeof (arg_str),
                   "age-withdraw/%s/reveal",
                   pub_str);

  awrh->request_url = TALER_url_join (exchange_url,
                                      arg_str,
                                      NULL);
  if (NULL == awrh->request_url)
  {
    GNUNET_break (0);
    TALER_EXCHANGE_age_withdraw_reveal_cancel (awrh);
    return GNUNET_SYSERR;
  }

  return GNUNET_OK;
}


/**
 * Call /age-withdraw/$ACH/reveal
 *
 * @param curl_ctx The context for CURL
 * @param awrh The handler
 */
static
void
perform_protocol (
  struct GNUNET_CURL_Context *curl_ctx,
  struct TALER_EXCHANGE_AgeWithdrawRevealHandle *awrh)
{
  CURL *curlh = NULL;
  json_t *j_request_body = NULL;
  json_t *j_array_of_secrets = NULL;
  json_t *j_secrets = NULL;
  json_t *j_sec = NULL;

#define FAIL_IF(cond) \
        do { \
          if ((cond)) \
          { \
            GNUNET_break (! (cond)); \
            goto ERROR; \
          } \
        } while (0)

  j_array_of_secrets = json_array ();
  FAIL_IF (NULL == j_array_of_secrets);

  for (size_t n = 0; n < awrh->num_coins; n++)
  {
    const struct TALER_PlanchetMasterSecretP *secrets =
      awrh->coins_input[n].secrets;

    j_secrets = json_array ();
    FAIL_IF (NULL == j_secrets);

    for (uint8_t k = 0; k < TALER_CNC_KAPPA; k++)
    {
      const struct TALER_PlanchetMasterSecretP *secret = &secrets[k];
      if (awrh->noreveal_index == k)
        continue;

      j_sec = GNUNET_JSON_PACK (
        GNUNET_JSON_pack_data_auto (NULL, secret));

      FAIL_IF (NULL == j_sec);
      FAIL_IF (0 < json_array_append_new (j_secrets,
                                          j_sec));
    }

    FAIL_IF (0 < json_array_append_new (j_array_of_secrets,
                                        j_secrets));
  }
  j_request_body = GNUNET_JSON_PACK (
    GNUNET_JSON_pack_data_auto ("reserve_pub",
                                awrh->reserve_pub),
    GNUNET_JSON_pack_array_steal ("disclosed_coin_secrets",
                                  j_array_of_secrets));
  FAIL_IF (NULL == j_request_body);

  curlh = TALER_EXCHANGE_curl_easy_get_ (awrh->request_url);
  FAIL_IF (NULL == curlh);
  FAIL_IF (GNUNET_OK !=
           TALER_curl_easy_post (&awrh->post_ctx,
                                 curlh,
                                 j_request_body));
  json_decref (j_request_body);
  j_request_body = NULL;

  awrh->job = GNUNET_CURL_job_add2 (curl_ctx,
                                    curlh,
                                    awrh->post_ctx.headers,
                                    &handle_age_withdraw_reveal_finished,
                                    awrh);
  FAIL_IF (NULL == awrh->job);

  /* No error, return */
  return;

ERROR:
  if (NULL != j_sec)
    json_decref (j_sec);
  if (NULL != j_secrets)
    json_decref (j_secrets);
  if (NULL != j_array_of_secrets)
    json_decref (j_array_of_secrets);
  if (NULL != j_request_body)
    json_decref (j_request_body);
  if (NULL != curlh)
    curl_easy_cleanup (curlh);
  TALER_EXCHANGE_age_withdraw_reveal_cancel (awrh);
  return;
#undef FAIL_IF
}


struct TALER_EXCHANGE_AgeWithdrawRevealHandle *
TALER_EXCHANGE_age_withdraw_reveal (
  struct GNUNET_CURL_Context *curl_ctx,
  const char *exchange_url,
  size_t num_coins,
  const struct TALER_EXCHANGE_AgeWithdrawCoinInput coins_input[static
                                                               num_coins],
  uint8_t noreveal_index,
  const struct TALER_AgeWithdrawCommitmentHashP *h_commitment,
  const struct TALER_ReservePublicKeyP *reserve_pub,
  TALER_EXCHANGE_AgeWithdrawRevealCallback reveal_cb,
  void *reveal_cb_cls)
{
  struct TALER_EXCHANGE_AgeWithdrawRevealHandle *awrh =
    GNUNET_new (struct TALER_EXCHANGE_AgeWithdrawRevealHandle);
  awrh->noreveal_index = noreveal_index;
  awrh->h_commitment = *h_commitment;
  awrh->num_coins = num_coins;
  awrh->coins_input = coins_input;
  awrh->callback = reveal_cb;
  awrh->callback_cls = reveal_cb_cls;
  awrh->reserve_pub = reserve_pub;

  if (GNUNET_OK !=
      prepare_url (exchange_url,
                   awrh))
    return NULL;

  perform_protocol (curl_ctx, awrh);

  return awrh;
}


void
TALER_EXCHANGE_age_withdraw_reveal_cancel (
  struct TALER_EXCHANGE_AgeWithdrawRevealHandle *awrh)
{
  if (NULL != awrh->job)
  {
    GNUNET_CURL_job_cancel (awrh->job);
    awrh->job = NULL;
  }
  TALER_curl_easy_post_finished (&awrh->post_ctx);

  if (NULL != awrh->request_url)
    GNUNET_free (awrh->request_url);

  GNUNET_free (awrh);
}


/* exchange_api_age_withdraw_reveal.c */