Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2015-2024 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_revoke_denomination_key.c
19 : * @brief functions to revoke an exchange denomination 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 "exchange_api_curl_defaults.h"
28 : #include "taler/taler_signatures.h"
29 : #include "taler/taler_curl_lib.h"
30 : #include "taler/taler_json_lib.h"
31 :
32 :
33 : /**
34 : * @brief Handle for a POST /management/denominations/$H_DENOM_PUB/revoke request.
35 : */
36 : struct TALER_EXCHANGE_ManagementRevokeDenominationKeyHandle
37 : {
38 :
39 : /**
40 : * The url for this request.
41 : */
42 : char *url;
43 :
44 : /**
45 : * Minor context that holds body and headers.
46 : */
47 : struct TALER_CURL_PostContext post_ctx;
48 :
49 : /**
50 : * Handle for the request.
51 : */
52 : struct GNUNET_CURL_Job *job;
53 :
54 : /**
55 : * Function to call with the result.
56 : */
57 : TALER_EXCHANGE_ManagementRevokeDenominationKeyCallback cb;
58 :
59 : /**
60 : * Closure for @a cb.
61 : */
62 : void *cb_cls;
63 :
64 : /**
65 : * Reference to the execution context.
66 : */
67 : struct GNUNET_CURL_Context *ctx;
68 : };
69 :
70 :
71 : /**
72 : * Function called when we're done processing the
73 : * HTTP /management/denominations/$H_DENOM_PUB/revoke request.
74 : *
75 : * @param cls the `struct TALER_EXCHANGE_ManagementRevokeDenominationKeyHandle *`
76 : * @param response_code HTTP response code, 0 on error
77 : * @param response response body, NULL if not in JSON
78 : */
79 : static void
80 0 : handle_revoke_denomination_finished (void *cls,
81 : long response_code,
82 : const void *response)
83 : {
84 0 : struct TALER_EXCHANGE_ManagementRevokeDenominationKeyHandle *rh = cls;
85 0 : const json_t *json = response;
86 0 : struct TALER_EXCHANGE_ManagementRevokeDenominationResponse rdr = {
87 0 : .hr.http_status = (unsigned int) response_code,
88 : .hr.reply = json
89 : };
90 :
91 0 : rh->job = NULL;
92 0 : switch (response_code)
93 : {
94 0 : case 0:
95 : /* no reply */
96 0 : rdr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
97 0 : rdr.hr.hint = "server offline?";
98 0 : break;
99 0 : case MHD_HTTP_NO_CONTENT:
100 0 : break;
101 0 : case MHD_HTTP_FORBIDDEN:
102 0 : rdr.hr.ec = TALER_JSON_get_error_code (json);
103 0 : rdr.hr.hint = TALER_JSON_get_error_hint (json);
104 0 : break;
105 0 : default:
106 : /* unexpected response code */
107 0 : GNUNET_break_op (0);
108 0 : rdr.hr.ec = TALER_JSON_get_error_code (json);
109 0 : rdr.hr.hint = TALER_JSON_get_error_hint (json);
110 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
111 : "Unexpected response code %u/%d for exchange management revoke denomination\n",
112 : (unsigned int) response_code,
113 : (int) rdr.hr.ec);
114 0 : break;
115 : }
116 0 : if (NULL != rh->cb)
117 : {
118 0 : rh->cb (rh->cb_cls,
119 : &rdr);
120 0 : rh->cb = NULL;
121 : }
122 0 : TALER_EXCHANGE_management_revoke_denomination_key_cancel (rh);
123 0 : }
124 :
125 :
126 : struct TALER_EXCHANGE_ManagementRevokeDenominationKeyHandle *
127 0 : TALER_EXCHANGE_management_revoke_denomination_key (
128 : struct GNUNET_CURL_Context *ctx,
129 : const char *url,
130 : const struct TALER_DenominationHashP *h_denom_pub,
131 : const struct TALER_MasterSignatureP *master_sig,
132 : TALER_EXCHANGE_ManagementRevokeDenominationKeyCallback cb,
133 : void *cb_cls)
134 : {
135 : struct TALER_EXCHANGE_ManagementRevokeDenominationKeyHandle *rh;
136 : CURL *eh;
137 : json_t *body;
138 :
139 0 : rh = GNUNET_new (struct TALER_EXCHANGE_ManagementRevokeDenominationKeyHandle);
140 0 : rh->cb = cb;
141 0 : rh->cb_cls = cb_cls;
142 0 : rh->ctx = ctx;
143 : {
144 : char epub_str[sizeof (*h_denom_pub) * 2];
145 : char arg_str[sizeof (epub_str) + 64];
146 : char *end;
147 :
148 0 : end = GNUNET_STRINGS_data_to_string (h_denom_pub,
149 : sizeof (*h_denom_pub),
150 : epub_str,
151 : sizeof (epub_str));
152 0 : *end = '\0';
153 0 : GNUNET_snprintf (arg_str,
154 : sizeof (arg_str),
155 : "management/denominations/%s/revoke",
156 : epub_str);
157 0 : rh->url = TALER_url_join (url,
158 : arg_str,
159 : NULL);
160 : }
161 0 : if (NULL == rh->url)
162 : {
163 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
164 : "Could not construct request URL.\n");
165 0 : GNUNET_free (rh);
166 0 : return NULL;
167 : }
168 0 : body = GNUNET_JSON_PACK (
169 : GNUNET_JSON_pack_data_auto ("master_sig",
170 : master_sig));
171 0 : if (NULL == body)
172 : {
173 0 : GNUNET_break (0);
174 0 : GNUNET_free (rh->url);
175 0 : GNUNET_free (rh);
176 0 : return NULL;
177 : }
178 0 : eh = TALER_EXCHANGE_curl_easy_get_ (rh->url);
179 0 : if ( (NULL == eh) ||
180 : (GNUNET_OK !=
181 0 : TALER_curl_easy_post (&rh->post_ctx,
182 : eh,
183 : body)) )
184 : {
185 0 : GNUNET_break (0);
186 0 : if (NULL != eh)
187 0 : curl_easy_cleanup (eh);
188 0 : json_decref (body);
189 0 : GNUNET_free (rh->url);
190 0 : GNUNET_free (rh);
191 0 : return NULL;
192 : }
193 0 : json_decref (body);
194 0 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
195 : "Requesting URL '%s'\n",
196 : rh->url);
197 0 : rh->job = GNUNET_CURL_job_add2 (ctx,
198 : eh,
199 0 : rh->post_ctx.headers,
200 : &handle_revoke_denomination_finished,
201 : rh);
202 0 : if (NULL == rh->job)
203 : {
204 0 : TALER_EXCHANGE_management_revoke_denomination_key_cancel (rh);
205 0 : return NULL;
206 : }
207 0 : return rh;
208 : }
209 :
210 :
211 : void
212 0 : TALER_EXCHANGE_management_revoke_denomination_key_cancel (
213 : struct TALER_EXCHANGE_ManagementRevokeDenominationKeyHandle *rh)
214 : {
215 0 : if (NULL != rh->job)
216 : {
217 0 : GNUNET_CURL_job_cancel (rh->job);
218 0 : rh->job = NULL;
219 : }
220 0 : TALER_curl_easy_post_finished (&rh->post_ctx);
221 0 : GNUNET_free (rh->url);
222 0 : GNUNET_free (rh);
223 0 : }
|