LCOV - code coverage report
Current view: top level - lib - exchange_api_management_post_keys.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 61.2 % 80 49
Test Date: 2025-12-26 23:00:34 Functions: 100.0 % 3 3

            Line data    Source code
       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           19 : handle_post_keys_finished (void *cls,
      81              :                            long response_code,
      82              :                            const void *response)
      83              : {
      84           19 :   struct TALER_EXCHANGE_ManagementPostKeysHandle *ph = cls;
      85           19 :   const json_t *json = response;
      86           19 :   struct TALER_EXCHANGE_ManagementPostKeysResponse pkr = {
      87           19 :     .hr.http_status = (unsigned int) response_code,
      88              :     .hr.reply = json
      89              :   };
      90              : 
      91           19 :   ph->job = NULL;
      92           19 :   switch (response_code)
      93              :   {
      94           19 :   case MHD_HTTP_NO_CONTENT:
      95           19 :     break;
      96            0 :   case MHD_HTTP_FORBIDDEN:
      97            0 :     pkr.hr.ec = TALER_JSON_get_error_code (json);
      98            0 :     pkr.hr.hint = TALER_JSON_get_error_hint (json);
      99            0 :     break;
     100            0 :   case MHD_HTTP_NOT_FOUND:
     101            0 :     pkr.hr.ec = TALER_JSON_get_error_code (json);
     102            0 :     pkr.hr.hint = TALER_JSON_get_error_hint (json);
     103            0 :     break;
     104            0 :   case MHD_HTTP_REQUEST_ENTITY_TOO_LARGE:
     105            0 :     pkr.hr.ec = TALER_JSON_get_error_code (json);
     106            0 :     pkr.hr.hint = TALER_JSON_get_error_hint (json);
     107            0 :     break;
     108            0 :   default:
     109              :     /* unexpected response code */
     110            0 :     GNUNET_break_op (0);
     111            0 :     pkr.hr.ec = TALER_JSON_get_error_code (json);
     112            0 :     pkr.hr.hint = TALER_JSON_get_error_hint (json);
     113            0 :     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            0 :     break;
     118              :   }
     119           19 :   if (NULL != ph->cb)
     120              :   {
     121           19 :     ph->cb (ph->cb_cls,
     122              :             &pkr);
     123           19 :     ph->cb = NULL;
     124              :   }
     125           19 :   TALER_EXCHANGE_post_management_keys_cancel (ph);
     126           19 : }
     127              : 
     128              : 
     129              : struct TALER_EXCHANGE_ManagementPostKeysHandle *
     130           19 : 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           19 :   ph = GNUNET_new (struct TALER_EXCHANGE_ManagementPostKeysHandle);
     144           19 :   ph->cb = cb;
     145           19 :   ph->cb_cls = cb_cls;
     146           19 :   ph->ctx = ctx;
     147           19 :   ph->url = TALER_url_join (url,
     148              :                             "management/keys",
     149              :                             NULL);
     150           19 :   if (NULL == ph->url)
     151              :   {
     152            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     153              :                 "Could not construct request URL.\n");
     154            0 :     GNUNET_free (ph);
     155            0 :     return NULL;
     156              :   }
     157           19 :   denom_sigs = json_array ();
     158           19 :   GNUNET_assert (NULL != denom_sigs);
     159         5392 :   for (unsigned int i = 0; i<pkd->num_denom_sigs; i++)
     160              :   {
     161         5373 :     const struct TALER_EXCHANGE_DenominationKeySignature *dks
     162         5373 :       = &pkd->denom_sigs[i];
     163              : 
     164         5373 :     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           19 :   signkey_sigs = json_array ();
     174           19 :   GNUNET_assert (NULL != signkey_sigs);
     175           76 :   for (unsigned int i = 0; i<pkd->num_sign_sigs; i++)
     176              :   {
     177           57 :     const struct TALER_EXCHANGE_SigningKeySignature *sks
     178           57 :       = &pkd->sign_sigs[i];
     179              : 
     180           57 :     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           19 :   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           19 :   eh = TALER_EXCHANGE_curl_easy_get_ (ph->url);
     195           38 :   if ( (NULL == eh) ||
     196              :        (GNUNET_OK !=
     197           19 :         TALER_curl_easy_post (&ph->post_ctx,
     198              :                               eh,
     199              :                               body)) )
     200              :   {
     201            0 :     GNUNET_break (0);
     202            0 :     if (NULL != eh)
     203            0 :       curl_easy_cleanup (eh);
     204            0 :     json_decref (body);
     205            0 :     GNUNET_free (ph->url);
     206            0 :     return NULL;
     207              :   }
     208           19 :   json_decref (body);
     209           19 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     210              :               "Requesting URL '%s'\n",
     211              :               ph->url);
     212           38 :   ph->job = GNUNET_CURL_job_add2 (ctx,
     213              :                                   eh,
     214           19 :                                   ph->post_ctx.headers,
     215              :                                   &handle_post_keys_finished,
     216              :                                   ph);
     217           19 :   if (NULL == ph->job)
     218              :   {
     219            0 :     TALER_EXCHANGE_post_management_keys_cancel (ph);
     220            0 :     return NULL;
     221              :   }
     222           19 :   return ph;
     223              : }
     224              : 
     225              : 
     226              : void
     227           19 : TALER_EXCHANGE_post_management_keys_cancel (
     228              :   struct TALER_EXCHANGE_ManagementPostKeysHandle *ph)
     229              : {
     230           19 :   if (NULL != ph->job)
     231              :   {
     232            0 :     GNUNET_CURL_job_cancel (ph->job);
     233            0 :     ph->job = NULL;
     234              :   }
     235           19 :   TALER_curl_easy_post_finished (&ph->post_ctx);
     236           19 :   GNUNET_free (ph->url);
     237           19 :   GNUNET_free (ph);
     238           19 : }
        

Generated by: LCOV version 2.0-1