2 This file is part of GNUnet
3 Copyright (C) 2010-2015 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file reclaim-attribute/reclaim_attribute.c
23 * @brief helper library to manage identity attributes
24 * @author Martin Schanzenbach
27 #include "gnunet_util_lib.h"
28 #include "gnunet_reclaim_attribute_plugin.h"
29 #include "reclaim_attribute.h"
45 struct GNUNET_RECLAIM_ATTRIBUTE_PluginFunctions *api;
52 static struct Plugin **attr_plugins;
58 static unsigned int num_plugins;
64 static int initialized;
71 * @param library_name name of the API library
72 * @param lib_ret the plugin API pointer
75 add_plugin (void *cls, const char *library_name, void *lib_ret)
77 struct GNUNET_RECLAIM_ATTRIBUTE_PluginFunctions *api = lib_ret;
78 struct Plugin *plugin;
80 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
81 "Loading attribute plugin `%s'\n",
83 plugin = GNUNET_new (struct Plugin);
85 plugin->library_name = GNUNET_strdup (library_name);
86 GNUNET_array_append (attr_plugins, num_plugins, plugin);
96 if (GNUNET_YES == initialized)
98 initialized = GNUNET_YES;
99 GNUNET_PLUGIN_load_all ("libgnunet_plugin_reclaim_attribute_",
107 * Convert a type name to the corresponding number
109 * @param typename name to convert
110 * @return corresponding number, UINT32_MAX on error
113 GNUNET_RECLAIM_ATTRIBUTE_typename_to_number (const char *typename)
116 struct Plugin *plugin;
120 for (i = 0; i < num_plugins; i++)
122 plugin = attr_plugins[i];
124 (ret = plugin->api->typename_to_number (plugin->api->cls, typename)))
132 * Convert a type number to the corresponding type string
134 * @param type number of a type
135 * @return corresponding typestring, NULL on error
138 GNUNET_RECLAIM_ATTRIBUTE_number_to_typename (uint32_t type)
141 struct Plugin *plugin;
145 for (i = 0; i < num_plugins; i++)
147 plugin = attr_plugins[i];
149 (ret = plugin->api->number_to_typename (plugin->api->cls, type)))
157 * Convert human-readable version of a 'claim' of an attribute to the binary
160 * @param type type of the claim
161 * @param s human-readable string
162 * @param data set to value in binary encoding (will be allocated)
163 * @param data_size set to number of bytes in @a data
164 * @return #GNUNET_OK on success
167 GNUNET_RECLAIM_ATTRIBUTE_string_to_value (uint32_t type,
173 struct Plugin *plugin;
176 for (i = 0; i < num_plugins; i++)
178 plugin = attr_plugins[i];
179 if (GNUNET_OK == plugin->api->string_to_value (plugin->api->cls,
186 return GNUNET_SYSERR;
191 * Convert the 'claim' of an attribute to a string
193 * @param type the type of attribute
194 * @param data claim in binary encoding
195 * @param data_size number of bytes in @a data
196 * @return NULL on error, otherwise human-readable representation of the claim
199 GNUNET_RECLAIM_ATTRIBUTE_value_to_string (uint32_t type,
204 struct Plugin *plugin;
208 for (i = 0; i < num_plugins; i++)
210 plugin = attr_plugins[i];
211 if (NULL != (ret = plugin->api->value_to_string (plugin->api->cls,
221 * Convert an attestation type name to the corresponding number
223 * @param typename name to convert
224 * @return corresponding number, UINT32_MAX on error
227 GNUNET_RECLAIM_ATTESTATION_typename_to_number (const char *typename)
230 struct Plugin *plugin;
233 for (i = 0; i < num_plugins; i++)
235 plugin = attr_plugins[i];
237 (ret = plugin->api->typename_to_number_attest (plugin->api->cls,
245 * Convert an attestation type number to the corresponding attestation type string
247 * @param type number of a type
248 * @return corresponding typestring, NULL on error
251 GNUNET_RECLAIM_ATTESTATION_number_to_typename (uint32_t type)
254 struct Plugin *plugin;
258 for (i = 0; i < num_plugins; i++)
260 plugin = attr_plugins[i];
262 (ret = plugin->api->number_to_typename_attest (plugin->api->cls, type)))
268 * Convert human-readable version of a 'claim' of an attestation to the binary
271 * @param type type of the claim
272 * @param s human-readable string
273 * @param data set to value in binary encoding (will be allocated)
274 * @param data_size set to number of bytes in @a data
275 * @return #GNUNET_OK on success
278 GNUNET_RECLAIM_ATTESTATION_string_to_value (uint32_t type,
284 struct Plugin *plugin;
287 for (i = 0; i < num_plugins; i++)
289 plugin = attr_plugins[i];
290 if (GNUNET_OK == plugin->api->string_to_value_attest (plugin->api->cls,
297 return GNUNET_SYSERR;
302 * Convert the 'claim' of an attestation to a string
304 * @param type the type of attestation
305 * @param data claim in binary encoding
306 * @param data_size number of bytes in @a data
307 * @return NULL on error, otherwise human-readable representation of the claim
310 GNUNET_RECLAIM_ATTESTATION_value_to_string (uint32_t type,
315 struct Plugin *plugin;
319 for (i = 0; i < num_plugins; i++)
321 plugin = attr_plugins[i];
322 if (NULL != (ret = plugin->api->value_to_string_attest (plugin->api->cls,
332 * Create a new attribute.
334 * @param attr_name the attribute name
335 * @param type the attribute type
336 * @param data the attribute value
337 * @param data_size the attribute value size
338 * @return the new attribute
340 struct GNUNET_RECLAIM_ATTRIBUTE_Claim *
341 GNUNET_RECLAIM_ATTRIBUTE_claim_new (const char *attr_name,
346 struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr;
348 char *attr_name_tmp = GNUNET_strdup (attr_name);
350 GNUNET_STRINGS_utf8_tolower (attr_name, attr_name_tmp);
352 attr = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_ATTRIBUTE_Claim)
353 + strlen (attr_name_tmp) + 1 + data_size);
355 attr->data_size = data_size;
357 write_ptr = (char *) &attr[1];
358 GNUNET_memcpy (write_ptr, attr_name_tmp, strlen (attr_name_tmp) + 1);
359 attr->name = write_ptr;
360 write_ptr += strlen (attr->name) + 1;
361 GNUNET_memcpy (write_ptr, data, data_size);
362 attr->data = write_ptr;
363 GNUNET_free (attr_name_tmp);
368 * Create a new attestation.
370 * @param attr_name the attestation name
371 * @param type the attestation type
372 * @param data the attestation value
373 * @param data_size the attestation value size
374 * @return the new attestation
376 struct GNUNET_RECLAIM_ATTESTATION_Claim *
377 GNUNET_RECLAIM_ATTESTATION_claim_new (const char *attr_name,
382 struct GNUNET_RECLAIM_ATTESTATION_Claim *attr;
384 char *attr_name_tmp = GNUNET_strdup (attr_name);
386 GNUNET_STRINGS_utf8_tolower (attr_name, attr_name_tmp);
388 attr = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_ATTESTATION_Claim)
389 + strlen (attr_name_tmp) + 1 + data_size);
391 attr->data_size = data_size;
393 write_ptr = (char *) &attr[1];
394 GNUNET_memcpy (write_ptr, attr_name_tmp, strlen (attr_name_tmp) + 1);
395 attr->name = write_ptr;
396 write_ptr += strlen (attr->name) + 1;
397 GNUNET_memcpy (write_ptr, data, data_size);
398 attr->data = write_ptr;
399 GNUNET_free (attr_name_tmp);
404 * Create a new attestation reference.
406 * @param attr_name the referenced claim name
407 * @param ref_value the claim name in the attestation
408 * @return the new reference
410 struct GNUNET_RECLAIM_ATTESTATION_REFERENCE *
411 GNUNET_RECLAIM_ATTESTATION_reference_new (const char *attr_name,
412 const char *ref_value)
414 struct GNUNET_RECLAIM_ATTESTATION_REFERENCE *attr;
416 char *attr_name_tmp = GNUNET_strdup (attr_name);
417 char *ref_value_tmp = GNUNET_strdup (ref_value);
419 GNUNET_STRINGS_utf8_tolower (attr_name, attr_name_tmp);
420 GNUNET_STRINGS_utf8_tolower (ref_value, ref_value_tmp);
422 attr = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_ATTESTATION_REFERENCE)
423 + strlen (attr_name_tmp) + strlen (ref_value_tmp) + 2);
425 write_ptr = (char *) &attr[1];
426 GNUNET_memcpy (write_ptr, attr_name_tmp, strlen (attr_name_tmp) + 1);
427 attr->name = write_ptr;
429 write_ptr += strlen (attr_name) + 1;
430 GNUNET_memcpy (write_ptr, ref_value_tmp, strlen (ref_value_tmp) + 1);
431 attr->reference_value = write_ptr;
433 GNUNET_free (attr_name_tmp);
434 GNUNET_free (ref_value_tmp);
439 * Add a new attribute to a claim list
441 * @param attr_name the name of the new attribute claim
442 * @param type the type of the claim
443 * @param data claim payload
444 * @param data_size claim payload size
447 GNUNET_RECLAIM_ATTRIBUTE_list_add (
448 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *claim_list,
449 const char *attr_name,
454 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
456 le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry);
458 GNUNET_RECLAIM_ATTRIBUTE_claim_new (attr_name, type, data, data_size);
459 GNUNET_CONTAINER_DLL_insert (claim_list->list_head,
460 claim_list->list_tail,
466 * Get required size for serialization buffer
468 * @param attrs the attribute list to serialize
469 * @return the required buffer size
472 GNUNET_RECLAIM_ATTRIBUTE_list_serialize_get_size (
473 const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs)
475 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
478 for (le = attrs->list_head; NULL != le; le = le->next)
480 if (NULL != le->claim)
482 len += sizeof(struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntryType);
483 len += GNUNET_RECLAIM_ATTRIBUTE_serialize_get_size (le->claim);
485 else if (NULL != le->attest )
487 len += sizeof(struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntryType);
488 len += GNUNET_RECLAIM_ATTESTATION_serialize_get_size (le->attest);
490 else if (NULL != le->reference)
492 len += sizeof(struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntryType);
493 len += GNUNET_RECLAIM_ATTESTATION_REF_serialize_get_size (le->reference);
497 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
498 "Unserialized Claim List Entry Type for size not known.\n");
501 len += sizeof(struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry);
508 * Serialize an attribute list
510 * @param attrs the attribute list to serialize
511 * @param result the serialized attribute
512 * @return length of serialized data
515 GNUNET_RECLAIM_ATTRIBUTE_list_serialize (
516 const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs,
519 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
525 for (le = attrs->list_head; NULL != le; le = le->next)
527 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntryType *list_type;
528 if (NULL != le->claim)
531 GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntryType *) write_ptr;
532 list_type->type = htons (1);
533 total_len += sizeof(struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntryType);
534 write_ptr += sizeof(struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntryType);
535 len = GNUNET_RECLAIM_ATTRIBUTE_serialize (le->claim, write_ptr);
539 else if (NULL != le->attest )
542 GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntryType *) write_ptr;
543 list_type->type = htons (2);
544 total_len += sizeof(struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntryType);
545 write_ptr += sizeof(struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntryType);
546 len = GNUNET_RECLAIM_ATTESTATION_serialize (le->attest, write_ptr);
550 else if (NULL != le->reference)
553 GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntryType *) write_ptr;
554 list_type->type = htons (3);
555 total_len += sizeof(struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntryType);
556 write_ptr += sizeof(struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntryType);
557 len = GNUNET_RECLAIM_ATTESTATION_REF_serialize (le->reference, write_ptr);
563 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
564 "Unserialized Claim List Entry Type not known.\n");
573 * Deserialize an attribute list
575 * @param data the serialized attribute list
576 * @param data_size the length of the serialized data
577 * @return a GNUNET_IDENTITY_PROVIDER_AttributeList, must be free'd by caller
579 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *
580 GNUNET_RECLAIM_ATTRIBUTE_list_deserialize (const char *data, size_t data_size)
582 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs;
583 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
585 const char *read_ptr;
587 if ((data_size < sizeof(struct Attribute) + sizeof(struct
588 GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry))
589 && (data_size < sizeof(struct
591 + sizeof(struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry)) &&
592 (data_size < sizeof(struct Attestation_Reference) + sizeof(struct
593 GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry)) )
596 attrs = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList);
598 while (((data + data_size) - read_ptr) >= sizeof(struct Attribute))
600 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntryType *list_type;
601 list_type = (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntryType *) read_ptr;
602 if (1 == ntohs (list_type->type))
604 le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry);
605 read_ptr += sizeof(struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntryType);
606 if (((data + data_size) - read_ptr) < sizeof(struct Attribute))
609 le->reference = NULL;
611 GNUNET_RECLAIM_ATTRIBUTE_deserialize (read_ptr,
612 data_size - (read_ptr - data));
613 GNUNET_CONTAINER_DLL_insert (attrs->list_head, attrs->list_tail, le);
614 attr_len = GNUNET_RECLAIM_ATTRIBUTE_serialize_get_size (le->claim);
615 read_ptr += attr_len;
617 else if (2 == ntohs (list_type->type))
619 le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry);
620 read_ptr += sizeof(struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntryType);
621 if (((data + data_size) - read_ptr) < sizeof(struct Attestation))
624 le->reference = NULL;
626 GNUNET_RECLAIM_ATTESTATION_deserialize (read_ptr,
627 data_size - (read_ptr - data));
628 GNUNET_CONTAINER_DLL_insert (attrs->list_head, attrs->list_tail, le);
629 attr_len = GNUNET_RECLAIM_ATTESTATION_serialize_get_size (le->attest);
630 read_ptr += attr_len;
632 else if (3 == ntohs (list_type->type))
634 le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry);
635 read_ptr += sizeof(struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntryType);
636 if (((data + data_size) - read_ptr) < sizeof(struct
637 Attestation_Reference))
642 GNUNET_RECLAIM_ATTESTATION_REF_deserialize (read_ptr,
643 data_size - (read_ptr
645 GNUNET_CONTAINER_DLL_insert (attrs->list_head, attrs->list_tail, le);
646 attr_len = GNUNET_RECLAIM_ATTESTATION_REF_serialize_get_size (
648 read_ptr += attr_len;
652 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
653 "Serialized Claim List Entry Type not known.\n");
662 * Make a (deep) copy of a claim list
663 * @param attrs claim list to copy
664 * @return copied claim list
666 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *
667 GNUNET_RECLAIM_ATTRIBUTE_list_dup (
668 const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs)
670 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
671 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *result_le;
672 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *result;
674 result = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList);
675 if (NULL == attrs->list_head)
677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Duplicating empty List\n");
679 for (le = attrs->list_head; NULL != le; le = le->next)
681 result_le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry);
682 result_le->claim = NULL;
683 result_le->attest = NULL;
684 result_le->reference = NULL;
685 if (NULL != le->claim)
688 GNUNET_RECLAIM_ATTRIBUTE_claim_new (le->claim->name,
691 le->claim->data_size);
693 result_le->claim->id = le->claim->id;
694 result_le->claim->flag = le->claim->flag;
696 if ( NULL != le->attest)
698 result_le->attest = GNUNET_RECLAIM_ATTESTATION_claim_new (
704 result_le->attest->id = le->attest->id;
706 if (NULL !=le->reference)
708 result_le->reference = GNUNET_RECLAIM_ATTESTATION_reference_new (
710 le->reference->reference_value);
711 result_le->reference->id = le->reference->id;
712 result_le->reference->id_attest = le->reference->id_attest;
714 GNUNET_CONTAINER_DLL_insert (result->list_head,
725 * @param attrs list to destroy
728 GNUNET_RECLAIM_ATTRIBUTE_list_destroy (
729 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs)
731 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
732 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *tmp_le;
734 for (le = attrs->list_head; NULL != le; le = le->next)
736 if (NULL != le->claim)
737 GNUNET_free (le->claim);
738 if (NULL != le->attest)
739 GNUNET_free (le->attest);
740 if (NULL != le->reference)
741 GNUNET_free (le->reference);
744 GNUNET_free (tmp_le);
750 * Count attestations in claim list
755 GNUNET_RECLAIM_ATTRIBUTE_list_count_attest (
756 const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs)
758 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
760 for (le = attrs->list_head; NULL != le; le = le->next)
762 if (NULL != le->attest)
768 * Get required size for serialization buffer
770 * @param attr the attribute to serialize
771 * @return the required buffer size
774 GNUNET_RECLAIM_ATTRIBUTE_serialize_get_size (
775 const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr)
777 return sizeof(struct Attribute) + strlen (attr->name) + attr->data_size;
782 * Serialize an attribute
784 * @param attr the attribute to serialize
785 * @param result the serialized attribute
786 * @return length of serialized data
789 GNUNET_RECLAIM_ATTRIBUTE_serialize (
790 const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr,
795 struct Attribute *attr_ser;
798 attr_ser = (struct Attribute *) result;
799 attr_ser->attribute_type = htons (attr->type);
800 attr_ser->attribute_version = htonl (attr->flag);
801 attr_ser->attribute_id = GNUNET_htonll (attr->id);
802 name_len = strlen (attr->name);
803 attr_ser->name_len = htons (name_len);
804 write_ptr = (char *) &attr_ser[1];
805 GNUNET_memcpy (write_ptr, attr->name, name_len);
806 write_ptr += name_len;
808 // data_len_ser = plugin->serialize_attribute_value (attr,
810 data_len_ser = attr->data_size;
811 GNUNET_memcpy (write_ptr, attr->data, attr->data_size);
812 attr_ser->data_size = htons (data_len_ser);
814 return sizeof(struct Attribute) + strlen (attr->name) + attr->data_size;
819 * Deserialize an attribute
821 * @param data the serialized attribute
822 * @param data_size the length of the serialized data
824 * @return a GNUNET_IDENTITY_PROVIDER_Attribute, must be free'd by caller
826 struct GNUNET_RECLAIM_ATTRIBUTE_Claim *
827 GNUNET_RECLAIM_ATTRIBUTE_deserialize (const char *data, size_t data_size)
829 struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr;
830 struct Attribute *attr_ser;
835 if (data_size < sizeof(struct Attribute))
838 attr_ser = (struct Attribute *) data;
839 data_len = ntohs (attr_ser->data_size);
840 name_len = ntohs (attr_ser->name_len);
841 if (data_size < sizeof(struct Attribute) + data_len + name_len)
843 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
844 "Buffer too small to deserialize\n");
847 attr = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_ATTRIBUTE_Claim)
848 + data_len + name_len + 1);
849 attr->type = ntohs (attr_ser->attribute_type);
850 attr->flag = ntohl (attr_ser->attribute_version);
851 attr->id = GNUNET_ntohll (attr_ser->attribute_id);
852 attr->data_size = data_len;
854 write_ptr = (char *) &attr[1];
855 GNUNET_memcpy (write_ptr, &attr_ser[1], name_len);
856 write_ptr[name_len] = '\0';
857 attr->name = write_ptr;
859 write_ptr += name_len + 1;
860 GNUNET_memcpy (write_ptr, (char *) &attr_ser[1] + name_len, attr->data_size);
861 attr->data = write_ptr;
867 * Get required size for serialization buffer
869 * @param attr the attestation to serialize
870 * @return the required buffer size
873 GNUNET_RECLAIM_ATTESTATION_serialize_get_size (
874 const struct GNUNET_RECLAIM_ATTESTATION_Claim *attr)
876 return sizeof(struct Attestation) + strlen (attr->name) + attr->data_size;
880 * Serialize an attestation
882 * @param attr the attestation to serialize
883 * @param result the serialized attestation
884 * @return length of serialized data
887 GNUNET_RECLAIM_ATTESTATION_serialize (
888 const struct GNUNET_RECLAIM_ATTESTATION_Claim *attr,
893 struct Attestation *attr_ser;
896 attr_ser = (struct Attestation *) result;
897 attr_ser->attestation_type = htons (attr->type);
898 attr_ser->attestation_version = htonl (attr->version);
899 attr_ser->attestation_id = GNUNET_htonll (attr->id);
900 name_len = strlen (attr->name);
901 attr_ser->name_len = htons (name_len);
902 write_ptr = (char *) &attr_ser[1];
903 GNUNET_memcpy (write_ptr, attr->name, name_len);
904 write_ptr += name_len;
906 // data_len_ser = plugin->serialize_attribute_value (attr,
908 data_len_ser = attr->data_size;
909 GNUNET_memcpy (write_ptr, attr->data, attr->data_size);
910 attr_ser->data_size = htons (data_len_ser);
912 return sizeof(struct Attestation) + strlen (attr->name) + attr->data_size;
916 * Deserialize an attestation
918 * @param data the serialized attestation
919 * @param data_size the length of the serialized data
921 * @return a GNUNET_IDENTITY_PROVIDER_Attribute, must be free'd by caller
923 struct GNUNET_RECLAIM_ATTESTATION_Claim *
924 GNUNET_RECLAIM_ATTESTATION_deserialize (const char *data, size_t data_size)
926 struct GNUNET_RECLAIM_ATTESTATION_Claim *attr;
927 struct Attestation *attr_ser;
932 if (data_size < sizeof(struct Attestation))
935 attr_ser = (struct Attestation *) data;
936 data_len = ntohs (attr_ser->data_size);
937 name_len = ntohs (attr_ser->name_len);
938 if (data_size < sizeof(struct Attestation) + data_len + name_len)
940 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
941 "Buffer too small to deserialize\n");
944 attr = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_ATTESTATION_Claim)
945 + data_len + name_len + 1);
946 attr->type = ntohs (attr_ser->attestation_type);
947 attr->version = ntohl (attr_ser->attestation_version);
948 attr->id = GNUNET_ntohll (attr_ser->attestation_id);
949 attr->data_size = data_len;
951 write_ptr = (char *) &attr[1];
952 GNUNET_memcpy (write_ptr, &attr_ser[1], name_len);
953 write_ptr[name_len] = '\0';
954 attr->name = write_ptr;
956 write_ptr += name_len + 1;
957 GNUNET_memcpy (write_ptr, (char *) &attr_ser[1] + name_len, attr->data_size);
958 attr->data = write_ptr;
963 * Get required size for serialization buffer
965 * @param attr the reference to serialize
966 * @return the required buffer size
969 GNUNET_RECLAIM_ATTESTATION_REF_serialize_get_size (
970 const struct GNUNET_RECLAIM_ATTESTATION_REFERENCE *attr)
972 return sizeof(struct Attestation_Reference) + strlen (attr->name) + strlen (
973 attr->reference_value);
978 * Serialize a reference
980 * @param attr the reference to serialize
981 * @param result the serialized reference
982 * @return length of serialized data
985 GNUNET_RECLAIM_ATTESTATION_REF_serialize (
986 const struct GNUNET_RECLAIM_ATTESTATION_REFERENCE *attr,
991 struct Attestation_Reference *attr_ser;
993 attr_ser = (struct Attestation_Reference *) result;
994 attr_ser->reference_id = GNUNET_htonll (attr->id);
995 attr_ser->attestation_id = GNUNET_htonll (attr->id_attest);
996 name_len = strlen (attr->name);
997 refval_len = strlen (attr->reference_value);
998 attr_ser->name_len = htons (name_len);
999 attr_ser->ref_value_len = htons (refval_len);
1000 write_ptr = (char *) &attr_ser[1];
1001 GNUNET_memcpy (write_ptr, attr->name, name_len);
1002 write_ptr += name_len;
1003 GNUNET_memcpy (write_ptr, attr->reference_value, refval_len);
1005 return sizeof(struct Attestation_Reference) + strlen (attr->name) + strlen (
1006 attr->reference_value);
1011 * Deserialize a reference
1013 * @param data the serialized reference
1014 * @param data_size the length of the serialized data
1016 * @return a GNUNET_IDENTITY_PROVIDER_Attribute, must be free'd by caller
1018 struct GNUNET_RECLAIM_ATTESTATION_REFERENCE *
1019 GNUNET_RECLAIM_ATTESTATION_REF_deserialize (const char *data, size_t data_size)
1021 struct GNUNET_RECLAIM_ATTESTATION_REFERENCE *attr;
1022 struct Attestation_Reference *attr_ser;
1027 if (data_size < sizeof(struct Attestation_Reference))
1029 attr_ser = (struct Attestation_Reference *) data;
1030 name_len = ntohs (attr_ser->name_len);
1031 refval_len = ntohs (attr_ser->ref_value_len);
1032 if (data_size < sizeof(struct Attestation_Reference) + refval_len + name_len)
1034 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1035 "Buffer too small to deserialize\n");
1038 attr = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_ATTESTATION_REFERENCE)
1039 + refval_len + name_len + 2);
1041 attr->id = GNUNET_ntohll (attr_ser->reference_id);
1042 attr->id_attest = GNUNET_ntohll (attr_ser->attestation_id);
1044 write_ptr = (char *) &attr[1];
1045 GNUNET_memcpy (write_ptr, &attr_ser[1], name_len);
1046 write_ptr[name_len] = '\0';
1047 attr->name = write_ptr;
1049 write_ptr += name_len + 1;
1050 GNUNET_memcpy (write_ptr, (char *) &attr_ser[1] + name_len, refval_len);
1051 write_ptr[refval_len] = '\0';
1052 attr->reference_value = write_ptr;
1055 /* end of reclaim_attribute.c */