LCOV - code coverage report
Current view: top level - lib - exchange_api_post-management-keys.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 64.1 % 92 59
Test Date: 2026-03-10 12:10:57 Functions: 100.0 % 4 4

            Line data    Source code
       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           19 : handle_post_keys_finished (void *cls,
     107              :                            long response_code,
     108              :                            const void *response)
     109              : {
     110           19 :   struct TALER_EXCHANGE_PostManagementKeysHandle *pmkh = cls;
     111           19 :   const json_t *json = response;
     112           19 :   struct TALER_EXCHANGE_PostManagementKeysResponse res = {
     113           19 :     .hr.http_status = (unsigned int) response_code,
     114              :     .hr.reply = json
     115              :   };
     116              : 
     117           19 :   pmkh->job = NULL;
     118           19 :   switch (response_code)
     119              :   {
     120           19 :   case MHD_HTTP_NO_CONTENT:
     121           19 :     break;
     122            0 :   case MHD_HTTP_FORBIDDEN:
     123            0 :     res.hr.ec = TALER_JSON_get_error_code (json);
     124            0 :     res.hr.hint = TALER_JSON_get_error_hint (json);
     125            0 :     break;
     126            0 :   case MHD_HTTP_NOT_FOUND:
     127            0 :     res.hr.ec = TALER_JSON_get_error_code (json);
     128            0 :     res.hr.hint = TALER_JSON_get_error_hint (json);
     129            0 :     break;
     130            0 :   case MHD_HTTP_REQUEST_ENTITY_TOO_LARGE:
     131            0 :     res.hr.ec = TALER_JSON_get_error_code (json);
     132            0 :     res.hr.hint = TALER_JSON_get_error_hint (json);
     133            0 :     break;
     134            0 :   default:
     135              :     /* unexpected response code */
     136            0 :     GNUNET_break_op (0);
     137            0 :     res.hr.ec = TALER_JSON_get_error_code (json);
     138            0 :     res.hr.hint = TALER_JSON_get_error_hint (json);
     139            0 :     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            0 :     break;
     144              :   }
     145           19 :   if (NULL != pmkh->cb)
     146              :   {
     147           19 :     pmkh->cb (pmkh->cb_cls,
     148              :               &res);
     149           19 :     pmkh->cb = NULL;
     150              :   }
     151           19 :   TALER_EXCHANGE_post_management_keys_cancel (pmkh);
     152           19 : }
     153              : 
     154              : 
     155              : struct TALER_EXCHANGE_PostManagementKeysHandle *
     156           19 : 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           19 :   pmkh = GNUNET_new (struct TALER_EXCHANGE_PostManagementKeysHandle);
     164           19 :   pmkh->ctx = ctx;
     165           19 :   pmkh->base_url = GNUNET_strdup (url);
     166           19 :   pmkh->num_sign_sigs = pkd->num_sign_sigs;
     167           19 :   pmkh->num_denom_sigs = pkd->num_denom_sigs;
     168           19 :   pmkh->sign_sigs = GNUNET_memdup (pkd->sign_sigs,
     169              :                                    pkd->num_sign_sigs
     170              :                                    * sizeof (struct
     171              :                                              TALER_EXCHANGE_SigningKeySignature));
     172           19 :   pmkh->denom_sigs = GNUNET_memdup (pkd->denom_sigs,
     173              :                                     pkd->num_denom_sigs
     174              :                                     * sizeof (struct
     175              :                                               TALER_EXCHANGE_DenominationKeySignature));
     176           19 :   return pmkh;
     177              : }
     178              : 
     179              : 
     180              : enum TALER_ErrorCode
     181           19 : 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           19 :   pmkh->cb = cb;
     192           19 :   pmkh->cb_cls = cb_cls;
     193           19 :   pmkh->url = TALER_url_join (pmkh->base_url,
     194              :                               "management/keys",
     195              :                               NULL);
     196           19 :   if (NULL == pmkh->url)
     197              :   {
     198            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     199              :                 "Could not construct request URL.\n");
     200            0 :     return TALER_EC_GENERIC_CONFIGURATION_INVALID;
     201              :   }
     202           19 :   denom_sigs = json_array ();
     203           19 :   GNUNET_assert (NULL != denom_sigs);
     204          484 :   for (unsigned int i = 0; i < pmkh->num_denom_sigs; i++)
     205              :   {
     206          465 :     const struct TALER_EXCHANGE_DenominationKeySignature *dks
     207          465 :       = &pmkh->denom_sigs[i];
     208              : 
     209          465 :     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           19 :   signkey_sigs = json_array ();
     219           19 :   GNUNET_assert (NULL != signkey_sigs);
     220           40 :   for (unsigned int i = 0; i < pmkh->num_sign_sigs; i++)
     221              :   {
     222           21 :     const struct TALER_EXCHANGE_SigningKeySignature *sks
     223           21 :       = &pmkh->sign_sigs[i];
     224              : 
     225           21 :     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           19 :   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           19 :   eh = TALER_EXCHANGE_curl_easy_get_ (pmkh->url);
     240           38 :   if ( (NULL == eh) ||
     241              :        (GNUNET_OK !=
     242           19 :         TALER_curl_easy_post (&pmkh->post_ctx,
     243              :                               eh,
     244              :                               body)) )
     245              :   {
     246            0 :     GNUNET_break (0);
     247            0 :     if (NULL != eh)
     248            0 :       curl_easy_cleanup (eh);
     249            0 :     json_decref (body);
     250            0 :     GNUNET_free (pmkh->url);
     251            0 :     pmkh->url = NULL;
     252            0 :     return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
     253              :   }
     254           19 :   json_decref (body);
     255           19 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     256              :               "Requesting URL '%s'\n",
     257              :               pmkh->url);
     258           38 :   pmkh->job = GNUNET_CURL_job_add2 (pmkh->ctx,
     259              :                                     eh,
     260           19 :                                     pmkh->post_ctx.headers,
     261              :                                     &handle_post_keys_finished,
     262              :                                     pmkh);
     263           19 :   if (NULL == pmkh->job)
     264              :   {
     265            0 :     TALER_curl_easy_post_finished (&pmkh->post_ctx);
     266            0 :     GNUNET_free (pmkh->url);
     267            0 :     pmkh->url = NULL;
     268            0 :     return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
     269              :   }
     270           19 :   return TALER_EC_NONE;
     271              : }
     272              : 
     273              : 
     274              : void
     275           19 : TALER_EXCHANGE_post_management_keys_cancel (
     276              :   struct TALER_EXCHANGE_PostManagementKeysHandle *pmkh)
     277              : {
     278           19 :   if (NULL != pmkh->job)
     279              :   {
     280            0 :     GNUNET_CURL_job_cancel (pmkh->job);
     281            0 :     pmkh->job = NULL;
     282              :   }
     283           19 :   TALER_curl_easy_post_finished (&pmkh->post_ctx);
     284           19 :   GNUNET_free (pmkh->sign_sigs);
     285           19 :   GNUNET_free (pmkh->denom_sigs);
     286           19 :   GNUNET_free (pmkh->url);
     287           19 :   GNUNET_free (pmkh->base_url);
     288           19 :   GNUNET_free (pmkh);
     289           19 : }
     290              : 
     291              : 
     292              : /* end of exchange_api_post-management-keys.c */
        

Generated by: LCOV version 2.0-1