From 3b0dadc28bb1d8b9849243fa9114603303f149ba Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 17 May 2015 17:28:18 +0200 Subject: add functions for conversion from json to rsa public key/signature for symmetry, testing, etc. --- src/util/json.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 70 insertions(+), 14 deletions(-) (limited to 'src/util') diff --git a/src/util/json.c b/src/util/json.c index 3d702b7be..34e41e0d7 100644 --- a/src/util/json.c +++ b/src/util/json.c @@ -152,6 +152,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. * @@ -196,20 +266,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 * -- cgit v1.2.3