Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2017-2022 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_recoup_refresh.c
19 : * @brief Implementation of the /recoup-refresh request of the exchange's HTTP API
20 : * @author Christian Grothoff
21 : */
22 : #include "platform.h"
23 : #include <jansson.h>
24 : #include <microhttpd.h> /* just for HTTP status codes */
25 : #include <gnunet/gnunet_util_lib.h>
26 : #include <gnunet/gnunet_json_lib.h>
27 : #include <gnunet/gnunet_curl_lib.h>
28 : #include "taler_json_lib.h"
29 : #include "taler_exchange_service.h"
30 : #include "exchange_api_common.h"
31 : #include "exchange_api_handle.h"
32 : #include "taler_signatures.h"
33 : #include "exchange_api_curl_defaults.h"
34 :
35 :
36 : /**
37 : * @brief A Recoup Handle
38 : */
39 : struct TALER_EXCHANGE_RecoupRefreshHandle
40 : {
41 :
42 : /**
43 : * The connection to exchange this request handle will use
44 : */
45 : struct TALER_EXCHANGE_Handle *exchange;
46 :
47 : /**
48 : * The url for this request.
49 : */
50 : char *url;
51 :
52 : /**
53 : * Context for #TEH_curl_easy_post(). Keeps the data that must
54 : * persist for Curl to make the upload.
55 : */
56 : struct TALER_CURL_PostContext ctx;
57 :
58 : /**
59 : * Denomination key of the coin.
60 : */
61 : struct TALER_EXCHANGE_DenomPublicKey pk;
62 :
63 : /**
64 : * Handle for the request.
65 : */
66 : struct GNUNET_CURL_Job *job;
67 :
68 : /**
69 : * Function to call with the result.
70 : */
71 : TALER_EXCHANGE_RecoupRefreshResultCallback cb;
72 :
73 : /**
74 : * Closure for @a cb.
75 : */
76 : void *cb_cls;
77 :
78 : /**
79 : * Public key of the coin we are trying to get paid back.
80 : */
81 : struct TALER_CoinSpendPublicKeyP coin_pub;
82 :
83 : /**
84 : * Signature affirming the recoup-refresh operation.
85 : */
86 : struct TALER_CoinSpendSignatureP coin_sig;
87 :
88 : };
89 :
90 :
91 : /**
92 : * Parse a recoup-refresh response. If it is valid, call the callback.
93 : *
94 : * @param ph recoup handle
95 : * @param json json reply with the signature
96 : * @return #GNUNET_OK if the signature is valid and we called the callback;
97 : * #GNUNET_SYSERR if not (callback must still be called)
98 : */
99 : static enum GNUNET_GenericReturnValue
100 0 : process_recoup_response (
101 : const struct TALER_EXCHANGE_RecoupRefreshHandle *ph,
102 : const json_t *json)
103 : {
104 : struct TALER_CoinSpendPublicKeyP old_coin_pub;
105 : struct GNUNET_JSON_Specification spec_refresh[] = {
106 0 : GNUNET_JSON_spec_fixed_auto ("old_coin_pub",
107 : &old_coin_pub),
108 0 : GNUNET_JSON_spec_end ()
109 : };
110 0 : struct TALER_EXCHANGE_HttpResponse hr = {
111 : .reply = json,
112 : .http_status = MHD_HTTP_OK
113 : };
114 :
115 0 : if (GNUNET_OK !=
116 0 : GNUNET_JSON_parse (json,
117 : spec_refresh,
118 : NULL, NULL))
119 : {
120 0 : GNUNET_break_op (0);
121 0 : return GNUNET_SYSERR;
122 : }
123 0 : ph->cb (ph->cb_cls,
124 : &hr,
125 : &old_coin_pub);
126 0 : return GNUNET_OK;
127 : }
128 :
129 :
130 : /**
131 : * Function called when we're done processing the
132 : * HTTP /recoup-refresh request.
133 : *
134 : * @param cls the `struct TALER_EXCHANGE_RecoupRefreshHandle`
135 : * @param response_code HTTP response code, 0 on error
136 : * @param response parsed JSON result, NULL on error
137 : */
138 : static void
139 0 : handle_recoup_refresh_finished (void *cls,
140 : long response_code,
141 : const void *response)
142 : {
143 0 : struct TALER_EXCHANGE_RecoupRefreshHandle *ph = cls;
144 0 : const json_t *j = response;
145 0 : struct TALER_EXCHANGE_HttpResponse hr = {
146 : .reply = j,
147 0 : .http_status = (unsigned int) response_code
148 : };
149 : const struct TALER_EXCHANGE_Keys *keys;
150 :
151 0 : ph->job = NULL;
152 0 : keys = TALER_EXCHANGE_get_keys (ph->exchange);
153 0 : switch (response_code)
154 : {
155 0 : case 0:
156 0 : hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
157 0 : break;
158 0 : case MHD_HTTP_OK:
159 0 : if (GNUNET_OK !=
160 0 : process_recoup_response (ph,
161 : j))
162 : {
163 0 : GNUNET_break_op (0);
164 0 : hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
165 0 : hr.http_status = 0;
166 0 : break;
167 : }
168 0 : TALER_EXCHANGE_recoup_refresh_cancel (ph);
169 0 : return;
170 0 : case MHD_HTTP_BAD_REQUEST:
171 : /* This should never happen, either us or the exchange is buggy
172 : (or API version conflict); just pass JSON reply to the application */
173 0 : hr.ec = TALER_JSON_get_error_code (j);
174 0 : hr.hint = TALER_JSON_get_error_hint (j);
175 0 : break;
176 0 : case MHD_HTTP_FORBIDDEN:
177 : /* Nothing really to verify, exchange says one of the signatures is
178 : invalid; as we checked them, this should never happen, we
179 : should pass the JSON reply to the application */
180 0 : hr.ec = TALER_JSON_get_error_code (j);
181 0 : hr.hint = TALER_JSON_get_error_hint (j);
182 0 : break;
183 0 : case MHD_HTTP_NOT_FOUND:
184 : /* Nothing really to verify, this should never
185 : happen, we should pass the JSON reply to the application */
186 0 : hr.ec = TALER_JSON_get_error_code (j);
187 0 : hr.hint = TALER_JSON_get_error_hint (j);
188 0 : break;
189 0 : case MHD_HTTP_CONFLICT:
190 : {
191 : struct TALER_Amount min_key;
192 :
193 0 : hr.ec = TALER_JSON_get_error_code (j);
194 0 : hr.hint = TALER_JSON_get_error_hint (j);
195 0 : if (GNUNET_OK !=
196 0 : TALER_EXCHANGE_get_min_denomination_ (keys,
197 : &min_key))
198 : {
199 0 : GNUNET_break (0);
200 0 : hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
201 0 : hr.http_status = 0;
202 0 : break;
203 : }
204 0 : if (GNUNET_OK !=
205 0 : TALER_EXCHANGE_check_coin_conflict_ (
206 : keys,
207 : j,
208 0 : &ph->pk,
209 0 : &ph->coin_pub,
210 0 : &ph->coin_sig,
211 : &min_key))
212 : {
213 0 : GNUNET_break (0);
214 0 : hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
215 0 : hr.http_status = 0;
216 0 : break;
217 : }
218 0 : break;
219 : }
220 0 : case MHD_HTTP_GONE:
221 : /* Kind of normal: the money was already sent to the merchant
222 : (it was too late for the refund). */
223 0 : hr.ec = TALER_JSON_get_error_code (j);
224 0 : hr.hint = TALER_JSON_get_error_hint (j);
225 0 : break;
226 0 : case MHD_HTTP_INTERNAL_SERVER_ERROR:
227 : /* Server had an internal issue; we should retry, but this API
228 : leaves this to the application */
229 0 : hr.ec = TALER_JSON_get_error_code (j);
230 0 : hr.hint = TALER_JSON_get_error_hint (j);
231 0 : break;
232 0 : default:
233 : /* unexpected response code */
234 0 : hr.ec = TALER_JSON_get_error_code (j);
235 0 : hr.hint = TALER_JSON_get_error_hint (j);
236 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
237 : "Unexpected response code %u/%d for exchange recoup\n",
238 : (unsigned int) response_code,
239 : (int) hr.ec);
240 0 : GNUNET_break (0);
241 0 : break;
242 : }
243 0 : ph->cb (ph->cb_cls,
244 : &hr,
245 : NULL);
246 0 : TALER_EXCHANGE_recoup_refresh_cancel (ph);
247 : }
248 :
249 :
250 : struct TALER_EXCHANGE_RecoupRefreshHandle *
251 0 : TALER_EXCHANGE_recoup_refresh (
252 : struct TALER_EXCHANGE_Handle *exchange,
253 : const struct TALER_EXCHANGE_DenomPublicKey *pk,
254 : const struct TALER_DenominationSignature *denom_sig,
255 : const struct TALER_ExchangeWithdrawValues *exchange_vals,
256 : const struct TALER_RefreshMasterSecretP *rms,
257 : const struct TALER_PlanchetMasterSecretP *ps,
258 : unsigned int idx,
259 : TALER_EXCHANGE_RecoupRefreshResultCallback recoup_cb,
260 : void *recoup_cb_cls)
261 : {
262 : struct TALER_EXCHANGE_RecoupRefreshHandle *ph;
263 : struct GNUNET_CURL_Context *ctx;
264 : struct TALER_DenominationHashP h_denom_pub;
265 : json_t *recoup_obj;
266 : CURL *eh;
267 : char arg_str[sizeof (struct TALER_CoinSpendPublicKeyP) * 2 + 32];
268 : struct TALER_CoinSpendPrivateKeyP coin_priv;
269 : union TALER_DenominationBlindingKeyP bks;
270 :
271 0 : GNUNET_assert (NULL != recoup_cb);
272 0 : GNUNET_assert (GNUNET_YES ==
273 : TEAH_handle_is_ready (exchange));
274 0 : ph = GNUNET_new (struct TALER_EXCHANGE_RecoupRefreshHandle);
275 0 : ph->exchange = exchange;
276 0 : ph->pk = *pk;
277 0 : memset (&ph->pk.key,
278 : 0,
279 : sizeof (ph->pk.key)); /* zero out, as lifetime cannot be warranted */
280 0 : ph->cb = recoup_cb;
281 0 : ph->cb_cls = recoup_cb_cls;
282 0 : TALER_planchet_setup_coin_priv (ps,
283 : exchange_vals,
284 : &coin_priv);
285 0 : TALER_planchet_blinding_secret_create (ps,
286 : exchange_vals,
287 : &bks);
288 0 : GNUNET_CRYPTO_eddsa_key_get_public (&coin_priv.eddsa_priv,
289 : &ph->coin_pub.eddsa_pub);
290 0 : TALER_denom_pub_hash (&pk->key,
291 : &h_denom_pub);
292 0 : TALER_wallet_recoup_refresh_sign (&h_denom_pub,
293 : &bks,
294 : &coin_priv,
295 : &ph->coin_sig);
296 0 : recoup_obj = GNUNET_JSON_PACK (
297 : GNUNET_JSON_pack_data_auto ("denom_pub_hash",
298 : &h_denom_pub),
299 : TALER_JSON_pack_denom_sig ("denom_sig",
300 : denom_sig),
301 : TALER_JSON_pack_exchange_withdraw_values ("ewv",
302 : exchange_vals),
303 : GNUNET_JSON_pack_data_auto ("coin_sig",
304 : &ph->coin_sig),
305 : GNUNET_JSON_pack_data_auto ("coin_blind_key_secret",
306 : &bks));
307 :
308 0 : if (TALER_DENOMINATION_CS == denom_sig->cipher)
309 : {
310 : struct TALER_CsNonce nonce;
311 :
312 : /* NOTE: this is not elegant, and as per the note in TALER_coin_ev_hash()
313 : it is not strictly clear that the nonce is needed. Best case would be
314 : to find a way to include it more 'naturally' somehow, for example with
315 : the variant union version of bks! */
316 0 : TALER_cs_refresh_nonce_derive (rms,
317 : idx,
318 : &nonce);
319 0 : GNUNET_assert (
320 : 0 ==
321 : json_object_set_new (recoup_obj,
322 : "cs_nonce",
323 : GNUNET_JSON_from_data_auto (
324 : &nonce)));
325 : }
326 :
327 : {
328 : char pub_str[sizeof (struct TALER_CoinSpendPublicKeyP) * 2];
329 : char *end;
330 :
331 0 : end = GNUNET_STRINGS_data_to_string (
332 0 : &ph->coin_pub,
333 : sizeof (struct TALER_CoinSpendPublicKeyP),
334 : pub_str,
335 : sizeof (pub_str));
336 0 : *end = '\0';
337 0 : GNUNET_snprintf (arg_str,
338 : sizeof (arg_str),
339 : "/coins/%s/recoup-refresh",
340 : pub_str);
341 : }
342 :
343 0 : ph->url = TEAH_path_to_url (exchange,
344 : arg_str);
345 0 : if (NULL == ph->url)
346 : {
347 0 : json_decref (recoup_obj);
348 0 : GNUNET_free (ph);
349 0 : return NULL;
350 : }
351 0 : eh = TALER_EXCHANGE_curl_easy_get_ (ph->url);
352 0 : if ( (NULL == eh) ||
353 : (GNUNET_OK !=
354 0 : TALER_curl_easy_post (&ph->ctx,
355 : eh,
356 : recoup_obj)) )
357 : {
358 0 : GNUNET_break (0);
359 0 : if (NULL != eh)
360 0 : curl_easy_cleanup (eh);
361 0 : json_decref (recoup_obj);
362 0 : GNUNET_free (ph->url);
363 0 : GNUNET_free (ph);
364 0 : return NULL;
365 : }
366 0 : json_decref (recoup_obj);
367 0 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
368 : "URL for recoup-refresh: `%s'\n",
369 : ph->url);
370 0 : ctx = TEAH_handle_to_context (exchange);
371 0 : ph->job = GNUNET_CURL_job_add2 (ctx,
372 : eh,
373 0 : ph->ctx.headers,
374 : &handle_recoup_refresh_finished,
375 : ph);
376 0 : return ph;
377 : }
378 :
379 :
380 : void
381 0 : TALER_EXCHANGE_recoup_refresh_cancel (
382 : struct TALER_EXCHANGE_RecoupRefreshHandle *ph)
383 : {
384 0 : if (NULL != ph->job)
385 : {
386 0 : GNUNET_CURL_job_cancel (ph->job);
387 0 : ph->job = NULL;
388 : }
389 0 : GNUNET_free (ph->url);
390 0 : TALER_curl_easy_post_finished (&ph->ctx);
391 0 : GNUNET_free (ph);
392 0 : }
393 :
394 :
395 : /* end of exchange_api_recoup_refresh.c */
|