Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2014-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-reserves-attest-RESERVE_PUB.c
19 : * @brief Implementation of the POST /reserves-attest/$RESERVE_PUB requests
20 : * @author Christian Grothoff
21 : */
22 : #include "taler/platform.h"
23 : #include <jansson.h>
24 : #include <microhttpd.h> /* just for HTTP attest 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_exchange_service.h"
29 : #include "taler/taler_json_lib.h"
30 : #include "exchange_api_handle.h"
31 : #include "taler/taler_signatures.h"
32 : #include "exchange_api_curl_defaults.h"
33 :
34 :
35 : /**
36 : * @brief A POST /reserves-attest/$RID Handle
37 : */
38 : struct TALER_EXCHANGE_PostReservesAttestHandle
39 : {
40 :
41 : /**
42 : * Reference to the execution context.
43 : */
44 : struct GNUNET_CURL_Context *ctx;
45 :
46 : /**
47 : * Base URL of the exchange.
48 : */
49 : char *base_url;
50 :
51 : /**
52 : * The url for this request, set during _start.
53 : */
54 : char *url;
55 :
56 : /**
57 : * Context for #TEH_curl_easy_post(). Keeps the data that must
58 : * persist for Curl to make the upload.
59 : */
60 : struct TALER_CURL_PostContext post_ctx;
61 :
62 : /**
63 : * Handle for the request.
64 : */
65 : struct GNUNET_CURL_Job *job;
66 :
67 : /**
68 : * Function to call with the result.
69 : */
70 : TALER_EXCHANGE_PostReservesAttestCallback cb;
71 :
72 : /**
73 : * Closure for @a cb.
74 : */
75 : TALER_EXCHANGE_POST_RESERVES_ATTEST_RESULT_CLOSURE *cb_cls;
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 reserve we are querying.
84 : */
85 : struct TALER_ReservePublicKeyP reserve_pub;
86 :
87 : /**
88 : * Pre-built JSON body for the request.
89 : */
90 : json_t *body;
91 :
92 : };
93 :
94 :
95 : /**
96 : * We received an #MHD_HTTP_OK attest code. Handle the JSON
97 : * response.
98 : *
99 : * @param prah handle of the request
100 : * @param j JSON response
101 : * @return #GNUNET_OK on success
102 : */
103 : static enum GNUNET_GenericReturnValue
104 1 : handle_reserves_attest_ok (struct TALER_EXCHANGE_PostReservesAttestHandle *prah,
105 : const json_t *j)
106 : {
107 1 : struct TALER_EXCHANGE_PostReservesAttestResponse rs = {
108 : .hr.reply = j,
109 : .hr.http_status = MHD_HTTP_OK
110 : };
111 : const json_t *attributes;
112 : struct GNUNET_JSON_Specification spec[] = {
113 1 : GNUNET_JSON_spec_timestamp ("exchange_timestamp",
114 : &rs.details.ok.exchange_time),
115 1 : GNUNET_JSON_spec_timestamp ("expiration_time",
116 : &rs.details.ok.expiration_time),
117 1 : GNUNET_JSON_spec_fixed_auto ("exchange_sig",
118 : &rs.details.ok.exchange_sig),
119 1 : GNUNET_JSON_spec_fixed_auto ("exchange_pub",
120 : &rs.details.ok.exchange_pub),
121 1 : GNUNET_JSON_spec_object_const ("attributes",
122 : &attributes),
123 1 : GNUNET_JSON_spec_end ()
124 : };
125 :
126 1 : if (GNUNET_OK !=
127 1 : GNUNET_JSON_parse (j,
128 : spec,
129 : NULL,
130 : NULL))
131 : {
132 0 : GNUNET_break_op (0);
133 0 : return GNUNET_SYSERR;
134 : }
135 1 : if (GNUNET_OK !=
136 1 : TALER_EXCHANGE_test_signing_key (prah->keys,
137 : &rs.details.ok.exchange_pub))
138 : {
139 0 : GNUNET_break_op (0);
140 0 : rs.hr.http_status = 0;
141 0 : rs.hr.ec = TALER_EC_EXCHANGE_DEPOSITS_GET_INVALID_SIGNATURE_BY_EXCHANGE;
142 0 : prah->cb (prah->cb_cls,
143 : &rs);
144 0 : prah->cb = NULL;
145 0 : GNUNET_JSON_parse_free (spec);
146 0 : return GNUNET_SYSERR;
147 : }
148 1 : rs.details.ok.attributes = attributes;
149 1 : if (GNUNET_OK !=
150 1 : TALER_exchange_online_reserve_attest_details_verify (
151 : rs.details.ok.exchange_time,
152 : rs.details.ok.expiration_time,
153 1 : &prah->reserve_pub,
154 : attributes,
155 : &rs.details.ok.exchange_pub,
156 : &rs.details.ok.exchange_sig))
157 : {
158 0 : GNUNET_break_op (0);
159 0 : GNUNET_JSON_parse_free (spec);
160 0 : return GNUNET_SYSERR;
161 : }
162 1 : prah->cb (prah->cb_cls,
163 : &rs);
164 1 : prah->cb = NULL;
165 1 : GNUNET_JSON_parse_free (spec);
166 1 : return GNUNET_OK;
167 : }
168 :
169 :
170 : /**
171 : * Function called when we're done processing the
172 : * HTTP /reserves-attest/$RID request.
173 : *
174 : * @param cls the `struct TALER_EXCHANGE_PostReservesAttestHandle`
175 : * @param response_code HTTP response code, 0 on error
176 : * @param response parsed JSON result, NULL on error
177 : */
178 : static void
179 1 : handle_reserves_attest_finished (void *cls,
180 : long response_code,
181 : const void *response)
182 : {
183 1 : struct TALER_EXCHANGE_PostReservesAttestHandle *prah = cls;
184 1 : const json_t *j = response;
185 1 : struct TALER_EXCHANGE_PostReservesAttestResponse rs = {
186 : .hr.reply = j,
187 1 : .hr.http_status = (unsigned int) response_code
188 : };
189 :
190 1 : prah->job = NULL;
191 1 : switch (response_code)
192 : {
193 0 : case 0:
194 0 : rs.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
195 0 : break;
196 1 : case MHD_HTTP_OK:
197 1 : if (GNUNET_OK !=
198 1 : handle_reserves_attest_ok (prah,
199 : j))
200 : {
201 0 : rs.hr.http_status = 0;
202 0 : rs.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
203 : }
204 1 : break;
205 0 : case MHD_HTTP_BAD_REQUEST:
206 : /* This should never happen, either us or the exchange is buggy
207 : (or API version conflict); just pass JSON reply to the application */
208 0 : GNUNET_break (0);
209 0 : rs.hr.ec = TALER_JSON_get_error_code (j);
210 0 : rs.hr.hint = TALER_JSON_get_error_hint (j);
211 0 : break;
212 0 : case MHD_HTTP_FORBIDDEN:
213 : /* This should never happen, either us or the exchange is buggy
214 : (or API version conflict); just pass JSON reply to the application */
215 0 : GNUNET_break (0);
216 0 : rs.hr.ec = TALER_JSON_get_error_code (j);
217 0 : rs.hr.hint = TALER_JSON_get_error_hint (j);
218 0 : break;
219 0 : case MHD_HTTP_NOT_FOUND:
220 : /* Nothing really to verify, this should never
221 : happen, we should pass the JSON reply to the application */
222 0 : rs.hr.ec = TALER_JSON_get_error_code (j);
223 0 : rs.hr.hint = TALER_JSON_get_error_hint (j);
224 0 : break;
225 0 : case MHD_HTTP_CONFLICT:
226 : /* Server doesn't have the requested attributes */
227 0 : rs.hr.ec = TALER_JSON_get_error_code (j);
228 0 : rs.hr.hint = TALER_JSON_get_error_hint (j);
229 0 : break;
230 0 : case MHD_HTTP_INTERNAL_SERVER_ERROR:
231 : /* Server had an internal issue; we should retry, but this API
232 : leaves this to the application */
233 0 : rs.hr.ec = TALER_JSON_get_error_code (j);
234 0 : rs.hr.hint = TALER_JSON_get_error_hint (j);
235 0 : break;
236 0 : default:
237 : /* unexpected response code */
238 0 : GNUNET_break_op (0);
239 0 : rs.hr.ec = TALER_JSON_get_error_code (j);
240 0 : rs.hr.hint = TALER_JSON_get_error_hint (j);
241 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
242 : "Unexpected response code %u/%d for reserves attest\n",
243 : (unsigned int) response_code,
244 : (int) rs.hr.ec);
245 0 : break;
246 : }
247 1 : if (NULL != prah->cb)
248 : {
249 0 : prah->cb (prah->cb_cls,
250 : &rs);
251 0 : prah->cb = NULL;
252 : }
253 1 : TALER_EXCHANGE_post_reserves_attest_cancel (prah);
254 1 : }
255 :
256 :
257 : struct TALER_EXCHANGE_PostReservesAttestHandle *
258 1 : TALER_EXCHANGE_post_reserves_attest_create (
259 : struct GNUNET_CURL_Context *ctx,
260 : const char *url,
261 : struct TALER_EXCHANGE_Keys *keys,
262 : const struct TALER_ReservePrivateKeyP *reserve_priv,
263 : unsigned int attributes_length,
264 : const char *attributes[const static attributes_length])
265 1 : {
266 : struct TALER_EXCHANGE_PostReservesAttestHandle *prah;
267 : struct TALER_ReserveSignatureP reserve_sig;
268 : json_t *details;
269 : struct GNUNET_TIME_Timestamp ts;
270 :
271 1 : if (0 == attributes_length)
272 : {
273 0 : GNUNET_break (0);
274 0 : return NULL;
275 : }
276 1 : details = json_array ();
277 1 : GNUNET_assert (NULL != details);
278 2 : for (unsigned int i = 0; i < attributes_length; i++)
279 : {
280 1 : GNUNET_assert (0 ==
281 : json_array_append_new (details,
282 : json_string (attributes[i])));
283 : }
284 1 : prah = GNUNET_new (struct TALER_EXCHANGE_PostReservesAttestHandle);
285 1 : prah->ctx = ctx;
286 1 : prah->base_url = GNUNET_strdup (url);
287 1 : prah->keys = TALER_EXCHANGE_keys_incref (keys);
288 1 : GNUNET_CRYPTO_eddsa_key_get_public (&reserve_priv->eddsa_priv,
289 : &prah->reserve_pub.eddsa_pub);
290 1 : ts = GNUNET_TIME_timestamp_get ();
291 1 : TALER_wallet_reserve_attest_request_sign (ts,
292 : details,
293 : reserve_priv,
294 : &reserve_sig);
295 1 : prah->body = GNUNET_JSON_PACK (
296 : GNUNET_JSON_pack_data_auto ("reserve_sig",
297 : &reserve_sig),
298 : GNUNET_JSON_pack_timestamp ("request_timestamp",
299 : ts),
300 : GNUNET_JSON_pack_array_steal ("details",
301 : details));
302 1 : if (NULL == prah->body)
303 : {
304 0 : GNUNET_break (0);
305 0 : GNUNET_free (prah->base_url);
306 0 : TALER_EXCHANGE_keys_decref (prah->keys);
307 0 : GNUNET_free (prah);
308 0 : return NULL;
309 : }
310 1 : return prah;
311 : }
312 :
313 :
314 : enum TALER_ErrorCode
315 1 : TALER_EXCHANGE_post_reserves_attest_start (
316 : struct TALER_EXCHANGE_PostReservesAttestHandle *prah,
317 : TALER_EXCHANGE_PostReservesAttestCallback cb,
318 : TALER_EXCHANGE_POST_RESERVES_ATTEST_RESULT_CLOSURE *cb_cls)
319 : {
320 : CURL *eh;
321 : char arg_str[sizeof (struct TALER_ReservePublicKeyP) * 2 + 32];
322 :
323 1 : prah->cb = cb;
324 1 : prah->cb_cls = cb_cls;
325 : {
326 : char pub_str[sizeof (struct TALER_ReservePublicKeyP) * 2];
327 : char *end;
328 :
329 1 : end = GNUNET_STRINGS_data_to_string (
330 1 : &prah->reserve_pub,
331 : sizeof (prah->reserve_pub),
332 : pub_str,
333 : sizeof (pub_str));
334 1 : *end = '\0';
335 1 : GNUNET_snprintf (arg_str,
336 : sizeof (arg_str),
337 : "reserves-attest/%s",
338 : pub_str);
339 : }
340 1 : prah->url = TALER_url_join (prah->base_url,
341 : arg_str,
342 : NULL);
343 1 : if (NULL == prah->url)
344 0 : return TALER_EC_GENERIC_CONFIGURATION_INVALID;
345 1 : eh = TALER_EXCHANGE_curl_easy_get_ (prah->url);
346 2 : if ( (NULL == eh) ||
347 : (GNUNET_OK !=
348 1 : TALER_curl_easy_post (&prah->post_ctx,
349 : eh,
350 1 : prah->body)) )
351 : {
352 0 : GNUNET_break (0);
353 0 : if (NULL != eh)
354 0 : curl_easy_cleanup (eh);
355 0 : GNUNET_free (prah->url);
356 0 : prah->url = NULL;
357 0 : return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
358 : }
359 2 : prah->job = GNUNET_CURL_job_add2 (prah->ctx,
360 : eh,
361 1 : prah->post_ctx.headers,
362 : &handle_reserves_attest_finished,
363 : prah);
364 1 : if (NULL == prah->job)
365 : {
366 0 : TALER_curl_easy_post_finished (&prah->post_ctx);
367 0 : GNUNET_free (prah->url);
368 0 : prah->url = NULL;
369 0 : return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
370 : }
371 1 : return TALER_EC_NONE;
372 : }
373 :
374 :
375 : void
376 1 : TALER_EXCHANGE_post_reserves_attest_cancel (
377 : struct TALER_EXCHANGE_PostReservesAttestHandle *prah)
378 : {
379 1 : if (NULL != prah->job)
380 : {
381 0 : GNUNET_CURL_job_cancel (prah->job);
382 0 : prah->job = NULL;
383 : }
384 1 : TALER_curl_easy_post_finished (&prah->post_ctx);
385 1 : json_decref (prah->body);
386 1 : GNUNET_free (prah->url);
387 1 : GNUNET_free (prah->base_url);
388 1 : TALER_EXCHANGE_keys_decref (prah->keys);
389 1 : GNUNET_free (prah);
390 1 : }
391 :
392 :
393 : /* end of exchange_api_post-reserves-attest-RESERVE_PUB.c */
|