From b5cba3251053c22bf1df46282f1dd0a4c46f6a38 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Tue, 1 Mar 2016 15:35:04 +0100 Subject: renaming mint->exchange --- src/exchange-lib/exchange_api_common.c | 194 +++++++++++++++++++++++++++++++++ 1 file changed, 194 insertions(+) create mode 100644 src/exchange-lib/exchange_api_common.c (limited to 'src/exchange-lib/exchange_api_common.c') diff --git a/src/exchange-lib/exchange_api_common.c b/src/exchange-lib/exchange_api_common.c new file mode 100644 index 00000000..805c3fc4 --- /dev/null +++ b/src/exchange-lib/exchange_api_common.c @@ -0,0 +1,194 @@ +/* + This file is part of TALER + Copyright (C) 2015 GNUnet e.V. + + 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, If not, see + +*/ +/** + * @file exchange-lib/exchange_api_common.c + * @brief common functions for the exchange API + * @author Christian Grothoff + */ +#include "platform.h" +#include "exchange_api_common.h" +#include "exchange_api_json.h" +#include "exchange_api_context.h" +#include "exchange_api_handle.h" +#include "taler_signatures.h" + + +/** + * Verify a coins transaction history as returned by the exchange. + * + * @param currency expected currency for the coin + * @param coin_pub public key of the coin + * @param history history of the coin in json encoding + * @param[out] total how much of the coin has been spent according to @a history + * @return #GNUNET_OK if @a history is valid, #GNUNET_SYSERR if not + */ +int +TALER_EXCHANGE_verify_coin_history_ (const char *currency, + const struct TALER_CoinSpendPublicKeyP *coin_pub, + json_t *history, + struct TALER_Amount *total) +{ + size_t len; + size_t off; + + if (NULL == history) + { + GNUNET_break_op (0); + return GNUNET_SYSERR; + } + len = json_array_size (history); + if (0 == len) + { + GNUNET_break_op (0); + return GNUNET_SYSERR; + } + TALER_amount_get_zero (currency, + total); + for (off=0;offpurpose.size)) + { + GNUNET_break_op (0); + MAJ_parse_free (spec); + return GNUNET_SYSERR; + } + if (GNUNET_OK != + GNUNET_CRYPTO_eddsa_verify (TALER_SIGNATURE_WALLET_COIN_DEPOSIT, + &dr->purpose, + &sig.eddsa_signature, + &coin_pub->eddsa_pub)) + { + GNUNET_break_op (0); + MAJ_parse_free (spec); + return GNUNET_SYSERR; + } + + // FIXME: check sig! + TALER_amount_ntoh (&dr_amount, + &dr->amount_with_fee); + if (0 != TALER_amount_cmp (&dr_amount, + &amount)) + { + GNUNET_break (0); + MAJ_parse_free (spec); + return GNUNET_SYSERR; + } + } + else if (0 == strcasecmp (type, + "MELT")) + { + const struct TALER_RefreshMeltCoinAffirmationPS *rm; + struct TALER_Amount rm_amount; + + if (details_size != sizeof (struct TALER_RefreshMeltCoinAffirmationPS)) + { + GNUNET_break_op (0); + MAJ_parse_free (spec); + return GNUNET_SYSERR; + } + rm = (const struct TALER_RefreshMeltCoinAffirmationPS *) details; + if (details_size != ntohl (rm->purpose.size)) + { + GNUNET_break_op (0); + MAJ_parse_free (spec); + return GNUNET_SYSERR; + } + if (GNUNET_OK != + GNUNET_CRYPTO_eddsa_verify (TALER_SIGNATURE_WALLET_COIN_MELT, + &rm->purpose, + &sig.eddsa_signature, + &coin_pub->eddsa_pub)) + { + GNUNET_break_op (0); + MAJ_parse_free (spec); + return GNUNET_SYSERR; + } + TALER_amount_ntoh (&rm_amount, + &rm->amount_with_fee); + if (0 != TALER_amount_cmp (&rm_amount, + &amount)) + { + GNUNET_break_op (0); + MAJ_parse_free (spec); + return GNUNET_SYSERR; + } + } + else + { + /* signature not supported, new version on server? */ + GNUNET_break_op (0); + MAJ_parse_free (spec); + return GNUNET_SYSERR; + } + if (GNUNET_OK != + TALER_amount_add (total, + total, + &amount)) + { + /* overflow in history already!? inconceivable! Bad exchange! */ + GNUNET_break_op (0); + MAJ_parse_free (spec); + return GNUNET_SYSERR; + } + MAJ_parse_free (spec); + } + return GNUNET_OK; +} + + +/* end of exchange_api_common.c */ -- cgit v1.2.3 From ae726ea31b8607eacd2896617251cc0cab253111 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sat, 19 Mar 2016 15:54:21 +0100 Subject: remove duplicated JSON parsing code from exchange-lib (#4150) --- src/exchange-lib/Makefile.am | 1 - src/exchange-lib/exchange_api_admin.c | 1 - src/exchange-lib/exchange_api_common.c | 41 +- src/exchange-lib/exchange_api_deposit.c | 14 +- src/exchange-lib/exchange_api_deposit_wtid.c | 41 +- src/exchange-lib/exchange_api_handle.c | 96 ++--- src/exchange-lib/exchange_api_json.c | 541 -------------------------- src/exchange-lib/exchange_api_json.h | 352 ----------------- src/exchange-lib/exchange_api_refresh.c | 55 +-- src/exchange-lib/exchange_api_refresh_link.c | 66 ++-- src/exchange-lib/exchange_api_reserve.c | 69 ++-- src/exchange-lib/exchange_api_wire.c | 23 +- src/exchange-lib/exchange_api_wire_deposits.c | 39 +- 13 files changed, 231 insertions(+), 1108 deletions(-) delete mode 100644 src/exchange-lib/exchange_api_json.c delete mode 100644 src/exchange-lib/exchange_api_json.h (limited to 'src/exchange-lib/exchange_api_common.c') diff --git a/src/exchange-lib/Makefile.am b/src/exchange-lib/Makefile.am index a043e860..10188c8f 100644 --- a/src/exchange-lib/Makefile.am +++ b/src/exchange-lib/Makefile.am @@ -16,7 +16,6 @@ libtalerexchange_la_LDFLAGS = \ libtalerexchange_la_SOURCES = \ exchange_api_common.c exchange_api_common.h \ exchange_api_context.c exchange_api_context.h \ - exchange_api_json.c exchange_api_json.h \ exchange_api_handle.c exchange_api_handle.h \ exchange_api_admin.c \ exchange_api_deposit.c \ diff --git a/src/exchange-lib/exchange_api_admin.c b/src/exchange-lib/exchange_api_admin.c index 4ed761fb..871a88a6 100644 --- a/src/exchange-lib/exchange_api_admin.c +++ b/src/exchange-lib/exchange_api_admin.c @@ -27,7 +27,6 @@ #include #include "taler_json_lib.h" #include "taler_exchange_service.h" -#include "exchange_api_json.h" #include "exchange_api_context.h" #include "exchange_api_handle.h" #include "taler_signatures.h" diff --git a/src/exchange-lib/exchange_api_common.c b/src/exchange-lib/exchange_api_common.c index 805c3fc4..6d2408d9 100644 --- a/src/exchange-lib/exchange_api_common.c +++ b/src/exchange-lib/exchange_api_common.c @@ -21,7 +21,7 @@ */ #include "platform.h" #include "exchange_api_common.h" -#include "exchange_api_json.h" +#include "taler_json_lib.h" #include "exchange_api_context.h" #include "exchange_api_handle.h" #include "taler_signatures.h" @@ -66,24 +66,25 @@ TALER_EXCHANGE_verify_coin_history_ (const char *currency, void *details; size_t details_size; const char *type; - struct MAJ_Specification spec[] = { - MAJ_spec_amount ("amount", + struct GNUNET_JSON_Specification spec[] = { + TALER_JSON_spec_amount ("amount", &amount), - MAJ_spec_string ("type", + GNUNET_JSON_spec_string ("type", &type), - MAJ_spec_fixed_auto ("signature", + GNUNET_JSON_spec_fixed_auto ("signature", &sig), - MAJ_spec_varsize ("details", + GNUNET_JSON_spec_varsize ("details", &details, &details_size), - MAJ_spec_end + GNUNET_JSON_spec_end() }; transaction = json_array_get (history, off); if (GNUNET_OK != - MAJ_parse_json (transaction, - spec)) + GNUNET_JSON_parse (transaction, + spec, + NULL, NULL)) { GNUNET_break_op (0); return GNUNET_SYSERR; @@ -97,14 +98,14 @@ TALER_EXCHANGE_verify_coin_history_ (const char *currency, if (details_size != sizeof (struct TALER_DepositRequestPS)) { GNUNET_break_op (0); - MAJ_parse_free (spec); + GNUNET_JSON_parse_free (spec); return GNUNET_SYSERR; } dr = (const struct TALER_DepositRequestPS *) details; if (details_size != ntohl (dr->purpose.size)) { GNUNET_break_op (0); - MAJ_parse_free (spec); + GNUNET_JSON_parse_free (spec); return GNUNET_SYSERR; } if (GNUNET_OK != @@ -114,7 +115,7 @@ TALER_EXCHANGE_verify_coin_history_ (const char *currency, &coin_pub->eddsa_pub)) { GNUNET_break_op (0); - MAJ_parse_free (spec); + GNUNET_JSON_parse_free (spec); return GNUNET_SYSERR; } @@ -125,7 +126,7 @@ TALER_EXCHANGE_verify_coin_history_ (const char *currency, &amount)) { GNUNET_break (0); - MAJ_parse_free (spec); + GNUNET_JSON_parse_free (spec); return GNUNET_SYSERR; } } @@ -138,14 +139,14 @@ TALER_EXCHANGE_verify_coin_history_ (const char *currency, if (details_size != sizeof (struct TALER_RefreshMeltCoinAffirmationPS)) { GNUNET_break_op (0); - MAJ_parse_free (spec); + GNUNET_JSON_parse_free (spec); return GNUNET_SYSERR; } rm = (const struct TALER_RefreshMeltCoinAffirmationPS *) details; if (details_size != ntohl (rm->purpose.size)) { GNUNET_break_op (0); - MAJ_parse_free (spec); + GNUNET_JSON_parse_free (spec); return GNUNET_SYSERR; } if (GNUNET_OK != @@ -155,7 +156,7 @@ TALER_EXCHANGE_verify_coin_history_ (const char *currency, &coin_pub->eddsa_pub)) { GNUNET_break_op (0); - MAJ_parse_free (spec); + GNUNET_JSON_parse_free (spec); return GNUNET_SYSERR; } TALER_amount_ntoh (&rm_amount, @@ -164,7 +165,7 @@ TALER_EXCHANGE_verify_coin_history_ (const char *currency, &amount)) { GNUNET_break_op (0); - MAJ_parse_free (spec); + GNUNET_JSON_parse_free (spec); return GNUNET_SYSERR; } } @@ -172,7 +173,7 @@ TALER_EXCHANGE_verify_coin_history_ (const char *currency, { /* signature not supported, new version on server? */ GNUNET_break_op (0); - MAJ_parse_free (spec); + GNUNET_JSON_parse_free (spec); return GNUNET_SYSERR; } if (GNUNET_OK != @@ -182,10 +183,10 @@ TALER_EXCHANGE_verify_coin_history_ (const char *currency, { /* overflow in history already!? inconceivable! Bad exchange! */ GNUNET_break_op (0); - MAJ_parse_free (spec); + GNUNET_JSON_parse_free (spec); return GNUNET_SYSERR; } - MAJ_parse_free (spec); + GNUNET_JSON_parse_free (spec); } return GNUNET_OK; } diff --git a/src/exchange-lib/exchange_api_deposit.c b/src/exchange-lib/exchange_api_deposit.c index ef7a59e6..8ec45a23 100644 --- a/src/exchange-lib/exchange_api_deposit.c +++ b/src/exchange-lib/exchange_api_deposit.c @@ -29,7 +29,6 @@ #include "taler_json_lib.h" #include "taler_exchange_service.h" #include "exchange_api_common.h" -#include "exchange_api_json.h" #include "exchange_api_context.h" #include "exchange_api_handle.h" #include "taler_signatures.h" @@ -109,15 +108,16 @@ verify_deposit_signature_ok (const struct TALER_EXCHANGE_DepositHandle *dh, struct TALER_ExchangeSignatureP exchange_sig; struct TALER_ExchangePublicKeyP exchange_pub; const struct TALER_EXCHANGE_Keys *key_state; - struct MAJ_Specification spec[] = { - MAJ_spec_fixed_auto ("sig", &exchange_sig), - MAJ_spec_fixed_auto ("pub", &exchange_pub), - MAJ_spec_end + struct GNUNET_JSON_Specification spec[] = { + GNUNET_JSON_spec_fixed_auto ("sig", &exchange_sig), + GNUNET_JSON_spec_fixed_auto ("pub", &exchange_pub), + GNUNET_JSON_spec_end() }; if (GNUNET_OK != - MAJ_parse_json (json, - spec)) + GNUNET_JSON_parse (json, + spec, + NULL, NULL)) { GNUNET_break_op (0); return GNUNET_SYSERR; diff --git a/src/exchange-lib/exchange_api_deposit_wtid.c b/src/exchange-lib/exchange_api_deposit_wtid.c index f3b5d2c0..1ad1dd01 100644 --- a/src/exchange-lib/exchange_api_deposit_wtid.c +++ b/src/exchange-lib/exchange_api_deposit_wtid.c @@ -25,9 +25,9 @@ #include /* just for HTTP status codes */ #include #include +#include "taler_json_lib.h" #include "taler_exchange_service.h" #include "exchange_api_common.h" -#include "exchange_api_json.h" #include "exchange_api_context.h" #include "exchange_api_handle.h" #include "taler_signatures.h" @@ -98,15 +98,16 @@ verify_deposit_wtid_signature_ok (const struct TALER_EXCHANGE_DepositWtidHandle struct TALER_ExchangeSignatureP exchange_sig; struct TALER_ExchangePublicKeyP exchange_pub; const struct TALER_EXCHANGE_Keys *key_state; - struct MAJ_Specification spec[] = { - MAJ_spec_fixed_auto ("exchange_sig", &exchange_sig), - MAJ_spec_fixed_auto ("exchange_pub", &exchange_pub), - MAJ_spec_end + struct GNUNET_JSON_Specification spec[] = { + GNUNET_JSON_spec_fixed_auto ("exchange_sig", &exchange_sig), + GNUNET_JSON_spec_fixed_auto ("exchange_pub", &exchange_pub), + GNUNET_JSON_spec_end() }; if (GNUNET_OK != - MAJ_parse_json (json, - spec)) + GNUNET_JSON_parse (json, + spec, + NULL, NULL)) { GNUNET_break_op (0); return GNUNET_SYSERR; @@ -161,16 +162,17 @@ handle_deposit_wtid_finished (void *cls, break; case MHD_HTTP_OK: { - struct MAJ_Specification spec[] = { - MAJ_spec_fixed_auto ("wtid", &dwh->depconf.wtid), - MAJ_spec_absolute_time ("execution_time", &execution_time), - MAJ_spec_amount ("coin_contribution", &coin_contribution_s), - MAJ_spec_end + struct GNUNET_JSON_Specification spec[] = { + GNUNET_JSON_spec_fixed_auto ("wtid", &dwh->depconf.wtid), + GNUNET_JSON_spec_absolute_time ("execution_time", &execution_time), + TALER_JSON_spec_amount ("coin_contribution", &coin_contribution_s), + GNUNET_JSON_spec_end() }; if (GNUNET_OK != - MAJ_parse_json (json, - spec)) + GNUNET_JSON_parse (json, + spec, + NULL, NULL)) { GNUNET_break_op (0); response_code = 0; @@ -193,14 +195,15 @@ handle_deposit_wtid_finished (void *cls, case MHD_HTTP_ACCEPTED: { /* Transaction known, but not executed yet */ - struct MAJ_Specification spec[] = { - MAJ_spec_absolute_time ("execution_time", &execution_time), - MAJ_spec_end + struct GNUNET_JSON_Specification spec[] = { + GNUNET_JSON_spec_absolute_time ("execution_time", &execution_time), + GNUNET_JSON_spec_end() }; if (GNUNET_OK != - MAJ_parse_json (json, - spec)) + GNUNET_JSON_parse (json, + spec, + NULL, NULL)) { GNUNET_break_op (0); response_code = 0; diff --git a/src/exchange-lib/exchange_api_handle.c b/src/exchange-lib/exchange_api_handle.c index d4b3e4de..aaca8bac 100644 --- a/src/exchange-lib/exchange_api_handle.c +++ b/src/exchange-lib/exchange_api_handle.c @@ -22,13 +22,11 @@ */ #include "platform.h" #include -#include -#include #include +#include "taler_json_lib.h" #include "taler_exchange_service.h" #include "taler_signatures.h" #include "exchange_api_context.h" -#include "exchange_api_json.h" #include "exchange_api_handle.h" @@ -189,23 +187,24 @@ parse_json_signkey (struct TALER_EXCHANGE_SigningPublicKey *sign_key, struct GNUNET_TIME_Absolute valid_from; struct GNUNET_TIME_Absolute valid_until; struct GNUNET_TIME_Absolute valid_legal; - struct MAJ_Specification spec[] = { - MAJ_spec_fixed_auto ("master_sig", + struct GNUNET_JSON_Specification spec[] = { + GNUNET_JSON_spec_fixed_auto ("master_sig", &sig), - MAJ_spec_fixed_auto ("key", + GNUNET_JSON_spec_fixed_auto ("key", &sign_key_issue.signkey_pub), - MAJ_spec_absolute_time ("stamp_start", + GNUNET_JSON_spec_absolute_time ("stamp_start", &valid_from), - MAJ_spec_absolute_time ("stamp_expire", + GNUNET_JSON_spec_absolute_time ("stamp_expire", &valid_until), - MAJ_spec_absolute_time ("stamp_end", + GNUNET_JSON_spec_absolute_time ("stamp_end", &valid_legal), - MAJ_spec_end + GNUNET_JSON_spec_end() }; if (GNUNET_OK != - MAJ_parse_json (sign_key_obj, - spec)) + GNUNET_JSON_parse (sign_key_obj, + spec, + NULL, NULL)) { GNUNET_break_op (0); return GNUNET_SYSERR; @@ -264,33 +263,33 @@ parse_json_denomkey (struct TALER_EXCHANGE_DenomPublicKey *denom_key, struct GNUNET_CRYPTO_rsa_PublicKey *pk; struct GNUNET_CRYPTO_EddsaSignature sig; - struct MAJ_Specification spec[] = { - MAJ_spec_fixed_auto ("master_sig", + struct GNUNET_JSON_Specification spec[] = { + GNUNET_JSON_spec_fixed_auto ("master_sig", &sig), - MAJ_spec_absolute_time ("stamp_expire_deposit", + GNUNET_JSON_spec_absolute_time ("stamp_expire_deposit", &deposit_valid_until), - MAJ_spec_absolute_time ("stamp_expire_withdraw", + GNUNET_JSON_spec_absolute_time ("stamp_expire_withdraw", &withdraw_valid_until), - MAJ_spec_absolute_time ("stamp_start", + GNUNET_JSON_spec_absolute_time ("stamp_start", &valid_from), - MAJ_spec_absolute_time ("stamp_expire_legal", + GNUNET_JSON_spec_absolute_time ("stamp_expire_legal", &expire_legal), - MAJ_spec_amount ("value", + TALER_JSON_spec_amount ("value", &value), - MAJ_spec_amount ("fee_withdraw", + TALER_JSON_spec_amount ("fee_withdraw", &fee_withdraw), - MAJ_spec_amount ("fee_deposit", + TALER_JSON_spec_amount ("fee_deposit", &fee_deposit), - MAJ_spec_amount ("fee_refresh", + TALER_JSON_spec_amount ("fee_refresh", &fee_refresh), - MAJ_spec_rsa_public_key ("denom_pub", + GNUNET_JSON_spec_rsa_public_key ("denom_pub", &pk), - MAJ_spec_end + GNUNET_JSON_spec_end() }; if (GNUNET_OK != - MAJ_parse_json (denom_key_obj, - spec)) + GNUNET_JSON_parse (denom_key_obj, + spec, NULL, NULL)) { GNUNET_break_op (0); return GNUNET_SYSERR; @@ -337,7 +336,7 @@ parse_json_denomkey (struct TALER_EXCHANGE_DenomPublicKey *denom_key, return GNUNET_OK; EXITIF_exit: - MAJ_parse_free (spec); + GNUNET_JSON_parse_free (spec); return GNUNET_SYSERR; } @@ -362,18 +361,19 @@ parse_json_auditor (struct TALER_EXCHANGE_AuditorInformation *auditor, unsigned int off; unsigned int i; struct TALER_ExchangeKeyValidityPS kv; - struct MAJ_Specification spec[] = { - MAJ_spec_fixed_auto ("auditor_pub", + struct GNUNET_JSON_Specification spec[] = { + GNUNET_JSON_spec_fixed_auto ("auditor_pub", &auditor->auditor_pub), - MAJ_spec_json ("denomination_keys", + GNUNET_JSON_spec_json ("denomination_keys", &keys), - MAJ_spec_end + GNUNET_JSON_spec_end() }; auditor->auditor_url = NULL; /* #3987 */ if (GNUNET_OK != - MAJ_parse_json (auditor_obj, - spec)) + GNUNET_JSON_parse (auditor_obj, + spec, + NULL, NULL)) { GNUNET_break_op (0); return GNUNET_SYSERR; @@ -391,17 +391,18 @@ parse_json_auditor (struct TALER_EXCHANGE_AuditorInformation *auditor, struct GNUNET_HashCode denom_h; const struct TALER_EXCHANGE_DenomPublicKey *dk; unsigned int j; - struct MAJ_Specification spec[] = { - MAJ_spec_fixed_auto ("denom_pub_h", + struct GNUNET_JSON_Specification spec[] = { + GNUNET_JSON_spec_fixed_auto ("denom_pub_h", &denom_h), - MAJ_spec_fixed_auto ("auditor_sig", + GNUNET_JSON_spec_fixed_auto ("auditor_sig", &auditor_sig), - MAJ_spec_end + GNUNET_JSON_spec_end() }; if (GNUNET_OK != - MAJ_parse_json (key, - spec)) + GNUNET_JSON_parse (key, + spec, + NULL, NULL)) { GNUNET_break_op (0); continue; @@ -476,21 +477,22 @@ decode_keys_json (json_t *resp_obj, hash_context = GNUNET_CRYPTO_hash_context_start (); /* parse the master public key and issue date of the response */ { - struct MAJ_Specification spec[] = { - MAJ_spec_fixed_auto ("master_public_key", + struct GNUNET_JSON_Specification spec[] = { + GNUNET_JSON_spec_fixed_auto ("master_public_key", &key_data->master_pub), - MAJ_spec_fixed_auto ("eddsa_sig", + GNUNET_JSON_spec_fixed_auto ("eddsa_sig", &sig), - MAJ_spec_fixed_auto ("eddsa_pub", + GNUNET_JSON_spec_fixed_auto ("eddsa_pub", &pub), - MAJ_spec_absolute_time ("list_issue_date", + GNUNET_JSON_spec_absolute_time ("list_issue_date", &list_issue_date), - MAJ_spec_end + GNUNET_JSON_spec_end() }; EXITIF (GNUNET_OK != - MAJ_parse_json (resp_obj, - spec)); + GNUNET_JSON_parse (resp_obj, + spec, + NULL, NULL)); } /* parse the signing keys */ diff --git a/src/exchange-lib/exchange_api_json.c b/src/exchange-lib/exchange_api_json.c deleted file mode 100644 index d6c54be5..00000000 --- a/src/exchange-lib/exchange_api_json.c +++ /dev/null @@ -1,541 +0,0 @@ -/* - This file is part of TALER - Copyright (C) 2014, 2015 GNUnet e.V. - - TALER is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero 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 Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - TALER; see the file COPYING. If not, If not, see -*/ -/** - * @file exchange-lib/exchange_api_json.c - * @brief functions to parse incoming requests (JSON snippets) - * @author Florian Dold - * @author Benedikt Mueller - * @author Christian Grothoff - */ -#include "platform.h" -#include "exchange_api_json.h" -#include "taler_json_lib.h" - -/** - * Navigate and parse data in a JSON tree. - * - * @param root the JSON node to start the navigation at. - * @param spec parse specification array - * @return offset in @a spec where parsing failed, -1 on success (!) - */ -static int -parse_json (json_t *root, - struct MAJ_Specification *spec) -{ - int i; - json_t *pos; /* what's our current position? */ - - pos = root; - for (i=0;MAJ_CMD_END != spec[i].cmd;i++) - { - pos = json_object_get (root, - spec[i].field); - if (NULL == pos) - { - GNUNET_break_op (0); - return i; - } - switch (spec[i].cmd) - { - case MAJ_CMD_END: - GNUNET_assert (0); - return i; - case MAJ_CMD_AMOUNT: - { - struct GNUNET_JSON_Specification nspec[] = { - TALER_JSON_spec_amount (NULL, spec[i].details.amount), - GNUNET_JSON_spec_end () - }; - - if (GNUNET_OK != - GNUNET_JSON_parse (pos, - nspec, - NULL, NULL)) - { - GNUNET_break_op (0); - return i; - } - break; - } - case MAJ_CMD_TIME_ABSOLUTE: - { - struct GNUNET_JSON_Specification nspec[] = { - GNUNET_JSON_spec_absolute_time (NULL, spec[i].details.abs_time), - GNUNET_JSON_spec_end () - }; - - if (GNUNET_OK != - GNUNET_JSON_parse (pos, - nspec, - NULL, NULL)) - { - GNUNET_break_op (0); - return i; - } - break; - } - case MAJ_CMD_STRING: - { - const char *str; - - str = json_string_value (pos); - if (NULL == str) - { - GNUNET_break_op (0); - return i; - } - *spec[i].details.strptr = str; - } - break; - - case MAJ_CMD_BINARY_FIXED: - { - const char *str; - int res; - - str = json_string_value (pos); - if (NULL == str) - { - GNUNET_break_op (0); - return i; - } - res = GNUNET_STRINGS_string_to_data (str, strlen (str), - spec[i].details.fixed_data.dest, - spec[i].details.fixed_data.dest_size); - if (GNUNET_OK != res) - { - GNUNET_break_op (0); - return i; - } - } - break; - - case MAJ_CMD_BINARY_VARIABLE: - { - const char *str; - size_t size; - void *data; - int res; - - str = json_string_value (pos); - if (NULL == str) - { - GNUNET_break_op (0); - return i; - } - size = (strlen (str) * 5) / 8; - if (size >= 1024) - { - GNUNET_break_op (0); - return i; - } - data = GNUNET_malloc (size); - res = GNUNET_STRINGS_string_to_data (str, - strlen (str), - data, - size); - if (GNUNET_OK != res) - { - GNUNET_break_op (0); - GNUNET_free (data); - return i; - } - *spec[i].details.variable_data.dest_p = data; - *spec[i].details.variable_data.dest_size_p = size; - } - break; - - case MAJ_CMD_RSA_PUBLIC_KEY: - { - size_t size; - const char *str; - int res; - void *buf; - - str = json_string_value (pos); - if (NULL == str) - { - GNUNET_break_op (0); - return i; - } - size = (strlen (str) * 5) / 8; - buf = GNUNET_malloc (size); - res = GNUNET_STRINGS_string_to_data (str, - strlen (str), - buf, - size); - if (GNUNET_OK != res) - { - GNUNET_free (buf); - GNUNET_break_op (0); - return i; - } - *spec[i].details.rsa_public_key - = GNUNET_CRYPTO_rsa_public_key_decode (buf, - size); - GNUNET_free (buf); - if (NULL == spec[i].details.rsa_public_key) - { - GNUNET_break_op (0); - return i; - } - } - break; - - case MAJ_CMD_RSA_SIGNATURE: - { - size_t size; - const char *str; - int res; - void *buf; - - str = json_string_value (pos); - if (NULL == str) - { - GNUNET_break_op (0); - return i; - } - size = (strlen (str) * 5) / 8; - buf = GNUNET_malloc (size); - res = GNUNET_STRINGS_string_to_data (str, - strlen (str), - buf, - size); - if (GNUNET_OK != res) - { - GNUNET_free (buf); - GNUNET_break_op (0); - return i; - } - *spec[i].details.rsa_signature - = GNUNET_CRYPTO_rsa_signature_decode (buf, - size); - GNUNET_free (buf); - if (NULL == spec[i].details.rsa_signature) - return i; - } - break; - - case MAJ_CMD_UINT16: - { - json_int_t val; - - if (! json_is_integer (pos)) - { - GNUNET_break_op (0); - return i; - } - val = json_integer_value (pos); - if ( (0 > val) || (val > UINT16_MAX) ) - { - GNUNET_break_op (0); - return i; - } - *spec[i].details.u16 = (uint16_t) val; - } - break; - - case MAJ_CMD_UINT64: - { - json_int_t val; - - if (! json_is_integer (pos)) - { - GNUNET_break_op (0); - return i; - } - val = json_integer_value (pos); - *spec[i].details.u64 = (uint64_t) val; - } - break; - - case MAJ_CMD_JSON_OBJECT: - { - if (! (json_is_object (pos) || json_is_array (pos)) ) - { - GNUNET_break_op (0); - return i; - } - json_incref (pos); - *spec[i].details.obj = pos; - } - break; - - default: - GNUNET_break (0); - return i; - } - } - return -1; /* all OK! */ -} - - -/** - * Free all elements allocated during a - * #MAJ_parse_json() operation. - * - * @param spec specification of the parse operation - * @param end number of elements in @a spec to process - */ -static void -parse_free (struct MAJ_Specification *spec, - int end) -{ - int i; - - for (i=0;i -*/ -/** - * @file exchange-lib/exchange_api_json.h - * @brief functions to parse incoming requests (JSON snippets) - * @author Florian Dold - * @author Benedikt Mueller - * @author Christian Grothoff - */ -#include "platform.h" -#include -#include "taler_util.h" -#include - - -/** - * Enumeration with the various commands for the - * #MAJ_parse_json interpreter. - */ -enum MAJ_Command -{ - - /** - * End of command list. - */ - MAJ_CMD_END, - - /** - * Parse amount at current position. - */ - MAJ_CMD_AMOUNT, - - /** - * Parse absolute time at current position. - */ - MAJ_CMD_TIME_ABSOLUTE, - - /** - * Parse fixed binary value at current position. - */ - MAJ_CMD_BINARY_FIXED, - - /** - * Parse variable-size binary value at current position. - */ - MAJ_CMD_BINARY_VARIABLE, - - /** - * Parse RSA public key at current position. - */ - MAJ_CMD_RSA_PUBLIC_KEY, - - /** - * Parse RSA signature at current position. - */ - MAJ_CMD_RSA_SIGNATURE, - - /** - * Parse `const char *` JSON string at current position. - */ - MAJ_CMD_STRING, - - /** - * Parse `uint16_t` integer at the current position. - */ - MAJ_CMD_UINT16, - - /** - * Parse `uint64_t` integer at the current position. - */ - MAJ_CMD_UINT64, - - /** - * Parse JSON object at the current position. - */ - MAJ_CMD_JSON_OBJECT, - - /** - * Parse ??? at current position. - */ - MAJ_CMD_C - -}; - - -/** - * @brief Entry in parser specification for #MAJ_parse_json. - */ -struct MAJ_Specification -{ - - /** - * Command to execute. - */ - enum MAJ_Command cmd; - - /** - * Name of the field to access. - */ - const char *field; - - /** - * Further details for the command. - */ - union { - - /** - * Where to store amount for #MAJ_CMD_AMOUNT. - */ - struct TALER_Amount *amount; - - /** - * Where to store time, for #MAJ_CMD_TIME_ABSOLUTE. - */ - struct GNUNET_TIME_Absolute *abs_time; - - /** - * Where to write binary data, for #MAJ_CMD_BINARY_FIXED. - */ - struct { - /** - * Where to write the data. - */ - void *dest; - - /** - * How many bytes to write to @e dest. - */ - size_t dest_size; - - } fixed_data; - - /** - * Where to write binary data, for #MAJ_CMD_BINARY_VARIABLE. - */ - struct { - /** - * Where to store the pointer with the data (is allocated). - */ - void **dest_p; - - /** - * Where to store the number of bytes allocated at `*dest`. - */ - size_t *dest_size_p; - - } variable_data; - - /** - * Where to store the RSA public key for #MAJ_CMD_RSA_PUBLIC_KEY - */ - struct GNUNET_CRYPTO_rsa_PublicKey **rsa_public_key; - - /** - * Where to store the RSA signature for #MAJ_CMD_RSA_SIGNATURE - */ - struct GNUNET_CRYPTO_rsa_Signature **rsa_signature; - - /** - * Details for #MAJ_CMD_EDDSA_SIGNATURE - */ - struct { - - /** - * Where to store the purpose. - */ - struct GNUNET_CRYPTO_EccSignaturePurpose **purpose_p; - - /** - * Key to verify the signature against. - */ - const struct GNUNET_CRYPTO_EddsaPublicKey *pub_key; - - } eddsa_signature; - - /** - * Where to store a pointer to the string. - */ - const char **strptr; - - /** - * Where to store 16-bit integer. - */ - uint16_t *u16; - - /** - * Where to store 64-bit integer. - */ - uint64_t *u64; - - /** - * Where to store a JSON object. - */ - json_t **obj; - - } details; - -}; - - -/** - * Navigate and parse data in a JSON tree. - * - * @param root the JSON node to start the navigation at. - * @param spec parse specification array - * @return #GNUNET_OK on success, #GNUNET_SYSERR on error - */ -int -MAJ_parse_json (const json_t *root, - struct MAJ_Specification *spec); - - -/** - * Free all elements allocated during a - * #MAJ_parse_json() operation. - * - * @param spec specification of the parse operation - */ -void -MAJ_parse_free (struct MAJ_Specification *spec); - - -/** - * End of a parser specification. - */ -#define MAJ_spec_end { .cmd = MAJ_CMD_END } - -/** - * Fixed size object (in network byte order, encoded using Crockford - * Base32hex encoding). - * - * @param name name of the JSON field - * @param obj pointer where to write the data (type of `*obj` will determine size) - */ -#define MAJ_spec_fixed_auto(name,obj) { .cmd = MAJ_CMD_BINARY_FIXED, .field = name, .details.fixed_data.dest = obj, .details.fixed_data.dest_size = sizeof (*obj) } - - -/** - * Variable size object (in network byte order, encoded using Crockford - * Base32hex encoding). - * - * @param name name of the JSON field - * @param obj pointer where to write the data (a `void **`) - * @param size where to store the number of bytes allocated for @a obj (of type `size_t *` - */ -#define MAJ_spec_varsize(name,obj,size) { .cmd = MAJ_CMD_BINARY_VARIABLE, .field = name, .details.variable_data.dest_p = obj, .details.variable_data.dest_size_p = size } - - -/** - * The expected field stores a string. - * - * @param name name of the JSON field - * @param strptr where to store a pointer to the field - */ -struct MAJ_Specification -MAJ_spec_string (const char *name, - const char **strptr); - - -/** - * Absolute time. - * - * @param name name of the JSON field - * @param[out] at where to store the absolute time found under @a name - */ -struct MAJ_Specification -MAJ_spec_absolute_time (const char *name, - struct GNUNET_TIME_Absolute *at); - - -/** - * 16-bit integer. - * - * @param name name of the JSON field - * @param[out] u16 where to store the integer found under @a name - */ -struct MAJ_Specification -MAJ_spec_uint16 (const char *name, - uint16_t *u16); - - -/** - * 64-bit integer. - * - * @param name name of the JSON field - * @param[out] u64 where to store the integer found under @a name - */ -struct MAJ_Specification -MAJ_spec_uint64 (const char *name, - uint64_t *u64); - - -/** - * JSON object. - * - * @param name name of the JSON field - * @param[out] jsonp where to store the JSON found under @a name - */ -struct MAJ_Specification -MAJ_spec_json (const char *name, - json_t **jsonp); - - -/** - * Specification for parsing an amount value. - * - * @param name name of the JSON field - * @param amount where to store the amount under @a name - */ -struct MAJ_Specification -MAJ_spec_amount (const char *name, - struct TALER_Amount *amount); - - -/** - * Specification for parsing an RSA public key. - * - * @param name name of the JSON field - * @param pk where to store the RSA key found under @a name - */ -struct MAJ_Specification -MAJ_spec_rsa_public_key (const char *name, - struct GNUNET_CRYPTO_rsa_PublicKey **pk); - - -/** - * Specification for parsing an RSA signature. - * - * @param name name of the JSON field - * @param sig where to store the RSA signature found under @a name - */ -struct MAJ_Specification -MAJ_spec_rsa_signature (const char *name, - struct GNUNET_CRYPTO_rsa_Signature **sig); - - - - -/* end of exchange_api_json.h */ diff --git a/src/exchange-lib/exchange_api_refresh.c b/src/exchange-lib/exchange_api_refresh.c index bcec1354..2949cf1e 100644 --- a/src/exchange-lib/exchange_api_refresh.c +++ b/src/exchange-lib/exchange_api_refresh.c @@ -28,7 +28,6 @@ #include "taler_json_lib.h" #include "taler_exchange_service.h" #include "exchange_api_common.h" -#include "exchange_api_json.h" #include "exchange_api_context.h" #include "exchange_api_handle.h" #include "taler_signatures.h" @@ -1086,17 +1085,18 @@ verify_refresh_melt_signature_ok (struct TALER_EXCHANGE_RefreshMeltHandle *rmh, struct TALER_ExchangeSignatureP exchange_sig; struct TALER_ExchangePublicKeyP exchange_pub; const struct TALER_EXCHANGE_Keys *key_state; - struct MAJ_Specification spec[] = { - MAJ_spec_fixed_auto ("exchange_sig", &exchange_sig), - MAJ_spec_fixed_auto ("exchange_pub", &exchange_pub), - MAJ_spec_uint16 ("noreveal_index", noreveal_index), - MAJ_spec_end + struct GNUNET_JSON_Specification spec[] = { + GNUNET_JSON_spec_fixed_auto ("exchange_sig", &exchange_sig), + GNUNET_JSON_spec_fixed_auto ("exchange_pub", &exchange_pub), + GNUNET_JSON_spec_uint16 ("noreveal_index", noreveal_index), + GNUNET_JSON_spec_end() }; struct TALER_RefreshMeltConfirmationPS confirm; if (GNUNET_OK != - MAJ_parse_json (json, - spec)) + GNUNET_JSON_parse (json, + spec, + NULL, NULL)) { GNUNET_break_op (0); return GNUNET_SYSERR; @@ -1156,19 +1156,20 @@ verify_refresh_melt_signature_forbidden (struct TALER_EXCHANGE_RefreshMeltHandle struct TALER_Amount total; struct TALER_CoinSpendPublicKeyP coin_pub; unsigned int i; - struct MAJ_Specification spec[] = { - MAJ_spec_json ("history", &history), - MAJ_spec_fixed_auto ("coin_pub", &coin_pub), - MAJ_spec_amount ("original_value", &original_value), - MAJ_spec_amount ("requested_value", &melt_value_with_fee), - MAJ_spec_end + struct GNUNET_JSON_Specification spec[] = { + GNUNET_JSON_spec_json ("history", &history), + GNUNET_JSON_spec_fixed_auto ("coin_pub", &coin_pub), + TALER_JSON_spec_amount ("original_value", &original_value), + TALER_JSON_spec_amount ("requested_value", &melt_value_with_fee), + GNUNET_JSON_spec_end() }; const struct MeltedCoin *mc; /* parse JSON reply */ if (GNUNET_OK != - MAJ_parse_json (json, - spec)) + GNUNET_JSON_parse (json, + spec, + NULL, NULL)) { GNUNET_break_op (0); return GNUNET_SYSERR; @@ -1731,14 +1732,15 @@ refresh_reveal_ok (struct TALER_EXCHANGE_RefreshRevealHandle *rrh, { unsigned int i; json_t *jsona; - struct MAJ_Specification spec[] = { - MAJ_spec_json ("ev_sigs", &jsona), - MAJ_spec_end + struct GNUNET_JSON_Specification spec[] = { + GNUNET_JSON_spec_json ("ev_sigs", &jsona), + GNUNET_JSON_spec_end() }; if (GNUNET_OK != - MAJ_parse_json (json, - spec)) + GNUNET_JSON_parse (json, + spec, + NULL, NULL)) { GNUNET_break_op (0); return GNUNET_SYSERR; @@ -1765,9 +1767,9 @@ refresh_reveal_ok (struct TALER_EXCHANGE_RefreshRevealHandle *rrh, struct TALER_CoinSpendPublicKeyP coin_pub; struct GNUNET_HashCode coin_hash; - struct MAJ_Specification spec[] = { - MAJ_spec_rsa_signature ("ev_sig", &blind_sig), - MAJ_spec_end + struct GNUNET_JSON_Specification spec[] = { + GNUNET_JSON_spec_rsa_signature ("ev_sig", &blind_sig), + GNUNET_JSON_spec_end() }; fc = &rrh->md->fresh_coins[rrh->noreveal_index][i]; @@ -1776,8 +1778,9 @@ refresh_reveal_ok (struct TALER_EXCHANGE_RefreshRevealHandle *rrh, GNUNET_assert (NULL != jsonai); if (GNUNET_OK != - MAJ_parse_json (jsonai, - spec)) + GNUNET_JSON_parse (jsonai, + spec, + NULL, NULL)) { GNUNET_break_op (0); return GNUNET_SYSERR; diff --git a/src/exchange-lib/exchange_api_refresh_link.c b/src/exchange-lib/exchange_api_refresh_link.c index 9576916b..41d421f5 100644 --- a/src/exchange-lib/exchange_api_refresh_link.c +++ b/src/exchange-lib/exchange_api_refresh_link.c @@ -21,11 +21,10 @@ */ #include "platform.h" #include -#include #include /* just for HTTP status codes */ #include #include "taler_exchange_service.h" -#include "exchange_api_json.h" +#include "taler_json_lib.h" #include "exchange_api_context.h" #include "exchange_api_handle.h" #include "taler_signatures.h" @@ -101,11 +100,11 @@ parse_refresh_link_coin (const struct TALER_EXCHANGE_RefreshLinkHandle *rlh, size_t link_enc_size; struct GNUNET_CRYPTO_rsa_Signature *bsig; struct GNUNET_CRYPTO_rsa_PublicKey *rpub; - struct MAJ_Specification spec[] = { - MAJ_spec_varsize ("link_enc", &link_enc, &link_enc_size), - MAJ_spec_rsa_public_key ("denom_pub", &rpub), - MAJ_spec_rsa_signature ("ev_sig", &bsig), - MAJ_spec_end + struct GNUNET_JSON_Specification spec[] = { + GNUNET_JSON_spec_varsize ("link_enc", &link_enc, &link_enc_size), + GNUNET_JSON_spec_rsa_public_key ("denom_pub", &rpub), + GNUNET_JSON_spec_rsa_signature ("ev_sig", &bsig), + GNUNET_JSON_spec_end() }; struct TALER_RefreshLinkEncrypted *rle; struct TALER_RefreshLinkDecrypted *rld; @@ -113,8 +112,9 @@ parse_refresh_link_coin (const struct TALER_EXCHANGE_RefreshLinkHandle *rlh, /* parse reply */ if (GNUNET_OK != - MAJ_parse_json (json, - spec)) + GNUNET_JSON_parse (json, + spec, + NULL, NULL)) { GNUNET_break_op (0); return GNUNET_SYSERR; @@ -126,7 +126,7 @@ parse_refresh_link_coin (const struct TALER_EXCHANGE_RefreshLinkHandle *rlh, if (NULL == rle) { GNUNET_break_op (0); - MAJ_parse_free (spec); + GNUNET_JSON_parse_free (spec); return GNUNET_SYSERR; } if (GNUNET_OK != @@ -136,7 +136,7 @@ parse_refresh_link_coin (const struct TALER_EXCHANGE_RefreshLinkHandle *rlh, &secret)) { GNUNET_break_op (0); - MAJ_parse_free (spec); + GNUNET_JSON_parse_free (spec); return GNUNET_SYSERR; } rld = TALER_refresh_decrypt (rle, @@ -144,7 +144,7 @@ parse_refresh_link_coin (const struct TALER_EXCHANGE_RefreshLinkHandle *rlh, if (NULL == rld) { GNUNET_break_op (0); - MAJ_parse_free (spec); + GNUNET_JSON_parse_free (spec); return GNUNET_SYSERR; } @@ -158,7 +158,7 @@ parse_refresh_link_coin (const struct TALER_EXCHANGE_RefreshLinkHandle *rlh, /* clean up */ GNUNET_free (rld); pub->rsa_public_key = GNUNET_CRYPTO_rsa_public_key_dup (rpub); - MAJ_parse_free (spec); + GNUNET_JSON_parse_free (spec); return GNUNET_OK; } @@ -199,15 +199,16 @@ parse_refresh_link_ok (struct TALER_EXCHANGE_RefreshLinkHandle *rlh, for (session=0;session #include #include "taler_exchange_service.h" -#include "exchange_api_json.h" +#include "taler_json_lib.h" #include "exchange_api_context.h" #include "exchange_api_handle.h" #include "taler_signatures.h" @@ -117,19 +117,20 @@ parse_reserve_history (json_t *history, json_t *transaction; struct TALER_Amount amount; const char *type; - struct MAJ_Specification hist_spec[] = { - MAJ_spec_string ("type", &type), - MAJ_spec_amount ("amount", + struct GNUNET_JSON_Specification hist_spec[] = { + GNUNET_JSON_spec_string ("type", &type), + TALER_JSON_spec_amount ("amount", &amount), /* 'wire' and 'signature' are optional depending on 'type'! */ - MAJ_spec_end + GNUNET_JSON_spec_end() }; transaction = json_array_get (history, off); if (GNUNET_OK != - MAJ_parse_json (transaction, - hist_spec)) + GNUNET_JSON_parse (transaction, + hist_spec, + NULL, NULL)) { GNUNET_break_op (0); return GNUNET_SYSERR; @@ -171,19 +172,20 @@ parse_reserve_history (json_t *history, struct TALER_ReserveSignatureP sig; struct TALER_WithdrawRequestPS withdraw_purpose; struct TALER_Amount amount_from_purpose; - struct MAJ_Specification withdraw_spec[] = { - MAJ_spec_fixed_auto ("signature", + struct GNUNET_JSON_Specification withdraw_spec[] = { + GNUNET_JSON_spec_fixed_auto ("signature", &sig), - MAJ_spec_fixed_auto ("details", + GNUNET_JSON_spec_fixed_auto ("details", &withdraw_purpose), - MAJ_spec_end + GNUNET_JSON_spec_end() }; unsigned int i; rhistory[off].type = TALER_EXCHANGE_RTT_WITHDRAWAL; if (GNUNET_OK != - MAJ_parse_json (transaction, - withdraw_spec)) + GNUNET_JSON_parse (transaction, + withdraw_spec, + NULL, NULL)) { GNUNET_break_op (0); return GNUNET_SYSERR; @@ -196,7 +198,7 @@ parse_reserve_history (json_t *history, &reserve_pub->eddsa_pub)) { GNUNET_break_op (0); - MAJ_parse_free (withdraw_spec); + GNUNET_JSON_parse_free (withdraw_spec); return GNUNET_SYSERR; } TALER_amount_ntoh (&amount_from_purpose, @@ -205,7 +207,7 @@ parse_reserve_history (json_t *history, &amount_from_purpose)) { GNUNET_break_op (0); - MAJ_parse_free (withdraw_spec); + GNUNET_JSON_parse_free (withdraw_spec); return GNUNET_SYSERR; } rhistory[off].details.out_authorization_sig = json_object_get (transaction, @@ -225,7 +227,7 @@ parse_reserve_history (json_t *history, sizeof (struct GNUNET_HashCode))) { GNUNET_break_op (0); - MAJ_parse_free (withdraw_spec); + GNUNET_JSON_parse_free (withdraw_spec); return GNUNET_SYSERR; } } @@ -238,7 +240,7 @@ parse_reserve_history (json_t *history, { /* overflow in history already!? inconceivable! Bad exchange! */ GNUNET_break_op (0); - MAJ_parse_free (withdraw_spec); + GNUNET_JSON_parse_free (withdraw_spec); return GNUNET_SYSERR; } /* end type==WITHDRAW */ @@ -296,14 +298,15 @@ handle_reserve_status_finished (void *cls, unsigned int len; struct TALER_Amount balance; struct TALER_Amount balance_from_history; - struct MAJ_Specification spec[] = { - MAJ_spec_amount ("balance", &balance), - MAJ_spec_end + struct GNUNET_JSON_Specification spec[] = { + TALER_JSON_spec_amount ("balance", &balance), + GNUNET_JSON_spec_end() }; if (GNUNET_OK != - MAJ_parse_json (json, - spec)) + GNUNET_JSON_parse (json, + spec, + NULL, NULL)) { GNUNET_break_op (0); response_code = 0; @@ -563,14 +566,15 @@ reserve_withdraw_ok (struct TALER_EXCHANGE_ReserveWithdrawHandle *wsh, struct GNUNET_CRYPTO_rsa_Signature *blind_sig; struct GNUNET_CRYPTO_rsa_Signature *sig; struct TALER_DenominationSignature dsig; - struct MAJ_Specification spec[] = { - MAJ_spec_rsa_signature ("ev_sig", &blind_sig), - MAJ_spec_end + struct GNUNET_JSON_Specification spec[] = { + GNUNET_JSON_spec_rsa_signature ("ev_sig", &blind_sig), + GNUNET_JSON_spec_end() }; if (GNUNET_OK != - MAJ_parse_json (json, - spec)) + GNUNET_JSON_parse (json, + spec, + NULL, NULL)) { GNUNET_break_op (0); return GNUNET_SYSERR; @@ -622,14 +626,15 @@ reserve_withdraw_payment_required (struct TALER_EXCHANGE_ReserveWithdrawHandle * struct TALER_Amount requested_amount; json_t *history; size_t len; - struct MAJ_Specification spec[] = { - MAJ_spec_amount ("balance", &balance), - MAJ_spec_end + struct GNUNET_JSON_Specification spec[] = { + TALER_JSON_spec_amount ("balance", &balance), + GNUNET_JSON_spec_end() }; if (GNUNET_OK != - MAJ_parse_json (json, - spec)) + GNUNET_JSON_parse (json, + spec, + NULL, NULL)) { GNUNET_break_op (0); return GNUNET_SYSERR; diff --git a/src/exchange-lib/exchange_api_wire.c b/src/exchange-lib/exchange_api_wire.c index 35c443ff..27ae1dce 100644 --- a/src/exchange-lib/exchange_api_wire.c +++ b/src/exchange-lib/exchange_api_wire.c @@ -25,9 +25,9 @@ #include /* just for HTTP status codes */ #include #include "taler_exchange_service.h" +#include "taler_json_lib.h" #include "taler_wire_plugin.h" #include "exchange_api_common.h" -#include "exchange_api_json.h" #include "exchange_api_context.h" #include "exchange_api_handle.h" @@ -311,17 +311,18 @@ verify_wire_signature_ok (const struct TALER_EXCHANGE_WireHandle *wh, json_t *methods; const struct TALER_EXCHANGE_Keys *key_state; struct GNUNET_HashContext *hc; - struct MAJ_Specification spec[] = { - MAJ_spec_fixed_auto ("sig", &exchange_sig), - MAJ_spec_fixed_auto ("pub", &exchange_pub), - MAJ_spec_json ("methods", &methods), - MAJ_spec_end + struct GNUNET_JSON_Specification spec[] = { + GNUNET_JSON_spec_fixed_auto ("sig", &exchange_sig), + GNUNET_JSON_spec_fixed_auto ("pub", &exchange_pub), + GNUNET_JSON_spec_json ("methods", &methods), + GNUNET_JSON_spec_end() }; unsigned int i; if (GNUNET_OK != - MAJ_parse_json (json, - spec)) + GNUNET_JSON_parse (json, + spec, + NULL, NULL)) { GNUNET_break_op (0); return NULL; @@ -329,7 +330,7 @@ verify_wire_signature_ok (const struct TALER_EXCHANGE_WireHandle *wh, if (! json_is_array (methods)) { GNUNET_break_op (0); - MAJ_parse_free (spec); + GNUNET_JSON_parse_free (spec); return NULL; } @@ -351,7 +352,7 @@ verify_wire_signature_ok (const struct TALER_EXCHANGE_WireHandle *wh, { GNUNET_CRYPTO_hash_context_abort (hc); GNUNET_break_op (0); - MAJ_parse_free (spec); + GNUNET_JSON_parse_free (spec); return NULL; } method = json_string_value (element); @@ -371,7 +372,7 @@ verify_wire_signature_ok (const struct TALER_EXCHANGE_WireHandle *wh, &exchange_pub.eddsa_pub)) { GNUNET_break_op (0); - MAJ_parse_free (spec); + GNUNET_JSON_parse_free (spec); return NULL; } return methods; diff --git a/src/exchange-lib/exchange_api_wire_deposits.c b/src/exchange-lib/exchange_api_wire_deposits.c index 40625602..0112f856 100644 --- a/src/exchange-lib/exchange_api_wire_deposits.c +++ b/src/exchange-lib/exchange_api_wire_deposits.c @@ -24,10 +24,9 @@ #include #include /* just for HTTP status codes */ #include -#include #include "taler_exchange_service.h" #include "exchange_api_common.h" -#include "exchange_api_json.h" +#include "taler_json_lib.h" #include "exchange_api_context.h" #include "exchange_api_handle.h" #include "taler_signatures.h" @@ -102,17 +101,18 @@ handle_wire_deposits_finished (void *cls, struct TALER_Amount total_amount; struct TALER_MerchantPublicKeyP merchant_pub; unsigned int num_details; - struct MAJ_Specification spec[] = { - MAJ_spec_fixed_auto ("H_wire", &h_wire), - MAJ_spec_fixed_auto ("merchant_pub", &merchant_pub), - MAJ_spec_amount ("total_amount", &total_amount), - MAJ_spec_json ("details", &details_j), - MAJ_spec_end + struct GNUNET_JSON_Specification spec[] = { + GNUNET_JSON_spec_fixed_auto ("H_wire", &h_wire), + GNUNET_JSON_spec_fixed_auto ("merchant_pub", &merchant_pub), + TALER_JSON_spec_amount ("total_amount", &total_amount), + GNUNET_JSON_spec_json ("details", &details_j), + GNUNET_JSON_spec_end() }; if (GNUNET_OK != - MAJ_parse_json (json, - spec)) + GNUNET_JSON_parse (json, + spec, + NULL, NULL)) { GNUNET_break_op (0); response_code = 0; @@ -127,18 +127,19 @@ handle_wire_deposits_finished (void *cls, { struct TALER_WireDepositDetails *detail = &details[i]; struct json_t *detail_j = json_array_get (details_j, i); - struct MAJ_Specification spec_detail[] = { - MAJ_spec_fixed_auto ("H_contract", &detail->h_contract), - MAJ_spec_amount ("deposit_value", &detail->coin_value), - MAJ_spec_amount ("deposit_fee", &detail->coin_fee), - MAJ_spec_uint64 ("transaction_id", &detail->transaction_id), - MAJ_spec_fixed_auto ("coin_pub", &detail->coin_pub), - MAJ_spec_end + struct GNUNET_JSON_Specification spec_detail[] = { + GNUNET_JSON_spec_fixed_auto ("H_contract", &detail->h_contract), + TALER_JSON_spec_amount ("deposit_value", &detail->coin_value), + TALER_JSON_spec_amount ("deposit_fee", &detail->coin_fee), + GNUNET_JSON_spec_uint64 ("transaction_id", &detail->transaction_id), + GNUNET_JSON_spec_fixed_auto ("coin_pub", &detail->coin_pub), + GNUNET_JSON_spec_end() }; if (GNUNET_OK != - MAJ_parse_json (detail_j, - spec_detail)) + GNUNET_JSON_parse (detail_j, + spec_detail, + NULL, NULL)) { GNUNET_break_op (0); response_code = 0; -- cgit v1.2.3 From cb987575c17bd7c570d83b516dae86b4eabfe0d2 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Mon, 11 Apr 2016 20:16:58 +0200 Subject: check exchange signature on /wire/deposit response (#4135) --- src/exchange-lib/exchange_api_common.c | 7 ++-- src/exchange-lib/exchange_api_common.h | 6 ++-- src/exchange-lib/exchange_api_context.c | 10 +++--- src/exchange-lib/exchange_api_wire_deposits.c | 49 ++++++++++++++++++++++++--- 4 files changed, 55 insertions(+), 17 deletions(-) (limited to 'src/exchange-lib/exchange_api_common.c') diff --git a/src/exchange-lib/exchange_api_common.c b/src/exchange-lib/exchange_api_common.c index 6d2408d9..aacbb4a2 100644 --- a/src/exchange-lib/exchange_api_common.c +++ b/src/exchange-lib/exchange_api_common.c @@ -38,9 +38,9 @@ */ int TALER_EXCHANGE_verify_coin_history_ (const char *currency, - const struct TALER_CoinSpendPublicKeyP *coin_pub, - json_t *history, - struct TALER_Amount *total) + const struct TALER_CoinSpendPublicKeyP *coin_pub, + json_t *history, + struct TALER_Amount *total) { size_t len; size_t off; @@ -119,7 +119,6 @@ TALER_EXCHANGE_verify_coin_history_ (const char *currency, return GNUNET_SYSERR; } - // FIXME: check sig! TALER_amount_ntoh (&dr_amount, &dr->amount_with_fee); if (0 != TALER_amount_cmp (&dr_amount, diff --git a/src/exchange-lib/exchange_api_common.h b/src/exchange-lib/exchange_api_common.h index 49f486b0..2a521ceb 100644 --- a/src/exchange-lib/exchange_api_common.h +++ b/src/exchange-lib/exchange_api_common.h @@ -34,8 +34,8 @@ */ int TALER_EXCHANGE_verify_coin_history_ (const char *currency, - const struct TALER_CoinSpendPublicKeyP *coin_pub, - json_t *history, - struct TALER_Amount *total); + const struct TALER_CoinSpendPublicKeyP *coin_pub, + json_t *history, + struct TALER_Amount *total); /* end of exchange_api_common.h */ diff --git a/src/exchange-lib/exchange_api_context.c b/src/exchange-lib/exchange_api_context.c index 54471900..8d6746a1 100644 --- a/src/exchange-lib/exchange_api_context.c +++ b/src/exchange-lib/exchange_api_context.c @@ -327,11 +327,11 @@ TALER_EXCHANGE_perform (struct TALER_EXCHANGE_Context *ctx) */ void TALER_EXCHANGE_get_select_info (struct TALER_EXCHANGE_Context *ctx, - fd_set *read_fd_set, - fd_set *write_fd_set, - fd_set *except_fd_set, - int *max_fd, - long *timeout) + fd_set *read_fd_set, + fd_set *write_fd_set, + fd_set *except_fd_set, + int *max_fd, + long *timeout) { long to; int m; diff --git a/src/exchange-lib/exchange_api_wire_deposits.c b/src/exchange-lib/exchange_api_wire_deposits.c index 5b661920..49a50f64 100644 --- a/src/exchange-lib/exchange_api_wire_deposits.c +++ b/src/exchange-lib/exchange_api_wire_deposits.c @@ -101,12 +101,12 @@ handle_wire_deposits_finished (void *cls, struct TALER_Amount total_amount; struct TALER_MerchantPublicKeyP merchant_pub; unsigned int num_details; - struct TALER_ExchangePublicKeyP pub; - struct TALER_ExchangeSignatureP sig; + struct TALER_ExchangePublicKeyP exchange_pub; + struct TALER_ExchangeSignatureP exchange_sig; struct GNUNET_JSON_Specification spec[] = { GNUNET_JSON_spec_fixed_auto ("H_wire", &h_wire), - GNUNET_JSON_spec_fixed_auto ("exchange_pub", &pub), - GNUNET_JSON_spec_fixed_auto ("exchange_sig", &sig), + GNUNET_JSON_spec_fixed_auto ("exchange_pub", &exchange_pub), + GNUNET_JSON_spec_fixed_auto ("exchange_sig", &exchange_sig), GNUNET_JSON_spec_fixed_auto ("merchant_pub", &merchant_pub), TALER_JSON_spec_amount ("total_amount", &total_amount), GNUNET_JSON_spec_json ("details", &details_j), @@ -126,7 +126,11 @@ handle_wire_deposits_finished (void *cls, { struct TALER_WireDepositDetails details[num_details]; unsigned int i; + struct GNUNET_HashContext *hash_context; + struct TALER_WireDepositDetailP dd; + struct TALER_WireDepositDataPS wdp; + hash_context = GNUNET_CRYPTO_hash_context_start (); for (i=0;ih_contract; + dd.transaction_id = GNUNET_htonll (detail->transaction_id); + dd.coin_pub = detail->coin_pub; + TALER_amount_hton (&dd.deposit_value, + &detail->coin_value); + TALER_amount_hton (&dd.deposit_fee, + &detail->coin_fee); + GNUNET_CRYPTO_hash_context_read (hash_context, + &dd, + sizeof (struct TALER_WireDepositDetailP)); + } + /* Check signature */ + wdp.purpose.purpose = htonl (TALER_SIGNATURE_EXCHANGE_CONFIRM_WIRE_DEPOSIT); + wdp.purpose.size = htonl (sizeof (struct TALER_WireDepositDataPS)); + TALER_amount_hton (&wdp.total, + &total_amount); + wdp.merchant_pub = merchant_pub; + wdp.h_wire = h_wire; + GNUNET_CRYPTO_hash_context_finish (hash_context, + &wdp.h_details); + if ( (0 == response_code /* avoid crypto if things are already wrong */) && + (GNUNET_OK != + TALER_EXCHANGE_test_signing_key (TALER_EXCHANGE_get_keys (wdh->exchange), + &exchange_pub)) ) + { + GNUNET_break_op (0); + response_code = 0; + } + if ( (0 == response_code /* avoid crypto if things are already wrong */) && + (GNUNET_OK != + TALER_EXCHANGE_test_signing_key (TALER_EXCHANGE_get_keys (wdh->exchange), + &exchange_pub)) ) + { + GNUNET_break_op (0); + response_code = 0; } if (0 == response_code) break; - /* FIXME: check signature (#4135) */ wdh->cb (wdh->cb_cls, response_code, json, -- cgit v1.2.3