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 (8937B)


      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_CONFLICT:
    131     res.hr.ec = TALER_JSON_get_error_code (json);
    132     res.hr.hint = TALER_JSON_get_error_hint (json);
    133     break;
    134   case MHD_HTTP_REQUEST_ENTITY_TOO_LARGE:
    135     res.hr.ec = TALER_JSON_get_error_code (json);
    136     res.hr.hint = TALER_JSON_get_error_hint (json);
    137     break;
    138   default:
    139     /* unexpected response code */
    140     GNUNET_break_op (0);
    141     res.hr.ec = TALER_JSON_get_error_code (json);
    142     res.hr.hint = TALER_JSON_get_error_hint (json);
    143     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    144                 "Unexpected response code %u/%d for exchange management post keys\n",
    145                 (unsigned int) response_code,
    146                 (int) res.hr.ec);
    147     break;
    148   }
    149   if (NULL != pmkh->cb)
    150   {
    151     pmkh->cb (pmkh->cb_cls,
    152               &res);
    153     pmkh->cb = NULL;
    154   }
    155   TALER_EXCHANGE_post_management_keys_cancel (pmkh);
    156 }
    157 
    158 
    159 struct TALER_EXCHANGE_PostManagementKeysHandle *
    160 TALER_EXCHANGE_post_management_keys_create (
    161   struct GNUNET_CURL_Context *ctx,
    162   const char *url,
    163   const struct TALER_EXCHANGE_ManagementPostKeysData *pkd)
    164 {
    165   struct TALER_EXCHANGE_PostManagementKeysHandle *pmkh;
    166 
    167   pmkh = GNUNET_new (struct TALER_EXCHANGE_PostManagementKeysHandle);
    168   pmkh->ctx = ctx;
    169   pmkh->base_url = GNUNET_strdup (url);
    170   pmkh->num_sign_sigs = pkd->num_sign_sigs;
    171   pmkh->num_denom_sigs = pkd->num_denom_sigs;
    172   pmkh->sign_sigs = GNUNET_memdup (pkd->sign_sigs,
    173                                    pkd->num_sign_sigs
    174                                    * sizeof (struct
    175                                              TALER_EXCHANGE_SigningKeySignature)
    176                                    );
    177   pmkh->denom_sigs = GNUNET_memdup (pkd->denom_sigs,
    178                                     pkd->num_denom_sigs
    179                                     * sizeof (struct
    180                                               TALER_EXCHANGE_DenominationKeySignature));
    181   return pmkh;
    182 }
    183 
    184 
    185 enum TALER_ErrorCode
    186 TALER_EXCHANGE_post_management_keys_start (
    187   struct TALER_EXCHANGE_PostManagementKeysHandle *pmkh,
    188   TALER_EXCHANGE_PostManagementKeysCallback cb,
    189   TALER_EXCHANGE_POST_MANAGEMENT_KEYS_RESULT_CLOSURE *cb_cls)
    190 {
    191   CURL *eh;
    192   json_t *body;
    193   json_t *denom_sigs;
    194   json_t *signkey_sigs;
    195 
    196   pmkh->cb = cb;
    197   pmkh->cb_cls = cb_cls;
    198   pmkh->url = TALER_url_join (pmkh->base_url,
    199                               "management/keys",
    200                               NULL);
    201   if (NULL == pmkh->url)
    202   {
    203     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    204                 "Could not construct request URL.\n");
    205     return TALER_EC_GENERIC_CONFIGURATION_INVALID;
    206   }
    207   denom_sigs = json_array ();
    208   GNUNET_assert (NULL != denom_sigs);
    209   for (unsigned int i = 0; i < pmkh->num_denom_sigs; i++)
    210   {
    211     const struct TALER_EXCHANGE_DenominationKeySignature *dks
    212       = &pmkh->denom_sigs[i];
    213 
    214     GNUNET_assert (0 ==
    215                    json_array_append_new (
    216                      denom_sigs,
    217                      GNUNET_JSON_PACK (
    218                        GNUNET_JSON_pack_data_auto ("h_denom_pub",
    219                                                    &dks->h_denom_pub),
    220                        GNUNET_JSON_pack_data_auto ("master_sig",
    221                                                    &dks->master_sig))));
    222   }
    223   signkey_sigs = json_array ();
    224   GNUNET_assert (NULL != signkey_sigs);
    225   for (unsigned int i = 0; i < pmkh->num_sign_sigs; i++)
    226   {
    227     const struct TALER_EXCHANGE_SigningKeySignature *sks
    228       = &pmkh->sign_sigs[i];
    229 
    230     GNUNET_assert (0 ==
    231                    json_array_append_new (
    232                      signkey_sigs,
    233                      GNUNET_JSON_PACK (
    234                        GNUNET_JSON_pack_data_auto ("exchange_pub",
    235                                                    &sks->exchange_pub),
    236                        GNUNET_JSON_pack_data_auto ("master_sig",
    237                                                    &sks->master_sig))));
    238   }
    239   body = GNUNET_JSON_PACK (
    240     GNUNET_JSON_pack_array_steal ("denom_sigs",
    241                                   denom_sigs),
    242     GNUNET_JSON_pack_array_steal ("signkey_sigs",
    243                                   signkey_sigs));
    244   eh = TALER_EXCHANGE_curl_easy_get_ (pmkh->url);
    245   if ( (NULL == eh) ||
    246        (GNUNET_OK !=
    247         TALER_curl_easy_post (&pmkh->post_ctx,
    248                               eh,
    249                               body)) )
    250   {
    251     GNUNET_break (0);
    252     if (NULL != eh)
    253       curl_easy_cleanup (eh);
    254     json_decref (body);
    255     GNUNET_free (pmkh->url);
    256     pmkh->url = NULL;
    257     return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
    258   }
    259   json_decref (body);
    260   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
    261               "Requesting URL '%s'\n",
    262               pmkh->url);
    263   pmkh->job = GNUNET_CURL_job_add2 (pmkh->ctx,
    264                                     eh,
    265                                     pmkh->post_ctx.headers,
    266                                     &handle_post_keys_finished,
    267                                     pmkh);
    268   if (NULL == pmkh->job)
    269   {
    270     TALER_curl_easy_post_finished (&pmkh->post_ctx);
    271     GNUNET_free (pmkh->url);
    272     pmkh->url = NULL;
    273     return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
    274   }
    275   return TALER_EC_NONE;
    276 }
    277 
    278 
    279 void
    280 TALER_EXCHANGE_post_management_keys_cancel (
    281   struct TALER_EXCHANGE_PostManagementKeysHandle *pmkh)
    282 {
    283   if (NULL != pmkh->job)
    284   {
    285     GNUNET_CURL_job_cancel (pmkh->job);
    286     pmkh->job = NULL;
    287   }
    288   TALER_curl_easy_post_finished (&pmkh->post_ctx);
    289   GNUNET_free (pmkh->sign_sigs);
    290   GNUNET_free (pmkh->denom_sigs);
    291   GNUNET_free (pmkh->url);
    292   GNUNET_free (pmkh->base_url);
    293   GNUNET_free (pmkh);
    294 }
    295 
    296 
    297 /* end of exchange_api_post-management-keys.c */