exchange_api_post-reserves-attest-RESERVE_PUB.c (11630B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2014-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-reserves-attest-RESERVE_PUB.c 19 * @brief Implementation of the POST /reserves-attest/$RESERVE_PUB requests 20 * @author Christian Grothoff 21 */ 22 #include "taler/platform.h" 23 #include <jansson.h> 24 #include <microhttpd.h> /* just for HTTP attest codes */ 25 #include <gnunet/gnunet_util_lib.h> 26 #include <gnunet/gnunet_json_lib.h> 27 #include <gnunet/gnunet_curl_lib.h> 28 #include "taler/taler_exchange_service.h" 29 #include "taler/taler_json_lib.h" 30 #include "exchange_api_handle.h" 31 #include "taler/taler_signatures.h" 32 #include "exchange_api_curl_defaults.h" 33 34 35 /** 36 * @brief A POST /reserves-attest/$RID Handle 37 */ 38 struct TALER_EXCHANGE_PostReservesAttestHandle 39 { 40 41 /** 42 * Reference to the execution context. 43 */ 44 struct GNUNET_CURL_Context *ctx; 45 46 /** 47 * Base URL of the exchange. 48 */ 49 char *base_url; 50 51 /** 52 * The url for this request, set during _start. 53 */ 54 char *url; 55 56 /** 57 * Context for #TEH_curl_easy_post(). Keeps the data that must 58 * persist for Curl to make the upload. 59 */ 60 struct TALER_CURL_PostContext post_ctx; 61 62 /** 63 * Handle for the request. 64 */ 65 struct GNUNET_CURL_Job *job; 66 67 /** 68 * Function to call with the result. 69 */ 70 TALER_EXCHANGE_PostReservesAttestCallback cb; 71 72 /** 73 * Closure for @a cb. 74 */ 75 TALER_EXCHANGE_POST_RESERVES_ATTEST_RESULT_CLOSURE *cb_cls; 76 77 /** 78 * The keys of the exchange this request handle will use. 79 */ 80 struct TALER_EXCHANGE_Keys *keys; 81 82 /** 83 * Public key of the reserve we are querying. 84 */ 85 struct TALER_ReservePublicKeyP reserve_pub; 86 87 /** 88 * Pre-built JSON body for the request. 89 */ 90 json_t *body; 91 92 }; 93 94 95 /** 96 * We received an #MHD_HTTP_OK attest code. Handle the JSON 97 * response. 98 * 99 * @param prah handle of the request 100 * @param j JSON response 101 * @return #GNUNET_OK on success 102 */ 103 static enum GNUNET_GenericReturnValue 104 handle_reserves_attest_ok (struct TALER_EXCHANGE_PostReservesAttestHandle *prah, 105 const json_t *j) 106 { 107 struct TALER_EXCHANGE_PostReservesAttestResponse rs = { 108 .hr.reply = j, 109 .hr.http_status = MHD_HTTP_OK 110 }; 111 const json_t *attributes; 112 struct GNUNET_JSON_Specification spec[] = { 113 GNUNET_JSON_spec_timestamp ("exchange_timestamp", 114 &rs.details.ok.exchange_time), 115 GNUNET_JSON_spec_timestamp ("expiration_time", 116 &rs.details.ok.expiration_time), 117 GNUNET_JSON_spec_fixed_auto ("exchange_sig", 118 &rs.details.ok.exchange_sig), 119 GNUNET_JSON_spec_fixed_auto ("exchange_pub", 120 &rs.details.ok.exchange_pub), 121 GNUNET_JSON_spec_object_const ("attributes", 122 &attributes), 123 GNUNET_JSON_spec_end () 124 }; 125 126 if (GNUNET_OK != 127 GNUNET_JSON_parse (j, 128 spec, 129 NULL, 130 NULL)) 131 { 132 GNUNET_break_op (0); 133 return GNUNET_SYSERR; 134 } 135 if (GNUNET_OK != 136 TALER_EXCHANGE_test_signing_key (prah->keys, 137 &rs.details.ok.exchange_pub)) 138 { 139 GNUNET_break_op (0); 140 rs.hr.http_status = 0; 141 rs.hr.ec = TALER_EC_EXCHANGE_DEPOSITS_GET_INVALID_SIGNATURE_BY_EXCHANGE; 142 prah->cb (prah->cb_cls, 143 &rs); 144 prah->cb = NULL; 145 GNUNET_JSON_parse_free (spec); 146 return GNUNET_SYSERR; 147 } 148 rs.details.ok.attributes = attributes; 149 if (GNUNET_OK != 150 TALER_exchange_online_reserve_attest_details_verify ( 151 rs.details.ok.exchange_time, 152 rs.details.ok.expiration_time, 153 &prah->reserve_pub, 154 attributes, 155 &rs.details.ok.exchange_pub, 156 &rs.details.ok.exchange_sig)) 157 { 158 GNUNET_break_op (0); 159 GNUNET_JSON_parse_free (spec); 160 return GNUNET_SYSERR; 161 } 162 prah->cb (prah->cb_cls, 163 &rs); 164 prah->cb = NULL; 165 GNUNET_JSON_parse_free (spec); 166 return GNUNET_OK; 167 } 168 169 170 /** 171 * Function called when we're done processing the 172 * HTTP /reserves-attest/$RID request. 173 * 174 * @param cls the `struct TALER_EXCHANGE_PostReservesAttestHandle` 175 * @param response_code HTTP response code, 0 on error 176 * @param response parsed JSON result, NULL on error 177 */ 178 static void 179 handle_reserves_attest_finished (void *cls, 180 long response_code, 181 const void *response) 182 { 183 struct TALER_EXCHANGE_PostReservesAttestHandle *prah = cls; 184 const json_t *j = response; 185 struct TALER_EXCHANGE_PostReservesAttestResponse rs = { 186 .hr.reply = j, 187 .hr.http_status = (unsigned int) response_code 188 }; 189 190 prah->job = NULL; 191 switch (response_code) 192 { 193 case 0: 194 rs.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; 195 break; 196 case MHD_HTTP_OK: 197 if (GNUNET_OK != 198 handle_reserves_attest_ok (prah, 199 j)) 200 { 201 rs.hr.http_status = 0; 202 rs.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED; 203 } 204 break; 205 case MHD_HTTP_BAD_REQUEST: 206 /* This should never happen, either us or the exchange is buggy 207 (or API version conflict); just pass JSON reply to the application */ 208 GNUNET_break (0); 209 rs.hr.ec = TALER_JSON_get_error_code (j); 210 rs.hr.hint = TALER_JSON_get_error_hint (j); 211 break; 212 case MHD_HTTP_FORBIDDEN: 213 /* This should never happen, either us or the exchange is buggy 214 (or API version conflict); just pass JSON reply to the application */ 215 GNUNET_break (0); 216 rs.hr.ec = TALER_JSON_get_error_code (j); 217 rs.hr.hint = TALER_JSON_get_error_hint (j); 218 break; 219 case MHD_HTTP_NOT_FOUND: 220 /* Nothing really to verify, this should never 221 happen, we should pass the JSON reply to the application */ 222 rs.hr.ec = TALER_JSON_get_error_code (j); 223 rs.hr.hint = TALER_JSON_get_error_hint (j); 224 break; 225 case MHD_HTTP_CONFLICT: 226 /* Server doesn't have the requested attributes */ 227 rs.hr.ec = TALER_JSON_get_error_code (j); 228 rs.hr.hint = TALER_JSON_get_error_hint (j); 229 break; 230 case MHD_HTTP_INTERNAL_SERVER_ERROR: 231 /* Server had an internal issue; we should retry, but this API 232 leaves this to the application */ 233 rs.hr.ec = TALER_JSON_get_error_code (j); 234 rs.hr.hint = TALER_JSON_get_error_hint (j); 235 break; 236 default: 237 /* unexpected response code */ 238 GNUNET_break_op (0); 239 rs.hr.ec = TALER_JSON_get_error_code (j); 240 rs.hr.hint = TALER_JSON_get_error_hint (j); 241 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 242 "Unexpected response code %u/%d for reserves attest\n", 243 (unsigned int) response_code, 244 (int) rs.hr.ec); 245 break; 246 } 247 if (NULL != prah->cb) 248 { 249 prah->cb (prah->cb_cls, 250 &rs); 251 prah->cb = NULL; 252 } 253 TALER_EXCHANGE_post_reserves_attest_cancel (prah); 254 } 255 256 257 struct TALER_EXCHANGE_PostReservesAttestHandle * 258 TALER_EXCHANGE_post_reserves_attest_create ( 259 struct GNUNET_CURL_Context *ctx, 260 const char *url, 261 struct TALER_EXCHANGE_Keys *keys, 262 const struct TALER_ReservePrivateKeyP *reserve_priv, 263 unsigned int attributes_length, 264 const char *attributes[const static attributes_length]) 265 { 266 struct TALER_EXCHANGE_PostReservesAttestHandle *prah; 267 struct TALER_ReserveSignatureP reserve_sig; 268 json_t *details; 269 struct GNUNET_TIME_Timestamp ts; 270 271 if (0 == attributes_length) 272 { 273 GNUNET_break (0); 274 return NULL; 275 } 276 details = json_array (); 277 GNUNET_assert (NULL != details); 278 for (unsigned int i = 0; i < attributes_length; i++) 279 { 280 GNUNET_assert (0 == 281 json_array_append_new (details, 282 json_string (attributes[i]))); 283 } 284 prah = GNUNET_new (struct TALER_EXCHANGE_PostReservesAttestHandle); 285 prah->ctx = ctx; 286 prah->base_url = GNUNET_strdup (url); 287 prah->keys = TALER_EXCHANGE_keys_incref (keys); 288 GNUNET_CRYPTO_eddsa_key_get_public (&reserve_priv->eddsa_priv, 289 &prah->reserve_pub.eddsa_pub); 290 ts = GNUNET_TIME_timestamp_get (); 291 TALER_wallet_reserve_attest_request_sign (ts, 292 details, 293 reserve_priv, 294 &reserve_sig); 295 prah->body = GNUNET_JSON_PACK ( 296 GNUNET_JSON_pack_data_auto ("reserve_sig", 297 &reserve_sig), 298 GNUNET_JSON_pack_timestamp ("request_timestamp", 299 ts), 300 GNUNET_JSON_pack_array_steal ("details", 301 details)); 302 if (NULL == prah->body) 303 { 304 GNUNET_break (0); 305 GNUNET_free (prah->base_url); 306 TALER_EXCHANGE_keys_decref (prah->keys); 307 GNUNET_free (prah); 308 return NULL; 309 } 310 return prah; 311 } 312 313 314 enum TALER_ErrorCode 315 TALER_EXCHANGE_post_reserves_attest_start ( 316 struct TALER_EXCHANGE_PostReservesAttestHandle *prah, 317 TALER_EXCHANGE_PostReservesAttestCallback cb, 318 TALER_EXCHANGE_POST_RESERVES_ATTEST_RESULT_CLOSURE *cb_cls) 319 { 320 CURL *eh; 321 char arg_str[sizeof (struct TALER_ReservePublicKeyP) * 2 + 32]; 322 323 prah->cb = cb; 324 prah->cb_cls = cb_cls; 325 { 326 char pub_str[sizeof (struct TALER_ReservePublicKeyP) * 2]; 327 char *end; 328 329 end = GNUNET_STRINGS_data_to_string ( 330 &prah->reserve_pub, 331 sizeof (prah->reserve_pub), 332 pub_str, 333 sizeof (pub_str)); 334 *end = '\0'; 335 GNUNET_snprintf (arg_str, 336 sizeof (arg_str), 337 "reserves-attest/%s", 338 pub_str); 339 } 340 prah->url = TALER_url_join (prah->base_url, 341 arg_str, 342 NULL); 343 if (NULL == prah->url) 344 return TALER_EC_GENERIC_CONFIGURATION_INVALID; 345 eh = TALER_EXCHANGE_curl_easy_get_ (prah->url); 346 if ( (NULL == eh) || 347 (GNUNET_OK != 348 TALER_curl_easy_post (&prah->post_ctx, 349 eh, 350 prah->body)) ) 351 { 352 GNUNET_break (0); 353 if (NULL != eh) 354 curl_easy_cleanup (eh); 355 GNUNET_free (prah->url); 356 prah->url = NULL; 357 return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE; 358 } 359 prah->job = GNUNET_CURL_job_add2 (prah->ctx, 360 eh, 361 prah->post_ctx.headers, 362 &handle_reserves_attest_finished, 363 prah); 364 if (NULL == prah->job) 365 { 366 TALER_curl_easy_post_finished (&prah->post_ctx); 367 GNUNET_free (prah->url); 368 prah->url = NULL; 369 return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE; 370 } 371 return TALER_EC_NONE; 372 } 373 374 375 void 376 TALER_EXCHANGE_post_reserves_attest_cancel ( 377 struct TALER_EXCHANGE_PostReservesAttestHandle *prah) 378 { 379 if (NULL != prah->job) 380 { 381 GNUNET_CURL_job_cancel (prah->job); 382 prah->job = NULL; 383 } 384 TALER_curl_easy_post_finished (&prah->post_ctx); 385 json_decref (prah->body); 386 GNUNET_free (prah->url); 387 GNUNET_free (prah->base_url); 388 TALER_EXCHANGE_keys_decref (prah->keys); 389 GNUNET_free (prah); 390 } 391 392 393 /* end of exchange_api_post-reserves-attest-RESERVE_PUB.c */