aboutsummaryrefslogtreecommitdiff
path: root/src/reclaim/json_reclaim.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/reclaim/json_reclaim.c')
-rw-r--r--src/reclaim/json_reclaim.c242
1 files changed, 242 insertions, 0 deletions
diff --git a/src/reclaim/json_reclaim.c b/src/reclaim/json_reclaim.c
new file mode 100644
index 000000000..0fe9150d9
--- /dev/null
+++ b/src/reclaim/json_reclaim.c
@@ -0,0 +1,242 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2009-2018 GNUnet e.V.
4
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.
9
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.
14
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/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19*/
20
21/**
22 * @file rest-plugins/json_reclaim.c
23 * @brief JSON handling of reclaim data
24 * @author Martin Schanzenbach
25 */
26#include "platform.h"
27#include "gnunet_util_lib.h"
28#include "gnunet_json_lib.h"
29#include "gnunet_reclaim_service.h"
30#include "gnunet_reclaim_attribute_lib.h"
31
32/**
33 * Parse given JSON object to a claim
34 *
35 * @param cls closure, NULL
36 * @param root the json object representing data
37 * @param spec where to write the data
38 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
39 */
40static int
41parse_attr (void *cls,
42 json_t *root,
43 struct GNUNET_JSON_Specification *spec)
44{
45 struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr;
46 const char* name_str;
47 const char* val_str;
48 const char* type_str;
49 char *data;
50 int unpack_state;
51 uint32_t type;
52 size_t data_size;
53
54 GNUNET_assert(NULL != root);
55
56 if(!json_is_object(root))
57 {
58 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
59 "Error json is not array nor object!\n");
60 return GNUNET_SYSERR;
61 }
62 //interpret single attribute
63 unpack_state = json_unpack(root,
64 "{s:s, s:s, s:s!}",
65 "name", &name_str,
66 "type", &type_str,
67 "value", &val_str);
68 if (0 != unpack_state)
69 {
70 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
71 "Error json object has a wrong format!\n");
72 return GNUNET_SYSERR;
73 }
74 type = GNUNET_RECLAIM_ATTRIBUTE_typename_to_number (type_str);
75 if (GNUNET_SYSERR == (GNUNET_RECLAIM_ATTRIBUTE_string_to_value (type,
76 val_str,
77 (void**)&data,
78 &data_size)))
79 {
80 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
81 "Attribute value invalid!\n");
82 return GNUNET_SYSERR;
83 }
84 attr = GNUNET_RECLAIM_ATTRIBUTE_claim_new (name_str,
85 type,
86 data,
87 data_size);
88 *(struct GNUNET_RECLAIM_ATTRIBUTE_Claim **) spec->ptr = attr;
89 return GNUNET_OK;
90}
91
92/**
93 * Cleanup data left from parsing RSA public key.
94 *
95 * @param cls closure, NULL
96 * @param[out] spec where to free the data
97 */
98static void
99clean_attr (void *cls, struct GNUNET_JSON_Specification *spec)
100{
101 struct GNUNET_RECLAIM_ATTRIBUTE_Claim **attr;
102 attr = (struct GNUNET_RECLAIM_ATTRIBUTE_Claim **) spec->ptr;
103 if (NULL != *attr)
104 {
105 GNUNET_free(*attr);
106 *attr = NULL;
107 }
108}
109
110/**
111 * JSON Specification for Reclaim claims.
112 *
113 * @param ticket struct of GNUNET_RECLAIM_ATTRIBUTE_Claim to fill
114 * @return JSON Specification
115 */
116struct GNUNET_JSON_Specification
117GNUNET_RECLAIM_JSON_spec_claim (struct GNUNET_RECLAIM_ATTRIBUTE_Claim **attr)
118{
119 struct GNUNET_JSON_Specification ret = {
120 .parser = &parse_attr,
121 .cleaner = &clean_attr,
122 .cls = NULL,
123 .field = NULL,
124 .ptr = attr,
125 .ptr_size = 0,
126 .size_ptr = NULL
127 };
128 *attr = NULL;
129 return ret;
130}
131/**
132 * Parse given JSON object to a ticket
133 *
134 * @param cls closure, NULL
135 * @param root the json object representing data
136 * @param spec where to write the data
137 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
138 */
139static int
140parse_ticket (void *cls,
141 json_t *root,
142 struct GNUNET_JSON_Specification *spec)
143{
144 struct GNUNET_RECLAIM_Ticket *ticket;
145 const char* rnd_str;
146 const char* aud_str;
147 const char* id_str;
148 int unpack_state;
149
150 GNUNET_assert(NULL != root);
151
152 if(!json_is_object(root))
153 {
154 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
155 "Error json is not array nor object!\n");
156 return GNUNET_SYSERR;
157 }
158 //interpret single ticket
159 unpack_state = json_unpack(root,
160 "{s:s, s:s, s:s!}",
161 "rnd", &rnd_str,
162 "audience", &aud_str,
163 "identity", &id_str);
164 if (0 != unpack_state)
165 {
166 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
167 "Error json object has a wrong format!\n");
168 return GNUNET_SYSERR;
169 }
170 ticket = GNUNET_new (struct GNUNET_RECLAIM_Ticket);
171 if (GNUNET_OK != GNUNET_STRINGS_string_to_data (rnd_str,
172 strlen (rnd_str),
173 &ticket->rnd,
174 sizeof (uint64_t)))
175 {
176 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,"Rnd invalid\n");
177 GNUNET_free(ticket);
178 return GNUNET_SYSERR;
179 }
180 GNUNET_STRINGS_string_to_data (id_str,
181 strlen (id_str),
182 &ticket->identity,
183 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
184 {
185 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,"Identity invalid\n");
186 GNUNET_free(ticket);
187 return GNUNET_SYSERR;
188 }
189
190 GNUNET_STRINGS_string_to_data (aud_str,
191 strlen (aud_str),
192 &ticket->audience,
193 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
194 {
195 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,"Audience invalid\n");
196 GNUNET_free(ticket);
197 return GNUNET_SYSERR;
198 }
199
200 *(struct GNUNET_RECLAIM_Ticket **) spec->ptr = ticket;
201 return GNUNET_OK;
202}
203
204/**
205 * Cleanup data left from parsing RSA public key.
206 *
207 * @param cls closure, NULL
208 * @param[out] spec where to free the data
209 */
210static void
211clean_ticket (void *cls, struct GNUNET_JSON_Specification *spec)
212{
213 struct GNUNET_RECLAIM_Ticket **ticket;
214 ticket = (struct GNUNET_RECLAIM_Ticket **) spec->ptr;
215 if (NULL != *ticket)
216 {
217 GNUNET_free(*ticket);
218 *ticket = NULL;
219 }
220}
221
222/**
223 * JSON Specification for Reclaim tickets.
224 *
225 * @param ticket struct of GNUNET_RECLAIM_Ticket to fill
226 * @return JSON Specification
227 */
228struct GNUNET_JSON_Specification
229GNUNET_RECLAIM_JSON_spec_ticket (struct GNUNET_RECLAIM_Ticket **ticket)
230{
231 struct GNUNET_JSON_Specification ret = {
232 .parser = &parse_ticket,
233 .cleaner = &clean_ticket,
234 .cls = NULL,
235 .field = NULL,
236 .ptr = ticket,
237 .ptr_size = 0,
238 .size_ptr = NULL
239 };
240 *ticket = NULL;
241 return ret;
242}