diff options
author | Marcello Stanisci <marcello.stanisci@inria.fr> | 2015-10-20 16:08:47 +0200 |
---|---|---|
committer | Marcello Stanisci <marcello.stanisci@inria.fr> | 2015-10-20 16:08:47 +0200 |
commit | 2e4b1eb7a968833300b8a582835ef97ff11e82c4 (patch) | |
tree | ef78866c47d3db2c7a9ba90d25c01febd57a6945 /src/backend-lib | |
parent | 66a1cb6f783d1fd2c3ae08cd4a0699084590d0e8 (diff) | |
download | merchant-2e4b1eb7a968833300b8a582835ef97ff11e82c4.tar.gz merchant-2e4b1eb7a968833300b8a582835ef97ff11e82c4.tar.bz2 merchant-2e4b1eb7a968833300b8a582835ef97ff11e82c4.zip |
Making the "non official" way of verifying a deposit
confirmation compile.
Diffstat (limited to 'src/backend-lib')
-rw-r--r-- | src/backend-lib/Makefile.am | 1 | ||||
-rw-r--r-- | src/backend-lib/merchant_db.c | 66 | ||||
-rw-r--r-- | src/backend-lib/merchant_db.h | 39 | ||||
-rw-r--r-- | src/backend-lib/taler-merchant-httpd_contract.c | 17 | ||||
-rw-r--r-- | src/backend-lib/taler-merchant-httpd_deposit.c | 88 | ||||
-rw-r--r-- | src/backend-lib/taler_merchant_contract_lib.h | 3 | ||||
-rw-r--r-- | src/backend-lib/taler_merchant_deposit_lib.h | 28 | ||||
-rw-r--r-- | src/backend-lib/taler_merchant_lib.h | 1 |
8 files changed, 224 insertions, 19 deletions
diff --git a/src/backend-lib/Makefile.am b/src/backend-lib/Makefile.am index 61867e41..e4cdb7a3 100644 --- a/src/backend-lib/Makefile.am +++ b/src/backend-lib/Makefile.am @@ -10,6 +10,7 @@ include_HEADERS = \ libtalermerchant_la_SOURCES = \ taler-merchant-httpd_contract.c \ + taler-merchant-httpd_deposit.c \ taler_merchant_contract_lib.h \ merchant_db.c merchant_db.h \ merchant.h diff --git a/src/backend-lib/merchant_db.c b/src/backend-lib/merchant_db.c index befb2c48..035a6646 100644 --- a/src/backend-lib/merchant_db.c +++ b/src/backend-lib/merchant_db.c @@ -150,6 +150,18 @@ MERCHANT_DB_initialize (PGconn *conn, int tmp) EXITIF (PGRES_COMMAND_OK != (status = PQresultStatus(res))); PQclear (res); + EXITIF (NULL == (res = PQprepare + (conn, + "get_contract_set", + "SELECT " + "contract_id, nounce, timestamp, edate, " + "refund_deadline FROM contracts " + "WHERE (" + "hash=$1" + ")", + 1, NULL))); + EXITIF (PGRES_COMMAND_OK != (status = PQresultStatus(res))); + PQclear (res); EXITIF (NULL == (res = PQprepare (conn, @@ -460,3 +472,57 @@ MERCHANT_DB_get_contract_values (PGconn *conn, PQclear (res); return GNUNET_SYSERR; } + +/** +* Get a set of values representing a contract. This function is meant +* to obsolete the '_get_contract_values' version. +* @param h_contract the hashcode of this contract +* @param contract_handle where to store the results +* @raturn GNUNET_OK in case of success, GNUNET_SYSERR +* upon errors +* +*/ + +uint32_t +MERCHANT_DB_get_contract_handle (PGconn *conn, + const struct GNUNET_HashCode *h_contract, + struct MERCHANT_contract_handle *contract_handle) +{ + struct MERCHANT_contract_handle ch; + PGresult *res; + ExecStatusType status; + + struct TALER_PQ_QueryParam params[] = { + TALER_PQ_query_param_fixed_size (h_contract, sizeof (struct GNUNET_HashCode)), + TALER_PQ_query_param_end + }; + + struct TALER_PQ_ResultSpec rs[] = { + TALER_PQ_result_spec_uint64 ("nounce", &ch.nounce), + TALER_PQ_result_spec_absolute_time ("edate", &ch.edate), + TALER_PQ_result_spec_absolute_time ("timestamp", &ch.timestamp), + TALER_PQ_result_spec_absolute_time ("refund_deadline", &ch.refund_deadline), + TALER_PQ_result_spec_uint64 ("contract_id", &ch.contract_id), + TALER_PQ_result_spec_end + }; + + res = TALER_PQ_exec_prepared (conn, "get_contract_set", params); + + status = PQresultStatus (res); + EXITIF (PGRES_TUPLES_OK != status); + if (0 == PQntuples (res)) + { + TALER_LOG_DEBUG ("Contract not found"); + goto EXITIF_exit; + } + + EXITIF (1 != PQntuples (res)); + EXITIF (GNUNET_YES != TALER_PQ_extract_result (res, rs, 0)); + *contract_handle = ch; + PQclear (res); + return GNUNET_OK; + + EXITIF_exit: + PQclear (res); + return GNUNET_SYSERR; +} diff --git a/src/backend-lib/merchant_db.h b/src/backend-lib/merchant_db.h index 2a45c925..540bddbd 100644 --- a/src/backend-lib/merchant_db.h +++ b/src/backend-lib/merchant_db.h @@ -26,6 +26,30 @@ #include <gnunet/gnunet_postgres_lib.h> #include <taler/taler_util.h> +/* Set of values that represent a contract. To be expanded on an + as-needed basis */ +struct MERCHANT_contract_handle +{ + /* The nounce used when hashing the wire details + for this contract */ + uint64_t nounce; + + /* The maximum time when the merchant expects the money tranfer + to his bank account to happen */ + struct GNUNET_TIME_Absolute edate; + + /* The time when this contract was generated */ + struct GNUNET_TIME_Absolute timestamp; + + /* The maximum time until which the merchant could issue a + refund to the customer */ + struct GNUNET_TIME_Absolute refund_deadline; + + /* The identification number for this contract */ + uint64_t contract_id; + +}; + /** * Connect to postgresql database * @@ -125,4 +149,19 @@ MERCHANT_DB_get_contract_values (PGconn *conn, #endif /* MERCHANT_DB_H */ +/** +* Get a set of values representing a contract. This function is meant +* to obsolete the '_get_contract_values' version. +* @param h_contract the hashcode of this contract +* @param contract_handle where to store the results +* @raturn GNUNET_OK in case of success, GNUNET_SYSERR +* upon errors +* +*/ + +uint32_t +MERCHANT_DB_get_contract_handle (PGconn *conn, + const struct GNUNET_HashCode *h_contract, + struct MERCHANT_contract_handle *contract_handle); + /* end of merchant-db.h */ diff --git a/src/backend-lib/taler-merchant-httpd_contract.c b/src/backend-lib/taler-merchant-httpd_contract.c index 5a937e66..5f6744aa 100644 --- a/src/backend-lib/taler-merchant-httpd_contract.c +++ b/src/backend-lib/taler-merchant-httpd_contract.c @@ -6,23 +6,6 @@ #include "merchant_db.h" #include "taler_merchant_contract_lib.h" - -/* TODO: make this file a library, and programmatically call the following - * functions */ - -/** - * Macro to round microseconds to seconds in GNUNET_TIME_* structs. - */ -#define ROUND_TO_SECS(name,us_field) name.us_field -= name.us_field % (1000 * 1000) - -/** - * Shorthand for exit jumps. - */ -#define EXITIF(cond) \ - do { \ - if (cond) { GNUNET_break (0); goto EXITIF_exit; } \ - } while (0) - /** * Take the global wire details and return a JSON containing them, * compliantly with the Taler's API. diff --git a/src/backend-lib/taler-merchant-httpd_deposit.c b/src/backend-lib/taler-merchant-httpd_deposit.c new file mode 100644 index 00000000..41d4ca3d --- /dev/null +++ b/src/backend-lib/taler-merchant-httpd_deposit.c @@ -0,0 +1,88 @@ +#include "platform.h" +#include <jansson.h> +#include <taler/taler_signatures.h> +#include <gnunet/gnunet_util_lib.h> +#include <taler/taler_util.h> +#include "merchant.h" +#include "merchant_db.h" +#include "taler_merchant_contract_lib.h" + +/** +* Verify the signature on a successful deposit permission +* @param h_contract the hashed stringification of this contract +* @param h_wire the hashed 'wire' object holdign the merchant bank's details +* @param timestamp the 32bit wide number representing the number of seconds +* since the Epoch +* @param refund the refund deadline for this deal, expressed in seconds as @a +* timestamp +* @param trans_id an id number for this deal +* @param amount_minus_fee what paid minus its deposit fee +* @param coin_pub the coin's public key +* @param sig the mint's signature +* @param mint_pub mint's key to verify this signature against +* @return GNUNET_OK if the verification succeeds, GNUNET_NO if not, +* GNUNET_SYSERR upon errors +*/ + +uint32_t +MERCHANT_verify_confirmation (const struct GNUNET_HashCode *h_contract, + const struct GNUNET_HashCode *h_wire, + struct GNUNET_TIME_Absolute timestamp, + struct GNUNET_TIME_Absolute refund, + uint64_t trans_id, + const struct TALER_Amount *amount_minus_fee, + const struct TALER_CoinSpendPublicKeyP *coin, + const struct TALER_MerchantPublicKeyP *merchant, + const struct GNUNET_CRYPTO_EddsaSignature *sig, + const struct TALER_MintPublicKeyP *mint_pub) +{ + struct TALER_DepositConfirmationPS dc; + + dc.h_contract = *h_contract; + dc.h_wire = *h_wire; + + dc.merchant = *merchant; + dc.coin_pub = *coin; + + dc.timestamp = GNUNET_TIME_absolute_hton (timestamp); + dc.refund_deadline = GNUNET_TIME_absolute_hton (refund); + TALER_amount_hton (&dc.amount_without_fee, amount_minus_fee); + dc.transaction_id = GNUNET_htonll (trans_id); + + #ifdef DEBUG + char *hwire_enc; + char *hcontract_enc; + char *merchant_enc; + char *coinpub_enc; + + hwire_enc = GNUNET_STRINGS_data_to_string_alloc (h_wire, sizeof (struct GNUNET_HashCode)); + hcontract_enc = GNUNET_STRINGS_data_to_string_alloc (h_contract, sizeof (struct GNUNET_HashCode)); + merchant_enc = GNUNET_STRINGS_data_to_string_alloc (&merchant.eddsa_pub, sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)); + coinpub_enc = GNUNET_STRINGS_data_to_string_alloc (&coin.eddsa_pub, sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)); + + printf ("Signing Confirmation:\nH_wire: %s\nH_contract: %s\nmerchant_pub: %s\ncoin_pub: %s\n" + "timestamp: %llu,\nrefund: %llu,\namount: %s %llu.%lu,\ntrid: %llu\n", + hwire_enc, + hcontract_enc, + merchant_enc, + coinpub_enc, + timestamp_abs.abs_value_us, + refund_abs.abs_value_us, + amount_minus_fee->currency, + amount_minus_fee->value, + amount_minus_fee->fraction, + trans_id); + #endif + + dc.purpose.purpose = htonl (TALER_SIGNATURE_MINT_CONFIRM_DEPOSIT); + dc.purpose.size = htonl (sizeof (struct TALER_DepositConfirmationPS)); + + if (GNUNET_SYSERR == + GNUNET_CRYPTO_eddsa_verify (TALER_SIGNATURE_MINT_CONFIRM_DEPOSIT, + &dc.purpose, + sig, + &mint_pub->eddsa_pub)) + return GNUNET_NO; + return GNUNET_OK; +} + diff --git a/src/backend-lib/taler_merchant_contract_lib.h b/src/backend-lib/taler_merchant_contract_lib.h index 692c849d..ca799ff2 100644 --- a/src/backend-lib/taler_merchant_contract_lib.h +++ b/src/backend-lib/taler_merchant_contract_lib.h @@ -1,6 +1,5 @@ /** - * Simplified version of the contract to be signed, meant to obsolete - * 'struct ContractNBO'. + * The contract sent by the merchant to the wallet */ struct Contract { diff --git a/src/backend-lib/taler_merchant_deposit_lib.h b/src/backend-lib/taler_merchant_deposit_lib.h new file mode 100644 index 00000000..95bbea78 --- /dev/null +++ b/src/backend-lib/taler_merchant_deposit_lib.h @@ -0,0 +1,28 @@ +/** +* Verify the signature on a successful deposit permission +* @param h_contract the hashed stringification of this contract +* @param h_wire the hashed 'wire' object holdign the merchant bank's details +* @param timestamp the 32bit wide number representing the number of seconds +* since the Epoch +* @param refund the refund deadline for this deal, expressed in seconds as @a +* timestamp +* @param trans_id an id number for this deal +* @param amount_minus_fee what paid minus its deposit fee +* @param coin_pub the coin's public key +* @param sig the mint's signature +* @param mint_pub mint's key to verify this signature against +* @return GNUNET_OK if the verification succeeds, GNUNET_NO if not, +* GNUNET_SYSERR upon errors +*/ + +uint32_t +MERCHANT_verify_confirmation (const struct GNUNET_HashCode *h_contract, + const struct GNUNET_HashCode *h_wire, + struct GNUNET_TIME_Absolute timestamp, + struct GNUNET_TIME_Absolute refund, + uint64_t trans_id, + const struct TALER_Amount *amount_minus_fee, + const struct TALER_CoinSpendPublicKeyP *coin, + const struct TALER_MerchantPublicKeyP *merchant, + const struct GNUNET_CRYPTO_EddsaSignature *sig, + const struct TALER_MintPublicKeyP *mint_pub); diff --git a/src/backend-lib/taler_merchant_lib.h b/src/backend-lib/taler_merchant_lib.h index 41891783..ff8d85b7 100644 --- a/src/backend-lib/taler_merchant_lib.h +++ b/src/backend-lib/taler_merchant_lib.h @@ -1 +1,2 @@ #include "taler_merchant_contract_lib.h" +#include "taler_merchant_deposit_lib.h" |