exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

testing_api_cmd_withdraw_cs_nonce.c (7163B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2026 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or modify
      6   it under the terms of the GNU General Public License as
      7   published by the Free Software Foundation; either version 3, or
      8   (at your option) any later version.
      9 
     10   TALER is distributed in the hope that it will be useful, but
     11   WITHOUT ANY WARRANTY; without even the implied warranty of
     12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13   GNU General Public License for more details.
     14 
     15   You should have received a copy of the GNU General Public
     16   License along with TALER; see the file COPYING.  If not, see
     17   <http://www.gnu.org/licenses/>
     18 */
     19 /**
     20  * @file testing/testing_api_cmd_withdraw_cs_nonce.c
     21  * @brief withdraw one Clause-Schnorr planchet whose envelope nonce is
     22  *        either the nonce derived from the request's blinding seed or
     23  *        a different one; the exchange must accept only the former.
     24  */
     25 #include "taler/taler_json_lib.h"
     26 #include <gnunet/gnunet_curl_lib.h>
     27 struct CsNonceState;
     28 #define TALER_EXCHANGE_POST_WITHDRAW_BLINDED_RESULT_CLOSURE \
     29         struct CsNonceState
     30 #include "taler/exchange/post-withdraw_blinded.h"
     31 #include "taler/taler_testing_lib.h"
     32 
     33 
     34 /**
     35  * State of a "withdraw with CS nonce" command.
     36  */
     37 struct CsNonceState
     38 {
     39 
     40   /**
     41    * Label of the command that created the reserve.
     42    */
     43   const char *reserve_reference;
     44 
     45   /**
     46    * Amount (value) of the denomination to withdraw.
     47    */
     48   struct TALER_Amount amount;
     49 
     50   /**
     51    * True: the envelope carries the derived nonce; false: a random one.
     52    */
     53   bool derived_nonce;
     54 
     55   /**
     56    * Seed for the CS nonces of the request.
     57    */
     58   struct TALER_BlindingMasterSeedP blinding_seed;
     59 
     60   /**
     61    * The planchet to withdraw.
     62    */
     63   struct TALER_EXCHANGE_WithdrawBlindedCoinInput input;
     64 
     65   /**
     66    * Handle of the withdraw operation.
     67    */
     68   struct TALER_EXCHANGE_PostWithdrawBlindedHandle *wbh;
     69 
     70   /**
     71    * Interpreter state.
     72    */
     73   struct TALER_TESTING_Interpreter *is;
     74 };
     75 
     76 
     77 /**
     78  * Fill @a s with a uniformly random scalar below 2^252, which is below
     79  * the order of the Ed25519 group.
     80  *
     81  * @param[out] s scalar to set
     82  */
     83 static void
     84 random_scalar (struct GNUNET_CRYPTO_Cs25519Scalar *s)
     85 {
     86   GNUNET_CRYPTO_random_block (s->d,
     87                               sizeof (s->d));
     88   s->d[31] &= 0x0f;
     89 }
     90 
     91 
     92 static void
     93 cs_nonce_cb (struct CsNonceState *ns,
     94              const struct TALER_EXCHANGE_PostWithdrawBlindedResponse *wr)
     95 {
     96   unsigned int status = wr->hr.http_status;
     97 
     98   ns->wbh = NULL;
     99   if (ns->derived_nonce)
    100   {
    101     if (MHD_HTTP_OK != status)
    102     {
    103       TALER_TESTING_unexpected_status (ns->is,
    104                                        status,
    105                                        MHD_HTTP_OK);
    106       return;
    107     }
    108   }
    109   else if ( (status < 400) ||
    110             (status >= 500) )
    111   {
    112     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    113                 "The exchange answered %u to a CS planchet whose nonce was not derived from the blinding seed; expected a client error (4xx)\n",
    114                 status);
    115     TALER_TESTING_interpreter_fail (ns->is);
    116     return;
    117   }
    118   TALER_TESTING_interpreter_next (ns->is);
    119 }
    120 
    121 
    122 static void
    123 cs_nonce_run (void *cls,
    124               const struct TALER_TESTING_Command *cmd,
    125               struct TALER_TESTING_Interpreter *is)
    126 {
    127   struct CsNonceState *ns = cls;
    128   const struct TALER_TESTING_Command *create_reserve;
    129   const struct TALER_ReservePrivateKeyP *rp;
    130   const struct TALER_EXCHANGE_DenomPublicKey *dpk;
    131   struct GNUNET_CRYPTO_BlindedMessage *bm;
    132   struct GNUNET_CRYPTO_CsBlindedMessage *cbm;
    133 
    134   (void) cmd;
    135   ns->is = is;
    136   create_reserve
    137     = TALER_TESTING_interpreter_lookup_command (is,
    138                                                 ns->reserve_reference);
    139   if ( (NULL == create_reserve) ||
    140        (GNUNET_OK !=
    141         TALER_TESTING_get_trait_reserve_priv (create_reserve,
    142                                               &rp)) )
    143   {
    144     GNUNET_break (0);
    145     TALER_TESTING_interpreter_fail (is);
    146     return;
    147   }
    148   dpk = TALER_TESTING_find_pk (TALER_TESTING_get_keys (is),
    149                                &ns->amount,
    150                                false);
    151   if ( (NULL == dpk) ||
    152        (GNUNET_CRYPTO_BSA_CS != dpk->key.bsign_pub_key->cipher) )
    153   {
    154     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    155                 "No Clause-Schnorr denomination for %s\n",
    156                 TALER_amount2s (&ns->amount));
    157     TALER_TESTING_interpreter_fail (is);
    158     return;
    159   }
    160   GNUNET_CRYPTO_random_block (&ns->blinding_seed,
    161                               sizeof (ns->blinding_seed));
    162   bm = GNUNET_new (struct GNUNET_CRYPTO_BlindedMessage);
    163   bm->cipher = GNUNET_CRYPTO_BSA_CS;
    164   bm->rc = 1;
    165   cbm = &bm->details.cs_blinded_message;
    166   if (ns->derived_nonce)
    167     TALER_cs_nonce_derive_indexed (&ns->blinding_seed,
    168                                    false, /* not for melt */
    169                                    0, /* coin index */
    170                                    &cbm->nonce);
    171   else
    172     GNUNET_CRYPTO_random_block (&cbm->nonce,
    173                                 sizeof (cbm->nonce));
    174   /* The exchange cannot tell these from blinded challenges. */
    175   random_scalar (&cbm->c[0].scalar);
    176   random_scalar (&cbm->c[1].scalar);
    177   ns->input.denom_pub = dpk;
    178   ns->input.planchet_details.denom_pub_hash = dpk->h_key;
    179   ns->input.planchet_details.blinded_planchet.blinded_message = bm;
    180   ns->wbh = TALER_EXCHANGE_post_withdraw_blinded_create (
    181     TALER_TESTING_interpreter_get_context (is),
    182     TALER_TESTING_get_keys (is),
    183     TALER_TESTING_get_exchange_url (is),
    184     rp,
    185     &ns->blinding_seed,
    186     1,
    187     &ns->input);
    188   GNUNET_assert (NULL != ns->wbh);
    189   GNUNET_assert (TALER_EC_NONE ==
    190                  TALER_EXCHANGE_post_withdraw_blinded_start (ns->wbh,
    191                                                              &cs_nonce_cb,
    192                                                              ns));
    193 }
    194 
    195 
    196 static void
    197 cs_nonce_cleanup (void *cls,
    198                   const struct TALER_TESTING_Command *cmd)
    199 {
    200   struct CsNonceState *ns = cls;
    201 
    202   if (NULL != ns->wbh)
    203   {
    204     TALER_TESTING_command_incomplete (ns->is,
    205                                       cmd->label);
    206     TALER_EXCHANGE_post_withdraw_blinded_cancel (ns->wbh);
    207     ns->wbh = NULL;
    208   }
    209   if (NULL != ns->input.planchet_details.blinded_planchet.blinded_message)
    210     TALER_blinded_planchet_free (
    211       &ns->input.planchet_details.blinded_planchet);
    212   GNUNET_free (ns);
    213 }
    214 
    215 
    216 struct TALER_TESTING_Command
    217 TALER_TESTING_cmd_withdraw_cs_nonce (const char *label,
    218                                      const char *reserve_reference,
    219                                      const char *amount,
    220                                      bool derived_nonce)
    221 {
    222   struct CsNonceState *ns;
    223 
    224   ns = GNUNET_new (struct CsNonceState);
    225   ns->reserve_reference = reserve_reference;
    226   ns->derived_nonce = derived_nonce;
    227   GNUNET_assert (GNUNET_OK ==
    228                  TALER_string_to_amount (amount,
    229                                          &ns->amount));
    230   {
    231     struct TALER_TESTING_Command cmd = {
    232       .cls = ns,
    233       .label = label,
    234       .run = &cs_nonce_run,
    235       .cleanup = &cs_nonce_cleanup
    236     };
    237 
    238     return cmd;
    239   }
    240 }
    241 
    242 
    243 /* end of testing_api_cmd_withdraw_cs_nonce.c */