diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/exchange/taler-exchange-httpd_responses.c | 3 | ||||
-rw-r--r-- | src/include/taler_exchange_service.h | 264 | ||||
-rw-r--r-- | src/lib/Makefile.am | 1 | ||||
-rw-r--r-- | src/lib/exchange_api_age_withdraw.c | 679 | ||||
-rw-r--r-- | src/lib/exchange_api_age_withdraw_reveal.c | 561 | ||||
-rw-r--r-- | src/testing/testing_api_cmd_age_withdraw.c | 155 |
6 files changed, 1426 insertions, 237 deletions
diff --git a/src/exchange/taler-exchange-httpd_responses.c b/src/exchange/taler-exchange-httpd_responses.c index d1636a0af..863166e00 100644 --- a/src/exchange/taler-exchange-httpd_responses.c +++ b/src/exchange/taler-exchange-httpd_responses.c @@ -23,6 +23,7 @@ * @author Christian Grothoff */ #include "platform.h" +#include <microhttpd.h> #include <zlib.h> #include "taler-exchange-httpd_responses.h" #include "taler_util.h" @@ -1091,7 +1092,7 @@ TEH_RESPONSE_reply_reserve_age_restriction_required ( { return TALER_MHD_REPLY_JSON_PACK ( connection, - MHD_HTTP_BAD_REQUEST, + MHD_HTTP_CONFLICT, TALER_JSON_pack_ec (TALER_EC_EXCHANGE_RESERVES_AGE_RESTRICTION_REQUIRED), GNUNET_JSON_pack_uint64 ("maximum_allowed_age", maximum_allowed_age)); diff --git a/src/include/taler_exchange_service.h b/src/include/taler_exchange_service.h index d19d4fdb8..d98aba254 100644 --- a/src/include/taler_exchange_service.h +++ b/src/include/taler_exchange_service.h @@ -2662,7 +2662,7 @@ TALER_EXCHANGE_batch_withdraw2_cancel ( /* ********************* /reserve/$RESERVE_PUB/age-withdraw *************** */ /** - * @brief Information needed to withdraw age restricted coins. + * @brief Information needed to withdraw (and reveal) age restricted coins. */ struct TALER_EXCHANGE_AgeWithdrawCoinInput { @@ -2670,7 +2670,7 @@ struct TALER_EXCHANGE_AgeWithdrawCoinInput * The master secret from which we derive all other relevant values for * the coin: private key, nonces (if applicable) and age restriction */ - const struct TALER_PlanchetMasterSecretP secret[TALER_CNC_KAPPA]; + const struct TALER_PlanchetMasterSecretP secrets[TALER_CNC_KAPPA]; /** * The denomination of the coin. Must support age restriction, i.e @@ -2712,6 +2712,23 @@ struct TALER_EXCHANGE_AgeWithdrawResponse uint8_t noreveal_index; /** + * The commitment of the call to /age-withdraw + */ + struct TALER_AgeWithdrawCommitmentHashP h_commitment; + + /** + * The algorithm specific values (for CS) need for the coins that were + * retrieved from /csr-withdraw. + */ + struct TALER_ExchangeWithdrawValues *alg_values; + + /** + * Number of elements in @e alg_values, same as number coin candidates.from + * the request. + */ + size_t num_alg_values; + + /** * Signature of the exchange over the origina TALER_AgeWithdrawRequestPS */ struct TALER_ExchangeSignatureP exchange_sig; @@ -2722,7 +2739,6 @@ struct TALER_EXCHANGE_AgeWithdrawResponse struct TALER_ExchangePublicKeyP exchange_pub; } ok; - /* FIXME[oec]: error cases */ } details; }; @@ -2755,11 +2771,11 @@ typedef void struct TALER_EXCHANGE_AgeWithdrawHandle * TALER_EXCHANGE_age_withdraw ( struct GNUNET_CURL_Context *curl_ctx, - const char *exchange_url, struct TALER_EXCHANGE_Keys *keys, + const char *exchange_url, const struct TALER_ReservePrivateKeyP *reserve_priv, size_t num_coins, - const struct TALER_EXCHANGE_AgeWithdrawCoinInput coin_inputs[const static + const struct TALER_EXCHANGE_AgeWithdrawCoinInput coin_inputs[static num_coins], uint8_t max_age, TALER_EXCHANGE_AgeWithdrawCallback res_cb, @@ -2776,6 +2792,244 @@ TALER_EXCHANGE_age_withdraw_cancel ( struct TALER_EXCHANGE_AgeWithdrawHandle *awh); +/**++++++ age-withdraw with pre-blinded planchets ***************************/ + +/** + * @brief Information needed to withdraw (and reveal) age restricted coins. + */ +struct TALER_EXCHANGE_AgeWithdrawBlindedInput +{ + /** + * The denomination of the coin. Must support age restriction, i.e + * its .keys.age_mask MUST not be 0 + */ + const struct TALER_EXCHANGE_DenomPublicKey *denom_pub; + + /** + * Blinded Planchets + */ + struct TALER_PlanchetDetail planchet_details[TALER_CNC_KAPPA]; + +}; + +/** + * Response from an age-withdraw request with pre-blinded planchets + */ +struct TALER_EXCHANGE_AgeWithdrawBlindedResponse +{ + /** + * HTTP response data + */ + struct TALER_EXCHANGE_HttpResponse hr; + + /** + * Response details depending on the HTTP status. + */ + union + { + /** + * Details if HTTP status is #MHD_HTTP_OK. + */ + struct + { + /** + * Index that should not be revealed during the age-withdraw reveal phase. + * The struct TALER_PlanchetMasterSecretP * from the request + * with this index are the ones to keep. + */ + uint8_t noreveal_index; + + /** + * The commitment of the call to /age-withdraw + */ + struct TALER_AgeWithdrawCommitmentHashP h_commitment; + + /** + * Signature of the exchange over the origina TALER_AgeWithdrawRequestPS + */ + struct TALER_ExchangeSignatureP exchange_sig; + + /** + * Key used by the exchange for @e exchange_sig + */ + struct TALER_ExchangePublicKeyP exchange_pub; + } ok; + } details; + +}; + + +/** + * Callbacks of this type are used to serve the result of submitting an + * age-withdraw request to a exchange with pre-blinded planchets + * without the (un)blinding factor. + * + * @param cls closure + * @param awbr response data + */ +typedef void +(*TALER_EXCHANGE_AgeWithdrawBlindedCallback) ( + void *cls, + const struct TALER_EXCHANGE_AgeWithdrawBlindedResponse *awbr); + + +/** + * @brief A /reserves/$RESERVE_PUB/age-withdraw Handle, 2nd variant with + * pre-blinded planchets. + * + * This variant does not do the blinding/unblinding and only + * fetches the blind signatures on the already blinded planchets. + * Used internally by the `struct TALER_EXCHANGE_BatchWithdrawHandle` + * implementation as well as for the reward logic of merchants. + */ +struct TALER_EXCHANGE_BatchWithdraw2Handle; + +/** + * Withdraw age-restricted coins from the exchange using a + * /reserves/$RESERVE_PUB/age-withdraw request. This API is typically used + * by a merchant to withdraw a reward where the planchets are pre-blinded and + * the blinding factor is unknown to the merchant. + * + * Note that to ensure that no money is lost in case of hardware + * failures, the caller must have committed (most of) the arguments to + * disk before calling, and be ready to repeat the request with the + * same arguments in case of failures. + * + * @param curl_ctx The curl context to use + * @param exchange_url The base-URL of the exchange + * @param keys The /keys material from the exchange + * @param num_input number of entries in the @a blinded_input array + * @param blinded_input array of planchet details of the planchet to withdraw + * @param reserve_priv private key of the reserve to withdraw from + * @param res_cb the callback to call when the final result for this request is available + * @param res_cb_cls closure for @a res_cb + * @return NULL + * if the inputs are invalid (i.e. denomination key not with this exchange). + * In this case, the callback is not called. + */ +struct TALER_EXCHANGE_AgeWithdrawBlindedHandle * +TALER_EXCHANGE_age_withdraw_blinded ( + struct GNUNET_CURL_Context *curl_ctx, + struct TALER_EXCHANGE_Keys *keys, + const char *exchange_url, + const struct TALER_ReservePrivateKeyP *reserve_priv, + unsigned int num_input, + const struct TALER_EXCHANGE_AgeWithdrawBlindedInput blinded_input[static + num_input], + TALER_EXCHANGE_AgeWithdrawBlindedCallback res_cb, + void *res_cb_cls); + + +/** + * Cancel an age-withdraw request. This function cannot be used + * on a request handle if a response is already served for it. + * + * @param awbh the age-withdraw handle + */ +void +TALER_EXCHANGE_age_withdraw_blinded_cancel ( + struct TALER_EXCHANGE_AgeWithdrawBlindedHandle *awbh); + + +/* ********************* /age-withdraw/$ACH/reveal ************************ */ + +/** + * @brief A handle to a /age-withdraw/$ACH/reveal request + */ +struct TALER_EXCHANGE_AgeWithdrawRevealHandle; + +/** + * The response from a /age-withdraw/$ACH/reveal request + */ +struct TALER_EXCHANGE_AgeWithdrawRevealResponse +{ + /** + * HTTP response data. + */ + struct TALER_EXCHANGE_HttpResponse hr; + + /** + * Details about the response + */ + union + { + /** + * Details if the status is #MHD_HTTP_OK. + */ + struct + { + /** + * Number of coins returned. + */ + unsigned int num_coins; + + /** + * Array of @e num_coins values about the coins obtained via the reveal + * operation. The array give thes coins in the same order (and should + * have the same length) in which the original age-withdraw request + * specified the respective denomination keys. + */ + const struct TALER_EXCHANGE_RevealedCoinInfo *revealed_coins; + + } ok; + /* FIXME[oec]: error cases */ + } details; + +}; + +typedef void +(*TALER_EXCHANGE_AgeWithdrawRevealCallback)( + void *cls, + const struct TALER_EXCHANGE_AgeWithdrawRevealResponse *awr); + +/** + * Submit an age-withdraw-reveal request to the exchange and get the exchange's + * response. + * + * This API is typically used by a wallet. Note that to ensure that + * no money is lost in case of hardware failures, the provided + * argument @a rd should be committed to persistent storage + * prior to calling this function. + * + * @param curl_ctx The curl context + * @param exchange_url The base url of the exchange + * @param reserve_priv The pivate key to the reserve + * @param num_coins The number of elements in @e coin_inputs and @e alg_values + * @param coins_input The input for the coins to withdraw, same as in the previous call to /age-withdraw + * @param alg_values The algorithm specific parameters per coin, from the result to the previous call to /age-withdraw + * @param noreveal_index The index into each of the kappa coin candidates, that should not be revealed to the exchange + * @param h_commitment The commmitment from the previous call to /age-withdraw + * @param max_age maximum age, as used in the to /age-withdraw + * @param res_cb A callback for the result, maybe NULL + * @param res_cb_cls A closure for @e res_cb, maybe NULL + * @return a handle for this request; NULL if the argument was invalid. + * In this case, the callback will not be called. + */ +struct TALER_EXCHANGE_AgeWithdrawRevealHandle * +TALER_EXCHANGE_age_withdraw_reveal ( + struct GNUNET_CURL_Context *curl_ctx, + const char *exchange_url, + size_t num_coins, + const struct TALER_EXCHANGE_AgeWithdrawCoinInput coins_input[static + num_coins], + const struct TALER_ExchangeWithdrawValues alg_values[static num_coins], + uint8_t noreveal_index, + const struct TALER_AgeWithdrawCommitmentHashP *h_commitment, + uint8_t max_age, + TALER_EXCHANGE_AgeWithdrawRevealCallback res_cb, + void *res_cb_cls); + + +/** + * @brief Cancel an age-withdraw-reveal request + * + * @param awrh Handle to an age-withdraw-reqveal request + */ +void +TALER_EXCHANGE_age_withdraw_reveal_cancel ( + struct TALER_EXCHANGE_AgeWithdrawRevealHandle *awrh); + + /* ********************* /refresh/melt+reveal ***************************** */ diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index 3d4604c13..f69a4e81a 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -23,6 +23,7 @@ libtalerexchange_la_LDFLAGS = \ libtalerexchange_la_SOURCES = \ exchange_api_add_aml_decision.c \ exchange_api_age_withdraw.c \ + exchange_api_age_withdraw_reveal.c \ exchange_api_auditor_add_denomination.c \ exchange_api_batch_deposit.c \ exchange_api_batch_withdraw.c \ diff --git a/src/lib/exchange_api_age_withdraw.c b/src/lib/exchange_api_age_withdraw.c index 7bad025b2..825515581 100644 --- a/src/lib/exchange_api_age_withdraw.c +++ b/src/lib/exchange_api_age_withdraw.c @@ -27,6 +27,7 @@ #include <gnunet/gnunet_util_lib.h> #include <gnunet/gnunet_json_lib.h> #include <gnunet/gnunet_curl_lib.h> +#include <sys/wait.h> #include "taler_curl_lib.h" #include "taler_json_lib.h" #include "taler_exchange_service.h" @@ -35,6 +36,9 @@ #include "taler_signatures.h" #include "exchange_api_curl_defaults.h" +/** + * A CoinCandidate is populated from a master secret + */ struct CoinCandidate { /** @@ -64,11 +68,6 @@ struct CoinCandidate struct TALER_CoinSpendPrivateKeyP coin_priv; /** - * Details of the planchet. - */ - struct TALER_PlanchetDetail planchet_detail; - - /** * Values of the @cipher selected */ struct TALER_ExchangeWithdrawValues alg_values; @@ -78,36 +77,51 @@ struct CoinCandidate */ struct TALER_CoinPubHashP h_coin_pub; - /* Blinded hash of the coin */ - struct TALER_BlindedCoinHashP blinded_coin_h; /** - * The following fields are needed as closure for the call to /csr-withdrwaw - * per coin-candidate. - */ + * Blinded hash of the coin + **/ + struct TALER_BlindedCoinHashP blinded_coin_h; - /* Denomination information, needed for CS coins for the step after /csr-withdraw */ - struct TALER_EXCHANGE_DenomPublicKey *denom_pub; +}; - /** - * Handler for the CS R request (only used for TALER_DENOMINATION_CS denominations) - */ - struct TALER_EXCHANGE_CsRWithdrawHandle *csr_withdraw_handle; - /* Needed in the closure for csr-withdraw calls */ +/** + * Closure for a call to /csr-withdraw, contains data that is needed to process + * the result. + */ +struct CSRClosure +{ + /* Points to the actual candidate in CoinData.coin_candidates, to continue + * to build its contents based on the results from /csr-withdraw */ + struct CoinCandidate *candidate; + + /* The planchet to finally generate. Points to the corresponding candidate + * in CoindData.planchet_details */ + struct TALER_PlanchetDetail *planchet; + + /* Handler to the originating call to /age-withdraw, needed to either + * cancel the running age-withdraw request (on failure of the current call + * to /csr-withdraw), or to eventually perform the protocol, once all + * csr-withdraw requests have successfully finished. */ struct TALER_EXCHANGE_AgeWithdrawHandle *age_withdraw_handle; -}; + /* Denomination information, needed for CS coins for the + * step after /csr-withdraw */ + const struct TALER_EXCHANGE_DenomPublicKey *denom_pub; + /* Handler for the CS R request */ + struct TALER_EXCHANGE_CsRWithdrawHandle *csr_withdraw_handle; +}; /** * Data we keep per coin in the batch. */ struct CoinData { - /** - * Denomination key we are withdrawing. + * The denomination of the coin. Must support age restriction, i.e + * its .keys.age_mask MUST not be 0 */ struct TALER_EXCHANGE_DenomPublicKey denom_pub; @@ -116,13 +130,23 @@ struct CoinData */ struct CoinCandidate coin_candidates[TALER_CNC_KAPPA]; -}; + /** + * Details of the planchet(s). + */ + struct TALER_PlanchetDetail planchet_details[TALER_CNC_KAPPA]; + /** + * Closure for each candidate of type CS for the preflight request to + * /csr-withdraw + */ + struct CSRClosure csr_cls[TALER_CNC_KAPPA]; +}; /** - * @brief A /reserves/$RESERVE_PUB/age-withdraw request-handle + * A /reserves/$RESERVE_PUB/age-withdraw request-handle for calls with + * pre-blinded planchets. Returned by TALER_EXCHANGE_age_withdraw_blinded. */ -struct TALER_EXCHANGE_AgeWithdrawHandle +struct TALER_EXCHANGE_AgeWithdrawBlindedHandle { /** @@ -147,11 +171,6 @@ struct TALER_EXCHANGE_AgeWithdrawHandle struct TALER_EXCHANGE_Keys *keys; /** - * The base-URL of the exchange. - */ - const char *exchange_url; - - /** * The age mask, extacted from the denominations. * MUST be the same for all denominations * @@ -164,16 +183,6 @@ struct TALER_EXCHANGE_AgeWithdrawHandle uint8_t max_age; /** - * Length of the @e coin_data Array - */ - size_t num_coins; - - /** - * Array of per-coin data - */ - struct CoinData *coin_data; - - /** * The commitment calculated as SHA512 hash over all blinded_coin_h */ struct TALER_AgeWithdrawCommitmentHashP h_commitment; @@ -184,9 +193,15 @@ struct TALER_EXCHANGE_AgeWithdrawHandle struct TALER_Amount amount_with_fee; /** - * Number of /csr-withdraw requests still pending. + * Length of the @e blinded_input Array */ - unsigned int csr_pending; + size_t num_input; + + /** + * The blinded planchet input for the call to /age-withdraw via + * TALER_EXCHANGE_age_withdraw_blinded + */ + const struct TALER_EXCHANGE_AgeWithdrawBlindedInput *blinded_input; /** * The url for this request. @@ -211,6 +226,91 @@ struct TALER_EXCHANGE_AgeWithdrawHandle /** * Function to call with age-withdraw response results. */ + TALER_EXCHANGE_AgeWithdrawBlindedCallback callback; + + /** + * Closure for @e blinded_callback + */ + void *callback_cls; +}; + +/** + * A /reserves/$RESERVE_PUB/age-withdraw request-handle for calls from + * a wallet, i. e. when blinding data is available. + */ +struct TALER_EXCHANGE_AgeWithdrawHandle +{ + + /** + * Length of the @e coin_data Array + */ + size_t num_coins; + + /** + * The base-URL of the exchange. + */ + const char *exchange_url; + + /** + * Reserve private key. + */ + const struct TALER_ReservePrivateKeyP *reserve_priv; + + /** + * Reserve public key, calculated + */ + struct TALER_ReservePublicKeyP reserve_pub; + + /** + * Signature of the reserve for the request, calculated after all + * parameters for the coins are collected. + */ + struct TALER_ReserveSignatureP reserve_sig; + + /* + * The denomination keys of the exchange + */ + struct TALER_EXCHANGE_Keys *keys; + + /** + * The age mask, extacted from the denominations. + * MUST be the same for all denominations + * + */ + struct TALER_AgeMask age_mask; + + /** + * Maximum age to commit to. + */ + uint8_t max_age; + + /** + * Array of per-coin data + */ + struct CoinData *coin_data; + + /** + * Context for curl. + */ + struct GNUNET_CURL_Context *curl_ctx; + + struct + { + /** + * Number of /csr-withdraw requests still pending. + */ + unsigned int pending; + + /** + * CURL handle for the request job. + */ + struct GNUNET_CURL_Job *job; + } csr; + + + /** + * Function to call with age-withdraw response results. + */ TALER_EXCHANGE_AgeWithdrawCallback callback; /** @@ -218,6 +318,8 @@ struct TALER_EXCHANGE_AgeWithdrawHandle */ void *callback_cls; + /* The Handler for the actual call to the exchange */ + struct TALER_EXCHANGE_AgeWithdrawBlindedHandle *protocol_handler; }; /** @@ -230,12 +332,13 @@ struct TALER_EXCHANGE_AgeWithdrawHandle */ static enum GNUNET_GenericReturnValue reserve_age_withdraw_ok ( - struct TALER_EXCHANGE_AgeWithdrawHandle *awh, + struct TALER_EXCHANGE_AgeWithdrawBlindedHandle *awbh, const json_t *j_response) { - struct TALER_EXCHANGE_AgeWithdrawResponse response = { + struct TALER_EXCHANGE_AgeWithdrawBlindedResponse response = { .hr.reply = j_response, - .hr.http_status = MHD_HTTP_OK + .hr.http_status = MHD_HTTP_OK, + .details.ok.h_commitment = awbh->h_commitment }; struct GNUNET_JSON_Specification spec[] = { GNUNET_JSON_spec_uint8 ("noreaveal_index", @@ -257,7 +360,7 @@ reserve_age_withdraw_ok ( if (GNUNET_OK != TALER_exchange_online_age_withdraw_confirmation_verify ( - &awh->h_commitment, + &awbh->h_commitment, response.details.ok.noreveal_index, &response.details.ok.exchange_pub, &response.details.ok.exchange_sig)) @@ -266,10 +369,10 @@ reserve_age_withdraw_ok ( return GNUNET_SYSERR; } - awh->callback (awh->callback_cls, - &response); + awbh->callback (awbh->callback_cls, + &response); /* make sure the callback isn't called again */ - awh->callback = NULL; + awbh->callback = NULL; return GNUNET_OK; } @@ -386,36 +489,36 @@ reserve_age_withdraw_payment_required ( * @param response response data */ static void -handle_reserve_age_withdraw_finished ( +handle_reserve_age_withdraw_blinded_finished ( void *cls, long response_code, const void *response) { - struct TALER_EXCHANGE_AgeWithdrawHandle *awh = cls; + struct TALER_EXCHANGE_AgeWithdrawBlindedHandle *awbh = cls; const json_t *j_response = response; - struct TALER_EXCHANGE_AgeWithdrawResponse awr = { + struct TALER_EXCHANGE_AgeWithdrawBlindedResponse awbr = { .hr.reply = j_response, .hr.http_status = (unsigned int) response_code }; - awh->job = NULL; + awbh->job = NULL; switch (response_code) { case 0: - awr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; + awbr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; break; case MHD_HTTP_OK: if (GNUNET_OK != - reserve_age_withdraw_ok (awh, + reserve_age_withdraw_ok (awbh, j_response)) { GNUNET_break_op (0); - awr.hr.http_status = 0; - awr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED; + awbr.hr.http_status = 0; + awbr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED; break; } - GNUNET_assert (NULL == awh->callback); - TALER_EXCHANGE_age_withdraw_cancel (awh); + GNUNET_assert (NULL == awbh->callback); + TALER_EXCHANGE_age_withdraw_blinded_cancel (awbh); return; case MHD_HTTP_UNAVAILABLE_FOR_LEGAL_REASONS: /* only validate reply is well-formed */ @@ -433,50 +536,50 @@ handle_reserve_age_withdraw_finished ( NULL, NULL)) { GNUNET_break_op (0); - awr.hr.http_status = 0; - awr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED; + awbr.hr.http_status = 0; + awbr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED; break; } } case MHD_HTTP_BAD_REQUEST: /* This should never happen, either us or the exchange is buggy (or API version conflict); just pass JSON reply to the application */ - awr.hr.ec = TALER_JSON_get_error_code (j_response); - awr.hr.hint = TALER_JSON_get_error_hint (j_response); + awbr.hr.ec = TALER_JSON_get_error_code (j_response); + awbr.hr.hint = TALER_JSON_get_error_hint (j_response); break; case MHD_HTTP_FORBIDDEN: GNUNET_break_op (0); /* Nothing really to verify, exchange says one of the signatures is invalid; as we checked them, this should never happen, we should pass the JSON reply to the application */ - awr.hr.ec = TALER_JSON_get_error_code (j_response); - awr.hr.hint = TALER_JSON_get_error_hint (j_response); + awbr.hr.ec = TALER_JSON_get_error_code (j_response); + awbr.hr.hint = TALER_JSON_get_error_hint (j_response); break; case MHD_HTTP_NOT_FOUND: /* Nothing really to verify, the exchange basically just says that it doesn't know this reserve. Can happen if we query before the wire transfer went through. We should simply pass the JSON reply to the application. */ - awr.hr.ec = TALER_JSON_get_error_code (j_response); - awr.hr.hint = TALER_JSON_get_error_hint (j_response); + awbr.hr.ec = TALER_JSON_get_error_code (j_response); + awbr.hr.hint = TALER_JSON_get_error_hint (j_response); break; case MHD_HTTP_CONFLICT: /* The exchange says that the reserve has insufficient funds; check the signatures in the history... */ if (GNUNET_OK != - reserve_age_withdraw_payment_required (awh->keys, - &awh->reserve_pub, - &awh->amount_with_fee, + reserve_age_withdraw_payment_required (awbh->keys, + &awbh->reserve_pub, + &awbh->amount_with_fee, j_response)) { GNUNET_break_op (0); - awr.hr.http_status = 0; - awr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED; + awbr.hr.http_status = 0; + awbr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED; } else { - awr.hr.ec = TALER_JSON_get_error_code (j_response); - awr.hr.hint = TALER_JSON_get_error_hint (j_response); + awbr.hr.ec = TALER_JSON_get_error_code (j_response); + awbr.hr.hint = TALER_JSON_get_error_hint (j_response); } break; case MHD_HTTP_GONE: @@ -484,41 +587,40 @@ handle_reserve_age_withdraw_finished ( /* Note: one might want to check /keys for revocation signature here, alas tricky in case our /keys is outdated => left to clients */ - awr.hr.ec = TALER_JSON_get_error_code (j_response); - awr.hr.hint = TALER_JSON_get_error_hint (j_response); + awbr.hr.ec = TALER_JSON_get_error_code (j_response); + awbr.hr.hint = TALER_JSON_get_error_hint (j_response); break; case MHD_HTTP_INTERNAL_SERVER_ERROR: /* Server had an internal issue; we should retry, but this API leaves this to the application */ - awr.hr.ec = TALER_JSON_get_error_code (j_response); - awr.hr.hint = TALER_JSON_get_error_hint (j_response); + awbr.hr.ec = TALER_JSON_get_error_code (j_response); + awbr.hr.hint = TALER_JSON_get_error_hint (j_response); break; default: /* unexpected response code */ GNUNET_break_op (0); - awr.hr.ec = TALER_JSON_get_error_code (j_response); - awr.hr.hint = TALER_JSON_get_error_hint (j_response); + awbr.hr.ec = TALER_JSON_get_error_code (j_response); + awbr.hr.hint = TALER_JSON_get_error_hint (j_response); GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unexpected response code %u/%d for exchange age-withdraw\n", (unsigned int) response_code, - (int) awr.hr.ec); + (int) awbr.hr.ec); break; } - awh->callback (awh->callback_cls, - &awr); - TALER_EXCHANGE_age_withdraw_cancel (awh); + awbh->callback (awbh->callback_cls, + &awbr); + TALER_EXCHANGE_age_withdraw_blinded_cancel (awbh); } /** - * Runs the actual age-withdraw operation. If there were CS-denominations - * involved, started once the all calls to /csr-withdraw are done. + * Runs the actual age-withdraw operation with the blinded planchets. * * @param[in,out] awh age withdraw handler */ static void perform_protocol ( - struct TALER_EXCHANGE_AgeWithdrawHandle *awh) + struct TALER_EXCHANGE_AgeWithdrawBlindedHandle *awbh) { #define FAIL_IF(cond) \ do { \ @@ -535,13 +637,30 @@ perform_protocol ( json_t *j_request_body = NULL; CURL *curlh = NULL; - - GNUNET_assert (0 == awh->csr_pending); + FAIL_IF (GNUNET_OK != + TALER_amount_set_zero (awbh->keys->currency, + &awbh->amount_with_fee)); + /* Accumulate total value with fees */ + for (size_t i = 0; i < awbh->num_input; i++) + { + struct TALER_Amount coin_total; + const struct TALER_EXCHANGE_DenomPublicKey *dpub = + awbh->blinded_input[i].denom_pub; + + FAIL_IF (0 > + TALER_amount_add (&coin_total, + &dpub->fees.withdraw, + &dpub->value)); + FAIL_IF (0 > + TALER_amount_add (&awbh->amount_with_fee, + &awbh->amount_with_fee, + &coin_total)); + } GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Attempting to age-withdraw from reserve %s with maximum age %d\n", - TALER_B2S (&awh->reserve_pub), - awh->max_age); + TALER_B2S (&awbh->reserve_pub), + awbh->max_age); coins_hctx = GNUNET_CRYPTO_hash_context_start (); FAIL_IF (NULL == coins_hctx); @@ -552,12 +671,12 @@ perform_protocol ( FAIL_IF ((NULL == j_denoms) || (NULL == j_array_candidates)); - for (size_t i = 0; i< awh->num_coins; i++) + for (size_t i = 0; i< awbh->num_input; i++) { /* Build the denomination array */ { - struct TALER_EXCHANGE_DenomPublicKey *denom = - &awh->coin_data[i].denom_pub; + const struct TALER_EXCHANGE_DenomPublicKey *denom = + awbh->blinded_input[i].denom_pub; json_t *jdenom = GNUNET_JSON_PACK ( TALER_JSON_pack_denom_pub (NULL, &denom->key)); @@ -568,67 +687,71 @@ perform_protocol ( /* Build the candidate array */ { - const struct CoinCandidate *can = awh->coin_data[i].coin_candidates; json_t *j_can = json_array (); FAIL_IF (NULL == j_can); for (size_t k = 0; k < TALER_CNC_KAPPA; k++) { + struct TALER_BlindedCoinHashP bch; + const struct TALER_PlanchetDetail *planchet = + &awbh->blinded_input[i].planchet_details[k]; json_t *jc = GNUNET_JSON_PACK ( TALER_JSON_pack_blinded_planchet ( NULL, - &can->planchet_detail.blinded_planchet)); + &planchet->blinded_planchet)); FAIL_IF (NULL == jc); FAIL_IF (0 < json_array_append_new (j_can, jc)); + TALER_coin_ev_hash (&planchet->blinded_planchet, + &planchet->denom_pub_hash, + &bch); + GNUNET_CRYPTO_hash_context_read (coins_hctx, - &can->blinded_coin_h, - sizeof(can->blinded_coin_h)); + &bch, + sizeof(bch)); } } } } - /* Sign the request */ - { - struct TALER_AgeWithdrawCommitmentHashP coins_commitment_h; - - GNUNET_CRYPTO_hash_context_finish (coins_hctx, - &coins_commitment_h.hash); + /* Build the hash of the commitment */ + GNUNET_CRYPTO_hash_context_finish (coins_hctx, + &awbh->h_commitment.hash); - TALER_wallet_age_withdraw_sign (&coins_commitment_h, - &awh->amount_with_fee, - &awh->age_mask, - awh->max_age, - awh->reserve_priv, - &awh->reserve_sig); - } + /* Sign the request */ + TALER_wallet_age_withdraw_sign (&awbh->h_commitment, + &awbh->amount_with_fee, + &awbh->age_mask, + awbh->max_age, + awbh->reserve_priv, + &awbh->reserve_sig); /* Initiate the POST-request */ j_request_body = GNUNET_JSON_PACK ( GNUNET_JSON_pack_array_steal ("denoms_h", j_denoms), GNUNET_JSON_pack_array_steal ("blinded_coin_evs", j_array_candidates), - GNUNET_JSON_pack_uint64 ("max_age", awh->max_age), - GNUNET_JSON_pack_data_auto ("reserve_sig", &awh->reserve_sig)); + GNUNET_JSON_pack_uint64 ("max_age", awbh->max_age), + GNUNET_JSON_pack_data_auto ("reserve_sig", &awbh->reserve_sig)); FAIL_IF (NULL == j_request_body); - curlh = TALER_EXCHANGE_curl_easy_get_ (awh->request_url); + curlh = TALER_EXCHANGE_curl_easy_get_ (awbh->request_url); FAIL_IF (NULL == curlh); FAIL_IF (GNUNET_OK != - TALER_curl_easy_post (&awh->post_ctx, + TALER_curl_easy_post (&awbh->post_ctx, curlh, j_request_body)); json_decref (j_request_body); j_request_body = NULL; - awh->job = GNUNET_CURL_job_add2 (awh->curl_ctx, - curlh, - awh->post_ctx.headers, - &handle_reserve_age_withdraw_finished, - awh); - FAIL_IF (NULL == awh->job); + awbh->job = GNUNET_CURL_job_add2 ( + awbh->curl_ctx, + curlh, + awbh->post_ctx.headers, + &handle_reserve_age_withdraw_blinded_finished, + awbh); + FAIL_IF (NULL == awbh->job); /* No errors, return */ return; @@ -642,13 +765,83 @@ ERROR: json_decref (j_request_body); if (NULL != curlh) curl_easy_cleanup (curlh); - TALER_EXCHANGE_age_withdraw_cancel (awh); + TALER_EXCHANGE_age_withdraw_blinded_cancel (awbh); return; #undef FAIL_IF } /** + * @brief Callback to copy the results from the call to TALER_age_withdraw_blinded + * to the result for the originating call from TALER_age_withdraw. + * + * @param cls struct TALER_AgeWithdrawHandle + * @param awbr The response + */ +static void +copy_results ( + void *cls, + const struct TALER_EXCHANGE_AgeWithdrawBlindedResponse *awbr) +{ + struct TALER_EXCHANGE_AgeWithdrawHandle *awh = cls; + uint8_t idx = awbr->details.ok.noreveal_index; + struct TALER_ExchangeWithdrawValues alg_values[awh->num_coins]; + struct TALER_EXCHANGE_AgeWithdrawResponse resp = { + .hr = awbr->hr, + .details = { + .ok = { .noreveal_index = awbr->details.ok.noreveal_index, + .h_commitment = awbr->details.ok.h_commitment, + .exchange_pub = awbr->details.ok.exchange_pub, + .exchange_sig = awbr->details.ok.exchange_sig, + .num_alg_values = awh->num_coins, + .alg_values = alg_values}, + }, + }; + + for (size_t n = 0; n< awh->num_coins; n++) + alg_values[n] = awh->coin_data[n].coin_candidates[idx].alg_values; + + awh->callback (awh->callback_cls, + &resp); + + awh->callback = NULL; +} + + +/** + * @brief Prepares and executes TALER_EXCHANGE_age_withdraw_blinded. + * If there were CS-denominations involved, started once the all calls + * to /csr-withdraw are done. + */ +static void +call_age_withdraw_blinded ( + struct TALER_EXCHANGE_AgeWithdrawHandle *awh) +{ + struct TALER_EXCHANGE_AgeWithdrawBlindedInput blinded_input[awh->num_coins]; + + /* Prepare the blinded planchets as input */ + for (size_t n = 0; n < awh->num_coins; n++) + { + blinded_input[n].denom_pub = &awh->coin_data[n].denom_pub; + for (uint8_t i = 0; i < TALER_CNC_KAPPA; i++) + blinded_input[n].planchet_details[i] = + awh->coin_data[n].planchet_details[i]; + } + + awh->protocol_handler = + TALER_EXCHANGE_age_withdraw_blinded ( + awh->curl_ctx, + awh->keys, + awh->exchange_url, + awh->reserve_priv, + awh->num_coins, + blinded_input, + copy_results, + awh); +} + + +/** * Prepares the request URL for the age-withdraw request * * @param awh The handler @@ -657,7 +850,7 @@ ERROR: static enum GNUNET_GenericReturnValue prepare_url ( - struct TALER_EXCHANGE_AgeWithdrawHandle *awh, + struct TALER_EXCHANGE_AgeWithdrawBlindedHandle *awbh, const char *exchange_url) { char arg_str[sizeof (struct TALER_ReservePublicKeyP) * 2 + 32]; @@ -665,8 +858,8 @@ prepare_url ( char *end; end = GNUNET_STRINGS_data_to_string ( - &awh->reserve_pub, - sizeof (awh->reserve_pub), + &awbh->reserve_pub, + sizeof (awbh->reserve_pub), pub_str, sizeof (pub_str)); *end = '\0'; @@ -675,13 +868,13 @@ prepare_url ( "reserves/%s/age-withdraw", pub_str); - awh->request_url = TALER_url_join (exchange_url, - arg_str, - NULL); - if (NULL == awh->request_url) + awbh->request_url = TALER_url_join (exchange_url, + arg_str, + NULL); + if (NULL == awbh->request_url) { GNUNET_break (0); - TALER_EXCHANGE_age_withdraw_cancel (awh); + TALER_EXCHANGE_age_withdraw_blinded_cancel (awbh); return GNUNET_SYSERR; } @@ -692,7 +885,7 @@ prepare_url ( /** * @brief Function called when CSR withdraw retrieval is finished * - * @param cls the `struct CoinCandidate *` + * @param cls the `struct CSRClosure *` * @param csrr replies from the /csr-withdraw request */ static void @@ -700,17 +893,27 @@ csr_withdraw_done ( void *cls, const struct TALER_EXCHANGE_CsRWithdrawResponse *csrr) { - struct CoinCandidate *can = cls; - struct TALER_EXCHANGE_AgeWithdrawHandle *awh = can->age_withdraw_handle; - struct TALER_EXCHANGE_AgeWithdrawResponse awr = { .hr = csrr->hr }; + struct CSRClosure *csr = cls; + struct CoinCandidate *can; + struct TALER_PlanchetDetail *planchet; + struct TALER_EXCHANGE_AgeWithdrawHandle *awh; - can->csr_withdraw_handle = NULL; + GNUNET_assert (NULL != csr); + awh = csr->age_withdraw_handle; + planchet = csr->planchet; + can = csr->candidate; + + GNUNET_assert (NULL != can); + GNUNET_assert (NULL != planchet); + GNUNET_assert (NULL != awh); + + csr->csr_withdraw_handle = NULL; switch (csrr->hr.http_status) { case MHD_HTTP_OK: { - bool success = true; + bool success = false; /* Complete the initialization of the coin with CS denomination */ can->alg_values = csrr->details.ok.alg_values; TALER_planchet_setup_coin_priv (&can->secret, @@ -721,44 +924,46 @@ csr_withdraw_done ( &can->blinding_key); /* This initializes the 2nd half of the can->planchet_detail.blinded_planchet! */ - if (GNUNET_OK != - TALER_planchet_prepare (&can->denom_pub->key, - &can->alg_values, - &can->blinding_key, - &can->coin_priv, - &can->h_age_commitment, - &can->h_coin_pub, - &can->planchet_detail)) - { - GNUNET_break (0); - success = false; - TALER_EXCHANGE_age_withdraw_cancel (awh); - } + do { + if (GNUNET_OK != + TALER_planchet_prepare (&csr->denom_pub->key, + &can->alg_values, + &can->blinding_key, + &can->coin_priv, + &can->h_age_commitment, + &can->h_coin_pub, + planchet)) + { + GNUNET_break (0); + TALER_EXCHANGE_age_withdraw_cancel (awh); + break; + } - if (GNUNET_OK != - TALER_coin_ev_hash (&can->planchet_detail.blinded_planchet, - &can->planchet_detail.denom_pub_hash, - &can->blinded_coin_h)) - { - GNUNET_break (0); - success = false; - TALER_EXCHANGE_age_withdraw_cancel (awh); - } + if (GNUNET_OK != + TALER_coin_ev_hash (&planchet->blinded_planchet, + &planchet->denom_pub_hash, + &can->blinded_coin_h)) + { + GNUNET_break (0); + TALER_EXCHANGE_age_withdraw_cancel (awh); + break; + } - awh->csr_pending--; + success = true; + } while(0); + + awh->csr.pending--; /* No more pending requests to /csr-withdraw, we can now perform the * actual age-withdraw operation */ - if (0 == awh->csr_pending && success) - perform_protocol (awh); + if (0 == awh->csr.pending && success) + call_age_withdraw_blinded (awh); return; } default: break; } - awh->callback (awh->callback_cls, - &awr); TALER_EXCHANGE_age_withdraw_cancel (awh); } @@ -795,10 +1000,6 @@ prepare_coins ( GNUNET_assert (0 < num_coins); awh->age_mask = coin_inputs[0].denom_pub->key.age_mask; - FAIL_IF (GNUNET_OK != - TALER_amount_set_zero (awh->keys->currency, - &awh->amount_with_fee)); - awh->coin_data = GNUNET_new_array (awh->num_coins, struct CoinData); @@ -814,26 +1015,12 @@ prepare_coins ( TALER_denom_pub_deep_copy (&cd->denom_pub.key, &input->denom_pub->key); - /* Accumulate total value with fees */ - { - struct TALER_Amount coin_total; - - FAIL_IF (0 > - TALER_amount_add (&coin_total, - &cd->denom_pub.fees.withdraw, - &cd->denom_pub.value)); - - FAIL_IF (0 > - TALER_amount_add (&awh->amount_with_fee, - &awh->amount_with_fee, - &coin_total)); - } - for (uint8_t k = 0; k < TALER_CNC_KAPPA; k++) { struct CoinCandidate *can = &cd->coin_candidates[k]; + struct TALER_PlanchetDetail *planchet = &cd->planchet_details[k]; - can->secret = input->secret[k]; + can->secret = input->secrets[k]; /* Derive the age restriction from the given secret and * the maximum age */ @@ -865,47 +1052,43 @@ prepare_coins ( &can->coin_priv, &can->h_age_commitment, &can->h_coin_pub, - &can->planchet_detail)); + planchet)); FAIL_IF (GNUNET_OK != - TALER_coin_ev_hash (&can->planchet_detail.blinded_planchet, - &can->planchet_detail.denom_pub_hash, + TALER_coin_ev_hash (&planchet->blinded_planchet, + &planchet->denom_pub_hash, &can->blinded_coin_h)); break; } case TALER_DENOMINATION_CS: { + struct CSRClosure *cls = &cd->csr_cls[k]; /** * Save the handler and the denomination for the callback * after the call to csr-withdraw */ - can->age_withdraw_handle = awh; - can->denom_pub = &cd->denom_pub; + cls->age_withdraw_handle = awh; + cls->candidate = can; + cls->planchet = planchet; + cls->denom_pub = &cd->denom_pub; TALER_cs_withdraw_nonce_derive ( &can->secret, - &can->planchet_detail - .blinded_planchet - .details - .cs_blinded_planchet - .nonce); + &planchet->blinded_planchet.details.cs_blinded_planchet.nonce); /* Note that we only initialize the first half of the blinded_planchet here; the other part will be done after the /csr-withdraw request! */ - can->planchet_detail.blinded_planchet.cipher = TALER_DENOMINATION_CS; - can->csr_withdraw_handle = - TALER_EXCHANGE_csr_withdraw (awh->curl_ctx, - awh->exchange_url, - &cd->denom_pub, - &can->planchet_detail - .blinded_planchet - .details - .cs_blinded_planchet - .nonce, - &csr_withdraw_done, - &can); - FAIL_IF (NULL == can->csr_withdraw_handle); - - awh->csr_pending++; + planchet->blinded_planchet.cipher = TALER_DENOMINATION_CS; + cls->csr_withdraw_handle = + TALER_EXCHANGE_csr_withdraw ( + awh->curl_ctx, + awh->exchange_url, + &cd->denom_pub, + &planchet->blinded_planchet.details.cs_blinded_planchet.nonce, + &csr_withdraw_done, + &cls); + FAIL_IF (NULL == cls->csr_withdraw_handle); + + awh->csr.pending++; break; } default: @@ -924,8 +1107,8 @@ ERROR: struct TALER_EXCHANGE_AgeWithdrawHandle * TALER_EXCHANGE_age_withdraw ( struct GNUNET_CURL_Context *curl_ctx, - const char *exchange_url, struct TALER_EXCHANGE_Keys *keys, + const char *exchange_url, const struct TALER_ReservePrivateKeyP *reserve_priv, size_t num_coins, const struct TALER_EXCHANGE_AgeWithdrawCoinInput coin_inputs[const static @@ -938,7 +1121,7 @@ TALER_EXCHANGE_age_withdraw ( awh = GNUNET_new (struct TALER_EXCHANGE_AgeWithdrawHandle); awh->exchange_url = exchange_url; - awh->keys = TALER_EXCHANGE_keys_incref (keys); + awh->keys = keys; awh->curl_ctx = curl_ctx; awh->reserve_priv = reserve_priv; awh->callback = res_cb; @@ -946,13 +1129,6 @@ TALER_EXCHANGE_age_withdraw ( awh->num_coins = num_coins; awh->max_age = max_age; - GNUNET_CRYPTO_eddsa_key_get_public (&awh->reserve_priv->eddsa_priv, - &awh->reserve_pub.eddsa_pub); - - - if (GNUNET_OK != prepare_url (awh, - exchange_url)) - return NULL; if (GNUNET_OK != prepare_coins (awh, num_coins, @@ -964,8 +1140,8 @@ TALER_EXCHANGE_age_withdraw ( * in flight and once they finish, the age-withdraw-protocol will be * called from within the csr_withdraw_done-function. */ - if (0 == awh->csr_pending) - perform_protocol (awh); + if (0 == awh->csr.pending) + call_age_withdraw_blinded (awh); return awh; } @@ -982,30 +1158,71 @@ TALER_EXCHANGE_age_withdraw_cancel ( for (uint8_t k = 0; k < TALER_CNC_KAPPA; k++) { - struct CoinCandidate *can = &cd->coin_candidates[k]; + struct TALER_PlanchetDetail *planchet = &cd->planchet_details[k]; + struct CSRClosure *cls = &cd->csr_cls[k]; - if (NULL != can->csr_withdraw_handle) + if (NULL != cls->csr_withdraw_handle) { - TALER_EXCHANGE_csr_withdraw_cancel (can->csr_withdraw_handle); - can->csr_withdraw_handle = NULL; + TALER_EXCHANGE_csr_withdraw_cancel (cls->csr_withdraw_handle); + cls->csr_withdraw_handle = NULL; } - TALER_blinded_planchet_free (&can->planchet_detail.blinded_planchet); + TALER_blinded_planchet_free (&planchet->blinded_planchet); } TALER_denom_pub_free (&cd->denom_pub.key); } GNUNET_free (awh->coin_data); - /* Cleanup CURL job data */ - if (NULL != awh->job) - { - GNUNET_CURL_job_cancel (awh->job); - awh->job = NULL; - } - TALER_curl_easy_post_finished (&awh->post_ctx); - TALER_EXCHANGE_keys_decref (awh->keys); - GNUNET_free (awh->request_url); + TALER_EXCHANGE_age_withdraw_blinded_cancel (awh->protocol_handler); + awh->protocol_handler = NULL; GNUNET_free (awh); +} + + +struct TALER_EXCHANGE_AgeWithdrawBlindedHandle * +TALER_EXCHANGE_age_withdraw_blinded ( + struct GNUNET_CURL_Context *curl_ctx, + struct TALER_EXCHANGE_Keys *keys, + const char *exchange_url, + const struct TALER_ReservePrivateKeyP *reserve_priv, + unsigned int num_input, + const struct TALER_EXCHANGE_AgeWithdrawBlindedInput blinded_input[static + num_input], + TALER_EXCHANGE_AgeWithdrawBlindedCallback res_cb, + void *res_cb_cls) +{ + struct TALER_EXCHANGE_AgeWithdrawBlindedHandle *awbh = + GNUNET_new (struct TALER_EXCHANGE_AgeWithdrawBlindedHandle); + + awbh->num_input = num_input; + awbh->blinded_input = blinded_input; + awbh->keys = TALER_EXCHANGE_keys_incref (keys); + awbh->curl_ctx = curl_ctx; + awbh->reserve_priv = reserve_priv; + awbh->callback = res_cb; + awbh->callback_cls = res_cb_cls; + + GNUNET_CRYPTO_eddsa_key_get_public (&awbh->reserve_priv->eddsa_priv, + &awbh->reserve_pub.eddsa_pub); + + if (GNUNET_OK != prepare_url (awbh, + exchange_url)) + return NULL; + + perform_protocol (awbh); + return awbh; +} + + +void +TALER_EXCHANGE_age_withdraw_blinded_cancel ( + struct TALER_EXCHANGE_AgeWithdrawBlindedHandle *awbh) +{ + if (NULL == awbh) + return; + + // TODO[oec]: curl stuff Cleanup + TALER_EXCHANGE_keys_decref (awbh->keys); } diff --git a/src/lib/exchange_api_age_withdraw_reveal.c b/src/lib/exchange_api_age_withdraw_reveal.c new file mode 100644 index 000000000..9803be28c --- /dev/null +++ b/src/lib/exchange_api_age_withdraw_reveal.c @@ -0,0 +1,561 @@ +/* + This file is part of TALER + Copyright (C) 2023 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 lib/exchange_api_age_withdraw_reveal.c + * @brief Implementation of /age-withdraw/$ACH/reveal requests + * @author Özgür Kesim + */ + +#include "platform.h" +#include <gnunet/gnunet_common.h> +#include <jansson.h> +#include <microhttpd.h> /* just for HTTP status codes */ +#include <gnunet/gnunet_util_lib.h> +#include <gnunet/gnunet_json_lib.h> +#include <gnunet/gnunet_curl_lib.h> +#include "taler_curl_lib.h" +#include "taler_json_lib.h" +#include "taler_exchange_service.h" +#include "exchange_api_common.h" +#include "exchange_api_handle.h" +#include "taler_signatures.h" +#include "exchange_api_curl_defaults.h" + +/** + * Handler for a running age-withdraw-reveal request + */ +struct TALER_EXCHANGE_AgeWithdrawRevealHandle +{ + + /* The index not to be disclosed */ + uint8_t noreveal_index; + + /* The age-withdraw commitment */ + struct TALER_AgeWithdrawCommitmentHashP h_commitment; + + /* The maximum age */ + uint8_t max_age; + + /* Number of coins */ + size_t num_coins; + + /* The @e num_coins * kappa coin secrets from the age-withdraw commitment */ + const struct TALER_EXCHANGE_AgeWithdrawCoinInput *coins_input; + + /* The @e num_coins algorithm- and coin-specific parameters from the + * previous call to /age-withdraw. */ + const struct TALER_ExchangeWithdrawValues *alg_values; + + /* The url for the reveal request */ + const char *request_url; + + /** + * CURL handle for the request job. + */ + struct GNUNET_CURL_Job *job; + + /** + * Post Context + */ + struct TALER_CURL_PostContext post_ctx; + + /* Callback */ + TALER_EXCHANGE_AgeWithdrawRevealCallback callback; + + /* Reveal */ + void *callback_cls; +}; + + +static enum GNUNET_GenericReturnValue +reveal_coin ( + const struct TALER_PlanchetMasterSecretP *secret, + const struct TALER_DenominationPublicKey *denom_pub, + const struct TALER_ExchangeWithdrawValues *alg_values, + const struct TALER_BlindedDenominationSignature *blind_sig, + uint8_t max_age, + struct TALER_EXCHANGE_RevealedCoinInfo *revealed_coin) +{ + enum GNUNET_GenericReturnValue ret = GNUNET_SYSERR; + +#define BREAK_ON_FAILURE(call) \ + do { \ + if (GNUNET_OK != (call)) { GNUNET_break (0); break; } \ + } while(0) + + do { + struct TALER_CoinPubHashP h_coin_pub; + struct TALER_PlanchetDetail planchet_detail; + const struct TALER_AgeCommitmentHash *hac = NULL; + struct TALER_FreshCoin fresh_coin; + + TALER_planchet_setup_coin_priv (secret, + alg_values, + &revealed_coin->coin_priv); + + TALER_planchet_blinding_secret_create (secret, + alg_values, + &revealed_coin->bks); + + revealed_coin->age_commitment_proof = NULL; + if (0 < max_age) + { + BREAK_ON_FAILURE ( + TALER_age_restriction_from_secret ( + secret, + &denom_pub->age_mask, + max_age, + revealed_coin->age_commitment_proof)); + + TALER_age_commitment_hash ( + &revealed_coin->age_commitment_proof->commitment, + &revealed_coin->h_age_commitment); + + hac = &revealed_coin->h_age_commitment; + } + + BREAK_ON_FAILURE ( + TALER_planchet_prepare (denom_pub, + alg_values, + &revealed_coin->bks, + &revealed_coin->coin_priv, + hac, + &h_coin_pub, + &planchet_detail)); + + BREAK_ON_FAILURE ( + TALER_planchet_to_coin (denom_pub, + blind_sig, + &revealed_coin->bks, + &revealed_coin->coin_priv, + &revealed_coin->h_age_commitment, + &h_coin_pub, + alg_values, + &fresh_coin)); + + /* success */ + revealed_coin->sig = fresh_coin.sig; + ret = GNUNET_OK; + } while(0); + + return ret; +#undef BREAK_ON_FAILURE +} + + +/** + * We got a 200 OK response for the /age-withdraw/$ACH/reveal operation. + * Extract the signed blindedcoins and return it to the caller. + * + * @param awrh operation handle + * @param j_response reply from the exchange + * @param num_coins The (expected) number of revealed coins + * @param[in,out] revealed_coins The @e num_coins revealed coins to populate + * @return #GNUNET_OK on success, #GNUNET_SYSERR on errors + */ +static enum GNUNET_GenericReturnValue +age_withdraw_reveal_ok ( + struct TALER_EXCHANGE_AgeWithdrawRevealHandle *awrh, + const json_t *j_response) +{ + struct TALER_EXCHANGE_AgeWithdrawRevealResponse response = { + .hr.reply = j_response, + .hr.http_status = MHD_HTTP_OK, + }; + const json_t *j_sigs; + struct GNUNET_JSON_Specification spec[] = { + GNUNET_JSON_spec_array_const ("ev_sigs", + &j_sigs), + GNUNET_JSON_spec_end () + }; + + if (GNUNET_OK != GNUNET_JSON_parse (j_response, + spec, + NULL, NULL)) + { + GNUNET_break_op (0); + return GNUNET_SYSERR; + } + + if (awrh->num_coins != json_array_size (j_sigs)) + { + /* Number of coins generated does not match our expectation */ + GNUNET_break_op (0); + return GNUNET_SYSERR; + } + + { + struct TALER_EXCHANGE_RevealedCoinInfo revealed_coins[awrh->num_coins]; + + /* Reconstruct the coins and unblind the signatures */ + for (size_t n = 0; n < awrh->num_coins; n++) + { + enum GNUNET_GenericReturnValue ret; + struct TALER_BlindedDenominationSignature blinded_sig; + json_t *j_sig = json_array_get (j_sigs, n); + struct GNUNET_JSON_Specification spec[] = { + GNUNET_JSON_spec_fixed_auto ("", &blinded_sig), + GNUNET_JSON_spec_end () + }; + + GNUNET_assert (NULL != j_sig); + if (GNUNET_OK != GNUNET_JSON_parse (j_sig, + spec, + NULL, NULL)) + { + GNUNET_break_op (0); + return GNUNET_SYSERR; + } + ret = reveal_coin (&awrh->coins_input[n].secrets[awrh->noreveal_index], + &awrh->coins_input[n].denom_pub->key, + &awrh->alg_values[n], + &blinded_sig, + awrh->max_age, + &revealed_coins[n]); + + if (GNUNET_OK != ret) + return ret; + } + + response.details.ok.num_coins = awrh->num_coins; + response.details.ok.revealed_coins = revealed_coins; + awrh->callback (awrh->callback_cls, + &response); + /* Make sure the callback isn't called again */ + awrh->callback = NULL; + } + + return GNUNET_OK; +} + + +/** + * Function called when we're done processing the + * HTTP /age-withdraw/$ACH/reveal request. + * + * @param cls the `struct TALER_EXCHANGE_AgeWithdrawRevealHandle` + * @param response_code The HTTP response code + * @param response response data + */ +static void +handle_age_withdraw_reveal_finished ( + void *cls, + long response_code, + const void *response) +{ + struct TALER_EXCHANGE_AgeWithdrawRevealHandle *awrh = cls; + const json_t *j_response = response; + struct TALER_EXCHANGE_AgeWithdrawRevealResponse awr = { + .hr.reply = j_response, + .hr.http_status = (unsigned int) response_code + }; + + awrh->job = NULL; + /* FIXME[oec]: Only handle response-codes that are in the spec */ + switch (response_code) + { + case 0: + awr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; + break; + case MHD_HTTP_OK: + { + enum GNUNET_GenericReturnValue ret; + + ret = age_withdraw_reveal_ok (awrh, + j_response); + if (GNUNET_OK != ret) + { + GNUNET_break_op (0); + awr.hr.http_status = 0; + awr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED; + break; + } + GNUNET_assert (NULL == awrh->callback); + TALER_EXCHANGE_age_withdraw_reveal_cancel (awrh); + return; + } + case MHD_HTTP_UNAVAILABLE_FOR_LEGAL_REASONS: + /* only validate reply is well-formed */ + { + uint64_t ptu; + struct GNUNET_JSON_Specification spec[] = { + GNUNET_JSON_spec_uint64 ("legitimization_uuid", + &ptu), + GNUNET_JSON_spec_end () + }; + + if (GNUNET_OK != + GNUNET_JSON_parse (j_response, + spec, + NULL, NULL)) + { + GNUNET_break_op (0); + awr.hr.http_status = 0; + awr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED; + break; + } + } + case MHD_HTTP_BAD_REQUEST: + /* This should never happen, either us or the exchange is buggy + (or API version conflict); just pass JSON reply to the application */ + awr.hr.ec = TALER_JSON_get_error_code (j_response); + awr.hr.hint = TALER_JSON_get_error_hint (j_response); + break; + case MHD_HTTP_FORBIDDEN: + GNUNET_break_op (0); + /** + * This should never happen, as we don't sent any signatures + * to the exchange to verify. We should simply pass the JSON reply + * to the application + **/ + awr.hr.ec = TALER_JSON_get_error_code (j_response); + awr.hr.hint = TALER_JSON_get_error_hint (j_response); + break; + case MHD_HTTP_NOT_FOUND: + /* Nothing really to verify, the exchange basically just says + that it doesn't know this age-withraw commitment. */ + awr.hr.ec = TALER_JSON_get_error_code (j_response); + awr.hr.hint = TALER_JSON_get_error_hint (j_response); + break; + case MHD_HTTP_CONFLICT: + /* An age commitment for one of the coins did not fulfill + * the required maximum age requirement of the corresponding + * reserve. + * Error code: TALER_EC_EXCHANGE_GENERIC_COIN_AGE_REQUIREMENT_FAILURE. + */ + awr.hr.ec = TALER_JSON_get_error_code (j_response); + awr.hr.hint = TALER_JSON_get_error_hint (j_response); + break; + case MHD_HTTP_GONE: + /* could happen if denomination was revoked */ + /* Note: one might want to check /keys for revocation + signature here, alas tricky in case our /keys + is outdated => left to clients */ + awr.hr.ec = TALER_JSON_get_error_code (j_response); + awr.hr.hint = TALER_JSON_get_error_hint (j_response); + break; + case MHD_HTTP_INTERNAL_SERVER_ERROR: + /* Server had an internal issue; we should retry, but this API + leaves this to the application */ + awr.hr.ec = TALER_JSON_get_error_code (j_response); + awr.hr.hint = TALER_JSON_get_error_hint (j_response); + break; + default: + /* unexpected response code */ + GNUNET_break_op (0); + awr.hr.ec = TALER_JSON_get_error_code (j_response); + awr.hr.hint = TALER_JSON_get_error_hint (j_response); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Unexpected response code %u/%d for exchange age-withdraw\n", + (unsigned int) response_code, + (int) awr.hr.ec); + break; + } + awrh->callback (awrh->callback_cls, + &awr); + TALER_EXCHANGE_age_withdraw_reveal_cancel (awrh); +} + + +/** + * Prepares the request URL for the age-withdraw-reveal request + * + * @param exchange_url The base-URL to the exchange + * @param[in,out] awrh The handler + * @return GNUNET_OK on success, GNUNET_SYSERR otherwise + */ +static +enum GNUNET_GenericReturnValue +prepare_url ( + const char *exchange_url, + struct TALER_EXCHANGE_AgeWithdrawRevealHandle *awrh) +{ + char arg_str[sizeof (struct TALER_AgeWithdrawCommitmentHashP) * 2 + 32]; + char pub_str[sizeof (struct TALER_AgeWithdrawCommitmentHashP) * 2]; + char *end; + + end = GNUNET_STRINGS_data_to_string (&awrh->h_commitment, + sizeof (awrh->h_commitment), + pub_str, + sizeof (pub_str)); + *end = '\0'; + GNUNET_snprintf (arg_str, + sizeof (arg_str), + "age-withraw/%s/reveal", + pub_str); + + awrh->request_url = TALER_url_join (exchange_url, + arg_str, + NULL); + if (NULL == awrh->request_url) + { + GNUNET_break (0); + TALER_EXCHANGE_age_withdraw_reveal_cancel (awrh); + return GNUNET_SYSERR; + } + + return GNUNET_OK; +} + + +/** + * Call /age-withdraw/$ACH/reveal + * + * @param curl_ctx The context for CURL + * @param awrh The handler + * @param reveal_inputs The secrets of the coin candidates + */ +static +void +perform_protocol ( + struct GNUNET_CURL_Context *curl_ctx, + struct TALER_EXCHANGE_AgeWithdrawRevealHandle *awrh) +{ + CURL *curlh = NULL; + json_t *j_request_body = NULL; + json_t *j_array_of_secrets = NULL; + json_t *j_secrets = NULL; + json_t *j_sec = NULL; + +#define FAIL_IF(cond) \ + do { \ + if ((cond)) \ + { \ + GNUNET_break (! (cond)); \ + goto ERROR; \ + } \ + } while(0) + + for (size_t n = 0; n < awrh->num_coins; n++) + { + const struct TALER_PlanchetMasterSecretP *secrets = + awrh->coins_input[n].secrets; + + j_secrets = json_array (); + FAIL_IF (NULL == j_secrets); + + for (uint8_t k = 0; k < TALER_CNC_KAPPA; k++) + { + const struct TALER_PlanchetMasterSecretP *secret = &secrets[k]; + if (awrh->noreveal_index == k) + continue; + + j_sec = GNUNET_JSON_PACK ( + GNUNET_JSON_pack_data_auto (NULL, secret)); + + FAIL_IF (NULL == j_sec); + FAIL_IF (0 < json_array_append_new (j_secrets, + j_sec)); + } + + FAIL_IF (0 < json_array_append_new (j_array_of_secrets, + j_secrets)); + } + j_request_body = GNUNET_JSON_PACK ( + GNUNET_JSON_pack_array_steal ("disclosed_coin_secrets", + j_array_of_secrets)); + FAIL_IF (NULL == j_request_body); + + curlh = TALER_EXCHANGE_curl_easy_get_ (awrh->request_url); + FAIL_IF (NULL == curlh); + FAIL_IF (GNUNET_OK != + TALER_curl_easy_post (&awrh->post_ctx, + curlh, + j_request_body)); + json_decref (j_request_body); + j_request_body = NULL; + + awrh->job = GNUNET_CURL_job_add2 (curl_ctx, + curlh, + awrh->post_ctx.headers, + &handle_age_withdraw_reveal_finished, + awrh); + FAIL_IF (NULL == awrh->job); + + /* No error, return */ + return; + +ERROR: + if (NULL != j_sec) + json_decref (j_sec); + if (NULL != j_secrets) + json_decref (j_secrets); + if (NULL != j_array_of_secrets) + json_decref (j_array_of_secrets); + if (NULL != j_request_body) + json_decref (j_request_body); + if (NULL != curlh) + curl_easy_cleanup (curlh); + TALER_EXCHANGE_age_withdraw_reveal_cancel (awrh); + return; +#undef FAIL_IF +} + + +struct TALER_EXCHANGE_AgeWithdrawRevealHandle * +TALER_EXCHANGE_age_withdraw_reveal ( + struct GNUNET_CURL_Context *curl_ctx, + const char *exchange_url, + size_t num_coins, + const struct TALER_EXCHANGE_AgeWithdrawCoinInput coins_input[static + num_coins], + const struct TALER_ExchangeWithdrawValues alg_values[static num_coins], + uint8_t noreveal_index, + const struct TALER_AgeWithdrawCommitmentHashP *h_commitment, + uint8_t max_age, + TALER_EXCHANGE_AgeWithdrawRevealCallback reveal_cb, + void *reveal_cb_cls) +{ + struct TALER_EXCHANGE_AgeWithdrawRevealHandle *awrh = + GNUNET_new (struct TALER_EXCHANGE_AgeWithdrawRevealHandle); + awrh->noreveal_index = noreveal_index; + awrh->callback = reveal_cb; + awrh->callback_cls = reveal_cb_cls; + awrh->h_commitment = *h_commitment; + awrh->num_coins = num_coins; + awrh->coins_input = coins_input; + awrh->alg_values = alg_values; + awrh->max_age = max_age; + + + if (GNUNET_OK != + prepare_url (exchange_url, + awrh)) + return NULL; + + perform_protocol (curl_ctx, awrh); + + return awrh; +} + + +void +TALER_EXCHANGE_age_withdraw_reveal_cancel ( + struct TALER_EXCHANGE_AgeWithdrawRevealHandle *awrh) +{ + if (NULL != awrh->job) + { + GNUNET_CURL_job_cancel (awrh->job); + awrh->job = NULL; + } + TALER_curl_easy_post_finished (&awrh->post_ctx); + /* FIXME[oec]: anything else left to cleanup!? */ + GNUNET_free (awrh); +} + + +/* exchange_api_age_withdraw_reveal.c */ diff --git a/src/testing/testing_api_cmd_age_withdraw.c b/src/testing/testing_api_cmd_age_withdraw.c new file mode 100644 index 000000000..56e65a990 --- /dev/null +++ b/src/testing/testing_api_cmd_age_withdraw.c @@ -0,0 +1,155 @@ +/* + This file is part of TALER + Copyright (C) 2023 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_age_withdraw.c + * @brief implements the age-withdraw command + * @author Özgür Kesim + */ + +#include "platform.h" +#include "taler_json_lib.h" +#include <microhttpd.h> +#include <gnunet/gnunet_curl_lib.h> +#include "taler_signatures.h" +#include "taler_extensions.h" +#include "taler_testing_lib.h" + +/** + * State for a "age withdraw" CMD: + */ + +struct AgeWithdrawState +{ + /* + * Which reserve should we withdraw from? + */ + const char *reserve_reference; + + /** + * Expected HTTP response code to the request. + */ + unsigned int expected_response_code; + + /** + * The maximum age we commit to + */ + uint8_t max_age; + + /** + * Number of coins to withdraw + */ + size_t num_coins; +}; + + +struct TALER_TESTING_Command +TALER_TESTING_cmd_age_withdraw (const char *label, + const char *reserve_reference, + uint8_t max_age, + unsigned int expected_response_code, + const char *amount, + ...) +{ + struct AgeWithdrawState *aws; + unsigned int cnt; + va_list ap; + + aws = GNUNET_new (struct AgeWithdrawState); + aws->max_age = max_age; + aws->reserve_reference = reserve_reference; + aws->expected_response_code = expected_response_code; + + cnt = 1; + va_start (ap, amount); + while (NULL != (va_arg (ap, const char *))) + cnt++; + aws->num_coins = cnt; + aws->coins = GNUNET_new_array (cnt, + struct CoinState); + va_end (ap); + va_start (ap, amount); + for (unsigned int i = 0; i<ws->num_coins; i++) + { + struct CoinState *cs = &ws->coins[i]; + + if (0 < age) + { + struct TALER_AgeCommitmentProof *acp; + struct TALER_AgeCommitmentHash *hac; + struct GNUNET_HashCode seed; + struct TALER_AgeMask mask; + + acp = GNUNET_new (struct TALER_AgeCommitmentProof); + hac = GNUNET_new (struct TALER_AgeCommitmentHash); + mask = TALER_extensions_get_age_restriction_mask (); + GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, + &seed, + sizeof(seed)); + + if (GNUNET_OK != + TALER_age_restriction_commit ( + &mask, + age, + &seed, + acp)) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Failed to generate age commitment for age %d at %s\n", + age, + label); + GNUNET_assert (0); + } + + TALER_age_commitment_hash (&acp->commitment, + hac); + cs->age_commitment_proof = acp; + cs->h_age_commitment = hac; + } + + if (GNUNET_OK != + TALER_string_to_amount (amount, + &cs->amount)) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Failed to parse amount `%s' at %s\n", + amount, + label); + GNUNET_assert (0); + } + /* move on to next vararg! */ + amount = va_arg (ap, const char *); + } + GNUNET_assert (NULL == amount); + va_end (ap); + + { + struct TALER_TESTING_Command cmd = { + .cls = ws, + .label = label, + .run = &age_withdraw_run, + .cleanup = &age_withdraw_cleanup, + .traits = &age_withdraw_traits + }; + + return cmd; + } +} + + +/* end of testing_api_cmd_age_withdraw.c */ |