Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2017-2026 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-recoup-refresh.c
19 : * @brief Implementation of the /recoup-refresh request of the exchange's HTTP API
20 : * @author Christian Grothoff
21 : */
22 : #include "taler/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/taler_json_lib.h"
29 : #include "taler/taler_exchange_service.h"
30 : #include "exchange_api_common.h"
31 : #include "exchange_api_handle.h"
32 : #include "taler/taler_signatures.h"
33 : #include "exchange_api_curl_defaults.h"
34 :
35 :
36 : /**
37 : * @brief A Recoup-Refresh Handle
38 : */
39 : struct TALER_EXCHANGE_PostRecoupRefreshHandle
40 : {
41 :
42 : /**
43 : * The base URL for this request.
44 : */
45 : char *base_url;
46 :
47 : /**
48 : * The full URL for this request, set during _start.
49 : */
50 : char *url;
51 :
52 : /**
53 : * Minor context that holds body and headers.
54 : */
55 : struct TALER_CURL_PostContext post_ctx;
56 :
57 : /**
58 : * Handle for the request.
59 : */
60 : struct GNUNET_CURL_Job *job;
61 :
62 : /**
63 : * Function to call with the result.
64 : */
65 : TALER_EXCHANGE_PostRecoupRefreshCallback cb;
66 :
67 : /**
68 : * Closure for @a cb.
69 : */
70 : TALER_EXCHANGE_POST_RECOUP_REFRESH_RESULT_CLOSURE *cb_cls;
71 :
72 : /**
73 : * Reference to the execution context.
74 : */
75 : struct GNUNET_CURL_Context *ctx;
76 :
77 : /**
78 : * The keys of the exchange this request handle will use.
79 : */
80 : struct TALER_EXCHANGE_Keys *keys;
81 :
82 : /**
83 : * Public key of the coin we are trying to get paid back.
84 : */
85 : struct TALER_CoinSpendPublicKeyP coin_pub;
86 :
87 : /**
88 : * Pre-built request body.
89 : */
90 : json_t *body;
91 :
92 : };
93 :
94 :
95 : /**
96 : * Parse a recoup-refresh response. If it is valid, call the callback.
97 : *
98 : * @param ph recoup-refresh handle
99 : * @param json json reply with the signature
100 : * @return #GNUNET_OK if the signature is valid and we called the callback;
101 : * #GNUNET_SYSERR if not (callback must still be called)
102 : */
103 : static enum GNUNET_GenericReturnValue
104 0 : process_recoup_response (
105 : const struct TALER_EXCHANGE_PostRecoupRefreshHandle *ph,
106 : const json_t *json)
107 : {
108 0 : struct TALER_EXCHANGE_PostRecoupRefreshResponse rrr = {
109 : .hr.reply = json,
110 : .hr.http_status = MHD_HTTP_OK
111 : };
112 : struct GNUNET_JSON_Specification spec_refresh[] = {
113 0 : GNUNET_JSON_spec_fixed_auto ("old_coin_pub",
114 : &rrr.details.ok.old_coin_pub),
115 0 : GNUNET_JSON_spec_end ()
116 : };
117 :
118 0 : if (GNUNET_OK !=
119 0 : GNUNET_JSON_parse (json,
120 : spec_refresh,
121 : NULL, NULL))
122 : {
123 0 : GNUNET_break_op (0);
124 0 : return GNUNET_SYSERR;
125 : }
126 0 : ph->cb (ph->cb_cls,
127 : &rrr);
128 0 : return GNUNET_OK;
129 : }
130 :
131 :
132 : /**
133 : * Function called when we're done processing the
134 : * HTTP /recoup-refresh request.
135 : *
136 : * @param cls the `struct TALER_EXCHANGE_PostRecoupRefreshHandle`
137 : * @param response_code HTTP response code, 0 on error
138 : * @param response parsed JSON result, NULL on error
139 : */
140 : static void
141 0 : handle_recoup_refresh_finished (void *cls,
142 : long response_code,
143 : const void *response)
144 : {
145 0 : struct TALER_EXCHANGE_PostRecoupRefreshHandle *ph = cls;
146 0 : const json_t *j = response;
147 0 : struct TALER_EXCHANGE_PostRecoupRefreshResponse rrr = {
148 : .hr.reply = j,
149 0 : .hr.http_status = (unsigned int) response_code
150 : };
151 :
152 0 : ph->job = NULL;
153 0 : switch (response_code)
154 : {
155 0 : case 0:
156 0 : rrr.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 : rrr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
165 0 : rrr.hr.http_status = 0;
166 0 : break;
167 : }
168 0 : TALER_EXCHANGE_post_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 : rrr.hr.ec = TALER_JSON_get_error_code (j);
174 0 : rrr.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 : rrr.hr.ec = TALER_JSON_get_error_code (j);
181 0 : rrr.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 : rrr.hr.ec = TALER_JSON_get_error_code (j);
187 0 : rrr.hr.hint = TALER_JSON_get_error_hint (j);
188 0 : break;
189 0 : case MHD_HTTP_CONFLICT:
190 0 : rrr.hr.ec = TALER_JSON_get_error_code (j);
191 0 : rrr.hr.hint = TALER_JSON_get_error_hint (j);
192 0 : break;
193 0 : case MHD_HTTP_GONE:
194 : /* Kind of normal: the money was already sent to the merchant
195 : (it was too late for the refund). */
196 0 : rrr.hr.ec = TALER_JSON_get_error_code (j);
197 0 : rrr.hr.hint = TALER_JSON_get_error_hint (j);
198 0 : break;
199 0 : case MHD_HTTP_INTERNAL_SERVER_ERROR:
200 : /* Server had an internal issue; we should retry, but this API
201 : leaves this to the application */
202 0 : rrr.hr.ec = TALER_JSON_get_error_code (j);
203 0 : rrr.hr.hint = TALER_JSON_get_error_hint (j);
204 0 : break;
205 0 : default:
206 : /* unexpected response code */
207 0 : rrr.hr.ec = TALER_JSON_get_error_code (j);
208 0 : rrr.hr.hint = TALER_JSON_get_error_hint (j);
209 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
210 : "Unexpected response code %u/%d for exchange recoup-refresh\n",
211 : (unsigned int) response_code,
212 : (int) rrr.hr.ec);
213 0 : GNUNET_break (0);
214 0 : break;
215 : }
216 0 : ph->cb (ph->cb_cls,
217 : &rrr);
218 0 : TALER_EXCHANGE_post_recoup_refresh_cancel (ph);
219 : }
220 :
221 :
222 : struct TALER_EXCHANGE_PostRecoupRefreshHandle *
223 0 : TALER_EXCHANGE_post_recoup_refresh_create (
224 : struct GNUNET_CURL_Context *ctx,
225 : const char *url,
226 : struct TALER_EXCHANGE_Keys *keys,
227 : const struct TALER_EXCHANGE_DenomPublicKey *pk,
228 : const struct TALER_DenominationSignature *denom_sig,
229 : const struct TALER_ExchangeBlindingValues *exchange_vals,
230 : const struct TALER_PublicRefreshMasterSeedP *rms,
231 : const struct TALER_PlanchetMasterSecretP *ps,
232 : unsigned int idx)
233 : {
234 : struct TALER_EXCHANGE_PostRecoupRefreshHandle *ph;
235 : struct TALER_DenominationHashP h_denom_pub;
236 : struct TALER_CoinSpendPrivateKeyP coin_priv;
237 : union GNUNET_CRYPTO_BlindingSecretP bks;
238 : struct TALER_CoinSpendSignatureP coin_sig;
239 :
240 : (void) rms; // FIXME: why did we pass this again?
241 : (void) idx; // FIXME: why did we pass this again?
242 0 : ph = GNUNET_new (struct TALER_EXCHANGE_PostRecoupRefreshHandle);
243 0 : ph->ctx = ctx;
244 0 : ph->base_url = GNUNET_strdup (url);
245 0 : ph->keys = TALER_EXCHANGE_keys_incref (keys);
246 0 : TALER_planchet_setup_coin_priv (ps,
247 : exchange_vals,
248 : &coin_priv);
249 0 : TALER_planchet_blinding_secret_create (ps,
250 : exchange_vals,
251 : &bks);
252 0 : GNUNET_CRYPTO_eddsa_key_get_public (&coin_priv.eddsa_priv,
253 : &ph->coin_pub.eddsa_pub);
254 0 : TALER_denom_pub_hash (&pk->key,
255 : &h_denom_pub);
256 0 : TALER_wallet_recoup_refresh_sign (&h_denom_pub,
257 : &bks,
258 : &coin_priv,
259 : &coin_sig);
260 0 : ph->body = GNUNET_JSON_PACK (
261 : GNUNET_JSON_pack_data_auto ("denom_pub_hash",
262 : &h_denom_pub),
263 : TALER_JSON_pack_denom_sig ("denom_sig",
264 : denom_sig),
265 : TALER_JSON_pack_exchange_blinding_values ("ewv",
266 : exchange_vals),
267 : GNUNET_JSON_pack_data_auto ("coin_sig",
268 : &coin_sig),
269 : GNUNET_JSON_pack_data_auto ("coin_blind_key_secret",
270 : &bks));
271 0 : switch (denom_sig->unblinded_sig->cipher)
272 : {
273 0 : case GNUNET_CRYPTO_BSA_INVALID:
274 0 : json_decref (ph->body);
275 0 : GNUNET_free (ph->base_url);
276 0 : TALER_EXCHANGE_keys_decref (ph->keys);
277 0 : GNUNET_free (ph);
278 0 : GNUNET_break (0);
279 0 : return NULL;
280 0 : case GNUNET_CRYPTO_BSA_RSA:
281 0 : break;
282 0 : case GNUNET_CRYPTO_BSA_CS:
283 0 : break;
284 : }
285 0 : return ph;
286 : }
287 :
288 :
289 : enum TALER_ErrorCode
290 0 : TALER_EXCHANGE_post_recoup_refresh_start (
291 : struct TALER_EXCHANGE_PostRecoupRefreshHandle *ph,
292 : TALER_EXCHANGE_PostRecoupRefreshCallback cb,
293 : TALER_EXCHANGE_POST_RECOUP_REFRESH_RESULT_CLOSURE *cb_cls)
294 : {
295 : CURL *eh;
296 : char arg_str[sizeof (struct TALER_CoinSpendPublicKeyP) * 2 + 32];
297 :
298 0 : ph->cb = cb;
299 0 : ph->cb_cls = cb_cls;
300 : {
301 : char pub_str[sizeof (struct TALER_CoinSpendPublicKeyP) * 2];
302 : char *end;
303 :
304 0 : end = GNUNET_STRINGS_data_to_string (
305 0 : &ph->coin_pub,
306 : sizeof (struct TALER_CoinSpendPublicKeyP),
307 : pub_str,
308 : sizeof (pub_str));
309 0 : *end = '\0';
310 0 : GNUNET_snprintf (arg_str,
311 : sizeof (arg_str),
312 : "coins/%s/recoup-refresh",
313 : pub_str);
314 : }
315 0 : ph->url = TALER_url_join (ph->base_url,
316 : arg_str,
317 : NULL);
318 0 : if (NULL == ph->url)
319 : {
320 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
321 : "Could not construct request URL.\n");
322 0 : return TALER_EC_GENERIC_CONFIGURATION_INVALID;
323 : }
324 0 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
325 : "URL for recoup-refresh: `%s'\n",
326 : ph->url);
327 0 : eh = TALER_EXCHANGE_curl_easy_get_ (ph->url);
328 0 : if ( (NULL == eh) ||
329 : (GNUNET_OK !=
330 0 : TALER_curl_easy_post (&ph->post_ctx,
331 : eh,
332 0 : ph->body)) )
333 : {
334 0 : GNUNET_break (0);
335 0 : if (NULL != eh)
336 0 : curl_easy_cleanup (eh);
337 0 : return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
338 : }
339 0 : ph->job = GNUNET_CURL_job_add2 (ph->ctx,
340 : eh,
341 0 : ph->post_ctx.headers,
342 : &handle_recoup_refresh_finished,
343 : ph);
344 0 : if (NULL == ph->job)
345 0 : return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
346 0 : return TALER_EC_NONE;
347 : }
348 :
349 :
350 : void
351 0 : TALER_EXCHANGE_post_recoup_refresh_cancel (
352 : struct TALER_EXCHANGE_PostRecoupRefreshHandle *ph)
353 : {
354 0 : if (NULL != ph->job)
355 : {
356 0 : GNUNET_CURL_job_cancel (ph->job);
357 0 : ph->job = NULL;
358 : }
359 0 : TALER_curl_easy_post_finished (&ph->post_ctx);
360 0 : GNUNET_free (ph->url);
361 0 : GNUNET_free (ph->base_url);
362 0 : json_decref (ph->body);
363 0 : TALER_EXCHANGE_keys_decref (ph->keys);
364 0 : GNUNET_free (ph);
365 0 : }
366 :
367 :
368 : /* end of exchange_api_post-recoup-refresh.c */
|