diff options
Diffstat (limited to 'src/lib/exchange_api_purse_create_with_deposit.c')
-rw-r--r-- | src/lib/exchange_api_purse_create_with_deposit.c | 364 |
1 files changed, 263 insertions, 101 deletions
diff --git a/src/lib/exchange_api_purse_create_with_deposit.c b/src/lib/exchange_api_purse_create_with_deposit.c index 42dd6914b..fff898e57 100644 --- a/src/lib/exchange_api_purse_create_with_deposit.c +++ b/src/lib/exchange_api_purse_create_with_deposit.c @@ -1,6 +1,6 @@ /* This file is part of TALER - Copyright (C) 2022 Taler Systems SA + Copyright (C) 2022-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 @@ -29,20 +29,53 @@ #include "taler_json_lib.h" #include "taler_exchange_service.h" #include "exchange_api_handle.h" +#include "exchange_api_common.h" #include "taler_signatures.h" #include "exchange_api_curl_defaults.h" /** + * Information we track per deposited coin. + */ +struct Deposit +{ + /** + * Coin's public key. + */ + struct TALER_CoinSpendPublicKeyP coin_pub; + + /** + * Signature made with the coin. + */ + struct TALER_CoinSpendSignatureP coin_sig; + + /** + * Coin's denomination. + */ + struct TALER_DenominationHashP h_denom_pub; + + /** + * Age restriction hash for the coin. + */ + struct TALER_AgeCommitmentHash ahac; + + /** + * How much did we say the coin contributed. + */ + struct TALER_Amount contribution; +}; + + +/** * @brief A purse create with deposit handle */ struct TALER_EXCHANGE_PurseCreateDepositHandle { /** - * The connection to exchange this request handle will use + * The keys of the exchange this request handle will use */ - struct TALER_EXCHANGE_Handle *exchange; + struct TALER_EXCHANGE_Keys *keys; /** * The url for this request. @@ -50,6 +83,11 @@ struct TALER_EXCHANGE_PurseCreateDepositHandle char *url; /** + * The base URL of the exchange. + */ + char *exchange_url; + + /** * Context for #TEH_curl_easy_post(). Keeps the data that must * persist for Curl to make the upload. */ @@ -76,6 +114,11 @@ struct TALER_EXCHANGE_PurseCreateDepositHandle struct TALER_Amount purse_value_after_fees; /** + * Our encrypted contract (if we had any). + */ + struct TALER_EncryptedContract econtract; + + /** * Public key of the merge capability. */ struct TALER_PurseMergePublicKeyP merge_pub; @@ -86,7 +129,12 @@ struct TALER_EXCHANGE_PurseCreateDepositHandle struct TALER_PurseContractPublicKeyP purse_pub; /** - * Hash over the purse's contrac terms. + * Signature with the purse key on the request. + */ + struct TALER_PurseContractSignatureP purse_sig; + + /** + * Hash over the purse's contract terms. */ struct TALER_PrivateContractHashP h_contract_terms; @@ -94,6 +142,17 @@ struct TALER_EXCHANGE_PurseCreateDepositHandle * When does the purse expire. */ struct GNUNET_TIME_Timestamp purse_expiration; + + /** + * Array of @e num_deposit deposits. + */ + struct Deposit *deposits; + + /** + * How many deposits did we make? + */ + unsigned int num_deposits; + }; @@ -116,6 +175,7 @@ handle_purse_create_deposit_finished (void *cls, .hr.reply = j, .hr.http_status = (unsigned int) response_code }; + const struct TALER_EXCHANGE_Keys *keys = pch->keys; pch->job = NULL; switch (response_code) @@ -125,7 +185,6 @@ handle_purse_create_deposit_finished (void *cls, break; case MHD_HTTP_OK: { - const struct TALER_EXCHANGE_Keys *key_state; struct GNUNET_TIME_Timestamp etime; struct TALER_Amount total_deposited; struct TALER_ExchangeSignatureP exchange_sig; @@ -153,9 +212,8 @@ handle_purse_create_deposit_finished (void *cls, dr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED; break; } - key_state = TALER_EXCHANGE_get_keys (pch->exchange); if (GNUNET_OK != - TALER_EXCHANGE_test_signing_key (key_state, + TALER_EXCHANGE_test_signing_key (keys, &exchange_pub)) { GNUNET_break_op (0); @@ -201,7 +259,116 @@ handle_purse_create_deposit_finished (void *cls, happen, we should pass the JSON reply to the application */ break; case MHD_HTTP_CONFLICT: - // FIXME: check reply! + { + dr.hr.ec = TALER_JSON_get_error_code (j); + switch (dr.hr.ec) + { + case TALER_EC_EXCHANGE_PURSE_CREATE_CONFLICTING_META_DATA: + if (GNUNET_OK != + TALER_EXCHANGE_check_purse_create_conflict_ ( + &pch->purse_sig, + &pch->purse_pub, + j)) + { + GNUNET_break_op (0); + dr.hr.http_status = 0; + dr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED; + break; + } + break; + case TALER_EC_EXCHANGE_GENERIC_INSUFFICIENT_FUNDS: + /* Nothing to check anymore here, proof needs to be + checked in the GET /coins/$COIN_PUB handler */ + break; + case TALER_EC_EXCHANGE_GENERIC_COIN_CONFLICTING_DENOMINATION_KEY: + // FIXME #7267: write check (add to exchange_api_common! */ + break; + case TALER_EC_EXCHANGE_PURSE_DEPOSIT_CONFLICTING_META_DATA: + { + struct TALER_CoinSpendPublicKeyP coin_pub; + struct TALER_CoinSpendSignatureP coin_sig; + struct TALER_DenominationHashP h_denom_pub; + struct TALER_AgeCommitmentHash phac; + bool found = false; + + if (GNUNET_OK != + TALER_EXCHANGE_check_purse_coin_conflict_ ( + &pch->purse_pub, + pch->exchange_url, + j, + &h_denom_pub, + &phac, + &coin_pub, + &coin_sig)) + { + GNUNET_break_op (0); + dr.hr.http_status = 0; + dr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED; + break; + } + for (unsigned int i = 0; i<pch->num_deposits; i++) + { + struct Deposit *deposit = &pch->deposits[i]; + + if (0 != + GNUNET_memcmp (&coin_pub, + &deposit->coin_pub)) + continue; + if (0 != + GNUNET_memcmp (&deposit->h_denom_pub, + &h_denom_pub)) + { + found = true; + break; + } + if (0 != + GNUNET_memcmp (&deposit->ahac, + &phac)) + { + found = true; + break; + } + if (0 == + GNUNET_memcmp (&coin_sig, + &deposit->coin_sig)) + { + GNUNET_break_op (0); + continue; + } + found = true; + break; + } + if (! found) + { + /* conflict is for a different coin! */ + GNUNET_break_op (0); + dr.hr.http_status = 0; + dr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED; + break; + } + } + case TALER_EC_EXCHANGE_PURSE_ECONTRACT_CONFLICTING_META_DATA: + if (GNUNET_OK != + TALER_EXCHANGE_check_purse_econtract_conflict_ ( + &pch->econtract.econtract_sig, + &pch->purse_pub, + j)) + { + GNUNET_break_op (0); + dr.hr.http_status = 0; + dr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED; + break; + } + break; + default: + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Unexpected error code %d for conflcting deposit\n", + dr.hr.ec); + GNUNET_break_op (0); + dr.hr.http_status = 0; + dr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED; + } + } break; case MHD_HTTP_GONE: /* could happen if denomination was revoked */ @@ -236,31 +403,27 @@ handle_purse_create_deposit_finished (void *cls, struct TALER_EXCHANGE_PurseCreateDepositHandle * TALER_EXCHANGE_purse_create_with_deposit ( - struct TALER_EXCHANGE_Handle *exchange, + struct GNUNET_CURL_Context *ctx, + const char *url, + struct TALER_EXCHANGE_Keys *keys, const struct TALER_PurseContractPrivateKeyP *purse_priv, const struct TALER_PurseMergePrivateKeyP *merge_priv, const struct TALER_ContractDiffiePrivateP *contract_priv, const json_t *contract_terms, unsigned int num_deposits, - const struct TALER_EXCHANGE_PurseDeposit *deposits, + const struct TALER_EXCHANGE_PurseDeposit deposits[static num_deposits], bool upload_contract, TALER_EXCHANGE_PurseCreateDepositCallback cb, void *cb_cls) { struct TALER_EXCHANGE_PurseCreateDepositHandle *pch; - struct GNUNET_CURL_Context *ctx; json_t *create_obj; json_t *deposit_arr; CURL *eh; - struct TALER_PurseContractSignatureP purse_sig; - struct TALER_PurseContractSignatureP econtract_sig; - struct TALER_ContractDiffiePublicP contract_pub; char arg_str[sizeof (pch->purse_pub) * 2 + 32]; - char *url; uint32_t min_age = 0; pch = GNUNET_new (struct TALER_EXCHANGE_PurseCreateDepositHandle); - pch->exchange = exchange; pch->cb = cb; pch->cb_cls = cb_cls; { @@ -285,8 +448,6 @@ TALER_EXCHANGE_purse_create_with_deposit ( return NULL; } } - GNUNET_assert (GNUNET_YES == - TEAH_handle_is_ready (exchange)); if (GNUNET_OK != TALER_JSON_contract_hash (contract_terms, &pch->h_contract_terms)) @@ -308,71 +469,77 @@ TALER_EXCHANGE_purse_create_with_deposit ( *end = '\0'; GNUNET_snprintf (arg_str, sizeof (arg_str), - "/purses/%s/create", + "purses/%s/create", pub_str); } - GNUNET_CRYPTO_ecdhe_key_get_public (&contract_priv->ecdhe_priv, - &contract_pub.ecdhe_pub); GNUNET_CRYPTO_eddsa_key_get_public (&merge_priv->eddsa_priv, &pch->merge_pub.eddsa_pub); - pch->url = TEAH_path_to_url (exchange, - arg_str); + pch->url = TALER_url_join (url, + arg_str, + NULL); if (NULL == pch->url) { GNUNET_break (0); GNUNET_free (pch); return NULL; } + pch->num_deposits = num_deposits; + pch->deposits = GNUNET_new_array (num_deposits, + struct Deposit); deposit_arr = json_array (); GNUNET_assert (NULL != deposit_arr); - url = TEAH_path_to_url (exchange, - "/"); GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Signing with URL `%s'\n", url); for (unsigned int i = 0; i<num_deposits; i++) { const struct TALER_EXCHANGE_PurseDeposit *deposit = &deposits[i]; + const struct TALER_AgeCommitmentProof *acp = deposit->age_commitment_proof; + struct Deposit *d = &pch->deposits[i]; json_t *jdeposit; - struct TALER_CoinSpendSignatureP coin_sig; - struct TALER_CoinSpendPublicKeyP coin_pub; -#if FIXME_OEC - struct TALER_AgeCommitmentHash agh; struct TALER_AgeCommitmentHash *aghp = NULL; struct TALER_AgeAttestation attest; + struct TALER_AgeAttestation *attestp = NULL; - TALER_age_commitment_hash (&deposit->age_commitment, - &agh); - aghp = &agh; - if (GNUNET_OK != - TALER_age_commitment_attest (&deposit->age_proof, - min_age, - &attest)) + if (NULL != acp) { - GNUNET_break (0); - json_decref (deposit_arr); - GNUNET_free (url); - GNUNET_free (pch); - return NULL; + TALER_age_commitment_hash (&acp->commitment, + &d->ahac); + aghp = &d->ahac; + if (GNUNET_OK != + TALER_age_commitment_attest (acp, + min_age, + &attest)) + { + GNUNET_break (0); + GNUNET_array_grow (pch->deposits, + pch->num_deposits, + 0); + GNUNET_free (pch->url); + json_decref (deposit_arr); + GNUNET_free (pch); + return NULL; + } } -#endif + d->contribution = deposit->amount; + d->h_denom_pub = deposit->h_denom_pub; GNUNET_CRYPTO_eddsa_key_get_public (&deposit->coin_priv.eddsa_priv, - &coin_pub.eddsa_pub); + &d->coin_pub.eddsa_pub); TALER_wallet_purse_deposit_sign ( url, &pch->purse_pub, &deposit->amount, + &d->h_denom_pub, + &d->ahac, &deposit->coin_priv, - &coin_sig); + &d->coin_sig); jdeposit = GNUNET_JSON_PACK ( -#if FIXME_OEC GNUNET_JSON_pack_allow_null ( GNUNET_JSON_pack_data_auto ("h_age_commitment", aghp)), GNUNET_JSON_pack_allow_null ( GNUNET_JSON_pack_data_auto ("age_attestation", - &attest)), -#endif + attestp)), TALER_JSON_pack_amount ("amount", &deposit->amount), GNUNET_JSON_pack_data_auto ("denom_pub_hash", @@ -380,72 +547,56 @@ TALER_EXCHANGE_purse_create_with_deposit ( TALER_JSON_pack_denom_sig ("ub_sig", &deposit->denom_sig), GNUNET_JSON_pack_data_auto ("coin_sig", - &coin_sig), + &d->coin_sig), GNUNET_JSON_pack_data_auto ("coin_pub", - &coin_pub)); + &d->coin_pub)); GNUNET_assert (0 == json_array_append_new (deposit_arr, jdeposit)); } - GNUNET_free (url); TALER_wallet_purse_create_sign (pch->purse_expiration, &pch->h_contract_terms, &pch->merge_pub, min_age, &pch->purse_value_after_fees, purse_priv, - &purse_sig); + &pch->purse_sig); + if (upload_contract) { - void *econtract = NULL; - size_t econtract_size = 0; - - if (upload_contract) - { - TALER_CRYPTO_contract_encrypt_for_merge (&pch->purse_pub, - contract_priv, - merge_priv, - contract_terms, - &econtract, - &econtract_size); - TALER_wallet_econtract_upload_sign (econtract, - econtract_size, - &contract_pub, - purse_priv, - &econtract_sig); - } - create_obj = GNUNET_JSON_PACK ( - TALER_JSON_pack_amount ("amount", - &pch->purse_value_after_fees), - GNUNET_JSON_pack_uint64 ("min_age", - min_age), - GNUNET_JSON_pack_allow_null ( - GNUNET_JSON_pack_data_varsize ("econtract", - econtract, - econtract_size)), - GNUNET_JSON_pack_allow_null ( - (upload_contract) - ? GNUNET_JSON_pack_data_auto ("contract_pub", - &contract_pub) - : GNUNET_JSON_pack_string ("dummy", - NULL)), - GNUNET_JSON_pack_allow_null ( - (upload_contract) - ? GNUNET_JSON_pack_data_auto ("econtract_sig", - &econtract_sig) - : GNUNET_JSON_pack_string ("dummy2", - NULL)), - GNUNET_JSON_pack_data_auto ("purse_sig", - &purse_sig), - GNUNET_JSON_pack_data_auto ("merge_pub", - &pch->merge_pub), - GNUNET_JSON_pack_data_auto ("h_contract_terms", - &pch->h_contract_terms), - GNUNET_JSON_pack_timestamp ("purse_expiration", - pch->purse_expiration), - GNUNET_JSON_pack_array_steal ("deposits", - deposit_arr)); - GNUNET_free (econtract); + TALER_CRYPTO_contract_encrypt_for_merge (&pch->purse_pub, + contract_priv, + merge_priv, + contract_terms, + &pch->econtract.econtract, + &pch->econtract.econtract_size); + GNUNET_CRYPTO_ecdhe_key_get_public (&contract_priv->ecdhe_priv, + &pch->econtract.contract_pub.ecdhe_pub); + TALER_wallet_econtract_upload_sign (pch->econtract.econtract, + pch->econtract.econtract_size, + &pch->econtract.contract_pub, + purse_priv, + &pch->econtract.econtract_sig); } + create_obj = GNUNET_JSON_PACK ( + TALER_JSON_pack_amount ("amount", + &pch->purse_value_after_fees), + GNUNET_JSON_pack_uint64 ("min_age", + min_age), + GNUNET_JSON_pack_allow_null ( + TALER_JSON_pack_econtract ("econtract", + upload_contract + ? &pch->econtract + : NULL)), + GNUNET_JSON_pack_data_auto ("purse_sig", + &pch->purse_sig), + GNUNET_JSON_pack_data_auto ("merge_pub", + &pch->merge_pub), + GNUNET_JSON_pack_data_auto ("h_contract_terms", + &pch->h_contract_terms), + GNUNET_JSON_pack_timestamp ("purse_expiration", + pch->purse_expiration), + GNUNET_JSON_pack_array_steal ("deposits", + deposit_arr)); GNUNET_assert (NULL != create_obj); eh = TALER_EXCHANGE_curl_easy_get_ (pch->url); if ( (NULL == eh) || @@ -458,6 +609,10 @@ TALER_EXCHANGE_purse_create_with_deposit ( if (NULL != eh) curl_easy_cleanup (eh); json_decref (create_obj); + GNUNET_free (pch->econtract.econtract); + GNUNET_array_grow (pch->deposits, + pch->num_deposits, + 0); GNUNET_free (pch->url); GNUNET_free (pch); return NULL; @@ -466,7 +621,8 @@ TALER_EXCHANGE_purse_create_with_deposit ( GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "URL for purse create with deposit: `%s'\n", pch->url); - ctx = TEAH_handle_to_context (exchange); + pch->keys = TALER_EXCHANGE_keys_incref (keys); + pch->exchange_url = GNUNET_strdup (url); pch->job = GNUNET_CURL_job_add2 (ctx, eh, pch->ctx.headers, @@ -485,7 +641,13 @@ TALER_EXCHANGE_purse_create_with_deposit_cancel ( GNUNET_CURL_job_cancel (pch->job); pch->job = NULL; } + GNUNET_free (pch->econtract.econtract); + GNUNET_free (pch->exchange_url); GNUNET_free (pch->url); + GNUNET_array_grow (pch->deposits, + pch->num_deposits, + 0); + TALER_EXCHANGE_keys_decref (pch->keys); TALER_curl_easy_post_finished (&pch->ctx); GNUNET_free (pch); } |