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