aboutsummaryrefslogtreecommitdiff
path: root/src/wire
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2016-11-18 18:29:18 +0100
committerChristian Grothoff <christian@grothoff.org>2016-11-18 18:29:18 +0100
commit7d6b8d53d5a6ee6ca1545fb5d458199c6249edc5 (patch)
treef44f7afb792184ef46e6d4882cb20de0e2d8b519 /src/wire
parentde68a7b301fd78a89c4f5e6f34791c8debab36e0 (diff)
addressing #4803: nicer error messages for invalid wire formats
Diffstat (limited to 'src/wire')
-rw-r--r--src/wire/plugin_wire_sepa.c70
-rw-r--r--src/wire/plugin_wire_template.c14
-rw-r--r--src/wire/plugin_wire_test.c83
-rw-r--r--src/wire/test_sepa_wireformat.c30
-rw-r--r--src/wire/test_wire_plugin.c13
5 files changed, 134 insertions, 76 deletions
diff --git a/src/wire/plugin_wire_sepa.c b/src/wire/plugin_wire_sepa.c
index 61b30259a..cc00bcad7 100644
--- a/src/wire/plugin_wire_sepa.c
+++ b/src/wire/plugin_wire_sepa.c
@@ -451,12 +451,15 @@ verify_wire_sepa_signature_ok (const json_t *json,
* @param cls the @e cls of this struct with the plugin-specific state
* @param wire the JSON wire format object
* @param master_pub public key of the exchange to verify against
- * @return #GNUNET_YES if correctly formatted; #GNUNET_NO if not
+ * @param[OUT] emsg set to an error message, unless we return #TALER_EC_NONE;
+ * error message must be freed by the caller using GNUNET_free()
+ * @return #TALER_EC_NONE if correctly formatted
*/
-static int
+static enum TALER_ErrorCode
sepa_wire_validate (void *cls,
const json_t *wire,
- const struct TALER_MasterPublicKeyP *master_pub)
+ const struct TALER_MasterPublicKeyP *master_pub,
+ char **emsg)
{
json_error_t error;
const char *type;
@@ -464,6 +467,7 @@ sepa_wire_validate (void *cls,
const char *name;
const char *bic;
+ *emsg = NULL;
if (0 != json_unpack_ex
((json_t *) wire,
&error, 0,
@@ -478,39 +482,44 @@ sepa_wire_validate (void *cls,
"name", &name,
"bic", &bic))
{
- GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
- "JSON parsing failed at %s:%u: %s (%s)\n",
- __FILE__, __LINE__,
- error.text, error.source);
- json_dumpf (wire, stderr, 0);
- fprintf (stderr, "\n");
- return GNUNET_SYSERR;
+ char *dump;
+
+ dump = json_dumps (wire, 0);
+ GNUNET_asprintf (emsg,
+ "JSON parsing failed at %s:%u: %s (%s): %s\n",
+ __FILE__, __LINE__,
+ error.text,
+ error.source,
+ dump);
+ free (dump);
+ return TALER_EC_DEPOSIT_INVALID_WIRE_FORMAT_JSON;
}
if (0 != strcasecmp (type,
"sepa"))
{
- GNUNET_log (GNUNET_ERROR_TYPE_INFO,
- "Transfer type `%s' invalid\n",
- type);
- return GNUNET_SYSERR;
+ GNUNET_asprintf (emsg,
+ "Transfer type `%s' invalid for SEPA wire plugin\n",
+ type);
+ return TALER_EC_DEPOSIT_INVALID_WIRE_FORMAT_TYPE;
}
if (1 != validate_iban (iban))
{
- GNUNET_log (GNUNET_ERROR_TYPE_INFO,
- "IBAN `%s' invalid\n",
- iban);
- return GNUNET_NO;
+ GNUNET_asprintf (emsg,
+ "IBAN `%s' invalid\n",
+ iban);
+ return TALER_EC_DEPOSIT_INVALID_WIRE_FORMAT_ACCOUNT_NUMBER;
}
/* FIXME: don't parse again, integrate properly... */
if (GNUNET_OK !=
verify_wire_sepa_signature_ok (wire,
master_pub))
{
- GNUNET_log (GNUNET_ERROR_TYPE_INFO,
- "Signature invalid\n");
- return GNUNET_NO;
+ GNUNET_asprintf (emsg,
+ "Signature using public key `%s' invalid\n",
+ TALER_B2S (master_pub));
+ return TALER_EC_DEPOSIT_INVALID_WIRE_FORMAT_SIGNATURE;
}
- return GNUNET_YES;
+ return TALER_EC_NONE;
}
@@ -531,6 +540,7 @@ sepa_get_wire_details (void *cls,
char *sepa_wire_file;
json_error_t err;
json_t *ret;
+ char *emsg;
/* Fetch reply */
if (GNUNET_OK !=
@@ -558,13 +568,17 @@ sepa_get_wire_details (void *cls,
GNUNET_free (sepa_wire_file);
return NULL;
}
- if (GNUNET_YES != sepa_wire_validate (cls,
- ret,
- NULL))
- {
+ if (TALER_EC_NONE !=
+ sepa_wire_validate (cls,
+ ret,
+ NULL,
+ &emsg))
+ {
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "Failed to validate SEPA data in %s\n",
- sepa_wire_file);
+ "Failed to validate SEPA data in %s: %s\n",
+ sepa_wire_file,
+ emsg);
+ GNUNET_free (emsg);
GNUNET_free (sepa_wire_file);
json_decref (ret);
return NULL;
diff --git a/src/wire/plugin_wire_template.c b/src/wire/plugin_wire_template.c
index 2bc2a2abf..416eb9c58 100644
--- a/src/wire/plugin_wire_template.c
+++ b/src/wire/plugin_wire_template.c
@@ -98,15 +98,19 @@ template_get_wire_details (void *cls,
* @param cls the @e cls of this struct with the plugin-specific state
* @param wire the JSON wire format object
* @param master_pub public key of the exchange to verify against
- * @return #GNUNET_YES if correctly formatted; #GNUNET_NO if not
+ * @param[OUT] emsg set to an error message, unless we return #TALER_EC_NONE;
+ * error message must be freed by the caller using GNUNET_free()
+ * @return #TALER_EC_NONE if correctly formatted
*/
-static int
+static enum TALER_ErrorCode
template_wire_validate (void *cls,
const json_t *wire,
- const struct TALER_MasterPublicKeyP *master_pub)
+ const struct TALER_MasterPublicKeyP *master_pub,
+ char **emsg)
{
- GNUNET_break (0);
- return GNUNET_SYSERR;
+ GNUNET_asprintf (emsg,
+ "Not implemented");
+ return TALER_EC_NOT_IMPLEMENTED;
}
diff --git a/src/wire/plugin_wire_test.c b/src/wire/plugin_wire_test.c
index bdcf78058..7b52dee40 100644
--- a/src/wire/plugin_wire_test.c
+++ b/src/wire/plugin_wire_test.c
@@ -213,12 +213,15 @@ compute_purpose (uint64_t account,
* @param cls the @e cls of this struct with the plugin-specific state
* @param wire the JSON wire format object
* @param master_pub public key of the exchange to verify against
- * @return #GNUNET_YES if correctly formatted; #GNUNET_NO if not
+ * @param[OUT] emsg set to an error message, unless we return #TALER_EC_NONE;
+ * error message must be freed by the caller using GNUNET_free()
+ * @return #TALER_EC_NONE if correctly formatted
*/
-static int
+static enum TALER_ErrorCode
test_wire_validate (void *cls,
const json_t *wire,
- const struct TALER_MasterPublicKeyP *master_pub)
+ const struct TALER_MasterPublicKeyP *master_pub,
+ char **emsg)
{
struct TestClosure *tc = cls;
json_error_t error;
@@ -228,6 +231,7 @@ test_wire_validate (void *cls,
struct TALER_MasterWireDetailsPS wsd;
struct TALER_MasterSignatureP sig;
+ *emsg = NULL;
if (0 !=
json_unpack_ex ((json_t *) wire,
&error,
@@ -236,30 +240,41 @@ test_wire_validate (void *cls,
"account_number", &account_no,
"bank_uri", &bank_uri))
{
- GNUNET_break_op (0);
- return GNUNET_SYSERR;
+ char *dump;
+
+ dump = json_dumps (wire, 0);
+ GNUNET_asprintf (emsg,
+ "JSON parsing failed at %s:%u: %s (%s): %s\n",
+ __FILE__, __LINE__,
+ error.text,
+ error.source,
+ dump);
+ free (dump);
+ return TALER_EC_DEPOSIT_INVALID_WIRE_FORMAT_JSON;
}
if ( (account_no < 0) ||
(account_no > (1LL << 53)) )
{
- GNUNET_break (0);
- return GNUNET_SYSERR;
+ GNUNET_asprintf (emsg,
+ "Account number %llu outside of permitted range\n",
+ account_no);
+ return TALER_EC_DEPOSIT_INVALID_WIRE_FORMAT_ACCOUNT_NUMBER;
}
if ( (NULL != tc->bank_uri) &&
(0 != strcmp (bank_uri,
tc->bank_uri)) )
{
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "Wire specifies bank URI %s, but this exchange only supports %s\n",
- bank_uri,
- tc->bank_uri);
- return GNUNET_NO;
+ GNUNET_asprintf (emsg,
+ "Wire specifies bank URI `%s', but this exchange only supports `%s'\n",
+ bank_uri,
+ tc->bank_uri);
+ return TALER_EC_DEPOSIT_INVALID_WIRE_FORMAT_BANK;
}
if (NULL == master_pub)
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Skipping signature check as master public key not given\n");
- return GNUNET_OK;
+ return TALER_EC_NONE;
}
if (0 !=
json_unpack_ex ((json_t *) wire,
@@ -268,9 +283,9 @@ test_wire_validate (void *cls,
"{s:s}",
"sig", &sig_s))
{
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "Signature check required, but signature is missing\n");
- return GNUNET_NO;
+ GNUNET_asprintf (emsg,
+ "Signature check required, but signature is missing\n");
+ return TALER_EC_DEPOSIT_INVALID_WIRE_FORMAT_SIGNATURE;
}
compute_purpose (account_no,
bank_uri,
@@ -290,10 +305,12 @@ test_wire_validate (void *cls,
&sig.eddsa_signature,
&master_pub->eddsa_pub))
{
- GNUNET_break (0);
- return GNUNET_SYSERR;
+ GNUNET_asprintf (emsg,
+ "Signature using public key `%s' invalid\n",
+ TALER_B2S (master_pub));
+ return TALER_EC_DEPOSIT_INVALID_WIRE_FORMAT_SIGNATURE;
}
- return GNUNET_YES;
+ return TALER_EC_NONE;
}
@@ -315,6 +332,7 @@ test_get_wire_details (void *cls,
char *test_wire_file;
json_error_t err;
json_t *ret;
+ char *emsg;
/* Fetch reply */
if (GNUNET_OK !=
@@ -342,13 +360,17 @@ test_get_wire_details (void *cls,
GNUNET_free (test_wire_file);
return NULL;
}
- if (GNUNET_YES != test_wire_validate (tc,
- ret,
- NULL))
+ if (TALER_EC_NONE !=
+ test_wire_validate (tc,
+ ret,
+ NULL,
+ &emsg))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "Failed to validate TEST wire data in %s\n",
- test_wire_file);
+ "Failed to validate TEST wire data in %s: %s\n",
+ test_wire_file,
+ emsg);
+ GNUNET_free (emsg);
GNUNET_free (test_wire_file);
json_decref (ret);
return NULL;
@@ -478,13 +500,16 @@ test_prepare_wire_transfer (void *cls,
{
struct TestClosure *tc = cls;
struct TALER_WIRE_PrepareHandle *pth;
+ char *emsg;
- if (GNUNET_YES !=
+ if (TALER_EC_NONE !=
test_wire_validate (tc,
wire,
- NULL))
+ NULL,
+ &emsg))
{
GNUNET_break_op (0);
+ GNUNET_free (emsg);
return NULL;
}
pth = GNUNET_new (struct TALER_WIRE_PrepareHandle);
@@ -624,6 +649,7 @@ test_execute_wire_transfer (void *cls,
struct TALER_Amount amount;
json_int_t account_no;
struct BufFormatP bf;
+ char *emsg;
if (NULL == tc->ctx)
{
@@ -650,10 +676,11 @@ test_execute_wire_transfer (void *cls,
GNUNET_break (0);
return NULL;
}
- GNUNET_assert (GNUNET_YES ==
+ GNUNET_assert (TALER_EC_NONE ==
test_wire_validate (tc,
wire,
- NULL));
+ NULL,
+ &emsg));
if (0 !=
json_unpack_ex (wire,
&error,
diff --git a/src/wire/test_sepa_wireformat.c b/src/wire/test_sepa_wireformat.c
index 43bd51a32..07a3784c6 100644
--- a/src/wire/test_sepa_wireformat.c
+++ b/src/wire/test_sepa_wireformat.c
@@ -67,6 +67,7 @@ main(int argc,
const char *const argv[])
{
json_t *wire;
+ char *emsg;
json_error_t error;
int ret;
struct GNUNET_CONFIGURATION_Handle *cfg;
@@ -85,28 +86,35 @@ main(int argc,
GNUNET_assert (NULL != plugin);
(void) memset(&error, 0, sizeof(error));
GNUNET_assert (NULL != (wire = json_loads (unsupported_wire_str, 0, NULL)));
- GNUNET_assert (GNUNET_YES != plugin->wire_validate (NULL,
- wire,
- NULL));
+ GNUNET_assert (TALER_EC_NONE != plugin->wire_validate (NULL,
+ wire,
+ NULL,
+ &emsg));
+ GNUNET_free (emsg);
json_decref (wire);
GNUNET_assert (NULL != (wire = json_loads (invalid_wire_str, 0, NULL)));
- GNUNET_assert (GNUNET_NO == plugin->wire_validate (NULL,
- wire,
- NULL));
+ GNUNET_assert (TALER_EC_NONE != plugin->wire_validate (NULL,
+ wire,
+ NULL,
+ &emsg));
+ GNUNET_free (emsg);
json_decref (wire);
GNUNET_assert (NULL != (wire = json_loads (invalid_wire_str2, 0, NULL)));
- GNUNET_assert (GNUNET_NO == plugin->wire_validate (NULL,
- wire,
- NULL));
+ GNUNET_assert (TALER_EC_NONE != plugin->wire_validate (NULL,
+ wire,
+ NULL,
+ &emsg));
+ GNUNET_free (emsg);
json_decref (wire);
GNUNET_assert (NULL != (wire = json_loads (valid_wire_str, 0, &error)));
ret = plugin->wire_validate (NULL,
wire,
- NULL);
+ NULL,
+ &emsg);
json_decref (wire);
TALER_WIRE_plugin_unload (plugin);
GNUNET_CONFIGURATION_destroy (cfg);
- if (GNUNET_NO == ret)
+ if (TALER_EC_NONE != ret)
return 1;
return 0;
}
diff --git a/src/wire/test_wire_plugin.c b/src/wire/test_wire_plugin.c
index c465302f1..0828a06b7 100644
--- a/src/wire/test_wire_plugin.c
+++ b/src/wire/test_wire_plugin.c
@@ -112,6 +112,7 @@ run_test (const struct TestBlock *test,
json_t *lwire;
struct TALER_Amount in;
struct TALER_Amount expect;
+ char *emsg;
GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_NONCE,
&salt,
@@ -134,12 +135,14 @@ run_test (const struct TestBlock *test,
"sig",
GNUNET_JSON_from_data (&sig,
sizeof (sig)));
- if (GNUNET_OK !=
+ if (TALER_EC_NONE !=
plugin->wire_validate (plugin->cls,
wire,
- &pub_key))
+ &pub_key,
+ &emsg))
{
GNUNET_break (0);
+ GNUNET_free (emsg);
return GNUNET_SYSERR;
}
/* load wire details from file */
@@ -151,12 +154,14 @@ run_test (const struct TestBlock *test,
GNUNET_break (0);
return GNUNET_SYSERR;
}
- if (GNUNET_OK !=
+ if (TALER_EC_NONE !=
plugin->wire_validate (plugin->cls,
lwire,
- &pub_key))
+ &pub_key,
+ &emsg))
{
GNUNET_break (0);
+ GNUNET_free (emsg);
json_decref (lwire);
return GNUNET_SYSERR;
}