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
27 #include "gnunet_util_lib.h"
28 #include "gnunet_json_lib.h"
29 #include "gnunet_reclaim_lib.h"
30 #include "gnunet_reclaim_service.h"
34 * Parse given JSON object to a claim
36 * @param cls closure, NULL
37 * @param root the json object representing data
38 * @param spec where to write the data
39 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
42 parse_attr (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
44 struct GNUNET_RECLAIM_Attribute *attr;
45 const char *name_str = NULL;
46 const char *val_str = NULL;
47 const char *type_str = NULL;
48 const char *id_str = NULL;
49 const char *attest_str = NULL;
50 const char *flag_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, s:s, s?s!}",
79 if ((0 != unpack_state) || (NULL == name_str) || (NULL == val_str) ||
82 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
83 "Error json object has a wrong format!\n");
86 type = GNUNET_RECLAIM_attribute_typename_to_number (type_str);
88 (GNUNET_RECLAIM_attribute_string_to_value (type,
93 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Attribute value invalid!\n");
96 attr = GNUNET_RECLAIM_attribute_new (name_str, NULL,
97 type, data, data_size);
98 if ((NULL != attest_str) && (0 != strlen (attest_str)))
100 GNUNET_STRINGS_string_to_data (attest_str,
103 sizeof(attr->attestation));
105 if ((NULL == id_str) || (0 == strlen (id_str)))
106 memset (&attr->id, 0, sizeof (attr->id));
108 GNUNET_STRINGS_string_to_data (id_str,
113 *(struct GNUNET_RECLAIM_Attribute **) spec->ptr = attr;
119 * Cleanup data left from parsing RSA public key.
121 * @param cls closure, NULL
122 * @param[out] spec where to free the data
125 clean_attr (void *cls, struct GNUNET_JSON_Specification *spec)
127 struct GNUNET_RECLAIM_Attribute **attr;
129 attr = (struct GNUNET_RECLAIM_Attribute **) spec->ptr;
139 * JSON Specification for Reclaim claims.
141 * @param ticket struct of GNUNET_RECLAIM_Attribute to fill
142 * @return JSON Specification
144 struct GNUNET_JSON_Specification
145 GNUNET_RECLAIM_JSON_spec_claim (struct GNUNET_RECLAIM_Attribute **attr)
147 struct GNUNET_JSON_Specification ret = { .parser = &parse_attr,
148 .cleaner = &clean_attr,
161 * Parse given JSON object to a ticket
163 * @param cls closure, NULL
164 * @param root the json object representing data
165 * @param spec where to write the data
166 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
169 parse_ticket (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
171 struct GNUNET_RECLAIM_Ticket *ticket;
177 GNUNET_assert (NULL != root);
179 if (! json_is_object (root))
181 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
182 "Error json is not array nor object!\n");
183 return GNUNET_SYSERR;
185 // interpret single ticket
186 unpack_state = json_unpack (root,
194 if (0 != unpack_state)
196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
197 "Error json object has a wrong format!\n");
198 return GNUNET_SYSERR;
200 ticket = GNUNET_new (struct GNUNET_RECLAIM_Ticket);
201 if (GNUNET_OK != GNUNET_STRINGS_string_to_data (rnd_str,
204 sizeof(ticket->rnd)))
206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Rnd invalid\n");
207 GNUNET_free (ticket);
208 return GNUNET_SYSERR;
211 GNUNET_STRINGS_string_to_data (id_str,
215 struct GNUNET_CRYPTO_EcdsaPublicKey)))
217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Identity invalid\n");
218 GNUNET_free (ticket);
219 return GNUNET_SYSERR;
223 GNUNET_STRINGS_string_to_data (aud_str,
227 GNUNET_CRYPTO_EcdsaPublicKey)))
229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Audience invalid\n");
230 GNUNET_free (ticket);
231 return GNUNET_SYSERR;
234 *(struct GNUNET_RECLAIM_Ticket **) spec->ptr = ticket;
240 * Cleanup data left from parsing RSA public key.
242 * @param cls closure, NULL
243 * @param[out] spec where to free the data
246 clean_ticket (void *cls, struct GNUNET_JSON_Specification *spec)
248 struct GNUNET_RECLAIM_Ticket **ticket;
250 ticket = (struct GNUNET_RECLAIM_Ticket **) spec->ptr;
253 GNUNET_free (*ticket);
260 * JSON Specification for Reclaim tickets.
262 * @param ticket struct of GNUNET_RECLAIM_Ticket to fill
263 * @return JSON Specification
265 struct GNUNET_JSON_Specification
266 GNUNET_RECLAIM_JSON_spec_ticket (struct GNUNET_RECLAIM_Ticket **ticket)
268 struct GNUNET_JSON_Specification ret = { .parser = &parse_ticket,
269 .cleaner = &clean_ticket,
282 * Parse given JSON object to an attestation claim
284 * @param cls closure, NULL
285 * @param root the json object representing data
286 * @param spec where to write the data
287 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
290 parse_attest (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
292 struct GNUNET_RECLAIM_Attestation *attr;
293 const char *name_str = NULL;
294 const char *val_str = NULL;
295 const char *type_str = NULL;
296 const char *id_str = NULL;
302 GNUNET_assert (NULL != root);
304 if (! json_is_object (root))
306 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
307 "Error json is not array nor object!\n");
308 return GNUNET_SYSERR;
310 // interpret single attribute
311 unpack_state = json_unpack (root,
312 "{s:s, s?s, s:s, s:s!}",
321 if ((0 != unpack_state) || (NULL == name_str) || (NULL == val_str) ||
324 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
325 "Error json object has a wrong format!\n");
326 return GNUNET_SYSERR;
328 type = GNUNET_RECLAIM_attestation_typename_to_number (type_str);
330 (GNUNET_RECLAIM_attestation_string_to_value (type,
335 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Attestation value invalid!\n");
336 return GNUNET_SYSERR;
338 attr = GNUNET_RECLAIM_attestation_new (name_str, type, data, data_size);
339 if ((NULL == id_str) || (0 == strlen (id_str)))
340 memset (&attr->id, 0, sizeof (attr->id));
342 GNUNET_STRINGS_string_to_data (id_str,
347 *(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;
373 * JSON Specification for Reclaim attestation claims.
375 * @param ticket struct of GNUNET_RECLAIM_ATTESTATION_Claim to fill
376 * @return JSON Specification
378 struct GNUNET_JSON_Specification
379 GNUNET_RECLAIM_JSON_spec_claim_attest (struct
380 GNUNET_RECLAIM_Attestation **attr)
382 struct GNUNET_JSON_Specification ret = { .parser = &parse_attest,
383 .cleaner = &clean_attest,