aboutsummaryrefslogtreecommitdiff
path: root/src/reclaim/json_reclaim.c
blob: b1ca7a4a5431ad2cf0eb2145ab531e0f5e6eaf8a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
   This file is part of GNUnet.
   Copyright (C) 2009-2018 GNUnet e.V.

   GNUnet is free software: you can redistribute it and/or modify it
   under the terms of the GNU Affero General Public License as published
   by the Free Software Foundation, either version 3 of the License,
   or (at your option) any later version.

   GNUnet is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

   SPDX-License-Identifier: AGPL3.0-or-later
 */

/**
 * @file rest-plugins/json_reclaim.c
 * @brief JSON handling of reclaim data
 * @author Martin Schanzenbach
 */
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_json_lib.h"
#include "gnunet_reclaim_lib.h"
#include "gnunet_reclaim_service.h"


/**
 * Parse given JSON object to a claim
 *
 * @param cls closure, NULL
 * @param root the json object representing data
 * @param spec where to write the data
 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
 */
static int
parse_attr (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
{
  struct GNUNET_RECLAIM_Attribute *attr;
  const char *name_str = NULL;
  const char *val_str = NULL;
  const char *type_str = NULL;
  const char *id_str = NULL;
  const char *cred_str = NULL;
  const char *flag_str = NULL;
  char *data;
  int unpack_state;
  uint32_t type;
  size_t data_size;

  GNUNET_assert (NULL != root);

  if (! json_is_object (root))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Error json is not array nor object!\n");
    return GNUNET_SYSERR;
  }
  // interpret single attribute
  unpack_state = json_unpack (root,
                              "{s:s, s?s, s?s, s:s, s:s, s?s!}",
                              "name",
                              &name_str,
                              "id",
                              &id_str,
                              "credential",
                              &cred_str,
                              "type",
                              &type_str,
                              "value",
                              &val_str,
                              "flag",
                              &flag_str);
  if ((0 != unpack_state) || (NULL == name_str) || (NULL == val_str) ||
      (NULL == type_str))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Error json object has a wrong format!\n");
    return GNUNET_SYSERR;
  }
  type = GNUNET_RECLAIM_attribute_typename_to_number (type_str);
  if (GNUNET_SYSERR ==
      (GNUNET_RECLAIM_attribute_string_to_value (type,
                                                 val_str,
                                                 (void **) &data,
                                                 &data_size)))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Attribute value invalid!\n");
    return GNUNET_SYSERR;
  }
  attr = GNUNET_RECLAIM_attribute_new (name_str, NULL,
                                       type, data, data_size);
  GNUNET_free (data);
  if ((NULL != cred_str) && (0 != strlen (cred_str)))
  {
    GNUNET_STRINGS_string_to_data (cred_str,
                                   strlen (cred_str),
                                   &attr->credential,
                                   sizeof(attr->credential));
  }
  if ((NULL == id_str) || (0 == strlen (id_str)))
    memset (&attr->id, 0, sizeof (attr->id));
  else
    GNUNET_STRINGS_string_to_data (id_str,
                                   strlen (id_str),
                                   &attr->id,
                                   sizeof(attr->id));

  *(struct GNUNET_RECLAIM_Attribute **) spec->ptr = attr;
  return GNUNET_OK;
}


/**
 * Cleanup data left from parsing RSA public key.
 *
 * @param cls closure, NULL
 * @param[out] spec where to free the data
 */
static void
clean_attr (void *cls, struct GNUNET_JSON_Specification *spec)
{
  struct GNUNET_RECLAIM_Attribute **attr;

  attr = (struct GNUNET_RECLAIM_Attribute **) spec->ptr;
  if (NULL != *attr)
  {
    GNUNET_free (*attr);
    *attr = NULL;
  }
}


/**
 * JSON Specification for Reclaim claims.
 *
 * @param ticket struct of GNUNET_RECLAIM_Attribute to fill
 * @return JSON Specification
 */
struct GNUNET_JSON_Specification
GNUNET_RECLAIM_JSON_spec_attribute (struct GNUNET_RECLAIM_Attribute **attr)
{
  struct GNUNET_JSON_Specification ret = { .parser = &parse_attr,
                                           .cleaner = &clean_attr,
                                           .cls = NULL,
                                           .field = NULL,
                                           .ptr = attr,
                                           .ptr_size = 0,
                                           .size_ptr = NULL };

  *attr = NULL;
  return ret;
}


/**
 * Parse given JSON object to a ticket
 *
 * @param cls closure, NULL
 * @param root the json object representing data
 * @param spec where to write the data
 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
 */
