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