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.c
19 : * @brief Implementation of the /recoup 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_RecoupHandle
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 : * Our signature requesting the recoup.
65 : */
66 : struct TALER_CoinSpendSignatureP coin_sig;
67 :
68 : /**
69 : * Handle for the request.
70 : */
71 : struct GNUNET_CURL_Job *job;
72 :
73 : /**
74 : * Function to call with the result.
75 : */
76 : TALER_EXCHANGE_RecoupResultCallback cb;
77 :
78 : /**
79 : * Closure for @a cb.
80 : */
81 : void *cb_cls;
82 :
83 : /**
84 : * Public key of the coin we are trying to get paid back.
85 : */
86 : struct TALER_CoinSpendPublicKeyP coin_pub;
87 :
88 : };
89 :
90 :
91 : /**
92 : * Parse a recoup 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 (const struct TALER_EXCHANGE_RecoupHandle *ph,
101 : const json_t *json)
102 : {
103 : struct TALER_ReservePublicKeyP reserve_pub;
104 : struct GNUNET_JSON_Specification spec_withdraw[] = {
105 0 : GNUNET_JSON_spec_fixed_auto ("reserve_pub",
106 : &reserve_pub),
107 0 : GNUNET_JSON_spec_end ()
108 : };
109 0 : struct TALER_EXCHANGE_HttpResponse hr = {
110 : .reply = json,
111 : .http_status = MHD_HTTP_OK
112 : };
113 :
114 0 : if (GNUNET_OK !=
115 0 : GNUNET_JSON_parse (json,
116 : spec_withdraw,
117 : NULL, NULL))
118 : {
119 0 : GNUNET_break_op (0);
120 0 : return GNUNET_SYSERR;
121 : }
122 0 : ph->cb (ph->cb_cls,
123 : &hr,
124 : &reserve_pub);
125 0 : return GNUNET_OK;
126 : }
127 :
128 :
129 : /**
130 : * Function called when we're done processing the
131 : * HTTP /recoup request.
132 : *
133 : * @param cls the `struct TALER_EXCHANGE_RecoupHandle`
134 : * @param response_code HTTP response code, 0 on error
135 : * @param response parsed JSON result, NULL on error
136 : */
137 : static void
138 0 : handle_recoup_finished (void *cls,
139 : long response_code,
140 : const void *response)
141 : {
142 0 : struct TALER_EXCHANGE_RecoupHandle *ph = cls;
143 0 : const json_t *j = response;
144 0 : struct TALER_EXCHANGE_HttpResponse hr = {
145 : .reply = j,
146 0 : .http_status = (unsigned int) response_code
147 : };
148 : const struct TALER_EXCHANGE_Keys *keys;
149 :
150 0 : ph->job = NULL;
151 0 : keys = TALER_EXCHANGE_get_keys (ph->exchange);
152 0 : switch (response_code)
153 : {
154 0 : case 0:
155 0 : hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
156 0 : break;
157 0 : case MHD_HTTP_OK:
158 0 : if (GNUNET_OK !=
159 0 : process_recoup_response (ph,
160 : j))
161 : {
162 0 : GNUNET_break_op (0);
163 0 : hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
164 0 : hr.http_status = 0;
165 0 : break;
166 : }
167 0 : TALER_EXCHANGE_recoup_cancel (ph);
168 0 : return;
169 0 : case MHD_HTTP_BAD_REQUEST:
170 : /* This should never happen, either us or the exchange is buggy
171 : (or API version conflict); just pass JSON reply to the application */
172 0 : hr.ec = TALER_JSON_get_error_code (j);
173 0 : hr.hint = TALER_JSON_get_error_hint (j);
174 0 : break;
175 0 : case MHD_HTTP_CONFLICT:
176 : {
177 : struct TALER_Amount min_key;
178 :
179 0 : hr.ec = TALER_JSON_get_error_code (j);
180 0 : hr.hint = TALER_JSON_get_error_hint (j);
181 0 : if (GNUNET_OK !=
182 0 : TALER_EXCHANGE_get_min_denomination_ (keys,
183 : &min_key))
184 : {
185 0 : GNUNET_break (0);
186 0 : hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
187 0 : hr.http_status = 0;
188 0 : break;
189 : }
190 0 : if (GNUNET_OK !=
191 0 : TALER_EXCHANGE_check_coin_conflict_ (
192 : keys,
193 : j,
194 0 : &ph->pk,
195 0 : &ph->coin_pub,
196 0 : &ph->coin_sig,
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 : break;
205 : }
206 0 : case MHD_HTTP_FORBIDDEN:
207 : /* Nothing really to verify, exchange says one of the signatures is
208 : invalid; as we checked them, this should never happen, we
209 : should pass the JSON reply to the application */
210 0 : hr.ec = TALER_JSON_get_error_code (j);
211 0 : hr.hint = TALER_JSON_get_error_hint (j);
212 0 : break;
213 0 : case MHD_HTTP_NOT_FOUND:
214 : /* Nothing really to verify, this should never
215 : happen, we should pass the JSON reply to the application */
216 0 : hr.ec = TALER_JSON_get_error_code (j);
217 0 : hr.hint = TALER_JSON_get_error_hint (j);
218 0 : break;
219 0 : case MHD_HTTP_GONE:
220 : /* Kind of normal: the money was already sent to the merchant
221 : (it was too late for the refund). */
222 0 : hr.ec = TALER_JSON_get_error_code (j);
223 0 : hr.hint = TALER_JSON_get_error_hint (j);
224 0 : break;
225 0 : case MHD_HTTP_INTERNAL_SERVER_ERROR:
226 : /* Server had an internal issue; we should retry, but this API
227 : leaves this to the application */
228 0 : hr.ec = TALER_JSON_get_error_code (j);
229 0 : hr.hint = TALER_JSON_get_error_hint (j);
230 0 : break;
231 0 : default:
232 : /* unexpected response code */
233 0 : hr.ec = TALER_JSON_get_error_code (j);
234 0 : hr.hint = TALER_JSON_get_error_hint (j);
235 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
236 : "Unexpected response code %u/%d for exchange recoup\n",
237 : (unsigned int) response_code,
238 : (int) hr.ec);
239 0 : GNUNET_break (0);
240 0 : break;
241 : }
242 0 : ph->cb (ph->cb_cls,
243 : &hr,
244 : NULL);
245 0 : TALER_EXCHANGE_recoup_cancel (ph);
246 : }
247 :
248 :
249 : struct TALER_EXCHANGE_RecoupHandle *
250 0 : TALER_EXCHANGE_recoup (struct TALER_EXCHANGE_Handle *exchange,
251 : const struct TALER_EXCHANGE_DenomPublicKey *pk,
252 : const struct TALER_DenominationSignature *denom_sig,
253 : const struct TALER_ExchangeWithdrawValues *exchange_vals,
254 : const struct TALER_PlanchetMasterSecretP *ps,
255 : TALER_EXCHANGE_RecoupResultCallback recoup_cb,
256 : void *recoup_cb_cls)
257 : {
258 : struct TALER_EXCHANGE_RecoupHandle *ph;
259 : struct GNUNET_CURL_Context *ctx;
260 : struct TALER_DenominationHashP h_denom_pub;
261 : json_t *recoup_obj;
262 : CURL *eh;
263 : char arg_str[sizeof (struct TALER_CoinSpendPublicKeyP) * 2 + 32];
264 : struct TALER_CoinSpendPrivateKeyP coin_priv;
265 : union TALER_DenominationBlindingKeyP bks;
266 :
267 0 : GNUNET_assert (GNUNET_YES ==
268 : TEAH_handle_is_ready (exchange));
269 0 : ph = GNUNET_new (struct TALER_EXCHANGE_RecoupHandle);
270 0 : TALER_planchet_setup_coin_priv (ps,
271 : exchange_vals,
272 : &coin_priv);
273 0 : TALER_planchet_blinding_secret_create (ps,
274 : exchange_vals,
275 : &bks);
276 0 : GNUNET_CRYPTO_eddsa_key_get_public (&coin_priv.eddsa_priv,
277 : &ph->coin_pub.eddsa_pub);
278 0 : TALER_denom_pub_hash (&pk->key,
279 : &h_denom_pub);
280 0 : TALER_wallet_recoup_sign (&h_denom_pub,
281 : &bks,
282 : &coin_priv,
283 : &ph->coin_sig);
284 0 : recoup_obj = GNUNET_JSON_PACK (
285 : GNUNET_JSON_pack_data_auto ("denom_pub_hash",
286 : &h_denom_pub),
287 : TALER_JSON_pack_denom_sig ("denom_sig",
288 : denom_sig),
289 : TALER_JSON_pack_exchange_withdraw_values ("ewv",
290 : exchange_vals),
291 : GNUNET_JSON_pack_data_auto ("coin_sig",
292 : &ph->coin_sig),
293 : GNUNET_JSON_pack_data_auto ("coin_blind_key_secret",
294 : &bks));
295 0 : if (TALER_DENOMINATION_CS == denom_sig->cipher)
296 : {
297 : struct TALER_CsNonce nonce;
298 :
299 : /* NOTE: this is not elegant, and as per the note in TALER_coin_ev_hash()
300 : it is not strictly clear that the nonce is needed. Best case would be
301 : to find a way to include it more 'naturally' somehow, for example with
302 : the variant union version of bks! */
303 0 : TALER_cs_withdraw_nonce_derive (ps,
304 : &nonce);
305 0 : GNUNET_assert (
306 : 0 ==
307 : json_object_set_new (recoup_obj,
308 : "cs_nonce",
309 : GNUNET_JSON_from_data_auto (
310 : &nonce)));
311 : }
312 :
313 : {
314 : char pub_str[sizeof (struct TALER_CoinSpendPublicKeyP) * 2];
315 : char *end;
316 :
317 0 : end = GNUNET_STRINGS_data_to_string (
318 0 : &ph->coin_pub,
319 : sizeof (struct TALER_CoinSpendPublicKeyP),
320 : pub_str,
321 : sizeof (pub_str));
322 0 : *end = '\0';
323 0 : GNUNET_snprintf (arg_str,
324 : sizeof (arg_str),
325 : "/coins/%s/recoup",
326 : pub_str);
327 : }
328 :
329 0 : ph->exchange = exchange;
330 0 : ph->pk = *pk;
331 0 : memset (&ph->pk.key,
332 : 0,
333 : sizeof (ph->pk.key)); /* zero out, as lifetime cannot be warranted */
334 0 : ph->cb = recoup_cb;
335 0 : ph->cb_cls = recoup_cb_cls;
336 0 : ph->url = TEAH_path_to_url (exchange,
337 : arg_str);
338 0 : if (NULL == ph->url)
339 : {
340 0 : json_decref (recoup_obj);
341 0 : GNUNET_free (ph);
342 0 : return NULL;
343 : }
344 0 : eh = TALER_EXCHANGE_curl_easy_get_ (ph->url);
345 0 : if ( (NULL == eh) ||
346 : (GNUNET_OK !=
347 0 : TALER_curl_easy_post (&ph->ctx,
348 : eh,
349 : recoup_obj)) )
350 : {
351 0 : GNUNET_break (0);
352 0 : if (NULL != eh)
353 0 : curl_easy_cleanup (eh);
354 0 : json_decref (recoup_obj);
355 0 : GNUNET_free (ph->url);
356 0 : GNUNET_free (ph);
357 0 : return NULL;
358 : }
359 0 : json_decref (recoup_obj);
360 0 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
361 : "URL for recoup: `%s'\n",
362 : ph->url);
363 0 : ctx = TEAH_handle_to_context (exchange);
364 0 : ph->job = GNUNET_CURL_job_add2 (ctx,
365 : eh,
366 0 : ph->ctx.headers,
367 : &handle_recoup_finished,
368 : ph);
369 0 : return ph;
370 : }
371 :
372 :
373 : void
374 0 : TALER_EXCHANGE_recoup_cancel (struct TALER_EXCHANGE_RecoupHandle *ph)
375 : {
376 0 : if (NULL != ph->job)
377 : {
378 0 : GNUNET_CURL_job_cancel (ph->job);
379 0 : ph->job = NULL;
380 : }
381 0 : GNUNET_free (ph->url);
382 0 : TALER_curl_easy_post_finished (&ph->ctx);
383 0 : GNUNET_free (ph);
384 0 : }
385 :
386 :
387 : /* end of exchange_api_recoup.c */
|