diff options
Diffstat (limited to 'src/reclaim/reclaim_credential.c')
-rw-r--r-- | src/reclaim/reclaim_credential.c | 1064 |
1 files changed, 0 insertions, 1064 deletions
diff --git a/src/reclaim/reclaim_credential.c b/src/reclaim/reclaim_credential.c deleted file mode 100644 index b4aeedf29..000000000 --- a/src/reclaim/reclaim_credential.c +++ /dev/null | |||
@@ -1,1064 +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 | */ | ||
35 | struct 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 | */ | ||
52 | static struct Plugin **credential_plugins; | ||
53 | |||
54 | |||
55 | /** | ||
56 | * Number of plugins | ||
57 | */ | ||
58 | static unsigned int num_plugins; | ||
59 | |||
60 | |||
61 | /** | ||
62 | * Init canary | ||
63 | */ | ||
64 | static 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 | */ | ||
74 | static void | ||
75 | add_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 | */ | ||
93 | static void | ||
94 | init () | ||
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 | */ | ||
110 | void __attribute__ ((destructor)) | ||
111 | RECLAIM_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 | */ | ||
144 | uint32_t | ||
145 | GNUNET_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 | */ | ||
169 | const char * | ||
170 | GNUNET_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 | */ | ||
198 | int | ||
199 | GNUNET_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 | */ | ||
230 | char * | ||
231 | GNUNET_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 | |||
253 | /** | ||
254 | * Create a new credential. | ||
255 | * | ||
256 | * @param attr_name the credential name | ||
257 | * @param type the credential type | ||
258 | * @param data the credential value | ||
259 | * @param data_size the credential value size | ||
260 | * @return the new credential | ||
261 | */ | ||
262 | struct GNUNET_RECLAIM_Credential * | ||
263 | GNUNET_RECLAIM_credential_new (const char *attr_name, | ||
264 | uint32_t type, | ||
265 | const void *data, | ||
266 | size_t data_size) | ||
267 | { | ||
268 | struct GNUNET_RECLAIM_Credential *attr; | ||
269 | char *write_ptr; | ||
270 | char *attr_name_tmp = GNUNET_strdup (attr_name); | ||
271 | |||
272 | GNUNET_STRINGS_utf8_tolower (attr_name, attr_name_tmp); | ||
273 | |||
274 | attr = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_Credential) | ||
275 | + strlen (attr_name_tmp) + 1 + data_size); | ||
276 | attr->type = type; | ||
277 | attr->data_size = data_size; | ||
278 | attr->flag = 0; | ||
279 | write_ptr = (char *) &attr[1]; | ||
280 | GNUNET_memcpy (write_ptr, attr_name_tmp, strlen (attr_name_tmp) + 1); | ||
281 | attr->name = write_ptr; | ||
282 | write_ptr += strlen (attr->name) + 1; | ||
283 | GNUNET_memcpy (write_ptr, data, data_size); | ||
284 | attr->data = write_ptr; | ||
285 | GNUNET_free (attr_name_tmp); | ||
286 | return attr; | ||
287 | } | ||
288 | |||
289 | |||
290 | /** | ||
291 | * Get required size for serialization buffer | ||
292 | * | ||
293 | * @param attrs the attribute list to serialize | ||
294 | * @return the required buffer size | ||
295 | */ | ||
296 | size_t | ||
297 | GNUNET_RECLAIM_credential_list_serialize_get_size ( | ||
298 | const struct GNUNET_RECLAIM_CredentialList *credentials) | ||
299 | { | ||
300 | struct GNUNET_RECLAIM_CredentialListEntry *le; | ||
301 | size_t len = 0; | ||
302 | |||
303 | for (le = credentials->list_head; NULL != le; le = le->next) | ||
304 | { | ||
305 | GNUNET_assert (NULL != le->credential); | ||
306 | len += GNUNET_RECLAIM_credential_serialize_get_size (le->credential); | ||
307 | len += sizeof(struct GNUNET_RECLAIM_CredentialListEntry); | ||
308 | } | ||
309 | return len; | ||
310 | } | ||
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_credential_list_serialize ( | ||
322 | const struct GNUNET_RECLAIM_CredentialList *credentials, | ||
323 | char *result) | ||
324 | { | ||
325 | struct GNUNET_RECLAIM_CredentialListEntry *le; | ||
326 | size_t len; | ||
327 | size_t total_len; | ||
328 | char *write_ptr; | ||
329 | write_ptr = result; | ||
330 | total_len = 0; | ||
331 | for (le = credentials->list_head; NULL != le; le = le->next) | ||
332 | { | ||
333 | GNUNET_assert (NULL != le->credential); | ||
334 | len = GNUNET_RECLAIM_credential_serialize (le->credential, write_ptr); | ||
335 | total_len += len; | ||
336 | write_ptr += len; | ||
337 | } | ||
338 | return total_len; | ||
339 | } | ||
340 | |||
341 | |||
342 | /** | ||
343 | * Deserialize an credential list | ||
344 | * | ||
345 | * @param data the serialized attribute list | ||
346 | * @param data_size the length of the serialized data | ||
347 | * @return a GNUNET_IDENTITY_PROVIDER_AttributeList, must be free'd by caller | ||
348 | */ | ||
349 | struct GNUNET_RECLAIM_CredentialList * | ||
350 | GNUNET_RECLAIM_credential_list_deserialize (const char *data, size_t data_size) | ||
351 | { | ||
352 | struct GNUNET_RECLAIM_CredentialList *al; | ||
353 | struct GNUNET_RECLAIM_CredentialListEntry *ale; | ||
354 | size_t att_len; | ||
355 | const char *read_ptr; | ||
356 | |||
357 | al = GNUNET_new (struct GNUNET_RECLAIM_CredentialList); | ||
358 | |||
359 | if ((data_size < sizeof(struct | ||
360 | Credential) | ||
361 | + sizeof(struct GNUNET_RECLAIM_CredentialListEntry))) | ||
362 | return al; | ||
363 | |||
364 | read_ptr = data; | ||
365 | while (((data + data_size) - read_ptr) >= sizeof(struct Credential)) | ||
366 | { | ||
367 | ale = GNUNET_new (struct GNUNET_RECLAIM_CredentialListEntry); | ||
368 | ale->credential = | ||
369 | GNUNET_RECLAIM_credential_deserialize (read_ptr, | ||
370 | data_size - (read_ptr - data)); | ||
371 | if (NULL == ale->credential) | ||
372 | { | ||
373 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
374 | "Failed to deserialize malformed credential.\n"); | ||
375 | GNUNET_free (ale); | ||
376 | return al; | ||
377 | } | ||
378 | GNUNET_CONTAINER_DLL_insert (al->list_head, al->list_tail, ale); | ||
379 | att_len = GNUNET_RECLAIM_credential_serialize_get_size (ale->credential); | ||
380 | read_ptr += att_len; | ||
381 | } | ||
382 | return al; | ||
383 | } | ||
384 | |||
385 | |||
386 | /** | ||
387 | * Make a (deep) copy of the credential list | ||
388 | * @param attrs claim list to copy | ||
389 | * @return copied claim list | ||
390 | */ | ||
391 | struct GNUNET_RECLAIM_CredentialList * | ||
392 | GNUNET_RECLAIM_credential_list_dup ( | ||
393 | const struct GNUNET_RECLAIM_CredentialList *al) | ||
394 | { | ||
395 | struct GNUNET_RECLAIM_CredentialListEntry *ale; | ||
396 | struct GNUNET_RECLAIM_CredentialListEntry *result_ale; | ||
397 | struct GNUNET_RECLAIM_CredentialList *result; | ||
398 | |||
399 | result = GNUNET_new (struct GNUNET_RECLAIM_CredentialList); | ||
400 | for (ale = al->list_head; NULL != ale; ale = ale->next) | ||
401 | { | ||
402 | result_ale = GNUNET_new (struct GNUNET_RECLAIM_CredentialListEntry); | ||
403 | GNUNET_assert (NULL != ale->credential); | ||
404 | result_ale->credential = | ||
405 | GNUNET_RECLAIM_credential_new (ale->credential->name, | ||
406 | ale->credential->type, | ||
407 | ale->credential->data, | ||
408 | ale->credential->data_size); | ||
409 | result_ale->credential->id = ale->credential->id; | ||
410 | GNUNET_CONTAINER_DLL_insert (result->list_head, | ||
411 | result->list_tail, | ||
412 | result_ale); | ||
413 | } | ||
414 | return result; | ||
415 | } | ||
416 | |||
417 | |||
418 | /** | ||
419 | * Destroy credential list | ||
420 | * | ||
421 | * @param attrs list to destroy | ||
422 | */ | ||
423 | void | ||
424 | GNUNET_RECLAIM_credential_list_destroy ( | ||
425 | struct GNUNET_RECLAIM_CredentialList *al) | ||
426 | { | ||
427 | struct GNUNET_RECLAIM_CredentialListEntry *ale; | ||
428 | struct GNUNET_RECLAIM_CredentialListEntry *tmp_ale; | ||
429 | |||
430 | for (ale = al->list_head; NULL != ale;) | ||
431 | { | ||
432 | if (NULL != ale->credential) | ||
433 | GNUNET_free (ale->credential); | ||
434 | tmp_ale = ale; | ||
435 | ale = ale->next; | ||
436 | GNUNET_free (tmp_ale); | ||
437 | } | ||
438 | GNUNET_free (al); | ||
439 | } | ||
440 | |||
441 | |||
442 | /** | ||
443 | * Get required size for serialization buffer | ||
444 | * | ||
445 | * @param attr the credential to serialize | ||
446 | * @return the required buffer size | ||
447 | */ | ||
448 | size_t | ||
449 | GNUNET_RECLAIM_credential_serialize_get_size ( | ||
450 | const struct GNUNET_RECLAIM_Credential *credential) | ||
451 | { | ||
452 | return sizeof(struct Credential) + strlen (credential->name) | ||
453 | + credential->data_size; | ||
454 | } | ||
455 | |||
456 | |||
457 | /** | ||
458 | * Serialize an credential | ||
459 | * | ||
460 | * @param attr the credential to serialize | ||
461 | * @param result the serialized credential | ||
462 | * @return length of serialized data | ||
463 | */ | ||
464 | size_t | ||
465 | GNUNET_RECLAIM_credential_serialize ( | ||
466 | const struct GNUNET_RECLAIM_Credential *credential, | ||
467 | char *result) | ||
468 | { | ||
469 | size_t data_len_ser; | ||
470 | size_t name_len; | ||
471 | struct Credential *atts; | ||
472 | char *write_ptr; | ||
473 | |||
474 | atts = (struct Credential *) result; | ||
475 | atts->credential_type = htons (credential->type); | ||
476 | atts->credential_flag = htonl (credential->flag); | ||
477 | atts->credential_id = credential->id; | ||
478 | name_len = strlen (credential->name); | ||
479 | atts->name_len = htons (name_len); | ||
480 | write_ptr = (char *) &atts[1]; | ||
481 | GNUNET_memcpy (write_ptr, credential->name, name_len); | ||
482 | write_ptr += name_len; | ||
483 | // TODO plugin-ize | ||
484 | // data_len_ser = plugin->serialize_attribute_value (attr, | ||
485 | // &attr_ser[1]); | ||
486 | data_len_ser = credential->data_size; | ||
487 | GNUNET_memcpy (write_ptr, credential->data, credential->data_size); | ||
488 | atts->data_size = htons (data_len_ser); | ||
489 | |||
490 | return sizeof(struct Credential) + strlen (credential->name) | ||
491 | + credential->data_size; | ||
492 | } | ||
493 | |||
494 | |||
495 | /** | ||
496 | * Deserialize an credential | ||
497 | * | ||
498 | * @param data the serialized credential | ||
499 | * @param data_size the length of the serialized data | ||
500 | * | ||
501 | * @return a GNUNET_IDENTITY_PROVIDER_Attribute, must be free'd by caller | ||
502 | */ | ||
503 | struct GNUNET_RECLAIM_Credential * | ||
504 | GNUNET_RECLAIM_credential_deserialize (const char *data, size_t data_size) | ||
505 | { | ||
506 | struct GNUNET_RECLAIM_Credential *credential; | ||
507 | struct Credential *atts; | ||
508 | size_t data_len; | ||
509 | size_t name_len; | ||
510 | char *write_ptr; | ||
511 | |||
512 | if (data_size < sizeof(struct Credential)) | ||
513 | return NULL; | ||
514 | |||
515 | atts = (struct Credential *) data; | ||
516 | data_len = ntohs (atts->data_size); | ||
517 | name_len = ntohs (atts->name_len); | ||
518 | if (data_size < sizeof(struct Credential) + data_len + name_len) | ||
519 | { | ||
520 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
521 | "Buffer too small to deserialize\n"); | ||
522 | return NULL; | ||
523 | } | ||
524 | credential = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_Credential) | ||
525 | + data_len + name_len + 1); | ||
526 | credential->type = ntohs (atts->credential_type); | ||
527 | credential->flag = ntohl (atts->credential_flag); | ||
528 | credential->id = atts->credential_id; | ||
529 | credential->data_size = data_len; | ||
530 | |||
531 | write_ptr = (char *) &credential[1]; | ||
532 | GNUNET_memcpy (write_ptr, &atts[1], name_len); | ||
533 | write_ptr[name_len] = '\0'; | ||
534 | credential->name = write_ptr; | ||
535 | |||
536 | write_ptr += name_len + 1; | ||
537 | GNUNET_memcpy (write_ptr, (char *) &atts[1] + name_len, | ||
538 | credential->data_size); | ||
539 | credential->data = write_ptr; | ||
540 | return credential; | ||
541 | } | ||
542 | |||
543 | |||
544 | struct GNUNET_RECLAIM_AttributeList* | ||
545 | GNUNET_RECLAIM_credential_get_attributes (const struct | ||
546 | GNUNET_RECLAIM_Credential *credential) | ||
547 | { | ||
548 | unsigned int i; | ||
549 | struct Plugin *plugin; | ||
550 | struct GNUNET_RECLAIM_AttributeList *ret; | ||
551 | init (); | ||
552 | for (i = 0; i < num_plugins; i++) | ||
553 | { | ||
554 | plugin = credential_plugins[i]; | ||
555 | if (NULL != | ||
556 | (ret = plugin->api->get_attributes (plugin->api->cls, | ||
557 | credential))) | ||
558 | return ret; | ||
559 | } | ||
560 | return NULL; | ||
561 | } | ||
562 | |||
563 | |||
564 | char* | ||
565 | GNUNET_RECLAIM_credential_get_issuer (const struct | ||
566 | GNUNET_RECLAIM_Credential *credential) | ||
567 | { | ||
568 | unsigned int i; | ||
569 | struct Plugin *plugin; | ||
570 | char *ret; | ||
571 | init (); | ||
572 | for (i = 0; i < num_plugins; i++) | ||
573 | { | ||
574 | plugin = credential_plugins[i]; | ||
575 | if (NULL != | ||
576 | (ret = plugin->api->get_issuer (plugin->api->cls, | ||
577 | credential))) | ||
578 | return ret; | ||
579 | } | ||
580 | return NULL; | ||
581 | } | ||
582 | |||
583 | |||
584 | int | ||
585 | GNUNET_RECLAIM_credential_get_expiration (const struct | ||
586 | GNUNET_RECLAIM_Credential *credential, | ||
587 | struct GNUNET_TIME_Absolute*exp) | ||
588 | { | ||
589 | unsigned int i; | ||
590 | struct Plugin *plugin; | ||
591 | init (); | ||
592 | for (i = 0; i < num_plugins; i++) | ||
593 | { | ||
594 | plugin = credential_plugins[i]; | ||
595 | if (GNUNET_OK != plugin->api->get_expiration (plugin->api->cls, | ||
596 | credential, | ||
597 | exp)) | ||
598 | continue; | ||
599 | return GNUNET_OK; | ||
600 | } | ||
601 | return GNUNET_SYSERR; | ||
602 | } | ||
603 | |||
604 | |||
605 | /** | ||
606 | * Convert an presentation type name to the corresponding number | ||
607 | * | ||
608 | * @param typename name to convert | ||
609 | * @return corresponding number, UINT32_MAX on error | ||
610 | */ | ||
611 | uint32_t | ||
612 | GNUNET_RECLAIM_presentation_typename_to_number (const char *typename) | ||
613 | { | ||
614 | unsigned int i; | ||
615 | struct Plugin *plugin; | ||
616 | uint32_t ret; | ||
617 | init (); | ||
618 | for (i = 0; i < num_plugins; i++) | ||
619 | { | ||
620 | plugin = credential_plugins[i]; | ||
621 | if (UINT32_MAX != | ||
622 | (ret = plugin->api->typename_to_number_p (plugin->api->cls, | ||
623 | typename))) | ||
624 | return ret; | ||
625 | } | ||
626 | return UINT32_MAX; | ||
627 | } | ||
628 | |||
629 | |||
630 | /** | ||
631 | * Convert an presentation type number to the corresponding presentation type string | ||
632 | * | ||
633 | * @param type number of a type | ||
634 | * @return corresponding typestring, NULL on error | ||
635 | */ | ||
636 | const char * | ||
637 | GNUNET_RECLAIM_presentation_number_to_typename (uint32_t type) | ||
638 | { | ||
639 | unsigned int i; | ||
640 | struct Plugin *plugin; | ||
641 | const char *ret; | ||
642 | |||
643 | init (); | ||
644 | for (i = 0; i < num_plugins; i++) | ||
645 | { | ||
646 | plugin = credential_plugins[i]; | ||
647 | if (NULL != | ||
648 | (ret = plugin->api->number_to_typename_p (plugin->api->cls, type))) | ||
649 | return ret; | ||
650 | } | ||
651 | return NULL; | ||
652 | } | ||
653 | |||
654 | |||
655 | /** | ||
656 | * Convert human-readable version of a 'claim' of an presentation to the binary | ||
657 | * representation | ||
658 | * | ||
659 | * @param type type of the claim | ||
660 | * @param s human-readable string | ||
661 | * @param data set to value in binary encoding (will be allocated) | ||
662 | * @param data_size set to number of bytes in @a data | ||
663 | * @return #GNUNET_OK on success | ||
664 | */ | ||
665 | int | ||
666 | GNUNET_RECLAIM_presentation_string_to_value (uint32_t type, | ||
667 | const char *s, | ||
668 | void **data, | ||
669 | size_t *data_size) | ||
670 | { | ||
671 | unsigned int i; | ||
672 | struct Plugin *plugin; | ||
673 | |||
674 | init (); | ||
675 | for (i = 0; i < num_plugins; i++) | ||
676 | { | ||
677 | plugin = credential_plugins[i]; | ||
678 | if (GNUNET_OK == plugin->api->string_to_value_p (plugin->api->cls, | ||
679 | type, | ||
680 | s, | ||
681 | data, | ||
682 | data_size)) | ||
683 | return GNUNET_OK; | ||
684 | } | ||
685 | return GNUNET_SYSERR; | ||
686 | } | ||
687 | |||
688 | |||
689 | /** | ||
690 | * Convert the 'claim' of an presentation to a string | ||
691 | * | ||
692 | * @param type the type of presentation | ||
693 | * @param data claim in binary encoding | ||
694 | * @param data_size number of bytes in @a data | ||
695 | * @return NULL on error, otherwise human-readable representation of the claim | ||
696 | */ | ||
697 | char * | ||
698 | GNUNET_RECLAIM_presentation_value_to_string (uint32_t type, | ||
699 | const void *data, | ||
700 | size_t data_size) | ||
701 | { | ||
702 | unsigned int i; | ||
703 | struct Plugin *plugin; | ||
704 | char *ret; | ||
705 | |||
706 | init (); | ||
707 | for (i = 0; i < num_plugins; i++) | ||
708 | { | ||
709 | plugin = credential_plugins[i]; | ||
710 | if (NULL != (ret = plugin->api->value_to_string_p (plugin->api->cls, | ||
711 | type, | ||
712 | data, | ||
713 | data_size))) | ||
714 | return ret; | ||
715 | } | ||
716 | return NULL; | ||
717 | } | ||
718 | |||
719 | |||
720 | struct GNUNET_RECLAIM_Presentation * | ||
721 | GNUNET_RECLAIM_presentation_new (uint32_t type, | ||
722 | const void *data, | ||
723 | size_t data_size) | ||
724 | { | ||
725 | struct GNUNET_RECLAIM_Presentation *attr; | ||
726 | char *write_ptr; | ||
727 | |||
728 | attr = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_Presentation) | ||
729 | + data_size); | ||
730 | attr->type = type; | ||
731 | attr->data_size = data_size; | ||
732 | write_ptr = (char *) &attr[1]; | ||
733 | GNUNET_memcpy (write_ptr, data, data_size); | ||
734 | attr->data = write_ptr; | ||
735 | return attr; | ||
736 | } | ||
737 | |||
738 | |||
739 | /** | ||
740 | * Get required size for serialization buffer | ||
741 | * | ||
742 | * @param attrs the attribute list to serialize | ||
743 | * @return the required buffer size | ||
744 | */ | ||
745 | size_t | ||
746 | GNUNET_RECLAIM_presentation_list_serialize_get_size ( | ||
747 | const struct GNUNET_RECLAIM_PresentationList *presentations) | ||
748 | { | ||
749 | struct GNUNET_RECLAIM_PresentationListEntry *le; | ||
750 | size_t len = 0; | ||
751 | |||
752 | for (le = presentations->list_head; NULL != le; le = le->next) | ||
753 | { | ||
754 | GNUNET_assert (NULL != le->presentation); | ||
755 | len += GNUNET_RECLAIM_presentation_serialize_get_size (le->presentation); | ||
756 | } | ||
757 | return len; | ||
758 | } | ||
759 | |||
760 | |||
761 | /** | ||
762 | * Serialize an attribute list | ||
763 | * | ||
764 | * @param attrs the attribute list to serialize | ||
765 | * @param result the serialized attribute | ||
766 | * @return length of serialized data | ||
767 | */ | ||
768 | size_t | ||
769 | GNUNET_RECLAIM_presentation_list_serialize ( | ||
770 | const struct GNUNET_RECLAIM_PresentationList *presentations, | ||
771 | char *result) | ||
772 | { | ||
773 | struct GNUNET_RECLAIM_PresentationListEntry *le; | ||
774 | size_t len; | ||
775 | size_t total_len; | ||
776 | char *write_ptr; | ||
777 | write_ptr = result; | ||
778 | total_len = 0; | ||
779 | for (le = presentations->list_head; NULL != le; le = le->next) | ||
780 | { | ||
781 | GNUNET_assert (NULL != le->presentation); | ||
782 | len = GNUNET_RECLAIM_presentation_serialize (le->presentation, write_ptr); | ||
783 | total_len += len; | ||
784 | write_ptr += len; | ||
785 | } | ||
786 | return total_len; | ||
787 | } | ||
788 | |||
789 | |||
790 | /** | ||
791 | * Deserialize an presentation list | ||
792 | * | ||
793 | * @param data the serialized attribute list | ||
794 | * @param data_size the length of the serialized data | ||
795 | * @return a GNUNET_IDENTITY_PROVIDER_AttributeList, must be free'd by caller | ||
796 | */ | ||
797 | struct GNUNET_RECLAIM_PresentationList * | ||
798 | GNUNET_RECLAIM_presentation_list_deserialize (const char *data, size_t | ||
799 | data_size) | ||
800 | { | ||
801 | struct GNUNET_RECLAIM_PresentationList *al; | ||
802 | struct GNUNET_RECLAIM_PresentationListEntry *ale; | ||
803 | size_t att_len; | ||
804 | const char *read_ptr; | ||
805 | |||
806 | al = GNUNET_new (struct GNUNET_RECLAIM_PresentationList); | ||
807 | |||
808 | if (data_size < sizeof(struct Presentation)) | ||
809 | return al; | ||
810 | |||
811 | read_ptr = data; | ||
812 | while (((data + data_size) - read_ptr) >= sizeof(struct Presentation)) | ||
813 | { | ||
814 | ale = GNUNET_new (struct GNUNET_RECLAIM_PresentationListEntry); | ||
815 | ale->presentation = | ||
816 | GNUNET_RECLAIM_presentation_deserialize (read_ptr, | ||
817 | data_size - (read_ptr - data)); | ||
818 | if (NULL == ale->presentation) | ||
819 | { | ||
820 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
821 | "Failed to deserialize malformed presentation.\n"); | ||
822 | GNUNET_free (ale); | ||
823 | return al; | ||
824 | } | ||
825 | GNUNET_CONTAINER_DLL_insert (al->list_head, al->list_tail, ale); | ||
826 | att_len = GNUNET_RECLAIM_presentation_serialize_get_size ( | ||
827 | ale->presentation); | ||
828 | read_ptr += att_len; | ||
829 | } | ||
830 | return al; | ||
831 | } | ||
832 | |||
833 | |||
834 | /** | ||
835 | * Make a (deep) copy of the presentation list | ||
836 | * @param attrs claim list to copy | ||
837 | * @return copied claim list | ||
838 | */ | ||
839 | struct GNUNET_RECLAIM_PresentationList * | ||
840 | GNUNET_RECLAIM_presentation_list_dup ( | ||
841 | const struct GNUNET_RECLAIM_PresentationList *al) | ||
842 | { | ||
843 | struct GNUNET_RECLAIM_PresentationListEntry *ale; | ||
844 | struct GNUNET_RECLAIM_PresentationListEntry *result_ale; | ||
845 | struct GNUNET_RECLAIM_PresentationList *result; | ||
846 | |||
847 | result = GNUNET_new (struct GNUNET_RECLAIM_PresentationList); | ||
848 | for (ale = al->list_head; NULL != ale; ale = ale->next) | ||
849 | { | ||
850 | result_ale = GNUNET_new (struct GNUNET_RECLAIM_PresentationListEntry); | ||
851 | GNUNET_assert (NULL != ale->presentation); | ||
852 | result_ale->presentation = | ||
853 | GNUNET_RECLAIM_presentation_new (ale->presentation->type, | ||
854 | ale->presentation->data, | ||
855 | ale->presentation->data_size); | ||
856 | result_ale->presentation->credential_id = ale->presentation->credential_id; | ||
857 | GNUNET_CONTAINER_DLL_insert (result->list_head, | ||
858 | result->list_tail, | ||
859 | result_ale); | ||
860 | } | ||
861 | return result; | ||
862 | } | ||
863 | |||
864 | |||
865 | /** | ||
866 | * Destroy presentation list | ||
867 | * | ||
868 | * @param attrs list to destroy | ||
869 | */ | ||
870 | void | ||
871 | GNUNET_RECLAIM_presentation_list_destroy ( | ||
872 | struct GNUNET_RECLAIM_PresentationList *al) | ||
873 | { | ||
874 | struct GNUNET_RECLAIM_PresentationListEntry *ale; | ||
875 | struct GNUNET_RECLAIM_PresentationListEntry *tmp_ale; | ||
876 | |||
877 | for (ale = al->list_head; NULL != ale;) | ||
878 | { | ||
879 | if (NULL != ale->presentation) | ||
880 | GNUNET_free (ale->presentation); | ||
881 | tmp_ale = ale; | ||
882 | ale = ale->next; | ||
883 | GNUNET_free (tmp_ale); | ||
884 | } | ||
885 | GNUNET_free (al); | ||
886 | } | ||
887 | |||
888 | |||
889 | /** | ||
890 | * Get required size for serialization buffer | ||
891 | * | ||
892 | * @param attr the presentation to serialize | ||
893 | * @return the required buffer size | ||
894 | */ | ||
895 | size_t | ||
896 | GNUNET_RECLAIM_presentation_serialize_get_size ( | ||
897 | const struct GNUNET_RECLAIM_Presentation *presentation) | ||
898 | { | ||
899 | return sizeof(struct Presentation) + presentation->data_size; | ||
900 | } | ||
901 | |||
902 | |||
903 | /** | ||
904 | * Serialize an presentation | ||
905 | * | ||
906 | * @param attr the presentation to serialize | ||
907 | * @param result the serialized presentation | ||
908 | * @return length of serialized data | ||
909 | */ | ||
910 | size_t | ||
911 | GNUNET_RECLAIM_presentation_serialize ( | ||
912 | const struct GNUNET_RECLAIM_Presentation *presentation, | ||
913 | char *result) | ||
914 | { | ||
915 | struct Presentation *atts; | ||
916 | char *write_ptr; | ||
917 | |||
918 | atts = (struct Presentation *) result; | ||
919 | atts->presentation_type = htons (presentation->type); | ||
920 | atts->credential_id = presentation->credential_id; | ||
921 | write_ptr = (char *) &atts[1]; | ||
922 | GNUNET_memcpy (write_ptr, presentation->data, presentation->data_size); | ||
923 | atts->data_size = htons (presentation->data_size); | ||
924 | |||
925 | return sizeof(struct Presentation) + presentation->data_size; | ||
926 | } | ||
927 | |||
928 | |||
929 | /** | ||
930 | * Deserialize an presentation | ||
931 | * | ||
932 | * @param data the serialized presentation | ||
933 | * @param data_size the length of the serialized data | ||
934 | * | ||
935 | * @return a GNUNET_IDENTITY_PROVIDER_Attribute, must be free'd by caller | ||
936 | */ | ||
937 | struct GNUNET_RECLAIM_Presentation * | ||
938 | GNUNET_RECLAIM_presentation_deserialize (const char *data, size_t data_size) | ||
939 | { | ||
940 | struct GNUNET_RECLAIM_Presentation *presentation; | ||
941 | struct Presentation *atts; | ||
942 | size_t data_len; | ||
943 | char *write_ptr; | ||
944 | |||
945 | if (data_size < sizeof(struct Presentation)) | ||
946 | return NULL; | ||
947 | |||
948 | atts = (struct Presentation *) data; | ||
949 | data_len = ntohs (atts->data_size); | ||
950 | if (data_size < sizeof(struct Presentation) + data_len) | ||
951 | { | ||
952 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
953 | "Buffer too small to deserialize\n"); | ||
954 | return NULL; | ||
955 | } | ||
956 | presentation = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_Presentation) | ||
957 | + data_len); | ||
958 | presentation->type = ntohs (atts->presentation_type); | ||
959 | presentation->credential_id = atts->credential_id; | ||
960 | presentation->data_size = data_len; | ||
961 | |||
962 | write_ptr = (char *) &presentation[1]; | ||
963 | GNUNET_memcpy (write_ptr, &atts[1], data_len); | ||
964 | presentation->data = write_ptr; | ||
965 | return presentation; | ||
966 | } | ||
967 | |||
968 | |||
969 | struct GNUNET_RECLAIM_AttributeList* | ||
970 | GNUNET_RECLAIM_presentation_get_attributes (const struct | ||
971 | GNUNET_RECLAIM_Presentation * | ||
972 | presentation) | ||
973 | { | ||
974 | unsigned int i; | ||
975 | struct Plugin *plugin; | ||
976 | struct GNUNET_RECLAIM_AttributeList *ret; | ||
977 | init (); | ||
978 | for (i = 0; i < num_plugins; i++) | ||
979 | { | ||
980 | plugin = credential_plugins[i]; | ||
981 | if (NULL != | ||
982 | (ret = plugin->api->get_attributes_p (plugin->api->cls, | ||
983 | presentation))) | ||
984 | return ret; | ||
985 | } | ||
986 | return NULL; | ||
987 | } | ||
988 | |||
989 | |||
990 | char* | ||
991 | GNUNET_RECLAIM_presentation_get_issuer (const struct | ||
992 | GNUNET_RECLAIM_Presentation * | ||
993 | presentation) | ||
994 | { | ||
995 | unsigned int i; | ||
996 | struct Plugin *plugin; | ||
997 | char *ret; | ||
998 | init (); | ||
999 | for (i = 0; i < num_plugins; i++) | ||
1000 | { | ||
1001 | plugin = credential_plugins[i]; | ||
1002 | if (NULL != | ||
1003 | (ret = plugin->api->get_issuer_p (plugin->api->cls, | ||
1004 | presentation))) | ||
1005 | return ret; | ||
1006 | } | ||
1007 | return NULL; | ||
1008 | } | ||
1009 | |||
1010 | |||
1011 | int | ||
1012 | GNUNET_RECLAIM_presentation_get_expiration (const struct | ||
1013 | GNUNET_RECLAIM_Presentation * | ||
1014 | presentation, | ||
1015 | struct GNUNET_TIME_Absolute*exp) | ||
1016 | { | ||
1017 | unsigned int i; | ||
1018 | struct Plugin *plugin; | ||
1019 | init (); | ||
1020 | for (i = 0; i < num_plugins; i++) | ||
1021 | { | ||
1022 | plugin = credential_plugins[i]; | ||
1023 | if (GNUNET_OK != plugin->api->get_expiration_p (plugin->api->cls, | ||
1024 | presentation, | ||
1025 | exp)) | ||
1026 | continue; | ||
1027 | return GNUNET_OK; | ||
1028 | } | ||
1029 | return GNUNET_SYSERR; | ||
1030 | } | ||
1031 | |||
1032 | |||
1033 | /** | ||
1034 | * Create a presentation from a credential and a lift of (selected) | ||
1035 | * attributes in the credential. | ||
1036 | * | ||
1037 | * @param cred the credential to use | ||
1038 | * @param attrs the attributes to present from the credential | ||
1039 | * @return the credential presentation presenting the attributes according | ||
1040 | * to the presentation mechanism of the credential | ||
1041 | * or NULL on error. | ||
1042 | */ | ||
1043 | int | ||
1044 | GNUNET_RECLAIM_credential_get_presentation ( | ||
1045 | const struct GNUNET_RECLAIM_Credential *cred, | ||
1046 | const struct GNUNET_RECLAIM_AttributeList *attrs, | ||
1047 | struct GNUNET_RECLAIM_Presentation **presentation) | ||
1048 | { | ||
1049 | unsigned int i; | ||
1050 | struct Plugin *plugin; | ||
1051 | init (); | ||
1052 | for (i = 0; i < num_plugins; i++) | ||
1053 | { | ||
1054 | plugin = credential_plugins[i]; | ||
1055 | if (GNUNET_OK != plugin->api->create_presentation (plugin->api->cls, | ||
1056 | cred, | ||
1057 | attrs, | ||
1058 | presentation)) | ||
1059 | continue; | ||
1060 | (*presentation)->credential_id = cred->id; | ||
1061 | return GNUNET_OK; | ||
1062 | } | ||
1063 | return GNUNET_SYSERR; | ||
1064 | } | ||