LCOV - code coverage report
Current view: top level - lib - exchange_api_get-management-keys.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 68.6 % 172 118
Test Date: 2026-09-09 15:11:34 Functions: 100.0 % 5 5

            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_get-management-keys.c
      19              :  * @brief functions to obtain future online keys of the exchange
      20              :  * @author Christian Grothoff
      21              :  */
      22              : #include "taler/taler_json_lib.h"
      23              : #include <gnunet/gnunet_curl_lib.h>
      24              : #include <microhttpd.h>
      25              : #include "taler/exchange/get-management-keys.h"
      26              : #include "exchange_api_curl_defaults.h"
      27              : #include "taler/taler_signatures.h"
      28              : #include "taler/taler_curl_lib.h"
      29              : #include "taler/taler_util.h"
      30              : 
      31              : /**
      32              :  * Set to 1 for extra debug logging.
      33              :  */
      34              : #define DEBUG 0
      35              : 
      36              : 
      37              : /**
      38              :  * @brief Handle for a GET /management/keys request.
      39              :  */
      40              : struct TALER_EXCHANGE_GetManagementKeysHandle
      41              : {
      42              : 
      43              :   /**
      44              :    * The base URL for this request.
      45              :    */
      46              :   char *base_url;
      47              : 
      48              :   /**
      49              :    * The full URL for this request, set during _start.
      50              :    */
      51              :   char *url;
      52              : 
      53              :   /**
      54              :    * Handle for the request.
      55              :    */
      56              :   struct GNUNET_CURL_Job *job;
      57              : 
      58              :   /**
      59              :    * Function to call with the result.
      60              :    */
      61              :   TALER_EXCHANGE_GetManagementKeysCallback cb;
      62              : 
      63              :   /**
      64              :    * Closure for @a cb.
      65              :    */
      66              :   TALER_EXCHANGE_GET_MANAGEMENT_KEYS_RESULT_CLOSURE *cb_cls;
      67              : 
      68              :   /**
      69              :    * Reference to the execution context.
      70              :    */
      71              :   struct GNUNET_CURL_Context *ctx;
      72              : 
      73              : };
      74              : 
      75              : 
      76              : /**
      77              :  * Handle the case that the response was of type #MHD_HTTP_OK.
      78              :  *
      79              :  * @param[in,out] gmkh request handle
      80              :  * @param response the response
      81              :  * @return #GNUNET_OK if the response was well-formed
      82              :  */
      83              : static enum GNUNET_GenericReturnValue
      84           19 : handle_ok (struct TALER_EXCHANGE_GetManagementKeysHandle *gmkh,
      85              :            const json_t *response)
      86              : {
      87           19 :   struct TALER_EXCHANGE_GetManagementKeysResponse gkr = {
      88              :     .hr.http_status = MHD_HTTP_OK,
      89              :     .hr.reply = response,
      90              :   };
      91           19 :   struct TALER_EXCHANGE_FutureKeys *fk
      92              :     = &gkr.details.ok.keys;
      93              :   const json_t *sk;
      94              :   const json_t *dk;
      95              :   bool ok;
      96              :   struct GNUNET_JSON_Specification spec[] = {
      97           19 :     GNUNET_JSON_spec_array_const ("future_denoms",
      98              :                                   &dk),
      99           19 :     GNUNET_JSON_spec_array_const ("future_signkeys",
     100              :                                   &sk),
     101           19 :     GNUNET_JSON_spec_fixed_auto ("master_pub",
     102              :                                  &fk->master_pub),
     103           19 :     GNUNET_JSON_spec_fixed_auto ("denom_secmod_public_key",
     104              :                                  &fk->denom_secmod_public_key),
     105           19 :     GNUNET_JSON_spec_fixed_auto ("denom_secmod_cs_public_key",
     106              :                                  &fk->denom_secmod_cs_public_key),
     107           19 :     GNUNET_JSON_spec_fixed_auto ("signkey_secmod_public_key",
     108              :                                  &fk->signkey_secmod_public_key),
     109           19 :     GNUNET_JSON_spec_end ()
     110              :   };
     111              : 
     112           19 :   if (GNUNET_OK !=
     113           19 :       GNUNET_JSON_parse (response,
     114              :                          spec,
     115              :                          NULL, NULL))
     116              :   {
     117            0 :     GNUNET_break_op (0);
     118            0 :     return GNUNET_SYSERR;
     119              :   }
     120           19 :   fk->num_sign_keys = json_array_size (sk);
     121           19 :   fk->num_denom_keys = json_array_size (dk);
     122           19 :   fk->sign_keys = GNUNET_new_array (
     123              :     fk->num_sign_keys,
     124              :     struct TALER_EXCHANGE_FutureSigningPublicKey);
     125           19 :   fk->denom_keys = GNUNET_new_array (
     126              :     fk->num_denom_keys,
     127              :     struct TALER_EXCHANGE_FutureDenomPublicKey);
     128           19 :   ok = true;
     129           40 :   for (unsigned int i = 0; i < fk->num_sign_keys; i++)
     130              :   {
     131           21 :     json_t *j = json_array_get (sk,
     132              :                                 i);
     133           21 :     struct TALER_EXCHANGE_FutureSigningPublicKey *sign_key
     134           21 :       = &fk->sign_keys[i];
     135              :     struct GNUNET_JSON_Specification ispec[] = {
     136           21 :       GNUNET_JSON_spec_fixed_auto ("key",
     137              :                                    &sign_key->key),
     138           21 :       GNUNET_JSON_spec_fixed_auto ("signkey_secmod_sig",
     139              :                                    &sign_key->signkey_secmod_sig),
     140           21 :       GNUNET_JSON_spec_timestamp ("stamp_start",
     141              :                                   &sign_key->valid_from),
     142           21 :       GNUNET_JSON_spec_timestamp ("stamp_expire",
     143              :                                   &sign_key->valid_until),
     144           21 :       GNUNET_JSON_spec_timestamp ("stamp_end",
     145              :                                   &sign_key->valid_legal),
     146           21 :       GNUNET_JSON_spec_end ()
     147              :     };
     148              : 
     149           21 :     if (GNUNET_OK !=
     150           21 :         GNUNET_JSON_parse (j,
     151              :                            ispec,
     152              :                            NULL, NULL))
     153              :     {
     154            0 :       GNUNET_break_op (0);
     155            0 :       ok = false;
     156            0 :       break;
     157              :     }
     158              :     {
     159              :       struct GNUNET_TIME_Relative duration
     160           21 :         = GNUNET_TIME_absolute_get_difference (sign_key->valid_from.abs_time,
     161              :                                                sign_key->valid_until.abs_time);
     162              : 
     163           21 :       if (GNUNET_OK !=
     164           21 :           TALER_exchange_secmod_eddsa_verify (
     165           21 :             &sign_key->key,
     166              :             sign_key->valid_from,
     167              :             duration,
     168           21 :             &fk->signkey_secmod_public_key,
     169           21 :             &sign_key->signkey_secmod_sig))
     170              :       {
     171            0 :         GNUNET_break_op (0);
     172            0 :         ok = false;
     173            0 :         break;
     174              :       }
     175              :     }
     176              :   }
     177          486 :   for (unsigned int i = 0; i < fk->num_denom_keys; i++)
     178              :   {
     179          467 :     json_t *j = json_array_get (dk,
     180              :                                 i);
     181          467 :     struct TALER_EXCHANGE_FutureDenomPublicKey *denom_key
     182          467 :       = &fk->denom_keys[i];
     183              :     const char *section_name;
     184              :     struct GNUNET_JSON_Specification ispec[] = {
     185          467 :       TALER_JSON_spec_amount_any ("value",
     186              :                                   &denom_key->value),
     187          467 :       GNUNET_JSON_spec_timestamp ("stamp_start",
     188              :                                   &denom_key->valid_from),
     189          467 :       GNUNET_JSON_spec_timestamp ("stamp_expire_withdraw",
     190              :                                   &denom_key->withdraw_valid_until),
     191          467 :       GNUNET_JSON_spec_timestamp ("stamp_expire_deposit",
     192              :                                   &denom_key->expire_deposit),
     193          467 :       GNUNET_JSON_spec_timestamp ("stamp_expire_legal",
     194              :                                   &denom_key->expire_legal),
     195          467 :       TALER_JSON_spec_denom_pub ("denom_pub",
     196              :                                  &denom_key->key),
     197          467 :       TALER_JSON_spec_amount_any ("fee_withdraw",
     198              :                                   &denom_key->fee_withdraw),
     199          467 :       TALER_JSON_spec_amount_any ("fee_deposit",
     200              :                                   &denom_key->fee_deposit),
     201          467 :       TALER_JSON_spec_amount_any ("fee_refresh",
     202              :                                   &denom_key->fee_refresh),
     203          467 :       TALER_JSON_spec_amount_any ("fee_refund",
     204              :                                   &denom_key->fee_refund),
     205          467 :       GNUNET_JSON_spec_fixed_auto ("denom_secmod_sig",
     206              :                                    &denom_key->denom_secmod_sig),
     207          467 :       GNUNET_JSON_spec_string ("section_name",
     208              :                                &section_name),
     209          467 :       GNUNET_JSON_spec_end ()
     210              :     };
     211              : 
     212          467 :     if (GNUNET_OK !=
     213          467 :         GNUNET_JSON_parse (j,
     214              :                            ispec,
     215              :                            NULL, NULL))
     216              :     {
     217            0 :       GNUNET_break_op (0);
     218              : #if DEBUG
     219              :       json_dumpf (j,
     220              :                   stderr,
     221              :                   JSON_INDENT (2));
     222              : #endif
     223            0 :       ok = false;
     224            0 :       break;
     225              :     }
     226              : 
     227              :     {
     228              :       struct TALER_DenominationHashP h_denom_pub;
     229              :       struct GNUNET_TIME_Relative duration
     230          467 :         = GNUNET_TIME_absolute_get_difference (
     231              :             denom_key->valid_from.abs_time,
     232              :             denom_key->withdraw_valid_until.abs_time);
     233              : 
     234          467 :       TALER_denom_pub_hash (&denom_key->key,
     235              :                             &h_denom_pub);
     236          467 :       switch (denom_key->key.bsign_pub_key->cipher)
     237              :       {
     238          258 :       case GNUNET_CRYPTO_BSA_RSA:
     239              :         {
     240              :           struct TALER_RsaPubHashP h_rsa;
     241              : 
     242          258 :           TALER_rsa_pub_hash (
     243          258 :             denom_key->key.bsign_pub_key->details.rsa_public_key,
     244              :             &h_rsa);
     245          258 :           if (GNUNET_OK !=
     246          258 :               TALER_exchange_secmod_rsa_verify (&h_rsa,
     247              :                                                 section_name,
     248              :                                                 denom_key->valid_from,
     249              :                                                 duration,
     250          258 :                                                 &fk->denom_secmod_public_key,
     251          258 :                                                 &denom_key->denom_secmod_sig))
     252              :           {
     253            0 :             GNUNET_break_op (0);
     254            0 :             ok = false;
     255            0 :             break;
     256              :           }
     257              :         }
     258          258 :         break;
     259          209 :       case GNUNET_CRYPTO_BSA_CS:
     260              :         {
     261              :           struct TALER_CsPubHashP h_cs;
     262              : 
     263          209 :           TALER_cs_pub_hash (
     264          209 :             &denom_key->key.bsign_pub_key->details.cs_public_key,
     265              :             &h_cs);
     266          209 :           if (GNUNET_OK !=
     267          209 :               TALER_exchange_secmod_cs_verify (&h_cs,
     268              :                                                section_name,
     269              :                                                denom_key->valid_from,
     270              :                                                duration,
     271          209 :                                                &fk->denom_secmod_cs_public_key,
     272          209 :                                                &denom_key->denom_secmod_sig))
     273              :           {
     274            0 :             GNUNET_break_op (0);
     275            0 :             ok = false;
     276            0 :             break;
     277              :           }
     278              :         }
     279          209 :         break;
     280            0 :       default:
     281            0 :         GNUNET_break_op (0);
     282            0 :         ok = false;
     283            0 :         break;
     284              :       }
     285              :     }
     286          467 :     if (! ok)
     287            0 :       break;
     288              :   }
     289           19 :   if (ok)
     290              :   {
     291           19 :     gmkh->cb (gmkh->cb_cls,
     292              :               &gkr);
     293              :   }
     294          486 :   for (unsigned int i = 0; i < fk->num_denom_keys; i++)
     295          467 :     TALER_denom_pub_free (&fk->denom_keys[i].key);
     296           19 :   GNUNET_free (fk->sign_keys);
     297           19 :   GNUNET_free (fk->denom_keys);
     298           19 :   return (ok) ? GNUNET_OK : GNUNET_SYSERR;
     299              : }
     300              : 
     301              : 
     302              : /**
     303              :  * Function called when we're done processing the
     304              :  * HTTP GET /management/keys request.
     305              :  *
     306              :  * @param cls the `struct TALER_EXCHANGE_GetManagementKeysHandle`
     307              :  * @param response_code HTTP response code, 0 on error
     308              :  * @param response response body, NULL if not in JSON
     309              :  */
     310              : static void
     311           19 : handle_get_keys_finished (void *cls,
     312              :                           long response_code,
     313              :                           const void *response)
     314              : {
     315           19 :   struct TALER_EXCHANGE_GetManagementKeysHandle *gmkh = cls;
     316           19 :   const json_t *json = response;
     317           19 :   struct TALER_EXCHANGE_GetManagementKeysResponse gkr = {
     318           19 :     .hr.http_status = (unsigned int) response_code,
     319              :     .hr.reply = json
     320              :   };
     321              : 
     322           19 :   gmkh->job = NULL;
     323           19 :   switch (response_code)
     324              :   {
     325           19 :   case MHD_HTTP_OK:
     326           19 :     if (GNUNET_OK ==
     327           19 :         handle_ok (gmkh,
     328              :                    response))
     329              :     {
     330           19 :       gmkh->cb = NULL;
     331              :     }
     332              :     else
     333              :     {
     334            0 :       GNUNET_break_op (0);
     335            0 :       gkr.hr.http_status = 0;
     336            0 :       gkr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
     337              :     }
     338           19 :     break;
     339            0 :   case MHD_HTTP_NOT_FOUND:
     340            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     341              :                 "Server did not find handler at `%s'. Did you configure the correct exchange base URL?\n",
     342              :                 gmkh->url);
     343            0 :     if (NULL != json)
     344              :     {
     345            0 :       gkr.hr.ec = TALER_JSON_get_error_code (json);
     346            0 :       gkr.hr.hint = TALER_JSON_get_error_hint (json);
     347              :     }
     348              :     else
     349              :     {
     350            0 :       gkr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     351            0 :       gkr.hr.hint = TALER_ErrorCode_get_hint (gkr.hr.ec);
     352              :     }
     353            0 :     break;
     354            0 :   default:
     355              :     /* unexpected response code */
     356            0 :     if (NULL != json)
     357              :     {
     358            0 :       gkr.hr.ec = TALER_JSON_get_error_code (json);
     359            0 :       gkr.hr.hint = TALER_JSON_get_error_hint (json);
     360              :     }
     361              :     else
     362              :     {
     363            0 :       gkr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     364            0 :       gkr.hr.hint = TALER_ErrorCode_get_hint (gkr.hr.ec);
     365              :     }
     366            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     367              :                 "Unexpected response code %u/%d for exchange management get keys\n",
     368              :                 (unsigned int) response_code,
     369              :                 (int) gkr.hr.ec);
     370            0 :     break;
     371              :   }
     372           19 :   if (NULL != gmkh->cb)
     373              :   {
     374            0 :     gmkh->cb (gmkh->cb_cls,
     375              :               &gkr);
     376            0 :     gmkh->cb = NULL;
     377              :   }
     378           19 :   TALER_EXCHANGE_get_management_keys_cancel (gmkh);
     379           19 : }
     380              : 
     381              : 
     382              : struct TALER_EXCHANGE_GetManagementKeysHandle *
     383           19 : TALER_EXCHANGE_get_management_keys_create (
     384              :   struct GNUNET_CURL_Context *ctx,
     385              :   const char *url)
     386              : {
     387              :   struct TALER_EXCHANGE_GetManagementKeysHandle *gmkh;
     388              : 
     389           19 :   gmkh = GNUNET_new (struct TALER_EXCHANGE_GetManagementKeysHandle);
     390           19 :   gmkh->ctx = ctx;
     391           19 :   gmkh->base_url = GNUNET_strdup (url);
     392           19 :   return gmkh;
     393              : }
     394              : 
     395              : 
     396              : enum TALER_ErrorCode
     397           19 : TALER_EXCHANGE_get_management_keys_start (
     398              :   struct TALER_EXCHANGE_GetManagementKeysHandle *gmkh,
     399              :   TALER_EXCHANGE_GetManagementKeysCallback cb,
     400              :   TALER_EXCHANGE_GET_MANAGEMENT_KEYS_RESULT_CLOSURE *cb_cls)
     401              : {
     402              :   CURL *eh;
     403              : 
     404           19 :   gmkh->cb = cb;
     405           19 :   gmkh->cb_cls = cb_cls;
     406           19 :   gmkh->url = TALER_url_join (gmkh->base_url,
     407              :                               "management/keys",
     408              :                               NULL);
     409           19 :   if (NULL == gmkh->url)
     410              :   {
     411            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     412              :                 "Could not construct request URL.\n");
     413            0 :     return TALER_EC_GENERIC_CONFIGURATION_INVALID;
     414              :   }
     415           19 :   eh = TALER_EXCHANGE_curl_easy_get_ (gmkh->url);
     416           19 :   if (NULL == eh)
     417              :   {
     418            0 :     GNUNET_break (0);
     419            0 :     GNUNET_free (gmkh->url);
     420            0 :     gmkh->url = NULL;
     421            0 :     return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
     422              :   }
     423           19 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     424              :               "Requesting URL '%s'\n",
     425              :               gmkh->url);
     426           19 :   gmkh->job = GNUNET_CURL_job_add (gmkh->ctx,
     427              :                                    eh,
     428              :                                    &handle_get_keys_finished,
     429              :                                    gmkh);
     430           19 :   if (NULL == gmkh->job)
     431              :   {
     432            0 :     GNUNET_free (gmkh->url);
     433            0 :     gmkh->url = NULL;
     434            0 :     return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
     435              :   }
     436           19 :   return TALER_EC_NONE;
     437              : }
     438              : 
     439              : 
     440              : void
     441           19 : TALER_EXCHANGE_get_management_keys_cancel (
     442              :   struct TALER_EXCHANGE_GetManagementKeysHandle *gmkh)
     443              : {
     444           19 :   if (NULL != gmkh->job)
     445              :   {
     446            0 :     GNUNET_CURL_job_cancel (gmkh->job);
     447            0 :     gmkh->job = NULL;
     448              :   }
     449           19 :   GNUNET_free (gmkh->url);
     450           19 :   GNUNET_free (gmkh->base_url);
     451           19 :   GNUNET_free (gmkh);
     452           19 : }
     453              : 
     454              : 
     455              : /* end of exchange_api_get-management-keys.c */
        

Generated by: LCOV version 2.0-1