exchange

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

commit fac62a8376e7925dd60611fe7a122a21b3cd2013
parent 1c550f2a985fb1e9c52c2606fe87f2857433e203
Author: Özgür Kesim <oec@codeblau.de>
Date:   Sun,  8 Nov 2026 15:33:21 +0100

[exchange] melt: prove denomination conflicts of zombie coins (#11916)

As the v42 spec promises; TEH_DB_reply_coin_denomination_conflict() builds the proof for all paths, from one query.

Diffstat:
Msrc/exchange/taler-exchange-httpd_db.c | 96++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
Msrc/exchange/taler-exchange-httpd_db.h | 17+++++++++++++++++
Msrc/exchange/taler-exchange-httpd_post-melt.c | 6+++---
Asrc/exchangedb/get_denomination_proof_for_known_coin.c | 65+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/exchangedb/meson.build | 1+
Msrc/exchangedb/test_known_coins.c | 61++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Asrc/include/exchange-database/get_denomination_proof_for_known_coin.h | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/testing/test_exchange_api_revocation.c | 50++++++++++++++++++++++++++++++++++++++++++++++++++
8 files changed, 305 insertions(+), 42 deletions(-)

diff --git a/src/exchange/taler-exchange-httpd_db.c b/src/exchange/taler-exchange-httpd_db.c @@ -31,6 +31,7 @@ #include "exchange-database/start.h" #include "exchange-database/commit.h" #include "exchange-database/do_insert_known_coins.h" +#include "exchange-database/get_denomination_proof_for_known_coin.h" #include "exchange-database/get_signature_for_known_coin.h" #include "exchange-database/preflight.h" #include "exchange-database/rollback.h" @@ -104,44 +105,10 @@ TEH_make_coins_known ( continue; case TALER_EXCHANGEDB_CKS_DENOM_CONFLICT: /* The exchange has seen this coin before, but with a different - * denomination. Get the corresponding signature and send it to - * the client as proof */ - { - struct - { - struct TALER_DenominationPublicKey pub; - struct TALER_DenominationSignature sig; - } prev_denom = {0}; - - if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != - TALER_EXCHANGEDB_get_signature_for_known_coin (TEH_pg, - &coin->coin_pub, - &prev_denom.pub, - &prev_denom.sig)) - { - /* There _should_ have been a result, because - * we ended here due to a conflict! */ - GNUNET_break (0); - *mhd_ret = TALER_MHD_reply_with_error (connection, - MHD_HTTP_INTERNAL_SERVER_ERROR, - TALER_EC_GENERIC_DB_FETCH_FAILED, - NULL); - return GNUNET_DB_STATUS_HARD_ERROR; - } - - *mhd_ret = TEH_RESPONSE_reply_coin_denomination_conflict ( - connection, - TALER_EC_EXCHANGE_GENERIC_COIN_CONFLICTING_DENOMINATION_KEY, - &coin->coin_pub, - &prev_denom.pub, - &prev_denom.sig, - res->no_age_commitment - ? NULL - : &res->h_age_commitment); - TALER_denom_pub_free (&prev_denom.pub); - TALER_denom_sig_free (&prev_denom.sig); - return GNUNET_DB_STATUS_HARD_ERROR; - } + * denomination: send the stored one to the client as proof */ + *mhd_ret = TEH_DB_reply_coin_denomination_conflict (connection, + &coin->coin_pub); + return GNUNET_DB_STATUS_HARD_ERROR; case TALER_EXCHANGEDB_CKS_AGE_CONFLICT_EXPECTED_NULL: case TALER_EXCHANGEDB_CKS_AGE_CONFLICT_EXPECTED_NON_NULL: case TALER_EXCHANGEDB_CKS_AGE_CONFLICT_VALUE_DIFFERS: @@ -191,6 +158,59 @@ TEH_make_coins_known ( } +enum MHD_Result +TEH_DB_reply_coin_denomination_conflict ( + struct MHD_Connection *connection, + const struct TALER_CoinSpendPublicKeyP *coin_pub) +{ + struct TALER_DenominationPublicKey denom_pub = { 0 }; + struct TALER_DenominationSignature denom_sig = { 0 }; + struct TALER_AgeCommitmentHashP h_age_commitment; + bool no_age_commitment; + enum MHD_Result ret; + + switch (TALER_EXCHANGEDB_get_denomination_proof_for_known_coin ( + TEH_pg, + coin_pub, + &denom_pub, + &denom_sig, + &h_age_commitment, + &no_age_commitment)) + { + case GNUNET_DB_STATUS_HARD_ERROR: + case GNUNET_DB_STATUS_SOFT_ERROR: + GNUNET_break (0); + return TALER_MHD_reply_with_error ( + connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_GENERIC_DB_FETCH_FAILED, + "get_denomination_proof_for_known_coin"); + case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: + /* the conflict was found with this very coin */ + GNUNET_break (0); + return TALER_MHD_reply_with_error ( + connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_GENERIC_DB_INVARIANT_FAILURE, + "coin with conflicting denomination not found"); + case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: + break; + } + ret = TEH_RESPONSE_reply_coin_denomination_conflict ( + connection, + TALER_EC_EXCHANGE_GENERIC_COIN_CONFLICTING_DENOMINATION_KEY, + coin_pub, + &denom_pub, + &denom_sig, + no_age_commitment + ? NULL + : &h_age_commitment); + TALER_denom_pub_free (&denom_pub); + TALER_denom_sig_free (&denom_sig); + return ret; +} + + enum GNUNET_GenericReturnValue TEH_DB_run_transaction (struct MHD_Connection *connection, const char *name, diff --git a/src/exchange/taler-exchange-httpd_db.h b/src/exchange/taler-exchange-httpd_db.h @@ -41,6 +41,23 @@ /** + * Reply that the exchange knows the coin @a coin_pub under another + * denomination than the one of the request, with the stored denomination, + * denomination signature and age commitment hash of the coin as proof. + * Can be called inside of a database transaction, as by + * #TEH_make_coins_known(), or outside of one, as by /melt. + * + * @param connection MHD request context + * @param coin_pub public key of the (known) coin + * @return MHD result code + */ +enum MHD_Result +TEH_DB_reply_coin_denomination_conflict ( + struct MHD_Connection *connection, + const struct TALER_CoinSpendPublicKeyP *coin_pub); + + +/** * Ensure the @a coins are known in the database, in one round trip, and * handle conflicts and errors. Must be called inside of a database * transaction: on a conflict the reply for the first conflicting coin is diff --git a/src/exchange/taler-exchange-httpd_post-melt.c b/src/exchange/taler-exchange-httpd_post-melt.c @@ -28,6 +28,7 @@ #include "taler/taler_mhd_lib.h" #include "taler-exchange-httpd_post-melt.h" #include "taler-exchange-httpd_responses.h" +#include "taler-exchange-httpd_db.h" #include "taler-exchange-httpd_get-keys.h" #include "taler-exchange-httpd_secmod-helpers.h" #include "taler/taler_util.h" @@ -1456,10 +1457,9 @@ phase_generate_reply_error ( return; case MELT_ERROR_COIN_CONFLICTING_DENOMINATION_KEY: finish_loop (mc, - TALER_MHD_reply_with_ec ( + TEH_DB_reply_coin_denomination_conflict ( mc->rc->connection, - TALER_EC_EXCHANGE_GENERIC_COIN_CONFLICTING_DENOMINATION_KEY, - TALER_B2S (&mc->error.details.denom_h))); + &mc->request.refresh.coin.coin_pub)); return; case MELT_ERROR_COIN_EXPIRED_NO_ZOMBIE: finish_loop (mc, diff --git a/src/exchangedb/get_denomination_proof_for_known_coin.c b/src/exchangedb/get_denomination_proof_for_known_coin.c @@ -0,0 +1,65 @@ +/* + 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 exchangedb/get_denomination_proof_for_known_coin.c + * @brief Implementation of the get_denomination_proof_for_known_coin function for Postgres + * @author Özgür Kesim + */ +#include "taler/taler_pq_lib.h" +#include "exchange-database/get_denomination_proof_for_known_coin.h" +#include "helper.h" + + +enum GNUNET_DB_QueryStatus +TALER_EXCHANGEDB_get_denomination_proof_for_known_coin ( + struct TALER_EXCHANGEDB_PostgresContext *pg, + const struct TALER_CoinSpendPublicKeyP *coin_pub, + struct TALER_DenominationPublicKey *denom_pub, + struct TALER_DenominationSignature *denom_sig, + struct TALER_AgeCommitmentHashP *h_age_commitment, + bool *no_age_commitment) +{ + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (coin_pub), + GNUNET_PQ_query_param_end + }; + struct GNUNET_PQ_ResultSpec rs[] = { + TALER_PQ_result_spec_denom_pub ("denom_pub", + denom_pub), + TALER_PQ_result_spec_denom_sig ("denom_sig", + denom_sig), + GNUNET_PQ_result_spec_allow_null ( + GNUNET_PQ_result_spec_auto_from_type ("age_commitment_hash", + h_age_commitment), + no_age_commitment), + GNUNET_PQ_result_spec_end + }; + + PREPARE (pg, + "get_denomination_proof_for_known_coin", + "SELECT" + " denominations.denom_pub" + ",denom_sig" + ",age_commitment_hash" + " FROM known_coins" + " JOIN denominations USING (denominations_serial)" + " WHERE coin_pub=$1;"); + return GNUNET_PQ_eval_prepared_singleton_select ( + pg->conn, + "get_denomination_proof_for_known_coin", + params, + rs); +} diff --git a/src/exchangedb/meson.build b/src/exchangedb/meson.build @@ -108,6 +108,7 @@ libtalerexchangedb = library( 'get_refresh.c', 'get_reserve_balance.c', 'get_reserve_history.c', + 'get_denomination_proof_for_known_coin.c', 'get_signature_for_known_coin.c', 'iterate_unfinished_close_requests.c', 'iterate_wire_accounts.c', diff --git a/src/exchangedb/test_known_coins.c b/src/exchangedb/test_known_coins.c @@ -22,7 +22,8 @@ * Covers #TALER_EXCHANGEDB_do_insert_known_coins(), * #TALER_EXCHANGEDB_get_known_coin(), * #TALER_EXCHANGEDB_get_coin_denomination(), - * #TALER_EXCHANGEDB_get_signature_for_known_coin() and + * #TALER_EXCHANGEDB_get_signature_for_known_coin(), + * #TALER_EXCHANGEDB_get_denomination_proof_for_known_coin() and * #TALER_EXCHANGEDB_get_count_known_coins(). * * `known_coins` references `denominations`, so a denomination is created @@ -38,6 +39,7 @@ #include "exchange-database/get_known_coin.h" #include "exchange-database/get_coin_denomination.h" #include "exchange-database/get_signature_for_known_coin.h" +#include "exchange-database/get_denomination_proof_for_known_coin.h" #include "exchange-database/get_count_known_coins.h" #include "exchange-database/start.h" #include "exchange-database/rollback.h" @@ -79,6 +81,8 @@ check_empty (struct TALER_EXCHANGEDB_PostgresContext *pg) struct TALER_DenominationHashP h_denom_pub; struct TALER_DenominationPublicKey denom_pub = { 0 }; struct TALER_DenominationSignature denom_sig = { 0 }; + struct TALER_AgeCommitmentHashP h_age_commitment; + bool no_age_commitment; uint64_t known_coin_id; TDB_FILL (coin_pub, @@ -101,6 +105,16 @@ check_empty (struct TALER_EXCHANGEDB_PostgresContext *pg) &denom_sig)); FAILIF (NULL != denom_pub.bsign_pub_key); FAILIF (NULL != denom_sig.unblinded_sig); + FAILIF (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS != + TALER_EXCHANGEDB_get_denomination_proof_for_known_coin ( + pg, + &coin_pub, + &denom_pub, + &denom_sig, + &h_age_commitment, + &no_age_commitment)); + FAILIF (NULL != denom_pub.bsign_pub_key); + FAILIF (NULL != denom_sig.unblinded_sig); /* a denomination that does not exist has no coins */ FAILIF (0 != TALER_EXCHANGEDB_get_count_known_coins (pg, &h_denom_pub)); @@ -159,6 +173,8 @@ check_insert_and_lookup (struct TALER_EXCHANGEDB_PostgresContext *pg) struct TALER_DenominationHashP h_denom_pub; struct TALER_DenominationPublicKey denom_pub = { 0 }; struct TALER_DenominationSignature denom_sig = { 0 }; + struct TALER_AgeCommitmentHashP h_age_commitment; + bool no_age_commitment = false; uint64_t known_coin_id = 0; uint64_t id2 = 0; @@ -227,6 +243,27 @@ check_insert_and_lookup (struct TALER_EXCHANGEDB_PostgresContext *pg) TALER_denom_pub_free (&denom_pub); TALER_denom_sig_free (&denom_sig); + /* the same in one lookup, together with the (absent) age commitment */ + FAILIF_C (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != + TALER_EXCHANGEDB_get_denomination_proof_for_known_coin ( + pg, + &coin.coin_pub, + &denom_pub, + &denom_sig, + &h_age_commitment, + &no_age_commitment), + TDB_coin_free (&coin); TDB_denom_free (&denom)); + FAILIF_C ( (0 != TALER_denom_pub_cmp (&denom_pub, + &denom.pub)) || + (0 != TALER_denom_sig_cmp (&denom_sig, + &coin.denom_sig)) || + (! no_age_commitment), + TALER_denom_pub_free (&denom_pub); + TALER_denom_sig_free (&denom_sig); + TDB_coin_free (&coin); TDB_denom_free (&denom)); + TALER_denom_pub_free (&denom_pub); + TALER_denom_sig_free (&denom_sig); + FAILIF_C (1 != TALER_EXCHANGEDB_get_count_known_coins (pg, &denom.h_denom_pub), TDB_coin_free (&coin); TDB_denom_free (&denom)); @@ -349,6 +386,10 @@ check_age_commitment (struct TALER_EXCHANGEDB_PostgresContext *pg) struct TALER_CoinPublicInfo coin; struct TALER_CoinPublicInfo got; struct TALER_EXCHANGEDB_CoinKnownResult res; + struct TALER_DenominationPublicKey denom_pub = { 0 }; + struct TALER_DenominationSignature denom_sig = { 0 }; + struct TALER_AgeCommitmentHashP h_age_commitment; + bool no_age_commitment = true; TDB_denom (pg, 10, @@ -397,6 +438,24 @@ check_age_commitment (struct TALER_EXCHANGEDB_PostgresContext *pg) TALER_denom_sig_free (&coin.denom_sig); TDB_denom_free (&denom)); TALER_denom_sig_free (&got.denom_sig); + /* the age commitment hash is part of the proof of the denomination */ + FAILIF_C (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != + TALER_EXCHANGEDB_get_denomination_proof_for_known_coin ( + pg, + &coin.coin_pub, + &denom_pub, + &denom_sig, + &h_age_commitment, + &no_age_commitment), + TALER_denom_sig_free (&coin.denom_sig); + TDB_denom_free (&denom)); + TALER_denom_pub_free (&denom_pub); + TALER_denom_sig_free (&denom_sig); + FAILIF_C ( (no_age_commitment) || + (0 != GNUNET_memcmp (&h_age_commitment, + &coin.h_age_commitment)), + TALER_denom_sig_free (&coin.denom_sig); + TDB_denom_free (&denom)); /* a different age commitment for the same coin is a conflict... */ TDB_FILL (coin.h_age_commitment, diff --git a/src/include/exchange-database/get_denomination_proof_for_known_coin.h b/src/include/exchange-database/get_denomination_proof_for_known_coin.h @@ -0,0 +1,51 @@ +/* + 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 src/include/exchange-database/get_denomination_proof_for_known_coin.h + * @brief implementation of the get_denomination_proof_for_known_coin function for Postgres + * @author Özgür Kesim + */ +#ifndef EXCHANGE_DATABASE_GET_DENOMINATION_PROOF_FOR_KNOWN_COIN_H +#define EXCHANGE_DATABASE_GET_DENOMINATION_PROOF_FOR_KNOWN_COIN_H + +#include "taler/taler_json_lib.h" +#include "exchangedb_lib.h" + +/** + * Retrieve what proves the denomination of a known coin: the denomination + * public key, the denomination signature over the coin and the age + * commitment hash covered by that signature, if any. + * + * Primary test table: `known_coins` (see test_known_coins.c). + * + * @param pg the database context + * @param coin_pub the public key of the coin to search for + * @param[out] denom_pub the denomination of the coin, if it is known + * @param[out] denom_sig the denomination signature of the coin, if it is known + * @param[out] h_age_commitment the age commitment hash of the coin, if any + * @param[out] no_age_commitment set to true if the coin has no age commitment + * @return transaction status code + */ +enum GNUNET_DB_QueryStatus +TALER_EXCHANGEDB_get_denomination_proof_for_known_coin ( + struct TALER_EXCHANGEDB_PostgresContext *pg, + const struct TALER_CoinSpendPublicKeyP *coin_pub, + struct TALER_DenominationPublicKey *denom_pub, + struct TALER_DenominationSignature *denom_sig, + struct TALER_AgeCommitmentHashP *h_age_commitment, + bool *no_age_commitment); + +#endif diff --git a/src/testing/test_exchange_api_revocation.c b/src/testing/test_exchange_api_revocation.c @@ -38,6 +38,12 @@ static char *config_file; /** + * Special case: Clause-Schnorr derives the coin key from /csr values, + * so reusing a coin key for another denomination does not work. + */ +static bool uses_cs; + +/** * Our credentials. */ static struct TALER_TESTING_Credentials cred; @@ -150,6 +156,38 @@ run (void *cls, GNUNET_TIME_UNIT_ZERO, "EUR:5", MHD_HTTP_OK), + /* A coin key known under EUR:1, also withdrawn under EUR:5 (which is + revoked below), for the zombie path of /melt. */ + TALER_TESTING_cmd_admin_add_incoming ("create-reserve-zombie", + "EUR:6.02", + &cred.ba, + cred.user42_payto), + TALER_TESTING_cmd_check_bank_admin_transfer ("check-create-reserve-zombie", + "EUR:6.02", + cred.user42_payto, + cred.exchange_payto, + "create-reserve-zombie"), + CMD_EXEC_WIREWATCH ("wirewatch-zombie"), + TALER_TESTING_cmd_withdraw_amount ("withdraw-zombie-coin-5", + "create-reserve-zombie", + "EUR:5", + 0, /* age restriction off */ + MHD_HTTP_OK), + TALER_TESTING_cmd_withdraw_amount_reuse_key ("withdraw-zombie-coin-1", + "create-reserve-zombie", + "EUR:1", + 0, /* age restriction off */ + "withdraw-zombie-coin-5", + MHD_HTTP_OK), + TALER_TESTING_cmd_deposit ( + "deposit-zombie-coin-1", + "withdraw-zombie-coin-1", + 0, + cred.user42_payto, + "{\"items\":[{\"name\":\"zombie ice cream\",\"value\":\"EUR:1\"}]}", + GNUNET_TIME_UNIT_ZERO, + "EUR:1", + MHD_HTTP_OK), /** * Melt SOME of the rest of the coin's value * (EUR:3.17 = 3x EUR:1.03 + 7x EUR:0.13) @@ -180,6 +218,16 @@ run (void *cls, MHD_HTTP_OK, "withdraw-revocation-coin-2", config_file), + /* The coin key is known under EUR:1, so melting it as an EUR:5 coin + takes the zombie path, which must prove the conflict. With CS, + the key was not reused, and the coin of a revoked denomination is + unknown. */ + TALER_TESTING_cmd_melt ("melt-zombie-conflict", + "withdraw-zombie-coin-5", + uses_cs + ? MHD_HTTP_GONE + : MHD_HTTP_CONFLICT, + NULL), /* Refund fully spent coin (which should fail) */ TALER_TESTING_cmd_recoup ("recoup-fully-spent", MHD_HTTP_CONFLICT, @@ -380,6 +428,8 @@ main (int argc, cipher = GNUNET_STRINGS_get_suffix_from_binary_name (argv[0]); GNUNET_assert (NULL != cipher); + uses_cs = (0 == strcmp (cipher, + "cs")); GNUNET_asprintf (&config_file, "test_exchange_api-%s.conf", cipher);