-remove debug message
[oweals/gnunet.git] / src / reclaim / json_reclaim.c
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_lib.h"
30 #include "gnunet_reclaim_service.h"
31
32
33 /**
34  * Parse given JSON object to a claim
35  *
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
40  */
41 static int
42 parse_attr (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
43 {
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;
51   char *data;
52   int unpack_state;
53   uint32_t type;
54   size_t data_size;
55
56   GNUNET_assert (NULL != root);
57
58   if (! json_is_object (root))
59   {
60     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
61                 "Error json is not array nor object!\n");
62     return GNUNET_SYSERR;
63   }
64   // interpret single attribute
65   unpack_state = json_unpack (root,
66                               "{s:s, s?s, s?s, s:s, s:s, s?s!}",
67                               "name",
68                               &name_str,
69                               "id",
70                               &id_str,
71                               "attestation",
72                               &attest_str,
73                               "type",
74                               &type_str,
75                               "value",
76                               &val_str,
77                               "flag",
78                               &flag_str);
79   if ((0 != unpack_state) || (NULL == name_str) || (NULL == val_str) ||
80       (NULL == type_str))
81   {
82     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
83                 "Error json object has a wrong format!\n");
84     return GNUNET_SYSERR;
85   }
86   type = GNUNET_RECLAIM_attribute_typename_to_number (type_str);
87   if (GNUNET_SYSERR ==
88       (GNUNET_RECLAIM_attribute_string_to_value (type,
89                                                  val_str,
90                                                  (void **) &data,
91                                                  &data_size)))
92   {
93     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Attribute value invalid!\n");
94     return GNUNET_SYSERR;
95   }
96   attr = GNUNET_RECLAIM_attribute_new (name_str, NULL,
97                                        type, data, data_size);
98   if ((NULL != attest_str) && (0 != strlen (attest_str)))
99   {
100     GNUNET_STRINGS_string_to_data (attest_str,
101                                    strlen (attest_str),
102                                    &attr->attestation,
103                                    sizeof(attr->attestation));
104   }
105   if ((NULL == id_str) || (0 == strlen (id_str)))
106     memset (&attr->id, 0, sizeof (attr->id));
107   else
108     GNUNET_STRINGS_string_to_data (id_str,
109                                    strlen (id_str),
110                                    &attr->id,
111                                    sizeof(attr->id));
112
113   *(struct GNUNET_RECLAIM_Attribute **) spec->ptr = attr;
114   return GNUNET_OK;
115 }
116
117
118 /**
119  * Cleanup data left from parsing RSA public key.
120  *
121  * @param cls closure, NULL
122  * @param[out] spec where to free the data
123  */
124 static void
125 clean_attr (void *cls, struct GNUNET_JSON_Specification *spec)
126 {
127   struct GNUNET_RECLAIM_Attribute **attr;
128
129   attr = (struct GNUNET_RECLAIM_Attribute **) spec->ptr;
130   if (NULL != *attr)
131   {
132     GNUNET_free (*attr);
133     *attr = NULL;
134   }
135 }
136
137
138 /**
139  * JSON Specification for Reclaim claims.
140  *
141  * @param ticket struct of GNUNET_RECLAIM_Attribute to fill
142  * @return JSON Specification
143  */
144 struct GNUNET_JSON_Specification
145 GNUNET_RECLAIM_JSON_spec_claim (struct GNUNET_RECLAIM_Attribute **attr)
146 {
147   struct GNUNET_JSON_Specification ret = { .parser = &parse_attr,
148                                            .cleaner = &clean_attr,
149                                            .cls = NULL,
150                                            .field = NULL,
151                                            .ptr = attr,
152                                            .ptr_size = 0,
153                                            .size_ptr = NULL };
154
155   *attr = NULL;
156   return ret;
157 }
158
159
160 /**
161  * Parse given JSON object to a ticket
162  *
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
167  */
168 static int
169 parse_ticket (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
170 {
171   struct GNUNET_RECLAIM_Ticket *ticket;
172   const char *rnd_str;
173   const char *aud_str;
174   const char *id_str;
175   int unpack_state;
176
177   GNUNET_assert (NULL != root);
178
179   if (! json_is_object (root))
180   {
181     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
182                 "Error json is not array nor object!\n");
183     return GNUNET_SYSERR;
184   }
185   // interpret single ticket
186   unpack_state = json_unpack (root,
187                               "{s:s, s:s, s:s!}",
188                               "rnd",
189                               &rnd_str,
190                               "audience",
191                               &aud_str,
192                               "issuer",
193                               &id_str);
194   if (0 != unpack_state)
195   {
196     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
197                 "Error json object has a wrong format!\n");
198     return GNUNET_SYSERR;
199   }
200   ticket = GNUNET_new (struct GNUNET_RECLAIM_Ticket);
201   if (GNUNET_OK != GNUNET_STRINGS_string_to_data (rnd_str,
202                                                   strlen (rnd_str),
203                                                   &ticket->rnd,
204                                                   sizeof(ticket->rnd)))
205   {
206     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Rnd invalid\n");
207     GNUNET_free (ticket);
208     return GNUNET_SYSERR;
209   }
210   if (GNUNET_OK !=
211       GNUNET_STRINGS_string_to_data (id_str,
212                                      strlen (id_str),
213                                      &ticket->identity,
214                                      sizeof(
215                                        struct GNUNET_CRYPTO_EcdsaPublicKey)))
216   {
217     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Identity invalid\n");
218     GNUNET_free (ticket);
219     return GNUNET_SYSERR;
220   }
221
222   if (GNUNET_OK !=
223       GNUNET_STRINGS_string_to_data (aud_str,
224                                      strlen (aud_str),
225                                      &ticket->audience,
226                                      sizeof(struct
227                                             GNUNET_CRYPTO_EcdsaPublicKey)))
228   {
229     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Audience invalid\n");
230     GNUNET_free (ticket);
231     return GNUNET_SYSERR;
232   }
233
234   *(struct GNUNET_RECLAIM_Ticket **) spec->ptr = ticket;
235   return GNUNET_OK;
236 }
237
238
239 /**
240  * Cleanup data left from parsing RSA public key.
241  *
242  * @param cls closure, NULL
243  * @param[out] spec where to free the data
244  */
245 static void
246 clean_ticket (void *cls, struct GNUNET_JSON_Specification *spec)
247 {
248   struct GNUNET_RECLAIM_Ticket **ticket;
249
250   ticket = (struct GNUNET_RECLAIM_Ticket **) spec->ptr;
251   if (NULL != *ticket)
252   {
253     GNUNET_free (*ticket);
254     *ticket = NULL;
255   }
256 }
257
258
259 /**
260  * JSON Specification for Reclaim tickets.
261  *
262  * @param ticket struct of GNUNET_RECLAIM_Ticket to fill
263  * @return JSON Specification
264  */
265 struct GNUNET_JSON_Specification
266 GNUNET_RECLAIM_JSON_spec_ticket (struct GNUNET_RECLAIM_Ticket **ticket)
267 {
268   struct GNUNET_JSON_Specification ret = { .parser = &parse_ticket,
269                                            .cleaner = &clean_ticket,
270                                            .cls = NULL,
271                                            .field = NULL,
272                                            .ptr = ticket,
273                                            .ptr_size = 0,
274                                            .size_ptr = NULL };
275
276   *ticket = NULL;
277   return ret;
278 }
279
280
281 /**
282    * Parse given JSON object to an attestation claim
283    *
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
288    */
289 static int
290 parse_attest (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
291 {
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;
297   char *data;
298   int unpack_state;
299   uint32_t type;
300   size_t data_size;
301
302   GNUNET_assert (NULL != root);
303
304   if (! json_is_object (root))
305   {
306     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
307                 "Error json is not array nor object!\n");
308     return GNUNET_SYSERR;
309   }
310   // interpret single attribute
311   unpack_state = json_unpack (root,
312                               "{s:s, s?s, s:s, s:s!}",
313                               "name",
314                               &name_str,
315                               "id",
316                               &id_str,
317                               "type",
318                               &type_str,
319                               "value",
320                               &val_str);
321   if ((0 != unpack_state) || (NULL == name_str) || (NULL == val_str) ||
322       (NULL == type_str))
323   {
324     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
325                 "Error json object has a wrong format!\n");
326     return GNUNET_SYSERR;
327   }
328   type = GNUNET_RECLAIM_attestation_typename_to_number (type_str);
329   if (GNUNET_SYSERR ==
330       (GNUNET_RECLAIM_attestation_string_to_value (type,
331                                                    val_str,
332                                                    (void **) &data,
333                                                    &data_size)))
334   {
335     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Attestation value invalid!\n");
336     return GNUNET_SYSERR;
337   }
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));
341   else
342     GNUNET_STRINGS_string_to_data (id_str,
343                                    strlen (id_str),
344                                    &attr->id,
345                                    sizeof(attr->id));
346
347   *(struct GNUNET_RECLAIM_Attestation **) spec->ptr = attr;
348   return GNUNET_OK;
349 }
350
351
352 /**
353  * Cleanup data left from parsing RSA public key.
354  *
355  * @param cls closure, NULL
356  * @param[out] spec where to free the data
357  */
358 static void
359 clean_attest (void *cls, struct GNUNET_JSON_Specification *spec)
360 {
361   struct GNUNET_RECLAIM_Attestation **attr;
362
363   attr = (struct GNUNET_RECLAIM_Attestation **) spec->ptr;
364   if (NULL != *attr)
365   {
366     GNUNET_free (*attr);
367     *attr = NULL;
368   }
369 }
370
371
372 /**
373  * JSON Specification for Reclaim attestation claims.
374  *
375  * @param ticket struct of GNUNET_RECLAIM_ATTESTATION_Claim to fill
376  * @return JSON Specification
377  */
378 struct GNUNET_JSON_Specification
379 GNUNET_RECLAIM_JSON_spec_claim_attest (struct
380                                        GNUNET_RECLAIM_Attestation **attr)
381 {
382   struct GNUNET_JSON_Specification ret = { .parser = &parse_attest,
383                                            .cleaner = &clean_attest,
384                                            .cls = NULL,
385                                            .field = NULL,
386                                            .ptr = attr,
387                                            .ptr_size = 0,
388                                            .size_ptr = NULL };
389
390   *attr = NULL;
391   return ret;
392 }