aboutsummaryrefslogtreecommitdiff
path: root/src/reclaim/reclaim_credential.c
diff options
context:
space:
mode:
authorMartin Schanzenbach <schanzen@gnunet.org>2023-10-19 12:49:08 +0200
committerMartin Schanzenbach <schanzen@gnunet.org>2023-10-19 12:49:08 +0200
commit39e327905f421c470b9cbd5c1ea548261bcae026 (patch)
treefa0df2a80fe4c785e12c609f619eb5f95e55904c /src/reclaim/reclaim_credential.c
parenta2de0769515468c733b72698f04b24d03190f719 (diff)
downloadgnunet-39e327905f421c470b9cbd5c1ea548261bcae026.tar.gz
gnunet-39e327905f421c470b9cbd5c1ea548261bcae026.zip
BUILD: Move reclaim to service
Diffstat (limited to 'src/reclaim/reclaim_credential.c')
-rw-r--r--src/reclaim/reclaim_credential.c1004
1 files changed, 0 insertions, 1004 deletions
diff --git a/src/reclaim/reclaim_credential.c b/src/reclaim/reclaim_credential.c
deleted file mode 100644
index 1aad261a1..000000000
--- a/src/reclaim/reclaim_credential.c
+++ /dev/null
@@ -1,1004 +0,0 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2010-2015 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 * @file reclaim/reclaim_credential.c
23 * @brief helper library to manage identity attribute credentials
24 * @author Martin Schanzenbach
25 */
26#include "platform.h"
27#include "gnunet_util_lib.h"
28#include "gnunet_reclaim_plugin.h"
29#include "reclaim_credential.h"
30
31
32/**
33 * Handle for a plugin
34 */
35struct Plugin
36{
37 /**
38 * Name of the plugin
39 */
40 char *library_name;
41
42 /**
43 * Plugin API
44 */
45 struct GNUNET_RECLAIM_CredentialPluginFunctions *api;
46};
47
48
49/**
50 * Plugins
51 */
52static struct Plugin **credential_plugins;
53
54
55/**
56 * Number of plugins
57 */
58static unsigned int num_plugins;
59
60
61/**
62 * Init canary
63 */
64static int initialized;
65
66
67/**
68 * Add a plugin
69 *
70 * @param cls closure
71 * @param library_name name of the API library
72 * @param lib_ret the plugin API pointer
73 */
74static void
75add_plugin (void *cls, const char *library_name, void *lib_ret)
76{
77 struct GNUNET_RECLAIM_CredentialPluginFunctions *api = lib_ret;
78 struct Plugin *plugin;
79
80 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
81 "Loading credential plugin `%s'\n",
82 library_name);
83 plugin = GNUNET_new (struct Plugin);
84 plugin->api = api;
85 plugin->library_name = GNUNET_strdup (library_name);
86 GNUNET_array_append (credential_plugins, num_plugins, plugin);
87}
88
89
90/**
91 * Load plugins
92 */
93static void
94init ()
95{
96 if (GNUNET_YES == initialized)
97 return;
98 initialized = GNUNET_YES;
99 GNUNET_PLUGIN_load_all_in_context (GNUNET_OS_project_data_default (),
100 "libgnunet_plugin_reclaim_credential_",
101 NULL,
102 &add_plugin,
103 NULL);
104}
105
106
107/**
108 * Dual function to #init().
109 */
110void __attribute__ ((destructor))
111RECLAIM_CREDENTIAL_fini ()
112{
113 struct Plugin *plugin;
114 const struct GNUNET_OS_ProjectData *pd = GNUNET_OS_project_data_get ();
115 const struct GNUNET_OS_ProjectData *dpd = GNUNET_OS_project_data_default ();
116
117 if (pd != dpd)
118 GNUNET_OS_init (dpd);
119
120 for (unsigned int i = 0; i < num_plugins; i++)
121 {
122 plugin = credential_plugins[i];
123 GNUNET_break (NULL ==
124 GNUNET_PLUGIN_unload (plugin->library_name,
125 plugin->api));
126 GNUNET_free (plugin->library_name);
127 GNUNET_free (plugin);
128 }
129 GNUNET_free (credential_plugins);
130
131 if (pd != dpd)
132 GNUNET_OS_init (pd);
133
134 credential_plugins = NULL;
135}
136
137
138/**
139 * Convert an credential type name to the corresponding number
140 *
141 * @param typename name to convert
142 * @return corresponding number, UINT32_MAX on error
143 */
144uint32_t
145GNUNET_RECLAIM_credential_typename_to_number (const char *typename)
146{
147 unsigned int i;
148 struct Plugin *plugin;
149 uint32_t ret;
150 init ();
151 for (i = 0; i < num_plugins; i++)
152 {
153 plugin = credential_plugins[i];
154 if (UINT32_MAX !=
155 (ret = plugin->api->typename_to_number (plugin->api->cls,
156 typename)))
157 return ret;
158 }
159 return UINT32_MAX;
160}
161
162
163/**
164 * Convert an credential type number to the corresponding credential type string
165 *
166 * @param type number of a type
167 * @return corresponding typestring, NULL on error
168 */
169const char *
170GNUNET_RECLAIM_credential_number_to_typename (uint32_t type)
171{
172 unsigned int i;
173 struct Plugin *plugin;
174 const char *ret;
175
176 init ();
177 for (i = 0; i < num_plugins; i++)
178 {
179 plugin = credential_plugins[i];
180 if (NULL !=
181 (ret = plugin->api->number_to_typename (plugin->api->cls, type)))
182 return ret;
183 }
184 return NULL;
185}
186
187
188/**
189 * Convert human-readable version of a 'claim' of an credential to the binary
190 * representation
191 *
192 * @param type type of the claim
193 * @param s human-readable string
194 * @param data set to value in binary encoding (will be allocated)
195 * @param data_size set to number of bytes in @a data
196 * @return #GNUNET_OK on success
197 */
198int
199GNUNET_RECLAIM_credential_string_to_value (uint32_t type,
200 const char *s,
201 void **data,
202 size_t *data_size)
203{
204 unsigned int i;
205 struct Plugin *plugin;
206
207 init ();
208 for (i = 0; i < num_plugins; i++)
209 {
210 plugin = credential_plugins[i];
211 if (GNUNET_OK == plugin->api->string_to_value (plugin->api->cls,
212 type,
213 s,
214 data,
215 data_size))
216 return GNUNET_OK;
217 }
218 return GNUNET_SYSERR;
219}
220
221
222/**
223 * Convert the 'claim' of an credential to a string
224 *
225 * @param type the type of credential
226 * @param data claim in binary encoding
227 * @param data_size number of bytes in @a data
228 * @return NULL on error, otherwise human-readable representation of the claim
229 */
230char *
231GNUNET_RECLAIM_credential_value_to_string (uint32_t type,
232 const void *data,
233 size_t data_size)
234{
235 unsigned int i;
236 struct Plugin *plugin;
237 char *ret;
238
239 init ();
240 for (i = 0; i < num_plugins; i++)
241 {
242 plugin = credential_plugins[i];
243 if (NULL != (ret = plugin->api->value_to_string (plugin->api->cls,
244 type,
245 data,
246 data_size)))
247 return ret;
248 }
249 return NULL;
250}
251
252
253struct GNUNET_RECLAIM_Credential *
254GNUNET_RECLAIM_credential_new (const char *attr_name,
255 uint32_t type,
256 const void *data,
257 size_t data_size)
258{
259 struct GNUNET_RECLAIM_Credential *attr;
260 char *write_ptr;
261 char *attr_name_tmp = GNUNET_strdup (attr_name);
262
263 GNUNET_STRINGS_utf8_tolower (attr_name, attr_name_tmp);
264
265 attr = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_Credential)
266 + strlen (attr_name_tmp) + 1 + data_size);
267 attr->type = type;
268 attr->data_size = data_size;
269 attr->flag = 0;
270 write_ptr = (char *) &attr[1];
271 GNUNET_memcpy (write_ptr, attr_name_tmp, strlen (attr_name_tmp) + 1);
272 attr->name = write_ptr;
273 write_ptr += strlen (attr->name) + 1;
274 GNUNET_memcpy (write_ptr, data, data_size);
275 attr->data = write_ptr;
276 GNUNET_free (attr_name_tmp);
277 return attr;
278}
279
280
281/**
282 * Get required size for serialization buffer
283 *
284 * @param attrs the attribute list to serialize
285 * @return the required buffer size
286 */
287size_t
288GNUNET_RECLAIM_credential_list_serialize_get_size (
289 const struct GNUNET_RECLAIM_CredentialList *credentials)
290{
291 struct GNUNET_RECLAIM_CredentialListEntry *le;
292 size_t len = 0;
293
294 for (le = credentials->list_head; NULL != le; le = le->next)
295 {
296 GNUNET_assert (NULL != le->credential);
297 len += GNUNET_RECLAIM_credential_serialize_get_size (le->credential);
298 len += sizeof(struct GNUNET_RECLAIM_CredentialListEntry);
299 }
300 return len;
301}
302
303
304size_t
305GNUNET_RECLAIM_credential_list_serialize (
306 const struct GNUNET_RECLAIM_CredentialList *credentials,
307 char *result)
308{
309 struct GNUNET_RECLAIM_CredentialListEntry *le;
310 size_t len;
311 size_t total_len;
312 char *write_ptr;
313 write_ptr = result;
314 total_len = 0;
315 for (le = credentials->list_head; NULL != le; le = le->next)
316 {
317 GNUNET_assert (NULL != le->credential);
318 len = GNUNET_RECLAIM_credential_serialize (le->credential, write_ptr);
319 total_len += len;
320 write_ptr += len;
321 }
322 return total_len;
323}
324
325
326struct GNUNET_RECLAIM_CredentialList *
327GNUNET_RECLAIM_credential_list_deserialize (const char *data, size_t data_size)
328{
329 struct GNUNET_RECLAIM_CredentialList *al;
330 struct GNUNET_RECLAIM_CredentialListEntry *ale;
331 size_t att_len;
332 const char *read_ptr;
333
334 al = GNUNET_new (struct GNUNET_RECLAIM_CredentialList);
335
336 if ((data_size < sizeof(struct
337 Credential)
338 + sizeof(struct GNUNET_RECLAIM_CredentialListEntry)))
339 return al;
340
341 read_ptr = data;
342 while (((data + data_size) - read_ptr) >= sizeof(struct Credential))
343 {
344 ale = GNUNET_new (struct GNUNET_RECLAIM_CredentialListEntry);
345 ale->credential =
346 GNUNET_RECLAIM_credential_deserialize (read_ptr,
347 data_size - (read_ptr - data));
348 if (NULL == ale->credential)
349 {
350 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
351 "Failed to deserialize malformed credential.\n");
352 GNUNET_free (ale);
353 return al;
354 }
355 GNUNET_CONTAINER_DLL_insert (al->list_head, al->list_tail, ale);
356 att_len = GNUNET_RECLAIM_credential_serialize_get_size (ale->credential);
357 read_ptr += att_len;
358 }
359 return al;
360}
361
362
363/**
364 * Make a (deep) copy of the credential list
365 * @param attrs claim list to copy
366 * @return copied claim list
367 */
368struct GNUNET_RECLAIM_CredentialList *
369GNUNET_RECLAIM_credential_list_dup (
370 const struct GNUNET_RECLAIM_CredentialList *al)
371{
372 struct GNUNET_RECLAIM_CredentialListEntry *ale;
373 struct GNUNET_RECLAIM_CredentialListEntry *result_ale;
374 struct GNUNET_RECLAIM_CredentialList *result;
375
376 result = GNUNET_new (struct GNUNET_RECLAIM_CredentialList);
377 for (ale = al->list_head; NULL != ale; ale = ale->next)
378 {
379 result_ale = GNUNET_new (struct GNUNET_RECLAIM_CredentialListEntry);
380 GNUNET_assert (NULL != ale->credential);
381 result_ale->credential =
382 GNUNET_RECLAIM_credential_new (ale->credential->name,
383 ale->credential->type,
384 ale->credential->data,
385 ale->credential->data_size);
386 result_ale->credential->id = ale->credential->id;
387 GNUNET_CONTAINER_DLL_insert (result->list_head,
388 result->list_tail,
389 result_ale);
390 }
391 return result;
392}
393
394
395void
396GNUNET_RECLAIM_credential_list_destroy (
397 struct GNUNET_RECLAIM_CredentialList *credentials)
398{
399 struct GNUNET_RECLAIM_CredentialListEntry *ale;
400 struct GNUNET_RECLAIM_CredentialListEntry *tmp_ale;
401
402 for (ale = credentials->list_head; NULL != ale;)
403 {
404 if (NULL != ale->credential)
405 GNUNET_free (ale->credential);
406 tmp_ale = ale;
407 ale = ale->next;
408 GNUNET_free (tmp_ale);
409 }
410 GNUNET_free (credentials);
411}
412
413
414/**
415 * Get required size for serialization buffer
416 *
417 * @param attr the credential to serialize
418 * @return the required buffer size
419 */
420size_t
421GNUNET_RECLAIM_credential_serialize_get_size (
422 const struct GNUNET_RECLAIM_Credential *credential)
423{
424 return sizeof(struct Credential) + strlen (credential->name)
425 + credential->data_size;
426}
427
428
429size_t
430GNUNET_RECLAIM_credential_serialize (
431 const struct GNUNET_RECLAIM_Credential *credential,
432 char *result)
433{
434 size_t data_len_ser;
435 size_t name_len;
436 struct Credential *atts;
437 char *write_ptr;
438
439 atts = (struct Credential *) result;
440 atts->credential_type = htonl (credential->type);
441 atts->credential_flag = htonl (credential->flag);
442 atts->credential_id = credential->id;
443 name_len = strlen (credential->name);
444 atts->name_len = htons (name_len);
445 write_ptr = (char *) &atts[1];
446 GNUNET_memcpy (write_ptr, credential->name, name_len);
447 write_ptr += name_len;
448 // TODO plugin-ize
449 // data_len_ser = plugin->serialize_attribute_value (attr,
450 // &attr_ser[1]);
451 data_len_ser = credential->data_size;
452 GNUNET_memcpy (write_ptr, credential->data, credential->data_size);
453 atts->data_size = htons (data_len_ser);
454
455 return sizeof(struct Credential) + strlen (credential->name)
456 + credential->data_size;
457}
458
459
460/**
461 * Deserialize an credential
462 *
463 * @param data the serialized credential
464 * @param data_size the length of the serialized data
465 *
466 * @return a GNUNET_IDENTITY_PROVIDER_Attribute, must be free'd by caller
467 */
468struct GNUNET_RECLAIM_Credential *
469GNUNET_RECLAIM_credential_deserialize (const char *data, size_t data_size)
470{
471 struct GNUNET_RECLAIM_Credential *credential;
472 struct Credential *atts;
473 size_t data_len;
474 size_t name_len;
475 char *write_ptr;
476
477 if (data_size < sizeof(struct Credential))
478 return NULL;
479
480 atts = (struct Credential *) data;
481 data_len = ntohs (atts->data_size);
482 name_len = ntohs (atts->name_len);
483 if (data_size < sizeof(struct Credential) + data_len + name_len)
484 {
485 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
486 "Buffer too small to deserialize\n");
487 return NULL;
488 }
489 credential = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_Credential)
490 + data_len + name_len + 1);
491 credential->type = ntohl (atts->credential_type);
492 credential->flag = ntohl (atts->credential_flag);
493 credential->id = atts->credential_id;
494 credential->data_size = data_len;
495
496 write_ptr = (char *) &credential[1];
497 GNUNET_memcpy (write_ptr, &atts[1], name_len);
498 write_ptr[name_len] = '\0';
499 credential->name = write_ptr;
500
501 write_ptr += name_len + 1;
502 GNUNET_memcpy (write_ptr, (char *) &atts[1] + name_len,
503 credential->data_size);
504 credential->data = write_ptr;
505 return credential;
506}
507
508
509struct GNUNET_RECLAIM_AttributeList*
510GNUNET_RECLAIM_credential_get_attributes (const struct
511 GNUNET_RECLAIM_Credential *credential)
512{
513 unsigned int i;
514 struct Plugin *plugin;
515 struct GNUNET_RECLAIM_AttributeList *ret;
516 init ();
517 for (i = 0; i < num_plugins; i++)
518 {
519 plugin = credential_plugins[i];
520 if (NULL !=
521 (ret = plugin->api->get_attributes (plugin->api->cls,
522 credential)))
523 return ret;
524 }
525 return NULL;
526}
527
528
529char*
530GNUNET_RECLAIM_credential_get_issuer (const struct
531 GNUNET_RECLAIM_Credential *credential)
532{
533 unsigned int i;
534 struct Plugin *plugin;
535 char *ret;
536 init ();
537 for (i = 0; i < num_plugins; i++)
538 {
539 plugin = credential_plugins[i];
540 if (NULL !=
541 (ret = plugin->api->get_issuer (plugin->api->cls,
542 credential)))
543 return ret;
544 }
545 return NULL;
546}
547
548
549int
550GNUNET_RECLAIM_credential_get_expiration (const struct
551 GNUNET_RECLAIM_Credential *credential,
552 struct GNUNET_TIME_Absolute*exp)
553{
554 unsigned int i;
555 struct Plugin *plugin;
556 init ();
557 for (i = 0; i < num_plugins; i++)
558 {
559 plugin = credential_plugins[i];
560 if (GNUNET_OK != plugin->api->get_expiration (plugin->api->cls,
561 credential,
562 exp))
563 continue;
564 return GNUNET_OK;
565 }
566 return GNUNET_SYSERR;
567}
568
569
570/**
571 * Convert an presentation type name to the corresponding number
572 *
573 * @param typename name to convert
574 * @return corresponding number, UINT32_MAX on error
575 */
576uint32_t
577GNUNET_RECLAIM_presentation_typename_to_number (const char *typename)
578{
579 unsigned int i;
580 struct Plugin *plugin;
581 uint32_t ret;
582 init ();
583 for (i = 0; i < num_plugins; i++)
584 {
585 plugin = credential_plugins[i];
586 if (UINT32_MAX !=
587 (ret = plugin->api->typename_to_number_p (plugin->api->cls,
588 typename)))
589 return ret;
590 }
591 return UINT32_MAX;
592}
593
594
595const char *
596GNUNET_RECLAIM_presentation_number_to_typename (uint32_t type)
597{
598 unsigned int i;
599 struct Plugin *plugin;
600 const char *ret;
601
602 init ();
603 for (i = 0; i < num_plugins; i++)
604 {
605 plugin = credential_plugins[i];
606 if (NULL !=
607 (ret = plugin->api->number_to_typename_p (plugin->api->cls, type)))
608 return ret;
609 }
610 return NULL;
611}
612
613
614/**
615 * Convert human-readable version of a 'claim' of an presentation to the binary
616 * representation
617 *
618 * @param type type of the claim
619 * @param s human-readable string
620 * @param data set to value in binary encoding (will be allocated)
621 * @param data_size set to number of bytes in @a data
622 * @return #GNUNET_OK on success
623 */
624int
625GNUNET_RECLAIM_presentation_string_to_value (uint32_t type,
626 const char *s,
627 void **data,
628 size_t *data_size)
629{
630 unsigned int i;
631 struct Plugin *plugin;
632
633 init ();
634 for (i = 0; i < num_plugins; i++)
635 {
636 plugin = credential_plugins[i];
637 if (GNUNET_OK == plugin->api->string_to_value_p (plugin->api->cls,
638 type,
639 s,
640 data,
641 data_size))
642 return GNUNET_OK;
643 }
644 return GNUNET_SYSERR;
645}
646
647
648/**
649 * Convert the 'claim' of an presentation to a string
650 *
651 * @param type the type of presentation
652 * @param data claim in binary encoding
653 * @param data_size number of bytes in @a data
654 * @return NULL on error, otherwise human-readable representation of the claim
655 */
656char *
657GNUNET_RECLAIM_presentation_value_to_string (uint32_t type,
658 const void *data,
659 size_t data_size)
660{
661 unsigned int i;
662 struct Plugin *plugin;
663 char *ret;
664
665 init ();
666 for (i = 0; i < num_plugins; i++)
667 {
668 plugin = credential_plugins[i];
669 if (NULL != (ret = plugin->api->value_to_string_p (plugin->api->cls,
670 type,
671 data,
672 data_size)))
673 return ret;
674 }
675 return NULL;
676}
677
678
679struct GNUNET_RECLAIM_Presentation *
680GNUNET_RECLAIM_presentation_new (uint32_t type,
681 const void *data,
682 size_t data_size)
683{
684 struct GNUNET_RECLAIM_Presentation *attr;
685 char *write_ptr;
686
687 attr = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_Presentation)
688 + data_size);
689 attr->type = type;
690 attr->data_size = data_size;
691 write_ptr = (char *) &attr[1];
692 GNUNET_memcpy (write_ptr, data, data_size);
693 attr->data = write_ptr;
694 return attr;
695}
696
697
698/**
699 * Get required size for serialization buffer
700 *
701 * @param attrs the attribute list to serialize
702 * @return the required buffer size
703 */
704size_t
705GNUNET_RECLAIM_presentation_list_serialize_get_size (
706 const struct GNUNET_RECLAIM_PresentationList *presentations)
707{
708 struct GNUNET_RECLAIM_PresentationListEntry *le;
709 size_t len = 0;
710
711 for (le = presentations->list_head; NULL != le; le = le->next)
712 {
713 GNUNET_assert (NULL != le->presentation);
714 len += GNUNET_RECLAIM_presentation_serialize_get_size (le->presentation);
715 }
716 return len;
717}
718
719
720size_t
721GNUNET_RECLAIM_presentation_list_serialize (
722 const struct GNUNET_RECLAIM_PresentationList *presentations,
723 char *result)
724{
725 struct GNUNET_RECLAIM_PresentationListEntry *le;
726 size_t len;
727 size_t total_len;
728 char *write_ptr;
729 write_ptr = result;
730 total_len = 0;
731 for (le = presentations->list_head; NULL != le; le = le->next)
732 {
733 GNUNET_assert (NULL != le->presentation);
734 len = GNUNET_RECLAIM_presentation_serialize (le->presentation, write_ptr);
735 total_len += len;
736 write_ptr += len;
737 }
738 return total_len;
739}
740
741
742/**
743 * Deserialize an presentation list
744 *
745 * @param data the serialized attribute list
746 * @param data_size the length of the serialized data
747 * @return a GNUNET_IDENTITY_PROVIDER_AttributeList, must be free'd by caller
748 */
749struct GNUNET_RECLAIM_PresentationList *
750GNUNET_RECLAIM_presentation_list_deserialize (const char *data, size_t
751 data_size)
752{
753 struct GNUNET_RECLAIM_PresentationList *al;
754 struct GNUNET_RECLAIM_PresentationListEntry *ale;
755 size_t att_len;
756 const char *read_ptr;
757
758 al = GNUNET_new (struct GNUNET_RECLAIM_PresentationList);
759
760 if (data_size < sizeof(struct Presentation))
761 return al;
762
763 read_ptr = data;
764 while (((data + data_size) - read_ptr) >= sizeof(struct Presentation))
765 {
766 ale = GNUNET_new (struct GNUNET_RECLAIM_PresentationListEntry);
767 ale->presentation =
768 GNUNET_RECLAIM_presentation_deserialize (read_ptr,
769 data_size - (read_ptr - data));
770 if (NULL == ale->presentation)
771 {
772 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
773 "Failed to deserialize malformed presentation.\n");
774 GNUNET_free (ale);
775 return al;
776 }
777 GNUNET_CONTAINER_DLL_insert (al->list_head, al->list_tail, ale);
778 att_len = GNUNET_RECLAIM_presentation_serialize_get_size (
779 ale->presentation);
780 read_ptr += att_len;
781 }
782 return al;
783}
784
785
786/**
787 * Make a (deep) copy of the presentation list
788 * @param attrs claim list to copy
789 * @return copied claim list
790 */
791struct GNUNET_RECLAIM_PresentationList *
792GNUNET_RECLAIM_presentation_list_dup (
793 const struct GNUNET_RECLAIM_PresentationList *al)
794{
795 struct GNUNET_RECLAIM_PresentationListEntry *ale;
796 struct GNUNET_RECLAIM_PresentationListEntry *result_ale;
797 struct GNUNET_RECLAIM_PresentationList *result;
798
799 result = GNUNET_new (struct GNUNET_RECLAIM_PresentationList);
800 for (ale = al->list_head; NULL != ale; ale = ale->next)
801 {
802 result_ale = GNUNET_new (struct GNUNET_RECLAIM_PresentationListEntry);
803 GNUNET_assert (NULL != ale->presentation);
804 result_ale->presentation =
805 GNUNET_RECLAIM_presentation_new (ale->presentation->type,
806 ale->presentation->data,
807 ale->presentation->data_size);
808 result_ale->presentation->credential_id = ale->presentation->credential_id;
809 GNUNET_CONTAINER_DLL_insert (result->list_head,
810 result->list_tail,
811 result_ale);
812 }
813 return result;
814}
815
816
817void
818GNUNET_RECLAIM_presentation_list_destroy (
819 struct GNUNET_RECLAIM_PresentationList *presentations)
820{
821 struct GNUNET_RECLAIM_PresentationListEntry *ale;
822 struct GNUNET_RECLAIM_PresentationListEntry *tmp_ale;
823
824 for (ale = presentations->list_head; NULL != ale;)
825 {
826 if (NULL != ale->presentation)
827 GNUNET_free (ale->presentation);
828 tmp_ale = ale;
829 ale = ale->next;
830 GNUNET_free (tmp_ale);
831 }
832 GNUNET_free (presentations);
833}
834
835
836/**
837 * Get required size for serialization buffer
838 *
839 * @param attr the presentation to serialize
840 * @return the required buffer size
841 */
842size_t
843GNUNET_RECLAIM_presentation_serialize_get_size (
844 const struct GNUNET_RECLAIM_Presentation *presentation)
845{
846 return sizeof(struct Presentation) + presentation->data_size;
847}
848
849
850size_t
851GNUNET_RECLAIM_presentation_serialize (
852 const struct GNUNET_RECLAIM_Presentation *presentation,
853 char *result)
854{
855 struct Presentation *atts;
856 char *write_ptr;
857
858 atts = (struct Presentation *) result;
859 atts->presentation_type = htonl (presentation->type);
860 atts->credential_id = presentation->credential_id;
861 write_ptr = (char *) &atts[1];
862 GNUNET_memcpy (write_ptr, presentation->data, presentation->data_size);
863 atts->data_size = htons (presentation->data_size);
864
865 return sizeof(struct Presentation) + presentation->data_size;
866}
867
868
869/**
870 * Deserialize an presentation
871 *
872 * @param data the serialized presentation
873 * @param data_size the length of the serialized data
874 *
875 * @return a GNUNET_IDENTITY_PROVIDER_Attribute, must be free'd by caller
876 */
877struct GNUNET_RECLAIM_Presentation *
878GNUNET_RECLAIM_presentation_deserialize (const char *data, size_t data_size)
879{
880 struct GNUNET_RECLAIM_Presentation *presentation;
881 struct Presentation *atts;
882 size_t data_len;
883 char *write_ptr;
884
885 if (data_size < sizeof(struct Presentation))
886 return NULL;
887
888 atts = (struct Presentation *) data;
889 data_len = ntohs (atts->data_size);
890 if (data_size < sizeof(struct Presentation) + data_len)
891 {
892 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
893 "Buffer too small to deserialize\n");
894 return NULL;
895 }
896 presentation = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_Presentation)
897 + data_len);
898 presentation->type = ntohl (atts->presentation_type);
899 presentation->credential_id = atts->credential_id;
900 presentation->data_size = data_len;
901
902 write_ptr = (char *) &presentation[1];
903 GNUNET_memcpy (write_ptr, &atts[1], data_len);
904 presentation->data = write_ptr;
905 return presentation;
906}
907
908
909struct GNUNET_RECLAIM_AttributeList*
910GNUNET_RECLAIM_presentation_get_attributes (const struct
911 GNUNET_RECLAIM_Presentation *
912 presentation)
913{
914 unsigned int i;
915 struct Plugin *plugin;
916 struct GNUNET_RECLAIM_AttributeList *ret;
917 init ();
918 for (i = 0; i < num_plugins; i++)
919 {
920 plugin = credential_plugins[i];
921 if (NULL !=
922 (ret = plugin->api->get_attributes_p (plugin->api->cls,
923 presentation)))
924 return ret;
925 }
926 return NULL;
927}
928
929
930char*
931GNUNET_RECLAIM_presentation_get_issuer (const struct
932 GNUNET_RECLAIM_Presentation *
933 presentation)
934{
935 unsigned int i;
936 struct Plugin *plugin;
937 char *ret;
938 init ();
939 for (i = 0; i < num_plugins; i++)
940 {
941 plugin = credential_plugins[i];
942 if (NULL !=
943 (ret = plugin->api->get_issuer_p (plugin->api->cls,
944 presentation)))
945 return ret;
946 }
947 return NULL;
948}
949
950
951int
952GNUNET_RECLAIM_presentation_get_expiration (const struct
953 GNUNET_RECLAIM_Presentation *
954 presentation,
955 struct GNUNET_TIME_Absolute*exp)
956{
957 unsigned int i;
958 struct Plugin *plugin;
959 init ();
960 for (i = 0; i < num_plugins; i++)
961 {
962 plugin = credential_plugins[i];
963 if (GNUNET_OK != plugin->api->get_expiration_p (plugin->api->cls,
964 presentation,
965 exp))
966 continue;
967 return GNUNET_OK;
968 }
969 return GNUNET_SYSERR;
970}
971
972
973/**
974 * Create a presentation from a credential and a lift of (selected)
975 * attributes in the credential.
976 *
977 * @param cred the credential to use
978 * @param attrs the attributes to present from the credential
979 * @return the credential presentation presenting the attributes according
980 * to the presentation mechanism of the credential
981 * or NULL on error.
982 */
983int
984GNUNET_RECLAIM_credential_get_presentation (
985 const struct GNUNET_RECLAIM_Credential *cred,
986 const struct GNUNET_RECLAIM_AttributeList *attrs,
987 struct GNUNET_RECLAIM_Presentation **presentation)
988{
989 unsigned int i;
990 struct Plugin *plugin;
991 init ();
992 for (i = 0; i < num_plugins; i++)
993 {
994 plugin = credential_plugins[i];
995 if (GNUNET_OK != plugin->api->create_presentation (plugin->api->cls,
996 cred,
997 attrs,
998 presentation))
999 continue;
1000 (*presentation)->credential_id = cred->id;
1001 return GNUNET_OK;
1002 }
1003 return GNUNET_SYSERR;
1004}