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;
51 const char *flag_str = NULL;
57 GNUNET_assert (NULL != root);
59 if (! json_is_object (root))
61 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
62 "Error json is not array nor object!\n");
65 // interpret single attribute
66 unpack_state = json_unpack (root,
67 "{s:s, s?s, s:s, s:s, s?s!}",
78 if ((0 != unpack_state) || (NULL == name_str) || (NULL == val_str) ||
81 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
82 "Error json object has a wrong format!\n");
85 type = GNUNET_RECLAIM_ATTRIBUTE_typename_to_number (type_str);
87 (GNUNET_RECLAIM_ATTRIBUTE_string_to_value (type,
92 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Attribute value invalid!\n");
95 attr = GNUNET_RECLAIM_ATTRIBUTE_claim_new (name_str, type, data, data_size);
96 if ((NULL == id_str) || (0 == strlen (id_str)))
99 GNUNET_STRINGS_string_to_data (id_str,
104 *(struct GNUNET_RECLAIM_ATTRIBUTE_Claim **) spec->ptr = attr;
110 * Cleanup data left from parsing RSA public key.
112 * @param cls closure, NULL
113 * @param[out] spec where to free the data
116 clean_attr (void *cls, struct GNUNET_JSON_Specification *spec)
118 struct GNUNET_RECLAIM_ATTRIBUTE_Claim **attr;
120 attr = (struct GNUNET_RECLAIM_ATTRIBUTE_Claim **) spec->ptr;
130 * JSON Specification for Reclaim claims.
132 * @param ticket struct of GNUNET_RECLAIM_ATTRIBUTE_Claim to fill
133 * @return JSON Specification
135 struct GNUNET_JSON_Specification
136 GNUNET_RECLAIM_JSON_spec_claim (struct GNUNET_RECLAIM_ATTRIBUTE_Claim **attr)
138 struct GNUNET_JSON_Specification ret = { .parser = &parse_attr,
139 .cleaner = &clean_attr,
152 * Parse given JSON object to a ticket
154 * @param cls closure, NULL
155 * @param root the json object representing data
156 * @param spec where to write the data
157 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
160 parse_ticket (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
162 struct GNUNET_RECLAIM_Ticket *ticket;
168 GNUNET_assert (NULL != root);
170 if (! json_is_object (root))
172 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
173 "Error json is not array nor object!\n");
174 return GNUNET_SYSERR;
176 // interpret single ticket
177 unpack_state = json_unpack (root,
185 if (0 != unpack_state)
187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
188 "Error json object has a wrong format!\n");
189 return GNUNET_SYSERR;
191 ticket = GNUNET_new (struct GNUNET_RECLAIM_Ticket);
192 if (GNUNET_OK != GNUNET_STRINGS_string_to_data (rnd_str,
197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Rnd invalid\n");
198 GNUNET_free (ticket);
199 return GNUNET_SYSERR;
202 GNUNET_STRINGS_string_to_data (id_str,
206 struct GNUNET_CRYPTO_EcdsaPublicKey)))
208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Identity invalid\n");
209 GNUNET_free (ticket);
210 return GNUNET_SYSERR;
214 GNUNET_STRINGS_string_to_data (aud_str,
218 GNUNET_CRYPTO_EcdsaPublicKey)))
220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Audience invalid\n");
221 GNUNET_free (ticket);
222 return GNUNET_SYSERR;
225 *(struct GNUNET_RECLAIM_Ticket **) spec->ptr = ticket;
231 * Cleanup data left from parsing RSA public key.
233 * @param cls closure, NULL
234 * @param[out] spec where to free the data
237 clean_ticket (void *cls, struct GNUNET_JSON_Specification *spec)
239 struct GNUNET_RECLAIM_Ticket **ticket;
241 ticket = (struct GNUNET_RECLAIM_Ticket **) spec->ptr;
244 GNUNET_free (*ticket);
251 * JSON Specification for Reclaim tickets.
253 * @param ticket struct of GNUNET_RECLAIM_Ticket to fill
254 * @return JSON Specification
256 struct GNUNET_JSON_Specification
257 GNUNET_RECLAIM_JSON_spec_ticket (struct GNUNET_RECLAIM_Ticket **ticket)
259 struct GNUNET_JSON_Specification ret = { .parser = &parse_ticket,
260 .cleaner = &clean_ticket,
272 * Parse given JSON object to an attestation claim
274 * @param cls closure, NULL
275 * @param root the json object representing data
276 * @param spec where to write the data
277 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
280 parse_attest (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
282 struct GNUNET_RECLAIM_ATTESTATION_Claim *attr;
283 const char *name_str = NULL;
284 const char *val_str = NULL;
285 const char *type_str = NULL;
286 const char *id_str = NULL;
292 GNUNET_assert (NULL != root);
294 if (! json_is_object (root))
296 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
297 "Error json is not array nor object!\n");
298 return GNUNET_SYSERR;
300 // interpret single attribute
301 unpack_state = json_unpack (root,
302 "{s:s, s?s, s:s, s:s!}",
311 if ((0 != unpack_state) || (NULL == name_str) || (NULL == val_str) ||
314 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
315 "Error json object has a wrong format!\n");
316 return GNUNET_SYSERR;
318 type = GNUNET_RECLAIM_ATTESTATION_typename_to_number (type_str);
320 (GNUNET_RECLAIM_ATTESTATION_string_to_value (type,
325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Attestation value invalid!\n");
326 return GNUNET_SYSERR;
328 attr = GNUNET_RECLAIM_ATTESTATION_claim_new (name_str, type, data, data_size);
329 if ((NULL == id_str) || (0 == strlen (id_str)))
332 GNUNET_STRINGS_string_to_data (id_str,
337 *(struct GNUNET_RECLAIM_ATTESTATION_Claim **) spec->ptr = attr;
342 * Cleanup data left from parsing RSA public key.
344 * @param cls closure, NULL
345 * @param[out] spec where to free the data
348 clean_attest (void *cls, struct GNUNET_JSON_Specification *spec)
350 struct GNUNET_RECLAIM_ATTESTATION_Claim **attr;
352 attr = (struct GNUNET_RECLAIM_ATTESTATION_Claim **) spec->ptr;
360 * JSON Specification for Reclaim attestation claims.
362 * @param ticket struct of GNUNET_RECLAIM_ATTESTATION_Claim to fill
363 * @return JSON Specification
365 struct GNUNET_JSON_Specification
366 GNUNET_RECLAIM_JSON_spec_claim_attest (struct
367 GNUNET_RECLAIM_ATTESTATION_Claim **attr)
369 struct GNUNET_JSON_Specification ret = { .parser = &parse_attest,
370 .cleaner = &clean_attest,
382 * Parse given JSON object to an attestation claim
384 * @param cls closure, NULL
385 * @param root the json object representing data
386 * @param spec where to write the data
387 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
390 parse_attest_ref (void *cls, json_t *root, struct
391 GNUNET_JSON_Specification *spec)
393 struct GNUNET_RECLAIM_ATTESTATION_REFERENCE *attr;
394 const char *name_str = NULL;
395 const char *ref_val_str = NULL;
396 const char *ref_id_str = NULL;
397 const char *id_str = NULL;
400 GNUNET_assert (NULL != root);
402 if (! json_is_object (root))
404 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
405 "Error json is not array nor object!\n");
406 return GNUNET_SYSERR;
408 // interpret single reference
409 unpack_state = json_unpack (root,
410 "{s:s, s?s, s:s, s:s!}",
419 if ((0 != unpack_state) || (NULL == name_str) || (NULL == ref_val_str) ||
420 (NULL == ref_id_str))
422 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
423 "Error json object has a wrong format!\n");
424 return GNUNET_SYSERR;
427 attr = GNUNET_RECLAIM_ATTESTATION_reference_new (name_str, ref_val_str);
431 if ((NULL == ref_id_str) || (0 == strlen (ref_id_str)))
434 GNUNET_STRINGS_string_to_data (ref_id_str,
439 *(struct GNUNET_RECLAIM_ATTESTATION_REFERENCE **) spec->ptr = attr;
444 * Cleanup data left from parsing RSA public key.
446 * @param cls closure, NULL
447 * @param[out] spec where to free the data
450 clean_attest_ref (void *cls, struct GNUNET_JSON_Specification *spec)
452 struct GNUNET_RECLAIM_ATTESTATION_REFERENCE **attr;
454 attr = (struct GNUNET_RECLAIM_ATTESTATION_REFERENCE **) spec->ptr;
463 * JSON Specification for Reclaim attestation references.
465 * @param ticket struct of GNUNET_RECLAIM_ATTESTATION_REFERENCE to fill
466 * @return JSON Specification
468 struct GNUNET_JSON_Specification
469 GNUNET_RECLAIM_JSON_spec_claim_attest_ref (struct
470 GNUNET_RECLAIM_ATTESTATION_REFERENCE
473 struct GNUNET_JSON_Specification ret = { .parser = &parse_attest_ref,
474 .cleaner = &clean_attest_ref,