exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

exchange_api_post-management-keys.c (8762B)


      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-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 "taler/taler-exchange/post-management-keys.h"
     28 #include "exchange_api_curl_defaults.h"
     29 #include "taler/taler_signatures.h"
     30 #include "taler/taler_curl_lib.h"
     31 
     32 
     33 /**
     34  * @brief Handle for a POST /management/keys request.
     35  */
     36 struct TALER_EXCHANGE_PostManagementKeysHandle
     37 {
     38 
     39   /**
     40    * The base URL for this request.
     41    */
     42   char *base_url;
     43 
     44   /**
     45    * The full URL for this request, set during _start.
     46    */
     47   char *url;
     48 
     49   /**
     50    * Minor context that holds body and headers.
     51    */
     52   struct TALER_CURL_PostContext post_ctx;
     53 
     54   /**
     55    * Handle for the request.
     56    */
     57   struct GNUNET_CURL_Job *job;
     58 
     59   /**
     60    * Function to call with the result.
     61    */
     62   TALER_EXCHANGE_PostManagementKeysCallback cb;
     63 
     64   /**
     65    * Closure for @a cb.
     66    */
     67   TALER_EXCHANGE_POST_MANAGEMENT_KEYS_RESULT_CLOSURE *cb_cls;
     68 
     69   /**
     70    * Reference to the execution context.
     71    */
     72   struct GNUNET_CURL_Context *ctx;
     73 
     74   /**
     75    * Array of master signatures for the exchange's online signing keys.
     76    */
     77   struct TALER_EXCHANGE_SigningKeySignature *sign_sigs;
     78 
     79   /**
     80    * Length of the @e sign_sigs array.
     81    */
     82   unsigned int num_sign_sigs;
     83 
     84   /**
     85    * Array of master signatures for the exchange's denomination keys.
     86    */
     87   struct TALER_EXCHANGE_DenominationKeySignature *denom_sigs;
     88 
     89   /**
     90    * Length of the @e denom_sigs array.
     91    */
     92   unsigned int num_denom_sigs;
     93 
     94 };
     95 
     96 
     97 /**
     98  * Function called when we're done processing the
     99  * HTTP POST /management/keys request.
    100  *
    101  * @param cls the `struct TALER_EXCHANGE_PostManagementKeysHandle`
    102  * @param response_code HTTP response code, 0 on error
    103  * @param response response body, NULL if not in JSON
    104  */
    105 static void
    106 handle_post_keys_finished (void *cls,
    107                            long response_code,
    108                            const void *response)
    109 {
    110   struct TALER_EXCHANGE_PostManagementKeysHandle *pmkh = cls;
    111   const json_t *json = response;
    112   struct TALER_EXCHANGE_PostManagementKeysResponse res = {
    113     .hr.http_status = (unsigned int) response_code,
    114     .hr.reply = json
    115   };
    116 
    117   pmkh->job = NULL;
    118   switch (response_code)
    119   {
    120   case MHD_HTTP_NO_CONTENT:
    121     break;
    122   case MHD_HTTP_FORBIDDEN:
    123     res.hr.ec = TALER_JSON_get_error_code (json);
    124     res.hr.hint = TALER_JSON_get_error_hint (json);
    125     break;
    126   case MHD_HTTP_NOT_FOUND:
    127     res.hr.ec = TALER_JSON_get_error_code (json);
    128     res.hr.hint = TALER_JSON_get_error_hint (json);
    129     break;
    130   case MHD_HTTP_REQUEST_ENTITY_TOO_LARGE:
    131     res.hr.ec = TALER_JSON_get_error_code (json);
    132     res.hr.hint = TALER_JSON_get_error_hint (json);
    133     break;
    134   default:
    135     /* unexpected response code */
    136     GNUNET_break_op (0);
    137     res.hr.ec = TALER_JSON_get_error_code (json);
    138     res.hr.hint = TALER_JSON_get_error_hint (json);
    139     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    140                 "Unexpected response code %u/%d for exchange management post keys\n",
    141                 (unsigned int) response_code,
    142                 (int) res.hr.ec);
    143     break;
    144   }
    145   if (NULL != pmkh->cb)
    146   {
    147     pmkh->cb (pmkh->cb_cls,
    148               &res);
    149     pmkh->cb = NULL;
    150   }
    151   TALER_EXCHANGE_post_management_keys_cancel (pmkh);
    152 }
    153 
    154 
    155 struct TALER_EXCHANGE_PostManagementKeysHandle *
    156 TALER_EXCHANGE_post_management_keys_create (
    157   struct GNUNET_CURL_Context *ctx,
    158   const char *url,
    159   const struct TALER_EXCHANGE_ManagementPostKeysData *pkd)
    160 {
    161   struct TALER_EXCHANGE_PostManagementKeysHandle *pmkh;
    162 
    163   pmkh = GNUNET_new (struct TALER_EXCHANGE_PostManagementKeysHandle);
    164   pmkh->ctx = ctx;
    165   pmkh->base_url = GNUNET_strdup (url);
    166   pmkh->num_sign_sigs = pkd->num_sign_sigs;
    167   pmkh->num_denom_sigs = pkd->num_denom_sigs;
    168   pmkh->sign_sigs = GNUNET_memdup (pkd->sign_sigs,
    169                                    pkd->num_sign_sigs
    170                                    * sizeof (struct
    171                                              TALER_EXCHANGE_SigningKeySignature));
    172   pmkh->denom_sigs = GNUNET_memdup (pkd->denom_sigs,
    173                                     pkd->num_denom_sigs
    174                                     * sizeof (struct
    175                                               TALER_EXCHANGE_DenominationKeySignature));
    176   return pmkh;
    177 }
    178 
    179 
    180 enum TALER_ErrorCode
    181 TALER_EXCHANGE_post_management_keys_start (
    182   struct TALER_EXCHANGE_PostManagementKeysHandle *pmkh,
    183   TALER_EXCHANGE_PostManagementKeysCallback cb,
    184   TALER_EXCHANGE_POST_MANAGEMENT_KEYS_RESULT_CLOSURE *cb_cls)
    185 {
    186   CURL *eh;
    187   json_t *body;
    188   json_t *denom_sigs;
    189   json_t *signkey_sigs;
    190 
    191   pmkh->cb = cb;
    192   pmkh->cb_cls = cb_cls;
    193   pmkh->url = TALER_url_join (pmkh->base_url,
    194                               "management/keys",
    195                               NULL);
    196   if (NULL == pmkh->url)
    197   {
    198     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    199                 "Could not construct request URL.\n");
    200     return TALER_EC_GENERIC_CONFIGURATION_INVALID;
    201   }
    202   denom_sigs = json_array ();
    203   GNUNET_assert (NULL != denom_sigs);
    204   for (unsigned int i = 0; i < pmkh->num_denom_sigs; i++)
    205   {
    206     const struct TALER_EXCHANGE_DenominationKeySignature *dks
    207       = &pmkh->denom_sigs[i];
    208 
    209     GNUNET_assert (0 ==
    210                    json_array_append_new (
    211                      denom_sigs,
    212                      GNUNET_JSON_PACK (
    213                        GNUNET_JSON_pack_data_auto ("h_denom_pub",
    214                                                    &dks->h_denom_pub),
    215                        GNUNET_JSON_pack_data_auto ("master_sig",
    216                                                    &dks->master_sig))));
    217   }
    218   signkey_sigs = json_array ();
    219   GNUNET_assert (NULL != signkey_sigs);
    220   for (unsigned int i = 0; i < pmkh->num_sign_sigs; i++)
    221   {
    222     const struct TALER_EXCHANGE_SigningKeySignature *sks
    223       = &pmkh->sign_sigs[i];
    224 
    225     GNUNET_assert (0 ==
    226                    json_array_append_new (
    227                      signkey_sigs,
    228                      GNUNET_JSON_PACK (
    229                        GNUNET_JSON_pack_data_auto ("exchange_pub",
    230                                                    &sks->exchange_pub),
    231                        GNUNET_JSON_pack_data_auto ("master_sig",
    232                                                    &sks->master_sig))));
    233   }
    234   body = GNUNET_JSON_PACK (
    235     GNUNET_JSON_pack_array_steal ("denom_sigs",
    236                                   denom_sigs),
    237     GNUNET_JSON_pack_array_steal ("signkey_sigs",
    238                                   signkey_sigs));
    239   eh = TALER_EXCHANGE_curl_easy_get_ (pmkh->url);
    240   if ( (NULL == eh) ||
    241        (GNUNET_OK !=
    242         TALER_curl_easy_post (&pmkh->post_ctx,
    243                               eh,
    244                               body)) )
    245   {
    246     GNUNET_break (0);
    247     if (NULL != eh)
    248       curl_easy_cleanup (eh);
    249     json_decref (body);
    250     GNUNET_free (pmkh->url);
    251     pmkh->url = NULL;
    252     return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
    253   }
    254   json_decref (body);
    255   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
    256               "Requesting URL '%s'\n",
    257               pmkh->url);
    258   pmkh->job = GNUNET_CURL_job_add2 (pmkh->ctx,
    259                                     eh,
    260                                     pmkh->post_ctx.headers,
    261                                     &handle_post_keys_finished,
    262                                     pmkh);
    263   if (NULL == pmkh->job)
    264   {
    265     TALER_curl_easy_post_finished (&pmkh->post_ctx);
    266     GNUNET_free (pmkh->url);
    267     pmkh->url = NULL;
    268     return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
    269   }
    270   return TALER_EC_NONE;
    271 }
    272 
    273 
    274 void
    275 TALER_EXCHANGE_post_management_keys_cancel (
    276   struct TALER_EXCHANGE_PostManagementKeysHandle *pmkh)
    277 {
    278   if (NULL != pmkh->job)
    279   {
    280     GNUNET_CURL_job_cancel (pmkh->job);
    281     pmkh->job = NULL;
    282   }
    283   TALER_curl_easy_post_finished (&pmkh->post_ctx);
    284   GNUNET_free (pmkh->sign_sigs);
    285   GNUNET_free (pmkh->denom_sigs);
    286   GNUNET_free (pmkh->url);
    287   GNUNET_free (pmkh->base_url);
    288   GNUNET_free (pmkh);
    289 }
    290 
    291 
    292 /* end of exchange_api_post-management-keys.c */