aboutsummaryrefslogtreecommitdiff
path: root/src/include/gnunet_reclaim_lib.h
blob: 7d521ffa81b30e8f8bbbd5bf1edb3ca518b92f4a (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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/*
     This file is part of GNUnet.
     Copyright (C) 2017 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
 */

/**
 * @author Martin Schanzenbach
 *
 * @file
 * Identity attribute definitions
 *
 * @defgroup reclaim-attribute reclaim attributes
 * @{
 */
#ifndef GNUNET_RECLAIM_ATTRIBUTE_LIB_H
#define GNUNET_RECLAIM_ATTRIBUTE_LIB_H

#ifdef __cplusplus
extern "C" {
#if 0 /* keep Emacsens' auto-indent happy */
}
#endif
#endif

#include "gnunet_util_lib.h"


/**
 * No value attribute.
 */
#define GNUNET_RECLAIM_ATTRIBUTE_TYPE_NONE 0

/**
 * String attribute.
 */
#define GNUNET_RECLAIM_ATTRIBUTE_TYPE_STRING 1

/**
* No value attestation.
*/
#define GNUNET_RECLAIM_ATTESTATION_TYPE_NONE 10

/**
* A JSON Web Token attestation.
*/
#define GNUNET_RECLAIM_ATTESTATION_TYPE_JWT 11

/**
 * We want an ID to be a 256-bit symmetric key
 */
#define GNUNET_RECLAIM_ID_LENGTH (256 / 8)

GNUNET_NETWORK_STRUCT_BEGIN
/**
 * A reclaim identifier
 * FIXME maybe put this in a different namespace
 */
struct GNUNET_RECLAIM_Identifier
{
  char id[GNUNET_RECLAIM_ID_LENGTH];
};

GNUNET_NETWORK_STRUCT_END

static const struct GNUNET_RECLAIM_Identifier GNUNET_RECLAIM_ID_ZERO;

#define GNUNET_RECLAIM_id_is_equal(a,b) ((0 == \
                                          memcmp (a, \
                                                  b, \
                                                  sizeof (GNUNET_RECLAIM_ID_ZERO))) \
                                         ? \
                                         GNUNET_YES : GNUNET_NO)


#define GNUNET_RECLAIM_id_is_zero(a) GNUNET_RECLAIM_id_is_equal (a, \
                                                                 & \
                                                                 GNUNET_RECLAIM_ID_ZERO)

#define GNUNET_RECLAIM_id_generate(id) \
  (GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_STRONG, \
                               id, \
                               sizeof (GNUNET_RECLAIM_ID_ZERO)))

/**
 * An attribute.
 */
struct GNUNET_RECLAIM_Attribute
{
  /**
   * ID
   */
  struct GNUNET_RECLAIM_Identifier id;

  /**
   * Referenced ID of Attestation (may be 0 if self-attested)
   */
  struct GNUNET_RECLAIM_Identifier attestation;

  /**
   * Type of Claim
   */
  uint32_t type;

  /**
   * Flags
   */
  uint32_t flag;

  /**
   * The name of the attribute. Note "name" must never be individually
   * free'd
   */
  const char *name;

  /**
   * Number of bytes in @e data.
   */
  size_t data_size;

  /**
   * Binary value stored as attribute value.  Note: "data" must never
   * be individually 'malloc'ed, but instead always points into some
   * existing data area.
   */
  const void *data;
};

/**
 * An attestation.
 */
struct GNUNET_RECLAIM_Attestation
{
  /**
   * ID
   */
  struct GNUNET_RECLAIM_Identifier id;

  /**
   * Type/Format of Claim
   */
  uint32_t type;

  /**
   * Flag
   */
  uint32_t flag;

  /**
   * The name of the attribute. Note "name" must never be individually
   * free'd
   */
  const char *name;

  /**
   * Number of bytes in @e data.
   */
  size_t data_size;

  /**
   * Binary value stored as attribute value.  Note: "data" must never
   * be individually 'malloc'ed, but instead always points into some
   * existing data area.
   */
  const void *data;
};


/**
 * A list of GNUNET_RECLAIM_Attribute structures.
 */
struct GNUNET_RECLAIM_AttributeList
{
  /**
   * List head
   */
  struct GNUNET_RECLAIM_AttributeListEntry *list_head;

  /**
   * List tail
   */
  struct GNUNET_RECLAIM_AttributeListEntry *list_tail;
};


struct GNUNET_RECLAIM_AttributeListEntry
{
  /**
   * DLL
   */
  struct GNUNET_RECLAIM_AttributeListEntry *prev;

  /**
   * DLL
   */
  struct GNUNET_RECLAIM_AttributeListEntry *next;

  /**
   * The attribute claim
   */
  struct GNUNET_RECLAIM_Attribute *attribute;

};

/**
 * A list of GNUNET_RECLAIM_Attestation structures.
 */
struct GNUNET_RECLAIM_AttestationList
{
  /**
   * List head
   */
  struct GNUNET_RECLAIM_AttestationListEntry *list_head;

  /**
   * List tail
   */
  struct GNUNET_RECLAIM_AttestationListEntry *list_tail;
};