static int
parse_ticket (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
{
  struct GNUNET_RECLAIM_Ticket *ticket;
  const char *rnd_str;
  const char *aud_str;
  const char *id_str;
  int unpack_state;

  GNUNET_assert (NULL != root);

  if (! json_is_object (root))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Error json is not array nor object!\n");
    return GNUNET_SYSERR;
  }
  // interpret single ticket
  unpack_state = json_unpack (root,
                              "{s:s, s:s, s:s!}",
                              "rnd",
                              &rnd_str,
                              "audience",
                              &aud_str,
                              "issuer",
                              &id_str);
  if (0 != unpack_state)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Error json object has a wrong format!\n");
    return GNUNET_SYSERR;
  }
  ticket = GNUNET_new (struct GNUNET_RECLAIM_Ticket);
  if (GNUNET_OK != GNUNET_STRINGS_string_to_data (rnd_str,
                                                  strlen (rnd_str),
                                                  &ticket->rnd,
                                                  sizeof(ticket->rnd)))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Rnd invalid\n");
    GNUNET_free (ticket);
    return GNUNET_SYSERR;
  }
  if (GNUNET_OK !=
      GNUNET_STRINGS_string_to_data (id_str,
                                     strlen (id_str),
                                     &ticket->identity,
                                     sizeof(ticket->identity)))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Identity invalid\n");
    GNUNET_free (ticket);
    return GNUNET_SYSERR;
  }

  if (GNUNET_OK !=
      GNUNET_STRINGS_string_to_data (aud_str,
                                     strlen (aud_str),
                                     &ticket->audience,
                                     sizeof(ticket->audience)))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Audience invalid\n");
    GNUNET_free (ticket);
    return GNUNET_SYSERR;
  }

  *(struct GNUNET_RECLAIM_Ticket **) spec->ptr = ticket;
  return GNUNET_OK;
}


/**
 * Cleanup data left from parsing RSA public key.
 *
 * @param cls closure, NULL
 * @param[out] spec where to free the data
 */
static void
clean_ticket (void *cls, struct GNUNET_JSON_Specification *spec)
{
  struct GNUNET_RECLAIM_Ticket **ticket;

  ticket = (struct GNUNET_RECLAIM_Ticket **) spec->ptr;
  if (NULL != *ticket)
  {
    GNUNET_free (*ticket);
    *ticket = NULL;
  }
}


/**
 * JSON Specification for Reclaim tickets.
 *
 * @param ticket struct of GNUNET_RECLAIM_Ticket to fill
 * @return JSON Specification
 */
struct GNUNET_JSON_Specification
GNUNET_RECLAIM_JSON_spec_ticket (struct GNUNET_RECLAIM_Ticket **ticket)
{
  struct GNUNET_JSON_Specification ret = { .parser = &parse_ticket,
                                           .cleaner = &clean_ticket,
                                           .cls = NULL,
                                           .field = NULL,
                                           .ptr = ticket,
                                           .ptr_size = 0,
                                           .size_ptr = NULL };

  *ticket = NULL;
  return ret;
}


/**
   * Parse given JSON object to a credential claim
   *
   * @param cls closure, NULL
   * @param root the json object representing data
   * @param spec where to write the data
   * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
   */
static int
parse_credential (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
{
  struct GNUNET_RECLAIM_Credential *cred;
  const char *name_str = NULL;
  const char *type_str = NULL;
  const char *id_str = NULL;
  json_t *val_json;
  char *data = NULL;
  char *val_str = NULL;
  int unpack_state;
  uint32_t type;
  size_t data_size;

  GNUNET_assert (NULL != root);

  if (! json_is_object (root))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Error json is not array nor object!\n");
    return GNUNET_SYSERR;
  }
  // interpret single attribute
  unpack_state = json_unpack (root,
                              "{s:s, s?s, s:s, s:o!}",
                              "name",
                              &name_str,
                              "id",
                              &id_str,
                              "type",
                              &type_str,
                              "value",
                              &val_json);
  if ((0 != unpack_state) || (NULL == name_str) || (NULL == val_json) ||
      (NULL == type_str))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Error json object has a wrong format!\n");
    return GNUNET_SYSERR;
  }
  if (json_is_string (val_json)) {
    val_str = GNUNET_strdup (json_string_value (val_json));
  } else {
    val_str = json_dumps (val_json, JSON_COMPACT);
  }
  type = GNUNET_RECLAIM_credential_typename_to_number (type_str);
  if (GNUNET_SYSERR ==
      (GNUNET_RECLAIM_credential_string_to_value (type,
                                                  val_str,
                                                  (void **) &data,
                                                  &data_size)))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Credential value invalid!\n");
    return GNUNET_SYSERR;
  }
  cred = GNUNET_RECLAIM_credential_new (name_str, type, data, data_size);
  GNUNET_free (data);
  if ((NULL == id_str) || (0 == strlen (id_str)))
    memset (&cred->id, 0, sizeof (cred->id));
  else
    GNUNET_STRINGS_string_to_data (id_str,
                                   strlen (id_str),
                                   &cred->id,
                                   sizeof(cred->id));

  *(struct GNUNET_RECLAIM_Credential **) spec->ptr = cred;
  return GNUNET_OK;
}


/**
 * Cleanup data left from parsing RSA public key.
 *
 * @param cls closure, NULL
 * @param[out] spec where to free the data
 */
static void
clean_credential (void *cls, struct GNUNET_JSON_Specification *spec)
{
  struct GNUNET_RECLAIM_Credential **attr;

  attr = (struct GNUNET_RECLAIM_Credential **) spec->ptr;
  if (NULL != *attr)
  {
    GNUNET_free (*attr);
    *attr = NULL;
  }
}


/**
 * JSON Specification for credential claims.
 *
 * @param attr struct of GNUNET_RECLAIM_Credential to fill
 * @return JSON Specification
 */
struct GNUNET_JSON_Specification
GNUNET_RECLAIM_JSON_spec_credential (struct
                                     GNUNET_RECLAIM_Credential **cred)
{
  struct GNUNET_JSON_Specification ret = { .parser = &parse_credential,
                                           .cleaner = &clean_credential,
                                           .cls = NULL,
                                           .field = NULL,
                                           .ptr = cred,
                                           .ptr_size = 0,
                                           .size_ptr = NULL };

  *cred = NULL;
  return ret;
}