GNUnet  0.11.x
json_reclaim.c
Go to the documentation of this file.
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 
26 #include "platform.h"
27 
28 #include "gnunet_util_lib.h"
29 
30 #include "gnunet_json_lib.h"
31 #include "gnunet_reclaim_lib.h"
32 #include "gnunet_reclaim_service.h"
33 
34 
43 static int
44 parse_attr (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
45 {
46  struct GNUNET_RECLAIM_Attribute *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 *attest_str = NULL;
52  const char *flag_str = NULL;
53  char *data;
54  int unpack_state;
55  uint32_t type;
56  size_t data_size;
57 
58  GNUNET_assert (NULL != root);
59 
60  if (! json_is_object (root))
61  {
63  "Error json is not array nor object!\n");
64  return GNUNET_SYSERR;
65  }
66  // interpret single attribute
67  unpack_state = json_unpack (root,
68  "{s:s, s?s, s?s, s:s, s:s, s?s!}",
69  "name",
70  &name_str,
71  "id",
72  &id_str,
73  "attestation",
74  &attest_str,
75  "type",
76  &type_str,
77  "value",
78  &val_str,
79  "flag",
80  &flag_str);
81  if ((0 != unpack_state) || (NULL == name_str) || (NULL == val_str) ||
82  (NULL == type_str))
83  {
85  "Error json object has a wrong format!\n");
86  return GNUNET_SYSERR;
87  }
89  if (GNUNET_SYSERR ==
91  val_str,
92  (void **) &data,
93  &data_size)))
94  {
95  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Attribute value invalid!\n");
96  return GNUNET_SYSERR;
97  }
98  attr = GNUNET_RECLAIM_attribute_new (name_str, NULL,
99  type, data, data_size);
100  if ((NULL != attest_str) && (0 != strlen (attest_str)))
101  {
102  GNUNET_STRINGS_string_to_data (attest_str,
103  strlen (attest_str),
104  &attr->attestation,
105  sizeof(attr->attestation));
106  }
107  if ((NULL == id_str) || (0 == strlen (id_str)))
108  memset (&attr->id, 0, sizeof (attr->id));
109  else
111  strlen (id_str),
112  &attr->id,
113  sizeof(attr->id));
114 
115  *(struct GNUNET_RECLAIM_Attribute **) spec->ptr = attr;
116  return GNUNET_OK;
117 }
118 
119 
126 static void
127 clean_attr (void *cls, struct GNUNET_JSON_Specification *spec)
128 {
129  struct GNUNET_RECLAIM_Attribute **attr;
130 
131  attr = (struct GNUNET_RECLAIM_Attribute **) spec->ptr;
132  if (NULL != *attr)
133  {
134  GNUNET_free (*attr);
135  *attr = NULL;
136  }
137 }
138 
139 
148 {
149  struct GNUNET_JSON_Specification ret = { .parser = &parse_attr,
150  .cleaner = &clean_attr,
151  .cls = NULL,
152  .field = NULL,
153  .ptr = attr,
154  .ptr_size = 0,
155  .size_ptr = NULL };
156 
157  *attr = NULL;
158  return ret;
159 }
160 
161 
170 static int
171 parse_ticket (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
172 {
174  const char *rnd_str;
175  const char *aud_str;
176  const char *id_str;
177  int unpack_state;
178 
179  GNUNET_assert (NULL != root);
180 
181  if (! json_is_object (root))
182  {
184  "Error json is not array nor object!\n");
185  return GNUNET_SYSERR;
186  }
187  // interpret single ticket
188  unpack_state = json_unpack (root,
189  "{s:s, s:s, s:s!}",
190  "rnd",
191  &rnd_str,
192  "audience",
193  &aud_str,
194  "issuer",
195  &id_str);
196  if (0 != unpack_state)
197  {
199  "Error json object has a wrong format!\n");
200  return GNUNET_SYSERR;
201  }
202  ticket = GNUNET_new (struct GNUNET_RECLAIM_Ticket);
204  strlen (rnd_str),
205  &ticket->rnd,
206  sizeof(ticket->rnd)))
207  {
208  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Rnd invalid\n");
209  GNUNET_free (ticket);
210  return GNUNET_SYSERR;
211  }
212  if (GNUNET_OK !=
214  strlen (id_str),
215  &ticket->identity,
216  sizeof(
218  {
219  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Identity invalid\n");
220  GNUNET_free (ticket);
221  return GNUNET_SYSERR;
222  }
223 
224  if (GNUNET_OK !=
226  strlen (aud_str),
227  &ticket->audience,
228  sizeof(struct
230  {
231  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Audience invalid\n");
232  GNUNET_free (ticket);
233  return GNUNET_SYSERR;
234  }
235 
236  *(struct GNUNET_RECLAIM_Ticket **) spec->ptr = ticket;
237  return GNUNET_OK;
238 }
239 
240 
247 static void
248 clean_ticket (void *cls, struct GNUNET_JSON_Specification *spec)
249 {
250  struct GNUNET_RECLAIM_Ticket **ticket;
251 
252  ticket = (struct GNUNET_RECLAIM_Ticket **) spec->ptr;
253  if (NULL != *ticket)
254  {
255  GNUNET_free (*ticket);
256  *ticket = NULL;
257  }
258 }
259 
260 
269 {
270  struct GNUNET_JSON_Specification ret = { .parser = &parse_ticket,
271  .cleaner = &clean_ticket,
272  .cls = NULL,
273  .field = NULL,
274  .ptr = ticket,
275  .ptr_size = 0,
276  .size_ptr = NULL };
277 
278  *ticket = NULL;
279  return ret;
280 }
281 
290 static int
291 parse_attest (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
292 {
293  struct GNUNET_RECLAIM_Attestation *attr;
294  const char *name_str = NULL;
295  const char *val_str = NULL;
296  const char *type_str = NULL;
297  const char *id_str = NULL;
298  char *data;
299  int unpack_state;
300  uint32_t type;
301  size_t data_size;
302 
303  GNUNET_assert (NULL != root);
304 
305  if (! json_is_object (root))
306  {
308  "Error json is not array nor object!\n");
309  return GNUNET_SYSERR;
310  }
311  // interpret single attribute
312  unpack_state = json_unpack (root,
313  "{s:s, s?s, s:s, s:s!}",
314  "name",
315  &name_str,
316  "id",
317  &id_str,
318  "type",
319  &type_str,
320  "value",
321  &val_str);
322  if ((0 != unpack_state) || (NULL == name_str) || (NULL == val_str) ||
323  (NULL == type_str))
324  {
326  "Error json object has a wrong format!\n");
327  return GNUNET_SYSERR;
328  }
330  if (GNUNET_SYSERR ==
332  val_str,
333  (void **) &data,
334  &data_size)))
335  {
336  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Attestation value invalid!\n");
337  return GNUNET_SYSERR;
338  }
339  attr = GNUNET_RECLAIM_attestation_new (name_str, type, data, data_size);
340  if ((NULL == id_str) || (0 == strlen (id_str)))
341  memset (&attr->id, 0, sizeof (attr->id));
342  else
344  strlen (id_str),
345  &attr->id,
346  sizeof(attr->id));
347 
348  *(struct GNUNET_RECLAIM_Attestation **) spec->ptr = attr;
349  return GNUNET_OK;
350 }
351 
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 }
379 {
380  struct GNUNET_JSON_Specification ret = { .parser = &parse_attest,
381  .cleaner = &clean_attest,
382  .cls = NULL,
383  .field = NULL,
384  .ptr = attr,
385  .ptr_size = 0,
386  .size_ptr = NULL };
387 
388  *attr = NULL;
389  return ret;
390 }
391 
void * cls
Closure for parser and cleaner.
The authorization ticket.
functions to parse JSON objects into GNUnet objects
static size_t data_size
Number of bytes in data.
Definition: gnunet-abd.c:187
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static void clean_attest(void *cls, struct GNUNET_JSON_Specification *spec)
Cleanup data left from parsing RSA public key.
Definition: json_reclaim.c:359
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
Entry in parser specification for GNUNET_JSON_parse().
#define GNUNET_new(type)
Allocate a struct or union of the given type.
static void clean_attr(void *cls, struct GNUNET_JSON_Specification *spec)
Cleanup data left from parsing RSA public key.
Definition: json_reclaim.c:127
uint32_t GNUNET_RECLAIM_attestation_typename_to_number(const char *typename)
Convert an attestation type name to the corresponding number.
static int parse_attest(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to an attestation claim.
Definition: json_reclaim.c:291
struct GNUNET_RECLAIM_Identifier attestation
Referenced ID of Attestation (may be 0 if self-attested)
static struct GNUNET_RECLAIM_Ticket ticket
Ticket to consume.
int GNUNET_RECLAIM_attestation_string_to_value(uint32_t type, const char *s, void **data, size_t *data_size)
Convert human-readable version of a &#39;claim&#39; of an attestation to the binary representation.
static int parse_ticket(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to a ticket.
Definition: json_reclaim.c:171
uint32_t GNUNET_RECLAIM_attribute_typename_to_number(const char *typename)
Convert a type name to the corresponding number.
struct GNUNET_RECLAIM_Identifier id
ID.
static int parse_attr(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to a claim.
Definition: json_reclaim.c:44
GNUNET_JSON_Parser parser
Function for how to parse this type of entry.
struct GNUNET_RECLAIM_Attestation * GNUNET_RECLAIM_attestation_new(const char *name, uint32_t type, const void *data, size_t data_size)
Create a new attestation.
struct GNUNET_JSON_Specification GNUNET_RECLAIM_JSON_spec_claim(struct GNUNET_RECLAIM_Attribute **attr)
JSON Specification for Reclaim claims.
Definition: json_reclaim.c:147
int GNUNET_RECLAIM_attribute_string_to_value(uint32_t type, const char *s, void **data, size_t *data_size)
Convert human-readable version of a &#39;claim&#39; of an attribute to the binary representation.
struct GNUNET_RECLAIM_Identifier id
ID.
struct GNUNET_CRYPTO_EcdsaPublicKey identity
The ticket issuer (= the user)
static char * type_str
Attribute type.
void * ptr
Pointer, details specific to the parser.
struct GNUNET_RECLAIM_Attribute * GNUNET_RECLAIM_attribute_new(const char *attr_name, const struct GNUNET_RECLAIM_Identifier *attestation, uint32_t type, const void *data, size_t data_size)
Create a new attribute claim.
struct GNUNET_RECLAIM_Identifier rnd
The ticket random identifier.
struct GNUNET_CRYPTO_EcdsaPublicKey audience
The ticket audience (= relying party)
Public ECC key (always for Curve25519) encoded in a format suitable for network transmission and ECDS...
#define GNUNET_log(kind,...)
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
uint32_t data
The data value.
static void clean_ticket(void *cls, struct GNUNET_JSON_Specification *spec)
Cleanup data left from parsing RSA public key.
Definition: json_reclaim.c:248
int GNUNET_STRINGS_string_to_data(const char *enc, size_t enclen, void *out, size_t out_size)
Convert CrockfordBase32 encoding back to data.
Definition: strings.c:970
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_JSON_Specification GNUNET_RECLAIM_JSON_spec_claim_attest(struct GNUNET_RECLAIM_Attestation **attr)
JSON Specification for Reclaim attestation claims.
Definition: json_reclaim.c:377
struct GNUNET_JSON_Specification GNUNET_RECLAIM_JSON_spec_ticket(struct GNUNET_RECLAIM_Ticket **ticket)
JSON Specification for Reclaim tickets.
Definition: json_reclaim.c:268