struct GNUNET_RECLAIM_AttestationListEntry
{
  /**
   * DLL
   */
  struct GNUNET_RECLAIM_AttestationListEntry *prev;

  /**
   * DLL
   */
  struct GNUNET_RECLAIM_AttestationListEntry *next;

  /**
   * The attestation
   */
  struct GNUNET_RECLAIM_Attestation *attestation;

};


/**
 * Create a new attribute claim.
 *
 * @param attr_name the attribute name
 * @param attestation ID of the attestation (may be NULL)
 * @param type the attribute type
 * @param data the attribute value. Must be the mapped name if attestation not NULL
 * @param data_size the attribute value size
 * @return the new attribute
 */
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);


/**
 * Get required size for serialization buffer
 *
 * @param attrs the attribute list to serialize
 * @return the required buffer size
 */
size_t
GNUNET_RECLAIM_attribute_list_serialize_get_size (
  const struct GNUNET_RECLAIM_AttributeList *attrs);


/**
 * Destroy claim list
 *
 * @param attrs list to destroy
 */
void
GNUNET_RECLAIM_attribute_list_destroy (
  struct GNUNET_RECLAIM_AttributeList *attrs);


/**
 * Add a new attribute to a claim list
 *
 * @param attrs the attribute list to add to
 * @param attr_name the name of the new attribute claim
 * @param attestation attestation ID (may be NULL)
 * @param type the type of the claim
 * @param data claim payload
 * @param data_size claim payload size
 */
void
GNUNET_RECLAIM_attribute_list_add (
  struct GNUNET_RECLAIM_AttributeList *attrs,
  const char *attr_name,
  const struct GNUNET_RECLAIM_Identifier *attestation,
  uint32_t type,
  const void *data,
  size_t data_size);


/**
 * Serialize an attribute list
 *
 * @param attrs the attribute list to serialize
 * @param result the serialized attribute
 * @return length of serialized data
 */
size_t
GNUNET_RECLAIM_attribute_list_serialize (
  const struct GNUNET_RECLAIM_AttributeList *attrs,
  char *result);


/**
 * Deserialize an attribute list
 *
 * @param data the serialized attribute list
 * @param data_size the length of the serialized data
 * @return a GNUNET_IDENTITY_PROVIDER_AttributeList, must be free'd by caller
 */
struct GNUNET_RECLAIM_AttributeList *
GNUNET_RECLAIM_attribute_list_deserialize (const char *data, size_t data_size);

/**
 * Get required size for serialization buffer
 *
 * @param attr the attribute to serialize
 * @return the required buffer size
 */
size_t
GNUNET_RECLAIM_attribute_serialize_get_size (
  const struct GNUNET_RECLAIM_Attribute *attr);


/**
 * Serialize an attribute
 *
 * @param attr the attribute to serialize
 * @param result the serialized attribute
 * @return length of serialized data
 */
size_t
GNUNET_RECLAIM_attribute_serialize (const struct GNUNET_RECLAIM_Attribute *attr,
                                    char *result);


/**
 * Deserialize an attribute
 *
 * @param data the serialized attribute
 * @param data_size the length of the serialized data
 * @param attr deserialized attribute. Will be allocated. Must be free'd
 *
 * @return number of bytes read or -1 for error
 */
ssize_t
GNUNET_RECLAIM_attribute_deserialize (const char *data, size_t data_size,
                                      struct GNUNET_RECLAIM_Attribute **attr);


/**
 * Make a (deep) copy of a claim list
 * @param attrs claim list to copy
 * @return copied claim list
 */
struct GNUNET_RECLAIM_AttributeList *
GNUNET_RECLAIM_attribute_list_dup (
  const struct GNUNET_RECLAIM_AttributeList *attrs);


/**
 * Convert a type name to the corresponding number
 *
 * @param typename name to convert
 * @return corresponding number, UINT32_MAX on error
 */
uint32_t
GNUNET_RECLAIM_attribute_typename_to_number (const char *typename);

/**
 * Convert human-readable version of a 'claim' of an attribute to the binary
 * representation
 *
 * @param type type of the claim
 * @param s human-readable string
 * @param data set to value in binary encoding (will be allocated)
 * @param data_size set to number of bytes in @a data
 * @return #GNUNET_OK on success
 */
int
GNUNET_RECLAIM_attribute_string_to_value (uint32_t type,
                                          const char *s,
                                          void **data,
                                          size_t *data_size);


/**
 * Convert the 'claim' of an attribute to a string
 *
 * @param type the type of attribute
 * @param data claim in binary encoding
 * @param data_size number of bytes in @a data
 * @return NULL on error, otherwise human-readable representation of the claim
 */
char *
GNUNET_RECLAIM_attribute_value_to_string (uint32_t type,
                                          const void *data,
                                          size_t data_size);

/**
 * Convert a type number to the corresponding type string
 *
 * @param type number of a type
 * @return corresponding typestring, NULL on error
 */
const char *
GNUNET_RECLAIM_attribute_number_to_typename (uint32_t type);


/**
 * Get required size for serialization buffer
 *
 * @param attrs the attribute list to serialize
 * @return the required buffer size
 */
