bugfixes; CLI improvements
[oweals/gnunet.git] / src / include / gnunet_reclaim_attribute_lib.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2017 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
21 /**
22  * @author Martin Schanzenbach
23  *
24  * @file
25  * Identity attribute definitions
26  *
27  * @defgroup reclaim-attribute reclaim attributes
28  * @{
29  */
30 #ifndef GNUNET_RECLAIM_ATTRIBUTE_LIB_H
31 #define GNUNET_RECLAIM_ATTRIBUTE_LIB_H
32
33 #ifdef __cplusplus
34 extern "C" {
35 #if 0 /* keep Emacsens' auto-indent happy */
36 }
37 #endif
38 #endif
39
40 #include "gnunet_util_lib.h"
41
42
43 /**
44  * No value attribute.
45  */
46 #define GNUNET_RECLAIM_ATTRIBUTE_TYPE_NONE 0
47
48 /**
49  * String attribute.
50  */
51 #define GNUNET_RECLAIM_ATTRIBUTE_TYPE_STRING 1
52
53 /**
54 * No value attestation.
55 */
56 #define GNUNET_RECLAIM_ATTESTATION_TYPE_NONE 10
57
58 /**
59 * A JSON Web Token attestation.
60 */
61 #define GNUNET_RECLAIM_ATTESTATION_TYPE_JWT 11
62
63 /**
64  * We want an ID to be a 256-bit symmetric key
65  */
66 #define GNUNET_RECLAIM_ID_LENGTH (256 / 8)
67
68 /**
69  * A reclaim identifier
70  * FIXME maybe put this in a different namespace
71  */
72 struct GNUNET_RECLAIM_Identifier
73 {
74   char id[GNUNET_RECLAIM_ID_LENGTH];
75 };
76
77 static const struct GNUNET_RECLAIM_Identifier GNUNET_RECLAIM_ID_ZERO;
78
79 #define GNUNET_RECLAIM_id_is_equal(a,b) ((0 == \
80                                           memcmp (a, \
81                                                   b, \
82                                                   sizeof (GNUNET_RECLAIM_ID_ZERO))) \
83   ? \
84                                          GNUNET_YES : GNUNET_NO)
85
86
87 #define GNUNET_RECLAIM_id_is_zero(a) GNUNET_RECLAIM_id_is_equal (a, \
88                                                                  & \
89                                                                  GNUNET_RECLAIM_ID_ZERO)
90
91 #define GNUNET_RECLAIM_id_generate(id) \
92   (GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_STRONG, \
93                                id, \
94                                sizeof (GNUNET_RECLAIM_ID_ZERO)))
95
96 /**
97  * An attribute.
98  */
99 struct GNUNET_RECLAIM_Attribute
100 {
101   /**
102    * ID
103    */
104   struct GNUNET_RECLAIM_Identifier id;
105
106   /**
107    * Referenced ID of Attestation (may be 0 if self-attested)
108    */
109   struct GNUNET_RECLAIM_Identifier attestation;
110
111   /**
112    * Type of Claim
113    */
114   uint32_t type;
115
116   /**
117    * Flags
118    */
119   uint32_t flag;
120
121   /**
122    * The name of the attribute. Note "name" must never be individually
123    * free'd
124    */
125   const char *name;
126
127   /**
128    * Number of bytes in @e data.
129    */
130   size_t data_size;
131
132   /**
133    * Binary value stored as attribute value.  Note: "data" must never
134    * be individually 'malloc'ed, but instead always points into some
135    * existing data area.
136    */
137   const void *data;
138 };
139
140 /**
141  * An attestation.
142  */
143 struct GNUNET_RECLAIM_Attestation
144 {
145   /**
146    * ID
147    */
148   struct GNUNET_RECLAIM_Identifier id;
149
150   /**
151    * Type/Format of Claim
152    */
153   uint32_t type;
154
155   /**
156    * Flag
157    */
158   uint32_t flag;
159
160   /**
161    * The name of the attribute. Note "name" must never be individually
162    * free'd
163    */
164   const char *name;
165
166   /**
167    * Number of bytes in @e data.
168    */
169   size_t data_size;
170
171   /**
172    * Binary value stored as attribute value.  Note: "data" must never
173    * be individually 'malloc'ed, but instead always points into some
174    * existing data area.
175    */
176   const void *data;
177 };
178
179
180 /**
181  * A list of GNUNET_RECLAIM_Attribute structures.
182  */
183 struct GNUNET_RECLAIM_AttributeList
184 {
185   /**
186    * List head
187    */
188   struct GNUNET_RECLAIM_AttributeListEntry *list_head;
189
190   /**
191    * List tail
192    */
193   struct GNUNET_RECLAIM_AttributeListEntry *list_tail;
194 };
195
196
197 struct GNUNET_RECLAIM_AttributeListEntry
198 {
199   /**
200    * DLL
201    */
202   struct GNUNET_RECLAIM_AttributeListEntry *prev;
203
204   /**
205    * DLL
206    */
207   struct GNUNET_RECLAIM_AttributeListEntry *next;
208
209   /**
210    * The attribute claim
211    */
212   struct GNUNET_RECLAIM_Attribute *attribute;
213
214 };
215
216 /**
217  * A list of GNUNET_RECLAIM_Attestation structures.
218  */
219 struct GNUNET_RECLAIM_AttestationList
220 {
221   /**
222    * List head
223    */
224   struct GNUNET_RECLAIM_AttestationListEntry *list_head;
225
226   /**
227    * List tail
228    */
229   struct GNUNET_RECLAIM_AttestationListEntry *list_tail;
230 };
231
232
233 struct GNUNET_RECLAIM_AttestationListEntry
234 {
235   /**
236    * DLL
237    */
238   struct GNUNET_RECLAIM_AttestationListEntry *prev;
239
240   /**
241    * DLL
242    */
243   struct GNUNET_RECLAIM_AttestationListEntry *next;
244
245   /**
246    * The attestation
247    */
248   struct GNUNET_RECLAIM_Attestation *attestation;
249
250 };
251
252
253 /**
254  * Create a new attribute claim.
255  *
256  * @param attr_name the attribute name
257  * @param attestation ID of the attestation (may be NULL)
258  * @param type the attribute type
259  * @param data the attribute value. Must be the mapped name if attestation not NULL
260  * @param data_size the attribute value size
261  * @return the new attribute
262  */
263 struct GNUNET_RECLAIM_Attribute *
264 GNUNET_RECLAIM_attribute_new (const char *attr_name,
265                               const struct GNUNET_RECLAIM_Identifier *attestation,
266                               uint32_t type,
267                               const void *data,
268                               size_t data_size);
269
270
271 /**
272  * Get required size for serialization buffer
273  *
274  * @param attrs the attribute list to serialize
275  * @return the required buffer size
276  */
277 size_t
278 GNUNET_RECLAIM_attribute_list_serialize_get_size (
279   const struct GNUNET_RECLAIM_AttributeList *attrs);
280
281
282 /**
283  * Destroy claim list
284  *
285  * @param attrs list to destroy
286  */
287 void
288 GNUNET_RECLAIM_attribute_list_destroy (
289   struct GNUNET_RECLAIM_AttributeList *attrs);
290
291
292 /**
293  * Add a new attribute to a claim list
294  *
295  * @param attrs the attribute list to add to
296  * @param attr_name the name of the new attribute claim
297  * @param attestation attestation ID (may be NULL)
298  * @param type the type of the claim
299  * @param data claim payload
300  * @param data_size claim payload size
301  */
302 void
303 GNUNET_RECLAIM_attribute_list_add (
304   struct GNUNET_RECLAIM_AttributeList *attrs,
305   const char *attr_name,
306   const struct GNUNET_RECLAIM_Identifier *attestation,
307   uint32_t type,
308   const void *data,
309   size_t data_size);
310
311
312 /**
313  * Serialize an attribute list
314  *
315  * @param attrs the attribute list to serialize
316  * @param result the serialized attribute
317  * @return length of serialized data
318  */
319 size_t
320 GNUNET_RECLAIM_attribute_list_serialize (
321   const struct GNUNET_RECLAIM_AttributeList *attrs,
322   char *result);
323
324
325 /**
326  * Deserialize an attribute list
327  *
328  * @param data the serialized attribute list
329  * @param data_size the length of the serialized data
330  * @return a GNUNET_IDENTITY_PROVIDER_AttributeList, must be free'd by caller
331  */
332 struct GNUNET_RECLAIM_AttributeList *
333 GNUNET_RECLAIM_attribute_list_deserialize (const char *data, size_t data_size);
334
335 /**
336  * Get required size for serialization buffer
337  *
338  * @param attr the attribute to serialize
339  * @return the required buffer size
340  */
341 size_t
342 GNUNET_RECLAIM_attribute_serialize_get_size (
343   const struct GNUNET_RECLAIM_Attribute *attr);
344
345
346 /**
347  * Serialize an attribute
348  *
349  * @param attr the attribute to serialize
350  * @param result the serialized attribute
351  * @return length of serialized data
352  */
353 size_t
354 GNUNET_RECLAIM_attribute_serialize (const struct GNUNET_RECLAIM_Attribute *attr,
355                                     char *result);
356
357
358 /**
359  * Deserialize an attribute
360  *
361  * @param data the serialized attribute
362  * @param data_size the length of the serialized data
363  *
364  * @return a GNUNET_IDENTITY_PROVIDER_Attribute, must be free'd by caller
365  */
366 struct GNUNET_RECLAIM_Attribute *
367 GNUNET_RECLAIM_attribute_deserialize (const char *data, size_t data_size);
368
369
370 /**
371  * Make a (deep) copy of a claim list
372  * @param attrs claim list to copy
373  * @return copied claim list
374  */
375 struct GNUNET_RECLAIM_AttributeList *
376 GNUNET_RECLAIM_attribute_list_dup (
377   const struct GNUNET_RECLAIM_AttributeList *attrs);
378
379
380 /**
381  * Convert a type name to the corresponding number
382  *
383  * @param typename name to convert
384  * @return corresponding number, UINT32_MAX on error
385  */
386 uint32_t
387 GNUNET_RECLAIM_attribute_typename_to_number (const char *typename);
388
389 /**
390  * Convert human-readable version of a 'claim' of an attribute to the binary
391  * representation
392  *
393  * @param type type of the claim
394  * @param s human-readable string
395  * @param data set to value in binary encoding (will be allocated)
396  * @param data_size set to number of bytes in @a data
397  * @return #GNUNET_OK on success
398  */
399 int
400 GNUNET_RECLAIM_attribute_string_to_value (uint32_t type,
401                                           const char *s,
402                                           void **data,
403                                           size_t *data_size);
404
405
406 /**
407  * Convert the 'claim' of an attribute to a string
408  *
409  * @param type the type of attribute
410  * @param data claim in binary encoding
411  * @param data_size number of bytes in @a data
412  * @return NULL on error, otherwise human-readable representation of the claim
413  */
414 char *
415 GNUNET_RECLAIM_attribute_value_to_string (uint32_t type,
416                                           const void *data,
417                                           size_t data_size);
418
419 /**
420  * Convert a type number to the corresponding type string
421  *
422  * @param type number of a type
423  * @return corresponding typestring, NULL on error
424  */
425 const char *
426 GNUNET_RECLAIM_attribute_number_to_typename (uint32_t type);
427
428
429 /**
430  * Get required size for serialization buffer
431  *
432  * @param attrs the attribute list to serialize
433  * @return the required buffer size
434  */
435 size_t
436 GNUNET_RECLAIM_attestation_list_serialize_get_size (
437   const struct GNUNET_RECLAIM_AttestationList *attestations);
438
439
440 /**
441  * Destroy claim list
442  *
443  * @param attrs list to destroy
444  */
445 void
446 GNUNET_RECLAIM_attestation_list_destroy (
447   struct GNUNET_RECLAIM_AttestationList *attestations);
448
449
450 /**
451  * Add a new attribute to a claim list
452  *
453  * @param attr_name the name of the new attribute claim
454  * @param type the type of the claim
455  * @param data claim payload
456  * @param data_size claim payload size
457  */
458 void
459 GNUNET_RECLAIM_attestation_list_add (
460   struct GNUNET_RECLAIM_AttestationList *attrs,
461   const char *att_name,
462   uint32_t type,
463   const void *data,
464   size_t data_size);
465
466
467 /**
468  * Serialize an attribute list
469  *
470  * @param attrs the attribute list to serialize
471  * @param result the serialized attribute
472  * @return length of serialized data
473  */
474 size_t
475 GNUNET_RECLAIM_attestation_list_serialize (
476   const struct GNUNET_RECLAIM_AttestationList *attrs,
477   char *result);
478
479
480 /**
481  * Deserialize an attribute list
482  *
483  * @param data the serialized attribute list
484  * @param data_size the length of the serialized data
485  * @return a GNUNET_IDENTITY_PROVIDER_AttributeList, must be free'd by caller
486  */
487 struct GNUNET_RECLAIM_AttestationList *
488 GNUNET_RECLAIM_attestation_list_deserialize (const char *data,
489                                              size_t data_size);
490
491
492
493 /**
494    * @param attestation the attestation to serialize
495    * @return the required buffer size
496    */
497 size_t
498 GNUNET_RECLAIM_attestation_serialize_get_size (
499   const struct GNUNET_RECLAIM_Attestation *attestation);
500
501
502 /**
503  * Serialize an attestation
504  *
505  * @param attestation the attestation to serialize
506  * @param result the serialized attestation
507  * @return length of serialized data
508  */
509 size_t
510 GNUNET_RECLAIM_attestation_serialize (
511   const struct GNUNET_RECLAIM_Attestation *attestation,
512   char *result);
513
514
515 /**
516  * Deserialize an attestation
517  *
518  * @param data the serialized attestation
519  * @param data_size the length of the serialized data
520  *
521  * @return a GNUNET_IDENTITY_PROVIDER_Attribute, must be free'd by caller
522  */
523 struct GNUNET_RECLAIM_Attestation *
524 GNUNET_RECLAIM_attestation_deserialize (const char *data, size_t data_size);
525
526
527 /**
528  * Create a new attestation.
529  *
530  * @param name the attestation name
531  * @param type the attestation type
532  * @param data the attestation value
533  * @param data_size the attestation value size
534  * @return the new attestation
535  */
536 struct GNUNET_RECLAIM_Attestation *
537 GNUNET_RECLAIM_attestation_new (const char *name,
538                                 uint32_t type,
539                                 const void *data,
540                                 size_t data_size);
541
542 /**
543  * Convert the 'claim' of an attestation to a string
544  *
545  * @param type the type of attestation
546  * @param data claim in binary encoding
547  * @param data_size number of bytes in @a data
548  * @return NULL on error, otherwise human-readable representation of the claim
549  */
550 char *
551 GNUNET_RECLAIM_attestation_value_to_string (uint32_t type,
552                                             const void *data,
553                                             size_t data_size);
554
555 /**
556  * Convert human-readable version of a 'claim' of an attestation to the binary
557  * representation
558  *
559  * @param type type of the claim
560  * @param s human-readable string
561  * @param data set to value in binary encoding (will be allocated)
562  * @param data_size set to number of bytes in @a data
563  * @return #GNUNET_OK on success
564  */
565 int
566 GNUNET_RECLAIM_attestation_string_to_value (uint32_t type,
567                                             const char *s,
568                                             void **data,
569                                             size_t *data_size);
570
571 /**
572  * Convert an attestation type number to the corresponding attestation type string
573  *
574  * @param type number of a type
575  * @return corresponding typestring, NULL on error
576  */
577 const char *
578 GNUNET_RECLAIM_attestation_number_to_typename (uint32_t type);
579
580 /**
581  * Convert an attestation type name to the corresponding number
582  *
583  * @param typename name to convert
584  * @return corresponding number, UINT32_MAX on error
585  */
586 uint32_t
587 GNUNET_RECLAIM_attestation_typename_to_number (const char *typename);
588
589 /**
590  * Convert an attestation type name to the corresponding number
591  *
592  * @param typename name to convert
593  * @return corresponding number, UINT32_MAX on error
594  */
595 struct GNUNET_RECLAIM_AttributeList*
596 GNUNET_RECLAIM_attestation_get_attributes (const struct GNUNET_RECLAIM_Attestation *attest);
597
598
599 #if 0 /* keep Emacsens' auto-indent happy */
600 {
601 #endif
602 #ifdef __cplusplus
603 }
604 #endif
605
606
607 /* ifndef GNUNET_RECLAIM_ATTRIBUTE_LIB_H */
608 #endif
609
610 /** @} */ /* end of group reclaim-attribute */
611
612 /* end of gnunet_reclaim_attribute_lib.h */