GNUnet  0.19.4
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 #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 
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 *cred_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  {
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  "credential",
72  &cred_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  {
83  "Error json object has a wrong format!\n");
84  return GNUNET_SYSERR;
85  }
87  if (GNUNET_SYSERR ==
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  GNUNET_free (data);
99  if ((NULL != cred_str) && (0 != strlen (cred_str)))
100  {
102  strlen (cred_str),
103  &attr->credential,
104  sizeof(attr->credential));
105  }
106  if ((NULL == id_str) || (0 == strlen (id_str)))
107  memset (&attr->id, 0, sizeof (attr->id));
108  else
110  strlen (id_str),
111  &attr->id,
112  sizeof(attr->id));
113 
114  *(struct GNUNET_RECLAIM_Attribute **) spec->ptr = attr;
115  return GNUNET_OK;
116 }
117 
118 
125 static void
126 clean_attr (void *cls, struct GNUNET_JSON_Specification *spec)
127 {
128  struct GNUNET_RECLAIM_Attribute **attr;
129 
130  attr = (struct GNUNET_RECLAIM_Attribute **) spec->ptr;
131  if (NULL != *attr)
132  {
133  GNUNET_free (*attr);
134  *attr = NULL;
135  }
136 }
137 
138 
147 {
148  struct GNUNET_JSON_Specification ret = { .parser = &parse_attr,
149  .cleaner = &clean_attr,
150  .cls = NULL,
151  .field = NULL,
152  .ptr = attr,
153  .ptr_size = 0,
154  .size_ptr = NULL };
155 
156  *attr = NULL;
157  return ret;
158 }
159 
160 
169 static int
170 parse_ticket (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
171 {
173  const char *rnd_str;
174  const char *aud_str;
175  const char *id_str;
176  int unpack_state;
177 
178  GNUNET_assert (NULL != root);
179 
180  if (! json_is_object (root))
181  {
183  "Error json is not array nor object!\n");
184  return GNUNET_SYSERR;
185  }
186  // interpret single ticket
187  unpack_state = json_unpack (root,
188  "{s:s, s:s, s:s!}",
189  "rnd",
190  &rnd_str,
191  "audience",
192  &aud_str,
193  "issuer",
194  &id_str);
195  if (0 != unpack_state)
196  {
198  "Error json object has a wrong format!\n");
199  return GNUNET_SYSERR;
200  }
203  strlen (rnd_str),
204  &ticket->rnd,
205  sizeof(ticket->rnd)))
206  {
207  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Rnd invalid\n");
209  return GNUNET_SYSERR;
210  }
211  if (GNUNET_OK !=
213  strlen (id_str),
214  &ticket->identity,
215  sizeof(ticket->identity)))
216  {
217  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Identity invalid\n");
219  return GNUNET_SYSERR;
220  }
221 
222  if (GNUNET_OK !=
224  strlen (aud_str),
225  &ticket->audience,
226  sizeof(ticket->audience)))
227  {
228  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Audience invalid\n");
230  return GNUNET_SYSERR;
231  }
232 
233  *(struct GNUNET_RECLAIM_Ticket **) spec->ptr = ticket;
234  return GNUNET_OK;
235 }
236 
237 
244 static void
245 clean_ticket (void *cls, struct GNUNET_JSON_Specification *spec)
246 {
247  struct GNUNET_RECLAIM_Ticket **ticket;
248 
249  ticket = (struct GNUNET_RECLAIM_Ticket **) spec->ptr;
250  if (NULL != *ticket)
251  {
252  GNUNET_free (*ticket);
253  *ticket = NULL;
254  }
255 }
256 
257 
266 {
267  struct GNUNET_JSON_Specification ret = { .parser = &parse_ticket,
268  .cleaner = &clean_ticket,
269  .cls = NULL,
270  .field = NULL,
271  .ptr = ticket,
272  .ptr_size = 0,
273  .size_ptr = NULL };
274 
275  *ticket = NULL;
276  return ret;
277 }
278 
279 
288 static int
289 parse_credential (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
290 {
291  struct GNUNET_RECLAIM_Credential *cred;
292  const char *name_str = NULL;
293  const char *type_str = NULL;
294  const char *id_str = NULL;
295  json_t *val_json;
296  char *data = NULL;
297  char *val_str = NULL;
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  {
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:o!}",
313  "name",
314  &name_str,
315  "id",
316  &id_str,
317  "type",
318  &type_str,
319  "value",
320  &val_json);
321  if ((0 != unpack_state) || (NULL == name_str) || (NULL == val_json) ||
322  (NULL == type_str))
323  {
325  "Error json object has a wrong format!\n");
326  return GNUNET_SYSERR;
327  }
328  if (json_is_string (val_json)) {
329  val_str = GNUNET_strdup (json_string_value (val_json));
330  } else {
331  val_str = json_dumps (val_json, JSON_COMPACT);
332  }
334  if (GNUNET_SYSERR ==
336  val_str,
337  (void **) &data,
338  &data_size)))
339  {
340  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Credential value invalid!\n");
341  return GNUNET_SYSERR;
342  }
343  cred = GNUNET_RECLAIM_credential_new (name_str, type, data, data_size);
344  GNUNET_free (data);
345  if ((NULL == id_str) || (0 == strlen (id_str)))
346  memset (&cred->id, 0, sizeof (cred->id));
347  else
349  strlen (id_str),
350  &cred->id,
351  sizeof(cred->id));
352 
353  *(struct GNUNET_RECLAIM_Credential **) spec->ptr = cred;
354  return GNUNET_OK;
355 }
356 
357 
364 static void
366 {
367  struct GNUNET_RECLAIM_Credential **attr;
368 
369  attr = (struct GNUNET_RECLAIM_Credential **) spec->ptr;
370  if (NULL != *attr)
371  {
372  GNUNET_free (*attr);
373  *attr = NULL;
374  }
375 }
376 
377 
387 {
388  struct GNUNET_JSON_Specification ret = { .parser = &parse_credential,
389  .cleaner = &clean_credential,
390  .cls = NULL,
391  .field = NULL,
392  .ptr = cred,
393  .ptr_size = 0,
394  .size_ptr = NULL };
395 
396  *cred = NULL;
397  return ret;
398 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static size_t data_size
Number of bytes in data.
Definition: gnunet-abd.c:187
uint32_t data
The data value.
static struct GNUNET_RECLAIM_Ticket ticket
Ticket to consume.
static char * type_str
Attribute type.
functions to parse JSON objects into GNUnet objects
Identity attribute definitions.
reclaim service; implements identity and personal data sharing for GNUnet
#define GNUNET_log(kind,...)
@ GNUNET_OK
@ GNUNET_SYSERR
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
int GNUNET_RECLAIM_credential_string_to_value(uint32_t type, const char *s, void **data, size_t *data_size)
Convert human-readable version of a 'claim' of an credential to the binary representation.
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 'claim' of an attribute to the binary representation.
struct GNUNET_RECLAIM_Credential * GNUNET_RECLAIM_credential_new(const char *name, uint32_t type, const void *data, size_t data_size)
Create a new credential.
struct GNUNET_RECLAIM_Attribute * GNUNET_RECLAIM_attribute_new(const char *attr_name, const struct GNUNET_RECLAIM_Identifier *credential, uint32_t type, const void *data, size_t data_size)
Create a new attribute claim.
uint32_t GNUNET_RECLAIM_credential_typename_to_number(const char *typename)
Convert an credential type name to the corresponding number.
uint32_t GNUNET_RECLAIM_attribute_typename_to_number(const char *typename)
Convert a type name to the corresponding number.
enum GNUNET_GenericReturnValue 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:788
struct GNUNET_JSON_Specification GNUNET_RECLAIM_JSON_spec_credential(struct GNUNET_RECLAIM_Credential **cred)
JSON Specification for credential claims.
Definition: json_reclaim.c:385
static void clean_attr(void *cls, struct GNUNET_JSON_Specification *spec)
Cleanup data left from parsing RSA public key.
Definition: json_reclaim.c:126
static int parse_credential(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Parse given JSON object to a credential claim.
Definition: json_reclaim.c:289
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:42
struct GNUNET_JSON_Specification GNUNET_RECLAIM_JSON_spec_attribute(struct GNUNET_RECLAIM_Attribute **attr)
JSON Specification for Reclaim claims.
Definition: json_reclaim.c:146
static void clean_credential(void *cls, struct GNUNET_JSON_Specification *spec)
Cleanup data left from parsing RSA public key.
Definition: json_reclaim.c:365
struct GNUNET_JSON_Specification GNUNET_RECLAIM_JSON_spec_ticket(struct GNUNET_RECLAIM_Ticket **ticket)
JSON Specification for Reclaim tickets.
Definition: json_reclaim.c:265
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:170
static void clean_ticket(void *cls, struct GNUNET_JSON_Specification *spec)
Cleanup data left from parsing RSA public key.
Definition: json_reclaim.c:245
Entry in parser specification for GNUNET_JSON_parse().
void * ptr
Pointer, details specific to the parser.
void * cls
Closure for parser and cleaner.
struct GNUNET_RECLAIM_Identifier credential
Referenced ID of credential (may be GNUNET_RECLAIM_ID_ZERO if self-creded)
struct GNUNET_RECLAIM_Identifier id
ID.
struct GNUNET_RECLAIM_Identifier id
ID.
The authorization ticket.
struct GNUNET_RECLAIM_Identifier rnd
The ticket random identifier.
struct GNUNET_IDENTITY_PublicKey audience
The ticket audience (= relying party)
struct GNUNET_IDENTITY_PublicKey identity
The ticket issuer (= the user)
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model