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_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 *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;
51 const char *attest_str = NULL;
52 const char *flag_str = NULL;
58 GNUNET_assert (NULL != root);
60 if (! json_is_object (root))
62 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
63 "Error json is not array nor object!\n");
66 // interpret single attribute
67 unpack_state = json_unpack (root,
68 "{s:s, s?s, s?s, s:s, s:s, s?s!}",
81 if ((0 != unpack_state) || (NULL == name_str) || (NULL == val_str) ||
84 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
85 "Error json object has a wrong format!\n");
88 type = GNUNET_RECLAIM_attribute_typename_to_number (type_str);
90 (GNUNET_RECLAIM_attribute_string_to_value (type,
95 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Attribute value invalid!\n");
98 attr = GNUNET_RECLAIM_attribute_new (name_str, NULL,
99 type, data, data_size);
100 if ((NULL != attest_str) && (0 != strlen (attest_str)))
102 GNUNET_STRINGS_string_to_data (attest_str,
105 sizeof(attr->attestation));
107 if ((NULL == id_str) || (0 == strlen (id_str)))
108 memset (&attr->id, 0, sizeof (attr->id));
110 GNUNET_STRINGS_string_to_data (id_str,
115 *(struct GNUNET_RECLAIM_Attribute **) spec->ptr = attr;
121 * Cleanup data left from parsing RSA public key.
123 * @param cls closure, NULL
124 * @param[out] spec where to free the data
127 clean_attr (void *cls, struct GNUNET_JSON_Specification *spec)
129 struct GNUNET_RECLAIM_Attribute **attr;
131 attr = (struct GNUNET_RECLAIM_Attribute **) spec->ptr;
141 * JSON Specification for Reclaim claims.
143 * @param ticket struct of GNUNET_RECLAIM_Attribute to fill
144 * @return JSON Specification
146 struct GNUNET_JSON_Specification
147 GNUNET_RECLAIM_JSON_spec_claim (struct GNUNET_RECLAIM_Attribute **attr)
149 struct GNUNET_JSON_Specification ret = { .parser = &parse_attr,
150 .cleaner = &clean_attr,
163 * Parse given JSON object to a ticket
165 * @param cls closure, NULL
166 * @param root the json object representing data
167 * @param spec where to write the data
168 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
171 parse_ticket (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
173 struct GNUNET_RECLAIM_Ticket *ticket;
179 GNUNET_assert (NULL != root);
181 if (! json_is_object (root))
183 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
184 "Error json is not array nor object!\n");
185 return GNUNET_SYSERR;
187 // interpret single ticket
188 unpack_state = json_unpack (root,
196 if (0 != unpack_state)
198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
199 "Error json object has a wrong format!\n");
200 return GNUNET_SYSERR;
202 ticket = GNUNET_new (struct GNUNET_RECLAIM_Ticket);
203 if (GNUNET_OK != GNUNET_STRINGS_string_to_data (rnd_str,
206 sizeof(ticket->rnd)))
208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Rnd invalid\n");
209 GNUNET_free (ticket);
210 return GNUNET_SYSERR;
213 GNUNET_STRINGS_string_to_data (id_str,
217 struct GNUNET_CRYPTO_EcdsaPublicKey)))
219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Identity invalid\n");
220 GNUNET_free (ticket);
221 return GNUNET_SYSERR;
225 GNUNET_STRINGS_string_to_data (aud_str,
229 GNUNET_CRYPTO_EcdsaPublicKey)))
231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Audience invalid\n");
232 GNUNET_free (ticket);
233 return GNUNET_SYSERR;
236 *(struct GNUNET_RECLAIM_Ticket **) spec->ptr = ticket;
242 * Cleanup data left from parsing RSA public key.
244 * @param cls closure, NULL
245 * @param[out] spec where to free the data
248 clean_ticket (void *cls, struct GNUNET_JSON_Specification *spec)
250 struct GNUNET_RECLAIM_Ticket **ticket;
252 ticket = (struct GNUNET_RECLAIM_Ticket **) spec->ptr;
255 GNUNET_free (*ticket);
262 * JSON Specification for Reclaim tickets.
264 * @param ticket struct of GNUNET_RECLAIM_Ticket to fill
265 * @return JSON Specification
267 struct GNUNET_JSON_Specification
268 GNUNET_RECLAIM_JSON_spec_ticket (struct GNUNET_RECLAIM_Ticket **ticket)
270 struct GNUNET_JSON_Specification ret = { .parser = &parse_ticket,
271 .cleaner = &clean_ticket,
283 * Parse given JSON object to an attestation claim
285 * @param cls closure, NULL
286 * @param root the json object representing data
287 * @param spec where to write the data
288 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
291 parse_attest (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
293 struct GNUNET_RECLAIM_Attestation *attr;
294 const char *name_str = NULL;
295 const char *val_str = NULL;
296 const char *type_str = NULL;
297 const char *id_str = NULL;
303 GNUNET_assert (NULL != root);
305 if (! json_is_object (root))
307 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
308 "Error json is not array nor object!\n");
309 return GNUNET_SYSERR;
311 // interpret single attribute
312 unpack_state = json_unpack (root,
313 "{s:s, s?s, s:s, s:s!}",
322 if ((0 != unpack_state) || (NULL == name_str) || (NULL == val_str) ||
325 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
326 "Error json object has a wrong format!\n");
327 return GNUNET_SYSERR;
329 type = GNUNET_RECLAIM_attestation_typename_to_number (type_str);
331 (GNUNET_RECLAIM_attestation_string_to_value (type,
336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Attestation value invalid!\n");
337 return GNUNET_SYSERR;
339 attr = GNUNET_RECLAIM_attestation_new (name_str, type, data, data_size);
340 if ((NULL == id_str) || (0 == strlen (id_str)))
341 memset (&attr->id, 0, sizeof (attr->id));
343 GNUNET_STRINGS_string_to_data (id_str,
348 *(struct GNUNET_RECLAIM_Attestation **) spec->ptr = attr;
353 * Cleanup data left from parsing RSA public key.
355 * @param cls closure, NULL
356 * @param[out] spec where to free the data
359 clean_attest (void *cls, struct GNUNET_JSON_Specification *spec)
361 struct GNUNET_RECLAIM_Attestation **attr;
363 attr = (struct GNUNET_RECLAIM_Attestation **) spec->ptr;
371 * JSON Specification for Reclaim attestation claims.
373 * @param ticket struct of GNUNET_RECLAIM_ATTESTATION_Claim to fill
374 * @return JSON Specification
376 struct GNUNET_JSON_Specification
377 GNUNET_RECLAIM_JSON_spec_claim_attest (struct
378 GNUNET_RECLAIM_Attestation **attr)
380 struct GNUNET_JSON_Specification ret = { .parser = &parse_attest,
381 .cleaner = &clean_attest,