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;
107 * Cleanup data left from parsing RSA public key.
109 * @param cls closure, NULL
110 * @param[out] spec where to free the data
113 clean_attr (void *cls, struct GNUNET_JSON_Specification *spec)
115 struct GNUNET_RECLAIM_ATTRIBUTE_Claim **attr;
117 attr = (struct GNUNET_RECLAIM_ATTRIBUTE_Claim **) spec->ptr;
127 * JSON Specification for Reclaim claims.
129 * @param ticket struct of GNUNET_RECLAIM_ATTRIBUTE_Claim to fill
130 * @return JSON Specification
132 struct GNUNET_JSON_Specification
133 GNUNET_RECLAIM_JSON_spec_claim (struct GNUNET_RECLAIM_ATTRIBUTE_Claim **attr)
135 struct GNUNET_JSON_Specification ret = { .parser = &parse_attr,
136 .cleaner = &clean_attr,
149 * Parse given JSON object to a ticket
151 * @param cls closure, NULL
152 * @param root the json object representing data
153 * @param spec where to write the data
154 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
157 parse_ticket (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
159 struct GNUNET_RECLAIM_Ticket *ticket;
165 GNUNET_assert (NULL != root);
167 if (! json_is_object (root))
169 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
170 "Error json is not array nor object!\n");
171 return GNUNET_SYSERR;
173 // interpret single ticket
174 unpack_state = json_unpack (root,
182 if (0 != unpack_state)
184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
185 "Error json object has a wrong format!\n");
186 return GNUNET_SYSERR;
188 ticket = GNUNET_new (struct GNUNET_RECLAIM_Ticket);
189 if (GNUNET_OK != GNUNET_STRINGS_string_to_data (rnd_str,
194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Rnd invalid\n");
195 GNUNET_free (ticket);
196 return GNUNET_SYSERR;
199 GNUNET_STRINGS_string_to_data (id_str,
203 struct GNUNET_CRYPTO_EcdsaPublicKey)))
205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Identity invalid\n");
206 GNUNET_free (ticket);
207 return GNUNET_SYSERR;
211 GNUNET_STRINGS_string_to_data (aud_str,
215 GNUNET_CRYPTO_EcdsaPublicKey)))
217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Audience invalid\n");
218 GNUNET_free (ticket);
219 return GNUNET_SYSERR;
222 *(struct GNUNET_RECLAIM_Ticket **) spec->ptr = ticket;
228 * Cleanup data left from parsing RSA public key.
230 * @param cls closure, NULL
231 * @param[out] spec where to free the data
234 clean_ticket (void *cls, struct GNUNET_JSON_Specification *spec)
236 struct GNUNET_RECLAIM_Ticket **ticket;
238 ticket = (struct GNUNET_RECLAIM_Ticket **) spec->ptr;
241 GNUNET_free (*ticket);
248 * JSON Specification for Reclaim tickets.
250 * @param ticket struct of GNUNET_RECLAIM_Ticket to fill
251 * @return JSON Specification
253 struct GNUNET_JSON_Specification
254 GNUNET_RECLAIM_JSON_spec_ticket (struct GNUNET_RECLAIM_Ticket **ticket)
256 struct GNUNET_JSON_Specification ret = { .parser = &parse_ticket,
257 .cleaner = &clean_ticket,