2 This file is part of GNUnet.
3 Copyright (C) 2009-2018 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file rest-plugins/json_reclaim.c
23 * @brief JSON handling of reclaim data
24 * @author Martin Schanzenbach
28 #include "gnunet_util_lib.h"
30 #include "gnunet_json_lib.h"
31 #include "gnunet_reclaim_attribute_lib.h"
32 #include "gnunet_reclaim_service.h"
36 * Parse given JSON object to a claim
38 * @param cls closure, NULL
39 * @param root the json object representing data
40 * @param spec where to write the data
41 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
44 parse_attr (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
46 struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr;
47 const char *name_str = NULL;
48 const char *val_str = NULL;
49 const char *type_str = NULL;
50 const char *id_str = NULL;
56 GNUNET_assert (NULL != root);
58 if (! json_is_object (root))
60 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
61 "Error json is not array nor object!\n");
64 // interpret single attribute
65 unpack_state = json_unpack (root,
66 "{s:s, s?s, s:s, s:s!}",
75 if ((0 != unpack_state) || (NULL == name_str) || (NULL == val_str) ||
78 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
79 "Error json object has a wrong format!\n");
82 type = GNUNET_RECLAIM_ATTRIBUTE_typename_to_number (type_str);
84 (GNUNET_RECLAIM_ATTRIBUTE_string_to_value (type,
89 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Attribute value invalid!\n");
92 attr = GNUNET_RECLAIM_ATTRIBUTE_claim_new (name_str, type, data, data_size);
93 if ((NULL == id_str) || (0 == strlen (id_str)))
96 GNUNET_STRINGS_string_to_data (id_str,
101 *(struct GNUNET_RECLAIM_ATTRIBUTE_Claim **) spec->ptr = attr;
106 * Cleanup data left from parsing RSA public key.
108 * @param cls closure, NULL
109 * @param[out] spec where to free the data
112 clean_attr (void *cls, struct GNUNET_JSON_Specification *spec)
114 struct GNUNET_RECLAIM_ATTRIBUTE_Claim **attr;
115 attr = (struct GNUNET_RECLAIM_ATTRIBUTE_Claim **) spec->ptr;
124 * JSON Specification for Reclaim claims.
126 * @param ticket struct of GNUNET_RECLAIM_ATTRIBUTE_Claim to fill
127 * @return JSON Specification
129 struct GNUNET_JSON_Specification
130 GNUNET_RECLAIM_JSON_spec_claim (struct GNUNET_RECLAIM_ATTRIBUTE_Claim **attr)
132 struct GNUNET_JSON_Specification ret = {.parser = &parse_attr,
133 .cleaner = &clean_attr,
143 * Parse given JSON object to a ticket
145 * @param cls closure, NULL
146 * @param root the json object representing data
147 * @param spec where to write the data
148 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
151 parse_ticket (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
153 struct GNUNET_RECLAIM_Ticket *ticket;
159 GNUNET_assert (NULL != root);
161 if (! json_is_object (root))
163 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
164 "Error json is not array nor object!\n");
165 return GNUNET_SYSERR;
167 // interpret single ticket
168 unpack_state = json_unpack (root,
176 if (0 != unpack_state)
178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
179 "Error json object has a wrong format!\n");
180 return GNUNET_SYSERR;
182 ticket = GNUNET_new (struct GNUNET_RECLAIM_Ticket);
183 if (GNUNET_OK != GNUNET_STRINGS_string_to_data (rnd_str,
188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Rnd invalid\n");
189 GNUNET_free (ticket);
190 return GNUNET_SYSERR;
193 GNUNET_STRINGS_string_to_data (id_str,
197 struct GNUNET_CRYPTO_EcdsaPublicKey)))
199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Identity invalid\n");
200 GNUNET_free (ticket);
201 return GNUNET_SYSERR;
205 GNUNET_STRINGS_string_to_data (aud_str,
208 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Audience invalid\n");
211 GNUNET_free (ticket);
212 return GNUNET_SYSERR;
215 *(struct GNUNET_RECLAIM_Ticket **) spec->ptr = ticket;
220 * Cleanup data left from parsing RSA public key.
222 * @param cls closure, NULL
223 * @param[out] spec where to free the data
226 clean_ticket (void *cls, struct GNUNET_JSON_Specification *spec)
228 struct GNUNET_RECLAIM_Ticket **ticket;
229 ticket = (struct GNUNET_RECLAIM_Ticket **) spec->ptr;
232 GNUNET_free (*ticket);
238 * JSON Specification for Reclaim tickets.
240 * @param ticket struct of GNUNET_RECLAIM_Ticket to fill
241 * @return JSON Specification
243 struct GNUNET_JSON_Specification
244 GNUNET_RECLAIM_JSON_spec_ticket (struct GNUNET_RECLAIM_Ticket **ticket)
246 struct GNUNET_JSON_Specification ret = {.parser = &parse_ticket,
247 .cleaner = &clean_ticket,