error handling
[oweals/gnunet.git] / src / include / gnunet_reclaim_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
266                               GNUNET_RECLAIM_Identifier *attestation,
267                               uint32_t type,
268                               const void *data,
269                               size_t data_size);
270
271
272 /**
273  * Get required size for serialization buffer
274  *
275  * @param attrs the attribute list to serialize
276  * @return the required buffer size
277  */
278 size_t
279 GNUNET_RECLAIM_attribute_list_serialize_get_size (
280   const struct GNUNET_RECLAIM_AttributeList *attrs);
281
282
283 /**
284  * Destroy claim list
285  *
286  * @param attrs list to destroy
287  */
288 void
289 GNUNET_RECLAIM_attribute_list_destroy (
290   struct GNUNET_RECLAIM_AttributeList *attrs);
291
292
293 /**
294  * Add a new attribute to a claim list
295  *
296  * @param attrs the attribute list to add to
297  * @param attr_name the name of the new attribute claim
298  * @param attestation attestation ID (may be NULL)
299  * @param type the type of the claim
300  * @param data claim payload
301  * @param data_size claim payload size
302  */
303 void
304 GNUNET_RECLAIM_attribute_list_add (
305   struct GNUNET_RECLAIM_AttributeList *attrs,
306   const char *attr_name,
307   const struct GNUNET_RECLAIM_Identifier *attestation,
308   uint32_t type,
309   const void *data,
310   size_t data_size);
311
312
313 /**
314  * Serialize an attribute list
315  *
316  * @param attrs the attribute list to serialize
317  * @param result the serialized attribute
318  * @return length of serialized data
319  */
320 size_t
321 GNUNET_RECLAIM_attribute_list_serialize (
322   const struct GNUNET_RECLAIM_AttributeList *attrs,
323   char *result);
324
325
326 /**
327  * Deserialize an attribute list
328  *
329  * @param data the serialized attribute list
330  * @param data_size the length of the serialized data
331  * @return a GNUNET_IDENTITY_PROVIDER_AttributeList, must be free'd by caller
332  */
333 struct GNUNET_RECLAIM_AttributeList *
334 GNUNET_RECLAIM_attribute_list_deserialize (const char *data, size_t data_size);
335
336 /**
337  * Get required size for serialization buffer
338  *
339  * @param attr the attribute to serialize
340  * @return the required buffer size
341  */
342 size_t
343 GNUNET_RECLAIM_attribute_serialize_get_size (
344   const struct GNUNET_RECLAIM_Attribute *attr);
345
346
347 /**
348  * Serialize an attribute
349  *
350  * @param attr the attribute to serialize
351  * @param result the serialized attribute
352  * @return length of serialized data
353  */
354 size_t
355 GNUNET_RECLAIM_attribute_serialize (const struct GNUNET_RECLAIM_Attribute *attr,
356                                     char *result);
357
358
359 /**
360  * Deserialize an attribute
361  *
362  * @param data the serialized attribute
363  * @param data_size the length of the serialized data
364  *
365  * @return a GNUNET_IDENTITY_PROVIDER_Attribute, must be free'd by caller
366  */
367 struct GNUNET_RECLAIM_Attribute *
368 GNUNET_RECLAIM_attribute_deserialize (const char *data, size_t data_size);
369
370
371 /**
372  * Make a (deep) copy of a claim list
373  * @param attrs claim list to copy
374  * @return copied claim list
375  */
376 struct GNUNET_RECLAIM_AttributeList *
377 GNUNET_RECLAIM_attribute_list_dup (
378   const struct GNUNET_RECLAIM_AttributeList *attrs);
379
380
381 /**
382  * Convert a type name to the corresponding number
383  *
384  * @param typename name to convert
385  * @return corresponding number, UINT32_MAX on error
386  */
387 uint32_t
388 GNUNET_RECLAIM_attribute_typename_to_number (const char *typename);
389
390 /**
391  * Convert human-readable version of a 'claim' of an attribute to the binary
392  * representation
393  *
394  * @param type type of the claim
395  * @param s human-readable string
396  * @param data set to value in binary encoding (will be allocated)
397  * @param data_size set to number of bytes in @a data
398  * @return #GNUNET_OK on success
399  */
400 int
401 GNUNET_RECLAIM_attribute_string_to_value (uint32_t type,
402                                           const char *s,
403                                           void **data,
404                                           size_t *data_size);
405
406
407 /**
408  * Convert the 'claim' of an attribute to a string
409  *
410  * @param type the type of attribute
411  * @param data claim in binary encoding
412  * @param data_size number of bytes in @a data
413  * @return NULL on error, otherwise human-readable representation of the claim
414  */
415 char *
416 GNUNET_RECLAIM_attribute_value_to_string (uint32_t type,
417                                           const void *data,
418                                           size_t data_size);
419
420 /**
421  * Convert a type number to the corresponding type string
422  *
423  * @param type number of a type
424  * @return corresponding typestring, NULL on error
425  */
426 const char *
427 GNUNET_RECLAIM_attribute_number_to_typename (uint32_t type);
428
429
430 /**
431  * Get required size for serialization buffer
432  *
433  * @param attrs the attribute list to serialize
434  * @return the required buffer size
435  */
436 size_t
437 GNUNET_RECLAIM_attestation_list_serialize_get_size (
438   const struct GNUNET_RECLAIM_AttestationList *attestations);
439
440
441 /**
442  * Destroy claim list
443  *
444  * @param attrs list to destroy
445  */
446 void
447 GNUNET_RECLAIM_attestation_list_destroy (
448   struct GNUNET_RECLAIM_AttestationList *attestations);
449
450
451 /**
452  * Add a new attribute to a claim list
453  *
454  * @param attr_name the name of the new attribute claim
455  * @param type the type of the claim
456  * @param data claim payload
457  * @param data_size claim payload size
458  */
459 void
460 GNUNET_RECLAIM_attestation_list_add (
461   struct GNUNET_RECLAIM_AttestationList *attrs,
462   const char *att_name,
463   uint32_t type,
464   const void *data,
465   size_t data_size);
466
467
468 /**
469  * Serialize an attribute list
470  *
471  * @param attrs the attribute list to serialize
472  * @param result the serialized attribute
473  * @return length of serialized data
474  */
475 size_t
476 GNUNET_RECLAIM_attestation_list_serialize (
477   const struct GNUNET_RECLAIM_AttestationList *attrs,
478   char *result);
479
480
481 /**
482  * Deserialize an attribute list
483  *
484  * @param data the serialized attribute list
485  * @param data_size the length of the serialized data
486  * @return a GNUNET_IDENTITY_PROVIDER_AttributeList, must be free'd by caller
487  */
488 struct GNUNET_RECLAIM_AttestationList *
489 GNUNET_RECLAIM_attestation_list_deserialize (const char *data,
490                                              size_t data_size);
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
597                                            GNUNET_RECLAIM_Attestation *attest);
598
599 char*
600 GNUNET_RECLAIM_attestation_get_issuer (const struct
601                                        GNUNET_RECLAIM_Attestation *attest);
602
603 int
604 GNUNET_RECLAIM_attestation_get_expiration (const struct
605                                            GNUNET_RECLAIM_Attestation *attest,
606                                            struct GNUNET_TIME_Absolute *exp);
607
608 #if 0 /* keep Emacsens' auto-indent happy */
609 {
610 #endif
611 #ifdef __cplusplus
612 }
613 #endif
614
615
616 /* ifndef GNUNET_RECLAIM_ATTRIBUTE_LIB_H */
617 #endif
618
619 /** @} */ /* end of group reclaim-attribute */
620
621 /* end of gnunet_reclaim_attribute_lib.h */