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;
116 attr = (struct GNUNET_RECLAIM_ATTRIBUTE_Claim **) spec->ptr;
125 * JSON Specification for Reclaim claims.
127 * @param ticket struct of GNUNET_RECLAIM_ATTRIBUTE_Claim to fill
128 * @return JSON Specification
130 struct GNUNET_JSON_Specification
131 GNUNET_RECLAIM_JSON_spec_claim (struct GNUNET_RECLAIM_ATTRIBUTE_Claim **attr)
133 struct GNUNET_JSON_Specification ret = { .parser = &parse_attr,
134 .cleaner = &clean_attr,
145 * Parse given JSON object to a ticket
147 * @param cls closure, NULL
148 * @param root the json object representing data
149 * @param spec where to write the data
150 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
153 parse_ticket (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
155 struct GNUNET_RECLAIM_Ticket *ticket;
161 GNUNET_assert (NULL != root);
163 if (! json_is_object (root))
165 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
166 "Error json is not array nor object!\n");
167 return GNUNET_SYSERR;
169 // interpret single ticket
170 unpack_state = json_unpack (root,
178 if (0 != unpack_state)
180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
181 "Error json object has a wrong format!\n");
182 return GNUNET_SYSERR;
184 ticket = GNUNET_new (struct GNUNET_RECLAIM_Ticket);
185 if (GNUNET_OK != GNUNET_STRINGS_string_to_data (rnd_str,
190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Rnd invalid\n");
191 GNUNET_free (ticket);
192 return GNUNET_SYSERR;
195 GNUNET_STRINGS_string_to_data (id_str,
199 struct GNUNET_CRYPTO_EcdsaPublicKey)))
201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Identity invalid\n");
202 GNUNET_free (ticket);
203 return GNUNET_SYSERR;
207 GNUNET_STRINGS_string_to_data (aud_str,
211 GNUNET_CRYPTO_EcdsaPublicKey)))
213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Audience invalid\n");
214 GNUNET_free (ticket);
215 return GNUNET_SYSERR;
218 *(struct GNUNET_RECLAIM_Ticket **) spec->ptr = ticket;
223 * Cleanup data left from parsing RSA public key.
225 * @param cls closure, NULL
226 * @param[out] spec where to free the data
229 clean_ticket (void *cls, struct GNUNET_JSON_Specification *spec)
231 struct GNUNET_RECLAIM_Ticket **ticket;
233 ticket = (struct GNUNET_RECLAIM_Ticket **) spec->ptr;
236 GNUNET_free (*ticket);
242 * JSON Specification for Reclaim tickets.
244 * @param ticket struct of GNUNET_RECLAIM_Ticket to fill
245 * @return JSON Specification
247 struct GNUNET_JSON_Specification
248 GNUNET_RECLAIM_JSON_spec_ticket (struct GNUNET_RECLAIM_Ticket **ticket)
250 struct GNUNET_JSON_Specification ret = { .parser = &parse_ticket,
251 .cleaner = &clean_ticket,