GNUnet  0.10.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  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!}",
67  "name",
68  &name_str,
69  "id",
70  &id_str,
71  "type",
72  &type_str,
73  "value",
74  &val_str);
75  if ((0 != unpack_state) || (NULL == name_str) || (NULL == val_str) ||
76  (NULL == type_str))
77  {
79  "Error json object has a wrong format!\n");
80  return GNUNET_SYSERR;
81  }
83  if (GNUNET_SYSERR ==
85  val_str,
86  (void **) &data,
87  &data_size)))
88  {
89  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Attribute value invalid!\n");
90  return GNUNET_SYSERR;
91  }
92  attr = GNUNET_RECLAIM_ATTRIBUTE_claim_new (name_str, type, data, data_size);
93  if ((NULL == id_str) || (0 == strlen (id_str)))
94  attr->id = 0;
95  else
97  strlen (id_str),
98  &attr->id,
99  sizeof (uint64_t));
100 
101  *(struct GNUNET_RECLAIM_ATTRIBUTE_Claim **) spec->ptr = attr;
102  return GNUNET_OK;
103 }
104 
111 static void
112 clean_attr (void *cls, struct GNUNET_JSON_Specification *spec)
113 {
114  struct GNUNET_RECLAIM_ATTRIBUTE_Claim **attr;
115  attr = (struct GNUNET_RECLAIM_ATTRIBUTE_Claim **) spec->ptr;
116  if (NULL != *attr)
117  {
118  GNUNET_free (*attr);
119  *attr = NULL;
120  }
121 }
122 
131 {
132  struct GNUNET_JSON_Specification ret = {.parser = &parse_attr,
133  .cleaner = &clean_attr,
134  .cls = NULL,
135  .field = NULL,
136  .ptr = attr,
137  .ptr_size = 0,
138  .size_ptr = NULL};
139  *attr = NULL;
140  return ret;
141 }
150 static int
151 parse_ticket (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
152 {
154  const char *rnd_str;
155  const char *aud_str;
156  const char *id_str;
157  int unpack_state;
158 
159  GNUNET_assert (NULL != root);
160 
161  if (! json_is_object (root))
162  {
164  "Error json is not array nor object!\n");
165  return GNUNET_SYSERR;
166  }
167  // interpret single ticket
168  unpack_state = json_unpack (root,
169  "{s:s, s:s, s:s!}",
170  "rnd",
171  &rnd_str,
172  "audience",
173  &aud_str,
174  "issuer",
175  &id_str);
176  if (0 != unpack_state)
177  {
179  "Error json object has a wrong format!\n");
180  return GNUNET_SYSERR;
181  }
182  ticket = GNUNET_new (struct GNUNET_RECLAIM_Ticket);
184  strlen (rnd_str),
185  &ticket->rnd,
186  sizeof (uint64_t)))
187  {
188  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Rnd invalid\n");
189  GNUNET_free (ticket);
190  return GNUNET_SYSERR;
191  }
192  if (GNUNET_OK !=
194  strlen (id_str),
195  &ticket->identity,
196  sizeof (
198  {
199  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Identity invalid\n");
200  GNUNET_free (ticket);
201  return GNUNET_SYSERR;
202  }
203 
204  if (GNUNET_OK !=
206  strlen (aud_str),
207  &ticket->audience,
208  sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
209  {
210  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Audience invalid\n");
211  GNUNET_free (ticket);
212  return GNUNET_SYSERR;
213  }
214 
215  *(struct GNUNET_RECLAIM_Ticket **) spec->ptr = ticket;
216  return GNUNET_OK;
217 }
218 
225 static void
226 clean_ticket (void *cls, struct GNUNET_JSON_Specification *spec)
227 {
228  struct GNUNET_RECLAIM_Ticket **ticket;
229  ticket = (struct GNUNET_RECLAIM_Ticket **) spec->ptr;
230  if (NULL != *ticket)
231  {
232  GNUNET_free (*ticket);
233  *ticket = NULL;
234  }
235 }
236 
245 {
247  .cleaner = &clean_ticket,
248  .cls = NULL,
249  .field = NULL,
250  .ptr = ticket,
251  .ptr_size = 0,
252  .size_ptr = NULL};
253  *ticket = NULL;
254  return ret;
255 }
void * cls
Closure for parser and cleaner.
The authorization ticket.
functions to parse JSON objects into GNUnet objects
#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.
Entry in parser specification for GNUNET_JSON_parse().
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
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.
static void clean_attr(void *cls, struct GNUNET_JSON_Specification *spec)
Cleanup data left from parsing RSA public key.
Definition: json_reclaim.c:112
static int ret
Final status code.
Definition: gnunet-arm.c:89
static struct GNUNET_RECLAIM_Ticket ticket
Ticket to consume.
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:151
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:79
uint64_t rnd
The ticket random (NBO)
static char * type_str
Attribute type.
void * ptr
Pointer, details specific to the parser.
struct GNUNET_JSON_Specification GNUNET_RECLAIM_JSON_spec_claim(struct GNUNET_RECLAIM_ATTRIBUTE_Claim **attr)
JSON Specification for Reclaim claims.
Definition: json_reclaim.c:130
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,...)
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:226
static size_t data_size
Number of bytes in data.
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:1021
#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:244