LCOV - code coverage report
Current view: top level - lib - exchange_api_management_post_keys.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 51 77 66.2 %
Date: 2021-08-30 06:43:37 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          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 "platform.h"
      23             : #include "taler_json_lib.h"
      24             : #include <gnunet/gnunet_curl_lib.h>
      25             : #include "taler_exchange_service.h"
      26             : #include "taler_signatures.h"
      27             : #include "taler_curl_lib.h"
      28             : #include "taler_json_lib.h"
      29             : 
      30             : 
      31             : /**
      32             :  * @brief Handle for a POST /management/keys request.
      33             :  */
      34             : struct TALER_EXCHANGE_ManagementPostKeysHandle
      35             : {
      36             : 
      37             :   /**
      38             :    * The url for this request.
      39             :    */
      40             :   char *url;
      41             : 
      42             :   /**
      43             :    * Minor context that holds body and headers.
      44             :    */
      45             :   struct TALER_CURL_PostContext post_ctx;
      46             : 
      47             :   /**
      48             :    * Handle for the request.
      49             :    */
      50             :   struct GNUNET_CURL_Job *job;
      51             : 
      52             :   /**
      53             :    * Function to call with the result.
      54             :    */
      55             :   TALER_EXCHANGE_ManagementPostKeysCallback cb;
      56             : 
      57             :   /**
      58             :    * Closure for @a cb.
      59             :    */
      60             :   void *cb_cls;
      61             : 
      62             :   /**
      63             :    * Reference to the execution context.
      64             :    */
      65             :   struct GNUNET_CURL_Context *ctx;
      66             : };
      67             : 
      68             : 
      69             : /**
      70             :  * Function called when we're done processing the
      71             :  * HTTP POST /management/keys request.
      72             :  *
      73             :  * @param cls the `struct TALER_EXCHANGE_ManagementPostKeysHandle *`
      74             :  * @param response_code HTTP response code, 0 on error
      75             :  * @param response response body, NULL if not in JSON
      76             :  */
      77             : static void
      78           8 : handle_post_keys_finished (void *cls,
      79             :                            long response_code,
      80             :                            const void *response)
      81             : {
      82           8 :   struct TALER_EXCHANGE_ManagementPostKeysHandle *ph = cls;
      83           8 :   const json_t *json = response;
      84           8 :   struct TALER_EXCHANGE_HttpResponse hr = {
      85           8 :     .http_status = (unsigned int) response_code,
      86             :     .reply = json
      87             :   };
      88             : 
      89           8 :   ph->job = NULL;
      90           8 :   switch (response_code)
      91             :   {
      92           8 :   case MHD_HTTP_NO_CONTENT:
      93           8 :     break;
      94           0 :   case MHD_HTTP_FORBIDDEN:
      95           0 :     hr.ec = TALER_JSON_get_error_code (json);
      96           0 :     hr.hint = TALER_JSON_get_error_hint (json);
      97           0 :     break;
      98           0 :   case MHD_HTTP_NOT_FOUND:
      99           0 :     hr.ec = TALER_JSON_get_error_code (json);
     100           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     101           0 :     break;
     102           0 :   default:
     103             :     /* unexpected response code */
     104           0 :     GNUNET_break_op (0);
     105           0 :     hr.ec = TALER_JSON_get_error_code (json);
     106           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     107           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     108             :                 "Unexpected response code %u/%d for exchange management post keys\n",
     109             :                 (unsigned int) response_code,
     110             :                 (int) hr.ec);
     111           0 :     break;
     112             :   }
     113           8 :   if (NULL != ph->cb)
     114             :   {
     115           8 :     ph->cb (ph->cb_cls,
     116             :             &hr);
     117           8 :     ph->cb = NULL;
     118             :   }
     119           8 :   TALER_EXCHANGE_post_management_keys_cancel (ph);
     120           8 : }
     121             : 
     122             : 
     123             : struct TALER_EXCHANGE_ManagementPostKeysHandle *
     124           8 : TALER_EXCHANGE_post_management_keys (
     125             :   struct GNUNET_CURL_Context *ctx,
     126             :   const char *url,
     127             :   const struct TALER_EXCHANGE_ManagementPostKeysData *pkd,
     128             :   TALER_EXCHANGE_ManagementPostKeysCallback cb,
     129             :   void *cb_cls)
     130             : {
     131             :   struct TALER_EXCHANGE_ManagementPostKeysHandle *ph;
     132             :   CURL *eh;
     133             :   json_t *body;
     134             :   json_t *denom_sigs;
     135             :   json_t *signkey_sigs;
     136             : 
     137           8 :   ph = GNUNET_new (struct TALER_EXCHANGE_ManagementPostKeysHandle);
     138           8 :   ph->cb = cb;
     139           8 :   ph->cb_cls = cb_cls;
     140           8 :   ph->ctx = ctx;
     141           8 :   ph->url = TALER_url_join (url,
     142             :                             "management/keys",
     143             :                             NULL);
     144           8 :   if (NULL == ph->url)
     145             :   {
     146           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     147             :                 "Could not construct request URL.\n");
     148           0 :     GNUNET_free (ph);
     149           0 :     return NULL;
     150             :   }
     151           8 :   denom_sigs = json_array ();
     152           8 :   GNUNET_assert (NULL != denom_sigs);
     153         124 :   for (unsigned int i = 0; i<pkd->num_denom_sigs; i++)
     154             :   {
     155         116 :     const struct TALER_EXCHANGE_DenominationKeySignature *dks
     156         116 :       = &pkd->denom_sigs[i];
     157             : 
     158         116 :     GNUNET_assert (0 ==
     159             :                    json_array_append_new (
     160             :                      denom_sigs,
     161             :                      GNUNET_JSON_PACK (
     162             :                        GNUNET_JSON_pack_data_auto ("h_denom_pub",
     163             :                                                    &dks->h_denom_pub),
     164             :                        GNUNET_JSON_pack_data_auto ("master_sig",
     165             :                                                    &dks->master_sig))));
     166             :   }
     167           8 :   signkey_sigs = json_array ();
     168           8 :   GNUNET_assert (NULL != signkey_sigs);
     169          22 :   for (unsigned int i = 0; i<pkd->num_sign_sigs; i++)
     170             :   {
     171          14 :     const struct TALER_EXCHANGE_SigningKeySignature *sks
     172          14 :       = &pkd->sign_sigs[i];
     173             : 
     174          14 :     GNUNET_assert (0 ==
     175             :                    json_array_append_new (
     176             :                      signkey_sigs,
     177             :                      GNUNET_JSON_PACK (
     178             :                        GNUNET_JSON_pack_data_auto ("exchange_pub",
     179             :                                                    &sks->exchange_pub),
     180             :                        GNUNET_JSON_pack_data_auto ("master_sig",
     181             :                                                    &sks->master_sig))));
     182             :   }
     183           8 :   body = GNUNET_JSON_PACK (
     184             :     GNUNET_JSON_pack_array_steal ("denom_sigs",
     185             :                                   denom_sigs),
     186             :     GNUNET_JSON_pack_array_steal ("signkey_sigs",
     187             :                                   signkey_sigs));
     188           8 :   eh = curl_easy_init ();
     189           8 :   GNUNET_assert (NULL != eh);
     190           8 :   if (GNUNET_OK !=
     191           8 :       TALER_curl_easy_post (&ph->post_ctx,
     192             :                             eh,
     193             :                             body))
     194             :   {
     195           0 :     GNUNET_break (0);
     196           0 :     json_decref (body);
     197           0 :     GNUNET_free (ph->url);
     198           0 :     GNUNET_free (eh);
     199           0 :     return NULL;
     200             :   }
     201           8 :   json_decref (body);
     202           8 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     203             :               "Requesting URL '%s'\n",
     204             :               ph->url);
     205           8 :   GNUNET_assert (CURLE_OK == curl_easy_setopt (eh,
     206             :                                                CURLOPT_URL,
     207             :                                                ph->url));
     208          16 :   ph->job = GNUNET_CURL_job_add2 (ctx,
     209             :                                   eh,
     210           8 :                                   ph->post_ctx.headers,
     211             :                                   &handle_post_keys_finished,
     212             :                                   ph);
     213           8 :   if (NULL == ph->job)
     214             :   {
     215           0 :     TALER_EXCHANGE_post_management_keys_cancel (ph);
     216           0 :     return NULL;
     217             :   }
     218           8 :   return ph;
     219             : }
     220             : 
     221             : 
     222             : void
     223           8 : TALER_EXCHANGE_post_management_keys_cancel (
     224             :   struct TALER_EXCHANGE_ManagementPostKeysHandle *ph)
     225             : {
     226           8 :   if (NULL != ph->job)
     227             :   {
     228           0 :     GNUNET_CURL_job_cancel (ph->job);
     229           0 :     ph->job = NULL;
     230             :   }
     231           8 :   TALER_curl_easy_post_finished (&ph->post_ctx);
     232           8 :   GNUNET_free (ph->url);
     233           8 :   GNUNET_free (ph);
     234           8 : }

Generated by: LCOV version 1.14