size_t
GNUNET_RECLAIM_attestation_list_serialize_get_size (
  const struct GNUNET_RECLAIM_AttestationList *attestations);


/**
 * Destroy claim list
 *
 * @param attrs list to destroy
 */
void
GNUNET_RECLAIM_attestation_list_destroy (
  struct GNUNET_RECLAIM_AttestationList *attestations);


/**
 * Add a new attribute to a claim list
 *
 * @param attr_name the name of the new attribute claim
 * @param type the type of the claim
 * @param data claim payload
 * @param data_size claim payload size
 */
void
GNUNET_RECLAIM_attestation_list_add (
  struct GNUNET_RECLAIM_AttestationList *attrs,
  const char *att_name,
  uint32_t type,
  const void *data,
  size_t data_size);


/**
 * Serialize an attribute list
 *
 * @param attrs the attribute list to serialize
 * @param result the serialized attribute
 * @return length of serialized data
 */
size_t
GNUNET_RECLAIM_attestation_list_serialize (
  const struct GNUNET_RECLAIM_AttestationList *attrs,
  char *result);


/**
 * Deserialize an attribute list
 *
 * @param data the serialized attribute list
 * @param data_size the length of the serialized data
 * @return a GNUNET_IDENTITY_PROVIDER_AttributeList, must be free'd by caller
 */
struct GNUNET_RECLAIM_AttestationList *
GNUNET_RECLAIM_attestation_list_deserialize (const char *data,
                                             size_t data_size);


/**
   * @param attestation the attestation to serialize
   * @return the required buffer size
   */
size_t
GNUNET_RECLAIM_attestation_serialize_get_size (
  const struct GNUNET_RECLAIM_Attestation *attestation);


/**
 * Serialize an attestation
 *
 * @param attestation the attestation to serialize
 * @param result the serialized attestation
 * @return length of serialized data
 */
size_t
GNUNET_RECLAIM_attestation_serialize (
  const struct GNUNET_RECLAIM_Attestation *attestation,
  char *result);


/**
 * Deserialize an attestation
 *
 * @param data the serialized attestation
 * @param data_size the length of the serialized data
 *
 * @return a GNUNET_IDENTITY_PROVIDER_Attribute, must be free'd by caller
 */
struct GNUNET_RECLAIM_Attestation *
GNUNET_RECLAIM_attestation_deserialize (const char *data, size_t data_size);


/**
 * Create a new attestation.
 *
 * @param name the attestation name
 * @param type the attestation type
 * @param data the attestation value
 * @param data_size the attestation value size
 * @return the new attestation
 */
struct GNUNET_RECLAIM_Attestation *
GNUNET_RECLAIM_attestation_new (const char *name,
                                uint32_t type,
                                const void *data,
                                size_t data_size);

/**
 * Convert the 'claim' of an attestation to a string
 *
 * @param type the type of attestation
 * @param data claim in binary encoding
 * @param data_size number of bytes in @a data
 * @return NULL on error, otherwise human-readable representation of the claim
 */
char *
GNUNET_RECLAIM_attestation_value_to_string (uint32_t type,
                                            const void *data,
                                            size_t data_size);

/**
 * Convert human-readable version of a 'claim' of an attestation to the binary
 * representation
 *
 * @param type type of the claim
 * @param s human-readable string
 * @param data set to value in binary encoding (will be allocated)
 * @param data_size set to number of bytes in @a data
 * @return #GNUNET_OK on success
 */
int
GNUNET_RECLAIM_attestation_string_to_value (uint32_t type,
                                            const char *s,
                                            void **data,
                                            size_t *data_size);

/**
 * Convert an attestation type number to the corresponding attestation type string
 *
 * @param type number of a type
 * @return corresponding typestring, NULL on error
 */
const char *
GNUNET_RECLAIM_attestation_number_to_typename (uint32_t type);

/**
 * Convert an attestation type name to the corresponding number
 *
 * @param typename name to convert
 * @return corresponding number, UINT32_MAX on error
 */
uint32_t
GNUNET_RECLAIM_attestation_typename_to_number (const char *typename);

/**
 * Convert an attestation type name to the corresponding number
 *
 * @param typename name to convert
 * @return corresponding number, UINT32_MAX on error
 */
struct GNUNET_RECLAIM_AttributeList*
GNUNET_RECLAIM_attestation_get_attributes (const struct
                                           GNUNET_RECLAIM_Attestation *attest);

char*
GNUNET_RECLAIM_attestation_get_issuer (const struct
                                       GNUNET_RECLAIM_Attestation *attest);

int
GNUNET_RECLAIM_attestation_get_expiration (const struct
                                           GNUNET_RECLAIM_Attestation *attest,
                                           struct GNUNET_TIME_Absolute *exp);

#if 0 /* keep Emacsens' auto-indent happy */
{
#endif
#ifdef __cplusplus
}
#endif


/* ifndef GNUNET_RECLAIM_ATTRIBUTE_LIB_H */
#endif

/** @} */ /* end of group reclaim-attribute */

/* end of gnunet_reclaim_attribute_lib.h */