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"
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_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;
51  const char *flag_str = NULL;
52  char *data;
53  int unpack_state;
54  uint32_t type;
55  size_t data_size;
56 
57  GNUNET_assert (NULL != root);
58 
59  if (! json_is_object (root))
60  {
62  "Error json is not array nor object!\n");
63  return GNUNET_SYSERR;
64  }
65  // interpret single attribute
66  unpack_state = json_unpack (root,
67  "{s:s, s?s, s:s, s:s, s?s!}",
68  "name",
69  &name_str,
70  "id",
71  &id_str,
72  "type",
73  &type_str,
74  "value",
75  &val_str,
76  "flag",
77  &flag_str);
78  if ((0 != unpack_state) || (NULL == name_str) || (NULL == val_str) ||
79  (NULL == type_str))
80  {
82  "Error json object has a wrong format!\n");
83  return GNUNET_SYSERR;
84  }
86  if (GNUNET_SYSERR ==
88  val_str,
89  (void **) &data,
90  &data_size)))
91  {
92  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Attribute value invalid!\n");
93  return GNUNET_SYSERR;
94  }
95  attr = GNUNET_RECLAIM_ATTRIBUTE_claim_new (name_str, type, data, data_size);
96  if ((NULL == id_str) || (0 == strlen (id_str)))
97  attr->id = 0;
98  else
100  strlen (id_str),
101  &attr->id,
102  sizeof(uint64_t));
103 
104  *(struct GNUNET_RECLAIM_ATTRIBUTE_Claim **) spec->ptr = attr;
105  return GNUNET_OK;
106 }
107 
108 
115 static void
116 clean_attr (void *cls, struct GNUNET_JSON_Specification *spec)
117 {
118  struct GNUNET_RECLAIM_ATTRIBUTE_Claim **attr;
119 
120  attr = (struct GNUNET_RECLAIM_ATTRIBUTE_Claim **) spec->ptr;
121  if (NULL != *attr)
122  {
123  GNUNET_free (*attr);
124  *attr = NULL;
125  }
126 }
127 
128 
137 {
138  struct GNUNET_JSON_Specification ret = { .parser = &parse_attr,
139  .cleaner = &clean_attr,
140  .cls = NULL,
141  .field = NULL,
142  .ptr = attr,
143  .ptr_size = 0,
144  .size_ptr = NULL };
145 
146  *attr = NULL;
147  return ret;
148 }
149 
150 
159 static int
160 parse_ticket (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
161 {
163  const char *rnd_str;
164  const char *aud_str;
165  const char *id_str;
166  int unpack_state;
167 
168  GNUNET_assert (NULL != root);
169 
170  if (! json_is_object (root))
171  {
173  "Error json is not array nor object!\n");
174  return GNUNET_SYSERR;
175  }
176  // interpret single ticket
177  unpack_state = json_unpack (root,
178  "{s:s, s:s, s:s!}",
179  "rnd",
180  &rnd_str,
181  "audience",
182  &aud_str,
183  "issuer",
184  &id_str);
185  if (0 != unpack_state)
186  {
188  "Error json object has a wrong format!\n");
189  return GNUNET_SYSERR;
190  }
191  ticket = GNUNET_new (struct GNUNET_RECLAIM_Ticket);
193  strlen (rnd_str),
194  &ticket->rnd,
195  sizeof(uint64_t)))
196  {
197  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Rnd invalid\n");
198  GNUNET_free (ticket);
199  return GNUNET_SYSERR;
200  }
201  if (GNUNET_OK !=
203  strlen (id_str),
204  &ticket->identity,
205  sizeof(
207  {
208  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Identity invalid\n");
209  GNUNET_free (ticket);
210  return GNUNET_SYSERR;
211  }
212 
213  if (GNUNET_OK !=
215  strlen (aud_str),
216  &ticket->audience,
217  sizeof(struct
219  {
220  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Audience invalid\n");
221  GNUNET_free (ticket);
222  return GNUNET_SYSERR;
223  }
224 
225  *(struct GNUNET_RECLAIM_Ticket **) spec->ptr = ticket;
226  return GNUNET_OK;
227 }
228 
229 
236 static void
237 clean_ticket (void *cls, struct GNUNET_JSON_Specification *spec)
238 {
239  struct GNUNET_RECLAIM_Ticket **ticket;
240 
241  ticket = (struct GNUNET_RECLAIM_Ticket **) spec->ptr;
242  if (NULL != *ticket)
243  {
244  GNUNET_free (*ticket);
245  *ticket = NULL;
246  }
247 }
248 
249 
258 {
259  struct GNUNET_JSON_Specification ret = { .parser = &parse_ticket,
260  .cleaner = &clean_ticket,
261  .cls = NULL,
262  .field = NULL,
263  .ptr = ticket,
264  .ptr_size = 0,
265  .size_ptr = NULL };
266 
267  *ticket = NULL;
268  return ret;
269 }
270 
279 static int
280 parse_attest (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
281 {
283  const char *name_str = NULL;
284  const char *val_str = NULL;
285  const char *type_str = NULL;
286  const char *id_str = NULL;
287  char *data;
288  int unpack_state;
289  uint32_t type;
290  size_t data_size;
291 
292  GNUNET_assert (NULL != root);
293 
294  if (! json_is_object (root))
295  {
297  "Error json is not array nor object!\n");
298  return GNUNET_SYSERR;
299  }
300  // interpret single attribute
301  unpack_state = json_unpack (root,
302  "{s:s, s?s, s:s, s:s!}",
303  "name",
304  &name_str,
305  "id",
306  &id_str,
307  "type",
308  &type_str,
309  "value",
310  &val_str);
311  if ((0 != unpack_state) || (NULL == name_str) || (NULL == val_str) ||
312  (NULL == type_str))
313  {
315  "Error json object has a wrong format!\n");
316  return GNUNET_SYSERR;
317  }
319  if (GNUNET_SYSERR ==
321  val_str,
322  (void **) &data,
323  &data_size)))
324  {
325  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Attestation value invalid!\n");
326  return GNUNET_SYSERR;
327  }
328  attr = GNUNET_RECLAIM_ATTESTATION_claim_new (name_str, type, data, data_size);
329  if ((NULL == id_str) || (0 == strlen (id_str)))
330  attr->id = 0;
331  else
333  strlen (id_str),
334  &attr->id,
335  sizeof(uint64_t));
336 
337  *(struct GNUNET_RECLAIM_ATTESTATION_Claim **) spec->ptr = attr;
338  return GNUNET_OK;
339 }
340 
347 static void
348 clean_attest (void *cls, struct GNUNET_JSON_Specification *spec)
349 {
350  struct GNUNET_RECLAIM_ATTESTATION_Claim **attr;
351 
352  attr = (struct GNUNET_RECLAIM_ATTESTATION_Claim **) spec->ptr;
353  if (NULL != *attr)
354  {
355  GNUNET_free (*attr);
356  *attr = NULL;
357  }
358 }
368 {
369  struct GNUNET_JSON_Specification ret = { .parser = &parse_attest,
370  .cleaner = &clean_attest,
371  .cls = NULL,
372  .field = NULL,
373  .ptr = attr,
374  .ptr_size = 0,
375  .size_ptr = NULL };
376 
377  *attr = NULL;
378  return ret;
379 }
380 
389 static int
390 parse_attest_ref (void *cls, json_t *root, struct
392 {
394  const char *name_str = NULL;
395  const char *ref_val_str = NULL;
396  const char *ref_id_str = NULL;
397  const char *id_str = NULL;
398  int unpack_state;
399 
400  GNUNET_assert (NULL != root);
401 
402  if (! json_is_object (root))
403  {
405  "Error json is not array nor object!\n");
406  return GNUNET_SYSERR;
407  }
408  // interpret single reference
409  unpack_state = json_unpack (root,
410  "{s:s, s?s, s:s, s:s!}",
411  "name",
412  &name_str,
413  "id",
414  &id_str,
415  "ref_id",
416  &ref_id_str,
417  "ref_value",
418  &ref_val_str);
419  if ((0 != unpack_state) || (NULL == name_str) || (NULL == ref_val_str) ||
420  (NULL == ref_id_str))
421  {
423  "Error json object has a wrong format!\n");
424  return GNUNET_SYSERR;
425  }
426 
427  attr = GNUNET_RECLAIM_ATTESTATION_reference_new (name_str, ref_val_str);
428 
429  attr->id = 0;
430 
431  if ((NULL == ref_id_str) || (0 == strlen (ref_id_str)))
432  attr->id_attest = 0;
433  else
434  GNUNET_STRINGS_string_to_data (ref_id_str,
435  strlen (ref_id_str),
436  &attr->id_attest,
437  sizeof(uint64_t));
438 
439  *(struct GNUNET_RECLAIM_ATTESTATION_REFERENCE **) spec->ptr = attr;
440  return GNUNET_OK;
441 }
442 
449 static void
451 {
453 
454  attr = (struct GNUNET_RECLAIM_ATTESTATION_REFERENCE **) spec->ptr;
455  if (NULL != *attr)
456  {
457  GNUNET_free (*attr);
458  *attr = NULL;
459  }
460 }
461 
471  **attr)
472 {
474  .cleaner = &clean_attest_ref,
475  .cls = NULL,
476  .field = NULL,
477  .ptr = attr,
478  .ptr_size = 0,
479  .size_ptr = NULL };
480 
481  *attr = NULL;
482  return ret;
483 }
void * cls
Closure for parser and cleaner.
struct GNUNET_RECLAIM_ATTESTATION_Claim * GNUNET_RECLAIM_ATTESTATION_claim_new(const char *attr_name, uint32_t type, const void *data, size_t data_size)
Create a new attestation.
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.
uint32_t GNUNET_RECLAIM_ATTRIBUTE_typename_to_number(const char *typename)
Convert a type name to the corresponding number.
static void clean_attest(void *cls, struct GNUNET_JSON_Specification *spec)
Cleanup data left from parsing RSA public key.
Definition: json_reclaim.c:348
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
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.
Entry in parser specification for GNUNET_JSON_parse().
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:83
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.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
uint64_t id_attest
Referenced ID of Attestation.
static void clean_attr(void *cls, struct GNUNET_JSON_Specification *spec)
Cleanup data left from parsing RSA public key.
Definition: json_reclaim.c:116
struct GNUNET_JSON_Specification GNUNET_RECLAIM_JSON_spec_claim_attest_ref(struct GNUNET_RECLAIM_ATTESTATION_REFERENCE **attr)
JSON Specification for Reclaim attestation references.
Definition: json_reclaim.c:469
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:280
static struct GNUNET_RECLAIM_Ticket ticket
Ticket to consume.
struct GNUNET_RECLAIM_ATTESTATION_REFERENCE * GNUNET_RECLAIM_ATTESTATION_reference_new(const char *attr_name, const char *ref_value)
Create a new attestation reference.
static void clean_attest_ref(void *cls, struct GNUNET_JSON_Specification *spec)
Cleanup data left from parsing RSA public key.
Definition: json_reclaim.c:450
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:160
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_CRYPTO_EcdsaPublicKey identity
The ticket issuer (= the user)
#define GNUNET_SYSERR
Definition: gnunet_common.h:84
uint64_t rnd
The ticket random (NBO)
static char * type_str
Attribute type.
void * ptr
Pointer, details specific to the parser.
static int parse_attest_ref(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to an attestation claim.
Definition: json_reclaim.c:390
struct GNUNET_JSON_Specification GNUNET_RECLAIM_JSON_spec_claim(struct GNUNET_RECLAIM_ATTRIBUTE_Claim **attr)
JSON Specification for Reclaim claims.
Definition: json_reclaim.c:136
struct GNUNET_RECLAIM_ATTRIBUTE_Claim * GNUNET_RECLAIM_ATTRIBUTE_claim_new(const char *attr_name, uint32_t type, const void *data, size_t data_size)
Create a new attribute claim.
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,...)
struct GNUNET_JSON_Specification GNUNET_RECLAIM_JSON_spec_claim_attest(struct GNUNET_RECLAIM_ATTESTATION_Claim **attr)
JSON Specification for Reclaim attestation claims.
Definition: json_reclaim.c:366
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:237
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:952
uint32_t GNUNET_RECLAIM_ATTESTATION_typename_to_number(const char *typename)
Convert an attestation type name to the corresponding number.
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_JSON_Specification GNUNET_RECLAIM_JSON_spec_ticket(struct GNUNET_RECLAIM_Ticket **ticket)
JSON Specification for Reclaim tickets.
Definition: json_reclaim.c:257