exchange

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

commit 4e256063b85d6c1f29e224a1b761d824bf266eb2
parent 7b658d8da27c903594b45d942a64132e7128d292
Author: Özgür Kesim <oec@codeblau.de>
Date:   Sat, 21 Nov 2026 11:32:59 +0100

testing: Clause-Schnorr nonce must be derived from the blinding seed (#12264)

test_exchange_api_cs_nonce_cs withdraws a CS coin whose planchet
carries a nonce the client chose instead of the one derived from the
blinding seed.  The exchange must refuse it (4xx); it signs it (200).
New command TALER_TESTING_cmd_withdraw_cs_nonce.

Fails on master at withdraw-foreign-nonce.

Issue: https://bugs.gnunet.org/view?id=12264

Diffstat:
Msrc/include/taler/taler_testing_lib.h | 21+++++++++++++++++++++
Msrc/testing/meson.build | 29+++++++++++++++++++++++++++++
Asrc/testing/test_exchange_api_cs_nonce-cs.conf | 4++++
Asrc/testing/test_exchange_api_cs_nonce.c | 115+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/testing/testing_api_cmd_withdraw_cs_nonce.c | 243+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 412 insertions(+), 0 deletions(-)

diff --git a/src/include/taler/taler_testing_lib.h b/src/include/taler/taler_testing_lib.h @@ -1409,6 +1409,27 @@ TALER_TESTING_cmd_coin_history ( /** + * Withdraw one Clause-Schnorr coin of value @a amount with a hand-made + * planchet. If @a derived_nonce, the envelope carries the nonce derived + * from the request's blinding seed and the command expects 200; + * otherwise it carries a random nonce and the command expects a client + * error (4xx), as the exchange must not sign under a nonce the client + * chose. + * + * @param label the command label. + * @param reserve_reference reference to the command creating the reserve. + * @param amount value of the denomination to withdraw. + * @param derived_nonce whether the envelope carries the derived nonce. + * @return the command. + */ +struct TALER_TESTING_Command +TALER_TESTING_cmd_withdraw_cs_nonce (const char *label, + const char *reserve_reference, + const char *amount, + bool derived_nonce); + + +/** * Create a POST "/reserves/$RID/open" command. * * @param label the command label. diff --git a/src/testing/meson.build b/src/testing/meson.build @@ -96,6 +96,7 @@ libtalertesting_la_SOURCES = [ 'testing_api_cmd_wire_add.c', 'testing_api_cmd_wire_del.c', 'testing_api_cmd_withdraw.c', + 'testing_api_cmd_withdraw_cs_nonce.c', 'testing_api_loop.c', 'testing_api_misc.c', 'testing_api_traits.c', @@ -907,7 +908,35 @@ test( ## Distribution +test_exchange_api_cs_nonce_cs = executable( + 'test_exchange_api_cs_nonce_cs', + ['test_exchange_api_cs_nonce.c'], + dependencies: [ + libtalertesting_dep, + libtalerexchange_dep, + libtalerbank_dep, + libtalerfakebank_dep, + libtalerjson_dep, + libtalerutil_dep, + json_dep, + gcrypt_dep, + gnunetutil_dep, + gnunetcurl_dep, + ], + include_directories: [incdir, configuration_inc], + build_by_default: false, + install: false, +) +test( + 'test_exchange_api_cs_nonce_cs', + test_exchange_api_cs_nonce_cs, + workdir: meson.current_build_dir(), + suite: ['testing', 'integrationtests'], + is_parallel: false, +) + EXTRA_DIST = [ + 'test_exchange_api_cs_nonce-cs.conf', 'valgrind.h', 'setup.sh', 'coins-cs.conf', diff --git a/src/testing/test_exchange_api_cs_nonce-cs.conf b/src/testing/test_exchange_api_cs_nonce-cs.conf @@ -0,0 +1,4 @@ +# This file is in the public domain. +# +@INLINE@ coins-cs.conf +@INLINE@ test_exchange_api.conf diff --git a/src/testing/test_exchange_api_cs_nonce.c b/src/testing/test_exchange_api_cs_nonce.c @@ -0,0 +1,115 @@ +/* + This file is part of TALER + Copyright (C) 2026 Taler Systems SA + + TALER is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 3, or + (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public + License along with TALER; see the file COPYING. If not, see + <http://www.gnu.org/licenses/> +*/ +/** + * @file testing/test_exchange_api_cs_nonce.c + * @brief regression test: the exchange must sign a Clause-Schnorr + * planchet only under the nonce derived from the request's + * blinding seed, not under a nonce the client chose. + */ +#include "platform.h" +#include "taler/taler_util.h" +#include "taler/taler_json_lib.h" +#include <gnunet/gnunet_util_lib.h> +#include <microhttpd.h> +#include "taler/taler_bank_service.h" +#include "taler/taler_testing_lib.h" + +/** + * Configuration file we use (CS denominations only). + */ +#define CONFIG_FILE "test_exchange_api_cs_nonce-cs.conf" + +/** + * Credentials for the test. + */ +static struct TALER_TESTING_Credentials cred; + + +/** + * Main function that will tell the interpreter what commands to run. + * + * @param cls closure + * @param is interpreter we use to run commands + */ +static void +run (void *cls, + struct TALER_TESTING_Interpreter *is) +{ + struct TALER_TESTING_Command commands[] = { + TALER_TESTING_cmd_run_fakebank ("run-fakebank", + cred.cfg, + "exchange-account-2"), + TALER_TESTING_cmd_system_start ("start-taler", + CONFIG_FILE, + "-e", + NULL), + TALER_TESTING_cmd_get_exchange ("get-exchange", + cred.cfg, + NULL, + true, + true), + TALER_TESTING_cmd_admin_add_incoming ("create-reserve", + "EUR:10.02", + &cred.ba, + cred.user42_payto), + TALER_TESTING_cmd_check_bank_admin_transfer ("check-create-reserve", + "EUR:10.02", + cred.user42_payto, + cred.exchange_payto, + "create-reserve"), + TALER_TESTING_cmd_exec_wirewatch2 ("wirewatch", + CONFIG_FILE, + "exchange-account-2"), + /* Control: the nonce derived from the blinding seed is accepted. */ + TALER_TESTING_cmd_withdraw_cs_nonce ("withdraw-derived-nonce", + "create-reserve", + "EUR:5", + true), + /* Fails on the current exchange, which signs under the envelope's + nonce without comparing it with the derived one. */ + TALER_TESTING_cmd_withdraw_cs_nonce ("withdraw-foreign-nonce", + "create-reserve", + "EUR:5", + false), + TALER_TESTING_cmd_end () + }; + + (void) cls; + TALER_TESTING_run (is, + commands); +} + + +int +main (int argc, + char *const *argv) +{ + (void) argc; + return TALER_TESTING_main (argv, + "INFO", + CONFIG_FILE, + "exchange-account-2", + TALER_TESTING_BS_FAKEBANK, + &cred, + &run, + NULL); +} + + +/* end of test_exchange_api_cs_nonce.c */ diff --git a/src/testing/testing_api_cmd_withdraw_cs_nonce.c b/src/testing/testing_api_cmd_withdraw_cs_nonce.c @@ -0,0 +1,243 @@ +/* + This file is part of TALER + Copyright (C) 2026 Taler Systems SA + + TALER is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 3, or + (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public + License along with TALER; see the file COPYING. If not, see + <http://www.gnu.org/licenses/> +*/ +/** + * @file testing/testing_api_cmd_withdraw_cs_nonce.c + * @brief withdraw one Clause-Schnorr planchet whose envelope nonce is + * either the nonce derived from the request's blinding seed or + * a different one; the exchange must accept only the former. + */ +#include "taler/taler_json_lib.h" +#include <gnunet/gnunet_curl_lib.h> +struct CsNonceState; +#define TALER_EXCHANGE_POST_WITHDRAW_BLINDED_RESULT_CLOSURE \ + struct CsNonceState +#include "taler/exchange/post-withdraw_blinded.h" +#include "taler/taler_testing_lib.h" + + +/** + * State of a "withdraw with CS nonce" command. + */ +struct CsNonceState +{ + + /** + * Label of the command that created the reserve. + */ + const char *reserve_reference; + + /** + * Amount (value) of the denomination to withdraw. + */ + struct TALER_Amount amount; + + /** + * True: the envelope carries the derived nonce; false: a random one. + */ + bool derived_nonce; + + /** + * Seed for the CS nonces of the request. + */ + struct TALER_BlindingMasterSeedP blinding_seed; + + /** + * The planchet to withdraw. + */ + struct TALER_EXCHANGE_WithdrawBlindedCoinInput input; + + /** + * Handle of the withdraw operation. + */ + struct TALER_EXCHANGE_PostWithdrawBlindedHandle *wbh; + + /** + * Interpreter state. + */ + struct TALER_TESTING_Interpreter *is; +}; + + +/** + * Fill @a s with a uniformly random scalar below 2^252, which is below + * the order of the Ed25519 group. + * + * @param[out] s scalar to set + */ +static void +random_scalar (struct GNUNET_CRYPTO_Cs25519Scalar *s) +{ + GNUNET_CRYPTO_random_block (s->d, + sizeof (s->d)); + s->d[31] &= 0x0f; +} + + +static void +cs_nonce_cb (struct CsNonceState *ns, + const struct TALER_EXCHANGE_PostWithdrawBlindedResponse *wr) +{ + unsigned int status = wr->hr.http_status; + + ns->wbh = NULL; + if (ns->derived_nonce) + { + if (MHD_HTTP_OK != status) + { + TALER_TESTING_unexpected_status (ns->is, + status, + MHD_HTTP_OK); + return; + } + } + else if ( (status < 400) || + (status >= 500) ) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "The exchange answered %u to a CS planchet whose nonce was not derived from the blinding seed; expected a client error (4xx)\n", + status); + TALER_TESTING_interpreter_fail (ns->is); + return; + } + TALER_TESTING_interpreter_next (ns->is); +} + + +static void +cs_nonce_run (void *cls, + const struct TALER_TESTING_Command *cmd, + struct TALER_TESTING_Interpreter *is) +{ + struct CsNonceState *ns = cls; + const struct TALER_TESTING_Command *create_reserve; + const struct TALER_ReservePrivateKeyP *rp; + const struct TALER_EXCHANGE_DenomPublicKey *dpk; + struct GNUNET_CRYPTO_BlindedMessage *bm; + struct GNUNET_CRYPTO_CsBlindedMessage *cbm; + + (void) cmd; + ns->is = is; + create_reserve + = TALER_TESTING_interpreter_lookup_command (is, + ns->reserve_reference); + if ( (NULL == create_reserve) || + (GNUNET_OK != + TALER_TESTING_get_trait_reserve_priv (create_reserve, + &rp)) ) + { + GNUNET_break (0); + TALER_TESTING_interpreter_fail (is); + return; + } + dpk = TALER_TESTING_find_pk (TALER_TESTING_get_keys (is), + &ns->amount, + false); + if ( (NULL == dpk) || + (GNUNET_CRYPTO_BSA_CS != dpk->key.bsign_pub_key->cipher) ) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "No Clause-Schnorr denomination for %s\n", + TALER_amount2s (&ns->amount)); + TALER_TESTING_interpreter_fail (is); + return; + } + GNUNET_CRYPTO_random_block (&ns->blinding_seed, + sizeof (ns->blinding_seed)); + bm = GNUNET_new (struct GNUNET_CRYPTO_BlindedMessage); + bm->cipher = GNUNET_CRYPTO_BSA_CS; + bm->rc = 1; + cbm = &bm->details.cs_blinded_message; + if (ns->derived_nonce) + TALER_cs_nonce_derive_indexed (&ns->blinding_seed, + false, /* not for melt */ + 0, /* coin index */ + &cbm->nonce); + else + GNUNET_CRYPTO_random_block (&cbm->nonce, + sizeof (cbm->nonce)); + /* The exchange cannot tell these from blinded challenges. */ + random_scalar (&cbm->c[0].scalar); + random_scalar (&cbm->c[1].scalar); + ns->input.denom_pub = dpk; + ns->input.planchet_details.denom_pub_hash = dpk->h_key; + ns->input.planchet_details.blinded_planchet.blinded_message = bm; + ns->wbh = TALER_EXCHANGE_post_withdraw_blinded_create ( + TALER_TESTING_interpreter_get_context (is), + TALER_TESTING_get_keys (is), + TALER_TESTING_get_exchange_url (is), + rp, + &ns->blinding_seed, + 1, + &ns->input); + GNUNET_assert (NULL != ns->wbh); + GNUNET_assert (TALER_EC_NONE == + TALER_EXCHANGE_post_withdraw_blinded_start (ns->wbh, + &cs_nonce_cb, + ns)); +} + + +static void +cs_nonce_cleanup (void *cls, + const struct TALER_TESTING_Command *cmd) +{ + struct CsNonceState *ns = cls; + + if (NULL != ns->wbh) + { + TALER_TESTING_command_incomplete (ns->is, + cmd->label); + TALER_EXCHANGE_post_withdraw_blinded_cancel (ns->wbh); + ns->wbh = NULL; + } + if (NULL != ns->input.planchet_details.blinded_planchet.blinded_message) + TALER_blinded_planchet_free ( + &ns->input.planchet_details.blinded_planchet); + GNUNET_free (ns); +} + + +struct TALER_TESTING_Command +TALER_TESTING_cmd_withdraw_cs_nonce (const char *label, + const char *reserve_reference, + const char *amount, + bool derived_nonce) +{ + struct CsNonceState *ns; + + ns = GNUNET_new (struct CsNonceState); + ns->reserve_reference = reserve_reference; + ns->derived_nonce = derived_nonce; + GNUNET_assert (GNUNET_OK == + TALER_string_to_amount (amount, + &ns->amount)); + { + struct TALER_TESTING_Command cmd = { + .cls = ns, + .label = label, + .run = &cs_nonce_run, + .cleanup = &cs_nonce_cleanup + }; + + return cmd; + } +} + + +/* end of testing_api_cmd_withdraw_cs_nonce.c */