Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2015-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-management-keys.c
19 : * @brief functions to affirm the validity of exchange keys using the master private key
20 : * @author Christian Grothoff
21 : */
22 : #include "taler/platform.h"
23 : #include "taler/taler_json_lib.h"
24 : #include <gnunet/gnunet_curl_lib.h>
25 : #include <microhttpd.h>
26 : #include "taler/taler_exchange_service.h"
27 : #include "taler/taler-exchange/post-management-keys.h"
28 : #include "exchange_api_curl_defaults.h"
29 : #include "taler/taler_signatures.h"
30 : #include "taler/taler_curl_lib.h"
31 :
32 :
33 : /**
34 : * @brief Handle for a POST /management/keys request.
35 : */
36 : struct TALER_EXCHANGE_PostManagementKeysHandle
37 : {
38 :
39 : /**
40 : * The base URL for this request.
41 : */
42 : char *base_url;
43 :
44 : /**
45 : * The full URL for this request, set during _start.
46 : */
47 : char *url;
48 :
49 : /**
50 : * Minor context that holds body and headers.
51 : */
52 : struct TALER_CURL_PostContext post_ctx;
53 :
54 : /**
55 : * Handle for the request.
56 : */
57 : struct GNUNET_CURL_Job *job;
58 :
59 : /**
60 : * Function to call with the result.
61 : */
62 : TALER_EXCHANGE_PostManagementKeysCallback cb;
63 :
64 : /**
65 : * Closure for @a cb.
66 : */
67 : TALER_EXCHANGE_POST_MANAGEMENT_KEYS_RESULT_CLOSURE *cb_cls;
68 :
69 : /**
70 : * Reference to the execution context.
71 : */
72 : struct GNUNET_CURL_Context *ctx;
73 :
74 : /**
75 : * Array of master signatures for the exchange's online signing keys.
76 : */
77 : struct TALER_EXCHANGE_SigningKeySignature *sign_sigs;
78 :
79 : /**
80 : * Length of the @e sign_sigs array.
81 : */
82 : unsigned int num_sign_sigs;
83 :
84 : /**
85 : * Array of master signatures for the exchange's denomination keys.
86 : */
87 : struct TALER_EXCHANGE_DenominationKeySignature *denom_sigs;
88 :
89 : /**
90 : * Length of the @e denom_sigs array.
91 : */
92 : unsigned int num_denom_sigs;
93 :
94 : };
95 :
96 :
97 : /**
98 : * Function called when we're done processing the
99 : * HTTP POST /management/keys request.
100 : *
101 : * @param cls the `struct TALER_EXCHANGE_PostManagementKeysHandle`
102 : * @param response_code HTTP response code, 0 on error
103 : * @param response response body, NULL if not in JSON
104 : */
105 : static void
106 19 : handle_post_keys_finished (void *cls,
107 : long response_code,
108 : const void *response)
109 : {
110 19 : struct TALER_EXCHANGE_PostManagementKeysHandle *pmkh = cls;
111 19 : const json_t *json = response;
112 19 : struct TALER_EXCHANGE_PostManagementKeysResponse res = {
113 19 : .hr.http_status = (unsigned int) response_code,
114 : .hr.reply = json
115 : };
116 :
117 19 : pmkh->job = NULL;
118 19 : switch (response_code)
119 : {
120 19 : case MHD_HTTP_NO_CONTENT:
121 19 : break;
122 0 : case MHD_HTTP_FORBIDDEN:
123 0 : res.hr.ec = TALER_JSON_get_error_code (json);
124 0 : res.hr.hint = TALER_JSON_get_error_hint (json);
125 0 : break;
126 0 : case MHD_HTTP_NOT_FOUND:
127 0 : res.hr.ec = TALER_JSON_get_error_code (json);
128 0 : res.hr.hint = TALER_JSON_get_error_hint (json);
129 0 : break;
130 0 : case MHD_HTTP_CONFLICT:
131 0 : res.hr.ec = TALER_JSON_get_error_code (json);
132 0 : res.hr.hint = TALER_JSON_get_error_hint (json);
133 0 : break;
134 0 : case MHD_HTTP_REQUEST_ENTITY_TOO_LARGE:
135 0 : res.hr.ec = TALER_JSON_get_error_code (json);
136 0 : res.hr.hint = TALER_JSON_get_error_hint (json);
137 0 : break;
138 0 : default:
139 : /* unexpected response code */
140 0 : GNUNET_break_op (0);
141 0 : res.hr.ec = TALER_JSON_get_error_code (json);
142 0 : res.hr.hint = TALER_JSON_get_error_hint (json);
143 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
144 : "Unexpected response code %u/%d for exchange management post keys\n",
145 : (unsigned int) response_code,
146 : (int) res.hr.ec);
147 0 : break;
148 : }
149 19 : if (NULL != pmkh->cb)
150 : {
151 19 : pmkh->cb (pmkh->cb_cls,
152 : &res);
153 19 : pmkh->cb = NULL;
154 : }
155 19 : TALER_EXCHANGE_post_management_keys_cancel (pmkh);
156 19 : }
157 :
158 :
159 : struct TALER_EXCHANGE_PostManagementKeysHandle *
160 19 : TALER_EXCHANGE_post_management_keys_create (
161 : struct GNUNET_CURL_Context *ctx,
162 : const char *url,
163 : const struct TALER_EXCHANGE_ManagementPostKeysData *pkd)
164 : {
165 : struct TALER_EXCHANGE_PostManagementKeysHandle *pmkh;
166 :
167 19 : pmkh = GNUNET_new (struct TALER_EXCHANGE_PostManagementKeysHandle);
168 19 : pmkh->ctx = ctx;
169 19 : pmkh->base_url = GNUNET_strdup (url);
170 19 : pmkh->num_sign_sigs = pkd->num_sign_sigs;
171 19 : pmkh->num_denom_sigs = pkd->num_denom_sigs;
172 19 : pmkh->sign_sigs = GNUNET_memdup (pkd->sign_sigs,
173 : pkd->num_sign_sigs
174 : * sizeof (struct
175 : TALER_EXCHANGE_SigningKeySignature)
176 : );
177 19 : pmkh->denom_sigs = GNUNET_memdup (pkd->denom_sigs,
178 : pkd->num_denom_sigs
179 : * sizeof (struct
180 : TALER_EXCHANGE_DenominationKeySignature));
181 19 : return pmkh;
182 : }
183 :
184 :
185 : enum TALER_ErrorCode
186 19 : TALER_EXCHANGE_post_management_keys_start (
187 : struct TALER_EXCHANGE_PostManagementKeysHandle *pmkh,
188 : TALER_EXCHANGE_PostManagementKeysCallback cb,
189 : TALER_EXCHANGE_POST_MANAGEMENT_KEYS_RESULT_CLOSURE *cb_cls)
190 : {
191 : CURL *eh;
192 : json_t *body;
193 : json_t *denom_sigs;
194 : json_t *signkey_sigs;
195 :
196 19 : pmkh->cb = cb;
197 19 : pmkh->cb_cls = cb_cls;
198 19 : pmkh->url = TALER_url_join (pmkh->base_url,
199 : "management/keys",
200 : NULL);
201 19 : if (NULL == pmkh->url)
202 : {
203 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
204 : "Could not construct request URL.\n");
205 0 : return TALER_EC_GENERIC_CONFIGURATION_INVALID;
206 : }
207 19 : denom_sigs = json_array ();
208 19 : GNUNET_assert (NULL != denom_sigs);
209 372 : for (unsigned int i = 0; i < pmkh->num_denom_sigs; i++)
210 : {
211 353 : const struct TALER_EXCHANGE_DenominationKeySignature *dks
212 353 : = &pmkh->denom_sigs[i];
213 :
214 353 : GNUNET_assert (0 ==
215 : json_array_append_new (
216 : denom_sigs,
217 : GNUNET_JSON_PACK (
218 : GNUNET_JSON_pack_data_auto ("h_denom_pub",
219 : &dks->h_denom_pub),
220 : GNUNET_JSON_pack_data_auto ("master_sig",
221 : &dks->master_sig))));
222 : }
223 19 : signkey_sigs = json_array ();
224 19 : GNUNET_assert (NULL != signkey_sigs);
225 40 : for (unsigned int i = 0; i < pmkh->num_sign_sigs; i++)
226 : {
227 21 : const struct TALER_EXCHANGE_SigningKeySignature *sks
228 21 : = &pmkh->sign_sigs[i];
229 :
230 21 : GNUNET_assert (0 ==
231 : json_array_append_new (
232 : signkey_sigs,
233 : GNUNET_JSON_PACK (
234 : GNUNET_JSON_pack_data_auto ("exchange_pub",
235 : &sks->exchange_pub),
236 : GNUNET_JSON_pack_data_auto ("master_sig",
237 : &sks->master_sig))));
238 : }
239 19 : body = GNUNET_JSON_PACK (
240 : GNUNET_JSON_pack_array_steal ("denom_sigs",
241 : denom_sigs),
242 : GNUNET_JSON_pack_array_steal ("signkey_sigs",
243 : signkey_sigs));
244 19 : eh = TALER_EXCHANGE_curl_easy_get_ (pmkh->url);
245 38 : if ( (NULL == eh) ||
246 : (GNUNET_OK !=
247 19 : TALER_curl_easy_post (&pmkh->post_ctx,
248 : eh,
249 : body)) )
250 : {
251 0 : GNUNET_break (0);
252 0 : if (NULL != eh)
253 0 : curl_easy_cleanup (eh);
254 0 : json_decref (body);
255 0 : GNUNET_free (pmkh->url);
256 0 : pmkh->url = NULL;
257 0 : return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
258 : }
259 19 : json_decref (body);
260 19 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
261 : "Requesting URL '%s'\n",
262 : pmkh->url);
263 38 : pmkh->job = GNUNET_CURL_job_add2 (pmkh->ctx,
264 : eh,
265 19 : pmkh->post_ctx.headers,
266 : &handle_post_keys_finished,
267 : pmkh);
268 19 : if (NULL == pmkh->job)
269 : {
270 0 : TALER_curl_easy_post_finished (&pmkh->post_ctx);
271 0 : GNUNET_free (pmkh->url);
272 0 : pmkh->url = NULL;
273 0 : return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
274 : }
275 19 : return TALER_EC_NONE;
276 : }
277 :
278 :
279 : void
280 19 : TALER_EXCHANGE_post_management_keys_cancel (
281 : struct TALER_EXCHANGE_PostManagementKeysHandle *pmkh)
282 : {
283 19 : if (NULL != pmkh->job)
284 : {
285 0 : GNUNET_CURL_job_cancel (pmkh->job);
286 0 : pmkh->job = NULL;
287 : }
288 19 : TALER_curl_easy_post_finished (&pmkh->post_ctx);
289 19 : GNUNET_free (pmkh->sign_sigs);
290 19 : GNUNET_free (pmkh->denom_sigs);
291 19 : GNUNET_free (pmkh->url);
292 19 : GNUNET_free (pmkh->base_url);
293 19 : GNUNET_free (pmkh);
294 19 : }
295 :
296 :
297 : /* end of exchange_api_post-management-keys.c */
|