diff options
author | Christian Grothoff <christian@grothoff.org> | 2024-08-24 23:36:59 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2024-08-24 23:36:59 +0200 |
commit | f7d302f8ccfdf8d41bb05fa5b542b94f04cdc8c2 (patch) | |
tree | 299c74a955901b9c5f1ae0dbd0cb184be8291ee1 | |
parent | a80789646ed9610048f3ad71e11f90eded1e52a9 (diff) |
fix NULL deref bug
-rw-r--r-- | src/backend/taler-merchant-httpd_contract.c | 139 | ||||
-rw-r--r-- | src/backend/taler-merchant-httpd_contract.h | 19 |
2 files changed, 88 insertions, 70 deletions
diff --git a/src/backend/taler-merchant-httpd_contract.c b/src/backend/taler-merchant-httpd_contract.c index e7c7ac49..0783a30e 100644 --- a/src/backend/taler-merchant-httpd_contract.c +++ b/src/backend/taler-merchant-httpd_contract.c @@ -24,11 +24,12 @@ #include <stdint.h> #include "taler-merchant-httpd_contract.h" + enum TALER_MerchantContractInputType TMH_contract_input_type_from_string (const char *str) { /* For now, only 'token' is the only supported option. */ - if (0 == strcmp("token", str)) + if (0 == strcmp ("token", str)) { return TALER_MCIT_TOKEN; } @@ -36,22 +37,24 @@ TMH_contract_input_type_from_string (const char *str) return TALER_MCIT_INVALID; } + enum TALER_MerchantContractOutputType TMH_contract_output_type_from_string (const char *str) { /* For now, only 'token' is the only supported option. */ - if (0 == strcmp("token", str)) + if (0 == strcmp ("token", str)) { return TALER_MCOT_TOKEN; } - return TALER_MCOT_INVALID; } + const char * TMH_string_from_contract_input_type (enum TALER_MerchantContractInputType t) { - switch (t) { + switch (t) + { case TALER_MCIT_TOKEN: return "token"; case TALER_MCIT_COIN: @@ -61,10 +64,12 @@ TMH_string_from_contract_input_type (enum TALER_MerchantContractInputType t) } } + const char * TMH_string_from_contract_output_type (enum TALER_MerchantContractOutputType t) { - switch (t) { + switch (t) + { case TALER_MCOT_TOKEN: return "token"; case TALER_MCOT_COIN: @@ -76,6 +81,7 @@ TMH_string_from_contract_output_type (enum TALER_MerchantContractOutputType t) } } + /** * Parse given JSON object to choices array. * @@ -86,12 +92,13 @@ TMH_string_from_contract_output_type (enum TALER_MerchantContractOutputType t) */ static enum GNUNET_GenericReturnValue parse_choices (void *cls, - json_t *root, - struct GNUNET_JSON_Specification *spec) + json_t *root, + struct GNUNET_JSON_Specification *spec) { struct TALER_MerchantContractChoice **choices = spec->ptr; unsigned int *choices_len = cls; - if (!json_is_array (root)) + + if (! json_is_array (root)) { GNUNET_break_op (0); return GNUNET_SYSERR; @@ -118,9 +125,9 @@ parse_choices (void *cls, if (GNUNET_OK != GNUNET_JSON_parse (json_array_get (root, i), - spec, - &error_name, - &error_line)) + spec, + &error_name, + &error_line)) { GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to parse %s at %u: %s\n", @@ -140,25 +147,25 @@ parse_choices (void *cls, const char *kind; struct GNUNET_JSON_Specification ispec[] = { GNUNET_JSON_spec_string ("kind", - &kind), + &kind), GNUNET_JSON_spec_string ("token_family_slug", - &input.details.token.token_family_slug), + &input.details.token.token_family_slug), GNUNET_JSON_spec_timestamp ("valid_after", &input.details.token.valid_after), GNUNET_JSON_spec_mark_optional ( GNUNET_JSON_spec_uint32 ("count", - &input.details.token.count), + &input.details.token.count), NULL), - GNUNET_JSON_spec_end() + GNUNET_JSON_spec_end () }; const char *ierror_name; unsigned int ierror_line; if (GNUNET_OK != GNUNET_JSON_parse (jinput, - ispec, - &ierror_name, - &ierror_line)) + ispec, + &ierror_name, + &ierror_line)) { GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to parse %s at %u: %s\n", @@ -197,29 +204,31 @@ parse_choices (void *cls, size_t idx; json_array_foreach ((json_t *) joutputs, idx, joutput) { - struct TALER_MerchantContractOutput output = {.details.token.count = 1}; + struct TALER_MerchantContractOutput output = { + .details.token.count = 1 + }; const char *kind; struct GNUNET_JSON_Specification ispec[] = { GNUNET_JSON_spec_string ("kind", - &kind), + &kind), GNUNET_JSON_spec_string ("token_family_slug", - &output.details.token.token_family_slug), + &output.details.token.token_family_slug), GNUNET_JSON_spec_timestamp ("valid_after", &output.details.token.valid_after), GNUNET_JSON_spec_mark_optional ( GNUNET_JSON_spec_uint32 ("count", - &output.details.token.count), + &output.details.token.count), NULL), - GNUNET_JSON_spec_end() + GNUNET_JSON_spec_end () }; const char *ierror_name; unsigned int ierror_line; if (GNUNET_OK != GNUNET_JSON_parse (joutput, - ispec, - &ierror_name, - &ierror_line)) + ispec, + &ierror_name, + &ierror_line)) { GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to parse %s at %u: %s\n", @@ -253,7 +262,6 @@ parse_choices (void *cls, } } } - return GNUNET_OK; } @@ -292,13 +300,13 @@ parse_token_families (void *cls, unsigned int *families_len = cls; json_t *jfamily; const char *slug; - if (!json_is_object (root)) + + if (! json_is_object (root)) { GNUNET_break_op (0); return GNUNET_SYSERR; } - - json_object_foreach(root, slug, jfamily) + json_object_foreach (root, slug, jfamily) { const json_t *keys; struct TALER_MerchantContractTokenFamily family = { @@ -307,9 +315,9 @@ parse_token_families (void *cls, struct GNUNET_JSON_Specification spec[] = { GNUNET_JSON_spec_array_const ("keys", - &keys), + &keys), GNUNET_JSON_spec_bool ("critical", - &family.critical), + &family.critical), GNUNET_JSON_spec_end () /* TODO: Figure out if these fields should be 'const' */ // GNUNET_JSON_spec_string ("description", @@ -322,9 +330,9 @@ parse_token_families (void *cls, if (GNUNET_OK != GNUNET_JSON_parse (jfamily, - spec, - &error_name, - &error_line)) + spec, + &error_name, + &error_line)) { GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to parse %s at %u: %s\n", @@ -346,12 +354,14 @@ parse_token_families (void *cls, struct TALER_MerchantContractTokenFamilyKey *key; key = &family.keys[i]; /* TODO: Free when not used anymore */ - key->pub.public_key = GNUNET_new (struct GNUNET_CRYPTO_BlindSignPublicKey); + key->pub.public_key = GNUNET_new (struct GNUNET_CRYPTO_BlindSignPublicKey) + ; struct GNUNET_JSON_Specification key_spec[] = { GNUNET_JSON_spec_fixed_auto ("h_pub", - &key->pub.public_key->pub_key_hash), + &key->pub.public_key->pub_key_hash), GNUNET_JSON_spec_rsa_public_key ("rsa_pub", - &key->pub.public_key->details.rsa_public_key), + &key->pub.public_key->details. + rsa_public_key), // GNUNET_JSON_spec_fixed_auto ("cs_pub", // &key.pub.public_key->details.cs_public_key)), GNUNET_JSON_spec_int64 ("cipher", @@ -360,13 +370,14 @@ parse_token_families (void *cls, &key->valid_after), GNUNET_JSON_spec_timestamp ("valid_before", &key->valid_before), - GNUNET_JSON_spec_end() + GNUNET_JSON_spec_end () }; const char *ierror_name; unsigned int ierror_line; if (GNUNET_OK != - GNUNET_JSON_parse (json_array_get (keys, i), + GNUNET_JSON_parse (json_array_get (keys, + i), key_spec, &ierror_name, &ierror_line)) @@ -380,7 +391,8 @@ parse_token_families (void *cls, return GNUNET_SYSERR; } - switch (cipher) { + switch (cipher) + { case GNUNET_CRYPTO_BSA_RSA: key->pub.public_key->cipher = GNUNET_CRYPTO_BSA_RSA; break; @@ -405,7 +417,8 @@ parse_token_families (void *cls, struct GNUNET_JSON_Specification TALER_JSON_spec_token_families (const char *name, - struct TALER_MerchantContractTokenFamily **families, + struct TALER_MerchantContractTokenFamily ** + families, unsigned int *families_len) { struct GNUNET_JSON_Specification ret = { @@ -418,34 +431,36 @@ TALER_JSON_spec_token_families (const char *name, return ret; } + enum GNUNET_GenericReturnValue -TMH_find_token_family_key (const char *slug, - struct GNUNET_TIME_Timestamp valid_after, - struct TALER_MerchantContractTokenFamily *families, - unsigned int families_len, - struct TALER_MerchantContractTokenFamily *family, - struct TALER_MerchantContractTokenFamilyKey *key) +TMH_find_token_family_key ( + const char *slug, + struct GNUNET_TIME_Timestamp valid_after, + const struct TALER_MerchantContractTokenFamily *families, + unsigned int families_len, + struct TALER_MerchantContractTokenFamily *family, + struct TALER_MerchantContractTokenFamilyKey *key) { for (unsigned int i = 0; i < families_len; i++) { - if (0 != strcmp (families[i].slug, slug)) - { + const struct TALER_MerchantContractTokenFamily *fami + = &families[i]; + + if (0 != strcmp (fami->slug, + slug)) continue; - } if (NULL != family) + *family = *fami; + for (unsigned int k = 0; k < fami->keys_len; k++) { - *family = families[i]; - } - for (unsigned int k = 0; k < family->keys_len; k++) - { - if (GNUNET_TIME_timestamp_cmp(family->keys[k].valid_after, - ==, - valid_after)) + struct TALER_MerchantContractTokenFamilyKey *ki = &fami->keys[k]; + + if (GNUNET_TIME_timestamp_cmp (ki->valid_after, + ==, + valid_after)) { if (NULL != key) - { - *key = family->keys[k]; - } + *key = *ki; return GNUNET_OK; } } @@ -455,4 +470,4 @@ TMH_find_token_family_key (const char *slug, /* no matching family found */ return GNUNET_NO; -}
\ No newline at end of file +} diff --git a/src/backend/taler-merchant-httpd_contract.h b/src/backend/taler-merchant-httpd_contract.h index 6389cc70..7d29da2e 100644 --- a/src/backend/taler-merchant-httpd_contract.h +++ b/src/backend/taler-merchant-httpd_contract.h @@ -176,7 +176,8 @@ struct TALER_MerchantContractOutput /** * Coin-based output. */ - struct { + struct + { /** * Coins that will be yielded. This excludes any applicable withdraw fees. */ @@ -613,7 +614,8 @@ TALER_JSON_spec_choices (const char *name, */ struct GNUNET_JSON_Specification TALER_JSON_spec_token_families (const char *name, - struct TALER_MerchantContractTokenFamily **families, + struct TALER_MerchantContractTokenFamily ** + families, unsigned int *families_len); @@ -630,9 +632,10 @@ TALER_JSON_spec_token_families (const char *name, * @return #GNUNET_OK on success #GNUNET_NO if no key was found */ enum GNUNET_GenericReturnValue -TMH_find_token_family_key (const char *slug, - struct GNUNET_TIME_Timestamp valid_after, - struct TALER_MerchantContractTokenFamily *families, - unsigned int families_len, - struct TALER_MerchantContractTokenFamily *family, - struct TALER_MerchantContractTokenFamilyKey *key);
\ No newline at end of file +TMH_find_token_family_key ( + const char *slug, + struct GNUNET_TIME_Timestamp valid_after, + const struct TALER_MerchantContractTokenFamily *families, + unsigned int families_len, + struct TALER_MerchantContractTokenFamily *family, + struct TALER_MerchantContractTokenFamilyKey *key); |