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