exchange_api_management_post_keys.c (6915B)
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 "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/keys request. 35 */ 36 struct TALER_EXCHANGE_ManagementPostKeysHandle 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_ManagementPostKeysCallback 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 POST /management/keys request. 74 * 75 * @param cls the `struct TALER_EXCHANGE_ManagementPostKeysHandle *` 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 handle_post_keys_finished (void *cls, 81 long response_code, 82 const void *response) 83 { 84 struct TALER_EXCHANGE_ManagementPostKeysHandle *ph = cls; 85 const json_t *json = response; 86 struct TALER_EXCHANGE_ManagementPostKeysResponse pkr = { 87 .hr.http_status = (unsigned int) response_code, 88 .hr.reply = json 89 }; 90 91 ph->job = NULL; 92 switch (response_code) 93 { 94 case MHD_HTTP_NO_CONTENT: 95 break; 96 case MHD_HTTP_FORBIDDEN: 97 pkr.hr.ec = TALER_JSON_get_error_code (json); 98 pkr.hr.hint = TALER_JSON_get_error_hint (json); 99 break; 100 case MHD_HTTP_NOT_FOUND: 101 pkr.hr.ec = TALER_JSON_get_error_code (json); 102 pkr.hr.hint = TALER_JSON_get_error_hint (json); 103 break; 104 case MHD_HTTP_REQUEST_ENTITY_TOO_LARGE: 105 pkr.hr.ec = TALER_JSON_get_error_code (json); 106 pkr.hr.hint = TALER_JSON_get_error_hint (json); 107 break; 108 default: 109 /* unexpected response code */ 110 GNUNET_break_op (0); 111 pkr.hr.ec = TALER_JSON_get_error_code (json); 112 pkr.hr.hint = TALER_JSON_get_error_hint (json); 113 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 114 "Unexpected response code %u/%d for exchange management post keys\n", 115 (unsigned int) response_code, 116 (int) pkr.hr.ec); 117 break; 118 } 119 if (NULL != ph->cb) 120 { 121 ph->cb (ph->cb_cls, 122 &pkr); 123 ph->cb = NULL; 124 } 125 TALER_EXCHANGE_post_management_keys_cancel (ph); 126 } 127 128 129 struct TALER_EXCHANGE_ManagementPostKeysHandle * 130 TALER_EXCHANGE_post_management_keys ( 131 struct GNUNET_CURL_Context *ctx, 132 const char *url, 133 const struct TALER_EXCHANGE_ManagementPostKeysData *pkd, 134 TALER_EXCHANGE_ManagementPostKeysCallback cb, 135 void *cb_cls) 136 { 137 struct TALER_EXCHANGE_ManagementPostKeysHandle *ph; 138 CURL *eh; 139 json_t *body; 140 json_t *denom_sigs; 141 json_t *signkey_sigs; 142 143 ph = GNUNET_new (struct TALER_EXCHANGE_ManagementPostKeysHandle); 144 ph->cb = cb; 145 ph->cb_cls = cb_cls; 146 ph->ctx = ctx; 147 ph->url = TALER_url_join (url, 148 "management/keys", 149 NULL); 150 if (NULL == ph->url) 151 { 152 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 153 "Could not construct request URL.\n"); 154 GNUNET_free (ph); 155 return NULL; 156 } 157 denom_sigs = json_array (); 158 GNUNET_assert (NULL != denom_sigs); 159 for (unsigned int i = 0; i<pkd->num_denom_sigs; i++) 160 { 161 const struct TALER_EXCHANGE_DenominationKeySignature *dks 162 = &pkd->denom_sigs[i]; 163 164 GNUNET_assert (0 == 165 json_array_append_new ( 166 denom_sigs, 167 GNUNET_JSON_PACK ( 168 GNUNET_JSON_pack_data_auto ("h_denom_pub", 169 &dks->h_denom_pub), 170 GNUNET_JSON_pack_data_auto ("master_sig", 171 &dks->master_sig)))); 172 } 173 signkey_sigs = json_array (); 174 GNUNET_assert (NULL != signkey_sigs); 175 for (unsigned int i = 0; i<pkd->num_sign_sigs; i++) 176 { 177 const struct TALER_EXCHANGE_SigningKeySignature *sks 178 = &pkd->sign_sigs[i]; 179 180 GNUNET_assert (0 == 181 json_array_append_new ( 182 signkey_sigs, 183 GNUNET_JSON_PACK ( 184 GNUNET_JSON_pack_data_auto ("exchange_pub", 185 &sks->exchange_pub), 186 GNUNET_JSON_pack_data_auto ("master_sig", 187 &sks->master_sig)))); 188 } 189 body = GNUNET_JSON_PACK ( 190 GNUNET_JSON_pack_array_steal ("denom_sigs", 191 denom_sigs), 192 GNUNET_JSON_pack_array_steal ("signkey_sigs", 193 signkey_sigs)); 194 eh = TALER_EXCHANGE_curl_easy_get_ (ph->url); 195 if ( (NULL == eh) || 196 (GNUNET_OK != 197 TALER_curl_easy_post (&ph->post_ctx, 198 eh, 199 body)) ) 200 { 201 GNUNET_break (0); 202 if (NULL != eh) 203 curl_easy_cleanup (eh); 204 json_decref (body); 205 GNUNET_free (ph->url); 206 return NULL; 207 } 208 json_decref (body); 209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 210 "Requesting URL '%s'\n", 211 ph->url); 212 ph->job = GNUNET_CURL_job_add2 (ctx, 213 eh, 214 ph->post_ctx.headers, 215 &handle_post_keys_finished, 216 ph); 217 if (NULL == ph->job) 218 { 219 TALER_EXCHANGE_post_management_keys_cancel (ph); 220 return NULL; 221 } 222 return ph; 223 } 224 225 226 void 227 TALER_EXCHANGE_post_management_keys_cancel ( 228 struct TALER_EXCHANGE_ManagementPostKeysHandle *ph) 229 { 230 if (NULL != ph->job) 231 { 232 GNUNET_CURL_job_cancel (ph->job); 233 ph->job = NULL; 234 } 235 TALER_curl_easy_post_finished (&ph->post_ctx); 236 GNUNET_free (ph->url); 237 GNUNET_free (ph); 238 }