exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

commit 3b0dadc28bb1d8b9849243fa9114603303f149ba
parent 2f67dbebc572f1c70c3bfcb39fec45591aea9bc8
Author: Christian Grothoff <christian@grothoff.org>
Date:   Sun, 17 May 2015 17:28:18 +0200

add functions for conversion from json to rsa public key/signature for symmetry, testing, etc.

Diffstat:
Msrc/include/taler_json_lib.h | 32+++++++++++++++++++++-----------
Msrc/util/json.c | 84++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------
2 files changed, 91 insertions(+), 25 deletions(-)

diff --git a/src/include/taler_json_lib.h b/src/include/taler_json_lib.h @@ -97,17 +97,6 @@ TALER_json_from_data (const void *data, size_t size); /** - * Convert binary hash to a JSON string with the base32crockford - * encoding. - * - * @param hc binary data - * @return json string that encodes @a hc - */ -json_t * -TALER_json_from_hash (const struct GNUNET_HashCode *hc); - - -/** * Parse given JSON object to Amount * * @param json the json object representing Amount @@ -142,6 +131,27 @@ TALER_json_to_data (json_t *json, void *out, size_t out_size); + +/** + * Convert JSON to RSA public key. + * + * @param pk JSON encoding to convert + * @return corresponding public key + */ +struct GNUNET_CRYPTO_rsa_PublicKey * +TALER_json_to_rsa_public_key (json_t *json); + + +/** + * Convert JSON to RSA signature. + * + * @param pk JSON encoding to convert + * @return corresponding signature + */ +struct GNUNET_CRYPTO_rsa_Signature * +TALER_json_to_rsa_signature (json_t *json); + + /** * Check if the given wire format JSON object is correctly formatted * diff --git a/src/util/json.c b/src/util/json.c @@ -153,6 +153,76 @@ TALER_json_from_rsa_public_key (struct GNUNET_CRYPTO_rsa_PublicKey *pk) /** + * Convert JSON to RSA public key. + * + * @param pk JSON encoding to convert + * @return corresponding public key + */ +struct GNUNET_CRYPTO_rsa_PublicKey * +TALER_json_to_rsa_public_key (json_t *json) +{ + const char *enc; + char *buf; + size_t len; + size_t buf_len; + struct GNUNET_CRYPTO_rsa_PublicKey *pk; + + buf = NULL; + EXITIF (NULL == (enc = json_string_value (json))); + len = strlen (enc); + buf_len = (len * 5) / 8; + buf = GNUNET_malloc (buf_len); + EXITIF (GNUNET_OK != + GNUNET_STRINGS_string_to_data (enc, + len, + buf, + buf_len)); + EXITIF (NULL == (pk = GNUNET_CRYPTO_rsa_public_key_decode (buf, + buf_len))); + GNUNET_free (buf); + return pk; + EXITIF_exit: + GNUNET_free_non_null (buf); + return NULL; +} + + +/** + * Convert JSON to RSA signature. + * + * @param pk JSON encoding to convert + * @return corresponding signature + */ +struct GNUNET_CRYPTO_rsa_Signature * +TALER_json_to_rsa_signature (json_t *json) +{ + const char *enc; + char *buf; + size_t len; + size_t buf_len; + struct GNUNET_CRYPTO_rsa_Signature *sig; + + buf = NULL; + EXITIF (NULL == (enc = json_string_value (json))); + len = strlen (enc); + buf_len = (len * 5) / 8; + buf = GNUNET_malloc (buf_len); + EXITIF (GNUNET_OK != + GNUNET_STRINGS_string_to_data (enc, + len, + buf, + buf_len)); + EXITIF (NULL == (sig = GNUNET_CRYPTO_rsa_signature_decode (buf, + buf_len))); + GNUNET_free (buf); + return sig; + EXITIF_exit: + GNUNET_free_non_null (buf); + return NULL; +} + + +/** * Convert RSA signature to JSON. * * @param sig signature to convert @@ -197,20 +267,6 @@ TALER_json_from_data (const void *data, /** - * Convert binary hash to a JSON string with the base32crockford - * encoding. - * - * @param hc binary data - * @return json string that encodes @a hc - */ -json_t * -TALER_json_from_hash (const struct GNUNET_HashCode *hc) -{ - return TALER_json_from_data (hc, sizeof (struct GNUNET_HashCode)); -} - - -/** * Parse given JSON object to Amount * * @param json the json object representing Amount