Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2025 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/exchange_api_post-melt.c
19 : * @brief Implementation of the /melt request
20 : * @author Özgür Kesim
21 : */
22 : #include <jansson.h>
23 : #include <microhttpd.h> /* just for HTTP status codes */
24 : #include <gnunet/gnunet_util_lib.h>
25 : #include <gnunet/gnunet_json_lib.h>
26 : #include <gnunet/gnunet_curl_lib.h>
27 : #include "taler/taler_json_lib.h"
28 : #include "exchange_api_common.h"
29 : #include "exchange_api_handle.h"
30 : #include "taler/taler_signatures.h"
31 : #include "exchange_api_curl_defaults.h"
32 : #include "exchange_api_refresh_common.h"
33 :
34 :
35 : /**
36 : * @brief A /melt Handle
37 : */
38 : struct TALER_EXCHANGE_PostMeltHandle
39 : {
40 :
41 : /**
42 : * The keys of the this request handle will use
43 : */
44 : struct TALER_EXCHANGE_Keys *keys;
45 :
46 : /**
47 : * Hash of the denomination of the melted coin, filled by
48 : * #melt_coin_lookup() when checking a conflict reply.
49 : */
50 : struct TALER_DenominationHashP h_melt_denom_pub;
51 :
52 : /**
53 : * The url for this request.
54 : */
55 : char *url;
56 :
57 : /**
58 : * The exchange base url.
59 : */
60 : char *exchange_url;
61 :
62 : /**
63 : * Curl context.
64 : */
65 : struct GNUNET_CURL_Context *cctx;
66 :
67 : /**
68 : * Context for #TEH_curl_easy_post(). Keeps the data that must
69 : * persist for Curl to make the upload.
70 : */
71 : struct TALER_CURL_PostContext ctx;
72 :
73 : /**
74 : * Handle for the request.
75 : */
76 : struct GNUNET_CURL_Job *job;
77 :
78 : /**
79 : * Function to call with refresh melt failure results.
80 : */
81 : TALER_EXCHANGE_PostMeltCallback melt_cb;
82 :
83 : /**
84 : * Closure for @e result_cb and @e melt_failure_cb.
85 : */
86 : void *melt_cb_cls;
87 :
88 : /**
89 : * Actual information about the melt operation.
90 : */
91 : struct MeltData md;
92 :
93 : /**
94 : * The seed for the melt operation.
95 : */
96 : struct TALER_PublicRefreshMasterSeedP rms;
97 :
98 : /**
99 : * Details about the characteristics of the requested melt operation.
100 : */
101 : const struct TALER_EXCHANGE_MeltInput *rd;
102 :
103 : /**
104 : * True, if no blinding_seed is needed (no CS denominations involved)
105 : */
106 : bool no_blinding_seed;
107 :
108 : /**
109 : * If @e no_blinding_seed is false, the blinding seed for the intermediate
110 : * call to /blinding-prepare, in order to retrieve the R-values from the
111 : * exchange for the blind Clause-Schnorr signature.
112 : */
113 : struct TALER_BlindingMasterSeedP blinding_seed;
114 :
115 : /**
116 : * Array of `num_fresh_denom_pubs` per-coin values
117 : * returned from melt operation.
118 : */
119 : struct TALER_ExchangeBlindingValues *melt_blinding_values;
120 :
121 : /**
122 : * Handle for the preflight request, or NULL.
123 : */
124 : struct TALER_EXCHANGE_PostBlindingPrepareHandle *bpr;
125 :
126 : /**
127 : * Public key of the coin being melted.
128 : */
129 : struct TALER_CoinSpendPublicKeyP coin_pub;
130 :
131 : /**
132 : * Signature affirming the melt.
133 : */
134 : struct TALER_CoinSpendSignatureP coin_sig;
135 :
136 : /**
137 : * @brief Public information about the coin's denomination key
138 : */
139 : const struct TALER_EXCHANGE_DenomPublicKey *dki;
140 :
141 : /**
142 : * Gamma value chosen by the exchange during melt.
143 : */
144 : uint32_t noreveal_index;
145 :
146 : };
147 :
148 :
149 : /**
150 : * Verify that the signature on the "200 OK" response
151 : * from the exchange is valid.
152 : *
153 : * @param[in,out] mh melt handle
154 : * @param json json reply with the signature
155 : * @param[out] exchange_pub public key of the exchange used for the signature
156 : * @return #GNUNET_OK if the signature is valid, #GNUNET_SYSERR if not
157 : */
158 : static enum GNUNET_GenericReturnValue
159 44 : verify_melt_signature_ok (struct TALER_EXCHANGE_PostMeltHandle *mh,
160 : const json_t *json,
161 : struct TALER_ExchangePublicKeyP *exchange_pub)
162 : {
163 : struct TALER_ExchangeSignatureP exchange_sig;
164 : struct GNUNET_JSON_Specification spec[] = {
165 44 : GNUNET_JSON_spec_fixed_auto ("exchange_sig",
166 : &exchange_sig),
167 44 : GNUNET_JSON_spec_fixed_auto ("exchange_pub",
168 : exchange_pub),
169 44 : GNUNET_JSON_spec_uint32 ("noreveal_index",
170 : &mh->noreveal_index),
171 44 : GNUNET_JSON_spec_end ()
172 : };
173 :
174 44 : if (GNUNET_OK !=
175 44 : GNUNET_JSON_parse (json,
176 : spec,
177 : NULL, NULL))
178 : {
179 0 : GNUNET_break_op (0);
180 0 : return GNUNET_SYSERR;
181 : }
182 : /* check that exchange signing key is permitted */
183 44 : if (GNUNET_OK !=
184 44 : TALER_EXCHANGE_test_signing_key (mh->keys,
185 : exchange_pub))
186 : {
187 0 : GNUNET_break_op (0);
188 0 : return GNUNET_SYSERR;
189 : }
190 :
191 : /* check that noreveal index is in permitted range */
192 44 : if (TALER_CNC_KAPPA <= mh->noreveal_index)
193 : {
194 0 : GNUNET_break_op (0);
195 0 : return GNUNET_SYSERR;
196 : }
197 :
198 44 : if (GNUNET_OK !=
199 44 : TALER_exchange_online_melt_confirmation_verify (
200 44 : &mh->md.rc,
201 : mh->noreveal_index,
202 : exchange_pub,
203 : &exchange_sig))
204 : {
205 0 : GNUNET_break_op (0);
206 0 : return GNUNET_SYSERR;
207 : }
208 44 : return GNUNET_OK;
209 : }
210 :
211 :
212 : /**
213 : * Check that @a coin_pub is the melted coin and report how we used it.
214 : *
215 : * @param cls a `struct TALER_EXCHANGE_PostMeltHandle *`
216 : * @param coin_pub public key of the coin named in the conflict reply
217 : * @param[out] h_denom_pub set to the hash of the denomination we used
218 : * @param[out] h_age_commitment set to the age commitment hash we used, or NULL
219 : * @return #GNUNET_OK if found, #GNUNET_NO if the coin is not ours
220 : */
221 : static enum GNUNET_GenericReturnValue
222 2 : melt_coin_lookup (void *cls,
223 : const struct TALER_CoinSpendPublicKeyP *coin_pub,
224 : const struct TALER_DenominationHashP **h_denom_pub,
225 : const struct TALER_AgeCommitmentHashP **h_age_commitment)
226 : {
227 2 : struct TALER_EXCHANGE_PostMeltHandle *mh = cls;
228 :
229 : struct TALER_CoinSpendPublicKeyP my_pub;
230 :
231 2 : GNUNET_CRYPTO_eddsa_key_get_public (
232 2 : &mh->md.melted_coin.coin_priv.eddsa_priv,
233 : &my_pub.eddsa_pub);
234 2 : if (0 != GNUNET_memcmp (coin_pub,
235 : &my_pub))
236 0 : return GNUNET_NO;
237 2 : TALER_denom_pub_hash (&mh->md.melted_coin.pub_key,
238 : &mh->h_melt_denom_pub);
239 2 : *h_denom_pub = &mh->h_melt_denom_pub;
240 2 : *h_age_commitment = mh->md.melted_coin.h_age_commitment;
241 2 : return GNUNET_OK;
242 : }
243 :
244 :
245 : /**
246 : * Function called when we're done processing the
247 : * HTTP /melt request.
248 : *
249 : * @param cls the `struct TALER_EXCHANGE_MeltHandle`
250 : * @param response_code HTTP response code, 0 on error
251 : * @param response parsed JSON result, NULL on error
252 : */
253 : static void
254 61 : handle_melt_finished (void *cls,
255 : long response_code,
256 : const void *response)
257 : {
258 61 : struct TALER_EXCHANGE_PostMeltHandle *mh = cls;
259 61 : const json_t *j = response;
260 61 : struct TALER_EXCHANGE_PostMeltResponse mr = {
261 : .hr.reply = j,
262 61 : .hr.http_status = (unsigned int) response_code
263 : };
264 :
265 61 : mh->job = NULL;
266 61 : switch (response_code)
267 : {
268 0 : case 0:
269 0 : mr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
270 0 : break;
271 44 : case MHD_HTTP_OK:
272 44 : if (GNUNET_OK !=
273 44 : verify_melt_signature_ok (mh,
274 : j,
275 : &mr.details.ok.sign_key))
276 : {
277 0 : GNUNET_break_op (0);
278 0 : mr.hr.http_status = 0;
279 0 : mr.hr.ec = TALER_EC_EXCHANGE_MELT_INVALID_SIGNATURE_BY_EXCHANGE;
280 0 : break;
281 : }
282 44 : mr.details.ok.noreveal_index = mh->noreveal_index;
283 44 : mr.details.ok.rc = mh->md.rc;
284 44 : mr.details.ok.rc_version = mh->md.rc_version;
285 44 : mr.details.ok.num_melt_blinding_values = mh->rd->num_fresh_denom_pubs;
286 44 : mr.details.ok.melt_blinding_values = mh->melt_blinding_values;
287 88 : mr.details.ok.blinding_seed = mh->no_blinding_seed
288 : ? NULL
289 44 : : &mh->blinding_seed;
290 44 : mh->melt_cb (mh->melt_cb_cls,
291 : &mr);
292 44 : mh->melt_cb = NULL;
293 44 : break;
294 0 : case MHD_HTTP_BAD_REQUEST:
295 : /* This should never happen, either us or the exchange is buggy
296 : (or API version conflict); just pass JSON reply to the application */
297 0 : mr.hr.ec = TALER_JSON_get_error_code (j);
298 0 : mr.hr.hint = TALER_JSON_get_error_hint (j);
299 0 : break;
300 17 : case MHD_HTTP_CONFLICT:
301 17 : mr.hr.ec = TALER_JSON_get_error_code (j);
302 17 : mr.hr.hint = TALER_JSON_get_error_hint (j);
303 17 : switch (mr.hr.ec)
304 : {
305 2 : case TALER_EC_EXCHANGE_GENERIC_COIN_CONFLICTING_DENOMINATION_KEY:
306 : case TALER_EC_EXCHANGE_GENERIC_COIN_CONFLICTING_AGE_HASH:
307 2 : if (GNUNET_OK !=
308 2 : TALER_EXCHANGE_check_coin_conflict_ (mh->keys,
309 : mr.hr.ec,
310 : j,
311 : &melt_coin_lookup,
312 : mh,
313 : &mr.details.conflict))
314 : {
315 0 : GNUNET_break_op (0);
316 0 : mr.hr.http_status = 0;
317 0 : mr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
318 : }
319 2 : break;
320 15 : default:
321 : /* insufficient funds: proof is the coin history, checked by
322 : the application via GET /coins/$COIN_PUB/history */
323 15 : break;
324 : }
325 17 : break;
326 0 : case MHD_HTTP_FORBIDDEN:
327 : /* Nothing really to verify, exchange says one of the signatures is
328 : invalid; assuming we checked them, this should never happen, we
329 : should pass the JSON reply to the application */
330 0 : mr.hr.ec = TALER_JSON_get_error_code (j);
331 0 : mr.hr.hint = TALER_JSON_get_error_hint (j);
332 0 : break;
333 0 : case MHD_HTTP_NOT_FOUND:
334 : /* Nothing really to verify, this should never
335 : happen, we should pass the JSON reply to the application */
336 0 : mr.hr.ec = TALER_JSON_get_error_code (j);
337 0 : mr.hr.hint = TALER_JSON_get_error_hint (j);
338 0 : break;
339 0 : case MHD_HTTP_INTERNAL_SERVER_ERROR:
340 : /* Server had an internal issue; we should retry, but this API
341 : leaves this to the application */
342 0 : mr.hr.ec = TALER_JSON_get_error_code (j);
343 0 : mr.hr.hint = TALER_JSON_get_error_hint (j);
344 0 : break;
345 0 : default:
346 : /* unexpected response code */
347 0 : mr.hr.ec = TALER_JSON_get_error_code (j);
348 0 : mr.hr.hint = TALER_JSON_get_error_hint (j);
349 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
350 : "Unexpected response code %u/%d for exchange melt\n",
351 : (unsigned int) response_code,
352 : mr.hr.ec);
353 0 : GNUNET_break_op (0);
354 0 : break;
355 : }
356 61 : if (NULL != mh->melt_cb)
357 17 : mh->melt_cb (mh->melt_cb_cls,
358 : &mr);
359 61 : TALER_EXCHANGE_free_coin_conflict_ (&mr.hr,
360 : &mr.details.conflict);
361 61 : TALER_EXCHANGE_post_melt_cancel (mh);
362 61 : }
363 :
364 :
365 : /**
366 : * Start the actual melt operation, now that we have
367 : * the exchange's input values.
368 : *
369 : * @param[in,out] mh melt operation to run
370 : * @return #GNUNET_OK if we could start the operation
371 : */
372 : static enum GNUNET_GenericReturnValue
373 61 : start_melt (struct TALER_EXCHANGE_PostMeltHandle *mh)
374 : {
375 : json_t *j_request_body;
376 : json_t *j_transfer_pubs;
377 : json_t *j_coin_evs;
378 : CURL *eh;
379 : struct TALER_DenominationHashP h_denom_pub;
380 :
381 61 : if (GNUNET_OK !=
382 61 : TALER_EXCHANGE_get_melt_data (&mh->rms,
383 : mh->rd,
384 61 : mh->no_blinding_seed
385 : ? NULL
386 : : &mh->blinding_seed,
387 61 : mh->melt_blinding_values,
388 61 : mh->rd->rc_version,
389 : &mh->md))
390 : {
391 0 : GNUNET_break (0);
392 0 : return GNUNET_SYSERR;
393 : }
394 61 : TALER_denom_pub_hash (
395 61 : &mh->md.melted_coin.pub_key,
396 : &h_denom_pub);
397 61 : TALER_wallet_melt_sign (
398 61 : &mh->md.melted_coin.melt_amount_with_fee,
399 61 : &mh->md.melted_coin.fee_melt,
400 61 : &mh->md.rc,
401 : &h_denom_pub,
402 : mh->md.melted_coin.h_age_commitment,
403 61 : &mh->md.melted_coin.coin_priv,
404 : &mh->coin_sig);
405 61 : GNUNET_CRYPTO_eddsa_key_get_public (
406 61 : &mh->md.melted_coin.coin_priv.eddsa_priv,
407 : &mh->coin_pub.eddsa_pub);
408 122 : mh->dki = TALER_EXCHANGE_get_denomination_key (
409 61 : mh->keys,
410 61 : &mh->md.melted_coin.pub_key);
411 61 : j_request_body = GNUNET_JSON_PACK (
412 : GNUNET_JSON_pack_data_auto ("old_coin_pub",
413 : &mh->coin_pub),
414 : GNUNET_JSON_pack_data_auto ("old_denom_pub_h",
415 : &h_denom_pub),
416 : TALER_JSON_pack_denom_sig ("old_denom_sig",
417 : &mh->md.melted_coin.sig),
418 : GNUNET_JSON_pack_data_auto ("confirm_sig",
419 : &mh->coin_sig),
420 : TALER_JSON_pack_amount ("value_with_fee",
421 : &mh->md.melted_coin.melt_amount_with_fee),
422 : GNUNET_JSON_pack_allow_null (
423 : (NULL != mh->md.melted_coin.h_age_commitment)
424 : ? GNUNET_JSON_pack_data_auto ("old_age_commitment_h",
425 : mh->md.melted_coin.h_age_commitment)
426 : : GNUNET_JSON_pack_string ("old_age_commitment_h",
427 : NULL)),
428 : GNUNET_JSON_pack_data_auto ("refresh_seed",
429 : &mh->md.refresh_seed),
430 : GNUNET_JSON_pack_allow_null (
431 : (mh->md.no_blinding_seed)
432 : ? GNUNET_JSON_pack_string ("blinding_seed",
433 : NULL)
434 : : GNUNET_JSON_pack_data_auto ("blinding_seed",
435 : &mh->md.blinding_seed)),
436 : TALER_JSON_pack_array_of_data_auto ("denoms_h",
437 : mh->md.num_fresh_coins,
438 : mh->md.denoms_h)
439 : );
440 61 : GNUNET_assert (NULL != j_request_body);
441 61 : GNUNET_assert (NULL !=
442 : (j_transfer_pubs = json_array ()));
443 61 : GNUNET_assert (NULL !=
444 : (j_coin_evs = json_array ()));
445 : /**
446 : * Fill the kappa array of coin envelopes and
447 : * the array of transfer pubs.
448 : */
449 244 : for (uint8_t k=0; k<TALER_CNC_KAPPA; k++)
450 : {
451 : json_t *j_envs;
452 183 : json_t *j_tbs = GNUNET_JSON_PACK (
453 : TALER_JSON_pack_array_of_data_auto ("_",
454 : mh->md.num_fresh_coins,
455 : mh->md.kappa_transfer_pubs[k])
456 : );
457 :
458 183 : GNUNET_assert (NULL != (j_envs = json_array ()));
459 183 : GNUNET_assert (NULL != j_tbs);
460 :
461 699 : for (size_t i = 0; i < mh->md.num_fresh_coins; i++)
462 : {
463 516 : json_t *j_coin = GNUNET_JSON_PACK (
464 : TALER_JSON_pack_blinded_planchet (NULL,
465 : &mh->md.kappa_blinded_planchets[k][i])
466 : );
467 516 : GNUNET_assert (NULL != j_coin);
468 516 : GNUNET_assert (0 ==
469 : json_array_append_new (j_envs,
470 : j_coin));
471 : }
472 183 : GNUNET_assert (0 ==
473 : json_array_append_new (j_coin_evs,
474 : j_envs));
475 183 : GNUNET_assert (0 ==
476 : json_array_append (j_transfer_pubs,
477 : json_object_get (j_tbs,
478 : "_")));
479 183 : json_decref (j_tbs);
480 : }
481 61 : GNUNET_assert (0 ==
482 : json_object_set_new (j_request_body,
483 : "coin_evs",
484 : j_coin_evs));
485 61 : GNUNET_assert (0 ==
486 : json_object_set_new (j_request_body,
487 : "transfer_pubs",
488 : j_transfer_pubs));
489 : /* and now we can at last begin the actual request handling */
490 61 : mh->url = TALER_url_join (mh->exchange_url,
491 : "melt",
492 : NULL);
493 61 : if (NULL == mh->url)
494 : {
495 0 : json_decref (j_request_body);
496 0 : return GNUNET_SYSERR;
497 : }
498 61 : eh = TALER_EXCHANGE_curl_easy_get_ (mh->url);
499 122 : if ( (NULL == eh) ||
500 : (GNUNET_OK !=
501 61 : TALER_curl_easy_post (&mh->ctx,
502 : eh,
503 : j_request_body)) )
504 : {
505 0 : GNUNET_break (0);
506 0 : if (NULL != eh)
507 0 : curl_easy_cleanup (eh);
508 0 : json_decref (j_request_body);
509 0 : return GNUNET_SYSERR;
510 : }
511 61 : json_decref (j_request_body);
512 122 : mh->job = GNUNET_CURL_job_add2 (mh->cctx,
513 : eh,
514 61 : mh->ctx.headers,
515 : &handle_melt_finished,
516 : mh);
517 61 : if (NULL == mh->job)
518 : {
519 0 : GNUNET_break (0);
520 : /* GNUNET_CURL_job_add2() already released @a eh */
521 0 : return GNUNET_SYSERR;
522 : }
523 61 : return GNUNET_OK;
524 : }
525 :
526 :
527 : /**
528 : * The melt request @a mh failed, return an error to
529 : * the application and cancel the operation.
530 : *
531 : * @param[in] mh melt request that failed
532 : * @param ec error code to fail with
533 : */
534 : static void
535 0 : fail_mh (struct TALER_EXCHANGE_PostMeltHandle *mh,
536 : enum TALER_ErrorCode ec)
537 : {
538 0 : struct TALER_EXCHANGE_PostMeltResponse mr = {
539 : .hr.ec = ec
540 : };
541 :
542 0 : mh->melt_cb (mh->melt_cb_cls,
543 : &mr);
544 0 : TALER_EXCHANGE_post_melt_cancel (mh);
545 0 : }
546 :
547 :
548 : /**
549 : * Callbacks of this type are used to serve the result of submitting a
550 : * /blinding-prepare request to a exchange.
551 : *
552 : * @param cls closure with our `struct TALER_EXCHANGE_MeltHandle *`
553 : * @param bpr response details
554 : */
555 : static void
556 31 : blinding_prepare_cb (
557 : void *cls,
558 : const struct TALER_EXCHANGE_PostBlindingPrepareResponse *bpr)
559 : {
560 31 : struct TALER_EXCHANGE_PostMeltHandle *mh = cls;
561 31 : unsigned int nks_off = 0;
562 :
563 31 : mh->bpr = NULL;
564 31 : if (MHD_HTTP_OK != bpr->hr.http_status)
565 : {
566 1 : struct TALER_EXCHANGE_PostMeltResponse mr = {
567 : .hr = bpr->hr
568 : };
569 :
570 1 : mr.hr.hint = "/blinding-prepare failed";
571 1 : mh->melt_cb (mh->melt_cb_cls,
572 : &mr);
573 1 : TALER_EXCHANGE_post_melt_cancel (mh);
574 1 : return;
575 : }
576 114 : for (unsigned int i = 0; i<mh->rd->num_fresh_denom_pubs; i++)
577 : {
578 84 : const struct TALER_EXCHANGE_DenomPublicKey *fresh_pk =
579 84 : &mh->rd->fresh_denom_pubs[i];
580 84 : struct TALER_ExchangeBlindingValues *wv = &mh->melt_blinding_values[i];
581 :
582 84 : switch (fresh_pk->key.bsign_pub_key->cipher)
583 : {
584 0 : case GNUNET_CRYPTO_BSA_INVALID:
585 0 : GNUNET_break (0);
586 0 : fail_mh (mh,
587 : TALER_EC_GENERIC_CLIENT_INTERNAL_ERROR);
588 0 : return;
589 0 : case GNUNET_CRYPTO_BSA_RSA:
590 0 : break;
591 84 : case GNUNET_CRYPTO_BSA_CS:
592 84 : TALER_denom_ewv_copy (wv,
593 84 : &bpr->details.ok.blinding_values[nks_off]);
594 84 : nks_off++;
595 84 : break;
596 : }
597 : }
598 30 : if (GNUNET_OK !=
599 30 : start_melt (mh))
600 : {
601 0 : GNUNET_break (0);
602 0 : fail_mh (mh,
603 : TALER_EC_GENERIC_CLIENT_INTERNAL_ERROR);
604 0 : return;
605 : }
606 : }
607 :
608 :
609 : struct TALER_EXCHANGE_PostMeltHandle *
610 62 : TALER_EXCHANGE_post_melt_create (
611 : struct GNUNET_CURL_Context *ctx,
612 : const char *url,
613 : struct TALER_EXCHANGE_Keys *keys,
614 : const struct TALER_PublicRefreshMasterSeedP *rms,
615 : const struct TALER_EXCHANGE_MeltInput *rd)
616 : {
617 : struct TALER_EXCHANGE_PostMeltHandle *mh;
618 :
619 62 : if (0 == rd->num_fresh_denom_pubs)
620 : {
621 0 : GNUNET_break (0);
622 0 : return NULL;
623 : }
624 62 : mh = GNUNET_new (struct TALER_EXCHANGE_PostMeltHandle);
625 62 : mh->noreveal_index = TALER_CNC_KAPPA; /* invalid value */
626 62 : mh->cctx = ctx;
627 62 : mh->exchange_url = GNUNET_strdup (url);
628 62 : mh->rd = rd;
629 62 : mh->rms = *rms;
630 62 : mh->no_blinding_seed = true;
631 62 : mh->melt_blinding_values =
632 62 : GNUNET_new_array (rd->num_fresh_denom_pubs,
633 : struct TALER_ExchangeBlindingValues);
634 238 : for (unsigned int i = 0; i < rd->num_fresh_denom_pubs; i++)
635 : {
636 176 : const struct TALER_EXCHANGE_DenomPublicKey *fresh_pk =
637 176 : &rd->fresh_denom_pubs[i];
638 :
639 176 : switch (fresh_pk->key.bsign_pub_key->cipher)
640 : {
641 0 : case GNUNET_CRYPTO_BSA_INVALID:
642 0 : GNUNET_break (0);
643 0 : GNUNET_free (mh->melt_blinding_values);
644 0 : GNUNET_free (mh->exchange_url);
645 0 : GNUNET_free (mh);
646 0 : return NULL;
647 88 : case GNUNET_CRYPTO_BSA_RSA:
648 88 : TALER_denom_ewv_copy (&mh->melt_blinding_values[i],
649 : TALER_denom_ewv_rsa_singleton ());
650 88 : break;
651 88 : case GNUNET_CRYPTO_BSA_CS:
652 88 : mh->no_blinding_seed = false;
653 88 : break;
654 : }
655 : }
656 62 : mh->keys = TALER_EXCHANGE_keys_incref (keys);
657 62 : return mh;
658 : }
659 :
660 :
661 : enum TALER_ErrorCode
662 62 : TALER_EXCHANGE_post_melt_start (
663 : struct TALER_EXCHANGE_PostMeltHandle *mh,
664 : TALER_EXCHANGE_PostMeltCallback melt_cb,
665 : TALER_EXCHANGE_POST_MELT_RESULT_CLOSURE *melt_cb_cls)
666 : {
667 62 : mh->melt_cb = melt_cb;
668 62 : mh->melt_cb_cls = melt_cb_cls;
669 :
670 62 : if (! mh->no_blinding_seed)
671 31 : {
672 0 : struct TALER_EXCHANGE_NonceKey nks[
673 31 : GNUNET_NZL (mh->rd->num_fresh_denom_pubs)];
674 31 : unsigned int nks_off = 0;
675 :
676 119 : for (unsigned int i = 0; i < mh->rd->num_fresh_denom_pubs; i++)
677 : {
678 88 : const struct TALER_EXCHANGE_DenomPublicKey *fresh_pk =
679 88 : &mh->rd->fresh_denom_pubs[i];
680 :
681 88 : if (GNUNET_CRYPTO_BSA_CS ==
682 88 : fresh_pk->key.bsign_pub_key->cipher)
683 : {
684 88 : nks[nks_off].pk = fresh_pk;
685 88 : nks[nks_off].cnc_num = i;
686 88 : nks_off++;
687 : }
688 : }
689 31 : TALER_cs_refresh_seed_to_blinding_seed (
690 31 : &mh->rms,
691 31 : &mh->rd->melt_priv,
692 : &mh->blinding_seed);
693 31 : mh->bpr = TALER_EXCHANGE_post_blinding_prepare_for_melt_create (
694 : mh->cctx,
695 : mh->exchange_url,
696 : &mh->blinding_seed,
697 : nks_off,
698 : nks);
699 31 : if (NULL == mh->bpr)
700 : {
701 0 : GNUNET_break (0);
702 0 : return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
703 : }
704 : {
705 : enum TALER_ErrorCode ec;
706 :
707 31 : ec = TALER_EXCHANGE_post_blinding_prepare_start (mh->bpr,
708 : &blinding_prepare_cb,
709 : mh);
710 31 : if (TALER_EC_NONE != ec)
711 : {
712 0 : GNUNET_break (0);
713 0 : TALER_EXCHANGE_post_blinding_prepare_cancel (mh->bpr);
714 0 : mh->bpr = NULL;
715 0 : return ec;
716 : }
717 : }
718 31 : return TALER_EC_NONE;
719 : }
720 31 : if (GNUNET_OK !=
721 31 : start_melt (mh))
722 : {
723 0 : GNUNET_break (0);
724 0 : return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
725 : }
726 31 : return TALER_EC_NONE;
727 : }
728 :
729 :
730 : void
731 62 : TALER_EXCHANGE_post_melt_cancel (struct TALER_EXCHANGE_PostMeltHandle *mh)
732 : {
733 238 : for (unsigned int i = 0; i < mh->rd->num_fresh_denom_pubs; i++)
734 176 : TALER_denom_ewv_free (&mh->melt_blinding_values[i]);
735 62 : if (NULL != mh->job)
736 : {
737 0 : GNUNET_CURL_job_cancel (mh->job);
738 0 : mh->job = NULL;
739 : }
740 62 : if (NULL != mh->bpr)
741 : {
742 0 : TALER_EXCHANGE_post_blinding_prepare_cancel (mh->bpr);
743 0 : mh->bpr = NULL;
744 : }
745 62 : TALER_EXCHANGE_free_melt_data (&mh->md); /* does not free 'md' itself */
746 62 : GNUNET_free (mh->melt_blinding_values);
747 62 : GNUNET_free (mh->url);
748 62 : GNUNET_free (mh->exchange_url);
749 62 : TALER_curl_easy_post_finished (&mh->ctx);
750 62 : TALER_EXCHANGE_keys_decref (mh->keys);
751 62 : GNUNET_free (mh);
752 62 : }
753 :
754 :
755 : /* end of exchange_api_melt.c */
|